xref: /dpdk/drivers/net/cxgbe/cxgbe_main.c (revision 04868e5bfddda2021df2fb191c1b9b2ee09d71ce)
183189849SRahul Lakkireddy /*-
283189849SRahul Lakkireddy  *   BSD LICENSE
383189849SRahul Lakkireddy  *
4*04868e5bSRahul 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 {
20992c8a632SRahul Lakkireddy 	return ((lc->supported & FW_PORT_CAP_SPEED_10G) != 0 ||
21092c8a632SRahul Lakkireddy 		(lc->supported & FW_PORT_CAP_SPEED_40G) != 0 ||
21192c8a632SRahul Lakkireddy 		(lc->supported & FW_PORT_CAP_SPEED_100G) != 0);
21292c8a632SRahul Lakkireddy }
21392c8a632SRahul Lakkireddy 
21492c8a632SRahul Lakkireddy inline void init_rspq(struct adapter *adap, struct sge_rspq *q,
21592c8a632SRahul Lakkireddy 		      unsigned int us, unsigned int cnt,
21692c8a632SRahul Lakkireddy 		      unsigned int size, unsigned int iqe_size)
21792c8a632SRahul Lakkireddy {
21892c8a632SRahul Lakkireddy 	q->adapter = adap;
21992c8a632SRahul Lakkireddy 	cxgb4_set_rspq_intr_params(q, us, cnt);
22092c8a632SRahul Lakkireddy 	q->iqe_len = iqe_size;
22192c8a632SRahul Lakkireddy 	q->size = size;
22292c8a632SRahul Lakkireddy }
22392c8a632SRahul Lakkireddy 
22492c8a632SRahul Lakkireddy int cfg_queue_count(struct rte_eth_dev *eth_dev)
22592c8a632SRahul Lakkireddy {
22692c8a632SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
22792c8a632SRahul Lakkireddy 	struct adapter *adap = pi->adapter;
22892c8a632SRahul Lakkireddy 	struct sge *s = &adap->sge;
22992c8a632SRahul Lakkireddy 	unsigned int max_queues = s->max_ethqsets / adap->params.nports;
23092c8a632SRahul Lakkireddy 
23192c8a632SRahul Lakkireddy 	if ((eth_dev->data->nb_rx_queues < 1) ||
23292c8a632SRahul Lakkireddy 	    (eth_dev->data->nb_tx_queues < 1))
23392c8a632SRahul Lakkireddy 		return -EINVAL;
23492c8a632SRahul Lakkireddy 
23592c8a632SRahul Lakkireddy 	if ((eth_dev->data->nb_rx_queues > max_queues) ||
23692c8a632SRahul Lakkireddy 	    (eth_dev->data->nb_tx_queues > max_queues))
23792c8a632SRahul Lakkireddy 		return -EINVAL;
23892c8a632SRahul Lakkireddy 
23992c8a632SRahul Lakkireddy 	if (eth_dev->data->nb_rx_queues > pi->rss_size)
24092c8a632SRahul Lakkireddy 		return -EINVAL;
24192c8a632SRahul Lakkireddy 
24292c8a632SRahul Lakkireddy 	/* We must configure RSS, since config has changed*/
24392c8a632SRahul Lakkireddy 	pi->flags &= ~PORT_RSS_DONE;
24492c8a632SRahul Lakkireddy 
24592c8a632SRahul Lakkireddy 	pi->n_rx_qsets = eth_dev->data->nb_rx_queues;
24692c8a632SRahul Lakkireddy 	pi->n_tx_qsets = eth_dev->data->nb_tx_queues;
24792c8a632SRahul Lakkireddy 
24892c8a632SRahul Lakkireddy 	return 0;
24992c8a632SRahul Lakkireddy }
25092c8a632SRahul Lakkireddy 
25192c8a632SRahul Lakkireddy void cfg_queues(struct rte_eth_dev *eth_dev)
25292c8a632SRahul Lakkireddy {
25392c8a632SRahul Lakkireddy 	struct rte_config *config = rte_eal_get_configuration();
25492c8a632SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
25592c8a632SRahul Lakkireddy 	struct adapter *adap = pi->adapter;
25692c8a632SRahul Lakkireddy 	struct sge *s = &adap->sge;
25792c8a632SRahul Lakkireddy 	unsigned int i, nb_ports = 0, qidx = 0;
25892c8a632SRahul Lakkireddy 	unsigned int q_per_port = 0;
25992c8a632SRahul Lakkireddy 
26092c8a632SRahul Lakkireddy 	if (!(adap->flags & CFG_QUEUES)) {
26192c8a632SRahul Lakkireddy 		for_each_port(adap, i) {
26292c8a632SRahul Lakkireddy 			struct port_info *tpi = adap2pinfo(adap, i);
26392c8a632SRahul Lakkireddy 
26492c8a632SRahul Lakkireddy 			nb_ports += (is_x_10g_port(&tpi->link_cfg)) ||
26592c8a632SRahul Lakkireddy 				     is_x_1g_port(&tpi->link_cfg) ? 1 : 0;
26692c8a632SRahul Lakkireddy 		}
26792c8a632SRahul Lakkireddy 
26892c8a632SRahul Lakkireddy 		/*
26992c8a632SRahul Lakkireddy 		 * We default up to # of cores queues per 1G/10G port.
27092c8a632SRahul Lakkireddy 		 */
27192c8a632SRahul Lakkireddy 		if (nb_ports)
27292c8a632SRahul Lakkireddy 			q_per_port = (MAX_ETH_QSETS -
27392c8a632SRahul Lakkireddy 				     (adap->params.nports - nb_ports)) /
27492c8a632SRahul Lakkireddy 				     nb_ports;
27592c8a632SRahul Lakkireddy 
27692c8a632SRahul Lakkireddy 		if (q_per_port > config->lcore_count)
27792c8a632SRahul Lakkireddy 			q_per_port = config->lcore_count;
27892c8a632SRahul Lakkireddy 
27992c8a632SRahul Lakkireddy 		for_each_port(adap, i) {
28092c8a632SRahul Lakkireddy 			struct port_info *pi = adap2pinfo(adap, i);
28192c8a632SRahul Lakkireddy 
28292c8a632SRahul Lakkireddy 			pi->first_qset = qidx;
28392c8a632SRahul Lakkireddy 
28492c8a632SRahul Lakkireddy 			/* Initially n_rx_qsets == n_tx_qsets */
28592c8a632SRahul Lakkireddy 			pi->n_rx_qsets = (is_x_10g_port(&pi->link_cfg) ||
28692c8a632SRahul Lakkireddy 					  is_x_1g_port(&pi->link_cfg)) ?
28792c8a632SRahul Lakkireddy 					  q_per_port : 1;
28892c8a632SRahul Lakkireddy 			pi->n_tx_qsets = pi->n_rx_qsets;
28992c8a632SRahul Lakkireddy 
29092c8a632SRahul Lakkireddy 			if (pi->n_rx_qsets > pi->rss_size)
29192c8a632SRahul Lakkireddy 				pi->n_rx_qsets = pi->rss_size;
29292c8a632SRahul Lakkireddy 
29392c8a632SRahul Lakkireddy 			qidx += pi->n_rx_qsets;
29492c8a632SRahul Lakkireddy 		}
29592c8a632SRahul Lakkireddy 
29692c8a632SRahul Lakkireddy 		s->max_ethqsets = qidx;
29792c8a632SRahul Lakkireddy 
29892c8a632SRahul Lakkireddy 		for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
29992c8a632SRahul Lakkireddy 			struct sge_eth_rxq *r = &s->ethrxq[i];
30092c8a632SRahul Lakkireddy 
30192c8a632SRahul Lakkireddy 			init_rspq(adap, &r->rspq, 0, 0, 1024, 64);
30292c8a632SRahul Lakkireddy 			r->usembufs = 1;
30392c8a632SRahul Lakkireddy 			r->fl.size = (r->usembufs ? 1024 : 72);
30492c8a632SRahul Lakkireddy 		}
30592c8a632SRahul Lakkireddy 
30692c8a632SRahul Lakkireddy 		for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
30792c8a632SRahul Lakkireddy 			s->ethtxq[i].q.size = 1024;
30892c8a632SRahul Lakkireddy 
30992c8a632SRahul Lakkireddy 		init_rspq(adap, &adap->sge.fw_evtq, 0, 0, 1024, 64);
31092c8a632SRahul Lakkireddy 		adap->flags |= CFG_QUEUES;
31192c8a632SRahul Lakkireddy 	}
31292c8a632SRahul Lakkireddy }
31392c8a632SRahul Lakkireddy 
314856505d3SRahul Lakkireddy void cxgbe_stats_get(struct port_info *pi, struct port_stats *stats)
315856505d3SRahul Lakkireddy {
316856505d3SRahul Lakkireddy 	t4_get_port_stats_offset(pi->adapter, pi->tx_chan, stats,
317856505d3SRahul Lakkireddy 				 &pi->stats_base);
318856505d3SRahul Lakkireddy }
319856505d3SRahul Lakkireddy 
320856505d3SRahul Lakkireddy void cxgbe_stats_reset(struct port_info *pi)
321856505d3SRahul Lakkireddy {
322856505d3SRahul Lakkireddy 	t4_clr_port_stats(pi->adapter, pi->tx_chan);
323856505d3SRahul Lakkireddy }
324856505d3SRahul Lakkireddy 
32583189849SRahul Lakkireddy static void setup_memwin(struct adapter *adap)
32683189849SRahul Lakkireddy {
32783189849SRahul Lakkireddy 	u32 mem_win0_base;
32883189849SRahul Lakkireddy 
32983189849SRahul Lakkireddy 	/* For T5, only relative offset inside the PCIe BAR is passed */
33083189849SRahul Lakkireddy 	mem_win0_base = MEMWIN0_BASE;
33183189849SRahul Lakkireddy 
33283189849SRahul Lakkireddy 	/*
33383189849SRahul Lakkireddy 	 * Set up memory window for accessing adapter memory ranges.  (Read
33483189849SRahul Lakkireddy 	 * back MA register to ensure that changes propagate before we attempt
33583189849SRahul Lakkireddy 	 * to use the new values.)
33683189849SRahul Lakkireddy 	 */
33783189849SRahul Lakkireddy 	t4_write_reg(adap,
33883189849SRahul Lakkireddy 		     PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN,
33983189849SRahul Lakkireddy 					 MEMWIN_NIC),
34083189849SRahul Lakkireddy 		     mem_win0_base | V_BIR(0) |
34183189849SRahul Lakkireddy 		     V_WINDOW(ilog2(MEMWIN0_APERTURE) - X_WINDOW_SHIFT));
34283189849SRahul Lakkireddy 	t4_read_reg(adap,
34383189849SRahul Lakkireddy 		    PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN,
34483189849SRahul Lakkireddy 					MEMWIN_NIC));
34583189849SRahul Lakkireddy }
34683189849SRahul Lakkireddy 
34792c8a632SRahul Lakkireddy static int init_rss(struct adapter *adap)
34892c8a632SRahul Lakkireddy {
34992c8a632SRahul Lakkireddy 	unsigned int i;
35092c8a632SRahul Lakkireddy 	int err;
35192c8a632SRahul Lakkireddy 
35292c8a632SRahul Lakkireddy 	err = t4_init_rss_mode(adap, adap->mbox);
35392c8a632SRahul Lakkireddy 	if (err)
35492c8a632SRahul Lakkireddy 		return err;
35592c8a632SRahul Lakkireddy 
35692c8a632SRahul Lakkireddy 	for_each_port(adap, i) {
35792c8a632SRahul Lakkireddy 		struct port_info *pi = adap2pinfo(adap, i);
35892c8a632SRahul Lakkireddy 
3598dca8cc5SRahul Lakkireddy 		pi->rss = rte_zmalloc(NULL, pi->rss_size * sizeof(u16), 0);
36092c8a632SRahul Lakkireddy 		if (!pi->rss)
36192c8a632SRahul Lakkireddy 			return -ENOMEM;
36292c8a632SRahul Lakkireddy 	}
36392c8a632SRahul Lakkireddy 	return 0;
36492c8a632SRahul Lakkireddy }
36592c8a632SRahul Lakkireddy 
36683189849SRahul Lakkireddy static void print_port_info(struct adapter *adap)
36783189849SRahul Lakkireddy {
36883189849SRahul Lakkireddy 	int i;
36983189849SRahul Lakkireddy 	char buf[80];
37083189849SRahul Lakkireddy 	struct rte_pci_addr *loc = &adap->pdev->addr;
37183189849SRahul Lakkireddy 
37283189849SRahul Lakkireddy 	for_each_port(adap, i) {
37383189849SRahul Lakkireddy 		const struct port_info *pi = &adap->port[i];
37483189849SRahul Lakkireddy 		char *bufp = buf;
37583189849SRahul Lakkireddy 
37683189849SRahul Lakkireddy 		if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_100M)
37783189849SRahul Lakkireddy 			bufp += sprintf(bufp, "100/");
37883189849SRahul Lakkireddy 		if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_1G)
37983189849SRahul Lakkireddy 			bufp += sprintf(bufp, "1000/");
38083189849SRahul Lakkireddy 		if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_10G)
38183189849SRahul Lakkireddy 			bufp += sprintf(bufp, "10G/");
38283189849SRahul Lakkireddy 		if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_40G)
38383189849SRahul Lakkireddy 			bufp += sprintf(bufp, "40G/");
38483189849SRahul Lakkireddy 		if (bufp != buf)
38583189849SRahul Lakkireddy 			--bufp;
38683189849SRahul Lakkireddy 		sprintf(bufp, "BASE-%s",
387efa8a43eSBruce Richardson 			t4_get_port_type_description(
388efa8a43eSBruce Richardson 					(enum fw_port_type)pi->port_type));
38983189849SRahul Lakkireddy 
39083189849SRahul Lakkireddy 		dev_info(adap,
39183189849SRahul Lakkireddy 			 " " PCI_PRI_FMT " Chelsio rev %d %s %s\n",
39283189849SRahul Lakkireddy 			 loc->domain, loc->bus, loc->devid, loc->function,
39383189849SRahul Lakkireddy 			 CHELSIO_CHIP_RELEASE(adap->params.chip), buf,
39483189849SRahul Lakkireddy 			 (adap->flags & USING_MSIX) ? " MSI-X" :
39583189849SRahul Lakkireddy 			 (adap->flags & USING_MSI) ? " MSI" : "");
39683189849SRahul Lakkireddy 	}
39783189849SRahul Lakkireddy }
39883189849SRahul Lakkireddy 
39983189849SRahul Lakkireddy /*
40083189849SRahul Lakkireddy  * Tweak configuration based on system architecture, etc.  Most of these have
40183189849SRahul Lakkireddy  * defaults assigned to them by Firmware Configuration Files (if we're using
40283189849SRahul Lakkireddy  * them) but need to be explicitly set if we're using hard-coded
40383189849SRahul Lakkireddy  * initialization. So these are essentially common tweaks/settings for
40483189849SRahul Lakkireddy  * Configuration Files and hard-coded initialization ...
40583189849SRahul Lakkireddy  */
40683189849SRahul Lakkireddy static int adap_init0_tweaks(struct adapter *adapter)
40783189849SRahul Lakkireddy {
40883189849SRahul Lakkireddy 	u8 rx_dma_offset;
40983189849SRahul Lakkireddy 
41083189849SRahul Lakkireddy 	/*
41183189849SRahul Lakkireddy 	 * Fix up various Host-Dependent Parameters like Page Size, Cache
41283189849SRahul Lakkireddy 	 * Line Size, etc.  The firmware default is for a 4KB Page Size and
41383189849SRahul Lakkireddy 	 * 64B Cache Line Size ...
41483189849SRahul Lakkireddy 	 */
4151f8613f1SRahul Lakkireddy 	t4_fixup_host_params_compat(adapter, CXGBE_PAGE_SIZE, L1_CACHE_BYTES,
41683189849SRahul Lakkireddy 				    T5_LAST_REV);
41783189849SRahul Lakkireddy 
41883189849SRahul Lakkireddy 	/*
41983189849SRahul Lakkireddy 	 * Keep the chip default offset to deliver Ingress packets into our
42083189849SRahul Lakkireddy 	 * DMA buffers to zero
42183189849SRahul Lakkireddy 	 */
42283189849SRahul Lakkireddy 	rx_dma_offset = 0;
42383189849SRahul Lakkireddy 	t4_set_reg_field(adapter, A_SGE_CONTROL, V_PKTSHIFT(M_PKTSHIFT),
42483189849SRahul Lakkireddy 			 V_PKTSHIFT(rx_dma_offset));
42583189849SRahul Lakkireddy 
426bf89cbedSRahul Lakkireddy 	t4_set_reg_field(adapter, A_SGE_FLM_CFG,
427bf89cbedSRahul Lakkireddy 			 V_CREDITCNT(M_CREDITCNT) | M_CREDITCNTPACKING,
428bf89cbedSRahul Lakkireddy 			 V_CREDITCNT(3) | V_CREDITCNTPACKING(1));
429bf89cbedSRahul Lakkireddy 
430bf89cbedSRahul Lakkireddy 	t4_set_reg_field(adapter, A_SGE_CONTROL2, V_IDMAARBROUNDROBIN(1U),
431bf89cbedSRahul Lakkireddy 			 V_IDMAARBROUNDROBIN(1U));
432bf89cbedSRahul Lakkireddy 
43383189849SRahul Lakkireddy 	/*
43483189849SRahul Lakkireddy 	 * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
43583189849SRahul Lakkireddy 	 * adds the pseudo header itself.
43683189849SRahul Lakkireddy 	 */
43783189849SRahul Lakkireddy 	t4_tp_wr_bits_indirect(adapter, A_TP_INGRESS_CONFIG,
43883189849SRahul Lakkireddy 			       F_CSUM_HAS_PSEUDO_HDR, 0);
43983189849SRahul Lakkireddy 
44083189849SRahul Lakkireddy 	return 0;
44183189849SRahul Lakkireddy }
44283189849SRahul Lakkireddy 
44383189849SRahul Lakkireddy /*
44483189849SRahul Lakkireddy  * Attempt to initialize the adapter via a Firmware Configuration File.
44583189849SRahul Lakkireddy  */
44683189849SRahul Lakkireddy static int adap_init0_config(struct adapter *adapter, int reset)
44783189849SRahul Lakkireddy {
44883189849SRahul Lakkireddy 	struct fw_caps_config_cmd caps_cmd;
44983189849SRahul Lakkireddy 	unsigned long mtype = 0, maddr = 0;
45083189849SRahul Lakkireddy 	u32 finiver, finicsum, cfcsum;
45183189849SRahul Lakkireddy 	int ret;
45283189849SRahul Lakkireddy 	int config_issued = 0;
45383189849SRahul Lakkireddy 	int cfg_addr;
45483189849SRahul Lakkireddy 	char config_name[20];
45583189849SRahul Lakkireddy 
45683189849SRahul Lakkireddy 	/*
45783189849SRahul Lakkireddy 	 * Reset device if necessary.
45883189849SRahul Lakkireddy 	 */
45983189849SRahul Lakkireddy 	if (reset) {
46083189849SRahul Lakkireddy 		ret = t4_fw_reset(adapter, adapter->mbox,
46183189849SRahul Lakkireddy 				  F_PIORSTMODE | F_PIORST);
46283189849SRahul Lakkireddy 		if (ret < 0) {
46383189849SRahul Lakkireddy 			dev_warn(adapter, "Firmware reset failed, error %d\n",
46483189849SRahul Lakkireddy 				 -ret);
46583189849SRahul Lakkireddy 			goto bye;
46683189849SRahul Lakkireddy 		}
46783189849SRahul Lakkireddy 	}
46883189849SRahul Lakkireddy 
46983189849SRahul Lakkireddy 	cfg_addr = t4_flash_cfg_addr(adapter);
47083189849SRahul Lakkireddy 	if (cfg_addr < 0) {
47183189849SRahul Lakkireddy 		ret = cfg_addr;
47283189849SRahul Lakkireddy 		dev_warn(adapter, "Finding address for firmware config file in flash failed, error %d\n",
47383189849SRahul Lakkireddy 			 -ret);
47483189849SRahul Lakkireddy 		goto bye;
47583189849SRahul Lakkireddy 	}
47683189849SRahul Lakkireddy 
47783189849SRahul Lakkireddy 	strcpy(config_name, "On Flash");
47883189849SRahul Lakkireddy 	mtype = FW_MEMTYPE_CF_FLASH;
47983189849SRahul Lakkireddy 	maddr = cfg_addr;
48083189849SRahul Lakkireddy 
48183189849SRahul Lakkireddy 	/*
48283189849SRahul Lakkireddy 	 * Issue a Capability Configuration command to the firmware to get it
48383189849SRahul Lakkireddy 	 * to parse the Configuration File.  We don't use t4_fw_config_file()
48483189849SRahul Lakkireddy 	 * because we want the ability to modify various features after we've
48583189849SRahul Lakkireddy 	 * processed the configuration file ...
48683189849SRahul Lakkireddy 	 */
48783189849SRahul Lakkireddy 	memset(&caps_cmd, 0, sizeof(caps_cmd));
48883189849SRahul Lakkireddy 	caps_cmd.op_to_write = cpu_to_be32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
48983189849SRahul Lakkireddy 					   F_FW_CMD_REQUEST | F_FW_CMD_READ);
49083189849SRahul Lakkireddy 	caps_cmd.cfvalid_to_len16 =
49183189849SRahul Lakkireddy 		cpu_to_be32(F_FW_CAPS_CONFIG_CMD_CFVALID |
49283189849SRahul Lakkireddy 			    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
49383189849SRahul Lakkireddy 			    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(maddr >> 16) |
49483189849SRahul Lakkireddy 			    FW_LEN16(caps_cmd));
49583189849SRahul Lakkireddy 	ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
49683189849SRahul Lakkireddy 			 &caps_cmd);
49783189849SRahul Lakkireddy 	/*
49883189849SRahul Lakkireddy 	 * If the CAPS_CONFIG failed with an ENOENT (for a Firmware
49983189849SRahul Lakkireddy 	 * Configuration File in FLASH), our last gasp effort is to use the
50083189849SRahul Lakkireddy 	 * Firmware Configuration File which is embedded in the firmware.  A
50183189849SRahul Lakkireddy 	 * very few early versions of the firmware didn't have one embedded
50283189849SRahul Lakkireddy 	 * but we can ignore those.
50383189849SRahul Lakkireddy 	 */
50483189849SRahul Lakkireddy 	if (ret == -ENOENT) {
50583189849SRahul Lakkireddy 		dev_info(adapter, "%s: Going for embedded config in firmware..\n",
50683189849SRahul Lakkireddy 			 __func__);
50783189849SRahul Lakkireddy 
50883189849SRahul Lakkireddy 		memset(&caps_cmd, 0, sizeof(caps_cmd));
50983189849SRahul Lakkireddy 		caps_cmd.op_to_write =
51083189849SRahul Lakkireddy 			cpu_to_be32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
51183189849SRahul Lakkireddy 				    F_FW_CMD_REQUEST | F_FW_CMD_READ);
51283189849SRahul Lakkireddy 		caps_cmd.cfvalid_to_len16 = cpu_to_be32(FW_LEN16(caps_cmd));
51383189849SRahul Lakkireddy 		ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd,
51483189849SRahul Lakkireddy 				 sizeof(caps_cmd), &caps_cmd);
51583189849SRahul Lakkireddy 		strcpy(config_name, "Firmware Default");
51683189849SRahul Lakkireddy 	}
51783189849SRahul Lakkireddy 
51883189849SRahul Lakkireddy 	config_issued = 1;
51983189849SRahul Lakkireddy 	if (ret < 0)
52083189849SRahul Lakkireddy 		goto bye;
52183189849SRahul Lakkireddy 
52283189849SRahul Lakkireddy 	finiver = be32_to_cpu(caps_cmd.finiver);
52383189849SRahul Lakkireddy 	finicsum = be32_to_cpu(caps_cmd.finicsum);
52483189849SRahul Lakkireddy 	cfcsum = be32_to_cpu(caps_cmd.cfcsum);
52583189849SRahul Lakkireddy 	if (finicsum != cfcsum)
52683189849SRahul Lakkireddy 		dev_warn(adapter, "Configuration File checksum mismatch: [fini] csum=%#x, computed csum=%#x\n",
52783189849SRahul Lakkireddy 			 finicsum, cfcsum);
52883189849SRahul Lakkireddy 
52983189849SRahul Lakkireddy 	/*
53083189849SRahul Lakkireddy 	 * If we're a pure NIC driver then disable all offloading facilities.
53183189849SRahul Lakkireddy 	 * This will allow the firmware to optimize aspects of the hardware
53283189849SRahul Lakkireddy 	 * configuration which will result in improved performance.
53383189849SRahul Lakkireddy 	 */
53483189849SRahul Lakkireddy 	caps_cmd.niccaps &= cpu_to_be16(~(FW_CAPS_CONFIG_NIC_HASHFILTER |
53583189849SRahul Lakkireddy 					  FW_CAPS_CONFIG_NIC_ETHOFLD));
53683189849SRahul Lakkireddy 	caps_cmd.toecaps = 0;
53783189849SRahul Lakkireddy 	caps_cmd.iscsicaps = 0;
53883189849SRahul Lakkireddy 	caps_cmd.rdmacaps = 0;
53983189849SRahul Lakkireddy 	caps_cmd.fcoecaps = 0;
54083189849SRahul Lakkireddy 
54183189849SRahul Lakkireddy 	/*
54283189849SRahul Lakkireddy 	 * And now tell the firmware to use the configuration we just loaded.
54383189849SRahul Lakkireddy 	 */
54483189849SRahul Lakkireddy 	caps_cmd.op_to_write = cpu_to_be32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
54583189849SRahul Lakkireddy 					   F_FW_CMD_REQUEST | F_FW_CMD_WRITE);
54683189849SRahul Lakkireddy 	caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
54783189849SRahul Lakkireddy 	ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
54883189849SRahul Lakkireddy 			 NULL);
54983189849SRahul Lakkireddy 	if (ret < 0) {
55083189849SRahul Lakkireddy 		dev_warn(adapter, "Unable to finalize Firmware Capabilities %d\n",
55183189849SRahul Lakkireddy 			 -ret);
55283189849SRahul Lakkireddy 		goto bye;
55383189849SRahul Lakkireddy 	}
55483189849SRahul Lakkireddy 
55583189849SRahul Lakkireddy 	/*
55683189849SRahul Lakkireddy 	 * Tweak configuration based on system architecture, etc.
55783189849SRahul Lakkireddy 	 */
55883189849SRahul Lakkireddy 	ret = adap_init0_tweaks(adapter);
55983189849SRahul Lakkireddy 	if (ret < 0) {
56083189849SRahul Lakkireddy 		dev_warn(adapter, "Unable to do init0-tweaks %d\n", -ret);
56183189849SRahul Lakkireddy 		goto bye;
56283189849SRahul Lakkireddy 	}
56383189849SRahul Lakkireddy 
56483189849SRahul Lakkireddy 	/*
56583189849SRahul Lakkireddy 	 * And finally tell the firmware to initialize itself using the
56683189849SRahul Lakkireddy 	 * parameters from the Configuration File.
56783189849SRahul Lakkireddy 	 */
56883189849SRahul Lakkireddy 	ret = t4_fw_initialize(adapter, adapter->mbox);
56983189849SRahul Lakkireddy 	if (ret < 0) {
57083189849SRahul Lakkireddy 		dev_warn(adapter, "Initializing Firmware failed, error %d\n",
57183189849SRahul Lakkireddy 			 -ret);
57283189849SRahul Lakkireddy 		goto bye;
57383189849SRahul Lakkireddy 	}
57483189849SRahul Lakkireddy 
57583189849SRahul Lakkireddy 	/*
57683189849SRahul Lakkireddy 	 * Return successfully and note that we're operating with parameters
57783189849SRahul Lakkireddy 	 * not supplied by the driver, rather than from hard-wired
57883189849SRahul Lakkireddy 	 * initialization constants burried in the driver.
57983189849SRahul Lakkireddy 	 */
58083189849SRahul Lakkireddy 	dev_info(adapter,
58183189849SRahul Lakkireddy 		 "Successfully configured using Firmware Configuration File \"%s\", version %#x, computed checksum %#x\n",
58283189849SRahul Lakkireddy 		 config_name, finiver, cfcsum);
58383189849SRahul Lakkireddy 
58483189849SRahul Lakkireddy 	return 0;
58583189849SRahul Lakkireddy 
58683189849SRahul Lakkireddy 	/*
58783189849SRahul Lakkireddy 	 * Something bad happened.  Return the error ...  (If the "error"
58883189849SRahul Lakkireddy 	 * is that there's no Configuration File on the adapter we don't
58983189849SRahul Lakkireddy 	 * want to issue a warning since this is fairly common.)
59083189849SRahul Lakkireddy 	 */
59183189849SRahul Lakkireddy bye:
59283189849SRahul Lakkireddy 	if (config_issued && ret != -ENOENT)
59383189849SRahul Lakkireddy 		dev_warn(adapter, "\"%s\" configuration file error %d\n",
59483189849SRahul Lakkireddy 			 config_name, -ret);
59583189849SRahul Lakkireddy 
59683189849SRahul Lakkireddy 	dev_debug(adapter, "%s: returning ret = %d ..\n", __func__, ret);
59783189849SRahul Lakkireddy 	return ret;
59883189849SRahul Lakkireddy }
59983189849SRahul Lakkireddy 
60083189849SRahul Lakkireddy static int adap_init0(struct adapter *adap)
60183189849SRahul Lakkireddy {
60283189849SRahul Lakkireddy 	int ret = 0;
60383189849SRahul Lakkireddy 	u32 v, port_vec;
60483189849SRahul Lakkireddy 	enum dev_state state;
60583189849SRahul Lakkireddy 	u32 params[7], val[7];
60683189849SRahul Lakkireddy 	int reset = 1;
60783189849SRahul Lakkireddy 	int mbox = adap->mbox;
60883189849SRahul Lakkireddy 
60983189849SRahul Lakkireddy 	/*
61083189849SRahul Lakkireddy 	 * Contact FW, advertising Master capability.
61183189849SRahul Lakkireddy 	 */
61283189849SRahul Lakkireddy 	ret = t4_fw_hello(adap, adap->mbox, adap->mbox, MASTER_MAY, &state);
61383189849SRahul Lakkireddy 	if (ret < 0) {
61483189849SRahul Lakkireddy 		dev_err(adap, "%s: could not connect to FW, error %d\n",
61583189849SRahul Lakkireddy 			__func__, -ret);
61683189849SRahul Lakkireddy 		goto bye;
61783189849SRahul Lakkireddy 	}
61883189849SRahul Lakkireddy 
61983189849SRahul Lakkireddy 	CXGBE_DEBUG_MBOX(adap, "%s: adap->mbox = %d; ret = %d\n", __func__,
62083189849SRahul Lakkireddy 			 adap->mbox, ret);
62183189849SRahul Lakkireddy 
62283189849SRahul Lakkireddy 	if (ret == mbox)
62383189849SRahul Lakkireddy 		adap->flags |= MASTER_PF;
62483189849SRahul Lakkireddy 
62583189849SRahul Lakkireddy 	if (state == DEV_STATE_INIT) {
62683189849SRahul Lakkireddy 		/*
62783189849SRahul Lakkireddy 		 * Force halt and reset FW because a previous instance may have
62883189849SRahul Lakkireddy 		 * exited abnormally without properly shutting down
62983189849SRahul Lakkireddy 		 */
63083189849SRahul Lakkireddy 		ret = t4_fw_halt(adap, adap->mbox, reset);
63183189849SRahul Lakkireddy 		if (ret < 0) {
63283189849SRahul Lakkireddy 			dev_err(adap, "Failed to halt. Exit.\n");
63383189849SRahul Lakkireddy 			goto bye;
63483189849SRahul Lakkireddy 		}
63583189849SRahul Lakkireddy 
63683189849SRahul Lakkireddy 		ret = t4_fw_restart(adap, adap->mbox, reset);
63783189849SRahul Lakkireddy 		if (ret < 0) {
63883189849SRahul Lakkireddy 			dev_err(adap, "Failed to restart. Exit.\n");
63983189849SRahul Lakkireddy 			goto bye;
64083189849SRahul Lakkireddy 		}
641efa8a43eSBruce Richardson 		state = (enum dev_state)((unsigned)state & ~DEV_STATE_INIT);
64283189849SRahul Lakkireddy 	}
64383189849SRahul Lakkireddy 
64483189849SRahul Lakkireddy 	t4_get_fw_version(adap, &adap->params.fw_vers);
64583189849SRahul Lakkireddy 	t4_get_tp_version(adap, &adap->params.tp_vers);
64683189849SRahul Lakkireddy 
64783189849SRahul Lakkireddy 	dev_info(adap, "fw: %u.%u.%u.%u, TP: %u.%u.%u.%u\n",
64883189849SRahul Lakkireddy 		 G_FW_HDR_FW_VER_MAJOR(adap->params.fw_vers),
64983189849SRahul Lakkireddy 		 G_FW_HDR_FW_VER_MINOR(adap->params.fw_vers),
65083189849SRahul Lakkireddy 		 G_FW_HDR_FW_VER_MICRO(adap->params.fw_vers),
65183189849SRahul Lakkireddy 		 G_FW_HDR_FW_VER_BUILD(adap->params.fw_vers),
65283189849SRahul Lakkireddy 		 G_FW_HDR_FW_VER_MAJOR(adap->params.tp_vers),
65383189849SRahul Lakkireddy 		 G_FW_HDR_FW_VER_MINOR(adap->params.tp_vers),
65483189849SRahul Lakkireddy 		 G_FW_HDR_FW_VER_MICRO(adap->params.tp_vers),
65583189849SRahul Lakkireddy 		 G_FW_HDR_FW_VER_BUILD(adap->params.tp_vers));
65683189849SRahul Lakkireddy 
65783189849SRahul Lakkireddy 	ret = t4_get_core_clock(adap, &adap->params.vpd);
65883189849SRahul Lakkireddy 	if (ret < 0) {
65983189849SRahul Lakkireddy 		dev_err(adap, "%s: could not get core clock, error %d\n",
66083189849SRahul Lakkireddy 			__func__, -ret);
66183189849SRahul Lakkireddy 		goto bye;
66283189849SRahul Lakkireddy 	}
66383189849SRahul Lakkireddy 
66483189849SRahul Lakkireddy 	/*
66583189849SRahul Lakkireddy 	 * Find out what ports are available to us.  Note that we need to do
66683189849SRahul Lakkireddy 	 * this before calling adap_init0_no_config() since it needs nports
66783189849SRahul Lakkireddy 	 * and portvec ...
66883189849SRahul Lakkireddy 	 */
66983189849SRahul Lakkireddy 	v = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
67083189849SRahul Lakkireddy 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_PORTVEC);
67183189849SRahul Lakkireddy 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, &v, &port_vec);
67283189849SRahul Lakkireddy 	if (ret < 0) {
67383189849SRahul Lakkireddy 		dev_err(adap, "%s: failure in t4_queury_params; error = %d\n",
67483189849SRahul Lakkireddy 			__func__, ret);
67583189849SRahul Lakkireddy 		goto bye;
67683189849SRahul Lakkireddy 	}
67783189849SRahul Lakkireddy 
67883189849SRahul Lakkireddy 	adap->params.nports = hweight32(port_vec);
67983189849SRahul Lakkireddy 	adap->params.portvec = port_vec;
68083189849SRahul Lakkireddy 
68183189849SRahul Lakkireddy 	dev_debug(adap, "%s: adap->params.nports = %u\n", __func__,
68283189849SRahul Lakkireddy 		  adap->params.nports);
68383189849SRahul Lakkireddy 
68483189849SRahul Lakkireddy 	/*
68583189849SRahul Lakkireddy 	 * If the firmware is initialized already (and we're not forcing a
68683189849SRahul Lakkireddy 	 * master initialization), note that we're living with existing
68783189849SRahul Lakkireddy 	 * adapter parameters.  Otherwise, it's time to try initializing the
68883189849SRahul Lakkireddy 	 * adapter ...
68983189849SRahul Lakkireddy 	 */
69083189849SRahul Lakkireddy 	if (state == DEV_STATE_INIT) {
69183189849SRahul Lakkireddy 		dev_info(adap, "Coming up as %s: Adapter already initialized\n",
69283189849SRahul Lakkireddy 			 adap->flags & MASTER_PF ? "MASTER" : "SLAVE");
69383189849SRahul Lakkireddy 	} else {
69483189849SRahul Lakkireddy 		dev_info(adap, "Coming up as MASTER: Initializing adapter\n");
69583189849SRahul Lakkireddy 
69683189849SRahul Lakkireddy 		ret = adap_init0_config(adap, reset);
69783189849SRahul Lakkireddy 		if (ret == -ENOENT) {
69883189849SRahul Lakkireddy 			dev_err(adap,
69983189849SRahul Lakkireddy 				"No Configuration File present on adapter. Using hard-wired configuration parameters.\n");
70083189849SRahul Lakkireddy 			goto bye;
70183189849SRahul Lakkireddy 		}
70283189849SRahul Lakkireddy 	}
70383189849SRahul Lakkireddy 	if (ret < 0) {
70483189849SRahul Lakkireddy 		dev_err(adap, "could not initialize adapter, error %d\n", -ret);
70583189849SRahul Lakkireddy 		goto bye;
70683189849SRahul Lakkireddy 	}
70783189849SRahul Lakkireddy 
70883189849SRahul Lakkireddy 	/*
70983189849SRahul Lakkireddy 	 * Give the SGE code a chance to pull in anything that it needs ...
71083189849SRahul Lakkireddy 	 * Note that this must be called after we retrieve our VPD parameters
71183189849SRahul Lakkireddy 	 * in order to know how to convert core ticks to seconds, etc.
71283189849SRahul Lakkireddy 	 */
71383189849SRahul Lakkireddy 	ret = t4_sge_init(adap);
71483189849SRahul Lakkireddy 	if (ret < 0) {
71583189849SRahul Lakkireddy 		dev_err(adap, "t4_sge_init failed with error %d\n",
71683189849SRahul Lakkireddy 			-ret);
71783189849SRahul Lakkireddy 		goto bye;
71883189849SRahul Lakkireddy 	}
71983189849SRahul Lakkireddy 
72083189849SRahul Lakkireddy 	/*
72183189849SRahul Lakkireddy 	 * Grab some of our basic fundamental operating parameters.
72283189849SRahul Lakkireddy 	 */
72383189849SRahul Lakkireddy #define FW_PARAM_DEV(param) \
72483189849SRahul Lakkireddy 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) | \
72583189849SRahul Lakkireddy 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_##param))
72683189849SRahul Lakkireddy 
72783189849SRahul Lakkireddy #define FW_PARAM_PFVF(param) \
72883189849SRahul Lakkireddy 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_PFVF) | \
72983189849SRahul Lakkireddy 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_PFVF_##param) |  \
73083189849SRahul Lakkireddy 	 V_FW_PARAMS_PARAM_Y(0) | \
73183189849SRahul Lakkireddy 	 V_FW_PARAMS_PARAM_Z(0))
73283189849SRahul Lakkireddy 
73383189849SRahul Lakkireddy 	/* If we're running on newer firmware, let it know that we're
73483189849SRahul Lakkireddy 	 * prepared to deal with encapsulated CPL messages.  Older
73583189849SRahul Lakkireddy 	 * firmware won't understand this and we'll just get
73683189849SRahul Lakkireddy 	 * unencapsulated messages ...
73783189849SRahul Lakkireddy 	 */
73883189849SRahul Lakkireddy 	params[0] = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
73983189849SRahul Lakkireddy 	val[0] = 1;
74083189849SRahul Lakkireddy 	(void)t4_set_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
74183189849SRahul Lakkireddy 
74283189849SRahul Lakkireddy 	/*
74383189849SRahul Lakkireddy 	 * Find out whether we're allowed to use the T5+ ULPTX MEMWRITE DSGL
74483189849SRahul Lakkireddy 	 * capability.  Earlier versions of the firmware didn't have the
74583189849SRahul Lakkireddy 	 * ULPTX_MEMWRITE_DSGL so we'll interpret a query failure as no
74683189849SRahul Lakkireddy 	 * permission to use ULPTX MEMWRITE DSGL.
74783189849SRahul Lakkireddy 	 */
74883189849SRahul Lakkireddy 	if (is_t4(adap->params.chip)) {
74983189849SRahul Lakkireddy 		adap->params.ulptx_memwrite_dsgl = false;
75083189849SRahul Lakkireddy 	} else {
75183189849SRahul Lakkireddy 		params[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
75283189849SRahul Lakkireddy 		ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
75383189849SRahul Lakkireddy 				      1, params, val);
75483189849SRahul Lakkireddy 		adap->params.ulptx_memwrite_dsgl = (ret == 0 && val[0] != 0);
75583189849SRahul Lakkireddy 	}
75683189849SRahul Lakkireddy 
75783189849SRahul Lakkireddy 	/*
75883189849SRahul Lakkireddy 	 * The MTU/MSS Table is initialized by now, so load their values.  If
75983189849SRahul Lakkireddy 	 * we're initializing the adapter, then we'll make any modifications
76083189849SRahul Lakkireddy 	 * we want to the MTU/MSS Table and also initialize the congestion
76183189849SRahul Lakkireddy 	 * parameters.
76283189849SRahul Lakkireddy 	 */
76383189849SRahul Lakkireddy 	t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
76483189849SRahul Lakkireddy 	if (state != DEV_STATE_INIT) {
76583189849SRahul Lakkireddy 		int i;
76683189849SRahul Lakkireddy 
76783189849SRahul Lakkireddy 		/*
76883189849SRahul Lakkireddy 		 * The default MTU Table contains values 1492 and 1500.
76983189849SRahul Lakkireddy 		 * However, for TCP, it's better to have two values which are
77083189849SRahul Lakkireddy 		 * a multiple of 8 +/- 4 bytes apart near this popular MTU.
77183189849SRahul Lakkireddy 		 * This allows us to have a TCP Data Payload which is a
77283189849SRahul Lakkireddy 		 * multiple of 8 regardless of what combination of TCP Options
77383189849SRahul Lakkireddy 		 * are in use (always a multiple of 4 bytes) which is
77483189849SRahul Lakkireddy 		 * important for performance reasons.  For instance, if no
77583189849SRahul Lakkireddy 		 * options are in use, then we have a 20-byte IP header and a
77683189849SRahul Lakkireddy 		 * 20-byte TCP header.  In this case, a 1500-byte MSS would
77783189849SRahul Lakkireddy 		 * result in a TCP Data Payload of 1500 - 40 == 1460 bytes
77883189849SRahul Lakkireddy 		 * which is not a multiple of 8.  So using an MSS of 1488 in
77983189849SRahul Lakkireddy 		 * this case results in a TCP Data Payload of 1448 bytes which
78083189849SRahul Lakkireddy 		 * is a multiple of 8.  On the other hand, if 12-byte TCP Time
78183189849SRahul Lakkireddy 		 * Stamps have been negotiated, then an MTU of 1500 bytes
78283189849SRahul Lakkireddy 		 * results in a TCP Data Payload of 1448 bytes which, as
78383189849SRahul Lakkireddy 		 * above, is a multiple of 8 bytes ...
78483189849SRahul Lakkireddy 		 */
78583189849SRahul Lakkireddy 		for (i = 0; i < NMTUS; i++)
78683189849SRahul Lakkireddy 			if (adap->params.mtus[i] == 1492) {
78783189849SRahul Lakkireddy 				adap->params.mtus[i] = 1488;
78883189849SRahul Lakkireddy 				break;
78983189849SRahul Lakkireddy 			}
79083189849SRahul Lakkireddy 
79183189849SRahul Lakkireddy 		t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
79283189849SRahul Lakkireddy 			     adap->params.b_wnd);
79383189849SRahul Lakkireddy 	}
79483189849SRahul Lakkireddy 	t4_init_sge_params(adap);
79583189849SRahul Lakkireddy 	t4_init_tp_params(adap);
79683189849SRahul Lakkireddy 
79783189849SRahul Lakkireddy 	adap->params.drv_memwin = MEMWIN_NIC;
79883189849SRahul Lakkireddy 	adap->flags |= FW_OK;
79983189849SRahul Lakkireddy 	dev_debug(adap, "%s: returning zero..\n", __func__);
80083189849SRahul Lakkireddy 	return 0;
80183189849SRahul Lakkireddy 
80283189849SRahul Lakkireddy 	/*
80383189849SRahul Lakkireddy 	 * Something bad happened.  If a command timed out or failed with EIO
80483189849SRahul Lakkireddy 	 * FW does not operate within its spec or something catastrophic
80583189849SRahul Lakkireddy 	 * happened to HW/FW, stop issuing commands.
80683189849SRahul Lakkireddy 	 */
80783189849SRahul Lakkireddy bye:
80883189849SRahul Lakkireddy 	if (ret != -ETIMEDOUT && ret != -EIO)
80983189849SRahul Lakkireddy 		t4_fw_bye(adap, adap->mbox);
81083189849SRahul Lakkireddy 	return ret;
81183189849SRahul Lakkireddy }
81283189849SRahul Lakkireddy 
81383189849SRahul Lakkireddy /**
81483189849SRahul Lakkireddy  * t4_os_portmod_changed - handle port module changes
81583189849SRahul Lakkireddy  * @adap: the adapter associated with the module change
81683189849SRahul Lakkireddy  * @port_id: the port index whose module status has changed
81783189849SRahul Lakkireddy  *
81883189849SRahul Lakkireddy  * This is the OS-dependent handler for port module changes.  It is
81983189849SRahul Lakkireddy  * invoked when a port module is removed or inserted for any OS-specific
82083189849SRahul Lakkireddy  * processing.
82183189849SRahul Lakkireddy  */
82283189849SRahul Lakkireddy void t4_os_portmod_changed(const struct adapter *adap, int port_id)
82383189849SRahul Lakkireddy {
82483189849SRahul Lakkireddy 	static const char * const mod_str[] = {
82583189849SRahul Lakkireddy 		NULL, "LR", "SR", "ER", "passive DA", "active DA", "LRM"
82683189849SRahul Lakkireddy 	};
82783189849SRahul Lakkireddy 
82883189849SRahul Lakkireddy 	const struct port_info *pi = &adap->port[port_id];
82983189849SRahul Lakkireddy 
83083189849SRahul Lakkireddy 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
83183189849SRahul Lakkireddy 		dev_info(adap, "Port%d: port module unplugged\n", pi->port_id);
83283189849SRahul Lakkireddy 	else if (pi->mod_type < ARRAY_SIZE(mod_str))
83383189849SRahul Lakkireddy 		dev_info(adap, "Port%d: %s port module inserted\n", pi->port_id,
83483189849SRahul Lakkireddy 			 mod_str[pi->mod_type]);
83583189849SRahul Lakkireddy 	else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
83683189849SRahul Lakkireddy 		dev_info(adap, "Port%d: unsupported optical port module inserted\n",
83783189849SRahul Lakkireddy 			 pi->port_id);
83883189849SRahul Lakkireddy 	else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
83983189849SRahul Lakkireddy 		dev_info(adap, "Port%d: unknown port module inserted, forcing TWINAX\n",
84083189849SRahul Lakkireddy 			 pi->port_id);
84183189849SRahul Lakkireddy 	else if (pi->mod_type == FW_PORT_MOD_TYPE_ERROR)
84283189849SRahul Lakkireddy 		dev_info(adap, "Port%d: transceiver module error\n",
84383189849SRahul Lakkireddy 			 pi->port_id);
84483189849SRahul Lakkireddy 	else
84583189849SRahul Lakkireddy 		dev_info(adap, "Port%d: unknown module type %d inserted\n",
84683189849SRahul Lakkireddy 			 pi->port_id, pi->mod_type);
84783189849SRahul Lakkireddy }
84883189849SRahul Lakkireddy 
84992c8a632SRahul Lakkireddy /**
8500462d115SRahul Lakkireddy  * link_start - enable a port
8510462d115SRahul Lakkireddy  * @dev: the port to enable
8520462d115SRahul Lakkireddy  *
8530462d115SRahul Lakkireddy  * Performs the MAC and PHY actions needed to enable a port.
8540462d115SRahul Lakkireddy  */
8550462d115SRahul Lakkireddy int link_start(struct port_info *pi)
8560462d115SRahul Lakkireddy {
8570462d115SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
8580462d115SRahul Lakkireddy 	int ret;
8595a9e303aSRahul Lakkireddy 	unsigned int mtu;
8605a9e303aSRahul Lakkireddy 
8615a9e303aSRahul Lakkireddy 	mtu = pi->eth_dev->data->dev_conf.rxmode.max_rx_pkt_len -
8625a9e303aSRahul Lakkireddy 	      (ETHER_HDR_LEN + ETHER_CRC_LEN);
8630462d115SRahul Lakkireddy 
8640462d115SRahul Lakkireddy 	/*
8650462d115SRahul Lakkireddy 	 * We do not set address filters and promiscuity here, the stack does
8660462d115SRahul Lakkireddy 	 * that step explicitly.
8670462d115SRahul Lakkireddy 	 */
8684b2eff45SRahul Lakkireddy 	ret = t4_set_rxmode(adapter, adapter->mbox, pi->viid, mtu, -1, -1,
8690462d115SRahul Lakkireddy 			    -1, 1, true);
8700462d115SRahul Lakkireddy 	if (ret == 0) {
8710462d115SRahul Lakkireddy 		ret = t4_change_mac(adapter, adapter->mbox, pi->viid,
8720462d115SRahul Lakkireddy 				    pi->xact_addr_filt,
8730462d115SRahul Lakkireddy 				    (u8 *)&pi->eth_dev->data->mac_addrs[0],
8740462d115SRahul Lakkireddy 				    true, true);
8750462d115SRahul Lakkireddy 		if (ret >= 0) {
8760462d115SRahul Lakkireddy 			pi->xact_addr_filt = ret;
8770462d115SRahul Lakkireddy 			ret = 0;
8780462d115SRahul Lakkireddy 		}
8790462d115SRahul Lakkireddy 	}
8800462d115SRahul Lakkireddy 	if (ret == 0)
8810462d115SRahul Lakkireddy 		ret = t4_link_l1cfg(adapter, adapter->mbox, pi->tx_chan,
8820462d115SRahul Lakkireddy 				    &pi->link_cfg);
8830462d115SRahul Lakkireddy 	if (ret == 0) {
8840462d115SRahul Lakkireddy 		/*
8850462d115SRahul Lakkireddy 		 * Enabling a Virtual Interface can result in an interrupt
8860462d115SRahul Lakkireddy 		 * during the processing of the VI Enable command and, in some
8870462d115SRahul Lakkireddy 		 * paths, result in an attempt to issue another command in the
8880462d115SRahul Lakkireddy 		 * interrupt context.  Thus, we disable interrupts during the
8890462d115SRahul Lakkireddy 		 * course of the VI Enable command ...
8900462d115SRahul Lakkireddy 		 */
8910462d115SRahul Lakkireddy 		ret = t4_enable_vi_params(adapter, adapter->mbox, pi->viid,
8920462d115SRahul Lakkireddy 					  true, true, false);
8930462d115SRahul Lakkireddy 	}
8940462d115SRahul Lakkireddy 	return ret;
8950462d115SRahul Lakkireddy }
8960462d115SRahul Lakkireddy 
8970462d115SRahul Lakkireddy /**
89892c8a632SRahul Lakkireddy  * cxgb4_write_rss - write the RSS table for a given port
89992c8a632SRahul Lakkireddy  * @pi: the port
90092c8a632SRahul Lakkireddy  * @queues: array of queue indices for RSS
90192c8a632SRahul Lakkireddy  *
90292c8a632SRahul Lakkireddy  * Sets up the portion of the HW RSS table for the port's VI to distribute
90392c8a632SRahul Lakkireddy  * packets to the Rx queues in @queues.
90492c8a632SRahul Lakkireddy  */
90592c8a632SRahul Lakkireddy int cxgb4_write_rss(const struct port_info *pi, const u16 *queues)
90692c8a632SRahul Lakkireddy {
90792c8a632SRahul Lakkireddy 	u16 *rss;
90892c8a632SRahul Lakkireddy 	int i, err;
90992c8a632SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
91092c8a632SRahul Lakkireddy 	const struct sge_eth_rxq *rxq;
91192c8a632SRahul Lakkireddy 
91292c8a632SRahul Lakkireddy 	/*  Should never be called before setting up sge eth rx queues */
91392c8a632SRahul Lakkireddy 	BUG_ON(!(adapter->flags & FULL_INIT_DONE));
91492c8a632SRahul Lakkireddy 
91592c8a632SRahul Lakkireddy 	rxq = &adapter->sge.ethrxq[pi->first_qset];
91692c8a632SRahul Lakkireddy 	rss = rte_zmalloc(NULL, pi->rss_size * sizeof(u16), 0);
91792c8a632SRahul Lakkireddy 	if (!rss)
91892c8a632SRahul Lakkireddy 		return -ENOMEM;
91992c8a632SRahul Lakkireddy 
92092c8a632SRahul Lakkireddy 	/* map the queue indices to queue ids */
92192c8a632SRahul Lakkireddy 	for (i = 0; i < pi->rss_size; i++, queues++)
92292c8a632SRahul Lakkireddy 		rss[i] = rxq[*queues].rspq.abs_id;
92392c8a632SRahul Lakkireddy 
92492c8a632SRahul Lakkireddy 	err = t4_config_rss_range(adapter, adapter->pf, pi->viid, 0,
92592c8a632SRahul Lakkireddy 				  pi->rss_size, rss, pi->rss_size);
92692c8a632SRahul Lakkireddy 	/*
92792c8a632SRahul Lakkireddy 	 * If Tunnel All Lookup isn't specified in the global RSS
92892c8a632SRahul Lakkireddy 	 * Configuration, then we need to specify a default Ingress
92992c8a632SRahul Lakkireddy 	 * Queue for any ingress packets which aren't hashed.  We'll
93092c8a632SRahul Lakkireddy 	 * use our first ingress queue ...
93192c8a632SRahul Lakkireddy 	 */
93292c8a632SRahul Lakkireddy 	if (!err)
93392c8a632SRahul Lakkireddy 		err = t4_config_vi_rss(adapter, adapter->mbox, pi->viid,
93492c8a632SRahul Lakkireddy 				       F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN |
93592c8a632SRahul Lakkireddy 				       F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN |
93692c8a632SRahul Lakkireddy 				       F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
93792c8a632SRahul Lakkireddy 				       F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN |
93892c8a632SRahul Lakkireddy 				       F_FW_RSS_VI_CONFIG_CMD_UDPEN,
93992c8a632SRahul Lakkireddy 				       rss[0]);
94092c8a632SRahul Lakkireddy 	rte_free(rss);
94192c8a632SRahul Lakkireddy 	return err;
94292c8a632SRahul Lakkireddy }
94392c8a632SRahul Lakkireddy 
94492c8a632SRahul Lakkireddy /**
94592c8a632SRahul Lakkireddy  * setup_rss - configure RSS
94692c8a632SRahul Lakkireddy  * @adapter: the adapter
94792c8a632SRahul Lakkireddy  *
94892c8a632SRahul Lakkireddy  * Sets up RSS to distribute packets to multiple receive queues.  We
94992c8a632SRahul Lakkireddy  * configure the RSS CPU lookup table to distribute to the number of HW
95092c8a632SRahul Lakkireddy  * receive queues, and the response queue lookup table to narrow that
95192c8a632SRahul Lakkireddy  * down to the response queues actually configured for each port.
95292c8a632SRahul Lakkireddy  * We always configure the RSS mapping for all ports since the mapping
95392c8a632SRahul Lakkireddy  * table has plenty of entries.
95492c8a632SRahul Lakkireddy  */
95592c8a632SRahul Lakkireddy int setup_rss(struct port_info *pi)
95692c8a632SRahul Lakkireddy {
95792c8a632SRahul Lakkireddy 	int j, err;
95892c8a632SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
95992c8a632SRahul Lakkireddy 
96092c8a632SRahul Lakkireddy 	dev_debug(adapter, "%s:  pi->rss_size = %u; pi->n_rx_qsets = %u\n",
96192c8a632SRahul Lakkireddy 		  __func__, pi->rss_size, pi->n_rx_qsets);
96292c8a632SRahul Lakkireddy 
9631039ee1cSEmmanuel Roullit 	if (!(pi->flags & PORT_RSS_DONE)) {
96492c8a632SRahul Lakkireddy 		if (adapter->flags & FULL_INIT_DONE) {
96592c8a632SRahul Lakkireddy 			/* Fill default values with equal distribution */
96692c8a632SRahul Lakkireddy 			for (j = 0; j < pi->rss_size; j++)
96792c8a632SRahul Lakkireddy 				pi->rss[j] = j % pi->n_rx_qsets;
96892c8a632SRahul Lakkireddy 
96992c8a632SRahul Lakkireddy 			err = cxgb4_write_rss(pi, pi->rss);
97092c8a632SRahul Lakkireddy 			if (err)
97192c8a632SRahul Lakkireddy 				return err;
97292c8a632SRahul Lakkireddy 			pi->flags |= PORT_RSS_DONE;
97392c8a632SRahul Lakkireddy 		}
97492c8a632SRahul Lakkireddy 	}
97592c8a632SRahul Lakkireddy 	return 0;
97692c8a632SRahul Lakkireddy }
97792c8a632SRahul Lakkireddy 
9780462d115SRahul Lakkireddy /*
9790462d115SRahul Lakkireddy  * Enable NAPI scheduling and interrupt generation for all Rx queues.
9800462d115SRahul Lakkireddy  */
9810462d115SRahul Lakkireddy static void enable_rx(struct adapter *adap)
9820462d115SRahul Lakkireddy {
9830462d115SRahul Lakkireddy 	struct sge *s = &adap->sge;
9840462d115SRahul Lakkireddy 	struct sge_rspq *q = &s->fw_evtq;
9850462d115SRahul Lakkireddy 	int i, j;
9860462d115SRahul Lakkireddy 
9870462d115SRahul Lakkireddy 	/* 0-increment GTS to start the timer and enable interrupts */
9880462d115SRahul Lakkireddy 	t4_write_reg(adap, MYPF_REG(A_SGE_PF_GTS),
9890462d115SRahul Lakkireddy 		     V_SEINTARM(q->intr_params) |
9900462d115SRahul Lakkireddy 		     V_INGRESSQID(q->cntxt_id));
9910462d115SRahul Lakkireddy 
9920462d115SRahul Lakkireddy 	for_each_port(adap, i) {
9930462d115SRahul Lakkireddy 		const struct port_info *pi = &adap->port[i];
9940462d115SRahul Lakkireddy 		struct rte_eth_dev *eth_dev = pi->eth_dev;
9950462d115SRahul Lakkireddy 
9960462d115SRahul Lakkireddy 		for (j = 0; j < eth_dev->data->nb_rx_queues; j++) {
9970462d115SRahul Lakkireddy 			q = eth_dev->data->rx_queues[j];
9980462d115SRahul Lakkireddy 
9990462d115SRahul Lakkireddy 			/*
10000462d115SRahul Lakkireddy 			 * 0-increment GTS to start the timer and enable
10010462d115SRahul Lakkireddy 			 * interrupts
10020462d115SRahul Lakkireddy 			 */
10030462d115SRahul Lakkireddy 			t4_write_reg(adap, MYPF_REG(A_SGE_PF_GTS),
10040462d115SRahul Lakkireddy 				     V_SEINTARM(q->intr_params) |
10050462d115SRahul Lakkireddy 				     V_INGRESSQID(q->cntxt_id));
10060462d115SRahul Lakkireddy 		}
10070462d115SRahul Lakkireddy 	}
10080462d115SRahul Lakkireddy }
10090462d115SRahul Lakkireddy 
10100462d115SRahul Lakkireddy /**
10110462d115SRahul Lakkireddy  * cxgb_up - enable the adapter
10120462d115SRahul Lakkireddy  * @adap: adapter being enabled
10130462d115SRahul Lakkireddy  *
10140462d115SRahul Lakkireddy  * Called when the first port is enabled, this function performs the
10150462d115SRahul Lakkireddy  * actions necessary to make an adapter operational, such as completing
10160462d115SRahul Lakkireddy  * the initialization of HW modules, and enabling interrupts.
10170462d115SRahul Lakkireddy  */
10180462d115SRahul Lakkireddy int cxgbe_up(struct adapter *adap)
10190462d115SRahul Lakkireddy {
10200462d115SRahul Lakkireddy 	enable_rx(adap);
10210462d115SRahul Lakkireddy 	t4_sge_tx_monitor_start(adap);
10220462d115SRahul Lakkireddy 	t4_intr_enable(adap);
10230462d115SRahul Lakkireddy 	adap->flags |= FULL_INIT_DONE;
10240462d115SRahul Lakkireddy 
10250462d115SRahul Lakkireddy 	/* TODO: deadman watchdog ?? */
10260462d115SRahul Lakkireddy 	return 0;
10270462d115SRahul Lakkireddy }
10280462d115SRahul Lakkireddy 
10290462d115SRahul Lakkireddy /*
10300462d115SRahul Lakkireddy  * Close the port
10310462d115SRahul Lakkireddy  */
10320462d115SRahul Lakkireddy int cxgbe_down(struct port_info *pi)
10330462d115SRahul Lakkireddy {
10340462d115SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
10350462d115SRahul Lakkireddy 	int err = 0;
10360462d115SRahul Lakkireddy 
10370462d115SRahul Lakkireddy 	err = t4_enable_vi(adapter, adapter->mbox, pi->viid, false, false);
10380462d115SRahul Lakkireddy 	if (err) {
10390462d115SRahul Lakkireddy 		dev_err(adapter, "%s: disable_vi failed: %d\n", __func__, err);
10400462d115SRahul Lakkireddy 		return err;
10410462d115SRahul Lakkireddy 	}
10420462d115SRahul Lakkireddy 
10430462d115SRahul Lakkireddy 	t4_reset_link_config(adapter, pi->port_id);
10440462d115SRahul Lakkireddy 	return 0;
10450462d115SRahul Lakkireddy }
10460462d115SRahul Lakkireddy 
10470462d115SRahul Lakkireddy /*
10480462d115SRahul Lakkireddy  * Release resources when all the ports have been stopped.
10490462d115SRahul Lakkireddy  */
10500462d115SRahul Lakkireddy void cxgbe_close(struct adapter *adapter)
10510462d115SRahul Lakkireddy {
10520462d115SRahul Lakkireddy 	struct port_info *pi;
10530462d115SRahul Lakkireddy 	int i;
10540462d115SRahul Lakkireddy 
10550462d115SRahul Lakkireddy 	if (adapter->flags & FULL_INIT_DONE) {
10560462d115SRahul Lakkireddy 		t4_intr_disable(adapter);
10570462d115SRahul Lakkireddy 		t4_sge_tx_monitor_stop(adapter);
10580462d115SRahul Lakkireddy 		t4_free_sge_resources(adapter);
10590462d115SRahul Lakkireddy 		for_each_port(adapter, i) {
10600462d115SRahul Lakkireddy 			pi = adap2pinfo(adapter, i);
10610462d115SRahul Lakkireddy 			if (pi->viid != 0)
10620462d115SRahul Lakkireddy 				t4_free_vi(adapter, adapter->mbox,
10630462d115SRahul Lakkireddy 					   adapter->pf, 0, pi->viid);
10640462d115SRahul Lakkireddy 			rte_free(pi->eth_dev->data->mac_addrs);
10650462d115SRahul Lakkireddy 		}
10660462d115SRahul Lakkireddy 		adapter->flags &= ~FULL_INIT_DONE;
10670462d115SRahul Lakkireddy 	}
10680462d115SRahul Lakkireddy 
10690462d115SRahul Lakkireddy 	if (adapter->flags & FW_OK)
10700462d115SRahul Lakkireddy 		t4_fw_bye(adapter, adapter->mbox);
10710462d115SRahul Lakkireddy }
10720462d115SRahul Lakkireddy 
107383189849SRahul Lakkireddy int cxgbe_probe(struct adapter *adapter)
107483189849SRahul Lakkireddy {
107583189849SRahul Lakkireddy 	struct port_info *pi;
1076*04868e5bSRahul Lakkireddy 	int chip;
107783189849SRahul Lakkireddy 	int func, i;
107883189849SRahul Lakkireddy 	int err = 0;
1079*04868e5bSRahul Lakkireddy 	u32 whoami;
108083189849SRahul Lakkireddy 
1081*04868e5bSRahul Lakkireddy 	whoami = t4_read_reg(adapter, A_PL_WHOAMI);
1082*04868e5bSRahul Lakkireddy 	chip = t4_get_chip_type(adapter,
1083*04868e5bSRahul Lakkireddy 			CHELSIO_PCI_ID_VER(adapter->pdev->id.device_id));
1084*04868e5bSRahul Lakkireddy 	if (chip < 0)
1085*04868e5bSRahul Lakkireddy 		return chip;
1086*04868e5bSRahul Lakkireddy 
1087*04868e5bSRahul Lakkireddy 	func = CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5 ?
1088*04868e5bSRahul Lakkireddy 	       G_SOURCEPF(whoami) : G_T6_SOURCEPF(whoami);
1089*04868e5bSRahul Lakkireddy 
109083189849SRahul Lakkireddy 	adapter->mbox = func;
109183189849SRahul Lakkireddy 	adapter->pf = func;
109283189849SRahul Lakkireddy 
109383189849SRahul Lakkireddy 	t4_os_lock_init(&adapter->mbox_lock);
109483189849SRahul Lakkireddy 	TAILQ_INIT(&adapter->mbox_list);
109583189849SRahul Lakkireddy 
109683189849SRahul Lakkireddy 	err = t4_prep_adapter(adapter);
109783189849SRahul Lakkireddy 	if (err)
109883189849SRahul Lakkireddy 		return err;
109983189849SRahul Lakkireddy 
110083189849SRahul Lakkireddy 	setup_memwin(adapter);
110183189849SRahul Lakkireddy 	err = adap_init0(adapter);
110283189849SRahul Lakkireddy 	if (err) {
110383189849SRahul Lakkireddy 		dev_err(adapter, "%s: Adapter initialization failed, error %d\n",
110483189849SRahul Lakkireddy 			__func__, err);
110583189849SRahul Lakkireddy 		goto out_free;
110683189849SRahul Lakkireddy 	}
110783189849SRahul Lakkireddy 
110883189849SRahul Lakkireddy 	if (!is_t4(adapter->params.chip)) {
110983189849SRahul Lakkireddy 		/*
111083189849SRahul Lakkireddy 		 * The userspace doorbell BAR is split evenly into doorbell
111183189849SRahul Lakkireddy 		 * regions, each associated with an egress queue.  If this
111283189849SRahul Lakkireddy 		 * per-queue region is large enough (at least UDBS_SEG_SIZE)
111383189849SRahul Lakkireddy 		 * then it can be used to submit a tx work request with an
111483189849SRahul Lakkireddy 		 * implied doorbell.  Enable write combining on the BAR if
111583189849SRahul Lakkireddy 		 * there is room for such work requests.
111683189849SRahul Lakkireddy 		 */
111783189849SRahul Lakkireddy 		int s_qpp, qpp, num_seg;
111883189849SRahul Lakkireddy 
111983189849SRahul Lakkireddy 		s_qpp = (S_QUEUESPERPAGEPF0 +
112083189849SRahul Lakkireddy 			(S_QUEUESPERPAGEPF1 - S_QUEUESPERPAGEPF0) *
112183189849SRahul Lakkireddy 			adapter->pf);
112283189849SRahul Lakkireddy 		qpp = 1 << ((t4_read_reg(adapter,
112383189849SRahul Lakkireddy 				A_SGE_EGRESS_QUEUES_PER_PAGE_PF) >> s_qpp)
112483189849SRahul Lakkireddy 				& M_QUEUESPERPAGEPF0);
11251f8613f1SRahul Lakkireddy 		num_seg = CXGBE_PAGE_SIZE / UDBS_SEG_SIZE;
112683189849SRahul Lakkireddy 		if (qpp > num_seg)
112783189849SRahul Lakkireddy 			dev_warn(adapter, "Incorrect SGE EGRESS QUEUES_PER_PAGE configuration, continuing in debug mode\n");
112883189849SRahul Lakkireddy 
112983189849SRahul Lakkireddy 		adapter->bar2 = (void *)adapter->pdev->mem_resource[2].addr;
113083189849SRahul Lakkireddy 		if (!adapter->bar2) {
113183189849SRahul Lakkireddy 			dev_err(adapter, "cannot map device bar2 region\n");
113283189849SRahul Lakkireddy 			err = -ENOMEM;
113383189849SRahul Lakkireddy 			goto out_free;
113483189849SRahul Lakkireddy 		}
113583189849SRahul Lakkireddy 		t4_write_reg(adapter, A_SGE_STAT_CFG, V_STATSOURCE_T5(7) |
113683189849SRahul Lakkireddy 			     V_STATMODE(0));
113783189849SRahul Lakkireddy 	}
113883189849SRahul Lakkireddy 
113983189849SRahul Lakkireddy 	for_each_port(adapter, i) {
114083189849SRahul Lakkireddy 		char name[RTE_ETH_NAME_MAX_LEN];
114183189849SRahul Lakkireddy 		struct rte_eth_dev_data *data = NULL;
114283189849SRahul Lakkireddy 		const unsigned int numa_node = rte_socket_id();
114383189849SRahul Lakkireddy 
114483189849SRahul Lakkireddy 		pi = &adapter->port[i];
114583189849SRahul Lakkireddy 		pi->adapter = adapter;
114683189849SRahul Lakkireddy 		pi->xact_addr_filt = -1;
114783189849SRahul Lakkireddy 		pi->port_id = i;
114883189849SRahul Lakkireddy 
114983189849SRahul Lakkireddy 		snprintf(name, sizeof(name), "cxgbe%d",
115083189849SRahul Lakkireddy 			 adapter->eth_dev->data->port_id + i);
115183189849SRahul Lakkireddy 
115283189849SRahul Lakkireddy 		if (i == 0) {
115383189849SRahul Lakkireddy 			/* First port is already allocated by DPDK */
115483189849SRahul Lakkireddy 			pi->eth_dev = adapter->eth_dev;
115583189849SRahul Lakkireddy 			goto allocate_mac;
115683189849SRahul Lakkireddy 		}
115783189849SRahul Lakkireddy 
115883189849SRahul Lakkireddy 		/*
115983189849SRahul Lakkireddy 		 * now do all data allocation - for eth_dev structure,
116083189849SRahul Lakkireddy 		 * and internal (private) data for the remaining ports
116183189849SRahul Lakkireddy 		 */
116283189849SRahul Lakkireddy 
116383189849SRahul Lakkireddy 		/* reserve an ethdev entry */
11646751f6deSDavid Marchand 		pi->eth_dev = rte_eth_dev_allocate(name);
116583189849SRahul Lakkireddy 		if (!pi->eth_dev)
116683189849SRahul Lakkireddy 			goto out_free;
116783189849SRahul Lakkireddy 
116883189849SRahul Lakkireddy 		data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
116983189849SRahul Lakkireddy 		if (!data)
117083189849SRahul Lakkireddy 			goto out_free;
117183189849SRahul Lakkireddy 
117283189849SRahul Lakkireddy 		data->port_id = adapter->eth_dev->data->port_id + i;
117383189849SRahul Lakkireddy 
117483189849SRahul Lakkireddy 		pi->eth_dev->data = data;
117583189849SRahul Lakkireddy 
117683189849SRahul Lakkireddy allocate_mac:
1177eac901ceSJan Blunck 		pi->eth_dev->device = &adapter->pdev->device;
117883189849SRahul Lakkireddy 		pi->eth_dev->data->dev_private = pi;
117983189849SRahul Lakkireddy 		pi->eth_dev->dev_ops = adapter->eth_dev->dev_ops;
11804a01078bSRahul Lakkireddy 		pi->eth_dev->tx_pkt_burst = adapter->eth_dev->tx_pkt_burst;
118192c8a632SRahul Lakkireddy 		pi->eth_dev->rx_pkt_burst = adapter->eth_dev->rx_pkt_burst;
118213b0f500SRahul Lakkireddy 
1183eac901ceSJan Blunck 		rte_eth_copy_pci_info(pi->eth_dev, adapter->pdev);
118413b0f500SRahul Lakkireddy 
118583189849SRahul Lakkireddy 		pi->eth_dev->data->mac_addrs = rte_zmalloc(name,
118683189849SRahul Lakkireddy 							   ETHER_ADDR_LEN, 0);
118783189849SRahul Lakkireddy 		if (!pi->eth_dev->data->mac_addrs) {
118883189849SRahul Lakkireddy 			dev_err(adapter, "%s: Mem allocation failed for storing mac addr, aborting\n",
118983189849SRahul Lakkireddy 				__func__);
119083189849SRahul Lakkireddy 			err = -1;
119183189849SRahul Lakkireddy 			goto out_free;
119283189849SRahul Lakkireddy 		}
119383189849SRahul Lakkireddy 	}
119483189849SRahul Lakkireddy 
119583189849SRahul Lakkireddy 	if (adapter->flags & FW_OK) {
119683189849SRahul Lakkireddy 		err = t4_port_init(adapter, adapter->mbox, adapter->pf, 0);
119783189849SRahul Lakkireddy 		if (err) {
119883189849SRahul Lakkireddy 			dev_err(adapter, "%s: t4_port_init failed with err %d\n",
119983189849SRahul Lakkireddy 				__func__, err);
120083189849SRahul Lakkireddy 			goto out_free;
120183189849SRahul Lakkireddy 		}
120283189849SRahul Lakkireddy 	}
120383189849SRahul Lakkireddy 
120492c8a632SRahul Lakkireddy 	cfg_queues(adapter->eth_dev);
120592c8a632SRahul Lakkireddy 
120683189849SRahul Lakkireddy 	print_port_info(adapter);
120783189849SRahul Lakkireddy 
120892c8a632SRahul Lakkireddy 	err = init_rss(adapter);
120992c8a632SRahul Lakkireddy 	if (err)
121092c8a632SRahul Lakkireddy 		goto out_free;
121192c8a632SRahul Lakkireddy 
121283189849SRahul Lakkireddy 	return 0;
121383189849SRahul Lakkireddy 
121483189849SRahul Lakkireddy out_free:
121583189849SRahul Lakkireddy 	for_each_port(adapter, i) {
121683189849SRahul Lakkireddy 		pi = adap2pinfo(adapter, i);
121783189849SRahul Lakkireddy 		if (pi->viid != 0)
121883189849SRahul Lakkireddy 			t4_free_vi(adapter, adapter->mbox, adapter->pf,
121983189849SRahul Lakkireddy 				   0, pi->viid);
122083189849SRahul Lakkireddy 		/* Skip first port since it'll be de-allocated by DPDK */
122183189849SRahul Lakkireddy 		if (i == 0)
122283189849SRahul Lakkireddy 			continue;
122383189849SRahul Lakkireddy 		if (pi->eth_dev->data)
122483189849SRahul Lakkireddy 			rte_free(pi->eth_dev->data);
122583189849SRahul Lakkireddy 	}
122683189849SRahul Lakkireddy 
122783189849SRahul Lakkireddy 	if (adapter->flags & FW_OK)
122883189849SRahul Lakkireddy 		t4_fw_bye(adapter, adapter->mbox);
122983189849SRahul Lakkireddy 	return -err;
123083189849SRahul Lakkireddy }
1231