xref: /dpdk/drivers/net/cxgbe/cxgbe_main.c (revision e12a0166c80f65e35408f4715b2f3a60763c3741)
12aa5c722SRahul Lakkireddy /* SPDX-License-Identifier: BSD-3-Clause
22aa5c722SRahul Lakkireddy  * Copyright(c) 2014-2018 Chelsio Communications.
383189849SRahul Lakkireddy  * All rights reserved.
483189849SRahul Lakkireddy  */
583189849SRahul Lakkireddy 
683189849SRahul Lakkireddy #include <sys/queue.h>
76d7d651bSRahul Lakkireddy #include <sys/stat.h>
883189849SRahul Lakkireddy #include <stdio.h>
983189849SRahul Lakkireddy #include <errno.h>
1083189849SRahul Lakkireddy #include <stdint.h>
1183189849SRahul Lakkireddy #include <string.h>
1283189849SRahul Lakkireddy #include <unistd.h>
1383189849SRahul Lakkireddy #include <stdarg.h>
1483189849SRahul Lakkireddy #include <inttypes.h>
156d7d651bSRahul Lakkireddy #include <fcntl.h>
1683189849SRahul Lakkireddy #include <netinet/in.h>
1783189849SRahul Lakkireddy 
1883189849SRahul Lakkireddy #include <rte_byteorder.h>
1983189849SRahul Lakkireddy #include <rte_common.h>
2083189849SRahul Lakkireddy #include <rte_cycles.h>
2183189849SRahul Lakkireddy #include <rte_interrupts.h>
2283189849SRahul Lakkireddy #include <rte_log.h>
2383189849SRahul Lakkireddy #include <rte_debug.h>
2483189849SRahul Lakkireddy #include <rte_pci.h>
2583189849SRahul Lakkireddy #include <rte_branch_prediction.h>
2683189849SRahul Lakkireddy #include <rte_memory.h>
2783189849SRahul Lakkireddy #include <rte_tailq.h>
2883189849SRahul Lakkireddy #include <rte_eal.h>
2983189849SRahul Lakkireddy #include <rte_alarm.h>
3083189849SRahul Lakkireddy #include <rte_ether.h>
31df96fd0dSBruce Richardson #include <ethdev_driver.h>
32df96fd0dSBruce Richardson #include <ethdev_pci.h>
3383189849SRahul Lakkireddy #include <rte_random.h>
341acb7f54SDavid Marchand #include <dev_driver.h>
35cda260a4SShagun Agrawal #include <rte_kvargs.h>
3683189849SRahul Lakkireddy 
3789c8bd95SRahul Lakkireddy #include "base/common.h"
3889c8bd95SRahul Lakkireddy #include "base/t4_regs.h"
3989c8bd95SRahul Lakkireddy #include "base/t4_msg.h"
4083189849SRahul Lakkireddy #include "cxgbe.h"
4151abd7b2SRahul Lakkireddy #include "cxgbe_pfvf.h"
423f2c1e20SShagun Agrawal #include "clip_tbl.h"
4323af667fSShagun Agrawal #include "l2t.h"
44a99564c6SKarra Satwik #include "smt.h"
456fda3f0dSShagun Agrawal #include "mps_tcam.h"
4683189849SRahul Lakkireddy 
47536db938SKarra Satwik static const u16 cxgbe_filter_mode_features[] = {
48536db938SKarra Satwik 	(F_FRAGMENTATION | F_MPSHITTYPE | F_MACMATCH | F_ETHERTYPE |
49536db938SKarra Satwik 	 F_PROTOCOL | F_PORT),
50536db938SKarra Satwik 	(F_FRAGMENTATION | F_MPSHITTYPE | F_MACMATCH | F_ETHERTYPE |
51536db938SKarra Satwik 	 F_PROTOCOL | F_FCOE),
52536db938SKarra Satwik 	(F_FRAGMENTATION | F_MPSHITTYPE | F_MACMATCH | F_ETHERTYPE | F_TOS |
53536db938SKarra Satwik 	 F_PORT),
54536db938SKarra Satwik 	(F_FRAGMENTATION | F_MPSHITTYPE | F_MACMATCH | F_ETHERTYPE | F_TOS |
55536db938SKarra Satwik 	 F_FCOE),
56536db938SKarra Satwik 	(F_FRAGMENTATION | F_MPSHITTYPE | F_MACMATCH | F_ETHERTYPE | F_PORT |
57536db938SKarra Satwik 	 F_FCOE),
58536db938SKarra Satwik 	(F_FRAGMENTATION | F_MPSHITTYPE | F_MACMATCH | F_PROTOCOL | F_TOS |
59536db938SKarra Satwik 	 F_PORT | F_FCOE),
60536db938SKarra Satwik 	(F_FRAGMENTATION | F_MPSHITTYPE | F_MACMATCH | F_PROTOCOL | F_VLAN |
61536db938SKarra Satwik 	 F_FCOE),
62536db938SKarra Satwik 	(F_FRAGMENTATION | F_MPSHITTYPE | F_MACMATCH | F_PROTOCOL | F_VNIC_ID |
63536db938SKarra Satwik 	 F_FCOE),
64536db938SKarra Satwik 	(F_FRAGMENTATION | F_MPSHITTYPE | F_MACMATCH | F_TOS | F_VLAN |
65536db938SKarra Satwik 	 F_FCOE),
66536db938SKarra Satwik 	(F_FRAGMENTATION | F_MPSHITTYPE | F_MACMATCH | F_TOS | F_VNIC_ID |
67536db938SKarra Satwik 	 F_FCOE),
68536db938SKarra Satwik 	(F_FRAGMENTATION | F_MPSHITTYPE | F_MACMATCH | F_VLAN | F_PORT |
69536db938SKarra Satwik 	 F_FCOE),
70536db938SKarra Satwik 	(F_FRAGMENTATION | F_MPSHITTYPE | F_MACMATCH | F_VNIC_ID | F_PORT |
71536db938SKarra Satwik 	 F_FCOE),
72536db938SKarra Satwik 	(F_FRAGMENTATION | F_MPSHITTYPE | F_ETHERTYPE | F_PROTOCOL | F_TOS |
73536db938SKarra Satwik 	 F_PORT | F_FCOE),
74536db938SKarra Satwik 	(F_FRAGMENTATION | F_MPSHITTYPE | F_ETHERTYPE | F_VLAN | F_PORT),
75536db938SKarra Satwik 	(F_FRAGMENTATION | F_MPSHITTYPE | F_ETHERTYPE | F_VLAN | F_FCOE),
76536db938SKarra Satwik 	(F_FRAGMENTATION | F_MPSHITTYPE | F_ETHERTYPE | F_VNIC_ID | F_PORT),
77536db938SKarra Satwik 	(F_FRAGMENTATION | F_MPSHITTYPE | F_ETHERTYPE | F_VNIC_ID | F_FCOE),
78536db938SKarra Satwik 	(F_FRAGMENTATION | F_MPSHITTYPE | F_PROTOCOL | F_TOS | F_VLAN | F_PORT),
79536db938SKarra Satwik 	(F_FRAGMENTATION | F_MPSHITTYPE | F_PROTOCOL | F_TOS | F_VLAN | F_FCOE),
80536db938SKarra Satwik 	(F_FRAGMENTATION | F_MPSHITTYPE | F_PROTOCOL | F_TOS | F_VNIC_ID |
81536db938SKarra Satwik 	 F_PORT),
82536db938SKarra Satwik 	(F_FRAGMENTATION | F_MPSHITTYPE | F_PROTOCOL | F_TOS | F_VNIC_ID |
83536db938SKarra Satwik 	 F_FCOE),
84536db938SKarra Satwik 	(F_FRAGMENTATION | F_MPSHITTYPE | F_PROTOCOL | F_VLAN | F_PORT |
85536db938SKarra Satwik 	 F_FCOE),
86536db938SKarra Satwik 	(F_FRAGMENTATION | F_MPSHITTYPE | F_PROTOCOL | F_VNIC_ID | F_PORT |
87536db938SKarra Satwik 	 F_FCOE),
88536db938SKarra Satwik 	(F_FRAGMENTATION | F_MPSHITTYPE | F_TOS | F_VLAN | F_PORT | F_FCOE),
89536db938SKarra Satwik 	(F_FRAGMENTATION | F_MPSHITTYPE | F_TOS | F_VNIC_ID | F_PORT | F_FCOE),
90536db938SKarra Satwik 	(F_FRAGMENTATION | F_MPSHITTYPE | F_VLAN | F_VNIC_ID | F_FCOE),
91536db938SKarra Satwik 	(F_FRAGMENTATION | F_MACMATCH | F_ETHERTYPE | F_PROTOCOL | F_PORT |
92536db938SKarra Satwik 	 F_FCOE),
93536db938SKarra Satwik 	(F_FRAGMENTATION | F_MACMATCH | F_ETHERTYPE | F_TOS | F_PORT | F_FCOE),
94536db938SKarra Satwik 	(F_FRAGMENTATION | F_MACMATCH | F_PROTOCOL | F_VLAN | F_PORT | F_FCOE),
95536db938SKarra Satwik 	(F_FRAGMENTATION | F_MACMATCH | F_PROTOCOL | F_VNIC_ID | F_PORT |
96536db938SKarra Satwik 	 F_FCOE),
97536db938SKarra Satwik 	(F_FRAGMENTATION | F_MACMATCH | F_TOS | F_VLAN | F_PORT | F_FCOE),
98536db938SKarra Satwik 	(F_FRAGMENTATION | F_MACMATCH | F_TOS | F_VNIC_ID | F_PORT | F_FCOE),
99536db938SKarra Satwik 	(F_FRAGMENTATION | F_ETHERTYPE | F_VLAN | F_PORT | F_FCOE),
100536db938SKarra Satwik 	(F_FRAGMENTATION | F_ETHERTYPE | F_VNIC_ID | F_PORT | F_FCOE),
101536db938SKarra Satwik 	(F_FRAGMENTATION | F_PROTOCOL | F_TOS | F_VLAN | F_FCOE),
102536db938SKarra Satwik 	(F_FRAGMENTATION | F_PROTOCOL | F_TOS | F_VNIC_ID | F_FCOE),
103536db938SKarra Satwik 	(F_FRAGMENTATION | F_VLAN | F_VNIC_ID | F_PORT | F_FCOE),
104536db938SKarra Satwik 	(F_MPSHITTYPE | F_MACMATCH | F_ETHERTYPE | F_PROTOCOL | F_PORT |
105536db938SKarra Satwik 	 F_FCOE),
106536db938SKarra Satwik 	(F_MPSHITTYPE | F_MACMATCH | F_ETHERTYPE | F_TOS | F_PORT | F_FCOE),
107536db938SKarra Satwik 	(F_MPSHITTYPE | F_MACMATCH | F_PROTOCOL | F_VLAN | F_PORT),
108536db938SKarra Satwik 	(F_MPSHITTYPE | F_MACMATCH | F_PROTOCOL | F_VNIC_ID | F_PORT),
109536db938SKarra Satwik 	(F_MPSHITTYPE | F_MACMATCH | F_TOS | F_VLAN | F_PORT),
110536db938SKarra Satwik 	(F_MPSHITTYPE | F_MACMATCH | F_TOS | F_VNIC_ID | F_PORT),
111536db938SKarra Satwik 	(F_MPSHITTYPE | F_ETHERTYPE | F_VLAN | F_PORT | F_FCOE),
112536db938SKarra Satwik 	(F_MPSHITTYPE | F_ETHERTYPE | F_VNIC_ID | F_PORT | F_FCOE),
113536db938SKarra Satwik 	(F_MPSHITTYPE | F_PROTOCOL | F_TOS | F_VLAN | F_PORT | F_FCOE),
114536db938SKarra Satwik 	(F_MPSHITTYPE | F_PROTOCOL | F_TOS | F_VNIC_ID | F_PORT | F_FCOE),
115536db938SKarra Satwik 	(F_MPSHITTYPE | F_VLAN | F_VNIC_ID | F_PORT),
116536db938SKarra Satwik };
117536db938SKarra Satwik 
1186f2a064bSShagun Agrawal /**
1196f2a064bSShagun Agrawal  * Allocate a chunk of memory. The allocated memory is cleared.
1206f2a064bSShagun Agrawal  */
t4_alloc_mem(size_t size)1216f2a064bSShagun Agrawal void *t4_alloc_mem(size_t size)
1226f2a064bSShagun Agrawal {
1236f2a064bSShagun Agrawal 	return rte_zmalloc(NULL, size, 0);
1246f2a064bSShagun Agrawal }
1256f2a064bSShagun Agrawal 
1266f2a064bSShagun Agrawal /**
1276f2a064bSShagun Agrawal  * Free memory allocated through t4_alloc_mem().
1286f2a064bSShagun Agrawal  */
t4_free_mem(void * addr)1296f2a064bSShagun Agrawal void t4_free_mem(void *addr)
1306f2a064bSShagun Agrawal {
1316f2a064bSShagun Agrawal 	rte_free(addr);
1326f2a064bSShagun Agrawal }
1336f2a064bSShagun Agrawal 
13492c8a632SRahul Lakkireddy /*
13592c8a632SRahul Lakkireddy  * Response queue handler for the FW event queue.
13692c8a632SRahul Lakkireddy  */
fwevtq_handler(struct sge_rspq * q,const __be64 * rsp,__rte_unused const struct pkt_gl * gl)13792c8a632SRahul Lakkireddy static int fwevtq_handler(struct sge_rspq *q, const __be64 *rsp,
13892c8a632SRahul Lakkireddy 			  __rte_unused const struct pkt_gl *gl)
13992c8a632SRahul Lakkireddy {
14092c8a632SRahul Lakkireddy 	u8 opcode = ((const struct rss_header *)rsp)->opcode;
14192c8a632SRahul Lakkireddy 
14292c8a632SRahul Lakkireddy 	rsp++;                                          /* skip RSS header */
14392c8a632SRahul Lakkireddy 
14492c8a632SRahul Lakkireddy 	/*
14592c8a632SRahul Lakkireddy 	 * FW can send EGR_UPDATEs encapsulated in a CPL_FW4_MSG.
14692c8a632SRahul Lakkireddy 	 */
14792c8a632SRahul Lakkireddy 	if (unlikely(opcode == CPL_FW4_MSG &&
14892c8a632SRahul Lakkireddy 		     ((const struct cpl_fw4_msg *)rsp)->type ==
14992c8a632SRahul Lakkireddy 		      FW_TYPE_RSSCPL)) {
15092c8a632SRahul Lakkireddy 		rsp++;
15192c8a632SRahul Lakkireddy 		opcode = ((const struct rss_header *)rsp)->opcode;
15292c8a632SRahul Lakkireddy 		rsp++;
15392c8a632SRahul Lakkireddy 		if (opcode != CPL_SGE_EGR_UPDATE) {
15492c8a632SRahul Lakkireddy 			dev_err(q->adapter, "unexpected FW4/CPL %#x on FW event queue\n",
15592c8a632SRahul Lakkireddy 				opcode);
15692c8a632SRahul Lakkireddy 			goto out;
15792c8a632SRahul Lakkireddy 		}
15892c8a632SRahul Lakkireddy 	}
15992c8a632SRahul Lakkireddy 
16092c8a632SRahul Lakkireddy 	if (likely(opcode == CPL_SGE_EGR_UPDATE)) {
16192c8a632SRahul Lakkireddy 		/* do nothing */
16292c8a632SRahul Lakkireddy 	} else if (opcode == CPL_FW6_MSG || opcode == CPL_FW4_MSG) {
16392c8a632SRahul Lakkireddy 		const struct cpl_fw6_msg *msg = (const void *)rsp;
16492c8a632SRahul Lakkireddy 
16592c8a632SRahul Lakkireddy 		t4_handle_fw_rpl(q->adapter, msg->data);
16641dc98b0SShagun Agrawal 	} else if (opcode == CPL_ABORT_RPL_RSS) {
16741dc98b0SShagun Agrawal 		const struct cpl_abort_rpl_rss *p = (const void *)rsp;
16841dc98b0SShagun Agrawal 
16971e9b334SRahul Lakkireddy 		cxgbe_hash_del_filter_rpl(q->adapter, p);
1709eb2c9a4SShagun Agrawal 	} else if (opcode == CPL_SET_TCB_RPL) {
1719eb2c9a4SShagun Agrawal 		const struct cpl_set_tcb_rpl *p = (const void *)rsp;
1729eb2c9a4SShagun Agrawal 
17371e9b334SRahul Lakkireddy 		cxgbe_filter_rpl(q->adapter, p);
174af44a577SShagun Agrawal 	} else if (opcode == CPL_ACT_OPEN_RPL) {
175af44a577SShagun Agrawal 		const struct cpl_act_open_rpl *p = (const void *)rsp;
176af44a577SShagun Agrawal 
17771e9b334SRahul Lakkireddy 		cxgbe_hash_filter_rpl(q->adapter, p);
17823af667fSShagun Agrawal 	} else if (opcode == CPL_L2T_WRITE_RPL) {
17923af667fSShagun Agrawal 		const struct cpl_l2t_write_rpl *p = (const void *)rsp;
18023af667fSShagun Agrawal 
18171e9b334SRahul Lakkireddy 		cxgbe_do_l2t_write_rpl(q->adapter, p);
182993541b2SKarra Satwik 	} else if (opcode == CPL_SMT_WRITE_RPL) {
183993541b2SKarra Satwik 		const struct cpl_smt_write_rpl *p = (const void *)rsp;
184993541b2SKarra Satwik 
185993541b2SKarra Satwik 		cxgbe_do_smt_write_rpl(q->adapter, p);
18692c8a632SRahul Lakkireddy 	} else {
18792c8a632SRahul Lakkireddy 		dev_err(adapter, "unexpected CPL %#x on FW event queue\n",
18892c8a632SRahul Lakkireddy 			opcode);
18992c8a632SRahul Lakkireddy 	}
19092c8a632SRahul Lakkireddy out:
19192c8a632SRahul Lakkireddy 	return 0;
19292c8a632SRahul Lakkireddy }
19392c8a632SRahul Lakkireddy 
1943a3aaabcSShagun Agrawal /**
1953a3aaabcSShagun Agrawal  * Setup sge control queues to pass control information.
1963a3aaabcSShagun Agrawal  */
cxgbe_setup_sge_ctrl_txq(struct adapter * adapter)197b7fd9ea8SStephen Hemminger int cxgbe_setup_sge_ctrl_txq(struct adapter *adapter)
1983a3aaabcSShagun Agrawal {
1993a3aaabcSShagun Agrawal 	struct sge *s = &adapter->sge;
2003a3aaabcSShagun Agrawal 	int err = 0, i = 0;
2013a3aaabcSShagun Agrawal 
2023a3aaabcSShagun Agrawal 	for_each_port(adapter, i) {
20346df488bSRahul Lakkireddy 		struct port_info *pi = adap2pinfo(adapter, i);
2043a3aaabcSShagun Agrawal 		char name[RTE_ETH_NAME_MAX_LEN];
2053a3aaabcSShagun Agrawal 		struct sge_ctrl_txq *q = &s->ctrlq[i];
2063a3aaabcSShagun Agrawal 
2073a3aaabcSShagun Agrawal 		q->q.size = 1024;
2083a3aaabcSShagun Agrawal 		err = t4_sge_alloc_ctrl_txq(adapter, q,
2093a3aaabcSShagun Agrawal 					    adapter->eth_dev,  i,
2103a3aaabcSShagun Agrawal 					    s->fw_evtq.cntxt_id,
2113a3aaabcSShagun Agrawal 					    rte_socket_id());
2123a3aaabcSShagun Agrawal 		if (err) {
2133a3aaabcSShagun Agrawal 			dev_err(adapter, "Failed to alloc ctrl txq. Err: %d",
2143a3aaabcSShagun Agrawal 				err);
2153a3aaabcSShagun Agrawal 			goto out;
2163a3aaabcSShagun Agrawal 		}
21746df488bSRahul Lakkireddy 		snprintf(name, sizeof(name), "%s_ctrl_pool_%d",
21846df488bSRahul Lakkireddy 			 pi->eth_dev->device->driver->name,
21946df488bSRahul Lakkireddy 			 pi->eth_dev->data->port_id);
2203a3aaabcSShagun Agrawal 		q->mb_pool = rte_pktmbuf_pool_create(name, s->ctrlq[i].q.size,
2213a3aaabcSShagun Agrawal 						     RTE_CACHE_LINE_SIZE,
2223a3aaabcSShagun Agrawal 						     RTE_MBUF_PRIV_ALIGN,
2233a3aaabcSShagun Agrawal 						     RTE_MBUF_DEFAULT_BUF_SIZE,
2243a3aaabcSShagun Agrawal 						     SOCKET_ID_ANY);
2253a3aaabcSShagun Agrawal 		if (!q->mb_pool) {
22646df488bSRahul Lakkireddy 			err = -rte_errno;
22746df488bSRahul Lakkireddy 			dev_err(adapter,
22846df488bSRahul Lakkireddy 				"Can't create ctrl pool for port %d. Err: %d\n",
22946df488bSRahul Lakkireddy 				pi->eth_dev->data->port_id, err);
2303a3aaabcSShagun Agrawal 			goto out;
2313a3aaabcSShagun Agrawal 		}
2323a3aaabcSShagun Agrawal 	}
2333a3aaabcSShagun Agrawal 	return 0;
2343a3aaabcSShagun Agrawal out:
2353a3aaabcSShagun Agrawal 	t4_free_sge_resources(adapter);
2363a3aaabcSShagun Agrawal 	return err;
2373a3aaabcSShagun Agrawal }
2383a3aaabcSShagun Agrawal 
2399eb2c9a4SShagun Agrawal /**
2409eb2c9a4SShagun Agrawal  * cxgbe_poll_for_completion: Poll rxq for completion
2419eb2c9a4SShagun Agrawal  * @q: rxq to poll
242f1e9d2afSRahul Lakkireddy  * @ms: milliseconds to delay
2439eb2c9a4SShagun Agrawal  * @cnt: number of times to poll
2449eb2c9a4SShagun Agrawal  * @c: completion to check for 'done' status
2459eb2c9a4SShagun Agrawal  *
2469eb2c9a4SShagun Agrawal  * Polls the rxq for reples until completion is done or the count
2479eb2c9a4SShagun Agrawal  * expires.
2489eb2c9a4SShagun Agrawal  */
cxgbe_poll_for_completion(struct sge_rspq * q,unsigned int ms,unsigned int cnt,struct t4_completion * c)249f1e9d2afSRahul Lakkireddy int cxgbe_poll_for_completion(struct sge_rspq *q, unsigned int ms,
2509eb2c9a4SShagun Agrawal 			      unsigned int cnt, struct t4_completion *c)
2519eb2c9a4SShagun Agrawal {
2529eb2c9a4SShagun Agrawal 	unsigned int i;
253f1e9d2afSRahul Lakkireddy 	unsigned int work_done, budget = 32;
2549eb2c9a4SShagun Agrawal 
2559eb2c9a4SShagun Agrawal 	if (!c)
2569eb2c9a4SShagun Agrawal 		return -EINVAL;
2579eb2c9a4SShagun Agrawal 
2589eb2c9a4SShagun Agrawal 	for (i = 0; i < cnt; i++) {
2599eb2c9a4SShagun Agrawal 		cxgbe_poll(q, NULL, budget, &work_done);
2609eb2c9a4SShagun Agrawal 		t4_os_lock(&c->lock);
2619eb2c9a4SShagun Agrawal 		if (c->done) {
2629eb2c9a4SShagun Agrawal 			t4_os_unlock(&c->lock);
2639eb2c9a4SShagun Agrawal 			return 0;
2649eb2c9a4SShagun Agrawal 		}
2659eb2c9a4SShagun Agrawal 		t4_os_unlock(&c->lock);
266f1e9d2afSRahul Lakkireddy 		rte_delay_ms(ms);
2679eb2c9a4SShagun Agrawal 	}
2689eb2c9a4SShagun Agrawal 	return -ETIMEDOUT;
2699eb2c9a4SShagun Agrawal }
2709eb2c9a4SShagun Agrawal 
cxgbe_setup_sge_fwevtq(struct adapter * adapter)271b7fd9ea8SStephen Hemminger int cxgbe_setup_sge_fwevtq(struct adapter *adapter)
27292c8a632SRahul Lakkireddy {
27392c8a632SRahul Lakkireddy 	struct sge *s = &adapter->sge;
27492c8a632SRahul Lakkireddy 	int err = 0;
27592c8a632SRahul Lakkireddy 	int msi_idx = 0;
27692c8a632SRahul Lakkireddy 
27792c8a632SRahul Lakkireddy 	err = t4_sge_alloc_rxq(adapter, &s->fw_evtq, true, adapter->eth_dev,
27892c8a632SRahul Lakkireddy 			       msi_idx, NULL, fwevtq_handler, -1, NULL, 0,
27992c8a632SRahul Lakkireddy 			       rte_socket_id());
28092c8a632SRahul Lakkireddy 	return err;
28192c8a632SRahul Lakkireddy }
28292c8a632SRahul Lakkireddy 
closest_timer(const struct sge * s,int time)28392c8a632SRahul Lakkireddy static int closest_timer(const struct sge *s, int time)
28492c8a632SRahul Lakkireddy {
28592c8a632SRahul Lakkireddy 	unsigned int i, match = 0;
28692c8a632SRahul Lakkireddy 	int delta, min_delta = INT_MAX;
28792c8a632SRahul Lakkireddy 
28892c8a632SRahul Lakkireddy 	for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
28992c8a632SRahul Lakkireddy 		delta = time - s->timer_val[i];
29092c8a632SRahul Lakkireddy 		if (delta < 0)
29192c8a632SRahul Lakkireddy 			delta = -delta;
29292c8a632SRahul Lakkireddy 		if (delta < min_delta) {
29392c8a632SRahul Lakkireddy 			min_delta = delta;
29492c8a632SRahul Lakkireddy 			match = i;
29592c8a632SRahul Lakkireddy 		}
29692c8a632SRahul Lakkireddy 	}
29792c8a632SRahul Lakkireddy 	return match;
29892c8a632SRahul Lakkireddy }
29992c8a632SRahul Lakkireddy 
closest_thres(const struct sge * s,int thres)30092c8a632SRahul Lakkireddy static int closest_thres(const struct sge *s, int thres)
30192c8a632SRahul Lakkireddy {
30292c8a632SRahul Lakkireddy 	unsigned int i, match = 0;
30392c8a632SRahul Lakkireddy 	int delta, min_delta = INT_MAX;
30492c8a632SRahul Lakkireddy 
30592c8a632SRahul Lakkireddy 	for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
30692c8a632SRahul Lakkireddy 		delta = thres - s->counter_val[i];
30792c8a632SRahul Lakkireddy 		if (delta < 0)
30892c8a632SRahul Lakkireddy 			delta = -delta;
30992c8a632SRahul Lakkireddy 		if (delta < min_delta) {
31092c8a632SRahul Lakkireddy 			min_delta = delta;
31192c8a632SRahul Lakkireddy 			match = i;
31292c8a632SRahul Lakkireddy 		}
31392c8a632SRahul Lakkireddy 	}
31492c8a632SRahul Lakkireddy 	return match;
31592c8a632SRahul Lakkireddy }
31692c8a632SRahul Lakkireddy 
31792c8a632SRahul Lakkireddy /**
31892c8a632SRahul Lakkireddy  * cxgb4_set_rspq_intr_params - set a queue's interrupt holdoff parameters
31992c8a632SRahul Lakkireddy  * @q: the Rx queue
32092c8a632SRahul Lakkireddy  * @us: the hold-off time in us, or 0 to disable timer
32192c8a632SRahul Lakkireddy  * @cnt: the hold-off packet count, or 0 to disable counter
32292c8a632SRahul Lakkireddy  *
32392c8a632SRahul Lakkireddy  * Sets an Rx queue's interrupt hold-off time and packet count.  At least
32492c8a632SRahul Lakkireddy  * one of the two needs to be enabled for the queue to generate interrupts.
32592c8a632SRahul Lakkireddy  */
cxgb4_set_rspq_intr_params(struct sge_rspq * q,unsigned int us,unsigned int cnt)32692c8a632SRahul Lakkireddy int cxgb4_set_rspq_intr_params(struct sge_rspq *q, unsigned int us,
32792c8a632SRahul Lakkireddy 			       unsigned int cnt)
32892c8a632SRahul Lakkireddy {
32992c8a632SRahul Lakkireddy 	struct adapter *adap = q->adapter;
33092c8a632SRahul Lakkireddy 	unsigned int timer_val;
33192c8a632SRahul Lakkireddy 
33292c8a632SRahul Lakkireddy 	if (cnt) {
33392c8a632SRahul Lakkireddy 		int err;
33492c8a632SRahul Lakkireddy 		u32 v, new_idx;
33592c8a632SRahul Lakkireddy 
33692c8a632SRahul Lakkireddy 		new_idx = closest_thres(&adap->sge, cnt);
33792c8a632SRahul Lakkireddy 		if (q->desc && q->pktcnt_idx != new_idx) {
33892c8a632SRahul Lakkireddy 			/* the queue has already been created, update it */
33992c8a632SRahul Lakkireddy 			v = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) |
34092c8a632SRahul Lakkireddy 			    V_FW_PARAMS_PARAM_X(
34192c8a632SRahul Lakkireddy 			    FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
34292c8a632SRahul Lakkireddy 			    V_FW_PARAMS_PARAM_YZ(q->cntxt_id);
34392c8a632SRahul Lakkireddy 			err = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
34492c8a632SRahul Lakkireddy 					    &v, &new_idx);
34592c8a632SRahul Lakkireddy 			if (err)
34692c8a632SRahul Lakkireddy 				return err;
34792c8a632SRahul Lakkireddy 		}
34892c8a632SRahul Lakkireddy 		q->pktcnt_idx = new_idx;
34992c8a632SRahul Lakkireddy 	}
35092c8a632SRahul Lakkireddy 
35192c8a632SRahul Lakkireddy 	timer_val = (us == 0) ? X_TIMERREG_RESTART_COUNTER :
35292c8a632SRahul Lakkireddy 				closest_timer(&adap->sge, us);
35392c8a632SRahul Lakkireddy 
35492c8a632SRahul Lakkireddy 	if ((us | cnt) == 0)
35592c8a632SRahul Lakkireddy 		q->intr_params = V_QINTR_TIMER_IDX(X_TIMERREG_UPDATE_CIDX);
35692c8a632SRahul Lakkireddy 	else
35792c8a632SRahul Lakkireddy 		q->intr_params = V_QINTR_TIMER_IDX(timer_val) |
35892c8a632SRahul Lakkireddy 				 V_QINTR_CNT_EN(cnt > 0);
35992c8a632SRahul Lakkireddy 	return 0;
36092c8a632SRahul Lakkireddy }
36192c8a632SRahul Lakkireddy 
3626f2a064bSShagun Agrawal /**
363af44a577SShagun Agrawal  * Allocate an active-open TID and set it to the supplied value.
364af44a577SShagun Agrawal  */
cxgbe_alloc_atid(struct tid_info * t,void * data)365af44a577SShagun Agrawal int cxgbe_alloc_atid(struct tid_info *t, void *data)
366af44a577SShagun Agrawal {
367af44a577SShagun Agrawal 	int atid = -1;
368af44a577SShagun Agrawal 
369af44a577SShagun Agrawal 	t4_os_lock(&t->atid_lock);
370af44a577SShagun Agrawal 	if (t->afree) {
371af44a577SShagun Agrawal 		union aopen_entry *p = t->afree;
372af44a577SShagun Agrawal 
373af44a577SShagun Agrawal 		atid = p - t->atid_tab;
374af44a577SShagun Agrawal 		t->afree = p->next;
375af44a577SShagun Agrawal 		p->data = data;
376af44a577SShagun Agrawal 		t->atids_in_use++;
377af44a577SShagun Agrawal 	}
378af44a577SShagun Agrawal 	t4_os_unlock(&t->atid_lock);
379af44a577SShagun Agrawal 	return atid;
380af44a577SShagun Agrawal }
381af44a577SShagun Agrawal 
382af44a577SShagun Agrawal /**
383af44a577SShagun Agrawal  * Release an active-open TID.
384af44a577SShagun Agrawal  */
cxgbe_free_atid(struct tid_info * t,unsigned int atid)385af44a577SShagun Agrawal void cxgbe_free_atid(struct tid_info *t, unsigned int atid)
386af44a577SShagun Agrawal {
387af44a577SShagun Agrawal 	union aopen_entry *p = &t->atid_tab[atid];
388af44a577SShagun Agrawal 
389af44a577SShagun Agrawal 	t4_os_lock(&t->atid_lock);
390af44a577SShagun Agrawal 	p->next = t->afree;
391af44a577SShagun Agrawal 	t->afree = p;
392af44a577SShagun Agrawal 	t->atids_in_use--;
393af44a577SShagun Agrawal 	t4_os_unlock(&t->atid_lock);
394af44a577SShagun Agrawal }
395af44a577SShagun Agrawal 
396af44a577SShagun Agrawal /**
39741dc98b0SShagun Agrawal  * Populate a TID_RELEASE WR.  Caller must properly size the skb.
39841dc98b0SShagun Agrawal  */
mk_tid_release(struct rte_mbuf * mbuf,unsigned int tid)39941dc98b0SShagun Agrawal static void mk_tid_release(struct rte_mbuf *mbuf, unsigned int tid)
40041dc98b0SShagun Agrawal {
40141dc98b0SShagun Agrawal 	struct cpl_tid_release *req;
40241dc98b0SShagun Agrawal 
40341dc98b0SShagun Agrawal 	req = rte_pktmbuf_mtod(mbuf, struct cpl_tid_release *);
40441dc98b0SShagun Agrawal 	INIT_TP_WR_MIT_CPL(req, CPL_TID_RELEASE, tid);
40541dc98b0SShagun Agrawal }
40641dc98b0SShagun Agrawal 
40741dc98b0SShagun Agrawal /**
40841dc98b0SShagun Agrawal  * Release a TID and inform HW.  If we are unable to allocate the release
40941dc98b0SShagun Agrawal  * message we defer to a work queue.
41041dc98b0SShagun Agrawal  */
cxgbe_remove_tid(struct tid_info * t,unsigned int chan,unsigned int tid,unsigned short family)41141dc98b0SShagun Agrawal void cxgbe_remove_tid(struct tid_info *t, unsigned int chan, unsigned int tid,
41241dc98b0SShagun Agrawal 		      unsigned short family)
41341dc98b0SShagun Agrawal {
41441dc98b0SShagun Agrawal 	struct rte_mbuf *mbuf;
41541dc98b0SShagun Agrawal 	struct adapter *adap = container_of(t, struct adapter, tids);
41641dc98b0SShagun Agrawal 
41741dc98b0SShagun Agrawal 	WARN_ON(tid >= t->ntids);
41841dc98b0SShagun Agrawal 
41941dc98b0SShagun Agrawal 	if (t->tid_tab[tid]) {
42041dc98b0SShagun Agrawal 		t->tid_tab[tid] = NULL;
421*e12a0166STyler Retzlaff 		rte_atomic_fetch_sub_explicit(&t->conns_in_use, 1, rte_memory_order_relaxed);
42241dc98b0SShagun Agrawal 		if (t->hash_base && tid >= t->hash_base) {
42341dc98b0SShagun Agrawal 			if (family == FILTER_TYPE_IPV4)
424*e12a0166STyler Retzlaff 				rte_atomic_fetch_sub_explicit(&t->hash_tids_in_use, 1,
425*e12a0166STyler Retzlaff 						   rte_memory_order_relaxed);
42641dc98b0SShagun Agrawal 		} else {
42741dc98b0SShagun Agrawal 			if (family == FILTER_TYPE_IPV4)
428*e12a0166STyler Retzlaff 				rte_atomic_fetch_sub_explicit(&t->tids_in_use, 1,
429*e12a0166STyler Retzlaff 						   rte_memory_order_relaxed);
43041dc98b0SShagun Agrawal 		}
43141dc98b0SShagun Agrawal 	}
43241dc98b0SShagun Agrawal 
43341dc98b0SShagun Agrawal 	mbuf = rte_pktmbuf_alloc((&adap->sge.ctrlq[chan])->mb_pool);
43441dc98b0SShagun Agrawal 	if (mbuf) {
43541dc98b0SShagun Agrawal 		mbuf->data_len = sizeof(struct cpl_tid_release);
43641dc98b0SShagun Agrawal 		mbuf->pkt_len = mbuf->data_len;
43741dc98b0SShagun Agrawal 		mk_tid_release(mbuf, tid);
43841dc98b0SShagun Agrawal 		t4_mgmt_tx(&adap->sge.ctrlq[chan], mbuf);
43941dc98b0SShagun Agrawal 	}
44041dc98b0SShagun Agrawal }
44141dc98b0SShagun Agrawal 
44241dc98b0SShagun Agrawal /**
443af44a577SShagun Agrawal  * Insert a TID.
444af44a577SShagun Agrawal  */
cxgbe_insert_tid(struct tid_info * t,void * data,unsigned int tid,unsigned short family)445af44a577SShagun Agrawal void cxgbe_insert_tid(struct tid_info *t, void *data, unsigned int tid,
446af44a577SShagun Agrawal 		      unsigned short family)
447af44a577SShagun Agrawal {
448af44a577SShagun Agrawal 	t->tid_tab[tid] = data;
449af44a577SShagun Agrawal 	if (t->hash_base && tid >= t->hash_base) {
450af44a577SShagun Agrawal 		if (family == FILTER_TYPE_IPV4)
451*e12a0166STyler Retzlaff 			rte_atomic_fetch_add_explicit(&t->hash_tids_in_use, 1,
452*e12a0166STyler Retzlaff 					   rte_memory_order_relaxed);
453af44a577SShagun Agrawal 	} else {
454af44a577SShagun Agrawal 		if (family == FILTER_TYPE_IPV4)
455*e12a0166STyler Retzlaff 			rte_atomic_fetch_add_explicit(&t->tids_in_use, 1,
456*e12a0166STyler Retzlaff 					   rte_memory_order_relaxed);
457af44a577SShagun Agrawal 	}
458af44a577SShagun Agrawal 
459*e12a0166STyler Retzlaff 	rte_atomic_fetch_add_explicit(&t->conns_in_use, 1, rte_memory_order_relaxed);
460af44a577SShagun Agrawal }
461af44a577SShagun Agrawal 
462af44a577SShagun Agrawal /**
4636f2a064bSShagun Agrawal  * Free TID tables.
4646f2a064bSShagun Agrawal  */
tid_free(struct tid_info * t)4656f2a064bSShagun Agrawal static void tid_free(struct tid_info *t)
4666f2a064bSShagun Agrawal {
4676f2a064bSShagun Agrawal 	if (t->tid_tab) {
4686f2a064bSShagun Agrawal 		rte_bitmap_free(t->ftid_bmap);
4696f2a064bSShagun Agrawal 
4706f2a064bSShagun Agrawal 		if (t->ftid_bmap_array)
4716f2a064bSShagun Agrawal 			t4_os_free(t->ftid_bmap_array);
4726f2a064bSShagun Agrawal 
4736f2a064bSShagun Agrawal 		t4_os_free(t->tid_tab);
4746f2a064bSShagun Agrawal 	}
4756f2a064bSShagun Agrawal 
4766f2a064bSShagun Agrawal 	memset(t, 0, sizeof(struct tid_info));
4776f2a064bSShagun Agrawal }
4786f2a064bSShagun Agrawal 
4796f2a064bSShagun Agrawal /**
4806f2a064bSShagun Agrawal  * Allocate and initialize the TID tables.  Returns 0 on success.
4816f2a064bSShagun Agrawal  */
tid_init(struct tid_info * t)4826f2a064bSShagun Agrawal static int tid_init(struct tid_info *t)
4836f2a064bSShagun Agrawal {
4846f2a064bSShagun Agrawal 	size_t size;
4856f2a064bSShagun Agrawal 	unsigned int ftid_bmap_size;
4863a381a41SShagun Agrawal 	unsigned int natids = t->natids;
4876f2a064bSShagun Agrawal 	unsigned int max_ftids = t->nftids;
4886f2a064bSShagun Agrawal 
4896f2a064bSShagun Agrawal 	ftid_bmap_size = rte_bitmap_get_memory_footprint(t->nftids);
4906f2a064bSShagun Agrawal 	size = t->ntids * sizeof(*t->tid_tab) +
4913a381a41SShagun Agrawal 		max_ftids * sizeof(*t->ftid_tab) +
4923a381a41SShagun Agrawal 		natids * sizeof(*t->atid_tab);
4936f2a064bSShagun Agrawal 
4946f2a064bSShagun Agrawal 	t->tid_tab = t4_os_alloc(size);
4956f2a064bSShagun Agrawal 	if (!t->tid_tab)
4966f2a064bSShagun Agrawal 		return -ENOMEM;
4976f2a064bSShagun Agrawal 
4983a381a41SShagun Agrawal 	t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
49927288219SRahul Lakkireddy 	t->ftid_tab = (struct filter_entry *)&t->atid_tab[t->natids];
5006f2a064bSShagun Agrawal 	t->ftid_bmap_array = t4_os_alloc(ftid_bmap_size);
5016f2a064bSShagun Agrawal 	if (!t->ftid_bmap_array) {
5026f2a064bSShagun Agrawal 		tid_free(t);
5036f2a064bSShagun Agrawal 		return -ENOMEM;
5046f2a064bSShagun Agrawal 	}
5056f2a064bSShagun Agrawal 
5063a381a41SShagun Agrawal 	t4_os_lock_init(&t->atid_lock);
5076f2a064bSShagun Agrawal 	t4_os_lock_init(&t->ftid_lock);
5083a381a41SShagun Agrawal 
5093a381a41SShagun Agrawal 	t->afree = NULL;
5103a381a41SShagun Agrawal 	t->atids_in_use = 0;
511a12f14bcSRahul Lakkireddy 	t->tids_in_use = 0;
512a12f14bcSRahul Lakkireddy 	t->conns_in_use = 0;
5133a381a41SShagun Agrawal 
5143a381a41SShagun Agrawal 	/* Setup the free list for atid_tab and clear the stid bitmap. */
5153a381a41SShagun Agrawal 	if (natids) {
5163a381a41SShagun Agrawal 		while (--natids)
5173a381a41SShagun Agrawal 			t->atid_tab[natids - 1].next = &t->atid_tab[natids];
5183a381a41SShagun Agrawal 		t->afree = t->atid_tab;
5193a381a41SShagun Agrawal 	}
5203a381a41SShagun Agrawal 
5216f2a064bSShagun Agrawal 	t->ftid_bmap = rte_bitmap_init(t->nftids, t->ftid_bmap_array,
5226f2a064bSShagun Agrawal 				       ftid_bmap_size);
5236f2a064bSShagun Agrawal 	if (!t->ftid_bmap) {
5246f2a064bSShagun Agrawal 		tid_free(t);
5256f2a064bSShagun Agrawal 		return -ENOMEM;
5266f2a064bSShagun Agrawal 	}
5276f2a064bSShagun Agrawal 
5286f2a064bSShagun Agrawal 	return 0;
5296f2a064bSShagun Agrawal }
5306f2a064bSShagun Agrawal 
init_rspq(struct adapter * adap,struct sge_rspq * q,unsigned int us,unsigned int cnt,unsigned int size,unsigned int iqe_size)531b7fd9ea8SStephen Hemminger static inline void init_rspq(struct adapter *adap, struct sge_rspq *q,
53292c8a632SRahul Lakkireddy 		      unsigned int us, unsigned int cnt,
53392c8a632SRahul Lakkireddy 		      unsigned int size, unsigned int iqe_size)
53492c8a632SRahul Lakkireddy {
53592c8a632SRahul Lakkireddy 	q->adapter = adap;
53692c8a632SRahul Lakkireddy 	cxgb4_set_rspq_intr_params(q, us, cnt);
53792c8a632SRahul Lakkireddy 	q->iqe_len = iqe_size;
53892c8a632SRahul Lakkireddy 	q->size = size;
53992c8a632SRahul Lakkireddy }
54092c8a632SRahul Lakkireddy 
cxgbe_cfg_queue_count(struct rte_eth_dev * eth_dev)541b7fd9ea8SStephen Hemminger int cxgbe_cfg_queue_count(struct rte_eth_dev *eth_dev)
54292c8a632SRahul Lakkireddy {
5437b3d5298SRahul Lakkireddy 	struct port_info *temp_pi, *pi = eth_dev->data->dev_private;
54492c8a632SRahul Lakkireddy 	struct adapter *adap = pi->adapter;
5457b3d5298SRahul Lakkireddy 	u16 first_txq = 0, first_rxq = 0;
54692c8a632SRahul Lakkireddy 	struct sge *s = &adap->sge;
5477b3d5298SRahul Lakkireddy 	u16 i, max_rxqs, max_txqs;
5487b3d5298SRahul Lakkireddy 
5497b3d5298SRahul Lakkireddy 	max_rxqs = s->max_ethqsets;
5507b3d5298SRahul Lakkireddy 	max_txqs = s->max_ethqsets;
5517b3d5298SRahul Lakkireddy 	for_each_port(adap, i) {
5527b3d5298SRahul Lakkireddy 		temp_pi = adap2pinfo(adap, i);
5537b3d5298SRahul Lakkireddy 		if (i == pi->port_id)
5547b3d5298SRahul Lakkireddy 			break;
5557b3d5298SRahul Lakkireddy 
5567b3d5298SRahul Lakkireddy 		if (max_rxqs <= temp_pi->n_rx_qsets ||
5577b3d5298SRahul Lakkireddy 		    max_txqs <= temp_pi->n_tx_qsets)
5587b3d5298SRahul Lakkireddy 			return -ENOMEM;
5597b3d5298SRahul Lakkireddy 
5607b3d5298SRahul Lakkireddy 		first_rxq += temp_pi->n_rx_qsets;
5617b3d5298SRahul Lakkireddy 		first_txq += temp_pi->n_tx_qsets;
5627b3d5298SRahul Lakkireddy 		max_rxqs -= temp_pi->n_rx_qsets;
5637b3d5298SRahul Lakkireddy 		max_txqs -= temp_pi->n_tx_qsets;
5647b3d5298SRahul Lakkireddy 	}
56592c8a632SRahul Lakkireddy 
56692c8a632SRahul Lakkireddy 	if ((eth_dev->data->nb_rx_queues < 1) ||
56792c8a632SRahul Lakkireddy 	    (eth_dev->data->nb_tx_queues < 1))
56892c8a632SRahul Lakkireddy 		return -EINVAL;
56992c8a632SRahul Lakkireddy 
5707b3d5298SRahul Lakkireddy 	if (eth_dev->data->nb_rx_queues > max_rxqs ||
5717b3d5298SRahul Lakkireddy 	    eth_dev->data->nb_tx_queues > max_txqs)
57292c8a632SRahul Lakkireddy 		return -EINVAL;
57392c8a632SRahul Lakkireddy 
57492c8a632SRahul Lakkireddy 	/* We must configure RSS, since config has changed*/
57592c8a632SRahul Lakkireddy 	pi->flags &= ~PORT_RSS_DONE;
57692c8a632SRahul Lakkireddy 
57792c8a632SRahul Lakkireddy 	pi->n_rx_qsets = eth_dev->data->nb_rx_queues;
57892c8a632SRahul Lakkireddy 	pi->n_tx_qsets = eth_dev->data->nb_tx_queues;
5797b3d5298SRahul Lakkireddy 	pi->first_rxqset = first_rxq;
5807b3d5298SRahul Lakkireddy 	pi->first_txqset = first_txq;
58192c8a632SRahul Lakkireddy 
58292c8a632SRahul Lakkireddy 	return 0;
58392c8a632SRahul Lakkireddy }
58492c8a632SRahul Lakkireddy 
cxgbe_cfg_queues_free(struct adapter * adap)5857b3d5298SRahul Lakkireddy void cxgbe_cfg_queues_free(struct adapter *adap)
5867b3d5298SRahul Lakkireddy {
5877b3d5298SRahul Lakkireddy 	if (adap->sge.ethtxq) {
5887b3d5298SRahul Lakkireddy 		rte_free(adap->sge.ethtxq);
5897b3d5298SRahul Lakkireddy 		adap->sge.ethtxq = NULL;
5907b3d5298SRahul Lakkireddy 	}
5917b3d5298SRahul Lakkireddy 
5927b3d5298SRahul Lakkireddy 	if (adap->sge.ethrxq) {
5937b3d5298SRahul Lakkireddy 		rte_free(adap->sge.ethrxq);
5947b3d5298SRahul Lakkireddy 		adap->sge.ethrxq = NULL;
5957b3d5298SRahul Lakkireddy 	}
5967b3d5298SRahul Lakkireddy 
5977b3d5298SRahul Lakkireddy 	adap->flags &= ~CFG_QUEUES;
5987b3d5298SRahul Lakkireddy }
5997b3d5298SRahul Lakkireddy 
cxgbe_cfg_queues(struct rte_eth_dev * eth_dev)6007b3d5298SRahul Lakkireddy int cxgbe_cfg_queues(struct rte_eth_dev *eth_dev)
60192c8a632SRahul Lakkireddy {
60263a97e58SStephen Hemminger 	struct port_info *pi = eth_dev->data->dev_private;
60392c8a632SRahul Lakkireddy 	struct adapter *adap = pi->adapter;
60492c8a632SRahul Lakkireddy 	struct sge *s = &adap->sge;
6057b3d5298SRahul Lakkireddy 	u16 i;
60692c8a632SRahul Lakkireddy 
60792c8a632SRahul Lakkireddy 	if (!(adap->flags & CFG_QUEUES)) {
6087b3d5298SRahul Lakkireddy 		s->ethrxq = rte_calloc_socket(NULL, s->max_ethqsets,
6097b3d5298SRahul Lakkireddy 					      sizeof(struct sge_eth_rxq), 0,
6107b3d5298SRahul Lakkireddy 					      rte_socket_id());
6117b3d5298SRahul Lakkireddy 		if (!s->ethrxq)
6127b3d5298SRahul Lakkireddy 			return -ENOMEM;
61392c8a632SRahul Lakkireddy 
6147b3d5298SRahul Lakkireddy 		s->ethtxq = rte_calloc_socket(NULL, s->max_ethqsets,
6157b3d5298SRahul Lakkireddy 					      sizeof(struct sge_eth_txq), 0,
6167b3d5298SRahul Lakkireddy 					      rte_socket_id());
6177b3d5298SRahul Lakkireddy 		if (!s->ethtxq) {
6187b3d5298SRahul Lakkireddy 			rte_free(s->ethrxq);
6197b3d5298SRahul Lakkireddy 			s->ethrxq = NULL;
6207b3d5298SRahul Lakkireddy 			return -ENOMEM;
62192c8a632SRahul Lakkireddy 		}
62292c8a632SRahul Lakkireddy 
6237b3d5298SRahul Lakkireddy 		for (i = 0; i < s->max_ethqsets; i++) {
62492c8a632SRahul Lakkireddy 			struct sge_eth_rxq *r = &s->ethrxq[i];
6257b3d5298SRahul Lakkireddy 			struct sge_eth_txq *t = &s->ethtxq[i];
62692c8a632SRahul Lakkireddy 
6276c280962SRahul Lakkireddy 			init_rspq(adap, &r->rspq, 5, 32, 1024, 64);
628745b8836SRahul Lakkireddy 			r->fl.size = 1024;
62992c8a632SRahul Lakkireddy 
6307b3d5298SRahul Lakkireddy 			t->q.size = 1024;
6317b3d5298SRahul Lakkireddy 		}
63292c8a632SRahul Lakkireddy 
63392c8a632SRahul Lakkireddy 		init_rspq(adap, &adap->sge.fw_evtq, 0, 0, 1024, 64);
63492c8a632SRahul Lakkireddy 		adap->flags |= CFG_QUEUES;
63592c8a632SRahul Lakkireddy 	}
6367b3d5298SRahul Lakkireddy 
6377b3d5298SRahul Lakkireddy 	return 0;
63892c8a632SRahul Lakkireddy }
63992c8a632SRahul Lakkireddy 
cxgbe_stats_get(struct port_info * pi,struct port_stats * stats)640856505d3SRahul Lakkireddy void cxgbe_stats_get(struct port_info *pi, struct port_stats *stats)
641856505d3SRahul Lakkireddy {
642856505d3SRahul Lakkireddy 	t4_get_port_stats_offset(pi->adapter, pi->tx_chan, stats,
643856505d3SRahul Lakkireddy 				 &pi->stats_base);
644856505d3SRahul Lakkireddy }
645856505d3SRahul Lakkireddy 
cxgbe_stats_reset(struct port_info * pi)646856505d3SRahul Lakkireddy void cxgbe_stats_reset(struct port_info *pi)
647856505d3SRahul Lakkireddy {
648856505d3SRahul Lakkireddy 	t4_clr_port_stats(pi->adapter, pi->tx_chan);
649856505d3SRahul Lakkireddy }
650856505d3SRahul Lakkireddy 
setup_memwin(struct adapter * adap)65183189849SRahul Lakkireddy static void setup_memwin(struct adapter *adap)
65283189849SRahul Lakkireddy {
65383189849SRahul Lakkireddy 	u32 mem_win0_base;
65483189849SRahul Lakkireddy 
65583189849SRahul Lakkireddy 	/* For T5, only relative offset inside the PCIe BAR is passed */
65683189849SRahul Lakkireddy 	mem_win0_base = MEMWIN0_BASE;
65783189849SRahul Lakkireddy 
65883189849SRahul Lakkireddy 	/*
65983189849SRahul Lakkireddy 	 * Set up memory window for accessing adapter memory ranges.  (Read
66083189849SRahul Lakkireddy 	 * back MA register to ensure that changes propagate before we attempt
66183189849SRahul Lakkireddy 	 * to use the new values.)
66283189849SRahul Lakkireddy 	 */
66383189849SRahul Lakkireddy 	t4_write_reg(adap,
66483189849SRahul Lakkireddy 		     PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN,
66583189849SRahul Lakkireddy 					 MEMWIN_NIC),
66683189849SRahul Lakkireddy 		     mem_win0_base | V_BIR(0) |
66783189849SRahul Lakkireddy 		     V_WINDOW(ilog2(MEMWIN0_APERTURE) - X_WINDOW_SHIFT));
66883189849SRahul Lakkireddy 	t4_read_reg(adap,
66983189849SRahul Lakkireddy 		    PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN,
67083189849SRahul Lakkireddy 					MEMWIN_NIC));
67183189849SRahul Lakkireddy }
67283189849SRahul Lakkireddy 
cxgbe_init_rss(struct adapter * adap)673b7fd9ea8SStephen Hemminger int cxgbe_init_rss(struct adapter *adap)
67492c8a632SRahul Lakkireddy {
67592c8a632SRahul Lakkireddy 	unsigned int i;
676bfcb257dSKumar Sanghvi 
677bfcb257dSKumar Sanghvi 	if (is_pf4(adap)) {
67892c8a632SRahul Lakkireddy 		int err;
67992c8a632SRahul Lakkireddy 
68092c8a632SRahul Lakkireddy 		err = t4_init_rss_mode(adap, adap->mbox);
68192c8a632SRahul Lakkireddy 		if (err)
68292c8a632SRahul Lakkireddy 			return err;
683bfcb257dSKumar Sanghvi 	}
68492c8a632SRahul Lakkireddy 
68592c8a632SRahul Lakkireddy 	for_each_port(adap, i) {
68692c8a632SRahul Lakkireddy 		struct port_info *pi = adap2pinfo(adap, i);
68792c8a632SRahul Lakkireddy 
6888dca8cc5SRahul Lakkireddy 		pi->rss = rte_zmalloc(NULL, pi->rss_size * sizeof(u16), 0);
68992c8a632SRahul Lakkireddy 		if (!pi->rss)
69092c8a632SRahul Lakkireddy 			return -ENOMEM;
69108e21af9SKumar Sanghvi 
69208e21af9SKumar Sanghvi 		pi->rss_hf = CXGBE_RSS_HF_ALL;
69392c8a632SRahul Lakkireddy 	}
69492c8a632SRahul Lakkireddy 	return 0;
69592c8a632SRahul Lakkireddy }
69692c8a632SRahul Lakkireddy 
697c962618cSRahul Lakkireddy /**
698c962618cSRahul Lakkireddy  * Dump basic information about the adapter.
699c962618cSRahul Lakkireddy  */
cxgbe_print_adapter_info(struct adapter * adap)700b7fd9ea8SStephen Hemminger void cxgbe_print_adapter_info(struct adapter *adap)
701c962618cSRahul Lakkireddy {
702c962618cSRahul Lakkireddy 	/**
703c962618cSRahul Lakkireddy 	 * Hardware/Firmware/etc. Version/Revision IDs.
704c962618cSRahul Lakkireddy 	 */
705c962618cSRahul Lakkireddy 	t4_dump_version_info(adap);
706c962618cSRahul Lakkireddy }
707c962618cSRahul Lakkireddy 
cxgbe_print_port_info(struct adapter * adap)708b7fd9ea8SStephen Hemminger void cxgbe_print_port_info(struct adapter *adap)
70983189849SRahul Lakkireddy {
71083189849SRahul Lakkireddy 	int i;
71183189849SRahul Lakkireddy 	char buf[80];
71283189849SRahul Lakkireddy 	struct rte_pci_addr *loc = &adap->pdev->addr;
71383189849SRahul Lakkireddy 
71483189849SRahul Lakkireddy 	for_each_port(adap, i) {
7152195df6dSRahul Lakkireddy 		const struct port_info *pi = adap2pinfo(adap, i);
71683189849SRahul Lakkireddy 		char *bufp = buf;
71783189849SRahul Lakkireddy 
71876488837SRahul Lakkireddy 		if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_100M)
7199da2a694SRahul Lakkireddy 			bufp += sprintf(bufp, "100M/");
72076488837SRahul Lakkireddy 		if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_1G)
7219da2a694SRahul Lakkireddy 			bufp += sprintf(bufp, "1G/");
72276488837SRahul Lakkireddy 		if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_10G)
72383189849SRahul Lakkireddy 			bufp += sprintf(bufp, "10G/");
72476488837SRahul Lakkireddy 		if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_25G)
7259da2a694SRahul Lakkireddy 			bufp += sprintf(bufp, "25G/");
72676488837SRahul Lakkireddy 		if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_40G)
72783189849SRahul Lakkireddy 			bufp += sprintf(bufp, "40G/");
72876488837SRahul Lakkireddy 		if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_50G)
72976488837SRahul Lakkireddy 			bufp += sprintf(bufp, "50G/");
73076488837SRahul Lakkireddy 		if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_100G)
7319da2a694SRahul Lakkireddy 			bufp += sprintf(bufp, "100G/");
73283189849SRahul Lakkireddy 		if (bufp != buf)
73383189849SRahul Lakkireddy 			--bufp;
73483189849SRahul Lakkireddy 		sprintf(bufp, "BASE-%s",
735efa8a43eSBruce Richardson 			t4_get_port_type_description(
736a83041b1SKarra Satwik 				(enum fw_port_type)pi->link_cfg.port_type));
73783189849SRahul Lakkireddy 
73883189849SRahul Lakkireddy 		dev_info(adap,
73983189849SRahul Lakkireddy 			 " " PCI_PRI_FMT " Chelsio rev %d %s %s\n",
74083189849SRahul Lakkireddy 			 loc->domain, loc->bus, loc->devid, loc->function,
74183189849SRahul Lakkireddy 			 CHELSIO_CHIP_RELEASE(adap->params.chip), buf,
74283189849SRahul Lakkireddy 			 (adap->flags & USING_MSIX) ? " MSI-X" :
74383189849SRahul Lakkireddy 			 (adap->flags & USING_MSI) ? " MSI" : "");
74483189849SRahul Lakkireddy 	}
74583189849SRahul Lakkireddy }
74683189849SRahul Lakkireddy 
check_devargs_handler(const char * key,const char * value,void * p)747dd7c9f12SRahul Lakkireddy static int check_devargs_handler(const char *key, const char *value, void *p)
748cda260a4SShagun Agrawal {
749dd7c9f12SRahul Lakkireddy 	if (!strncmp(key, CXGBE_DEVARG_CMN_KEEP_OVLAN, strlen(key)) ||
750fa033437SRahul Lakkireddy 	    !strncmp(key, CXGBE_DEVARG_CMN_TX_MODE_LATENCY, strlen(key)) ||
751dd7c9f12SRahul Lakkireddy 	    !strncmp(key, CXGBE_DEVARG_VF_FORCE_LINK_UP, strlen(key))) {
752dd7c9f12SRahul Lakkireddy 		if (!strncmp(value, "1", 1)) {
753dd7c9f12SRahul Lakkireddy 			bool *dst_val = (bool *)p;
754dd7c9f12SRahul Lakkireddy 
755dd7c9f12SRahul Lakkireddy 			*dst_val = true;
756dd7c9f12SRahul Lakkireddy 		}
757dd7c9f12SRahul Lakkireddy 	}
758cda260a4SShagun Agrawal 
759536db938SKarra Satwik 	if (!strncmp(key, CXGBE_DEVARG_PF_FILTER_MODE, strlen(key)) ||
760536db938SKarra Satwik 	    !strncmp(key, CXGBE_DEVARG_PF_FILTER_MASK, strlen(key))) {
761536db938SKarra Satwik 		u32 *dst_val = (u32 *)p;
762536db938SKarra Satwik 		char *endptr = NULL;
763536db938SKarra Satwik 		u32 arg_val;
764536db938SKarra Satwik 
765536db938SKarra Satwik 		arg_val = strtoul(value, &endptr, 16);
766536db938SKarra Satwik 		if (errno || endptr == value)
767536db938SKarra Satwik 			return -EINVAL;
768536db938SKarra Satwik 
769536db938SKarra Satwik 		*dst_val = arg_val;
770536db938SKarra Satwik 	}
771536db938SKarra Satwik 
772cda260a4SShagun Agrawal 	return 0;
773cda260a4SShagun Agrawal }
774cda260a4SShagun Agrawal 
cxgbe_get_devargs(struct rte_devargs * devargs,const char * key,void * p)775dd7c9f12SRahul Lakkireddy static int cxgbe_get_devargs(struct rte_devargs *devargs, const char *key,
776dd7c9f12SRahul Lakkireddy 			     void *p)
777cda260a4SShagun Agrawal {
778cda260a4SShagun Agrawal 	struct rte_kvargs *kvlist;
779dd7c9f12SRahul Lakkireddy 	int ret = 0;
780cda260a4SShagun Agrawal 
781cda260a4SShagun Agrawal 	if (!devargs)
782cda260a4SShagun Agrawal 		return 0;
783cda260a4SShagun Agrawal 
784cda260a4SShagun Agrawal 	kvlist = rte_kvargs_parse(devargs->args, NULL);
785cda260a4SShagun Agrawal 	if (!kvlist)
786cda260a4SShagun Agrawal 		return 0;
787cda260a4SShagun Agrawal 
788dd7c9f12SRahul Lakkireddy 	if (!rte_kvargs_count(kvlist, key))
789dd7c9f12SRahul Lakkireddy 		goto out;
790cda260a4SShagun Agrawal 
791dd7c9f12SRahul Lakkireddy 	ret = rte_kvargs_process(kvlist, key, check_devargs_handler, p);
792dd7c9f12SRahul Lakkireddy 
793dd7c9f12SRahul Lakkireddy out:
794cda260a4SShagun Agrawal 	rte_kvargs_free(kvlist);
795cda260a4SShagun Agrawal 
796dd7c9f12SRahul Lakkireddy 	return ret;
797dd7c9f12SRahul Lakkireddy }
798dd7c9f12SRahul Lakkireddy 
cxgbe_get_devargs_int(struct adapter * adap,bool * dst,const char * key,bool default_value)79900a6e154SDharmik Thakkar static void cxgbe_get_devargs_int(struct adapter *adap, bool *dst,
80000a6e154SDharmik Thakkar 				  const char *key, bool default_value)
801dd7c9f12SRahul Lakkireddy {
802dd7c9f12SRahul Lakkireddy 	struct rte_pci_device *pdev = adap->pdev;
80300a6e154SDharmik Thakkar 	int ret;
80400a6e154SDharmik Thakkar 	bool devarg_value = default_value;
805dd7c9f12SRahul Lakkireddy 
806dd7c9f12SRahul Lakkireddy 	*dst = default_value;
807dd7c9f12SRahul Lakkireddy 	if (!pdev)
808dd7c9f12SRahul Lakkireddy 		return;
809dd7c9f12SRahul Lakkireddy 
810dd7c9f12SRahul Lakkireddy 	ret = cxgbe_get_devargs(pdev->device.devargs, key, &devarg_value);
811dd7c9f12SRahul Lakkireddy 	if (ret)
812dd7c9f12SRahul Lakkireddy 		return;
813dd7c9f12SRahul Lakkireddy 
814dd7c9f12SRahul Lakkireddy 	*dst = devarg_value;
815dd7c9f12SRahul Lakkireddy }
816dd7c9f12SRahul Lakkireddy 
cxgbe_get_devargs_u32(struct adapter * adap,u32 * dst,const char * key,u32 default_value)817536db938SKarra Satwik static void cxgbe_get_devargs_u32(struct adapter *adap, u32 *dst,
818536db938SKarra Satwik 				  const char *key, u32 default_value)
819536db938SKarra Satwik {
820536db938SKarra Satwik 	struct rte_pci_device *pdev = adap->pdev;
821536db938SKarra Satwik 	u32 devarg_value = default_value;
822536db938SKarra Satwik 	int ret;
823536db938SKarra Satwik 
824536db938SKarra Satwik 	*dst = default_value;
825536db938SKarra Satwik 	if (!pdev)
826536db938SKarra Satwik 		return;
827536db938SKarra Satwik 
828536db938SKarra Satwik 	ret = cxgbe_get_devargs(pdev->device.devargs, key, &devarg_value);
829536db938SKarra Satwik 	if (ret)
830536db938SKarra Satwik 		return;
831536db938SKarra Satwik 
832536db938SKarra Satwik 	*dst = devarg_value;
833536db938SKarra Satwik }
834536db938SKarra Satwik 
cxgbe_process_devargs(struct adapter * adap)835dd7c9f12SRahul Lakkireddy void cxgbe_process_devargs(struct adapter *adap)
836dd7c9f12SRahul Lakkireddy {
837dd7c9f12SRahul Lakkireddy 	cxgbe_get_devargs_int(adap, &adap->devargs.keep_ovlan,
83800a6e154SDharmik Thakkar 			      CXGBE_DEVARG_CMN_KEEP_OVLAN, false);
839fa033437SRahul Lakkireddy 	cxgbe_get_devargs_int(adap, &adap->devargs.tx_mode_latency,
84000a6e154SDharmik Thakkar 			      CXGBE_DEVARG_CMN_TX_MODE_LATENCY, false);
841dd7c9f12SRahul Lakkireddy 	cxgbe_get_devargs_int(adap, &adap->devargs.force_link_up,
84200a6e154SDharmik Thakkar 			      CXGBE_DEVARG_VF_FORCE_LINK_UP, false);
843536db938SKarra Satwik 	cxgbe_get_devargs_u32(adap, &adap->devargs.filtermode,
844536db938SKarra Satwik 			      CXGBE_DEVARG_PF_FILTER_MODE, 0);
845536db938SKarra Satwik 	cxgbe_get_devargs_u32(adap, &adap->devargs.filtermask,
846536db938SKarra Satwik 			      CXGBE_DEVARG_PF_FILTER_MASK, 0);
847cda260a4SShagun Agrawal }
848cda260a4SShagun Agrawal 
configure_vlan_types(struct adapter * adapter)849cda260a4SShagun Agrawal static void configure_vlan_types(struct adapter *adapter)
850cda260a4SShagun Agrawal {
851cda260a4SShagun Agrawal 	int i;
852cda260a4SShagun Agrawal 
853cda260a4SShagun Agrawal 	for_each_port(adapter, i) {
854cda260a4SShagun Agrawal 		/* OVLAN Type 0x88a8 */
855cda260a4SShagun Agrawal 		t4_set_reg_field(adapter, MPS_PORT_RX_OVLAN_REG(i, A_RX_OVLAN0),
856cda260a4SShagun Agrawal 				 V_OVLAN_MASK(M_OVLAN_MASK) |
857cda260a4SShagun Agrawal 				 V_OVLAN_ETYPE(M_OVLAN_ETYPE),
858cda260a4SShagun Agrawal 				 V_OVLAN_MASK(M_OVLAN_MASK) |
859cda260a4SShagun Agrawal 				 V_OVLAN_ETYPE(0x88a8));
860cda260a4SShagun Agrawal 		/* OVLAN Type 0x9100 */
861cda260a4SShagun Agrawal 		t4_set_reg_field(adapter, MPS_PORT_RX_OVLAN_REG(i, A_RX_OVLAN1),
862cda260a4SShagun Agrawal 				 V_OVLAN_MASK(M_OVLAN_MASK) |
863cda260a4SShagun Agrawal 				 V_OVLAN_ETYPE(M_OVLAN_ETYPE),
864cda260a4SShagun Agrawal 				 V_OVLAN_MASK(M_OVLAN_MASK) |
865cda260a4SShagun Agrawal 				 V_OVLAN_ETYPE(0x9100));
866cda260a4SShagun Agrawal 
867cda260a4SShagun Agrawal 		/* IVLAN 0X8100 */
868cda260a4SShagun Agrawal 		t4_set_reg_field(adapter, MPS_PORT_RX_IVLAN(i),
869cda260a4SShagun Agrawal 				 V_IVLAN_ETYPE(M_IVLAN_ETYPE),
870cda260a4SShagun Agrawal 				 V_IVLAN_ETYPE(0x8100));
871cda260a4SShagun Agrawal 
872cda260a4SShagun Agrawal 		t4_set_reg_field(adapter, MPS_PORT_RX_CTL(i),
873cda260a4SShagun Agrawal 				 F_OVLAN_EN0 | F_OVLAN_EN1 |
874ed709206SRahul Lakkireddy 				 F_IVLAN_EN,
875cda260a4SShagun Agrawal 				 F_OVLAN_EN0 | F_OVLAN_EN1 |
876ed709206SRahul Lakkireddy 				 F_IVLAN_EN);
877cda260a4SShagun Agrawal 	}
878cda260a4SShagun Agrawal 
879dd7c9f12SRahul Lakkireddy 	t4_tp_wr_bits_indirect(adapter, A_TP_INGRESS_CONFIG, V_RM_OVLAN(1),
880dd7c9f12SRahul Lakkireddy 			       V_RM_OVLAN(!adapter->devargs.keep_ovlan));
881cda260a4SShagun Agrawal }
882cda260a4SShagun Agrawal 
cxgbe_get_filter_vnic_mode_from_devargs(u32 val)883536db938SKarra Satwik static int cxgbe_get_filter_vnic_mode_from_devargs(u32 val)
884536db938SKarra Satwik {
885536db938SKarra Satwik 	u32 vnic_mode;
886536db938SKarra Satwik 
887536db938SKarra Satwik 	vnic_mode = val & (CXGBE_DEVARGS_FILTER_MODE_PF_VF |
888536db938SKarra Satwik 			   CXGBE_DEVARGS_FILTER_MODE_VLAN_OUTER);
889536db938SKarra Satwik 	if (vnic_mode) {
890536db938SKarra Satwik 		switch (vnic_mode) {
891536db938SKarra Satwik 		case CXGBE_DEVARGS_FILTER_MODE_VLAN_OUTER:
892536db938SKarra Satwik 			return CXGBE_FILTER_VNIC_MODE_OVLAN;
893536db938SKarra Satwik 		case CXGBE_DEVARGS_FILTER_MODE_PF_VF:
894536db938SKarra Satwik 			return CXGBE_FILTER_VNIC_MODE_PFVF;
895536db938SKarra Satwik 		default:
896536db938SKarra Satwik 			return -EINVAL;
897536db938SKarra Satwik 		}
898536db938SKarra Satwik 	}
899536db938SKarra Satwik 
900536db938SKarra Satwik 	return CXGBE_FILTER_VNIC_MODE_NONE;
901536db938SKarra Satwik }
902536db938SKarra Satwik 
cxgbe_get_filter_mode_from_devargs(u32 val,bool closest_match)903536db938SKarra Satwik static int cxgbe_get_filter_mode_from_devargs(u32 val, bool closest_match)
904536db938SKarra Satwik {
905536db938SKarra Satwik 	int vnic_mode, fmode = 0;
906536db938SKarra Satwik 	bool found = false;
907536db938SKarra Satwik 	u8 i;
908536db938SKarra Satwik 
909536db938SKarra Satwik 	if (val >= CXGBE_DEVARGS_FILTER_MODE_MAX) {
910536db938SKarra Satwik 		pr_err("Unsupported flags set in filter mode. Must be < 0x%x\n",
911536db938SKarra Satwik 		       CXGBE_DEVARGS_FILTER_MODE_MAX);
912536db938SKarra Satwik 		return -ERANGE;
913536db938SKarra Satwik 	}
914536db938SKarra Satwik 
915536db938SKarra Satwik 	vnic_mode = cxgbe_get_filter_vnic_mode_from_devargs(val);
916536db938SKarra Satwik 	if (vnic_mode < 0) {
917536db938SKarra Satwik 		pr_err("Unsupported Vnic-mode, more than 1 Vnic-mode selected\n");
918536db938SKarra Satwik 		return vnic_mode;
919536db938SKarra Satwik 	}
920536db938SKarra Satwik 
921536db938SKarra Satwik 	if (vnic_mode)
922536db938SKarra Satwik 		fmode |= F_VNIC_ID;
923536db938SKarra Satwik 	if (val & CXGBE_DEVARGS_FILTER_MODE_PHYSICAL_PORT)
924536db938SKarra Satwik 		fmode |= F_PORT;
925536db938SKarra Satwik 	if (val & CXGBE_DEVARGS_FILTER_MODE_ETHERNET_DSTMAC)
926536db938SKarra Satwik 		fmode |= F_MACMATCH;
927536db938SKarra Satwik 	if (val & CXGBE_DEVARGS_FILTER_MODE_ETHERNET_ETHTYPE)
928536db938SKarra Satwik 		fmode |= F_ETHERTYPE;
929536db938SKarra Satwik 	if (val & CXGBE_DEVARGS_FILTER_MODE_VLAN_INNER)
930536db938SKarra Satwik 		fmode |= F_VLAN;
931536db938SKarra Satwik 	if (val & CXGBE_DEVARGS_FILTER_MODE_IP_TOS)
932536db938SKarra Satwik 		fmode |= F_TOS;
933536db938SKarra Satwik 	if (val & CXGBE_DEVARGS_FILTER_MODE_IP_PROTOCOL)
934536db938SKarra Satwik 		fmode |= F_PROTOCOL;
935536db938SKarra Satwik 
936536db938SKarra Satwik 	for (i = 0; i < ARRAY_SIZE(cxgbe_filter_mode_features); i++) {
937536db938SKarra Satwik 		if ((cxgbe_filter_mode_features[i] & fmode) == fmode) {
938536db938SKarra Satwik 			found = true;
939536db938SKarra Satwik 			break;
940536db938SKarra Satwik 		}
941536db938SKarra Satwik 	}
942536db938SKarra Satwik 
943536db938SKarra Satwik 	if (!found)
944536db938SKarra Satwik 		return -EINVAL;
945536db938SKarra Satwik 
946536db938SKarra Satwik 	return closest_match ? cxgbe_filter_mode_features[i] : fmode;
947536db938SKarra Satwik }
948536db938SKarra Satwik 
configure_filter_mode_mask(struct adapter * adap)949536db938SKarra Satwik static int configure_filter_mode_mask(struct adapter *adap)
950536db938SKarra Satwik {
951536db938SKarra Satwik 	u32 params[2], val[2], nparams = 0;
952536db938SKarra Satwik 	int ret;
953536db938SKarra Satwik 
954536db938SKarra Satwik 	if (!adap->devargs.filtermode && !adap->devargs.filtermask)
955536db938SKarra Satwik 		return 0;
956536db938SKarra Satwik 
957536db938SKarra Satwik 	if (!adap->devargs.filtermode || !adap->devargs.filtermask) {
958536db938SKarra Satwik 		pr_err("Unsupported, Provide both filtermode and filtermask devargs\n");
959536db938SKarra Satwik 		return -EINVAL;
960536db938SKarra Satwik 	}
961536db938SKarra Satwik 
962536db938SKarra Satwik 	if (adap->devargs.filtermask & ~adap->devargs.filtermode) {
963536db938SKarra Satwik 		pr_err("Unsupported, filtermask (0x%x) must be subset of filtermode (0x%x)\n",
964536db938SKarra Satwik 		       adap->devargs.filtermask, adap->devargs.filtermode);
965536db938SKarra Satwik 
966536db938SKarra Satwik 		return -EINVAL;
967536db938SKarra Satwik 	}
968536db938SKarra Satwik 
969536db938SKarra Satwik 	params[0] = CXGBE_FW_PARAM_DEV(FILTER) |
970536db938SKarra Satwik 		    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_FILTER_MODE_MASK);
971536db938SKarra Satwik 
972536db938SKarra Satwik 	ret = cxgbe_get_filter_mode_from_devargs(adap->devargs.filtermode,
973536db938SKarra Satwik 						 true);
974536db938SKarra Satwik 	if (ret < 0) {
975536db938SKarra Satwik 		pr_err("Unsupported filtermode devargs combination:0x%x\n",
976536db938SKarra Satwik 		       adap->devargs.filtermode);
977536db938SKarra Satwik 		return ret;
978536db938SKarra Satwik 	}
979536db938SKarra Satwik 
980536db938SKarra Satwik 	val[0] = V_FW_PARAMS_PARAM_FILTER_MODE(ret);
981536db938SKarra Satwik 
982536db938SKarra Satwik 	ret = cxgbe_get_filter_mode_from_devargs(adap->devargs.filtermask,
983536db938SKarra Satwik 						 false);
984536db938SKarra Satwik 	if (ret < 0) {
985536db938SKarra Satwik 		pr_err("Unsupported filtermask devargs combination:0x%x\n",
986536db938SKarra Satwik 		       adap->devargs.filtermask);
987536db938SKarra Satwik 		return ret;
988536db938SKarra Satwik 	}
989536db938SKarra Satwik 
990536db938SKarra Satwik 	val[0] |= V_FW_PARAMS_PARAM_FILTER_MASK(ret);
991536db938SKarra Satwik 
992536db938SKarra Satwik 	nparams++;
993536db938SKarra Satwik 
994536db938SKarra Satwik 	ret = cxgbe_get_filter_vnic_mode_from_devargs(adap->devargs.filtermode);
995536db938SKarra Satwik 	if (ret < 0)
996536db938SKarra Satwik 		return ret;
997536db938SKarra Satwik 
998536db938SKarra Satwik 	if (ret) {
999536db938SKarra Satwik 		params[1] = CXGBE_FW_PARAM_DEV(FILTER) |
1000536db938SKarra Satwik 			    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_FILTER_VNIC_MODE);
1001536db938SKarra Satwik 
1002536db938SKarra Satwik 		val[1] = ret - 1;
1003536db938SKarra Satwik 
1004536db938SKarra Satwik 		nparams++;
1005536db938SKarra Satwik 	}
1006536db938SKarra Satwik 
1007536db938SKarra Satwik 	return t4_set_params(adap, adap->mbox, adap->pf, 0, nparams,
1008536db938SKarra Satwik 			     params, val);
1009536db938SKarra Satwik }
1010536db938SKarra Satwik 
10116d7d651bSRahul Lakkireddy #define CXGBE_FW_CONFIG_PATH_T5 "/lib/firmware/cxgb4/t5-config.txt"
10126d7d651bSRahul Lakkireddy #define CXGBE_FW_CONFIG_PATH_T6 "/lib/firmware/cxgb4/t6-config.txt"
10136d7d651bSRahul Lakkireddy 
10146d7d651bSRahul Lakkireddy /*
10156d7d651bSRahul Lakkireddy  * Load firmware configuration from file in /lib/firmware/cxgb4/ path,
10166d7d651bSRahul Lakkireddy  * if it is present.
10176d7d651bSRahul Lakkireddy  */
cxgbe_load_fw_config_from_filesystem(struct adapter * adap,const char ** config_name,u32 * mem_type,u32 * mem_addr)10186d7d651bSRahul Lakkireddy static int cxgbe_load_fw_config_from_filesystem(struct adapter *adap,
10196d7d651bSRahul Lakkireddy 						const char **config_name,
10206d7d651bSRahul Lakkireddy 						u32 *mem_type, u32 *mem_addr)
10216d7d651bSRahul Lakkireddy {
10226d7d651bSRahul Lakkireddy 	u32 param, val, mtype, maddr;
10236d7d651bSRahul Lakkireddy 	const char *fw_cfg_path;
10246d7d651bSRahul Lakkireddy 	char *fw_cfg = NULL;
10256d7d651bSRahul Lakkireddy 	struct stat st;
10266d7d651bSRahul Lakkireddy 	int ret, fd;
10276d7d651bSRahul Lakkireddy 
10286d7d651bSRahul Lakkireddy 	switch (CHELSIO_CHIP_VERSION(adap->params.chip)) {
10296d7d651bSRahul Lakkireddy 	case CHELSIO_T5:
10306d7d651bSRahul Lakkireddy 		fw_cfg_path = CXGBE_FW_CONFIG_PATH_T5;
10316d7d651bSRahul Lakkireddy 		break;
10326d7d651bSRahul Lakkireddy 	case CHELSIO_T6:
10336d7d651bSRahul Lakkireddy 		fw_cfg_path = CXGBE_FW_CONFIG_PATH_T6;
10346d7d651bSRahul Lakkireddy 		break;
10356d7d651bSRahul Lakkireddy 	default:
10366d7d651bSRahul Lakkireddy 		return -ENOENT;
10376d7d651bSRahul Lakkireddy 	}
10386d7d651bSRahul Lakkireddy 
10396d7d651bSRahul Lakkireddy 	ret = open(fw_cfg_path, O_RDONLY);
10406d7d651bSRahul Lakkireddy 	if (ret < 0) {
10416d7d651bSRahul Lakkireddy 		dev_debug(adap, "Couldn't open FW config file\n");
10426d7d651bSRahul Lakkireddy 		return ret;
10436d7d651bSRahul Lakkireddy 	}
10446d7d651bSRahul Lakkireddy 
10456d7d651bSRahul Lakkireddy 	fd = ret;
10466d7d651bSRahul Lakkireddy 
10476d7d651bSRahul Lakkireddy 	ret = fstat(fd, &st);
10486d7d651bSRahul Lakkireddy 	if (ret < 0) {
10496d7d651bSRahul Lakkireddy 		dev_debug(adap, "Couldn't get FW config file size\n");
10506d7d651bSRahul Lakkireddy 		goto out_err;
10516d7d651bSRahul Lakkireddy 	}
10526d7d651bSRahul Lakkireddy 
10536d7d651bSRahul Lakkireddy 	if (st.st_size >= FLASH_CFG_MAX_SIZE) {
10546d7d651bSRahul Lakkireddy 		dev_debug(adap, "FW config file size >= max(%u)\n",
10556d7d651bSRahul Lakkireddy 			  FLASH_CFG_MAX_SIZE);
10566d7d651bSRahul Lakkireddy 		ret = -ENOMEM;
10576d7d651bSRahul Lakkireddy 		goto out_err;
10586d7d651bSRahul Lakkireddy 	}
10596d7d651bSRahul Lakkireddy 
10606d7d651bSRahul Lakkireddy 	fw_cfg = rte_zmalloc(NULL, st.st_size, 0);
10616d7d651bSRahul Lakkireddy 	if (fw_cfg == NULL) {
10626d7d651bSRahul Lakkireddy 		ret = -ENOMEM;
10636d7d651bSRahul Lakkireddy 		goto out_err;
10646d7d651bSRahul Lakkireddy 	}
10656d7d651bSRahul Lakkireddy 
10666d7d651bSRahul Lakkireddy 	if (read(fd, fw_cfg, st.st_size) != st.st_size) {
10676d7d651bSRahul Lakkireddy 		dev_debug(adap, "Couldn't read FW config file data\n");
10686d7d651bSRahul Lakkireddy 		ret = -EIO;
10696d7d651bSRahul Lakkireddy 		goto out_err;
10706d7d651bSRahul Lakkireddy 	}
10716d7d651bSRahul Lakkireddy 
10726d7d651bSRahul Lakkireddy 	close(fd);
10736d7d651bSRahul Lakkireddy 
10746d7d651bSRahul Lakkireddy 	/* Send it to FW to verify and update to new configuration */
10756d7d651bSRahul Lakkireddy 	param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
10766d7d651bSRahul Lakkireddy 		V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_CF);
10776d7d651bSRahul Lakkireddy 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, &param, &val);
10786d7d651bSRahul Lakkireddy 	if (ret < 0) {
10796d7d651bSRahul Lakkireddy 		dev_debug(adap, "FW config param query failed: %d\n", ret);
10806d7d651bSRahul Lakkireddy 		goto out_free;
10816d7d651bSRahul Lakkireddy 	}
10826d7d651bSRahul Lakkireddy 
10836d7d651bSRahul Lakkireddy 	mtype = val >> 8;
10846d7d651bSRahul Lakkireddy 	maddr = (val & 0xff) << 16;
10856d7d651bSRahul Lakkireddy 
10866d7d651bSRahul Lakkireddy 	t4_os_lock(&adap->win0_lock);
10876d7d651bSRahul Lakkireddy 	ret = t4_memory_rw(adap, MEMWIN_NIC, mtype, maddr, st.st_size,
10886d7d651bSRahul Lakkireddy 			   fw_cfg, T4_MEMORY_WRITE);
10896d7d651bSRahul Lakkireddy 	t4_os_unlock(&adap->win0_lock);
10906d7d651bSRahul Lakkireddy 	if (ret < 0) {
10916d7d651bSRahul Lakkireddy 		dev_debug(adap, "FW config file update failed: %d\n", ret);
10926d7d651bSRahul Lakkireddy 		goto out_free;
10936d7d651bSRahul Lakkireddy 	}
10946d7d651bSRahul Lakkireddy 
10956d7d651bSRahul Lakkireddy 	rte_free(fw_cfg);
10966d7d651bSRahul Lakkireddy 
10976d7d651bSRahul Lakkireddy 	*mem_type = mtype;
10986d7d651bSRahul Lakkireddy 	*mem_addr = maddr;
10996d7d651bSRahul Lakkireddy 	*config_name = fw_cfg_path;
11006d7d651bSRahul Lakkireddy 	return 0;
11016d7d651bSRahul Lakkireddy 
11026d7d651bSRahul Lakkireddy out_err:
11036d7d651bSRahul Lakkireddy 	close(fd);
11046d7d651bSRahul Lakkireddy out_free:
11056d7d651bSRahul Lakkireddy 	rte_free(fw_cfg);
11066d7d651bSRahul Lakkireddy 	return ret;
11076d7d651bSRahul Lakkireddy }
11086d7d651bSRahul Lakkireddy 
cxgbe_load_fw_config(struct adapter * adap)11096d7d651bSRahul Lakkireddy static int cxgbe_load_fw_config(struct adapter *adap)
11106d7d651bSRahul Lakkireddy {
11116d7d651bSRahul Lakkireddy 	struct fw_caps_config_cmd caps_cmd = { 0 };
1112c6db4a15SRahul Lakkireddy 	u32 finiver, finicsum, cfcsum, param, val;
11136d7d651bSRahul Lakkireddy 	const char *config_name = NULL;
1114c6db4a15SRahul Lakkireddy 	u32 mtype = 0, maddr = 0;
11156d7d651bSRahul Lakkireddy 	int ret;
11166d7d651bSRahul Lakkireddy 
11176d7d651bSRahul Lakkireddy 	ret = cxgbe_load_fw_config_from_filesystem(adap, &config_name,
11186d7d651bSRahul Lakkireddy 						   &mtype, &maddr);
11196d7d651bSRahul Lakkireddy 	if (ret < 0) {
11206d7d651bSRahul Lakkireddy 		config_name = "On Flash";
11216d7d651bSRahul Lakkireddy 
11226d7d651bSRahul Lakkireddy 		ret = t4_flash_cfg_addr(adap);
11236d7d651bSRahul Lakkireddy 		if (ret < 0) {
11246d7d651bSRahul Lakkireddy 			dev_warn(adap,
11256d7d651bSRahul Lakkireddy 				 "Finding address for FW config file in flash failed: %d\n",
11266d7d651bSRahul Lakkireddy 				 ret);
11276d7d651bSRahul Lakkireddy 			goto out_default_config;
11286d7d651bSRahul Lakkireddy 		}
11296d7d651bSRahul Lakkireddy 
11306d7d651bSRahul Lakkireddy 		mtype = FW_MEMTYPE_CF_FLASH;
11316d7d651bSRahul Lakkireddy 		maddr = ret;
11326d7d651bSRahul Lakkireddy 	}
11336d7d651bSRahul Lakkireddy 
11346d7d651bSRahul Lakkireddy 	/* Enable HASH filter region when support is available. */
11356d7d651bSRahul Lakkireddy 	val = 1;
11366d7d651bSRahul Lakkireddy 	param = CXGBE_FW_PARAM_DEV(HASHFILTER_WITH_OFLD);
11376d7d651bSRahul Lakkireddy 	t4_set_params(adap, adap->mbox, adap->pf, 0, 1, &param, &val);
11386d7d651bSRahul Lakkireddy 
11396d7d651bSRahul Lakkireddy 	/*
11406d7d651bSRahul Lakkireddy 	 * Issue a Capability Configuration command to the firmware to get it
11416d7d651bSRahul Lakkireddy 	 * to parse the Configuration File.
11426d7d651bSRahul Lakkireddy 	 */
11436d7d651bSRahul Lakkireddy 	caps_cmd.op_to_write = cpu_to_be32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
11446d7d651bSRahul Lakkireddy 					   F_FW_CMD_REQUEST | F_FW_CMD_READ);
11456d7d651bSRahul Lakkireddy 	caps_cmd.cfvalid_to_len16 =
11466d7d651bSRahul Lakkireddy 		cpu_to_be32(F_FW_CAPS_CONFIG_CMD_CFVALID |
11476d7d651bSRahul Lakkireddy 			    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
11486d7d651bSRahul Lakkireddy 			    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(maddr >> 16) |
11496d7d651bSRahul Lakkireddy 			    FW_LEN16(caps_cmd));
11506d7d651bSRahul Lakkireddy 	ret = t4_wr_mbox(adap, adap->mbox, &caps_cmd, sizeof(caps_cmd),
11516d7d651bSRahul Lakkireddy 			 &caps_cmd);
11526d7d651bSRahul Lakkireddy 
11536d7d651bSRahul Lakkireddy out_default_config:
11546d7d651bSRahul Lakkireddy 	/*
11556d7d651bSRahul Lakkireddy 	 * If the CAPS_CONFIG failed with an ENOENT (for a Firmware
11566d7d651bSRahul Lakkireddy 	 * Configuration File in filesystem or FLASH), our last gasp
11576d7d651bSRahul Lakkireddy 	 * effort is to use the Firmware Configuration File which is
11586d7d651bSRahul Lakkireddy 	 * embedded in the firmware.
11596d7d651bSRahul Lakkireddy 	 */
11606d7d651bSRahul Lakkireddy 	if (ret == -ENOENT) {
11616d7d651bSRahul Lakkireddy 		config_name = "Firmware Default";
11626d7d651bSRahul Lakkireddy 
11636d7d651bSRahul Lakkireddy 		memset(&caps_cmd, 0, sizeof(caps_cmd));
11646d7d651bSRahul Lakkireddy 		caps_cmd.op_to_write =
11656d7d651bSRahul Lakkireddy 			cpu_to_be32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
11666d7d651bSRahul Lakkireddy 				    F_FW_CMD_REQUEST | F_FW_CMD_READ);
11676d7d651bSRahul Lakkireddy 		caps_cmd.cfvalid_to_len16 = cpu_to_be32(FW_LEN16(caps_cmd));
11686d7d651bSRahul Lakkireddy 		ret = t4_wr_mbox(adap, adap->mbox, &caps_cmd, sizeof(caps_cmd),
11696d7d651bSRahul Lakkireddy 				 &caps_cmd);
11706d7d651bSRahul Lakkireddy 	}
11716d7d651bSRahul Lakkireddy 
11726d7d651bSRahul Lakkireddy 	if (ret < 0) {
11736d7d651bSRahul Lakkireddy 		dev_info(adap,
11746d7d651bSRahul Lakkireddy 			 "Failed to configure using %s Firmware Configuration file: %d\n",
11756d7d651bSRahul Lakkireddy 			 config_name, ret);
11766d7d651bSRahul Lakkireddy 		return ret;
11776d7d651bSRahul Lakkireddy 	}
11786d7d651bSRahul Lakkireddy 
11796d7d651bSRahul Lakkireddy 	finiver = be32_to_cpu(caps_cmd.finiver);
11806d7d651bSRahul Lakkireddy 	finicsum = be32_to_cpu(caps_cmd.finicsum);
11816d7d651bSRahul Lakkireddy 	cfcsum = be32_to_cpu(caps_cmd.cfcsum);
11826d7d651bSRahul Lakkireddy 	if (finicsum != cfcsum)
11836d7d651bSRahul Lakkireddy 		dev_warn(adap,
11846d7d651bSRahul Lakkireddy 			 "Configuration File checksum mismatch: [fini] csum=0x%x, computed csum=0x%x\n",
11856d7d651bSRahul Lakkireddy 			 finicsum, cfcsum);
11866d7d651bSRahul Lakkireddy 
11876d7d651bSRahul Lakkireddy 	/*
11886d7d651bSRahul Lakkireddy 	 * If we're a pure NIC driver then disable all offloading facilities.
11896d7d651bSRahul Lakkireddy 	 * This will allow the firmware to optimize aspects of the hardware
11906d7d651bSRahul Lakkireddy 	 * configuration which will result in improved performance.
11916d7d651bSRahul Lakkireddy 	 */
11926d7d651bSRahul Lakkireddy 	caps_cmd.niccaps &= cpu_to_be16(~FW_CAPS_CONFIG_NIC_ETHOFLD);
11936d7d651bSRahul Lakkireddy 	caps_cmd.toecaps = 0;
11946d7d651bSRahul Lakkireddy 	caps_cmd.iscsicaps = 0;
11956d7d651bSRahul Lakkireddy 	caps_cmd.rdmacaps = 0;
11966d7d651bSRahul Lakkireddy 	caps_cmd.fcoecaps = 0;
11976d7d651bSRahul Lakkireddy 	caps_cmd.cryptocaps = 0;
11986d7d651bSRahul Lakkireddy 
11996d7d651bSRahul Lakkireddy 	/*
12006d7d651bSRahul Lakkireddy 	 * And now tell the firmware to use the configuration we just loaded.
12016d7d651bSRahul Lakkireddy 	 */
12026d7d651bSRahul Lakkireddy 	caps_cmd.op_to_write = cpu_to_be32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
12036d7d651bSRahul Lakkireddy 					   F_FW_CMD_REQUEST | F_FW_CMD_WRITE);
12046d7d651bSRahul Lakkireddy 	caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
12056d7d651bSRahul Lakkireddy 	ret = t4_wr_mbox(adap, adap->mbox, &caps_cmd, sizeof(caps_cmd),
12066d7d651bSRahul Lakkireddy 			 NULL);
12076d7d651bSRahul Lakkireddy 	if (ret < 0) {
12086d7d651bSRahul Lakkireddy 		dev_warn(adap, "Unable to finalize Firmware Capabilities %d\n",
12096d7d651bSRahul Lakkireddy 			 ret);
12106d7d651bSRahul Lakkireddy 		return ret;
12116d7d651bSRahul Lakkireddy 	}
12126d7d651bSRahul Lakkireddy 
12136d7d651bSRahul Lakkireddy 	/*
12146d7d651bSRahul Lakkireddy 	 * Return successfully and note that we're operating with parameters
12156d7d651bSRahul Lakkireddy 	 * not supplied by the driver, rather than from hard-wired
12166d7d651bSRahul Lakkireddy 	 * initialization constants buried in the driver.
12176d7d651bSRahul Lakkireddy 	 */
12186d7d651bSRahul Lakkireddy 	dev_info(adap,
12196d7d651bSRahul Lakkireddy 		 "Successfully configured using Firmware Configuration File \"%s\", version: 0x%x, computed csum: 0x%x\n",
12206d7d651bSRahul Lakkireddy 		 config_name, finiver, cfcsum);
12216d7d651bSRahul Lakkireddy 	return 0;
12226d7d651bSRahul Lakkireddy }
12236d7d651bSRahul Lakkireddy 
configure_pcie_ext_tag(struct adapter * adapter)1224ee606d92SRahul Lakkireddy static void configure_pcie_ext_tag(struct adapter *adapter)
1225ee606d92SRahul Lakkireddy {
1226ee606d92SRahul Lakkireddy 	u16 v;
1227ee606d92SRahul Lakkireddy 	int pos = t4_os_find_pci_capability(adapter, PCI_CAP_ID_EXP);
1228ee606d92SRahul Lakkireddy 
1229ee606d92SRahul Lakkireddy 	if (!pos)
1230ee606d92SRahul Lakkireddy 		return;
1231ee606d92SRahul Lakkireddy 
1232ee606d92SRahul Lakkireddy 	if (pos > 0) {
1233ee606d92SRahul Lakkireddy 		t4_os_pci_read_cfg2(adapter, pos + PCI_EXP_DEVCTL, &v);
1234ee606d92SRahul Lakkireddy 		v |= PCI_EXP_DEVCTL_EXT_TAG;
1235ee606d92SRahul Lakkireddy 		t4_os_pci_write_cfg2(adapter, pos + PCI_EXP_DEVCTL, v);
1236ee606d92SRahul Lakkireddy 		if (is_t6(adapter->params.chip)) {
1237ee606d92SRahul Lakkireddy 			t4_set_reg_field(adapter, A_PCIE_CFG2,
1238ee606d92SRahul Lakkireddy 					 V_T6_TOTMAXTAG(M_T6_TOTMAXTAG),
1239ee606d92SRahul Lakkireddy 					 V_T6_TOTMAXTAG(7));
1240ee606d92SRahul Lakkireddy 			t4_set_reg_field(adapter, A_PCIE_CMD_CFG,
1241ee606d92SRahul Lakkireddy 					 V_T6_MINTAG(M_T6_MINTAG),
1242ee606d92SRahul Lakkireddy 					 V_T6_MINTAG(8));
1243ee606d92SRahul Lakkireddy 		} else {
1244ee606d92SRahul Lakkireddy 			t4_set_reg_field(adapter, A_PCIE_CFG2,
1245ee606d92SRahul Lakkireddy 					 V_TOTMAXTAG(M_TOTMAXTAG),
1246ee606d92SRahul Lakkireddy 					 V_TOTMAXTAG(3));
1247ee606d92SRahul Lakkireddy 			t4_set_reg_field(adapter, A_PCIE_CMD_CFG,
1248ee606d92SRahul Lakkireddy 					 V_MINTAG(M_MINTAG),
1249ee606d92SRahul Lakkireddy 					 V_MINTAG(8));
1250ee606d92SRahul Lakkireddy 		}
1251ee606d92SRahul Lakkireddy 	}
1252ee606d92SRahul Lakkireddy }
1253ee606d92SRahul Lakkireddy 
125487a3ae3eSRahul Lakkireddy /* Figure out how many Queue Sets we can support */
cxgbe_configure_max_ethqsets(struct adapter * adapter)1255b7fd9ea8SStephen Hemminger void cxgbe_configure_max_ethqsets(struct adapter *adapter)
125687a3ae3eSRahul Lakkireddy {
12577b3d5298SRahul Lakkireddy 	unsigned int ethqsets, reserved;
125887a3ae3eSRahul Lakkireddy 
12597b3d5298SRahul Lakkireddy 	/* We need to reserve an Ingress Queue for the Asynchronous Firmware
12607b3d5298SRahul Lakkireddy 	 * Event Queue and 1 Control Queue per port.
126187a3ae3eSRahul Lakkireddy 	 *
126287a3ae3eSRahul Lakkireddy 	 * For each Queue Set, we'll need the ability to allocate two Egress
126387a3ae3eSRahul Lakkireddy 	 * Contexts -- one for the Ingress Queue Free List and one for the TX
126487a3ae3eSRahul Lakkireddy 	 * Ethernet Queue.
126587a3ae3eSRahul Lakkireddy 	 */
12667b3d5298SRahul Lakkireddy 	reserved = max(adapter->params.nports, 1);
126787a3ae3eSRahul Lakkireddy 	if (is_pf4(adapter)) {
126887a3ae3eSRahul Lakkireddy 		struct pf_resources *pfres = &adapter->params.pfres;
126987a3ae3eSRahul Lakkireddy 
12707b3d5298SRahul Lakkireddy 		ethqsets = min(pfres->niqflint, pfres->nethctrl);
12717b3d5298SRahul Lakkireddy 		if (ethqsets > (pfres->neq / 2))
127287a3ae3eSRahul Lakkireddy 			ethqsets = pfres->neq / 2;
127387a3ae3eSRahul Lakkireddy 	} else {
127487a3ae3eSRahul Lakkireddy 		struct vf_resources *vfres = &adapter->params.vfres;
127587a3ae3eSRahul Lakkireddy 
12767b3d5298SRahul Lakkireddy 		ethqsets = min(vfres->niqflint, vfres->nethctrl);
12777b3d5298SRahul Lakkireddy 		if (ethqsets > (vfres->neq / 2))
127887a3ae3eSRahul Lakkireddy 			ethqsets = vfres->neq / 2;
127987a3ae3eSRahul Lakkireddy 	}
128087a3ae3eSRahul Lakkireddy 
12817b3d5298SRahul Lakkireddy 	ethqsets -= reserved;
128287a3ae3eSRahul Lakkireddy 	adapter->sge.max_ethqsets = ethqsets;
128387a3ae3eSRahul Lakkireddy }
128487a3ae3eSRahul Lakkireddy 
128583189849SRahul Lakkireddy /*
128683189849SRahul Lakkireddy  * Tweak configuration based on system architecture, etc.  Most of these have
128783189849SRahul Lakkireddy  * defaults assigned to them by Firmware Configuration Files (if we're using
128883189849SRahul Lakkireddy  * them) but need to be explicitly set if we're using hard-coded
128983189849SRahul Lakkireddy  * initialization. So these are essentially common tweaks/settings for
129083189849SRahul Lakkireddy  * Configuration Files and hard-coded initialization ...
129183189849SRahul Lakkireddy  */
adap_init0_tweaks(struct adapter * adapter)129283189849SRahul Lakkireddy static int adap_init0_tweaks(struct adapter *adapter)
129383189849SRahul Lakkireddy {
129483189849SRahul Lakkireddy 	u8 rx_dma_offset;
129583189849SRahul Lakkireddy 
129683189849SRahul Lakkireddy 	/*
129783189849SRahul Lakkireddy 	 * Fix up various Host-Dependent Parameters like Page Size, Cache
129883189849SRahul Lakkireddy 	 * Line Size, etc.  The firmware default is for a 4KB Page Size and
129983189849SRahul Lakkireddy 	 * 64B Cache Line Size ...
130083189849SRahul Lakkireddy 	 */
13011f8613f1SRahul Lakkireddy 	t4_fixup_host_params_compat(adapter, CXGBE_PAGE_SIZE, L1_CACHE_BYTES,
130283189849SRahul Lakkireddy 				    T5_LAST_REV);
130383189849SRahul Lakkireddy 
130483189849SRahul Lakkireddy 	/*
130583189849SRahul Lakkireddy 	 * Keep the chip default offset to deliver Ingress packets into our
130683189849SRahul Lakkireddy 	 * DMA buffers to zero
130783189849SRahul Lakkireddy 	 */
130883189849SRahul Lakkireddy 	rx_dma_offset = 0;
130983189849SRahul Lakkireddy 	t4_set_reg_field(adapter, A_SGE_CONTROL, V_PKTSHIFT(M_PKTSHIFT),
131083189849SRahul Lakkireddy 			 V_PKTSHIFT(rx_dma_offset));
131183189849SRahul Lakkireddy 
1312bf89cbedSRahul Lakkireddy 	t4_set_reg_field(adapter, A_SGE_FLM_CFG,
1313bf89cbedSRahul Lakkireddy 			 V_CREDITCNT(M_CREDITCNT) | M_CREDITCNTPACKING,
1314bf89cbedSRahul Lakkireddy 			 V_CREDITCNT(3) | V_CREDITCNTPACKING(1));
1315bf89cbedSRahul Lakkireddy 
13166c280962SRahul Lakkireddy 	t4_set_reg_field(adapter, A_SGE_INGRESS_RX_THRESHOLD,
13176c280962SRahul Lakkireddy 			 V_THRESHOLD_3(M_THRESHOLD_3), V_THRESHOLD_3(32U));
13186c280962SRahul Lakkireddy 
1319bf89cbedSRahul Lakkireddy 	t4_set_reg_field(adapter, A_SGE_CONTROL2, V_IDMAARBROUNDROBIN(1U),
1320bf89cbedSRahul Lakkireddy 			 V_IDMAARBROUNDROBIN(1U));
1321bf89cbedSRahul Lakkireddy 
132283189849SRahul Lakkireddy 	/*
132383189849SRahul Lakkireddy 	 * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
132483189849SRahul Lakkireddy 	 * adds the pseudo header itself.
132583189849SRahul Lakkireddy 	 */
132683189849SRahul Lakkireddy 	t4_tp_wr_bits_indirect(adapter, A_TP_INGRESS_CONFIG,
132783189849SRahul Lakkireddy 			       F_CSUM_HAS_PSEUDO_HDR, 0);
132883189849SRahul Lakkireddy 
132983189849SRahul Lakkireddy 	return 0;
133083189849SRahul Lakkireddy }
133183189849SRahul Lakkireddy 
133283189849SRahul Lakkireddy /*
133383189849SRahul Lakkireddy  * Attempt to initialize the adapter via a Firmware Configuration File.
133483189849SRahul Lakkireddy  */
adap_init0_config(struct adapter * adapter,int reset)133583189849SRahul Lakkireddy static int adap_init0_config(struct adapter *adapter, int reset)
133683189849SRahul Lakkireddy {
13376d7d651bSRahul Lakkireddy 	int ret;
133883189849SRahul Lakkireddy 
133983189849SRahul Lakkireddy 	/*
134083189849SRahul Lakkireddy 	 * Reset device if necessary.
134183189849SRahul Lakkireddy 	 */
134283189849SRahul Lakkireddy 	if (reset) {
134383189849SRahul Lakkireddy 		ret = t4_fw_reset(adapter, adapter->mbox,
134483189849SRahul Lakkireddy 				  F_PIORSTMODE | F_PIORST);
134583189849SRahul Lakkireddy 		if (ret < 0) {
134683189849SRahul Lakkireddy 			dev_warn(adapter, "Firmware reset failed, error %d\n",
134783189849SRahul Lakkireddy 				 -ret);
134883189849SRahul Lakkireddy 			goto bye;
134983189849SRahul Lakkireddy 		}
135083189849SRahul Lakkireddy 	}
135183189849SRahul Lakkireddy 
13526d7d651bSRahul Lakkireddy 	ret = cxgbe_load_fw_config(adapter);
135383189849SRahul Lakkireddy 	if (ret < 0)
135483189849SRahul Lakkireddy 		goto bye;
135583189849SRahul Lakkireddy 
135683189849SRahul Lakkireddy 	/*
135783189849SRahul Lakkireddy 	 * Tweak configuration based on system architecture, etc.
135883189849SRahul Lakkireddy 	 */
135983189849SRahul Lakkireddy 	ret = adap_init0_tweaks(adapter);
136083189849SRahul Lakkireddy 	if (ret < 0) {
136183189849SRahul Lakkireddy 		dev_warn(adapter, "Unable to do init0-tweaks %d\n", -ret);
136283189849SRahul Lakkireddy 		goto bye;
136383189849SRahul Lakkireddy 	}
136483189849SRahul Lakkireddy 
136583189849SRahul Lakkireddy 	/*
136683189849SRahul Lakkireddy 	 * And finally tell the firmware to initialize itself using the
136783189849SRahul Lakkireddy 	 * parameters from the Configuration File.
136883189849SRahul Lakkireddy 	 */
136983189849SRahul Lakkireddy 	ret = t4_fw_initialize(adapter, adapter->mbox);
137083189849SRahul Lakkireddy 	if (ret < 0) {
137183189849SRahul Lakkireddy 		dev_warn(adapter, "Initializing Firmware failed, error %d\n",
137283189849SRahul Lakkireddy 			 -ret);
137383189849SRahul Lakkireddy 		goto bye;
137483189849SRahul Lakkireddy 	}
137583189849SRahul Lakkireddy 
137683189849SRahul Lakkireddy 	return 0;
137783189849SRahul Lakkireddy 
137883189849SRahul Lakkireddy bye:
137983189849SRahul Lakkireddy 	dev_debug(adapter, "%s: returning ret = %d ..\n", __func__, ret);
138083189849SRahul Lakkireddy 	return ret;
138183189849SRahul Lakkireddy }
138283189849SRahul Lakkireddy 
adap_init0(struct adapter * adap)138383189849SRahul Lakkireddy static int adap_init0(struct adapter *adap)
138483189849SRahul Lakkireddy {
13856f2a064bSShagun Agrawal 	struct fw_caps_config_cmd caps_cmd;
138683189849SRahul Lakkireddy 	int ret = 0;
138783189849SRahul Lakkireddy 	u32 v, port_vec;
138883189849SRahul Lakkireddy 	enum dev_state state;
138983189849SRahul Lakkireddy 	u32 params[7], val[7];
139083189849SRahul Lakkireddy 	int reset = 1;
139183189849SRahul Lakkireddy 	int mbox = adap->mbox;
139283189849SRahul Lakkireddy 
139383189849SRahul Lakkireddy 	/*
139483189849SRahul Lakkireddy 	 * Contact FW, advertising Master capability.
139583189849SRahul Lakkireddy 	 */
139683189849SRahul Lakkireddy 	ret = t4_fw_hello(adap, adap->mbox, adap->mbox, MASTER_MAY, &state);
139783189849SRahul Lakkireddy 	if (ret < 0) {
139883189849SRahul Lakkireddy 		dev_err(adap, "%s: could not connect to FW, error %d\n",
139983189849SRahul Lakkireddy 			__func__, -ret);
140083189849SRahul Lakkireddy 		goto bye;
140183189849SRahul Lakkireddy 	}
140283189849SRahul Lakkireddy 
140383189849SRahul Lakkireddy 	CXGBE_DEBUG_MBOX(adap, "%s: adap->mbox = %d; ret = %d\n", __func__,
140483189849SRahul Lakkireddy 			 adap->mbox, ret);
140583189849SRahul Lakkireddy 
140683189849SRahul Lakkireddy 	if (ret == mbox)
140783189849SRahul Lakkireddy 		adap->flags |= MASTER_PF;
140883189849SRahul Lakkireddy 
140983189849SRahul Lakkireddy 	if (state == DEV_STATE_INIT) {
141083189849SRahul Lakkireddy 		/*
141183189849SRahul Lakkireddy 		 * Force halt and reset FW because a previous instance may have
141283189849SRahul Lakkireddy 		 * exited abnormally without properly shutting down
141383189849SRahul Lakkireddy 		 */
141483189849SRahul Lakkireddy 		ret = t4_fw_halt(adap, adap->mbox, reset);
141583189849SRahul Lakkireddy 		if (ret < 0) {
141683189849SRahul Lakkireddy 			dev_err(adap, "Failed to halt. Exit.\n");
141783189849SRahul Lakkireddy 			goto bye;
141883189849SRahul Lakkireddy 		}
141983189849SRahul Lakkireddy 
142083189849SRahul Lakkireddy 		ret = t4_fw_restart(adap, adap->mbox, reset);
142183189849SRahul Lakkireddy 		if (ret < 0) {
142283189849SRahul Lakkireddy 			dev_err(adap, "Failed to restart. Exit.\n");
142383189849SRahul Lakkireddy 			goto bye;
142483189849SRahul Lakkireddy 		}
1425efa8a43eSBruce Richardson 		state = (enum dev_state)((unsigned)state & ~DEV_STATE_INIT);
142683189849SRahul Lakkireddy 	}
142783189849SRahul Lakkireddy 
1428c962618cSRahul Lakkireddy 	t4_get_version_info(adap);
142983189849SRahul Lakkireddy 
143083189849SRahul Lakkireddy 	ret = t4_get_core_clock(adap, &adap->params.vpd);
143183189849SRahul Lakkireddy 	if (ret < 0) {
143283189849SRahul Lakkireddy 		dev_err(adap, "%s: could not get core clock, error %d\n",
143383189849SRahul Lakkireddy 			__func__, -ret);
143483189849SRahul Lakkireddy 		goto bye;
143583189849SRahul Lakkireddy 	}
143683189849SRahul Lakkireddy 
143783189849SRahul Lakkireddy 	/*
143883189849SRahul Lakkireddy 	 * If the firmware is initialized already (and we're not forcing a
143983189849SRahul Lakkireddy 	 * master initialization), note that we're living with existing
144083189849SRahul Lakkireddy 	 * adapter parameters.  Otherwise, it's time to try initializing the
144183189849SRahul Lakkireddy 	 * adapter ...
144283189849SRahul Lakkireddy 	 */
144383189849SRahul Lakkireddy 	if (state == DEV_STATE_INIT) {
144483189849SRahul Lakkireddy 		dev_info(adap, "Coming up as %s: Adapter already initialized\n",
144583189849SRahul Lakkireddy 			 adap->flags & MASTER_PF ? "MASTER" : "SLAVE");
144683189849SRahul Lakkireddy 	} else {
144783189849SRahul Lakkireddy 		dev_info(adap, "Coming up as MASTER: Initializing adapter\n");
144883189849SRahul Lakkireddy 
144983189849SRahul Lakkireddy 		ret = adap_init0_config(adap, reset);
145083189849SRahul Lakkireddy 		if (ret == -ENOENT) {
145183189849SRahul Lakkireddy 			dev_err(adap,
145283189849SRahul Lakkireddy 				"No Configuration File present on adapter. Using hard-wired configuration parameters.\n");
145383189849SRahul Lakkireddy 			goto bye;
145483189849SRahul Lakkireddy 		}
145583189849SRahul Lakkireddy 	}
145683189849SRahul Lakkireddy 	if (ret < 0) {
145783189849SRahul Lakkireddy 		dev_err(adap, "could not initialize adapter, error %d\n", -ret);
145883189849SRahul Lakkireddy 		goto bye;
145983189849SRahul Lakkireddy 	}
146083189849SRahul Lakkireddy 
146187a3ae3eSRahul Lakkireddy 	/* Now that we've successfully configured and initialized the adapter
146287a3ae3eSRahul Lakkireddy 	 * (or found it already initialized), we can ask the Firmware what
146387a3ae3eSRahul Lakkireddy 	 * resources it has provisioned for us.
146487a3ae3eSRahul Lakkireddy 	 */
146587a3ae3eSRahul Lakkireddy 	ret = t4_get_pfres(adap);
146687a3ae3eSRahul Lakkireddy 	if (ret) {
146787a3ae3eSRahul Lakkireddy 		dev_err(adap->pdev_dev,
146887a3ae3eSRahul Lakkireddy 			"Unable to retrieve resource provisioning info\n");
146987a3ae3eSRahul Lakkireddy 		goto bye;
147087a3ae3eSRahul Lakkireddy 	}
147187a3ae3eSRahul Lakkireddy 
14725eb55bf8SRahul Lakkireddy 	/* Find out what ports are available to us. */
14735eb55bf8SRahul Lakkireddy 	v = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
14745eb55bf8SRahul Lakkireddy 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_PORTVEC);
14755eb55bf8SRahul Lakkireddy 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, &v, &port_vec);
14765eb55bf8SRahul Lakkireddy 	if (ret < 0) {
14775eb55bf8SRahul Lakkireddy 		dev_err(adap, "%s: failure in t4_query_params; error = %d\n",
14785eb55bf8SRahul Lakkireddy 			__func__, ret);
14795eb55bf8SRahul Lakkireddy 		goto bye;
14805eb55bf8SRahul Lakkireddy 	}
14815eb55bf8SRahul Lakkireddy 
14825eb55bf8SRahul Lakkireddy 	adap->params.nports = hweight32(port_vec);
14835eb55bf8SRahul Lakkireddy 	adap->params.portvec = port_vec;
14845eb55bf8SRahul Lakkireddy 
14855eb55bf8SRahul Lakkireddy 	dev_debug(adap, "%s: adap->params.nports = %u\n", __func__,
14865eb55bf8SRahul Lakkireddy 		  adap->params.nports);
14875eb55bf8SRahul Lakkireddy 
148883189849SRahul Lakkireddy 	/*
148983189849SRahul Lakkireddy 	 * Give the SGE code a chance to pull in anything that it needs ...
149083189849SRahul Lakkireddy 	 * Note that this must be called after we retrieve our VPD parameters
149183189849SRahul Lakkireddy 	 * in order to know how to convert core ticks to seconds, etc.
149283189849SRahul Lakkireddy 	 */
149383189849SRahul Lakkireddy 	ret = t4_sge_init(adap);
149483189849SRahul Lakkireddy 	if (ret < 0) {
149583189849SRahul Lakkireddy 		dev_err(adap, "t4_sge_init failed with error %d\n",
149683189849SRahul Lakkireddy 			-ret);
149783189849SRahul Lakkireddy 		goto bye;
149883189849SRahul Lakkireddy 	}
149983189849SRahul Lakkireddy 
150083189849SRahul Lakkireddy 	/*
150183189849SRahul Lakkireddy 	 * Grab some of our basic fundamental operating parameters.
150283189849SRahul Lakkireddy 	 */
150351abd7b2SRahul Lakkireddy 	params[0] = CXGBE_FW_PARAM_PFVF(L2T_START);
150451abd7b2SRahul Lakkireddy 	params[1] = CXGBE_FW_PARAM_PFVF(L2T_END);
150551abd7b2SRahul Lakkireddy 	params[2] = CXGBE_FW_PARAM_PFVF(FILTER_START);
150651abd7b2SRahul Lakkireddy 	params[3] = CXGBE_FW_PARAM_PFVF(FILTER_END);
150723af667fSShagun Agrawal 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 4, params, val);
15086f2a064bSShagun Agrawal 	if (ret < 0)
15096f2a064bSShagun Agrawal 		goto bye;
151023af667fSShagun Agrawal 	adap->l2t_start = val[0];
151123af667fSShagun Agrawal 	adap->l2t_end = val[1];
151223af667fSShagun Agrawal 	adap->tids.ftid_base = val[2];
151323af667fSShagun Agrawal 	adap->tids.nftids = val[3] - val[2] + 1;
15146f2a064bSShagun Agrawal 
151551abd7b2SRahul Lakkireddy 	params[0] = CXGBE_FW_PARAM_PFVF(CLIP_START);
151651abd7b2SRahul Lakkireddy 	params[1] = CXGBE_FW_PARAM_PFVF(CLIP_END);
15173f2c1e20SShagun Agrawal 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
15183f2c1e20SShagun Agrawal 	if (ret < 0)
15193f2c1e20SShagun Agrawal 		goto bye;
15203f2c1e20SShagun Agrawal 	adap->clipt_start = val[0];
15213f2c1e20SShagun Agrawal 	adap->clipt_end = val[1];
15223f2c1e20SShagun Agrawal 
15236f2a064bSShagun Agrawal 	/*
15246f2a064bSShagun Agrawal 	 * Get device capabilities so we can determine what resources we need
15256f2a064bSShagun Agrawal 	 * to manage.
15266f2a064bSShagun Agrawal 	 */
15276f2a064bSShagun Agrawal 	memset(&caps_cmd, 0, sizeof(caps_cmd));
15286f2a064bSShagun Agrawal 	caps_cmd.op_to_write = htonl(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
15296f2a064bSShagun Agrawal 				     F_FW_CMD_REQUEST | F_FW_CMD_READ);
15306f2a064bSShagun Agrawal 	caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
15316f2a064bSShagun Agrawal 	ret = t4_wr_mbox(adap, adap->mbox, &caps_cmd, sizeof(caps_cmd),
15326f2a064bSShagun Agrawal 			 &caps_cmd);
15336f2a064bSShagun Agrawal 	if (ret < 0)
15346f2a064bSShagun Agrawal 		goto bye;
15356f2a064bSShagun Agrawal 
15363a381a41SShagun Agrawal 	if ((caps_cmd.niccaps & cpu_to_be16(FW_CAPS_CONFIG_NIC_HASHFILTER)) &&
15373a381a41SShagun Agrawal 	    is_t6(adap->params.chip)) {
153871e9b334SRahul Lakkireddy 		if (cxgbe_init_hash_filter(adap) < 0)
15393a381a41SShagun Agrawal 			goto bye;
15403a381a41SShagun Agrawal 	}
15413a381a41SShagun Agrawal 
154248f523f6SRahul Lakkireddy 	/* See if FW supports FW_FILTER2 work request */
154348f523f6SRahul Lakkireddy 	if (is_t4(adap->params.chip)) {
154448f523f6SRahul Lakkireddy 		adap->params.filter2_wr_support = 0;
154548f523f6SRahul Lakkireddy 	} else {
154651abd7b2SRahul Lakkireddy 		params[0] = CXGBE_FW_PARAM_DEV(FILTER2_WR);
154748f523f6SRahul Lakkireddy 		ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
154848f523f6SRahul Lakkireddy 				      1, params, val);
154948f523f6SRahul Lakkireddy 		adap->params.filter2_wr_support = (ret == 0 && val[0] != 0);
155048f523f6SRahul Lakkireddy 	}
155148f523f6SRahul Lakkireddy 
155224c1d49aSKarra Satwik 	/* Check if FW supports returning vin.
155324c1d49aSKarra Satwik 	 * If this is not supported, driver will interpret
155424c1d49aSKarra Satwik 	 * these values from viid.
155524c1d49aSKarra Satwik 	 */
155624c1d49aSKarra Satwik 	params[0] = CXGBE_FW_PARAM_DEV(OPAQUE_VIID_SMT_EXTN);
155724c1d49aSKarra Satwik 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
155824c1d49aSKarra Satwik 			      1, params, val);
155924c1d49aSKarra Satwik 	adap->params.viid_smt_extn_support = (ret == 0 && val[0] != 0);
156024c1d49aSKarra Satwik 
15616f2a064bSShagun Agrawal 	/* query tid-related parameters */
156251abd7b2SRahul Lakkireddy 	params[0] = CXGBE_FW_PARAM_DEV(NTID);
15636f2a064bSShagun Agrawal 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
15646f2a064bSShagun Agrawal 			      params, val);
15656f2a064bSShagun Agrawal 	if (ret < 0)
15666f2a064bSShagun Agrawal 		goto bye;
15676f2a064bSShagun Agrawal 	adap->tids.ntids = val[0];
15683a381a41SShagun Agrawal 	adap->tids.natids = min(adap->tids.ntids / 2, MAX_ATIDS);
15696f2a064bSShagun Agrawal 
157083189849SRahul Lakkireddy 	/* If we're running on newer firmware, let it know that we're
157183189849SRahul Lakkireddy 	 * prepared to deal with encapsulated CPL messages.  Older
157283189849SRahul Lakkireddy 	 * firmware won't understand this and we'll just get
157383189849SRahul Lakkireddy 	 * unencapsulated messages ...
157483189849SRahul Lakkireddy 	 */
157551abd7b2SRahul Lakkireddy 	params[0] = CXGBE_FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
157683189849SRahul Lakkireddy 	val[0] = 1;
157783189849SRahul Lakkireddy 	(void)t4_set_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
157883189849SRahul Lakkireddy 
157983189849SRahul Lakkireddy 	/*
158083189849SRahul Lakkireddy 	 * Find out whether we're allowed to use the T5+ ULPTX MEMWRITE DSGL
158183189849SRahul Lakkireddy 	 * capability.  Earlier versions of the firmware didn't have the
158283189849SRahul Lakkireddy 	 * ULPTX_MEMWRITE_DSGL so we'll interpret a query failure as no
158383189849SRahul Lakkireddy 	 * permission to use ULPTX MEMWRITE DSGL.
158483189849SRahul Lakkireddy 	 */
158583189849SRahul Lakkireddy 	if (is_t4(adap->params.chip)) {
158683189849SRahul Lakkireddy 		adap->params.ulptx_memwrite_dsgl = false;
158783189849SRahul Lakkireddy 	} else {
158851abd7b2SRahul Lakkireddy 		params[0] = CXGBE_FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
158983189849SRahul Lakkireddy 		ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
159083189849SRahul Lakkireddy 				      1, params, val);
159183189849SRahul Lakkireddy 		adap->params.ulptx_memwrite_dsgl = (ret == 0 && val[0] != 0);
159283189849SRahul Lakkireddy 	}
159383189849SRahul Lakkireddy 
159451abd7b2SRahul Lakkireddy 	/* Query for max number of packets that can be coalesced for Tx */
159551abd7b2SRahul Lakkireddy 	params[0] = CXGBE_FW_PARAM_PFVF(MAX_PKTS_PER_ETH_TX_PKTS_WR);
159651abd7b2SRahul Lakkireddy 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
159751abd7b2SRahul Lakkireddy 	if (!ret && val[0] > 0)
159851abd7b2SRahul Lakkireddy 		adap->params.max_tx_coalesce_num = val[0];
159951abd7b2SRahul Lakkireddy 	else
160051abd7b2SRahul Lakkireddy 		adap->params.max_tx_coalesce_num = ETH_COALESCE_PKT_NUM;
160151abd7b2SRahul Lakkireddy 
160223d5fee3SRahul Lakkireddy 	params[0] = CXGBE_FW_PARAM_DEV(VI_ENABLE_INGRESS_AFTER_LINKUP);
160323d5fee3SRahul Lakkireddy 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
160423d5fee3SRahul Lakkireddy 	adap->params.vi_enable_rx = (ret == 0 && val[0] != 0);
160523d5fee3SRahul Lakkireddy 
1606422d7823SRahul Lakkireddy 	/* Read the RAW MPS entries. In T6, the last 2 TCAM entries
1607422d7823SRahul Lakkireddy 	 * are reserved for RAW MAC addresses (rawf = 2, one per port).
1608422d7823SRahul Lakkireddy 	 */
1609422d7823SRahul Lakkireddy 	if (CHELSIO_CHIP_VERSION(adap->params.chip) > CHELSIO_T5) {
1610422d7823SRahul Lakkireddy 		params[0] = CXGBE_FW_PARAM_PFVF(RAWF_START);
1611422d7823SRahul Lakkireddy 		params[1] = CXGBE_FW_PARAM_PFVF(RAWF_END);
1612422d7823SRahul Lakkireddy 		ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
1613422d7823SRahul Lakkireddy 				      params, val);
1614422d7823SRahul Lakkireddy 		if (ret == 0) {
1615422d7823SRahul Lakkireddy 			adap->params.rawf_start = val[0];
1616422d7823SRahul Lakkireddy 			adap->params.rawf_size = val[1] - val[0] + 1;
1617422d7823SRahul Lakkireddy 		}
1618422d7823SRahul Lakkireddy 	}
1619422d7823SRahul Lakkireddy 
162083189849SRahul Lakkireddy 	/*
162183189849SRahul Lakkireddy 	 * The MTU/MSS Table is initialized by now, so load their values.  If
162283189849SRahul Lakkireddy 	 * we're initializing the adapter, then we'll make any modifications
162383189849SRahul Lakkireddy 	 * we want to the MTU/MSS Table and also initialize the congestion
162483189849SRahul Lakkireddy 	 * parameters.
162583189849SRahul Lakkireddy 	 */
162683189849SRahul Lakkireddy 	t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
162783189849SRahul Lakkireddy 	if (state != DEV_STATE_INIT) {
162883189849SRahul Lakkireddy 		int i;
162983189849SRahul Lakkireddy 
163083189849SRahul Lakkireddy 		/*
163183189849SRahul Lakkireddy 		 * The default MTU Table contains values 1492 and 1500.
163283189849SRahul Lakkireddy 		 * However, for TCP, it's better to have two values which are
163383189849SRahul Lakkireddy 		 * a multiple of 8 +/- 4 bytes apart near this popular MTU.
163483189849SRahul Lakkireddy 		 * This allows us to have a TCP Data Payload which is a
163583189849SRahul Lakkireddy 		 * multiple of 8 regardless of what combination of TCP Options
163683189849SRahul Lakkireddy 		 * are in use (always a multiple of 4 bytes) which is
163783189849SRahul Lakkireddy 		 * important for performance reasons.  For instance, if no
163883189849SRahul Lakkireddy 		 * options are in use, then we have a 20-byte IP header and a
163983189849SRahul Lakkireddy 		 * 20-byte TCP header.  In this case, a 1500-byte MSS would
164083189849SRahul Lakkireddy 		 * result in a TCP Data Payload of 1500 - 40 == 1460 bytes
164183189849SRahul Lakkireddy 		 * which is not a multiple of 8.  So using an MSS of 1488 in
164283189849SRahul Lakkireddy 		 * this case results in a TCP Data Payload of 1448 bytes which
164383189849SRahul Lakkireddy 		 * is a multiple of 8.  On the other hand, if 12-byte TCP Time
164483189849SRahul Lakkireddy 		 * Stamps have been negotiated, then an MTU of 1500 bytes
164583189849SRahul Lakkireddy 		 * results in a TCP Data Payload of 1448 bytes which, as
164683189849SRahul Lakkireddy 		 * above, is a multiple of 8 bytes ...
164783189849SRahul Lakkireddy 		 */
164883189849SRahul Lakkireddy 		for (i = 0; i < NMTUS; i++)
164983189849SRahul Lakkireddy 			if (adap->params.mtus[i] == 1492) {
165083189849SRahul Lakkireddy 				adap->params.mtus[i] = 1488;
165183189849SRahul Lakkireddy 				break;
165283189849SRahul Lakkireddy 			}
165383189849SRahul Lakkireddy 
165483189849SRahul Lakkireddy 		t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
165583189849SRahul Lakkireddy 			     adap->params.b_wnd);
165683189849SRahul Lakkireddy 	}
165783189849SRahul Lakkireddy 	t4_init_sge_params(adap);
1658536db938SKarra Satwik 	ret = configure_filter_mode_mask(adap);
1659536db938SKarra Satwik 	if (ret < 0)
1660536db938SKarra Satwik 		goto bye;
166183189849SRahul Lakkireddy 	t4_init_tp_params(adap);
1662ee606d92SRahul Lakkireddy 	configure_pcie_ext_tag(adap);
1663cda260a4SShagun Agrawal 	configure_vlan_types(adap);
1664b7fd9ea8SStephen Hemminger 	cxgbe_configure_max_ethqsets(adap);
166583189849SRahul Lakkireddy 
166683189849SRahul Lakkireddy 	adap->params.drv_memwin = MEMWIN_NIC;
166783189849SRahul Lakkireddy 	adap->flags |= FW_OK;
166883189849SRahul Lakkireddy 	dev_debug(adap, "%s: returning zero..\n", __func__);
166983189849SRahul Lakkireddy 	return 0;
167083189849SRahul Lakkireddy 
167183189849SRahul Lakkireddy 	/*
167283189849SRahul Lakkireddy 	 * Something bad happened.  If a command timed out or failed with EIO
167383189849SRahul Lakkireddy 	 * FW does not operate within its spec or something catastrophic
167483189849SRahul Lakkireddy 	 * happened to HW/FW, stop issuing commands.
167583189849SRahul Lakkireddy 	 */
167683189849SRahul Lakkireddy bye:
167783189849SRahul Lakkireddy 	if (ret != -ETIMEDOUT && ret != -EIO)
167883189849SRahul Lakkireddy 		t4_fw_bye(adap, adap->mbox);
167983189849SRahul Lakkireddy 	return ret;
168083189849SRahul Lakkireddy }
168183189849SRahul Lakkireddy 
168283189849SRahul Lakkireddy /**
168383189849SRahul Lakkireddy  * t4_os_portmod_changed - handle port module changes
168483189849SRahul Lakkireddy  * @adap: the adapter associated with the module change
168583189849SRahul Lakkireddy  * @port_id: the port index whose module status has changed
168683189849SRahul Lakkireddy  *
168783189849SRahul Lakkireddy  * This is the OS-dependent handler for port module changes.  It is
168883189849SRahul Lakkireddy  * invoked when a port module is removed or inserted for any OS-specific
168983189849SRahul Lakkireddy  * processing.
169083189849SRahul Lakkireddy  */
t4_os_portmod_changed(const struct adapter * adap,int port_id)169183189849SRahul Lakkireddy void t4_os_portmod_changed(const struct adapter *adap, int port_id)
169283189849SRahul Lakkireddy {
169383189849SRahul Lakkireddy 	static const char * const mod_str[] = {
169483189849SRahul Lakkireddy 		NULL, "LR", "SR", "ER", "passive DA", "active DA", "LRM"
169583189849SRahul Lakkireddy 	};
169683189849SRahul Lakkireddy 
16972195df6dSRahul Lakkireddy 	const struct port_info *pi = adap2pinfo(adap, port_id);
169883189849SRahul Lakkireddy 
1699a83041b1SKarra Satwik 	if (pi->link_cfg.mod_type == FW_PORT_MOD_TYPE_NONE)
170083189849SRahul Lakkireddy 		dev_info(adap, "Port%d: port module unplugged\n", pi->port_id);
1701a83041b1SKarra Satwik 	else if (pi->link_cfg.mod_type < ARRAY_SIZE(mod_str))
170283189849SRahul Lakkireddy 		dev_info(adap, "Port%d: %s port module inserted\n", pi->port_id,
1703a83041b1SKarra Satwik 			 mod_str[pi->link_cfg.mod_type]);
1704a83041b1SKarra Satwik 	else if (pi->link_cfg.mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
17059da2a694SRahul Lakkireddy 		dev_info(adap, "Port%d: unsupported port module inserted\n",
170683189849SRahul Lakkireddy 			 pi->port_id);
1707a83041b1SKarra Satwik 	else if (pi->link_cfg.mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
17089da2a694SRahul Lakkireddy 		dev_info(adap, "Port%d: unknown port module inserted\n",
170983189849SRahul Lakkireddy 			 pi->port_id);
1710a83041b1SKarra Satwik 	else if (pi->link_cfg.mod_type == FW_PORT_MOD_TYPE_ERROR)
171183189849SRahul Lakkireddy 		dev_info(adap, "Port%d: transceiver module error\n",
171283189849SRahul Lakkireddy 			 pi->port_id);
171383189849SRahul Lakkireddy 	else
171483189849SRahul Lakkireddy 		dev_info(adap, "Port%d: unknown module type %d inserted\n",
1715a83041b1SKarra Satwik 			 pi->port_id, pi->link_cfg.mod_type);
171683189849SRahul Lakkireddy }
171783189849SRahul Lakkireddy 
t4_os_link_changed(struct adapter * adap,int port_id)171823d5fee3SRahul Lakkireddy void t4_os_link_changed(struct adapter *adap, int port_id)
171923d5fee3SRahul Lakkireddy {
172023d5fee3SRahul Lakkireddy 	struct port_info *pi = adap2pinfo(adap, port_id);
172123d5fee3SRahul Lakkireddy 
172223d5fee3SRahul Lakkireddy 	/* If link status has not changed or if firmware doesn't
172323d5fee3SRahul Lakkireddy 	 * support enabling/disabling VI's Rx path during runtime,
172423d5fee3SRahul Lakkireddy 	 * then return.
172523d5fee3SRahul Lakkireddy 	 */
172623d5fee3SRahul Lakkireddy 	if (adap->params.vi_enable_rx == 0 ||
172723d5fee3SRahul Lakkireddy 	    pi->vi_en_rx == pi->link_cfg.link_ok)
172823d5fee3SRahul Lakkireddy 		return;
172923d5fee3SRahul Lakkireddy 
173023d5fee3SRahul Lakkireddy 	/* Don't enable VI Rx path, if link has been administratively
173123d5fee3SRahul Lakkireddy 	 * turned off.
173223d5fee3SRahul Lakkireddy 	 */
173323d5fee3SRahul Lakkireddy 	if (pi->vi_en_tx == 0 && pi->vi_en_rx == 0)
173423d5fee3SRahul Lakkireddy 		return;
173523d5fee3SRahul Lakkireddy 
173623d5fee3SRahul Lakkireddy 	/* When link goes down, disable the port's Rx path to drop
173723d5fee3SRahul Lakkireddy 	 * Rx traffic closer to the wire, instead of processing it
173823d5fee3SRahul Lakkireddy 	 * further in the Rx pipeline. The Rx path will be re-enabled
173923d5fee3SRahul Lakkireddy 	 * once the link up message comes in firmware event queue.
174023d5fee3SRahul Lakkireddy 	 */
174123d5fee3SRahul Lakkireddy 	pi->vi_en_rx = pi->link_cfg.link_ok;
174223d5fee3SRahul Lakkireddy 	t4_enable_vi(adap, adap->mbox, pi->viid, pi->vi_en_rx, pi->vi_en_tx);
174323d5fee3SRahul Lakkireddy }
174423d5fee3SRahul Lakkireddy 
cxgbe_force_linkup(struct adapter * adap)1745b7fd9ea8SStephen Hemminger bool cxgbe_force_linkup(struct adapter *adap)
1746f5b3c7b2SShagun Agrawal {
1747f5b3c7b2SShagun Agrawal 	if (is_pf4(adap))
1748f5b3c7b2SShagun Agrawal 		return false;	/* force_linkup not required for pf driver */
1749dd7c9f12SRahul Lakkireddy 
1750dd7c9f12SRahul Lakkireddy 	return adap->devargs.force_link_up;
1751f5b3c7b2SShagun Agrawal }
1752f5b3c7b2SShagun Agrawal 
175392c8a632SRahul Lakkireddy /**
17540462d115SRahul Lakkireddy  * link_start - enable a port
17550462d115SRahul Lakkireddy  * @dev: the port to enable
17560462d115SRahul Lakkireddy  *
17570462d115SRahul Lakkireddy  * Performs the MAC and PHY actions needed to enable a port.
17580462d115SRahul Lakkireddy  */
cxgbe_link_start(struct port_info * pi)1759b7fd9ea8SStephen Hemminger int cxgbe_link_start(struct port_info *pi)
17600462d115SRahul Lakkireddy {
17610462d115SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
17626507fb6fSRahul Lakkireddy 	u64 conf_offloads;
17635a9e303aSRahul Lakkireddy 	unsigned int mtu;
17646507fb6fSRahul Lakkireddy 	int ret;
17655a9e303aSRahul Lakkireddy 
17661bb4a528SFerruh Yigit 	mtu = pi->eth_dev->data->mtu;
17670462d115SRahul Lakkireddy 
17686507fb6fSRahul Lakkireddy 	conf_offloads = pi->eth_dev->data->dev_conf.rxmode.offloads;
17696507fb6fSRahul Lakkireddy 
17700462d115SRahul Lakkireddy 	/*
17710462d115SRahul Lakkireddy 	 * We do not set address filters and promiscuity here, the stack does
17720462d115SRahul Lakkireddy 	 * that step explicitly.
17730462d115SRahul Lakkireddy 	 */
17746507fb6fSRahul Lakkireddy 	ret = t4_set_rxmode(adapter, adapter->mbox, pi->viid, mtu, -1, -1, -1,
1775295968d1SFerruh Yigit 			    !!(conf_offloads & RTE_ETH_RX_OFFLOAD_VLAN_STRIP),
17766507fb6fSRahul Lakkireddy 			    true);
17770462d115SRahul Lakkireddy 	if (ret == 0) {
1778fefee7a6SShagun Agrawal 		ret = cxgbe_mpstcam_modify(pi, (int)pi->xact_addr_filt,
1779fefee7a6SShagun Agrawal 				(u8 *)&pi->eth_dev->data->mac_addrs[0]);
17800462d115SRahul Lakkireddy 		if (ret >= 0) {
17810462d115SRahul Lakkireddy 			pi->xact_addr_filt = ret;
17820462d115SRahul Lakkireddy 			ret = 0;
17830462d115SRahul Lakkireddy 		}
17840462d115SRahul Lakkireddy 	}
17855e80364aSKumar Sanghvi 	if (ret == 0 && is_pf4(adapter))
1786a83041b1SKarra Satwik 		ret = t4_link_l1cfg(pi, pi->link_cfg.admin_caps);
17870462d115SRahul Lakkireddy 	if (ret == 0) {
178823d5fee3SRahul Lakkireddy 		/* Disable VI Rx until link up message is received in
178923d5fee3SRahul Lakkireddy 		 * firmware event queue, if firmware supports enabling/
179023d5fee3SRahul Lakkireddy 		 * disabling VI Rx at runtime.
17910462d115SRahul Lakkireddy 		 */
179223d5fee3SRahul Lakkireddy 		pi->vi_en_rx = adapter->params.vi_enable_rx ? 0 : 1;
179323d5fee3SRahul Lakkireddy 		pi->vi_en_tx = 1;
17940462d115SRahul Lakkireddy 		ret = t4_enable_vi_params(adapter, adapter->mbox, pi->viid,
179523d5fee3SRahul Lakkireddy 					  pi->vi_en_rx, pi->vi_en_tx, false);
17960462d115SRahul Lakkireddy 	}
1797f5b3c7b2SShagun Agrawal 
1798b7fd9ea8SStephen Hemminger 	if (ret == 0 && cxgbe_force_linkup(adapter))
1799295968d1SFerruh Yigit 		pi->eth_dev->data->dev_link.link_status = RTE_ETH_LINK_UP;
18000462d115SRahul Lakkireddy 	return ret;
18010462d115SRahul Lakkireddy }
18020462d115SRahul Lakkireddy 
18030462d115SRahul Lakkireddy /**
180408e21af9SKumar Sanghvi  * cxgbe_write_rss_conf - flash the RSS configuration for a given port
180508e21af9SKumar Sanghvi  * @pi: the port
180608e21af9SKumar Sanghvi  * @rss_hf: Hash configuration to apply
180708e21af9SKumar Sanghvi  */
cxgbe_write_rss_conf(const struct port_info * pi,uint64_t rss_hf)180808e21af9SKumar Sanghvi int cxgbe_write_rss_conf(const struct port_info *pi, uint64_t rss_hf)
180908e21af9SKumar Sanghvi {
181008e21af9SKumar Sanghvi 	struct adapter *adapter = pi->adapter;
181108e21af9SKumar Sanghvi 	const struct sge_eth_rxq *rxq;
181208e21af9SKumar Sanghvi 	u64 flags = 0;
181308e21af9SKumar Sanghvi 	u16 rss;
181408e21af9SKumar Sanghvi 	int err;
181508e21af9SKumar Sanghvi 
181608e21af9SKumar Sanghvi 	/*  Should never be called before setting up sge eth rx queues */
181708e21af9SKumar Sanghvi 	if (!(adapter->flags & FULL_INIT_DONE)) {
181808e21af9SKumar Sanghvi 		dev_err(adap, "%s No RXQs available on port %d\n",
181908e21af9SKumar Sanghvi 			__func__, pi->port_id);
182008e21af9SKumar Sanghvi 		return -EINVAL;
182108e21af9SKumar Sanghvi 	}
182208e21af9SKumar Sanghvi 
182308e21af9SKumar Sanghvi 	/* Don't allow unsupported hash functions */
182408e21af9SKumar Sanghvi 	if (rss_hf & ~CXGBE_RSS_HF_ALL)
182508e21af9SKumar Sanghvi 		return -EINVAL;
182608e21af9SKumar Sanghvi 
1827d97aa415SRahul Lakkireddy 	if (rss_hf & CXGBE_RSS_HF_IPV4_MASK)
182808e21af9SKumar Sanghvi 		flags |= F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN;
182908e21af9SKumar Sanghvi 
1830295968d1SFerruh Yigit 	if (rss_hf & RTE_ETH_RSS_NONFRAG_IPV4_TCP)
183108e21af9SKumar Sanghvi 		flags |= F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
183208e21af9SKumar Sanghvi 
1833295968d1SFerruh Yigit 	if (rss_hf & RTE_ETH_RSS_NONFRAG_IPV4_UDP)
183408e21af9SKumar Sanghvi 		flags |= F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
183508e21af9SKumar Sanghvi 			 F_FW_RSS_VI_CONFIG_CMD_UDPEN;
183608e21af9SKumar Sanghvi 
1837d97aa415SRahul Lakkireddy 	if (rss_hf & CXGBE_RSS_HF_IPV6_MASK)
183808e21af9SKumar Sanghvi 		flags |= F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN;
183908e21af9SKumar Sanghvi 
1840d97aa415SRahul Lakkireddy 	if (rss_hf & CXGBE_RSS_HF_TCP_IPV6_MASK)
1841d97aa415SRahul Lakkireddy 		flags |= F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN |
1842d97aa415SRahul Lakkireddy 			 F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
184308e21af9SKumar Sanghvi 
1844d97aa415SRahul Lakkireddy 	if (rss_hf & CXGBE_RSS_HF_UDP_IPV6_MASK)
1845d97aa415SRahul Lakkireddy 		flags |= F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN |
1846d97aa415SRahul Lakkireddy 			 F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN |
184708e21af9SKumar Sanghvi 			 F_FW_RSS_VI_CONFIG_CMD_UDPEN;
184808e21af9SKumar Sanghvi 
18497b3d5298SRahul Lakkireddy 	rxq = &adapter->sge.ethrxq[pi->first_rxqset];
185008e21af9SKumar Sanghvi 	rss = rxq[0].rspq.abs_id;
185108e21af9SKumar Sanghvi 
185208e21af9SKumar Sanghvi 	/* If Tunnel All Lookup isn't specified in the global RSS
185308e21af9SKumar Sanghvi 	 * Configuration, then we need to specify a default Ingress
185408e21af9SKumar Sanghvi 	 * Queue for any ingress packets which aren't hashed.  We'll
185508e21af9SKumar Sanghvi 	 * use our first ingress queue ...
185608e21af9SKumar Sanghvi 	 */
185708e21af9SKumar Sanghvi 	err = t4_config_vi_rss(adapter, adapter->mbox, pi->viid,
185808e21af9SKumar Sanghvi 			       flags, rss);
185908e21af9SKumar Sanghvi 	return err;
186008e21af9SKumar Sanghvi }
186108e21af9SKumar Sanghvi 
186208e21af9SKumar Sanghvi /**
186308e21af9SKumar Sanghvi  * cxgbe_write_rss - write the RSS table for a given port
186492c8a632SRahul Lakkireddy  * @pi: the port
186592c8a632SRahul Lakkireddy  * @queues: array of queue indices for RSS
186692c8a632SRahul Lakkireddy  *
186792c8a632SRahul Lakkireddy  * Sets up the portion of the HW RSS table for the port's VI to distribute
186892c8a632SRahul Lakkireddy  * packets to the Rx queues in @queues.
186992c8a632SRahul Lakkireddy  */
cxgbe_write_rss(const struct port_info * pi,const u16 * queues)187008e21af9SKumar Sanghvi int cxgbe_write_rss(const struct port_info *pi, const u16 *queues)
187192c8a632SRahul Lakkireddy {
187292c8a632SRahul Lakkireddy 	u16 *rss;
187392c8a632SRahul Lakkireddy 	int i, err;
187492c8a632SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
187592c8a632SRahul Lakkireddy 	const struct sge_eth_rxq *rxq;
187692c8a632SRahul Lakkireddy 
187792c8a632SRahul Lakkireddy 	/*  Should never be called before setting up sge eth rx queues */
187892c8a632SRahul Lakkireddy 	BUG_ON(!(adapter->flags & FULL_INIT_DONE));
187992c8a632SRahul Lakkireddy 
18807b3d5298SRahul Lakkireddy 	rxq = &adapter->sge.ethrxq[pi->first_rxqset];
188192c8a632SRahul Lakkireddy 	rss = rte_zmalloc(NULL, pi->rss_size * sizeof(u16), 0);
188292c8a632SRahul Lakkireddy 	if (!rss)
188392c8a632SRahul Lakkireddy 		return -ENOMEM;
188492c8a632SRahul Lakkireddy 
188592c8a632SRahul Lakkireddy 	/* map the queue indices to queue ids */
188692c8a632SRahul Lakkireddy 	for (i = 0; i < pi->rss_size; i++, queues++)
188792c8a632SRahul Lakkireddy 		rss[i] = rxq[*queues].rspq.abs_id;
188892c8a632SRahul Lakkireddy 
188992c8a632SRahul Lakkireddy 	err = t4_config_rss_range(adapter, adapter->pf, pi->viid, 0,
189092c8a632SRahul Lakkireddy 				  pi->rss_size, rss, pi->rss_size);
189192c8a632SRahul Lakkireddy 	rte_free(rss);
189292c8a632SRahul Lakkireddy 	return err;
189392c8a632SRahul Lakkireddy }
189492c8a632SRahul Lakkireddy 
189592c8a632SRahul Lakkireddy /**
189692c8a632SRahul Lakkireddy  * setup_rss - configure RSS
189792c8a632SRahul Lakkireddy  * @adapter: the adapter
189892c8a632SRahul Lakkireddy  *
189992c8a632SRahul Lakkireddy  * Sets up RSS to distribute packets to multiple receive queues.  We
190092c8a632SRahul Lakkireddy  * configure the RSS CPU lookup table to distribute to the number of HW
190192c8a632SRahul Lakkireddy  * receive queues, and the response queue lookup table to narrow that
190292c8a632SRahul Lakkireddy  * down to the response queues actually configured for each port.
190392c8a632SRahul Lakkireddy  * We always configure the RSS mapping for all ports since the mapping
190492c8a632SRahul Lakkireddy  * table has plenty of entries.
190592c8a632SRahul Lakkireddy  */
cxgbe_setup_rss(struct port_info * pi)1906b7fd9ea8SStephen Hemminger int cxgbe_setup_rss(struct port_info *pi)
190792c8a632SRahul Lakkireddy {
190892c8a632SRahul Lakkireddy 	int j, err;
190992c8a632SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
191092c8a632SRahul Lakkireddy 
191192c8a632SRahul Lakkireddy 	dev_debug(adapter, "%s:  pi->rss_size = %u; pi->n_rx_qsets = %u\n",
191292c8a632SRahul Lakkireddy 		  __func__, pi->rss_size, pi->n_rx_qsets);
191392c8a632SRahul Lakkireddy 
19141039ee1cSEmmanuel Roullit 	if (!(pi->flags & PORT_RSS_DONE)) {
191592c8a632SRahul Lakkireddy 		if (adapter->flags & FULL_INIT_DONE) {
191692c8a632SRahul Lakkireddy 			/* Fill default values with equal distribution */
191792c8a632SRahul Lakkireddy 			for (j = 0; j < pi->rss_size; j++)
191892c8a632SRahul Lakkireddy 				pi->rss[j] = j % pi->n_rx_qsets;
191992c8a632SRahul Lakkireddy 
192008e21af9SKumar Sanghvi 			err = cxgbe_write_rss(pi, pi->rss);
192108e21af9SKumar Sanghvi 			if (err)
192208e21af9SKumar Sanghvi 				return err;
192308e21af9SKumar Sanghvi 
192408e21af9SKumar Sanghvi 			err = cxgbe_write_rss_conf(pi, pi->rss_hf);
192592c8a632SRahul Lakkireddy 			if (err)
192692c8a632SRahul Lakkireddy 				return err;
192792c8a632SRahul Lakkireddy 			pi->flags |= PORT_RSS_DONE;
192892c8a632SRahul Lakkireddy 		}
192992c8a632SRahul Lakkireddy 	}
193092c8a632SRahul Lakkireddy 	return 0;
193192c8a632SRahul Lakkireddy }
193292c8a632SRahul Lakkireddy 
19330462d115SRahul Lakkireddy /*
19340462d115SRahul Lakkireddy  * Enable NAPI scheduling and interrupt generation for all Rx queues.
19350462d115SRahul Lakkireddy  */
enable_rx(struct adapter * adap,struct sge_rspq * q)1936d87ba24dSRahul Lakkireddy static void enable_rx(struct adapter *adap, struct sge_rspq *q)
19370462d115SRahul Lakkireddy {
19380462d115SRahul Lakkireddy 	/* 0-increment GTS to start the timer and enable interrupts */
19395e59e39aSKumar Sanghvi 	t4_write_reg(adap, is_pf4(adap) ? MYPF_REG(A_SGE_PF_GTS) :
19405e59e39aSKumar Sanghvi 					  T4VF_SGE_BASE_ADDR + A_SGE_VF_GTS,
19410462d115SRahul Lakkireddy 		     V_SEINTARM(q->intr_params) |
19420462d115SRahul Lakkireddy 		     V_INGRESSQID(q->cntxt_id));
19430462d115SRahul Lakkireddy }
1944d87ba24dSRahul Lakkireddy 
cxgbe_enable_rx_queues(struct port_info * pi)1945d87ba24dSRahul Lakkireddy void cxgbe_enable_rx_queues(struct port_info *pi)
1946d87ba24dSRahul Lakkireddy {
1947d87ba24dSRahul Lakkireddy 	struct adapter *adap = pi->adapter;
1948d87ba24dSRahul Lakkireddy 	struct sge *s = &adap->sge;
1949d87ba24dSRahul Lakkireddy 	unsigned int i;
1950d87ba24dSRahul Lakkireddy 
1951d87ba24dSRahul Lakkireddy 	for (i = 0; i < pi->n_rx_qsets; i++)
19527b3d5298SRahul Lakkireddy 		enable_rx(adap, &s->ethrxq[pi->first_rxqset + i].rspq);
19530462d115SRahul Lakkireddy }
19540462d115SRahul Lakkireddy 
19550462d115SRahul Lakkireddy /**
1956e307e65bSRahul Lakkireddy  * fw_caps_to_speed_caps - translate Firmware Port Caps to Speed Caps.
1957e307e65bSRahul Lakkireddy  * @port_type: Firmware Port Type
1958e307e65bSRahul Lakkireddy  * @fw_caps: Firmware Port Capabilities
1959e307e65bSRahul Lakkireddy  * @speed_caps: Device Info Speed Capabilities
1960e307e65bSRahul Lakkireddy  *
1961e307e65bSRahul Lakkireddy  * Translate a Firmware Port Capabilities specification to Device Info
1962e307e65bSRahul Lakkireddy  * Speed Capabilities.
1963e307e65bSRahul Lakkireddy  */
fw_caps_to_speed_caps(enum fw_port_type port_type,unsigned int fw_caps,u32 * speed_caps)1964e307e65bSRahul Lakkireddy static void fw_caps_to_speed_caps(enum fw_port_type port_type,
1965e307e65bSRahul Lakkireddy 				  unsigned int fw_caps,
1966e307e65bSRahul Lakkireddy 				  u32 *speed_caps)
1967e307e65bSRahul Lakkireddy {
1968e307e65bSRahul Lakkireddy #define SET_SPEED(__speed_name) \
1969e307e65bSRahul Lakkireddy 	do { \
1970295968d1SFerruh Yigit 		*speed_caps |= RTE_ETH_LINK_ ## __speed_name; \
1971e307e65bSRahul Lakkireddy 	} while (0)
1972e307e65bSRahul Lakkireddy 
1973e307e65bSRahul Lakkireddy #define FW_CAPS_TO_SPEED(__fw_name) \
1974e307e65bSRahul Lakkireddy 	do { \
197576488837SRahul Lakkireddy 		if (fw_caps & FW_PORT_CAP32_ ## __fw_name) \
1976e307e65bSRahul Lakkireddy 			SET_SPEED(__fw_name); \
1977e307e65bSRahul Lakkireddy 	} while (0)
1978e307e65bSRahul Lakkireddy 
1979e307e65bSRahul Lakkireddy 	switch (port_type) {
1980e307e65bSRahul Lakkireddy 	case FW_PORT_TYPE_BT_SGMII:
1981e307e65bSRahul Lakkireddy 	case FW_PORT_TYPE_BT_XFI:
1982e307e65bSRahul Lakkireddy 	case FW_PORT_TYPE_BT_XAUI:
1983e307e65bSRahul Lakkireddy 		FW_CAPS_TO_SPEED(SPEED_100M);
1984e307e65bSRahul Lakkireddy 		FW_CAPS_TO_SPEED(SPEED_1G);
1985e307e65bSRahul Lakkireddy 		FW_CAPS_TO_SPEED(SPEED_10G);
1986e307e65bSRahul Lakkireddy 		break;
1987e307e65bSRahul Lakkireddy 
1988e307e65bSRahul Lakkireddy 	case FW_PORT_TYPE_KX4:
1989e307e65bSRahul Lakkireddy 	case FW_PORT_TYPE_KX:
1990e307e65bSRahul Lakkireddy 	case FW_PORT_TYPE_FIBER_XFI:
1991e307e65bSRahul Lakkireddy 	case FW_PORT_TYPE_FIBER_XAUI:
1992e307e65bSRahul Lakkireddy 	case FW_PORT_TYPE_SFP:
1993e307e65bSRahul Lakkireddy 	case FW_PORT_TYPE_QSFP_10G:
1994e307e65bSRahul Lakkireddy 	case FW_PORT_TYPE_QSA:
1995e307e65bSRahul Lakkireddy 		FW_CAPS_TO_SPEED(SPEED_1G);
1996e307e65bSRahul Lakkireddy 		FW_CAPS_TO_SPEED(SPEED_10G);
1997e307e65bSRahul Lakkireddy 		break;
1998e307e65bSRahul Lakkireddy 
1999e307e65bSRahul Lakkireddy 	case FW_PORT_TYPE_KR:
2000e307e65bSRahul Lakkireddy 		SET_SPEED(SPEED_10G);
2001e307e65bSRahul Lakkireddy 		break;
2002e307e65bSRahul Lakkireddy 
2003e307e65bSRahul Lakkireddy 	case FW_PORT_TYPE_BP_AP:
2004e307e65bSRahul Lakkireddy 	case FW_PORT_TYPE_BP4_AP:
2005e307e65bSRahul Lakkireddy 		SET_SPEED(SPEED_1G);
2006e307e65bSRahul Lakkireddy 		SET_SPEED(SPEED_10G);
2007e307e65bSRahul Lakkireddy 		break;
2008e307e65bSRahul Lakkireddy 
2009e307e65bSRahul Lakkireddy 	case FW_PORT_TYPE_BP40_BA:
2010e307e65bSRahul Lakkireddy 	case FW_PORT_TYPE_QSFP:
2011e307e65bSRahul Lakkireddy 		SET_SPEED(SPEED_40G);
2012e307e65bSRahul Lakkireddy 		break;
2013e307e65bSRahul Lakkireddy 
2014e307e65bSRahul Lakkireddy 	case FW_PORT_TYPE_CR_QSFP:
2015e307e65bSRahul Lakkireddy 	case FW_PORT_TYPE_SFP28:
2016e307e65bSRahul Lakkireddy 	case FW_PORT_TYPE_KR_SFP28:
2017e307e65bSRahul Lakkireddy 		FW_CAPS_TO_SPEED(SPEED_1G);
2018e307e65bSRahul Lakkireddy 		FW_CAPS_TO_SPEED(SPEED_10G);
2019e307e65bSRahul Lakkireddy 		FW_CAPS_TO_SPEED(SPEED_25G);
2020e307e65bSRahul Lakkireddy 		break;
2021e307e65bSRahul Lakkireddy 
2022e307e65bSRahul Lakkireddy 	case FW_PORT_TYPE_CR2_QSFP:
2023e307e65bSRahul Lakkireddy 		SET_SPEED(SPEED_50G);
2024e307e65bSRahul Lakkireddy 		break;
2025e307e65bSRahul Lakkireddy 
2026e307e65bSRahul Lakkireddy 	case FW_PORT_TYPE_KR4_100G:
2027e307e65bSRahul Lakkireddy 	case FW_PORT_TYPE_CR4_QSFP:
2028e307e65bSRahul Lakkireddy 		FW_CAPS_TO_SPEED(SPEED_25G);
2029e307e65bSRahul Lakkireddy 		FW_CAPS_TO_SPEED(SPEED_40G);
203076488837SRahul Lakkireddy 		FW_CAPS_TO_SPEED(SPEED_50G);
2031e307e65bSRahul Lakkireddy 		FW_CAPS_TO_SPEED(SPEED_100G);
2032e307e65bSRahul Lakkireddy 		break;
2033e307e65bSRahul Lakkireddy 
2034e307e65bSRahul Lakkireddy 	default:
2035e307e65bSRahul Lakkireddy 		break;
2036e307e65bSRahul Lakkireddy 	}
2037e307e65bSRahul Lakkireddy 
2038e307e65bSRahul Lakkireddy #undef FW_CAPS_TO_SPEED
2039e307e65bSRahul Lakkireddy #undef SET_SPEED
2040e307e65bSRahul Lakkireddy }
2041e307e65bSRahul Lakkireddy 
2042e307e65bSRahul Lakkireddy /**
2043e307e65bSRahul Lakkireddy  * cxgbe_get_speed_caps - Fetch supported speed capabilities
2044e307e65bSRahul Lakkireddy  * @pi: Underlying port's info
2045e307e65bSRahul Lakkireddy  * @speed_caps: Device Info speed capabilities
2046e307e65bSRahul Lakkireddy  *
2047e307e65bSRahul Lakkireddy  * Fetch supported speed capabilities of the underlying port.
2048e307e65bSRahul Lakkireddy  */
cxgbe_get_speed_caps(struct port_info * pi,u32 * speed_caps)2049e307e65bSRahul Lakkireddy void cxgbe_get_speed_caps(struct port_info *pi, u32 *speed_caps)
2050e307e65bSRahul Lakkireddy {
2051e307e65bSRahul Lakkireddy 	*speed_caps = 0;
2052e307e65bSRahul Lakkireddy 
2053a83041b1SKarra Satwik 	fw_caps_to_speed_caps(pi->link_cfg.port_type, pi->link_cfg.pcaps,
2054e307e65bSRahul Lakkireddy 			      speed_caps);
2055e307e65bSRahul Lakkireddy 
205676488837SRahul Lakkireddy 	if (!(pi->link_cfg.pcaps & FW_PORT_CAP32_ANEG))
2057295968d1SFerruh Yigit 		*speed_caps |= RTE_ETH_LINK_SPEED_FIXED;
2058e307e65bSRahul Lakkireddy }
2059e307e65bSRahul Lakkireddy 
2060e307e65bSRahul Lakkireddy /**
2061265af08eSRahul Lakkireddy  * cxgbe_set_link_status - Set device link up or down.
2062265af08eSRahul Lakkireddy  * @pi: Underlying port's info
2063265af08eSRahul Lakkireddy  * @status: 0 - down, 1 - up
2064265af08eSRahul Lakkireddy  *
2065265af08eSRahul Lakkireddy  * Set the device link up or down.
2066265af08eSRahul Lakkireddy  */
cxgbe_set_link_status(struct port_info * pi,bool status)2067265af08eSRahul Lakkireddy int cxgbe_set_link_status(struct port_info *pi, bool status)
2068265af08eSRahul Lakkireddy {
2069265af08eSRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
2070265af08eSRahul Lakkireddy 	int err = 0;
2071265af08eSRahul Lakkireddy 
207223d5fee3SRahul Lakkireddy 	/* Wait for link up message from firmware to enable Rx path,
207323d5fee3SRahul Lakkireddy 	 * if firmware supports enabling/disabling VI Rx at runtime.
207423d5fee3SRahul Lakkireddy 	 */
207523d5fee3SRahul Lakkireddy 	pi->vi_en_rx = adapter->params.vi_enable_rx ? 0 : status;
207623d5fee3SRahul Lakkireddy 	pi->vi_en_tx = status;
207723d5fee3SRahul Lakkireddy 	err = t4_enable_vi(adapter, adapter->mbox, pi->viid, pi->vi_en_rx,
207823d5fee3SRahul Lakkireddy 			   pi->vi_en_tx);
2079265af08eSRahul Lakkireddy 	if (err) {
2080265af08eSRahul Lakkireddy 		dev_err(adapter, "%s: disable_vi failed: %d\n", __func__, err);
2081265af08eSRahul Lakkireddy 		return err;
2082265af08eSRahul Lakkireddy 	}
2083265af08eSRahul Lakkireddy 
2084265af08eSRahul Lakkireddy 	if (!status)
2085265af08eSRahul Lakkireddy 		t4_reset_link_config(adapter, pi->pidx);
2086265af08eSRahul Lakkireddy 
2087265af08eSRahul Lakkireddy 	return 0;
2088265af08eSRahul Lakkireddy }
2089265af08eSRahul Lakkireddy 
2090265af08eSRahul Lakkireddy /**
20910462d115SRahul Lakkireddy  * cxgb_up - enable the adapter
20920462d115SRahul Lakkireddy  * @adap: adapter being enabled
20930462d115SRahul Lakkireddy  *
20940462d115SRahul Lakkireddy  * Called when the first port is enabled, this function performs the
20950462d115SRahul Lakkireddy  * actions necessary to make an adapter operational, such as completing
20960462d115SRahul Lakkireddy  * the initialization of HW modules, and enabling interrupts.
20970462d115SRahul Lakkireddy  */
cxgbe_up(struct adapter * adap)20980462d115SRahul Lakkireddy int cxgbe_up(struct adapter *adap)
20990462d115SRahul Lakkireddy {
2100d87ba24dSRahul Lakkireddy 	enable_rx(adap, &adap->sge.fw_evtq);
21010462d115SRahul Lakkireddy 	t4_sge_tx_monitor_start(adap);
21025e80364aSKumar Sanghvi 	if (is_pf4(adap))
21030462d115SRahul Lakkireddy 		t4_intr_enable(adap);
21040462d115SRahul Lakkireddy 	adap->flags |= FULL_INIT_DONE;
21050462d115SRahul Lakkireddy 
21060462d115SRahul Lakkireddy 	/* TODO: deadman watchdog ?? */
21070462d115SRahul Lakkireddy 	return 0;
21080462d115SRahul Lakkireddy }
21090462d115SRahul Lakkireddy 
21100462d115SRahul Lakkireddy /*
21110462d115SRahul Lakkireddy  * Close the port
21120462d115SRahul Lakkireddy  */
cxgbe_down(struct port_info * pi)21130462d115SRahul Lakkireddy int cxgbe_down(struct port_info *pi)
21140462d115SRahul Lakkireddy {
2115265af08eSRahul Lakkireddy 	return cxgbe_set_link_status(pi, false);
21160462d115SRahul Lakkireddy }
21170462d115SRahul Lakkireddy 
21180462d115SRahul Lakkireddy /*
21190462d115SRahul Lakkireddy  * Release resources when all the ports have been stopped.
21200462d115SRahul Lakkireddy  */
cxgbe_close(struct adapter * adapter)21210462d115SRahul Lakkireddy void cxgbe_close(struct adapter *adapter)
21220462d115SRahul Lakkireddy {
21230462d115SRahul Lakkireddy 	if (adapter->flags & FULL_INIT_DONE) {
21246f2a064bSShagun Agrawal 		tid_free(&adapter->tids);
21256fda3f0dSShagun Agrawal 		t4_cleanup_mpstcam(adapter);
21263f2c1e20SShagun Agrawal 		t4_cleanup_clip_tbl(adapter);
212723af667fSShagun Agrawal 		t4_cleanup_l2t(adapter);
2128a99564c6SKarra Satwik 		t4_cleanup_smt(adapter);
212923af667fSShagun Agrawal 		if (is_pf4(adapter))
213023af667fSShagun Agrawal 			t4_intr_disable(adapter);
21310462d115SRahul Lakkireddy 		t4_sge_tx_monitor_stop(adapter);
21320462d115SRahul Lakkireddy 		t4_free_sge_resources(adapter);
21330462d115SRahul Lakkireddy 		adapter->flags &= ~FULL_INIT_DONE;
21340462d115SRahul Lakkireddy 	}
21350462d115SRahul Lakkireddy 
21367b3d5298SRahul Lakkireddy 	cxgbe_cfg_queues_free(adapter);
21377b3d5298SRahul Lakkireddy 
21385e80364aSKumar Sanghvi 	if (is_pf4(adapter) && (adapter->flags & FW_OK))
21390462d115SRahul Lakkireddy 		t4_fw_bye(adapter, adapter->mbox);
21400462d115SRahul Lakkireddy }
21410462d115SRahul Lakkireddy 
adap_smt_index(struct adapter * adapter,u32 * smt_start_idx,u32 * smt_size)2142a99564c6SKarra Satwik static void adap_smt_index(struct adapter *adapter, u32 *smt_start_idx,
2143a99564c6SKarra Satwik 			   u32 *smt_size)
2144a99564c6SKarra Satwik {
2145a99564c6SKarra Satwik 	u32 params[2], smt_val[2];
2146a99564c6SKarra Satwik 	int ret;
2147a99564c6SKarra Satwik 
2148a99564c6SKarra Satwik 	params[0] = CXGBE_FW_PARAM_PFVF(GET_SMT_START);
2149a99564c6SKarra Satwik 	params[1] = CXGBE_FW_PARAM_PFVF(GET_SMT_SIZE);
2150a99564c6SKarra Satwik 
2151a99564c6SKarra Satwik 	ret = t4_query_params(adapter, adapter->mbox, adapter->pf, 0,
2152a99564c6SKarra Satwik 			      2, params, smt_val);
2153a99564c6SKarra Satwik 
2154a99564c6SKarra Satwik 	/* if FW doesn't recognize this command then set it to default setting
2155a99564c6SKarra Satwik 	 * which is start index as 0 and size as 256.
2156a99564c6SKarra Satwik 	 */
2157a99564c6SKarra Satwik 	if (ret < 0) {
2158a99564c6SKarra Satwik 		*smt_start_idx = 0;
2159a99564c6SKarra Satwik 		*smt_size = SMT_SIZE;
2160a99564c6SKarra Satwik 	} else {
2161a99564c6SKarra Satwik 		*smt_start_idx = smt_val[0];
2162a99564c6SKarra Satwik 		/* smt size can be zero, if nsmt is not yet configured in
2163a99564c6SKarra Satwik 		 * the config file or set as zero, then configure all the
2164a99564c6SKarra Satwik 		 * remaining entries to this PF itself.
2165a99564c6SKarra Satwik 		 */
2166a99564c6SKarra Satwik 		if (!smt_val[1])
2167a99564c6SKarra Satwik 			*smt_size = SMT_SIZE - *smt_start_idx;
2168a99564c6SKarra Satwik 		else
2169a99564c6SKarra Satwik 			*smt_size = smt_val[1];
2170a99564c6SKarra Satwik 	}
2171a99564c6SKarra Satwik }
2172a99564c6SKarra Satwik 
cxgbe_probe(struct adapter * adapter)217383189849SRahul Lakkireddy int cxgbe_probe(struct adapter *adapter)
217483189849SRahul Lakkireddy {
2175a99564c6SKarra Satwik 	u32 smt_start_idx, smt_size;
217683189849SRahul Lakkireddy 	struct port_info *pi;
217783189849SRahul Lakkireddy 	int func, i;
217883189849SRahul Lakkireddy 	int err = 0;
217904868e5bSRahul Lakkireddy 	u32 whoami;
2180a99564c6SKarra Satwik 	int chip;
218183189849SRahul Lakkireddy 
218204868e5bSRahul Lakkireddy 	whoami = t4_read_reg(adapter, A_PL_WHOAMI);
218304868e5bSRahul Lakkireddy 	chip = t4_get_chip_type(adapter,
218404868e5bSRahul Lakkireddy 			CHELSIO_PCI_ID_VER(adapter->pdev->id.device_id));
218504868e5bSRahul Lakkireddy 	if (chip < 0)
218604868e5bSRahul Lakkireddy 		return chip;
218704868e5bSRahul Lakkireddy 
218804868e5bSRahul Lakkireddy 	func = CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5 ?
218904868e5bSRahul Lakkireddy 	       G_SOURCEPF(whoami) : G_T6_SOURCEPF(whoami);
219004868e5bSRahul Lakkireddy 
219183189849SRahul Lakkireddy 	adapter->mbox = func;
219283189849SRahul Lakkireddy 	adapter->pf = func;
219383189849SRahul Lakkireddy 
219483189849SRahul Lakkireddy 	t4_os_lock_init(&adapter->mbox_lock);
219583189849SRahul Lakkireddy 	TAILQ_INIT(&adapter->mbox_list);
21968d3c12e1SShagun Agrawal 	t4_os_lock_init(&adapter->win0_lock);
219783189849SRahul Lakkireddy 
219883189849SRahul Lakkireddy 	err = t4_prep_adapter(adapter);
219983189849SRahul Lakkireddy 	if (err)
220083189849SRahul Lakkireddy 		return err;
220183189849SRahul Lakkireddy 
220283189849SRahul Lakkireddy 	setup_memwin(adapter);
220383189849SRahul Lakkireddy 	err = adap_init0(adapter);
220483189849SRahul Lakkireddy 	if (err) {
220583189849SRahul Lakkireddy 		dev_err(adapter, "%s: Adapter initialization failed, error %d\n",
220683189849SRahul Lakkireddy 			__func__, err);
220783189849SRahul Lakkireddy 		goto out_free;
220883189849SRahul Lakkireddy 	}
220983189849SRahul Lakkireddy 
221083189849SRahul Lakkireddy 	if (!is_t4(adapter->params.chip)) {
221183189849SRahul Lakkireddy 		/*
221283189849SRahul Lakkireddy 		 * The userspace doorbell BAR is split evenly into doorbell
221383189849SRahul Lakkireddy 		 * regions, each associated with an egress queue.  If this
221483189849SRahul Lakkireddy 		 * per-queue region is large enough (at least UDBS_SEG_SIZE)
221583189849SRahul Lakkireddy 		 * then it can be used to submit a tx work request with an
221683189849SRahul Lakkireddy 		 * implied doorbell.  Enable write combining on the BAR if
221783189849SRahul Lakkireddy 		 * there is room for such work requests.
221883189849SRahul Lakkireddy 		 */
221983189849SRahul Lakkireddy 		int s_qpp, qpp, num_seg;
222083189849SRahul Lakkireddy 
222183189849SRahul Lakkireddy 		s_qpp = (S_QUEUESPERPAGEPF0 +
222283189849SRahul Lakkireddy 			(S_QUEUESPERPAGEPF1 - S_QUEUESPERPAGEPF0) *
222383189849SRahul Lakkireddy 			adapter->pf);
222483189849SRahul Lakkireddy 		qpp = 1 << ((t4_read_reg(adapter,
222583189849SRahul Lakkireddy 				A_SGE_EGRESS_QUEUES_PER_PAGE_PF) >> s_qpp)
222683189849SRahul Lakkireddy 				& M_QUEUESPERPAGEPF0);
22271f8613f1SRahul Lakkireddy 		num_seg = CXGBE_PAGE_SIZE / UDBS_SEG_SIZE;
222883189849SRahul Lakkireddy 		if (qpp > num_seg)
222983189849SRahul Lakkireddy 			dev_warn(adapter, "Incorrect SGE EGRESS QUEUES_PER_PAGE configuration, continuing in debug mode\n");
223083189849SRahul Lakkireddy 
223183189849SRahul Lakkireddy 		adapter->bar2 = (void *)adapter->pdev->mem_resource[2].addr;
223283189849SRahul Lakkireddy 		if (!adapter->bar2) {
223383189849SRahul Lakkireddy 			dev_err(adapter, "cannot map device bar2 region\n");
223483189849SRahul Lakkireddy 			err = -ENOMEM;
223583189849SRahul Lakkireddy 			goto out_free;
223683189849SRahul Lakkireddy 		}
223783189849SRahul Lakkireddy 		t4_write_reg(adapter, A_SGE_STAT_CFG, V_STATSOURCE_T5(7) |
223883189849SRahul Lakkireddy 			     V_STATMODE(0));
223983189849SRahul Lakkireddy 	}
224083189849SRahul Lakkireddy 
224183189849SRahul Lakkireddy 	for_each_port(adapter, i) {
224283189849SRahul Lakkireddy 		const unsigned int numa_node = rte_socket_id();
22432195df6dSRahul Lakkireddy 		char name[RTE_ETH_NAME_MAX_LEN];
22442195df6dSRahul Lakkireddy 		struct rte_eth_dev *eth_dev;
224583189849SRahul Lakkireddy 
22462195df6dSRahul Lakkireddy 		snprintf(name, sizeof(name), "%s_%d",
22472195df6dSRahul Lakkireddy 			 adapter->pdev->device.name, i);
224883189849SRahul Lakkireddy 
224983189849SRahul Lakkireddy 		if (i == 0) {
225083189849SRahul Lakkireddy 			/* First port is already allocated by DPDK */
22512195df6dSRahul Lakkireddy 			eth_dev = adapter->eth_dev;
225283189849SRahul Lakkireddy 			goto allocate_mac;
225383189849SRahul Lakkireddy 		}
225483189849SRahul Lakkireddy 
225583189849SRahul Lakkireddy 		/*
225683189849SRahul Lakkireddy 		 * now do all data allocation - for eth_dev structure,
225783189849SRahul Lakkireddy 		 * and internal (private) data for the remaining ports
225883189849SRahul Lakkireddy 		 */
225983189849SRahul Lakkireddy 
226083189849SRahul Lakkireddy 		/* reserve an ethdev entry */
22612195df6dSRahul Lakkireddy 		eth_dev = rte_eth_dev_allocate(name);
22622195df6dSRahul Lakkireddy 		if (!eth_dev)
226383189849SRahul Lakkireddy 			goto out_free;
226483189849SRahul Lakkireddy 
22652195df6dSRahul Lakkireddy 		eth_dev->data->dev_private =
22662195df6dSRahul Lakkireddy 			rte_zmalloc_socket(name, sizeof(struct port_info),
22672195df6dSRahul Lakkireddy 					   RTE_CACHE_LINE_SIZE, numa_node);
22682195df6dSRahul Lakkireddy 		if (!eth_dev->data->dev_private)
226983189849SRahul Lakkireddy 			goto out_free;
227083189849SRahul Lakkireddy 
227183189849SRahul Lakkireddy allocate_mac:
227263a97e58SStephen Hemminger 		pi = eth_dev->data->dev_private;
22732195df6dSRahul Lakkireddy 		adapter->port[i] = pi;
22742195df6dSRahul Lakkireddy 		pi->eth_dev = eth_dev;
22752195df6dSRahul Lakkireddy 		pi->adapter = adapter;
22762195df6dSRahul Lakkireddy 		pi->xact_addr_filt = -1;
22772195df6dSRahul Lakkireddy 		pi->port_id = i;
22785e80364aSKumar Sanghvi 		pi->pidx = i;
22792195df6dSRahul Lakkireddy 
2280eac901ceSJan Blunck 		pi->eth_dev->device = &adapter->pdev->device;
228183189849SRahul Lakkireddy 		pi->eth_dev->dev_ops = adapter->eth_dev->dev_ops;
22824a01078bSRahul Lakkireddy 		pi->eth_dev->tx_pkt_burst = adapter->eth_dev->tx_pkt_burst;
228392c8a632SRahul Lakkireddy 		pi->eth_dev->rx_pkt_burst = adapter->eth_dev->rx_pkt_burst;
228413b0f500SRahul Lakkireddy 
2285eac901ceSJan Blunck 		rte_eth_copy_pci_info(pi->eth_dev, adapter->pdev);
228613b0f500SRahul Lakkireddy 
228783189849SRahul Lakkireddy 		pi->eth_dev->data->mac_addrs = rte_zmalloc(name,
228835b2d13fSOlivier Matz 							RTE_ETHER_ADDR_LEN, 0);
228983189849SRahul Lakkireddy 		if (!pi->eth_dev->data->mac_addrs) {
229083189849SRahul Lakkireddy 			dev_err(adapter, "%s: Mem allocation failed for storing mac addr, aborting\n",
229183189849SRahul Lakkireddy 				__func__);
229283189849SRahul Lakkireddy 			err = -1;
229383189849SRahul Lakkireddy 			goto out_free;
229483189849SRahul Lakkireddy 		}
2295fbe90cddSThomas Monjalon 
2296fbe90cddSThomas Monjalon 		if (i > 0) {
2297fbe90cddSThomas Monjalon 			/* First port will be notified by upper layer */
2298fbe90cddSThomas Monjalon 			rte_eth_dev_probing_finish(eth_dev);
2299fbe90cddSThomas Monjalon 		}
230083189849SRahul Lakkireddy 	}
230183189849SRahul Lakkireddy 
230283189849SRahul Lakkireddy 	if (adapter->flags & FW_OK) {
230383189849SRahul Lakkireddy 		err = t4_port_init(adapter, adapter->mbox, adapter->pf, 0);
230483189849SRahul Lakkireddy 		if (err) {
230583189849SRahul Lakkireddy 			dev_err(adapter, "%s: t4_port_init failed with err %d\n",
230683189849SRahul Lakkireddy 				__func__, err);
230783189849SRahul Lakkireddy 			goto out_free;
230883189849SRahul Lakkireddy 		}
230983189849SRahul Lakkireddy 	}
231083189849SRahul Lakkireddy 
23117b3d5298SRahul Lakkireddy 	err = cxgbe_cfg_queues(adapter->eth_dev);
23127b3d5298SRahul Lakkireddy 	if (err)
23137b3d5298SRahul Lakkireddy 		goto out_free;
231492c8a632SRahul Lakkireddy 
2315b7fd9ea8SStephen Hemminger 	cxgbe_print_adapter_info(adapter);
2316b7fd9ea8SStephen Hemminger 	cxgbe_print_port_info(adapter);
231783189849SRahul Lakkireddy 
23183f2c1e20SShagun Agrawal 	adapter->clipt = t4_init_clip_tbl(adapter->clipt_start,
23193f2c1e20SShagun Agrawal 					  adapter->clipt_end);
23203f2c1e20SShagun Agrawal 	if (!adapter->clipt) {
23213f2c1e20SShagun Agrawal 		/* We tolerate a lack of clip_table, giving up some
23223f2c1e20SShagun Agrawal 		 * functionality
23233f2c1e20SShagun Agrawal 		 */
23243f2c1e20SShagun Agrawal 		dev_warn(adapter, "could not allocate CLIP. Continuing\n");
23253f2c1e20SShagun Agrawal 	}
23263f2c1e20SShagun Agrawal 
2327a99564c6SKarra Satwik 	adap_smt_index(adapter, &smt_start_idx, &smt_size);
2328a99564c6SKarra Satwik 	adapter->smt = t4_init_smt(smt_start_idx, smt_size);
2329a99564c6SKarra Satwik 	if (!adapter->smt)
2330a99564c6SKarra Satwik 		dev_warn(adapter, "could not allocate SMT, continuing\n");
2331a99564c6SKarra Satwik 
233223af667fSShagun Agrawal 	adapter->l2t = t4_init_l2t(adapter->l2t_start, adapter->l2t_end);
233323af667fSShagun Agrawal 	if (!adapter->l2t) {
233423af667fSShagun Agrawal 		/* We tolerate a lack of L2T, giving up some functionality */
233523af667fSShagun Agrawal 		dev_warn(adapter, "could not allocate L2T. Continuing\n");
233623af667fSShagun Agrawal 	}
233723af667fSShagun Agrawal 
23386f2a064bSShagun Agrawal 	if (tid_init(&adapter->tids) < 0) {
23396f2a064bSShagun Agrawal 		/* Disable filtering support */
23406f2a064bSShagun Agrawal 		dev_warn(adapter, "could not allocate TID table, "
23416f2a064bSShagun Agrawal 			 "filter support disabled. Continuing\n");
23426f2a064bSShagun Agrawal 	}
23436f2a064bSShagun Agrawal 
234497e02581SRahul Lakkireddy 	t4_os_lock_init(&adapter->flow_lock);
234597e02581SRahul Lakkireddy 
23466fda3f0dSShagun Agrawal 	adapter->mpstcam = t4_init_mpstcam(adapter);
23476fda3f0dSShagun Agrawal 	if (!adapter->mpstcam)
23486fda3f0dSShagun Agrawal 		dev_warn(adapter, "could not allocate mps tcam table."
23496fda3f0dSShagun Agrawal 			 " Continuing\n");
23506fda3f0dSShagun Agrawal 
23513a381a41SShagun Agrawal 	if (is_hashfilter(adapter)) {
23523a381a41SShagun Agrawal 		if (t4_read_reg(adapter, A_LE_DB_CONFIG) & F_HASHEN) {
23533a381a41SShagun Agrawal 			u32 hash_base, hash_reg;
23543a381a41SShagun Agrawal 
23553a381a41SShagun Agrawal 			hash_reg = A_LE_DB_TID_HASHBASE;
23563a381a41SShagun Agrawal 			hash_base = t4_read_reg(adapter, hash_reg);
23573a381a41SShagun Agrawal 			adapter->tids.hash_base = hash_base / 4;
23583a381a41SShagun Agrawal 		}
23593a381a41SShagun Agrawal 	} else {
23603a381a41SShagun Agrawal 		/* Disable hash filtering support */
23613a381a41SShagun Agrawal 		dev_warn(adapter,
23623a381a41SShagun Agrawal 			 "Maskless filter support disabled. Continuing\n");
23633a381a41SShagun Agrawal 	}
23643a381a41SShagun Agrawal 
2365b7fd9ea8SStephen Hemminger 	err = cxgbe_init_rss(adapter);
236692c8a632SRahul Lakkireddy 	if (err)
236792c8a632SRahul Lakkireddy 		goto out_free;
236892c8a632SRahul Lakkireddy 
236983189849SRahul Lakkireddy 	return 0;
237083189849SRahul Lakkireddy 
237183189849SRahul Lakkireddy out_free:
23727b3d5298SRahul Lakkireddy 	cxgbe_cfg_queues_free(adapter);
23737b3d5298SRahul Lakkireddy 
237483189849SRahul Lakkireddy 	for_each_port(adapter, i) {
237583189849SRahul Lakkireddy 		pi = adap2pinfo(adapter, i);
237683189849SRahul Lakkireddy 		if (pi->viid != 0)
237783189849SRahul Lakkireddy 			t4_free_vi(adapter, adapter->mbox, adapter->pf,
237883189849SRahul Lakkireddy 				   0, pi->viid);
23792195df6dSRahul Lakkireddy 		rte_eth_dev_release_port(pi->eth_dev);
23802195df6dSRahul Lakkireddy 	}
238183189849SRahul Lakkireddy 
238283189849SRahul Lakkireddy 	if (adapter->flags & FW_OK)
238383189849SRahul Lakkireddy 		t4_fw_bye(adapter, adapter->mbox);
238483189849SRahul Lakkireddy 	return -err;
238583189849SRahul Lakkireddy }
2386