xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision 2bd87951de659df3381ce083342aaf5b1ea24689)
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/bitmap.h>
52 #include <linux/cpu_rmap.h>
53 #include <linux/cpumask.h>
54 #include <net/pkt_cls.h>
55 #include <net/page_pool/helpers.h>
56 #include <linux/align.h>
57 #include <net/netdev_queues.h>
58 
59 #include "bnxt_hsi.h"
60 #include "bnxt.h"
61 #include "bnxt_hwrm.h"
62 #include "bnxt_ulp.h"
63 #include "bnxt_sriov.h"
64 #include "bnxt_ethtool.h"
65 #include "bnxt_dcb.h"
66 #include "bnxt_xdp.h"
67 #include "bnxt_ptp.h"
68 #include "bnxt_vfr.h"
69 #include "bnxt_tc.h"
70 #include "bnxt_devlink.h"
71 #include "bnxt_debugfs.h"
72 #include "bnxt_hwmon.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 NetXtreme 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 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
124 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
125 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
126 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
127 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
128 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
129 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
130 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
131 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
132 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
133 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
134 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
135 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
136 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
137 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
138 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
139 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
140 };
141 
142 static const struct pci_device_id bnxt_pci_tbl[] = {
143 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
144 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
145 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
146 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
147 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
148 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
149 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
150 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
151 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
152 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
153 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
155 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
158 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
159 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
160 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
164 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
165 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
166 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
167 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
168 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
169 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
170 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
171 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
172 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
173 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
174 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
175 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
176 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
177 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
178 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
179 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
180 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
181 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
182 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
183 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
184 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
185 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
186 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
187 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
188 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
189 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
190 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
191 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
192 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
193 #ifdef CONFIG_BNXT_SRIOV
194 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
195 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
196 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
197 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
198 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
199 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
200 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
201 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
202 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
203 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
204 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
205 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
206 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
207 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
208 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
209 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
210 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
211 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
212 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
213 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
214 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
215 #endif
216 	{ 0 }
217 };
218 
219 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
220 
221 static const u16 bnxt_vf_req_snif[] = {
222 	HWRM_FUNC_CFG,
223 	HWRM_FUNC_VF_CFG,
224 	HWRM_PORT_PHY_QCFG,
225 	HWRM_CFA_L2_FILTER_ALLOC,
226 };
227 
228 static const u16 bnxt_async_events_arr[] = {
229 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
230 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
231 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
232 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
233 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
234 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
235 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
236 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
237 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
238 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
239 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
240 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
241 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
242 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
243 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
244 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
245 };
246 
247 static struct workqueue_struct *bnxt_pf_wq;
248 
249 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
250 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
251 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
252 
253 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
254 	.ports = {
255 		.src = 0,
256 		.dst = 0,
257 	},
258 	.addrs = {
259 		.v6addrs = {
260 			.src = BNXT_IPV6_MASK_NONE,
261 			.dst = BNXT_IPV6_MASK_NONE,
262 		},
263 	},
264 };
265 
266 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
267 	.ports = {
268 		.src = cpu_to_be16(0xffff),
269 		.dst = cpu_to_be16(0xffff),
270 	},
271 	.addrs = {
272 		.v6addrs = {
273 			.src = BNXT_IPV6_MASK_ALL,
274 			.dst = BNXT_IPV6_MASK_ALL,
275 		},
276 	},
277 };
278 
279 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
280 	.ports = {
281 		.src = cpu_to_be16(0xffff),
282 		.dst = cpu_to_be16(0xffff),
283 	},
284 	.addrs = {
285 		.v4addrs = {
286 			.src = cpu_to_be32(0xffffffff),
287 			.dst = cpu_to_be32(0xffffffff),
288 		},
289 	},
290 };
291 
292 static bool bnxt_vf_pciid(enum board_idx idx)
293 {
294 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
295 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
296 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
297 		idx == NETXTREME_E_P5_VF_HV);
298 }
299 
300 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
301 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
302 #define DB_CP_IRQ_DIS_FLAGS	(DB_KEY_CP | DB_IRQ_DIS)
303 
304 #define BNXT_CP_DB_IRQ_DIS(db)						\
305 		writel(DB_CP_IRQ_DIS_FLAGS, db)
306 
307 #define BNXT_DB_CQ(db, idx)						\
308 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
309 
310 #define BNXT_DB_NQ_P5(db, idx)						\
311 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
312 		    (db)->doorbell)
313 
314 #define BNXT_DB_NQ_P7(db, idx)						\
315 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
316 		    DB_RING_IDX(db, idx), (db)->doorbell)
317 
318 #define BNXT_DB_CQ_ARM(db, idx)						\
319 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
320 
321 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
322 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
323 		    DB_RING_IDX(db, idx), (db)->doorbell)
324 
325 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
326 {
327 	if (bp->flags & BNXT_FLAG_CHIP_P7)
328 		BNXT_DB_NQ_P7(db, idx);
329 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
330 		BNXT_DB_NQ_P5(db, idx);
331 	else
332 		BNXT_DB_CQ(db, idx);
333 }
334 
335 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
336 {
337 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
338 		BNXT_DB_NQ_ARM_P5(db, idx);
339 	else
340 		BNXT_DB_CQ_ARM(db, idx);
341 }
342 
343 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
344 {
345 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
346 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
347 			    DB_RING_IDX(db, idx), db->doorbell);
348 	else
349 		BNXT_DB_CQ(db, idx);
350 }
351 
352 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
353 {
354 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
355 		return;
356 
357 	if (BNXT_PF(bp))
358 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
359 	else
360 		schedule_delayed_work(&bp->fw_reset_task, delay);
361 }
362 
363 static void __bnxt_queue_sp_work(struct bnxt *bp)
364 {
365 	if (BNXT_PF(bp))
366 		queue_work(bnxt_pf_wq, &bp->sp_task);
367 	else
368 		schedule_work(&bp->sp_task);
369 }
370 
371 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
372 {
373 	set_bit(event, &bp->sp_event);
374 	__bnxt_queue_sp_work(bp);
375 }
376 
377 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
378 {
379 	if (!rxr->bnapi->in_reset) {
380 		rxr->bnapi->in_reset = true;
381 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
382 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
383 		else
384 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
385 		__bnxt_queue_sp_work(bp);
386 	}
387 	rxr->rx_next_cons = 0xffff;
388 }
389 
390 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
391 			  u16 curr)
392 {
393 	struct bnxt_napi *bnapi = txr->bnapi;
394 
395 	if (bnapi->tx_fault)
396 		return;
397 
398 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
399 		   txr->txq_index, txr->tx_hw_cons,
400 		   txr->tx_cons, txr->tx_prod, curr);
401 	WARN_ON_ONCE(1);
402 	bnapi->tx_fault = 1;
403 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
404 }
405 
406 const u16 bnxt_lhint_arr[] = {
407 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
408 	TX_BD_FLAGS_LHINT_512_TO_1023,
409 	TX_BD_FLAGS_LHINT_1024_TO_2047,
410 	TX_BD_FLAGS_LHINT_1024_TO_2047,
411 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
412 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
413 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
414 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
415 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
416 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
417 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
418 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
419 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
420 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
421 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
422 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
423 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
424 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
425 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
426 };
427 
428 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
429 {
430 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
431 
432 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
433 		return 0;
434 
435 	return md_dst->u.port_info.port_id;
436 }
437 
438 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
439 			     u16 prod)
440 {
441 	/* Sync BD data before updating doorbell */
442 	wmb();
443 	bnxt_db_write(bp, &txr->tx_db, prod);
444 	txr->kick_pending = 0;
445 }
446 
447 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
448 {
449 	struct bnxt *bp = netdev_priv(dev);
450 	struct tx_bd *txbd, *txbd0;
451 	struct tx_bd_ext *txbd1;
452 	struct netdev_queue *txq;
453 	int i;
454 	dma_addr_t mapping;
455 	unsigned int length, pad = 0;
456 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
457 	u16 prod, last_frag;
458 	struct pci_dev *pdev = bp->pdev;
459 	struct bnxt_tx_ring_info *txr;
460 	struct bnxt_sw_tx_bd *tx_buf;
461 	__le32 lflags = 0;
462 
463 	i = skb_get_queue_mapping(skb);
464 	if (unlikely(i >= bp->tx_nr_rings)) {
465 		dev_kfree_skb_any(skb);
466 		dev_core_stats_tx_dropped_inc(dev);
467 		return NETDEV_TX_OK;
468 	}
469 
470 	txq = netdev_get_tx_queue(dev, i);
471 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
472 	prod = txr->tx_prod;
473 
474 	free_size = bnxt_tx_avail(bp, txr);
475 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
476 		/* We must have raced with NAPI cleanup */
477 		if (net_ratelimit() && txr->kick_pending)
478 			netif_warn(bp, tx_err, dev,
479 				   "bnxt: ring busy w/ flush pending!\n");
480 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
481 					bp->tx_wake_thresh))
482 			return NETDEV_TX_BUSY;
483 	}
484 
485 	if (unlikely(ipv6_hopopt_jumbo_remove(skb)))
486 		goto tx_free;
487 
488 	length = skb->len;
489 	len = skb_headlen(skb);
490 	last_frag = skb_shinfo(skb)->nr_frags;
491 
492 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
493 
494 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
495 	tx_buf->skb = skb;
496 	tx_buf->nr_frags = last_frag;
497 
498 	vlan_tag_flags = 0;
499 	cfa_action = bnxt_xmit_get_cfa_action(skb);
500 	if (skb_vlan_tag_present(skb)) {
501 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
502 				 skb_vlan_tag_get(skb);
503 		/* Currently supports 8021Q, 8021AD vlan offloads
504 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
505 		 */
506 		if (skb->vlan_proto == htons(ETH_P_8021Q))
507 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
508 	}
509 
510 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
511 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
512 
513 		if (ptp && ptp->tx_tstamp_en && !skb_is_gso(skb) &&
514 		    atomic_dec_if_positive(&ptp->tx_avail) >= 0) {
515 			if (!bnxt_ptp_parse(skb, &ptp->tx_seqid,
516 					    &ptp->tx_hdr_off)) {
517 				if (vlan_tag_flags)
518 					ptp->tx_hdr_off += VLAN_HLEN;
519 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
520 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
521 			} else {
522 				atomic_inc(&bp->ptp_cfg->tx_avail);
523 			}
524 		}
525 	}
526 
527 	if (unlikely(skb->no_fcs))
528 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
529 
530 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
531 	    !lflags) {
532 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
533 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
534 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
535 		void __iomem *db = txr->tx_db.doorbell;
536 		void *pdata = tx_push_buf->data;
537 		u64 *end;
538 		int j, push_len;
539 
540 		/* Set COAL_NOW to be ready quickly for the next push */
541 		tx_push->tx_bd_len_flags_type =
542 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
543 					TX_BD_TYPE_LONG_TX_BD |
544 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
545 					TX_BD_FLAGS_COAL_NOW |
546 					TX_BD_FLAGS_PACKET_END |
547 					(2 << TX_BD_FLAGS_BD_CNT_SHIFT));
548 
549 		if (skb->ip_summed == CHECKSUM_PARTIAL)
550 			tx_push1->tx_bd_hsize_lflags =
551 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
552 		else
553 			tx_push1->tx_bd_hsize_lflags = 0;
554 
555 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
556 		tx_push1->tx_bd_cfa_action =
557 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
558 
559 		end = pdata + length;
560 		end = PTR_ALIGN(end, 8) - 1;
561 		*end = 0;
562 
563 		skb_copy_from_linear_data(skb, pdata, len);
564 		pdata += len;
565 		for (j = 0; j < last_frag; j++) {
566 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
567 			void *fptr;
568 
569 			fptr = skb_frag_address_safe(frag);
570 			if (!fptr)
571 				goto normal_tx;
572 
573 			memcpy(pdata, fptr, skb_frag_size(frag));
574 			pdata += skb_frag_size(frag);
575 		}
576 
577 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
578 		txbd->tx_bd_haddr = txr->data_mapping;
579 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
580 		prod = NEXT_TX(prod);
581 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
582 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
583 		memcpy(txbd, tx_push1, sizeof(*txbd));
584 		prod = NEXT_TX(prod);
585 		tx_push->doorbell =
586 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
587 				    DB_RING_IDX(&txr->tx_db, prod));
588 		WRITE_ONCE(txr->tx_prod, prod);
589 
590 		tx_buf->is_push = 1;
591 		netdev_tx_sent_queue(txq, skb->len);
592 		wmb();	/* Sync is_push and byte queue before pushing data */
593 
594 		push_len = (length + sizeof(*tx_push) + 7) / 8;
595 		if (push_len > 16) {
596 			__iowrite64_copy(db, tx_push_buf, 16);
597 			__iowrite32_copy(db + 4, tx_push_buf + 1,
598 					 (push_len - 16) << 1);
599 		} else {
600 			__iowrite64_copy(db, tx_push_buf, push_len);
601 		}
602 
603 		goto tx_done;
604 	}
605 
606 normal_tx:
607 	if (length < BNXT_MIN_PKT_SIZE) {
608 		pad = BNXT_MIN_PKT_SIZE - length;
609 		if (skb_pad(skb, pad))
610 			/* SKB already freed. */
611 			goto tx_kick_pending;
612 		length = BNXT_MIN_PKT_SIZE;
613 	}
614 
615 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
616 
617 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
618 		goto tx_free;
619 
620 	dma_unmap_addr_set(tx_buf, mapping, mapping);
621 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
622 		((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
623 
624 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
625 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
626 
627 	prod = NEXT_TX(prod);
628 	txbd1 = (struct tx_bd_ext *)
629 		&txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
630 
631 	txbd1->tx_bd_hsize_lflags = lflags;
632 	if (skb_is_gso(skb)) {
633 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
634 		u32 hdr_len;
635 
636 		if (skb->encapsulation) {
637 			if (udp_gso)
638 				hdr_len = skb_inner_transport_offset(skb) +
639 					  sizeof(struct udphdr);
640 			else
641 				hdr_len = skb_inner_tcp_all_headers(skb);
642 		} else if (udp_gso) {
643 			hdr_len = skb_transport_offset(skb) +
644 				  sizeof(struct udphdr);
645 		} else {
646 			hdr_len = skb_tcp_all_headers(skb);
647 		}
648 
649 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
650 					TX_BD_FLAGS_T_IPID |
651 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
652 		length = skb_shinfo(skb)->gso_size;
653 		txbd1->tx_bd_mss = cpu_to_le32(length);
654 		length += hdr_len;
655 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
656 		txbd1->tx_bd_hsize_lflags |=
657 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
658 		txbd1->tx_bd_mss = 0;
659 	}
660 
661 	length >>= 9;
662 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
663 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
664 				     skb->len);
665 		i = 0;
666 		goto tx_dma_error;
667 	}
668 	flags |= bnxt_lhint_arr[length];
669 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
670 
671 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
672 	txbd1->tx_bd_cfa_action =
673 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
674 	txbd0 = txbd;
675 	for (i = 0; i < last_frag; i++) {
676 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
677 
678 		prod = NEXT_TX(prod);
679 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
680 
681 		len = skb_frag_size(frag);
682 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
683 					   DMA_TO_DEVICE);
684 
685 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
686 			goto tx_dma_error;
687 
688 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
689 		dma_unmap_addr_set(tx_buf, mapping, mapping);
690 
691 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
692 
693 		flags = len << TX_BD_LEN_SHIFT;
694 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
695 	}
696 
697 	flags &= ~TX_BD_LEN;
698 	txbd->tx_bd_len_flags_type =
699 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
700 			    TX_BD_FLAGS_PACKET_END);
701 
702 	netdev_tx_sent_queue(txq, skb->len);
703 
704 	skb_tx_timestamp(skb);
705 
706 	prod = NEXT_TX(prod);
707 	WRITE_ONCE(txr->tx_prod, prod);
708 
709 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
710 		bnxt_txr_db_kick(bp, txr, prod);
711 	} else {
712 		if (free_size >= bp->tx_wake_thresh)
713 			txbd0->tx_bd_len_flags_type |=
714 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
715 		txr->kick_pending = 1;
716 	}
717 
718 tx_done:
719 
720 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
721 		if (netdev_xmit_more() && !tx_buf->is_push) {
722 			txbd0->tx_bd_len_flags_type &=
723 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
724 			bnxt_txr_db_kick(bp, txr, prod);
725 		}
726 
727 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
728 				   bp->tx_wake_thresh);
729 	}
730 	return NETDEV_TX_OK;
731 
732 tx_dma_error:
733 	if (BNXT_TX_PTP_IS_SET(lflags))
734 		atomic_inc(&bp->ptp_cfg->tx_avail);
735 
736 	last_frag = i;
737 
738 	/* start back at beginning and unmap skb */
739 	prod = txr->tx_prod;
740 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
741 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
742 			 skb_headlen(skb), DMA_TO_DEVICE);
743 	prod = NEXT_TX(prod);
744 
745 	/* unmap remaining mapped pages */
746 	for (i = 0; i < last_frag; i++) {
747 		prod = NEXT_TX(prod);
748 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
749 		dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
750 			       skb_frag_size(&skb_shinfo(skb)->frags[i]),
751 			       DMA_TO_DEVICE);
752 	}
753 
754 tx_free:
755 	dev_kfree_skb_any(skb);
756 tx_kick_pending:
757 	if (txr->kick_pending)
758 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
759 	txr->tx_buf_ring[txr->tx_prod].skb = NULL;
760 	dev_core_stats_tx_dropped_inc(dev);
761 	return NETDEV_TX_OK;
762 }
763 
764 static void __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
765 			  int budget)
766 {
767 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
768 	struct pci_dev *pdev = bp->pdev;
769 	u16 hw_cons = txr->tx_hw_cons;
770 	unsigned int tx_bytes = 0;
771 	u16 cons = txr->tx_cons;
772 	int tx_pkts = 0;
773 
774 	while (RING_TX(bp, cons) != hw_cons) {
775 		struct bnxt_sw_tx_bd *tx_buf;
776 		struct sk_buff *skb;
777 		int j, last;
778 
779 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
780 		cons = NEXT_TX(cons);
781 		skb = tx_buf->skb;
782 		tx_buf->skb = NULL;
783 
784 		if (unlikely(!skb)) {
785 			bnxt_sched_reset_txr(bp, txr, cons);
786 			return;
787 		}
788 
789 		tx_pkts++;
790 		tx_bytes += skb->len;
791 
792 		if (tx_buf->is_push) {
793 			tx_buf->is_push = 0;
794 			goto next_tx_int;
795 		}
796 
797 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
798 				 skb_headlen(skb), DMA_TO_DEVICE);
799 		last = tx_buf->nr_frags;
800 
801 		for (j = 0; j < last; j++) {
802 			cons = NEXT_TX(cons);
803 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
804 			dma_unmap_page(
805 				&pdev->dev,
806 				dma_unmap_addr(tx_buf, mapping),
807 				skb_frag_size(&skb_shinfo(skb)->frags[j]),
808 				DMA_TO_DEVICE);
809 		}
810 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
811 			if (BNXT_CHIP_P5(bp)) {
812 				/* PTP worker takes ownership of the skb */
813 				if (!bnxt_get_tx_ts_p5(bp, skb))
814 					skb = NULL;
815 				else
816 					atomic_inc(&bp->ptp_cfg->tx_avail);
817 			}
818 		}
819 
820 next_tx_int:
821 		cons = NEXT_TX(cons);
822 
823 		dev_consume_skb_any(skb);
824 	}
825 
826 	WRITE_ONCE(txr->tx_cons, cons);
827 
828 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
829 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
830 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
831 }
832 
833 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
834 {
835 	struct bnxt_tx_ring_info *txr;
836 	int i;
837 
838 	bnxt_for_each_napi_tx(i, bnapi, txr) {
839 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
840 			__bnxt_tx_int(bp, txr, budget);
841 	}
842 	bnapi->events &= ~BNXT_TX_CMP_EVENT;
843 }
844 
845 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
846 					 struct bnxt_rx_ring_info *rxr,
847 					 unsigned int *offset,
848 					 gfp_t gfp)
849 {
850 	struct page *page;
851 
852 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
853 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
854 						BNXT_RX_PAGE_SIZE);
855 	} else {
856 		page = page_pool_dev_alloc_pages(rxr->page_pool);
857 		*offset = 0;
858 	}
859 	if (!page)
860 		return NULL;
861 
862 	*mapping = page_pool_get_dma_addr(page) + *offset;
863 	return page;
864 }
865 
866 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
867 				       gfp_t gfp)
868 {
869 	u8 *data;
870 	struct pci_dev *pdev = bp->pdev;
871 
872 	if (gfp == GFP_ATOMIC)
873 		data = napi_alloc_frag(bp->rx_buf_size);
874 	else
875 		data = netdev_alloc_frag(bp->rx_buf_size);
876 	if (!data)
877 		return NULL;
878 
879 	*mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
880 					bp->rx_buf_use_size, bp->rx_dir,
881 					DMA_ATTR_WEAK_ORDERING);
882 
883 	if (dma_mapping_error(&pdev->dev, *mapping)) {
884 		skb_free_frag(data);
885 		data = NULL;
886 	}
887 	return data;
888 }
889 
890 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
891 		       u16 prod, gfp_t gfp)
892 {
893 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
894 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
895 	dma_addr_t mapping;
896 
897 	if (BNXT_RX_PAGE_MODE(bp)) {
898 		unsigned int offset;
899 		struct page *page =
900 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
901 
902 		if (!page)
903 			return -ENOMEM;
904 
905 		mapping += bp->rx_dma_offset;
906 		rx_buf->data = page;
907 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
908 	} else {
909 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, gfp);
910 
911 		if (!data)
912 			return -ENOMEM;
913 
914 		rx_buf->data = data;
915 		rx_buf->data_ptr = data + bp->rx_offset;
916 	}
917 	rx_buf->mapping = mapping;
918 
919 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
920 	return 0;
921 }
922 
923 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
924 {
925 	u16 prod = rxr->rx_prod;
926 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
927 	struct bnxt *bp = rxr->bnapi->bp;
928 	struct rx_bd *cons_bd, *prod_bd;
929 
930 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
931 	cons_rx_buf = &rxr->rx_buf_ring[cons];
932 
933 	prod_rx_buf->data = data;
934 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
935 
936 	prod_rx_buf->mapping = cons_rx_buf->mapping;
937 
938 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
939 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
940 
941 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
942 }
943 
944 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
945 {
946 	u16 next, max = rxr->rx_agg_bmap_size;
947 
948 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
949 	if (next >= max)
950 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
951 	return next;
952 }
953 
954 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
955 				     struct bnxt_rx_ring_info *rxr,
956 				     u16 prod, gfp_t gfp)
957 {
958 	struct rx_bd *rxbd =
959 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
960 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
961 	struct page *page;
962 	dma_addr_t mapping;
963 	u16 sw_prod = rxr->rx_sw_agg_prod;
964 	unsigned int offset = 0;
965 
966 	page = __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
967 
968 	if (!page)
969 		return -ENOMEM;
970 
971 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
972 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
973 
974 	__set_bit(sw_prod, rxr->rx_agg_bmap);
975 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
976 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
977 
978 	rx_agg_buf->page = page;
979 	rx_agg_buf->offset = offset;
980 	rx_agg_buf->mapping = mapping;
981 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
982 	rxbd->rx_bd_opaque = sw_prod;
983 	return 0;
984 }
985 
986 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
987 				       struct bnxt_cp_ring_info *cpr,
988 				       u16 cp_cons, u16 curr)
989 {
990 	struct rx_agg_cmp *agg;
991 
992 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
993 	agg = (struct rx_agg_cmp *)
994 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
995 	return agg;
996 }
997 
998 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
999 					      struct bnxt_rx_ring_info *rxr,
1000 					      u16 agg_id, u16 curr)
1001 {
1002 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1003 
1004 	return &tpa_info->agg_arr[curr];
1005 }
1006 
1007 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1008 				   u16 start, u32 agg_bufs, bool tpa)
1009 {
1010 	struct bnxt_napi *bnapi = cpr->bnapi;
1011 	struct bnxt *bp = bnapi->bp;
1012 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1013 	u16 prod = rxr->rx_agg_prod;
1014 	u16 sw_prod = rxr->rx_sw_agg_prod;
1015 	bool p5_tpa = false;
1016 	u32 i;
1017 
1018 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1019 		p5_tpa = true;
1020 
1021 	for (i = 0; i < agg_bufs; i++) {
1022 		u16 cons;
1023 		struct rx_agg_cmp *agg;
1024 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1025 		struct rx_bd *prod_bd;
1026 		struct page *page;
1027 
1028 		if (p5_tpa)
1029 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1030 		else
1031 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1032 		cons = agg->rx_agg_cmp_opaque;
1033 		__clear_bit(cons, rxr->rx_agg_bmap);
1034 
1035 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1036 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1037 
1038 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1039 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1040 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1041 
1042 		/* It is possible for sw_prod to be equal to cons, so
1043 		 * set cons_rx_buf->page to NULL first.
1044 		 */
1045 		page = cons_rx_buf->page;
1046 		cons_rx_buf->page = NULL;
1047 		prod_rx_buf->page = page;
1048 		prod_rx_buf->offset = cons_rx_buf->offset;
1049 
1050 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1051 
1052 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1053 
1054 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1055 		prod_bd->rx_bd_opaque = sw_prod;
1056 
1057 		prod = NEXT_RX_AGG(prod);
1058 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1059 	}
1060 	rxr->rx_agg_prod = prod;
1061 	rxr->rx_sw_agg_prod = sw_prod;
1062 }
1063 
1064 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1065 					      struct bnxt_rx_ring_info *rxr,
1066 					      u16 cons, void *data, u8 *data_ptr,
1067 					      dma_addr_t dma_addr,
1068 					      unsigned int offset_and_len)
1069 {
1070 	unsigned int len = offset_and_len & 0xffff;
1071 	struct page *page = data;
1072 	u16 prod = rxr->rx_prod;
1073 	struct sk_buff *skb;
1074 	int err;
1075 
1076 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1077 	if (unlikely(err)) {
1078 		bnxt_reuse_rx_data(rxr, cons, data);
1079 		return NULL;
1080 	}
1081 	dma_addr -= bp->rx_dma_offset;
1082 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1083 				bp->rx_dir);
1084 	skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE);
1085 	if (!skb) {
1086 		page_pool_recycle_direct(rxr->page_pool, page);
1087 		return NULL;
1088 	}
1089 	skb_mark_for_recycle(skb);
1090 	skb_reserve(skb, bp->rx_offset);
1091 	__skb_put(skb, len);
1092 
1093 	return skb;
1094 }
1095 
1096 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1097 					struct bnxt_rx_ring_info *rxr,
1098 					u16 cons, void *data, u8 *data_ptr,
1099 					dma_addr_t dma_addr,
1100 					unsigned int offset_and_len)
1101 {
1102 	unsigned int payload = offset_and_len >> 16;
1103 	unsigned int len = offset_and_len & 0xffff;
1104 	skb_frag_t *frag;
1105 	struct page *page = data;
1106 	u16 prod = rxr->rx_prod;
1107 	struct sk_buff *skb;
1108 	int off, err;
1109 
1110 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1111 	if (unlikely(err)) {
1112 		bnxt_reuse_rx_data(rxr, cons, data);
1113 		return NULL;
1114 	}
1115 	dma_addr -= bp->rx_dma_offset;
1116 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1117 				bp->rx_dir);
1118 
1119 	if (unlikely(!payload))
1120 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1121 
1122 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1123 	if (!skb) {
1124 		page_pool_recycle_direct(rxr->page_pool, page);
1125 		return NULL;
1126 	}
1127 
1128 	skb_mark_for_recycle(skb);
1129 	off = (void *)data_ptr - page_address(page);
1130 	skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE);
1131 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1132 	       payload + NET_IP_ALIGN);
1133 
1134 	frag = &skb_shinfo(skb)->frags[0];
1135 	skb_frag_size_sub(frag, payload);
1136 	skb_frag_off_add(frag, payload);
1137 	skb->data_len -= payload;
1138 	skb->tail += payload;
1139 
1140 	return skb;
1141 }
1142 
1143 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1144 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1145 				   void *data, u8 *data_ptr,
1146 				   dma_addr_t dma_addr,
1147 				   unsigned int offset_and_len)
1148 {
1149 	u16 prod = rxr->rx_prod;
1150 	struct sk_buff *skb;
1151 	int err;
1152 
1153 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1154 	if (unlikely(err)) {
1155 		bnxt_reuse_rx_data(rxr, cons, data);
1156 		return NULL;
1157 	}
1158 
1159 	skb = napi_build_skb(data, bp->rx_buf_size);
1160 	dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1161 			       bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
1162 	if (!skb) {
1163 		skb_free_frag(data);
1164 		return NULL;
1165 	}
1166 
1167 	skb_reserve(skb, bp->rx_offset);
1168 	skb_put(skb, offset_and_len & 0xffff);
1169 	return skb;
1170 }
1171 
1172 static u32 __bnxt_rx_agg_pages(struct bnxt *bp,
1173 			       struct bnxt_cp_ring_info *cpr,
1174 			       struct skb_shared_info *shinfo,
1175 			       u16 idx, u32 agg_bufs, bool tpa,
1176 			       struct xdp_buff *xdp)
1177 {
1178 	struct bnxt_napi *bnapi = cpr->bnapi;
1179 	struct pci_dev *pdev = bp->pdev;
1180 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1181 	u16 prod = rxr->rx_agg_prod;
1182 	u32 i, total_frag_len = 0;
1183 	bool p5_tpa = false;
1184 
1185 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1186 		p5_tpa = true;
1187 
1188 	for (i = 0; i < agg_bufs; i++) {
1189 		skb_frag_t *frag = &shinfo->frags[i];
1190 		u16 cons, frag_len;
1191 		struct rx_agg_cmp *agg;
1192 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1193 		struct page *page;
1194 		dma_addr_t mapping;
1195 
1196 		if (p5_tpa)
1197 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1198 		else
1199 			agg = bnxt_get_agg(bp, cpr, idx, i);
1200 		cons = agg->rx_agg_cmp_opaque;
1201 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1202 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1203 
1204 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1205 		skb_frag_fill_page_desc(frag, cons_rx_buf->page,
1206 					cons_rx_buf->offset, frag_len);
1207 		shinfo->nr_frags = i + 1;
1208 		__clear_bit(cons, rxr->rx_agg_bmap);
1209 
1210 		/* It is possible for bnxt_alloc_rx_page() to allocate
1211 		 * a sw_prod index that equals the cons index, so we
1212 		 * need to clear the cons entry now.
1213 		 */
1214 		mapping = cons_rx_buf->mapping;
1215 		page = cons_rx_buf->page;
1216 		cons_rx_buf->page = NULL;
1217 
1218 		if (xdp && page_is_pfmemalloc(page))
1219 			xdp_buff_set_frag_pfmemalloc(xdp);
1220 
1221 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1222 			--shinfo->nr_frags;
1223 			cons_rx_buf->page = page;
1224 
1225 			/* Update prod since possibly some pages have been
1226 			 * allocated already.
1227 			 */
1228 			rxr->rx_agg_prod = prod;
1229 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1230 			return 0;
1231 		}
1232 
1233 		dma_sync_single_for_cpu(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1234 					bp->rx_dir);
1235 
1236 		total_frag_len += frag_len;
1237 		prod = NEXT_RX_AGG(prod);
1238 	}
1239 	rxr->rx_agg_prod = prod;
1240 	return total_frag_len;
1241 }
1242 
1243 static struct sk_buff *bnxt_rx_agg_pages_skb(struct bnxt *bp,
1244 					     struct bnxt_cp_ring_info *cpr,
1245 					     struct sk_buff *skb, u16 idx,
1246 					     u32 agg_bufs, bool tpa)
1247 {
1248 	struct skb_shared_info *shinfo = skb_shinfo(skb);
1249 	u32 total_frag_len = 0;
1250 
1251 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo, idx,
1252 					     agg_bufs, tpa, NULL);
1253 	if (!total_frag_len) {
1254 		skb_mark_for_recycle(skb);
1255 		dev_kfree_skb(skb);
1256 		return NULL;
1257 	}
1258 
1259 	skb->data_len += total_frag_len;
1260 	skb->len += total_frag_len;
1261 	skb->truesize += BNXT_RX_PAGE_SIZE * agg_bufs;
1262 	return skb;
1263 }
1264 
1265 static u32 bnxt_rx_agg_pages_xdp(struct bnxt *bp,
1266 				 struct bnxt_cp_ring_info *cpr,
1267 				 struct xdp_buff *xdp, u16 idx,
1268 				 u32 agg_bufs, bool tpa)
1269 {
1270 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1271 	u32 total_frag_len = 0;
1272 
1273 	if (!xdp_buff_has_frags(xdp))
1274 		shinfo->nr_frags = 0;
1275 
1276 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo,
1277 					     idx, agg_bufs, tpa, xdp);
1278 	if (total_frag_len) {
1279 		xdp_buff_set_frags_flag(xdp);
1280 		shinfo->nr_frags = agg_bufs;
1281 		shinfo->xdp_frags_size = total_frag_len;
1282 	}
1283 	return total_frag_len;
1284 }
1285 
1286 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1287 			       u8 agg_bufs, u32 *raw_cons)
1288 {
1289 	u16 last;
1290 	struct rx_agg_cmp *agg;
1291 
1292 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1293 	last = RING_CMP(*raw_cons);
1294 	agg = (struct rx_agg_cmp *)
1295 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1296 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1297 }
1298 
1299 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1300 				      unsigned int len,
1301 				      dma_addr_t mapping)
1302 {
1303 	struct bnxt *bp = bnapi->bp;
1304 	struct pci_dev *pdev = bp->pdev;
1305 	struct sk_buff *skb;
1306 
1307 	skb = napi_alloc_skb(&bnapi->napi, len);
1308 	if (!skb)
1309 		return NULL;
1310 
1311 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1312 				bp->rx_dir);
1313 
1314 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1315 	       len + NET_IP_ALIGN);
1316 
1317 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1318 				   bp->rx_dir);
1319 
1320 	skb_put(skb, len);
1321 
1322 	return skb;
1323 }
1324 
1325 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1326 				     unsigned int len,
1327 				     dma_addr_t mapping)
1328 {
1329 	return bnxt_copy_data(bnapi, data, len, mapping);
1330 }
1331 
1332 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1333 				     struct xdp_buff *xdp,
1334 				     unsigned int len,
1335 				     dma_addr_t mapping)
1336 {
1337 	unsigned int metasize = 0;
1338 	u8 *data = xdp->data;
1339 	struct sk_buff *skb;
1340 
1341 	len = xdp->data_end - xdp->data_meta;
1342 	metasize = xdp->data - xdp->data_meta;
1343 	data = xdp->data_meta;
1344 
1345 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1346 	if (!skb)
1347 		return skb;
1348 
1349 	if (metasize) {
1350 		skb_metadata_set(skb, metasize);
1351 		__skb_pull(skb, metasize);
1352 	}
1353 
1354 	return skb;
1355 }
1356 
1357 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1358 			   u32 *raw_cons, void *cmp)
1359 {
1360 	struct rx_cmp *rxcmp = cmp;
1361 	u32 tmp_raw_cons = *raw_cons;
1362 	u8 cmp_type, agg_bufs = 0;
1363 
1364 	cmp_type = RX_CMP_TYPE(rxcmp);
1365 
1366 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1367 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1368 			    RX_CMP_AGG_BUFS) >>
1369 			   RX_CMP_AGG_BUFS_SHIFT;
1370 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1371 		struct rx_tpa_end_cmp *tpa_end = cmp;
1372 
1373 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1374 			return 0;
1375 
1376 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1377 	}
1378 
1379 	if (agg_bufs) {
1380 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1381 			return -EBUSY;
1382 	}
1383 	*raw_cons = tmp_raw_cons;
1384 	return 0;
1385 }
1386 
1387 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1388 {
1389 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1390 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1391 
1392 	if (test_bit(idx, map->agg_idx_bmap))
1393 		idx = find_first_zero_bit(map->agg_idx_bmap,
1394 					  BNXT_AGG_IDX_BMAP_SIZE);
1395 	__set_bit(idx, map->agg_idx_bmap);
1396 	map->agg_id_tbl[agg_id] = idx;
1397 	return idx;
1398 }
1399 
1400 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1401 {
1402 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1403 
1404 	__clear_bit(idx, map->agg_idx_bmap);
1405 }
1406 
1407 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1408 {
1409 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1410 
1411 	return map->agg_id_tbl[agg_id];
1412 }
1413 
1414 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1415 			      struct rx_tpa_start_cmp *tpa_start,
1416 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1417 {
1418 	tpa_info->cfa_code_valid = 1;
1419 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1420 	tpa_info->vlan_valid = 0;
1421 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1422 		tpa_info->vlan_valid = 1;
1423 		tpa_info->metadata =
1424 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1425 	}
1426 }
1427 
1428 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1429 				 struct rx_tpa_start_cmp *tpa_start,
1430 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1431 {
1432 	tpa_info->vlan_valid = 0;
1433 	if (TPA_START_VLAN_VALID(tpa_start)) {
1434 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1435 		u32 vlan_proto = ETH_P_8021Q;
1436 
1437 		tpa_info->vlan_valid = 1;
1438 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1439 			vlan_proto = ETH_P_8021AD;
1440 		tpa_info->metadata = vlan_proto << 16 |
1441 				     TPA_START_METADATA0_TCI(tpa_start1);
1442 	}
1443 }
1444 
1445 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1446 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1447 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1448 {
1449 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1450 	struct bnxt_tpa_info *tpa_info;
1451 	u16 cons, prod, agg_id;
1452 	struct rx_bd *prod_bd;
1453 	dma_addr_t mapping;
1454 
1455 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1456 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1457 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1458 	} else {
1459 		agg_id = TPA_START_AGG_ID(tpa_start);
1460 	}
1461 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1462 	prod = rxr->rx_prod;
1463 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1464 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1465 	tpa_info = &rxr->rx_tpa[agg_id];
1466 
1467 	if (unlikely(cons != rxr->rx_next_cons ||
1468 		     TPA_START_ERROR(tpa_start))) {
1469 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1470 			    cons, rxr->rx_next_cons,
1471 			    TPA_START_ERROR_CODE(tpa_start1));
1472 		bnxt_sched_reset_rxr(bp, rxr);
1473 		return;
1474 	}
1475 	prod_rx_buf->data = tpa_info->data;
1476 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1477 
1478 	mapping = tpa_info->mapping;
1479 	prod_rx_buf->mapping = mapping;
1480 
1481 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1482 
1483 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1484 
1485 	tpa_info->data = cons_rx_buf->data;
1486 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1487 	cons_rx_buf->data = NULL;
1488 	tpa_info->mapping = cons_rx_buf->mapping;
1489 
1490 	tpa_info->len =
1491 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1492 				RX_TPA_START_CMP_LEN_SHIFT;
1493 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1494 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1495 		tpa_info->gso_type = SKB_GSO_TCPV4;
1496 		if (TPA_START_IS_IPV6(tpa_start1))
1497 			tpa_info->gso_type = SKB_GSO_TCPV6;
1498 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1499 		else if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP &&
1500 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1501 			tpa_info->gso_type = SKB_GSO_TCPV6;
1502 		tpa_info->rss_hash =
1503 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1504 	} else {
1505 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1506 		tpa_info->gso_type = 0;
1507 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1508 	}
1509 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1510 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1511 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1512 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1513 	else
1514 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1515 	tpa_info->agg_count = 0;
1516 
1517 	rxr->rx_prod = NEXT_RX(prod);
1518 	cons = RING_RX(bp, NEXT_RX(cons));
1519 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1520 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1521 
1522 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1523 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1524 	cons_rx_buf->data = NULL;
1525 }
1526 
1527 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1528 {
1529 	if (agg_bufs)
1530 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1531 }
1532 
1533 #ifdef CONFIG_INET
1534 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1535 {
1536 	struct udphdr *uh = NULL;
1537 
1538 	if (ip_proto == htons(ETH_P_IP)) {
1539 		struct iphdr *iph = (struct iphdr *)skb->data;
1540 
1541 		if (iph->protocol == IPPROTO_UDP)
1542 			uh = (struct udphdr *)(iph + 1);
1543 	} else {
1544 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1545 
1546 		if (iph->nexthdr == IPPROTO_UDP)
1547 			uh = (struct udphdr *)(iph + 1);
1548 	}
1549 	if (uh) {
1550 		if (uh->check)
1551 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1552 		else
1553 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1554 	}
1555 }
1556 #endif
1557 
1558 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1559 					   int payload_off, int tcp_ts,
1560 					   struct sk_buff *skb)
1561 {
1562 #ifdef CONFIG_INET
1563 	struct tcphdr *th;
1564 	int len, nw_off;
1565 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1566 	u32 hdr_info = tpa_info->hdr_info;
1567 	bool loopback = false;
1568 
1569 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1570 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1571 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1572 
1573 	/* If the packet is an internal loopback packet, the offsets will
1574 	 * have an extra 4 bytes.
1575 	 */
1576 	if (inner_mac_off == 4) {
1577 		loopback = true;
1578 	} else if (inner_mac_off > 4) {
1579 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1580 					    ETH_HLEN - 2));
1581 
1582 		/* We only support inner iPv4/ipv6.  If we don't see the
1583 		 * correct protocol ID, it must be a loopback packet where
1584 		 * the offsets are off by 4.
1585 		 */
1586 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1587 			loopback = true;
1588 	}
1589 	if (loopback) {
1590 		/* internal loopback packet, subtract all offsets by 4 */
1591 		inner_ip_off -= 4;
1592 		inner_mac_off -= 4;
1593 		outer_ip_off -= 4;
1594 	}
1595 
1596 	nw_off = inner_ip_off - ETH_HLEN;
1597 	skb_set_network_header(skb, nw_off);
1598 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1599 		struct ipv6hdr *iph = ipv6_hdr(skb);
1600 
1601 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1602 		len = skb->len - skb_transport_offset(skb);
1603 		th = tcp_hdr(skb);
1604 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1605 	} else {
1606 		struct iphdr *iph = ip_hdr(skb);
1607 
1608 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1609 		len = skb->len - skb_transport_offset(skb);
1610 		th = tcp_hdr(skb);
1611 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1612 	}
1613 
1614 	if (inner_mac_off) { /* tunnel */
1615 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1616 					    ETH_HLEN - 2));
1617 
1618 		bnxt_gro_tunnel(skb, proto);
1619 	}
1620 #endif
1621 	return skb;
1622 }
1623 
1624 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1625 					   int payload_off, int tcp_ts,
1626 					   struct sk_buff *skb)
1627 {
1628 #ifdef CONFIG_INET
1629 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1630 	u32 hdr_info = tpa_info->hdr_info;
1631 	int iphdr_len, nw_off;
1632 
1633 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1634 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1635 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1636 
1637 	nw_off = inner_ip_off - ETH_HLEN;
1638 	skb_set_network_header(skb, nw_off);
1639 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1640 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1641 	skb_set_transport_header(skb, nw_off + iphdr_len);
1642 
1643 	if (inner_mac_off) { /* tunnel */
1644 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1645 					    ETH_HLEN - 2));
1646 
1647 		bnxt_gro_tunnel(skb, proto);
1648 	}
1649 #endif
1650 	return skb;
1651 }
1652 
1653 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1654 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1655 
1656 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1657 					   int payload_off, int tcp_ts,
1658 					   struct sk_buff *skb)
1659 {
1660 #ifdef CONFIG_INET
1661 	struct tcphdr *th;
1662 	int len, nw_off, tcp_opt_len = 0;
1663 
1664 	if (tcp_ts)
1665 		tcp_opt_len = 12;
1666 
1667 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1668 		struct iphdr *iph;
1669 
1670 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1671 			 ETH_HLEN;
1672 		skb_set_network_header(skb, nw_off);
1673 		iph = ip_hdr(skb);
1674 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1675 		len = skb->len - skb_transport_offset(skb);
1676 		th = tcp_hdr(skb);
1677 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1678 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1679 		struct ipv6hdr *iph;
1680 
1681 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1682 			 ETH_HLEN;
1683 		skb_set_network_header(skb, nw_off);
1684 		iph = ipv6_hdr(skb);
1685 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1686 		len = skb->len - skb_transport_offset(skb);
1687 		th = tcp_hdr(skb);
1688 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1689 	} else {
1690 		dev_kfree_skb_any(skb);
1691 		return NULL;
1692 	}
1693 
1694 	if (nw_off) /* tunnel */
1695 		bnxt_gro_tunnel(skb, skb->protocol);
1696 #endif
1697 	return skb;
1698 }
1699 
1700 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1701 					   struct bnxt_tpa_info *tpa_info,
1702 					   struct rx_tpa_end_cmp *tpa_end,
1703 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1704 					   struct sk_buff *skb)
1705 {
1706 #ifdef CONFIG_INET
1707 	int payload_off;
1708 	u16 segs;
1709 
1710 	segs = TPA_END_TPA_SEGS(tpa_end);
1711 	if (segs == 1)
1712 		return skb;
1713 
1714 	NAPI_GRO_CB(skb)->count = segs;
1715 	skb_shinfo(skb)->gso_size =
1716 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1717 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1718 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1719 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1720 	else
1721 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1722 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1723 	if (likely(skb))
1724 		tcp_gro_complete(skb);
1725 #endif
1726 	return skb;
1727 }
1728 
1729 /* Given the cfa_code of a received packet determine which
1730  * netdev (vf-rep or PF) the packet is destined to.
1731  */
1732 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1733 {
1734 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1735 
1736 	/* if vf-rep dev is NULL, the must belongs to the PF */
1737 	return dev ? dev : bp->dev;
1738 }
1739 
1740 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1741 					   struct bnxt_cp_ring_info *cpr,
1742 					   u32 *raw_cons,
1743 					   struct rx_tpa_end_cmp *tpa_end,
1744 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1745 					   u8 *event)
1746 {
1747 	struct bnxt_napi *bnapi = cpr->bnapi;
1748 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1749 	struct net_device *dev = bp->dev;
1750 	u8 *data_ptr, agg_bufs;
1751 	unsigned int len;
1752 	struct bnxt_tpa_info *tpa_info;
1753 	dma_addr_t mapping;
1754 	struct sk_buff *skb;
1755 	u16 idx = 0, agg_id;
1756 	void *data;
1757 	bool gro;
1758 
1759 	if (unlikely(bnapi->in_reset)) {
1760 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1761 
1762 		if (rc < 0)
1763 			return ERR_PTR(-EBUSY);
1764 		return NULL;
1765 	}
1766 
1767 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1768 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1769 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1770 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1771 		tpa_info = &rxr->rx_tpa[agg_id];
1772 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1773 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1774 				    agg_bufs, tpa_info->agg_count);
1775 			agg_bufs = tpa_info->agg_count;
1776 		}
1777 		tpa_info->agg_count = 0;
1778 		*event |= BNXT_AGG_EVENT;
1779 		bnxt_free_agg_idx(rxr, agg_id);
1780 		idx = agg_id;
1781 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1782 	} else {
1783 		agg_id = TPA_END_AGG_ID(tpa_end);
1784 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1785 		tpa_info = &rxr->rx_tpa[agg_id];
1786 		idx = RING_CMP(*raw_cons);
1787 		if (agg_bufs) {
1788 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1789 				return ERR_PTR(-EBUSY);
1790 
1791 			*event |= BNXT_AGG_EVENT;
1792 			idx = NEXT_CMP(idx);
1793 		}
1794 		gro = !!TPA_END_GRO(tpa_end);
1795 	}
1796 	data = tpa_info->data;
1797 	data_ptr = tpa_info->data_ptr;
1798 	prefetch(data_ptr);
1799 	len = tpa_info->len;
1800 	mapping = tpa_info->mapping;
1801 
1802 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1803 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1804 		if (agg_bufs > MAX_SKB_FRAGS)
1805 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1806 				    agg_bufs, (int)MAX_SKB_FRAGS);
1807 		return NULL;
1808 	}
1809 
1810 	if (len <= bp->rx_copy_thresh) {
1811 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1812 		if (!skb) {
1813 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1814 			cpr->bnapi->cp_ring.sw_stats.rx.rx_oom_discards += 1;
1815 			return NULL;
1816 		}
1817 	} else {
1818 		u8 *new_data;
1819 		dma_addr_t new_mapping;
1820 
1821 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, GFP_ATOMIC);
1822 		if (!new_data) {
1823 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1824 			cpr->bnapi->cp_ring.sw_stats.rx.rx_oom_discards += 1;
1825 			return NULL;
1826 		}
1827 
1828 		tpa_info->data = new_data;
1829 		tpa_info->data_ptr = new_data + bp->rx_offset;
1830 		tpa_info->mapping = new_mapping;
1831 
1832 		skb = napi_build_skb(data, bp->rx_buf_size);
1833 		dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1834 				       bp->rx_buf_use_size, bp->rx_dir,
1835 				       DMA_ATTR_WEAK_ORDERING);
1836 
1837 		if (!skb) {
1838 			skb_free_frag(data);
1839 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1840 			cpr->bnapi->cp_ring.sw_stats.rx.rx_oom_discards += 1;
1841 			return NULL;
1842 		}
1843 		skb_reserve(skb, bp->rx_offset);
1844 		skb_put(skb, len);
1845 	}
1846 
1847 	if (agg_bufs) {
1848 		skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, idx, agg_bufs, true);
1849 		if (!skb) {
1850 			/* Page reuse already handled by bnxt_rx_pages(). */
1851 			cpr->bnapi->cp_ring.sw_stats.rx.rx_oom_discards += 1;
1852 			return NULL;
1853 		}
1854 	}
1855 
1856 	if (tpa_info->cfa_code_valid)
1857 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1858 	skb->protocol = eth_type_trans(skb, dev);
1859 
1860 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1861 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1862 
1863 	if (tpa_info->vlan_valid &&
1864 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1865 		__be16 vlan_proto = htons(tpa_info->metadata >>
1866 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1867 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1868 
1869 		if (eth_type_vlan(vlan_proto)) {
1870 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1871 		} else {
1872 			dev_kfree_skb(skb);
1873 			return NULL;
1874 		}
1875 	}
1876 
1877 	skb_checksum_none_assert(skb);
1878 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1879 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1880 		skb->csum_level =
1881 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1882 	}
1883 
1884 	if (gro)
1885 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1886 
1887 	return skb;
1888 }
1889 
1890 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1891 			 struct rx_agg_cmp *rx_agg)
1892 {
1893 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1894 	struct bnxt_tpa_info *tpa_info;
1895 
1896 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1897 	tpa_info = &rxr->rx_tpa[agg_id];
1898 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1899 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1900 }
1901 
1902 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1903 			     struct sk_buff *skb)
1904 {
1905 	skb_mark_for_recycle(skb);
1906 
1907 	if (skb->dev != bp->dev) {
1908 		/* this packet belongs to a vf-rep */
1909 		bnxt_vf_rep_rx(bp, skb);
1910 		return;
1911 	}
1912 	skb_record_rx_queue(skb, bnapi->index);
1913 	napi_gro_receive(&bnapi->napi, skb);
1914 }
1915 
1916 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
1917 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
1918 {
1919 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
1920 
1921 	if (BNXT_PTP_RX_TS_VALID(flags))
1922 		goto ts_valid;
1923 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
1924 		return false;
1925 
1926 ts_valid:
1927 	*cmpl_ts = ts;
1928 	return true;
1929 }
1930 
1931 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
1932 				    struct rx_cmp *rxcmp,
1933 				    struct rx_cmp_ext *rxcmp1)
1934 {
1935 	__be16 vlan_proto;
1936 	u16 vtag;
1937 
1938 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1939 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
1940 		u32 meta_data;
1941 
1942 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
1943 			return skb;
1944 
1945 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1946 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1947 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
1948 		if (eth_type_vlan(vlan_proto))
1949 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1950 		else
1951 			goto vlan_err;
1952 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
1953 		if (RX_CMP_VLAN_VALID(rxcmp)) {
1954 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
1955 
1956 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
1957 				vlan_proto = htons(ETH_P_8021Q);
1958 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
1959 				vlan_proto = htons(ETH_P_8021AD);
1960 			else
1961 				goto vlan_err;
1962 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
1963 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1964 		}
1965 	}
1966 	return skb;
1967 vlan_err:
1968 	dev_kfree_skb(skb);
1969 	return NULL;
1970 }
1971 
1972 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp,
1973 					   struct rx_cmp *rxcmp)
1974 {
1975 	u8 ext_op;
1976 
1977 	ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp);
1978 	switch (ext_op) {
1979 	case EXT_OP_INNER_4:
1980 	case EXT_OP_OUTER_4:
1981 	case EXT_OP_INNFL_3:
1982 	case EXT_OP_OUTFL_3:
1983 		return PKT_HASH_TYPE_L4;
1984 	default:
1985 		return PKT_HASH_TYPE_L3;
1986 	}
1987 }
1988 
1989 /* returns the following:
1990  * 1       - 1 packet successfully received
1991  * 0       - successful TPA_START, packet not completed yet
1992  * -EBUSY  - completion ring does not have all the agg buffers yet
1993  * -ENOMEM - packet aborted due to out of memory
1994  * -EIO    - packet aborted due to hw error indicated in BD
1995  */
1996 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1997 		       u32 *raw_cons, u8 *event)
1998 {
1999 	struct bnxt_napi *bnapi = cpr->bnapi;
2000 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2001 	struct net_device *dev = bp->dev;
2002 	struct rx_cmp *rxcmp;
2003 	struct rx_cmp_ext *rxcmp1;
2004 	u32 tmp_raw_cons = *raw_cons;
2005 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2006 	struct bnxt_sw_rx_bd *rx_buf;
2007 	unsigned int len;
2008 	u8 *data_ptr, agg_bufs, cmp_type;
2009 	bool xdp_active = false;
2010 	dma_addr_t dma_addr;
2011 	struct sk_buff *skb;
2012 	struct xdp_buff xdp;
2013 	u32 flags, misc;
2014 	u32 cmpl_ts;
2015 	void *data;
2016 	int rc = 0;
2017 
2018 	rxcmp = (struct rx_cmp *)
2019 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2020 
2021 	cmp_type = RX_CMP_TYPE(rxcmp);
2022 
2023 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2024 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2025 		goto next_rx_no_prod_no_len;
2026 	}
2027 
2028 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2029 	cp_cons = RING_CMP(tmp_raw_cons);
2030 	rxcmp1 = (struct rx_cmp_ext *)
2031 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2032 
2033 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2034 		return -EBUSY;
2035 
2036 	/* The valid test of the entry must be done first before
2037 	 * reading any further.
2038 	 */
2039 	dma_rmb();
2040 	prod = rxr->rx_prod;
2041 
2042 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2043 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2044 		bnxt_tpa_start(bp, rxr, cmp_type,
2045 			       (struct rx_tpa_start_cmp *)rxcmp,
2046 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2047 
2048 		*event |= BNXT_RX_EVENT;
2049 		goto next_rx_no_prod_no_len;
2050 
2051 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2052 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2053 				   (struct rx_tpa_end_cmp *)rxcmp,
2054 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2055 
2056 		if (IS_ERR(skb))
2057 			return -EBUSY;
2058 
2059 		rc = -ENOMEM;
2060 		if (likely(skb)) {
2061 			bnxt_deliver_skb(bp, bnapi, skb);
2062 			rc = 1;
2063 		}
2064 		*event |= BNXT_RX_EVENT;
2065 		goto next_rx_no_prod_no_len;
2066 	}
2067 
2068 	cons = rxcmp->rx_cmp_opaque;
2069 	if (unlikely(cons != rxr->rx_next_cons)) {
2070 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2071 
2072 		/* 0xffff is forced error, don't print it */
2073 		if (rxr->rx_next_cons != 0xffff)
2074 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2075 				    cons, rxr->rx_next_cons);
2076 		bnxt_sched_reset_rxr(bp, rxr);
2077 		if (rc1)
2078 			return rc1;
2079 		goto next_rx_no_prod_no_len;
2080 	}
2081 	rx_buf = &rxr->rx_buf_ring[cons];
2082 	data = rx_buf->data;
2083 	data_ptr = rx_buf->data_ptr;
2084 	prefetch(data_ptr);
2085 
2086 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2087 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2088 
2089 	if (agg_bufs) {
2090 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2091 			return -EBUSY;
2092 
2093 		cp_cons = NEXT_CMP(cp_cons);
2094 		*event |= BNXT_AGG_EVENT;
2095 	}
2096 	*event |= BNXT_RX_EVENT;
2097 
2098 	rx_buf->data = NULL;
2099 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2100 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2101 
2102 		bnxt_reuse_rx_data(rxr, cons, data);
2103 		if (agg_bufs)
2104 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2105 					       false);
2106 
2107 		rc = -EIO;
2108 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2109 			bnapi->cp_ring.sw_stats.rx.rx_buf_errors++;
2110 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2111 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2112 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2113 						 rx_err);
2114 				bnxt_sched_reset_rxr(bp, rxr);
2115 			}
2116 		}
2117 		goto next_rx_no_len;
2118 	}
2119 
2120 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2121 	len = flags >> RX_CMP_LEN_SHIFT;
2122 	dma_addr = rx_buf->mapping;
2123 
2124 	if (bnxt_xdp_attached(bp, rxr)) {
2125 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2126 		if (agg_bufs) {
2127 			u32 frag_len = bnxt_rx_agg_pages_xdp(bp, cpr, &xdp,
2128 							     cp_cons, agg_bufs,
2129 							     false);
2130 			if (!frag_len)
2131 				goto oom_next_rx;
2132 		}
2133 		xdp_active = true;
2134 	}
2135 
2136 	if (xdp_active) {
2137 		if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) {
2138 			rc = 1;
2139 			goto next_rx;
2140 		}
2141 	}
2142 
2143 	if (len <= bp->rx_copy_thresh) {
2144 		if (!xdp_active)
2145 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2146 		else
2147 			skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr);
2148 		bnxt_reuse_rx_data(rxr, cons, data);
2149 		if (!skb) {
2150 			if (agg_bufs) {
2151 				if (!xdp_active)
2152 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2153 							       agg_bufs, false);
2154 				else
2155 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2156 			}
2157 			goto oom_next_rx;
2158 		}
2159 	} else {
2160 		u32 payload;
2161 
2162 		if (rx_buf->data_ptr == data_ptr)
2163 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2164 		else
2165 			payload = 0;
2166 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2167 				      payload | len);
2168 		if (!skb)
2169 			goto oom_next_rx;
2170 	}
2171 
2172 	if (agg_bufs) {
2173 		if (!xdp_active) {
2174 			skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, cp_cons, agg_bufs, false);
2175 			if (!skb)
2176 				goto oom_next_rx;
2177 		} else {
2178 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, rxr->page_pool, &xdp, rxcmp1);
2179 			if (!skb) {
2180 				/* we should be able to free the old skb here */
2181 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2182 				goto oom_next_rx;
2183 			}
2184 		}
2185 	}
2186 
2187 	if (RX_CMP_HASH_VALID(rxcmp)) {
2188 		enum pkt_hash_types type;
2189 
2190 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2191 			type = bnxt_rss_ext_op(bp, rxcmp);
2192 		} else {
2193 			u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
2194 
2195 			/* RSS profiles 1 and 3 with extract code 0 for inner
2196 			 * 4-tuple
2197 			 */
2198 			if (hash_type != 1 && hash_type != 3)
2199 				type = PKT_HASH_TYPE_L3;
2200 			else
2201 				type = PKT_HASH_TYPE_L4;
2202 		}
2203 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2204 	}
2205 
2206 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2207 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2208 	skb->protocol = eth_type_trans(skb, dev);
2209 
2210 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2211 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2212 		if (!skb)
2213 			goto next_rx;
2214 	}
2215 
2216 	skb_checksum_none_assert(skb);
2217 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2218 		if (dev->features & NETIF_F_RXCSUM) {
2219 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2220 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2221 		}
2222 	} else {
2223 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2224 			if (dev->features & NETIF_F_RXCSUM)
2225 				bnapi->cp_ring.sw_stats.rx.rx_l4_csum_errors++;
2226 		}
2227 	}
2228 
2229 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2230 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2231 			u64 ns, ts;
2232 
2233 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2234 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2235 
2236 				spin_lock_bh(&ptp->ptp_lock);
2237 				ns = timecounter_cyc2time(&ptp->tc, ts);
2238 				spin_unlock_bh(&ptp->ptp_lock);
2239 				memset(skb_hwtstamps(skb), 0,
2240 				       sizeof(*skb_hwtstamps(skb)));
2241 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2242 			}
2243 		}
2244 	}
2245 	bnxt_deliver_skb(bp, bnapi, skb);
2246 	rc = 1;
2247 
2248 next_rx:
2249 	cpr->rx_packets += 1;
2250 	cpr->rx_bytes += len;
2251 
2252 next_rx_no_len:
2253 	rxr->rx_prod = NEXT_RX(prod);
2254 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2255 
2256 next_rx_no_prod_no_len:
2257 	*raw_cons = tmp_raw_cons;
2258 
2259 	return rc;
2260 
2261 oom_next_rx:
2262 	cpr->bnapi->cp_ring.sw_stats.rx.rx_oom_discards += 1;
2263 	rc = -ENOMEM;
2264 	goto next_rx;
2265 }
2266 
2267 /* In netpoll mode, if we are using a combined completion ring, we need to
2268  * discard the rx packets and recycle the buffers.
2269  */
2270 static int bnxt_force_rx_discard(struct bnxt *bp,
2271 				 struct bnxt_cp_ring_info *cpr,
2272 				 u32 *raw_cons, u8 *event)
2273 {
2274 	u32 tmp_raw_cons = *raw_cons;
2275 	struct rx_cmp_ext *rxcmp1;
2276 	struct rx_cmp *rxcmp;
2277 	u16 cp_cons;
2278 	u8 cmp_type;
2279 	int rc;
2280 
2281 	cp_cons = RING_CMP(tmp_raw_cons);
2282 	rxcmp = (struct rx_cmp *)
2283 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2284 
2285 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2286 	cp_cons = RING_CMP(tmp_raw_cons);
2287 	rxcmp1 = (struct rx_cmp_ext *)
2288 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2289 
2290 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2291 		return -EBUSY;
2292 
2293 	/* The valid test of the entry must be done first before
2294 	 * reading any further.
2295 	 */
2296 	dma_rmb();
2297 	cmp_type = RX_CMP_TYPE(rxcmp);
2298 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2299 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2300 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2301 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2302 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2303 		struct rx_tpa_end_cmp_ext *tpa_end1;
2304 
2305 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2306 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2307 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2308 	}
2309 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2310 	if (rc && rc != -EBUSY)
2311 		cpr->bnapi->cp_ring.sw_stats.rx.rx_netpoll_discards += 1;
2312 	return rc;
2313 }
2314 
2315 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2316 {
2317 	struct bnxt_fw_health *fw_health = bp->fw_health;
2318 	u32 reg = fw_health->regs[reg_idx];
2319 	u32 reg_type, reg_off, val = 0;
2320 
2321 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2322 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2323 	switch (reg_type) {
2324 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2325 		pci_read_config_dword(bp->pdev, reg_off, &val);
2326 		break;
2327 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2328 		reg_off = fw_health->mapped_regs[reg_idx];
2329 		fallthrough;
2330 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2331 		val = readl(bp->bar0 + reg_off);
2332 		break;
2333 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2334 		val = readl(bp->bar1 + reg_off);
2335 		break;
2336 	}
2337 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2338 		val &= fw_health->fw_reset_inprog_reg_mask;
2339 	return val;
2340 }
2341 
2342 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2343 {
2344 	int i;
2345 
2346 	for (i = 0; i < bp->rx_nr_rings; i++) {
2347 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2348 		struct bnxt_ring_grp_info *grp_info;
2349 
2350 		grp_info = &bp->grp_info[grp_idx];
2351 		if (grp_info->agg_fw_ring_id == ring_id)
2352 			return grp_idx;
2353 	}
2354 	return INVALID_HW_RING_ID;
2355 }
2356 
2357 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2358 {
2359 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2360 
2361 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2362 		return link_info->force_link_speed2;
2363 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2364 		return link_info->force_pam4_link_speed;
2365 	return link_info->force_link_speed;
2366 }
2367 
2368 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2369 {
2370 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2371 
2372 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2373 		link_info->req_link_speed = link_info->force_link_speed2;
2374 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2375 		switch (link_info->req_link_speed) {
2376 		case BNXT_LINK_SPEED_50GB_PAM4:
2377 		case BNXT_LINK_SPEED_100GB_PAM4:
2378 		case BNXT_LINK_SPEED_200GB_PAM4:
2379 		case BNXT_LINK_SPEED_400GB_PAM4:
2380 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2381 			break;
2382 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2383 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2384 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2385 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2386 			break;
2387 		default:
2388 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2389 		}
2390 		return;
2391 	}
2392 	link_info->req_link_speed = link_info->force_link_speed;
2393 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2394 	if (link_info->force_pam4_link_speed) {
2395 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2396 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2397 	}
2398 }
2399 
2400 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2401 {
2402 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2403 
2404 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2405 		link_info->advertising = link_info->auto_link_speeds2;
2406 		return;
2407 	}
2408 	link_info->advertising = link_info->auto_link_speeds;
2409 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2410 }
2411 
2412 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2413 {
2414 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2415 
2416 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2417 		if (link_info->req_link_speed != link_info->force_link_speed2)
2418 			return true;
2419 		return false;
2420 	}
2421 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2422 	    link_info->req_link_speed != link_info->force_link_speed)
2423 		return true;
2424 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2425 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2426 		return true;
2427 	return false;
2428 }
2429 
2430 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2431 {
2432 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2433 
2434 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2435 		if (link_info->advertising != link_info->auto_link_speeds2)
2436 			return true;
2437 		return false;
2438 	}
2439 	if (link_info->advertising != link_info->auto_link_speeds ||
2440 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2441 		return true;
2442 	return false;
2443 }
2444 
2445 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2446 	((data2) &							\
2447 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2448 
2449 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2450 	(((data2) &							\
2451 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2452 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2453 
2454 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2455 	((data1) &							\
2456 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2457 
2458 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2459 	(((data1) &							\
2460 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2461 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2462 
2463 /* Return true if the workqueue has to be scheduled */
2464 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2465 {
2466 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2467 
2468 	switch (err_type) {
2469 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2470 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2471 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2472 		break;
2473 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2474 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2475 		break;
2476 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2477 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2478 		break;
2479 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2480 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2481 		char *threshold_type;
2482 		bool notify = false;
2483 		char *dir_str;
2484 
2485 		switch (type) {
2486 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2487 			threshold_type = "warning";
2488 			break;
2489 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2490 			threshold_type = "critical";
2491 			break;
2492 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2493 			threshold_type = "fatal";
2494 			break;
2495 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2496 			threshold_type = "shutdown";
2497 			break;
2498 		default:
2499 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2500 			return false;
2501 		}
2502 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2503 			dir_str = "above";
2504 			notify = true;
2505 		} else {
2506 			dir_str = "below";
2507 		}
2508 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2509 			    dir_str, threshold_type);
2510 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2511 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2512 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2513 		if (notify) {
2514 			bp->thermal_threshold_type = type;
2515 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2516 			return true;
2517 		}
2518 		return false;
2519 	}
2520 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2521 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2522 		break;
2523 	default:
2524 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2525 			   err_type);
2526 		break;
2527 	}
2528 	return false;
2529 }
2530 
2531 #define BNXT_GET_EVENT_PORT(data)	\
2532 	((data) &			\
2533 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2534 
2535 #define BNXT_EVENT_RING_TYPE(data2)	\
2536 	((data2) &			\
2537 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2538 
2539 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2540 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2541 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2542 
2543 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2544 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2545 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2546 
2547 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2548 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2549 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2550 
2551 #define BNXT_PHC_BITS	48
2552 
2553 static int bnxt_async_event_process(struct bnxt *bp,
2554 				    struct hwrm_async_event_cmpl *cmpl)
2555 {
2556 	u16 event_id = le16_to_cpu(cmpl->event_id);
2557 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2558 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2559 
2560 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2561 		   event_id, data1, data2);
2562 
2563 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2564 	switch (event_id) {
2565 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2566 		struct bnxt_link_info *link_info = &bp->link_info;
2567 
2568 		if (BNXT_VF(bp))
2569 			goto async_event_process_exit;
2570 
2571 		/* print unsupported speed warning in forced speed mode only */
2572 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2573 		    (data1 & 0x20000)) {
2574 			u16 fw_speed = bnxt_get_force_speed(link_info);
2575 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2576 
2577 			if (speed != SPEED_UNKNOWN)
2578 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2579 					    speed);
2580 		}
2581 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2582 	}
2583 		fallthrough;
2584 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2585 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2586 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2587 		fallthrough;
2588 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2589 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2590 		break;
2591 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2592 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2593 		break;
2594 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2595 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2596 
2597 		if (BNXT_VF(bp))
2598 			break;
2599 
2600 		if (bp->pf.port_id != port_id)
2601 			break;
2602 
2603 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2604 		break;
2605 	}
2606 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2607 		if (BNXT_PF(bp))
2608 			goto async_event_process_exit;
2609 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2610 		break;
2611 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2612 		char *type_str = "Solicited";
2613 
2614 		if (!bp->fw_health)
2615 			goto async_event_process_exit;
2616 
2617 		bp->fw_reset_timestamp = jiffies;
2618 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2619 		if (!bp->fw_reset_min_dsecs)
2620 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2621 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2622 		if (!bp->fw_reset_max_dsecs)
2623 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2624 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2625 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2626 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2627 			type_str = "Fatal";
2628 			bp->fw_health->fatalities++;
2629 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2630 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2631 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2632 			type_str = "Non-fatal";
2633 			bp->fw_health->survivals++;
2634 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2635 		}
2636 		netif_warn(bp, hw, bp->dev,
2637 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2638 			   type_str, data1, data2,
2639 			   bp->fw_reset_min_dsecs * 100,
2640 			   bp->fw_reset_max_dsecs * 100);
2641 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2642 		break;
2643 	}
2644 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2645 		struct bnxt_fw_health *fw_health = bp->fw_health;
2646 		char *status_desc = "healthy";
2647 		u32 status;
2648 
2649 		if (!fw_health)
2650 			goto async_event_process_exit;
2651 
2652 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2653 			fw_health->enabled = false;
2654 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2655 			break;
2656 		}
2657 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2658 		fw_health->tmr_multiplier =
2659 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2660 				     bp->current_interval * 10);
2661 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2662 		if (!fw_health->enabled)
2663 			fw_health->last_fw_heartbeat =
2664 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2665 		fw_health->last_fw_reset_cnt =
2666 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2667 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2668 		if (status != BNXT_FW_STATUS_HEALTHY)
2669 			status_desc = "unhealthy";
2670 		netif_info(bp, drv, bp->dev,
2671 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2672 			   fw_health->primary ? "primary" : "backup", status,
2673 			   status_desc, fw_health->last_fw_reset_cnt);
2674 		if (!fw_health->enabled) {
2675 			/* Make sure tmr_counter is set and visible to
2676 			 * bnxt_health_check() before setting enabled to true.
2677 			 */
2678 			smp_wmb();
2679 			fw_health->enabled = true;
2680 		}
2681 		goto async_event_process_exit;
2682 	}
2683 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2684 		netif_notice(bp, hw, bp->dev,
2685 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2686 			     data1, data2);
2687 		goto async_event_process_exit;
2688 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2689 		struct bnxt_rx_ring_info *rxr;
2690 		u16 grp_idx;
2691 
2692 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2693 			goto async_event_process_exit;
2694 
2695 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2696 			    BNXT_EVENT_RING_TYPE(data2), data1);
2697 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2698 			goto async_event_process_exit;
2699 
2700 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2701 		if (grp_idx == INVALID_HW_RING_ID) {
2702 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2703 				    data1);
2704 			goto async_event_process_exit;
2705 		}
2706 		rxr = bp->bnapi[grp_idx]->rx_ring;
2707 		bnxt_sched_reset_rxr(bp, rxr);
2708 		goto async_event_process_exit;
2709 	}
2710 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2711 		struct bnxt_fw_health *fw_health = bp->fw_health;
2712 
2713 		netif_notice(bp, hw, bp->dev,
2714 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2715 			     data1, data2);
2716 		if (fw_health) {
2717 			fw_health->echo_req_data1 = data1;
2718 			fw_health->echo_req_data2 = data2;
2719 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2720 			break;
2721 		}
2722 		goto async_event_process_exit;
2723 	}
2724 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2725 		bnxt_ptp_pps_event(bp, data1, data2);
2726 		goto async_event_process_exit;
2727 	}
2728 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2729 		if (bnxt_event_error_report(bp, data1, data2))
2730 			break;
2731 		goto async_event_process_exit;
2732 	}
2733 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2734 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2735 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2736 			if (BNXT_PTP_USE_RTC(bp)) {
2737 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2738 				u64 ns;
2739 
2740 				if (!ptp)
2741 					goto async_event_process_exit;
2742 
2743 				spin_lock_bh(&ptp->ptp_lock);
2744 				bnxt_ptp_update_current_time(bp);
2745 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2746 				       BNXT_PHC_BITS) | ptp->current_time);
2747 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2748 				spin_unlock_bh(&ptp->ptp_lock);
2749 			}
2750 			break;
2751 		}
2752 		goto async_event_process_exit;
2753 	}
2754 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2755 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2756 
2757 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2758 		goto async_event_process_exit;
2759 	}
2760 	default:
2761 		goto async_event_process_exit;
2762 	}
2763 	__bnxt_queue_sp_work(bp);
2764 async_event_process_exit:
2765 	return 0;
2766 }
2767 
2768 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2769 {
2770 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2771 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2772 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2773 				(struct hwrm_fwd_req_cmpl *)txcmp;
2774 
2775 	switch (cmpl_type) {
2776 	case CMPL_BASE_TYPE_HWRM_DONE:
2777 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2778 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2779 		break;
2780 
2781 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2782 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2783 
2784 		if ((vf_id < bp->pf.first_vf_id) ||
2785 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2786 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2787 				   vf_id);
2788 			return -EINVAL;
2789 		}
2790 
2791 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2792 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2793 		break;
2794 
2795 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2796 		bnxt_async_event_process(bp,
2797 					 (struct hwrm_async_event_cmpl *)txcmp);
2798 		break;
2799 
2800 	default:
2801 		break;
2802 	}
2803 
2804 	return 0;
2805 }
2806 
2807 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2808 {
2809 	struct bnxt_napi *bnapi = dev_instance;
2810 	struct bnxt *bp = bnapi->bp;
2811 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2812 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2813 
2814 	cpr->event_ctr++;
2815 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2816 	napi_schedule(&bnapi->napi);
2817 	return IRQ_HANDLED;
2818 }
2819 
2820 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2821 {
2822 	u32 raw_cons = cpr->cp_raw_cons;
2823 	u16 cons = RING_CMP(raw_cons);
2824 	struct tx_cmp *txcmp;
2825 
2826 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2827 
2828 	return TX_CMP_VALID(txcmp, raw_cons);
2829 }
2830 
2831 static irqreturn_t bnxt_inta(int irq, void *dev_instance)
2832 {
2833 	struct bnxt_napi *bnapi = dev_instance;
2834 	struct bnxt *bp = bnapi->bp;
2835 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2836 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2837 	u32 int_status;
2838 
2839 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2840 
2841 	if (!bnxt_has_work(bp, cpr)) {
2842 		int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
2843 		/* return if erroneous interrupt */
2844 		if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
2845 			return IRQ_NONE;
2846 	}
2847 
2848 	/* disable ring IRQ */
2849 	BNXT_CP_DB_IRQ_DIS(cpr->cp_db.doorbell);
2850 
2851 	/* Return here if interrupt is shared and is disabled. */
2852 	if (unlikely(atomic_read(&bp->intr_sem) != 0))
2853 		return IRQ_HANDLED;
2854 
2855 	napi_schedule(&bnapi->napi);
2856 	return IRQ_HANDLED;
2857 }
2858 
2859 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2860 			    int budget)
2861 {
2862 	struct bnxt_napi *bnapi = cpr->bnapi;
2863 	u32 raw_cons = cpr->cp_raw_cons;
2864 	u32 cons;
2865 	int rx_pkts = 0;
2866 	u8 event = 0;
2867 	struct tx_cmp *txcmp;
2868 
2869 	cpr->has_more_work = 0;
2870 	cpr->had_work_done = 1;
2871 	while (1) {
2872 		u8 cmp_type;
2873 		int rc;
2874 
2875 		cons = RING_CMP(raw_cons);
2876 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2877 
2878 		if (!TX_CMP_VALID(txcmp, raw_cons))
2879 			break;
2880 
2881 		/* The valid test of the entry must be done first before
2882 		 * reading any further.
2883 		 */
2884 		dma_rmb();
2885 		cmp_type = TX_CMP_TYPE(txcmp);
2886 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
2887 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
2888 			u32 opaque = txcmp->tx_cmp_opaque;
2889 			struct bnxt_tx_ring_info *txr;
2890 			u16 tx_freed;
2891 
2892 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
2893 			event |= BNXT_TX_CMP_EVENT;
2894 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
2895 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
2896 			else
2897 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
2898 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
2899 				   bp->tx_ring_mask;
2900 			/* return full budget so NAPI will complete. */
2901 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
2902 				rx_pkts = budget;
2903 				raw_cons = NEXT_RAW_CMP(raw_cons);
2904 				if (budget)
2905 					cpr->has_more_work = 1;
2906 				break;
2907 			}
2908 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
2909 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2910 			if (likely(budget))
2911 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2912 			else
2913 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2914 							   &event);
2915 			if (likely(rc >= 0))
2916 				rx_pkts += rc;
2917 			/* Increment rx_pkts when rc is -ENOMEM to count towards
2918 			 * the NAPI budget.  Otherwise, we may potentially loop
2919 			 * here forever if we consistently cannot allocate
2920 			 * buffers.
2921 			 */
2922 			else if (rc == -ENOMEM && budget)
2923 				rx_pkts++;
2924 			else if (rc == -EBUSY)	/* partial completion */
2925 				break;
2926 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
2927 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
2928 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
2929 			bnxt_hwrm_handler(bp, txcmp);
2930 		}
2931 		raw_cons = NEXT_RAW_CMP(raw_cons);
2932 
2933 		if (rx_pkts && rx_pkts == budget) {
2934 			cpr->has_more_work = 1;
2935 			break;
2936 		}
2937 	}
2938 
2939 	if (event & BNXT_REDIRECT_EVENT)
2940 		xdp_do_flush();
2941 
2942 	if (event & BNXT_TX_EVENT) {
2943 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
2944 		u16 prod = txr->tx_prod;
2945 
2946 		/* Sync BD data before updating doorbell */
2947 		wmb();
2948 
2949 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
2950 	}
2951 
2952 	cpr->cp_raw_cons = raw_cons;
2953 	bnapi->events |= event;
2954 	return rx_pkts;
2955 }
2956 
2957 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
2958 				  int budget)
2959 {
2960 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
2961 		bnapi->tx_int(bp, bnapi, budget);
2962 
2963 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
2964 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2965 
2966 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2967 	}
2968 	if (bnapi->events & BNXT_AGG_EVENT) {
2969 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2970 
2971 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2972 	}
2973 	bnapi->events &= BNXT_TX_CMP_EVENT;
2974 }
2975 
2976 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2977 			  int budget)
2978 {
2979 	struct bnxt_napi *bnapi = cpr->bnapi;
2980 	int rx_pkts;
2981 
2982 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
2983 
2984 	/* ACK completion ring before freeing tx ring and producing new
2985 	 * buffers in rx/agg rings to prevent overflowing the completion
2986 	 * ring.
2987 	 */
2988 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
2989 
2990 	__bnxt_poll_work_done(bp, bnapi, budget);
2991 	return rx_pkts;
2992 }
2993 
2994 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
2995 {
2996 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2997 	struct bnxt *bp = bnapi->bp;
2998 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2999 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3000 	struct tx_cmp *txcmp;
3001 	struct rx_cmp_ext *rxcmp1;
3002 	u32 cp_cons, tmp_raw_cons;
3003 	u32 raw_cons = cpr->cp_raw_cons;
3004 	bool flush_xdp = false;
3005 	u32 rx_pkts = 0;
3006 	u8 event = 0;
3007 
3008 	while (1) {
3009 		int rc;
3010 
3011 		cp_cons = RING_CMP(raw_cons);
3012 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3013 
3014 		if (!TX_CMP_VALID(txcmp, raw_cons))
3015 			break;
3016 
3017 		/* The valid test of the entry must be done first before
3018 		 * reading any further.
3019 		 */
3020 		dma_rmb();
3021 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3022 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3023 			cp_cons = RING_CMP(tmp_raw_cons);
3024 			rxcmp1 = (struct rx_cmp_ext *)
3025 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3026 
3027 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3028 				break;
3029 
3030 			/* force an error to recycle the buffer */
3031 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3032 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3033 
3034 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3035 			if (likely(rc == -EIO) && budget)
3036 				rx_pkts++;
3037 			else if (rc == -EBUSY)	/* partial completion */
3038 				break;
3039 			if (event & BNXT_REDIRECT_EVENT)
3040 				flush_xdp = true;
3041 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3042 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3043 			bnxt_hwrm_handler(bp, txcmp);
3044 		} else {
3045 			netdev_err(bp->dev,
3046 				   "Invalid completion received on special ring\n");
3047 		}
3048 		raw_cons = NEXT_RAW_CMP(raw_cons);
3049 
3050 		if (rx_pkts == budget)
3051 			break;
3052 	}
3053 
3054 	cpr->cp_raw_cons = raw_cons;
3055 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3056 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3057 
3058 	if (event & BNXT_AGG_EVENT)
3059 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3060 	if (flush_xdp)
3061 		xdp_do_flush();
3062 
3063 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3064 		napi_complete_done(napi, rx_pkts);
3065 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3066 	}
3067 	return rx_pkts;
3068 }
3069 
3070 static int bnxt_poll(struct napi_struct *napi, int budget)
3071 {
3072 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3073 	struct bnxt *bp = bnapi->bp;
3074 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3075 	int work_done = 0;
3076 
3077 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3078 		napi_complete(napi);
3079 		return 0;
3080 	}
3081 	while (1) {
3082 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3083 
3084 		if (work_done >= budget) {
3085 			if (!budget)
3086 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3087 			break;
3088 		}
3089 
3090 		if (!bnxt_has_work(bp, cpr)) {
3091 			if (napi_complete_done(napi, work_done))
3092 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3093 			break;
3094 		}
3095 	}
3096 	if (bp->flags & BNXT_FLAG_DIM) {
3097 		struct dim_sample dim_sample = {};
3098 
3099 		dim_update_sample(cpr->event_ctr,
3100 				  cpr->rx_packets,
3101 				  cpr->rx_bytes,
3102 				  &dim_sample);
3103 		net_dim(&cpr->dim, dim_sample);
3104 	}
3105 	return work_done;
3106 }
3107 
3108 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3109 {
3110 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3111 	int i, work_done = 0;
3112 
3113 	for (i = 0; i < cpr->cp_ring_count; i++) {
3114 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3115 
3116 		if (cpr2->had_nqe_notify) {
3117 			work_done += __bnxt_poll_work(bp, cpr2,
3118 						      budget - work_done);
3119 			cpr->has_more_work |= cpr2->has_more_work;
3120 		}
3121 	}
3122 	return work_done;
3123 }
3124 
3125 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3126 				 u64 dbr_type, int budget)
3127 {
3128 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3129 	int i;
3130 
3131 	for (i = 0; i < cpr->cp_ring_count; i++) {
3132 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3133 		struct bnxt_db_info *db;
3134 
3135 		if (cpr2->had_work_done) {
3136 			u32 tgl = 0;
3137 
3138 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3139 				cpr2->had_nqe_notify = 0;
3140 				tgl = cpr2->toggle;
3141 			}
3142 			db = &cpr2->cp_db;
3143 			bnxt_writeq(bp,
3144 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3145 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3146 				    db->doorbell);
3147 			cpr2->had_work_done = 0;
3148 		}
3149 	}
3150 	__bnxt_poll_work_done(bp, bnapi, budget);
3151 }
3152 
3153 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3154 {
3155 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3156 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3157 	struct bnxt_cp_ring_info *cpr_rx;
3158 	u32 raw_cons = cpr->cp_raw_cons;
3159 	struct bnxt *bp = bnapi->bp;
3160 	struct nqe_cn *nqcmp;
3161 	int work_done = 0;
3162 	u32 cons;
3163 
3164 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3165 		napi_complete(napi);
3166 		return 0;
3167 	}
3168 	if (cpr->has_more_work) {
3169 		cpr->has_more_work = 0;
3170 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3171 	}
3172 	while (1) {
3173 		u16 type;
3174 
3175 		cons = RING_CMP(raw_cons);
3176 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3177 
3178 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3179 			if (cpr->has_more_work)
3180 				break;
3181 
3182 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3183 					     budget);
3184 			cpr->cp_raw_cons = raw_cons;
3185 			if (napi_complete_done(napi, work_done))
3186 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3187 						  cpr->cp_raw_cons);
3188 			goto poll_done;
3189 		}
3190 
3191 		/* The valid test of the entry must be done first before
3192 		 * reading any further.
3193 		 */
3194 		dma_rmb();
3195 
3196 		type = le16_to_cpu(nqcmp->type);
3197 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3198 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3199 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3200 			struct bnxt_cp_ring_info *cpr2;
3201 
3202 			/* No more budget for RX work */
3203 			if (budget && work_done >= budget &&
3204 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3205 				break;
3206 
3207 			idx = BNXT_NQ_HDL_IDX(idx);
3208 			cpr2 = &cpr->cp_ring_arr[idx];
3209 			cpr2->had_nqe_notify = 1;
3210 			cpr2->toggle = NQE_CN_TOGGLE(type);
3211 			work_done += __bnxt_poll_work(bp, cpr2,
3212 						      budget - work_done);
3213 			cpr->has_more_work |= cpr2->has_more_work;
3214 		} else {
3215 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3216 		}
3217 		raw_cons = NEXT_RAW_CMP(raw_cons);
3218 	}
3219 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3220 	if (raw_cons != cpr->cp_raw_cons) {
3221 		cpr->cp_raw_cons = raw_cons;
3222 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3223 	}
3224 poll_done:
3225 	cpr_rx = &cpr->cp_ring_arr[0];
3226 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3227 	    (bp->flags & BNXT_FLAG_DIM)) {
3228 		struct dim_sample dim_sample = {};
3229 
3230 		dim_update_sample(cpr->event_ctr,
3231 				  cpr_rx->rx_packets,
3232 				  cpr_rx->rx_bytes,
3233 				  &dim_sample);
3234 		net_dim(&cpr->dim, dim_sample);
3235 	}
3236 	return work_done;
3237 }
3238 
3239 static void bnxt_free_tx_skbs(struct bnxt *bp)
3240 {
3241 	int i, max_idx;
3242 	struct pci_dev *pdev = bp->pdev;
3243 
3244 	if (!bp->tx_ring)
3245 		return;
3246 
3247 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3248 	for (i = 0; i < bp->tx_nr_rings; i++) {
3249 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3250 		int j;
3251 
3252 		if (!txr->tx_buf_ring)
3253 			continue;
3254 
3255 		for (j = 0; j < max_idx;) {
3256 			struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
3257 			struct sk_buff *skb;
3258 			int k, last;
3259 
3260 			if (i < bp->tx_nr_rings_xdp &&
3261 			    tx_buf->action == XDP_REDIRECT) {
3262 				dma_unmap_single(&pdev->dev,
3263 					dma_unmap_addr(tx_buf, mapping),
3264 					dma_unmap_len(tx_buf, len),
3265 					DMA_TO_DEVICE);
3266 				xdp_return_frame(tx_buf->xdpf);
3267 				tx_buf->action = 0;
3268 				tx_buf->xdpf = NULL;
3269 				j++;
3270 				continue;
3271 			}
3272 
3273 			skb = tx_buf->skb;
3274 			if (!skb) {
3275 				j++;
3276 				continue;
3277 			}
3278 
3279 			tx_buf->skb = NULL;
3280 
3281 			if (tx_buf->is_push) {
3282 				dev_kfree_skb(skb);
3283 				j += 2;
3284 				continue;
3285 			}
3286 
3287 			dma_unmap_single(&pdev->dev,
3288 					 dma_unmap_addr(tx_buf, mapping),
3289 					 skb_headlen(skb),
3290 					 DMA_TO_DEVICE);
3291 
3292 			last = tx_buf->nr_frags;
3293 			j += 2;
3294 			for (k = 0; k < last; k++, j++) {
3295 				int ring_idx = j & bp->tx_ring_mask;
3296 				skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
3297 
3298 				tx_buf = &txr->tx_buf_ring[ring_idx];
3299 				dma_unmap_page(
3300 					&pdev->dev,
3301 					dma_unmap_addr(tx_buf, mapping),
3302 					skb_frag_size(frag), DMA_TO_DEVICE);
3303 			}
3304 			dev_kfree_skb(skb);
3305 		}
3306 		netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
3307 	}
3308 }
3309 
3310 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, int ring_nr)
3311 {
3312 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
3313 	struct pci_dev *pdev = bp->pdev;
3314 	struct bnxt_tpa_idx_map *map;
3315 	int i, max_idx, max_agg_idx;
3316 
3317 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3318 	max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3319 	if (!rxr->rx_tpa)
3320 		goto skip_rx_tpa_free;
3321 
3322 	for (i = 0; i < bp->max_tpa; i++) {
3323 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3324 		u8 *data = tpa_info->data;
3325 
3326 		if (!data)
3327 			continue;
3328 
3329 		dma_unmap_single_attrs(&pdev->dev, tpa_info->mapping,
3330 				       bp->rx_buf_use_size, bp->rx_dir,
3331 				       DMA_ATTR_WEAK_ORDERING);
3332 
3333 		tpa_info->data = NULL;
3334 
3335 		skb_free_frag(data);
3336 	}
3337 
3338 skip_rx_tpa_free:
3339 	if (!rxr->rx_buf_ring)
3340 		goto skip_rx_buf_free;
3341 
3342 	for (i = 0; i < max_idx; i++) {
3343 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3344 		dma_addr_t mapping = rx_buf->mapping;
3345 		void *data = rx_buf->data;
3346 
3347 		if (!data)
3348 			continue;
3349 
3350 		rx_buf->data = NULL;
3351 		if (BNXT_RX_PAGE_MODE(bp)) {
3352 			page_pool_recycle_direct(rxr->page_pool, data);
3353 		} else {
3354 			dma_unmap_single_attrs(&pdev->dev, mapping,
3355 					       bp->rx_buf_use_size, bp->rx_dir,
3356 					       DMA_ATTR_WEAK_ORDERING);
3357 			skb_free_frag(data);
3358 		}
3359 	}
3360 
3361 skip_rx_buf_free:
3362 	if (!rxr->rx_agg_ring)
3363 		goto skip_rx_agg_free;
3364 
3365 	for (i = 0; i < max_agg_idx; i++) {
3366 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3367 		struct page *page = rx_agg_buf->page;
3368 
3369 		if (!page)
3370 			continue;
3371 
3372 		rx_agg_buf->page = NULL;
3373 		__clear_bit(i, rxr->rx_agg_bmap);
3374 
3375 		page_pool_recycle_direct(rxr->page_pool, page);
3376 	}
3377 
3378 skip_rx_agg_free:
3379 	map = rxr->rx_tpa_idx_map;
3380 	if (map)
3381 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3382 }
3383 
3384 static void bnxt_free_rx_skbs(struct bnxt *bp)
3385 {
3386 	int i;
3387 
3388 	if (!bp->rx_ring)
3389 		return;
3390 
3391 	for (i = 0; i < bp->rx_nr_rings; i++)
3392 		bnxt_free_one_rx_ring_skbs(bp, i);
3393 }
3394 
3395 static void bnxt_free_skbs(struct bnxt *bp)
3396 {
3397 	bnxt_free_tx_skbs(bp);
3398 	bnxt_free_rx_skbs(bp);
3399 }
3400 
3401 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3402 {
3403 	u8 init_val = ctxm->init_value;
3404 	u16 offset = ctxm->init_offset;
3405 	u8 *p2 = p;
3406 	int i;
3407 
3408 	if (!init_val)
3409 		return;
3410 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3411 		memset(p, init_val, len);
3412 		return;
3413 	}
3414 	for (i = 0; i < len; i += ctxm->entry_size)
3415 		*(p2 + i + offset) = init_val;
3416 }
3417 
3418 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3419 {
3420 	struct pci_dev *pdev = bp->pdev;
3421 	int i;
3422 
3423 	if (!rmem->pg_arr)
3424 		goto skip_pages;
3425 
3426 	for (i = 0; i < rmem->nr_pages; i++) {
3427 		if (!rmem->pg_arr[i])
3428 			continue;
3429 
3430 		dma_free_coherent(&pdev->dev, rmem->page_size,
3431 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3432 
3433 		rmem->pg_arr[i] = NULL;
3434 	}
3435 skip_pages:
3436 	if (rmem->pg_tbl) {
3437 		size_t pg_tbl_size = rmem->nr_pages * 8;
3438 
3439 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3440 			pg_tbl_size = rmem->page_size;
3441 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3442 				  rmem->pg_tbl, rmem->pg_tbl_map);
3443 		rmem->pg_tbl = NULL;
3444 	}
3445 	if (rmem->vmem_size && *rmem->vmem) {
3446 		vfree(*rmem->vmem);
3447 		*rmem->vmem = NULL;
3448 	}
3449 }
3450 
3451 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3452 {
3453 	struct pci_dev *pdev = bp->pdev;
3454 	u64 valid_bit = 0;
3455 	int i;
3456 
3457 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3458 		valid_bit = PTU_PTE_VALID;
3459 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3460 		size_t pg_tbl_size = rmem->nr_pages * 8;
3461 
3462 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3463 			pg_tbl_size = rmem->page_size;
3464 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3465 						  &rmem->pg_tbl_map,
3466 						  GFP_KERNEL);
3467 		if (!rmem->pg_tbl)
3468 			return -ENOMEM;
3469 	}
3470 
3471 	for (i = 0; i < rmem->nr_pages; i++) {
3472 		u64 extra_bits = valid_bit;
3473 
3474 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3475 						     rmem->page_size,
3476 						     &rmem->dma_arr[i],
3477 						     GFP_KERNEL);
3478 		if (!rmem->pg_arr[i])
3479 			return -ENOMEM;
3480 
3481 		if (rmem->ctx_mem)
3482 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3483 					  rmem->page_size);
3484 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3485 			if (i == rmem->nr_pages - 2 &&
3486 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3487 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3488 			else if (i == rmem->nr_pages - 1 &&
3489 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3490 				extra_bits |= PTU_PTE_LAST;
3491 			rmem->pg_tbl[i] =
3492 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3493 		}
3494 	}
3495 
3496 	if (rmem->vmem_size) {
3497 		*rmem->vmem = vzalloc(rmem->vmem_size);
3498 		if (!(*rmem->vmem))
3499 			return -ENOMEM;
3500 	}
3501 	return 0;
3502 }
3503 
3504 static void bnxt_free_tpa_info(struct bnxt *bp)
3505 {
3506 	int i, j;
3507 
3508 	for (i = 0; i < bp->rx_nr_rings; i++) {
3509 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3510 
3511 		kfree(rxr->rx_tpa_idx_map);
3512 		rxr->rx_tpa_idx_map = NULL;
3513 		if (rxr->rx_tpa) {
3514 			for (j = 0; j < bp->max_tpa; j++) {
3515 				kfree(rxr->rx_tpa[j].agg_arr);
3516 				rxr->rx_tpa[j].agg_arr = NULL;
3517 			}
3518 		}
3519 		kfree(rxr->rx_tpa);
3520 		rxr->rx_tpa = NULL;
3521 	}
3522 }
3523 
3524 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3525 {
3526 	int i, j;
3527 
3528 	bp->max_tpa = MAX_TPA;
3529 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3530 		if (!bp->max_tpa_v2)
3531 			return 0;
3532 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3533 	}
3534 
3535 	for (i = 0; i < bp->rx_nr_rings; i++) {
3536 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3537 		struct rx_agg_cmp *agg;
3538 
3539 		rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3540 				      GFP_KERNEL);
3541 		if (!rxr->rx_tpa)
3542 			return -ENOMEM;
3543 
3544 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3545 			continue;
3546 		for (j = 0; j < bp->max_tpa; j++) {
3547 			agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3548 			if (!agg)
3549 				return -ENOMEM;
3550 			rxr->rx_tpa[j].agg_arr = agg;
3551 		}
3552 		rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3553 					      GFP_KERNEL);
3554 		if (!rxr->rx_tpa_idx_map)
3555 			return -ENOMEM;
3556 	}
3557 	return 0;
3558 }
3559 
3560 static void bnxt_free_rx_rings(struct bnxt *bp)
3561 {
3562 	int i;
3563 
3564 	if (!bp->rx_ring)
3565 		return;
3566 
3567 	bnxt_free_tpa_info(bp);
3568 	for (i = 0; i < bp->rx_nr_rings; i++) {
3569 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3570 		struct bnxt_ring_struct *ring;
3571 
3572 		if (rxr->xdp_prog)
3573 			bpf_prog_put(rxr->xdp_prog);
3574 
3575 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3576 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3577 
3578 		page_pool_destroy(rxr->page_pool);
3579 		rxr->page_pool = NULL;
3580 
3581 		kfree(rxr->rx_agg_bmap);
3582 		rxr->rx_agg_bmap = NULL;
3583 
3584 		ring = &rxr->rx_ring_struct;
3585 		bnxt_free_ring(bp, &ring->ring_mem);
3586 
3587 		ring = &rxr->rx_agg_ring_struct;
3588 		bnxt_free_ring(bp, &ring->ring_mem);
3589 	}
3590 }
3591 
3592 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3593 				   struct bnxt_rx_ring_info *rxr,
3594 				   int numa_node)
3595 {
3596 	struct page_pool_params pp = { 0 };
3597 
3598 	pp.pool_size = bp->rx_agg_ring_size;
3599 	if (BNXT_RX_PAGE_MODE(bp))
3600 		pp.pool_size += bp->rx_ring_size;
3601 	pp.nid = numa_node;
3602 	pp.napi = &rxr->bnapi->napi;
3603 	pp.netdev = bp->dev;
3604 	pp.dev = &bp->pdev->dev;
3605 	pp.dma_dir = bp->rx_dir;
3606 	pp.max_len = PAGE_SIZE;
3607 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3608 
3609 	rxr->page_pool = page_pool_create(&pp);
3610 	if (IS_ERR(rxr->page_pool)) {
3611 		int err = PTR_ERR(rxr->page_pool);
3612 
3613 		rxr->page_pool = NULL;
3614 		return err;
3615 	}
3616 	return 0;
3617 }
3618 
3619 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3620 {
3621 	int numa_node = dev_to_node(&bp->pdev->dev);
3622 	int i, rc = 0, agg_rings = 0, cpu;
3623 
3624 	if (!bp->rx_ring)
3625 		return -ENOMEM;
3626 
3627 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3628 		agg_rings = 1;
3629 
3630 	for (i = 0; i < bp->rx_nr_rings; i++) {
3631 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3632 		struct bnxt_ring_struct *ring;
3633 		int cpu_node;
3634 
3635 		ring = &rxr->rx_ring_struct;
3636 
3637 		cpu = cpumask_local_spread(i, numa_node);
3638 		cpu_node = cpu_to_node(cpu);
3639 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3640 			   i, cpu_node);
3641 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3642 		if (rc)
3643 			return rc;
3644 
3645 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3646 		if (rc < 0)
3647 			return rc;
3648 
3649 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3650 						MEM_TYPE_PAGE_POOL,
3651 						rxr->page_pool);
3652 		if (rc) {
3653 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3654 			return rc;
3655 		}
3656 
3657 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3658 		if (rc)
3659 			return rc;
3660 
3661 		ring->grp_idx = i;
3662 		if (agg_rings) {
3663 			u16 mem_size;
3664 
3665 			ring = &rxr->rx_agg_ring_struct;
3666 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3667 			if (rc)
3668 				return rc;
3669 
3670 			ring->grp_idx = i;
3671 			rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3672 			mem_size = rxr->rx_agg_bmap_size / 8;
3673 			rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3674 			if (!rxr->rx_agg_bmap)
3675 				return -ENOMEM;
3676 		}
3677 	}
3678 	if (bp->flags & BNXT_FLAG_TPA)
3679 		rc = bnxt_alloc_tpa_info(bp);
3680 	return rc;
3681 }
3682 
3683 static void bnxt_free_tx_rings(struct bnxt *bp)
3684 {
3685 	int i;
3686 	struct pci_dev *pdev = bp->pdev;
3687 
3688 	if (!bp->tx_ring)
3689 		return;
3690 
3691 	for (i = 0; i < bp->tx_nr_rings; i++) {
3692 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3693 		struct bnxt_ring_struct *ring;
3694 
3695 		if (txr->tx_push) {
3696 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3697 					  txr->tx_push, txr->tx_push_mapping);
3698 			txr->tx_push = NULL;
3699 		}
3700 
3701 		ring = &txr->tx_ring_struct;
3702 
3703 		bnxt_free_ring(bp, &ring->ring_mem);
3704 	}
3705 }
3706 
3707 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3708 	((tc) * (bp)->tx_nr_rings_per_tc)
3709 
3710 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3711 	((tx) % (bp)->tx_nr_rings_per_tc)
3712 
3713 #define BNXT_RING_TO_TC(bp, tx)		\
3714 	((tx) / (bp)->tx_nr_rings_per_tc)
3715 
3716 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3717 {
3718 	int i, j, rc;
3719 	struct pci_dev *pdev = bp->pdev;
3720 
3721 	bp->tx_push_size = 0;
3722 	if (bp->tx_push_thresh) {
3723 		int push_size;
3724 
3725 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3726 					bp->tx_push_thresh);
3727 
3728 		if (push_size > 256) {
3729 			push_size = 0;
3730 			bp->tx_push_thresh = 0;
3731 		}
3732 
3733 		bp->tx_push_size = push_size;
3734 	}
3735 
3736 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3737 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3738 		struct bnxt_ring_struct *ring;
3739 		u8 qidx;
3740 
3741 		ring = &txr->tx_ring_struct;
3742 
3743 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3744 		if (rc)
3745 			return rc;
3746 
3747 		ring->grp_idx = txr->bnapi->index;
3748 		if (bp->tx_push_size) {
3749 			dma_addr_t mapping;
3750 
3751 			/* One pre-allocated DMA buffer to backup
3752 			 * TX push operation
3753 			 */
3754 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3755 						bp->tx_push_size,
3756 						&txr->tx_push_mapping,
3757 						GFP_KERNEL);
3758 
3759 			if (!txr->tx_push)
3760 				return -ENOMEM;
3761 
3762 			mapping = txr->tx_push_mapping +
3763 				sizeof(struct tx_push_bd);
3764 			txr->data_mapping = cpu_to_le64(mapping);
3765 		}
3766 		qidx = bp->tc_to_qidx[j];
3767 		ring->queue_id = bp->q_info[qidx].queue_id;
3768 		spin_lock_init(&txr->xdp_tx_lock);
3769 		if (i < bp->tx_nr_rings_xdp)
3770 			continue;
3771 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
3772 			j++;
3773 	}
3774 	return 0;
3775 }
3776 
3777 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
3778 {
3779 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3780 
3781 	kfree(cpr->cp_desc_ring);
3782 	cpr->cp_desc_ring = NULL;
3783 	ring->ring_mem.pg_arr = NULL;
3784 	kfree(cpr->cp_desc_mapping);
3785 	cpr->cp_desc_mapping = NULL;
3786 	ring->ring_mem.dma_arr = NULL;
3787 }
3788 
3789 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
3790 {
3791 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
3792 	if (!cpr->cp_desc_ring)
3793 		return -ENOMEM;
3794 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
3795 				       GFP_KERNEL);
3796 	if (!cpr->cp_desc_mapping)
3797 		return -ENOMEM;
3798 	return 0;
3799 }
3800 
3801 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
3802 {
3803 	int i;
3804 
3805 	if (!bp->bnapi)
3806 		return;
3807 	for (i = 0; i < bp->cp_nr_rings; i++) {
3808 		struct bnxt_napi *bnapi = bp->bnapi[i];
3809 
3810 		if (!bnapi)
3811 			continue;
3812 		bnxt_free_cp_arrays(&bnapi->cp_ring);
3813 	}
3814 }
3815 
3816 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
3817 {
3818 	int i, n = bp->cp_nr_pages;
3819 
3820 	for (i = 0; i < bp->cp_nr_rings; i++) {
3821 		struct bnxt_napi *bnapi = bp->bnapi[i];
3822 		int rc;
3823 
3824 		if (!bnapi)
3825 			continue;
3826 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
3827 		if (rc)
3828 			return rc;
3829 	}
3830 	return 0;
3831 }
3832 
3833 static void bnxt_free_cp_rings(struct bnxt *bp)
3834 {
3835 	int i;
3836 
3837 	if (!bp->bnapi)
3838 		return;
3839 
3840 	for (i = 0; i < bp->cp_nr_rings; i++) {
3841 		struct bnxt_napi *bnapi = bp->bnapi[i];
3842 		struct bnxt_cp_ring_info *cpr;
3843 		struct bnxt_ring_struct *ring;
3844 		int j;
3845 
3846 		if (!bnapi)
3847 			continue;
3848 
3849 		cpr = &bnapi->cp_ring;
3850 		ring = &cpr->cp_ring_struct;
3851 
3852 		bnxt_free_ring(bp, &ring->ring_mem);
3853 
3854 		if (!cpr->cp_ring_arr)
3855 			continue;
3856 
3857 		for (j = 0; j < cpr->cp_ring_count; j++) {
3858 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
3859 
3860 			ring = &cpr2->cp_ring_struct;
3861 			bnxt_free_ring(bp, &ring->ring_mem);
3862 			bnxt_free_cp_arrays(cpr2);
3863 		}
3864 		kfree(cpr->cp_ring_arr);
3865 		cpr->cp_ring_arr = NULL;
3866 		cpr->cp_ring_count = 0;
3867 	}
3868 }
3869 
3870 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
3871 				  struct bnxt_cp_ring_info *cpr)
3872 {
3873 	struct bnxt_ring_mem_info *rmem;
3874 	struct bnxt_ring_struct *ring;
3875 	int rc;
3876 
3877 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
3878 	if (rc) {
3879 		bnxt_free_cp_arrays(cpr);
3880 		return -ENOMEM;
3881 	}
3882 	ring = &cpr->cp_ring_struct;
3883 	rmem = &ring->ring_mem;
3884 	rmem->nr_pages = bp->cp_nr_pages;
3885 	rmem->page_size = HW_CMPD_RING_SIZE;
3886 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
3887 	rmem->dma_arr = cpr->cp_desc_mapping;
3888 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
3889 	rc = bnxt_alloc_ring(bp, rmem);
3890 	if (rc) {
3891 		bnxt_free_ring(bp, rmem);
3892 		bnxt_free_cp_arrays(cpr);
3893 	}
3894 	return rc;
3895 }
3896 
3897 static int bnxt_alloc_cp_rings(struct bnxt *bp)
3898 {
3899 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
3900 	int i, j, rc, ulp_msix;
3901 	int tcs = bp->num_tc;
3902 
3903 	if (!tcs)
3904 		tcs = 1;
3905 	ulp_msix = bnxt_get_ulp_msix_num(bp);
3906 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
3907 		struct bnxt_napi *bnapi = bp->bnapi[i];
3908 		struct bnxt_cp_ring_info *cpr, *cpr2;
3909 		struct bnxt_ring_struct *ring;
3910 		int cp_count = 0, k;
3911 		int rx = 0, tx = 0;
3912 
3913 		if (!bnapi)
3914 			continue;
3915 
3916 		cpr = &bnapi->cp_ring;
3917 		cpr->bnapi = bnapi;
3918 		ring = &cpr->cp_ring_struct;
3919 
3920 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3921 		if (rc)
3922 			return rc;
3923 
3924 		ring->map_idx = ulp_msix + i;
3925 
3926 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3927 			continue;
3928 
3929 		if (i < bp->rx_nr_rings) {
3930 			cp_count++;
3931 			rx = 1;
3932 		}
3933 		if (i < bp->tx_nr_rings_xdp) {
3934 			cp_count++;
3935 			tx = 1;
3936 		} else if ((sh && i < bp->tx_nr_rings) ||
3937 			 (!sh && i >= bp->rx_nr_rings)) {
3938 			cp_count += tcs;
3939 			tx = 1;
3940 		}
3941 
3942 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
3943 					   GFP_KERNEL);
3944 		if (!cpr->cp_ring_arr)
3945 			return -ENOMEM;
3946 		cpr->cp_ring_count = cp_count;
3947 
3948 		for (k = 0; k < cp_count; k++) {
3949 			cpr2 = &cpr->cp_ring_arr[k];
3950 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
3951 			if (rc)
3952 				return rc;
3953 			cpr2->bnapi = bnapi;
3954 			cpr2->cp_idx = k;
3955 			if (!k && rx) {
3956 				bp->rx_ring[i].rx_cpr = cpr2;
3957 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
3958 			} else {
3959 				int n, tc = k - rx;
3960 
3961 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
3962 				bp->tx_ring[n].tx_cpr = cpr2;
3963 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
3964 			}
3965 		}
3966 		if (tx)
3967 			j++;
3968 	}
3969 	return 0;
3970 }
3971 
3972 static void bnxt_init_ring_struct(struct bnxt *bp)
3973 {
3974 	int i, j;
3975 
3976 	for (i = 0; i < bp->cp_nr_rings; i++) {
3977 		struct bnxt_napi *bnapi = bp->bnapi[i];
3978 		struct bnxt_ring_mem_info *rmem;
3979 		struct bnxt_cp_ring_info *cpr;
3980 		struct bnxt_rx_ring_info *rxr;
3981 		struct bnxt_tx_ring_info *txr;
3982 		struct bnxt_ring_struct *ring;
3983 
3984 		if (!bnapi)
3985 			continue;
3986 
3987 		cpr = &bnapi->cp_ring;
3988 		ring = &cpr->cp_ring_struct;
3989 		rmem = &ring->ring_mem;
3990 		rmem->nr_pages = bp->cp_nr_pages;
3991 		rmem->page_size = HW_CMPD_RING_SIZE;
3992 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
3993 		rmem->dma_arr = cpr->cp_desc_mapping;
3994 		rmem->vmem_size = 0;
3995 
3996 		rxr = bnapi->rx_ring;
3997 		if (!rxr)
3998 			goto skip_rx;
3999 
4000 		ring = &rxr->rx_ring_struct;
4001 		rmem = &ring->ring_mem;
4002 		rmem->nr_pages = bp->rx_nr_pages;
4003 		rmem->page_size = HW_RXBD_RING_SIZE;
4004 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4005 		rmem->dma_arr = rxr->rx_desc_mapping;
4006 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4007 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4008 
4009 		ring = &rxr->rx_agg_ring_struct;
4010 		rmem = &ring->ring_mem;
4011 		rmem->nr_pages = bp->rx_agg_nr_pages;
4012 		rmem->page_size = HW_RXBD_RING_SIZE;
4013 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4014 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4015 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4016 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4017 
4018 skip_rx:
4019 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4020 			ring = &txr->tx_ring_struct;
4021 			rmem = &ring->ring_mem;
4022 			rmem->nr_pages = bp->tx_nr_pages;
4023 			rmem->page_size = HW_TXBD_RING_SIZE;
4024 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4025 			rmem->dma_arr = txr->tx_desc_mapping;
4026 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4027 			rmem->vmem = (void **)&txr->tx_buf_ring;
4028 		}
4029 	}
4030 }
4031 
4032 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4033 {
4034 	int i;
4035 	u32 prod;
4036 	struct rx_bd **rx_buf_ring;
4037 
4038 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4039 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4040 		int j;
4041 		struct rx_bd *rxbd;
4042 
4043 		rxbd = rx_buf_ring[i];
4044 		if (!rxbd)
4045 			continue;
4046 
4047 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4048 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4049 			rxbd->rx_bd_opaque = prod;
4050 		}
4051 	}
4052 }
4053 
4054 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4055 {
4056 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4057 	struct net_device *dev = bp->dev;
4058 	u32 prod;
4059 	int i;
4060 
4061 	prod = rxr->rx_prod;
4062 	for (i = 0; i < bp->rx_ring_size; i++) {
4063 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4064 			netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
4065 				    ring_nr, i, bp->rx_ring_size);
4066 			break;
4067 		}
4068 		prod = NEXT_RX(prod);
4069 	}
4070 	rxr->rx_prod = prod;
4071 
4072 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4073 		return 0;
4074 
4075 	prod = rxr->rx_agg_prod;
4076 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4077 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
4078 			netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
4079 				    ring_nr, i, bp->rx_ring_size);
4080 			break;
4081 		}
4082 		prod = NEXT_RX_AGG(prod);
4083 	}
4084 	rxr->rx_agg_prod = prod;
4085 
4086 	if (rxr->rx_tpa) {
4087 		dma_addr_t mapping;
4088 		u8 *data;
4089 
4090 		for (i = 0; i < bp->max_tpa; i++) {
4091 			data = __bnxt_alloc_rx_frag(bp, &mapping, GFP_KERNEL);
4092 			if (!data)
4093 				return -ENOMEM;
4094 
4095 			rxr->rx_tpa[i].data = data;
4096 			rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4097 			rxr->rx_tpa[i].mapping = mapping;
4098 		}
4099 	}
4100 	return 0;
4101 }
4102 
4103 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4104 {
4105 	struct bnxt_rx_ring_info *rxr;
4106 	struct bnxt_ring_struct *ring;
4107 	u32 type;
4108 
4109 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4110 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4111 
4112 	if (NET_IP_ALIGN == 2)
4113 		type |= RX_BD_FLAGS_SOP;
4114 
4115 	rxr = &bp->rx_ring[ring_nr];
4116 	ring = &rxr->rx_ring_struct;
4117 	bnxt_init_rxbd_pages(ring, type);
4118 
4119 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4120 			     &rxr->bnapi->napi);
4121 
4122 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4123 		bpf_prog_add(bp->xdp_prog, 1);
4124 		rxr->xdp_prog = bp->xdp_prog;
4125 	}
4126 	ring->fw_ring_id = INVALID_HW_RING_ID;
4127 
4128 	ring = &rxr->rx_agg_ring_struct;
4129 	ring->fw_ring_id = INVALID_HW_RING_ID;
4130 
4131 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4132 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4133 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4134 
4135 		bnxt_init_rxbd_pages(ring, type);
4136 	}
4137 
4138 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4139 }
4140 
4141 static void bnxt_init_cp_rings(struct bnxt *bp)
4142 {
4143 	int i, j;
4144 
4145 	for (i = 0; i < bp->cp_nr_rings; i++) {
4146 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4147 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4148 
4149 		ring->fw_ring_id = INVALID_HW_RING_ID;
4150 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4151 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4152 		if (!cpr->cp_ring_arr)
4153 			continue;
4154 		for (j = 0; j < cpr->cp_ring_count; j++) {
4155 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4156 
4157 			ring = &cpr2->cp_ring_struct;
4158 			ring->fw_ring_id = INVALID_HW_RING_ID;
4159 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4160 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4161 		}
4162 	}
4163 }
4164 
4165 static int bnxt_init_rx_rings(struct bnxt *bp)
4166 {
4167 	int i, rc = 0;
4168 
4169 	if (BNXT_RX_PAGE_MODE(bp)) {
4170 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4171 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4172 	} else {
4173 		bp->rx_offset = BNXT_RX_OFFSET;
4174 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4175 	}
4176 
4177 	for (i = 0; i < bp->rx_nr_rings; i++) {
4178 		rc = bnxt_init_one_rx_ring(bp, i);
4179 		if (rc)
4180 			break;
4181 	}
4182 
4183 	return rc;
4184 }
4185 
4186 static int bnxt_init_tx_rings(struct bnxt *bp)
4187 {
4188 	u16 i;
4189 
4190 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4191 				   BNXT_MIN_TX_DESC_CNT);
4192 
4193 	for (i = 0; i < bp->tx_nr_rings; i++) {
4194 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4195 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4196 
4197 		ring->fw_ring_id = INVALID_HW_RING_ID;
4198 
4199 		if (i >= bp->tx_nr_rings_xdp)
4200 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4201 					     NETDEV_QUEUE_TYPE_TX,
4202 					     &txr->bnapi->napi);
4203 	}
4204 
4205 	return 0;
4206 }
4207 
4208 static void bnxt_free_ring_grps(struct bnxt *bp)
4209 {
4210 	kfree(bp->grp_info);
4211 	bp->grp_info = NULL;
4212 }
4213 
4214 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4215 {
4216 	int i;
4217 
4218 	if (irq_re_init) {
4219 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4220 				       sizeof(struct bnxt_ring_grp_info),
4221 				       GFP_KERNEL);
4222 		if (!bp->grp_info)
4223 			return -ENOMEM;
4224 	}
4225 	for (i = 0; i < bp->cp_nr_rings; i++) {
4226 		if (irq_re_init)
4227 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4228 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4229 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4230 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4231 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4232 	}
4233 	return 0;
4234 }
4235 
4236 static void bnxt_free_vnics(struct bnxt *bp)
4237 {
4238 	kfree(bp->vnic_info);
4239 	bp->vnic_info = NULL;
4240 	bp->nr_vnics = 0;
4241 }
4242 
4243 static int bnxt_alloc_vnics(struct bnxt *bp)
4244 {
4245 	int num_vnics = 1;
4246 
4247 #ifdef CONFIG_RFS_ACCEL
4248 	if (bp->flags & BNXT_FLAG_RFS) {
4249 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4250 			num_vnics++;
4251 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4252 			num_vnics += bp->rx_nr_rings;
4253 	}
4254 #endif
4255 
4256 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4257 		num_vnics++;
4258 
4259 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4260 				GFP_KERNEL);
4261 	if (!bp->vnic_info)
4262 		return -ENOMEM;
4263 
4264 	bp->nr_vnics = num_vnics;
4265 	return 0;
4266 }
4267 
4268 static void bnxt_init_vnics(struct bnxt *bp)
4269 {
4270 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4271 	int i;
4272 
4273 	for (i = 0; i < bp->nr_vnics; i++) {
4274 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4275 		int j;
4276 
4277 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4278 		vnic->vnic_id = i;
4279 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4280 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4281 
4282 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4283 
4284 		if (bp->vnic_info[i].rss_hash_key) {
4285 			if (i == BNXT_VNIC_DEFAULT) {
4286 				u8 *key = (void *)vnic->rss_hash_key;
4287 				int k;
4288 
4289 				if (!bp->rss_hash_key_valid &&
4290 				    !bp->rss_hash_key_updated) {
4291 					get_random_bytes(bp->rss_hash_key,
4292 							 HW_HASH_KEY_SIZE);
4293 					bp->rss_hash_key_updated = true;
4294 				}
4295 
4296 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4297 				       HW_HASH_KEY_SIZE);
4298 
4299 				if (!bp->rss_hash_key_updated)
4300 					continue;
4301 
4302 				bp->rss_hash_key_updated = false;
4303 				bp->rss_hash_key_valid = true;
4304 
4305 				bp->toeplitz_prefix = 0;
4306 				for (k = 0; k < 8; k++) {
4307 					bp->toeplitz_prefix <<= 8;
4308 					bp->toeplitz_prefix |= key[k];
4309 				}
4310 			} else {
4311 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4312 				       HW_HASH_KEY_SIZE);
4313 			}
4314 		}
4315 	}
4316 }
4317 
4318 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4319 {
4320 	int pages;
4321 
4322 	pages = ring_size / desc_per_pg;
4323 
4324 	if (!pages)
4325 		return 1;
4326 
4327 	pages++;
4328 
4329 	while (pages & (pages - 1))
4330 		pages++;
4331 
4332 	return pages;
4333 }
4334 
4335 void bnxt_set_tpa_flags(struct bnxt *bp)
4336 {
4337 	bp->flags &= ~BNXT_FLAG_TPA;
4338 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4339 		return;
4340 	if (bp->dev->features & NETIF_F_LRO)
4341 		bp->flags |= BNXT_FLAG_LRO;
4342 	else if (bp->dev->features & NETIF_F_GRO_HW)
4343 		bp->flags |= BNXT_FLAG_GRO;
4344 }
4345 
4346 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4347  * be set on entry.
4348  */
4349 void bnxt_set_ring_params(struct bnxt *bp)
4350 {
4351 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4352 	u32 agg_factor = 0, agg_ring_size = 0;
4353 
4354 	/* 8 for CRC and VLAN */
4355 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4356 
4357 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4358 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4359 
4360 	bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
4361 	ring_size = bp->rx_ring_size;
4362 	bp->rx_agg_ring_size = 0;
4363 	bp->rx_agg_nr_pages = 0;
4364 
4365 	if (bp->flags & BNXT_FLAG_TPA)
4366 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4367 
4368 	bp->flags &= ~BNXT_FLAG_JUMBO;
4369 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4370 		u32 jumbo_factor;
4371 
4372 		bp->flags |= BNXT_FLAG_JUMBO;
4373 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4374 		if (jumbo_factor > agg_factor)
4375 			agg_factor = jumbo_factor;
4376 	}
4377 	if (agg_factor) {
4378 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4379 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4380 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4381 				    bp->rx_ring_size, ring_size);
4382 			bp->rx_ring_size = ring_size;
4383 		}
4384 		agg_ring_size = ring_size * agg_factor;
4385 
4386 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4387 							RX_DESC_CNT);
4388 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4389 			u32 tmp = agg_ring_size;
4390 
4391 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4392 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4393 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4394 				    tmp, agg_ring_size);
4395 		}
4396 		bp->rx_agg_ring_size = agg_ring_size;
4397 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4398 
4399 		if (BNXT_RX_PAGE_MODE(bp)) {
4400 			rx_space = PAGE_SIZE;
4401 			rx_size = PAGE_SIZE -
4402 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4403 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4404 		} else {
4405 			rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
4406 			rx_space = rx_size + NET_SKB_PAD +
4407 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4408 		}
4409 	}
4410 
4411 	bp->rx_buf_use_size = rx_size;
4412 	bp->rx_buf_size = rx_space;
4413 
4414 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4415 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4416 
4417 	ring_size = bp->tx_ring_size;
4418 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4419 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4420 
4421 	max_rx_cmpl = bp->rx_ring_size;
4422 	/* MAX TPA needs to be added because TPA_START completions are
4423 	 * immediately recycled, so the TPA completions are not bound by
4424 	 * the RX ring size.
4425 	 */
4426 	if (bp->flags & BNXT_FLAG_TPA)
4427 		max_rx_cmpl += bp->max_tpa;
4428 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4429 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4430 	bp->cp_ring_size = ring_size;
4431 
4432 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4433 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4434 		bp->cp_nr_pages = MAX_CP_PAGES;
4435 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4436 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4437 			    ring_size, bp->cp_ring_size);
4438 	}
4439 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4440 	bp->cp_ring_mask = bp->cp_bit - 1;
4441 }
4442 
4443 /* Changing allocation mode of RX rings.
4444  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4445  */
4446 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4447 {
4448 	struct net_device *dev = bp->dev;
4449 
4450 	if (page_mode) {
4451 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
4452 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4453 
4454 		if (bp->xdp_prog->aux->xdp_has_frags)
4455 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4456 		else
4457 			dev->max_mtu =
4458 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4459 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4460 			bp->flags |= BNXT_FLAG_JUMBO;
4461 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4462 		} else {
4463 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4464 			bp->rx_skb_func = bnxt_rx_page_skb;
4465 		}
4466 		bp->rx_dir = DMA_BIDIRECTIONAL;
4467 		/* Disable LRO or GRO_HW */
4468 		netdev_update_features(dev);
4469 	} else {
4470 		dev->max_mtu = bp->max_mtu;
4471 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4472 		bp->rx_dir = DMA_FROM_DEVICE;
4473 		bp->rx_skb_func = bnxt_rx_skb;
4474 	}
4475 	return 0;
4476 }
4477 
4478 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4479 {
4480 	int i;
4481 	struct bnxt_vnic_info *vnic;
4482 	struct pci_dev *pdev = bp->pdev;
4483 
4484 	if (!bp->vnic_info)
4485 		return;
4486 
4487 	for (i = 0; i < bp->nr_vnics; i++) {
4488 		vnic = &bp->vnic_info[i];
4489 
4490 		kfree(vnic->fw_grp_ids);
4491 		vnic->fw_grp_ids = NULL;
4492 
4493 		kfree(vnic->uc_list);
4494 		vnic->uc_list = NULL;
4495 
4496 		if (vnic->mc_list) {
4497 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4498 					  vnic->mc_list, vnic->mc_list_mapping);
4499 			vnic->mc_list = NULL;
4500 		}
4501 
4502 		if (vnic->rss_table) {
4503 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4504 					  vnic->rss_table,
4505 					  vnic->rss_table_dma_addr);
4506 			vnic->rss_table = NULL;
4507 		}
4508 
4509 		vnic->rss_hash_key = NULL;
4510 		vnic->flags = 0;
4511 	}
4512 }
4513 
4514 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4515 {
4516 	int i, rc = 0, size;
4517 	struct bnxt_vnic_info *vnic;
4518 	struct pci_dev *pdev = bp->pdev;
4519 	int max_rings;
4520 
4521 	for (i = 0; i < bp->nr_vnics; i++) {
4522 		vnic = &bp->vnic_info[i];
4523 
4524 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4525 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4526 
4527 			if (mem_size > 0) {
4528 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4529 				if (!vnic->uc_list) {
4530 					rc = -ENOMEM;
4531 					goto out;
4532 				}
4533 			}
4534 		}
4535 
4536 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4537 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4538 			vnic->mc_list =
4539 				dma_alloc_coherent(&pdev->dev,
4540 						   vnic->mc_list_size,
4541 						   &vnic->mc_list_mapping,
4542 						   GFP_KERNEL);
4543 			if (!vnic->mc_list) {
4544 				rc = -ENOMEM;
4545 				goto out;
4546 			}
4547 		}
4548 
4549 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4550 			goto vnic_skip_grps;
4551 
4552 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4553 			max_rings = bp->rx_nr_rings;
4554 		else
4555 			max_rings = 1;
4556 
4557 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4558 		if (!vnic->fw_grp_ids) {
4559 			rc = -ENOMEM;
4560 			goto out;
4561 		}
4562 vnic_skip_grps:
4563 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4564 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4565 			continue;
4566 
4567 		/* Allocate rss table and hash key */
4568 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4569 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4570 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4571 
4572 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4573 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4574 						     vnic->rss_table_size,
4575 						     &vnic->rss_table_dma_addr,
4576 						     GFP_KERNEL);
4577 		if (!vnic->rss_table) {
4578 			rc = -ENOMEM;
4579 			goto out;
4580 		}
4581 
4582 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4583 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4584 	}
4585 	return 0;
4586 
4587 out:
4588 	return rc;
4589 }
4590 
4591 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4592 {
4593 	struct bnxt_hwrm_wait_token *token;
4594 
4595 	dma_pool_destroy(bp->hwrm_dma_pool);
4596 	bp->hwrm_dma_pool = NULL;
4597 
4598 	rcu_read_lock();
4599 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4600 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4601 	rcu_read_unlock();
4602 }
4603 
4604 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4605 {
4606 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4607 					    BNXT_HWRM_DMA_SIZE,
4608 					    BNXT_HWRM_DMA_ALIGN, 0);
4609 	if (!bp->hwrm_dma_pool)
4610 		return -ENOMEM;
4611 
4612 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4613 
4614 	return 0;
4615 }
4616 
4617 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4618 {
4619 	kfree(stats->hw_masks);
4620 	stats->hw_masks = NULL;
4621 	kfree(stats->sw_stats);
4622 	stats->sw_stats = NULL;
4623 	if (stats->hw_stats) {
4624 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
4625 				  stats->hw_stats_map);
4626 		stats->hw_stats = NULL;
4627 	}
4628 }
4629 
4630 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
4631 				bool alloc_masks)
4632 {
4633 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
4634 					     &stats->hw_stats_map, GFP_KERNEL);
4635 	if (!stats->hw_stats)
4636 		return -ENOMEM;
4637 
4638 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
4639 	if (!stats->sw_stats)
4640 		goto stats_mem_err;
4641 
4642 	if (alloc_masks) {
4643 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
4644 		if (!stats->hw_masks)
4645 			goto stats_mem_err;
4646 	}
4647 	return 0;
4648 
4649 stats_mem_err:
4650 	bnxt_free_stats_mem(bp, stats);
4651 	return -ENOMEM;
4652 }
4653 
4654 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
4655 {
4656 	int i;
4657 
4658 	for (i = 0; i < count; i++)
4659 		mask_arr[i] = mask;
4660 }
4661 
4662 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
4663 {
4664 	int i;
4665 
4666 	for (i = 0; i < count; i++)
4667 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
4668 }
4669 
4670 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
4671 				    struct bnxt_stats_mem *stats)
4672 {
4673 	struct hwrm_func_qstats_ext_output *resp;
4674 	struct hwrm_func_qstats_ext_input *req;
4675 	__le64 *hw_masks;
4676 	int rc;
4677 
4678 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
4679 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4680 		return -EOPNOTSUPP;
4681 
4682 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
4683 	if (rc)
4684 		return rc;
4685 
4686 	req->fid = cpu_to_le16(0xffff);
4687 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4688 
4689 	resp = hwrm_req_hold(bp, req);
4690 	rc = hwrm_req_send(bp, req);
4691 	if (!rc) {
4692 		hw_masks = &resp->rx_ucast_pkts;
4693 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
4694 	}
4695 	hwrm_req_drop(bp, req);
4696 	return rc;
4697 }
4698 
4699 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
4700 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
4701 
4702 static void bnxt_init_stats(struct bnxt *bp)
4703 {
4704 	struct bnxt_napi *bnapi = bp->bnapi[0];
4705 	struct bnxt_cp_ring_info *cpr;
4706 	struct bnxt_stats_mem *stats;
4707 	__le64 *rx_stats, *tx_stats;
4708 	int rc, rx_count, tx_count;
4709 	u64 *rx_masks, *tx_masks;
4710 	u64 mask;
4711 	u8 flags;
4712 
4713 	cpr = &bnapi->cp_ring;
4714 	stats = &cpr->stats;
4715 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
4716 	if (rc) {
4717 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4718 			mask = (1ULL << 48) - 1;
4719 		else
4720 			mask = -1ULL;
4721 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
4722 	}
4723 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
4724 		stats = &bp->port_stats;
4725 		rx_stats = stats->hw_stats;
4726 		rx_masks = stats->hw_masks;
4727 		rx_count = sizeof(struct rx_port_stats) / 8;
4728 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4729 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4730 		tx_count = sizeof(struct tx_port_stats) / 8;
4731 
4732 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
4733 		rc = bnxt_hwrm_port_qstats(bp, flags);
4734 		if (rc) {
4735 			mask = (1ULL << 40) - 1;
4736 
4737 			bnxt_fill_masks(rx_masks, mask, rx_count);
4738 			bnxt_fill_masks(tx_masks, mask, tx_count);
4739 		} else {
4740 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4741 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
4742 			bnxt_hwrm_port_qstats(bp, 0);
4743 		}
4744 	}
4745 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
4746 		stats = &bp->rx_port_stats_ext;
4747 		rx_stats = stats->hw_stats;
4748 		rx_masks = stats->hw_masks;
4749 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
4750 		stats = &bp->tx_port_stats_ext;
4751 		tx_stats = stats->hw_stats;
4752 		tx_masks = stats->hw_masks;
4753 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
4754 
4755 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4756 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
4757 		if (rc) {
4758 			mask = (1ULL << 40) - 1;
4759 
4760 			bnxt_fill_masks(rx_masks, mask, rx_count);
4761 			if (tx_stats)
4762 				bnxt_fill_masks(tx_masks, mask, tx_count);
4763 		} else {
4764 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4765 			if (tx_stats)
4766 				bnxt_copy_hw_masks(tx_masks, tx_stats,
4767 						   tx_count);
4768 			bnxt_hwrm_port_qstats_ext(bp, 0);
4769 		}
4770 	}
4771 }
4772 
4773 static void bnxt_free_port_stats(struct bnxt *bp)
4774 {
4775 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
4776 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
4777 
4778 	bnxt_free_stats_mem(bp, &bp->port_stats);
4779 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
4780 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
4781 }
4782 
4783 static void bnxt_free_ring_stats(struct bnxt *bp)
4784 {
4785 	int i;
4786 
4787 	if (!bp->bnapi)
4788 		return;
4789 
4790 	for (i = 0; i < bp->cp_nr_rings; i++) {
4791 		struct bnxt_napi *bnapi = bp->bnapi[i];
4792 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4793 
4794 		bnxt_free_stats_mem(bp, &cpr->stats);
4795 	}
4796 }
4797 
4798 static int bnxt_alloc_stats(struct bnxt *bp)
4799 {
4800 	u32 size, i;
4801 	int rc;
4802 
4803 	size = bp->hw_ring_stats_size;
4804 
4805 	for (i = 0; i < bp->cp_nr_rings; i++) {
4806 		struct bnxt_napi *bnapi = bp->bnapi[i];
4807 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4808 
4809 		cpr->stats.len = size;
4810 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
4811 		if (rc)
4812 			return rc;
4813 
4814 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4815 	}
4816 
4817 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
4818 		return 0;
4819 
4820 	if (bp->port_stats.hw_stats)
4821 		goto alloc_ext_stats;
4822 
4823 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
4824 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
4825 	if (rc)
4826 		return rc;
4827 
4828 	bp->flags |= BNXT_FLAG_PORT_STATS;
4829 
4830 alloc_ext_stats:
4831 	/* Display extended statistics only if FW supports it */
4832 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
4833 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
4834 			return 0;
4835 
4836 	if (bp->rx_port_stats_ext.hw_stats)
4837 		goto alloc_tx_ext_stats;
4838 
4839 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
4840 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
4841 	/* Extended stats are optional */
4842 	if (rc)
4843 		return 0;
4844 
4845 alloc_tx_ext_stats:
4846 	if (bp->tx_port_stats_ext.hw_stats)
4847 		return 0;
4848 
4849 	if (bp->hwrm_spec_code >= 0x10902 ||
4850 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
4851 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
4852 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
4853 		/* Extended stats are optional */
4854 		if (rc)
4855 			return 0;
4856 	}
4857 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
4858 	return 0;
4859 }
4860 
4861 static void bnxt_clear_ring_indices(struct bnxt *bp)
4862 {
4863 	int i, j;
4864 
4865 	if (!bp->bnapi)
4866 		return;
4867 
4868 	for (i = 0; i < bp->cp_nr_rings; i++) {
4869 		struct bnxt_napi *bnapi = bp->bnapi[i];
4870 		struct bnxt_cp_ring_info *cpr;
4871 		struct bnxt_rx_ring_info *rxr;
4872 		struct bnxt_tx_ring_info *txr;
4873 
4874 		if (!bnapi)
4875 			continue;
4876 
4877 		cpr = &bnapi->cp_ring;
4878 		cpr->cp_raw_cons = 0;
4879 
4880 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4881 			txr->tx_prod = 0;
4882 			txr->tx_cons = 0;
4883 			txr->tx_hw_cons = 0;
4884 		}
4885 
4886 		rxr = bnapi->rx_ring;
4887 		if (rxr) {
4888 			rxr->rx_prod = 0;
4889 			rxr->rx_agg_prod = 0;
4890 			rxr->rx_sw_agg_prod = 0;
4891 			rxr->rx_next_cons = 0;
4892 		}
4893 		bnapi->events = 0;
4894 	}
4895 }
4896 
4897 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
4898 {
4899 	u8 type = fltr->type, flags = fltr->flags;
4900 
4901 	INIT_LIST_HEAD(&fltr->list);
4902 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
4903 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
4904 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
4905 }
4906 
4907 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
4908 {
4909 	if (!list_empty(&fltr->list))
4910 		list_del_init(&fltr->list);
4911 }
4912 
4913 void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
4914 {
4915 	struct bnxt_filter_base *usr_fltr, *tmp;
4916 
4917 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
4918 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
4919 			continue;
4920 		bnxt_del_one_usr_fltr(bp, usr_fltr);
4921 	}
4922 }
4923 
4924 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
4925 {
4926 	hlist_del(&fltr->hash);
4927 	bnxt_del_one_usr_fltr(bp, fltr);
4928 	if (fltr->flags) {
4929 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
4930 		bp->ntp_fltr_count--;
4931 	}
4932 	kfree(fltr);
4933 }
4934 
4935 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
4936 {
4937 	int i;
4938 
4939 	/* Under rtnl_lock and all our NAPIs have been disabled.  It's
4940 	 * safe to delete the hash table.
4941 	 */
4942 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
4943 		struct hlist_head *head;
4944 		struct hlist_node *tmp;
4945 		struct bnxt_ntuple_filter *fltr;
4946 
4947 		head = &bp->ntp_fltr_hash_tbl[i];
4948 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
4949 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
4950 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
4951 				     !list_empty(&fltr->base.list)))
4952 				continue;
4953 			bnxt_del_fltr(bp, &fltr->base);
4954 		}
4955 	}
4956 	if (!all)
4957 		return;
4958 
4959 	bitmap_free(bp->ntp_fltr_bmap);
4960 	bp->ntp_fltr_bmap = NULL;
4961 	bp->ntp_fltr_count = 0;
4962 }
4963 
4964 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
4965 {
4966 	int i, rc = 0;
4967 
4968 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
4969 		return 0;
4970 
4971 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
4972 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
4973 
4974 	bp->ntp_fltr_count = 0;
4975 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
4976 
4977 	if (!bp->ntp_fltr_bmap)
4978 		rc = -ENOMEM;
4979 
4980 	return rc;
4981 }
4982 
4983 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
4984 {
4985 	int i;
4986 
4987 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
4988 		struct hlist_head *head;
4989 		struct hlist_node *tmp;
4990 		struct bnxt_l2_filter *fltr;
4991 
4992 		head = &bp->l2_fltr_hash_tbl[i];
4993 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
4994 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
4995 				     !list_empty(&fltr->base.list)))
4996 				continue;
4997 			bnxt_del_fltr(bp, &fltr->base);
4998 		}
4999 	}
5000 }
5001 
5002 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5003 {
5004 	int i;
5005 
5006 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5007 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5008 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5009 }
5010 
5011 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5012 {
5013 	bnxt_free_vnic_attributes(bp);
5014 	bnxt_free_tx_rings(bp);
5015 	bnxt_free_rx_rings(bp);
5016 	bnxt_free_cp_rings(bp);
5017 	bnxt_free_all_cp_arrays(bp);
5018 	bnxt_free_ntp_fltrs(bp, false);
5019 	bnxt_free_l2_filters(bp, false);
5020 	if (irq_re_init) {
5021 		bnxt_free_ring_stats(bp);
5022 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5023 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5024 			bnxt_free_port_stats(bp);
5025 		bnxt_free_ring_grps(bp);
5026 		bnxt_free_vnics(bp);
5027 		kfree(bp->tx_ring_map);
5028 		bp->tx_ring_map = NULL;
5029 		kfree(bp->tx_ring);
5030 		bp->tx_ring = NULL;
5031 		kfree(bp->rx_ring);
5032 		bp->rx_ring = NULL;
5033 		kfree(bp->bnapi);
5034 		bp->bnapi = NULL;
5035 	} else {
5036 		bnxt_clear_ring_indices(bp);
5037 	}
5038 }
5039 
5040 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5041 {
5042 	int i, j, rc, size, arr_size;
5043 	void *bnapi;
5044 
5045 	if (irq_re_init) {
5046 		/* Allocate bnapi mem pointer array and mem block for
5047 		 * all queues
5048 		 */
5049 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5050 				bp->cp_nr_rings);
5051 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5052 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5053 		if (!bnapi)
5054 			return -ENOMEM;
5055 
5056 		bp->bnapi = bnapi;
5057 		bnapi += arr_size;
5058 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5059 			bp->bnapi[i] = bnapi;
5060 			bp->bnapi[i]->index = i;
5061 			bp->bnapi[i]->bp = bp;
5062 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5063 				struct bnxt_cp_ring_info *cpr =
5064 					&bp->bnapi[i]->cp_ring;
5065 
5066 				cpr->cp_ring_struct.ring_mem.flags =
5067 					BNXT_RMEM_RING_PTE_FLAG;
5068 			}
5069 		}
5070 
5071 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5072 				      sizeof(struct bnxt_rx_ring_info),
5073 				      GFP_KERNEL);
5074 		if (!bp->rx_ring)
5075 			return -ENOMEM;
5076 
5077 		for (i = 0; i < bp->rx_nr_rings; i++) {
5078 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5079 
5080 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5081 				rxr->rx_ring_struct.ring_mem.flags =
5082 					BNXT_RMEM_RING_PTE_FLAG;
5083 				rxr->rx_agg_ring_struct.ring_mem.flags =
5084 					BNXT_RMEM_RING_PTE_FLAG;
5085 			} else {
5086 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5087 			}
5088 			rxr->bnapi = bp->bnapi[i];
5089 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5090 		}
5091 
5092 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5093 				      sizeof(struct bnxt_tx_ring_info),
5094 				      GFP_KERNEL);
5095 		if (!bp->tx_ring)
5096 			return -ENOMEM;
5097 
5098 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5099 					  GFP_KERNEL);
5100 
5101 		if (!bp->tx_ring_map)
5102 			return -ENOMEM;
5103 
5104 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5105 			j = 0;
5106 		else
5107 			j = bp->rx_nr_rings;
5108 
5109 		for (i = 0; i < bp->tx_nr_rings; i++) {
5110 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5111 			struct bnxt_napi *bnapi2;
5112 
5113 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5114 				txr->tx_ring_struct.ring_mem.flags =
5115 					BNXT_RMEM_RING_PTE_FLAG;
5116 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5117 			if (i >= bp->tx_nr_rings_xdp) {
5118 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5119 
5120 				bnapi2 = bp->bnapi[k];
5121 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5122 				txr->tx_napi_idx =
5123 					BNXT_RING_TO_TC(bp, txr->txq_index);
5124 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5125 				bnapi2->tx_int = bnxt_tx_int;
5126 			} else {
5127 				bnapi2 = bp->bnapi[j];
5128 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5129 				bnapi2->tx_ring[0] = txr;
5130 				bnapi2->tx_int = bnxt_tx_int_xdp;
5131 				j++;
5132 			}
5133 			txr->bnapi = bnapi2;
5134 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5135 				txr->tx_cpr = &bnapi2->cp_ring;
5136 		}
5137 
5138 		rc = bnxt_alloc_stats(bp);
5139 		if (rc)
5140 			goto alloc_mem_err;
5141 		bnxt_init_stats(bp);
5142 
5143 		rc = bnxt_alloc_ntp_fltrs(bp);
5144 		if (rc)
5145 			goto alloc_mem_err;
5146 
5147 		rc = bnxt_alloc_vnics(bp);
5148 		if (rc)
5149 			goto alloc_mem_err;
5150 	}
5151 
5152 	rc = bnxt_alloc_all_cp_arrays(bp);
5153 	if (rc)
5154 		goto alloc_mem_err;
5155 
5156 	bnxt_init_ring_struct(bp);
5157 
5158 	rc = bnxt_alloc_rx_rings(bp);
5159 	if (rc)
5160 		goto alloc_mem_err;
5161 
5162 	rc = bnxt_alloc_tx_rings(bp);
5163 	if (rc)
5164 		goto alloc_mem_err;
5165 
5166 	rc = bnxt_alloc_cp_rings(bp);
5167 	if (rc)
5168 		goto alloc_mem_err;
5169 
5170 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5171 						  BNXT_VNIC_MCAST_FLAG |
5172 						  BNXT_VNIC_UCAST_FLAG;
5173 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5174 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5175 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5176 
5177 	rc = bnxt_alloc_vnic_attributes(bp);
5178 	if (rc)
5179 		goto alloc_mem_err;
5180 	return 0;
5181 
5182 alloc_mem_err:
5183 	bnxt_free_mem(bp, true);
5184 	return rc;
5185 }
5186 
5187 static void bnxt_disable_int(struct bnxt *bp)
5188 {
5189 	int i;
5190 
5191 	if (!bp->bnapi)
5192 		return;
5193 
5194 	for (i = 0; i < bp->cp_nr_rings; i++) {
5195 		struct bnxt_napi *bnapi = bp->bnapi[i];
5196 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5197 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5198 
5199 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5200 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5201 	}
5202 }
5203 
5204 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5205 {
5206 	struct bnxt_napi *bnapi = bp->bnapi[n];
5207 	struct bnxt_cp_ring_info *cpr;
5208 
5209 	cpr = &bnapi->cp_ring;
5210 	return cpr->cp_ring_struct.map_idx;
5211 }
5212 
5213 static void bnxt_disable_int_sync(struct bnxt *bp)
5214 {
5215 	int i;
5216 
5217 	if (!bp->irq_tbl)
5218 		return;
5219 
5220 	atomic_inc(&bp->intr_sem);
5221 
5222 	bnxt_disable_int(bp);
5223 	for (i = 0; i < bp->cp_nr_rings; i++) {
5224 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5225 
5226 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5227 	}
5228 }
5229 
5230 static void bnxt_enable_int(struct bnxt *bp)
5231 {
5232 	int i;
5233 
5234 	atomic_set(&bp->intr_sem, 0);
5235 	for (i = 0; i < bp->cp_nr_rings; i++) {
5236 		struct bnxt_napi *bnapi = bp->bnapi[i];
5237 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5238 
5239 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5240 	}
5241 }
5242 
5243 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5244 			    bool async_only)
5245 {
5246 	DECLARE_BITMAP(async_events_bmap, 256);
5247 	u32 *events = (u32 *)async_events_bmap;
5248 	struct hwrm_func_drv_rgtr_output *resp;
5249 	struct hwrm_func_drv_rgtr_input *req;
5250 	u32 flags;
5251 	int rc, i;
5252 
5253 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5254 	if (rc)
5255 		return rc;
5256 
5257 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5258 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5259 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5260 
5261 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5262 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5263 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5264 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5265 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5266 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5267 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5268 	req->flags = cpu_to_le32(flags);
5269 	req->ver_maj_8b = DRV_VER_MAJ;
5270 	req->ver_min_8b = DRV_VER_MIN;
5271 	req->ver_upd_8b = DRV_VER_UPD;
5272 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5273 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5274 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5275 
5276 	if (BNXT_PF(bp)) {
5277 		u32 data[8];
5278 		int i;
5279 
5280 		memset(data, 0, sizeof(data));
5281 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5282 			u16 cmd = bnxt_vf_req_snif[i];
5283 			unsigned int bit, idx;
5284 
5285 			idx = cmd / 32;
5286 			bit = cmd % 32;
5287 			data[idx] |= 1 << bit;
5288 		}
5289 
5290 		for (i = 0; i < 8; i++)
5291 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5292 
5293 		req->enables |=
5294 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5295 	}
5296 
5297 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5298 		req->flags |= cpu_to_le32(
5299 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5300 
5301 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5302 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5303 		u16 event_id = bnxt_async_events_arr[i];
5304 
5305 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5306 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5307 			continue;
5308 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5309 		    !bp->ptp_cfg)
5310 			continue;
5311 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5312 	}
5313 	if (bmap && bmap_size) {
5314 		for (i = 0; i < bmap_size; i++) {
5315 			if (test_bit(i, bmap))
5316 				__set_bit(i, async_events_bmap);
5317 		}
5318 	}
5319 	for (i = 0; i < 8; i++)
5320 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5321 
5322 	if (async_only)
5323 		req->enables =
5324 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5325 
5326 	resp = hwrm_req_hold(bp, req);
5327 	rc = hwrm_req_send(bp, req);
5328 	if (!rc) {
5329 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5330 		if (resp->flags &
5331 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5332 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5333 	}
5334 	hwrm_req_drop(bp, req);
5335 	return rc;
5336 }
5337 
5338 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5339 {
5340 	struct hwrm_func_drv_unrgtr_input *req;
5341 	int rc;
5342 
5343 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5344 		return 0;
5345 
5346 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5347 	if (rc)
5348 		return rc;
5349 	return hwrm_req_send(bp, req);
5350 }
5351 
5352 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5353 
5354 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5355 {
5356 	struct hwrm_tunnel_dst_port_free_input *req;
5357 	int rc;
5358 
5359 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5360 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5361 		return 0;
5362 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5363 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5364 		return 0;
5365 
5366 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5367 	if (rc)
5368 		return rc;
5369 
5370 	req->tunnel_type = tunnel_type;
5371 
5372 	switch (tunnel_type) {
5373 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5374 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5375 		bp->vxlan_port = 0;
5376 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5377 		break;
5378 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5379 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5380 		bp->nge_port = 0;
5381 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5382 		break;
5383 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5384 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5385 		bp->vxlan_gpe_port = 0;
5386 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5387 		break;
5388 	default:
5389 		break;
5390 	}
5391 
5392 	rc = hwrm_req_send(bp, req);
5393 	if (rc)
5394 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5395 			   rc);
5396 	if (bp->flags & BNXT_FLAG_TPA)
5397 		bnxt_set_tpa(bp, true);
5398 	return rc;
5399 }
5400 
5401 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5402 					   u8 tunnel_type)
5403 {
5404 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5405 	struct hwrm_tunnel_dst_port_alloc_input *req;
5406 	int rc;
5407 
5408 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5409 	if (rc)
5410 		return rc;
5411 
5412 	req->tunnel_type = tunnel_type;
5413 	req->tunnel_dst_port_val = port;
5414 
5415 	resp = hwrm_req_hold(bp, req);
5416 	rc = hwrm_req_send(bp, req);
5417 	if (rc) {
5418 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5419 			   rc);
5420 		goto err_out;
5421 	}
5422 
5423 	switch (tunnel_type) {
5424 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5425 		bp->vxlan_port = port;
5426 		bp->vxlan_fw_dst_port_id =
5427 			le16_to_cpu(resp->tunnel_dst_port_id);
5428 		break;
5429 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5430 		bp->nge_port = port;
5431 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5432 		break;
5433 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5434 		bp->vxlan_gpe_port = port;
5435 		bp->vxlan_gpe_fw_dst_port_id =
5436 			le16_to_cpu(resp->tunnel_dst_port_id);
5437 		break;
5438 	default:
5439 		break;
5440 	}
5441 	if (bp->flags & BNXT_FLAG_TPA)
5442 		bnxt_set_tpa(bp, true);
5443 
5444 err_out:
5445 	hwrm_req_drop(bp, req);
5446 	return rc;
5447 }
5448 
5449 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5450 {
5451 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5452 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5453 	int rc;
5454 
5455 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5456 	if (rc)
5457 		return rc;
5458 
5459 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5460 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5461 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5462 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5463 	}
5464 	req->mask = cpu_to_le32(vnic->rx_mask);
5465 	return hwrm_req_send_silent(bp, req);
5466 }
5467 
5468 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5469 {
5470 	if (!atomic_dec_and_test(&fltr->refcnt))
5471 		return;
5472 	spin_lock_bh(&bp->ntp_fltr_lock);
5473 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5474 		spin_unlock_bh(&bp->ntp_fltr_lock);
5475 		return;
5476 	}
5477 	hlist_del_rcu(&fltr->base.hash);
5478 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5479 	if (fltr->base.flags) {
5480 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5481 		bp->ntp_fltr_count--;
5482 	}
5483 	spin_unlock_bh(&bp->ntp_fltr_lock);
5484 	kfree_rcu(fltr, base.rcu);
5485 }
5486 
5487 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5488 						      struct bnxt_l2_key *key,
5489 						      u32 idx)
5490 {
5491 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5492 	struct bnxt_l2_filter *fltr;
5493 
5494 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5495 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5496 
5497 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5498 		    l2_key->vlan == key->vlan)
5499 			return fltr;
5500 	}
5501 	return NULL;
5502 }
5503 
5504 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5505 						    struct bnxt_l2_key *key,
5506 						    u32 idx)
5507 {
5508 	struct bnxt_l2_filter *fltr = NULL;
5509 
5510 	rcu_read_lock();
5511 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5512 	if (fltr)
5513 		atomic_inc(&fltr->refcnt);
5514 	rcu_read_unlock();
5515 	return fltr;
5516 }
5517 
5518 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5519 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5520 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5521 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5522 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5523 
5524 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5525 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5526 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5527 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5528 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5529 
5530 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5531 {
5532 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5533 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5534 			return sizeof(fkeys->addrs.v4addrs) +
5535 			       sizeof(fkeys->ports);
5536 
5537 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5538 			return sizeof(fkeys->addrs.v4addrs);
5539 	}
5540 
5541 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5542 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5543 			return sizeof(fkeys->addrs.v6addrs) +
5544 			       sizeof(fkeys->ports);
5545 
5546 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5547 			return sizeof(fkeys->addrs.v6addrs);
5548 	}
5549 
5550 	return 0;
5551 }
5552 
5553 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5554 			 const unsigned char *key)
5555 {
5556 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5557 	struct bnxt_ipv4_tuple tuple4;
5558 	struct bnxt_ipv6_tuple tuple6;
5559 	int i, j, len = 0;
5560 	u8 *four_tuple;
5561 
5562 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5563 	if (!len)
5564 		return 0;
5565 
5566 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5567 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5568 		tuple4.ports = fkeys->ports;
5569 		four_tuple = (unsigned char *)&tuple4;
5570 	} else {
5571 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5572 		tuple6.ports = fkeys->ports;
5573 		four_tuple = (unsigned char *)&tuple6;
5574 	}
5575 
5576 	for (i = 0, j = 8; i < len; i++, j++) {
5577 		u8 byte = four_tuple[i];
5578 		int bit;
5579 
5580 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5581 			if (byte & 0x80)
5582 				hash ^= prefix;
5583 		}
5584 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5585 	}
5586 
5587 	/* The valid part of the hash is in the upper 32 bits. */
5588 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5589 }
5590 
5591 #ifdef CONFIG_RFS_ACCEL
5592 static struct bnxt_l2_filter *
5593 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5594 {
5595 	struct bnxt_l2_filter *fltr;
5596 	u32 idx;
5597 
5598 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5599 	      BNXT_L2_FLTR_HASH_MASK;
5600 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5601 	return fltr;
5602 }
5603 #endif
5604 
5605 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
5606 			       struct bnxt_l2_key *key, u32 idx)
5607 {
5608 	struct hlist_head *head;
5609 
5610 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
5611 	fltr->l2_key.vlan = key->vlan;
5612 	fltr->base.type = BNXT_FLTR_TYPE_L2;
5613 	if (fltr->base.flags) {
5614 		int bit_id;
5615 
5616 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
5617 						 bp->max_fltr, 0);
5618 		if (bit_id < 0)
5619 			return -ENOMEM;
5620 		fltr->base.sw_id = (u16)bit_id;
5621 		bp->ntp_fltr_count++;
5622 	}
5623 	head = &bp->l2_fltr_hash_tbl[idx];
5624 	hlist_add_head_rcu(&fltr->base.hash, head);
5625 	bnxt_insert_usr_fltr(bp, &fltr->base);
5626 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
5627 	atomic_set(&fltr->refcnt, 1);
5628 	return 0;
5629 }
5630 
5631 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
5632 						   struct bnxt_l2_key *key,
5633 						   gfp_t gfp)
5634 {
5635 	struct bnxt_l2_filter *fltr;
5636 	u32 idx;
5637 	int rc;
5638 
5639 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5640 	      BNXT_L2_FLTR_HASH_MASK;
5641 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5642 	if (fltr)
5643 		return fltr;
5644 
5645 	fltr = kzalloc(sizeof(*fltr), gfp);
5646 	if (!fltr)
5647 		return ERR_PTR(-ENOMEM);
5648 	spin_lock_bh(&bp->ntp_fltr_lock);
5649 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5650 	spin_unlock_bh(&bp->ntp_fltr_lock);
5651 	if (rc) {
5652 		bnxt_del_l2_filter(bp, fltr);
5653 		fltr = ERR_PTR(rc);
5654 	}
5655 	return fltr;
5656 }
5657 
5658 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
5659 						struct bnxt_l2_key *key,
5660 						u16 flags)
5661 {
5662 	struct bnxt_l2_filter *fltr;
5663 	u32 idx;
5664 	int rc;
5665 
5666 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5667 	      BNXT_L2_FLTR_HASH_MASK;
5668 	spin_lock_bh(&bp->ntp_fltr_lock);
5669 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5670 	if (fltr) {
5671 		fltr = ERR_PTR(-EEXIST);
5672 		goto l2_filter_exit;
5673 	}
5674 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
5675 	if (!fltr) {
5676 		fltr = ERR_PTR(-ENOMEM);
5677 		goto l2_filter_exit;
5678 	}
5679 	fltr->base.flags = flags;
5680 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5681 	if (rc) {
5682 		spin_unlock_bh(&bp->ntp_fltr_lock);
5683 		bnxt_del_l2_filter(bp, fltr);
5684 		return ERR_PTR(rc);
5685 	}
5686 
5687 l2_filter_exit:
5688 	spin_unlock_bh(&bp->ntp_fltr_lock);
5689 	return fltr;
5690 }
5691 
5692 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
5693 {
5694 #ifdef CONFIG_BNXT_SRIOV
5695 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
5696 
5697 	return vf->fw_fid;
5698 #else
5699 	return INVALID_HW_RING_ID;
5700 #endif
5701 }
5702 
5703 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5704 {
5705 	struct hwrm_cfa_l2_filter_free_input *req;
5706 	u16 target_id = 0xffff;
5707 	int rc;
5708 
5709 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
5710 		struct bnxt_pf_info *pf = &bp->pf;
5711 
5712 		if (fltr->base.vf_idx >= pf->active_vfs)
5713 			return -EINVAL;
5714 
5715 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
5716 		if (target_id == INVALID_HW_RING_ID)
5717 			return -EINVAL;
5718 	}
5719 
5720 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
5721 	if (rc)
5722 		return rc;
5723 
5724 	req->target_id = cpu_to_le16(target_id);
5725 	req->l2_filter_id = fltr->base.filter_id;
5726 	return hwrm_req_send(bp, req);
5727 }
5728 
5729 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5730 {
5731 	struct hwrm_cfa_l2_filter_alloc_output *resp;
5732 	struct hwrm_cfa_l2_filter_alloc_input *req;
5733 	u16 target_id = 0xffff;
5734 	int rc;
5735 
5736 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
5737 		struct bnxt_pf_info *pf = &bp->pf;
5738 
5739 		if (fltr->base.vf_idx >= pf->active_vfs)
5740 			return -EINVAL;
5741 
5742 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
5743 	}
5744 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
5745 	if (rc)
5746 		return rc;
5747 
5748 	req->target_id = cpu_to_le16(target_id);
5749 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
5750 
5751 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
5752 		req->flags |=
5753 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
5754 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
5755 	req->enables =
5756 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
5757 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
5758 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
5759 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
5760 	eth_broadcast_addr(req->l2_addr_mask);
5761 
5762 	if (fltr->l2_key.vlan) {
5763 		req->enables |=
5764 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
5765 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
5766 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
5767 		req->num_vlans = 1;
5768 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
5769 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
5770 	}
5771 
5772 	resp = hwrm_req_hold(bp, req);
5773 	rc = hwrm_req_send(bp, req);
5774 	if (!rc) {
5775 		fltr->base.filter_id = resp->l2_filter_id;
5776 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
5777 	}
5778 	hwrm_req_drop(bp, req);
5779 	return rc;
5780 }
5781 
5782 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
5783 				     struct bnxt_ntuple_filter *fltr)
5784 {
5785 	struct hwrm_cfa_ntuple_filter_free_input *req;
5786 	int rc;
5787 
5788 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
5789 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
5790 	if (rc)
5791 		return rc;
5792 
5793 	req->ntuple_filter_id = fltr->base.filter_id;
5794 	return hwrm_req_send(bp, req);
5795 }
5796 
5797 #define BNXT_NTP_FLTR_FLAGS					\
5798 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
5799 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
5800 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
5801 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
5802 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
5803 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
5804 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
5805 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
5806 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
5807 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
5808 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
5809 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
5810 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
5811 
5812 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
5813 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
5814 
5815 void bnxt_fill_ipv6_mask(__be32 mask[4])
5816 {
5817 	int i;
5818 
5819 	for (i = 0; i < 4; i++)
5820 		mask[i] = cpu_to_be32(~0);
5821 }
5822 
5823 static void
5824 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
5825 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
5826 			  struct bnxt_ntuple_filter *fltr)
5827 {
5828 	struct bnxt_rss_ctx *rss_ctx, *tmp;
5829 	u16 rxq = fltr->base.rxq;
5830 
5831 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
5832 		list_for_each_entry_safe(rss_ctx, tmp, &bp->rss_ctx_list, list) {
5833 			if (rss_ctx->index == fltr->base.fw_vnic_id) {
5834 				struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
5835 
5836 				req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
5837 				break;
5838 			}
5839 		}
5840 		return;
5841 	}
5842 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
5843 		struct bnxt_vnic_info *vnic;
5844 		u32 enables;
5845 
5846 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
5847 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
5848 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
5849 		req->enables |= cpu_to_le32(enables);
5850 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
5851 	} else {
5852 		u32 flags;
5853 
5854 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
5855 		req->flags |= cpu_to_le32(flags);
5856 		req->dst_id = cpu_to_le16(rxq);
5857 	}
5858 }
5859 
5860 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
5861 				      struct bnxt_ntuple_filter *fltr)
5862 {
5863 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
5864 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
5865 	struct bnxt_flow_masks *masks = &fltr->fmasks;
5866 	struct flow_keys *keys = &fltr->fkeys;
5867 	struct bnxt_l2_filter *l2_fltr;
5868 	struct bnxt_vnic_info *vnic;
5869 	int rc;
5870 
5871 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
5872 	if (rc)
5873 		return rc;
5874 
5875 	l2_fltr = fltr->l2_fltr;
5876 	req->l2_filter_id = l2_fltr->base.filter_id;
5877 
5878 	if (fltr->base.flags & BNXT_ACT_DROP) {
5879 		req->flags =
5880 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
5881 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
5882 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
5883 	} else {
5884 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
5885 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
5886 	}
5887 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
5888 
5889 	req->ethertype = htons(ETH_P_IP);
5890 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
5891 	req->ip_protocol = keys->basic.ip_proto;
5892 
5893 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
5894 		req->ethertype = htons(ETH_P_IPV6);
5895 		req->ip_addr_type =
5896 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
5897 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
5898 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
5899 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
5900 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
5901 	} else {
5902 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
5903 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
5904 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
5905 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
5906 	}
5907 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
5908 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
5909 		req->tunnel_type =
5910 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
5911 	}
5912 
5913 	req->src_port = keys->ports.src;
5914 	req->src_port_mask = masks->ports.src;
5915 	req->dst_port = keys->ports.dst;
5916 	req->dst_port_mask = masks->ports.dst;
5917 
5918 	resp = hwrm_req_hold(bp, req);
5919 	rc = hwrm_req_send(bp, req);
5920 	if (!rc)
5921 		fltr->base.filter_id = resp->ntuple_filter_id;
5922 	hwrm_req_drop(bp, req);
5923 	return rc;
5924 }
5925 
5926 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
5927 				     const u8 *mac_addr)
5928 {
5929 	struct bnxt_l2_filter *fltr;
5930 	struct bnxt_l2_key key;
5931 	int rc;
5932 
5933 	ether_addr_copy(key.dst_mac_addr, mac_addr);
5934 	key.vlan = 0;
5935 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
5936 	if (IS_ERR(fltr))
5937 		return PTR_ERR(fltr);
5938 
5939 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
5940 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
5941 	if (rc)
5942 		bnxt_del_l2_filter(bp, fltr);
5943 	else
5944 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
5945 	return rc;
5946 }
5947 
5948 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
5949 {
5950 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
5951 
5952 	/* Any associated ntuple filters will also be cleared by firmware. */
5953 	for (i = 0; i < num_of_vnics; i++) {
5954 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
5955 
5956 		for (j = 0; j < vnic->uc_filter_count; j++) {
5957 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
5958 
5959 			bnxt_hwrm_l2_filter_free(bp, fltr);
5960 			bnxt_del_l2_filter(bp, fltr);
5961 		}
5962 		vnic->uc_filter_count = 0;
5963 	}
5964 }
5965 
5966 #define BNXT_DFLT_TUNL_TPA_BMAP				\
5967 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
5968 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
5969 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
5970 
5971 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
5972 					   struct hwrm_vnic_tpa_cfg_input *req)
5973 {
5974 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
5975 
5976 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
5977 		return;
5978 
5979 	if (bp->vxlan_port)
5980 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
5981 	if (bp->vxlan_gpe_port)
5982 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
5983 	if (bp->nge_port)
5984 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
5985 
5986 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
5987 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
5988 }
5989 
5990 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
5991 			   u32 tpa_flags)
5992 {
5993 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
5994 	struct hwrm_vnic_tpa_cfg_input *req;
5995 	int rc;
5996 
5997 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
5998 		return 0;
5999 
6000 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6001 	if (rc)
6002 		return rc;
6003 
6004 	if (tpa_flags) {
6005 		u16 mss = bp->dev->mtu - 40;
6006 		u32 nsegs, n, segs = 0, flags;
6007 
6008 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6009 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6010 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6011 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6012 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6013 		if (tpa_flags & BNXT_FLAG_GRO)
6014 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6015 
6016 		req->flags = cpu_to_le32(flags);
6017 
6018 		req->enables =
6019 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6020 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6021 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6022 
6023 		/* Number of segs are log2 units, and first packet is not
6024 		 * included as part of this units.
6025 		 */
6026 		if (mss <= BNXT_RX_PAGE_SIZE) {
6027 			n = BNXT_RX_PAGE_SIZE / mss;
6028 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6029 		} else {
6030 			n = mss / BNXT_RX_PAGE_SIZE;
6031 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6032 				n++;
6033 			nsegs = (MAX_SKB_FRAGS - n) / n;
6034 		}
6035 
6036 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6037 			segs = MAX_TPA_SEGS_P5;
6038 			max_aggs = bp->max_tpa;
6039 		} else {
6040 			segs = ilog2(nsegs);
6041 		}
6042 		req->max_agg_segs = cpu_to_le16(segs);
6043 		req->max_aggs = cpu_to_le16(max_aggs);
6044 
6045 		req->min_agg_len = cpu_to_le32(512);
6046 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6047 	}
6048 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6049 
6050 	return hwrm_req_send(bp, req);
6051 }
6052 
6053 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6054 {
6055 	struct bnxt_ring_grp_info *grp_info;
6056 
6057 	grp_info = &bp->grp_info[ring->grp_idx];
6058 	return grp_info->cp_fw_ring_id;
6059 }
6060 
6061 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6062 {
6063 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6064 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6065 	else
6066 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6067 }
6068 
6069 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6070 {
6071 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6072 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6073 	else
6074 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6075 }
6076 
6077 int bnxt_alloc_rss_indir_tbl(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx)
6078 {
6079 	int entries;
6080 	u16 *tbl;
6081 
6082 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6083 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6084 	else
6085 		entries = HW_HASH_INDEX_SIZE;
6086 
6087 	bp->rss_indir_tbl_entries = entries;
6088 	tbl = kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6089 	if (!tbl)
6090 		return -ENOMEM;
6091 
6092 	if (rss_ctx)
6093 		rss_ctx->rss_indir_tbl = tbl;
6094 	else
6095 		bp->rss_indir_tbl = tbl;
6096 
6097 	return 0;
6098 }
6099 
6100 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx)
6101 {
6102 	u16 max_rings, max_entries, pad, i;
6103 	u16 *rss_indir_tbl;
6104 
6105 	if (!bp->rx_nr_rings)
6106 		return;
6107 
6108 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6109 		max_rings = bp->rx_nr_rings - 1;
6110 	else
6111 		max_rings = bp->rx_nr_rings;
6112 
6113 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6114 	if (rss_ctx)
6115 		rss_indir_tbl = &rss_ctx->rss_indir_tbl[0];
6116 	else
6117 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6118 
6119 	for (i = 0; i < max_entries; i++)
6120 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6121 
6122 	pad = bp->rss_indir_tbl_entries - max_entries;
6123 	if (pad)
6124 		memset(&rss_indir_tbl[i], 0, pad * sizeof(u16));
6125 }
6126 
6127 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6128 {
6129 	u16 i, tbl_size, max_ring = 0;
6130 
6131 	if (!bp->rss_indir_tbl)
6132 		return 0;
6133 
6134 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6135 	for (i = 0; i < tbl_size; i++)
6136 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6137 	return max_ring;
6138 }
6139 
6140 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6141 {
6142 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6143 		if (!rx_rings)
6144 			return 0;
6145 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6146 					       BNXT_RSS_TABLE_ENTRIES_P5);
6147 	}
6148 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6149 		return 2;
6150 	return 1;
6151 }
6152 
6153 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6154 {
6155 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6156 	u16 i, j;
6157 
6158 	/* Fill the RSS indirection table with ring group ids */
6159 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6160 		if (!no_rss)
6161 			j = bp->rss_indir_tbl[i];
6162 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6163 	}
6164 }
6165 
6166 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6167 				    struct bnxt_vnic_info *vnic)
6168 {
6169 	__le16 *ring_tbl = vnic->rss_table;
6170 	struct bnxt_rx_ring_info *rxr;
6171 	u16 tbl_size, i;
6172 
6173 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6174 
6175 	for (i = 0; i < tbl_size; i++) {
6176 		u16 ring_id, j;
6177 
6178 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6179 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6180 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6181 			j = vnic->rss_ctx->rss_indir_tbl[i];
6182 		else
6183 			j = bp->rss_indir_tbl[i];
6184 		rxr = &bp->rx_ring[j];
6185 
6186 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6187 		*ring_tbl++ = cpu_to_le16(ring_id);
6188 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6189 		*ring_tbl++ = cpu_to_le16(ring_id);
6190 	}
6191 }
6192 
6193 static void
6194 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6195 			 struct bnxt_vnic_info *vnic)
6196 {
6197 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6198 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6199 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6200 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6201 	} else {
6202 		bnxt_fill_hw_rss_tbl(bp, vnic);
6203 	}
6204 
6205 	if (bp->rss_hash_delta) {
6206 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6207 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6208 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6209 		else
6210 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6211 	} else {
6212 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6213 	}
6214 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6215 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6216 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6217 }
6218 
6219 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6220 				  bool set_rss)
6221 {
6222 	struct hwrm_vnic_rss_cfg_input *req;
6223 	int rc;
6224 
6225 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6226 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6227 		return 0;
6228 
6229 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6230 	if (rc)
6231 		return rc;
6232 
6233 	if (set_rss)
6234 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6235 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6236 	return hwrm_req_send(bp, req);
6237 }
6238 
6239 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6240 				     struct bnxt_vnic_info *vnic, bool set_rss)
6241 {
6242 	struct hwrm_vnic_rss_cfg_input *req;
6243 	dma_addr_t ring_tbl_map;
6244 	u32 i, nr_ctxs;
6245 	int rc;
6246 
6247 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6248 	if (rc)
6249 		return rc;
6250 
6251 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6252 	if (!set_rss)
6253 		return hwrm_req_send(bp, req);
6254 
6255 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6256 	ring_tbl_map = vnic->rss_table_dma_addr;
6257 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6258 
6259 	hwrm_req_hold(bp, req);
6260 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6261 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6262 		req->ring_table_pair_index = i;
6263 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6264 		rc = hwrm_req_send(bp, req);
6265 		if (rc)
6266 			goto exit;
6267 	}
6268 
6269 exit:
6270 	hwrm_req_drop(bp, req);
6271 	return rc;
6272 }
6273 
6274 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6275 {
6276 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6277 	struct hwrm_vnic_rss_qcfg_output *resp;
6278 	struct hwrm_vnic_rss_qcfg_input *req;
6279 
6280 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6281 		return;
6282 
6283 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6284 	/* all contexts configured to same hash_type, zero always exists */
6285 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6286 	resp = hwrm_req_hold(bp, req);
6287 	if (!hwrm_req_send(bp, req)) {
6288 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6289 		bp->rss_hash_delta = 0;
6290 	}
6291 	hwrm_req_drop(bp, req);
6292 }
6293 
6294 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6295 {
6296 	struct hwrm_vnic_plcmodes_cfg_input *req;
6297 	int rc;
6298 
6299 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6300 	if (rc)
6301 		return rc;
6302 
6303 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6304 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6305 
6306 	if (BNXT_RX_PAGE_MODE(bp)) {
6307 		req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6308 	} else {
6309 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6310 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6311 		req->enables |=
6312 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6313 		req->jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
6314 		req->hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
6315 	}
6316 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6317 	return hwrm_req_send(bp, req);
6318 }
6319 
6320 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6321 					struct bnxt_vnic_info *vnic,
6322 					u16 ctx_idx)
6323 {
6324 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6325 
6326 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6327 		return;
6328 
6329 	req->rss_cos_lb_ctx_id =
6330 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6331 
6332 	hwrm_req_send(bp, req);
6333 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6334 }
6335 
6336 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6337 {
6338 	int i, j;
6339 
6340 	for (i = 0; i < bp->nr_vnics; i++) {
6341 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6342 
6343 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6344 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6345 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6346 		}
6347 	}
6348 	bp->rsscos_nr_ctxs = 0;
6349 }
6350 
6351 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6352 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6353 {
6354 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6355 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6356 	int rc;
6357 
6358 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6359 	if (rc)
6360 		return rc;
6361 
6362 	resp = hwrm_req_hold(bp, req);
6363 	rc = hwrm_req_send(bp, req);
6364 	if (!rc)
6365 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6366 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6367 	hwrm_req_drop(bp, req);
6368 
6369 	return rc;
6370 }
6371 
6372 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6373 {
6374 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6375 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6376 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6377 }
6378 
6379 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6380 {
6381 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6382 	struct hwrm_vnic_cfg_input *req;
6383 	unsigned int ring = 0, grp_idx;
6384 	u16 def_vlan = 0;
6385 	int rc;
6386 
6387 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6388 	if (rc)
6389 		return rc;
6390 
6391 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6392 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6393 
6394 		req->default_rx_ring_id =
6395 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6396 		req->default_cmpl_ring_id =
6397 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6398 		req->enables =
6399 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6400 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6401 		goto vnic_mru;
6402 	}
6403 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6404 	/* Only RSS support for now TBD: COS & LB */
6405 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6406 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6407 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6408 					   VNIC_CFG_REQ_ENABLES_MRU);
6409 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6410 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6411 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6412 					   VNIC_CFG_REQ_ENABLES_MRU);
6413 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6414 	} else {
6415 		req->rss_rule = cpu_to_le16(0xffff);
6416 	}
6417 
6418 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6419 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6420 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6421 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6422 	} else {
6423 		req->cos_rule = cpu_to_le16(0xffff);
6424 	}
6425 
6426 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6427 		ring = 0;
6428 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6429 		ring = vnic->vnic_id - 1;
6430 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6431 		ring = bp->rx_nr_rings - 1;
6432 
6433 	grp_idx = bp->rx_ring[ring].bnapi->index;
6434 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6435 	req->lb_rule = cpu_to_le16(0xffff);
6436 vnic_mru:
6437 	req->mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + VLAN_HLEN);
6438 
6439 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6440 #ifdef CONFIG_BNXT_SRIOV
6441 	if (BNXT_VF(bp))
6442 		def_vlan = bp->vf.vlan;
6443 #endif
6444 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6445 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6446 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6447 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6448 
6449 	return hwrm_req_send(bp, req);
6450 }
6451 
6452 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6453 				    struct bnxt_vnic_info *vnic)
6454 {
6455 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6456 		struct hwrm_vnic_free_input *req;
6457 
6458 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6459 			return;
6460 
6461 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6462 
6463 		hwrm_req_send(bp, req);
6464 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6465 	}
6466 }
6467 
6468 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6469 {
6470 	u16 i;
6471 
6472 	for (i = 0; i < bp->nr_vnics; i++)
6473 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6474 }
6475 
6476 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6477 			 unsigned int start_rx_ring_idx,
6478 			 unsigned int nr_rings)
6479 {
6480 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6481 	struct hwrm_vnic_alloc_output *resp;
6482 	struct hwrm_vnic_alloc_input *req;
6483 	int rc;
6484 
6485 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6486 	if (rc)
6487 		return rc;
6488 
6489 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6490 		goto vnic_no_ring_grps;
6491 
6492 	/* map ring groups to this vnic */
6493 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6494 		grp_idx = bp->rx_ring[i].bnapi->index;
6495 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6496 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6497 				   j, nr_rings);
6498 			break;
6499 		}
6500 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6501 	}
6502 
6503 vnic_no_ring_grps:
6504 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6505 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6506 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6507 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6508 
6509 	resp = hwrm_req_hold(bp, req);
6510 	rc = hwrm_req_send(bp, req);
6511 	if (!rc)
6512 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6513 	hwrm_req_drop(bp, req);
6514 	return rc;
6515 }
6516 
6517 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6518 {
6519 	struct hwrm_vnic_qcaps_output *resp;
6520 	struct hwrm_vnic_qcaps_input *req;
6521 	int rc;
6522 
6523 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6524 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6525 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6526 	if (bp->hwrm_spec_code < 0x10600)
6527 		return 0;
6528 
6529 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6530 	if (rc)
6531 		return rc;
6532 
6533 	resp = hwrm_req_hold(bp, req);
6534 	rc = hwrm_req_send(bp, req);
6535 	if (!rc) {
6536 		u32 flags = le32_to_cpu(resp->flags);
6537 
6538 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6539 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6540 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6541 		if (flags &
6542 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6543 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6544 
6545 		/* Older P5 fw before EXT_HW_STATS support did not set
6546 		 * VLAN_STRIP_CAP properly.
6547 		 */
6548 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6549 		    (BNXT_CHIP_P5(bp) &&
6550 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6551 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6552 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6553 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6554 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6555 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6556 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6557 		if (bp->max_tpa_v2) {
6558 			if (BNXT_CHIP_P5(bp))
6559 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6560 			else
6561 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6562 		}
6563 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6564 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6565 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6566 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6567 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6568 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6569 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6570 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6571 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6572 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6573 	}
6574 	hwrm_req_drop(bp, req);
6575 	return rc;
6576 }
6577 
6578 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6579 {
6580 	struct hwrm_ring_grp_alloc_output *resp;
6581 	struct hwrm_ring_grp_alloc_input *req;
6582 	int rc;
6583 	u16 i;
6584 
6585 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6586 		return 0;
6587 
6588 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6589 	if (rc)
6590 		return rc;
6591 
6592 	resp = hwrm_req_hold(bp, req);
6593 	for (i = 0; i < bp->rx_nr_rings; i++) {
6594 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
6595 
6596 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
6597 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
6598 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
6599 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
6600 
6601 		rc = hwrm_req_send(bp, req);
6602 
6603 		if (rc)
6604 			break;
6605 
6606 		bp->grp_info[grp_idx].fw_grp_id =
6607 			le32_to_cpu(resp->ring_group_id);
6608 	}
6609 	hwrm_req_drop(bp, req);
6610 	return rc;
6611 }
6612 
6613 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
6614 {
6615 	struct hwrm_ring_grp_free_input *req;
6616 	u16 i;
6617 
6618 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
6619 		return;
6620 
6621 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
6622 		return;
6623 
6624 	hwrm_req_hold(bp, req);
6625 	for (i = 0; i < bp->cp_nr_rings; i++) {
6626 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
6627 			continue;
6628 		req->ring_group_id =
6629 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
6630 
6631 		hwrm_req_send(bp, req);
6632 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
6633 	}
6634 	hwrm_req_drop(bp, req);
6635 }
6636 
6637 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
6638 				    struct bnxt_ring_struct *ring,
6639 				    u32 ring_type, u32 map_index)
6640 {
6641 	struct hwrm_ring_alloc_output *resp;
6642 	struct hwrm_ring_alloc_input *req;
6643 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
6644 	struct bnxt_ring_grp_info *grp_info;
6645 	int rc, err = 0;
6646 	u16 ring_id;
6647 
6648 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
6649 	if (rc)
6650 		goto exit;
6651 
6652 	req->enables = 0;
6653 	if (rmem->nr_pages > 1) {
6654 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
6655 		/* Page size is in log2 units */
6656 		req->page_size = BNXT_PAGE_SHIFT;
6657 		req->page_tbl_depth = 1;
6658 	} else {
6659 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
6660 	}
6661 	req->fbo = 0;
6662 	/* Association of ring index with doorbell index and MSIX number */
6663 	req->logical_id = cpu_to_le16(map_index);
6664 
6665 	switch (ring_type) {
6666 	case HWRM_RING_ALLOC_TX: {
6667 		struct bnxt_tx_ring_info *txr;
6668 
6669 		txr = container_of(ring, struct bnxt_tx_ring_info,
6670 				   tx_ring_struct);
6671 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
6672 		/* Association of transmit ring with completion ring */
6673 		grp_info = &bp->grp_info[ring->grp_idx];
6674 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
6675 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
6676 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6677 		req->queue_id = cpu_to_le16(ring->queue_id);
6678 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
6679 			req->cmpl_coal_cnt =
6680 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
6681 		break;
6682 	}
6683 	case HWRM_RING_ALLOC_RX:
6684 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6685 		req->length = cpu_to_le32(bp->rx_ring_mask + 1);
6686 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6687 			u16 flags = 0;
6688 
6689 			/* Association of rx ring with stats context */
6690 			grp_info = &bp->grp_info[ring->grp_idx];
6691 			req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
6692 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6693 			req->enables |= cpu_to_le32(
6694 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6695 			if (NET_IP_ALIGN == 2)
6696 				flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
6697 			req->flags = cpu_to_le16(flags);
6698 		}
6699 		break;
6700 	case HWRM_RING_ALLOC_AGG:
6701 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6702 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
6703 			/* Association of agg ring with rx ring */
6704 			grp_info = &bp->grp_info[ring->grp_idx];
6705 			req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
6706 			req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
6707 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6708 			req->enables |= cpu_to_le32(
6709 				RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
6710 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6711 		} else {
6712 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6713 		}
6714 		req->length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
6715 		break;
6716 	case HWRM_RING_ALLOC_CMPL:
6717 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
6718 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
6719 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6720 			/* Association of cp ring with nq */
6721 			grp_info = &bp->grp_info[map_index];
6722 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
6723 			req->cq_handle = cpu_to_le64(ring->handle);
6724 			req->enables |= cpu_to_le32(
6725 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
6726 		} else if (bp->flags & BNXT_FLAG_USING_MSIX) {
6727 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
6728 		}
6729 		break;
6730 	case HWRM_RING_ALLOC_NQ:
6731 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
6732 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
6733 		if (bp->flags & BNXT_FLAG_USING_MSIX)
6734 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
6735 		break;
6736 	default:
6737 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
6738 			   ring_type);
6739 		return -1;
6740 	}
6741 
6742 	resp = hwrm_req_hold(bp, req);
6743 	rc = hwrm_req_send(bp, req);
6744 	err = le16_to_cpu(resp->error_code);
6745 	ring_id = le16_to_cpu(resp->ring_id);
6746 	hwrm_req_drop(bp, req);
6747 
6748 exit:
6749 	if (rc || err) {
6750 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
6751 			   ring_type, rc, err);
6752 		return -EIO;
6753 	}
6754 	ring->fw_ring_id = ring_id;
6755 	return rc;
6756 }
6757 
6758 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
6759 {
6760 	int rc;
6761 
6762 	if (BNXT_PF(bp)) {
6763 		struct hwrm_func_cfg_input *req;
6764 
6765 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
6766 		if (rc)
6767 			return rc;
6768 
6769 		req->fid = cpu_to_le16(0xffff);
6770 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
6771 		req->async_event_cr = cpu_to_le16(idx);
6772 		return hwrm_req_send(bp, req);
6773 	} else {
6774 		struct hwrm_func_vf_cfg_input *req;
6775 
6776 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
6777 		if (rc)
6778 			return rc;
6779 
6780 		req->enables =
6781 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
6782 		req->async_event_cr = cpu_to_le16(idx);
6783 		return hwrm_req_send(bp, req);
6784 	}
6785 }
6786 
6787 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
6788 			     u32 ring_type)
6789 {
6790 	switch (ring_type) {
6791 	case HWRM_RING_ALLOC_TX:
6792 		db->db_ring_mask = bp->tx_ring_mask;
6793 		break;
6794 	case HWRM_RING_ALLOC_RX:
6795 		db->db_ring_mask = bp->rx_ring_mask;
6796 		break;
6797 	case HWRM_RING_ALLOC_AGG:
6798 		db->db_ring_mask = bp->rx_agg_ring_mask;
6799 		break;
6800 	case HWRM_RING_ALLOC_CMPL:
6801 	case HWRM_RING_ALLOC_NQ:
6802 		db->db_ring_mask = bp->cp_ring_mask;
6803 		break;
6804 	}
6805 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
6806 		db->db_epoch_mask = db->db_ring_mask + 1;
6807 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
6808 	}
6809 }
6810 
6811 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
6812 			u32 map_idx, u32 xid)
6813 {
6814 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6815 		switch (ring_type) {
6816 		case HWRM_RING_ALLOC_TX:
6817 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
6818 			break;
6819 		case HWRM_RING_ALLOC_RX:
6820 		case HWRM_RING_ALLOC_AGG:
6821 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
6822 			break;
6823 		case HWRM_RING_ALLOC_CMPL:
6824 			db->db_key64 = DBR_PATH_L2;
6825 			break;
6826 		case HWRM_RING_ALLOC_NQ:
6827 			db->db_key64 = DBR_PATH_L2;
6828 			break;
6829 		}
6830 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
6831 
6832 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6833 			db->db_key64 |= DBR_VALID;
6834 
6835 		db->doorbell = bp->bar1 + bp->db_offset;
6836 	} else {
6837 		db->doorbell = bp->bar1 + map_idx * 0x80;
6838 		switch (ring_type) {
6839 		case HWRM_RING_ALLOC_TX:
6840 			db->db_key32 = DB_KEY_TX;
6841 			break;
6842 		case HWRM_RING_ALLOC_RX:
6843 		case HWRM_RING_ALLOC_AGG:
6844 			db->db_key32 = DB_KEY_RX;
6845 			break;
6846 		case HWRM_RING_ALLOC_CMPL:
6847 			db->db_key32 = DB_KEY_CP;
6848 			break;
6849 		}
6850 	}
6851 	bnxt_set_db_mask(bp, db, ring_type);
6852 }
6853 
6854 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
6855 {
6856 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
6857 	int i, rc = 0;
6858 	u32 type;
6859 
6860 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6861 		type = HWRM_RING_ALLOC_NQ;
6862 	else
6863 		type = HWRM_RING_ALLOC_CMPL;
6864 	for (i = 0; i < bp->cp_nr_rings; i++) {
6865 		struct bnxt_napi *bnapi = bp->bnapi[i];
6866 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6867 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
6868 		u32 map_idx = ring->map_idx;
6869 		unsigned int vector;
6870 
6871 		vector = bp->irq_tbl[map_idx].vector;
6872 		disable_irq_nosync(vector);
6873 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6874 		if (rc) {
6875 			enable_irq(vector);
6876 			goto err_out;
6877 		}
6878 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
6879 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
6880 		enable_irq(vector);
6881 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
6882 
6883 		if (!i) {
6884 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
6885 			if (rc)
6886 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
6887 		}
6888 	}
6889 
6890 	type = HWRM_RING_ALLOC_TX;
6891 	for (i = 0; i < bp->tx_nr_rings; i++) {
6892 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
6893 		struct bnxt_ring_struct *ring;
6894 		u32 map_idx;
6895 
6896 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6897 			struct bnxt_cp_ring_info *cpr2 = txr->tx_cpr;
6898 			struct bnxt_napi *bnapi = txr->bnapi;
6899 			u32 type2 = HWRM_RING_ALLOC_CMPL;
6900 
6901 			ring = &cpr2->cp_ring_struct;
6902 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
6903 			map_idx = bnapi->index;
6904 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
6905 			if (rc)
6906 				goto err_out;
6907 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
6908 				    ring->fw_ring_id);
6909 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
6910 		}
6911 		ring = &txr->tx_ring_struct;
6912 		map_idx = i;
6913 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6914 		if (rc)
6915 			goto err_out;
6916 		bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
6917 	}
6918 
6919 	type = HWRM_RING_ALLOC_RX;
6920 	for (i = 0; i < bp->rx_nr_rings; i++) {
6921 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
6922 		struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
6923 		struct bnxt_napi *bnapi = rxr->bnapi;
6924 		u32 map_idx = bnapi->index;
6925 
6926 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6927 		if (rc)
6928 			goto err_out;
6929 		bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
6930 		/* If we have agg rings, post agg buffers first. */
6931 		if (!agg_rings)
6932 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
6933 		bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
6934 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6935 			struct bnxt_cp_ring_info *cpr2 = rxr->rx_cpr;
6936 			u32 type2 = HWRM_RING_ALLOC_CMPL;
6937 
6938 			ring = &cpr2->cp_ring_struct;
6939 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
6940 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
6941 			if (rc)
6942 				goto err_out;
6943 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
6944 				    ring->fw_ring_id);
6945 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
6946 		}
6947 	}
6948 
6949 	if (agg_rings) {
6950 		type = HWRM_RING_ALLOC_AGG;
6951 		for (i = 0; i < bp->rx_nr_rings; i++) {
6952 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
6953 			struct bnxt_ring_struct *ring =
6954 						&rxr->rx_agg_ring_struct;
6955 			u32 grp_idx = ring->grp_idx;
6956 			u32 map_idx = grp_idx + bp->rx_nr_rings;
6957 
6958 			rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6959 			if (rc)
6960 				goto err_out;
6961 
6962 			bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
6963 				    ring->fw_ring_id);
6964 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
6965 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
6966 			bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
6967 		}
6968 	}
6969 err_out:
6970 	return rc;
6971 }
6972 
6973 static int hwrm_ring_free_send_msg(struct bnxt *bp,
6974 				   struct bnxt_ring_struct *ring,
6975 				   u32 ring_type, int cmpl_ring_id)
6976 {
6977 	struct hwrm_ring_free_output *resp;
6978 	struct hwrm_ring_free_input *req;
6979 	u16 error_code = 0;
6980 	int rc;
6981 
6982 	if (BNXT_NO_FW_ACCESS(bp))
6983 		return 0;
6984 
6985 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
6986 	if (rc)
6987 		goto exit;
6988 
6989 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
6990 	req->ring_type = ring_type;
6991 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
6992 
6993 	resp = hwrm_req_hold(bp, req);
6994 	rc = hwrm_req_send(bp, req);
6995 	error_code = le16_to_cpu(resp->error_code);
6996 	hwrm_req_drop(bp, req);
6997 exit:
6998 	if (rc || error_code) {
6999 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7000 			   ring_type, rc, error_code);
7001 		return -EIO;
7002 	}
7003 	return 0;
7004 }
7005 
7006 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7007 {
7008 	u32 type;
7009 	int i;
7010 
7011 	if (!bp->bnapi)
7012 		return;
7013 
7014 	for (i = 0; i < bp->tx_nr_rings; i++) {
7015 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7016 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7017 
7018 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7019 			u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
7020 
7021 			hwrm_ring_free_send_msg(bp, ring,
7022 						RING_FREE_REQ_RING_TYPE_TX,
7023 						close_path ? cmpl_ring_id :
7024 						INVALID_HW_RING_ID);
7025 			ring->fw_ring_id = INVALID_HW_RING_ID;
7026 		}
7027 	}
7028 
7029 	for (i = 0; i < bp->rx_nr_rings; i++) {
7030 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7031 		struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7032 		u32 grp_idx = rxr->bnapi->index;
7033 
7034 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7035 			u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7036 
7037 			hwrm_ring_free_send_msg(bp, ring,
7038 						RING_FREE_REQ_RING_TYPE_RX,
7039 						close_path ? cmpl_ring_id :
7040 						INVALID_HW_RING_ID);
7041 			ring->fw_ring_id = INVALID_HW_RING_ID;
7042 			bp->grp_info[grp_idx].rx_fw_ring_id =
7043 				INVALID_HW_RING_ID;
7044 		}
7045 	}
7046 
7047 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7048 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7049 	else
7050 		type = RING_FREE_REQ_RING_TYPE_RX;
7051 	for (i = 0; i < bp->rx_nr_rings; i++) {
7052 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7053 		struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7054 		u32 grp_idx = rxr->bnapi->index;
7055 
7056 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7057 			u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7058 
7059 			hwrm_ring_free_send_msg(bp, ring, type,
7060 						close_path ? cmpl_ring_id :
7061 						INVALID_HW_RING_ID);
7062 			ring->fw_ring_id = INVALID_HW_RING_ID;
7063 			bp->grp_info[grp_idx].agg_fw_ring_id =
7064 				INVALID_HW_RING_ID;
7065 		}
7066 	}
7067 
7068 	/* The completion rings are about to be freed.  After that the
7069 	 * IRQ doorbell will not work anymore.  So we need to disable
7070 	 * IRQ here.
7071 	 */
7072 	bnxt_disable_int_sync(bp);
7073 
7074 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7075 		type = RING_FREE_REQ_RING_TYPE_NQ;
7076 	else
7077 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7078 	for (i = 0; i < bp->cp_nr_rings; i++) {
7079 		struct bnxt_napi *bnapi = bp->bnapi[i];
7080 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7081 		struct bnxt_ring_struct *ring;
7082 		int j;
7083 
7084 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++) {
7085 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
7086 
7087 			ring = &cpr2->cp_ring_struct;
7088 			if (ring->fw_ring_id == INVALID_HW_RING_ID)
7089 				continue;
7090 			hwrm_ring_free_send_msg(bp, ring,
7091 						RING_FREE_REQ_RING_TYPE_L2_CMPL,
7092 						INVALID_HW_RING_ID);
7093 			ring->fw_ring_id = INVALID_HW_RING_ID;
7094 		}
7095 		ring = &cpr->cp_ring_struct;
7096 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7097 			hwrm_ring_free_send_msg(bp, ring, type,
7098 						INVALID_HW_RING_ID);
7099 			ring->fw_ring_id = INVALID_HW_RING_ID;
7100 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7101 		}
7102 	}
7103 }
7104 
7105 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7106 			     bool shared);
7107 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7108 			   bool shared);
7109 
7110 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7111 {
7112 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7113 	struct hwrm_func_qcfg_output *resp;
7114 	struct hwrm_func_qcfg_input *req;
7115 	int rc;
7116 
7117 	if (bp->hwrm_spec_code < 0x10601)
7118 		return 0;
7119 
7120 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7121 	if (rc)
7122 		return rc;
7123 
7124 	req->fid = cpu_to_le16(0xffff);
7125 	resp = hwrm_req_hold(bp, req);
7126 	rc = hwrm_req_send(bp, req);
7127 	if (rc) {
7128 		hwrm_req_drop(bp, req);
7129 		return rc;
7130 	}
7131 
7132 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7133 	if (BNXT_NEW_RM(bp)) {
7134 		u16 cp, stats;
7135 
7136 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7137 		hw_resc->resv_hw_ring_grps =
7138 			le32_to_cpu(resp->alloc_hw_ring_grps);
7139 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7140 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7141 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7142 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7143 		hw_resc->resv_irqs = cp;
7144 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7145 			int rx = hw_resc->resv_rx_rings;
7146 			int tx = hw_resc->resv_tx_rings;
7147 
7148 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7149 				rx >>= 1;
7150 			if (cp < (rx + tx)) {
7151 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7152 				if (rc)
7153 					goto get_rings_exit;
7154 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7155 					rx <<= 1;
7156 				hw_resc->resv_rx_rings = rx;
7157 				hw_resc->resv_tx_rings = tx;
7158 			}
7159 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7160 			hw_resc->resv_hw_ring_grps = rx;
7161 		}
7162 		hw_resc->resv_cp_rings = cp;
7163 		hw_resc->resv_stat_ctxs = stats;
7164 	}
7165 get_rings_exit:
7166 	hwrm_req_drop(bp, req);
7167 	return rc;
7168 }
7169 
7170 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7171 {
7172 	struct hwrm_func_qcfg_output *resp;
7173 	struct hwrm_func_qcfg_input *req;
7174 	int rc;
7175 
7176 	if (bp->hwrm_spec_code < 0x10601)
7177 		return 0;
7178 
7179 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7180 	if (rc)
7181 		return rc;
7182 
7183 	req->fid = cpu_to_le16(fid);
7184 	resp = hwrm_req_hold(bp, req);
7185 	rc = hwrm_req_send(bp, req);
7186 	if (!rc)
7187 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7188 
7189 	hwrm_req_drop(bp, req);
7190 	return rc;
7191 }
7192 
7193 static bool bnxt_rfs_supported(struct bnxt *bp);
7194 
7195 static struct hwrm_func_cfg_input *
7196 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7197 {
7198 	struct hwrm_func_cfg_input *req;
7199 	u32 enables = 0;
7200 
7201 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7202 		return NULL;
7203 
7204 	req->fid = cpu_to_le16(0xffff);
7205 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7206 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7207 	if (BNXT_NEW_RM(bp)) {
7208 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7209 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7210 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7211 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7212 			enables |= hwr->cp_p5 ?
7213 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7214 		} else {
7215 			enables |= hwr->cp ?
7216 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7217 			enables |= hwr->grp ?
7218 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7219 		}
7220 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7221 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7222 					  0;
7223 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7224 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7225 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7226 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7227 			req->num_msix = cpu_to_le16(hwr->cp);
7228 		} else {
7229 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7230 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7231 		}
7232 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7233 		req->num_vnics = cpu_to_le16(hwr->vnic);
7234 	}
7235 	req->enables = cpu_to_le32(enables);
7236 	return req;
7237 }
7238 
7239 static struct hwrm_func_vf_cfg_input *
7240 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7241 {
7242 	struct hwrm_func_vf_cfg_input *req;
7243 	u32 enables = 0;
7244 
7245 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7246 		return NULL;
7247 
7248 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7249 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7250 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7251 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7252 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7253 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7254 		enables |= hwr->cp_p5 ?
7255 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7256 	} else {
7257 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7258 		enables |= hwr->grp ?
7259 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7260 	}
7261 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7262 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7263 
7264 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7265 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7266 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7267 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7268 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7269 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7270 	} else {
7271 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7272 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7273 	}
7274 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7275 	req->num_vnics = cpu_to_le16(hwr->vnic);
7276 
7277 	req->enables = cpu_to_le32(enables);
7278 	return req;
7279 }
7280 
7281 static int
7282 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7283 {
7284 	struct hwrm_func_cfg_input *req;
7285 	int rc;
7286 
7287 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7288 	if (!req)
7289 		return -ENOMEM;
7290 
7291 	if (!req->enables) {
7292 		hwrm_req_drop(bp, req);
7293 		return 0;
7294 	}
7295 
7296 	rc = hwrm_req_send(bp, req);
7297 	if (rc)
7298 		return rc;
7299 
7300 	if (bp->hwrm_spec_code < 0x10601)
7301 		bp->hw_resc.resv_tx_rings = hwr->tx;
7302 
7303 	return bnxt_hwrm_get_rings(bp);
7304 }
7305 
7306 static int
7307 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7308 {
7309 	struct hwrm_func_vf_cfg_input *req;
7310 	int rc;
7311 
7312 	if (!BNXT_NEW_RM(bp)) {
7313 		bp->hw_resc.resv_tx_rings = hwr->tx;
7314 		return 0;
7315 	}
7316 
7317 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7318 	if (!req)
7319 		return -ENOMEM;
7320 
7321 	rc = hwrm_req_send(bp, req);
7322 	if (rc)
7323 		return rc;
7324 
7325 	return bnxt_hwrm_get_rings(bp);
7326 }
7327 
7328 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7329 {
7330 	if (BNXT_PF(bp))
7331 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7332 	else
7333 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7334 }
7335 
7336 int bnxt_nq_rings_in_use(struct bnxt *bp)
7337 {
7338 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7339 }
7340 
7341 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7342 {
7343 	int cp;
7344 
7345 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7346 		return bnxt_nq_rings_in_use(bp);
7347 
7348 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7349 	return cp;
7350 }
7351 
7352 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7353 {
7354 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7355 }
7356 
7357 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7358 {
7359 	if (!hwr->grp)
7360 		return 0;
7361 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7362 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7363 
7364 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7365 			rss_ctx *= hwr->vnic;
7366 		return rss_ctx;
7367 	}
7368 	if (BNXT_VF(bp))
7369 		return BNXT_VF_MAX_RSS_CTX;
7370 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7371 		return hwr->grp + 1;
7372 	return 1;
7373 }
7374 
7375 /* Check if a default RSS map needs to be setup.  This function is only
7376  * used on older firmware that does not require reserving RX rings.
7377  */
7378 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7379 {
7380 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7381 
7382 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7383 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7384 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7385 		if (!netif_is_rxfh_configured(bp->dev))
7386 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7387 	}
7388 }
7389 
7390 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7391 {
7392 	if (bp->flags & BNXT_FLAG_RFS) {
7393 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7394 			return 2 + bp->num_rss_ctx;
7395 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7396 			return rx_rings + 1;
7397 	}
7398 	return 1;
7399 }
7400 
7401 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7402 {
7403 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7404 	int cp = bnxt_cp_rings_in_use(bp);
7405 	int nq = bnxt_nq_rings_in_use(bp);
7406 	int rx = bp->rx_nr_rings, stat;
7407 	int vnic, grp = rx;
7408 
7409 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7410 	    bp->hwrm_spec_code >= 0x10601)
7411 		return true;
7412 
7413 	/* Old firmware does not need RX ring reservations but we still
7414 	 * need to setup a default RSS map when needed.  With new firmware
7415 	 * we go through RX ring reservations first and then set up the
7416 	 * RSS map for the successfully reserved RX rings when needed.
7417 	 */
7418 	if (!BNXT_NEW_RM(bp)) {
7419 		bnxt_check_rss_tbl_no_rmgr(bp);
7420 		return false;
7421 	}
7422 
7423 	vnic = bnxt_get_total_vnics(bp, rx);
7424 
7425 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7426 		rx <<= 1;
7427 	stat = bnxt_get_func_stat_ctxs(bp);
7428 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7429 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7430 	    (hw_resc->resv_hw_ring_grps != grp &&
7431 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7432 		return true;
7433 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7434 	    hw_resc->resv_irqs != nq)
7435 		return true;
7436 	return false;
7437 }
7438 
7439 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7440 {
7441 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7442 
7443 	hwr->tx = hw_resc->resv_tx_rings;
7444 	if (BNXT_NEW_RM(bp)) {
7445 		hwr->rx = hw_resc->resv_rx_rings;
7446 		hwr->cp = hw_resc->resv_irqs;
7447 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7448 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7449 		hwr->grp = hw_resc->resv_hw_ring_grps;
7450 		hwr->vnic = hw_resc->resv_vnics;
7451 		hwr->stat = hw_resc->resv_stat_ctxs;
7452 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7453 	}
7454 }
7455 
7456 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7457 {
7458 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7459 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7460 }
7461 
7462 static int __bnxt_reserve_rings(struct bnxt *bp)
7463 {
7464 	struct bnxt_hw_rings hwr = {0};
7465 	int cp = bp->cp_nr_rings;
7466 	int rx_rings, rc;
7467 	int ulp_msix = 0;
7468 	bool sh = false;
7469 	int tx_cp;
7470 
7471 	if (!bnxt_need_reserve_rings(bp))
7472 		return 0;
7473 
7474 	if (!bnxt_ulp_registered(bp->edev)) {
7475 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
7476 		if (!ulp_msix)
7477 			bnxt_set_ulp_stat_ctxs(bp, 0);
7478 
7479 		if (ulp_msix > bp->ulp_num_msix_want)
7480 			ulp_msix = bp->ulp_num_msix_want;
7481 		hwr.cp = cp + ulp_msix;
7482 	} else {
7483 		hwr.cp = bnxt_nq_rings_in_use(bp);
7484 	}
7485 
7486 	hwr.tx = bp->tx_nr_rings;
7487 	hwr.rx = bp->rx_nr_rings;
7488 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7489 		sh = true;
7490 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7491 		hwr.cp_p5 = hwr.rx + hwr.tx;
7492 
7493 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7494 
7495 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7496 		hwr.rx <<= 1;
7497 	hwr.grp = bp->rx_nr_rings;
7498 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7499 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
7500 
7501 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
7502 	if (rc)
7503 		return rc;
7504 
7505 	bnxt_copy_reserved_rings(bp, &hwr);
7506 
7507 	rx_rings = hwr.rx;
7508 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7509 		if (hwr.rx >= 2) {
7510 			rx_rings = hwr.rx >> 1;
7511 		} else {
7512 			if (netif_running(bp->dev))
7513 				return -ENOMEM;
7514 
7515 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7516 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7517 			bp->dev->hw_features &= ~NETIF_F_LRO;
7518 			bp->dev->features &= ~NETIF_F_LRO;
7519 			bnxt_set_ring_params(bp);
7520 		}
7521 	}
7522 	rx_rings = min_t(int, rx_rings, hwr.grp);
7523 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
7524 	if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
7525 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
7526 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
7527 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
7528 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7529 		hwr.rx = rx_rings << 1;
7530 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
7531 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
7532 	bp->tx_nr_rings = hwr.tx;
7533 
7534 	/* If we cannot reserve all the RX rings, reset the RSS map only
7535 	 * if absolutely necessary
7536 	 */
7537 	if (rx_rings != bp->rx_nr_rings) {
7538 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
7539 			    rx_rings, bp->rx_nr_rings);
7540 		if (netif_is_rxfh_configured(bp->dev) &&
7541 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
7542 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
7543 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
7544 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
7545 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
7546 		}
7547 	}
7548 	bp->rx_nr_rings = rx_rings;
7549 	bp->cp_nr_rings = hwr.cp;
7550 
7551 	if (!bnxt_rings_ok(bp, &hwr))
7552 		return -ENOMEM;
7553 
7554 	if (!netif_is_rxfh_configured(bp->dev))
7555 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7556 
7557 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
7558 		int resv_msix, resv_ctx, ulp_ctxs;
7559 		struct bnxt_hw_resc *hw_resc;
7560 
7561 		hw_resc = &bp->hw_resc;
7562 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
7563 		ulp_msix = min_t(int, resv_msix, ulp_msix);
7564 		bnxt_set_ulp_msix_num(bp, ulp_msix);
7565 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
7566 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
7567 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
7568 	}
7569 
7570 	return rc;
7571 }
7572 
7573 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7574 {
7575 	struct hwrm_func_vf_cfg_input *req;
7576 	u32 flags;
7577 
7578 	if (!BNXT_NEW_RM(bp))
7579 		return 0;
7580 
7581 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7582 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
7583 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7584 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7585 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7586 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
7587 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
7588 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7589 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7590 
7591 	req->flags = cpu_to_le32(flags);
7592 	return hwrm_req_send_silent(bp, req);
7593 }
7594 
7595 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7596 {
7597 	struct hwrm_func_cfg_input *req;
7598 	u32 flags;
7599 
7600 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7601 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
7602 	if (BNXT_NEW_RM(bp)) {
7603 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7604 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7605 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7606 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
7607 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7608 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
7609 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
7610 		else
7611 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7612 	}
7613 
7614 	req->flags = cpu_to_le32(flags);
7615 	return hwrm_req_send_silent(bp, req);
7616 }
7617 
7618 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7619 {
7620 	if (bp->hwrm_spec_code < 0x10801)
7621 		return 0;
7622 
7623 	if (BNXT_PF(bp))
7624 		return bnxt_hwrm_check_pf_rings(bp, hwr);
7625 
7626 	return bnxt_hwrm_check_vf_rings(bp, hwr);
7627 }
7628 
7629 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
7630 {
7631 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7632 	struct hwrm_ring_aggint_qcaps_output *resp;
7633 	struct hwrm_ring_aggint_qcaps_input *req;
7634 	int rc;
7635 
7636 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
7637 	coal_cap->num_cmpl_dma_aggr_max = 63;
7638 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
7639 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
7640 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
7641 	coal_cap->int_lat_tmr_min_max = 65535;
7642 	coal_cap->int_lat_tmr_max_max = 65535;
7643 	coal_cap->num_cmpl_aggr_int_max = 65535;
7644 	coal_cap->timer_units = 80;
7645 
7646 	if (bp->hwrm_spec_code < 0x10902)
7647 		return;
7648 
7649 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
7650 		return;
7651 
7652 	resp = hwrm_req_hold(bp, req);
7653 	rc = hwrm_req_send_silent(bp, req);
7654 	if (!rc) {
7655 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
7656 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
7657 		coal_cap->num_cmpl_dma_aggr_max =
7658 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
7659 		coal_cap->num_cmpl_dma_aggr_during_int_max =
7660 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
7661 		coal_cap->cmpl_aggr_dma_tmr_max =
7662 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
7663 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
7664 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
7665 		coal_cap->int_lat_tmr_min_max =
7666 			le16_to_cpu(resp->int_lat_tmr_min_max);
7667 		coal_cap->int_lat_tmr_max_max =
7668 			le16_to_cpu(resp->int_lat_tmr_max_max);
7669 		coal_cap->num_cmpl_aggr_int_max =
7670 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
7671 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
7672 	}
7673 	hwrm_req_drop(bp, req);
7674 }
7675 
7676 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
7677 {
7678 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7679 
7680 	return usec * 1000 / coal_cap->timer_units;
7681 }
7682 
7683 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
7684 	struct bnxt_coal *hw_coal,
7685 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7686 {
7687 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7688 	u16 val, tmr, max, flags = hw_coal->flags;
7689 	u32 cmpl_params = coal_cap->cmpl_params;
7690 
7691 	max = hw_coal->bufs_per_record * 128;
7692 	if (hw_coal->budget)
7693 		max = hw_coal->bufs_per_record * hw_coal->budget;
7694 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
7695 
7696 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
7697 	req->num_cmpl_aggr_int = cpu_to_le16(val);
7698 
7699 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
7700 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
7701 
7702 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
7703 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
7704 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
7705 
7706 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
7707 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
7708 	req->int_lat_tmr_max = cpu_to_le16(tmr);
7709 
7710 	/* min timer set to 1/2 of interrupt timer */
7711 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
7712 		val = tmr / 2;
7713 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
7714 		req->int_lat_tmr_min = cpu_to_le16(val);
7715 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
7716 	}
7717 
7718 	/* buf timer set to 1/4 of interrupt timer */
7719 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
7720 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
7721 
7722 	if (cmpl_params &
7723 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
7724 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
7725 		val = clamp_t(u16, tmr, 1,
7726 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
7727 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
7728 		req->enables |=
7729 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
7730 	}
7731 
7732 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
7733 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
7734 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
7735 	req->flags = cpu_to_le16(flags);
7736 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
7737 }
7738 
7739 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
7740 				   struct bnxt_coal *hw_coal)
7741 {
7742 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
7743 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7744 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7745 	u32 nq_params = coal_cap->nq_params;
7746 	u16 tmr;
7747 	int rc;
7748 
7749 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
7750 		return 0;
7751 
7752 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7753 	if (rc)
7754 		return rc;
7755 
7756 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
7757 	req->flags =
7758 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
7759 
7760 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
7761 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
7762 	req->int_lat_tmr_min = cpu_to_le16(tmr);
7763 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
7764 	return hwrm_req_send(bp, req);
7765 }
7766 
7767 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
7768 {
7769 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
7770 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7771 	struct bnxt_coal coal;
7772 	int rc;
7773 
7774 	/* Tick values in micro seconds.
7775 	 * 1 coal_buf x bufs_per_record = 1 completion record.
7776 	 */
7777 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
7778 
7779 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
7780 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
7781 
7782 	if (!bnapi->rx_ring)
7783 		return -ENODEV;
7784 
7785 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7786 	if (rc)
7787 		return rc;
7788 
7789 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
7790 
7791 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
7792 
7793 	return hwrm_req_send(bp, req_rx);
7794 }
7795 
7796 static int
7797 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
7798 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7799 {
7800 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
7801 
7802 	req->ring_id = cpu_to_le16(ring_id);
7803 	return hwrm_req_send(bp, req);
7804 }
7805 
7806 static int
7807 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
7808 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7809 {
7810 	struct bnxt_tx_ring_info *txr;
7811 	int i, rc;
7812 
7813 	bnxt_for_each_napi_tx(i, bnapi, txr) {
7814 		u16 ring_id;
7815 
7816 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
7817 		req->ring_id = cpu_to_le16(ring_id);
7818 		rc = hwrm_req_send(bp, req);
7819 		if (rc)
7820 			return rc;
7821 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7822 			return 0;
7823 	}
7824 	return 0;
7825 }
7826 
7827 int bnxt_hwrm_set_coal(struct bnxt *bp)
7828 {
7829 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
7830 	int i, rc;
7831 
7832 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7833 	if (rc)
7834 		return rc;
7835 
7836 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7837 	if (rc) {
7838 		hwrm_req_drop(bp, req_rx);
7839 		return rc;
7840 	}
7841 
7842 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
7843 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
7844 
7845 	hwrm_req_hold(bp, req_rx);
7846 	hwrm_req_hold(bp, req_tx);
7847 	for (i = 0; i < bp->cp_nr_rings; i++) {
7848 		struct bnxt_napi *bnapi = bp->bnapi[i];
7849 		struct bnxt_coal *hw_coal;
7850 
7851 		if (!bnapi->rx_ring)
7852 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
7853 		else
7854 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
7855 		if (rc)
7856 			break;
7857 
7858 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7859 			continue;
7860 
7861 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
7862 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
7863 			if (rc)
7864 				break;
7865 		}
7866 		if (bnapi->rx_ring)
7867 			hw_coal = &bp->rx_coal;
7868 		else
7869 			hw_coal = &bp->tx_coal;
7870 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
7871 	}
7872 	hwrm_req_drop(bp, req_rx);
7873 	hwrm_req_drop(bp, req_tx);
7874 	return rc;
7875 }
7876 
7877 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
7878 {
7879 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
7880 	struct hwrm_stat_ctx_free_input *req;
7881 	int i;
7882 
7883 	if (!bp->bnapi)
7884 		return;
7885 
7886 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
7887 		return;
7888 
7889 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
7890 		return;
7891 	if (BNXT_FW_MAJ(bp) <= 20) {
7892 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
7893 			hwrm_req_drop(bp, req);
7894 			return;
7895 		}
7896 		hwrm_req_hold(bp, req0);
7897 	}
7898 	hwrm_req_hold(bp, req);
7899 	for (i = 0; i < bp->cp_nr_rings; i++) {
7900 		struct bnxt_napi *bnapi = bp->bnapi[i];
7901 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7902 
7903 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
7904 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
7905 			if (req0) {
7906 				req0->stat_ctx_id = req->stat_ctx_id;
7907 				hwrm_req_send(bp, req0);
7908 			}
7909 			hwrm_req_send(bp, req);
7910 
7911 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
7912 		}
7913 	}
7914 	hwrm_req_drop(bp, req);
7915 	if (req0)
7916 		hwrm_req_drop(bp, req0);
7917 }
7918 
7919 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
7920 {
7921 	struct hwrm_stat_ctx_alloc_output *resp;
7922 	struct hwrm_stat_ctx_alloc_input *req;
7923 	int rc, i;
7924 
7925 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
7926 		return 0;
7927 
7928 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
7929 	if (rc)
7930 		return rc;
7931 
7932 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
7933 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
7934 
7935 	resp = hwrm_req_hold(bp, req);
7936 	for (i = 0; i < bp->cp_nr_rings; i++) {
7937 		struct bnxt_napi *bnapi = bp->bnapi[i];
7938 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7939 
7940 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
7941 
7942 		rc = hwrm_req_send(bp, req);
7943 		if (rc)
7944 			break;
7945 
7946 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
7947 
7948 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
7949 	}
7950 	hwrm_req_drop(bp, req);
7951 	return rc;
7952 }
7953 
7954 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
7955 {
7956 	struct hwrm_func_qcfg_output *resp;
7957 	struct hwrm_func_qcfg_input *req;
7958 	u16 flags;
7959 	int rc;
7960 
7961 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7962 	if (rc)
7963 		return rc;
7964 
7965 	req->fid = cpu_to_le16(0xffff);
7966 	resp = hwrm_req_hold(bp, req);
7967 	rc = hwrm_req_send(bp, req);
7968 	if (rc)
7969 		goto func_qcfg_exit;
7970 
7971 #ifdef CONFIG_BNXT_SRIOV
7972 	if (BNXT_VF(bp)) {
7973 		struct bnxt_vf_info *vf = &bp->vf;
7974 
7975 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
7976 	} else {
7977 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
7978 	}
7979 #endif
7980 	flags = le16_to_cpu(resp->flags);
7981 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
7982 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
7983 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
7984 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
7985 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
7986 	}
7987 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
7988 		bp->flags |= BNXT_FLAG_MULTI_HOST;
7989 
7990 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
7991 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
7992 
7993 	switch (resp->port_partition_type) {
7994 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
7995 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
7996 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
7997 		bp->port_partition_type = resp->port_partition_type;
7998 		break;
7999 	}
8000 	if (bp->hwrm_spec_code < 0x10707 ||
8001 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8002 		bp->br_mode = BRIDGE_MODE_VEB;
8003 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8004 		bp->br_mode = BRIDGE_MODE_VEPA;
8005 	else
8006 		bp->br_mode = BRIDGE_MODE_UNDEF;
8007 
8008 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8009 	if (!bp->max_mtu)
8010 		bp->max_mtu = BNXT_MAX_MTU;
8011 
8012 	if (bp->db_size)
8013 		goto func_qcfg_exit;
8014 
8015 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8016 	if (BNXT_CHIP_P5(bp)) {
8017 		if (BNXT_PF(bp))
8018 			bp->db_offset = DB_PF_OFFSET_P5;
8019 		else
8020 			bp->db_offset = DB_VF_OFFSET_P5;
8021 	}
8022 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8023 				 1024);
8024 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8025 	    bp->db_size <= bp->db_offset)
8026 		bp->db_size = pci_resource_len(bp->pdev, 2);
8027 
8028 func_qcfg_exit:
8029 	hwrm_req_drop(bp, req);
8030 	return rc;
8031 }
8032 
8033 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8034 				      u8 init_val, u8 init_offset,
8035 				      bool init_mask_set)
8036 {
8037 	ctxm->init_value = init_val;
8038 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8039 	if (init_mask_set)
8040 		ctxm->init_offset = init_offset * 4;
8041 	else
8042 		ctxm->init_value = 0;
8043 }
8044 
8045 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8046 {
8047 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8048 	u16 type;
8049 
8050 	for (type = 0; type < ctx_max; type++) {
8051 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8052 		int n = 1;
8053 
8054 		if (!ctxm->max_entries)
8055 			continue;
8056 
8057 		if (ctxm->instance_bmap)
8058 			n = hweight32(ctxm->instance_bmap);
8059 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8060 		if (!ctxm->pg_info)
8061 			return -ENOMEM;
8062 	}
8063 	return 0;
8064 }
8065 
8066 #define BNXT_CTX_INIT_VALID(flags)	\
8067 	(!!((flags) &			\
8068 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8069 
8070 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8071 {
8072 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8073 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8074 	struct bnxt_ctx_mem_info *ctx;
8075 	u16 type;
8076 	int rc;
8077 
8078 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8079 	if (rc)
8080 		return rc;
8081 
8082 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8083 	if (!ctx)
8084 		return -ENOMEM;
8085 	bp->ctx = ctx;
8086 
8087 	resp = hwrm_req_hold(bp, req);
8088 
8089 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8090 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8091 		u8 init_val, init_off, i;
8092 		__le32 *p;
8093 		u32 flags;
8094 
8095 		req->type = cpu_to_le16(type);
8096 		rc = hwrm_req_send(bp, req);
8097 		if (rc)
8098 			goto ctx_done;
8099 		flags = le32_to_cpu(resp->flags);
8100 		type = le16_to_cpu(resp->next_valid_type);
8101 		if (!(flags & FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_TYPE_VALID))
8102 			continue;
8103 
8104 		ctxm->type = le16_to_cpu(resp->type);
8105 		ctxm->entry_size = le16_to_cpu(resp->entry_size);
8106 		ctxm->flags = flags;
8107 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8108 		ctxm->entry_multiple = resp->entry_multiple;
8109 		ctxm->max_entries = le32_to_cpu(resp->max_num_entries);
8110 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8111 		init_val = resp->ctx_init_value;
8112 		init_off = resp->ctx_init_offset;
8113 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8114 					  BNXT_CTX_INIT_VALID(flags));
8115 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8116 					      BNXT_MAX_SPLIT_ENTRY);
8117 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8118 		     i++, p++)
8119 			ctxm->split[i] = le32_to_cpu(*p);
8120 	}
8121 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8122 
8123 ctx_done:
8124 	hwrm_req_drop(bp, req);
8125 	return rc;
8126 }
8127 
8128 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8129 {
8130 	struct hwrm_func_backing_store_qcaps_output *resp;
8131 	struct hwrm_func_backing_store_qcaps_input *req;
8132 	int rc;
8133 
8134 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx)
8135 		return 0;
8136 
8137 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8138 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8139 
8140 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8141 	if (rc)
8142 		return rc;
8143 
8144 	resp = hwrm_req_hold(bp, req);
8145 	rc = hwrm_req_send_silent(bp, req);
8146 	if (!rc) {
8147 		struct bnxt_ctx_mem_type *ctxm;
8148 		struct bnxt_ctx_mem_info *ctx;
8149 		u8 init_val, init_idx = 0;
8150 		u16 init_mask;
8151 
8152 		ctx = bp->ctx;
8153 		if (!ctx) {
8154 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8155 			if (!ctx) {
8156 				rc = -ENOMEM;
8157 				goto ctx_err;
8158 			}
8159 			bp->ctx = ctx;
8160 		}
8161 		init_val = resp->ctx_kind_initializer;
8162 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8163 
8164 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8165 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8166 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8167 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8168 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8169 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8170 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8171 					  (init_mask & (1 << init_idx++)) != 0);
8172 
8173 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8174 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8175 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8176 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8177 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8178 					  (init_mask & (1 << init_idx++)) != 0);
8179 
8180 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8181 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8182 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8183 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8184 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8185 					  (init_mask & (1 << init_idx++)) != 0);
8186 
8187 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8188 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8189 		ctxm->max_entries = ctxm->vnic_entries +
8190 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8191 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8192 		bnxt_init_ctx_initializer(ctxm, init_val,
8193 					  resp->vnic_init_offset,
8194 					  (init_mask & (1 << init_idx++)) != 0);
8195 
8196 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8197 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8198 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8199 		bnxt_init_ctx_initializer(ctxm, init_val,
8200 					  resp->stat_init_offset,
8201 					  (init_mask & (1 << init_idx++)) != 0);
8202 
8203 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8204 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8205 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8206 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8207 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8208 		if (!ctxm->entry_multiple)
8209 			ctxm->entry_multiple = 1;
8210 
8211 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8212 
8213 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8214 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8215 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8216 		ctxm->mrav_num_entries_units =
8217 			le16_to_cpu(resp->mrav_num_entries_units);
8218 		bnxt_init_ctx_initializer(ctxm, init_val,
8219 					  resp->mrav_init_offset,
8220 					  (init_mask & (1 << init_idx++)) != 0);
8221 
8222 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8223 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8224 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8225 
8226 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8227 		if (!ctx->tqm_fp_rings_count)
8228 			ctx->tqm_fp_rings_count = bp->max_q;
8229 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8230 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8231 
8232 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8233 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8234 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8235 
8236 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8237 	} else {
8238 		rc = 0;
8239 	}
8240 ctx_err:
8241 	hwrm_req_drop(bp, req);
8242 	return rc;
8243 }
8244 
8245 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8246 				  __le64 *pg_dir)
8247 {
8248 	if (!rmem->nr_pages)
8249 		return;
8250 
8251 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8252 	if (rmem->depth >= 1) {
8253 		if (rmem->depth == 2)
8254 			*pg_attr |= 2;
8255 		else
8256 			*pg_attr |= 1;
8257 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8258 	} else {
8259 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8260 	}
8261 }
8262 
8263 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8264 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8265 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8266 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8267 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8268 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8269 
8270 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8271 {
8272 	struct hwrm_func_backing_store_cfg_input *req;
8273 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8274 	struct bnxt_ctx_pg_info *ctx_pg;
8275 	struct bnxt_ctx_mem_type *ctxm;
8276 	void **__req = (void **)&req;
8277 	u32 req_len = sizeof(*req);
8278 	__le32 *num_entries;
8279 	__le64 *pg_dir;
8280 	u32 flags = 0;
8281 	u8 *pg_attr;
8282 	u32 ena;
8283 	int rc;
8284 	int i;
8285 
8286 	if (!ctx)
8287 		return 0;
8288 
8289 	if (req_len > bp->hwrm_max_ext_req_len)
8290 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8291 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8292 	if (rc)
8293 		return rc;
8294 
8295 	req->enables = cpu_to_le32(enables);
8296 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8297 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8298 		ctx_pg = ctxm->pg_info;
8299 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8300 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8301 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8302 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8303 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8304 				      &req->qpc_pg_size_qpc_lvl,
8305 				      &req->qpc_page_dir);
8306 
8307 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8308 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8309 	}
8310 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8311 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8312 		ctx_pg = ctxm->pg_info;
8313 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8314 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8315 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8316 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8317 				      &req->srq_pg_size_srq_lvl,
8318 				      &req->srq_page_dir);
8319 	}
8320 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8321 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8322 		ctx_pg = ctxm->pg_info;
8323 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8324 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8325 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8326 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8327 				      &req->cq_pg_size_cq_lvl,
8328 				      &req->cq_page_dir);
8329 	}
8330 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8331 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8332 		ctx_pg = ctxm->pg_info;
8333 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8334 		req->vnic_num_ring_table_entries =
8335 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8336 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8337 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8338 				      &req->vnic_pg_size_vnic_lvl,
8339 				      &req->vnic_page_dir);
8340 	}
8341 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8342 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8343 		ctx_pg = ctxm->pg_info;
8344 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8345 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8346 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8347 				      &req->stat_pg_size_stat_lvl,
8348 				      &req->stat_page_dir);
8349 	}
8350 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8351 		u32 units;
8352 
8353 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8354 		ctx_pg = ctxm->pg_info;
8355 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8356 		units = ctxm->mrav_num_entries_units;
8357 		if (units) {
8358 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8359 			u32 entries;
8360 
8361 			num_mr = ctx_pg->entries - num_ah;
8362 			entries = ((num_mr / units) << 16) | (num_ah / units);
8363 			req->mrav_num_entries = cpu_to_le32(entries);
8364 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8365 		}
8366 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8367 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8368 				      &req->mrav_pg_size_mrav_lvl,
8369 				      &req->mrav_page_dir);
8370 	}
8371 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8372 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8373 		ctx_pg = ctxm->pg_info;
8374 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8375 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8376 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8377 				      &req->tim_pg_size_tim_lvl,
8378 				      &req->tim_page_dir);
8379 	}
8380 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8381 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8382 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8383 	     pg_dir = &req->tqm_sp_page_dir,
8384 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8385 	     ctx_pg = ctxm->pg_info;
8386 	     i < BNXT_MAX_TQM_RINGS;
8387 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8388 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8389 		if (!(enables & ena))
8390 			continue;
8391 
8392 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8393 		*num_entries = cpu_to_le32(ctx_pg->entries);
8394 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8395 	}
8396 	req->flags = cpu_to_le32(flags);
8397 	return hwrm_req_send(bp, req);
8398 }
8399 
8400 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8401 				  struct bnxt_ctx_pg_info *ctx_pg)
8402 {
8403 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8404 
8405 	rmem->page_size = BNXT_PAGE_SIZE;
8406 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8407 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8408 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8409 	if (rmem->depth >= 1)
8410 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8411 	return bnxt_alloc_ring(bp, rmem);
8412 }
8413 
8414 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8415 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8416 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8417 {
8418 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8419 	int rc;
8420 
8421 	if (!mem_size)
8422 		return -EINVAL;
8423 
8424 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8425 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8426 		ctx_pg->nr_pages = 0;
8427 		return -EINVAL;
8428 	}
8429 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8430 		int nr_tbls, i;
8431 
8432 		rmem->depth = 2;
8433 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8434 					     GFP_KERNEL);
8435 		if (!ctx_pg->ctx_pg_tbl)
8436 			return -ENOMEM;
8437 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8438 		rmem->nr_pages = nr_tbls;
8439 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8440 		if (rc)
8441 			return rc;
8442 		for (i = 0; i < nr_tbls; i++) {
8443 			struct bnxt_ctx_pg_info *pg_tbl;
8444 
8445 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8446 			if (!pg_tbl)
8447 				return -ENOMEM;
8448 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8449 			rmem = &pg_tbl->ring_mem;
8450 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8451 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8452 			rmem->depth = 1;
8453 			rmem->nr_pages = MAX_CTX_PAGES;
8454 			rmem->ctx_mem = ctxm;
8455 			if (i == (nr_tbls - 1)) {
8456 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8457 
8458 				if (rem)
8459 					rmem->nr_pages = rem;
8460 			}
8461 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8462 			if (rc)
8463 				break;
8464 		}
8465 	} else {
8466 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8467 		if (rmem->nr_pages > 1 || depth)
8468 			rmem->depth = 1;
8469 		rmem->ctx_mem = ctxm;
8470 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8471 	}
8472 	return rc;
8473 }
8474 
8475 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
8476 				  struct bnxt_ctx_pg_info *ctx_pg)
8477 {
8478 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8479 
8480 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
8481 	    ctx_pg->ctx_pg_tbl) {
8482 		int i, nr_tbls = rmem->nr_pages;
8483 
8484 		for (i = 0; i < nr_tbls; i++) {
8485 			struct bnxt_ctx_pg_info *pg_tbl;
8486 			struct bnxt_ring_mem_info *rmem2;
8487 
8488 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8489 			if (!pg_tbl)
8490 				continue;
8491 			rmem2 = &pg_tbl->ring_mem;
8492 			bnxt_free_ring(bp, rmem2);
8493 			ctx_pg->ctx_pg_arr[i] = NULL;
8494 			kfree(pg_tbl);
8495 			ctx_pg->ctx_pg_tbl[i] = NULL;
8496 		}
8497 		kfree(ctx_pg->ctx_pg_tbl);
8498 		ctx_pg->ctx_pg_tbl = NULL;
8499 	}
8500 	bnxt_free_ring(bp, rmem);
8501 	ctx_pg->nr_pages = 0;
8502 }
8503 
8504 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
8505 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
8506 				   u8 pg_lvl)
8507 {
8508 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8509 	int i, rc = 0, n = 1;
8510 	u32 mem_size;
8511 
8512 	if (!ctxm->entry_size || !ctx_pg)
8513 		return -EINVAL;
8514 	if (ctxm->instance_bmap)
8515 		n = hweight32(ctxm->instance_bmap);
8516 	if (ctxm->entry_multiple)
8517 		entries = roundup(entries, ctxm->entry_multiple);
8518 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
8519 	mem_size = entries * ctxm->entry_size;
8520 	for (i = 0; i < n && !rc; i++) {
8521 		ctx_pg[i].entries = entries;
8522 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
8523 					    ctxm->init_value ? ctxm : NULL);
8524 	}
8525 	return rc;
8526 }
8527 
8528 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
8529 					       struct bnxt_ctx_mem_type *ctxm,
8530 					       bool last)
8531 {
8532 	struct hwrm_func_backing_store_cfg_v2_input *req;
8533 	u32 instance_bmap = ctxm->instance_bmap;
8534 	int i, j, rc = 0, n = 1;
8535 	__le32 *p;
8536 
8537 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
8538 		return 0;
8539 
8540 	if (instance_bmap)
8541 		n = hweight32(ctxm->instance_bmap);
8542 	else
8543 		instance_bmap = 1;
8544 
8545 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
8546 	if (rc)
8547 		return rc;
8548 	hwrm_req_hold(bp, req);
8549 	req->type = cpu_to_le16(ctxm->type);
8550 	req->entry_size = cpu_to_le16(ctxm->entry_size);
8551 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
8552 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
8553 		p[i] = cpu_to_le32(ctxm->split[i]);
8554 	for (i = 0, j = 0; j < n && !rc; i++) {
8555 		struct bnxt_ctx_pg_info *ctx_pg;
8556 
8557 		if (!(instance_bmap & (1 << i)))
8558 			continue;
8559 		req->instance = cpu_to_le16(i);
8560 		ctx_pg = &ctxm->pg_info[j++];
8561 		if (!ctx_pg->entries)
8562 			continue;
8563 		req->num_entries = cpu_to_le32(ctx_pg->entries);
8564 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8565 				      &req->page_size_pbl_level,
8566 				      &req->page_dir);
8567 		if (last && j == n)
8568 			req->flags =
8569 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
8570 		rc = hwrm_req_send(bp, req);
8571 	}
8572 	hwrm_req_drop(bp, req);
8573 	return rc;
8574 }
8575 
8576 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
8577 {
8578 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8579 	struct bnxt_ctx_mem_type *ctxm;
8580 	u16 last_type;
8581 	int rc = 0;
8582 	u16 type;
8583 
8584 	if (!ena)
8585 		return 0;
8586 	else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
8587 		last_type = BNXT_CTX_MAX - 1;
8588 	else
8589 		last_type = BNXT_CTX_L2_MAX - 1;
8590 	ctx->ctx_arr[last_type].last = 1;
8591 
8592 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
8593 		ctxm = &ctx->ctx_arr[type];
8594 
8595 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
8596 		if (rc)
8597 			return rc;
8598 	}
8599 	return 0;
8600 }
8601 
8602 void bnxt_free_ctx_mem(struct bnxt *bp)
8603 {
8604 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8605 	u16 type;
8606 
8607 	if (!ctx)
8608 		return;
8609 
8610 	for (type = 0; type < BNXT_CTX_V2_MAX; type++) {
8611 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8612 		struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8613 		int i, n = 1;
8614 
8615 		if (!ctx_pg)
8616 			continue;
8617 		if (ctxm->instance_bmap)
8618 			n = hweight32(ctxm->instance_bmap);
8619 		for (i = 0; i < n; i++)
8620 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
8621 
8622 		kfree(ctx_pg);
8623 		ctxm->pg_info = NULL;
8624 	}
8625 
8626 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
8627 	kfree(ctx);
8628 	bp->ctx = NULL;
8629 }
8630 
8631 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
8632 {
8633 	struct bnxt_ctx_mem_type *ctxm;
8634 	struct bnxt_ctx_mem_info *ctx;
8635 	u32 l2_qps, qp1_qps, max_qps;
8636 	u32 ena, entries_sp, entries;
8637 	u32 srqs, max_srqs, min;
8638 	u32 num_mr, num_ah;
8639 	u32 extra_srqs = 0;
8640 	u32 extra_qps = 0;
8641 	u32 fast_qpmd_qps;
8642 	u8 pg_lvl = 1;
8643 	int i, rc;
8644 
8645 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
8646 	if (rc) {
8647 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
8648 			   rc);
8649 		return rc;
8650 	}
8651 	ctx = bp->ctx;
8652 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
8653 		return 0;
8654 
8655 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8656 	l2_qps = ctxm->qp_l2_entries;
8657 	qp1_qps = ctxm->qp_qp1_entries;
8658 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
8659 	max_qps = ctxm->max_entries;
8660 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8661 	srqs = ctxm->srq_l2_entries;
8662 	max_srqs = ctxm->max_entries;
8663 	ena = 0;
8664 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
8665 		pg_lvl = 2;
8666 		extra_qps = min_t(u32, 65536, max_qps - l2_qps - qp1_qps);
8667 		/* allocate extra qps if fw supports RoCE fast qp destroy feature */
8668 		extra_qps += fast_qpmd_qps;
8669 		extra_srqs = min_t(u32, 8192, max_srqs - srqs);
8670 		if (fast_qpmd_qps)
8671 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
8672 	}
8673 
8674 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8675 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
8676 				     pg_lvl);
8677 	if (rc)
8678 		return rc;
8679 
8680 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8681 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
8682 	if (rc)
8683 		return rc;
8684 
8685 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8686 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
8687 				     extra_qps * 2, pg_lvl);
8688 	if (rc)
8689 		return rc;
8690 
8691 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8692 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
8693 	if (rc)
8694 		return rc;
8695 
8696 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8697 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
8698 	if (rc)
8699 		return rc;
8700 
8701 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
8702 		goto skip_rdma;
8703 
8704 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8705 	/* 128K extra is needed to accommodate static AH context
8706 	 * allocation by f/w.
8707 	 */
8708 	num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
8709 	num_ah = min_t(u32, num_mr, 1024 * 128);
8710 	ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
8711 	if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
8712 		ctxm->mrav_av_entries = num_ah;
8713 
8714 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
8715 	if (rc)
8716 		return rc;
8717 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
8718 
8719 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8720 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
8721 	if (rc)
8722 		return rc;
8723 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
8724 
8725 skip_rdma:
8726 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8727 	min = ctxm->min_entries;
8728 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
8729 		     2 * (extra_qps + qp1_qps) + min;
8730 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
8731 	if (rc)
8732 		return rc;
8733 
8734 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8735 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
8736 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
8737 	if (rc)
8738 		return rc;
8739 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
8740 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
8741 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
8742 
8743 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8744 		rc = bnxt_backing_store_cfg_v2(bp, ena);
8745 	else
8746 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
8747 	if (rc) {
8748 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
8749 			   rc);
8750 		return rc;
8751 	}
8752 	ctx->flags |= BNXT_CTX_FLAG_INITED;
8753 	return 0;
8754 }
8755 
8756 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
8757 {
8758 	struct hwrm_func_resource_qcaps_output *resp;
8759 	struct hwrm_func_resource_qcaps_input *req;
8760 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8761 	int rc;
8762 
8763 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
8764 	if (rc)
8765 		return rc;
8766 
8767 	req->fid = cpu_to_le16(0xffff);
8768 	resp = hwrm_req_hold(bp, req);
8769 	rc = hwrm_req_send_silent(bp, req);
8770 	if (rc)
8771 		goto hwrm_func_resc_qcaps_exit;
8772 
8773 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
8774 	if (!all)
8775 		goto hwrm_func_resc_qcaps_exit;
8776 
8777 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
8778 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
8779 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
8780 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
8781 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
8782 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
8783 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
8784 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
8785 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
8786 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
8787 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
8788 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
8789 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
8790 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
8791 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
8792 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
8793 
8794 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8795 		u16 max_msix = le16_to_cpu(resp->max_msix);
8796 
8797 		hw_resc->max_nqs = max_msix;
8798 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
8799 	}
8800 
8801 	if (BNXT_PF(bp)) {
8802 		struct bnxt_pf_info *pf = &bp->pf;
8803 
8804 		pf->vf_resv_strategy =
8805 			le16_to_cpu(resp->vf_reservation_strategy);
8806 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
8807 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
8808 	}
8809 hwrm_func_resc_qcaps_exit:
8810 	hwrm_req_drop(bp, req);
8811 	return rc;
8812 }
8813 
8814 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
8815 {
8816 	struct hwrm_port_mac_ptp_qcfg_output *resp;
8817 	struct hwrm_port_mac_ptp_qcfg_input *req;
8818 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
8819 	bool phc_cfg;
8820 	u8 flags;
8821 	int rc;
8822 
8823 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5(bp)) {
8824 		rc = -ENODEV;
8825 		goto no_ptp;
8826 	}
8827 
8828 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
8829 	if (rc)
8830 		goto no_ptp;
8831 
8832 	req->port_id = cpu_to_le16(bp->pf.port_id);
8833 	resp = hwrm_req_hold(bp, req);
8834 	rc = hwrm_req_send(bp, req);
8835 	if (rc)
8836 		goto exit;
8837 
8838 	flags = resp->flags;
8839 	if (!(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
8840 		rc = -ENODEV;
8841 		goto exit;
8842 	}
8843 	if (!ptp) {
8844 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
8845 		if (!ptp) {
8846 			rc = -ENOMEM;
8847 			goto exit;
8848 		}
8849 		ptp->bp = bp;
8850 		bp->ptp_cfg = ptp;
8851 	}
8852 	if (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK) {
8853 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
8854 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
8855 	} else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8856 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
8857 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
8858 	} else {
8859 		rc = -ENODEV;
8860 		goto exit;
8861 	}
8862 	phc_cfg = (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
8863 	rc = bnxt_ptp_init(bp, phc_cfg);
8864 	if (rc)
8865 		netdev_warn(bp->dev, "PTP initialization failed.\n");
8866 exit:
8867 	hwrm_req_drop(bp, req);
8868 	if (!rc)
8869 		return 0;
8870 
8871 no_ptp:
8872 	bnxt_ptp_clear(bp);
8873 	kfree(ptp);
8874 	bp->ptp_cfg = NULL;
8875 	return rc;
8876 }
8877 
8878 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
8879 {
8880 	struct hwrm_func_qcaps_output *resp;
8881 	struct hwrm_func_qcaps_input *req;
8882 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8883 	u32 flags, flags_ext, flags_ext2;
8884 	int rc;
8885 
8886 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
8887 	if (rc)
8888 		return rc;
8889 
8890 	req->fid = cpu_to_le16(0xffff);
8891 	resp = hwrm_req_hold(bp, req);
8892 	rc = hwrm_req_send(bp, req);
8893 	if (rc)
8894 		goto hwrm_func_qcaps_exit;
8895 
8896 	flags = le32_to_cpu(resp->flags);
8897 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
8898 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
8899 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
8900 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
8901 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
8902 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
8903 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
8904 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
8905 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
8906 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
8907 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
8908 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
8909 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
8910 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
8911 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
8912 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
8913 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
8914 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
8915 
8916 	flags_ext = le32_to_cpu(resp->flags_ext);
8917 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
8918 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
8919 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
8920 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
8921 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
8922 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
8923 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
8924 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
8925 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
8926 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
8927 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
8928 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
8929 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
8930 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
8931 
8932 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
8933 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
8934 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
8935 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
8936 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
8937 
8938 	bp->tx_push_thresh = 0;
8939 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
8940 	    BNXT_FW_MAJ(bp) > 217)
8941 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
8942 
8943 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
8944 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
8945 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
8946 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
8947 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
8948 	if (!hw_resc->max_hw_ring_grps)
8949 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
8950 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
8951 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
8952 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
8953 
8954 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
8955 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
8956 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
8957 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
8958 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
8959 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
8960 
8961 	if (BNXT_PF(bp)) {
8962 		struct bnxt_pf_info *pf = &bp->pf;
8963 
8964 		pf->fw_fid = le16_to_cpu(resp->fid);
8965 		pf->port_id = le16_to_cpu(resp->port_id);
8966 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
8967 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
8968 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
8969 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
8970 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
8971 			bp->flags |= BNXT_FLAG_WOL_CAP;
8972 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
8973 			bp->fw_cap |= BNXT_FW_CAP_PTP;
8974 		} else {
8975 			bnxt_ptp_clear(bp);
8976 			kfree(bp->ptp_cfg);
8977 			bp->ptp_cfg = NULL;
8978 		}
8979 	} else {
8980 #ifdef CONFIG_BNXT_SRIOV
8981 		struct bnxt_vf_info *vf = &bp->vf;
8982 
8983 		vf->fw_fid = le16_to_cpu(resp->fid);
8984 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
8985 #endif
8986 	}
8987 
8988 hwrm_func_qcaps_exit:
8989 	hwrm_req_drop(bp, req);
8990 	return rc;
8991 }
8992 
8993 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
8994 {
8995 	struct hwrm_dbg_qcaps_output *resp;
8996 	struct hwrm_dbg_qcaps_input *req;
8997 	int rc;
8998 
8999 	bp->fw_dbg_cap = 0;
9000 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9001 		return;
9002 
9003 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9004 	if (rc)
9005 		return;
9006 
9007 	req->fid = cpu_to_le16(0xffff);
9008 	resp = hwrm_req_hold(bp, req);
9009 	rc = hwrm_req_send(bp, req);
9010 	if (rc)
9011 		goto hwrm_dbg_qcaps_exit;
9012 
9013 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9014 
9015 hwrm_dbg_qcaps_exit:
9016 	hwrm_req_drop(bp, req);
9017 }
9018 
9019 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9020 
9021 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9022 {
9023 	int rc;
9024 
9025 	rc = __bnxt_hwrm_func_qcaps(bp);
9026 	if (rc)
9027 		return rc;
9028 
9029 	bnxt_hwrm_dbg_qcaps(bp);
9030 
9031 	rc = bnxt_hwrm_queue_qportcfg(bp);
9032 	if (rc) {
9033 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9034 		return rc;
9035 	}
9036 	if (bp->hwrm_spec_code >= 0x10803) {
9037 		rc = bnxt_alloc_ctx_mem(bp);
9038 		if (rc)
9039 			return rc;
9040 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9041 		if (!rc)
9042 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9043 	}
9044 	return 0;
9045 }
9046 
9047 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9048 {
9049 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9050 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9051 	u32 flags;
9052 	int rc;
9053 
9054 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9055 		return 0;
9056 
9057 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9058 	if (rc)
9059 		return rc;
9060 
9061 	resp = hwrm_req_hold(bp, req);
9062 	rc = hwrm_req_send(bp, req);
9063 	if (rc)
9064 		goto hwrm_cfa_adv_qcaps_exit;
9065 
9066 	flags = le32_to_cpu(resp->flags);
9067 	if (flags &
9068 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9069 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9070 
9071 	if (flags &
9072 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9073 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9074 
9075 	if (flags &
9076 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9077 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9078 
9079 hwrm_cfa_adv_qcaps_exit:
9080 	hwrm_req_drop(bp, req);
9081 	return rc;
9082 }
9083 
9084 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9085 {
9086 	if (bp->fw_health)
9087 		return 0;
9088 
9089 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9090 	if (!bp->fw_health)
9091 		return -ENOMEM;
9092 
9093 	mutex_init(&bp->fw_health->lock);
9094 	return 0;
9095 }
9096 
9097 static int bnxt_alloc_fw_health(struct bnxt *bp)
9098 {
9099 	int rc;
9100 
9101 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9102 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9103 		return 0;
9104 
9105 	rc = __bnxt_alloc_fw_health(bp);
9106 	if (rc) {
9107 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9108 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9109 		return rc;
9110 	}
9111 
9112 	return 0;
9113 }
9114 
9115 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9116 {
9117 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9118 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9119 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9120 }
9121 
9122 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9123 {
9124 	struct bnxt_fw_health *fw_health = bp->fw_health;
9125 	u32 reg_type;
9126 
9127 	if (!fw_health)
9128 		return;
9129 
9130 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9131 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9132 		fw_health->status_reliable = false;
9133 
9134 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9135 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9136 		fw_health->resets_reliable = false;
9137 }
9138 
9139 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9140 {
9141 	void __iomem *hs;
9142 	u32 status_loc;
9143 	u32 reg_type;
9144 	u32 sig;
9145 
9146 	if (bp->fw_health)
9147 		bp->fw_health->status_reliable = false;
9148 
9149 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9150 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9151 
9152 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9153 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9154 		if (!bp->chip_num) {
9155 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9156 			bp->chip_num = readl(bp->bar0 +
9157 					     BNXT_FW_HEALTH_WIN_BASE +
9158 					     BNXT_GRC_REG_CHIP_NUM);
9159 		}
9160 		if (!BNXT_CHIP_P5_PLUS(bp))
9161 			return;
9162 
9163 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9164 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9165 	} else {
9166 		status_loc = readl(hs + offsetof(struct hcomm_status,
9167 						 fw_status_loc));
9168 	}
9169 
9170 	if (__bnxt_alloc_fw_health(bp)) {
9171 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9172 		return;
9173 	}
9174 
9175 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9176 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9177 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9178 		__bnxt_map_fw_health_reg(bp, status_loc);
9179 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9180 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9181 	}
9182 
9183 	bp->fw_health->status_reliable = true;
9184 }
9185 
9186 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9187 {
9188 	struct bnxt_fw_health *fw_health = bp->fw_health;
9189 	u32 reg_base = 0xffffffff;
9190 	int i;
9191 
9192 	bp->fw_health->status_reliable = false;
9193 	bp->fw_health->resets_reliable = false;
9194 	/* Only pre-map the monitoring GRC registers using window 3 */
9195 	for (i = 0; i < 4; i++) {
9196 		u32 reg = fw_health->regs[i];
9197 
9198 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9199 			continue;
9200 		if (reg_base == 0xffffffff)
9201 			reg_base = reg & BNXT_GRC_BASE_MASK;
9202 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9203 			return -ERANGE;
9204 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9205 	}
9206 	bp->fw_health->status_reliable = true;
9207 	bp->fw_health->resets_reliable = true;
9208 	if (reg_base == 0xffffffff)
9209 		return 0;
9210 
9211 	__bnxt_map_fw_health_reg(bp, reg_base);
9212 	return 0;
9213 }
9214 
9215 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9216 {
9217 	if (!bp->fw_health)
9218 		return;
9219 
9220 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9221 		bp->fw_health->status_reliable = true;
9222 		bp->fw_health->resets_reliable = true;
9223 	} else {
9224 		bnxt_try_map_fw_health_reg(bp);
9225 	}
9226 }
9227 
9228 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9229 {
9230 	struct bnxt_fw_health *fw_health = bp->fw_health;
9231 	struct hwrm_error_recovery_qcfg_output *resp;
9232 	struct hwrm_error_recovery_qcfg_input *req;
9233 	int rc, i;
9234 
9235 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9236 		return 0;
9237 
9238 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9239 	if (rc)
9240 		return rc;
9241 
9242 	resp = hwrm_req_hold(bp, req);
9243 	rc = hwrm_req_send(bp, req);
9244 	if (rc)
9245 		goto err_recovery_out;
9246 	fw_health->flags = le32_to_cpu(resp->flags);
9247 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
9248 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
9249 		rc = -EINVAL;
9250 		goto err_recovery_out;
9251 	}
9252 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
9253 	fw_health->master_func_wait_dsecs =
9254 		le32_to_cpu(resp->master_func_wait_period);
9255 	fw_health->normal_func_wait_dsecs =
9256 		le32_to_cpu(resp->normal_func_wait_period);
9257 	fw_health->post_reset_wait_dsecs =
9258 		le32_to_cpu(resp->master_func_wait_period_after_reset);
9259 	fw_health->post_reset_max_wait_dsecs =
9260 		le32_to_cpu(resp->max_bailout_time_after_reset);
9261 	fw_health->regs[BNXT_FW_HEALTH_REG] =
9262 		le32_to_cpu(resp->fw_health_status_reg);
9263 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
9264 		le32_to_cpu(resp->fw_heartbeat_reg);
9265 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
9266 		le32_to_cpu(resp->fw_reset_cnt_reg);
9267 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
9268 		le32_to_cpu(resp->reset_inprogress_reg);
9269 	fw_health->fw_reset_inprog_reg_mask =
9270 		le32_to_cpu(resp->reset_inprogress_reg_mask);
9271 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
9272 	if (fw_health->fw_reset_seq_cnt >= 16) {
9273 		rc = -EINVAL;
9274 		goto err_recovery_out;
9275 	}
9276 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
9277 		fw_health->fw_reset_seq_regs[i] =
9278 			le32_to_cpu(resp->reset_reg[i]);
9279 		fw_health->fw_reset_seq_vals[i] =
9280 			le32_to_cpu(resp->reset_reg_val[i]);
9281 		fw_health->fw_reset_seq_delay_msec[i] =
9282 			resp->delay_after_reset[i];
9283 	}
9284 err_recovery_out:
9285 	hwrm_req_drop(bp, req);
9286 	if (!rc)
9287 		rc = bnxt_map_fw_health_regs(bp);
9288 	if (rc)
9289 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9290 	return rc;
9291 }
9292 
9293 static int bnxt_hwrm_func_reset(struct bnxt *bp)
9294 {
9295 	struct hwrm_func_reset_input *req;
9296 	int rc;
9297 
9298 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
9299 	if (rc)
9300 		return rc;
9301 
9302 	req->enables = 0;
9303 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
9304 	return hwrm_req_send(bp, req);
9305 }
9306 
9307 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
9308 {
9309 	struct hwrm_nvm_get_dev_info_output nvm_info;
9310 
9311 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
9312 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
9313 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
9314 			 nvm_info.nvm_cfg_ver_upd);
9315 }
9316 
9317 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
9318 {
9319 	struct hwrm_queue_qportcfg_output *resp;
9320 	struct hwrm_queue_qportcfg_input *req;
9321 	u8 i, j, *qptr;
9322 	bool no_rdma;
9323 	int rc = 0;
9324 
9325 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
9326 	if (rc)
9327 		return rc;
9328 
9329 	resp = hwrm_req_hold(bp, req);
9330 	rc = hwrm_req_send(bp, req);
9331 	if (rc)
9332 		goto qportcfg_exit;
9333 
9334 	if (!resp->max_configurable_queues) {
9335 		rc = -EINVAL;
9336 		goto qportcfg_exit;
9337 	}
9338 	bp->max_tc = resp->max_configurable_queues;
9339 	bp->max_lltc = resp->max_configurable_lossless_queues;
9340 	if (bp->max_tc > BNXT_MAX_QUEUE)
9341 		bp->max_tc = BNXT_MAX_QUEUE;
9342 
9343 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
9344 	qptr = &resp->queue_id0;
9345 	for (i = 0, j = 0; i < bp->max_tc; i++) {
9346 		bp->q_info[j].queue_id = *qptr;
9347 		bp->q_ids[i] = *qptr++;
9348 		bp->q_info[j].queue_profile = *qptr++;
9349 		bp->tc_to_qidx[j] = j;
9350 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
9351 		    (no_rdma && BNXT_PF(bp)))
9352 			j++;
9353 	}
9354 	bp->max_q = bp->max_tc;
9355 	bp->max_tc = max_t(u8, j, 1);
9356 
9357 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
9358 		bp->max_tc = 1;
9359 
9360 	if (bp->max_lltc > bp->max_tc)
9361 		bp->max_lltc = bp->max_tc;
9362 
9363 qportcfg_exit:
9364 	hwrm_req_drop(bp, req);
9365 	return rc;
9366 }
9367 
9368 static int bnxt_hwrm_poll(struct bnxt *bp)
9369 {
9370 	struct hwrm_ver_get_input *req;
9371 	int rc;
9372 
9373 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9374 	if (rc)
9375 		return rc;
9376 
9377 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9378 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9379 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9380 
9381 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
9382 	rc = hwrm_req_send(bp, req);
9383 	return rc;
9384 }
9385 
9386 static int bnxt_hwrm_ver_get(struct bnxt *bp)
9387 {
9388 	struct hwrm_ver_get_output *resp;
9389 	struct hwrm_ver_get_input *req;
9390 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
9391 	u32 dev_caps_cfg, hwrm_ver;
9392 	int rc, len;
9393 
9394 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9395 	if (rc)
9396 		return rc;
9397 
9398 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
9399 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
9400 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9401 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9402 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9403 
9404 	resp = hwrm_req_hold(bp, req);
9405 	rc = hwrm_req_send(bp, req);
9406 	if (rc)
9407 		goto hwrm_ver_get_exit;
9408 
9409 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
9410 
9411 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
9412 			     resp->hwrm_intf_min_8b << 8 |
9413 			     resp->hwrm_intf_upd_8b;
9414 	if (resp->hwrm_intf_maj_8b < 1) {
9415 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
9416 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
9417 			    resp->hwrm_intf_upd_8b);
9418 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
9419 	}
9420 
9421 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
9422 			HWRM_VERSION_UPDATE;
9423 
9424 	if (bp->hwrm_spec_code > hwrm_ver)
9425 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
9426 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
9427 			 HWRM_VERSION_UPDATE);
9428 	else
9429 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
9430 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
9431 			 resp->hwrm_intf_upd_8b);
9432 
9433 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
9434 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
9435 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
9436 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
9437 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
9438 		len = FW_VER_STR_LEN;
9439 	} else {
9440 		fw_maj = resp->hwrm_fw_maj_8b;
9441 		fw_min = resp->hwrm_fw_min_8b;
9442 		fw_bld = resp->hwrm_fw_bld_8b;
9443 		fw_rsv = resp->hwrm_fw_rsvd_8b;
9444 		len = BC_HWRM_STR_LEN;
9445 	}
9446 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
9447 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
9448 		 fw_rsv);
9449 
9450 	if (strlen(resp->active_pkg_name)) {
9451 		int fw_ver_len = strlen(bp->fw_ver_str);
9452 
9453 		snprintf(bp->fw_ver_str + fw_ver_len,
9454 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
9455 			 resp->active_pkg_name);
9456 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
9457 	}
9458 
9459 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
9460 	if (!bp->hwrm_cmd_timeout)
9461 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
9462 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
9463 	if (!bp->hwrm_cmd_max_timeout)
9464 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
9465 	else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT)
9466 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n",
9467 			    bp->hwrm_cmd_max_timeout / 1000);
9468 
9469 	if (resp->hwrm_intf_maj_8b >= 1) {
9470 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
9471 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
9472 	}
9473 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
9474 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
9475 
9476 	bp->chip_num = le16_to_cpu(resp->chip_num);
9477 	bp->chip_rev = resp->chip_rev;
9478 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
9479 	    !resp->chip_metal)
9480 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
9481 
9482 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
9483 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
9484 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
9485 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
9486 
9487 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
9488 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
9489 
9490 	if (dev_caps_cfg &
9491 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
9492 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
9493 
9494 	if (dev_caps_cfg &
9495 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
9496 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
9497 
9498 	if (dev_caps_cfg &
9499 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
9500 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
9501 
9502 hwrm_ver_get_exit:
9503 	hwrm_req_drop(bp, req);
9504 	return rc;
9505 }
9506 
9507 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
9508 {
9509 	struct hwrm_fw_set_time_input *req;
9510 	struct tm tm;
9511 	time64_t now = ktime_get_real_seconds();
9512 	int rc;
9513 
9514 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
9515 	    bp->hwrm_spec_code < 0x10400)
9516 		return -EOPNOTSUPP;
9517 
9518 	time64_to_tm(now, 0, &tm);
9519 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
9520 	if (rc)
9521 		return rc;
9522 
9523 	req->year = cpu_to_le16(1900 + tm.tm_year);
9524 	req->month = 1 + tm.tm_mon;
9525 	req->day = tm.tm_mday;
9526 	req->hour = tm.tm_hour;
9527 	req->minute = tm.tm_min;
9528 	req->second = tm.tm_sec;
9529 	return hwrm_req_send(bp, req);
9530 }
9531 
9532 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
9533 {
9534 	u64 sw_tmp;
9535 
9536 	hw &= mask;
9537 	sw_tmp = (*sw & ~mask) | hw;
9538 	if (hw < (*sw & mask))
9539 		sw_tmp += mask + 1;
9540 	WRITE_ONCE(*sw, sw_tmp);
9541 }
9542 
9543 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
9544 				    int count, bool ignore_zero)
9545 {
9546 	int i;
9547 
9548 	for (i = 0; i < count; i++) {
9549 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
9550 
9551 		if (ignore_zero && !hw)
9552 			continue;
9553 
9554 		if (masks[i] == -1ULL)
9555 			sw_stats[i] = hw;
9556 		else
9557 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
9558 	}
9559 }
9560 
9561 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
9562 {
9563 	if (!stats->hw_stats)
9564 		return;
9565 
9566 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
9567 				stats->hw_masks, stats->len / 8, false);
9568 }
9569 
9570 static void bnxt_accumulate_all_stats(struct bnxt *bp)
9571 {
9572 	struct bnxt_stats_mem *ring0_stats;
9573 	bool ignore_zero = false;
9574 	int i;
9575 
9576 	/* Chip bug.  Counter intermittently becomes 0. */
9577 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
9578 		ignore_zero = true;
9579 
9580 	for (i = 0; i < bp->cp_nr_rings; i++) {
9581 		struct bnxt_napi *bnapi = bp->bnapi[i];
9582 		struct bnxt_cp_ring_info *cpr;
9583 		struct bnxt_stats_mem *stats;
9584 
9585 		cpr = &bnapi->cp_ring;
9586 		stats = &cpr->stats;
9587 		if (!i)
9588 			ring0_stats = stats;
9589 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
9590 					ring0_stats->hw_masks,
9591 					ring0_stats->len / 8, ignore_zero);
9592 	}
9593 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
9594 		struct bnxt_stats_mem *stats = &bp->port_stats;
9595 		__le64 *hw_stats = stats->hw_stats;
9596 		u64 *sw_stats = stats->sw_stats;
9597 		u64 *masks = stats->hw_masks;
9598 		int cnt;
9599 
9600 		cnt = sizeof(struct rx_port_stats) / 8;
9601 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
9602 
9603 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9604 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9605 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9606 		cnt = sizeof(struct tx_port_stats) / 8;
9607 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
9608 	}
9609 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
9610 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
9611 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
9612 	}
9613 }
9614 
9615 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
9616 {
9617 	struct hwrm_port_qstats_input *req;
9618 	struct bnxt_pf_info *pf = &bp->pf;
9619 	int rc;
9620 
9621 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
9622 		return 0;
9623 
9624 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
9625 		return -EOPNOTSUPP;
9626 
9627 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
9628 	if (rc)
9629 		return rc;
9630 
9631 	req->flags = flags;
9632 	req->port_id = cpu_to_le16(pf->port_id);
9633 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
9634 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
9635 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
9636 	return hwrm_req_send(bp, req);
9637 }
9638 
9639 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
9640 {
9641 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
9642 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
9643 	struct hwrm_port_qstats_ext_output *resp_qs;
9644 	struct hwrm_port_qstats_ext_input *req_qs;
9645 	struct bnxt_pf_info *pf = &bp->pf;
9646 	u32 tx_stat_size;
9647 	int rc;
9648 
9649 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
9650 		return 0;
9651 
9652 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
9653 		return -EOPNOTSUPP;
9654 
9655 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
9656 	if (rc)
9657 		return rc;
9658 
9659 	req_qs->flags = flags;
9660 	req_qs->port_id = cpu_to_le16(pf->port_id);
9661 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
9662 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
9663 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
9664 		       sizeof(struct tx_port_stats_ext) : 0;
9665 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
9666 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
9667 	resp_qs = hwrm_req_hold(bp, req_qs);
9668 	rc = hwrm_req_send(bp, req_qs);
9669 	if (!rc) {
9670 		bp->fw_rx_stats_ext_size =
9671 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
9672 		if (BNXT_FW_MAJ(bp) < 220 &&
9673 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
9674 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
9675 
9676 		bp->fw_tx_stats_ext_size = tx_stat_size ?
9677 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
9678 	} else {
9679 		bp->fw_rx_stats_ext_size = 0;
9680 		bp->fw_tx_stats_ext_size = 0;
9681 	}
9682 	hwrm_req_drop(bp, req_qs);
9683 
9684 	if (flags)
9685 		return rc;
9686 
9687 	if (bp->fw_tx_stats_ext_size <=
9688 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
9689 		bp->pri2cos_valid = 0;
9690 		return rc;
9691 	}
9692 
9693 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
9694 	if (rc)
9695 		return rc;
9696 
9697 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
9698 
9699 	resp_qc = hwrm_req_hold(bp, req_qc);
9700 	rc = hwrm_req_send(bp, req_qc);
9701 	if (!rc) {
9702 		u8 *pri2cos;
9703 		int i, j;
9704 
9705 		pri2cos = &resp_qc->pri0_cos_queue_id;
9706 		for (i = 0; i < 8; i++) {
9707 			u8 queue_id = pri2cos[i];
9708 			u8 queue_idx;
9709 
9710 			/* Per port queue IDs start from 0, 10, 20, etc */
9711 			queue_idx = queue_id % 10;
9712 			if (queue_idx > BNXT_MAX_QUEUE) {
9713 				bp->pri2cos_valid = false;
9714 				hwrm_req_drop(bp, req_qc);
9715 				return rc;
9716 			}
9717 			for (j = 0; j < bp->max_q; j++) {
9718 				if (bp->q_ids[j] == queue_id)
9719 					bp->pri2cos_idx[i] = queue_idx;
9720 			}
9721 		}
9722 		bp->pri2cos_valid = true;
9723 	}
9724 	hwrm_req_drop(bp, req_qc);
9725 
9726 	return rc;
9727 }
9728 
9729 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
9730 {
9731 	bnxt_hwrm_tunnel_dst_port_free(bp,
9732 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
9733 	bnxt_hwrm_tunnel_dst_port_free(bp,
9734 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
9735 }
9736 
9737 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
9738 {
9739 	int rc, i;
9740 	u32 tpa_flags = 0;
9741 
9742 	if (set_tpa)
9743 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
9744 	else if (BNXT_NO_FW_ACCESS(bp))
9745 		return 0;
9746 	for (i = 0; i < bp->nr_vnics; i++) {
9747 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
9748 		if (rc) {
9749 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
9750 				   i, rc);
9751 			return rc;
9752 		}
9753 	}
9754 	return 0;
9755 }
9756 
9757 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
9758 {
9759 	int i;
9760 
9761 	for (i = 0; i < bp->nr_vnics; i++)
9762 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
9763 }
9764 
9765 static void bnxt_clear_vnic(struct bnxt *bp)
9766 {
9767 	if (!bp->vnic_info)
9768 		return;
9769 
9770 	bnxt_hwrm_clear_vnic_filter(bp);
9771 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
9772 		/* clear all RSS setting before free vnic ctx */
9773 		bnxt_hwrm_clear_vnic_rss(bp);
9774 		bnxt_hwrm_vnic_ctx_free(bp);
9775 	}
9776 	/* before free the vnic, undo the vnic tpa settings */
9777 	if (bp->flags & BNXT_FLAG_TPA)
9778 		bnxt_set_tpa(bp, false);
9779 	bnxt_hwrm_vnic_free(bp);
9780 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
9781 		bnxt_hwrm_vnic_ctx_free(bp);
9782 }
9783 
9784 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
9785 				    bool irq_re_init)
9786 {
9787 	bnxt_clear_vnic(bp);
9788 	bnxt_hwrm_ring_free(bp, close_path);
9789 	bnxt_hwrm_ring_grp_free(bp);
9790 	if (irq_re_init) {
9791 		bnxt_hwrm_stat_ctx_free(bp);
9792 		bnxt_hwrm_free_tunnel_ports(bp);
9793 	}
9794 }
9795 
9796 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
9797 {
9798 	struct hwrm_func_cfg_input *req;
9799 	u8 evb_mode;
9800 	int rc;
9801 
9802 	if (br_mode == BRIDGE_MODE_VEB)
9803 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
9804 	else if (br_mode == BRIDGE_MODE_VEPA)
9805 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
9806 	else
9807 		return -EINVAL;
9808 
9809 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
9810 	if (rc)
9811 		return rc;
9812 
9813 	req->fid = cpu_to_le16(0xffff);
9814 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
9815 	req->evb_mode = evb_mode;
9816 	return hwrm_req_send(bp, req);
9817 }
9818 
9819 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
9820 {
9821 	struct hwrm_func_cfg_input *req;
9822 	int rc;
9823 
9824 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
9825 		return 0;
9826 
9827 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
9828 	if (rc)
9829 		return rc;
9830 
9831 	req->fid = cpu_to_le16(0xffff);
9832 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
9833 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
9834 	if (size == 128)
9835 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
9836 
9837 	return hwrm_req_send(bp, req);
9838 }
9839 
9840 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
9841 {
9842 	int rc;
9843 
9844 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
9845 		goto skip_rss_ctx;
9846 
9847 	/* allocate context for vnic */
9848 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
9849 	if (rc) {
9850 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
9851 			   vnic->vnic_id, rc);
9852 		goto vnic_setup_err;
9853 	}
9854 	bp->rsscos_nr_ctxs++;
9855 
9856 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
9857 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
9858 		if (rc) {
9859 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
9860 				   vnic->vnic_id, rc);
9861 			goto vnic_setup_err;
9862 		}
9863 		bp->rsscos_nr_ctxs++;
9864 	}
9865 
9866 skip_rss_ctx:
9867 	/* configure default vnic, ring grp */
9868 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
9869 	if (rc) {
9870 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
9871 			   vnic->vnic_id, rc);
9872 		goto vnic_setup_err;
9873 	}
9874 
9875 	/* Enable RSS hashing on vnic */
9876 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
9877 	if (rc) {
9878 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
9879 			   vnic->vnic_id, rc);
9880 		goto vnic_setup_err;
9881 	}
9882 
9883 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
9884 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
9885 		if (rc) {
9886 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
9887 				   vnic->vnic_id, rc);
9888 		}
9889 	}
9890 
9891 vnic_setup_err:
9892 	return rc;
9893 }
9894 
9895 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
9896 {
9897 	int rc;
9898 
9899 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
9900 	if (rc) {
9901 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
9902 			   vnic->vnic_id, rc);
9903 		return rc;
9904 	}
9905 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
9906 	if (rc)
9907 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
9908 			   vnic->vnic_id, rc);
9909 	return rc;
9910 }
9911 
9912 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
9913 {
9914 	int rc, i, nr_ctxs;
9915 
9916 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
9917 	for (i = 0; i < nr_ctxs; i++) {
9918 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
9919 		if (rc) {
9920 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
9921 				   vnic->vnic_id, i, rc);
9922 			break;
9923 		}
9924 		bp->rsscos_nr_ctxs++;
9925 	}
9926 	if (i < nr_ctxs)
9927 		return -ENOMEM;
9928 
9929 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
9930 	if (rc)
9931 		return rc;
9932 
9933 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
9934 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
9935 		if (rc) {
9936 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
9937 				   vnic->vnic_id, rc);
9938 		}
9939 	}
9940 	return rc;
9941 }
9942 
9943 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
9944 {
9945 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
9946 		return __bnxt_setup_vnic_p5(bp, vnic);
9947 	else
9948 		return __bnxt_setup_vnic(bp, vnic);
9949 }
9950 
9951 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
9952 				     struct bnxt_vnic_info *vnic,
9953 				     u16 start_rx_ring_idx, int rx_rings)
9954 {
9955 	int rc;
9956 
9957 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
9958 	if (rc) {
9959 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
9960 			   vnic->vnic_id, rc);
9961 		return rc;
9962 	}
9963 	return bnxt_setup_vnic(bp, vnic);
9964 }
9965 
9966 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
9967 {
9968 	struct bnxt_vnic_info *vnic;
9969 	int i, rc = 0;
9970 
9971 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
9972 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
9973 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
9974 	}
9975 
9976 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
9977 		return 0;
9978 
9979 	for (i = 0; i < bp->rx_nr_rings; i++) {
9980 		u16 vnic_id = i + 1;
9981 		u16 ring_id = i;
9982 
9983 		if (vnic_id >= bp->nr_vnics)
9984 			break;
9985 
9986 		vnic = &bp->vnic_info[vnic_id];
9987 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
9988 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
9989 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
9990 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
9991 			break;
9992 	}
9993 	return rc;
9994 }
9995 
9996 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
9997 			  bool all)
9998 {
9999 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10000 	struct bnxt_filter_base *usr_fltr, *tmp;
10001 	struct bnxt_ntuple_filter *ntp_fltr;
10002 	int i;
10003 
10004 	bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10005 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10006 		if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10007 			bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10008 	}
10009 	if (!all)
10010 		return;
10011 
10012 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10013 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10014 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10015 			ntp_fltr = container_of(usr_fltr,
10016 						struct bnxt_ntuple_filter,
10017 						base);
10018 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10019 			bnxt_del_ntp_filter(bp, ntp_fltr);
10020 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10021 		}
10022 	}
10023 
10024 	if (vnic->rss_table)
10025 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10026 				  vnic->rss_table,
10027 				  vnic->rss_table_dma_addr);
10028 	kfree(rss_ctx->rss_indir_tbl);
10029 	list_del(&rss_ctx->list);
10030 	bp->num_rss_ctx--;
10031 	clear_bit(rss_ctx->index, bp->rss_ctx_bmap);
10032 	kfree(rss_ctx);
10033 }
10034 
10035 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10036 {
10037 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10038 	struct bnxt_rss_ctx *rss_ctx, *tmp;
10039 
10040 	list_for_each_entry_safe(rss_ctx, tmp, &bp->rss_ctx_list, list) {
10041 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10042 
10043 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10044 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10045 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10046 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10047 				   rss_ctx->index);
10048 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10049 		}
10050 	}
10051 }
10052 
10053 struct bnxt_rss_ctx *bnxt_alloc_rss_ctx(struct bnxt *bp)
10054 {
10055 	struct bnxt_rss_ctx *rss_ctx = NULL;
10056 
10057 	rss_ctx = kzalloc(sizeof(*rss_ctx), GFP_KERNEL);
10058 	if (rss_ctx) {
10059 		rss_ctx->vnic.rss_ctx = rss_ctx;
10060 		list_add_tail(&rss_ctx->list, &bp->rss_ctx_list);
10061 		bp->num_rss_ctx++;
10062 	}
10063 	return rss_ctx;
10064 }
10065 
10066 void bnxt_clear_rss_ctxs(struct bnxt *bp, bool all)
10067 {
10068 	struct bnxt_rss_ctx *rss_ctx, *tmp;
10069 
10070 	list_for_each_entry_safe(rss_ctx, tmp, &bp->rss_ctx_list, list)
10071 		bnxt_del_one_rss_ctx(bp, rss_ctx, all);
10072 
10073 	if (all)
10074 		bitmap_free(bp->rss_ctx_bmap);
10075 }
10076 
10077 static void bnxt_init_multi_rss_ctx(struct bnxt *bp)
10078 {
10079 	bp->rss_ctx_bmap = bitmap_zalloc(BNXT_RSS_CTX_BMAP_LEN, GFP_KERNEL);
10080 	if (bp->rss_ctx_bmap) {
10081 		/* burn index 0 since we cannot have context 0 */
10082 		__set_bit(0, bp->rss_ctx_bmap);
10083 		INIT_LIST_HEAD(&bp->rss_ctx_list);
10084 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
10085 	}
10086 }
10087 
10088 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
10089 static bool bnxt_promisc_ok(struct bnxt *bp)
10090 {
10091 #ifdef CONFIG_BNXT_SRIOV
10092 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10093 		return false;
10094 #endif
10095 	return true;
10096 }
10097 
10098 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10099 {
10100 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10101 	unsigned int rc = 0;
10102 
10103 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10104 	if (rc) {
10105 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10106 			   rc);
10107 		return rc;
10108 	}
10109 
10110 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10111 	if (rc) {
10112 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10113 			   rc);
10114 		return rc;
10115 	}
10116 	return rc;
10117 }
10118 
10119 static int bnxt_cfg_rx_mode(struct bnxt *);
10120 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10121 
10122 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10123 {
10124 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10125 	int rc = 0;
10126 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10127 
10128 	if (irq_re_init) {
10129 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10130 		if (rc) {
10131 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10132 				   rc);
10133 			goto err_out;
10134 		}
10135 	}
10136 
10137 	rc = bnxt_hwrm_ring_alloc(bp);
10138 	if (rc) {
10139 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10140 		goto err_out;
10141 	}
10142 
10143 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10144 	if (rc) {
10145 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10146 		goto err_out;
10147 	}
10148 
10149 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10150 		rx_nr_rings--;
10151 
10152 	/* default vnic 0 */
10153 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10154 	if (rc) {
10155 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10156 		goto err_out;
10157 	}
10158 
10159 	if (BNXT_VF(bp))
10160 		bnxt_hwrm_func_qcfg(bp);
10161 
10162 	rc = bnxt_setup_vnic(bp, vnic);
10163 	if (rc)
10164 		goto err_out;
10165 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10166 		bnxt_hwrm_update_rss_hash_cfg(bp);
10167 
10168 	if (bp->flags & BNXT_FLAG_RFS) {
10169 		rc = bnxt_alloc_rfs_vnics(bp);
10170 		if (rc)
10171 			goto err_out;
10172 	}
10173 
10174 	if (bp->flags & BNXT_FLAG_TPA) {
10175 		rc = bnxt_set_tpa(bp, true);
10176 		if (rc)
10177 			goto err_out;
10178 	}
10179 
10180 	if (BNXT_VF(bp))
10181 		bnxt_update_vf_mac(bp);
10182 
10183 	/* Filter for default vnic 0 */
10184 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
10185 	if (rc) {
10186 		if (BNXT_VF(bp) && rc == -ENODEV)
10187 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
10188 		else
10189 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
10190 		goto err_out;
10191 	}
10192 	vnic->uc_filter_count = 1;
10193 
10194 	vnic->rx_mask = 0;
10195 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
10196 		goto skip_rx_mask;
10197 
10198 	if (bp->dev->flags & IFF_BROADCAST)
10199 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10200 
10201 	if (bp->dev->flags & IFF_PROMISC)
10202 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10203 
10204 	if (bp->dev->flags & IFF_ALLMULTI) {
10205 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10206 		vnic->mc_list_count = 0;
10207 	} else if (bp->dev->flags & IFF_MULTICAST) {
10208 		u32 mask = 0;
10209 
10210 		bnxt_mc_list_updated(bp, &mask);
10211 		vnic->rx_mask |= mask;
10212 	}
10213 
10214 	rc = bnxt_cfg_rx_mode(bp);
10215 	if (rc)
10216 		goto err_out;
10217 
10218 skip_rx_mask:
10219 	rc = bnxt_hwrm_set_coal(bp);
10220 	if (rc)
10221 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
10222 				rc);
10223 
10224 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10225 		rc = bnxt_setup_nitroa0_vnic(bp);
10226 		if (rc)
10227 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
10228 				   rc);
10229 	}
10230 
10231 	if (BNXT_VF(bp)) {
10232 		bnxt_hwrm_func_qcfg(bp);
10233 		netdev_update_features(bp->dev);
10234 	}
10235 
10236 	return 0;
10237 
10238 err_out:
10239 	bnxt_hwrm_resource_free(bp, 0, true);
10240 
10241 	return rc;
10242 }
10243 
10244 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
10245 {
10246 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
10247 	return 0;
10248 }
10249 
10250 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
10251 {
10252 	bnxt_init_cp_rings(bp);
10253 	bnxt_init_rx_rings(bp);
10254 	bnxt_init_tx_rings(bp);
10255 	bnxt_init_ring_grps(bp, irq_re_init);
10256 	bnxt_init_vnics(bp);
10257 
10258 	return bnxt_init_chip(bp, irq_re_init);
10259 }
10260 
10261 static int bnxt_set_real_num_queues(struct bnxt *bp)
10262 {
10263 	int rc;
10264 	struct net_device *dev = bp->dev;
10265 
10266 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
10267 					  bp->tx_nr_rings_xdp);
10268 	if (rc)
10269 		return rc;
10270 
10271 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
10272 	if (rc)
10273 		return rc;
10274 
10275 #ifdef CONFIG_RFS_ACCEL
10276 	if (bp->flags & BNXT_FLAG_RFS)
10277 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
10278 #endif
10279 
10280 	return rc;
10281 }
10282 
10283 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10284 			     bool shared)
10285 {
10286 	int _rx = *rx, _tx = *tx;
10287 
10288 	if (shared) {
10289 		*rx = min_t(int, _rx, max);
10290 		*tx = min_t(int, _tx, max);
10291 	} else {
10292 		if (max < 2)
10293 			return -ENOMEM;
10294 
10295 		while (_rx + _tx > max) {
10296 			if (_rx > _tx && _rx > 1)
10297 				_rx--;
10298 			else if (_tx > 1)
10299 				_tx--;
10300 		}
10301 		*rx = _rx;
10302 		*tx = _tx;
10303 	}
10304 	return 0;
10305 }
10306 
10307 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
10308 {
10309 	return (tx - tx_xdp) / tx_sets + tx_xdp;
10310 }
10311 
10312 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
10313 {
10314 	int tcs = bp->num_tc;
10315 
10316 	if (!tcs)
10317 		tcs = 1;
10318 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
10319 }
10320 
10321 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
10322 {
10323 	int tcs = bp->num_tc;
10324 
10325 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
10326 	       bp->tx_nr_rings_xdp;
10327 }
10328 
10329 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10330 			   bool sh)
10331 {
10332 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
10333 
10334 	if (tx_cp != *tx) {
10335 		int tx_saved = tx_cp, rc;
10336 
10337 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
10338 		if (rc)
10339 			return rc;
10340 		if (tx_cp != tx_saved)
10341 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
10342 		return 0;
10343 	}
10344 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
10345 }
10346 
10347 static void bnxt_setup_msix(struct bnxt *bp)
10348 {
10349 	const int len = sizeof(bp->irq_tbl[0].name);
10350 	struct net_device *dev = bp->dev;
10351 	int tcs, i;
10352 
10353 	tcs = bp->num_tc;
10354 	if (tcs) {
10355 		int i, off, count;
10356 
10357 		for (i = 0; i < tcs; i++) {
10358 			count = bp->tx_nr_rings_per_tc;
10359 			off = BNXT_TC_TO_RING_BASE(bp, i);
10360 			netdev_set_tc_queue(dev, i, count, off);
10361 		}
10362 	}
10363 
10364 	for (i = 0; i < bp->cp_nr_rings; i++) {
10365 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10366 		char *attr;
10367 
10368 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
10369 			attr = "TxRx";
10370 		else if (i < bp->rx_nr_rings)
10371 			attr = "rx";
10372 		else
10373 			attr = "tx";
10374 
10375 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
10376 			 attr, i);
10377 		bp->irq_tbl[map_idx].handler = bnxt_msix;
10378 	}
10379 }
10380 
10381 static void bnxt_setup_inta(struct bnxt *bp)
10382 {
10383 	const int len = sizeof(bp->irq_tbl[0].name);
10384 
10385 	if (bp->num_tc) {
10386 		netdev_reset_tc(bp->dev);
10387 		bp->num_tc = 0;
10388 	}
10389 
10390 	snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
10391 		 0);
10392 	bp->irq_tbl[0].handler = bnxt_inta;
10393 }
10394 
10395 static int bnxt_init_int_mode(struct bnxt *bp);
10396 
10397 static int bnxt_setup_int_mode(struct bnxt *bp)
10398 {
10399 	int rc;
10400 
10401 	if (!bp->irq_tbl) {
10402 		rc = bnxt_init_int_mode(bp);
10403 		if (rc || !bp->irq_tbl)
10404 			return rc ?: -ENODEV;
10405 	}
10406 
10407 	if (bp->flags & BNXT_FLAG_USING_MSIX)
10408 		bnxt_setup_msix(bp);
10409 	else
10410 		bnxt_setup_inta(bp);
10411 
10412 	rc = bnxt_set_real_num_queues(bp);
10413 	return rc;
10414 }
10415 
10416 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
10417 {
10418 	return bp->hw_resc.max_rsscos_ctxs;
10419 }
10420 
10421 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
10422 {
10423 	return bp->hw_resc.max_vnics;
10424 }
10425 
10426 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
10427 {
10428 	return bp->hw_resc.max_stat_ctxs;
10429 }
10430 
10431 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
10432 {
10433 	return bp->hw_resc.max_cp_rings;
10434 }
10435 
10436 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
10437 {
10438 	unsigned int cp = bp->hw_resc.max_cp_rings;
10439 
10440 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
10441 		cp -= bnxt_get_ulp_msix_num(bp);
10442 
10443 	return cp;
10444 }
10445 
10446 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
10447 {
10448 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
10449 
10450 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10451 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
10452 
10453 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
10454 }
10455 
10456 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
10457 {
10458 	bp->hw_resc.max_irqs = max_irqs;
10459 }
10460 
10461 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
10462 {
10463 	unsigned int cp;
10464 
10465 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
10466 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10467 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
10468 	else
10469 		return cp - bp->cp_nr_rings;
10470 }
10471 
10472 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
10473 {
10474 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
10475 }
10476 
10477 int bnxt_get_avail_msix(struct bnxt *bp, int num)
10478 {
10479 	int max_cp = bnxt_get_max_func_cp_rings(bp);
10480 	int max_irq = bnxt_get_max_func_irqs(bp);
10481 	int total_req = bp->cp_nr_rings + num;
10482 	int max_idx, avail_msix;
10483 
10484 	max_idx = bp->total_irqs;
10485 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
10486 		max_idx = min_t(int, bp->total_irqs, max_cp);
10487 	avail_msix = max_idx - bp->cp_nr_rings;
10488 	if (!BNXT_NEW_RM(bp) || avail_msix >= num)
10489 		return avail_msix;
10490 
10491 	if (max_irq < total_req) {
10492 		num = max_irq - bp->cp_nr_rings;
10493 		if (num <= 0)
10494 			return 0;
10495 	}
10496 	return num;
10497 }
10498 
10499 static int bnxt_get_num_msix(struct bnxt *bp)
10500 {
10501 	if (!BNXT_NEW_RM(bp))
10502 		return bnxt_get_max_func_irqs(bp);
10503 
10504 	return bnxt_nq_rings_in_use(bp);
10505 }
10506 
10507 static int bnxt_init_msix(struct bnxt *bp)
10508 {
10509 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp;
10510 	struct msix_entry *msix_ent;
10511 
10512 	total_vecs = bnxt_get_num_msix(bp);
10513 	max = bnxt_get_max_func_irqs(bp);
10514 	if (total_vecs > max)
10515 		total_vecs = max;
10516 
10517 	if (!total_vecs)
10518 		return 0;
10519 
10520 	msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
10521 	if (!msix_ent)
10522 		return -ENOMEM;
10523 
10524 	for (i = 0; i < total_vecs; i++) {
10525 		msix_ent[i].entry = i;
10526 		msix_ent[i].vector = 0;
10527 	}
10528 
10529 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
10530 		min = 2;
10531 
10532 	total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
10533 	ulp_msix = bnxt_get_ulp_msix_num(bp);
10534 	if (total_vecs < 0 || total_vecs < ulp_msix) {
10535 		rc = -ENODEV;
10536 		goto msix_setup_exit;
10537 	}
10538 
10539 	bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
10540 	if (bp->irq_tbl) {
10541 		for (i = 0; i < total_vecs; i++)
10542 			bp->irq_tbl[i].vector = msix_ent[i].vector;
10543 
10544 		bp->total_irqs = total_vecs;
10545 		/* Trim rings based upon num of vectors allocated */
10546 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
10547 				     total_vecs - ulp_msix, min == 1);
10548 		if (rc)
10549 			goto msix_setup_exit;
10550 
10551 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
10552 		bp->cp_nr_rings = (min == 1) ?
10553 				  max_t(int, tx_cp, bp->rx_nr_rings) :
10554 				  tx_cp + bp->rx_nr_rings;
10555 
10556 	} else {
10557 		rc = -ENOMEM;
10558 		goto msix_setup_exit;
10559 	}
10560 	bp->flags |= BNXT_FLAG_USING_MSIX;
10561 	kfree(msix_ent);
10562 	return 0;
10563 
10564 msix_setup_exit:
10565 	netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
10566 	kfree(bp->irq_tbl);
10567 	bp->irq_tbl = NULL;
10568 	pci_disable_msix(bp->pdev);
10569 	kfree(msix_ent);
10570 	return rc;
10571 }
10572 
10573 static int bnxt_init_inta(struct bnxt *bp)
10574 {
10575 	bp->irq_tbl = kzalloc(sizeof(struct bnxt_irq), GFP_KERNEL);
10576 	if (!bp->irq_tbl)
10577 		return -ENOMEM;
10578 
10579 	bp->total_irqs = 1;
10580 	bp->rx_nr_rings = 1;
10581 	bp->tx_nr_rings = 1;
10582 	bp->cp_nr_rings = 1;
10583 	bp->flags |= BNXT_FLAG_SHARED_RINGS;
10584 	bp->irq_tbl[0].vector = bp->pdev->irq;
10585 	return 0;
10586 }
10587 
10588 static int bnxt_init_int_mode(struct bnxt *bp)
10589 {
10590 	int rc = -ENODEV;
10591 
10592 	if (bp->flags & BNXT_FLAG_MSIX_CAP)
10593 		rc = bnxt_init_msix(bp);
10594 
10595 	if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
10596 		/* fallback to INTA */
10597 		rc = bnxt_init_inta(bp);
10598 	}
10599 	return rc;
10600 }
10601 
10602 static void bnxt_clear_int_mode(struct bnxt *bp)
10603 {
10604 	if (bp->flags & BNXT_FLAG_USING_MSIX)
10605 		pci_disable_msix(bp->pdev);
10606 
10607 	kfree(bp->irq_tbl);
10608 	bp->irq_tbl = NULL;
10609 	bp->flags &= ~BNXT_FLAG_USING_MSIX;
10610 }
10611 
10612 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
10613 {
10614 	bool irq_cleared = false;
10615 	int tcs = bp->num_tc;
10616 	int irqs_required;
10617 	int rc;
10618 
10619 	if (!bnxt_need_reserve_rings(bp))
10620 		return 0;
10621 
10622 	if (!bnxt_ulp_registered(bp->edev)) {
10623 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
10624 
10625 		if (ulp_msix > bp->ulp_num_msix_want)
10626 			ulp_msix = bp->ulp_num_msix_want;
10627 		irqs_required = ulp_msix + bp->cp_nr_rings;
10628 	} else {
10629 		irqs_required = bnxt_get_num_msix(bp);
10630 	}
10631 
10632 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
10633 		bnxt_ulp_irq_stop(bp);
10634 		bnxt_clear_int_mode(bp);
10635 		irq_cleared = true;
10636 	}
10637 	rc = __bnxt_reserve_rings(bp);
10638 	if (irq_cleared) {
10639 		if (!rc)
10640 			rc = bnxt_init_int_mode(bp);
10641 		bnxt_ulp_irq_restart(bp, rc);
10642 	}
10643 	if (rc) {
10644 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
10645 		return rc;
10646 	}
10647 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
10648 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
10649 		netdev_err(bp->dev, "tx ring reservation failure\n");
10650 		netdev_reset_tc(bp->dev);
10651 		bp->num_tc = 0;
10652 		if (bp->tx_nr_rings_xdp)
10653 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
10654 		else
10655 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
10656 		return -ENOMEM;
10657 	}
10658 	return 0;
10659 }
10660 
10661 static void bnxt_free_irq(struct bnxt *bp)
10662 {
10663 	struct bnxt_irq *irq;
10664 	int i;
10665 
10666 #ifdef CONFIG_RFS_ACCEL
10667 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
10668 	bp->dev->rx_cpu_rmap = NULL;
10669 #endif
10670 	if (!bp->irq_tbl || !bp->bnapi)
10671 		return;
10672 
10673 	for (i = 0; i < bp->cp_nr_rings; i++) {
10674 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10675 
10676 		irq = &bp->irq_tbl[map_idx];
10677 		if (irq->requested) {
10678 			if (irq->have_cpumask) {
10679 				irq_set_affinity_hint(irq->vector, NULL);
10680 				free_cpumask_var(irq->cpu_mask);
10681 				irq->have_cpumask = 0;
10682 			}
10683 			free_irq(irq->vector, bp->bnapi[i]);
10684 		}
10685 
10686 		irq->requested = 0;
10687 	}
10688 }
10689 
10690 static int bnxt_request_irq(struct bnxt *bp)
10691 {
10692 	int i, j, rc = 0;
10693 	unsigned long flags = 0;
10694 #ifdef CONFIG_RFS_ACCEL
10695 	struct cpu_rmap *rmap;
10696 #endif
10697 
10698 	rc = bnxt_setup_int_mode(bp);
10699 	if (rc) {
10700 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
10701 			   rc);
10702 		return rc;
10703 	}
10704 #ifdef CONFIG_RFS_ACCEL
10705 	rmap = bp->dev->rx_cpu_rmap;
10706 #endif
10707 	if (!(bp->flags & BNXT_FLAG_USING_MSIX))
10708 		flags = IRQF_SHARED;
10709 
10710 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
10711 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10712 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
10713 
10714 #ifdef CONFIG_RFS_ACCEL
10715 		if (rmap && bp->bnapi[i]->rx_ring) {
10716 			rc = irq_cpu_rmap_add(rmap, irq->vector);
10717 			if (rc)
10718 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
10719 					    j);
10720 			j++;
10721 		}
10722 #endif
10723 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
10724 				 bp->bnapi[i]);
10725 		if (rc)
10726 			break;
10727 
10728 		netif_napi_set_irq(&bp->bnapi[i]->napi, irq->vector);
10729 		irq->requested = 1;
10730 
10731 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
10732 			int numa_node = dev_to_node(&bp->pdev->dev);
10733 
10734 			irq->have_cpumask = 1;
10735 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
10736 					irq->cpu_mask);
10737 			rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
10738 			if (rc) {
10739 				netdev_warn(bp->dev,
10740 					    "Set affinity failed, IRQ = %d\n",
10741 					    irq->vector);
10742 				break;
10743 			}
10744 		}
10745 	}
10746 	return rc;
10747 }
10748 
10749 static void bnxt_del_napi(struct bnxt *bp)
10750 {
10751 	int i;
10752 
10753 	if (!bp->bnapi)
10754 		return;
10755 
10756 	for (i = 0; i < bp->rx_nr_rings; i++)
10757 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
10758 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
10759 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
10760 
10761 	for (i = 0; i < bp->cp_nr_rings; i++) {
10762 		struct bnxt_napi *bnapi = bp->bnapi[i];
10763 
10764 		__netif_napi_del(&bnapi->napi);
10765 	}
10766 	/* We called __netif_napi_del(), we need
10767 	 * to respect an RCU grace period before freeing napi structures.
10768 	 */
10769 	synchronize_net();
10770 }
10771 
10772 static void bnxt_init_napi(struct bnxt *bp)
10773 {
10774 	int i;
10775 	unsigned int cp_nr_rings = bp->cp_nr_rings;
10776 	struct bnxt_napi *bnapi;
10777 
10778 	if (bp->flags & BNXT_FLAG_USING_MSIX) {
10779 		int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
10780 
10781 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10782 			poll_fn = bnxt_poll_p5;
10783 		else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10784 			cp_nr_rings--;
10785 		for (i = 0; i < cp_nr_rings; i++) {
10786 			bnapi = bp->bnapi[i];
10787 			netif_napi_add(bp->dev, &bnapi->napi, poll_fn);
10788 		}
10789 		if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10790 			bnapi = bp->bnapi[cp_nr_rings];
10791 			netif_napi_add(bp->dev, &bnapi->napi,
10792 				       bnxt_poll_nitroa0);
10793 		}
10794 	} else {
10795 		bnapi = bp->bnapi[0];
10796 		netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll);
10797 	}
10798 }
10799 
10800 static void bnxt_disable_napi(struct bnxt *bp)
10801 {
10802 	int i;
10803 
10804 	if (!bp->bnapi ||
10805 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
10806 		return;
10807 
10808 	for (i = 0; i < bp->cp_nr_rings; i++) {
10809 		struct bnxt_napi *bnapi = bp->bnapi[i];
10810 		struct bnxt_cp_ring_info *cpr;
10811 
10812 		cpr = &bnapi->cp_ring;
10813 		if (bnapi->tx_fault)
10814 			cpr->sw_stats.tx.tx_resets++;
10815 		if (bnapi->in_reset)
10816 			cpr->sw_stats.rx.rx_resets++;
10817 		napi_disable(&bnapi->napi);
10818 		if (bnapi->rx_ring)
10819 			cancel_work_sync(&cpr->dim.work);
10820 	}
10821 }
10822 
10823 static void bnxt_enable_napi(struct bnxt *bp)
10824 {
10825 	int i;
10826 
10827 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
10828 	for (i = 0; i < bp->cp_nr_rings; i++) {
10829 		struct bnxt_napi *bnapi = bp->bnapi[i];
10830 		struct bnxt_cp_ring_info *cpr;
10831 
10832 		bnapi->tx_fault = 0;
10833 
10834 		cpr = &bnapi->cp_ring;
10835 		bnapi->in_reset = false;
10836 
10837 		if (bnapi->rx_ring) {
10838 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
10839 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
10840 		}
10841 		napi_enable(&bnapi->napi);
10842 	}
10843 }
10844 
10845 void bnxt_tx_disable(struct bnxt *bp)
10846 {
10847 	int i;
10848 	struct bnxt_tx_ring_info *txr;
10849 
10850 	if (bp->tx_ring) {
10851 		for (i = 0; i < bp->tx_nr_rings; i++) {
10852 			txr = &bp->tx_ring[i];
10853 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
10854 		}
10855 	}
10856 	/* Make sure napi polls see @dev_state change */
10857 	synchronize_net();
10858 	/* Drop carrier first to prevent TX timeout */
10859 	netif_carrier_off(bp->dev);
10860 	/* Stop all TX queues */
10861 	netif_tx_disable(bp->dev);
10862 }
10863 
10864 void bnxt_tx_enable(struct bnxt *bp)
10865 {
10866 	int i;
10867 	struct bnxt_tx_ring_info *txr;
10868 
10869 	for (i = 0; i < bp->tx_nr_rings; i++) {
10870 		txr = &bp->tx_ring[i];
10871 		WRITE_ONCE(txr->dev_state, 0);
10872 	}
10873 	/* Make sure napi polls see @dev_state change */
10874 	synchronize_net();
10875 	netif_tx_wake_all_queues(bp->dev);
10876 	if (BNXT_LINK_IS_UP(bp))
10877 		netif_carrier_on(bp->dev);
10878 }
10879 
10880 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
10881 {
10882 	u8 active_fec = link_info->active_fec_sig_mode &
10883 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
10884 
10885 	switch (active_fec) {
10886 	default:
10887 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
10888 		return "None";
10889 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
10890 		return "Clause 74 BaseR";
10891 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
10892 		return "Clause 91 RS(528,514)";
10893 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
10894 		return "Clause 91 RS544_1XN";
10895 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
10896 		return "Clause 91 RS(544,514)";
10897 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
10898 		return "Clause 91 RS272_1XN";
10899 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
10900 		return "Clause 91 RS(272,257)";
10901 	}
10902 }
10903 
10904 void bnxt_report_link(struct bnxt *bp)
10905 {
10906 	if (BNXT_LINK_IS_UP(bp)) {
10907 		const char *signal = "";
10908 		const char *flow_ctrl;
10909 		const char *duplex;
10910 		u32 speed;
10911 		u16 fec;
10912 
10913 		netif_carrier_on(bp->dev);
10914 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
10915 		if (speed == SPEED_UNKNOWN) {
10916 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
10917 			return;
10918 		}
10919 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
10920 			duplex = "full";
10921 		else
10922 			duplex = "half";
10923 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
10924 			flow_ctrl = "ON - receive & transmit";
10925 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
10926 			flow_ctrl = "ON - transmit";
10927 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
10928 			flow_ctrl = "ON - receive";
10929 		else
10930 			flow_ctrl = "none";
10931 		if (bp->link_info.phy_qcfg_resp.option_flags &
10932 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
10933 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
10934 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
10935 			switch (sig_mode) {
10936 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
10937 				signal = "(NRZ) ";
10938 				break;
10939 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
10940 				signal = "(PAM4 56Gbps) ";
10941 				break;
10942 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
10943 				signal = "(PAM4 112Gbps) ";
10944 				break;
10945 			default:
10946 				break;
10947 			}
10948 		}
10949 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
10950 			    speed, signal, duplex, flow_ctrl);
10951 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
10952 			netdev_info(bp->dev, "EEE is %s\n",
10953 				    bp->eee.eee_active ? "active" :
10954 							 "not active");
10955 		fec = bp->link_info.fec_cfg;
10956 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
10957 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
10958 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
10959 				    bnxt_report_fec(&bp->link_info));
10960 	} else {
10961 		netif_carrier_off(bp->dev);
10962 		netdev_err(bp->dev, "NIC Link is Down\n");
10963 	}
10964 }
10965 
10966 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
10967 {
10968 	if (!resp->supported_speeds_auto_mode &&
10969 	    !resp->supported_speeds_force_mode &&
10970 	    !resp->supported_pam4_speeds_auto_mode &&
10971 	    !resp->supported_pam4_speeds_force_mode &&
10972 	    !resp->supported_speeds2_auto_mode &&
10973 	    !resp->supported_speeds2_force_mode)
10974 		return true;
10975 	return false;
10976 }
10977 
10978 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
10979 {
10980 	struct bnxt_link_info *link_info = &bp->link_info;
10981 	struct hwrm_port_phy_qcaps_output *resp;
10982 	struct hwrm_port_phy_qcaps_input *req;
10983 	int rc = 0;
10984 
10985 	if (bp->hwrm_spec_code < 0x10201)
10986 		return 0;
10987 
10988 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
10989 	if (rc)
10990 		return rc;
10991 
10992 	resp = hwrm_req_hold(bp, req);
10993 	rc = hwrm_req_send(bp, req);
10994 	if (rc)
10995 		goto hwrm_phy_qcaps_exit;
10996 
10997 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
10998 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
10999 		struct ethtool_keee *eee = &bp->eee;
11000 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
11001 
11002 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
11003 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
11004 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11005 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11006 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11007 	}
11008 
11009 	if (bp->hwrm_spec_code >= 0x10a01) {
11010 		if (bnxt_phy_qcaps_no_speed(resp)) {
11011 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11012 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11013 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11014 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11015 			netdev_info(bp->dev, "Ethernet link enabled\n");
11016 			/* Phy re-enabled, reprobe the speeds */
11017 			link_info->support_auto_speeds = 0;
11018 			link_info->support_pam4_auto_speeds = 0;
11019 			link_info->support_auto_speeds2 = 0;
11020 		}
11021 	}
11022 	if (resp->supported_speeds_auto_mode)
11023 		link_info->support_auto_speeds =
11024 			le16_to_cpu(resp->supported_speeds_auto_mode);
11025 	if (resp->supported_pam4_speeds_auto_mode)
11026 		link_info->support_pam4_auto_speeds =
11027 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11028 	if (resp->supported_speeds2_auto_mode)
11029 		link_info->support_auto_speeds2 =
11030 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11031 
11032 	bp->port_count = resp->port_cnt;
11033 
11034 hwrm_phy_qcaps_exit:
11035 	hwrm_req_drop(bp, req);
11036 	return rc;
11037 }
11038 
11039 static bool bnxt_support_dropped(u16 advertising, u16 supported)
11040 {
11041 	u16 diff = advertising ^ supported;
11042 
11043 	return ((supported | diff) != supported);
11044 }
11045 
11046 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
11047 {
11048 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
11049 
11050 	/* Check if any advertised speeds are no longer supported. The caller
11051 	 * holds the link_lock mutex, so we can modify link_info settings.
11052 	 */
11053 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11054 		if (bnxt_support_dropped(link_info->advertising,
11055 					 link_info->support_auto_speeds2)) {
11056 			link_info->advertising = link_info->support_auto_speeds2;
11057 			return true;
11058 		}
11059 		return false;
11060 	}
11061 	if (bnxt_support_dropped(link_info->advertising,
11062 				 link_info->support_auto_speeds)) {
11063 		link_info->advertising = link_info->support_auto_speeds;
11064 		return true;
11065 	}
11066 	if (bnxt_support_dropped(link_info->advertising_pam4,
11067 				 link_info->support_pam4_auto_speeds)) {
11068 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
11069 		return true;
11070 	}
11071 	return false;
11072 }
11073 
11074 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
11075 {
11076 	struct bnxt_link_info *link_info = &bp->link_info;
11077 	struct hwrm_port_phy_qcfg_output *resp;
11078 	struct hwrm_port_phy_qcfg_input *req;
11079 	u8 link_state = link_info->link_state;
11080 	bool support_changed;
11081 	int rc;
11082 
11083 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
11084 	if (rc)
11085 		return rc;
11086 
11087 	resp = hwrm_req_hold(bp, req);
11088 	rc = hwrm_req_send(bp, req);
11089 	if (rc) {
11090 		hwrm_req_drop(bp, req);
11091 		if (BNXT_VF(bp) && rc == -ENODEV) {
11092 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
11093 			rc = 0;
11094 		}
11095 		return rc;
11096 	}
11097 
11098 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
11099 	link_info->phy_link_status = resp->link;
11100 	link_info->duplex = resp->duplex_cfg;
11101 	if (bp->hwrm_spec_code >= 0x10800)
11102 		link_info->duplex = resp->duplex_state;
11103 	link_info->pause = resp->pause;
11104 	link_info->auto_mode = resp->auto_mode;
11105 	link_info->auto_pause_setting = resp->auto_pause;
11106 	link_info->lp_pause = resp->link_partner_adv_pause;
11107 	link_info->force_pause_setting = resp->force_pause;
11108 	link_info->duplex_setting = resp->duplex_cfg;
11109 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
11110 		link_info->link_speed = le16_to_cpu(resp->link_speed);
11111 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
11112 			link_info->active_lanes = resp->active_lanes;
11113 	} else {
11114 		link_info->link_speed = 0;
11115 		link_info->active_lanes = 0;
11116 	}
11117 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
11118 	link_info->force_pam4_link_speed =
11119 		le16_to_cpu(resp->force_pam4_link_speed);
11120 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
11121 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
11122 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
11123 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
11124 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
11125 	link_info->auto_pam4_link_speeds =
11126 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
11127 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
11128 	link_info->lp_auto_link_speeds =
11129 		le16_to_cpu(resp->link_partner_adv_speeds);
11130 	link_info->lp_auto_pam4_link_speeds =
11131 		resp->link_partner_pam4_adv_speeds;
11132 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
11133 	link_info->phy_ver[0] = resp->phy_maj;
11134 	link_info->phy_ver[1] = resp->phy_min;
11135 	link_info->phy_ver[2] = resp->phy_bld;
11136 	link_info->media_type = resp->media_type;
11137 	link_info->phy_type = resp->phy_type;
11138 	link_info->transceiver = resp->xcvr_pkg_type;
11139 	link_info->phy_addr = resp->eee_config_phy_addr &
11140 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
11141 	link_info->module_status = resp->module_status;
11142 
11143 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
11144 		struct ethtool_keee *eee = &bp->eee;
11145 		u16 fw_speeds;
11146 
11147 		eee->eee_active = 0;
11148 		if (resp->eee_config_phy_addr &
11149 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
11150 			eee->eee_active = 1;
11151 			fw_speeds = le16_to_cpu(
11152 				resp->link_partner_adv_eee_link_speed_mask);
11153 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
11154 		}
11155 
11156 		/* Pull initial EEE config */
11157 		if (!chng_link_state) {
11158 			if (resp->eee_config_phy_addr &
11159 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
11160 				eee->eee_enabled = 1;
11161 
11162 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
11163 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
11164 
11165 			if (resp->eee_config_phy_addr &
11166 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
11167 				__le32 tmr;
11168 
11169 				eee->tx_lpi_enabled = 1;
11170 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
11171 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
11172 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
11173 			}
11174 		}
11175 	}
11176 
11177 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
11178 	if (bp->hwrm_spec_code >= 0x10504) {
11179 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
11180 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
11181 	}
11182 	/* TODO: need to add more logic to report VF link */
11183 	if (chng_link_state) {
11184 		if (link_info->phy_link_status == BNXT_LINK_LINK)
11185 			link_info->link_state = BNXT_LINK_STATE_UP;
11186 		else
11187 			link_info->link_state = BNXT_LINK_STATE_DOWN;
11188 		if (link_state != link_info->link_state)
11189 			bnxt_report_link(bp);
11190 	} else {
11191 		/* always link down if not require to update link state */
11192 		link_info->link_state = BNXT_LINK_STATE_DOWN;
11193 	}
11194 	hwrm_req_drop(bp, req);
11195 
11196 	if (!BNXT_PHY_CFG_ABLE(bp))
11197 		return 0;
11198 
11199 	support_changed = bnxt_support_speed_dropped(link_info);
11200 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
11201 		bnxt_hwrm_set_link_setting(bp, true, false);
11202 	return 0;
11203 }
11204 
11205 static void bnxt_get_port_module_status(struct bnxt *bp)
11206 {
11207 	struct bnxt_link_info *link_info = &bp->link_info;
11208 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
11209 	u8 module_status;
11210 
11211 	if (bnxt_update_link(bp, true))
11212 		return;
11213 
11214 	module_status = link_info->module_status;
11215 	switch (module_status) {
11216 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
11217 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
11218 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
11219 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
11220 			    bp->pf.port_id);
11221 		if (bp->hwrm_spec_code >= 0x10201) {
11222 			netdev_warn(bp->dev, "Module part number %s\n",
11223 				    resp->phy_vendor_partnumber);
11224 		}
11225 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
11226 			netdev_warn(bp->dev, "TX is disabled\n");
11227 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
11228 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
11229 	}
11230 }
11231 
11232 static void
11233 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11234 {
11235 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
11236 		if (bp->hwrm_spec_code >= 0x10201)
11237 			req->auto_pause =
11238 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
11239 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11240 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
11241 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11242 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
11243 		req->enables |=
11244 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11245 	} else {
11246 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11247 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
11248 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11249 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
11250 		req->enables |=
11251 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
11252 		if (bp->hwrm_spec_code >= 0x10201) {
11253 			req->auto_pause = req->force_pause;
11254 			req->enables |= cpu_to_le32(
11255 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11256 		}
11257 	}
11258 }
11259 
11260 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11261 {
11262 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
11263 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
11264 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11265 			req->enables |=
11266 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
11267 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
11268 		} else if (bp->link_info.advertising) {
11269 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
11270 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
11271 		}
11272 		if (bp->link_info.advertising_pam4) {
11273 			req->enables |=
11274 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
11275 			req->auto_link_pam4_speed_mask =
11276 				cpu_to_le16(bp->link_info.advertising_pam4);
11277 		}
11278 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
11279 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
11280 	} else {
11281 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
11282 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11283 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
11284 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
11285 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
11286 				   (u32)bp->link_info.req_link_speed);
11287 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
11288 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11289 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
11290 		} else {
11291 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11292 		}
11293 	}
11294 
11295 	/* tell chimp that the setting takes effect immediately */
11296 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
11297 }
11298 
11299 int bnxt_hwrm_set_pause(struct bnxt *bp)
11300 {
11301 	struct hwrm_port_phy_cfg_input *req;
11302 	int rc;
11303 
11304 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11305 	if (rc)
11306 		return rc;
11307 
11308 	bnxt_hwrm_set_pause_common(bp, req);
11309 
11310 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
11311 	    bp->link_info.force_link_chng)
11312 		bnxt_hwrm_set_link_common(bp, req);
11313 
11314 	rc = hwrm_req_send(bp, req);
11315 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
11316 		/* since changing of pause setting doesn't trigger any link
11317 		 * change event, the driver needs to update the current pause
11318 		 * result upon successfully return of the phy_cfg command
11319 		 */
11320 		bp->link_info.pause =
11321 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
11322 		bp->link_info.auto_pause_setting = 0;
11323 		if (!bp->link_info.force_link_chng)
11324 			bnxt_report_link(bp);
11325 	}
11326 	bp->link_info.force_link_chng = false;
11327 	return rc;
11328 }
11329 
11330 static void bnxt_hwrm_set_eee(struct bnxt *bp,
11331 			      struct hwrm_port_phy_cfg_input *req)
11332 {
11333 	struct ethtool_keee *eee = &bp->eee;
11334 
11335 	if (eee->eee_enabled) {
11336 		u16 eee_speeds;
11337 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
11338 
11339 		if (eee->tx_lpi_enabled)
11340 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
11341 		else
11342 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
11343 
11344 		req->flags |= cpu_to_le32(flags);
11345 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
11346 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
11347 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
11348 	} else {
11349 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
11350 	}
11351 }
11352 
11353 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
11354 {
11355 	struct hwrm_port_phy_cfg_input *req;
11356 	int rc;
11357 
11358 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11359 	if (rc)
11360 		return rc;
11361 
11362 	if (set_pause)
11363 		bnxt_hwrm_set_pause_common(bp, req);
11364 
11365 	bnxt_hwrm_set_link_common(bp, req);
11366 
11367 	if (set_eee)
11368 		bnxt_hwrm_set_eee(bp, req);
11369 	return hwrm_req_send(bp, req);
11370 }
11371 
11372 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
11373 {
11374 	struct hwrm_port_phy_cfg_input *req;
11375 	int rc;
11376 
11377 	if (!BNXT_SINGLE_PF(bp))
11378 		return 0;
11379 
11380 	if (pci_num_vf(bp->pdev) &&
11381 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
11382 		return 0;
11383 
11384 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11385 	if (rc)
11386 		return rc;
11387 
11388 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
11389 	rc = hwrm_req_send(bp, req);
11390 	if (!rc) {
11391 		mutex_lock(&bp->link_lock);
11392 		/* Device is not obliged link down in certain scenarios, even
11393 		 * when forced. Setting the state unknown is consistent with
11394 		 * driver startup and will force link state to be reported
11395 		 * during subsequent open based on PORT_PHY_QCFG.
11396 		 */
11397 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
11398 		mutex_unlock(&bp->link_lock);
11399 	}
11400 	return rc;
11401 }
11402 
11403 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
11404 {
11405 #ifdef CONFIG_TEE_BNXT_FW
11406 	int rc = tee_bnxt_fw_load();
11407 
11408 	if (rc)
11409 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
11410 
11411 	return rc;
11412 #else
11413 	netdev_err(bp->dev, "OP-TEE not supported\n");
11414 	return -ENODEV;
11415 #endif
11416 }
11417 
11418 static int bnxt_try_recover_fw(struct bnxt *bp)
11419 {
11420 	if (bp->fw_health && bp->fw_health->status_reliable) {
11421 		int retry = 0, rc;
11422 		u32 sts;
11423 
11424 		do {
11425 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
11426 			rc = bnxt_hwrm_poll(bp);
11427 			if (!BNXT_FW_IS_BOOTING(sts) &&
11428 			    !BNXT_FW_IS_RECOVERING(sts))
11429 				break;
11430 			retry++;
11431 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
11432 
11433 		if (!BNXT_FW_IS_HEALTHY(sts)) {
11434 			netdev_err(bp->dev,
11435 				   "Firmware not responding, status: 0x%x\n",
11436 				   sts);
11437 			rc = -ENODEV;
11438 		}
11439 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
11440 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
11441 			return bnxt_fw_reset_via_optee(bp);
11442 		}
11443 		return rc;
11444 	}
11445 
11446 	return -ENODEV;
11447 }
11448 
11449 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
11450 {
11451 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11452 
11453 	if (!BNXT_NEW_RM(bp))
11454 		return; /* no resource reservations required */
11455 
11456 	hw_resc->resv_cp_rings = 0;
11457 	hw_resc->resv_stat_ctxs = 0;
11458 	hw_resc->resv_irqs = 0;
11459 	hw_resc->resv_tx_rings = 0;
11460 	hw_resc->resv_rx_rings = 0;
11461 	hw_resc->resv_hw_ring_grps = 0;
11462 	hw_resc->resv_vnics = 0;
11463 	hw_resc->resv_rsscos_ctxs = 0;
11464 	if (!fw_reset) {
11465 		bp->tx_nr_rings = 0;
11466 		bp->rx_nr_rings = 0;
11467 	}
11468 }
11469 
11470 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
11471 {
11472 	int rc;
11473 
11474 	if (!BNXT_NEW_RM(bp))
11475 		return 0; /* no resource reservations required */
11476 
11477 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
11478 	if (rc)
11479 		netdev_err(bp->dev, "resc_qcaps failed\n");
11480 
11481 	bnxt_clear_reservations(bp, fw_reset);
11482 
11483 	return rc;
11484 }
11485 
11486 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
11487 {
11488 	struct hwrm_func_drv_if_change_output *resp;
11489 	struct hwrm_func_drv_if_change_input *req;
11490 	bool fw_reset = !bp->irq_tbl;
11491 	bool resc_reinit = false;
11492 	int rc, retry = 0;
11493 	u32 flags = 0;
11494 
11495 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
11496 		return 0;
11497 
11498 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
11499 	if (rc)
11500 		return rc;
11501 
11502 	if (up)
11503 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
11504 	resp = hwrm_req_hold(bp, req);
11505 
11506 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
11507 	while (retry < BNXT_FW_IF_RETRY) {
11508 		rc = hwrm_req_send(bp, req);
11509 		if (rc != -EAGAIN)
11510 			break;
11511 
11512 		msleep(50);
11513 		retry++;
11514 	}
11515 
11516 	if (rc == -EAGAIN) {
11517 		hwrm_req_drop(bp, req);
11518 		return rc;
11519 	} else if (!rc) {
11520 		flags = le32_to_cpu(resp->flags);
11521 	} else if (up) {
11522 		rc = bnxt_try_recover_fw(bp);
11523 		fw_reset = true;
11524 	}
11525 	hwrm_req_drop(bp, req);
11526 	if (rc)
11527 		return rc;
11528 
11529 	if (!up) {
11530 		bnxt_inv_fw_health_reg(bp);
11531 		return 0;
11532 	}
11533 
11534 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
11535 		resc_reinit = true;
11536 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
11537 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
11538 		fw_reset = true;
11539 	else
11540 		bnxt_remap_fw_health_regs(bp);
11541 
11542 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
11543 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
11544 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
11545 		return -ENODEV;
11546 	}
11547 	if (resc_reinit || fw_reset) {
11548 		if (fw_reset) {
11549 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11550 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11551 				bnxt_ulp_stop(bp);
11552 			bnxt_free_ctx_mem(bp);
11553 			bnxt_dcb_free(bp);
11554 			rc = bnxt_fw_init_one(bp);
11555 			if (rc) {
11556 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11557 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
11558 				return rc;
11559 			}
11560 			bnxt_clear_int_mode(bp);
11561 			rc = bnxt_init_int_mode(bp);
11562 			if (rc) {
11563 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11564 				netdev_err(bp->dev, "init int mode failed\n");
11565 				return rc;
11566 			}
11567 		}
11568 		rc = bnxt_cancel_reservations(bp, fw_reset);
11569 	}
11570 	return rc;
11571 }
11572 
11573 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
11574 {
11575 	struct hwrm_port_led_qcaps_output *resp;
11576 	struct hwrm_port_led_qcaps_input *req;
11577 	struct bnxt_pf_info *pf = &bp->pf;
11578 	int rc;
11579 
11580 	bp->num_leds = 0;
11581 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
11582 		return 0;
11583 
11584 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
11585 	if (rc)
11586 		return rc;
11587 
11588 	req->port_id = cpu_to_le16(pf->port_id);
11589 	resp = hwrm_req_hold(bp, req);
11590 	rc = hwrm_req_send(bp, req);
11591 	if (rc) {
11592 		hwrm_req_drop(bp, req);
11593 		return rc;
11594 	}
11595 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
11596 		int i;
11597 
11598 		bp->num_leds = resp->num_leds;
11599 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
11600 						 bp->num_leds);
11601 		for (i = 0; i < bp->num_leds; i++) {
11602 			struct bnxt_led_info *led = &bp->leds[i];
11603 			__le16 caps = led->led_state_caps;
11604 
11605 			if (!led->led_group_id ||
11606 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
11607 				bp->num_leds = 0;
11608 				break;
11609 			}
11610 		}
11611 	}
11612 	hwrm_req_drop(bp, req);
11613 	return 0;
11614 }
11615 
11616 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
11617 {
11618 	struct hwrm_wol_filter_alloc_output *resp;
11619 	struct hwrm_wol_filter_alloc_input *req;
11620 	int rc;
11621 
11622 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
11623 	if (rc)
11624 		return rc;
11625 
11626 	req->port_id = cpu_to_le16(bp->pf.port_id);
11627 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
11628 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
11629 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
11630 
11631 	resp = hwrm_req_hold(bp, req);
11632 	rc = hwrm_req_send(bp, req);
11633 	if (!rc)
11634 		bp->wol_filter_id = resp->wol_filter_id;
11635 	hwrm_req_drop(bp, req);
11636 	return rc;
11637 }
11638 
11639 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
11640 {
11641 	struct hwrm_wol_filter_free_input *req;
11642 	int rc;
11643 
11644 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
11645 	if (rc)
11646 		return rc;
11647 
11648 	req->port_id = cpu_to_le16(bp->pf.port_id);
11649 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
11650 	req->wol_filter_id = bp->wol_filter_id;
11651 
11652 	return hwrm_req_send(bp, req);
11653 }
11654 
11655 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
11656 {
11657 	struct hwrm_wol_filter_qcfg_output *resp;
11658 	struct hwrm_wol_filter_qcfg_input *req;
11659 	u16 next_handle = 0;
11660 	int rc;
11661 
11662 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
11663 	if (rc)
11664 		return rc;
11665 
11666 	req->port_id = cpu_to_le16(bp->pf.port_id);
11667 	req->handle = cpu_to_le16(handle);
11668 	resp = hwrm_req_hold(bp, req);
11669 	rc = hwrm_req_send(bp, req);
11670 	if (!rc) {
11671 		next_handle = le16_to_cpu(resp->next_handle);
11672 		if (next_handle != 0) {
11673 			if (resp->wol_type ==
11674 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
11675 				bp->wol = 1;
11676 				bp->wol_filter_id = resp->wol_filter_id;
11677 			}
11678 		}
11679 	}
11680 	hwrm_req_drop(bp, req);
11681 	return next_handle;
11682 }
11683 
11684 static void bnxt_get_wol_settings(struct bnxt *bp)
11685 {
11686 	u16 handle = 0;
11687 
11688 	bp->wol = 0;
11689 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
11690 		return;
11691 
11692 	do {
11693 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
11694 	} while (handle && handle != 0xffff);
11695 }
11696 
11697 static bool bnxt_eee_config_ok(struct bnxt *bp)
11698 {
11699 	struct ethtool_keee *eee = &bp->eee;
11700 	struct bnxt_link_info *link_info = &bp->link_info;
11701 
11702 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
11703 		return true;
11704 
11705 	if (eee->eee_enabled) {
11706 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
11707 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
11708 
11709 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
11710 
11711 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
11712 			eee->eee_enabled = 0;
11713 			return false;
11714 		}
11715 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
11716 			linkmode_and(eee->advertised, advertising,
11717 				     eee->supported);
11718 			return false;
11719 		}
11720 	}
11721 	return true;
11722 }
11723 
11724 static int bnxt_update_phy_setting(struct bnxt *bp)
11725 {
11726 	int rc;
11727 	bool update_link = false;
11728 	bool update_pause = false;
11729 	bool update_eee = false;
11730 	struct bnxt_link_info *link_info = &bp->link_info;
11731 
11732 	rc = bnxt_update_link(bp, true);
11733 	if (rc) {
11734 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
11735 			   rc);
11736 		return rc;
11737 	}
11738 	if (!BNXT_SINGLE_PF(bp))
11739 		return 0;
11740 
11741 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
11742 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
11743 	    link_info->req_flow_ctrl)
11744 		update_pause = true;
11745 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
11746 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
11747 		update_pause = true;
11748 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
11749 		if (BNXT_AUTO_MODE(link_info->auto_mode))
11750 			update_link = true;
11751 		if (bnxt_force_speed_updated(link_info))
11752 			update_link = true;
11753 		if (link_info->req_duplex != link_info->duplex_setting)
11754 			update_link = true;
11755 	} else {
11756 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
11757 			update_link = true;
11758 		if (bnxt_auto_speed_updated(link_info))
11759 			update_link = true;
11760 	}
11761 
11762 	/* The last close may have shutdown the link, so need to call
11763 	 * PHY_CFG to bring it back up.
11764 	 */
11765 	if (!BNXT_LINK_IS_UP(bp))
11766 		update_link = true;
11767 
11768 	if (!bnxt_eee_config_ok(bp))
11769 		update_eee = true;
11770 
11771 	if (update_link)
11772 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
11773 	else if (update_pause)
11774 		rc = bnxt_hwrm_set_pause(bp);
11775 	if (rc) {
11776 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
11777 			   rc);
11778 		return rc;
11779 	}
11780 
11781 	return rc;
11782 }
11783 
11784 /* Common routine to pre-map certain register block to different GRC window.
11785  * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
11786  * in PF and 3 windows in VF that can be customized to map in different
11787  * register blocks.
11788  */
11789 static void bnxt_preset_reg_win(struct bnxt *bp)
11790 {
11791 	if (BNXT_PF(bp)) {
11792 		/* CAG registers map to GRC window #4 */
11793 		writel(BNXT_CAG_REG_BASE,
11794 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
11795 	}
11796 }
11797 
11798 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
11799 
11800 static int bnxt_reinit_after_abort(struct bnxt *bp)
11801 {
11802 	int rc;
11803 
11804 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11805 		return -EBUSY;
11806 
11807 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
11808 		return -ENODEV;
11809 
11810 	rc = bnxt_fw_init_one(bp);
11811 	if (!rc) {
11812 		bnxt_clear_int_mode(bp);
11813 		rc = bnxt_init_int_mode(bp);
11814 		if (!rc) {
11815 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
11816 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11817 		}
11818 	}
11819 	return rc;
11820 }
11821 
11822 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
11823 {
11824 	struct bnxt_ntuple_filter *ntp_fltr;
11825 	struct bnxt_l2_filter *l2_fltr;
11826 
11827 	if (list_empty(&fltr->list))
11828 		return;
11829 
11830 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
11831 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
11832 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
11833 		atomic_inc(&l2_fltr->refcnt);
11834 		ntp_fltr->l2_fltr = l2_fltr;
11835 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
11836 			bnxt_del_ntp_filter(bp, ntp_fltr);
11837 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
11838 				   fltr->sw_id);
11839 		}
11840 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
11841 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
11842 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
11843 			bnxt_del_l2_filter(bp, l2_fltr);
11844 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
11845 				   fltr->sw_id);
11846 		}
11847 	}
11848 }
11849 
11850 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
11851 {
11852 	struct bnxt_filter_base *usr_fltr, *tmp;
11853 
11854 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
11855 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
11856 }
11857 
11858 static int bnxt_set_xps_mapping(struct bnxt *bp)
11859 {
11860 	int numa_node = dev_to_node(&bp->pdev->dev);
11861 	unsigned int q_idx, map_idx, cpu, i;
11862 	const struct cpumask *cpu_mask_ptr;
11863 	int nr_cpus = num_online_cpus();
11864 	cpumask_t *q_map;
11865 	int rc = 0;
11866 
11867 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
11868 	if (!q_map)
11869 		return -ENOMEM;
11870 
11871 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
11872 	 * Each TC has the same number of TX queues. The nth TX queue for each
11873 	 * TC will have the same CPU mask.
11874 	 */
11875 	for (i = 0; i < nr_cpus; i++) {
11876 		map_idx = i % bp->tx_nr_rings_per_tc;
11877 		cpu = cpumask_local_spread(i, numa_node);
11878 		cpu_mask_ptr = get_cpu_mask(cpu);
11879 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
11880 	}
11881 
11882 	/* Register CPU mask for each TX queue except the ones marked for XDP */
11883 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
11884 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
11885 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
11886 		if (rc) {
11887 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
11888 				    q_idx);
11889 			break;
11890 		}
11891 	}
11892 
11893 	kfree(q_map);
11894 
11895 	return rc;
11896 }
11897 
11898 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
11899 {
11900 	int rc = 0;
11901 
11902 	bnxt_preset_reg_win(bp);
11903 	netif_carrier_off(bp->dev);
11904 	if (irq_re_init) {
11905 		/* Reserve rings now if none were reserved at driver probe. */
11906 		rc = bnxt_init_dflt_ring_mode(bp);
11907 		if (rc) {
11908 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
11909 			return rc;
11910 		}
11911 	}
11912 	rc = bnxt_reserve_rings(bp, irq_re_init);
11913 	if (rc)
11914 		return rc;
11915 	if ((bp->flags & BNXT_FLAG_RFS) &&
11916 	    !(bp->flags & BNXT_FLAG_USING_MSIX)) {
11917 		/* disable RFS if falling back to INTA */
11918 		bp->dev->hw_features &= ~NETIF_F_NTUPLE;
11919 		bp->flags &= ~BNXT_FLAG_RFS;
11920 	}
11921 
11922 	rc = bnxt_alloc_mem(bp, irq_re_init);
11923 	if (rc) {
11924 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
11925 		goto open_err_free_mem;
11926 	}
11927 
11928 	if (irq_re_init) {
11929 		bnxt_init_napi(bp);
11930 		rc = bnxt_request_irq(bp);
11931 		if (rc) {
11932 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
11933 			goto open_err_irq;
11934 		}
11935 	}
11936 
11937 	rc = bnxt_init_nic(bp, irq_re_init);
11938 	if (rc) {
11939 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
11940 		goto open_err_irq;
11941 	}
11942 
11943 	bnxt_enable_napi(bp);
11944 	bnxt_debug_dev_init(bp);
11945 
11946 	if (link_re_init) {
11947 		mutex_lock(&bp->link_lock);
11948 		rc = bnxt_update_phy_setting(bp);
11949 		mutex_unlock(&bp->link_lock);
11950 		if (rc) {
11951 			netdev_warn(bp->dev, "failed to update phy settings\n");
11952 			if (BNXT_SINGLE_PF(bp)) {
11953 				bp->link_info.phy_retry = true;
11954 				bp->link_info.phy_retry_expires =
11955 					jiffies + 5 * HZ;
11956 			}
11957 		}
11958 	}
11959 
11960 	if (irq_re_init) {
11961 		udp_tunnel_nic_reset_ntf(bp->dev);
11962 		rc = bnxt_set_xps_mapping(bp);
11963 		if (rc)
11964 			netdev_warn(bp->dev, "failed to set xps mapping\n");
11965 	}
11966 
11967 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
11968 		if (!static_key_enabled(&bnxt_xdp_locking_key))
11969 			static_branch_enable(&bnxt_xdp_locking_key);
11970 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
11971 		static_branch_disable(&bnxt_xdp_locking_key);
11972 	}
11973 	set_bit(BNXT_STATE_OPEN, &bp->state);
11974 	bnxt_enable_int(bp);
11975 	/* Enable TX queues */
11976 	bnxt_tx_enable(bp);
11977 	mod_timer(&bp->timer, jiffies + bp->current_interval);
11978 	/* Poll link status and check for SFP+ module status */
11979 	mutex_lock(&bp->link_lock);
11980 	bnxt_get_port_module_status(bp);
11981 	mutex_unlock(&bp->link_lock);
11982 
11983 	/* VF-reps may need to be re-opened after the PF is re-opened */
11984 	if (BNXT_PF(bp))
11985 		bnxt_vf_reps_open(bp);
11986 	if (bp->ptp_cfg)
11987 		atomic_set(&bp->ptp_cfg->tx_avail, BNXT_MAX_TX_TS);
11988 	bnxt_ptp_init_rtc(bp, true);
11989 	bnxt_ptp_cfg_tstamp_filters(bp);
11990 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
11991 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
11992 	bnxt_cfg_usr_fltrs(bp);
11993 	return 0;
11994 
11995 open_err_irq:
11996 	bnxt_del_napi(bp);
11997 
11998 open_err_free_mem:
11999 	bnxt_free_skbs(bp);
12000 	bnxt_free_irq(bp);
12001 	bnxt_free_mem(bp, true);
12002 	return rc;
12003 }
12004 
12005 /* rtnl_lock held */
12006 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12007 {
12008 	int rc = 0;
12009 
12010 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12011 		rc = -EIO;
12012 	if (!rc)
12013 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12014 	if (rc) {
12015 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12016 		dev_close(bp->dev);
12017 	}
12018 	return rc;
12019 }
12020 
12021 /* rtnl_lock held, open the NIC half way by allocating all resources, but
12022  * NAPI, IRQ, and TX are not enabled.  This is mainly used for offline
12023  * self tests.
12024  */
12025 int bnxt_half_open_nic(struct bnxt *bp)
12026 {
12027 	int rc = 0;
12028 
12029 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12030 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
12031 		rc = -ENODEV;
12032 		goto half_open_err;
12033 	}
12034 
12035 	rc = bnxt_alloc_mem(bp, true);
12036 	if (rc) {
12037 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12038 		goto half_open_err;
12039 	}
12040 	bnxt_init_napi(bp);
12041 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12042 	rc = bnxt_init_nic(bp, true);
12043 	if (rc) {
12044 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12045 		bnxt_del_napi(bp);
12046 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12047 		goto half_open_err;
12048 	}
12049 	return 0;
12050 
12051 half_open_err:
12052 	bnxt_free_skbs(bp);
12053 	bnxt_free_mem(bp, true);
12054 	dev_close(bp->dev);
12055 	return rc;
12056 }
12057 
12058 /* rtnl_lock held, this call can only be made after a previous successful
12059  * call to bnxt_half_open_nic().
12060  */
12061 void bnxt_half_close_nic(struct bnxt *bp)
12062 {
12063 	bnxt_hwrm_resource_free(bp, false, true);
12064 	bnxt_del_napi(bp);
12065 	bnxt_free_skbs(bp);
12066 	bnxt_free_mem(bp, true);
12067 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12068 }
12069 
12070 void bnxt_reenable_sriov(struct bnxt *bp)
12071 {
12072 	if (BNXT_PF(bp)) {
12073 		struct bnxt_pf_info *pf = &bp->pf;
12074 		int n = pf->active_vfs;
12075 
12076 		if (n)
12077 			bnxt_cfg_hw_sriov(bp, &n, true);
12078 	}
12079 }
12080 
12081 static int bnxt_open(struct net_device *dev)
12082 {
12083 	struct bnxt *bp = netdev_priv(dev);
12084 	int rc;
12085 
12086 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12087 		rc = bnxt_reinit_after_abort(bp);
12088 		if (rc) {
12089 			if (rc == -EBUSY)
12090 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
12091 			else
12092 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
12093 			return -ENODEV;
12094 		}
12095 	}
12096 
12097 	rc = bnxt_hwrm_if_change(bp, true);
12098 	if (rc)
12099 		return rc;
12100 
12101 	rc = __bnxt_open_nic(bp, true, true);
12102 	if (rc) {
12103 		bnxt_hwrm_if_change(bp, false);
12104 	} else {
12105 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
12106 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12107 				bnxt_ulp_start(bp, 0);
12108 				bnxt_reenable_sriov(bp);
12109 			}
12110 		}
12111 	}
12112 
12113 	return rc;
12114 }
12115 
12116 static bool bnxt_drv_busy(struct bnxt *bp)
12117 {
12118 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
12119 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
12120 }
12121 
12122 static void bnxt_get_ring_stats(struct bnxt *bp,
12123 				struct rtnl_link_stats64 *stats);
12124 
12125 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
12126 			     bool link_re_init)
12127 {
12128 	/* Close the VF-reps before closing PF */
12129 	if (BNXT_PF(bp))
12130 		bnxt_vf_reps_close(bp);
12131 
12132 	/* Change device state to avoid TX queue wake up's */
12133 	bnxt_tx_disable(bp);
12134 
12135 	clear_bit(BNXT_STATE_OPEN, &bp->state);
12136 	smp_mb__after_atomic();
12137 	while (bnxt_drv_busy(bp))
12138 		msleep(20);
12139 
12140 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12141 		bnxt_clear_rss_ctxs(bp, false);
12142 	/* Flush rings and disable interrupts */
12143 	bnxt_shutdown_nic(bp, irq_re_init);
12144 
12145 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
12146 
12147 	bnxt_debug_dev_exit(bp);
12148 	bnxt_disable_napi(bp);
12149 	del_timer_sync(&bp->timer);
12150 	bnxt_free_skbs(bp);
12151 
12152 	/* Save ring stats before shutdown */
12153 	if (bp->bnapi && irq_re_init) {
12154 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
12155 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
12156 	}
12157 	if (irq_re_init) {
12158 		bnxt_free_irq(bp);
12159 		bnxt_del_napi(bp);
12160 	}
12161 	bnxt_free_mem(bp, irq_re_init);
12162 }
12163 
12164 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12165 {
12166 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12167 		/* If we get here, it means firmware reset is in progress
12168 		 * while we are trying to close.  We can safely proceed with
12169 		 * the close because we are holding rtnl_lock().  Some firmware
12170 		 * messages may fail as we proceed to close.  We set the
12171 		 * ABORT_ERR flag here so that the FW reset thread will later
12172 		 * abort when it gets the rtnl_lock() and sees the flag.
12173 		 */
12174 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
12175 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12176 	}
12177 
12178 #ifdef CONFIG_BNXT_SRIOV
12179 	if (bp->sriov_cfg) {
12180 		int rc;
12181 
12182 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
12183 						      !bp->sriov_cfg,
12184 						      BNXT_SRIOV_CFG_WAIT_TMO);
12185 		if (!rc)
12186 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
12187 		else if (rc < 0)
12188 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
12189 	}
12190 #endif
12191 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
12192 }
12193 
12194 static int bnxt_close(struct net_device *dev)
12195 {
12196 	struct bnxt *bp = netdev_priv(dev);
12197 
12198 	bnxt_close_nic(bp, true, true);
12199 	bnxt_hwrm_shutdown_link(bp);
12200 	bnxt_hwrm_if_change(bp, false);
12201 	return 0;
12202 }
12203 
12204 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
12205 				   u16 *val)
12206 {
12207 	struct hwrm_port_phy_mdio_read_output *resp;
12208 	struct hwrm_port_phy_mdio_read_input *req;
12209 	int rc;
12210 
12211 	if (bp->hwrm_spec_code < 0x10a00)
12212 		return -EOPNOTSUPP;
12213 
12214 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
12215 	if (rc)
12216 		return rc;
12217 
12218 	req->port_id = cpu_to_le16(bp->pf.port_id);
12219 	req->phy_addr = phy_addr;
12220 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12221 	if (mdio_phy_id_is_c45(phy_addr)) {
12222 		req->cl45_mdio = 1;
12223 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12224 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12225 		req->reg_addr = cpu_to_le16(reg);
12226 	}
12227 
12228 	resp = hwrm_req_hold(bp, req);
12229 	rc = hwrm_req_send(bp, req);
12230 	if (!rc)
12231 		*val = le16_to_cpu(resp->reg_data);
12232 	hwrm_req_drop(bp, req);
12233 	return rc;
12234 }
12235 
12236 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
12237 				    u16 val)
12238 {
12239 	struct hwrm_port_phy_mdio_write_input *req;
12240 	int rc;
12241 
12242 	if (bp->hwrm_spec_code < 0x10a00)
12243 		return -EOPNOTSUPP;
12244 
12245 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
12246 	if (rc)
12247 		return rc;
12248 
12249 	req->port_id = cpu_to_le16(bp->pf.port_id);
12250 	req->phy_addr = phy_addr;
12251 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12252 	if (mdio_phy_id_is_c45(phy_addr)) {
12253 		req->cl45_mdio = 1;
12254 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12255 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12256 		req->reg_addr = cpu_to_le16(reg);
12257 	}
12258 	req->reg_data = cpu_to_le16(val);
12259 
12260 	return hwrm_req_send(bp, req);
12261 }
12262 
12263 /* rtnl_lock held */
12264 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12265 {
12266 	struct mii_ioctl_data *mdio = if_mii(ifr);
12267 	struct bnxt *bp = netdev_priv(dev);
12268 	int rc;
12269 
12270 	switch (cmd) {
12271 	case SIOCGMIIPHY:
12272 		mdio->phy_id = bp->link_info.phy_addr;
12273 
12274 		fallthrough;
12275 	case SIOCGMIIREG: {
12276 		u16 mii_regval = 0;
12277 
12278 		if (!netif_running(dev))
12279 			return -EAGAIN;
12280 
12281 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
12282 					     &mii_regval);
12283 		mdio->val_out = mii_regval;
12284 		return rc;
12285 	}
12286 
12287 	case SIOCSMIIREG:
12288 		if (!netif_running(dev))
12289 			return -EAGAIN;
12290 
12291 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
12292 						mdio->val_in);
12293 
12294 	case SIOCSHWTSTAMP:
12295 		return bnxt_hwtstamp_set(dev, ifr);
12296 
12297 	case SIOCGHWTSTAMP:
12298 		return bnxt_hwtstamp_get(dev, ifr);
12299 
12300 	default:
12301 		/* do nothing */
12302 		break;
12303 	}
12304 	return -EOPNOTSUPP;
12305 }
12306 
12307 static void bnxt_get_ring_stats(struct bnxt *bp,
12308 				struct rtnl_link_stats64 *stats)
12309 {
12310 	int i;
12311 
12312 	for (i = 0; i < bp->cp_nr_rings; i++) {
12313 		struct bnxt_napi *bnapi = bp->bnapi[i];
12314 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
12315 		u64 *sw = cpr->stats.sw_stats;
12316 
12317 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
12318 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12319 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
12320 
12321 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
12322 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
12323 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
12324 
12325 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
12326 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
12327 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
12328 
12329 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
12330 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
12331 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
12332 
12333 		stats->rx_missed_errors +=
12334 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
12335 
12336 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12337 
12338 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
12339 
12340 		stats->rx_dropped +=
12341 			cpr->sw_stats.rx.rx_netpoll_discards +
12342 			cpr->sw_stats.rx.rx_oom_discards;
12343 	}
12344 }
12345 
12346 static void bnxt_add_prev_stats(struct bnxt *bp,
12347 				struct rtnl_link_stats64 *stats)
12348 {
12349 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
12350 
12351 	stats->rx_packets += prev_stats->rx_packets;
12352 	stats->tx_packets += prev_stats->tx_packets;
12353 	stats->rx_bytes += prev_stats->rx_bytes;
12354 	stats->tx_bytes += prev_stats->tx_bytes;
12355 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
12356 	stats->multicast += prev_stats->multicast;
12357 	stats->rx_dropped += prev_stats->rx_dropped;
12358 	stats->tx_dropped += prev_stats->tx_dropped;
12359 }
12360 
12361 static void
12362 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
12363 {
12364 	struct bnxt *bp = netdev_priv(dev);
12365 
12366 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
12367 	/* Make sure bnxt_close_nic() sees that we are reading stats before
12368 	 * we check the BNXT_STATE_OPEN flag.
12369 	 */
12370 	smp_mb__after_atomic();
12371 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12372 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12373 		*stats = bp->net_stats_prev;
12374 		return;
12375 	}
12376 
12377 	bnxt_get_ring_stats(bp, stats);
12378 	bnxt_add_prev_stats(bp, stats);
12379 
12380 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
12381 		u64 *rx = bp->port_stats.sw_stats;
12382 		u64 *tx = bp->port_stats.sw_stats +
12383 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
12384 
12385 		stats->rx_crc_errors =
12386 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
12387 		stats->rx_frame_errors =
12388 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
12389 		stats->rx_length_errors =
12390 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
12391 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
12392 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
12393 		stats->rx_errors =
12394 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
12395 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
12396 		stats->collisions =
12397 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
12398 		stats->tx_fifo_errors =
12399 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
12400 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
12401 	}
12402 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12403 }
12404 
12405 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
12406 					struct bnxt_total_ring_err_stats *stats,
12407 					struct bnxt_cp_ring_info *cpr)
12408 {
12409 	struct bnxt_sw_stats *sw_stats = &cpr->sw_stats;
12410 	u64 *hw_stats = cpr->stats.sw_stats;
12411 
12412 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
12413 	stats->rx_total_resets += sw_stats->rx.rx_resets;
12414 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
12415 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
12416 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
12417 	stats->rx_total_ring_discards +=
12418 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
12419 	stats->tx_total_resets += sw_stats->tx.tx_resets;
12420 	stats->tx_total_ring_discards +=
12421 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
12422 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
12423 }
12424 
12425 void bnxt_get_ring_err_stats(struct bnxt *bp,
12426 			     struct bnxt_total_ring_err_stats *stats)
12427 {
12428 	int i;
12429 
12430 	for (i = 0; i < bp->cp_nr_rings; i++)
12431 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
12432 }
12433 
12434 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
12435 {
12436 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12437 	struct net_device *dev = bp->dev;
12438 	struct netdev_hw_addr *ha;
12439 	u8 *haddr;
12440 	int mc_count = 0;
12441 	bool update = false;
12442 	int off = 0;
12443 
12444 	netdev_for_each_mc_addr(ha, dev) {
12445 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
12446 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12447 			vnic->mc_list_count = 0;
12448 			return false;
12449 		}
12450 		haddr = ha->addr;
12451 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
12452 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
12453 			update = true;
12454 		}
12455 		off += ETH_ALEN;
12456 		mc_count++;
12457 	}
12458 	if (mc_count)
12459 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
12460 
12461 	if (mc_count != vnic->mc_list_count) {
12462 		vnic->mc_list_count = mc_count;
12463 		update = true;
12464 	}
12465 	return update;
12466 }
12467 
12468 static bool bnxt_uc_list_updated(struct bnxt *bp)
12469 {
12470 	struct net_device *dev = bp->dev;
12471 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12472 	struct netdev_hw_addr *ha;
12473 	int off = 0;
12474 
12475 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
12476 		return true;
12477 
12478 	netdev_for_each_uc_addr(ha, dev) {
12479 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
12480 			return true;
12481 
12482 		off += ETH_ALEN;
12483 	}
12484 	return false;
12485 }
12486 
12487 static void bnxt_set_rx_mode(struct net_device *dev)
12488 {
12489 	struct bnxt *bp = netdev_priv(dev);
12490 	struct bnxt_vnic_info *vnic;
12491 	bool mc_update = false;
12492 	bool uc_update;
12493 	u32 mask;
12494 
12495 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
12496 		return;
12497 
12498 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12499 	mask = vnic->rx_mask;
12500 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
12501 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
12502 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
12503 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
12504 
12505 	if (dev->flags & IFF_PROMISC)
12506 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12507 
12508 	uc_update = bnxt_uc_list_updated(bp);
12509 
12510 	if (dev->flags & IFF_BROADCAST)
12511 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
12512 	if (dev->flags & IFF_ALLMULTI) {
12513 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12514 		vnic->mc_list_count = 0;
12515 	} else if (dev->flags & IFF_MULTICAST) {
12516 		mc_update = bnxt_mc_list_updated(bp, &mask);
12517 	}
12518 
12519 	if (mask != vnic->rx_mask || uc_update || mc_update) {
12520 		vnic->rx_mask = mask;
12521 
12522 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
12523 	}
12524 }
12525 
12526 static int bnxt_cfg_rx_mode(struct bnxt *bp)
12527 {
12528 	struct net_device *dev = bp->dev;
12529 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12530 	struct netdev_hw_addr *ha;
12531 	int i, off = 0, rc;
12532 	bool uc_update;
12533 
12534 	netif_addr_lock_bh(dev);
12535 	uc_update = bnxt_uc_list_updated(bp);
12536 	netif_addr_unlock_bh(dev);
12537 
12538 	if (!uc_update)
12539 		goto skip_uc;
12540 
12541 	for (i = 1; i < vnic->uc_filter_count; i++) {
12542 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
12543 
12544 		bnxt_hwrm_l2_filter_free(bp, fltr);
12545 		bnxt_del_l2_filter(bp, fltr);
12546 	}
12547 
12548 	vnic->uc_filter_count = 1;
12549 
12550 	netif_addr_lock_bh(dev);
12551 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
12552 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12553 	} else {
12554 		netdev_for_each_uc_addr(ha, dev) {
12555 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
12556 			off += ETH_ALEN;
12557 			vnic->uc_filter_count++;
12558 		}
12559 	}
12560 	netif_addr_unlock_bh(dev);
12561 
12562 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
12563 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
12564 		if (rc) {
12565 			if (BNXT_VF(bp) && rc == -ENODEV) {
12566 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
12567 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
12568 				else
12569 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
12570 				rc = 0;
12571 			} else {
12572 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
12573 			}
12574 			vnic->uc_filter_count = i;
12575 			return rc;
12576 		}
12577 	}
12578 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
12579 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
12580 
12581 skip_uc:
12582 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
12583 	    !bnxt_promisc_ok(bp))
12584 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12585 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
12586 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
12587 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
12588 			    rc);
12589 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
12590 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12591 		vnic->mc_list_count = 0;
12592 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
12593 	}
12594 	if (rc)
12595 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
12596 			   rc);
12597 
12598 	return rc;
12599 }
12600 
12601 static bool bnxt_can_reserve_rings(struct bnxt *bp)
12602 {
12603 #ifdef CONFIG_BNXT_SRIOV
12604 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
12605 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12606 
12607 		/* No minimum rings were provisioned by the PF.  Don't
12608 		 * reserve rings by default when device is down.
12609 		 */
12610 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
12611 			return true;
12612 
12613 		if (!netif_running(bp->dev))
12614 			return false;
12615 	}
12616 #endif
12617 	return true;
12618 }
12619 
12620 /* If the chip and firmware supports RFS */
12621 static bool bnxt_rfs_supported(struct bnxt *bp)
12622 {
12623 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
12624 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
12625 			return true;
12626 		return false;
12627 	}
12628 	/* 212 firmware is broken for aRFS */
12629 	if (BNXT_FW_MAJ(bp) == 212)
12630 		return false;
12631 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
12632 		return true;
12633 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
12634 		return true;
12635 	return false;
12636 }
12637 
12638 /* If runtime conditions support RFS */
12639 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
12640 {
12641 	struct bnxt_hw_rings hwr = {0};
12642 	int max_vnics, max_rss_ctxs;
12643 
12644 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
12645 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
12646 		return bnxt_rfs_supported(bp);
12647 
12648 	if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
12649 		return false;
12650 
12651 	hwr.grp = bp->rx_nr_rings;
12652 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
12653 	if (new_rss_ctx)
12654 		hwr.vnic++;
12655 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
12656 	max_vnics = bnxt_get_max_func_vnics(bp);
12657 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
12658 
12659 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
12660 		if (bp->rx_nr_rings > 1)
12661 			netdev_warn(bp->dev,
12662 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
12663 				    min(max_rss_ctxs - 1, max_vnics - 1));
12664 		return false;
12665 	}
12666 
12667 	if (!BNXT_NEW_RM(bp))
12668 		return true;
12669 
12670 	if (hwr.vnic == bp->hw_resc.resv_vnics &&
12671 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
12672 		return true;
12673 
12674 	bnxt_hwrm_reserve_rings(bp, &hwr);
12675 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
12676 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
12677 		return true;
12678 
12679 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
12680 	hwr.vnic = 1;
12681 	hwr.rss_ctx = 0;
12682 	bnxt_hwrm_reserve_rings(bp, &hwr);
12683 	return false;
12684 }
12685 
12686 static netdev_features_t bnxt_fix_features(struct net_device *dev,
12687 					   netdev_features_t features)
12688 {
12689 	struct bnxt *bp = netdev_priv(dev);
12690 	netdev_features_t vlan_features;
12691 
12692 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
12693 		features &= ~NETIF_F_NTUPLE;
12694 
12695 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
12696 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
12697 
12698 	if (!(features & NETIF_F_GRO))
12699 		features &= ~NETIF_F_GRO_HW;
12700 
12701 	if (features & NETIF_F_GRO_HW)
12702 		features &= ~NETIF_F_LRO;
12703 
12704 	/* Both CTAG and STAG VLAN accelaration on the RX side have to be
12705 	 * turned on or off together.
12706 	 */
12707 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
12708 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
12709 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
12710 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
12711 		else if (vlan_features)
12712 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
12713 	}
12714 #ifdef CONFIG_BNXT_SRIOV
12715 	if (BNXT_VF(bp) && bp->vf.vlan)
12716 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
12717 #endif
12718 	return features;
12719 }
12720 
12721 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
12722 				bool link_re_init, u32 flags, bool update_tpa)
12723 {
12724 	bnxt_close_nic(bp, irq_re_init, link_re_init);
12725 	bp->flags = flags;
12726 	if (update_tpa)
12727 		bnxt_set_ring_params(bp);
12728 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
12729 }
12730 
12731 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
12732 {
12733 	bool update_tpa = false, update_ntuple = false;
12734 	struct bnxt *bp = netdev_priv(dev);
12735 	u32 flags = bp->flags;
12736 	u32 changes;
12737 	int rc = 0;
12738 	bool re_init = false;
12739 
12740 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
12741 	if (features & NETIF_F_GRO_HW)
12742 		flags |= BNXT_FLAG_GRO;
12743 	else if (features & NETIF_F_LRO)
12744 		flags |= BNXT_FLAG_LRO;
12745 
12746 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
12747 		flags &= ~BNXT_FLAG_TPA;
12748 
12749 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
12750 		flags |= BNXT_FLAG_STRIP_VLAN;
12751 
12752 	if (features & NETIF_F_NTUPLE)
12753 		flags |= BNXT_FLAG_RFS;
12754 	else
12755 		bnxt_clear_usr_fltrs(bp, true);
12756 
12757 	changes = flags ^ bp->flags;
12758 	if (changes & BNXT_FLAG_TPA) {
12759 		update_tpa = true;
12760 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
12761 		    (flags & BNXT_FLAG_TPA) == 0 ||
12762 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
12763 			re_init = true;
12764 	}
12765 
12766 	if (changes & ~BNXT_FLAG_TPA)
12767 		re_init = true;
12768 
12769 	if (changes & BNXT_FLAG_RFS)
12770 		update_ntuple = true;
12771 
12772 	if (flags != bp->flags) {
12773 		u32 old_flags = bp->flags;
12774 
12775 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12776 			bp->flags = flags;
12777 			if (update_tpa)
12778 				bnxt_set_ring_params(bp);
12779 			return rc;
12780 		}
12781 
12782 		if (update_ntuple)
12783 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
12784 
12785 		if (re_init)
12786 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
12787 
12788 		if (update_tpa) {
12789 			bp->flags = flags;
12790 			rc = bnxt_set_tpa(bp,
12791 					  (flags & BNXT_FLAG_TPA) ?
12792 					  true : false);
12793 			if (rc)
12794 				bp->flags = old_flags;
12795 		}
12796 	}
12797 	return rc;
12798 }
12799 
12800 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
12801 			      u8 **nextp)
12802 {
12803 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
12804 	struct hop_jumbo_hdr *jhdr;
12805 	int hdr_count = 0;
12806 	u8 *nexthdr;
12807 	int start;
12808 
12809 	/* Check that there are at most 2 IPv6 extension headers, no
12810 	 * fragment header, and each is <= 64 bytes.
12811 	 */
12812 	start = nw_off + sizeof(*ip6h);
12813 	nexthdr = &ip6h->nexthdr;
12814 	while (ipv6_ext_hdr(*nexthdr)) {
12815 		struct ipv6_opt_hdr *hp;
12816 		int hdrlen;
12817 
12818 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
12819 		    *nexthdr == NEXTHDR_FRAGMENT)
12820 			return false;
12821 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
12822 					  skb_headlen(skb), NULL);
12823 		if (!hp)
12824 			return false;
12825 		if (*nexthdr == NEXTHDR_AUTH)
12826 			hdrlen = ipv6_authlen(hp);
12827 		else
12828 			hdrlen = ipv6_optlen(hp);
12829 
12830 		if (hdrlen > 64)
12831 			return false;
12832 
12833 		/* The ext header may be a hop-by-hop header inserted for
12834 		 * big TCP purposes. This will be removed before sending
12835 		 * from NIC, so do not count it.
12836 		 */
12837 		if (*nexthdr == NEXTHDR_HOP) {
12838 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
12839 				goto increment_hdr;
12840 
12841 			jhdr = (struct hop_jumbo_hdr *)hp;
12842 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
12843 			    jhdr->nexthdr != IPPROTO_TCP)
12844 				goto increment_hdr;
12845 
12846 			goto next_hdr;
12847 		}
12848 increment_hdr:
12849 		hdr_count++;
12850 next_hdr:
12851 		nexthdr = &hp->nexthdr;
12852 		start += hdrlen;
12853 	}
12854 	if (nextp) {
12855 		/* Caller will check inner protocol */
12856 		if (skb->encapsulation) {
12857 			*nextp = nexthdr;
12858 			return true;
12859 		}
12860 		*nextp = NULL;
12861 	}
12862 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
12863 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
12864 }
12865 
12866 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
12867 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
12868 {
12869 	struct udphdr *uh = udp_hdr(skb);
12870 	__be16 udp_port = uh->dest;
12871 
12872 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
12873 	    udp_port != bp->vxlan_gpe_port)
12874 		return false;
12875 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
12876 		struct ethhdr *eh = inner_eth_hdr(skb);
12877 
12878 		switch (eh->h_proto) {
12879 		case htons(ETH_P_IP):
12880 			return true;
12881 		case htons(ETH_P_IPV6):
12882 			return bnxt_exthdr_check(bp, skb,
12883 						 skb_inner_network_offset(skb),
12884 						 NULL);
12885 		}
12886 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
12887 		return true;
12888 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
12889 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
12890 					 NULL);
12891 	}
12892 	return false;
12893 }
12894 
12895 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
12896 {
12897 	switch (l4_proto) {
12898 	case IPPROTO_UDP:
12899 		return bnxt_udp_tunl_check(bp, skb);
12900 	case IPPROTO_IPIP:
12901 		return true;
12902 	case IPPROTO_GRE: {
12903 		switch (skb->inner_protocol) {
12904 		default:
12905 			return false;
12906 		case htons(ETH_P_IP):
12907 			return true;
12908 		case htons(ETH_P_IPV6):
12909 			fallthrough;
12910 		}
12911 	}
12912 	case IPPROTO_IPV6:
12913 		/* Check ext headers of inner ipv6 */
12914 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
12915 					 NULL);
12916 	}
12917 	return false;
12918 }
12919 
12920 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
12921 					     struct net_device *dev,
12922 					     netdev_features_t features)
12923 {
12924 	struct bnxt *bp = netdev_priv(dev);
12925 	u8 *l4_proto;
12926 
12927 	features = vlan_features_check(skb, features);
12928 	switch (vlan_get_protocol(skb)) {
12929 	case htons(ETH_P_IP):
12930 		if (!skb->encapsulation)
12931 			return features;
12932 		l4_proto = &ip_hdr(skb)->protocol;
12933 		if (bnxt_tunl_check(bp, skb, *l4_proto))
12934 			return features;
12935 		break;
12936 	case htons(ETH_P_IPV6):
12937 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
12938 				       &l4_proto))
12939 			break;
12940 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
12941 			return features;
12942 		break;
12943 	}
12944 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
12945 }
12946 
12947 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
12948 			 u32 *reg_buf)
12949 {
12950 	struct hwrm_dbg_read_direct_output *resp;
12951 	struct hwrm_dbg_read_direct_input *req;
12952 	__le32 *dbg_reg_buf;
12953 	dma_addr_t mapping;
12954 	int rc, i;
12955 
12956 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
12957 	if (rc)
12958 		return rc;
12959 
12960 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
12961 					 &mapping);
12962 	if (!dbg_reg_buf) {
12963 		rc = -ENOMEM;
12964 		goto dbg_rd_reg_exit;
12965 	}
12966 
12967 	req->host_dest_addr = cpu_to_le64(mapping);
12968 
12969 	resp = hwrm_req_hold(bp, req);
12970 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
12971 	req->read_len32 = cpu_to_le32(num_words);
12972 
12973 	rc = hwrm_req_send(bp, req);
12974 	if (rc || resp->error_code) {
12975 		rc = -EIO;
12976 		goto dbg_rd_reg_exit;
12977 	}
12978 	for (i = 0; i < num_words; i++)
12979 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
12980 
12981 dbg_rd_reg_exit:
12982 	hwrm_req_drop(bp, req);
12983 	return rc;
12984 }
12985 
12986 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
12987 				       u32 ring_id, u32 *prod, u32 *cons)
12988 {
12989 	struct hwrm_dbg_ring_info_get_output *resp;
12990 	struct hwrm_dbg_ring_info_get_input *req;
12991 	int rc;
12992 
12993 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
12994 	if (rc)
12995 		return rc;
12996 
12997 	req->ring_type = ring_type;
12998 	req->fw_ring_id = cpu_to_le32(ring_id);
12999 	resp = hwrm_req_hold(bp, req);
13000 	rc = hwrm_req_send(bp, req);
13001 	if (!rc) {
13002 		*prod = le32_to_cpu(resp->producer_index);
13003 		*cons = le32_to_cpu(resp->consumer_index);
13004 	}
13005 	hwrm_req_drop(bp, req);
13006 	return rc;
13007 }
13008 
13009 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13010 {
13011 	struct bnxt_tx_ring_info *txr;
13012 	int i = bnapi->index, j;
13013 
13014 	bnxt_for_each_napi_tx(j, bnapi, txr)
13015 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13016 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13017 			    txr->tx_cons);
13018 }
13019 
13020 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
13021 {
13022 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
13023 	int i = bnapi->index;
13024 
13025 	if (!rxr)
13026 		return;
13027 
13028 	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",
13029 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
13030 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
13031 		    rxr->rx_sw_agg_prod);
13032 }
13033 
13034 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
13035 {
13036 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13037 	int i = bnapi->index;
13038 
13039 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
13040 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
13041 }
13042 
13043 static void bnxt_dbg_dump_states(struct bnxt *bp)
13044 {
13045 	int i;
13046 	struct bnxt_napi *bnapi;
13047 
13048 	for (i = 0; i < bp->cp_nr_rings; i++) {
13049 		bnapi = bp->bnapi[i];
13050 		if (netif_msg_drv(bp)) {
13051 			bnxt_dump_tx_sw_state(bnapi);
13052 			bnxt_dump_rx_sw_state(bnapi);
13053 			bnxt_dump_cp_sw_state(bnapi);
13054 		}
13055 	}
13056 }
13057 
13058 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
13059 {
13060 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
13061 	struct hwrm_ring_reset_input *req;
13062 	struct bnxt_napi *bnapi = rxr->bnapi;
13063 	struct bnxt_cp_ring_info *cpr;
13064 	u16 cp_ring_id;
13065 	int rc;
13066 
13067 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
13068 	if (rc)
13069 		return rc;
13070 
13071 	cpr = &bnapi->cp_ring;
13072 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
13073 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
13074 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
13075 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
13076 	return hwrm_req_send_silent(bp, req);
13077 }
13078 
13079 static void bnxt_reset_task(struct bnxt *bp, bool silent)
13080 {
13081 	if (!silent)
13082 		bnxt_dbg_dump_states(bp);
13083 	if (netif_running(bp->dev)) {
13084 		int rc;
13085 
13086 		if (silent) {
13087 			bnxt_close_nic(bp, false, false);
13088 			bnxt_open_nic(bp, false, false);
13089 		} else {
13090 			bnxt_ulp_stop(bp);
13091 			bnxt_close_nic(bp, true, false);
13092 			rc = bnxt_open_nic(bp, true, false);
13093 			bnxt_ulp_start(bp, rc);
13094 		}
13095 	}
13096 }
13097 
13098 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
13099 {
13100 	struct bnxt *bp = netdev_priv(dev);
13101 
13102 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
13103 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
13104 }
13105 
13106 static void bnxt_fw_health_check(struct bnxt *bp)
13107 {
13108 	struct bnxt_fw_health *fw_health = bp->fw_health;
13109 	struct pci_dev *pdev = bp->pdev;
13110 	u32 val;
13111 
13112 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13113 		return;
13114 
13115 	/* Make sure it is enabled before checking the tmr_counter. */
13116 	smp_rmb();
13117 	if (fw_health->tmr_counter) {
13118 		fw_health->tmr_counter--;
13119 		return;
13120 	}
13121 
13122 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13123 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
13124 		fw_health->arrests++;
13125 		goto fw_reset;
13126 	}
13127 
13128 	fw_health->last_fw_heartbeat = val;
13129 
13130 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13131 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
13132 		fw_health->discoveries++;
13133 		goto fw_reset;
13134 	}
13135 
13136 	fw_health->tmr_counter = fw_health->tmr_multiplier;
13137 	return;
13138 
13139 fw_reset:
13140 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
13141 }
13142 
13143 static void bnxt_timer(struct timer_list *t)
13144 {
13145 	struct bnxt *bp = from_timer(bp, t, timer);
13146 	struct net_device *dev = bp->dev;
13147 
13148 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
13149 		return;
13150 
13151 	if (atomic_read(&bp->intr_sem) != 0)
13152 		goto bnxt_restart_timer;
13153 
13154 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
13155 		bnxt_fw_health_check(bp);
13156 
13157 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
13158 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
13159 
13160 	if (bnxt_tc_flower_enabled(bp))
13161 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
13162 
13163 #ifdef CONFIG_RFS_ACCEL
13164 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
13165 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
13166 #endif /*CONFIG_RFS_ACCEL*/
13167 
13168 	if (bp->link_info.phy_retry) {
13169 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
13170 			bp->link_info.phy_retry = false;
13171 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
13172 		} else {
13173 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
13174 		}
13175 	}
13176 
13177 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13178 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13179 
13180 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
13181 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
13182 
13183 bnxt_restart_timer:
13184 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13185 }
13186 
13187 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
13188 {
13189 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
13190 	 * set.  If the device is being closed, bnxt_close() may be holding
13191 	 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear.  So we
13192 	 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
13193 	 */
13194 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13195 	rtnl_lock();
13196 }
13197 
13198 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
13199 {
13200 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13201 	rtnl_unlock();
13202 }
13203 
13204 /* Only called from bnxt_sp_task() */
13205 static void bnxt_reset(struct bnxt *bp, bool silent)
13206 {
13207 	bnxt_rtnl_lock_sp(bp);
13208 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
13209 		bnxt_reset_task(bp, silent);
13210 	bnxt_rtnl_unlock_sp(bp);
13211 }
13212 
13213 /* Only called from bnxt_sp_task() */
13214 static void bnxt_rx_ring_reset(struct bnxt *bp)
13215 {
13216 	int i;
13217 
13218 	bnxt_rtnl_lock_sp(bp);
13219 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13220 		bnxt_rtnl_unlock_sp(bp);
13221 		return;
13222 	}
13223 	/* Disable and flush TPA before resetting the RX ring */
13224 	if (bp->flags & BNXT_FLAG_TPA)
13225 		bnxt_set_tpa(bp, false);
13226 	for (i = 0; i < bp->rx_nr_rings; i++) {
13227 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
13228 		struct bnxt_cp_ring_info *cpr;
13229 		int rc;
13230 
13231 		if (!rxr->bnapi->in_reset)
13232 			continue;
13233 
13234 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
13235 		if (rc) {
13236 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
13237 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
13238 			else
13239 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
13240 					    rc);
13241 			bnxt_reset_task(bp, true);
13242 			break;
13243 		}
13244 		bnxt_free_one_rx_ring_skbs(bp, i);
13245 		rxr->rx_prod = 0;
13246 		rxr->rx_agg_prod = 0;
13247 		rxr->rx_sw_agg_prod = 0;
13248 		rxr->rx_next_cons = 0;
13249 		rxr->bnapi->in_reset = false;
13250 		bnxt_alloc_one_rx_ring(bp, i);
13251 		cpr = &rxr->bnapi->cp_ring;
13252 		cpr->sw_stats.rx.rx_resets++;
13253 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
13254 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
13255 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
13256 	}
13257 	if (bp->flags & BNXT_FLAG_TPA)
13258 		bnxt_set_tpa(bp, true);
13259 	bnxt_rtnl_unlock_sp(bp);
13260 }
13261 
13262 static void bnxt_fw_fatal_close(struct bnxt *bp)
13263 {
13264 	bnxt_tx_disable(bp);
13265 	bnxt_disable_napi(bp);
13266 	bnxt_disable_int_sync(bp);
13267 	bnxt_free_irq(bp);
13268 	bnxt_clear_int_mode(bp);
13269 	pci_disable_device(bp->pdev);
13270 }
13271 
13272 static void bnxt_fw_reset_close(struct bnxt *bp)
13273 {
13274 	bnxt_ulp_stop(bp);
13275 	/* When firmware is in fatal state, quiesce device and disable
13276 	 * bus master to prevent any potential bad DMAs before freeing
13277 	 * kernel memory.
13278 	 */
13279 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
13280 		u16 val = 0;
13281 
13282 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
13283 		if (val == 0xffff)
13284 			bp->fw_reset_min_dsecs = 0;
13285 		bnxt_fw_fatal_close(bp);
13286 	}
13287 	__bnxt_close_nic(bp, true, false);
13288 	bnxt_vf_reps_free(bp);
13289 	bnxt_clear_int_mode(bp);
13290 	bnxt_hwrm_func_drv_unrgtr(bp);
13291 	if (pci_is_enabled(bp->pdev))
13292 		pci_disable_device(bp->pdev);
13293 	bnxt_free_ctx_mem(bp);
13294 }
13295 
13296 static bool is_bnxt_fw_ok(struct bnxt *bp)
13297 {
13298 	struct bnxt_fw_health *fw_health = bp->fw_health;
13299 	bool no_heartbeat = false, has_reset = false;
13300 	u32 val;
13301 
13302 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13303 	if (val == fw_health->last_fw_heartbeat)
13304 		no_heartbeat = true;
13305 
13306 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13307 	if (val != fw_health->last_fw_reset_cnt)
13308 		has_reset = true;
13309 
13310 	if (!no_heartbeat && has_reset)
13311 		return true;
13312 
13313 	return false;
13314 }
13315 
13316 /* rtnl_lock is acquired before calling this function */
13317 static void bnxt_force_fw_reset(struct bnxt *bp)
13318 {
13319 	struct bnxt_fw_health *fw_health = bp->fw_health;
13320 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13321 	u32 wait_dsecs;
13322 
13323 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
13324 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13325 		return;
13326 
13327 	if (ptp) {
13328 		spin_lock_bh(&ptp->ptp_lock);
13329 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13330 		spin_unlock_bh(&ptp->ptp_lock);
13331 	} else {
13332 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13333 	}
13334 	bnxt_fw_reset_close(bp);
13335 	wait_dsecs = fw_health->master_func_wait_dsecs;
13336 	if (fw_health->primary) {
13337 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
13338 			wait_dsecs = 0;
13339 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
13340 	} else {
13341 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
13342 		wait_dsecs = fw_health->normal_func_wait_dsecs;
13343 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13344 	}
13345 
13346 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
13347 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
13348 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
13349 }
13350 
13351 void bnxt_fw_exception(struct bnxt *bp)
13352 {
13353 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
13354 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
13355 	bnxt_rtnl_lock_sp(bp);
13356 	bnxt_force_fw_reset(bp);
13357 	bnxt_rtnl_unlock_sp(bp);
13358 }
13359 
13360 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
13361  * < 0 on error.
13362  */
13363 static int bnxt_get_registered_vfs(struct bnxt *bp)
13364 {
13365 #ifdef CONFIG_BNXT_SRIOV
13366 	int rc;
13367 
13368 	if (!BNXT_PF(bp))
13369 		return 0;
13370 
13371 	rc = bnxt_hwrm_func_qcfg(bp);
13372 	if (rc) {
13373 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
13374 		return rc;
13375 	}
13376 	if (bp->pf.registered_vfs)
13377 		return bp->pf.registered_vfs;
13378 	if (bp->sriov_cfg)
13379 		return 1;
13380 #endif
13381 	return 0;
13382 }
13383 
13384 void bnxt_fw_reset(struct bnxt *bp)
13385 {
13386 	bnxt_rtnl_lock_sp(bp);
13387 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
13388 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13389 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13390 		int n = 0, tmo;
13391 
13392 		if (ptp) {
13393 			spin_lock_bh(&ptp->ptp_lock);
13394 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13395 			spin_unlock_bh(&ptp->ptp_lock);
13396 		} else {
13397 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13398 		}
13399 		if (bp->pf.active_vfs &&
13400 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
13401 			n = bnxt_get_registered_vfs(bp);
13402 		if (n < 0) {
13403 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
13404 				   n);
13405 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13406 			dev_close(bp->dev);
13407 			goto fw_reset_exit;
13408 		} else if (n > 0) {
13409 			u16 vf_tmo_dsecs = n * 10;
13410 
13411 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
13412 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
13413 			bp->fw_reset_state =
13414 				BNXT_FW_RESET_STATE_POLL_VF;
13415 			bnxt_queue_fw_reset_work(bp, HZ / 10);
13416 			goto fw_reset_exit;
13417 		}
13418 		bnxt_fw_reset_close(bp);
13419 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
13420 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
13421 			tmo = HZ / 10;
13422 		} else {
13423 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13424 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
13425 		}
13426 		bnxt_queue_fw_reset_work(bp, tmo);
13427 	}
13428 fw_reset_exit:
13429 	bnxt_rtnl_unlock_sp(bp);
13430 }
13431 
13432 static void bnxt_chk_missed_irq(struct bnxt *bp)
13433 {
13434 	int i;
13435 
13436 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13437 		return;
13438 
13439 	for (i = 0; i < bp->cp_nr_rings; i++) {
13440 		struct bnxt_napi *bnapi = bp->bnapi[i];
13441 		struct bnxt_cp_ring_info *cpr;
13442 		u32 fw_ring_id;
13443 		int j;
13444 
13445 		if (!bnapi)
13446 			continue;
13447 
13448 		cpr = &bnapi->cp_ring;
13449 		for (j = 0; j < cpr->cp_ring_count; j++) {
13450 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
13451 			u32 val[2];
13452 
13453 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
13454 				continue;
13455 
13456 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
13457 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
13458 				continue;
13459 			}
13460 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
13461 			bnxt_dbg_hwrm_ring_info_get(bp,
13462 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
13463 				fw_ring_id, &val[0], &val[1]);
13464 			cpr->sw_stats.cmn.missed_irqs++;
13465 		}
13466 	}
13467 }
13468 
13469 static void bnxt_cfg_ntp_filters(struct bnxt *);
13470 
13471 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
13472 {
13473 	struct bnxt_link_info *link_info = &bp->link_info;
13474 
13475 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
13476 		link_info->autoneg = BNXT_AUTONEG_SPEED;
13477 		if (bp->hwrm_spec_code >= 0x10201) {
13478 			if (link_info->auto_pause_setting &
13479 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
13480 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13481 		} else {
13482 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13483 		}
13484 		bnxt_set_auto_speed(link_info);
13485 	} else {
13486 		bnxt_set_force_speed(link_info);
13487 		link_info->req_duplex = link_info->duplex_setting;
13488 	}
13489 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
13490 		link_info->req_flow_ctrl =
13491 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
13492 	else
13493 		link_info->req_flow_ctrl = link_info->force_pause_setting;
13494 }
13495 
13496 static void bnxt_fw_echo_reply(struct bnxt *bp)
13497 {
13498 	struct bnxt_fw_health *fw_health = bp->fw_health;
13499 	struct hwrm_func_echo_response_input *req;
13500 	int rc;
13501 
13502 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
13503 	if (rc)
13504 		return;
13505 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
13506 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
13507 	hwrm_req_send(bp, req);
13508 }
13509 
13510 static void bnxt_sp_task(struct work_struct *work)
13511 {
13512 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
13513 
13514 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13515 	smp_mb__after_atomic();
13516 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13517 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13518 		return;
13519 	}
13520 
13521 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
13522 		bnxt_cfg_rx_mode(bp);
13523 
13524 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
13525 		bnxt_cfg_ntp_filters(bp);
13526 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
13527 		bnxt_hwrm_exec_fwd_req(bp);
13528 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
13529 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
13530 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
13531 		bnxt_hwrm_port_qstats(bp, 0);
13532 		bnxt_hwrm_port_qstats_ext(bp, 0);
13533 		bnxt_accumulate_all_stats(bp);
13534 	}
13535 
13536 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
13537 		int rc;
13538 
13539 		mutex_lock(&bp->link_lock);
13540 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
13541 				       &bp->sp_event))
13542 			bnxt_hwrm_phy_qcaps(bp);
13543 
13544 		rc = bnxt_update_link(bp, true);
13545 		if (rc)
13546 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
13547 				   rc);
13548 
13549 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
13550 				       &bp->sp_event))
13551 			bnxt_init_ethtool_link_settings(bp);
13552 		mutex_unlock(&bp->link_lock);
13553 	}
13554 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
13555 		int rc;
13556 
13557 		mutex_lock(&bp->link_lock);
13558 		rc = bnxt_update_phy_setting(bp);
13559 		mutex_unlock(&bp->link_lock);
13560 		if (rc) {
13561 			netdev_warn(bp->dev, "update phy settings retry failed\n");
13562 		} else {
13563 			bp->link_info.phy_retry = false;
13564 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
13565 		}
13566 	}
13567 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
13568 		mutex_lock(&bp->link_lock);
13569 		bnxt_get_port_module_status(bp);
13570 		mutex_unlock(&bp->link_lock);
13571 	}
13572 
13573 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
13574 		bnxt_tc_flow_stats_work(bp);
13575 
13576 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
13577 		bnxt_chk_missed_irq(bp);
13578 
13579 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
13580 		bnxt_fw_echo_reply(bp);
13581 
13582 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
13583 		bnxt_hwmon_notify_event(bp);
13584 
13585 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
13586 	 * must be the last functions to be called before exiting.
13587 	 */
13588 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
13589 		bnxt_reset(bp, false);
13590 
13591 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
13592 		bnxt_reset(bp, true);
13593 
13594 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
13595 		bnxt_rx_ring_reset(bp);
13596 
13597 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
13598 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
13599 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
13600 			bnxt_devlink_health_fw_report(bp);
13601 		else
13602 			bnxt_fw_reset(bp);
13603 	}
13604 
13605 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
13606 		if (!is_bnxt_fw_ok(bp))
13607 			bnxt_devlink_health_fw_report(bp);
13608 	}
13609 
13610 	smp_mb__before_atomic();
13611 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13612 }
13613 
13614 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
13615 				int *max_cp);
13616 
13617 /* Under rtnl_lock */
13618 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
13619 		     int tx_xdp)
13620 {
13621 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
13622 	struct bnxt_hw_rings hwr = {0};
13623 	int rx_rings = rx;
13624 
13625 	if (tcs)
13626 		tx_sets = tcs;
13627 
13628 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
13629 
13630 	if (max_rx < rx_rings)
13631 		return -ENOMEM;
13632 
13633 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
13634 		rx_rings <<= 1;
13635 
13636 	hwr.rx = rx_rings;
13637 	hwr.tx = tx * tx_sets + tx_xdp;
13638 	if (max_tx < hwr.tx)
13639 		return -ENOMEM;
13640 
13641 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
13642 
13643 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
13644 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
13645 	if (max_cp < hwr.cp)
13646 		return -ENOMEM;
13647 	hwr.stat = hwr.cp;
13648 	if (BNXT_NEW_RM(bp)) {
13649 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
13650 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
13651 		hwr.grp = rx;
13652 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13653 	}
13654 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
13655 		hwr.cp_p5 = hwr.tx + rx;
13656 	return bnxt_hwrm_check_rings(bp, &hwr);
13657 }
13658 
13659 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
13660 {
13661 	if (bp->bar2) {
13662 		pci_iounmap(pdev, bp->bar2);
13663 		bp->bar2 = NULL;
13664 	}
13665 
13666 	if (bp->bar1) {
13667 		pci_iounmap(pdev, bp->bar1);
13668 		bp->bar1 = NULL;
13669 	}
13670 
13671 	if (bp->bar0) {
13672 		pci_iounmap(pdev, bp->bar0);
13673 		bp->bar0 = NULL;
13674 	}
13675 }
13676 
13677 static void bnxt_cleanup_pci(struct bnxt *bp)
13678 {
13679 	bnxt_unmap_bars(bp, bp->pdev);
13680 	pci_release_regions(bp->pdev);
13681 	if (pci_is_enabled(bp->pdev))
13682 		pci_disable_device(bp->pdev);
13683 }
13684 
13685 static void bnxt_init_dflt_coal(struct bnxt *bp)
13686 {
13687 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
13688 	struct bnxt_coal *coal;
13689 	u16 flags = 0;
13690 
13691 	if (coal_cap->cmpl_params &
13692 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
13693 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
13694 
13695 	/* Tick values in micro seconds.
13696 	 * 1 coal_buf x bufs_per_record = 1 completion record.
13697 	 */
13698 	coal = &bp->rx_coal;
13699 	coal->coal_ticks = 10;
13700 	coal->coal_bufs = 30;
13701 	coal->coal_ticks_irq = 1;
13702 	coal->coal_bufs_irq = 2;
13703 	coal->idle_thresh = 50;
13704 	coal->bufs_per_record = 2;
13705 	coal->budget = 64;		/* NAPI budget */
13706 	coal->flags = flags;
13707 
13708 	coal = &bp->tx_coal;
13709 	coal->coal_ticks = 28;
13710 	coal->coal_bufs = 30;
13711 	coal->coal_ticks_irq = 2;
13712 	coal->coal_bufs_irq = 2;
13713 	coal->bufs_per_record = 1;
13714 	coal->flags = flags;
13715 
13716 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
13717 }
13718 
13719 /* FW that pre-reserves 1 VNIC per function */
13720 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
13721 {
13722 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
13723 
13724 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13725 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
13726 		return true;
13727 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13728 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
13729 		return true;
13730 	return false;
13731 }
13732 
13733 static int bnxt_fw_init_one_p1(struct bnxt *bp)
13734 {
13735 	int rc;
13736 
13737 	bp->fw_cap = 0;
13738 	rc = bnxt_hwrm_ver_get(bp);
13739 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
13740 	 * so wait before continuing with recovery.
13741 	 */
13742 	if (rc)
13743 		msleep(100);
13744 	bnxt_try_map_fw_health_reg(bp);
13745 	if (rc) {
13746 		rc = bnxt_try_recover_fw(bp);
13747 		if (rc)
13748 			return rc;
13749 		rc = bnxt_hwrm_ver_get(bp);
13750 		if (rc)
13751 			return rc;
13752 	}
13753 
13754 	bnxt_nvm_cfg_ver_get(bp);
13755 
13756 	rc = bnxt_hwrm_func_reset(bp);
13757 	if (rc)
13758 		return -ENODEV;
13759 
13760 	bnxt_hwrm_fw_set_time(bp);
13761 	return 0;
13762 }
13763 
13764 static int bnxt_fw_init_one_p2(struct bnxt *bp)
13765 {
13766 	int rc;
13767 
13768 	/* Get the MAX capabilities for this function */
13769 	rc = bnxt_hwrm_func_qcaps(bp);
13770 	if (rc) {
13771 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
13772 			   rc);
13773 		return -ENODEV;
13774 	}
13775 
13776 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
13777 	if (rc)
13778 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
13779 			    rc);
13780 
13781 	if (bnxt_alloc_fw_health(bp)) {
13782 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
13783 	} else {
13784 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
13785 		if (rc)
13786 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
13787 				    rc);
13788 	}
13789 
13790 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
13791 	if (rc)
13792 		return -ENODEV;
13793 
13794 	if (bnxt_fw_pre_resv_vnics(bp))
13795 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
13796 
13797 	bnxt_hwrm_func_qcfg(bp);
13798 	bnxt_hwrm_vnic_qcaps(bp);
13799 	bnxt_hwrm_port_led_qcaps(bp);
13800 	bnxt_ethtool_init(bp);
13801 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
13802 		__bnxt_hwrm_ptp_qcfg(bp);
13803 	bnxt_dcb_init(bp);
13804 	bnxt_hwmon_init(bp);
13805 	return 0;
13806 }
13807 
13808 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
13809 {
13810 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
13811 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
13812 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
13813 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
13814 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
13815 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
13816 		bp->rss_hash_delta = bp->rss_hash_cfg;
13817 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
13818 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
13819 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
13820 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
13821 	}
13822 }
13823 
13824 static void bnxt_set_dflt_rfs(struct bnxt *bp)
13825 {
13826 	struct net_device *dev = bp->dev;
13827 
13828 	dev->hw_features &= ~NETIF_F_NTUPLE;
13829 	dev->features &= ~NETIF_F_NTUPLE;
13830 	bp->flags &= ~BNXT_FLAG_RFS;
13831 	if (bnxt_rfs_supported(bp)) {
13832 		dev->hw_features |= NETIF_F_NTUPLE;
13833 		if (bnxt_rfs_capable(bp, false)) {
13834 			bp->flags |= BNXT_FLAG_RFS;
13835 			dev->features |= NETIF_F_NTUPLE;
13836 		}
13837 	}
13838 }
13839 
13840 static void bnxt_fw_init_one_p3(struct bnxt *bp)
13841 {
13842 	struct pci_dev *pdev = bp->pdev;
13843 
13844 	bnxt_set_dflt_rss_hash_type(bp);
13845 	bnxt_set_dflt_rfs(bp);
13846 
13847 	bnxt_get_wol_settings(bp);
13848 	if (bp->flags & BNXT_FLAG_WOL_CAP)
13849 		device_set_wakeup_enable(&pdev->dev, bp->wol);
13850 	else
13851 		device_set_wakeup_capable(&pdev->dev, false);
13852 
13853 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
13854 	bnxt_hwrm_coal_params_qcaps(bp);
13855 }
13856 
13857 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
13858 
13859 int bnxt_fw_init_one(struct bnxt *bp)
13860 {
13861 	int rc;
13862 
13863 	rc = bnxt_fw_init_one_p1(bp);
13864 	if (rc) {
13865 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
13866 		return rc;
13867 	}
13868 	rc = bnxt_fw_init_one_p2(bp);
13869 	if (rc) {
13870 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
13871 		return rc;
13872 	}
13873 	rc = bnxt_probe_phy(bp, false);
13874 	if (rc)
13875 		return rc;
13876 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
13877 	if (rc)
13878 		return rc;
13879 
13880 	bnxt_fw_init_one_p3(bp);
13881 	return 0;
13882 }
13883 
13884 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
13885 {
13886 	struct bnxt_fw_health *fw_health = bp->fw_health;
13887 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
13888 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
13889 	u32 reg_type, reg_off, delay_msecs;
13890 
13891 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
13892 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
13893 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
13894 	switch (reg_type) {
13895 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
13896 		pci_write_config_dword(bp->pdev, reg_off, val);
13897 		break;
13898 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
13899 		writel(reg_off & BNXT_GRC_BASE_MASK,
13900 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
13901 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
13902 		fallthrough;
13903 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
13904 		writel(val, bp->bar0 + reg_off);
13905 		break;
13906 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
13907 		writel(val, bp->bar1 + reg_off);
13908 		break;
13909 	}
13910 	if (delay_msecs) {
13911 		pci_read_config_dword(bp->pdev, 0, &val);
13912 		msleep(delay_msecs);
13913 	}
13914 }
13915 
13916 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
13917 {
13918 	struct hwrm_func_qcfg_output *resp;
13919 	struct hwrm_func_qcfg_input *req;
13920 	bool result = true; /* firmware will enforce if unknown */
13921 
13922 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
13923 		return result;
13924 
13925 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
13926 		return result;
13927 
13928 	req->fid = cpu_to_le16(0xffff);
13929 	resp = hwrm_req_hold(bp, req);
13930 	if (!hwrm_req_send(bp, req))
13931 		result = !!(le16_to_cpu(resp->flags) &
13932 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
13933 	hwrm_req_drop(bp, req);
13934 	return result;
13935 }
13936 
13937 static void bnxt_reset_all(struct bnxt *bp)
13938 {
13939 	struct bnxt_fw_health *fw_health = bp->fw_health;
13940 	int i, rc;
13941 
13942 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
13943 		bnxt_fw_reset_via_optee(bp);
13944 		bp->fw_reset_timestamp = jiffies;
13945 		return;
13946 	}
13947 
13948 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
13949 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
13950 			bnxt_fw_reset_writel(bp, i);
13951 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
13952 		struct hwrm_fw_reset_input *req;
13953 
13954 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
13955 		if (!rc) {
13956 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
13957 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
13958 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
13959 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
13960 			rc = hwrm_req_send(bp, req);
13961 		}
13962 		if (rc != -ENODEV)
13963 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
13964 	}
13965 	bp->fw_reset_timestamp = jiffies;
13966 }
13967 
13968 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
13969 {
13970 	return time_after(jiffies, bp->fw_reset_timestamp +
13971 			  (bp->fw_reset_max_dsecs * HZ / 10));
13972 }
13973 
13974 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
13975 {
13976 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13977 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF) {
13978 		bnxt_ulp_start(bp, rc);
13979 		bnxt_dl_health_fw_status_update(bp, false);
13980 	}
13981 	bp->fw_reset_state = 0;
13982 	dev_close(bp->dev);
13983 }
13984 
13985 static void bnxt_fw_reset_task(struct work_struct *work)
13986 {
13987 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
13988 	int rc = 0;
13989 
13990 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13991 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
13992 		return;
13993 	}
13994 
13995 	switch (bp->fw_reset_state) {
13996 	case BNXT_FW_RESET_STATE_POLL_VF: {
13997 		int n = bnxt_get_registered_vfs(bp);
13998 		int tmo;
13999 
14000 		if (n < 0) {
14001 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
14002 				   n, jiffies_to_msecs(jiffies -
14003 				   bp->fw_reset_timestamp));
14004 			goto fw_reset_abort;
14005 		} else if (n > 0) {
14006 			if (bnxt_fw_reset_timeout(bp)) {
14007 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14008 				bp->fw_reset_state = 0;
14009 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
14010 					   n);
14011 				return;
14012 			}
14013 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14014 			return;
14015 		}
14016 		bp->fw_reset_timestamp = jiffies;
14017 		rtnl_lock();
14018 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
14019 			bnxt_fw_reset_abort(bp, rc);
14020 			rtnl_unlock();
14021 			return;
14022 		}
14023 		bnxt_fw_reset_close(bp);
14024 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14025 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14026 			tmo = HZ / 10;
14027 		} else {
14028 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14029 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14030 		}
14031 		rtnl_unlock();
14032 		bnxt_queue_fw_reset_work(bp, tmo);
14033 		return;
14034 	}
14035 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
14036 		u32 val;
14037 
14038 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14039 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
14040 		    !bnxt_fw_reset_timeout(bp)) {
14041 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14042 			return;
14043 		}
14044 
14045 		if (!bp->fw_health->primary) {
14046 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
14047 
14048 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14049 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14050 			return;
14051 		}
14052 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14053 	}
14054 		fallthrough;
14055 	case BNXT_FW_RESET_STATE_RESET_FW:
14056 		bnxt_reset_all(bp);
14057 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14058 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
14059 		return;
14060 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
14061 		bnxt_inv_fw_health_reg(bp);
14062 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
14063 		    !bp->fw_reset_min_dsecs) {
14064 			u16 val;
14065 
14066 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14067 			if (val == 0xffff) {
14068 				if (bnxt_fw_reset_timeout(bp)) {
14069 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
14070 					rc = -ETIMEDOUT;
14071 					goto fw_reset_abort;
14072 				}
14073 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
14074 				return;
14075 			}
14076 		}
14077 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14078 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
14079 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
14080 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
14081 			bnxt_dl_remote_reload(bp);
14082 		if (pci_enable_device(bp->pdev)) {
14083 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
14084 			rc = -ENODEV;
14085 			goto fw_reset_abort;
14086 		}
14087 		pci_set_master(bp->pdev);
14088 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
14089 		fallthrough;
14090 	case BNXT_FW_RESET_STATE_POLL_FW:
14091 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
14092 		rc = bnxt_hwrm_poll(bp);
14093 		if (rc) {
14094 			if (bnxt_fw_reset_timeout(bp)) {
14095 				netdev_err(bp->dev, "Firmware reset aborted\n");
14096 				goto fw_reset_abort_status;
14097 			}
14098 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14099 			return;
14100 		}
14101 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
14102 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
14103 		fallthrough;
14104 	case BNXT_FW_RESET_STATE_OPENING:
14105 		while (!rtnl_trylock()) {
14106 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14107 			return;
14108 		}
14109 		rc = bnxt_open(bp->dev);
14110 		if (rc) {
14111 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
14112 			bnxt_fw_reset_abort(bp, rc);
14113 			rtnl_unlock();
14114 			return;
14115 		}
14116 
14117 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
14118 		    bp->fw_health->enabled) {
14119 			bp->fw_health->last_fw_reset_cnt =
14120 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14121 		}
14122 		bp->fw_reset_state = 0;
14123 		/* Make sure fw_reset_state is 0 before clearing the flag */
14124 		smp_mb__before_atomic();
14125 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14126 		bnxt_ulp_start(bp, 0);
14127 		bnxt_reenable_sriov(bp);
14128 		bnxt_vf_reps_alloc(bp);
14129 		bnxt_vf_reps_open(bp);
14130 		bnxt_ptp_reapply_pps(bp);
14131 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
14132 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
14133 			bnxt_dl_health_fw_recovery_done(bp);
14134 			bnxt_dl_health_fw_status_update(bp, true);
14135 		}
14136 		rtnl_unlock();
14137 		break;
14138 	}
14139 	return;
14140 
14141 fw_reset_abort_status:
14142 	if (bp->fw_health->status_reliable ||
14143 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
14144 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14145 
14146 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
14147 	}
14148 fw_reset_abort:
14149 	rtnl_lock();
14150 	bnxt_fw_reset_abort(bp, rc);
14151 	rtnl_unlock();
14152 }
14153 
14154 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
14155 {
14156 	int rc;
14157 	struct bnxt *bp = netdev_priv(dev);
14158 
14159 	SET_NETDEV_DEV(dev, &pdev->dev);
14160 
14161 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
14162 	rc = pci_enable_device(pdev);
14163 	if (rc) {
14164 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
14165 		goto init_err;
14166 	}
14167 
14168 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
14169 		dev_err(&pdev->dev,
14170 			"Cannot find PCI device base address, aborting\n");
14171 		rc = -ENODEV;
14172 		goto init_err_disable;
14173 	}
14174 
14175 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
14176 	if (rc) {
14177 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
14178 		goto init_err_disable;
14179 	}
14180 
14181 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
14182 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
14183 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
14184 		rc = -EIO;
14185 		goto init_err_release;
14186 	}
14187 
14188 	pci_set_master(pdev);
14189 
14190 	bp->dev = dev;
14191 	bp->pdev = pdev;
14192 
14193 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
14194 	 * determines the BAR size.
14195 	 */
14196 	bp->bar0 = pci_ioremap_bar(pdev, 0);
14197 	if (!bp->bar0) {
14198 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
14199 		rc = -ENOMEM;
14200 		goto init_err_release;
14201 	}
14202 
14203 	bp->bar2 = pci_ioremap_bar(pdev, 4);
14204 	if (!bp->bar2) {
14205 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
14206 		rc = -ENOMEM;
14207 		goto init_err_release;
14208 	}
14209 
14210 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
14211 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
14212 
14213 	spin_lock_init(&bp->ntp_fltr_lock);
14214 #if BITS_PER_LONG == 32
14215 	spin_lock_init(&bp->db_lock);
14216 #endif
14217 
14218 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
14219 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
14220 
14221 	timer_setup(&bp->timer, bnxt_timer, 0);
14222 	bp->current_interval = BNXT_TIMER_INTERVAL;
14223 
14224 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
14225 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
14226 
14227 	clear_bit(BNXT_STATE_OPEN, &bp->state);
14228 	return 0;
14229 
14230 init_err_release:
14231 	bnxt_unmap_bars(bp, pdev);
14232 	pci_release_regions(pdev);
14233 
14234 init_err_disable:
14235 	pci_disable_device(pdev);
14236 
14237 init_err:
14238 	return rc;
14239 }
14240 
14241 /* rtnl_lock held */
14242 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
14243 {
14244 	struct sockaddr *addr = p;
14245 	struct bnxt *bp = netdev_priv(dev);
14246 	int rc = 0;
14247 
14248 	if (!is_valid_ether_addr(addr->sa_data))
14249 		return -EADDRNOTAVAIL;
14250 
14251 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
14252 		return 0;
14253 
14254 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
14255 	if (rc)
14256 		return rc;
14257 
14258 	eth_hw_addr_set(dev, addr->sa_data);
14259 	bnxt_clear_usr_fltrs(bp, true);
14260 	if (netif_running(dev)) {
14261 		bnxt_close_nic(bp, false, false);
14262 		rc = bnxt_open_nic(bp, false, false);
14263 	}
14264 
14265 	return rc;
14266 }
14267 
14268 /* rtnl_lock held */
14269 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
14270 {
14271 	struct bnxt *bp = netdev_priv(dev);
14272 
14273 	if (netif_running(dev))
14274 		bnxt_close_nic(bp, true, false);
14275 
14276 	dev->mtu = new_mtu;
14277 	bnxt_set_ring_params(bp);
14278 
14279 	if (netif_running(dev))
14280 		return bnxt_open_nic(bp, true, false);
14281 
14282 	return 0;
14283 }
14284 
14285 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
14286 {
14287 	struct bnxt *bp = netdev_priv(dev);
14288 	bool sh = false;
14289 	int rc, tx_cp;
14290 
14291 	if (tc > bp->max_tc) {
14292 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
14293 			   tc, bp->max_tc);
14294 		return -EINVAL;
14295 	}
14296 
14297 	if (bp->num_tc == tc)
14298 		return 0;
14299 
14300 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
14301 		sh = true;
14302 
14303 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
14304 			      sh, tc, bp->tx_nr_rings_xdp);
14305 	if (rc)
14306 		return rc;
14307 
14308 	/* Needs to close the device and do hw resource re-allocations */
14309 	if (netif_running(bp->dev))
14310 		bnxt_close_nic(bp, true, false);
14311 
14312 	if (tc) {
14313 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
14314 		netdev_set_num_tc(dev, tc);
14315 		bp->num_tc = tc;
14316 	} else {
14317 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
14318 		netdev_reset_tc(dev);
14319 		bp->num_tc = 0;
14320 	}
14321 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
14322 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
14323 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
14324 			       tx_cp + bp->rx_nr_rings;
14325 
14326 	if (netif_running(bp->dev))
14327 		return bnxt_open_nic(bp, true, false);
14328 
14329 	return 0;
14330 }
14331 
14332 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
14333 				  void *cb_priv)
14334 {
14335 	struct bnxt *bp = cb_priv;
14336 
14337 	if (!bnxt_tc_flower_enabled(bp) ||
14338 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
14339 		return -EOPNOTSUPP;
14340 
14341 	switch (type) {
14342 	case TC_SETUP_CLSFLOWER:
14343 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
14344 	default:
14345 		return -EOPNOTSUPP;
14346 	}
14347 }
14348 
14349 LIST_HEAD(bnxt_block_cb_list);
14350 
14351 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
14352 			 void *type_data)
14353 {
14354 	struct bnxt *bp = netdev_priv(dev);
14355 
14356 	switch (type) {
14357 	case TC_SETUP_BLOCK:
14358 		return flow_block_cb_setup_simple(type_data,
14359 						  &bnxt_block_cb_list,
14360 						  bnxt_setup_tc_block_cb,
14361 						  bp, bp, true);
14362 	case TC_SETUP_QDISC_MQPRIO: {
14363 		struct tc_mqprio_qopt *mqprio = type_data;
14364 
14365 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
14366 
14367 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
14368 	}
14369 	default:
14370 		return -EOPNOTSUPP;
14371 	}
14372 }
14373 
14374 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
14375 			    const struct sk_buff *skb)
14376 {
14377 	struct bnxt_vnic_info *vnic;
14378 
14379 	if (skb)
14380 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
14381 
14382 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
14383 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
14384 }
14385 
14386 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
14387 			   u32 idx)
14388 {
14389 	struct hlist_head *head;
14390 	int bit_id;
14391 
14392 	spin_lock_bh(&bp->ntp_fltr_lock);
14393 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
14394 	if (bit_id < 0) {
14395 		spin_unlock_bh(&bp->ntp_fltr_lock);
14396 		return -ENOMEM;
14397 	}
14398 
14399 	fltr->base.sw_id = (u16)bit_id;
14400 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
14401 	fltr->base.flags |= BNXT_ACT_RING_DST;
14402 	head = &bp->ntp_fltr_hash_tbl[idx];
14403 	hlist_add_head_rcu(&fltr->base.hash, head);
14404 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
14405 	bnxt_insert_usr_fltr(bp, &fltr->base);
14406 	bp->ntp_fltr_count++;
14407 	spin_unlock_bh(&bp->ntp_fltr_lock);
14408 	return 0;
14409 }
14410 
14411 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
14412 			    struct bnxt_ntuple_filter *f2)
14413 {
14414 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
14415 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
14416 	struct flow_keys *keys1 = &f1->fkeys;
14417 	struct flow_keys *keys2 = &f2->fkeys;
14418 
14419 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
14420 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
14421 		return false;
14422 
14423 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
14424 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
14425 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
14426 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
14427 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
14428 			return false;
14429 	} else {
14430 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
14431 				     &keys2->addrs.v6addrs.src) ||
14432 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
14433 				     &masks2->addrs.v6addrs.src) ||
14434 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
14435 				     &keys2->addrs.v6addrs.dst) ||
14436 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
14437 				     &masks2->addrs.v6addrs.dst))
14438 			return false;
14439 	}
14440 
14441 	return keys1->ports.src == keys2->ports.src &&
14442 	       masks1->ports.src == masks2->ports.src &&
14443 	       keys1->ports.dst == keys2->ports.dst &&
14444 	       masks1->ports.dst == masks2->ports.dst &&
14445 	       keys1->control.flags == keys2->control.flags &&
14446 	       f1->l2_fltr == f2->l2_fltr;
14447 }
14448 
14449 struct bnxt_ntuple_filter *
14450 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
14451 				struct bnxt_ntuple_filter *fltr, u32 idx)
14452 {
14453 	struct bnxt_ntuple_filter *f;
14454 	struct hlist_head *head;
14455 
14456 	head = &bp->ntp_fltr_hash_tbl[idx];
14457 	hlist_for_each_entry_rcu(f, head, base.hash) {
14458 		if (bnxt_fltr_match(f, fltr))
14459 			return f;
14460 	}
14461 	return NULL;
14462 }
14463 
14464 #ifdef CONFIG_RFS_ACCEL
14465 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
14466 			      u16 rxq_index, u32 flow_id)
14467 {
14468 	struct bnxt *bp = netdev_priv(dev);
14469 	struct bnxt_ntuple_filter *fltr, *new_fltr;
14470 	struct flow_keys *fkeys;
14471 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
14472 	struct bnxt_l2_filter *l2_fltr;
14473 	int rc = 0, idx;
14474 	u32 flags;
14475 
14476 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
14477 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
14478 		atomic_inc(&l2_fltr->refcnt);
14479 	} else {
14480 		struct bnxt_l2_key key;
14481 
14482 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
14483 		key.vlan = 0;
14484 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
14485 		if (!l2_fltr)
14486 			return -EINVAL;
14487 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
14488 			bnxt_del_l2_filter(bp, l2_fltr);
14489 			return -EINVAL;
14490 		}
14491 	}
14492 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
14493 	if (!new_fltr) {
14494 		bnxt_del_l2_filter(bp, l2_fltr);
14495 		return -ENOMEM;
14496 	}
14497 
14498 	fkeys = &new_fltr->fkeys;
14499 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
14500 		rc = -EPROTONOSUPPORT;
14501 		goto err_free;
14502 	}
14503 
14504 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
14505 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
14506 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
14507 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
14508 		rc = -EPROTONOSUPPORT;
14509 		goto err_free;
14510 	}
14511 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
14512 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
14513 		if (bp->hwrm_spec_code < 0x10601) {
14514 			rc = -EPROTONOSUPPORT;
14515 			goto err_free;
14516 		}
14517 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
14518 	}
14519 	flags = fkeys->control.flags;
14520 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
14521 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
14522 		rc = -EPROTONOSUPPORT;
14523 		goto err_free;
14524 	}
14525 	new_fltr->l2_fltr = l2_fltr;
14526 
14527 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
14528 	rcu_read_lock();
14529 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
14530 	if (fltr) {
14531 		rc = fltr->base.sw_id;
14532 		rcu_read_unlock();
14533 		goto err_free;
14534 	}
14535 	rcu_read_unlock();
14536 
14537 	new_fltr->flow_id = flow_id;
14538 	new_fltr->base.rxq = rxq_index;
14539 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
14540 	if (!rc) {
14541 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14542 		return new_fltr->base.sw_id;
14543 	}
14544 
14545 err_free:
14546 	bnxt_del_l2_filter(bp, l2_fltr);
14547 	kfree(new_fltr);
14548 	return rc;
14549 }
14550 #endif
14551 
14552 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
14553 {
14554 	spin_lock_bh(&bp->ntp_fltr_lock);
14555 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
14556 		spin_unlock_bh(&bp->ntp_fltr_lock);
14557 		return;
14558 	}
14559 	hlist_del_rcu(&fltr->base.hash);
14560 	bnxt_del_one_usr_fltr(bp, &fltr->base);
14561 	bp->ntp_fltr_count--;
14562 	spin_unlock_bh(&bp->ntp_fltr_lock);
14563 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
14564 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
14565 	kfree_rcu(fltr, base.rcu);
14566 }
14567 
14568 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
14569 {
14570 #ifdef CONFIG_RFS_ACCEL
14571 	int i;
14572 
14573 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
14574 		struct hlist_head *head;
14575 		struct hlist_node *tmp;
14576 		struct bnxt_ntuple_filter *fltr;
14577 		int rc;
14578 
14579 		head = &bp->ntp_fltr_hash_tbl[i];
14580 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
14581 			bool del = false;
14582 
14583 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
14584 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
14585 					continue;
14586 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
14587 							fltr->flow_id,
14588 							fltr->base.sw_id)) {
14589 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
14590 									 fltr);
14591 					del = true;
14592 				}
14593 			} else {
14594 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
14595 								       fltr);
14596 				if (rc)
14597 					del = true;
14598 				else
14599 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
14600 			}
14601 
14602 			if (del)
14603 				bnxt_del_ntp_filter(bp, fltr);
14604 		}
14605 	}
14606 #endif
14607 }
14608 
14609 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
14610 				    unsigned int entry, struct udp_tunnel_info *ti)
14611 {
14612 	struct bnxt *bp = netdev_priv(netdev);
14613 	unsigned int cmd;
14614 
14615 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
14616 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
14617 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
14618 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
14619 	else
14620 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
14621 
14622 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
14623 }
14624 
14625 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
14626 				      unsigned int entry, struct udp_tunnel_info *ti)
14627 {
14628 	struct bnxt *bp = netdev_priv(netdev);
14629 	unsigned int cmd;
14630 
14631 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
14632 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
14633 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
14634 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
14635 	else
14636 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
14637 
14638 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
14639 }
14640 
14641 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
14642 	.set_port	= bnxt_udp_tunnel_set_port,
14643 	.unset_port	= bnxt_udp_tunnel_unset_port,
14644 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
14645 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
14646 	.tables		= {
14647 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
14648 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
14649 	},
14650 }, bnxt_udp_tunnels_p7 = {
14651 	.set_port	= bnxt_udp_tunnel_set_port,
14652 	.unset_port	= bnxt_udp_tunnel_unset_port,
14653 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
14654 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
14655 	.tables		= {
14656 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
14657 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
14658 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
14659 	},
14660 };
14661 
14662 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
14663 			       struct net_device *dev, u32 filter_mask,
14664 			       int nlflags)
14665 {
14666 	struct bnxt *bp = netdev_priv(dev);
14667 
14668 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
14669 				       nlflags, filter_mask, NULL);
14670 }
14671 
14672 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
14673 			       u16 flags, struct netlink_ext_ack *extack)
14674 {
14675 	struct bnxt *bp = netdev_priv(dev);
14676 	struct nlattr *attr, *br_spec;
14677 	int rem, rc = 0;
14678 
14679 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
14680 		return -EOPNOTSUPP;
14681 
14682 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
14683 	if (!br_spec)
14684 		return -EINVAL;
14685 
14686 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
14687 		u16 mode;
14688 
14689 		mode = nla_get_u16(attr);
14690 		if (mode == bp->br_mode)
14691 			break;
14692 
14693 		rc = bnxt_hwrm_set_br_mode(bp, mode);
14694 		if (!rc)
14695 			bp->br_mode = mode;
14696 		break;
14697 	}
14698 	return rc;
14699 }
14700 
14701 int bnxt_get_port_parent_id(struct net_device *dev,
14702 			    struct netdev_phys_item_id *ppid)
14703 {
14704 	struct bnxt *bp = netdev_priv(dev);
14705 
14706 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
14707 		return -EOPNOTSUPP;
14708 
14709 	/* The PF and it's VF-reps only support the switchdev framework */
14710 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
14711 		return -EOPNOTSUPP;
14712 
14713 	ppid->id_len = sizeof(bp->dsn);
14714 	memcpy(ppid->id, bp->dsn, ppid->id_len);
14715 
14716 	return 0;
14717 }
14718 
14719 static const struct net_device_ops bnxt_netdev_ops = {
14720 	.ndo_open		= bnxt_open,
14721 	.ndo_start_xmit		= bnxt_start_xmit,
14722 	.ndo_stop		= bnxt_close,
14723 	.ndo_get_stats64	= bnxt_get_stats64,
14724 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
14725 	.ndo_eth_ioctl		= bnxt_ioctl,
14726 	.ndo_validate_addr	= eth_validate_addr,
14727 	.ndo_set_mac_address	= bnxt_change_mac_addr,
14728 	.ndo_change_mtu		= bnxt_change_mtu,
14729 	.ndo_fix_features	= bnxt_fix_features,
14730 	.ndo_set_features	= bnxt_set_features,
14731 	.ndo_features_check	= bnxt_features_check,
14732 	.ndo_tx_timeout		= bnxt_tx_timeout,
14733 #ifdef CONFIG_BNXT_SRIOV
14734 	.ndo_get_vf_config	= bnxt_get_vf_config,
14735 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
14736 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
14737 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
14738 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
14739 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
14740 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
14741 #endif
14742 	.ndo_setup_tc           = bnxt_setup_tc,
14743 #ifdef CONFIG_RFS_ACCEL
14744 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
14745 #endif
14746 	.ndo_bpf		= bnxt_xdp,
14747 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
14748 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
14749 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
14750 };
14751 
14752 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
14753 				    struct netdev_queue_stats_rx *stats)
14754 {
14755 	struct bnxt *bp = netdev_priv(dev);
14756 	struct bnxt_cp_ring_info *cpr;
14757 	u64 *sw;
14758 
14759 	cpr = &bp->bnapi[i]->cp_ring;
14760 	sw = cpr->stats.sw_stats;
14761 
14762 	stats->packets = 0;
14763 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
14764 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
14765 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
14766 
14767 	stats->bytes = 0;
14768 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
14769 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
14770 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
14771 
14772 	stats->alloc_fail = cpr->sw_stats.rx.rx_oom_discards;
14773 }
14774 
14775 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
14776 				    struct netdev_queue_stats_tx *stats)
14777 {
14778 	struct bnxt *bp = netdev_priv(dev);
14779 	struct bnxt_napi *bnapi;
14780 	u64 *sw;
14781 
14782 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
14783 	sw = bnapi->cp_ring.stats.sw_stats;
14784 
14785 	stats->packets = 0;
14786 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
14787 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
14788 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
14789 
14790 	stats->bytes = 0;
14791 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
14792 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
14793 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
14794 }
14795 
14796 static void bnxt_get_base_stats(struct net_device *dev,
14797 				struct netdev_queue_stats_rx *rx,
14798 				struct netdev_queue_stats_tx *tx)
14799 {
14800 	struct bnxt *bp = netdev_priv(dev);
14801 
14802 	rx->packets = bp->net_stats_prev.rx_packets;
14803 	rx->bytes = bp->net_stats_prev.rx_bytes;
14804 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
14805 
14806 	tx->packets = bp->net_stats_prev.tx_packets;
14807 	tx->bytes = bp->net_stats_prev.tx_bytes;
14808 }
14809 
14810 static const struct netdev_stat_ops bnxt_stat_ops = {
14811 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
14812 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
14813 	.get_base_stats		= bnxt_get_base_stats,
14814 };
14815 
14816 static void bnxt_remove_one(struct pci_dev *pdev)
14817 {
14818 	struct net_device *dev = pci_get_drvdata(pdev);
14819 	struct bnxt *bp = netdev_priv(dev);
14820 
14821 	if (BNXT_PF(bp))
14822 		bnxt_sriov_disable(bp);
14823 
14824 	bnxt_rdma_aux_device_del(bp);
14825 
14826 	bnxt_ptp_clear(bp);
14827 	unregister_netdev(dev);
14828 
14829 	bnxt_rdma_aux_device_uninit(bp);
14830 
14831 	bnxt_free_l2_filters(bp, true);
14832 	bnxt_free_ntp_fltrs(bp, true);
14833 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
14834 		bnxt_clear_rss_ctxs(bp, true);
14835 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14836 	/* Flush any pending tasks */
14837 	cancel_work_sync(&bp->sp_task);
14838 	cancel_delayed_work_sync(&bp->fw_reset_task);
14839 	bp->sp_event = 0;
14840 
14841 	bnxt_dl_fw_reporters_destroy(bp);
14842 	bnxt_dl_unregister(bp);
14843 	bnxt_shutdown_tc(bp);
14844 
14845 	bnxt_clear_int_mode(bp);
14846 	bnxt_hwrm_func_drv_unrgtr(bp);
14847 	bnxt_free_hwrm_resources(bp);
14848 	bnxt_hwmon_uninit(bp);
14849 	bnxt_ethtool_free(bp);
14850 	bnxt_dcb_free(bp);
14851 	kfree(bp->ptp_cfg);
14852 	bp->ptp_cfg = NULL;
14853 	kfree(bp->fw_health);
14854 	bp->fw_health = NULL;
14855 	bnxt_cleanup_pci(bp);
14856 	bnxt_free_ctx_mem(bp);
14857 	kfree(bp->rss_indir_tbl);
14858 	bp->rss_indir_tbl = NULL;
14859 	bnxt_free_port_stats(bp);
14860 	free_netdev(dev);
14861 }
14862 
14863 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
14864 {
14865 	int rc = 0;
14866 	struct bnxt_link_info *link_info = &bp->link_info;
14867 
14868 	bp->phy_flags = 0;
14869 	rc = bnxt_hwrm_phy_qcaps(bp);
14870 	if (rc) {
14871 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
14872 			   rc);
14873 		return rc;
14874 	}
14875 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
14876 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
14877 	else
14878 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
14879 	if (!fw_dflt)
14880 		return 0;
14881 
14882 	mutex_lock(&bp->link_lock);
14883 	rc = bnxt_update_link(bp, false);
14884 	if (rc) {
14885 		mutex_unlock(&bp->link_lock);
14886 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
14887 			   rc);
14888 		return rc;
14889 	}
14890 
14891 	/* Older firmware does not have supported_auto_speeds, so assume
14892 	 * that all supported speeds can be autonegotiated.
14893 	 */
14894 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
14895 		link_info->support_auto_speeds = link_info->support_speeds;
14896 
14897 	bnxt_init_ethtool_link_settings(bp);
14898 	mutex_unlock(&bp->link_lock);
14899 	return 0;
14900 }
14901 
14902 static int bnxt_get_max_irq(struct pci_dev *pdev)
14903 {
14904 	u16 ctrl;
14905 
14906 	if (!pdev->msix_cap)
14907 		return 1;
14908 
14909 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
14910 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
14911 }
14912 
14913 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14914 				int *max_cp)
14915 {
14916 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
14917 	int max_ring_grps = 0, max_irq;
14918 
14919 	*max_tx = hw_resc->max_tx_rings;
14920 	*max_rx = hw_resc->max_rx_rings;
14921 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
14922 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
14923 			bnxt_get_ulp_msix_num_in_use(bp),
14924 			hw_resc->max_stat_ctxs -
14925 			bnxt_get_ulp_stat_ctxs_in_use(bp));
14926 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14927 		*max_cp = min_t(int, *max_cp, max_irq);
14928 	max_ring_grps = hw_resc->max_hw_ring_grps;
14929 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
14930 		*max_cp -= 1;
14931 		*max_rx -= 2;
14932 	}
14933 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14934 		*max_rx >>= 1;
14935 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
14936 		int rc;
14937 
14938 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
14939 		if (rc) {
14940 			*max_rx = 0;
14941 			*max_tx = 0;
14942 		}
14943 		/* On P5 chips, max_cp output param should be available NQs */
14944 		*max_cp = max_irq;
14945 	}
14946 	*max_rx = min_t(int, *max_rx, max_ring_grps);
14947 }
14948 
14949 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
14950 {
14951 	int rx, tx, cp;
14952 
14953 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
14954 	*max_rx = rx;
14955 	*max_tx = tx;
14956 	if (!rx || !tx || !cp)
14957 		return -ENOMEM;
14958 
14959 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
14960 }
14961 
14962 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14963 			       bool shared)
14964 {
14965 	int rc;
14966 
14967 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
14968 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
14969 		/* Not enough rings, try disabling agg rings. */
14970 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
14971 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
14972 		if (rc) {
14973 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
14974 			bp->flags |= BNXT_FLAG_AGG_RINGS;
14975 			return rc;
14976 		}
14977 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
14978 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
14979 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
14980 		bnxt_set_ring_params(bp);
14981 	}
14982 
14983 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
14984 		int max_cp, max_stat, max_irq;
14985 
14986 		/* Reserve minimum resources for RoCE */
14987 		max_cp = bnxt_get_max_func_cp_rings(bp);
14988 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
14989 		max_irq = bnxt_get_max_func_irqs(bp);
14990 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
14991 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
14992 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
14993 			return 0;
14994 
14995 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
14996 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
14997 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
14998 		max_cp = min_t(int, max_cp, max_irq);
14999 		max_cp = min_t(int, max_cp, max_stat);
15000 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
15001 		if (rc)
15002 			rc = 0;
15003 	}
15004 	return rc;
15005 }
15006 
15007 /* In initial default shared ring setting, each shared ring must have a
15008  * RX/TX ring pair.
15009  */
15010 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
15011 {
15012 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
15013 	bp->rx_nr_rings = bp->cp_nr_rings;
15014 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
15015 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15016 }
15017 
15018 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
15019 {
15020 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
15021 	int avail_msix;
15022 
15023 	if (!bnxt_can_reserve_rings(bp))
15024 		return 0;
15025 
15026 	if (sh)
15027 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
15028 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
15029 	/* Reduce default rings on multi-port cards so that total default
15030 	 * rings do not exceed CPU count.
15031 	 */
15032 	if (bp->port_count > 1) {
15033 		int max_rings =
15034 			max_t(int, num_online_cpus() / bp->port_count, 1);
15035 
15036 		dflt_rings = min_t(int, dflt_rings, max_rings);
15037 	}
15038 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
15039 	if (rc)
15040 		return rc;
15041 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
15042 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
15043 	if (sh)
15044 		bnxt_trim_dflt_sh_rings(bp);
15045 	else
15046 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
15047 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15048 
15049 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
15050 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
15051 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
15052 
15053 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
15054 		bnxt_set_dflt_ulp_stat_ctxs(bp);
15055 	}
15056 
15057 	rc = __bnxt_reserve_rings(bp);
15058 	if (rc && rc != -ENODEV)
15059 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
15060 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15061 	if (sh)
15062 		bnxt_trim_dflt_sh_rings(bp);
15063 
15064 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
15065 	if (bnxt_need_reserve_rings(bp)) {
15066 		rc = __bnxt_reserve_rings(bp);
15067 		if (rc && rc != -ENODEV)
15068 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
15069 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15070 	}
15071 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
15072 		bp->rx_nr_rings++;
15073 		bp->cp_nr_rings++;
15074 	}
15075 	if (rc) {
15076 		bp->tx_nr_rings = 0;
15077 		bp->rx_nr_rings = 0;
15078 	}
15079 	return rc;
15080 }
15081 
15082 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
15083 {
15084 	int rc;
15085 
15086 	if (bp->tx_nr_rings)
15087 		return 0;
15088 
15089 	bnxt_ulp_irq_stop(bp);
15090 	bnxt_clear_int_mode(bp);
15091 	rc = bnxt_set_dflt_rings(bp, true);
15092 	if (rc) {
15093 		if (BNXT_VF(bp) && rc == -ENODEV)
15094 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
15095 		else
15096 			netdev_err(bp->dev, "Not enough rings available.\n");
15097 		goto init_dflt_ring_err;
15098 	}
15099 	rc = bnxt_init_int_mode(bp);
15100 	if (rc)
15101 		goto init_dflt_ring_err;
15102 
15103 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15104 
15105 	bnxt_set_dflt_rfs(bp);
15106 
15107 init_dflt_ring_err:
15108 	bnxt_ulp_irq_restart(bp, rc);
15109 	return rc;
15110 }
15111 
15112 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
15113 {
15114 	int rc;
15115 
15116 	ASSERT_RTNL();
15117 	bnxt_hwrm_func_qcaps(bp);
15118 
15119 	if (netif_running(bp->dev))
15120 		__bnxt_close_nic(bp, true, false);
15121 
15122 	bnxt_ulp_irq_stop(bp);
15123 	bnxt_clear_int_mode(bp);
15124 	rc = bnxt_init_int_mode(bp);
15125 	bnxt_ulp_irq_restart(bp, rc);
15126 
15127 	if (netif_running(bp->dev)) {
15128 		if (rc)
15129 			dev_close(bp->dev);
15130 		else
15131 			rc = bnxt_open_nic(bp, true, false);
15132 	}
15133 
15134 	return rc;
15135 }
15136 
15137 static int bnxt_init_mac_addr(struct bnxt *bp)
15138 {
15139 	int rc = 0;
15140 
15141 	if (BNXT_PF(bp)) {
15142 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
15143 	} else {
15144 #ifdef CONFIG_BNXT_SRIOV
15145 		struct bnxt_vf_info *vf = &bp->vf;
15146 		bool strict_approval = true;
15147 
15148 		if (is_valid_ether_addr(vf->mac_addr)) {
15149 			/* overwrite netdev dev_addr with admin VF MAC */
15150 			eth_hw_addr_set(bp->dev, vf->mac_addr);
15151 			/* Older PF driver or firmware may not approve this
15152 			 * correctly.
15153 			 */
15154 			strict_approval = false;
15155 		} else {
15156 			eth_hw_addr_random(bp->dev);
15157 		}
15158 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
15159 #endif
15160 	}
15161 	return rc;
15162 }
15163 
15164 static void bnxt_vpd_read_info(struct bnxt *bp)
15165 {
15166 	struct pci_dev *pdev = bp->pdev;
15167 	unsigned int vpd_size, kw_len;
15168 	int pos, size;
15169 	u8 *vpd_data;
15170 
15171 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
15172 	if (IS_ERR(vpd_data)) {
15173 		pci_warn(pdev, "Unable to read VPD\n");
15174 		return;
15175 	}
15176 
15177 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
15178 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
15179 	if (pos < 0)
15180 		goto read_sn;
15181 
15182 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
15183 	memcpy(bp->board_partno, &vpd_data[pos], size);
15184 
15185 read_sn:
15186 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
15187 					   PCI_VPD_RO_KEYWORD_SERIALNO,
15188 					   &kw_len);
15189 	if (pos < 0)
15190 		goto exit;
15191 
15192 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
15193 	memcpy(bp->board_serialno, &vpd_data[pos], size);
15194 exit:
15195 	kfree(vpd_data);
15196 }
15197 
15198 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
15199 {
15200 	struct pci_dev *pdev = bp->pdev;
15201 	u64 qword;
15202 
15203 	qword = pci_get_dsn(pdev);
15204 	if (!qword) {
15205 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
15206 		return -EOPNOTSUPP;
15207 	}
15208 
15209 	put_unaligned_le64(qword, dsn);
15210 
15211 	bp->flags |= BNXT_FLAG_DSN_VALID;
15212 	return 0;
15213 }
15214 
15215 static int bnxt_map_db_bar(struct bnxt *bp)
15216 {
15217 	if (!bp->db_size)
15218 		return -ENODEV;
15219 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
15220 	if (!bp->bar1)
15221 		return -ENOMEM;
15222 	return 0;
15223 }
15224 
15225 void bnxt_print_device_info(struct bnxt *bp)
15226 {
15227 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
15228 		    board_info[bp->board_idx].name,
15229 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
15230 
15231 	pcie_print_link_status(bp->pdev);
15232 }
15233 
15234 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
15235 {
15236 	struct bnxt_hw_resc *hw_resc;
15237 	struct net_device *dev;
15238 	struct bnxt *bp;
15239 	int rc, max_irqs;
15240 
15241 	if (pci_is_bridge(pdev))
15242 		return -ENODEV;
15243 
15244 	/* Clear any pending DMA transactions from crash kernel
15245 	 * while loading driver in capture kernel.
15246 	 */
15247 	if (is_kdump_kernel()) {
15248 		pci_clear_master(pdev);
15249 		pcie_flr(pdev);
15250 	}
15251 
15252 	max_irqs = bnxt_get_max_irq(pdev);
15253 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
15254 				 max_irqs);
15255 	if (!dev)
15256 		return -ENOMEM;
15257 
15258 	bp = netdev_priv(dev);
15259 	bp->board_idx = ent->driver_data;
15260 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
15261 	bnxt_set_max_func_irqs(bp, max_irqs);
15262 
15263 	if (bnxt_vf_pciid(bp->board_idx))
15264 		bp->flags |= BNXT_FLAG_VF;
15265 
15266 	/* No devlink port registration in case of a VF */
15267 	if (BNXT_PF(bp))
15268 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
15269 
15270 	if (pdev->msix_cap)
15271 		bp->flags |= BNXT_FLAG_MSIX_CAP;
15272 
15273 	rc = bnxt_init_board(pdev, dev);
15274 	if (rc < 0)
15275 		goto init_err_free;
15276 
15277 	dev->netdev_ops = &bnxt_netdev_ops;
15278 	dev->stat_ops = &bnxt_stat_ops;
15279 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
15280 	dev->ethtool_ops = &bnxt_ethtool_ops;
15281 	pci_set_drvdata(pdev, dev);
15282 
15283 	rc = bnxt_alloc_hwrm_resources(bp);
15284 	if (rc)
15285 		goto init_err_pci_clean;
15286 
15287 	mutex_init(&bp->hwrm_cmd_lock);
15288 	mutex_init(&bp->link_lock);
15289 
15290 	rc = bnxt_fw_init_one_p1(bp);
15291 	if (rc)
15292 		goto init_err_pci_clean;
15293 
15294 	if (BNXT_PF(bp))
15295 		bnxt_vpd_read_info(bp);
15296 
15297 	if (BNXT_CHIP_P5_PLUS(bp)) {
15298 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
15299 		if (BNXT_CHIP_P7(bp))
15300 			bp->flags |= BNXT_FLAG_CHIP_P7;
15301 	}
15302 
15303 	rc = bnxt_alloc_rss_indir_tbl(bp, NULL);
15304 	if (rc)
15305 		goto init_err_pci_clean;
15306 
15307 	rc = bnxt_fw_init_one_p2(bp);
15308 	if (rc)
15309 		goto init_err_pci_clean;
15310 
15311 	rc = bnxt_map_db_bar(bp);
15312 	if (rc) {
15313 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
15314 			rc);
15315 		goto init_err_pci_clean;
15316 	}
15317 
15318 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
15319 			   NETIF_F_TSO | NETIF_F_TSO6 |
15320 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
15321 			   NETIF_F_GSO_IPXIP4 |
15322 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
15323 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
15324 			   NETIF_F_RXCSUM | NETIF_F_GRO;
15325 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
15326 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
15327 
15328 	if (BNXT_SUPPORTS_TPA(bp))
15329 		dev->hw_features |= NETIF_F_LRO;
15330 
15331 	dev->hw_enc_features =
15332 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
15333 			NETIF_F_TSO | NETIF_F_TSO6 |
15334 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
15335 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
15336 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
15337 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
15338 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
15339 	if (bp->flags & BNXT_FLAG_CHIP_P7)
15340 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
15341 	else
15342 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
15343 
15344 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
15345 				    NETIF_F_GSO_GRE_CSUM;
15346 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
15347 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
15348 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
15349 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
15350 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
15351 	if (BNXT_SUPPORTS_TPA(bp))
15352 		dev->hw_features |= NETIF_F_GRO_HW;
15353 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
15354 	if (dev->features & NETIF_F_GRO_HW)
15355 		dev->features &= ~NETIF_F_LRO;
15356 	dev->priv_flags |= IFF_UNICAST_FLT;
15357 
15358 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
15359 
15360 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
15361 			    NETDEV_XDP_ACT_RX_SG;
15362 
15363 #ifdef CONFIG_BNXT_SRIOV
15364 	init_waitqueue_head(&bp->sriov_cfg_wait);
15365 #endif
15366 	if (BNXT_SUPPORTS_TPA(bp)) {
15367 		bp->gro_func = bnxt_gro_func_5730x;
15368 		if (BNXT_CHIP_P4(bp))
15369 			bp->gro_func = bnxt_gro_func_5731x;
15370 		else if (BNXT_CHIP_P5_PLUS(bp))
15371 			bp->gro_func = bnxt_gro_func_5750x;
15372 	}
15373 	if (!BNXT_CHIP_P4_PLUS(bp))
15374 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
15375 
15376 	rc = bnxt_init_mac_addr(bp);
15377 	if (rc) {
15378 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
15379 		rc = -EADDRNOTAVAIL;
15380 		goto init_err_pci_clean;
15381 	}
15382 
15383 	if (BNXT_PF(bp)) {
15384 		/* Read the adapter's DSN to use as the eswitch switch_id */
15385 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
15386 	}
15387 
15388 	/* MTU range: 60 - FW defined max */
15389 	dev->min_mtu = ETH_ZLEN;
15390 	dev->max_mtu = bp->max_mtu;
15391 
15392 	rc = bnxt_probe_phy(bp, true);
15393 	if (rc)
15394 		goto init_err_pci_clean;
15395 
15396 	hw_resc = &bp->hw_resc;
15397 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
15398 		       BNXT_L2_FLTR_MAX_FLTR;
15399 	/* Older firmware may not report these filters properly */
15400 	if (bp->max_fltr < BNXT_MAX_FLTR)
15401 		bp->max_fltr = BNXT_MAX_FLTR;
15402 	bnxt_init_l2_fltr_tbl(bp);
15403 	bnxt_set_rx_skb_mode(bp, false);
15404 	bnxt_set_tpa_flags(bp);
15405 	bnxt_set_ring_params(bp);
15406 	bnxt_rdma_aux_device_init(bp);
15407 	rc = bnxt_set_dflt_rings(bp, true);
15408 	if (rc) {
15409 		if (BNXT_VF(bp) && rc == -ENODEV) {
15410 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
15411 		} else {
15412 			netdev_err(bp->dev, "Not enough rings available.\n");
15413 			rc = -ENOMEM;
15414 		}
15415 		goto init_err_pci_clean;
15416 	}
15417 
15418 	bnxt_fw_init_one_p3(bp);
15419 
15420 	bnxt_init_dflt_coal(bp);
15421 
15422 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
15423 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
15424 
15425 	rc = bnxt_init_int_mode(bp);
15426 	if (rc)
15427 		goto init_err_pci_clean;
15428 
15429 	/* No TC has been set yet and rings may have been trimmed due to
15430 	 * limited MSIX, so we re-initialize the TX rings per TC.
15431 	 */
15432 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15433 
15434 	if (BNXT_PF(bp)) {
15435 		if (!bnxt_pf_wq) {
15436 			bnxt_pf_wq =
15437 				create_singlethread_workqueue("bnxt_pf_wq");
15438 			if (!bnxt_pf_wq) {
15439 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
15440 				rc = -ENOMEM;
15441 				goto init_err_pci_clean;
15442 			}
15443 		}
15444 		rc = bnxt_init_tc(bp);
15445 		if (rc)
15446 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
15447 				   rc);
15448 	}
15449 
15450 	bnxt_inv_fw_health_reg(bp);
15451 	rc = bnxt_dl_register(bp);
15452 	if (rc)
15453 		goto init_err_dl;
15454 
15455 	INIT_LIST_HEAD(&bp->usr_fltr_list);
15456 
15457 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
15458 		bnxt_init_multi_rss_ctx(bp);
15459 
15460 
15461 	rc = register_netdev(dev);
15462 	if (rc)
15463 		goto init_err_cleanup;
15464 
15465 	bnxt_dl_fw_reporters_create(bp);
15466 
15467 	bnxt_rdma_aux_device_add(bp);
15468 
15469 	bnxt_print_device_info(bp);
15470 
15471 	pci_save_state(pdev);
15472 
15473 	return 0;
15474 init_err_cleanup:
15475 	bnxt_rdma_aux_device_uninit(bp);
15476 	bnxt_dl_unregister(bp);
15477 init_err_dl:
15478 	bnxt_shutdown_tc(bp);
15479 	bnxt_clear_int_mode(bp);
15480 
15481 init_err_pci_clean:
15482 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
15483 		bnxt_clear_rss_ctxs(bp, true);
15484 	bnxt_hwrm_func_drv_unrgtr(bp);
15485 	bnxt_free_hwrm_resources(bp);
15486 	bnxt_hwmon_uninit(bp);
15487 	bnxt_ethtool_free(bp);
15488 	bnxt_ptp_clear(bp);
15489 	kfree(bp->ptp_cfg);
15490 	bp->ptp_cfg = NULL;
15491 	kfree(bp->fw_health);
15492 	bp->fw_health = NULL;
15493 	bnxt_cleanup_pci(bp);
15494 	bnxt_free_ctx_mem(bp);
15495 	kfree(bp->rss_indir_tbl);
15496 	bp->rss_indir_tbl = NULL;
15497 
15498 init_err_free:
15499 	free_netdev(dev);
15500 	return rc;
15501 }
15502 
15503 static void bnxt_shutdown(struct pci_dev *pdev)
15504 {
15505 	struct net_device *dev = pci_get_drvdata(pdev);
15506 	struct bnxt *bp;
15507 
15508 	if (!dev)
15509 		return;
15510 
15511 	rtnl_lock();
15512 	bp = netdev_priv(dev);
15513 	if (!bp)
15514 		goto shutdown_exit;
15515 
15516 	if (netif_running(dev))
15517 		dev_close(dev);
15518 
15519 	bnxt_clear_int_mode(bp);
15520 	pci_disable_device(pdev);
15521 
15522 	if (system_state == SYSTEM_POWER_OFF) {
15523 		pci_wake_from_d3(pdev, bp->wol);
15524 		pci_set_power_state(pdev, PCI_D3hot);
15525 	}
15526 
15527 shutdown_exit:
15528 	rtnl_unlock();
15529 }
15530 
15531 #ifdef CONFIG_PM_SLEEP
15532 static int bnxt_suspend(struct device *device)
15533 {
15534 	struct net_device *dev = dev_get_drvdata(device);
15535 	struct bnxt *bp = netdev_priv(dev);
15536 	int rc = 0;
15537 
15538 	rtnl_lock();
15539 	bnxt_ulp_stop(bp);
15540 	if (netif_running(dev)) {
15541 		netif_device_detach(dev);
15542 		rc = bnxt_close(dev);
15543 	}
15544 	bnxt_hwrm_func_drv_unrgtr(bp);
15545 	pci_disable_device(bp->pdev);
15546 	bnxt_free_ctx_mem(bp);
15547 	rtnl_unlock();
15548 	return rc;
15549 }
15550 
15551 static int bnxt_resume(struct device *device)
15552 {
15553 	struct net_device *dev = dev_get_drvdata(device);
15554 	struct bnxt *bp = netdev_priv(dev);
15555 	int rc = 0;
15556 
15557 	rtnl_lock();
15558 	rc = pci_enable_device(bp->pdev);
15559 	if (rc) {
15560 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
15561 			   rc);
15562 		goto resume_exit;
15563 	}
15564 	pci_set_master(bp->pdev);
15565 	if (bnxt_hwrm_ver_get(bp)) {
15566 		rc = -ENODEV;
15567 		goto resume_exit;
15568 	}
15569 	rc = bnxt_hwrm_func_reset(bp);
15570 	if (rc) {
15571 		rc = -EBUSY;
15572 		goto resume_exit;
15573 	}
15574 
15575 	rc = bnxt_hwrm_func_qcaps(bp);
15576 	if (rc)
15577 		goto resume_exit;
15578 
15579 	bnxt_clear_reservations(bp, true);
15580 
15581 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
15582 		rc = -ENODEV;
15583 		goto resume_exit;
15584 	}
15585 
15586 	bnxt_get_wol_settings(bp);
15587 	if (netif_running(dev)) {
15588 		rc = bnxt_open(dev);
15589 		if (!rc)
15590 			netif_device_attach(dev);
15591 	}
15592 
15593 resume_exit:
15594 	bnxt_ulp_start(bp, rc);
15595 	if (!rc)
15596 		bnxt_reenable_sriov(bp);
15597 	rtnl_unlock();
15598 	return rc;
15599 }
15600 
15601 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
15602 #define BNXT_PM_OPS (&bnxt_pm_ops)
15603 
15604 #else
15605 
15606 #define BNXT_PM_OPS NULL
15607 
15608 #endif /* CONFIG_PM_SLEEP */
15609 
15610 /**
15611  * bnxt_io_error_detected - called when PCI error is detected
15612  * @pdev: Pointer to PCI device
15613  * @state: The current pci connection state
15614  *
15615  * This function is called after a PCI bus error affecting
15616  * this device has been detected.
15617  */
15618 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
15619 					       pci_channel_state_t state)
15620 {
15621 	struct net_device *netdev = pci_get_drvdata(pdev);
15622 	struct bnxt *bp = netdev_priv(netdev);
15623 	bool abort = false;
15624 
15625 	netdev_info(netdev, "PCI I/O error detected\n");
15626 
15627 	rtnl_lock();
15628 	netif_device_detach(netdev);
15629 
15630 	bnxt_ulp_stop(bp);
15631 
15632 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
15633 		netdev_err(bp->dev, "Firmware reset already in progress\n");
15634 		abort = true;
15635 	}
15636 
15637 	if (abort || state == pci_channel_io_perm_failure) {
15638 		rtnl_unlock();
15639 		return PCI_ERS_RESULT_DISCONNECT;
15640 	}
15641 
15642 	/* Link is not reliable anymore if state is pci_channel_io_frozen
15643 	 * so we disable bus master to prevent any potential bad DMAs before
15644 	 * freeing kernel memory.
15645 	 */
15646 	if (state == pci_channel_io_frozen) {
15647 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
15648 		bnxt_fw_fatal_close(bp);
15649 	}
15650 
15651 	if (netif_running(netdev))
15652 		__bnxt_close_nic(bp, true, true);
15653 
15654 	if (pci_is_enabled(pdev))
15655 		pci_disable_device(pdev);
15656 	bnxt_free_ctx_mem(bp);
15657 	rtnl_unlock();
15658 
15659 	/* Request a slot slot reset. */
15660 	return PCI_ERS_RESULT_NEED_RESET;
15661 }
15662 
15663 /**
15664  * bnxt_io_slot_reset - called after the pci bus has been reset.
15665  * @pdev: Pointer to PCI device
15666  *
15667  * Restart the card from scratch, as if from a cold-boot.
15668  * At this point, the card has exprienced a hard reset,
15669  * followed by fixups by BIOS, and has its config space
15670  * set up identically to what it was at cold boot.
15671  */
15672 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
15673 {
15674 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
15675 	struct net_device *netdev = pci_get_drvdata(pdev);
15676 	struct bnxt *bp = netdev_priv(netdev);
15677 	int retry = 0;
15678 	int err = 0;
15679 	int off;
15680 
15681 	netdev_info(bp->dev, "PCI Slot Reset\n");
15682 
15683 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
15684 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
15685 		msleep(900);
15686 
15687 	rtnl_lock();
15688 
15689 	if (pci_enable_device(pdev)) {
15690 		dev_err(&pdev->dev,
15691 			"Cannot re-enable PCI device after reset.\n");
15692 	} else {
15693 		pci_set_master(pdev);
15694 		/* Upon fatal error, our device internal logic that latches to
15695 		 * BAR value is getting reset and will restore only upon
15696 		 * rewritting the BARs.
15697 		 *
15698 		 * As pci_restore_state() does not re-write the BARs if the
15699 		 * value is same as saved value earlier, driver needs to
15700 		 * write the BARs to 0 to force restore, in case of fatal error.
15701 		 */
15702 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
15703 				       &bp->state)) {
15704 			for (off = PCI_BASE_ADDRESS_0;
15705 			     off <= PCI_BASE_ADDRESS_5; off += 4)
15706 				pci_write_config_dword(bp->pdev, off, 0);
15707 		}
15708 		pci_restore_state(pdev);
15709 		pci_save_state(pdev);
15710 
15711 		bnxt_inv_fw_health_reg(bp);
15712 		bnxt_try_map_fw_health_reg(bp);
15713 
15714 		/* In some PCIe AER scenarios, firmware may take up to
15715 		 * 10 seconds to become ready in the worst case.
15716 		 */
15717 		do {
15718 			err = bnxt_try_recover_fw(bp);
15719 			if (!err)
15720 				break;
15721 			retry++;
15722 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
15723 
15724 		if (err) {
15725 			dev_err(&pdev->dev, "Firmware not ready\n");
15726 			goto reset_exit;
15727 		}
15728 
15729 		err = bnxt_hwrm_func_reset(bp);
15730 		if (!err)
15731 			result = PCI_ERS_RESULT_RECOVERED;
15732 
15733 		bnxt_ulp_irq_stop(bp);
15734 		bnxt_clear_int_mode(bp);
15735 		err = bnxt_init_int_mode(bp);
15736 		bnxt_ulp_irq_restart(bp, err);
15737 	}
15738 
15739 reset_exit:
15740 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15741 	bnxt_clear_reservations(bp, true);
15742 	rtnl_unlock();
15743 
15744 	return result;
15745 }
15746 
15747 /**
15748  * bnxt_io_resume - called when traffic can start flowing again.
15749  * @pdev: Pointer to PCI device
15750  *
15751  * This callback is called when the error recovery driver tells
15752  * us that its OK to resume normal operation.
15753  */
15754 static void bnxt_io_resume(struct pci_dev *pdev)
15755 {
15756 	struct net_device *netdev = pci_get_drvdata(pdev);
15757 	struct bnxt *bp = netdev_priv(netdev);
15758 	int err;
15759 
15760 	netdev_info(bp->dev, "PCI Slot Resume\n");
15761 	rtnl_lock();
15762 
15763 	err = bnxt_hwrm_func_qcaps(bp);
15764 	if (!err && netif_running(netdev))
15765 		err = bnxt_open(netdev);
15766 
15767 	bnxt_ulp_start(bp, err);
15768 	if (!err) {
15769 		bnxt_reenable_sriov(bp);
15770 		netif_device_attach(netdev);
15771 	}
15772 
15773 	rtnl_unlock();
15774 }
15775 
15776 static const struct pci_error_handlers bnxt_err_handler = {
15777 	.error_detected	= bnxt_io_error_detected,
15778 	.slot_reset	= bnxt_io_slot_reset,
15779 	.resume		= bnxt_io_resume
15780 };
15781 
15782 static struct pci_driver bnxt_pci_driver = {
15783 	.name		= DRV_MODULE_NAME,
15784 	.id_table	= bnxt_pci_tbl,
15785 	.probe		= bnxt_init_one,
15786 	.remove		= bnxt_remove_one,
15787 	.shutdown	= bnxt_shutdown,
15788 	.driver.pm	= BNXT_PM_OPS,
15789 	.err_handler	= &bnxt_err_handler,
15790 #if defined(CONFIG_BNXT_SRIOV)
15791 	.sriov_configure = bnxt_sriov_configure,
15792 #endif
15793 };
15794 
15795 static int __init bnxt_init(void)
15796 {
15797 	int err;
15798 
15799 	bnxt_debug_init();
15800 	err = pci_register_driver(&bnxt_pci_driver);
15801 	if (err) {
15802 		bnxt_debug_exit();
15803 		return err;
15804 	}
15805 
15806 	return 0;
15807 }
15808 
15809 static void __exit bnxt_exit(void)
15810 {
15811 	pci_unregister_driver(&bnxt_pci_driver);
15812 	if (bnxt_pf_wq)
15813 		destroy_workqueue(bnxt_pf_wq);
15814 	bnxt_debug_exit();
15815 }
15816 
15817 module_init(bnxt_init);
15818 module_exit(bnxt_exit);
15819