xref: /dpdk/drivers/net/cxgbe/cxgbe_main.c (revision efa8a43e392c1bdea4a128a62dfbe2861bfbe3a6)
183189849SRahul Lakkireddy /*-
283189849SRahul Lakkireddy  *   BSD LICENSE
383189849SRahul Lakkireddy  *
483189849SRahul Lakkireddy  *   Copyright(c) 2014-2015 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>
6083189849SRahul Lakkireddy #include <rte_atomic.h>
6183189849SRahul Lakkireddy #include <rte_malloc.h>
6283189849SRahul Lakkireddy #include <rte_random.h>
6383189849SRahul Lakkireddy #include <rte_dev.h>
6483189849SRahul Lakkireddy 
6583189849SRahul Lakkireddy #include "common.h"
6683189849SRahul Lakkireddy #include "t4_regs.h"
6783189849SRahul Lakkireddy #include "t4_msg.h"
6883189849SRahul Lakkireddy #include "cxgbe.h"
6983189849SRahul Lakkireddy 
7092c8a632SRahul Lakkireddy /*
7192c8a632SRahul Lakkireddy  * Response queue handler for the FW event queue.
7292c8a632SRahul Lakkireddy  */
7392c8a632SRahul Lakkireddy static int fwevtq_handler(struct sge_rspq *q, const __be64 *rsp,
7492c8a632SRahul Lakkireddy 			  __rte_unused const struct pkt_gl *gl)
7592c8a632SRahul Lakkireddy {
7692c8a632SRahul Lakkireddy 	u8 opcode = ((const struct rss_header *)rsp)->opcode;
7792c8a632SRahul Lakkireddy 
7892c8a632SRahul Lakkireddy 	rsp++;                                          /* skip RSS header */
7992c8a632SRahul Lakkireddy 
8092c8a632SRahul Lakkireddy 	/*
8192c8a632SRahul Lakkireddy 	 * FW can send EGR_UPDATEs encapsulated in a CPL_FW4_MSG.
8292c8a632SRahul Lakkireddy 	 */
8392c8a632SRahul Lakkireddy 	if (unlikely(opcode == CPL_FW4_MSG &&
8492c8a632SRahul Lakkireddy 		     ((const struct cpl_fw4_msg *)rsp)->type ==
8592c8a632SRahul Lakkireddy 		      FW_TYPE_RSSCPL)) {
8692c8a632SRahul Lakkireddy 		rsp++;
8792c8a632SRahul Lakkireddy 		opcode = ((const struct rss_header *)rsp)->opcode;
8892c8a632SRahul Lakkireddy 		rsp++;
8992c8a632SRahul Lakkireddy 		if (opcode != CPL_SGE_EGR_UPDATE) {
9092c8a632SRahul Lakkireddy 			dev_err(q->adapter, "unexpected FW4/CPL %#x on FW event queue\n",
9192c8a632SRahul Lakkireddy 				opcode);
9292c8a632SRahul Lakkireddy 			goto out;
9392c8a632SRahul Lakkireddy 		}
9492c8a632SRahul Lakkireddy 	}
9592c8a632SRahul Lakkireddy 
9692c8a632SRahul Lakkireddy 	if (likely(opcode == CPL_SGE_EGR_UPDATE)) {
9792c8a632SRahul Lakkireddy 		/* do nothing */
9892c8a632SRahul Lakkireddy 	} else if (opcode == CPL_FW6_MSG || opcode == CPL_FW4_MSG) {
9992c8a632SRahul Lakkireddy 		const struct cpl_fw6_msg *msg = (const void *)rsp;
10092c8a632SRahul Lakkireddy 
10192c8a632SRahul Lakkireddy 		t4_handle_fw_rpl(q->adapter, msg->data);
10292c8a632SRahul Lakkireddy 	} else {
10392c8a632SRahul Lakkireddy 		dev_err(adapter, "unexpected CPL %#x on FW event queue\n",
10492c8a632SRahul Lakkireddy 			opcode);
10592c8a632SRahul Lakkireddy 	}
10692c8a632SRahul Lakkireddy out:
10792c8a632SRahul Lakkireddy 	return 0;
10892c8a632SRahul Lakkireddy }
10992c8a632SRahul Lakkireddy 
11092c8a632SRahul Lakkireddy int setup_sge_fwevtq(struct adapter *adapter)
11192c8a632SRahul Lakkireddy {
11292c8a632SRahul Lakkireddy 	struct sge *s = &adapter->sge;
11392c8a632SRahul Lakkireddy 	int err = 0;
11492c8a632SRahul Lakkireddy 	int msi_idx = 0;
11592c8a632SRahul Lakkireddy 
11692c8a632SRahul Lakkireddy 	err = t4_sge_alloc_rxq(adapter, &s->fw_evtq, true, adapter->eth_dev,
11792c8a632SRahul Lakkireddy 			       msi_idx, NULL, fwevtq_handler, -1, NULL, 0,
11892c8a632SRahul Lakkireddy 			       rte_socket_id());
11992c8a632SRahul Lakkireddy 	return err;
12092c8a632SRahul Lakkireddy }
12192c8a632SRahul Lakkireddy 
12292c8a632SRahul Lakkireddy static int closest_timer(const struct sge *s, int time)
12392c8a632SRahul Lakkireddy {
12492c8a632SRahul Lakkireddy 	unsigned int i, match = 0;
12592c8a632SRahul Lakkireddy 	int delta, min_delta = INT_MAX;
12692c8a632SRahul Lakkireddy 
12792c8a632SRahul Lakkireddy 	for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
12892c8a632SRahul Lakkireddy 		delta = time - s->timer_val[i];
12992c8a632SRahul Lakkireddy 		if (delta < 0)
13092c8a632SRahul Lakkireddy 			delta = -delta;
13192c8a632SRahul Lakkireddy 		if (delta < min_delta) {
13292c8a632SRahul Lakkireddy 			min_delta = delta;
13392c8a632SRahul Lakkireddy 			match = i;
13492c8a632SRahul Lakkireddy 		}
13592c8a632SRahul Lakkireddy 	}
13692c8a632SRahul Lakkireddy 	return match;
13792c8a632SRahul Lakkireddy }
13892c8a632SRahul Lakkireddy 
13992c8a632SRahul Lakkireddy static int closest_thres(const struct sge *s, int thres)
14092c8a632SRahul Lakkireddy {
14192c8a632SRahul Lakkireddy 	unsigned int i, match = 0;
14292c8a632SRahul Lakkireddy 	int delta, min_delta = INT_MAX;
14392c8a632SRahul Lakkireddy 
14492c8a632SRahul Lakkireddy 	for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
14592c8a632SRahul Lakkireddy 		delta = thres - s->counter_val[i];
14692c8a632SRahul Lakkireddy 		if (delta < 0)
14792c8a632SRahul Lakkireddy 			delta = -delta;
14892c8a632SRahul Lakkireddy 		if (delta < min_delta) {
14992c8a632SRahul Lakkireddy 			min_delta = delta;
15092c8a632SRahul Lakkireddy 			match = i;
15192c8a632SRahul Lakkireddy 		}
15292c8a632SRahul Lakkireddy 	}
15392c8a632SRahul Lakkireddy 	return match;
15492c8a632SRahul Lakkireddy }
15592c8a632SRahul Lakkireddy 
15692c8a632SRahul Lakkireddy /**
15792c8a632SRahul Lakkireddy  * cxgb4_set_rspq_intr_params - set a queue's interrupt holdoff parameters
15892c8a632SRahul Lakkireddy  * @q: the Rx queue
15992c8a632SRahul Lakkireddy  * @us: the hold-off time in us, or 0 to disable timer
16092c8a632SRahul Lakkireddy  * @cnt: the hold-off packet count, or 0 to disable counter
16192c8a632SRahul Lakkireddy  *
16292c8a632SRahul Lakkireddy  * Sets an Rx queue's interrupt hold-off time and packet count.  At least
16392c8a632SRahul Lakkireddy  * one of the two needs to be enabled for the queue to generate interrupts.
16492c8a632SRahul Lakkireddy  */
16592c8a632SRahul Lakkireddy int cxgb4_set_rspq_intr_params(struct sge_rspq *q, unsigned int us,
16692c8a632SRahul Lakkireddy 			       unsigned int cnt)
16792c8a632SRahul Lakkireddy {
16892c8a632SRahul Lakkireddy 	struct adapter *adap = q->adapter;
16992c8a632SRahul Lakkireddy 	unsigned int timer_val;
17092c8a632SRahul Lakkireddy 
17192c8a632SRahul Lakkireddy 	if (cnt) {
17292c8a632SRahul Lakkireddy 		int err;
17392c8a632SRahul Lakkireddy 		u32 v, new_idx;
17492c8a632SRahul Lakkireddy 
17592c8a632SRahul Lakkireddy 		new_idx = closest_thres(&adap->sge, cnt);
17692c8a632SRahul Lakkireddy 		if (q->desc && q->pktcnt_idx != new_idx) {
17792c8a632SRahul Lakkireddy 			/* the queue has already been created, update it */
17892c8a632SRahul Lakkireddy 			v = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) |
17992c8a632SRahul Lakkireddy 			    V_FW_PARAMS_PARAM_X(
18092c8a632SRahul Lakkireddy 			    FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
18192c8a632SRahul Lakkireddy 			    V_FW_PARAMS_PARAM_YZ(q->cntxt_id);
18292c8a632SRahul Lakkireddy 			err = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
18392c8a632SRahul Lakkireddy 					    &v, &new_idx);
18492c8a632SRahul Lakkireddy 			if (err)
18592c8a632SRahul Lakkireddy 				return err;
18692c8a632SRahul Lakkireddy 		}
18792c8a632SRahul Lakkireddy 		q->pktcnt_idx = new_idx;
18892c8a632SRahul Lakkireddy 	}
18992c8a632SRahul Lakkireddy 
19092c8a632SRahul Lakkireddy 	timer_val = (us == 0) ? X_TIMERREG_RESTART_COUNTER :
19192c8a632SRahul Lakkireddy 				closest_timer(&adap->sge, us);
19292c8a632SRahul Lakkireddy 
19392c8a632SRahul Lakkireddy 	if ((us | cnt) == 0)
19492c8a632SRahul Lakkireddy 		q->intr_params = V_QINTR_TIMER_IDX(X_TIMERREG_UPDATE_CIDX);
19592c8a632SRahul Lakkireddy 	else
19692c8a632SRahul Lakkireddy 		q->intr_params = V_QINTR_TIMER_IDX(timer_val) |
19792c8a632SRahul Lakkireddy 				 V_QINTR_CNT_EN(cnt > 0);
19892c8a632SRahul Lakkireddy 	return 0;
19992c8a632SRahul Lakkireddy }
20092c8a632SRahul Lakkireddy 
20192c8a632SRahul Lakkireddy static inline bool is_x_1g_port(const struct link_config *lc)
20292c8a632SRahul Lakkireddy {
20392c8a632SRahul Lakkireddy 	return ((lc->supported & FW_PORT_CAP_SPEED_1G) != 0);
20492c8a632SRahul Lakkireddy }
20592c8a632SRahul Lakkireddy 
20692c8a632SRahul Lakkireddy static inline bool is_x_10g_port(const struct link_config *lc)
20792c8a632SRahul Lakkireddy {
20892c8a632SRahul Lakkireddy 	return ((lc->supported & FW_PORT_CAP_SPEED_10G) != 0 ||
20992c8a632SRahul Lakkireddy 		(lc->supported & FW_PORT_CAP_SPEED_40G) != 0 ||
21092c8a632SRahul Lakkireddy 		(lc->supported & FW_PORT_CAP_SPEED_100G) != 0);
21192c8a632SRahul Lakkireddy }
21292c8a632SRahul Lakkireddy 
21392c8a632SRahul Lakkireddy inline void init_rspq(struct adapter *adap, struct sge_rspq *q,
21492c8a632SRahul Lakkireddy 		      unsigned int us, unsigned int cnt,
21592c8a632SRahul Lakkireddy 		      unsigned int size, unsigned int iqe_size)
21692c8a632SRahul Lakkireddy {
21792c8a632SRahul Lakkireddy 	q->adapter = adap;
21892c8a632SRahul Lakkireddy 	cxgb4_set_rspq_intr_params(q, us, cnt);
21992c8a632SRahul Lakkireddy 	q->iqe_len = iqe_size;
22092c8a632SRahul Lakkireddy 	q->size = size;
22192c8a632SRahul Lakkireddy }
22292c8a632SRahul Lakkireddy 
22392c8a632SRahul Lakkireddy int cfg_queue_count(struct rte_eth_dev *eth_dev)
22492c8a632SRahul Lakkireddy {
22592c8a632SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
22692c8a632SRahul Lakkireddy 	struct adapter *adap = pi->adapter;
22792c8a632SRahul Lakkireddy 	struct sge *s = &adap->sge;
22892c8a632SRahul Lakkireddy 	unsigned int max_queues = s->max_ethqsets / adap->params.nports;
22992c8a632SRahul Lakkireddy 
23092c8a632SRahul Lakkireddy 	if ((eth_dev->data->nb_rx_queues < 1) ||
23192c8a632SRahul Lakkireddy 	    (eth_dev->data->nb_tx_queues < 1))
23292c8a632SRahul Lakkireddy 		return -EINVAL;
23392c8a632SRahul Lakkireddy 
23492c8a632SRahul Lakkireddy 	if ((eth_dev->data->nb_rx_queues > max_queues) ||
23592c8a632SRahul Lakkireddy 	    (eth_dev->data->nb_tx_queues > max_queues))
23692c8a632SRahul Lakkireddy 		return -EINVAL;
23792c8a632SRahul Lakkireddy 
23892c8a632SRahul Lakkireddy 	if (eth_dev->data->nb_rx_queues > pi->rss_size)
23992c8a632SRahul Lakkireddy 		return -EINVAL;
24092c8a632SRahul Lakkireddy 
24192c8a632SRahul Lakkireddy 	/* We must configure RSS, since config has changed*/
24292c8a632SRahul Lakkireddy 	pi->flags &= ~PORT_RSS_DONE;
24392c8a632SRahul Lakkireddy 
24492c8a632SRahul Lakkireddy 	pi->n_rx_qsets = eth_dev->data->nb_rx_queues;
24592c8a632SRahul Lakkireddy 	pi->n_tx_qsets = eth_dev->data->nb_tx_queues;
24692c8a632SRahul Lakkireddy 
24792c8a632SRahul Lakkireddy 	return 0;
24892c8a632SRahul Lakkireddy }
24992c8a632SRahul Lakkireddy 
25092c8a632SRahul Lakkireddy void cfg_queues(struct rte_eth_dev *eth_dev)
25192c8a632SRahul Lakkireddy {
25292c8a632SRahul Lakkireddy 	struct rte_config *config = rte_eal_get_configuration();
25392c8a632SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
25492c8a632SRahul Lakkireddy 	struct adapter *adap = pi->adapter;
25592c8a632SRahul Lakkireddy 	struct sge *s = &adap->sge;
25692c8a632SRahul Lakkireddy 	unsigned int i, nb_ports = 0, qidx = 0;
25792c8a632SRahul Lakkireddy 	unsigned int q_per_port = 0;
25892c8a632SRahul Lakkireddy 
25992c8a632SRahul Lakkireddy 	if (!(adap->flags & CFG_QUEUES)) {
26092c8a632SRahul Lakkireddy 		for_each_port(adap, i) {
26192c8a632SRahul Lakkireddy 			struct port_info *tpi = adap2pinfo(adap, i);
26292c8a632SRahul Lakkireddy 
26392c8a632SRahul Lakkireddy 			nb_ports += (is_x_10g_port(&tpi->link_cfg)) ||
26492c8a632SRahul Lakkireddy 				     is_x_1g_port(&tpi->link_cfg) ? 1 : 0;
26592c8a632SRahul Lakkireddy 		}
26692c8a632SRahul Lakkireddy 
26792c8a632SRahul Lakkireddy 		/*
26892c8a632SRahul Lakkireddy 		 * We default up to # of cores queues per 1G/10G port.
26992c8a632SRahul Lakkireddy 		 */
27092c8a632SRahul Lakkireddy 		if (nb_ports)
27192c8a632SRahul Lakkireddy 			q_per_port = (MAX_ETH_QSETS -
27292c8a632SRahul Lakkireddy 				     (adap->params.nports - nb_ports)) /
27392c8a632SRahul Lakkireddy 				     nb_ports;
27492c8a632SRahul Lakkireddy 
27592c8a632SRahul Lakkireddy 		if (q_per_port > config->lcore_count)
27692c8a632SRahul Lakkireddy 			q_per_port = config->lcore_count;
27792c8a632SRahul Lakkireddy 
27892c8a632SRahul Lakkireddy 		for_each_port(adap, i) {
27992c8a632SRahul Lakkireddy 			struct port_info *pi = adap2pinfo(adap, i);
28092c8a632SRahul Lakkireddy 
28192c8a632SRahul Lakkireddy 			pi->first_qset = qidx;
28292c8a632SRahul Lakkireddy 
28392c8a632SRahul Lakkireddy 			/* Initially n_rx_qsets == n_tx_qsets */
28492c8a632SRahul Lakkireddy 			pi->n_rx_qsets = (is_x_10g_port(&pi->link_cfg) ||
28592c8a632SRahul Lakkireddy 					  is_x_1g_port(&pi->link_cfg)) ?
28692c8a632SRahul Lakkireddy 					  q_per_port : 1;
28792c8a632SRahul Lakkireddy 			pi->n_tx_qsets = pi->n_rx_qsets;
28892c8a632SRahul Lakkireddy 
28992c8a632SRahul Lakkireddy 			if (pi->n_rx_qsets > pi->rss_size)
29092c8a632SRahul Lakkireddy 				pi->n_rx_qsets = pi->rss_size;
29192c8a632SRahul Lakkireddy 
29292c8a632SRahul Lakkireddy 			qidx += pi->n_rx_qsets;
29392c8a632SRahul Lakkireddy 		}
29492c8a632SRahul Lakkireddy 
29592c8a632SRahul Lakkireddy 		s->max_ethqsets = qidx;
29692c8a632SRahul Lakkireddy 
29792c8a632SRahul Lakkireddy 		for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
29892c8a632SRahul Lakkireddy 			struct sge_eth_rxq *r = &s->ethrxq[i];
29992c8a632SRahul Lakkireddy 
30092c8a632SRahul Lakkireddy 			init_rspq(adap, &r->rspq, 0, 0, 1024, 64);
30192c8a632SRahul Lakkireddy 			r->usembufs = 1;
30292c8a632SRahul Lakkireddy 			r->fl.size = (r->usembufs ? 1024 : 72);
30392c8a632SRahul Lakkireddy 		}
30492c8a632SRahul Lakkireddy 
30592c8a632SRahul Lakkireddy 		for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
30692c8a632SRahul Lakkireddy 			s->ethtxq[i].q.size = 1024;
30792c8a632SRahul Lakkireddy 
30892c8a632SRahul Lakkireddy 		init_rspq(adap, &adap->sge.fw_evtq, 0, 0, 1024, 64);
30992c8a632SRahul Lakkireddy 		adap->flags |= CFG_QUEUES;
31092c8a632SRahul Lakkireddy 	}
31192c8a632SRahul Lakkireddy }
31292c8a632SRahul Lakkireddy 
313856505d3SRahul Lakkireddy void cxgbe_stats_get(struct port_info *pi, struct port_stats *stats)
314856505d3SRahul Lakkireddy {
315856505d3SRahul Lakkireddy 	t4_get_port_stats_offset(pi->adapter, pi->tx_chan, stats,
316856505d3SRahul Lakkireddy 				 &pi->stats_base);
317856505d3SRahul Lakkireddy }
318856505d3SRahul Lakkireddy 
319856505d3SRahul Lakkireddy void cxgbe_stats_reset(struct port_info *pi)
320856505d3SRahul Lakkireddy {
321856505d3SRahul Lakkireddy 	t4_clr_port_stats(pi->adapter, pi->tx_chan);
322856505d3SRahul Lakkireddy }
323856505d3SRahul Lakkireddy 
32483189849SRahul Lakkireddy static void setup_memwin(struct adapter *adap)
32583189849SRahul Lakkireddy {
32683189849SRahul Lakkireddy 	u32 mem_win0_base;
32783189849SRahul Lakkireddy 
32883189849SRahul Lakkireddy 	/* For T5, only relative offset inside the PCIe BAR is passed */
32983189849SRahul Lakkireddy 	mem_win0_base = MEMWIN0_BASE;
33083189849SRahul Lakkireddy 
33183189849SRahul Lakkireddy 	/*
33283189849SRahul Lakkireddy 	 * Set up memory window for accessing adapter memory ranges.  (Read
33383189849SRahul Lakkireddy 	 * back MA register to ensure that changes propagate before we attempt
33483189849SRahul Lakkireddy 	 * to use the new values.)
33583189849SRahul Lakkireddy 	 */
33683189849SRahul Lakkireddy 	t4_write_reg(adap,
33783189849SRahul Lakkireddy 		     PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN,
33883189849SRahul Lakkireddy 					 MEMWIN_NIC),
33983189849SRahul Lakkireddy 		     mem_win0_base | V_BIR(0) |
34083189849SRahul Lakkireddy 		     V_WINDOW(ilog2(MEMWIN0_APERTURE) - X_WINDOW_SHIFT));
34183189849SRahul Lakkireddy 	t4_read_reg(adap,
34283189849SRahul Lakkireddy 		    PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN,
34383189849SRahul Lakkireddy 					MEMWIN_NIC));
34483189849SRahul Lakkireddy }
34583189849SRahul Lakkireddy 
34692c8a632SRahul Lakkireddy static int init_rss(struct adapter *adap)
34792c8a632SRahul Lakkireddy {
34892c8a632SRahul Lakkireddy 	unsigned int i;
34992c8a632SRahul Lakkireddy 	int err;
35092c8a632SRahul Lakkireddy 
35192c8a632SRahul Lakkireddy 	err = t4_init_rss_mode(adap, adap->mbox);
35292c8a632SRahul Lakkireddy 	if (err)
35392c8a632SRahul Lakkireddy 		return err;
35492c8a632SRahul Lakkireddy 
35592c8a632SRahul Lakkireddy 	for_each_port(adap, i) {
35692c8a632SRahul Lakkireddy 		struct port_info *pi = adap2pinfo(adap, i);
35792c8a632SRahul Lakkireddy 
35892c8a632SRahul Lakkireddy 		pi->rss = rte_zmalloc(NULL, pi->rss_size, 0);
35992c8a632SRahul Lakkireddy 		if (!pi->rss)
36092c8a632SRahul Lakkireddy 			return -ENOMEM;
36192c8a632SRahul Lakkireddy 	}
36292c8a632SRahul Lakkireddy 	return 0;
36392c8a632SRahul Lakkireddy }
36492c8a632SRahul Lakkireddy 
36583189849SRahul Lakkireddy static void print_port_info(struct adapter *adap)
36683189849SRahul Lakkireddy {
36783189849SRahul Lakkireddy 	int i;
36883189849SRahul Lakkireddy 	char buf[80];
36983189849SRahul Lakkireddy 	struct rte_pci_addr *loc = &adap->pdev->addr;
37083189849SRahul Lakkireddy 
37183189849SRahul Lakkireddy 	for_each_port(adap, i) {
37283189849SRahul Lakkireddy 		const struct port_info *pi = &adap->port[i];
37383189849SRahul Lakkireddy 		char *bufp = buf;
37483189849SRahul Lakkireddy 
37583189849SRahul Lakkireddy 		if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_100M)
37683189849SRahul Lakkireddy 			bufp += sprintf(bufp, "100/");
37783189849SRahul Lakkireddy 		if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_1G)
37883189849SRahul Lakkireddy 			bufp += sprintf(bufp, "1000/");
37983189849SRahul Lakkireddy 		if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_10G)
38083189849SRahul Lakkireddy 			bufp += sprintf(bufp, "10G/");
38183189849SRahul Lakkireddy 		if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_40G)
38283189849SRahul Lakkireddy 			bufp += sprintf(bufp, "40G/");
38383189849SRahul Lakkireddy 		if (bufp != buf)
38483189849SRahul Lakkireddy 			--bufp;
38583189849SRahul Lakkireddy 		sprintf(bufp, "BASE-%s",
386*efa8a43eSBruce Richardson 			t4_get_port_type_description(
387*efa8a43eSBruce Richardson 					(enum fw_port_type)pi->port_type));
38883189849SRahul Lakkireddy 
38983189849SRahul Lakkireddy 		dev_info(adap,
39083189849SRahul Lakkireddy 			 " " PCI_PRI_FMT " Chelsio rev %d %s %s\n",
39183189849SRahul Lakkireddy 			 loc->domain, loc->bus, loc->devid, loc->function,
39283189849SRahul Lakkireddy 			 CHELSIO_CHIP_RELEASE(adap->params.chip), buf,
39383189849SRahul Lakkireddy 			 (adap->flags & USING_MSIX) ? " MSI-X" :
39483189849SRahul Lakkireddy 			 (adap->flags & USING_MSI) ? " MSI" : "");
39583189849SRahul Lakkireddy 	}
39683189849SRahul Lakkireddy }
39783189849SRahul Lakkireddy 
39883189849SRahul Lakkireddy /*
39983189849SRahul Lakkireddy  * Tweak configuration based on system architecture, etc.  Most of these have
40083189849SRahul Lakkireddy  * defaults assigned to them by Firmware Configuration Files (if we're using
40183189849SRahul Lakkireddy  * them) but need to be explicitly set if we're using hard-coded
40283189849SRahul Lakkireddy  * initialization. So these are essentially common tweaks/settings for
40383189849SRahul Lakkireddy  * Configuration Files and hard-coded initialization ...
40483189849SRahul Lakkireddy  */
40583189849SRahul Lakkireddy static int adap_init0_tweaks(struct adapter *adapter)
40683189849SRahul Lakkireddy {
40783189849SRahul Lakkireddy 	u8 rx_dma_offset;
40883189849SRahul Lakkireddy 
40983189849SRahul Lakkireddy 	/*
41083189849SRahul Lakkireddy 	 * Fix up various Host-Dependent Parameters like Page Size, Cache
41183189849SRahul Lakkireddy 	 * Line Size, etc.  The firmware default is for a 4KB Page Size and
41283189849SRahul Lakkireddy 	 * 64B Cache Line Size ...
41383189849SRahul Lakkireddy 	 */
41483189849SRahul Lakkireddy 	t4_fixup_host_params_compat(adapter, PAGE_SIZE, L1_CACHE_BYTES,
41583189849SRahul Lakkireddy 				    T5_LAST_REV);
41683189849SRahul Lakkireddy 
41783189849SRahul Lakkireddy 	/*
41883189849SRahul Lakkireddy 	 * Keep the chip default offset to deliver Ingress packets into our
41983189849SRahul Lakkireddy 	 * DMA buffers to zero
42083189849SRahul Lakkireddy 	 */
42183189849SRahul Lakkireddy 	rx_dma_offset = 0;
42283189849SRahul Lakkireddy 	t4_set_reg_field(adapter, A_SGE_CONTROL, V_PKTSHIFT(M_PKTSHIFT),
42383189849SRahul Lakkireddy 			 V_PKTSHIFT(rx_dma_offset));
42483189849SRahul Lakkireddy 
42583189849SRahul Lakkireddy 	/*
42683189849SRahul Lakkireddy 	 * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
42783189849SRahul Lakkireddy 	 * adds the pseudo header itself.
42883189849SRahul Lakkireddy 	 */
42983189849SRahul Lakkireddy 	t4_tp_wr_bits_indirect(adapter, A_TP_INGRESS_CONFIG,
43083189849SRahul Lakkireddy 			       F_CSUM_HAS_PSEUDO_HDR, 0);
43183189849SRahul Lakkireddy 
43283189849SRahul Lakkireddy 	return 0;
43383189849SRahul Lakkireddy }
43483189849SRahul Lakkireddy 
43583189849SRahul Lakkireddy /*
43683189849SRahul Lakkireddy  * Attempt to initialize the adapter via a Firmware Configuration File.
43783189849SRahul Lakkireddy  */
43883189849SRahul Lakkireddy static int adap_init0_config(struct adapter *adapter, int reset)
43983189849SRahul Lakkireddy {
44083189849SRahul Lakkireddy 	struct fw_caps_config_cmd caps_cmd;
44183189849SRahul Lakkireddy 	unsigned long mtype = 0, maddr = 0;
44283189849SRahul Lakkireddy 	u32 finiver, finicsum, cfcsum;
44383189849SRahul Lakkireddy 	int ret;
44483189849SRahul Lakkireddy 	int config_issued = 0;
44583189849SRahul Lakkireddy 	int cfg_addr;
44683189849SRahul Lakkireddy 	char config_name[20];
44783189849SRahul Lakkireddy 
44883189849SRahul Lakkireddy 	/*
44983189849SRahul Lakkireddy 	 * Reset device if necessary.
45083189849SRahul Lakkireddy 	 */
45183189849SRahul Lakkireddy 	if (reset) {
45283189849SRahul Lakkireddy 		ret = t4_fw_reset(adapter, adapter->mbox,
45383189849SRahul Lakkireddy 				  F_PIORSTMODE | F_PIORST);
45483189849SRahul Lakkireddy 		if (ret < 0) {
45583189849SRahul Lakkireddy 			dev_warn(adapter, "Firmware reset failed, error %d\n",
45683189849SRahul Lakkireddy 				 -ret);
45783189849SRahul Lakkireddy 			goto bye;
45883189849SRahul Lakkireddy 		}
45983189849SRahul Lakkireddy 	}
46083189849SRahul Lakkireddy 
46183189849SRahul Lakkireddy 	cfg_addr = t4_flash_cfg_addr(adapter);
46283189849SRahul Lakkireddy 	if (cfg_addr < 0) {
46383189849SRahul Lakkireddy 		ret = cfg_addr;
46483189849SRahul Lakkireddy 		dev_warn(adapter, "Finding address for firmware config file in flash failed, error %d\n",
46583189849SRahul Lakkireddy 			 -ret);
46683189849SRahul Lakkireddy 		goto bye;
46783189849SRahul Lakkireddy 	}
46883189849SRahul Lakkireddy 
46983189849SRahul Lakkireddy 	strcpy(config_name, "On Flash");
47083189849SRahul Lakkireddy 	mtype = FW_MEMTYPE_CF_FLASH;
47183189849SRahul Lakkireddy 	maddr = cfg_addr;
47283189849SRahul Lakkireddy 
47383189849SRahul Lakkireddy 	/*
47483189849SRahul Lakkireddy 	 * Issue a Capability Configuration command to the firmware to get it
47583189849SRahul Lakkireddy 	 * to parse the Configuration File.  We don't use t4_fw_config_file()
47683189849SRahul Lakkireddy 	 * because we want the ability to modify various features after we've
47783189849SRahul Lakkireddy 	 * processed the configuration file ...
47883189849SRahul Lakkireddy 	 */
47983189849SRahul Lakkireddy 	memset(&caps_cmd, 0, sizeof(caps_cmd));
48083189849SRahul Lakkireddy 	caps_cmd.op_to_write = cpu_to_be32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
48183189849SRahul Lakkireddy 					   F_FW_CMD_REQUEST | F_FW_CMD_READ);
48283189849SRahul Lakkireddy 	caps_cmd.cfvalid_to_len16 =
48383189849SRahul Lakkireddy 		cpu_to_be32(F_FW_CAPS_CONFIG_CMD_CFVALID |
48483189849SRahul Lakkireddy 			    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
48583189849SRahul Lakkireddy 			    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(maddr >> 16) |
48683189849SRahul Lakkireddy 			    FW_LEN16(caps_cmd));
48783189849SRahul Lakkireddy 	ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
48883189849SRahul Lakkireddy 			 &caps_cmd);
48983189849SRahul Lakkireddy 	/*
49083189849SRahul Lakkireddy 	 * If the CAPS_CONFIG failed with an ENOENT (for a Firmware
49183189849SRahul Lakkireddy 	 * Configuration File in FLASH), our last gasp effort is to use the
49283189849SRahul Lakkireddy 	 * Firmware Configuration File which is embedded in the firmware.  A
49383189849SRahul Lakkireddy 	 * very few early versions of the firmware didn't have one embedded
49483189849SRahul Lakkireddy 	 * but we can ignore those.
49583189849SRahul Lakkireddy 	 */
49683189849SRahul Lakkireddy 	if (ret == -ENOENT) {
49783189849SRahul Lakkireddy 		dev_info(adapter, "%s: Going for embedded config in firmware..\n",
49883189849SRahul Lakkireddy 			 __func__);
49983189849SRahul Lakkireddy 
50083189849SRahul Lakkireddy 		memset(&caps_cmd, 0, sizeof(caps_cmd));
50183189849SRahul Lakkireddy 		caps_cmd.op_to_write =
50283189849SRahul Lakkireddy 			cpu_to_be32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
50383189849SRahul Lakkireddy 				    F_FW_CMD_REQUEST | F_FW_CMD_READ);
50483189849SRahul Lakkireddy 		caps_cmd.cfvalid_to_len16 = cpu_to_be32(FW_LEN16(caps_cmd));
50583189849SRahul Lakkireddy 		ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd,
50683189849SRahul Lakkireddy 				 sizeof(caps_cmd), &caps_cmd);
50783189849SRahul Lakkireddy 		strcpy(config_name, "Firmware Default");
50883189849SRahul Lakkireddy 	}
50983189849SRahul Lakkireddy 
51083189849SRahul Lakkireddy 	config_issued = 1;
51183189849SRahul Lakkireddy 	if (ret < 0)
51283189849SRahul Lakkireddy 		goto bye;
51383189849SRahul Lakkireddy 
51483189849SRahul Lakkireddy 	finiver = be32_to_cpu(caps_cmd.finiver);
51583189849SRahul Lakkireddy 	finicsum = be32_to_cpu(caps_cmd.finicsum);
51683189849SRahul Lakkireddy 	cfcsum = be32_to_cpu(caps_cmd.cfcsum);
51783189849SRahul Lakkireddy 	if (finicsum != cfcsum)
51883189849SRahul Lakkireddy 		dev_warn(adapter, "Configuration File checksum mismatch: [fini] csum=%#x, computed csum=%#x\n",
51983189849SRahul Lakkireddy 			 finicsum, cfcsum);
52083189849SRahul Lakkireddy 
52183189849SRahul Lakkireddy 	/*
52283189849SRahul Lakkireddy 	 * If we're a pure NIC driver then disable all offloading facilities.
52383189849SRahul Lakkireddy 	 * This will allow the firmware to optimize aspects of the hardware
52483189849SRahul Lakkireddy 	 * configuration which will result in improved performance.
52583189849SRahul Lakkireddy 	 */
52683189849SRahul Lakkireddy 	caps_cmd.niccaps &= cpu_to_be16(~(FW_CAPS_CONFIG_NIC_HASHFILTER |
52783189849SRahul Lakkireddy 					  FW_CAPS_CONFIG_NIC_ETHOFLD));
52883189849SRahul Lakkireddy 	caps_cmd.toecaps = 0;
52983189849SRahul Lakkireddy 	caps_cmd.iscsicaps = 0;
53083189849SRahul Lakkireddy 	caps_cmd.rdmacaps = 0;
53183189849SRahul Lakkireddy 	caps_cmd.fcoecaps = 0;
53283189849SRahul Lakkireddy 
53383189849SRahul Lakkireddy 	/*
53483189849SRahul Lakkireddy 	 * And now tell the firmware to use the configuration we just loaded.
53583189849SRahul Lakkireddy 	 */
53683189849SRahul Lakkireddy 	caps_cmd.op_to_write = cpu_to_be32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
53783189849SRahul Lakkireddy 					   F_FW_CMD_REQUEST | F_FW_CMD_WRITE);
53883189849SRahul Lakkireddy 	caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
53983189849SRahul Lakkireddy 	ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
54083189849SRahul Lakkireddy 			 NULL);
54183189849SRahul Lakkireddy 	if (ret < 0) {
54283189849SRahul Lakkireddy 		dev_warn(adapter, "Unable to finalize Firmware Capabilities %d\n",
54383189849SRahul Lakkireddy 			 -ret);
54483189849SRahul Lakkireddy 		goto bye;
54583189849SRahul Lakkireddy 	}
54683189849SRahul Lakkireddy 
54783189849SRahul Lakkireddy 	/*
54883189849SRahul Lakkireddy 	 * Tweak configuration based on system architecture, etc.
54983189849SRahul Lakkireddy 	 */
55083189849SRahul Lakkireddy 	ret = adap_init0_tweaks(adapter);
55183189849SRahul Lakkireddy 	if (ret < 0) {
55283189849SRahul Lakkireddy 		dev_warn(adapter, "Unable to do init0-tweaks %d\n", -ret);
55383189849SRahul Lakkireddy 		goto bye;
55483189849SRahul Lakkireddy 	}
55583189849SRahul Lakkireddy 
55683189849SRahul Lakkireddy 	/*
55783189849SRahul Lakkireddy 	 * And finally tell the firmware to initialize itself using the
55883189849SRahul Lakkireddy 	 * parameters from the Configuration File.
55983189849SRahul Lakkireddy 	 */
56083189849SRahul Lakkireddy 	ret = t4_fw_initialize(adapter, adapter->mbox);
56183189849SRahul Lakkireddy 	if (ret < 0) {
56283189849SRahul Lakkireddy 		dev_warn(adapter, "Initializing Firmware failed, error %d\n",
56383189849SRahul Lakkireddy 			 -ret);
56483189849SRahul Lakkireddy 		goto bye;
56583189849SRahul Lakkireddy 	}
56683189849SRahul Lakkireddy 
56783189849SRahul Lakkireddy 	/*
56883189849SRahul Lakkireddy 	 * Return successfully and note that we're operating with parameters
56983189849SRahul Lakkireddy 	 * not supplied by the driver, rather than from hard-wired
57083189849SRahul Lakkireddy 	 * initialization constants burried in the driver.
57183189849SRahul Lakkireddy 	 */
57283189849SRahul Lakkireddy 	dev_info(adapter,
57383189849SRahul Lakkireddy 		 "Successfully configured using Firmware Configuration File \"%s\", version %#x, computed checksum %#x\n",
57483189849SRahul Lakkireddy 		 config_name, finiver, cfcsum);
57583189849SRahul Lakkireddy 
57683189849SRahul Lakkireddy 	return 0;
57783189849SRahul Lakkireddy 
57883189849SRahul Lakkireddy 	/*
57983189849SRahul Lakkireddy 	 * Something bad happened.  Return the error ...  (If the "error"
58083189849SRahul Lakkireddy 	 * is that there's no Configuration File on the adapter we don't
58183189849SRahul Lakkireddy 	 * want to issue a warning since this is fairly common.)
58283189849SRahul Lakkireddy 	 */
58383189849SRahul Lakkireddy bye:
58483189849SRahul Lakkireddy 	if (config_issued && ret != -ENOENT)
58583189849SRahul Lakkireddy 		dev_warn(adapter, "\"%s\" configuration file error %d\n",
58683189849SRahul Lakkireddy 			 config_name, -ret);
58783189849SRahul Lakkireddy 
58883189849SRahul Lakkireddy 	dev_debug(adapter, "%s: returning ret = %d ..\n", __func__, ret);
58983189849SRahul Lakkireddy 	return ret;
59083189849SRahul Lakkireddy }
59183189849SRahul Lakkireddy 
59283189849SRahul Lakkireddy static int adap_init0(struct adapter *adap)
59383189849SRahul Lakkireddy {
59483189849SRahul Lakkireddy 	int ret = 0;
59583189849SRahul Lakkireddy 	u32 v, port_vec;
59683189849SRahul Lakkireddy 	enum dev_state state;
59783189849SRahul Lakkireddy 	u32 params[7], val[7];
59883189849SRahul Lakkireddy 	int reset = 1;
59983189849SRahul Lakkireddy 	int mbox = adap->mbox;
60083189849SRahul Lakkireddy 
60183189849SRahul Lakkireddy 	/*
60283189849SRahul Lakkireddy 	 * Contact FW, advertising Master capability.
60383189849SRahul Lakkireddy 	 */
60483189849SRahul Lakkireddy 	ret = t4_fw_hello(adap, adap->mbox, adap->mbox, MASTER_MAY, &state);
60583189849SRahul Lakkireddy 	if (ret < 0) {
60683189849SRahul Lakkireddy 		dev_err(adap, "%s: could not connect to FW, error %d\n",
60783189849SRahul Lakkireddy 			__func__, -ret);
60883189849SRahul Lakkireddy 		goto bye;
60983189849SRahul Lakkireddy 	}
61083189849SRahul Lakkireddy 
61183189849SRahul Lakkireddy 	CXGBE_DEBUG_MBOX(adap, "%s: adap->mbox = %d; ret = %d\n", __func__,
61283189849SRahul Lakkireddy 			 adap->mbox, ret);
61383189849SRahul Lakkireddy 
61483189849SRahul Lakkireddy 	if (ret == mbox)
61583189849SRahul Lakkireddy 		adap->flags |= MASTER_PF;
61683189849SRahul Lakkireddy 
61783189849SRahul Lakkireddy 	if (state == DEV_STATE_INIT) {
61883189849SRahul Lakkireddy 		/*
61983189849SRahul Lakkireddy 		 * Force halt and reset FW because a previous instance may have
62083189849SRahul Lakkireddy 		 * exited abnormally without properly shutting down
62183189849SRahul Lakkireddy 		 */
62283189849SRahul Lakkireddy 		ret = t4_fw_halt(adap, adap->mbox, reset);
62383189849SRahul Lakkireddy 		if (ret < 0) {
62483189849SRahul Lakkireddy 			dev_err(adap, "Failed to halt. Exit.\n");
62583189849SRahul Lakkireddy 			goto bye;
62683189849SRahul Lakkireddy 		}
62783189849SRahul Lakkireddy 
62883189849SRahul Lakkireddy 		ret = t4_fw_restart(adap, adap->mbox, reset);
62983189849SRahul Lakkireddy 		if (ret < 0) {
63083189849SRahul Lakkireddy 			dev_err(adap, "Failed to restart. Exit.\n");
63183189849SRahul Lakkireddy 			goto bye;
63283189849SRahul Lakkireddy 		}
633*efa8a43eSBruce Richardson 		state = (enum dev_state)((unsigned)state & ~DEV_STATE_INIT);
63483189849SRahul Lakkireddy 	}
63583189849SRahul Lakkireddy 
63683189849SRahul Lakkireddy 	t4_get_fw_version(adap, &adap->params.fw_vers);
63783189849SRahul Lakkireddy 	t4_get_tp_version(adap, &adap->params.tp_vers);
63883189849SRahul Lakkireddy 
63983189849SRahul Lakkireddy 	dev_info(adap, "fw: %u.%u.%u.%u, TP: %u.%u.%u.%u\n",
64083189849SRahul Lakkireddy 		 G_FW_HDR_FW_VER_MAJOR(adap->params.fw_vers),
64183189849SRahul Lakkireddy 		 G_FW_HDR_FW_VER_MINOR(adap->params.fw_vers),
64283189849SRahul Lakkireddy 		 G_FW_HDR_FW_VER_MICRO(adap->params.fw_vers),
64383189849SRahul Lakkireddy 		 G_FW_HDR_FW_VER_BUILD(adap->params.fw_vers),
64483189849SRahul Lakkireddy 		 G_FW_HDR_FW_VER_MAJOR(adap->params.tp_vers),
64583189849SRahul Lakkireddy 		 G_FW_HDR_FW_VER_MINOR(adap->params.tp_vers),
64683189849SRahul Lakkireddy 		 G_FW_HDR_FW_VER_MICRO(adap->params.tp_vers),
64783189849SRahul Lakkireddy 		 G_FW_HDR_FW_VER_BUILD(adap->params.tp_vers));
64883189849SRahul Lakkireddy 
64983189849SRahul Lakkireddy 	ret = t4_get_core_clock(adap, &adap->params.vpd);
65083189849SRahul Lakkireddy 	if (ret < 0) {
65183189849SRahul Lakkireddy 		dev_err(adap, "%s: could not get core clock, error %d\n",
65283189849SRahul Lakkireddy 			__func__, -ret);
65383189849SRahul Lakkireddy 		goto bye;
65483189849SRahul Lakkireddy 	}
65583189849SRahul Lakkireddy 
65683189849SRahul Lakkireddy 	/*
65783189849SRahul Lakkireddy 	 * Find out what ports are available to us.  Note that we need to do
65883189849SRahul Lakkireddy 	 * this before calling adap_init0_no_config() since it needs nports
65983189849SRahul Lakkireddy 	 * and portvec ...
66083189849SRahul Lakkireddy 	 */
66183189849SRahul Lakkireddy 	v = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
66283189849SRahul Lakkireddy 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_PORTVEC);
66383189849SRahul Lakkireddy 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, &v, &port_vec);
66483189849SRahul Lakkireddy 	if (ret < 0) {
66583189849SRahul Lakkireddy 		dev_err(adap, "%s: failure in t4_queury_params; error = %d\n",
66683189849SRahul Lakkireddy 			__func__, ret);
66783189849SRahul Lakkireddy 		goto bye;
66883189849SRahul Lakkireddy 	}
66983189849SRahul Lakkireddy 
67083189849SRahul Lakkireddy 	adap->params.nports = hweight32(port_vec);
67183189849SRahul Lakkireddy 	adap->params.portvec = port_vec;
67283189849SRahul Lakkireddy 
67383189849SRahul Lakkireddy 	dev_debug(adap, "%s: adap->params.nports = %u\n", __func__,
67483189849SRahul Lakkireddy 		  adap->params.nports);
67583189849SRahul Lakkireddy 
67683189849SRahul Lakkireddy 	/*
67783189849SRahul Lakkireddy 	 * If the firmware is initialized already (and we're not forcing a
67883189849SRahul Lakkireddy 	 * master initialization), note that we're living with existing
67983189849SRahul Lakkireddy 	 * adapter parameters.  Otherwise, it's time to try initializing the
68083189849SRahul Lakkireddy 	 * adapter ...
68183189849SRahul Lakkireddy 	 */
68283189849SRahul Lakkireddy 	if (state == DEV_STATE_INIT) {
68383189849SRahul Lakkireddy 		dev_info(adap, "Coming up as %s: Adapter already initialized\n",
68483189849SRahul Lakkireddy 			 adap->flags & MASTER_PF ? "MASTER" : "SLAVE");
68583189849SRahul Lakkireddy 	} else {
68683189849SRahul Lakkireddy 		dev_info(adap, "Coming up as MASTER: Initializing adapter\n");
68783189849SRahul Lakkireddy 
68883189849SRahul Lakkireddy 		ret = adap_init0_config(adap, reset);
68983189849SRahul Lakkireddy 		if (ret == -ENOENT) {
69083189849SRahul Lakkireddy 			dev_err(adap,
69183189849SRahul Lakkireddy 				"No Configuration File present on adapter. Using hard-wired configuration parameters.\n");
69283189849SRahul Lakkireddy 			goto bye;
69383189849SRahul Lakkireddy 		}
69483189849SRahul Lakkireddy 	}
69583189849SRahul Lakkireddy 	if (ret < 0) {
69683189849SRahul Lakkireddy 		dev_err(adap, "could not initialize adapter, error %d\n", -ret);
69783189849SRahul Lakkireddy 		goto bye;
69883189849SRahul Lakkireddy 	}
69983189849SRahul Lakkireddy 
70083189849SRahul Lakkireddy 	/*
70183189849SRahul Lakkireddy 	 * Give the SGE code a chance to pull in anything that it needs ...
70283189849SRahul Lakkireddy 	 * Note that this must be called after we retrieve our VPD parameters
70383189849SRahul Lakkireddy 	 * in order to know how to convert core ticks to seconds, etc.
70483189849SRahul Lakkireddy 	 */
70583189849SRahul Lakkireddy 	ret = t4_sge_init(adap);
70683189849SRahul Lakkireddy 	if (ret < 0) {
70783189849SRahul Lakkireddy 		dev_err(adap, "t4_sge_init failed with error %d\n",
70883189849SRahul Lakkireddy 			-ret);
70983189849SRahul Lakkireddy 		goto bye;
71083189849SRahul Lakkireddy 	}
71183189849SRahul Lakkireddy 
71283189849SRahul Lakkireddy 	/*
71383189849SRahul Lakkireddy 	 * Grab some of our basic fundamental operating parameters.
71483189849SRahul Lakkireddy 	 */
71583189849SRahul Lakkireddy #define FW_PARAM_DEV(param) \
71683189849SRahul Lakkireddy 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) | \
71783189849SRahul Lakkireddy 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_##param))
71883189849SRahul Lakkireddy 
71983189849SRahul Lakkireddy #define FW_PARAM_PFVF(param) \
72083189849SRahul Lakkireddy 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_PFVF) | \
72183189849SRahul Lakkireddy 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_PFVF_##param) |  \
72283189849SRahul Lakkireddy 	 V_FW_PARAMS_PARAM_Y(0) | \
72383189849SRahul Lakkireddy 	 V_FW_PARAMS_PARAM_Z(0))
72483189849SRahul Lakkireddy 
72583189849SRahul Lakkireddy 	/* If we're running on newer firmware, let it know that we're
72683189849SRahul Lakkireddy 	 * prepared to deal with encapsulated CPL messages.  Older
72783189849SRahul Lakkireddy 	 * firmware won't understand this and we'll just get
72883189849SRahul Lakkireddy 	 * unencapsulated messages ...
72983189849SRahul Lakkireddy 	 */
73083189849SRahul Lakkireddy 	params[0] = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
73183189849SRahul Lakkireddy 	val[0] = 1;
73283189849SRahul Lakkireddy 	(void)t4_set_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
73383189849SRahul Lakkireddy 
73483189849SRahul Lakkireddy 	/*
73583189849SRahul Lakkireddy 	 * Find out whether we're allowed to use the T5+ ULPTX MEMWRITE DSGL
73683189849SRahul Lakkireddy 	 * capability.  Earlier versions of the firmware didn't have the
73783189849SRahul Lakkireddy 	 * ULPTX_MEMWRITE_DSGL so we'll interpret a query failure as no
73883189849SRahul Lakkireddy 	 * permission to use ULPTX MEMWRITE DSGL.
73983189849SRahul Lakkireddy 	 */
74083189849SRahul Lakkireddy 	if (is_t4(adap->params.chip)) {
74183189849SRahul Lakkireddy 		adap->params.ulptx_memwrite_dsgl = false;
74283189849SRahul Lakkireddy 	} else {
74383189849SRahul Lakkireddy 		params[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
74483189849SRahul Lakkireddy 		ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
74583189849SRahul Lakkireddy 				      1, params, val);
74683189849SRahul Lakkireddy 		adap->params.ulptx_memwrite_dsgl = (ret == 0 && val[0] != 0);
74783189849SRahul Lakkireddy 	}
74883189849SRahul Lakkireddy 
74983189849SRahul Lakkireddy 	/*
75083189849SRahul Lakkireddy 	 * The MTU/MSS Table is initialized by now, so load their values.  If
75183189849SRahul Lakkireddy 	 * we're initializing the adapter, then we'll make any modifications
75283189849SRahul Lakkireddy 	 * we want to the MTU/MSS Table and also initialize the congestion
75383189849SRahul Lakkireddy 	 * parameters.
75483189849SRahul Lakkireddy 	 */
75583189849SRahul Lakkireddy 	t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
75683189849SRahul Lakkireddy 	if (state != DEV_STATE_INIT) {
75783189849SRahul Lakkireddy 		int i;
75883189849SRahul Lakkireddy 
75983189849SRahul Lakkireddy 		/*
76083189849SRahul Lakkireddy 		 * The default MTU Table contains values 1492 and 1500.
76183189849SRahul Lakkireddy 		 * However, for TCP, it's better to have two values which are
76283189849SRahul Lakkireddy 		 * a multiple of 8 +/- 4 bytes apart near this popular MTU.
76383189849SRahul Lakkireddy 		 * This allows us to have a TCP Data Payload which is a
76483189849SRahul Lakkireddy 		 * multiple of 8 regardless of what combination of TCP Options
76583189849SRahul Lakkireddy 		 * are in use (always a multiple of 4 bytes) which is
76683189849SRahul Lakkireddy 		 * important for performance reasons.  For instance, if no
76783189849SRahul Lakkireddy 		 * options are in use, then we have a 20-byte IP header and a
76883189849SRahul Lakkireddy 		 * 20-byte TCP header.  In this case, a 1500-byte MSS would
76983189849SRahul Lakkireddy 		 * result in a TCP Data Payload of 1500 - 40 == 1460 bytes
77083189849SRahul Lakkireddy 		 * which is not a multiple of 8.  So using an MSS of 1488 in
77183189849SRahul Lakkireddy 		 * this case results in a TCP Data Payload of 1448 bytes which
77283189849SRahul Lakkireddy 		 * is a multiple of 8.  On the other hand, if 12-byte TCP Time
77383189849SRahul Lakkireddy 		 * Stamps have been negotiated, then an MTU of 1500 bytes
77483189849SRahul Lakkireddy 		 * results in a TCP Data Payload of 1448 bytes which, as
77583189849SRahul Lakkireddy 		 * above, is a multiple of 8 bytes ...
77683189849SRahul Lakkireddy 		 */
77783189849SRahul Lakkireddy 		for (i = 0; i < NMTUS; i++)
77883189849SRahul Lakkireddy 			if (adap->params.mtus[i] == 1492) {
77983189849SRahul Lakkireddy 				adap->params.mtus[i] = 1488;
78083189849SRahul Lakkireddy 				break;
78183189849SRahul Lakkireddy 			}
78283189849SRahul Lakkireddy 
78383189849SRahul Lakkireddy 		t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
78483189849SRahul Lakkireddy 			     adap->params.b_wnd);
78583189849SRahul Lakkireddy 	}
78683189849SRahul Lakkireddy 	t4_init_sge_params(adap);
78783189849SRahul Lakkireddy 	t4_init_tp_params(adap);
78883189849SRahul Lakkireddy 
78983189849SRahul Lakkireddy 	adap->params.drv_memwin = MEMWIN_NIC;
79083189849SRahul Lakkireddy 	adap->flags |= FW_OK;
79183189849SRahul Lakkireddy 	dev_debug(adap, "%s: returning zero..\n", __func__);
79283189849SRahul Lakkireddy 	return 0;
79383189849SRahul Lakkireddy 
79483189849SRahul Lakkireddy 	/*
79583189849SRahul Lakkireddy 	 * Something bad happened.  If a command timed out or failed with EIO
79683189849SRahul Lakkireddy 	 * FW does not operate within its spec or something catastrophic
79783189849SRahul Lakkireddy 	 * happened to HW/FW, stop issuing commands.
79883189849SRahul Lakkireddy 	 */
79983189849SRahul Lakkireddy bye:
80083189849SRahul Lakkireddy 	if (ret != -ETIMEDOUT && ret != -EIO)
80183189849SRahul Lakkireddy 		t4_fw_bye(adap, adap->mbox);
80283189849SRahul Lakkireddy 	return ret;
80383189849SRahul Lakkireddy }
80483189849SRahul Lakkireddy 
80583189849SRahul Lakkireddy /**
80683189849SRahul Lakkireddy  * t4_os_portmod_changed - handle port module changes
80783189849SRahul Lakkireddy  * @adap: the adapter associated with the module change
80883189849SRahul Lakkireddy  * @port_id: the port index whose module status has changed
80983189849SRahul Lakkireddy  *
81083189849SRahul Lakkireddy  * This is the OS-dependent handler for port module changes.  It is
81183189849SRahul Lakkireddy  * invoked when a port module is removed or inserted for any OS-specific
81283189849SRahul Lakkireddy  * processing.
81383189849SRahul Lakkireddy  */
81483189849SRahul Lakkireddy void t4_os_portmod_changed(const struct adapter *adap, int port_id)
81583189849SRahul Lakkireddy {
81683189849SRahul Lakkireddy 	static const char * const mod_str[] = {
81783189849SRahul Lakkireddy 		NULL, "LR", "SR", "ER", "passive DA", "active DA", "LRM"
81883189849SRahul Lakkireddy 	};
81983189849SRahul Lakkireddy 
82083189849SRahul Lakkireddy 	const struct port_info *pi = &adap->port[port_id];
82183189849SRahul Lakkireddy 
82283189849SRahul Lakkireddy 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
82383189849SRahul Lakkireddy 		dev_info(adap, "Port%d: port module unplugged\n", pi->port_id);
82483189849SRahul Lakkireddy 	else if (pi->mod_type < ARRAY_SIZE(mod_str))
82583189849SRahul Lakkireddy 		dev_info(adap, "Port%d: %s port module inserted\n", pi->port_id,
82683189849SRahul Lakkireddy 			 mod_str[pi->mod_type]);
82783189849SRahul Lakkireddy 	else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
82883189849SRahul Lakkireddy 		dev_info(adap, "Port%d: unsupported optical port module inserted\n",
82983189849SRahul Lakkireddy 			 pi->port_id);
83083189849SRahul Lakkireddy 	else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
83183189849SRahul Lakkireddy 		dev_info(adap, "Port%d: unknown port module inserted, forcing TWINAX\n",
83283189849SRahul Lakkireddy 			 pi->port_id);
83383189849SRahul Lakkireddy 	else if (pi->mod_type == FW_PORT_MOD_TYPE_ERROR)
83483189849SRahul Lakkireddy 		dev_info(adap, "Port%d: transceiver module error\n",
83583189849SRahul Lakkireddy 			 pi->port_id);
83683189849SRahul Lakkireddy 	else
83783189849SRahul Lakkireddy 		dev_info(adap, "Port%d: unknown module type %d inserted\n",
83883189849SRahul Lakkireddy 			 pi->port_id, pi->mod_type);
83983189849SRahul Lakkireddy }
84083189849SRahul Lakkireddy 
84192c8a632SRahul Lakkireddy /**
8420462d115SRahul Lakkireddy  * link_start - enable a port
8430462d115SRahul Lakkireddy  * @dev: the port to enable
8440462d115SRahul Lakkireddy  *
8450462d115SRahul Lakkireddy  * Performs the MAC and PHY actions needed to enable a port.
8460462d115SRahul Lakkireddy  */
8470462d115SRahul Lakkireddy int link_start(struct port_info *pi)
8480462d115SRahul Lakkireddy {
8490462d115SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
8500462d115SRahul Lakkireddy 	int ret;
8510462d115SRahul Lakkireddy 
8520462d115SRahul Lakkireddy 	/*
8530462d115SRahul Lakkireddy 	 * We do not set address filters and promiscuity here, the stack does
8540462d115SRahul Lakkireddy 	 * that step explicitly.
8550462d115SRahul Lakkireddy 	 */
8560462d115SRahul Lakkireddy 	ret = t4_set_rxmode(adapter, adapter->mbox, pi->viid, 1500, -1, -1,
8570462d115SRahul Lakkireddy 			    -1, 1, true);
8580462d115SRahul Lakkireddy 	if (ret == 0) {
8590462d115SRahul Lakkireddy 		ret = t4_change_mac(adapter, adapter->mbox, pi->viid,
8600462d115SRahul Lakkireddy 				    pi->xact_addr_filt,
8610462d115SRahul Lakkireddy 				    (u8 *)&pi->eth_dev->data->mac_addrs[0],
8620462d115SRahul Lakkireddy 				    true, true);
8630462d115SRahul Lakkireddy 		if (ret >= 0) {
8640462d115SRahul Lakkireddy 			pi->xact_addr_filt = ret;
8650462d115SRahul Lakkireddy 			ret = 0;
8660462d115SRahul Lakkireddy 		}
8670462d115SRahul Lakkireddy 	}
8680462d115SRahul Lakkireddy 	if (ret == 0)
8690462d115SRahul Lakkireddy 		ret = t4_link_l1cfg(adapter, adapter->mbox, pi->tx_chan,
8700462d115SRahul Lakkireddy 				    &pi->link_cfg);
8710462d115SRahul Lakkireddy 	if (ret == 0) {
8720462d115SRahul Lakkireddy 		/*
8730462d115SRahul Lakkireddy 		 * Enabling a Virtual Interface can result in an interrupt
8740462d115SRahul Lakkireddy 		 * during the processing of the VI Enable command and, in some
8750462d115SRahul Lakkireddy 		 * paths, result in an attempt to issue another command in the
8760462d115SRahul Lakkireddy 		 * interrupt context.  Thus, we disable interrupts during the
8770462d115SRahul Lakkireddy 		 * course of the VI Enable command ...
8780462d115SRahul Lakkireddy 		 */
8790462d115SRahul Lakkireddy 		ret = t4_enable_vi_params(adapter, adapter->mbox, pi->viid,
8800462d115SRahul Lakkireddy 					  true, true, false);
8810462d115SRahul Lakkireddy 	}
8820462d115SRahul Lakkireddy 	return ret;
8830462d115SRahul Lakkireddy }
8840462d115SRahul Lakkireddy 
8850462d115SRahul Lakkireddy /**
88692c8a632SRahul Lakkireddy  * cxgb4_write_rss - write the RSS table for a given port
88792c8a632SRahul Lakkireddy  * @pi: the port
88892c8a632SRahul Lakkireddy  * @queues: array of queue indices for RSS
88992c8a632SRahul Lakkireddy  *
89092c8a632SRahul Lakkireddy  * Sets up the portion of the HW RSS table for the port's VI to distribute
89192c8a632SRahul Lakkireddy  * packets to the Rx queues in @queues.
89292c8a632SRahul Lakkireddy  */
89392c8a632SRahul Lakkireddy int cxgb4_write_rss(const struct port_info *pi, const u16 *queues)
89492c8a632SRahul Lakkireddy {
89592c8a632SRahul Lakkireddy 	u16 *rss;
89692c8a632SRahul Lakkireddy 	int i, err;
89792c8a632SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
89892c8a632SRahul Lakkireddy 	const struct sge_eth_rxq *rxq;
89992c8a632SRahul Lakkireddy 
90092c8a632SRahul Lakkireddy 	/*  Should never be called before setting up sge eth rx queues */
90192c8a632SRahul Lakkireddy 	BUG_ON(!(adapter->flags & FULL_INIT_DONE));
90292c8a632SRahul Lakkireddy 
90392c8a632SRahul Lakkireddy 	rxq = &adapter->sge.ethrxq[pi->first_qset];
90492c8a632SRahul Lakkireddy 	rss = rte_zmalloc(NULL, pi->rss_size * sizeof(u16), 0);
90592c8a632SRahul Lakkireddy 	if (!rss)
90692c8a632SRahul Lakkireddy 		return -ENOMEM;
90792c8a632SRahul Lakkireddy 
90892c8a632SRahul Lakkireddy 	/* map the queue indices to queue ids */
90992c8a632SRahul Lakkireddy 	for (i = 0; i < pi->rss_size; i++, queues++)
91092c8a632SRahul Lakkireddy 		rss[i] = rxq[*queues].rspq.abs_id;
91192c8a632SRahul Lakkireddy 
91292c8a632SRahul Lakkireddy 	err = t4_config_rss_range(adapter, adapter->pf, pi->viid, 0,
91392c8a632SRahul Lakkireddy 				  pi->rss_size, rss, pi->rss_size);
91492c8a632SRahul Lakkireddy 	/*
91592c8a632SRahul Lakkireddy 	 * If Tunnel All Lookup isn't specified in the global RSS
91692c8a632SRahul Lakkireddy 	 * Configuration, then we need to specify a default Ingress
91792c8a632SRahul Lakkireddy 	 * Queue for any ingress packets which aren't hashed.  We'll
91892c8a632SRahul Lakkireddy 	 * use our first ingress queue ...
91992c8a632SRahul Lakkireddy 	 */
92092c8a632SRahul Lakkireddy 	if (!err)
92192c8a632SRahul Lakkireddy 		err = t4_config_vi_rss(adapter, adapter->mbox, pi->viid,
92292c8a632SRahul Lakkireddy 				       F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN |
92392c8a632SRahul Lakkireddy 				       F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN |
92492c8a632SRahul Lakkireddy 				       F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
92592c8a632SRahul Lakkireddy 				       F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN |
92692c8a632SRahul Lakkireddy 				       F_FW_RSS_VI_CONFIG_CMD_UDPEN,
92792c8a632SRahul Lakkireddy 				       rss[0]);
92892c8a632SRahul Lakkireddy 	rte_free(rss);
92992c8a632SRahul Lakkireddy 	return err;
93092c8a632SRahul Lakkireddy }
93192c8a632SRahul Lakkireddy 
93292c8a632SRahul Lakkireddy /**
93392c8a632SRahul Lakkireddy  * setup_rss - configure RSS
93492c8a632SRahul Lakkireddy  * @adapter: the adapter
93592c8a632SRahul Lakkireddy  *
93692c8a632SRahul Lakkireddy  * Sets up RSS to distribute packets to multiple receive queues.  We
93792c8a632SRahul Lakkireddy  * configure the RSS CPU lookup table to distribute to the number of HW
93892c8a632SRahul Lakkireddy  * receive queues, and the response queue lookup table to narrow that
93992c8a632SRahul Lakkireddy  * down to the response queues actually configured for each port.
94092c8a632SRahul Lakkireddy  * We always configure the RSS mapping for all ports since the mapping
94192c8a632SRahul Lakkireddy  * table has plenty of entries.
94292c8a632SRahul Lakkireddy  */
94392c8a632SRahul Lakkireddy int setup_rss(struct port_info *pi)
94492c8a632SRahul Lakkireddy {
94592c8a632SRahul Lakkireddy 	int j, err;
94692c8a632SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
94792c8a632SRahul Lakkireddy 
94892c8a632SRahul Lakkireddy 	dev_debug(adapter, "%s:  pi->rss_size = %u; pi->n_rx_qsets = %u\n",
94992c8a632SRahul Lakkireddy 		  __func__, pi->rss_size, pi->n_rx_qsets);
95092c8a632SRahul Lakkireddy 
95192c8a632SRahul Lakkireddy 	if (!pi->flags & PORT_RSS_DONE) {
95292c8a632SRahul Lakkireddy 		if (adapter->flags & FULL_INIT_DONE) {
95392c8a632SRahul Lakkireddy 			/* Fill default values with equal distribution */
95492c8a632SRahul Lakkireddy 			for (j = 0; j < pi->rss_size; j++)
95592c8a632SRahul Lakkireddy 				pi->rss[j] = j % pi->n_rx_qsets;
95692c8a632SRahul Lakkireddy 
95792c8a632SRahul Lakkireddy 			err = cxgb4_write_rss(pi, pi->rss);
95892c8a632SRahul Lakkireddy 			if (err)
95992c8a632SRahul Lakkireddy 				return err;
96092c8a632SRahul Lakkireddy 			pi->flags |= PORT_RSS_DONE;
96192c8a632SRahul Lakkireddy 		}
96292c8a632SRahul Lakkireddy 	}
96392c8a632SRahul Lakkireddy 	return 0;
96492c8a632SRahul Lakkireddy }
96592c8a632SRahul Lakkireddy 
9660462d115SRahul Lakkireddy /*
9670462d115SRahul Lakkireddy  * Enable NAPI scheduling and interrupt generation for all Rx queues.
9680462d115SRahul Lakkireddy  */
9690462d115SRahul Lakkireddy static void enable_rx(struct adapter *adap)
9700462d115SRahul Lakkireddy {
9710462d115SRahul Lakkireddy 	struct sge *s = &adap->sge;
9720462d115SRahul Lakkireddy 	struct sge_rspq *q = &s->fw_evtq;
9730462d115SRahul Lakkireddy 	int i, j;
9740462d115SRahul Lakkireddy 
9750462d115SRahul Lakkireddy 	/* 0-increment GTS to start the timer and enable interrupts */
9760462d115SRahul Lakkireddy 	t4_write_reg(adap, MYPF_REG(A_SGE_PF_GTS),
9770462d115SRahul Lakkireddy 		     V_SEINTARM(q->intr_params) |
9780462d115SRahul Lakkireddy 		     V_INGRESSQID(q->cntxt_id));
9790462d115SRahul Lakkireddy 
9800462d115SRahul Lakkireddy 	for_each_port(adap, i) {
9810462d115SRahul Lakkireddy 		const struct port_info *pi = &adap->port[i];
9820462d115SRahul Lakkireddy 		struct rte_eth_dev *eth_dev = pi->eth_dev;
9830462d115SRahul Lakkireddy 
9840462d115SRahul Lakkireddy 		for (j = 0; j < eth_dev->data->nb_rx_queues; j++) {
9850462d115SRahul Lakkireddy 			q = eth_dev->data->rx_queues[j];
9860462d115SRahul Lakkireddy 
9870462d115SRahul Lakkireddy 			/*
9880462d115SRahul Lakkireddy 			 * 0-increment GTS to start the timer and enable
9890462d115SRahul Lakkireddy 			 * interrupts
9900462d115SRahul Lakkireddy 			 */
9910462d115SRahul Lakkireddy 			t4_write_reg(adap, MYPF_REG(A_SGE_PF_GTS),
9920462d115SRahul Lakkireddy 				     V_SEINTARM(q->intr_params) |
9930462d115SRahul Lakkireddy 				     V_INGRESSQID(q->cntxt_id));
9940462d115SRahul Lakkireddy 		}
9950462d115SRahul Lakkireddy 	}
9960462d115SRahul Lakkireddy }
9970462d115SRahul Lakkireddy 
9980462d115SRahul Lakkireddy /**
9990462d115SRahul Lakkireddy  * cxgb_up - enable the adapter
10000462d115SRahul Lakkireddy  * @adap: adapter being enabled
10010462d115SRahul Lakkireddy  *
10020462d115SRahul Lakkireddy  * Called when the first port is enabled, this function performs the
10030462d115SRahul Lakkireddy  * actions necessary to make an adapter operational, such as completing
10040462d115SRahul Lakkireddy  * the initialization of HW modules, and enabling interrupts.
10050462d115SRahul Lakkireddy  */
10060462d115SRahul Lakkireddy int cxgbe_up(struct adapter *adap)
10070462d115SRahul Lakkireddy {
10080462d115SRahul Lakkireddy 	enable_rx(adap);
10090462d115SRahul Lakkireddy 	t4_sge_tx_monitor_start(adap);
10100462d115SRahul Lakkireddy 	t4_intr_enable(adap);
10110462d115SRahul Lakkireddy 	adap->flags |= FULL_INIT_DONE;
10120462d115SRahul Lakkireddy 
10130462d115SRahul Lakkireddy 	/* TODO: deadman watchdog ?? */
10140462d115SRahul Lakkireddy 	return 0;
10150462d115SRahul Lakkireddy }
10160462d115SRahul Lakkireddy 
10170462d115SRahul Lakkireddy /*
10180462d115SRahul Lakkireddy  * Close the port
10190462d115SRahul Lakkireddy  */
10200462d115SRahul Lakkireddy int cxgbe_down(struct port_info *pi)
10210462d115SRahul Lakkireddy {
10220462d115SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
10230462d115SRahul Lakkireddy 	int err = 0;
10240462d115SRahul Lakkireddy 
10250462d115SRahul Lakkireddy 	err = t4_enable_vi(adapter, adapter->mbox, pi->viid, false, false);
10260462d115SRahul Lakkireddy 	if (err) {
10270462d115SRahul Lakkireddy 		dev_err(adapter, "%s: disable_vi failed: %d\n", __func__, err);
10280462d115SRahul Lakkireddy 		return err;
10290462d115SRahul Lakkireddy 	}
10300462d115SRahul Lakkireddy 
10310462d115SRahul Lakkireddy 	t4_reset_link_config(adapter, pi->port_id);
10320462d115SRahul Lakkireddy 	return 0;
10330462d115SRahul Lakkireddy }
10340462d115SRahul Lakkireddy 
10350462d115SRahul Lakkireddy /*
10360462d115SRahul Lakkireddy  * Release resources when all the ports have been stopped.
10370462d115SRahul Lakkireddy  */
10380462d115SRahul Lakkireddy void cxgbe_close(struct adapter *adapter)
10390462d115SRahul Lakkireddy {
10400462d115SRahul Lakkireddy 	struct port_info *pi;
10410462d115SRahul Lakkireddy 	int i;
10420462d115SRahul Lakkireddy 
10430462d115SRahul Lakkireddy 	if (adapter->flags & FULL_INIT_DONE) {
10440462d115SRahul Lakkireddy 		t4_intr_disable(adapter);
10450462d115SRahul Lakkireddy 		t4_sge_tx_monitor_stop(adapter);
10460462d115SRahul Lakkireddy 		t4_free_sge_resources(adapter);
10470462d115SRahul Lakkireddy 		for_each_port(adapter, i) {
10480462d115SRahul Lakkireddy 			pi = adap2pinfo(adapter, i);
10490462d115SRahul Lakkireddy 			if (pi->viid != 0)
10500462d115SRahul Lakkireddy 				t4_free_vi(adapter, adapter->mbox,
10510462d115SRahul Lakkireddy 					   adapter->pf, 0, pi->viid);
10520462d115SRahul Lakkireddy 			rte_free(pi->eth_dev->data->mac_addrs);
10530462d115SRahul Lakkireddy 		}
10540462d115SRahul Lakkireddy 		adapter->flags &= ~FULL_INIT_DONE;
10550462d115SRahul Lakkireddy 	}
10560462d115SRahul Lakkireddy 
10570462d115SRahul Lakkireddy 	if (adapter->flags & FW_OK)
10580462d115SRahul Lakkireddy 		t4_fw_bye(adapter, adapter->mbox);
10590462d115SRahul Lakkireddy }
10600462d115SRahul Lakkireddy 
106183189849SRahul Lakkireddy int cxgbe_probe(struct adapter *adapter)
106283189849SRahul Lakkireddy {
106383189849SRahul Lakkireddy 	struct port_info *pi;
106483189849SRahul Lakkireddy 	int func, i;
106583189849SRahul Lakkireddy 	int err = 0;
106683189849SRahul Lakkireddy 
106783189849SRahul Lakkireddy 	func = G_SOURCEPF(t4_read_reg(adapter, A_PL_WHOAMI));
106883189849SRahul Lakkireddy 	adapter->mbox = func;
106983189849SRahul Lakkireddy 	adapter->pf = func;
107083189849SRahul Lakkireddy 
107183189849SRahul Lakkireddy 	t4_os_lock_init(&adapter->mbox_lock);
107283189849SRahul Lakkireddy 	TAILQ_INIT(&adapter->mbox_list);
107383189849SRahul Lakkireddy 
107483189849SRahul Lakkireddy 	err = t4_prep_adapter(adapter);
107583189849SRahul Lakkireddy 	if (err)
107683189849SRahul Lakkireddy 		return err;
107783189849SRahul Lakkireddy 
107883189849SRahul Lakkireddy 	setup_memwin(adapter);
107983189849SRahul Lakkireddy 	err = adap_init0(adapter);
108083189849SRahul Lakkireddy 	if (err) {
108183189849SRahul Lakkireddy 		dev_err(adapter, "%s: Adapter initialization failed, error %d\n",
108283189849SRahul Lakkireddy 			__func__, err);
108383189849SRahul Lakkireddy 		goto out_free;
108483189849SRahul Lakkireddy 	}
108583189849SRahul Lakkireddy 
108683189849SRahul Lakkireddy 	if (!is_t4(adapter->params.chip)) {
108783189849SRahul Lakkireddy 		/*
108883189849SRahul Lakkireddy 		 * The userspace doorbell BAR is split evenly into doorbell
108983189849SRahul Lakkireddy 		 * regions, each associated with an egress queue.  If this
109083189849SRahul Lakkireddy 		 * per-queue region is large enough (at least UDBS_SEG_SIZE)
109183189849SRahul Lakkireddy 		 * then it can be used to submit a tx work request with an
109283189849SRahul Lakkireddy 		 * implied doorbell.  Enable write combining on the BAR if
109383189849SRahul Lakkireddy 		 * there is room for such work requests.
109483189849SRahul Lakkireddy 		 */
109583189849SRahul Lakkireddy 		int s_qpp, qpp, num_seg;
109683189849SRahul Lakkireddy 
109783189849SRahul Lakkireddy 		s_qpp = (S_QUEUESPERPAGEPF0 +
109883189849SRahul Lakkireddy 			(S_QUEUESPERPAGEPF1 - S_QUEUESPERPAGEPF0) *
109983189849SRahul Lakkireddy 			adapter->pf);
110083189849SRahul Lakkireddy 		qpp = 1 << ((t4_read_reg(adapter,
110183189849SRahul Lakkireddy 				A_SGE_EGRESS_QUEUES_PER_PAGE_PF) >> s_qpp)
110283189849SRahul Lakkireddy 				& M_QUEUESPERPAGEPF0);
110383189849SRahul Lakkireddy 		num_seg = PAGE_SIZE / UDBS_SEG_SIZE;
110483189849SRahul Lakkireddy 		if (qpp > num_seg)
110583189849SRahul Lakkireddy 			dev_warn(adapter, "Incorrect SGE EGRESS QUEUES_PER_PAGE configuration, continuing in debug mode\n");
110683189849SRahul Lakkireddy 
110783189849SRahul Lakkireddy 		adapter->bar2 = (void *)adapter->pdev->mem_resource[2].addr;
110883189849SRahul Lakkireddy 		if (!adapter->bar2) {
110983189849SRahul Lakkireddy 			dev_err(adapter, "cannot map device bar2 region\n");
111083189849SRahul Lakkireddy 			err = -ENOMEM;
111183189849SRahul Lakkireddy 			goto out_free;
111283189849SRahul Lakkireddy 		}
111383189849SRahul Lakkireddy 		t4_write_reg(adapter, A_SGE_STAT_CFG, V_STATSOURCE_T5(7) |
111483189849SRahul Lakkireddy 			     V_STATMODE(0));
111583189849SRahul Lakkireddy 	}
111683189849SRahul Lakkireddy 
111783189849SRahul Lakkireddy 	for_each_port(adapter, i) {
111883189849SRahul Lakkireddy 		char name[RTE_ETH_NAME_MAX_LEN];
111983189849SRahul Lakkireddy 		struct rte_eth_dev_data *data = NULL;
112083189849SRahul Lakkireddy 		const unsigned int numa_node = rte_socket_id();
112183189849SRahul Lakkireddy 
112283189849SRahul Lakkireddy 		pi = &adapter->port[i];
112383189849SRahul Lakkireddy 		pi->adapter = adapter;
112483189849SRahul Lakkireddy 		pi->xact_addr_filt = -1;
112583189849SRahul Lakkireddy 		pi->port_id = i;
112683189849SRahul Lakkireddy 
112783189849SRahul Lakkireddy 		snprintf(name, sizeof(name), "cxgbe%d",
112883189849SRahul Lakkireddy 			 adapter->eth_dev->data->port_id + i);
112983189849SRahul Lakkireddy 
113083189849SRahul Lakkireddy 		if (i == 0) {
113183189849SRahul Lakkireddy 			/* First port is already allocated by DPDK */
113283189849SRahul Lakkireddy 			pi->eth_dev = adapter->eth_dev;
113383189849SRahul Lakkireddy 			goto allocate_mac;
113483189849SRahul Lakkireddy 		}
113583189849SRahul Lakkireddy 
113683189849SRahul Lakkireddy 		/*
113783189849SRahul Lakkireddy 		 * now do all data allocation - for eth_dev structure,
113883189849SRahul Lakkireddy 		 * and internal (private) data for the remaining ports
113983189849SRahul Lakkireddy 		 */
114083189849SRahul Lakkireddy 
114183189849SRahul Lakkireddy 		/* reserve an ethdev entry */
114283189849SRahul Lakkireddy 		pi->eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_PCI);
114383189849SRahul Lakkireddy 		if (!pi->eth_dev)
114483189849SRahul Lakkireddy 			goto out_free;
114583189849SRahul Lakkireddy 
114683189849SRahul Lakkireddy 		data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
114783189849SRahul Lakkireddy 		if (!data)
114883189849SRahul Lakkireddy 			goto out_free;
114983189849SRahul Lakkireddy 
115083189849SRahul Lakkireddy 		data->port_id = adapter->eth_dev->data->port_id + i;
115183189849SRahul Lakkireddy 
115283189849SRahul Lakkireddy 		pi->eth_dev->data = data;
115383189849SRahul Lakkireddy 
115483189849SRahul Lakkireddy allocate_mac:
115583189849SRahul Lakkireddy 		pi->eth_dev->pci_dev = adapter->pdev;
115683189849SRahul Lakkireddy 		pi->eth_dev->data->dev_private = pi;
115783189849SRahul Lakkireddy 		pi->eth_dev->driver = adapter->eth_dev->driver;
115883189849SRahul Lakkireddy 		pi->eth_dev->dev_ops = adapter->eth_dev->dev_ops;
11594a01078bSRahul Lakkireddy 		pi->eth_dev->tx_pkt_burst = adapter->eth_dev->tx_pkt_burst;
116092c8a632SRahul Lakkireddy 		pi->eth_dev->rx_pkt_burst = adapter->eth_dev->rx_pkt_burst;
116183189849SRahul Lakkireddy 		TAILQ_INIT(&pi->eth_dev->link_intr_cbs);
116283189849SRahul Lakkireddy 
116383189849SRahul Lakkireddy 		pi->eth_dev->data->mac_addrs = rte_zmalloc(name,
116483189849SRahul Lakkireddy 							   ETHER_ADDR_LEN, 0);
116583189849SRahul Lakkireddy 		if (!pi->eth_dev->data->mac_addrs) {
116683189849SRahul Lakkireddy 			dev_err(adapter, "%s: Mem allocation failed for storing mac addr, aborting\n",
116783189849SRahul Lakkireddy 				__func__);
116883189849SRahul Lakkireddy 			err = -1;
116983189849SRahul Lakkireddy 			goto out_free;
117083189849SRahul Lakkireddy 		}
117183189849SRahul Lakkireddy 	}
117283189849SRahul Lakkireddy 
117383189849SRahul Lakkireddy 	if (adapter->flags & FW_OK) {
117483189849SRahul Lakkireddy 		err = t4_port_init(adapter, adapter->mbox, adapter->pf, 0);
117583189849SRahul Lakkireddy 		if (err) {
117683189849SRahul Lakkireddy 			dev_err(adapter, "%s: t4_port_init failed with err %d\n",
117783189849SRahul Lakkireddy 				__func__, err);
117883189849SRahul Lakkireddy 			goto out_free;
117983189849SRahul Lakkireddy 		}
118083189849SRahul Lakkireddy 	}
118183189849SRahul Lakkireddy 
118292c8a632SRahul Lakkireddy 	cfg_queues(adapter->eth_dev);
118392c8a632SRahul Lakkireddy 
118483189849SRahul Lakkireddy 	print_port_info(adapter);
118583189849SRahul Lakkireddy 
118692c8a632SRahul Lakkireddy 	err = init_rss(adapter);
118792c8a632SRahul Lakkireddy 	if (err)
118892c8a632SRahul Lakkireddy 		goto out_free;
118992c8a632SRahul Lakkireddy 
119083189849SRahul Lakkireddy 	return 0;
119183189849SRahul Lakkireddy 
119283189849SRahul Lakkireddy out_free:
119383189849SRahul Lakkireddy 	for_each_port(adapter, i) {
119483189849SRahul Lakkireddy 		pi = adap2pinfo(adapter, i);
119583189849SRahul Lakkireddy 		if (pi->viid != 0)
119683189849SRahul Lakkireddy 			t4_free_vi(adapter, adapter->mbox, adapter->pf,
119783189849SRahul Lakkireddy 				   0, pi->viid);
119883189849SRahul Lakkireddy 		/* Skip first port since it'll be de-allocated by DPDK */
119983189849SRahul Lakkireddy 		if (i == 0)
120083189849SRahul Lakkireddy 			continue;
120183189849SRahul Lakkireddy 		if (pi->eth_dev->data)
120283189849SRahul Lakkireddy 			rte_free(pi->eth_dev->data);
120383189849SRahul Lakkireddy 	}
120483189849SRahul Lakkireddy 
120583189849SRahul Lakkireddy 	if (adapter->flags & FW_OK)
120683189849SRahul Lakkireddy 		t4_fw_bye(adapter, adapter->mbox);
120783189849SRahul Lakkireddy 	return -err;
120883189849SRahul Lakkireddy }
1209