xref: /dpdk/drivers/net/cxgbe/cxgbe_main.c (revision d87ba24d1a9f68e01f7dc61d0a979ee9d92ddec0)
183189849SRahul Lakkireddy /*-
283189849SRahul Lakkireddy  *   BSD LICENSE
383189849SRahul Lakkireddy  *
404868e5bSRahul Lakkireddy  *   Copyright(c) 2014-2017 Chelsio Communications.
583189849SRahul Lakkireddy  *   All rights reserved.
683189849SRahul Lakkireddy  *
783189849SRahul Lakkireddy  *   Redistribution and use in source and binary forms, with or without
883189849SRahul Lakkireddy  *   modification, are permitted provided that the following conditions
983189849SRahul Lakkireddy  *   are met:
1083189849SRahul Lakkireddy  *
1183189849SRahul Lakkireddy  *     * Redistributions of source code must retain the above copyright
1283189849SRahul Lakkireddy  *       notice, this list of conditions and the following disclaimer.
1383189849SRahul Lakkireddy  *     * Redistributions in binary form must reproduce the above copyright
1483189849SRahul Lakkireddy  *       notice, this list of conditions and the following disclaimer in
1583189849SRahul Lakkireddy  *       the documentation and/or other materials provided with the
1683189849SRahul Lakkireddy  *       distribution.
1783189849SRahul Lakkireddy  *     * Neither the name of Chelsio Communications nor the names of its
1883189849SRahul Lakkireddy  *       contributors may be used to endorse or promote products derived
1983189849SRahul Lakkireddy  *       from this software without specific prior written permission.
2083189849SRahul Lakkireddy  *
2183189849SRahul Lakkireddy  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2283189849SRahul Lakkireddy  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2383189849SRahul Lakkireddy  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2483189849SRahul Lakkireddy  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2583189849SRahul Lakkireddy  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2683189849SRahul Lakkireddy  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2783189849SRahul Lakkireddy  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2883189849SRahul Lakkireddy  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2983189849SRahul Lakkireddy  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3083189849SRahul Lakkireddy  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3183189849SRahul Lakkireddy  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3283189849SRahul Lakkireddy  */
3383189849SRahul Lakkireddy 
3483189849SRahul Lakkireddy #include <sys/queue.h>
3583189849SRahul Lakkireddy #include <stdio.h>
3683189849SRahul Lakkireddy #include <errno.h>
3783189849SRahul Lakkireddy #include <stdint.h>
3883189849SRahul Lakkireddy #include <string.h>
3983189849SRahul Lakkireddy #include <unistd.h>
4083189849SRahul Lakkireddy #include <stdarg.h>
4183189849SRahul Lakkireddy #include <inttypes.h>
4283189849SRahul Lakkireddy #include <netinet/in.h>
4383189849SRahul Lakkireddy 
4483189849SRahul Lakkireddy #include <rte_byteorder.h>
4583189849SRahul Lakkireddy #include <rte_common.h>
4683189849SRahul Lakkireddy #include <rte_cycles.h>
4783189849SRahul Lakkireddy #include <rte_interrupts.h>
4883189849SRahul Lakkireddy #include <rte_log.h>
4983189849SRahul Lakkireddy #include <rte_debug.h>
5083189849SRahul Lakkireddy #include <rte_pci.h>
5183189849SRahul Lakkireddy #include <rte_atomic.h>
5283189849SRahul Lakkireddy #include <rte_branch_prediction.h>
5383189849SRahul Lakkireddy #include <rte_memory.h>
5483189849SRahul Lakkireddy #include <rte_memzone.h>
5583189849SRahul Lakkireddy #include <rte_tailq.h>
5683189849SRahul Lakkireddy #include <rte_eal.h>
5783189849SRahul Lakkireddy #include <rte_alarm.h>
5883189849SRahul Lakkireddy #include <rte_ether.h>
5983189849SRahul Lakkireddy #include <rte_ethdev.h>
607d012402SJan Blunck #include <rte_ethdev_pci.h>
6183189849SRahul Lakkireddy #include <rte_atomic.h>
6283189849SRahul Lakkireddy #include <rte_malloc.h>
6383189849SRahul Lakkireddy #include <rte_random.h>
6483189849SRahul Lakkireddy #include <rte_dev.h>
6583189849SRahul Lakkireddy 
6683189849SRahul Lakkireddy #include "common.h"
6783189849SRahul Lakkireddy #include "t4_regs.h"
6883189849SRahul Lakkireddy #include "t4_msg.h"
6983189849SRahul Lakkireddy #include "cxgbe.h"
7083189849SRahul Lakkireddy 
7192c8a632SRahul Lakkireddy /*
7292c8a632SRahul Lakkireddy  * Response queue handler for the FW event queue.
7392c8a632SRahul Lakkireddy  */
7492c8a632SRahul Lakkireddy static int fwevtq_handler(struct sge_rspq *q, const __be64 *rsp,
7592c8a632SRahul Lakkireddy 			  __rte_unused const struct pkt_gl *gl)
7692c8a632SRahul Lakkireddy {
7792c8a632SRahul Lakkireddy 	u8 opcode = ((const struct rss_header *)rsp)->opcode;
7892c8a632SRahul Lakkireddy 
7992c8a632SRahul Lakkireddy 	rsp++;                                          /* skip RSS header */
8092c8a632SRahul Lakkireddy 
8192c8a632SRahul Lakkireddy 	/*
8292c8a632SRahul Lakkireddy 	 * FW can send EGR_UPDATEs encapsulated in a CPL_FW4_MSG.
8392c8a632SRahul Lakkireddy 	 */
8492c8a632SRahul Lakkireddy 	if (unlikely(opcode == CPL_FW4_MSG &&
8592c8a632SRahul Lakkireddy 		     ((const struct cpl_fw4_msg *)rsp)->type ==
8692c8a632SRahul Lakkireddy 		      FW_TYPE_RSSCPL)) {
8792c8a632SRahul Lakkireddy 		rsp++;
8892c8a632SRahul Lakkireddy 		opcode = ((const struct rss_header *)rsp)->opcode;
8992c8a632SRahul Lakkireddy 		rsp++;
9092c8a632SRahul Lakkireddy 		if (opcode != CPL_SGE_EGR_UPDATE) {
9192c8a632SRahul Lakkireddy 			dev_err(q->adapter, "unexpected FW4/CPL %#x on FW event queue\n",
9292c8a632SRahul Lakkireddy 				opcode);
9392c8a632SRahul Lakkireddy 			goto out;
9492c8a632SRahul Lakkireddy 		}
9592c8a632SRahul Lakkireddy 	}
9692c8a632SRahul Lakkireddy 
9792c8a632SRahul Lakkireddy 	if (likely(opcode == CPL_SGE_EGR_UPDATE)) {
9892c8a632SRahul Lakkireddy 		/* do nothing */
9992c8a632SRahul Lakkireddy 	} else if (opcode == CPL_FW6_MSG || opcode == CPL_FW4_MSG) {
10092c8a632SRahul Lakkireddy 		const struct cpl_fw6_msg *msg = (const void *)rsp;
10192c8a632SRahul Lakkireddy 
10292c8a632SRahul Lakkireddy 		t4_handle_fw_rpl(q->adapter, msg->data);
10392c8a632SRahul Lakkireddy 	} else {
10492c8a632SRahul Lakkireddy 		dev_err(adapter, "unexpected CPL %#x on FW event queue\n",
10592c8a632SRahul Lakkireddy 			opcode);
10692c8a632SRahul Lakkireddy 	}
10792c8a632SRahul Lakkireddy out:
10892c8a632SRahul Lakkireddy 	return 0;
10992c8a632SRahul Lakkireddy }
11092c8a632SRahul Lakkireddy 
11192c8a632SRahul Lakkireddy int setup_sge_fwevtq(struct adapter *adapter)
11292c8a632SRahul Lakkireddy {
11392c8a632SRahul Lakkireddy 	struct sge *s = &adapter->sge;
11492c8a632SRahul Lakkireddy 	int err = 0;
11592c8a632SRahul Lakkireddy 	int msi_idx = 0;
11692c8a632SRahul Lakkireddy 
11792c8a632SRahul Lakkireddy 	err = t4_sge_alloc_rxq(adapter, &s->fw_evtq, true, adapter->eth_dev,
11892c8a632SRahul Lakkireddy 			       msi_idx, NULL, fwevtq_handler, -1, NULL, 0,
11992c8a632SRahul Lakkireddy 			       rte_socket_id());
12092c8a632SRahul Lakkireddy 	return err;
12192c8a632SRahul Lakkireddy }
12292c8a632SRahul Lakkireddy 
12392c8a632SRahul Lakkireddy static int closest_timer(const struct sge *s, int time)
12492c8a632SRahul Lakkireddy {
12592c8a632SRahul Lakkireddy 	unsigned int i, match = 0;
12692c8a632SRahul Lakkireddy 	int delta, min_delta = INT_MAX;
12792c8a632SRahul Lakkireddy 
12892c8a632SRahul Lakkireddy 	for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
12992c8a632SRahul Lakkireddy 		delta = time - s->timer_val[i];
13092c8a632SRahul Lakkireddy 		if (delta < 0)
13192c8a632SRahul Lakkireddy 			delta = -delta;
13292c8a632SRahul Lakkireddy 		if (delta < min_delta) {
13392c8a632SRahul Lakkireddy 			min_delta = delta;
13492c8a632SRahul Lakkireddy 			match = i;
13592c8a632SRahul Lakkireddy 		}
13692c8a632SRahul Lakkireddy 	}
13792c8a632SRahul Lakkireddy 	return match;
13892c8a632SRahul Lakkireddy }
13992c8a632SRahul Lakkireddy 
14092c8a632SRahul Lakkireddy static int closest_thres(const struct sge *s, int thres)
14192c8a632SRahul Lakkireddy {
14292c8a632SRahul Lakkireddy 	unsigned int i, match = 0;
14392c8a632SRahul Lakkireddy 	int delta, min_delta = INT_MAX;
14492c8a632SRahul Lakkireddy 
14592c8a632SRahul Lakkireddy 	for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
14692c8a632SRahul Lakkireddy 		delta = thres - s->counter_val[i];
14792c8a632SRahul Lakkireddy 		if (delta < 0)
14892c8a632SRahul Lakkireddy 			delta = -delta;
14992c8a632SRahul Lakkireddy 		if (delta < min_delta) {
15092c8a632SRahul Lakkireddy 			min_delta = delta;
15192c8a632SRahul Lakkireddy 			match = i;
15292c8a632SRahul Lakkireddy 		}
15392c8a632SRahul Lakkireddy 	}
15492c8a632SRahul Lakkireddy 	return match;
15592c8a632SRahul Lakkireddy }
15692c8a632SRahul Lakkireddy 
15792c8a632SRahul Lakkireddy /**
15892c8a632SRahul Lakkireddy  * cxgb4_set_rspq_intr_params - set a queue's interrupt holdoff parameters
15992c8a632SRahul Lakkireddy  * @q: the Rx queue
16092c8a632SRahul Lakkireddy  * @us: the hold-off time in us, or 0 to disable timer
16192c8a632SRahul Lakkireddy  * @cnt: the hold-off packet count, or 0 to disable counter
16292c8a632SRahul Lakkireddy  *
16392c8a632SRahul Lakkireddy  * Sets an Rx queue's interrupt hold-off time and packet count.  At least
16492c8a632SRahul Lakkireddy  * one of the two needs to be enabled for the queue to generate interrupts.
16592c8a632SRahul Lakkireddy  */
16692c8a632SRahul Lakkireddy int cxgb4_set_rspq_intr_params(struct sge_rspq *q, unsigned int us,
16792c8a632SRahul Lakkireddy 			       unsigned int cnt)
16892c8a632SRahul Lakkireddy {
16992c8a632SRahul Lakkireddy 	struct adapter *adap = q->adapter;
17092c8a632SRahul Lakkireddy 	unsigned int timer_val;
17192c8a632SRahul Lakkireddy 
17292c8a632SRahul Lakkireddy 	if (cnt) {
17392c8a632SRahul Lakkireddy 		int err;
17492c8a632SRahul Lakkireddy 		u32 v, new_idx;
17592c8a632SRahul Lakkireddy 
17692c8a632SRahul Lakkireddy 		new_idx = closest_thres(&adap->sge, cnt);
17792c8a632SRahul Lakkireddy 		if (q->desc && q->pktcnt_idx != new_idx) {
17892c8a632SRahul Lakkireddy 			/* the queue has already been created, update it */
17992c8a632SRahul Lakkireddy 			v = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) |
18092c8a632SRahul Lakkireddy 			    V_FW_PARAMS_PARAM_X(
18192c8a632SRahul Lakkireddy 			    FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
18292c8a632SRahul Lakkireddy 			    V_FW_PARAMS_PARAM_YZ(q->cntxt_id);
18392c8a632SRahul Lakkireddy 			err = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
18492c8a632SRahul Lakkireddy 					    &v, &new_idx);
18592c8a632SRahul Lakkireddy 			if (err)
18692c8a632SRahul Lakkireddy 				return err;
18792c8a632SRahul Lakkireddy 		}
18892c8a632SRahul Lakkireddy 		q->pktcnt_idx = new_idx;
18992c8a632SRahul Lakkireddy 	}
19092c8a632SRahul Lakkireddy 
19192c8a632SRahul Lakkireddy 	timer_val = (us == 0) ? X_TIMERREG_RESTART_COUNTER :
19292c8a632SRahul Lakkireddy 				closest_timer(&adap->sge, us);
19392c8a632SRahul Lakkireddy 
19492c8a632SRahul Lakkireddy 	if ((us | cnt) == 0)
19592c8a632SRahul Lakkireddy 		q->intr_params = V_QINTR_TIMER_IDX(X_TIMERREG_UPDATE_CIDX);
19692c8a632SRahul Lakkireddy 	else
19792c8a632SRahul Lakkireddy 		q->intr_params = V_QINTR_TIMER_IDX(timer_val) |
19892c8a632SRahul Lakkireddy 				 V_QINTR_CNT_EN(cnt > 0);
19992c8a632SRahul Lakkireddy 	return 0;
20092c8a632SRahul Lakkireddy }
20192c8a632SRahul Lakkireddy 
20292c8a632SRahul Lakkireddy static inline bool is_x_1g_port(const struct link_config *lc)
20392c8a632SRahul Lakkireddy {
204693f715dSHuawei Xie 	return (lc->supported & FW_PORT_CAP_SPEED_1G) != 0;
20592c8a632SRahul Lakkireddy }
20692c8a632SRahul Lakkireddy 
20792c8a632SRahul Lakkireddy static inline bool is_x_10g_port(const struct link_config *lc)
20892c8a632SRahul Lakkireddy {
2099da2a694SRahul Lakkireddy 	unsigned int speeds, high_speeds;
2109da2a694SRahul Lakkireddy 
2119da2a694SRahul Lakkireddy 	speeds = V_FW_PORT_CAP_SPEED(G_FW_PORT_CAP_SPEED(lc->supported));
2129da2a694SRahul Lakkireddy 	high_speeds = speeds & ~(FW_PORT_CAP_SPEED_100M | FW_PORT_CAP_SPEED_1G);
2139da2a694SRahul Lakkireddy 
2149da2a694SRahul Lakkireddy 	return high_speeds != 0;
21592c8a632SRahul Lakkireddy }
21692c8a632SRahul Lakkireddy 
21792c8a632SRahul Lakkireddy inline void init_rspq(struct adapter *adap, struct sge_rspq *q,
21892c8a632SRahul Lakkireddy 		      unsigned int us, unsigned int cnt,
21992c8a632SRahul Lakkireddy 		      unsigned int size, unsigned int iqe_size)
22092c8a632SRahul Lakkireddy {
22192c8a632SRahul Lakkireddy 	q->adapter = adap;
22292c8a632SRahul Lakkireddy 	cxgb4_set_rspq_intr_params(q, us, cnt);
22392c8a632SRahul Lakkireddy 	q->iqe_len = iqe_size;
22492c8a632SRahul Lakkireddy 	q->size = size;
22592c8a632SRahul Lakkireddy }
22692c8a632SRahul Lakkireddy 
22792c8a632SRahul Lakkireddy int cfg_queue_count(struct rte_eth_dev *eth_dev)
22892c8a632SRahul Lakkireddy {
22992c8a632SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
23092c8a632SRahul Lakkireddy 	struct adapter *adap = pi->adapter;
23192c8a632SRahul Lakkireddy 	struct sge *s = &adap->sge;
23292c8a632SRahul Lakkireddy 	unsigned int max_queues = s->max_ethqsets / adap->params.nports;
23392c8a632SRahul Lakkireddy 
23492c8a632SRahul Lakkireddy 	if ((eth_dev->data->nb_rx_queues < 1) ||
23592c8a632SRahul Lakkireddy 	    (eth_dev->data->nb_tx_queues < 1))
23692c8a632SRahul Lakkireddy 		return -EINVAL;
23792c8a632SRahul Lakkireddy 
23892c8a632SRahul Lakkireddy 	if ((eth_dev->data->nb_rx_queues > max_queues) ||
23992c8a632SRahul Lakkireddy 	    (eth_dev->data->nb_tx_queues > max_queues))
24092c8a632SRahul Lakkireddy 		return -EINVAL;
24192c8a632SRahul Lakkireddy 
24292c8a632SRahul Lakkireddy 	if (eth_dev->data->nb_rx_queues > pi->rss_size)
24392c8a632SRahul Lakkireddy 		return -EINVAL;
24492c8a632SRahul Lakkireddy 
24592c8a632SRahul Lakkireddy 	/* We must configure RSS, since config has changed*/
24692c8a632SRahul Lakkireddy 	pi->flags &= ~PORT_RSS_DONE;
24792c8a632SRahul Lakkireddy 
24892c8a632SRahul Lakkireddy 	pi->n_rx_qsets = eth_dev->data->nb_rx_queues;
24992c8a632SRahul Lakkireddy 	pi->n_tx_qsets = eth_dev->data->nb_tx_queues;
25092c8a632SRahul Lakkireddy 
25192c8a632SRahul Lakkireddy 	return 0;
25292c8a632SRahul Lakkireddy }
25392c8a632SRahul Lakkireddy 
25492c8a632SRahul Lakkireddy void cfg_queues(struct rte_eth_dev *eth_dev)
25592c8a632SRahul Lakkireddy {
25692c8a632SRahul Lakkireddy 	struct rte_config *config = rte_eal_get_configuration();
25792c8a632SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
25892c8a632SRahul Lakkireddy 	struct adapter *adap = pi->adapter;
25992c8a632SRahul Lakkireddy 	struct sge *s = &adap->sge;
26092c8a632SRahul Lakkireddy 	unsigned int i, nb_ports = 0, qidx = 0;
26192c8a632SRahul Lakkireddy 	unsigned int q_per_port = 0;
26292c8a632SRahul Lakkireddy 
26392c8a632SRahul Lakkireddy 	if (!(adap->flags & CFG_QUEUES)) {
26492c8a632SRahul Lakkireddy 		for_each_port(adap, i) {
26592c8a632SRahul Lakkireddy 			struct port_info *tpi = adap2pinfo(adap, i);
26692c8a632SRahul Lakkireddy 
26792c8a632SRahul Lakkireddy 			nb_ports += (is_x_10g_port(&tpi->link_cfg)) ||
26892c8a632SRahul Lakkireddy 				     is_x_1g_port(&tpi->link_cfg) ? 1 : 0;
26992c8a632SRahul Lakkireddy 		}
27092c8a632SRahul Lakkireddy 
27192c8a632SRahul Lakkireddy 		/*
27292c8a632SRahul Lakkireddy 		 * We default up to # of cores queues per 1G/10G port.
27392c8a632SRahul Lakkireddy 		 */
27492c8a632SRahul Lakkireddy 		if (nb_ports)
27592c8a632SRahul Lakkireddy 			q_per_port = (MAX_ETH_QSETS -
27692c8a632SRahul Lakkireddy 				     (adap->params.nports - nb_ports)) /
27792c8a632SRahul Lakkireddy 				     nb_ports;
27892c8a632SRahul Lakkireddy 
27992c8a632SRahul Lakkireddy 		if (q_per_port > config->lcore_count)
28092c8a632SRahul Lakkireddy 			q_per_port = config->lcore_count;
28192c8a632SRahul Lakkireddy 
28292c8a632SRahul Lakkireddy 		for_each_port(adap, i) {
28392c8a632SRahul Lakkireddy 			struct port_info *pi = adap2pinfo(adap, i);
28492c8a632SRahul Lakkireddy 
28592c8a632SRahul Lakkireddy 			pi->first_qset = qidx;
28692c8a632SRahul Lakkireddy 
28792c8a632SRahul Lakkireddy 			/* Initially n_rx_qsets == n_tx_qsets */
28892c8a632SRahul Lakkireddy 			pi->n_rx_qsets = (is_x_10g_port(&pi->link_cfg) ||
28992c8a632SRahul Lakkireddy 					  is_x_1g_port(&pi->link_cfg)) ?
29092c8a632SRahul Lakkireddy 					  q_per_port : 1;
29192c8a632SRahul Lakkireddy 			pi->n_tx_qsets = pi->n_rx_qsets;
29292c8a632SRahul Lakkireddy 
29392c8a632SRahul Lakkireddy 			if (pi->n_rx_qsets > pi->rss_size)
29492c8a632SRahul Lakkireddy 				pi->n_rx_qsets = pi->rss_size;
29592c8a632SRahul Lakkireddy 
29692c8a632SRahul Lakkireddy 			qidx += pi->n_rx_qsets;
29792c8a632SRahul Lakkireddy 		}
29892c8a632SRahul Lakkireddy 
29992c8a632SRahul Lakkireddy 		s->max_ethqsets = qidx;
30092c8a632SRahul Lakkireddy 
30192c8a632SRahul Lakkireddy 		for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
30292c8a632SRahul Lakkireddy 			struct sge_eth_rxq *r = &s->ethrxq[i];
30392c8a632SRahul Lakkireddy 
3046c280962SRahul Lakkireddy 			init_rspq(adap, &r->rspq, 5, 32, 1024, 64);
30592c8a632SRahul Lakkireddy 			r->usembufs = 1;
30692c8a632SRahul Lakkireddy 			r->fl.size = (r->usembufs ? 1024 : 72);
30792c8a632SRahul Lakkireddy 		}
30892c8a632SRahul Lakkireddy 
30992c8a632SRahul Lakkireddy 		for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
31092c8a632SRahul Lakkireddy 			s->ethtxq[i].q.size = 1024;
31192c8a632SRahul Lakkireddy 
31292c8a632SRahul Lakkireddy 		init_rspq(adap, &adap->sge.fw_evtq, 0, 0, 1024, 64);
31392c8a632SRahul Lakkireddy 		adap->flags |= CFG_QUEUES;
31492c8a632SRahul Lakkireddy 	}
31592c8a632SRahul Lakkireddy }
31692c8a632SRahul Lakkireddy 
317856505d3SRahul Lakkireddy void cxgbe_stats_get(struct port_info *pi, struct port_stats *stats)
318856505d3SRahul Lakkireddy {
319856505d3SRahul Lakkireddy 	t4_get_port_stats_offset(pi->adapter, pi->tx_chan, stats,
320856505d3SRahul Lakkireddy 				 &pi->stats_base);
321856505d3SRahul Lakkireddy }
322856505d3SRahul Lakkireddy 
323856505d3SRahul Lakkireddy void cxgbe_stats_reset(struct port_info *pi)
324856505d3SRahul Lakkireddy {
325856505d3SRahul Lakkireddy 	t4_clr_port_stats(pi->adapter, pi->tx_chan);
326856505d3SRahul Lakkireddy }
327856505d3SRahul Lakkireddy 
32883189849SRahul Lakkireddy static void setup_memwin(struct adapter *adap)
32983189849SRahul Lakkireddy {
33083189849SRahul Lakkireddy 	u32 mem_win0_base;
33183189849SRahul Lakkireddy 
33283189849SRahul Lakkireddy 	/* For T5, only relative offset inside the PCIe BAR is passed */
33383189849SRahul Lakkireddy 	mem_win0_base = MEMWIN0_BASE;
33483189849SRahul Lakkireddy 
33583189849SRahul Lakkireddy 	/*
33683189849SRahul Lakkireddy 	 * Set up memory window for accessing adapter memory ranges.  (Read
33783189849SRahul Lakkireddy 	 * back MA register to ensure that changes propagate before we attempt
33883189849SRahul Lakkireddy 	 * to use the new values.)
33983189849SRahul Lakkireddy 	 */
34083189849SRahul Lakkireddy 	t4_write_reg(adap,
34183189849SRahul Lakkireddy 		     PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN,
34283189849SRahul Lakkireddy 					 MEMWIN_NIC),
34383189849SRahul Lakkireddy 		     mem_win0_base | V_BIR(0) |
34483189849SRahul Lakkireddy 		     V_WINDOW(ilog2(MEMWIN0_APERTURE) - X_WINDOW_SHIFT));
34583189849SRahul Lakkireddy 	t4_read_reg(adap,
34683189849SRahul Lakkireddy 		    PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN,
34783189849SRahul Lakkireddy 					MEMWIN_NIC));
34883189849SRahul Lakkireddy }
34983189849SRahul Lakkireddy 
35092c8a632SRahul Lakkireddy static int init_rss(struct adapter *adap)
35192c8a632SRahul Lakkireddy {
35292c8a632SRahul Lakkireddy 	unsigned int i;
35392c8a632SRahul Lakkireddy 	int err;
35492c8a632SRahul Lakkireddy 
35592c8a632SRahul Lakkireddy 	err = t4_init_rss_mode(adap, adap->mbox);
35692c8a632SRahul Lakkireddy 	if (err)
35792c8a632SRahul Lakkireddy 		return err;
35892c8a632SRahul Lakkireddy 
35992c8a632SRahul Lakkireddy 	for_each_port(adap, i) {
36092c8a632SRahul Lakkireddy 		struct port_info *pi = adap2pinfo(adap, i);
36192c8a632SRahul Lakkireddy 
3628dca8cc5SRahul Lakkireddy 		pi->rss = rte_zmalloc(NULL, pi->rss_size * sizeof(u16), 0);
36392c8a632SRahul Lakkireddy 		if (!pi->rss)
36492c8a632SRahul Lakkireddy 			return -ENOMEM;
36592c8a632SRahul Lakkireddy 	}
36692c8a632SRahul Lakkireddy 	return 0;
36792c8a632SRahul Lakkireddy }
36892c8a632SRahul Lakkireddy 
369c962618cSRahul Lakkireddy /**
370c962618cSRahul Lakkireddy  * Dump basic information about the adapter.
371c962618cSRahul Lakkireddy  */
372c962618cSRahul Lakkireddy static void print_adapter_info(struct adapter *adap)
373c962618cSRahul Lakkireddy {
374c962618cSRahul Lakkireddy 	/**
375c962618cSRahul Lakkireddy 	 * Hardware/Firmware/etc. Version/Revision IDs.
376c962618cSRahul Lakkireddy 	 */
377c962618cSRahul Lakkireddy 	t4_dump_version_info(adap);
378c962618cSRahul Lakkireddy }
379c962618cSRahul Lakkireddy 
38083189849SRahul Lakkireddy static void print_port_info(struct adapter *adap)
38183189849SRahul Lakkireddy {
38283189849SRahul Lakkireddy 	int i;
38383189849SRahul Lakkireddy 	char buf[80];
38483189849SRahul Lakkireddy 	struct rte_pci_addr *loc = &adap->pdev->addr;
38583189849SRahul Lakkireddy 
38683189849SRahul Lakkireddy 	for_each_port(adap, i) {
38783189849SRahul Lakkireddy 		const struct port_info *pi = &adap->port[i];
38883189849SRahul Lakkireddy 		char *bufp = buf;
38983189849SRahul Lakkireddy 
39083189849SRahul Lakkireddy 		if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_100M)
3919da2a694SRahul Lakkireddy 			bufp += sprintf(bufp, "100M/");
39283189849SRahul Lakkireddy 		if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_1G)
3939da2a694SRahul Lakkireddy 			bufp += sprintf(bufp, "1G/");
39483189849SRahul Lakkireddy 		if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_10G)
39583189849SRahul Lakkireddy 			bufp += sprintf(bufp, "10G/");
3969da2a694SRahul Lakkireddy 		if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_25G)
3979da2a694SRahul Lakkireddy 			bufp += sprintf(bufp, "25G/");
39883189849SRahul Lakkireddy 		if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_40G)
39983189849SRahul Lakkireddy 			bufp += sprintf(bufp, "40G/");
4009da2a694SRahul Lakkireddy 		if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_100G)
4019da2a694SRahul Lakkireddy 			bufp += sprintf(bufp, "100G/");
40283189849SRahul Lakkireddy 		if (bufp != buf)
40383189849SRahul Lakkireddy 			--bufp;
40483189849SRahul Lakkireddy 		sprintf(bufp, "BASE-%s",
405efa8a43eSBruce Richardson 			t4_get_port_type_description(
406efa8a43eSBruce Richardson 					(enum fw_port_type)pi->port_type));
40783189849SRahul Lakkireddy 
40883189849SRahul Lakkireddy 		dev_info(adap,
40983189849SRahul Lakkireddy 			 " " PCI_PRI_FMT " Chelsio rev %d %s %s\n",
41083189849SRahul Lakkireddy 			 loc->domain, loc->bus, loc->devid, loc->function,
41183189849SRahul Lakkireddy 			 CHELSIO_CHIP_RELEASE(adap->params.chip), buf,
41283189849SRahul Lakkireddy 			 (adap->flags & USING_MSIX) ? " MSI-X" :
41383189849SRahul Lakkireddy 			 (adap->flags & USING_MSI) ? " MSI" : "");
41483189849SRahul Lakkireddy 	}
41583189849SRahul Lakkireddy }
41683189849SRahul Lakkireddy 
41783189849SRahul Lakkireddy /*
41883189849SRahul Lakkireddy  * Tweak configuration based on system architecture, etc.  Most of these have
41983189849SRahul Lakkireddy  * defaults assigned to them by Firmware Configuration Files (if we're using
42083189849SRahul Lakkireddy  * them) but need to be explicitly set if we're using hard-coded
42183189849SRahul Lakkireddy  * initialization. So these are essentially common tweaks/settings for
42283189849SRahul Lakkireddy  * Configuration Files and hard-coded initialization ...
42383189849SRahul Lakkireddy  */
42483189849SRahul Lakkireddy static int adap_init0_tweaks(struct adapter *adapter)
42583189849SRahul Lakkireddy {
42683189849SRahul Lakkireddy 	u8 rx_dma_offset;
42783189849SRahul Lakkireddy 
42883189849SRahul Lakkireddy 	/*
42983189849SRahul Lakkireddy 	 * Fix up various Host-Dependent Parameters like Page Size, Cache
43083189849SRahul Lakkireddy 	 * Line Size, etc.  The firmware default is for a 4KB Page Size and
43183189849SRahul Lakkireddy 	 * 64B Cache Line Size ...
43283189849SRahul Lakkireddy 	 */
4331f8613f1SRahul Lakkireddy 	t4_fixup_host_params_compat(adapter, CXGBE_PAGE_SIZE, L1_CACHE_BYTES,
43483189849SRahul Lakkireddy 				    T5_LAST_REV);
43583189849SRahul Lakkireddy 
43683189849SRahul Lakkireddy 	/*
43783189849SRahul Lakkireddy 	 * Keep the chip default offset to deliver Ingress packets into our
43883189849SRahul Lakkireddy 	 * DMA buffers to zero
43983189849SRahul Lakkireddy 	 */
44083189849SRahul Lakkireddy 	rx_dma_offset = 0;
44183189849SRahul Lakkireddy 	t4_set_reg_field(adapter, A_SGE_CONTROL, V_PKTSHIFT(M_PKTSHIFT),
44283189849SRahul Lakkireddy 			 V_PKTSHIFT(rx_dma_offset));
44383189849SRahul Lakkireddy 
444bf89cbedSRahul Lakkireddy 	t4_set_reg_field(adapter, A_SGE_FLM_CFG,
445bf89cbedSRahul Lakkireddy 			 V_CREDITCNT(M_CREDITCNT) | M_CREDITCNTPACKING,
446bf89cbedSRahul Lakkireddy 			 V_CREDITCNT(3) | V_CREDITCNTPACKING(1));
447bf89cbedSRahul Lakkireddy 
4486c280962SRahul Lakkireddy 	t4_set_reg_field(adapter, A_SGE_INGRESS_RX_THRESHOLD,
4496c280962SRahul Lakkireddy 			 V_THRESHOLD_3(M_THRESHOLD_3), V_THRESHOLD_3(32U));
4506c280962SRahul Lakkireddy 
451bf89cbedSRahul Lakkireddy 	t4_set_reg_field(adapter, A_SGE_CONTROL2, V_IDMAARBROUNDROBIN(1U),
452bf89cbedSRahul Lakkireddy 			 V_IDMAARBROUNDROBIN(1U));
453bf89cbedSRahul Lakkireddy 
45483189849SRahul Lakkireddy 	/*
45583189849SRahul Lakkireddy 	 * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
45683189849SRahul Lakkireddy 	 * adds the pseudo header itself.
45783189849SRahul Lakkireddy 	 */
45883189849SRahul Lakkireddy 	t4_tp_wr_bits_indirect(adapter, A_TP_INGRESS_CONFIG,
45983189849SRahul Lakkireddy 			       F_CSUM_HAS_PSEUDO_HDR, 0);
46083189849SRahul Lakkireddy 
46183189849SRahul Lakkireddy 	return 0;
46283189849SRahul Lakkireddy }
46383189849SRahul Lakkireddy 
46483189849SRahul Lakkireddy /*
46583189849SRahul Lakkireddy  * Attempt to initialize the adapter via a Firmware Configuration File.
46683189849SRahul Lakkireddy  */
46783189849SRahul Lakkireddy static int adap_init0_config(struct adapter *adapter, int reset)
46883189849SRahul Lakkireddy {
46983189849SRahul Lakkireddy 	struct fw_caps_config_cmd caps_cmd;
47083189849SRahul Lakkireddy 	unsigned long mtype = 0, maddr = 0;
47183189849SRahul Lakkireddy 	u32 finiver, finicsum, cfcsum;
47283189849SRahul Lakkireddy 	int ret;
47383189849SRahul Lakkireddy 	int config_issued = 0;
47483189849SRahul Lakkireddy 	int cfg_addr;
47583189849SRahul Lakkireddy 	char config_name[20];
47683189849SRahul Lakkireddy 
47783189849SRahul Lakkireddy 	/*
47883189849SRahul Lakkireddy 	 * Reset device if necessary.
47983189849SRahul Lakkireddy 	 */
48083189849SRahul Lakkireddy 	if (reset) {
48183189849SRahul Lakkireddy 		ret = t4_fw_reset(adapter, adapter->mbox,
48283189849SRahul Lakkireddy 				  F_PIORSTMODE | F_PIORST);
48383189849SRahul Lakkireddy 		if (ret < 0) {
48483189849SRahul Lakkireddy 			dev_warn(adapter, "Firmware reset failed, error %d\n",
48583189849SRahul Lakkireddy 				 -ret);
48683189849SRahul Lakkireddy 			goto bye;
48783189849SRahul Lakkireddy 		}
48883189849SRahul Lakkireddy 	}
48983189849SRahul Lakkireddy 
49083189849SRahul Lakkireddy 	cfg_addr = t4_flash_cfg_addr(adapter);
49183189849SRahul Lakkireddy 	if (cfg_addr < 0) {
49283189849SRahul Lakkireddy 		ret = cfg_addr;
49383189849SRahul Lakkireddy 		dev_warn(adapter, "Finding address for firmware config file in flash failed, error %d\n",
49483189849SRahul Lakkireddy 			 -ret);
49583189849SRahul Lakkireddy 		goto bye;
49683189849SRahul Lakkireddy 	}
49783189849SRahul Lakkireddy 
49883189849SRahul Lakkireddy 	strcpy(config_name, "On Flash");
49983189849SRahul Lakkireddy 	mtype = FW_MEMTYPE_CF_FLASH;
50083189849SRahul Lakkireddy 	maddr = cfg_addr;
50183189849SRahul Lakkireddy 
50283189849SRahul Lakkireddy 	/*
50383189849SRahul Lakkireddy 	 * Issue a Capability Configuration command to the firmware to get it
50483189849SRahul Lakkireddy 	 * to parse the Configuration File.  We don't use t4_fw_config_file()
50583189849SRahul Lakkireddy 	 * because we want the ability to modify various features after we've
50683189849SRahul Lakkireddy 	 * processed the configuration file ...
50783189849SRahul Lakkireddy 	 */
50883189849SRahul Lakkireddy 	memset(&caps_cmd, 0, sizeof(caps_cmd));
50983189849SRahul Lakkireddy 	caps_cmd.op_to_write = cpu_to_be32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
51083189849SRahul Lakkireddy 					   F_FW_CMD_REQUEST | F_FW_CMD_READ);
51183189849SRahul Lakkireddy 	caps_cmd.cfvalid_to_len16 =
51283189849SRahul Lakkireddy 		cpu_to_be32(F_FW_CAPS_CONFIG_CMD_CFVALID |
51383189849SRahul Lakkireddy 			    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
51483189849SRahul Lakkireddy 			    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(maddr >> 16) |
51583189849SRahul Lakkireddy 			    FW_LEN16(caps_cmd));
51683189849SRahul Lakkireddy 	ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
51783189849SRahul Lakkireddy 			 &caps_cmd);
51883189849SRahul Lakkireddy 	/*
51983189849SRahul Lakkireddy 	 * If the CAPS_CONFIG failed with an ENOENT (for a Firmware
52083189849SRahul Lakkireddy 	 * Configuration File in FLASH), our last gasp effort is to use the
52183189849SRahul Lakkireddy 	 * Firmware Configuration File which is embedded in the firmware.  A
52283189849SRahul Lakkireddy 	 * very few early versions of the firmware didn't have one embedded
52383189849SRahul Lakkireddy 	 * but we can ignore those.
52483189849SRahul Lakkireddy 	 */
52583189849SRahul Lakkireddy 	if (ret == -ENOENT) {
52683189849SRahul Lakkireddy 		dev_info(adapter, "%s: Going for embedded config in firmware..\n",
52783189849SRahul Lakkireddy 			 __func__);
52883189849SRahul Lakkireddy 
52983189849SRahul Lakkireddy 		memset(&caps_cmd, 0, sizeof(caps_cmd));
53083189849SRahul Lakkireddy 		caps_cmd.op_to_write =
53183189849SRahul Lakkireddy 			cpu_to_be32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
53283189849SRahul Lakkireddy 				    F_FW_CMD_REQUEST | F_FW_CMD_READ);
53383189849SRahul Lakkireddy 		caps_cmd.cfvalid_to_len16 = cpu_to_be32(FW_LEN16(caps_cmd));
53483189849SRahul Lakkireddy 		ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd,
53583189849SRahul Lakkireddy 				 sizeof(caps_cmd), &caps_cmd);
53683189849SRahul Lakkireddy 		strcpy(config_name, "Firmware Default");
53783189849SRahul Lakkireddy 	}
53883189849SRahul Lakkireddy 
53983189849SRahul Lakkireddy 	config_issued = 1;
54083189849SRahul Lakkireddy 	if (ret < 0)
54183189849SRahul Lakkireddy 		goto bye;
54283189849SRahul Lakkireddy 
54383189849SRahul Lakkireddy 	finiver = be32_to_cpu(caps_cmd.finiver);
54483189849SRahul Lakkireddy 	finicsum = be32_to_cpu(caps_cmd.finicsum);
54583189849SRahul Lakkireddy 	cfcsum = be32_to_cpu(caps_cmd.cfcsum);
54683189849SRahul Lakkireddy 	if (finicsum != cfcsum)
54783189849SRahul Lakkireddy 		dev_warn(adapter, "Configuration File checksum mismatch: [fini] csum=%#x, computed csum=%#x\n",
54883189849SRahul Lakkireddy 			 finicsum, cfcsum);
54983189849SRahul Lakkireddy 
55083189849SRahul Lakkireddy 	/*
55183189849SRahul Lakkireddy 	 * If we're a pure NIC driver then disable all offloading facilities.
55283189849SRahul Lakkireddy 	 * This will allow the firmware to optimize aspects of the hardware
55383189849SRahul Lakkireddy 	 * configuration which will result in improved performance.
55483189849SRahul Lakkireddy 	 */
55583189849SRahul Lakkireddy 	caps_cmd.niccaps &= cpu_to_be16(~(FW_CAPS_CONFIG_NIC_HASHFILTER |
55683189849SRahul Lakkireddy 					  FW_CAPS_CONFIG_NIC_ETHOFLD));
55783189849SRahul Lakkireddy 	caps_cmd.toecaps = 0;
55883189849SRahul Lakkireddy 	caps_cmd.iscsicaps = 0;
55983189849SRahul Lakkireddy 	caps_cmd.rdmacaps = 0;
56083189849SRahul Lakkireddy 	caps_cmd.fcoecaps = 0;
56183189849SRahul Lakkireddy 
56283189849SRahul Lakkireddy 	/*
56383189849SRahul Lakkireddy 	 * And now tell the firmware to use the configuration we just loaded.
56483189849SRahul Lakkireddy 	 */
56583189849SRahul Lakkireddy 	caps_cmd.op_to_write = cpu_to_be32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
56683189849SRahul Lakkireddy 					   F_FW_CMD_REQUEST | F_FW_CMD_WRITE);
56783189849SRahul Lakkireddy 	caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
56883189849SRahul Lakkireddy 	ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
56983189849SRahul Lakkireddy 			 NULL);
57083189849SRahul Lakkireddy 	if (ret < 0) {
57183189849SRahul Lakkireddy 		dev_warn(adapter, "Unable to finalize Firmware Capabilities %d\n",
57283189849SRahul Lakkireddy 			 -ret);
57383189849SRahul Lakkireddy 		goto bye;
57483189849SRahul Lakkireddy 	}
57583189849SRahul Lakkireddy 
57683189849SRahul Lakkireddy 	/*
57783189849SRahul Lakkireddy 	 * Tweak configuration based on system architecture, etc.
57883189849SRahul Lakkireddy 	 */
57983189849SRahul Lakkireddy 	ret = adap_init0_tweaks(adapter);
58083189849SRahul Lakkireddy 	if (ret < 0) {
58183189849SRahul Lakkireddy 		dev_warn(adapter, "Unable to do init0-tweaks %d\n", -ret);
58283189849SRahul Lakkireddy 		goto bye;
58383189849SRahul Lakkireddy 	}
58483189849SRahul Lakkireddy 
58583189849SRahul Lakkireddy 	/*
58683189849SRahul Lakkireddy 	 * And finally tell the firmware to initialize itself using the
58783189849SRahul Lakkireddy 	 * parameters from the Configuration File.
58883189849SRahul Lakkireddy 	 */
58983189849SRahul Lakkireddy 	ret = t4_fw_initialize(adapter, adapter->mbox);
59083189849SRahul Lakkireddy 	if (ret < 0) {
59183189849SRahul Lakkireddy 		dev_warn(adapter, "Initializing Firmware failed, error %d\n",
59283189849SRahul Lakkireddy 			 -ret);
59383189849SRahul Lakkireddy 		goto bye;
59483189849SRahul Lakkireddy 	}
59583189849SRahul Lakkireddy 
59683189849SRahul Lakkireddy 	/*
59783189849SRahul Lakkireddy 	 * Return successfully and note that we're operating with parameters
59883189849SRahul Lakkireddy 	 * not supplied by the driver, rather than from hard-wired
59983189849SRahul Lakkireddy 	 * initialization constants burried in the driver.
60083189849SRahul Lakkireddy 	 */
60183189849SRahul Lakkireddy 	dev_info(adapter,
60283189849SRahul Lakkireddy 		 "Successfully configured using Firmware Configuration File \"%s\", version %#x, computed checksum %#x\n",
60383189849SRahul Lakkireddy 		 config_name, finiver, cfcsum);
60483189849SRahul Lakkireddy 
60583189849SRahul Lakkireddy 	return 0;
60683189849SRahul Lakkireddy 
60783189849SRahul Lakkireddy 	/*
60883189849SRahul Lakkireddy 	 * Something bad happened.  Return the error ...  (If the "error"
60983189849SRahul Lakkireddy 	 * is that there's no Configuration File on the adapter we don't
61083189849SRahul Lakkireddy 	 * want to issue a warning since this is fairly common.)
61183189849SRahul Lakkireddy 	 */
61283189849SRahul Lakkireddy bye:
61383189849SRahul Lakkireddy 	if (config_issued && ret != -ENOENT)
61483189849SRahul Lakkireddy 		dev_warn(adapter, "\"%s\" configuration file error %d\n",
61583189849SRahul Lakkireddy 			 config_name, -ret);
61683189849SRahul Lakkireddy 
61783189849SRahul Lakkireddy 	dev_debug(adapter, "%s: returning ret = %d ..\n", __func__, ret);
61883189849SRahul Lakkireddy 	return ret;
61983189849SRahul Lakkireddy }
62083189849SRahul Lakkireddy 
62183189849SRahul Lakkireddy static int adap_init0(struct adapter *adap)
62283189849SRahul Lakkireddy {
62383189849SRahul Lakkireddy 	int ret = 0;
62483189849SRahul Lakkireddy 	u32 v, port_vec;
62583189849SRahul Lakkireddy 	enum dev_state state;
62683189849SRahul Lakkireddy 	u32 params[7], val[7];
62783189849SRahul Lakkireddy 	int reset = 1;
62883189849SRahul Lakkireddy 	int mbox = adap->mbox;
62983189849SRahul Lakkireddy 
63083189849SRahul Lakkireddy 	/*
63183189849SRahul Lakkireddy 	 * Contact FW, advertising Master capability.
63283189849SRahul Lakkireddy 	 */
63383189849SRahul Lakkireddy 	ret = t4_fw_hello(adap, adap->mbox, adap->mbox, MASTER_MAY, &state);
63483189849SRahul Lakkireddy 	if (ret < 0) {
63583189849SRahul Lakkireddy 		dev_err(adap, "%s: could not connect to FW, error %d\n",
63683189849SRahul Lakkireddy 			__func__, -ret);
63783189849SRahul Lakkireddy 		goto bye;
63883189849SRahul Lakkireddy 	}
63983189849SRahul Lakkireddy 
64083189849SRahul Lakkireddy 	CXGBE_DEBUG_MBOX(adap, "%s: adap->mbox = %d; ret = %d\n", __func__,
64183189849SRahul Lakkireddy 			 adap->mbox, ret);
64283189849SRahul Lakkireddy 
64383189849SRahul Lakkireddy 	if (ret == mbox)
64483189849SRahul Lakkireddy 		adap->flags |= MASTER_PF;
64583189849SRahul Lakkireddy 
64683189849SRahul Lakkireddy 	if (state == DEV_STATE_INIT) {
64783189849SRahul Lakkireddy 		/*
64883189849SRahul Lakkireddy 		 * Force halt and reset FW because a previous instance may have
64983189849SRahul Lakkireddy 		 * exited abnormally without properly shutting down
65083189849SRahul Lakkireddy 		 */
65183189849SRahul Lakkireddy 		ret = t4_fw_halt(adap, adap->mbox, reset);
65283189849SRahul Lakkireddy 		if (ret < 0) {
65383189849SRahul Lakkireddy 			dev_err(adap, "Failed to halt. Exit.\n");
65483189849SRahul Lakkireddy 			goto bye;
65583189849SRahul Lakkireddy 		}
65683189849SRahul Lakkireddy 
65783189849SRahul Lakkireddy 		ret = t4_fw_restart(adap, adap->mbox, reset);
65883189849SRahul Lakkireddy 		if (ret < 0) {
65983189849SRahul Lakkireddy 			dev_err(adap, "Failed to restart. Exit.\n");
66083189849SRahul Lakkireddy 			goto bye;
66183189849SRahul Lakkireddy 		}
662efa8a43eSBruce Richardson 		state = (enum dev_state)((unsigned)state & ~DEV_STATE_INIT);
66383189849SRahul Lakkireddy 	}
66483189849SRahul Lakkireddy 
665c962618cSRahul Lakkireddy 	t4_get_version_info(adap);
66683189849SRahul Lakkireddy 
66783189849SRahul Lakkireddy 	ret = t4_get_core_clock(adap, &adap->params.vpd);
66883189849SRahul Lakkireddy 	if (ret < 0) {
66983189849SRahul Lakkireddy 		dev_err(adap, "%s: could not get core clock, error %d\n",
67083189849SRahul Lakkireddy 			__func__, -ret);
67183189849SRahul Lakkireddy 		goto bye;
67283189849SRahul Lakkireddy 	}
67383189849SRahul Lakkireddy 
67483189849SRahul Lakkireddy 	/*
67583189849SRahul Lakkireddy 	 * If the firmware is initialized already (and we're not forcing a
67683189849SRahul Lakkireddy 	 * master initialization), note that we're living with existing
67783189849SRahul Lakkireddy 	 * adapter parameters.  Otherwise, it's time to try initializing the
67883189849SRahul Lakkireddy 	 * adapter ...
67983189849SRahul Lakkireddy 	 */
68083189849SRahul Lakkireddy 	if (state == DEV_STATE_INIT) {
68183189849SRahul Lakkireddy 		dev_info(adap, "Coming up as %s: Adapter already initialized\n",
68283189849SRahul Lakkireddy 			 adap->flags & MASTER_PF ? "MASTER" : "SLAVE");
68383189849SRahul Lakkireddy 	} else {
68483189849SRahul Lakkireddy 		dev_info(adap, "Coming up as MASTER: Initializing adapter\n");
68583189849SRahul Lakkireddy 
68683189849SRahul Lakkireddy 		ret = adap_init0_config(adap, reset);
68783189849SRahul Lakkireddy 		if (ret == -ENOENT) {
68883189849SRahul Lakkireddy 			dev_err(adap,
68983189849SRahul Lakkireddy 				"No Configuration File present on adapter. Using hard-wired configuration parameters.\n");
69083189849SRahul Lakkireddy 			goto bye;
69183189849SRahul Lakkireddy 		}
69283189849SRahul Lakkireddy 	}
69383189849SRahul Lakkireddy 	if (ret < 0) {
69483189849SRahul Lakkireddy 		dev_err(adap, "could not initialize adapter, error %d\n", -ret);
69583189849SRahul Lakkireddy 		goto bye;
69683189849SRahul Lakkireddy 	}
69783189849SRahul Lakkireddy 
6985eb55bf8SRahul Lakkireddy 	/* Find out what ports are available to us. */
6995eb55bf8SRahul Lakkireddy 	v = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
7005eb55bf8SRahul Lakkireddy 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_PORTVEC);
7015eb55bf8SRahul Lakkireddy 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, &v, &port_vec);
7025eb55bf8SRahul Lakkireddy 	if (ret < 0) {
7035eb55bf8SRahul Lakkireddy 		dev_err(adap, "%s: failure in t4_query_params; error = %d\n",
7045eb55bf8SRahul Lakkireddy 			__func__, ret);
7055eb55bf8SRahul Lakkireddy 		goto bye;
7065eb55bf8SRahul Lakkireddy 	}
7075eb55bf8SRahul Lakkireddy 
7085eb55bf8SRahul Lakkireddy 	adap->params.nports = hweight32(port_vec);
7095eb55bf8SRahul Lakkireddy 	adap->params.portvec = port_vec;
7105eb55bf8SRahul Lakkireddy 
7115eb55bf8SRahul Lakkireddy 	dev_debug(adap, "%s: adap->params.nports = %u\n", __func__,
7125eb55bf8SRahul Lakkireddy 		  adap->params.nports);
7135eb55bf8SRahul Lakkireddy 
71483189849SRahul Lakkireddy 	/*
71583189849SRahul Lakkireddy 	 * Give the SGE code a chance to pull in anything that it needs ...
71683189849SRahul Lakkireddy 	 * Note that this must be called after we retrieve our VPD parameters
71783189849SRahul Lakkireddy 	 * in order to know how to convert core ticks to seconds, etc.
71883189849SRahul Lakkireddy 	 */
71983189849SRahul Lakkireddy 	ret = t4_sge_init(adap);
72083189849SRahul Lakkireddy 	if (ret < 0) {
72183189849SRahul Lakkireddy 		dev_err(adap, "t4_sge_init failed with error %d\n",
72283189849SRahul Lakkireddy 			-ret);
72383189849SRahul Lakkireddy 		goto bye;
72483189849SRahul Lakkireddy 	}
72583189849SRahul Lakkireddy 
72683189849SRahul Lakkireddy 	/*
72783189849SRahul Lakkireddy 	 * Grab some of our basic fundamental operating parameters.
72883189849SRahul Lakkireddy 	 */
72983189849SRahul Lakkireddy #define FW_PARAM_DEV(param) \
73083189849SRahul Lakkireddy 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) | \
73183189849SRahul Lakkireddy 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_##param))
73283189849SRahul Lakkireddy 
73383189849SRahul Lakkireddy #define FW_PARAM_PFVF(param) \
73483189849SRahul Lakkireddy 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_PFVF) | \
73583189849SRahul Lakkireddy 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_PFVF_##param) |  \
73683189849SRahul Lakkireddy 	 V_FW_PARAMS_PARAM_Y(0) | \
73783189849SRahul Lakkireddy 	 V_FW_PARAMS_PARAM_Z(0))
73883189849SRahul Lakkireddy 
73983189849SRahul Lakkireddy 	/* If we're running on newer firmware, let it know that we're
74083189849SRahul Lakkireddy 	 * prepared to deal with encapsulated CPL messages.  Older
74183189849SRahul Lakkireddy 	 * firmware won't understand this and we'll just get
74283189849SRahul Lakkireddy 	 * unencapsulated messages ...
74383189849SRahul Lakkireddy 	 */
74483189849SRahul Lakkireddy 	params[0] = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
74583189849SRahul Lakkireddy 	val[0] = 1;
74683189849SRahul Lakkireddy 	(void)t4_set_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
74783189849SRahul Lakkireddy 
74883189849SRahul Lakkireddy 	/*
74983189849SRahul Lakkireddy 	 * Find out whether we're allowed to use the T5+ ULPTX MEMWRITE DSGL
75083189849SRahul Lakkireddy 	 * capability.  Earlier versions of the firmware didn't have the
75183189849SRahul Lakkireddy 	 * ULPTX_MEMWRITE_DSGL so we'll interpret a query failure as no
75283189849SRahul Lakkireddy 	 * permission to use ULPTX MEMWRITE DSGL.
75383189849SRahul Lakkireddy 	 */
75483189849SRahul Lakkireddy 	if (is_t4(adap->params.chip)) {
75583189849SRahul Lakkireddy 		adap->params.ulptx_memwrite_dsgl = false;
75683189849SRahul Lakkireddy 	} else {
75783189849SRahul Lakkireddy 		params[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
75883189849SRahul Lakkireddy 		ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
75983189849SRahul Lakkireddy 				      1, params, val);
76083189849SRahul Lakkireddy 		adap->params.ulptx_memwrite_dsgl = (ret == 0 && val[0] != 0);
76183189849SRahul Lakkireddy 	}
76283189849SRahul Lakkireddy 
76383189849SRahul Lakkireddy 	/*
76483189849SRahul Lakkireddy 	 * The MTU/MSS Table is initialized by now, so load their values.  If
76583189849SRahul Lakkireddy 	 * we're initializing the adapter, then we'll make any modifications
76683189849SRahul Lakkireddy 	 * we want to the MTU/MSS Table and also initialize the congestion
76783189849SRahul Lakkireddy 	 * parameters.
76883189849SRahul Lakkireddy 	 */
76983189849SRahul Lakkireddy 	t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
77083189849SRahul Lakkireddy 	if (state != DEV_STATE_INIT) {
77183189849SRahul Lakkireddy 		int i;
77283189849SRahul Lakkireddy 
77383189849SRahul Lakkireddy 		/*
77483189849SRahul Lakkireddy 		 * The default MTU Table contains values 1492 and 1500.
77583189849SRahul Lakkireddy 		 * However, for TCP, it's better to have two values which are
77683189849SRahul Lakkireddy 		 * a multiple of 8 +/- 4 bytes apart near this popular MTU.
77783189849SRahul Lakkireddy 		 * This allows us to have a TCP Data Payload which is a
77883189849SRahul Lakkireddy 		 * multiple of 8 regardless of what combination of TCP Options
77983189849SRahul Lakkireddy 		 * are in use (always a multiple of 4 bytes) which is
78083189849SRahul Lakkireddy 		 * important for performance reasons.  For instance, if no
78183189849SRahul Lakkireddy 		 * options are in use, then we have a 20-byte IP header and a
78283189849SRahul Lakkireddy 		 * 20-byte TCP header.  In this case, a 1500-byte MSS would
78383189849SRahul Lakkireddy 		 * result in a TCP Data Payload of 1500 - 40 == 1460 bytes
78483189849SRahul Lakkireddy 		 * which is not a multiple of 8.  So using an MSS of 1488 in
78583189849SRahul Lakkireddy 		 * this case results in a TCP Data Payload of 1448 bytes which
78683189849SRahul Lakkireddy 		 * is a multiple of 8.  On the other hand, if 12-byte TCP Time
78783189849SRahul Lakkireddy 		 * Stamps have been negotiated, then an MTU of 1500 bytes
78883189849SRahul Lakkireddy 		 * results in a TCP Data Payload of 1448 bytes which, as
78983189849SRahul Lakkireddy 		 * above, is a multiple of 8 bytes ...
79083189849SRahul Lakkireddy 		 */
79183189849SRahul Lakkireddy 		for (i = 0; i < NMTUS; i++)
79283189849SRahul Lakkireddy 			if (adap->params.mtus[i] == 1492) {
79383189849SRahul Lakkireddy 				adap->params.mtus[i] = 1488;
79483189849SRahul Lakkireddy 				break;
79583189849SRahul Lakkireddy 			}
79683189849SRahul Lakkireddy 
79783189849SRahul Lakkireddy 		t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
79883189849SRahul Lakkireddy 			     adap->params.b_wnd);
79983189849SRahul Lakkireddy 	}
80083189849SRahul Lakkireddy 	t4_init_sge_params(adap);
80183189849SRahul Lakkireddy 	t4_init_tp_params(adap);
80283189849SRahul Lakkireddy 
80383189849SRahul Lakkireddy 	adap->params.drv_memwin = MEMWIN_NIC;
80483189849SRahul Lakkireddy 	adap->flags |= FW_OK;
80583189849SRahul Lakkireddy 	dev_debug(adap, "%s: returning zero..\n", __func__);
80683189849SRahul Lakkireddy 	return 0;
80783189849SRahul Lakkireddy 
80883189849SRahul Lakkireddy 	/*
80983189849SRahul Lakkireddy 	 * Something bad happened.  If a command timed out or failed with EIO
81083189849SRahul Lakkireddy 	 * FW does not operate within its spec or something catastrophic
81183189849SRahul Lakkireddy 	 * happened to HW/FW, stop issuing commands.
81283189849SRahul Lakkireddy 	 */
81383189849SRahul Lakkireddy bye:
81483189849SRahul Lakkireddy 	if (ret != -ETIMEDOUT && ret != -EIO)
81583189849SRahul Lakkireddy 		t4_fw_bye(adap, adap->mbox);
81683189849SRahul Lakkireddy 	return ret;
81783189849SRahul Lakkireddy }
81883189849SRahul Lakkireddy 
81983189849SRahul Lakkireddy /**
82083189849SRahul Lakkireddy  * t4_os_portmod_changed - handle port module changes
82183189849SRahul Lakkireddy  * @adap: the adapter associated with the module change
82283189849SRahul Lakkireddy  * @port_id: the port index whose module status has changed
82383189849SRahul Lakkireddy  *
82483189849SRahul Lakkireddy  * This is the OS-dependent handler for port module changes.  It is
82583189849SRahul Lakkireddy  * invoked when a port module is removed or inserted for any OS-specific
82683189849SRahul Lakkireddy  * processing.
82783189849SRahul Lakkireddy  */
82883189849SRahul Lakkireddy void t4_os_portmod_changed(const struct adapter *adap, int port_id)
82983189849SRahul Lakkireddy {
83083189849SRahul Lakkireddy 	static const char * const mod_str[] = {
83183189849SRahul Lakkireddy 		NULL, "LR", "SR", "ER", "passive DA", "active DA", "LRM"
83283189849SRahul Lakkireddy 	};
83383189849SRahul Lakkireddy 
83483189849SRahul Lakkireddy 	const struct port_info *pi = &adap->port[port_id];
83583189849SRahul Lakkireddy 
83683189849SRahul Lakkireddy 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
83783189849SRahul Lakkireddy 		dev_info(adap, "Port%d: port module unplugged\n", pi->port_id);
83883189849SRahul Lakkireddy 	else if (pi->mod_type < ARRAY_SIZE(mod_str))
83983189849SRahul Lakkireddy 		dev_info(adap, "Port%d: %s port module inserted\n", pi->port_id,
84083189849SRahul Lakkireddy 			 mod_str[pi->mod_type]);
84183189849SRahul Lakkireddy 	else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
8429da2a694SRahul Lakkireddy 		dev_info(adap, "Port%d: unsupported port module inserted\n",
84383189849SRahul Lakkireddy 			 pi->port_id);
84483189849SRahul Lakkireddy 	else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
8459da2a694SRahul Lakkireddy 		dev_info(adap, "Port%d: unknown port module inserted\n",
84683189849SRahul Lakkireddy 			 pi->port_id);
84783189849SRahul Lakkireddy 	else if (pi->mod_type == FW_PORT_MOD_TYPE_ERROR)
84883189849SRahul Lakkireddy 		dev_info(adap, "Port%d: transceiver module error\n",
84983189849SRahul Lakkireddy 			 pi->port_id);
85083189849SRahul Lakkireddy 	else
85183189849SRahul Lakkireddy 		dev_info(adap, "Port%d: unknown module type %d inserted\n",
85283189849SRahul Lakkireddy 			 pi->port_id, pi->mod_type);
85383189849SRahul Lakkireddy }
85483189849SRahul Lakkireddy 
85592c8a632SRahul Lakkireddy /**
8560462d115SRahul Lakkireddy  * link_start - enable a port
8570462d115SRahul Lakkireddy  * @dev: the port to enable
8580462d115SRahul Lakkireddy  *
8590462d115SRahul Lakkireddy  * Performs the MAC and PHY actions needed to enable a port.
8600462d115SRahul Lakkireddy  */
8610462d115SRahul Lakkireddy int link_start(struct port_info *pi)
8620462d115SRahul Lakkireddy {
8630462d115SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
8640462d115SRahul Lakkireddy 	int ret;
8655a9e303aSRahul Lakkireddy 	unsigned int mtu;
8665a9e303aSRahul Lakkireddy 
8675a9e303aSRahul Lakkireddy 	mtu = pi->eth_dev->data->dev_conf.rxmode.max_rx_pkt_len -
8685a9e303aSRahul Lakkireddy 	      (ETHER_HDR_LEN + ETHER_CRC_LEN);
8690462d115SRahul Lakkireddy 
8700462d115SRahul Lakkireddy 	/*
8710462d115SRahul Lakkireddy 	 * We do not set address filters and promiscuity here, the stack does
8720462d115SRahul Lakkireddy 	 * that step explicitly.
8730462d115SRahul Lakkireddy 	 */
8744b2eff45SRahul Lakkireddy 	ret = t4_set_rxmode(adapter, adapter->mbox, pi->viid, mtu, -1, -1,
8750462d115SRahul Lakkireddy 			    -1, 1, true);
8760462d115SRahul Lakkireddy 	if (ret == 0) {
8770462d115SRahul Lakkireddy 		ret = t4_change_mac(adapter, adapter->mbox, pi->viid,
8780462d115SRahul Lakkireddy 				    pi->xact_addr_filt,
8790462d115SRahul Lakkireddy 				    (u8 *)&pi->eth_dev->data->mac_addrs[0],
8800462d115SRahul Lakkireddy 				    true, true);
8810462d115SRahul Lakkireddy 		if (ret >= 0) {
8820462d115SRahul Lakkireddy 			pi->xact_addr_filt = ret;
8830462d115SRahul Lakkireddy 			ret = 0;
8840462d115SRahul Lakkireddy 		}
8850462d115SRahul Lakkireddy 	}
8860462d115SRahul Lakkireddy 	if (ret == 0)
8870462d115SRahul Lakkireddy 		ret = t4_link_l1cfg(adapter, adapter->mbox, pi->tx_chan,
8880462d115SRahul Lakkireddy 				    &pi->link_cfg);
8890462d115SRahul Lakkireddy 	if (ret == 0) {
8900462d115SRahul Lakkireddy 		/*
8910462d115SRahul Lakkireddy 		 * Enabling a Virtual Interface can result in an interrupt
8920462d115SRahul Lakkireddy 		 * during the processing of the VI Enable command and, in some
8930462d115SRahul Lakkireddy 		 * paths, result in an attempt to issue another command in the
8940462d115SRahul Lakkireddy 		 * interrupt context.  Thus, we disable interrupts during the
8950462d115SRahul Lakkireddy 		 * course of the VI Enable command ...
8960462d115SRahul Lakkireddy 		 */
8970462d115SRahul Lakkireddy 		ret = t4_enable_vi_params(adapter, adapter->mbox, pi->viid,
8980462d115SRahul Lakkireddy 					  true, true, false);
8990462d115SRahul Lakkireddy 	}
9000462d115SRahul Lakkireddy 	return ret;
9010462d115SRahul Lakkireddy }
9020462d115SRahul Lakkireddy 
9030462d115SRahul Lakkireddy /**
90492c8a632SRahul Lakkireddy  * cxgb4_write_rss - write the RSS table for a given port
90592c8a632SRahul Lakkireddy  * @pi: the port
90692c8a632SRahul Lakkireddy  * @queues: array of queue indices for RSS
90792c8a632SRahul Lakkireddy  *
90892c8a632SRahul Lakkireddy  * Sets up the portion of the HW RSS table for the port's VI to distribute
90992c8a632SRahul Lakkireddy  * packets to the Rx queues in @queues.
91092c8a632SRahul Lakkireddy  */
91192c8a632SRahul Lakkireddy int cxgb4_write_rss(const struct port_info *pi, const u16 *queues)
91292c8a632SRahul Lakkireddy {
91392c8a632SRahul Lakkireddy 	u16 *rss;
91492c8a632SRahul Lakkireddy 	int i, err;
91592c8a632SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
91692c8a632SRahul Lakkireddy 	const struct sge_eth_rxq *rxq;
91792c8a632SRahul Lakkireddy 
91892c8a632SRahul Lakkireddy 	/*  Should never be called before setting up sge eth rx queues */
91992c8a632SRahul Lakkireddy 	BUG_ON(!(adapter->flags & FULL_INIT_DONE));
92092c8a632SRahul Lakkireddy 
92192c8a632SRahul Lakkireddy 	rxq = &adapter->sge.ethrxq[pi->first_qset];
92292c8a632SRahul Lakkireddy 	rss = rte_zmalloc(NULL, pi->rss_size * sizeof(u16), 0);
92392c8a632SRahul Lakkireddy 	if (!rss)
92492c8a632SRahul Lakkireddy 		return -ENOMEM;
92592c8a632SRahul Lakkireddy 
92692c8a632SRahul Lakkireddy 	/* map the queue indices to queue ids */
92792c8a632SRahul Lakkireddy 	for (i = 0; i < pi->rss_size; i++, queues++)
92892c8a632SRahul Lakkireddy 		rss[i] = rxq[*queues].rspq.abs_id;
92992c8a632SRahul Lakkireddy 
93092c8a632SRahul Lakkireddy 	err = t4_config_rss_range(adapter, adapter->pf, pi->viid, 0,
93192c8a632SRahul Lakkireddy 				  pi->rss_size, rss, pi->rss_size);
93292c8a632SRahul Lakkireddy 	/*
93392c8a632SRahul Lakkireddy 	 * If Tunnel All Lookup isn't specified in the global RSS
93492c8a632SRahul Lakkireddy 	 * Configuration, then we need to specify a default Ingress
93592c8a632SRahul Lakkireddy 	 * Queue for any ingress packets which aren't hashed.  We'll
93692c8a632SRahul Lakkireddy 	 * use our first ingress queue ...
93792c8a632SRahul Lakkireddy 	 */
93892c8a632SRahul Lakkireddy 	if (!err)
93992c8a632SRahul Lakkireddy 		err = t4_config_vi_rss(adapter, adapter->mbox, pi->viid,
94092c8a632SRahul Lakkireddy 				       F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN |
94192c8a632SRahul Lakkireddy 				       F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN |
94292c8a632SRahul Lakkireddy 				       F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
94392c8a632SRahul Lakkireddy 				       F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN |
94492c8a632SRahul Lakkireddy 				       F_FW_RSS_VI_CONFIG_CMD_UDPEN,
94592c8a632SRahul Lakkireddy 				       rss[0]);
94692c8a632SRahul Lakkireddy 	rte_free(rss);
94792c8a632SRahul Lakkireddy 	return err;
94892c8a632SRahul Lakkireddy }
94992c8a632SRahul Lakkireddy 
95092c8a632SRahul Lakkireddy /**
95192c8a632SRahul Lakkireddy  * setup_rss - configure RSS
95292c8a632SRahul Lakkireddy  * @adapter: the adapter
95392c8a632SRahul Lakkireddy  *
95492c8a632SRahul Lakkireddy  * Sets up RSS to distribute packets to multiple receive queues.  We
95592c8a632SRahul Lakkireddy  * configure the RSS CPU lookup table to distribute to the number of HW
95692c8a632SRahul Lakkireddy  * receive queues, and the response queue lookup table to narrow that
95792c8a632SRahul Lakkireddy  * down to the response queues actually configured for each port.
95892c8a632SRahul Lakkireddy  * We always configure the RSS mapping for all ports since the mapping
95992c8a632SRahul Lakkireddy  * table has plenty of entries.
96092c8a632SRahul Lakkireddy  */
96192c8a632SRahul Lakkireddy int setup_rss(struct port_info *pi)
96292c8a632SRahul Lakkireddy {
96392c8a632SRahul Lakkireddy 	int j, err;
96492c8a632SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
96592c8a632SRahul Lakkireddy 
96692c8a632SRahul Lakkireddy 	dev_debug(adapter, "%s:  pi->rss_size = %u; pi->n_rx_qsets = %u\n",
96792c8a632SRahul Lakkireddy 		  __func__, pi->rss_size, pi->n_rx_qsets);
96892c8a632SRahul Lakkireddy 
9691039ee1cSEmmanuel Roullit 	if (!(pi->flags & PORT_RSS_DONE)) {
97092c8a632SRahul Lakkireddy 		if (adapter->flags & FULL_INIT_DONE) {
97192c8a632SRahul Lakkireddy 			/* Fill default values with equal distribution */
97292c8a632SRahul Lakkireddy 			for (j = 0; j < pi->rss_size; j++)
97392c8a632SRahul Lakkireddy 				pi->rss[j] = j % pi->n_rx_qsets;
97492c8a632SRahul Lakkireddy 
97592c8a632SRahul Lakkireddy 			err = cxgb4_write_rss(pi, pi->rss);
97692c8a632SRahul Lakkireddy 			if (err)
97792c8a632SRahul Lakkireddy 				return err;
97892c8a632SRahul Lakkireddy 			pi->flags |= PORT_RSS_DONE;
97992c8a632SRahul Lakkireddy 		}
98092c8a632SRahul Lakkireddy 	}
98192c8a632SRahul Lakkireddy 	return 0;
98292c8a632SRahul Lakkireddy }
98392c8a632SRahul Lakkireddy 
9840462d115SRahul Lakkireddy /*
9850462d115SRahul Lakkireddy  * Enable NAPI scheduling and interrupt generation for all Rx queues.
9860462d115SRahul Lakkireddy  */
987*d87ba24dSRahul Lakkireddy static void enable_rx(struct adapter *adap, struct sge_rspq *q)
9880462d115SRahul Lakkireddy {
9890462d115SRahul Lakkireddy 	/* 0-increment GTS to start the timer and enable interrupts */
9900462d115SRahul Lakkireddy 	t4_write_reg(adap, MYPF_REG(A_SGE_PF_GTS),
9910462d115SRahul Lakkireddy 		     V_SEINTARM(q->intr_params) |
9920462d115SRahul Lakkireddy 		     V_INGRESSQID(q->cntxt_id));
9930462d115SRahul Lakkireddy }
994*d87ba24dSRahul Lakkireddy 
995*d87ba24dSRahul Lakkireddy void cxgbe_enable_rx_queues(struct port_info *pi)
996*d87ba24dSRahul Lakkireddy {
997*d87ba24dSRahul Lakkireddy 	struct adapter *adap = pi->adapter;
998*d87ba24dSRahul Lakkireddy 	struct sge *s = &adap->sge;
999*d87ba24dSRahul Lakkireddy 	unsigned int i;
1000*d87ba24dSRahul Lakkireddy 
1001*d87ba24dSRahul Lakkireddy 	for (i = 0; i < pi->n_rx_qsets; i++)
1002*d87ba24dSRahul Lakkireddy 		enable_rx(adap, &s->ethrxq[pi->first_qset + i].rspq);
10030462d115SRahul Lakkireddy }
10040462d115SRahul Lakkireddy 
10050462d115SRahul Lakkireddy /**
10060462d115SRahul Lakkireddy  * cxgb_up - enable the adapter
10070462d115SRahul Lakkireddy  * @adap: adapter being enabled
10080462d115SRahul Lakkireddy  *
10090462d115SRahul Lakkireddy  * Called when the first port is enabled, this function performs the
10100462d115SRahul Lakkireddy  * actions necessary to make an adapter operational, such as completing
10110462d115SRahul Lakkireddy  * the initialization of HW modules, and enabling interrupts.
10120462d115SRahul Lakkireddy  */
10130462d115SRahul Lakkireddy int cxgbe_up(struct adapter *adap)
10140462d115SRahul Lakkireddy {
1015*d87ba24dSRahul Lakkireddy 	enable_rx(adap, &adap->sge.fw_evtq);
10160462d115SRahul Lakkireddy 	t4_sge_tx_monitor_start(adap);
10170462d115SRahul Lakkireddy 	t4_intr_enable(adap);
10180462d115SRahul Lakkireddy 	adap->flags |= FULL_INIT_DONE;
10190462d115SRahul Lakkireddy 
10200462d115SRahul Lakkireddy 	/* TODO: deadman watchdog ?? */
10210462d115SRahul Lakkireddy 	return 0;
10220462d115SRahul Lakkireddy }
10230462d115SRahul Lakkireddy 
10240462d115SRahul Lakkireddy /*
10250462d115SRahul Lakkireddy  * Close the port
10260462d115SRahul Lakkireddy  */
10270462d115SRahul Lakkireddy int cxgbe_down(struct port_info *pi)
10280462d115SRahul Lakkireddy {
10290462d115SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
10300462d115SRahul Lakkireddy 	int err = 0;
10310462d115SRahul Lakkireddy 
10320462d115SRahul Lakkireddy 	err = t4_enable_vi(adapter, adapter->mbox, pi->viid, false, false);
10330462d115SRahul Lakkireddy 	if (err) {
10340462d115SRahul Lakkireddy 		dev_err(adapter, "%s: disable_vi failed: %d\n", __func__, err);
10350462d115SRahul Lakkireddy 		return err;
10360462d115SRahul Lakkireddy 	}
10370462d115SRahul Lakkireddy 
10380462d115SRahul Lakkireddy 	t4_reset_link_config(adapter, pi->port_id);
10390462d115SRahul Lakkireddy 	return 0;
10400462d115SRahul Lakkireddy }
10410462d115SRahul Lakkireddy 
10420462d115SRahul Lakkireddy /*
10430462d115SRahul Lakkireddy  * Release resources when all the ports have been stopped.
10440462d115SRahul Lakkireddy  */
10450462d115SRahul Lakkireddy void cxgbe_close(struct adapter *adapter)
10460462d115SRahul Lakkireddy {
10470462d115SRahul Lakkireddy 	struct port_info *pi;
10480462d115SRahul Lakkireddy 	int i;
10490462d115SRahul Lakkireddy 
10500462d115SRahul Lakkireddy 	if (adapter->flags & FULL_INIT_DONE) {
10510462d115SRahul Lakkireddy 		t4_intr_disable(adapter);
10520462d115SRahul Lakkireddy 		t4_sge_tx_monitor_stop(adapter);
10530462d115SRahul Lakkireddy 		t4_free_sge_resources(adapter);
10540462d115SRahul Lakkireddy 		for_each_port(adapter, i) {
10550462d115SRahul Lakkireddy 			pi = adap2pinfo(adapter, i);
10560462d115SRahul Lakkireddy 			if (pi->viid != 0)
10570462d115SRahul Lakkireddy 				t4_free_vi(adapter, adapter->mbox,
10580462d115SRahul Lakkireddy 					   adapter->pf, 0, pi->viid);
10590462d115SRahul Lakkireddy 			rte_free(pi->eth_dev->data->mac_addrs);
10600462d115SRahul Lakkireddy 		}
10610462d115SRahul Lakkireddy 		adapter->flags &= ~FULL_INIT_DONE;
10620462d115SRahul Lakkireddy 	}
10630462d115SRahul Lakkireddy 
10640462d115SRahul Lakkireddy 	if (adapter->flags & FW_OK)
10650462d115SRahul Lakkireddy 		t4_fw_bye(adapter, adapter->mbox);
10660462d115SRahul Lakkireddy }
10670462d115SRahul Lakkireddy 
106883189849SRahul Lakkireddy int cxgbe_probe(struct adapter *adapter)
106983189849SRahul Lakkireddy {
107083189849SRahul Lakkireddy 	struct port_info *pi;
107104868e5bSRahul Lakkireddy 	int chip;
107283189849SRahul Lakkireddy 	int func, i;
107383189849SRahul Lakkireddy 	int err = 0;
107404868e5bSRahul Lakkireddy 	u32 whoami;
107583189849SRahul Lakkireddy 
107604868e5bSRahul Lakkireddy 	whoami = t4_read_reg(adapter, A_PL_WHOAMI);
107704868e5bSRahul Lakkireddy 	chip = t4_get_chip_type(adapter,
107804868e5bSRahul Lakkireddy 			CHELSIO_PCI_ID_VER(adapter->pdev->id.device_id));
107904868e5bSRahul Lakkireddy 	if (chip < 0)
108004868e5bSRahul Lakkireddy 		return chip;
108104868e5bSRahul Lakkireddy 
108204868e5bSRahul Lakkireddy 	func = CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5 ?
108304868e5bSRahul Lakkireddy 	       G_SOURCEPF(whoami) : G_T6_SOURCEPF(whoami);
108404868e5bSRahul Lakkireddy 
108583189849SRahul Lakkireddy 	adapter->mbox = func;
108683189849SRahul Lakkireddy 	adapter->pf = func;
108783189849SRahul Lakkireddy 
108883189849SRahul Lakkireddy 	t4_os_lock_init(&adapter->mbox_lock);
108983189849SRahul Lakkireddy 	TAILQ_INIT(&adapter->mbox_list);
109083189849SRahul Lakkireddy 
109183189849SRahul Lakkireddy 	err = t4_prep_adapter(adapter);
109283189849SRahul Lakkireddy 	if (err)
109383189849SRahul Lakkireddy 		return err;
109483189849SRahul Lakkireddy 
109583189849SRahul Lakkireddy 	setup_memwin(adapter);
109683189849SRahul Lakkireddy 	err = adap_init0(adapter);
109783189849SRahul Lakkireddy 	if (err) {
109883189849SRahul Lakkireddy 		dev_err(adapter, "%s: Adapter initialization failed, error %d\n",
109983189849SRahul Lakkireddy 			__func__, err);
110083189849SRahul Lakkireddy 		goto out_free;
110183189849SRahul Lakkireddy 	}
110283189849SRahul Lakkireddy 
110383189849SRahul Lakkireddy 	if (!is_t4(adapter->params.chip)) {
110483189849SRahul Lakkireddy 		/*
110583189849SRahul Lakkireddy 		 * The userspace doorbell BAR is split evenly into doorbell
110683189849SRahul Lakkireddy 		 * regions, each associated with an egress queue.  If this
110783189849SRahul Lakkireddy 		 * per-queue region is large enough (at least UDBS_SEG_SIZE)
110883189849SRahul Lakkireddy 		 * then it can be used to submit a tx work request with an
110983189849SRahul Lakkireddy 		 * implied doorbell.  Enable write combining on the BAR if
111083189849SRahul Lakkireddy 		 * there is room for such work requests.
111183189849SRahul Lakkireddy 		 */
111283189849SRahul Lakkireddy 		int s_qpp, qpp, num_seg;
111383189849SRahul Lakkireddy 
111483189849SRahul Lakkireddy 		s_qpp = (S_QUEUESPERPAGEPF0 +
111583189849SRahul Lakkireddy 			(S_QUEUESPERPAGEPF1 - S_QUEUESPERPAGEPF0) *
111683189849SRahul Lakkireddy 			adapter->pf);
111783189849SRahul Lakkireddy 		qpp = 1 << ((t4_read_reg(adapter,
111883189849SRahul Lakkireddy 				A_SGE_EGRESS_QUEUES_PER_PAGE_PF) >> s_qpp)
111983189849SRahul Lakkireddy 				& M_QUEUESPERPAGEPF0);
11201f8613f1SRahul Lakkireddy 		num_seg = CXGBE_PAGE_SIZE / UDBS_SEG_SIZE;
112183189849SRahul Lakkireddy 		if (qpp > num_seg)
112283189849SRahul Lakkireddy 			dev_warn(adapter, "Incorrect SGE EGRESS QUEUES_PER_PAGE configuration, continuing in debug mode\n");
112383189849SRahul Lakkireddy 
112483189849SRahul Lakkireddy 		adapter->bar2 = (void *)adapter->pdev->mem_resource[2].addr;
112583189849SRahul Lakkireddy 		if (!adapter->bar2) {
112683189849SRahul Lakkireddy 			dev_err(adapter, "cannot map device bar2 region\n");
112783189849SRahul Lakkireddy 			err = -ENOMEM;
112883189849SRahul Lakkireddy 			goto out_free;
112983189849SRahul Lakkireddy 		}
113083189849SRahul Lakkireddy 		t4_write_reg(adapter, A_SGE_STAT_CFG, V_STATSOURCE_T5(7) |
113183189849SRahul Lakkireddy 			     V_STATMODE(0));
113283189849SRahul Lakkireddy 	}
113383189849SRahul Lakkireddy 
113483189849SRahul Lakkireddy 	for_each_port(adapter, i) {
113583189849SRahul Lakkireddy 		char name[RTE_ETH_NAME_MAX_LEN];
113683189849SRahul Lakkireddy 		struct rte_eth_dev_data *data = NULL;
113783189849SRahul Lakkireddy 		const unsigned int numa_node = rte_socket_id();
113883189849SRahul Lakkireddy 
113983189849SRahul Lakkireddy 		pi = &adapter->port[i];
114083189849SRahul Lakkireddy 		pi->adapter = adapter;
114183189849SRahul Lakkireddy 		pi->xact_addr_filt = -1;
114283189849SRahul Lakkireddy 		pi->port_id = i;
114383189849SRahul Lakkireddy 
114483189849SRahul Lakkireddy 		snprintf(name, sizeof(name), "cxgbe%d",
114583189849SRahul Lakkireddy 			 adapter->eth_dev->data->port_id + i);
114683189849SRahul Lakkireddy 
114783189849SRahul Lakkireddy 		if (i == 0) {
114883189849SRahul Lakkireddy 			/* First port is already allocated by DPDK */
114983189849SRahul Lakkireddy 			pi->eth_dev = adapter->eth_dev;
115083189849SRahul Lakkireddy 			goto allocate_mac;
115183189849SRahul Lakkireddy 		}
115283189849SRahul Lakkireddy 
115383189849SRahul Lakkireddy 		/*
115483189849SRahul Lakkireddy 		 * now do all data allocation - for eth_dev structure,
115583189849SRahul Lakkireddy 		 * and internal (private) data for the remaining ports
115683189849SRahul Lakkireddy 		 */
115783189849SRahul Lakkireddy 
115883189849SRahul Lakkireddy 		/* reserve an ethdev entry */
11596751f6deSDavid Marchand 		pi->eth_dev = rte_eth_dev_allocate(name);
116083189849SRahul Lakkireddy 		if (!pi->eth_dev)
116183189849SRahul Lakkireddy 			goto out_free;
116283189849SRahul Lakkireddy 
116383189849SRahul Lakkireddy 		data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
116483189849SRahul Lakkireddy 		if (!data)
116583189849SRahul Lakkireddy 			goto out_free;
116683189849SRahul Lakkireddy 
116783189849SRahul Lakkireddy 		data->port_id = adapter->eth_dev->data->port_id + i;
116883189849SRahul Lakkireddy 
116983189849SRahul Lakkireddy 		pi->eth_dev->data = data;
117083189849SRahul Lakkireddy 
117183189849SRahul Lakkireddy allocate_mac:
1172eac901ceSJan Blunck 		pi->eth_dev->device = &adapter->pdev->device;
117383189849SRahul Lakkireddy 		pi->eth_dev->data->dev_private = pi;
117483189849SRahul Lakkireddy 		pi->eth_dev->dev_ops = adapter->eth_dev->dev_ops;
11754a01078bSRahul Lakkireddy 		pi->eth_dev->tx_pkt_burst = adapter->eth_dev->tx_pkt_burst;
117692c8a632SRahul Lakkireddy 		pi->eth_dev->rx_pkt_burst = adapter->eth_dev->rx_pkt_burst;
117713b0f500SRahul Lakkireddy 
1178eac901ceSJan Blunck 		rte_eth_copy_pci_info(pi->eth_dev, adapter->pdev);
117913b0f500SRahul Lakkireddy 
118083189849SRahul Lakkireddy 		pi->eth_dev->data->mac_addrs = rte_zmalloc(name,
118183189849SRahul Lakkireddy 							   ETHER_ADDR_LEN, 0);
118283189849SRahul Lakkireddy 		if (!pi->eth_dev->data->mac_addrs) {
118383189849SRahul Lakkireddy 			dev_err(adapter, "%s: Mem allocation failed for storing mac addr, aborting\n",
118483189849SRahul Lakkireddy 				__func__);
118583189849SRahul Lakkireddy 			err = -1;
118683189849SRahul Lakkireddy 			goto out_free;
118783189849SRahul Lakkireddy 		}
118883189849SRahul Lakkireddy 	}
118983189849SRahul Lakkireddy 
119083189849SRahul Lakkireddy 	if (adapter->flags & FW_OK) {
119183189849SRahul Lakkireddy 		err = t4_port_init(adapter, adapter->mbox, adapter->pf, 0);
119283189849SRahul Lakkireddy 		if (err) {
119383189849SRahul Lakkireddy 			dev_err(adapter, "%s: t4_port_init failed with err %d\n",
119483189849SRahul Lakkireddy 				__func__, err);
119583189849SRahul Lakkireddy 			goto out_free;
119683189849SRahul Lakkireddy 		}
119783189849SRahul Lakkireddy 	}
119883189849SRahul Lakkireddy 
119992c8a632SRahul Lakkireddy 	cfg_queues(adapter->eth_dev);
120092c8a632SRahul Lakkireddy 
1201c962618cSRahul Lakkireddy 	print_adapter_info(adapter);
120283189849SRahul Lakkireddy 	print_port_info(adapter);
120383189849SRahul Lakkireddy 
120492c8a632SRahul Lakkireddy 	err = init_rss(adapter);
120592c8a632SRahul Lakkireddy 	if (err)
120692c8a632SRahul Lakkireddy 		goto out_free;
120792c8a632SRahul Lakkireddy 
120883189849SRahul Lakkireddy 	return 0;
120983189849SRahul Lakkireddy 
121083189849SRahul Lakkireddy out_free:
121183189849SRahul Lakkireddy 	for_each_port(adapter, i) {
121283189849SRahul Lakkireddy 		pi = adap2pinfo(adapter, i);
121383189849SRahul Lakkireddy 		if (pi->viid != 0)
121483189849SRahul Lakkireddy 			t4_free_vi(adapter, adapter->mbox, adapter->pf,
121583189849SRahul Lakkireddy 				   0, pi->viid);
121683189849SRahul Lakkireddy 		/* Skip first port since it'll be de-allocated by DPDK */
121783189849SRahul Lakkireddy 		if (i == 0)
121883189849SRahul Lakkireddy 			continue;
121983189849SRahul Lakkireddy 		if (pi->eth_dev->data)
122083189849SRahul Lakkireddy 			rte_free(pi->eth_dev->data);
122183189849SRahul Lakkireddy 	}
122283189849SRahul Lakkireddy 
122383189849SRahul Lakkireddy 	if (adapter->flags & FW_OK)
122483189849SRahul Lakkireddy 		t4_fw_bye(adapter, adapter->mbox);
122583189849SRahul Lakkireddy 	return -err;
122683189849SRahul Lakkireddy }
1227