xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision 3e7819886281e077e82006fe4804b0d6b0f5643b)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  * Copyright (c) 2016-2019 Broadcom Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  */
10 
11 #include <linux/module.h>
12 
13 #include <linux/stringify.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/pci.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/bitops.h>
27 #include <linux/io.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/byteorder.h>
31 #include <asm/page.h>
32 #include <linux/time.h>
33 #include <linux/mii.h>
34 #include <linux/mdio.h>
35 #include <linux/if.h>
36 #include <linux/if_vlan.h>
37 #include <linux/if_bridge.h>
38 #include <linux/rtc.h>
39 #include <linux/bpf.h>
40 #include <net/gro.h>
41 #include <net/ip.h>
42 #include <net/tcp.h>
43 #include <net/udp.h>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
46 #include <net/udp_tunnel.h>
47 #include <linux/workqueue.h>
48 #include <linux/prefetch.h>
49 #include <linux/cache.h>
50 #include <linux/log2.h>
51 #include <linux/bitmap.h>
52 #include <linux/cpu_rmap.h>
53 #include <linux/cpumask.h>
54 #include <net/pkt_cls.h>
55 #include <net/page_pool/helpers.h>
56 #include <linux/align.h>
57 #include <net/netdev_queues.h>
58 
59 #include "bnxt_hsi.h"
60 #include "bnxt.h"
61 #include "bnxt_hwrm.h"
62 #include "bnxt_ulp.h"
63 #include "bnxt_sriov.h"
64 #include "bnxt_ethtool.h"
65 #include "bnxt_dcb.h"
66 #include "bnxt_xdp.h"
67 #include "bnxt_ptp.h"
68 #include "bnxt_vfr.h"
69 #include "bnxt_tc.h"
70 #include "bnxt_devlink.h"
71 #include "bnxt_debugfs.h"
72 #include "bnxt_hwmon.h"
73 
74 #define BNXT_TX_TIMEOUT		(5 * HZ)
75 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
76 				 NETIF_MSG_TX_ERR)
77 
78 MODULE_LICENSE("GPL");
79 MODULE_DESCRIPTION("Broadcom NetXtreme network driver");
80 
81 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
82 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
83 #define BNXT_RX_COPY_THRESH 256
84 
85 #define BNXT_TX_PUSH_THRESH 164
86 
87 /* indexed by enum board_idx */
88 static const struct {
89 	char *name;
90 } board_info[] = {
91 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
92 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
93 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
94 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
95 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
96 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
97 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
98 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
99 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
100 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
101 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
102 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
103 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
104 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
105 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
106 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
108 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
109 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
110 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
111 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
112 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
113 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
114 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
115 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
116 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
117 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
118 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
119 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
120 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
121 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
122 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
123 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
124 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
125 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
126 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
127 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
128 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
129 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
130 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
131 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
132 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
133 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
134 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
135 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
136 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
137 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
138 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
139 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
140 	[NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" },
141 };
142 
143 static const struct pci_device_id bnxt_pci_tbl[] = {
144 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
145 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
146 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
147 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
148 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
149 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
150 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
151 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
152 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
153 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
155 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
159 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
160 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
164 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
165 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
166 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
167 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
168 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
169 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
170 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
171 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
172 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
173 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
174 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
175 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
176 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
178 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
179 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
180 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
181 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
182 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
183 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
184 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
185 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
187 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
188 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
189 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
190 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
191 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
192 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
193 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
194 #ifdef CONFIG_BNXT_SRIOV
195 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
196 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
197 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
198 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
199 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
200 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
201 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
202 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
203 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
204 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
205 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
206 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
207 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
208 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
209 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
210 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
211 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
212 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
214 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
215 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
216 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
217 #endif
218 	{ 0 }
219 };
220 
221 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
222 
223 static const u16 bnxt_vf_req_snif[] = {
224 	HWRM_FUNC_CFG,
225 	HWRM_FUNC_VF_CFG,
226 	HWRM_PORT_PHY_QCFG,
227 	HWRM_CFA_L2_FILTER_ALLOC,
228 };
229 
230 static const u16 bnxt_async_events_arr[] = {
231 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
232 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
233 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
234 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
235 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
236 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
237 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
238 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
239 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
240 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
241 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
242 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
243 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
244 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
245 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
246 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
247 };
248 
249 static struct workqueue_struct *bnxt_pf_wq;
250 
251 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
252 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
253 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
254 
255 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
256 	.ports = {
257 		.src = 0,
258 		.dst = 0,
259 	},
260 	.addrs = {
261 		.v6addrs = {
262 			.src = BNXT_IPV6_MASK_NONE,
263 			.dst = BNXT_IPV6_MASK_NONE,
264 		},
265 	},
266 };
267 
268 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
269 	.ports = {
270 		.src = cpu_to_be16(0xffff),
271 		.dst = cpu_to_be16(0xffff),
272 	},
273 	.addrs = {
274 		.v6addrs = {
275 			.src = BNXT_IPV6_MASK_ALL,
276 			.dst = BNXT_IPV6_MASK_ALL,
277 		},
278 	},
279 };
280 
281 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
282 	.ports = {
283 		.src = cpu_to_be16(0xffff),
284 		.dst = cpu_to_be16(0xffff),
285 	},
286 	.addrs = {
287 		.v4addrs = {
288 			.src = cpu_to_be32(0xffffffff),
289 			.dst = cpu_to_be32(0xffffffff),
290 		},
291 	},
292 };
293 
294 static bool bnxt_vf_pciid(enum board_idx idx)
295 {
296 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
297 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
298 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
299 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF);
300 }
301 
302 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
303 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
304 #define DB_CP_IRQ_DIS_FLAGS	(DB_KEY_CP | DB_IRQ_DIS)
305 
306 #define BNXT_CP_DB_IRQ_DIS(db)						\
307 		writel(DB_CP_IRQ_DIS_FLAGS, db)
308 
309 #define BNXT_DB_CQ(db, idx)						\
310 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
311 
312 #define BNXT_DB_NQ_P5(db, idx)						\
313 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
314 		    (db)->doorbell)
315 
316 #define BNXT_DB_NQ_P7(db, idx)						\
317 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
318 		    DB_RING_IDX(db, idx), (db)->doorbell)
319 
320 #define BNXT_DB_CQ_ARM(db, idx)						\
321 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
322 
323 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
324 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
325 		    DB_RING_IDX(db, idx), (db)->doorbell)
326 
327 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
328 {
329 	if (bp->flags & BNXT_FLAG_CHIP_P7)
330 		BNXT_DB_NQ_P7(db, idx);
331 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
332 		BNXT_DB_NQ_P5(db, idx);
333 	else
334 		BNXT_DB_CQ(db, idx);
335 }
336 
337 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
338 {
339 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
340 		BNXT_DB_NQ_ARM_P5(db, idx);
341 	else
342 		BNXT_DB_CQ_ARM(db, idx);
343 }
344 
345 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
346 {
347 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
348 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
349 			    DB_RING_IDX(db, idx), db->doorbell);
350 	else
351 		BNXT_DB_CQ(db, idx);
352 }
353 
354 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
355 {
356 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
357 		return;
358 
359 	if (BNXT_PF(bp))
360 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
361 	else
362 		schedule_delayed_work(&bp->fw_reset_task, delay);
363 }
364 
365 static void __bnxt_queue_sp_work(struct bnxt *bp)
366 {
367 	if (BNXT_PF(bp))
368 		queue_work(bnxt_pf_wq, &bp->sp_task);
369 	else
370 		schedule_work(&bp->sp_task);
371 }
372 
373 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
374 {
375 	set_bit(event, &bp->sp_event);
376 	__bnxt_queue_sp_work(bp);
377 }
378 
379 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
380 {
381 	if (!rxr->bnapi->in_reset) {
382 		rxr->bnapi->in_reset = true;
383 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
384 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
385 		else
386 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
387 		__bnxt_queue_sp_work(bp);
388 	}
389 	rxr->rx_next_cons = 0xffff;
390 }
391 
392 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
393 			  u16 curr)
394 {
395 	struct bnxt_napi *bnapi = txr->bnapi;
396 
397 	if (bnapi->tx_fault)
398 		return;
399 
400 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
401 		   txr->txq_index, txr->tx_hw_cons,
402 		   txr->tx_cons, txr->tx_prod, curr);
403 	WARN_ON_ONCE(1);
404 	bnapi->tx_fault = 1;
405 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
406 }
407 
408 const u16 bnxt_lhint_arr[] = {
409 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
410 	TX_BD_FLAGS_LHINT_512_TO_1023,
411 	TX_BD_FLAGS_LHINT_1024_TO_2047,
412 	TX_BD_FLAGS_LHINT_1024_TO_2047,
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 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
427 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
428 };
429 
430 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
431 {
432 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
433 
434 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
435 		return 0;
436 
437 	return md_dst->u.port_info.port_id;
438 }
439 
440 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
441 			     u16 prod)
442 {
443 	/* Sync BD data before updating doorbell */
444 	wmb();
445 	bnxt_db_write(bp, &txr->tx_db, prod);
446 	txr->kick_pending = 0;
447 }
448 
449 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
450 {
451 	struct bnxt *bp = netdev_priv(dev);
452 	struct tx_bd *txbd, *txbd0;
453 	struct tx_bd_ext *txbd1;
454 	struct netdev_queue *txq;
455 	int i;
456 	dma_addr_t mapping;
457 	unsigned int length, pad = 0;
458 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
459 	u16 prod, last_frag;
460 	struct pci_dev *pdev = bp->pdev;
461 	struct bnxt_tx_ring_info *txr;
462 	struct bnxt_sw_tx_bd *tx_buf;
463 	__le32 lflags = 0;
464 
465 	i = skb_get_queue_mapping(skb);
466 	if (unlikely(i >= bp->tx_nr_rings)) {
467 		dev_kfree_skb_any(skb);
468 		dev_core_stats_tx_dropped_inc(dev);
469 		return NETDEV_TX_OK;
470 	}
471 
472 	txq = netdev_get_tx_queue(dev, i);
473 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
474 	prod = txr->tx_prod;
475 
476 	free_size = bnxt_tx_avail(bp, txr);
477 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
478 		/* We must have raced with NAPI cleanup */
479 		if (net_ratelimit() && txr->kick_pending)
480 			netif_warn(bp, tx_err, dev,
481 				   "bnxt: ring busy w/ flush pending!\n");
482 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
483 					bp->tx_wake_thresh))
484 			return NETDEV_TX_BUSY;
485 	}
486 
487 	if (unlikely(ipv6_hopopt_jumbo_remove(skb)))
488 		goto tx_free;
489 
490 	length = skb->len;
491 	len = skb_headlen(skb);
492 	last_frag = skb_shinfo(skb)->nr_frags;
493 
494 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
495 
496 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
497 	tx_buf->skb = skb;
498 	tx_buf->nr_frags = last_frag;
499 
500 	vlan_tag_flags = 0;
501 	cfa_action = bnxt_xmit_get_cfa_action(skb);
502 	if (skb_vlan_tag_present(skb)) {
503 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
504 				 skb_vlan_tag_get(skb);
505 		/* Currently supports 8021Q, 8021AD vlan offloads
506 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
507 		 */
508 		if (skb->vlan_proto == htons(ETH_P_8021Q))
509 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
510 	}
511 
512 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
513 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
514 
515 		if (ptp && ptp->tx_tstamp_en && !skb_is_gso(skb) &&
516 		    atomic_dec_if_positive(&ptp->tx_avail) >= 0) {
517 			if (!bnxt_ptp_parse(skb, &ptp->tx_seqid,
518 					    &ptp->tx_hdr_off)) {
519 				if (vlan_tag_flags)
520 					ptp->tx_hdr_off += VLAN_HLEN;
521 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
522 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
523 			} else {
524 				atomic_inc(&bp->ptp_cfg->tx_avail);
525 			}
526 		}
527 	}
528 
529 	if (unlikely(skb->no_fcs))
530 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
531 
532 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
533 	    !lflags) {
534 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
535 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
536 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
537 		void __iomem *db = txr->tx_db.doorbell;
538 		void *pdata = tx_push_buf->data;
539 		u64 *end;
540 		int j, push_len;
541 
542 		/* Set COAL_NOW to be ready quickly for the next push */
543 		tx_push->tx_bd_len_flags_type =
544 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
545 					TX_BD_TYPE_LONG_TX_BD |
546 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
547 					TX_BD_FLAGS_COAL_NOW |
548 					TX_BD_FLAGS_PACKET_END |
549 					(2 << TX_BD_FLAGS_BD_CNT_SHIFT));
550 
551 		if (skb->ip_summed == CHECKSUM_PARTIAL)
552 			tx_push1->tx_bd_hsize_lflags =
553 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
554 		else
555 			tx_push1->tx_bd_hsize_lflags = 0;
556 
557 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
558 		tx_push1->tx_bd_cfa_action =
559 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
560 
561 		end = pdata + length;
562 		end = PTR_ALIGN(end, 8) - 1;
563 		*end = 0;
564 
565 		skb_copy_from_linear_data(skb, pdata, len);
566 		pdata += len;
567 		for (j = 0; j < last_frag; j++) {
568 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
569 			void *fptr;
570 
571 			fptr = skb_frag_address_safe(frag);
572 			if (!fptr)
573 				goto normal_tx;
574 
575 			memcpy(pdata, fptr, skb_frag_size(frag));
576 			pdata += skb_frag_size(frag);
577 		}
578 
579 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
580 		txbd->tx_bd_haddr = txr->data_mapping;
581 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
582 		prod = NEXT_TX(prod);
583 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
584 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
585 		memcpy(txbd, tx_push1, sizeof(*txbd));
586 		prod = NEXT_TX(prod);
587 		tx_push->doorbell =
588 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
589 				    DB_RING_IDX(&txr->tx_db, prod));
590 		WRITE_ONCE(txr->tx_prod, prod);
591 
592 		tx_buf->is_push = 1;
593 		netdev_tx_sent_queue(txq, skb->len);
594 		wmb();	/* Sync is_push and byte queue before pushing data */
595 
596 		push_len = (length + sizeof(*tx_push) + 7) / 8;
597 		if (push_len > 16) {
598 			__iowrite64_copy(db, tx_push_buf, 16);
599 			__iowrite32_copy(db + 4, tx_push_buf + 1,
600 					 (push_len - 16) << 1);
601 		} else {
602 			__iowrite64_copy(db, tx_push_buf, push_len);
603 		}
604 
605 		goto tx_done;
606 	}
607 
608 normal_tx:
609 	if (length < BNXT_MIN_PKT_SIZE) {
610 		pad = BNXT_MIN_PKT_SIZE - length;
611 		if (skb_pad(skb, pad))
612 			/* SKB already freed. */
613 			goto tx_kick_pending;
614 		length = BNXT_MIN_PKT_SIZE;
615 	}
616 
617 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
618 
619 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
620 		goto tx_free;
621 
622 	dma_unmap_addr_set(tx_buf, mapping, mapping);
623 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
624 		((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
625 
626 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
627 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
628 
629 	prod = NEXT_TX(prod);
630 	txbd1 = (struct tx_bd_ext *)
631 		&txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
632 
633 	txbd1->tx_bd_hsize_lflags = lflags;
634 	if (skb_is_gso(skb)) {
635 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
636 		u32 hdr_len;
637 
638 		if (skb->encapsulation) {
639 			if (udp_gso)
640 				hdr_len = skb_inner_transport_offset(skb) +
641 					  sizeof(struct udphdr);
642 			else
643 				hdr_len = skb_inner_tcp_all_headers(skb);
644 		} else if (udp_gso) {
645 			hdr_len = skb_transport_offset(skb) +
646 				  sizeof(struct udphdr);
647 		} else {
648 			hdr_len = skb_tcp_all_headers(skb);
649 		}
650 
651 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
652 					TX_BD_FLAGS_T_IPID |
653 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
654 		length = skb_shinfo(skb)->gso_size;
655 		txbd1->tx_bd_mss = cpu_to_le32(length);
656 		length += hdr_len;
657 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
658 		txbd1->tx_bd_hsize_lflags |=
659 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
660 		txbd1->tx_bd_mss = 0;
661 	}
662 
663 	length >>= 9;
664 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
665 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
666 				     skb->len);
667 		i = 0;
668 		goto tx_dma_error;
669 	}
670 	flags |= bnxt_lhint_arr[length];
671 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
672 
673 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
674 	txbd1->tx_bd_cfa_action =
675 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
676 	txbd0 = txbd;
677 	for (i = 0; i < last_frag; i++) {
678 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
679 
680 		prod = NEXT_TX(prod);
681 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
682 
683 		len = skb_frag_size(frag);
684 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
685 					   DMA_TO_DEVICE);
686 
687 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
688 			goto tx_dma_error;
689 
690 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
691 		dma_unmap_addr_set(tx_buf, mapping, mapping);
692 
693 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
694 
695 		flags = len << TX_BD_LEN_SHIFT;
696 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
697 	}
698 
699 	flags &= ~TX_BD_LEN;
700 	txbd->tx_bd_len_flags_type =
701 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
702 			    TX_BD_FLAGS_PACKET_END);
703 
704 	netdev_tx_sent_queue(txq, skb->len);
705 
706 	skb_tx_timestamp(skb);
707 
708 	prod = NEXT_TX(prod);
709 	WRITE_ONCE(txr->tx_prod, prod);
710 
711 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
712 		bnxt_txr_db_kick(bp, txr, prod);
713 	} else {
714 		if (free_size >= bp->tx_wake_thresh)
715 			txbd0->tx_bd_len_flags_type |=
716 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
717 		txr->kick_pending = 1;
718 	}
719 
720 tx_done:
721 
722 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
723 		if (netdev_xmit_more() && !tx_buf->is_push) {
724 			txbd0->tx_bd_len_flags_type &=
725 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
726 			bnxt_txr_db_kick(bp, txr, prod);
727 		}
728 
729 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
730 				   bp->tx_wake_thresh);
731 	}
732 	return NETDEV_TX_OK;
733 
734 tx_dma_error:
735 	last_frag = i;
736 
737 	/* start back at beginning and unmap skb */
738 	prod = txr->tx_prod;
739 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
740 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
741 			 skb_headlen(skb), DMA_TO_DEVICE);
742 	prod = NEXT_TX(prod);
743 
744 	/* unmap remaining mapped pages */
745 	for (i = 0; i < last_frag; i++) {
746 		prod = NEXT_TX(prod);
747 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
748 		dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
749 			       skb_frag_size(&skb_shinfo(skb)->frags[i]),
750 			       DMA_TO_DEVICE);
751 	}
752 
753 tx_free:
754 	dev_kfree_skb_any(skb);
755 tx_kick_pending:
756 	if (BNXT_TX_PTP_IS_SET(lflags))
757 		atomic_inc(&bp->ptp_cfg->tx_avail);
758 	if (txr->kick_pending)
759 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
760 	txr->tx_buf_ring[txr->tx_prod].skb = NULL;
761 	dev_core_stats_tx_dropped_inc(dev);
762 	return NETDEV_TX_OK;
763 }
764 
765 static void __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
766 			  int budget)
767 {
768 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
769 	struct pci_dev *pdev = bp->pdev;
770 	u16 hw_cons = txr->tx_hw_cons;
771 	unsigned int tx_bytes = 0;
772 	u16 cons = txr->tx_cons;
773 	int tx_pkts = 0;
774 
775 	while (RING_TX(bp, cons) != hw_cons) {
776 		struct bnxt_sw_tx_bd *tx_buf;
777 		struct sk_buff *skb;
778 		int j, last;
779 
780 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
781 		cons = NEXT_TX(cons);
782 		skb = tx_buf->skb;
783 		tx_buf->skb = NULL;
784 
785 		if (unlikely(!skb)) {
786 			bnxt_sched_reset_txr(bp, txr, cons);
787 			return;
788 		}
789 
790 		tx_pkts++;
791 		tx_bytes += skb->len;
792 
793 		if (tx_buf->is_push) {
794 			tx_buf->is_push = 0;
795 			goto next_tx_int;
796 		}
797 
798 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
799 				 skb_headlen(skb), DMA_TO_DEVICE);
800 		last = tx_buf->nr_frags;
801 
802 		for (j = 0; j < last; j++) {
803 			cons = NEXT_TX(cons);
804 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
805 			dma_unmap_page(
806 				&pdev->dev,
807 				dma_unmap_addr(tx_buf, mapping),
808 				skb_frag_size(&skb_shinfo(skb)->frags[j]),
809 				DMA_TO_DEVICE);
810 		}
811 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
812 			if (BNXT_CHIP_P5(bp)) {
813 				/* PTP worker takes ownership of the skb */
814 				if (!bnxt_get_tx_ts_p5(bp, skb))
815 					skb = NULL;
816 				else
817 					atomic_inc(&bp->ptp_cfg->tx_avail);
818 			}
819 		}
820 
821 next_tx_int:
822 		cons = NEXT_TX(cons);
823 
824 		dev_consume_skb_any(skb);
825 	}
826 
827 	WRITE_ONCE(txr->tx_cons, cons);
828 
829 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
830 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
831 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
832 }
833 
834 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
835 {
836 	struct bnxt_tx_ring_info *txr;
837 	int i;
838 
839 	bnxt_for_each_napi_tx(i, bnapi, txr) {
840 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
841 			__bnxt_tx_int(bp, txr, budget);
842 	}
843 	bnapi->events &= ~BNXT_TX_CMP_EVENT;
844 }
845 
846 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
847 					 struct bnxt_rx_ring_info *rxr,
848 					 unsigned int *offset,
849 					 gfp_t gfp)
850 {
851 	struct page *page;
852 
853 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
854 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
855 						BNXT_RX_PAGE_SIZE);
856 	} else {
857 		page = page_pool_dev_alloc_pages(rxr->page_pool);
858 		*offset = 0;
859 	}
860 	if (!page)
861 		return NULL;
862 
863 	*mapping = page_pool_get_dma_addr(page) + *offset;
864 	return page;
865 }
866 
867 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
868 				       gfp_t gfp)
869 {
870 	u8 *data;
871 	struct pci_dev *pdev = bp->pdev;
872 
873 	if (gfp == GFP_ATOMIC)
874 		data = napi_alloc_frag(bp->rx_buf_size);
875 	else
876 		data = netdev_alloc_frag(bp->rx_buf_size);
877 	if (!data)
878 		return NULL;
879 
880 	*mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
881 					bp->rx_buf_use_size, bp->rx_dir,
882 					DMA_ATTR_WEAK_ORDERING);
883 
884 	if (dma_mapping_error(&pdev->dev, *mapping)) {
885 		skb_free_frag(data);
886 		data = NULL;
887 	}
888 	return data;
889 }
890 
891 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
892 		       u16 prod, gfp_t gfp)
893 {
894 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
895 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
896 	dma_addr_t mapping;
897 
898 	if (BNXT_RX_PAGE_MODE(bp)) {
899 		unsigned int offset;
900 		struct page *page =
901 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
902 
903 		if (!page)
904 			return -ENOMEM;
905 
906 		mapping += bp->rx_dma_offset;
907 		rx_buf->data = page;
908 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
909 	} else {
910 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, gfp);
911 
912 		if (!data)
913 			return -ENOMEM;
914 
915 		rx_buf->data = data;
916 		rx_buf->data_ptr = data + bp->rx_offset;
917 	}
918 	rx_buf->mapping = mapping;
919 
920 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
921 	return 0;
922 }
923 
924 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
925 {
926 	u16 prod = rxr->rx_prod;
927 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
928 	struct bnxt *bp = rxr->bnapi->bp;
929 	struct rx_bd *cons_bd, *prod_bd;
930 
931 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
932 	cons_rx_buf = &rxr->rx_buf_ring[cons];
933 
934 	prod_rx_buf->data = data;
935 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
936 
937 	prod_rx_buf->mapping = cons_rx_buf->mapping;
938 
939 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
940 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
941 
942 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
943 }
944 
945 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
946 {
947 	u16 next, max = rxr->rx_agg_bmap_size;
948 
949 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
950 	if (next >= max)
951 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
952 	return next;
953 }
954 
955 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
956 				     struct bnxt_rx_ring_info *rxr,
957 				     u16 prod, gfp_t gfp)
958 {
959 	struct rx_bd *rxbd =
960 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
961 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
962 	struct page *page;
963 	dma_addr_t mapping;
964 	u16 sw_prod = rxr->rx_sw_agg_prod;
965 	unsigned int offset = 0;
966 
967 	page = __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
968 
969 	if (!page)
970 		return -ENOMEM;
971 
972 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
973 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
974 
975 	__set_bit(sw_prod, rxr->rx_agg_bmap);
976 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
977 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
978 
979 	rx_agg_buf->page = page;
980 	rx_agg_buf->offset = offset;
981 	rx_agg_buf->mapping = mapping;
982 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
983 	rxbd->rx_bd_opaque = sw_prod;
984 	return 0;
985 }
986 
987 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
988 				       struct bnxt_cp_ring_info *cpr,
989 				       u16 cp_cons, u16 curr)
990 {
991 	struct rx_agg_cmp *agg;
992 
993 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
994 	agg = (struct rx_agg_cmp *)
995 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
996 	return agg;
997 }
998 
999 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1000 					      struct bnxt_rx_ring_info *rxr,
1001 					      u16 agg_id, u16 curr)
1002 {
1003 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1004 
1005 	return &tpa_info->agg_arr[curr];
1006 }
1007 
1008 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1009 				   u16 start, u32 agg_bufs, bool tpa)
1010 {
1011 	struct bnxt_napi *bnapi = cpr->bnapi;
1012 	struct bnxt *bp = bnapi->bp;
1013 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1014 	u16 prod = rxr->rx_agg_prod;
1015 	u16 sw_prod = rxr->rx_sw_agg_prod;
1016 	bool p5_tpa = false;
1017 	u32 i;
1018 
1019 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1020 		p5_tpa = true;
1021 
1022 	for (i = 0; i < agg_bufs; i++) {
1023 		u16 cons;
1024 		struct rx_agg_cmp *agg;
1025 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1026 		struct rx_bd *prod_bd;
1027 		struct page *page;
1028 
1029 		if (p5_tpa)
1030 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1031 		else
1032 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1033 		cons = agg->rx_agg_cmp_opaque;
1034 		__clear_bit(cons, rxr->rx_agg_bmap);
1035 
1036 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1037 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1038 
1039 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1040 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1041 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1042 
1043 		/* It is possible for sw_prod to be equal to cons, so
1044 		 * set cons_rx_buf->page to NULL first.
1045 		 */
1046 		page = cons_rx_buf->page;
1047 		cons_rx_buf->page = NULL;
1048 		prod_rx_buf->page = page;
1049 		prod_rx_buf->offset = cons_rx_buf->offset;
1050 
1051 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1052 
1053 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1054 
1055 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1056 		prod_bd->rx_bd_opaque = sw_prod;
1057 
1058 		prod = NEXT_RX_AGG(prod);
1059 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1060 	}
1061 	rxr->rx_agg_prod = prod;
1062 	rxr->rx_sw_agg_prod = sw_prod;
1063 }
1064 
1065 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1066 					      struct bnxt_rx_ring_info *rxr,
1067 					      u16 cons, void *data, u8 *data_ptr,
1068 					      dma_addr_t dma_addr,
1069 					      unsigned int offset_and_len)
1070 {
1071 	unsigned int len = offset_and_len & 0xffff;
1072 	struct page *page = data;
1073 	u16 prod = rxr->rx_prod;
1074 	struct sk_buff *skb;
1075 	int err;
1076 
1077 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1078 	if (unlikely(err)) {
1079 		bnxt_reuse_rx_data(rxr, cons, data);
1080 		return NULL;
1081 	}
1082 	dma_addr -= bp->rx_dma_offset;
1083 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1084 				bp->rx_dir);
1085 	skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE);
1086 	if (!skb) {
1087 		page_pool_recycle_direct(rxr->page_pool, page);
1088 		return NULL;
1089 	}
1090 	skb_mark_for_recycle(skb);
1091 	skb_reserve(skb, bp->rx_offset);
1092 	__skb_put(skb, len);
1093 
1094 	return skb;
1095 }
1096 
1097 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1098 					struct bnxt_rx_ring_info *rxr,
1099 					u16 cons, void *data, u8 *data_ptr,
1100 					dma_addr_t dma_addr,
1101 					unsigned int offset_and_len)
1102 {
1103 	unsigned int payload = offset_and_len >> 16;
1104 	unsigned int len = offset_and_len & 0xffff;
1105 	skb_frag_t *frag;
1106 	struct page *page = data;
1107 	u16 prod = rxr->rx_prod;
1108 	struct sk_buff *skb;
1109 	int off, err;
1110 
1111 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1112 	if (unlikely(err)) {
1113 		bnxt_reuse_rx_data(rxr, cons, data);
1114 		return NULL;
1115 	}
1116 	dma_addr -= bp->rx_dma_offset;
1117 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1118 				bp->rx_dir);
1119 
1120 	if (unlikely(!payload))
1121 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1122 
1123 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1124 	if (!skb) {
1125 		page_pool_recycle_direct(rxr->page_pool, page);
1126 		return NULL;
1127 	}
1128 
1129 	skb_mark_for_recycle(skb);
1130 	off = (void *)data_ptr - page_address(page);
1131 	skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE);
1132 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1133 	       payload + NET_IP_ALIGN);
1134 
1135 	frag = &skb_shinfo(skb)->frags[0];
1136 	skb_frag_size_sub(frag, payload);
1137 	skb_frag_off_add(frag, payload);
1138 	skb->data_len -= payload;
1139 	skb->tail += payload;
1140 
1141 	return skb;
1142 }
1143 
1144 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1145 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1146 				   void *data, u8 *data_ptr,
1147 				   dma_addr_t dma_addr,
1148 				   unsigned int offset_and_len)
1149 {
1150 	u16 prod = rxr->rx_prod;
1151 	struct sk_buff *skb;
1152 	int err;
1153 
1154 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1155 	if (unlikely(err)) {
1156 		bnxt_reuse_rx_data(rxr, cons, data);
1157 		return NULL;
1158 	}
1159 
1160 	skb = napi_build_skb(data, bp->rx_buf_size);
1161 	dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1162 			       bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
1163 	if (!skb) {
1164 		skb_free_frag(data);
1165 		return NULL;
1166 	}
1167 
1168 	skb_reserve(skb, bp->rx_offset);
1169 	skb_put(skb, offset_and_len & 0xffff);
1170 	return skb;
1171 }
1172 
1173 static u32 __bnxt_rx_agg_pages(struct bnxt *bp,
1174 			       struct bnxt_cp_ring_info *cpr,
1175 			       struct skb_shared_info *shinfo,
1176 			       u16 idx, u32 agg_bufs, bool tpa,
1177 			       struct xdp_buff *xdp)
1178 {
1179 	struct bnxt_napi *bnapi = cpr->bnapi;
1180 	struct pci_dev *pdev = bp->pdev;
1181 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1182 	u16 prod = rxr->rx_agg_prod;
1183 	u32 i, total_frag_len = 0;
1184 	bool p5_tpa = false;
1185 
1186 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1187 		p5_tpa = true;
1188 
1189 	for (i = 0; i < agg_bufs; i++) {
1190 		skb_frag_t *frag = &shinfo->frags[i];
1191 		u16 cons, frag_len;
1192 		struct rx_agg_cmp *agg;
1193 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1194 		struct page *page;
1195 		dma_addr_t mapping;
1196 
1197 		if (p5_tpa)
1198 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1199 		else
1200 			agg = bnxt_get_agg(bp, cpr, idx, i);
1201 		cons = agg->rx_agg_cmp_opaque;
1202 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1203 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1204 
1205 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1206 		skb_frag_fill_page_desc(frag, cons_rx_buf->page,
1207 					cons_rx_buf->offset, frag_len);
1208 		shinfo->nr_frags = i + 1;
1209 		__clear_bit(cons, rxr->rx_agg_bmap);
1210 
1211 		/* It is possible for bnxt_alloc_rx_page() to allocate
1212 		 * a sw_prod index that equals the cons index, so we
1213 		 * need to clear the cons entry now.
1214 		 */
1215 		mapping = cons_rx_buf->mapping;
1216 		page = cons_rx_buf->page;
1217 		cons_rx_buf->page = NULL;
1218 
1219 		if (xdp && page_is_pfmemalloc(page))
1220 			xdp_buff_set_frag_pfmemalloc(xdp);
1221 
1222 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1223 			--shinfo->nr_frags;
1224 			cons_rx_buf->page = page;
1225 
1226 			/* Update prod since possibly some pages have been
1227 			 * allocated already.
1228 			 */
1229 			rxr->rx_agg_prod = prod;
1230 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1231 			return 0;
1232 		}
1233 
1234 		dma_sync_single_for_cpu(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1235 					bp->rx_dir);
1236 
1237 		total_frag_len += frag_len;
1238 		prod = NEXT_RX_AGG(prod);
1239 	}
1240 	rxr->rx_agg_prod = prod;
1241 	return total_frag_len;
1242 }
1243 
1244 static struct sk_buff *bnxt_rx_agg_pages_skb(struct bnxt *bp,
1245 					     struct bnxt_cp_ring_info *cpr,
1246 					     struct sk_buff *skb, u16 idx,
1247 					     u32 agg_bufs, bool tpa)
1248 {
1249 	struct skb_shared_info *shinfo = skb_shinfo(skb);
1250 	u32 total_frag_len = 0;
1251 
1252 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo, idx,
1253 					     agg_bufs, tpa, NULL);
1254 	if (!total_frag_len) {
1255 		skb_mark_for_recycle(skb);
1256 		dev_kfree_skb(skb);
1257 		return NULL;
1258 	}
1259 
1260 	skb->data_len += total_frag_len;
1261 	skb->len += total_frag_len;
1262 	skb->truesize += BNXT_RX_PAGE_SIZE * agg_bufs;
1263 	return skb;
1264 }
1265 
1266 static u32 bnxt_rx_agg_pages_xdp(struct bnxt *bp,
1267 				 struct bnxt_cp_ring_info *cpr,
1268 				 struct xdp_buff *xdp, u16 idx,
1269 				 u32 agg_bufs, bool tpa)
1270 {
1271 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1272 	u32 total_frag_len = 0;
1273 
1274 	if (!xdp_buff_has_frags(xdp))
1275 		shinfo->nr_frags = 0;
1276 
1277 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo,
1278 					     idx, agg_bufs, tpa, xdp);
1279 	if (total_frag_len) {
1280 		xdp_buff_set_frags_flag(xdp);
1281 		shinfo->nr_frags = agg_bufs;
1282 		shinfo->xdp_frags_size = total_frag_len;
1283 	}
1284 	return total_frag_len;
1285 }
1286 
1287 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1288 			       u8 agg_bufs, u32 *raw_cons)
1289 {
1290 	u16 last;
1291 	struct rx_agg_cmp *agg;
1292 
1293 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1294 	last = RING_CMP(*raw_cons);
1295 	agg = (struct rx_agg_cmp *)
1296 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1297 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1298 }
1299 
1300 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1301 				      unsigned int len,
1302 				      dma_addr_t mapping)
1303 {
1304 	struct bnxt *bp = bnapi->bp;
1305 	struct pci_dev *pdev = bp->pdev;
1306 	struct sk_buff *skb;
1307 
1308 	skb = napi_alloc_skb(&bnapi->napi, len);
1309 	if (!skb)
1310 		return NULL;
1311 
1312 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1313 				bp->rx_dir);
1314 
1315 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1316 	       len + NET_IP_ALIGN);
1317 
1318 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1319 				   bp->rx_dir);
1320 
1321 	skb_put(skb, len);
1322 
1323 	return skb;
1324 }
1325 
1326 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1327 				     unsigned int len,
1328 				     dma_addr_t mapping)
1329 {
1330 	return bnxt_copy_data(bnapi, data, len, mapping);
1331 }
1332 
1333 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1334 				     struct xdp_buff *xdp,
1335 				     unsigned int len,
1336 				     dma_addr_t mapping)
1337 {
1338 	unsigned int metasize = 0;
1339 	u8 *data = xdp->data;
1340 	struct sk_buff *skb;
1341 
1342 	len = xdp->data_end - xdp->data_meta;
1343 	metasize = xdp->data - xdp->data_meta;
1344 	data = xdp->data_meta;
1345 
1346 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1347 	if (!skb)
1348 		return skb;
1349 
1350 	if (metasize) {
1351 		skb_metadata_set(skb, metasize);
1352 		__skb_pull(skb, metasize);
1353 	}
1354 
1355 	return skb;
1356 }
1357 
1358 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1359 			   u32 *raw_cons, void *cmp)
1360 {
1361 	struct rx_cmp *rxcmp = cmp;
1362 	u32 tmp_raw_cons = *raw_cons;
1363 	u8 cmp_type, agg_bufs = 0;
1364 
1365 	cmp_type = RX_CMP_TYPE(rxcmp);
1366 
1367 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1368 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1369 			    RX_CMP_AGG_BUFS) >>
1370 			   RX_CMP_AGG_BUFS_SHIFT;
1371 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1372 		struct rx_tpa_end_cmp *tpa_end = cmp;
1373 
1374 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1375 			return 0;
1376 
1377 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1378 	}
1379 
1380 	if (agg_bufs) {
1381 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1382 			return -EBUSY;
1383 	}
1384 	*raw_cons = tmp_raw_cons;
1385 	return 0;
1386 }
1387 
1388 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1389 {
1390 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1391 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1392 
1393 	if (test_bit(idx, map->agg_idx_bmap))
1394 		idx = find_first_zero_bit(map->agg_idx_bmap,
1395 					  BNXT_AGG_IDX_BMAP_SIZE);
1396 	__set_bit(idx, map->agg_idx_bmap);
1397 	map->agg_id_tbl[agg_id] = idx;
1398 	return idx;
1399 }
1400 
1401 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1402 {
1403 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1404 
1405 	__clear_bit(idx, map->agg_idx_bmap);
1406 }
1407 
1408 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1409 {
1410 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1411 
1412 	return map->agg_id_tbl[agg_id];
1413 }
1414 
1415 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1416 			      struct rx_tpa_start_cmp *tpa_start,
1417 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1418 {
1419 	tpa_info->cfa_code_valid = 1;
1420 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1421 	tpa_info->vlan_valid = 0;
1422 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1423 		tpa_info->vlan_valid = 1;
1424 		tpa_info->metadata =
1425 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1426 	}
1427 }
1428 
1429 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1430 				 struct rx_tpa_start_cmp *tpa_start,
1431 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1432 {
1433 	tpa_info->vlan_valid = 0;
1434 	if (TPA_START_VLAN_VALID(tpa_start)) {
1435 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1436 		u32 vlan_proto = ETH_P_8021Q;
1437 
1438 		tpa_info->vlan_valid = 1;
1439 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1440 			vlan_proto = ETH_P_8021AD;
1441 		tpa_info->metadata = vlan_proto << 16 |
1442 				     TPA_START_METADATA0_TCI(tpa_start1);
1443 	}
1444 }
1445 
1446 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1447 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1448 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1449 {
1450 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1451 	struct bnxt_tpa_info *tpa_info;
1452 	u16 cons, prod, agg_id;
1453 	struct rx_bd *prod_bd;
1454 	dma_addr_t mapping;
1455 
1456 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1457 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1458 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1459 	} else {
1460 		agg_id = TPA_START_AGG_ID(tpa_start);
1461 	}
1462 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1463 	prod = rxr->rx_prod;
1464 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1465 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1466 	tpa_info = &rxr->rx_tpa[agg_id];
1467 
1468 	if (unlikely(cons != rxr->rx_next_cons ||
1469 		     TPA_START_ERROR(tpa_start))) {
1470 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1471 			    cons, rxr->rx_next_cons,
1472 			    TPA_START_ERROR_CODE(tpa_start1));
1473 		bnxt_sched_reset_rxr(bp, rxr);
1474 		return;
1475 	}
1476 	prod_rx_buf->data = tpa_info->data;
1477 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1478 
1479 	mapping = tpa_info->mapping;
1480 	prod_rx_buf->mapping = mapping;
1481 
1482 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1483 
1484 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1485 
1486 	tpa_info->data = cons_rx_buf->data;
1487 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1488 	cons_rx_buf->data = NULL;
1489 	tpa_info->mapping = cons_rx_buf->mapping;
1490 
1491 	tpa_info->len =
1492 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1493 				RX_TPA_START_CMP_LEN_SHIFT;
1494 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1495 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1496 		tpa_info->gso_type = SKB_GSO_TCPV4;
1497 		if (TPA_START_IS_IPV6(tpa_start1))
1498 			tpa_info->gso_type = SKB_GSO_TCPV6;
1499 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1500 		else if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP &&
1501 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1502 			tpa_info->gso_type = SKB_GSO_TCPV6;
1503 		tpa_info->rss_hash =
1504 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1505 	} else {
1506 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1507 		tpa_info->gso_type = 0;
1508 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1509 	}
1510 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1511 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1512 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1513 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1514 	else
1515 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1516 	tpa_info->agg_count = 0;
1517 
1518 	rxr->rx_prod = NEXT_RX(prod);
1519 	cons = RING_RX(bp, NEXT_RX(cons));
1520 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1521 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1522 
1523 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1524 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1525 	cons_rx_buf->data = NULL;
1526 }
1527 
1528 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1529 {
1530 	if (agg_bufs)
1531 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1532 }
1533 
1534 #ifdef CONFIG_INET
1535 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1536 {
1537 	struct udphdr *uh = NULL;
1538 
1539 	if (ip_proto == htons(ETH_P_IP)) {
1540 		struct iphdr *iph = (struct iphdr *)skb->data;
1541 
1542 		if (iph->protocol == IPPROTO_UDP)
1543 			uh = (struct udphdr *)(iph + 1);
1544 	} else {
1545 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1546 
1547 		if (iph->nexthdr == IPPROTO_UDP)
1548 			uh = (struct udphdr *)(iph + 1);
1549 	}
1550 	if (uh) {
1551 		if (uh->check)
1552 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1553 		else
1554 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1555 	}
1556 }
1557 #endif
1558 
1559 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1560 					   int payload_off, int tcp_ts,
1561 					   struct sk_buff *skb)
1562 {
1563 #ifdef CONFIG_INET
1564 	struct tcphdr *th;
1565 	int len, nw_off;
1566 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1567 	u32 hdr_info = tpa_info->hdr_info;
1568 	bool loopback = false;
1569 
1570 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1571 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1572 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1573 
1574 	/* If the packet is an internal loopback packet, the offsets will
1575 	 * have an extra 4 bytes.
1576 	 */
1577 	if (inner_mac_off == 4) {
1578 		loopback = true;
1579 	} else if (inner_mac_off > 4) {
1580 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1581 					    ETH_HLEN - 2));
1582 
1583 		/* We only support inner iPv4/ipv6.  If we don't see the
1584 		 * correct protocol ID, it must be a loopback packet where
1585 		 * the offsets are off by 4.
1586 		 */
1587 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1588 			loopback = true;
1589 	}
1590 	if (loopback) {
1591 		/* internal loopback packet, subtract all offsets by 4 */
1592 		inner_ip_off -= 4;
1593 		inner_mac_off -= 4;
1594 		outer_ip_off -= 4;
1595 	}
1596 
1597 	nw_off = inner_ip_off - ETH_HLEN;
1598 	skb_set_network_header(skb, nw_off);
1599 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1600 		struct ipv6hdr *iph = ipv6_hdr(skb);
1601 
1602 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1603 		len = skb->len - skb_transport_offset(skb);
1604 		th = tcp_hdr(skb);
1605 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1606 	} else {
1607 		struct iphdr *iph = ip_hdr(skb);
1608 
1609 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1610 		len = skb->len - skb_transport_offset(skb);
1611 		th = tcp_hdr(skb);
1612 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1613 	}
1614 
1615 	if (inner_mac_off) { /* tunnel */
1616 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1617 					    ETH_HLEN - 2));
1618 
1619 		bnxt_gro_tunnel(skb, proto);
1620 	}
1621 #endif
1622 	return skb;
1623 }
1624 
1625 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1626 					   int payload_off, int tcp_ts,
1627 					   struct sk_buff *skb)
1628 {
1629 #ifdef CONFIG_INET
1630 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1631 	u32 hdr_info = tpa_info->hdr_info;
1632 	int iphdr_len, nw_off;
1633 
1634 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1635 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1636 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1637 
1638 	nw_off = inner_ip_off - ETH_HLEN;
1639 	skb_set_network_header(skb, nw_off);
1640 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1641 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1642 	skb_set_transport_header(skb, nw_off + iphdr_len);
1643 
1644 	if (inner_mac_off) { /* tunnel */
1645 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1646 					    ETH_HLEN - 2));
1647 
1648 		bnxt_gro_tunnel(skb, proto);
1649 	}
1650 #endif
1651 	return skb;
1652 }
1653 
1654 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1655 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1656 
1657 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1658 					   int payload_off, int tcp_ts,
1659 					   struct sk_buff *skb)
1660 {
1661 #ifdef CONFIG_INET
1662 	struct tcphdr *th;
1663 	int len, nw_off, tcp_opt_len = 0;
1664 
1665 	if (tcp_ts)
1666 		tcp_opt_len = 12;
1667 
1668 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1669 		struct iphdr *iph;
1670 
1671 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1672 			 ETH_HLEN;
1673 		skb_set_network_header(skb, nw_off);
1674 		iph = ip_hdr(skb);
1675 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1676 		len = skb->len - skb_transport_offset(skb);
1677 		th = tcp_hdr(skb);
1678 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1679 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1680 		struct ipv6hdr *iph;
1681 
1682 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1683 			 ETH_HLEN;
1684 		skb_set_network_header(skb, nw_off);
1685 		iph = ipv6_hdr(skb);
1686 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1687 		len = skb->len - skb_transport_offset(skb);
1688 		th = tcp_hdr(skb);
1689 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1690 	} else {
1691 		dev_kfree_skb_any(skb);
1692 		return NULL;
1693 	}
1694 
1695 	if (nw_off) /* tunnel */
1696 		bnxt_gro_tunnel(skb, skb->protocol);
1697 #endif
1698 	return skb;
1699 }
1700 
1701 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1702 					   struct bnxt_tpa_info *tpa_info,
1703 					   struct rx_tpa_end_cmp *tpa_end,
1704 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1705 					   struct sk_buff *skb)
1706 {
1707 #ifdef CONFIG_INET
1708 	int payload_off;
1709 	u16 segs;
1710 
1711 	segs = TPA_END_TPA_SEGS(tpa_end);
1712 	if (segs == 1)
1713 		return skb;
1714 
1715 	NAPI_GRO_CB(skb)->count = segs;
1716 	skb_shinfo(skb)->gso_size =
1717 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1718 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1719 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1720 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1721 	else
1722 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1723 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1724 	if (likely(skb))
1725 		tcp_gro_complete(skb);
1726 #endif
1727 	return skb;
1728 }
1729 
1730 /* Given the cfa_code of a received packet determine which
1731  * netdev (vf-rep or PF) the packet is destined to.
1732  */
1733 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1734 {
1735 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1736 
1737 	/* if vf-rep dev is NULL, the must belongs to the PF */
1738 	return dev ? dev : bp->dev;
1739 }
1740 
1741 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1742 					   struct bnxt_cp_ring_info *cpr,
1743 					   u32 *raw_cons,
1744 					   struct rx_tpa_end_cmp *tpa_end,
1745 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1746 					   u8 *event)
1747 {
1748 	struct bnxt_napi *bnapi = cpr->bnapi;
1749 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1750 	struct net_device *dev = bp->dev;
1751 	u8 *data_ptr, agg_bufs;
1752 	unsigned int len;
1753 	struct bnxt_tpa_info *tpa_info;
1754 	dma_addr_t mapping;
1755 	struct sk_buff *skb;
1756 	u16 idx = 0, agg_id;
1757 	void *data;
1758 	bool gro;
1759 
1760 	if (unlikely(bnapi->in_reset)) {
1761 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1762 
1763 		if (rc < 0)
1764 			return ERR_PTR(-EBUSY);
1765 		return NULL;
1766 	}
1767 
1768 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1769 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1770 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1771 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1772 		tpa_info = &rxr->rx_tpa[agg_id];
1773 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1774 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1775 				    agg_bufs, tpa_info->agg_count);
1776 			agg_bufs = tpa_info->agg_count;
1777 		}
1778 		tpa_info->agg_count = 0;
1779 		*event |= BNXT_AGG_EVENT;
1780 		bnxt_free_agg_idx(rxr, agg_id);
1781 		idx = agg_id;
1782 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1783 	} else {
1784 		agg_id = TPA_END_AGG_ID(tpa_end);
1785 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1786 		tpa_info = &rxr->rx_tpa[agg_id];
1787 		idx = RING_CMP(*raw_cons);
1788 		if (agg_bufs) {
1789 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1790 				return ERR_PTR(-EBUSY);
1791 
1792 			*event |= BNXT_AGG_EVENT;
1793 			idx = NEXT_CMP(idx);
1794 		}
1795 		gro = !!TPA_END_GRO(tpa_end);
1796 	}
1797 	data = tpa_info->data;
1798 	data_ptr = tpa_info->data_ptr;
1799 	prefetch(data_ptr);
1800 	len = tpa_info->len;
1801 	mapping = tpa_info->mapping;
1802 
1803 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1804 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1805 		if (agg_bufs > MAX_SKB_FRAGS)
1806 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1807 				    agg_bufs, (int)MAX_SKB_FRAGS);
1808 		return NULL;
1809 	}
1810 
1811 	if (len <= bp->rx_copy_thresh) {
1812 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1813 		if (!skb) {
1814 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1815 			cpr->sw_stats->rx.rx_oom_discards += 1;
1816 			return NULL;
1817 		}
1818 	} else {
1819 		u8 *new_data;
1820 		dma_addr_t new_mapping;
1821 
1822 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, GFP_ATOMIC);
1823 		if (!new_data) {
1824 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1825 			cpr->sw_stats->rx.rx_oom_discards += 1;
1826 			return NULL;
1827 		}
1828 
1829 		tpa_info->data = new_data;
1830 		tpa_info->data_ptr = new_data + bp->rx_offset;
1831 		tpa_info->mapping = new_mapping;
1832 
1833 		skb = napi_build_skb(data, bp->rx_buf_size);
1834 		dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1835 				       bp->rx_buf_use_size, bp->rx_dir,
1836 				       DMA_ATTR_WEAK_ORDERING);
1837 
1838 		if (!skb) {
1839 			skb_free_frag(data);
1840 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1841 			cpr->sw_stats->rx.rx_oom_discards += 1;
1842 			return NULL;
1843 		}
1844 		skb_reserve(skb, bp->rx_offset);
1845 		skb_put(skb, len);
1846 	}
1847 
1848 	if (agg_bufs) {
1849 		skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, idx, agg_bufs, true);
1850 		if (!skb) {
1851 			/* Page reuse already handled by bnxt_rx_pages(). */
1852 			cpr->sw_stats->rx.rx_oom_discards += 1;
1853 			return NULL;
1854 		}
1855 	}
1856 
1857 	if (tpa_info->cfa_code_valid)
1858 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1859 	skb->protocol = eth_type_trans(skb, dev);
1860 
1861 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1862 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1863 
1864 	if (tpa_info->vlan_valid &&
1865 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1866 		__be16 vlan_proto = htons(tpa_info->metadata >>
1867 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1868 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1869 
1870 		if (eth_type_vlan(vlan_proto)) {
1871 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1872 		} else {
1873 			dev_kfree_skb(skb);
1874 			return NULL;
1875 		}
1876 	}
1877 
1878 	skb_checksum_none_assert(skb);
1879 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1880 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1881 		skb->csum_level =
1882 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1883 	}
1884 
1885 	if (gro)
1886 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1887 
1888 	return skb;
1889 }
1890 
1891 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1892 			 struct rx_agg_cmp *rx_agg)
1893 {
1894 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1895 	struct bnxt_tpa_info *tpa_info;
1896 
1897 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1898 	tpa_info = &rxr->rx_tpa[agg_id];
1899 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1900 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1901 }
1902 
1903 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1904 			     struct sk_buff *skb)
1905 {
1906 	skb_mark_for_recycle(skb);
1907 
1908 	if (skb->dev != bp->dev) {
1909 		/* this packet belongs to a vf-rep */
1910 		bnxt_vf_rep_rx(bp, skb);
1911 		return;
1912 	}
1913 	skb_record_rx_queue(skb, bnapi->index);
1914 	napi_gro_receive(&bnapi->napi, skb);
1915 }
1916 
1917 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
1918 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
1919 {
1920 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
1921 
1922 	if (BNXT_PTP_RX_TS_VALID(flags))
1923 		goto ts_valid;
1924 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
1925 		return false;
1926 
1927 ts_valid:
1928 	*cmpl_ts = ts;
1929 	return true;
1930 }
1931 
1932 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
1933 				    struct rx_cmp *rxcmp,
1934 				    struct rx_cmp_ext *rxcmp1)
1935 {
1936 	__be16 vlan_proto;
1937 	u16 vtag;
1938 
1939 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1940 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
1941 		u32 meta_data;
1942 
1943 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
1944 			return skb;
1945 
1946 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1947 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1948 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
1949 		if (eth_type_vlan(vlan_proto))
1950 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1951 		else
1952 			goto vlan_err;
1953 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
1954 		if (RX_CMP_VLAN_VALID(rxcmp)) {
1955 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
1956 
1957 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
1958 				vlan_proto = htons(ETH_P_8021Q);
1959 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
1960 				vlan_proto = htons(ETH_P_8021AD);
1961 			else
1962 				goto vlan_err;
1963 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
1964 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1965 		}
1966 	}
1967 	return skb;
1968 vlan_err:
1969 	dev_kfree_skb(skb);
1970 	return NULL;
1971 }
1972 
1973 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp,
1974 					   struct rx_cmp *rxcmp)
1975 {
1976 	u8 ext_op;
1977 
1978 	ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp);
1979 	switch (ext_op) {
1980 	case EXT_OP_INNER_4:
1981 	case EXT_OP_OUTER_4:
1982 	case EXT_OP_INNFL_3:
1983 	case EXT_OP_OUTFL_3:
1984 		return PKT_HASH_TYPE_L4;
1985 	default:
1986 		return PKT_HASH_TYPE_L3;
1987 	}
1988 }
1989 
1990 /* returns the following:
1991  * 1       - 1 packet successfully received
1992  * 0       - successful TPA_START, packet not completed yet
1993  * -EBUSY  - completion ring does not have all the agg buffers yet
1994  * -ENOMEM - packet aborted due to out of memory
1995  * -EIO    - packet aborted due to hw error indicated in BD
1996  */
1997 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1998 		       u32 *raw_cons, u8 *event)
1999 {
2000 	struct bnxt_napi *bnapi = cpr->bnapi;
2001 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2002 	struct net_device *dev = bp->dev;
2003 	struct rx_cmp *rxcmp;
2004 	struct rx_cmp_ext *rxcmp1;
2005 	u32 tmp_raw_cons = *raw_cons;
2006 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2007 	struct bnxt_sw_rx_bd *rx_buf;
2008 	unsigned int len;
2009 	u8 *data_ptr, agg_bufs, cmp_type;
2010 	bool xdp_active = false;
2011 	dma_addr_t dma_addr;
2012 	struct sk_buff *skb;
2013 	struct xdp_buff xdp;
2014 	u32 flags, misc;
2015 	u32 cmpl_ts;
2016 	void *data;
2017 	int rc = 0;
2018 
2019 	rxcmp = (struct rx_cmp *)
2020 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2021 
2022 	cmp_type = RX_CMP_TYPE(rxcmp);
2023 
2024 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2025 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2026 		goto next_rx_no_prod_no_len;
2027 	}
2028 
2029 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2030 	cp_cons = RING_CMP(tmp_raw_cons);
2031 	rxcmp1 = (struct rx_cmp_ext *)
2032 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2033 
2034 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2035 		return -EBUSY;
2036 
2037 	/* The valid test of the entry must be done first before
2038 	 * reading any further.
2039 	 */
2040 	dma_rmb();
2041 	prod = rxr->rx_prod;
2042 
2043 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2044 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2045 		bnxt_tpa_start(bp, rxr, cmp_type,
2046 			       (struct rx_tpa_start_cmp *)rxcmp,
2047 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2048 
2049 		*event |= BNXT_RX_EVENT;
2050 		goto next_rx_no_prod_no_len;
2051 
2052 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2053 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2054 				   (struct rx_tpa_end_cmp *)rxcmp,
2055 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2056 
2057 		if (IS_ERR(skb))
2058 			return -EBUSY;
2059 
2060 		rc = -ENOMEM;
2061 		if (likely(skb)) {
2062 			bnxt_deliver_skb(bp, bnapi, skb);
2063 			rc = 1;
2064 		}
2065 		*event |= BNXT_RX_EVENT;
2066 		goto next_rx_no_prod_no_len;
2067 	}
2068 
2069 	cons = rxcmp->rx_cmp_opaque;
2070 	if (unlikely(cons != rxr->rx_next_cons)) {
2071 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2072 
2073 		/* 0xffff is forced error, don't print it */
2074 		if (rxr->rx_next_cons != 0xffff)
2075 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2076 				    cons, rxr->rx_next_cons);
2077 		bnxt_sched_reset_rxr(bp, rxr);
2078 		if (rc1)
2079 			return rc1;
2080 		goto next_rx_no_prod_no_len;
2081 	}
2082 	rx_buf = &rxr->rx_buf_ring[cons];
2083 	data = rx_buf->data;
2084 	data_ptr = rx_buf->data_ptr;
2085 	prefetch(data_ptr);
2086 
2087 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2088 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2089 
2090 	if (agg_bufs) {
2091 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2092 			return -EBUSY;
2093 
2094 		cp_cons = NEXT_CMP(cp_cons);
2095 		*event |= BNXT_AGG_EVENT;
2096 	}
2097 	*event |= BNXT_RX_EVENT;
2098 
2099 	rx_buf->data = NULL;
2100 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2101 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2102 
2103 		bnxt_reuse_rx_data(rxr, cons, data);
2104 		if (agg_bufs)
2105 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2106 					       false);
2107 
2108 		rc = -EIO;
2109 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2110 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2111 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2112 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2113 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2114 						 rx_err);
2115 				bnxt_sched_reset_rxr(bp, rxr);
2116 			}
2117 		}
2118 		goto next_rx_no_len;
2119 	}
2120 
2121 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2122 	len = flags >> RX_CMP_LEN_SHIFT;
2123 	dma_addr = rx_buf->mapping;
2124 
2125 	if (bnxt_xdp_attached(bp, rxr)) {
2126 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2127 		if (agg_bufs) {
2128 			u32 frag_len = bnxt_rx_agg_pages_xdp(bp, cpr, &xdp,
2129 							     cp_cons, agg_bufs,
2130 							     false);
2131 			if (!frag_len)
2132 				goto oom_next_rx;
2133 		}
2134 		xdp_active = true;
2135 	}
2136 
2137 	if (xdp_active) {
2138 		if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) {
2139 			rc = 1;
2140 			goto next_rx;
2141 		}
2142 	}
2143 
2144 	if (len <= bp->rx_copy_thresh) {
2145 		if (!xdp_active)
2146 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2147 		else
2148 			skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr);
2149 		bnxt_reuse_rx_data(rxr, cons, data);
2150 		if (!skb) {
2151 			if (agg_bufs) {
2152 				if (!xdp_active)
2153 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2154 							       agg_bufs, false);
2155 				else
2156 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2157 			}
2158 			goto oom_next_rx;
2159 		}
2160 	} else {
2161 		u32 payload;
2162 
2163 		if (rx_buf->data_ptr == data_ptr)
2164 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2165 		else
2166 			payload = 0;
2167 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2168 				      payload | len);
2169 		if (!skb)
2170 			goto oom_next_rx;
2171 	}
2172 
2173 	if (agg_bufs) {
2174 		if (!xdp_active) {
2175 			skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, cp_cons, agg_bufs, false);
2176 			if (!skb)
2177 				goto oom_next_rx;
2178 		} else {
2179 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, rxr->page_pool, &xdp, rxcmp1);
2180 			if (!skb) {
2181 				/* we should be able to free the old skb here */
2182 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2183 				goto oom_next_rx;
2184 			}
2185 		}
2186 	}
2187 
2188 	if (RX_CMP_HASH_VALID(rxcmp)) {
2189 		enum pkt_hash_types type;
2190 
2191 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2192 			type = bnxt_rss_ext_op(bp, rxcmp);
2193 		} else {
2194 			u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
2195 
2196 			/* RSS profiles 1 and 3 with extract code 0 for inner
2197 			 * 4-tuple
2198 			 */
2199 			if (hash_type != 1 && hash_type != 3)
2200 				type = PKT_HASH_TYPE_L3;
2201 			else
2202 				type = PKT_HASH_TYPE_L4;
2203 		}
2204 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2205 	}
2206 
2207 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2208 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2209 	skb->protocol = eth_type_trans(skb, dev);
2210 
2211 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2212 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2213 		if (!skb)
2214 			goto next_rx;
2215 	}
2216 
2217 	skb_checksum_none_assert(skb);
2218 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2219 		if (dev->features & NETIF_F_RXCSUM) {
2220 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2221 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2222 		}
2223 	} else {
2224 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2225 			if (dev->features & NETIF_F_RXCSUM)
2226 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2227 		}
2228 	}
2229 
2230 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2231 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2232 			u64 ns, ts;
2233 
2234 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2235 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2236 
2237 				spin_lock_bh(&ptp->ptp_lock);
2238 				ns = timecounter_cyc2time(&ptp->tc, ts);
2239 				spin_unlock_bh(&ptp->ptp_lock);
2240 				memset(skb_hwtstamps(skb), 0,
2241 				       sizeof(*skb_hwtstamps(skb)));
2242 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2243 			}
2244 		}
2245 	}
2246 	bnxt_deliver_skb(bp, bnapi, skb);
2247 	rc = 1;
2248 
2249 next_rx:
2250 	cpr->rx_packets += 1;
2251 	cpr->rx_bytes += len;
2252 
2253 next_rx_no_len:
2254 	rxr->rx_prod = NEXT_RX(prod);
2255 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2256 
2257 next_rx_no_prod_no_len:
2258 	*raw_cons = tmp_raw_cons;
2259 
2260 	return rc;
2261 
2262 oom_next_rx:
2263 	cpr->sw_stats->rx.rx_oom_discards += 1;
2264 	rc = -ENOMEM;
2265 	goto next_rx;
2266 }
2267 
2268 /* In netpoll mode, if we are using a combined completion ring, we need to
2269  * discard the rx packets and recycle the buffers.
2270  */
2271 static int bnxt_force_rx_discard(struct bnxt *bp,
2272 				 struct bnxt_cp_ring_info *cpr,
2273 				 u32 *raw_cons, u8 *event)
2274 {
2275 	u32 tmp_raw_cons = *raw_cons;
2276 	struct rx_cmp_ext *rxcmp1;
2277 	struct rx_cmp *rxcmp;
2278 	u16 cp_cons;
2279 	u8 cmp_type;
2280 	int rc;
2281 
2282 	cp_cons = RING_CMP(tmp_raw_cons);
2283 	rxcmp = (struct rx_cmp *)
2284 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2285 
2286 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2287 	cp_cons = RING_CMP(tmp_raw_cons);
2288 	rxcmp1 = (struct rx_cmp_ext *)
2289 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2290 
2291 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2292 		return -EBUSY;
2293 
2294 	/* The valid test of the entry must be done first before
2295 	 * reading any further.
2296 	 */
2297 	dma_rmb();
2298 	cmp_type = RX_CMP_TYPE(rxcmp);
2299 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2300 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2301 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2302 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2303 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2304 		struct rx_tpa_end_cmp_ext *tpa_end1;
2305 
2306 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2307 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2308 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2309 	}
2310 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2311 	if (rc && rc != -EBUSY)
2312 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2313 	return rc;
2314 }
2315 
2316 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2317 {
2318 	struct bnxt_fw_health *fw_health = bp->fw_health;
2319 	u32 reg = fw_health->regs[reg_idx];
2320 	u32 reg_type, reg_off, val = 0;
2321 
2322 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2323 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2324 	switch (reg_type) {
2325 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2326 		pci_read_config_dword(bp->pdev, reg_off, &val);
2327 		break;
2328 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2329 		reg_off = fw_health->mapped_regs[reg_idx];
2330 		fallthrough;
2331 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2332 		val = readl(bp->bar0 + reg_off);
2333 		break;
2334 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2335 		val = readl(bp->bar1 + reg_off);
2336 		break;
2337 	}
2338 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2339 		val &= fw_health->fw_reset_inprog_reg_mask;
2340 	return val;
2341 }
2342 
2343 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2344 {
2345 	int i;
2346 
2347 	for (i = 0; i < bp->rx_nr_rings; i++) {
2348 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2349 		struct bnxt_ring_grp_info *grp_info;
2350 
2351 		grp_info = &bp->grp_info[grp_idx];
2352 		if (grp_info->agg_fw_ring_id == ring_id)
2353 			return grp_idx;
2354 	}
2355 	return INVALID_HW_RING_ID;
2356 }
2357 
2358 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2359 {
2360 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2361 
2362 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2363 		return link_info->force_link_speed2;
2364 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2365 		return link_info->force_pam4_link_speed;
2366 	return link_info->force_link_speed;
2367 }
2368 
2369 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2370 {
2371 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2372 
2373 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2374 		link_info->req_link_speed = link_info->force_link_speed2;
2375 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2376 		switch (link_info->req_link_speed) {
2377 		case BNXT_LINK_SPEED_50GB_PAM4:
2378 		case BNXT_LINK_SPEED_100GB_PAM4:
2379 		case BNXT_LINK_SPEED_200GB_PAM4:
2380 		case BNXT_LINK_SPEED_400GB_PAM4:
2381 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2382 			break;
2383 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2384 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2385 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2386 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2387 			break;
2388 		default:
2389 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2390 		}
2391 		return;
2392 	}
2393 	link_info->req_link_speed = link_info->force_link_speed;
2394 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2395 	if (link_info->force_pam4_link_speed) {
2396 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2397 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2398 	}
2399 }
2400 
2401 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2402 {
2403 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2404 
2405 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2406 		link_info->advertising = link_info->auto_link_speeds2;
2407 		return;
2408 	}
2409 	link_info->advertising = link_info->auto_link_speeds;
2410 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2411 }
2412 
2413 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2414 {
2415 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2416 
2417 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2418 		if (link_info->req_link_speed != link_info->force_link_speed2)
2419 			return true;
2420 		return false;
2421 	}
2422 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2423 	    link_info->req_link_speed != link_info->force_link_speed)
2424 		return true;
2425 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2426 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2427 		return true;
2428 	return false;
2429 }
2430 
2431 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2432 {
2433 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2434 
2435 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2436 		if (link_info->advertising != link_info->auto_link_speeds2)
2437 			return true;
2438 		return false;
2439 	}
2440 	if (link_info->advertising != link_info->auto_link_speeds ||
2441 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2442 		return true;
2443 	return false;
2444 }
2445 
2446 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2447 	((data2) &							\
2448 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2449 
2450 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2451 	(((data2) &							\
2452 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2453 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2454 
2455 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2456 	((data1) &							\
2457 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2458 
2459 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2460 	(((data1) &							\
2461 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2462 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2463 
2464 /* Return true if the workqueue has to be scheduled */
2465 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2466 {
2467 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2468 
2469 	switch (err_type) {
2470 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2471 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2472 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2473 		break;
2474 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2475 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2476 		break;
2477 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2478 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2479 		break;
2480 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2481 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2482 		char *threshold_type;
2483 		bool notify = false;
2484 		char *dir_str;
2485 
2486 		switch (type) {
2487 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2488 			threshold_type = "warning";
2489 			break;
2490 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2491 			threshold_type = "critical";
2492 			break;
2493 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2494 			threshold_type = "fatal";
2495 			break;
2496 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2497 			threshold_type = "shutdown";
2498 			break;
2499 		default:
2500 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2501 			return false;
2502 		}
2503 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2504 			dir_str = "above";
2505 			notify = true;
2506 		} else {
2507 			dir_str = "below";
2508 		}
2509 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2510 			    dir_str, threshold_type);
2511 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2512 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2513 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2514 		if (notify) {
2515 			bp->thermal_threshold_type = type;
2516 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2517 			return true;
2518 		}
2519 		return false;
2520 	}
2521 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2522 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2523 		break;
2524 	default:
2525 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2526 			   err_type);
2527 		break;
2528 	}
2529 	return false;
2530 }
2531 
2532 #define BNXT_GET_EVENT_PORT(data)	\
2533 	((data) &			\
2534 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2535 
2536 #define BNXT_EVENT_RING_TYPE(data2)	\
2537 	((data2) &			\
2538 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2539 
2540 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2541 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2542 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2543 
2544 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2545 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2546 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2547 
2548 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2549 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2550 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2551 
2552 #define BNXT_PHC_BITS	48
2553 
2554 static int bnxt_async_event_process(struct bnxt *bp,
2555 				    struct hwrm_async_event_cmpl *cmpl)
2556 {
2557 	u16 event_id = le16_to_cpu(cmpl->event_id);
2558 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2559 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2560 
2561 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2562 		   event_id, data1, data2);
2563 
2564 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2565 	switch (event_id) {
2566 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2567 		struct bnxt_link_info *link_info = &bp->link_info;
2568 
2569 		if (BNXT_VF(bp))
2570 			goto async_event_process_exit;
2571 
2572 		/* print unsupported speed warning in forced speed mode only */
2573 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2574 		    (data1 & 0x20000)) {
2575 			u16 fw_speed = bnxt_get_force_speed(link_info);
2576 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2577 
2578 			if (speed != SPEED_UNKNOWN)
2579 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2580 					    speed);
2581 		}
2582 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2583 	}
2584 		fallthrough;
2585 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2586 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2587 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2588 		fallthrough;
2589 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2590 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2591 		break;
2592 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2593 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2594 		break;
2595 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2596 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2597 
2598 		if (BNXT_VF(bp))
2599 			break;
2600 
2601 		if (bp->pf.port_id != port_id)
2602 			break;
2603 
2604 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2605 		break;
2606 	}
2607 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2608 		if (BNXT_PF(bp))
2609 			goto async_event_process_exit;
2610 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2611 		break;
2612 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2613 		char *type_str = "Solicited";
2614 
2615 		if (!bp->fw_health)
2616 			goto async_event_process_exit;
2617 
2618 		bp->fw_reset_timestamp = jiffies;
2619 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2620 		if (!bp->fw_reset_min_dsecs)
2621 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2622 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2623 		if (!bp->fw_reset_max_dsecs)
2624 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2625 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2626 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2627 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2628 			type_str = "Fatal";
2629 			bp->fw_health->fatalities++;
2630 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2631 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2632 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2633 			type_str = "Non-fatal";
2634 			bp->fw_health->survivals++;
2635 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2636 		}
2637 		netif_warn(bp, hw, bp->dev,
2638 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2639 			   type_str, data1, data2,
2640 			   bp->fw_reset_min_dsecs * 100,
2641 			   bp->fw_reset_max_dsecs * 100);
2642 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2643 		break;
2644 	}
2645 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2646 		struct bnxt_fw_health *fw_health = bp->fw_health;
2647 		char *status_desc = "healthy";
2648 		u32 status;
2649 
2650 		if (!fw_health)
2651 			goto async_event_process_exit;
2652 
2653 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2654 			fw_health->enabled = false;
2655 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2656 			break;
2657 		}
2658 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2659 		fw_health->tmr_multiplier =
2660 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2661 				     bp->current_interval * 10);
2662 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2663 		if (!fw_health->enabled)
2664 			fw_health->last_fw_heartbeat =
2665 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2666 		fw_health->last_fw_reset_cnt =
2667 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2668 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2669 		if (status != BNXT_FW_STATUS_HEALTHY)
2670 			status_desc = "unhealthy";
2671 		netif_info(bp, drv, bp->dev,
2672 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2673 			   fw_health->primary ? "primary" : "backup", status,
2674 			   status_desc, fw_health->last_fw_reset_cnt);
2675 		if (!fw_health->enabled) {
2676 			/* Make sure tmr_counter is set and visible to
2677 			 * bnxt_health_check() before setting enabled to true.
2678 			 */
2679 			smp_wmb();
2680 			fw_health->enabled = true;
2681 		}
2682 		goto async_event_process_exit;
2683 	}
2684 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2685 		netif_notice(bp, hw, bp->dev,
2686 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2687 			     data1, data2);
2688 		goto async_event_process_exit;
2689 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2690 		struct bnxt_rx_ring_info *rxr;
2691 		u16 grp_idx;
2692 
2693 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2694 			goto async_event_process_exit;
2695 
2696 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2697 			    BNXT_EVENT_RING_TYPE(data2), data1);
2698 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2699 			goto async_event_process_exit;
2700 
2701 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2702 		if (grp_idx == INVALID_HW_RING_ID) {
2703 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2704 				    data1);
2705 			goto async_event_process_exit;
2706 		}
2707 		rxr = bp->bnapi[grp_idx]->rx_ring;
2708 		bnxt_sched_reset_rxr(bp, rxr);
2709 		goto async_event_process_exit;
2710 	}
2711 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2712 		struct bnxt_fw_health *fw_health = bp->fw_health;
2713 
2714 		netif_notice(bp, hw, bp->dev,
2715 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2716 			     data1, data2);
2717 		if (fw_health) {
2718 			fw_health->echo_req_data1 = data1;
2719 			fw_health->echo_req_data2 = data2;
2720 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2721 			break;
2722 		}
2723 		goto async_event_process_exit;
2724 	}
2725 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2726 		bnxt_ptp_pps_event(bp, data1, data2);
2727 		goto async_event_process_exit;
2728 	}
2729 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2730 		if (bnxt_event_error_report(bp, data1, data2))
2731 			break;
2732 		goto async_event_process_exit;
2733 	}
2734 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2735 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2736 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2737 			if (BNXT_PTP_USE_RTC(bp)) {
2738 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2739 				u64 ns;
2740 
2741 				if (!ptp)
2742 					goto async_event_process_exit;
2743 
2744 				spin_lock_bh(&ptp->ptp_lock);
2745 				bnxt_ptp_update_current_time(bp);
2746 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2747 				       BNXT_PHC_BITS) | ptp->current_time);
2748 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2749 				spin_unlock_bh(&ptp->ptp_lock);
2750 			}
2751 			break;
2752 		}
2753 		goto async_event_process_exit;
2754 	}
2755 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2756 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2757 
2758 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2759 		goto async_event_process_exit;
2760 	}
2761 	default:
2762 		goto async_event_process_exit;
2763 	}
2764 	__bnxt_queue_sp_work(bp);
2765 async_event_process_exit:
2766 	return 0;
2767 }
2768 
2769 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2770 {
2771 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2772 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2773 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2774 				(struct hwrm_fwd_req_cmpl *)txcmp;
2775 
2776 	switch (cmpl_type) {
2777 	case CMPL_BASE_TYPE_HWRM_DONE:
2778 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2779 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2780 		break;
2781 
2782 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2783 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2784 
2785 		if ((vf_id < bp->pf.first_vf_id) ||
2786 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2787 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2788 				   vf_id);
2789 			return -EINVAL;
2790 		}
2791 
2792 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2793 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2794 		break;
2795 
2796 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2797 		bnxt_async_event_process(bp,
2798 					 (struct hwrm_async_event_cmpl *)txcmp);
2799 		break;
2800 
2801 	default:
2802 		break;
2803 	}
2804 
2805 	return 0;
2806 }
2807 
2808 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2809 {
2810 	struct bnxt_napi *bnapi = dev_instance;
2811 	struct bnxt *bp = bnapi->bp;
2812 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2813 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2814 
2815 	cpr->event_ctr++;
2816 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2817 	napi_schedule(&bnapi->napi);
2818 	return IRQ_HANDLED;
2819 }
2820 
2821 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2822 {
2823 	u32 raw_cons = cpr->cp_raw_cons;
2824 	u16 cons = RING_CMP(raw_cons);
2825 	struct tx_cmp *txcmp;
2826 
2827 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2828 
2829 	return TX_CMP_VALID(txcmp, raw_cons);
2830 }
2831 
2832 static irqreturn_t bnxt_inta(int irq, void *dev_instance)
2833 {
2834 	struct bnxt_napi *bnapi = dev_instance;
2835 	struct bnxt *bp = bnapi->bp;
2836 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2837 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2838 	u32 int_status;
2839 
2840 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2841 
2842 	if (!bnxt_has_work(bp, cpr)) {
2843 		int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
2844 		/* return if erroneous interrupt */
2845 		if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
2846 			return IRQ_NONE;
2847 	}
2848 
2849 	/* disable ring IRQ */
2850 	BNXT_CP_DB_IRQ_DIS(cpr->cp_db.doorbell);
2851 
2852 	/* Return here if interrupt is shared and is disabled. */
2853 	if (unlikely(atomic_read(&bp->intr_sem) != 0))
2854 		return IRQ_HANDLED;
2855 
2856 	napi_schedule(&bnapi->napi);
2857 	return IRQ_HANDLED;
2858 }
2859 
2860 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2861 			    int budget)
2862 {
2863 	struct bnxt_napi *bnapi = cpr->bnapi;
2864 	u32 raw_cons = cpr->cp_raw_cons;
2865 	u32 cons;
2866 	int rx_pkts = 0;
2867 	u8 event = 0;
2868 	struct tx_cmp *txcmp;
2869 
2870 	cpr->has_more_work = 0;
2871 	cpr->had_work_done = 1;
2872 	while (1) {
2873 		u8 cmp_type;
2874 		int rc;
2875 
2876 		cons = RING_CMP(raw_cons);
2877 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2878 
2879 		if (!TX_CMP_VALID(txcmp, raw_cons))
2880 			break;
2881 
2882 		/* The valid test of the entry must be done first before
2883 		 * reading any further.
2884 		 */
2885 		dma_rmb();
2886 		cmp_type = TX_CMP_TYPE(txcmp);
2887 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
2888 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
2889 			u32 opaque = txcmp->tx_cmp_opaque;
2890 			struct bnxt_tx_ring_info *txr;
2891 			u16 tx_freed;
2892 
2893 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
2894 			event |= BNXT_TX_CMP_EVENT;
2895 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
2896 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
2897 			else
2898 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
2899 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
2900 				   bp->tx_ring_mask;
2901 			/* return full budget so NAPI will complete. */
2902 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
2903 				rx_pkts = budget;
2904 				raw_cons = NEXT_RAW_CMP(raw_cons);
2905 				if (budget)
2906 					cpr->has_more_work = 1;
2907 				break;
2908 			}
2909 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
2910 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2911 			if (likely(budget))
2912 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2913 			else
2914 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2915 							   &event);
2916 			if (likely(rc >= 0))
2917 				rx_pkts += rc;
2918 			/* Increment rx_pkts when rc is -ENOMEM to count towards
2919 			 * the NAPI budget.  Otherwise, we may potentially loop
2920 			 * here forever if we consistently cannot allocate
2921 			 * buffers.
2922 			 */
2923 			else if (rc == -ENOMEM && budget)
2924 				rx_pkts++;
2925 			else if (rc == -EBUSY)	/* partial completion */
2926 				break;
2927 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
2928 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
2929 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
2930 			bnxt_hwrm_handler(bp, txcmp);
2931 		}
2932 		raw_cons = NEXT_RAW_CMP(raw_cons);
2933 
2934 		if (rx_pkts && rx_pkts == budget) {
2935 			cpr->has_more_work = 1;
2936 			break;
2937 		}
2938 	}
2939 
2940 	if (event & BNXT_REDIRECT_EVENT)
2941 		xdp_do_flush();
2942 
2943 	if (event & BNXT_TX_EVENT) {
2944 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
2945 		u16 prod = txr->tx_prod;
2946 
2947 		/* Sync BD data before updating doorbell */
2948 		wmb();
2949 
2950 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
2951 	}
2952 
2953 	cpr->cp_raw_cons = raw_cons;
2954 	bnapi->events |= event;
2955 	return rx_pkts;
2956 }
2957 
2958 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
2959 				  int budget)
2960 {
2961 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
2962 		bnapi->tx_int(bp, bnapi, budget);
2963 
2964 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
2965 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2966 
2967 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2968 	}
2969 	if (bnapi->events & BNXT_AGG_EVENT) {
2970 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2971 
2972 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2973 	}
2974 	bnapi->events &= BNXT_TX_CMP_EVENT;
2975 }
2976 
2977 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2978 			  int budget)
2979 {
2980 	struct bnxt_napi *bnapi = cpr->bnapi;
2981 	int rx_pkts;
2982 
2983 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
2984 
2985 	/* ACK completion ring before freeing tx ring and producing new
2986 	 * buffers in rx/agg rings to prevent overflowing the completion
2987 	 * ring.
2988 	 */
2989 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
2990 
2991 	__bnxt_poll_work_done(bp, bnapi, budget);
2992 	return rx_pkts;
2993 }
2994 
2995 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
2996 {
2997 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2998 	struct bnxt *bp = bnapi->bp;
2999 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3000 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3001 	struct tx_cmp *txcmp;
3002 	struct rx_cmp_ext *rxcmp1;
3003 	u32 cp_cons, tmp_raw_cons;
3004 	u32 raw_cons = cpr->cp_raw_cons;
3005 	bool flush_xdp = false;
3006 	u32 rx_pkts = 0;
3007 	u8 event = 0;
3008 
3009 	while (1) {
3010 		int rc;
3011 
3012 		cp_cons = RING_CMP(raw_cons);
3013 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3014 
3015 		if (!TX_CMP_VALID(txcmp, raw_cons))
3016 			break;
3017 
3018 		/* The valid test of the entry must be done first before
3019 		 * reading any further.
3020 		 */
3021 		dma_rmb();
3022 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3023 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3024 			cp_cons = RING_CMP(tmp_raw_cons);
3025 			rxcmp1 = (struct rx_cmp_ext *)
3026 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3027 
3028 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3029 				break;
3030 
3031 			/* force an error to recycle the buffer */
3032 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3033 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3034 
3035 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3036 			if (likely(rc == -EIO) && budget)
3037 				rx_pkts++;
3038 			else if (rc == -EBUSY)	/* partial completion */
3039 				break;
3040 			if (event & BNXT_REDIRECT_EVENT)
3041 				flush_xdp = true;
3042 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3043 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3044 			bnxt_hwrm_handler(bp, txcmp);
3045 		} else {
3046 			netdev_err(bp->dev,
3047 				   "Invalid completion received on special ring\n");
3048 		}
3049 		raw_cons = NEXT_RAW_CMP(raw_cons);
3050 
3051 		if (rx_pkts == budget)
3052 			break;
3053 	}
3054 
3055 	cpr->cp_raw_cons = raw_cons;
3056 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3057 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3058 
3059 	if (event & BNXT_AGG_EVENT)
3060 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3061 	if (flush_xdp)
3062 		xdp_do_flush();
3063 
3064 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3065 		napi_complete_done(napi, rx_pkts);
3066 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3067 	}
3068 	return rx_pkts;
3069 }
3070 
3071 static int bnxt_poll(struct napi_struct *napi, int budget)
3072 {
3073 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3074 	struct bnxt *bp = bnapi->bp;
3075 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3076 	int work_done = 0;
3077 
3078 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3079 		napi_complete(napi);
3080 		return 0;
3081 	}
3082 	while (1) {
3083 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3084 
3085 		if (work_done >= budget) {
3086 			if (!budget)
3087 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3088 			break;
3089 		}
3090 
3091 		if (!bnxt_has_work(bp, cpr)) {
3092 			if (napi_complete_done(napi, work_done))
3093 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3094 			break;
3095 		}
3096 	}
3097 	if (bp->flags & BNXT_FLAG_DIM) {
3098 		struct dim_sample dim_sample = {};
3099 
3100 		dim_update_sample(cpr->event_ctr,
3101 				  cpr->rx_packets,
3102 				  cpr->rx_bytes,
3103 				  &dim_sample);
3104 		net_dim(&cpr->dim, dim_sample);
3105 	}
3106 	return work_done;
3107 }
3108 
3109 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3110 {
3111 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3112 	int i, work_done = 0;
3113 
3114 	for (i = 0; i < cpr->cp_ring_count; i++) {
3115 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3116 
3117 		if (cpr2->had_nqe_notify) {
3118 			work_done += __bnxt_poll_work(bp, cpr2,
3119 						      budget - work_done);
3120 			cpr->has_more_work |= cpr2->has_more_work;
3121 		}
3122 	}
3123 	return work_done;
3124 }
3125 
3126 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3127 				 u64 dbr_type, int budget)
3128 {
3129 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3130 	int i;
3131 
3132 	for (i = 0; i < cpr->cp_ring_count; i++) {
3133 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3134 		struct bnxt_db_info *db;
3135 
3136 		if (cpr2->had_work_done) {
3137 			u32 tgl = 0;
3138 
3139 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3140 				cpr2->had_nqe_notify = 0;
3141 				tgl = cpr2->toggle;
3142 			}
3143 			db = &cpr2->cp_db;
3144 			bnxt_writeq(bp,
3145 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3146 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3147 				    db->doorbell);
3148 			cpr2->had_work_done = 0;
3149 		}
3150 	}
3151 	__bnxt_poll_work_done(bp, bnapi, budget);
3152 }
3153 
3154 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3155 {
3156 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3157 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3158 	struct bnxt_cp_ring_info *cpr_rx;
3159 	u32 raw_cons = cpr->cp_raw_cons;
3160 	struct bnxt *bp = bnapi->bp;
3161 	struct nqe_cn *nqcmp;
3162 	int work_done = 0;
3163 	u32 cons;
3164 
3165 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3166 		napi_complete(napi);
3167 		return 0;
3168 	}
3169 	if (cpr->has_more_work) {
3170 		cpr->has_more_work = 0;
3171 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3172 	}
3173 	while (1) {
3174 		u16 type;
3175 
3176 		cons = RING_CMP(raw_cons);
3177 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3178 
3179 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3180 			if (cpr->has_more_work)
3181 				break;
3182 
3183 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3184 					     budget);
3185 			cpr->cp_raw_cons = raw_cons;
3186 			if (napi_complete_done(napi, work_done))
3187 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3188 						  cpr->cp_raw_cons);
3189 			goto poll_done;
3190 		}
3191 
3192 		/* The valid test of the entry must be done first before
3193 		 * reading any further.
3194 		 */
3195 		dma_rmb();
3196 
3197 		type = le16_to_cpu(nqcmp->type);
3198 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3199 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3200 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3201 			struct bnxt_cp_ring_info *cpr2;
3202 
3203 			/* No more budget for RX work */
3204 			if (budget && work_done >= budget &&
3205 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3206 				break;
3207 
3208 			idx = BNXT_NQ_HDL_IDX(idx);
3209 			cpr2 = &cpr->cp_ring_arr[idx];
3210 			cpr2->had_nqe_notify = 1;
3211 			cpr2->toggle = NQE_CN_TOGGLE(type);
3212 			work_done += __bnxt_poll_work(bp, cpr2,
3213 						      budget - work_done);
3214 			cpr->has_more_work |= cpr2->has_more_work;
3215 		} else {
3216 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3217 		}
3218 		raw_cons = NEXT_RAW_CMP(raw_cons);
3219 	}
3220 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3221 	if (raw_cons != cpr->cp_raw_cons) {
3222 		cpr->cp_raw_cons = raw_cons;
3223 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3224 	}
3225 poll_done:
3226 	cpr_rx = &cpr->cp_ring_arr[0];
3227 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3228 	    (bp->flags & BNXT_FLAG_DIM)) {
3229 		struct dim_sample dim_sample = {};
3230 
3231 		dim_update_sample(cpr->event_ctr,
3232 				  cpr_rx->rx_packets,
3233 				  cpr_rx->rx_bytes,
3234 				  &dim_sample);
3235 		net_dim(&cpr->dim, dim_sample);
3236 	}
3237 	return work_done;
3238 }
3239 
3240 static void bnxt_free_tx_skbs(struct bnxt *bp)
3241 {
3242 	int i, max_idx;
3243 	struct pci_dev *pdev = bp->pdev;
3244 
3245 	if (!bp->tx_ring)
3246 		return;
3247 
3248 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3249 	for (i = 0; i < bp->tx_nr_rings; i++) {
3250 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3251 		int j;
3252 
3253 		if (!txr->tx_buf_ring)
3254 			continue;
3255 
3256 		for (j = 0; j < max_idx;) {
3257 			struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
3258 			struct sk_buff *skb;
3259 			int k, last;
3260 
3261 			if (i < bp->tx_nr_rings_xdp &&
3262 			    tx_buf->action == XDP_REDIRECT) {
3263 				dma_unmap_single(&pdev->dev,
3264 					dma_unmap_addr(tx_buf, mapping),
3265 					dma_unmap_len(tx_buf, len),
3266 					DMA_TO_DEVICE);
3267 				xdp_return_frame(tx_buf->xdpf);
3268 				tx_buf->action = 0;
3269 				tx_buf->xdpf = NULL;
3270 				j++;
3271 				continue;
3272 			}
3273 
3274 			skb = tx_buf->skb;
3275 			if (!skb) {
3276 				j++;
3277 				continue;
3278 			}
3279 
3280 			tx_buf->skb = NULL;
3281 
3282 			if (tx_buf->is_push) {
3283 				dev_kfree_skb(skb);
3284 				j += 2;
3285 				continue;
3286 			}
3287 
3288 			dma_unmap_single(&pdev->dev,
3289 					 dma_unmap_addr(tx_buf, mapping),
3290 					 skb_headlen(skb),
3291 					 DMA_TO_DEVICE);
3292 
3293 			last = tx_buf->nr_frags;
3294 			j += 2;
3295 			for (k = 0; k < last; k++, j++) {
3296 				int ring_idx = j & bp->tx_ring_mask;
3297 				skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
3298 
3299 				tx_buf = &txr->tx_buf_ring[ring_idx];
3300 				dma_unmap_page(
3301 					&pdev->dev,
3302 					dma_unmap_addr(tx_buf, mapping),
3303 					skb_frag_size(frag), DMA_TO_DEVICE);
3304 			}
3305 			dev_kfree_skb(skb);
3306 		}
3307 		netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
3308 	}
3309 }
3310 
3311 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, int ring_nr)
3312 {
3313 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
3314 	struct pci_dev *pdev = bp->pdev;
3315 	struct bnxt_tpa_idx_map *map;
3316 	int i, max_idx, max_agg_idx;
3317 
3318 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3319 	max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3320 	if (!rxr->rx_tpa)
3321 		goto skip_rx_tpa_free;
3322 
3323 	for (i = 0; i < bp->max_tpa; i++) {
3324 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3325 		u8 *data = tpa_info->data;
3326 
3327 		if (!data)
3328 			continue;
3329 
3330 		dma_unmap_single_attrs(&pdev->dev, tpa_info->mapping,
3331 				       bp->rx_buf_use_size, bp->rx_dir,
3332 				       DMA_ATTR_WEAK_ORDERING);
3333 
3334 		tpa_info->data = NULL;
3335 
3336 		skb_free_frag(data);
3337 	}
3338 
3339 skip_rx_tpa_free:
3340 	if (!rxr->rx_buf_ring)
3341 		goto skip_rx_buf_free;
3342 
3343 	for (i = 0; i < max_idx; i++) {
3344 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3345 		dma_addr_t mapping = rx_buf->mapping;
3346 		void *data = rx_buf->data;
3347 
3348 		if (!data)
3349 			continue;
3350 
3351 		rx_buf->data = NULL;
3352 		if (BNXT_RX_PAGE_MODE(bp)) {
3353 			page_pool_recycle_direct(rxr->page_pool, data);
3354 		} else {
3355 			dma_unmap_single_attrs(&pdev->dev, mapping,
3356 					       bp->rx_buf_use_size, bp->rx_dir,
3357 					       DMA_ATTR_WEAK_ORDERING);
3358 			skb_free_frag(data);
3359 		}
3360 	}
3361 
3362 skip_rx_buf_free:
3363 	if (!rxr->rx_agg_ring)
3364 		goto skip_rx_agg_free;
3365 
3366 	for (i = 0; i < max_agg_idx; i++) {
3367 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3368 		struct page *page = rx_agg_buf->page;
3369 
3370 		if (!page)
3371 			continue;
3372 
3373 		rx_agg_buf->page = NULL;
3374 		__clear_bit(i, rxr->rx_agg_bmap);
3375 
3376 		page_pool_recycle_direct(rxr->page_pool, page);
3377 	}
3378 
3379 skip_rx_agg_free:
3380 	map = rxr->rx_tpa_idx_map;
3381 	if (map)
3382 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3383 }
3384 
3385 static void bnxt_free_rx_skbs(struct bnxt *bp)
3386 {
3387 	int i;
3388 
3389 	if (!bp->rx_ring)
3390 		return;
3391 
3392 	for (i = 0; i < bp->rx_nr_rings; i++)
3393 		bnxt_free_one_rx_ring_skbs(bp, i);
3394 }
3395 
3396 static void bnxt_free_skbs(struct bnxt *bp)
3397 {
3398 	bnxt_free_tx_skbs(bp);
3399 	bnxt_free_rx_skbs(bp);
3400 }
3401 
3402 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3403 {
3404 	u8 init_val = ctxm->init_value;
3405 	u16 offset = ctxm->init_offset;
3406 	u8 *p2 = p;
3407 	int i;
3408 
3409 	if (!init_val)
3410 		return;
3411 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3412 		memset(p, init_val, len);
3413 		return;
3414 	}
3415 	for (i = 0; i < len; i += ctxm->entry_size)
3416 		*(p2 + i + offset) = init_val;
3417 }
3418 
3419 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3420 {
3421 	struct pci_dev *pdev = bp->pdev;
3422 	int i;
3423 
3424 	if (!rmem->pg_arr)
3425 		goto skip_pages;
3426 
3427 	for (i = 0; i < rmem->nr_pages; i++) {
3428 		if (!rmem->pg_arr[i])
3429 			continue;
3430 
3431 		dma_free_coherent(&pdev->dev, rmem->page_size,
3432 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3433 
3434 		rmem->pg_arr[i] = NULL;
3435 	}
3436 skip_pages:
3437 	if (rmem->pg_tbl) {
3438 		size_t pg_tbl_size = rmem->nr_pages * 8;
3439 
3440 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3441 			pg_tbl_size = rmem->page_size;
3442 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3443 				  rmem->pg_tbl, rmem->pg_tbl_map);
3444 		rmem->pg_tbl = NULL;
3445 	}
3446 	if (rmem->vmem_size && *rmem->vmem) {
3447 		vfree(*rmem->vmem);
3448 		*rmem->vmem = NULL;
3449 	}
3450 }
3451 
3452 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3453 {
3454 	struct pci_dev *pdev = bp->pdev;
3455 	u64 valid_bit = 0;
3456 	int i;
3457 
3458 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3459 		valid_bit = PTU_PTE_VALID;
3460 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3461 		size_t pg_tbl_size = rmem->nr_pages * 8;
3462 
3463 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3464 			pg_tbl_size = rmem->page_size;
3465 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3466 						  &rmem->pg_tbl_map,
3467 						  GFP_KERNEL);
3468 		if (!rmem->pg_tbl)
3469 			return -ENOMEM;
3470 	}
3471 
3472 	for (i = 0; i < rmem->nr_pages; i++) {
3473 		u64 extra_bits = valid_bit;
3474 
3475 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3476 						     rmem->page_size,
3477 						     &rmem->dma_arr[i],
3478 						     GFP_KERNEL);
3479 		if (!rmem->pg_arr[i])
3480 			return -ENOMEM;
3481 
3482 		if (rmem->ctx_mem)
3483 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3484 					  rmem->page_size);
3485 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3486 			if (i == rmem->nr_pages - 2 &&
3487 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3488 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3489 			else if (i == rmem->nr_pages - 1 &&
3490 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3491 				extra_bits |= PTU_PTE_LAST;
3492 			rmem->pg_tbl[i] =
3493 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3494 		}
3495 	}
3496 
3497 	if (rmem->vmem_size) {
3498 		*rmem->vmem = vzalloc(rmem->vmem_size);
3499 		if (!(*rmem->vmem))
3500 			return -ENOMEM;
3501 	}
3502 	return 0;
3503 }
3504 
3505 static void bnxt_free_tpa_info(struct bnxt *bp)
3506 {
3507 	int i, j;
3508 
3509 	for (i = 0; i < bp->rx_nr_rings; i++) {
3510 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3511 
3512 		kfree(rxr->rx_tpa_idx_map);
3513 		rxr->rx_tpa_idx_map = NULL;
3514 		if (rxr->rx_tpa) {
3515 			for (j = 0; j < bp->max_tpa; j++) {
3516 				kfree(rxr->rx_tpa[j].agg_arr);
3517 				rxr->rx_tpa[j].agg_arr = NULL;
3518 			}
3519 		}
3520 		kfree(rxr->rx_tpa);
3521 		rxr->rx_tpa = NULL;
3522 	}
3523 }
3524 
3525 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3526 {
3527 	int i, j;
3528 
3529 	bp->max_tpa = MAX_TPA;
3530 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3531 		if (!bp->max_tpa_v2)
3532 			return 0;
3533 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3534 	}
3535 
3536 	for (i = 0; i < bp->rx_nr_rings; i++) {
3537 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3538 		struct rx_agg_cmp *agg;
3539 
3540 		rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3541 				      GFP_KERNEL);
3542 		if (!rxr->rx_tpa)
3543 			return -ENOMEM;
3544 
3545 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3546 			continue;
3547 		for (j = 0; j < bp->max_tpa; j++) {
3548 			agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3549 			if (!agg)
3550 				return -ENOMEM;
3551 			rxr->rx_tpa[j].agg_arr = agg;
3552 		}
3553 		rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3554 					      GFP_KERNEL);
3555 		if (!rxr->rx_tpa_idx_map)
3556 			return -ENOMEM;
3557 	}
3558 	return 0;
3559 }
3560 
3561 static void bnxt_free_rx_rings(struct bnxt *bp)
3562 {
3563 	int i;
3564 
3565 	if (!bp->rx_ring)
3566 		return;
3567 
3568 	bnxt_free_tpa_info(bp);
3569 	for (i = 0; i < bp->rx_nr_rings; i++) {
3570 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3571 		struct bnxt_ring_struct *ring;
3572 
3573 		if (rxr->xdp_prog)
3574 			bpf_prog_put(rxr->xdp_prog);
3575 
3576 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3577 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3578 
3579 		page_pool_destroy(rxr->page_pool);
3580 		rxr->page_pool = NULL;
3581 
3582 		kfree(rxr->rx_agg_bmap);
3583 		rxr->rx_agg_bmap = NULL;
3584 
3585 		ring = &rxr->rx_ring_struct;
3586 		bnxt_free_ring(bp, &ring->ring_mem);
3587 
3588 		ring = &rxr->rx_agg_ring_struct;
3589 		bnxt_free_ring(bp, &ring->ring_mem);
3590 	}
3591 }
3592 
3593 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3594 				   struct bnxt_rx_ring_info *rxr,
3595 				   int numa_node)
3596 {
3597 	struct page_pool_params pp = { 0 };
3598 
3599 	pp.pool_size = bp->rx_agg_ring_size;
3600 	if (BNXT_RX_PAGE_MODE(bp))
3601 		pp.pool_size += bp->rx_ring_size;
3602 	pp.nid = numa_node;
3603 	pp.napi = &rxr->bnapi->napi;
3604 	pp.netdev = bp->dev;
3605 	pp.dev = &bp->pdev->dev;
3606 	pp.dma_dir = bp->rx_dir;
3607 	pp.max_len = PAGE_SIZE;
3608 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3609 
3610 	rxr->page_pool = page_pool_create(&pp);
3611 	if (IS_ERR(rxr->page_pool)) {
3612 		int err = PTR_ERR(rxr->page_pool);
3613 
3614 		rxr->page_pool = NULL;
3615 		return err;
3616 	}
3617 	return 0;
3618 }
3619 
3620 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3621 {
3622 	int numa_node = dev_to_node(&bp->pdev->dev);
3623 	int i, rc = 0, agg_rings = 0, cpu;
3624 
3625 	if (!bp->rx_ring)
3626 		return -ENOMEM;
3627 
3628 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3629 		agg_rings = 1;
3630 
3631 	for (i = 0; i < bp->rx_nr_rings; i++) {
3632 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3633 		struct bnxt_ring_struct *ring;
3634 		int cpu_node;
3635 
3636 		ring = &rxr->rx_ring_struct;
3637 
3638 		cpu = cpumask_local_spread(i, numa_node);
3639 		cpu_node = cpu_to_node(cpu);
3640 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3641 			   i, cpu_node);
3642 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3643 		if (rc)
3644 			return rc;
3645 
3646 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3647 		if (rc < 0)
3648 			return rc;
3649 
3650 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3651 						MEM_TYPE_PAGE_POOL,
3652 						rxr->page_pool);
3653 		if (rc) {
3654 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3655 			return rc;
3656 		}
3657 
3658 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3659 		if (rc)
3660 			return rc;
3661 
3662 		ring->grp_idx = i;
3663 		if (agg_rings) {
3664 			u16 mem_size;
3665 
3666 			ring = &rxr->rx_agg_ring_struct;
3667 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3668 			if (rc)
3669 				return rc;
3670 
3671 			ring->grp_idx = i;
3672 			rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3673 			mem_size = rxr->rx_agg_bmap_size / 8;
3674 			rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3675 			if (!rxr->rx_agg_bmap)
3676 				return -ENOMEM;
3677 		}
3678 	}
3679 	if (bp->flags & BNXT_FLAG_TPA)
3680 		rc = bnxt_alloc_tpa_info(bp);
3681 	return rc;
3682 }
3683 
3684 static void bnxt_free_tx_rings(struct bnxt *bp)
3685 {
3686 	int i;
3687 	struct pci_dev *pdev = bp->pdev;
3688 
3689 	if (!bp->tx_ring)
3690 		return;
3691 
3692 	for (i = 0; i < bp->tx_nr_rings; i++) {
3693 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3694 		struct bnxt_ring_struct *ring;
3695 
3696 		if (txr->tx_push) {
3697 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3698 					  txr->tx_push, txr->tx_push_mapping);
3699 			txr->tx_push = NULL;
3700 		}
3701 
3702 		ring = &txr->tx_ring_struct;
3703 
3704 		bnxt_free_ring(bp, &ring->ring_mem);
3705 	}
3706 }
3707 
3708 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3709 	((tc) * (bp)->tx_nr_rings_per_tc)
3710 
3711 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3712 	((tx) % (bp)->tx_nr_rings_per_tc)
3713 
3714 #define BNXT_RING_TO_TC(bp, tx)		\
3715 	((tx) / (bp)->tx_nr_rings_per_tc)
3716 
3717 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3718 {
3719 	int i, j, rc;
3720 	struct pci_dev *pdev = bp->pdev;
3721 
3722 	bp->tx_push_size = 0;
3723 	if (bp->tx_push_thresh) {
3724 		int push_size;
3725 
3726 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3727 					bp->tx_push_thresh);
3728 
3729 		if (push_size > 256) {
3730 			push_size = 0;
3731 			bp->tx_push_thresh = 0;
3732 		}
3733 
3734 		bp->tx_push_size = push_size;
3735 	}
3736 
3737 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3738 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3739 		struct bnxt_ring_struct *ring;
3740 		u8 qidx;
3741 
3742 		ring = &txr->tx_ring_struct;
3743 
3744 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3745 		if (rc)
3746 			return rc;
3747 
3748 		ring->grp_idx = txr->bnapi->index;
3749 		if (bp->tx_push_size) {
3750 			dma_addr_t mapping;
3751 
3752 			/* One pre-allocated DMA buffer to backup
3753 			 * TX push operation
3754 			 */
3755 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3756 						bp->tx_push_size,
3757 						&txr->tx_push_mapping,
3758 						GFP_KERNEL);
3759 
3760 			if (!txr->tx_push)
3761 				return -ENOMEM;
3762 
3763 			mapping = txr->tx_push_mapping +
3764 				sizeof(struct tx_push_bd);
3765 			txr->data_mapping = cpu_to_le64(mapping);
3766 		}
3767 		qidx = bp->tc_to_qidx[j];
3768 		ring->queue_id = bp->q_info[qidx].queue_id;
3769 		spin_lock_init(&txr->xdp_tx_lock);
3770 		if (i < bp->tx_nr_rings_xdp)
3771 			continue;
3772 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
3773 			j++;
3774 	}
3775 	return 0;
3776 }
3777 
3778 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
3779 {
3780 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3781 
3782 	kfree(cpr->cp_desc_ring);
3783 	cpr->cp_desc_ring = NULL;
3784 	ring->ring_mem.pg_arr = NULL;
3785 	kfree(cpr->cp_desc_mapping);
3786 	cpr->cp_desc_mapping = NULL;
3787 	ring->ring_mem.dma_arr = NULL;
3788 }
3789 
3790 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
3791 {
3792 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
3793 	if (!cpr->cp_desc_ring)
3794 		return -ENOMEM;
3795 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
3796 				       GFP_KERNEL);
3797 	if (!cpr->cp_desc_mapping)
3798 		return -ENOMEM;
3799 	return 0;
3800 }
3801 
3802 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
3803 {
3804 	int i;
3805 
3806 	if (!bp->bnapi)
3807 		return;
3808 	for (i = 0; i < bp->cp_nr_rings; i++) {
3809 		struct bnxt_napi *bnapi = bp->bnapi[i];
3810 
3811 		if (!bnapi)
3812 			continue;
3813 		bnxt_free_cp_arrays(&bnapi->cp_ring);
3814 	}
3815 }
3816 
3817 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
3818 {
3819 	int i, n = bp->cp_nr_pages;
3820 
3821 	for (i = 0; i < bp->cp_nr_rings; i++) {
3822 		struct bnxt_napi *bnapi = bp->bnapi[i];
3823 		int rc;
3824 
3825 		if (!bnapi)
3826 			continue;
3827 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
3828 		if (rc)
3829 			return rc;
3830 	}
3831 	return 0;
3832 }
3833 
3834 static void bnxt_free_cp_rings(struct bnxt *bp)
3835 {
3836 	int i;
3837 
3838 	if (!bp->bnapi)
3839 		return;
3840 
3841 	for (i = 0; i < bp->cp_nr_rings; i++) {
3842 		struct bnxt_napi *bnapi = bp->bnapi[i];
3843 		struct bnxt_cp_ring_info *cpr;
3844 		struct bnxt_ring_struct *ring;
3845 		int j;
3846 
3847 		if (!bnapi)
3848 			continue;
3849 
3850 		cpr = &bnapi->cp_ring;
3851 		ring = &cpr->cp_ring_struct;
3852 
3853 		bnxt_free_ring(bp, &ring->ring_mem);
3854 
3855 		if (!cpr->cp_ring_arr)
3856 			continue;
3857 
3858 		for (j = 0; j < cpr->cp_ring_count; j++) {
3859 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
3860 
3861 			ring = &cpr2->cp_ring_struct;
3862 			bnxt_free_ring(bp, &ring->ring_mem);
3863 			bnxt_free_cp_arrays(cpr2);
3864 		}
3865 		kfree(cpr->cp_ring_arr);
3866 		cpr->cp_ring_arr = NULL;
3867 		cpr->cp_ring_count = 0;
3868 	}
3869 }
3870 
3871 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
3872 				  struct bnxt_cp_ring_info *cpr)
3873 {
3874 	struct bnxt_ring_mem_info *rmem;
3875 	struct bnxt_ring_struct *ring;
3876 	int rc;
3877 
3878 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
3879 	if (rc) {
3880 		bnxt_free_cp_arrays(cpr);
3881 		return -ENOMEM;
3882 	}
3883 	ring = &cpr->cp_ring_struct;
3884 	rmem = &ring->ring_mem;
3885 	rmem->nr_pages = bp->cp_nr_pages;
3886 	rmem->page_size = HW_CMPD_RING_SIZE;
3887 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
3888 	rmem->dma_arr = cpr->cp_desc_mapping;
3889 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
3890 	rc = bnxt_alloc_ring(bp, rmem);
3891 	if (rc) {
3892 		bnxt_free_ring(bp, rmem);
3893 		bnxt_free_cp_arrays(cpr);
3894 	}
3895 	return rc;
3896 }
3897 
3898 static int bnxt_alloc_cp_rings(struct bnxt *bp)
3899 {
3900 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
3901 	int i, j, rc, ulp_msix;
3902 	int tcs = bp->num_tc;
3903 
3904 	if (!tcs)
3905 		tcs = 1;
3906 	ulp_msix = bnxt_get_ulp_msix_num(bp);
3907 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
3908 		struct bnxt_napi *bnapi = bp->bnapi[i];
3909 		struct bnxt_cp_ring_info *cpr, *cpr2;
3910 		struct bnxt_ring_struct *ring;
3911 		int cp_count = 0, k;
3912 		int rx = 0, tx = 0;
3913 
3914 		if (!bnapi)
3915 			continue;
3916 
3917 		cpr = &bnapi->cp_ring;
3918 		cpr->bnapi = bnapi;
3919 		ring = &cpr->cp_ring_struct;
3920 
3921 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3922 		if (rc)
3923 			return rc;
3924 
3925 		ring->map_idx = ulp_msix + i;
3926 
3927 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3928 			continue;
3929 
3930 		if (i < bp->rx_nr_rings) {
3931 			cp_count++;
3932 			rx = 1;
3933 		}
3934 		if (i < bp->tx_nr_rings_xdp) {
3935 			cp_count++;
3936 			tx = 1;
3937 		} else if ((sh && i < bp->tx_nr_rings) ||
3938 			 (!sh && i >= bp->rx_nr_rings)) {
3939 			cp_count += tcs;
3940 			tx = 1;
3941 		}
3942 
3943 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
3944 					   GFP_KERNEL);
3945 		if (!cpr->cp_ring_arr)
3946 			return -ENOMEM;
3947 		cpr->cp_ring_count = cp_count;
3948 
3949 		for (k = 0; k < cp_count; k++) {
3950 			cpr2 = &cpr->cp_ring_arr[k];
3951 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
3952 			if (rc)
3953 				return rc;
3954 			cpr2->bnapi = bnapi;
3955 			cpr2->sw_stats = cpr->sw_stats;
3956 			cpr2->cp_idx = k;
3957 			if (!k && rx) {
3958 				bp->rx_ring[i].rx_cpr = cpr2;
3959 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
3960 			} else {
3961 				int n, tc = k - rx;
3962 
3963 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
3964 				bp->tx_ring[n].tx_cpr = cpr2;
3965 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
3966 			}
3967 		}
3968 		if (tx)
3969 			j++;
3970 	}
3971 	return 0;
3972 }
3973 
3974 static void bnxt_init_ring_struct(struct bnxt *bp)
3975 {
3976 	int i, j;
3977 
3978 	for (i = 0; i < bp->cp_nr_rings; i++) {
3979 		struct bnxt_napi *bnapi = bp->bnapi[i];
3980 		struct bnxt_ring_mem_info *rmem;
3981 		struct bnxt_cp_ring_info *cpr;
3982 		struct bnxt_rx_ring_info *rxr;
3983 		struct bnxt_tx_ring_info *txr;
3984 		struct bnxt_ring_struct *ring;
3985 
3986 		if (!bnapi)
3987 			continue;
3988 
3989 		cpr = &bnapi->cp_ring;
3990 		ring = &cpr->cp_ring_struct;
3991 		rmem = &ring->ring_mem;
3992 		rmem->nr_pages = bp->cp_nr_pages;
3993 		rmem->page_size = HW_CMPD_RING_SIZE;
3994 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
3995 		rmem->dma_arr = cpr->cp_desc_mapping;
3996 		rmem->vmem_size = 0;
3997 
3998 		rxr = bnapi->rx_ring;
3999 		if (!rxr)
4000 			goto skip_rx;
4001 
4002 		ring = &rxr->rx_ring_struct;
4003 		rmem = &ring->ring_mem;
4004 		rmem->nr_pages = bp->rx_nr_pages;
4005 		rmem->page_size = HW_RXBD_RING_SIZE;
4006 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4007 		rmem->dma_arr = rxr->rx_desc_mapping;
4008 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4009 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4010 
4011 		ring = &rxr->rx_agg_ring_struct;
4012 		rmem = &ring->ring_mem;
4013 		rmem->nr_pages = bp->rx_agg_nr_pages;
4014 		rmem->page_size = HW_RXBD_RING_SIZE;
4015 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4016 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4017 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4018 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4019 
4020 skip_rx:
4021 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4022 			ring = &txr->tx_ring_struct;
4023 			rmem = &ring->ring_mem;
4024 			rmem->nr_pages = bp->tx_nr_pages;
4025 			rmem->page_size = HW_TXBD_RING_SIZE;
4026 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4027 			rmem->dma_arr = txr->tx_desc_mapping;
4028 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4029 			rmem->vmem = (void **)&txr->tx_buf_ring;
4030 		}
4031 	}
4032 }
4033 
4034 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4035 {
4036 	int i;
4037 	u32 prod;
4038 	struct rx_bd **rx_buf_ring;
4039 
4040 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4041 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4042 		int j;
4043 		struct rx_bd *rxbd;
4044 
4045 		rxbd = rx_buf_ring[i];
4046 		if (!rxbd)
4047 			continue;
4048 
4049 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4050 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4051 			rxbd->rx_bd_opaque = prod;
4052 		}
4053 	}
4054 }
4055 
4056 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4057 {
4058 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4059 	struct net_device *dev = bp->dev;
4060 	u32 prod;
4061 	int i;
4062 
4063 	prod = rxr->rx_prod;
4064 	for (i = 0; i < bp->rx_ring_size; i++) {
4065 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4066 			netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
4067 				    ring_nr, i, bp->rx_ring_size);
4068 			break;
4069 		}
4070 		prod = NEXT_RX(prod);
4071 	}
4072 	rxr->rx_prod = prod;
4073 
4074 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4075 		return 0;
4076 
4077 	prod = rxr->rx_agg_prod;
4078 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4079 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
4080 			netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
4081 				    ring_nr, i, bp->rx_ring_size);
4082 			break;
4083 		}
4084 		prod = NEXT_RX_AGG(prod);
4085 	}
4086 	rxr->rx_agg_prod = prod;
4087 
4088 	if (rxr->rx_tpa) {
4089 		dma_addr_t mapping;
4090 		u8 *data;
4091 
4092 		for (i = 0; i < bp->max_tpa; i++) {
4093 			data = __bnxt_alloc_rx_frag(bp, &mapping, GFP_KERNEL);
4094 			if (!data)
4095 				return -ENOMEM;
4096 
4097 			rxr->rx_tpa[i].data = data;
4098 			rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4099 			rxr->rx_tpa[i].mapping = mapping;
4100 		}
4101 	}
4102 	return 0;
4103 }
4104 
4105 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4106 {
4107 	struct bnxt_rx_ring_info *rxr;
4108 	struct bnxt_ring_struct *ring;
4109 	u32 type;
4110 
4111 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4112 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4113 
4114 	if (NET_IP_ALIGN == 2)
4115 		type |= RX_BD_FLAGS_SOP;
4116 
4117 	rxr = &bp->rx_ring[ring_nr];
4118 	ring = &rxr->rx_ring_struct;
4119 	bnxt_init_rxbd_pages(ring, type);
4120 
4121 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4122 			     &rxr->bnapi->napi);
4123 
4124 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4125 		bpf_prog_add(bp->xdp_prog, 1);
4126 		rxr->xdp_prog = bp->xdp_prog;
4127 	}
4128 	ring->fw_ring_id = INVALID_HW_RING_ID;
4129 
4130 	ring = &rxr->rx_agg_ring_struct;
4131 	ring->fw_ring_id = INVALID_HW_RING_ID;
4132 
4133 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4134 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4135 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4136 
4137 		bnxt_init_rxbd_pages(ring, type);
4138 	}
4139 
4140 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4141 }
4142 
4143 static void bnxt_init_cp_rings(struct bnxt *bp)
4144 {
4145 	int i, j;
4146 
4147 	for (i = 0; i < bp->cp_nr_rings; i++) {
4148 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4149 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4150 
4151 		ring->fw_ring_id = INVALID_HW_RING_ID;
4152 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4153 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4154 		if (!cpr->cp_ring_arr)
4155 			continue;
4156 		for (j = 0; j < cpr->cp_ring_count; j++) {
4157 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4158 
4159 			ring = &cpr2->cp_ring_struct;
4160 			ring->fw_ring_id = INVALID_HW_RING_ID;
4161 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4162 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4163 		}
4164 	}
4165 }
4166 
4167 static int bnxt_init_rx_rings(struct bnxt *bp)
4168 {
4169 	int i, rc = 0;
4170 
4171 	if (BNXT_RX_PAGE_MODE(bp)) {
4172 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4173 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4174 	} else {
4175 		bp->rx_offset = BNXT_RX_OFFSET;
4176 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4177 	}
4178 
4179 	for (i = 0; i < bp->rx_nr_rings; i++) {
4180 		rc = bnxt_init_one_rx_ring(bp, i);
4181 		if (rc)
4182 			break;
4183 	}
4184 
4185 	return rc;
4186 }
4187 
4188 static int bnxt_init_tx_rings(struct bnxt *bp)
4189 {
4190 	u16 i;
4191 
4192 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4193 				   BNXT_MIN_TX_DESC_CNT);
4194 
4195 	for (i = 0; i < bp->tx_nr_rings; i++) {
4196 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4197 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4198 
4199 		ring->fw_ring_id = INVALID_HW_RING_ID;
4200 
4201 		if (i >= bp->tx_nr_rings_xdp)
4202 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4203 					     NETDEV_QUEUE_TYPE_TX,
4204 					     &txr->bnapi->napi);
4205 	}
4206 
4207 	return 0;
4208 }
4209 
4210 static void bnxt_free_ring_grps(struct bnxt *bp)
4211 {
4212 	kfree(bp->grp_info);
4213 	bp->grp_info = NULL;
4214 }
4215 
4216 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4217 {
4218 	int i;
4219 
4220 	if (irq_re_init) {
4221 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4222 				       sizeof(struct bnxt_ring_grp_info),
4223 				       GFP_KERNEL);
4224 		if (!bp->grp_info)
4225 			return -ENOMEM;
4226 	}
4227 	for (i = 0; i < bp->cp_nr_rings; i++) {
4228 		if (irq_re_init)
4229 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4230 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4231 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4232 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4233 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4234 	}
4235 	return 0;
4236 }
4237 
4238 static void bnxt_free_vnics(struct bnxt *bp)
4239 {
4240 	kfree(bp->vnic_info);
4241 	bp->vnic_info = NULL;
4242 	bp->nr_vnics = 0;
4243 }
4244 
4245 static int bnxt_alloc_vnics(struct bnxt *bp)
4246 {
4247 	int num_vnics = 1;
4248 
4249 #ifdef CONFIG_RFS_ACCEL
4250 	if (bp->flags & BNXT_FLAG_RFS) {
4251 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4252 			num_vnics++;
4253 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4254 			num_vnics += bp->rx_nr_rings;
4255 	}
4256 #endif
4257 
4258 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4259 		num_vnics++;
4260 
4261 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4262 				GFP_KERNEL);
4263 	if (!bp->vnic_info)
4264 		return -ENOMEM;
4265 
4266 	bp->nr_vnics = num_vnics;
4267 	return 0;
4268 }
4269 
4270 static void bnxt_init_vnics(struct bnxt *bp)
4271 {
4272 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4273 	int i;
4274 
4275 	for (i = 0; i < bp->nr_vnics; i++) {
4276 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4277 		int j;
4278 
4279 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4280 		vnic->vnic_id = i;
4281 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4282 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4283 
4284 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4285 
4286 		if (bp->vnic_info[i].rss_hash_key) {
4287 			if (i == BNXT_VNIC_DEFAULT) {
4288 				u8 *key = (void *)vnic->rss_hash_key;
4289 				int k;
4290 
4291 				if (!bp->rss_hash_key_valid &&
4292 				    !bp->rss_hash_key_updated) {
4293 					get_random_bytes(bp->rss_hash_key,
4294 							 HW_HASH_KEY_SIZE);
4295 					bp->rss_hash_key_updated = true;
4296 				}
4297 
4298 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4299 				       HW_HASH_KEY_SIZE);
4300 
4301 				if (!bp->rss_hash_key_updated)
4302 					continue;
4303 
4304 				bp->rss_hash_key_updated = false;
4305 				bp->rss_hash_key_valid = true;
4306 
4307 				bp->toeplitz_prefix = 0;
4308 				for (k = 0; k < 8; k++) {
4309 					bp->toeplitz_prefix <<= 8;
4310 					bp->toeplitz_prefix |= key[k];
4311 				}
4312 			} else {
4313 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4314 				       HW_HASH_KEY_SIZE);
4315 			}
4316 		}
4317 	}
4318 }
4319 
4320 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4321 {
4322 	int pages;
4323 
4324 	pages = ring_size / desc_per_pg;
4325 
4326 	if (!pages)
4327 		return 1;
4328 
4329 	pages++;
4330 
4331 	while (pages & (pages - 1))
4332 		pages++;
4333 
4334 	return pages;
4335 }
4336 
4337 void bnxt_set_tpa_flags(struct bnxt *bp)
4338 {
4339 	bp->flags &= ~BNXT_FLAG_TPA;
4340 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4341 		return;
4342 	if (bp->dev->features & NETIF_F_LRO)
4343 		bp->flags |= BNXT_FLAG_LRO;
4344 	else if (bp->dev->features & NETIF_F_GRO_HW)
4345 		bp->flags |= BNXT_FLAG_GRO;
4346 }
4347 
4348 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4349  * be set on entry.
4350  */
4351 void bnxt_set_ring_params(struct bnxt *bp)
4352 {
4353 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4354 	u32 agg_factor = 0, agg_ring_size = 0;
4355 
4356 	/* 8 for CRC and VLAN */
4357 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4358 
4359 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4360 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4361 
4362 	bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
4363 	ring_size = bp->rx_ring_size;
4364 	bp->rx_agg_ring_size = 0;
4365 	bp->rx_agg_nr_pages = 0;
4366 
4367 	if (bp->flags & BNXT_FLAG_TPA)
4368 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4369 
4370 	bp->flags &= ~BNXT_FLAG_JUMBO;
4371 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4372 		u32 jumbo_factor;
4373 
4374 		bp->flags |= BNXT_FLAG_JUMBO;
4375 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4376 		if (jumbo_factor > agg_factor)
4377 			agg_factor = jumbo_factor;
4378 	}
4379 	if (agg_factor) {
4380 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4381 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4382 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4383 				    bp->rx_ring_size, ring_size);
4384 			bp->rx_ring_size = ring_size;
4385 		}
4386 		agg_ring_size = ring_size * agg_factor;
4387 
4388 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4389 							RX_DESC_CNT);
4390 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4391 			u32 tmp = agg_ring_size;
4392 
4393 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4394 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4395 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4396 				    tmp, agg_ring_size);
4397 		}
4398 		bp->rx_agg_ring_size = agg_ring_size;
4399 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4400 
4401 		if (BNXT_RX_PAGE_MODE(bp)) {
4402 			rx_space = PAGE_SIZE;
4403 			rx_size = PAGE_SIZE -
4404 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4405 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4406 		} else {
4407 			rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
4408 			rx_space = rx_size + NET_SKB_PAD +
4409 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4410 		}
4411 	}
4412 
4413 	bp->rx_buf_use_size = rx_size;
4414 	bp->rx_buf_size = rx_space;
4415 
4416 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4417 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4418 
4419 	ring_size = bp->tx_ring_size;
4420 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4421 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4422 
4423 	max_rx_cmpl = bp->rx_ring_size;
4424 	/* MAX TPA needs to be added because TPA_START completions are
4425 	 * immediately recycled, so the TPA completions are not bound by
4426 	 * the RX ring size.
4427 	 */
4428 	if (bp->flags & BNXT_FLAG_TPA)
4429 		max_rx_cmpl += bp->max_tpa;
4430 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4431 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4432 	bp->cp_ring_size = ring_size;
4433 
4434 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4435 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4436 		bp->cp_nr_pages = MAX_CP_PAGES;
4437 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4438 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4439 			    ring_size, bp->cp_ring_size);
4440 	}
4441 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4442 	bp->cp_ring_mask = bp->cp_bit - 1;
4443 }
4444 
4445 /* Changing allocation mode of RX rings.
4446  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4447  */
4448 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4449 {
4450 	struct net_device *dev = bp->dev;
4451 
4452 	if (page_mode) {
4453 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
4454 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4455 
4456 		if (bp->xdp_prog->aux->xdp_has_frags)
4457 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4458 		else
4459 			dev->max_mtu =
4460 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4461 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4462 			bp->flags |= BNXT_FLAG_JUMBO;
4463 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4464 		} else {
4465 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4466 			bp->rx_skb_func = bnxt_rx_page_skb;
4467 		}
4468 		bp->rx_dir = DMA_BIDIRECTIONAL;
4469 		/* Disable LRO or GRO_HW */
4470 		netdev_update_features(dev);
4471 	} else {
4472 		dev->max_mtu = bp->max_mtu;
4473 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4474 		bp->rx_dir = DMA_FROM_DEVICE;
4475 		bp->rx_skb_func = bnxt_rx_skb;
4476 	}
4477 	return 0;
4478 }
4479 
4480 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4481 {
4482 	int i;
4483 	struct bnxt_vnic_info *vnic;
4484 	struct pci_dev *pdev = bp->pdev;
4485 
4486 	if (!bp->vnic_info)
4487 		return;
4488 
4489 	for (i = 0; i < bp->nr_vnics; i++) {
4490 		vnic = &bp->vnic_info[i];
4491 
4492 		kfree(vnic->fw_grp_ids);
4493 		vnic->fw_grp_ids = NULL;
4494 
4495 		kfree(vnic->uc_list);
4496 		vnic->uc_list = NULL;
4497 
4498 		if (vnic->mc_list) {
4499 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4500 					  vnic->mc_list, vnic->mc_list_mapping);
4501 			vnic->mc_list = NULL;
4502 		}
4503 
4504 		if (vnic->rss_table) {
4505 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4506 					  vnic->rss_table,
4507 					  vnic->rss_table_dma_addr);
4508 			vnic->rss_table = NULL;
4509 		}
4510 
4511 		vnic->rss_hash_key = NULL;
4512 		vnic->flags = 0;
4513 	}
4514 }
4515 
4516 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4517 {
4518 	int i, rc = 0, size;
4519 	struct bnxt_vnic_info *vnic;
4520 	struct pci_dev *pdev = bp->pdev;
4521 	int max_rings;
4522 
4523 	for (i = 0; i < bp->nr_vnics; i++) {
4524 		vnic = &bp->vnic_info[i];
4525 
4526 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4527 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4528 
4529 			if (mem_size > 0) {
4530 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4531 				if (!vnic->uc_list) {
4532 					rc = -ENOMEM;
4533 					goto out;
4534 				}
4535 			}
4536 		}
4537 
4538 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4539 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4540 			vnic->mc_list =
4541 				dma_alloc_coherent(&pdev->dev,
4542 						   vnic->mc_list_size,
4543 						   &vnic->mc_list_mapping,
4544 						   GFP_KERNEL);
4545 			if (!vnic->mc_list) {
4546 				rc = -ENOMEM;
4547 				goto out;
4548 			}
4549 		}
4550 
4551 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4552 			goto vnic_skip_grps;
4553 
4554 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4555 			max_rings = bp->rx_nr_rings;
4556 		else
4557 			max_rings = 1;
4558 
4559 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4560 		if (!vnic->fw_grp_ids) {
4561 			rc = -ENOMEM;
4562 			goto out;
4563 		}
4564 vnic_skip_grps:
4565 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4566 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4567 			continue;
4568 
4569 		/* Allocate rss table and hash key */
4570 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4571 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4572 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4573 
4574 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4575 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4576 						     vnic->rss_table_size,
4577 						     &vnic->rss_table_dma_addr,
4578 						     GFP_KERNEL);
4579 		if (!vnic->rss_table) {
4580 			rc = -ENOMEM;
4581 			goto out;
4582 		}
4583 
4584 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4585 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4586 	}
4587 	return 0;
4588 
4589 out:
4590 	return rc;
4591 }
4592 
4593 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4594 {
4595 	struct bnxt_hwrm_wait_token *token;
4596 
4597 	dma_pool_destroy(bp->hwrm_dma_pool);
4598 	bp->hwrm_dma_pool = NULL;
4599 
4600 	rcu_read_lock();
4601 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4602 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4603 	rcu_read_unlock();
4604 }
4605 
4606 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4607 {
4608 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4609 					    BNXT_HWRM_DMA_SIZE,
4610 					    BNXT_HWRM_DMA_ALIGN, 0);
4611 	if (!bp->hwrm_dma_pool)
4612 		return -ENOMEM;
4613 
4614 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4615 
4616 	return 0;
4617 }
4618 
4619 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4620 {
4621 	kfree(stats->hw_masks);
4622 	stats->hw_masks = NULL;
4623 	kfree(stats->sw_stats);
4624 	stats->sw_stats = NULL;
4625 	if (stats->hw_stats) {
4626 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
4627 				  stats->hw_stats_map);
4628 		stats->hw_stats = NULL;
4629 	}
4630 }
4631 
4632 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
4633 				bool alloc_masks)
4634 {
4635 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
4636 					     &stats->hw_stats_map, GFP_KERNEL);
4637 	if (!stats->hw_stats)
4638 		return -ENOMEM;
4639 
4640 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
4641 	if (!stats->sw_stats)
4642 		goto stats_mem_err;
4643 
4644 	if (alloc_masks) {
4645 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
4646 		if (!stats->hw_masks)
4647 			goto stats_mem_err;
4648 	}
4649 	return 0;
4650 
4651 stats_mem_err:
4652 	bnxt_free_stats_mem(bp, stats);
4653 	return -ENOMEM;
4654 }
4655 
4656 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
4657 {
4658 	int i;
4659 
4660 	for (i = 0; i < count; i++)
4661 		mask_arr[i] = mask;
4662 }
4663 
4664 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
4665 {
4666 	int i;
4667 
4668 	for (i = 0; i < count; i++)
4669 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
4670 }
4671 
4672 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
4673 				    struct bnxt_stats_mem *stats)
4674 {
4675 	struct hwrm_func_qstats_ext_output *resp;
4676 	struct hwrm_func_qstats_ext_input *req;
4677 	__le64 *hw_masks;
4678 	int rc;
4679 
4680 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
4681 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4682 		return -EOPNOTSUPP;
4683 
4684 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
4685 	if (rc)
4686 		return rc;
4687 
4688 	req->fid = cpu_to_le16(0xffff);
4689 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4690 
4691 	resp = hwrm_req_hold(bp, req);
4692 	rc = hwrm_req_send(bp, req);
4693 	if (!rc) {
4694 		hw_masks = &resp->rx_ucast_pkts;
4695 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
4696 	}
4697 	hwrm_req_drop(bp, req);
4698 	return rc;
4699 }
4700 
4701 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
4702 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
4703 
4704 static void bnxt_init_stats(struct bnxt *bp)
4705 {
4706 	struct bnxt_napi *bnapi = bp->bnapi[0];
4707 	struct bnxt_cp_ring_info *cpr;
4708 	struct bnxt_stats_mem *stats;
4709 	__le64 *rx_stats, *tx_stats;
4710 	int rc, rx_count, tx_count;
4711 	u64 *rx_masks, *tx_masks;
4712 	u64 mask;
4713 	u8 flags;
4714 
4715 	cpr = &bnapi->cp_ring;
4716 	stats = &cpr->stats;
4717 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
4718 	if (rc) {
4719 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4720 			mask = (1ULL << 48) - 1;
4721 		else
4722 			mask = -1ULL;
4723 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
4724 	}
4725 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
4726 		stats = &bp->port_stats;
4727 		rx_stats = stats->hw_stats;
4728 		rx_masks = stats->hw_masks;
4729 		rx_count = sizeof(struct rx_port_stats) / 8;
4730 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4731 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4732 		tx_count = sizeof(struct tx_port_stats) / 8;
4733 
4734 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
4735 		rc = bnxt_hwrm_port_qstats(bp, flags);
4736 		if (rc) {
4737 			mask = (1ULL << 40) - 1;
4738 
4739 			bnxt_fill_masks(rx_masks, mask, rx_count);
4740 			bnxt_fill_masks(tx_masks, mask, tx_count);
4741 		} else {
4742 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4743 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
4744 			bnxt_hwrm_port_qstats(bp, 0);
4745 		}
4746 	}
4747 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
4748 		stats = &bp->rx_port_stats_ext;
4749 		rx_stats = stats->hw_stats;
4750 		rx_masks = stats->hw_masks;
4751 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
4752 		stats = &bp->tx_port_stats_ext;
4753 		tx_stats = stats->hw_stats;
4754 		tx_masks = stats->hw_masks;
4755 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
4756 
4757 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4758 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
4759 		if (rc) {
4760 			mask = (1ULL << 40) - 1;
4761 
4762 			bnxt_fill_masks(rx_masks, mask, rx_count);
4763 			if (tx_stats)
4764 				bnxt_fill_masks(tx_masks, mask, tx_count);
4765 		} else {
4766 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4767 			if (tx_stats)
4768 				bnxt_copy_hw_masks(tx_masks, tx_stats,
4769 						   tx_count);
4770 			bnxt_hwrm_port_qstats_ext(bp, 0);
4771 		}
4772 	}
4773 }
4774 
4775 static void bnxt_free_port_stats(struct bnxt *bp)
4776 {
4777 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
4778 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
4779 
4780 	bnxt_free_stats_mem(bp, &bp->port_stats);
4781 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
4782 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
4783 }
4784 
4785 static void bnxt_free_ring_stats(struct bnxt *bp)
4786 {
4787 	int i;
4788 
4789 	if (!bp->bnapi)
4790 		return;
4791 
4792 	for (i = 0; i < bp->cp_nr_rings; i++) {
4793 		struct bnxt_napi *bnapi = bp->bnapi[i];
4794 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4795 
4796 		bnxt_free_stats_mem(bp, &cpr->stats);
4797 
4798 		kfree(cpr->sw_stats);
4799 		cpr->sw_stats = NULL;
4800 	}
4801 }
4802 
4803 static int bnxt_alloc_stats(struct bnxt *bp)
4804 {
4805 	u32 size, i;
4806 	int rc;
4807 
4808 	size = bp->hw_ring_stats_size;
4809 
4810 	for (i = 0; i < bp->cp_nr_rings; i++) {
4811 		struct bnxt_napi *bnapi = bp->bnapi[i];
4812 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4813 
4814 		cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL);
4815 		if (!cpr->sw_stats)
4816 			return -ENOMEM;
4817 
4818 		cpr->stats.len = size;
4819 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
4820 		if (rc)
4821 			return rc;
4822 
4823 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4824 	}
4825 
4826 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
4827 		return 0;
4828 
4829 	if (bp->port_stats.hw_stats)
4830 		goto alloc_ext_stats;
4831 
4832 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
4833 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
4834 	if (rc)
4835 		return rc;
4836 
4837 	bp->flags |= BNXT_FLAG_PORT_STATS;
4838 
4839 alloc_ext_stats:
4840 	/* Display extended statistics only if FW supports it */
4841 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
4842 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
4843 			return 0;
4844 
4845 	if (bp->rx_port_stats_ext.hw_stats)
4846 		goto alloc_tx_ext_stats;
4847 
4848 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
4849 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
4850 	/* Extended stats are optional */
4851 	if (rc)
4852 		return 0;
4853 
4854 alloc_tx_ext_stats:
4855 	if (bp->tx_port_stats_ext.hw_stats)
4856 		return 0;
4857 
4858 	if (bp->hwrm_spec_code >= 0x10902 ||
4859 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
4860 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
4861 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
4862 		/* Extended stats are optional */
4863 		if (rc)
4864 			return 0;
4865 	}
4866 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
4867 	return 0;
4868 }
4869 
4870 static void bnxt_clear_ring_indices(struct bnxt *bp)
4871 {
4872 	int i, j;
4873 
4874 	if (!bp->bnapi)
4875 		return;
4876 
4877 	for (i = 0; i < bp->cp_nr_rings; i++) {
4878 		struct bnxt_napi *bnapi = bp->bnapi[i];
4879 		struct bnxt_cp_ring_info *cpr;
4880 		struct bnxt_rx_ring_info *rxr;
4881 		struct bnxt_tx_ring_info *txr;
4882 
4883 		if (!bnapi)
4884 			continue;
4885 
4886 		cpr = &bnapi->cp_ring;
4887 		cpr->cp_raw_cons = 0;
4888 
4889 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4890 			txr->tx_prod = 0;
4891 			txr->tx_cons = 0;
4892 			txr->tx_hw_cons = 0;
4893 		}
4894 
4895 		rxr = bnapi->rx_ring;
4896 		if (rxr) {
4897 			rxr->rx_prod = 0;
4898 			rxr->rx_agg_prod = 0;
4899 			rxr->rx_sw_agg_prod = 0;
4900 			rxr->rx_next_cons = 0;
4901 		}
4902 		bnapi->events = 0;
4903 	}
4904 }
4905 
4906 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
4907 {
4908 	u8 type = fltr->type, flags = fltr->flags;
4909 
4910 	INIT_LIST_HEAD(&fltr->list);
4911 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
4912 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
4913 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
4914 }
4915 
4916 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
4917 {
4918 	if (!list_empty(&fltr->list))
4919 		list_del_init(&fltr->list);
4920 }
4921 
4922 void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
4923 {
4924 	struct bnxt_filter_base *usr_fltr, *tmp;
4925 
4926 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
4927 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
4928 			continue;
4929 		bnxt_del_one_usr_fltr(bp, usr_fltr);
4930 	}
4931 }
4932 
4933 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
4934 {
4935 	hlist_del(&fltr->hash);
4936 	bnxt_del_one_usr_fltr(bp, fltr);
4937 	if (fltr->flags) {
4938 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
4939 		bp->ntp_fltr_count--;
4940 	}
4941 	kfree(fltr);
4942 }
4943 
4944 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
4945 {
4946 	int i;
4947 
4948 	/* Under rtnl_lock and all our NAPIs have been disabled.  It's
4949 	 * safe to delete the hash table.
4950 	 */
4951 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
4952 		struct hlist_head *head;
4953 		struct hlist_node *tmp;
4954 		struct bnxt_ntuple_filter *fltr;
4955 
4956 		head = &bp->ntp_fltr_hash_tbl[i];
4957 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
4958 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
4959 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
4960 				     !list_empty(&fltr->base.list)))
4961 				continue;
4962 			bnxt_del_fltr(bp, &fltr->base);
4963 		}
4964 	}
4965 	if (!all)
4966 		return;
4967 
4968 	bitmap_free(bp->ntp_fltr_bmap);
4969 	bp->ntp_fltr_bmap = NULL;
4970 	bp->ntp_fltr_count = 0;
4971 }
4972 
4973 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
4974 {
4975 	int i, rc = 0;
4976 
4977 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
4978 		return 0;
4979 
4980 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
4981 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
4982 
4983 	bp->ntp_fltr_count = 0;
4984 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
4985 
4986 	if (!bp->ntp_fltr_bmap)
4987 		rc = -ENOMEM;
4988 
4989 	return rc;
4990 }
4991 
4992 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
4993 {
4994 	int i;
4995 
4996 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
4997 		struct hlist_head *head;
4998 		struct hlist_node *tmp;
4999 		struct bnxt_l2_filter *fltr;
5000 
5001 		head = &bp->l2_fltr_hash_tbl[i];
5002 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5003 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5004 				     !list_empty(&fltr->base.list)))
5005 				continue;
5006 			bnxt_del_fltr(bp, &fltr->base);
5007 		}
5008 	}
5009 }
5010 
5011 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5012 {
5013 	int i;
5014 
5015 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5016 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5017 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5018 }
5019 
5020 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5021 {
5022 	bnxt_free_vnic_attributes(bp);
5023 	bnxt_free_tx_rings(bp);
5024 	bnxt_free_rx_rings(bp);
5025 	bnxt_free_cp_rings(bp);
5026 	bnxt_free_all_cp_arrays(bp);
5027 	bnxt_free_ntp_fltrs(bp, false);
5028 	bnxt_free_l2_filters(bp, false);
5029 	if (irq_re_init) {
5030 		bnxt_free_ring_stats(bp);
5031 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5032 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5033 			bnxt_free_port_stats(bp);
5034 		bnxt_free_ring_grps(bp);
5035 		bnxt_free_vnics(bp);
5036 		kfree(bp->tx_ring_map);
5037 		bp->tx_ring_map = NULL;
5038 		kfree(bp->tx_ring);
5039 		bp->tx_ring = NULL;
5040 		kfree(bp->rx_ring);
5041 		bp->rx_ring = NULL;
5042 		kfree(bp->bnapi);
5043 		bp->bnapi = NULL;
5044 	} else {
5045 		bnxt_clear_ring_indices(bp);
5046 	}
5047 }
5048 
5049 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5050 {
5051 	int i, j, rc, size, arr_size;
5052 	void *bnapi;
5053 
5054 	if (irq_re_init) {
5055 		/* Allocate bnapi mem pointer array and mem block for
5056 		 * all queues
5057 		 */
5058 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5059 				bp->cp_nr_rings);
5060 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5061 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5062 		if (!bnapi)
5063 			return -ENOMEM;
5064 
5065 		bp->bnapi = bnapi;
5066 		bnapi += arr_size;
5067 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5068 			bp->bnapi[i] = bnapi;
5069 			bp->bnapi[i]->index = i;
5070 			bp->bnapi[i]->bp = bp;
5071 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5072 				struct bnxt_cp_ring_info *cpr =
5073 					&bp->bnapi[i]->cp_ring;
5074 
5075 				cpr->cp_ring_struct.ring_mem.flags =
5076 					BNXT_RMEM_RING_PTE_FLAG;
5077 			}
5078 		}
5079 
5080 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5081 				      sizeof(struct bnxt_rx_ring_info),
5082 				      GFP_KERNEL);
5083 		if (!bp->rx_ring)
5084 			return -ENOMEM;
5085 
5086 		for (i = 0; i < bp->rx_nr_rings; i++) {
5087 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5088 
5089 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5090 				rxr->rx_ring_struct.ring_mem.flags =
5091 					BNXT_RMEM_RING_PTE_FLAG;
5092 				rxr->rx_agg_ring_struct.ring_mem.flags =
5093 					BNXT_RMEM_RING_PTE_FLAG;
5094 			} else {
5095 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5096 			}
5097 			rxr->bnapi = bp->bnapi[i];
5098 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5099 		}
5100 
5101 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5102 				      sizeof(struct bnxt_tx_ring_info),
5103 				      GFP_KERNEL);
5104 		if (!bp->tx_ring)
5105 			return -ENOMEM;
5106 
5107 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5108 					  GFP_KERNEL);
5109 
5110 		if (!bp->tx_ring_map)
5111 			return -ENOMEM;
5112 
5113 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5114 			j = 0;
5115 		else
5116 			j = bp->rx_nr_rings;
5117 
5118 		for (i = 0; i < bp->tx_nr_rings; i++) {
5119 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5120 			struct bnxt_napi *bnapi2;
5121 
5122 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5123 				txr->tx_ring_struct.ring_mem.flags =
5124 					BNXT_RMEM_RING_PTE_FLAG;
5125 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5126 			if (i >= bp->tx_nr_rings_xdp) {
5127 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5128 
5129 				bnapi2 = bp->bnapi[k];
5130 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5131 				txr->tx_napi_idx =
5132 					BNXT_RING_TO_TC(bp, txr->txq_index);
5133 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5134 				bnapi2->tx_int = bnxt_tx_int;
5135 			} else {
5136 				bnapi2 = bp->bnapi[j];
5137 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5138 				bnapi2->tx_ring[0] = txr;
5139 				bnapi2->tx_int = bnxt_tx_int_xdp;
5140 				j++;
5141 			}
5142 			txr->bnapi = bnapi2;
5143 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5144 				txr->tx_cpr = &bnapi2->cp_ring;
5145 		}
5146 
5147 		rc = bnxt_alloc_stats(bp);
5148 		if (rc)
5149 			goto alloc_mem_err;
5150 		bnxt_init_stats(bp);
5151 
5152 		rc = bnxt_alloc_ntp_fltrs(bp);
5153 		if (rc)
5154 			goto alloc_mem_err;
5155 
5156 		rc = bnxt_alloc_vnics(bp);
5157 		if (rc)
5158 			goto alloc_mem_err;
5159 	}
5160 
5161 	rc = bnxt_alloc_all_cp_arrays(bp);
5162 	if (rc)
5163 		goto alloc_mem_err;
5164 
5165 	bnxt_init_ring_struct(bp);
5166 
5167 	rc = bnxt_alloc_rx_rings(bp);
5168 	if (rc)
5169 		goto alloc_mem_err;
5170 
5171 	rc = bnxt_alloc_tx_rings(bp);
5172 	if (rc)
5173 		goto alloc_mem_err;
5174 
5175 	rc = bnxt_alloc_cp_rings(bp);
5176 	if (rc)
5177 		goto alloc_mem_err;
5178 
5179 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5180 						  BNXT_VNIC_MCAST_FLAG |
5181 						  BNXT_VNIC_UCAST_FLAG;
5182 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5183 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5184 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5185 
5186 	rc = bnxt_alloc_vnic_attributes(bp);
5187 	if (rc)
5188 		goto alloc_mem_err;
5189 	return 0;
5190 
5191 alloc_mem_err:
5192 	bnxt_free_mem(bp, true);
5193 	return rc;
5194 }
5195 
5196 static void bnxt_disable_int(struct bnxt *bp)
5197 {
5198 	int i;
5199 
5200 	if (!bp->bnapi)
5201 		return;
5202 
5203 	for (i = 0; i < bp->cp_nr_rings; i++) {
5204 		struct bnxt_napi *bnapi = bp->bnapi[i];
5205 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5206 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5207 
5208 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5209 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5210 	}
5211 }
5212 
5213 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5214 {
5215 	struct bnxt_napi *bnapi = bp->bnapi[n];
5216 	struct bnxt_cp_ring_info *cpr;
5217 
5218 	cpr = &bnapi->cp_ring;
5219 	return cpr->cp_ring_struct.map_idx;
5220 }
5221 
5222 static void bnxt_disable_int_sync(struct bnxt *bp)
5223 {
5224 	int i;
5225 
5226 	if (!bp->irq_tbl)
5227 		return;
5228 
5229 	atomic_inc(&bp->intr_sem);
5230 
5231 	bnxt_disable_int(bp);
5232 	for (i = 0; i < bp->cp_nr_rings; i++) {
5233 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5234 
5235 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5236 	}
5237 }
5238 
5239 static void bnxt_enable_int(struct bnxt *bp)
5240 {
5241 	int i;
5242 
5243 	atomic_set(&bp->intr_sem, 0);
5244 	for (i = 0; i < bp->cp_nr_rings; i++) {
5245 		struct bnxt_napi *bnapi = bp->bnapi[i];
5246 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5247 
5248 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5249 	}
5250 }
5251 
5252 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5253 			    bool async_only)
5254 {
5255 	DECLARE_BITMAP(async_events_bmap, 256);
5256 	u32 *events = (u32 *)async_events_bmap;
5257 	struct hwrm_func_drv_rgtr_output *resp;
5258 	struct hwrm_func_drv_rgtr_input *req;
5259 	u32 flags;
5260 	int rc, i;
5261 
5262 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5263 	if (rc)
5264 		return rc;
5265 
5266 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5267 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5268 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5269 
5270 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5271 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5272 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5273 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5274 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5275 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5276 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5277 	req->flags = cpu_to_le32(flags);
5278 	req->ver_maj_8b = DRV_VER_MAJ;
5279 	req->ver_min_8b = DRV_VER_MIN;
5280 	req->ver_upd_8b = DRV_VER_UPD;
5281 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5282 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5283 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5284 
5285 	if (BNXT_PF(bp)) {
5286 		u32 data[8];
5287 		int i;
5288 
5289 		memset(data, 0, sizeof(data));
5290 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5291 			u16 cmd = bnxt_vf_req_snif[i];
5292 			unsigned int bit, idx;
5293 
5294 			idx = cmd / 32;
5295 			bit = cmd % 32;
5296 			data[idx] |= 1 << bit;
5297 		}
5298 
5299 		for (i = 0; i < 8; i++)
5300 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5301 
5302 		req->enables |=
5303 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5304 	}
5305 
5306 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5307 		req->flags |= cpu_to_le32(
5308 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5309 
5310 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5311 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5312 		u16 event_id = bnxt_async_events_arr[i];
5313 
5314 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5315 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5316 			continue;
5317 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5318 		    !bp->ptp_cfg)
5319 			continue;
5320 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5321 	}
5322 	if (bmap && bmap_size) {
5323 		for (i = 0; i < bmap_size; i++) {
5324 			if (test_bit(i, bmap))
5325 				__set_bit(i, async_events_bmap);
5326 		}
5327 	}
5328 	for (i = 0; i < 8; i++)
5329 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5330 
5331 	if (async_only)
5332 		req->enables =
5333 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5334 
5335 	resp = hwrm_req_hold(bp, req);
5336 	rc = hwrm_req_send(bp, req);
5337 	if (!rc) {
5338 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5339 		if (resp->flags &
5340 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5341 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5342 	}
5343 	hwrm_req_drop(bp, req);
5344 	return rc;
5345 }
5346 
5347 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5348 {
5349 	struct hwrm_func_drv_unrgtr_input *req;
5350 	int rc;
5351 
5352 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5353 		return 0;
5354 
5355 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5356 	if (rc)
5357 		return rc;
5358 	return hwrm_req_send(bp, req);
5359 }
5360 
5361 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5362 
5363 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5364 {
5365 	struct hwrm_tunnel_dst_port_free_input *req;
5366 	int rc;
5367 
5368 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5369 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5370 		return 0;
5371 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5372 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5373 		return 0;
5374 
5375 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5376 	if (rc)
5377 		return rc;
5378 
5379 	req->tunnel_type = tunnel_type;
5380 
5381 	switch (tunnel_type) {
5382 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5383 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5384 		bp->vxlan_port = 0;
5385 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5386 		break;
5387 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5388 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5389 		bp->nge_port = 0;
5390 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5391 		break;
5392 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5393 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5394 		bp->vxlan_gpe_port = 0;
5395 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5396 		break;
5397 	default:
5398 		break;
5399 	}
5400 
5401 	rc = hwrm_req_send(bp, req);
5402 	if (rc)
5403 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5404 			   rc);
5405 	if (bp->flags & BNXT_FLAG_TPA)
5406 		bnxt_set_tpa(bp, true);
5407 	return rc;
5408 }
5409 
5410 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5411 					   u8 tunnel_type)
5412 {
5413 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5414 	struct hwrm_tunnel_dst_port_alloc_input *req;
5415 	int rc;
5416 
5417 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5418 	if (rc)
5419 		return rc;
5420 
5421 	req->tunnel_type = tunnel_type;
5422 	req->tunnel_dst_port_val = port;
5423 
5424 	resp = hwrm_req_hold(bp, req);
5425 	rc = hwrm_req_send(bp, req);
5426 	if (rc) {
5427 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5428 			   rc);
5429 		goto err_out;
5430 	}
5431 
5432 	switch (tunnel_type) {
5433 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5434 		bp->vxlan_port = port;
5435 		bp->vxlan_fw_dst_port_id =
5436 			le16_to_cpu(resp->tunnel_dst_port_id);
5437 		break;
5438 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5439 		bp->nge_port = port;
5440 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5441 		break;
5442 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5443 		bp->vxlan_gpe_port = port;
5444 		bp->vxlan_gpe_fw_dst_port_id =
5445 			le16_to_cpu(resp->tunnel_dst_port_id);
5446 		break;
5447 	default:
5448 		break;
5449 	}
5450 	if (bp->flags & BNXT_FLAG_TPA)
5451 		bnxt_set_tpa(bp, true);
5452 
5453 err_out:
5454 	hwrm_req_drop(bp, req);
5455 	return rc;
5456 }
5457 
5458 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5459 {
5460 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5461 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5462 	int rc;
5463 
5464 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5465 	if (rc)
5466 		return rc;
5467 
5468 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5469 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5470 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5471 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5472 	}
5473 	req->mask = cpu_to_le32(vnic->rx_mask);
5474 	return hwrm_req_send_silent(bp, req);
5475 }
5476 
5477 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5478 {
5479 	if (!atomic_dec_and_test(&fltr->refcnt))
5480 		return;
5481 	spin_lock_bh(&bp->ntp_fltr_lock);
5482 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5483 		spin_unlock_bh(&bp->ntp_fltr_lock);
5484 		return;
5485 	}
5486 	hlist_del_rcu(&fltr->base.hash);
5487 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5488 	if (fltr->base.flags) {
5489 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5490 		bp->ntp_fltr_count--;
5491 	}
5492 	spin_unlock_bh(&bp->ntp_fltr_lock);
5493 	kfree_rcu(fltr, base.rcu);
5494 }
5495 
5496 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5497 						      struct bnxt_l2_key *key,
5498 						      u32 idx)
5499 {
5500 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5501 	struct bnxt_l2_filter *fltr;
5502 
5503 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5504 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5505 
5506 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5507 		    l2_key->vlan == key->vlan)
5508 			return fltr;
5509 	}
5510 	return NULL;
5511 }
5512 
5513 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5514 						    struct bnxt_l2_key *key,
5515 						    u32 idx)
5516 {
5517 	struct bnxt_l2_filter *fltr = NULL;
5518 
5519 	rcu_read_lock();
5520 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5521 	if (fltr)
5522 		atomic_inc(&fltr->refcnt);
5523 	rcu_read_unlock();
5524 	return fltr;
5525 }
5526 
5527 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5528 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5529 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5530 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5531 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5532 
5533 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5534 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5535 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5536 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5537 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5538 
5539 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5540 {
5541 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5542 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5543 			return sizeof(fkeys->addrs.v4addrs) +
5544 			       sizeof(fkeys->ports);
5545 
5546 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5547 			return sizeof(fkeys->addrs.v4addrs);
5548 	}
5549 
5550 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5551 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5552 			return sizeof(fkeys->addrs.v6addrs) +
5553 			       sizeof(fkeys->ports);
5554 
5555 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5556 			return sizeof(fkeys->addrs.v6addrs);
5557 	}
5558 
5559 	return 0;
5560 }
5561 
5562 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5563 			 const unsigned char *key)
5564 {
5565 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5566 	struct bnxt_ipv4_tuple tuple4;
5567 	struct bnxt_ipv6_tuple tuple6;
5568 	int i, j, len = 0;
5569 	u8 *four_tuple;
5570 
5571 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5572 	if (!len)
5573 		return 0;
5574 
5575 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5576 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5577 		tuple4.ports = fkeys->ports;
5578 		four_tuple = (unsigned char *)&tuple4;
5579 	} else {
5580 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5581 		tuple6.ports = fkeys->ports;
5582 		four_tuple = (unsigned char *)&tuple6;
5583 	}
5584 
5585 	for (i = 0, j = 8; i < len; i++, j++) {
5586 		u8 byte = four_tuple[i];
5587 		int bit;
5588 
5589 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5590 			if (byte & 0x80)
5591 				hash ^= prefix;
5592 		}
5593 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5594 	}
5595 
5596 	/* The valid part of the hash is in the upper 32 bits. */
5597 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5598 }
5599 
5600 #ifdef CONFIG_RFS_ACCEL
5601 static struct bnxt_l2_filter *
5602 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5603 {
5604 	struct bnxt_l2_filter *fltr;
5605 	u32 idx;
5606 
5607 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5608 	      BNXT_L2_FLTR_HASH_MASK;
5609 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5610 	return fltr;
5611 }
5612 #endif
5613 
5614 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
5615 			       struct bnxt_l2_key *key, u32 idx)
5616 {
5617 	struct hlist_head *head;
5618 
5619 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
5620 	fltr->l2_key.vlan = key->vlan;
5621 	fltr->base.type = BNXT_FLTR_TYPE_L2;
5622 	if (fltr->base.flags) {
5623 		int bit_id;
5624 
5625 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
5626 						 bp->max_fltr, 0);
5627 		if (bit_id < 0)
5628 			return -ENOMEM;
5629 		fltr->base.sw_id = (u16)bit_id;
5630 		bp->ntp_fltr_count++;
5631 	}
5632 	head = &bp->l2_fltr_hash_tbl[idx];
5633 	hlist_add_head_rcu(&fltr->base.hash, head);
5634 	bnxt_insert_usr_fltr(bp, &fltr->base);
5635 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
5636 	atomic_set(&fltr->refcnt, 1);
5637 	return 0;
5638 }
5639 
5640 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
5641 						   struct bnxt_l2_key *key,
5642 						   gfp_t gfp)
5643 {
5644 	struct bnxt_l2_filter *fltr;
5645 	u32 idx;
5646 	int rc;
5647 
5648 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5649 	      BNXT_L2_FLTR_HASH_MASK;
5650 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5651 	if (fltr)
5652 		return fltr;
5653 
5654 	fltr = kzalloc(sizeof(*fltr), gfp);
5655 	if (!fltr)
5656 		return ERR_PTR(-ENOMEM);
5657 	spin_lock_bh(&bp->ntp_fltr_lock);
5658 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5659 	spin_unlock_bh(&bp->ntp_fltr_lock);
5660 	if (rc) {
5661 		bnxt_del_l2_filter(bp, fltr);
5662 		fltr = ERR_PTR(rc);
5663 	}
5664 	return fltr;
5665 }
5666 
5667 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
5668 						struct bnxt_l2_key *key,
5669 						u16 flags)
5670 {
5671 	struct bnxt_l2_filter *fltr;
5672 	u32 idx;
5673 	int rc;
5674 
5675 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5676 	      BNXT_L2_FLTR_HASH_MASK;
5677 	spin_lock_bh(&bp->ntp_fltr_lock);
5678 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5679 	if (fltr) {
5680 		fltr = ERR_PTR(-EEXIST);
5681 		goto l2_filter_exit;
5682 	}
5683 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
5684 	if (!fltr) {
5685 		fltr = ERR_PTR(-ENOMEM);
5686 		goto l2_filter_exit;
5687 	}
5688 	fltr->base.flags = flags;
5689 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5690 	if (rc) {
5691 		spin_unlock_bh(&bp->ntp_fltr_lock);
5692 		bnxt_del_l2_filter(bp, fltr);
5693 		return ERR_PTR(rc);
5694 	}
5695 
5696 l2_filter_exit:
5697 	spin_unlock_bh(&bp->ntp_fltr_lock);
5698 	return fltr;
5699 }
5700 
5701 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
5702 {
5703 #ifdef CONFIG_BNXT_SRIOV
5704 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
5705 
5706 	return vf->fw_fid;
5707 #else
5708 	return INVALID_HW_RING_ID;
5709 #endif
5710 }
5711 
5712 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5713 {
5714 	struct hwrm_cfa_l2_filter_free_input *req;
5715 	u16 target_id = 0xffff;
5716 	int rc;
5717 
5718 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
5719 		struct bnxt_pf_info *pf = &bp->pf;
5720 
5721 		if (fltr->base.vf_idx >= pf->active_vfs)
5722 			return -EINVAL;
5723 
5724 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
5725 		if (target_id == INVALID_HW_RING_ID)
5726 			return -EINVAL;
5727 	}
5728 
5729 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
5730 	if (rc)
5731 		return rc;
5732 
5733 	req->target_id = cpu_to_le16(target_id);
5734 	req->l2_filter_id = fltr->base.filter_id;
5735 	return hwrm_req_send(bp, req);
5736 }
5737 
5738 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5739 {
5740 	struct hwrm_cfa_l2_filter_alloc_output *resp;
5741 	struct hwrm_cfa_l2_filter_alloc_input *req;
5742 	u16 target_id = 0xffff;
5743 	int rc;
5744 
5745 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
5746 		struct bnxt_pf_info *pf = &bp->pf;
5747 
5748 		if (fltr->base.vf_idx >= pf->active_vfs)
5749 			return -EINVAL;
5750 
5751 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
5752 	}
5753 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
5754 	if (rc)
5755 		return rc;
5756 
5757 	req->target_id = cpu_to_le16(target_id);
5758 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
5759 
5760 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
5761 		req->flags |=
5762 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
5763 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
5764 	req->enables =
5765 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
5766 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
5767 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
5768 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
5769 	eth_broadcast_addr(req->l2_addr_mask);
5770 
5771 	if (fltr->l2_key.vlan) {
5772 		req->enables |=
5773 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
5774 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
5775 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
5776 		req->num_vlans = 1;
5777 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
5778 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
5779 	}
5780 
5781 	resp = hwrm_req_hold(bp, req);
5782 	rc = hwrm_req_send(bp, req);
5783 	if (!rc) {
5784 		fltr->base.filter_id = resp->l2_filter_id;
5785 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
5786 	}
5787 	hwrm_req_drop(bp, req);
5788 	return rc;
5789 }
5790 
5791 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
5792 				     struct bnxt_ntuple_filter *fltr)
5793 {
5794 	struct hwrm_cfa_ntuple_filter_free_input *req;
5795 	int rc;
5796 
5797 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
5798 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
5799 	if (rc)
5800 		return rc;
5801 
5802 	req->ntuple_filter_id = fltr->base.filter_id;
5803 	return hwrm_req_send(bp, req);
5804 }
5805 
5806 #define BNXT_NTP_FLTR_FLAGS					\
5807 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
5808 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
5809 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
5810 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
5811 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
5812 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
5813 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
5814 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
5815 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
5816 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
5817 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
5818 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
5819 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
5820 
5821 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
5822 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
5823 
5824 void bnxt_fill_ipv6_mask(__be32 mask[4])
5825 {
5826 	int i;
5827 
5828 	for (i = 0; i < 4; i++)
5829 		mask[i] = cpu_to_be32(~0);
5830 }
5831 
5832 static void
5833 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
5834 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
5835 			  struct bnxt_ntuple_filter *fltr)
5836 {
5837 	struct bnxt_rss_ctx *rss_ctx, *tmp;
5838 	u16 rxq = fltr->base.rxq;
5839 
5840 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
5841 		list_for_each_entry_safe(rss_ctx, tmp, &bp->rss_ctx_list, list) {
5842 			if (rss_ctx->index == fltr->base.fw_vnic_id) {
5843 				struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
5844 
5845 				req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
5846 				break;
5847 			}
5848 		}
5849 		return;
5850 	}
5851 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
5852 		struct bnxt_vnic_info *vnic;
5853 		u32 enables;
5854 
5855 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
5856 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
5857 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
5858 		req->enables |= cpu_to_le32(enables);
5859 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
5860 	} else {
5861 		u32 flags;
5862 
5863 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
5864 		req->flags |= cpu_to_le32(flags);
5865 		req->dst_id = cpu_to_le16(rxq);
5866 	}
5867 }
5868 
5869 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
5870 				      struct bnxt_ntuple_filter *fltr)
5871 {
5872 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
5873 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
5874 	struct bnxt_flow_masks *masks = &fltr->fmasks;
5875 	struct flow_keys *keys = &fltr->fkeys;
5876 	struct bnxt_l2_filter *l2_fltr;
5877 	struct bnxt_vnic_info *vnic;
5878 	int rc;
5879 
5880 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
5881 	if (rc)
5882 		return rc;
5883 
5884 	l2_fltr = fltr->l2_fltr;
5885 	req->l2_filter_id = l2_fltr->base.filter_id;
5886 
5887 	if (fltr->base.flags & BNXT_ACT_DROP) {
5888 		req->flags =
5889 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
5890 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
5891 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
5892 	} else {
5893 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
5894 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
5895 	}
5896 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
5897 
5898 	req->ethertype = htons(ETH_P_IP);
5899 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
5900 	req->ip_protocol = keys->basic.ip_proto;
5901 
5902 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
5903 		req->ethertype = htons(ETH_P_IPV6);
5904 		req->ip_addr_type =
5905 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
5906 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
5907 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
5908 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
5909 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
5910 	} else {
5911 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
5912 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
5913 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
5914 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
5915 	}
5916 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
5917 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
5918 		req->tunnel_type =
5919 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
5920 	}
5921 
5922 	req->src_port = keys->ports.src;
5923 	req->src_port_mask = masks->ports.src;
5924 	req->dst_port = keys->ports.dst;
5925 	req->dst_port_mask = masks->ports.dst;
5926 
5927 	resp = hwrm_req_hold(bp, req);
5928 	rc = hwrm_req_send(bp, req);
5929 	if (!rc)
5930 		fltr->base.filter_id = resp->ntuple_filter_id;
5931 	hwrm_req_drop(bp, req);
5932 	return rc;
5933 }
5934 
5935 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
5936 				     const u8 *mac_addr)
5937 {
5938 	struct bnxt_l2_filter *fltr;
5939 	struct bnxt_l2_key key;
5940 	int rc;
5941 
5942 	ether_addr_copy(key.dst_mac_addr, mac_addr);
5943 	key.vlan = 0;
5944 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
5945 	if (IS_ERR(fltr))
5946 		return PTR_ERR(fltr);
5947 
5948 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
5949 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
5950 	if (rc)
5951 		bnxt_del_l2_filter(bp, fltr);
5952 	else
5953 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
5954 	return rc;
5955 }
5956 
5957 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
5958 {
5959 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
5960 
5961 	/* Any associated ntuple filters will also be cleared by firmware. */
5962 	for (i = 0; i < num_of_vnics; i++) {
5963 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
5964 
5965 		for (j = 0; j < vnic->uc_filter_count; j++) {
5966 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
5967 
5968 			bnxt_hwrm_l2_filter_free(bp, fltr);
5969 			bnxt_del_l2_filter(bp, fltr);
5970 		}
5971 		vnic->uc_filter_count = 0;
5972 	}
5973 }
5974 
5975 #define BNXT_DFLT_TUNL_TPA_BMAP				\
5976 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
5977 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
5978 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
5979 
5980 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
5981 					   struct hwrm_vnic_tpa_cfg_input *req)
5982 {
5983 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
5984 
5985 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
5986 		return;
5987 
5988 	if (bp->vxlan_port)
5989 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
5990 	if (bp->vxlan_gpe_port)
5991 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
5992 	if (bp->nge_port)
5993 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
5994 
5995 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
5996 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
5997 }
5998 
5999 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6000 			   u32 tpa_flags)
6001 {
6002 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6003 	struct hwrm_vnic_tpa_cfg_input *req;
6004 	int rc;
6005 
6006 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6007 		return 0;
6008 
6009 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6010 	if (rc)
6011 		return rc;
6012 
6013 	if (tpa_flags) {
6014 		u16 mss = bp->dev->mtu - 40;
6015 		u32 nsegs, n, segs = 0, flags;
6016 
6017 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6018 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6019 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6020 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6021 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6022 		if (tpa_flags & BNXT_FLAG_GRO)
6023 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6024 
6025 		req->flags = cpu_to_le32(flags);
6026 
6027 		req->enables =
6028 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6029 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6030 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6031 
6032 		/* Number of segs are log2 units, and first packet is not
6033 		 * included as part of this units.
6034 		 */
6035 		if (mss <= BNXT_RX_PAGE_SIZE) {
6036 			n = BNXT_RX_PAGE_SIZE / mss;
6037 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6038 		} else {
6039 			n = mss / BNXT_RX_PAGE_SIZE;
6040 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6041 				n++;
6042 			nsegs = (MAX_SKB_FRAGS - n) / n;
6043 		}
6044 
6045 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6046 			segs = MAX_TPA_SEGS_P5;
6047 			max_aggs = bp->max_tpa;
6048 		} else {
6049 			segs = ilog2(nsegs);
6050 		}
6051 		req->max_agg_segs = cpu_to_le16(segs);
6052 		req->max_aggs = cpu_to_le16(max_aggs);
6053 
6054 		req->min_agg_len = cpu_to_le32(512);
6055 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6056 	}
6057 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6058 
6059 	return hwrm_req_send(bp, req);
6060 }
6061 
6062 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6063 {
6064 	struct bnxt_ring_grp_info *grp_info;
6065 
6066 	grp_info = &bp->grp_info[ring->grp_idx];
6067 	return grp_info->cp_fw_ring_id;
6068 }
6069 
6070 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6071 {
6072 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6073 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6074 	else
6075 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6076 }
6077 
6078 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6079 {
6080 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6081 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6082 	else
6083 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6084 }
6085 
6086 int bnxt_alloc_rss_indir_tbl(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx)
6087 {
6088 	int entries;
6089 	u16 *tbl;
6090 
6091 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6092 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6093 	else
6094 		entries = HW_HASH_INDEX_SIZE;
6095 
6096 	bp->rss_indir_tbl_entries = entries;
6097 	tbl = kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6098 	if (!tbl)
6099 		return -ENOMEM;
6100 
6101 	if (rss_ctx)
6102 		rss_ctx->rss_indir_tbl = tbl;
6103 	else
6104 		bp->rss_indir_tbl = tbl;
6105 
6106 	return 0;
6107 }
6108 
6109 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx)
6110 {
6111 	u16 max_rings, max_entries, pad, i;
6112 	u16 *rss_indir_tbl;
6113 
6114 	if (!bp->rx_nr_rings)
6115 		return;
6116 
6117 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6118 		max_rings = bp->rx_nr_rings - 1;
6119 	else
6120 		max_rings = bp->rx_nr_rings;
6121 
6122 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6123 	if (rss_ctx)
6124 		rss_indir_tbl = &rss_ctx->rss_indir_tbl[0];
6125 	else
6126 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6127 
6128 	for (i = 0; i < max_entries; i++)
6129 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6130 
6131 	pad = bp->rss_indir_tbl_entries - max_entries;
6132 	if (pad)
6133 		memset(&rss_indir_tbl[i], 0, pad * sizeof(u16));
6134 }
6135 
6136 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6137 {
6138 	u16 i, tbl_size, max_ring = 0;
6139 
6140 	if (!bp->rss_indir_tbl)
6141 		return 0;
6142 
6143 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6144 	for (i = 0; i < tbl_size; i++)
6145 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6146 	return max_ring;
6147 }
6148 
6149 u16 bnxt_get_max_rss_ctx_ring(struct bnxt *bp)
6150 {
6151 	u16 i, tbl_size, max_ring = 0;
6152 	struct bnxt_rss_ctx *rss_ctx;
6153 
6154 	if (!BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
6155 		return 0;
6156 
6157 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6158 
6159 	list_for_each_entry(rss_ctx, &bp->rss_ctx_list, list) {
6160 		for (i = 0; i < tbl_size; i++)
6161 			max_ring = max(max_ring, rss_ctx->rss_indir_tbl[i]);
6162 	}
6163 
6164 	return max_ring;
6165 }
6166 
6167 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6168 {
6169 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6170 		if (!rx_rings)
6171 			return 0;
6172 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6173 					       BNXT_RSS_TABLE_ENTRIES_P5);
6174 	}
6175 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6176 		return 2;
6177 	return 1;
6178 }
6179 
6180 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6181 {
6182 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6183 	u16 i, j;
6184 
6185 	/* Fill the RSS indirection table with ring group ids */
6186 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6187 		if (!no_rss)
6188 			j = bp->rss_indir_tbl[i];
6189 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6190 	}
6191 }
6192 
6193 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6194 				    struct bnxt_vnic_info *vnic)
6195 {
6196 	__le16 *ring_tbl = vnic->rss_table;
6197 	struct bnxt_rx_ring_info *rxr;
6198 	u16 tbl_size, i;
6199 
6200 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6201 
6202 	for (i = 0; i < tbl_size; i++) {
6203 		u16 ring_id, j;
6204 
6205 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6206 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6207 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6208 			j = vnic->rss_ctx->rss_indir_tbl[i];
6209 		else
6210 			j = bp->rss_indir_tbl[i];
6211 		rxr = &bp->rx_ring[j];
6212 
6213 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6214 		*ring_tbl++ = cpu_to_le16(ring_id);
6215 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6216 		*ring_tbl++ = cpu_to_le16(ring_id);
6217 	}
6218 }
6219 
6220 static void
6221 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6222 			 struct bnxt_vnic_info *vnic)
6223 {
6224 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6225 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6226 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6227 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6228 	} else {
6229 		bnxt_fill_hw_rss_tbl(bp, vnic);
6230 	}
6231 
6232 	if (bp->rss_hash_delta) {
6233 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6234 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6235 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6236 		else
6237 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6238 	} else {
6239 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6240 	}
6241 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6242 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6243 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6244 }
6245 
6246 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6247 				  bool set_rss)
6248 {
6249 	struct hwrm_vnic_rss_cfg_input *req;
6250 	int rc;
6251 
6252 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6253 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6254 		return 0;
6255 
6256 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6257 	if (rc)
6258 		return rc;
6259 
6260 	if (set_rss)
6261 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6262 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6263 	return hwrm_req_send(bp, req);
6264 }
6265 
6266 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6267 				     struct bnxt_vnic_info *vnic, bool set_rss)
6268 {
6269 	struct hwrm_vnic_rss_cfg_input *req;
6270 	dma_addr_t ring_tbl_map;
6271 	u32 i, nr_ctxs;
6272 	int rc;
6273 
6274 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6275 	if (rc)
6276 		return rc;
6277 
6278 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6279 	if (!set_rss)
6280 		return hwrm_req_send(bp, req);
6281 
6282 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6283 	ring_tbl_map = vnic->rss_table_dma_addr;
6284 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6285 
6286 	hwrm_req_hold(bp, req);
6287 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6288 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6289 		req->ring_table_pair_index = i;
6290 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6291 		rc = hwrm_req_send(bp, req);
6292 		if (rc)
6293 			goto exit;
6294 	}
6295 
6296 exit:
6297 	hwrm_req_drop(bp, req);
6298 	return rc;
6299 }
6300 
6301 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6302 {
6303 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6304 	struct hwrm_vnic_rss_qcfg_output *resp;
6305 	struct hwrm_vnic_rss_qcfg_input *req;
6306 
6307 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6308 		return;
6309 
6310 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6311 	/* all contexts configured to same hash_type, zero always exists */
6312 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6313 	resp = hwrm_req_hold(bp, req);
6314 	if (!hwrm_req_send(bp, req)) {
6315 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6316 		bp->rss_hash_delta = 0;
6317 	}
6318 	hwrm_req_drop(bp, req);
6319 }
6320 
6321 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6322 {
6323 	struct hwrm_vnic_plcmodes_cfg_input *req;
6324 	int rc;
6325 
6326 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6327 	if (rc)
6328 		return rc;
6329 
6330 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6331 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6332 
6333 	if (BNXT_RX_PAGE_MODE(bp)) {
6334 		req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6335 	} else {
6336 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6337 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6338 		req->enables |=
6339 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6340 		req->jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
6341 		req->hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
6342 	}
6343 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6344 	return hwrm_req_send(bp, req);
6345 }
6346 
6347 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6348 					struct bnxt_vnic_info *vnic,
6349 					u16 ctx_idx)
6350 {
6351 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6352 
6353 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6354 		return;
6355 
6356 	req->rss_cos_lb_ctx_id =
6357 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6358 
6359 	hwrm_req_send(bp, req);
6360 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6361 }
6362 
6363 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6364 {
6365 	int i, j;
6366 
6367 	for (i = 0; i < bp->nr_vnics; i++) {
6368 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6369 
6370 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6371 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6372 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6373 		}
6374 	}
6375 	bp->rsscos_nr_ctxs = 0;
6376 }
6377 
6378 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6379 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6380 {
6381 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6382 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6383 	int rc;
6384 
6385 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6386 	if (rc)
6387 		return rc;
6388 
6389 	resp = hwrm_req_hold(bp, req);
6390 	rc = hwrm_req_send(bp, req);
6391 	if (!rc)
6392 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6393 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6394 	hwrm_req_drop(bp, req);
6395 
6396 	return rc;
6397 }
6398 
6399 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6400 {
6401 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6402 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6403 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6404 }
6405 
6406 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6407 {
6408 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6409 	struct hwrm_vnic_cfg_input *req;
6410 	unsigned int ring = 0, grp_idx;
6411 	u16 def_vlan = 0;
6412 	int rc;
6413 
6414 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6415 	if (rc)
6416 		return rc;
6417 
6418 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6419 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6420 
6421 		req->default_rx_ring_id =
6422 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6423 		req->default_cmpl_ring_id =
6424 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6425 		req->enables =
6426 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6427 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6428 		goto vnic_mru;
6429 	}
6430 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6431 	/* Only RSS support for now TBD: COS & LB */
6432 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6433 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6434 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6435 					   VNIC_CFG_REQ_ENABLES_MRU);
6436 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6437 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6438 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6439 					   VNIC_CFG_REQ_ENABLES_MRU);
6440 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6441 	} else {
6442 		req->rss_rule = cpu_to_le16(0xffff);
6443 	}
6444 
6445 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6446 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6447 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6448 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6449 	} else {
6450 		req->cos_rule = cpu_to_le16(0xffff);
6451 	}
6452 
6453 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6454 		ring = 0;
6455 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6456 		ring = vnic->vnic_id - 1;
6457 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6458 		ring = bp->rx_nr_rings - 1;
6459 
6460 	grp_idx = bp->rx_ring[ring].bnapi->index;
6461 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6462 	req->lb_rule = cpu_to_le16(0xffff);
6463 vnic_mru:
6464 	req->mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + VLAN_HLEN);
6465 
6466 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6467 #ifdef CONFIG_BNXT_SRIOV
6468 	if (BNXT_VF(bp))
6469 		def_vlan = bp->vf.vlan;
6470 #endif
6471 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6472 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6473 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6474 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6475 
6476 	return hwrm_req_send(bp, req);
6477 }
6478 
6479 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6480 				    struct bnxt_vnic_info *vnic)
6481 {
6482 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6483 		struct hwrm_vnic_free_input *req;
6484 
6485 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6486 			return;
6487 
6488 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6489 
6490 		hwrm_req_send(bp, req);
6491 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6492 	}
6493 }
6494 
6495 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6496 {
6497 	u16 i;
6498 
6499 	for (i = 0; i < bp->nr_vnics; i++)
6500 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6501 }
6502 
6503 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6504 			 unsigned int start_rx_ring_idx,
6505 			 unsigned int nr_rings)
6506 {
6507 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6508 	struct hwrm_vnic_alloc_output *resp;
6509 	struct hwrm_vnic_alloc_input *req;
6510 	int rc;
6511 
6512 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6513 	if (rc)
6514 		return rc;
6515 
6516 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6517 		goto vnic_no_ring_grps;
6518 
6519 	/* map ring groups to this vnic */
6520 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6521 		grp_idx = bp->rx_ring[i].bnapi->index;
6522 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6523 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6524 				   j, nr_rings);
6525 			break;
6526 		}
6527 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6528 	}
6529 
6530 vnic_no_ring_grps:
6531 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6532 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6533 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6534 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6535 
6536 	resp = hwrm_req_hold(bp, req);
6537 	rc = hwrm_req_send(bp, req);
6538 	if (!rc)
6539 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6540 	hwrm_req_drop(bp, req);
6541 	return rc;
6542 }
6543 
6544 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6545 {
6546 	struct hwrm_vnic_qcaps_output *resp;
6547 	struct hwrm_vnic_qcaps_input *req;
6548 	int rc;
6549 
6550 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6551 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6552 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6553 	if (bp->hwrm_spec_code < 0x10600)
6554 		return 0;
6555 
6556 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6557 	if (rc)
6558 		return rc;
6559 
6560 	resp = hwrm_req_hold(bp, req);
6561 	rc = hwrm_req_send(bp, req);
6562 	if (!rc) {
6563 		u32 flags = le32_to_cpu(resp->flags);
6564 
6565 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6566 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6567 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6568 		if (flags &
6569 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6570 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6571 
6572 		/* Older P5 fw before EXT_HW_STATS support did not set
6573 		 * VLAN_STRIP_CAP properly.
6574 		 */
6575 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6576 		    (BNXT_CHIP_P5(bp) &&
6577 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6578 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6579 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6580 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6581 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6582 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6583 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6584 		if (bp->max_tpa_v2) {
6585 			if (BNXT_CHIP_P5(bp))
6586 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6587 			else
6588 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6589 		}
6590 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6591 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6592 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6593 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6594 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6595 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6596 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6597 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6598 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6599 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6600 	}
6601 	hwrm_req_drop(bp, req);
6602 	return rc;
6603 }
6604 
6605 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6606 {
6607 	struct hwrm_ring_grp_alloc_output *resp;
6608 	struct hwrm_ring_grp_alloc_input *req;
6609 	int rc;
6610 	u16 i;
6611 
6612 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6613 		return 0;
6614 
6615 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6616 	if (rc)
6617 		return rc;
6618 
6619 	resp = hwrm_req_hold(bp, req);
6620 	for (i = 0; i < bp->rx_nr_rings; i++) {
6621 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
6622 
6623 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
6624 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
6625 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
6626 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
6627 
6628 		rc = hwrm_req_send(bp, req);
6629 
6630 		if (rc)
6631 			break;
6632 
6633 		bp->grp_info[grp_idx].fw_grp_id =
6634 			le32_to_cpu(resp->ring_group_id);
6635 	}
6636 	hwrm_req_drop(bp, req);
6637 	return rc;
6638 }
6639 
6640 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
6641 {
6642 	struct hwrm_ring_grp_free_input *req;
6643 	u16 i;
6644 
6645 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
6646 		return;
6647 
6648 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
6649 		return;
6650 
6651 	hwrm_req_hold(bp, req);
6652 	for (i = 0; i < bp->cp_nr_rings; i++) {
6653 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
6654 			continue;
6655 		req->ring_group_id =
6656 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
6657 
6658 		hwrm_req_send(bp, req);
6659 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
6660 	}
6661 	hwrm_req_drop(bp, req);
6662 }
6663 
6664 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
6665 				    struct bnxt_ring_struct *ring,
6666 				    u32 ring_type, u32 map_index)
6667 {
6668 	struct hwrm_ring_alloc_output *resp;
6669 	struct hwrm_ring_alloc_input *req;
6670 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
6671 	struct bnxt_ring_grp_info *grp_info;
6672 	int rc, err = 0;
6673 	u16 ring_id;
6674 
6675 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
6676 	if (rc)
6677 		goto exit;
6678 
6679 	req->enables = 0;
6680 	if (rmem->nr_pages > 1) {
6681 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
6682 		/* Page size is in log2 units */
6683 		req->page_size = BNXT_PAGE_SHIFT;
6684 		req->page_tbl_depth = 1;
6685 	} else {
6686 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
6687 	}
6688 	req->fbo = 0;
6689 	/* Association of ring index with doorbell index and MSIX number */
6690 	req->logical_id = cpu_to_le16(map_index);
6691 
6692 	switch (ring_type) {
6693 	case HWRM_RING_ALLOC_TX: {
6694 		struct bnxt_tx_ring_info *txr;
6695 
6696 		txr = container_of(ring, struct bnxt_tx_ring_info,
6697 				   tx_ring_struct);
6698 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
6699 		/* Association of transmit ring with completion ring */
6700 		grp_info = &bp->grp_info[ring->grp_idx];
6701 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
6702 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
6703 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6704 		req->queue_id = cpu_to_le16(ring->queue_id);
6705 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
6706 			req->cmpl_coal_cnt =
6707 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
6708 		break;
6709 	}
6710 	case HWRM_RING_ALLOC_RX:
6711 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6712 		req->length = cpu_to_le32(bp->rx_ring_mask + 1);
6713 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6714 			u16 flags = 0;
6715 
6716 			/* Association of rx ring with stats context */
6717 			grp_info = &bp->grp_info[ring->grp_idx];
6718 			req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
6719 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6720 			req->enables |= cpu_to_le32(
6721 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6722 			if (NET_IP_ALIGN == 2)
6723 				flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
6724 			req->flags = cpu_to_le16(flags);
6725 		}
6726 		break;
6727 	case HWRM_RING_ALLOC_AGG:
6728 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6729 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
6730 			/* Association of agg ring with rx ring */
6731 			grp_info = &bp->grp_info[ring->grp_idx];
6732 			req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
6733 			req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
6734 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6735 			req->enables |= cpu_to_le32(
6736 				RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
6737 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6738 		} else {
6739 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6740 		}
6741 		req->length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
6742 		break;
6743 	case HWRM_RING_ALLOC_CMPL:
6744 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
6745 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
6746 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6747 			/* Association of cp ring with nq */
6748 			grp_info = &bp->grp_info[map_index];
6749 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
6750 			req->cq_handle = cpu_to_le64(ring->handle);
6751 			req->enables |= cpu_to_le32(
6752 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
6753 		} else if (bp->flags & BNXT_FLAG_USING_MSIX) {
6754 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
6755 		}
6756 		break;
6757 	case HWRM_RING_ALLOC_NQ:
6758 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
6759 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
6760 		if (bp->flags & BNXT_FLAG_USING_MSIX)
6761 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
6762 		break;
6763 	default:
6764 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
6765 			   ring_type);
6766 		return -1;
6767 	}
6768 
6769 	resp = hwrm_req_hold(bp, req);
6770 	rc = hwrm_req_send(bp, req);
6771 	err = le16_to_cpu(resp->error_code);
6772 	ring_id = le16_to_cpu(resp->ring_id);
6773 	hwrm_req_drop(bp, req);
6774 
6775 exit:
6776 	if (rc || err) {
6777 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
6778 			   ring_type, rc, err);
6779 		return -EIO;
6780 	}
6781 	ring->fw_ring_id = ring_id;
6782 	return rc;
6783 }
6784 
6785 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
6786 {
6787 	int rc;
6788 
6789 	if (BNXT_PF(bp)) {
6790 		struct hwrm_func_cfg_input *req;
6791 
6792 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
6793 		if (rc)
6794 			return rc;
6795 
6796 		req->fid = cpu_to_le16(0xffff);
6797 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
6798 		req->async_event_cr = cpu_to_le16(idx);
6799 		return hwrm_req_send(bp, req);
6800 	} else {
6801 		struct hwrm_func_vf_cfg_input *req;
6802 
6803 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
6804 		if (rc)
6805 			return rc;
6806 
6807 		req->enables =
6808 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
6809 		req->async_event_cr = cpu_to_le16(idx);
6810 		return hwrm_req_send(bp, req);
6811 	}
6812 }
6813 
6814 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
6815 			     u32 ring_type)
6816 {
6817 	switch (ring_type) {
6818 	case HWRM_RING_ALLOC_TX:
6819 		db->db_ring_mask = bp->tx_ring_mask;
6820 		break;
6821 	case HWRM_RING_ALLOC_RX:
6822 		db->db_ring_mask = bp->rx_ring_mask;
6823 		break;
6824 	case HWRM_RING_ALLOC_AGG:
6825 		db->db_ring_mask = bp->rx_agg_ring_mask;
6826 		break;
6827 	case HWRM_RING_ALLOC_CMPL:
6828 	case HWRM_RING_ALLOC_NQ:
6829 		db->db_ring_mask = bp->cp_ring_mask;
6830 		break;
6831 	}
6832 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
6833 		db->db_epoch_mask = db->db_ring_mask + 1;
6834 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
6835 	}
6836 }
6837 
6838 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
6839 			u32 map_idx, u32 xid)
6840 {
6841 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6842 		switch (ring_type) {
6843 		case HWRM_RING_ALLOC_TX:
6844 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
6845 			break;
6846 		case HWRM_RING_ALLOC_RX:
6847 		case HWRM_RING_ALLOC_AGG:
6848 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
6849 			break;
6850 		case HWRM_RING_ALLOC_CMPL:
6851 			db->db_key64 = DBR_PATH_L2;
6852 			break;
6853 		case HWRM_RING_ALLOC_NQ:
6854 			db->db_key64 = DBR_PATH_L2;
6855 			break;
6856 		}
6857 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
6858 
6859 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6860 			db->db_key64 |= DBR_VALID;
6861 
6862 		db->doorbell = bp->bar1 + bp->db_offset;
6863 	} else {
6864 		db->doorbell = bp->bar1 + map_idx * 0x80;
6865 		switch (ring_type) {
6866 		case HWRM_RING_ALLOC_TX:
6867 			db->db_key32 = DB_KEY_TX;
6868 			break;
6869 		case HWRM_RING_ALLOC_RX:
6870 		case HWRM_RING_ALLOC_AGG:
6871 			db->db_key32 = DB_KEY_RX;
6872 			break;
6873 		case HWRM_RING_ALLOC_CMPL:
6874 			db->db_key32 = DB_KEY_CP;
6875 			break;
6876 		}
6877 	}
6878 	bnxt_set_db_mask(bp, db, ring_type);
6879 }
6880 
6881 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
6882 {
6883 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
6884 	int i, rc = 0;
6885 	u32 type;
6886 
6887 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6888 		type = HWRM_RING_ALLOC_NQ;
6889 	else
6890 		type = HWRM_RING_ALLOC_CMPL;
6891 	for (i = 0; i < bp->cp_nr_rings; i++) {
6892 		struct bnxt_napi *bnapi = bp->bnapi[i];
6893 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6894 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
6895 		u32 map_idx = ring->map_idx;
6896 		unsigned int vector;
6897 
6898 		vector = bp->irq_tbl[map_idx].vector;
6899 		disable_irq_nosync(vector);
6900 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6901 		if (rc) {
6902 			enable_irq(vector);
6903 			goto err_out;
6904 		}
6905 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
6906 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
6907 		enable_irq(vector);
6908 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
6909 
6910 		if (!i) {
6911 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
6912 			if (rc)
6913 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
6914 		}
6915 	}
6916 
6917 	type = HWRM_RING_ALLOC_TX;
6918 	for (i = 0; i < bp->tx_nr_rings; i++) {
6919 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
6920 		struct bnxt_ring_struct *ring;
6921 		u32 map_idx;
6922 
6923 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6924 			struct bnxt_cp_ring_info *cpr2 = txr->tx_cpr;
6925 			struct bnxt_napi *bnapi = txr->bnapi;
6926 			u32 type2 = HWRM_RING_ALLOC_CMPL;
6927 
6928 			ring = &cpr2->cp_ring_struct;
6929 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
6930 			map_idx = bnapi->index;
6931 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
6932 			if (rc)
6933 				goto err_out;
6934 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
6935 				    ring->fw_ring_id);
6936 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
6937 		}
6938 		ring = &txr->tx_ring_struct;
6939 		map_idx = i;
6940 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6941 		if (rc)
6942 			goto err_out;
6943 		bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
6944 	}
6945 
6946 	type = HWRM_RING_ALLOC_RX;
6947 	for (i = 0; i < bp->rx_nr_rings; i++) {
6948 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
6949 		struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
6950 		struct bnxt_napi *bnapi = rxr->bnapi;
6951 		u32 map_idx = bnapi->index;
6952 
6953 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6954 		if (rc)
6955 			goto err_out;
6956 		bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
6957 		/* If we have agg rings, post agg buffers first. */
6958 		if (!agg_rings)
6959 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
6960 		bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
6961 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6962 			struct bnxt_cp_ring_info *cpr2 = rxr->rx_cpr;
6963 			u32 type2 = HWRM_RING_ALLOC_CMPL;
6964 
6965 			ring = &cpr2->cp_ring_struct;
6966 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
6967 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
6968 			if (rc)
6969 				goto err_out;
6970 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
6971 				    ring->fw_ring_id);
6972 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
6973 		}
6974 	}
6975 
6976 	if (agg_rings) {
6977 		type = HWRM_RING_ALLOC_AGG;
6978 		for (i = 0; i < bp->rx_nr_rings; i++) {
6979 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
6980 			struct bnxt_ring_struct *ring =
6981 						&rxr->rx_agg_ring_struct;
6982 			u32 grp_idx = ring->grp_idx;
6983 			u32 map_idx = grp_idx + bp->rx_nr_rings;
6984 
6985 			rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6986 			if (rc)
6987 				goto err_out;
6988 
6989 			bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
6990 				    ring->fw_ring_id);
6991 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
6992 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
6993 			bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
6994 		}
6995 	}
6996 err_out:
6997 	return rc;
6998 }
6999 
7000 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7001 				   struct bnxt_ring_struct *ring,
7002 				   u32 ring_type, int cmpl_ring_id)
7003 {
7004 	struct hwrm_ring_free_output *resp;
7005 	struct hwrm_ring_free_input *req;
7006 	u16 error_code = 0;
7007 	int rc;
7008 
7009 	if (BNXT_NO_FW_ACCESS(bp))
7010 		return 0;
7011 
7012 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7013 	if (rc)
7014 		goto exit;
7015 
7016 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7017 	req->ring_type = ring_type;
7018 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7019 
7020 	resp = hwrm_req_hold(bp, req);
7021 	rc = hwrm_req_send(bp, req);
7022 	error_code = le16_to_cpu(resp->error_code);
7023 	hwrm_req_drop(bp, req);
7024 exit:
7025 	if (rc || error_code) {
7026 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7027 			   ring_type, rc, error_code);
7028 		return -EIO;
7029 	}
7030 	return 0;
7031 }
7032 
7033 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7034 {
7035 	u32 type;
7036 	int i;
7037 
7038 	if (!bp->bnapi)
7039 		return;
7040 
7041 	for (i = 0; i < bp->tx_nr_rings; i++) {
7042 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7043 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7044 
7045 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7046 			u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
7047 
7048 			hwrm_ring_free_send_msg(bp, ring,
7049 						RING_FREE_REQ_RING_TYPE_TX,
7050 						close_path ? cmpl_ring_id :
7051 						INVALID_HW_RING_ID);
7052 			ring->fw_ring_id = INVALID_HW_RING_ID;
7053 		}
7054 	}
7055 
7056 	for (i = 0; i < bp->rx_nr_rings; i++) {
7057 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7058 		struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7059 		u32 grp_idx = rxr->bnapi->index;
7060 
7061 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7062 			u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7063 
7064 			hwrm_ring_free_send_msg(bp, ring,
7065 						RING_FREE_REQ_RING_TYPE_RX,
7066 						close_path ? cmpl_ring_id :
7067 						INVALID_HW_RING_ID);
7068 			ring->fw_ring_id = INVALID_HW_RING_ID;
7069 			bp->grp_info[grp_idx].rx_fw_ring_id =
7070 				INVALID_HW_RING_ID;
7071 		}
7072 	}
7073 
7074 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7075 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7076 	else
7077 		type = RING_FREE_REQ_RING_TYPE_RX;
7078 	for (i = 0; i < bp->rx_nr_rings; i++) {
7079 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7080 		struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7081 		u32 grp_idx = rxr->bnapi->index;
7082 
7083 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7084 			u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7085 
7086 			hwrm_ring_free_send_msg(bp, ring, type,
7087 						close_path ? cmpl_ring_id :
7088 						INVALID_HW_RING_ID);
7089 			ring->fw_ring_id = INVALID_HW_RING_ID;
7090 			bp->grp_info[grp_idx].agg_fw_ring_id =
7091 				INVALID_HW_RING_ID;
7092 		}
7093 	}
7094 
7095 	/* The completion rings are about to be freed.  After that the
7096 	 * IRQ doorbell will not work anymore.  So we need to disable
7097 	 * IRQ here.
7098 	 */
7099 	bnxt_disable_int_sync(bp);
7100 
7101 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7102 		type = RING_FREE_REQ_RING_TYPE_NQ;
7103 	else
7104 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7105 	for (i = 0; i < bp->cp_nr_rings; i++) {
7106 		struct bnxt_napi *bnapi = bp->bnapi[i];
7107 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7108 		struct bnxt_ring_struct *ring;
7109 		int j;
7110 
7111 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++) {
7112 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
7113 
7114 			ring = &cpr2->cp_ring_struct;
7115 			if (ring->fw_ring_id == INVALID_HW_RING_ID)
7116 				continue;
7117 			hwrm_ring_free_send_msg(bp, ring,
7118 						RING_FREE_REQ_RING_TYPE_L2_CMPL,
7119 						INVALID_HW_RING_ID);
7120 			ring->fw_ring_id = INVALID_HW_RING_ID;
7121 		}
7122 		ring = &cpr->cp_ring_struct;
7123 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7124 			hwrm_ring_free_send_msg(bp, ring, type,
7125 						INVALID_HW_RING_ID);
7126 			ring->fw_ring_id = INVALID_HW_RING_ID;
7127 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7128 		}
7129 	}
7130 }
7131 
7132 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7133 			     bool shared);
7134 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7135 			   bool shared);
7136 
7137 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7138 {
7139 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7140 	struct hwrm_func_qcfg_output *resp;
7141 	struct hwrm_func_qcfg_input *req;
7142 	int rc;
7143 
7144 	if (bp->hwrm_spec_code < 0x10601)
7145 		return 0;
7146 
7147 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7148 	if (rc)
7149 		return rc;
7150 
7151 	req->fid = cpu_to_le16(0xffff);
7152 	resp = hwrm_req_hold(bp, req);
7153 	rc = hwrm_req_send(bp, req);
7154 	if (rc) {
7155 		hwrm_req_drop(bp, req);
7156 		return rc;
7157 	}
7158 
7159 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7160 	if (BNXT_NEW_RM(bp)) {
7161 		u16 cp, stats;
7162 
7163 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7164 		hw_resc->resv_hw_ring_grps =
7165 			le32_to_cpu(resp->alloc_hw_ring_grps);
7166 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7167 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7168 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7169 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7170 		hw_resc->resv_irqs = cp;
7171 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7172 			int rx = hw_resc->resv_rx_rings;
7173 			int tx = hw_resc->resv_tx_rings;
7174 
7175 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7176 				rx >>= 1;
7177 			if (cp < (rx + tx)) {
7178 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7179 				if (rc)
7180 					goto get_rings_exit;
7181 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7182 					rx <<= 1;
7183 				hw_resc->resv_rx_rings = rx;
7184 				hw_resc->resv_tx_rings = tx;
7185 			}
7186 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7187 			hw_resc->resv_hw_ring_grps = rx;
7188 		}
7189 		hw_resc->resv_cp_rings = cp;
7190 		hw_resc->resv_stat_ctxs = stats;
7191 	}
7192 get_rings_exit:
7193 	hwrm_req_drop(bp, req);
7194 	return rc;
7195 }
7196 
7197 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7198 {
7199 	struct hwrm_func_qcfg_output *resp;
7200 	struct hwrm_func_qcfg_input *req;
7201 	int rc;
7202 
7203 	if (bp->hwrm_spec_code < 0x10601)
7204 		return 0;
7205 
7206 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7207 	if (rc)
7208 		return rc;
7209 
7210 	req->fid = cpu_to_le16(fid);
7211 	resp = hwrm_req_hold(bp, req);
7212 	rc = hwrm_req_send(bp, req);
7213 	if (!rc)
7214 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7215 
7216 	hwrm_req_drop(bp, req);
7217 	return rc;
7218 }
7219 
7220 static bool bnxt_rfs_supported(struct bnxt *bp);
7221 
7222 static struct hwrm_func_cfg_input *
7223 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7224 {
7225 	struct hwrm_func_cfg_input *req;
7226 	u32 enables = 0;
7227 
7228 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7229 		return NULL;
7230 
7231 	req->fid = cpu_to_le16(0xffff);
7232 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7233 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7234 	if (BNXT_NEW_RM(bp)) {
7235 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7236 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7237 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7238 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7239 			enables |= hwr->cp_p5 ?
7240 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7241 		} else {
7242 			enables |= hwr->cp ?
7243 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7244 			enables |= hwr->grp ?
7245 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7246 		}
7247 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7248 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7249 					  0;
7250 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7251 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7252 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7253 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7254 			req->num_msix = cpu_to_le16(hwr->cp);
7255 		} else {
7256 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7257 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7258 		}
7259 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7260 		req->num_vnics = cpu_to_le16(hwr->vnic);
7261 	}
7262 	req->enables = cpu_to_le32(enables);
7263 	return req;
7264 }
7265 
7266 static struct hwrm_func_vf_cfg_input *
7267 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7268 {
7269 	struct hwrm_func_vf_cfg_input *req;
7270 	u32 enables = 0;
7271 
7272 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7273 		return NULL;
7274 
7275 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7276 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7277 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7278 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7279 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7280 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7281 		enables |= hwr->cp_p5 ?
7282 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7283 	} else {
7284 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7285 		enables |= hwr->grp ?
7286 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7287 	}
7288 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7289 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7290 
7291 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7292 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7293 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7294 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7295 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7296 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7297 	} else {
7298 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7299 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7300 	}
7301 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7302 	req->num_vnics = cpu_to_le16(hwr->vnic);
7303 
7304 	req->enables = cpu_to_le32(enables);
7305 	return req;
7306 }
7307 
7308 static int
7309 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7310 {
7311 	struct hwrm_func_cfg_input *req;
7312 	int rc;
7313 
7314 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7315 	if (!req)
7316 		return -ENOMEM;
7317 
7318 	if (!req->enables) {
7319 		hwrm_req_drop(bp, req);
7320 		return 0;
7321 	}
7322 
7323 	rc = hwrm_req_send(bp, req);
7324 	if (rc)
7325 		return rc;
7326 
7327 	if (bp->hwrm_spec_code < 0x10601)
7328 		bp->hw_resc.resv_tx_rings = hwr->tx;
7329 
7330 	return bnxt_hwrm_get_rings(bp);
7331 }
7332 
7333 static int
7334 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7335 {
7336 	struct hwrm_func_vf_cfg_input *req;
7337 	int rc;
7338 
7339 	if (!BNXT_NEW_RM(bp)) {
7340 		bp->hw_resc.resv_tx_rings = hwr->tx;
7341 		return 0;
7342 	}
7343 
7344 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7345 	if (!req)
7346 		return -ENOMEM;
7347 
7348 	rc = hwrm_req_send(bp, req);
7349 	if (rc)
7350 		return rc;
7351 
7352 	return bnxt_hwrm_get_rings(bp);
7353 }
7354 
7355 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7356 {
7357 	if (BNXT_PF(bp))
7358 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7359 	else
7360 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7361 }
7362 
7363 int bnxt_nq_rings_in_use(struct bnxt *bp)
7364 {
7365 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7366 }
7367 
7368 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7369 {
7370 	int cp;
7371 
7372 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7373 		return bnxt_nq_rings_in_use(bp);
7374 
7375 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7376 	return cp;
7377 }
7378 
7379 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7380 {
7381 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7382 }
7383 
7384 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7385 {
7386 	if (!hwr->grp)
7387 		return 0;
7388 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7389 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7390 
7391 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7392 			rss_ctx *= hwr->vnic;
7393 		return rss_ctx;
7394 	}
7395 	if (BNXT_VF(bp))
7396 		return BNXT_VF_MAX_RSS_CTX;
7397 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7398 		return hwr->grp + 1;
7399 	return 1;
7400 }
7401 
7402 /* Check if a default RSS map needs to be setup.  This function is only
7403  * used on older firmware that does not require reserving RX rings.
7404  */
7405 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7406 {
7407 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7408 
7409 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7410 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7411 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7412 		if (!netif_is_rxfh_configured(bp->dev))
7413 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7414 	}
7415 }
7416 
7417 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7418 {
7419 	if (bp->flags & BNXT_FLAG_RFS) {
7420 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7421 			return 2 + bp->num_rss_ctx;
7422 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7423 			return rx_rings + 1;
7424 	}
7425 	return 1;
7426 }
7427 
7428 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7429 {
7430 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7431 	int cp = bnxt_cp_rings_in_use(bp);
7432 	int nq = bnxt_nq_rings_in_use(bp);
7433 	int rx = bp->rx_nr_rings, stat;
7434 	int vnic, grp = rx;
7435 
7436 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7437 	    bp->hwrm_spec_code >= 0x10601)
7438 		return true;
7439 
7440 	/* Old firmware does not need RX ring reservations but we still
7441 	 * need to setup a default RSS map when needed.  With new firmware
7442 	 * we go through RX ring reservations first and then set up the
7443 	 * RSS map for the successfully reserved RX rings when needed.
7444 	 */
7445 	if (!BNXT_NEW_RM(bp)) {
7446 		bnxt_check_rss_tbl_no_rmgr(bp);
7447 		return false;
7448 	}
7449 
7450 	vnic = bnxt_get_total_vnics(bp, rx);
7451 
7452 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7453 		rx <<= 1;
7454 	stat = bnxt_get_func_stat_ctxs(bp);
7455 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7456 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7457 	    (hw_resc->resv_hw_ring_grps != grp &&
7458 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7459 		return true;
7460 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7461 	    hw_resc->resv_irqs != nq)
7462 		return true;
7463 	return false;
7464 }
7465 
7466 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7467 {
7468 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7469 
7470 	hwr->tx = hw_resc->resv_tx_rings;
7471 	if (BNXT_NEW_RM(bp)) {
7472 		hwr->rx = hw_resc->resv_rx_rings;
7473 		hwr->cp = hw_resc->resv_irqs;
7474 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7475 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7476 		hwr->grp = hw_resc->resv_hw_ring_grps;
7477 		hwr->vnic = hw_resc->resv_vnics;
7478 		hwr->stat = hw_resc->resv_stat_ctxs;
7479 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7480 	}
7481 }
7482 
7483 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7484 {
7485 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7486 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7487 }
7488 
7489 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
7490 
7491 static int __bnxt_reserve_rings(struct bnxt *bp)
7492 {
7493 	struct bnxt_hw_rings hwr = {0};
7494 	int cp = bp->cp_nr_rings;
7495 	int rx_rings, rc;
7496 	int ulp_msix = 0;
7497 	bool sh = false;
7498 	int tx_cp;
7499 
7500 	if (!bnxt_need_reserve_rings(bp))
7501 		return 0;
7502 
7503 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
7504 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
7505 		if (!ulp_msix)
7506 			bnxt_set_ulp_stat_ctxs(bp, 0);
7507 
7508 		if (ulp_msix > bp->ulp_num_msix_want)
7509 			ulp_msix = bp->ulp_num_msix_want;
7510 		hwr.cp = cp + ulp_msix;
7511 	} else {
7512 		hwr.cp = bnxt_nq_rings_in_use(bp);
7513 	}
7514 
7515 	hwr.tx = bp->tx_nr_rings;
7516 	hwr.rx = bp->rx_nr_rings;
7517 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7518 		sh = true;
7519 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7520 		hwr.cp_p5 = hwr.rx + hwr.tx;
7521 
7522 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7523 
7524 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7525 		hwr.rx <<= 1;
7526 	hwr.grp = bp->rx_nr_rings;
7527 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7528 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
7529 
7530 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
7531 	if (rc)
7532 		return rc;
7533 
7534 	bnxt_copy_reserved_rings(bp, &hwr);
7535 
7536 	rx_rings = hwr.rx;
7537 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7538 		if (hwr.rx >= 2) {
7539 			rx_rings = hwr.rx >> 1;
7540 		} else {
7541 			if (netif_running(bp->dev))
7542 				return -ENOMEM;
7543 
7544 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7545 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7546 			bp->dev->hw_features &= ~NETIF_F_LRO;
7547 			bp->dev->features &= ~NETIF_F_LRO;
7548 			bnxt_set_ring_params(bp);
7549 		}
7550 	}
7551 	rx_rings = min_t(int, rx_rings, hwr.grp);
7552 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
7553 	if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
7554 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
7555 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
7556 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
7557 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7558 		hwr.rx = rx_rings << 1;
7559 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
7560 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
7561 	bp->tx_nr_rings = hwr.tx;
7562 
7563 	/* If we cannot reserve all the RX rings, reset the RSS map only
7564 	 * if absolutely necessary
7565 	 */
7566 	if (rx_rings != bp->rx_nr_rings) {
7567 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
7568 			    rx_rings, bp->rx_nr_rings);
7569 		if (netif_is_rxfh_configured(bp->dev) &&
7570 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
7571 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
7572 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
7573 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
7574 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
7575 		}
7576 	}
7577 	bp->rx_nr_rings = rx_rings;
7578 	bp->cp_nr_rings = hwr.cp;
7579 
7580 	if (!bnxt_rings_ok(bp, &hwr))
7581 		return -ENOMEM;
7582 
7583 	if (!netif_is_rxfh_configured(bp->dev))
7584 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7585 
7586 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
7587 		int resv_msix, resv_ctx, ulp_ctxs;
7588 		struct bnxt_hw_resc *hw_resc;
7589 
7590 		hw_resc = &bp->hw_resc;
7591 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
7592 		ulp_msix = min_t(int, resv_msix, ulp_msix);
7593 		bnxt_set_ulp_msix_num(bp, ulp_msix);
7594 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
7595 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
7596 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
7597 	}
7598 
7599 	return rc;
7600 }
7601 
7602 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7603 {
7604 	struct hwrm_func_vf_cfg_input *req;
7605 	u32 flags;
7606 
7607 	if (!BNXT_NEW_RM(bp))
7608 		return 0;
7609 
7610 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7611 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
7612 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7613 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7614 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7615 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
7616 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
7617 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7618 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7619 
7620 	req->flags = cpu_to_le32(flags);
7621 	return hwrm_req_send_silent(bp, req);
7622 }
7623 
7624 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7625 {
7626 	struct hwrm_func_cfg_input *req;
7627 	u32 flags;
7628 
7629 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7630 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
7631 	if (BNXT_NEW_RM(bp)) {
7632 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7633 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7634 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7635 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
7636 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7637 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
7638 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
7639 		else
7640 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7641 	}
7642 
7643 	req->flags = cpu_to_le32(flags);
7644 	return hwrm_req_send_silent(bp, req);
7645 }
7646 
7647 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7648 {
7649 	if (bp->hwrm_spec_code < 0x10801)
7650 		return 0;
7651 
7652 	if (BNXT_PF(bp))
7653 		return bnxt_hwrm_check_pf_rings(bp, hwr);
7654 
7655 	return bnxt_hwrm_check_vf_rings(bp, hwr);
7656 }
7657 
7658 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
7659 {
7660 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7661 	struct hwrm_ring_aggint_qcaps_output *resp;
7662 	struct hwrm_ring_aggint_qcaps_input *req;
7663 	int rc;
7664 
7665 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
7666 	coal_cap->num_cmpl_dma_aggr_max = 63;
7667 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
7668 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
7669 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
7670 	coal_cap->int_lat_tmr_min_max = 65535;
7671 	coal_cap->int_lat_tmr_max_max = 65535;
7672 	coal_cap->num_cmpl_aggr_int_max = 65535;
7673 	coal_cap->timer_units = 80;
7674 
7675 	if (bp->hwrm_spec_code < 0x10902)
7676 		return;
7677 
7678 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
7679 		return;
7680 
7681 	resp = hwrm_req_hold(bp, req);
7682 	rc = hwrm_req_send_silent(bp, req);
7683 	if (!rc) {
7684 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
7685 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
7686 		coal_cap->num_cmpl_dma_aggr_max =
7687 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
7688 		coal_cap->num_cmpl_dma_aggr_during_int_max =
7689 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
7690 		coal_cap->cmpl_aggr_dma_tmr_max =
7691 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
7692 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
7693 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
7694 		coal_cap->int_lat_tmr_min_max =
7695 			le16_to_cpu(resp->int_lat_tmr_min_max);
7696 		coal_cap->int_lat_tmr_max_max =
7697 			le16_to_cpu(resp->int_lat_tmr_max_max);
7698 		coal_cap->num_cmpl_aggr_int_max =
7699 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
7700 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
7701 	}
7702 	hwrm_req_drop(bp, req);
7703 }
7704 
7705 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
7706 {
7707 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7708 
7709 	return usec * 1000 / coal_cap->timer_units;
7710 }
7711 
7712 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
7713 	struct bnxt_coal *hw_coal,
7714 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7715 {
7716 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7717 	u16 val, tmr, max, flags = hw_coal->flags;
7718 	u32 cmpl_params = coal_cap->cmpl_params;
7719 
7720 	max = hw_coal->bufs_per_record * 128;
7721 	if (hw_coal->budget)
7722 		max = hw_coal->bufs_per_record * hw_coal->budget;
7723 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
7724 
7725 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
7726 	req->num_cmpl_aggr_int = cpu_to_le16(val);
7727 
7728 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
7729 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
7730 
7731 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
7732 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
7733 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
7734 
7735 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
7736 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
7737 	req->int_lat_tmr_max = cpu_to_le16(tmr);
7738 
7739 	/* min timer set to 1/2 of interrupt timer */
7740 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
7741 		val = tmr / 2;
7742 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
7743 		req->int_lat_tmr_min = cpu_to_le16(val);
7744 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
7745 	}
7746 
7747 	/* buf timer set to 1/4 of interrupt timer */
7748 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
7749 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
7750 
7751 	if (cmpl_params &
7752 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
7753 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
7754 		val = clamp_t(u16, tmr, 1,
7755 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
7756 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
7757 		req->enables |=
7758 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
7759 	}
7760 
7761 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
7762 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
7763 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
7764 	req->flags = cpu_to_le16(flags);
7765 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
7766 }
7767 
7768 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
7769 				   struct bnxt_coal *hw_coal)
7770 {
7771 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
7772 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7773 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7774 	u32 nq_params = coal_cap->nq_params;
7775 	u16 tmr;
7776 	int rc;
7777 
7778 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
7779 		return 0;
7780 
7781 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7782 	if (rc)
7783 		return rc;
7784 
7785 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
7786 	req->flags =
7787 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
7788 
7789 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
7790 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
7791 	req->int_lat_tmr_min = cpu_to_le16(tmr);
7792 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
7793 	return hwrm_req_send(bp, req);
7794 }
7795 
7796 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
7797 {
7798 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
7799 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7800 	struct bnxt_coal coal;
7801 	int rc;
7802 
7803 	/* Tick values in micro seconds.
7804 	 * 1 coal_buf x bufs_per_record = 1 completion record.
7805 	 */
7806 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
7807 
7808 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
7809 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
7810 
7811 	if (!bnapi->rx_ring)
7812 		return -ENODEV;
7813 
7814 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7815 	if (rc)
7816 		return rc;
7817 
7818 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
7819 
7820 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
7821 
7822 	return hwrm_req_send(bp, req_rx);
7823 }
7824 
7825 static int
7826 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
7827 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7828 {
7829 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
7830 
7831 	req->ring_id = cpu_to_le16(ring_id);
7832 	return hwrm_req_send(bp, req);
7833 }
7834 
7835 static int
7836 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
7837 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7838 {
7839 	struct bnxt_tx_ring_info *txr;
7840 	int i, rc;
7841 
7842 	bnxt_for_each_napi_tx(i, bnapi, txr) {
7843 		u16 ring_id;
7844 
7845 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
7846 		req->ring_id = cpu_to_le16(ring_id);
7847 		rc = hwrm_req_send(bp, req);
7848 		if (rc)
7849 			return rc;
7850 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7851 			return 0;
7852 	}
7853 	return 0;
7854 }
7855 
7856 int bnxt_hwrm_set_coal(struct bnxt *bp)
7857 {
7858 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
7859 	int i, rc;
7860 
7861 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7862 	if (rc)
7863 		return rc;
7864 
7865 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7866 	if (rc) {
7867 		hwrm_req_drop(bp, req_rx);
7868 		return rc;
7869 	}
7870 
7871 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
7872 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
7873 
7874 	hwrm_req_hold(bp, req_rx);
7875 	hwrm_req_hold(bp, req_tx);
7876 	for (i = 0; i < bp->cp_nr_rings; i++) {
7877 		struct bnxt_napi *bnapi = bp->bnapi[i];
7878 		struct bnxt_coal *hw_coal;
7879 
7880 		if (!bnapi->rx_ring)
7881 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
7882 		else
7883 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
7884 		if (rc)
7885 			break;
7886 
7887 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7888 			continue;
7889 
7890 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
7891 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
7892 			if (rc)
7893 				break;
7894 		}
7895 		if (bnapi->rx_ring)
7896 			hw_coal = &bp->rx_coal;
7897 		else
7898 			hw_coal = &bp->tx_coal;
7899 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
7900 	}
7901 	hwrm_req_drop(bp, req_rx);
7902 	hwrm_req_drop(bp, req_tx);
7903 	return rc;
7904 }
7905 
7906 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
7907 {
7908 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
7909 	struct hwrm_stat_ctx_free_input *req;
7910 	int i;
7911 
7912 	if (!bp->bnapi)
7913 		return;
7914 
7915 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
7916 		return;
7917 
7918 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
7919 		return;
7920 	if (BNXT_FW_MAJ(bp) <= 20) {
7921 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
7922 			hwrm_req_drop(bp, req);
7923 			return;
7924 		}
7925 		hwrm_req_hold(bp, req0);
7926 	}
7927 	hwrm_req_hold(bp, req);
7928 	for (i = 0; i < bp->cp_nr_rings; i++) {
7929 		struct bnxt_napi *bnapi = bp->bnapi[i];
7930 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7931 
7932 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
7933 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
7934 			if (req0) {
7935 				req0->stat_ctx_id = req->stat_ctx_id;
7936 				hwrm_req_send(bp, req0);
7937 			}
7938 			hwrm_req_send(bp, req);
7939 
7940 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
7941 		}
7942 	}
7943 	hwrm_req_drop(bp, req);
7944 	if (req0)
7945 		hwrm_req_drop(bp, req0);
7946 }
7947 
7948 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
7949 {
7950 	struct hwrm_stat_ctx_alloc_output *resp;
7951 	struct hwrm_stat_ctx_alloc_input *req;
7952 	int rc, i;
7953 
7954 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
7955 		return 0;
7956 
7957 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
7958 	if (rc)
7959 		return rc;
7960 
7961 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
7962 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
7963 
7964 	resp = hwrm_req_hold(bp, req);
7965 	for (i = 0; i < bp->cp_nr_rings; i++) {
7966 		struct bnxt_napi *bnapi = bp->bnapi[i];
7967 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7968 
7969 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
7970 
7971 		rc = hwrm_req_send(bp, req);
7972 		if (rc)
7973 			break;
7974 
7975 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
7976 
7977 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
7978 	}
7979 	hwrm_req_drop(bp, req);
7980 	return rc;
7981 }
7982 
7983 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
7984 {
7985 	struct hwrm_func_qcfg_output *resp;
7986 	struct hwrm_func_qcfg_input *req;
7987 	u16 flags;
7988 	int rc;
7989 
7990 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7991 	if (rc)
7992 		return rc;
7993 
7994 	req->fid = cpu_to_le16(0xffff);
7995 	resp = hwrm_req_hold(bp, req);
7996 	rc = hwrm_req_send(bp, req);
7997 	if (rc)
7998 		goto func_qcfg_exit;
7999 
8000 #ifdef CONFIG_BNXT_SRIOV
8001 	if (BNXT_VF(bp)) {
8002 		struct bnxt_vf_info *vf = &bp->vf;
8003 
8004 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8005 	} else {
8006 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8007 	}
8008 #endif
8009 	flags = le16_to_cpu(resp->flags);
8010 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8011 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8012 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8013 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8014 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8015 	}
8016 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8017 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8018 
8019 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8020 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8021 
8022 	switch (resp->port_partition_type) {
8023 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8024 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8025 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8026 		bp->port_partition_type = resp->port_partition_type;
8027 		break;
8028 	}
8029 	if (bp->hwrm_spec_code < 0x10707 ||
8030 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8031 		bp->br_mode = BRIDGE_MODE_VEB;
8032 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8033 		bp->br_mode = BRIDGE_MODE_VEPA;
8034 	else
8035 		bp->br_mode = BRIDGE_MODE_UNDEF;
8036 
8037 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8038 	if (!bp->max_mtu)
8039 		bp->max_mtu = BNXT_MAX_MTU;
8040 
8041 	if (bp->db_size)
8042 		goto func_qcfg_exit;
8043 
8044 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8045 	if (BNXT_CHIP_P5(bp)) {
8046 		if (BNXT_PF(bp))
8047 			bp->db_offset = DB_PF_OFFSET_P5;
8048 		else
8049 			bp->db_offset = DB_VF_OFFSET_P5;
8050 	}
8051 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8052 				 1024);
8053 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8054 	    bp->db_size <= bp->db_offset)
8055 		bp->db_size = pci_resource_len(bp->pdev, 2);
8056 
8057 func_qcfg_exit:
8058 	hwrm_req_drop(bp, req);
8059 	return rc;
8060 }
8061 
8062 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8063 				      u8 init_val, u8 init_offset,
8064 				      bool init_mask_set)
8065 {
8066 	ctxm->init_value = init_val;
8067 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8068 	if (init_mask_set)
8069 		ctxm->init_offset = init_offset * 4;
8070 	else
8071 		ctxm->init_value = 0;
8072 }
8073 
8074 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8075 {
8076 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8077 	u16 type;
8078 
8079 	for (type = 0; type < ctx_max; type++) {
8080 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8081 		int n = 1;
8082 
8083 		if (!ctxm->max_entries)
8084 			continue;
8085 
8086 		if (ctxm->instance_bmap)
8087 			n = hweight32(ctxm->instance_bmap);
8088 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8089 		if (!ctxm->pg_info)
8090 			return -ENOMEM;
8091 	}
8092 	return 0;
8093 }
8094 
8095 #define BNXT_CTX_INIT_VALID(flags)	\
8096 	(!!((flags) &			\
8097 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8098 
8099 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8100 {
8101 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8102 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8103 	struct bnxt_ctx_mem_info *ctx;
8104 	u16 type;
8105 	int rc;
8106 
8107 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8108 	if (rc)
8109 		return rc;
8110 
8111 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8112 	if (!ctx)
8113 		return -ENOMEM;
8114 	bp->ctx = ctx;
8115 
8116 	resp = hwrm_req_hold(bp, req);
8117 
8118 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8119 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8120 		u8 init_val, init_off, i;
8121 		__le32 *p;
8122 		u32 flags;
8123 
8124 		req->type = cpu_to_le16(type);
8125 		rc = hwrm_req_send(bp, req);
8126 		if (rc)
8127 			goto ctx_done;
8128 		flags = le32_to_cpu(resp->flags);
8129 		type = le16_to_cpu(resp->next_valid_type);
8130 		if (!(flags & FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_TYPE_VALID))
8131 			continue;
8132 
8133 		ctxm->type = le16_to_cpu(resp->type);
8134 		ctxm->entry_size = le16_to_cpu(resp->entry_size);
8135 		ctxm->flags = flags;
8136 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8137 		ctxm->entry_multiple = resp->entry_multiple;
8138 		ctxm->max_entries = le32_to_cpu(resp->max_num_entries);
8139 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8140 		init_val = resp->ctx_init_value;
8141 		init_off = resp->ctx_init_offset;
8142 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8143 					  BNXT_CTX_INIT_VALID(flags));
8144 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8145 					      BNXT_MAX_SPLIT_ENTRY);
8146 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8147 		     i++, p++)
8148 			ctxm->split[i] = le32_to_cpu(*p);
8149 	}
8150 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8151 
8152 ctx_done:
8153 	hwrm_req_drop(bp, req);
8154 	return rc;
8155 }
8156 
8157 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8158 {
8159 	struct hwrm_func_backing_store_qcaps_output *resp;
8160 	struct hwrm_func_backing_store_qcaps_input *req;
8161 	int rc;
8162 
8163 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx)
8164 		return 0;
8165 
8166 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8167 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8168 
8169 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8170 	if (rc)
8171 		return rc;
8172 
8173 	resp = hwrm_req_hold(bp, req);
8174 	rc = hwrm_req_send_silent(bp, req);
8175 	if (!rc) {
8176 		struct bnxt_ctx_mem_type *ctxm;
8177 		struct bnxt_ctx_mem_info *ctx;
8178 		u8 init_val, init_idx = 0;
8179 		u16 init_mask;
8180 
8181 		ctx = bp->ctx;
8182 		if (!ctx) {
8183 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8184 			if (!ctx) {
8185 				rc = -ENOMEM;
8186 				goto ctx_err;
8187 			}
8188 			bp->ctx = ctx;
8189 		}
8190 		init_val = resp->ctx_kind_initializer;
8191 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8192 
8193 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8194 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8195 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8196 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8197 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8198 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8199 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8200 					  (init_mask & (1 << init_idx++)) != 0);
8201 
8202 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8203 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8204 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8205 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8206 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8207 					  (init_mask & (1 << init_idx++)) != 0);
8208 
8209 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8210 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8211 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8212 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8213 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8214 					  (init_mask & (1 << init_idx++)) != 0);
8215 
8216 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8217 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8218 		ctxm->max_entries = ctxm->vnic_entries +
8219 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8220 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8221 		bnxt_init_ctx_initializer(ctxm, init_val,
8222 					  resp->vnic_init_offset,
8223 					  (init_mask & (1 << init_idx++)) != 0);
8224 
8225 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8226 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8227 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8228 		bnxt_init_ctx_initializer(ctxm, init_val,
8229 					  resp->stat_init_offset,
8230 					  (init_mask & (1 << init_idx++)) != 0);
8231 
8232 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8233 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8234 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8235 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8236 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8237 		if (!ctxm->entry_multiple)
8238 			ctxm->entry_multiple = 1;
8239 
8240 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8241 
8242 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8243 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8244 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8245 		ctxm->mrav_num_entries_units =
8246 			le16_to_cpu(resp->mrav_num_entries_units);
8247 		bnxt_init_ctx_initializer(ctxm, init_val,
8248 					  resp->mrav_init_offset,
8249 					  (init_mask & (1 << init_idx++)) != 0);
8250 
8251 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8252 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8253 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8254 
8255 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8256 		if (!ctx->tqm_fp_rings_count)
8257 			ctx->tqm_fp_rings_count = bp->max_q;
8258 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8259 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8260 
8261 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8262 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8263 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8264 
8265 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8266 	} else {
8267 		rc = 0;
8268 	}
8269 ctx_err:
8270 	hwrm_req_drop(bp, req);
8271 	return rc;
8272 }
8273 
8274 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8275 				  __le64 *pg_dir)
8276 {
8277 	if (!rmem->nr_pages)
8278 		return;
8279 
8280 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8281 	if (rmem->depth >= 1) {
8282 		if (rmem->depth == 2)
8283 			*pg_attr |= 2;
8284 		else
8285 			*pg_attr |= 1;
8286 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8287 	} else {
8288 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8289 	}
8290 }
8291 
8292 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8293 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8294 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8295 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8296 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8297 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8298 
8299 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8300 {
8301 	struct hwrm_func_backing_store_cfg_input *req;
8302 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8303 	struct bnxt_ctx_pg_info *ctx_pg;
8304 	struct bnxt_ctx_mem_type *ctxm;
8305 	void **__req = (void **)&req;
8306 	u32 req_len = sizeof(*req);
8307 	__le32 *num_entries;
8308 	__le64 *pg_dir;
8309 	u32 flags = 0;
8310 	u8 *pg_attr;
8311 	u32 ena;
8312 	int rc;
8313 	int i;
8314 
8315 	if (!ctx)
8316 		return 0;
8317 
8318 	if (req_len > bp->hwrm_max_ext_req_len)
8319 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8320 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8321 	if (rc)
8322 		return rc;
8323 
8324 	req->enables = cpu_to_le32(enables);
8325 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8326 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8327 		ctx_pg = ctxm->pg_info;
8328 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8329 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8330 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8331 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8332 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8333 				      &req->qpc_pg_size_qpc_lvl,
8334 				      &req->qpc_page_dir);
8335 
8336 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8337 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8338 	}
8339 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8340 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8341 		ctx_pg = ctxm->pg_info;
8342 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8343 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8344 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8345 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8346 				      &req->srq_pg_size_srq_lvl,
8347 				      &req->srq_page_dir);
8348 	}
8349 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8350 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8351 		ctx_pg = ctxm->pg_info;
8352 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8353 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8354 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8355 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8356 				      &req->cq_pg_size_cq_lvl,
8357 				      &req->cq_page_dir);
8358 	}
8359 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8360 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8361 		ctx_pg = ctxm->pg_info;
8362 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8363 		req->vnic_num_ring_table_entries =
8364 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8365 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8366 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8367 				      &req->vnic_pg_size_vnic_lvl,
8368 				      &req->vnic_page_dir);
8369 	}
8370 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8371 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8372 		ctx_pg = ctxm->pg_info;
8373 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8374 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8375 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8376 				      &req->stat_pg_size_stat_lvl,
8377 				      &req->stat_page_dir);
8378 	}
8379 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8380 		u32 units;
8381 
8382 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8383 		ctx_pg = ctxm->pg_info;
8384 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8385 		units = ctxm->mrav_num_entries_units;
8386 		if (units) {
8387 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8388 			u32 entries;
8389 
8390 			num_mr = ctx_pg->entries - num_ah;
8391 			entries = ((num_mr / units) << 16) | (num_ah / units);
8392 			req->mrav_num_entries = cpu_to_le32(entries);
8393 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8394 		}
8395 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8396 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8397 				      &req->mrav_pg_size_mrav_lvl,
8398 				      &req->mrav_page_dir);
8399 	}
8400 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8401 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8402 		ctx_pg = ctxm->pg_info;
8403 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8404 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8405 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8406 				      &req->tim_pg_size_tim_lvl,
8407 				      &req->tim_page_dir);
8408 	}
8409 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8410 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8411 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8412 	     pg_dir = &req->tqm_sp_page_dir,
8413 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8414 	     ctx_pg = ctxm->pg_info;
8415 	     i < BNXT_MAX_TQM_RINGS;
8416 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8417 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8418 		if (!(enables & ena))
8419 			continue;
8420 
8421 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8422 		*num_entries = cpu_to_le32(ctx_pg->entries);
8423 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8424 	}
8425 	req->flags = cpu_to_le32(flags);
8426 	return hwrm_req_send(bp, req);
8427 }
8428 
8429 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8430 				  struct bnxt_ctx_pg_info *ctx_pg)
8431 {
8432 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8433 
8434 	rmem->page_size = BNXT_PAGE_SIZE;
8435 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8436 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8437 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8438 	if (rmem->depth >= 1)
8439 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8440 	return bnxt_alloc_ring(bp, rmem);
8441 }
8442 
8443 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8444 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8445 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8446 {
8447 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8448 	int rc;
8449 
8450 	if (!mem_size)
8451 		return -EINVAL;
8452 
8453 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8454 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8455 		ctx_pg->nr_pages = 0;
8456 		return -EINVAL;
8457 	}
8458 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8459 		int nr_tbls, i;
8460 
8461 		rmem->depth = 2;
8462 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8463 					     GFP_KERNEL);
8464 		if (!ctx_pg->ctx_pg_tbl)
8465 			return -ENOMEM;
8466 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8467 		rmem->nr_pages = nr_tbls;
8468 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8469 		if (rc)
8470 			return rc;
8471 		for (i = 0; i < nr_tbls; i++) {
8472 			struct bnxt_ctx_pg_info *pg_tbl;
8473 
8474 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8475 			if (!pg_tbl)
8476 				return -ENOMEM;
8477 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8478 			rmem = &pg_tbl->ring_mem;
8479 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8480 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8481 			rmem->depth = 1;
8482 			rmem->nr_pages = MAX_CTX_PAGES;
8483 			rmem->ctx_mem = ctxm;
8484 			if (i == (nr_tbls - 1)) {
8485 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8486 
8487 				if (rem)
8488 					rmem->nr_pages = rem;
8489 			}
8490 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8491 			if (rc)
8492 				break;
8493 		}
8494 	} else {
8495 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8496 		if (rmem->nr_pages > 1 || depth)
8497 			rmem->depth = 1;
8498 		rmem->ctx_mem = ctxm;
8499 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8500 	}
8501 	return rc;
8502 }
8503 
8504 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
8505 				  struct bnxt_ctx_pg_info *ctx_pg)
8506 {
8507 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8508 
8509 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
8510 	    ctx_pg->ctx_pg_tbl) {
8511 		int i, nr_tbls = rmem->nr_pages;
8512 
8513 		for (i = 0; i < nr_tbls; i++) {
8514 			struct bnxt_ctx_pg_info *pg_tbl;
8515 			struct bnxt_ring_mem_info *rmem2;
8516 
8517 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8518 			if (!pg_tbl)
8519 				continue;
8520 			rmem2 = &pg_tbl->ring_mem;
8521 			bnxt_free_ring(bp, rmem2);
8522 			ctx_pg->ctx_pg_arr[i] = NULL;
8523 			kfree(pg_tbl);
8524 			ctx_pg->ctx_pg_tbl[i] = NULL;
8525 		}
8526 		kfree(ctx_pg->ctx_pg_tbl);
8527 		ctx_pg->ctx_pg_tbl = NULL;
8528 	}
8529 	bnxt_free_ring(bp, rmem);
8530 	ctx_pg->nr_pages = 0;
8531 }
8532 
8533 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
8534 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
8535 				   u8 pg_lvl)
8536 {
8537 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8538 	int i, rc = 0, n = 1;
8539 	u32 mem_size;
8540 
8541 	if (!ctxm->entry_size || !ctx_pg)
8542 		return -EINVAL;
8543 	if (ctxm->instance_bmap)
8544 		n = hweight32(ctxm->instance_bmap);
8545 	if (ctxm->entry_multiple)
8546 		entries = roundup(entries, ctxm->entry_multiple);
8547 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
8548 	mem_size = entries * ctxm->entry_size;
8549 	for (i = 0; i < n && !rc; i++) {
8550 		ctx_pg[i].entries = entries;
8551 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
8552 					    ctxm->init_value ? ctxm : NULL);
8553 	}
8554 	return rc;
8555 }
8556 
8557 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
8558 					       struct bnxt_ctx_mem_type *ctxm,
8559 					       bool last)
8560 {
8561 	struct hwrm_func_backing_store_cfg_v2_input *req;
8562 	u32 instance_bmap = ctxm->instance_bmap;
8563 	int i, j, rc = 0, n = 1;
8564 	__le32 *p;
8565 
8566 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
8567 		return 0;
8568 
8569 	if (instance_bmap)
8570 		n = hweight32(ctxm->instance_bmap);
8571 	else
8572 		instance_bmap = 1;
8573 
8574 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
8575 	if (rc)
8576 		return rc;
8577 	hwrm_req_hold(bp, req);
8578 	req->type = cpu_to_le16(ctxm->type);
8579 	req->entry_size = cpu_to_le16(ctxm->entry_size);
8580 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
8581 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
8582 		p[i] = cpu_to_le32(ctxm->split[i]);
8583 	for (i = 0, j = 0; j < n && !rc; i++) {
8584 		struct bnxt_ctx_pg_info *ctx_pg;
8585 
8586 		if (!(instance_bmap & (1 << i)))
8587 			continue;
8588 		req->instance = cpu_to_le16(i);
8589 		ctx_pg = &ctxm->pg_info[j++];
8590 		if (!ctx_pg->entries)
8591 			continue;
8592 		req->num_entries = cpu_to_le32(ctx_pg->entries);
8593 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8594 				      &req->page_size_pbl_level,
8595 				      &req->page_dir);
8596 		if (last && j == n)
8597 			req->flags =
8598 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
8599 		rc = hwrm_req_send(bp, req);
8600 	}
8601 	hwrm_req_drop(bp, req);
8602 	return rc;
8603 }
8604 
8605 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
8606 {
8607 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8608 	struct bnxt_ctx_mem_type *ctxm;
8609 	u16 last_type;
8610 	int rc = 0;
8611 	u16 type;
8612 
8613 	if (!ena)
8614 		return 0;
8615 	else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
8616 		last_type = BNXT_CTX_MAX - 1;
8617 	else
8618 		last_type = BNXT_CTX_L2_MAX - 1;
8619 	ctx->ctx_arr[last_type].last = 1;
8620 
8621 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
8622 		ctxm = &ctx->ctx_arr[type];
8623 
8624 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
8625 		if (rc)
8626 			return rc;
8627 	}
8628 	return 0;
8629 }
8630 
8631 void bnxt_free_ctx_mem(struct bnxt *bp)
8632 {
8633 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8634 	u16 type;
8635 
8636 	if (!ctx)
8637 		return;
8638 
8639 	for (type = 0; type < BNXT_CTX_V2_MAX; type++) {
8640 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8641 		struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8642 		int i, n = 1;
8643 
8644 		if (!ctx_pg)
8645 			continue;
8646 		if (ctxm->instance_bmap)
8647 			n = hweight32(ctxm->instance_bmap);
8648 		for (i = 0; i < n; i++)
8649 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
8650 
8651 		kfree(ctx_pg);
8652 		ctxm->pg_info = NULL;
8653 	}
8654 
8655 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
8656 	kfree(ctx);
8657 	bp->ctx = NULL;
8658 }
8659 
8660 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
8661 {
8662 	struct bnxt_ctx_mem_type *ctxm;
8663 	struct bnxt_ctx_mem_info *ctx;
8664 	u32 l2_qps, qp1_qps, max_qps;
8665 	u32 ena, entries_sp, entries;
8666 	u32 srqs, max_srqs, min;
8667 	u32 num_mr, num_ah;
8668 	u32 extra_srqs = 0;
8669 	u32 extra_qps = 0;
8670 	u32 fast_qpmd_qps;
8671 	u8 pg_lvl = 1;
8672 	int i, rc;
8673 
8674 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
8675 	if (rc) {
8676 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
8677 			   rc);
8678 		return rc;
8679 	}
8680 	ctx = bp->ctx;
8681 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
8682 		return 0;
8683 
8684 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8685 	l2_qps = ctxm->qp_l2_entries;
8686 	qp1_qps = ctxm->qp_qp1_entries;
8687 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
8688 	max_qps = ctxm->max_entries;
8689 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8690 	srqs = ctxm->srq_l2_entries;
8691 	max_srqs = ctxm->max_entries;
8692 	ena = 0;
8693 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
8694 		pg_lvl = 2;
8695 		extra_qps = min_t(u32, 65536, max_qps - l2_qps - qp1_qps);
8696 		/* allocate extra qps if fw supports RoCE fast qp destroy feature */
8697 		extra_qps += fast_qpmd_qps;
8698 		extra_srqs = min_t(u32, 8192, max_srqs - srqs);
8699 		if (fast_qpmd_qps)
8700 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
8701 	}
8702 
8703 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8704 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
8705 				     pg_lvl);
8706 	if (rc)
8707 		return rc;
8708 
8709 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8710 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
8711 	if (rc)
8712 		return rc;
8713 
8714 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8715 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
8716 				     extra_qps * 2, pg_lvl);
8717 	if (rc)
8718 		return rc;
8719 
8720 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8721 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
8722 	if (rc)
8723 		return rc;
8724 
8725 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8726 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
8727 	if (rc)
8728 		return rc;
8729 
8730 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
8731 		goto skip_rdma;
8732 
8733 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8734 	/* 128K extra is needed to accommodate static AH context
8735 	 * allocation by f/w.
8736 	 */
8737 	num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
8738 	num_ah = min_t(u32, num_mr, 1024 * 128);
8739 	ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
8740 	if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
8741 		ctxm->mrav_av_entries = num_ah;
8742 
8743 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
8744 	if (rc)
8745 		return rc;
8746 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
8747 
8748 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8749 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
8750 	if (rc)
8751 		return rc;
8752 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
8753 
8754 skip_rdma:
8755 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8756 	min = ctxm->min_entries;
8757 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
8758 		     2 * (extra_qps + qp1_qps) + min;
8759 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
8760 	if (rc)
8761 		return rc;
8762 
8763 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8764 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
8765 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
8766 	if (rc)
8767 		return rc;
8768 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
8769 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
8770 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
8771 
8772 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8773 		rc = bnxt_backing_store_cfg_v2(bp, ena);
8774 	else
8775 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
8776 	if (rc) {
8777 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
8778 			   rc);
8779 		return rc;
8780 	}
8781 	ctx->flags |= BNXT_CTX_FLAG_INITED;
8782 	return 0;
8783 }
8784 
8785 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
8786 {
8787 	struct hwrm_func_resource_qcaps_output *resp;
8788 	struct hwrm_func_resource_qcaps_input *req;
8789 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8790 	int rc;
8791 
8792 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
8793 	if (rc)
8794 		return rc;
8795 
8796 	req->fid = cpu_to_le16(0xffff);
8797 	resp = hwrm_req_hold(bp, req);
8798 	rc = hwrm_req_send_silent(bp, req);
8799 	if (rc)
8800 		goto hwrm_func_resc_qcaps_exit;
8801 
8802 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
8803 	if (!all)
8804 		goto hwrm_func_resc_qcaps_exit;
8805 
8806 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
8807 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
8808 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
8809 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
8810 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
8811 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
8812 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
8813 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
8814 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
8815 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
8816 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
8817 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
8818 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
8819 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
8820 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
8821 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
8822 
8823 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8824 		u16 max_msix = le16_to_cpu(resp->max_msix);
8825 
8826 		hw_resc->max_nqs = max_msix;
8827 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
8828 	}
8829 
8830 	if (BNXT_PF(bp)) {
8831 		struct bnxt_pf_info *pf = &bp->pf;
8832 
8833 		pf->vf_resv_strategy =
8834 			le16_to_cpu(resp->vf_reservation_strategy);
8835 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
8836 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
8837 	}
8838 hwrm_func_resc_qcaps_exit:
8839 	hwrm_req_drop(bp, req);
8840 	return rc;
8841 }
8842 
8843 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
8844 {
8845 	struct hwrm_port_mac_ptp_qcfg_output *resp;
8846 	struct hwrm_port_mac_ptp_qcfg_input *req;
8847 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
8848 	bool phc_cfg;
8849 	u8 flags;
8850 	int rc;
8851 
8852 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5(bp)) {
8853 		rc = -ENODEV;
8854 		goto no_ptp;
8855 	}
8856 
8857 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
8858 	if (rc)
8859 		goto no_ptp;
8860 
8861 	req->port_id = cpu_to_le16(bp->pf.port_id);
8862 	resp = hwrm_req_hold(bp, req);
8863 	rc = hwrm_req_send(bp, req);
8864 	if (rc)
8865 		goto exit;
8866 
8867 	flags = resp->flags;
8868 	if (!(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
8869 		rc = -ENODEV;
8870 		goto exit;
8871 	}
8872 	if (!ptp) {
8873 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
8874 		if (!ptp) {
8875 			rc = -ENOMEM;
8876 			goto exit;
8877 		}
8878 		ptp->bp = bp;
8879 		bp->ptp_cfg = ptp;
8880 	}
8881 	if (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK) {
8882 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
8883 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
8884 	} else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8885 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
8886 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
8887 	} else {
8888 		rc = -ENODEV;
8889 		goto exit;
8890 	}
8891 	phc_cfg = (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
8892 	rc = bnxt_ptp_init(bp, phc_cfg);
8893 	if (rc)
8894 		netdev_warn(bp->dev, "PTP initialization failed.\n");
8895 exit:
8896 	hwrm_req_drop(bp, req);
8897 	if (!rc)
8898 		return 0;
8899 
8900 no_ptp:
8901 	bnxt_ptp_clear(bp);
8902 	kfree(ptp);
8903 	bp->ptp_cfg = NULL;
8904 	return rc;
8905 }
8906 
8907 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
8908 {
8909 	struct hwrm_func_qcaps_output *resp;
8910 	struct hwrm_func_qcaps_input *req;
8911 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8912 	u32 flags, flags_ext, flags_ext2;
8913 	int rc;
8914 
8915 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
8916 	if (rc)
8917 		return rc;
8918 
8919 	req->fid = cpu_to_le16(0xffff);
8920 	resp = hwrm_req_hold(bp, req);
8921 	rc = hwrm_req_send(bp, req);
8922 	if (rc)
8923 		goto hwrm_func_qcaps_exit;
8924 
8925 	flags = le32_to_cpu(resp->flags);
8926 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
8927 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
8928 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
8929 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
8930 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
8931 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
8932 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
8933 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
8934 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
8935 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
8936 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
8937 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
8938 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
8939 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
8940 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
8941 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
8942 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
8943 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
8944 
8945 	flags_ext = le32_to_cpu(resp->flags_ext);
8946 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
8947 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
8948 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
8949 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
8950 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
8951 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
8952 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
8953 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
8954 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
8955 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
8956 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
8957 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
8958 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
8959 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
8960 
8961 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
8962 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
8963 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
8964 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
8965 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
8966 
8967 	bp->tx_push_thresh = 0;
8968 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
8969 	    BNXT_FW_MAJ(bp) > 217)
8970 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
8971 
8972 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
8973 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
8974 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
8975 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
8976 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
8977 	if (!hw_resc->max_hw_ring_grps)
8978 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
8979 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
8980 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
8981 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
8982 
8983 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
8984 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
8985 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
8986 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
8987 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
8988 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
8989 
8990 	if (BNXT_PF(bp)) {
8991 		struct bnxt_pf_info *pf = &bp->pf;
8992 
8993 		pf->fw_fid = le16_to_cpu(resp->fid);
8994 		pf->port_id = le16_to_cpu(resp->port_id);
8995 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
8996 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
8997 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
8998 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
8999 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9000 			bp->flags |= BNXT_FLAG_WOL_CAP;
9001 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9002 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9003 		} else {
9004 			bnxt_ptp_clear(bp);
9005 			kfree(bp->ptp_cfg);
9006 			bp->ptp_cfg = NULL;
9007 		}
9008 	} else {
9009 #ifdef CONFIG_BNXT_SRIOV
9010 		struct bnxt_vf_info *vf = &bp->vf;
9011 
9012 		vf->fw_fid = le16_to_cpu(resp->fid);
9013 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9014 #endif
9015 	}
9016 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9017 
9018 hwrm_func_qcaps_exit:
9019 	hwrm_req_drop(bp, req);
9020 	return rc;
9021 }
9022 
9023 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9024 {
9025 	struct hwrm_dbg_qcaps_output *resp;
9026 	struct hwrm_dbg_qcaps_input *req;
9027 	int rc;
9028 
9029 	bp->fw_dbg_cap = 0;
9030 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9031 		return;
9032 
9033 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9034 	if (rc)
9035 		return;
9036 
9037 	req->fid = cpu_to_le16(0xffff);
9038 	resp = hwrm_req_hold(bp, req);
9039 	rc = hwrm_req_send(bp, req);
9040 	if (rc)
9041 		goto hwrm_dbg_qcaps_exit;
9042 
9043 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9044 
9045 hwrm_dbg_qcaps_exit:
9046 	hwrm_req_drop(bp, req);
9047 }
9048 
9049 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9050 
9051 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9052 {
9053 	int rc;
9054 
9055 	rc = __bnxt_hwrm_func_qcaps(bp);
9056 	if (rc)
9057 		return rc;
9058 
9059 	bnxt_hwrm_dbg_qcaps(bp);
9060 
9061 	rc = bnxt_hwrm_queue_qportcfg(bp);
9062 	if (rc) {
9063 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9064 		return rc;
9065 	}
9066 	if (bp->hwrm_spec_code >= 0x10803) {
9067 		rc = bnxt_alloc_ctx_mem(bp);
9068 		if (rc)
9069 			return rc;
9070 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9071 		if (!rc)
9072 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9073 	}
9074 	return 0;
9075 }
9076 
9077 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9078 {
9079 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9080 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9081 	u32 flags;
9082 	int rc;
9083 
9084 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9085 		return 0;
9086 
9087 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9088 	if (rc)
9089 		return rc;
9090 
9091 	resp = hwrm_req_hold(bp, req);
9092 	rc = hwrm_req_send(bp, req);
9093 	if (rc)
9094 		goto hwrm_cfa_adv_qcaps_exit;
9095 
9096 	flags = le32_to_cpu(resp->flags);
9097 	if (flags &
9098 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9099 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9100 
9101 	if (flags &
9102 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9103 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9104 
9105 	if (flags &
9106 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9107 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9108 
9109 hwrm_cfa_adv_qcaps_exit:
9110 	hwrm_req_drop(bp, req);
9111 	return rc;
9112 }
9113 
9114 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9115 {
9116 	if (bp->fw_health)
9117 		return 0;
9118 
9119 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9120 	if (!bp->fw_health)
9121 		return -ENOMEM;
9122 
9123 	mutex_init(&bp->fw_health->lock);
9124 	return 0;
9125 }
9126 
9127 static int bnxt_alloc_fw_health(struct bnxt *bp)
9128 {
9129 	int rc;
9130 
9131 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9132 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9133 		return 0;
9134 
9135 	rc = __bnxt_alloc_fw_health(bp);
9136 	if (rc) {
9137 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9138 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9139 		return rc;
9140 	}
9141 
9142 	return 0;
9143 }
9144 
9145 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9146 {
9147 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9148 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9149 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9150 }
9151 
9152 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9153 {
9154 	struct bnxt_fw_health *fw_health = bp->fw_health;
9155 	u32 reg_type;
9156 
9157 	if (!fw_health)
9158 		return;
9159 
9160 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9161 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9162 		fw_health->status_reliable = false;
9163 
9164 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9165 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9166 		fw_health->resets_reliable = false;
9167 }
9168 
9169 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9170 {
9171 	void __iomem *hs;
9172 	u32 status_loc;
9173 	u32 reg_type;
9174 	u32 sig;
9175 
9176 	if (bp->fw_health)
9177 		bp->fw_health->status_reliable = false;
9178 
9179 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9180 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9181 
9182 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9183 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9184 		if (!bp->chip_num) {
9185 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9186 			bp->chip_num = readl(bp->bar0 +
9187 					     BNXT_FW_HEALTH_WIN_BASE +
9188 					     BNXT_GRC_REG_CHIP_NUM);
9189 		}
9190 		if (!BNXT_CHIP_P5_PLUS(bp))
9191 			return;
9192 
9193 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9194 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9195 	} else {
9196 		status_loc = readl(hs + offsetof(struct hcomm_status,
9197 						 fw_status_loc));
9198 	}
9199 
9200 	if (__bnxt_alloc_fw_health(bp)) {
9201 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9202 		return;
9203 	}
9204 
9205 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9206 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9207 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9208 		__bnxt_map_fw_health_reg(bp, status_loc);
9209 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9210 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9211 	}
9212 
9213 	bp->fw_health->status_reliable = true;
9214 }
9215 
9216 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9217 {
9218 	struct bnxt_fw_health *fw_health = bp->fw_health;
9219 	u32 reg_base = 0xffffffff;
9220 	int i;
9221 
9222 	bp->fw_health->status_reliable = false;
9223 	bp->fw_health->resets_reliable = false;
9224 	/* Only pre-map the monitoring GRC registers using window 3 */
9225 	for (i = 0; i < 4; i++) {
9226 		u32 reg = fw_health->regs[i];
9227 
9228 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9229 			continue;
9230 		if (reg_base == 0xffffffff)
9231 			reg_base = reg & BNXT_GRC_BASE_MASK;
9232 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9233 			return -ERANGE;
9234 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9235 	}
9236 	bp->fw_health->status_reliable = true;
9237 	bp->fw_health->resets_reliable = true;
9238 	if (reg_base == 0xffffffff)
9239 		return 0;
9240 
9241 	__bnxt_map_fw_health_reg(bp, reg_base);
9242 	return 0;
9243 }
9244 
9245 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9246 {
9247 	if (!bp->fw_health)
9248 		return;
9249 
9250 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9251 		bp->fw_health->status_reliable = true;
9252 		bp->fw_health->resets_reliable = true;
9253 	} else {
9254 		bnxt_try_map_fw_health_reg(bp);
9255 	}
9256 }
9257 
9258 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9259 {
9260 	struct bnxt_fw_health *fw_health = bp->fw_health;
9261 	struct hwrm_error_recovery_qcfg_output *resp;
9262 	struct hwrm_error_recovery_qcfg_input *req;
9263 	int rc, i;
9264 
9265 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9266 		return 0;
9267 
9268 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9269 	if (rc)
9270 		return rc;
9271 
9272 	resp = hwrm_req_hold(bp, req);
9273 	rc = hwrm_req_send(bp, req);
9274 	if (rc)
9275 		goto err_recovery_out;
9276 	fw_health->flags = le32_to_cpu(resp->flags);
9277 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
9278 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
9279 		rc = -EINVAL;
9280 		goto err_recovery_out;
9281 	}
9282 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
9283 	fw_health->master_func_wait_dsecs =
9284 		le32_to_cpu(resp->master_func_wait_period);
9285 	fw_health->normal_func_wait_dsecs =
9286 		le32_to_cpu(resp->normal_func_wait_period);
9287 	fw_health->post_reset_wait_dsecs =
9288 		le32_to_cpu(resp->master_func_wait_period_after_reset);
9289 	fw_health->post_reset_max_wait_dsecs =
9290 		le32_to_cpu(resp->max_bailout_time_after_reset);
9291 	fw_health->regs[BNXT_FW_HEALTH_REG] =
9292 		le32_to_cpu(resp->fw_health_status_reg);
9293 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
9294 		le32_to_cpu(resp->fw_heartbeat_reg);
9295 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
9296 		le32_to_cpu(resp->fw_reset_cnt_reg);
9297 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
9298 		le32_to_cpu(resp->reset_inprogress_reg);
9299 	fw_health->fw_reset_inprog_reg_mask =
9300 		le32_to_cpu(resp->reset_inprogress_reg_mask);
9301 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
9302 	if (fw_health->fw_reset_seq_cnt >= 16) {
9303 		rc = -EINVAL;
9304 		goto err_recovery_out;
9305 	}
9306 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
9307 		fw_health->fw_reset_seq_regs[i] =
9308 			le32_to_cpu(resp->reset_reg[i]);
9309 		fw_health->fw_reset_seq_vals[i] =
9310 			le32_to_cpu(resp->reset_reg_val[i]);
9311 		fw_health->fw_reset_seq_delay_msec[i] =
9312 			resp->delay_after_reset[i];
9313 	}
9314 err_recovery_out:
9315 	hwrm_req_drop(bp, req);
9316 	if (!rc)
9317 		rc = bnxt_map_fw_health_regs(bp);
9318 	if (rc)
9319 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9320 	return rc;
9321 }
9322 
9323 static int bnxt_hwrm_func_reset(struct bnxt *bp)
9324 {
9325 	struct hwrm_func_reset_input *req;
9326 	int rc;
9327 
9328 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
9329 	if (rc)
9330 		return rc;
9331 
9332 	req->enables = 0;
9333 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
9334 	return hwrm_req_send(bp, req);
9335 }
9336 
9337 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
9338 {
9339 	struct hwrm_nvm_get_dev_info_output nvm_info;
9340 
9341 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
9342 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
9343 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
9344 			 nvm_info.nvm_cfg_ver_upd);
9345 }
9346 
9347 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
9348 {
9349 	struct hwrm_queue_qportcfg_output *resp;
9350 	struct hwrm_queue_qportcfg_input *req;
9351 	u8 i, j, *qptr;
9352 	bool no_rdma;
9353 	int rc = 0;
9354 
9355 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
9356 	if (rc)
9357 		return rc;
9358 
9359 	resp = hwrm_req_hold(bp, req);
9360 	rc = hwrm_req_send(bp, req);
9361 	if (rc)
9362 		goto qportcfg_exit;
9363 
9364 	if (!resp->max_configurable_queues) {
9365 		rc = -EINVAL;
9366 		goto qportcfg_exit;
9367 	}
9368 	bp->max_tc = resp->max_configurable_queues;
9369 	bp->max_lltc = resp->max_configurable_lossless_queues;
9370 	if (bp->max_tc > BNXT_MAX_QUEUE)
9371 		bp->max_tc = BNXT_MAX_QUEUE;
9372 
9373 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
9374 	qptr = &resp->queue_id0;
9375 	for (i = 0, j = 0; i < bp->max_tc; i++) {
9376 		bp->q_info[j].queue_id = *qptr;
9377 		bp->q_ids[i] = *qptr++;
9378 		bp->q_info[j].queue_profile = *qptr++;
9379 		bp->tc_to_qidx[j] = j;
9380 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
9381 		    (no_rdma && BNXT_PF(bp)))
9382 			j++;
9383 	}
9384 	bp->max_q = bp->max_tc;
9385 	bp->max_tc = max_t(u8, j, 1);
9386 
9387 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
9388 		bp->max_tc = 1;
9389 
9390 	if (bp->max_lltc > bp->max_tc)
9391 		bp->max_lltc = bp->max_tc;
9392 
9393 qportcfg_exit:
9394 	hwrm_req_drop(bp, req);
9395 	return rc;
9396 }
9397 
9398 static int bnxt_hwrm_poll(struct bnxt *bp)
9399 {
9400 	struct hwrm_ver_get_input *req;
9401 	int rc;
9402 
9403 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9404 	if (rc)
9405 		return rc;
9406 
9407 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9408 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9409 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9410 
9411 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
9412 	rc = hwrm_req_send(bp, req);
9413 	return rc;
9414 }
9415 
9416 static int bnxt_hwrm_ver_get(struct bnxt *bp)
9417 {
9418 	struct hwrm_ver_get_output *resp;
9419 	struct hwrm_ver_get_input *req;
9420 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
9421 	u32 dev_caps_cfg, hwrm_ver;
9422 	int rc, len;
9423 
9424 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9425 	if (rc)
9426 		return rc;
9427 
9428 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
9429 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
9430 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9431 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9432 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9433 
9434 	resp = hwrm_req_hold(bp, req);
9435 	rc = hwrm_req_send(bp, req);
9436 	if (rc)
9437 		goto hwrm_ver_get_exit;
9438 
9439 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
9440 
9441 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
9442 			     resp->hwrm_intf_min_8b << 8 |
9443 			     resp->hwrm_intf_upd_8b;
9444 	if (resp->hwrm_intf_maj_8b < 1) {
9445 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
9446 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
9447 			    resp->hwrm_intf_upd_8b);
9448 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
9449 	}
9450 
9451 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
9452 			HWRM_VERSION_UPDATE;
9453 
9454 	if (bp->hwrm_spec_code > hwrm_ver)
9455 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
9456 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
9457 			 HWRM_VERSION_UPDATE);
9458 	else
9459 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
9460 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
9461 			 resp->hwrm_intf_upd_8b);
9462 
9463 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
9464 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
9465 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
9466 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
9467 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
9468 		len = FW_VER_STR_LEN;
9469 	} else {
9470 		fw_maj = resp->hwrm_fw_maj_8b;
9471 		fw_min = resp->hwrm_fw_min_8b;
9472 		fw_bld = resp->hwrm_fw_bld_8b;
9473 		fw_rsv = resp->hwrm_fw_rsvd_8b;
9474 		len = BC_HWRM_STR_LEN;
9475 	}
9476 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
9477 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
9478 		 fw_rsv);
9479 
9480 	if (strlen(resp->active_pkg_name)) {
9481 		int fw_ver_len = strlen(bp->fw_ver_str);
9482 
9483 		snprintf(bp->fw_ver_str + fw_ver_len,
9484 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
9485 			 resp->active_pkg_name);
9486 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
9487 	}
9488 
9489 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
9490 	if (!bp->hwrm_cmd_timeout)
9491 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
9492 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
9493 	if (!bp->hwrm_cmd_max_timeout)
9494 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
9495 	else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT)
9496 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n",
9497 			    bp->hwrm_cmd_max_timeout / 1000);
9498 
9499 	if (resp->hwrm_intf_maj_8b >= 1) {
9500 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
9501 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
9502 	}
9503 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
9504 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
9505 
9506 	bp->chip_num = le16_to_cpu(resp->chip_num);
9507 	bp->chip_rev = resp->chip_rev;
9508 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
9509 	    !resp->chip_metal)
9510 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
9511 
9512 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
9513 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
9514 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
9515 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
9516 
9517 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
9518 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
9519 
9520 	if (dev_caps_cfg &
9521 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
9522 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
9523 
9524 	if (dev_caps_cfg &
9525 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
9526 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
9527 
9528 	if (dev_caps_cfg &
9529 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
9530 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
9531 
9532 hwrm_ver_get_exit:
9533 	hwrm_req_drop(bp, req);
9534 	return rc;
9535 }
9536 
9537 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
9538 {
9539 	struct hwrm_fw_set_time_input *req;
9540 	struct tm tm;
9541 	time64_t now = ktime_get_real_seconds();
9542 	int rc;
9543 
9544 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
9545 	    bp->hwrm_spec_code < 0x10400)
9546 		return -EOPNOTSUPP;
9547 
9548 	time64_to_tm(now, 0, &tm);
9549 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
9550 	if (rc)
9551 		return rc;
9552 
9553 	req->year = cpu_to_le16(1900 + tm.tm_year);
9554 	req->month = 1 + tm.tm_mon;
9555 	req->day = tm.tm_mday;
9556 	req->hour = tm.tm_hour;
9557 	req->minute = tm.tm_min;
9558 	req->second = tm.tm_sec;
9559 	return hwrm_req_send(bp, req);
9560 }
9561 
9562 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
9563 {
9564 	u64 sw_tmp;
9565 
9566 	hw &= mask;
9567 	sw_tmp = (*sw & ~mask) | hw;
9568 	if (hw < (*sw & mask))
9569 		sw_tmp += mask + 1;
9570 	WRITE_ONCE(*sw, sw_tmp);
9571 }
9572 
9573 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
9574 				    int count, bool ignore_zero)
9575 {
9576 	int i;
9577 
9578 	for (i = 0; i < count; i++) {
9579 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
9580 
9581 		if (ignore_zero && !hw)
9582 			continue;
9583 
9584 		if (masks[i] == -1ULL)
9585 			sw_stats[i] = hw;
9586 		else
9587 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
9588 	}
9589 }
9590 
9591 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
9592 {
9593 	if (!stats->hw_stats)
9594 		return;
9595 
9596 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
9597 				stats->hw_masks, stats->len / 8, false);
9598 }
9599 
9600 static void bnxt_accumulate_all_stats(struct bnxt *bp)
9601 {
9602 	struct bnxt_stats_mem *ring0_stats;
9603 	bool ignore_zero = false;
9604 	int i;
9605 
9606 	/* Chip bug.  Counter intermittently becomes 0. */
9607 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
9608 		ignore_zero = true;
9609 
9610 	for (i = 0; i < bp->cp_nr_rings; i++) {
9611 		struct bnxt_napi *bnapi = bp->bnapi[i];
9612 		struct bnxt_cp_ring_info *cpr;
9613 		struct bnxt_stats_mem *stats;
9614 
9615 		cpr = &bnapi->cp_ring;
9616 		stats = &cpr->stats;
9617 		if (!i)
9618 			ring0_stats = stats;
9619 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
9620 					ring0_stats->hw_masks,
9621 					ring0_stats->len / 8, ignore_zero);
9622 	}
9623 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
9624 		struct bnxt_stats_mem *stats = &bp->port_stats;
9625 		__le64 *hw_stats = stats->hw_stats;
9626 		u64 *sw_stats = stats->sw_stats;
9627 		u64 *masks = stats->hw_masks;
9628 		int cnt;
9629 
9630 		cnt = sizeof(struct rx_port_stats) / 8;
9631 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
9632 
9633 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9634 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9635 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9636 		cnt = sizeof(struct tx_port_stats) / 8;
9637 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
9638 	}
9639 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
9640 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
9641 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
9642 	}
9643 }
9644 
9645 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
9646 {
9647 	struct hwrm_port_qstats_input *req;
9648 	struct bnxt_pf_info *pf = &bp->pf;
9649 	int rc;
9650 
9651 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
9652 		return 0;
9653 
9654 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
9655 		return -EOPNOTSUPP;
9656 
9657 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
9658 	if (rc)
9659 		return rc;
9660 
9661 	req->flags = flags;
9662 	req->port_id = cpu_to_le16(pf->port_id);
9663 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
9664 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
9665 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
9666 	return hwrm_req_send(bp, req);
9667 }
9668 
9669 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
9670 {
9671 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
9672 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
9673 	struct hwrm_port_qstats_ext_output *resp_qs;
9674 	struct hwrm_port_qstats_ext_input *req_qs;
9675 	struct bnxt_pf_info *pf = &bp->pf;
9676 	u32 tx_stat_size;
9677 	int rc;
9678 
9679 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
9680 		return 0;
9681 
9682 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
9683 		return -EOPNOTSUPP;
9684 
9685 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
9686 	if (rc)
9687 		return rc;
9688 
9689 	req_qs->flags = flags;
9690 	req_qs->port_id = cpu_to_le16(pf->port_id);
9691 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
9692 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
9693 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
9694 		       sizeof(struct tx_port_stats_ext) : 0;
9695 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
9696 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
9697 	resp_qs = hwrm_req_hold(bp, req_qs);
9698 	rc = hwrm_req_send(bp, req_qs);
9699 	if (!rc) {
9700 		bp->fw_rx_stats_ext_size =
9701 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
9702 		if (BNXT_FW_MAJ(bp) < 220 &&
9703 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
9704 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
9705 
9706 		bp->fw_tx_stats_ext_size = tx_stat_size ?
9707 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
9708 	} else {
9709 		bp->fw_rx_stats_ext_size = 0;
9710 		bp->fw_tx_stats_ext_size = 0;
9711 	}
9712 	hwrm_req_drop(bp, req_qs);
9713 
9714 	if (flags)
9715 		return rc;
9716 
9717 	if (bp->fw_tx_stats_ext_size <=
9718 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
9719 		bp->pri2cos_valid = 0;
9720 		return rc;
9721 	}
9722 
9723 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
9724 	if (rc)
9725 		return rc;
9726 
9727 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
9728 
9729 	resp_qc = hwrm_req_hold(bp, req_qc);
9730 	rc = hwrm_req_send(bp, req_qc);
9731 	if (!rc) {
9732 		u8 *pri2cos;
9733 		int i, j;
9734 
9735 		pri2cos = &resp_qc->pri0_cos_queue_id;
9736 		for (i = 0; i < 8; i++) {
9737 			u8 queue_id = pri2cos[i];
9738 			u8 queue_idx;
9739 
9740 			/* Per port queue IDs start from 0, 10, 20, etc */
9741 			queue_idx = queue_id % 10;
9742 			if (queue_idx > BNXT_MAX_QUEUE) {
9743 				bp->pri2cos_valid = false;
9744 				hwrm_req_drop(bp, req_qc);
9745 				return rc;
9746 			}
9747 			for (j = 0; j < bp->max_q; j++) {
9748 				if (bp->q_ids[j] == queue_id)
9749 					bp->pri2cos_idx[i] = queue_idx;
9750 			}
9751 		}
9752 		bp->pri2cos_valid = true;
9753 	}
9754 	hwrm_req_drop(bp, req_qc);
9755 
9756 	return rc;
9757 }
9758 
9759 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
9760 {
9761 	bnxt_hwrm_tunnel_dst_port_free(bp,
9762 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
9763 	bnxt_hwrm_tunnel_dst_port_free(bp,
9764 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
9765 }
9766 
9767 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
9768 {
9769 	int rc, i;
9770 	u32 tpa_flags = 0;
9771 
9772 	if (set_tpa)
9773 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
9774 	else if (BNXT_NO_FW_ACCESS(bp))
9775 		return 0;
9776 	for (i = 0; i < bp->nr_vnics; i++) {
9777 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
9778 		if (rc) {
9779 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
9780 				   i, rc);
9781 			return rc;
9782 		}
9783 	}
9784 	return 0;
9785 }
9786 
9787 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
9788 {
9789 	int i;
9790 
9791 	for (i = 0; i < bp->nr_vnics; i++)
9792 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
9793 }
9794 
9795 static void bnxt_clear_vnic(struct bnxt *bp)
9796 {
9797 	if (!bp->vnic_info)
9798 		return;
9799 
9800 	bnxt_hwrm_clear_vnic_filter(bp);
9801 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
9802 		/* clear all RSS setting before free vnic ctx */
9803 		bnxt_hwrm_clear_vnic_rss(bp);
9804 		bnxt_hwrm_vnic_ctx_free(bp);
9805 	}
9806 	/* before free the vnic, undo the vnic tpa settings */
9807 	if (bp->flags & BNXT_FLAG_TPA)
9808 		bnxt_set_tpa(bp, false);
9809 	bnxt_hwrm_vnic_free(bp);
9810 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
9811 		bnxt_hwrm_vnic_ctx_free(bp);
9812 }
9813 
9814 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
9815 				    bool irq_re_init)
9816 {
9817 	bnxt_clear_vnic(bp);
9818 	bnxt_hwrm_ring_free(bp, close_path);
9819 	bnxt_hwrm_ring_grp_free(bp);
9820 	if (irq_re_init) {
9821 		bnxt_hwrm_stat_ctx_free(bp);
9822 		bnxt_hwrm_free_tunnel_ports(bp);
9823 	}
9824 }
9825 
9826 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
9827 {
9828 	struct hwrm_func_cfg_input *req;
9829 	u8 evb_mode;
9830 	int rc;
9831 
9832 	if (br_mode == BRIDGE_MODE_VEB)
9833 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
9834 	else if (br_mode == BRIDGE_MODE_VEPA)
9835 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
9836 	else
9837 		return -EINVAL;
9838 
9839 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
9840 	if (rc)
9841 		return rc;
9842 
9843 	req->fid = cpu_to_le16(0xffff);
9844 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
9845 	req->evb_mode = evb_mode;
9846 	return hwrm_req_send(bp, req);
9847 }
9848 
9849 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
9850 {
9851 	struct hwrm_func_cfg_input *req;
9852 	int rc;
9853 
9854 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
9855 		return 0;
9856 
9857 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
9858 	if (rc)
9859 		return rc;
9860 
9861 	req->fid = cpu_to_le16(0xffff);
9862 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
9863 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
9864 	if (size == 128)
9865 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
9866 
9867 	return hwrm_req_send(bp, req);
9868 }
9869 
9870 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
9871 {
9872 	int rc;
9873 
9874 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
9875 		goto skip_rss_ctx;
9876 
9877 	/* allocate context for vnic */
9878 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
9879 	if (rc) {
9880 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
9881 			   vnic->vnic_id, rc);
9882 		goto vnic_setup_err;
9883 	}
9884 	bp->rsscos_nr_ctxs++;
9885 
9886 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
9887 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
9888 		if (rc) {
9889 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
9890 				   vnic->vnic_id, rc);
9891 			goto vnic_setup_err;
9892 		}
9893 		bp->rsscos_nr_ctxs++;
9894 	}
9895 
9896 skip_rss_ctx:
9897 	/* configure default vnic, ring grp */
9898 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
9899 	if (rc) {
9900 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
9901 			   vnic->vnic_id, rc);
9902 		goto vnic_setup_err;
9903 	}
9904 
9905 	/* Enable RSS hashing on vnic */
9906 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
9907 	if (rc) {
9908 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
9909 			   vnic->vnic_id, rc);
9910 		goto vnic_setup_err;
9911 	}
9912 
9913 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
9914 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
9915 		if (rc) {
9916 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
9917 				   vnic->vnic_id, rc);
9918 		}
9919 	}
9920 
9921 vnic_setup_err:
9922 	return rc;
9923 }
9924 
9925 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
9926 {
9927 	int rc;
9928 
9929 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
9930 	if (rc) {
9931 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
9932 			   vnic->vnic_id, rc);
9933 		return rc;
9934 	}
9935 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
9936 	if (rc)
9937 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
9938 			   vnic->vnic_id, rc);
9939 	return rc;
9940 }
9941 
9942 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
9943 {
9944 	int rc, i, nr_ctxs;
9945 
9946 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
9947 	for (i = 0; i < nr_ctxs; i++) {
9948 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
9949 		if (rc) {
9950 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
9951 				   vnic->vnic_id, i, rc);
9952 			break;
9953 		}
9954 		bp->rsscos_nr_ctxs++;
9955 	}
9956 	if (i < nr_ctxs)
9957 		return -ENOMEM;
9958 
9959 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
9960 	if (rc)
9961 		return rc;
9962 
9963 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
9964 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
9965 		if (rc) {
9966 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
9967 				   vnic->vnic_id, rc);
9968 		}
9969 	}
9970 	return rc;
9971 }
9972 
9973 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
9974 {
9975 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
9976 		return __bnxt_setup_vnic_p5(bp, vnic);
9977 	else
9978 		return __bnxt_setup_vnic(bp, vnic);
9979 }
9980 
9981 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
9982 				     struct bnxt_vnic_info *vnic,
9983 				     u16 start_rx_ring_idx, int rx_rings)
9984 {
9985 	int rc;
9986 
9987 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
9988 	if (rc) {
9989 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
9990 			   vnic->vnic_id, rc);
9991 		return rc;
9992 	}
9993 	return bnxt_setup_vnic(bp, vnic);
9994 }
9995 
9996 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
9997 {
9998 	struct bnxt_vnic_info *vnic;
9999 	int i, rc = 0;
10000 
10001 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10002 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10003 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10004 	}
10005 
10006 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10007 		return 0;
10008 
10009 	for (i = 0; i < bp->rx_nr_rings; i++) {
10010 		u16 vnic_id = i + 1;
10011 		u16 ring_id = i;
10012 
10013 		if (vnic_id >= bp->nr_vnics)
10014 			break;
10015 
10016 		vnic = &bp->vnic_info[vnic_id];
10017 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10018 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10019 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10020 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10021 			break;
10022 	}
10023 	return rc;
10024 }
10025 
10026 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10027 			  bool all)
10028 {
10029 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10030 	struct bnxt_filter_base *usr_fltr, *tmp;
10031 	struct bnxt_ntuple_filter *ntp_fltr;
10032 	int i;
10033 
10034 	bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10035 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10036 		if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10037 			bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10038 	}
10039 	if (!all)
10040 		return;
10041 
10042 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10043 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10044 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10045 			ntp_fltr = container_of(usr_fltr,
10046 						struct bnxt_ntuple_filter,
10047 						base);
10048 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10049 			bnxt_del_ntp_filter(bp, ntp_fltr);
10050 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10051 		}
10052 	}
10053 
10054 	if (vnic->rss_table)
10055 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10056 				  vnic->rss_table,
10057 				  vnic->rss_table_dma_addr);
10058 	kfree(rss_ctx->rss_indir_tbl);
10059 	list_del(&rss_ctx->list);
10060 	bp->num_rss_ctx--;
10061 	clear_bit(rss_ctx->index, bp->rss_ctx_bmap);
10062 	kfree(rss_ctx);
10063 }
10064 
10065 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10066 {
10067 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10068 	struct bnxt_rss_ctx *rss_ctx, *tmp;
10069 
10070 	list_for_each_entry_safe(rss_ctx, tmp, &bp->rss_ctx_list, list) {
10071 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10072 
10073 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10074 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10075 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10076 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10077 				   rss_ctx->index);
10078 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10079 		}
10080 	}
10081 }
10082 
10083 struct bnxt_rss_ctx *bnxt_alloc_rss_ctx(struct bnxt *bp)
10084 {
10085 	struct bnxt_rss_ctx *rss_ctx = NULL;
10086 
10087 	rss_ctx = kzalloc(sizeof(*rss_ctx), GFP_KERNEL);
10088 	if (rss_ctx) {
10089 		rss_ctx->vnic.rss_ctx = rss_ctx;
10090 		list_add_tail(&rss_ctx->list, &bp->rss_ctx_list);
10091 		bp->num_rss_ctx++;
10092 	}
10093 	return rss_ctx;
10094 }
10095 
10096 void bnxt_clear_rss_ctxs(struct bnxt *bp, bool all)
10097 {
10098 	struct bnxt_rss_ctx *rss_ctx, *tmp;
10099 
10100 	list_for_each_entry_safe(rss_ctx, tmp, &bp->rss_ctx_list, list)
10101 		bnxt_del_one_rss_ctx(bp, rss_ctx, all);
10102 
10103 	if (all)
10104 		bitmap_free(bp->rss_ctx_bmap);
10105 }
10106 
10107 static void bnxt_init_multi_rss_ctx(struct bnxt *bp)
10108 {
10109 	bp->rss_ctx_bmap = bitmap_zalloc(BNXT_RSS_CTX_BMAP_LEN, GFP_KERNEL);
10110 	if (bp->rss_ctx_bmap) {
10111 		/* burn index 0 since we cannot have context 0 */
10112 		__set_bit(0, bp->rss_ctx_bmap);
10113 		INIT_LIST_HEAD(&bp->rss_ctx_list);
10114 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
10115 	}
10116 }
10117 
10118 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
10119 static bool bnxt_promisc_ok(struct bnxt *bp)
10120 {
10121 #ifdef CONFIG_BNXT_SRIOV
10122 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10123 		return false;
10124 #endif
10125 	return true;
10126 }
10127 
10128 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10129 {
10130 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10131 	unsigned int rc = 0;
10132 
10133 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10134 	if (rc) {
10135 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10136 			   rc);
10137 		return rc;
10138 	}
10139 
10140 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10141 	if (rc) {
10142 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10143 			   rc);
10144 		return rc;
10145 	}
10146 	return rc;
10147 }
10148 
10149 static int bnxt_cfg_rx_mode(struct bnxt *);
10150 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10151 
10152 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10153 {
10154 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10155 	int rc = 0;
10156 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10157 
10158 	if (irq_re_init) {
10159 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10160 		if (rc) {
10161 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10162 				   rc);
10163 			goto err_out;
10164 		}
10165 	}
10166 
10167 	rc = bnxt_hwrm_ring_alloc(bp);
10168 	if (rc) {
10169 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10170 		goto err_out;
10171 	}
10172 
10173 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10174 	if (rc) {
10175 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10176 		goto err_out;
10177 	}
10178 
10179 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10180 		rx_nr_rings--;
10181 
10182 	/* default vnic 0 */
10183 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10184 	if (rc) {
10185 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10186 		goto err_out;
10187 	}
10188 
10189 	if (BNXT_VF(bp))
10190 		bnxt_hwrm_func_qcfg(bp);
10191 
10192 	rc = bnxt_setup_vnic(bp, vnic);
10193 	if (rc)
10194 		goto err_out;
10195 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10196 		bnxt_hwrm_update_rss_hash_cfg(bp);
10197 
10198 	if (bp->flags & BNXT_FLAG_RFS) {
10199 		rc = bnxt_alloc_rfs_vnics(bp);
10200 		if (rc)
10201 			goto err_out;
10202 	}
10203 
10204 	if (bp->flags & BNXT_FLAG_TPA) {
10205 		rc = bnxt_set_tpa(bp, true);
10206 		if (rc)
10207 			goto err_out;
10208 	}
10209 
10210 	if (BNXT_VF(bp))
10211 		bnxt_update_vf_mac(bp);
10212 
10213 	/* Filter for default vnic 0 */
10214 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
10215 	if (rc) {
10216 		if (BNXT_VF(bp) && rc == -ENODEV)
10217 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
10218 		else
10219 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
10220 		goto err_out;
10221 	}
10222 	vnic->uc_filter_count = 1;
10223 
10224 	vnic->rx_mask = 0;
10225 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
10226 		goto skip_rx_mask;
10227 
10228 	if (bp->dev->flags & IFF_BROADCAST)
10229 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10230 
10231 	if (bp->dev->flags & IFF_PROMISC)
10232 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10233 
10234 	if (bp->dev->flags & IFF_ALLMULTI) {
10235 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10236 		vnic->mc_list_count = 0;
10237 	} else if (bp->dev->flags & IFF_MULTICAST) {
10238 		u32 mask = 0;
10239 
10240 		bnxt_mc_list_updated(bp, &mask);
10241 		vnic->rx_mask |= mask;
10242 	}
10243 
10244 	rc = bnxt_cfg_rx_mode(bp);
10245 	if (rc)
10246 		goto err_out;
10247 
10248 skip_rx_mask:
10249 	rc = bnxt_hwrm_set_coal(bp);
10250 	if (rc)
10251 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
10252 				rc);
10253 
10254 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10255 		rc = bnxt_setup_nitroa0_vnic(bp);
10256 		if (rc)
10257 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
10258 				   rc);
10259 	}
10260 
10261 	if (BNXT_VF(bp)) {
10262 		bnxt_hwrm_func_qcfg(bp);
10263 		netdev_update_features(bp->dev);
10264 	}
10265 
10266 	return 0;
10267 
10268 err_out:
10269 	bnxt_hwrm_resource_free(bp, 0, true);
10270 
10271 	return rc;
10272 }
10273 
10274 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
10275 {
10276 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
10277 	return 0;
10278 }
10279 
10280 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
10281 {
10282 	bnxt_init_cp_rings(bp);
10283 	bnxt_init_rx_rings(bp);
10284 	bnxt_init_tx_rings(bp);
10285 	bnxt_init_ring_grps(bp, irq_re_init);
10286 	bnxt_init_vnics(bp);
10287 
10288 	return bnxt_init_chip(bp, irq_re_init);
10289 }
10290 
10291 static int bnxt_set_real_num_queues(struct bnxt *bp)
10292 {
10293 	int rc;
10294 	struct net_device *dev = bp->dev;
10295 
10296 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
10297 					  bp->tx_nr_rings_xdp);
10298 	if (rc)
10299 		return rc;
10300 
10301 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
10302 	if (rc)
10303 		return rc;
10304 
10305 #ifdef CONFIG_RFS_ACCEL
10306 	if (bp->flags & BNXT_FLAG_RFS)
10307 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
10308 #endif
10309 
10310 	return rc;
10311 }
10312 
10313 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10314 			     bool shared)
10315 {
10316 	int _rx = *rx, _tx = *tx;
10317 
10318 	if (shared) {
10319 		*rx = min_t(int, _rx, max);
10320 		*tx = min_t(int, _tx, max);
10321 	} else {
10322 		if (max < 2)
10323 			return -ENOMEM;
10324 
10325 		while (_rx + _tx > max) {
10326 			if (_rx > _tx && _rx > 1)
10327 				_rx--;
10328 			else if (_tx > 1)
10329 				_tx--;
10330 		}
10331 		*rx = _rx;
10332 		*tx = _tx;
10333 	}
10334 	return 0;
10335 }
10336 
10337 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
10338 {
10339 	return (tx - tx_xdp) / tx_sets + tx_xdp;
10340 }
10341 
10342 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
10343 {
10344 	int tcs = bp->num_tc;
10345 
10346 	if (!tcs)
10347 		tcs = 1;
10348 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
10349 }
10350 
10351 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
10352 {
10353 	int tcs = bp->num_tc;
10354 
10355 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
10356 	       bp->tx_nr_rings_xdp;
10357 }
10358 
10359 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10360 			   bool sh)
10361 {
10362 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
10363 
10364 	if (tx_cp != *tx) {
10365 		int tx_saved = tx_cp, rc;
10366 
10367 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
10368 		if (rc)
10369 			return rc;
10370 		if (tx_cp != tx_saved)
10371 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
10372 		return 0;
10373 	}
10374 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
10375 }
10376 
10377 static void bnxt_setup_msix(struct bnxt *bp)
10378 {
10379 	const int len = sizeof(bp->irq_tbl[0].name);
10380 	struct net_device *dev = bp->dev;
10381 	int tcs, i;
10382 
10383 	tcs = bp->num_tc;
10384 	if (tcs) {
10385 		int i, off, count;
10386 
10387 		for (i = 0; i < tcs; i++) {
10388 			count = bp->tx_nr_rings_per_tc;
10389 			off = BNXT_TC_TO_RING_BASE(bp, i);
10390 			netdev_set_tc_queue(dev, i, count, off);
10391 		}
10392 	}
10393 
10394 	for (i = 0; i < bp->cp_nr_rings; i++) {
10395 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10396 		char *attr;
10397 
10398 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
10399 			attr = "TxRx";
10400 		else if (i < bp->rx_nr_rings)
10401 			attr = "rx";
10402 		else
10403 			attr = "tx";
10404 
10405 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
10406 			 attr, i);
10407 		bp->irq_tbl[map_idx].handler = bnxt_msix;
10408 	}
10409 }
10410 
10411 static void bnxt_setup_inta(struct bnxt *bp)
10412 {
10413 	const int len = sizeof(bp->irq_tbl[0].name);
10414 
10415 	if (bp->num_tc) {
10416 		netdev_reset_tc(bp->dev);
10417 		bp->num_tc = 0;
10418 	}
10419 
10420 	snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
10421 		 0);
10422 	bp->irq_tbl[0].handler = bnxt_inta;
10423 }
10424 
10425 static int bnxt_init_int_mode(struct bnxt *bp);
10426 
10427 static int bnxt_setup_int_mode(struct bnxt *bp)
10428 {
10429 	int rc;
10430 
10431 	if (!bp->irq_tbl) {
10432 		rc = bnxt_init_int_mode(bp);
10433 		if (rc || !bp->irq_tbl)
10434 			return rc ?: -ENODEV;
10435 	}
10436 
10437 	if (bp->flags & BNXT_FLAG_USING_MSIX)
10438 		bnxt_setup_msix(bp);
10439 	else
10440 		bnxt_setup_inta(bp);
10441 
10442 	rc = bnxt_set_real_num_queues(bp);
10443 	return rc;
10444 }
10445 
10446 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
10447 {
10448 	return bp->hw_resc.max_rsscos_ctxs;
10449 }
10450 
10451 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
10452 {
10453 	return bp->hw_resc.max_vnics;
10454 }
10455 
10456 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
10457 {
10458 	return bp->hw_resc.max_stat_ctxs;
10459 }
10460 
10461 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
10462 {
10463 	return bp->hw_resc.max_cp_rings;
10464 }
10465 
10466 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
10467 {
10468 	unsigned int cp = bp->hw_resc.max_cp_rings;
10469 
10470 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
10471 		cp -= bnxt_get_ulp_msix_num(bp);
10472 
10473 	return cp;
10474 }
10475 
10476 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
10477 {
10478 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
10479 
10480 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10481 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
10482 
10483 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
10484 }
10485 
10486 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
10487 {
10488 	bp->hw_resc.max_irqs = max_irqs;
10489 }
10490 
10491 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
10492 {
10493 	unsigned int cp;
10494 
10495 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
10496 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10497 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
10498 	else
10499 		return cp - bp->cp_nr_rings;
10500 }
10501 
10502 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
10503 {
10504 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
10505 }
10506 
10507 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
10508 {
10509 	int max_irq = bnxt_get_max_func_irqs(bp);
10510 	int total_req = bp->cp_nr_rings + num;
10511 
10512 	if (max_irq < total_req) {
10513 		num = max_irq - bp->cp_nr_rings;
10514 		if (num <= 0)
10515 			return 0;
10516 	}
10517 	return num;
10518 }
10519 
10520 static int bnxt_get_num_msix(struct bnxt *bp)
10521 {
10522 	if (!BNXT_NEW_RM(bp))
10523 		return bnxt_get_max_func_irqs(bp);
10524 
10525 	return bnxt_nq_rings_in_use(bp);
10526 }
10527 
10528 static int bnxt_init_msix(struct bnxt *bp)
10529 {
10530 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp;
10531 	struct msix_entry *msix_ent;
10532 
10533 	total_vecs = bnxt_get_num_msix(bp);
10534 	max = bnxt_get_max_func_irqs(bp);
10535 	if (total_vecs > max)
10536 		total_vecs = max;
10537 
10538 	if (!total_vecs)
10539 		return 0;
10540 
10541 	msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
10542 	if (!msix_ent)
10543 		return -ENOMEM;
10544 
10545 	for (i = 0; i < total_vecs; i++) {
10546 		msix_ent[i].entry = i;
10547 		msix_ent[i].vector = 0;
10548 	}
10549 
10550 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
10551 		min = 2;
10552 
10553 	total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
10554 	ulp_msix = bnxt_get_ulp_msix_num(bp);
10555 	if (total_vecs < 0 || total_vecs < ulp_msix) {
10556 		rc = -ENODEV;
10557 		goto msix_setup_exit;
10558 	}
10559 
10560 	bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
10561 	if (bp->irq_tbl) {
10562 		for (i = 0; i < total_vecs; i++)
10563 			bp->irq_tbl[i].vector = msix_ent[i].vector;
10564 
10565 		bp->total_irqs = total_vecs;
10566 		/* Trim rings based upon num of vectors allocated */
10567 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
10568 				     total_vecs - ulp_msix, min == 1);
10569 		if (rc)
10570 			goto msix_setup_exit;
10571 
10572 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
10573 		bp->cp_nr_rings = (min == 1) ?
10574 				  max_t(int, tx_cp, bp->rx_nr_rings) :
10575 				  tx_cp + bp->rx_nr_rings;
10576 
10577 	} else {
10578 		rc = -ENOMEM;
10579 		goto msix_setup_exit;
10580 	}
10581 	bp->flags |= BNXT_FLAG_USING_MSIX;
10582 	kfree(msix_ent);
10583 	return 0;
10584 
10585 msix_setup_exit:
10586 	netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
10587 	kfree(bp->irq_tbl);
10588 	bp->irq_tbl = NULL;
10589 	pci_disable_msix(bp->pdev);
10590 	kfree(msix_ent);
10591 	return rc;
10592 }
10593 
10594 static int bnxt_init_inta(struct bnxt *bp)
10595 {
10596 	bp->irq_tbl = kzalloc(sizeof(struct bnxt_irq), GFP_KERNEL);
10597 	if (!bp->irq_tbl)
10598 		return -ENOMEM;
10599 
10600 	bp->total_irqs = 1;
10601 	bp->rx_nr_rings = 1;
10602 	bp->tx_nr_rings = 1;
10603 	bp->cp_nr_rings = 1;
10604 	bp->flags |= BNXT_FLAG_SHARED_RINGS;
10605 	bp->irq_tbl[0].vector = bp->pdev->irq;
10606 	return 0;
10607 }
10608 
10609 static int bnxt_init_int_mode(struct bnxt *bp)
10610 {
10611 	int rc = -ENODEV;
10612 
10613 	if (bp->flags & BNXT_FLAG_MSIX_CAP)
10614 		rc = bnxt_init_msix(bp);
10615 
10616 	if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
10617 		/* fallback to INTA */
10618 		rc = bnxt_init_inta(bp);
10619 	}
10620 	return rc;
10621 }
10622 
10623 static void bnxt_clear_int_mode(struct bnxt *bp)
10624 {
10625 	if (bp->flags & BNXT_FLAG_USING_MSIX)
10626 		pci_disable_msix(bp->pdev);
10627 
10628 	kfree(bp->irq_tbl);
10629 	bp->irq_tbl = NULL;
10630 	bp->flags &= ~BNXT_FLAG_USING_MSIX;
10631 }
10632 
10633 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
10634 {
10635 	bool irq_cleared = false;
10636 	int tcs = bp->num_tc;
10637 	int irqs_required;
10638 	int rc;
10639 
10640 	if (!bnxt_need_reserve_rings(bp))
10641 		return 0;
10642 
10643 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
10644 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
10645 
10646 		if (ulp_msix > bp->ulp_num_msix_want)
10647 			ulp_msix = bp->ulp_num_msix_want;
10648 		irqs_required = ulp_msix + bp->cp_nr_rings;
10649 	} else {
10650 		irqs_required = bnxt_get_num_msix(bp);
10651 	}
10652 
10653 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
10654 		bnxt_ulp_irq_stop(bp);
10655 		bnxt_clear_int_mode(bp);
10656 		irq_cleared = true;
10657 	}
10658 	rc = __bnxt_reserve_rings(bp);
10659 	if (irq_cleared) {
10660 		if (!rc)
10661 			rc = bnxt_init_int_mode(bp);
10662 		bnxt_ulp_irq_restart(bp, rc);
10663 	}
10664 	if (rc) {
10665 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
10666 		return rc;
10667 	}
10668 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
10669 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
10670 		netdev_err(bp->dev, "tx ring reservation failure\n");
10671 		netdev_reset_tc(bp->dev);
10672 		bp->num_tc = 0;
10673 		if (bp->tx_nr_rings_xdp)
10674 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
10675 		else
10676 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
10677 		return -ENOMEM;
10678 	}
10679 	return 0;
10680 }
10681 
10682 static void bnxt_free_irq(struct bnxt *bp)
10683 {
10684 	struct bnxt_irq *irq;
10685 	int i;
10686 
10687 #ifdef CONFIG_RFS_ACCEL
10688 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
10689 	bp->dev->rx_cpu_rmap = NULL;
10690 #endif
10691 	if (!bp->irq_tbl || !bp->bnapi)
10692 		return;
10693 
10694 	for (i = 0; i < bp->cp_nr_rings; i++) {
10695 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10696 
10697 		irq = &bp->irq_tbl[map_idx];
10698 		if (irq->requested) {
10699 			if (irq->have_cpumask) {
10700 				irq_set_affinity_hint(irq->vector, NULL);
10701 				free_cpumask_var(irq->cpu_mask);
10702 				irq->have_cpumask = 0;
10703 			}
10704 			free_irq(irq->vector, bp->bnapi[i]);
10705 		}
10706 
10707 		irq->requested = 0;
10708 	}
10709 }
10710 
10711 static int bnxt_request_irq(struct bnxt *bp)
10712 {
10713 	int i, j, rc = 0;
10714 	unsigned long flags = 0;
10715 #ifdef CONFIG_RFS_ACCEL
10716 	struct cpu_rmap *rmap;
10717 #endif
10718 
10719 	rc = bnxt_setup_int_mode(bp);
10720 	if (rc) {
10721 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
10722 			   rc);
10723 		return rc;
10724 	}
10725 #ifdef CONFIG_RFS_ACCEL
10726 	rmap = bp->dev->rx_cpu_rmap;
10727 #endif
10728 	if (!(bp->flags & BNXT_FLAG_USING_MSIX))
10729 		flags = IRQF_SHARED;
10730 
10731 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
10732 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10733 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
10734 
10735 #ifdef CONFIG_RFS_ACCEL
10736 		if (rmap && bp->bnapi[i]->rx_ring) {
10737 			rc = irq_cpu_rmap_add(rmap, irq->vector);
10738 			if (rc)
10739 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
10740 					    j);
10741 			j++;
10742 		}
10743 #endif
10744 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
10745 				 bp->bnapi[i]);
10746 		if (rc)
10747 			break;
10748 
10749 		netif_napi_set_irq(&bp->bnapi[i]->napi, irq->vector);
10750 		irq->requested = 1;
10751 
10752 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
10753 			int numa_node = dev_to_node(&bp->pdev->dev);
10754 
10755 			irq->have_cpumask = 1;
10756 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
10757 					irq->cpu_mask);
10758 			rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
10759 			if (rc) {
10760 				netdev_warn(bp->dev,
10761 					    "Set affinity failed, IRQ = %d\n",
10762 					    irq->vector);
10763 				break;
10764 			}
10765 		}
10766 	}
10767 	return rc;
10768 }
10769 
10770 static void bnxt_del_napi(struct bnxt *bp)
10771 {
10772 	int i;
10773 
10774 	if (!bp->bnapi)
10775 		return;
10776 
10777 	for (i = 0; i < bp->rx_nr_rings; i++)
10778 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
10779 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
10780 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
10781 
10782 	for (i = 0; i < bp->cp_nr_rings; i++) {
10783 		struct bnxt_napi *bnapi = bp->bnapi[i];
10784 
10785 		__netif_napi_del(&bnapi->napi);
10786 	}
10787 	/* We called __netif_napi_del(), we need
10788 	 * to respect an RCU grace period before freeing napi structures.
10789 	 */
10790 	synchronize_net();
10791 }
10792 
10793 static void bnxt_init_napi(struct bnxt *bp)
10794 {
10795 	int i;
10796 	unsigned int cp_nr_rings = bp->cp_nr_rings;
10797 	struct bnxt_napi *bnapi;
10798 
10799 	if (bp->flags & BNXT_FLAG_USING_MSIX) {
10800 		int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
10801 
10802 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10803 			poll_fn = bnxt_poll_p5;
10804 		else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10805 			cp_nr_rings--;
10806 		for (i = 0; i < cp_nr_rings; i++) {
10807 			bnapi = bp->bnapi[i];
10808 			netif_napi_add(bp->dev, &bnapi->napi, poll_fn);
10809 		}
10810 		if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10811 			bnapi = bp->bnapi[cp_nr_rings];
10812 			netif_napi_add(bp->dev, &bnapi->napi,
10813 				       bnxt_poll_nitroa0);
10814 		}
10815 	} else {
10816 		bnapi = bp->bnapi[0];
10817 		netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll);
10818 	}
10819 }
10820 
10821 static void bnxt_disable_napi(struct bnxt *bp)
10822 {
10823 	int i;
10824 
10825 	if (!bp->bnapi ||
10826 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
10827 		return;
10828 
10829 	for (i = 0; i < bp->cp_nr_rings; i++) {
10830 		struct bnxt_napi *bnapi = bp->bnapi[i];
10831 		struct bnxt_cp_ring_info *cpr;
10832 
10833 		cpr = &bnapi->cp_ring;
10834 		if (bnapi->tx_fault)
10835 			cpr->sw_stats->tx.tx_resets++;
10836 		if (bnapi->in_reset)
10837 			cpr->sw_stats->rx.rx_resets++;
10838 		napi_disable(&bnapi->napi);
10839 		if (bnapi->rx_ring)
10840 			cancel_work_sync(&cpr->dim.work);
10841 	}
10842 }
10843 
10844 static void bnxt_enable_napi(struct bnxt *bp)
10845 {
10846 	int i;
10847 
10848 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
10849 	for (i = 0; i < bp->cp_nr_rings; i++) {
10850 		struct bnxt_napi *bnapi = bp->bnapi[i];
10851 		struct bnxt_cp_ring_info *cpr;
10852 
10853 		bnapi->tx_fault = 0;
10854 
10855 		cpr = &bnapi->cp_ring;
10856 		bnapi->in_reset = false;
10857 
10858 		if (bnapi->rx_ring) {
10859 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
10860 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
10861 		}
10862 		napi_enable(&bnapi->napi);
10863 	}
10864 }
10865 
10866 void bnxt_tx_disable(struct bnxt *bp)
10867 {
10868 	int i;
10869 	struct bnxt_tx_ring_info *txr;
10870 
10871 	if (bp->tx_ring) {
10872 		for (i = 0; i < bp->tx_nr_rings; i++) {
10873 			txr = &bp->tx_ring[i];
10874 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
10875 		}
10876 	}
10877 	/* Make sure napi polls see @dev_state change */
10878 	synchronize_net();
10879 	/* Drop carrier first to prevent TX timeout */
10880 	netif_carrier_off(bp->dev);
10881 	/* Stop all TX queues */
10882 	netif_tx_disable(bp->dev);
10883 }
10884 
10885 void bnxt_tx_enable(struct bnxt *bp)
10886 {
10887 	int i;
10888 	struct bnxt_tx_ring_info *txr;
10889 
10890 	for (i = 0; i < bp->tx_nr_rings; i++) {
10891 		txr = &bp->tx_ring[i];
10892 		WRITE_ONCE(txr->dev_state, 0);
10893 	}
10894 	/* Make sure napi polls see @dev_state change */
10895 	synchronize_net();
10896 	netif_tx_wake_all_queues(bp->dev);
10897 	if (BNXT_LINK_IS_UP(bp))
10898 		netif_carrier_on(bp->dev);
10899 }
10900 
10901 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
10902 {
10903 	u8 active_fec = link_info->active_fec_sig_mode &
10904 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
10905 
10906 	switch (active_fec) {
10907 	default:
10908 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
10909 		return "None";
10910 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
10911 		return "Clause 74 BaseR";
10912 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
10913 		return "Clause 91 RS(528,514)";
10914 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
10915 		return "Clause 91 RS544_1XN";
10916 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
10917 		return "Clause 91 RS(544,514)";
10918 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
10919 		return "Clause 91 RS272_1XN";
10920 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
10921 		return "Clause 91 RS(272,257)";
10922 	}
10923 }
10924 
10925 void bnxt_report_link(struct bnxt *bp)
10926 {
10927 	if (BNXT_LINK_IS_UP(bp)) {
10928 		const char *signal = "";
10929 		const char *flow_ctrl;
10930 		const char *duplex;
10931 		u32 speed;
10932 		u16 fec;
10933 
10934 		netif_carrier_on(bp->dev);
10935 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
10936 		if (speed == SPEED_UNKNOWN) {
10937 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
10938 			return;
10939 		}
10940 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
10941 			duplex = "full";
10942 		else
10943 			duplex = "half";
10944 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
10945 			flow_ctrl = "ON - receive & transmit";
10946 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
10947 			flow_ctrl = "ON - transmit";
10948 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
10949 			flow_ctrl = "ON - receive";
10950 		else
10951 			flow_ctrl = "none";
10952 		if (bp->link_info.phy_qcfg_resp.option_flags &
10953 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
10954 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
10955 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
10956 			switch (sig_mode) {
10957 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
10958 				signal = "(NRZ) ";
10959 				break;
10960 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
10961 				signal = "(PAM4 56Gbps) ";
10962 				break;
10963 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
10964 				signal = "(PAM4 112Gbps) ";
10965 				break;
10966 			default:
10967 				break;
10968 			}
10969 		}
10970 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
10971 			    speed, signal, duplex, flow_ctrl);
10972 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
10973 			netdev_info(bp->dev, "EEE is %s\n",
10974 				    bp->eee.eee_active ? "active" :
10975 							 "not active");
10976 		fec = bp->link_info.fec_cfg;
10977 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
10978 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
10979 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
10980 				    bnxt_report_fec(&bp->link_info));
10981 	} else {
10982 		netif_carrier_off(bp->dev);
10983 		netdev_err(bp->dev, "NIC Link is Down\n");
10984 	}
10985 }
10986 
10987 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
10988 {
10989 	if (!resp->supported_speeds_auto_mode &&
10990 	    !resp->supported_speeds_force_mode &&
10991 	    !resp->supported_pam4_speeds_auto_mode &&
10992 	    !resp->supported_pam4_speeds_force_mode &&
10993 	    !resp->supported_speeds2_auto_mode &&
10994 	    !resp->supported_speeds2_force_mode)
10995 		return true;
10996 	return false;
10997 }
10998 
10999 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
11000 {
11001 	struct bnxt_link_info *link_info = &bp->link_info;
11002 	struct hwrm_port_phy_qcaps_output *resp;
11003 	struct hwrm_port_phy_qcaps_input *req;
11004 	int rc = 0;
11005 
11006 	if (bp->hwrm_spec_code < 0x10201)
11007 		return 0;
11008 
11009 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
11010 	if (rc)
11011 		return rc;
11012 
11013 	resp = hwrm_req_hold(bp, req);
11014 	rc = hwrm_req_send(bp, req);
11015 	if (rc)
11016 		goto hwrm_phy_qcaps_exit;
11017 
11018 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
11019 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
11020 		struct ethtool_keee *eee = &bp->eee;
11021 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
11022 
11023 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
11024 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
11025 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11026 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11027 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11028 	}
11029 
11030 	if (bp->hwrm_spec_code >= 0x10a01) {
11031 		if (bnxt_phy_qcaps_no_speed(resp)) {
11032 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11033 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11034 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11035 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11036 			netdev_info(bp->dev, "Ethernet link enabled\n");
11037 			/* Phy re-enabled, reprobe the speeds */
11038 			link_info->support_auto_speeds = 0;
11039 			link_info->support_pam4_auto_speeds = 0;
11040 			link_info->support_auto_speeds2 = 0;
11041 		}
11042 	}
11043 	if (resp->supported_speeds_auto_mode)
11044 		link_info->support_auto_speeds =
11045 			le16_to_cpu(resp->supported_speeds_auto_mode);
11046 	if (resp->supported_pam4_speeds_auto_mode)
11047 		link_info->support_pam4_auto_speeds =
11048 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11049 	if (resp->supported_speeds2_auto_mode)
11050 		link_info->support_auto_speeds2 =
11051 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11052 
11053 	bp->port_count = resp->port_cnt;
11054 
11055 hwrm_phy_qcaps_exit:
11056 	hwrm_req_drop(bp, req);
11057 	return rc;
11058 }
11059 
11060 static bool bnxt_support_dropped(u16 advertising, u16 supported)
11061 {
11062 	u16 diff = advertising ^ supported;
11063 
11064 	return ((supported | diff) != supported);
11065 }
11066 
11067 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
11068 {
11069 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
11070 
11071 	/* Check if any advertised speeds are no longer supported. The caller
11072 	 * holds the link_lock mutex, so we can modify link_info settings.
11073 	 */
11074 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11075 		if (bnxt_support_dropped(link_info->advertising,
11076 					 link_info->support_auto_speeds2)) {
11077 			link_info->advertising = link_info->support_auto_speeds2;
11078 			return true;
11079 		}
11080 		return false;
11081 	}
11082 	if (bnxt_support_dropped(link_info->advertising,
11083 				 link_info->support_auto_speeds)) {
11084 		link_info->advertising = link_info->support_auto_speeds;
11085 		return true;
11086 	}
11087 	if (bnxt_support_dropped(link_info->advertising_pam4,
11088 				 link_info->support_pam4_auto_speeds)) {
11089 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
11090 		return true;
11091 	}
11092 	return false;
11093 }
11094 
11095 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
11096 {
11097 	struct bnxt_link_info *link_info = &bp->link_info;
11098 	struct hwrm_port_phy_qcfg_output *resp;
11099 	struct hwrm_port_phy_qcfg_input *req;
11100 	u8 link_state = link_info->link_state;
11101 	bool support_changed;
11102 	int rc;
11103 
11104 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
11105 	if (rc)
11106 		return rc;
11107 
11108 	resp = hwrm_req_hold(bp, req);
11109 	rc = hwrm_req_send(bp, req);
11110 	if (rc) {
11111 		hwrm_req_drop(bp, req);
11112 		if (BNXT_VF(bp) && rc == -ENODEV) {
11113 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
11114 			rc = 0;
11115 		}
11116 		return rc;
11117 	}
11118 
11119 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
11120 	link_info->phy_link_status = resp->link;
11121 	link_info->duplex = resp->duplex_cfg;
11122 	if (bp->hwrm_spec_code >= 0x10800)
11123 		link_info->duplex = resp->duplex_state;
11124 	link_info->pause = resp->pause;
11125 	link_info->auto_mode = resp->auto_mode;
11126 	link_info->auto_pause_setting = resp->auto_pause;
11127 	link_info->lp_pause = resp->link_partner_adv_pause;
11128 	link_info->force_pause_setting = resp->force_pause;
11129 	link_info->duplex_setting = resp->duplex_cfg;
11130 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
11131 		link_info->link_speed = le16_to_cpu(resp->link_speed);
11132 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
11133 			link_info->active_lanes = resp->active_lanes;
11134 	} else {
11135 		link_info->link_speed = 0;
11136 		link_info->active_lanes = 0;
11137 	}
11138 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
11139 	link_info->force_pam4_link_speed =
11140 		le16_to_cpu(resp->force_pam4_link_speed);
11141 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
11142 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
11143 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
11144 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
11145 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
11146 	link_info->auto_pam4_link_speeds =
11147 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
11148 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
11149 	link_info->lp_auto_link_speeds =
11150 		le16_to_cpu(resp->link_partner_adv_speeds);
11151 	link_info->lp_auto_pam4_link_speeds =
11152 		resp->link_partner_pam4_adv_speeds;
11153 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
11154 	link_info->phy_ver[0] = resp->phy_maj;
11155 	link_info->phy_ver[1] = resp->phy_min;
11156 	link_info->phy_ver[2] = resp->phy_bld;
11157 	link_info->media_type = resp->media_type;
11158 	link_info->phy_type = resp->phy_type;
11159 	link_info->transceiver = resp->xcvr_pkg_type;
11160 	link_info->phy_addr = resp->eee_config_phy_addr &
11161 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
11162 	link_info->module_status = resp->module_status;
11163 
11164 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
11165 		struct ethtool_keee *eee = &bp->eee;
11166 		u16 fw_speeds;
11167 
11168 		eee->eee_active = 0;
11169 		if (resp->eee_config_phy_addr &
11170 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
11171 			eee->eee_active = 1;
11172 			fw_speeds = le16_to_cpu(
11173 				resp->link_partner_adv_eee_link_speed_mask);
11174 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
11175 		}
11176 
11177 		/* Pull initial EEE config */
11178 		if (!chng_link_state) {
11179 			if (resp->eee_config_phy_addr &
11180 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
11181 				eee->eee_enabled = 1;
11182 
11183 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
11184 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
11185 
11186 			if (resp->eee_config_phy_addr &
11187 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
11188 				__le32 tmr;
11189 
11190 				eee->tx_lpi_enabled = 1;
11191 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
11192 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
11193 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
11194 			}
11195 		}
11196 	}
11197 
11198 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
11199 	if (bp->hwrm_spec_code >= 0x10504) {
11200 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
11201 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
11202 	}
11203 	/* TODO: need to add more logic to report VF link */
11204 	if (chng_link_state) {
11205 		if (link_info->phy_link_status == BNXT_LINK_LINK)
11206 			link_info->link_state = BNXT_LINK_STATE_UP;
11207 		else
11208 			link_info->link_state = BNXT_LINK_STATE_DOWN;
11209 		if (link_state != link_info->link_state)
11210 			bnxt_report_link(bp);
11211 	} else {
11212 		/* always link down if not require to update link state */
11213 		link_info->link_state = BNXT_LINK_STATE_DOWN;
11214 	}
11215 	hwrm_req_drop(bp, req);
11216 
11217 	if (!BNXT_PHY_CFG_ABLE(bp))
11218 		return 0;
11219 
11220 	support_changed = bnxt_support_speed_dropped(link_info);
11221 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
11222 		bnxt_hwrm_set_link_setting(bp, true, false);
11223 	return 0;
11224 }
11225 
11226 static void bnxt_get_port_module_status(struct bnxt *bp)
11227 {
11228 	struct bnxt_link_info *link_info = &bp->link_info;
11229 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
11230 	u8 module_status;
11231 
11232 	if (bnxt_update_link(bp, true))
11233 		return;
11234 
11235 	module_status = link_info->module_status;
11236 	switch (module_status) {
11237 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
11238 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
11239 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
11240 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
11241 			    bp->pf.port_id);
11242 		if (bp->hwrm_spec_code >= 0x10201) {
11243 			netdev_warn(bp->dev, "Module part number %s\n",
11244 				    resp->phy_vendor_partnumber);
11245 		}
11246 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
11247 			netdev_warn(bp->dev, "TX is disabled\n");
11248 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
11249 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
11250 	}
11251 }
11252 
11253 static void
11254 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11255 {
11256 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
11257 		if (bp->hwrm_spec_code >= 0x10201)
11258 			req->auto_pause =
11259 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
11260 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11261 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
11262 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11263 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
11264 		req->enables |=
11265 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11266 	} else {
11267 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11268 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
11269 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11270 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
11271 		req->enables |=
11272 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
11273 		if (bp->hwrm_spec_code >= 0x10201) {
11274 			req->auto_pause = req->force_pause;
11275 			req->enables |= cpu_to_le32(
11276 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11277 		}
11278 	}
11279 }
11280 
11281 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11282 {
11283 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
11284 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
11285 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11286 			req->enables |=
11287 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
11288 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
11289 		} else if (bp->link_info.advertising) {
11290 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
11291 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
11292 		}
11293 		if (bp->link_info.advertising_pam4) {
11294 			req->enables |=
11295 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
11296 			req->auto_link_pam4_speed_mask =
11297 				cpu_to_le16(bp->link_info.advertising_pam4);
11298 		}
11299 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
11300 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
11301 	} else {
11302 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
11303 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11304 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
11305 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
11306 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
11307 				   (u32)bp->link_info.req_link_speed);
11308 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
11309 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11310 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
11311 		} else {
11312 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11313 		}
11314 	}
11315 
11316 	/* tell chimp that the setting takes effect immediately */
11317 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
11318 }
11319 
11320 int bnxt_hwrm_set_pause(struct bnxt *bp)
11321 {
11322 	struct hwrm_port_phy_cfg_input *req;
11323 	int rc;
11324 
11325 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11326 	if (rc)
11327 		return rc;
11328 
11329 	bnxt_hwrm_set_pause_common(bp, req);
11330 
11331 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
11332 	    bp->link_info.force_link_chng)
11333 		bnxt_hwrm_set_link_common(bp, req);
11334 
11335 	rc = hwrm_req_send(bp, req);
11336 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
11337 		/* since changing of pause setting doesn't trigger any link
11338 		 * change event, the driver needs to update the current pause
11339 		 * result upon successfully return of the phy_cfg command
11340 		 */
11341 		bp->link_info.pause =
11342 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
11343 		bp->link_info.auto_pause_setting = 0;
11344 		if (!bp->link_info.force_link_chng)
11345 			bnxt_report_link(bp);
11346 	}
11347 	bp->link_info.force_link_chng = false;
11348 	return rc;
11349 }
11350 
11351 static void bnxt_hwrm_set_eee(struct bnxt *bp,
11352 			      struct hwrm_port_phy_cfg_input *req)
11353 {
11354 	struct ethtool_keee *eee = &bp->eee;
11355 
11356 	if (eee->eee_enabled) {
11357 		u16 eee_speeds;
11358 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
11359 
11360 		if (eee->tx_lpi_enabled)
11361 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
11362 		else
11363 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
11364 
11365 		req->flags |= cpu_to_le32(flags);
11366 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
11367 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
11368 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
11369 	} else {
11370 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
11371 	}
11372 }
11373 
11374 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
11375 {
11376 	struct hwrm_port_phy_cfg_input *req;
11377 	int rc;
11378 
11379 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11380 	if (rc)
11381 		return rc;
11382 
11383 	if (set_pause)
11384 		bnxt_hwrm_set_pause_common(bp, req);
11385 
11386 	bnxt_hwrm_set_link_common(bp, req);
11387 
11388 	if (set_eee)
11389 		bnxt_hwrm_set_eee(bp, req);
11390 	return hwrm_req_send(bp, req);
11391 }
11392 
11393 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
11394 {
11395 	struct hwrm_port_phy_cfg_input *req;
11396 	int rc;
11397 
11398 	if (!BNXT_SINGLE_PF(bp))
11399 		return 0;
11400 
11401 	if (pci_num_vf(bp->pdev) &&
11402 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
11403 		return 0;
11404 
11405 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11406 	if (rc)
11407 		return rc;
11408 
11409 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
11410 	rc = hwrm_req_send(bp, req);
11411 	if (!rc) {
11412 		mutex_lock(&bp->link_lock);
11413 		/* Device is not obliged link down in certain scenarios, even
11414 		 * when forced. Setting the state unknown is consistent with
11415 		 * driver startup and will force link state to be reported
11416 		 * during subsequent open based on PORT_PHY_QCFG.
11417 		 */
11418 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
11419 		mutex_unlock(&bp->link_lock);
11420 	}
11421 	return rc;
11422 }
11423 
11424 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
11425 {
11426 #ifdef CONFIG_TEE_BNXT_FW
11427 	int rc = tee_bnxt_fw_load();
11428 
11429 	if (rc)
11430 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
11431 
11432 	return rc;
11433 #else
11434 	netdev_err(bp->dev, "OP-TEE not supported\n");
11435 	return -ENODEV;
11436 #endif
11437 }
11438 
11439 static int bnxt_try_recover_fw(struct bnxt *bp)
11440 {
11441 	if (bp->fw_health && bp->fw_health->status_reliable) {
11442 		int retry = 0, rc;
11443 		u32 sts;
11444 
11445 		do {
11446 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
11447 			rc = bnxt_hwrm_poll(bp);
11448 			if (!BNXT_FW_IS_BOOTING(sts) &&
11449 			    !BNXT_FW_IS_RECOVERING(sts))
11450 				break;
11451 			retry++;
11452 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
11453 
11454 		if (!BNXT_FW_IS_HEALTHY(sts)) {
11455 			netdev_err(bp->dev,
11456 				   "Firmware not responding, status: 0x%x\n",
11457 				   sts);
11458 			rc = -ENODEV;
11459 		}
11460 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
11461 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
11462 			return bnxt_fw_reset_via_optee(bp);
11463 		}
11464 		return rc;
11465 	}
11466 
11467 	return -ENODEV;
11468 }
11469 
11470 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
11471 {
11472 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11473 
11474 	if (!BNXT_NEW_RM(bp))
11475 		return; /* no resource reservations required */
11476 
11477 	hw_resc->resv_cp_rings = 0;
11478 	hw_resc->resv_stat_ctxs = 0;
11479 	hw_resc->resv_irqs = 0;
11480 	hw_resc->resv_tx_rings = 0;
11481 	hw_resc->resv_rx_rings = 0;
11482 	hw_resc->resv_hw_ring_grps = 0;
11483 	hw_resc->resv_vnics = 0;
11484 	hw_resc->resv_rsscos_ctxs = 0;
11485 	if (!fw_reset) {
11486 		bp->tx_nr_rings = 0;
11487 		bp->rx_nr_rings = 0;
11488 	}
11489 }
11490 
11491 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
11492 {
11493 	int rc;
11494 
11495 	if (!BNXT_NEW_RM(bp))
11496 		return 0; /* no resource reservations required */
11497 
11498 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
11499 	if (rc)
11500 		netdev_err(bp->dev, "resc_qcaps failed\n");
11501 
11502 	bnxt_clear_reservations(bp, fw_reset);
11503 
11504 	return rc;
11505 }
11506 
11507 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
11508 {
11509 	struct hwrm_func_drv_if_change_output *resp;
11510 	struct hwrm_func_drv_if_change_input *req;
11511 	bool fw_reset = !bp->irq_tbl;
11512 	bool resc_reinit = false;
11513 	int rc, retry = 0;
11514 	u32 flags = 0;
11515 
11516 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
11517 		return 0;
11518 
11519 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
11520 	if (rc)
11521 		return rc;
11522 
11523 	if (up)
11524 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
11525 	resp = hwrm_req_hold(bp, req);
11526 
11527 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
11528 	while (retry < BNXT_FW_IF_RETRY) {
11529 		rc = hwrm_req_send(bp, req);
11530 		if (rc != -EAGAIN)
11531 			break;
11532 
11533 		msleep(50);
11534 		retry++;
11535 	}
11536 
11537 	if (rc == -EAGAIN) {
11538 		hwrm_req_drop(bp, req);
11539 		return rc;
11540 	} else if (!rc) {
11541 		flags = le32_to_cpu(resp->flags);
11542 	} else if (up) {
11543 		rc = bnxt_try_recover_fw(bp);
11544 		fw_reset = true;
11545 	}
11546 	hwrm_req_drop(bp, req);
11547 	if (rc)
11548 		return rc;
11549 
11550 	if (!up) {
11551 		bnxt_inv_fw_health_reg(bp);
11552 		return 0;
11553 	}
11554 
11555 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
11556 		resc_reinit = true;
11557 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
11558 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
11559 		fw_reset = true;
11560 	else
11561 		bnxt_remap_fw_health_regs(bp);
11562 
11563 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
11564 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
11565 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
11566 		return -ENODEV;
11567 	}
11568 	if (resc_reinit || fw_reset) {
11569 		if (fw_reset) {
11570 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11571 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11572 				bnxt_ulp_irq_stop(bp);
11573 			bnxt_free_ctx_mem(bp);
11574 			bnxt_dcb_free(bp);
11575 			rc = bnxt_fw_init_one(bp);
11576 			if (rc) {
11577 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11578 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
11579 				return rc;
11580 			}
11581 			bnxt_clear_int_mode(bp);
11582 			rc = bnxt_init_int_mode(bp);
11583 			if (rc) {
11584 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11585 				netdev_err(bp->dev, "init int mode failed\n");
11586 				return rc;
11587 			}
11588 		}
11589 		rc = bnxt_cancel_reservations(bp, fw_reset);
11590 	}
11591 	return rc;
11592 }
11593 
11594 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
11595 {
11596 	struct hwrm_port_led_qcaps_output *resp;
11597 	struct hwrm_port_led_qcaps_input *req;
11598 	struct bnxt_pf_info *pf = &bp->pf;
11599 	int rc;
11600 
11601 	bp->num_leds = 0;
11602 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
11603 		return 0;
11604 
11605 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
11606 	if (rc)
11607 		return rc;
11608 
11609 	req->port_id = cpu_to_le16(pf->port_id);
11610 	resp = hwrm_req_hold(bp, req);
11611 	rc = hwrm_req_send(bp, req);
11612 	if (rc) {
11613 		hwrm_req_drop(bp, req);
11614 		return rc;
11615 	}
11616 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
11617 		int i;
11618 
11619 		bp->num_leds = resp->num_leds;
11620 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
11621 						 bp->num_leds);
11622 		for (i = 0; i < bp->num_leds; i++) {
11623 			struct bnxt_led_info *led = &bp->leds[i];
11624 			__le16 caps = led->led_state_caps;
11625 
11626 			if (!led->led_group_id ||
11627 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
11628 				bp->num_leds = 0;
11629 				break;
11630 			}
11631 		}
11632 	}
11633 	hwrm_req_drop(bp, req);
11634 	return 0;
11635 }
11636 
11637 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
11638 {
11639 	struct hwrm_wol_filter_alloc_output *resp;
11640 	struct hwrm_wol_filter_alloc_input *req;
11641 	int rc;
11642 
11643 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
11644 	if (rc)
11645 		return rc;
11646 
11647 	req->port_id = cpu_to_le16(bp->pf.port_id);
11648 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
11649 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
11650 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
11651 
11652 	resp = hwrm_req_hold(bp, req);
11653 	rc = hwrm_req_send(bp, req);
11654 	if (!rc)
11655 		bp->wol_filter_id = resp->wol_filter_id;
11656 	hwrm_req_drop(bp, req);
11657 	return rc;
11658 }
11659 
11660 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
11661 {
11662 	struct hwrm_wol_filter_free_input *req;
11663 	int rc;
11664 
11665 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
11666 	if (rc)
11667 		return rc;
11668 
11669 	req->port_id = cpu_to_le16(bp->pf.port_id);
11670 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
11671 	req->wol_filter_id = bp->wol_filter_id;
11672 
11673 	return hwrm_req_send(bp, req);
11674 }
11675 
11676 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
11677 {
11678 	struct hwrm_wol_filter_qcfg_output *resp;
11679 	struct hwrm_wol_filter_qcfg_input *req;
11680 	u16 next_handle = 0;
11681 	int rc;
11682 
11683 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
11684 	if (rc)
11685 		return rc;
11686 
11687 	req->port_id = cpu_to_le16(bp->pf.port_id);
11688 	req->handle = cpu_to_le16(handle);
11689 	resp = hwrm_req_hold(bp, req);
11690 	rc = hwrm_req_send(bp, req);
11691 	if (!rc) {
11692 		next_handle = le16_to_cpu(resp->next_handle);
11693 		if (next_handle != 0) {
11694 			if (resp->wol_type ==
11695 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
11696 				bp->wol = 1;
11697 				bp->wol_filter_id = resp->wol_filter_id;
11698 			}
11699 		}
11700 	}
11701 	hwrm_req_drop(bp, req);
11702 	return next_handle;
11703 }
11704 
11705 static void bnxt_get_wol_settings(struct bnxt *bp)
11706 {
11707 	u16 handle = 0;
11708 
11709 	bp->wol = 0;
11710 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
11711 		return;
11712 
11713 	do {
11714 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
11715 	} while (handle && handle != 0xffff);
11716 }
11717 
11718 static bool bnxt_eee_config_ok(struct bnxt *bp)
11719 {
11720 	struct ethtool_keee *eee = &bp->eee;
11721 	struct bnxt_link_info *link_info = &bp->link_info;
11722 
11723 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
11724 		return true;
11725 
11726 	if (eee->eee_enabled) {
11727 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
11728 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
11729 
11730 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
11731 
11732 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
11733 			eee->eee_enabled = 0;
11734 			return false;
11735 		}
11736 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
11737 			linkmode_and(eee->advertised, advertising,
11738 				     eee->supported);
11739 			return false;
11740 		}
11741 	}
11742 	return true;
11743 }
11744 
11745 static int bnxt_update_phy_setting(struct bnxt *bp)
11746 {
11747 	int rc;
11748 	bool update_link = false;
11749 	bool update_pause = false;
11750 	bool update_eee = false;
11751 	struct bnxt_link_info *link_info = &bp->link_info;
11752 
11753 	rc = bnxt_update_link(bp, true);
11754 	if (rc) {
11755 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
11756 			   rc);
11757 		return rc;
11758 	}
11759 	if (!BNXT_SINGLE_PF(bp))
11760 		return 0;
11761 
11762 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
11763 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
11764 	    link_info->req_flow_ctrl)
11765 		update_pause = true;
11766 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
11767 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
11768 		update_pause = true;
11769 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
11770 		if (BNXT_AUTO_MODE(link_info->auto_mode))
11771 			update_link = true;
11772 		if (bnxt_force_speed_updated(link_info))
11773 			update_link = true;
11774 		if (link_info->req_duplex != link_info->duplex_setting)
11775 			update_link = true;
11776 	} else {
11777 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
11778 			update_link = true;
11779 		if (bnxt_auto_speed_updated(link_info))
11780 			update_link = true;
11781 	}
11782 
11783 	/* The last close may have shutdown the link, so need to call
11784 	 * PHY_CFG to bring it back up.
11785 	 */
11786 	if (!BNXT_LINK_IS_UP(bp))
11787 		update_link = true;
11788 
11789 	if (!bnxt_eee_config_ok(bp))
11790 		update_eee = true;
11791 
11792 	if (update_link)
11793 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
11794 	else if (update_pause)
11795 		rc = bnxt_hwrm_set_pause(bp);
11796 	if (rc) {
11797 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
11798 			   rc);
11799 		return rc;
11800 	}
11801 
11802 	return rc;
11803 }
11804 
11805 /* Common routine to pre-map certain register block to different GRC window.
11806  * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
11807  * in PF and 3 windows in VF that can be customized to map in different
11808  * register blocks.
11809  */
11810 static void bnxt_preset_reg_win(struct bnxt *bp)
11811 {
11812 	if (BNXT_PF(bp)) {
11813 		/* CAG registers map to GRC window #4 */
11814 		writel(BNXT_CAG_REG_BASE,
11815 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
11816 	}
11817 }
11818 
11819 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
11820 
11821 static int bnxt_reinit_after_abort(struct bnxt *bp)
11822 {
11823 	int rc;
11824 
11825 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11826 		return -EBUSY;
11827 
11828 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
11829 		return -ENODEV;
11830 
11831 	rc = bnxt_fw_init_one(bp);
11832 	if (!rc) {
11833 		bnxt_clear_int_mode(bp);
11834 		rc = bnxt_init_int_mode(bp);
11835 		if (!rc) {
11836 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
11837 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11838 		}
11839 	}
11840 	return rc;
11841 }
11842 
11843 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
11844 {
11845 	struct bnxt_ntuple_filter *ntp_fltr;
11846 	struct bnxt_l2_filter *l2_fltr;
11847 
11848 	if (list_empty(&fltr->list))
11849 		return;
11850 
11851 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
11852 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
11853 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
11854 		atomic_inc(&l2_fltr->refcnt);
11855 		ntp_fltr->l2_fltr = l2_fltr;
11856 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
11857 			bnxt_del_ntp_filter(bp, ntp_fltr);
11858 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
11859 				   fltr->sw_id);
11860 		}
11861 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
11862 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
11863 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
11864 			bnxt_del_l2_filter(bp, l2_fltr);
11865 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
11866 				   fltr->sw_id);
11867 		}
11868 	}
11869 }
11870 
11871 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
11872 {
11873 	struct bnxt_filter_base *usr_fltr, *tmp;
11874 
11875 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
11876 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
11877 }
11878 
11879 static int bnxt_set_xps_mapping(struct bnxt *bp)
11880 {
11881 	int numa_node = dev_to_node(&bp->pdev->dev);
11882 	unsigned int q_idx, map_idx, cpu, i;
11883 	const struct cpumask *cpu_mask_ptr;
11884 	int nr_cpus = num_online_cpus();
11885 	cpumask_t *q_map;
11886 	int rc = 0;
11887 
11888 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
11889 	if (!q_map)
11890 		return -ENOMEM;
11891 
11892 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
11893 	 * Each TC has the same number of TX queues. The nth TX queue for each
11894 	 * TC will have the same CPU mask.
11895 	 */
11896 	for (i = 0; i < nr_cpus; i++) {
11897 		map_idx = i % bp->tx_nr_rings_per_tc;
11898 		cpu = cpumask_local_spread(i, numa_node);
11899 		cpu_mask_ptr = get_cpu_mask(cpu);
11900 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
11901 	}
11902 
11903 	/* Register CPU mask for each TX queue except the ones marked for XDP */
11904 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
11905 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
11906 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
11907 		if (rc) {
11908 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
11909 				    q_idx);
11910 			break;
11911 		}
11912 	}
11913 
11914 	kfree(q_map);
11915 
11916 	return rc;
11917 }
11918 
11919 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
11920 {
11921 	int rc = 0;
11922 
11923 	bnxt_preset_reg_win(bp);
11924 	netif_carrier_off(bp->dev);
11925 	if (irq_re_init) {
11926 		/* Reserve rings now if none were reserved at driver probe. */
11927 		rc = bnxt_init_dflt_ring_mode(bp);
11928 		if (rc) {
11929 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
11930 			return rc;
11931 		}
11932 	}
11933 	rc = bnxt_reserve_rings(bp, irq_re_init);
11934 	if (rc)
11935 		return rc;
11936 	if ((bp->flags & BNXT_FLAG_RFS) &&
11937 	    !(bp->flags & BNXT_FLAG_USING_MSIX)) {
11938 		/* disable RFS if falling back to INTA */
11939 		bp->dev->hw_features &= ~NETIF_F_NTUPLE;
11940 		bp->flags &= ~BNXT_FLAG_RFS;
11941 	}
11942 
11943 	rc = bnxt_alloc_mem(bp, irq_re_init);
11944 	if (rc) {
11945 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
11946 		goto open_err_free_mem;
11947 	}
11948 
11949 	if (irq_re_init) {
11950 		bnxt_init_napi(bp);
11951 		rc = bnxt_request_irq(bp);
11952 		if (rc) {
11953 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
11954 			goto open_err_irq;
11955 		}
11956 	}
11957 
11958 	rc = bnxt_init_nic(bp, irq_re_init);
11959 	if (rc) {
11960 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
11961 		goto open_err_irq;
11962 	}
11963 
11964 	bnxt_enable_napi(bp);
11965 	bnxt_debug_dev_init(bp);
11966 
11967 	if (link_re_init) {
11968 		mutex_lock(&bp->link_lock);
11969 		rc = bnxt_update_phy_setting(bp);
11970 		mutex_unlock(&bp->link_lock);
11971 		if (rc) {
11972 			netdev_warn(bp->dev, "failed to update phy settings\n");
11973 			if (BNXT_SINGLE_PF(bp)) {
11974 				bp->link_info.phy_retry = true;
11975 				bp->link_info.phy_retry_expires =
11976 					jiffies + 5 * HZ;
11977 			}
11978 		}
11979 	}
11980 
11981 	if (irq_re_init) {
11982 		udp_tunnel_nic_reset_ntf(bp->dev);
11983 		rc = bnxt_set_xps_mapping(bp);
11984 		if (rc)
11985 			netdev_warn(bp->dev, "failed to set xps mapping\n");
11986 	}
11987 
11988 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
11989 		if (!static_key_enabled(&bnxt_xdp_locking_key))
11990 			static_branch_enable(&bnxt_xdp_locking_key);
11991 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
11992 		static_branch_disable(&bnxt_xdp_locking_key);
11993 	}
11994 	set_bit(BNXT_STATE_OPEN, &bp->state);
11995 	bnxt_enable_int(bp);
11996 	/* Enable TX queues */
11997 	bnxt_tx_enable(bp);
11998 	mod_timer(&bp->timer, jiffies + bp->current_interval);
11999 	/* Poll link status and check for SFP+ module status */
12000 	mutex_lock(&bp->link_lock);
12001 	bnxt_get_port_module_status(bp);
12002 	mutex_unlock(&bp->link_lock);
12003 
12004 	/* VF-reps may need to be re-opened after the PF is re-opened */
12005 	if (BNXT_PF(bp))
12006 		bnxt_vf_reps_open(bp);
12007 	if (bp->ptp_cfg)
12008 		atomic_set(&bp->ptp_cfg->tx_avail, BNXT_MAX_TX_TS);
12009 	bnxt_ptp_init_rtc(bp, true);
12010 	bnxt_ptp_cfg_tstamp_filters(bp);
12011 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12012 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
12013 	bnxt_cfg_usr_fltrs(bp);
12014 	return 0;
12015 
12016 open_err_irq:
12017 	bnxt_del_napi(bp);
12018 
12019 open_err_free_mem:
12020 	bnxt_free_skbs(bp);
12021 	bnxt_free_irq(bp);
12022 	bnxt_free_mem(bp, true);
12023 	return rc;
12024 }
12025 
12026 /* rtnl_lock held */
12027 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12028 {
12029 	int rc = 0;
12030 
12031 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12032 		rc = -EIO;
12033 	if (!rc)
12034 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12035 	if (rc) {
12036 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12037 		dev_close(bp->dev);
12038 	}
12039 	return rc;
12040 }
12041 
12042 /* rtnl_lock held, open the NIC half way by allocating all resources, but
12043  * NAPI, IRQ, and TX are not enabled.  This is mainly used for offline
12044  * self tests.
12045  */
12046 int bnxt_half_open_nic(struct bnxt *bp)
12047 {
12048 	int rc = 0;
12049 
12050 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12051 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
12052 		rc = -ENODEV;
12053 		goto half_open_err;
12054 	}
12055 
12056 	rc = bnxt_alloc_mem(bp, true);
12057 	if (rc) {
12058 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12059 		goto half_open_err;
12060 	}
12061 	bnxt_init_napi(bp);
12062 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12063 	rc = bnxt_init_nic(bp, true);
12064 	if (rc) {
12065 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12066 		bnxt_del_napi(bp);
12067 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12068 		goto half_open_err;
12069 	}
12070 	return 0;
12071 
12072 half_open_err:
12073 	bnxt_free_skbs(bp);
12074 	bnxt_free_mem(bp, true);
12075 	dev_close(bp->dev);
12076 	return rc;
12077 }
12078 
12079 /* rtnl_lock held, this call can only be made after a previous successful
12080  * call to bnxt_half_open_nic().
12081  */
12082 void bnxt_half_close_nic(struct bnxt *bp)
12083 {
12084 	bnxt_hwrm_resource_free(bp, false, true);
12085 	bnxt_del_napi(bp);
12086 	bnxt_free_skbs(bp);
12087 	bnxt_free_mem(bp, true);
12088 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12089 }
12090 
12091 void bnxt_reenable_sriov(struct bnxt *bp)
12092 {
12093 	if (BNXT_PF(bp)) {
12094 		struct bnxt_pf_info *pf = &bp->pf;
12095 		int n = pf->active_vfs;
12096 
12097 		if (n)
12098 			bnxt_cfg_hw_sriov(bp, &n, true);
12099 	}
12100 }
12101 
12102 static int bnxt_open(struct net_device *dev)
12103 {
12104 	struct bnxt *bp = netdev_priv(dev);
12105 	int rc;
12106 
12107 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12108 		rc = bnxt_reinit_after_abort(bp);
12109 		if (rc) {
12110 			if (rc == -EBUSY)
12111 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
12112 			else
12113 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
12114 			return -ENODEV;
12115 		}
12116 	}
12117 
12118 	rc = bnxt_hwrm_if_change(bp, true);
12119 	if (rc)
12120 		return rc;
12121 
12122 	rc = __bnxt_open_nic(bp, true, true);
12123 	if (rc) {
12124 		bnxt_hwrm_if_change(bp, false);
12125 	} else {
12126 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
12127 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12128 				bnxt_queue_sp_work(bp,
12129 						   BNXT_RESTART_ULP_SP_EVENT);
12130 		}
12131 	}
12132 
12133 	return rc;
12134 }
12135 
12136 static bool bnxt_drv_busy(struct bnxt *bp)
12137 {
12138 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
12139 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
12140 }
12141 
12142 static void bnxt_get_ring_stats(struct bnxt *bp,
12143 				struct rtnl_link_stats64 *stats);
12144 
12145 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
12146 			     bool link_re_init)
12147 {
12148 	/* Close the VF-reps before closing PF */
12149 	if (BNXT_PF(bp))
12150 		bnxt_vf_reps_close(bp);
12151 
12152 	/* Change device state to avoid TX queue wake up's */
12153 	bnxt_tx_disable(bp);
12154 
12155 	clear_bit(BNXT_STATE_OPEN, &bp->state);
12156 	smp_mb__after_atomic();
12157 	while (bnxt_drv_busy(bp))
12158 		msleep(20);
12159 
12160 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12161 		bnxt_clear_rss_ctxs(bp, false);
12162 	/* Flush rings and disable interrupts */
12163 	bnxt_shutdown_nic(bp, irq_re_init);
12164 
12165 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
12166 
12167 	bnxt_debug_dev_exit(bp);
12168 	bnxt_disable_napi(bp);
12169 	del_timer_sync(&bp->timer);
12170 	bnxt_free_skbs(bp);
12171 
12172 	/* Save ring stats before shutdown */
12173 	if (bp->bnapi && irq_re_init) {
12174 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
12175 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
12176 	}
12177 	if (irq_re_init) {
12178 		bnxt_free_irq(bp);
12179 		bnxt_del_napi(bp);
12180 	}
12181 	bnxt_free_mem(bp, irq_re_init);
12182 }
12183 
12184 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12185 {
12186 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12187 		/* If we get here, it means firmware reset is in progress
12188 		 * while we are trying to close.  We can safely proceed with
12189 		 * the close because we are holding rtnl_lock().  Some firmware
12190 		 * messages may fail as we proceed to close.  We set the
12191 		 * ABORT_ERR flag here so that the FW reset thread will later
12192 		 * abort when it gets the rtnl_lock() and sees the flag.
12193 		 */
12194 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
12195 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12196 	}
12197 
12198 #ifdef CONFIG_BNXT_SRIOV
12199 	if (bp->sriov_cfg) {
12200 		int rc;
12201 
12202 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
12203 						      !bp->sriov_cfg,
12204 						      BNXT_SRIOV_CFG_WAIT_TMO);
12205 		if (!rc)
12206 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
12207 		else if (rc < 0)
12208 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
12209 	}
12210 #endif
12211 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
12212 }
12213 
12214 static int bnxt_close(struct net_device *dev)
12215 {
12216 	struct bnxt *bp = netdev_priv(dev);
12217 
12218 	bnxt_close_nic(bp, true, true);
12219 	bnxt_hwrm_shutdown_link(bp);
12220 	bnxt_hwrm_if_change(bp, false);
12221 	return 0;
12222 }
12223 
12224 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
12225 				   u16 *val)
12226 {
12227 	struct hwrm_port_phy_mdio_read_output *resp;
12228 	struct hwrm_port_phy_mdio_read_input *req;
12229 	int rc;
12230 
12231 	if (bp->hwrm_spec_code < 0x10a00)
12232 		return -EOPNOTSUPP;
12233 
12234 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
12235 	if (rc)
12236 		return rc;
12237 
12238 	req->port_id = cpu_to_le16(bp->pf.port_id);
12239 	req->phy_addr = phy_addr;
12240 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12241 	if (mdio_phy_id_is_c45(phy_addr)) {
12242 		req->cl45_mdio = 1;
12243 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12244 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12245 		req->reg_addr = cpu_to_le16(reg);
12246 	}
12247 
12248 	resp = hwrm_req_hold(bp, req);
12249 	rc = hwrm_req_send(bp, req);
12250 	if (!rc)
12251 		*val = le16_to_cpu(resp->reg_data);
12252 	hwrm_req_drop(bp, req);
12253 	return rc;
12254 }
12255 
12256 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
12257 				    u16 val)
12258 {
12259 	struct hwrm_port_phy_mdio_write_input *req;
12260 	int rc;
12261 
12262 	if (bp->hwrm_spec_code < 0x10a00)
12263 		return -EOPNOTSUPP;
12264 
12265 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
12266 	if (rc)
12267 		return rc;
12268 
12269 	req->port_id = cpu_to_le16(bp->pf.port_id);
12270 	req->phy_addr = phy_addr;
12271 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12272 	if (mdio_phy_id_is_c45(phy_addr)) {
12273 		req->cl45_mdio = 1;
12274 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12275 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12276 		req->reg_addr = cpu_to_le16(reg);
12277 	}
12278 	req->reg_data = cpu_to_le16(val);
12279 
12280 	return hwrm_req_send(bp, req);
12281 }
12282 
12283 /* rtnl_lock held */
12284 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12285 {
12286 	struct mii_ioctl_data *mdio = if_mii(ifr);
12287 	struct bnxt *bp = netdev_priv(dev);
12288 	int rc;
12289 
12290 	switch (cmd) {
12291 	case SIOCGMIIPHY:
12292 		mdio->phy_id = bp->link_info.phy_addr;
12293 
12294 		fallthrough;
12295 	case SIOCGMIIREG: {
12296 		u16 mii_regval = 0;
12297 
12298 		if (!netif_running(dev))
12299 			return -EAGAIN;
12300 
12301 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
12302 					     &mii_regval);
12303 		mdio->val_out = mii_regval;
12304 		return rc;
12305 	}
12306 
12307 	case SIOCSMIIREG:
12308 		if (!netif_running(dev))
12309 			return -EAGAIN;
12310 
12311 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
12312 						mdio->val_in);
12313 
12314 	case SIOCSHWTSTAMP:
12315 		return bnxt_hwtstamp_set(dev, ifr);
12316 
12317 	case SIOCGHWTSTAMP:
12318 		return bnxt_hwtstamp_get(dev, ifr);
12319 
12320 	default:
12321 		/* do nothing */
12322 		break;
12323 	}
12324 	return -EOPNOTSUPP;
12325 }
12326 
12327 static void bnxt_get_ring_stats(struct bnxt *bp,
12328 				struct rtnl_link_stats64 *stats)
12329 {
12330 	int i;
12331 
12332 	for (i = 0; i < bp->cp_nr_rings; i++) {
12333 		struct bnxt_napi *bnapi = bp->bnapi[i];
12334 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
12335 		u64 *sw = cpr->stats.sw_stats;
12336 
12337 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
12338 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12339 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
12340 
12341 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
12342 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
12343 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
12344 
12345 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
12346 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
12347 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
12348 
12349 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
12350 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
12351 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
12352 
12353 		stats->rx_missed_errors +=
12354 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
12355 
12356 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12357 
12358 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
12359 
12360 		stats->rx_dropped +=
12361 			cpr->sw_stats->rx.rx_netpoll_discards +
12362 			cpr->sw_stats->rx.rx_oom_discards;
12363 	}
12364 }
12365 
12366 static void bnxt_add_prev_stats(struct bnxt *bp,
12367 				struct rtnl_link_stats64 *stats)
12368 {
12369 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
12370 
12371 	stats->rx_packets += prev_stats->rx_packets;
12372 	stats->tx_packets += prev_stats->tx_packets;
12373 	stats->rx_bytes += prev_stats->rx_bytes;
12374 	stats->tx_bytes += prev_stats->tx_bytes;
12375 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
12376 	stats->multicast += prev_stats->multicast;
12377 	stats->rx_dropped += prev_stats->rx_dropped;
12378 	stats->tx_dropped += prev_stats->tx_dropped;
12379 }
12380 
12381 static void
12382 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
12383 {
12384 	struct bnxt *bp = netdev_priv(dev);
12385 
12386 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
12387 	/* Make sure bnxt_close_nic() sees that we are reading stats before
12388 	 * we check the BNXT_STATE_OPEN flag.
12389 	 */
12390 	smp_mb__after_atomic();
12391 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12392 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12393 		*stats = bp->net_stats_prev;
12394 		return;
12395 	}
12396 
12397 	bnxt_get_ring_stats(bp, stats);
12398 	bnxt_add_prev_stats(bp, stats);
12399 
12400 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
12401 		u64 *rx = bp->port_stats.sw_stats;
12402 		u64 *tx = bp->port_stats.sw_stats +
12403 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
12404 
12405 		stats->rx_crc_errors =
12406 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
12407 		stats->rx_frame_errors =
12408 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
12409 		stats->rx_length_errors =
12410 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
12411 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
12412 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
12413 		stats->rx_errors =
12414 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
12415 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
12416 		stats->collisions =
12417 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
12418 		stats->tx_fifo_errors =
12419 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
12420 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
12421 	}
12422 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12423 }
12424 
12425 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
12426 					struct bnxt_total_ring_err_stats *stats,
12427 					struct bnxt_cp_ring_info *cpr)
12428 {
12429 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
12430 	u64 *hw_stats = cpr->stats.sw_stats;
12431 
12432 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
12433 	stats->rx_total_resets += sw_stats->rx.rx_resets;
12434 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
12435 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
12436 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
12437 	stats->rx_total_ring_discards +=
12438 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
12439 	stats->tx_total_resets += sw_stats->tx.tx_resets;
12440 	stats->tx_total_ring_discards +=
12441 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
12442 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
12443 }
12444 
12445 void bnxt_get_ring_err_stats(struct bnxt *bp,
12446 			     struct bnxt_total_ring_err_stats *stats)
12447 {
12448 	int i;
12449 
12450 	for (i = 0; i < bp->cp_nr_rings; i++)
12451 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
12452 }
12453 
12454 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
12455 {
12456 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12457 	struct net_device *dev = bp->dev;
12458 	struct netdev_hw_addr *ha;
12459 	u8 *haddr;
12460 	int mc_count = 0;
12461 	bool update = false;
12462 	int off = 0;
12463 
12464 	netdev_for_each_mc_addr(ha, dev) {
12465 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
12466 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12467 			vnic->mc_list_count = 0;
12468 			return false;
12469 		}
12470 		haddr = ha->addr;
12471 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
12472 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
12473 			update = true;
12474 		}
12475 		off += ETH_ALEN;
12476 		mc_count++;
12477 	}
12478 	if (mc_count)
12479 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
12480 
12481 	if (mc_count != vnic->mc_list_count) {
12482 		vnic->mc_list_count = mc_count;
12483 		update = true;
12484 	}
12485 	return update;
12486 }
12487 
12488 static bool bnxt_uc_list_updated(struct bnxt *bp)
12489 {
12490 	struct net_device *dev = bp->dev;
12491 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12492 	struct netdev_hw_addr *ha;
12493 	int off = 0;
12494 
12495 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
12496 		return true;
12497 
12498 	netdev_for_each_uc_addr(ha, dev) {
12499 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
12500 			return true;
12501 
12502 		off += ETH_ALEN;
12503 	}
12504 	return false;
12505 }
12506 
12507 static void bnxt_set_rx_mode(struct net_device *dev)
12508 {
12509 	struct bnxt *bp = netdev_priv(dev);
12510 	struct bnxt_vnic_info *vnic;
12511 	bool mc_update = false;
12512 	bool uc_update;
12513 	u32 mask;
12514 
12515 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
12516 		return;
12517 
12518 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12519 	mask = vnic->rx_mask;
12520 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
12521 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
12522 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
12523 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
12524 
12525 	if (dev->flags & IFF_PROMISC)
12526 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12527 
12528 	uc_update = bnxt_uc_list_updated(bp);
12529 
12530 	if (dev->flags & IFF_BROADCAST)
12531 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
12532 	if (dev->flags & IFF_ALLMULTI) {
12533 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12534 		vnic->mc_list_count = 0;
12535 	} else if (dev->flags & IFF_MULTICAST) {
12536 		mc_update = bnxt_mc_list_updated(bp, &mask);
12537 	}
12538 
12539 	if (mask != vnic->rx_mask || uc_update || mc_update) {
12540 		vnic->rx_mask = mask;
12541 
12542 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
12543 	}
12544 }
12545 
12546 static int bnxt_cfg_rx_mode(struct bnxt *bp)
12547 {
12548 	struct net_device *dev = bp->dev;
12549 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12550 	struct netdev_hw_addr *ha;
12551 	int i, off = 0, rc;
12552 	bool uc_update;
12553 
12554 	netif_addr_lock_bh(dev);
12555 	uc_update = bnxt_uc_list_updated(bp);
12556 	netif_addr_unlock_bh(dev);
12557 
12558 	if (!uc_update)
12559 		goto skip_uc;
12560 
12561 	for (i = 1; i < vnic->uc_filter_count; i++) {
12562 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
12563 
12564 		bnxt_hwrm_l2_filter_free(bp, fltr);
12565 		bnxt_del_l2_filter(bp, fltr);
12566 	}
12567 
12568 	vnic->uc_filter_count = 1;
12569 
12570 	netif_addr_lock_bh(dev);
12571 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
12572 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12573 	} else {
12574 		netdev_for_each_uc_addr(ha, dev) {
12575 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
12576 			off += ETH_ALEN;
12577 			vnic->uc_filter_count++;
12578 		}
12579 	}
12580 	netif_addr_unlock_bh(dev);
12581 
12582 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
12583 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
12584 		if (rc) {
12585 			if (BNXT_VF(bp) && rc == -ENODEV) {
12586 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
12587 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
12588 				else
12589 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
12590 				rc = 0;
12591 			} else {
12592 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
12593 			}
12594 			vnic->uc_filter_count = i;
12595 			return rc;
12596 		}
12597 	}
12598 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
12599 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
12600 
12601 skip_uc:
12602 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
12603 	    !bnxt_promisc_ok(bp))
12604 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12605 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
12606 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
12607 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
12608 			    rc);
12609 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
12610 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12611 		vnic->mc_list_count = 0;
12612 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
12613 	}
12614 	if (rc)
12615 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
12616 			   rc);
12617 
12618 	return rc;
12619 }
12620 
12621 static bool bnxt_can_reserve_rings(struct bnxt *bp)
12622 {
12623 #ifdef CONFIG_BNXT_SRIOV
12624 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
12625 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12626 
12627 		/* No minimum rings were provisioned by the PF.  Don't
12628 		 * reserve rings by default when device is down.
12629 		 */
12630 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
12631 			return true;
12632 
12633 		if (!netif_running(bp->dev))
12634 			return false;
12635 	}
12636 #endif
12637 	return true;
12638 }
12639 
12640 /* If the chip and firmware supports RFS */
12641 static bool bnxt_rfs_supported(struct bnxt *bp)
12642 {
12643 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
12644 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
12645 			return true;
12646 		return false;
12647 	}
12648 	/* 212 firmware is broken for aRFS */
12649 	if (BNXT_FW_MAJ(bp) == 212)
12650 		return false;
12651 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
12652 		return true;
12653 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
12654 		return true;
12655 	return false;
12656 }
12657 
12658 /* If runtime conditions support RFS */
12659 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
12660 {
12661 	struct bnxt_hw_rings hwr = {0};
12662 	int max_vnics, max_rss_ctxs;
12663 
12664 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
12665 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
12666 		return bnxt_rfs_supported(bp);
12667 
12668 	if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
12669 		return false;
12670 
12671 	hwr.grp = bp->rx_nr_rings;
12672 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
12673 	if (new_rss_ctx)
12674 		hwr.vnic++;
12675 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
12676 	max_vnics = bnxt_get_max_func_vnics(bp);
12677 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
12678 
12679 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
12680 		if (bp->rx_nr_rings > 1)
12681 			netdev_warn(bp->dev,
12682 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
12683 				    min(max_rss_ctxs - 1, max_vnics - 1));
12684 		return false;
12685 	}
12686 
12687 	if (!BNXT_NEW_RM(bp))
12688 		return true;
12689 
12690 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
12691 	 * issue that will mess up the default VNIC if we reduce the
12692 	 * reservations.
12693 	 */
12694 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
12695 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
12696 		return true;
12697 
12698 	bnxt_hwrm_reserve_rings(bp, &hwr);
12699 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
12700 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
12701 		return true;
12702 
12703 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
12704 	hwr.vnic = 1;
12705 	hwr.rss_ctx = 0;
12706 	bnxt_hwrm_reserve_rings(bp, &hwr);
12707 	return false;
12708 }
12709 
12710 static netdev_features_t bnxt_fix_features(struct net_device *dev,
12711 					   netdev_features_t features)
12712 {
12713 	struct bnxt *bp = netdev_priv(dev);
12714 	netdev_features_t vlan_features;
12715 
12716 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
12717 		features &= ~NETIF_F_NTUPLE;
12718 
12719 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
12720 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
12721 
12722 	if (!(features & NETIF_F_GRO))
12723 		features &= ~NETIF_F_GRO_HW;
12724 
12725 	if (features & NETIF_F_GRO_HW)
12726 		features &= ~NETIF_F_LRO;
12727 
12728 	/* Both CTAG and STAG VLAN accelaration on the RX side have to be
12729 	 * turned on or off together.
12730 	 */
12731 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
12732 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
12733 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
12734 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
12735 		else if (vlan_features)
12736 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
12737 	}
12738 #ifdef CONFIG_BNXT_SRIOV
12739 	if (BNXT_VF(bp) && bp->vf.vlan)
12740 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
12741 #endif
12742 	return features;
12743 }
12744 
12745 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
12746 				bool link_re_init, u32 flags, bool update_tpa)
12747 {
12748 	bnxt_close_nic(bp, irq_re_init, link_re_init);
12749 	bp->flags = flags;
12750 	if (update_tpa)
12751 		bnxt_set_ring_params(bp);
12752 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
12753 }
12754 
12755 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
12756 {
12757 	bool update_tpa = false, update_ntuple = false;
12758 	struct bnxt *bp = netdev_priv(dev);
12759 	u32 flags = bp->flags;
12760 	u32 changes;
12761 	int rc = 0;
12762 	bool re_init = false;
12763 
12764 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
12765 	if (features & NETIF_F_GRO_HW)
12766 		flags |= BNXT_FLAG_GRO;
12767 	else if (features & NETIF_F_LRO)
12768 		flags |= BNXT_FLAG_LRO;
12769 
12770 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
12771 		flags &= ~BNXT_FLAG_TPA;
12772 
12773 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
12774 		flags |= BNXT_FLAG_STRIP_VLAN;
12775 
12776 	if (features & NETIF_F_NTUPLE)
12777 		flags |= BNXT_FLAG_RFS;
12778 	else
12779 		bnxt_clear_usr_fltrs(bp, true);
12780 
12781 	changes = flags ^ bp->flags;
12782 	if (changes & BNXT_FLAG_TPA) {
12783 		update_tpa = true;
12784 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
12785 		    (flags & BNXT_FLAG_TPA) == 0 ||
12786 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
12787 			re_init = true;
12788 	}
12789 
12790 	if (changes & ~BNXT_FLAG_TPA)
12791 		re_init = true;
12792 
12793 	if (changes & BNXT_FLAG_RFS)
12794 		update_ntuple = true;
12795 
12796 	if (flags != bp->flags) {
12797 		u32 old_flags = bp->flags;
12798 
12799 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12800 			bp->flags = flags;
12801 			if (update_tpa)
12802 				bnxt_set_ring_params(bp);
12803 			return rc;
12804 		}
12805 
12806 		if (update_ntuple)
12807 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
12808 
12809 		if (re_init)
12810 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
12811 
12812 		if (update_tpa) {
12813 			bp->flags = flags;
12814 			rc = bnxt_set_tpa(bp,
12815 					  (flags & BNXT_FLAG_TPA) ?
12816 					  true : false);
12817 			if (rc)
12818 				bp->flags = old_flags;
12819 		}
12820 	}
12821 	return rc;
12822 }
12823 
12824 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
12825 			      u8 **nextp)
12826 {
12827 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
12828 	struct hop_jumbo_hdr *jhdr;
12829 	int hdr_count = 0;
12830 	u8 *nexthdr;
12831 	int start;
12832 
12833 	/* Check that there are at most 2 IPv6 extension headers, no
12834 	 * fragment header, and each is <= 64 bytes.
12835 	 */
12836 	start = nw_off + sizeof(*ip6h);
12837 	nexthdr = &ip6h->nexthdr;
12838 	while (ipv6_ext_hdr(*nexthdr)) {
12839 		struct ipv6_opt_hdr *hp;
12840 		int hdrlen;
12841 
12842 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
12843 		    *nexthdr == NEXTHDR_FRAGMENT)
12844 			return false;
12845 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
12846 					  skb_headlen(skb), NULL);
12847 		if (!hp)
12848 			return false;
12849 		if (*nexthdr == NEXTHDR_AUTH)
12850 			hdrlen = ipv6_authlen(hp);
12851 		else
12852 			hdrlen = ipv6_optlen(hp);
12853 
12854 		if (hdrlen > 64)
12855 			return false;
12856 
12857 		/* The ext header may be a hop-by-hop header inserted for
12858 		 * big TCP purposes. This will be removed before sending
12859 		 * from NIC, so do not count it.
12860 		 */
12861 		if (*nexthdr == NEXTHDR_HOP) {
12862 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
12863 				goto increment_hdr;
12864 
12865 			jhdr = (struct hop_jumbo_hdr *)hp;
12866 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
12867 			    jhdr->nexthdr != IPPROTO_TCP)
12868 				goto increment_hdr;
12869 
12870 			goto next_hdr;
12871 		}
12872 increment_hdr:
12873 		hdr_count++;
12874 next_hdr:
12875 		nexthdr = &hp->nexthdr;
12876 		start += hdrlen;
12877 	}
12878 	if (nextp) {
12879 		/* Caller will check inner protocol */
12880 		if (skb->encapsulation) {
12881 			*nextp = nexthdr;
12882 			return true;
12883 		}
12884 		*nextp = NULL;
12885 	}
12886 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
12887 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
12888 }
12889 
12890 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
12891 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
12892 {
12893 	struct udphdr *uh = udp_hdr(skb);
12894 	__be16 udp_port = uh->dest;
12895 
12896 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
12897 	    udp_port != bp->vxlan_gpe_port)
12898 		return false;
12899 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
12900 		struct ethhdr *eh = inner_eth_hdr(skb);
12901 
12902 		switch (eh->h_proto) {
12903 		case htons(ETH_P_IP):
12904 			return true;
12905 		case htons(ETH_P_IPV6):
12906 			return bnxt_exthdr_check(bp, skb,
12907 						 skb_inner_network_offset(skb),
12908 						 NULL);
12909 		}
12910 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
12911 		return true;
12912 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
12913 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
12914 					 NULL);
12915 	}
12916 	return false;
12917 }
12918 
12919 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
12920 {
12921 	switch (l4_proto) {
12922 	case IPPROTO_UDP:
12923 		return bnxt_udp_tunl_check(bp, skb);
12924 	case IPPROTO_IPIP:
12925 		return true;
12926 	case IPPROTO_GRE: {
12927 		switch (skb->inner_protocol) {
12928 		default:
12929 			return false;
12930 		case htons(ETH_P_IP):
12931 			return true;
12932 		case htons(ETH_P_IPV6):
12933 			fallthrough;
12934 		}
12935 	}
12936 	case IPPROTO_IPV6:
12937 		/* Check ext headers of inner ipv6 */
12938 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
12939 					 NULL);
12940 	}
12941 	return false;
12942 }
12943 
12944 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
12945 					     struct net_device *dev,
12946 					     netdev_features_t features)
12947 {
12948 	struct bnxt *bp = netdev_priv(dev);
12949 	u8 *l4_proto;
12950 
12951 	features = vlan_features_check(skb, features);
12952 	switch (vlan_get_protocol(skb)) {
12953 	case htons(ETH_P_IP):
12954 		if (!skb->encapsulation)
12955 			return features;
12956 		l4_proto = &ip_hdr(skb)->protocol;
12957 		if (bnxt_tunl_check(bp, skb, *l4_proto))
12958 			return features;
12959 		break;
12960 	case htons(ETH_P_IPV6):
12961 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
12962 				       &l4_proto))
12963 			break;
12964 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
12965 			return features;
12966 		break;
12967 	}
12968 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
12969 }
12970 
12971 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
12972 			 u32 *reg_buf)
12973 {
12974 	struct hwrm_dbg_read_direct_output *resp;
12975 	struct hwrm_dbg_read_direct_input *req;
12976 	__le32 *dbg_reg_buf;
12977 	dma_addr_t mapping;
12978 	int rc, i;
12979 
12980 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
12981 	if (rc)
12982 		return rc;
12983 
12984 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
12985 					 &mapping);
12986 	if (!dbg_reg_buf) {
12987 		rc = -ENOMEM;
12988 		goto dbg_rd_reg_exit;
12989 	}
12990 
12991 	req->host_dest_addr = cpu_to_le64(mapping);
12992 
12993 	resp = hwrm_req_hold(bp, req);
12994 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
12995 	req->read_len32 = cpu_to_le32(num_words);
12996 
12997 	rc = hwrm_req_send(bp, req);
12998 	if (rc || resp->error_code) {
12999 		rc = -EIO;
13000 		goto dbg_rd_reg_exit;
13001 	}
13002 	for (i = 0; i < num_words; i++)
13003 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
13004 
13005 dbg_rd_reg_exit:
13006 	hwrm_req_drop(bp, req);
13007 	return rc;
13008 }
13009 
13010 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
13011 				       u32 ring_id, u32 *prod, u32 *cons)
13012 {
13013 	struct hwrm_dbg_ring_info_get_output *resp;
13014 	struct hwrm_dbg_ring_info_get_input *req;
13015 	int rc;
13016 
13017 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
13018 	if (rc)
13019 		return rc;
13020 
13021 	req->ring_type = ring_type;
13022 	req->fw_ring_id = cpu_to_le32(ring_id);
13023 	resp = hwrm_req_hold(bp, req);
13024 	rc = hwrm_req_send(bp, req);
13025 	if (!rc) {
13026 		*prod = le32_to_cpu(resp->producer_index);
13027 		*cons = le32_to_cpu(resp->consumer_index);
13028 	}
13029 	hwrm_req_drop(bp, req);
13030 	return rc;
13031 }
13032 
13033 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13034 {
13035 	struct bnxt_tx_ring_info *txr;
13036 	int i = bnapi->index, j;
13037 
13038 	bnxt_for_each_napi_tx(j, bnapi, txr)
13039 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13040 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13041 			    txr->tx_cons);
13042 }
13043 
13044 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
13045 {
13046 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
13047 	int i = bnapi->index;
13048 
13049 	if (!rxr)
13050 		return;
13051 
13052 	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",
13053 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
13054 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
13055 		    rxr->rx_sw_agg_prod);
13056 }
13057 
13058 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
13059 {
13060 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13061 	int i = bnapi->index;
13062 
13063 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
13064 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
13065 }
13066 
13067 static void bnxt_dbg_dump_states(struct bnxt *bp)
13068 {
13069 	int i;
13070 	struct bnxt_napi *bnapi;
13071 
13072 	for (i = 0; i < bp->cp_nr_rings; i++) {
13073 		bnapi = bp->bnapi[i];
13074 		if (netif_msg_drv(bp)) {
13075 			bnxt_dump_tx_sw_state(bnapi);
13076 			bnxt_dump_rx_sw_state(bnapi);
13077 			bnxt_dump_cp_sw_state(bnapi);
13078 		}
13079 	}
13080 }
13081 
13082 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
13083 {
13084 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
13085 	struct hwrm_ring_reset_input *req;
13086 	struct bnxt_napi *bnapi = rxr->bnapi;
13087 	struct bnxt_cp_ring_info *cpr;
13088 	u16 cp_ring_id;
13089 	int rc;
13090 
13091 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
13092 	if (rc)
13093 		return rc;
13094 
13095 	cpr = &bnapi->cp_ring;
13096 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
13097 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
13098 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
13099 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
13100 	return hwrm_req_send_silent(bp, req);
13101 }
13102 
13103 static void bnxt_reset_task(struct bnxt *bp, bool silent)
13104 {
13105 	if (!silent)
13106 		bnxt_dbg_dump_states(bp);
13107 	if (netif_running(bp->dev)) {
13108 		bnxt_close_nic(bp, !silent, false);
13109 		bnxt_open_nic(bp, !silent, false);
13110 	}
13111 }
13112 
13113 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
13114 {
13115 	struct bnxt *bp = netdev_priv(dev);
13116 
13117 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
13118 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
13119 }
13120 
13121 static void bnxt_fw_health_check(struct bnxt *bp)
13122 {
13123 	struct bnxt_fw_health *fw_health = bp->fw_health;
13124 	struct pci_dev *pdev = bp->pdev;
13125 	u32 val;
13126 
13127 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13128 		return;
13129 
13130 	/* Make sure it is enabled before checking the tmr_counter. */
13131 	smp_rmb();
13132 	if (fw_health->tmr_counter) {
13133 		fw_health->tmr_counter--;
13134 		return;
13135 	}
13136 
13137 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13138 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
13139 		fw_health->arrests++;
13140 		goto fw_reset;
13141 	}
13142 
13143 	fw_health->last_fw_heartbeat = val;
13144 
13145 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13146 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
13147 		fw_health->discoveries++;
13148 		goto fw_reset;
13149 	}
13150 
13151 	fw_health->tmr_counter = fw_health->tmr_multiplier;
13152 	return;
13153 
13154 fw_reset:
13155 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
13156 }
13157 
13158 static void bnxt_timer(struct timer_list *t)
13159 {
13160 	struct bnxt *bp = from_timer(bp, t, timer);
13161 	struct net_device *dev = bp->dev;
13162 
13163 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
13164 		return;
13165 
13166 	if (atomic_read(&bp->intr_sem) != 0)
13167 		goto bnxt_restart_timer;
13168 
13169 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
13170 		bnxt_fw_health_check(bp);
13171 
13172 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
13173 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
13174 
13175 	if (bnxt_tc_flower_enabled(bp))
13176 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
13177 
13178 #ifdef CONFIG_RFS_ACCEL
13179 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
13180 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
13181 #endif /*CONFIG_RFS_ACCEL*/
13182 
13183 	if (bp->link_info.phy_retry) {
13184 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
13185 			bp->link_info.phy_retry = false;
13186 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
13187 		} else {
13188 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
13189 		}
13190 	}
13191 
13192 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13193 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13194 
13195 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
13196 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
13197 
13198 bnxt_restart_timer:
13199 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13200 }
13201 
13202 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
13203 {
13204 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
13205 	 * set.  If the device is being closed, bnxt_close() may be holding
13206 	 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear.  So we
13207 	 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
13208 	 */
13209 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13210 	rtnl_lock();
13211 }
13212 
13213 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
13214 {
13215 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13216 	rtnl_unlock();
13217 }
13218 
13219 /* Only called from bnxt_sp_task() */
13220 static void bnxt_reset(struct bnxt *bp, bool silent)
13221 {
13222 	bnxt_rtnl_lock_sp(bp);
13223 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
13224 		bnxt_reset_task(bp, silent);
13225 	bnxt_rtnl_unlock_sp(bp);
13226 }
13227 
13228 /* Only called from bnxt_sp_task() */
13229 static void bnxt_rx_ring_reset(struct bnxt *bp)
13230 {
13231 	int i;
13232 
13233 	bnxt_rtnl_lock_sp(bp);
13234 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13235 		bnxt_rtnl_unlock_sp(bp);
13236 		return;
13237 	}
13238 	/* Disable and flush TPA before resetting the RX ring */
13239 	if (bp->flags & BNXT_FLAG_TPA)
13240 		bnxt_set_tpa(bp, false);
13241 	for (i = 0; i < bp->rx_nr_rings; i++) {
13242 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
13243 		struct bnxt_cp_ring_info *cpr;
13244 		int rc;
13245 
13246 		if (!rxr->bnapi->in_reset)
13247 			continue;
13248 
13249 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
13250 		if (rc) {
13251 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
13252 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
13253 			else
13254 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
13255 					    rc);
13256 			bnxt_reset_task(bp, true);
13257 			break;
13258 		}
13259 		bnxt_free_one_rx_ring_skbs(bp, i);
13260 		rxr->rx_prod = 0;
13261 		rxr->rx_agg_prod = 0;
13262 		rxr->rx_sw_agg_prod = 0;
13263 		rxr->rx_next_cons = 0;
13264 		rxr->bnapi->in_reset = false;
13265 		bnxt_alloc_one_rx_ring(bp, i);
13266 		cpr = &rxr->bnapi->cp_ring;
13267 		cpr->sw_stats->rx.rx_resets++;
13268 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
13269 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
13270 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
13271 	}
13272 	if (bp->flags & BNXT_FLAG_TPA)
13273 		bnxt_set_tpa(bp, true);
13274 	bnxt_rtnl_unlock_sp(bp);
13275 }
13276 
13277 static void bnxt_fw_fatal_close(struct bnxt *bp)
13278 {
13279 	bnxt_tx_disable(bp);
13280 	bnxt_disable_napi(bp);
13281 	bnxt_disable_int_sync(bp);
13282 	bnxt_free_irq(bp);
13283 	bnxt_clear_int_mode(bp);
13284 	pci_disable_device(bp->pdev);
13285 }
13286 
13287 static void bnxt_fw_reset_close(struct bnxt *bp)
13288 {
13289 	/* When firmware is in fatal state, quiesce device and disable
13290 	 * bus master to prevent any potential bad DMAs before freeing
13291 	 * kernel memory.
13292 	 */
13293 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
13294 		u16 val = 0;
13295 
13296 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
13297 		if (val == 0xffff)
13298 			bp->fw_reset_min_dsecs = 0;
13299 		bnxt_fw_fatal_close(bp);
13300 	}
13301 	__bnxt_close_nic(bp, true, false);
13302 	bnxt_vf_reps_free(bp);
13303 	bnxt_clear_int_mode(bp);
13304 	bnxt_hwrm_func_drv_unrgtr(bp);
13305 	if (pci_is_enabled(bp->pdev))
13306 		pci_disable_device(bp->pdev);
13307 	bnxt_free_ctx_mem(bp);
13308 }
13309 
13310 static bool is_bnxt_fw_ok(struct bnxt *bp)
13311 {
13312 	struct bnxt_fw_health *fw_health = bp->fw_health;
13313 	bool no_heartbeat = false, has_reset = false;
13314 	u32 val;
13315 
13316 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13317 	if (val == fw_health->last_fw_heartbeat)
13318 		no_heartbeat = true;
13319 
13320 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13321 	if (val != fw_health->last_fw_reset_cnt)
13322 		has_reset = true;
13323 
13324 	if (!no_heartbeat && has_reset)
13325 		return true;
13326 
13327 	return false;
13328 }
13329 
13330 /* rtnl_lock is acquired before calling this function */
13331 static void bnxt_force_fw_reset(struct bnxt *bp)
13332 {
13333 	struct bnxt_fw_health *fw_health = bp->fw_health;
13334 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13335 	u32 wait_dsecs;
13336 
13337 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
13338 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13339 		return;
13340 
13341 	if (ptp) {
13342 		spin_lock_bh(&ptp->ptp_lock);
13343 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13344 		spin_unlock_bh(&ptp->ptp_lock);
13345 	} else {
13346 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13347 	}
13348 	bnxt_fw_reset_close(bp);
13349 	wait_dsecs = fw_health->master_func_wait_dsecs;
13350 	if (fw_health->primary) {
13351 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
13352 			wait_dsecs = 0;
13353 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
13354 	} else {
13355 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
13356 		wait_dsecs = fw_health->normal_func_wait_dsecs;
13357 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13358 	}
13359 
13360 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
13361 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
13362 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
13363 }
13364 
13365 void bnxt_fw_exception(struct bnxt *bp)
13366 {
13367 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
13368 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
13369 	bnxt_ulp_stop(bp);
13370 	bnxt_rtnl_lock_sp(bp);
13371 	bnxt_force_fw_reset(bp);
13372 	bnxt_rtnl_unlock_sp(bp);
13373 }
13374 
13375 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
13376  * < 0 on error.
13377  */
13378 static int bnxt_get_registered_vfs(struct bnxt *bp)
13379 {
13380 #ifdef CONFIG_BNXT_SRIOV
13381 	int rc;
13382 
13383 	if (!BNXT_PF(bp))
13384 		return 0;
13385 
13386 	rc = bnxt_hwrm_func_qcfg(bp);
13387 	if (rc) {
13388 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
13389 		return rc;
13390 	}
13391 	if (bp->pf.registered_vfs)
13392 		return bp->pf.registered_vfs;
13393 	if (bp->sriov_cfg)
13394 		return 1;
13395 #endif
13396 	return 0;
13397 }
13398 
13399 void bnxt_fw_reset(struct bnxt *bp)
13400 {
13401 	bnxt_ulp_stop(bp);
13402 	bnxt_rtnl_lock_sp(bp);
13403 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
13404 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13405 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13406 		int n = 0, tmo;
13407 
13408 		if (ptp) {
13409 			spin_lock_bh(&ptp->ptp_lock);
13410 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13411 			spin_unlock_bh(&ptp->ptp_lock);
13412 		} else {
13413 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13414 		}
13415 		if (bp->pf.active_vfs &&
13416 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
13417 			n = bnxt_get_registered_vfs(bp);
13418 		if (n < 0) {
13419 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
13420 				   n);
13421 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13422 			dev_close(bp->dev);
13423 			goto fw_reset_exit;
13424 		} else if (n > 0) {
13425 			u16 vf_tmo_dsecs = n * 10;
13426 
13427 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
13428 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
13429 			bp->fw_reset_state =
13430 				BNXT_FW_RESET_STATE_POLL_VF;
13431 			bnxt_queue_fw_reset_work(bp, HZ / 10);
13432 			goto fw_reset_exit;
13433 		}
13434 		bnxt_fw_reset_close(bp);
13435 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
13436 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
13437 			tmo = HZ / 10;
13438 		} else {
13439 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13440 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
13441 		}
13442 		bnxt_queue_fw_reset_work(bp, tmo);
13443 	}
13444 fw_reset_exit:
13445 	bnxt_rtnl_unlock_sp(bp);
13446 }
13447 
13448 static void bnxt_chk_missed_irq(struct bnxt *bp)
13449 {
13450 	int i;
13451 
13452 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13453 		return;
13454 
13455 	for (i = 0; i < bp->cp_nr_rings; i++) {
13456 		struct bnxt_napi *bnapi = bp->bnapi[i];
13457 		struct bnxt_cp_ring_info *cpr;
13458 		u32 fw_ring_id;
13459 		int j;
13460 
13461 		if (!bnapi)
13462 			continue;
13463 
13464 		cpr = &bnapi->cp_ring;
13465 		for (j = 0; j < cpr->cp_ring_count; j++) {
13466 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
13467 			u32 val[2];
13468 
13469 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
13470 				continue;
13471 
13472 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
13473 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
13474 				continue;
13475 			}
13476 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
13477 			bnxt_dbg_hwrm_ring_info_get(bp,
13478 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
13479 				fw_ring_id, &val[0], &val[1]);
13480 			cpr->sw_stats->cmn.missed_irqs++;
13481 		}
13482 	}
13483 }
13484 
13485 static void bnxt_cfg_ntp_filters(struct bnxt *);
13486 
13487 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
13488 {
13489 	struct bnxt_link_info *link_info = &bp->link_info;
13490 
13491 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
13492 		link_info->autoneg = BNXT_AUTONEG_SPEED;
13493 		if (bp->hwrm_spec_code >= 0x10201) {
13494 			if (link_info->auto_pause_setting &
13495 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
13496 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13497 		} else {
13498 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13499 		}
13500 		bnxt_set_auto_speed(link_info);
13501 	} else {
13502 		bnxt_set_force_speed(link_info);
13503 		link_info->req_duplex = link_info->duplex_setting;
13504 	}
13505 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
13506 		link_info->req_flow_ctrl =
13507 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
13508 	else
13509 		link_info->req_flow_ctrl = link_info->force_pause_setting;
13510 }
13511 
13512 static void bnxt_fw_echo_reply(struct bnxt *bp)
13513 {
13514 	struct bnxt_fw_health *fw_health = bp->fw_health;
13515 	struct hwrm_func_echo_response_input *req;
13516 	int rc;
13517 
13518 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
13519 	if (rc)
13520 		return;
13521 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
13522 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
13523 	hwrm_req_send(bp, req);
13524 }
13525 
13526 static void bnxt_ulp_restart(struct bnxt *bp)
13527 {
13528 	bnxt_ulp_stop(bp);
13529 	bnxt_ulp_start(bp, 0);
13530 }
13531 
13532 static void bnxt_sp_task(struct work_struct *work)
13533 {
13534 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
13535 
13536 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13537 	smp_mb__after_atomic();
13538 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13539 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13540 		return;
13541 	}
13542 
13543 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
13544 		bnxt_ulp_restart(bp);
13545 		bnxt_reenable_sriov(bp);
13546 	}
13547 
13548 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
13549 		bnxt_cfg_rx_mode(bp);
13550 
13551 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
13552 		bnxt_cfg_ntp_filters(bp);
13553 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
13554 		bnxt_hwrm_exec_fwd_req(bp);
13555 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
13556 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
13557 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
13558 		bnxt_hwrm_port_qstats(bp, 0);
13559 		bnxt_hwrm_port_qstats_ext(bp, 0);
13560 		bnxt_accumulate_all_stats(bp);
13561 	}
13562 
13563 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
13564 		int rc;
13565 
13566 		mutex_lock(&bp->link_lock);
13567 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
13568 				       &bp->sp_event))
13569 			bnxt_hwrm_phy_qcaps(bp);
13570 
13571 		rc = bnxt_update_link(bp, true);
13572 		if (rc)
13573 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
13574 				   rc);
13575 
13576 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
13577 				       &bp->sp_event))
13578 			bnxt_init_ethtool_link_settings(bp);
13579 		mutex_unlock(&bp->link_lock);
13580 	}
13581 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
13582 		int rc;
13583 
13584 		mutex_lock(&bp->link_lock);
13585 		rc = bnxt_update_phy_setting(bp);
13586 		mutex_unlock(&bp->link_lock);
13587 		if (rc) {
13588 			netdev_warn(bp->dev, "update phy settings retry failed\n");
13589 		} else {
13590 			bp->link_info.phy_retry = false;
13591 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
13592 		}
13593 	}
13594 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
13595 		mutex_lock(&bp->link_lock);
13596 		bnxt_get_port_module_status(bp);
13597 		mutex_unlock(&bp->link_lock);
13598 	}
13599 
13600 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
13601 		bnxt_tc_flow_stats_work(bp);
13602 
13603 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
13604 		bnxt_chk_missed_irq(bp);
13605 
13606 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
13607 		bnxt_fw_echo_reply(bp);
13608 
13609 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
13610 		bnxt_hwmon_notify_event(bp);
13611 
13612 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
13613 	 * must be the last functions to be called before exiting.
13614 	 */
13615 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
13616 		bnxt_reset(bp, false);
13617 
13618 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
13619 		bnxt_reset(bp, true);
13620 
13621 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
13622 		bnxt_rx_ring_reset(bp);
13623 
13624 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
13625 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
13626 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
13627 			bnxt_devlink_health_fw_report(bp);
13628 		else
13629 			bnxt_fw_reset(bp);
13630 	}
13631 
13632 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
13633 		if (!is_bnxt_fw_ok(bp))
13634 			bnxt_devlink_health_fw_report(bp);
13635 	}
13636 
13637 	smp_mb__before_atomic();
13638 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13639 }
13640 
13641 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
13642 				int *max_cp);
13643 
13644 /* Under rtnl_lock */
13645 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
13646 		     int tx_xdp)
13647 {
13648 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
13649 	struct bnxt_hw_rings hwr = {0};
13650 	int rx_rings = rx;
13651 
13652 	if (tcs)
13653 		tx_sets = tcs;
13654 
13655 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
13656 
13657 	if (max_rx < rx_rings)
13658 		return -ENOMEM;
13659 
13660 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
13661 		rx_rings <<= 1;
13662 
13663 	hwr.rx = rx_rings;
13664 	hwr.tx = tx * tx_sets + tx_xdp;
13665 	if (max_tx < hwr.tx)
13666 		return -ENOMEM;
13667 
13668 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
13669 
13670 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
13671 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
13672 	if (max_cp < hwr.cp)
13673 		return -ENOMEM;
13674 	hwr.stat = hwr.cp;
13675 	if (BNXT_NEW_RM(bp)) {
13676 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
13677 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
13678 		hwr.grp = rx;
13679 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13680 	}
13681 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
13682 		hwr.cp_p5 = hwr.tx + rx;
13683 	return bnxt_hwrm_check_rings(bp, &hwr);
13684 }
13685 
13686 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
13687 {
13688 	if (bp->bar2) {
13689 		pci_iounmap(pdev, bp->bar2);
13690 		bp->bar2 = NULL;
13691 	}
13692 
13693 	if (bp->bar1) {
13694 		pci_iounmap(pdev, bp->bar1);
13695 		bp->bar1 = NULL;
13696 	}
13697 
13698 	if (bp->bar0) {
13699 		pci_iounmap(pdev, bp->bar0);
13700 		bp->bar0 = NULL;
13701 	}
13702 }
13703 
13704 static void bnxt_cleanup_pci(struct bnxt *bp)
13705 {
13706 	bnxt_unmap_bars(bp, bp->pdev);
13707 	pci_release_regions(bp->pdev);
13708 	if (pci_is_enabled(bp->pdev))
13709 		pci_disable_device(bp->pdev);
13710 }
13711 
13712 static void bnxt_init_dflt_coal(struct bnxt *bp)
13713 {
13714 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
13715 	struct bnxt_coal *coal;
13716 	u16 flags = 0;
13717 
13718 	if (coal_cap->cmpl_params &
13719 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
13720 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
13721 
13722 	/* Tick values in micro seconds.
13723 	 * 1 coal_buf x bufs_per_record = 1 completion record.
13724 	 */
13725 	coal = &bp->rx_coal;
13726 	coal->coal_ticks = 10;
13727 	coal->coal_bufs = 30;
13728 	coal->coal_ticks_irq = 1;
13729 	coal->coal_bufs_irq = 2;
13730 	coal->idle_thresh = 50;
13731 	coal->bufs_per_record = 2;
13732 	coal->budget = 64;		/* NAPI budget */
13733 	coal->flags = flags;
13734 
13735 	coal = &bp->tx_coal;
13736 	coal->coal_ticks = 28;
13737 	coal->coal_bufs = 30;
13738 	coal->coal_ticks_irq = 2;
13739 	coal->coal_bufs_irq = 2;
13740 	coal->bufs_per_record = 1;
13741 	coal->flags = flags;
13742 
13743 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
13744 }
13745 
13746 /* FW that pre-reserves 1 VNIC per function */
13747 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
13748 {
13749 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
13750 
13751 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13752 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
13753 		return true;
13754 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13755 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
13756 		return true;
13757 	return false;
13758 }
13759 
13760 static int bnxt_fw_init_one_p1(struct bnxt *bp)
13761 {
13762 	int rc;
13763 
13764 	bp->fw_cap = 0;
13765 	rc = bnxt_hwrm_ver_get(bp);
13766 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
13767 	 * so wait before continuing with recovery.
13768 	 */
13769 	if (rc)
13770 		msleep(100);
13771 	bnxt_try_map_fw_health_reg(bp);
13772 	if (rc) {
13773 		rc = bnxt_try_recover_fw(bp);
13774 		if (rc)
13775 			return rc;
13776 		rc = bnxt_hwrm_ver_get(bp);
13777 		if (rc)
13778 			return rc;
13779 	}
13780 
13781 	bnxt_nvm_cfg_ver_get(bp);
13782 
13783 	rc = bnxt_hwrm_func_reset(bp);
13784 	if (rc)
13785 		return -ENODEV;
13786 
13787 	bnxt_hwrm_fw_set_time(bp);
13788 	return 0;
13789 }
13790 
13791 static int bnxt_fw_init_one_p2(struct bnxt *bp)
13792 {
13793 	int rc;
13794 
13795 	/* Get the MAX capabilities for this function */
13796 	rc = bnxt_hwrm_func_qcaps(bp);
13797 	if (rc) {
13798 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
13799 			   rc);
13800 		return -ENODEV;
13801 	}
13802 
13803 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
13804 	if (rc)
13805 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
13806 			    rc);
13807 
13808 	if (bnxt_alloc_fw_health(bp)) {
13809 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
13810 	} else {
13811 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
13812 		if (rc)
13813 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
13814 				    rc);
13815 	}
13816 
13817 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
13818 	if (rc)
13819 		return -ENODEV;
13820 
13821 	if (bnxt_fw_pre_resv_vnics(bp))
13822 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
13823 
13824 	bnxt_hwrm_func_qcfg(bp);
13825 	bnxt_hwrm_vnic_qcaps(bp);
13826 	bnxt_hwrm_port_led_qcaps(bp);
13827 	bnxt_ethtool_init(bp);
13828 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
13829 		__bnxt_hwrm_ptp_qcfg(bp);
13830 	bnxt_dcb_init(bp);
13831 	bnxt_hwmon_init(bp);
13832 	return 0;
13833 }
13834 
13835 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
13836 {
13837 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
13838 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
13839 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
13840 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
13841 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
13842 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
13843 		bp->rss_hash_delta = bp->rss_hash_cfg;
13844 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
13845 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
13846 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
13847 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
13848 	}
13849 }
13850 
13851 static void bnxt_set_dflt_rfs(struct bnxt *bp)
13852 {
13853 	struct net_device *dev = bp->dev;
13854 
13855 	dev->hw_features &= ~NETIF_F_NTUPLE;
13856 	dev->features &= ~NETIF_F_NTUPLE;
13857 	bp->flags &= ~BNXT_FLAG_RFS;
13858 	if (bnxt_rfs_supported(bp)) {
13859 		dev->hw_features |= NETIF_F_NTUPLE;
13860 		if (bnxt_rfs_capable(bp, false)) {
13861 			bp->flags |= BNXT_FLAG_RFS;
13862 			dev->features |= NETIF_F_NTUPLE;
13863 		}
13864 	}
13865 }
13866 
13867 static void bnxt_fw_init_one_p3(struct bnxt *bp)
13868 {
13869 	struct pci_dev *pdev = bp->pdev;
13870 
13871 	bnxt_set_dflt_rss_hash_type(bp);
13872 	bnxt_set_dflt_rfs(bp);
13873 
13874 	bnxt_get_wol_settings(bp);
13875 	if (bp->flags & BNXT_FLAG_WOL_CAP)
13876 		device_set_wakeup_enable(&pdev->dev, bp->wol);
13877 	else
13878 		device_set_wakeup_capable(&pdev->dev, false);
13879 
13880 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
13881 	bnxt_hwrm_coal_params_qcaps(bp);
13882 }
13883 
13884 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
13885 
13886 int bnxt_fw_init_one(struct bnxt *bp)
13887 {
13888 	int rc;
13889 
13890 	rc = bnxt_fw_init_one_p1(bp);
13891 	if (rc) {
13892 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
13893 		return rc;
13894 	}
13895 	rc = bnxt_fw_init_one_p2(bp);
13896 	if (rc) {
13897 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
13898 		return rc;
13899 	}
13900 	rc = bnxt_probe_phy(bp, false);
13901 	if (rc)
13902 		return rc;
13903 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
13904 	if (rc)
13905 		return rc;
13906 
13907 	bnxt_fw_init_one_p3(bp);
13908 	return 0;
13909 }
13910 
13911 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
13912 {
13913 	struct bnxt_fw_health *fw_health = bp->fw_health;
13914 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
13915 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
13916 	u32 reg_type, reg_off, delay_msecs;
13917 
13918 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
13919 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
13920 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
13921 	switch (reg_type) {
13922 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
13923 		pci_write_config_dword(bp->pdev, reg_off, val);
13924 		break;
13925 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
13926 		writel(reg_off & BNXT_GRC_BASE_MASK,
13927 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
13928 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
13929 		fallthrough;
13930 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
13931 		writel(val, bp->bar0 + reg_off);
13932 		break;
13933 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
13934 		writel(val, bp->bar1 + reg_off);
13935 		break;
13936 	}
13937 	if (delay_msecs) {
13938 		pci_read_config_dword(bp->pdev, 0, &val);
13939 		msleep(delay_msecs);
13940 	}
13941 }
13942 
13943 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
13944 {
13945 	struct hwrm_func_qcfg_output *resp;
13946 	struct hwrm_func_qcfg_input *req;
13947 	bool result = true; /* firmware will enforce if unknown */
13948 
13949 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
13950 		return result;
13951 
13952 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
13953 		return result;
13954 
13955 	req->fid = cpu_to_le16(0xffff);
13956 	resp = hwrm_req_hold(bp, req);
13957 	if (!hwrm_req_send(bp, req))
13958 		result = !!(le16_to_cpu(resp->flags) &
13959 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
13960 	hwrm_req_drop(bp, req);
13961 	return result;
13962 }
13963 
13964 static void bnxt_reset_all(struct bnxt *bp)
13965 {
13966 	struct bnxt_fw_health *fw_health = bp->fw_health;
13967 	int i, rc;
13968 
13969 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
13970 		bnxt_fw_reset_via_optee(bp);
13971 		bp->fw_reset_timestamp = jiffies;
13972 		return;
13973 	}
13974 
13975 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
13976 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
13977 			bnxt_fw_reset_writel(bp, i);
13978 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
13979 		struct hwrm_fw_reset_input *req;
13980 
13981 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
13982 		if (!rc) {
13983 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
13984 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
13985 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
13986 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
13987 			rc = hwrm_req_send(bp, req);
13988 		}
13989 		if (rc != -ENODEV)
13990 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
13991 	}
13992 	bp->fw_reset_timestamp = jiffies;
13993 }
13994 
13995 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
13996 {
13997 	return time_after(jiffies, bp->fw_reset_timestamp +
13998 			  (bp->fw_reset_max_dsecs * HZ / 10));
13999 }
14000 
14001 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
14002 {
14003 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14004 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
14005 		bnxt_dl_health_fw_status_update(bp, false);
14006 	bp->fw_reset_state = 0;
14007 	dev_close(bp->dev);
14008 }
14009 
14010 static void bnxt_fw_reset_task(struct work_struct *work)
14011 {
14012 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
14013 	int rc = 0;
14014 
14015 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14016 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
14017 		return;
14018 	}
14019 
14020 	switch (bp->fw_reset_state) {
14021 	case BNXT_FW_RESET_STATE_POLL_VF: {
14022 		int n = bnxt_get_registered_vfs(bp);
14023 		int tmo;
14024 
14025 		if (n < 0) {
14026 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
14027 				   n, jiffies_to_msecs(jiffies -
14028 				   bp->fw_reset_timestamp));
14029 			goto fw_reset_abort;
14030 		} else if (n > 0) {
14031 			if (bnxt_fw_reset_timeout(bp)) {
14032 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14033 				bp->fw_reset_state = 0;
14034 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
14035 					   n);
14036 				goto ulp_start;
14037 			}
14038 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14039 			return;
14040 		}
14041 		bp->fw_reset_timestamp = jiffies;
14042 		rtnl_lock();
14043 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
14044 			bnxt_fw_reset_abort(bp, rc);
14045 			rtnl_unlock();
14046 			goto ulp_start;
14047 		}
14048 		bnxt_fw_reset_close(bp);
14049 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14050 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14051 			tmo = HZ / 10;
14052 		} else {
14053 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14054 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14055 		}
14056 		rtnl_unlock();
14057 		bnxt_queue_fw_reset_work(bp, tmo);
14058 		return;
14059 	}
14060 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
14061 		u32 val;
14062 
14063 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14064 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
14065 		    !bnxt_fw_reset_timeout(bp)) {
14066 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14067 			return;
14068 		}
14069 
14070 		if (!bp->fw_health->primary) {
14071 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
14072 
14073 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14074 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14075 			return;
14076 		}
14077 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14078 	}
14079 		fallthrough;
14080 	case BNXT_FW_RESET_STATE_RESET_FW:
14081 		bnxt_reset_all(bp);
14082 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14083 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
14084 		return;
14085 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
14086 		bnxt_inv_fw_health_reg(bp);
14087 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
14088 		    !bp->fw_reset_min_dsecs) {
14089 			u16 val;
14090 
14091 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14092 			if (val == 0xffff) {
14093 				if (bnxt_fw_reset_timeout(bp)) {
14094 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
14095 					rc = -ETIMEDOUT;
14096 					goto fw_reset_abort;
14097 				}
14098 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
14099 				return;
14100 			}
14101 		}
14102 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14103 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
14104 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
14105 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
14106 			bnxt_dl_remote_reload(bp);
14107 		if (pci_enable_device(bp->pdev)) {
14108 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
14109 			rc = -ENODEV;
14110 			goto fw_reset_abort;
14111 		}
14112 		pci_set_master(bp->pdev);
14113 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
14114 		fallthrough;
14115 	case BNXT_FW_RESET_STATE_POLL_FW:
14116 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
14117 		rc = bnxt_hwrm_poll(bp);
14118 		if (rc) {
14119 			if (bnxt_fw_reset_timeout(bp)) {
14120 				netdev_err(bp->dev, "Firmware reset aborted\n");
14121 				goto fw_reset_abort_status;
14122 			}
14123 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14124 			return;
14125 		}
14126 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
14127 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
14128 		fallthrough;
14129 	case BNXT_FW_RESET_STATE_OPENING:
14130 		while (!rtnl_trylock()) {
14131 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14132 			return;
14133 		}
14134 		rc = bnxt_open(bp->dev);
14135 		if (rc) {
14136 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
14137 			bnxt_fw_reset_abort(bp, rc);
14138 			rtnl_unlock();
14139 			goto ulp_start;
14140 		}
14141 
14142 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
14143 		    bp->fw_health->enabled) {
14144 			bp->fw_health->last_fw_reset_cnt =
14145 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14146 		}
14147 		bp->fw_reset_state = 0;
14148 		/* Make sure fw_reset_state is 0 before clearing the flag */
14149 		smp_mb__before_atomic();
14150 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14151 		bnxt_ptp_reapply_pps(bp);
14152 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
14153 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
14154 			bnxt_dl_health_fw_recovery_done(bp);
14155 			bnxt_dl_health_fw_status_update(bp, true);
14156 		}
14157 		rtnl_unlock();
14158 		bnxt_ulp_start(bp, 0);
14159 		bnxt_reenable_sriov(bp);
14160 		rtnl_lock();
14161 		bnxt_vf_reps_alloc(bp);
14162 		bnxt_vf_reps_open(bp);
14163 		rtnl_unlock();
14164 		break;
14165 	}
14166 	return;
14167 
14168 fw_reset_abort_status:
14169 	if (bp->fw_health->status_reliable ||
14170 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
14171 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14172 
14173 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
14174 	}
14175 fw_reset_abort:
14176 	rtnl_lock();
14177 	bnxt_fw_reset_abort(bp, rc);
14178 	rtnl_unlock();
14179 ulp_start:
14180 	bnxt_ulp_start(bp, rc);
14181 }
14182 
14183 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
14184 {
14185 	int rc;
14186 	struct bnxt *bp = netdev_priv(dev);
14187 
14188 	SET_NETDEV_DEV(dev, &pdev->dev);
14189 
14190 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
14191 	rc = pci_enable_device(pdev);
14192 	if (rc) {
14193 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
14194 		goto init_err;
14195 	}
14196 
14197 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
14198 		dev_err(&pdev->dev,
14199 			"Cannot find PCI device base address, aborting\n");
14200 		rc = -ENODEV;
14201 		goto init_err_disable;
14202 	}
14203 
14204 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
14205 	if (rc) {
14206 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
14207 		goto init_err_disable;
14208 	}
14209 
14210 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
14211 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
14212 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
14213 		rc = -EIO;
14214 		goto init_err_release;
14215 	}
14216 
14217 	pci_set_master(pdev);
14218 
14219 	bp->dev = dev;
14220 	bp->pdev = pdev;
14221 
14222 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
14223 	 * determines the BAR size.
14224 	 */
14225 	bp->bar0 = pci_ioremap_bar(pdev, 0);
14226 	if (!bp->bar0) {
14227 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
14228 		rc = -ENOMEM;
14229 		goto init_err_release;
14230 	}
14231 
14232 	bp->bar2 = pci_ioremap_bar(pdev, 4);
14233 	if (!bp->bar2) {
14234 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
14235 		rc = -ENOMEM;
14236 		goto init_err_release;
14237 	}
14238 
14239 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
14240 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
14241 
14242 	spin_lock_init(&bp->ntp_fltr_lock);
14243 #if BITS_PER_LONG == 32
14244 	spin_lock_init(&bp->db_lock);
14245 #endif
14246 
14247 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
14248 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
14249 
14250 	timer_setup(&bp->timer, bnxt_timer, 0);
14251 	bp->current_interval = BNXT_TIMER_INTERVAL;
14252 
14253 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
14254 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
14255 
14256 	clear_bit(BNXT_STATE_OPEN, &bp->state);
14257 	return 0;
14258 
14259 init_err_release:
14260 	bnxt_unmap_bars(bp, pdev);
14261 	pci_release_regions(pdev);
14262 
14263 init_err_disable:
14264 	pci_disable_device(pdev);
14265 
14266 init_err:
14267 	return rc;
14268 }
14269 
14270 /* rtnl_lock held */
14271 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
14272 {
14273 	struct sockaddr *addr = p;
14274 	struct bnxt *bp = netdev_priv(dev);
14275 	int rc = 0;
14276 
14277 	if (!is_valid_ether_addr(addr->sa_data))
14278 		return -EADDRNOTAVAIL;
14279 
14280 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
14281 		return 0;
14282 
14283 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
14284 	if (rc)
14285 		return rc;
14286 
14287 	eth_hw_addr_set(dev, addr->sa_data);
14288 	bnxt_clear_usr_fltrs(bp, true);
14289 	if (netif_running(dev)) {
14290 		bnxt_close_nic(bp, false, false);
14291 		rc = bnxt_open_nic(bp, false, false);
14292 	}
14293 
14294 	return rc;
14295 }
14296 
14297 /* rtnl_lock held */
14298 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
14299 {
14300 	struct bnxt *bp = netdev_priv(dev);
14301 
14302 	if (netif_running(dev))
14303 		bnxt_close_nic(bp, true, false);
14304 
14305 	WRITE_ONCE(dev->mtu, new_mtu);
14306 	bnxt_set_ring_params(bp);
14307 
14308 	if (netif_running(dev))
14309 		return bnxt_open_nic(bp, true, false);
14310 
14311 	return 0;
14312 }
14313 
14314 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
14315 {
14316 	struct bnxt *bp = netdev_priv(dev);
14317 	bool sh = false;
14318 	int rc, tx_cp;
14319 
14320 	if (tc > bp->max_tc) {
14321 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
14322 			   tc, bp->max_tc);
14323 		return -EINVAL;
14324 	}
14325 
14326 	if (bp->num_tc == tc)
14327 		return 0;
14328 
14329 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
14330 		sh = true;
14331 
14332 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
14333 			      sh, tc, bp->tx_nr_rings_xdp);
14334 	if (rc)
14335 		return rc;
14336 
14337 	/* Needs to close the device and do hw resource re-allocations */
14338 	if (netif_running(bp->dev))
14339 		bnxt_close_nic(bp, true, false);
14340 
14341 	if (tc) {
14342 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
14343 		netdev_set_num_tc(dev, tc);
14344 		bp->num_tc = tc;
14345 	} else {
14346 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
14347 		netdev_reset_tc(dev);
14348 		bp->num_tc = 0;
14349 	}
14350 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
14351 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
14352 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
14353 			       tx_cp + bp->rx_nr_rings;
14354 
14355 	if (netif_running(bp->dev))
14356 		return bnxt_open_nic(bp, true, false);
14357 
14358 	return 0;
14359 }
14360 
14361 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
14362 				  void *cb_priv)
14363 {
14364 	struct bnxt *bp = cb_priv;
14365 
14366 	if (!bnxt_tc_flower_enabled(bp) ||
14367 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
14368 		return -EOPNOTSUPP;
14369 
14370 	switch (type) {
14371 	case TC_SETUP_CLSFLOWER:
14372 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
14373 	default:
14374 		return -EOPNOTSUPP;
14375 	}
14376 }
14377 
14378 LIST_HEAD(bnxt_block_cb_list);
14379 
14380 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
14381 			 void *type_data)
14382 {
14383 	struct bnxt *bp = netdev_priv(dev);
14384 
14385 	switch (type) {
14386 	case TC_SETUP_BLOCK:
14387 		return flow_block_cb_setup_simple(type_data,
14388 						  &bnxt_block_cb_list,
14389 						  bnxt_setup_tc_block_cb,
14390 						  bp, bp, true);
14391 	case TC_SETUP_QDISC_MQPRIO: {
14392 		struct tc_mqprio_qopt *mqprio = type_data;
14393 
14394 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
14395 
14396 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
14397 	}
14398 	default:
14399 		return -EOPNOTSUPP;
14400 	}
14401 }
14402 
14403 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
14404 			    const struct sk_buff *skb)
14405 {
14406 	struct bnxt_vnic_info *vnic;
14407 
14408 	if (skb)
14409 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
14410 
14411 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
14412 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
14413 }
14414 
14415 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
14416 			   u32 idx)
14417 {
14418 	struct hlist_head *head;
14419 	int bit_id;
14420 
14421 	spin_lock_bh(&bp->ntp_fltr_lock);
14422 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
14423 	if (bit_id < 0) {
14424 		spin_unlock_bh(&bp->ntp_fltr_lock);
14425 		return -ENOMEM;
14426 	}
14427 
14428 	fltr->base.sw_id = (u16)bit_id;
14429 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
14430 	fltr->base.flags |= BNXT_ACT_RING_DST;
14431 	head = &bp->ntp_fltr_hash_tbl[idx];
14432 	hlist_add_head_rcu(&fltr->base.hash, head);
14433 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
14434 	bnxt_insert_usr_fltr(bp, &fltr->base);
14435 	bp->ntp_fltr_count++;
14436 	spin_unlock_bh(&bp->ntp_fltr_lock);
14437 	return 0;
14438 }
14439 
14440 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
14441 			    struct bnxt_ntuple_filter *f2)
14442 {
14443 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
14444 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
14445 	struct flow_keys *keys1 = &f1->fkeys;
14446 	struct flow_keys *keys2 = &f2->fkeys;
14447 
14448 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
14449 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
14450 		return false;
14451 
14452 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
14453 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
14454 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
14455 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
14456 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
14457 			return false;
14458 	} else {
14459 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
14460 				     &keys2->addrs.v6addrs.src) ||
14461 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
14462 				     &masks2->addrs.v6addrs.src) ||
14463 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
14464 				     &keys2->addrs.v6addrs.dst) ||
14465 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
14466 				     &masks2->addrs.v6addrs.dst))
14467 			return false;
14468 	}
14469 
14470 	return keys1->ports.src == keys2->ports.src &&
14471 	       masks1->ports.src == masks2->ports.src &&
14472 	       keys1->ports.dst == keys2->ports.dst &&
14473 	       masks1->ports.dst == masks2->ports.dst &&
14474 	       keys1->control.flags == keys2->control.flags &&
14475 	       f1->l2_fltr == f2->l2_fltr;
14476 }
14477 
14478 struct bnxt_ntuple_filter *
14479 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
14480 				struct bnxt_ntuple_filter *fltr, u32 idx)
14481 {
14482 	struct bnxt_ntuple_filter *f;
14483 	struct hlist_head *head;
14484 
14485 	head = &bp->ntp_fltr_hash_tbl[idx];
14486 	hlist_for_each_entry_rcu(f, head, base.hash) {
14487 		if (bnxt_fltr_match(f, fltr))
14488 			return f;
14489 	}
14490 	return NULL;
14491 }
14492 
14493 #ifdef CONFIG_RFS_ACCEL
14494 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
14495 			      u16 rxq_index, u32 flow_id)
14496 {
14497 	struct bnxt *bp = netdev_priv(dev);
14498 	struct bnxt_ntuple_filter *fltr, *new_fltr;
14499 	struct flow_keys *fkeys;
14500 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
14501 	struct bnxt_l2_filter *l2_fltr;
14502 	int rc = 0, idx;
14503 	u32 flags;
14504 
14505 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
14506 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
14507 		atomic_inc(&l2_fltr->refcnt);
14508 	} else {
14509 		struct bnxt_l2_key key;
14510 
14511 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
14512 		key.vlan = 0;
14513 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
14514 		if (!l2_fltr)
14515 			return -EINVAL;
14516 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
14517 			bnxt_del_l2_filter(bp, l2_fltr);
14518 			return -EINVAL;
14519 		}
14520 	}
14521 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
14522 	if (!new_fltr) {
14523 		bnxt_del_l2_filter(bp, l2_fltr);
14524 		return -ENOMEM;
14525 	}
14526 
14527 	fkeys = &new_fltr->fkeys;
14528 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
14529 		rc = -EPROTONOSUPPORT;
14530 		goto err_free;
14531 	}
14532 
14533 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
14534 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
14535 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
14536 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
14537 		rc = -EPROTONOSUPPORT;
14538 		goto err_free;
14539 	}
14540 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
14541 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
14542 		if (bp->hwrm_spec_code < 0x10601) {
14543 			rc = -EPROTONOSUPPORT;
14544 			goto err_free;
14545 		}
14546 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
14547 	}
14548 	flags = fkeys->control.flags;
14549 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
14550 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
14551 		rc = -EPROTONOSUPPORT;
14552 		goto err_free;
14553 	}
14554 	new_fltr->l2_fltr = l2_fltr;
14555 
14556 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
14557 	rcu_read_lock();
14558 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
14559 	if (fltr) {
14560 		rc = fltr->base.sw_id;
14561 		rcu_read_unlock();
14562 		goto err_free;
14563 	}
14564 	rcu_read_unlock();
14565 
14566 	new_fltr->flow_id = flow_id;
14567 	new_fltr->base.rxq = rxq_index;
14568 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
14569 	if (!rc) {
14570 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14571 		return new_fltr->base.sw_id;
14572 	}
14573 
14574 err_free:
14575 	bnxt_del_l2_filter(bp, l2_fltr);
14576 	kfree(new_fltr);
14577 	return rc;
14578 }
14579 #endif
14580 
14581 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
14582 {
14583 	spin_lock_bh(&bp->ntp_fltr_lock);
14584 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
14585 		spin_unlock_bh(&bp->ntp_fltr_lock);
14586 		return;
14587 	}
14588 	hlist_del_rcu(&fltr->base.hash);
14589 	bnxt_del_one_usr_fltr(bp, &fltr->base);
14590 	bp->ntp_fltr_count--;
14591 	spin_unlock_bh(&bp->ntp_fltr_lock);
14592 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
14593 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
14594 	kfree_rcu(fltr, base.rcu);
14595 }
14596 
14597 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
14598 {
14599 #ifdef CONFIG_RFS_ACCEL
14600 	int i;
14601 
14602 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
14603 		struct hlist_head *head;
14604 		struct hlist_node *tmp;
14605 		struct bnxt_ntuple_filter *fltr;
14606 		int rc;
14607 
14608 		head = &bp->ntp_fltr_hash_tbl[i];
14609 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
14610 			bool del = false;
14611 
14612 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
14613 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
14614 					continue;
14615 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
14616 							fltr->flow_id,
14617 							fltr->base.sw_id)) {
14618 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
14619 									 fltr);
14620 					del = true;
14621 				}
14622 			} else {
14623 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
14624 								       fltr);
14625 				if (rc)
14626 					del = true;
14627 				else
14628 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
14629 			}
14630 
14631 			if (del)
14632 				bnxt_del_ntp_filter(bp, fltr);
14633 		}
14634 	}
14635 #endif
14636 }
14637 
14638 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
14639 				    unsigned int entry, struct udp_tunnel_info *ti)
14640 {
14641 	struct bnxt *bp = netdev_priv(netdev);
14642 	unsigned int cmd;
14643 
14644 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
14645 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
14646 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
14647 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
14648 	else
14649 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
14650 
14651 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
14652 }
14653 
14654 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
14655 				      unsigned int entry, struct udp_tunnel_info *ti)
14656 {
14657 	struct bnxt *bp = netdev_priv(netdev);
14658 	unsigned int cmd;
14659 
14660 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
14661 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
14662 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
14663 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
14664 	else
14665 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
14666 
14667 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
14668 }
14669 
14670 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
14671 	.set_port	= bnxt_udp_tunnel_set_port,
14672 	.unset_port	= bnxt_udp_tunnel_unset_port,
14673 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
14674 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
14675 	.tables		= {
14676 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
14677 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
14678 	},
14679 }, bnxt_udp_tunnels_p7 = {
14680 	.set_port	= bnxt_udp_tunnel_set_port,
14681 	.unset_port	= bnxt_udp_tunnel_unset_port,
14682 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
14683 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
14684 	.tables		= {
14685 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
14686 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
14687 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
14688 	},
14689 };
14690 
14691 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
14692 			       struct net_device *dev, u32 filter_mask,
14693 			       int nlflags)
14694 {
14695 	struct bnxt *bp = netdev_priv(dev);
14696 
14697 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
14698 				       nlflags, filter_mask, NULL);
14699 }
14700 
14701 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
14702 			       u16 flags, struct netlink_ext_ack *extack)
14703 {
14704 	struct bnxt *bp = netdev_priv(dev);
14705 	struct nlattr *attr, *br_spec;
14706 	int rem, rc = 0;
14707 
14708 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
14709 		return -EOPNOTSUPP;
14710 
14711 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
14712 	if (!br_spec)
14713 		return -EINVAL;
14714 
14715 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
14716 		u16 mode;
14717 
14718 		mode = nla_get_u16(attr);
14719 		if (mode == bp->br_mode)
14720 			break;
14721 
14722 		rc = bnxt_hwrm_set_br_mode(bp, mode);
14723 		if (!rc)
14724 			bp->br_mode = mode;
14725 		break;
14726 	}
14727 	return rc;
14728 }
14729 
14730 int bnxt_get_port_parent_id(struct net_device *dev,
14731 			    struct netdev_phys_item_id *ppid)
14732 {
14733 	struct bnxt *bp = netdev_priv(dev);
14734 
14735 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
14736 		return -EOPNOTSUPP;
14737 
14738 	/* The PF and it's VF-reps only support the switchdev framework */
14739 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
14740 		return -EOPNOTSUPP;
14741 
14742 	ppid->id_len = sizeof(bp->dsn);
14743 	memcpy(ppid->id, bp->dsn, ppid->id_len);
14744 
14745 	return 0;
14746 }
14747 
14748 static const struct net_device_ops bnxt_netdev_ops = {
14749 	.ndo_open		= bnxt_open,
14750 	.ndo_start_xmit		= bnxt_start_xmit,
14751 	.ndo_stop		= bnxt_close,
14752 	.ndo_get_stats64	= bnxt_get_stats64,
14753 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
14754 	.ndo_eth_ioctl		= bnxt_ioctl,
14755 	.ndo_validate_addr	= eth_validate_addr,
14756 	.ndo_set_mac_address	= bnxt_change_mac_addr,
14757 	.ndo_change_mtu		= bnxt_change_mtu,
14758 	.ndo_fix_features	= bnxt_fix_features,
14759 	.ndo_set_features	= bnxt_set_features,
14760 	.ndo_features_check	= bnxt_features_check,
14761 	.ndo_tx_timeout		= bnxt_tx_timeout,
14762 #ifdef CONFIG_BNXT_SRIOV
14763 	.ndo_get_vf_config	= bnxt_get_vf_config,
14764 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
14765 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
14766 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
14767 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
14768 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
14769 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
14770 #endif
14771 	.ndo_setup_tc           = bnxt_setup_tc,
14772 #ifdef CONFIG_RFS_ACCEL
14773 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
14774 #endif
14775 	.ndo_bpf		= bnxt_xdp,
14776 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
14777 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
14778 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
14779 };
14780 
14781 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
14782 				    struct netdev_queue_stats_rx *stats)
14783 {
14784 	struct bnxt *bp = netdev_priv(dev);
14785 	struct bnxt_cp_ring_info *cpr;
14786 	u64 *sw;
14787 
14788 	cpr = &bp->bnapi[i]->cp_ring;
14789 	sw = cpr->stats.sw_stats;
14790 
14791 	stats->packets = 0;
14792 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
14793 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
14794 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
14795 
14796 	stats->bytes = 0;
14797 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
14798 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
14799 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
14800 
14801 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
14802 }
14803 
14804 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
14805 				    struct netdev_queue_stats_tx *stats)
14806 {
14807 	struct bnxt *bp = netdev_priv(dev);
14808 	struct bnxt_napi *bnapi;
14809 	u64 *sw;
14810 
14811 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
14812 	sw = bnapi->cp_ring.stats.sw_stats;
14813 
14814 	stats->packets = 0;
14815 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
14816 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
14817 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
14818 
14819 	stats->bytes = 0;
14820 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
14821 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
14822 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
14823 }
14824 
14825 static void bnxt_get_base_stats(struct net_device *dev,
14826 				struct netdev_queue_stats_rx *rx,
14827 				struct netdev_queue_stats_tx *tx)
14828 {
14829 	struct bnxt *bp = netdev_priv(dev);
14830 
14831 	rx->packets = bp->net_stats_prev.rx_packets;
14832 	rx->bytes = bp->net_stats_prev.rx_bytes;
14833 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
14834 
14835 	tx->packets = bp->net_stats_prev.tx_packets;
14836 	tx->bytes = bp->net_stats_prev.tx_bytes;
14837 }
14838 
14839 static const struct netdev_stat_ops bnxt_stat_ops = {
14840 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
14841 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
14842 	.get_base_stats		= bnxt_get_base_stats,
14843 };
14844 
14845 static void bnxt_remove_one(struct pci_dev *pdev)
14846 {
14847 	struct net_device *dev = pci_get_drvdata(pdev);
14848 	struct bnxt *bp = netdev_priv(dev);
14849 
14850 	if (BNXT_PF(bp))
14851 		bnxt_sriov_disable(bp);
14852 
14853 	bnxt_rdma_aux_device_del(bp);
14854 
14855 	bnxt_ptp_clear(bp);
14856 	unregister_netdev(dev);
14857 
14858 	bnxt_rdma_aux_device_uninit(bp);
14859 
14860 	bnxt_free_l2_filters(bp, true);
14861 	bnxt_free_ntp_fltrs(bp, true);
14862 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
14863 		bnxt_clear_rss_ctxs(bp, true);
14864 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14865 	/* Flush any pending tasks */
14866 	cancel_work_sync(&bp->sp_task);
14867 	cancel_delayed_work_sync(&bp->fw_reset_task);
14868 	bp->sp_event = 0;
14869 
14870 	bnxt_dl_fw_reporters_destroy(bp);
14871 	bnxt_dl_unregister(bp);
14872 	bnxt_shutdown_tc(bp);
14873 
14874 	bnxt_clear_int_mode(bp);
14875 	bnxt_hwrm_func_drv_unrgtr(bp);
14876 	bnxt_free_hwrm_resources(bp);
14877 	bnxt_hwmon_uninit(bp);
14878 	bnxt_ethtool_free(bp);
14879 	bnxt_dcb_free(bp);
14880 	kfree(bp->ptp_cfg);
14881 	bp->ptp_cfg = NULL;
14882 	kfree(bp->fw_health);
14883 	bp->fw_health = NULL;
14884 	bnxt_cleanup_pci(bp);
14885 	bnxt_free_ctx_mem(bp);
14886 	kfree(bp->rss_indir_tbl);
14887 	bp->rss_indir_tbl = NULL;
14888 	bnxt_free_port_stats(bp);
14889 	free_netdev(dev);
14890 }
14891 
14892 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
14893 {
14894 	int rc = 0;
14895 	struct bnxt_link_info *link_info = &bp->link_info;
14896 
14897 	bp->phy_flags = 0;
14898 	rc = bnxt_hwrm_phy_qcaps(bp);
14899 	if (rc) {
14900 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
14901 			   rc);
14902 		return rc;
14903 	}
14904 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
14905 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
14906 	else
14907 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
14908 	if (!fw_dflt)
14909 		return 0;
14910 
14911 	mutex_lock(&bp->link_lock);
14912 	rc = bnxt_update_link(bp, false);
14913 	if (rc) {
14914 		mutex_unlock(&bp->link_lock);
14915 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
14916 			   rc);
14917 		return rc;
14918 	}
14919 
14920 	/* Older firmware does not have supported_auto_speeds, so assume
14921 	 * that all supported speeds can be autonegotiated.
14922 	 */
14923 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
14924 		link_info->support_auto_speeds = link_info->support_speeds;
14925 
14926 	bnxt_init_ethtool_link_settings(bp);
14927 	mutex_unlock(&bp->link_lock);
14928 	return 0;
14929 }
14930 
14931 static int bnxt_get_max_irq(struct pci_dev *pdev)
14932 {
14933 	u16 ctrl;
14934 
14935 	if (!pdev->msix_cap)
14936 		return 1;
14937 
14938 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
14939 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
14940 }
14941 
14942 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14943 				int *max_cp)
14944 {
14945 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
14946 	int max_ring_grps = 0, max_irq;
14947 
14948 	*max_tx = hw_resc->max_tx_rings;
14949 	*max_rx = hw_resc->max_rx_rings;
14950 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
14951 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
14952 			bnxt_get_ulp_msix_num_in_use(bp),
14953 			hw_resc->max_stat_ctxs -
14954 			bnxt_get_ulp_stat_ctxs_in_use(bp));
14955 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14956 		*max_cp = min_t(int, *max_cp, max_irq);
14957 	max_ring_grps = hw_resc->max_hw_ring_grps;
14958 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
14959 		*max_cp -= 1;
14960 		*max_rx -= 2;
14961 	}
14962 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14963 		*max_rx >>= 1;
14964 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
14965 		int rc;
14966 
14967 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
14968 		if (rc) {
14969 			*max_rx = 0;
14970 			*max_tx = 0;
14971 		}
14972 		/* On P5 chips, max_cp output param should be available NQs */
14973 		*max_cp = max_irq;
14974 	}
14975 	*max_rx = min_t(int, *max_rx, max_ring_grps);
14976 }
14977 
14978 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
14979 {
14980 	int rx, tx, cp;
14981 
14982 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
14983 	*max_rx = rx;
14984 	*max_tx = tx;
14985 	if (!rx || !tx || !cp)
14986 		return -ENOMEM;
14987 
14988 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
14989 }
14990 
14991 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14992 			       bool shared)
14993 {
14994 	int rc;
14995 
14996 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
14997 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
14998 		/* Not enough rings, try disabling agg rings. */
14999 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
15000 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
15001 		if (rc) {
15002 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
15003 			bp->flags |= BNXT_FLAG_AGG_RINGS;
15004 			return rc;
15005 		}
15006 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
15007 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
15008 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
15009 		bnxt_set_ring_params(bp);
15010 	}
15011 
15012 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
15013 		int max_cp, max_stat, max_irq;
15014 
15015 		/* Reserve minimum resources for RoCE */
15016 		max_cp = bnxt_get_max_func_cp_rings(bp);
15017 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
15018 		max_irq = bnxt_get_max_func_irqs(bp);
15019 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
15020 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
15021 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
15022 			return 0;
15023 
15024 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
15025 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
15026 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
15027 		max_cp = min_t(int, max_cp, max_irq);
15028 		max_cp = min_t(int, max_cp, max_stat);
15029 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
15030 		if (rc)
15031 			rc = 0;
15032 	}
15033 	return rc;
15034 }
15035 
15036 /* In initial default shared ring setting, each shared ring must have a
15037  * RX/TX ring pair.
15038  */
15039 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
15040 {
15041 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
15042 	bp->rx_nr_rings = bp->cp_nr_rings;
15043 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
15044 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15045 }
15046 
15047 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
15048 {
15049 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
15050 	int avail_msix;
15051 
15052 	if (!bnxt_can_reserve_rings(bp))
15053 		return 0;
15054 
15055 	if (sh)
15056 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
15057 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
15058 	/* Reduce default rings on multi-port cards so that total default
15059 	 * rings do not exceed CPU count.
15060 	 */
15061 	if (bp->port_count > 1) {
15062 		int max_rings =
15063 			max_t(int, num_online_cpus() / bp->port_count, 1);
15064 
15065 		dflt_rings = min_t(int, dflt_rings, max_rings);
15066 	}
15067 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
15068 	if (rc)
15069 		return rc;
15070 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
15071 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
15072 	if (sh)
15073 		bnxt_trim_dflt_sh_rings(bp);
15074 	else
15075 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
15076 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15077 
15078 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
15079 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
15080 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
15081 
15082 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
15083 		bnxt_set_dflt_ulp_stat_ctxs(bp);
15084 	}
15085 
15086 	rc = __bnxt_reserve_rings(bp);
15087 	if (rc && rc != -ENODEV)
15088 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
15089 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15090 	if (sh)
15091 		bnxt_trim_dflt_sh_rings(bp);
15092 
15093 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
15094 	if (bnxt_need_reserve_rings(bp)) {
15095 		rc = __bnxt_reserve_rings(bp);
15096 		if (rc && rc != -ENODEV)
15097 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
15098 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15099 	}
15100 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
15101 		bp->rx_nr_rings++;
15102 		bp->cp_nr_rings++;
15103 	}
15104 	if (rc) {
15105 		bp->tx_nr_rings = 0;
15106 		bp->rx_nr_rings = 0;
15107 	}
15108 	return rc;
15109 }
15110 
15111 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
15112 {
15113 	int rc;
15114 
15115 	if (bp->tx_nr_rings)
15116 		return 0;
15117 
15118 	bnxt_ulp_irq_stop(bp);
15119 	bnxt_clear_int_mode(bp);
15120 	rc = bnxt_set_dflt_rings(bp, true);
15121 	if (rc) {
15122 		if (BNXT_VF(bp) && rc == -ENODEV)
15123 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
15124 		else
15125 			netdev_err(bp->dev, "Not enough rings available.\n");
15126 		goto init_dflt_ring_err;
15127 	}
15128 	rc = bnxt_init_int_mode(bp);
15129 	if (rc)
15130 		goto init_dflt_ring_err;
15131 
15132 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15133 
15134 	bnxt_set_dflt_rfs(bp);
15135 
15136 init_dflt_ring_err:
15137 	bnxt_ulp_irq_restart(bp, rc);
15138 	return rc;
15139 }
15140 
15141 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
15142 {
15143 	int rc;
15144 
15145 	ASSERT_RTNL();
15146 	bnxt_hwrm_func_qcaps(bp);
15147 
15148 	if (netif_running(bp->dev))
15149 		__bnxt_close_nic(bp, true, false);
15150 
15151 	bnxt_ulp_irq_stop(bp);
15152 	bnxt_clear_int_mode(bp);
15153 	rc = bnxt_init_int_mode(bp);
15154 	bnxt_ulp_irq_restart(bp, rc);
15155 
15156 	if (netif_running(bp->dev)) {
15157 		if (rc)
15158 			dev_close(bp->dev);
15159 		else
15160 			rc = bnxt_open_nic(bp, true, false);
15161 	}
15162 
15163 	return rc;
15164 }
15165 
15166 static int bnxt_init_mac_addr(struct bnxt *bp)
15167 {
15168 	int rc = 0;
15169 
15170 	if (BNXT_PF(bp)) {
15171 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
15172 	} else {
15173 #ifdef CONFIG_BNXT_SRIOV
15174 		struct bnxt_vf_info *vf = &bp->vf;
15175 		bool strict_approval = true;
15176 
15177 		if (is_valid_ether_addr(vf->mac_addr)) {
15178 			/* overwrite netdev dev_addr with admin VF MAC */
15179 			eth_hw_addr_set(bp->dev, vf->mac_addr);
15180 			/* Older PF driver or firmware may not approve this
15181 			 * correctly.
15182 			 */
15183 			strict_approval = false;
15184 		} else {
15185 			eth_hw_addr_random(bp->dev);
15186 		}
15187 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
15188 #endif
15189 	}
15190 	return rc;
15191 }
15192 
15193 static void bnxt_vpd_read_info(struct bnxt *bp)
15194 {
15195 	struct pci_dev *pdev = bp->pdev;
15196 	unsigned int vpd_size, kw_len;
15197 	int pos, size;
15198 	u8 *vpd_data;
15199 
15200 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
15201 	if (IS_ERR(vpd_data)) {
15202 		pci_warn(pdev, "Unable to read VPD\n");
15203 		return;
15204 	}
15205 
15206 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
15207 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
15208 	if (pos < 0)
15209 		goto read_sn;
15210 
15211 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
15212 	memcpy(bp->board_partno, &vpd_data[pos], size);
15213 
15214 read_sn:
15215 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
15216 					   PCI_VPD_RO_KEYWORD_SERIALNO,
15217 					   &kw_len);
15218 	if (pos < 0)
15219 		goto exit;
15220 
15221 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
15222 	memcpy(bp->board_serialno, &vpd_data[pos], size);
15223 exit:
15224 	kfree(vpd_data);
15225 }
15226 
15227 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
15228 {
15229 	struct pci_dev *pdev = bp->pdev;
15230 	u64 qword;
15231 
15232 	qword = pci_get_dsn(pdev);
15233 	if (!qword) {
15234 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
15235 		return -EOPNOTSUPP;
15236 	}
15237 
15238 	put_unaligned_le64(qword, dsn);
15239 
15240 	bp->flags |= BNXT_FLAG_DSN_VALID;
15241 	return 0;
15242 }
15243 
15244 static int bnxt_map_db_bar(struct bnxt *bp)
15245 {
15246 	if (!bp->db_size)
15247 		return -ENODEV;
15248 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
15249 	if (!bp->bar1)
15250 		return -ENOMEM;
15251 	return 0;
15252 }
15253 
15254 void bnxt_print_device_info(struct bnxt *bp)
15255 {
15256 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
15257 		    board_info[bp->board_idx].name,
15258 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
15259 
15260 	pcie_print_link_status(bp->pdev);
15261 }
15262 
15263 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
15264 {
15265 	struct bnxt_hw_resc *hw_resc;
15266 	struct net_device *dev;
15267 	struct bnxt *bp;
15268 	int rc, max_irqs;
15269 
15270 	if (pci_is_bridge(pdev))
15271 		return -ENODEV;
15272 
15273 	/* Clear any pending DMA transactions from crash kernel
15274 	 * while loading driver in capture kernel.
15275 	 */
15276 	if (is_kdump_kernel()) {
15277 		pci_clear_master(pdev);
15278 		pcie_flr(pdev);
15279 	}
15280 
15281 	max_irqs = bnxt_get_max_irq(pdev);
15282 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
15283 				 max_irqs);
15284 	if (!dev)
15285 		return -ENOMEM;
15286 
15287 	bp = netdev_priv(dev);
15288 	bp->board_idx = ent->driver_data;
15289 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
15290 	bnxt_set_max_func_irqs(bp, max_irqs);
15291 
15292 	if (bnxt_vf_pciid(bp->board_idx))
15293 		bp->flags |= BNXT_FLAG_VF;
15294 
15295 	/* No devlink port registration in case of a VF */
15296 	if (BNXT_PF(bp))
15297 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
15298 
15299 	if (pdev->msix_cap)
15300 		bp->flags |= BNXT_FLAG_MSIX_CAP;
15301 
15302 	rc = bnxt_init_board(pdev, dev);
15303 	if (rc < 0)
15304 		goto init_err_free;
15305 
15306 	dev->netdev_ops = &bnxt_netdev_ops;
15307 	dev->stat_ops = &bnxt_stat_ops;
15308 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
15309 	dev->ethtool_ops = &bnxt_ethtool_ops;
15310 	pci_set_drvdata(pdev, dev);
15311 
15312 	rc = bnxt_alloc_hwrm_resources(bp);
15313 	if (rc)
15314 		goto init_err_pci_clean;
15315 
15316 	mutex_init(&bp->hwrm_cmd_lock);
15317 	mutex_init(&bp->link_lock);
15318 
15319 	rc = bnxt_fw_init_one_p1(bp);
15320 	if (rc)
15321 		goto init_err_pci_clean;
15322 
15323 	if (BNXT_PF(bp))
15324 		bnxt_vpd_read_info(bp);
15325 
15326 	if (BNXT_CHIP_P5_PLUS(bp)) {
15327 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
15328 		if (BNXT_CHIP_P7(bp))
15329 			bp->flags |= BNXT_FLAG_CHIP_P7;
15330 	}
15331 
15332 	rc = bnxt_alloc_rss_indir_tbl(bp, NULL);
15333 	if (rc)
15334 		goto init_err_pci_clean;
15335 
15336 	rc = bnxt_fw_init_one_p2(bp);
15337 	if (rc)
15338 		goto init_err_pci_clean;
15339 
15340 	rc = bnxt_map_db_bar(bp);
15341 	if (rc) {
15342 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
15343 			rc);
15344 		goto init_err_pci_clean;
15345 	}
15346 
15347 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
15348 			   NETIF_F_TSO | NETIF_F_TSO6 |
15349 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
15350 			   NETIF_F_GSO_IPXIP4 |
15351 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
15352 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
15353 			   NETIF_F_RXCSUM | NETIF_F_GRO;
15354 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
15355 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
15356 
15357 	if (BNXT_SUPPORTS_TPA(bp))
15358 		dev->hw_features |= NETIF_F_LRO;
15359 
15360 	dev->hw_enc_features =
15361 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
15362 			NETIF_F_TSO | NETIF_F_TSO6 |
15363 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
15364 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
15365 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
15366 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
15367 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
15368 	if (bp->flags & BNXT_FLAG_CHIP_P7)
15369 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
15370 	else
15371 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
15372 
15373 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
15374 				    NETIF_F_GSO_GRE_CSUM;
15375 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
15376 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
15377 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
15378 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
15379 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
15380 	if (BNXT_SUPPORTS_TPA(bp))
15381 		dev->hw_features |= NETIF_F_GRO_HW;
15382 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
15383 	if (dev->features & NETIF_F_GRO_HW)
15384 		dev->features &= ~NETIF_F_LRO;
15385 	dev->priv_flags |= IFF_UNICAST_FLT;
15386 
15387 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
15388 	if (bp->tso_max_segs)
15389 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
15390 
15391 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
15392 			    NETDEV_XDP_ACT_RX_SG;
15393 
15394 #ifdef CONFIG_BNXT_SRIOV
15395 	init_waitqueue_head(&bp->sriov_cfg_wait);
15396 #endif
15397 	if (BNXT_SUPPORTS_TPA(bp)) {
15398 		bp->gro_func = bnxt_gro_func_5730x;
15399 		if (BNXT_CHIP_P4(bp))
15400 			bp->gro_func = bnxt_gro_func_5731x;
15401 		else if (BNXT_CHIP_P5_PLUS(bp))
15402 			bp->gro_func = bnxt_gro_func_5750x;
15403 	}
15404 	if (!BNXT_CHIP_P4_PLUS(bp))
15405 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
15406 
15407 	rc = bnxt_init_mac_addr(bp);
15408 	if (rc) {
15409 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
15410 		rc = -EADDRNOTAVAIL;
15411 		goto init_err_pci_clean;
15412 	}
15413 
15414 	if (BNXT_PF(bp)) {
15415 		/* Read the adapter's DSN to use as the eswitch switch_id */
15416 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
15417 	}
15418 
15419 	/* MTU range: 60 - FW defined max */
15420 	dev->min_mtu = ETH_ZLEN;
15421 	dev->max_mtu = bp->max_mtu;
15422 
15423 	rc = bnxt_probe_phy(bp, true);
15424 	if (rc)
15425 		goto init_err_pci_clean;
15426 
15427 	hw_resc = &bp->hw_resc;
15428 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
15429 		       BNXT_L2_FLTR_MAX_FLTR;
15430 	/* Older firmware may not report these filters properly */
15431 	if (bp->max_fltr < BNXT_MAX_FLTR)
15432 		bp->max_fltr = BNXT_MAX_FLTR;
15433 	bnxt_init_l2_fltr_tbl(bp);
15434 	bnxt_set_rx_skb_mode(bp, false);
15435 	bnxt_set_tpa_flags(bp);
15436 	bnxt_set_ring_params(bp);
15437 	bnxt_rdma_aux_device_init(bp);
15438 	rc = bnxt_set_dflt_rings(bp, true);
15439 	if (rc) {
15440 		if (BNXT_VF(bp) && rc == -ENODEV) {
15441 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
15442 		} else {
15443 			netdev_err(bp->dev, "Not enough rings available.\n");
15444 			rc = -ENOMEM;
15445 		}
15446 		goto init_err_pci_clean;
15447 	}
15448 
15449 	bnxt_fw_init_one_p3(bp);
15450 
15451 	bnxt_init_dflt_coal(bp);
15452 
15453 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
15454 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
15455 
15456 	rc = bnxt_init_int_mode(bp);
15457 	if (rc)
15458 		goto init_err_pci_clean;
15459 
15460 	/* No TC has been set yet and rings may have been trimmed due to
15461 	 * limited MSIX, so we re-initialize the TX rings per TC.
15462 	 */
15463 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15464 
15465 	if (BNXT_PF(bp)) {
15466 		if (!bnxt_pf_wq) {
15467 			bnxt_pf_wq =
15468 				create_singlethread_workqueue("bnxt_pf_wq");
15469 			if (!bnxt_pf_wq) {
15470 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
15471 				rc = -ENOMEM;
15472 				goto init_err_pci_clean;
15473 			}
15474 		}
15475 		rc = bnxt_init_tc(bp);
15476 		if (rc)
15477 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
15478 				   rc);
15479 	}
15480 
15481 	bnxt_inv_fw_health_reg(bp);
15482 	rc = bnxt_dl_register(bp);
15483 	if (rc)
15484 		goto init_err_dl;
15485 
15486 	INIT_LIST_HEAD(&bp->usr_fltr_list);
15487 
15488 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
15489 		bnxt_init_multi_rss_ctx(bp);
15490 
15491 
15492 	rc = register_netdev(dev);
15493 	if (rc)
15494 		goto init_err_cleanup;
15495 
15496 	bnxt_dl_fw_reporters_create(bp);
15497 
15498 	bnxt_rdma_aux_device_add(bp);
15499 
15500 	bnxt_print_device_info(bp);
15501 
15502 	pci_save_state(pdev);
15503 
15504 	return 0;
15505 init_err_cleanup:
15506 	bnxt_rdma_aux_device_uninit(bp);
15507 	bnxt_dl_unregister(bp);
15508 init_err_dl:
15509 	bnxt_shutdown_tc(bp);
15510 	bnxt_clear_int_mode(bp);
15511 
15512 init_err_pci_clean:
15513 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
15514 		bnxt_clear_rss_ctxs(bp, true);
15515 	bnxt_hwrm_func_drv_unrgtr(bp);
15516 	bnxt_free_hwrm_resources(bp);
15517 	bnxt_hwmon_uninit(bp);
15518 	bnxt_ethtool_free(bp);
15519 	bnxt_ptp_clear(bp);
15520 	kfree(bp->ptp_cfg);
15521 	bp->ptp_cfg = NULL;
15522 	kfree(bp->fw_health);
15523 	bp->fw_health = NULL;
15524 	bnxt_cleanup_pci(bp);
15525 	bnxt_free_ctx_mem(bp);
15526 	kfree(bp->rss_indir_tbl);
15527 	bp->rss_indir_tbl = NULL;
15528 
15529 init_err_free:
15530 	free_netdev(dev);
15531 	return rc;
15532 }
15533 
15534 static void bnxt_shutdown(struct pci_dev *pdev)
15535 {
15536 	struct net_device *dev = pci_get_drvdata(pdev);
15537 	struct bnxt *bp;
15538 
15539 	if (!dev)
15540 		return;
15541 
15542 	rtnl_lock();
15543 	bp = netdev_priv(dev);
15544 	if (!bp)
15545 		goto shutdown_exit;
15546 
15547 	if (netif_running(dev))
15548 		dev_close(dev);
15549 
15550 	bnxt_clear_int_mode(bp);
15551 	pci_disable_device(pdev);
15552 
15553 	if (system_state == SYSTEM_POWER_OFF) {
15554 		pci_wake_from_d3(pdev, bp->wol);
15555 		pci_set_power_state(pdev, PCI_D3hot);
15556 	}
15557 
15558 shutdown_exit:
15559 	rtnl_unlock();
15560 }
15561 
15562 #ifdef CONFIG_PM_SLEEP
15563 static int bnxt_suspend(struct device *device)
15564 {
15565 	struct net_device *dev = dev_get_drvdata(device);
15566 	struct bnxt *bp = netdev_priv(dev);
15567 	int rc = 0;
15568 
15569 	bnxt_ulp_stop(bp);
15570 
15571 	rtnl_lock();
15572 	if (netif_running(dev)) {
15573 		netif_device_detach(dev);
15574 		rc = bnxt_close(dev);
15575 	}
15576 	bnxt_hwrm_func_drv_unrgtr(bp);
15577 	pci_disable_device(bp->pdev);
15578 	bnxt_free_ctx_mem(bp);
15579 	rtnl_unlock();
15580 	return rc;
15581 }
15582 
15583 static int bnxt_resume(struct device *device)
15584 {
15585 	struct net_device *dev = dev_get_drvdata(device);
15586 	struct bnxt *bp = netdev_priv(dev);
15587 	int rc = 0;
15588 
15589 	rtnl_lock();
15590 	rc = pci_enable_device(bp->pdev);
15591 	if (rc) {
15592 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
15593 			   rc);
15594 		goto resume_exit;
15595 	}
15596 	pci_set_master(bp->pdev);
15597 	if (bnxt_hwrm_ver_get(bp)) {
15598 		rc = -ENODEV;
15599 		goto resume_exit;
15600 	}
15601 	rc = bnxt_hwrm_func_reset(bp);
15602 	if (rc) {
15603 		rc = -EBUSY;
15604 		goto resume_exit;
15605 	}
15606 
15607 	rc = bnxt_hwrm_func_qcaps(bp);
15608 	if (rc)
15609 		goto resume_exit;
15610 
15611 	bnxt_clear_reservations(bp, true);
15612 
15613 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
15614 		rc = -ENODEV;
15615 		goto resume_exit;
15616 	}
15617 
15618 	bnxt_get_wol_settings(bp);
15619 	if (netif_running(dev)) {
15620 		rc = bnxt_open(dev);
15621 		if (!rc)
15622 			netif_device_attach(dev);
15623 	}
15624 
15625 resume_exit:
15626 	rtnl_unlock();
15627 	bnxt_ulp_start(bp, rc);
15628 	if (!rc)
15629 		bnxt_reenable_sriov(bp);
15630 	return rc;
15631 }
15632 
15633 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
15634 #define BNXT_PM_OPS (&bnxt_pm_ops)
15635 
15636 #else
15637 
15638 #define BNXT_PM_OPS NULL
15639 
15640 #endif /* CONFIG_PM_SLEEP */
15641 
15642 /**
15643  * bnxt_io_error_detected - called when PCI error is detected
15644  * @pdev: Pointer to PCI device
15645  * @state: The current pci connection state
15646  *
15647  * This function is called after a PCI bus error affecting
15648  * this device has been detected.
15649  */
15650 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
15651 					       pci_channel_state_t state)
15652 {
15653 	struct net_device *netdev = pci_get_drvdata(pdev);
15654 	struct bnxt *bp = netdev_priv(netdev);
15655 	bool abort = false;
15656 
15657 	netdev_info(netdev, "PCI I/O error detected\n");
15658 
15659 	bnxt_ulp_stop(bp);
15660 
15661 	rtnl_lock();
15662 	netif_device_detach(netdev);
15663 
15664 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
15665 		netdev_err(bp->dev, "Firmware reset already in progress\n");
15666 		abort = true;
15667 	}
15668 
15669 	if (abort || state == pci_channel_io_perm_failure) {
15670 		rtnl_unlock();
15671 		return PCI_ERS_RESULT_DISCONNECT;
15672 	}
15673 
15674 	/* Link is not reliable anymore if state is pci_channel_io_frozen
15675 	 * so we disable bus master to prevent any potential bad DMAs before
15676 	 * freeing kernel memory.
15677 	 */
15678 	if (state == pci_channel_io_frozen) {
15679 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
15680 		bnxt_fw_fatal_close(bp);
15681 	}
15682 
15683 	if (netif_running(netdev))
15684 		__bnxt_close_nic(bp, true, true);
15685 
15686 	if (pci_is_enabled(pdev))
15687 		pci_disable_device(pdev);
15688 	bnxt_free_ctx_mem(bp);
15689 	rtnl_unlock();
15690 
15691 	/* Request a slot slot reset. */
15692 	return PCI_ERS_RESULT_NEED_RESET;
15693 }
15694 
15695 /**
15696  * bnxt_io_slot_reset - called after the pci bus has been reset.
15697  * @pdev: Pointer to PCI device
15698  *
15699  * Restart the card from scratch, as if from a cold-boot.
15700  * At this point, the card has exprienced a hard reset,
15701  * followed by fixups by BIOS, and has its config space
15702  * set up identically to what it was at cold boot.
15703  */
15704 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
15705 {
15706 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
15707 	struct net_device *netdev = pci_get_drvdata(pdev);
15708 	struct bnxt *bp = netdev_priv(netdev);
15709 	int retry = 0;
15710 	int err = 0;
15711 	int off;
15712 
15713 	netdev_info(bp->dev, "PCI Slot Reset\n");
15714 
15715 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
15716 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
15717 		msleep(900);
15718 
15719 	rtnl_lock();
15720 
15721 	if (pci_enable_device(pdev)) {
15722 		dev_err(&pdev->dev,
15723 			"Cannot re-enable PCI device after reset.\n");
15724 	} else {
15725 		pci_set_master(pdev);
15726 		/* Upon fatal error, our device internal logic that latches to
15727 		 * BAR value is getting reset and will restore only upon
15728 		 * rewritting the BARs.
15729 		 *
15730 		 * As pci_restore_state() does not re-write the BARs if the
15731 		 * value is same as saved value earlier, driver needs to
15732 		 * write the BARs to 0 to force restore, in case of fatal error.
15733 		 */
15734 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
15735 				       &bp->state)) {
15736 			for (off = PCI_BASE_ADDRESS_0;
15737 			     off <= PCI_BASE_ADDRESS_5; off += 4)
15738 				pci_write_config_dword(bp->pdev, off, 0);
15739 		}
15740 		pci_restore_state(pdev);
15741 		pci_save_state(pdev);
15742 
15743 		bnxt_inv_fw_health_reg(bp);
15744 		bnxt_try_map_fw_health_reg(bp);
15745 
15746 		/* In some PCIe AER scenarios, firmware may take up to
15747 		 * 10 seconds to become ready in the worst case.
15748 		 */
15749 		do {
15750 			err = bnxt_try_recover_fw(bp);
15751 			if (!err)
15752 				break;
15753 			retry++;
15754 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
15755 
15756 		if (err) {
15757 			dev_err(&pdev->dev, "Firmware not ready\n");
15758 			goto reset_exit;
15759 		}
15760 
15761 		err = bnxt_hwrm_func_reset(bp);
15762 		if (!err)
15763 			result = PCI_ERS_RESULT_RECOVERED;
15764 
15765 		bnxt_ulp_irq_stop(bp);
15766 		bnxt_clear_int_mode(bp);
15767 		err = bnxt_init_int_mode(bp);
15768 		bnxt_ulp_irq_restart(bp, err);
15769 	}
15770 
15771 reset_exit:
15772 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15773 	bnxt_clear_reservations(bp, true);
15774 	rtnl_unlock();
15775 
15776 	return result;
15777 }
15778 
15779 /**
15780  * bnxt_io_resume - called when traffic can start flowing again.
15781  * @pdev: Pointer to PCI device
15782  *
15783  * This callback is called when the error recovery driver tells
15784  * us that its OK to resume normal operation.
15785  */
15786 static void bnxt_io_resume(struct pci_dev *pdev)
15787 {
15788 	struct net_device *netdev = pci_get_drvdata(pdev);
15789 	struct bnxt *bp = netdev_priv(netdev);
15790 	int err;
15791 
15792 	netdev_info(bp->dev, "PCI Slot Resume\n");
15793 	rtnl_lock();
15794 
15795 	err = bnxt_hwrm_func_qcaps(bp);
15796 	if (!err && netif_running(netdev))
15797 		err = bnxt_open(netdev);
15798 
15799 	if (!err)
15800 		netif_device_attach(netdev);
15801 
15802 	rtnl_unlock();
15803 	bnxt_ulp_start(bp, err);
15804 	if (!err)
15805 		bnxt_reenable_sriov(bp);
15806 }
15807 
15808 static const struct pci_error_handlers bnxt_err_handler = {
15809 	.error_detected	= bnxt_io_error_detected,
15810 	.slot_reset	= bnxt_io_slot_reset,
15811 	.resume		= bnxt_io_resume
15812 };
15813 
15814 static struct pci_driver bnxt_pci_driver = {
15815 	.name		= DRV_MODULE_NAME,
15816 	.id_table	= bnxt_pci_tbl,
15817 	.probe		= bnxt_init_one,
15818 	.remove		= bnxt_remove_one,
15819 	.shutdown	= bnxt_shutdown,
15820 	.driver.pm	= BNXT_PM_OPS,
15821 	.err_handler	= &bnxt_err_handler,
15822 #if defined(CONFIG_BNXT_SRIOV)
15823 	.sriov_configure = bnxt_sriov_configure,
15824 #endif
15825 };
15826 
15827 static int __init bnxt_init(void)
15828 {
15829 	int err;
15830 
15831 	bnxt_debug_init();
15832 	err = pci_register_driver(&bnxt_pci_driver);
15833 	if (err) {
15834 		bnxt_debug_exit();
15835 		return err;
15836 	}
15837 
15838 	return 0;
15839 }
15840 
15841 static void __exit bnxt_exit(void)
15842 {
15843 	pci_unregister_driver(&bnxt_pci_driver);
15844 	if (bnxt_pf_wq)
15845 		destroy_workqueue(bnxt_pf_wq);
15846 	bnxt_debug_exit();
15847 }
15848 
15849 module_init(bnxt_init);
15850 module_exit(bnxt_exit);
15851