xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision 41fb0cf1bced59c1fe178cf6cc9f716b5da9e40e)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  * Copyright (c) 2016-2019 Broadcom Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  */
10 
11 #include <linux/module.h>
12 
13 #include <linux/stringify.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/pci.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/bitops.h>
27 #include <linux/io.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/byteorder.h>
31 #include <asm/page.h>
32 #include <linux/time.h>
33 #include <linux/mii.h>
34 #include <linux/mdio.h>
35 #include <linux/if.h>
36 #include <linux/if_vlan.h>
37 #include <linux/if_bridge.h>
38 #include <linux/rtc.h>
39 #include <linux/bpf.h>
40 #include <net/gro.h>
41 #include <net/ip.h>
42 #include <net/tcp.h>
43 #include <net/udp.h>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
46 #include <net/udp_tunnel.h>
47 #include <linux/workqueue.h>
48 #include <linux/prefetch.h>
49 #include <linux/cache.h>
50 #include <linux/log2.h>
51 #include <linux/aer.h>
52 #include <linux/bitmap.h>
53 #include <linux/cpu_rmap.h>
54 #include <linux/cpumask.h>
55 #include <net/pkt_cls.h>
56 #include <linux/hwmon.h>
57 #include <linux/hwmon-sysfs.h>
58 #include <net/page_pool.h>
59 
60 #include "bnxt_hsi.h"
61 #include "bnxt.h"
62 #include "bnxt_hwrm.h"
63 #include "bnxt_ulp.h"
64 #include "bnxt_sriov.h"
65 #include "bnxt_ethtool.h"
66 #include "bnxt_dcb.h"
67 #include "bnxt_xdp.h"
68 #include "bnxt_ptp.h"
69 #include "bnxt_vfr.h"
70 #include "bnxt_tc.h"
71 #include "bnxt_devlink.h"
72 #include "bnxt_debugfs.h"
73 
74 #define BNXT_TX_TIMEOUT		(5 * HZ)
75 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
76 				 NETIF_MSG_TX_ERR)
77 
78 MODULE_LICENSE("GPL");
79 MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
80 
81 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
82 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
83 #define BNXT_RX_COPY_THRESH 256
84 
85 #define BNXT_TX_PUSH_THRESH 164
86 
87 /* indexed by enum board_idx */
88 static const struct {
89 	char *name;
90 } board_info[] = {
91 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
92 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
93 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
94 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
95 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
96 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
97 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
98 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
99 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
100 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
101 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
102 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
103 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
104 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
105 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
106 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
108 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
109 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
110 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
111 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
112 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
113 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
114 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
115 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
116 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
117 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
118 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
119 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
120 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
121 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
122 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
123 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
124 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
125 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
126 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
127 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
128 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
129 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
130 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
131 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
132 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
133 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
134 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
135 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
136 };
137 
138 static const struct pci_device_id bnxt_pci_tbl[] = {
139 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
140 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
141 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
142 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
143 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
144 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
145 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
146 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
147 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
148 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
149 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
150 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
151 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
152 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
153 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
154 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
155 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
159 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
160 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
162 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
163 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
164 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
165 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
166 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
167 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
168 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
169 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
170 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
171 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
172 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
173 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
174 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
175 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
176 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
177 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57508_NPAR },
178 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
179 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57502_NPAR },
180 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57508_NPAR },
181 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
182 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57502_NPAR },
183 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
184 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
185 #ifdef CONFIG_BNXT_SRIOV
186 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
187 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
188 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
189 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
190 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
191 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
192 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
193 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
194 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
195 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
196 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
197 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
198 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
199 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
200 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
201 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
202 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
203 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
204 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
205 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
206 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
207 #endif
208 	{ 0 }
209 };
210 
211 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
212 
213 static const u16 bnxt_vf_req_snif[] = {
214 	HWRM_FUNC_CFG,
215 	HWRM_FUNC_VF_CFG,
216 	HWRM_PORT_PHY_QCFG,
217 	HWRM_CFA_L2_FILTER_ALLOC,
218 };
219 
220 static const u16 bnxt_async_events_arr[] = {
221 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
222 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
223 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
224 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
225 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
226 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
227 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
228 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
229 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
230 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
231 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
232 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
233 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
234 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
235 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
236 };
237 
238 static struct workqueue_struct *bnxt_pf_wq;
239 
240 static bool bnxt_vf_pciid(enum board_idx idx)
241 {
242 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
243 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
244 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
245 		idx == NETXTREME_E_P5_VF_HV);
246 }
247 
248 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
249 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
250 #define DB_CP_IRQ_DIS_FLAGS	(DB_KEY_CP | DB_IRQ_DIS)
251 
252 #define BNXT_CP_DB_IRQ_DIS(db)						\
253 		writel(DB_CP_IRQ_DIS_FLAGS, db)
254 
255 #define BNXT_DB_CQ(db, idx)						\
256 	writel(DB_CP_FLAGS | RING_CMP(idx), (db)->doorbell)
257 
258 #define BNXT_DB_NQ_P5(db, idx)						\
259 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | RING_CMP(idx),	\
260 		    (db)->doorbell)
261 
262 #define BNXT_DB_CQ_ARM(db, idx)						\
263 	writel(DB_CP_REARM_FLAGS | RING_CMP(idx), (db)->doorbell)
264 
265 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
266 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM | RING_CMP(idx),\
267 		    (db)->doorbell)
268 
269 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
270 {
271 	if (bp->flags & BNXT_FLAG_CHIP_P5)
272 		BNXT_DB_NQ_P5(db, idx);
273 	else
274 		BNXT_DB_CQ(db, idx);
275 }
276 
277 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
278 {
279 	if (bp->flags & BNXT_FLAG_CHIP_P5)
280 		BNXT_DB_NQ_ARM_P5(db, idx);
281 	else
282 		BNXT_DB_CQ_ARM(db, idx);
283 }
284 
285 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
286 {
287 	if (bp->flags & BNXT_FLAG_CHIP_P5)
288 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
289 			    RING_CMP(idx), db->doorbell);
290 	else
291 		BNXT_DB_CQ(db, idx);
292 }
293 
294 const u16 bnxt_lhint_arr[] = {
295 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
296 	TX_BD_FLAGS_LHINT_512_TO_1023,
297 	TX_BD_FLAGS_LHINT_1024_TO_2047,
298 	TX_BD_FLAGS_LHINT_1024_TO_2047,
299 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
300 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
301 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
302 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
303 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
304 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
305 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
306 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
307 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
308 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
309 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
310 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
311 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
312 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
313 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
314 };
315 
316 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
317 {
318 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
319 
320 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
321 		return 0;
322 
323 	return md_dst->u.port_info.port_id;
324 }
325 
326 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
327 			     u16 prod)
328 {
329 	bnxt_db_write(bp, &txr->tx_db, prod);
330 	txr->kick_pending = 0;
331 }
332 
333 static bool bnxt_txr_netif_try_stop_queue(struct bnxt *bp,
334 					  struct bnxt_tx_ring_info *txr,
335 					  struct netdev_queue *txq)
336 {
337 	netif_tx_stop_queue(txq);
338 
339 	/* netif_tx_stop_queue() must be done before checking
340 	 * tx index in bnxt_tx_avail() below, because in
341 	 * bnxt_tx_int(), we update tx index before checking for
342 	 * netif_tx_queue_stopped().
343 	 */
344 	smp_mb();
345 	if (bnxt_tx_avail(bp, txr) >= bp->tx_wake_thresh) {
346 		netif_tx_wake_queue(txq);
347 		return false;
348 	}
349 
350 	return true;
351 }
352 
353 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
354 {
355 	struct bnxt *bp = netdev_priv(dev);
356 	struct tx_bd *txbd;
357 	struct tx_bd_ext *txbd1;
358 	struct netdev_queue *txq;
359 	int i;
360 	dma_addr_t mapping;
361 	unsigned int length, pad = 0;
362 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
363 	u16 prod, last_frag;
364 	struct pci_dev *pdev = bp->pdev;
365 	struct bnxt_tx_ring_info *txr;
366 	struct bnxt_sw_tx_bd *tx_buf;
367 	__le32 lflags = 0;
368 
369 	i = skb_get_queue_mapping(skb);
370 	if (unlikely(i >= bp->tx_nr_rings)) {
371 		dev_kfree_skb_any(skb);
372 		atomic_long_inc(&dev->tx_dropped);
373 		return NETDEV_TX_OK;
374 	}
375 
376 	txq = netdev_get_tx_queue(dev, i);
377 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
378 	prod = txr->tx_prod;
379 
380 	free_size = bnxt_tx_avail(bp, txr);
381 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
382 		/* We must have raced with NAPI cleanup */
383 		if (net_ratelimit() && txr->kick_pending)
384 			netif_warn(bp, tx_err, dev,
385 				   "bnxt: ring busy w/ flush pending!\n");
386 		if (bnxt_txr_netif_try_stop_queue(bp, txr, txq))
387 			return NETDEV_TX_BUSY;
388 	}
389 
390 	length = skb->len;
391 	len = skb_headlen(skb);
392 	last_frag = skb_shinfo(skb)->nr_frags;
393 
394 	txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
395 
396 	txbd->tx_bd_opaque = prod;
397 
398 	tx_buf = &txr->tx_buf_ring[prod];
399 	tx_buf->skb = skb;
400 	tx_buf->nr_frags = last_frag;
401 
402 	vlan_tag_flags = 0;
403 	cfa_action = bnxt_xmit_get_cfa_action(skb);
404 	if (skb_vlan_tag_present(skb)) {
405 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
406 				 skb_vlan_tag_get(skb);
407 		/* Currently supports 8021Q, 8021AD vlan offloads
408 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
409 		 */
410 		if (skb->vlan_proto == htons(ETH_P_8021Q))
411 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
412 	}
413 
414 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
415 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
416 
417 		if (ptp && ptp->tx_tstamp_en && !skb_is_gso(skb) &&
418 		    atomic_dec_if_positive(&ptp->tx_avail) >= 0) {
419 			if (!bnxt_ptp_parse(skb, &ptp->tx_seqid,
420 					    &ptp->tx_hdr_off)) {
421 				if (vlan_tag_flags)
422 					ptp->tx_hdr_off += VLAN_HLEN;
423 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
424 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
425 			} else {
426 				atomic_inc(&bp->ptp_cfg->tx_avail);
427 			}
428 		}
429 	}
430 
431 	if (unlikely(skb->no_fcs))
432 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
433 
434 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
435 	    !lflags) {
436 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
437 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
438 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
439 		void __iomem *db = txr->tx_db.doorbell;
440 		void *pdata = tx_push_buf->data;
441 		u64 *end;
442 		int j, push_len;
443 
444 		/* Set COAL_NOW to be ready quickly for the next push */
445 		tx_push->tx_bd_len_flags_type =
446 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
447 					TX_BD_TYPE_LONG_TX_BD |
448 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
449 					TX_BD_FLAGS_COAL_NOW |
450 					TX_BD_FLAGS_PACKET_END |
451 					(2 << TX_BD_FLAGS_BD_CNT_SHIFT));
452 
453 		if (skb->ip_summed == CHECKSUM_PARTIAL)
454 			tx_push1->tx_bd_hsize_lflags =
455 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
456 		else
457 			tx_push1->tx_bd_hsize_lflags = 0;
458 
459 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
460 		tx_push1->tx_bd_cfa_action =
461 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
462 
463 		end = pdata + length;
464 		end = PTR_ALIGN(end, 8) - 1;
465 		*end = 0;
466 
467 		skb_copy_from_linear_data(skb, pdata, len);
468 		pdata += len;
469 		for (j = 0; j < last_frag; j++) {
470 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
471 			void *fptr;
472 
473 			fptr = skb_frag_address_safe(frag);
474 			if (!fptr)
475 				goto normal_tx;
476 
477 			memcpy(pdata, fptr, skb_frag_size(frag));
478 			pdata += skb_frag_size(frag);
479 		}
480 
481 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
482 		txbd->tx_bd_haddr = txr->data_mapping;
483 		prod = NEXT_TX(prod);
484 		txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
485 		memcpy(txbd, tx_push1, sizeof(*txbd));
486 		prod = NEXT_TX(prod);
487 		tx_push->doorbell =
488 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
489 		txr->tx_prod = prod;
490 
491 		tx_buf->is_push = 1;
492 		netdev_tx_sent_queue(txq, skb->len);
493 		wmb();	/* Sync is_push and byte queue before pushing data */
494 
495 		push_len = (length + sizeof(*tx_push) + 7) / 8;
496 		if (push_len > 16) {
497 			__iowrite64_copy(db, tx_push_buf, 16);
498 			__iowrite32_copy(db + 4, tx_push_buf + 1,
499 					 (push_len - 16) << 1);
500 		} else {
501 			__iowrite64_copy(db, tx_push_buf, push_len);
502 		}
503 
504 		goto tx_done;
505 	}
506 
507 normal_tx:
508 	if (length < BNXT_MIN_PKT_SIZE) {
509 		pad = BNXT_MIN_PKT_SIZE - length;
510 		if (skb_pad(skb, pad))
511 			/* SKB already freed. */
512 			goto tx_kick_pending;
513 		length = BNXT_MIN_PKT_SIZE;
514 	}
515 
516 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
517 
518 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
519 		goto tx_free;
520 
521 	dma_unmap_addr_set(tx_buf, mapping, mapping);
522 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
523 		((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
524 
525 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
526 
527 	prod = NEXT_TX(prod);
528 	txbd1 = (struct tx_bd_ext *)
529 		&txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
530 
531 	txbd1->tx_bd_hsize_lflags = lflags;
532 	if (skb_is_gso(skb)) {
533 		u32 hdr_len;
534 
535 		if (skb->encapsulation)
536 			hdr_len = skb_inner_network_offset(skb) +
537 				skb_inner_network_header_len(skb) +
538 				inner_tcp_hdrlen(skb);
539 		else
540 			hdr_len = skb_transport_offset(skb) +
541 				tcp_hdrlen(skb);
542 
543 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
544 					TX_BD_FLAGS_T_IPID |
545 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
546 		length = skb_shinfo(skb)->gso_size;
547 		txbd1->tx_bd_mss = cpu_to_le32(length);
548 		length += hdr_len;
549 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
550 		txbd1->tx_bd_hsize_lflags |=
551 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
552 		txbd1->tx_bd_mss = 0;
553 	}
554 
555 	length >>= 9;
556 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
557 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
558 				     skb->len);
559 		i = 0;
560 		goto tx_dma_error;
561 	}
562 	flags |= bnxt_lhint_arr[length];
563 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
564 
565 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
566 	txbd1->tx_bd_cfa_action =
567 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
568 	for (i = 0; i < last_frag; i++) {
569 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
570 
571 		prod = NEXT_TX(prod);
572 		txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
573 
574 		len = skb_frag_size(frag);
575 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
576 					   DMA_TO_DEVICE);
577 
578 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
579 			goto tx_dma_error;
580 
581 		tx_buf = &txr->tx_buf_ring[prod];
582 		dma_unmap_addr_set(tx_buf, mapping, mapping);
583 
584 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
585 
586 		flags = len << TX_BD_LEN_SHIFT;
587 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
588 	}
589 
590 	flags &= ~TX_BD_LEN;
591 	txbd->tx_bd_len_flags_type =
592 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
593 			    TX_BD_FLAGS_PACKET_END);
594 
595 	netdev_tx_sent_queue(txq, skb->len);
596 
597 	skb_tx_timestamp(skb);
598 
599 	/* Sync BD data before updating doorbell */
600 	wmb();
601 
602 	prod = NEXT_TX(prod);
603 	txr->tx_prod = prod;
604 
605 	if (!netdev_xmit_more() || netif_xmit_stopped(txq))
606 		bnxt_txr_db_kick(bp, txr, prod);
607 	else
608 		txr->kick_pending = 1;
609 
610 tx_done:
611 
612 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
613 		if (netdev_xmit_more() && !tx_buf->is_push)
614 			bnxt_txr_db_kick(bp, txr, prod);
615 
616 		bnxt_txr_netif_try_stop_queue(bp, txr, txq);
617 	}
618 	return NETDEV_TX_OK;
619 
620 tx_dma_error:
621 	if (BNXT_TX_PTP_IS_SET(lflags))
622 		atomic_inc(&bp->ptp_cfg->tx_avail);
623 
624 	last_frag = i;
625 
626 	/* start back at beginning and unmap skb */
627 	prod = txr->tx_prod;
628 	tx_buf = &txr->tx_buf_ring[prod];
629 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
630 			 skb_headlen(skb), DMA_TO_DEVICE);
631 	prod = NEXT_TX(prod);
632 
633 	/* unmap remaining mapped pages */
634 	for (i = 0; i < last_frag; i++) {
635 		prod = NEXT_TX(prod);
636 		tx_buf = &txr->tx_buf_ring[prod];
637 		dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
638 			       skb_frag_size(&skb_shinfo(skb)->frags[i]),
639 			       DMA_TO_DEVICE);
640 	}
641 
642 tx_free:
643 	dev_kfree_skb_any(skb);
644 tx_kick_pending:
645 	if (txr->kick_pending)
646 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
647 	txr->tx_buf_ring[txr->tx_prod].skb = NULL;
648 	atomic_long_inc(&dev->tx_dropped);
649 	return NETDEV_TX_OK;
650 }
651 
652 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
653 {
654 	struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
655 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
656 	u16 cons = txr->tx_cons;
657 	struct pci_dev *pdev = bp->pdev;
658 	int i;
659 	unsigned int tx_bytes = 0;
660 
661 	for (i = 0; i < nr_pkts; i++) {
662 		struct bnxt_sw_tx_bd *tx_buf;
663 		bool compl_deferred = false;
664 		struct sk_buff *skb;
665 		int j, last;
666 
667 		tx_buf = &txr->tx_buf_ring[cons];
668 		cons = NEXT_TX(cons);
669 		skb = tx_buf->skb;
670 		tx_buf->skb = NULL;
671 
672 		if (tx_buf->is_push) {
673 			tx_buf->is_push = 0;
674 			goto next_tx_int;
675 		}
676 
677 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
678 				 skb_headlen(skb), DMA_TO_DEVICE);
679 		last = tx_buf->nr_frags;
680 
681 		for (j = 0; j < last; j++) {
682 			cons = NEXT_TX(cons);
683 			tx_buf = &txr->tx_buf_ring[cons];
684 			dma_unmap_page(
685 				&pdev->dev,
686 				dma_unmap_addr(tx_buf, mapping),
687 				skb_frag_size(&skb_shinfo(skb)->frags[j]),
688 				DMA_TO_DEVICE);
689 		}
690 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
691 			if (bp->flags & BNXT_FLAG_CHIP_P5) {
692 				if (!bnxt_get_tx_ts_p5(bp, skb))
693 					compl_deferred = true;
694 				else
695 					atomic_inc(&bp->ptp_cfg->tx_avail);
696 			}
697 		}
698 
699 next_tx_int:
700 		cons = NEXT_TX(cons);
701 
702 		tx_bytes += skb->len;
703 		if (!compl_deferred)
704 			dev_kfree_skb_any(skb);
705 	}
706 
707 	netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
708 	txr->tx_cons = cons;
709 
710 	/* Need to make the tx_cons update visible to bnxt_start_xmit()
711 	 * before checking for netif_tx_queue_stopped().  Without the
712 	 * memory barrier, there is a small possibility that bnxt_start_xmit()
713 	 * will miss it and cause the queue to be stopped forever.
714 	 */
715 	smp_mb();
716 
717 	if (unlikely(netif_tx_queue_stopped(txq)) &&
718 	    bnxt_tx_avail(bp, txr) >= bp->tx_wake_thresh &&
719 	    READ_ONCE(txr->dev_state) != BNXT_DEV_STATE_CLOSING)
720 		netif_tx_wake_queue(txq);
721 }
722 
723 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
724 					 struct bnxt_rx_ring_info *rxr,
725 					 gfp_t gfp)
726 {
727 	struct device *dev = &bp->pdev->dev;
728 	struct page *page;
729 
730 	page = page_pool_dev_alloc_pages(rxr->page_pool);
731 	if (!page)
732 		return NULL;
733 
734 	*mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir,
735 				      DMA_ATTR_WEAK_ORDERING);
736 	if (dma_mapping_error(dev, *mapping)) {
737 		page_pool_recycle_direct(rxr->page_pool, page);
738 		return NULL;
739 	}
740 	*mapping += bp->rx_dma_offset;
741 	return page;
742 }
743 
744 static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
745 				       gfp_t gfp)
746 {
747 	u8 *data;
748 	struct pci_dev *pdev = bp->pdev;
749 
750 	data = kmalloc(bp->rx_buf_size, gfp);
751 	if (!data)
752 		return NULL;
753 
754 	*mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
755 					bp->rx_buf_use_size, bp->rx_dir,
756 					DMA_ATTR_WEAK_ORDERING);
757 
758 	if (dma_mapping_error(&pdev->dev, *mapping)) {
759 		kfree(data);
760 		data = NULL;
761 	}
762 	return data;
763 }
764 
765 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
766 		       u16 prod, gfp_t gfp)
767 {
768 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
769 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
770 	dma_addr_t mapping;
771 
772 	if (BNXT_RX_PAGE_MODE(bp)) {
773 		struct page *page =
774 			__bnxt_alloc_rx_page(bp, &mapping, rxr, gfp);
775 
776 		if (!page)
777 			return -ENOMEM;
778 
779 		rx_buf->data = page;
780 		rx_buf->data_ptr = page_address(page) + bp->rx_offset;
781 	} else {
782 		u8 *data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
783 
784 		if (!data)
785 			return -ENOMEM;
786 
787 		rx_buf->data = data;
788 		rx_buf->data_ptr = data + bp->rx_offset;
789 	}
790 	rx_buf->mapping = mapping;
791 
792 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
793 	return 0;
794 }
795 
796 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
797 {
798 	u16 prod = rxr->rx_prod;
799 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
800 	struct rx_bd *cons_bd, *prod_bd;
801 
802 	prod_rx_buf = &rxr->rx_buf_ring[prod];
803 	cons_rx_buf = &rxr->rx_buf_ring[cons];
804 
805 	prod_rx_buf->data = data;
806 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
807 
808 	prod_rx_buf->mapping = cons_rx_buf->mapping;
809 
810 	prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
811 	cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
812 
813 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
814 }
815 
816 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
817 {
818 	u16 next, max = rxr->rx_agg_bmap_size;
819 
820 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
821 	if (next >= max)
822 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
823 	return next;
824 }
825 
826 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
827 				     struct bnxt_rx_ring_info *rxr,
828 				     u16 prod, gfp_t gfp)
829 {
830 	struct rx_bd *rxbd =
831 		&rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
832 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
833 	struct pci_dev *pdev = bp->pdev;
834 	struct page *page;
835 	dma_addr_t mapping;
836 	u16 sw_prod = rxr->rx_sw_agg_prod;
837 	unsigned int offset = 0;
838 
839 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
840 		page = rxr->rx_page;
841 		if (!page) {
842 			page = alloc_page(gfp);
843 			if (!page)
844 				return -ENOMEM;
845 			rxr->rx_page = page;
846 			rxr->rx_page_offset = 0;
847 		}
848 		offset = rxr->rx_page_offset;
849 		rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
850 		if (rxr->rx_page_offset == PAGE_SIZE)
851 			rxr->rx_page = NULL;
852 		else
853 			get_page(page);
854 	} else {
855 		page = alloc_page(gfp);
856 		if (!page)
857 			return -ENOMEM;
858 	}
859 
860 	mapping = dma_map_page_attrs(&pdev->dev, page, offset,
861 				     BNXT_RX_PAGE_SIZE, DMA_FROM_DEVICE,
862 				     DMA_ATTR_WEAK_ORDERING);
863 	if (dma_mapping_error(&pdev->dev, mapping)) {
864 		__free_page(page);
865 		return -EIO;
866 	}
867 
868 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
869 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
870 
871 	__set_bit(sw_prod, rxr->rx_agg_bmap);
872 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
873 	rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
874 
875 	rx_agg_buf->page = page;
876 	rx_agg_buf->offset = offset;
877 	rx_agg_buf->mapping = mapping;
878 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
879 	rxbd->rx_bd_opaque = sw_prod;
880 	return 0;
881 }
882 
883 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
884 				       struct bnxt_cp_ring_info *cpr,
885 				       u16 cp_cons, u16 curr)
886 {
887 	struct rx_agg_cmp *agg;
888 
889 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
890 	agg = (struct rx_agg_cmp *)
891 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
892 	return agg;
893 }
894 
895 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
896 					      struct bnxt_rx_ring_info *rxr,
897 					      u16 agg_id, u16 curr)
898 {
899 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
900 
901 	return &tpa_info->agg_arr[curr];
902 }
903 
904 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
905 				   u16 start, u32 agg_bufs, bool tpa)
906 {
907 	struct bnxt_napi *bnapi = cpr->bnapi;
908 	struct bnxt *bp = bnapi->bp;
909 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
910 	u16 prod = rxr->rx_agg_prod;
911 	u16 sw_prod = rxr->rx_sw_agg_prod;
912 	bool p5_tpa = false;
913 	u32 i;
914 
915 	if ((bp->flags & BNXT_FLAG_CHIP_P5) && tpa)
916 		p5_tpa = true;
917 
918 	for (i = 0; i < agg_bufs; i++) {
919 		u16 cons;
920 		struct rx_agg_cmp *agg;
921 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
922 		struct rx_bd *prod_bd;
923 		struct page *page;
924 
925 		if (p5_tpa)
926 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
927 		else
928 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
929 		cons = agg->rx_agg_cmp_opaque;
930 		__clear_bit(cons, rxr->rx_agg_bmap);
931 
932 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
933 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
934 
935 		__set_bit(sw_prod, rxr->rx_agg_bmap);
936 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
937 		cons_rx_buf = &rxr->rx_agg_ring[cons];
938 
939 		/* It is possible for sw_prod to be equal to cons, so
940 		 * set cons_rx_buf->page to NULL first.
941 		 */
942 		page = cons_rx_buf->page;
943 		cons_rx_buf->page = NULL;
944 		prod_rx_buf->page = page;
945 		prod_rx_buf->offset = cons_rx_buf->offset;
946 
947 		prod_rx_buf->mapping = cons_rx_buf->mapping;
948 
949 		prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
950 
951 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
952 		prod_bd->rx_bd_opaque = sw_prod;
953 
954 		prod = NEXT_RX_AGG(prod);
955 		sw_prod = NEXT_RX_AGG(sw_prod);
956 	}
957 	rxr->rx_agg_prod = prod;
958 	rxr->rx_sw_agg_prod = sw_prod;
959 }
960 
961 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
962 					struct bnxt_rx_ring_info *rxr,
963 					u16 cons, void *data, u8 *data_ptr,
964 					dma_addr_t dma_addr,
965 					unsigned int offset_and_len)
966 {
967 	unsigned int payload = offset_and_len >> 16;
968 	unsigned int len = offset_and_len & 0xffff;
969 	skb_frag_t *frag;
970 	struct page *page = data;
971 	u16 prod = rxr->rx_prod;
972 	struct sk_buff *skb;
973 	int off, err;
974 
975 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
976 	if (unlikely(err)) {
977 		bnxt_reuse_rx_data(rxr, cons, data);
978 		return NULL;
979 	}
980 	dma_addr -= bp->rx_dma_offset;
981 	dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
982 			     DMA_ATTR_WEAK_ORDERING);
983 	page_pool_release_page(rxr->page_pool, page);
984 
985 	if (unlikely(!payload))
986 		payload = eth_get_headlen(bp->dev, data_ptr, len);
987 
988 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
989 	if (!skb) {
990 		__free_page(page);
991 		return NULL;
992 	}
993 
994 	off = (void *)data_ptr - page_address(page);
995 	skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
996 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
997 	       payload + NET_IP_ALIGN);
998 
999 	frag = &skb_shinfo(skb)->frags[0];
1000 	skb_frag_size_sub(frag, payload);
1001 	skb_frag_off_add(frag, payload);
1002 	skb->data_len -= payload;
1003 	skb->tail += payload;
1004 
1005 	return skb;
1006 }
1007 
1008 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1009 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1010 				   void *data, u8 *data_ptr,
1011 				   dma_addr_t dma_addr,
1012 				   unsigned int offset_and_len)
1013 {
1014 	u16 prod = rxr->rx_prod;
1015 	struct sk_buff *skb;
1016 	int err;
1017 
1018 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1019 	if (unlikely(err)) {
1020 		bnxt_reuse_rx_data(rxr, cons, data);
1021 		return NULL;
1022 	}
1023 
1024 	skb = build_skb(data, 0);
1025 	dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1026 			       bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
1027 	if (!skb) {
1028 		kfree(data);
1029 		return NULL;
1030 	}
1031 
1032 	skb_reserve(skb, bp->rx_offset);
1033 	skb_put(skb, offset_and_len & 0xffff);
1034 	return skb;
1035 }
1036 
1037 static struct sk_buff *bnxt_rx_pages(struct bnxt *bp,
1038 				     struct bnxt_cp_ring_info *cpr,
1039 				     struct sk_buff *skb, u16 idx,
1040 				     u32 agg_bufs, bool tpa)
1041 {
1042 	struct bnxt_napi *bnapi = cpr->bnapi;
1043 	struct pci_dev *pdev = bp->pdev;
1044 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1045 	u16 prod = rxr->rx_agg_prod;
1046 	bool p5_tpa = false;
1047 	u32 i;
1048 
1049 	if ((bp->flags & BNXT_FLAG_CHIP_P5) && tpa)
1050 		p5_tpa = true;
1051 
1052 	for (i = 0; i < agg_bufs; i++) {
1053 		u16 cons, frag_len;
1054 		struct rx_agg_cmp *agg;
1055 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1056 		struct page *page;
1057 		dma_addr_t mapping;
1058 
1059 		if (p5_tpa)
1060 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1061 		else
1062 			agg = bnxt_get_agg(bp, cpr, idx, i);
1063 		cons = agg->rx_agg_cmp_opaque;
1064 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1065 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1066 
1067 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1068 		skb_fill_page_desc(skb, i, cons_rx_buf->page,
1069 				   cons_rx_buf->offset, frag_len);
1070 		__clear_bit(cons, rxr->rx_agg_bmap);
1071 
1072 		/* It is possible for bnxt_alloc_rx_page() to allocate
1073 		 * a sw_prod index that equals the cons index, so we
1074 		 * need to clear the cons entry now.
1075 		 */
1076 		mapping = cons_rx_buf->mapping;
1077 		page = cons_rx_buf->page;
1078 		cons_rx_buf->page = NULL;
1079 
1080 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1081 			struct skb_shared_info *shinfo;
1082 			unsigned int nr_frags;
1083 
1084 			shinfo = skb_shinfo(skb);
1085 			nr_frags = --shinfo->nr_frags;
1086 			__skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
1087 
1088 			dev_kfree_skb(skb);
1089 
1090 			cons_rx_buf->page = page;
1091 
1092 			/* Update prod since possibly some pages have been
1093 			 * allocated already.
1094 			 */
1095 			rxr->rx_agg_prod = prod;
1096 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1097 			return NULL;
1098 		}
1099 
1100 		dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1101 				     DMA_FROM_DEVICE,
1102 				     DMA_ATTR_WEAK_ORDERING);
1103 
1104 		skb->data_len += frag_len;
1105 		skb->len += frag_len;
1106 		skb->truesize += PAGE_SIZE;
1107 
1108 		prod = NEXT_RX_AGG(prod);
1109 	}
1110 	rxr->rx_agg_prod = prod;
1111 	return skb;
1112 }
1113 
1114 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1115 			       u8 agg_bufs, u32 *raw_cons)
1116 {
1117 	u16 last;
1118 	struct rx_agg_cmp *agg;
1119 
1120 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1121 	last = RING_CMP(*raw_cons);
1122 	agg = (struct rx_agg_cmp *)
1123 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1124 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1125 }
1126 
1127 static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1128 					    unsigned int len,
1129 					    dma_addr_t mapping)
1130 {
1131 	struct bnxt *bp = bnapi->bp;
1132 	struct pci_dev *pdev = bp->pdev;
1133 	struct sk_buff *skb;
1134 
1135 	skb = napi_alloc_skb(&bnapi->napi, len);
1136 	if (!skb)
1137 		return NULL;
1138 
1139 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1140 				bp->rx_dir);
1141 
1142 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1143 	       len + NET_IP_ALIGN);
1144 
1145 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1146 				   bp->rx_dir);
1147 
1148 	skb_put(skb, len);
1149 	return skb;
1150 }
1151 
1152 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1153 			   u32 *raw_cons, void *cmp)
1154 {
1155 	struct rx_cmp *rxcmp = cmp;
1156 	u32 tmp_raw_cons = *raw_cons;
1157 	u8 cmp_type, agg_bufs = 0;
1158 
1159 	cmp_type = RX_CMP_TYPE(rxcmp);
1160 
1161 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1162 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1163 			    RX_CMP_AGG_BUFS) >>
1164 			   RX_CMP_AGG_BUFS_SHIFT;
1165 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1166 		struct rx_tpa_end_cmp *tpa_end = cmp;
1167 
1168 		if (bp->flags & BNXT_FLAG_CHIP_P5)
1169 			return 0;
1170 
1171 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1172 	}
1173 
1174 	if (agg_bufs) {
1175 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1176 			return -EBUSY;
1177 	}
1178 	*raw_cons = tmp_raw_cons;
1179 	return 0;
1180 }
1181 
1182 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
1183 {
1184 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
1185 		return;
1186 
1187 	if (BNXT_PF(bp))
1188 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
1189 	else
1190 		schedule_delayed_work(&bp->fw_reset_task, delay);
1191 }
1192 
1193 static void bnxt_queue_sp_work(struct bnxt *bp)
1194 {
1195 	if (BNXT_PF(bp))
1196 		queue_work(bnxt_pf_wq, &bp->sp_task);
1197 	else
1198 		schedule_work(&bp->sp_task);
1199 }
1200 
1201 static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
1202 {
1203 	if (!rxr->bnapi->in_reset) {
1204 		rxr->bnapi->in_reset = true;
1205 		if (bp->flags & BNXT_FLAG_CHIP_P5)
1206 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
1207 		else
1208 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
1209 		bnxt_queue_sp_work(bp);
1210 	}
1211 	rxr->rx_next_cons = 0xffff;
1212 }
1213 
1214 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1215 {
1216 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1217 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1218 
1219 	if (test_bit(idx, map->agg_idx_bmap))
1220 		idx = find_first_zero_bit(map->agg_idx_bmap,
1221 					  BNXT_AGG_IDX_BMAP_SIZE);
1222 	__set_bit(idx, map->agg_idx_bmap);
1223 	map->agg_id_tbl[agg_id] = idx;
1224 	return idx;
1225 }
1226 
1227 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1228 {
1229 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1230 
1231 	__clear_bit(idx, map->agg_idx_bmap);
1232 }
1233 
1234 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1235 {
1236 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1237 
1238 	return map->agg_id_tbl[agg_id];
1239 }
1240 
1241 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1242 			   struct rx_tpa_start_cmp *tpa_start,
1243 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1244 {
1245 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1246 	struct bnxt_tpa_info *tpa_info;
1247 	u16 cons, prod, agg_id;
1248 	struct rx_bd *prod_bd;
1249 	dma_addr_t mapping;
1250 
1251 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
1252 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1253 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1254 	} else {
1255 		agg_id = TPA_START_AGG_ID(tpa_start);
1256 	}
1257 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1258 	prod = rxr->rx_prod;
1259 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1260 	prod_rx_buf = &rxr->rx_buf_ring[prod];
1261 	tpa_info = &rxr->rx_tpa[agg_id];
1262 
1263 	if (unlikely(cons != rxr->rx_next_cons ||
1264 		     TPA_START_ERROR(tpa_start))) {
1265 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1266 			    cons, rxr->rx_next_cons,
1267 			    TPA_START_ERROR_CODE(tpa_start1));
1268 		bnxt_sched_reset(bp, rxr);
1269 		return;
1270 	}
1271 	/* Store cfa_code in tpa_info to use in tpa_end
1272 	 * completion processing.
1273 	 */
1274 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1275 	prod_rx_buf->data = tpa_info->data;
1276 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1277 
1278 	mapping = tpa_info->mapping;
1279 	prod_rx_buf->mapping = mapping;
1280 
1281 	prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1282 
1283 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1284 
1285 	tpa_info->data = cons_rx_buf->data;
1286 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1287 	cons_rx_buf->data = NULL;
1288 	tpa_info->mapping = cons_rx_buf->mapping;
1289 
1290 	tpa_info->len =
1291 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1292 				RX_TPA_START_CMP_LEN_SHIFT;
1293 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1294 		u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1295 
1296 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1297 		tpa_info->gso_type = SKB_GSO_TCPV4;
1298 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1299 		if (hash_type == 3 || TPA_START_IS_IPV6(tpa_start1))
1300 			tpa_info->gso_type = SKB_GSO_TCPV6;
1301 		tpa_info->rss_hash =
1302 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1303 	} else {
1304 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1305 		tpa_info->gso_type = 0;
1306 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1307 	}
1308 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1309 	tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1310 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1311 	tpa_info->agg_count = 0;
1312 
1313 	rxr->rx_prod = NEXT_RX(prod);
1314 	cons = NEXT_RX(cons);
1315 	rxr->rx_next_cons = NEXT_RX(cons);
1316 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1317 
1318 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1319 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1320 	cons_rx_buf->data = NULL;
1321 }
1322 
1323 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1324 {
1325 	if (agg_bufs)
1326 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1327 }
1328 
1329 #ifdef CONFIG_INET
1330 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1331 {
1332 	struct udphdr *uh = NULL;
1333 
1334 	if (ip_proto == htons(ETH_P_IP)) {
1335 		struct iphdr *iph = (struct iphdr *)skb->data;
1336 
1337 		if (iph->protocol == IPPROTO_UDP)
1338 			uh = (struct udphdr *)(iph + 1);
1339 	} else {
1340 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1341 
1342 		if (iph->nexthdr == IPPROTO_UDP)
1343 			uh = (struct udphdr *)(iph + 1);
1344 	}
1345 	if (uh) {
1346 		if (uh->check)
1347 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1348 		else
1349 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1350 	}
1351 }
1352 #endif
1353 
1354 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1355 					   int payload_off, int tcp_ts,
1356 					   struct sk_buff *skb)
1357 {
1358 #ifdef CONFIG_INET
1359 	struct tcphdr *th;
1360 	int len, nw_off;
1361 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1362 	u32 hdr_info = tpa_info->hdr_info;
1363 	bool loopback = false;
1364 
1365 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1366 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1367 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1368 
1369 	/* If the packet is an internal loopback packet, the offsets will
1370 	 * have an extra 4 bytes.
1371 	 */
1372 	if (inner_mac_off == 4) {
1373 		loopback = true;
1374 	} else if (inner_mac_off > 4) {
1375 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1376 					    ETH_HLEN - 2));
1377 
1378 		/* We only support inner iPv4/ipv6.  If we don't see the
1379 		 * correct protocol ID, it must be a loopback packet where
1380 		 * the offsets are off by 4.
1381 		 */
1382 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1383 			loopback = true;
1384 	}
1385 	if (loopback) {
1386 		/* internal loopback packet, subtract all offsets by 4 */
1387 		inner_ip_off -= 4;
1388 		inner_mac_off -= 4;
1389 		outer_ip_off -= 4;
1390 	}
1391 
1392 	nw_off = inner_ip_off - ETH_HLEN;
1393 	skb_set_network_header(skb, nw_off);
1394 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1395 		struct ipv6hdr *iph = ipv6_hdr(skb);
1396 
1397 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1398 		len = skb->len - skb_transport_offset(skb);
1399 		th = tcp_hdr(skb);
1400 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1401 	} else {
1402 		struct iphdr *iph = ip_hdr(skb);
1403 
1404 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1405 		len = skb->len - skb_transport_offset(skb);
1406 		th = tcp_hdr(skb);
1407 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1408 	}
1409 
1410 	if (inner_mac_off) { /* tunnel */
1411 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1412 					    ETH_HLEN - 2));
1413 
1414 		bnxt_gro_tunnel(skb, proto);
1415 	}
1416 #endif
1417 	return skb;
1418 }
1419 
1420 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1421 					   int payload_off, int tcp_ts,
1422 					   struct sk_buff *skb)
1423 {
1424 #ifdef CONFIG_INET
1425 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1426 	u32 hdr_info = tpa_info->hdr_info;
1427 	int iphdr_len, nw_off;
1428 
1429 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1430 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1431 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1432 
1433 	nw_off = inner_ip_off - ETH_HLEN;
1434 	skb_set_network_header(skb, nw_off);
1435 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1436 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1437 	skb_set_transport_header(skb, nw_off + iphdr_len);
1438 
1439 	if (inner_mac_off) { /* tunnel */
1440 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1441 					    ETH_HLEN - 2));
1442 
1443 		bnxt_gro_tunnel(skb, proto);
1444 	}
1445 #endif
1446 	return skb;
1447 }
1448 
1449 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1450 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1451 
1452 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1453 					   int payload_off, int tcp_ts,
1454 					   struct sk_buff *skb)
1455 {
1456 #ifdef CONFIG_INET
1457 	struct tcphdr *th;
1458 	int len, nw_off, tcp_opt_len = 0;
1459 
1460 	if (tcp_ts)
1461 		tcp_opt_len = 12;
1462 
1463 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1464 		struct iphdr *iph;
1465 
1466 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1467 			 ETH_HLEN;
1468 		skb_set_network_header(skb, nw_off);
1469 		iph = ip_hdr(skb);
1470 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1471 		len = skb->len - skb_transport_offset(skb);
1472 		th = tcp_hdr(skb);
1473 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1474 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1475 		struct ipv6hdr *iph;
1476 
1477 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1478 			 ETH_HLEN;
1479 		skb_set_network_header(skb, nw_off);
1480 		iph = ipv6_hdr(skb);
1481 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1482 		len = skb->len - skb_transport_offset(skb);
1483 		th = tcp_hdr(skb);
1484 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1485 	} else {
1486 		dev_kfree_skb_any(skb);
1487 		return NULL;
1488 	}
1489 
1490 	if (nw_off) /* tunnel */
1491 		bnxt_gro_tunnel(skb, skb->protocol);
1492 #endif
1493 	return skb;
1494 }
1495 
1496 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1497 					   struct bnxt_tpa_info *tpa_info,
1498 					   struct rx_tpa_end_cmp *tpa_end,
1499 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1500 					   struct sk_buff *skb)
1501 {
1502 #ifdef CONFIG_INET
1503 	int payload_off;
1504 	u16 segs;
1505 
1506 	segs = TPA_END_TPA_SEGS(tpa_end);
1507 	if (segs == 1)
1508 		return skb;
1509 
1510 	NAPI_GRO_CB(skb)->count = segs;
1511 	skb_shinfo(skb)->gso_size =
1512 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1513 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1514 	if (bp->flags & BNXT_FLAG_CHIP_P5)
1515 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1516 	else
1517 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1518 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1519 	if (likely(skb))
1520 		tcp_gro_complete(skb);
1521 #endif
1522 	return skb;
1523 }
1524 
1525 /* Given the cfa_code of a received packet determine which
1526  * netdev (vf-rep or PF) the packet is destined to.
1527  */
1528 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1529 {
1530 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1531 
1532 	/* if vf-rep dev is NULL, the must belongs to the PF */
1533 	return dev ? dev : bp->dev;
1534 }
1535 
1536 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1537 					   struct bnxt_cp_ring_info *cpr,
1538 					   u32 *raw_cons,
1539 					   struct rx_tpa_end_cmp *tpa_end,
1540 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1541 					   u8 *event)
1542 {
1543 	struct bnxt_napi *bnapi = cpr->bnapi;
1544 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1545 	u8 *data_ptr, agg_bufs;
1546 	unsigned int len;
1547 	struct bnxt_tpa_info *tpa_info;
1548 	dma_addr_t mapping;
1549 	struct sk_buff *skb;
1550 	u16 idx = 0, agg_id;
1551 	void *data;
1552 	bool gro;
1553 
1554 	if (unlikely(bnapi->in_reset)) {
1555 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1556 
1557 		if (rc < 0)
1558 			return ERR_PTR(-EBUSY);
1559 		return NULL;
1560 	}
1561 
1562 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
1563 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1564 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1565 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1566 		tpa_info = &rxr->rx_tpa[agg_id];
1567 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1568 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1569 				    agg_bufs, tpa_info->agg_count);
1570 			agg_bufs = tpa_info->agg_count;
1571 		}
1572 		tpa_info->agg_count = 0;
1573 		*event |= BNXT_AGG_EVENT;
1574 		bnxt_free_agg_idx(rxr, agg_id);
1575 		idx = agg_id;
1576 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1577 	} else {
1578 		agg_id = TPA_END_AGG_ID(tpa_end);
1579 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1580 		tpa_info = &rxr->rx_tpa[agg_id];
1581 		idx = RING_CMP(*raw_cons);
1582 		if (agg_bufs) {
1583 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1584 				return ERR_PTR(-EBUSY);
1585 
1586 			*event |= BNXT_AGG_EVENT;
1587 			idx = NEXT_CMP(idx);
1588 		}
1589 		gro = !!TPA_END_GRO(tpa_end);
1590 	}
1591 	data = tpa_info->data;
1592 	data_ptr = tpa_info->data_ptr;
1593 	prefetch(data_ptr);
1594 	len = tpa_info->len;
1595 	mapping = tpa_info->mapping;
1596 
1597 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1598 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1599 		if (agg_bufs > MAX_SKB_FRAGS)
1600 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1601 				    agg_bufs, (int)MAX_SKB_FRAGS);
1602 		return NULL;
1603 	}
1604 
1605 	if (len <= bp->rx_copy_thresh) {
1606 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1607 		if (!skb) {
1608 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1609 			cpr->sw_stats.rx.rx_oom_discards += 1;
1610 			return NULL;
1611 		}
1612 	} else {
1613 		u8 *new_data;
1614 		dma_addr_t new_mapping;
1615 
1616 		new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1617 		if (!new_data) {
1618 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1619 			cpr->sw_stats.rx.rx_oom_discards += 1;
1620 			return NULL;
1621 		}
1622 
1623 		tpa_info->data = new_data;
1624 		tpa_info->data_ptr = new_data + bp->rx_offset;
1625 		tpa_info->mapping = new_mapping;
1626 
1627 		skb = build_skb(data, 0);
1628 		dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1629 				       bp->rx_buf_use_size, bp->rx_dir,
1630 				       DMA_ATTR_WEAK_ORDERING);
1631 
1632 		if (!skb) {
1633 			kfree(data);
1634 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1635 			cpr->sw_stats.rx.rx_oom_discards += 1;
1636 			return NULL;
1637 		}
1638 		skb_reserve(skb, bp->rx_offset);
1639 		skb_put(skb, len);
1640 	}
1641 
1642 	if (agg_bufs) {
1643 		skb = bnxt_rx_pages(bp, cpr, skb, idx, agg_bufs, true);
1644 		if (!skb) {
1645 			/* Page reuse already handled by bnxt_rx_pages(). */
1646 			cpr->sw_stats.rx.rx_oom_discards += 1;
1647 			return NULL;
1648 		}
1649 	}
1650 
1651 	skb->protocol =
1652 		eth_type_trans(skb, bnxt_get_pkt_dev(bp, tpa_info->cfa_code));
1653 
1654 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1655 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1656 
1657 	if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1658 	    (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1659 		__be16 vlan_proto = htons(tpa_info->metadata >>
1660 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1661 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1662 
1663 		if (eth_type_vlan(vlan_proto)) {
1664 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1665 		} else {
1666 			dev_kfree_skb(skb);
1667 			return NULL;
1668 		}
1669 	}
1670 
1671 	skb_checksum_none_assert(skb);
1672 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1673 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1674 		skb->csum_level =
1675 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1676 	}
1677 
1678 	if (gro)
1679 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1680 
1681 	return skb;
1682 }
1683 
1684 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1685 			 struct rx_agg_cmp *rx_agg)
1686 {
1687 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1688 	struct bnxt_tpa_info *tpa_info;
1689 
1690 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1691 	tpa_info = &rxr->rx_tpa[agg_id];
1692 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1693 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1694 }
1695 
1696 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1697 			     struct sk_buff *skb)
1698 {
1699 	if (skb->dev != bp->dev) {
1700 		/* this packet belongs to a vf-rep */
1701 		bnxt_vf_rep_rx(bp, skb);
1702 		return;
1703 	}
1704 	skb_record_rx_queue(skb, bnapi->index);
1705 	napi_gro_receive(&bnapi->napi, skb);
1706 }
1707 
1708 /* returns the following:
1709  * 1       - 1 packet successfully received
1710  * 0       - successful TPA_START, packet not completed yet
1711  * -EBUSY  - completion ring does not have all the agg buffers yet
1712  * -ENOMEM - packet aborted due to out of memory
1713  * -EIO    - packet aborted due to hw error indicated in BD
1714  */
1715 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1716 		       u32 *raw_cons, u8 *event)
1717 {
1718 	struct bnxt_napi *bnapi = cpr->bnapi;
1719 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1720 	struct net_device *dev = bp->dev;
1721 	struct rx_cmp *rxcmp;
1722 	struct rx_cmp_ext *rxcmp1;
1723 	u32 tmp_raw_cons = *raw_cons;
1724 	u16 cfa_code, cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1725 	struct bnxt_sw_rx_bd *rx_buf;
1726 	unsigned int len;
1727 	u8 *data_ptr, agg_bufs, cmp_type;
1728 	dma_addr_t dma_addr;
1729 	struct sk_buff *skb;
1730 	u32 flags, misc;
1731 	void *data;
1732 	int rc = 0;
1733 
1734 	rxcmp = (struct rx_cmp *)
1735 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1736 
1737 	cmp_type = RX_CMP_TYPE(rxcmp);
1738 
1739 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
1740 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
1741 		goto next_rx_no_prod_no_len;
1742 	}
1743 
1744 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1745 	cp_cons = RING_CMP(tmp_raw_cons);
1746 	rxcmp1 = (struct rx_cmp_ext *)
1747 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1748 
1749 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1750 		return -EBUSY;
1751 
1752 	/* The valid test of the entry must be done first before
1753 	 * reading any further.
1754 	 */
1755 	dma_rmb();
1756 	prod = rxr->rx_prod;
1757 
1758 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1759 		bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1760 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
1761 
1762 		*event |= BNXT_RX_EVENT;
1763 		goto next_rx_no_prod_no_len;
1764 
1765 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1766 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
1767 				   (struct rx_tpa_end_cmp *)rxcmp,
1768 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
1769 
1770 		if (IS_ERR(skb))
1771 			return -EBUSY;
1772 
1773 		rc = -ENOMEM;
1774 		if (likely(skb)) {
1775 			bnxt_deliver_skb(bp, bnapi, skb);
1776 			rc = 1;
1777 		}
1778 		*event |= BNXT_RX_EVENT;
1779 		goto next_rx_no_prod_no_len;
1780 	}
1781 
1782 	cons = rxcmp->rx_cmp_opaque;
1783 	if (unlikely(cons != rxr->rx_next_cons)) {
1784 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
1785 
1786 		/* 0xffff is forced error, don't print it */
1787 		if (rxr->rx_next_cons != 0xffff)
1788 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
1789 				    cons, rxr->rx_next_cons);
1790 		bnxt_sched_reset(bp, rxr);
1791 		if (rc1)
1792 			return rc1;
1793 		goto next_rx_no_prod_no_len;
1794 	}
1795 	rx_buf = &rxr->rx_buf_ring[cons];
1796 	data = rx_buf->data;
1797 	data_ptr = rx_buf->data_ptr;
1798 	prefetch(data_ptr);
1799 
1800 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1801 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
1802 
1803 	if (agg_bufs) {
1804 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1805 			return -EBUSY;
1806 
1807 		cp_cons = NEXT_CMP(cp_cons);
1808 		*event |= BNXT_AGG_EVENT;
1809 	}
1810 	*event |= BNXT_RX_EVENT;
1811 
1812 	rx_buf->data = NULL;
1813 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1814 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
1815 
1816 		bnxt_reuse_rx_data(rxr, cons, data);
1817 		if (agg_bufs)
1818 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
1819 					       false);
1820 
1821 		rc = -EIO;
1822 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
1823 			bnapi->cp_ring.sw_stats.rx.rx_buf_errors++;
1824 			if (!(bp->flags & BNXT_FLAG_CHIP_P5) &&
1825 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
1826 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
1827 						 rx_err);
1828 				bnxt_sched_reset(bp, rxr);
1829 			}
1830 		}
1831 		goto next_rx_no_len;
1832 	}
1833 
1834 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
1835 	len = flags >> RX_CMP_LEN_SHIFT;
1836 	dma_addr = rx_buf->mapping;
1837 
1838 	if (bnxt_rx_xdp(bp, rxr, cons, data, &data_ptr, &len, event)) {
1839 		rc = 1;
1840 		goto next_rx;
1841 	}
1842 
1843 	if (len <= bp->rx_copy_thresh) {
1844 		skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
1845 		bnxt_reuse_rx_data(rxr, cons, data);
1846 		if (!skb) {
1847 			if (agg_bufs)
1848 				bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
1849 						       agg_bufs, false);
1850 			cpr->sw_stats.rx.rx_oom_discards += 1;
1851 			rc = -ENOMEM;
1852 			goto next_rx;
1853 		}
1854 	} else {
1855 		u32 payload;
1856 
1857 		if (rx_buf->data_ptr == data_ptr)
1858 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
1859 		else
1860 			payload = 0;
1861 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
1862 				      payload | len);
1863 		if (!skb) {
1864 			cpr->sw_stats.rx.rx_oom_discards += 1;
1865 			rc = -ENOMEM;
1866 			goto next_rx;
1867 		}
1868 	}
1869 
1870 	if (agg_bufs) {
1871 		skb = bnxt_rx_pages(bp, cpr, skb, cp_cons, agg_bufs, false);
1872 		if (!skb) {
1873 			cpr->sw_stats.rx.rx_oom_discards += 1;
1874 			rc = -ENOMEM;
1875 			goto next_rx;
1876 		}
1877 	}
1878 
1879 	if (RX_CMP_HASH_VALID(rxcmp)) {
1880 		u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1881 		enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1882 
1883 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1884 		if (hash_type != 1 && hash_type != 3)
1885 			type = PKT_HASH_TYPE_L3;
1886 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1887 	}
1888 
1889 	cfa_code = RX_CMP_CFA_CODE(rxcmp1);
1890 	skb->protocol = eth_type_trans(skb, bnxt_get_pkt_dev(bp, cfa_code));
1891 
1892 	if ((rxcmp1->rx_cmp_flags2 &
1893 	     cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1894 	    (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1895 		u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1896 		u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1897 		__be16 vlan_proto = htons(meta_data >>
1898 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1899 
1900 		if (eth_type_vlan(vlan_proto)) {
1901 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1902 		} else {
1903 			dev_kfree_skb(skb);
1904 			goto next_rx;
1905 		}
1906 	}
1907 
1908 	skb_checksum_none_assert(skb);
1909 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
1910 		if (dev->features & NETIF_F_RXCSUM) {
1911 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1912 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1913 		}
1914 	} else {
1915 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1916 			if (dev->features & NETIF_F_RXCSUM)
1917 				bnapi->cp_ring.sw_stats.rx.rx_l4_csum_errors++;
1918 		}
1919 	}
1920 
1921 	if (unlikely((flags & RX_CMP_FLAGS_ITYPES_MASK) ==
1922 		     RX_CMP_FLAGS_ITYPE_PTP_W_TS)) {
1923 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
1924 			u32 cmpl_ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
1925 			u64 ns, ts;
1926 
1927 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
1928 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
1929 
1930 				spin_lock_bh(&ptp->ptp_lock);
1931 				ns = timecounter_cyc2time(&ptp->tc, ts);
1932 				spin_unlock_bh(&ptp->ptp_lock);
1933 				memset(skb_hwtstamps(skb), 0,
1934 				       sizeof(*skb_hwtstamps(skb)));
1935 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
1936 			}
1937 		}
1938 	}
1939 	bnxt_deliver_skb(bp, bnapi, skb);
1940 	rc = 1;
1941 
1942 next_rx:
1943 	cpr->rx_packets += 1;
1944 	cpr->rx_bytes += len;
1945 
1946 next_rx_no_len:
1947 	rxr->rx_prod = NEXT_RX(prod);
1948 	rxr->rx_next_cons = NEXT_RX(cons);
1949 
1950 next_rx_no_prod_no_len:
1951 	*raw_cons = tmp_raw_cons;
1952 
1953 	return rc;
1954 }
1955 
1956 /* In netpoll mode, if we are using a combined completion ring, we need to
1957  * discard the rx packets and recycle the buffers.
1958  */
1959 static int bnxt_force_rx_discard(struct bnxt *bp,
1960 				 struct bnxt_cp_ring_info *cpr,
1961 				 u32 *raw_cons, u8 *event)
1962 {
1963 	u32 tmp_raw_cons = *raw_cons;
1964 	struct rx_cmp_ext *rxcmp1;
1965 	struct rx_cmp *rxcmp;
1966 	u16 cp_cons;
1967 	u8 cmp_type;
1968 	int rc;
1969 
1970 	cp_cons = RING_CMP(tmp_raw_cons);
1971 	rxcmp = (struct rx_cmp *)
1972 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1973 
1974 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1975 	cp_cons = RING_CMP(tmp_raw_cons);
1976 	rxcmp1 = (struct rx_cmp_ext *)
1977 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1978 
1979 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1980 		return -EBUSY;
1981 
1982 	/* The valid test of the entry must be done first before
1983 	 * reading any further.
1984 	 */
1985 	dma_rmb();
1986 	cmp_type = RX_CMP_TYPE(rxcmp);
1987 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1988 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1989 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1990 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1991 		struct rx_tpa_end_cmp_ext *tpa_end1;
1992 
1993 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
1994 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
1995 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
1996 	}
1997 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
1998 	if (rc && rc != -EBUSY)
1999 		cpr->sw_stats.rx.rx_netpoll_discards += 1;
2000 	return rc;
2001 }
2002 
2003 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2004 {
2005 	struct bnxt_fw_health *fw_health = bp->fw_health;
2006 	u32 reg = fw_health->regs[reg_idx];
2007 	u32 reg_type, reg_off, val = 0;
2008 
2009 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2010 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2011 	switch (reg_type) {
2012 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2013 		pci_read_config_dword(bp->pdev, reg_off, &val);
2014 		break;
2015 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2016 		reg_off = fw_health->mapped_regs[reg_idx];
2017 		fallthrough;
2018 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2019 		val = readl(bp->bar0 + reg_off);
2020 		break;
2021 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2022 		val = readl(bp->bar1 + reg_off);
2023 		break;
2024 	}
2025 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2026 		val &= fw_health->fw_reset_inprog_reg_mask;
2027 	return val;
2028 }
2029 
2030 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2031 {
2032 	int i;
2033 
2034 	for (i = 0; i < bp->rx_nr_rings; i++) {
2035 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2036 		struct bnxt_ring_grp_info *grp_info;
2037 
2038 		grp_info = &bp->grp_info[grp_idx];
2039 		if (grp_info->agg_fw_ring_id == ring_id)
2040 			return grp_idx;
2041 	}
2042 	return INVALID_HW_RING_ID;
2043 }
2044 
2045 static void bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2046 {
2047 	switch (BNXT_EVENT_ERROR_REPORT_TYPE(data1)) {
2048 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2049 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2050 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2051 		break;
2052 	default:
2053 		netdev_err(bp->dev, "FW reported unknown error type\n");
2054 		break;
2055 	}
2056 }
2057 
2058 #define BNXT_GET_EVENT_PORT(data)	\
2059 	((data) &			\
2060 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2061 
2062 #define BNXT_EVENT_RING_TYPE(data2)	\
2063 	((data2) &			\
2064 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2065 
2066 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2067 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2068 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2069 
2070 static int bnxt_async_event_process(struct bnxt *bp,
2071 				    struct hwrm_async_event_cmpl *cmpl)
2072 {
2073 	u16 event_id = le16_to_cpu(cmpl->event_id);
2074 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2075 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2076 
2077 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2078 	switch (event_id) {
2079 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2080 		struct bnxt_link_info *link_info = &bp->link_info;
2081 
2082 		if (BNXT_VF(bp))
2083 			goto async_event_process_exit;
2084 
2085 		/* print unsupported speed warning in forced speed mode only */
2086 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2087 		    (data1 & 0x20000)) {
2088 			u16 fw_speed = link_info->force_link_speed;
2089 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2090 
2091 			if (speed != SPEED_UNKNOWN)
2092 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2093 					    speed);
2094 		}
2095 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2096 	}
2097 		fallthrough;
2098 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2099 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2100 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2101 		fallthrough;
2102 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2103 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2104 		break;
2105 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2106 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2107 		break;
2108 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2109 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2110 
2111 		if (BNXT_VF(bp))
2112 			break;
2113 
2114 		if (bp->pf.port_id != port_id)
2115 			break;
2116 
2117 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2118 		break;
2119 	}
2120 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2121 		if (BNXT_PF(bp))
2122 			goto async_event_process_exit;
2123 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2124 		break;
2125 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2126 		char *type_str = "Solicited";
2127 
2128 		if (!bp->fw_health)
2129 			goto async_event_process_exit;
2130 
2131 		bp->fw_reset_timestamp = jiffies;
2132 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2133 		if (!bp->fw_reset_min_dsecs)
2134 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2135 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2136 		if (!bp->fw_reset_max_dsecs)
2137 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2138 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2139 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2140 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2141 			type_str = "Fatal";
2142 			bp->fw_health->fatalities++;
2143 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2144 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2145 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2146 			type_str = "Non-fatal";
2147 			bp->fw_health->survivals++;
2148 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2149 		}
2150 		netif_warn(bp, hw, bp->dev,
2151 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2152 			   type_str, data1, data2,
2153 			   bp->fw_reset_min_dsecs * 100,
2154 			   bp->fw_reset_max_dsecs * 100);
2155 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2156 		break;
2157 	}
2158 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2159 		struct bnxt_fw_health *fw_health = bp->fw_health;
2160 		char *status_desc = "healthy";
2161 		u32 status;
2162 
2163 		if (!fw_health)
2164 			goto async_event_process_exit;
2165 
2166 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2167 			fw_health->enabled = false;
2168 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2169 			break;
2170 		}
2171 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2172 		fw_health->tmr_multiplier =
2173 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2174 				     bp->current_interval * 10);
2175 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2176 		if (!fw_health->enabled)
2177 			fw_health->last_fw_heartbeat =
2178 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2179 		fw_health->last_fw_reset_cnt =
2180 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2181 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2182 		if (status != BNXT_FW_STATUS_HEALTHY)
2183 			status_desc = "unhealthy";
2184 		netif_info(bp, drv, bp->dev,
2185 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2186 			   fw_health->primary ? "primary" : "backup", status,
2187 			   status_desc, fw_health->last_fw_reset_cnt);
2188 		if (!fw_health->enabled) {
2189 			/* Make sure tmr_counter is set and visible to
2190 			 * bnxt_health_check() before setting enabled to true.
2191 			 */
2192 			smp_wmb();
2193 			fw_health->enabled = true;
2194 		}
2195 		goto async_event_process_exit;
2196 	}
2197 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2198 		netif_notice(bp, hw, bp->dev,
2199 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2200 			     data1, data2);
2201 		goto async_event_process_exit;
2202 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2203 		struct bnxt_rx_ring_info *rxr;
2204 		u16 grp_idx;
2205 
2206 		if (bp->flags & BNXT_FLAG_CHIP_P5)
2207 			goto async_event_process_exit;
2208 
2209 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2210 			    BNXT_EVENT_RING_TYPE(data2), data1);
2211 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2212 			goto async_event_process_exit;
2213 
2214 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2215 		if (grp_idx == INVALID_HW_RING_ID) {
2216 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2217 				    data1);
2218 			goto async_event_process_exit;
2219 		}
2220 		rxr = bp->bnapi[grp_idx]->rx_ring;
2221 		bnxt_sched_reset(bp, rxr);
2222 		goto async_event_process_exit;
2223 	}
2224 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2225 		struct bnxt_fw_health *fw_health = bp->fw_health;
2226 
2227 		netif_notice(bp, hw, bp->dev,
2228 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2229 			     data1, data2);
2230 		if (fw_health) {
2231 			fw_health->echo_req_data1 = data1;
2232 			fw_health->echo_req_data2 = data2;
2233 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2234 			break;
2235 		}
2236 		goto async_event_process_exit;
2237 	}
2238 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2239 		bnxt_ptp_pps_event(bp, data1, data2);
2240 		goto async_event_process_exit;
2241 	}
2242 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2243 		bnxt_event_error_report(bp, data1, data2);
2244 		goto async_event_process_exit;
2245 	}
2246 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2247 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2248 
2249 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2250 		goto async_event_process_exit;
2251 	}
2252 	default:
2253 		goto async_event_process_exit;
2254 	}
2255 	bnxt_queue_sp_work(bp);
2256 async_event_process_exit:
2257 	bnxt_ulp_async_events(bp, cmpl);
2258 	return 0;
2259 }
2260 
2261 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2262 {
2263 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2264 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2265 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2266 				(struct hwrm_fwd_req_cmpl *)txcmp;
2267 
2268 	switch (cmpl_type) {
2269 	case CMPL_BASE_TYPE_HWRM_DONE:
2270 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2271 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2272 		break;
2273 
2274 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2275 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2276 
2277 		if ((vf_id < bp->pf.first_vf_id) ||
2278 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2279 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2280 				   vf_id);
2281 			return -EINVAL;
2282 		}
2283 
2284 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2285 		set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
2286 		bnxt_queue_sp_work(bp);
2287 		break;
2288 
2289 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2290 		bnxt_async_event_process(bp,
2291 					 (struct hwrm_async_event_cmpl *)txcmp);
2292 		break;
2293 
2294 	default:
2295 		break;
2296 	}
2297 
2298 	return 0;
2299 }
2300 
2301 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2302 {
2303 	struct bnxt_napi *bnapi = dev_instance;
2304 	struct bnxt *bp = bnapi->bp;
2305 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2306 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2307 
2308 	cpr->event_ctr++;
2309 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2310 	napi_schedule(&bnapi->napi);
2311 	return IRQ_HANDLED;
2312 }
2313 
2314 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2315 {
2316 	u32 raw_cons = cpr->cp_raw_cons;
2317 	u16 cons = RING_CMP(raw_cons);
2318 	struct tx_cmp *txcmp;
2319 
2320 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2321 
2322 	return TX_CMP_VALID(txcmp, raw_cons);
2323 }
2324 
2325 static irqreturn_t bnxt_inta(int irq, void *dev_instance)
2326 {
2327 	struct bnxt_napi *bnapi = dev_instance;
2328 	struct bnxt *bp = bnapi->bp;
2329 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2330 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2331 	u32 int_status;
2332 
2333 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2334 
2335 	if (!bnxt_has_work(bp, cpr)) {
2336 		int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
2337 		/* return if erroneous interrupt */
2338 		if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
2339 			return IRQ_NONE;
2340 	}
2341 
2342 	/* disable ring IRQ */
2343 	BNXT_CP_DB_IRQ_DIS(cpr->cp_db.doorbell);
2344 
2345 	/* Return here if interrupt is shared and is disabled. */
2346 	if (unlikely(atomic_read(&bp->intr_sem) != 0))
2347 		return IRQ_HANDLED;
2348 
2349 	napi_schedule(&bnapi->napi);
2350 	return IRQ_HANDLED;
2351 }
2352 
2353 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2354 			    int budget)
2355 {
2356 	struct bnxt_napi *bnapi = cpr->bnapi;
2357 	u32 raw_cons = cpr->cp_raw_cons;
2358 	u32 cons;
2359 	int tx_pkts = 0;
2360 	int rx_pkts = 0;
2361 	u8 event = 0;
2362 	struct tx_cmp *txcmp;
2363 
2364 	cpr->has_more_work = 0;
2365 	cpr->had_work_done = 1;
2366 	while (1) {
2367 		int rc;
2368 
2369 		cons = RING_CMP(raw_cons);
2370 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2371 
2372 		if (!TX_CMP_VALID(txcmp, raw_cons))
2373 			break;
2374 
2375 		/* The valid test of the entry must be done first before
2376 		 * reading any further.
2377 		 */
2378 		dma_rmb();
2379 		if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
2380 			tx_pkts++;
2381 			/* return full budget so NAPI will complete. */
2382 			if (unlikely(tx_pkts >= bp->tx_wake_thresh)) {
2383 				rx_pkts = budget;
2384 				raw_cons = NEXT_RAW_CMP(raw_cons);
2385 				if (budget)
2386 					cpr->has_more_work = 1;
2387 				break;
2388 			}
2389 		} else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
2390 			if (likely(budget))
2391 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2392 			else
2393 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2394 							   &event);
2395 			if (likely(rc >= 0))
2396 				rx_pkts += rc;
2397 			/* Increment rx_pkts when rc is -ENOMEM to count towards
2398 			 * the NAPI budget.  Otherwise, we may potentially loop
2399 			 * here forever if we consistently cannot allocate
2400 			 * buffers.
2401 			 */
2402 			else if (rc == -ENOMEM && budget)
2403 				rx_pkts++;
2404 			else if (rc == -EBUSY)	/* partial completion */
2405 				break;
2406 		} else if (unlikely((TX_CMP_TYPE(txcmp) ==
2407 				     CMPL_BASE_TYPE_HWRM_DONE) ||
2408 				    (TX_CMP_TYPE(txcmp) ==
2409 				     CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
2410 				    (TX_CMP_TYPE(txcmp) ==
2411 				     CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
2412 			bnxt_hwrm_handler(bp, txcmp);
2413 		}
2414 		raw_cons = NEXT_RAW_CMP(raw_cons);
2415 
2416 		if (rx_pkts && rx_pkts == budget) {
2417 			cpr->has_more_work = 1;
2418 			break;
2419 		}
2420 	}
2421 
2422 	if (event & BNXT_REDIRECT_EVENT)
2423 		xdp_do_flush_map();
2424 
2425 	if (event & BNXT_TX_EVENT) {
2426 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
2427 		u16 prod = txr->tx_prod;
2428 
2429 		/* Sync BD data before updating doorbell */
2430 		wmb();
2431 
2432 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
2433 	}
2434 
2435 	cpr->cp_raw_cons = raw_cons;
2436 	bnapi->tx_pkts += tx_pkts;
2437 	bnapi->events |= event;
2438 	return rx_pkts;
2439 }
2440 
2441 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi)
2442 {
2443 	if (bnapi->tx_pkts) {
2444 		bnapi->tx_int(bp, bnapi, bnapi->tx_pkts);
2445 		bnapi->tx_pkts = 0;
2446 	}
2447 
2448 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
2449 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2450 
2451 		if (bnapi->events & BNXT_AGG_EVENT)
2452 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2453 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2454 	}
2455 	bnapi->events = 0;
2456 }
2457 
2458 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2459 			  int budget)
2460 {
2461 	struct bnxt_napi *bnapi = cpr->bnapi;
2462 	int rx_pkts;
2463 
2464 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
2465 
2466 	/* ACK completion ring before freeing tx ring and producing new
2467 	 * buffers in rx/agg rings to prevent overflowing the completion
2468 	 * ring.
2469 	 */
2470 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
2471 
2472 	__bnxt_poll_work_done(bp, bnapi);
2473 	return rx_pkts;
2474 }
2475 
2476 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
2477 {
2478 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2479 	struct bnxt *bp = bnapi->bp;
2480 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2481 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2482 	struct tx_cmp *txcmp;
2483 	struct rx_cmp_ext *rxcmp1;
2484 	u32 cp_cons, tmp_raw_cons;
2485 	u32 raw_cons = cpr->cp_raw_cons;
2486 	u32 rx_pkts = 0;
2487 	u8 event = 0;
2488 
2489 	while (1) {
2490 		int rc;
2491 
2492 		cp_cons = RING_CMP(raw_cons);
2493 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2494 
2495 		if (!TX_CMP_VALID(txcmp, raw_cons))
2496 			break;
2497 
2498 		/* The valid test of the entry must be done first before
2499 		 * reading any further.
2500 		 */
2501 		dma_rmb();
2502 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
2503 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
2504 			cp_cons = RING_CMP(tmp_raw_cons);
2505 			rxcmp1 = (struct rx_cmp_ext *)
2506 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2507 
2508 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2509 				break;
2510 
2511 			/* force an error to recycle the buffer */
2512 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2513 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2514 
2515 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2516 			if (likely(rc == -EIO) && budget)
2517 				rx_pkts++;
2518 			else if (rc == -EBUSY)	/* partial completion */
2519 				break;
2520 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
2521 				    CMPL_BASE_TYPE_HWRM_DONE)) {
2522 			bnxt_hwrm_handler(bp, txcmp);
2523 		} else {
2524 			netdev_err(bp->dev,
2525 				   "Invalid completion received on special ring\n");
2526 		}
2527 		raw_cons = NEXT_RAW_CMP(raw_cons);
2528 
2529 		if (rx_pkts == budget)
2530 			break;
2531 	}
2532 
2533 	cpr->cp_raw_cons = raw_cons;
2534 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
2535 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2536 
2537 	if (event & BNXT_AGG_EVENT)
2538 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2539 
2540 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
2541 		napi_complete_done(napi, rx_pkts);
2542 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2543 	}
2544 	return rx_pkts;
2545 }
2546 
2547 static int bnxt_poll(struct napi_struct *napi, int budget)
2548 {
2549 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2550 	struct bnxt *bp = bnapi->bp;
2551 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2552 	int work_done = 0;
2553 
2554 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
2555 		napi_complete(napi);
2556 		return 0;
2557 	}
2558 	while (1) {
2559 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
2560 
2561 		if (work_done >= budget) {
2562 			if (!budget)
2563 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2564 			break;
2565 		}
2566 
2567 		if (!bnxt_has_work(bp, cpr)) {
2568 			if (napi_complete_done(napi, work_done))
2569 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2570 			break;
2571 		}
2572 	}
2573 	if (bp->flags & BNXT_FLAG_DIM) {
2574 		struct dim_sample dim_sample = {};
2575 
2576 		dim_update_sample(cpr->event_ctr,
2577 				  cpr->rx_packets,
2578 				  cpr->rx_bytes,
2579 				  &dim_sample);
2580 		net_dim(&cpr->dim, dim_sample);
2581 	}
2582 	return work_done;
2583 }
2584 
2585 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
2586 {
2587 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2588 	int i, work_done = 0;
2589 
2590 	for (i = 0; i < 2; i++) {
2591 		struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i];
2592 
2593 		if (cpr2) {
2594 			work_done += __bnxt_poll_work(bp, cpr2,
2595 						      budget - work_done);
2596 			cpr->has_more_work |= cpr2->has_more_work;
2597 		}
2598 	}
2599 	return work_done;
2600 }
2601 
2602 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
2603 				 u64 dbr_type)
2604 {
2605 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2606 	int i;
2607 
2608 	for (i = 0; i < 2; i++) {
2609 		struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i];
2610 		struct bnxt_db_info *db;
2611 
2612 		if (cpr2 && cpr2->had_work_done) {
2613 			db = &cpr2->cp_db;
2614 			bnxt_writeq(bp, db->db_key64 | dbr_type |
2615 				    RING_CMP(cpr2->cp_raw_cons), db->doorbell);
2616 			cpr2->had_work_done = 0;
2617 		}
2618 	}
2619 	__bnxt_poll_work_done(bp, bnapi);
2620 }
2621 
2622 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
2623 {
2624 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2625 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2626 	u32 raw_cons = cpr->cp_raw_cons;
2627 	struct bnxt *bp = bnapi->bp;
2628 	struct nqe_cn *nqcmp;
2629 	int work_done = 0;
2630 	u32 cons;
2631 
2632 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
2633 		napi_complete(napi);
2634 		return 0;
2635 	}
2636 	if (cpr->has_more_work) {
2637 		cpr->has_more_work = 0;
2638 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
2639 	}
2640 	while (1) {
2641 		cons = RING_CMP(raw_cons);
2642 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2643 
2644 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
2645 			if (cpr->has_more_work)
2646 				break;
2647 
2648 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL);
2649 			cpr->cp_raw_cons = raw_cons;
2650 			if (napi_complete_done(napi, work_done))
2651 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
2652 						  cpr->cp_raw_cons);
2653 			return work_done;
2654 		}
2655 
2656 		/* The valid test of the entry must be done first before
2657 		 * reading any further.
2658 		 */
2659 		dma_rmb();
2660 
2661 		if (nqcmp->type == cpu_to_le16(NQ_CN_TYPE_CQ_NOTIFICATION)) {
2662 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
2663 			struct bnxt_cp_ring_info *cpr2;
2664 
2665 			cpr2 = cpr->cp_ring_arr[idx];
2666 			work_done += __bnxt_poll_work(bp, cpr2,
2667 						      budget - work_done);
2668 			cpr->has_more_work |= cpr2->has_more_work;
2669 		} else {
2670 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
2671 		}
2672 		raw_cons = NEXT_RAW_CMP(raw_cons);
2673 	}
2674 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ);
2675 	if (raw_cons != cpr->cp_raw_cons) {
2676 		cpr->cp_raw_cons = raw_cons;
2677 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
2678 	}
2679 	return work_done;
2680 }
2681 
2682 static void bnxt_free_tx_skbs(struct bnxt *bp)
2683 {
2684 	int i, max_idx;
2685 	struct pci_dev *pdev = bp->pdev;
2686 
2687 	if (!bp->tx_ring)
2688 		return;
2689 
2690 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
2691 	for (i = 0; i < bp->tx_nr_rings; i++) {
2692 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2693 		int j;
2694 
2695 		if (!txr->tx_buf_ring)
2696 			continue;
2697 
2698 		for (j = 0; j < max_idx;) {
2699 			struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
2700 			struct sk_buff *skb;
2701 			int k, last;
2702 
2703 			if (i < bp->tx_nr_rings_xdp &&
2704 			    tx_buf->action == XDP_REDIRECT) {
2705 				dma_unmap_single(&pdev->dev,
2706 					dma_unmap_addr(tx_buf, mapping),
2707 					dma_unmap_len(tx_buf, len),
2708 					DMA_TO_DEVICE);
2709 				xdp_return_frame(tx_buf->xdpf);
2710 				tx_buf->action = 0;
2711 				tx_buf->xdpf = NULL;
2712 				j++;
2713 				continue;
2714 			}
2715 
2716 			skb = tx_buf->skb;
2717 			if (!skb) {
2718 				j++;
2719 				continue;
2720 			}
2721 
2722 			tx_buf->skb = NULL;
2723 
2724 			if (tx_buf->is_push) {
2725 				dev_kfree_skb(skb);
2726 				j += 2;
2727 				continue;
2728 			}
2729 
2730 			dma_unmap_single(&pdev->dev,
2731 					 dma_unmap_addr(tx_buf, mapping),
2732 					 skb_headlen(skb),
2733 					 DMA_TO_DEVICE);
2734 
2735 			last = tx_buf->nr_frags;
2736 			j += 2;
2737 			for (k = 0; k < last; k++, j++) {
2738 				int ring_idx = j & bp->tx_ring_mask;
2739 				skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2740 
2741 				tx_buf = &txr->tx_buf_ring[ring_idx];
2742 				dma_unmap_page(
2743 					&pdev->dev,
2744 					dma_unmap_addr(tx_buf, mapping),
2745 					skb_frag_size(frag), DMA_TO_DEVICE);
2746 			}
2747 			dev_kfree_skb(skb);
2748 		}
2749 		netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
2750 	}
2751 }
2752 
2753 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, int ring_nr)
2754 {
2755 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
2756 	struct pci_dev *pdev = bp->pdev;
2757 	struct bnxt_tpa_idx_map *map;
2758 	int i, max_idx, max_agg_idx;
2759 
2760 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
2761 	max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
2762 	if (!rxr->rx_tpa)
2763 		goto skip_rx_tpa_free;
2764 
2765 	for (i = 0; i < bp->max_tpa; i++) {
2766 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
2767 		u8 *data = tpa_info->data;
2768 
2769 		if (!data)
2770 			continue;
2771 
2772 		dma_unmap_single_attrs(&pdev->dev, tpa_info->mapping,
2773 				       bp->rx_buf_use_size, bp->rx_dir,
2774 				       DMA_ATTR_WEAK_ORDERING);
2775 
2776 		tpa_info->data = NULL;
2777 
2778 		kfree(data);
2779 	}
2780 
2781 skip_rx_tpa_free:
2782 	if (!rxr->rx_buf_ring)
2783 		goto skip_rx_buf_free;
2784 
2785 	for (i = 0; i < max_idx; i++) {
2786 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
2787 		dma_addr_t mapping = rx_buf->mapping;
2788 		void *data = rx_buf->data;
2789 
2790 		if (!data)
2791 			continue;
2792 
2793 		rx_buf->data = NULL;
2794 		if (BNXT_RX_PAGE_MODE(bp)) {
2795 			mapping -= bp->rx_dma_offset;
2796 			dma_unmap_page_attrs(&pdev->dev, mapping, PAGE_SIZE,
2797 					     bp->rx_dir,
2798 					     DMA_ATTR_WEAK_ORDERING);
2799 			page_pool_recycle_direct(rxr->page_pool, data);
2800 		} else {
2801 			dma_unmap_single_attrs(&pdev->dev, mapping,
2802 					       bp->rx_buf_use_size, bp->rx_dir,
2803 					       DMA_ATTR_WEAK_ORDERING);
2804 			kfree(data);
2805 		}
2806 	}
2807 
2808 skip_rx_buf_free:
2809 	if (!rxr->rx_agg_ring)
2810 		goto skip_rx_agg_free;
2811 
2812 	for (i = 0; i < max_agg_idx; i++) {
2813 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
2814 		struct page *page = rx_agg_buf->page;
2815 
2816 		if (!page)
2817 			continue;
2818 
2819 		dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
2820 				     BNXT_RX_PAGE_SIZE, DMA_FROM_DEVICE,
2821 				     DMA_ATTR_WEAK_ORDERING);
2822 
2823 		rx_agg_buf->page = NULL;
2824 		__clear_bit(i, rxr->rx_agg_bmap);
2825 
2826 		__free_page(page);
2827 	}
2828 
2829 skip_rx_agg_free:
2830 	if (rxr->rx_page) {
2831 		__free_page(rxr->rx_page);
2832 		rxr->rx_page = NULL;
2833 	}
2834 	map = rxr->rx_tpa_idx_map;
2835 	if (map)
2836 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
2837 }
2838 
2839 static void bnxt_free_rx_skbs(struct bnxt *bp)
2840 {
2841 	int i;
2842 
2843 	if (!bp->rx_ring)
2844 		return;
2845 
2846 	for (i = 0; i < bp->rx_nr_rings; i++)
2847 		bnxt_free_one_rx_ring_skbs(bp, i);
2848 }
2849 
2850 static void bnxt_free_skbs(struct bnxt *bp)
2851 {
2852 	bnxt_free_tx_skbs(bp);
2853 	bnxt_free_rx_skbs(bp);
2854 }
2855 
2856 static void bnxt_init_ctx_mem(struct bnxt_mem_init *mem_init, void *p, int len)
2857 {
2858 	u8 init_val = mem_init->init_val;
2859 	u16 offset = mem_init->offset;
2860 	u8 *p2 = p;
2861 	int i;
2862 
2863 	if (!init_val)
2864 		return;
2865 	if (offset == BNXT_MEM_INVALID_OFFSET) {
2866 		memset(p, init_val, len);
2867 		return;
2868 	}
2869 	for (i = 0; i < len; i += mem_init->size)
2870 		*(p2 + i + offset) = init_val;
2871 }
2872 
2873 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
2874 {
2875 	struct pci_dev *pdev = bp->pdev;
2876 	int i;
2877 
2878 	if (!rmem->pg_arr)
2879 		goto skip_pages;
2880 
2881 	for (i = 0; i < rmem->nr_pages; i++) {
2882 		if (!rmem->pg_arr[i])
2883 			continue;
2884 
2885 		dma_free_coherent(&pdev->dev, rmem->page_size,
2886 				  rmem->pg_arr[i], rmem->dma_arr[i]);
2887 
2888 		rmem->pg_arr[i] = NULL;
2889 	}
2890 skip_pages:
2891 	if (rmem->pg_tbl) {
2892 		size_t pg_tbl_size = rmem->nr_pages * 8;
2893 
2894 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
2895 			pg_tbl_size = rmem->page_size;
2896 		dma_free_coherent(&pdev->dev, pg_tbl_size,
2897 				  rmem->pg_tbl, rmem->pg_tbl_map);
2898 		rmem->pg_tbl = NULL;
2899 	}
2900 	if (rmem->vmem_size && *rmem->vmem) {
2901 		vfree(*rmem->vmem);
2902 		*rmem->vmem = NULL;
2903 	}
2904 }
2905 
2906 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
2907 {
2908 	struct pci_dev *pdev = bp->pdev;
2909 	u64 valid_bit = 0;
2910 	int i;
2911 
2912 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
2913 		valid_bit = PTU_PTE_VALID;
2914 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
2915 		size_t pg_tbl_size = rmem->nr_pages * 8;
2916 
2917 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
2918 			pg_tbl_size = rmem->page_size;
2919 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
2920 						  &rmem->pg_tbl_map,
2921 						  GFP_KERNEL);
2922 		if (!rmem->pg_tbl)
2923 			return -ENOMEM;
2924 	}
2925 
2926 	for (i = 0; i < rmem->nr_pages; i++) {
2927 		u64 extra_bits = valid_bit;
2928 
2929 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2930 						     rmem->page_size,
2931 						     &rmem->dma_arr[i],
2932 						     GFP_KERNEL);
2933 		if (!rmem->pg_arr[i])
2934 			return -ENOMEM;
2935 
2936 		if (rmem->mem_init)
2937 			bnxt_init_ctx_mem(rmem->mem_init, rmem->pg_arr[i],
2938 					  rmem->page_size);
2939 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
2940 			if (i == rmem->nr_pages - 2 &&
2941 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
2942 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
2943 			else if (i == rmem->nr_pages - 1 &&
2944 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
2945 				extra_bits |= PTU_PTE_LAST;
2946 			rmem->pg_tbl[i] =
2947 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
2948 		}
2949 	}
2950 
2951 	if (rmem->vmem_size) {
2952 		*rmem->vmem = vzalloc(rmem->vmem_size);
2953 		if (!(*rmem->vmem))
2954 			return -ENOMEM;
2955 	}
2956 	return 0;
2957 }
2958 
2959 static void bnxt_free_tpa_info(struct bnxt *bp)
2960 {
2961 	int i;
2962 
2963 	for (i = 0; i < bp->rx_nr_rings; i++) {
2964 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2965 
2966 		kfree(rxr->rx_tpa_idx_map);
2967 		rxr->rx_tpa_idx_map = NULL;
2968 		if (rxr->rx_tpa) {
2969 			kfree(rxr->rx_tpa[0].agg_arr);
2970 			rxr->rx_tpa[0].agg_arr = NULL;
2971 		}
2972 		kfree(rxr->rx_tpa);
2973 		rxr->rx_tpa = NULL;
2974 	}
2975 }
2976 
2977 static int bnxt_alloc_tpa_info(struct bnxt *bp)
2978 {
2979 	int i, j, total_aggs = 0;
2980 
2981 	bp->max_tpa = MAX_TPA;
2982 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
2983 		if (!bp->max_tpa_v2)
2984 			return 0;
2985 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
2986 		total_aggs = bp->max_tpa * MAX_SKB_FRAGS;
2987 	}
2988 
2989 	for (i = 0; i < bp->rx_nr_rings; i++) {
2990 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2991 		struct rx_agg_cmp *agg;
2992 
2993 		rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
2994 				      GFP_KERNEL);
2995 		if (!rxr->rx_tpa)
2996 			return -ENOMEM;
2997 
2998 		if (!(bp->flags & BNXT_FLAG_CHIP_P5))
2999 			continue;
3000 		agg = kcalloc(total_aggs, sizeof(*agg), GFP_KERNEL);
3001 		rxr->rx_tpa[0].agg_arr = agg;
3002 		if (!agg)
3003 			return -ENOMEM;
3004 		for (j = 1; j < bp->max_tpa; j++)
3005 			rxr->rx_tpa[j].agg_arr = agg + j * MAX_SKB_FRAGS;
3006 		rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3007 					      GFP_KERNEL);
3008 		if (!rxr->rx_tpa_idx_map)
3009 			return -ENOMEM;
3010 	}
3011 	return 0;
3012 }
3013 
3014 static void bnxt_free_rx_rings(struct bnxt *bp)
3015 {
3016 	int i;
3017 
3018 	if (!bp->rx_ring)
3019 		return;
3020 
3021 	bnxt_free_tpa_info(bp);
3022 	for (i = 0; i < bp->rx_nr_rings; i++) {
3023 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3024 		struct bnxt_ring_struct *ring;
3025 
3026 		if (rxr->xdp_prog)
3027 			bpf_prog_put(rxr->xdp_prog);
3028 
3029 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3030 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3031 
3032 		page_pool_destroy(rxr->page_pool);
3033 		rxr->page_pool = NULL;
3034 
3035 		kfree(rxr->rx_agg_bmap);
3036 		rxr->rx_agg_bmap = NULL;
3037 
3038 		ring = &rxr->rx_ring_struct;
3039 		bnxt_free_ring(bp, &ring->ring_mem);
3040 
3041 		ring = &rxr->rx_agg_ring_struct;
3042 		bnxt_free_ring(bp, &ring->ring_mem);
3043 	}
3044 }
3045 
3046 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3047 				   struct bnxt_rx_ring_info *rxr)
3048 {
3049 	struct page_pool_params pp = { 0 };
3050 
3051 	pp.pool_size = bp->rx_ring_size;
3052 	pp.nid = dev_to_node(&bp->pdev->dev);
3053 	pp.dev = &bp->pdev->dev;
3054 	pp.dma_dir = DMA_BIDIRECTIONAL;
3055 
3056 	rxr->page_pool = page_pool_create(&pp);
3057 	if (IS_ERR(rxr->page_pool)) {
3058 		int err = PTR_ERR(rxr->page_pool);
3059 
3060 		rxr->page_pool = NULL;
3061 		return err;
3062 	}
3063 	return 0;
3064 }
3065 
3066 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3067 {
3068 	int i, rc = 0, agg_rings = 0;
3069 
3070 	if (!bp->rx_ring)
3071 		return -ENOMEM;
3072 
3073 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3074 		agg_rings = 1;
3075 
3076 	for (i = 0; i < bp->rx_nr_rings; i++) {
3077 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3078 		struct bnxt_ring_struct *ring;
3079 
3080 		ring = &rxr->rx_ring_struct;
3081 
3082 		rc = bnxt_alloc_rx_page_pool(bp, rxr);
3083 		if (rc)
3084 			return rc;
3085 
3086 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3087 		if (rc < 0)
3088 			return rc;
3089 
3090 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3091 						MEM_TYPE_PAGE_POOL,
3092 						rxr->page_pool);
3093 		if (rc) {
3094 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3095 			return rc;
3096 		}
3097 
3098 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3099 		if (rc)
3100 			return rc;
3101 
3102 		ring->grp_idx = i;
3103 		if (agg_rings) {
3104 			u16 mem_size;
3105 
3106 			ring = &rxr->rx_agg_ring_struct;
3107 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3108 			if (rc)
3109 				return rc;
3110 
3111 			ring->grp_idx = i;
3112 			rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3113 			mem_size = rxr->rx_agg_bmap_size / 8;
3114 			rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3115 			if (!rxr->rx_agg_bmap)
3116 				return -ENOMEM;
3117 		}
3118 	}
3119 	if (bp->flags & BNXT_FLAG_TPA)
3120 		rc = bnxt_alloc_tpa_info(bp);
3121 	return rc;
3122 }
3123 
3124 static void bnxt_free_tx_rings(struct bnxt *bp)
3125 {
3126 	int i;
3127 	struct pci_dev *pdev = bp->pdev;
3128 
3129 	if (!bp->tx_ring)
3130 		return;
3131 
3132 	for (i = 0; i < bp->tx_nr_rings; i++) {
3133 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3134 		struct bnxt_ring_struct *ring;
3135 
3136 		if (txr->tx_push) {
3137 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3138 					  txr->tx_push, txr->tx_push_mapping);
3139 			txr->tx_push = NULL;
3140 		}
3141 
3142 		ring = &txr->tx_ring_struct;
3143 
3144 		bnxt_free_ring(bp, &ring->ring_mem);
3145 	}
3146 }
3147 
3148 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3149 {
3150 	int i, j, rc;
3151 	struct pci_dev *pdev = bp->pdev;
3152 
3153 	bp->tx_push_size = 0;
3154 	if (bp->tx_push_thresh) {
3155 		int push_size;
3156 
3157 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3158 					bp->tx_push_thresh);
3159 
3160 		if (push_size > 256) {
3161 			push_size = 0;
3162 			bp->tx_push_thresh = 0;
3163 		}
3164 
3165 		bp->tx_push_size = push_size;
3166 	}
3167 
3168 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3169 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3170 		struct bnxt_ring_struct *ring;
3171 		u8 qidx;
3172 
3173 		ring = &txr->tx_ring_struct;
3174 
3175 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3176 		if (rc)
3177 			return rc;
3178 
3179 		ring->grp_idx = txr->bnapi->index;
3180 		if (bp->tx_push_size) {
3181 			dma_addr_t mapping;
3182 
3183 			/* One pre-allocated DMA buffer to backup
3184 			 * TX push operation
3185 			 */
3186 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3187 						bp->tx_push_size,
3188 						&txr->tx_push_mapping,
3189 						GFP_KERNEL);
3190 
3191 			if (!txr->tx_push)
3192 				return -ENOMEM;
3193 
3194 			mapping = txr->tx_push_mapping +
3195 				sizeof(struct tx_push_bd);
3196 			txr->data_mapping = cpu_to_le64(mapping);
3197 		}
3198 		qidx = bp->tc_to_qidx[j];
3199 		ring->queue_id = bp->q_info[qidx].queue_id;
3200 		if (i < bp->tx_nr_rings_xdp)
3201 			continue;
3202 		if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
3203 			j++;
3204 	}
3205 	return 0;
3206 }
3207 
3208 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
3209 {
3210 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3211 
3212 	kfree(cpr->cp_desc_ring);
3213 	cpr->cp_desc_ring = NULL;
3214 	ring->ring_mem.pg_arr = NULL;
3215 	kfree(cpr->cp_desc_mapping);
3216 	cpr->cp_desc_mapping = NULL;
3217 	ring->ring_mem.dma_arr = NULL;
3218 }
3219 
3220 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
3221 {
3222 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
3223 	if (!cpr->cp_desc_ring)
3224 		return -ENOMEM;
3225 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
3226 				       GFP_KERNEL);
3227 	if (!cpr->cp_desc_mapping)
3228 		return -ENOMEM;
3229 	return 0;
3230 }
3231 
3232 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
3233 {
3234 	int i;
3235 
3236 	if (!bp->bnapi)
3237 		return;
3238 	for (i = 0; i < bp->cp_nr_rings; i++) {
3239 		struct bnxt_napi *bnapi = bp->bnapi[i];
3240 
3241 		if (!bnapi)
3242 			continue;
3243 		bnxt_free_cp_arrays(&bnapi->cp_ring);
3244 	}
3245 }
3246 
3247 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
3248 {
3249 	int i, n = bp->cp_nr_pages;
3250 
3251 	for (i = 0; i < bp->cp_nr_rings; i++) {
3252 		struct bnxt_napi *bnapi = bp->bnapi[i];
3253 		int rc;
3254 
3255 		if (!bnapi)
3256 			continue;
3257 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
3258 		if (rc)
3259 			return rc;
3260 	}
3261 	return 0;
3262 }
3263 
3264 static void bnxt_free_cp_rings(struct bnxt *bp)
3265 {
3266 	int i;
3267 
3268 	if (!bp->bnapi)
3269 		return;
3270 
3271 	for (i = 0; i < bp->cp_nr_rings; i++) {
3272 		struct bnxt_napi *bnapi = bp->bnapi[i];
3273 		struct bnxt_cp_ring_info *cpr;
3274 		struct bnxt_ring_struct *ring;
3275 		int j;
3276 
3277 		if (!bnapi)
3278 			continue;
3279 
3280 		cpr = &bnapi->cp_ring;
3281 		ring = &cpr->cp_ring_struct;
3282 
3283 		bnxt_free_ring(bp, &ring->ring_mem);
3284 
3285 		for (j = 0; j < 2; j++) {
3286 			struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
3287 
3288 			if (cpr2) {
3289 				ring = &cpr2->cp_ring_struct;
3290 				bnxt_free_ring(bp, &ring->ring_mem);
3291 				bnxt_free_cp_arrays(cpr2);
3292 				kfree(cpr2);
3293 				cpr->cp_ring_arr[j] = NULL;
3294 			}
3295 		}
3296 	}
3297 }
3298 
3299 static struct bnxt_cp_ring_info *bnxt_alloc_cp_sub_ring(struct bnxt *bp)
3300 {
3301 	struct bnxt_ring_mem_info *rmem;
3302 	struct bnxt_ring_struct *ring;
3303 	struct bnxt_cp_ring_info *cpr;
3304 	int rc;
3305 
3306 	cpr = kzalloc(sizeof(*cpr), GFP_KERNEL);
3307 	if (!cpr)
3308 		return NULL;
3309 
3310 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
3311 	if (rc) {
3312 		bnxt_free_cp_arrays(cpr);
3313 		kfree(cpr);
3314 		return NULL;
3315 	}
3316 	ring = &cpr->cp_ring_struct;
3317 	rmem = &ring->ring_mem;
3318 	rmem->nr_pages = bp->cp_nr_pages;
3319 	rmem->page_size = HW_CMPD_RING_SIZE;
3320 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
3321 	rmem->dma_arr = cpr->cp_desc_mapping;
3322 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
3323 	rc = bnxt_alloc_ring(bp, rmem);
3324 	if (rc) {
3325 		bnxt_free_ring(bp, rmem);
3326 		bnxt_free_cp_arrays(cpr);
3327 		kfree(cpr);
3328 		cpr = NULL;
3329 	}
3330 	return cpr;
3331 }
3332 
3333 static int bnxt_alloc_cp_rings(struct bnxt *bp)
3334 {
3335 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
3336 	int i, rc, ulp_base_vec, ulp_msix;
3337 
3338 	ulp_msix = bnxt_get_ulp_msix_num(bp);
3339 	ulp_base_vec = bnxt_get_ulp_msix_base(bp);
3340 	for (i = 0; i < bp->cp_nr_rings; i++) {
3341 		struct bnxt_napi *bnapi = bp->bnapi[i];
3342 		struct bnxt_cp_ring_info *cpr;
3343 		struct bnxt_ring_struct *ring;
3344 
3345 		if (!bnapi)
3346 			continue;
3347 
3348 		cpr = &bnapi->cp_ring;
3349 		cpr->bnapi = bnapi;
3350 		ring = &cpr->cp_ring_struct;
3351 
3352 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3353 		if (rc)
3354 			return rc;
3355 
3356 		if (ulp_msix && i >= ulp_base_vec)
3357 			ring->map_idx = i + ulp_msix;
3358 		else
3359 			ring->map_idx = i;
3360 
3361 		if (!(bp->flags & BNXT_FLAG_CHIP_P5))
3362 			continue;
3363 
3364 		if (i < bp->rx_nr_rings) {
3365 			struct bnxt_cp_ring_info *cpr2 =
3366 				bnxt_alloc_cp_sub_ring(bp);
3367 
3368 			cpr->cp_ring_arr[BNXT_RX_HDL] = cpr2;
3369 			if (!cpr2)
3370 				return -ENOMEM;
3371 			cpr2->bnapi = bnapi;
3372 		}
3373 		if ((sh && i < bp->tx_nr_rings) ||
3374 		    (!sh && i >= bp->rx_nr_rings)) {
3375 			struct bnxt_cp_ring_info *cpr2 =
3376 				bnxt_alloc_cp_sub_ring(bp);
3377 
3378 			cpr->cp_ring_arr[BNXT_TX_HDL] = cpr2;
3379 			if (!cpr2)
3380 				return -ENOMEM;
3381 			cpr2->bnapi = bnapi;
3382 		}
3383 	}
3384 	return 0;
3385 }
3386 
3387 static void bnxt_init_ring_struct(struct bnxt *bp)
3388 {
3389 	int i;
3390 
3391 	for (i = 0; i < bp->cp_nr_rings; i++) {
3392 		struct bnxt_napi *bnapi = bp->bnapi[i];
3393 		struct bnxt_ring_mem_info *rmem;
3394 		struct bnxt_cp_ring_info *cpr;
3395 		struct bnxt_rx_ring_info *rxr;
3396 		struct bnxt_tx_ring_info *txr;
3397 		struct bnxt_ring_struct *ring;
3398 
3399 		if (!bnapi)
3400 			continue;
3401 
3402 		cpr = &bnapi->cp_ring;
3403 		ring = &cpr->cp_ring_struct;
3404 		rmem = &ring->ring_mem;
3405 		rmem->nr_pages = bp->cp_nr_pages;
3406 		rmem->page_size = HW_CMPD_RING_SIZE;
3407 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
3408 		rmem->dma_arr = cpr->cp_desc_mapping;
3409 		rmem->vmem_size = 0;
3410 
3411 		rxr = bnapi->rx_ring;
3412 		if (!rxr)
3413 			goto skip_rx;
3414 
3415 		ring = &rxr->rx_ring_struct;
3416 		rmem = &ring->ring_mem;
3417 		rmem->nr_pages = bp->rx_nr_pages;
3418 		rmem->page_size = HW_RXBD_RING_SIZE;
3419 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
3420 		rmem->dma_arr = rxr->rx_desc_mapping;
3421 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
3422 		rmem->vmem = (void **)&rxr->rx_buf_ring;
3423 
3424 		ring = &rxr->rx_agg_ring_struct;
3425 		rmem = &ring->ring_mem;
3426 		rmem->nr_pages = bp->rx_agg_nr_pages;
3427 		rmem->page_size = HW_RXBD_RING_SIZE;
3428 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
3429 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
3430 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
3431 		rmem->vmem = (void **)&rxr->rx_agg_ring;
3432 
3433 skip_rx:
3434 		txr = bnapi->tx_ring;
3435 		if (!txr)
3436 			continue;
3437 
3438 		ring = &txr->tx_ring_struct;
3439 		rmem = &ring->ring_mem;
3440 		rmem->nr_pages = bp->tx_nr_pages;
3441 		rmem->page_size = HW_RXBD_RING_SIZE;
3442 		rmem->pg_arr = (void **)txr->tx_desc_ring;
3443 		rmem->dma_arr = txr->tx_desc_mapping;
3444 		rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
3445 		rmem->vmem = (void **)&txr->tx_buf_ring;
3446 	}
3447 }
3448 
3449 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
3450 {
3451 	int i;
3452 	u32 prod;
3453 	struct rx_bd **rx_buf_ring;
3454 
3455 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
3456 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
3457 		int j;
3458 		struct rx_bd *rxbd;
3459 
3460 		rxbd = rx_buf_ring[i];
3461 		if (!rxbd)
3462 			continue;
3463 
3464 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
3465 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
3466 			rxbd->rx_bd_opaque = prod;
3467 		}
3468 	}
3469 }
3470 
3471 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
3472 {
3473 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
3474 	struct net_device *dev = bp->dev;
3475 	u32 prod;
3476 	int i;
3477 
3478 	prod = rxr->rx_prod;
3479 	for (i = 0; i < bp->rx_ring_size; i++) {
3480 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
3481 			netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
3482 				    ring_nr, i, bp->rx_ring_size);
3483 			break;
3484 		}
3485 		prod = NEXT_RX(prod);
3486 	}
3487 	rxr->rx_prod = prod;
3488 
3489 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
3490 		return 0;
3491 
3492 	prod = rxr->rx_agg_prod;
3493 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
3494 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
3495 			netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
3496 				    ring_nr, i, bp->rx_ring_size);
3497 			break;
3498 		}
3499 		prod = NEXT_RX_AGG(prod);
3500 	}
3501 	rxr->rx_agg_prod = prod;
3502 
3503 	if (rxr->rx_tpa) {
3504 		dma_addr_t mapping;
3505 		u8 *data;
3506 
3507 		for (i = 0; i < bp->max_tpa; i++) {
3508 			data = __bnxt_alloc_rx_data(bp, &mapping, GFP_KERNEL);
3509 			if (!data)
3510 				return -ENOMEM;
3511 
3512 			rxr->rx_tpa[i].data = data;
3513 			rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
3514 			rxr->rx_tpa[i].mapping = mapping;
3515 		}
3516 	}
3517 	return 0;
3518 }
3519 
3520 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
3521 {
3522 	struct bnxt_rx_ring_info *rxr;
3523 	struct bnxt_ring_struct *ring;
3524 	u32 type;
3525 
3526 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
3527 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
3528 
3529 	if (NET_IP_ALIGN == 2)
3530 		type |= RX_BD_FLAGS_SOP;
3531 
3532 	rxr = &bp->rx_ring[ring_nr];
3533 	ring = &rxr->rx_ring_struct;
3534 	bnxt_init_rxbd_pages(ring, type);
3535 
3536 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
3537 		bpf_prog_add(bp->xdp_prog, 1);
3538 		rxr->xdp_prog = bp->xdp_prog;
3539 	}
3540 	ring->fw_ring_id = INVALID_HW_RING_ID;
3541 
3542 	ring = &rxr->rx_agg_ring_struct;
3543 	ring->fw_ring_id = INVALID_HW_RING_ID;
3544 
3545 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
3546 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
3547 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
3548 
3549 		bnxt_init_rxbd_pages(ring, type);
3550 	}
3551 
3552 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
3553 }
3554 
3555 static void bnxt_init_cp_rings(struct bnxt *bp)
3556 {
3557 	int i, j;
3558 
3559 	for (i = 0; i < bp->cp_nr_rings; i++) {
3560 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
3561 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3562 
3563 		ring->fw_ring_id = INVALID_HW_RING_ID;
3564 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
3565 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
3566 		for (j = 0; j < 2; j++) {
3567 			struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
3568 
3569 			if (!cpr2)
3570 				continue;
3571 
3572 			ring = &cpr2->cp_ring_struct;
3573 			ring->fw_ring_id = INVALID_HW_RING_ID;
3574 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
3575 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
3576 		}
3577 	}
3578 }
3579 
3580 static int bnxt_init_rx_rings(struct bnxt *bp)
3581 {
3582 	int i, rc = 0;
3583 
3584 	if (BNXT_RX_PAGE_MODE(bp)) {
3585 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
3586 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
3587 	} else {
3588 		bp->rx_offset = BNXT_RX_OFFSET;
3589 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
3590 	}
3591 
3592 	for (i = 0; i < bp->rx_nr_rings; i++) {
3593 		rc = bnxt_init_one_rx_ring(bp, i);
3594 		if (rc)
3595 			break;
3596 	}
3597 
3598 	return rc;
3599 }
3600 
3601 static int bnxt_init_tx_rings(struct bnxt *bp)
3602 {
3603 	u16 i;
3604 
3605 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
3606 				   BNXT_MIN_TX_DESC_CNT);
3607 
3608 	for (i = 0; i < bp->tx_nr_rings; i++) {
3609 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3610 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
3611 
3612 		ring->fw_ring_id = INVALID_HW_RING_ID;
3613 	}
3614 
3615 	return 0;
3616 }
3617 
3618 static void bnxt_free_ring_grps(struct bnxt *bp)
3619 {
3620 	kfree(bp->grp_info);
3621 	bp->grp_info = NULL;
3622 }
3623 
3624 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
3625 {
3626 	int i;
3627 
3628 	if (irq_re_init) {
3629 		bp->grp_info = kcalloc(bp->cp_nr_rings,
3630 				       sizeof(struct bnxt_ring_grp_info),
3631 				       GFP_KERNEL);
3632 		if (!bp->grp_info)
3633 			return -ENOMEM;
3634 	}
3635 	for (i = 0; i < bp->cp_nr_rings; i++) {
3636 		if (irq_re_init)
3637 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
3638 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
3639 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
3640 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
3641 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
3642 	}
3643 	return 0;
3644 }
3645 
3646 static void bnxt_free_vnics(struct bnxt *bp)
3647 {
3648 	kfree(bp->vnic_info);
3649 	bp->vnic_info = NULL;
3650 	bp->nr_vnics = 0;
3651 }
3652 
3653 static int bnxt_alloc_vnics(struct bnxt *bp)
3654 {
3655 	int num_vnics = 1;
3656 
3657 #ifdef CONFIG_RFS_ACCEL
3658 	if ((bp->flags & (BNXT_FLAG_RFS | BNXT_FLAG_CHIP_P5)) == BNXT_FLAG_RFS)
3659 		num_vnics += bp->rx_nr_rings;
3660 #endif
3661 
3662 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3663 		num_vnics++;
3664 
3665 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
3666 				GFP_KERNEL);
3667 	if (!bp->vnic_info)
3668 		return -ENOMEM;
3669 
3670 	bp->nr_vnics = num_vnics;
3671 	return 0;
3672 }
3673 
3674 static void bnxt_init_vnics(struct bnxt *bp)
3675 {
3676 	int i;
3677 
3678 	for (i = 0; i < bp->nr_vnics; i++) {
3679 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3680 		int j;
3681 
3682 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
3683 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
3684 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
3685 
3686 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
3687 
3688 		if (bp->vnic_info[i].rss_hash_key) {
3689 			if (i == 0)
3690 				prandom_bytes(vnic->rss_hash_key,
3691 					      HW_HASH_KEY_SIZE);
3692 			else
3693 				memcpy(vnic->rss_hash_key,
3694 				       bp->vnic_info[0].rss_hash_key,
3695 				       HW_HASH_KEY_SIZE);
3696 		}
3697 	}
3698 }
3699 
3700 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
3701 {
3702 	int pages;
3703 
3704 	pages = ring_size / desc_per_pg;
3705 
3706 	if (!pages)
3707 		return 1;
3708 
3709 	pages++;
3710 
3711 	while (pages & (pages - 1))
3712 		pages++;
3713 
3714 	return pages;
3715 }
3716 
3717 void bnxt_set_tpa_flags(struct bnxt *bp)
3718 {
3719 	bp->flags &= ~BNXT_FLAG_TPA;
3720 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
3721 		return;
3722 	if (bp->dev->features & NETIF_F_LRO)
3723 		bp->flags |= BNXT_FLAG_LRO;
3724 	else if (bp->dev->features & NETIF_F_GRO_HW)
3725 		bp->flags |= BNXT_FLAG_GRO;
3726 }
3727 
3728 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
3729  * be set on entry.
3730  */
3731 void bnxt_set_ring_params(struct bnxt *bp)
3732 {
3733 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
3734 	u32 agg_factor = 0, agg_ring_size = 0;
3735 
3736 	/* 8 for CRC and VLAN */
3737 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
3738 
3739 	rx_space = rx_size + NET_SKB_PAD +
3740 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3741 
3742 	bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
3743 	ring_size = bp->rx_ring_size;
3744 	bp->rx_agg_ring_size = 0;
3745 	bp->rx_agg_nr_pages = 0;
3746 
3747 	if (bp->flags & BNXT_FLAG_TPA)
3748 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
3749 
3750 	bp->flags &= ~BNXT_FLAG_JUMBO;
3751 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
3752 		u32 jumbo_factor;
3753 
3754 		bp->flags |= BNXT_FLAG_JUMBO;
3755 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
3756 		if (jumbo_factor > agg_factor)
3757 			agg_factor = jumbo_factor;
3758 	}
3759 	if (agg_factor) {
3760 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
3761 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
3762 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
3763 				    bp->rx_ring_size, ring_size);
3764 			bp->rx_ring_size = ring_size;
3765 		}
3766 		agg_ring_size = ring_size * agg_factor;
3767 
3768 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
3769 							RX_DESC_CNT);
3770 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
3771 			u32 tmp = agg_ring_size;
3772 
3773 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
3774 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
3775 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
3776 				    tmp, agg_ring_size);
3777 		}
3778 		bp->rx_agg_ring_size = agg_ring_size;
3779 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
3780 		rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
3781 		rx_space = rx_size + NET_SKB_PAD +
3782 			SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3783 	}
3784 
3785 	bp->rx_buf_use_size = rx_size;
3786 	bp->rx_buf_size = rx_space;
3787 
3788 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
3789 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
3790 
3791 	ring_size = bp->tx_ring_size;
3792 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
3793 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
3794 
3795 	max_rx_cmpl = bp->rx_ring_size;
3796 	/* MAX TPA needs to be added because TPA_START completions are
3797 	 * immediately recycled, so the TPA completions are not bound by
3798 	 * the RX ring size.
3799 	 */
3800 	if (bp->flags & BNXT_FLAG_TPA)
3801 		max_rx_cmpl += bp->max_tpa;
3802 	/* RX and TPA completions are 32-byte, all others are 16-byte */
3803 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
3804 	bp->cp_ring_size = ring_size;
3805 
3806 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
3807 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
3808 		bp->cp_nr_pages = MAX_CP_PAGES;
3809 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
3810 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
3811 			    ring_size, bp->cp_ring_size);
3812 	}
3813 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
3814 	bp->cp_ring_mask = bp->cp_bit - 1;
3815 }
3816 
3817 /* Changing allocation mode of RX rings.
3818  * TODO: Update when extending xdp_rxq_info to support allocation modes.
3819  */
3820 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
3821 {
3822 	if (page_mode) {
3823 		if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
3824 			return -EOPNOTSUPP;
3825 		bp->dev->max_mtu =
3826 			min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
3827 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
3828 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
3829 		bp->rx_dir = DMA_BIDIRECTIONAL;
3830 		bp->rx_skb_func = bnxt_rx_page_skb;
3831 		/* Disable LRO or GRO_HW */
3832 		netdev_update_features(bp->dev);
3833 	} else {
3834 		bp->dev->max_mtu = bp->max_mtu;
3835 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
3836 		bp->rx_dir = DMA_FROM_DEVICE;
3837 		bp->rx_skb_func = bnxt_rx_skb;
3838 	}
3839 	return 0;
3840 }
3841 
3842 static void bnxt_free_vnic_attributes(struct bnxt *bp)
3843 {
3844 	int i;
3845 	struct bnxt_vnic_info *vnic;
3846 	struct pci_dev *pdev = bp->pdev;
3847 
3848 	if (!bp->vnic_info)
3849 		return;
3850 
3851 	for (i = 0; i < bp->nr_vnics; i++) {
3852 		vnic = &bp->vnic_info[i];
3853 
3854 		kfree(vnic->fw_grp_ids);
3855 		vnic->fw_grp_ids = NULL;
3856 
3857 		kfree(vnic->uc_list);
3858 		vnic->uc_list = NULL;
3859 
3860 		if (vnic->mc_list) {
3861 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
3862 					  vnic->mc_list, vnic->mc_list_mapping);
3863 			vnic->mc_list = NULL;
3864 		}
3865 
3866 		if (vnic->rss_table) {
3867 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
3868 					  vnic->rss_table,
3869 					  vnic->rss_table_dma_addr);
3870 			vnic->rss_table = NULL;
3871 		}
3872 
3873 		vnic->rss_hash_key = NULL;
3874 		vnic->flags = 0;
3875 	}
3876 }
3877 
3878 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
3879 {
3880 	int i, rc = 0, size;
3881 	struct bnxt_vnic_info *vnic;
3882 	struct pci_dev *pdev = bp->pdev;
3883 	int max_rings;
3884 
3885 	for (i = 0; i < bp->nr_vnics; i++) {
3886 		vnic = &bp->vnic_info[i];
3887 
3888 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
3889 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
3890 
3891 			if (mem_size > 0) {
3892 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
3893 				if (!vnic->uc_list) {
3894 					rc = -ENOMEM;
3895 					goto out;
3896 				}
3897 			}
3898 		}
3899 
3900 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
3901 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
3902 			vnic->mc_list =
3903 				dma_alloc_coherent(&pdev->dev,
3904 						   vnic->mc_list_size,
3905 						   &vnic->mc_list_mapping,
3906 						   GFP_KERNEL);
3907 			if (!vnic->mc_list) {
3908 				rc = -ENOMEM;
3909 				goto out;
3910 			}
3911 		}
3912 
3913 		if (bp->flags & BNXT_FLAG_CHIP_P5)
3914 			goto vnic_skip_grps;
3915 
3916 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
3917 			max_rings = bp->rx_nr_rings;
3918 		else
3919 			max_rings = 1;
3920 
3921 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
3922 		if (!vnic->fw_grp_ids) {
3923 			rc = -ENOMEM;
3924 			goto out;
3925 		}
3926 vnic_skip_grps:
3927 		if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
3928 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
3929 			continue;
3930 
3931 		/* Allocate rss table and hash key */
3932 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
3933 		if (bp->flags & BNXT_FLAG_CHIP_P5)
3934 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
3935 
3936 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
3937 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
3938 						     vnic->rss_table_size,
3939 						     &vnic->rss_table_dma_addr,
3940 						     GFP_KERNEL);
3941 		if (!vnic->rss_table) {
3942 			rc = -ENOMEM;
3943 			goto out;
3944 		}
3945 
3946 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
3947 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
3948 	}
3949 	return 0;
3950 
3951 out:
3952 	return rc;
3953 }
3954 
3955 static void bnxt_free_hwrm_resources(struct bnxt *bp)
3956 {
3957 	struct bnxt_hwrm_wait_token *token;
3958 
3959 	dma_pool_destroy(bp->hwrm_dma_pool);
3960 	bp->hwrm_dma_pool = NULL;
3961 
3962 	rcu_read_lock();
3963 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
3964 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
3965 	rcu_read_unlock();
3966 }
3967 
3968 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
3969 {
3970 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
3971 					    BNXT_HWRM_DMA_SIZE,
3972 					    BNXT_HWRM_DMA_ALIGN, 0);
3973 	if (!bp->hwrm_dma_pool)
3974 		return -ENOMEM;
3975 
3976 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
3977 
3978 	return 0;
3979 }
3980 
3981 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
3982 {
3983 	kfree(stats->hw_masks);
3984 	stats->hw_masks = NULL;
3985 	kfree(stats->sw_stats);
3986 	stats->sw_stats = NULL;
3987 	if (stats->hw_stats) {
3988 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
3989 				  stats->hw_stats_map);
3990 		stats->hw_stats = NULL;
3991 	}
3992 }
3993 
3994 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
3995 				bool alloc_masks)
3996 {
3997 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
3998 					     &stats->hw_stats_map, GFP_KERNEL);
3999 	if (!stats->hw_stats)
4000 		return -ENOMEM;
4001 
4002 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
4003 	if (!stats->sw_stats)
4004 		goto stats_mem_err;
4005 
4006 	if (alloc_masks) {
4007 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
4008 		if (!stats->hw_masks)
4009 			goto stats_mem_err;
4010 	}
4011 	return 0;
4012 
4013 stats_mem_err:
4014 	bnxt_free_stats_mem(bp, stats);
4015 	return -ENOMEM;
4016 }
4017 
4018 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
4019 {
4020 	int i;
4021 
4022 	for (i = 0; i < count; i++)
4023 		mask_arr[i] = mask;
4024 }
4025 
4026 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
4027 {
4028 	int i;
4029 
4030 	for (i = 0; i < count; i++)
4031 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
4032 }
4033 
4034 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
4035 				    struct bnxt_stats_mem *stats)
4036 {
4037 	struct hwrm_func_qstats_ext_output *resp;
4038 	struct hwrm_func_qstats_ext_input *req;
4039 	__le64 *hw_masks;
4040 	int rc;
4041 
4042 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
4043 	    !(bp->flags & BNXT_FLAG_CHIP_P5))
4044 		return -EOPNOTSUPP;
4045 
4046 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
4047 	if (rc)
4048 		return rc;
4049 
4050 	req->fid = cpu_to_le16(0xffff);
4051 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4052 
4053 	resp = hwrm_req_hold(bp, req);
4054 	rc = hwrm_req_send(bp, req);
4055 	if (!rc) {
4056 		hw_masks = &resp->rx_ucast_pkts;
4057 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
4058 	}
4059 	hwrm_req_drop(bp, req);
4060 	return rc;
4061 }
4062 
4063 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
4064 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
4065 
4066 static void bnxt_init_stats(struct bnxt *bp)
4067 {
4068 	struct bnxt_napi *bnapi = bp->bnapi[0];
4069 	struct bnxt_cp_ring_info *cpr;
4070 	struct bnxt_stats_mem *stats;
4071 	__le64 *rx_stats, *tx_stats;
4072 	int rc, rx_count, tx_count;
4073 	u64 *rx_masks, *tx_masks;
4074 	u64 mask;
4075 	u8 flags;
4076 
4077 	cpr = &bnapi->cp_ring;
4078 	stats = &cpr->stats;
4079 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
4080 	if (rc) {
4081 		if (bp->flags & BNXT_FLAG_CHIP_P5)
4082 			mask = (1ULL << 48) - 1;
4083 		else
4084 			mask = -1ULL;
4085 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
4086 	}
4087 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
4088 		stats = &bp->port_stats;
4089 		rx_stats = stats->hw_stats;
4090 		rx_masks = stats->hw_masks;
4091 		rx_count = sizeof(struct rx_port_stats) / 8;
4092 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4093 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4094 		tx_count = sizeof(struct tx_port_stats) / 8;
4095 
4096 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
4097 		rc = bnxt_hwrm_port_qstats(bp, flags);
4098 		if (rc) {
4099 			mask = (1ULL << 40) - 1;
4100 
4101 			bnxt_fill_masks(rx_masks, mask, rx_count);
4102 			bnxt_fill_masks(tx_masks, mask, tx_count);
4103 		} else {
4104 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4105 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
4106 			bnxt_hwrm_port_qstats(bp, 0);
4107 		}
4108 	}
4109 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
4110 		stats = &bp->rx_port_stats_ext;
4111 		rx_stats = stats->hw_stats;
4112 		rx_masks = stats->hw_masks;
4113 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
4114 		stats = &bp->tx_port_stats_ext;
4115 		tx_stats = stats->hw_stats;
4116 		tx_masks = stats->hw_masks;
4117 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
4118 
4119 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4120 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
4121 		if (rc) {
4122 			mask = (1ULL << 40) - 1;
4123 
4124 			bnxt_fill_masks(rx_masks, mask, rx_count);
4125 			if (tx_stats)
4126 				bnxt_fill_masks(tx_masks, mask, tx_count);
4127 		} else {
4128 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4129 			if (tx_stats)
4130 				bnxt_copy_hw_masks(tx_masks, tx_stats,
4131 						   tx_count);
4132 			bnxt_hwrm_port_qstats_ext(bp, 0);
4133 		}
4134 	}
4135 }
4136 
4137 static void bnxt_free_port_stats(struct bnxt *bp)
4138 {
4139 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
4140 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
4141 
4142 	bnxt_free_stats_mem(bp, &bp->port_stats);
4143 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
4144 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
4145 }
4146 
4147 static void bnxt_free_ring_stats(struct bnxt *bp)
4148 {
4149 	int i;
4150 
4151 	if (!bp->bnapi)
4152 		return;
4153 
4154 	for (i = 0; i < bp->cp_nr_rings; i++) {
4155 		struct bnxt_napi *bnapi = bp->bnapi[i];
4156 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4157 
4158 		bnxt_free_stats_mem(bp, &cpr->stats);
4159 	}
4160 }
4161 
4162 static int bnxt_alloc_stats(struct bnxt *bp)
4163 {
4164 	u32 size, i;
4165 	int rc;
4166 
4167 	size = bp->hw_ring_stats_size;
4168 
4169 	for (i = 0; i < bp->cp_nr_rings; i++) {
4170 		struct bnxt_napi *bnapi = bp->bnapi[i];
4171 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4172 
4173 		cpr->stats.len = size;
4174 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
4175 		if (rc)
4176 			return rc;
4177 
4178 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4179 	}
4180 
4181 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
4182 		return 0;
4183 
4184 	if (bp->port_stats.hw_stats)
4185 		goto alloc_ext_stats;
4186 
4187 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
4188 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
4189 	if (rc)
4190 		return rc;
4191 
4192 	bp->flags |= BNXT_FLAG_PORT_STATS;
4193 
4194 alloc_ext_stats:
4195 	/* Display extended statistics only if FW supports it */
4196 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
4197 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
4198 			return 0;
4199 
4200 	if (bp->rx_port_stats_ext.hw_stats)
4201 		goto alloc_tx_ext_stats;
4202 
4203 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
4204 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
4205 	/* Extended stats are optional */
4206 	if (rc)
4207 		return 0;
4208 
4209 alloc_tx_ext_stats:
4210 	if (bp->tx_port_stats_ext.hw_stats)
4211 		return 0;
4212 
4213 	if (bp->hwrm_spec_code >= 0x10902 ||
4214 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
4215 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
4216 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
4217 		/* Extended stats are optional */
4218 		if (rc)
4219 			return 0;
4220 	}
4221 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
4222 	return 0;
4223 }
4224 
4225 static void bnxt_clear_ring_indices(struct bnxt *bp)
4226 {
4227 	int i;
4228 
4229 	if (!bp->bnapi)
4230 		return;
4231 
4232 	for (i = 0; i < bp->cp_nr_rings; i++) {
4233 		struct bnxt_napi *bnapi = bp->bnapi[i];
4234 		struct bnxt_cp_ring_info *cpr;
4235 		struct bnxt_rx_ring_info *rxr;
4236 		struct bnxt_tx_ring_info *txr;
4237 
4238 		if (!bnapi)
4239 			continue;
4240 
4241 		cpr = &bnapi->cp_ring;
4242 		cpr->cp_raw_cons = 0;
4243 
4244 		txr = bnapi->tx_ring;
4245 		if (txr) {
4246 			txr->tx_prod = 0;
4247 			txr->tx_cons = 0;
4248 		}
4249 
4250 		rxr = bnapi->rx_ring;
4251 		if (rxr) {
4252 			rxr->rx_prod = 0;
4253 			rxr->rx_agg_prod = 0;
4254 			rxr->rx_sw_agg_prod = 0;
4255 			rxr->rx_next_cons = 0;
4256 		}
4257 	}
4258 }
4259 
4260 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
4261 {
4262 #ifdef CONFIG_RFS_ACCEL
4263 	int i;
4264 
4265 	/* Under rtnl_lock and all our NAPIs have been disabled.  It's
4266 	 * safe to delete the hash table.
4267 	 */
4268 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
4269 		struct hlist_head *head;
4270 		struct hlist_node *tmp;
4271 		struct bnxt_ntuple_filter *fltr;
4272 
4273 		head = &bp->ntp_fltr_hash_tbl[i];
4274 		hlist_for_each_entry_safe(fltr, tmp, head, hash) {
4275 			hlist_del(&fltr->hash);
4276 			kfree(fltr);
4277 		}
4278 	}
4279 	if (irq_reinit) {
4280 		kfree(bp->ntp_fltr_bmap);
4281 		bp->ntp_fltr_bmap = NULL;
4282 	}
4283 	bp->ntp_fltr_count = 0;
4284 #endif
4285 }
4286 
4287 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
4288 {
4289 #ifdef CONFIG_RFS_ACCEL
4290 	int i, rc = 0;
4291 
4292 	if (!(bp->flags & BNXT_FLAG_RFS))
4293 		return 0;
4294 
4295 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
4296 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
4297 
4298 	bp->ntp_fltr_count = 0;
4299 	bp->ntp_fltr_bmap = kcalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
4300 				    sizeof(long),
4301 				    GFP_KERNEL);
4302 
4303 	if (!bp->ntp_fltr_bmap)
4304 		rc = -ENOMEM;
4305 
4306 	return rc;
4307 #else
4308 	return 0;
4309 #endif
4310 }
4311 
4312 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
4313 {
4314 	bnxt_free_vnic_attributes(bp);
4315 	bnxt_free_tx_rings(bp);
4316 	bnxt_free_rx_rings(bp);
4317 	bnxt_free_cp_rings(bp);
4318 	bnxt_free_all_cp_arrays(bp);
4319 	bnxt_free_ntp_fltrs(bp, irq_re_init);
4320 	if (irq_re_init) {
4321 		bnxt_free_ring_stats(bp);
4322 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
4323 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
4324 			bnxt_free_port_stats(bp);
4325 		bnxt_free_ring_grps(bp);
4326 		bnxt_free_vnics(bp);
4327 		kfree(bp->tx_ring_map);
4328 		bp->tx_ring_map = NULL;
4329 		kfree(bp->tx_ring);
4330 		bp->tx_ring = NULL;
4331 		kfree(bp->rx_ring);
4332 		bp->rx_ring = NULL;
4333 		kfree(bp->bnapi);
4334 		bp->bnapi = NULL;
4335 	} else {
4336 		bnxt_clear_ring_indices(bp);
4337 	}
4338 }
4339 
4340 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
4341 {
4342 	int i, j, rc, size, arr_size;
4343 	void *bnapi;
4344 
4345 	if (irq_re_init) {
4346 		/* Allocate bnapi mem pointer array and mem block for
4347 		 * all queues
4348 		 */
4349 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
4350 				bp->cp_nr_rings);
4351 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
4352 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
4353 		if (!bnapi)
4354 			return -ENOMEM;
4355 
4356 		bp->bnapi = bnapi;
4357 		bnapi += arr_size;
4358 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
4359 			bp->bnapi[i] = bnapi;
4360 			bp->bnapi[i]->index = i;
4361 			bp->bnapi[i]->bp = bp;
4362 			if (bp->flags & BNXT_FLAG_CHIP_P5) {
4363 				struct bnxt_cp_ring_info *cpr =
4364 					&bp->bnapi[i]->cp_ring;
4365 
4366 				cpr->cp_ring_struct.ring_mem.flags =
4367 					BNXT_RMEM_RING_PTE_FLAG;
4368 			}
4369 		}
4370 
4371 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
4372 				      sizeof(struct bnxt_rx_ring_info),
4373 				      GFP_KERNEL);
4374 		if (!bp->rx_ring)
4375 			return -ENOMEM;
4376 
4377 		for (i = 0; i < bp->rx_nr_rings; i++) {
4378 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
4379 
4380 			if (bp->flags & BNXT_FLAG_CHIP_P5) {
4381 				rxr->rx_ring_struct.ring_mem.flags =
4382 					BNXT_RMEM_RING_PTE_FLAG;
4383 				rxr->rx_agg_ring_struct.ring_mem.flags =
4384 					BNXT_RMEM_RING_PTE_FLAG;
4385 			}
4386 			rxr->bnapi = bp->bnapi[i];
4387 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
4388 		}
4389 
4390 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
4391 				      sizeof(struct bnxt_tx_ring_info),
4392 				      GFP_KERNEL);
4393 		if (!bp->tx_ring)
4394 			return -ENOMEM;
4395 
4396 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
4397 					  GFP_KERNEL);
4398 
4399 		if (!bp->tx_ring_map)
4400 			return -ENOMEM;
4401 
4402 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
4403 			j = 0;
4404 		else
4405 			j = bp->rx_nr_rings;
4406 
4407 		for (i = 0; i < bp->tx_nr_rings; i++, j++) {
4408 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4409 
4410 			if (bp->flags & BNXT_FLAG_CHIP_P5)
4411 				txr->tx_ring_struct.ring_mem.flags =
4412 					BNXT_RMEM_RING_PTE_FLAG;
4413 			txr->bnapi = bp->bnapi[j];
4414 			bp->bnapi[j]->tx_ring = txr;
4415 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
4416 			if (i >= bp->tx_nr_rings_xdp) {
4417 				txr->txq_index = i - bp->tx_nr_rings_xdp;
4418 				bp->bnapi[j]->tx_int = bnxt_tx_int;
4419 			} else {
4420 				bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
4421 				bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
4422 			}
4423 		}
4424 
4425 		rc = bnxt_alloc_stats(bp);
4426 		if (rc)
4427 			goto alloc_mem_err;
4428 		bnxt_init_stats(bp);
4429 
4430 		rc = bnxt_alloc_ntp_fltrs(bp);
4431 		if (rc)
4432 			goto alloc_mem_err;
4433 
4434 		rc = bnxt_alloc_vnics(bp);
4435 		if (rc)
4436 			goto alloc_mem_err;
4437 	}
4438 
4439 	rc = bnxt_alloc_all_cp_arrays(bp);
4440 	if (rc)
4441 		goto alloc_mem_err;
4442 
4443 	bnxt_init_ring_struct(bp);
4444 
4445 	rc = bnxt_alloc_rx_rings(bp);
4446 	if (rc)
4447 		goto alloc_mem_err;
4448 
4449 	rc = bnxt_alloc_tx_rings(bp);
4450 	if (rc)
4451 		goto alloc_mem_err;
4452 
4453 	rc = bnxt_alloc_cp_rings(bp);
4454 	if (rc)
4455 		goto alloc_mem_err;
4456 
4457 	bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
4458 				  BNXT_VNIC_UCAST_FLAG;
4459 	rc = bnxt_alloc_vnic_attributes(bp);
4460 	if (rc)
4461 		goto alloc_mem_err;
4462 	return 0;
4463 
4464 alloc_mem_err:
4465 	bnxt_free_mem(bp, true);
4466 	return rc;
4467 }
4468 
4469 static void bnxt_disable_int(struct bnxt *bp)
4470 {
4471 	int i;
4472 
4473 	if (!bp->bnapi)
4474 		return;
4475 
4476 	for (i = 0; i < bp->cp_nr_rings; i++) {
4477 		struct bnxt_napi *bnapi = bp->bnapi[i];
4478 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4479 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4480 
4481 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
4482 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
4483 	}
4484 }
4485 
4486 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
4487 {
4488 	struct bnxt_napi *bnapi = bp->bnapi[n];
4489 	struct bnxt_cp_ring_info *cpr;
4490 
4491 	cpr = &bnapi->cp_ring;
4492 	return cpr->cp_ring_struct.map_idx;
4493 }
4494 
4495 static void bnxt_disable_int_sync(struct bnxt *bp)
4496 {
4497 	int i;
4498 
4499 	if (!bp->irq_tbl)
4500 		return;
4501 
4502 	atomic_inc(&bp->intr_sem);
4503 
4504 	bnxt_disable_int(bp);
4505 	for (i = 0; i < bp->cp_nr_rings; i++) {
4506 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
4507 
4508 		synchronize_irq(bp->irq_tbl[map_idx].vector);
4509 	}
4510 }
4511 
4512 static void bnxt_enable_int(struct bnxt *bp)
4513 {
4514 	int i;
4515 
4516 	atomic_set(&bp->intr_sem, 0);
4517 	for (i = 0; i < bp->cp_nr_rings; i++) {
4518 		struct bnxt_napi *bnapi = bp->bnapi[i];
4519 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4520 
4521 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
4522 	}
4523 }
4524 
4525 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
4526 			    bool async_only)
4527 {
4528 	DECLARE_BITMAP(async_events_bmap, 256);
4529 	u32 *events = (u32 *)async_events_bmap;
4530 	struct hwrm_func_drv_rgtr_output *resp;
4531 	struct hwrm_func_drv_rgtr_input *req;
4532 	u32 flags;
4533 	int rc, i;
4534 
4535 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
4536 	if (rc)
4537 		return rc;
4538 
4539 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
4540 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
4541 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
4542 
4543 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
4544 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
4545 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
4546 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
4547 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
4548 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
4549 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
4550 	req->flags = cpu_to_le32(flags);
4551 	req->ver_maj_8b = DRV_VER_MAJ;
4552 	req->ver_min_8b = DRV_VER_MIN;
4553 	req->ver_upd_8b = DRV_VER_UPD;
4554 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
4555 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
4556 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
4557 
4558 	if (BNXT_PF(bp)) {
4559 		u32 data[8];
4560 		int i;
4561 
4562 		memset(data, 0, sizeof(data));
4563 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
4564 			u16 cmd = bnxt_vf_req_snif[i];
4565 			unsigned int bit, idx;
4566 
4567 			idx = cmd / 32;
4568 			bit = cmd % 32;
4569 			data[idx] |= 1 << bit;
4570 		}
4571 
4572 		for (i = 0; i < 8; i++)
4573 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
4574 
4575 		req->enables |=
4576 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
4577 	}
4578 
4579 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
4580 		req->flags |= cpu_to_le32(
4581 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
4582 
4583 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
4584 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
4585 		u16 event_id = bnxt_async_events_arr[i];
4586 
4587 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
4588 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
4589 			continue;
4590 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
4591 	}
4592 	if (bmap && bmap_size) {
4593 		for (i = 0; i < bmap_size; i++) {
4594 			if (test_bit(i, bmap))
4595 				__set_bit(i, async_events_bmap);
4596 		}
4597 	}
4598 	for (i = 0; i < 8; i++)
4599 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
4600 
4601 	if (async_only)
4602 		req->enables =
4603 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
4604 
4605 	resp = hwrm_req_hold(bp, req);
4606 	rc = hwrm_req_send(bp, req);
4607 	if (!rc) {
4608 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
4609 		if (resp->flags &
4610 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
4611 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
4612 	}
4613 	hwrm_req_drop(bp, req);
4614 	return rc;
4615 }
4616 
4617 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
4618 {
4619 	struct hwrm_func_drv_unrgtr_input *req;
4620 	int rc;
4621 
4622 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
4623 		return 0;
4624 
4625 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
4626 	if (rc)
4627 		return rc;
4628 	return hwrm_req_send(bp, req);
4629 }
4630 
4631 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
4632 {
4633 	struct hwrm_tunnel_dst_port_free_input *req;
4634 	int rc;
4635 
4636 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
4637 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
4638 		return 0;
4639 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
4640 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
4641 		return 0;
4642 
4643 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
4644 	if (rc)
4645 		return rc;
4646 
4647 	req->tunnel_type = tunnel_type;
4648 
4649 	switch (tunnel_type) {
4650 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
4651 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
4652 		bp->vxlan_port = 0;
4653 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
4654 		break;
4655 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
4656 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
4657 		bp->nge_port = 0;
4658 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
4659 		break;
4660 	default:
4661 		break;
4662 	}
4663 
4664 	rc = hwrm_req_send(bp, req);
4665 	if (rc)
4666 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
4667 			   rc);
4668 	return rc;
4669 }
4670 
4671 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
4672 					   u8 tunnel_type)
4673 {
4674 	struct hwrm_tunnel_dst_port_alloc_output *resp;
4675 	struct hwrm_tunnel_dst_port_alloc_input *req;
4676 	int rc;
4677 
4678 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
4679 	if (rc)
4680 		return rc;
4681 
4682 	req->tunnel_type = tunnel_type;
4683 	req->tunnel_dst_port_val = port;
4684 
4685 	resp = hwrm_req_hold(bp, req);
4686 	rc = hwrm_req_send(bp, req);
4687 	if (rc) {
4688 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
4689 			   rc);
4690 		goto err_out;
4691 	}
4692 
4693 	switch (tunnel_type) {
4694 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
4695 		bp->vxlan_port = port;
4696 		bp->vxlan_fw_dst_port_id =
4697 			le16_to_cpu(resp->tunnel_dst_port_id);
4698 		break;
4699 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
4700 		bp->nge_port = port;
4701 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
4702 		break;
4703 	default:
4704 		break;
4705 	}
4706 
4707 err_out:
4708 	hwrm_req_drop(bp, req);
4709 	return rc;
4710 }
4711 
4712 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
4713 {
4714 	struct hwrm_cfa_l2_set_rx_mask_input *req;
4715 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4716 	int rc;
4717 
4718 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
4719 	if (rc)
4720 		return rc;
4721 
4722 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
4723 	req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
4724 	req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
4725 	req->mask = cpu_to_le32(vnic->rx_mask);
4726 	return hwrm_req_send_silent(bp, req);
4727 }
4728 
4729 #ifdef CONFIG_RFS_ACCEL
4730 static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
4731 					    struct bnxt_ntuple_filter *fltr)
4732 {
4733 	struct hwrm_cfa_ntuple_filter_free_input *req;
4734 	int rc;
4735 
4736 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
4737 	if (rc)
4738 		return rc;
4739 
4740 	req->ntuple_filter_id = fltr->filter_id;
4741 	return hwrm_req_send(bp, req);
4742 }
4743 
4744 #define BNXT_NTP_FLTR_FLAGS					\
4745 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
4746 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
4747 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR |	\
4748 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
4749 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
4750 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
4751 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
4752 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
4753 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
4754 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
4755 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
4756 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
4757 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
4758 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
4759 
4760 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
4761 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
4762 
4763 static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
4764 					     struct bnxt_ntuple_filter *fltr)
4765 {
4766 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
4767 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
4768 	struct flow_keys *keys = &fltr->fkeys;
4769 	struct bnxt_vnic_info *vnic;
4770 	u32 flags = 0;
4771 	int rc;
4772 
4773 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
4774 	if (rc)
4775 		return rc;
4776 
4777 	req->l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
4778 
4779 	if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
4780 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
4781 		req->dst_id = cpu_to_le16(fltr->rxq);
4782 	} else {
4783 		vnic = &bp->vnic_info[fltr->rxq + 1];
4784 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
4785 	}
4786 	req->flags = cpu_to_le32(flags);
4787 	req->enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
4788 
4789 	req->ethertype = htons(ETH_P_IP);
4790 	memcpy(req->src_macaddr, fltr->src_mac_addr, ETH_ALEN);
4791 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
4792 	req->ip_protocol = keys->basic.ip_proto;
4793 
4794 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
4795 		int i;
4796 
4797 		req->ethertype = htons(ETH_P_IPV6);
4798 		req->ip_addr_type =
4799 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
4800 		*(struct in6_addr *)&req->src_ipaddr[0] =
4801 			keys->addrs.v6addrs.src;
4802 		*(struct in6_addr *)&req->dst_ipaddr[0] =
4803 			keys->addrs.v6addrs.dst;
4804 		for (i = 0; i < 4; i++) {
4805 			req->src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
4806 			req->dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
4807 		}
4808 	} else {
4809 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
4810 		req->src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
4811 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
4812 		req->dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
4813 	}
4814 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
4815 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
4816 		req->tunnel_type =
4817 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
4818 	}
4819 
4820 	req->src_port = keys->ports.src;
4821 	req->src_port_mask = cpu_to_be16(0xffff);
4822 	req->dst_port = keys->ports.dst;
4823 	req->dst_port_mask = cpu_to_be16(0xffff);
4824 
4825 	resp = hwrm_req_hold(bp, req);
4826 	rc = hwrm_req_send(bp, req);
4827 	if (!rc)
4828 		fltr->filter_id = resp->ntuple_filter_id;
4829 	hwrm_req_drop(bp, req);
4830 	return rc;
4831 }
4832 #endif
4833 
4834 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
4835 				     const u8 *mac_addr)
4836 {
4837 	struct hwrm_cfa_l2_filter_alloc_output *resp;
4838 	struct hwrm_cfa_l2_filter_alloc_input *req;
4839 	int rc;
4840 
4841 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
4842 	if (rc)
4843 		return rc;
4844 
4845 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
4846 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
4847 		req->flags |=
4848 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
4849 	req->dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
4850 	req->enables =
4851 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
4852 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
4853 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
4854 	memcpy(req->l2_addr, mac_addr, ETH_ALEN);
4855 	req->l2_addr_mask[0] = 0xff;
4856 	req->l2_addr_mask[1] = 0xff;
4857 	req->l2_addr_mask[2] = 0xff;
4858 	req->l2_addr_mask[3] = 0xff;
4859 	req->l2_addr_mask[4] = 0xff;
4860 	req->l2_addr_mask[5] = 0xff;
4861 
4862 	resp = hwrm_req_hold(bp, req);
4863 	rc = hwrm_req_send(bp, req);
4864 	if (!rc)
4865 		bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
4866 							resp->l2_filter_id;
4867 	hwrm_req_drop(bp, req);
4868 	return rc;
4869 }
4870 
4871 static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
4872 {
4873 	struct hwrm_cfa_l2_filter_free_input *req;
4874 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
4875 	int rc;
4876 
4877 	/* Any associated ntuple filters will also be cleared by firmware. */
4878 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
4879 	if (rc)
4880 		return rc;
4881 	hwrm_req_hold(bp, req);
4882 	for (i = 0; i < num_of_vnics; i++) {
4883 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4884 
4885 		for (j = 0; j < vnic->uc_filter_count; j++) {
4886 			req->l2_filter_id = vnic->fw_l2_filter_id[j];
4887 
4888 			rc = hwrm_req_send(bp, req);
4889 		}
4890 		vnic->uc_filter_count = 0;
4891 	}
4892 	hwrm_req_drop(bp, req);
4893 	return rc;
4894 }
4895 
4896 static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
4897 {
4898 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4899 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
4900 	struct hwrm_vnic_tpa_cfg_input *req;
4901 	int rc;
4902 
4903 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
4904 		return 0;
4905 
4906 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
4907 	if (rc)
4908 		return rc;
4909 
4910 	if (tpa_flags) {
4911 		u16 mss = bp->dev->mtu - 40;
4912 		u32 nsegs, n, segs = 0, flags;
4913 
4914 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
4915 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
4916 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
4917 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
4918 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
4919 		if (tpa_flags & BNXT_FLAG_GRO)
4920 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
4921 
4922 		req->flags = cpu_to_le32(flags);
4923 
4924 		req->enables =
4925 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
4926 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
4927 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
4928 
4929 		/* Number of segs are log2 units, and first packet is not
4930 		 * included as part of this units.
4931 		 */
4932 		if (mss <= BNXT_RX_PAGE_SIZE) {
4933 			n = BNXT_RX_PAGE_SIZE / mss;
4934 			nsegs = (MAX_SKB_FRAGS - 1) * n;
4935 		} else {
4936 			n = mss / BNXT_RX_PAGE_SIZE;
4937 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
4938 				n++;
4939 			nsegs = (MAX_SKB_FRAGS - n) / n;
4940 		}
4941 
4942 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
4943 			segs = MAX_TPA_SEGS_P5;
4944 			max_aggs = bp->max_tpa;
4945 		} else {
4946 			segs = ilog2(nsegs);
4947 		}
4948 		req->max_agg_segs = cpu_to_le16(segs);
4949 		req->max_aggs = cpu_to_le16(max_aggs);
4950 
4951 		req->min_agg_len = cpu_to_le32(512);
4952 	}
4953 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
4954 
4955 	return hwrm_req_send(bp, req);
4956 }
4957 
4958 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
4959 {
4960 	struct bnxt_ring_grp_info *grp_info;
4961 
4962 	grp_info = &bp->grp_info[ring->grp_idx];
4963 	return grp_info->cp_fw_ring_id;
4964 }
4965 
4966 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
4967 {
4968 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
4969 		struct bnxt_napi *bnapi = rxr->bnapi;
4970 		struct bnxt_cp_ring_info *cpr;
4971 
4972 		cpr = bnapi->cp_ring.cp_ring_arr[BNXT_RX_HDL];
4973 		return cpr->cp_ring_struct.fw_ring_id;
4974 	} else {
4975 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
4976 	}
4977 }
4978 
4979 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
4980 {
4981 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
4982 		struct bnxt_napi *bnapi = txr->bnapi;
4983 		struct bnxt_cp_ring_info *cpr;
4984 
4985 		cpr = bnapi->cp_ring.cp_ring_arr[BNXT_TX_HDL];
4986 		return cpr->cp_ring_struct.fw_ring_id;
4987 	} else {
4988 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
4989 	}
4990 }
4991 
4992 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
4993 {
4994 	int entries;
4995 
4996 	if (bp->flags & BNXT_FLAG_CHIP_P5)
4997 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
4998 	else
4999 		entries = HW_HASH_INDEX_SIZE;
5000 
5001 	bp->rss_indir_tbl_entries = entries;
5002 	bp->rss_indir_tbl = kmalloc_array(entries, sizeof(*bp->rss_indir_tbl),
5003 					  GFP_KERNEL);
5004 	if (!bp->rss_indir_tbl)
5005 		return -ENOMEM;
5006 	return 0;
5007 }
5008 
5009 static void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp)
5010 {
5011 	u16 max_rings, max_entries, pad, i;
5012 
5013 	if (!bp->rx_nr_rings)
5014 		return;
5015 
5016 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5017 		max_rings = bp->rx_nr_rings - 1;
5018 	else
5019 		max_rings = bp->rx_nr_rings;
5020 
5021 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
5022 
5023 	for (i = 0; i < max_entries; i++)
5024 		bp->rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
5025 
5026 	pad = bp->rss_indir_tbl_entries - max_entries;
5027 	if (pad)
5028 		memset(&bp->rss_indir_tbl[i], 0, pad * sizeof(u16));
5029 }
5030 
5031 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
5032 {
5033 	u16 i, tbl_size, max_ring = 0;
5034 
5035 	if (!bp->rss_indir_tbl)
5036 		return 0;
5037 
5038 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
5039 	for (i = 0; i < tbl_size; i++)
5040 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
5041 	return max_ring;
5042 }
5043 
5044 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
5045 {
5046 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5047 		return DIV_ROUND_UP(rx_rings, BNXT_RSS_TABLE_ENTRIES_P5);
5048 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5049 		return 2;
5050 	return 1;
5051 }
5052 
5053 static void __bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
5054 {
5055 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
5056 	u16 i, j;
5057 
5058 	/* Fill the RSS indirection table with ring group ids */
5059 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
5060 		if (!no_rss)
5061 			j = bp->rss_indir_tbl[i];
5062 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
5063 	}
5064 }
5065 
5066 static void __bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
5067 				      struct bnxt_vnic_info *vnic)
5068 {
5069 	__le16 *ring_tbl = vnic->rss_table;
5070 	struct bnxt_rx_ring_info *rxr;
5071 	u16 tbl_size, i;
5072 
5073 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
5074 
5075 	for (i = 0; i < tbl_size; i++) {
5076 		u16 ring_id, j;
5077 
5078 		j = bp->rss_indir_tbl[i];
5079 		rxr = &bp->rx_ring[j];
5080 
5081 		ring_id = rxr->rx_ring_struct.fw_ring_id;
5082 		*ring_tbl++ = cpu_to_le16(ring_id);
5083 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5084 		*ring_tbl++ = cpu_to_le16(ring_id);
5085 	}
5086 }
5087 
5088 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
5089 {
5090 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5091 		__bnxt_fill_hw_rss_tbl_p5(bp, vnic);
5092 	else
5093 		__bnxt_fill_hw_rss_tbl(bp, vnic);
5094 }
5095 
5096 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
5097 {
5098 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5099 	struct hwrm_vnic_rss_cfg_input *req;
5100 	int rc;
5101 
5102 	if ((bp->flags & BNXT_FLAG_CHIP_P5) ||
5103 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
5104 		return 0;
5105 
5106 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
5107 	if (rc)
5108 		return rc;
5109 
5110 	if (set_rss) {
5111 		bnxt_fill_hw_rss_tbl(bp, vnic);
5112 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
5113 		req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
5114 		req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
5115 		req->hash_key_tbl_addr =
5116 			cpu_to_le64(vnic->rss_hash_key_dma_addr);
5117 	}
5118 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
5119 	return hwrm_req_send(bp, req);
5120 }
5121 
5122 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp, u16 vnic_id, bool set_rss)
5123 {
5124 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5125 	struct hwrm_vnic_rss_cfg_input *req;
5126 	dma_addr_t ring_tbl_map;
5127 	u32 i, nr_ctxs;
5128 	int rc;
5129 
5130 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
5131 	if (rc)
5132 		return rc;
5133 
5134 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5135 	if (!set_rss)
5136 		return hwrm_req_send(bp, req);
5137 
5138 	bnxt_fill_hw_rss_tbl(bp, vnic);
5139 	req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
5140 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
5141 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
5142 	ring_tbl_map = vnic->rss_table_dma_addr;
5143 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
5144 
5145 	hwrm_req_hold(bp, req);
5146 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
5147 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
5148 		req->ring_table_pair_index = i;
5149 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
5150 		rc = hwrm_req_send(bp, req);
5151 		if (rc)
5152 			goto exit;
5153 	}
5154 
5155 exit:
5156 	hwrm_req_drop(bp, req);
5157 	return rc;
5158 }
5159 
5160 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
5161 {
5162 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5163 	struct hwrm_vnic_plcmodes_cfg_input *req;
5164 	int rc;
5165 
5166 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
5167 	if (rc)
5168 		return rc;
5169 
5170 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
5171 				 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
5172 				 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
5173 	req->enables =
5174 		cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
5175 			    VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
5176 	/* thresholds not implemented in firmware yet */
5177 	req->jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
5178 	req->hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
5179 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5180 	return hwrm_req_send(bp, req);
5181 }
5182 
5183 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
5184 					u16 ctx_idx)
5185 {
5186 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
5187 
5188 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
5189 		return;
5190 
5191 	req->rss_cos_lb_ctx_id =
5192 		cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
5193 
5194 	hwrm_req_send(bp, req);
5195 	bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
5196 }
5197 
5198 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
5199 {
5200 	int i, j;
5201 
5202 	for (i = 0; i < bp->nr_vnics; i++) {
5203 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
5204 
5205 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
5206 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
5207 				bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
5208 		}
5209 	}
5210 	bp->rsscos_nr_ctxs = 0;
5211 }
5212 
5213 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
5214 {
5215 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
5216 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
5217 	int rc;
5218 
5219 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
5220 	if (rc)
5221 		return rc;
5222 
5223 	resp = hwrm_req_hold(bp, req);
5224 	rc = hwrm_req_send(bp, req);
5225 	if (!rc)
5226 		bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
5227 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
5228 	hwrm_req_drop(bp, req);
5229 
5230 	return rc;
5231 }
5232 
5233 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
5234 {
5235 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
5236 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
5237 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
5238 }
5239 
5240 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
5241 {
5242 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5243 	struct hwrm_vnic_cfg_input *req;
5244 	unsigned int ring = 0, grp_idx;
5245 	u16 def_vlan = 0;
5246 	int rc;
5247 
5248 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
5249 	if (rc)
5250 		return rc;
5251 
5252 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
5253 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
5254 
5255 		req->default_rx_ring_id =
5256 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
5257 		req->default_cmpl_ring_id =
5258 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
5259 		req->enables =
5260 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
5261 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
5262 		goto vnic_mru;
5263 	}
5264 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
5265 	/* Only RSS support for now TBD: COS & LB */
5266 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
5267 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
5268 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
5269 					   VNIC_CFG_REQ_ENABLES_MRU);
5270 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
5271 		req->rss_rule =
5272 			cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
5273 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
5274 					   VNIC_CFG_REQ_ENABLES_MRU);
5275 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
5276 	} else {
5277 		req->rss_rule = cpu_to_le16(0xffff);
5278 	}
5279 
5280 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
5281 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
5282 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
5283 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
5284 	} else {
5285 		req->cos_rule = cpu_to_le16(0xffff);
5286 	}
5287 
5288 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
5289 		ring = 0;
5290 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
5291 		ring = vnic_id - 1;
5292 	else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
5293 		ring = bp->rx_nr_rings - 1;
5294 
5295 	grp_idx = bp->rx_ring[ring].bnapi->index;
5296 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
5297 	req->lb_rule = cpu_to_le16(0xffff);
5298 vnic_mru:
5299 	req->mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + VLAN_HLEN);
5300 
5301 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5302 #ifdef CONFIG_BNXT_SRIOV
5303 	if (BNXT_VF(bp))
5304 		def_vlan = bp->vf.vlan;
5305 #endif
5306 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
5307 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
5308 	if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
5309 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
5310 
5311 	return hwrm_req_send(bp, req);
5312 }
5313 
5314 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
5315 {
5316 	if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
5317 		struct hwrm_vnic_free_input *req;
5318 
5319 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
5320 			return;
5321 
5322 		req->vnic_id =
5323 			cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
5324 
5325 		hwrm_req_send(bp, req);
5326 		bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
5327 	}
5328 }
5329 
5330 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
5331 {
5332 	u16 i;
5333 
5334 	for (i = 0; i < bp->nr_vnics; i++)
5335 		bnxt_hwrm_vnic_free_one(bp, i);
5336 }
5337 
5338 static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
5339 				unsigned int start_rx_ring_idx,
5340 				unsigned int nr_rings)
5341 {
5342 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
5343 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5344 	struct hwrm_vnic_alloc_output *resp;
5345 	struct hwrm_vnic_alloc_input *req;
5346 	int rc;
5347 
5348 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
5349 	if (rc)
5350 		return rc;
5351 
5352 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5353 		goto vnic_no_ring_grps;
5354 
5355 	/* map ring groups to this vnic */
5356 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
5357 		grp_idx = bp->rx_ring[i].bnapi->index;
5358 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
5359 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
5360 				   j, nr_rings);
5361 			break;
5362 		}
5363 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
5364 	}
5365 
5366 vnic_no_ring_grps:
5367 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
5368 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
5369 	if (vnic_id == 0)
5370 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
5371 
5372 	resp = hwrm_req_hold(bp, req);
5373 	rc = hwrm_req_send(bp, req);
5374 	if (!rc)
5375 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
5376 	hwrm_req_drop(bp, req);
5377 	return rc;
5378 }
5379 
5380 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
5381 {
5382 	struct hwrm_vnic_qcaps_output *resp;
5383 	struct hwrm_vnic_qcaps_input *req;
5384 	int rc;
5385 
5386 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
5387 	bp->flags &= ~(BNXT_FLAG_NEW_RSS_CAP | BNXT_FLAG_ROCE_MIRROR_CAP);
5388 	if (bp->hwrm_spec_code < 0x10600)
5389 		return 0;
5390 
5391 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
5392 	if (rc)
5393 		return rc;
5394 
5395 	resp = hwrm_req_hold(bp, req);
5396 	rc = hwrm_req_send(bp, req);
5397 	if (!rc) {
5398 		u32 flags = le32_to_cpu(resp->flags);
5399 
5400 		if (!(bp->flags & BNXT_FLAG_CHIP_P5) &&
5401 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
5402 			bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
5403 		if (flags &
5404 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
5405 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
5406 
5407 		/* Older P5 fw before EXT_HW_STATS support did not set
5408 		 * VLAN_STRIP_CAP properly.
5409 		 */
5410 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
5411 		    (BNXT_CHIP_P5_THOR(bp) &&
5412 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
5413 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
5414 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
5415 		if (bp->max_tpa_v2) {
5416 			if (BNXT_CHIP_P5_THOR(bp))
5417 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
5418 			else
5419 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5_SR2;
5420 		}
5421 	}
5422 	hwrm_req_drop(bp, req);
5423 	return rc;
5424 }
5425 
5426 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
5427 {
5428 	struct hwrm_ring_grp_alloc_output *resp;
5429 	struct hwrm_ring_grp_alloc_input *req;
5430 	int rc;
5431 	u16 i;
5432 
5433 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5434 		return 0;
5435 
5436 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
5437 	if (rc)
5438 		return rc;
5439 
5440 	resp = hwrm_req_hold(bp, req);
5441 	for (i = 0; i < bp->rx_nr_rings; i++) {
5442 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
5443 
5444 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
5445 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
5446 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
5447 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
5448 
5449 		rc = hwrm_req_send(bp, req);
5450 
5451 		if (rc)
5452 			break;
5453 
5454 		bp->grp_info[grp_idx].fw_grp_id =
5455 			le32_to_cpu(resp->ring_group_id);
5456 	}
5457 	hwrm_req_drop(bp, req);
5458 	return rc;
5459 }
5460 
5461 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
5462 {
5463 	struct hwrm_ring_grp_free_input *req;
5464 	u16 i;
5465 
5466 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5))
5467 		return;
5468 
5469 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
5470 		return;
5471 
5472 	hwrm_req_hold(bp, req);
5473 	for (i = 0; i < bp->cp_nr_rings; i++) {
5474 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
5475 			continue;
5476 		req->ring_group_id =
5477 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
5478 
5479 		hwrm_req_send(bp, req);
5480 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
5481 	}
5482 	hwrm_req_drop(bp, req);
5483 }
5484 
5485 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
5486 				    struct bnxt_ring_struct *ring,
5487 				    u32 ring_type, u32 map_index)
5488 {
5489 	struct hwrm_ring_alloc_output *resp;
5490 	struct hwrm_ring_alloc_input *req;
5491 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
5492 	struct bnxt_ring_grp_info *grp_info;
5493 	int rc, err = 0;
5494 	u16 ring_id;
5495 
5496 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
5497 	if (rc)
5498 		goto exit;
5499 
5500 	req->enables = 0;
5501 	if (rmem->nr_pages > 1) {
5502 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
5503 		/* Page size is in log2 units */
5504 		req->page_size = BNXT_PAGE_SHIFT;
5505 		req->page_tbl_depth = 1;
5506 	} else {
5507 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
5508 	}
5509 	req->fbo = 0;
5510 	/* Association of ring index with doorbell index and MSIX number */
5511 	req->logical_id = cpu_to_le16(map_index);
5512 
5513 	switch (ring_type) {
5514 	case HWRM_RING_ALLOC_TX: {
5515 		struct bnxt_tx_ring_info *txr;
5516 
5517 		txr = container_of(ring, struct bnxt_tx_ring_info,
5518 				   tx_ring_struct);
5519 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
5520 		/* Association of transmit ring with completion ring */
5521 		grp_info = &bp->grp_info[ring->grp_idx];
5522 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
5523 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
5524 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5525 		req->queue_id = cpu_to_le16(ring->queue_id);
5526 		break;
5527 	}
5528 	case HWRM_RING_ALLOC_RX:
5529 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
5530 		req->length = cpu_to_le32(bp->rx_ring_mask + 1);
5531 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5532 			u16 flags = 0;
5533 
5534 			/* Association of rx ring with stats context */
5535 			grp_info = &bp->grp_info[ring->grp_idx];
5536 			req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
5537 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5538 			req->enables |= cpu_to_le32(
5539 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
5540 			if (NET_IP_ALIGN == 2)
5541 				flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
5542 			req->flags = cpu_to_le16(flags);
5543 		}
5544 		break;
5545 	case HWRM_RING_ALLOC_AGG:
5546 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5547 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
5548 			/* Association of agg ring with rx ring */
5549 			grp_info = &bp->grp_info[ring->grp_idx];
5550 			req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
5551 			req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
5552 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5553 			req->enables |= cpu_to_le32(
5554 				RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
5555 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
5556 		} else {
5557 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
5558 		}
5559 		req->length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
5560 		break;
5561 	case HWRM_RING_ALLOC_CMPL:
5562 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
5563 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
5564 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5565 			/* Association of cp ring with nq */
5566 			grp_info = &bp->grp_info[map_index];
5567 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
5568 			req->cq_handle = cpu_to_le64(ring->handle);
5569 			req->enables |= cpu_to_le32(
5570 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
5571 		} else if (bp->flags & BNXT_FLAG_USING_MSIX) {
5572 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
5573 		}
5574 		break;
5575 	case HWRM_RING_ALLOC_NQ:
5576 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
5577 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
5578 		if (bp->flags & BNXT_FLAG_USING_MSIX)
5579 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
5580 		break;
5581 	default:
5582 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
5583 			   ring_type);
5584 		return -1;
5585 	}
5586 
5587 	resp = hwrm_req_hold(bp, req);
5588 	rc = hwrm_req_send(bp, req);
5589 	err = le16_to_cpu(resp->error_code);
5590 	ring_id = le16_to_cpu(resp->ring_id);
5591 	hwrm_req_drop(bp, req);
5592 
5593 exit:
5594 	if (rc || err) {
5595 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
5596 			   ring_type, rc, err);
5597 		return -EIO;
5598 	}
5599 	ring->fw_ring_id = ring_id;
5600 	return rc;
5601 }
5602 
5603 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
5604 {
5605 	int rc;
5606 
5607 	if (BNXT_PF(bp)) {
5608 		struct hwrm_func_cfg_input *req;
5609 
5610 		rc = hwrm_req_init(bp, req, HWRM_FUNC_CFG);
5611 		if (rc)
5612 			return rc;
5613 
5614 		req->fid = cpu_to_le16(0xffff);
5615 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
5616 		req->async_event_cr = cpu_to_le16(idx);
5617 		return hwrm_req_send(bp, req);
5618 	} else {
5619 		struct hwrm_func_vf_cfg_input *req;
5620 
5621 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
5622 		if (rc)
5623 			return rc;
5624 
5625 		req->enables =
5626 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
5627 		req->async_event_cr = cpu_to_le16(idx);
5628 		return hwrm_req_send(bp, req);
5629 	}
5630 }
5631 
5632 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
5633 			u32 map_idx, u32 xid)
5634 {
5635 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
5636 		if (BNXT_PF(bp))
5637 			db->doorbell = bp->bar1 + DB_PF_OFFSET_P5;
5638 		else
5639 			db->doorbell = bp->bar1 + DB_VF_OFFSET_P5;
5640 		switch (ring_type) {
5641 		case HWRM_RING_ALLOC_TX:
5642 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
5643 			break;
5644 		case HWRM_RING_ALLOC_RX:
5645 		case HWRM_RING_ALLOC_AGG:
5646 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
5647 			break;
5648 		case HWRM_RING_ALLOC_CMPL:
5649 			db->db_key64 = DBR_PATH_L2;
5650 			break;
5651 		case HWRM_RING_ALLOC_NQ:
5652 			db->db_key64 = DBR_PATH_L2;
5653 			break;
5654 		}
5655 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
5656 	} else {
5657 		db->doorbell = bp->bar1 + map_idx * 0x80;
5658 		switch (ring_type) {
5659 		case HWRM_RING_ALLOC_TX:
5660 			db->db_key32 = DB_KEY_TX;
5661 			break;
5662 		case HWRM_RING_ALLOC_RX:
5663 		case HWRM_RING_ALLOC_AGG:
5664 			db->db_key32 = DB_KEY_RX;
5665 			break;
5666 		case HWRM_RING_ALLOC_CMPL:
5667 			db->db_key32 = DB_KEY_CP;
5668 			break;
5669 		}
5670 	}
5671 }
5672 
5673 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
5674 {
5675 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
5676 	int i, rc = 0;
5677 	u32 type;
5678 
5679 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5680 		type = HWRM_RING_ALLOC_NQ;
5681 	else
5682 		type = HWRM_RING_ALLOC_CMPL;
5683 	for (i = 0; i < bp->cp_nr_rings; i++) {
5684 		struct bnxt_napi *bnapi = bp->bnapi[i];
5685 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5686 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5687 		u32 map_idx = ring->map_idx;
5688 		unsigned int vector;
5689 
5690 		vector = bp->irq_tbl[map_idx].vector;
5691 		disable_irq_nosync(vector);
5692 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5693 		if (rc) {
5694 			enable_irq(vector);
5695 			goto err_out;
5696 		}
5697 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
5698 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5699 		enable_irq(vector);
5700 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
5701 
5702 		if (!i) {
5703 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
5704 			if (rc)
5705 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
5706 		}
5707 	}
5708 
5709 	type = HWRM_RING_ALLOC_TX;
5710 	for (i = 0; i < bp->tx_nr_rings; i++) {
5711 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5712 		struct bnxt_ring_struct *ring;
5713 		u32 map_idx;
5714 
5715 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5716 			struct bnxt_napi *bnapi = txr->bnapi;
5717 			struct bnxt_cp_ring_info *cpr, *cpr2;
5718 			u32 type2 = HWRM_RING_ALLOC_CMPL;
5719 
5720 			cpr = &bnapi->cp_ring;
5721 			cpr2 = cpr->cp_ring_arr[BNXT_TX_HDL];
5722 			ring = &cpr2->cp_ring_struct;
5723 			ring->handle = BNXT_TX_HDL;
5724 			map_idx = bnapi->index;
5725 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
5726 			if (rc)
5727 				goto err_out;
5728 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
5729 				    ring->fw_ring_id);
5730 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
5731 		}
5732 		ring = &txr->tx_ring_struct;
5733 		map_idx = i;
5734 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5735 		if (rc)
5736 			goto err_out;
5737 		bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
5738 	}
5739 
5740 	type = HWRM_RING_ALLOC_RX;
5741 	for (i = 0; i < bp->rx_nr_rings; i++) {
5742 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5743 		struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
5744 		struct bnxt_napi *bnapi = rxr->bnapi;
5745 		u32 map_idx = bnapi->index;
5746 
5747 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5748 		if (rc)
5749 			goto err_out;
5750 		bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
5751 		/* If we have agg rings, post agg buffers first. */
5752 		if (!agg_rings)
5753 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
5754 		bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
5755 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5756 			struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5757 			u32 type2 = HWRM_RING_ALLOC_CMPL;
5758 			struct bnxt_cp_ring_info *cpr2;
5759 
5760 			cpr2 = cpr->cp_ring_arr[BNXT_RX_HDL];
5761 			ring = &cpr2->cp_ring_struct;
5762 			ring->handle = BNXT_RX_HDL;
5763 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
5764 			if (rc)
5765 				goto err_out;
5766 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
5767 				    ring->fw_ring_id);
5768 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
5769 		}
5770 	}
5771 
5772 	if (agg_rings) {
5773 		type = HWRM_RING_ALLOC_AGG;
5774 		for (i = 0; i < bp->rx_nr_rings; i++) {
5775 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5776 			struct bnxt_ring_struct *ring =
5777 						&rxr->rx_agg_ring_struct;
5778 			u32 grp_idx = ring->grp_idx;
5779 			u32 map_idx = grp_idx + bp->rx_nr_rings;
5780 
5781 			rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5782 			if (rc)
5783 				goto err_out;
5784 
5785 			bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
5786 				    ring->fw_ring_id);
5787 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
5788 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
5789 			bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
5790 		}
5791 	}
5792 err_out:
5793 	return rc;
5794 }
5795 
5796 static int hwrm_ring_free_send_msg(struct bnxt *bp,
5797 				   struct bnxt_ring_struct *ring,
5798 				   u32 ring_type, int cmpl_ring_id)
5799 {
5800 	struct hwrm_ring_free_output *resp;
5801 	struct hwrm_ring_free_input *req;
5802 	u16 error_code = 0;
5803 	int rc;
5804 
5805 	if (BNXT_NO_FW_ACCESS(bp))
5806 		return 0;
5807 
5808 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
5809 	if (rc)
5810 		goto exit;
5811 
5812 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
5813 	req->ring_type = ring_type;
5814 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
5815 
5816 	resp = hwrm_req_hold(bp, req);
5817 	rc = hwrm_req_send(bp, req);
5818 	error_code = le16_to_cpu(resp->error_code);
5819 	hwrm_req_drop(bp, req);
5820 exit:
5821 	if (rc || error_code) {
5822 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
5823 			   ring_type, rc, error_code);
5824 		return -EIO;
5825 	}
5826 	return 0;
5827 }
5828 
5829 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
5830 {
5831 	u32 type;
5832 	int i;
5833 
5834 	if (!bp->bnapi)
5835 		return;
5836 
5837 	for (i = 0; i < bp->tx_nr_rings; i++) {
5838 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5839 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
5840 
5841 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5842 			u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
5843 
5844 			hwrm_ring_free_send_msg(bp, ring,
5845 						RING_FREE_REQ_RING_TYPE_TX,
5846 						close_path ? cmpl_ring_id :
5847 						INVALID_HW_RING_ID);
5848 			ring->fw_ring_id = INVALID_HW_RING_ID;
5849 		}
5850 	}
5851 
5852 	for (i = 0; i < bp->rx_nr_rings; i++) {
5853 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5854 		struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
5855 		u32 grp_idx = rxr->bnapi->index;
5856 
5857 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5858 			u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5859 
5860 			hwrm_ring_free_send_msg(bp, ring,
5861 						RING_FREE_REQ_RING_TYPE_RX,
5862 						close_path ? cmpl_ring_id :
5863 						INVALID_HW_RING_ID);
5864 			ring->fw_ring_id = INVALID_HW_RING_ID;
5865 			bp->grp_info[grp_idx].rx_fw_ring_id =
5866 				INVALID_HW_RING_ID;
5867 		}
5868 	}
5869 
5870 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5871 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
5872 	else
5873 		type = RING_FREE_REQ_RING_TYPE_RX;
5874 	for (i = 0; i < bp->rx_nr_rings; i++) {
5875 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5876 		struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
5877 		u32 grp_idx = rxr->bnapi->index;
5878 
5879 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5880 			u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5881 
5882 			hwrm_ring_free_send_msg(bp, ring, type,
5883 						close_path ? cmpl_ring_id :
5884 						INVALID_HW_RING_ID);
5885 			ring->fw_ring_id = INVALID_HW_RING_ID;
5886 			bp->grp_info[grp_idx].agg_fw_ring_id =
5887 				INVALID_HW_RING_ID;
5888 		}
5889 	}
5890 
5891 	/* The completion rings are about to be freed.  After that the
5892 	 * IRQ doorbell will not work anymore.  So we need to disable
5893 	 * IRQ here.
5894 	 */
5895 	bnxt_disable_int_sync(bp);
5896 
5897 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5898 		type = RING_FREE_REQ_RING_TYPE_NQ;
5899 	else
5900 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
5901 	for (i = 0; i < bp->cp_nr_rings; i++) {
5902 		struct bnxt_napi *bnapi = bp->bnapi[i];
5903 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5904 		struct bnxt_ring_struct *ring;
5905 		int j;
5906 
5907 		for (j = 0; j < 2; j++) {
5908 			struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
5909 
5910 			if (cpr2) {
5911 				ring = &cpr2->cp_ring_struct;
5912 				if (ring->fw_ring_id == INVALID_HW_RING_ID)
5913 					continue;
5914 				hwrm_ring_free_send_msg(bp, ring,
5915 					RING_FREE_REQ_RING_TYPE_L2_CMPL,
5916 					INVALID_HW_RING_ID);
5917 				ring->fw_ring_id = INVALID_HW_RING_ID;
5918 			}
5919 		}
5920 		ring = &cpr->cp_ring_struct;
5921 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5922 			hwrm_ring_free_send_msg(bp, ring, type,
5923 						INVALID_HW_RING_ID);
5924 			ring->fw_ring_id = INVALID_HW_RING_ID;
5925 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
5926 		}
5927 	}
5928 }
5929 
5930 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
5931 			   bool shared);
5932 
5933 static int bnxt_hwrm_get_rings(struct bnxt *bp)
5934 {
5935 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
5936 	struct hwrm_func_qcfg_output *resp;
5937 	struct hwrm_func_qcfg_input *req;
5938 	int rc;
5939 
5940 	if (bp->hwrm_spec_code < 0x10601)
5941 		return 0;
5942 
5943 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
5944 	if (rc)
5945 		return rc;
5946 
5947 	req->fid = cpu_to_le16(0xffff);
5948 	resp = hwrm_req_hold(bp, req);
5949 	rc = hwrm_req_send(bp, req);
5950 	if (rc) {
5951 		hwrm_req_drop(bp, req);
5952 		return rc;
5953 	}
5954 
5955 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
5956 	if (BNXT_NEW_RM(bp)) {
5957 		u16 cp, stats;
5958 
5959 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
5960 		hw_resc->resv_hw_ring_grps =
5961 			le32_to_cpu(resp->alloc_hw_ring_grps);
5962 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
5963 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
5964 		stats = le16_to_cpu(resp->alloc_stat_ctx);
5965 		hw_resc->resv_irqs = cp;
5966 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5967 			int rx = hw_resc->resv_rx_rings;
5968 			int tx = hw_resc->resv_tx_rings;
5969 
5970 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
5971 				rx >>= 1;
5972 			if (cp < (rx + tx)) {
5973 				bnxt_trim_rings(bp, &rx, &tx, cp, false);
5974 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
5975 					rx <<= 1;
5976 				hw_resc->resv_rx_rings = rx;
5977 				hw_resc->resv_tx_rings = tx;
5978 			}
5979 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
5980 			hw_resc->resv_hw_ring_grps = rx;
5981 		}
5982 		hw_resc->resv_cp_rings = cp;
5983 		hw_resc->resv_stat_ctxs = stats;
5984 	}
5985 	hwrm_req_drop(bp, req);
5986 	return 0;
5987 }
5988 
5989 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
5990 {
5991 	struct hwrm_func_qcfg_output *resp;
5992 	struct hwrm_func_qcfg_input *req;
5993 	int rc;
5994 
5995 	if (bp->hwrm_spec_code < 0x10601)
5996 		return 0;
5997 
5998 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
5999 	if (rc)
6000 		return rc;
6001 
6002 	req->fid = cpu_to_le16(fid);
6003 	resp = hwrm_req_hold(bp, req);
6004 	rc = hwrm_req_send(bp, req);
6005 	if (!rc)
6006 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
6007 
6008 	hwrm_req_drop(bp, req);
6009 	return rc;
6010 }
6011 
6012 static bool bnxt_rfs_supported(struct bnxt *bp);
6013 
6014 static struct hwrm_func_cfg_input *
6015 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6016 			     int ring_grps, int cp_rings, int stats, int vnics)
6017 {
6018 	struct hwrm_func_cfg_input *req;
6019 	u32 enables = 0;
6020 
6021 	if (hwrm_req_init(bp, req, HWRM_FUNC_CFG))
6022 		return NULL;
6023 
6024 	req->fid = cpu_to_le16(0xffff);
6025 	enables |= tx_rings ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
6026 	req->num_tx_rings = cpu_to_le16(tx_rings);
6027 	if (BNXT_NEW_RM(bp)) {
6028 		enables |= rx_rings ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
6029 		enables |= stats ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
6030 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
6031 			enables |= cp_rings ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
6032 			enables |= tx_rings + ring_grps ?
6033 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6034 			enables |= rx_rings ?
6035 				FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6036 		} else {
6037 			enables |= cp_rings ?
6038 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6039 			enables |= ring_grps ?
6040 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS |
6041 				   FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6042 		}
6043 		enables |= vnics ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
6044 
6045 		req->num_rx_rings = cpu_to_le16(rx_rings);
6046 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
6047 			req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
6048 			req->num_msix = cpu_to_le16(cp_rings);
6049 			req->num_rsscos_ctxs =
6050 				cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
6051 		} else {
6052 			req->num_cmpl_rings = cpu_to_le16(cp_rings);
6053 			req->num_hw_ring_grps = cpu_to_le16(ring_grps);
6054 			req->num_rsscos_ctxs = cpu_to_le16(1);
6055 			if (!(bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
6056 			    bnxt_rfs_supported(bp))
6057 				req->num_rsscos_ctxs =
6058 					cpu_to_le16(ring_grps + 1);
6059 		}
6060 		req->num_stat_ctxs = cpu_to_le16(stats);
6061 		req->num_vnics = cpu_to_le16(vnics);
6062 	}
6063 	req->enables = cpu_to_le32(enables);
6064 	return req;
6065 }
6066 
6067 static struct hwrm_func_vf_cfg_input *
6068 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6069 			     int ring_grps, int cp_rings, int stats, int vnics)
6070 {
6071 	struct hwrm_func_vf_cfg_input *req;
6072 	u32 enables = 0;
6073 
6074 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
6075 		return NULL;
6076 
6077 	enables |= tx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
6078 	enables |= rx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
6079 			      FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6080 	enables |= stats ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
6081 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
6082 		enables |= tx_rings + ring_grps ?
6083 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6084 	} else {
6085 		enables |= cp_rings ?
6086 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6087 		enables |= ring_grps ?
6088 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
6089 	}
6090 	enables |= vnics ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
6091 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
6092 
6093 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
6094 	req->num_tx_rings = cpu_to_le16(tx_rings);
6095 	req->num_rx_rings = cpu_to_le16(rx_rings);
6096 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
6097 		req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
6098 		req->num_rsscos_ctxs = cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
6099 	} else {
6100 		req->num_cmpl_rings = cpu_to_le16(cp_rings);
6101 		req->num_hw_ring_grps = cpu_to_le16(ring_grps);
6102 		req->num_rsscos_ctxs = cpu_to_le16(BNXT_VF_MAX_RSS_CTX);
6103 	}
6104 	req->num_stat_ctxs = cpu_to_le16(stats);
6105 	req->num_vnics = cpu_to_le16(vnics);
6106 
6107 	req->enables = cpu_to_le32(enables);
6108 	return req;
6109 }
6110 
6111 static int
6112 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6113 			   int ring_grps, int cp_rings, int stats, int vnics)
6114 {
6115 	struct hwrm_func_cfg_input *req;
6116 	int rc;
6117 
6118 	req = __bnxt_hwrm_reserve_pf_rings(bp, tx_rings, rx_rings, ring_grps,
6119 					   cp_rings, stats, vnics);
6120 	if (!req)
6121 		return -ENOMEM;
6122 
6123 	if (!req->enables) {
6124 		hwrm_req_drop(bp, req);
6125 		return 0;
6126 	}
6127 
6128 	rc = hwrm_req_send(bp, req);
6129 	if (rc)
6130 		return rc;
6131 
6132 	if (bp->hwrm_spec_code < 0x10601)
6133 		bp->hw_resc.resv_tx_rings = tx_rings;
6134 
6135 	return bnxt_hwrm_get_rings(bp);
6136 }
6137 
6138 static int
6139 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6140 			   int ring_grps, int cp_rings, int stats, int vnics)
6141 {
6142 	struct hwrm_func_vf_cfg_input *req;
6143 	int rc;
6144 
6145 	if (!BNXT_NEW_RM(bp)) {
6146 		bp->hw_resc.resv_tx_rings = tx_rings;
6147 		return 0;
6148 	}
6149 
6150 	req = __bnxt_hwrm_reserve_vf_rings(bp, tx_rings, rx_rings, ring_grps,
6151 					   cp_rings, stats, vnics);
6152 	if (!req)
6153 		return -ENOMEM;
6154 
6155 	rc = hwrm_req_send(bp, req);
6156 	if (rc)
6157 		return rc;
6158 
6159 	return bnxt_hwrm_get_rings(bp);
6160 }
6161 
6162 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, int tx, int rx, int grp,
6163 				   int cp, int stat, int vnic)
6164 {
6165 	if (BNXT_PF(bp))
6166 		return bnxt_hwrm_reserve_pf_rings(bp, tx, rx, grp, cp, stat,
6167 						  vnic);
6168 	else
6169 		return bnxt_hwrm_reserve_vf_rings(bp, tx, rx, grp, cp, stat,
6170 						  vnic);
6171 }
6172 
6173 int bnxt_nq_rings_in_use(struct bnxt *bp)
6174 {
6175 	int cp = bp->cp_nr_rings;
6176 	int ulp_msix, ulp_base;
6177 
6178 	ulp_msix = bnxt_get_ulp_msix_num(bp);
6179 	if (ulp_msix) {
6180 		ulp_base = bnxt_get_ulp_msix_base(bp);
6181 		cp += ulp_msix;
6182 		if ((ulp_base + ulp_msix) > cp)
6183 			cp = ulp_base + ulp_msix;
6184 	}
6185 	return cp;
6186 }
6187 
6188 static int bnxt_cp_rings_in_use(struct bnxt *bp)
6189 {
6190 	int cp;
6191 
6192 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6193 		return bnxt_nq_rings_in_use(bp);
6194 
6195 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
6196 	return cp;
6197 }
6198 
6199 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
6200 {
6201 	int ulp_stat = bnxt_get_ulp_stat_ctxs(bp);
6202 	int cp = bp->cp_nr_rings;
6203 
6204 	if (!ulp_stat)
6205 		return cp;
6206 
6207 	if (bnxt_nq_rings_in_use(bp) > cp + bnxt_get_ulp_msix_num(bp))
6208 		return bnxt_get_ulp_msix_base(bp) + ulp_stat;
6209 
6210 	return cp + ulp_stat;
6211 }
6212 
6213 /* Check if a default RSS map needs to be setup.  This function is only
6214  * used on older firmware that does not require reserving RX rings.
6215  */
6216 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
6217 {
6218 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6219 
6220 	/* The RSS map is valid for RX rings set to resv_rx_rings */
6221 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
6222 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
6223 		if (!netif_is_rxfh_configured(bp->dev))
6224 			bnxt_set_dflt_rss_indir_tbl(bp);
6225 	}
6226 }
6227 
6228 static bool bnxt_need_reserve_rings(struct bnxt *bp)
6229 {
6230 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6231 	int cp = bnxt_cp_rings_in_use(bp);
6232 	int nq = bnxt_nq_rings_in_use(bp);
6233 	int rx = bp->rx_nr_rings, stat;
6234 	int vnic = 1, grp = rx;
6235 
6236 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
6237 	    bp->hwrm_spec_code >= 0x10601)
6238 		return true;
6239 
6240 	/* Old firmware does not need RX ring reservations but we still
6241 	 * need to setup a default RSS map when needed.  With new firmware
6242 	 * we go through RX ring reservations first and then set up the
6243 	 * RSS map for the successfully reserved RX rings when needed.
6244 	 */
6245 	if (!BNXT_NEW_RM(bp)) {
6246 		bnxt_check_rss_tbl_no_rmgr(bp);
6247 		return false;
6248 	}
6249 	if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
6250 		vnic = rx + 1;
6251 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
6252 		rx <<= 1;
6253 	stat = bnxt_get_func_stat_ctxs(bp);
6254 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
6255 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
6256 	    (hw_resc->resv_hw_ring_grps != grp &&
6257 	     !(bp->flags & BNXT_FLAG_CHIP_P5)))
6258 		return true;
6259 	if ((bp->flags & BNXT_FLAG_CHIP_P5) && BNXT_PF(bp) &&
6260 	    hw_resc->resv_irqs != nq)
6261 		return true;
6262 	return false;
6263 }
6264 
6265 static int __bnxt_reserve_rings(struct bnxt *bp)
6266 {
6267 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6268 	int cp = bnxt_nq_rings_in_use(bp);
6269 	int tx = bp->tx_nr_rings;
6270 	int rx = bp->rx_nr_rings;
6271 	int grp, rx_rings, rc;
6272 	int vnic = 1, stat;
6273 	bool sh = false;
6274 
6275 	if (!bnxt_need_reserve_rings(bp))
6276 		return 0;
6277 
6278 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
6279 		sh = true;
6280 	if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
6281 		vnic = rx + 1;
6282 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
6283 		rx <<= 1;
6284 	grp = bp->rx_nr_rings;
6285 	stat = bnxt_get_func_stat_ctxs(bp);
6286 
6287 	rc = bnxt_hwrm_reserve_rings(bp, tx, rx, grp, cp, stat, vnic);
6288 	if (rc)
6289 		return rc;
6290 
6291 	tx = hw_resc->resv_tx_rings;
6292 	if (BNXT_NEW_RM(bp)) {
6293 		rx = hw_resc->resv_rx_rings;
6294 		cp = hw_resc->resv_irqs;
6295 		grp = hw_resc->resv_hw_ring_grps;
6296 		vnic = hw_resc->resv_vnics;
6297 		stat = hw_resc->resv_stat_ctxs;
6298 	}
6299 
6300 	rx_rings = rx;
6301 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
6302 		if (rx >= 2) {
6303 			rx_rings = rx >> 1;
6304 		} else {
6305 			if (netif_running(bp->dev))
6306 				return -ENOMEM;
6307 
6308 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
6309 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
6310 			bp->dev->hw_features &= ~NETIF_F_LRO;
6311 			bp->dev->features &= ~NETIF_F_LRO;
6312 			bnxt_set_ring_params(bp);
6313 		}
6314 	}
6315 	rx_rings = min_t(int, rx_rings, grp);
6316 	cp = min_t(int, cp, bp->cp_nr_rings);
6317 	if (stat > bnxt_get_ulp_stat_ctxs(bp))
6318 		stat -= bnxt_get_ulp_stat_ctxs(bp);
6319 	cp = min_t(int, cp, stat);
6320 	rc = bnxt_trim_rings(bp, &rx_rings, &tx, cp, sh);
6321 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
6322 		rx = rx_rings << 1;
6323 	cp = sh ? max_t(int, tx, rx_rings) : tx + rx_rings;
6324 	bp->tx_nr_rings = tx;
6325 
6326 	/* If we cannot reserve all the RX rings, reset the RSS map only
6327 	 * if absolutely necessary
6328 	 */
6329 	if (rx_rings != bp->rx_nr_rings) {
6330 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
6331 			    rx_rings, bp->rx_nr_rings);
6332 		if (netif_is_rxfh_configured(bp->dev) &&
6333 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
6334 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
6335 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
6336 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
6337 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
6338 		}
6339 	}
6340 	bp->rx_nr_rings = rx_rings;
6341 	bp->cp_nr_rings = cp;
6342 
6343 	if (!tx || !rx || !cp || !grp || !vnic || !stat)
6344 		return -ENOMEM;
6345 
6346 	if (!netif_is_rxfh_configured(bp->dev))
6347 		bnxt_set_dflt_rss_indir_tbl(bp);
6348 
6349 	return rc;
6350 }
6351 
6352 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6353 				    int ring_grps, int cp_rings, int stats,
6354 				    int vnics)
6355 {
6356 	struct hwrm_func_vf_cfg_input *req;
6357 	u32 flags;
6358 
6359 	if (!BNXT_NEW_RM(bp))
6360 		return 0;
6361 
6362 	req = __bnxt_hwrm_reserve_vf_rings(bp, tx_rings, rx_rings, ring_grps,
6363 					   cp_rings, stats, vnics);
6364 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
6365 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
6366 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
6367 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
6368 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
6369 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
6370 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6371 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
6372 
6373 	req->flags = cpu_to_le32(flags);
6374 	return hwrm_req_send_silent(bp, req);
6375 }
6376 
6377 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6378 				    int ring_grps, int cp_rings, int stats,
6379 				    int vnics)
6380 {
6381 	struct hwrm_func_cfg_input *req;
6382 	u32 flags;
6383 
6384 	req = __bnxt_hwrm_reserve_pf_rings(bp, tx_rings, rx_rings, ring_grps,
6385 					   cp_rings, stats, vnics);
6386 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
6387 	if (BNXT_NEW_RM(bp)) {
6388 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
6389 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
6390 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
6391 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
6392 		if (bp->flags & BNXT_FLAG_CHIP_P5)
6393 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
6394 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
6395 		else
6396 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
6397 	}
6398 
6399 	req->flags = cpu_to_le32(flags);
6400 	return hwrm_req_send_silent(bp, req);
6401 }
6402 
6403 static int bnxt_hwrm_check_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6404 				 int ring_grps, int cp_rings, int stats,
6405 				 int vnics)
6406 {
6407 	if (bp->hwrm_spec_code < 0x10801)
6408 		return 0;
6409 
6410 	if (BNXT_PF(bp))
6411 		return bnxt_hwrm_check_pf_rings(bp, tx_rings, rx_rings,
6412 						ring_grps, cp_rings, stats,
6413 						vnics);
6414 
6415 	return bnxt_hwrm_check_vf_rings(bp, tx_rings, rx_rings, ring_grps,
6416 					cp_rings, stats, vnics);
6417 }
6418 
6419 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
6420 {
6421 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6422 	struct hwrm_ring_aggint_qcaps_output *resp;
6423 	struct hwrm_ring_aggint_qcaps_input *req;
6424 	int rc;
6425 
6426 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
6427 	coal_cap->num_cmpl_dma_aggr_max = 63;
6428 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
6429 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
6430 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
6431 	coal_cap->int_lat_tmr_min_max = 65535;
6432 	coal_cap->int_lat_tmr_max_max = 65535;
6433 	coal_cap->num_cmpl_aggr_int_max = 65535;
6434 	coal_cap->timer_units = 80;
6435 
6436 	if (bp->hwrm_spec_code < 0x10902)
6437 		return;
6438 
6439 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
6440 		return;
6441 
6442 	resp = hwrm_req_hold(bp, req);
6443 	rc = hwrm_req_send_silent(bp, req);
6444 	if (!rc) {
6445 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
6446 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
6447 		coal_cap->num_cmpl_dma_aggr_max =
6448 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
6449 		coal_cap->num_cmpl_dma_aggr_during_int_max =
6450 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
6451 		coal_cap->cmpl_aggr_dma_tmr_max =
6452 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
6453 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
6454 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
6455 		coal_cap->int_lat_tmr_min_max =
6456 			le16_to_cpu(resp->int_lat_tmr_min_max);
6457 		coal_cap->int_lat_tmr_max_max =
6458 			le16_to_cpu(resp->int_lat_tmr_max_max);
6459 		coal_cap->num_cmpl_aggr_int_max =
6460 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
6461 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
6462 	}
6463 	hwrm_req_drop(bp, req);
6464 }
6465 
6466 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
6467 {
6468 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6469 
6470 	return usec * 1000 / coal_cap->timer_units;
6471 }
6472 
6473 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
6474 	struct bnxt_coal *hw_coal,
6475 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
6476 {
6477 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6478 	u32 cmpl_params = coal_cap->cmpl_params;
6479 	u16 val, tmr, max, flags = 0;
6480 
6481 	max = hw_coal->bufs_per_record * 128;
6482 	if (hw_coal->budget)
6483 		max = hw_coal->bufs_per_record * hw_coal->budget;
6484 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
6485 
6486 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
6487 	req->num_cmpl_aggr_int = cpu_to_le16(val);
6488 
6489 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
6490 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
6491 
6492 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
6493 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
6494 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
6495 
6496 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
6497 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
6498 	req->int_lat_tmr_max = cpu_to_le16(tmr);
6499 
6500 	/* min timer set to 1/2 of interrupt timer */
6501 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
6502 		val = tmr / 2;
6503 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
6504 		req->int_lat_tmr_min = cpu_to_le16(val);
6505 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
6506 	}
6507 
6508 	/* buf timer set to 1/4 of interrupt timer */
6509 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
6510 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
6511 
6512 	if (cmpl_params &
6513 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
6514 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
6515 		val = clamp_t(u16, tmr, 1,
6516 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
6517 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
6518 		req->enables |=
6519 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
6520 	}
6521 
6522 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
6523 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
6524 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
6525 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
6526 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
6527 	req->flags = cpu_to_le16(flags);
6528 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
6529 }
6530 
6531 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
6532 				   struct bnxt_coal *hw_coal)
6533 {
6534 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
6535 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6536 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6537 	u32 nq_params = coal_cap->nq_params;
6538 	u16 tmr;
6539 	int rc;
6540 
6541 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
6542 		return 0;
6543 
6544 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
6545 	if (rc)
6546 		return rc;
6547 
6548 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
6549 	req->flags =
6550 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
6551 
6552 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
6553 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
6554 	req->int_lat_tmr_min = cpu_to_le16(tmr);
6555 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
6556 	return hwrm_req_send(bp, req);
6557 }
6558 
6559 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
6560 {
6561 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
6562 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6563 	struct bnxt_coal coal;
6564 	int rc;
6565 
6566 	/* Tick values in micro seconds.
6567 	 * 1 coal_buf x bufs_per_record = 1 completion record.
6568 	 */
6569 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
6570 
6571 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
6572 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
6573 
6574 	if (!bnapi->rx_ring)
6575 		return -ENODEV;
6576 
6577 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
6578 	if (rc)
6579 		return rc;
6580 
6581 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
6582 
6583 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
6584 
6585 	return hwrm_req_send(bp, req_rx);
6586 }
6587 
6588 int bnxt_hwrm_set_coal(struct bnxt *bp)
6589 {
6590 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx,
6591 							   *req;
6592 	int i, rc;
6593 
6594 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
6595 	if (rc)
6596 		return rc;
6597 
6598 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
6599 	if (rc) {
6600 		hwrm_req_drop(bp, req_rx);
6601 		return rc;
6602 	}
6603 
6604 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
6605 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
6606 
6607 	hwrm_req_hold(bp, req_rx);
6608 	hwrm_req_hold(bp, req_tx);
6609 	for (i = 0; i < bp->cp_nr_rings; i++) {
6610 		struct bnxt_napi *bnapi = bp->bnapi[i];
6611 		struct bnxt_coal *hw_coal;
6612 		u16 ring_id;
6613 
6614 		req = req_rx;
6615 		if (!bnapi->rx_ring) {
6616 			ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
6617 			req = req_tx;
6618 		} else {
6619 			ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
6620 		}
6621 		req->ring_id = cpu_to_le16(ring_id);
6622 
6623 		rc = hwrm_req_send(bp, req);
6624 		if (rc)
6625 			break;
6626 
6627 		if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6628 			continue;
6629 
6630 		if (bnapi->rx_ring && bnapi->tx_ring) {
6631 			req = req_tx;
6632 			ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
6633 			req->ring_id = cpu_to_le16(ring_id);
6634 			rc = hwrm_req_send(bp, req);
6635 			if (rc)
6636 				break;
6637 		}
6638 		if (bnapi->rx_ring)
6639 			hw_coal = &bp->rx_coal;
6640 		else
6641 			hw_coal = &bp->tx_coal;
6642 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
6643 	}
6644 	hwrm_req_drop(bp, req_rx);
6645 	hwrm_req_drop(bp, req_tx);
6646 	return rc;
6647 }
6648 
6649 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
6650 {
6651 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
6652 	struct hwrm_stat_ctx_free_input *req;
6653 	int i;
6654 
6655 	if (!bp->bnapi)
6656 		return;
6657 
6658 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6659 		return;
6660 
6661 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
6662 		return;
6663 	if (BNXT_FW_MAJ(bp) <= 20) {
6664 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
6665 			hwrm_req_drop(bp, req);
6666 			return;
6667 		}
6668 		hwrm_req_hold(bp, req0);
6669 	}
6670 	hwrm_req_hold(bp, req);
6671 	for (i = 0; i < bp->cp_nr_rings; i++) {
6672 		struct bnxt_napi *bnapi = bp->bnapi[i];
6673 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6674 
6675 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
6676 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
6677 			if (req0) {
6678 				req0->stat_ctx_id = req->stat_ctx_id;
6679 				hwrm_req_send(bp, req0);
6680 			}
6681 			hwrm_req_send(bp, req);
6682 
6683 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
6684 		}
6685 	}
6686 	hwrm_req_drop(bp, req);
6687 	if (req0)
6688 		hwrm_req_drop(bp, req0);
6689 }
6690 
6691 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
6692 {
6693 	struct hwrm_stat_ctx_alloc_output *resp;
6694 	struct hwrm_stat_ctx_alloc_input *req;
6695 	int rc, i;
6696 
6697 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6698 		return 0;
6699 
6700 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
6701 	if (rc)
6702 		return rc;
6703 
6704 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
6705 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
6706 
6707 	resp = hwrm_req_hold(bp, req);
6708 	for (i = 0; i < bp->cp_nr_rings; i++) {
6709 		struct bnxt_napi *bnapi = bp->bnapi[i];
6710 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6711 
6712 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
6713 
6714 		rc = hwrm_req_send(bp, req);
6715 		if (rc)
6716 			break;
6717 
6718 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
6719 
6720 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
6721 	}
6722 	hwrm_req_drop(bp, req);
6723 	return rc;
6724 }
6725 
6726 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
6727 {
6728 	struct hwrm_func_qcfg_output *resp;
6729 	struct hwrm_func_qcfg_input *req;
6730 	u32 min_db_offset = 0;
6731 	u16 flags;
6732 	int rc;
6733 
6734 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
6735 	if (rc)
6736 		return rc;
6737 
6738 	req->fid = cpu_to_le16(0xffff);
6739 	resp = hwrm_req_hold(bp, req);
6740 	rc = hwrm_req_send(bp, req);
6741 	if (rc)
6742 		goto func_qcfg_exit;
6743 
6744 #ifdef CONFIG_BNXT_SRIOV
6745 	if (BNXT_VF(bp)) {
6746 		struct bnxt_vf_info *vf = &bp->vf;
6747 
6748 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
6749 	} else {
6750 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
6751 	}
6752 #endif
6753 	flags = le16_to_cpu(resp->flags);
6754 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
6755 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
6756 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
6757 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
6758 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
6759 	}
6760 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
6761 		bp->flags |= BNXT_FLAG_MULTI_HOST;
6762 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
6763 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
6764 
6765 	switch (resp->port_partition_type) {
6766 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
6767 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
6768 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
6769 		bp->port_partition_type = resp->port_partition_type;
6770 		break;
6771 	}
6772 	if (bp->hwrm_spec_code < 0x10707 ||
6773 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
6774 		bp->br_mode = BRIDGE_MODE_VEB;
6775 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
6776 		bp->br_mode = BRIDGE_MODE_VEPA;
6777 	else
6778 		bp->br_mode = BRIDGE_MODE_UNDEF;
6779 
6780 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
6781 	if (!bp->max_mtu)
6782 		bp->max_mtu = BNXT_MAX_MTU;
6783 
6784 	if (bp->db_size)
6785 		goto func_qcfg_exit;
6786 
6787 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
6788 		if (BNXT_PF(bp))
6789 			min_db_offset = DB_PF_OFFSET_P5;
6790 		else
6791 			min_db_offset = DB_VF_OFFSET_P5;
6792 	}
6793 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
6794 				 1024);
6795 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
6796 	    bp->db_size <= min_db_offset)
6797 		bp->db_size = pci_resource_len(bp->pdev, 2);
6798 
6799 func_qcfg_exit:
6800 	hwrm_req_drop(bp, req);
6801 	return rc;
6802 }
6803 
6804 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_info *ctx,
6805 			struct hwrm_func_backing_store_qcaps_output *resp)
6806 {
6807 	struct bnxt_mem_init *mem_init;
6808 	u16 init_mask;
6809 	u8 init_val;
6810 	u8 *offset;
6811 	int i;
6812 
6813 	init_val = resp->ctx_kind_initializer;
6814 	init_mask = le16_to_cpu(resp->ctx_init_mask);
6815 	offset = &resp->qp_init_offset;
6816 	mem_init = &ctx->mem_init[BNXT_CTX_MEM_INIT_QP];
6817 	for (i = 0; i < BNXT_CTX_MEM_INIT_MAX; i++, mem_init++, offset++) {
6818 		mem_init->init_val = init_val;
6819 		mem_init->offset = BNXT_MEM_INVALID_OFFSET;
6820 		if (!init_mask)
6821 			continue;
6822 		if (i == BNXT_CTX_MEM_INIT_STAT)
6823 			offset = &resp->stat_init_offset;
6824 		if (init_mask & (1 << i))
6825 			mem_init->offset = *offset * 4;
6826 		else
6827 			mem_init->init_val = 0;
6828 	}
6829 	ctx->mem_init[BNXT_CTX_MEM_INIT_QP].size = ctx->qp_entry_size;
6830 	ctx->mem_init[BNXT_CTX_MEM_INIT_SRQ].size = ctx->srq_entry_size;
6831 	ctx->mem_init[BNXT_CTX_MEM_INIT_CQ].size = ctx->cq_entry_size;
6832 	ctx->mem_init[BNXT_CTX_MEM_INIT_VNIC].size = ctx->vnic_entry_size;
6833 	ctx->mem_init[BNXT_CTX_MEM_INIT_STAT].size = ctx->stat_entry_size;
6834 	ctx->mem_init[BNXT_CTX_MEM_INIT_MRAV].size = ctx->mrav_entry_size;
6835 }
6836 
6837 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
6838 {
6839 	struct hwrm_func_backing_store_qcaps_output *resp;
6840 	struct hwrm_func_backing_store_qcaps_input *req;
6841 	int rc;
6842 
6843 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx)
6844 		return 0;
6845 
6846 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
6847 	if (rc)
6848 		return rc;
6849 
6850 	resp = hwrm_req_hold(bp, req);
6851 	rc = hwrm_req_send_silent(bp, req);
6852 	if (!rc) {
6853 		struct bnxt_ctx_pg_info *ctx_pg;
6854 		struct bnxt_ctx_mem_info *ctx;
6855 		int i, tqm_rings;
6856 
6857 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
6858 		if (!ctx) {
6859 			rc = -ENOMEM;
6860 			goto ctx_err;
6861 		}
6862 		ctx->qp_max_entries = le32_to_cpu(resp->qp_max_entries);
6863 		ctx->qp_min_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
6864 		ctx->qp_max_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
6865 		ctx->qp_entry_size = le16_to_cpu(resp->qp_entry_size);
6866 		ctx->srq_max_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
6867 		ctx->srq_max_entries = le32_to_cpu(resp->srq_max_entries);
6868 		ctx->srq_entry_size = le16_to_cpu(resp->srq_entry_size);
6869 		ctx->cq_max_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
6870 		ctx->cq_max_entries = le32_to_cpu(resp->cq_max_entries);
6871 		ctx->cq_entry_size = le16_to_cpu(resp->cq_entry_size);
6872 		ctx->vnic_max_vnic_entries =
6873 			le16_to_cpu(resp->vnic_max_vnic_entries);
6874 		ctx->vnic_max_ring_table_entries =
6875 			le16_to_cpu(resp->vnic_max_ring_table_entries);
6876 		ctx->vnic_entry_size = le16_to_cpu(resp->vnic_entry_size);
6877 		ctx->stat_max_entries = le32_to_cpu(resp->stat_max_entries);
6878 		ctx->stat_entry_size = le16_to_cpu(resp->stat_entry_size);
6879 		ctx->tqm_entry_size = le16_to_cpu(resp->tqm_entry_size);
6880 		ctx->tqm_min_entries_per_ring =
6881 			le32_to_cpu(resp->tqm_min_entries_per_ring);
6882 		ctx->tqm_max_entries_per_ring =
6883 			le32_to_cpu(resp->tqm_max_entries_per_ring);
6884 		ctx->tqm_entries_multiple = resp->tqm_entries_multiple;
6885 		if (!ctx->tqm_entries_multiple)
6886 			ctx->tqm_entries_multiple = 1;
6887 		ctx->mrav_max_entries = le32_to_cpu(resp->mrav_max_entries);
6888 		ctx->mrav_entry_size = le16_to_cpu(resp->mrav_entry_size);
6889 		ctx->mrav_num_entries_units =
6890 			le16_to_cpu(resp->mrav_num_entries_units);
6891 		ctx->tim_entry_size = le16_to_cpu(resp->tim_entry_size);
6892 		ctx->tim_max_entries = le32_to_cpu(resp->tim_max_entries);
6893 
6894 		bnxt_init_ctx_initializer(ctx, resp);
6895 
6896 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
6897 		if (!ctx->tqm_fp_rings_count)
6898 			ctx->tqm_fp_rings_count = bp->max_q;
6899 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
6900 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
6901 
6902 		tqm_rings = ctx->tqm_fp_rings_count + BNXT_MAX_TQM_SP_RINGS;
6903 		ctx_pg = kcalloc(tqm_rings, sizeof(*ctx_pg), GFP_KERNEL);
6904 		if (!ctx_pg) {
6905 			kfree(ctx);
6906 			rc = -ENOMEM;
6907 			goto ctx_err;
6908 		}
6909 		for (i = 0; i < tqm_rings; i++, ctx_pg++)
6910 			ctx->tqm_mem[i] = ctx_pg;
6911 		bp->ctx = ctx;
6912 	} else {
6913 		rc = 0;
6914 	}
6915 ctx_err:
6916 	hwrm_req_drop(bp, req);
6917 	return rc;
6918 }
6919 
6920 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
6921 				  __le64 *pg_dir)
6922 {
6923 	if (!rmem->nr_pages)
6924 		return;
6925 
6926 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
6927 	if (rmem->depth >= 1) {
6928 		if (rmem->depth == 2)
6929 			*pg_attr |= 2;
6930 		else
6931 			*pg_attr |= 1;
6932 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
6933 	} else {
6934 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
6935 	}
6936 }
6937 
6938 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
6939 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
6940 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
6941 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
6942 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
6943 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
6944 
6945 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
6946 {
6947 	struct hwrm_func_backing_store_cfg_input *req;
6948 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
6949 	struct bnxt_ctx_pg_info *ctx_pg;
6950 	void **__req = (void **)&req;
6951 	u32 req_len = sizeof(*req);
6952 	__le32 *num_entries;
6953 	__le64 *pg_dir;
6954 	u32 flags = 0;
6955 	u8 *pg_attr;
6956 	u32 ena;
6957 	int rc;
6958 	int i;
6959 
6960 	if (!ctx)
6961 		return 0;
6962 
6963 	if (req_len > bp->hwrm_max_ext_req_len)
6964 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
6965 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
6966 	if (rc)
6967 		return rc;
6968 
6969 	req->enables = cpu_to_le32(enables);
6970 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
6971 		ctx_pg = &ctx->qp_mem;
6972 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
6973 		req->qp_num_qp1_entries = cpu_to_le16(ctx->qp_min_qp1_entries);
6974 		req->qp_num_l2_entries = cpu_to_le16(ctx->qp_max_l2_entries);
6975 		req->qp_entry_size = cpu_to_le16(ctx->qp_entry_size);
6976 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6977 				      &req->qpc_pg_size_qpc_lvl,
6978 				      &req->qpc_page_dir);
6979 	}
6980 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
6981 		ctx_pg = &ctx->srq_mem;
6982 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
6983 		req->srq_num_l2_entries = cpu_to_le16(ctx->srq_max_l2_entries);
6984 		req->srq_entry_size = cpu_to_le16(ctx->srq_entry_size);
6985 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6986 				      &req->srq_pg_size_srq_lvl,
6987 				      &req->srq_page_dir);
6988 	}
6989 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
6990 		ctx_pg = &ctx->cq_mem;
6991 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
6992 		req->cq_num_l2_entries = cpu_to_le16(ctx->cq_max_l2_entries);
6993 		req->cq_entry_size = cpu_to_le16(ctx->cq_entry_size);
6994 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6995 				      &req->cq_pg_size_cq_lvl,
6996 				      &req->cq_page_dir);
6997 	}
6998 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
6999 		ctx_pg = &ctx->vnic_mem;
7000 		req->vnic_num_vnic_entries =
7001 			cpu_to_le16(ctx->vnic_max_vnic_entries);
7002 		req->vnic_num_ring_table_entries =
7003 			cpu_to_le16(ctx->vnic_max_ring_table_entries);
7004 		req->vnic_entry_size = cpu_to_le16(ctx->vnic_entry_size);
7005 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7006 				      &req->vnic_pg_size_vnic_lvl,
7007 				      &req->vnic_page_dir);
7008 	}
7009 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
7010 		ctx_pg = &ctx->stat_mem;
7011 		req->stat_num_entries = cpu_to_le32(ctx->stat_max_entries);
7012 		req->stat_entry_size = cpu_to_le16(ctx->stat_entry_size);
7013 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7014 				      &req->stat_pg_size_stat_lvl,
7015 				      &req->stat_page_dir);
7016 	}
7017 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
7018 		ctx_pg = &ctx->mrav_mem;
7019 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
7020 		if (ctx->mrav_num_entries_units)
7021 			flags |=
7022 			FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
7023 		req->mrav_entry_size = cpu_to_le16(ctx->mrav_entry_size);
7024 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7025 				      &req->mrav_pg_size_mrav_lvl,
7026 				      &req->mrav_page_dir);
7027 	}
7028 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
7029 		ctx_pg = &ctx->tim_mem;
7030 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
7031 		req->tim_entry_size = cpu_to_le16(ctx->tim_entry_size);
7032 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7033 				      &req->tim_pg_size_tim_lvl,
7034 				      &req->tim_page_dir);
7035 	}
7036 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
7037 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
7038 	     pg_dir = &req->tqm_sp_page_dir,
7039 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP;
7040 	     i < BNXT_MAX_TQM_RINGS;
7041 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
7042 		if (!(enables & ena))
7043 			continue;
7044 
7045 		req->tqm_entry_size = cpu_to_le16(ctx->tqm_entry_size);
7046 		ctx_pg = ctx->tqm_mem[i];
7047 		*num_entries = cpu_to_le32(ctx_pg->entries);
7048 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
7049 	}
7050 	req->flags = cpu_to_le32(flags);
7051 	return hwrm_req_send(bp, req);
7052 }
7053 
7054 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
7055 				  struct bnxt_ctx_pg_info *ctx_pg)
7056 {
7057 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7058 
7059 	rmem->page_size = BNXT_PAGE_SIZE;
7060 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
7061 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
7062 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
7063 	if (rmem->depth >= 1)
7064 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
7065 	return bnxt_alloc_ring(bp, rmem);
7066 }
7067 
7068 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
7069 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
7070 				  u8 depth, struct bnxt_mem_init *mem_init)
7071 {
7072 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7073 	int rc;
7074 
7075 	if (!mem_size)
7076 		return -EINVAL;
7077 
7078 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
7079 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
7080 		ctx_pg->nr_pages = 0;
7081 		return -EINVAL;
7082 	}
7083 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
7084 		int nr_tbls, i;
7085 
7086 		rmem->depth = 2;
7087 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
7088 					     GFP_KERNEL);
7089 		if (!ctx_pg->ctx_pg_tbl)
7090 			return -ENOMEM;
7091 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
7092 		rmem->nr_pages = nr_tbls;
7093 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
7094 		if (rc)
7095 			return rc;
7096 		for (i = 0; i < nr_tbls; i++) {
7097 			struct bnxt_ctx_pg_info *pg_tbl;
7098 
7099 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
7100 			if (!pg_tbl)
7101 				return -ENOMEM;
7102 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
7103 			rmem = &pg_tbl->ring_mem;
7104 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
7105 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
7106 			rmem->depth = 1;
7107 			rmem->nr_pages = MAX_CTX_PAGES;
7108 			rmem->mem_init = mem_init;
7109 			if (i == (nr_tbls - 1)) {
7110 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
7111 
7112 				if (rem)
7113 					rmem->nr_pages = rem;
7114 			}
7115 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
7116 			if (rc)
7117 				break;
7118 		}
7119 	} else {
7120 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
7121 		if (rmem->nr_pages > 1 || depth)
7122 			rmem->depth = 1;
7123 		rmem->mem_init = mem_init;
7124 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
7125 	}
7126 	return rc;
7127 }
7128 
7129 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
7130 				  struct bnxt_ctx_pg_info *ctx_pg)
7131 {
7132 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7133 
7134 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
7135 	    ctx_pg->ctx_pg_tbl) {
7136 		int i, nr_tbls = rmem->nr_pages;
7137 
7138 		for (i = 0; i < nr_tbls; i++) {
7139 			struct bnxt_ctx_pg_info *pg_tbl;
7140 			struct bnxt_ring_mem_info *rmem2;
7141 
7142 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
7143 			if (!pg_tbl)
7144 				continue;
7145 			rmem2 = &pg_tbl->ring_mem;
7146 			bnxt_free_ring(bp, rmem2);
7147 			ctx_pg->ctx_pg_arr[i] = NULL;
7148 			kfree(pg_tbl);
7149 			ctx_pg->ctx_pg_tbl[i] = NULL;
7150 		}
7151 		kfree(ctx_pg->ctx_pg_tbl);
7152 		ctx_pg->ctx_pg_tbl = NULL;
7153 	}
7154 	bnxt_free_ring(bp, rmem);
7155 	ctx_pg->nr_pages = 0;
7156 }
7157 
7158 void bnxt_free_ctx_mem(struct bnxt *bp)
7159 {
7160 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
7161 	int i;
7162 
7163 	if (!ctx)
7164 		return;
7165 
7166 	if (ctx->tqm_mem[0]) {
7167 		for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
7168 			bnxt_free_ctx_pg_tbls(bp, ctx->tqm_mem[i]);
7169 		kfree(ctx->tqm_mem[0]);
7170 		ctx->tqm_mem[0] = NULL;
7171 	}
7172 
7173 	bnxt_free_ctx_pg_tbls(bp, &ctx->tim_mem);
7174 	bnxt_free_ctx_pg_tbls(bp, &ctx->mrav_mem);
7175 	bnxt_free_ctx_pg_tbls(bp, &ctx->stat_mem);
7176 	bnxt_free_ctx_pg_tbls(bp, &ctx->vnic_mem);
7177 	bnxt_free_ctx_pg_tbls(bp, &ctx->cq_mem);
7178 	bnxt_free_ctx_pg_tbls(bp, &ctx->srq_mem);
7179 	bnxt_free_ctx_pg_tbls(bp, &ctx->qp_mem);
7180 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
7181 }
7182 
7183 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
7184 {
7185 	struct bnxt_ctx_pg_info *ctx_pg;
7186 	struct bnxt_ctx_mem_info *ctx;
7187 	struct bnxt_mem_init *init;
7188 	u32 mem_size, ena, entries;
7189 	u32 entries_sp, min;
7190 	u32 num_mr, num_ah;
7191 	u32 extra_srqs = 0;
7192 	u32 extra_qps = 0;
7193 	u8 pg_lvl = 1;
7194 	int i, rc;
7195 
7196 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
7197 	if (rc) {
7198 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
7199 			   rc);
7200 		return rc;
7201 	}
7202 	ctx = bp->ctx;
7203 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
7204 		return 0;
7205 
7206 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
7207 		pg_lvl = 2;
7208 		extra_qps = 65536;
7209 		extra_srqs = 8192;
7210 	}
7211 
7212 	ctx_pg = &ctx->qp_mem;
7213 	ctx_pg->entries = ctx->qp_min_qp1_entries + ctx->qp_max_l2_entries +
7214 			  extra_qps;
7215 	if (ctx->qp_entry_size) {
7216 		mem_size = ctx->qp_entry_size * ctx_pg->entries;
7217 		init = &ctx->mem_init[BNXT_CTX_MEM_INIT_QP];
7218 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, init);
7219 		if (rc)
7220 			return rc;
7221 	}
7222 
7223 	ctx_pg = &ctx->srq_mem;
7224 	ctx_pg->entries = ctx->srq_max_l2_entries + extra_srqs;
7225 	if (ctx->srq_entry_size) {
7226 		mem_size = ctx->srq_entry_size * ctx_pg->entries;
7227 		init = &ctx->mem_init[BNXT_CTX_MEM_INIT_SRQ];
7228 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, init);
7229 		if (rc)
7230 			return rc;
7231 	}
7232 
7233 	ctx_pg = &ctx->cq_mem;
7234 	ctx_pg->entries = ctx->cq_max_l2_entries + extra_qps * 2;
7235 	if (ctx->cq_entry_size) {
7236 		mem_size = ctx->cq_entry_size * ctx_pg->entries;
7237 		init = &ctx->mem_init[BNXT_CTX_MEM_INIT_CQ];
7238 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, init);
7239 		if (rc)
7240 			return rc;
7241 	}
7242 
7243 	ctx_pg = &ctx->vnic_mem;
7244 	ctx_pg->entries = ctx->vnic_max_vnic_entries +
7245 			  ctx->vnic_max_ring_table_entries;
7246 	if (ctx->vnic_entry_size) {
7247 		mem_size = ctx->vnic_entry_size * ctx_pg->entries;
7248 		init = &ctx->mem_init[BNXT_CTX_MEM_INIT_VNIC];
7249 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, init);
7250 		if (rc)
7251 			return rc;
7252 	}
7253 
7254 	ctx_pg = &ctx->stat_mem;
7255 	ctx_pg->entries = ctx->stat_max_entries;
7256 	if (ctx->stat_entry_size) {
7257 		mem_size = ctx->stat_entry_size * ctx_pg->entries;
7258 		init = &ctx->mem_init[BNXT_CTX_MEM_INIT_STAT];
7259 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, init);
7260 		if (rc)
7261 			return rc;
7262 	}
7263 
7264 	ena = 0;
7265 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
7266 		goto skip_rdma;
7267 
7268 	ctx_pg = &ctx->mrav_mem;
7269 	/* 128K extra is needed to accommodate static AH context
7270 	 * allocation by f/w.
7271 	 */
7272 	num_mr = 1024 * 256;
7273 	num_ah = 1024 * 128;
7274 	ctx_pg->entries = num_mr + num_ah;
7275 	if (ctx->mrav_entry_size) {
7276 		mem_size = ctx->mrav_entry_size * ctx_pg->entries;
7277 		init = &ctx->mem_init[BNXT_CTX_MEM_INIT_MRAV];
7278 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 2, init);
7279 		if (rc)
7280 			return rc;
7281 	}
7282 	ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
7283 	if (ctx->mrav_num_entries_units)
7284 		ctx_pg->entries =
7285 			((num_mr / ctx->mrav_num_entries_units) << 16) |
7286 			 (num_ah / ctx->mrav_num_entries_units);
7287 
7288 	ctx_pg = &ctx->tim_mem;
7289 	ctx_pg->entries = ctx->qp_mem.entries;
7290 	if (ctx->tim_entry_size) {
7291 		mem_size = ctx->tim_entry_size * ctx_pg->entries;
7292 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, NULL);
7293 		if (rc)
7294 			return rc;
7295 	}
7296 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
7297 
7298 skip_rdma:
7299 	min = ctx->tqm_min_entries_per_ring;
7300 	entries_sp = ctx->vnic_max_vnic_entries + ctx->qp_max_l2_entries +
7301 		     2 * (extra_qps + ctx->qp_min_qp1_entries) + min;
7302 	entries_sp = roundup(entries_sp, ctx->tqm_entries_multiple);
7303 	entries = ctx->qp_max_l2_entries + 2 * (extra_qps + ctx->qp_min_qp1_entries);
7304 	entries = roundup(entries, ctx->tqm_entries_multiple);
7305 	entries = clamp_t(u32, entries, min, ctx->tqm_max_entries_per_ring);
7306 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++) {
7307 		ctx_pg = ctx->tqm_mem[i];
7308 		ctx_pg->entries = i ? entries : entries_sp;
7309 		if (ctx->tqm_entry_size) {
7310 			mem_size = ctx->tqm_entry_size * ctx_pg->entries;
7311 			rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1,
7312 						    NULL);
7313 			if (rc)
7314 				return rc;
7315 		}
7316 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
7317 	}
7318 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
7319 	rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
7320 	if (rc) {
7321 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
7322 			   rc);
7323 		return rc;
7324 	}
7325 	ctx->flags |= BNXT_CTX_FLAG_INITED;
7326 	return 0;
7327 }
7328 
7329 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
7330 {
7331 	struct hwrm_func_resource_qcaps_output *resp;
7332 	struct hwrm_func_resource_qcaps_input *req;
7333 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7334 	int rc;
7335 
7336 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
7337 	if (rc)
7338 		return rc;
7339 
7340 	req->fid = cpu_to_le16(0xffff);
7341 	resp = hwrm_req_hold(bp, req);
7342 	rc = hwrm_req_send_silent(bp, req);
7343 	if (rc)
7344 		goto hwrm_func_resc_qcaps_exit;
7345 
7346 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
7347 	if (!all)
7348 		goto hwrm_func_resc_qcaps_exit;
7349 
7350 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
7351 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
7352 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
7353 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
7354 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
7355 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
7356 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
7357 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
7358 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
7359 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
7360 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
7361 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
7362 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
7363 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
7364 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
7365 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
7366 
7367 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
7368 		u16 max_msix = le16_to_cpu(resp->max_msix);
7369 
7370 		hw_resc->max_nqs = max_msix;
7371 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
7372 	}
7373 
7374 	if (BNXT_PF(bp)) {
7375 		struct bnxt_pf_info *pf = &bp->pf;
7376 
7377 		pf->vf_resv_strategy =
7378 			le16_to_cpu(resp->vf_reservation_strategy);
7379 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
7380 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
7381 	}
7382 hwrm_func_resc_qcaps_exit:
7383 	hwrm_req_drop(bp, req);
7384 	return rc;
7385 }
7386 
7387 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
7388 {
7389 	struct hwrm_port_mac_ptp_qcfg_output *resp;
7390 	struct hwrm_port_mac_ptp_qcfg_input *req;
7391 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
7392 	u8 flags;
7393 	int rc;
7394 
7395 	if (bp->hwrm_spec_code < 0x10801) {
7396 		rc = -ENODEV;
7397 		goto no_ptp;
7398 	}
7399 
7400 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
7401 	if (rc)
7402 		goto no_ptp;
7403 
7404 	req->port_id = cpu_to_le16(bp->pf.port_id);
7405 	resp = hwrm_req_hold(bp, req);
7406 	rc = hwrm_req_send(bp, req);
7407 	if (rc)
7408 		goto exit;
7409 
7410 	flags = resp->flags;
7411 	if (!(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
7412 		rc = -ENODEV;
7413 		goto exit;
7414 	}
7415 	if (!ptp) {
7416 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
7417 		if (!ptp) {
7418 			rc = -ENOMEM;
7419 			goto exit;
7420 		}
7421 		ptp->bp = bp;
7422 		bp->ptp_cfg = ptp;
7423 	}
7424 	if (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK) {
7425 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
7426 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
7427 	} else if (bp->flags & BNXT_FLAG_CHIP_P5) {
7428 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
7429 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
7430 	} else {
7431 		rc = -ENODEV;
7432 		goto exit;
7433 	}
7434 	rc = bnxt_ptp_init(bp);
7435 	if (rc)
7436 		netdev_warn(bp->dev, "PTP initialization failed.\n");
7437 exit:
7438 	hwrm_req_drop(bp, req);
7439 	if (!rc)
7440 		return 0;
7441 
7442 no_ptp:
7443 	bnxt_ptp_clear(bp);
7444 	kfree(ptp);
7445 	bp->ptp_cfg = NULL;
7446 	return rc;
7447 }
7448 
7449 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
7450 {
7451 	struct hwrm_func_qcaps_output *resp;
7452 	struct hwrm_func_qcaps_input *req;
7453 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7454 	u32 flags, flags_ext;
7455 	int rc;
7456 
7457 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
7458 	if (rc)
7459 		return rc;
7460 
7461 	req->fid = cpu_to_le16(0xffff);
7462 	resp = hwrm_req_hold(bp, req);
7463 	rc = hwrm_req_send(bp, req);
7464 	if (rc)
7465 		goto hwrm_func_qcaps_exit;
7466 
7467 	flags = le32_to_cpu(resp->flags);
7468 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
7469 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
7470 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
7471 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
7472 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
7473 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
7474 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
7475 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
7476 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
7477 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
7478 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
7479 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
7480 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
7481 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
7482 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
7483 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
7484 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
7485 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
7486 
7487 	flags_ext = le32_to_cpu(resp->flags_ext);
7488 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
7489 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
7490 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
7491 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
7492 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
7493 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
7494 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
7495 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
7496 
7497 	bp->tx_push_thresh = 0;
7498 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
7499 	    BNXT_FW_MAJ(bp) > 217)
7500 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
7501 
7502 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
7503 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
7504 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
7505 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
7506 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
7507 	if (!hw_resc->max_hw_ring_grps)
7508 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
7509 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
7510 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
7511 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
7512 
7513 	if (BNXT_PF(bp)) {
7514 		struct bnxt_pf_info *pf = &bp->pf;
7515 
7516 		pf->fw_fid = le16_to_cpu(resp->fid);
7517 		pf->port_id = le16_to_cpu(resp->port_id);
7518 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
7519 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
7520 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
7521 		pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
7522 		pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
7523 		pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
7524 		pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
7525 		pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
7526 		pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
7527 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
7528 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
7529 			bp->flags |= BNXT_FLAG_WOL_CAP;
7530 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
7531 			__bnxt_hwrm_ptp_qcfg(bp);
7532 		} else {
7533 			bnxt_ptp_clear(bp);
7534 			kfree(bp->ptp_cfg);
7535 			bp->ptp_cfg = NULL;
7536 		}
7537 	} else {
7538 #ifdef CONFIG_BNXT_SRIOV
7539 		struct bnxt_vf_info *vf = &bp->vf;
7540 
7541 		vf->fw_fid = le16_to_cpu(resp->fid);
7542 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
7543 #endif
7544 	}
7545 
7546 hwrm_func_qcaps_exit:
7547 	hwrm_req_drop(bp, req);
7548 	return rc;
7549 }
7550 
7551 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
7552 {
7553 	struct hwrm_dbg_qcaps_output *resp;
7554 	struct hwrm_dbg_qcaps_input *req;
7555 	int rc;
7556 
7557 	bp->fw_dbg_cap = 0;
7558 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
7559 		return;
7560 
7561 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
7562 	if (rc)
7563 		return;
7564 
7565 	req->fid = cpu_to_le16(0xffff);
7566 	resp = hwrm_req_hold(bp, req);
7567 	rc = hwrm_req_send(bp, req);
7568 	if (rc)
7569 		goto hwrm_dbg_qcaps_exit;
7570 
7571 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
7572 
7573 hwrm_dbg_qcaps_exit:
7574 	hwrm_req_drop(bp, req);
7575 }
7576 
7577 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
7578 
7579 static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
7580 {
7581 	int rc;
7582 
7583 	rc = __bnxt_hwrm_func_qcaps(bp);
7584 	if (rc)
7585 		return rc;
7586 
7587 	bnxt_hwrm_dbg_qcaps(bp);
7588 
7589 	rc = bnxt_hwrm_queue_qportcfg(bp);
7590 	if (rc) {
7591 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
7592 		return rc;
7593 	}
7594 	if (bp->hwrm_spec_code >= 0x10803) {
7595 		rc = bnxt_alloc_ctx_mem(bp);
7596 		if (rc)
7597 			return rc;
7598 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
7599 		if (!rc)
7600 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
7601 	}
7602 	return 0;
7603 }
7604 
7605 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
7606 {
7607 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
7608 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
7609 	u32 flags;
7610 	int rc;
7611 
7612 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
7613 		return 0;
7614 
7615 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
7616 	if (rc)
7617 		return rc;
7618 
7619 	resp = hwrm_req_hold(bp, req);
7620 	rc = hwrm_req_send(bp, req);
7621 	if (rc)
7622 		goto hwrm_cfa_adv_qcaps_exit;
7623 
7624 	flags = le32_to_cpu(resp->flags);
7625 	if (flags &
7626 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
7627 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
7628 
7629 hwrm_cfa_adv_qcaps_exit:
7630 	hwrm_req_drop(bp, req);
7631 	return rc;
7632 }
7633 
7634 static int __bnxt_alloc_fw_health(struct bnxt *bp)
7635 {
7636 	if (bp->fw_health)
7637 		return 0;
7638 
7639 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
7640 	if (!bp->fw_health)
7641 		return -ENOMEM;
7642 
7643 	mutex_init(&bp->fw_health->lock);
7644 	return 0;
7645 }
7646 
7647 static int bnxt_alloc_fw_health(struct bnxt *bp)
7648 {
7649 	int rc;
7650 
7651 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
7652 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
7653 		return 0;
7654 
7655 	rc = __bnxt_alloc_fw_health(bp);
7656 	if (rc) {
7657 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
7658 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
7659 		return rc;
7660 	}
7661 
7662 	return 0;
7663 }
7664 
7665 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
7666 {
7667 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
7668 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
7669 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
7670 }
7671 
7672 bool bnxt_is_fw_healthy(struct bnxt *bp)
7673 {
7674 	if (bp->fw_health && bp->fw_health->status_reliable) {
7675 		u32 fw_status;
7676 
7677 		fw_status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
7678 		if (fw_status && !BNXT_FW_IS_HEALTHY(fw_status))
7679 			return false;
7680 	}
7681 
7682 	return true;
7683 }
7684 
7685 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
7686 {
7687 	struct bnxt_fw_health *fw_health = bp->fw_health;
7688 	u32 reg_type;
7689 
7690 	if (!fw_health)
7691 		return;
7692 
7693 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
7694 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
7695 		fw_health->status_reliable = false;
7696 
7697 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
7698 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
7699 		fw_health->resets_reliable = false;
7700 }
7701 
7702 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
7703 {
7704 	void __iomem *hs;
7705 	u32 status_loc;
7706 	u32 reg_type;
7707 	u32 sig;
7708 
7709 	if (bp->fw_health)
7710 		bp->fw_health->status_reliable = false;
7711 
7712 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
7713 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
7714 
7715 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
7716 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
7717 		if (!bp->chip_num) {
7718 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
7719 			bp->chip_num = readl(bp->bar0 +
7720 					     BNXT_FW_HEALTH_WIN_BASE +
7721 					     BNXT_GRC_REG_CHIP_NUM);
7722 		}
7723 		if (!BNXT_CHIP_P5(bp))
7724 			return;
7725 
7726 		status_loc = BNXT_GRC_REG_STATUS_P5 |
7727 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
7728 	} else {
7729 		status_loc = readl(hs + offsetof(struct hcomm_status,
7730 						 fw_status_loc));
7731 	}
7732 
7733 	if (__bnxt_alloc_fw_health(bp)) {
7734 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
7735 		return;
7736 	}
7737 
7738 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
7739 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
7740 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
7741 		__bnxt_map_fw_health_reg(bp, status_loc);
7742 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
7743 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
7744 	}
7745 
7746 	bp->fw_health->status_reliable = true;
7747 }
7748 
7749 static int bnxt_map_fw_health_regs(struct bnxt *bp)
7750 {
7751 	struct bnxt_fw_health *fw_health = bp->fw_health;
7752 	u32 reg_base = 0xffffffff;
7753 	int i;
7754 
7755 	bp->fw_health->status_reliable = false;
7756 	bp->fw_health->resets_reliable = false;
7757 	/* Only pre-map the monitoring GRC registers using window 3 */
7758 	for (i = 0; i < 4; i++) {
7759 		u32 reg = fw_health->regs[i];
7760 
7761 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
7762 			continue;
7763 		if (reg_base == 0xffffffff)
7764 			reg_base = reg & BNXT_GRC_BASE_MASK;
7765 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
7766 			return -ERANGE;
7767 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
7768 	}
7769 	bp->fw_health->status_reliable = true;
7770 	bp->fw_health->resets_reliable = true;
7771 	if (reg_base == 0xffffffff)
7772 		return 0;
7773 
7774 	__bnxt_map_fw_health_reg(bp, reg_base);
7775 	return 0;
7776 }
7777 
7778 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
7779 {
7780 	struct bnxt_fw_health *fw_health = bp->fw_health;
7781 	struct hwrm_error_recovery_qcfg_output *resp;
7782 	struct hwrm_error_recovery_qcfg_input *req;
7783 	int rc, i;
7784 
7785 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
7786 		return 0;
7787 
7788 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
7789 	if (rc)
7790 		return rc;
7791 
7792 	resp = hwrm_req_hold(bp, req);
7793 	rc = hwrm_req_send(bp, req);
7794 	if (rc)
7795 		goto err_recovery_out;
7796 	fw_health->flags = le32_to_cpu(resp->flags);
7797 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
7798 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
7799 		rc = -EINVAL;
7800 		goto err_recovery_out;
7801 	}
7802 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
7803 	fw_health->master_func_wait_dsecs =
7804 		le32_to_cpu(resp->master_func_wait_period);
7805 	fw_health->normal_func_wait_dsecs =
7806 		le32_to_cpu(resp->normal_func_wait_period);
7807 	fw_health->post_reset_wait_dsecs =
7808 		le32_to_cpu(resp->master_func_wait_period_after_reset);
7809 	fw_health->post_reset_max_wait_dsecs =
7810 		le32_to_cpu(resp->max_bailout_time_after_reset);
7811 	fw_health->regs[BNXT_FW_HEALTH_REG] =
7812 		le32_to_cpu(resp->fw_health_status_reg);
7813 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
7814 		le32_to_cpu(resp->fw_heartbeat_reg);
7815 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
7816 		le32_to_cpu(resp->fw_reset_cnt_reg);
7817 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
7818 		le32_to_cpu(resp->reset_inprogress_reg);
7819 	fw_health->fw_reset_inprog_reg_mask =
7820 		le32_to_cpu(resp->reset_inprogress_reg_mask);
7821 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
7822 	if (fw_health->fw_reset_seq_cnt >= 16) {
7823 		rc = -EINVAL;
7824 		goto err_recovery_out;
7825 	}
7826 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
7827 		fw_health->fw_reset_seq_regs[i] =
7828 			le32_to_cpu(resp->reset_reg[i]);
7829 		fw_health->fw_reset_seq_vals[i] =
7830 			le32_to_cpu(resp->reset_reg_val[i]);
7831 		fw_health->fw_reset_seq_delay_msec[i] =
7832 			resp->delay_after_reset[i];
7833 	}
7834 err_recovery_out:
7835 	hwrm_req_drop(bp, req);
7836 	if (!rc)
7837 		rc = bnxt_map_fw_health_regs(bp);
7838 	if (rc)
7839 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
7840 	return rc;
7841 }
7842 
7843 static int bnxt_hwrm_func_reset(struct bnxt *bp)
7844 {
7845 	struct hwrm_func_reset_input *req;
7846 	int rc;
7847 
7848 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
7849 	if (rc)
7850 		return rc;
7851 
7852 	req->enables = 0;
7853 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
7854 	return hwrm_req_send(bp, req);
7855 }
7856 
7857 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
7858 {
7859 	struct hwrm_nvm_get_dev_info_output nvm_info;
7860 
7861 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
7862 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
7863 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
7864 			 nvm_info.nvm_cfg_ver_upd);
7865 }
7866 
7867 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
7868 {
7869 	struct hwrm_queue_qportcfg_output *resp;
7870 	struct hwrm_queue_qportcfg_input *req;
7871 	u8 i, j, *qptr;
7872 	bool no_rdma;
7873 	int rc = 0;
7874 
7875 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
7876 	if (rc)
7877 		return rc;
7878 
7879 	resp = hwrm_req_hold(bp, req);
7880 	rc = hwrm_req_send(bp, req);
7881 	if (rc)
7882 		goto qportcfg_exit;
7883 
7884 	if (!resp->max_configurable_queues) {
7885 		rc = -EINVAL;
7886 		goto qportcfg_exit;
7887 	}
7888 	bp->max_tc = resp->max_configurable_queues;
7889 	bp->max_lltc = resp->max_configurable_lossless_queues;
7890 	if (bp->max_tc > BNXT_MAX_QUEUE)
7891 		bp->max_tc = BNXT_MAX_QUEUE;
7892 
7893 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
7894 	qptr = &resp->queue_id0;
7895 	for (i = 0, j = 0; i < bp->max_tc; i++) {
7896 		bp->q_info[j].queue_id = *qptr;
7897 		bp->q_ids[i] = *qptr++;
7898 		bp->q_info[j].queue_profile = *qptr++;
7899 		bp->tc_to_qidx[j] = j;
7900 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
7901 		    (no_rdma && BNXT_PF(bp)))
7902 			j++;
7903 	}
7904 	bp->max_q = bp->max_tc;
7905 	bp->max_tc = max_t(u8, j, 1);
7906 
7907 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
7908 		bp->max_tc = 1;
7909 
7910 	if (bp->max_lltc > bp->max_tc)
7911 		bp->max_lltc = bp->max_tc;
7912 
7913 qportcfg_exit:
7914 	hwrm_req_drop(bp, req);
7915 	return rc;
7916 }
7917 
7918 static int bnxt_hwrm_poll(struct bnxt *bp)
7919 {
7920 	struct hwrm_ver_get_input *req;
7921 	int rc;
7922 
7923 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
7924 	if (rc)
7925 		return rc;
7926 
7927 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
7928 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
7929 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
7930 
7931 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
7932 	rc = hwrm_req_send(bp, req);
7933 	return rc;
7934 }
7935 
7936 static int bnxt_hwrm_ver_get(struct bnxt *bp)
7937 {
7938 	struct hwrm_ver_get_output *resp;
7939 	struct hwrm_ver_get_input *req;
7940 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
7941 	u32 dev_caps_cfg, hwrm_ver;
7942 	int rc, len;
7943 
7944 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
7945 	if (rc)
7946 		return rc;
7947 
7948 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
7949 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
7950 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
7951 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
7952 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
7953 
7954 	resp = hwrm_req_hold(bp, req);
7955 	rc = hwrm_req_send(bp, req);
7956 	if (rc)
7957 		goto hwrm_ver_get_exit;
7958 
7959 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
7960 
7961 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
7962 			     resp->hwrm_intf_min_8b << 8 |
7963 			     resp->hwrm_intf_upd_8b;
7964 	if (resp->hwrm_intf_maj_8b < 1) {
7965 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
7966 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
7967 			    resp->hwrm_intf_upd_8b);
7968 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
7969 	}
7970 
7971 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
7972 			HWRM_VERSION_UPDATE;
7973 
7974 	if (bp->hwrm_spec_code > hwrm_ver)
7975 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
7976 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
7977 			 HWRM_VERSION_UPDATE);
7978 	else
7979 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
7980 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
7981 			 resp->hwrm_intf_upd_8b);
7982 
7983 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
7984 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
7985 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
7986 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
7987 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
7988 		len = FW_VER_STR_LEN;
7989 	} else {
7990 		fw_maj = resp->hwrm_fw_maj_8b;
7991 		fw_min = resp->hwrm_fw_min_8b;
7992 		fw_bld = resp->hwrm_fw_bld_8b;
7993 		fw_rsv = resp->hwrm_fw_rsvd_8b;
7994 		len = BC_HWRM_STR_LEN;
7995 	}
7996 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
7997 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
7998 		 fw_rsv);
7999 
8000 	if (strlen(resp->active_pkg_name)) {
8001 		int fw_ver_len = strlen(bp->fw_ver_str);
8002 
8003 		snprintf(bp->fw_ver_str + fw_ver_len,
8004 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
8005 			 resp->active_pkg_name);
8006 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
8007 	}
8008 
8009 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
8010 	if (!bp->hwrm_cmd_timeout)
8011 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
8012 
8013 	if (resp->hwrm_intf_maj_8b >= 1) {
8014 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
8015 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
8016 	}
8017 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
8018 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
8019 
8020 	bp->chip_num = le16_to_cpu(resp->chip_num);
8021 	bp->chip_rev = resp->chip_rev;
8022 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
8023 	    !resp->chip_metal)
8024 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
8025 
8026 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
8027 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
8028 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
8029 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
8030 
8031 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
8032 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
8033 
8034 	if (dev_caps_cfg &
8035 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
8036 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
8037 
8038 	if (dev_caps_cfg &
8039 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
8040 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
8041 
8042 	if (dev_caps_cfg &
8043 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
8044 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
8045 
8046 hwrm_ver_get_exit:
8047 	hwrm_req_drop(bp, req);
8048 	return rc;
8049 }
8050 
8051 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
8052 {
8053 	struct hwrm_fw_set_time_input *req;
8054 	struct tm tm;
8055 	time64_t now = ktime_get_real_seconds();
8056 	int rc;
8057 
8058 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
8059 	    bp->hwrm_spec_code < 0x10400)
8060 		return -EOPNOTSUPP;
8061 
8062 	time64_to_tm(now, 0, &tm);
8063 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
8064 	if (rc)
8065 		return rc;
8066 
8067 	req->year = cpu_to_le16(1900 + tm.tm_year);
8068 	req->month = 1 + tm.tm_mon;
8069 	req->day = tm.tm_mday;
8070 	req->hour = tm.tm_hour;
8071 	req->minute = tm.tm_min;
8072 	req->second = tm.tm_sec;
8073 	return hwrm_req_send(bp, req);
8074 }
8075 
8076 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
8077 {
8078 	u64 sw_tmp;
8079 
8080 	hw &= mask;
8081 	sw_tmp = (*sw & ~mask) | hw;
8082 	if (hw < (*sw & mask))
8083 		sw_tmp += mask + 1;
8084 	WRITE_ONCE(*sw, sw_tmp);
8085 }
8086 
8087 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
8088 				    int count, bool ignore_zero)
8089 {
8090 	int i;
8091 
8092 	for (i = 0; i < count; i++) {
8093 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
8094 
8095 		if (ignore_zero && !hw)
8096 			continue;
8097 
8098 		if (masks[i] == -1ULL)
8099 			sw_stats[i] = hw;
8100 		else
8101 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
8102 	}
8103 }
8104 
8105 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
8106 {
8107 	if (!stats->hw_stats)
8108 		return;
8109 
8110 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
8111 				stats->hw_masks, stats->len / 8, false);
8112 }
8113 
8114 static void bnxt_accumulate_all_stats(struct bnxt *bp)
8115 {
8116 	struct bnxt_stats_mem *ring0_stats;
8117 	bool ignore_zero = false;
8118 	int i;
8119 
8120 	/* Chip bug.  Counter intermittently becomes 0. */
8121 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8122 		ignore_zero = true;
8123 
8124 	for (i = 0; i < bp->cp_nr_rings; i++) {
8125 		struct bnxt_napi *bnapi = bp->bnapi[i];
8126 		struct bnxt_cp_ring_info *cpr;
8127 		struct bnxt_stats_mem *stats;
8128 
8129 		cpr = &bnapi->cp_ring;
8130 		stats = &cpr->stats;
8131 		if (!i)
8132 			ring0_stats = stats;
8133 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
8134 					ring0_stats->hw_masks,
8135 					ring0_stats->len / 8, ignore_zero);
8136 	}
8137 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
8138 		struct bnxt_stats_mem *stats = &bp->port_stats;
8139 		__le64 *hw_stats = stats->hw_stats;
8140 		u64 *sw_stats = stats->sw_stats;
8141 		u64 *masks = stats->hw_masks;
8142 		int cnt;
8143 
8144 		cnt = sizeof(struct rx_port_stats) / 8;
8145 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
8146 
8147 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
8148 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
8149 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
8150 		cnt = sizeof(struct tx_port_stats) / 8;
8151 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
8152 	}
8153 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
8154 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
8155 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
8156 	}
8157 }
8158 
8159 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
8160 {
8161 	struct hwrm_port_qstats_input *req;
8162 	struct bnxt_pf_info *pf = &bp->pf;
8163 	int rc;
8164 
8165 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
8166 		return 0;
8167 
8168 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
8169 		return -EOPNOTSUPP;
8170 
8171 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
8172 	if (rc)
8173 		return rc;
8174 
8175 	req->flags = flags;
8176 	req->port_id = cpu_to_le16(pf->port_id);
8177 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
8178 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
8179 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
8180 	return hwrm_req_send(bp, req);
8181 }
8182 
8183 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
8184 {
8185 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
8186 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
8187 	struct hwrm_port_qstats_ext_output *resp_qs;
8188 	struct hwrm_port_qstats_ext_input *req_qs;
8189 	struct bnxt_pf_info *pf = &bp->pf;
8190 	u32 tx_stat_size;
8191 	int rc;
8192 
8193 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
8194 		return 0;
8195 
8196 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
8197 		return -EOPNOTSUPP;
8198 
8199 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
8200 	if (rc)
8201 		return rc;
8202 
8203 	req_qs->flags = flags;
8204 	req_qs->port_id = cpu_to_le16(pf->port_id);
8205 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
8206 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
8207 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
8208 		       sizeof(struct tx_port_stats_ext) : 0;
8209 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
8210 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
8211 	resp_qs = hwrm_req_hold(bp, req_qs);
8212 	rc = hwrm_req_send(bp, req_qs);
8213 	if (!rc) {
8214 		bp->fw_rx_stats_ext_size =
8215 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
8216 		if (BNXT_FW_MAJ(bp) < 220 &&
8217 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
8218 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
8219 
8220 		bp->fw_tx_stats_ext_size = tx_stat_size ?
8221 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
8222 	} else {
8223 		bp->fw_rx_stats_ext_size = 0;
8224 		bp->fw_tx_stats_ext_size = 0;
8225 	}
8226 	hwrm_req_drop(bp, req_qs);
8227 
8228 	if (flags)
8229 		return rc;
8230 
8231 	if (bp->fw_tx_stats_ext_size <=
8232 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
8233 		bp->pri2cos_valid = 0;
8234 		return rc;
8235 	}
8236 
8237 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
8238 	if (rc)
8239 		return rc;
8240 
8241 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
8242 
8243 	resp_qc = hwrm_req_hold(bp, req_qc);
8244 	rc = hwrm_req_send(bp, req_qc);
8245 	if (!rc) {
8246 		u8 *pri2cos;
8247 		int i, j;
8248 
8249 		pri2cos = &resp_qc->pri0_cos_queue_id;
8250 		for (i = 0; i < 8; i++) {
8251 			u8 queue_id = pri2cos[i];
8252 			u8 queue_idx;
8253 
8254 			/* Per port queue IDs start from 0, 10, 20, etc */
8255 			queue_idx = queue_id % 10;
8256 			if (queue_idx > BNXT_MAX_QUEUE) {
8257 				bp->pri2cos_valid = false;
8258 				hwrm_req_drop(bp, req_qc);
8259 				return rc;
8260 			}
8261 			for (j = 0; j < bp->max_q; j++) {
8262 				if (bp->q_ids[j] == queue_id)
8263 					bp->pri2cos_idx[i] = queue_idx;
8264 			}
8265 		}
8266 		bp->pri2cos_valid = true;
8267 	}
8268 	hwrm_req_drop(bp, req_qc);
8269 
8270 	return rc;
8271 }
8272 
8273 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
8274 {
8275 	bnxt_hwrm_tunnel_dst_port_free(bp,
8276 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
8277 	bnxt_hwrm_tunnel_dst_port_free(bp,
8278 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
8279 }
8280 
8281 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
8282 {
8283 	int rc, i;
8284 	u32 tpa_flags = 0;
8285 
8286 	if (set_tpa)
8287 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
8288 	else if (BNXT_NO_FW_ACCESS(bp))
8289 		return 0;
8290 	for (i = 0; i < bp->nr_vnics; i++) {
8291 		rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
8292 		if (rc) {
8293 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
8294 				   i, rc);
8295 			return rc;
8296 		}
8297 	}
8298 	return 0;
8299 }
8300 
8301 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
8302 {
8303 	int i;
8304 
8305 	for (i = 0; i < bp->nr_vnics; i++)
8306 		bnxt_hwrm_vnic_set_rss(bp, i, false);
8307 }
8308 
8309 static void bnxt_clear_vnic(struct bnxt *bp)
8310 {
8311 	if (!bp->vnic_info)
8312 		return;
8313 
8314 	bnxt_hwrm_clear_vnic_filter(bp);
8315 	if (!(bp->flags & BNXT_FLAG_CHIP_P5)) {
8316 		/* clear all RSS setting before free vnic ctx */
8317 		bnxt_hwrm_clear_vnic_rss(bp);
8318 		bnxt_hwrm_vnic_ctx_free(bp);
8319 	}
8320 	/* before free the vnic, undo the vnic tpa settings */
8321 	if (bp->flags & BNXT_FLAG_TPA)
8322 		bnxt_set_tpa(bp, false);
8323 	bnxt_hwrm_vnic_free(bp);
8324 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8325 		bnxt_hwrm_vnic_ctx_free(bp);
8326 }
8327 
8328 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
8329 				    bool irq_re_init)
8330 {
8331 	bnxt_clear_vnic(bp);
8332 	bnxt_hwrm_ring_free(bp, close_path);
8333 	bnxt_hwrm_ring_grp_free(bp);
8334 	if (irq_re_init) {
8335 		bnxt_hwrm_stat_ctx_free(bp);
8336 		bnxt_hwrm_free_tunnel_ports(bp);
8337 	}
8338 }
8339 
8340 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
8341 {
8342 	struct hwrm_func_cfg_input *req;
8343 	u8 evb_mode;
8344 	int rc;
8345 
8346 	if (br_mode == BRIDGE_MODE_VEB)
8347 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
8348 	else if (br_mode == BRIDGE_MODE_VEPA)
8349 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
8350 	else
8351 		return -EINVAL;
8352 
8353 	rc = hwrm_req_init(bp, req, HWRM_FUNC_CFG);
8354 	if (rc)
8355 		return rc;
8356 
8357 	req->fid = cpu_to_le16(0xffff);
8358 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
8359 	req->evb_mode = evb_mode;
8360 	return hwrm_req_send(bp, req);
8361 }
8362 
8363 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
8364 {
8365 	struct hwrm_func_cfg_input *req;
8366 	int rc;
8367 
8368 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
8369 		return 0;
8370 
8371 	rc = hwrm_req_init(bp, req, HWRM_FUNC_CFG);
8372 	if (rc)
8373 		return rc;
8374 
8375 	req->fid = cpu_to_le16(0xffff);
8376 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
8377 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
8378 	if (size == 128)
8379 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
8380 
8381 	return hwrm_req_send(bp, req);
8382 }
8383 
8384 static int __bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
8385 {
8386 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
8387 	int rc;
8388 
8389 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
8390 		goto skip_rss_ctx;
8391 
8392 	/* allocate context for vnic */
8393 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
8394 	if (rc) {
8395 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
8396 			   vnic_id, rc);
8397 		goto vnic_setup_err;
8398 	}
8399 	bp->rsscos_nr_ctxs++;
8400 
8401 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8402 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
8403 		if (rc) {
8404 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
8405 				   vnic_id, rc);
8406 			goto vnic_setup_err;
8407 		}
8408 		bp->rsscos_nr_ctxs++;
8409 	}
8410 
8411 skip_rss_ctx:
8412 	/* configure default vnic, ring grp */
8413 	rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
8414 	if (rc) {
8415 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
8416 			   vnic_id, rc);
8417 		goto vnic_setup_err;
8418 	}
8419 
8420 	/* Enable RSS hashing on vnic */
8421 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
8422 	if (rc) {
8423 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
8424 			   vnic_id, rc);
8425 		goto vnic_setup_err;
8426 	}
8427 
8428 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8429 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
8430 		if (rc) {
8431 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
8432 				   vnic_id, rc);
8433 		}
8434 	}
8435 
8436 vnic_setup_err:
8437 	return rc;
8438 }
8439 
8440 static int __bnxt_setup_vnic_p5(struct bnxt *bp, u16 vnic_id)
8441 {
8442 	int rc, i, nr_ctxs;
8443 
8444 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
8445 	for (i = 0; i < nr_ctxs; i++) {
8446 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, i);
8447 		if (rc) {
8448 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
8449 				   vnic_id, i, rc);
8450 			break;
8451 		}
8452 		bp->rsscos_nr_ctxs++;
8453 	}
8454 	if (i < nr_ctxs)
8455 		return -ENOMEM;
8456 
8457 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic_id, true);
8458 	if (rc) {
8459 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
8460 			   vnic_id, rc);
8461 		return rc;
8462 	}
8463 	rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
8464 	if (rc) {
8465 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
8466 			   vnic_id, rc);
8467 		return rc;
8468 	}
8469 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8470 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
8471 		if (rc) {
8472 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
8473 				   vnic_id, rc);
8474 		}
8475 	}
8476 	return rc;
8477 }
8478 
8479 static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
8480 {
8481 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8482 		return __bnxt_setup_vnic_p5(bp, vnic_id);
8483 	else
8484 		return __bnxt_setup_vnic(bp, vnic_id);
8485 }
8486 
8487 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
8488 {
8489 #ifdef CONFIG_RFS_ACCEL
8490 	int i, rc = 0;
8491 
8492 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8493 		return 0;
8494 
8495 	for (i = 0; i < bp->rx_nr_rings; i++) {
8496 		struct bnxt_vnic_info *vnic;
8497 		u16 vnic_id = i + 1;
8498 		u16 ring_id = i;
8499 
8500 		if (vnic_id >= bp->nr_vnics)
8501 			break;
8502 
8503 		vnic = &bp->vnic_info[vnic_id];
8504 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
8505 		if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
8506 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
8507 		rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
8508 		if (rc) {
8509 			netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
8510 				   vnic_id, rc);
8511 			break;
8512 		}
8513 		rc = bnxt_setup_vnic(bp, vnic_id);
8514 		if (rc)
8515 			break;
8516 	}
8517 	return rc;
8518 #else
8519 	return 0;
8520 #endif
8521 }
8522 
8523 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
8524 static bool bnxt_promisc_ok(struct bnxt *bp)
8525 {
8526 #ifdef CONFIG_BNXT_SRIOV
8527 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
8528 		return false;
8529 #endif
8530 	return true;
8531 }
8532 
8533 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
8534 {
8535 	unsigned int rc = 0;
8536 
8537 	rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
8538 	if (rc) {
8539 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
8540 			   rc);
8541 		return rc;
8542 	}
8543 
8544 	rc = bnxt_hwrm_vnic_cfg(bp, 1);
8545 	if (rc) {
8546 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
8547 			   rc);
8548 		return rc;
8549 	}
8550 	return rc;
8551 }
8552 
8553 static int bnxt_cfg_rx_mode(struct bnxt *);
8554 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
8555 
8556 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
8557 {
8558 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
8559 	int rc = 0;
8560 	unsigned int rx_nr_rings = bp->rx_nr_rings;
8561 
8562 	if (irq_re_init) {
8563 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
8564 		if (rc) {
8565 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
8566 				   rc);
8567 			goto err_out;
8568 		}
8569 	}
8570 
8571 	rc = bnxt_hwrm_ring_alloc(bp);
8572 	if (rc) {
8573 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
8574 		goto err_out;
8575 	}
8576 
8577 	rc = bnxt_hwrm_ring_grp_alloc(bp);
8578 	if (rc) {
8579 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
8580 		goto err_out;
8581 	}
8582 
8583 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8584 		rx_nr_rings--;
8585 
8586 	/* default vnic 0 */
8587 	rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
8588 	if (rc) {
8589 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
8590 		goto err_out;
8591 	}
8592 
8593 	rc = bnxt_setup_vnic(bp, 0);
8594 	if (rc)
8595 		goto err_out;
8596 
8597 	if (bp->flags & BNXT_FLAG_RFS) {
8598 		rc = bnxt_alloc_rfs_vnics(bp);
8599 		if (rc)
8600 			goto err_out;
8601 	}
8602 
8603 	if (bp->flags & BNXT_FLAG_TPA) {
8604 		rc = bnxt_set_tpa(bp, true);
8605 		if (rc)
8606 			goto err_out;
8607 	}
8608 
8609 	if (BNXT_VF(bp))
8610 		bnxt_update_vf_mac(bp);
8611 
8612 	/* Filter for default vnic 0 */
8613 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
8614 	if (rc) {
8615 		netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
8616 		goto err_out;
8617 	}
8618 	vnic->uc_filter_count = 1;
8619 
8620 	vnic->rx_mask = 0;
8621 	if (bp->dev->flags & IFF_BROADCAST)
8622 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
8623 
8624 	if (bp->dev->flags & IFF_PROMISC)
8625 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
8626 
8627 	if (bp->dev->flags & IFF_ALLMULTI) {
8628 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
8629 		vnic->mc_list_count = 0;
8630 	} else {
8631 		u32 mask = 0;
8632 
8633 		bnxt_mc_list_updated(bp, &mask);
8634 		vnic->rx_mask |= mask;
8635 	}
8636 
8637 	rc = bnxt_cfg_rx_mode(bp);
8638 	if (rc)
8639 		goto err_out;
8640 
8641 	rc = bnxt_hwrm_set_coal(bp);
8642 	if (rc)
8643 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
8644 				rc);
8645 
8646 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8647 		rc = bnxt_setup_nitroa0_vnic(bp);
8648 		if (rc)
8649 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
8650 				   rc);
8651 	}
8652 
8653 	if (BNXT_VF(bp)) {
8654 		bnxt_hwrm_func_qcfg(bp);
8655 		netdev_update_features(bp->dev);
8656 	}
8657 
8658 	return 0;
8659 
8660 err_out:
8661 	bnxt_hwrm_resource_free(bp, 0, true);
8662 
8663 	return rc;
8664 }
8665 
8666 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
8667 {
8668 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
8669 	return 0;
8670 }
8671 
8672 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
8673 {
8674 	bnxt_init_cp_rings(bp);
8675 	bnxt_init_rx_rings(bp);
8676 	bnxt_init_tx_rings(bp);
8677 	bnxt_init_ring_grps(bp, irq_re_init);
8678 	bnxt_init_vnics(bp);
8679 
8680 	return bnxt_init_chip(bp, irq_re_init);
8681 }
8682 
8683 static int bnxt_set_real_num_queues(struct bnxt *bp)
8684 {
8685 	int rc;
8686 	struct net_device *dev = bp->dev;
8687 
8688 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
8689 					  bp->tx_nr_rings_xdp);
8690 	if (rc)
8691 		return rc;
8692 
8693 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
8694 	if (rc)
8695 		return rc;
8696 
8697 #ifdef CONFIG_RFS_ACCEL
8698 	if (bp->flags & BNXT_FLAG_RFS)
8699 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
8700 #endif
8701 
8702 	return rc;
8703 }
8704 
8705 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
8706 			   bool shared)
8707 {
8708 	int _rx = *rx, _tx = *tx;
8709 
8710 	if (shared) {
8711 		*rx = min_t(int, _rx, max);
8712 		*tx = min_t(int, _tx, max);
8713 	} else {
8714 		if (max < 2)
8715 			return -ENOMEM;
8716 
8717 		while (_rx + _tx > max) {
8718 			if (_rx > _tx && _rx > 1)
8719 				_rx--;
8720 			else if (_tx > 1)
8721 				_tx--;
8722 		}
8723 		*rx = _rx;
8724 		*tx = _tx;
8725 	}
8726 	return 0;
8727 }
8728 
8729 static void bnxt_setup_msix(struct bnxt *bp)
8730 {
8731 	const int len = sizeof(bp->irq_tbl[0].name);
8732 	struct net_device *dev = bp->dev;
8733 	int tcs, i;
8734 
8735 	tcs = netdev_get_num_tc(dev);
8736 	if (tcs) {
8737 		int i, off, count;
8738 
8739 		for (i = 0; i < tcs; i++) {
8740 			count = bp->tx_nr_rings_per_tc;
8741 			off = i * count;
8742 			netdev_set_tc_queue(dev, i, count, off);
8743 		}
8744 	}
8745 
8746 	for (i = 0; i < bp->cp_nr_rings; i++) {
8747 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
8748 		char *attr;
8749 
8750 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
8751 			attr = "TxRx";
8752 		else if (i < bp->rx_nr_rings)
8753 			attr = "rx";
8754 		else
8755 			attr = "tx";
8756 
8757 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
8758 			 attr, i);
8759 		bp->irq_tbl[map_idx].handler = bnxt_msix;
8760 	}
8761 }
8762 
8763 static void bnxt_setup_inta(struct bnxt *bp)
8764 {
8765 	const int len = sizeof(bp->irq_tbl[0].name);
8766 
8767 	if (netdev_get_num_tc(bp->dev))
8768 		netdev_reset_tc(bp->dev);
8769 
8770 	snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
8771 		 0);
8772 	bp->irq_tbl[0].handler = bnxt_inta;
8773 }
8774 
8775 static int bnxt_init_int_mode(struct bnxt *bp);
8776 
8777 static int bnxt_setup_int_mode(struct bnxt *bp)
8778 {
8779 	int rc;
8780 
8781 	if (!bp->irq_tbl) {
8782 		rc = bnxt_init_int_mode(bp);
8783 		if (rc || !bp->irq_tbl)
8784 			return rc ?: -ENODEV;
8785 	}
8786 
8787 	if (bp->flags & BNXT_FLAG_USING_MSIX)
8788 		bnxt_setup_msix(bp);
8789 	else
8790 		bnxt_setup_inta(bp);
8791 
8792 	rc = bnxt_set_real_num_queues(bp);
8793 	return rc;
8794 }
8795 
8796 #ifdef CONFIG_RFS_ACCEL
8797 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
8798 {
8799 	return bp->hw_resc.max_rsscos_ctxs;
8800 }
8801 
8802 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
8803 {
8804 	return bp->hw_resc.max_vnics;
8805 }
8806 #endif
8807 
8808 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
8809 {
8810 	return bp->hw_resc.max_stat_ctxs;
8811 }
8812 
8813 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
8814 {
8815 	return bp->hw_resc.max_cp_rings;
8816 }
8817 
8818 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
8819 {
8820 	unsigned int cp = bp->hw_resc.max_cp_rings;
8821 
8822 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
8823 		cp -= bnxt_get_ulp_msix_num(bp);
8824 
8825 	return cp;
8826 }
8827 
8828 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
8829 {
8830 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8831 
8832 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8833 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
8834 
8835 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
8836 }
8837 
8838 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
8839 {
8840 	bp->hw_resc.max_irqs = max_irqs;
8841 }
8842 
8843 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
8844 {
8845 	unsigned int cp;
8846 
8847 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
8848 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8849 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
8850 	else
8851 		return cp - bp->cp_nr_rings;
8852 }
8853 
8854 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
8855 {
8856 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
8857 }
8858 
8859 int bnxt_get_avail_msix(struct bnxt *bp, int num)
8860 {
8861 	int max_cp = bnxt_get_max_func_cp_rings(bp);
8862 	int max_irq = bnxt_get_max_func_irqs(bp);
8863 	int total_req = bp->cp_nr_rings + num;
8864 	int max_idx, avail_msix;
8865 
8866 	max_idx = bp->total_irqs;
8867 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
8868 		max_idx = min_t(int, bp->total_irqs, max_cp);
8869 	avail_msix = max_idx - bp->cp_nr_rings;
8870 	if (!BNXT_NEW_RM(bp) || avail_msix >= num)
8871 		return avail_msix;
8872 
8873 	if (max_irq < total_req) {
8874 		num = max_irq - bp->cp_nr_rings;
8875 		if (num <= 0)
8876 			return 0;
8877 	}
8878 	return num;
8879 }
8880 
8881 static int bnxt_get_num_msix(struct bnxt *bp)
8882 {
8883 	if (!BNXT_NEW_RM(bp))
8884 		return bnxt_get_max_func_irqs(bp);
8885 
8886 	return bnxt_nq_rings_in_use(bp);
8887 }
8888 
8889 static int bnxt_init_msix(struct bnxt *bp)
8890 {
8891 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix;
8892 	struct msix_entry *msix_ent;
8893 
8894 	total_vecs = bnxt_get_num_msix(bp);
8895 	max = bnxt_get_max_func_irqs(bp);
8896 	if (total_vecs > max)
8897 		total_vecs = max;
8898 
8899 	if (!total_vecs)
8900 		return 0;
8901 
8902 	msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
8903 	if (!msix_ent)
8904 		return -ENOMEM;
8905 
8906 	for (i = 0; i < total_vecs; i++) {
8907 		msix_ent[i].entry = i;
8908 		msix_ent[i].vector = 0;
8909 	}
8910 
8911 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
8912 		min = 2;
8913 
8914 	total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
8915 	ulp_msix = bnxt_get_ulp_msix_num(bp);
8916 	if (total_vecs < 0 || total_vecs < ulp_msix) {
8917 		rc = -ENODEV;
8918 		goto msix_setup_exit;
8919 	}
8920 
8921 	bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
8922 	if (bp->irq_tbl) {
8923 		for (i = 0; i < total_vecs; i++)
8924 			bp->irq_tbl[i].vector = msix_ent[i].vector;
8925 
8926 		bp->total_irqs = total_vecs;
8927 		/* Trim rings based upon num of vectors allocated */
8928 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
8929 				     total_vecs - ulp_msix, min == 1);
8930 		if (rc)
8931 			goto msix_setup_exit;
8932 
8933 		bp->cp_nr_rings = (min == 1) ?
8934 				  max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
8935 				  bp->tx_nr_rings + bp->rx_nr_rings;
8936 
8937 	} else {
8938 		rc = -ENOMEM;
8939 		goto msix_setup_exit;
8940 	}
8941 	bp->flags |= BNXT_FLAG_USING_MSIX;
8942 	kfree(msix_ent);
8943 	return 0;
8944 
8945 msix_setup_exit:
8946 	netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
8947 	kfree(bp->irq_tbl);
8948 	bp->irq_tbl = NULL;
8949 	pci_disable_msix(bp->pdev);
8950 	kfree(msix_ent);
8951 	return rc;
8952 }
8953 
8954 static int bnxt_init_inta(struct bnxt *bp)
8955 {
8956 	bp->irq_tbl = kzalloc(sizeof(struct bnxt_irq), GFP_KERNEL);
8957 	if (!bp->irq_tbl)
8958 		return -ENOMEM;
8959 
8960 	bp->total_irqs = 1;
8961 	bp->rx_nr_rings = 1;
8962 	bp->tx_nr_rings = 1;
8963 	bp->cp_nr_rings = 1;
8964 	bp->flags |= BNXT_FLAG_SHARED_RINGS;
8965 	bp->irq_tbl[0].vector = bp->pdev->irq;
8966 	return 0;
8967 }
8968 
8969 static int bnxt_init_int_mode(struct bnxt *bp)
8970 {
8971 	int rc = -ENODEV;
8972 
8973 	if (bp->flags & BNXT_FLAG_MSIX_CAP)
8974 		rc = bnxt_init_msix(bp);
8975 
8976 	if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
8977 		/* fallback to INTA */
8978 		rc = bnxt_init_inta(bp);
8979 	}
8980 	return rc;
8981 }
8982 
8983 static void bnxt_clear_int_mode(struct bnxt *bp)
8984 {
8985 	if (bp->flags & BNXT_FLAG_USING_MSIX)
8986 		pci_disable_msix(bp->pdev);
8987 
8988 	kfree(bp->irq_tbl);
8989 	bp->irq_tbl = NULL;
8990 	bp->flags &= ~BNXT_FLAG_USING_MSIX;
8991 }
8992 
8993 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
8994 {
8995 	int tcs = netdev_get_num_tc(bp->dev);
8996 	bool irq_cleared = false;
8997 	int rc;
8998 
8999 	if (!bnxt_need_reserve_rings(bp))
9000 		return 0;
9001 
9002 	if (irq_re_init && BNXT_NEW_RM(bp) &&
9003 	    bnxt_get_num_msix(bp) != bp->total_irqs) {
9004 		bnxt_ulp_irq_stop(bp);
9005 		bnxt_clear_int_mode(bp);
9006 		irq_cleared = true;
9007 	}
9008 	rc = __bnxt_reserve_rings(bp);
9009 	if (irq_cleared) {
9010 		if (!rc)
9011 			rc = bnxt_init_int_mode(bp);
9012 		bnxt_ulp_irq_restart(bp, rc);
9013 	}
9014 	if (rc) {
9015 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
9016 		return rc;
9017 	}
9018 	if (tcs && (bp->tx_nr_rings_per_tc * tcs != bp->tx_nr_rings)) {
9019 		netdev_err(bp->dev, "tx ring reservation failure\n");
9020 		netdev_reset_tc(bp->dev);
9021 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
9022 		return -ENOMEM;
9023 	}
9024 	return 0;
9025 }
9026 
9027 static void bnxt_free_irq(struct bnxt *bp)
9028 {
9029 	struct bnxt_irq *irq;
9030 	int i;
9031 
9032 #ifdef CONFIG_RFS_ACCEL
9033 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
9034 	bp->dev->rx_cpu_rmap = NULL;
9035 #endif
9036 	if (!bp->irq_tbl || !bp->bnapi)
9037 		return;
9038 
9039 	for (i = 0; i < bp->cp_nr_rings; i++) {
9040 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
9041 
9042 		irq = &bp->irq_tbl[map_idx];
9043 		if (irq->requested) {
9044 			if (irq->have_cpumask) {
9045 				irq_set_affinity_hint(irq->vector, NULL);
9046 				free_cpumask_var(irq->cpu_mask);
9047 				irq->have_cpumask = 0;
9048 			}
9049 			free_irq(irq->vector, bp->bnapi[i]);
9050 		}
9051 
9052 		irq->requested = 0;
9053 	}
9054 }
9055 
9056 static int bnxt_request_irq(struct bnxt *bp)
9057 {
9058 	int i, j, rc = 0;
9059 	unsigned long flags = 0;
9060 #ifdef CONFIG_RFS_ACCEL
9061 	struct cpu_rmap *rmap;
9062 #endif
9063 
9064 	rc = bnxt_setup_int_mode(bp);
9065 	if (rc) {
9066 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
9067 			   rc);
9068 		return rc;
9069 	}
9070 #ifdef CONFIG_RFS_ACCEL
9071 	rmap = bp->dev->rx_cpu_rmap;
9072 #endif
9073 	if (!(bp->flags & BNXT_FLAG_USING_MSIX))
9074 		flags = IRQF_SHARED;
9075 
9076 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
9077 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
9078 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
9079 
9080 #ifdef CONFIG_RFS_ACCEL
9081 		if (rmap && bp->bnapi[i]->rx_ring) {
9082 			rc = irq_cpu_rmap_add(rmap, irq->vector);
9083 			if (rc)
9084 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
9085 					    j);
9086 			j++;
9087 		}
9088 #endif
9089 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
9090 				 bp->bnapi[i]);
9091 		if (rc)
9092 			break;
9093 
9094 		irq->requested = 1;
9095 
9096 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
9097 			int numa_node = dev_to_node(&bp->pdev->dev);
9098 
9099 			irq->have_cpumask = 1;
9100 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
9101 					irq->cpu_mask);
9102 			rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
9103 			if (rc) {
9104 				netdev_warn(bp->dev,
9105 					    "Set affinity failed, IRQ = %d\n",
9106 					    irq->vector);
9107 				break;
9108 			}
9109 		}
9110 	}
9111 	return rc;
9112 }
9113 
9114 static void bnxt_del_napi(struct bnxt *bp)
9115 {
9116 	int i;
9117 
9118 	if (!bp->bnapi)
9119 		return;
9120 
9121 	for (i = 0; i < bp->cp_nr_rings; i++) {
9122 		struct bnxt_napi *bnapi = bp->bnapi[i];
9123 
9124 		__netif_napi_del(&bnapi->napi);
9125 	}
9126 	/* We called __netif_napi_del(), we need
9127 	 * to respect an RCU grace period before freeing napi structures.
9128 	 */
9129 	synchronize_net();
9130 }
9131 
9132 static void bnxt_init_napi(struct bnxt *bp)
9133 {
9134 	int i;
9135 	unsigned int cp_nr_rings = bp->cp_nr_rings;
9136 	struct bnxt_napi *bnapi;
9137 
9138 	if (bp->flags & BNXT_FLAG_USING_MSIX) {
9139 		int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
9140 
9141 		if (bp->flags & BNXT_FLAG_CHIP_P5)
9142 			poll_fn = bnxt_poll_p5;
9143 		else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
9144 			cp_nr_rings--;
9145 		for (i = 0; i < cp_nr_rings; i++) {
9146 			bnapi = bp->bnapi[i];
9147 			netif_napi_add(bp->dev, &bnapi->napi, poll_fn, 64);
9148 		}
9149 		if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
9150 			bnapi = bp->bnapi[cp_nr_rings];
9151 			netif_napi_add(bp->dev, &bnapi->napi,
9152 				       bnxt_poll_nitroa0, 64);
9153 		}
9154 	} else {
9155 		bnapi = bp->bnapi[0];
9156 		netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
9157 	}
9158 }
9159 
9160 static void bnxt_disable_napi(struct bnxt *bp)
9161 {
9162 	int i;
9163 
9164 	if (!bp->bnapi ||
9165 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
9166 		return;
9167 
9168 	for (i = 0; i < bp->cp_nr_rings; i++) {
9169 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
9170 
9171 		napi_disable(&bp->bnapi[i]->napi);
9172 		if (bp->bnapi[i]->rx_ring)
9173 			cancel_work_sync(&cpr->dim.work);
9174 	}
9175 }
9176 
9177 static void bnxt_enable_napi(struct bnxt *bp)
9178 {
9179 	int i;
9180 
9181 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
9182 	for (i = 0; i < bp->cp_nr_rings; i++) {
9183 		struct bnxt_napi *bnapi = bp->bnapi[i];
9184 		struct bnxt_cp_ring_info *cpr;
9185 
9186 		cpr = &bnapi->cp_ring;
9187 		if (bnapi->in_reset)
9188 			cpr->sw_stats.rx.rx_resets++;
9189 		bnapi->in_reset = false;
9190 
9191 		if (bnapi->rx_ring) {
9192 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
9193 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
9194 		}
9195 		napi_enable(&bnapi->napi);
9196 	}
9197 }
9198 
9199 void bnxt_tx_disable(struct bnxt *bp)
9200 {
9201 	int i;
9202 	struct bnxt_tx_ring_info *txr;
9203 
9204 	if (bp->tx_ring) {
9205 		for (i = 0; i < bp->tx_nr_rings; i++) {
9206 			txr = &bp->tx_ring[i];
9207 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
9208 		}
9209 	}
9210 	/* Make sure napi polls see @dev_state change */
9211 	synchronize_net();
9212 	/* Drop carrier first to prevent TX timeout */
9213 	netif_carrier_off(bp->dev);
9214 	/* Stop all TX queues */
9215 	netif_tx_disable(bp->dev);
9216 }
9217 
9218 void bnxt_tx_enable(struct bnxt *bp)
9219 {
9220 	int i;
9221 	struct bnxt_tx_ring_info *txr;
9222 
9223 	for (i = 0; i < bp->tx_nr_rings; i++) {
9224 		txr = &bp->tx_ring[i];
9225 		WRITE_ONCE(txr->dev_state, 0);
9226 	}
9227 	/* Make sure napi polls see @dev_state change */
9228 	synchronize_net();
9229 	netif_tx_wake_all_queues(bp->dev);
9230 	if (bp->link_info.link_up)
9231 		netif_carrier_on(bp->dev);
9232 }
9233 
9234 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
9235 {
9236 	u8 active_fec = link_info->active_fec_sig_mode &
9237 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
9238 
9239 	switch (active_fec) {
9240 	default:
9241 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
9242 		return "None";
9243 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
9244 		return "Clause 74 BaseR";
9245 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
9246 		return "Clause 91 RS(528,514)";
9247 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
9248 		return "Clause 91 RS544_1XN";
9249 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
9250 		return "Clause 91 RS(544,514)";
9251 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
9252 		return "Clause 91 RS272_1XN";
9253 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
9254 		return "Clause 91 RS(272,257)";
9255 	}
9256 }
9257 
9258 void bnxt_report_link(struct bnxt *bp)
9259 {
9260 	if (bp->link_info.link_up) {
9261 		const char *signal = "";
9262 		const char *flow_ctrl;
9263 		const char *duplex;
9264 		u32 speed;
9265 		u16 fec;
9266 
9267 		netif_carrier_on(bp->dev);
9268 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
9269 		if (speed == SPEED_UNKNOWN) {
9270 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
9271 			return;
9272 		}
9273 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
9274 			duplex = "full";
9275 		else
9276 			duplex = "half";
9277 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
9278 			flow_ctrl = "ON - receive & transmit";
9279 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
9280 			flow_ctrl = "ON - transmit";
9281 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
9282 			flow_ctrl = "ON - receive";
9283 		else
9284 			flow_ctrl = "none";
9285 		if (bp->link_info.phy_qcfg_resp.option_flags &
9286 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
9287 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
9288 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
9289 			switch (sig_mode) {
9290 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
9291 				signal = "(NRZ) ";
9292 				break;
9293 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
9294 				signal = "(PAM4) ";
9295 				break;
9296 			default:
9297 				break;
9298 			}
9299 		}
9300 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
9301 			    speed, signal, duplex, flow_ctrl);
9302 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
9303 			netdev_info(bp->dev, "EEE is %s\n",
9304 				    bp->eee.eee_active ? "active" :
9305 							 "not active");
9306 		fec = bp->link_info.fec_cfg;
9307 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
9308 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
9309 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
9310 				    bnxt_report_fec(&bp->link_info));
9311 	} else {
9312 		netif_carrier_off(bp->dev);
9313 		netdev_err(bp->dev, "NIC Link is Down\n");
9314 	}
9315 }
9316 
9317 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
9318 {
9319 	if (!resp->supported_speeds_auto_mode &&
9320 	    !resp->supported_speeds_force_mode &&
9321 	    !resp->supported_pam4_speeds_auto_mode &&
9322 	    !resp->supported_pam4_speeds_force_mode)
9323 		return true;
9324 	return false;
9325 }
9326 
9327 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
9328 {
9329 	struct bnxt_link_info *link_info = &bp->link_info;
9330 	struct hwrm_port_phy_qcaps_output *resp;
9331 	struct hwrm_port_phy_qcaps_input *req;
9332 	int rc = 0;
9333 
9334 	if (bp->hwrm_spec_code < 0x10201)
9335 		return 0;
9336 
9337 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
9338 	if (rc)
9339 		return rc;
9340 
9341 	resp = hwrm_req_hold(bp, req);
9342 	rc = hwrm_req_send(bp, req);
9343 	if (rc)
9344 		goto hwrm_phy_qcaps_exit;
9345 
9346 	bp->phy_flags = resp->flags;
9347 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
9348 		struct ethtool_eee *eee = &bp->eee;
9349 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
9350 
9351 		eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9352 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
9353 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
9354 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
9355 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
9356 	}
9357 
9358 	if (bp->hwrm_spec_code >= 0x10a01) {
9359 		if (bnxt_phy_qcaps_no_speed(resp)) {
9360 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
9361 			netdev_warn(bp->dev, "Ethernet link disabled\n");
9362 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
9363 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
9364 			netdev_info(bp->dev, "Ethernet link enabled\n");
9365 			/* Phy re-enabled, reprobe the speeds */
9366 			link_info->support_auto_speeds = 0;
9367 			link_info->support_pam4_auto_speeds = 0;
9368 		}
9369 	}
9370 	if (resp->supported_speeds_auto_mode)
9371 		link_info->support_auto_speeds =
9372 			le16_to_cpu(resp->supported_speeds_auto_mode);
9373 	if (resp->supported_pam4_speeds_auto_mode)
9374 		link_info->support_pam4_auto_speeds =
9375 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
9376 
9377 	bp->port_count = resp->port_cnt;
9378 
9379 hwrm_phy_qcaps_exit:
9380 	hwrm_req_drop(bp, req);
9381 	return rc;
9382 }
9383 
9384 static bool bnxt_support_dropped(u16 advertising, u16 supported)
9385 {
9386 	u16 diff = advertising ^ supported;
9387 
9388 	return ((supported | diff) != supported);
9389 }
9390 
9391 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
9392 {
9393 	struct bnxt_link_info *link_info = &bp->link_info;
9394 	struct hwrm_port_phy_qcfg_output *resp;
9395 	struct hwrm_port_phy_qcfg_input *req;
9396 	u8 link_up = link_info->link_up;
9397 	bool support_changed = false;
9398 	int rc;
9399 
9400 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
9401 	if (rc)
9402 		return rc;
9403 
9404 	resp = hwrm_req_hold(bp, req);
9405 	rc = hwrm_req_send(bp, req);
9406 	if (rc) {
9407 		hwrm_req_drop(bp, req);
9408 		return rc;
9409 	}
9410 
9411 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
9412 	link_info->phy_link_status = resp->link;
9413 	link_info->duplex = resp->duplex_cfg;
9414 	if (bp->hwrm_spec_code >= 0x10800)
9415 		link_info->duplex = resp->duplex_state;
9416 	link_info->pause = resp->pause;
9417 	link_info->auto_mode = resp->auto_mode;
9418 	link_info->auto_pause_setting = resp->auto_pause;
9419 	link_info->lp_pause = resp->link_partner_adv_pause;
9420 	link_info->force_pause_setting = resp->force_pause;
9421 	link_info->duplex_setting = resp->duplex_cfg;
9422 	if (link_info->phy_link_status == BNXT_LINK_LINK)
9423 		link_info->link_speed = le16_to_cpu(resp->link_speed);
9424 	else
9425 		link_info->link_speed = 0;
9426 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
9427 	link_info->force_pam4_link_speed =
9428 		le16_to_cpu(resp->force_pam4_link_speed);
9429 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
9430 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
9431 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
9432 	link_info->auto_pam4_link_speeds =
9433 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
9434 	link_info->lp_auto_link_speeds =
9435 		le16_to_cpu(resp->link_partner_adv_speeds);
9436 	link_info->lp_auto_pam4_link_speeds =
9437 		resp->link_partner_pam4_adv_speeds;
9438 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
9439 	link_info->phy_ver[0] = resp->phy_maj;
9440 	link_info->phy_ver[1] = resp->phy_min;
9441 	link_info->phy_ver[2] = resp->phy_bld;
9442 	link_info->media_type = resp->media_type;
9443 	link_info->phy_type = resp->phy_type;
9444 	link_info->transceiver = resp->xcvr_pkg_type;
9445 	link_info->phy_addr = resp->eee_config_phy_addr &
9446 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
9447 	link_info->module_status = resp->module_status;
9448 
9449 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
9450 		struct ethtool_eee *eee = &bp->eee;
9451 		u16 fw_speeds;
9452 
9453 		eee->eee_active = 0;
9454 		if (resp->eee_config_phy_addr &
9455 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
9456 			eee->eee_active = 1;
9457 			fw_speeds = le16_to_cpu(
9458 				resp->link_partner_adv_eee_link_speed_mask);
9459 			eee->lp_advertised =
9460 				_bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9461 		}
9462 
9463 		/* Pull initial EEE config */
9464 		if (!chng_link_state) {
9465 			if (resp->eee_config_phy_addr &
9466 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
9467 				eee->eee_enabled = 1;
9468 
9469 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
9470 			eee->advertised =
9471 				_bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9472 
9473 			if (resp->eee_config_phy_addr &
9474 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
9475 				__le32 tmr;
9476 
9477 				eee->tx_lpi_enabled = 1;
9478 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
9479 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
9480 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
9481 			}
9482 		}
9483 	}
9484 
9485 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
9486 	if (bp->hwrm_spec_code >= 0x10504) {
9487 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
9488 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
9489 	}
9490 	/* TODO: need to add more logic to report VF link */
9491 	if (chng_link_state) {
9492 		if (link_info->phy_link_status == BNXT_LINK_LINK)
9493 			link_info->link_up = 1;
9494 		else
9495 			link_info->link_up = 0;
9496 		if (link_up != link_info->link_up)
9497 			bnxt_report_link(bp);
9498 	} else {
9499 		/* alwasy link down if not require to update link state */
9500 		link_info->link_up = 0;
9501 	}
9502 	hwrm_req_drop(bp, req);
9503 
9504 	if (!BNXT_PHY_CFG_ABLE(bp))
9505 		return 0;
9506 
9507 	/* Check if any advertised speeds are no longer supported. The caller
9508 	 * holds the link_lock mutex, so we can modify link_info settings.
9509 	 */
9510 	if (bnxt_support_dropped(link_info->advertising,
9511 				 link_info->support_auto_speeds)) {
9512 		link_info->advertising = link_info->support_auto_speeds;
9513 		support_changed = true;
9514 	}
9515 	if (bnxt_support_dropped(link_info->advertising_pam4,
9516 				 link_info->support_pam4_auto_speeds)) {
9517 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
9518 		support_changed = true;
9519 	}
9520 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
9521 		bnxt_hwrm_set_link_setting(bp, true, false);
9522 	return 0;
9523 }
9524 
9525 static void bnxt_get_port_module_status(struct bnxt *bp)
9526 {
9527 	struct bnxt_link_info *link_info = &bp->link_info;
9528 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
9529 	u8 module_status;
9530 
9531 	if (bnxt_update_link(bp, true))
9532 		return;
9533 
9534 	module_status = link_info->module_status;
9535 	switch (module_status) {
9536 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
9537 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
9538 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
9539 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
9540 			    bp->pf.port_id);
9541 		if (bp->hwrm_spec_code >= 0x10201) {
9542 			netdev_warn(bp->dev, "Module part number %s\n",
9543 				    resp->phy_vendor_partnumber);
9544 		}
9545 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
9546 			netdev_warn(bp->dev, "TX is disabled\n");
9547 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
9548 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
9549 	}
9550 }
9551 
9552 static void
9553 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
9554 {
9555 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
9556 		if (bp->hwrm_spec_code >= 0x10201)
9557 			req->auto_pause =
9558 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
9559 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
9560 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
9561 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
9562 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
9563 		req->enables |=
9564 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
9565 	} else {
9566 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
9567 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
9568 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
9569 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
9570 		req->enables |=
9571 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
9572 		if (bp->hwrm_spec_code >= 0x10201) {
9573 			req->auto_pause = req->force_pause;
9574 			req->enables |= cpu_to_le32(
9575 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
9576 		}
9577 	}
9578 }
9579 
9580 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
9581 {
9582 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
9583 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
9584 		if (bp->link_info.advertising) {
9585 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
9586 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
9587 		}
9588 		if (bp->link_info.advertising_pam4) {
9589 			req->enables |=
9590 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
9591 			req->auto_link_pam4_speed_mask =
9592 				cpu_to_le16(bp->link_info.advertising_pam4);
9593 		}
9594 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
9595 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
9596 	} else {
9597 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
9598 		if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
9599 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
9600 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
9601 		} else {
9602 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
9603 		}
9604 	}
9605 
9606 	/* tell chimp that the setting takes effect immediately */
9607 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
9608 }
9609 
9610 int bnxt_hwrm_set_pause(struct bnxt *bp)
9611 {
9612 	struct hwrm_port_phy_cfg_input *req;
9613 	int rc;
9614 
9615 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
9616 	if (rc)
9617 		return rc;
9618 
9619 	bnxt_hwrm_set_pause_common(bp, req);
9620 
9621 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
9622 	    bp->link_info.force_link_chng)
9623 		bnxt_hwrm_set_link_common(bp, req);
9624 
9625 	rc = hwrm_req_send(bp, req);
9626 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
9627 		/* since changing of pause setting doesn't trigger any link
9628 		 * change event, the driver needs to update the current pause
9629 		 * result upon successfully return of the phy_cfg command
9630 		 */
9631 		bp->link_info.pause =
9632 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
9633 		bp->link_info.auto_pause_setting = 0;
9634 		if (!bp->link_info.force_link_chng)
9635 			bnxt_report_link(bp);
9636 	}
9637 	bp->link_info.force_link_chng = false;
9638 	return rc;
9639 }
9640 
9641 static void bnxt_hwrm_set_eee(struct bnxt *bp,
9642 			      struct hwrm_port_phy_cfg_input *req)
9643 {
9644 	struct ethtool_eee *eee = &bp->eee;
9645 
9646 	if (eee->eee_enabled) {
9647 		u16 eee_speeds;
9648 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
9649 
9650 		if (eee->tx_lpi_enabled)
9651 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
9652 		else
9653 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
9654 
9655 		req->flags |= cpu_to_le32(flags);
9656 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
9657 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
9658 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
9659 	} else {
9660 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
9661 	}
9662 }
9663 
9664 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
9665 {
9666 	struct hwrm_port_phy_cfg_input *req;
9667 	int rc;
9668 
9669 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
9670 	if (rc)
9671 		return rc;
9672 
9673 	if (set_pause)
9674 		bnxt_hwrm_set_pause_common(bp, req);
9675 
9676 	bnxt_hwrm_set_link_common(bp, req);
9677 
9678 	if (set_eee)
9679 		bnxt_hwrm_set_eee(bp, req);
9680 	return hwrm_req_send(bp, req);
9681 }
9682 
9683 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
9684 {
9685 	struct hwrm_port_phy_cfg_input *req;
9686 	int rc;
9687 
9688 	if (!BNXT_SINGLE_PF(bp))
9689 		return 0;
9690 
9691 	if (pci_num_vf(bp->pdev) &&
9692 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
9693 		return 0;
9694 
9695 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
9696 	if (rc)
9697 		return rc;
9698 
9699 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
9700 	return hwrm_req_send(bp, req);
9701 }
9702 
9703 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
9704 {
9705 #ifdef CONFIG_TEE_BNXT_FW
9706 	int rc = tee_bnxt_fw_load();
9707 
9708 	if (rc)
9709 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
9710 
9711 	return rc;
9712 #else
9713 	netdev_err(bp->dev, "OP-TEE not supported\n");
9714 	return -ENODEV;
9715 #endif
9716 }
9717 
9718 static int bnxt_try_recover_fw(struct bnxt *bp)
9719 {
9720 	if (bp->fw_health && bp->fw_health->status_reliable) {
9721 		int retry = 0, rc;
9722 		u32 sts;
9723 
9724 		do {
9725 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
9726 			rc = bnxt_hwrm_poll(bp);
9727 			if (!BNXT_FW_IS_BOOTING(sts) &&
9728 			    !BNXT_FW_IS_RECOVERING(sts))
9729 				break;
9730 			retry++;
9731 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
9732 
9733 		if (!BNXT_FW_IS_HEALTHY(sts)) {
9734 			netdev_err(bp->dev,
9735 				   "Firmware not responding, status: 0x%x\n",
9736 				   sts);
9737 			rc = -ENODEV;
9738 		}
9739 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
9740 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
9741 			return bnxt_fw_reset_via_optee(bp);
9742 		}
9743 		return rc;
9744 	}
9745 
9746 	return -ENODEV;
9747 }
9748 
9749 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
9750 {
9751 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9752 	int rc;
9753 
9754 	if (!BNXT_NEW_RM(bp))
9755 		return 0; /* no resource reservations required */
9756 
9757 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9758 	if (rc)
9759 		netdev_err(bp->dev, "resc_qcaps failed\n");
9760 
9761 	hw_resc->resv_cp_rings = 0;
9762 	hw_resc->resv_stat_ctxs = 0;
9763 	hw_resc->resv_irqs = 0;
9764 	hw_resc->resv_tx_rings = 0;
9765 	hw_resc->resv_rx_rings = 0;
9766 	hw_resc->resv_hw_ring_grps = 0;
9767 	hw_resc->resv_vnics = 0;
9768 	if (!fw_reset) {
9769 		bp->tx_nr_rings = 0;
9770 		bp->rx_nr_rings = 0;
9771 	}
9772 
9773 	return rc;
9774 }
9775 
9776 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
9777 {
9778 	struct hwrm_func_drv_if_change_output *resp;
9779 	struct hwrm_func_drv_if_change_input *req;
9780 	bool fw_reset = !bp->irq_tbl;
9781 	bool resc_reinit = false;
9782 	int rc, retry = 0;
9783 	u32 flags = 0;
9784 
9785 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
9786 		return 0;
9787 
9788 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
9789 	if (rc)
9790 		return rc;
9791 
9792 	if (up)
9793 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
9794 	resp = hwrm_req_hold(bp, req);
9795 
9796 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
9797 	while (retry < BNXT_FW_IF_RETRY) {
9798 		rc = hwrm_req_send(bp, req);
9799 		if (rc != -EAGAIN)
9800 			break;
9801 
9802 		msleep(50);
9803 		retry++;
9804 	}
9805 
9806 	if (rc == -EAGAIN) {
9807 		hwrm_req_drop(bp, req);
9808 		return rc;
9809 	} else if (!rc) {
9810 		flags = le32_to_cpu(resp->flags);
9811 	} else if (up) {
9812 		rc = bnxt_try_recover_fw(bp);
9813 		fw_reset = true;
9814 	}
9815 	hwrm_req_drop(bp, req);
9816 	if (rc)
9817 		return rc;
9818 
9819 	if (!up) {
9820 		bnxt_inv_fw_health_reg(bp);
9821 		return 0;
9822 	}
9823 
9824 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
9825 		resc_reinit = true;
9826 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE)
9827 		fw_reset = true;
9828 	else if (bp->fw_health && !bp->fw_health->status_reliable)
9829 		bnxt_try_map_fw_health_reg(bp);
9830 
9831 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
9832 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
9833 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
9834 		return -ENODEV;
9835 	}
9836 	if (resc_reinit || fw_reset) {
9837 		if (fw_reset) {
9838 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
9839 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
9840 				bnxt_ulp_stop(bp);
9841 			bnxt_free_ctx_mem(bp);
9842 			kfree(bp->ctx);
9843 			bp->ctx = NULL;
9844 			bnxt_dcb_free(bp);
9845 			rc = bnxt_fw_init_one(bp);
9846 			if (rc) {
9847 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
9848 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
9849 				return rc;
9850 			}
9851 			bnxt_clear_int_mode(bp);
9852 			rc = bnxt_init_int_mode(bp);
9853 			if (rc) {
9854 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
9855 				netdev_err(bp->dev, "init int mode failed\n");
9856 				return rc;
9857 			}
9858 		}
9859 		rc = bnxt_cancel_reservations(bp, fw_reset);
9860 	}
9861 	return rc;
9862 }
9863 
9864 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
9865 {
9866 	struct hwrm_port_led_qcaps_output *resp;
9867 	struct hwrm_port_led_qcaps_input *req;
9868 	struct bnxt_pf_info *pf = &bp->pf;
9869 	int rc;
9870 
9871 	bp->num_leds = 0;
9872 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
9873 		return 0;
9874 
9875 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
9876 	if (rc)
9877 		return rc;
9878 
9879 	req->port_id = cpu_to_le16(pf->port_id);
9880 	resp = hwrm_req_hold(bp, req);
9881 	rc = hwrm_req_send(bp, req);
9882 	if (rc) {
9883 		hwrm_req_drop(bp, req);
9884 		return rc;
9885 	}
9886 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
9887 		int i;
9888 
9889 		bp->num_leds = resp->num_leds;
9890 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
9891 						 bp->num_leds);
9892 		for (i = 0; i < bp->num_leds; i++) {
9893 			struct bnxt_led_info *led = &bp->leds[i];
9894 			__le16 caps = led->led_state_caps;
9895 
9896 			if (!led->led_group_id ||
9897 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
9898 				bp->num_leds = 0;
9899 				break;
9900 			}
9901 		}
9902 	}
9903 	hwrm_req_drop(bp, req);
9904 	return 0;
9905 }
9906 
9907 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
9908 {
9909 	struct hwrm_wol_filter_alloc_output *resp;
9910 	struct hwrm_wol_filter_alloc_input *req;
9911 	int rc;
9912 
9913 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
9914 	if (rc)
9915 		return rc;
9916 
9917 	req->port_id = cpu_to_le16(bp->pf.port_id);
9918 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
9919 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
9920 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
9921 
9922 	resp = hwrm_req_hold(bp, req);
9923 	rc = hwrm_req_send(bp, req);
9924 	if (!rc)
9925 		bp->wol_filter_id = resp->wol_filter_id;
9926 	hwrm_req_drop(bp, req);
9927 	return rc;
9928 }
9929 
9930 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
9931 {
9932 	struct hwrm_wol_filter_free_input *req;
9933 	int rc;
9934 
9935 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
9936 	if (rc)
9937 		return rc;
9938 
9939 	req->port_id = cpu_to_le16(bp->pf.port_id);
9940 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
9941 	req->wol_filter_id = bp->wol_filter_id;
9942 
9943 	return hwrm_req_send(bp, req);
9944 }
9945 
9946 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
9947 {
9948 	struct hwrm_wol_filter_qcfg_output *resp;
9949 	struct hwrm_wol_filter_qcfg_input *req;
9950 	u16 next_handle = 0;
9951 	int rc;
9952 
9953 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
9954 	if (rc)
9955 		return rc;
9956 
9957 	req->port_id = cpu_to_le16(bp->pf.port_id);
9958 	req->handle = cpu_to_le16(handle);
9959 	resp = hwrm_req_hold(bp, req);
9960 	rc = hwrm_req_send(bp, req);
9961 	if (!rc) {
9962 		next_handle = le16_to_cpu(resp->next_handle);
9963 		if (next_handle != 0) {
9964 			if (resp->wol_type ==
9965 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
9966 				bp->wol = 1;
9967 				bp->wol_filter_id = resp->wol_filter_id;
9968 			}
9969 		}
9970 	}
9971 	hwrm_req_drop(bp, req);
9972 	return next_handle;
9973 }
9974 
9975 static void bnxt_get_wol_settings(struct bnxt *bp)
9976 {
9977 	u16 handle = 0;
9978 
9979 	bp->wol = 0;
9980 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
9981 		return;
9982 
9983 	do {
9984 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
9985 	} while (handle && handle != 0xffff);
9986 }
9987 
9988 #ifdef CONFIG_BNXT_HWMON
9989 static ssize_t bnxt_show_temp(struct device *dev,
9990 			      struct device_attribute *devattr, char *buf)
9991 {
9992 	struct hwrm_temp_monitor_query_output *resp;
9993 	struct hwrm_temp_monitor_query_input *req;
9994 	struct bnxt *bp = dev_get_drvdata(dev);
9995 	u32 len = 0;
9996 	int rc;
9997 
9998 	rc = hwrm_req_init(bp, req, HWRM_TEMP_MONITOR_QUERY);
9999 	if (rc)
10000 		return rc;
10001 	resp = hwrm_req_hold(bp, req);
10002 	rc = hwrm_req_send(bp, req);
10003 	if (!rc)
10004 		len = sprintf(buf, "%u\n", resp->temp * 1000); /* display millidegree */
10005 	hwrm_req_drop(bp, req);
10006 	if (rc)
10007 		return rc;
10008 	return len;
10009 }
10010 static SENSOR_DEVICE_ATTR(temp1_input, 0444, bnxt_show_temp, NULL, 0);
10011 
10012 static struct attribute *bnxt_attrs[] = {
10013 	&sensor_dev_attr_temp1_input.dev_attr.attr,
10014 	NULL
10015 };
10016 ATTRIBUTE_GROUPS(bnxt);
10017 
10018 static void bnxt_hwmon_close(struct bnxt *bp)
10019 {
10020 	if (bp->hwmon_dev) {
10021 		hwmon_device_unregister(bp->hwmon_dev);
10022 		bp->hwmon_dev = NULL;
10023 	}
10024 }
10025 
10026 static void bnxt_hwmon_open(struct bnxt *bp)
10027 {
10028 	struct hwrm_temp_monitor_query_input *req;
10029 	struct pci_dev *pdev = bp->pdev;
10030 	int rc;
10031 
10032 	rc = hwrm_req_init(bp, req, HWRM_TEMP_MONITOR_QUERY);
10033 	if (!rc)
10034 		rc = hwrm_req_send_silent(bp, req);
10035 	if (rc == -EACCES || rc == -EOPNOTSUPP) {
10036 		bnxt_hwmon_close(bp);
10037 		return;
10038 	}
10039 
10040 	if (bp->hwmon_dev)
10041 		return;
10042 
10043 	bp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev,
10044 							  DRV_MODULE_NAME, bp,
10045 							  bnxt_groups);
10046 	if (IS_ERR(bp->hwmon_dev)) {
10047 		bp->hwmon_dev = NULL;
10048 		dev_warn(&pdev->dev, "Cannot register hwmon device\n");
10049 	}
10050 }
10051 #else
10052 static void bnxt_hwmon_close(struct bnxt *bp)
10053 {
10054 }
10055 
10056 static void bnxt_hwmon_open(struct bnxt *bp)
10057 {
10058 }
10059 #endif
10060 
10061 static bool bnxt_eee_config_ok(struct bnxt *bp)
10062 {
10063 	struct ethtool_eee *eee = &bp->eee;
10064 	struct bnxt_link_info *link_info = &bp->link_info;
10065 
10066 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
10067 		return true;
10068 
10069 	if (eee->eee_enabled) {
10070 		u32 advertising =
10071 			_bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
10072 
10073 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
10074 			eee->eee_enabled = 0;
10075 			return false;
10076 		}
10077 		if (eee->advertised & ~advertising) {
10078 			eee->advertised = advertising & eee->supported;
10079 			return false;
10080 		}
10081 	}
10082 	return true;
10083 }
10084 
10085 static int bnxt_update_phy_setting(struct bnxt *bp)
10086 {
10087 	int rc;
10088 	bool update_link = false;
10089 	bool update_pause = false;
10090 	bool update_eee = false;
10091 	struct bnxt_link_info *link_info = &bp->link_info;
10092 
10093 	rc = bnxt_update_link(bp, true);
10094 	if (rc) {
10095 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
10096 			   rc);
10097 		return rc;
10098 	}
10099 	if (!BNXT_SINGLE_PF(bp))
10100 		return 0;
10101 
10102 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
10103 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
10104 	    link_info->req_flow_ctrl)
10105 		update_pause = true;
10106 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
10107 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
10108 		update_pause = true;
10109 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
10110 		if (BNXT_AUTO_MODE(link_info->auto_mode))
10111 			update_link = true;
10112 		if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
10113 		    link_info->req_link_speed != link_info->force_link_speed)
10114 			update_link = true;
10115 		else if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
10116 			 link_info->req_link_speed != link_info->force_pam4_link_speed)
10117 			update_link = true;
10118 		if (link_info->req_duplex != link_info->duplex_setting)
10119 			update_link = true;
10120 	} else {
10121 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
10122 			update_link = true;
10123 		if (link_info->advertising != link_info->auto_link_speeds ||
10124 		    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
10125 			update_link = true;
10126 	}
10127 
10128 	/* The last close may have shutdown the link, so need to call
10129 	 * PHY_CFG to bring it back up.
10130 	 */
10131 	if (!bp->link_info.link_up)
10132 		update_link = true;
10133 
10134 	if (!bnxt_eee_config_ok(bp))
10135 		update_eee = true;
10136 
10137 	if (update_link)
10138 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
10139 	else if (update_pause)
10140 		rc = bnxt_hwrm_set_pause(bp);
10141 	if (rc) {
10142 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
10143 			   rc);
10144 		return rc;
10145 	}
10146 
10147 	return rc;
10148 }
10149 
10150 /* Common routine to pre-map certain register block to different GRC window.
10151  * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
10152  * in PF and 3 windows in VF that can be customized to map in different
10153  * register blocks.
10154  */
10155 static void bnxt_preset_reg_win(struct bnxt *bp)
10156 {
10157 	if (BNXT_PF(bp)) {
10158 		/* CAG registers map to GRC window #4 */
10159 		writel(BNXT_CAG_REG_BASE,
10160 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
10161 	}
10162 }
10163 
10164 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
10165 
10166 static int bnxt_reinit_after_abort(struct bnxt *bp)
10167 {
10168 	int rc;
10169 
10170 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
10171 		return -EBUSY;
10172 
10173 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
10174 		return -ENODEV;
10175 
10176 	rc = bnxt_fw_init_one(bp);
10177 	if (!rc) {
10178 		bnxt_clear_int_mode(bp);
10179 		rc = bnxt_init_int_mode(bp);
10180 		if (!rc) {
10181 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
10182 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
10183 		}
10184 	}
10185 	return rc;
10186 }
10187 
10188 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
10189 {
10190 	int rc = 0;
10191 
10192 	bnxt_preset_reg_win(bp);
10193 	netif_carrier_off(bp->dev);
10194 	if (irq_re_init) {
10195 		/* Reserve rings now if none were reserved at driver probe. */
10196 		rc = bnxt_init_dflt_ring_mode(bp);
10197 		if (rc) {
10198 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
10199 			return rc;
10200 		}
10201 	}
10202 	rc = bnxt_reserve_rings(bp, irq_re_init);
10203 	if (rc)
10204 		return rc;
10205 	if ((bp->flags & BNXT_FLAG_RFS) &&
10206 	    !(bp->flags & BNXT_FLAG_USING_MSIX)) {
10207 		/* disable RFS if falling back to INTA */
10208 		bp->dev->hw_features &= ~NETIF_F_NTUPLE;
10209 		bp->flags &= ~BNXT_FLAG_RFS;
10210 	}
10211 
10212 	rc = bnxt_alloc_mem(bp, irq_re_init);
10213 	if (rc) {
10214 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
10215 		goto open_err_free_mem;
10216 	}
10217 
10218 	if (irq_re_init) {
10219 		bnxt_init_napi(bp);
10220 		rc = bnxt_request_irq(bp);
10221 		if (rc) {
10222 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
10223 			goto open_err_irq;
10224 		}
10225 	}
10226 
10227 	rc = bnxt_init_nic(bp, irq_re_init);
10228 	if (rc) {
10229 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
10230 		goto open_err_irq;
10231 	}
10232 
10233 	bnxt_enable_napi(bp);
10234 	bnxt_debug_dev_init(bp);
10235 
10236 	if (link_re_init) {
10237 		mutex_lock(&bp->link_lock);
10238 		rc = bnxt_update_phy_setting(bp);
10239 		mutex_unlock(&bp->link_lock);
10240 		if (rc) {
10241 			netdev_warn(bp->dev, "failed to update phy settings\n");
10242 			if (BNXT_SINGLE_PF(bp)) {
10243 				bp->link_info.phy_retry = true;
10244 				bp->link_info.phy_retry_expires =
10245 					jiffies + 5 * HZ;
10246 			}
10247 		}
10248 	}
10249 
10250 	if (irq_re_init)
10251 		udp_tunnel_nic_reset_ntf(bp->dev);
10252 
10253 	set_bit(BNXT_STATE_OPEN, &bp->state);
10254 	bnxt_enable_int(bp);
10255 	/* Enable TX queues */
10256 	bnxt_tx_enable(bp);
10257 	mod_timer(&bp->timer, jiffies + bp->current_interval);
10258 	/* Poll link status and check for SFP+ module status */
10259 	mutex_lock(&bp->link_lock);
10260 	bnxt_get_port_module_status(bp);
10261 	mutex_unlock(&bp->link_lock);
10262 
10263 	/* VF-reps may need to be re-opened after the PF is re-opened */
10264 	if (BNXT_PF(bp))
10265 		bnxt_vf_reps_open(bp);
10266 	return 0;
10267 
10268 open_err_irq:
10269 	bnxt_del_napi(bp);
10270 
10271 open_err_free_mem:
10272 	bnxt_free_skbs(bp);
10273 	bnxt_free_irq(bp);
10274 	bnxt_free_mem(bp, true);
10275 	return rc;
10276 }
10277 
10278 /* rtnl_lock held */
10279 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
10280 {
10281 	int rc = 0;
10282 
10283 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
10284 		rc = -EIO;
10285 	if (!rc)
10286 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
10287 	if (rc) {
10288 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
10289 		dev_close(bp->dev);
10290 	}
10291 	return rc;
10292 }
10293 
10294 /* rtnl_lock held, open the NIC half way by allocating all resources, but
10295  * NAPI, IRQ, and TX are not enabled.  This is mainly used for offline
10296  * self tests.
10297  */
10298 int bnxt_half_open_nic(struct bnxt *bp)
10299 {
10300 	int rc = 0;
10301 
10302 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
10303 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
10304 		rc = -ENODEV;
10305 		goto half_open_err;
10306 	}
10307 
10308 	rc = bnxt_alloc_mem(bp, false);
10309 	if (rc) {
10310 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
10311 		goto half_open_err;
10312 	}
10313 	rc = bnxt_init_nic(bp, false);
10314 	if (rc) {
10315 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
10316 		goto half_open_err;
10317 	}
10318 	return 0;
10319 
10320 half_open_err:
10321 	bnxt_free_skbs(bp);
10322 	bnxt_free_mem(bp, false);
10323 	dev_close(bp->dev);
10324 	return rc;
10325 }
10326 
10327 /* rtnl_lock held, this call can only be made after a previous successful
10328  * call to bnxt_half_open_nic().
10329  */
10330 void bnxt_half_close_nic(struct bnxt *bp)
10331 {
10332 	bnxt_hwrm_resource_free(bp, false, false);
10333 	bnxt_free_skbs(bp);
10334 	bnxt_free_mem(bp, false);
10335 }
10336 
10337 void bnxt_reenable_sriov(struct bnxt *bp)
10338 {
10339 	if (BNXT_PF(bp)) {
10340 		struct bnxt_pf_info *pf = &bp->pf;
10341 		int n = pf->active_vfs;
10342 
10343 		if (n)
10344 			bnxt_cfg_hw_sriov(bp, &n, true);
10345 	}
10346 }
10347 
10348 static int bnxt_open(struct net_device *dev)
10349 {
10350 	struct bnxt *bp = netdev_priv(dev);
10351 	int rc;
10352 
10353 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
10354 		rc = bnxt_reinit_after_abort(bp);
10355 		if (rc) {
10356 			if (rc == -EBUSY)
10357 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
10358 			else
10359 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
10360 			return -ENODEV;
10361 		}
10362 	}
10363 
10364 	rc = bnxt_hwrm_if_change(bp, true);
10365 	if (rc)
10366 		return rc;
10367 
10368 	rc = __bnxt_open_nic(bp, true, true);
10369 	if (rc) {
10370 		bnxt_hwrm_if_change(bp, false);
10371 	} else {
10372 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
10373 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
10374 				bnxt_ulp_start(bp, 0);
10375 				bnxt_reenable_sriov(bp);
10376 			}
10377 		}
10378 		bnxt_hwmon_open(bp);
10379 	}
10380 
10381 	return rc;
10382 }
10383 
10384 static bool bnxt_drv_busy(struct bnxt *bp)
10385 {
10386 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
10387 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
10388 }
10389 
10390 static void bnxt_get_ring_stats(struct bnxt *bp,
10391 				struct rtnl_link_stats64 *stats);
10392 
10393 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
10394 			     bool link_re_init)
10395 {
10396 	/* Close the VF-reps before closing PF */
10397 	if (BNXT_PF(bp))
10398 		bnxt_vf_reps_close(bp);
10399 
10400 	/* Change device state to avoid TX queue wake up's */
10401 	bnxt_tx_disable(bp);
10402 
10403 	clear_bit(BNXT_STATE_OPEN, &bp->state);
10404 	smp_mb__after_atomic();
10405 	while (bnxt_drv_busy(bp))
10406 		msleep(20);
10407 
10408 	/* Flush rings and and disable interrupts */
10409 	bnxt_shutdown_nic(bp, irq_re_init);
10410 
10411 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
10412 
10413 	bnxt_debug_dev_exit(bp);
10414 	bnxt_disable_napi(bp);
10415 	del_timer_sync(&bp->timer);
10416 	bnxt_free_skbs(bp);
10417 
10418 	/* Save ring stats before shutdown */
10419 	if (bp->bnapi && irq_re_init)
10420 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
10421 	if (irq_re_init) {
10422 		bnxt_free_irq(bp);
10423 		bnxt_del_napi(bp);
10424 	}
10425 	bnxt_free_mem(bp, irq_re_init);
10426 }
10427 
10428 int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
10429 {
10430 	int rc = 0;
10431 
10432 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
10433 		/* If we get here, it means firmware reset is in progress
10434 		 * while we are trying to close.  We can safely proceed with
10435 		 * the close because we are holding rtnl_lock().  Some firmware
10436 		 * messages may fail as we proceed to close.  We set the
10437 		 * ABORT_ERR flag here so that the FW reset thread will later
10438 		 * abort when it gets the rtnl_lock() and sees the flag.
10439 		 */
10440 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
10441 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
10442 	}
10443 
10444 #ifdef CONFIG_BNXT_SRIOV
10445 	if (bp->sriov_cfg) {
10446 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
10447 						      !bp->sriov_cfg,
10448 						      BNXT_SRIOV_CFG_WAIT_TMO);
10449 		if (rc)
10450 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
10451 	}
10452 #endif
10453 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
10454 	return rc;
10455 }
10456 
10457 static int bnxt_close(struct net_device *dev)
10458 {
10459 	struct bnxt *bp = netdev_priv(dev);
10460 
10461 	bnxt_hwmon_close(bp);
10462 	bnxt_close_nic(bp, true, true);
10463 	bnxt_hwrm_shutdown_link(bp);
10464 	bnxt_hwrm_if_change(bp, false);
10465 	return 0;
10466 }
10467 
10468 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
10469 				   u16 *val)
10470 {
10471 	struct hwrm_port_phy_mdio_read_output *resp;
10472 	struct hwrm_port_phy_mdio_read_input *req;
10473 	int rc;
10474 
10475 	if (bp->hwrm_spec_code < 0x10a00)
10476 		return -EOPNOTSUPP;
10477 
10478 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
10479 	if (rc)
10480 		return rc;
10481 
10482 	req->port_id = cpu_to_le16(bp->pf.port_id);
10483 	req->phy_addr = phy_addr;
10484 	req->reg_addr = cpu_to_le16(reg & 0x1f);
10485 	if (mdio_phy_id_is_c45(phy_addr)) {
10486 		req->cl45_mdio = 1;
10487 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
10488 		req->dev_addr = mdio_phy_id_devad(phy_addr);
10489 		req->reg_addr = cpu_to_le16(reg);
10490 	}
10491 
10492 	resp = hwrm_req_hold(bp, req);
10493 	rc = hwrm_req_send(bp, req);
10494 	if (!rc)
10495 		*val = le16_to_cpu(resp->reg_data);
10496 	hwrm_req_drop(bp, req);
10497 	return rc;
10498 }
10499 
10500 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
10501 				    u16 val)
10502 {
10503 	struct hwrm_port_phy_mdio_write_input *req;
10504 	int rc;
10505 
10506 	if (bp->hwrm_spec_code < 0x10a00)
10507 		return -EOPNOTSUPP;
10508 
10509 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
10510 	if (rc)
10511 		return rc;
10512 
10513 	req->port_id = cpu_to_le16(bp->pf.port_id);
10514 	req->phy_addr = phy_addr;
10515 	req->reg_addr = cpu_to_le16(reg & 0x1f);
10516 	if (mdio_phy_id_is_c45(phy_addr)) {
10517 		req->cl45_mdio = 1;
10518 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
10519 		req->dev_addr = mdio_phy_id_devad(phy_addr);
10520 		req->reg_addr = cpu_to_le16(reg);
10521 	}
10522 	req->reg_data = cpu_to_le16(val);
10523 
10524 	return hwrm_req_send(bp, req);
10525 }
10526 
10527 /* rtnl_lock held */
10528 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
10529 {
10530 	struct mii_ioctl_data *mdio = if_mii(ifr);
10531 	struct bnxt *bp = netdev_priv(dev);
10532 	int rc;
10533 
10534 	switch (cmd) {
10535 	case SIOCGMIIPHY:
10536 		mdio->phy_id = bp->link_info.phy_addr;
10537 
10538 		fallthrough;
10539 	case SIOCGMIIREG: {
10540 		u16 mii_regval = 0;
10541 
10542 		if (!netif_running(dev))
10543 			return -EAGAIN;
10544 
10545 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
10546 					     &mii_regval);
10547 		mdio->val_out = mii_regval;
10548 		return rc;
10549 	}
10550 
10551 	case SIOCSMIIREG:
10552 		if (!netif_running(dev))
10553 			return -EAGAIN;
10554 
10555 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
10556 						mdio->val_in);
10557 
10558 	case SIOCSHWTSTAMP:
10559 		return bnxt_hwtstamp_set(dev, ifr);
10560 
10561 	case SIOCGHWTSTAMP:
10562 		return bnxt_hwtstamp_get(dev, ifr);
10563 
10564 	default:
10565 		/* do nothing */
10566 		break;
10567 	}
10568 	return -EOPNOTSUPP;
10569 }
10570 
10571 static void bnxt_get_ring_stats(struct bnxt *bp,
10572 				struct rtnl_link_stats64 *stats)
10573 {
10574 	int i;
10575 
10576 	for (i = 0; i < bp->cp_nr_rings; i++) {
10577 		struct bnxt_napi *bnapi = bp->bnapi[i];
10578 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
10579 		u64 *sw = cpr->stats.sw_stats;
10580 
10581 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
10582 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
10583 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
10584 
10585 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
10586 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
10587 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
10588 
10589 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
10590 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
10591 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
10592 
10593 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
10594 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
10595 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
10596 
10597 		stats->rx_missed_errors +=
10598 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
10599 
10600 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
10601 
10602 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
10603 
10604 		stats->rx_dropped +=
10605 			cpr->sw_stats.rx.rx_netpoll_discards +
10606 			cpr->sw_stats.rx.rx_oom_discards;
10607 	}
10608 }
10609 
10610 static void bnxt_add_prev_stats(struct bnxt *bp,
10611 				struct rtnl_link_stats64 *stats)
10612 {
10613 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
10614 
10615 	stats->rx_packets += prev_stats->rx_packets;
10616 	stats->tx_packets += prev_stats->tx_packets;
10617 	stats->rx_bytes += prev_stats->rx_bytes;
10618 	stats->tx_bytes += prev_stats->tx_bytes;
10619 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
10620 	stats->multicast += prev_stats->multicast;
10621 	stats->rx_dropped += prev_stats->rx_dropped;
10622 	stats->tx_dropped += prev_stats->tx_dropped;
10623 }
10624 
10625 static void
10626 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
10627 {
10628 	struct bnxt *bp = netdev_priv(dev);
10629 
10630 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
10631 	/* Make sure bnxt_close_nic() sees that we are reading stats before
10632 	 * we check the BNXT_STATE_OPEN flag.
10633 	 */
10634 	smp_mb__after_atomic();
10635 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
10636 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
10637 		*stats = bp->net_stats_prev;
10638 		return;
10639 	}
10640 
10641 	bnxt_get_ring_stats(bp, stats);
10642 	bnxt_add_prev_stats(bp, stats);
10643 
10644 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10645 		u64 *rx = bp->port_stats.sw_stats;
10646 		u64 *tx = bp->port_stats.sw_stats +
10647 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10648 
10649 		stats->rx_crc_errors =
10650 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
10651 		stats->rx_frame_errors =
10652 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
10653 		stats->rx_length_errors =
10654 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
10655 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
10656 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
10657 		stats->rx_errors =
10658 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
10659 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
10660 		stats->collisions =
10661 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
10662 		stats->tx_fifo_errors =
10663 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
10664 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
10665 	}
10666 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
10667 }
10668 
10669 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
10670 {
10671 	struct net_device *dev = bp->dev;
10672 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
10673 	struct netdev_hw_addr *ha;
10674 	u8 *haddr;
10675 	int mc_count = 0;
10676 	bool update = false;
10677 	int off = 0;
10678 
10679 	netdev_for_each_mc_addr(ha, dev) {
10680 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
10681 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10682 			vnic->mc_list_count = 0;
10683 			return false;
10684 		}
10685 		haddr = ha->addr;
10686 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
10687 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
10688 			update = true;
10689 		}
10690 		off += ETH_ALEN;
10691 		mc_count++;
10692 	}
10693 	if (mc_count)
10694 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
10695 
10696 	if (mc_count != vnic->mc_list_count) {
10697 		vnic->mc_list_count = mc_count;
10698 		update = true;
10699 	}
10700 	return update;
10701 }
10702 
10703 static bool bnxt_uc_list_updated(struct bnxt *bp)
10704 {
10705 	struct net_device *dev = bp->dev;
10706 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
10707 	struct netdev_hw_addr *ha;
10708 	int off = 0;
10709 
10710 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
10711 		return true;
10712 
10713 	netdev_for_each_uc_addr(ha, dev) {
10714 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
10715 			return true;
10716 
10717 		off += ETH_ALEN;
10718 	}
10719 	return false;
10720 }
10721 
10722 static void bnxt_set_rx_mode(struct net_device *dev)
10723 {
10724 	struct bnxt *bp = netdev_priv(dev);
10725 	struct bnxt_vnic_info *vnic;
10726 	bool mc_update = false;
10727 	bool uc_update;
10728 	u32 mask;
10729 
10730 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
10731 		return;
10732 
10733 	vnic = &bp->vnic_info[0];
10734 	mask = vnic->rx_mask;
10735 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
10736 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
10737 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
10738 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
10739 
10740 	if (dev->flags & IFF_PROMISC)
10741 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10742 
10743 	uc_update = bnxt_uc_list_updated(bp);
10744 
10745 	if (dev->flags & IFF_BROADCAST)
10746 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10747 	if (dev->flags & IFF_ALLMULTI) {
10748 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10749 		vnic->mc_list_count = 0;
10750 	} else {
10751 		mc_update = bnxt_mc_list_updated(bp, &mask);
10752 	}
10753 
10754 	if (mask != vnic->rx_mask || uc_update || mc_update) {
10755 		vnic->rx_mask = mask;
10756 
10757 		set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
10758 		bnxt_queue_sp_work(bp);
10759 	}
10760 }
10761 
10762 static int bnxt_cfg_rx_mode(struct bnxt *bp)
10763 {
10764 	struct net_device *dev = bp->dev;
10765 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
10766 	struct hwrm_cfa_l2_filter_free_input *req;
10767 	struct netdev_hw_addr *ha;
10768 	int i, off = 0, rc;
10769 	bool uc_update;
10770 
10771 	netif_addr_lock_bh(dev);
10772 	uc_update = bnxt_uc_list_updated(bp);
10773 	netif_addr_unlock_bh(dev);
10774 
10775 	if (!uc_update)
10776 		goto skip_uc;
10777 
10778 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
10779 	if (rc)
10780 		return rc;
10781 	hwrm_req_hold(bp, req);
10782 	for (i = 1; i < vnic->uc_filter_count; i++) {
10783 		req->l2_filter_id = vnic->fw_l2_filter_id[i];
10784 
10785 		rc = hwrm_req_send(bp, req);
10786 	}
10787 	hwrm_req_drop(bp, req);
10788 
10789 	vnic->uc_filter_count = 1;
10790 
10791 	netif_addr_lock_bh(dev);
10792 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
10793 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10794 	} else {
10795 		netdev_for_each_uc_addr(ha, dev) {
10796 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
10797 			off += ETH_ALEN;
10798 			vnic->uc_filter_count++;
10799 		}
10800 	}
10801 	netif_addr_unlock_bh(dev);
10802 
10803 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
10804 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
10805 		if (rc) {
10806 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
10807 				   rc);
10808 			vnic->uc_filter_count = i;
10809 			return rc;
10810 		}
10811 	}
10812 
10813 skip_uc:
10814 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
10815 	    !bnxt_promisc_ok(bp))
10816 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10817 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
10818 	if (rc && vnic->mc_list_count) {
10819 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
10820 			    rc);
10821 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10822 		vnic->mc_list_count = 0;
10823 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
10824 	}
10825 	if (rc)
10826 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
10827 			   rc);
10828 
10829 	return rc;
10830 }
10831 
10832 static bool bnxt_can_reserve_rings(struct bnxt *bp)
10833 {
10834 #ifdef CONFIG_BNXT_SRIOV
10835 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
10836 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
10837 
10838 		/* No minimum rings were provisioned by the PF.  Don't
10839 		 * reserve rings by default when device is down.
10840 		 */
10841 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
10842 			return true;
10843 
10844 		if (!netif_running(bp->dev))
10845 			return false;
10846 	}
10847 #endif
10848 	return true;
10849 }
10850 
10851 /* If the chip and firmware supports RFS */
10852 static bool bnxt_rfs_supported(struct bnxt *bp)
10853 {
10854 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
10855 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
10856 			return true;
10857 		return false;
10858 	}
10859 	/* 212 firmware is broken for aRFS */
10860 	if (BNXT_FW_MAJ(bp) == 212)
10861 		return false;
10862 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
10863 		return true;
10864 	if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
10865 		return true;
10866 	return false;
10867 }
10868 
10869 /* If runtime conditions support RFS */
10870 static bool bnxt_rfs_capable(struct bnxt *bp)
10871 {
10872 #ifdef CONFIG_RFS_ACCEL
10873 	int vnics, max_vnics, max_rss_ctxs;
10874 
10875 	if (bp->flags & BNXT_FLAG_CHIP_P5)
10876 		return bnxt_rfs_supported(bp);
10877 	if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp))
10878 		return false;
10879 
10880 	vnics = 1 + bp->rx_nr_rings;
10881 	max_vnics = bnxt_get_max_func_vnics(bp);
10882 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
10883 
10884 	/* RSS contexts not a limiting factor */
10885 	if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
10886 		max_rss_ctxs = max_vnics;
10887 	if (vnics > max_vnics || vnics > max_rss_ctxs) {
10888 		if (bp->rx_nr_rings > 1)
10889 			netdev_warn(bp->dev,
10890 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
10891 				    min(max_rss_ctxs - 1, max_vnics - 1));
10892 		return false;
10893 	}
10894 
10895 	if (!BNXT_NEW_RM(bp))
10896 		return true;
10897 
10898 	if (vnics == bp->hw_resc.resv_vnics)
10899 		return true;
10900 
10901 	bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, vnics);
10902 	if (vnics <= bp->hw_resc.resv_vnics)
10903 		return true;
10904 
10905 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
10906 	bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, 1);
10907 	return false;
10908 #else
10909 	return false;
10910 #endif
10911 }
10912 
10913 static netdev_features_t bnxt_fix_features(struct net_device *dev,
10914 					   netdev_features_t features)
10915 {
10916 	struct bnxt *bp = netdev_priv(dev);
10917 	netdev_features_t vlan_features;
10918 
10919 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
10920 		features &= ~NETIF_F_NTUPLE;
10921 
10922 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
10923 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
10924 
10925 	if (!(features & NETIF_F_GRO))
10926 		features &= ~NETIF_F_GRO_HW;
10927 
10928 	if (features & NETIF_F_GRO_HW)
10929 		features &= ~NETIF_F_LRO;
10930 
10931 	/* Both CTAG and STAG VLAN accelaration on the RX side have to be
10932 	 * turned on or off together.
10933 	 */
10934 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
10935 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
10936 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
10937 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
10938 		else if (vlan_features)
10939 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
10940 	}
10941 #ifdef CONFIG_BNXT_SRIOV
10942 	if (BNXT_VF(bp) && bp->vf.vlan)
10943 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
10944 #endif
10945 	return features;
10946 }
10947 
10948 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
10949 {
10950 	struct bnxt *bp = netdev_priv(dev);
10951 	u32 flags = bp->flags;
10952 	u32 changes;
10953 	int rc = 0;
10954 	bool re_init = false;
10955 	bool update_tpa = false;
10956 
10957 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
10958 	if (features & NETIF_F_GRO_HW)
10959 		flags |= BNXT_FLAG_GRO;
10960 	else if (features & NETIF_F_LRO)
10961 		flags |= BNXT_FLAG_LRO;
10962 
10963 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
10964 		flags &= ~BNXT_FLAG_TPA;
10965 
10966 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
10967 		flags |= BNXT_FLAG_STRIP_VLAN;
10968 
10969 	if (features & NETIF_F_NTUPLE)
10970 		flags |= BNXT_FLAG_RFS;
10971 
10972 	changes = flags ^ bp->flags;
10973 	if (changes & BNXT_FLAG_TPA) {
10974 		update_tpa = true;
10975 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
10976 		    (flags & BNXT_FLAG_TPA) == 0 ||
10977 		    (bp->flags & BNXT_FLAG_CHIP_P5))
10978 			re_init = true;
10979 	}
10980 
10981 	if (changes & ~BNXT_FLAG_TPA)
10982 		re_init = true;
10983 
10984 	if (flags != bp->flags) {
10985 		u32 old_flags = bp->flags;
10986 
10987 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
10988 			bp->flags = flags;
10989 			if (update_tpa)
10990 				bnxt_set_ring_params(bp);
10991 			return rc;
10992 		}
10993 
10994 		if (re_init) {
10995 			bnxt_close_nic(bp, false, false);
10996 			bp->flags = flags;
10997 			if (update_tpa)
10998 				bnxt_set_ring_params(bp);
10999 
11000 			return bnxt_open_nic(bp, false, false);
11001 		}
11002 		if (update_tpa) {
11003 			bp->flags = flags;
11004 			rc = bnxt_set_tpa(bp,
11005 					  (flags & BNXT_FLAG_TPA) ?
11006 					  true : false);
11007 			if (rc)
11008 				bp->flags = old_flags;
11009 		}
11010 	}
11011 	return rc;
11012 }
11013 
11014 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
11015 			      u8 **nextp)
11016 {
11017 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
11018 	int hdr_count = 0;
11019 	u8 *nexthdr;
11020 	int start;
11021 
11022 	/* Check that there are at most 2 IPv6 extension headers, no
11023 	 * fragment header, and each is <= 64 bytes.
11024 	 */
11025 	start = nw_off + sizeof(*ip6h);
11026 	nexthdr = &ip6h->nexthdr;
11027 	while (ipv6_ext_hdr(*nexthdr)) {
11028 		struct ipv6_opt_hdr *hp;
11029 		int hdrlen;
11030 
11031 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
11032 		    *nexthdr == NEXTHDR_FRAGMENT)
11033 			return false;
11034 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
11035 					  skb_headlen(skb), NULL);
11036 		if (!hp)
11037 			return false;
11038 		if (*nexthdr == NEXTHDR_AUTH)
11039 			hdrlen = ipv6_authlen(hp);
11040 		else
11041 			hdrlen = ipv6_optlen(hp);
11042 
11043 		if (hdrlen > 64)
11044 			return false;
11045 		nexthdr = &hp->nexthdr;
11046 		start += hdrlen;
11047 		hdr_count++;
11048 	}
11049 	if (nextp) {
11050 		/* Caller will check inner protocol */
11051 		if (skb->encapsulation) {
11052 			*nextp = nexthdr;
11053 			return true;
11054 		}
11055 		*nextp = NULL;
11056 	}
11057 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
11058 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
11059 }
11060 
11061 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
11062 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
11063 {
11064 	struct udphdr *uh = udp_hdr(skb);
11065 	__be16 udp_port = uh->dest;
11066 
11067 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port)
11068 		return false;
11069 	if (skb->inner_protocol_type == ENCAP_TYPE_ETHER) {
11070 		struct ethhdr *eh = inner_eth_hdr(skb);
11071 
11072 		switch (eh->h_proto) {
11073 		case htons(ETH_P_IP):
11074 			return true;
11075 		case htons(ETH_P_IPV6):
11076 			return bnxt_exthdr_check(bp, skb,
11077 						 skb_inner_network_offset(skb),
11078 						 NULL);
11079 		}
11080 	}
11081 	return false;
11082 }
11083 
11084 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
11085 {
11086 	switch (l4_proto) {
11087 	case IPPROTO_UDP:
11088 		return bnxt_udp_tunl_check(bp, skb);
11089 	case IPPROTO_IPIP:
11090 		return true;
11091 	case IPPROTO_GRE: {
11092 		switch (skb->inner_protocol) {
11093 		default:
11094 			return false;
11095 		case htons(ETH_P_IP):
11096 			return true;
11097 		case htons(ETH_P_IPV6):
11098 			fallthrough;
11099 		}
11100 	}
11101 	case IPPROTO_IPV6:
11102 		/* Check ext headers of inner ipv6 */
11103 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
11104 					 NULL);
11105 	}
11106 	return false;
11107 }
11108 
11109 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
11110 					     struct net_device *dev,
11111 					     netdev_features_t features)
11112 {
11113 	struct bnxt *bp = netdev_priv(dev);
11114 	u8 *l4_proto;
11115 
11116 	features = vlan_features_check(skb, features);
11117 	switch (vlan_get_protocol(skb)) {
11118 	case htons(ETH_P_IP):
11119 		if (!skb->encapsulation)
11120 			return features;
11121 		l4_proto = &ip_hdr(skb)->protocol;
11122 		if (bnxt_tunl_check(bp, skb, *l4_proto))
11123 			return features;
11124 		break;
11125 	case htons(ETH_P_IPV6):
11126 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
11127 				       &l4_proto))
11128 			break;
11129 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
11130 			return features;
11131 		break;
11132 	}
11133 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
11134 }
11135 
11136 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
11137 			 u32 *reg_buf)
11138 {
11139 	struct hwrm_dbg_read_direct_output *resp;
11140 	struct hwrm_dbg_read_direct_input *req;
11141 	__le32 *dbg_reg_buf;
11142 	dma_addr_t mapping;
11143 	int rc, i;
11144 
11145 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
11146 	if (rc)
11147 		return rc;
11148 
11149 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
11150 					 &mapping);
11151 	if (!dbg_reg_buf) {
11152 		rc = -ENOMEM;
11153 		goto dbg_rd_reg_exit;
11154 	}
11155 
11156 	req->host_dest_addr = cpu_to_le64(mapping);
11157 
11158 	resp = hwrm_req_hold(bp, req);
11159 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
11160 	req->read_len32 = cpu_to_le32(num_words);
11161 
11162 	rc = hwrm_req_send(bp, req);
11163 	if (rc || resp->error_code) {
11164 		rc = -EIO;
11165 		goto dbg_rd_reg_exit;
11166 	}
11167 	for (i = 0; i < num_words; i++)
11168 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
11169 
11170 dbg_rd_reg_exit:
11171 	hwrm_req_drop(bp, req);
11172 	return rc;
11173 }
11174 
11175 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
11176 				       u32 ring_id, u32 *prod, u32 *cons)
11177 {
11178 	struct hwrm_dbg_ring_info_get_output *resp;
11179 	struct hwrm_dbg_ring_info_get_input *req;
11180 	int rc;
11181 
11182 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
11183 	if (rc)
11184 		return rc;
11185 
11186 	req->ring_type = ring_type;
11187 	req->fw_ring_id = cpu_to_le32(ring_id);
11188 	resp = hwrm_req_hold(bp, req);
11189 	rc = hwrm_req_send(bp, req);
11190 	if (!rc) {
11191 		*prod = le32_to_cpu(resp->producer_index);
11192 		*cons = le32_to_cpu(resp->consumer_index);
11193 	}
11194 	hwrm_req_drop(bp, req);
11195 	return rc;
11196 }
11197 
11198 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
11199 {
11200 	struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
11201 	int i = bnapi->index;
11202 
11203 	if (!txr)
11204 		return;
11205 
11206 	netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
11207 		    i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
11208 		    txr->tx_cons);
11209 }
11210 
11211 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
11212 {
11213 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
11214 	int i = bnapi->index;
11215 
11216 	if (!rxr)
11217 		return;
11218 
11219 	netdev_info(bnapi->bp->dev, "[%d]: rx{fw_ring: %d prod: %x} rx_agg{fw_ring: %d agg_prod: %x sw_agg_prod: %x}\n",
11220 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
11221 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
11222 		    rxr->rx_sw_agg_prod);
11223 }
11224 
11225 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
11226 {
11227 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
11228 	int i = bnapi->index;
11229 
11230 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
11231 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
11232 }
11233 
11234 static void bnxt_dbg_dump_states(struct bnxt *bp)
11235 {
11236 	int i;
11237 	struct bnxt_napi *bnapi;
11238 
11239 	for (i = 0; i < bp->cp_nr_rings; i++) {
11240 		bnapi = bp->bnapi[i];
11241 		if (netif_msg_drv(bp)) {
11242 			bnxt_dump_tx_sw_state(bnapi);
11243 			bnxt_dump_rx_sw_state(bnapi);
11244 			bnxt_dump_cp_sw_state(bnapi);
11245 		}
11246 	}
11247 }
11248 
11249 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
11250 {
11251 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
11252 	struct hwrm_ring_reset_input *req;
11253 	struct bnxt_napi *bnapi = rxr->bnapi;
11254 	struct bnxt_cp_ring_info *cpr;
11255 	u16 cp_ring_id;
11256 	int rc;
11257 
11258 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
11259 	if (rc)
11260 		return rc;
11261 
11262 	cpr = &bnapi->cp_ring;
11263 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
11264 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
11265 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
11266 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
11267 	return hwrm_req_send_silent(bp, req);
11268 }
11269 
11270 static void bnxt_reset_task(struct bnxt *bp, bool silent)
11271 {
11272 	if (!silent)
11273 		bnxt_dbg_dump_states(bp);
11274 	if (netif_running(bp->dev)) {
11275 		int rc;
11276 
11277 		if (silent) {
11278 			bnxt_close_nic(bp, false, false);
11279 			bnxt_open_nic(bp, false, false);
11280 		} else {
11281 			bnxt_ulp_stop(bp);
11282 			bnxt_close_nic(bp, true, false);
11283 			rc = bnxt_open_nic(bp, true, false);
11284 			bnxt_ulp_start(bp, rc);
11285 		}
11286 	}
11287 }
11288 
11289 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
11290 {
11291 	struct bnxt *bp = netdev_priv(dev);
11292 
11293 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
11294 	set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
11295 	bnxt_queue_sp_work(bp);
11296 }
11297 
11298 static void bnxt_fw_health_check(struct bnxt *bp)
11299 {
11300 	struct bnxt_fw_health *fw_health = bp->fw_health;
11301 	u32 val;
11302 
11303 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11304 		return;
11305 
11306 	/* Make sure it is enabled before checking the tmr_counter. */
11307 	smp_rmb();
11308 	if (fw_health->tmr_counter) {
11309 		fw_health->tmr_counter--;
11310 		return;
11311 	}
11312 
11313 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
11314 	if (val == fw_health->last_fw_heartbeat) {
11315 		fw_health->arrests++;
11316 		goto fw_reset;
11317 	}
11318 
11319 	fw_health->last_fw_heartbeat = val;
11320 
11321 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
11322 	if (val != fw_health->last_fw_reset_cnt) {
11323 		fw_health->discoveries++;
11324 		goto fw_reset;
11325 	}
11326 
11327 	fw_health->tmr_counter = fw_health->tmr_multiplier;
11328 	return;
11329 
11330 fw_reset:
11331 	set_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event);
11332 	bnxt_queue_sp_work(bp);
11333 }
11334 
11335 static void bnxt_timer(struct timer_list *t)
11336 {
11337 	struct bnxt *bp = from_timer(bp, t, timer);
11338 	struct net_device *dev = bp->dev;
11339 
11340 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
11341 		return;
11342 
11343 	if (atomic_read(&bp->intr_sem) != 0)
11344 		goto bnxt_restart_timer;
11345 
11346 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
11347 		bnxt_fw_health_check(bp);
11348 
11349 	if (bp->link_info.link_up && bp->stats_coal_ticks) {
11350 		set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
11351 		bnxt_queue_sp_work(bp);
11352 	}
11353 
11354 	if (bnxt_tc_flower_enabled(bp)) {
11355 		set_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event);
11356 		bnxt_queue_sp_work(bp);
11357 	}
11358 
11359 #ifdef CONFIG_RFS_ACCEL
11360 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count) {
11361 		set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
11362 		bnxt_queue_sp_work(bp);
11363 	}
11364 #endif /*CONFIG_RFS_ACCEL*/
11365 
11366 	if (bp->link_info.phy_retry) {
11367 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
11368 			bp->link_info.phy_retry = false;
11369 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
11370 		} else {
11371 			set_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event);
11372 			bnxt_queue_sp_work(bp);
11373 		}
11374 	}
11375 
11376 	if ((bp->flags & BNXT_FLAG_CHIP_P5) && !bp->chip_rev &&
11377 	    netif_carrier_ok(dev)) {
11378 		set_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event);
11379 		bnxt_queue_sp_work(bp);
11380 	}
11381 bnxt_restart_timer:
11382 	mod_timer(&bp->timer, jiffies + bp->current_interval);
11383 }
11384 
11385 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
11386 {
11387 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
11388 	 * set.  If the device is being closed, bnxt_close() may be holding
11389 	 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear.  So we
11390 	 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
11391 	 */
11392 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11393 	rtnl_lock();
11394 }
11395 
11396 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
11397 {
11398 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11399 	rtnl_unlock();
11400 }
11401 
11402 /* Only called from bnxt_sp_task() */
11403 static void bnxt_reset(struct bnxt *bp, bool silent)
11404 {
11405 	bnxt_rtnl_lock_sp(bp);
11406 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
11407 		bnxt_reset_task(bp, silent);
11408 	bnxt_rtnl_unlock_sp(bp);
11409 }
11410 
11411 /* Only called from bnxt_sp_task() */
11412 static void bnxt_rx_ring_reset(struct bnxt *bp)
11413 {
11414 	int i;
11415 
11416 	bnxt_rtnl_lock_sp(bp);
11417 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
11418 		bnxt_rtnl_unlock_sp(bp);
11419 		return;
11420 	}
11421 	/* Disable and flush TPA before resetting the RX ring */
11422 	if (bp->flags & BNXT_FLAG_TPA)
11423 		bnxt_set_tpa(bp, false);
11424 	for (i = 0; i < bp->rx_nr_rings; i++) {
11425 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
11426 		struct bnxt_cp_ring_info *cpr;
11427 		int rc;
11428 
11429 		if (!rxr->bnapi->in_reset)
11430 			continue;
11431 
11432 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
11433 		if (rc) {
11434 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
11435 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
11436 			else
11437 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
11438 					    rc);
11439 			bnxt_reset_task(bp, true);
11440 			break;
11441 		}
11442 		bnxt_free_one_rx_ring_skbs(bp, i);
11443 		rxr->rx_prod = 0;
11444 		rxr->rx_agg_prod = 0;
11445 		rxr->rx_sw_agg_prod = 0;
11446 		rxr->rx_next_cons = 0;
11447 		rxr->bnapi->in_reset = false;
11448 		bnxt_alloc_one_rx_ring(bp, i);
11449 		cpr = &rxr->bnapi->cp_ring;
11450 		cpr->sw_stats.rx.rx_resets++;
11451 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
11452 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
11453 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
11454 	}
11455 	if (bp->flags & BNXT_FLAG_TPA)
11456 		bnxt_set_tpa(bp, true);
11457 	bnxt_rtnl_unlock_sp(bp);
11458 }
11459 
11460 static void bnxt_fw_reset_close(struct bnxt *bp)
11461 {
11462 	bnxt_ulp_stop(bp);
11463 	/* When firmware is in fatal state, quiesce device and disable
11464 	 * bus master to prevent any potential bad DMAs before freeing
11465 	 * kernel memory.
11466 	 */
11467 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
11468 		u16 val = 0;
11469 
11470 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
11471 		if (val == 0xffff)
11472 			bp->fw_reset_min_dsecs = 0;
11473 		bnxt_tx_disable(bp);
11474 		bnxt_disable_napi(bp);
11475 		bnxt_disable_int_sync(bp);
11476 		bnxt_free_irq(bp);
11477 		bnxt_clear_int_mode(bp);
11478 		pci_disable_device(bp->pdev);
11479 	}
11480 	__bnxt_close_nic(bp, true, false);
11481 	bnxt_vf_reps_free(bp);
11482 	bnxt_clear_int_mode(bp);
11483 	bnxt_hwrm_func_drv_unrgtr(bp);
11484 	if (pci_is_enabled(bp->pdev))
11485 		pci_disable_device(bp->pdev);
11486 	bnxt_free_ctx_mem(bp);
11487 	kfree(bp->ctx);
11488 	bp->ctx = NULL;
11489 }
11490 
11491 static bool is_bnxt_fw_ok(struct bnxt *bp)
11492 {
11493 	struct bnxt_fw_health *fw_health = bp->fw_health;
11494 	bool no_heartbeat = false, has_reset = false;
11495 	u32 val;
11496 
11497 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
11498 	if (val == fw_health->last_fw_heartbeat)
11499 		no_heartbeat = true;
11500 
11501 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
11502 	if (val != fw_health->last_fw_reset_cnt)
11503 		has_reset = true;
11504 
11505 	if (!no_heartbeat && has_reset)
11506 		return true;
11507 
11508 	return false;
11509 }
11510 
11511 /* rtnl_lock is acquired before calling this function */
11512 static void bnxt_force_fw_reset(struct bnxt *bp)
11513 {
11514 	struct bnxt_fw_health *fw_health = bp->fw_health;
11515 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
11516 	u32 wait_dsecs;
11517 
11518 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
11519 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11520 		return;
11521 
11522 	if (ptp) {
11523 		spin_lock_bh(&ptp->ptp_lock);
11524 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11525 		spin_unlock_bh(&ptp->ptp_lock);
11526 	} else {
11527 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11528 	}
11529 	bnxt_fw_reset_close(bp);
11530 	wait_dsecs = fw_health->master_func_wait_dsecs;
11531 	if (fw_health->primary) {
11532 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
11533 			wait_dsecs = 0;
11534 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
11535 	} else {
11536 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
11537 		wait_dsecs = fw_health->normal_func_wait_dsecs;
11538 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11539 	}
11540 
11541 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
11542 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
11543 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
11544 }
11545 
11546 void bnxt_fw_exception(struct bnxt *bp)
11547 {
11548 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
11549 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
11550 	bnxt_rtnl_lock_sp(bp);
11551 	bnxt_force_fw_reset(bp);
11552 	bnxt_rtnl_unlock_sp(bp);
11553 }
11554 
11555 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
11556  * < 0 on error.
11557  */
11558 static int bnxt_get_registered_vfs(struct bnxt *bp)
11559 {
11560 #ifdef CONFIG_BNXT_SRIOV
11561 	int rc;
11562 
11563 	if (!BNXT_PF(bp))
11564 		return 0;
11565 
11566 	rc = bnxt_hwrm_func_qcfg(bp);
11567 	if (rc) {
11568 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
11569 		return rc;
11570 	}
11571 	if (bp->pf.registered_vfs)
11572 		return bp->pf.registered_vfs;
11573 	if (bp->sriov_cfg)
11574 		return 1;
11575 #endif
11576 	return 0;
11577 }
11578 
11579 void bnxt_fw_reset(struct bnxt *bp)
11580 {
11581 	bnxt_rtnl_lock_sp(bp);
11582 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
11583 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
11584 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
11585 		int n = 0, tmo;
11586 
11587 		if (ptp) {
11588 			spin_lock_bh(&ptp->ptp_lock);
11589 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11590 			spin_unlock_bh(&ptp->ptp_lock);
11591 		} else {
11592 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11593 		}
11594 		if (bp->pf.active_vfs &&
11595 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
11596 			n = bnxt_get_registered_vfs(bp);
11597 		if (n < 0) {
11598 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
11599 				   n);
11600 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11601 			dev_close(bp->dev);
11602 			goto fw_reset_exit;
11603 		} else if (n > 0) {
11604 			u16 vf_tmo_dsecs = n * 10;
11605 
11606 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
11607 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
11608 			bp->fw_reset_state =
11609 				BNXT_FW_RESET_STATE_POLL_VF;
11610 			bnxt_queue_fw_reset_work(bp, HZ / 10);
11611 			goto fw_reset_exit;
11612 		}
11613 		bnxt_fw_reset_close(bp);
11614 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
11615 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
11616 			tmo = HZ / 10;
11617 		} else {
11618 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11619 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
11620 		}
11621 		bnxt_queue_fw_reset_work(bp, tmo);
11622 	}
11623 fw_reset_exit:
11624 	bnxt_rtnl_unlock_sp(bp);
11625 }
11626 
11627 static void bnxt_chk_missed_irq(struct bnxt *bp)
11628 {
11629 	int i;
11630 
11631 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
11632 		return;
11633 
11634 	for (i = 0; i < bp->cp_nr_rings; i++) {
11635 		struct bnxt_napi *bnapi = bp->bnapi[i];
11636 		struct bnxt_cp_ring_info *cpr;
11637 		u32 fw_ring_id;
11638 		int j;
11639 
11640 		if (!bnapi)
11641 			continue;
11642 
11643 		cpr = &bnapi->cp_ring;
11644 		for (j = 0; j < 2; j++) {
11645 			struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
11646 			u32 val[2];
11647 
11648 			if (!cpr2 || cpr2->has_more_work ||
11649 			    !bnxt_has_work(bp, cpr2))
11650 				continue;
11651 
11652 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
11653 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
11654 				continue;
11655 			}
11656 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
11657 			bnxt_dbg_hwrm_ring_info_get(bp,
11658 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
11659 				fw_ring_id, &val[0], &val[1]);
11660 			cpr->sw_stats.cmn.missed_irqs++;
11661 		}
11662 	}
11663 }
11664 
11665 static void bnxt_cfg_ntp_filters(struct bnxt *);
11666 
11667 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
11668 {
11669 	struct bnxt_link_info *link_info = &bp->link_info;
11670 
11671 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
11672 		link_info->autoneg = BNXT_AUTONEG_SPEED;
11673 		if (bp->hwrm_spec_code >= 0x10201) {
11674 			if (link_info->auto_pause_setting &
11675 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
11676 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
11677 		} else {
11678 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
11679 		}
11680 		link_info->advertising = link_info->auto_link_speeds;
11681 		link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
11682 	} else {
11683 		link_info->req_link_speed = link_info->force_link_speed;
11684 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
11685 		if (link_info->force_pam4_link_speed) {
11686 			link_info->req_link_speed =
11687 				link_info->force_pam4_link_speed;
11688 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
11689 		}
11690 		link_info->req_duplex = link_info->duplex_setting;
11691 	}
11692 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
11693 		link_info->req_flow_ctrl =
11694 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
11695 	else
11696 		link_info->req_flow_ctrl = link_info->force_pause_setting;
11697 }
11698 
11699 static void bnxt_fw_echo_reply(struct bnxt *bp)
11700 {
11701 	struct bnxt_fw_health *fw_health = bp->fw_health;
11702 	struct hwrm_func_echo_response_input *req;
11703 	int rc;
11704 
11705 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
11706 	if (rc)
11707 		return;
11708 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
11709 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
11710 	hwrm_req_send(bp, req);
11711 }
11712 
11713 static void bnxt_sp_task(struct work_struct *work)
11714 {
11715 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
11716 
11717 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11718 	smp_mb__after_atomic();
11719 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
11720 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11721 		return;
11722 	}
11723 
11724 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
11725 		bnxt_cfg_rx_mode(bp);
11726 
11727 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
11728 		bnxt_cfg_ntp_filters(bp);
11729 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
11730 		bnxt_hwrm_exec_fwd_req(bp);
11731 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
11732 		bnxt_hwrm_port_qstats(bp, 0);
11733 		bnxt_hwrm_port_qstats_ext(bp, 0);
11734 		bnxt_accumulate_all_stats(bp);
11735 	}
11736 
11737 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
11738 		int rc;
11739 
11740 		mutex_lock(&bp->link_lock);
11741 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
11742 				       &bp->sp_event))
11743 			bnxt_hwrm_phy_qcaps(bp);
11744 
11745 		rc = bnxt_update_link(bp, true);
11746 		if (rc)
11747 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
11748 				   rc);
11749 
11750 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
11751 				       &bp->sp_event))
11752 			bnxt_init_ethtool_link_settings(bp);
11753 		mutex_unlock(&bp->link_lock);
11754 	}
11755 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
11756 		int rc;
11757 
11758 		mutex_lock(&bp->link_lock);
11759 		rc = bnxt_update_phy_setting(bp);
11760 		mutex_unlock(&bp->link_lock);
11761 		if (rc) {
11762 			netdev_warn(bp->dev, "update phy settings retry failed\n");
11763 		} else {
11764 			bp->link_info.phy_retry = false;
11765 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
11766 		}
11767 	}
11768 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
11769 		mutex_lock(&bp->link_lock);
11770 		bnxt_get_port_module_status(bp);
11771 		mutex_unlock(&bp->link_lock);
11772 	}
11773 
11774 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
11775 		bnxt_tc_flow_stats_work(bp);
11776 
11777 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
11778 		bnxt_chk_missed_irq(bp);
11779 
11780 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
11781 		bnxt_fw_echo_reply(bp);
11782 
11783 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
11784 	 * must be the last functions to be called before exiting.
11785 	 */
11786 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
11787 		bnxt_reset(bp, false);
11788 
11789 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
11790 		bnxt_reset(bp, true);
11791 
11792 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
11793 		bnxt_rx_ring_reset(bp);
11794 
11795 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
11796 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
11797 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
11798 			bnxt_devlink_health_fw_report(bp);
11799 		else
11800 			bnxt_fw_reset(bp);
11801 	}
11802 
11803 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
11804 		if (!is_bnxt_fw_ok(bp))
11805 			bnxt_devlink_health_fw_report(bp);
11806 	}
11807 
11808 	smp_mb__before_atomic();
11809 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11810 }
11811 
11812 /* Under rtnl_lock */
11813 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
11814 		     int tx_xdp)
11815 {
11816 	int max_rx, max_tx, tx_sets = 1;
11817 	int tx_rings_needed, stats;
11818 	int rx_rings = rx;
11819 	int cp, vnics, rc;
11820 
11821 	if (tcs)
11822 		tx_sets = tcs;
11823 
11824 	rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
11825 	if (rc)
11826 		return rc;
11827 
11828 	if (max_rx < rx)
11829 		return -ENOMEM;
11830 
11831 	tx_rings_needed = tx * tx_sets + tx_xdp;
11832 	if (max_tx < tx_rings_needed)
11833 		return -ENOMEM;
11834 
11835 	vnics = 1;
11836 	if ((bp->flags & (BNXT_FLAG_RFS | BNXT_FLAG_CHIP_P5)) == BNXT_FLAG_RFS)
11837 		vnics += rx_rings;
11838 
11839 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
11840 		rx_rings <<= 1;
11841 	cp = sh ? max_t(int, tx_rings_needed, rx) : tx_rings_needed + rx;
11842 	stats = cp;
11843 	if (BNXT_NEW_RM(bp)) {
11844 		cp += bnxt_get_ulp_msix_num(bp);
11845 		stats += bnxt_get_ulp_stat_ctxs(bp);
11846 	}
11847 	return bnxt_hwrm_check_rings(bp, tx_rings_needed, rx_rings, rx, cp,
11848 				     stats, vnics);
11849 }
11850 
11851 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
11852 {
11853 	if (bp->bar2) {
11854 		pci_iounmap(pdev, bp->bar2);
11855 		bp->bar2 = NULL;
11856 	}
11857 
11858 	if (bp->bar1) {
11859 		pci_iounmap(pdev, bp->bar1);
11860 		bp->bar1 = NULL;
11861 	}
11862 
11863 	if (bp->bar0) {
11864 		pci_iounmap(pdev, bp->bar0);
11865 		bp->bar0 = NULL;
11866 	}
11867 }
11868 
11869 static void bnxt_cleanup_pci(struct bnxt *bp)
11870 {
11871 	bnxt_unmap_bars(bp, bp->pdev);
11872 	pci_release_regions(bp->pdev);
11873 	if (pci_is_enabled(bp->pdev))
11874 		pci_disable_device(bp->pdev);
11875 }
11876 
11877 static void bnxt_init_dflt_coal(struct bnxt *bp)
11878 {
11879 	struct bnxt_coal *coal;
11880 
11881 	/* Tick values in micro seconds.
11882 	 * 1 coal_buf x bufs_per_record = 1 completion record.
11883 	 */
11884 	coal = &bp->rx_coal;
11885 	coal->coal_ticks = 10;
11886 	coal->coal_bufs = 30;
11887 	coal->coal_ticks_irq = 1;
11888 	coal->coal_bufs_irq = 2;
11889 	coal->idle_thresh = 50;
11890 	coal->bufs_per_record = 2;
11891 	coal->budget = 64;		/* NAPI budget */
11892 
11893 	coal = &bp->tx_coal;
11894 	coal->coal_ticks = 28;
11895 	coal->coal_bufs = 30;
11896 	coal->coal_ticks_irq = 2;
11897 	coal->coal_bufs_irq = 2;
11898 	coal->bufs_per_record = 1;
11899 
11900 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
11901 }
11902 
11903 static int bnxt_fw_init_one_p1(struct bnxt *bp)
11904 {
11905 	int rc;
11906 
11907 	bp->fw_cap = 0;
11908 	rc = bnxt_hwrm_ver_get(bp);
11909 	bnxt_try_map_fw_health_reg(bp);
11910 	if (rc) {
11911 		rc = bnxt_try_recover_fw(bp);
11912 		if (rc)
11913 			return rc;
11914 		rc = bnxt_hwrm_ver_get(bp);
11915 		if (rc)
11916 			return rc;
11917 	}
11918 
11919 	bnxt_nvm_cfg_ver_get(bp);
11920 
11921 	rc = bnxt_hwrm_func_reset(bp);
11922 	if (rc)
11923 		return -ENODEV;
11924 
11925 	bnxt_hwrm_fw_set_time(bp);
11926 	return 0;
11927 }
11928 
11929 static int bnxt_fw_init_one_p2(struct bnxt *bp)
11930 {
11931 	int rc;
11932 
11933 	/* Get the MAX capabilities for this function */
11934 	rc = bnxt_hwrm_func_qcaps(bp);
11935 	if (rc) {
11936 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
11937 			   rc);
11938 		return -ENODEV;
11939 	}
11940 
11941 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
11942 	if (rc)
11943 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
11944 			    rc);
11945 
11946 	if (bnxt_alloc_fw_health(bp)) {
11947 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
11948 	} else {
11949 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
11950 		if (rc)
11951 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
11952 				    rc);
11953 	}
11954 
11955 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
11956 	if (rc)
11957 		return -ENODEV;
11958 
11959 	bnxt_hwrm_func_qcfg(bp);
11960 	bnxt_hwrm_vnic_qcaps(bp);
11961 	bnxt_hwrm_port_led_qcaps(bp);
11962 	bnxt_ethtool_init(bp);
11963 	bnxt_dcb_init(bp);
11964 	return 0;
11965 }
11966 
11967 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
11968 {
11969 	bp->flags &= ~BNXT_FLAG_UDP_RSS_CAP;
11970 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
11971 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
11972 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
11973 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
11974 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
11975 		bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
11976 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
11977 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
11978 	}
11979 }
11980 
11981 static void bnxt_set_dflt_rfs(struct bnxt *bp)
11982 {
11983 	struct net_device *dev = bp->dev;
11984 
11985 	dev->hw_features &= ~NETIF_F_NTUPLE;
11986 	dev->features &= ~NETIF_F_NTUPLE;
11987 	bp->flags &= ~BNXT_FLAG_RFS;
11988 	if (bnxt_rfs_supported(bp)) {
11989 		dev->hw_features |= NETIF_F_NTUPLE;
11990 		if (bnxt_rfs_capable(bp)) {
11991 			bp->flags |= BNXT_FLAG_RFS;
11992 			dev->features |= NETIF_F_NTUPLE;
11993 		}
11994 	}
11995 }
11996 
11997 static void bnxt_fw_init_one_p3(struct bnxt *bp)
11998 {
11999 	struct pci_dev *pdev = bp->pdev;
12000 
12001 	bnxt_set_dflt_rss_hash_type(bp);
12002 	bnxt_set_dflt_rfs(bp);
12003 
12004 	bnxt_get_wol_settings(bp);
12005 	if (bp->flags & BNXT_FLAG_WOL_CAP)
12006 		device_set_wakeup_enable(&pdev->dev, bp->wol);
12007 	else
12008 		device_set_wakeup_capable(&pdev->dev, false);
12009 
12010 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
12011 	bnxt_hwrm_coal_params_qcaps(bp);
12012 }
12013 
12014 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
12015 
12016 int bnxt_fw_init_one(struct bnxt *bp)
12017 {
12018 	int rc;
12019 
12020 	rc = bnxt_fw_init_one_p1(bp);
12021 	if (rc) {
12022 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
12023 		return rc;
12024 	}
12025 	rc = bnxt_fw_init_one_p2(bp);
12026 	if (rc) {
12027 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
12028 		return rc;
12029 	}
12030 	rc = bnxt_probe_phy(bp, false);
12031 	if (rc)
12032 		return rc;
12033 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
12034 	if (rc)
12035 		return rc;
12036 
12037 	/* In case fw capabilities have changed, destroy the unneeded
12038 	 * reporters and create newly capable ones.
12039 	 */
12040 	bnxt_dl_fw_reporters_destroy(bp, false);
12041 	bnxt_dl_fw_reporters_create(bp);
12042 	bnxt_fw_init_one_p3(bp);
12043 	return 0;
12044 }
12045 
12046 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
12047 {
12048 	struct bnxt_fw_health *fw_health = bp->fw_health;
12049 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
12050 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
12051 	u32 reg_type, reg_off, delay_msecs;
12052 
12053 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
12054 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
12055 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
12056 	switch (reg_type) {
12057 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
12058 		pci_write_config_dword(bp->pdev, reg_off, val);
12059 		break;
12060 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
12061 		writel(reg_off & BNXT_GRC_BASE_MASK,
12062 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
12063 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
12064 		fallthrough;
12065 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
12066 		writel(val, bp->bar0 + reg_off);
12067 		break;
12068 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
12069 		writel(val, bp->bar1 + reg_off);
12070 		break;
12071 	}
12072 	if (delay_msecs) {
12073 		pci_read_config_dword(bp->pdev, 0, &val);
12074 		msleep(delay_msecs);
12075 	}
12076 }
12077 
12078 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
12079 {
12080 	struct hwrm_func_qcfg_output *resp;
12081 	struct hwrm_func_qcfg_input *req;
12082 	bool result = true; /* firmware will enforce if unknown */
12083 
12084 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
12085 		return result;
12086 
12087 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
12088 		return result;
12089 
12090 	req->fid = cpu_to_le16(0xffff);
12091 	resp = hwrm_req_hold(bp, req);
12092 	if (!hwrm_req_send(bp, req))
12093 		result = !!(le16_to_cpu(resp->flags) &
12094 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
12095 	hwrm_req_drop(bp, req);
12096 	return result;
12097 }
12098 
12099 static void bnxt_reset_all(struct bnxt *bp)
12100 {
12101 	struct bnxt_fw_health *fw_health = bp->fw_health;
12102 	int i, rc;
12103 
12104 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
12105 		bnxt_fw_reset_via_optee(bp);
12106 		bp->fw_reset_timestamp = jiffies;
12107 		return;
12108 	}
12109 
12110 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
12111 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
12112 			bnxt_fw_reset_writel(bp, i);
12113 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
12114 		struct hwrm_fw_reset_input *req;
12115 
12116 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
12117 		if (!rc) {
12118 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
12119 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
12120 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
12121 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
12122 			rc = hwrm_req_send(bp, req);
12123 		}
12124 		if (rc != -ENODEV)
12125 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
12126 	}
12127 	bp->fw_reset_timestamp = jiffies;
12128 }
12129 
12130 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
12131 {
12132 	return time_after(jiffies, bp->fw_reset_timestamp +
12133 			  (bp->fw_reset_max_dsecs * HZ / 10));
12134 }
12135 
12136 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
12137 {
12138 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
12139 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF) {
12140 		bnxt_ulp_start(bp, rc);
12141 		bnxt_dl_health_fw_status_update(bp, false);
12142 	}
12143 	bp->fw_reset_state = 0;
12144 	dev_close(bp->dev);
12145 }
12146 
12147 static void bnxt_fw_reset_task(struct work_struct *work)
12148 {
12149 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
12150 	int rc = 0;
12151 
12152 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12153 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
12154 		return;
12155 	}
12156 
12157 	switch (bp->fw_reset_state) {
12158 	case BNXT_FW_RESET_STATE_POLL_VF: {
12159 		int n = bnxt_get_registered_vfs(bp);
12160 		int tmo;
12161 
12162 		if (n < 0) {
12163 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
12164 				   n, jiffies_to_msecs(jiffies -
12165 				   bp->fw_reset_timestamp));
12166 			goto fw_reset_abort;
12167 		} else if (n > 0) {
12168 			if (bnxt_fw_reset_timeout(bp)) {
12169 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
12170 				bp->fw_reset_state = 0;
12171 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
12172 					   n);
12173 				return;
12174 			}
12175 			bnxt_queue_fw_reset_work(bp, HZ / 10);
12176 			return;
12177 		}
12178 		bp->fw_reset_timestamp = jiffies;
12179 		rtnl_lock();
12180 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12181 			bnxt_fw_reset_abort(bp, rc);
12182 			rtnl_unlock();
12183 			return;
12184 		}
12185 		bnxt_fw_reset_close(bp);
12186 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
12187 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
12188 			tmo = HZ / 10;
12189 		} else {
12190 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
12191 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
12192 		}
12193 		rtnl_unlock();
12194 		bnxt_queue_fw_reset_work(bp, tmo);
12195 		return;
12196 	}
12197 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
12198 		u32 val;
12199 
12200 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12201 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
12202 		    !bnxt_fw_reset_timeout(bp)) {
12203 			bnxt_queue_fw_reset_work(bp, HZ / 5);
12204 			return;
12205 		}
12206 
12207 		if (!bp->fw_health->primary) {
12208 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
12209 
12210 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
12211 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
12212 			return;
12213 		}
12214 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
12215 	}
12216 		fallthrough;
12217 	case BNXT_FW_RESET_STATE_RESET_FW:
12218 		bnxt_reset_all(bp);
12219 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
12220 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
12221 		return;
12222 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
12223 		bnxt_inv_fw_health_reg(bp);
12224 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
12225 		    !bp->fw_reset_min_dsecs) {
12226 			u16 val;
12227 
12228 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
12229 			if (val == 0xffff) {
12230 				if (bnxt_fw_reset_timeout(bp)) {
12231 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
12232 					rc = -ETIMEDOUT;
12233 					goto fw_reset_abort;
12234 				}
12235 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
12236 				return;
12237 			}
12238 		}
12239 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
12240 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
12241 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
12242 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
12243 			bnxt_dl_remote_reload(bp);
12244 		if (pci_enable_device(bp->pdev)) {
12245 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
12246 			rc = -ENODEV;
12247 			goto fw_reset_abort;
12248 		}
12249 		pci_set_master(bp->pdev);
12250 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
12251 		fallthrough;
12252 	case BNXT_FW_RESET_STATE_POLL_FW:
12253 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
12254 		rc = bnxt_hwrm_poll(bp);
12255 		if (rc) {
12256 			if (bnxt_fw_reset_timeout(bp)) {
12257 				netdev_err(bp->dev, "Firmware reset aborted\n");
12258 				goto fw_reset_abort_status;
12259 			}
12260 			bnxt_queue_fw_reset_work(bp, HZ / 5);
12261 			return;
12262 		}
12263 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
12264 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
12265 		fallthrough;
12266 	case BNXT_FW_RESET_STATE_OPENING:
12267 		while (!rtnl_trylock()) {
12268 			bnxt_queue_fw_reset_work(bp, HZ / 10);
12269 			return;
12270 		}
12271 		rc = bnxt_open(bp->dev);
12272 		if (rc) {
12273 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
12274 			bnxt_fw_reset_abort(bp, rc);
12275 			rtnl_unlock();
12276 			return;
12277 		}
12278 
12279 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
12280 		    bp->fw_health->enabled) {
12281 			bp->fw_health->last_fw_reset_cnt =
12282 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
12283 		}
12284 		bp->fw_reset_state = 0;
12285 		/* Make sure fw_reset_state is 0 before clearing the flag */
12286 		smp_mb__before_atomic();
12287 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
12288 		bnxt_ulp_start(bp, 0);
12289 		bnxt_reenable_sriov(bp);
12290 		bnxt_vf_reps_alloc(bp);
12291 		bnxt_vf_reps_open(bp);
12292 		bnxt_ptp_reapply_pps(bp);
12293 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
12294 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
12295 			bnxt_dl_health_fw_recovery_done(bp);
12296 			bnxt_dl_health_fw_status_update(bp, true);
12297 		}
12298 		rtnl_unlock();
12299 		break;
12300 	}
12301 	return;
12302 
12303 fw_reset_abort_status:
12304 	if (bp->fw_health->status_reliable ||
12305 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
12306 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12307 
12308 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
12309 	}
12310 fw_reset_abort:
12311 	rtnl_lock();
12312 	bnxt_fw_reset_abort(bp, rc);
12313 	rtnl_unlock();
12314 }
12315 
12316 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
12317 {
12318 	int rc;
12319 	struct bnxt *bp = netdev_priv(dev);
12320 
12321 	SET_NETDEV_DEV(dev, &pdev->dev);
12322 
12323 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
12324 	rc = pci_enable_device(pdev);
12325 	if (rc) {
12326 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
12327 		goto init_err;
12328 	}
12329 
12330 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
12331 		dev_err(&pdev->dev,
12332 			"Cannot find PCI device base address, aborting\n");
12333 		rc = -ENODEV;
12334 		goto init_err_disable;
12335 	}
12336 
12337 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
12338 	if (rc) {
12339 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
12340 		goto init_err_disable;
12341 	}
12342 
12343 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
12344 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
12345 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
12346 		rc = -EIO;
12347 		goto init_err_release;
12348 	}
12349 
12350 	pci_set_master(pdev);
12351 
12352 	bp->dev = dev;
12353 	bp->pdev = pdev;
12354 
12355 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
12356 	 * determines the BAR size.
12357 	 */
12358 	bp->bar0 = pci_ioremap_bar(pdev, 0);
12359 	if (!bp->bar0) {
12360 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
12361 		rc = -ENOMEM;
12362 		goto init_err_release;
12363 	}
12364 
12365 	bp->bar2 = pci_ioremap_bar(pdev, 4);
12366 	if (!bp->bar2) {
12367 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
12368 		rc = -ENOMEM;
12369 		goto init_err_release;
12370 	}
12371 
12372 	pci_enable_pcie_error_reporting(pdev);
12373 
12374 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
12375 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
12376 
12377 	spin_lock_init(&bp->ntp_fltr_lock);
12378 #if BITS_PER_LONG == 32
12379 	spin_lock_init(&bp->db_lock);
12380 #endif
12381 
12382 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
12383 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
12384 
12385 	bnxt_init_dflt_coal(bp);
12386 
12387 	timer_setup(&bp->timer, bnxt_timer, 0);
12388 	bp->current_interval = BNXT_TIMER_INTERVAL;
12389 
12390 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
12391 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
12392 
12393 	clear_bit(BNXT_STATE_OPEN, &bp->state);
12394 	return 0;
12395 
12396 init_err_release:
12397 	bnxt_unmap_bars(bp, pdev);
12398 	pci_release_regions(pdev);
12399 
12400 init_err_disable:
12401 	pci_disable_device(pdev);
12402 
12403 init_err:
12404 	return rc;
12405 }
12406 
12407 /* rtnl_lock held */
12408 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
12409 {
12410 	struct sockaddr *addr = p;
12411 	struct bnxt *bp = netdev_priv(dev);
12412 	int rc = 0;
12413 
12414 	if (!is_valid_ether_addr(addr->sa_data))
12415 		return -EADDRNOTAVAIL;
12416 
12417 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
12418 		return 0;
12419 
12420 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
12421 	if (rc)
12422 		return rc;
12423 
12424 	eth_hw_addr_set(dev, addr->sa_data);
12425 	if (netif_running(dev)) {
12426 		bnxt_close_nic(bp, false, false);
12427 		rc = bnxt_open_nic(bp, false, false);
12428 	}
12429 
12430 	return rc;
12431 }
12432 
12433 /* rtnl_lock held */
12434 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
12435 {
12436 	struct bnxt *bp = netdev_priv(dev);
12437 
12438 	if (netif_running(dev))
12439 		bnxt_close_nic(bp, true, false);
12440 
12441 	dev->mtu = new_mtu;
12442 	bnxt_set_ring_params(bp);
12443 
12444 	if (netif_running(dev))
12445 		return bnxt_open_nic(bp, true, false);
12446 
12447 	return 0;
12448 }
12449 
12450 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
12451 {
12452 	struct bnxt *bp = netdev_priv(dev);
12453 	bool sh = false;
12454 	int rc;
12455 
12456 	if (tc > bp->max_tc) {
12457 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
12458 			   tc, bp->max_tc);
12459 		return -EINVAL;
12460 	}
12461 
12462 	if (netdev_get_num_tc(dev) == tc)
12463 		return 0;
12464 
12465 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
12466 		sh = true;
12467 
12468 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
12469 			      sh, tc, bp->tx_nr_rings_xdp);
12470 	if (rc)
12471 		return rc;
12472 
12473 	/* Needs to close the device and do hw resource re-allocations */
12474 	if (netif_running(bp->dev))
12475 		bnxt_close_nic(bp, true, false);
12476 
12477 	if (tc) {
12478 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
12479 		netdev_set_num_tc(dev, tc);
12480 	} else {
12481 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
12482 		netdev_reset_tc(dev);
12483 	}
12484 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
12485 	bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
12486 			       bp->tx_nr_rings + bp->rx_nr_rings;
12487 
12488 	if (netif_running(bp->dev))
12489 		return bnxt_open_nic(bp, true, false);
12490 
12491 	return 0;
12492 }
12493 
12494 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
12495 				  void *cb_priv)
12496 {
12497 	struct bnxt *bp = cb_priv;
12498 
12499 	if (!bnxt_tc_flower_enabled(bp) ||
12500 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
12501 		return -EOPNOTSUPP;
12502 
12503 	switch (type) {
12504 	case TC_SETUP_CLSFLOWER:
12505 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
12506 	default:
12507 		return -EOPNOTSUPP;
12508 	}
12509 }
12510 
12511 LIST_HEAD(bnxt_block_cb_list);
12512 
12513 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
12514 			 void *type_data)
12515 {
12516 	struct bnxt *bp = netdev_priv(dev);
12517 
12518 	switch (type) {
12519 	case TC_SETUP_BLOCK:
12520 		return flow_block_cb_setup_simple(type_data,
12521 						  &bnxt_block_cb_list,
12522 						  bnxt_setup_tc_block_cb,
12523 						  bp, bp, true);
12524 	case TC_SETUP_QDISC_MQPRIO: {
12525 		struct tc_mqprio_qopt *mqprio = type_data;
12526 
12527 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
12528 
12529 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
12530 	}
12531 	default:
12532 		return -EOPNOTSUPP;
12533 	}
12534 }
12535 
12536 #ifdef CONFIG_RFS_ACCEL
12537 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
12538 			    struct bnxt_ntuple_filter *f2)
12539 {
12540 	struct flow_keys *keys1 = &f1->fkeys;
12541 	struct flow_keys *keys2 = &f2->fkeys;
12542 
12543 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
12544 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
12545 		return false;
12546 
12547 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
12548 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
12549 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst)
12550 			return false;
12551 	} else {
12552 		if (memcmp(&keys1->addrs.v6addrs.src, &keys2->addrs.v6addrs.src,
12553 			   sizeof(keys1->addrs.v6addrs.src)) ||
12554 		    memcmp(&keys1->addrs.v6addrs.dst, &keys2->addrs.v6addrs.dst,
12555 			   sizeof(keys1->addrs.v6addrs.dst)))
12556 			return false;
12557 	}
12558 
12559 	if (keys1->ports.ports == keys2->ports.ports &&
12560 	    keys1->control.flags == keys2->control.flags &&
12561 	    ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
12562 	    ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
12563 		return true;
12564 
12565 	return false;
12566 }
12567 
12568 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
12569 			      u16 rxq_index, u32 flow_id)
12570 {
12571 	struct bnxt *bp = netdev_priv(dev);
12572 	struct bnxt_ntuple_filter *fltr, *new_fltr;
12573 	struct flow_keys *fkeys;
12574 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
12575 	int rc = 0, idx, bit_id, l2_idx = 0;
12576 	struct hlist_head *head;
12577 	u32 flags;
12578 
12579 	if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
12580 		struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
12581 		int off = 0, j;
12582 
12583 		netif_addr_lock_bh(dev);
12584 		for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
12585 			if (ether_addr_equal(eth->h_dest,
12586 					     vnic->uc_list + off)) {
12587 				l2_idx = j + 1;
12588 				break;
12589 			}
12590 		}
12591 		netif_addr_unlock_bh(dev);
12592 		if (!l2_idx)
12593 			return -EINVAL;
12594 	}
12595 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
12596 	if (!new_fltr)
12597 		return -ENOMEM;
12598 
12599 	fkeys = &new_fltr->fkeys;
12600 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
12601 		rc = -EPROTONOSUPPORT;
12602 		goto err_free;
12603 	}
12604 
12605 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
12606 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
12607 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
12608 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
12609 		rc = -EPROTONOSUPPORT;
12610 		goto err_free;
12611 	}
12612 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
12613 	    bp->hwrm_spec_code < 0x10601) {
12614 		rc = -EPROTONOSUPPORT;
12615 		goto err_free;
12616 	}
12617 	flags = fkeys->control.flags;
12618 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
12619 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
12620 		rc = -EPROTONOSUPPORT;
12621 		goto err_free;
12622 	}
12623 
12624 	memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
12625 	memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
12626 
12627 	idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
12628 	head = &bp->ntp_fltr_hash_tbl[idx];
12629 	rcu_read_lock();
12630 	hlist_for_each_entry_rcu(fltr, head, hash) {
12631 		if (bnxt_fltr_match(fltr, new_fltr)) {
12632 			rcu_read_unlock();
12633 			rc = 0;
12634 			goto err_free;
12635 		}
12636 	}
12637 	rcu_read_unlock();
12638 
12639 	spin_lock_bh(&bp->ntp_fltr_lock);
12640 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
12641 					 BNXT_NTP_FLTR_MAX_FLTR, 0);
12642 	if (bit_id < 0) {
12643 		spin_unlock_bh(&bp->ntp_fltr_lock);
12644 		rc = -ENOMEM;
12645 		goto err_free;
12646 	}
12647 
12648 	new_fltr->sw_id = (u16)bit_id;
12649 	new_fltr->flow_id = flow_id;
12650 	new_fltr->l2_fltr_idx = l2_idx;
12651 	new_fltr->rxq = rxq_index;
12652 	hlist_add_head_rcu(&new_fltr->hash, head);
12653 	bp->ntp_fltr_count++;
12654 	spin_unlock_bh(&bp->ntp_fltr_lock);
12655 
12656 	set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
12657 	bnxt_queue_sp_work(bp);
12658 
12659 	return new_fltr->sw_id;
12660 
12661 err_free:
12662 	kfree(new_fltr);
12663 	return rc;
12664 }
12665 
12666 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
12667 {
12668 	int i;
12669 
12670 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
12671 		struct hlist_head *head;
12672 		struct hlist_node *tmp;
12673 		struct bnxt_ntuple_filter *fltr;
12674 		int rc;
12675 
12676 		head = &bp->ntp_fltr_hash_tbl[i];
12677 		hlist_for_each_entry_safe(fltr, tmp, head, hash) {
12678 			bool del = false;
12679 
12680 			if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
12681 				if (rps_may_expire_flow(bp->dev, fltr->rxq,
12682 							fltr->flow_id,
12683 							fltr->sw_id)) {
12684 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
12685 									 fltr);
12686 					del = true;
12687 				}
12688 			} else {
12689 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
12690 								       fltr);
12691 				if (rc)
12692 					del = true;
12693 				else
12694 					set_bit(BNXT_FLTR_VALID, &fltr->state);
12695 			}
12696 
12697 			if (del) {
12698 				spin_lock_bh(&bp->ntp_fltr_lock);
12699 				hlist_del_rcu(&fltr->hash);
12700 				bp->ntp_fltr_count--;
12701 				spin_unlock_bh(&bp->ntp_fltr_lock);
12702 				synchronize_rcu();
12703 				clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
12704 				kfree(fltr);
12705 			}
12706 		}
12707 	}
12708 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
12709 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
12710 }
12711 
12712 #else
12713 
12714 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
12715 {
12716 }
12717 
12718 #endif /* CONFIG_RFS_ACCEL */
12719 
12720 static int bnxt_udp_tunnel_sync(struct net_device *netdev, unsigned int table)
12721 {
12722 	struct bnxt *bp = netdev_priv(netdev);
12723 	struct udp_tunnel_info ti;
12724 	unsigned int cmd;
12725 
12726 	udp_tunnel_nic_get_port(netdev, table, 0, &ti);
12727 	if (ti.type == UDP_TUNNEL_TYPE_VXLAN)
12728 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
12729 	else
12730 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
12731 
12732 	if (ti.port)
12733 		return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti.port, cmd);
12734 
12735 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
12736 }
12737 
12738 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
12739 	.sync_table	= bnxt_udp_tunnel_sync,
12740 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
12741 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
12742 	.tables		= {
12743 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
12744 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
12745 	},
12746 };
12747 
12748 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
12749 			       struct net_device *dev, u32 filter_mask,
12750 			       int nlflags)
12751 {
12752 	struct bnxt *bp = netdev_priv(dev);
12753 
12754 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
12755 				       nlflags, filter_mask, NULL);
12756 }
12757 
12758 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
12759 			       u16 flags, struct netlink_ext_ack *extack)
12760 {
12761 	struct bnxt *bp = netdev_priv(dev);
12762 	struct nlattr *attr, *br_spec;
12763 	int rem, rc = 0;
12764 
12765 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
12766 		return -EOPNOTSUPP;
12767 
12768 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
12769 	if (!br_spec)
12770 		return -EINVAL;
12771 
12772 	nla_for_each_nested(attr, br_spec, rem) {
12773 		u16 mode;
12774 
12775 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
12776 			continue;
12777 
12778 		if (nla_len(attr) < sizeof(mode))
12779 			return -EINVAL;
12780 
12781 		mode = nla_get_u16(attr);
12782 		if (mode == bp->br_mode)
12783 			break;
12784 
12785 		rc = bnxt_hwrm_set_br_mode(bp, mode);
12786 		if (!rc)
12787 			bp->br_mode = mode;
12788 		break;
12789 	}
12790 	return rc;
12791 }
12792 
12793 int bnxt_get_port_parent_id(struct net_device *dev,
12794 			    struct netdev_phys_item_id *ppid)
12795 {
12796 	struct bnxt *bp = netdev_priv(dev);
12797 
12798 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
12799 		return -EOPNOTSUPP;
12800 
12801 	/* The PF and it's VF-reps only support the switchdev framework */
12802 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
12803 		return -EOPNOTSUPP;
12804 
12805 	ppid->id_len = sizeof(bp->dsn);
12806 	memcpy(ppid->id, bp->dsn, ppid->id_len);
12807 
12808 	return 0;
12809 }
12810 
12811 static struct devlink_port *bnxt_get_devlink_port(struct net_device *dev)
12812 {
12813 	struct bnxt *bp = netdev_priv(dev);
12814 
12815 	return &bp->dl_port;
12816 }
12817 
12818 static const struct net_device_ops bnxt_netdev_ops = {
12819 	.ndo_open		= bnxt_open,
12820 	.ndo_start_xmit		= bnxt_start_xmit,
12821 	.ndo_stop		= bnxt_close,
12822 	.ndo_get_stats64	= bnxt_get_stats64,
12823 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
12824 	.ndo_eth_ioctl		= bnxt_ioctl,
12825 	.ndo_validate_addr	= eth_validate_addr,
12826 	.ndo_set_mac_address	= bnxt_change_mac_addr,
12827 	.ndo_change_mtu		= bnxt_change_mtu,
12828 	.ndo_fix_features	= bnxt_fix_features,
12829 	.ndo_set_features	= bnxt_set_features,
12830 	.ndo_features_check	= bnxt_features_check,
12831 	.ndo_tx_timeout		= bnxt_tx_timeout,
12832 #ifdef CONFIG_BNXT_SRIOV
12833 	.ndo_get_vf_config	= bnxt_get_vf_config,
12834 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
12835 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
12836 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
12837 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
12838 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
12839 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
12840 #endif
12841 	.ndo_setup_tc           = bnxt_setup_tc,
12842 #ifdef CONFIG_RFS_ACCEL
12843 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
12844 #endif
12845 	.ndo_bpf		= bnxt_xdp,
12846 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
12847 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
12848 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
12849 	.ndo_get_devlink_port	= bnxt_get_devlink_port,
12850 };
12851 
12852 static void bnxt_remove_one(struct pci_dev *pdev)
12853 {
12854 	struct net_device *dev = pci_get_drvdata(pdev);
12855 	struct bnxt *bp = netdev_priv(dev);
12856 
12857 	if (BNXT_PF(bp))
12858 		bnxt_sriov_disable(bp);
12859 
12860 	if (BNXT_PF(bp))
12861 		devlink_port_type_clear(&bp->dl_port);
12862 
12863 	bnxt_ptp_clear(bp);
12864 	pci_disable_pcie_error_reporting(pdev);
12865 	unregister_netdev(dev);
12866 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
12867 	/* Flush any pending tasks */
12868 	cancel_work_sync(&bp->sp_task);
12869 	cancel_delayed_work_sync(&bp->fw_reset_task);
12870 	bp->sp_event = 0;
12871 
12872 	bnxt_dl_fw_reporters_destroy(bp, true);
12873 	bnxt_dl_unregister(bp);
12874 	bnxt_shutdown_tc(bp);
12875 
12876 	bnxt_clear_int_mode(bp);
12877 	bnxt_hwrm_func_drv_unrgtr(bp);
12878 	bnxt_free_hwrm_resources(bp);
12879 	bnxt_ethtool_free(bp);
12880 	bnxt_dcb_free(bp);
12881 	kfree(bp->edev);
12882 	bp->edev = NULL;
12883 	kfree(bp->ptp_cfg);
12884 	bp->ptp_cfg = NULL;
12885 	kfree(bp->fw_health);
12886 	bp->fw_health = NULL;
12887 	bnxt_cleanup_pci(bp);
12888 	bnxt_free_ctx_mem(bp);
12889 	kfree(bp->ctx);
12890 	bp->ctx = NULL;
12891 	kfree(bp->rss_indir_tbl);
12892 	bp->rss_indir_tbl = NULL;
12893 	bnxt_free_port_stats(bp);
12894 	free_netdev(dev);
12895 }
12896 
12897 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
12898 {
12899 	int rc = 0;
12900 	struct bnxt_link_info *link_info = &bp->link_info;
12901 
12902 	bp->phy_flags = 0;
12903 	rc = bnxt_hwrm_phy_qcaps(bp);
12904 	if (rc) {
12905 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
12906 			   rc);
12907 		return rc;
12908 	}
12909 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
12910 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
12911 	else
12912 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
12913 	if (!fw_dflt)
12914 		return 0;
12915 
12916 	mutex_lock(&bp->link_lock);
12917 	rc = bnxt_update_link(bp, false);
12918 	if (rc) {
12919 		mutex_unlock(&bp->link_lock);
12920 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
12921 			   rc);
12922 		return rc;
12923 	}
12924 
12925 	/* Older firmware does not have supported_auto_speeds, so assume
12926 	 * that all supported speeds can be autonegotiated.
12927 	 */
12928 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
12929 		link_info->support_auto_speeds = link_info->support_speeds;
12930 
12931 	bnxt_init_ethtool_link_settings(bp);
12932 	mutex_unlock(&bp->link_lock);
12933 	return 0;
12934 }
12935 
12936 static int bnxt_get_max_irq(struct pci_dev *pdev)
12937 {
12938 	u16 ctrl;
12939 
12940 	if (!pdev->msix_cap)
12941 		return 1;
12942 
12943 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
12944 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
12945 }
12946 
12947 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
12948 				int *max_cp)
12949 {
12950 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12951 	int max_ring_grps = 0, max_irq;
12952 
12953 	*max_tx = hw_resc->max_tx_rings;
12954 	*max_rx = hw_resc->max_rx_rings;
12955 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
12956 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
12957 			bnxt_get_ulp_msix_num(bp),
12958 			hw_resc->max_stat_ctxs - bnxt_get_ulp_stat_ctxs(bp));
12959 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
12960 		*max_cp = min_t(int, *max_cp, max_irq);
12961 	max_ring_grps = hw_resc->max_hw_ring_grps;
12962 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
12963 		*max_cp -= 1;
12964 		*max_rx -= 2;
12965 	}
12966 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
12967 		*max_rx >>= 1;
12968 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
12969 		bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
12970 		/* On P5 chips, max_cp output param should be available NQs */
12971 		*max_cp = max_irq;
12972 	}
12973 	*max_rx = min_t(int, *max_rx, max_ring_grps);
12974 }
12975 
12976 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
12977 {
12978 	int rx, tx, cp;
12979 
12980 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
12981 	*max_rx = rx;
12982 	*max_tx = tx;
12983 	if (!rx || !tx || !cp)
12984 		return -ENOMEM;
12985 
12986 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
12987 }
12988 
12989 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
12990 			       bool shared)
12991 {
12992 	int rc;
12993 
12994 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
12995 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
12996 		/* Not enough rings, try disabling agg rings. */
12997 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
12998 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
12999 		if (rc) {
13000 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
13001 			bp->flags |= BNXT_FLAG_AGG_RINGS;
13002 			return rc;
13003 		}
13004 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
13005 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13006 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13007 		bnxt_set_ring_params(bp);
13008 	}
13009 
13010 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
13011 		int max_cp, max_stat, max_irq;
13012 
13013 		/* Reserve minimum resources for RoCE */
13014 		max_cp = bnxt_get_max_func_cp_rings(bp);
13015 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
13016 		max_irq = bnxt_get_max_func_irqs(bp);
13017 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
13018 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
13019 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
13020 			return 0;
13021 
13022 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
13023 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
13024 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
13025 		max_cp = min_t(int, max_cp, max_irq);
13026 		max_cp = min_t(int, max_cp, max_stat);
13027 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
13028 		if (rc)
13029 			rc = 0;
13030 	}
13031 	return rc;
13032 }
13033 
13034 /* In initial default shared ring setting, each shared ring must have a
13035  * RX/TX ring pair.
13036  */
13037 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
13038 {
13039 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
13040 	bp->rx_nr_rings = bp->cp_nr_rings;
13041 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
13042 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
13043 }
13044 
13045 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
13046 {
13047 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
13048 
13049 	if (!bnxt_can_reserve_rings(bp))
13050 		return 0;
13051 
13052 	if (sh)
13053 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
13054 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
13055 	/* Reduce default rings on multi-port cards so that total default
13056 	 * rings do not exceed CPU count.
13057 	 */
13058 	if (bp->port_count > 1) {
13059 		int max_rings =
13060 			max_t(int, num_online_cpus() / bp->port_count, 1);
13061 
13062 		dflt_rings = min_t(int, dflt_rings, max_rings);
13063 	}
13064 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
13065 	if (rc)
13066 		return rc;
13067 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
13068 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
13069 	if (sh)
13070 		bnxt_trim_dflt_sh_rings(bp);
13071 	else
13072 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
13073 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
13074 
13075 	rc = __bnxt_reserve_rings(bp);
13076 	if (rc)
13077 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
13078 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
13079 	if (sh)
13080 		bnxt_trim_dflt_sh_rings(bp);
13081 
13082 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
13083 	if (bnxt_need_reserve_rings(bp)) {
13084 		rc = __bnxt_reserve_rings(bp);
13085 		if (rc)
13086 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
13087 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
13088 	}
13089 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
13090 		bp->rx_nr_rings++;
13091 		bp->cp_nr_rings++;
13092 	}
13093 	if (rc) {
13094 		bp->tx_nr_rings = 0;
13095 		bp->rx_nr_rings = 0;
13096 	}
13097 	return rc;
13098 }
13099 
13100 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
13101 {
13102 	int rc;
13103 
13104 	if (bp->tx_nr_rings)
13105 		return 0;
13106 
13107 	bnxt_ulp_irq_stop(bp);
13108 	bnxt_clear_int_mode(bp);
13109 	rc = bnxt_set_dflt_rings(bp, true);
13110 	if (rc) {
13111 		netdev_err(bp->dev, "Not enough rings available.\n");
13112 		goto init_dflt_ring_err;
13113 	}
13114 	rc = bnxt_init_int_mode(bp);
13115 	if (rc)
13116 		goto init_dflt_ring_err;
13117 
13118 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
13119 	if (bnxt_rfs_supported(bp) && bnxt_rfs_capable(bp)) {
13120 		bp->flags |= BNXT_FLAG_RFS;
13121 		bp->dev->features |= NETIF_F_NTUPLE;
13122 	}
13123 init_dflt_ring_err:
13124 	bnxt_ulp_irq_restart(bp, rc);
13125 	return rc;
13126 }
13127 
13128 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
13129 {
13130 	int rc;
13131 
13132 	ASSERT_RTNL();
13133 	bnxt_hwrm_func_qcaps(bp);
13134 
13135 	if (netif_running(bp->dev))
13136 		__bnxt_close_nic(bp, true, false);
13137 
13138 	bnxt_ulp_irq_stop(bp);
13139 	bnxt_clear_int_mode(bp);
13140 	rc = bnxt_init_int_mode(bp);
13141 	bnxt_ulp_irq_restart(bp, rc);
13142 
13143 	if (netif_running(bp->dev)) {
13144 		if (rc)
13145 			dev_close(bp->dev);
13146 		else
13147 			rc = bnxt_open_nic(bp, true, false);
13148 	}
13149 
13150 	return rc;
13151 }
13152 
13153 static int bnxt_init_mac_addr(struct bnxt *bp)
13154 {
13155 	int rc = 0;
13156 
13157 	if (BNXT_PF(bp)) {
13158 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
13159 	} else {
13160 #ifdef CONFIG_BNXT_SRIOV
13161 		struct bnxt_vf_info *vf = &bp->vf;
13162 		bool strict_approval = true;
13163 
13164 		if (is_valid_ether_addr(vf->mac_addr)) {
13165 			/* overwrite netdev dev_addr with admin VF MAC */
13166 			eth_hw_addr_set(bp->dev, vf->mac_addr);
13167 			/* Older PF driver or firmware may not approve this
13168 			 * correctly.
13169 			 */
13170 			strict_approval = false;
13171 		} else {
13172 			eth_hw_addr_random(bp->dev);
13173 		}
13174 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
13175 #endif
13176 	}
13177 	return rc;
13178 }
13179 
13180 static void bnxt_vpd_read_info(struct bnxt *bp)
13181 {
13182 	struct pci_dev *pdev = bp->pdev;
13183 	unsigned int vpd_size, kw_len;
13184 	int pos, size;
13185 	u8 *vpd_data;
13186 
13187 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
13188 	if (IS_ERR(vpd_data)) {
13189 		pci_warn(pdev, "Unable to read VPD\n");
13190 		return;
13191 	}
13192 
13193 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
13194 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
13195 	if (pos < 0)
13196 		goto read_sn;
13197 
13198 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
13199 	memcpy(bp->board_partno, &vpd_data[pos], size);
13200 
13201 read_sn:
13202 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
13203 					   PCI_VPD_RO_KEYWORD_SERIALNO,
13204 					   &kw_len);
13205 	if (pos < 0)
13206 		goto exit;
13207 
13208 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
13209 	memcpy(bp->board_serialno, &vpd_data[pos], size);
13210 exit:
13211 	kfree(vpd_data);
13212 }
13213 
13214 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
13215 {
13216 	struct pci_dev *pdev = bp->pdev;
13217 	u64 qword;
13218 
13219 	qword = pci_get_dsn(pdev);
13220 	if (!qword) {
13221 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
13222 		return -EOPNOTSUPP;
13223 	}
13224 
13225 	put_unaligned_le64(qword, dsn);
13226 
13227 	bp->flags |= BNXT_FLAG_DSN_VALID;
13228 	return 0;
13229 }
13230 
13231 static int bnxt_map_db_bar(struct bnxt *bp)
13232 {
13233 	if (!bp->db_size)
13234 		return -ENODEV;
13235 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
13236 	if (!bp->bar1)
13237 		return -ENOMEM;
13238 	return 0;
13239 }
13240 
13241 void bnxt_print_device_info(struct bnxt *bp)
13242 {
13243 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
13244 		    board_info[bp->board_idx].name,
13245 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
13246 
13247 	pcie_print_link_status(bp->pdev);
13248 }
13249 
13250 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
13251 {
13252 	struct net_device *dev;
13253 	struct bnxt *bp;
13254 	int rc, max_irqs;
13255 
13256 	if (pci_is_bridge(pdev))
13257 		return -ENODEV;
13258 
13259 	/* Clear any pending DMA transactions from crash kernel
13260 	 * while loading driver in capture kernel.
13261 	 */
13262 	if (is_kdump_kernel()) {
13263 		pci_clear_master(pdev);
13264 		pcie_flr(pdev);
13265 	}
13266 
13267 	max_irqs = bnxt_get_max_irq(pdev);
13268 	dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
13269 	if (!dev)
13270 		return -ENOMEM;
13271 
13272 	bp = netdev_priv(dev);
13273 	bp->board_idx = ent->driver_data;
13274 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
13275 	bnxt_set_max_func_irqs(bp, max_irqs);
13276 
13277 	if (bnxt_vf_pciid(bp->board_idx))
13278 		bp->flags |= BNXT_FLAG_VF;
13279 
13280 	if (pdev->msix_cap)
13281 		bp->flags |= BNXT_FLAG_MSIX_CAP;
13282 
13283 	rc = bnxt_init_board(pdev, dev);
13284 	if (rc < 0)
13285 		goto init_err_free;
13286 
13287 	dev->netdev_ops = &bnxt_netdev_ops;
13288 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
13289 	dev->ethtool_ops = &bnxt_ethtool_ops;
13290 	pci_set_drvdata(pdev, dev);
13291 
13292 	rc = bnxt_alloc_hwrm_resources(bp);
13293 	if (rc)
13294 		goto init_err_pci_clean;
13295 
13296 	mutex_init(&bp->hwrm_cmd_lock);
13297 	mutex_init(&bp->link_lock);
13298 
13299 	rc = bnxt_fw_init_one_p1(bp);
13300 	if (rc)
13301 		goto init_err_pci_clean;
13302 
13303 	if (BNXT_PF(bp))
13304 		bnxt_vpd_read_info(bp);
13305 
13306 	if (BNXT_CHIP_P5(bp)) {
13307 		bp->flags |= BNXT_FLAG_CHIP_P5;
13308 		if (BNXT_CHIP_SR2(bp))
13309 			bp->flags |= BNXT_FLAG_CHIP_SR2;
13310 	}
13311 
13312 	rc = bnxt_alloc_rss_indir_tbl(bp);
13313 	if (rc)
13314 		goto init_err_pci_clean;
13315 
13316 	rc = bnxt_fw_init_one_p2(bp);
13317 	if (rc)
13318 		goto init_err_pci_clean;
13319 
13320 	rc = bnxt_map_db_bar(bp);
13321 	if (rc) {
13322 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
13323 			rc);
13324 		goto init_err_pci_clean;
13325 	}
13326 
13327 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
13328 			   NETIF_F_TSO | NETIF_F_TSO6 |
13329 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
13330 			   NETIF_F_GSO_IPXIP4 |
13331 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
13332 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
13333 			   NETIF_F_RXCSUM | NETIF_F_GRO;
13334 
13335 	if (BNXT_SUPPORTS_TPA(bp))
13336 		dev->hw_features |= NETIF_F_LRO;
13337 
13338 	dev->hw_enc_features =
13339 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
13340 			NETIF_F_TSO | NETIF_F_TSO6 |
13341 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
13342 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
13343 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
13344 	dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
13345 
13346 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
13347 				    NETIF_F_GSO_GRE_CSUM;
13348 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
13349 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
13350 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13351 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
13352 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
13353 	if (BNXT_SUPPORTS_TPA(bp))
13354 		dev->hw_features |= NETIF_F_GRO_HW;
13355 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
13356 	if (dev->features & NETIF_F_GRO_HW)
13357 		dev->features &= ~NETIF_F_LRO;
13358 	dev->priv_flags |= IFF_UNICAST_FLT;
13359 
13360 #ifdef CONFIG_BNXT_SRIOV
13361 	init_waitqueue_head(&bp->sriov_cfg_wait);
13362 	mutex_init(&bp->sriov_lock);
13363 #endif
13364 	if (BNXT_SUPPORTS_TPA(bp)) {
13365 		bp->gro_func = bnxt_gro_func_5730x;
13366 		if (BNXT_CHIP_P4(bp))
13367 			bp->gro_func = bnxt_gro_func_5731x;
13368 		else if (BNXT_CHIP_P5(bp))
13369 			bp->gro_func = bnxt_gro_func_5750x;
13370 	}
13371 	if (!BNXT_CHIP_P4_PLUS(bp))
13372 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
13373 
13374 	rc = bnxt_init_mac_addr(bp);
13375 	if (rc) {
13376 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
13377 		rc = -EADDRNOTAVAIL;
13378 		goto init_err_pci_clean;
13379 	}
13380 
13381 	if (BNXT_PF(bp)) {
13382 		/* Read the adapter's DSN to use as the eswitch switch_id */
13383 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
13384 	}
13385 
13386 	/* MTU range: 60 - FW defined max */
13387 	dev->min_mtu = ETH_ZLEN;
13388 	dev->max_mtu = bp->max_mtu;
13389 
13390 	rc = bnxt_probe_phy(bp, true);
13391 	if (rc)
13392 		goto init_err_pci_clean;
13393 
13394 	bnxt_set_rx_skb_mode(bp, false);
13395 	bnxt_set_tpa_flags(bp);
13396 	bnxt_set_ring_params(bp);
13397 	rc = bnxt_set_dflt_rings(bp, true);
13398 	if (rc) {
13399 		netdev_err(bp->dev, "Not enough rings available.\n");
13400 		rc = -ENOMEM;
13401 		goto init_err_pci_clean;
13402 	}
13403 
13404 	bnxt_fw_init_one_p3(bp);
13405 
13406 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13407 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
13408 
13409 	rc = bnxt_init_int_mode(bp);
13410 	if (rc)
13411 		goto init_err_pci_clean;
13412 
13413 	/* No TC has been set yet and rings may have been trimmed due to
13414 	 * limited MSIX, so we re-initialize the TX rings per TC.
13415 	 */
13416 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
13417 
13418 	if (BNXT_PF(bp)) {
13419 		if (!bnxt_pf_wq) {
13420 			bnxt_pf_wq =
13421 				create_singlethread_workqueue("bnxt_pf_wq");
13422 			if (!bnxt_pf_wq) {
13423 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
13424 				rc = -ENOMEM;
13425 				goto init_err_pci_clean;
13426 			}
13427 		}
13428 		rc = bnxt_init_tc(bp);
13429 		if (rc)
13430 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
13431 				   rc);
13432 	}
13433 
13434 	bnxt_inv_fw_health_reg(bp);
13435 	rc = bnxt_dl_register(bp);
13436 	if (rc)
13437 		goto init_err_dl;
13438 
13439 	rc = register_netdev(dev);
13440 	if (rc)
13441 		goto init_err_cleanup;
13442 
13443 	if (BNXT_PF(bp))
13444 		devlink_port_type_eth_set(&bp->dl_port, bp->dev);
13445 	bnxt_dl_fw_reporters_create(bp);
13446 
13447 	bnxt_print_device_info(bp);
13448 
13449 	pci_save_state(pdev);
13450 	return 0;
13451 
13452 init_err_cleanup:
13453 	bnxt_dl_unregister(bp);
13454 init_err_dl:
13455 	bnxt_shutdown_tc(bp);
13456 	bnxt_clear_int_mode(bp);
13457 
13458 init_err_pci_clean:
13459 	bnxt_hwrm_func_drv_unrgtr(bp);
13460 	bnxt_free_hwrm_resources(bp);
13461 	bnxt_ethtool_free(bp);
13462 	bnxt_ptp_clear(bp);
13463 	kfree(bp->ptp_cfg);
13464 	bp->ptp_cfg = NULL;
13465 	kfree(bp->fw_health);
13466 	bp->fw_health = NULL;
13467 	bnxt_cleanup_pci(bp);
13468 	bnxt_free_ctx_mem(bp);
13469 	kfree(bp->ctx);
13470 	bp->ctx = NULL;
13471 	kfree(bp->rss_indir_tbl);
13472 	bp->rss_indir_tbl = NULL;
13473 
13474 init_err_free:
13475 	free_netdev(dev);
13476 	return rc;
13477 }
13478 
13479 static void bnxt_shutdown(struct pci_dev *pdev)
13480 {
13481 	struct net_device *dev = pci_get_drvdata(pdev);
13482 	struct bnxt *bp;
13483 
13484 	if (!dev)
13485 		return;
13486 
13487 	rtnl_lock();
13488 	bp = netdev_priv(dev);
13489 	if (!bp)
13490 		goto shutdown_exit;
13491 
13492 	if (netif_running(dev))
13493 		dev_close(dev);
13494 
13495 	bnxt_ulp_shutdown(bp);
13496 	bnxt_clear_int_mode(bp);
13497 	pci_disable_device(pdev);
13498 
13499 	if (system_state == SYSTEM_POWER_OFF) {
13500 		pci_wake_from_d3(pdev, bp->wol);
13501 		pci_set_power_state(pdev, PCI_D3hot);
13502 	}
13503 
13504 shutdown_exit:
13505 	rtnl_unlock();
13506 }
13507 
13508 #ifdef CONFIG_PM_SLEEP
13509 static int bnxt_suspend(struct device *device)
13510 {
13511 	struct net_device *dev = dev_get_drvdata(device);
13512 	struct bnxt *bp = netdev_priv(dev);
13513 	int rc = 0;
13514 
13515 	rtnl_lock();
13516 	bnxt_ulp_stop(bp);
13517 	if (netif_running(dev)) {
13518 		netif_device_detach(dev);
13519 		rc = bnxt_close(dev);
13520 	}
13521 	bnxt_hwrm_func_drv_unrgtr(bp);
13522 	pci_disable_device(bp->pdev);
13523 	bnxt_free_ctx_mem(bp);
13524 	kfree(bp->ctx);
13525 	bp->ctx = NULL;
13526 	rtnl_unlock();
13527 	return rc;
13528 }
13529 
13530 static int bnxt_resume(struct device *device)
13531 {
13532 	struct net_device *dev = dev_get_drvdata(device);
13533 	struct bnxt *bp = netdev_priv(dev);
13534 	int rc = 0;
13535 
13536 	rtnl_lock();
13537 	rc = pci_enable_device(bp->pdev);
13538 	if (rc) {
13539 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
13540 			   rc);
13541 		goto resume_exit;
13542 	}
13543 	pci_set_master(bp->pdev);
13544 	if (bnxt_hwrm_ver_get(bp)) {
13545 		rc = -ENODEV;
13546 		goto resume_exit;
13547 	}
13548 	rc = bnxt_hwrm_func_reset(bp);
13549 	if (rc) {
13550 		rc = -EBUSY;
13551 		goto resume_exit;
13552 	}
13553 
13554 	rc = bnxt_hwrm_func_qcaps(bp);
13555 	if (rc)
13556 		goto resume_exit;
13557 
13558 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
13559 		rc = -ENODEV;
13560 		goto resume_exit;
13561 	}
13562 
13563 	bnxt_get_wol_settings(bp);
13564 	if (netif_running(dev)) {
13565 		rc = bnxt_open(dev);
13566 		if (!rc)
13567 			netif_device_attach(dev);
13568 	}
13569 
13570 resume_exit:
13571 	bnxt_ulp_start(bp, rc);
13572 	if (!rc)
13573 		bnxt_reenable_sriov(bp);
13574 	rtnl_unlock();
13575 	return rc;
13576 }
13577 
13578 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
13579 #define BNXT_PM_OPS (&bnxt_pm_ops)
13580 
13581 #else
13582 
13583 #define BNXT_PM_OPS NULL
13584 
13585 #endif /* CONFIG_PM_SLEEP */
13586 
13587 /**
13588  * bnxt_io_error_detected - called when PCI error is detected
13589  * @pdev: Pointer to PCI device
13590  * @state: The current pci connection state
13591  *
13592  * This function is called after a PCI bus error affecting
13593  * this device has been detected.
13594  */
13595 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
13596 					       pci_channel_state_t state)
13597 {
13598 	struct net_device *netdev = pci_get_drvdata(pdev);
13599 	struct bnxt *bp = netdev_priv(netdev);
13600 
13601 	netdev_info(netdev, "PCI I/O error detected\n");
13602 
13603 	rtnl_lock();
13604 	netif_device_detach(netdev);
13605 
13606 	bnxt_ulp_stop(bp);
13607 
13608 	if (state == pci_channel_io_perm_failure) {
13609 		rtnl_unlock();
13610 		return PCI_ERS_RESULT_DISCONNECT;
13611 	}
13612 
13613 	if (state == pci_channel_io_frozen)
13614 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
13615 
13616 	if (netif_running(netdev))
13617 		bnxt_close(netdev);
13618 
13619 	if (pci_is_enabled(pdev))
13620 		pci_disable_device(pdev);
13621 	bnxt_free_ctx_mem(bp);
13622 	kfree(bp->ctx);
13623 	bp->ctx = NULL;
13624 	rtnl_unlock();
13625 
13626 	/* Request a slot slot reset. */
13627 	return PCI_ERS_RESULT_NEED_RESET;
13628 }
13629 
13630 /**
13631  * bnxt_io_slot_reset - called after the pci bus has been reset.
13632  * @pdev: Pointer to PCI device
13633  *
13634  * Restart the card from scratch, as if from a cold-boot.
13635  * At this point, the card has exprienced a hard reset,
13636  * followed by fixups by BIOS, and has its config space
13637  * set up identically to what it was at cold boot.
13638  */
13639 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
13640 {
13641 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
13642 	struct net_device *netdev = pci_get_drvdata(pdev);
13643 	struct bnxt *bp = netdev_priv(netdev);
13644 	int err = 0, off;
13645 
13646 	netdev_info(bp->dev, "PCI Slot Reset\n");
13647 
13648 	rtnl_lock();
13649 
13650 	if (pci_enable_device(pdev)) {
13651 		dev_err(&pdev->dev,
13652 			"Cannot re-enable PCI device after reset.\n");
13653 	} else {
13654 		pci_set_master(pdev);
13655 		/* Upon fatal error, our device internal logic that latches to
13656 		 * BAR value is getting reset and will restore only upon
13657 		 * rewritting the BARs.
13658 		 *
13659 		 * As pci_restore_state() does not re-write the BARs if the
13660 		 * value is same as saved value earlier, driver needs to
13661 		 * write the BARs to 0 to force restore, in case of fatal error.
13662 		 */
13663 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
13664 				       &bp->state)) {
13665 			for (off = PCI_BASE_ADDRESS_0;
13666 			     off <= PCI_BASE_ADDRESS_5; off += 4)
13667 				pci_write_config_dword(bp->pdev, off, 0);
13668 		}
13669 		pci_restore_state(pdev);
13670 		pci_save_state(pdev);
13671 
13672 		err = bnxt_hwrm_func_reset(bp);
13673 		if (!err)
13674 			result = PCI_ERS_RESULT_RECOVERED;
13675 	}
13676 
13677 	rtnl_unlock();
13678 
13679 	return result;
13680 }
13681 
13682 /**
13683  * bnxt_io_resume - called when traffic can start flowing again.
13684  * @pdev: Pointer to PCI device
13685  *
13686  * This callback is called when the error recovery driver tells
13687  * us that its OK to resume normal operation.
13688  */
13689 static void bnxt_io_resume(struct pci_dev *pdev)
13690 {
13691 	struct net_device *netdev = pci_get_drvdata(pdev);
13692 	struct bnxt *bp = netdev_priv(netdev);
13693 	int err;
13694 
13695 	netdev_info(bp->dev, "PCI Slot Resume\n");
13696 	rtnl_lock();
13697 
13698 	err = bnxt_hwrm_func_qcaps(bp);
13699 	if (!err && netif_running(netdev))
13700 		err = bnxt_open(netdev);
13701 
13702 	bnxt_ulp_start(bp, err);
13703 	if (!err) {
13704 		bnxt_reenable_sriov(bp);
13705 		netif_device_attach(netdev);
13706 	}
13707 
13708 	rtnl_unlock();
13709 }
13710 
13711 static const struct pci_error_handlers bnxt_err_handler = {
13712 	.error_detected	= bnxt_io_error_detected,
13713 	.slot_reset	= bnxt_io_slot_reset,
13714 	.resume		= bnxt_io_resume
13715 };
13716 
13717 static struct pci_driver bnxt_pci_driver = {
13718 	.name		= DRV_MODULE_NAME,
13719 	.id_table	= bnxt_pci_tbl,
13720 	.probe		= bnxt_init_one,
13721 	.remove		= bnxt_remove_one,
13722 	.shutdown	= bnxt_shutdown,
13723 	.driver.pm	= BNXT_PM_OPS,
13724 	.err_handler	= &bnxt_err_handler,
13725 #if defined(CONFIG_BNXT_SRIOV)
13726 	.sriov_configure = bnxt_sriov_configure,
13727 #endif
13728 };
13729 
13730 static int __init bnxt_init(void)
13731 {
13732 	bnxt_debug_init();
13733 	return pci_register_driver(&bnxt_pci_driver);
13734 }
13735 
13736 static void __exit bnxt_exit(void)
13737 {
13738 	pci_unregister_driver(&bnxt_pci_driver);
13739 	if (bnxt_pf_wq)
13740 		destroy_workqueue(bnxt_pf_wq);
13741 	bnxt_debug_exit();
13742 }
13743 
13744 module_init(bnxt_init);
13745 module_exit(bnxt_exit);
13746