xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision 8f8d74ee110c02137f5b78ca0a2bd6c10331f267)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  * Copyright (c) 2016-2019 Broadcom Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  */
10 
11 #include <linux/module.h>
12 
13 #include <linux/stringify.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/pci.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/bitops.h>
27 #include <linux/io.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/byteorder.h>
31 #include <asm/page.h>
32 #include <linux/time.h>
33 #include <linux/mii.h>
34 #include <linux/mdio.h>
35 #include <linux/if.h>
36 #include <linux/if_vlan.h>
37 #include <linux/if_bridge.h>
38 #include <linux/rtc.h>
39 #include <linux/bpf.h>
40 #include <net/gro.h>
41 #include <net/ip.h>
42 #include <net/tcp.h>
43 #include <net/udp.h>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
46 #include <net/udp_tunnel.h>
47 #include <linux/workqueue.h>
48 #include <linux/prefetch.h>
49 #include <linux/cache.h>
50 #include <linux/log2.h>
51 #include <linux/bitmap.h>
52 #include <linux/cpu_rmap.h>
53 #include <linux/cpumask.h>
54 #include <net/pkt_cls.h>
55 #include <net/page_pool/helpers.h>
56 #include <linux/align.h>
57 #include <net/netdev_queues.h>
58 
59 #include "bnxt_hsi.h"
60 #include "bnxt.h"
61 #include "bnxt_hwrm.h"
62 #include "bnxt_ulp.h"
63 #include "bnxt_sriov.h"
64 #include "bnxt_ethtool.h"
65 #include "bnxt_dcb.h"
66 #include "bnxt_xdp.h"
67 #include "bnxt_ptp.h"
68 #include "bnxt_vfr.h"
69 #include "bnxt_tc.h"
70 #include "bnxt_devlink.h"
71 #include "bnxt_debugfs.h"
72 #include "bnxt_hwmon.h"
73 
74 #define BNXT_TX_TIMEOUT		(5 * HZ)
75 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
76 				 NETIF_MSG_TX_ERR)
77 
78 MODULE_LICENSE("GPL");
79 MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
80 
81 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
82 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
83 #define BNXT_RX_COPY_THRESH 256
84 
85 #define BNXT_TX_PUSH_THRESH 164
86 
87 /* indexed by enum board_idx */
88 static const struct {
89 	char *name;
90 } board_info[] = {
91 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
92 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
93 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
94 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
95 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
96 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
97 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
98 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
99 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
100 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
101 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
102 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
103 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
104 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
105 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
106 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
108 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
109 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
110 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
111 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
112 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
113 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
114 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
115 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
116 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
117 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
118 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
119 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
120 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
121 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
122 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
123 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
124 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
125 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
126 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
127 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
128 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
129 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
130 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
131 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
132 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
133 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
134 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
135 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
136 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
137 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
138 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
139 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
140 };
141 
142 static const struct pci_device_id bnxt_pci_tbl[] = {
143 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
144 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
145 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
146 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
147 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
148 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
149 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
150 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
151 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
152 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
153 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
155 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
158 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
159 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
160 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
164 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
165 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
166 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
167 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
168 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
169 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
170 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
171 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
172 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
173 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
174 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
175 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
176 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
177 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
178 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
179 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
180 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
181 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
182 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
183 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
184 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
185 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
186 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
187 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
188 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
189 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
190 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
191 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
192 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
193 #ifdef CONFIG_BNXT_SRIOV
194 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
195 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
196 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
197 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
198 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
199 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
200 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
201 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
202 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
203 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
204 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
205 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
206 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
207 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
208 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
209 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
210 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
211 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
212 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
213 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
214 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
215 #endif
216 	{ 0 }
217 };
218 
219 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
220 
221 static const u16 bnxt_vf_req_snif[] = {
222 	HWRM_FUNC_CFG,
223 	HWRM_FUNC_VF_CFG,
224 	HWRM_PORT_PHY_QCFG,
225 	HWRM_CFA_L2_FILTER_ALLOC,
226 };
227 
228 static const u16 bnxt_async_events_arr[] = {
229 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
230 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
231 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
232 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
233 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
234 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
235 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
236 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
237 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
238 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
239 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
240 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
241 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
242 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
243 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
244 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
245 };
246 
247 static struct workqueue_struct *bnxt_pf_wq;
248 
249 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
250 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
251 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
252 
253 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
254 	.ports = {
255 		.src = 0,
256 		.dst = 0,
257 	},
258 	.addrs = {
259 		.v6addrs = {
260 			.src = BNXT_IPV6_MASK_NONE,
261 			.dst = BNXT_IPV6_MASK_NONE,
262 		},
263 	},
264 };
265 
266 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
267 	.ports = {
268 		.src = cpu_to_be16(0xffff),
269 		.dst = cpu_to_be16(0xffff),
270 	},
271 	.addrs = {
272 		.v6addrs = {
273 			.src = BNXT_IPV6_MASK_ALL,
274 			.dst = BNXT_IPV6_MASK_ALL,
275 		},
276 	},
277 };
278 
279 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
280 	.ports = {
281 		.src = cpu_to_be16(0xffff),
282 		.dst = cpu_to_be16(0xffff),
283 	},
284 	.addrs = {
285 		.v4addrs = {
286 			.src = cpu_to_be32(0xffffffff),
287 			.dst = cpu_to_be32(0xffffffff),
288 		},
289 	},
290 };
291 
292 static bool bnxt_vf_pciid(enum board_idx idx)
293 {
294 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
295 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
296 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
297 		idx == NETXTREME_E_P5_VF_HV);
298 }
299 
300 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
301 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
302 #define DB_CP_IRQ_DIS_FLAGS	(DB_KEY_CP | DB_IRQ_DIS)
303 
304 #define BNXT_CP_DB_IRQ_DIS(db)						\
305 		writel(DB_CP_IRQ_DIS_FLAGS, db)
306 
307 #define BNXT_DB_CQ(db, idx)						\
308 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
309 
310 #define BNXT_DB_NQ_P5(db, idx)						\
311 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
312 		    (db)->doorbell)
313 
314 #define BNXT_DB_NQ_P7(db, idx)						\
315 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
316 		    DB_RING_IDX(db, idx), (db)->doorbell)
317 
318 #define BNXT_DB_CQ_ARM(db, idx)						\
319 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
320 
321 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
322 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
323 		    DB_RING_IDX(db, idx), (db)->doorbell)
324 
325 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
326 {
327 	if (bp->flags & BNXT_FLAG_CHIP_P7)
328 		BNXT_DB_NQ_P7(db, idx);
329 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
330 		BNXT_DB_NQ_P5(db, idx);
331 	else
332 		BNXT_DB_CQ(db, idx);
333 }
334 
335 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
336 {
337 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
338 		BNXT_DB_NQ_ARM_P5(db, idx);
339 	else
340 		BNXT_DB_CQ_ARM(db, idx);
341 }
342 
343 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
344 {
345 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
346 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
347 			    DB_RING_IDX(db, idx), db->doorbell);
348 	else
349 		BNXT_DB_CQ(db, idx);
350 }
351 
352 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
353 {
354 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
355 		return;
356 
357 	if (BNXT_PF(bp))
358 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
359 	else
360 		schedule_delayed_work(&bp->fw_reset_task, delay);
361 }
362 
363 static void __bnxt_queue_sp_work(struct bnxt *bp)
364 {
365 	if (BNXT_PF(bp))
366 		queue_work(bnxt_pf_wq, &bp->sp_task);
367 	else
368 		schedule_work(&bp->sp_task);
369 }
370 
371 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
372 {
373 	set_bit(event, &bp->sp_event);
374 	__bnxt_queue_sp_work(bp);
375 }
376 
377 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
378 {
379 	if (!rxr->bnapi->in_reset) {
380 		rxr->bnapi->in_reset = true;
381 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
382 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
383 		else
384 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
385 		__bnxt_queue_sp_work(bp);
386 	}
387 	rxr->rx_next_cons = 0xffff;
388 }
389 
390 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
391 			  u16 curr)
392 {
393 	struct bnxt_napi *bnapi = txr->bnapi;
394 
395 	if (bnapi->tx_fault)
396 		return;
397 
398 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
399 		   txr->txq_index, txr->tx_hw_cons,
400 		   txr->tx_cons, txr->tx_prod, curr);
401 	WARN_ON_ONCE(1);
402 	bnapi->tx_fault = 1;
403 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
404 }
405 
406 const u16 bnxt_lhint_arr[] = {
407 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
408 	TX_BD_FLAGS_LHINT_512_TO_1023,
409 	TX_BD_FLAGS_LHINT_1024_TO_2047,
410 	TX_BD_FLAGS_LHINT_1024_TO_2047,
411 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
412 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
413 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
414 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
415 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
416 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
417 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
418 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
419 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
420 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
421 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
422 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
423 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
424 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
425 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
426 };
427 
428 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
429 {
430 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
431 
432 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
433 		return 0;
434 
435 	return md_dst->u.port_info.port_id;
436 }
437 
438 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
439 			     u16 prod)
440 {
441 	/* Sync BD data before updating doorbell */
442 	wmb();
443 	bnxt_db_write(bp, &txr->tx_db, prod);
444 	txr->kick_pending = 0;
445 }
446 
447 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
448 {
449 	struct bnxt *bp = netdev_priv(dev);
450 	struct tx_bd *txbd, *txbd0;
451 	struct tx_bd_ext *txbd1;
452 	struct netdev_queue *txq;
453 	int i;
454 	dma_addr_t mapping;
455 	unsigned int length, pad = 0;
456 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
457 	u16 prod, last_frag;
458 	struct pci_dev *pdev = bp->pdev;
459 	struct bnxt_tx_ring_info *txr;
460 	struct bnxt_sw_tx_bd *tx_buf;
461 	__le32 lflags = 0;
462 
463 	i = skb_get_queue_mapping(skb);
464 	if (unlikely(i >= bp->tx_nr_rings)) {
465 		dev_kfree_skb_any(skb);
466 		dev_core_stats_tx_dropped_inc(dev);
467 		return NETDEV_TX_OK;
468 	}
469 
470 	txq = netdev_get_tx_queue(dev, i);
471 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
472 	prod = txr->tx_prod;
473 
474 	free_size = bnxt_tx_avail(bp, txr);
475 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
476 		/* We must have raced with NAPI cleanup */
477 		if (net_ratelimit() && txr->kick_pending)
478 			netif_warn(bp, tx_err, dev,
479 				   "bnxt: ring busy w/ flush pending!\n");
480 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
481 					bp->tx_wake_thresh))
482 			return NETDEV_TX_BUSY;
483 	}
484 
485 	if (unlikely(ipv6_hopopt_jumbo_remove(skb)))
486 		goto tx_free;
487 
488 	length = skb->len;
489 	len = skb_headlen(skb);
490 	last_frag = skb_shinfo(skb)->nr_frags;
491 
492 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
493 
494 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
495 	tx_buf->skb = skb;
496 	tx_buf->nr_frags = last_frag;
497 
498 	vlan_tag_flags = 0;
499 	cfa_action = bnxt_xmit_get_cfa_action(skb);
500 	if (skb_vlan_tag_present(skb)) {
501 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
502 				 skb_vlan_tag_get(skb);
503 		/* Currently supports 8021Q, 8021AD vlan offloads
504 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
505 		 */
506 		if (skb->vlan_proto == htons(ETH_P_8021Q))
507 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
508 	}
509 
510 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
511 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
512 
513 		if (ptp && ptp->tx_tstamp_en && !skb_is_gso(skb) &&
514 		    atomic_dec_if_positive(&ptp->tx_avail) >= 0) {
515 			if (!bnxt_ptp_parse(skb, &ptp->tx_seqid,
516 					    &ptp->tx_hdr_off)) {
517 				if (vlan_tag_flags)
518 					ptp->tx_hdr_off += VLAN_HLEN;
519 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
520 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
521 			} else {
522 				atomic_inc(&bp->ptp_cfg->tx_avail);
523 			}
524 		}
525 	}
526 
527 	if (unlikely(skb->no_fcs))
528 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
529 
530 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
531 	    !lflags) {
532 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
533 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
534 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
535 		void __iomem *db = txr->tx_db.doorbell;
536 		void *pdata = tx_push_buf->data;
537 		u64 *end;
538 		int j, push_len;
539 
540 		/* Set COAL_NOW to be ready quickly for the next push */
541 		tx_push->tx_bd_len_flags_type =
542 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
543 					TX_BD_TYPE_LONG_TX_BD |
544 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
545 					TX_BD_FLAGS_COAL_NOW |
546 					TX_BD_FLAGS_PACKET_END |
547 					(2 << TX_BD_FLAGS_BD_CNT_SHIFT));
548 
549 		if (skb->ip_summed == CHECKSUM_PARTIAL)
550 			tx_push1->tx_bd_hsize_lflags =
551 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
552 		else
553 			tx_push1->tx_bd_hsize_lflags = 0;
554 
555 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
556 		tx_push1->tx_bd_cfa_action =
557 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
558 
559 		end = pdata + length;
560 		end = PTR_ALIGN(end, 8) - 1;
561 		*end = 0;
562 
563 		skb_copy_from_linear_data(skb, pdata, len);
564 		pdata += len;
565 		for (j = 0; j < last_frag; j++) {
566 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
567 			void *fptr;
568 
569 			fptr = skb_frag_address_safe(frag);
570 			if (!fptr)
571 				goto normal_tx;
572 
573 			memcpy(pdata, fptr, skb_frag_size(frag));
574 			pdata += skb_frag_size(frag);
575 		}
576 
577 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
578 		txbd->tx_bd_haddr = txr->data_mapping;
579 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
580 		prod = NEXT_TX(prod);
581 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
582 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
583 		memcpy(txbd, tx_push1, sizeof(*txbd));
584 		prod = NEXT_TX(prod);
585 		tx_push->doorbell =
586 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
587 				    DB_RING_IDX(&txr->tx_db, prod));
588 		WRITE_ONCE(txr->tx_prod, prod);
589 
590 		tx_buf->is_push = 1;
591 		netdev_tx_sent_queue(txq, skb->len);
592 		wmb();	/* Sync is_push and byte queue before pushing data */
593 
594 		push_len = (length + sizeof(*tx_push) + 7) / 8;
595 		if (push_len > 16) {
596 			__iowrite64_copy(db, tx_push_buf, 16);
597 			__iowrite32_copy(db + 4, tx_push_buf + 1,
598 					 (push_len - 16) << 1);
599 		} else {
600 			__iowrite64_copy(db, tx_push_buf, push_len);
601 		}
602 
603 		goto tx_done;
604 	}
605 
606 normal_tx:
607 	if (length < BNXT_MIN_PKT_SIZE) {
608 		pad = BNXT_MIN_PKT_SIZE - length;
609 		if (skb_pad(skb, pad))
610 			/* SKB already freed. */
611 			goto tx_kick_pending;
612 		length = BNXT_MIN_PKT_SIZE;
613 	}
614 
615 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
616 
617 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
618 		goto tx_free;
619 
620 	dma_unmap_addr_set(tx_buf, mapping, mapping);
621 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
622 		((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
623 
624 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
625 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
626 
627 	prod = NEXT_TX(prod);
628 	txbd1 = (struct tx_bd_ext *)
629 		&txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
630 
631 	txbd1->tx_bd_hsize_lflags = lflags;
632 	if (skb_is_gso(skb)) {
633 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
634 		u32 hdr_len;
635 
636 		if (skb->encapsulation) {
637 			if (udp_gso)
638 				hdr_len = skb_inner_transport_offset(skb) +
639 					  sizeof(struct udphdr);
640 			else
641 				hdr_len = skb_inner_tcp_all_headers(skb);
642 		} else if (udp_gso) {
643 			hdr_len = skb_transport_offset(skb) +
644 				  sizeof(struct udphdr);
645 		} else {
646 			hdr_len = skb_tcp_all_headers(skb);
647 		}
648 
649 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
650 					TX_BD_FLAGS_T_IPID |
651 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
652 		length = skb_shinfo(skb)->gso_size;
653 		txbd1->tx_bd_mss = cpu_to_le32(length);
654 		length += hdr_len;
655 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
656 		txbd1->tx_bd_hsize_lflags |=
657 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
658 		txbd1->tx_bd_mss = 0;
659 	}
660 
661 	length >>= 9;
662 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
663 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
664 				     skb->len);
665 		i = 0;
666 		goto tx_dma_error;
667 	}
668 	flags |= bnxt_lhint_arr[length];
669 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
670 
671 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
672 	txbd1->tx_bd_cfa_action =
673 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
674 	txbd0 = txbd;
675 	for (i = 0; i < last_frag; i++) {
676 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
677 
678 		prod = NEXT_TX(prod);
679 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
680 
681 		len = skb_frag_size(frag);
682 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
683 					   DMA_TO_DEVICE);
684 
685 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
686 			goto tx_dma_error;
687 
688 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
689 		dma_unmap_addr_set(tx_buf, mapping, mapping);
690 
691 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
692 
693 		flags = len << TX_BD_LEN_SHIFT;
694 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
695 	}
696 
697 	flags &= ~TX_BD_LEN;
698 	txbd->tx_bd_len_flags_type =
699 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
700 			    TX_BD_FLAGS_PACKET_END);
701 
702 	netdev_tx_sent_queue(txq, skb->len);
703 
704 	skb_tx_timestamp(skb);
705 
706 	prod = NEXT_TX(prod);
707 	WRITE_ONCE(txr->tx_prod, prod);
708 
709 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
710 		bnxt_txr_db_kick(bp, txr, prod);
711 	} else {
712 		if (free_size >= bp->tx_wake_thresh)
713 			txbd0->tx_bd_len_flags_type |=
714 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
715 		txr->kick_pending = 1;
716 	}
717 
718 tx_done:
719 
720 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
721 		if (netdev_xmit_more() && !tx_buf->is_push) {
722 			txbd0->tx_bd_len_flags_type &=
723 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
724 			bnxt_txr_db_kick(bp, txr, prod);
725 		}
726 
727 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
728 				   bp->tx_wake_thresh);
729 	}
730 	return NETDEV_TX_OK;
731 
732 tx_dma_error:
733 	if (BNXT_TX_PTP_IS_SET(lflags))
734 		atomic_inc(&bp->ptp_cfg->tx_avail);
735 
736 	last_frag = i;
737 
738 	/* start back at beginning and unmap skb */
739 	prod = txr->tx_prod;
740 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
741 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
742 			 skb_headlen(skb), DMA_TO_DEVICE);
743 	prod = NEXT_TX(prod);
744 
745 	/* unmap remaining mapped pages */
746 	for (i = 0; i < last_frag; i++) {
747 		prod = NEXT_TX(prod);
748 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
749 		dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
750 			       skb_frag_size(&skb_shinfo(skb)->frags[i]),
751 			       DMA_TO_DEVICE);
752 	}
753 
754 tx_free:
755 	dev_kfree_skb_any(skb);
756 tx_kick_pending:
757 	if (txr->kick_pending)
758 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
759 	txr->tx_buf_ring[txr->tx_prod].skb = NULL;
760 	dev_core_stats_tx_dropped_inc(dev);
761 	return NETDEV_TX_OK;
762 }
763 
764 static void __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
765 			  int budget)
766 {
767 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
768 	struct pci_dev *pdev = bp->pdev;
769 	u16 hw_cons = txr->tx_hw_cons;
770 	unsigned int tx_bytes = 0;
771 	u16 cons = txr->tx_cons;
772 	int tx_pkts = 0;
773 
774 	while (RING_TX(bp, cons) != hw_cons) {
775 		struct bnxt_sw_tx_bd *tx_buf;
776 		struct sk_buff *skb;
777 		int j, last;
778 
779 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
780 		cons = NEXT_TX(cons);
781 		skb = tx_buf->skb;
782 		tx_buf->skb = NULL;
783 
784 		if (unlikely(!skb)) {
785 			bnxt_sched_reset_txr(bp, txr, cons);
786 			return;
787 		}
788 
789 		tx_pkts++;
790 		tx_bytes += skb->len;
791 
792 		if (tx_buf->is_push) {
793 			tx_buf->is_push = 0;
794 			goto next_tx_int;
795 		}
796 
797 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
798 				 skb_headlen(skb), DMA_TO_DEVICE);
799 		last = tx_buf->nr_frags;
800 
801 		for (j = 0; j < last; j++) {
802 			cons = NEXT_TX(cons);
803 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
804 			dma_unmap_page(
805 				&pdev->dev,
806 				dma_unmap_addr(tx_buf, mapping),
807 				skb_frag_size(&skb_shinfo(skb)->frags[j]),
808 				DMA_TO_DEVICE);
809 		}
810 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
811 			if (BNXT_CHIP_P5(bp)) {
812 				/* PTP worker takes ownership of the skb */
813 				if (!bnxt_get_tx_ts_p5(bp, skb))
814 					skb = NULL;
815 				else
816 					atomic_inc(&bp->ptp_cfg->tx_avail);
817 			}
818 		}
819 
820 next_tx_int:
821 		cons = NEXT_TX(cons);
822 
823 		dev_consume_skb_any(skb);
824 	}
825 
826 	WRITE_ONCE(txr->tx_cons, cons);
827 
828 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
829 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
830 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
831 }
832 
833 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
834 {
835 	struct bnxt_tx_ring_info *txr;
836 	int i;
837 
838 	bnxt_for_each_napi_tx(i, bnapi, txr) {
839 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
840 			__bnxt_tx_int(bp, txr, budget);
841 	}
842 	bnapi->events &= ~BNXT_TX_CMP_EVENT;
843 }
844 
845 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
846 					 struct bnxt_rx_ring_info *rxr,
847 					 unsigned int *offset,
848 					 gfp_t gfp)
849 {
850 	struct page *page;
851 
852 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
853 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
854 						BNXT_RX_PAGE_SIZE);
855 	} else {
856 		page = page_pool_dev_alloc_pages(rxr->page_pool);
857 		*offset = 0;
858 	}
859 	if (!page)
860 		return NULL;
861 
862 	*mapping = page_pool_get_dma_addr(page) + *offset;
863 	return page;
864 }
865 
866 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
867 				       gfp_t gfp)
868 {
869 	u8 *data;
870 	struct pci_dev *pdev = bp->pdev;
871 
872 	if (gfp == GFP_ATOMIC)
873 		data = napi_alloc_frag(bp->rx_buf_size);
874 	else
875 		data = netdev_alloc_frag(bp->rx_buf_size);
876 	if (!data)
877 		return NULL;
878 
879 	*mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
880 					bp->rx_buf_use_size, bp->rx_dir,
881 					DMA_ATTR_WEAK_ORDERING);
882 
883 	if (dma_mapping_error(&pdev->dev, *mapping)) {
884 		skb_free_frag(data);
885 		data = NULL;
886 	}
887 	return data;
888 }
889 
890 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
891 		       u16 prod, gfp_t gfp)
892 {
893 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
894 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
895 	dma_addr_t mapping;
896 
897 	if (BNXT_RX_PAGE_MODE(bp)) {
898 		unsigned int offset;
899 		struct page *page =
900 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
901 
902 		if (!page)
903 			return -ENOMEM;
904 
905 		mapping += bp->rx_dma_offset;
906 		rx_buf->data = page;
907 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
908 	} else {
909 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, gfp);
910 
911 		if (!data)
912 			return -ENOMEM;
913 
914 		rx_buf->data = data;
915 		rx_buf->data_ptr = data + bp->rx_offset;
916 	}
917 	rx_buf->mapping = mapping;
918 
919 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
920 	return 0;
921 }
922 
923 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
924 {
925 	u16 prod = rxr->rx_prod;
926 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
927 	struct bnxt *bp = rxr->bnapi->bp;
928 	struct rx_bd *cons_bd, *prod_bd;
929 
930 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
931 	cons_rx_buf = &rxr->rx_buf_ring[cons];
932 
933 	prod_rx_buf->data = data;
934 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
935 
936 	prod_rx_buf->mapping = cons_rx_buf->mapping;
937 
938 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
939 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
940 
941 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
942 }
943 
944 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
945 {
946 	u16 next, max = rxr->rx_agg_bmap_size;
947 
948 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
949 	if (next >= max)
950 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
951 	return next;
952 }
953 
954 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
955 				     struct bnxt_rx_ring_info *rxr,
956 				     u16 prod, gfp_t gfp)
957 {
958 	struct rx_bd *rxbd =
959 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
960 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
961 	struct page *page;
962 	dma_addr_t mapping;
963 	u16 sw_prod = rxr->rx_sw_agg_prod;
964 	unsigned int offset = 0;
965 
966 	page = __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
967 
968 	if (!page)
969 		return -ENOMEM;
970 
971 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
972 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
973 
974 	__set_bit(sw_prod, rxr->rx_agg_bmap);
975 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
976 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
977 
978 	rx_agg_buf->page = page;
979 	rx_agg_buf->offset = offset;
980 	rx_agg_buf->mapping = mapping;
981 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
982 	rxbd->rx_bd_opaque = sw_prod;
983 	return 0;
984 }
985 
986 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
987 				       struct bnxt_cp_ring_info *cpr,
988 				       u16 cp_cons, u16 curr)
989 {
990 	struct rx_agg_cmp *agg;
991 
992 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
993 	agg = (struct rx_agg_cmp *)
994 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
995 	return agg;
996 }
997 
998 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
999 					      struct bnxt_rx_ring_info *rxr,
1000 					      u16 agg_id, u16 curr)
1001 {
1002 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1003 
1004 	return &tpa_info->agg_arr[curr];
1005 }
1006 
1007 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1008 				   u16 start, u32 agg_bufs, bool tpa)
1009 {
1010 	struct bnxt_napi *bnapi = cpr->bnapi;
1011 	struct bnxt *bp = bnapi->bp;
1012 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1013 	u16 prod = rxr->rx_agg_prod;
1014 	u16 sw_prod = rxr->rx_sw_agg_prod;
1015 	bool p5_tpa = false;
1016 	u32 i;
1017 
1018 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1019 		p5_tpa = true;
1020 
1021 	for (i = 0; i < agg_bufs; i++) {
1022 		u16 cons;
1023 		struct rx_agg_cmp *agg;
1024 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1025 		struct rx_bd *prod_bd;
1026 		struct page *page;
1027 
1028 		if (p5_tpa)
1029 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1030 		else
1031 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1032 		cons = agg->rx_agg_cmp_opaque;
1033 		__clear_bit(cons, rxr->rx_agg_bmap);
1034 
1035 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1036 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1037 
1038 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1039 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1040 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1041 
1042 		/* It is possible for sw_prod to be equal to cons, so
1043 		 * set cons_rx_buf->page to NULL first.
1044 		 */
1045 		page = cons_rx_buf->page;
1046 		cons_rx_buf->page = NULL;
1047 		prod_rx_buf->page = page;
1048 		prod_rx_buf->offset = cons_rx_buf->offset;
1049 
1050 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1051 
1052 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1053 
1054 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1055 		prod_bd->rx_bd_opaque = sw_prod;
1056 
1057 		prod = NEXT_RX_AGG(prod);
1058 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1059 	}
1060 	rxr->rx_agg_prod = prod;
1061 	rxr->rx_sw_agg_prod = sw_prod;
1062 }
1063 
1064 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1065 					      struct bnxt_rx_ring_info *rxr,
1066 					      u16 cons, void *data, u8 *data_ptr,
1067 					      dma_addr_t dma_addr,
1068 					      unsigned int offset_and_len)
1069 {
1070 	unsigned int len = offset_and_len & 0xffff;
1071 	struct page *page = data;
1072 	u16 prod = rxr->rx_prod;
1073 	struct sk_buff *skb;
1074 	int err;
1075 
1076 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1077 	if (unlikely(err)) {
1078 		bnxt_reuse_rx_data(rxr, cons, data);
1079 		return NULL;
1080 	}
1081 	dma_addr -= bp->rx_dma_offset;
1082 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1083 				bp->rx_dir);
1084 	skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE);
1085 	if (!skb) {
1086 		page_pool_recycle_direct(rxr->page_pool, page);
1087 		return NULL;
1088 	}
1089 	skb_mark_for_recycle(skb);
1090 	skb_reserve(skb, bp->rx_offset);
1091 	__skb_put(skb, len);
1092 
1093 	return skb;
1094 }
1095 
1096 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1097 					struct bnxt_rx_ring_info *rxr,
1098 					u16 cons, void *data, u8 *data_ptr,
1099 					dma_addr_t dma_addr,
1100 					unsigned int offset_and_len)
1101 {
1102 	unsigned int payload = offset_and_len >> 16;
1103 	unsigned int len = offset_and_len & 0xffff;
1104 	skb_frag_t *frag;
1105 	struct page *page = data;
1106 	u16 prod = rxr->rx_prod;
1107 	struct sk_buff *skb;
1108 	int off, err;
1109 
1110 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1111 	if (unlikely(err)) {
1112 		bnxt_reuse_rx_data(rxr, cons, data);
1113 		return NULL;
1114 	}
1115 	dma_addr -= bp->rx_dma_offset;
1116 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1117 				bp->rx_dir);
1118 
1119 	if (unlikely(!payload))
1120 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1121 
1122 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1123 	if (!skb) {
1124 		page_pool_recycle_direct(rxr->page_pool, page);
1125 		return NULL;
1126 	}
1127 
1128 	skb_mark_for_recycle(skb);
1129 	off = (void *)data_ptr - page_address(page);
1130 	skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE);
1131 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1132 	       payload + NET_IP_ALIGN);
1133 
1134 	frag = &skb_shinfo(skb)->frags[0];
1135 	skb_frag_size_sub(frag, payload);
1136 	skb_frag_off_add(frag, payload);
1137 	skb->data_len -= payload;
1138 	skb->tail += payload;
1139 
1140 	return skb;
1141 }
1142 
1143 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1144 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1145 				   void *data, u8 *data_ptr,
1146 				   dma_addr_t dma_addr,
1147 				   unsigned int offset_and_len)
1148 {
1149 	u16 prod = rxr->rx_prod;
1150 	struct sk_buff *skb;
1151 	int err;
1152 
1153 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1154 	if (unlikely(err)) {
1155 		bnxt_reuse_rx_data(rxr, cons, data);
1156 		return NULL;
1157 	}
1158 
1159 	skb = napi_build_skb(data, bp->rx_buf_size);
1160 	dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1161 			       bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
1162 	if (!skb) {
1163 		skb_free_frag(data);
1164 		return NULL;
1165 	}
1166 
1167 	skb_reserve(skb, bp->rx_offset);
1168 	skb_put(skb, offset_and_len & 0xffff);
1169 	return skb;
1170 }
1171 
1172 static u32 __bnxt_rx_agg_pages(struct bnxt *bp,
1173 			       struct bnxt_cp_ring_info *cpr,
1174 			       struct skb_shared_info *shinfo,
1175 			       u16 idx, u32 agg_bufs, bool tpa,
1176 			       struct xdp_buff *xdp)
1177 {
1178 	struct bnxt_napi *bnapi = cpr->bnapi;
1179 	struct pci_dev *pdev = bp->pdev;
1180 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1181 	u16 prod = rxr->rx_agg_prod;
1182 	u32 i, total_frag_len = 0;
1183 	bool p5_tpa = false;
1184 
1185 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1186 		p5_tpa = true;
1187 
1188 	for (i = 0; i < agg_bufs; i++) {
1189 		skb_frag_t *frag = &shinfo->frags[i];
1190 		u16 cons, frag_len;
1191 		struct rx_agg_cmp *agg;
1192 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1193 		struct page *page;
1194 		dma_addr_t mapping;
1195 
1196 		if (p5_tpa)
1197 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1198 		else
1199 			agg = bnxt_get_agg(bp, cpr, idx, i);
1200 		cons = agg->rx_agg_cmp_opaque;
1201 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1202 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1203 
1204 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1205 		skb_frag_fill_page_desc(frag, cons_rx_buf->page,
1206 					cons_rx_buf->offset, frag_len);
1207 		shinfo->nr_frags = i + 1;
1208 		__clear_bit(cons, rxr->rx_agg_bmap);
1209 
1210 		/* It is possible for bnxt_alloc_rx_page() to allocate
1211 		 * a sw_prod index that equals the cons index, so we
1212 		 * need to clear the cons entry now.
1213 		 */
1214 		mapping = cons_rx_buf->mapping;
1215 		page = cons_rx_buf->page;
1216 		cons_rx_buf->page = NULL;
1217 
1218 		if (xdp && page_is_pfmemalloc(page))
1219 			xdp_buff_set_frag_pfmemalloc(xdp);
1220 
1221 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1222 			--shinfo->nr_frags;
1223 			cons_rx_buf->page = page;
1224 
1225 			/* Update prod since possibly some pages have been
1226 			 * allocated already.
1227 			 */
1228 			rxr->rx_agg_prod = prod;
1229 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1230 			return 0;
1231 		}
1232 
1233 		dma_sync_single_for_cpu(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1234 					bp->rx_dir);
1235 
1236 		total_frag_len += frag_len;
1237 		prod = NEXT_RX_AGG(prod);
1238 	}
1239 	rxr->rx_agg_prod = prod;
1240 	return total_frag_len;
1241 }
1242 
1243 static struct sk_buff *bnxt_rx_agg_pages_skb(struct bnxt *bp,
1244 					     struct bnxt_cp_ring_info *cpr,
1245 					     struct sk_buff *skb, u16 idx,
1246 					     u32 agg_bufs, bool tpa)
1247 {
1248 	struct skb_shared_info *shinfo = skb_shinfo(skb);
1249 	u32 total_frag_len = 0;
1250 
1251 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo, idx,
1252 					     agg_bufs, tpa, NULL);
1253 	if (!total_frag_len) {
1254 		skb_mark_for_recycle(skb);
1255 		dev_kfree_skb(skb);
1256 		return NULL;
1257 	}
1258 
1259 	skb->data_len += total_frag_len;
1260 	skb->len += total_frag_len;
1261 	skb->truesize += BNXT_RX_PAGE_SIZE * agg_bufs;
1262 	return skb;
1263 }
1264 
1265 static u32 bnxt_rx_agg_pages_xdp(struct bnxt *bp,
1266 				 struct bnxt_cp_ring_info *cpr,
1267 				 struct xdp_buff *xdp, u16 idx,
1268 				 u32 agg_bufs, bool tpa)
1269 {
1270 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1271 	u32 total_frag_len = 0;
1272 
1273 	if (!xdp_buff_has_frags(xdp))
1274 		shinfo->nr_frags = 0;
1275 
1276 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo,
1277 					     idx, agg_bufs, tpa, xdp);
1278 	if (total_frag_len) {
1279 		xdp_buff_set_frags_flag(xdp);
1280 		shinfo->nr_frags = agg_bufs;
1281 		shinfo->xdp_frags_size = total_frag_len;
1282 	}
1283 	return total_frag_len;
1284 }
1285 
1286 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1287 			       u8 agg_bufs, u32 *raw_cons)
1288 {
1289 	u16 last;
1290 	struct rx_agg_cmp *agg;
1291 
1292 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1293 	last = RING_CMP(*raw_cons);
1294 	agg = (struct rx_agg_cmp *)
1295 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1296 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1297 }
1298 
1299 static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1300 					    unsigned int len,
1301 					    dma_addr_t mapping)
1302 {
1303 	struct bnxt *bp = bnapi->bp;
1304 	struct pci_dev *pdev = bp->pdev;
1305 	struct sk_buff *skb;
1306 
1307 	skb = napi_alloc_skb(&bnapi->napi, len);
1308 	if (!skb)
1309 		return NULL;
1310 
1311 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1312 				bp->rx_dir);
1313 
1314 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1315 	       len + NET_IP_ALIGN);
1316 
1317 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1318 				   bp->rx_dir);
1319 
1320 	skb_put(skb, len);
1321 	return skb;
1322 }
1323 
1324 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1325 			   u32 *raw_cons, void *cmp)
1326 {
1327 	struct rx_cmp *rxcmp = cmp;
1328 	u32 tmp_raw_cons = *raw_cons;
1329 	u8 cmp_type, agg_bufs = 0;
1330 
1331 	cmp_type = RX_CMP_TYPE(rxcmp);
1332 
1333 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1334 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1335 			    RX_CMP_AGG_BUFS) >>
1336 			   RX_CMP_AGG_BUFS_SHIFT;
1337 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1338 		struct rx_tpa_end_cmp *tpa_end = cmp;
1339 
1340 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1341 			return 0;
1342 
1343 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1344 	}
1345 
1346 	if (agg_bufs) {
1347 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1348 			return -EBUSY;
1349 	}
1350 	*raw_cons = tmp_raw_cons;
1351 	return 0;
1352 }
1353 
1354 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1355 {
1356 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1357 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1358 
1359 	if (test_bit(idx, map->agg_idx_bmap))
1360 		idx = find_first_zero_bit(map->agg_idx_bmap,
1361 					  BNXT_AGG_IDX_BMAP_SIZE);
1362 	__set_bit(idx, map->agg_idx_bmap);
1363 	map->agg_id_tbl[agg_id] = idx;
1364 	return idx;
1365 }
1366 
1367 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1368 {
1369 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1370 
1371 	__clear_bit(idx, map->agg_idx_bmap);
1372 }
1373 
1374 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1375 {
1376 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1377 
1378 	return map->agg_id_tbl[agg_id];
1379 }
1380 
1381 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1382 			      struct rx_tpa_start_cmp *tpa_start,
1383 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1384 {
1385 	tpa_info->cfa_code_valid = 1;
1386 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1387 	tpa_info->vlan_valid = 0;
1388 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1389 		tpa_info->vlan_valid = 1;
1390 		tpa_info->metadata =
1391 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1392 	}
1393 }
1394 
1395 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1396 				 struct rx_tpa_start_cmp *tpa_start,
1397 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1398 {
1399 	tpa_info->vlan_valid = 0;
1400 	if (TPA_START_VLAN_VALID(tpa_start)) {
1401 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1402 		u32 vlan_proto = ETH_P_8021Q;
1403 
1404 		tpa_info->vlan_valid = 1;
1405 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1406 			vlan_proto = ETH_P_8021AD;
1407 		tpa_info->metadata = vlan_proto << 16 |
1408 				     TPA_START_METADATA0_TCI(tpa_start1);
1409 	}
1410 }
1411 
1412 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1413 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1414 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1415 {
1416 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1417 	struct bnxt_tpa_info *tpa_info;
1418 	u16 cons, prod, agg_id;
1419 	struct rx_bd *prod_bd;
1420 	dma_addr_t mapping;
1421 
1422 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1423 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1424 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1425 	} else {
1426 		agg_id = TPA_START_AGG_ID(tpa_start);
1427 	}
1428 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1429 	prod = rxr->rx_prod;
1430 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1431 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1432 	tpa_info = &rxr->rx_tpa[agg_id];
1433 
1434 	if (unlikely(cons != rxr->rx_next_cons ||
1435 		     TPA_START_ERROR(tpa_start))) {
1436 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1437 			    cons, rxr->rx_next_cons,
1438 			    TPA_START_ERROR_CODE(tpa_start1));
1439 		bnxt_sched_reset_rxr(bp, rxr);
1440 		return;
1441 	}
1442 	prod_rx_buf->data = tpa_info->data;
1443 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1444 
1445 	mapping = tpa_info->mapping;
1446 	prod_rx_buf->mapping = mapping;
1447 
1448 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1449 
1450 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1451 
1452 	tpa_info->data = cons_rx_buf->data;
1453 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1454 	cons_rx_buf->data = NULL;
1455 	tpa_info->mapping = cons_rx_buf->mapping;
1456 
1457 	tpa_info->len =
1458 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1459 				RX_TPA_START_CMP_LEN_SHIFT;
1460 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1461 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1462 		tpa_info->gso_type = SKB_GSO_TCPV4;
1463 		if (TPA_START_IS_IPV6(tpa_start1))
1464 			tpa_info->gso_type = SKB_GSO_TCPV6;
1465 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1466 		else if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP &&
1467 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1468 			tpa_info->gso_type = SKB_GSO_TCPV6;
1469 		tpa_info->rss_hash =
1470 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1471 	} else {
1472 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1473 		tpa_info->gso_type = 0;
1474 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1475 	}
1476 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1477 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1478 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1479 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1480 	else
1481 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1482 	tpa_info->agg_count = 0;
1483 
1484 	rxr->rx_prod = NEXT_RX(prod);
1485 	cons = RING_RX(bp, NEXT_RX(cons));
1486 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1487 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1488 
1489 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1490 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1491 	cons_rx_buf->data = NULL;
1492 }
1493 
1494 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1495 {
1496 	if (agg_bufs)
1497 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1498 }
1499 
1500 #ifdef CONFIG_INET
1501 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1502 {
1503 	struct udphdr *uh = NULL;
1504 
1505 	if (ip_proto == htons(ETH_P_IP)) {
1506 		struct iphdr *iph = (struct iphdr *)skb->data;
1507 
1508 		if (iph->protocol == IPPROTO_UDP)
1509 			uh = (struct udphdr *)(iph + 1);
1510 	} else {
1511 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1512 
1513 		if (iph->nexthdr == IPPROTO_UDP)
1514 			uh = (struct udphdr *)(iph + 1);
1515 	}
1516 	if (uh) {
1517 		if (uh->check)
1518 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1519 		else
1520 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1521 	}
1522 }
1523 #endif
1524 
1525 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1526 					   int payload_off, int tcp_ts,
1527 					   struct sk_buff *skb)
1528 {
1529 #ifdef CONFIG_INET
1530 	struct tcphdr *th;
1531 	int len, nw_off;
1532 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1533 	u32 hdr_info = tpa_info->hdr_info;
1534 	bool loopback = false;
1535 
1536 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1537 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1538 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1539 
1540 	/* If the packet is an internal loopback packet, the offsets will
1541 	 * have an extra 4 bytes.
1542 	 */
1543 	if (inner_mac_off == 4) {
1544 		loopback = true;
1545 	} else if (inner_mac_off > 4) {
1546 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1547 					    ETH_HLEN - 2));
1548 
1549 		/* We only support inner iPv4/ipv6.  If we don't see the
1550 		 * correct protocol ID, it must be a loopback packet where
1551 		 * the offsets are off by 4.
1552 		 */
1553 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1554 			loopback = true;
1555 	}
1556 	if (loopback) {
1557 		/* internal loopback packet, subtract all offsets by 4 */
1558 		inner_ip_off -= 4;
1559 		inner_mac_off -= 4;
1560 		outer_ip_off -= 4;
1561 	}
1562 
1563 	nw_off = inner_ip_off - ETH_HLEN;
1564 	skb_set_network_header(skb, nw_off);
1565 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1566 		struct ipv6hdr *iph = ipv6_hdr(skb);
1567 
1568 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1569 		len = skb->len - skb_transport_offset(skb);
1570 		th = tcp_hdr(skb);
1571 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1572 	} else {
1573 		struct iphdr *iph = ip_hdr(skb);
1574 
1575 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1576 		len = skb->len - skb_transport_offset(skb);
1577 		th = tcp_hdr(skb);
1578 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1579 	}
1580 
1581 	if (inner_mac_off) { /* tunnel */
1582 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1583 					    ETH_HLEN - 2));
1584 
1585 		bnxt_gro_tunnel(skb, proto);
1586 	}
1587 #endif
1588 	return skb;
1589 }
1590 
1591 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1592 					   int payload_off, int tcp_ts,
1593 					   struct sk_buff *skb)
1594 {
1595 #ifdef CONFIG_INET
1596 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1597 	u32 hdr_info = tpa_info->hdr_info;
1598 	int iphdr_len, nw_off;
1599 
1600 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1601 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1602 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1603 
1604 	nw_off = inner_ip_off - ETH_HLEN;
1605 	skb_set_network_header(skb, nw_off);
1606 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1607 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1608 	skb_set_transport_header(skb, nw_off + iphdr_len);
1609 
1610 	if (inner_mac_off) { /* tunnel */
1611 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1612 					    ETH_HLEN - 2));
1613 
1614 		bnxt_gro_tunnel(skb, proto);
1615 	}
1616 #endif
1617 	return skb;
1618 }
1619 
1620 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1621 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1622 
1623 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1624 					   int payload_off, int tcp_ts,
1625 					   struct sk_buff *skb)
1626 {
1627 #ifdef CONFIG_INET
1628 	struct tcphdr *th;
1629 	int len, nw_off, tcp_opt_len = 0;
1630 
1631 	if (tcp_ts)
1632 		tcp_opt_len = 12;
1633 
1634 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1635 		struct iphdr *iph;
1636 
1637 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1638 			 ETH_HLEN;
1639 		skb_set_network_header(skb, nw_off);
1640 		iph = ip_hdr(skb);
1641 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1642 		len = skb->len - skb_transport_offset(skb);
1643 		th = tcp_hdr(skb);
1644 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1645 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1646 		struct ipv6hdr *iph;
1647 
1648 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1649 			 ETH_HLEN;
1650 		skb_set_network_header(skb, nw_off);
1651 		iph = ipv6_hdr(skb);
1652 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1653 		len = skb->len - skb_transport_offset(skb);
1654 		th = tcp_hdr(skb);
1655 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1656 	} else {
1657 		dev_kfree_skb_any(skb);
1658 		return NULL;
1659 	}
1660 
1661 	if (nw_off) /* tunnel */
1662 		bnxt_gro_tunnel(skb, skb->protocol);
1663 #endif
1664 	return skb;
1665 }
1666 
1667 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1668 					   struct bnxt_tpa_info *tpa_info,
1669 					   struct rx_tpa_end_cmp *tpa_end,
1670 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1671 					   struct sk_buff *skb)
1672 {
1673 #ifdef CONFIG_INET
1674 	int payload_off;
1675 	u16 segs;
1676 
1677 	segs = TPA_END_TPA_SEGS(tpa_end);
1678 	if (segs == 1)
1679 		return skb;
1680 
1681 	NAPI_GRO_CB(skb)->count = segs;
1682 	skb_shinfo(skb)->gso_size =
1683 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1684 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1685 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1686 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1687 	else
1688 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1689 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1690 	if (likely(skb))
1691 		tcp_gro_complete(skb);
1692 #endif
1693 	return skb;
1694 }
1695 
1696 /* Given the cfa_code of a received packet determine which
1697  * netdev (vf-rep or PF) the packet is destined to.
1698  */
1699 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1700 {
1701 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1702 
1703 	/* if vf-rep dev is NULL, the must belongs to the PF */
1704 	return dev ? dev : bp->dev;
1705 }
1706 
1707 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1708 					   struct bnxt_cp_ring_info *cpr,
1709 					   u32 *raw_cons,
1710 					   struct rx_tpa_end_cmp *tpa_end,
1711 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1712 					   u8 *event)
1713 {
1714 	struct bnxt_napi *bnapi = cpr->bnapi;
1715 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1716 	struct net_device *dev = bp->dev;
1717 	u8 *data_ptr, agg_bufs;
1718 	unsigned int len;
1719 	struct bnxt_tpa_info *tpa_info;
1720 	dma_addr_t mapping;
1721 	struct sk_buff *skb;
1722 	u16 idx = 0, agg_id;
1723 	void *data;
1724 	bool gro;
1725 
1726 	if (unlikely(bnapi->in_reset)) {
1727 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1728 
1729 		if (rc < 0)
1730 			return ERR_PTR(-EBUSY);
1731 		return NULL;
1732 	}
1733 
1734 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1735 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1736 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1737 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1738 		tpa_info = &rxr->rx_tpa[agg_id];
1739 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1740 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1741 				    agg_bufs, tpa_info->agg_count);
1742 			agg_bufs = tpa_info->agg_count;
1743 		}
1744 		tpa_info->agg_count = 0;
1745 		*event |= BNXT_AGG_EVENT;
1746 		bnxt_free_agg_idx(rxr, agg_id);
1747 		idx = agg_id;
1748 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1749 	} else {
1750 		agg_id = TPA_END_AGG_ID(tpa_end);
1751 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1752 		tpa_info = &rxr->rx_tpa[agg_id];
1753 		idx = RING_CMP(*raw_cons);
1754 		if (agg_bufs) {
1755 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1756 				return ERR_PTR(-EBUSY);
1757 
1758 			*event |= BNXT_AGG_EVENT;
1759 			idx = NEXT_CMP(idx);
1760 		}
1761 		gro = !!TPA_END_GRO(tpa_end);
1762 	}
1763 	data = tpa_info->data;
1764 	data_ptr = tpa_info->data_ptr;
1765 	prefetch(data_ptr);
1766 	len = tpa_info->len;
1767 	mapping = tpa_info->mapping;
1768 
1769 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1770 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1771 		if (agg_bufs > MAX_SKB_FRAGS)
1772 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1773 				    agg_bufs, (int)MAX_SKB_FRAGS);
1774 		return NULL;
1775 	}
1776 
1777 	if (len <= bp->rx_copy_thresh) {
1778 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1779 		if (!skb) {
1780 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1781 			cpr->bnapi->cp_ring.sw_stats.rx.rx_oom_discards += 1;
1782 			return NULL;
1783 		}
1784 	} else {
1785 		u8 *new_data;
1786 		dma_addr_t new_mapping;
1787 
1788 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, GFP_ATOMIC);
1789 		if (!new_data) {
1790 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1791 			cpr->bnapi->cp_ring.sw_stats.rx.rx_oom_discards += 1;
1792 			return NULL;
1793 		}
1794 
1795 		tpa_info->data = new_data;
1796 		tpa_info->data_ptr = new_data + bp->rx_offset;
1797 		tpa_info->mapping = new_mapping;
1798 
1799 		skb = napi_build_skb(data, bp->rx_buf_size);
1800 		dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1801 				       bp->rx_buf_use_size, bp->rx_dir,
1802 				       DMA_ATTR_WEAK_ORDERING);
1803 
1804 		if (!skb) {
1805 			skb_free_frag(data);
1806 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1807 			cpr->bnapi->cp_ring.sw_stats.rx.rx_oom_discards += 1;
1808 			return NULL;
1809 		}
1810 		skb_reserve(skb, bp->rx_offset);
1811 		skb_put(skb, len);
1812 	}
1813 
1814 	if (agg_bufs) {
1815 		skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, idx, agg_bufs, true);
1816 		if (!skb) {
1817 			/* Page reuse already handled by bnxt_rx_pages(). */
1818 			cpr->bnapi->cp_ring.sw_stats.rx.rx_oom_discards += 1;
1819 			return NULL;
1820 		}
1821 	}
1822 
1823 	if (tpa_info->cfa_code_valid)
1824 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1825 	skb->protocol = eth_type_trans(skb, dev);
1826 
1827 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1828 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1829 
1830 	if (tpa_info->vlan_valid &&
1831 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1832 		__be16 vlan_proto = htons(tpa_info->metadata >>
1833 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1834 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1835 
1836 		if (eth_type_vlan(vlan_proto)) {
1837 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1838 		} else {
1839 			dev_kfree_skb(skb);
1840 			return NULL;
1841 		}
1842 	}
1843 
1844 	skb_checksum_none_assert(skb);
1845 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1846 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1847 		skb->csum_level =
1848 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1849 	}
1850 
1851 	if (gro)
1852 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1853 
1854 	return skb;
1855 }
1856 
1857 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1858 			 struct rx_agg_cmp *rx_agg)
1859 {
1860 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1861 	struct bnxt_tpa_info *tpa_info;
1862 
1863 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1864 	tpa_info = &rxr->rx_tpa[agg_id];
1865 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1866 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1867 }
1868 
1869 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1870 			     struct sk_buff *skb)
1871 {
1872 	skb_mark_for_recycle(skb);
1873 
1874 	if (skb->dev != bp->dev) {
1875 		/* this packet belongs to a vf-rep */
1876 		bnxt_vf_rep_rx(bp, skb);
1877 		return;
1878 	}
1879 	skb_record_rx_queue(skb, bnapi->index);
1880 	napi_gro_receive(&bnapi->napi, skb);
1881 }
1882 
1883 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
1884 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
1885 {
1886 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
1887 
1888 	if (BNXT_PTP_RX_TS_VALID(flags))
1889 		goto ts_valid;
1890 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
1891 		return false;
1892 
1893 ts_valid:
1894 	*cmpl_ts = ts;
1895 	return true;
1896 }
1897 
1898 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
1899 				    struct rx_cmp *rxcmp,
1900 				    struct rx_cmp_ext *rxcmp1)
1901 {
1902 	__be16 vlan_proto;
1903 	u16 vtag;
1904 
1905 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1906 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
1907 		u32 meta_data;
1908 
1909 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
1910 			return skb;
1911 
1912 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1913 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1914 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
1915 		if (eth_type_vlan(vlan_proto))
1916 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1917 		else
1918 			goto vlan_err;
1919 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
1920 		if (RX_CMP_VLAN_VALID(rxcmp)) {
1921 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
1922 
1923 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
1924 				vlan_proto = htons(ETH_P_8021Q);
1925 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
1926 				vlan_proto = htons(ETH_P_8021AD);
1927 			else
1928 				goto vlan_err;
1929 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
1930 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1931 		}
1932 	}
1933 	return skb;
1934 vlan_err:
1935 	dev_kfree_skb(skb);
1936 	return NULL;
1937 }
1938 
1939 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp,
1940 					   struct rx_cmp *rxcmp)
1941 {
1942 	u8 ext_op;
1943 
1944 	ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp);
1945 	switch (ext_op) {
1946 	case EXT_OP_INNER_4:
1947 	case EXT_OP_OUTER_4:
1948 	case EXT_OP_INNFL_3:
1949 	case EXT_OP_OUTFL_3:
1950 		return PKT_HASH_TYPE_L4;
1951 	default:
1952 		return PKT_HASH_TYPE_L3;
1953 	}
1954 }
1955 
1956 /* returns the following:
1957  * 1       - 1 packet successfully received
1958  * 0       - successful TPA_START, packet not completed yet
1959  * -EBUSY  - completion ring does not have all the agg buffers yet
1960  * -ENOMEM - packet aborted due to out of memory
1961  * -EIO    - packet aborted due to hw error indicated in BD
1962  */
1963 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1964 		       u32 *raw_cons, u8 *event)
1965 {
1966 	struct bnxt_napi *bnapi = cpr->bnapi;
1967 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1968 	struct net_device *dev = bp->dev;
1969 	struct rx_cmp *rxcmp;
1970 	struct rx_cmp_ext *rxcmp1;
1971 	u32 tmp_raw_cons = *raw_cons;
1972 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1973 	struct bnxt_sw_rx_bd *rx_buf;
1974 	unsigned int len;
1975 	u8 *data_ptr, agg_bufs, cmp_type;
1976 	bool xdp_active = false;
1977 	dma_addr_t dma_addr;
1978 	struct sk_buff *skb;
1979 	struct xdp_buff xdp;
1980 	u32 flags, misc;
1981 	u32 cmpl_ts;
1982 	void *data;
1983 	int rc = 0;
1984 
1985 	rxcmp = (struct rx_cmp *)
1986 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1987 
1988 	cmp_type = RX_CMP_TYPE(rxcmp);
1989 
1990 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
1991 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
1992 		goto next_rx_no_prod_no_len;
1993 	}
1994 
1995 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1996 	cp_cons = RING_CMP(tmp_raw_cons);
1997 	rxcmp1 = (struct rx_cmp_ext *)
1998 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1999 
2000 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2001 		return -EBUSY;
2002 
2003 	/* The valid test of the entry must be done first before
2004 	 * reading any further.
2005 	 */
2006 	dma_rmb();
2007 	prod = rxr->rx_prod;
2008 
2009 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2010 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2011 		bnxt_tpa_start(bp, rxr, cmp_type,
2012 			       (struct rx_tpa_start_cmp *)rxcmp,
2013 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2014 
2015 		*event |= BNXT_RX_EVENT;
2016 		goto next_rx_no_prod_no_len;
2017 
2018 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2019 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2020 				   (struct rx_tpa_end_cmp *)rxcmp,
2021 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2022 
2023 		if (IS_ERR(skb))
2024 			return -EBUSY;
2025 
2026 		rc = -ENOMEM;
2027 		if (likely(skb)) {
2028 			bnxt_deliver_skb(bp, bnapi, skb);
2029 			rc = 1;
2030 		}
2031 		*event |= BNXT_RX_EVENT;
2032 		goto next_rx_no_prod_no_len;
2033 	}
2034 
2035 	cons = rxcmp->rx_cmp_opaque;
2036 	if (unlikely(cons != rxr->rx_next_cons)) {
2037 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2038 
2039 		/* 0xffff is forced error, don't print it */
2040 		if (rxr->rx_next_cons != 0xffff)
2041 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2042 				    cons, rxr->rx_next_cons);
2043 		bnxt_sched_reset_rxr(bp, rxr);
2044 		if (rc1)
2045 			return rc1;
2046 		goto next_rx_no_prod_no_len;
2047 	}
2048 	rx_buf = &rxr->rx_buf_ring[cons];
2049 	data = rx_buf->data;
2050 	data_ptr = rx_buf->data_ptr;
2051 	prefetch(data_ptr);
2052 
2053 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2054 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2055 
2056 	if (agg_bufs) {
2057 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2058 			return -EBUSY;
2059 
2060 		cp_cons = NEXT_CMP(cp_cons);
2061 		*event |= BNXT_AGG_EVENT;
2062 	}
2063 	*event |= BNXT_RX_EVENT;
2064 
2065 	rx_buf->data = NULL;
2066 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2067 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2068 
2069 		bnxt_reuse_rx_data(rxr, cons, data);
2070 		if (agg_bufs)
2071 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2072 					       false);
2073 
2074 		rc = -EIO;
2075 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2076 			bnapi->cp_ring.sw_stats.rx.rx_buf_errors++;
2077 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2078 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2079 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2080 						 rx_err);
2081 				bnxt_sched_reset_rxr(bp, rxr);
2082 			}
2083 		}
2084 		goto next_rx_no_len;
2085 	}
2086 
2087 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2088 	len = flags >> RX_CMP_LEN_SHIFT;
2089 	dma_addr = rx_buf->mapping;
2090 
2091 	if (bnxt_xdp_attached(bp, rxr)) {
2092 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2093 		if (agg_bufs) {
2094 			u32 frag_len = bnxt_rx_agg_pages_xdp(bp, cpr, &xdp,
2095 							     cp_cons, agg_bufs,
2096 							     false);
2097 			if (!frag_len)
2098 				goto oom_next_rx;
2099 		}
2100 		xdp_active = true;
2101 	}
2102 
2103 	if (xdp_active) {
2104 		if (bnxt_rx_xdp(bp, rxr, cons, xdp, data, &data_ptr, &len, event)) {
2105 			rc = 1;
2106 			goto next_rx;
2107 		}
2108 	}
2109 
2110 	if (len <= bp->rx_copy_thresh) {
2111 		skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2112 		bnxt_reuse_rx_data(rxr, cons, data);
2113 		if (!skb) {
2114 			if (agg_bufs) {
2115 				if (!xdp_active)
2116 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2117 							       agg_bufs, false);
2118 				else
2119 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2120 			}
2121 			goto oom_next_rx;
2122 		}
2123 	} else {
2124 		u32 payload;
2125 
2126 		if (rx_buf->data_ptr == data_ptr)
2127 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2128 		else
2129 			payload = 0;
2130 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2131 				      payload | len);
2132 		if (!skb)
2133 			goto oom_next_rx;
2134 	}
2135 
2136 	if (agg_bufs) {
2137 		if (!xdp_active) {
2138 			skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, cp_cons, agg_bufs, false);
2139 			if (!skb)
2140 				goto oom_next_rx;
2141 		} else {
2142 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, rxr->page_pool, &xdp, rxcmp1);
2143 			if (!skb) {
2144 				/* we should be able to free the old skb here */
2145 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2146 				goto oom_next_rx;
2147 			}
2148 		}
2149 	}
2150 
2151 	if (RX_CMP_HASH_VALID(rxcmp)) {
2152 		enum pkt_hash_types type;
2153 
2154 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2155 			type = bnxt_rss_ext_op(bp, rxcmp);
2156 		} else {
2157 			u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
2158 
2159 			/* RSS profiles 1 and 3 with extract code 0 for inner
2160 			 * 4-tuple
2161 			 */
2162 			if (hash_type != 1 && hash_type != 3)
2163 				type = PKT_HASH_TYPE_L3;
2164 			else
2165 				type = PKT_HASH_TYPE_L4;
2166 		}
2167 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2168 	}
2169 
2170 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2171 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2172 	skb->protocol = eth_type_trans(skb, dev);
2173 
2174 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2175 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2176 		if (!skb)
2177 			goto next_rx;
2178 	}
2179 
2180 	skb_checksum_none_assert(skb);
2181 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2182 		if (dev->features & NETIF_F_RXCSUM) {
2183 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2184 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2185 		}
2186 	} else {
2187 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2188 			if (dev->features & NETIF_F_RXCSUM)
2189 				bnapi->cp_ring.sw_stats.rx.rx_l4_csum_errors++;
2190 		}
2191 	}
2192 
2193 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2194 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2195 			u64 ns, ts;
2196 
2197 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2198 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2199 
2200 				spin_lock_bh(&ptp->ptp_lock);
2201 				ns = timecounter_cyc2time(&ptp->tc, ts);
2202 				spin_unlock_bh(&ptp->ptp_lock);
2203 				memset(skb_hwtstamps(skb), 0,
2204 				       sizeof(*skb_hwtstamps(skb)));
2205 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2206 			}
2207 		}
2208 	}
2209 	bnxt_deliver_skb(bp, bnapi, skb);
2210 	rc = 1;
2211 
2212 next_rx:
2213 	cpr->rx_packets += 1;
2214 	cpr->rx_bytes += len;
2215 
2216 next_rx_no_len:
2217 	rxr->rx_prod = NEXT_RX(prod);
2218 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2219 
2220 next_rx_no_prod_no_len:
2221 	*raw_cons = tmp_raw_cons;
2222 
2223 	return rc;
2224 
2225 oom_next_rx:
2226 	cpr->bnapi->cp_ring.sw_stats.rx.rx_oom_discards += 1;
2227 	rc = -ENOMEM;
2228 	goto next_rx;
2229 }
2230 
2231 /* In netpoll mode, if we are using a combined completion ring, we need to
2232  * discard the rx packets and recycle the buffers.
2233  */
2234 static int bnxt_force_rx_discard(struct bnxt *bp,
2235 				 struct bnxt_cp_ring_info *cpr,
2236 				 u32 *raw_cons, u8 *event)
2237 {
2238 	u32 tmp_raw_cons = *raw_cons;
2239 	struct rx_cmp_ext *rxcmp1;
2240 	struct rx_cmp *rxcmp;
2241 	u16 cp_cons;
2242 	u8 cmp_type;
2243 	int rc;
2244 
2245 	cp_cons = RING_CMP(tmp_raw_cons);
2246 	rxcmp = (struct rx_cmp *)
2247 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2248 
2249 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2250 	cp_cons = RING_CMP(tmp_raw_cons);
2251 	rxcmp1 = (struct rx_cmp_ext *)
2252 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2253 
2254 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2255 		return -EBUSY;
2256 
2257 	/* The valid test of the entry must be done first before
2258 	 * reading any further.
2259 	 */
2260 	dma_rmb();
2261 	cmp_type = RX_CMP_TYPE(rxcmp);
2262 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2263 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2264 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2265 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2266 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2267 		struct rx_tpa_end_cmp_ext *tpa_end1;
2268 
2269 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2270 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2271 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2272 	}
2273 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2274 	if (rc && rc != -EBUSY)
2275 		cpr->bnapi->cp_ring.sw_stats.rx.rx_netpoll_discards += 1;
2276 	return rc;
2277 }
2278 
2279 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2280 {
2281 	struct bnxt_fw_health *fw_health = bp->fw_health;
2282 	u32 reg = fw_health->regs[reg_idx];
2283 	u32 reg_type, reg_off, val = 0;
2284 
2285 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2286 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2287 	switch (reg_type) {
2288 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2289 		pci_read_config_dword(bp->pdev, reg_off, &val);
2290 		break;
2291 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2292 		reg_off = fw_health->mapped_regs[reg_idx];
2293 		fallthrough;
2294 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2295 		val = readl(bp->bar0 + reg_off);
2296 		break;
2297 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2298 		val = readl(bp->bar1 + reg_off);
2299 		break;
2300 	}
2301 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2302 		val &= fw_health->fw_reset_inprog_reg_mask;
2303 	return val;
2304 }
2305 
2306 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2307 {
2308 	int i;
2309 
2310 	for (i = 0; i < bp->rx_nr_rings; i++) {
2311 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2312 		struct bnxt_ring_grp_info *grp_info;
2313 
2314 		grp_info = &bp->grp_info[grp_idx];
2315 		if (grp_info->agg_fw_ring_id == ring_id)
2316 			return grp_idx;
2317 	}
2318 	return INVALID_HW_RING_ID;
2319 }
2320 
2321 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2322 {
2323 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2324 
2325 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2326 		return link_info->force_link_speed2;
2327 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2328 		return link_info->force_pam4_link_speed;
2329 	return link_info->force_link_speed;
2330 }
2331 
2332 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2333 {
2334 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2335 
2336 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2337 		link_info->req_link_speed = link_info->force_link_speed2;
2338 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2339 		switch (link_info->req_link_speed) {
2340 		case BNXT_LINK_SPEED_50GB_PAM4:
2341 		case BNXT_LINK_SPEED_100GB_PAM4:
2342 		case BNXT_LINK_SPEED_200GB_PAM4:
2343 		case BNXT_LINK_SPEED_400GB_PAM4:
2344 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2345 			break;
2346 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2347 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2348 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2349 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2350 			break;
2351 		default:
2352 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2353 		}
2354 		return;
2355 	}
2356 	link_info->req_link_speed = link_info->force_link_speed;
2357 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2358 	if (link_info->force_pam4_link_speed) {
2359 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2360 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2361 	}
2362 }
2363 
2364 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2365 {
2366 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2367 
2368 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2369 		link_info->advertising = link_info->auto_link_speeds2;
2370 		return;
2371 	}
2372 	link_info->advertising = link_info->auto_link_speeds;
2373 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2374 }
2375 
2376 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2377 {
2378 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2379 
2380 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2381 		if (link_info->req_link_speed != link_info->force_link_speed2)
2382 			return true;
2383 		return false;
2384 	}
2385 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2386 	    link_info->req_link_speed != link_info->force_link_speed)
2387 		return true;
2388 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2389 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2390 		return true;
2391 	return false;
2392 }
2393 
2394 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2395 {
2396 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2397 
2398 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2399 		if (link_info->advertising != link_info->auto_link_speeds2)
2400 			return true;
2401 		return false;
2402 	}
2403 	if (link_info->advertising != link_info->auto_link_speeds ||
2404 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2405 		return true;
2406 	return false;
2407 }
2408 
2409 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2410 	((data2) &							\
2411 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2412 
2413 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2414 	(((data2) &							\
2415 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2416 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2417 
2418 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2419 	((data1) &							\
2420 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2421 
2422 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2423 	(((data1) &							\
2424 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2425 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2426 
2427 /* Return true if the workqueue has to be scheduled */
2428 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2429 {
2430 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2431 
2432 	switch (err_type) {
2433 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2434 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2435 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2436 		break;
2437 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2438 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2439 		break;
2440 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2441 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2442 		break;
2443 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2444 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2445 		char *threshold_type;
2446 		bool notify = false;
2447 		char *dir_str;
2448 
2449 		switch (type) {
2450 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2451 			threshold_type = "warning";
2452 			break;
2453 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2454 			threshold_type = "critical";
2455 			break;
2456 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2457 			threshold_type = "fatal";
2458 			break;
2459 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2460 			threshold_type = "shutdown";
2461 			break;
2462 		default:
2463 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2464 			return false;
2465 		}
2466 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2467 			dir_str = "above";
2468 			notify = true;
2469 		} else {
2470 			dir_str = "below";
2471 		}
2472 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2473 			    dir_str, threshold_type);
2474 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2475 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2476 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2477 		if (notify) {
2478 			bp->thermal_threshold_type = type;
2479 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2480 			return true;
2481 		}
2482 		return false;
2483 	}
2484 	default:
2485 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2486 			   err_type);
2487 		break;
2488 	}
2489 	return false;
2490 }
2491 
2492 #define BNXT_GET_EVENT_PORT(data)	\
2493 	((data) &			\
2494 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2495 
2496 #define BNXT_EVENT_RING_TYPE(data2)	\
2497 	((data2) &			\
2498 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2499 
2500 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2501 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2502 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2503 
2504 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2505 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2506 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2507 
2508 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2509 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2510 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2511 
2512 #define BNXT_PHC_BITS	48
2513 
2514 static int bnxt_async_event_process(struct bnxt *bp,
2515 				    struct hwrm_async_event_cmpl *cmpl)
2516 {
2517 	u16 event_id = le16_to_cpu(cmpl->event_id);
2518 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2519 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2520 
2521 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2522 		   event_id, data1, data2);
2523 
2524 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2525 	switch (event_id) {
2526 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2527 		struct bnxt_link_info *link_info = &bp->link_info;
2528 
2529 		if (BNXT_VF(bp))
2530 			goto async_event_process_exit;
2531 
2532 		/* print unsupported speed warning in forced speed mode only */
2533 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2534 		    (data1 & 0x20000)) {
2535 			u16 fw_speed = bnxt_get_force_speed(link_info);
2536 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2537 
2538 			if (speed != SPEED_UNKNOWN)
2539 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2540 					    speed);
2541 		}
2542 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2543 	}
2544 		fallthrough;
2545 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2546 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2547 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2548 		fallthrough;
2549 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2550 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2551 		break;
2552 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2553 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2554 		break;
2555 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2556 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2557 
2558 		if (BNXT_VF(bp))
2559 			break;
2560 
2561 		if (bp->pf.port_id != port_id)
2562 			break;
2563 
2564 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2565 		break;
2566 	}
2567 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2568 		if (BNXT_PF(bp))
2569 			goto async_event_process_exit;
2570 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2571 		break;
2572 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2573 		char *type_str = "Solicited";
2574 
2575 		if (!bp->fw_health)
2576 			goto async_event_process_exit;
2577 
2578 		bp->fw_reset_timestamp = jiffies;
2579 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2580 		if (!bp->fw_reset_min_dsecs)
2581 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2582 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2583 		if (!bp->fw_reset_max_dsecs)
2584 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2585 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2586 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2587 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2588 			type_str = "Fatal";
2589 			bp->fw_health->fatalities++;
2590 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2591 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2592 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2593 			type_str = "Non-fatal";
2594 			bp->fw_health->survivals++;
2595 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2596 		}
2597 		netif_warn(bp, hw, bp->dev,
2598 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2599 			   type_str, data1, data2,
2600 			   bp->fw_reset_min_dsecs * 100,
2601 			   bp->fw_reset_max_dsecs * 100);
2602 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2603 		break;
2604 	}
2605 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2606 		struct bnxt_fw_health *fw_health = bp->fw_health;
2607 		char *status_desc = "healthy";
2608 		u32 status;
2609 
2610 		if (!fw_health)
2611 			goto async_event_process_exit;
2612 
2613 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2614 			fw_health->enabled = false;
2615 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2616 			break;
2617 		}
2618 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2619 		fw_health->tmr_multiplier =
2620 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2621 				     bp->current_interval * 10);
2622 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2623 		if (!fw_health->enabled)
2624 			fw_health->last_fw_heartbeat =
2625 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2626 		fw_health->last_fw_reset_cnt =
2627 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2628 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2629 		if (status != BNXT_FW_STATUS_HEALTHY)
2630 			status_desc = "unhealthy";
2631 		netif_info(bp, drv, bp->dev,
2632 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2633 			   fw_health->primary ? "primary" : "backup", status,
2634 			   status_desc, fw_health->last_fw_reset_cnt);
2635 		if (!fw_health->enabled) {
2636 			/* Make sure tmr_counter is set and visible to
2637 			 * bnxt_health_check() before setting enabled to true.
2638 			 */
2639 			smp_wmb();
2640 			fw_health->enabled = true;
2641 		}
2642 		goto async_event_process_exit;
2643 	}
2644 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2645 		netif_notice(bp, hw, bp->dev,
2646 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2647 			     data1, data2);
2648 		goto async_event_process_exit;
2649 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2650 		struct bnxt_rx_ring_info *rxr;
2651 		u16 grp_idx;
2652 
2653 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2654 			goto async_event_process_exit;
2655 
2656 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2657 			    BNXT_EVENT_RING_TYPE(data2), data1);
2658 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2659 			goto async_event_process_exit;
2660 
2661 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2662 		if (grp_idx == INVALID_HW_RING_ID) {
2663 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2664 				    data1);
2665 			goto async_event_process_exit;
2666 		}
2667 		rxr = bp->bnapi[grp_idx]->rx_ring;
2668 		bnxt_sched_reset_rxr(bp, rxr);
2669 		goto async_event_process_exit;
2670 	}
2671 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2672 		struct bnxt_fw_health *fw_health = bp->fw_health;
2673 
2674 		netif_notice(bp, hw, bp->dev,
2675 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2676 			     data1, data2);
2677 		if (fw_health) {
2678 			fw_health->echo_req_data1 = data1;
2679 			fw_health->echo_req_data2 = data2;
2680 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2681 			break;
2682 		}
2683 		goto async_event_process_exit;
2684 	}
2685 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2686 		bnxt_ptp_pps_event(bp, data1, data2);
2687 		goto async_event_process_exit;
2688 	}
2689 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2690 		if (bnxt_event_error_report(bp, data1, data2))
2691 			break;
2692 		goto async_event_process_exit;
2693 	}
2694 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2695 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2696 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2697 			if (BNXT_PTP_USE_RTC(bp)) {
2698 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2699 				u64 ns;
2700 
2701 				if (!ptp)
2702 					goto async_event_process_exit;
2703 
2704 				spin_lock_bh(&ptp->ptp_lock);
2705 				bnxt_ptp_update_current_time(bp);
2706 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2707 				       BNXT_PHC_BITS) | ptp->current_time);
2708 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2709 				spin_unlock_bh(&ptp->ptp_lock);
2710 			}
2711 			break;
2712 		}
2713 		goto async_event_process_exit;
2714 	}
2715 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2716 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2717 
2718 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2719 		goto async_event_process_exit;
2720 	}
2721 	default:
2722 		goto async_event_process_exit;
2723 	}
2724 	__bnxt_queue_sp_work(bp);
2725 async_event_process_exit:
2726 	return 0;
2727 }
2728 
2729 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2730 {
2731 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2732 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2733 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2734 				(struct hwrm_fwd_req_cmpl *)txcmp;
2735 
2736 	switch (cmpl_type) {
2737 	case CMPL_BASE_TYPE_HWRM_DONE:
2738 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2739 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2740 		break;
2741 
2742 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2743 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2744 
2745 		if ((vf_id < bp->pf.first_vf_id) ||
2746 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2747 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2748 				   vf_id);
2749 			return -EINVAL;
2750 		}
2751 
2752 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2753 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2754 		break;
2755 
2756 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2757 		bnxt_async_event_process(bp,
2758 					 (struct hwrm_async_event_cmpl *)txcmp);
2759 		break;
2760 
2761 	default:
2762 		break;
2763 	}
2764 
2765 	return 0;
2766 }
2767 
2768 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2769 {
2770 	struct bnxt_napi *bnapi = dev_instance;
2771 	struct bnxt *bp = bnapi->bp;
2772 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2773 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2774 
2775 	cpr->event_ctr++;
2776 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2777 	napi_schedule(&bnapi->napi);
2778 	return IRQ_HANDLED;
2779 }
2780 
2781 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2782 {
2783 	u32 raw_cons = cpr->cp_raw_cons;
2784 	u16 cons = RING_CMP(raw_cons);
2785 	struct tx_cmp *txcmp;
2786 
2787 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2788 
2789 	return TX_CMP_VALID(txcmp, raw_cons);
2790 }
2791 
2792 static irqreturn_t bnxt_inta(int irq, void *dev_instance)
2793 {
2794 	struct bnxt_napi *bnapi = dev_instance;
2795 	struct bnxt *bp = bnapi->bp;
2796 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2797 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2798 	u32 int_status;
2799 
2800 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2801 
2802 	if (!bnxt_has_work(bp, cpr)) {
2803 		int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
2804 		/* return if erroneous interrupt */
2805 		if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
2806 			return IRQ_NONE;
2807 	}
2808 
2809 	/* disable ring IRQ */
2810 	BNXT_CP_DB_IRQ_DIS(cpr->cp_db.doorbell);
2811 
2812 	/* Return here if interrupt is shared and is disabled. */
2813 	if (unlikely(atomic_read(&bp->intr_sem) != 0))
2814 		return IRQ_HANDLED;
2815 
2816 	napi_schedule(&bnapi->napi);
2817 	return IRQ_HANDLED;
2818 }
2819 
2820 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2821 			    int budget)
2822 {
2823 	struct bnxt_napi *bnapi = cpr->bnapi;
2824 	u32 raw_cons = cpr->cp_raw_cons;
2825 	u32 cons;
2826 	int rx_pkts = 0;
2827 	u8 event = 0;
2828 	struct tx_cmp *txcmp;
2829 
2830 	cpr->has_more_work = 0;
2831 	cpr->had_work_done = 1;
2832 	while (1) {
2833 		u8 cmp_type;
2834 		int rc;
2835 
2836 		cons = RING_CMP(raw_cons);
2837 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2838 
2839 		if (!TX_CMP_VALID(txcmp, raw_cons))
2840 			break;
2841 
2842 		/* The valid test of the entry must be done first before
2843 		 * reading any further.
2844 		 */
2845 		dma_rmb();
2846 		cmp_type = TX_CMP_TYPE(txcmp);
2847 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
2848 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
2849 			u32 opaque = txcmp->tx_cmp_opaque;
2850 			struct bnxt_tx_ring_info *txr;
2851 			u16 tx_freed;
2852 
2853 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
2854 			event |= BNXT_TX_CMP_EVENT;
2855 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
2856 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
2857 			else
2858 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
2859 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
2860 				   bp->tx_ring_mask;
2861 			/* return full budget so NAPI will complete. */
2862 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
2863 				rx_pkts = budget;
2864 				raw_cons = NEXT_RAW_CMP(raw_cons);
2865 				if (budget)
2866 					cpr->has_more_work = 1;
2867 				break;
2868 			}
2869 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
2870 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2871 			if (likely(budget))
2872 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2873 			else
2874 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2875 							   &event);
2876 			if (likely(rc >= 0))
2877 				rx_pkts += rc;
2878 			/* Increment rx_pkts when rc is -ENOMEM to count towards
2879 			 * the NAPI budget.  Otherwise, we may potentially loop
2880 			 * here forever if we consistently cannot allocate
2881 			 * buffers.
2882 			 */
2883 			else if (rc == -ENOMEM && budget)
2884 				rx_pkts++;
2885 			else if (rc == -EBUSY)	/* partial completion */
2886 				break;
2887 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
2888 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
2889 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
2890 			bnxt_hwrm_handler(bp, txcmp);
2891 		}
2892 		raw_cons = NEXT_RAW_CMP(raw_cons);
2893 
2894 		if (rx_pkts && rx_pkts == budget) {
2895 			cpr->has_more_work = 1;
2896 			break;
2897 		}
2898 	}
2899 
2900 	if (event & BNXT_REDIRECT_EVENT)
2901 		xdp_do_flush();
2902 
2903 	if (event & BNXT_TX_EVENT) {
2904 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
2905 		u16 prod = txr->tx_prod;
2906 
2907 		/* Sync BD data before updating doorbell */
2908 		wmb();
2909 
2910 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
2911 	}
2912 
2913 	cpr->cp_raw_cons = raw_cons;
2914 	bnapi->events |= event;
2915 	return rx_pkts;
2916 }
2917 
2918 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
2919 				  int budget)
2920 {
2921 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
2922 		bnapi->tx_int(bp, bnapi, budget);
2923 
2924 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
2925 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2926 
2927 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2928 	}
2929 	if (bnapi->events & BNXT_AGG_EVENT) {
2930 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2931 
2932 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2933 	}
2934 	bnapi->events &= BNXT_TX_CMP_EVENT;
2935 }
2936 
2937 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2938 			  int budget)
2939 {
2940 	struct bnxt_napi *bnapi = cpr->bnapi;
2941 	int rx_pkts;
2942 
2943 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
2944 
2945 	/* ACK completion ring before freeing tx ring and producing new
2946 	 * buffers in rx/agg rings to prevent overflowing the completion
2947 	 * ring.
2948 	 */
2949 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
2950 
2951 	__bnxt_poll_work_done(bp, bnapi, budget);
2952 	return rx_pkts;
2953 }
2954 
2955 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
2956 {
2957 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2958 	struct bnxt *bp = bnapi->bp;
2959 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2960 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2961 	struct tx_cmp *txcmp;
2962 	struct rx_cmp_ext *rxcmp1;
2963 	u32 cp_cons, tmp_raw_cons;
2964 	u32 raw_cons = cpr->cp_raw_cons;
2965 	bool flush_xdp = false;
2966 	u32 rx_pkts = 0;
2967 	u8 event = 0;
2968 
2969 	while (1) {
2970 		int rc;
2971 
2972 		cp_cons = RING_CMP(raw_cons);
2973 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2974 
2975 		if (!TX_CMP_VALID(txcmp, raw_cons))
2976 			break;
2977 
2978 		/* The valid test of the entry must be done first before
2979 		 * reading any further.
2980 		 */
2981 		dma_rmb();
2982 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
2983 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
2984 			cp_cons = RING_CMP(tmp_raw_cons);
2985 			rxcmp1 = (struct rx_cmp_ext *)
2986 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2987 
2988 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2989 				break;
2990 
2991 			/* force an error to recycle the buffer */
2992 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2993 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2994 
2995 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2996 			if (likely(rc == -EIO) && budget)
2997 				rx_pkts++;
2998 			else if (rc == -EBUSY)	/* partial completion */
2999 				break;
3000 			if (event & BNXT_REDIRECT_EVENT)
3001 				flush_xdp = true;
3002 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3003 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3004 			bnxt_hwrm_handler(bp, txcmp);
3005 		} else {
3006 			netdev_err(bp->dev,
3007 				   "Invalid completion received on special ring\n");
3008 		}
3009 		raw_cons = NEXT_RAW_CMP(raw_cons);
3010 
3011 		if (rx_pkts == budget)
3012 			break;
3013 	}
3014 
3015 	cpr->cp_raw_cons = raw_cons;
3016 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3017 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3018 
3019 	if (event & BNXT_AGG_EVENT)
3020 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3021 	if (flush_xdp)
3022 		xdp_do_flush();
3023 
3024 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3025 		napi_complete_done(napi, rx_pkts);
3026 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3027 	}
3028 	return rx_pkts;
3029 }
3030 
3031 static int bnxt_poll(struct napi_struct *napi, int budget)
3032 {
3033 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3034 	struct bnxt *bp = bnapi->bp;
3035 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3036 	int work_done = 0;
3037 
3038 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3039 		napi_complete(napi);
3040 		return 0;
3041 	}
3042 	while (1) {
3043 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3044 
3045 		if (work_done >= budget) {
3046 			if (!budget)
3047 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3048 			break;
3049 		}
3050 
3051 		if (!bnxt_has_work(bp, cpr)) {
3052 			if (napi_complete_done(napi, work_done))
3053 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3054 			break;
3055 		}
3056 	}
3057 	if (bp->flags & BNXT_FLAG_DIM) {
3058 		struct dim_sample dim_sample = {};
3059 
3060 		dim_update_sample(cpr->event_ctr,
3061 				  cpr->rx_packets,
3062 				  cpr->rx_bytes,
3063 				  &dim_sample);
3064 		net_dim(&cpr->dim, dim_sample);
3065 	}
3066 	return work_done;
3067 }
3068 
3069 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3070 {
3071 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3072 	int i, work_done = 0;
3073 
3074 	for (i = 0; i < cpr->cp_ring_count; i++) {
3075 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3076 
3077 		if (cpr2->had_nqe_notify) {
3078 			work_done += __bnxt_poll_work(bp, cpr2,
3079 						      budget - work_done);
3080 			cpr->has_more_work |= cpr2->has_more_work;
3081 		}
3082 	}
3083 	return work_done;
3084 }
3085 
3086 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3087 				 u64 dbr_type, int budget)
3088 {
3089 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3090 	int i;
3091 
3092 	for (i = 0; i < cpr->cp_ring_count; i++) {
3093 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3094 		struct bnxt_db_info *db;
3095 
3096 		if (cpr2->had_work_done) {
3097 			u32 tgl = 0;
3098 
3099 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3100 				cpr2->had_nqe_notify = 0;
3101 				tgl = cpr2->toggle;
3102 			}
3103 			db = &cpr2->cp_db;
3104 			bnxt_writeq(bp,
3105 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3106 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3107 				    db->doorbell);
3108 			cpr2->had_work_done = 0;
3109 		}
3110 	}
3111 	__bnxt_poll_work_done(bp, bnapi, budget);
3112 }
3113 
3114 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3115 {
3116 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3117 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3118 	struct bnxt_cp_ring_info *cpr_rx;
3119 	u32 raw_cons = cpr->cp_raw_cons;
3120 	struct bnxt *bp = bnapi->bp;
3121 	struct nqe_cn *nqcmp;
3122 	int work_done = 0;
3123 	u32 cons;
3124 
3125 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3126 		napi_complete(napi);
3127 		return 0;
3128 	}
3129 	if (cpr->has_more_work) {
3130 		cpr->has_more_work = 0;
3131 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3132 	}
3133 	while (1) {
3134 		u16 type;
3135 
3136 		cons = RING_CMP(raw_cons);
3137 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3138 
3139 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3140 			if (cpr->has_more_work)
3141 				break;
3142 
3143 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3144 					     budget);
3145 			cpr->cp_raw_cons = raw_cons;
3146 			if (napi_complete_done(napi, work_done))
3147 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3148 						  cpr->cp_raw_cons);
3149 			goto poll_done;
3150 		}
3151 
3152 		/* The valid test of the entry must be done first before
3153 		 * reading any further.
3154 		 */
3155 		dma_rmb();
3156 
3157 		type = le16_to_cpu(nqcmp->type);
3158 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3159 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3160 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3161 			struct bnxt_cp_ring_info *cpr2;
3162 
3163 			/* No more budget for RX work */
3164 			if (budget && work_done >= budget &&
3165 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3166 				break;
3167 
3168 			idx = BNXT_NQ_HDL_IDX(idx);
3169 			cpr2 = &cpr->cp_ring_arr[idx];
3170 			cpr2->had_nqe_notify = 1;
3171 			cpr2->toggle = NQE_CN_TOGGLE(type);
3172 			work_done += __bnxt_poll_work(bp, cpr2,
3173 						      budget - work_done);
3174 			cpr->has_more_work |= cpr2->has_more_work;
3175 		} else {
3176 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3177 		}
3178 		raw_cons = NEXT_RAW_CMP(raw_cons);
3179 	}
3180 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3181 	if (raw_cons != cpr->cp_raw_cons) {
3182 		cpr->cp_raw_cons = raw_cons;
3183 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3184 	}
3185 poll_done:
3186 	cpr_rx = &cpr->cp_ring_arr[0];
3187 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3188 	    (bp->flags & BNXT_FLAG_DIM)) {
3189 		struct dim_sample dim_sample = {};
3190 
3191 		dim_update_sample(cpr->event_ctr,
3192 				  cpr_rx->rx_packets,
3193 				  cpr_rx->rx_bytes,
3194 				  &dim_sample);
3195 		net_dim(&cpr->dim, dim_sample);
3196 	}
3197 	return work_done;
3198 }
3199 
3200 static void bnxt_free_tx_skbs(struct bnxt *bp)
3201 {
3202 	int i, max_idx;
3203 	struct pci_dev *pdev = bp->pdev;
3204 
3205 	if (!bp->tx_ring)
3206 		return;
3207 
3208 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3209 	for (i = 0; i < bp->tx_nr_rings; i++) {
3210 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3211 		int j;
3212 
3213 		if (!txr->tx_buf_ring)
3214 			continue;
3215 
3216 		for (j = 0; j < max_idx;) {
3217 			struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
3218 			struct sk_buff *skb;
3219 			int k, last;
3220 
3221 			if (i < bp->tx_nr_rings_xdp &&
3222 			    tx_buf->action == XDP_REDIRECT) {
3223 				dma_unmap_single(&pdev->dev,
3224 					dma_unmap_addr(tx_buf, mapping),
3225 					dma_unmap_len(tx_buf, len),
3226 					DMA_TO_DEVICE);
3227 				xdp_return_frame(tx_buf->xdpf);
3228 				tx_buf->action = 0;
3229 				tx_buf->xdpf = NULL;
3230 				j++;
3231 				continue;
3232 			}
3233 
3234 			skb = tx_buf->skb;
3235 			if (!skb) {
3236 				j++;
3237 				continue;
3238 			}
3239 
3240 			tx_buf->skb = NULL;
3241 
3242 			if (tx_buf->is_push) {
3243 				dev_kfree_skb(skb);
3244 				j += 2;
3245 				continue;
3246 			}
3247 
3248 			dma_unmap_single(&pdev->dev,
3249 					 dma_unmap_addr(tx_buf, mapping),
3250 					 skb_headlen(skb),
3251 					 DMA_TO_DEVICE);
3252 
3253 			last = tx_buf->nr_frags;
3254 			j += 2;
3255 			for (k = 0; k < last; k++, j++) {
3256 				int ring_idx = j & bp->tx_ring_mask;
3257 				skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
3258 
3259 				tx_buf = &txr->tx_buf_ring[ring_idx];
3260 				dma_unmap_page(
3261 					&pdev->dev,
3262 					dma_unmap_addr(tx_buf, mapping),
3263 					skb_frag_size(frag), DMA_TO_DEVICE);
3264 			}
3265 			dev_kfree_skb(skb);
3266 		}
3267 		netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
3268 	}
3269 }
3270 
3271 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, int ring_nr)
3272 {
3273 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
3274 	struct pci_dev *pdev = bp->pdev;
3275 	struct bnxt_tpa_idx_map *map;
3276 	int i, max_idx, max_agg_idx;
3277 
3278 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3279 	max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3280 	if (!rxr->rx_tpa)
3281 		goto skip_rx_tpa_free;
3282 
3283 	for (i = 0; i < bp->max_tpa; i++) {
3284 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3285 		u8 *data = tpa_info->data;
3286 
3287 		if (!data)
3288 			continue;
3289 
3290 		dma_unmap_single_attrs(&pdev->dev, tpa_info->mapping,
3291 				       bp->rx_buf_use_size, bp->rx_dir,
3292 				       DMA_ATTR_WEAK_ORDERING);
3293 
3294 		tpa_info->data = NULL;
3295 
3296 		skb_free_frag(data);
3297 	}
3298 
3299 skip_rx_tpa_free:
3300 	if (!rxr->rx_buf_ring)
3301 		goto skip_rx_buf_free;
3302 
3303 	for (i = 0; i < max_idx; i++) {
3304 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3305 		dma_addr_t mapping = rx_buf->mapping;
3306 		void *data = rx_buf->data;
3307 
3308 		if (!data)
3309 			continue;
3310 
3311 		rx_buf->data = NULL;
3312 		if (BNXT_RX_PAGE_MODE(bp)) {
3313 			page_pool_recycle_direct(rxr->page_pool, data);
3314 		} else {
3315 			dma_unmap_single_attrs(&pdev->dev, mapping,
3316 					       bp->rx_buf_use_size, bp->rx_dir,
3317 					       DMA_ATTR_WEAK_ORDERING);
3318 			skb_free_frag(data);
3319 		}
3320 	}
3321 
3322 skip_rx_buf_free:
3323 	if (!rxr->rx_agg_ring)
3324 		goto skip_rx_agg_free;
3325 
3326 	for (i = 0; i < max_agg_idx; i++) {
3327 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3328 		struct page *page = rx_agg_buf->page;
3329 
3330 		if (!page)
3331 			continue;
3332 
3333 		rx_agg_buf->page = NULL;
3334 		__clear_bit(i, rxr->rx_agg_bmap);
3335 
3336 		page_pool_recycle_direct(rxr->page_pool, page);
3337 	}
3338 
3339 skip_rx_agg_free:
3340 	map = rxr->rx_tpa_idx_map;
3341 	if (map)
3342 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3343 }
3344 
3345 static void bnxt_free_rx_skbs(struct bnxt *bp)
3346 {
3347 	int i;
3348 
3349 	if (!bp->rx_ring)
3350 		return;
3351 
3352 	for (i = 0; i < bp->rx_nr_rings; i++)
3353 		bnxt_free_one_rx_ring_skbs(bp, i);
3354 }
3355 
3356 static void bnxt_free_skbs(struct bnxt *bp)
3357 {
3358 	bnxt_free_tx_skbs(bp);
3359 	bnxt_free_rx_skbs(bp);
3360 }
3361 
3362 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3363 {
3364 	u8 init_val = ctxm->init_value;
3365 	u16 offset = ctxm->init_offset;
3366 	u8 *p2 = p;
3367 	int i;
3368 
3369 	if (!init_val)
3370 		return;
3371 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3372 		memset(p, init_val, len);
3373 		return;
3374 	}
3375 	for (i = 0; i < len; i += ctxm->entry_size)
3376 		*(p2 + i + offset) = init_val;
3377 }
3378 
3379 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3380 {
3381 	struct pci_dev *pdev = bp->pdev;
3382 	int i;
3383 
3384 	if (!rmem->pg_arr)
3385 		goto skip_pages;
3386 
3387 	for (i = 0; i < rmem->nr_pages; i++) {
3388 		if (!rmem->pg_arr[i])
3389 			continue;
3390 
3391 		dma_free_coherent(&pdev->dev, rmem->page_size,
3392 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3393 
3394 		rmem->pg_arr[i] = NULL;
3395 	}
3396 skip_pages:
3397 	if (rmem->pg_tbl) {
3398 		size_t pg_tbl_size = rmem->nr_pages * 8;
3399 
3400 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3401 			pg_tbl_size = rmem->page_size;
3402 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3403 				  rmem->pg_tbl, rmem->pg_tbl_map);
3404 		rmem->pg_tbl = NULL;
3405 	}
3406 	if (rmem->vmem_size && *rmem->vmem) {
3407 		vfree(*rmem->vmem);
3408 		*rmem->vmem = NULL;
3409 	}
3410 }
3411 
3412 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3413 {
3414 	struct pci_dev *pdev = bp->pdev;
3415 	u64 valid_bit = 0;
3416 	int i;
3417 
3418 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3419 		valid_bit = PTU_PTE_VALID;
3420 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3421 		size_t pg_tbl_size = rmem->nr_pages * 8;
3422 
3423 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3424 			pg_tbl_size = rmem->page_size;
3425 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3426 						  &rmem->pg_tbl_map,
3427 						  GFP_KERNEL);
3428 		if (!rmem->pg_tbl)
3429 			return -ENOMEM;
3430 	}
3431 
3432 	for (i = 0; i < rmem->nr_pages; i++) {
3433 		u64 extra_bits = valid_bit;
3434 
3435 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3436 						     rmem->page_size,
3437 						     &rmem->dma_arr[i],
3438 						     GFP_KERNEL);
3439 		if (!rmem->pg_arr[i])
3440 			return -ENOMEM;
3441 
3442 		if (rmem->ctx_mem)
3443 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3444 					  rmem->page_size);
3445 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3446 			if (i == rmem->nr_pages - 2 &&
3447 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3448 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3449 			else if (i == rmem->nr_pages - 1 &&
3450 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3451 				extra_bits |= PTU_PTE_LAST;
3452 			rmem->pg_tbl[i] =
3453 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3454 		}
3455 	}
3456 
3457 	if (rmem->vmem_size) {
3458 		*rmem->vmem = vzalloc(rmem->vmem_size);
3459 		if (!(*rmem->vmem))
3460 			return -ENOMEM;
3461 	}
3462 	return 0;
3463 }
3464 
3465 static void bnxt_free_tpa_info(struct bnxt *bp)
3466 {
3467 	int i, j;
3468 
3469 	for (i = 0; i < bp->rx_nr_rings; i++) {
3470 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3471 
3472 		kfree(rxr->rx_tpa_idx_map);
3473 		rxr->rx_tpa_idx_map = NULL;
3474 		if (rxr->rx_tpa) {
3475 			for (j = 0; j < bp->max_tpa; j++) {
3476 				kfree(rxr->rx_tpa[j].agg_arr);
3477 				rxr->rx_tpa[j].agg_arr = NULL;
3478 			}
3479 		}
3480 		kfree(rxr->rx_tpa);
3481 		rxr->rx_tpa = NULL;
3482 	}
3483 }
3484 
3485 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3486 {
3487 	int i, j;
3488 
3489 	bp->max_tpa = MAX_TPA;
3490 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3491 		if (!bp->max_tpa_v2)
3492 			return 0;
3493 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3494 	}
3495 
3496 	for (i = 0; i < bp->rx_nr_rings; i++) {
3497 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3498 		struct rx_agg_cmp *agg;
3499 
3500 		rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3501 				      GFP_KERNEL);
3502 		if (!rxr->rx_tpa)
3503 			return -ENOMEM;
3504 
3505 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3506 			continue;
3507 		for (j = 0; j < bp->max_tpa; j++) {
3508 			agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3509 			if (!agg)
3510 				return -ENOMEM;
3511 			rxr->rx_tpa[j].agg_arr = agg;
3512 		}
3513 		rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3514 					      GFP_KERNEL);
3515 		if (!rxr->rx_tpa_idx_map)
3516 			return -ENOMEM;
3517 	}
3518 	return 0;
3519 }
3520 
3521 static void bnxt_free_rx_rings(struct bnxt *bp)
3522 {
3523 	int i;
3524 
3525 	if (!bp->rx_ring)
3526 		return;
3527 
3528 	bnxt_free_tpa_info(bp);
3529 	for (i = 0; i < bp->rx_nr_rings; i++) {
3530 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3531 		struct bnxt_ring_struct *ring;
3532 
3533 		if (rxr->xdp_prog)
3534 			bpf_prog_put(rxr->xdp_prog);
3535 
3536 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3537 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3538 
3539 		page_pool_destroy(rxr->page_pool);
3540 		rxr->page_pool = NULL;
3541 
3542 		kfree(rxr->rx_agg_bmap);
3543 		rxr->rx_agg_bmap = NULL;
3544 
3545 		ring = &rxr->rx_ring_struct;
3546 		bnxt_free_ring(bp, &ring->ring_mem);
3547 
3548 		ring = &rxr->rx_agg_ring_struct;
3549 		bnxt_free_ring(bp, &ring->ring_mem);
3550 	}
3551 }
3552 
3553 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3554 				   struct bnxt_rx_ring_info *rxr)
3555 {
3556 	struct page_pool_params pp = { 0 };
3557 
3558 	pp.pool_size = bp->rx_agg_ring_size;
3559 	if (BNXT_RX_PAGE_MODE(bp))
3560 		pp.pool_size += bp->rx_ring_size;
3561 	pp.nid = dev_to_node(&bp->pdev->dev);
3562 	pp.napi = &rxr->bnapi->napi;
3563 	pp.netdev = bp->dev;
3564 	pp.dev = &bp->pdev->dev;
3565 	pp.dma_dir = bp->rx_dir;
3566 	pp.max_len = PAGE_SIZE;
3567 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3568 
3569 	rxr->page_pool = page_pool_create(&pp);
3570 	if (IS_ERR(rxr->page_pool)) {
3571 		int err = PTR_ERR(rxr->page_pool);
3572 
3573 		rxr->page_pool = NULL;
3574 		return err;
3575 	}
3576 	return 0;
3577 }
3578 
3579 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3580 {
3581 	int i, rc = 0, agg_rings = 0;
3582 
3583 	if (!bp->rx_ring)
3584 		return -ENOMEM;
3585 
3586 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3587 		agg_rings = 1;
3588 
3589 	for (i = 0; i < bp->rx_nr_rings; i++) {
3590 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3591 		struct bnxt_ring_struct *ring;
3592 
3593 		ring = &rxr->rx_ring_struct;
3594 
3595 		rc = bnxt_alloc_rx_page_pool(bp, rxr);
3596 		if (rc)
3597 			return rc;
3598 
3599 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3600 		if (rc < 0)
3601 			return rc;
3602 
3603 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3604 						MEM_TYPE_PAGE_POOL,
3605 						rxr->page_pool);
3606 		if (rc) {
3607 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3608 			return rc;
3609 		}
3610 
3611 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3612 		if (rc)
3613 			return rc;
3614 
3615 		ring->grp_idx = i;
3616 		if (agg_rings) {
3617 			u16 mem_size;
3618 
3619 			ring = &rxr->rx_agg_ring_struct;
3620 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3621 			if (rc)
3622 				return rc;
3623 
3624 			ring->grp_idx = i;
3625 			rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3626 			mem_size = rxr->rx_agg_bmap_size / 8;
3627 			rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3628 			if (!rxr->rx_agg_bmap)
3629 				return -ENOMEM;
3630 		}
3631 	}
3632 	if (bp->flags & BNXT_FLAG_TPA)
3633 		rc = bnxt_alloc_tpa_info(bp);
3634 	return rc;
3635 }
3636 
3637 static void bnxt_free_tx_rings(struct bnxt *bp)
3638 {
3639 	int i;
3640 	struct pci_dev *pdev = bp->pdev;
3641 
3642 	if (!bp->tx_ring)
3643 		return;
3644 
3645 	for (i = 0; i < bp->tx_nr_rings; i++) {
3646 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3647 		struct bnxt_ring_struct *ring;
3648 
3649 		if (txr->tx_push) {
3650 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3651 					  txr->tx_push, txr->tx_push_mapping);
3652 			txr->tx_push = NULL;
3653 		}
3654 
3655 		ring = &txr->tx_ring_struct;
3656 
3657 		bnxt_free_ring(bp, &ring->ring_mem);
3658 	}
3659 }
3660 
3661 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3662 	((tc) * (bp)->tx_nr_rings_per_tc)
3663 
3664 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3665 	((tx) % (bp)->tx_nr_rings_per_tc)
3666 
3667 #define BNXT_RING_TO_TC(bp, tx)		\
3668 	((tx) / (bp)->tx_nr_rings_per_tc)
3669 
3670 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3671 {
3672 	int i, j, rc;
3673 	struct pci_dev *pdev = bp->pdev;
3674 
3675 	bp->tx_push_size = 0;
3676 	if (bp->tx_push_thresh) {
3677 		int push_size;
3678 
3679 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3680 					bp->tx_push_thresh);
3681 
3682 		if (push_size > 256) {
3683 			push_size = 0;
3684 			bp->tx_push_thresh = 0;
3685 		}
3686 
3687 		bp->tx_push_size = push_size;
3688 	}
3689 
3690 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3691 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3692 		struct bnxt_ring_struct *ring;
3693 		u8 qidx;
3694 
3695 		ring = &txr->tx_ring_struct;
3696 
3697 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3698 		if (rc)
3699 			return rc;
3700 
3701 		ring->grp_idx = txr->bnapi->index;
3702 		if (bp->tx_push_size) {
3703 			dma_addr_t mapping;
3704 
3705 			/* One pre-allocated DMA buffer to backup
3706 			 * TX push operation
3707 			 */
3708 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3709 						bp->tx_push_size,
3710 						&txr->tx_push_mapping,
3711 						GFP_KERNEL);
3712 
3713 			if (!txr->tx_push)
3714 				return -ENOMEM;
3715 
3716 			mapping = txr->tx_push_mapping +
3717 				sizeof(struct tx_push_bd);
3718 			txr->data_mapping = cpu_to_le64(mapping);
3719 		}
3720 		qidx = bp->tc_to_qidx[j];
3721 		ring->queue_id = bp->q_info[qidx].queue_id;
3722 		spin_lock_init(&txr->xdp_tx_lock);
3723 		if (i < bp->tx_nr_rings_xdp)
3724 			continue;
3725 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
3726 			j++;
3727 	}
3728 	return 0;
3729 }
3730 
3731 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
3732 {
3733 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3734 
3735 	kfree(cpr->cp_desc_ring);
3736 	cpr->cp_desc_ring = NULL;
3737 	ring->ring_mem.pg_arr = NULL;
3738 	kfree(cpr->cp_desc_mapping);
3739 	cpr->cp_desc_mapping = NULL;
3740 	ring->ring_mem.dma_arr = NULL;
3741 }
3742 
3743 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
3744 {
3745 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
3746 	if (!cpr->cp_desc_ring)
3747 		return -ENOMEM;
3748 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
3749 				       GFP_KERNEL);
3750 	if (!cpr->cp_desc_mapping)
3751 		return -ENOMEM;
3752 	return 0;
3753 }
3754 
3755 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
3756 {
3757 	int i;
3758 
3759 	if (!bp->bnapi)
3760 		return;
3761 	for (i = 0; i < bp->cp_nr_rings; i++) {
3762 		struct bnxt_napi *bnapi = bp->bnapi[i];
3763 
3764 		if (!bnapi)
3765 			continue;
3766 		bnxt_free_cp_arrays(&bnapi->cp_ring);
3767 	}
3768 }
3769 
3770 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
3771 {
3772 	int i, n = bp->cp_nr_pages;
3773 
3774 	for (i = 0; i < bp->cp_nr_rings; i++) {
3775 		struct bnxt_napi *bnapi = bp->bnapi[i];
3776 		int rc;
3777 
3778 		if (!bnapi)
3779 			continue;
3780 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
3781 		if (rc)
3782 			return rc;
3783 	}
3784 	return 0;
3785 }
3786 
3787 static void bnxt_free_cp_rings(struct bnxt *bp)
3788 {
3789 	int i;
3790 
3791 	if (!bp->bnapi)
3792 		return;
3793 
3794 	for (i = 0; i < bp->cp_nr_rings; i++) {
3795 		struct bnxt_napi *bnapi = bp->bnapi[i];
3796 		struct bnxt_cp_ring_info *cpr;
3797 		struct bnxt_ring_struct *ring;
3798 		int j;
3799 
3800 		if (!bnapi)
3801 			continue;
3802 
3803 		cpr = &bnapi->cp_ring;
3804 		ring = &cpr->cp_ring_struct;
3805 
3806 		bnxt_free_ring(bp, &ring->ring_mem);
3807 
3808 		if (!cpr->cp_ring_arr)
3809 			continue;
3810 
3811 		for (j = 0; j < cpr->cp_ring_count; j++) {
3812 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
3813 
3814 			ring = &cpr2->cp_ring_struct;
3815 			bnxt_free_ring(bp, &ring->ring_mem);
3816 			bnxt_free_cp_arrays(cpr2);
3817 		}
3818 		kfree(cpr->cp_ring_arr);
3819 		cpr->cp_ring_arr = NULL;
3820 		cpr->cp_ring_count = 0;
3821 	}
3822 }
3823 
3824 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
3825 				  struct bnxt_cp_ring_info *cpr)
3826 {
3827 	struct bnxt_ring_mem_info *rmem;
3828 	struct bnxt_ring_struct *ring;
3829 	int rc;
3830 
3831 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
3832 	if (rc) {
3833 		bnxt_free_cp_arrays(cpr);
3834 		return -ENOMEM;
3835 	}
3836 	ring = &cpr->cp_ring_struct;
3837 	rmem = &ring->ring_mem;
3838 	rmem->nr_pages = bp->cp_nr_pages;
3839 	rmem->page_size = HW_CMPD_RING_SIZE;
3840 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
3841 	rmem->dma_arr = cpr->cp_desc_mapping;
3842 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
3843 	rc = bnxt_alloc_ring(bp, rmem);
3844 	if (rc) {
3845 		bnxt_free_ring(bp, rmem);
3846 		bnxt_free_cp_arrays(cpr);
3847 	}
3848 	return rc;
3849 }
3850 
3851 static int bnxt_alloc_cp_rings(struct bnxt *bp)
3852 {
3853 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
3854 	int i, j, rc, ulp_base_vec, ulp_msix;
3855 	int tcs = bp->num_tc;
3856 
3857 	if (!tcs)
3858 		tcs = 1;
3859 	ulp_msix = bnxt_get_ulp_msix_num(bp);
3860 	ulp_base_vec = bnxt_get_ulp_msix_base(bp);
3861 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
3862 		struct bnxt_napi *bnapi = bp->bnapi[i];
3863 		struct bnxt_cp_ring_info *cpr, *cpr2;
3864 		struct bnxt_ring_struct *ring;
3865 		int cp_count = 0, k;
3866 		int rx = 0, tx = 0;
3867 
3868 		if (!bnapi)
3869 			continue;
3870 
3871 		cpr = &bnapi->cp_ring;
3872 		cpr->bnapi = bnapi;
3873 		ring = &cpr->cp_ring_struct;
3874 
3875 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3876 		if (rc)
3877 			return rc;
3878 
3879 		if (ulp_msix && i >= ulp_base_vec)
3880 			ring->map_idx = i + ulp_msix;
3881 		else
3882 			ring->map_idx = i;
3883 
3884 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3885 			continue;
3886 
3887 		if (i < bp->rx_nr_rings) {
3888 			cp_count++;
3889 			rx = 1;
3890 		}
3891 		if (i < bp->tx_nr_rings_xdp) {
3892 			cp_count++;
3893 			tx = 1;
3894 		} else if ((sh && i < bp->tx_nr_rings) ||
3895 			 (!sh && i >= bp->rx_nr_rings)) {
3896 			cp_count += tcs;
3897 			tx = 1;
3898 		}
3899 
3900 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
3901 					   GFP_KERNEL);
3902 		if (!cpr->cp_ring_arr)
3903 			return -ENOMEM;
3904 		cpr->cp_ring_count = cp_count;
3905 
3906 		for (k = 0; k < cp_count; k++) {
3907 			cpr2 = &cpr->cp_ring_arr[k];
3908 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
3909 			if (rc)
3910 				return rc;
3911 			cpr2->bnapi = bnapi;
3912 			cpr2->cp_idx = k;
3913 			if (!k && rx) {
3914 				bp->rx_ring[i].rx_cpr = cpr2;
3915 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
3916 			} else {
3917 				int n, tc = k - rx;
3918 
3919 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
3920 				bp->tx_ring[n].tx_cpr = cpr2;
3921 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
3922 			}
3923 		}
3924 		if (tx)
3925 			j++;
3926 	}
3927 	return 0;
3928 }
3929 
3930 static void bnxt_init_ring_struct(struct bnxt *bp)
3931 {
3932 	int i, j;
3933 
3934 	for (i = 0; i < bp->cp_nr_rings; i++) {
3935 		struct bnxt_napi *bnapi = bp->bnapi[i];
3936 		struct bnxt_ring_mem_info *rmem;
3937 		struct bnxt_cp_ring_info *cpr;
3938 		struct bnxt_rx_ring_info *rxr;
3939 		struct bnxt_tx_ring_info *txr;
3940 		struct bnxt_ring_struct *ring;
3941 
3942 		if (!bnapi)
3943 			continue;
3944 
3945 		cpr = &bnapi->cp_ring;
3946 		ring = &cpr->cp_ring_struct;
3947 		rmem = &ring->ring_mem;
3948 		rmem->nr_pages = bp->cp_nr_pages;
3949 		rmem->page_size = HW_CMPD_RING_SIZE;
3950 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
3951 		rmem->dma_arr = cpr->cp_desc_mapping;
3952 		rmem->vmem_size = 0;
3953 
3954 		rxr = bnapi->rx_ring;
3955 		if (!rxr)
3956 			goto skip_rx;
3957 
3958 		ring = &rxr->rx_ring_struct;
3959 		rmem = &ring->ring_mem;
3960 		rmem->nr_pages = bp->rx_nr_pages;
3961 		rmem->page_size = HW_RXBD_RING_SIZE;
3962 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
3963 		rmem->dma_arr = rxr->rx_desc_mapping;
3964 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
3965 		rmem->vmem = (void **)&rxr->rx_buf_ring;
3966 
3967 		ring = &rxr->rx_agg_ring_struct;
3968 		rmem = &ring->ring_mem;
3969 		rmem->nr_pages = bp->rx_agg_nr_pages;
3970 		rmem->page_size = HW_RXBD_RING_SIZE;
3971 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
3972 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
3973 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
3974 		rmem->vmem = (void **)&rxr->rx_agg_ring;
3975 
3976 skip_rx:
3977 		bnxt_for_each_napi_tx(j, bnapi, txr) {
3978 			ring = &txr->tx_ring_struct;
3979 			rmem = &ring->ring_mem;
3980 			rmem->nr_pages = bp->tx_nr_pages;
3981 			rmem->page_size = HW_TXBD_RING_SIZE;
3982 			rmem->pg_arr = (void **)txr->tx_desc_ring;
3983 			rmem->dma_arr = txr->tx_desc_mapping;
3984 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
3985 			rmem->vmem = (void **)&txr->tx_buf_ring;
3986 		}
3987 	}
3988 }
3989 
3990 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
3991 {
3992 	int i;
3993 	u32 prod;
3994 	struct rx_bd **rx_buf_ring;
3995 
3996 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
3997 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
3998 		int j;
3999 		struct rx_bd *rxbd;
4000 
4001 		rxbd = rx_buf_ring[i];
4002 		if (!rxbd)
4003 			continue;
4004 
4005 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4006 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4007 			rxbd->rx_bd_opaque = prod;
4008 		}
4009 	}
4010 }
4011 
4012 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4013 {
4014 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4015 	struct net_device *dev = bp->dev;
4016 	u32 prod;
4017 	int i;
4018 
4019 	prod = rxr->rx_prod;
4020 	for (i = 0; i < bp->rx_ring_size; i++) {
4021 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4022 			netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
4023 				    ring_nr, i, bp->rx_ring_size);
4024 			break;
4025 		}
4026 		prod = NEXT_RX(prod);
4027 	}
4028 	rxr->rx_prod = prod;
4029 
4030 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4031 		return 0;
4032 
4033 	prod = rxr->rx_agg_prod;
4034 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4035 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
4036 			netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
4037 				    ring_nr, i, bp->rx_ring_size);
4038 			break;
4039 		}
4040 		prod = NEXT_RX_AGG(prod);
4041 	}
4042 	rxr->rx_agg_prod = prod;
4043 
4044 	if (rxr->rx_tpa) {
4045 		dma_addr_t mapping;
4046 		u8 *data;
4047 
4048 		for (i = 0; i < bp->max_tpa; i++) {
4049 			data = __bnxt_alloc_rx_frag(bp, &mapping, GFP_KERNEL);
4050 			if (!data)
4051 				return -ENOMEM;
4052 
4053 			rxr->rx_tpa[i].data = data;
4054 			rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4055 			rxr->rx_tpa[i].mapping = mapping;
4056 		}
4057 	}
4058 	return 0;
4059 }
4060 
4061 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4062 {
4063 	struct bnxt_rx_ring_info *rxr;
4064 	struct bnxt_ring_struct *ring;
4065 	u32 type;
4066 
4067 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4068 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4069 
4070 	if (NET_IP_ALIGN == 2)
4071 		type |= RX_BD_FLAGS_SOP;
4072 
4073 	rxr = &bp->rx_ring[ring_nr];
4074 	ring = &rxr->rx_ring_struct;
4075 	bnxt_init_rxbd_pages(ring, type);
4076 
4077 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4078 			     &rxr->bnapi->napi);
4079 
4080 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4081 		bpf_prog_add(bp->xdp_prog, 1);
4082 		rxr->xdp_prog = bp->xdp_prog;
4083 	}
4084 	ring->fw_ring_id = INVALID_HW_RING_ID;
4085 
4086 	ring = &rxr->rx_agg_ring_struct;
4087 	ring->fw_ring_id = INVALID_HW_RING_ID;
4088 
4089 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4090 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4091 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4092 
4093 		bnxt_init_rxbd_pages(ring, type);
4094 	}
4095 
4096 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4097 }
4098 
4099 static void bnxt_init_cp_rings(struct bnxt *bp)
4100 {
4101 	int i, j;
4102 
4103 	for (i = 0; i < bp->cp_nr_rings; i++) {
4104 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4105 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4106 
4107 		ring->fw_ring_id = INVALID_HW_RING_ID;
4108 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4109 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4110 		if (!cpr->cp_ring_arr)
4111 			continue;
4112 		for (j = 0; j < cpr->cp_ring_count; j++) {
4113 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4114 
4115 			ring = &cpr2->cp_ring_struct;
4116 			ring->fw_ring_id = INVALID_HW_RING_ID;
4117 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4118 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4119 		}
4120 	}
4121 }
4122 
4123 static int bnxt_init_rx_rings(struct bnxt *bp)
4124 {
4125 	int i, rc = 0;
4126 
4127 	if (BNXT_RX_PAGE_MODE(bp)) {
4128 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4129 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4130 	} else {
4131 		bp->rx_offset = BNXT_RX_OFFSET;
4132 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4133 	}
4134 
4135 	for (i = 0; i < bp->rx_nr_rings; i++) {
4136 		rc = bnxt_init_one_rx_ring(bp, i);
4137 		if (rc)
4138 			break;
4139 	}
4140 
4141 	return rc;
4142 }
4143 
4144 static int bnxt_init_tx_rings(struct bnxt *bp)
4145 {
4146 	u16 i;
4147 
4148 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4149 				   BNXT_MIN_TX_DESC_CNT);
4150 
4151 	for (i = 0; i < bp->tx_nr_rings; i++) {
4152 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4153 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4154 
4155 		ring->fw_ring_id = INVALID_HW_RING_ID;
4156 
4157 		if (i >= bp->tx_nr_rings_xdp)
4158 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4159 					     NETDEV_QUEUE_TYPE_TX,
4160 					     &txr->bnapi->napi);
4161 	}
4162 
4163 	return 0;
4164 }
4165 
4166 static void bnxt_free_ring_grps(struct bnxt *bp)
4167 {
4168 	kfree(bp->grp_info);
4169 	bp->grp_info = NULL;
4170 }
4171 
4172 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4173 {
4174 	int i;
4175 
4176 	if (irq_re_init) {
4177 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4178 				       sizeof(struct bnxt_ring_grp_info),
4179 				       GFP_KERNEL);
4180 		if (!bp->grp_info)
4181 			return -ENOMEM;
4182 	}
4183 	for (i = 0; i < bp->cp_nr_rings; i++) {
4184 		if (irq_re_init)
4185 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4186 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4187 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4188 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4189 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4190 	}
4191 	return 0;
4192 }
4193 
4194 static void bnxt_free_vnics(struct bnxt *bp)
4195 {
4196 	kfree(bp->vnic_info);
4197 	bp->vnic_info = NULL;
4198 	bp->nr_vnics = 0;
4199 }
4200 
4201 static int bnxt_alloc_vnics(struct bnxt *bp)
4202 {
4203 	int num_vnics = 1;
4204 
4205 #ifdef CONFIG_RFS_ACCEL
4206 	if (bp->flags & BNXT_FLAG_RFS) {
4207 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4208 			num_vnics++;
4209 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4210 			num_vnics += bp->rx_nr_rings;
4211 	}
4212 #endif
4213 
4214 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4215 		num_vnics++;
4216 
4217 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4218 				GFP_KERNEL);
4219 	if (!bp->vnic_info)
4220 		return -ENOMEM;
4221 
4222 	bp->nr_vnics = num_vnics;
4223 	return 0;
4224 }
4225 
4226 static void bnxt_init_vnics(struct bnxt *bp)
4227 {
4228 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4229 	int i;
4230 
4231 	for (i = 0; i < bp->nr_vnics; i++) {
4232 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4233 		int j;
4234 
4235 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4236 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4237 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4238 
4239 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4240 
4241 		if (bp->vnic_info[i].rss_hash_key) {
4242 			if (i == BNXT_VNIC_DEFAULT) {
4243 				u8 *key = (void *)vnic->rss_hash_key;
4244 				int k;
4245 
4246 				if (!bp->rss_hash_key_valid &&
4247 				    !bp->rss_hash_key_updated) {
4248 					get_random_bytes(bp->rss_hash_key,
4249 							 HW_HASH_KEY_SIZE);
4250 					bp->rss_hash_key_updated = true;
4251 				}
4252 
4253 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4254 				       HW_HASH_KEY_SIZE);
4255 
4256 				if (!bp->rss_hash_key_updated)
4257 					continue;
4258 
4259 				bp->rss_hash_key_updated = false;
4260 				bp->rss_hash_key_valid = true;
4261 
4262 				bp->toeplitz_prefix = 0;
4263 				for (k = 0; k < 8; k++) {
4264 					bp->toeplitz_prefix <<= 8;
4265 					bp->toeplitz_prefix |= key[k];
4266 				}
4267 			} else {
4268 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4269 				       HW_HASH_KEY_SIZE);
4270 			}
4271 		}
4272 	}
4273 }
4274 
4275 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4276 {
4277 	int pages;
4278 
4279 	pages = ring_size / desc_per_pg;
4280 
4281 	if (!pages)
4282 		return 1;
4283 
4284 	pages++;
4285 
4286 	while (pages & (pages - 1))
4287 		pages++;
4288 
4289 	return pages;
4290 }
4291 
4292 void bnxt_set_tpa_flags(struct bnxt *bp)
4293 {
4294 	bp->flags &= ~BNXT_FLAG_TPA;
4295 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4296 		return;
4297 	if (bp->dev->features & NETIF_F_LRO)
4298 		bp->flags |= BNXT_FLAG_LRO;
4299 	else if (bp->dev->features & NETIF_F_GRO_HW)
4300 		bp->flags |= BNXT_FLAG_GRO;
4301 }
4302 
4303 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4304  * be set on entry.
4305  */
4306 void bnxt_set_ring_params(struct bnxt *bp)
4307 {
4308 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4309 	u32 agg_factor = 0, agg_ring_size = 0;
4310 
4311 	/* 8 for CRC and VLAN */
4312 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4313 
4314 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4315 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4316 
4317 	bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
4318 	ring_size = bp->rx_ring_size;
4319 	bp->rx_agg_ring_size = 0;
4320 	bp->rx_agg_nr_pages = 0;
4321 
4322 	if (bp->flags & BNXT_FLAG_TPA)
4323 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4324 
4325 	bp->flags &= ~BNXT_FLAG_JUMBO;
4326 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4327 		u32 jumbo_factor;
4328 
4329 		bp->flags |= BNXT_FLAG_JUMBO;
4330 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4331 		if (jumbo_factor > agg_factor)
4332 			agg_factor = jumbo_factor;
4333 	}
4334 	if (agg_factor) {
4335 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4336 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4337 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4338 				    bp->rx_ring_size, ring_size);
4339 			bp->rx_ring_size = ring_size;
4340 		}
4341 		agg_ring_size = ring_size * agg_factor;
4342 
4343 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4344 							RX_DESC_CNT);
4345 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4346 			u32 tmp = agg_ring_size;
4347 
4348 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4349 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4350 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4351 				    tmp, agg_ring_size);
4352 		}
4353 		bp->rx_agg_ring_size = agg_ring_size;
4354 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4355 
4356 		if (BNXT_RX_PAGE_MODE(bp)) {
4357 			rx_space = PAGE_SIZE;
4358 			rx_size = PAGE_SIZE -
4359 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4360 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4361 		} else {
4362 			rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
4363 			rx_space = rx_size + NET_SKB_PAD +
4364 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4365 		}
4366 	}
4367 
4368 	bp->rx_buf_use_size = rx_size;
4369 	bp->rx_buf_size = rx_space;
4370 
4371 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4372 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4373 
4374 	ring_size = bp->tx_ring_size;
4375 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4376 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4377 
4378 	max_rx_cmpl = bp->rx_ring_size;
4379 	/* MAX TPA needs to be added because TPA_START completions are
4380 	 * immediately recycled, so the TPA completions are not bound by
4381 	 * the RX ring size.
4382 	 */
4383 	if (bp->flags & BNXT_FLAG_TPA)
4384 		max_rx_cmpl += bp->max_tpa;
4385 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4386 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4387 	bp->cp_ring_size = ring_size;
4388 
4389 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4390 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4391 		bp->cp_nr_pages = MAX_CP_PAGES;
4392 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4393 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4394 			    ring_size, bp->cp_ring_size);
4395 	}
4396 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4397 	bp->cp_ring_mask = bp->cp_bit - 1;
4398 }
4399 
4400 /* Changing allocation mode of RX rings.
4401  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4402  */
4403 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4404 {
4405 	struct net_device *dev = bp->dev;
4406 
4407 	if (page_mode) {
4408 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
4409 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4410 
4411 		if (bp->xdp_prog->aux->xdp_has_frags)
4412 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4413 		else
4414 			dev->max_mtu =
4415 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4416 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4417 			bp->flags |= BNXT_FLAG_JUMBO;
4418 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4419 		} else {
4420 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4421 			bp->rx_skb_func = bnxt_rx_page_skb;
4422 		}
4423 		bp->rx_dir = DMA_BIDIRECTIONAL;
4424 		/* Disable LRO or GRO_HW */
4425 		netdev_update_features(dev);
4426 	} else {
4427 		dev->max_mtu = bp->max_mtu;
4428 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4429 		bp->rx_dir = DMA_FROM_DEVICE;
4430 		bp->rx_skb_func = bnxt_rx_skb;
4431 	}
4432 	return 0;
4433 }
4434 
4435 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4436 {
4437 	int i;
4438 	struct bnxt_vnic_info *vnic;
4439 	struct pci_dev *pdev = bp->pdev;
4440 
4441 	if (!bp->vnic_info)
4442 		return;
4443 
4444 	for (i = 0; i < bp->nr_vnics; i++) {
4445 		vnic = &bp->vnic_info[i];
4446 
4447 		kfree(vnic->fw_grp_ids);
4448 		vnic->fw_grp_ids = NULL;
4449 
4450 		kfree(vnic->uc_list);
4451 		vnic->uc_list = NULL;
4452 
4453 		if (vnic->mc_list) {
4454 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4455 					  vnic->mc_list, vnic->mc_list_mapping);
4456 			vnic->mc_list = NULL;
4457 		}
4458 
4459 		if (vnic->rss_table) {
4460 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4461 					  vnic->rss_table,
4462 					  vnic->rss_table_dma_addr);
4463 			vnic->rss_table = NULL;
4464 		}
4465 
4466 		vnic->rss_hash_key = NULL;
4467 		vnic->flags = 0;
4468 	}
4469 }
4470 
4471 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4472 {
4473 	int i, rc = 0, size;
4474 	struct bnxt_vnic_info *vnic;
4475 	struct pci_dev *pdev = bp->pdev;
4476 	int max_rings;
4477 
4478 	for (i = 0; i < bp->nr_vnics; i++) {
4479 		vnic = &bp->vnic_info[i];
4480 
4481 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4482 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4483 
4484 			if (mem_size > 0) {
4485 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4486 				if (!vnic->uc_list) {
4487 					rc = -ENOMEM;
4488 					goto out;
4489 				}
4490 			}
4491 		}
4492 
4493 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4494 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4495 			vnic->mc_list =
4496 				dma_alloc_coherent(&pdev->dev,
4497 						   vnic->mc_list_size,
4498 						   &vnic->mc_list_mapping,
4499 						   GFP_KERNEL);
4500 			if (!vnic->mc_list) {
4501 				rc = -ENOMEM;
4502 				goto out;
4503 			}
4504 		}
4505 
4506 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4507 			goto vnic_skip_grps;
4508 
4509 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4510 			max_rings = bp->rx_nr_rings;
4511 		else
4512 			max_rings = 1;
4513 
4514 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4515 		if (!vnic->fw_grp_ids) {
4516 			rc = -ENOMEM;
4517 			goto out;
4518 		}
4519 vnic_skip_grps:
4520 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4521 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4522 			continue;
4523 
4524 		/* Allocate rss table and hash key */
4525 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4526 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4527 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4528 
4529 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4530 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4531 						     vnic->rss_table_size,
4532 						     &vnic->rss_table_dma_addr,
4533 						     GFP_KERNEL);
4534 		if (!vnic->rss_table) {
4535 			rc = -ENOMEM;
4536 			goto out;
4537 		}
4538 
4539 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4540 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4541 	}
4542 	return 0;
4543 
4544 out:
4545 	return rc;
4546 }
4547 
4548 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4549 {
4550 	struct bnxt_hwrm_wait_token *token;
4551 
4552 	dma_pool_destroy(bp->hwrm_dma_pool);
4553 	bp->hwrm_dma_pool = NULL;
4554 
4555 	rcu_read_lock();
4556 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4557 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4558 	rcu_read_unlock();
4559 }
4560 
4561 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4562 {
4563 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4564 					    BNXT_HWRM_DMA_SIZE,
4565 					    BNXT_HWRM_DMA_ALIGN, 0);
4566 	if (!bp->hwrm_dma_pool)
4567 		return -ENOMEM;
4568 
4569 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4570 
4571 	return 0;
4572 }
4573 
4574 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4575 {
4576 	kfree(stats->hw_masks);
4577 	stats->hw_masks = NULL;
4578 	kfree(stats->sw_stats);
4579 	stats->sw_stats = NULL;
4580 	if (stats->hw_stats) {
4581 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
4582 				  stats->hw_stats_map);
4583 		stats->hw_stats = NULL;
4584 	}
4585 }
4586 
4587 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
4588 				bool alloc_masks)
4589 {
4590 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
4591 					     &stats->hw_stats_map, GFP_KERNEL);
4592 	if (!stats->hw_stats)
4593 		return -ENOMEM;
4594 
4595 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
4596 	if (!stats->sw_stats)
4597 		goto stats_mem_err;
4598 
4599 	if (alloc_masks) {
4600 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
4601 		if (!stats->hw_masks)
4602 			goto stats_mem_err;
4603 	}
4604 	return 0;
4605 
4606 stats_mem_err:
4607 	bnxt_free_stats_mem(bp, stats);
4608 	return -ENOMEM;
4609 }
4610 
4611 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
4612 {
4613 	int i;
4614 
4615 	for (i = 0; i < count; i++)
4616 		mask_arr[i] = mask;
4617 }
4618 
4619 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
4620 {
4621 	int i;
4622 
4623 	for (i = 0; i < count; i++)
4624 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
4625 }
4626 
4627 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
4628 				    struct bnxt_stats_mem *stats)
4629 {
4630 	struct hwrm_func_qstats_ext_output *resp;
4631 	struct hwrm_func_qstats_ext_input *req;
4632 	__le64 *hw_masks;
4633 	int rc;
4634 
4635 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
4636 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4637 		return -EOPNOTSUPP;
4638 
4639 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
4640 	if (rc)
4641 		return rc;
4642 
4643 	req->fid = cpu_to_le16(0xffff);
4644 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4645 
4646 	resp = hwrm_req_hold(bp, req);
4647 	rc = hwrm_req_send(bp, req);
4648 	if (!rc) {
4649 		hw_masks = &resp->rx_ucast_pkts;
4650 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
4651 	}
4652 	hwrm_req_drop(bp, req);
4653 	return rc;
4654 }
4655 
4656 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
4657 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
4658 
4659 static void bnxt_init_stats(struct bnxt *bp)
4660 {
4661 	struct bnxt_napi *bnapi = bp->bnapi[0];
4662 	struct bnxt_cp_ring_info *cpr;
4663 	struct bnxt_stats_mem *stats;
4664 	__le64 *rx_stats, *tx_stats;
4665 	int rc, rx_count, tx_count;
4666 	u64 *rx_masks, *tx_masks;
4667 	u64 mask;
4668 	u8 flags;
4669 
4670 	cpr = &bnapi->cp_ring;
4671 	stats = &cpr->stats;
4672 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
4673 	if (rc) {
4674 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4675 			mask = (1ULL << 48) - 1;
4676 		else
4677 			mask = -1ULL;
4678 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
4679 	}
4680 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
4681 		stats = &bp->port_stats;
4682 		rx_stats = stats->hw_stats;
4683 		rx_masks = stats->hw_masks;
4684 		rx_count = sizeof(struct rx_port_stats) / 8;
4685 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4686 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4687 		tx_count = sizeof(struct tx_port_stats) / 8;
4688 
4689 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
4690 		rc = bnxt_hwrm_port_qstats(bp, flags);
4691 		if (rc) {
4692 			mask = (1ULL << 40) - 1;
4693 
4694 			bnxt_fill_masks(rx_masks, mask, rx_count);
4695 			bnxt_fill_masks(tx_masks, mask, tx_count);
4696 		} else {
4697 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4698 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
4699 			bnxt_hwrm_port_qstats(bp, 0);
4700 		}
4701 	}
4702 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
4703 		stats = &bp->rx_port_stats_ext;
4704 		rx_stats = stats->hw_stats;
4705 		rx_masks = stats->hw_masks;
4706 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
4707 		stats = &bp->tx_port_stats_ext;
4708 		tx_stats = stats->hw_stats;
4709 		tx_masks = stats->hw_masks;
4710 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
4711 
4712 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4713 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
4714 		if (rc) {
4715 			mask = (1ULL << 40) - 1;
4716 
4717 			bnxt_fill_masks(rx_masks, mask, rx_count);
4718 			if (tx_stats)
4719 				bnxt_fill_masks(tx_masks, mask, tx_count);
4720 		} else {
4721 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4722 			if (tx_stats)
4723 				bnxt_copy_hw_masks(tx_masks, tx_stats,
4724 						   tx_count);
4725 			bnxt_hwrm_port_qstats_ext(bp, 0);
4726 		}
4727 	}
4728 }
4729 
4730 static void bnxt_free_port_stats(struct bnxt *bp)
4731 {
4732 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
4733 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
4734 
4735 	bnxt_free_stats_mem(bp, &bp->port_stats);
4736 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
4737 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
4738 }
4739 
4740 static void bnxt_free_ring_stats(struct bnxt *bp)
4741 {
4742 	int i;
4743 
4744 	if (!bp->bnapi)
4745 		return;
4746 
4747 	for (i = 0; i < bp->cp_nr_rings; i++) {
4748 		struct bnxt_napi *bnapi = bp->bnapi[i];
4749 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4750 
4751 		bnxt_free_stats_mem(bp, &cpr->stats);
4752 	}
4753 }
4754 
4755 static int bnxt_alloc_stats(struct bnxt *bp)
4756 {
4757 	u32 size, i;
4758 	int rc;
4759 
4760 	size = bp->hw_ring_stats_size;
4761 
4762 	for (i = 0; i < bp->cp_nr_rings; i++) {
4763 		struct bnxt_napi *bnapi = bp->bnapi[i];
4764 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4765 
4766 		cpr->stats.len = size;
4767 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
4768 		if (rc)
4769 			return rc;
4770 
4771 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4772 	}
4773 
4774 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
4775 		return 0;
4776 
4777 	if (bp->port_stats.hw_stats)
4778 		goto alloc_ext_stats;
4779 
4780 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
4781 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
4782 	if (rc)
4783 		return rc;
4784 
4785 	bp->flags |= BNXT_FLAG_PORT_STATS;
4786 
4787 alloc_ext_stats:
4788 	/* Display extended statistics only if FW supports it */
4789 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
4790 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
4791 			return 0;
4792 
4793 	if (bp->rx_port_stats_ext.hw_stats)
4794 		goto alloc_tx_ext_stats;
4795 
4796 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
4797 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
4798 	/* Extended stats are optional */
4799 	if (rc)
4800 		return 0;
4801 
4802 alloc_tx_ext_stats:
4803 	if (bp->tx_port_stats_ext.hw_stats)
4804 		return 0;
4805 
4806 	if (bp->hwrm_spec_code >= 0x10902 ||
4807 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
4808 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
4809 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
4810 		/* Extended stats are optional */
4811 		if (rc)
4812 			return 0;
4813 	}
4814 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
4815 	return 0;
4816 }
4817 
4818 static void bnxt_clear_ring_indices(struct bnxt *bp)
4819 {
4820 	int i, j;
4821 
4822 	if (!bp->bnapi)
4823 		return;
4824 
4825 	for (i = 0; i < bp->cp_nr_rings; i++) {
4826 		struct bnxt_napi *bnapi = bp->bnapi[i];
4827 		struct bnxt_cp_ring_info *cpr;
4828 		struct bnxt_rx_ring_info *rxr;
4829 		struct bnxt_tx_ring_info *txr;
4830 
4831 		if (!bnapi)
4832 			continue;
4833 
4834 		cpr = &bnapi->cp_ring;
4835 		cpr->cp_raw_cons = 0;
4836 
4837 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4838 			txr->tx_prod = 0;
4839 			txr->tx_cons = 0;
4840 			txr->tx_hw_cons = 0;
4841 		}
4842 
4843 		rxr = bnapi->rx_ring;
4844 		if (rxr) {
4845 			rxr->rx_prod = 0;
4846 			rxr->rx_agg_prod = 0;
4847 			rxr->rx_sw_agg_prod = 0;
4848 			rxr->rx_next_cons = 0;
4849 		}
4850 		bnapi->events = 0;
4851 	}
4852 }
4853 
4854 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
4855 {
4856 	u8 type = fltr->type, flags = fltr->flags;
4857 
4858 	INIT_LIST_HEAD(&fltr->list);
4859 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
4860 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
4861 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
4862 }
4863 
4864 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
4865 {
4866 	if (!list_empty(&fltr->list))
4867 		list_del_init(&fltr->list);
4868 }
4869 
4870 void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
4871 {
4872 	struct bnxt_filter_base *usr_fltr, *tmp;
4873 
4874 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
4875 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
4876 			continue;
4877 		bnxt_del_one_usr_fltr(bp, usr_fltr);
4878 	}
4879 }
4880 
4881 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
4882 {
4883 	hlist_del(&fltr->hash);
4884 	bnxt_del_one_usr_fltr(bp, fltr);
4885 	if (fltr->flags) {
4886 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
4887 		bp->ntp_fltr_count--;
4888 	}
4889 	kfree(fltr);
4890 }
4891 
4892 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
4893 {
4894 	int i;
4895 
4896 	/* Under rtnl_lock and all our NAPIs have been disabled.  It's
4897 	 * safe to delete the hash table.
4898 	 */
4899 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
4900 		struct hlist_head *head;
4901 		struct hlist_node *tmp;
4902 		struct bnxt_ntuple_filter *fltr;
4903 
4904 		head = &bp->ntp_fltr_hash_tbl[i];
4905 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
4906 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
4907 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
4908 				     !list_empty(&fltr->base.list)))
4909 				continue;
4910 			bnxt_del_fltr(bp, &fltr->base);
4911 		}
4912 	}
4913 	if (!all)
4914 		return;
4915 
4916 	bitmap_free(bp->ntp_fltr_bmap);
4917 	bp->ntp_fltr_bmap = NULL;
4918 	bp->ntp_fltr_count = 0;
4919 }
4920 
4921 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
4922 {
4923 	int i, rc = 0;
4924 
4925 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
4926 		return 0;
4927 
4928 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
4929 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
4930 
4931 	bp->ntp_fltr_count = 0;
4932 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
4933 
4934 	if (!bp->ntp_fltr_bmap)
4935 		rc = -ENOMEM;
4936 
4937 	return rc;
4938 }
4939 
4940 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
4941 {
4942 	int i;
4943 
4944 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
4945 		struct hlist_head *head;
4946 		struct hlist_node *tmp;
4947 		struct bnxt_l2_filter *fltr;
4948 
4949 		head = &bp->l2_fltr_hash_tbl[i];
4950 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
4951 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
4952 				     !list_empty(&fltr->base.list)))
4953 				continue;
4954 			bnxt_del_fltr(bp, &fltr->base);
4955 		}
4956 	}
4957 }
4958 
4959 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
4960 {
4961 	int i;
4962 
4963 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
4964 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
4965 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
4966 }
4967 
4968 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
4969 {
4970 	bnxt_free_vnic_attributes(bp);
4971 	bnxt_free_tx_rings(bp);
4972 	bnxt_free_rx_rings(bp);
4973 	bnxt_free_cp_rings(bp);
4974 	bnxt_free_all_cp_arrays(bp);
4975 	bnxt_free_ntp_fltrs(bp, false);
4976 	bnxt_free_l2_filters(bp, false);
4977 	if (irq_re_init) {
4978 		bnxt_free_ring_stats(bp);
4979 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
4980 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
4981 			bnxt_free_port_stats(bp);
4982 		bnxt_free_ring_grps(bp);
4983 		bnxt_free_vnics(bp);
4984 		kfree(bp->tx_ring_map);
4985 		bp->tx_ring_map = NULL;
4986 		kfree(bp->tx_ring);
4987 		bp->tx_ring = NULL;
4988 		kfree(bp->rx_ring);
4989 		bp->rx_ring = NULL;
4990 		kfree(bp->bnapi);
4991 		bp->bnapi = NULL;
4992 	} else {
4993 		bnxt_clear_ring_indices(bp);
4994 	}
4995 }
4996 
4997 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
4998 {
4999 	int i, j, rc, size, arr_size;
5000 	void *bnapi;
5001 
5002 	if (irq_re_init) {
5003 		/* Allocate bnapi mem pointer array and mem block for
5004 		 * all queues
5005 		 */
5006 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5007 				bp->cp_nr_rings);
5008 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5009 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5010 		if (!bnapi)
5011 			return -ENOMEM;
5012 
5013 		bp->bnapi = bnapi;
5014 		bnapi += arr_size;
5015 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5016 			bp->bnapi[i] = bnapi;
5017 			bp->bnapi[i]->index = i;
5018 			bp->bnapi[i]->bp = bp;
5019 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5020 				struct bnxt_cp_ring_info *cpr =
5021 					&bp->bnapi[i]->cp_ring;
5022 
5023 				cpr->cp_ring_struct.ring_mem.flags =
5024 					BNXT_RMEM_RING_PTE_FLAG;
5025 			}
5026 		}
5027 
5028 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5029 				      sizeof(struct bnxt_rx_ring_info),
5030 				      GFP_KERNEL);
5031 		if (!bp->rx_ring)
5032 			return -ENOMEM;
5033 
5034 		for (i = 0; i < bp->rx_nr_rings; i++) {
5035 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5036 
5037 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5038 				rxr->rx_ring_struct.ring_mem.flags =
5039 					BNXT_RMEM_RING_PTE_FLAG;
5040 				rxr->rx_agg_ring_struct.ring_mem.flags =
5041 					BNXT_RMEM_RING_PTE_FLAG;
5042 			} else {
5043 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5044 			}
5045 			rxr->bnapi = bp->bnapi[i];
5046 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5047 		}
5048 
5049 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5050 				      sizeof(struct bnxt_tx_ring_info),
5051 				      GFP_KERNEL);
5052 		if (!bp->tx_ring)
5053 			return -ENOMEM;
5054 
5055 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5056 					  GFP_KERNEL);
5057 
5058 		if (!bp->tx_ring_map)
5059 			return -ENOMEM;
5060 
5061 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5062 			j = 0;
5063 		else
5064 			j = bp->rx_nr_rings;
5065 
5066 		for (i = 0; i < bp->tx_nr_rings; i++) {
5067 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5068 			struct bnxt_napi *bnapi2;
5069 
5070 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5071 				txr->tx_ring_struct.ring_mem.flags =
5072 					BNXT_RMEM_RING_PTE_FLAG;
5073 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5074 			if (i >= bp->tx_nr_rings_xdp) {
5075 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5076 
5077 				bnapi2 = bp->bnapi[k];
5078 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5079 				txr->tx_napi_idx =
5080 					BNXT_RING_TO_TC(bp, txr->txq_index);
5081 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5082 				bnapi2->tx_int = bnxt_tx_int;
5083 			} else {
5084 				bnapi2 = bp->bnapi[j];
5085 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5086 				bnapi2->tx_ring[0] = txr;
5087 				bnapi2->tx_int = bnxt_tx_int_xdp;
5088 				j++;
5089 			}
5090 			txr->bnapi = bnapi2;
5091 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5092 				txr->tx_cpr = &bnapi2->cp_ring;
5093 		}
5094 
5095 		rc = bnxt_alloc_stats(bp);
5096 		if (rc)
5097 			goto alloc_mem_err;
5098 		bnxt_init_stats(bp);
5099 
5100 		rc = bnxt_alloc_ntp_fltrs(bp);
5101 		if (rc)
5102 			goto alloc_mem_err;
5103 
5104 		rc = bnxt_alloc_vnics(bp);
5105 		if (rc)
5106 			goto alloc_mem_err;
5107 	}
5108 
5109 	rc = bnxt_alloc_all_cp_arrays(bp);
5110 	if (rc)
5111 		goto alloc_mem_err;
5112 
5113 	bnxt_init_ring_struct(bp);
5114 
5115 	rc = bnxt_alloc_rx_rings(bp);
5116 	if (rc)
5117 		goto alloc_mem_err;
5118 
5119 	rc = bnxt_alloc_tx_rings(bp);
5120 	if (rc)
5121 		goto alloc_mem_err;
5122 
5123 	rc = bnxt_alloc_cp_rings(bp);
5124 	if (rc)
5125 		goto alloc_mem_err;
5126 
5127 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5128 						  BNXT_VNIC_MCAST_FLAG |
5129 						  BNXT_VNIC_UCAST_FLAG;
5130 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5131 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5132 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5133 
5134 	rc = bnxt_alloc_vnic_attributes(bp);
5135 	if (rc)
5136 		goto alloc_mem_err;
5137 	return 0;
5138 
5139 alloc_mem_err:
5140 	bnxt_free_mem(bp, true);
5141 	return rc;
5142 }
5143 
5144 static void bnxt_disable_int(struct bnxt *bp)
5145 {
5146 	int i;
5147 
5148 	if (!bp->bnapi)
5149 		return;
5150 
5151 	for (i = 0; i < bp->cp_nr_rings; i++) {
5152 		struct bnxt_napi *bnapi = bp->bnapi[i];
5153 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5154 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5155 
5156 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5157 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5158 	}
5159 }
5160 
5161 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5162 {
5163 	struct bnxt_napi *bnapi = bp->bnapi[n];
5164 	struct bnxt_cp_ring_info *cpr;
5165 
5166 	cpr = &bnapi->cp_ring;
5167 	return cpr->cp_ring_struct.map_idx;
5168 }
5169 
5170 static void bnxt_disable_int_sync(struct bnxt *bp)
5171 {
5172 	int i;
5173 
5174 	if (!bp->irq_tbl)
5175 		return;
5176 
5177 	atomic_inc(&bp->intr_sem);
5178 
5179 	bnxt_disable_int(bp);
5180 	for (i = 0; i < bp->cp_nr_rings; i++) {
5181 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5182 
5183 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5184 	}
5185 }
5186 
5187 static void bnxt_enable_int(struct bnxt *bp)
5188 {
5189 	int i;
5190 
5191 	atomic_set(&bp->intr_sem, 0);
5192 	for (i = 0; i < bp->cp_nr_rings; i++) {
5193 		struct bnxt_napi *bnapi = bp->bnapi[i];
5194 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5195 
5196 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5197 	}
5198 }
5199 
5200 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5201 			    bool async_only)
5202 {
5203 	DECLARE_BITMAP(async_events_bmap, 256);
5204 	u32 *events = (u32 *)async_events_bmap;
5205 	struct hwrm_func_drv_rgtr_output *resp;
5206 	struct hwrm_func_drv_rgtr_input *req;
5207 	u32 flags;
5208 	int rc, i;
5209 
5210 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5211 	if (rc)
5212 		return rc;
5213 
5214 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5215 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5216 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5217 
5218 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5219 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5220 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5221 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5222 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5223 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5224 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5225 	req->flags = cpu_to_le32(flags);
5226 	req->ver_maj_8b = DRV_VER_MAJ;
5227 	req->ver_min_8b = DRV_VER_MIN;
5228 	req->ver_upd_8b = DRV_VER_UPD;
5229 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5230 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5231 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5232 
5233 	if (BNXT_PF(bp)) {
5234 		u32 data[8];
5235 		int i;
5236 
5237 		memset(data, 0, sizeof(data));
5238 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5239 			u16 cmd = bnxt_vf_req_snif[i];
5240 			unsigned int bit, idx;
5241 
5242 			idx = cmd / 32;
5243 			bit = cmd % 32;
5244 			data[idx] |= 1 << bit;
5245 		}
5246 
5247 		for (i = 0; i < 8; i++)
5248 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5249 
5250 		req->enables |=
5251 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5252 	}
5253 
5254 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5255 		req->flags |= cpu_to_le32(
5256 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5257 
5258 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5259 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5260 		u16 event_id = bnxt_async_events_arr[i];
5261 
5262 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5263 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5264 			continue;
5265 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5266 		    !bp->ptp_cfg)
5267 			continue;
5268 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5269 	}
5270 	if (bmap && bmap_size) {
5271 		for (i = 0; i < bmap_size; i++) {
5272 			if (test_bit(i, bmap))
5273 				__set_bit(i, async_events_bmap);
5274 		}
5275 	}
5276 	for (i = 0; i < 8; i++)
5277 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5278 
5279 	if (async_only)
5280 		req->enables =
5281 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5282 
5283 	resp = hwrm_req_hold(bp, req);
5284 	rc = hwrm_req_send(bp, req);
5285 	if (!rc) {
5286 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5287 		if (resp->flags &
5288 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5289 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5290 	}
5291 	hwrm_req_drop(bp, req);
5292 	return rc;
5293 }
5294 
5295 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5296 {
5297 	struct hwrm_func_drv_unrgtr_input *req;
5298 	int rc;
5299 
5300 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5301 		return 0;
5302 
5303 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5304 	if (rc)
5305 		return rc;
5306 	return hwrm_req_send(bp, req);
5307 }
5308 
5309 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5310 
5311 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5312 {
5313 	struct hwrm_tunnel_dst_port_free_input *req;
5314 	int rc;
5315 
5316 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5317 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5318 		return 0;
5319 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5320 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5321 		return 0;
5322 
5323 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5324 	if (rc)
5325 		return rc;
5326 
5327 	req->tunnel_type = tunnel_type;
5328 
5329 	switch (tunnel_type) {
5330 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5331 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5332 		bp->vxlan_port = 0;
5333 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5334 		break;
5335 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5336 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5337 		bp->nge_port = 0;
5338 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5339 		break;
5340 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5341 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5342 		bp->vxlan_gpe_port = 0;
5343 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5344 		break;
5345 	default:
5346 		break;
5347 	}
5348 
5349 	rc = hwrm_req_send(bp, req);
5350 	if (rc)
5351 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5352 			   rc);
5353 	if (bp->flags & BNXT_FLAG_TPA)
5354 		bnxt_set_tpa(bp, true);
5355 	return rc;
5356 }
5357 
5358 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5359 					   u8 tunnel_type)
5360 {
5361 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5362 	struct hwrm_tunnel_dst_port_alloc_input *req;
5363 	int rc;
5364 
5365 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5366 	if (rc)
5367 		return rc;
5368 
5369 	req->tunnel_type = tunnel_type;
5370 	req->tunnel_dst_port_val = port;
5371 
5372 	resp = hwrm_req_hold(bp, req);
5373 	rc = hwrm_req_send(bp, req);
5374 	if (rc) {
5375 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5376 			   rc);
5377 		goto err_out;
5378 	}
5379 
5380 	switch (tunnel_type) {
5381 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5382 		bp->vxlan_port = port;
5383 		bp->vxlan_fw_dst_port_id =
5384 			le16_to_cpu(resp->tunnel_dst_port_id);
5385 		break;
5386 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5387 		bp->nge_port = port;
5388 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5389 		break;
5390 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5391 		bp->vxlan_gpe_port = port;
5392 		bp->vxlan_gpe_fw_dst_port_id =
5393 			le16_to_cpu(resp->tunnel_dst_port_id);
5394 		break;
5395 	default:
5396 		break;
5397 	}
5398 	if (bp->flags & BNXT_FLAG_TPA)
5399 		bnxt_set_tpa(bp, true);
5400 
5401 err_out:
5402 	hwrm_req_drop(bp, req);
5403 	return rc;
5404 }
5405 
5406 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5407 {
5408 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5409 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5410 	int rc;
5411 
5412 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5413 	if (rc)
5414 		return rc;
5415 
5416 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5417 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5418 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5419 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5420 	}
5421 	req->mask = cpu_to_le32(vnic->rx_mask);
5422 	return hwrm_req_send_silent(bp, req);
5423 }
5424 
5425 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5426 {
5427 	if (!atomic_dec_and_test(&fltr->refcnt))
5428 		return;
5429 	spin_lock_bh(&bp->ntp_fltr_lock);
5430 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5431 		spin_unlock_bh(&bp->ntp_fltr_lock);
5432 		return;
5433 	}
5434 	hlist_del_rcu(&fltr->base.hash);
5435 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5436 	if (fltr->base.flags) {
5437 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5438 		bp->ntp_fltr_count--;
5439 	}
5440 	spin_unlock_bh(&bp->ntp_fltr_lock);
5441 	kfree_rcu(fltr, base.rcu);
5442 }
5443 
5444 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5445 						      struct bnxt_l2_key *key,
5446 						      u32 idx)
5447 {
5448 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5449 	struct bnxt_l2_filter *fltr;
5450 
5451 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5452 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5453 
5454 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5455 		    l2_key->vlan == key->vlan)
5456 			return fltr;
5457 	}
5458 	return NULL;
5459 }
5460 
5461 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5462 						    struct bnxt_l2_key *key,
5463 						    u32 idx)
5464 {
5465 	struct bnxt_l2_filter *fltr = NULL;
5466 
5467 	rcu_read_lock();
5468 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5469 	if (fltr)
5470 		atomic_inc(&fltr->refcnt);
5471 	rcu_read_unlock();
5472 	return fltr;
5473 }
5474 
5475 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5476 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5477 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5478 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5479 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5480 
5481 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5482 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5483 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5484 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5485 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5486 
5487 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5488 {
5489 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5490 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5491 			return sizeof(fkeys->addrs.v4addrs) +
5492 			       sizeof(fkeys->ports);
5493 
5494 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5495 			return sizeof(fkeys->addrs.v4addrs);
5496 	}
5497 
5498 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5499 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5500 			return sizeof(fkeys->addrs.v6addrs) +
5501 			       sizeof(fkeys->ports);
5502 
5503 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5504 			return sizeof(fkeys->addrs.v6addrs);
5505 	}
5506 
5507 	return 0;
5508 }
5509 
5510 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5511 			 const unsigned char *key)
5512 {
5513 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5514 	struct bnxt_ipv4_tuple tuple4;
5515 	struct bnxt_ipv6_tuple tuple6;
5516 	int i, j, len = 0;
5517 	u8 *four_tuple;
5518 
5519 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5520 	if (!len)
5521 		return 0;
5522 
5523 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5524 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5525 		tuple4.ports = fkeys->ports;
5526 		four_tuple = (unsigned char *)&tuple4;
5527 	} else {
5528 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5529 		tuple6.ports = fkeys->ports;
5530 		four_tuple = (unsigned char *)&tuple6;
5531 	}
5532 
5533 	for (i = 0, j = 8; i < len; i++, j++) {
5534 		u8 byte = four_tuple[i];
5535 		int bit;
5536 
5537 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5538 			if (byte & 0x80)
5539 				hash ^= prefix;
5540 		}
5541 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5542 	}
5543 
5544 	/* The valid part of the hash is in the upper 32 bits. */
5545 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5546 }
5547 
5548 #ifdef CONFIG_RFS_ACCEL
5549 static struct bnxt_l2_filter *
5550 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5551 {
5552 	struct bnxt_l2_filter *fltr;
5553 	u32 idx;
5554 
5555 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5556 	      BNXT_L2_FLTR_HASH_MASK;
5557 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5558 	return fltr;
5559 }
5560 #endif
5561 
5562 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
5563 			       struct bnxt_l2_key *key, u32 idx)
5564 {
5565 	struct hlist_head *head;
5566 
5567 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
5568 	fltr->l2_key.vlan = key->vlan;
5569 	fltr->base.type = BNXT_FLTR_TYPE_L2;
5570 	if (fltr->base.flags) {
5571 		int bit_id;
5572 
5573 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
5574 						 bp->max_fltr, 0);
5575 		if (bit_id < 0)
5576 			return -ENOMEM;
5577 		fltr->base.sw_id = (u16)bit_id;
5578 		bp->ntp_fltr_count++;
5579 	}
5580 	head = &bp->l2_fltr_hash_tbl[idx];
5581 	hlist_add_head_rcu(&fltr->base.hash, head);
5582 	bnxt_insert_usr_fltr(bp, &fltr->base);
5583 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
5584 	atomic_set(&fltr->refcnt, 1);
5585 	return 0;
5586 }
5587 
5588 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
5589 						   struct bnxt_l2_key *key,
5590 						   gfp_t gfp)
5591 {
5592 	struct bnxt_l2_filter *fltr;
5593 	u32 idx;
5594 	int rc;
5595 
5596 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5597 	      BNXT_L2_FLTR_HASH_MASK;
5598 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5599 	if (fltr)
5600 		return fltr;
5601 
5602 	fltr = kzalloc(sizeof(*fltr), gfp);
5603 	if (!fltr)
5604 		return ERR_PTR(-ENOMEM);
5605 	spin_lock_bh(&bp->ntp_fltr_lock);
5606 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5607 	spin_unlock_bh(&bp->ntp_fltr_lock);
5608 	if (rc) {
5609 		bnxt_del_l2_filter(bp, fltr);
5610 		fltr = ERR_PTR(rc);
5611 	}
5612 	return fltr;
5613 }
5614 
5615 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
5616 						struct bnxt_l2_key *key,
5617 						u16 flags)
5618 {
5619 	struct bnxt_l2_filter *fltr;
5620 	u32 idx;
5621 	int rc;
5622 
5623 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5624 	      BNXT_L2_FLTR_HASH_MASK;
5625 	spin_lock_bh(&bp->ntp_fltr_lock);
5626 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5627 	if (fltr) {
5628 		fltr = ERR_PTR(-EEXIST);
5629 		goto l2_filter_exit;
5630 	}
5631 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
5632 	if (!fltr) {
5633 		fltr = ERR_PTR(-ENOMEM);
5634 		goto l2_filter_exit;
5635 	}
5636 	fltr->base.flags = flags;
5637 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5638 	if (rc) {
5639 		spin_unlock_bh(&bp->ntp_fltr_lock);
5640 		bnxt_del_l2_filter(bp, fltr);
5641 		return ERR_PTR(rc);
5642 	}
5643 
5644 l2_filter_exit:
5645 	spin_unlock_bh(&bp->ntp_fltr_lock);
5646 	return fltr;
5647 }
5648 
5649 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
5650 {
5651 #ifdef CONFIG_BNXT_SRIOV
5652 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
5653 
5654 	return vf->fw_fid;
5655 #else
5656 	return INVALID_HW_RING_ID;
5657 #endif
5658 }
5659 
5660 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5661 {
5662 	struct hwrm_cfa_l2_filter_free_input *req;
5663 	u16 target_id = 0xffff;
5664 	int rc;
5665 
5666 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
5667 		struct bnxt_pf_info *pf = &bp->pf;
5668 
5669 		if (fltr->base.vf_idx >= pf->active_vfs)
5670 			return -EINVAL;
5671 
5672 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
5673 		if (target_id == INVALID_HW_RING_ID)
5674 			return -EINVAL;
5675 	}
5676 
5677 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
5678 	if (rc)
5679 		return rc;
5680 
5681 	req->target_id = cpu_to_le16(target_id);
5682 	req->l2_filter_id = fltr->base.filter_id;
5683 	return hwrm_req_send(bp, req);
5684 }
5685 
5686 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5687 {
5688 	struct hwrm_cfa_l2_filter_alloc_output *resp;
5689 	struct hwrm_cfa_l2_filter_alloc_input *req;
5690 	u16 target_id = 0xffff;
5691 	int rc;
5692 
5693 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
5694 		struct bnxt_pf_info *pf = &bp->pf;
5695 
5696 		if (fltr->base.vf_idx >= pf->active_vfs)
5697 			return -EINVAL;
5698 
5699 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
5700 	}
5701 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
5702 	if (rc)
5703 		return rc;
5704 
5705 	req->target_id = cpu_to_le16(target_id);
5706 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
5707 
5708 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
5709 		req->flags |=
5710 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
5711 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
5712 	req->enables =
5713 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
5714 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
5715 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
5716 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
5717 	eth_broadcast_addr(req->l2_addr_mask);
5718 
5719 	if (fltr->l2_key.vlan) {
5720 		req->enables |=
5721 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
5722 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
5723 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
5724 		req->num_vlans = 1;
5725 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
5726 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
5727 	}
5728 
5729 	resp = hwrm_req_hold(bp, req);
5730 	rc = hwrm_req_send(bp, req);
5731 	if (!rc) {
5732 		fltr->base.filter_id = resp->l2_filter_id;
5733 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
5734 	}
5735 	hwrm_req_drop(bp, req);
5736 	return rc;
5737 }
5738 
5739 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
5740 				     struct bnxt_ntuple_filter *fltr)
5741 {
5742 	struct hwrm_cfa_ntuple_filter_free_input *req;
5743 	int rc;
5744 
5745 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
5746 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
5747 	if (rc)
5748 		return rc;
5749 
5750 	req->ntuple_filter_id = fltr->base.filter_id;
5751 	return hwrm_req_send(bp, req);
5752 }
5753 
5754 #define BNXT_NTP_FLTR_FLAGS					\
5755 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
5756 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
5757 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
5758 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
5759 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
5760 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
5761 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
5762 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
5763 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
5764 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
5765 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
5766 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
5767 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
5768 
5769 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
5770 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
5771 
5772 void bnxt_fill_ipv6_mask(__be32 mask[4])
5773 {
5774 	int i;
5775 
5776 	for (i = 0; i < 4; i++)
5777 		mask[i] = cpu_to_be32(~0);
5778 }
5779 
5780 static void
5781 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
5782 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
5783 			  u16 rxq)
5784 {
5785 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
5786 		struct bnxt_vnic_info *vnic;
5787 		u32 enables;
5788 
5789 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
5790 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
5791 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
5792 		req->enables |= cpu_to_le32(enables);
5793 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
5794 	} else {
5795 		u32 flags;
5796 
5797 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
5798 		req->flags |= cpu_to_le32(flags);
5799 		req->dst_id = cpu_to_le16(rxq);
5800 	}
5801 }
5802 
5803 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
5804 				      struct bnxt_ntuple_filter *fltr)
5805 {
5806 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
5807 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
5808 	struct bnxt_flow_masks *masks = &fltr->fmasks;
5809 	struct flow_keys *keys = &fltr->fkeys;
5810 	struct bnxt_l2_filter *l2_fltr;
5811 	struct bnxt_vnic_info *vnic;
5812 	int rc;
5813 
5814 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
5815 	if (rc)
5816 		return rc;
5817 
5818 	l2_fltr = fltr->l2_fltr;
5819 	req->l2_filter_id = l2_fltr->base.filter_id;
5820 
5821 	if (fltr->base.flags & BNXT_ACT_DROP) {
5822 		req->flags =
5823 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
5824 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
5825 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr->base.rxq);
5826 	} else {
5827 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
5828 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
5829 	}
5830 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
5831 
5832 	req->ethertype = htons(ETH_P_IP);
5833 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
5834 	req->ip_protocol = keys->basic.ip_proto;
5835 
5836 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
5837 		req->ethertype = htons(ETH_P_IPV6);
5838 		req->ip_addr_type =
5839 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
5840 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
5841 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
5842 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
5843 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
5844 	} else {
5845 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
5846 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
5847 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
5848 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
5849 	}
5850 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
5851 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
5852 		req->tunnel_type =
5853 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
5854 	}
5855 
5856 	req->src_port = keys->ports.src;
5857 	req->src_port_mask = masks->ports.src;
5858 	req->dst_port = keys->ports.dst;
5859 	req->dst_port_mask = masks->ports.dst;
5860 
5861 	resp = hwrm_req_hold(bp, req);
5862 	rc = hwrm_req_send(bp, req);
5863 	if (!rc)
5864 		fltr->base.filter_id = resp->ntuple_filter_id;
5865 	hwrm_req_drop(bp, req);
5866 	return rc;
5867 }
5868 
5869 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
5870 				     const u8 *mac_addr)
5871 {
5872 	struct bnxt_l2_filter *fltr;
5873 	struct bnxt_l2_key key;
5874 	int rc;
5875 
5876 	ether_addr_copy(key.dst_mac_addr, mac_addr);
5877 	key.vlan = 0;
5878 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
5879 	if (IS_ERR(fltr))
5880 		return PTR_ERR(fltr);
5881 
5882 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
5883 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
5884 	if (rc)
5885 		bnxt_del_l2_filter(bp, fltr);
5886 	else
5887 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
5888 	return rc;
5889 }
5890 
5891 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
5892 {
5893 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
5894 
5895 	/* Any associated ntuple filters will also be cleared by firmware. */
5896 	for (i = 0; i < num_of_vnics; i++) {
5897 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
5898 
5899 		for (j = 0; j < vnic->uc_filter_count; j++) {
5900 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
5901 
5902 			bnxt_hwrm_l2_filter_free(bp, fltr);
5903 			bnxt_del_l2_filter(bp, fltr);
5904 		}
5905 		vnic->uc_filter_count = 0;
5906 	}
5907 }
5908 
5909 #define BNXT_DFLT_TUNL_TPA_BMAP				\
5910 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
5911 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
5912 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
5913 
5914 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
5915 					   struct hwrm_vnic_tpa_cfg_input *req)
5916 {
5917 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
5918 
5919 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
5920 		return;
5921 
5922 	if (bp->vxlan_port)
5923 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
5924 	if (bp->vxlan_gpe_port)
5925 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
5926 	if (bp->nge_port)
5927 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
5928 
5929 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
5930 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
5931 }
5932 
5933 static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
5934 {
5935 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5936 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
5937 	struct hwrm_vnic_tpa_cfg_input *req;
5938 	int rc;
5939 
5940 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
5941 		return 0;
5942 
5943 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
5944 	if (rc)
5945 		return rc;
5946 
5947 	if (tpa_flags) {
5948 		u16 mss = bp->dev->mtu - 40;
5949 		u32 nsegs, n, segs = 0, flags;
5950 
5951 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
5952 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
5953 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
5954 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
5955 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
5956 		if (tpa_flags & BNXT_FLAG_GRO)
5957 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
5958 
5959 		req->flags = cpu_to_le32(flags);
5960 
5961 		req->enables =
5962 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
5963 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
5964 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
5965 
5966 		/* Number of segs are log2 units, and first packet is not
5967 		 * included as part of this units.
5968 		 */
5969 		if (mss <= BNXT_RX_PAGE_SIZE) {
5970 			n = BNXT_RX_PAGE_SIZE / mss;
5971 			nsegs = (MAX_SKB_FRAGS - 1) * n;
5972 		} else {
5973 			n = mss / BNXT_RX_PAGE_SIZE;
5974 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
5975 				n++;
5976 			nsegs = (MAX_SKB_FRAGS - n) / n;
5977 		}
5978 
5979 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5980 			segs = MAX_TPA_SEGS_P5;
5981 			max_aggs = bp->max_tpa;
5982 		} else {
5983 			segs = ilog2(nsegs);
5984 		}
5985 		req->max_agg_segs = cpu_to_le16(segs);
5986 		req->max_aggs = cpu_to_le16(max_aggs);
5987 
5988 		req->min_agg_len = cpu_to_le32(512);
5989 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
5990 	}
5991 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5992 
5993 	return hwrm_req_send(bp, req);
5994 }
5995 
5996 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
5997 {
5998 	struct bnxt_ring_grp_info *grp_info;
5999 
6000 	grp_info = &bp->grp_info[ring->grp_idx];
6001 	return grp_info->cp_fw_ring_id;
6002 }
6003 
6004 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6005 {
6006 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6007 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6008 	else
6009 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6010 }
6011 
6012 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6013 {
6014 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6015 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6016 	else
6017 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6018 }
6019 
6020 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6021 {
6022 	int entries;
6023 
6024 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6025 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6026 	else
6027 		entries = HW_HASH_INDEX_SIZE;
6028 
6029 	bp->rss_indir_tbl_entries = entries;
6030 	bp->rss_indir_tbl = kmalloc_array(entries, sizeof(*bp->rss_indir_tbl),
6031 					  GFP_KERNEL);
6032 	if (!bp->rss_indir_tbl)
6033 		return -ENOMEM;
6034 	return 0;
6035 }
6036 
6037 static void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp)
6038 {
6039 	u16 max_rings, max_entries, pad, i;
6040 
6041 	if (!bp->rx_nr_rings)
6042 		return;
6043 
6044 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6045 		max_rings = bp->rx_nr_rings - 1;
6046 	else
6047 		max_rings = bp->rx_nr_rings;
6048 
6049 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6050 
6051 	for (i = 0; i < max_entries; i++)
6052 		bp->rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6053 
6054 	pad = bp->rss_indir_tbl_entries - max_entries;
6055 	if (pad)
6056 		memset(&bp->rss_indir_tbl[i], 0, pad * sizeof(u16));
6057 }
6058 
6059 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6060 {
6061 	u16 i, tbl_size, max_ring = 0;
6062 
6063 	if (!bp->rss_indir_tbl)
6064 		return 0;
6065 
6066 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6067 	for (i = 0; i < tbl_size; i++)
6068 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6069 	return max_ring;
6070 }
6071 
6072 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6073 {
6074 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6075 		if (!rx_rings)
6076 			return 0;
6077 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6078 					       BNXT_RSS_TABLE_ENTRIES_P5);
6079 	}
6080 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6081 		return 2;
6082 	return 1;
6083 }
6084 
6085 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6086 {
6087 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6088 	u16 i, j;
6089 
6090 	/* Fill the RSS indirection table with ring group ids */
6091 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6092 		if (!no_rss)
6093 			j = bp->rss_indir_tbl[i];
6094 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6095 	}
6096 }
6097 
6098 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6099 				    struct bnxt_vnic_info *vnic)
6100 {
6101 	__le16 *ring_tbl = vnic->rss_table;
6102 	struct bnxt_rx_ring_info *rxr;
6103 	u16 tbl_size, i;
6104 
6105 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6106 
6107 	for (i = 0; i < tbl_size; i++) {
6108 		u16 ring_id, j;
6109 
6110 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6111 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6112 		else
6113 			j = bp->rss_indir_tbl[i];
6114 		rxr = &bp->rx_ring[j];
6115 
6116 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6117 		*ring_tbl++ = cpu_to_le16(ring_id);
6118 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6119 		*ring_tbl++ = cpu_to_le16(ring_id);
6120 	}
6121 }
6122 
6123 static void
6124 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6125 			 struct bnxt_vnic_info *vnic)
6126 {
6127 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6128 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6129 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6130 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6131 	} else {
6132 		bnxt_fill_hw_rss_tbl(bp, vnic);
6133 	}
6134 
6135 	if (bp->rss_hash_delta) {
6136 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6137 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6138 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6139 		else
6140 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6141 	} else {
6142 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6143 	}
6144 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6145 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6146 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6147 }
6148 
6149 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
6150 {
6151 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
6152 	struct hwrm_vnic_rss_cfg_input *req;
6153 	int rc;
6154 
6155 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6156 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6157 		return 0;
6158 
6159 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6160 	if (rc)
6161 		return rc;
6162 
6163 	if (set_rss)
6164 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6165 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6166 	return hwrm_req_send(bp, req);
6167 }
6168 
6169 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp, u16 vnic_id, bool set_rss)
6170 {
6171 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
6172 	struct hwrm_vnic_rss_cfg_input *req;
6173 	dma_addr_t ring_tbl_map;
6174 	u32 i, nr_ctxs;
6175 	int rc;
6176 
6177 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6178 	if (rc)
6179 		return rc;
6180 
6181 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6182 	if (!set_rss)
6183 		return hwrm_req_send(bp, req);
6184 
6185 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6186 	ring_tbl_map = vnic->rss_table_dma_addr;
6187 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6188 
6189 	hwrm_req_hold(bp, req);
6190 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6191 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6192 		req->ring_table_pair_index = i;
6193 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6194 		rc = hwrm_req_send(bp, req);
6195 		if (rc)
6196 			goto exit;
6197 	}
6198 
6199 exit:
6200 	hwrm_req_drop(bp, req);
6201 	return rc;
6202 }
6203 
6204 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6205 {
6206 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6207 	struct hwrm_vnic_rss_qcfg_output *resp;
6208 	struct hwrm_vnic_rss_qcfg_input *req;
6209 
6210 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6211 		return;
6212 
6213 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6214 	/* all contexts configured to same hash_type, zero always exists */
6215 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6216 	resp = hwrm_req_hold(bp, req);
6217 	if (!hwrm_req_send(bp, req)) {
6218 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6219 		bp->rss_hash_delta = 0;
6220 	}
6221 	hwrm_req_drop(bp, req);
6222 }
6223 
6224 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
6225 {
6226 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
6227 	struct hwrm_vnic_plcmodes_cfg_input *req;
6228 	int rc;
6229 
6230 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6231 	if (rc)
6232 		return rc;
6233 
6234 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6235 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6236 
6237 	if (BNXT_RX_PAGE_MODE(bp)) {
6238 		req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6239 	} else {
6240 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6241 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6242 		req->enables |=
6243 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6244 		req->jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
6245 		req->hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
6246 	}
6247 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6248 	return hwrm_req_send(bp, req);
6249 }
6250 
6251 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
6252 					u16 ctx_idx)
6253 {
6254 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6255 
6256 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6257 		return;
6258 
6259 	req->rss_cos_lb_ctx_id =
6260 		cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
6261 
6262 	hwrm_req_send(bp, req);
6263 	bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6264 }
6265 
6266 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6267 {
6268 	int i, j;
6269 
6270 	for (i = 0; i < bp->nr_vnics; i++) {
6271 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6272 
6273 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6274 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6275 				bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
6276 		}
6277 	}
6278 	bp->rsscos_nr_ctxs = 0;
6279 }
6280 
6281 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
6282 {
6283 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6284 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6285 	int rc;
6286 
6287 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6288 	if (rc)
6289 		return rc;
6290 
6291 	resp = hwrm_req_hold(bp, req);
6292 	rc = hwrm_req_send(bp, req);
6293 	if (!rc)
6294 		bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
6295 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6296 	hwrm_req_drop(bp, req);
6297 
6298 	return rc;
6299 }
6300 
6301 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6302 {
6303 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6304 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6305 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6306 }
6307 
6308 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
6309 {
6310 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6311 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
6312 	struct hwrm_vnic_cfg_input *req;
6313 	unsigned int ring = 0, grp_idx;
6314 	u16 def_vlan = 0;
6315 	int rc;
6316 
6317 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6318 	if (rc)
6319 		return rc;
6320 
6321 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6322 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6323 
6324 		req->default_rx_ring_id =
6325 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6326 		req->default_cmpl_ring_id =
6327 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6328 		req->enables =
6329 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6330 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6331 		goto vnic_mru;
6332 	}
6333 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6334 	/* Only RSS support for now TBD: COS & LB */
6335 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6336 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6337 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6338 					   VNIC_CFG_REQ_ENABLES_MRU);
6339 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6340 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6341 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6342 					   VNIC_CFG_REQ_ENABLES_MRU);
6343 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6344 	} else {
6345 		req->rss_rule = cpu_to_le16(0xffff);
6346 	}
6347 
6348 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6349 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6350 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6351 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6352 	} else {
6353 		req->cos_rule = cpu_to_le16(0xffff);
6354 	}
6355 
6356 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6357 		ring = 0;
6358 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6359 		ring = vnic_id - 1;
6360 	else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6361 		ring = bp->rx_nr_rings - 1;
6362 
6363 	grp_idx = bp->rx_ring[ring].bnapi->index;
6364 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6365 	req->lb_rule = cpu_to_le16(0xffff);
6366 vnic_mru:
6367 	req->mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + VLAN_HLEN);
6368 
6369 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6370 #ifdef CONFIG_BNXT_SRIOV
6371 	if (BNXT_VF(bp))
6372 		def_vlan = bp->vf.vlan;
6373 #endif
6374 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6375 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6376 	if (!vnic_id && bnxt_ulp_registered(bp->edev))
6377 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6378 
6379 	return hwrm_req_send(bp, req);
6380 }
6381 
6382 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
6383 {
6384 	if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
6385 		struct hwrm_vnic_free_input *req;
6386 
6387 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6388 			return;
6389 
6390 		req->vnic_id =
6391 			cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
6392 
6393 		hwrm_req_send(bp, req);
6394 		bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
6395 	}
6396 }
6397 
6398 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6399 {
6400 	u16 i;
6401 
6402 	for (i = 0; i < bp->nr_vnics; i++)
6403 		bnxt_hwrm_vnic_free_one(bp, i);
6404 }
6405 
6406 static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
6407 				unsigned int start_rx_ring_idx,
6408 				unsigned int nr_rings)
6409 {
6410 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6411 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
6412 	struct hwrm_vnic_alloc_output *resp;
6413 	struct hwrm_vnic_alloc_input *req;
6414 	int rc;
6415 
6416 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6417 	if (rc)
6418 		return rc;
6419 
6420 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6421 		goto vnic_no_ring_grps;
6422 
6423 	/* map ring groups to this vnic */
6424 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6425 		grp_idx = bp->rx_ring[i].bnapi->index;
6426 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6427 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6428 				   j, nr_rings);
6429 			break;
6430 		}
6431 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6432 	}
6433 
6434 vnic_no_ring_grps:
6435 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6436 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6437 	if (vnic_id == BNXT_VNIC_DEFAULT)
6438 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6439 
6440 	resp = hwrm_req_hold(bp, req);
6441 	rc = hwrm_req_send(bp, req);
6442 	if (!rc)
6443 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6444 	hwrm_req_drop(bp, req);
6445 	return rc;
6446 }
6447 
6448 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6449 {
6450 	struct hwrm_vnic_qcaps_output *resp;
6451 	struct hwrm_vnic_qcaps_input *req;
6452 	int rc;
6453 
6454 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6455 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6456 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6457 	if (bp->hwrm_spec_code < 0x10600)
6458 		return 0;
6459 
6460 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6461 	if (rc)
6462 		return rc;
6463 
6464 	resp = hwrm_req_hold(bp, req);
6465 	rc = hwrm_req_send(bp, req);
6466 	if (!rc) {
6467 		u32 flags = le32_to_cpu(resp->flags);
6468 
6469 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6470 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6471 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6472 		if (flags &
6473 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6474 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6475 
6476 		/* Older P5 fw before EXT_HW_STATS support did not set
6477 		 * VLAN_STRIP_CAP properly.
6478 		 */
6479 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6480 		    (BNXT_CHIP_P5(bp) &&
6481 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6482 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6483 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6484 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6485 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6486 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6487 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6488 		if (bp->max_tpa_v2) {
6489 			if (BNXT_CHIP_P5(bp))
6490 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6491 			else
6492 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6493 		}
6494 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6495 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6496 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6497 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6498 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6499 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6500 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6501 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6502 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6503 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6504 	}
6505 	hwrm_req_drop(bp, req);
6506 	return rc;
6507 }
6508 
6509 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6510 {
6511 	struct hwrm_ring_grp_alloc_output *resp;
6512 	struct hwrm_ring_grp_alloc_input *req;
6513 	int rc;
6514 	u16 i;
6515 
6516 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6517 		return 0;
6518 
6519 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6520 	if (rc)
6521 		return rc;
6522 
6523 	resp = hwrm_req_hold(bp, req);
6524 	for (i = 0; i < bp->rx_nr_rings; i++) {
6525 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
6526 
6527 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
6528 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
6529 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
6530 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
6531 
6532 		rc = hwrm_req_send(bp, req);
6533 
6534 		if (rc)
6535 			break;
6536 
6537 		bp->grp_info[grp_idx].fw_grp_id =
6538 			le32_to_cpu(resp->ring_group_id);
6539 	}
6540 	hwrm_req_drop(bp, req);
6541 	return rc;
6542 }
6543 
6544 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
6545 {
6546 	struct hwrm_ring_grp_free_input *req;
6547 	u16 i;
6548 
6549 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
6550 		return;
6551 
6552 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
6553 		return;
6554 
6555 	hwrm_req_hold(bp, req);
6556 	for (i = 0; i < bp->cp_nr_rings; i++) {
6557 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
6558 			continue;
6559 		req->ring_group_id =
6560 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
6561 
6562 		hwrm_req_send(bp, req);
6563 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
6564 	}
6565 	hwrm_req_drop(bp, req);
6566 }
6567 
6568 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
6569 				    struct bnxt_ring_struct *ring,
6570 				    u32 ring_type, u32 map_index)
6571 {
6572 	struct hwrm_ring_alloc_output *resp;
6573 	struct hwrm_ring_alloc_input *req;
6574 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
6575 	struct bnxt_ring_grp_info *grp_info;
6576 	int rc, err = 0;
6577 	u16 ring_id;
6578 
6579 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
6580 	if (rc)
6581 		goto exit;
6582 
6583 	req->enables = 0;
6584 	if (rmem->nr_pages > 1) {
6585 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
6586 		/* Page size is in log2 units */
6587 		req->page_size = BNXT_PAGE_SHIFT;
6588 		req->page_tbl_depth = 1;
6589 	} else {
6590 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
6591 	}
6592 	req->fbo = 0;
6593 	/* Association of ring index with doorbell index and MSIX number */
6594 	req->logical_id = cpu_to_le16(map_index);
6595 
6596 	switch (ring_type) {
6597 	case HWRM_RING_ALLOC_TX: {
6598 		struct bnxt_tx_ring_info *txr;
6599 
6600 		txr = container_of(ring, struct bnxt_tx_ring_info,
6601 				   tx_ring_struct);
6602 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
6603 		/* Association of transmit ring with completion ring */
6604 		grp_info = &bp->grp_info[ring->grp_idx];
6605 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
6606 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
6607 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6608 		req->queue_id = cpu_to_le16(ring->queue_id);
6609 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
6610 			req->cmpl_coal_cnt =
6611 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
6612 		break;
6613 	}
6614 	case HWRM_RING_ALLOC_RX:
6615 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6616 		req->length = cpu_to_le32(bp->rx_ring_mask + 1);
6617 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6618 			u16 flags = 0;
6619 
6620 			/* Association of rx ring with stats context */
6621 			grp_info = &bp->grp_info[ring->grp_idx];
6622 			req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
6623 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6624 			req->enables |= cpu_to_le32(
6625 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6626 			if (NET_IP_ALIGN == 2)
6627 				flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
6628 			req->flags = cpu_to_le16(flags);
6629 		}
6630 		break;
6631 	case HWRM_RING_ALLOC_AGG:
6632 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6633 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
6634 			/* Association of agg ring with rx ring */
6635 			grp_info = &bp->grp_info[ring->grp_idx];
6636 			req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
6637 			req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
6638 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6639 			req->enables |= cpu_to_le32(
6640 				RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
6641 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6642 		} else {
6643 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6644 		}
6645 		req->length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
6646 		break;
6647 	case HWRM_RING_ALLOC_CMPL:
6648 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
6649 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
6650 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6651 			/* Association of cp ring with nq */
6652 			grp_info = &bp->grp_info[map_index];
6653 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
6654 			req->cq_handle = cpu_to_le64(ring->handle);
6655 			req->enables |= cpu_to_le32(
6656 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
6657 		} else if (bp->flags & BNXT_FLAG_USING_MSIX) {
6658 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
6659 		}
6660 		break;
6661 	case HWRM_RING_ALLOC_NQ:
6662 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
6663 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
6664 		if (bp->flags & BNXT_FLAG_USING_MSIX)
6665 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
6666 		break;
6667 	default:
6668 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
6669 			   ring_type);
6670 		return -1;
6671 	}
6672 
6673 	resp = hwrm_req_hold(bp, req);
6674 	rc = hwrm_req_send(bp, req);
6675 	err = le16_to_cpu(resp->error_code);
6676 	ring_id = le16_to_cpu(resp->ring_id);
6677 	hwrm_req_drop(bp, req);
6678 
6679 exit:
6680 	if (rc || err) {
6681 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
6682 			   ring_type, rc, err);
6683 		return -EIO;
6684 	}
6685 	ring->fw_ring_id = ring_id;
6686 	return rc;
6687 }
6688 
6689 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
6690 {
6691 	int rc;
6692 
6693 	if (BNXT_PF(bp)) {
6694 		struct hwrm_func_cfg_input *req;
6695 
6696 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
6697 		if (rc)
6698 			return rc;
6699 
6700 		req->fid = cpu_to_le16(0xffff);
6701 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
6702 		req->async_event_cr = cpu_to_le16(idx);
6703 		return hwrm_req_send(bp, req);
6704 	} else {
6705 		struct hwrm_func_vf_cfg_input *req;
6706 
6707 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
6708 		if (rc)
6709 			return rc;
6710 
6711 		req->enables =
6712 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
6713 		req->async_event_cr = cpu_to_le16(idx);
6714 		return hwrm_req_send(bp, req);
6715 	}
6716 }
6717 
6718 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
6719 			     u32 ring_type)
6720 {
6721 	switch (ring_type) {
6722 	case HWRM_RING_ALLOC_TX:
6723 		db->db_ring_mask = bp->tx_ring_mask;
6724 		break;
6725 	case HWRM_RING_ALLOC_RX:
6726 		db->db_ring_mask = bp->rx_ring_mask;
6727 		break;
6728 	case HWRM_RING_ALLOC_AGG:
6729 		db->db_ring_mask = bp->rx_agg_ring_mask;
6730 		break;
6731 	case HWRM_RING_ALLOC_CMPL:
6732 	case HWRM_RING_ALLOC_NQ:
6733 		db->db_ring_mask = bp->cp_ring_mask;
6734 		break;
6735 	}
6736 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
6737 		db->db_epoch_mask = db->db_ring_mask + 1;
6738 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
6739 	}
6740 }
6741 
6742 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
6743 			u32 map_idx, u32 xid)
6744 {
6745 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6746 		switch (ring_type) {
6747 		case HWRM_RING_ALLOC_TX:
6748 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
6749 			break;
6750 		case HWRM_RING_ALLOC_RX:
6751 		case HWRM_RING_ALLOC_AGG:
6752 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
6753 			break;
6754 		case HWRM_RING_ALLOC_CMPL:
6755 			db->db_key64 = DBR_PATH_L2;
6756 			break;
6757 		case HWRM_RING_ALLOC_NQ:
6758 			db->db_key64 = DBR_PATH_L2;
6759 			break;
6760 		}
6761 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
6762 
6763 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6764 			db->db_key64 |= DBR_VALID;
6765 
6766 		db->doorbell = bp->bar1 + bp->db_offset;
6767 	} else {
6768 		db->doorbell = bp->bar1 + map_idx * 0x80;
6769 		switch (ring_type) {
6770 		case HWRM_RING_ALLOC_TX:
6771 			db->db_key32 = DB_KEY_TX;
6772 			break;
6773 		case HWRM_RING_ALLOC_RX:
6774 		case HWRM_RING_ALLOC_AGG:
6775 			db->db_key32 = DB_KEY_RX;
6776 			break;
6777 		case HWRM_RING_ALLOC_CMPL:
6778 			db->db_key32 = DB_KEY_CP;
6779 			break;
6780 		}
6781 	}
6782 	bnxt_set_db_mask(bp, db, ring_type);
6783 }
6784 
6785 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
6786 {
6787 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
6788 	int i, rc = 0;
6789 	u32 type;
6790 
6791 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6792 		type = HWRM_RING_ALLOC_NQ;
6793 	else
6794 		type = HWRM_RING_ALLOC_CMPL;
6795 	for (i = 0; i < bp->cp_nr_rings; i++) {
6796 		struct bnxt_napi *bnapi = bp->bnapi[i];
6797 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6798 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
6799 		u32 map_idx = ring->map_idx;
6800 		unsigned int vector;
6801 
6802 		vector = bp->irq_tbl[map_idx].vector;
6803 		disable_irq_nosync(vector);
6804 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6805 		if (rc) {
6806 			enable_irq(vector);
6807 			goto err_out;
6808 		}
6809 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
6810 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
6811 		enable_irq(vector);
6812 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
6813 
6814 		if (!i) {
6815 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
6816 			if (rc)
6817 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
6818 		}
6819 	}
6820 
6821 	type = HWRM_RING_ALLOC_TX;
6822 	for (i = 0; i < bp->tx_nr_rings; i++) {
6823 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
6824 		struct bnxt_ring_struct *ring;
6825 		u32 map_idx;
6826 
6827 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6828 			struct bnxt_cp_ring_info *cpr2 = txr->tx_cpr;
6829 			struct bnxt_napi *bnapi = txr->bnapi;
6830 			u32 type2 = HWRM_RING_ALLOC_CMPL;
6831 
6832 			ring = &cpr2->cp_ring_struct;
6833 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
6834 			map_idx = bnapi->index;
6835 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
6836 			if (rc)
6837 				goto err_out;
6838 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
6839 				    ring->fw_ring_id);
6840 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
6841 		}
6842 		ring = &txr->tx_ring_struct;
6843 		map_idx = i;
6844 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6845 		if (rc)
6846 			goto err_out;
6847 		bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
6848 	}
6849 
6850 	type = HWRM_RING_ALLOC_RX;
6851 	for (i = 0; i < bp->rx_nr_rings; i++) {
6852 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
6853 		struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
6854 		struct bnxt_napi *bnapi = rxr->bnapi;
6855 		u32 map_idx = bnapi->index;
6856 
6857 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6858 		if (rc)
6859 			goto err_out;
6860 		bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
6861 		/* If we have agg rings, post agg buffers first. */
6862 		if (!agg_rings)
6863 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
6864 		bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
6865 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6866 			struct bnxt_cp_ring_info *cpr2 = rxr->rx_cpr;
6867 			u32 type2 = HWRM_RING_ALLOC_CMPL;
6868 
6869 			ring = &cpr2->cp_ring_struct;
6870 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
6871 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
6872 			if (rc)
6873 				goto err_out;
6874 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
6875 				    ring->fw_ring_id);
6876 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
6877 		}
6878 	}
6879 
6880 	if (agg_rings) {
6881 		type = HWRM_RING_ALLOC_AGG;
6882 		for (i = 0; i < bp->rx_nr_rings; i++) {
6883 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
6884 			struct bnxt_ring_struct *ring =
6885 						&rxr->rx_agg_ring_struct;
6886 			u32 grp_idx = ring->grp_idx;
6887 			u32 map_idx = grp_idx + bp->rx_nr_rings;
6888 
6889 			rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6890 			if (rc)
6891 				goto err_out;
6892 
6893 			bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
6894 				    ring->fw_ring_id);
6895 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
6896 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
6897 			bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
6898 		}
6899 	}
6900 err_out:
6901 	return rc;
6902 }
6903 
6904 static int hwrm_ring_free_send_msg(struct bnxt *bp,
6905 				   struct bnxt_ring_struct *ring,
6906 				   u32 ring_type, int cmpl_ring_id)
6907 {
6908 	struct hwrm_ring_free_output *resp;
6909 	struct hwrm_ring_free_input *req;
6910 	u16 error_code = 0;
6911 	int rc;
6912 
6913 	if (BNXT_NO_FW_ACCESS(bp))
6914 		return 0;
6915 
6916 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
6917 	if (rc)
6918 		goto exit;
6919 
6920 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
6921 	req->ring_type = ring_type;
6922 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
6923 
6924 	resp = hwrm_req_hold(bp, req);
6925 	rc = hwrm_req_send(bp, req);
6926 	error_code = le16_to_cpu(resp->error_code);
6927 	hwrm_req_drop(bp, req);
6928 exit:
6929 	if (rc || error_code) {
6930 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
6931 			   ring_type, rc, error_code);
6932 		return -EIO;
6933 	}
6934 	return 0;
6935 }
6936 
6937 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
6938 {
6939 	u32 type;
6940 	int i;
6941 
6942 	if (!bp->bnapi)
6943 		return;
6944 
6945 	for (i = 0; i < bp->tx_nr_rings; i++) {
6946 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
6947 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
6948 
6949 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
6950 			u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
6951 
6952 			hwrm_ring_free_send_msg(bp, ring,
6953 						RING_FREE_REQ_RING_TYPE_TX,
6954 						close_path ? cmpl_ring_id :
6955 						INVALID_HW_RING_ID);
6956 			ring->fw_ring_id = INVALID_HW_RING_ID;
6957 		}
6958 	}
6959 
6960 	for (i = 0; i < bp->rx_nr_rings; i++) {
6961 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
6962 		struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
6963 		u32 grp_idx = rxr->bnapi->index;
6964 
6965 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
6966 			u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6967 
6968 			hwrm_ring_free_send_msg(bp, ring,
6969 						RING_FREE_REQ_RING_TYPE_RX,
6970 						close_path ? cmpl_ring_id :
6971 						INVALID_HW_RING_ID);
6972 			ring->fw_ring_id = INVALID_HW_RING_ID;
6973 			bp->grp_info[grp_idx].rx_fw_ring_id =
6974 				INVALID_HW_RING_ID;
6975 		}
6976 	}
6977 
6978 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6979 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
6980 	else
6981 		type = RING_FREE_REQ_RING_TYPE_RX;
6982 	for (i = 0; i < bp->rx_nr_rings; i++) {
6983 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
6984 		struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
6985 		u32 grp_idx = rxr->bnapi->index;
6986 
6987 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
6988 			u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6989 
6990 			hwrm_ring_free_send_msg(bp, ring, type,
6991 						close_path ? cmpl_ring_id :
6992 						INVALID_HW_RING_ID);
6993 			ring->fw_ring_id = INVALID_HW_RING_ID;
6994 			bp->grp_info[grp_idx].agg_fw_ring_id =
6995 				INVALID_HW_RING_ID;
6996 		}
6997 	}
6998 
6999 	/* The completion rings are about to be freed.  After that the
7000 	 * IRQ doorbell will not work anymore.  So we need to disable
7001 	 * IRQ here.
7002 	 */
7003 	bnxt_disable_int_sync(bp);
7004 
7005 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7006 		type = RING_FREE_REQ_RING_TYPE_NQ;
7007 	else
7008 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7009 	for (i = 0; i < bp->cp_nr_rings; i++) {
7010 		struct bnxt_napi *bnapi = bp->bnapi[i];
7011 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7012 		struct bnxt_ring_struct *ring;
7013 		int j;
7014 
7015 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++) {
7016 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
7017 
7018 			ring = &cpr2->cp_ring_struct;
7019 			if (ring->fw_ring_id == INVALID_HW_RING_ID)
7020 				continue;
7021 			hwrm_ring_free_send_msg(bp, ring,
7022 						RING_FREE_REQ_RING_TYPE_L2_CMPL,
7023 						INVALID_HW_RING_ID);
7024 			ring->fw_ring_id = INVALID_HW_RING_ID;
7025 		}
7026 		ring = &cpr->cp_ring_struct;
7027 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7028 			hwrm_ring_free_send_msg(bp, ring, type,
7029 						INVALID_HW_RING_ID);
7030 			ring->fw_ring_id = INVALID_HW_RING_ID;
7031 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7032 		}
7033 	}
7034 }
7035 
7036 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7037 			     bool shared);
7038 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7039 			   bool shared);
7040 
7041 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7042 {
7043 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7044 	struct hwrm_func_qcfg_output *resp;
7045 	struct hwrm_func_qcfg_input *req;
7046 	int rc;
7047 
7048 	if (bp->hwrm_spec_code < 0x10601)
7049 		return 0;
7050 
7051 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7052 	if (rc)
7053 		return rc;
7054 
7055 	req->fid = cpu_to_le16(0xffff);
7056 	resp = hwrm_req_hold(bp, req);
7057 	rc = hwrm_req_send(bp, req);
7058 	if (rc) {
7059 		hwrm_req_drop(bp, req);
7060 		return rc;
7061 	}
7062 
7063 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7064 	if (BNXT_NEW_RM(bp)) {
7065 		u16 cp, stats;
7066 
7067 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7068 		hw_resc->resv_hw_ring_grps =
7069 			le32_to_cpu(resp->alloc_hw_ring_grps);
7070 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7071 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7072 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7073 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7074 		hw_resc->resv_irqs = cp;
7075 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7076 			int rx = hw_resc->resv_rx_rings;
7077 			int tx = hw_resc->resv_tx_rings;
7078 
7079 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7080 				rx >>= 1;
7081 			if (cp < (rx + tx)) {
7082 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7083 				if (rc)
7084 					goto get_rings_exit;
7085 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7086 					rx <<= 1;
7087 				hw_resc->resv_rx_rings = rx;
7088 				hw_resc->resv_tx_rings = tx;
7089 			}
7090 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7091 			hw_resc->resv_hw_ring_grps = rx;
7092 		}
7093 		hw_resc->resv_cp_rings = cp;
7094 		hw_resc->resv_stat_ctxs = stats;
7095 	}
7096 get_rings_exit:
7097 	hwrm_req_drop(bp, req);
7098 	return rc;
7099 }
7100 
7101 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7102 {
7103 	struct hwrm_func_qcfg_output *resp;
7104 	struct hwrm_func_qcfg_input *req;
7105 	int rc;
7106 
7107 	if (bp->hwrm_spec_code < 0x10601)
7108 		return 0;
7109 
7110 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7111 	if (rc)
7112 		return rc;
7113 
7114 	req->fid = cpu_to_le16(fid);
7115 	resp = hwrm_req_hold(bp, req);
7116 	rc = hwrm_req_send(bp, req);
7117 	if (!rc)
7118 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7119 
7120 	hwrm_req_drop(bp, req);
7121 	return rc;
7122 }
7123 
7124 static bool bnxt_rfs_supported(struct bnxt *bp);
7125 
7126 static struct hwrm_func_cfg_input *
7127 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7128 {
7129 	struct hwrm_func_cfg_input *req;
7130 	u32 enables = 0;
7131 
7132 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7133 		return NULL;
7134 
7135 	req->fid = cpu_to_le16(0xffff);
7136 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7137 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7138 	if (BNXT_NEW_RM(bp)) {
7139 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7140 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7141 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7142 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7143 			enables |= hwr->cp_p5 ?
7144 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7145 		} else {
7146 			enables |= hwr->cp ?
7147 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7148 			enables |= hwr->grp ?
7149 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7150 		}
7151 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7152 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7153 					  0;
7154 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7155 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7156 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7157 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7158 			req->num_msix = cpu_to_le16(hwr->cp);
7159 		} else {
7160 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7161 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7162 		}
7163 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7164 		req->num_vnics = cpu_to_le16(hwr->vnic);
7165 	}
7166 	req->enables = cpu_to_le32(enables);
7167 	return req;
7168 }
7169 
7170 static struct hwrm_func_vf_cfg_input *
7171 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7172 {
7173 	struct hwrm_func_vf_cfg_input *req;
7174 	u32 enables = 0;
7175 
7176 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7177 		return NULL;
7178 
7179 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7180 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7181 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7182 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7183 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7184 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7185 		enables |= hwr->cp_p5 ?
7186 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7187 	} else {
7188 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7189 		enables |= hwr->grp ?
7190 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7191 	}
7192 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7193 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7194 
7195 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7196 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7197 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7198 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7199 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7200 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7201 	} else {
7202 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7203 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7204 	}
7205 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7206 	req->num_vnics = cpu_to_le16(hwr->vnic);
7207 
7208 	req->enables = cpu_to_le32(enables);
7209 	return req;
7210 }
7211 
7212 static int
7213 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7214 {
7215 	struct hwrm_func_cfg_input *req;
7216 	int rc;
7217 
7218 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7219 	if (!req)
7220 		return -ENOMEM;
7221 
7222 	if (!req->enables) {
7223 		hwrm_req_drop(bp, req);
7224 		return 0;
7225 	}
7226 
7227 	rc = hwrm_req_send(bp, req);
7228 	if (rc)
7229 		return rc;
7230 
7231 	if (bp->hwrm_spec_code < 0x10601)
7232 		bp->hw_resc.resv_tx_rings = hwr->tx;
7233 
7234 	return bnxt_hwrm_get_rings(bp);
7235 }
7236 
7237 static int
7238 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7239 {
7240 	struct hwrm_func_vf_cfg_input *req;
7241 	int rc;
7242 
7243 	if (!BNXT_NEW_RM(bp)) {
7244 		bp->hw_resc.resv_tx_rings = hwr->tx;
7245 		return 0;
7246 	}
7247 
7248 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7249 	if (!req)
7250 		return -ENOMEM;
7251 
7252 	rc = hwrm_req_send(bp, req);
7253 	if (rc)
7254 		return rc;
7255 
7256 	return bnxt_hwrm_get_rings(bp);
7257 }
7258 
7259 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7260 {
7261 	if (BNXT_PF(bp))
7262 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7263 	else
7264 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7265 }
7266 
7267 int bnxt_nq_rings_in_use(struct bnxt *bp)
7268 {
7269 	int cp = bp->cp_nr_rings;
7270 	int ulp_msix, ulp_base;
7271 
7272 	ulp_msix = bnxt_get_ulp_msix_num(bp);
7273 	if (ulp_msix) {
7274 		ulp_base = bnxt_get_ulp_msix_base(bp);
7275 		cp += ulp_msix;
7276 		if ((ulp_base + ulp_msix) > cp)
7277 			cp = ulp_base + ulp_msix;
7278 	}
7279 	return cp;
7280 }
7281 
7282 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7283 {
7284 	int cp;
7285 
7286 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7287 		return bnxt_nq_rings_in_use(bp);
7288 
7289 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7290 	return cp;
7291 }
7292 
7293 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7294 {
7295 	int ulp_stat = bnxt_get_ulp_stat_ctxs(bp);
7296 	int cp = bp->cp_nr_rings;
7297 
7298 	if (!ulp_stat)
7299 		return cp;
7300 
7301 	if (bnxt_nq_rings_in_use(bp) > cp + bnxt_get_ulp_msix_num(bp))
7302 		return bnxt_get_ulp_msix_base(bp) + ulp_stat;
7303 
7304 	return cp + ulp_stat;
7305 }
7306 
7307 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7308 {
7309 	if (!hwr->grp)
7310 		return 0;
7311 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7312 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7313 
7314 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7315 			rss_ctx *= hwr->vnic;
7316 		return rss_ctx;
7317 	}
7318 	if (BNXT_VF(bp))
7319 		return BNXT_VF_MAX_RSS_CTX;
7320 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7321 		return hwr->grp + 1;
7322 	return 1;
7323 }
7324 
7325 /* Check if a default RSS map needs to be setup.  This function is only
7326  * used on older firmware that does not require reserving RX rings.
7327  */
7328 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7329 {
7330 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7331 
7332 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7333 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7334 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7335 		if (!netif_is_rxfh_configured(bp->dev))
7336 			bnxt_set_dflt_rss_indir_tbl(bp);
7337 	}
7338 }
7339 
7340 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7341 {
7342 	if (bp->flags & BNXT_FLAG_RFS) {
7343 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7344 			return 2;
7345 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7346 			return rx_rings + 1;
7347 	}
7348 	return 1;
7349 }
7350 
7351 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7352 {
7353 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7354 	int cp = bnxt_cp_rings_in_use(bp);
7355 	int nq = bnxt_nq_rings_in_use(bp);
7356 	int rx = bp->rx_nr_rings, stat;
7357 	int vnic, grp = rx;
7358 
7359 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7360 	    bp->hwrm_spec_code >= 0x10601)
7361 		return true;
7362 
7363 	/* Old firmware does not need RX ring reservations but we still
7364 	 * need to setup a default RSS map when needed.  With new firmware
7365 	 * we go through RX ring reservations first and then set up the
7366 	 * RSS map for the successfully reserved RX rings when needed.
7367 	 */
7368 	if (!BNXT_NEW_RM(bp)) {
7369 		bnxt_check_rss_tbl_no_rmgr(bp);
7370 		return false;
7371 	}
7372 
7373 	vnic = bnxt_get_total_vnics(bp, rx);
7374 
7375 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7376 		rx <<= 1;
7377 	stat = bnxt_get_func_stat_ctxs(bp);
7378 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7379 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7380 	    (hw_resc->resv_hw_ring_grps != grp &&
7381 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7382 		return true;
7383 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7384 	    hw_resc->resv_irqs != nq)
7385 		return true;
7386 	return false;
7387 }
7388 
7389 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7390 {
7391 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7392 
7393 	hwr->tx = hw_resc->resv_tx_rings;
7394 	if (BNXT_NEW_RM(bp)) {
7395 		hwr->rx = hw_resc->resv_rx_rings;
7396 		hwr->cp = hw_resc->resv_irqs;
7397 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7398 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7399 		hwr->grp = hw_resc->resv_hw_ring_grps;
7400 		hwr->vnic = hw_resc->resv_vnics;
7401 		hwr->stat = hw_resc->resv_stat_ctxs;
7402 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7403 	}
7404 }
7405 
7406 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7407 {
7408 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7409 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7410 }
7411 
7412 static int __bnxt_reserve_rings(struct bnxt *bp)
7413 {
7414 	struct bnxt_hw_rings hwr = {0};
7415 	int rx_rings, rc;
7416 	bool sh = false;
7417 	int tx_cp;
7418 
7419 	if (!bnxt_need_reserve_rings(bp))
7420 		return 0;
7421 
7422 	hwr.cp = bnxt_nq_rings_in_use(bp);
7423 	hwr.tx = bp->tx_nr_rings;
7424 	hwr.rx = bp->rx_nr_rings;
7425 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7426 		sh = true;
7427 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7428 		hwr.cp_p5 = hwr.rx + hwr.tx;
7429 
7430 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7431 
7432 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7433 		hwr.rx <<= 1;
7434 	hwr.grp = bp->rx_nr_rings;
7435 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7436 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
7437 
7438 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
7439 	if (rc)
7440 		return rc;
7441 
7442 	bnxt_copy_reserved_rings(bp, &hwr);
7443 
7444 	rx_rings = hwr.rx;
7445 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7446 		if (hwr.rx >= 2) {
7447 			rx_rings = hwr.rx >> 1;
7448 		} else {
7449 			if (netif_running(bp->dev))
7450 				return -ENOMEM;
7451 
7452 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7453 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7454 			bp->dev->hw_features &= ~NETIF_F_LRO;
7455 			bp->dev->features &= ~NETIF_F_LRO;
7456 			bnxt_set_ring_params(bp);
7457 		}
7458 	}
7459 	rx_rings = min_t(int, rx_rings, hwr.grp);
7460 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
7461 	if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
7462 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
7463 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
7464 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
7465 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7466 		hwr.rx = rx_rings << 1;
7467 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
7468 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
7469 	bp->tx_nr_rings = hwr.tx;
7470 
7471 	/* If we cannot reserve all the RX rings, reset the RSS map only
7472 	 * if absolutely necessary
7473 	 */
7474 	if (rx_rings != bp->rx_nr_rings) {
7475 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
7476 			    rx_rings, bp->rx_nr_rings);
7477 		if (netif_is_rxfh_configured(bp->dev) &&
7478 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
7479 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
7480 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
7481 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
7482 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
7483 		}
7484 	}
7485 	bp->rx_nr_rings = rx_rings;
7486 	bp->cp_nr_rings = hwr.cp;
7487 
7488 	if (!bnxt_rings_ok(bp, &hwr))
7489 		return -ENOMEM;
7490 
7491 	if (!netif_is_rxfh_configured(bp->dev))
7492 		bnxt_set_dflt_rss_indir_tbl(bp);
7493 
7494 	return rc;
7495 }
7496 
7497 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7498 {
7499 	struct hwrm_func_vf_cfg_input *req;
7500 	u32 flags;
7501 
7502 	if (!BNXT_NEW_RM(bp))
7503 		return 0;
7504 
7505 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7506 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
7507 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7508 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7509 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7510 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
7511 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
7512 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7513 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7514 
7515 	req->flags = cpu_to_le32(flags);
7516 	return hwrm_req_send_silent(bp, req);
7517 }
7518 
7519 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7520 {
7521 	struct hwrm_func_cfg_input *req;
7522 	u32 flags;
7523 
7524 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7525 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
7526 	if (BNXT_NEW_RM(bp)) {
7527 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7528 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7529 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7530 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
7531 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7532 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
7533 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
7534 		else
7535 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7536 	}
7537 
7538 	req->flags = cpu_to_le32(flags);
7539 	return hwrm_req_send_silent(bp, req);
7540 }
7541 
7542 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7543 {
7544 	if (bp->hwrm_spec_code < 0x10801)
7545 		return 0;
7546 
7547 	if (BNXT_PF(bp))
7548 		return bnxt_hwrm_check_pf_rings(bp, hwr);
7549 
7550 	return bnxt_hwrm_check_vf_rings(bp, hwr);
7551 }
7552 
7553 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
7554 {
7555 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7556 	struct hwrm_ring_aggint_qcaps_output *resp;
7557 	struct hwrm_ring_aggint_qcaps_input *req;
7558 	int rc;
7559 
7560 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
7561 	coal_cap->num_cmpl_dma_aggr_max = 63;
7562 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
7563 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
7564 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
7565 	coal_cap->int_lat_tmr_min_max = 65535;
7566 	coal_cap->int_lat_tmr_max_max = 65535;
7567 	coal_cap->num_cmpl_aggr_int_max = 65535;
7568 	coal_cap->timer_units = 80;
7569 
7570 	if (bp->hwrm_spec_code < 0x10902)
7571 		return;
7572 
7573 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
7574 		return;
7575 
7576 	resp = hwrm_req_hold(bp, req);
7577 	rc = hwrm_req_send_silent(bp, req);
7578 	if (!rc) {
7579 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
7580 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
7581 		coal_cap->num_cmpl_dma_aggr_max =
7582 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
7583 		coal_cap->num_cmpl_dma_aggr_during_int_max =
7584 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
7585 		coal_cap->cmpl_aggr_dma_tmr_max =
7586 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
7587 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
7588 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
7589 		coal_cap->int_lat_tmr_min_max =
7590 			le16_to_cpu(resp->int_lat_tmr_min_max);
7591 		coal_cap->int_lat_tmr_max_max =
7592 			le16_to_cpu(resp->int_lat_tmr_max_max);
7593 		coal_cap->num_cmpl_aggr_int_max =
7594 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
7595 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
7596 	}
7597 	hwrm_req_drop(bp, req);
7598 }
7599 
7600 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
7601 {
7602 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7603 
7604 	return usec * 1000 / coal_cap->timer_units;
7605 }
7606 
7607 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
7608 	struct bnxt_coal *hw_coal,
7609 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7610 {
7611 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7612 	u16 val, tmr, max, flags = hw_coal->flags;
7613 	u32 cmpl_params = coal_cap->cmpl_params;
7614 
7615 	max = hw_coal->bufs_per_record * 128;
7616 	if (hw_coal->budget)
7617 		max = hw_coal->bufs_per_record * hw_coal->budget;
7618 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
7619 
7620 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
7621 	req->num_cmpl_aggr_int = cpu_to_le16(val);
7622 
7623 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
7624 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
7625 
7626 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
7627 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
7628 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
7629 
7630 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
7631 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
7632 	req->int_lat_tmr_max = cpu_to_le16(tmr);
7633 
7634 	/* min timer set to 1/2 of interrupt timer */
7635 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
7636 		val = tmr / 2;
7637 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
7638 		req->int_lat_tmr_min = cpu_to_le16(val);
7639 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
7640 	}
7641 
7642 	/* buf timer set to 1/4 of interrupt timer */
7643 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
7644 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
7645 
7646 	if (cmpl_params &
7647 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
7648 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
7649 		val = clamp_t(u16, tmr, 1,
7650 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
7651 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
7652 		req->enables |=
7653 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
7654 	}
7655 
7656 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
7657 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
7658 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
7659 	req->flags = cpu_to_le16(flags);
7660 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
7661 }
7662 
7663 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
7664 				   struct bnxt_coal *hw_coal)
7665 {
7666 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
7667 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7668 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7669 	u32 nq_params = coal_cap->nq_params;
7670 	u16 tmr;
7671 	int rc;
7672 
7673 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
7674 		return 0;
7675 
7676 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7677 	if (rc)
7678 		return rc;
7679 
7680 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
7681 	req->flags =
7682 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
7683 
7684 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
7685 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
7686 	req->int_lat_tmr_min = cpu_to_le16(tmr);
7687 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
7688 	return hwrm_req_send(bp, req);
7689 }
7690 
7691 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
7692 {
7693 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
7694 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7695 	struct bnxt_coal coal;
7696 	int rc;
7697 
7698 	/* Tick values in micro seconds.
7699 	 * 1 coal_buf x bufs_per_record = 1 completion record.
7700 	 */
7701 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
7702 
7703 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
7704 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
7705 
7706 	if (!bnapi->rx_ring)
7707 		return -ENODEV;
7708 
7709 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7710 	if (rc)
7711 		return rc;
7712 
7713 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
7714 
7715 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
7716 
7717 	return hwrm_req_send(bp, req_rx);
7718 }
7719 
7720 static int
7721 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
7722 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7723 {
7724 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
7725 
7726 	req->ring_id = cpu_to_le16(ring_id);
7727 	return hwrm_req_send(bp, req);
7728 }
7729 
7730 static int
7731 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
7732 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7733 {
7734 	struct bnxt_tx_ring_info *txr;
7735 	int i, rc;
7736 
7737 	bnxt_for_each_napi_tx(i, bnapi, txr) {
7738 		u16 ring_id;
7739 
7740 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
7741 		req->ring_id = cpu_to_le16(ring_id);
7742 		rc = hwrm_req_send(bp, req);
7743 		if (rc)
7744 			return rc;
7745 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7746 			return 0;
7747 	}
7748 	return 0;
7749 }
7750 
7751 int bnxt_hwrm_set_coal(struct bnxt *bp)
7752 {
7753 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
7754 	int i, rc;
7755 
7756 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7757 	if (rc)
7758 		return rc;
7759 
7760 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7761 	if (rc) {
7762 		hwrm_req_drop(bp, req_rx);
7763 		return rc;
7764 	}
7765 
7766 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
7767 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
7768 
7769 	hwrm_req_hold(bp, req_rx);
7770 	hwrm_req_hold(bp, req_tx);
7771 	for (i = 0; i < bp->cp_nr_rings; i++) {
7772 		struct bnxt_napi *bnapi = bp->bnapi[i];
7773 		struct bnxt_coal *hw_coal;
7774 
7775 		if (!bnapi->rx_ring)
7776 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
7777 		else
7778 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
7779 		if (rc)
7780 			break;
7781 
7782 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7783 			continue;
7784 
7785 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
7786 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
7787 			if (rc)
7788 				break;
7789 		}
7790 		if (bnapi->rx_ring)
7791 			hw_coal = &bp->rx_coal;
7792 		else
7793 			hw_coal = &bp->tx_coal;
7794 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
7795 	}
7796 	hwrm_req_drop(bp, req_rx);
7797 	hwrm_req_drop(bp, req_tx);
7798 	return rc;
7799 }
7800 
7801 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
7802 {
7803 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
7804 	struct hwrm_stat_ctx_free_input *req;
7805 	int i;
7806 
7807 	if (!bp->bnapi)
7808 		return;
7809 
7810 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
7811 		return;
7812 
7813 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
7814 		return;
7815 	if (BNXT_FW_MAJ(bp) <= 20) {
7816 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
7817 			hwrm_req_drop(bp, req);
7818 			return;
7819 		}
7820 		hwrm_req_hold(bp, req0);
7821 	}
7822 	hwrm_req_hold(bp, req);
7823 	for (i = 0; i < bp->cp_nr_rings; i++) {
7824 		struct bnxt_napi *bnapi = bp->bnapi[i];
7825 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7826 
7827 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
7828 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
7829 			if (req0) {
7830 				req0->stat_ctx_id = req->stat_ctx_id;
7831 				hwrm_req_send(bp, req0);
7832 			}
7833 			hwrm_req_send(bp, req);
7834 
7835 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
7836 		}
7837 	}
7838 	hwrm_req_drop(bp, req);
7839 	if (req0)
7840 		hwrm_req_drop(bp, req0);
7841 }
7842 
7843 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
7844 {
7845 	struct hwrm_stat_ctx_alloc_output *resp;
7846 	struct hwrm_stat_ctx_alloc_input *req;
7847 	int rc, i;
7848 
7849 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
7850 		return 0;
7851 
7852 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
7853 	if (rc)
7854 		return rc;
7855 
7856 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
7857 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
7858 
7859 	resp = hwrm_req_hold(bp, req);
7860 	for (i = 0; i < bp->cp_nr_rings; i++) {
7861 		struct bnxt_napi *bnapi = bp->bnapi[i];
7862 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7863 
7864 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
7865 
7866 		rc = hwrm_req_send(bp, req);
7867 		if (rc)
7868 			break;
7869 
7870 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
7871 
7872 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
7873 	}
7874 	hwrm_req_drop(bp, req);
7875 	return rc;
7876 }
7877 
7878 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
7879 {
7880 	struct hwrm_func_qcfg_output *resp;
7881 	struct hwrm_func_qcfg_input *req;
7882 	u16 flags;
7883 	int rc;
7884 
7885 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7886 	if (rc)
7887 		return rc;
7888 
7889 	req->fid = cpu_to_le16(0xffff);
7890 	resp = hwrm_req_hold(bp, req);
7891 	rc = hwrm_req_send(bp, req);
7892 	if (rc)
7893 		goto func_qcfg_exit;
7894 
7895 #ifdef CONFIG_BNXT_SRIOV
7896 	if (BNXT_VF(bp)) {
7897 		struct bnxt_vf_info *vf = &bp->vf;
7898 
7899 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
7900 	} else {
7901 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
7902 	}
7903 #endif
7904 	flags = le16_to_cpu(resp->flags);
7905 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
7906 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
7907 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
7908 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
7909 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
7910 	}
7911 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
7912 		bp->flags |= BNXT_FLAG_MULTI_HOST;
7913 
7914 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
7915 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
7916 
7917 	switch (resp->port_partition_type) {
7918 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
7919 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
7920 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
7921 		bp->port_partition_type = resp->port_partition_type;
7922 		break;
7923 	}
7924 	if (bp->hwrm_spec_code < 0x10707 ||
7925 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
7926 		bp->br_mode = BRIDGE_MODE_VEB;
7927 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
7928 		bp->br_mode = BRIDGE_MODE_VEPA;
7929 	else
7930 		bp->br_mode = BRIDGE_MODE_UNDEF;
7931 
7932 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
7933 	if (!bp->max_mtu)
7934 		bp->max_mtu = BNXT_MAX_MTU;
7935 
7936 	if (bp->db_size)
7937 		goto func_qcfg_exit;
7938 
7939 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
7940 	if (BNXT_CHIP_P5(bp)) {
7941 		if (BNXT_PF(bp))
7942 			bp->db_offset = DB_PF_OFFSET_P5;
7943 		else
7944 			bp->db_offset = DB_VF_OFFSET_P5;
7945 	}
7946 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
7947 				 1024);
7948 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
7949 	    bp->db_size <= bp->db_offset)
7950 		bp->db_size = pci_resource_len(bp->pdev, 2);
7951 
7952 func_qcfg_exit:
7953 	hwrm_req_drop(bp, req);
7954 	return rc;
7955 }
7956 
7957 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
7958 				      u8 init_val, u8 init_offset,
7959 				      bool init_mask_set)
7960 {
7961 	ctxm->init_value = init_val;
7962 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
7963 	if (init_mask_set)
7964 		ctxm->init_offset = init_offset * 4;
7965 	else
7966 		ctxm->init_value = 0;
7967 }
7968 
7969 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
7970 {
7971 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
7972 	u16 type;
7973 
7974 	for (type = 0; type < ctx_max; type++) {
7975 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
7976 		int n = 1;
7977 
7978 		if (!ctxm->max_entries)
7979 			continue;
7980 
7981 		if (ctxm->instance_bmap)
7982 			n = hweight32(ctxm->instance_bmap);
7983 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
7984 		if (!ctxm->pg_info)
7985 			return -ENOMEM;
7986 	}
7987 	return 0;
7988 }
7989 
7990 #define BNXT_CTX_INIT_VALID(flags)	\
7991 	(!!((flags) &			\
7992 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
7993 
7994 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
7995 {
7996 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
7997 	struct hwrm_func_backing_store_qcaps_v2_input *req;
7998 	struct bnxt_ctx_mem_info *ctx;
7999 	u16 type;
8000 	int rc;
8001 
8002 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8003 	if (rc)
8004 		return rc;
8005 
8006 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8007 	if (!ctx)
8008 		return -ENOMEM;
8009 	bp->ctx = ctx;
8010 
8011 	resp = hwrm_req_hold(bp, req);
8012 
8013 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8014 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8015 		u8 init_val, init_off, i;
8016 		__le32 *p;
8017 		u32 flags;
8018 
8019 		req->type = cpu_to_le16(type);
8020 		rc = hwrm_req_send(bp, req);
8021 		if (rc)
8022 			goto ctx_done;
8023 		flags = le32_to_cpu(resp->flags);
8024 		type = le16_to_cpu(resp->next_valid_type);
8025 		if (!(flags & FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_TYPE_VALID))
8026 			continue;
8027 
8028 		ctxm->type = le16_to_cpu(resp->type);
8029 		ctxm->entry_size = le16_to_cpu(resp->entry_size);
8030 		ctxm->flags = flags;
8031 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8032 		ctxm->entry_multiple = resp->entry_multiple;
8033 		ctxm->max_entries = le32_to_cpu(resp->max_num_entries);
8034 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8035 		init_val = resp->ctx_init_value;
8036 		init_off = resp->ctx_init_offset;
8037 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8038 					  BNXT_CTX_INIT_VALID(flags));
8039 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8040 					      BNXT_MAX_SPLIT_ENTRY);
8041 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8042 		     i++, p++)
8043 			ctxm->split[i] = le32_to_cpu(*p);
8044 	}
8045 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8046 
8047 ctx_done:
8048 	hwrm_req_drop(bp, req);
8049 	return rc;
8050 }
8051 
8052 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8053 {
8054 	struct hwrm_func_backing_store_qcaps_output *resp;
8055 	struct hwrm_func_backing_store_qcaps_input *req;
8056 	int rc;
8057 
8058 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx)
8059 		return 0;
8060 
8061 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8062 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8063 
8064 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8065 	if (rc)
8066 		return rc;
8067 
8068 	resp = hwrm_req_hold(bp, req);
8069 	rc = hwrm_req_send_silent(bp, req);
8070 	if (!rc) {
8071 		struct bnxt_ctx_mem_type *ctxm;
8072 		struct bnxt_ctx_mem_info *ctx;
8073 		u8 init_val, init_idx = 0;
8074 		u16 init_mask;
8075 
8076 		ctx = bp->ctx;
8077 		if (!ctx) {
8078 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8079 			if (!ctx) {
8080 				rc = -ENOMEM;
8081 				goto ctx_err;
8082 			}
8083 			bp->ctx = ctx;
8084 		}
8085 		init_val = resp->ctx_kind_initializer;
8086 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8087 
8088 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8089 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8090 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8091 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8092 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8093 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8094 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8095 					  (init_mask & (1 << init_idx++)) != 0);
8096 
8097 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8098 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8099 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8100 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8101 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8102 					  (init_mask & (1 << init_idx++)) != 0);
8103 
8104 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8105 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8106 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8107 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8108 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8109 					  (init_mask & (1 << init_idx++)) != 0);
8110 
8111 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8112 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8113 		ctxm->max_entries = ctxm->vnic_entries +
8114 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8115 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8116 		bnxt_init_ctx_initializer(ctxm, init_val,
8117 					  resp->vnic_init_offset,
8118 					  (init_mask & (1 << init_idx++)) != 0);
8119 
8120 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8121 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8122 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8123 		bnxt_init_ctx_initializer(ctxm, init_val,
8124 					  resp->stat_init_offset,
8125 					  (init_mask & (1 << init_idx++)) != 0);
8126 
8127 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8128 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8129 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8130 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8131 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8132 		if (!ctxm->entry_multiple)
8133 			ctxm->entry_multiple = 1;
8134 
8135 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8136 
8137 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8138 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8139 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8140 		ctxm->mrav_num_entries_units =
8141 			le16_to_cpu(resp->mrav_num_entries_units);
8142 		bnxt_init_ctx_initializer(ctxm, init_val,
8143 					  resp->mrav_init_offset,
8144 					  (init_mask & (1 << init_idx++)) != 0);
8145 
8146 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8147 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8148 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8149 
8150 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8151 		if (!ctx->tqm_fp_rings_count)
8152 			ctx->tqm_fp_rings_count = bp->max_q;
8153 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8154 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8155 
8156 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8157 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8158 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8159 
8160 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8161 	} else {
8162 		rc = 0;
8163 	}
8164 ctx_err:
8165 	hwrm_req_drop(bp, req);
8166 	return rc;
8167 }
8168 
8169 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8170 				  __le64 *pg_dir)
8171 {
8172 	if (!rmem->nr_pages)
8173 		return;
8174 
8175 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8176 	if (rmem->depth >= 1) {
8177 		if (rmem->depth == 2)
8178 			*pg_attr |= 2;
8179 		else
8180 			*pg_attr |= 1;
8181 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8182 	} else {
8183 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8184 	}
8185 }
8186 
8187 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8188 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8189 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8190 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8191 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8192 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8193 
8194 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8195 {
8196 	struct hwrm_func_backing_store_cfg_input *req;
8197 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8198 	struct bnxt_ctx_pg_info *ctx_pg;
8199 	struct bnxt_ctx_mem_type *ctxm;
8200 	void **__req = (void **)&req;
8201 	u32 req_len = sizeof(*req);
8202 	__le32 *num_entries;
8203 	__le64 *pg_dir;
8204 	u32 flags = 0;
8205 	u8 *pg_attr;
8206 	u32 ena;
8207 	int rc;
8208 	int i;
8209 
8210 	if (!ctx)
8211 		return 0;
8212 
8213 	if (req_len > bp->hwrm_max_ext_req_len)
8214 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8215 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8216 	if (rc)
8217 		return rc;
8218 
8219 	req->enables = cpu_to_le32(enables);
8220 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8221 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8222 		ctx_pg = ctxm->pg_info;
8223 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8224 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8225 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8226 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8227 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8228 				      &req->qpc_pg_size_qpc_lvl,
8229 				      &req->qpc_page_dir);
8230 
8231 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8232 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8233 	}
8234 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8235 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8236 		ctx_pg = ctxm->pg_info;
8237 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8238 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8239 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8240 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8241 				      &req->srq_pg_size_srq_lvl,
8242 				      &req->srq_page_dir);
8243 	}
8244 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8245 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8246 		ctx_pg = ctxm->pg_info;
8247 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8248 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8249 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8250 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8251 				      &req->cq_pg_size_cq_lvl,
8252 				      &req->cq_page_dir);
8253 	}
8254 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8255 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8256 		ctx_pg = ctxm->pg_info;
8257 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8258 		req->vnic_num_ring_table_entries =
8259 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8260 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8261 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8262 				      &req->vnic_pg_size_vnic_lvl,
8263 				      &req->vnic_page_dir);
8264 	}
8265 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8266 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8267 		ctx_pg = ctxm->pg_info;
8268 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8269 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8270 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8271 				      &req->stat_pg_size_stat_lvl,
8272 				      &req->stat_page_dir);
8273 	}
8274 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8275 		u32 units;
8276 
8277 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8278 		ctx_pg = ctxm->pg_info;
8279 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8280 		units = ctxm->mrav_num_entries_units;
8281 		if (units) {
8282 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8283 			u32 entries;
8284 
8285 			num_mr = ctx_pg->entries - num_ah;
8286 			entries = ((num_mr / units) << 16) | (num_ah / units);
8287 			req->mrav_num_entries = cpu_to_le32(entries);
8288 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8289 		}
8290 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8291 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8292 				      &req->mrav_pg_size_mrav_lvl,
8293 				      &req->mrav_page_dir);
8294 	}
8295 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8296 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8297 		ctx_pg = ctxm->pg_info;
8298 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8299 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8300 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8301 				      &req->tim_pg_size_tim_lvl,
8302 				      &req->tim_page_dir);
8303 	}
8304 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8305 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8306 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8307 	     pg_dir = &req->tqm_sp_page_dir,
8308 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8309 	     ctx_pg = ctxm->pg_info;
8310 	     i < BNXT_MAX_TQM_RINGS;
8311 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8312 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8313 		if (!(enables & ena))
8314 			continue;
8315 
8316 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8317 		*num_entries = cpu_to_le32(ctx_pg->entries);
8318 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8319 	}
8320 	req->flags = cpu_to_le32(flags);
8321 	return hwrm_req_send(bp, req);
8322 }
8323 
8324 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8325 				  struct bnxt_ctx_pg_info *ctx_pg)
8326 {
8327 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8328 
8329 	rmem->page_size = BNXT_PAGE_SIZE;
8330 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8331 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8332 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8333 	if (rmem->depth >= 1)
8334 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8335 	return bnxt_alloc_ring(bp, rmem);
8336 }
8337 
8338 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8339 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8340 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8341 {
8342 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8343 	int rc;
8344 
8345 	if (!mem_size)
8346 		return -EINVAL;
8347 
8348 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8349 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8350 		ctx_pg->nr_pages = 0;
8351 		return -EINVAL;
8352 	}
8353 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8354 		int nr_tbls, i;
8355 
8356 		rmem->depth = 2;
8357 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8358 					     GFP_KERNEL);
8359 		if (!ctx_pg->ctx_pg_tbl)
8360 			return -ENOMEM;
8361 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8362 		rmem->nr_pages = nr_tbls;
8363 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8364 		if (rc)
8365 			return rc;
8366 		for (i = 0; i < nr_tbls; i++) {
8367 			struct bnxt_ctx_pg_info *pg_tbl;
8368 
8369 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8370 			if (!pg_tbl)
8371 				return -ENOMEM;
8372 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8373 			rmem = &pg_tbl->ring_mem;
8374 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8375 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8376 			rmem->depth = 1;
8377 			rmem->nr_pages = MAX_CTX_PAGES;
8378 			rmem->ctx_mem = ctxm;
8379 			if (i == (nr_tbls - 1)) {
8380 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8381 
8382 				if (rem)
8383 					rmem->nr_pages = rem;
8384 			}
8385 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8386 			if (rc)
8387 				break;
8388 		}
8389 	} else {
8390 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8391 		if (rmem->nr_pages > 1 || depth)
8392 			rmem->depth = 1;
8393 		rmem->ctx_mem = ctxm;
8394 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8395 	}
8396 	return rc;
8397 }
8398 
8399 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
8400 				  struct bnxt_ctx_pg_info *ctx_pg)
8401 {
8402 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8403 
8404 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
8405 	    ctx_pg->ctx_pg_tbl) {
8406 		int i, nr_tbls = rmem->nr_pages;
8407 
8408 		for (i = 0; i < nr_tbls; i++) {
8409 			struct bnxt_ctx_pg_info *pg_tbl;
8410 			struct bnxt_ring_mem_info *rmem2;
8411 
8412 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8413 			if (!pg_tbl)
8414 				continue;
8415 			rmem2 = &pg_tbl->ring_mem;
8416 			bnxt_free_ring(bp, rmem2);
8417 			ctx_pg->ctx_pg_arr[i] = NULL;
8418 			kfree(pg_tbl);
8419 			ctx_pg->ctx_pg_tbl[i] = NULL;
8420 		}
8421 		kfree(ctx_pg->ctx_pg_tbl);
8422 		ctx_pg->ctx_pg_tbl = NULL;
8423 	}
8424 	bnxt_free_ring(bp, rmem);
8425 	ctx_pg->nr_pages = 0;
8426 }
8427 
8428 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
8429 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
8430 				   u8 pg_lvl)
8431 {
8432 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8433 	int i, rc = 0, n = 1;
8434 	u32 mem_size;
8435 
8436 	if (!ctxm->entry_size || !ctx_pg)
8437 		return -EINVAL;
8438 	if (ctxm->instance_bmap)
8439 		n = hweight32(ctxm->instance_bmap);
8440 	if (ctxm->entry_multiple)
8441 		entries = roundup(entries, ctxm->entry_multiple);
8442 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
8443 	mem_size = entries * ctxm->entry_size;
8444 	for (i = 0; i < n && !rc; i++) {
8445 		ctx_pg[i].entries = entries;
8446 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
8447 					    ctxm->init_value ? ctxm : NULL);
8448 	}
8449 	return rc;
8450 }
8451 
8452 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
8453 					       struct bnxt_ctx_mem_type *ctxm,
8454 					       bool last)
8455 {
8456 	struct hwrm_func_backing_store_cfg_v2_input *req;
8457 	u32 instance_bmap = ctxm->instance_bmap;
8458 	int i, j, rc = 0, n = 1;
8459 	__le32 *p;
8460 
8461 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
8462 		return 0;
8463 
8464 	if (instance_bmap)
8465 		n = hweight32(ctxm->instance_bmap);
8466 	else
8467 		instance_bmap = 1;
8468 
8469 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
8470 	if (rc)
8471 		return rc;
8472 	hwrm_req_hold(bp, req);
8473 	req->type = cpu_to_le16(ctxm->type);
8474 	req->entry_size = cpu_to_le16(ctxm->entry_size);
8475 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
8476 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
8477 		p[i] = cpu_to_le32(ctxm->split[i]);
8478 	for (i = 0, j = 0; j < n && !rc; i++) {
8479 		struct bnxt_ctx_pg_info *ctx_pg;
8480 
8481 		if (!(instance_bmap & (1 << i)))
8482 			continue;
8483 		req->instance = cpu_to_le16(i);
8484 		ctx_pg = &ctxm->pg_info[j++];
8485 		if (!ctx_pg->entries)
8486 			continue;
8487 		req->num_entries = cpu_to_le32(ctx_pg->entries);
8488 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8489 				      &req->page_size_pbl_level,
8490 				      &req->page_dir);
8491 		if (last && j == n)
8492 			req->flags =
8493 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
8494 		rc = hwrm_req_send(bp, req);
8495 	}
8496 	hwrm_req_drop(bp, req);
8497 	return rc;
8498 }
8499 
8500 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
8501 {
8502 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8503 	struct bnxt_ctx_mem_type *ctxm;
8504 	u16 last_type;
8505 	int rc = 0;
8506 	u16 type;
8507 
8508 	if (!ena)
8509 		return 0;
8510 	else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
8511 		last_type = BNXT_CTX_MAX - 1;
8512 	else
8513 		last_type = BNXT_CTX_L2_MAX - 1;
8514 	ctx->ctx_arr[last_type].last = 1;
8515 
8516 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
8517 		ctxm = &ctx->ctx_arr[type];
8518 
8519 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
8520 		if (rc)
8521 			return rc;
8522 	}
8523 	return 0;
8524 }
8525 
8526 void bnxt_free_ctx_mem(struct bnxt *bp)
8527 {
8528 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8529 	u16 type;
8530 
8531 	if (!ctx)
8532 		return;
8533 
8534 	for (type = 0; type < BNXT_CTX_V2_MAX; type++) {
8535 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8536 		struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8537 		int i, n = 1;
8538 
8539 		if (!ctx_pg)
8540 			continue;
8541 		if (ctxm->instance_bmap)
8542 			n = hweight32(ctxm->instance_bmap);
8543 		for (i = 0; i < n; i++)
8544 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
8545 
8546 		kfree(ctx_pg);
8547 		ctxm->pg_info = NULL;
8548 	}
8549 
8550 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
8551 	kfree(ctx);
8552 	bp->ctx = NULL;
8553 }
8554 
8555 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
8556 {
8557 	struct bnxt_ctx_mem_type *ctxm;
8558 	struct bnxt_ctx_mem_info *ctx;
8559 	u32 l2_qps, qp1_qps, max_qps;
8560 	u32 ena, entries_sp, entries;
8561 	u32 srqs, max_srqs, min;
8562 	u32 num_mr, num_ah;
8563 	u32 extra_srqs = 0;
8564 	u32 extra_qps = 0;
8565 	u32 fast_qpmd_qps;
8566 	u8 pg_lvl = 1;
8567 	int i, rc;
8568 
8569 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
8570 	if (rc) {
8571 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
8572 			   rc);
8573 		return rc;
8574 	}
8575 	ctx = bp->ctx;
8576 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
8577 		return 0;
8578 
8579 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8580 	l2_qps = ctxm->qp_l2_entries;
8581 	qp1_qps = ctxm->qp_qp1_entries;
8582 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
8583 	max_qps = ctxm->max_entries;
8584 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8585 	srqs = ctxm->srq_l2_entries;
8586 	max_srqs = ctxm->max_entries;
8587 	ena = 0;
8588 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
8589 		pg_lvl = 2;
8590 		extra_qps = min_t(u32, 65536, max_qps - l2_qps - qp1_qps);
8591 		/* allocate extra qps if fw supports RoCE fast qp destroy feature */
8592 		extra_qps += fast_qpmd_qps;
8593 		extra_srqs = min_t(u32, 8192, max_srqs - srqs);
8594 		if (fast_qpmd_qps)
8595 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
8596 	}
8597 
8598 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8599 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
8600 				     pg_lvl);
8601 	if (rc)
8602 		return rc;
8603 
8604 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8605 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
8606 	if (rc)
8607 		return rc;
8608 
8609 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8610 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
8611 				     extra_qps * 2, pg_lvl);
8612 	if (rc)
8613 		return rc;
8614 
8615 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8616 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
8617 	if (rc)
8618 		return rc;
8619 
8620 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8621 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
8622 	if (rc)
8623 		return rc;
8624 
8625 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
8626 		goto skip_rdma;
8627 
8628 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8629 	/* 128K extra is needed to accommodate static AH context
8630 	 * allocation by f/w.
8631 	 */
8632 	num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
8633 	num_ah = min_t(u32, num_mr, 1024 * 128);
8634 	ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
8635 	if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
8636 		ctxm->mrav_av_entries = num_ah;
8637 
8638 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
8639 	if (rc)
8640 		return rc;
8641 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
8642 
8643 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8644 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
8645 	if (rc)
8646 		return rc;
8647 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
8648 
8649 skip_rdma:
8650 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8651 	min = ctxm->min_entries;
8652 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
8653 		     2 * (extra_qps + qp1_qps) + min;
8654 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
8655 	if (rc)
8656 		return rc;
8657 
8658 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8659 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
8660 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
8661 	if (rc)
8662 		return rc;
8663 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
8664 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
8665 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
8666 
8667 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8668 		rc = bnxt_backing_store_cfg_v2(bp, ena);
8669 	else
8670 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
8671 	if (rc) {
8672 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
8673 			   rc);
8674 		return rc;
8675 	}
8676 	ctx->flags |= BNXT_CTX_FLAG_INITED;
8677 	return 0;
8678 }
8679 
8680 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
8681 {
8682 	struct hwrm_func_resource_qcaps_output *resp;
8683 	struct hwrm_func_resource_qcaps_input *req;
8684 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8685 	int rc;
8686 
8687 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
8688 	if (rc)
8689 		return rc;
8690 
8691 	req->fid = cpu_to_le16(0xffff);
8692 	resp = hwrm_req_hold(bp, req);
8693 	rc = hwrm_req_send_silent(bp, req);
8694 	if (rc)
8695 		goto hwrm_func_resc_qcaps_exit;
8696 
8697 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
8698 	if (!all)
8699 		goto hwrm_func_resc_qcaps_exit;
8700 
8701 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
8702 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
8703 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
8704 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
8705 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
8706 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
8707 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
8708 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
8709 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
8710 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
8711 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
8712 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
8713 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
8714 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
8715 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
8716 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
8717 
8718 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8719 		u16 max_msix = le16_to_cpu(resp->max_msix);
8720 
8721 		hw_resc->max_nqs = max_msix;
8722 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
8723 	}
8724 
8725 	if (BNXT_PF(bp)) {
8726 		struct bnxt_pf_info *pf = &bp->pf;
8727 
8728 		pf->vf_resv_strategy =
8729 			le16_to_cpu(resp->vf_reservation_strategy);
8730 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
8731 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
8732 	}
8733 hwrm_func_resc_qcaps_exit:
8734 	hwrm_req_drop(bp, req);
8735 	return rc;
8736 }
8737 
8738 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
8739 {
8740 	struct hwrm_port_mac_ptp_qcfg_output *resp;
8741 	struct hwrm_port_mac_ptp_qcfg_input *req;
8742 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
8743 	bool phc_cfg;
8744 	u8 flags;
8745 	int rc;
8746 
8747 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5(bp)) {
8748 		rc = -ENODEV;
8749 		goto no_ptp;
8750 	}
8751 
8752 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
8753 	if (rc)
8754 		goto no_ptp;
8755 
8756 	req->port_id = cpu_to_le16(bp->pf.port_id);
8757 	resp = hwrm_req_hold(bp, req);
8758 	rc = hwrm_req_send(bp, req);
8759 	if (rc)
8760 		goto exit;
8761 
8762 	flags = resp->flags;
8763 	if (!(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
8764 		rc = -ENODEV;
8765 		goto exit;
8766 	}
8767 	if (!ptp) {
8768 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
8769 		if (!ptp) {
8770 			rc = -ENOMEM;
8771 			goto exit;
8772 		}
8773 		ptp->bp = bp;
8774 		bp->ptp_cfg = ptp;
8775 	}
8776 	if (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK) {
8777 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
8778 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
8779 	} else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8780 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
8781 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
8782 	} else {
8783 		rc = -ENODEV;
8784 		goto exit;
8785 	}
8786 	phc_cfg = (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
8787 	rc = bnxt_ptp_init(bp, phc_cfg);
8788 	if (rc)
8789 		netdev_warn(bp->dev, "PTP initialization failed.\n");
8790 exit:
8791 	hwrm_req_drop(bp, req);
8792 	if (!rc)
8793 		return 0;
8794 
8795 no_ptp:
8796 	bnxt_ptp_clear(bp);
8797 	kfree(ptp);
8798 	bp->ptp_cfg = NULL;
8799 	return rc;
8800 }
8801 
8802 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
8803 {
8804 	struct hwrm_func_qcaps_output *resp;
8805 	struct hwrm_func_qcaps_input *req;
8806 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8807 	u32 flags, flags_ext, flags_ext2;
8808 	int rc;
8809 
8810 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
8811 	if (rc)
8812 		return rc;
8813 
8814 	req->fid = cpu_to_le16(0xffff);
8815 	resp = hwrm_req_hold(bp, req);
8816 	rc = hwrm_req_send(bp, req);
8817 	if (rc)
8818 		goto hwrm_func_qcaps_exit;
8819 
8820 	flags = le32_to_cpu(resp->flags);
8821 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
8822 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
8823 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
8824 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
8825 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
8826 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
8827 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
8828 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
8829 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
8830 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
8831 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
8832 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
8833 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
8834 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
8835 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
8836 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
8837 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
8838 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
8839 
8840 	flags_ext = le32_to_cpu(resp->flags_ext);
8841 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
8842 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
8843 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
8844 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
8845 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
8846 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
8847 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
8848 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
8849 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
8850 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
8851 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
8852 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
8853 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
8854 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
8855 
8856 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
8857 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
8858 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
8859 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
8860 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
8861 
8862 	bp->tx_push_thresh = 0;
8863 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
8864 	    BNXT_FW_MAJ(bp) > 217)
8865 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
8866 
8867 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
8868 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
8869 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
8870 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
8871 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
8872 	if (!hw_resc->max_hw_ring_grps)
8873 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
8874 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
8875 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
8876 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
8877 
8878 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
8879 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
8880 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
8881 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
8882 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
8883 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
8884 
8885 	if (BNXT_PF(bp)) {
8886 		struct bnxt_pf_info *pf = &bp->pf;
8887 
8888 		pf->fw_fid = le16_to_cpu(resp->fid);
8889 		pf->port_id = le16_to_cpu(resp->port_id);
8890 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
8891 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
8892 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
8893 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
8894 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
8895 			bp->flags |= BNXT_FLAG_WOL_CAP;
8896 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
8897 			bp->fw_cap |= BNXT_FW_CAP_PTP;
8898 		} else {
8899 			bnxt_ptp_clear(bp);
8900 			kfree(bp->ptp_cfg);
8901 			bp->ptp_cfg = NULL;
8902 		}
8903 	} else {
8904 #ifdef CONFIG_BNXT_SRIOV
8905 		struct bnxt_vf_info *vf = &bp->vf;
8906 
8907 		vf->fw_fid = le16_to_cpu(resp->fid);
8908 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
8909 #endif
8910 	}
8911 
8912 hwrm_func_qcaps_exit:
8913 	hwrm_req_drop(bp, req);
8914 	return rc;
8915 }
8916 
8917 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
8918 {
8919 	struct hwrm_dbg_qcaps_output *resp;
8920 	struct hwrm_dbg_qcaps_input *req;
8921 	int rc;
8922 
8923 	bp->fw_dbg_cap = 0;
8924 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
8925 		return;
8926 
8927 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
8928 	if (rc)
8929 		return;
8930 
8931 	req->fid = cpu_to_le16(0xffff);
8932 	resp = hwrm_req_hold(bp, req);
8933 	rc = hwrm_req_send(bp, req);
8934 	if (rc)
8935 		goto hwrm_dbg_qcaps_exit;
8936 
8937 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
8938 
8939 hwrm_dbg_qcaps_exit:
8940 	hwrm_req_drop(bp, req);
8941 }
8942 
8943 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
8944 
8945 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
8946 {
8947 	int rc;
8948 
8949 	rc = __bnxt_hwrm_func_qcaps(bp);
8950 	if (rc)
8951 		return rc;
8952 
8953 	bnxt_hwrm_dbg_qcaps(bp);
8954 
8955 	rc = bnxt_hwrm_queue_qportcfg(bp);
8956 	if (rc) {
8957 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
8958 		return rc;
8959 	}
8960 	if (bp->hwrm_spec_code >= 0x10803) {
8961 		rc = bnxt_alloc_ctx_mem(bp);
8962 		if (rc)
8963 			return rc;
8964 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
8965 		if (!rc)
8966 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
8967 	}
8968 	return 0;
8969 }
8970 
8971 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
8972 {
8973 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
8974 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
8975 	u32 flags;
8976 	int rc;
8977 
8978 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
8979 		return 0;
8980 
8981 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
8982 	if (rc)
8983 		return rc;
8984 
8985 	resp = hwrm_req_hold(bp, req);
8986 	rc = hwrm_req_send(bp, req);
8987 	if (rc)
8988 		goto hwrm_cfa_adv_qcaps_exit;
8989 
8990 	flags = le32_to_cpu(resp->flags);
8991 	if (flags &
8992 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
8993 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
8994 
8995 	if (flags &
8996 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
8997 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
8998 
8999 	if (flags &
9000 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9001 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9002 
9003 hwrm_cfa_adv_qcaps_exit:
9004 	hwrm_req_drop(bp, req);
9005 	return rc;
9006 }
9007 
9008 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9009 {
9010 	if (bp->fw_health)
9011 		return 0;
9012 
9013 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9014 	if (!bp->fw_health)
9015 		return -ENOMEM;
9016 
9017 	mutex_init(&bp->fw_health->lock);
9018 	return 0;
9019 }
9020 
9021 static int bnxt_alloc_fw_health(struct bnxt *bp)
9022 {
9023 	int rc;
9024 
9025 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9026 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9027 		return 0;
9028 
9029 	rc = __bnxt_alloc_fw_health(bp);
9030 	if (rc) {
9031 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9032 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9033 		return rc;
9034 	}
9035 
9036 	return 0;
9037 }
9038 
9039 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9040 {
9041 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9042 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9043 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9044 }
9045 
9046 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9047 {
9048 	struct bnxt_fw_health *fw_health = bp->fw_health;
9049 	u32 reg_type;
9050 
9051 	if (!fw_health)
9052 		return;
9053 
9054 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9055 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9056 		fw_health->status_reliable = false;
9057 
9058 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9059 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9060 		fw_health->resets_reliable = false;
9061 }
9062 
9063 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9064 {
9065 	void __iomem *hs;
9066 	u32 status_loc;
9067 	u32 reg_type;
9068 	u32 sig;
9069 
9070 	if (bp->fw_health)
9071 		bp->fw_health->status_reliable = false;
9072 
9073 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9074 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9075 
9076 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9077 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9078 		if (!bp->chip_num) {
9079 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9080 			bp->chip_num = readl(bp->bar0 +
9081 					     BNXT_FW_HEALTH_WIN_BASE +
9082 					     BNXT_GRC_REG_CHIP_NUM);
9083 		}
9084 		if (!BNXT_CHIP_P5_PLUS(bp))
9085 			return;
9086 
9087 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9088 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9089 	} else {
9090 		status_loc = readl(hs + offsetof(struct hcomm_status,
9091 						 fw_status_loc));
9092 	}
9093 
9094 	if (__bnxt_alloc_fw_health(bp)) {
9095 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9096 		return;
9097 	}
9098 
9099 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9100 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9101 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9102 		__bnxt_map_fw_health_reg(bp, status_loc);
9103 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9104 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9105 	}
9106 
9107 	bp->fw_health->status_reliable = true;
9108 }
9109 
9110 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9111 {
9112 	struct bnxt_fw_health *fw_health = bp->fw_health;
9113 	u32 reg_base = 0xffffffff;
9114 	int i;
9115 
9116 	bp->fw_health->status_reliable = false;
9117 	bp->fw_health->resets_reliable = false;
9118 	/* Only pre-map the monitoring GRC registers using window 3 */
9119 	for (i = 0; i < 4; i++) {
9120 		u32 reg = fw_health->regs[i];
9121 
9122 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9123 			continue;
9124 		if (reg_base == 0xffffffff)
9125 			reg_base = reg & BNXT_GRC_BASE_MASK;
9126 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9127 			return -ERANGE;
9128 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9129 	}
9130 	bp->fw_health->status_reliable = true;
9131 	bp->fw_health->resets_reliable = true;
9132 	if (reg_base == 0xffffffff)
9133 		return 0;
9134 
9135 	__bnxt_map_fw_health_reg(bp, reg_base);
9136 	return 0;
9137 }
9138 
9139 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9140 {
9141 	if (!bp->fw_health)
9142 		return;
9143 
9144 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9145 		bp->fw_health->status_reliable = true;
9146 		bp->fw_health->resets_reliable = true;
9147 	} else {
9148 		bnxt_try_map_fw_health_reg(bp);
9149 	}
9150 }
9151 
9152 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9153 {
9154 	struct bnxt_fw_health *fw_health = bp->fw_health;
9155 	struct hwrm_error_recovery_qcfg_output *resp;
9156 	struct hwrm_error_recovery_qcfg_input *req;
9157 	int rc, i;
9158 
9159 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9160 		return 0;
9161 
9162 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9163 	if (rc)
9164 		return rc;
9165 
9166 	resp = hwrm_req_hold(bp, req);
9167 	rc = hwrm_req_send(bp, req);
9168 	if (rc)
9169 		goto err_recovery_out;
9170 	fw_health->flags = le32_to_cpu(resp->flags);
9171 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
9172 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
9173 		rc = -EINVAL;
9174 		goto err_recovery_out;
9175 	}
9176 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
9177 	fw_health->master_func_wait_dsecs =
9178 		le32_to_cpu(resp->master_func_wait_period);
9179 	fw_health->normal_func_wait_dsecs =
9180 		le32_to_cpu(resp->normal_func_wait_period);
9181 	fw_health->post_reset_wait_dsecs =
9182 		le32_to_cpu(resp->master_func_wait_period_after_reset);
9183 	fw_health->post_reset_max_wait_dsecs =
9184 		le32_to_cpu(resp->max_bailout_time_after_reset);
9185 	fw_health->regs[BNXT_FW_HEALTH_REG] =
9186 		le32_to_cpu(resp->fw_health_status_reg);
9187 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
9188 		le32_to_cpu(resp->fw_heartbeat_reg);
9189 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
9190 		le32_to_cpu(resp->fw_reset_cnt_reg);
9191 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
9192 		le32_to_cpu(resp->reset_inprogress_reg);
9193 	fw_health->fw_reset_inprog_reg_mask =
9194 		le32_to_cpu(resp->reset_inprogress_reg_mask);
9195 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
9196 	if (fw_health->fw_reset_seq_cnt >= 16) {
9197 		rc = -EINVAL;
9198 		goto err_recovery_out;
9199 	}
9200 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
9201 		fw_health->fw_reset_seq_regs[i] =
9202 			le32_to_cpu(resp->reset_reg[i]);
9203 		fw_health->fw_reset_seq_vals[i] =
9204 			le32_to_cpu(resp->reset_reg_val[i]);
9205 		fw_health->fw_reset_seq_delay_msec[i] =
9206 			resp->delay_after_reset[i];
9207 	}
9208 err_recovery_out:
9209 	hwrm_req_drop(bp, req);
9210 	if (!rc)
9211 		rc = bnxt_map_fw_health_regs(bp);
9212 	if (rc)
9213 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9214 	return rc;
9215 }
9216 
9217 static int bnxt_hwrm_func_reset(struct bnxt *bp)
9218 {
9219 	struct hwrm_func_reset_input *req;
9220 	int rc;
9221 
9222 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
9223 	if (rc)
9224 		return rc;
9225 
9226 	req->enables = 0;
9227 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
9228 	return hwrm_req_send(bp, req);
9229 }
9230 
9231 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
9232 {
9233 	struct hwrm_nvm_get_dev_info_output nvm_info;
9234 
9235 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
9236 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
9237 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
9238 			 nvm_info.nvm_cfg_ver_upd);
9239 }
9240 
9241 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
9242 {
9243 	struct hwrm_queue_qportcfg_output *resp;
9244 	struct hwrm_queue_qportcfg_input *req;
9245 	u8 i, j, *qptr;
9246 	bool no_rdma;
9247 	int rc = 0;
9248 
9249 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
9250 	if (rc)
9251 		return rc;
9252 
9253 	resp = hwrm_req_hold(bp, req);
9254 	rc = hwrm_req_send(bp, req);
9255 	if (rc)
9256 		goto qportcfg_exit;
9257 
9258 	if (!resp->max_configurable_queues) {
9259 		rc = -EINVAL;
9260 		goto qportcfg_exit;
9261 	}
9262 	bp->max_tc = resp->max_configurable_queues;
9263 	bp->max_lltc = resp->max_configurable_lossless_queues;
9264 	if (bp->max_tc > BNXT_MAX_QUEUE)
9265 		bp->max_tc = BNXT_MAX_QUEUE;
9266 
9267 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
9268 	qptr = &resp->queue_id0;
9269 	for (i = 0, j = 0; i < bp->max_tc; i++) {
9270 		bp->q_info[j].queue_id = *qptr;
9271 		bp->q_ids[i] = *qptr++;
9272 		bp->q_info[j].queue_profile = *qptr++;
9273 		bp->tc_to_qidx[j] = j;
9274 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
9275 		    (no_rdma && BNXT_PF(bp)))
9276 			j++;
9277 	}
9278 	bp->max_q = bp->max_tc;
9279 	bp->max_tc = max_t(u8, j, 1);
9280 
9281 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
9282 		bp->max_tc = 1;
9283 
9284 	if (bp->max_lltc > bp->max_tc)
9285 		bp->max_lltc = bp->max_tc;
9286 
9287 qportcfg_exit:
9288 	hwrm_req_drop(bp, req);
9289 	return rc;
9290 }
9291 
9292 static int bnxt_hwrm_poll(struct bnxt *bp)
9293 {
9294 	struct hwrm_ver_get_input *req;
9295 	int rc;
9296 
9297 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9298 	if (rc)
9299 		return rc;
9300 
9301 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9302 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9303 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9304 
9305 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
9306 	rc = hwrm_req_send(bp, req);
9307 	return rc;
9308 }
9309 
9310 static int bnxt_hwrm_ver_get(struct bnxt *bp)
9311 {
9312 	struct hwrm_ver_get_output *resp;
9313 	struct hwrm_ver_get_input *req;
9314 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
9315 	u32 dev_caps_cfg, hwrm_ver;
9316 	int rc, len;
9317 
9318 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9319 	if (rc)
9320 		return rc;
9321 
9322 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
9323 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
9324 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9325 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9326 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9327 
9328 	resp = hwrm_req_hold(bp, req);
9329 	rc = hwrm_req_send(bp, req);
9330 	if (rc)
9331 		goto hwrm_ver_get_exit;
9332 
9333 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
9334 
9335 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
9336 			     resp->hwrm_intf_min_8b << 8 |
9337 			     resp->hwrm_intf_upd_8b;
9338 	if (resp->hwrm_intf_maj_8b < 1) {
9339 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
9340 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
9341 			    resp->hwrm_intf_upd_8b);
9342 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
9343 	}
9344 
9345 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
9346 			HWRM_VERSION_UPDATE;
9347 
9348 	if (bp->hwrm_spec_code > hwrm_ver)
9349 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
9350 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
9351 			 HWRM_VERSION_UPDATE);
9352 	else
9353 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
9354 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
9355 			 resp->hwrm_intf_upd_8b);
9356 
9357 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
9358 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
9359 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
9360 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
9361 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
9362 		len = FW_VER_STR_LEN;
9363 	} else {
9364 		fw_maj = resp->hwrm_fw_maj_8b;
9365 		fw_min = resp->hwrm_fw_min_8b;
9366 		fw_bld = resp->hwrm_fw_bld_8b;
9367 		fw_rsv = resp->hwrm_fw_rsvd_8b;
9368 		len = BC_HWRM_STR_LEN;
9369 	}
9370 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
9371 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
9372 		 fw_rsv);
9373 
9374 	if (strlen(resp->active_pkg_name)) {
9375 		int fw_ver_len = strlen(bp->fw_ver_str);
9376 
9377 		snprintf(bp->fw_ver_str + fw_ver_len,
9378 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
9379 			 resp->active_pkg_name);
9380 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
9381 	}
9382 
9383 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
9384 	if (!bp->hwrm_cmd_timeout)
9385 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
9386 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
9387 	if (!bp->hwrm_cmd_max_timeout)
9388 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
9389 	else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT)
9390 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n",
9391 			    bp->hwrm_cmd_max_timeout / 1000);
9392 
9393 	if (resp->hwrm_intf_maj_8b >= 1) {
9394 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
9395 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
9396 	}
9397 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
9398 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
9399 
9400 	bp->chip_num = le16_to_cpu(resp->chip_num);
9401 	bp->chip_rev = resp->chip_rev;
9402 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
9403 	    !resp->chip_metal)
9404 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
9405 
9406 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
9407 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
9408 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
9409 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
9410 
9411 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
9412 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
9413 
9414 	if (dev_caps_cfg &
9415 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
9416 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
9417 
9418 	if (dev_caps_cfg &
9419 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
9420 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
9421 
9422 	if (dev_caps_cfg &
9423 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
9424 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
9425 
9426 hwrm_ver_get_exit:
9427 	hwrm_req_drop(bp, req);
9428 	return rc;
9429 }
9430 
9431 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
9432 {
9433 	struct hwrm_fw_set_time_input *req;
9434 	struct tm tm;
9435 	time64_t now = ktime_get_real_seconds();
9436 	int rc;
9437 
9438 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
9439 	    bp->hwrm_spec_code < 0x10400)
9440 		return -EOPNOTSUPP;
9441 
9442 	time64_to_tm(now, 0, &tm);
9443 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
9444 	if (rc)
9445 		return rc;
9446 
9447 	req->year = cpu_to_le16(1900 + tm.tm_year);
9448 	req->month = 1 + tm.tm_mon;
9449 	req->day = tm.tm_mday;
9450 	req->hour = tm.tm_hour;
9451 	req->minute = tm.tm_min;
9452 	req->second = tm.tm_sec;
9453 	return hwrm_req_send(bp, req);
9454 }
9455 
9456 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
9457 {
9458 	u64 sw_tmp;
9459 
9460 	hw &= mask;
9461 	sw_tmp = (*sw & ~mask) | hw;
9462 	if (hw < (*sw & mask))
9463 		sw_tmp += mask + 1;
9464 	WRITE_ONCE(*sw, sw_tmp);
9465 }
9466 
9467 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
9468 				    int count, bool ignore_zero)
9469 {
9470 	int i;
9471 
9472 	for (i = 0; i < count; i++) {
9473 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
9474 
9475 		if (ignore_zero && !hw)
9476 			continue;
9477 
9478 		if (masks[i] == -1ULL)
9479 			sw_stats[i] = hw;
9480 		else
9481 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
9482 	}
9483 }
9484 
9485 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
9486 {
9487 	if (!stats->hw_stats)
9488 		return;
9489 
9490 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
9491 				stats->hw_masks, stats->len / 8, false);
9492 }
9493 
9494 static void bnxt_accumulate_all_stats(struct bnxt *bp)
9495 {
9496 	struct bnxt_stats_mem *ring0_stats;
9497 	bool ignore_zero = false;
9498 	int i;
9499 
9500 	/* Chip bug.  Counter intermittently becomes 0. */
9501 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
9502 		ignore_zero = true;
9503 
9504 	for (i = 0; i < bp->cp_nr_rings; i++) {
9505 		struct bnxt_napi *bnapi = bp->bnapi[i];
9506 		struct bnxt_cp_ring_info *cpr;
9507 		struct bnxt_stats_mem *stats;
9508 
9509 		cpr = &bnapi->cp_ring;
9510 		stats = &cpr->stats;
9511 		if (!i)
9512 			ring0_stats = stats;
9513 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
9514 					ring0_stats->hw_masks,
9515 					ring0_stats->len / 8, ignore_zero);
9516 	}
9517 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
9518 		struct bnxt_stats_mem *stats = &bp->port_stats;
9519 		__le64 *hw_stats = stats->hw_stats;
9520 		u64 *sw_stats = stats->sw_stats;
9521 		u64 *masks = stats->hw_masks;
9522 		int cnt;
9523 
9524 		cnt = sizeof(struct rx_port_stats) / 8;
9525 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
9526 
9527 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9528 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9529 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9530 		cnt = sizeof(struct tx_port_stats) / 8;
9531 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
9532 	}
9533 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
9534 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
9535 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
9536 	}
9537 }
9538 
9539 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
9540 {
9541 	struct hwrm_port_qstats_input *req;
9542 	struct bnxt_pf_info *pf = &bp->pf;
9543 	int rc;
9544 
9545 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
9546 		return 0;
9547 
9548 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
9549 		return -EOPNOTSUPP;
9550 
9551 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
9552 	if (rc)
9553 		return rc;
9554 
9555 	req->flags = flags;
9556 	req->port_id = cpu_to_le16(pf->port_id);
9557 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
9558 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
9559 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
9560 	return hwrm_req_send(bp, req);
9561 }
9562 
9563 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
9564 {
9565 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
9566 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
9567 	struct hwrm_port_qstats_ext_output *resp_qs;
9568 	struct hwrm_port_qstats_ext_input *req_qs;
9569 	struct bnxt_pf_info *pf = &bp->pf;
9570 	u32 tx_stat_size;
9571 	int rc;
9572 
9573 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
9574 		return 0;
9575 
9576 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
9577 		return -EOPNOTSUPP;
9578 
9579 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
9580 	if (rc)
9581 		return rc;
9582 
9583 	req_qs->flags = flags;
9584 	req_qs->port_id = cpu_to_le16(pf->port_id);
9585 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
9586 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
9587 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
9588 		       sizeof(struct tx_port_stats_ext) : 0;
9589 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
9590 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
9591 	resp_qs = hwrm_req_hold(bp, req_qs);
9592 	rc = hwrm_req_send(bp, req_qs);
9593 	if (!rc) {
9594 		bp->fw_rx_stats_ext_size =
9595 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
9596 		if (BNXT_FW_MAJ(bp) < 220 &&
9597 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
9598 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
9599 
9600 		bp->fw_tx_stats_ext_size = tx_stat_size ?
9601 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
9602 	} else {
9603 		bp->fw_rx_stats_ext_size = 0;
9604 		bp->fw_tx_stats_ext_size = 0;
9605 	}
9606 	hwrm_req_drop(bp, req_qs);
9607 
9608 	if (flags)
9609 		return rc;
9610 
9611 	if (bp->fw_tx_stats_ext_size <=
9612 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
9613 		bp->pri2cos_valid = 0;
9614 		return rc;
9615 	}
9616 
9617 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
9618 	if (rc)
9619 		return rc;
9620 
9621 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
9622 
9623 	resp_qc = hwrm_req_hold(bp, req_qc);
9624 	rc = hwrm_req_send(bp, req_qc);
9625 	if (!rc) {
9626 		u8 *pri2cos;
9627 		int i, j;
9628 
9629 		pri2cos = &resp_qc->pri0_cos_queue_id;
9630 		for (i = 0; i < 8; i++) {
9631 			u8 queue_id = pri2cos[i];
9632 			u8 queue_idx;
9633 
9634 			/* Per port queue IDs start from 0, 10, 20, etc */
9635 			queue_idx = queue_id % 10;
9636 			if (queue_idx > BNXT_MAX_QUEUE) {
9637 				bp->pri2cos_valid = false;
9638 				hwrm_req_drop(bp, req_qc);
9639 				return rc;
9640 			}
9641 			for (j = 0; j < bp->max_q; j++) {
9642 				if (bp->q_ids[j] == queue_id)
9643 					bp->pri2cos_idx[i] = queue_idx;
9644 			}
9645 		}
9646 		bp->pri2cos_valid = true;
9647 	}
9648 	hwrm_req_drop(bp, req_qc);
9649 
9650 	return rc;
9651 }
9652 
9653 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
9654 {
9655 	bnxt_hwrm_tunnel_dst_port_free(bp,
9656 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
9657 	bnxt_hwrm_tunnel_dst_port_free(bp,
9658 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
9659 }
9660 
9661 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
9662 {
9663 	int rc, i;
9664 	u32 tpa_flags = 0;
9665 
9666 	if (set_tpa)
9667 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
9668 	else if (BNXT_NO_FW_ACCESS(bp))
9669 		return 0;
9670 	for (i = 0; i < bp->nr_vnics; i++) {
9671 		rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
9672 		if (rc) {
9673 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
9674 				   i, rc);
9675 			return rc;
9676 		}
9677 	}
9678 	return 0;
9679 }
9680 
9681 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
9682 {
9683 	int i;
9684 
9685 	for (i = 0; i < bp->nr_vnics; i++)
9686 		bnxt_hwrm_vnic_set_rss(bp, i, false);
9687 }
9688 
9689 static void bnxt_clear_vnic(struct bnxt *bp)
9690 {
9691 	if (!bp->vnic_info)
9692 		return;
9693 
9694 	bnxt_hwrm_clear_vnic_filter(bp);
9695 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
9696 		/* clear all RSS setting before free vnic ctx */
9697 		bnxt_hwrm_clear_vnic_rss(bp);
9698 		bnxt_hwrm_vnic_ctx_free(bp);
9699 	}
9700 	/* before free the vnic, undo the vnic tpa settings */
9701 	if (bp->flags & BNXT_FLAG_TPA)
9702 		bnxt_set_tpa(bp, false);
9703 	bnxt_hwrm_vnic_free(bp);
9704 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
9705 		bnxt_hwrm_vnic_ctx_free(bp);
9706 }
9707 
9708 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
9709 				    bool irq_re_init)
9710 {
9711 	bnxt_clear_vnic(bp);
9712 	bnxt_hwrm_ring_free(bp, close_path);
9713 	bnxt_hwrm_ring_grp_free(bp);
9714 	if (irq_re_init) {
9715 		bnxt_hwrm_stat_ctx_free(bp);
9716 		bnxt_hwrm_free_tunnel_ports(bp);
9717 	}
9718 }
9719 
9720 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
9721 {
9722 	struct hwrm_func_cfg_input *req;
9723 	u8 evb_mode;
9724 	int rc;
9725 
9726 	if (br_mode == BRIDGE_MODE_VEB)
9727 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
9728 	else if (br_mode == BRIDGE_MODE_VEPA)
9729 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
9730 	else
9731 		return -EINVAL;
9732 
9733 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
9734 	if (rc)
9735 		return rc;
9736 
9737 	req->fid = cpu_to_le16(0xffff);
9738 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
9739 	req->evb_mode = evb_mode;
9740 	return hwrm_req_send(bp, req);
9741 }
9742 
9743 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
9744 {
9745 	struct hwrm_func_cfg_input *req;
9746 	int rc;
9747 
9748 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
9749 		return 0;
9750 
9751 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
9752 	if (rc)
9753 		return rc;
9754 
9755 	req->fid = cpu_to_le16(0xffff);
9756 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
9757 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
9758 	if (size == 128)
9759 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
9760 
9761 	return hwrm_req_send(bp, req);
9762 }
9763 
9764 static int __bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
9765 {
9766 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
9767 	int rc;
9768 
9769 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
9770 		goto skip_rss_ctx;
9771 
9772 	/* allocate context for vnic */
9773 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
9774 	if (rc) {
9775 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
9776 			   vnic_id, rc);
9777 		goto vnic_setup_err;
9778 	}
9779 	bp->rsscos_nr_ctxs++;
9780 
9781 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
9782 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
9783 		if (rc) {
9784 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
9785 				   vnic_id, rc);
9786 			goto vnic_setup_err;
9787 		}
9788 		bp->rsscos_nr_ctxs++;
9789 	}
9790 
9791 skip_rss_ctx:
9792 	/* configure default vnic, ring grp */
9793 	rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
9794 	if (rc) {
9795 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
9796 			   vnic_id, rc);
9797 		goto vnic_setup_err;
9798 	}
9799 
9800 	/* Enable RSS hashing on vnic */
9801 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
9802 	if (rc) {
9803 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
9804 			   vnic_id, rc);
9805 		goto vnic_setup_err;
9806 	}
9807 
9808 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
9809 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
9810 		if (rc) {
9811 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
9812 				   vnic_id, rc);
9813 		}
9814 	}
9815 
9816 vnic_setup_err:
9817 	return rc;
9818 }
9819 
9820 static int __bnxt_setup_vnic_p5(struct bnxt *bp, u16 vnic_id)
9821 {
9822 	int rc, i, nr_ctxs;
9823 
9824 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
9825 	for (i = 0; i < nr_ctxs; i++) {
9826 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, i);
9827 		if (rc) {
9828 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
9829 				   vnic_id, i, rc);
9830 			break;
9831 		}
9832 		bp->rsscos_nr_ctxs++;
9833 	}
9834 	if (i < nr_ctxs)
9835 		return -ENOMEM;
9836 
9837 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic_id, true);
9838 	if (rc) {
9839 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
9840 			   vnic_id, rc);
9841 		return rc;
9842 	}
9843 	rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
9844 	if (rc) {
9845 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
9846 			   vnic_id, rc);
9847 		return rc;
9848 	}
9849 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
9850 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
9851 		if (rc) {
9852 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
9853 				   vnic_id, rc);
9854 		}
9855 	}
9856 	return rc;
9857 }
9858 
9859 static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
9860 {
9861 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
9862 		return __bnxt_setup_vnic_p5(bp, vnic_id);
9863 	else
9864 		return __bnxt_setup_vnic(bp, vnic_id);
9865 }
9866 
9867 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp, u16 vnic_id,
9868 				     u16 start_rx_ring_idx, int rx_rings)
9869 {
9870 	int rc;
9871 
9872 	rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, start_rx_ring_idx, rx_rings);
9873 	if (rc) {
9874 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
9875 			   vnic_id, rc);
9876 		return rc;
9877 	}
9878 	return bnxt_setup_vnic(bp, vnic_id);
9879 }
9880 
9881 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
9882 {
9883 	int i, rc = 0;
9884 
9885 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
9886 		return bnxt_alloc_and_setup_vnic(bp, BNXT_VNIC_NTUPLE, 0,
9887 						 bp->rx_nr_rings);
9888 
9889 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
9890 		return 0;
9891 
9892 	for (i = 0; i < bp->rx_nr_rings; i++) {
9893 		struct bnxt_vnic_info *vnic;
9894 		u16 vnic_id = i + 1;
9895 		u16 ring_id = i;
9896 
9897 		if (vnic_id >= bp->nr_vnics)
9898 			break;
9899 
9900 		vnic = &bp->vnic_info[vnic_id];
9901 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
9902 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
9903 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
9904 		if (bnxt_alloc_and_setup_vnic(bp, vnic_id, ring_id, 1))
9905 			break;
9906 	}
9907 	return rc;
9908 }
9909 
9910 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
9911 static bool bnxt_promisc_ok(struct bnxt *bp)
9912 {
9913 #ifdef CONFIG_BNXT_SRIOV
9914 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
9915 		return false;
9916 #endif
9917 	return true;
9918 }
9919 
9920 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
9921 {
9922 	unsigned int rc = 0;
9923 
9924 	rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
9925 	if (rc) {
9926 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
9927 			   rc);
9928 		return rc;
9929 	}
9930 
9931 	rc = bnxt_hwrm_vnic_cfg(bp, 1);
9932 	if (rc) {
9933 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
9934 			   rc);
9935 		return rc;
9936 	}
9937 	return rc;
9938 }
9939 
9940 static int bnxt_cfg_rx_mode(struct bnxt *);
9941 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
9942 
9943 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
9944 {
9945 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
9946 	int rc = 0;
9947 	unsigned int rx_nr_rings = bp->rx_nr_rings;
9948 
9949 	if (irq_re_init) {
9950 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
9951 		if (rc) {
9952 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
9953 				   rc);
9954 			goto err_out;
9955 		}
9956 	}
9957 
9958 	rc = bnxt_hwrm_ring_alloc(bp);
9959 	if (rc) {
9960 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
9961 		goto err_out;
9962 	}
9963 
9964 	rc = bnxt_hwrm_ring_grp_alloc(bp);
9965 	if (rc) {
9966 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
9967 		goto err_out;
9968 	}
9969 
9970 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
9971 		rx_nr_rings--;
9972 
9973 	/* default vnic 0 */
9974 	rc = bnxt_hwrm_vnic_alloc(bp, BNXT_VNIC_DEFAULT, 0, rx_nr_rings);
9975 	if (rc) {
9976 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
9977 		goto err_out;
9978 	}
9979 
9980 	if (BNXT_VF(bp))
9981 		bnxt_hwrm_func_qcfg(bp);
9982 
9983 	rc = bnxt_setup_vnic(bp, BNXT_VNIC_DEFAULT);
9984 	if (rc)
9985 		goto err_out;
9986 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
9987 		bnxt_hwrm_update_rss_hash_cfg(bp);
9988 
9989 	if (bp->flags & BNXT_FLAG_RFS) {
9990 		rc = bnxt_alloc_rfs_vnics(bp);
9991 		if (rc)
9992 			goto err_out;
9993 	}
9994 
9995 	if (bp->flags & BNXT_FLAG_TPA) {
9996 		rc = bnxt_set_tpa(bp, true);
9997 		if (rc)
9998 			goto err_out;
9999 	}
10000 
10001 	if (BNXT_VF(bp))
10002 		bnxt_update_vf_mac(bp);
10003 
10004 	/* Filter for default vnic 0 */
10005 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
10006 	if (rc) {
10007 		if (BNXT_VF(bp) && rc == -ENODEV)
10008 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
10009 		else
10010 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
10011 		goto err_out;
10012 	}
10013 	vnic->uc_filter_count = 1;
10014 
10015 	vnic->rx_mask = 0;
10016 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
10017 		goto skip_rx_mask;
10018 
10019 	if (bp->dev->flags & IFF_BROADCAST)
10020 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10021 
10022 	if (bp->dev->flags & IFF_PROMISC)
10023 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10024 
10025 	if (bp->dev->flags & IFF_ALLMULTI) {
10026 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10027 		vnic->mc_list_count = 0;
10028 	} else if (bp->dev->flags & IFF_MULTICAST) {
10029 		u32 mask = 0;
10030 
10031 		bnxt_mc_list_updated(bp, &mask);
10032 		vnic->rx_mask |= mask;
10033 	}
10034 
10035 	rc = bnxt_cfg_rx_mode(bp);
10036 	if (rc)
10037 		goto err_out;
10038 
10039 skip_rx_mask:
10040 	rc = bnxt_hwrm_set_coal(bp);
10041 	if (rc)
10042 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
10043 				rc);
10044 
10045 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10046 		rc = bnxt_setup_nitroa0_vnic(bp);
10047 		if (rc)
10048 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
10049 				   rc);
10050 	}
10051 
10052 	if (BNXT_VF(bp)) {
10053 		bnxt_hwrm_func_qcfg(bp);
10054 		netdev_update_features(bp->dev);
10055 	}
10056 
10057 	return 0;
10058 
10059 err_out:
10060 	bnxt_hwrm_resource_free(bp, 0, true);
10061 
10062 	return rc;
10063 }
10064 
10065 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
10066 {
10067 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
10068 	return 0;
10069 }
10070 
10071 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
10072 {
10073 	bnxt_init_cp_rings(bp);
10074 	bnxt_init_rx_rings(bp);
10075 	bnxt_init_tx_rings(bp);
10076 	bnxt_init_ring_grps(bp, irq_re_init);
10077 	bnxt_init_vnics(bp);
10078 
10079 	return bnxt_init_chip(bp, irq_re_init);
10080 }
10081 
10082 static int bnxt_set_real_num_queues(struct bnxt *bp)
10083 {
10084 	int rc;
10085 	struct net_device *dev = bp->dev;
10086 
10087 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
10088 					  bp->tx_nr_rings_xdp);
10089 	if (rc)
10090 		return rc;
10091 
10092 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
10093 	if (rc)
10094 		return rc;
10095 
10096 #ifdef CONFIG_RFS_ACCEL
10097 	if (bp->flags & BNXT_FLAG_RFS)
10098 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
10099 #endif
10100 
10101 	return rc;
10102 }
10103 
10104 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10105 			     bool shared)
10106 {
10107 	int _rx = *rx, _tx = *tx;
10108 
10109 	if (shared) {
10110 		*rx = min_t(int, _rx, max);
10111 		*tx = min_t(int, _tx, max);
10112 	} else {
10113 		if (max < 2)
10114 			return -ENOMEM;
10115 
10116 		while (_rx + _tx > max) {
10117 			if (_rx > _tx && _rx > 1)
10118 				_rx--;
10119 			else if (_tx > 1)
10120 				_tx--;
10121 		}
10122 		*rx = _rx;
10123 		*tx = _tx;
10124 	}
10125 	return 0;
10126 }
10127 
10128 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
10129 {
10130 	return (tx - tx_xdp) / tx_sets + tx_xdp;
10131 }
10132 
10133 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
10134 {
10135 	int tcs = bp->num_tc;
10136 
10137 	if (!tcs)
10138 		tcs = 1;
10139 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
10140 }
10141 
10142 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
10143 {
10144 	int tcs = bp->num_tc;
10145 
10146 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
10147 	       bp->tx_nr_rings_xdp;
10148 }
10149 
10150 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10151 			   bool sh)
10152 {
10153 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
10154 
10155 	if (tx_cp != *tx) {
10156 		int tx_saved = tx_cp, rc;
10157 
10158 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
10159 		if (rc)
10160 			return rc;
10161 		if (tx_cp != tx_saved)
10162 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
10163 		return 0;
10164 	}
10165 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
10166 }
10167 
10168 static void bnxt_setup_msix(struct bnxt *bp)
10169 {
10170 	const int len = sizeof(bp->irq_tbl[0].name);
10171 	struct net_device *dev = bp->dev;
10172 	int tcs, i;
10173 
10174 	tcs = bp->num_tc;
10175 	if (tcs) {
10176 		int i, off, count;
10177 
10178 		for (i = 0; i < tcs; i++) {
10179 			count = bp->tx_nr_rings_per_tc;
10180 			off = BNXT_TC_TO_RING_BASE(bp, i);
10181 			netdev_set_tc_queue(dev, i, count, off);
10182 		}
10183 	}
10184 
10185 	for (i = 0; i < bp->cp_nr_rings; i++) {
10186 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10187 		char *attr;
10188 
10189 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
10190 			attr = "TxRx";
10191 		else if (i < bp->rx_nr_rings)
10192 			attr = "rx";
10193 		else
10194 			attr = "tx";
10195 
10196 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
10197 			 attr, i);
10198 		bp->irq_tbl[map_idx].handler = bnxt_msix;
10199 	}
10200 }
10201 
10202 static void bnxt_setup_inta(struct bnxt *bp)
10203 {
10204 	const int len = sizeof(bp->irq_tbl[0].name);
10205 
10206 	if (bp->num_tc) {
10207 		netdev_reset_tc(bp->dev);
10208 		bp->num_tc = 0;
10209 	}
10210 
10211 	snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
10212 		 0);
10213 	bp->irq_tbl[0].handler = bnxt_inta;
10214 }
10215 
10216 static int bnxt_init_int_mode(struct bnxt *bp);
10217 
10218 static int bnxt_setup_int_mode(struct bnxt *bp)
10219 {
10220 	int rc;
10221 
10222 	if (!bp->irq_tbl) {
10223 		rc = bnxt_init_int_mode(bp);
10224 		if (rc || !bp->irq_tbl)
10225 			return rc ?: -ENODEV;
10226 	}
10227 
10228 	if (bp->flags & BNXT_FLAG_USING_MSIX)
10229 		bnxt_setup_msix(bp);
10230 	else
10231 		bnxt_setup_inta(bp);
10232 
10233 	rc = bnxt_set_real_num_queues(bp);
10234 	return rc;
10235 }
10236 
10237 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
10238 {
10239 	return bp->hw_resc.max_rsscos_ctxs;
10240 }
10241 
10242 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
10243 {
10244 	return bp->hw_resc.max_vnics;
10245 }
10246 
10247 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
10248 {
10249 	return bp->hw_resc.max_stat_ctxs;
10250 }
10251 
10252 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
10253 {
10254 	return bp->hw_resc.max_cp_rings;
10255 }
10256 
10257 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
10258 {
10259 	unsigned int cp = bp->hw_resc.max_cp_rings;
10260 
10261 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
10262 		cp -= bnxt_get_ulp_msix_num(bp);
10263 
10264 	return cp;
10265 }
10266 
10267 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
10268 {
10269 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
10270 
10271 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10272 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
10273 
10274 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
10275 }
10276 
10277 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
10278 {
10279 	bp->hw_resc.max_irqs = max_irqs;
10280 }
10281 
10282 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
10283 {
10284 	unsigned int cp;
10285 
10286 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
10287 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10288 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
10289 	else
10290 		return cp - bp->cp_nr_rings;
10291 }
10292 
10293 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
10294 {
10295 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
10296 }
10297 
10298 int bnxt_get_avail_msix(struct bnxt *bp, int num)
10299 {
10300 	int max_cp = bnxt_get_max_func_cp_rings(bp);
10301 	int max_irq = bnxt_get_max_func_irqs(bp);
10302 	int total_req = bp->cp_nr_rings + num;
10303 	int max_idx, avail_msix;
10304 
10305 	max_idx = bp->total_irqs;
10306 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
10307 		max_idx = min_t(int, bp->total_irqs, max_cp);
10308 	avail_msix = max_idx - bp->cp_nr_rings;
10309 	if (!BNXT_NEW_RM(bp) || avail_msix >= num)
10310 		return avail_msix;
10311 
10312 	if (max_irq < total_req) {
10313 		num = max_irq - bp->cp_nr_rings;
10314 		if (num <= 0)
10315 			return 0;
10316 	}
10317 	return num;
10318 }
10319 
10320 static int bnxt_get_num_msix(struct bnxt *bp)
10321 {
10322 	if (!BNXT_NEW_RM(bp))
10323 		return bnxt_get_max_func_irqs(bp);
10324 
10325 	return bnxt_nq_rings_in_use(bp);
10326 }
10327 
10328 static int bnxt_init_msix(struct bnxt *bp)
10329 {
10330 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp;
10331 	struct msix_entry *msix_ent;
10332 
10333 	total_vecs = bnxt_get_num_msix(bp);
10334 	max = bnxt_get_max_func_irqs(bp);
10335 	if (total_vecs > max)
10336 		total_vecs = max;
10337 
10338 	if (!total_vecs)
10339 		return 0;
10340 
10341 	msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
10342 	if (!msix_ent)
10343 		return -ENOMEM;
10344 
10345 	for (i = 0; i < total_vecs; i++) {
10346 		msix_ent[i].entry = i;
10347 		msix_ent[i].vector = 0;
10348 	}
10349 
10350 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
10351 		min = 2;
10352 
10353 	total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
10354 	ulp_msix = bnxt_get_ulp_msix_num(bp);
10355 	if (total_vecs < 0 || total_vecs < ulp_msix) {
10356 		rc = -ENODEV;
10357 		goto msix_setup_exit;
10358 	}
10359 
10360 	bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
10361 	if (bp->irq_tbl) {
10362 		for (i = 0; i < total_vecs; i++)
10363 			bp->irq_tbl[i].vector = msix_ent[i].vector;
10364 
10365 		bp->total_irqs = total_vecs;
10366 		/* Trim rings based upon num of vectors allocated */
10367 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
10368 				     total_vecs - ulp_msix, min == 1);
10369 		if (rc)
10370 			goto msix_setup_exit;
10371 
10372 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
10373 		bp->cp_nr_rings = (min == 1) ?
10374 				  max_t(int, tx_cp, bp->rx_nr_rings) :
10375 				  tx_cp + bp->rx_nr_rings;
10376 
10377 	} else {
10378 		rc = -ENOMEM;
10379 		goto msix_setup_exit;
10380 	}
10381 	bp->flags |= BNXT_FLAG_USING_MSIX;
10382 	kfree(msix_ent);
10383 	return 0;
10384 
10385 msix_setup_exit:
10386 	netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
10387 	kfree(bp->irq_tbl);
10388 	bp->irq_tbl = NULL;
10389 	pci_disable_msix(bp->pdev);
10390 	kfree(msix_ent);
10391 	return rc;
10392 }
10393 
10394 static int bnxt_init_inta(struct bnxt *bp)
10395 {
10396 	bp->irq_tbl = kzalloc(sizeof(struct bnxt_irq), GFP_KERNEL);
10397 	if (!bp->irq_tbl)
10398 		return -ENOMEM;
10399 
10400 	bp->total_irqs = 1;
10401 	bp->rx_nr_rings = 1;
10402 	bp->tx_nr_rings = 1;
10403 	bp->cp_nr_rings = 1;
10404 	bp->flags |= BNXT_FLAG_SHARED_RINGS;
10405 	bp->irq_tbl[0].vector = bp->pdev->irq;
10406 	return 0;
10407 }
10408 
10409 static int bnxt_init_int_mode(struct bnxt *bp)
10410 {
10411 	int rc = -ENODEV;
10412 
10413 	if (bp->flags & BNXT_FLAG_MSIX_CAP)
10414 		rc = bnxt_init_msix(bp);
10415 
10416 	if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
10417 		/* fallback to INTA */
10418 		rc = bnxt_init_inta(bp);
10419 	}
10420 	return rc;
10421 }
10422 
10423 static void bnxt_clear_int_mode(struct bnxt *bp)
10424 {
10425 	if (bp->flags & BNXT_FLAG_USING_MSIX)
10426 		pci_disable_msix(bp->pdev);
10427 
10428 	kfree(bp->irq_tbl);
10429 	bp->irq_tbl = NULL;
10430 	bp->flags &= ~BNXT_FLAG_USING_MSIX;
10431 }
10432 
10433 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
10434 {
10435 	bool irq_cleared = false;
10436 	int tcs = bp->num_tc;
10437 	int rc;
10438 
10439 	if (!bnxt_need_reserve_rings(bp))
10440 		return 0;
10441 
10442 	if (irq_re_init && BNXT_NEW_RM(bp) &&
10443 	    bnxt_get_num_msix(bp) != bp->total_irqs) {
10444 		bnxt_ulp_irq_stop(bp);
10445 		bnxt_clear_int_mode(bp);
10446 		irq_cleared = true;
10447 	}
10448 	rc = __bnxt_reserve_rings(bp);
10449 	if (irq_cleared) {
10450 		if (!rc)
10451 			rc = bnxt_init_int_mode(bp);
10452 		bnxt_ulp_irq_restart(bp, rc);
10453 	}
10454 	if (rc) {
10455 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
10456 		return rc;
10457 	}
10458 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
10459 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
10460 		netdev_err(bp->dev, "tx ring reservation failure\n");
10461 		netdev_reset_tc(bp->dev);
10462 		bp->num_tc = 0;
10463 		if (bp->tx_nr_rings_xdp)
10464 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
10465 		else
10466 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
10467 		return -ENOMEM;
10468 	}
10469 	return 0;
10470 }
10471 
10472 static void bnxt_free_irq(struct bnxt *bp)
10473 {
10474 	struct bnxt_irq *irq;
10475 	int i;
10476 
10477 #ifdef CONFIG_RFS_ACCEL
10478 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
10479 	bp->dev->rx_cpu_rmap = NULL;
10480 #endif
10481 	if (!bp->irq_tbl || !bp->bnapi)
10482 		return;
10483 
10484 	for (i = 0; i < bp->cp_nr_rings; i++) {
10485 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10486 
10487 		irq = &bp->irq_tbl[map_idx];
10488 		if (irq->requested) {
10489 			if (irq->have_cpumask) {
10490 				irq_set_affinity_hint(irq->vector, NULL);
10491 				free_cpumask_var(irq->cpu_mask);
10492 				irq->have_cpumask = 0;
10493 			}
10494 			free_irq(irq->vector, bp->bnapi[i]);
10495 		}
10496 
10497 		irq->requested = 0;
10498 	}
10499 }
10500 
10501 static int bnxt_request_irq(struct bnxt *bp)
10502 {
10503 	int i, j, rc = 0;
10504 	unsigned long flags = 0;
10505 #ifdef CONFIG_RFS_ACCEL
10506 	struct cpu_rmap *rmap;
10507 #endif
10508 
10509 	rc = bnxt_setup_int_mode(bp);
10510 	if (rc) {
10511 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
10512 			   rc);
10513 		return rc;
10514 	}
10515 #ifdef CONFIG_RFS_ACCEL
10516 	rmap = bp->dev->rx_cpu_rmap;
10517 #endif
10518 	if (!(bp->flags & BNXT_FLAG_USING_MSIX))
10519 		flags = IRQF_SHARED;
10520 
10521 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
10522 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10523 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
10524 
10525 #ifdef CONFIG_RFS_ACCEL
10526 		if (rmap && bp->bnapi[i]->rx_ring) {
10527 			rc = irq_cpu_rmap_add(rmap, irq->vector);
10528 			if (rc)
10529 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
10530 					    j);
10531 			j++;
10532 		}
10533 #endif
10534 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
10535 				 bp->bnapi[i]);
10536 		if (rc)
10537 			break;
10538 
10539 		netif_napi_set_irq(&bp->bnapi[i]->napi, irq->vector);
10540 		irq->requested = 1;
10541 
10542 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
10543 			int numa_node = dev_to_node(&bp->pdev->dev);
10544 
10545 			irq->have_cpumask = 1;
10546 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
10547 					irq->cpu_mask);
10548 			rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
10549 			if (rc) {
10550 				netdev_warn(bp->dev,
10551 					    "Set affinity failed, IRQ = %d\n",
10552 					    irq->vector);
10553 				break;
10554 			}
10555 		}
10556 	}
10557 	return rc;
10558 }
10559 
10560 static void bnxt_del_napi(struct bnxt *bp)
10561 {
10562 	int i;
10563 
10564 	if (!bp->bnapi)
10565 		return;
10566 
10567 	for (i = 0; i < bp->rx_nr_rings; i++)
10568 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
10569 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
10570 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
10571 
10572 	for (i = 0; i < bp->cp_nr_rings; i++) {
10573 		struct bnxt_napi *bnapi = bp->bnapi[i];
10574 
10575 		__netif_napi_del(&bnapi->napi);
10576 	}
10577 	/* We called __netif_napi_del(), we need
10578 	 * to respect an RCU grace period before freeing napi structures.
10579 	 */
10580 	synchronize_net();
10581 }
10582 
10583 static void bnxt_init_napi(struct bnxt *bp)
10584 {
10585 	int i;
10586 	unsigned int cp_nr_rings = bp->cp_nr_rings;
10587 	struct bnxt_napi *bnapi;
10588 
10589 	if (bp->flags & BNXT_FLAG_USING_MSIX) {
10590 		int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
10591 
10592 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10593 			poll_fn = bnxt_poll_p5;
10594 		else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10595 			cp_nr_rings--;
10596 		for (i = 0; i < cp_nr_rings; i++) {
10597 			bnapi = bp->bnapi[i];
10598 			netif_napi_add(bp->dev, &bnapi->napi, poll_fn);
10599 		}
10600 		if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10601 			bnapi = bp->bnapi[cp_nr_rings];
10602 			netif_napi_add(bp->dev, &bnapi->napi,
10603 				       bnxt_poll_nitroa0);
10604 		}
10605 	} else {
10606 		bnapi = bp->bnapi[0];
10607 		netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll);
10608 	}
10609 }
10610 
10611 static void bnxt_disable_napi(struct bnxt *bp)
10612 {
10613 	int i;
10614 
10615 	if (!bp->bnapi ||
10616 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
10617 		return;
10618 
10619 	for (i = 0; i < bp->cp_nr_rings; i++) {
10620 		struct bnxt_napi *bnapi = bp->bnapi[i];
10621 		struct bnxt_cp_ring_info *cpr;
10622 
10623 		cpr = &bnapi->cp_ring;
10624 		if (bnapi->tx_fault)
10625 			cpr->sw_stats.tx.tx_resets++;
10626 		if (bnapi->in_reset)
10627 			cpr->sw_stats.rx.rx_resets++;
10628 		napi_disable(&bnapi->napi);
10629 		if (bnapi->rx_ring)
10630 			cancel_work_sync(&cpr->dim.work);
10631 	}
10632 }
10633 
10634 static void bnxt_enable_napi(struct bnxt *bp)
10635 {
10636 	int i;
10637 
10638 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
10639 	for (i = 0; i < bp->cp_nr_rings; i++) {
10640 		struct bnxt_napi *bnapi = bp->bnapi[i];
10641 		struct bnxt_cp_ring_info *cpr;
10642 
10643 		bnapi->tx_fault = 0;
10644 
10645 		cpr = &bnapi->cp_ring;
10646 		bnapi->in_reset = false;
10647 
10648 		if (bnapi->rx_ring) {
10649 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
10650 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
10651 		}
10652 		napi_enable(&bnapi->napi);
10653 	}
10654 }
10655 
10656 void bnxt_tx_disable(struct bnxt *bp)
10657 {
10658 	int i;
10659 	struct bnxt_tx_ring_info *txr;
10660 
10661 	if (bp->tx_ring) {
10662 		for (i = 0; i < bp->tx_nr_rings; i++) {
10663 			txr = &bp->tx_ring[i];
10664 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
10665 		}
10666 	}
10667 	/* Make sure napi polls see @dev_state change */
10668 	synchronize_net();
10669 	/* Drop carrier first to prevent TX timeout */
10670 	netif_carrier_off(bp->dev);
10671 	/* Stop all TX queues */
10672 	netif_tx_disable(bp->dev);
10673 }
10674 
10675 void bnxt_tx_enable(struct bnxt *bp)
10676 {
10677 	int i;
10678 	struct bnxt_tx_ring_info *txr;
10679 
10680 	for (i = 0; i < bp->tx_nr_rings; i++) {
10681 		txr = &bp->tx_ring[i];
10682 		WRITE_ONCE(txr->dev_state, 0);
10683 	}
10684 	/* Make sure napi polls see @dev_state change */
10685 	synchronize_net();
10686 	netif_tx_wake_all_queues(bp->dev);
10687 	if (BNXT_LINK_IS_UP(bp))
10688 		netif_carrier_on(bp->dev);
10689 }
10690 
10691 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
10692 {
10693 	u8 active_fec = link_info->active_fec_sig_mode &
10694 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
10695 
10696 	switch (active_fec) {
10697 	default:
10698 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
10699 		return "None";
10700 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
10701 		return "Clause 74 BaseR";
10702 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
10703 		return "Clause 91 RS(528,514)";
10704 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
10705 		return "Clause 91 RS544_1XN";
10706 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
10707 		return "Clause 91 RS(544,514)";
10708 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
10709 		return "Clause 91 RS272_1XN";
10710 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
10711 		return "Clause 91 RS(272,257)";
10712 	}
10713 }
10714 
10715 void bnxt_report_link(struct bnxt *bp)
10716 {
10717 	if (BNXT_LINK_IS_UP(bp)) {
10718 		const char *signal = "";
10719 		const char *flow_ctrl;
10720 		const char *duplex;
10721 		u32 speed;
10722 		u16 fec;
10723 
10724 		netif_carrier_on(bp->dev);
10725 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
10726 		if (speed == SPEED_UNKNOWN) {
10727 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
10728 			return;
10729 		}
10730 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
10731 			duplex = "full";
10732 		else
10733 			duplex = "half";
10734 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
10735 			flow_ctrl = "ON - receive & transmit";
10736 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
10737 			flow_ctrl = "ON - transmit";
10738 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
10739 			flow_ctrl = "ON - receive";
10740 		else
10741 			flow_ctrl = "none";
10742 		if (bp->link_info.phy_qcfg_resp.option_flags &
10743 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
10744 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
10745 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
10746 			switch (sig_mode) {
10747 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
10748 				signal = "(NRZ) ";
10749 				break;
10750 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
10751 				signal = "(PAM4 56Gbps) ";
10752 				break;
10753 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
10754 				signal = "(PAM4 112Gbps) ";
10755 				break;
10756 			default:
10757 				break;
10758 			}
10759 		}
10760 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
10761 			    speed, signal, duplex, flow_ctrl);
10762 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
10763 			netdev_info(bp->dev, "EEE is %s\n",
10764 				    bp->eee.eee_active ? "active" :
10765 							 "not active");
10766 		fec = bp->link_info.fec_cfg;
10767 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
10768 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
10769 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
10770 				    bnxt_report_fec(&bp->link_info));
10771 	} else {
10772 		netif_carrier_off(bp->dev);
10773 		netdev_err(bp->dev, "NIC Link is Down\n");
10774 	}
10775 }
10776 
10777 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
10778 {
10779 	if (!resp->supported_speeds_auto_mode &&
10780 	    !resp->supported_speeds_force_mode &&
10781 	    !resp->supported_pam4_speeds_auto_mode &&
10782 	    !resp->supported_pam4_speeds_force_mode &&
10783 	    !resp->supported_speeds2_auto_mode &&
10784 	    !resp->supported_speeds2_force_mode)
10785 		return true;
10786 	return false;
10787 }
10788 
10789 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
10790 {
10791 	struct bnxt_link_info *link_info = &bp->link_info;
10792 	struct hwrm_port_phy_qcaps_output *resp;
10793 	struct hwrm_port_phy_qcaps_input *req;
10794 	int rc = 0;
10795 
10796 	if (bp->hwrm_spec_code < 0x10201)
10797 		return 0;
10798 
10799 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
10800 	if (rc)
10801 		return rc;
10802 
10803 	resp = hwrm_req_hold(bp, req);
10804 	rc = hwrm_req_send(bp, req);
10805 	if (rc)
10806 		goto hwrm_phy_qcaps_exit;
10807 
10808 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
10809 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
10810 		struct ethtool_keee *eee = &bp->eee;
10811 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
10812 
10813 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
10814 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
10815 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
10816 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
10817 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
10818 	}
10819 
10820 	if (bp->hwrm_spec_code >= 0x10a01) {
10821 		if (bnxt_phy_qcaps_no_speed(resp)) {
10822 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
10823 			netdev_warn(bp->dev, "Ethernet link disabled\n");
10824 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
10825 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
10826 			netdev_info(bp->dev, "Ethernet link enabled\n");
10827 			/* Phy re-enabled, reprobe the speeds */
10828 			link_info->support_auto_speeds = 0;
10829 			link_info->support_pam4_auto_speeds = 0;
10830 			link_info->support_auto_speeds2 = 0;
10831 		}
10832 	}
10833 	if (resp->supported_speeds_auto_mode)
10834 		link_info->support_auto_speeds =
10835 			le16_to_cpu(resp->supported_speeds_auto_mode);
10836 	if (resp->supported_pam4_speeds_auto_mode)
10837 		link_info->support_pam4_auto_speeds =
10838 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
10839 	if (resp->supported_speeds2_auto_mode)
10840 		link_info->support_auto_speeds2 =
10841 			le16_to_cpu(resp->supported_speeds2_auto_mode);
10842 
10843 	bp->port_count = resp->port_cnt;
10844 
10845 hwrm_phy_qcaps_exit:
10846 	hwrm_req_drop(bp, req);
10847 	return rc;
10848 }
10849 
10850 static bool bnxt_support_dropped(u16 advertising, u16 supported)
10851 {
10852 	u16 diff = advertising ^ supported;
10853 
10854 	return ((supported | diff) != supported);
10855 }
10856 
10857 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
10858 {
10859 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
10860 
10861 	/* Check if any advertised speeds are no longer supported. The caller
10862 	 * holds the link_lock mutex, so we can modify link_info settings.
10863 	 */
10864 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
10865 		if (bnxt_support_dropped(link_info->advertising,
10866 					 link_info->support_auto_speeds2)) {
10867 			link_info->advertising = link_info->support_auto_speeds2;
10868 			return true;
10869 		}
10870 		return false;
10871 	}
10872 	if (bnxt_support_dropped(link_info->advertising,
10873 				 link_info->support_auto_speeds)) {
10874 		link_info->advertising = link_info->support_auto_speeds;
10875 		return true;
10876 	}
10877 	if (bnxt_support_dropped(link_info->advertising_pam4,
10878 				 link_info->support_pam4_auto_speeds)) {
10879 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
10880 		return true;
10881 	}
10882 	return false;
10883 }
10884 
10885 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
10886 {
10887 	struct bnxt_link_info *link_info = &bp->link_info;
10888 	struct hwrm_port_phy_qcfg_output *resp;
10889 	struct hwrm_port_phy_qcfg_input *req;
10890 	u8 link_state = link_info->link_state;
10891 	bool support_changed;
10892 	int rc;
10893 
10894 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
10895 	if (rc)
10896 		return rc;
10897 
10898 	resp = hwrm_req_hold(bp, req);
10899 	rc = hwrm_req_send(bp, req);
10900 	if (rc) {
10901 		hwrm_req_drop(bp, req);
10902 		if (BNXT_VF(bp) && rc == -ENODEV) {
10903 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
10904 			rc = 0;
10905 		}
10906 		return rc;
10907 	}
10908 
10909 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
10910 	link_info->phy_link_status = resp->link;
10911 	link_info->duplex = resp->duplex_cfg;
10912 	if (bp->hwrm_spec_code >= 0x10800)
10913 		link_info->duplex = resp->duplex_state;
10914 	link_info->pause = resp->pause;
10915 	link_info->auto_mode = resp->auto_mode;
10916 	link_info->auto_pause_setting = resp->auto_pause;
10917 	link_info->lp_pause = resp->link_partner_adv_pause;
10918 	link_info->force_pause_setting = resp->force_pause;
10919 	link_info->duplex_setting = resp->duplex_cfg;
10920 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
10921 		link_info->link_speed = le16_to_cpu(resp->link_speed);
10922 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
10923 			link_info->active_lanes = resp->active_lanes;
10924 	} else {
10925 		link_info->link_speed = 0;
10926 		link_info->active_lanes = 0;
10927 	}
10928 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
10929 	link_info->force_pam4_link_speed =
10930 		le16_to_cpu(resp->force_pam4_link_speed);
10931 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
10932 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
10933 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
10934 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
10935 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
10936 	link_info->auto_pam4_link_speeds =
10937 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
10938 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
10939 	link_info->lp_auto_link_speeds =
10940 		le16_to_cpu(resp->link_partner_adv_speeds);
10941 	link_info->lp_auto_pam4_link_speeds =
10942 		resp->link_partner_pam4_adv_speeds;
10943 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
10944 	link_info->phy_ver[0] = resp->phy_maj;
10945 	link_info->phy_ver[1] = resp->phy_min;
10946 	link_info->phy_ver[2] = resp->phy_bld;
10947 	link_info->media_type = resp->media_type;
10948 	link_info->phy_type = resp->phy_type;
10949 	link_info->transceiver = resp->xcvr_pkg_type;
10950 	link_info->phy_addr = resp->eee_config_phy_addr &
10951 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
10952 	link_info->module_status = resp->module_status;
10953 
10954 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
10955 		struct ethtool_keee *eee = &bp->eee;
10956 		u16 fw_speeds;
10957 
10958 		eee->eee_active = 0;
10959 		if (resp->eee_config_phy_addr &
10960 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
10961 			eee->eee_active = 1;
10962 			fw_speeds = le16_to_cpu(
10963 				resp->link_partner_adv_eee_link_speed_mask);
10964 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
10965 		}
10966 
10967 		/* Pull initial EEE config */
10968 		if (!chng_link_state) {
10969 			if (resp->eee_config_phy_addr &
10970 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
10971 				eee->eee_enabled = 1;
10972 
10973 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
10974 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
10975 
10976 			if (resp->eee_config_phy_addr &
10977 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
10978 				__le32 tmr;
10979 
10980 				eee->tx_lpi_enabled = 1;
10981 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
10982 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
10983 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
10984 			}
10985 		}
10986 	}
10987 
10988 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
10989 	if (bp->hwrm_spec_code >= 0x10504) {
10990 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
10991 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
10992 	}
10993 	/* TODO: need to add more logic to report VF link */
10994 	if (chng_link_state) {
10995 		if (link_info->phy_link_status == BNXT_LINK_LINK)
10996 			link_info->link_state = BNXT_LINK_STATE_UP;
10997 		else
10998 			link_info->link_state = BNXT_LINK_STATE_DOWN;
10999 		if (link_state != link_info->link_state)
11000 			bnxt_report_link(bp);
11001 	} else {
11002 		/* always link down if not require to update link state */
11003 		link_info->link_state = BNXT_LINK_STATE_DOWN;
11004 	}
11005 	hwrm_req_drop(bp, req);
11006 
11007 	if (!BNXT_PHY_CFG_ABLE(bp))
11008 		return 0;
11009 
11010 	support_changed = bnxt_support_speed_dropped(link_info);
11011 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
11012 		bnxt_hwrm_set_link_setting(bp, true, false);
11013 	return 0;
11014 }
11015 
11016 static void bnxt_get_port_module_status(struct bnxt *bp)
11017 {
11018 	struct bnxt_link_info *link_info = &bp->link_info;
11019 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
11020 	u8 module_status;
11021 
11022 	if (bnxt_update_link(bp, true))
11023 		return;
11024 
11025 	module_status = link_info->module_status;
11026 	switch (module_status) {
11027 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
11028 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
11029 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
11030 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
11031 			    bp->pf.port_id);
11032 		if (bp->hwrm_spec_code >= 0x10201) {
11033 			netdev_warn(bp->dev, "Module part number %s\n",
11034 				    resp->phy_vendor_partnumber);
11035 		}
11036 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
11037 			netdev_warn(bp->dev, "TX is disabled\n");
11038 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
11039 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
11040 	}
11041 }
11042 
11043 static void
11044 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11045 {
11046 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
11047 		if (bp->hwrm_spec_code >= 0x10201)
11048 			req->auto_pause =
11049 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
11050 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11051 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
11052 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11053 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
11054 		req->enables |=
11055 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11056 	} else {
11057 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11058 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
11059 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11060 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
11061 		req->enables |=
11062 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
11063 		if (bp->hwrm_spec_code >= 0x10201) {
11064 			req->auto_pause = req->force_pause;
11065 			req->enables |= cpu_to_le32(
11066 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11067 		}
11068 	}
11069 }
11070 
11071 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11072 {
11073 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
11074 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
11075 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11076 			req->enables |=
11077 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
11078 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
11079 		} else if (bp->link_info.advertising) {
11080 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
11081 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
11082 		}
11083 		if (bp->link_info.advertising_pam4) {
11084 			req->enables |=
11085 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
11086 			req->auto_link_pam4_speed_mask =
11087 				cpu_to_le16(bp->link_info.advertising_pam4);
11088 		}
11089 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
11090 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
11091 	} else {
11092 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
11093 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11094 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
11095 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
11096 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
11097 				   (u32)bp->link_info.req_link_speed);
11098 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
11099 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11100 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
11101 		} else {
11102 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11103 		}
11104 	}
11105 
11106 	/* tell chimp that the setting takes effect immediately */
11107 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
11108 }
11109 
11110 int bnxt_hwrm_set_pause(struct bnxt *bp)
11111 {
11112 	struct hwrm_port_phy_cfg_input *req;
11113 	int rc;
11114 
11115 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11116 	if (rc)
11117 		return rc;
11118 
11119 	bnxt_hwrm_set_pause_common(bp, req);
11120 
11121 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
11122 	    bp->link_info.force_link_chng)
11123 		bnxt_hwrm_set_link_common(bp, req);
11124 
11125 	rc = hwrm_req_send(bp, req);
11126 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
11127 		/* since changing of pause setting doesn't trigger any link
11128 		 * change event, the driver needs to update the current pause
11129 		 * result upon successfully return of the phy_cfg command
11130 		 */
11131 		bp->link_info.pause =
11132 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
11133 		bp->link_info.auto_pause_setting = 0;
11134 		if (!bp->link_info.force_link_chng)
11135 			bnxt_report_link(bp);
11136 	}
11137 	bp->link_info.force_link_chng = false;
11138 	return rc;
11139 }
11140 
11141 static void bnxt_hwrm_set_eee(struct bnxt *bp,
11142 			      struct hwrm_port_phy_cfg_input *req)
11143 {
11144 	struct ethtool_keee *eee = &bp->eee;
11145 
11146 	if (eee->eee_enabled) {
11147 		u16 eee_speeds;
11148 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
11149 
11150 		if (eee->tx_lpi_enabled)
11151 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
11152 		else
11153 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
11154 
11155 		req->flags |= cpu_to_le32(flags);
11156 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
11157 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
11158 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
11159 	} else {
11160 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
11161 	}
11162 }
11163 
11164 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
11165 {
11166 	struct hwrm_port_phy_cfg_input *req;
11167 	int rc;
11168 
11169 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11170 	if (rc)
11171 		return rc;
11172 
11173 	if (set_pause)
11174 		bnxt_hwrm_set_pause_common(bp, req);
11175 
11176 	bnxt_hwrm_set_link_common(bp, req);
11177 
11178 	if (set_eee)
11179 		bnxt_hwrm_set_eee(bp, req);
11180 	return hwrm_req_send(bp, req);
11181 }
11182 
11183 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
11184 {
11185 	struct hwrm_port_phy_cfg_input *req;
11186 	int rc;
11187 
11188 	if (!BNXT_SINGLE_PF(bp))
11189 		return 0;
11190 
11191 	if (pci_num_vf(bp->pdev) &&
11192 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
11193 		return 0;
11194 
11195 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11196 	if (rc)
11197 		return rc;
11198 
11199 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
11200 	rc = hwrm_req_send(bp, req);
11201 	if (!rc) {
11202 		mutex_lock(&bp->link_lock);
11203 		/* Device is not obliged link down in certain scenarios, even
11204 		 * when forced. Setting the state unknown is consistent with
11205 		 * driver startup and will force link state to be reported
11206 		 * during subsequent open based on PORT_PHY_QCFG.
11207 		 */
11208 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
11209 		mutex_unlock(&bp->link_lock);
11210 	}
11211 	return rc;
11212 }
11213 
11214 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
11215 {
11216 #ifdef CONFIG_TEE_BNXT_FW
11217 	int rc = tee_bnxt_fw_load();
11218 
11219 	if (rc)
11220 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
11221 
11222 	return rc;
11223 #else
11224 	netdev_err(bp->dev, "OP-TEE not supported\n");
11225 	return -ENODEV;
11226 #endif
11227 }
11228 
11229 static int bnxt_try_recover_fw(struct bnxt *bp)
11230 {
11231 	if (bp->fw_health && bp->fw_health->status_reliable) {
11232 		int retry = 0, rc;
11233 		u32 sts;
11234 
11235 		do {
11236 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
11237 			rc = bnxt_hwrm_poll(bp);
11238 			if (!BNXT_FW_IS_BOOTING(sts) &&
11239 			    !BNXT_FW_IS_RECOVERING(sts))
11240 				break;
11241 			retry++;
11242 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
11243 
11244 		if (!BNXT_FW_IS_HEALTHY(sts)) {
11245 			netdev_err(bp->dev,
11246 				   "Firmware not responding, status: 0x%x\n",
11247 				   sts);
11248 			rc = -ENODEV;
11249 		}
11250 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
11251 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
11252 			return bnxt_fw_reset_via_optee(bp);
11253 		}
11254 		return rc;
11255 	}
11256 
11257 	return -ENODEV;
11258 }
11259 
11260 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
11261 {
11262 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11263 
11264 	if (!BNXT_NEW_RM(bp))
11265 		return; /* no resource reservations required */
11266 
11267 	hw_resc->resv_cp_rings = 0;
11268 	hw_resc->resv_stat_ctxs = 0;
11269 	hw_resc->resv_irqs = 0;
11270 	hw_resc->resv_tx_rings = 0;
11271 	hw_resc->resv_rx_rings = 0;
11272 	hw_resc->resv_hw_ring_grps = 0;
11273 	hw_resc->resv_vnics = 0;
11274 	hw_resc->resv_rsscos_ctxs = 0;
11275 	if (!fw_reset) {
11276 		bp->tx_nr_rings = 0;
11277 		bp->rx_nr_rings = 0;
11278 	}
11279 }
11280 
11281 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
11282 {
11283 	int rc;
11284 
11285 	if (!BNXT_NEW_RM(bp))
11286 		return 0; /* no resource reservations required */
11287 
11288 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
11289 	if (rc)
11290 		netdev_err(bp->dev, "resc_qcaps failed\n");
11291 
11292 	bnxt_clear_reservations(bp, fw_reset);
11293 
11294 	return rc;
11295 }
11296 
11297 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
11298 {
11299 	struct hwrm_func_drv_if_change_output *resp;
11300 	struct hwrm_func_drv_if_change_input *req;
11301 	bool fw_reset = !bp->irq_tbl;
11302 	bool resc_reinit = false;
11303 	int rc, retry = 0;
11304 	u32 flags = 0;
11305 
11306 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
11307 		return 0;
11308 
11309 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
11310 	if (rc)
11311 		return rc;
11312 
11313 	if (up)
11314 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
11315 	resp = hwrm_req_hold(bp, req);
11316 
11317 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
11318 	while (retry < BNXT_FW_IF_RETRY) {
11319 		rc = hwrm_req_send(bp, req);
11320 		if (rc != -EAGAIN)
11321 			break;
11322 
11323 		msleep(50);
11324 		retry++;
11325 	}
11326 
11327 	if (rc == -EAGAIN) {
11328 		hwrm_req_drop(bp, req);
11329 		return rc;
11330 	} else if (!rc) {
11331 		flags = le32_to_cpu(resp->flags);
11332 	} else if (up) {
11333 		rc = bnxt_try_recover_fw(bp);
11334 		fw_reset = true;
11335 	}
11336 	hwrm_req_drop(bp, req);
11337 	if (rc)
11338 		return rc;
11339 
11340 	if (!up) {
11341 		bnxt_inv_fw_health_reg(bp);
11342 		return 0;
11343 	}
11344 
11345 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
11346 		resc_reinit = true;
11347 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
11348 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
11349 		fw_reset = true;
11350 	else
11351 		bnxt_remap_fw_health_regs(bp);
11352 
11353 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
11354 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
11355 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
11356 		return -ENODEV;
11357 	}
11358 	if (resc_reinit || fw_reset) {
11359 		if (fw_reset) {
11360 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11361 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11362 				bnxt_ulp_stop(bp);
11363 			bnxt_free_ctx_mem(bp);
11364 			bnxt_dcb_free(bp);
11365 			rc = bnxt_fw_init_one(bp);
11366 			if (rc) {
11367 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11368 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
11369 				return rc;
11370 			}
11371 			bnxt_clear_int_mode(bp);
11372 			rc = bnxt_init_int_mode(bp);
11373 			if (rc) {
11374 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11375 				netdev_err(bp->dev, "init int mode failed\n");
11376 				return rc;
11377 			}
11378 		}
11379 		rc = bnxt_cancel_reservations(bp, fw_reset);
11380 	}
11381 	return rc;
11382 }
11383 
11384 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
11385 {
11386 	struct hwrm_port_led_qcaps_output *resp;
11387 	struct hwrm_port_led_qcaps_input *req;
11388 	struct bnxt_pf_info *pf = &bp->pf;
11389 	int rc;
11390 
11391 	bp->num_leds = 0;
11392 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
11393 		return 0;
11394 
11395 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
11396 	if (rc)
11397 		return rc;
11398 
11399 	req->port_id = cpu_to_le16(pf->port_id);
11400 	resp = hwrm_req_hold(bp, req);
11401 	rc = hwrm_req_send(bp, req);
11402 	if (rc) {
11403 		hwrm_req_drop(bp, req);
11404 		return rc;
11405 	}
11406 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
11407 		int i;
11408 
11409 		bp->num_leds = resp->num_leds;
11410 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
11411 						 bp->num_leds);
11412 		for (i = 0; i < bp->num_leds; i++) {
11413 			struct bnxt_led_info *led = &bp->leds[i];
11414 			__le16 caps = led->led_state_caps;
11415 
11416 			if (!led->led_group_id ||
11417 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
11418 				bp->num_leds = 0;
11419 				break;
11420 			}
11421 		}
11422 	}
11423 	hwrm_req_drop(bp, req);
11424 	return 0;
11425 }
11426 
11427 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
11428 {
11429 	struct hwrm_wol_filter_alloc_output *resp;
11430 	struct hwrm_wol_filter_alloc_input *req;
11431 	int rc;
11432 
11433 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
11434 	if (rc)
11435 		return rc;
11436 
11437 	req->port_id = cpu_to_le16(bp->pf.port_id);
11438 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
11439 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
11440 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
11441 
11442 	resp = hwrm_req_hold(bp, req);
11443 	rc = hwrm_req_send(bp, req);
11444 	if (!rc)
11445 		bp->wol_filter_id = resp->wol_filter_id;
11446 	hwrm_req_drop(bp, req);
11447 	return rc;
11448 }
11449 
11450 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
11451 {
11452 	struct hwrm_wol_filter_free_input *req;
11453 	int rc;
11454 
11455 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
11456 	if (rc)
11457 		return rc;
11458 
11459 	req->port_id = cpu_to_le16(bp->pf.port_id);
11460 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
11461 	req->wol_filter_id = bp->wol_filter_id;
11462 
11463 	return hwrm_req_send(bp, req);
11464 }
11465 
11466 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
11467 {
11468 	struct hwrm_wol_filter_qcfg_output *resp;
11469 	struct hwrm_wol_filter_qcfg_input *req;
11470 	u16 next_handle = 0;
11471 	int rc;
11472 
11473 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
11474 	if (rc)
11475 		return rc;
11476 
11477 	req->port_id = cpu_to_le16(bp->pf.port_id);
11478 	req->handle = cpu_to_le16(handle);
11479 	resp = hwrm_req_hold(bp, req);
11480 	rc = hwrm_req_send(bp, req);
11481 	if (!rc) {
11482 		next_handle = le16_to_cpu(resp->next_handle);
11483 		if (next_handle != 0) {
11484 			if (resp->wol_type ==
11485 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
11486 				bp->wol = 1;
11487 				bp->wol_filter_id = resp->wol_filter_id;
11488 			}
11489 		}
11490 	}
11491 	hwrm_req_drop(bp, req);
11492 	return next_handle;
11493 }
11494 
11495 static void bnxt_get_wol_settings(struct bnxt *bp)
11496 {
11497 	u16 handle = 0;
11498 
11499 	bp->wol = 0;
11500 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
11501 		return;
11502 
11503 	do {
11504 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
11505 	} while (handle && handle != 0xffff);
11506 }
11507 
11508 static bool bnxt_eee_config_ok(struct bnxt *bp)
11509 {
11510 	struct ethtool_keee *eee = &bp->eee;
11511 	struct bnxt_link_info *link_info = &bp->link_info;
11512 
11513 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
11514 		return true;
11515 
11516 	if (eee->eee_enabled) {
11517 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
11518 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
11519 
11520 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
11521 
11522 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
11523 			eee->eee_enabled = 0;
11524 			return false;
11525 		}
11526 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
11527 			linkmode_and(eee->advertised, advertising,
11528 				     eee->supported);
11529 			return false;
11530 		}
11531 	}
11532 	return true;
11533 }
11534 
11535 static int bnxt_update_phy_setting(struct bnxt *bp)
11536 {
11537 	int rc;
11538 	bool update_link = false;
11539 	bool update_pause = false;
11540 	bool update_eee = false;
11541 	struct bnxt_link_info *link_info = &bp->link_info;
11542 
11543 	rc = bnxt_update_link(bp, true);
11544 	if (rc) {
11545 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
11546 			   rc);
11547 		return rc;
11548 	}
11549 	if (!BNXT_SINGLE_PF(bp))
11550 		return 0;
11551 
11552 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
11553 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
11554 	    link_info->req_flow_ctrl)
11555 		update_pause = true;
11556 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
11557 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
11558 		update_pause = true;
11559 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
11560 		if (BNXT_AUTO_MODE(link_info->auto_mode))
11561 			update_link = true;
11562 		if (bnxt_force_speed_updated(link_info))
11563 			update_link = true;
11564 		if (link_info->req_duplex != link_info->duplex_setting)
11565 			update_link = true;
11566 	} else {
11567 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
11568 			update_link = true;
11569 		if (bnxt_auto_speed_updated(link_info))
11570 			update_link = true;
11571 	}
11572 
11573 	/* The last close may have shutdown the link, so need to call
11574 	 * PHY_CFG to bring it back up.
11575 	 */
11576 	if (!BNXT_LINK_IS_UP(bp))
11577 		update_link = true;
11578 
11579 	if (!bnxt_eee_config_ok(bp))
11580 		update_eee = true;
11581 
11582 	if (update_link)
11583 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
11584 	else if (update_pause)
11585 		rc = bnxt_hwrm_set_pause(bp);
11586 	if (rc) {
11587 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
11588 			   rc);
11589 		return rc;
11590 	}
11591 
11592 	return rc;
11593 }
11594 
11595 /* Common routine to pre-map certain register block to different GRC window.
11596  * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
11597  * in PF and 3 windows in VF that can be customized to map in different
11598  * register blocks.
11599  */
11600 static void bnxt_preset_reg_win(struct bnxt *bp)
11601 {
11602 	if (BNXT_PF(bp)) {
11603 		/* CAG registers map to GRC window #4 */
11604 		writel(BNXT_CAG_REG_BASE,
11605 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
11606 	}
11607 }
11608 
11609 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
11610 
11611 static int bnxt_reinit_after_abort(struct bnxt *bp)
11612 {
11613 	int rc;
11614 
11615 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11616 		return -EBUSY;
11617 
11618 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
11619 		return -ENODEV;
11620 
11621 	rc = bnxt_fw_init_one(bp);
11622 	if (!rc) {
11623 		bnxt_clear_int_mode(bp);
11624 		rc = bnxt_init_int_mode(bp);
11625 		if (!rc) {
11626 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
11627 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11628 		}
11629 	}
11630 	return rc;
11631 }
11632 
11633 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
11634 {
11635 	struct bnxt_ntuple_filter *ntp_fltr;
11636 	struct bnxt_l2_filter *l2_fltr;
11637 
11638 	if (list_empty(&fltr->list))
11639 		return;
11640 
11641 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
11642 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
11643 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
11644 		atomic_inc(&l2_fltr->refcnt);
11645 		ntp_fltr->l2_fltr = l2_fltr;
11646 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
11647 			bnxt_del_ntp_filter(bp, ntp_fltr);
11648 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
11649 				   fltr->sw_id);
11650 		}
11651 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
11652 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
11653 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
11654 			bnxt_del_l2_filter(bp, l2_fltr);
11655 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
11656 				   fltr->sw_id);
11657 		}
11658 	}
11659 }
11660 
11661 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
11662 {
11663 	struct bnxt_filter_base *usr_fltr, *tmp;
11664 
11665 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
11666 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
11667 }
11668 
11669 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
11670 {
11671 	int rc = 0;
11672 
11673 	bnxt_preset_reg_win(bp);
11674 	netif_carrier_off(bp->dev);
11675 	if (irq_re_init) {
11676 		/* Reserve rings now if none were reserved at driver probe. */
11677 		rc = bnxt_init_dflt_ring_mode(bp);
11678 		if (rc) {
11679 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
11680 			return rc;
11681 		}
11682 	}
11683 	rc = bnxt_reserve_rings(bp, irq_re_init);
11684 	if (rc)
11685 		return rc;
11686 	if ((bp->flags & BNXT_FLAG_RFS) &&
11687 	    !(bp->flags & BNXT_FLAG_USING_MSIX)) {
11688 		/* disable RFS if falling back to INTA */
11689 		bp->dev->hw_features &= ~NETIF_F_NTUPLE;
11690 		bp->flags &= ~BNXT_FLAG_RFS;
11691 	}
11692 
11693 	rc = bnxt_alloc_mem(bp, irq_re_init);
11694 	if (rc) {
11695 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
11696 		goto open_err_free_mem;
11697 	}
11698 
11699 	if (irq_re_init) {
11700 		bnxt_init_napi(bp);
11701 		rc = bnxt_request_irq(bp);
11702 		if (rc) {
11703 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
11704 			goto open_err_irq;
11705 		}
11706 	}
11707 
11708 	rc = bnxt_init_nic(bp, irq_re_init);
11709 	if (rc) {
11710 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
11711 		goto open_err_irq;
11712 	}
11713 
11714 	bnxt_enable_napi(bp);
11715 	bnxt_debug_dev_init(bp);
11716 
11717 	if (link_re_init) {
11718 		mutex_lock(&bp->link_lock);
11719 		rc = bnxt_update_phy_setting(bp);
11720 		mutex_unlock(&bp->link_lock);
11721 		if (rc) {
11722 			netdev_warn(bp->dev, "failed to update phy settings\n");
11723 			if (BNXT_SINGLE_PF(bp)) {
11724 				bp->link_info.phy_retry = true;
11725 				bp->link_info.phy_retry_expires =
11726 					jiffies + 5 * HZ;
11727 			}
11728 		}
11729 	}
11730 
11731 	if (irq_re_init)
11732 		udp_tunnel_nic_reset_ntf(bp->dev);
11733 
11734 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
11735 		if (!static_key_enabled(&bnxt_xdp_locking_key))
11736 			static_branch_enable(&bnxt_xdp_locking_key);
11737 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
11738 		static_branch_disable(&bnxt_xdp_locking_key);
11739 	}
11740 	set_bit(BNXT_STATE_OPEN, &bp->state);
11741 	bnxt_enable_int(bp);
11742 	/* Enable TX queues */
11743 	bnxt_tx_enable(bp);
11744 	mod_timer(&bp->timer, jiffies + bp->current_interval);
11745 	/* Poll link status and check for SFP+ module status */
11746 	mutex_lock(&bp->link_lock);
11747 	bnxt_get_port_module_status(bp);
11748 	mutex_unlock(&bp->link_lock);
11749 
11750 	/* VF-reps may need to be re-opened after the PF is re-opened */
11751 	if (BNXT_PF(bp))
11752 		bnxt_vf_reps_open(bp);
11753 	if (bp->ptp_cfg)
11754 		atomic_set(&bp->ptp_cfg->tx_avail, BNXT_MAX_TX_TS);
11755 	bnxt_ptp_init_rtc(bp, true);
11756 	bnxt_ptp_cfg_tstamp_filters(bp);
11757 	bnxt_cfg_usr_fltrs(bp);
11758 	return 0;
11759 
11760 open_err_irq:
11761 	bnxt_del_napi(bp);
11762 
11763 open_err_free_mem:
11764 	bnxt_free_skbs(bp);
11765 	bnxt_free_irq(bp);
11766 	bnxt_free_mem(bp, true);
11767 	return rc;
11768 }
11769 
11770 /* rtnl_lock held */
11771 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
11772 {
11773 	int rc = 0;
11774 
11775 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
11776 		rc = -EIO;
11777 	if (!rc)
11778 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
11779 	if (rc) {
11780 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
11781 		dev_close(bp->dev);
11782 	}
11783 	return rc;
11784 }
11785 
11786 /* rtnl_lock held, open the NIC half way by allocating all resources, but
11787  * NAPI, IRQ, and TX are not enabled.  This is mainly used for offline
11788  * self tests.
11789  */
11790 int bnxt_half_open_nic(struct bnxt *bp)
11791 {
11792 	int rc = 0;
11793 
11794 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
11795 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
11796 		rc = -ENODEV;
11797 		goto half_open_err;
11798 	}
11799 
11800 	rc = bnxt_alloc_mem(bp, true);
11801 	if (rc) {
11802 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
11803 		goto half_open_err;
11804 	}
11805 	bnxt_init_napi(bp);
11806 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
11807 	rc = bnxt_init_nic(bp, true);
11808 	if (rc) {
11809 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
11810 		bnxt_del_napi(bp);
11811 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
11812 		goto half_open_err;
11813 	}
11814 	return 0;
11815 
11816 half_open_err:
11817 	bnxt_free_skbs(bp);
11818 	bnxt_free_mem(bp, true);
11819 	dev_close(bp->dev);
11820 	return rc;
11821 }
11822 
11823 /* rtnl_lock held, this call can only be made after a previous successful
11824  * call to bnxt_half_open_nic().
11825  */
11826 void bnxt_half_close_nic(struct bnxt *bp)
11827 {
11828 	bnxt_hwrm_resource_free(bp, false, true);
11829 	bnxt_del_napi(bp);
11830 	bnxt_free_skbs(bp);
11831 	bnxt_free_mem(bp, true);
11832 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
11833 }
11834 
11835 void bnxt_reenable_sriov(struct bnxt *bp)
11836 {
11837 	if (BNXT_PF(bp)) {
11838 		struct bnxt_pf_info *pf = &bp->pf;
11839 		int n = pf->active_vfs;
11840 
11841 		if (n)
11842 			bnxt_cfg_hw_sriov(bp, &n, true);
11843 	}
11844 }
11845 
11846 static int bnxt_open(struct net_device *dev)
11847 {
11848 	struct bnxt *bp = netdev_priv(dev);
11849 	int rc;
11850 
11851 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
11852 		rc = bnxt_reinit_after_abort(bp);
11853 		if (rc) {
11854 			if (rc == -EBUSY)
11855 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
11856 			else
11857 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
11858 			return -ENODEV;
11859 		}
11860 	}
11861 
11862 	rc = bnxt_hwrm_if_change(bp, true);
11863 	if (rc)
11864 		return rc;
11865 
11866 	rc = __bnxt_open_nic(bp, true, true);
11867 	if (rc) {
11868 		bnxt_hwrm_if_change(bp, false);
11869 	} else {
11870 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
11871 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
11872 				bnxt_ulp_start(bp, 0);
11873 				bnxt_reenable_sriov(bp);
11874 			}
11875 		}
11876 	}
11877 
11878 	return rc;
11879 }
11880 
11881 static bool bnxt_drv_busy(struct bnxt *bp)
11882 {
11883 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
11884 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
11885 }
11886 
11887 static void bnxt_get_ring_stats(struct bnxt *bp,
11888 				struct rtnl_link_stats64 *stats);
11889 
11890 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
11891 			     bool link_re_init)
11892 {
11893 	/* Close the VF-reps before closing PF */
11894 	if (BNXT_PF(bp))
11895 		bnxt_vf_reps_close(bp);
11896 
11897 	/* Change device state to avoid TX queue wake up's */
11898 	bnxt_tx_disable(bp);
11899 
11900 	clear_bit(BNXT_STATE_OPEN, &bp->state);
11901 	smp_mb__after_atomic();
11902 	while (bnxt_drv_busy(bp))
11903 		msleep(20);
11904 
11905 	/* Flush rings and disable interrupts */
11906 	bnxt_shutdown_nic(bp, irq_re_init);
11907 
11908 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
11909 
11910 	bnxt_debug_dev_exit(bp);
11911 	bnxt_disable_napi(bp);
11912 	del_timer_sync(&bp->timer);
11913 	bnxt_free_skbs(bp);
11914 
11915 	/* Save ring stats before shutdown */
11916 	if (bp->bnapi && irq_re_init) {
11917 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
11918 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
11919 	}
11920 	if (irq_re_init) {
11921 		bnxt_free_irq(bp);
11922 		bnxt_del_napi(bp);
11923 	}
11924 	bnxt_free_mem(bp, irq_re_init);
11925 }
11926 
11927 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
11928 {
11929 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
11930 		/* If we get here, it means firmware reset is in progress
11931 		 * while we are trying to close.  We can safely proceed with
11932 		 * the close because we are holding rtnl_lock().  Some firmware
11933 		 * messages may fail as we proceed to close.  We set the
11934 		 * ABORT_ERR flag here so that the FW reset thread will later
11935 		 * abort when it gets the rtnl_lock() and sees the flag.
11936 		 */
11937 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
11938 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
11939 	}
11940 
11941 #ifdef CONFIG_BNXT_SRIOV
11942 	if (bp->sriov_cfg) {
11943 		int rc;
11944 
11945 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
11946 						      !bp->sriov_cfg,
11947 						      BNXT_SRIOV_CFG_WAIT_TMO);
11948 		if (!rc)
11949 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
11950 		else if (rc < 0)
11951 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
11952 	}
11953 #endif
11954 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
11955 }
11956 
11957 static int bnxt_close(struct net_device *dev)
11958 {
11959 	struct bnxt *bp = netdev_priv(dev);
11960 
11961 	bnxt_close_nic(bp, true, true);
11962 	bnxt_hwrm_shutdown_link(bp);
11963 	bnxt_hwrm_if_change(bp, false);
11964 	return 0;
11965 }
11966 
11967 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
11968 				   u16 *val)
11969 {
11970 	struct hwrm_port_phy_mdio_read_output *resp;
11971 	struct hwrm_port_phy_mdio_read_input *req;
11972 	int rc;
11973 
11974 	if (bp->hwrm_spec_code < 0x10a00)
11975 		return -EOPNOTSUPP;
11976 
11977 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
11978 	if (rc)
11979 		return rc;
11980 
11981 	req->port_id = cpu_to_le16(bp->pf.port_id);
11982 	req->phy_addr = phy_addr;
11983 	req->reg_addr = cpu_to_le16(reg & 0x1f);
11984 	if (mdio_phy_id_is_c45(phy_addr)) {
11985 		req->cl45_mdio = 1;
11986 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
11987 		req->dev_addr = mdio_phy_id_devad(phy_addr);
11988 		req->reg_addr = cpu_to_le16(reg);
11989 	}
11990 
11991 	resp = hwrm_req_hold(bp, req);
11992 	rc = hwrm_req_send(bp, req);
11993 	if (!rc)
11994 		*val = le16_to_cpu(resp->reg_data);
11995 	hwrm_req_drop(bp, req);
11996 	return rc;
11997 }
11998 
11999 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
12000 				    u16 val)
12001 {
12002 	struct hwrm_port_phy_mdio_write_input *req;
12003 	int rc;
12004 
12005 	if (bp->hwrm_spec_code < 0x10a00)
12006 		return -EOPNOTSUPP;
12007 
12008 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
12009 	if (rc)
12010 		return rc;
12011 
12012 	req->port_id = cpu_to_le16(bp->pf.port_id);
12013 	req->phy_addr = phy_addr;
12014 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12015 	if (mdio_phy_id_is_c45(phy_addr)) {
12016 		req->cl45_mdio = 1;
12017 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12018 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12019 		req->reg_addr = cpu_to_le16(reg);
12020 	}
12021 	req->reg_data = cpu_to_le16(val);
12022 
12023 	return hwrm_req_send(bp, req);
12024 }
12025 
12026 /* rtnl_lock held */
12027 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12028 {
12029 	struct mii_ioctl_data *mdio = if_mii(ifr);
12030 	struct bnxt *bp = netdev_priv(dev);
12031 	int rc;
12032 
12033 	switch (cmd) {
12034 	case SIOCGMIIPHY:
12035 		mdio->phy_id = bp->link_info.phy_addr;
12036 
12037 		fallthrough;
12038 	case SIOCGMIIREG: {
12039 		u16 mii_regval = 0;
12040 
12041 		if (!netif_running(dev))
12042 			return -EAGAIN;
12043 
12044 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
12045 					     &mii_regval);
12046 		mdio->val_out = mii_regval;
12047 		return rc;
12048 	}
12049 
12050 	case SIOCSMIIREG:
12051 		if (!netif_running(dev))
12052 			return -EAGAIN;
12053 
12054 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
12055 						mdio->val_in);
12056 
12057 	case SIOCSHWTSTAMP:
12058 		return bnxt_hwtstamp_set(dev, ifr);
12059 
12060 	case SIOCGHWTSTAMP:
12061 		return bnxt_hwtstamp_get(dev, ifr);
12062 
12063 	default:
12064 		/* do nothing */
12065 		break;
12066 	}
12067 	return -EOPNOTSUPP;
12068 }
12069 
12070 static void bnxt_get_ring_stats(struct bnxt *bp,
12071 				struct rtnl_link_stats64 *stats)
12072 {
12073 	int i;
12074 
12075 	for (i = 0; i < bp->cp_nr_rings; i++) {
12076 		struct bnxt_napi *bnapi = bp->bnapi[i];
12077 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
12078 		u64 *sw = cpr->stats.sw_stats;
12079 
12080 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
12081 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12082 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
12083 
12084 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
12085 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
12086 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
12087 
12088 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
12089 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
12090 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
12091 
12092 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
12093 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
12094 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
12095 
12096 		stats->rx_missed_errors +=
12097 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
12098 
12099 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12100 
12101 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
12102 
12103 		stats->rx_dropped +=
12104 			cpr->sw_stats.rx.rx_netpoll_discards +
12105 			cpr->sw_stats.rx.rx_oom_discards;
12106 	}
12107 }
12108 
12109 static void bnxt_add_prev_stats(struct bnxt *bp,
12110 				struct rtnl_link_stats64 *stats)
12111 {
12112 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
12113 
12114 	stats->rx_packets += prev_stats->rx_packets;
12115 	stats->tx_packets += prev_stats->tx_packets;
12116 	stats->rx_bytes += prev_stats->rx_bytes;
12117 	stats->tx_bytes += prev_stats->tx_bytes;
12118 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
12119 	stats->multicast += prev_stats->multicast;
12120 	stats->rx_dropped += prev_stats->rx_dropped;
12121 	stats->tx_dropped += prev_stats->tx_dropped;
12122 }
12123 
12124 static void
12125 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
12126 {
12127 	struct bnxt *bp = netdev_priv(dev);
12128 
12129 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
12130 	/* Make sure bnxt_close_nic() sees that we are reading stats before
12131 	 * we check the BNXT_STATE_OPEN flag.
12132 	 */
12133 	smp_mb__after_atomic();
12134 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12135 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12136 		*stats = bp->net_stats_prev;
12137 		return;
12138 	}
12139 
12140 	bnxt_get_ring_stats(bp, stats);
12141 	bnxt_add_prev_stats(bp, stats);
12142 
12143 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
12144 		u64 *rx = bp->port_stats.sw_stats;
12145 		u64 *tx = bp->port_stats.sw_stats +
12146 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
12147 
12148 		stats->rx_crc_errors =
12149 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
12150 		stats->rx_frame_errors =
12151 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
12152 		stats->rx_length_errors =
12153 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
12154 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
12155 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
12156 		stats->rx_errors =
12157 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
12158 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
12159 		stats->collisions =
12160 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
12161 		stats->tx_fifo_errors =
12162 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
12163 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
12164 	}
12165 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12166 }
12167 
12168 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
12169 					struct bnxt_total_ring_err_stats *stats,
12170 					struct bnxt_cp_ring_info *cpr)
12171 {
12172 	struct bnxt_sw_stats *sw_stats = &cpr->sw_stats;
12173 	u64 *hw_stats = cpr->stats.sw_stats;
12174 
12175 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
12176 	stats->rx_total_resets += sw_stats->rx.rx_resets;
12177 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
12178 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
12179 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
12180 	stats->rx_total_ring_discards +=
12181 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
12182 	stats->tx_total_resets += sw_stats->tx.tx_resets;
12183 	stats->tx_total_ring_discards +=
12184 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
12185 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
12186 }
12187 
12188 void bnxt_get_ring_err_stats(struct bnxt *bp,
12189 			     struct bnxt_total_ring_err_stats *stats)
12190 {
12191 	int i;
12192 
12193 	for (i = 0; i < bp->cp_nr_rings; i++)
12194 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
12195 }
12196 
12197 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
12198 {
12199 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12200 	struct net_device *dev = bp->dev;
12201 	struct netdev_hw_addr *ha;
12202 	u8 *haddr;
12203 	int mc_count = 0;
12204 	bool update = false;
12205 	int off = 0;
12206 
12207 	netdev_for_each_mc_addr(ha, dev) {
12208 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
12209 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12210 			vnic->mc_list_count = 0;
12211 			return false;
12212 		}
12213 		haddr = ha->addr;
12214 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
12215 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
12216 			update = true;
12217 		}
12218 		off += ETH_ALEN;
12219 		mc_count++;
12220 	}
12221 	if (mc_count)
12222 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
12223 
12224 	if (mc_count != vnic->mc_list_count) {
12225 		vnic->mc_list_count = mc_count;
12226 		update = true;
12227 	}
12228 	return update;
12229 }
12230 
12231 static bool bnxt_uc_list_updated(struct bnxt *bp)
12232 {
12233 	struct net_device *dev = bp->dev;
12234 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12235 	struct netdev_hw_addr *ha;
12236 	int off = 0;
12237 
12238 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
12239 		return true;
12240 
12241 	netdev_for_each_uc_addr(ha, dev) {
12242 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
12243 			return true;
12244 
12245 		off += ETH_ALEN;
12246 	}
12247 	return false;
12248 }
12249 
12250 static void bnxt_set_rx_mode(struct net_device *dev)
12251 {
12252 	struct bnxt *bp = netdev_priv(dev);
12253 	struct bnxt_vnic_info *vnic;
12254 	bool mc_update = false;
12255 	bool uc_update;
12256 	u32 mask;
12257 
12258 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
12259 		return;
12260 
12261 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12262 	mask = vnic->rx_mask;
12263 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
12264 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
12265 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
12266 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
12267 
12268 	if (dev->flags & IFF_PROMISC)
12269 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12270 
12271 	uc_update = bnxt_uc_list_updated(bp);
12272 
12273 	if (dev->flags & IFF_BROADCAST)
12274 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
12275 	if (dev->flags & IFF_ALLMULTI) {
12276 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12277 		vnic->mc_list_count = 0;
12278 	} else if (dev->flags & IFF_MULTICAST) {
12279 		mc_update = bnxt_mc_list_updated(bp, &mask);
12280 	}
12281 
12282 	if (mask != vnic->rx_mask || uc_update || mc_update) {
12283 		vnic->rx_mask = mask;
12284 
12285 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
12286 	}
12287 }
12288 
12289 static int bnxt_cfg_rx_mode(struct bnxt *bp)
12290 {
12291 	struct net_device *dev = bp->dev;
12292 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12293 	struct netdev_hw_addr *ha;
12294 	int i, off = 0, rc;
12295 	bool uc_update;
12296 
12297 	netif_addr_lock_bh(dev);
12298 	uc_update = bnxt_uc_list_updated(bp);
12299 	netif_addr_unlock_bh(dev);
12300 
12301 	if (!uc_update)
12302 		goto skip_uc;
12303 
12304 	for (i = 1; i < vnic->uc_filter_count; i++) {
12305 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
12306 
12307 		bnxt_hwrm_l2_filter_free(bp, fltr);
12308 		bnxt_del_l2_filter(bp, fltr);
12309 	}
12310 
12311 	vnic->uc_filter_count = 1;
12312 
12313 	netif_addr_lock_bh(dev);
12314 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
12315 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12316 	} else {
12317 		netdev_for_each_uc_addr(ha, dev) {
12318 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
12319 			off += ETH_ALEN;
12320 			vnic->uc_filter_count++;
12321 		}
12322 	}
12323 	netif_addr_unlock_bh(dev);
12324 
12325 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
12326 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
12327 		if (rc) {
12328 			if (BNXT_VF(bp) && rc == -ENODEV) {
12329 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
12330 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
12331 				else
12332 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
12333 				rc = 0;
12334 			} else {
12335 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
12336 			}
12337 			vnic->uc_filter_count = i;
12338 			return rc;
12339 		}
12340 	}
12341 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
12342 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
12343 
12344 skip_uc:
12345 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
12346 	    !bnxt_promisc_ok(bp))
12347 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12348 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
12349 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
12350 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
12351 			    rc);
12352 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
12353 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12354 		vnic->mc_list_count = 0;
12355 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
12356 	}
12357 	if (rc)
12358 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
12359 			   rc);
12360 
12361 	return rc;
12362 }
12363 
12364 static bool bnxt_can_reserve_rings(struct bnxt *bp)
12365 {
12366 #ifdef CONFIG_BNXT_SRIOV
12367 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
12368 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12369 
12370 		/* No minimum rings were provisioned by the PF.  Don't
12371 		 * reserve rings by default when device is down.
12372 		 */
12373 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
12374 			return true;
12375 
12376 		if (!netif_running(bp->dev))
12377 			return false;
12378 	}
12379 #endif
12380 	return true;
12381 }
12382 
12383 /* If the chip and firmware supports RFS */
12384 static bool bnxt_rfs_supported(struct bnxt *bp)
12385 {
12386 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
12387 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
12388 			return true;
12389 		return false;
12390 	}
12391 	/* 212 firmware is broken for aRFS */
12392 	if (BNXT_FW_MAJ(bp) == 212)
12393 		return false;
12394 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
12395 		return true;
12396 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
12397 		return true;
12398 	return false;
12399 }
12400 
12401 /* If runtime conditions support RFS */
12402 static bool bnxt_rfs_capable(struct bnxt *bp)
12403 {
12404 	struct bnxt_hw_rings hwr = {0};
12405 	int max_vnics, max_rss_ctxs;
12406 
12407 	hwr.rss_ctx = 1;
12408 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
12409 		/* 2 VNICS: default + Ntuple */
12410 		hwr.vnic = 2;
12411 		hwr.rss_ctx = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) *
12412 			      hwr.vnic;
12413 		goto check_reserve_vnic;
12414 	}
12415 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
12416 		return bnxt_rfs_supported(bp);
12417 	if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
12418 		return false;
12419 
12420 	hwr.vnic = 1 + bp->rx_nr_rings;
12421 check_reserve_vnic:
12422 	max_vnics = bnxt_get_max_func_vnics(bp);
12423 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
12424 
12425 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
12426 	    !(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP))
12427 		hwr.rss_ctx = hwr.vnic;
12428 
12429 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
12430 		if (bp->rx_nr_rings > 1)
12431 			netdev_warn(bp->dev,
12432 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
12433 				    min(max_rss_ctxs - 1, max_vnics - 1));
12434 		return false;
12435 	}
12436 
12437 	if (!BNXT_NEW_RM(bp))
12438 		return true;
12439 
12440 	if (hwr.vnic == bp->hw_resc.resv_vnics &&
12441 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
12442 		return true;
12443 
12444 	bnxt_hwrm_reserve_rings(bp, &hwr);
12445 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
12446 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
12447 		return true;
12448 
12449 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
12450 	hwr.vnic = 1;
12451 	hwr.rss_ctx = 0;
12452 	bnxt_hwrm_reserve_rings(bp, &hwr);
12453 	return false;
12454 }
12455 
12456 static netdev_features_t bnxt_fix_features(struct net_device *dev,
12457 					   netdev_features_t features)
12458 {
12459 	struct bnxt *bp = netdev_priv(dev);
12460 	netdev_features_t vlan_features;
12461 
12462 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
12463 		features &= ~NETIF_F_NTUPLE;
12464 
12465 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
12466 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
12467 
12468 	if (!(features & NETIF_F_GRO))
12469 		features &= ~NETIF_F_GRO_HW;
12470 
12471 	if (features & NETIF_F_GRO_HW)
12472 		features &= ~NETIF_F_LRO;
12473 
12474 	/* Both CTAG and STAG VLAN accelaration on the RX side have to be
12475 	 * turned on or off together.
12476 	 */
12477 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
12478 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
12479 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
12480 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
12481 		else if (vlan_features)
12482 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
12483 	}
12484 #ifdef CONFIG_BNXT_SRIOV
12485 	if (BNXT_VF(bp) && bp->vf.vlan)
12486 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
12487 #endif
12488 	return features;
12489 }
12490 
12491 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
12492 				bool link_re_init, u32 flags, bool update_tpa)
12493 {
12494 	bnxt_close_nic(bp, irq_re_init, link_re_init);
12495 	bp->flags = flags;
12496 	if (update_tpa)
12497 		bnxt_set_ring_params(bp);
12498 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
12499 }
12500 
12501 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
12502 {
12503 	bool update_tpa = false, update_ntuple = false;
12504 	struct bnxt *bp = netdev_priv(dev);
12505 	u32 flags = bp->flags;
12506 	u32 changes;
12507 	int rc = 0;
12508 	bool re_init = false;
12509 
12510 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
12511 	if (features & NETIF_F_GRO_HW)
12512 		flags |= BNXT_FLAG_GRO;
12513 	else if (features & NETIF_F_LRO)
12514 		flags |= BNXT_FLAG_LRO;
12515 
12516 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
12517 		flags &= ~BNXT_FLAG_TPA;
12518 
12519 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
12520 		flags |= BNXT_FLAG_STRIP_VLAN;
12521 
12522 	if (features & NETIF_F_NTUPLE)
12523 		flags |= BNXT_FLAG_RFS;
12524 	else
12525 		bnxt_clear_usr_fltrs(bp, true);
12526 
12527 	changes = flags ^ bp->flags;
12528 	if (changes & BNXT_FLAG_TPA) {
12529 		update_tpa = true;
12530 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
12531 		    (flags & BNXT_FLAG_TPA) == 0 ||
12532 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
12533 			re_init = true;
12534 	}
12535 
12536 	if (changes & ~BNXT_FLAG_TPA)
12537 		re_init = true;
12538 
12539 	if (changes & BNXT_FLAG_RFS)
12540 		update_ntuple = true;
12541 
12542 	if (flags != bp->flags) {
12543 		u32 old_flags = bp->flags;
12544 
12545 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12546 			bp->flags = flags;
12547 			if (update_tpa)
12548 				bnxt_set_ring_params(bp);
12549 			return rc;
12550 		}
12551 
12552 		if (update_ntuple)
12553 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
12554 
12555 		if (re_init)
12556 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
12557 
12558 		if (update_tpa) {
12559 			bp->flags = flags;
12560 			rc = bnxt_set_tpa(bp,
12561 					  (flags & BNXT_FLAG_TPA) ?
12562 					  true : false);
12563 			if (rc)
12564 				bp->flags = old_flags;
12565 		}
12566 	}
12567 	return rc;
12568 }
12569 
12570 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
12571 			      u8 **nextp)
12572 {
12573 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
12574 	struct hop_jumbo_hdr *jhdr;
12575 	int hdr_count = 0;
12576 	u8 *nexthdr;
12577 	int start;
12578 
12579 	/* Check that there are at most 2 IPv6 extension headers, no
12580 	 * fragment header, and each is <= 64 bytes.
12581 	 */
12582 	start = nw_off + sizeof(*ip6h);
12583 	nexthdr = &ip6h->nexthdr;
12584 	while (ipv6_ext_hdr(*nexthdr)) {
12585 		struct ipv6_opt_hdr *hp;
12586 		int hdrlen;
12587 
12588 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
12589 		    *nexthdr == NEXTHDR_FRAGMENT)
12590 			return false;
12591 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
12592 					  skb_headlen(skb), NULL);
12593 		if (!hp)
12594 			return false;
12595 		if (*nexthdr == NEXTHDR_AUTH)
12596 			hdrlen = ipv6_authlen(hp);
12597 		else
12598 			hdrlen = ipv6_optlen(hp);
12599 
12600 		if (hdrlen > 64)
12601 			return false;
12602 
12603 		/* The ext header may be a hop-by-hop header inserted for
12604 		 * big TCP purposes. This will be removed before sending
12605 		 * from NIC, so do not count it.
12606 		 */
12607 		if (*nexthdr == NEXTHDR_HOP) {
12608 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
12609 				goto increment_hdr;
12610 
12611 			jhdr = (struct hop_jumbo_hdr *)hp;
12612 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
12613 			    jhdr->nexthdr != IPPROTO_TCP)
12614 				goto increment_hdr;
12615 
12616 			goto next_hdr;
12617 		}
12618 increment_hdr:
12619 		hdr_count++;
12620 next_hdr:
12621 		nexthdr = &hp->nexthdr;
12622 		start += hdrlen;
12623 	}
12624 	if (nextp) {
12625 		/* Caller will check inner protocol */
12626 		if (skb->encapsulation) {
12627 			*nextp = nexthdr;
12628 			return true;
12629 		}
12630 		*nextp = NULL;
12631 	}
12632 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
12633 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
12634 }
12635 
12636 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
12637 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
12638 {
12639 	struct udphdr *uh = udp_hdr(skb);
12640 	__be16 udp_port = uh->dest;
12641 
12642 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
12643 	    udp_port != bp->vxlan_gpe_port)
12644 		return false;
12645 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
12646 		struct ethhdr *eh = inner_eth_hdr(skb);
12647 
12648 		switch (eh->h_proto) {
12649 		case htons(ETH_P_IP):
12650 			return true;
12651 		case htons(ETH_P_IPV6):
12652 			return bnxt_exthdr_check(bp, skb,
12653 						 skb_inner_network_offset(skb),
12654 						 NULL);
12655 		}
12656 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
12657 		return true;
12658 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
12659 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
12660 					 NULL);
12661 	}
12662 	return false;
12663 }
12664 
12665 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
12666 {
12667 	switch (l4_proto) {
12668 	case IPPROTO_UDP:
12669 		return bnxt_udp_tunl_check(bp, skb);
12670 	case IPPROTO_IPIP:
12671 		return true;
12672 	case IPPROTO_GRE: {
12673 		switch (skb->inner_protocol) {
12674 		default:
12675 			return false;
12676 		case htons(ETH_P_IP):
12677 			return true;
12678 		case htons(ETH_P_IPV6):
12679 			fallthrough;
12680 		}
12681 	}
12682 	case IPPROTO_IPV6:
12683 		/* Check ext headers of inner ipv6 */
12684 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
12685 					 NULL);
12686 	}
12687 	return false;
12688 }
12689 
12690 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
12691 					     struct net_device *dev,
12692 					     netdev_features_t features)
12693 {
12694 	struct bnxt *bp = netdev_priv(dev);
12695 	u8 *l4_proto;
12696 
12697 	features = vlan_features_check(skb, features);
12698 	switch (vlan_get_protocol(skb)) {
12699 	case htons(ETH_P_IP):
12700 		if (!skb->encapsulation)
12701 			return features;
12702 		l4_proto = &ip_hdr(skb)->protocol;
12703 		if (bnxt_tunl_check(bp, skb, *l4_proto))
12704 			return features;
12705 		break;
12706 	case htons(ETH_P_IPV6):
12707 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
12708 				       &l4_proto))
12709 			break;
12710 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
12711 			return features;
12712 		break;
12713 	}
12714 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
12715 }
12716 
12717 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
12718 			 u32 *reg_buf)
12719 {
12720 	struct hwrm_dbg_read_direct_output *resp;
12721 	struct hwrm_dbg_read_direct_input *req;
12722 	__le32 *dbg_reg_buf;
12723 	dma_addr_t mapping;
12724 	int rc, i;
12725 
12726 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
12727 	if (rc)
12728 		return rc;
12729 
12730 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
12731 					 &mapping);
12732 	if (!dbg_reg_buf) {
12733 		rc = -ENOMEM;
12734 		goto dbg_rd_reg_exit;
12735 	}
12736 
12737 	req->host_dest_addr = cpu_to_le64(mapping);
12738 
12739 	resp = hwrm_req_hold(bp, req);
12740 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
12741 	req->read_len32 = cpu_to_le32(num_words);
12742 
12743 	rc = hwrm_req_send(bp, req);
12744 	if (rc || resp->error_code) {
12745 		rc = -EIO;
12746 		goto dbg_rd_reg_exit;
12747 	}
12748 	for (i = 0; i < num_words; i++)
12749 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
12750 
12751 dbg_rd_reg_exit:
12752 	hwrm_req_drop(bp, req);
12753 	return rc;
12754 }
12755 
12756 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
12757 				       u32 ring_id, u32 *prod, u32 *cons)
12758 {
12759 	struct hwrm_dbg_ring_info_get_output *resp;
12760 	struct hwrm_dbg_ring_info_get_input *req;
12761 	int rc;
12762 
12763 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
12764 	if (rc)
12765 		return rc;
12766 
12767 	req->ring_type = ring_type;
12768 	req->fw_ring_id = cpu_to_le32(ring_id);
12769 	resp = hwrm_req_hold(bp, req);
12770 	rc = hwrm_req_send(bp, req);
12771 	if (!rc) {
12772 		*prod = le32_to_cpu(resp->producer_index);
12773 		*cons = le32_to_cpu(resp->consumer_index);
12774 	}
12775 	hwrm_req_drop(bp, req);
12776 	return rc;
12777 }
12778 
12779 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
12780 {
12781 	struct bnxt_tx_ring_info *txr;
12782 	int i = bnapi->index, j;
12783 
12784 	bnxt_for_each_napi_tx(j, bnapi, txr)
12785 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
12786 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
12787 			    txr->tx_cons);
12788 }
12789 
12790 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
12791 {
12792 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
12793 	int i = bnapi->index;
12794 
12795 	if (!rxr)
12796 		return;
12797 
12798 	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",
12799 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
12800 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
12801 		    rxr->rx_sw_agg_prod);
12802 }
12803 
12804 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
12805 {
12806 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
12807 	int i = bnapi->index;
12808 
12809 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
12810 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
12811 }
12812 
12813 static void bnxt_dbg_dump_states(struct bnxt *bp)
12814 {
12815 	int i;
12816 	struct bnxt_napi *bnapi;
12817 
12818 	for (i = 0; i < bp->cp_nr_rings; i++) {
12819 		bnapi = bp->bnapi[i];
12820 		if (netif_msg_drv(bp)) {
12821 			bnxt_dump_tx_sw_state(bnapi);
12822 			bnxt_dump_rx_sw_state(bnapi);
12823 			bnxt_dump_cp_sw_state(bnapi);
12824 		}
12825 	}
12826 }
12827 
12828 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
12829 {
12830 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
12831 	struct hwrm_ring_reset_input *req;
12832 	struct bnxt_napi *bnapi = rxr->bnapi;
12833 	struct bnxt_cp_ring_info *cpr;
12834 	u16 cp_ring_id;
12835 	int rc;
12836 
12837 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
12838 	if (rc)
12839 		return rc;
12840 
12841 	cpr = &bnapi->cp_ring;
12842 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
12843 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
12844 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
12845 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
12846 	return hwrm_req_send_silent(bp, req);
12847 }
12848 
12849 static void bnxt_reset_task(struct bnxt *bp, bool silent)
12850 {
12851 	if (!silent)
12852 		bnxt_dbg_dump_states(bp);
12853 	if (netif_running(bp->dev)) {
12854 		int rc;
12855 
12856 		if (silent) {
12857 			bnxt_close_nic(bp, false, false);
12858 			bnxt_open_nic(bp, false, false);
12859 		} else {
12860 			bnxt_ulp_stop(bp);
12861 			bnxt_close_nic(bp, true, false);
12862 			rc = bnxt_open_nic(bp, true, false);
12863 			bnxt_ulp_start(bp, rc);
12864 		}
12865 	}
12866 }
12867 
12868 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
12869 {
12870 	struct bnxt *bp = netdev_priv(dev);
12871 
12872 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
12873 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
12874 }
12875 
12876 static void bnxt_fw_health_check(struct bnxt *bp)
12877 {
12878 	struct bnxt_fw_health *fw_health = bp->fw_health;
12879 	struct pci_dev *pdev = bp->pdev;
12880 	u32 val;
12881 
12882 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12883 		return;
12884 
12885 	/* Make sure it is enabled before checking the tmr_counter. */
12886 	smp_rmb();
12887 	if (fw_health->tmr_counter) {
12888 		fw_health->tmr_counter--;
12889 		return;
12890 	}
12891 
12892 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
12893 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
12894 		fw_health->arrests++;
12895 		goto fw_reset;
12896 	}
12897 
12898 	fw_health->last_fw_heartbeat = val;
12899 
12900 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
12901 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
12902 		fw_health->discoveries++;
12903 		goto fw_reset;
12904 	}
12905 
12906 	fw_health->tmr_counter = fw_health->tmr_multiplier;
12907 	return;
12908 
12909 fw_reset:
12910 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
12911 }
12912 
12913 static void bnxt_timer(struct timer_list *t)
12914 {
12915 	struct bnxt *bp = from_timer(bp, t, timer);
12916 	struct net_device *dev = bp->dev;
12917 
12918 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
12919 		return;
12920 
12921 	if (atomic_read(&bp->intr_sem) != 0)
12922 		goto bnxt_restart_timer;
12923 
12924 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
12925 		bnxt_fw_health_check(bp);
12926 
12927 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
12928 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
12929 
12930 	if (bnxt_tc_flower_enabled(bp))
12931 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
12932 
12933 #ifdef CONFIG_RFS_ACCEL
12934 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
12935 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
12936 #endif /*CONFIG_RFS_ACCEL*/
12937 
12938 	if (bp->link_info.phy_retry) {
12939 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
12940 			bp->link_info.phy_retry = false;
12941 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
12942 		} else {
12943 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
12944 		}
12945 	}
12946 
12947 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
12948 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
12949 
12950 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
12951 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
12952 
12953 bnxt_restart_timer:
12954 	mod_timer(&bp->timer, jiffies + bp->current_interval);
12955 }
12956 
12957 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
12958 {
12959 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
12960 	 * set.  If the device is being closed, bnxt_close() may be holding
12961 	 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear.  So we
12962 	 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
12963 	 */
12964 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
12965 	rtnl_lock();
12966 }
12967 
12968 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
12969 {
12970 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
12971 	rtnl_unlock();
12972 }
12973 
12974 /* Only called from bnxt_sp_task() */
12975 static void bnxt_reset(struct bnxt *bp, bool silent)
12976 {
12977 	bnxt_rtnl_lock_sp(bp);
12978 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
12979 		bnxt_reset_task(bp, silent);
12980 	bnxt_rtnl_unlock_sp(bp);
12981 }
12982 
12983 /* Only called from bnxt_sp_task() */
12984 static void bnxt_rx_ring_reset(struct bnxt *bp)
12985 {
12986 	int i;
12987 
12988 	bnxt_rtnl_lock_sp(bp);
12989 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12990 		bnxt_rtnl_unlock_sp(bp);
12991 		return;
12992 	}
12993 	/* Disable and flush TPA before resetting the RX ring */
12994 	if (bp->flags & BNXT_FLAG_TPA)
12995 		bnxt_set_tpa(bp, false);
12996 	for (i = 0; i < bp->rx_nr_rings; i++) {
12997 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
12998 		struct bnxt_cp_ring_info *cpr;
12999 		int rc;
13000 
13001 		if (!rxr->bnapi->in_reset)
13002 			continue;
13003 
13004 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
13005 		if (rc) {
13006 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
13007 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
13008 			else
13009 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
13010 					    rc);
13011 			bnxt_reset_task(bp, true);
13012 			break;
13013 		}
13014 		bnxt_free_one_rx_ring_skbs(bp, i);
13015 		rxr->rx_prod = 0;
13016 		rxr->rx_agg_prod = 0;
13017 		rxr->rx_sw_agg_prod = 0;
13018 		rxr->rx_next_cons = 0;
13019 		rxr->bnapi->in_reset = false;
13020 		bnxt_alloc_one_rx_ring(bp, i);
13021 		cpr = &rxr->bnapi->cp_ring;
13022 		cpr->sw_stats.rx.rx_resets++;
13023 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
13024 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
13025 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
13026 	}
13027 	if (bp->flags & BNXT_FLAG_TPA)
13028 		bnxt_set_tpa(bp, true);
13029 	bnxt_rtnl_unlock_sp(bp);
13030 }
13031 
13032 static void bnxt_fw_fatal_close(struct bnxt *bp)
13033 {
13034 	bnxt_tx_disable(bp);
13035 	bnxt_disable_napi(bp);
13036 	bnxt_disable_int_sync(bp);
13037 	bnxt_free_irq(bp);
13038 	bnxt_clear_int_mode(bp);
13039 	pci_disable_device(bp->pdev);
13040 }
13041 
13042 static void bnxt_fw_reset_close(struct bnxt *bp)
13043 {
13044 	bnxt_ulp_stop(bp);
13045 	/* When firmware is in fatal state, quiesce device and disable
13046 	 * bus master to prevent any potential bad DMAs before freeing
13047 	 * kernel memory.
13048 	 */
13049 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
13050 		u16 val = 0;
13051 
13052 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
13053 		if (val == 0xffff)
13054 			bp->fw_reset_min_dsecs = 0;
13055 		bnxt_fw_fatal_close(bp);
13056 	}
13057 	__bnxt_close_nic(bp, true, false);
13058 	bnxt_vf_reps_free(bp);
13059 	bnxt_clear_int_mode(bp);
13060 	bnxt_hwrm_func_drv_unrgtr(bp);
13061 	if (pci_is_enabled(bp->pdev))
13062 		pci_disable_device(bp->pdev);
13063 	bnxt_free_ctx_mem(bp);
13064 }
13065 
13066 static bool is_bnxt_fw_ok(struct bnxt *bp)
13067 {
13068 	struct bnxt_fw_health *fw_health = bp->fw_health;
13069 	bool no_heartbeat = false, has_reset = false;
13070 	u32 val;
13071 
13072 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13073 	if (val == fw_health->last_fw_heartbeat)
13074 		no_heartbeat = true;
13075 
13076 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13077 	if (val != fw_health->last_fw_reset_cnt)
13078 		has_reset = true;
13079 
13080 	if (!no_heartbeat && has_reset)
13081 		return true;
13082 
13083 	return false;
13084 }
13085 
13086 /* rtnl_lock is acquired before calling this function */
13087 static void bnxt_force_fw_reset(struct bnxt *bp)
13088 {
13089 	struct bnxt_fw_health *fw_health = bp->fw_health;
13090 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13091 	u32 wait_dsecs;
13092 
13093 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
13094 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13095 		return;
13096 
13097 	if (ptp) {
13098 		spin_lock_bh(&ptp->ptp_lock);
13099 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13100 		spin_unlock_bh(&ptp->ptp_lock);
13101 	} else {
13102 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13103 	}
13104 	bnxt_fw_reset_close(bp);
13105 	wait_dsecs = fw_health->master_func_wait_dsecs;
13106 	if (fw_health->primary) {
13107 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
13108 			wait_dsecs = 0;
13109 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
13110 	} else {
13111 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
13112 		wait_dsecs = fw_health->normal_func_wait_dsecs;
13113 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13114 	}
13115 
13116 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
13117 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
13118 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
13119 }
13120 
13121 void bnxt_fw_exception(struct bnxt *bp)
13122 {
13123 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
13124 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
13125 	bnxt_rtnl_lock_sp(bp);
13126 	bnxt_force_fw_reset(bp);
13127 	bnxt_rtnl_unlock_sp(bp);
13128 }
13129 
13130 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
13131  * < 0 on error.
13132  */
13133 static int bnxt_get_registered_vfs(struct bnxt *bp)
13134 {
13135 #ifdef CONFIG_BNXT_SRIOV
13136 	int rc;
13137 
13138 	if (!BNXT_PF(bp))
13139 		return 0;
13140 
13141 	rc = bnxt_hwrm_func_qcfg(bp);
13142 	if (rc) {
13143 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
13144 		return rc;
13145 	}
13146 	if (bp->pf.registered_vfs)
13147 		return bp->pf.registered_vfs;
13148 	if (bp->sriov_cfg)
13149 		return 1;
13150 #endif
13151 	return 0;
13152 }
13153 
13154 void bnxt_fw_reset(struct bnxt *bp)
13155 {
13156 	bnxt_rtnl_lock_sp(bp);
13157 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
13158 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13159 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13160 		int n = 0, tmo;
13161 
13162 		if (ptp) {
13163 			spin_lock_bh(&ptp->ptp_lock);
13164 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13165 			spin_unlock_bh(&ptp->ptp_lock);
13166 		} else {
13167 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13168 		}
13169 		if (bp->pf.active_vfs &&
13170 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
13171 			n = bnxt_get_registered_vfs(bp);
13172 		if (n < 0) {
13173 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
13174 				   n);
13175 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13176 			dev_close(bp->dev);
13177 			goto fw_reset_exit;
13178 		} else if (n > 0) {
13179 			u16 vf_tmo_dsecs = n * 10;
13180 
13181 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
13182 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
13183 			bp->fw_reset_state =
13184 				BNXT_FW_RESET_STATE_POLL_VF;
13185 			bnxt_queue_fw_reset_work(bp, HZ / 10);
13186 			goto fw_reset_exit;
13187 		}
13188 		bnxt_fw_reset_close(bp);
13189 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
13190 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
13191 			tmo = HZ / 10;
13192 		} else {
13193 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13194 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
13195 		}
13196 		bnxt_queue_fw_reset_work(bp, tmo);
13197 	}
13198 fw_reset_exit:
13199 	bnxt_rtnl_unlock_sp(bp);
13200 }
13201 
13202 static void bnxt_chk_missed_irq(struct bnxt *bp)
13203 {
13204 	int i;
13205 
13206 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13207 		return;
13208 
13209 	for (i = 0; i < bp->cp_nr_rings; i++) {
13210 		struct bnxt_napi *bnapi = bp->bnapi[i];
13211 		struct bnxt_cp_ring_info *cpr;
13212 		u32 fw_ring_id;
13213 		int j;
13214 
13215 		if (!bnapi)
13216 			continue;
13217 
13218 		cpr = &bnapi->cp_ring;
13219 		for (j = 0; j < cpr->cp_ring_count; j++) {
13220 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
13221 			u32 val[2];
13222 
13223 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
13224 				continue;
13225 
13226 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
13227 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
13228 				continue;
13229 			}
13230 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
13231 			bnxt_dbg_hwrm_ring_info_get(bp,
13232 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
13233 				fw_ring_id, &val[0], &val[1]);
13234 			cpr->sw_stats.cmn.missed_irqs++;
13235 		}
13236 	}
13237 }
13238 
13239 static void bnxt_cfg_ntp_filters(struct bnxt *);
13240 
13241 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
13242 {
13243 	struct bnxt_link_info *link_info = &bp->link_info;
13244 
13245 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
13246 		link_info->autoneg = BNXT_AUTONEG_SPEED;
13247 		if (bp->hwrm_spec_code >= 0x10201) {
13248 			if (link_info->auto_pause_setting &
13249 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
13250 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13251 		} else {
13252 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13253 		}
13254 		bnxt_set_auto_speed(link_info);
13255 	} else {
13256 		bnxt_set_force_speed(link_info);
13257 		link_info->req_duplex = link_info->duplex_setting;
13258 	}
13259 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
13260 		link_info->req_flow_ctrl =
13261 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
13262 	else
13263 		link_info->req_flow_ctrl = link_info->force_pause_setting;
13264 }
13265 
13266 static void bnxt_fw_echo_reply(struct bnxt *bp)
13267 {
13268 	struct bnxt_fw_health *fw_health = bp->fw_health;
13269 	struct hwrm_func_echo_response_input *req;
13270 	int rc;
13271 
13272 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
13273 	if (rc)
13274 		return;
13275 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
13276 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
13277 	hwrm_req_send(bp, req);
13278 }
13279 
13280 static void bnxt_sp_task(struct work_struct *work)
13281 {
13282 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
13283 
13284 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13285 	smp_mb__after_atomic();
13286 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13287 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13288 		return;
13289 	}
13290 
13291 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
13292 		bnxt_cfg_rx_mode(bp);
13293 
13294 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
13295 		bnxt_cfg_ntp_filters(bp);
13296 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
13297 		bnxt_hwrm_exec_fwd_req(bp);
13298 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
13299 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
13300 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
13301 		bnxt_hwrm_port_qstats(bp, 0);
13302 		bnxt_hwrm_port_qstats_ext(bp, 0);
13303 		bnxt_accumulate_all_stats(bp);
13304 	}
13305 
13306 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
13307 		int rc;
13308 
13309 		mutex_lock(&bp->link_lock);
13310 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
13311 				       &bp->sp_event))
13312 			bnxt_hwrm_phy_qcaps(bp);
13313 
13314 		rc = bnxt_update_link(bp, true);
13315 		if (rc)
13316 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
13317 				   rc);
13318 
13319 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
13320 				       &bp->sp_event))
13321 			bnxt_init_ethtool_link_settings(bp);
13322 		mutex_unlock(&bp->link_lock);
13323 	}
13324 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
13325 		int rc;
13326 
13327 		mutex_lock(&bp->link_lock);
13328 		rc = bnxt_update_phy_setting(bp);
13329 		mutex_unlock(&bp->link_lock);
13330 		if (rc) {
13331 			netdev_warn(bp->dev, "update phy settings retry failed\n");
13332 		} else {
13333 			bp->link_info.phy_retry = false;
13334 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
13335 		}
13336 	}
13337 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
13338 		mutex_lock(&bp->link_lock);
13339 		bnxt_get_port_module_status(bp);
13340 		mutex_unlock(&bp->link_lock);
13341 	}
13342 
13343 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
13344 		bnxt_tc_flow_stats_work(bp);
13345 
13346 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
13347 		bnxt_chk_missed_irq(bp);
13348 
13349 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
13350 		bnxt_fw_echo_reply(bp);
13351 
13352 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
13353 		bnxt_hwmon_notify_event(bp);
13354 
13355 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
13356 	 * must be the last functions to be called before exiting.
13357 	 */
13358 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
13359 		bnxt_reset(bp, false);
13360 
13361 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
13362 		bnxt_reset(bp, true);
13363 
13364 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
13365 		bnxt_rx_ring_reset(bp);
13366 
13367 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
13368 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
13369 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
13370 			bnxt_devlink_health_fw_report(bp);
13371 		else
13372 			bnxt_fw_reset(bp);
13373 	}
13374 
13375 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
13376 		if (!is_bnxt_fw_ok(bp))
13377 			bnxt_devlink_health_fw_report(bp);
13378 	}
13379 
13380 	smp_mb__before_atomic();
13381 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13382 }
13383 
13384 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
13385 				int *max_cp);
13386 
13387 /* Under rtnl_lock */
13388 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
13389 		     int tx_xdp)
13390 {
13391 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
13392 	struct bnxt_hw_rings hwr = {0};
13393 	int rx_rings = rx;
13394 
13395 	if (tcs)
13396 		tx_sets = tcs;
13397 
13398 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
13399 
13400 	if (max_rx < rx_rings)
13401 		return -ENOMEM;
13402 
13403 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
13404 		rx_rings <<= 1;
13405 
13406 	hwr.rx = rx_rings;
13407 	hwr.tx = tx * tx_sets + tx_xdp;
13408 	if (max_tx < hwr.tx)
13409 		return -ENOMEM;
13410 
13411 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
13412 
13413 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
13414 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
13415 	if (max_cp < hwr.cp)
13416 		return -ENOMEM;
13417 	hwr.stat = hwr.cp;
13418 	if (BNXT_NEW_RM(bp)) {
13419 		hwr.cp += bnxt_get_ulp_msix_num(bp);
13420 		hwr.stat += bnxt_get_ulp_stat_ctxs(bp);
13421 		hwr.grp = rx;
13422 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13423 	}
13424 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
13425 		hwr.cp_p5 = hwr.tx + rx;
13426 	return bnxt_hwrm_check_rings(bp, &hwr);
13427 }
13428 
13429 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
13430 {
13431 	if (bp->bar2) {
13432 		pci_iounmap(pdev, bp->bar2);
13433 		bp->bar2 = NULL;
13434 	}
13435 
13436 	if (bp->bar1) {
13437 		pci_iounmap(pdev, bp->bar1);
13438 		bp->bar1 = NULL;
13439 	}
13440 
13441 	if (bp->bar0) {
13442 		pci_iounmap(pdev, bp->bar0);
13443 		bp->bar0 = NULL;
13444 	}
13445 }
13446 
13447 static void bnxt_cleanup_pci(struct bnxt *bp)
13448 {
13449 	bnxt_unmap_bars(bp, bp->pdev);
13450 	pci_release_regions(bp->pdev);
13451 	if (pci_is_enabled(bp->pdev))
13452 		pci_disable_device(bp->pdev);
13453 }
13454 
13455 static void bnxt_init_dflt_coal(struct bnxt *bp)
13456 {
13457 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
13458 	struct bnxt_coal *coal;
13459 	u16 flags = 0;
13460 
13461 	if (coal_cap->cmpl_params &
13462 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
13463 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
13464 
13465 	/* Tick values in micro seconds.
13466 	 * 1 coal_buf x bufs_per_record = 1 completion record.
13467 	 */
13468 	coal = &bp->rx_coal;
13469 	coal->coal_ticks = 10;
13470 	coal->coal_bufs = 30;
13471 	coal->coal_ticks_irq = 1;
13472 	coal->coal_bufs_irq = 2;
13473 	coal->idle_thresh = 50;
13474 	coal->bufs_per_record = 2;
13475 	coal->budget = 64;		/* NAPI budget */
13476 	coal->flags = flags;
13477 
13478 	coal = &bp->tx_coal;
13479 	coal->coal_ticks = 28;
13480 	coal->coal_bufs = 30;
13481 	coal->coal_ticks_irq = 2;
13482 	coal->coal_bufs_irq = 2;
13483 	coal->bufs_per_record = 1;
13484 	coal->flags = flags;
13485 
13486 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
13487 }
13488 
13489 /* FW that pre-reserves 1 VNIC per function */
13490 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
13491 {
13492 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
13493 
13494 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13495 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
13496 		return true;
13497 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13498 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
13499 		return true;
13500 	return false;
13501 }
13502 
13503 static int bnxt_fw_init_one_p1(struct bnxt *bp)
13504 {
13505 	int rc;
13506 
13507 	bp->fw_cap = 0;
13508 	rc = bnxt_hwrm_ver_get(bp);
13509 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
13510 	 * so wait before continuing with recovery.
13511 	 */
13512 	if (rc)
13513 		msleep(100);
13514 	bnxt_try_map_fw_health_reg(bp);
13515 	if (rc) {
13516 		rc = bnxt_try_recover_fw(bp);
13517 		if (rc)
13518 			return rc;
13519 		rc = bnxt_hwrm_ver_get(bp);
13520 		if (rc)
13521 			return rc;
13522 	}
13523 
13524 	bnxt_nvm_cfg_ver_get(bp);
13525 
13526 	rc = bnxt_hwrm_func_reset(bp);
13527 	if (rc)
13528 		return -ENODEV;
13529 
13530 	bnxt_hwrm_fw_set_time(bp);
13531 	return 0;
13532 }
13533 
13534 static int bnxt_fw_init_one_p2(struct bnxt *bp)
13535 {
13536 	int rc;
13537 
13538 	/* Get the MAX capabilities for this function */
13539 	rc = bnxt_hwrm_func_qcaps(bp);
13540 	if (rc) {
13541 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
13542 			   rc);
13543 		return -ENODEV;
13544 	}
13545 
13546 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
13547 	if (rc)
13548 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
13549 			    rc);
13550 
13551 	if (bnxt_alloc_fw_health(bp)) {
13552 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
13553 	} else {
13554 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
13555 		if (rc)
13556 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
13557 				    rc);
13558 	}
13559 
13560 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
13561 	if (rc)
13562 		return -ENODEV;
13563 
13564 	if (bnxt_fw_pre_resv_vnics(bp))
13565 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
13566 
13567 	bnxt_hwrm_func_qcfg(bp);
13568 	bnxt_hwrm_vnic_qcaps(bp);
13569 	bnxt_hwrm_port_led_qcaps(bp);
13570 	bnxt_ethtool_init(bp);
13571 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
13572 		__bnxt_hwrm_ptp_qcfg(bp);
13573 	bnxt_dcb_init(bp);
13574 	bnxt_hwmon_init(bp);
13575 	return 0;
13576 }
13577 
13578 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
13579 {
13580 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
13581 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
13582 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
13583 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
13584 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
13585 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
13586 		bp->rss_hash_delta = bp->rss_hash_cfg;
13587 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
13588 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
13589 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
13590 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
13591 	}
13592 }
13593 
13594 static void bnxt_set_dflt_rfs(struct bnxt *bp)
13595 {
13596 	struct net_device *dev = bp->dev;
13597 
13598 	dev->hw_features &= ~NETIF_F_NTUPLE;
13599 	dev->features &= ~NETIF_F_NTUPLE;
13600 	bp->flags &= ~BNXT_FLAG_RFS;
13601 	if (bnxt_rfs_supported(bp)) {
13602 		dev->hw_features |= NETIF_F_NTUPLE;
13603 		if (bnxt_rfs_capable(bp)) {
13604 			bp->flags |= BNXT_FLAG_RFS;
13605 			dev->features |= NETIF_F_NTUPLE;
13606 		}
13607 	}
13608 }
13609 
13610 static void bnxt_fw_init_one_p3(struct bnxt *bp)
13611 {
13612 	struct pci_dev *pdev = bp->pdev;
13613 
13614 	bnxt_set_dflt_rss_hash_type(bp);
13615 	bnxt_set_dflt_rfs(bp);
13616 
13617 	bnxt_get_wol_settings(bp);
13618 	if (bp->flags & BNXT_FLAG_WOL_CAP)
13619 		device_set_wakeup_enable(&pdev->dev, bp->wol);
13620 	else
13621 		device_set_wakeup_capable(&pdev->dev, false);
13622 
13623 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
13624 	bnxt_hwrm_coal_params_qcaps(bp);
13625 }
13626 
13627 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
13628 
13629 int bnxt_fw_init_one(struct bnxt *bp)
13630 {
13631 	int rc;
13632 
13633 	rc = bnxt_fw_init_one_p1(bp);
13634 	if (rc) {
13635 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
13636 		return rc;
13637 	}
13638 	rc = bnxt_fw_init_one_p2(bp);
13639 	if (rc) {
13640 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
13641 		return rc;
13642 	}
13643 	rc = bnxt_probe_phy(bp, false);
13644 	if (rc)
13645 		return rc;
13646 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
13647 	if (rc)
13648 		return rc;
13649 
13650 	bnxt_fw_init_one_p3(bp);
13651 	return 0;
13652 }
13653 
13654 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
13655 {
13656 	struct bnxt_fw_health *fw_health = bp->fw_health;
13657 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
13658 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
13659 	u32 reg_type, reg_off, delay_msecs;
13660 
13661 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
13662 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
13663 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
13664 	switch (reg_type) {
13665 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
13666 		pci_write_config_dword(bp->pdev, reg_off, val);
13667 		break;
13668 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
13669 		writel(reg_off & BNXT_GRC_BASE_MASK,
13670 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
13671 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
13672 		fallthrough;
13673 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
13674 		writel(val, bp->bar0 + reg_off);
13675 		break;
13676 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
13677 		writel(val, bp->bar1 + reg_off);
13678 		break;
13679 	}
13680 	if (delay_msecs) {
13681 		pci_read_config_dword(bp->pdev, 0, &val);
13682 		msleep(delay_msecs);
13683 	}
13684 }
13685 
13686 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
13687 {
13688 	struct hwrm_func_qcfg_output *resp;
13689 	struct hwrm_func_qcfg_input *req;
13690 	bool result = true; /* firmware will enforce if unknown */
13691 
13692 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
13693 		return result;
13694 
13695 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
13696 		return result;
13697 
13698 	req->fid = cpu_to_le16(0xffff);
13699 	resp = hwrm_req_hold(bp, req);
13700 	if (!hwrm_req_send(bp, req))
13701 		result = !!(le16_to_cpu(resp->flags) &
13702 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
13703 	hwrm_req_drop(bp, req);
13704 	return result;
13705 }
13706 
13707 static void bnxt_reset_all(struct bnxt *bp)
13708 {
13709 	struct bnxt_fw_health *fw_health = bp->fw_health;
13710 	int i, rc;
13711 
13712 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
13713 		bnxt_fw_reset_via_optee(bp);
13714 		bp->fw_reset_timestamp = jiffies;
13715 		return;
13716 	}
13717 
13718 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
13719 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
13720 			bnxt_fw_reset_writel(bp, i);
13721 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
13722 		struct hwrm_fw_reset_input *req;
13723 
13724 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
13725 		if (!rc) {
13726 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
13727 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
13728 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
13729 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
13730 			rc = hwrm_req_send(bp, req);
13731 		}
13732 		if (rc != -ENODEV)
13733 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
13734 	}
13735 	bp->fw_reset_timestamp = jiffies;
13736 }
13737 
13738 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
13739 {
13740 	return time_after(jiffies, bp->fw_reset_timestamp +
13741 			  (bp->fw_reset_max_dsecs * HZ / 10));
13742 }
13743 
13744 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
13745 {
13746 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13747 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF) {
13748 		bnxt_ulp_start(bp, rc);
13749 		bnxt_dl_health_fw_status_update(bp, false);
13750 	}
13751 	bp->fw_reset_state = 0;
13752 	dev_close(bp->dev);
13753 }
13754 
13755 static void bnxt_fw_reset_task(struct work_struct *work)
13756 {
13757 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
13758 	int rc = 0;
13759 
13760 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13761 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
13762 		return;
13763 	}
13764 
13765 	switch (bp->fw_reset_state) {
13766 	case BNXT_FW_RESET_STATE_POLL_VF: {
13767 		int n = bnxt_get_registered_vfs(bp);
13768 		int tmo;
13769 
13770 		if (n < 0) {
13771 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
13772 				   n, jiffies_to_msecs(jiffies -
13773 				   bp->fw_reset_timestamp));
13774 			goto fw_reset_abort;
13775 		} else if (n > 0) {
13776 			if (bnxt_fw_reset_timeout(bp)) {
13777 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13778 				bp->fw_reset_state = 0;
13779 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
13780 					   n);
13781 				return;
13782 			}
13783 			bnxt_queue_fw_reset_work(bp, HZ / 10);
13784 			return;
13785 		}
13786 		bp->fw_reset_timestamp = jiffies;
13787 		rtnl_lock();
13788 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13789 			bnxt_fw_reset_abort(bp, rc);
13790 			rtnl_unlock();
13791 			return;
13792 		}
13793 		bnxt_fw_reset_close(bp);
13794 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
13795 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
13796 			tmo = HZ / 10;
13797 		} else {
13798 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13799 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
13800 		}
13801 		rtnl_unlock();
13802 		bnxt_queue_fw_reset_work(bp, tmo);
13803 		return;
13804 	}
13805 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
13806 		u32 val;
13807 
13808 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
13809 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
13810 		    !bnxt_fw_reset_timeout(bp)) {
13811 			bnxt_queue_fw_reset_work(bp, HZ / 5);
13812 			return;
13813 		}
13814 
13815 		if (!bp->fw_health->primary) {
13816 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
13817 
13818 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13819 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
13820 			return;
13821 		}
13822 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
13823 	}
13824 		fallthrough;
13825 	case BNXT_FW_RESET_STATE_RESET_FW:
13826 		bnxt_reset_all(bp);
13827 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13828 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
13829 		return;
13830 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
13831 		bnxt_inv_fw_health_reg(bp);
13832 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
13833 		    !bp->fw_reset_min_dsecs) {
13834 			u16 val;
13835 
13836 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
13837 			if (val == 0xffff) {
13838 				if (bnxt_fw_reset_timeout(bp)) {
13839 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
13840 					rc = -ETIMEDOUT;
13841 					goto fw_reset_abort;
13842 				}
13843 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
13844 				return;
13845 			}
13846 		}
13847 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
13848 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
13849 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
13850 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
13851 			bnxt_dl_remote_reload(bp);
13852 		if (pci_enable_device(bp->pdev)) {
13853 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
13854 			rc = -ENODEV;
13855 			goto fw_reset_abort;
13856 		}
13857 		pci_set_master(bp->pdev);
13858 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
13859 		fallthrough;
13860 	case BNXT_FW_RESET_STATE_POLL_FW:
13861 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
13862 		rc = bnxt_hwrm_poll(bp);
13863 		if (rc) {
13864 			if (bnxt_fw_reset_timeout(bp)) {
13865 				netdev_err(bp->dev, "Firmware reset aborted\n");
13866 				goto fw_reset_abort_status;
13867 			}
13868 			bnxt_queue_fw_reset_work(bp, HZ / 5);
13869 			return;
13870 		}
13871 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
13872 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
13873 		fallthrough;
13874 	case BNXT_FW_RESET_STATE_OPENING:
13875 		while (!rtnl_trylock()) {
13876 			bnxt_queue_fw_reset_work(bp, HZ / 10);
13877 			return;
13878 		}
13879 		rc = bnxt_open(bp->dev);
13880 		if (rc) {
13881 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
13882 			bnxt_fw_reset_abort(bp, rc);
13883 			rtnl_unlock();
13884 			return;
13885 		}
13886 
13887 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
13888 		    bp->fw_health->enabled) {
13889 			bp->fw_health->last_fw_reset_cnt =
13890 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13891 		}
13892 		bp->fw_reset_state = 0;
13893 		/* Make sure fw_reset_state is 0 before clearing the flag */
13894 		smp_mb__before_atomic();
13895 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13896 		bnxt_ulp_start(bp, 0);
13897 		bnxt_reenable_sriov(bp);
13898 		bnxt_vf_reps_alloc(bp);
13899 		bnxt_vf_reps_open(bp);
13900 		bnxt_ptp_reapply_pps(bp);
13901 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
13902 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
13903 			bnxt_dl_health_fw_recovery_done(bp);
13904 			bnxt_dl_health_fw_status_update(bp, true);
13905 		}
13906 		rtnl_unlock();
13907 		break;
13908 	}
13909 	return;
13910 
13911 fw_reset_abort_status:
13912 	if (bp->fw_health->status_reliable ||
13913 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
13914 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
13915 
13916 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
13917 	}
13918 fw_reset_abort:
13919 	rtnl_lock();
13920 	bnxt_fw_reset_abort(bp, rc);
13921 	rtnl_unlock();
13922 }
13923 
13924 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
13925 {
13926 	int rc;
13927 	struct bnxt *bp = netdev_priv(dev);
13928 
13929 	SET_NETDEV_DEV(dev, &pdev->dev);
13930 
13931 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
13932 	rc = pci_enable_device(pdev);
13933 	if (rc) {
13934 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
13935 		goto init_err;
13936 	}
13937 
13938 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
13939 		dev_err(&pdev->dev,
13940 			"Cannot find PCI device base address, aborting\n");
13941 		rc = -ENODEV;
13942 		goto init_err_disable;
13943 	}
13944 
13945 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
13946 	if (rc) {
13947 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
13948 		goto init_err_disable;
13949 	}
13950 
13951 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
13952 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
13953 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
13954 		rc = -EIO;
13955 		goto init_err_release;
13956 	}
13957 
13958 	pci_set_master(pdev);
13959 
13960 	bp->dev = dev;
13961 	bp->pdev = pdev;
13962 
13963 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
13964 	 * determines the BAR size.
13965 	 */
13966 	bp->bar0 = pci_ioremap_bar(pdev, 0);
13967 	if (!bp->bar0) {
13968 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
13969 		rc = -ENOMEM;
13970 		goto init_err_release;
13971 	}
13972 
13973 	bp->bar2 = pci_ioremap_bar(pdev, 4);
13974 	if (!bp->bar2) {
13975 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
13976 		rc = -ENOMEM;
13977 		goto init_err_release;
13978 	}
13979 
13980 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
13981 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
13982 
13983 	spin_lock_init(&bp->ntp_fltr_lock);
13984 #if BITS_PER_LONG == 32
13985 	spin_lock_init(&bp->db_lock);
13986 #endif
13987 
13988 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
13989 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
13990 
13991 	timer_setup(&bp->timer, bnxt_timer, 0);
13992 	bp->current_interval = BNXT_TIMER_INTERVAL;
13993 
13994 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
13995 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
13996 
13997 	clear_bit(BNXT_STATE_OPEN, &bp->state);
13998 	return 0;
13999 
14000 init_err_release:
14001 	bnxt_unmap_bars(bp, pdev);
14002 	pci_release_regions(pdev);
14003 
14004 init_err_disable:
14005 	pci_disable_device(pdev);
14006 
14007 init_err:
14008 	return rc;
14009 }
14010 
14011 /* rtnl_lock held */
14012 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
14013 {
14014 	struct sockaddr *addr = p;
14015 	struct bnxt *bp = netdev_priv(dev);
14016 	int rc = 0;
14017 
14018 	if (!is_valid_ether_addr(addr->sa_data))
14019 		return -EADDRNOTAVAIL;
14020 
14021 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
14022 		return 0;
14023 
14024 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
14025 	if (rc)
14026 		return rc;
14027 
14028 	eth_hw_addr_set(dev, addr->sa_data);
14029 	bnxt_clear_usr_fltrs(bp, true);
14030 	if (netif_running(dev)) {
14031 		bnxt_close_nic(bp, false, false);
14032 		rc = bnxt_open_nic(bp, false, false);
14033 	}
14034 
14035 	return rc;
14036 }
14037 
14038 /* rtnl_lock held */
14039 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
14040 {
14041 	struct bnxt *bp = netdev_priv(dev);
14042 
14043 	if (netif_running(dev))
14044 		bnxt_close_nic(bp, true, false);
14045 
14046 	dev->mtu = new_mtu;
14047 	bnxt_set_ring_params(bp);
14048 
14049 	if (netif_running(dev))
14050 		return bnxt_open_nic(bp, true, false);
14051 
14052 	return 0;
14053 }
14054 
14055 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
14056 {
14057 	struct bnxt *bp = netdev_priv(dev);
14058 	bool sh = false;
14059 	int rc, tx_cp;
14060 
14061 	if (tc > bp->max_tc) {
14062 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
14063 			   tc, bp->max_tc);
14064 		return -EINVAL;
14065 	}
14066 
14067 	if (bp->num_tc == tc)
14068 		return 0;
14069 
14070 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
14071 		sh = true;
14072 
14073 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
14074 			      sh, tc, bp->tx_nr_rings_xdp);
14075 	if (rc)
14076 		return rc;
14077 
14078 	/* Needs to close the device and do hw resource re-allocations */
14079 	if (netif_running(bp->dev))
14080 		bnxt_close_nic(bp, true, false);
14081 
14082 	if (tc) {
14083 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
14084 		netdev_set_num_tc(dev, tc);
14085 		bp->num_tc = tc;
14086 	} else {
14087 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
14088 		netdev_reset_tc(dev);
14089 		bp->num_tc = 0;
14090 	}
14091 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
14092 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
14093 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
14094 			       tx_cp + bp->rx_nr_rings;
14095 
14096 	if (netif_running(bp->dev))
14097 		return bnxt_open_nic(bp, true, false);
14098 
14099 	return 0;
14100 }
14101 
14102 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
14103 				  void *cb_priv)
14104 {
14105 	struct bnxt *bp = cb_priv;
14106 
14107 	if (!bnxt_tc_flower_enabled(bp) ||
14108 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
14109 		return -EOPNOTSUPP;
14110 
14111 	switch (type) {
14112 	case TC_SETUP_CLSFLOWER:
14113 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
14114 	default:
14115 		return -EOPNOTSUPP;
14116 	}
14117 }
14118 
14119 LIST_HEAD(bnxt_block_cb_list);
14120 
14121 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
14122 			 void *type_data)
14123 {
14124 	struct bnxt *bp = netdev_priv(dev);
14125 
14126 	switch (type) {
14127 	case TC_SETUP_BLOCK:
14128 		return flow_block_cb_setup_simple(type_data,
14129 						  &bnxt_block_cb_list,
14130 						  bnxt_setup_tc_block_cb,
14131 						  bp, bp, true);
14132 	case TC_SETUP_QDISC_MQPRIO: {
14133 		struct tc_mqprio_qopt *mqprio = type_data;
14134 
14135 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
14136 
14137 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
14138 	}
14139 	default:
14140 		return -EOPNOTSUPP;
14141 	}
14142 }
14143 
14144 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
14145 			    const struct sk_buff *skb)
14146 {
14147 	struct bnxt_vnic_info *vnic;
14148 
14149 	if (skb)
14150 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
14151 
14152 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
14153 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
14154 }
14155 
14156 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
14157 			   u32 idx)
14158 {
14159 	struct hlist_head *head;
14160 	int bit_id;
14161 
14162 	spin_lock_bh(&bp->ntp_fltr_lock);
14163 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
14164 	if (bit_id < 0) {
14165 		spin_unlock_bh(&bp->ntp_fltr_lock);
14166 		return -ENOMEM;
14167 	}
14168 
14169 	fltr->base.sw_id = (u16)bit_id;
14170 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
14171 	fltr->base.flags |= BNXT_ACT_RING_DST;
14172 	head = &bp->ntp_fltr_hash_tbl[idx];
14173 	hlist_add_head_rcu(&fltr->base.hash, head);
14174 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
14175 	bnxt_insert_usr_fltr(bp, &fltr->base);
14176 	bp->ntp_fltr_count++;
14177 	spin_unlock_bh(&bp->ntp_fltr_lock);
14178 	return 0;
14179 }
14180 
14181 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
14182 			    struct bnxt_ntuple_filter *f2)
14183 {
14184 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
14185 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
14186 	struct flow_keys *keys1 = &f1->fkeys;
14187 	struct flow_keys *keys2 = &f2->fkeys;
14188 
14189 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
14190 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
14191 		return false;
14192 
14193 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
14194 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
14195 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
14196 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
14197 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
14198 			return false;
14199 	} else {
14200 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
14201 				     &keys2->addrs.v6addrs.src) ||
14202 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
14203 				     &masks2->addrs.v6addrs.src) ||
14204 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
14205 				     &keys2->addrs.v6addrs.dst) ||
14206 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
14207 				     &masks2->addrs.v6addrs.dst))
14208 			return false;
14209 	}
14210 
14211 	return keys1->ports.src == keys2->ports.src &&
14212 	       masks1->ports.src == masks2->ports.src &&
14213 	       keys1->ports.dst == keys2->ports.dst &&
14214 	       masks1->ports.dst == masks2->ports.dst &&
14215 	       keys1->control.flags == keys2->control.flags &&
14216 	       f1->l2_fltr == f2->l2_fltr;
14217 }
14218 
14219 struct bnxt_ntuple_filter *
14220 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
14221 				struct bnxt_ntuple_filter *fltr, u32 idx)
14222 {
14223 	struct bnxt_ntuple_filter *f;
14224 	struct hlist_head *head;
14225 
14226 	head = &bp->ntp_fltr_hash_tbl[idx];
14227 	hlist_for_each_entry_rcu(f, head, base.hash) {
14228 		if (bnxt_fltr_match(f, fltr))
14229 			return f;
14230 	}
14231 	return NULL;
14232 }
14233 
14234 #ifdef CONFIG_RFS_ACCEL
14235 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
14236 			      u16 rxq_index, u32 flow_id)
14237 {
14238 	struct bnxt *bp = netdev_priv(dev);
14239 	struct bnxt_ntuple_filter *fltr, *new_fltr;
14240 	struct flow_keys *fkeys;
14241 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
14242 	struct bnxt_l2_filter *l2_fltr;
14243 	int rc = 0, idx;
14244 	u32 flags;
14245 
14246 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
14247 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
14248 		atomic_inc(&l2_fltr->refcnt);
14249 	} else {
14250 		struct bnxt_l2_key key;
14251 
14252 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
14253 		key.vlan = 0;
14254 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
14255 		if (!l2_fltr)
14256 			return -EINVAL;
14257 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
14258 			bnxt_del_l2_filter(bp, l2_fltr);
14259 			return -EINVAL;
14260 		}
14261 	}
14262 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
14263 	if (!new_fltr) {
14264 		bnxt_del_l2_filter(bp, l2_fltr);
14265 		return -ENOMEM;
14266 	}
14267 
14268 	fkeys = &new_fltr->fkeys;
14269 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
14270 		rc = -EPROTONOSUPPORT;
14271 		goto err_free;
14272 	}
14273 
14274 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
14275 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
14276 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
14277 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
14278 		rc = -EPROTONOSUPPORT;
14279 		goto err_free;
14280 	}
14281 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
14282 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
14283 		if (bp->hwrm_spec_code < 0x10601) {
14284 			rc = -EPROTONOSUPPORT;
14285 			goto err_free;
14286 		}
14287 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
14288 	}
14289 	flags = fkeys->control.flags;
14290 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
14291 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
14292 		rc = -EPROTONOSUPPORT;
14293 		goto err_free;
14294 	}
14295 	new_fltr->l2_fltr = l2_fltr;
14296 
14297 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
14298 	rcu_read_lock();
14299 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
14300 	if (fltr) {
14301 		rc = fltr->base.sw_id;
14302 		rcu_read_unlock();
14303 		goto err_free;
14304 	}
14305 	rcu_read_unlock();
14306 
14307 	new_fltr->flow_id = flow_id;
14308 	new_fltr->base.rxq = rxq_index;
14309 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
14310 	if (!rc) {
14311 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14312 		return new_fltr->base.sw_id;
14313 	}
14314 
14315 err_free:
14316 	bnxt_del_l2_filter(bp, l2_fltr);
14317 	kfree(new_fltr);
14318 	return rc;
14319 }
14320 #endif
14321 
14322 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
14323 {
14324 	spin_lock_bh(&bp->ntp_fltr_lock);
14325 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
14326 		spin_unlock_bh(&bp->ntp_fltr_lock);
14327 		return;
14328 	}
14329 	hlist_del_rcu(&fltr->base.hash);
14330 	bnxt_del_one_usr_fltr(bp, &fltr->base);
14331 	bp->ntp_fltr_count--;
14332 	spin_unlock_bh(&bp->ntp_fltr_lock);
14333 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
14334 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
14335 	kfree_rcu(fltr, base.rcu);
14336 }
14337 
14338 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
14339 {
14340 #ifdef CONFIG_RFS_ACCEL
14341 	int i;
14342 
14343 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
14344 		struct hlist_head *head;
14345 		struct hlist_node *tmp;
14346 		struct bnxt_ntuple_filter *fltr;
14347 		int rc;
14348 
14349 		head = &bp->ntp_fltr_hash_tbl[i];
14350 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
14351 			bool del = false;
14352 
14353 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
14354 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
14355 					continue;
14356 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
14357 							fltr->flow_id,
14358 							fltr->base.sw_id)) {
14359 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
14360 									 fltr);
14361 					del = true;
14362 				}
14363 			} else {
14364 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
14365 								       fltr);
14366 				if (rc)
14367 					del = true;
14368 				else
14369 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
14370 			}
14371 
14372 			if (del)
14373 				bnxt_del_ntp_filter(bp, fltr);
14374 		}
14375 	}
14376 #endif
14377 }
14378 
14379 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
14380 				    unsigned int entry, struct udp_tunnel_info *ti)
14381 {
14382 	struct bnxt *bp = netdev_priv(netdev);
14383 	unsigned int cmd;
14384 
14385 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
14386 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
14387 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
14388 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
14389 	else
14390 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
14391 
14392 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
14393 }
14394 
14395 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
14396 				      unsigned int entry, struct udp_tunnel_info *ti)
14397 {
14398 	struct bnxt *bp = netdev_priv(netdev);
14399 	unsigned int cmd;
14400 
14401 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
14402 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
14403 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
14404 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
14405 	else
14406 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
14407 
14408 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
14409 }
14410 
14411 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
14412 	.set_port	= bnxt_udp_tunnel_set_port,
14413 	.unset_port	= bnxt_udp_tunnel_unset_port,
14414 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
14415 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
14416 	.tables		= {
14417 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
14418 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
14419 	},
14420 }, bnxt_udp_tunnels_p7 = {
14421 	.set_port	= bnxt_udp_tunnel_set_port,
14422 	.unset_port	= bnxt_udp_tunnel_unset_port,
14423 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
14424 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
14425 	.tables		= {
14426 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
14427 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
14428 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
14429 	},
14430 };
14431 
14432 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
14433 			       struct net_device *dev, u32 filter_mask,
14434 			       int nlflags)
14435 {
14436 	struct bnxt *bp = netdev_priv(dev);
14437 
14438 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
14439 				       nlflags, filter_mask, NULL);
14440 }
14441 
14442 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
14443 			       u16 flags, struct netlink_ext_ack *extack)
14444 {
14445 	struct bnxt *bp = netdev_priv(dev);
14446 	struct nlattr *attr, *br_spec;
14447 	int rem, rc = 0;
14448 
14449 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
14450 		return -EOPNOTSUPP;
14451 
14452 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
14453 	if (!br_spec)
14454 		return -EINVAL;
14455 
14456 	nla_for_each_nested(attr, br_spec, rem) {
14457 		u16 mode;
14458 
14459 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
14460 			continue;
14461 
14462 		mode = nla_get_u16(attr);
14463 		if (mode == bp->br_mode)
14464 			break;
14465 
14466 		rc = bnxt_hwrm_set_br_mode(bp, mode);
14467 		if (!rc)
14468 			bp->br_mode = mode;
14469 		break;
14470 	}
14471 	return rc;
14472 }
14473 
14474 int bnxt_get_port_parent_id(struct net_device *dev,
14475 			    struct netdev_phys_item_id *ppid)
14476 {
14477 	struct bnxt *bp = netdev_priv(dev);
14478 
14479 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
14480 		return -EOPNOTSUPP;
14481 
14482 	/* The PF and it's VF-reps only support the switchdev framework */
14483 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
14484 		return -EOPNOTSUPP;
14485 
14486 	ppid->id_len = sizeof(bp->dsn);
14487 	memcpy(ppid->id, bp->dsn, ppid->id_len);
14488 
14489 	return 0;
14490 }
14491 
14492 static const struct net_device_ops bnxt_netdev_ops = {
14493 	.ndo_open		= bnxt_open,
14494 	.ndo_start_xmit		= bnxt_start_xmit,
14495 	.ndo_stop		= bnxt_close,
14496 	.ndo_get_stats64	= bnxt_get_stats64,
14497 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
14498 	.ndo_eth_ioctl		= bnxt_ioctl,
14499 	.ndo_validate_addr	= eth_validate_addr,
14500 	.ndo_set_mac_address	= bnxt_change_mac_addr,
14501 	.ndo_change_mtu		= bnxt_change_mtu,
14502 	.ndo_fix_features	= bnxt_fix_features,
14503 	.ndo_set_features	= bnxt_set_features,
14504 	.ndo_features_check	= bnxt_features_check,
14505 	.ndo_tx_timeout		= bnxt_tx_timeout,
14506 #ifdef CONFIG_BNXT_SRIOV
14507 	.ndo_get_vf_config	= bnxt_get_vf_config,
14508 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
14509 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
14510 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
14511 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
14512 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
14513 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
14514 #endif
14515 	.ndo_setup_tc           = bnxt_setup_tc,
14516 #ifdef CONFIG_RFS_ACCEL
14517 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
14518 #endif
14519 	.ndo_bpf		= bnxt_xdp,
14520 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
14521 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
14522 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
14523 };
14524 
14525 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
14526 				    struct netdev_queue_stats_rx *stats)
14527 {
14528 	struct bnxt *bp = netdev_priv(dev);
14529 	struct bnxt_cp_ring_info *cpr;
14530 	u64 *sw;
14531 
14532 	cpr = &bp->bnapi[i]->cp_ring;
14533 	sw = cpr->stats.sw_stats;
14534 
14535 	stats->packets = 0;
14536 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
14537 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
14538 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
14539 
14540 	stats->bytes = 0;
14541 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
14542 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
14543 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
14544 
14545 	stats->alloc_fail = cpr->sw_stats.rx.rx_oom_discards;
14546 }
14547 
14548 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
14549 				    struct netdev_queue_stats_tx *stats)
14550 {
14551 	struct bnxt *bp = netdev_priv(dev);
14552 	struct bnxt_napi *bnapi;
14553 	u64 *sw;
14554 
14555 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
14556 	sw = bnapi->cp_ring.stats.sw_stats;
14557 
14558 	stats->packets = 0;
14559 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
14560 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
14561 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
14562 
14563 	stats->bytes = 0;
14564 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
14565 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
14566 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
14567 }
14568 
14569 static void bnxt_get_base_stats(struct net_device *dev,
14570 				struct netdev_queue_stats_rx *rx,
14571 				struct netdev_queue_stats_tx *tx)
14572 {
14573 	struct bnxt *bp = netdev_priv(dev);
14574 
14575 	rx->packets = bp->net_stats_prev.rx_packets;
14576 	rx->bytes = bp->net_stats_prev.rx_bytes;
14577 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
14578 
14579 	tx->packets = bp->net_stats_prev.tx_packets;
14580 	tx->bytes = bp->net_stats_prev.tx_bytes;
14581 }
14582 
14583 static const struct netdev_stat_ops bnxt_stat_ops = {
14584 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
14585 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
14586 	.get_base_stats		= bnxt_get_base_stats,
14587 };
14588 
14589 static void bnxt_remove_one(struct pci_dev *pdev)
14590 {
14591 	struct net_device *dev = pci_get_drvdata(pdev);
14592 	struct bnxt *bp = netdev_priv(dev);
14593 
14594 	if (BNXT_PF(bp))
14595 		bnxt_sriov_disable(bp);
14596 
14597 	bnxt_rdma_aux_device_uninit(bp);
14598 
14599 	bnxt_ptp_clear(bp);
14600 	unregister_netdev(dev);
14601 	bnxt_free_l2_filters(bp, true);
14602 	bnxt_free_ntp_fltrs(bp, true);
14603 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14604 	/* Flush any pending tasks */
14605 	cancel_work_sync(&bp->sp_task);
14606 	cancel_delayed_work_sync(&bp->fw_reset_task);
14607 	bp->sp_event = 0;
14608 
14609 	bnxt_dl_fw_reporters_destroy(bp);
14610 	bnxt_dl_unregister(bp);
14611 	bnxt_shutdown_tc(bp);
14612 
14613 	bnxt_clear_int_mode(bp);
14614 	bnxt_hwrm_func_drv_unrgtr(bp);
14615 	bnxt_free_hwrm_resources(bp);
14616 	bnxt_hwmon_uninit(bp);
14617 	bnxt_ethtool_free(bp);
14618 	bnxt_dcb_free(bp);
14619 	kfree(bp->ptp_cfg);
14620 	bp->ptp_cfg = NULL;
14621 	kfree(bp->fw_health);
14622 	bp->fw_health = NULL;
14623 	bnxt_cleanup_pci(bp);
14624 	bnxt_free_ctx_mem(bp);
14625 	kfree(bp->rss_indir_tbl);
14626 	bp->rss_indir_tbl = NULL;
14627 	bnxt_free_port_stats(bp);
14628 	free_netdev(dev);
14629 }
14630 
14631 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
14632 {
14633 	int rc = 0;
14634 	struct bnxt_link_info *link_info = &bp->link_info;
14635 
14636 	bp->phy_flags = 0;
14637 	rc = bnxt_hwrm_phy_qcaps(bp);
14638 	if (rc) {
14639 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
14640 			   rc);
14641 		return rc;
14642 	}
14643 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
14644 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
14645 	else
14646 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
14647 	if (!fw_dflt)
14648 		return 0;
14649 
14650 	mutex_lock(&bp->link_lock);
14651 	rc = bnxt_update_link(bp, false);
14652 	if (rc) {
14653 		mutex_unlock(&bp->link_lock);
14654 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
14655 			   rc);
14656 		return rc;
14657 	}
14658 
14659 	/* Older firmware does not have supported_auto_speeds, so assume
14660 	 * that all supported speeds can be autonegotiated.
14661 	 */
14662 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
14663 		link_info->support_auto_speeds = link_info->support_speeds;
14664 
14665 	bnxt_init_ethtool_link_settings(bp);
14666 	mutex_unlock(&bp->link_lock);
14667 	return 0;
14668 }
14669 
14670 static int bnxt_get_max_irq(struct pci_dev *pdev)
14671 {
14672 	u16 ctrl;
14673 
14674 	if (!pdev->msix_cap)
14675 		return 1;
14676 
14677 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
14678 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
14679 }
14680 
14681 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14682 				int *max_cp)
14683 {
14684 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
14685 	int max_ring_grps = 0, max_irq;
14686 
14687 	*max_tx = hw_resc->max_tx_rings;
14688 	*max_rx = hw_resc->max_rx_rings;
14689 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
14690 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
14691 			bnxt_get_ulp_msix_num(bp),
14692 			hw_resc->max_stat_ctxs - bnxt_get_ulp_stat_ctxs(bp));
14693 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14694 		*max_cp = min_t(int, *max_cp, max_irq);
14695 	max_ring_grps = hw_resc->max_hw_ring_grps;
14696 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
14697 		*max_cp -= 1;
14698 		*max_rx -= 2;
14699 	}
14700 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14701 		*max_rx >>= 1;
14702 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
14703 		int rc;
14704 
14705 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
14706 		if (rc) {
14707 			*max_rx = 0;
14708 			*max_tx = 0;
14709 		}
14710 		/* On P5 chips, max_cp output param should be available NQs */
14711 		*max_cp = max_irq;
14712 	}
14713 	*max_rx = min_t(int, *max_rx, max_ring_grps);
14714 }
14715 
14716 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
14717 {
14718 	int rx, tx, cp;
14719 
14720 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
14721 	*max_rx = rx;
14722 	*max_tx = tx;
14723 	if (!rx || !tx || !cp)
14724 		return -ENOMEM;
14725 
14726 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
14727 }
14728 
14729 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14730 			       bool shared)
14731 {
14732 	int rc;
14733 
14734 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
14735 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
14736 		/* Not enough rings, try disabling agg rings. */
14737 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
14738 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
14739 		if (rc) {
14740 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
14741 			bp->flags |= BNXT_FLAG_AGG_RINGS;
14742 			return rc;
14743 		}
14744 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
14745 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
14746 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
14747 		bnxt_set_ring_params(bp);
14748 	}
14749 
14750 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
14751 		int max_cp, max_stat, max_irq;
14752 
14753 		/* Reserve minimum resources for RoCE */
14754 		max_cp = bnxt_get_max_func_cp_rings(bp);
14755 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
14756 		max_irq = bnxt_get_max_func_irqs(bp);
14757 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
14758 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
14759 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
14760 			return 0;
14761 
14762 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
14763 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
14764 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
14765 		max_cp = min_t(int, max_cp, max_irq);
14766 		max_cp = min_t(int, max_cp, max_stat);
14767 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
14768 		if (rc)
14769 			rc = 0;
14770 	}
14771 	return rc;
14772 }
14773 
14774 /* In initial default shared ring setting, each shared ring must have a
14775  * RX/TX ring pair.
14776  */
14777 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
14778 {
14779 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
14780 	bp->rx_nr_rings = bp->cp_nr_rings;
14781 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
14782 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
14783 }
14784 
14785 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
14786 {
14787 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
14788 
14789 	if (!bnxt_can_reserve_rings(bp))
14790 		return 0;
14791 
14792 	if (sh)
14793 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
14794 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
14795 	/* Reduce default rings on multi-port cards so that total default
14796 	 * rings do not exceed CPU count.
14797 	 */
14798 	if (bp->port_count > 1) {
14799 		int max_rings =
14800 			max_t(int, num_online_cpus() / bp->port_count, 1);
14801 
14802 		dflt_rings = min_t(int, dflt_rings, max_rings);
14803 	}
14804 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
14805 	if (rc)
14806 		return rc;
14807 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
14808 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
14809 	if (sh)
14810 		bnxt_trim_dflt_sh_rings(bp);
14811 	else
14812 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
14813 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
14814 
14815 	rc = __bnxt_reserve_rings(bp);
14816 	if (rc && rc != -ENODEV)
14817 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
14818 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
14819 	if (sh)
14820 		bnxt_trim_dflt_sh_rings(bp);
14821 
14822 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
14823 	if (bnxt_need_reserve_rings(bp)) {
14824 		rc = __bnxt_reserve_rings(bp);
14825 		if (rc && rc != -ENODEV)
14826 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
14827 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
14828 	}
14829 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
14830 		bp->rx_nr_rings++;
14831 		bp->cp_nr_rings++;
14832 	}
14833 	if (rc) {
14834 		bp->tx_nr_rings = 0;
14835 		bp->rx_nr_rings = 0;
14836 	}
14837 	return rc;
14838 }
14839 
14840 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
14841 {
14842 	int rc;
14843 
14844 	if (bp->tx_nr_rings)
14845 		return 0;
14846 
14847 	bnxt_ulp_irq_stop(bp);
14848 	bnxt_clear_int_mode(bp);
14849 	rc = bnxt_set_dflt_rings(bp, true);
14850 	if (rc) {
14851 		if (BNXT_VF(bp) && rc == -ENODEV)
14852 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
14853 		else
14854 			netdev_err(bp->dev, "Not enough rings available.\n");
14855 		goto init_dflt_ring_err;
14856 	}
14857 	rc = bnxt_init_int_mode(bp);
14858 	if (rc)
14859 		goto init_dflt_ring_err;
14860 
14861 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
14862 
14863 	bnxt_set_dflt_rfs(bp);
14864 
14865 init_dflt_ring_err:
14866 	bnxt_ulp_irq_restart(bp, rc);
14867 	return rc;
14868 }
14869 
14870 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
14871 {
14872 	int rc;
14873 
14874 	ASSERT_RTNL();
14875 	bnxt_hwrm_func_qcaps(bp);
14876 
14877 	if (netif_running(bp->dev))
14878 		__bnxt_close_nic(bp, true, false);
14879 
14880 	bnxt_ulp_irq_stop(bp);
14881 	bnxt_clear_int_mode(bp);
14882 	rc = bnxt_init_int_mode(bp);
14883 	bnxt_ulp_irq_restart(bp, rc);
14884 
14885 	if (netif_running(bp->dev)) {
14886 		if (rc)
14887 			dev_close(bp->dev);
14888 		else
14889 			rc = bnxt_open_nic(bp, true, false);
14890 	}
14891 
14892 	return rc;
14893 }
14894 
14895 static int bnxt_init_mac_addr(struct bnxt *bp)
14896 {
14897 	int rc = 0;
14898 
14899 	if (BNXT_PF(bp)) {
14900 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
14901 	} else {
14902 #ifdef CONFIG_BNXT_SRIOV
14903 		struct bnxt_vf_info *vf = &bp->vf;
14904 		bool strict_approval = true;
14905 
14906 		if (is_valid_ether_addr(vf->mac_addr)) {
14907 			/* overwrite netdev dev_addr with admin VF MAC */
14908 			eth_hw_addr_set(bp->dev, vf->mac_addr);
14909 			/* Older PF driver or firmware may not approve this
14910 			 * correctly.
14911 			 */
14912 			strict_approval = false;
14913 		} else {
14914 			eth_hw_addr_random(bp->dev);
14915 		}
14916 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
14917 #endif
14918 	}
14919 	return rc;
14920 }
14921 
14922 static void bnxt_vpd_read_info(struct bnxt *bp)
14923 {
14924 	struct pci_dev *pdev = bp->pdev;
14925 	unsigned int vpd_size, kw_len;
14926 	int pos, size;
14927 	u8 *vpd_data;
14928 
14929 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
14930 	if (IS_ERR(vpd_data)) {
14931 		pci_warn(pdev, "Unable to read VPD\n");
14932 		return;
14933 	}
14934 
14935 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
14936 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
14937 	if (pos < 0)
14938 		goto read_sn;
14939 
14940 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
14941 	memcpy(bp->board_partno, &vpd_data[pos], size);
14942 
14943 read_sn:
14944 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
14945 					   PCI_VPD_RO_KEYWORD_SERIALNO,
14946 					   &kw_len);
14947 	if (pos < 0)
14948 		goto exit;
14949 
14950 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
14951 	memcpy(bp->board_serialno, &vpd_data[pos], size);
14952 exit:
14953 	kfree(vpd_data);
14954 }
14955 
14956 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
14957 {
14958 	struct pci_dev *pdev = bp->pdev;
14959 	u64 qword;
14960 
14961 	qword = pci_get_dsn(pdev);
14962 	if (!qword) {
14963 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
14964 		return -EOPNOTSUPP;
14965 	}
14966 
14967 	put_unaligned_le64(qword, dsn);
14968 
14969 	bp->flags |= BNXT_FLAG_DSN_VALID;
14970 	return 0;
14971 }
14972 
14973 static int bnxt_map_db_bar(struct bnxt *bp)
14974 {
14975 	if (!bp->db_size)
14976 		return -ENODEV;
14977 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
14978 	if (!bp->bar1)
14979 		return -ENOMEM;
14980 	return 0;
14981 }
14982 
14983 void bnxt_print_device_info(struct bnxt *bp)
14984 {
14985 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
14986 		    board_info[bp->board_idx].name,
14987 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
14988 
14989 	pcie_print_link_status(bp->pdev);
14990 }
14991 
14992 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
14993 {
14994 	struct bnxt_hw_resc *hw_resc;
14995 	struct net_device *dev;
14996 	struct bnxt *bp;
14997 	int rc, max_irqs;
14998 
14999 	if (pci_is_bridge(pdev))
15000 		return -ENODEV;
15001 
15002 	/* Clear any pending DMA transactions from crash kernel
15003 	 * while loading driver in capture kernel.
15004 	 */
15005 	if (is_kdump_kernel()) {
15006 		pci_clear_master(pdev);
15007 		pcie_flr(pdev);
15008 	}
15009 
15010 	max_irqs = bnxt_get_max_irq(pdev);
15011 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
15012 				 max_irqs);
15013 	if (!dev)
15014 		return -ENOMEM;
15015 
15016 	bp = netdev_priv(dev);
15017 	bp->board_idx = ent->driver_data;
15018 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
15019 	bnxt_set_max_func_irqs(bp, max_irqs);
15020 
15021 	if (bnxt_vf_pciid(bp->board_idx))
15022 		bp->flags |= BNXT_FLAG_VF;
15023 
15024 	/* No devlink port registration in case of a VF */
15025 	if (BNXT_PF(bp))
15026 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
15027 
15028 	if (pdev->msix_cap)
15029 		bp->flags |= BNXT_FLAG_MSIX_CAP;
15030 
15031 	rc = bnxt_init_board(pdev, dev);
15032 	if (rc < 0)
15033 		goto init_err_free;
15034 
15035 	dev->netdev_ops = &bnxt_netdev_ops;
15036 	dev->stat_ops = &bnxt_stat_ops;
15037 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
15038 	dev->ethtool_ops = &bnxt_ethtool_ops;
15039 	pci_set_drvdata(pdev, dev);
15040 
15041 	rc = bnxt_alloc_hwrm_resources(bp);
15042 	if (rc)
15043 		goto init_err_pci_clean;
15044 
15045 	mutex_init(&bp->hwrm_cmd_lock);
15046 	mutex_init(&bp->link_lock);
15047 
15048 	rc = bnxt_fw_init_one_p1(bp);
15049 	if (rc)
15050 		goto init_err_pci_clean;
15051 
15052 	if (BNXT_PF(bp))
15053 		bnxt_vpd_read_info(bp);
15054 
15055 	if (BNXT_CHIP_P5_PLUS(bp)) {
15056 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
15057 		if (BNXT_CHIP_P7(bp))
15058 			bp->flags |= BNXT_FLAG_CHIP_P7;
15059 	}
15060 
15061 	rc = bnxt_alloc_rss_indir_tbl(bp);
15062 	if (rc)
15063 		goto init_err_pci_clean;
15064 
15065 	rc = bnxt_fw_init_one_p2(bp);
15066 	if (rc)
15067 		goto init_err_pci_clean;
15068 
15069 	rc = bnxt_map_db_bar(bp);
15070 	if (rc) {
15071 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
15072 			rc);
15073 		goto init_err_pci_clean;
15074 	}
15075 
15076 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
15077 			   NETIF_F_TSO | NETIF_F_TSO6 |
15078 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
15079 			   NETIF_F_GSO_IPXIP4 |
15080 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
15081 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
15082 			   NETIF_F_RXCSUM | NETIF_F_GRO;
15083 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
15084 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
15085 
15086 	if (BNXT_SUPPORTS_TPA(bp))
15087 		dev->hw_features |= NETIF_F_LRO;
15088 
15089 	dev->hw_enc_features =
15090 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
15091 			NETIF_F_TSO | NETIF_F_TSO6 |
15092 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
15093 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
15094 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
15095 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
15096 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
15097 	if (bp->flags & BNXT_FLAG_CHIP_P7)
15098 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
15099 	else
15100 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
15101 
15102 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
15103 				    NETIF_F_GSO_GRE_CSUM;
15104 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
15105 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
15106 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
15107 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
15108 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
15109 	if (BNXT_SUPPORTS_TPA(bp))
15110 		dev->hw_features |= NETIF_F_GRO_HW;
15111 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
15112 	if (dev->features & NETIF_F_GRO_HW)
15113 		dev->features &= ~NETIF_F_LRO;
15114 	dev->priv_flags |= IFF_UNICAST_FLT;
15115 
15116 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
15117 
15118 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
15119 			    NETDEV_XDP_ACT_RX_SG;
15120 
15121 #ifdef CONFIG_BNXT_SRIOV
15122 	init_waitqueue_head(&bp->sriov_cfg_wait);
15123 #endif
15124 	if (BNXT_SUPPORTS_TPA(bp)) {
15125 		bp->gro_func = bnxt_gro_func_5730x;
15126 		if (BNXT_CHIP_P4(bp))
15127 			bp->gro_func = bnxt_gro_func_5731x;
15128 		else if (BNXT_CHIP_P5_PLUS(bp))
15129 			bp->gro_func = bnxt_gro_func_5750x;
15130 	}
15131 	if (!BNXT_CHIP_P4_PLUS(bp))
15132 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
15133 
15134 	rc = bnxt_init_mac_addr(bp);
15135 	if (rc) {
15136 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
15137 		rc = -EADDRNOTAVAIL;
15138 		goto init_err_pci_clean;
15139 	}
15140 
15141 	if (BNXT_PF(bp)) {
15142 		/* Read the adapter's DSN to use as the eswitch switch_id */
15143 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
15144 	}
15145 
15146 	/* MTU range: 60 - FW defined max */
15147 	dev->min_mtu = ETH_ZLEN;
15148 	dev->max_mtu = bp->max_mtu;
15149 
15150 	rc = bnxt_probe_phy(bp, true);
15151 	if (rc)
15152 		goto init_err_pci_clean;
15153 
15154 	hw_resc = &bp->hw_resc;
15155 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
15156 		       BNXT_L2_FLTR_MAX_FLTR;
15157 	/* Older firmware may not report these filters properly */
15158 	if (bp->max_fltr < BNXT_MAX_FLTR)
15159 		bp->max_fltr = BNXT_MAX_FLTR;
15160 	bnxt_init_l2_fltr_tbl(bp);
15161 	bnxt_set_rx_skb_mode(bp, false);
15162 	bnxt_set_tpa_flags(bp);
15163 	bnxt_set_ring_params(bp);
15164 	rc = bnxt_set_dflt_rings(bp, true);
15165 	if (rc) {
15166 		if (BNXT_VF(bp) && rc == -ENODEV) {
15167 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
15168 		} else {
15169 			netdev_err(bp->dev, "Not enough rings available.\n");
15170 			rc = -ENOMEM;
15171 		}
15172 		goto init_err_pci_clean;
15173 	}
15174 
15175 	bnxt_fw_init_one_p3(bp);
15176 
15177 	bnxt_init_dflt_coal(bp);
15178 
15179 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
15180 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
15181 
15182 	rc = bnxt_init_int_mode(bp);
15183 	if (rc)
15184 		goto init_err_pci_clean;
15185 
15186 	/* No TC has been set yet and rings may have been trimmed due to
15187 	 * limited MSIX, so we re-initialize the TX rings per TC.
15188 	 */
15189 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15190 
15191 	if (BNXT_PF(bp)) {
15192 		if (!bnxt_pf_wq) {
15193 			bnxt_pf_wq =
15194 				create_singlethread_workqueue("bnxt_pf_wq");
15195 			if (!bnxt_pf_wq) {
15196 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
15197 				rc = -ENOMEM;
15198 				goto init_err_pci_clean;
15199 			}
15200 		}
15201 		rc = bnxt_init_tc(bp);
15202 		if (rc)
15203 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
15204 				   rc);
15205 	}
15206 
15207 	bnxt_inv_fw_health_reg(bp);
15208 	rc = bnxt_dl_register(bp);
15209 	if (rc)
15210 		goto init_err_dl;
15211 
15212 	INIT_LIST_HEAD(&bp->usr_fltr_list);
15213 
15214 	rc = register_netdev(dev);
15215 	if (rc)
15216 		goto init_err_cleanup;
15217 
15218 	bnxt_dl_fw_reporters_create(bp);
15219 
15220 	bnxt_rdma_aux_device_init(bp);
15221 
15222 	bnxt_print_device_info(bp);
15223 
15224 	pci_save_state(pdev);
15225 
15226 	return 0;
15227 init_err_cleanup:
15228 	bnxt_dl_unregister(bp);
15229 init_err_dl:
15230 	bnxt_shutdown_tc(bp);
15231 	bnxt_clear_int_mode(bp);
15232 
15233 init_err_pci_clean:
15234 	bnxt_hwrm_func_drv_unrgtr(bp);
15235 	bnxt_free_hwrm_resources(bp);
15236 	bnxt_hwmon_uninit(bp);
15237 	bnxt_ethtool_free(bp);
15238 	bnxt_ptp_clear(bp);
15239 	kfree(bp->ptp_cfg);
15240 	bp->ptp_cfg = NULL;
15241 	kfree(bp->fw_health);
15242 	bp->fw_health = NULL;
15243 	bnxt_cleanup_pci(bp);
15244 	bnxt_free_ctx_mem(bp);
15245 	kfree(bp->rss_indir_tbl);
15246 	bp->rss_indir_tbl = NULL;
15247 
15248 init_err_free:
15249 	free_netdev(dev);
15250 	return rc;
15251 }
15252 
15253 static void bnxt_shutdown(struct pci_dev *pdev)
15254 {
15255 	struct net_device *dev = pci_get_drvdata(pdev);
15256 	struct bnxt *bp;
15257 
15258 	if (!dev)
15259 		return;
15260 
15261 	rtnl_lock();
15262 	bp = netdev_priv(dev);
15263 	if (!bp)
15264 		goto shutdown_exit;
15265 
15266 	if (netif_running(dev))
15267 		dev_close(dev);
15268 
15269 	bnxt_clear_int_mode(bp);
15270 	pci_disable_device(pdev);
15271 
15272 	if (system_state == SYSTEM_POWER_OFF) {
15273 		pci_wake_from_d3(pdev, bp->wol);
15274 		pci_set_power_state(pdev, PCI_D3hot);
15275 	}
15276 
15277 shutdown_exit:
15278 	rtnl_unlock();
15279 }
15280 
15281 #ifdef CONFIG_PM_SLEEP
15282 static int bnxt_suspend(struct device *device)
15283 {
15284 	struct net_device *dev = dev_get_drvdata(device);
15285 	struct bnxt *bp = netdev_priv(dev);
15286 	int rc = 0;
15287 
15288 	rtnl_lock();
15289 	bnxt_ulp_stop(bp);
15290 	if (netif_running(dev)) {
15291 		netif_device_detach(dev);
15292 		rc = bnxt_close(dev);
15293 	}
15294 	bnxt_hwrm_func_drv_unrgtr(bp);
15295 	pci_disable_device(bp->pdev);
15296 	bnxt_free_ctx_mem(bp);
15297 	rtnl_unlock();
15298 	return rc;
15299 }
15300 
15301 static int bnxt_resume(struct device *device)
15302 {
15303 	struct net_device *dev = dev_get_drvdata(device);
15304 	struct bnxt *bp = netdev_priv(dev);
15305 	int rc = 0;
15306 
15307 	rtnl_lock();
15308 	rc = pci_enable_device(bp->pdev);
15309 	if (rc) {
15310 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
15311 			   rc);
15312 		goto resume_exit;
15313 	}
15314 	pci_set_master(bp->pdev);
15315 	if (bnxt_hwrm_ver_get(bp)) {
15316 		rc = -ENODEV;
15317 		goto resume_exit;
15318 	}
15319 	rc = bnxt_hwrm_func_reset(bp);
15320 	if (rc) {
15321 		rc = -EBUSY;
15322 		goto resume_exit;
15323 	}
15324 
15325 	rc = bnxt_hwrm_func_qcaps(bp);
15326 	if (rc)
15327 		goto resume_exit;
15328 
15329 	bnxt_clear_reservations(bp, true);
15330 
15331 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
15332 		rc = -ENODEV;
15333 		goto resume_exit;
15334 	}
15335 
15336 	bnxt_get_wol_settings(bp);
15337 	if (netif_running(dev)) {
15338 		rc = bnxt_open(dev);
15339 		if (!rc)
15340 			netif_device_attach(dev);
15341 	}
15342 
15343 resume_exit:
15344 	bnxt_ulp_start(bp, rc);
15345 	if (!rc)
15346 		bnxt_reenable_sriov(bp);
15347 	rtnl_unlock();
15348 	return rc;
15349 }
15350 
15351 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
15352 #define BNXT_PM_OPS (&bnxt_pm_ops)
15353 
15354 #else
15355 
15356 #define BNXT_PM_OPS NULL
15357 
15358 #endif /* CONFIG_PM_SLEEP */
15359 
15360 /**
15361  * bnxt_io_error_detected - called when PCI error is detected
15362  * @pdev: Pointer to PCI device
15363  * @state: The current pci connection state
15364  *
15365  * This function is called after a PCI bus error affecting
15366  * this device has been detected.
15367  */
15368 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
15369 					       pci_channel_state_t state)
15370 {
15371 	struct net_device *netdev = pci_get_drvdata(pdev);
15372 	struct bnxt *bp = netdev_priv(netdev);
15373 	bool abort = false;
15374 
15375 	netdev_info(netdev, "PCI I/O error detected\n");
15376 
15377 	rtnl_lock();
15378 	netif_device_detach(netdev);
15379 
15380 	bnxt_ulp_stop(bp);
15381 
15382 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
15383 		netdev_err(bp->dev, "Firmware reset already in progress\n");
15384 		abort = true;
15385 	}
15386 
15387 	if (abort || state == pci_channel_io_perm_failure) {
15388 		rtnl_unlock();
15389 		return PCI_ERS_RESULT_DISCONNECT;
15390 	}
15391 
15392 	/* Link is not reliable anymore if state is pci_channel_io_frozen
15393 	 * so we disable bus master to prevent any potential bad DMAs before
15394 	 * freeing kernel memory.
15395 	 */
15396 	if (state == pci_channel_io_frozen) {
15397 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
15398 		bnxt_fw_fatal_close(bp);
15399 	}
15400 
15401 	if (netif_running(netdev))
15402 		__bnxt_close_nic(bp, true, true);
15403 
15404 	if (pci_is_enabled(pdev))
15405 		pci_disable_device(pdev);
15406 	bnxt_free_ctx_mem(bp);
15407 	rtnl_unlock();
15408 
15409 	/* Request a slot slot reset. */
15410 	return PCI_ERS_RESULT_NEED_RESET;
15411 }
15412 
15413 /**
15414  * bnxt_io_slot_reset - called after the pci bus has been reset.
15415  * @pdev: Pointer to PCI device
15416  *
15417  * Restart the card from scratch, as if from a cold-boot.
15418  * At this point, the card has exprienced a hard reset,
15419  * followed by fixups by BIOS, and has its config space
15420  * set up identically to what it was at cold boot.
15421  */
15422 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
15423 {
15424 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
15425 	struct net_device *netdev = pci_get_drvdata(pdev);
15426 	struct bnxt *bp = netdev_priv(netdev);
15427 	int retry = 0;
15428 	int err = 0;
15429 	int off;
15430 
15431 	netdev_info(bp->dev, "PCI Slot Reset\n");
15432 
15433 	rtnl_lock();
15434 
15435 	if (pci_enable_device(pdev)) {
15436 		dev_err(&pdev->dev,
15437 			"Cannot re-enable PCI device after reset.\n");
15438 	} else {
15439 		pci_set_master(pdev);
15440 		/* Upon fatal error, our device internal logic that latches to
15441 		 * BAR value is getting reset and will restore only upon
15442 		 * rewritting the BARs.
15443 		 *
15444 		 * As pci_restore_state() does not re-write the BARs if the
15445 		 * value is same as saved value earlier, driver needs to
15446 		 * write the BARs to 0 to force restore, in case of fatal error.
15447 		 */
15448 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
15449 				       &bp->state)) {
15450 			for (off = PCI_BASE_ADDRESS_0;
15451 			     off <= PCI_BASE_ADDRESS_5; off += 4)
15452 				pci_write_config_dword(bp->pdev, off, 0);
15453 		}
15454 		pci_restore_state(pdev);
15455 		pci_save_state(pdev);
15456 
15457 		bnxt_inv_fw_health_reg(bp);
15458 		bnxt_try_map_fw_health_reg(bp);
15459 
15460 		/* In some PCIe AER scenarios, firmware may take up to
15461 		 * 10 seconds to become ready in the worst case.
15462 		 */
15463 		do {
15464 			err = bnxt_try_recover_fw(bp);
15465 			if (!err)
15466 				break;
15467 			retry++;
15468 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
15469 
15470 		if (err) {
15471 			dev_err(&pdev->dev, "Firmware not ready\n");
15472 			goto reset_exit;
15473 		}
15474 
15475 		err = bnxt_hwrm_func_reset(bp);
15476 		if (!err)
15477 			result = PCI_ERS_RESULT_RECOVERED;
15478 
15479 		bnxt_ulp_irq_stop(bp);
15480 		bnxt_clear_int_mode(bp);
15481 		err = bnxt_init_int_mode(bp);
15482 		bnxt_ulp_irq_restart(bp, err);
15483 	}
15484 
15485 reset_exit:
15486 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15487 	bnxt_clear_reservations(bp, true);
15488 	rtnl_unlock();
15489 
15490 	return result;
15491 }
15492 
15493 /**
15494  * bnxt_io_resume - called when traffic can start flowing again.
15495  * @pdev: Pointer to PCI device
15496  *
15497  * This callback is called when the error recovery driver tells
15498  * us that its OK to resume normal operation.
15499  */
15500 static void bnxt_io_resume(struct pci_dev *pdev)
15501 {
15502 	struct net_device *netdev = pci_get_drvdata(pdev);
15503 	struct bnxt *bp = netdev_priv(netdev);
15504 	int err;
15505 
15506 	netdev_info(bp->dev, "PCI Slot Resume\n");
15507 	rtnl_lock();
15508 
15509 	err = bnxt_hwrm_func_qcaps(bp);
15510 	if (!err && netif_running(netdev))
15511 		err = bnxt_open(netdev);
15512 
15513 	bnxt_ulp_start(bp, err);
15514 	if (!err) {
15515 		bnxt_reenable_sriov(bp);
15516 		netif_device_attach(netdev);
15517 	}
15518 
15519 	rtnl_unlock();
15520 }
15521 
15522 static const struct pci_error_handlers bnxt_err_handler = {
15523 	.error_detected	= bnxt_io_error_detected,
15524 	.slot_reset	= bnxt_io_slot_reset,
15525 	.resume		= bnxt_io_resume
15526 };
15527 
15528 static struct pci_driver bnxt_pci_driver = {
15529 	.name		= DRV_MODULE_NAME,
15530 	.id_table	= bnxt_pci_tbl,
15531 	.probe		= bnxt_init_one,
15532 	.remove		= bnxt_remove_one,
15533 	.shutdown	= bnxt_shutdown,
15534 	.driver.pm	= BNXT_PM_OPS,
15535 	.err_handler	= &bnxt_err_handler,
15536 #if defined(CONFIG_BNXT_SRIOV)
15537 	.sriov_configure = bnxt_sriov_configure,
15538 #endif
15539 };
15540 
15541 static int __init bnxt_init(void)
15542 {
15543 	int err;
15544 
15545 	bnxt_debug_init();
15546 	err = pci_register_driver(&bnxt_pci_driver);
15547 	if (err) {
15548 		bnxt_debug_exit();
15549 		return err;
15550 	}
15551 
15552 	return 0;
15553 }
15554 
15555 static void __exit bnxt_exit(void)
15556 {
15557 	pci_unregister_driver(&bnxt_pci_driver);
15558 	if (bnxt_pf_wq)
15559 		destroy_workqueue(bnxt_pf_wq);
15560 	bnxt_debug_exit();
15561 }
15562 
15563 module_init(bnxt_init);
15564 module_exit(bnxt_exit);
15565