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