xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision a6a6a98094116b60e5523a571d9443c53325f5b1)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  * Copyright (c) 2016-2019 Broadcom Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  */
10 
11 #include <linux/module.h>
12 
13 #include <linux/stringify.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/pci.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/bitops.h>
27 #include <linux/io.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/byteorder.h>
31 #include <asm/page.h>
32 #include <linux/time.h>
33 #include <linux/mii.h>
34 #include <linux/mdio.h>
35 #include <linux/if.h>
36 #include <linux/if_vlan.h>
37 #include <linux/if_bridge.h>
38 #include <linux/rtc.h>
39 #include <linux/bpf.h>
40 #include <net/gro.h>
41 #include <net/ip.h>
42 #include <net/tcp.h>
43 #include <net/udp.h>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
46 #include <net/udp_tunnel.h>
47 #include <linux/workqueue.h>
48 #include <linux/prefetch.h>
49 #include <linux/cache.h>
50 #include <linux/log2.h>
51 #include <linux/bitmap.h>
52 #include <linux/cpu_rmap.h>
53 #include <linux/cpumask.h>
54 #include <net/pkt_cls.h>
55 #include <net/page_pool/helpers.h>
56 #include <linux/align.h>
57 #include <net/netdev_queues.h>
58 
59 #include "bnxt_hsi.h"
60 #include "bnxt.h"
61 #include "bnxt_hwrm.h"
62 #include "bnxt_ulp.h"
63 #include "bnxt_sriov.h"
64 #include "bnxt_ethtool.h"
65 #include "bnxt_dcb.h"
66 #include "bnxt_xdp.h"
67 #include "bnxt_ptp.h"
68 #include "bnxt_vfr.h"
69 #include "bnxt_tc.h"
70 #include "bnxt_devlink.h"
71 #include "bnxt_debugfs.h"
72 #include "bnxt_hwmon.h"
73 
74 #define BNXT_TX_TIMEOUT		(5 * HZ)
75 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
76 				 NETIF_MSG_TX_ERR)
77 
78 MODULE_LICENSE("GPL");
79 MODULE_DESCRIPTION("Broadcom NetXtreme network driver");
80 
81 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
82 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
83 #define BNXT_RX_COPY_THRESH 256
84 
85 #define BNXT_TX_PUSH_THRESH 164
86 
87 /* indexed by enum board_idx */
88 static const struct {
89 	char *name;
90 } board_info[] = {
91 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
92 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
93 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
94 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
95 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
96 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
97 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
98 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
99 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
100 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
101 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
102 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
103 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
104 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
105 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
106 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
108 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
109 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
110 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
111 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
112 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
113 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
114 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
115 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
116 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
117 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
118 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
119 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
120 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
121 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
122 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
123 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
124 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
125 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
126 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
127 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
128 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
129 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
130 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
131 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
132 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
133 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
134 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
135 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
136 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
137 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
138 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
139 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
140 	[NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" },
141 };
142 
143 static const struct pci_device_id bnxt_pci_tbl[] = {
144 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
145 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
146 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
147 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
148 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
149 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
150 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
151 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
152 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
153 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
155 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
159 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
160 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
164 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
165 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
166 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
167 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
168 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
169 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
170 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
171 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
172 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
173 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
174 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
175 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
176 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
178 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
179 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
180 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
181 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
182 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
183 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
184 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
185 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
187 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
188 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
189 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
190 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
191 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
192 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
193 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
194 #ifdef CONFIG_BNXT_SRIOV
195 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
196 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
197 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
198 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
199 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
200 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
201 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
202 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
203 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
204 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
205 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
206 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
207 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
208 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
209 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
210 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
211 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
212 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
214 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
215 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
216 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
217 #endif
218 	{ 0 }
219 };
220 
221 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
222 
223 static const u16 bnxt_vf_req_snif[] = {
224 	HWRM_FUNC_CFG,
225 	HWRM_FUNC_VF_CFG,
226 	HWRM_PORT_PHY_QCFG,
227 	HWRM_CFA_L2_FILTER_ALLOC,
228 };
229 
230 static const u16 bnxt_async_events_arr[] = {
231 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
232 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
233 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
234 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
235 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
236 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
237 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
238 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
239 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
240 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
241 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
242 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
243 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
244 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
245 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
246 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
247 };
248 
249 static struct workqueue_struct *bnxt_pf_wq;
250 
251 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
252 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
253 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
254 
255 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
256 	.ports = {
257 		.src = 0,
258 		.dst = 0,
259 	},
260 	.addrs = {
261 		.v6addrs = {
262 			.src = BNXT_IPV6_MASK_NONE,
263 			.dst = BNXT_IPV6_MASK_NONE,
264 		},
265 	},
266 };
267 
268 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
269 	.ports = {
270 		.src = cpu_to_be16(0xffff),
271 		.dst = cpu_to_be16(0xffff),
272 	},
273 	.addrs = {
274 		.v6addrs = {
275 			.src = BNXT_IPV6_MASK_ALL,
276 			.dst = BNXT_IPV6_MASK_ALL,
277 		},
278 	},
279 };
280 
281 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
282 	.ports = {
283 		.src = cpu_to_be16(0xffff),
284 		.dst = cpu_to_be16(0xffff),
285 	},
286 	.addrs = {
287 		.v4addrs = {
288 			.src = cpu_to_be32(0xffffffff),
289 			.dst = cpu_to_be32(0xffffffff),
290 		},
291 	},
292 };
293 
294 static bool bnxt_vf_pciid(enum board_idx idx)
295 {
296 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
297 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
298 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
299 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF);
300 }
301 
302 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
303 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
304 #define DB_CP_IRQ_DIS_FLAGS	(DB_KEY_CP | DB_IRQ_DIS)
305 
306 #define BNXT_CP_DB_IRQ_DIS(db)						\
307 		writel(DB_CP_IRQ_DIS_FLAGS, db)
308 
309 #define BNXT_DB_CQ(db, idx)						\
310 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
311 
312 #define BNXT_DB_NQ_P5(db, idx)						\
313 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
314 		    (db)->doorbell)
315 
316 #define BNXT_DB_NQ_P7(db, idx)						\
317 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
318 		    DB_RING_IDX(db, idx), (db)->doorbell)
319 
320 #define BNXT_DB_CQ_ARM(db, idx)						\
321 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
322 
323 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
324 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
325 		    DB_RING_IDX(db, idx), (db)->doorbell)
326 
327 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
328 {
329 	if (bp->flags & BNXT_FLAG_CHIP_P7)
330 		BNXT_DB_NQ_P7(db, idx);
331 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
332 		BNXT_DB_NQ_P5(db, idx);
333 	else
334 		BNXT_DB_CQ(db, idx);
335 }
336 
337 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
338 {
339 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
340 		BNXT_DB_NQ_ARM_P5(db, idx);
341 	else
342 		BNXT_DB_CQ_ARM(db, idx);
343 }
344 
345 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
346 {
347 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
348 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
349 			    DB_RING_IDX(db, idx), db->doorbell);
350 	else
351 		BNXT_DB_CQ(db, idx);
352 }
353 
354 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
355 {
356 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
357 		return;
358 
359 	if (BNXT_PF(bp))
360 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
361 	else
362 		schedule_delayed_work(&bp->fw_reset_task, delay);
363 }
364 
365 static void __bnxt_queue_sp_work(struct bnxt *bp)
366 {
367 	if (BNXT_PF(bp))
368 		queue_work(bnxt_pf_wq, &bp->sp_task);
369 	else
370 		schedule_work(&bp->sp_task);
371 }
372 
373 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
374 {
375 	set_bit(event, &bp->sp_event);
376 	__bnxt_queue_sp_work(bp);
377 }
378 
379 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
380 {
381 	if (!rxr->bnapi->in_reset) {
382 		rxr->bnapi->in_reset = true;
383 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
384 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
385 		else
386 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
387 		__bnxt_queue_sp_work(bp);
388 	}
389 	rxr->rx_next_cons = 0xffff;
390 }
391 
392 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
393 			  u16 curr)
394 {
395 	struct bnxt_napi *bnapi = txr->bnapi;
396 
397 	if (bnapi->tx_fault)
398 		return;
399 
400 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
401 		   txr->txq_index, txr->tx_hw_cons,
402 		   txr->tx_cons, txr->tx_prod, curr);
403 	WARN_ON_ONCE(1);
404 	bnapi->tx_fault = 1;
405 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
406 }
407 
408 const u16 bnxt_lhint_arr[] = {
409 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
410 	TX_BD_FLAGS_LHINT_512_TO_1023,
411 	TX_BD_FLAGS_LHINT_1024_TO_2047,
412 	TX_BD_FLAGS_LHINT_1024_TO_2047,
413 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
414 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
415 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
416 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
417 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
418 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
419 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
420 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
421 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
422 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
423 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
424 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
425 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
426 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
427 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
428 };
429 
430 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
431 {
432 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
433 
434 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
435 		return 0;
436 
437 	return md_dst->u.port_info.port_id;
438 }
439 
440 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
441 			     u16 prod)
442 {
443 	/* Sync BD data before updating doorbell */
444 	wmb();
445 	bnxt_db_write(bp, &txr->tx_db, prod);
446 	txr->kick_pending = 0;
447 }
448 
449 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
450 {
451 	struct bnxt *bp = netdev_priv(dev);
452 	struct tx_bd *txbd, *txbd0;
453 	struct tx_bd_ext *txbd1;
454 	struct netdev_queue *txq;
455 	int i;
456 	dma_addr_t mapping;
457 	unsigned int length, pad = 0;
458 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
459 	u16 prod, last_frag;
460 	struct pci_dev *pdev = bp->pdev;
461 	struct bnxt_tx_ring_info *txr;
462 	struct bnxt_sw_tx_bd *tx_buf;
463 	__le32 lflags = 0;
464 
465 	i = skb_get_queue_mapping(skb);
466 	if (unlikely(i >= bp->tx_nr_rings)) {
467 		dev_kfree_skb_any(skb);
468 		dev_core_stats_tx_dropped_inc(dev);
469 		return NETDEV_TX_OK;
470 	}
471 
472 	txq = netdev_get_tx_queue(dev, i);
473 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
474 	prod = txr->tx_prod;
475 
476 	free_size = bnxt_tx_avail(bp, txr);
477 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
478 		/* We must have raced with NAPI cleanup */
479 		if (net_ratelimit() && txr->kick_pending)
480 			netif_warn(bp, tx_err, dev,
481 				   "bnxt: ring busy w/ flush pending!\n");
482 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
483 					bp->tx_wake_thresh))
484 			return NETDEV_TX_BUSY;
485 	}
486 
487 	if (unlikely(ipv6_hopopt_jumbo_remove(skb)))
488 		goto tx_free;
489 
490 	length = skb->len;
491 	len = skb_headlen(skb);
492 	last_frag = skb_shinfo(skb)->nr_frags;
493 
494 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
495 
496 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
497 	tx_buf->skb = skb;
498 	tx_buf->nr_frags = last_frag;
499 
500 	vlan_tag_flags = 0;
501 	cfa_action = bnxt_xmit_get_cfa_action(skb);
502 	if (skb_vlan_tag_present(skb)) {
503 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
504 				 skb_vlan_tag_get(skb);
505 		/* Currently supports 8021Q, 8021AD vlan offloads
506 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
507 		 */
508 		if (skb->vlan_proto == htons(ETH_P_8021Q))
509 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
510 	}
511 
512 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
513 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
514 
515 		if (ptp && ptp->tx_tstamp_en && !skb_is_gso(skb)) {
516 			if (atomic_dec_if_positive(&ptp->tx_avail) < 0) {
517 				atomic64_inc(&ptp->stats.ts_err);
518 				goto tx_no_ts;
519 			}
520 			if (!bnxt_ptp_parse(skb, &ptp->tx_seqid,
521 					    &ptp->tx_hdr_off)) {
522 				if (vlan_tag_flags)
523 					ptp->tx_hdr_off += VLAN_HLEN;
524 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
525 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
526 			} else {
527 				atomic_inc(&bp->ptp_cfg->tx_avail);
528 			}
529 		}
530 	}
531 
532 tx_no_ts:
533 	if (unlikely(skb->no_fcs))
534 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
535 
536 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
537 	    !lflags) {
538 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
539 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
540 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
541 		void __iomem *db = txr->tx_db.doorbell;
542 		void *pdata = tx_push_buf->data;
543 		u64 *end;
544 		int j, push_len;
545 
546 		/* Set COAL_NOW to be ready quickly for the next push */
547 		tx_push->tx_bd_len_flags_type =
548 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
549 					TX_BD_TYPE_LONG_TX_BD |
550 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
551 					TX_BD_FLAGS_COAL_NOW |
552 					TX_BD_FLAGS_PACKET_END |
553 					(2 << TX_BD_FLAGS_BD_CNT_SHIFT));
554 
555 		if (skb->ip_summed == CHECKSUM_PARTIAL)
556 			tx_push1->tx_bd_hsize_lflags =
557 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
558 		else
559 			tx_push1->tx_bd_hsize_lflags = 0;
560 
561 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
562 		tx_push1->tx_bd_cfa_action =
563 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
564 
565 		end = pdata + length;
566 		end = PTR_ALIGN(end, 8) - 1;
567 		*end = 0;
568 
569 		skb_copy_from_linear_data(skb, pdata, len);
570 		pdata += len;
571 		for (j = 0; j < last_frag; j++) {
572 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
573 			void *fptr;
574 
575 			fptr = skb_frag_address_safe(frag);
576 			if (!fptr)
577 				goto normal_tx;
578 
579 			memcpy(pdata, fptr, skb_frag_size(frag));
580 			pdata += skb_frag_size(frag);
581 		}
582 
583 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
584 		txbd->tx_bd_haddr = txr->data_mapping;
585 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
586 		prod = NEXT_TX(prod);
587 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
588 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
589 		memcpy(txbd, tx_push1, sizeof(*txbd));
590 		prod = NEXT_TX(prod);
591 		tx_push->doorbell =
592 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
593 				    DB_RING_IDX(&txr->tx_db, prod));
594 		WRITE_ONCE(txr->tx_prod, prod);
595 
596 		tx_buf->is_push = 1;
597 		netdev_tx_sent_queue(txq, skb->len);
598 		wmb();	/* Sync is_push and byte queue before pushing data */
599 
600 		push_len = (length + sizeof(*tx_push) + 7) / 8;
601 		if (push_len > 16) {
602 			__iowrite64_copy(db, tx_push_buf, 16);
603 			__iowrite32_copy(db + 4, tx_push_buf + 1,
604 					 (push_len - 16) << 1);
605 		} else {
606 			__iowrite64_copy(db, tx_push_buf, push_len);
607 		}
608 
609 		goto tx_done;
610 	}
611 
612 normal_tx:
613 	if (length < BNXT_MIN_PKT_SIZE) {
614 		pad = BNXT_MIN_PKT_SIZE - length;
615 		if (skb_pad(skb, pad))
616 			/* SKB already freed. */
617 			goto tx_kick_pending;
618 		length = BNXT_MIN_PKT_SIZE;
619 	}
620 
621 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
622 
623 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
624 		goto tx_free;
625 
626 	dma_unmap_addr_set(tx_buf, mapping, mapping);
627 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
628 		((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
629 
630 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
631 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
632 
633 	prod = NEXT_TX(prod);
634 	txbd1 = (struct tx_bd_ext *)
635 		&txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
636 
637 	txbd1->tx_bd_hsize_lflags = lflags;
638 	if (skb_is_gso(skb)) {
639 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
640 		u32 hdr_len;
641 
642 		if (skb->encapsulation) {
643 			if (udp_gso)
644 				hdr_len = skb_inner_transport_offset(skb) +
645 					  sizeof(struct udphdr);
646 			else
647 				hdr_len = skb_inner_tcp_all_headers(skb);
648 		} else if (udp_gso) {
649 			hdr_len = skb_transport_offset(skb) +
650 				  sizeof(struct udphdr);
651 		} else {
652 			hdr_len = skb_tcp_all_headers(skb);
653 		}
654 
655 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
656 					TX_BD_FLAGS_T_IPID |
657 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
658 		length = skb_shinfo(skb)->gso_size;
659 		txbd1->tx_bd_mss = cpu_to_le32(length);
660 		length += hdr_len;
661 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
662 		txbd1->tx_bd_hsize_lflags |=
663 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
664 		txbd1->tx_bd_mss = 0;
665 	}
666 
667 	length >>= 9;
668 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
669 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
670 				     skb->len);
671 		i = 0;
672 		goto tx_dma_error;
673 	}
674 	flags |= bnxt_lhint_arr[length];
675 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
676 
677 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
678 	txbd1->tx_bd_cfa_action =
679 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
680 	txbd0 = txbd;
681 	for (i = 0; i < last_frag; i++) {
682 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
683 
684 		prod = NEXT_TX(prod);
685 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
686 
687 		len = skb_frag_size(frag);
688 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
689 					   DMA_TO_DEVICE);
690 
691 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
692 			goto tx_dma_error;
693 
694 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
695 		dma_unmap_addr_set(tx_buf, mapping, mapping);
696 
697 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
698 
699 		flags = len << TX_BD_LEN_SHIFT;
700 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
701 	}
702 
703 	flags &= ~TX_BD_LEN;
704 	txbd->tx_bd_len_flags_type =
705 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
706 			    TX_BD_FLAGS_PACKET_END);
707 
708 	netdev_tx_sent_queue(txq, skb->len);
709 
710 	skb_tx_timestamp(skb);
711 
712 	prod = NEXT_TX(prod);
713 	WRITE_ONCE(txr->tx_prod, prod);
714 
715 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
716 		bnxt_txr_db_kick(bp, txr, prod);
717 	} else {
718 		if (free_size >= bp->tx_wake_thresh)
719 			txbd0->tx_bd_len_flags_type |=
720 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
721 		txr->kick_pending = 1;
722 	}
723 
724 tx_done:
725 
726 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
727 		if (netdev_xmit_more() && !tx_buf->is_push) {
728 			txbd0->tx_bd_len_flags_type &=
729 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
730 			bnxt_txr_db_kick(bp, txr, prod);
731 		}
732 
733 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
734 				   bp->tx_wake_thresh);
735 	}
736 	return NETDEV_TX_OK;
737 
738 tx_dma_error:
739 	last_frag = i;
740 
741 	/* start back at beginning and unmap skb */
742 	prod = txr->tx_prod;
743 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
744 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
745 			 skb_headlen(skb), DMA_TO_DEVICE);
746 	prod = NEXT_TX(prod);
747 
748 	/* unmap remaining mapped pages */
749 	for (i = 0; i < last_frag; i++) {
750 		prod = NEXT_TX(prod);
751 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
752 		dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
753 			       skb_frag_size(&skb_shinfo(skb)->frags[i]),
754 			       DMA_TO_DEVICE);
755 	}
756 
757 tx_free:
758 	dev_kfree_skb_any(skb);
759 tx_kick_pending:
760 	if (BNXT_TX_PTP_IS_SET(lflags)) {
761 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
762 		atomic_inc(&bp->ptp_cfg->tx_avail);
763 	}
764 	if (txr->kick_pending)
765 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
766 	txr->tx_buf_ring[txr->tx_prod].skb = NULL;
767 	dev_core_stats_tx_dropped_inc(dev);
768 	return NETDEV_TX_OK;
769 }
770 
771 static void __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
772 			  int budget)
773 {
774 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
775 	struct pci_dev *pdev = bp->pdev;
776 	u16 hw_cons = txr->tx_hw_cons;
777 	unsigned int tx_bytes = 0;
778 	u16 cons = txr->tx_cons;
779 	int tx_pkts = 0;
780 
781 	while (RING_TX(bp, cons) != hw_cons) {
782 		struct bnxt_sw_tx_bd *tx_buf;
783 		struct sk_buff *skb;
784 		int j, last;
785 
786 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
787 		cons = NEXT_TX(cons);
788 		skb = tx_buf->skb;
789 		tx_buf->skb = NULL;
790 
791 		if (unlikely(!skb)) {
792 			bnxt_sched_reset_txr(bp, txr, cons);
793 			return;
794 		}
795 
796 		tx_pkts++;
797 		tx_bytes += skb->len;
798 
799 		if (tx_buf->is_push) {
800 			tx_buf->is_push = 0;
801 			goto next_tx_int;
802 		}
803 
804 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
805 				 skb_headlen(skb), DMA_TO_DEVICE);
806 		last = tx_buf->nr_frags;
807 
808 		for (j = 0; j < last; j++) {
809 			cons = NEXT_TX(cons);
810 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
811 			dma_unmap_page(
812 				&pdev->dev,
813 				dma_unmap_addr(tx_buf, mapping),
814 				skb_frag_size(&skb_shinfo(skb)->frags[j]),
815 				DMA_TO_DEVICE);
816 		}
817 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
818 			if (BNXT_CHIP_P5(bp)) {
819 				/* PTP worker takes ownership of the skb */
820 				if (!bnxt_get_tx_ts_p5(bp, skb)) {
821 					skb = NULL;
822 				} else {
823 					atomic64_inc(&bp->ptp_cfg->stats.ts_err);
824 					atomic_inc(&bp->ptp_cfg->tx_avail);
825 				}
826 			}
827 		}
828 
829 next_tx_int:
830 		cons = NEXT_TX(cons);
831 
832 		dev_consume_skb_any(skb);
833 	}
834 
835 	WRITE_ONCE(txr->tx_cons, cons);
836 
837 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
838 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
839 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
840 }
841 
842 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
843 {
844 	struct bnxt_tx_ring_info *txr;
845 	int i;
846 
847 	bnxt_for_each_napi_tx(i, bnapi, txr) {
848 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
849 			__bnxt_tx_int(bp, txr, budget);
850 	}
851 	bnapi->events &= ~BNXT_TX_CMP_EVENT;
852 }
853 
854 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
855 					 struct bnxt_rx_ring_info *rxr,
856 					 unsigned int *offset,
857 					 gfp_t gfp)
858 {
859 	struct page *page;
860 
861 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
862 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
863 						BNXT_RX_PAGE_SIZE);
864 	} else {
865 		page = page_pool_dev_alloc_pages(rxr->page_pool);
866 		*offset = 0;
867 	}
868 	if (!page)
869 		return NULL;
870 
871 	*mapping = page_pool_get_dma_addr(page) + *offset;
872 	return page;
873 }
874 
875 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
876 				       gfp_t gfp)
877 {
878 	u8 *data;
879 	struct pci_dev *pdev = bp->pdev;
880 
881 	if (gfp == GFP_ATOMIC)
882 		data = napi_alloc_frag(bp->rx_buf_size);
883 	else
884 		data = netdev_alloc_frag(bp->rx_buf_size);
885 	if (!data)
886 		return NULL;
887 
888 	*mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
889 					bp->rx_buf_use_size, bp->rx_dir,
890 					DMA_ATTR_WEAK_ORDERING);
891 
892 	if (dma_mapping_error(&pdev->dev, *mapping)) {
893 		skb_free_frag(data);
894 		data = NULL;
895 	}
896 	return data;
897 }
898 
899 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
900 		       u16 prod, gfp_t gfp)
901 {
902 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
903 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
904 	dma_addr_t mapping;
905 
906 	if (BNXT_RX_PAGE_MODE(bp)) {
907 		unsigned int offset;
908 		struct page *page =
909 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
910 
911 		if (!page)
912 			return -ENOMEM;
913 
914 		mapping += bp->rx_dma_offset;
915 		rx_buf->data = page;
916 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
917 	} else {
918 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, gfp);
919 
920 		if (!data)
921 			return -ENOMEM;
922 
923 		rx_buf->data = data;
924 		rx_buf->data_ptr = data + bp->rx_offset;
925 	}
926 	rx_buf->mapping = mapping;
927 
928 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
929 	return 0;
930 }
931 
932 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
933 {
934 	u16 prod = rxr->rx_prod;
935 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
936 	struct bnxt *bp = rxr->bnapi->bp;
937 	struct rx_bd *cons_bd, *prod_bd;
938 
939 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
940 	cons_rx_buf = &rxr->rx_buf_ring[cons];
941 
942 	prod_rx_buf->data = data;
943 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
944 
945 	prod_rx_buf->mapping = cons_rx_buf->mapping;
946 
947 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
948 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
949 
950 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
951 }
952 
953 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
954 {
955 	u16 next, max = rxr->rx_agg_bmap_size;
956 
957 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
958 	if (next >= max)
959 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
960 	return next;
961 }
962 
963 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
964 				     struct bnxt_rx_ring_info *rxr,
965 				     u16 prod, gfp_t gfp)
966 {
967 	struct rx_bd *rxbd =
968 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
969 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
970 	struct page *page;
971 	dma_addr_t mapping;
972 	u16 sw_prod = rxr->rx_sw_agg_prod;
973 	unsigned int offset = 0;
974 
975 	page = __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
976 
977 	if (!page)
978 		return -ENOMEM;
979 
980 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
981 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
982 
983 	__set_bit(sw_prod, rxr->rx_agg_bmap);
984 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
985 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
986 
987 	rx_agg_buf->page = page;
988 	rx_agg_buf->offset = offset;
989 	rx_agg_buf->mapping = mapping;
990 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
991 	rxbd->rx_bd_opaque = sw_prod;
992 	return 0;
993 }
994 
995 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
996 				       struct bnxt_cp_ring_info *cpr,
997 				       u16 cp_cons, u16 curr)
998 {
999 	struct rx_agg_cmp *agg;
1000 
1001 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1002 	agg = (struct rx_agg_cmp *)
1003 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1004 	return agg;
1005 }
1006 
1007 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1008 					      struct bnxt_rx_ring_info *rxr,
1009 					      u16 agg_id, u16 curr)
1010 {
1011 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1012 
1013 	return &tpa_info->agg_arr[curr];
1014 }
1015 
1016 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1017 				   u16 start, u32 agg_bufs, bool tpa)
1018 {
1019 	struct bnxt_napi *bnapi = cpr->bnapi;
1020 	struct bnxt *bp = bnapi->bp;
1021 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1022 	u16 prod = rxr->rx_agg_prod;
1023 	u16 sw_prod = rxr->rx_sw_agg_prod;
1024 	bool p5_tpa = false;
1025 	u32 i;
1026 
1027 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1028 		p5_tpa = true;
1029 
1030 	for (i = 0; i < agg_bufs; i++) {
1031 		u16 cons;
1032 		struct rx_agg_cmp *agg;
1033 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1034 		struct rx_bd *prod_bd;
1035 		struct page *page;
1036 
1037 		if (p5_tpa)
1038 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1039 		else
1040 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1041 		cons = agg->rx_agg_cmp_opaque;
1042 		__clear_bit(cons, rxr->rx_agg_bmap);
1043 
1044 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1045 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1046 
1047 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1048 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1049 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1050 
1051 		/* It is possible for sw_prod to be equal to cons, so
1052 		 * set cons_rx_buf->page to NULL first.
1053 		 */
1054 		page = cons_rx_buf->page;
1055 		cons_rx_buf->page = NULL;
1056 		prod_rx_buf->page = page;
1057 		prod_rx_buf->offset = cons_rx_buf->offset;
1058 
1059 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1060 
1061 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1062 
1063 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1064 		prod_bd->rx_bd_opaque = sw_prod;
1065 
1066 		prod = NEXT_RX_AGG(prod);
1067 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1068 	}
1069 	rxr->rx_agg_prod = prod;
1070 	rxr->rx_sw_agg_prod = sw_prod;
1071 }
1072 
1073 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1074 					      struct bnxt_rx_ring_info *rxr,
1075 					      u16 cons, void *data, u8 *data_ptr,
1076 					      dma_addr_t dma_addr,
1077 					      unsigned int offset_and_len)
1078 {
1079 	unsigned int len = offset_and_len & 0xffff;
1080 	struct page *page = data;
1081 	u16 prod = rxr->rx_prod;
1082 	struct sk_buff *skb;
1083 	int err;
1084 
1085 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1086 	if (unlikely(err)) {
1087 		bnxt_reuse_rx_data(rxr, cons, data);
1088 		return NULL;
1089 	}
1090 	dma_addr -= bp->rx_dma_offset;
1091 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1092 				bp->rx_dir);
1093 	skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE);
1094 	if (!skb) {
1095 		page_pool_recycle_direct(rxr->page_pool, page);
1096 		return NULL;
1097 	}
1098 	skb_mark_for_recycle(skb);
1099 	skb_reserve(skb, bp->rx_offset);
1100 	__skb_put(skb, len);
1101 
1102 	return skb;
1103 }
1104 
1105 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1106 					struct bnxt_rx_ring_info *rxr,
1107 					u16 cons, void *data, u8 *data_ptr,
1108 					dma_addr_t dma_addr,
1109 					unsigned int offset_and_len)
1110 {
1111 	unsigned int payload = offset_and_len >> 16;
1112 	unsigned int len = offset_and_len & 0xffff;
1113 	skb_frag_t *frag;
1114 	struct page *page = data;
1115 	u16 prod = rxr->rx_prod;
1116 	struct sk_buff *skb;
1117 	int off, err;
1118 
1119 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1120 	if (unlikely(err)) {
1121 		bnxt_reuse_rx_data(rxr, cons, data);
1122 		return NULL;
1123 	}
1124 	dma_addr -= bp->rx_dma_offset;
1125 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1126 				bp->rx_dir);
1127 
1128 	if (unlikely(!payload))
1129 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1130 
1131 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1132 	if (!skb) {
1133 		page_pool_recycle_direct(rxr->page_pool, page);
1134 		return NULL;
1135 	}
1136 
1137 	skb_mark_for_recycle(skb);
1138 	off = (void *)data_ptr - page_address(page);
1139 	skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE);
1140 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1141 	       payload + NET_IP_ALIGN);
1142 
1143 	frag = &skb_shinfo(skb)->frags[0];
1144 	skb_frag_size_sub(frag, payload);
1145 	skb_frag_off_add(frag, payload);
1146 	skb->data_len -= payload;
1147 	skb->tail += payload;
1148 
1149 	return skb;
1150 }
1151 
1152 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1153 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1154 				   void *data, u8 *data_ptr,
1155 				   dma_addr_t dma_addr,
1156 				   unsigned int offset_and_len)
1157 {
1158 	u16 prod = rxr->rx_prod;
1159 	struct sk_buff *skb;
1160 	int err;
1161 
1162 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1163 	if (unlikely(err)) {
1164 		bnxt_reuse_rx_data(rxr, cons, data);
1165 		return NULL;
1166 	}
1167 
1168 	skb = napi_build_skb(data, bp->rx_buf_size);
1169 	dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1170 			       bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
1171 	if (!skb) {
1172 		skb_free_frag(data);
1173 		return NULL;
1174 	}
1175 
1176 	skb_reserve(skb, bp->rx_offset);
1177 	skb_put(skb, offset_and_len & 0xffff);
1178 	return skb;
1179 }
1180 
1181 static u32 __bnxt_rx_agg_pages(struct bnxt *bp,
1182 			       struct bnxt_cp_ring_info *cpr,
1183 			       struct skb_shared_info *shinfo,
1184 			       u16 idx, u32 agg_bufs, bool tpa,
1185 			       struct xdp_buff *xdp)
1186 {
1187 	struct bnxt_napi *bnapi = cpr->bnapi;
1188 	struct pci_dev *pdev = bp->pdev;
1189 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1190 	u16 prod = rxr->rx_agg_prod;
1191 	u32 i, total_frag_len = 0;
1192 	bool p5_tpa = false;
1193 
1194 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1195 		p5_tpa = true;
1196 
1197 	for (i = 0; i < agg_bufs; i++) {
1198 		skb_frag_t *frag = &shinfo->frags[i];
1199 		u16 cons, frag_len;
1200 		struct rx_agg_cmp *agg;
1201 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1202 		struct page *page;
1203 		dma_addr_t mapping;
1204 
1205 		if (p5_tpa)
1206 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1207 		else
1208 			agg = bnxt_get_agg(bp, cpr, idx, i);
1209 		cons = agg->rx_agg_cmp_opaque;
1210 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1211 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1212 
1213 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1214 		skb_frag_fill_page_desc(frag, cons_rx_buf->page,
1215 					cons_rx_buf->offset, frag_len);
1216 		shinfo->nr_frags = i + 1;
1217 		__clear_bit(cons, rxr->rx_agg_bmap);
1218 
1219 		/* It is possible for bnxt_alloc_rx_page() to allocate
1220 		 * a sw_prod index that equals the cons index, so we
1221 		 * need to clear the cons entry now.
1222 		 */
1223 		mapping = cons_rx_buf->mapping;
1224 		page = cons_rx_buf->page;
1225 		cons_rx_buf->page = NULL;
1226 
1227 		if (xdp && page_is_pfmemalloc(page))
1228 			xdp_buff_set_frag_pfmemalloc(xdp);
1229 
1230 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1231 			--shinfo->nr_frags;
1232 			cons_rx_buf->page = page;
1233 
1234 			/* Update prod since possibly some pages have been
1235 			 * allocated already.
1236 			 */
1237 			rxr->rx_agg_prod = prod;
1238 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1239 			return 0;
1240 		}
1241 
1242 		dma_sync_single_for_cpu(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1243 					bp->rx_dir);
1244 
1245 		total_frag_len += frag_len;
1246 		prod = NEXT_RX_AGG(prod);
1247 	}
1248 	rxr->rx_agg_prod = prod;
1249 	return total_frag_len;
1250 }
1251 
1252 static struct sk_buff *bnxt_rx_agg_pages_skb(struct bnxt *bp,
1253 					     struct bnxt_cp_ring_info *cpr,
1254 					     struct sk_buff *skb, u16 idx,
1255 					     u32 agg_bufs, bool tpa)
1256 {
1257 	struct skb_shared_info *shinfo = skb_shinfo(skb);
1258 	u32 total_frag_len = 0;
1259 
1260 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo, idx,
1261 					     agg_bufs, tpa, NULL);
1262 	if (!total_frag_len) {
1263 		skb_mark_for_recycle(skb);
1264 		dev_kfree_skb(skb);
1265 		return NULL;
1266 	}
1267 
1268 	skb->data_len += total_frag_len;
1269 	skb->len += total_frag_len;
1270 	skb->truesize += BNXT_RX_PAGE_SIZE * agg_bufs;
1271 	return skb;
1272 }
1273 
1274 static u32 bnxt_rx_agg_pages_xdp(struct bnxt *bp,
1275 				 struct bnxt_cp_ring_info *cpr,
1276 				 struct xdp_buff *xdp, u16 idx,
1277 				 u32 agg_bufs, bool tpa)
1278 {
1279 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1280 	u32 total_frag_len = 0;
1281 
1282 	if (!xdp_buff_has_frags(xdp))
1283 		shinfo->nr_frags = 0;
1284 
1285 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo,
1286 					     idx, agg_bufs, tpa, xdp);
1287 	if (total_frag_len) {
1288 		xdp_buff_set_frags_flag(xdp);
1289 		shinfo->nr_frags = agg_bufs;
1290 		shinfo->xdp_frags_size = total_frag_len;
1291 	}
1292 	return total_frag_len;
1293 }
1294 
1295 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1296 			       u8 agg_bufs, u32 *raw_cons)
1297 {
1298 	u16 last;
1299 	struct rx_agg_cmp *agg;
1300 
1301 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1302 	last = RING_CMP(*raw_cons);
1303 	agg = (struct rx_agg_cmp *)
1304 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1305 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1306 }
1307 
1308 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1309 				      unsigned int len,
1310 				      dma_addr_t mapping)
1311 {
1312 	struct bnxt *bp = bnapi->bp;
1313 	struct pci_dev *pdev = bp->pdev;
1314 	struct sk_buff *skb;
1315 
1316 	skb = napi_alloc_skb(&bnapi->napi, len);
1317 	if (!skb)
1318 		return NULL;
1319 
1320 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1321 				bp->rx_dir);
1322 
1323 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1324 	       len + NET_IP_ALIGN);
1325 
1326 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1327 				   bp->rx_dir);
1328 
1329 	skb_put(skb, len);
1330 
1331 	return skb;
1332 }
1333 
1334 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1335 				     unsigned int len,
1336 				     dma_addr_t mapping)
1337 {
1338 	return bnxt_copy_data(bnapi, data, len, mapping);
1339 }
1340 
1341 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1342 				     struct xdp_buff *xdp,
1343 				     unsigned int len,
1344 				     dma_addr_t mapping)
1345 {
1346 	unsigned int metasize = 0;
1347 	u8 *data = xdp->data;
1348 	struct sk_buff *skb;
1349 
1350 	len = xdp->data_end - xdp->data_meta;
1351 	metasize = xdp->data - xdp->data_meta;
1352 	data = xdp->data_meta;
1353 
1354 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1355 	if (!skb)
1356 		return skb;
1357 
1358 	if (metasize) {
1359 		skb_metadata_set(skb, metasize);
1360 		__skb_pull(skb, metasize);
1361 	}
1362 
1363 	return skb;
1364 }
1365 
1366 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1367 			   u32 *raw_cons, void *cmp)
1368 {
1369 	struct rx_cmp *rxcmp = cmp;
1370 	u32 tmp_raw_cons = *raw_cons;
1371 	u8 cmp_type, agg_bufs = 0;
1372 
1373 	cmp_type = RX_CMP_TYPE(rxcmp);
1374 
1375 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1376 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1377 			    RX_CMP_AGG_BUFS) >>
1378 			   RX_CMP_AGG_BUFS_SHIFT;
1379 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1380 		struct rx_tpa_end_cmp *tpa_end = cmp;
1381 
1382 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1383 			return 0;
1384 
1385 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1386 	}
1387 
1388 	if (agg_bufs) {
1389 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1390 			return -EBUSY;
1391 	}
1392 	*raw_cons = tmp_raw_cons;
1393 	return 0;
1394 }
1395 
1396 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1397 {
1398 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1399 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1400 
1401 	if (test_bit(idx, map->agg_idx_bmap))
1402 		idx = find_first_zero_bit(map->agg_idx_bmap,
1403 					  BNXT_AGG_IDX_BMAP_SIZE);
1404 	__set_bit(idx, map->agg_idx_bmap);
1405 	map->agg_id_tbl[agg_id] = idx;
1406 	return idx;
1407 }
1408 
1409 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1410 {
1411 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1412 
1413 	__clear_bit(idx, map->agg_idx_bmap);
1414 }
1415 
1416 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1417 {
1418 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1419 
1420 	return map->agg_id_tbl[agg_id];
1421 }
1422 
1423 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1424 			      struct rx_tpa_start_cmp *tpa_start,
1425 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1426 {
1427 	tpa_info->cfa_code_valid = 1;
1428 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1429 	tpa_info->vlan_valid = 0;
1430 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1431 		tpa_info->vlan_valid = 1;
1432 		tpa_info->metadata =
1433 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1434 	}
1435 }
1436 
1437 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1438 				 struct rx_tpa_start_cmp *tpa_start,
1439 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1440 {
1441 	tpa_info->vlan_valid = 0;
1442 	if (TPA_START_VLAN_VALID(tpa_start)) {
1443 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1444 		u32 vlan_proto = ETH_P_8021Q;
1445 
1446 		tpa_info->vlan_valid = 1;
1447 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1448 			vlan_proto = ETH_P_8021AD;
1449 		tpa_info->metadata = vlan_proto << 16 |
1450 				     TPA_START_METADATA0_TCI(tpa_start1);
1451 	}
1452 }
1453 
1454 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1455 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1456 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1457 {
1458 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1459 	struct bnxt_tpa_info *tpa_info;
1460 	u16 cons, prod, agg_id;
1461 	struct rx_bd *prod_bd;
1462 	dma_addr_t mapping;
1463 
1464 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1465 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1466 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1467 	} else {
1468 		agg_id = TPA_START_AGG_ID(tpa_start);
1469 	}
1470 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1471 	prod = rxr->rx_prod;
1472 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1473 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1474 	tpa_info = &rxr->rx_tpa[agg_id];
1475 
1476 	if (unlikely(cons != rxr->rx_next_cons ||
1477 		     TPA_START_ERROR(tpa_start))) {
1478 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1479 			    cons, rxr->rx_next_cons,
1480 			    TPA_START_ERROR_CODE(tpa_start1));
1481 		bnxt_sched_reset_rxr(bp, rxr);
1482 		return;
1483 	}
1484 	prod_rx_buf->data = tpa_info->data;
1485 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1486 
1487 	mapping = tpa_info->mapping;
1488 	prod_rx_buf->mapping = mapping;
1489 
1490 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1491 
1492 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1493 
1494 	tpa_info->data = cons_rx_buf->data;
1495 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1496 	cons_rx_buf->data = NULL;
1497 	tpa_info->mapping = cons_rx_buf->mapping;
1498 
1499 	tpa_info->len =
1500 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1501 				RX_TPA_START_CMP_LEN_SHIFT;
1502 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1503 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1504 		tpa_info->gso_type = SKB_GSO_TCPV4;
1505 		if (TPA_START_IS_IPV6(tpa_start1))
1506 			tpa_info->gso_type = SKB_GSO_TCPV6;
1507 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1508 		else if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP &&
1509 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1510 			tpa_info->gso_type = SKB_GSO_TCPV6;
1511 		tpa_info->rss_hash =
1512 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1513 	} else {
1514 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1515 		tpa_info->gso_type = 0;
1516 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1517 	}
1518 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1519 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1520 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1521 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1522 	else
1523 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1524 	tpa_info->agg_count = 0;
1525 
1526 	rxr->rx_prod = NEXT_RX(prod);
1527 	cons = RING_RX(bp, NEXT_RX(cons));
1528 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1529 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1530 
1531 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1532 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1533 	cons_rx_buf->data = NULL;
1534 }
1535 
1536 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1537 {
1538 	if (agg_bufs)
1539 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1540 }
1541 
1542 #ifdef CONFIG_INET
1543 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1544 {
1545 	struct udphdr *uh = NULL;
1546 
1547 	if (ip_proto == htons(ETH_P_IP)) {
1548 		struct iphdr *iph = (struct iphdr *)skb->data;
1549 
1550 		if (iph->protocol == IPPROTO_UDP)
1551 			uh = (struct udphdr *)(iph + 1);
1552 	} else {
1553 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1554 
1555 		if (iph->nexthdr == IPPROTO_UDP)
1556 			uh = (struct udphdr *)(iph + 1);
1557 	}
1558 	if (uh) {
1559 		if (uh->check)
1560 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1561 		else
1562 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1563 	}
1564 }
1565 #endif
1566 
1567 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1568 					   int payload_off, int tcp_ts,
1569 					   struct sk_buff *skb)
1570 {
1571 #ifdef CONFIG_INET
1572 	struct tcphdr *th;
1573 	int len, nw_off;
1574 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1575 	u32 hdr_info = tpa_info->hdr_info;
1576 	bool loopback = false;
1577 
1578 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1579 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1580 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1581 
1582 	/* If the packet is an internal loopback packet, the offsets will
1583 	 * have an extra 4 bytes.
1584 	 */
1585 	if (inner_mac_off == 4) {
1586 		loopback = true;
1587 	} else if (inner_mac_off > 4) {
1588 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1589 					    ETH_HLEN - 2));
1590 
1591 		/* We only support inner iPv4/ipv6.  If we don't see the
1592 		 * correct protocol ID, it must be a loopback packet where
1593 		 * the offsets are off by 4.
1594 		 */
1595 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1596 			loopback = true;
1597 	}
1598 	if (loopback) {
1599 		/* internal loopback packet, subtract all offsets by 4 */
1600 		inner_ip_off -= 4;
1601 		inner_mac_off -= 4;
1602 		outer_ip_off -= 4;
1603 	}
1604 
1605 	nw_off = inner_ip_off - ETH_HLEN;
1606 	skb_set_network_header(skb, nw_off);
1607 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1608 		struct ipv6hdr *iph = ipv6_hdr(skb);
1609 
1610 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1611 		len = skb->len - skb_transport_offset(skb);
1612 		th = tcp_hdr(skb);
1613 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1614 	} else {
1615 		struct iphdr *iph = ip_hdr(skb);
1616 
1617 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1618 		len = skb->len - skb_transport_offset(skb);
1619 		th = tcp_hdr(skb);
1620 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1621 	}
1622 
1623 	if (inner_mac_off) { /* tunnel */
1624 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1625 					    ETH_HLEN - 2));
1626 
1627 		bnxt_gro_tunnel(skb, proto);
1628 	}
1629 #endif
1630 	return skb;
1631 }
1632 
1633 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1634 					   int payload_off, int tcp_ts,
1635 					   struct sk_buff *skb)
1636 {
1637 #ifdef CONFIG_INET
1638 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1639 	u32 hdr_info = tpa_info->hdr_info;
1640 	int iphdr_len, nw_off;
1641 
1642 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1643 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1644 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1645 
1646 	nw_off = inner_ip_off - ETH_HLEN;
1647 	skb_set_network_header(skb, nw_off);
1648 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1649 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1650 	skb_set_transport_header(skb, nw_off + iphdr_len);
1651 
1652 	if (inner_mac_off) { /* tunnel */
1653 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1654 					    ETH_HLEN - 2));
1655 
1656 		bnxt_gro_tunnel(skb, proto);
1657 	}
1658 #endif
1659 	return skb;
1660 }
1661 
1662 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1663 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1664 
1665 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1666 					   int payload_off, int tcp_ts,
1667 					   struct sk_buff *skb)
1668 {
1669 #ifdef CONFIG_INET
1670 	struct tcphdr *th;
1671 	int len, nw_off, tcp_opt_len = 0;
1672 
1673 	if (tcp_ts)
1674 		tcp_opt_len = 12;
1675 
1676 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1677 		struct iphdr *iph;
1678 
1679 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1680 			 ETH_HLEN;
1681 		skb_set_network_header(skb, nw_off);
1682 		iph = ip_hdr(skb);
1683 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1684 		len = skb->len - skb_transport_offset(skb);
1685 		th = tcp_hdr(skb);
1686 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1687 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1688 		struct ipv6hdr *iph;
1689 
1690 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1691 			 ETH_HLEN;
1692 		skb_set_network_header(skb, nw_off);
1693 		iph = ipv6_hdr(skb);
1694 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1695 		len = skb->len - skb_transport_offset(skb);
1696 		th = tcp_hdr(skb);
1697 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1698 	} else {
1699 		dev_kfree_skb_any(skb);
1700 		return NULL;
1701 	}
1702 
1703 	if (nw_off) /* tunnel */
1704 		bnxt_gro_tunnel(skb, skb->protocol);
1705 #endif
1706 	return skb;
1707 }
1708 
1709 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1710 					   struct bnxt_tpa_info *tpa_info,
1711 					   struct rx_tpa_end_cmp *tpa_end,
1712 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1713 					   struct sk_buff *skb)
1714 {
1715 #ifdef CONFIG_INET
1716 	int payload_off;
1717 	u16 segs;
1718 
1719 	segs = TPA_END_TPA_SEGS(tpa_end);
1720 	if (segs == 1)
1721 		return skb;
1722 
1723 	NAPI_GRO_CB(skb)->count = segs;
1724 	skb_shinfo(skb)->gso_size =
1725 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1726 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1727 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1728 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1729 	else
1730 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1731 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1732 	if (likely(skb))
1733 		tcp_gro_complete(skb);
1734 #endif
1735 	return skb;
1736 }
1737 
1738 /* Given the cfa_code of a received packet determine which
1739  * netdev (vf-rep or PF) the packet is destined to.
1740  */
1741 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1742 {
1743 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1744 
1745 	/* if vf-rep dev is NULL, the must belongs to the PF */
1746 	return dev ? dev : bp->dev;
1747 }
1748 
1749 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1750 					   struct bnxt_cp_ring_info *cpr,
1751 					   u32 *raw_cons,
1752 					   struct rx_tpa_end_cmp *tpa_end,
1753 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1754 					   u8 *event)
1755 {
1756 	struct bnxt_napi *bnapi = cpr->bnapi;
1757 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1758 	struct net_device *dev = bp->dev;
1759 	u8 *data_ptr, agg_bufs;
1760 	unsigned int len;
1761 	struct bnxt_tpa_info *tpa_info;
1762 	dma_addr_t mapping;
1763 	struct sk_buff *skb;
1764 	u16 idx = 0, agg_id;
1765 	void *data;
1766 	bool gro;
1767 
1768 	if (unlikely(bnapi->in_reset)) {
1769 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1770 
1771 		if (rc < 0)
1772 			return ERR_PTR(-EBUSY);
1773 		return NULL;
1774 	}
1775 
1776 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1777 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1778 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1779 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1780 		tpa_info = &rxr->rx_tpa[agg_id];
1781 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1782 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1783 				    agg_bufs, tpa_info->agg_count);
1784 			agg_bufs = tpa_info->agg_count;
1785 		}
1786 		tpa_info->agg_count = 0;
1787 		*event |= BNXT_AGG_EVENT;
1788 		bnxt_free_agg_idx(rxr, agg_id);
1789 		idx = agg_id;
1790 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1791 	} else {
1792 		agg_id = TPA_END_AGG_ID(tpa_end);
1793 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1794 		tpa_info = &rxr->rx_tpa[agg_id];
1795 		idx = RING_CMP(*raw_cons);
1796 		if (agg_bufs) {
1797 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1798 				return ERR_PTR(-EBUSY);
1799 
1800 			*event |= BNXT_AGG_EVENT;
1801 			idx = NEXT_CMP(idx);
1802 		}
1803 		gro = !!TPA_END_GRO(tpa_end);
1804 	}
1805 	data = tpa_info->data;
1806 	data_ptr = tpa_info->data_ptr;
1807 	prefetch(data_ptr);
1808 	len = tpa_info->len;
1809 	mapping = tpa_info->mapping;
1810 
1811 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1812 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1813 		if (agg_bufs > MAX_SKB_FRAGS)
1814 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1815 				    agg_bufs, (int)MAX_SKB_FRAGS);
1816 		return NULL;
1817 	}
1818 
1819 	if (len <= bp->rx_copy_thresh) {
1820 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1821 		if (!skb) {
1822 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1823 			cpr->sw_stats->rx.rx_oom_discards += 1;
1824 			return NULL;
1825 		}
1826 	} else {
1827 		u8 *new_data;
1828 		dma_addr_t new_mapping;
1829 
1830 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, GFP_ATOMIC);
1831 		if (!new_data) {
1832 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1833 			cpr->sw_stats->rx.rx_oom_discards += 1;
1834 			return NULL;
1835 		}
1836 
1837 		tpa_info->data = new_data;
1838 		tpa_info->data_ptr = new_data + bp->rx_offset;
1839 		tpa_info->mapping = new_mapping;
1840 
1841 		skb = napi_build_skb(data, bp->rx_buf_size);
1842 		dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1843 				       bp->rx_buf_use_size, bp->rx_dir,
1844 				       DMA_ATTR_WEAK_ORDERING);
1845 
1846 		if (!skb) {
1847 			skb_free_frag(data);
1848 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1849 			cpr->sw_stats->rx.rx_oom_discards += 1;
1850 			return NULL;
1851 		}
1852 		skb_reserve(skb, bp->rx_offset);
1853 		skb_put(skb, len);
1854 	}
1855 
1856 	if (agg_bufs) {
1857 		skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, idx, agg_bufs, true);
1858 		if (!skb) {
1859 			/* Page reuse already handled by bnxt_rx_pages(). */
1860 			cpr->sw_stats->rx.rx_oom_discards += 1;
1861 			return NULL;
1862 		}
1863 	}
1864 
1865 	if (tpa_info->cfa_code_valid)
1866 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1867 	skb->protocol = eth_type_trans(skb, dev);
1868 
1869 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1870 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1871 
1872 	if (tpa_info->vlan_valid &&
1873 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1874 		__be16 vlan_proto = htons(tpa_info->metadata >>
1875 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1876 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1877 
1878 		if (eth_type_vlan(vlan_proto)) {
1879 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1880 		} else {
1881 			dev_kfree_skb(skb);
1882 			return NULL;
1883 		}
1884 	}
1885 
1886 	skb_checksum_none_assert(skb);
1887 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1888 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1889 		skb->csum_level =
1890 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1891 	}
1892 
1893 	if (gro)
1894 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1895 
1896 	return skb;
1897 }
1898 
1899 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1900 			 struct rx_agg_cmp *rx_agg)
1901 {
1902 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1903 	struct bnxt_tpa_info *tpa_info;
1904 
1905 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1906 	tpa_info = &rxr->rx_tpa[agg_id];
1907 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1908 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1909 }
1910 
1911 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1912 			     struct sk_buff *skb)
1913 {
1914 	skb_mark_for_recycle(skb);
1915 
1916 	if (skb->dev != bp->dev) {
1917 		/* this packet belongs to a vf-rep */
1918 		bnxt_vf_rep_rx(bp, skb);
1919 		return;
1920 	}
1921 	skb_record_rx_queue(skb, bnapi->index);
1922 	napi_gro_receive(&bnapi->napi, skb);
1923 }
1924 
1925 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
1926 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
1927 {
1928 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
1929 
1930 	if (BNXT_PTP_RX_TS_VALID(flags))
1931 		goto ts_valid;
1932 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
1933 		return false;
1934 
1935 ts_valid:
1936 	*cmpl_ts = ts;
1937 	return true;
1938 }
1939 
1940 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
1941 				    struct rx_cmp *rxcmp,
1942 				    struct rx_cmp_ext *rxcmp1)
1943 {
1944 	__be16 vlan_proto;
1945 	u16 vtag;
1946 
1947 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1948 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
1949 		u32 meta_data;
1950 
1951 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
1952 			return skb;
1953 
1954 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1955 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1956 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
1957 		if (eth_type_vlan(vlan_proto))
1958 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1959 		else
1960 			goto vlan_err;
1961 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
1962 		if (RX_CMP_VLAN_VALID(rxcmp)) {
1963 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
1964 
1965 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
1966 				vlan_proto = htons(ETH_P_8021Q);
1967 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
1968 				vlan_proto = htons(ETH_P_8021AD);
1969 			else
1970 				goto vlan_err;
1971 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
1972 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1973 		}
1974 	}
1975 	return skb;
1976 vlan_err:
1977 	dev_kfree_skb(skb);
1978 	return NULL;
1979 }
1980 
1981 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp,
1982 					   struct rx_cmp *rxcmp)
1983 {
1984 	u8 ext_op;
1985 
1986 	ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp);
1987 	switch (ext_op) {
1988 	case EXT_OP_INNER_4:
1989 	case EXT_OP_OUTER_4:
1990 	case EXT_OP_INNFL_3:
1991 	case EXT_OP_OUTFL_3:
1992 		return PKT_HASH_TYPE_L4;
1993 	default:
1994 		return PKT_HASH_TYPE_L3;
1995 	}
1996 }
1997 
1998 /* returns the following:
1999  * 1       - 1 packet successfully received
2000  * 0       - successful TPA_START, packet not completed yet
2001  * -EBUSY  - completion ring does not have all the agg buffers yet
2002  * -ENOMEM - packet aborted due to out of memory
2003  * -EIO    - packet aborted due to hw error indicated in BD
2004  */
2005 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2006 		       u32 *raw_cons, u8 *event)
2007 {
2008 	struct bnxt_napi *bnapi = cpr->bnapi;
2009 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2010 	struct net_device *dev = bp->dev;
2011 	struct rx_cmp *rxcmp;
2012 	struct rx_cmp_ext *rxcmp1;
2013 	u32 tmp_raw_cons = *raw_cons;
2014 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2015 	struct bnxt_sw_rx_bd *rx_buf;
2016 	unsigned int len;
2017 	u8 *data_ptr, agg_bufs, cmp_type;
2018 	bool xdp_active = false;
2019 	dma_addr_t dma_addr;
2020 	struct sk_buff *skb;
2021 	struct xdp_buff xdp;
2022 	u32 flags, misc;
2023 	u32 cmpl_ts;
2024 	void *data;
2025 	int rc = 0;
2026 
2027 	rxcmp = (struct rx_cmp *)
2028 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2029 
2030 	cmp_type = RX_CMP_TYPE(rxcmp);
2031 
2032 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2033 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2034 		goto next_rx_no_prod_no_len;
2035 	}
2036 
2037 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2038 	cp_cons = RING_CMP(tmp_raw_cons);
2039 	rxcmp1 = (struct rx_cmp_ext *)
2040 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2041 
2042 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2043 		return -EBUSY;
2044 
2045 	/* The valid test of the entry must be done first before
2046 	 * reading any further.
2047 	 */
2048 	dma_rmb();
2049 	prod = rxr->rx_prod;
2050 
2051 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2052 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2053 		bnxt_tpa_start(bp, rxr, cmp_type,
2054 			       (struct rx_tpa_start_cmp *)rxcmp,
2055 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2056 
2057 		*event |= BNXT_RX_EVENT;
2058 		goto next_rx_no_prod_no_len;
2059 
2060 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2061 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2062 				   (struct rx_tpa_end_cmp *)rxcmp,
2063 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2064 
2065 		if (IS_ERR(skb))
2066 			return -EBUSY;
2067 
2068 		rc = -ENOMEM;
2069 		if (likely(skb)) {
2070 			bnxt_deliver_skb(bp, bnapi, skb);
2071 			rc = 1;
2072 		}
2073 		*event |= BNXT_RX_EVENT;
2074 		goto next_rx_no_prod_no_len;
2075 	}
2076 
2077 	cons = rxcmp->rx_cmp_opaque;
2078 	if (unlikely(cons != rxr->rx_next_cons)) {
2079 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2080 
2081 		/* 0xffff is forced error, don't print it */
2082 		if (rxr->rx_next_cons != 0xffff)
2083 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2084 				    cons, rxr->rx_next_cons);
2085 		bnxt_sched_reset_rxr(bp, rxr);
2086 		if (rc1)
2087 			return rc1;
2088 		goto next_rx_no_prod_no_len;
2089 	}
2090 	rx_buf = &rxr->rx_buf_ring[cons];
2091 	data = rx_buf->data;
2092 	data_ptr = rx_buf->data_ptr;
2093 	prefetch(data_ptr);
2094 
2095 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2096 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2097 
2098 	if (agg_bufs) {
2099 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2100 			return -EBUSY;
2101 
2102 		cp_cons = NEXT_CMP(cp_cons);
2103 		*event |= BNXT_AGG_EVENT;
2104 	}
2105 	*event |= BNXT_RX_EVENT;
2106 
2107 	rx_buf->data = NULL;
2108 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2109 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2110 
2111 		bnxt_reuse_rx_data(rxr, cons, data);
2112 		if (agg_bufs)
2113 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2114 					       false);
2115 
2116 		rc = -EIO;
2117 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2118 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2119 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2120 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2121 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2122 						 rx_err);
2123 				bnxt_sched_reset_rxr(bp, rxr);
2124 			}
2125 		}
2126 		goto next_rx_no_len;
2127 	}
2128 
2129 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2130 	len = flags >> RX_CMP_LEN_SHIFT;
2131 	dma_addr = rx_buf->mapping;
2132 
2133 	if (bnxt_xdp_attached(bp, rxr)) {
2134 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2135 		if (agg_bufs) {
2136 			u32 frag_len = bnxt_rx_agg_pages_xdp(bp, cpr, &xdp,
2137 							     cp_cons, agg_bufs,
2138 							     false);
2139 			if (!frag_len)
2140 				goto oom_next_rx;
2141 		}
2142 		xdp_active = true;
2143 	}
2144 
2145 	if (xdp_active) {
2146 		if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) {
2147 			rc = 1;
2148 			goto next_rx;
2149 		}
2150 	}
2151 
2152 	if (len <= bp->rx_copy_thresh) {
2153 		if (!xdp_active)
2154 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2155 		else
2156 			skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr);
2157 		bnxt_reuse_rx_data(rxr, cons, data);
2158 		if (!skb) {
2159 			if (agg_bufs) {
2160 				if (!xdp_active)
2161 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2162 							       agg_bufs, false);
2163 				else
2164 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2165 			}
2166 			goto oom_next_rx;
2167 		}
2168 	} else {
2169 		u32 payload;
2170 
2171 		if (rx_buf->data_ptr == data_ptr)
2172 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2173 		else
2174 			payload = 0;
2175 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2176 				      payload | len);
2177 		if (!skb)
2178 			goto oom_next_rx;
2179 	}
2180 
2181 	if (agg_bufs) {
2182 		if (!xdp_active) {
2183 			skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, cp_cons, agg_bufs, false);
2184 			if (!skb)
2185 				goto oom_next_rx;
2186 		} else {
2187 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, rxr->page_pool, &xdp, rxcmp1);
2188 			if (!skb) {
2189 				/* we should be able to free the old skb here */
2190 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2191 				goto oom_next_rx;
2192 			}
2193 		}
2194 	}
2195 
2196 	if (RX_CMP_HASH_VALID(rxcmp)) {
2197 		enum pkt_hash_types type;
2198 
2199 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2200 			type = bnxt_rss_ext_op(bp, rxcmp);
2201 		} else {
2202 			u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
2203 
2204 			/* RSS profiles 1 and 3 with extract code 0 for inner
2205 			 * 4-tuple
2206 			 */
2207 			if (hash_type != 1 && hash_type != 3)
2208 				type = PKT_HASH_TYPE_L3;
2209 			else
2210 				type = PKT_HASH_TYPE_L4;
2211 		}
2212 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2213 	}
2214 
2215 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2216 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2217 	skb->protocol = eth_type_trans(skb, dev);
2218 
2219 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2220 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2221 		if (!skb)
2222 			goto next_rx;
2223 	}
2224 
2225 	skb_checksum_none_assert(skb);
2226 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2227 		if (dev->features & NETIF_F_RXCSUM) {
2228 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2229 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2230 		}
2231 	} else {
2232 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2233 			if (dev->features & NETIF_F_RXCSUM)
2234 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2235 		}
2236 	}
2237 
2238 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2239 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2240 			u64 ns, ts;
2241 
2242 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2243 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2244 
2245 				spin_lock_bh(&ptp->ptp_lock);
2246 				ns = timecounter_cyc2time(&ptp->tc, ts);
2247 				spin_unlock_bh(&ptp->ptp_lock);
2248 				memset(skb_hwtstamps(skb), 0,
2249 				       sizeof(*skb_hwtstamps(skb)));
2250 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2251 			}
2252 		}
2253 	}
2254 	bnxt_deliver_skb(bp, bnapi, skb);
2255 	rc = 1;
2256 
2257 next_rx:
2258 	cpr->rx_packets += 1;
2259 	cpr->rx_bytes += len;
2260 
2261 next_rx_no_len:
2262 	rxr->rx_prod = NEXT_RX(prod);
2263 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2264 
2265 next_rx_no_prod_no_len:
2266 	*raw_cons = tmp_raw_cons;
2267 
2268 	return rc;
2269 
2270 oom_next_rx:
2271 	cpr->sw_stats->rx.rx_oom_discards += 1;
2272 	rc = -ENOMEM;
2273 	goto next_rx;
2274 }
2275 
2276 /* In netpoll mode, if we are using a combined completion ring, we need to
2277  * discard the rx packets and recycle the buffers.
2278  */
2279 static int bnxt_force_rx_discard(struct bnxt *bp,
2280 				 struct bnxt_cp_ring_info *cpr,
2281 				 u32 *raw_cons, u8 *event)
2282 {
2283 	u32 tmp_raw_cons = *raw_cons;
2284 	struct rx_cmp_ext *rxcmp1;
2285 	struct rx_cmp *rxcmp;
2286 	u16 cp_cons;
2287 	u8 cmp_type;
2288 	int rc;
2289 
2290 	cp_cons = RING_CMP(tmp_raw_cons);
2291 	rxcmp = (struct rx_cmp *)
2292 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2293 
2294 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2295 	cp_cons = RING_CMP(tmp_raw_cons);
2296 	rxcmp1 = (struct rx_cmp_ext *)
2297 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2298 
2299 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2300 		return -EBUSY;
2301 
2302 	/* The valid test of the entry must be done first before
2303 	 * reading any further.
2304 	 */
2305 	dma_rmb();
2306 	cmp_type = RX_CMP_TYPE(rxcmp);
2307 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2308 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2309 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2310 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2311 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2312 		struct rx_tpa_end_cmp_ext *tpa_end1;
2313 
2314 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2315 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2316 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2317 	}
2318 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2319 	if (rc && rc != -EBUSY)
2320 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2321 	return rc;
2322 }
2323 
2324 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2325 {
2326 	struct bnxt_fw_health *fw_health = bp->fw_health;
2327 	u32 reg = fw_health->regs[reg_idx];
2328 	u32 reg_type, reg_off, val = 0;
2329 
2330 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2331 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2332 	switch (reg_type) {
2333 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2334 		pci_read_config_dword(bp->pdev, reg_off, &val);
2335 		break;
2336 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2337 		reg_off = fw_health->mapped_regs[reg_idx];
2338 		fallthrough;
2339 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2340 		val = readl(bp->bar0 + reg_off);
2341 		break;
2342 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2343 		val = readl(bp->bar1 + reg_off);
2344 		break;
2345 	}
2346 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2347 		val &= fw_health->fw_reset_inprog_reg_mask;
2348 	return val;
2349 }
2350 
2351 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2352 {
2353 	int i;
2354 
2355 	for (i = 0; i < bp->rx_nr_rings; i++) {
2356 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2357 		struct bnxt_ring_grp_info *grp_info;
2358 
2359 		grp_info = &bp->grp_info[grp_idx];
2360 		if (grp_info->agg_fw_ring_id == ring_id)
2361 			return grp_idx;
2362 	}
2363 	return INVALID_HW_RING_ID;
2364 }
2365 
2366 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2367 {
2368 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2369 
2370 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2371 		return link_info->force_link_speed2;
2372 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2373 		return link_info->force_pam4_link_speed;
2374 	return link_info->force_link_speed;
2375 }
2376 
2377 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2378 {
2379 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2380 
2381 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2382 		link_info->req_link_speed = link_info->force_link_speed2;
2383 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2384 		switch (link_info->req_link_speed) {
2385 		case BNXT_LINK_SPEED_50GB_PAM4:
2386 		case BNXT_LINK_SPEED_100GB_PAM4:
2387 		case BNXT_LINK_SPEED_200GB_PAM4:
2388 		case BNXT_LINK_SPEED_400GB_PAM4:
2389 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2390 			break;
2391 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2392 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2393 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2394 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2395 			break;
2396 		default:
2397 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2398 		}
2399 		return;
2400 	}
2401 	link_info->req_link_speed = link_info->force_link_speed;
2402 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2403 	if (link_info->force_pam4_link_speed) {
2404 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2405 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2406 	}
2407 }
2408 
2409 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2410 {
2411 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2412 
2413 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2414 		link_info->advertising = link_info->auto_link_speeds2;
2415 		return;
2416 	}
2417 	link_info->advertising = link_info->auto_link_speeds;
2418 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2419 }
2420 
2421 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2422 {
2423 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2424 
2425 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2426 		if (link_info->req_link_speed != link_info->force_link_speed2)
2427 			return true;
2428 		return false;
2429 	}
2430 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2431 	    link_info->req_link_speed != link_info->force_link_speed)
2432 		return true;
2433 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2434 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2435 		return true;
2436 	return false;
2437 }
2438 
2439 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2440 {
2441 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2442 
2443 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2444 		if (link_info->advertising != link_info->auto_link_speeds2)
2445 			return true;
2446 		return false;
2447 	}
2448 	if (link_info->advertising != link_info->auto_link_speeds ||
2449 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2450 		return true;
2451 	return false;
2452 }
2453 
2454 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2455 	((data2) &							\
2456 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2457 
2458 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2459 	(((data2) &							\
2460 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2461 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2462 
2463 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2464 	((data1) &							\
2465 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2466 
2467 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2468 	(((data1) &							\
2469 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2470 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2471 
2472 /* Return true if the workqueue has to be scheduled */
2473 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2474 {
2475 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2476 
2477 	switch (err_type) {
2478 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2479 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2480 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2481 		break;
2482 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2483 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2484 		break;
2485 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2486 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2487 		break;
2488 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2489 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2490 		char *threshold_type;
2491 		bool notify = false;
2492 		char *dir_str;
2493 
2494 		switch (type) {
2495 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2496 			threshold_type = "warning";
2497 			break;
2498 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2499 			threshold_type = "critical";
2500 			break;
2501 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2502 			threshold_type = "fatal";
2503 			break;
2504 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2505 			threshold_type = "shutdown";
2506 			break;
2507 		default:
2508 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2509 			return false;
2510 		}
2511 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2512 			dir_str = "above";
2513 			notify = true;
2514 		} else {
2515 			dir_str = "below";
2516 		}
2517 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2518 			    dir_str, threshold_type);
2519 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2520 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2521 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2522 		if (notify) {
2523 			bp->thermal_threshold_type = type;
2524 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2525 			return true;
2526 		}
2527 		return false;
2528 	}
2529 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2530 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2531 		break;
2532 	default:
2533 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2534 			   err_type);
2535 		break;
2536 	}
2537 	return false;
2538 }
2539 
2540 #define BNXT_GET_EVENT_PORT(data)	\
2541 	((data) &			\
2542 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2543 
2544 #define BNXT_EVENT_RING_TYPE(data2)	\
2545 	((data2) &			\
2546 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2547 
2548 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2549 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2550 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2551 
2552 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2553 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2554 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2555 
2556 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2557 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2558 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2559 
2560 #define BNXT_PHC_BITS	48
2561 
2562 static int bnxt_async_event_process(struct bnxt *bp,
2563 				    struct hwrm_async_event_cmpl *cmpl)
2564 {
2565 	u16 event_id = le16_to_cpu(cmpl->event_id);
2566 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2567 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2568 
2569 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2570 		   event_id, data1, data2);
2571 
2572 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2573 	switch (event_id) {
2574 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2575 		struct bnxt_link_info *link_info = &bp->link_info;
2576 
2577 		if (BNXT_VF(bp))
2578 			goto async_event_process_exit;
2579 
2580 		/* print unsupported speed warning in forced speed mode only */
2581 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2582 		    (data1 & 0x20000)) {
2583 			u16 fw_speed = bnxt_get_force_speed(link_info);
2584 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2585 
2586 			if (speed != SPEED_UNKNOWN)
2587 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2588 					    speed);
2589 		}
2590 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2591 	}
2592 		fallthrough;
2593 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2594 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2595 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2596 		fallthrough;
2597 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2598 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2599 		break;
2600 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2601 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2602 		break;
2603 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2604 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2605 
2606 		if (BNXT_VF(bp))
2607 			break;
2608 
2609 		if (bp->pf.port_id != port_id)
2610 			break;
2611 
2612 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2613 		break;
2614 	}
2615 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2616 		if (BNXT_PF(bp))
2617 			goto async_event_process_exit;
2618 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2619 		break;
2620 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2621 		char *type_str = "Solicited";
2622 
2623 		if (!bp->fw_health)
2624 			goto async_event_process_exit;
2625 
2626 		bp->fw_reset_timestamp = jiffies;
2627 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2628 		if (!bp->fw_reset_min_dsecs)
2629 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2630 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2631 		if (!bp->fw_reset_max_dsecs)
2632 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2633 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2634 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2635 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2636 			type_str = "Fatal";
2637 			bp->fw_health->fatalities++;
2638 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2639 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2640 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2641 			type_str = "Non-fatal";
2642 			bp->fw_health->survivals++;
2643 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2644 		}
2645 		netif_warn(bp, hw, bp->dev,
2646 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2647 			   type_str, data1, data2,
2648 			   bp->fw_reset_min_dsecs * 100,
2649 			   bp->fw_reset_max_dsecs * 100);
2650 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2651 		break;
2652 	}
2653 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2654 		struct bnxt_fw_health *fw_health = bp->fw_health;
2655 		char *status_desc = "healthy";
2656 		u32 status;
2657 
2658 		if (!fw_health)
2659 			goto async_event_process_exit;
2660 
2661 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2662 			fw_health->enabled = false;
2663 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2664 			break;
2665 		}
2666 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2667 		fw_health->tmr_multiplier =
2668 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2669 				     bp->current_interval * 10);
2670 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2671 		if (!fw_health->enabled)
2672 			fw_health->last_fw_heartbeat =
2673 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2674 		fw_health->last_fw_reset_cnt =
2675 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2676 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2677 		if (status != BNXT_FW_STATUS_HEALTHY)
2678 			status_desc = "unhealthy";
2679 		netif_info(bp, drv, bp->dev,
2680 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2681 			   fw_health->primary ? "primary" : "backup", status,
2682 			   status_desc, fw_health->last_fw_reset_cnt);
2683 		if (!fw_health->enabled) {
2684 			/* Make sure tmr_counter is set and visible to
2685 			 * bnxt_health_check() before setting enabled to true.
2686 			 */
2687 			smp_wmb();
2688 			fw_health->enabled = true;
2689 		}
2690 		goto async_event_process_exit;
2691 	}
2692 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2693 		netif_notice(bp, hw, bp->dev,
2694 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2695 			     data1, data2);
2696 		goto async_event_process_exit;
2697 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2698 		struct bnxt_rx_ring_info *rxr;
2699 		u16 grp_idx;
2700 
2701 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2702 			goto async_event_process_exit;
2703 
2704 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2705 			    BNXT_EVENT_RING_TYPE(data2), data1);
2706 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2707 			goto async_event_process_exit;
2708 
2709 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2710 		if (grp_idx == INVALID_HW_RING_ID) {
2711 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2712 				    data1);
2713 			goto async_event_process_exit;
2714 		}
2715 		rxr = bp->bnapi[grp_idx]->rx_ring;
2716 		bnxt_sched_reset_rxr(bp, rxr);
2717 		goto async_event_process_exit;
2718 	}
2719 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2720 		struct bnxt_fw_health *fw_health = bp->fw_health;
2721 
2722 		netif_notice(bp, hw, bp->dev,
2723 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2724 			     data1, data2);
2725 		if (fw_health) {
2726 			fw_health->echo_req_data1 = data1;
2727 			fw_health->echo_req_data2 = data2;
2728 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2729 			break;
2730 		}
2731 		goto async_event_process_exit;
2732 	}
2733 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2734 		bnxt_ptp_pps_event(bp, data1, data2);
2735 		goto async_event_process_exit;
2736 	}
2737 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2738 		if (bnxt_event_error_report(bp, data1, data2))
2739 			break;
2740 		goto async_event_process_exit;
2741 	}
2742 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2743 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2744 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2745 			if (BNXT_PTP_USE_RTC(bp)) {
2746 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2747 				u64 ns;
2748 
2749 				if (!ptp)
2750 					goto async_event_process_exit;
2751 
2752 				spin_lock_bh(&ptp->ptp_lock);
2753 				bnxt_ptp_update_current_time(bp);
2754 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2755 				       BNXT_PHC_BITS) | ptp->current_time);
2756 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2757 				spin_unlock_bh(&ptp->ptp_lock);
2758 			}
2759 			break;
2760 		}
2761 		goto async_event_process_exit;
2762 	}
2763 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2764 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2765 
2766 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2767 		goto async_event_process_exit;
2768 	}
2769 	default:
2770 		goto async_event_process_exit;
2771 	}
2772 	__bnxt_queue_sp_work(bp);
2773 async_event_process_exit:
2774 	return 0;
2775 }
2776 
2777 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2778 {
2779 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2780 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2781 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2782 				(struct hwrm_fwd_req_cmpl *)txcmp;
2783 
2784 	switch (cmpl_type) {
2785 	case CMPL_BASE_TYPE_HWRM_DONE:
2786 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2787 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2788 		break;
2789 
2790 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2791 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2792 
2793 		if ((vf_id < bp->pf.first_vf_id) ||
2794 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2795 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2796 				   vf_id);
2797 			return -EINVAL;
2798 		}
2799 
2800 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2801 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2802 		break;
2803 
2804 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2805 		bnxt_async_event_process(bp,
2806 					 (struct hwrm_async_event_cmpl *)txcmp);
2807 		break;
2808 
2809 	default:
2810 		break;
2811 	}
2812 
2813 	return 0;
2814 }
2815 
2816 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2817 {
2818 	struct bnxt_napi *bnapi = dev_instance;
2819 	struct bnxt *bp = bnapi->bp;
2820 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2821 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2822 
2823 	cpr->event_ctr++;
2824 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2825 	napi_schedule(&bnapi->napi);
2826 	return IRQ_HANDLED;
2827 }
2828 
2829 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2830 {
2831 	u32 raw_cons = cpr->cp_raw_cons;
2832 	u16 cons = RING_CMP(raw_cons);
2833 	struct tx_cmp *txcmp;
2834 
2835 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2836 
2837 	return TX_CMP_VALID(txcmp, raw_cons);
2838 }
2839 
2840 static irqreturn_t bnxt_inta(int irq, void *dev_instance)
2841 {
2842 	struct bnxt_napi *bnapi = dev_instance;
2843 	struct bnxt *bp = bnapi->bp;
2844 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2845 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2846 	u32 int_status;
2847 
2848 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2849 
2850 	if (!bnxt_has_work(bp, cpr)) {
2851 		int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
2852 		/* return if erroneous interrupt */
2853 		if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
2854 			return IRQ_NONE;
2855 	}
2856 
2857 	/* disable ring IRQ */
2858 	BNXT_CP_DB_IRQ_DIS(cpr->cp_db.doorbell);
2859 
2860 	/* Return here if interrupt is shared and is disabled. */
2861 	if (unlikely(atomic_read(&bp->intr_sem) != 0))
2862 		return IRQ_HANDLED;
2863 
2864 	napi_schedule(&bnapi->napi);
2865 	return IRQ_HANDLED;
2866 }
2867 
2868 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2869 			    int budget)
2870 {
2871 	struct bnxt_napi *bnapi = cpr->bnapi;
2872 	u32 raw_cons = cpr->cp_raw_cons;
2873 	u32 cons;
2874 	int rx_pkts = 0;
2875 	u8 event = 0;
2876 	struct tx_cmp *txcmp;
2877 
2878 	cpr->has_more_work = 0;
2879 	cpr->had_work_done = 1;
2880 	while (1) {
2881 		u8 cmp_type;
2882 		int rc;
2883 
2884 		cons = RING_CMP(raw_cons);
2885 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2886 
2887 		if (!TX_CMP_VALID(txcmp, raw_cons))
2888 			break;
2889 
2890 		/* The valid test of the entry must be done first before
2891 		 * reading any further.
2892 		 */
2893 		dma_rmb();
2894 		cmp_type = TX_CMP_TYPE(txcmp);
2895 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
2896 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
2897 			u32 opaque = txcmp->tx_cmp_opaque;
2898 			struct bnxt_tx_ring_info *txr;
2899 			u16 tx_freed;
2900 
2901 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
2902 			event |= BNXT_TX_CMP_EVENT;
2903 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
2904 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
2905 			else
2906 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
2907 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
2908 				   bp->tx_ring_mask;
2909 			/* return full budget so NAPI will complete. */
2910 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
2911 				rx_pkts = budget;
2912 				raw_cons = NEXT_RAW_CMP(raw_cons);
2913 				if (budget)
2914 					cpr->has_more_work = 1;
2915 				break;
2916 			}
2917 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
2918 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2919 			if (likely(budget))
2920 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2921 			else
2922 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2923 							   &event);
2924 			if (likely(rc >= 0))
2925 				rx_pkts += rc;
2926 			/* Increment rx_pkts when rc is -ENOMEM to count towards
2927 			 * the NAPI budget.  Otherwise, we may potentially loop
2928 			 * here forever if we consistently cannot allocate
2929 			 * buffers.
2930 			 */
2931 			else if (rc == -ENOMEM && budget)
2932 				rx_pkts++;
2933 			else if (rc == -EBUSY)	/* partial completion */
2934 				break;
2935 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
2936 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
2937 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
2938 			bnxt_hwrm_handler(bp, txcmp);
2939 		}
2940 		raw_cons = NEXT_RAW_CMP(raw_cons);
2941 
2942 		if (rx_pkts && rx_pkts == budget) {
2943 			cpr->has_more_work = 1;
2944 			break;
2945 		}
2946 	}
2947 
2948 	if (event & BNXT_REDIRECT_EVENT)
2949 		xdp_do_flush();
2950 
2951 	if (event & BNXT_TX_EVENT) {
2952 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
2953 		u16 prod = txr->tx_prod;
2954 
2955 		/* Sync BD data before updating doorbell */
2956 		wmb();
2957 
2958 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
2959 	}
2960 
2961 	cpr->cp_raw_cons = raw_cons;
2962 	bnapi->events |= event;
2963 	return rx_pkts;
2964 }
2965 
2966 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
2967 				  int budget)
2968 {
2969 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
2970 		bnapi->tx_int(bp, bnapi, budget);
2971 
2972 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
2973 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2974 
2975 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2976 	}
2977 	if (bnapi->events & BNXT_AGG_EVENT) {
2978 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2979 
2980 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2981 	}
2982 	bnapi->events &= BNXT_TX_CMP_EVENT;
2983 }
2984 
2985 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2986 			  int budget)
2987 {
2988 	struct bnxt_napi *bnapi = cpr->bnapi;
2989 	int rx_pkts;
2990 
2991 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
2992 
2993 	/* ACK completion ring before freeing tx ring and producing new
2994 	 * buffers in rx/agg rings to prevent overflowing the completion
2995 	 * ring.
2996 	 */
2997 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
2998 
2999 	__bnxt_poll_work_done(bp, bnapi, budget);
3000 	return rx_pkts;
3001 }
3002 
3003 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3004 {
3005 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3006 	struct bnxt *bp = bnapi->bp;
3007 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3008 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3009 	struct tx_cmp *txcmp;
3010 	struct rx_cmp_ext *rxcmp1;
3011 	u32 cp_cons, tmp_raw_cons;
3012 	u32 raw_cons = cpr->cp_raw_cons;
3013 	bool flush_xdp = false;
3014 	u32 rx_pkts = 0;
3015 	u8 event = 0;
3016 
3017 	while (1) {
3018 		int rc;
3019 
3020 		cp_cons = RING_CMP(raw_cons);
3021 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3022 
3023 		if (!TX_CMP_VALID(txcmp, raw_cons))
3024 			break;
3025 
3026 		/* The valid test of the entry must be done first before
3027 		 * reading any further.
3028 		 */
3029 		dma_rmb();
3030 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3031 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3032 			cp_cons = RING_CMP(tmp_raw_cons);
3033 			rxcmp1 = (struct rx_cmp_ext *)
3034 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3035 
3036 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3037 				break;
3038 
3039 			/* force an error to recycle the buffer */
3040 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3041 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3042 
3043 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3044 			if (likely(rc == -EIO) && budget)
3045 				rx_pkts++;
3046 			else if (rc == -EBUSY)	/* partial completion */
3047 				break;
3048 			if (event & BNXT_REDIRECT_EVENT)
3049 				flush_xdp = true;
3050 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3051 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3052 			bnxt_hwrm_handler(bp, txcmp);
3053 		} else {
3054 			netdev_err(bp->dev,
3055 				   "Invalid completion received on special ring\n");
3056 		}
3057 		raw_cons = NEXT_RAW_CMP(raw_cons);
3058 
3059 		if (rx_pkts == budget)
3060 			break;
3061 	}
3062 
3063 	cpr->cp_raw_cons = raw_cons;
3064 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3065 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3066 
3067 	if (event & BNXT_AGG_EVENT)
3068 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3069 	if (flush_xdp)
3070 		xdp_do_flush();
3071 
3072 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3073 		napi_complete_done(napi, rx_pkts);
3074 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3075 	}
3076 	return rx_pkts;
3077 }
3078 
3079 static int bnxt_poll(struct napi_struct *napi, int budget)
3080 {
3081 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3082 	struct bnxt *bp = bnapi->bp;
3083 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3084 	int work_done = 0;
3085 
3086 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3087 		napi_complete(napi);
3088 		return 0;
3089 	}
3090 	while (1) {
3091 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3092 
3093 		if (work_done >= budget) {
3094 			if (!budget)
3095 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3096 			break;
3097 		}
3098 
3099 		if (!bnxt_has_work(bp, cpr)) {
3100 			if (napi_complete_done(napi, work_done))
3101 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3102 			break;
3103 		}
3104 	}
3105 	if (bp->flags & BNXT_FLAG_DIM) {
3106 		struct dim_sample dim_sample = {};
3107 
3108 		dim_update_sample(cpr->event_ctr,
3109 				  cpr->rx_packets,
3110 				  cpr->rx_bytes,
3111 				  &dim_sample);
3112 		net_dim(&cpr->dim, dim_sample);
3113 	}
3114 	return work_done;
3115 }
3116 
3117 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3118 {
3119 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3120 	int i, work_done = 0;
3121 
3122 	for (i = 0; i < cpr->cp_ring_count; i++) {
3123 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3124 
3125 		if (cpr2->had_nqe_notify) {
3126 			work_done += __bnxt_poll_work(bp, cpr2,
3127 						      budget - work_done);
3128 			cpr->has_more_work |= cpr2->has_more_work;
3129 		}
3130 	}
3131 	return work_done;
3132 }
3133 
3134 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3135 				 u64 dbr_type, int budget)
3136 {
3137 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3138 	int i;
3139 
3140 	for (i = 0; i < cpr->cp_ring_count; i++) {
3141 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3142 		struct bnxt_db_info *db;
3143 
3144 		if (cpr2->had_work_done) {
3145 			u32 tgl = 0;
3146 
3147 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3148 				cpr2->had_nqe_notify = 0;
3149 				tgl = cpr2->toggle;
3150 			}
3151 			db = &cpr2->cp_db;
3152 			bnxt_writeq(bp,
3153 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3154 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3155 				    db->doorbell);
3156 			cpr2->had_work_done = 0;
3157 		}
3158 	}
3159 	__bnxt_poll_work_done(bp, bnapi, budget);
3160 }
3161 
3162 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3163 {
3164 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3165 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3166 	struct bnxt_cp_ring_info *cpr_rx;
3167 	u32 raw_cons = cpr->cp_raw_cons;
3168 	struct bnxt *bp = bnapi->bp;
3169 	struct nqe_cn *nqcmp;
3170 	int work_done = 0;
3171 	u32 cons;
3172 
3173 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3174 		napi_complete(napi);
3175 		return 0;
3176 	}
3177 	if (cpr->has_more_work) {
3178 		cpr->has_more_work = 0;
3179 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3180 	}
3181 	while (1) {
3182 		u16 type;
3183 
3184 		cons = RING_CMP(raw_cons);
3185 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3186 
3187 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3188 			if (cpr->has_more_work)
3189 				break;
3190 
3191 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3192 					     budget);
3193 			cpr->cp_raw_cons = raw_cons;
3194 			if (napi_complete_done(napi, work_done))
3195 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3196 						  cpr->cp_raw_cons);
3197 			goto poll_done;
3198 		}
3199 
3200 		/* The valid test of the entry must be done first before
3201 		 * reading any further.
3202 		 */
3203 		dma_rmb();
3204 
3205 		type = le16_to_cpu(nqcmp->type);
3206 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3207 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3208 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3209 			struct bnxt_cp_ring_info *cpr2;
3210 
3211 			/* No more budget for RX work */
3212 			if (budget && work_done >= budget &&
3213 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3214 				break;
3215 
3216 			idx = BNXT_NQ_HDL_IDX(idx);
3217 			cpr2 = &cpr->cp_ring_arr[idx];
3218 			cpr2->had_nqe_notify = 1;
3219 			cpr2->toggle = NQE_CN_TOGGLE(type);
3220 			work_done += __bnxt_poll_work(bp, cpr2,
3221 						      budget - work_done);
3222 			cpr->has_more_work |= cpr2->has_more_work;
3223 		} else {
3224 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3225 		}
3226 		raw_cons = NEXT_RAW_CMP(raw_cons);
3227 	}
3228 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3229 	if (raw_cons != cpr->cp_raw_cons) {
3230 		cpr->cp_raw_cons = raw_cons;
3231 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3232 	}
3233 poll_done:
3234 	cpr_rx = &cpr->cp_ring_arr[0];
3235 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3236 	    (bp->flags & BNXT_FLAG_DIM)) {
3237 		struct dim_sample dim_sample = {};
3238 
3239 		dim_update_sample(cpr->event_ctr,
3240 				  cpr_rx->rx_packets,
3241 				  cpr_rx->rx_bytes,
3242 				  &dim_sample);
3243 		net_dim(&cpr->dim, dim_sample);
3244 	}
3245 	return work_done;
3246 }
3247 
3248 static void bnxt_free_tx_skbs(struct bnxt *bp)
3249 {
3250 	int i, max_idx;
3251 	struct pci_dev *pdev = bp->pdev;
3252 
3253 	if (!bp->tx_ring)
3254 		return;
3255 
3256 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3257 	for (i = 0; i < bp->tx_nr_rings; i++) {
3258 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3259 		int j;
3260 
3261 		if (!txr->tx_buf_ring)
3262 			continue;
3263 
3264 		for (j = 0; j < max_idx;) {
3265 			struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
3266 			struct sk_buff *skb;
3267 			int k, last;
3268 
3269 			if (i < bp->tx_nr_rings_xdp &&
3270 			    tx_buf->action == XDP_REDIRECT) {
3271 				dma_unmap_single(&pdev->dev,
3272 					dma_unmap_addr(tx_buf, mapping),
3273 					dma_unmap_len(tx_buf, len),
3274 					DMA_TO_DEVICE);
3275 				xdp_return_frame(tx_buf->xdpf);
3276 				tx_buf->action = 0;
3277 				tx_buf->xdpf = NULL;
3278 				j++;
3279 				continue;
3280 			}
3281 
3282 			skb = tx_buf->skb;
3283 			if (!skb) {
3284 				j++;
3285 				continue;
3286 			}
3287 
3288 			tx_buf->skb = NULL;
3289 
3290 			if (tx_buf->is_push) {
3291 				dev_kfree_skb(skb);
3292 				j += 2;
3293 				continue;
3294 			}
3295 
3296 			dma_unmap_single(&pdev->dev,
3297 					 dma_unmap_addr(tx_buf, mapping),
3298 					 skb_headlen(skb),
3299 					 DMA_TO_DEVICE);
3300 
3301 			last = tx_buf->nr_frags;
3302 			j += 2;
3303 			for (k = 0; k < last; k++, j++) {
3304 				int ring_idx = j & bp->tx_ring_mask;
3305 				skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
3306 
3307 				tx_buf = &txr->tx_buf_ring[ring_idx];
3308 				dma_unmap_page(
3309 					&pdev->dev,
3310 					dma_unmap_addr(tx_buf, mapping),
3311 					skb_frag_size(frag), DMA_TO_DEVICE);
3312 			}
3313 			dev_kfree_skb(skb);
3314 		}
3315 		netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
3316 	}
3317 }
3318 
3319 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3320 {
3321 	struct pci_dev *pdev = bp->pdev;
3322 	int i, max_idx;
3323 
3324 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3325 
3326 	for (i = 0; i < max_idx; i++) {
3327 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3328 		dma_addr_t mapping = rx_buf->mapping;
3329 		void *data = rx_buf->data;
3330 
3331 		if (!data)
3332 			continue;
3333 
3334 		rx_buf->data = NULL;
3335 		if (BNXT_RX_PAGE_MODE(bp)) {
3336 			page_pool_recycle_direct(rxr->page_pool, data);
3337 		} else {
3338 			dma_unmap_single_attrs(&pdev->dev, mapping,
3339 					       bp->rx_buf_use_size, bp->rx_dir,
3340 					       DMA_ATTR_WEAK_ORDERING);
3341 			skb_free_frag(data);
3342 		}
3343 	}
3344 }
3345 
3346 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3347 {
3348 	int i, max_idx;
3349 
3350 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3351 
3352 	for (i = 0; i < max_idx; i++) {
3353 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3354 		struct page *page = rx_agg_buf->page;
3355 
3356 		if (!page)
3357 			continue;
3358 
3359 		rx_agg_buf->page = NULL;
3360 		__clear_bit(i, rxr->rx_agg_bmap);
3361 
3362 		page_pool_recycle_direct(rxr->page_pool, page);
3363 	}
3364 }
3365 
3366 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, int ring_nr)
3367 {
3368 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
3369 	struct pci_dev *pdev = bp->pdev;
3370 	struct bnxt_tpa_idx_map *map;
3371 	int i;
3372 
3373 	if (!rxr->rx_tpa)
3374 		goto skip_rx_tpa_free;
3375 
3376 	for (i = 0; i < bp->max_tpa; i++) {
3377 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3378 		u8 *data = tpa_info->data;
3379 
3380 		if (!data)
3381 			continue;
3382 
3383 		dma_unmap_single_attrs(&pdev->dev, tpa_info->mapping,
3384 				       bp->rx_buf_use_size, bp->rx_dir,
3385 				       DMA_ATTR_WEAK_ORDERING);
3386 
3387 		tpa_info->data = NULL;
3388 
3389 		skb_free_frag(data);
3390 	}
3391 
3392 skip_rx_tpa_free:
3393 	if (!rxr->rx_buf_ring)
3394 		goto skip_rx_buf_free;
3395 
3396 	bnxt_free_one_rx_ring(bp, rxr);
3397 
3398 skip_rx_buf_free:
3399 	if (!rxr->rx_agg_ring)
3400 		goto skip_rx_agg_free;
3401 
3402 	bnxt_free_one_rx_agg_ring(bp, rxr);
3403 
3404 skip_rx_agg_free:
3405 	map = rxr->rx_tpa_idx_map;
3406 	if (map)
3407 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3408 }
3409 
3410 static void bnxt_free_rx_skbs(struct bnxt *bp)
3411 {
3412 	int i;
3413 
3414 	if (!bp->rx_ring)
3415 		return;
3416 
3417 	for (i = 0; i < bp->rx_nr_rings; i++)
3418 		bnxt_free_one_rx_ring_skbs(bp, i);
3419 }
3420 
3421 static void bnxt_free_skbs(struct bnxt *bp)
3422 {
3423 	bnxt_free_tx_skbs(bp);
3424 	bnxt_free_rx_skbs(bp);
3425 }
3426 
3427 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3428 {
3429 	u8 init_val = ctxm->init_value;
3430 	u16 offset = ctxm->init_offset;
3431 	u8 *p2 = p;
3432 	int i;
3433 
3434 	if (!init_val)
3435 		return;
3436 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3437 		memset(p, init_val, len);
3438 		return;
3439 	}
3440 	for (i = 0; i < len; i += ctxm->entry_size)
3441 		*(p2 + i + offset) = init_val;
3442 }
3443 
3444 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3445 {
3446 	struct pci_dev *pdev = bp->pdev;
3447 	int i;
3448 
3449 	if (!rmem->pg_arr)
3450 		goto skip_pages;
3451 
3452 	for (i = 0; i < rmem->nr_pages; i++) {
3453 		if (!rmem->pg_arr[i])
3454 			continue;
3455 
3456 		dma_free_coherent(&pdev->dev, rmem->page_size,
3457 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3458 
3459 		rmem->pg_arr[i] = NULL;
3460 	}
3461 skip_pages:
3462 	if (rmem->pg_tbl) {
3463 		size_t pg_tbl_size = rmem->nr_pages * 8;
3464 
3465 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3466 			pg_tbl_size = rmem->page_size;
3467 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3468 				  rmem->pg_tbl, rmem->pg_tbl_map);
3469 		rmem->pg_tbl = NULL;
3470 	}
3471 	if (rmem->vmem_size && *rmem->vmem) {
3472 		vfree(*rmem->vmem);
3473 		*rmem->vmem = NULL;
3474 	}
3475 }
3476 
3477 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3478 {
3479 	struct pci_dev *pdev = bp->pdev;
3480 	u64 valid_bit = 0;
3481 	int i;
3482 
3483 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3484 		valid_bit = PTU_PTE_VALID;
3485 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3486 		size_t pg_tbl_size = rmem->nr_pages * 8;
3487 
3488 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3489 			pg_tbl_size = rmem->page_size;
3490 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3491 						  &rmem->pg_tbl_map,
3492 						  GFP_KERNEL);
3493 		if (!rmem->pg_tbl)
3494 			return -ENOMEM;
3495 	}
3496 
3497 	for (i = 0; i < rmem->nr_pages; i++) {
3498 		u64 extra_bits = valid_bit;
3499 
3500 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3501 						     rmem->page_size,
3502 						     &rmem->dma_arr[i],
3503 						     GFP_KERNEL);
3504 		if (!rmem->pg_arr[i])
3505 			return -ENOMEM;
3506 
3507 		if (rmem->ctx_mem)
3508 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3509 					  rmem->page_size);
3510 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3511 			if (i == rmem->nr_pages - 2 &&
3512 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3513 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3514 			else if (i == rmem->nr_pages - 1 &&
3515 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3516 				extra_bits |= PTU_PTE_LAST;
3517 			rmem->pg_tbl[i] =
3518 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3519 		}
3520 	}
3521 
3522 	if (rmem->vmem_size) {
3523 		*rmem->vmem = vzalloc(rmem->vmem_size);
3524 		if (!(*rmem->vmem))
3525 			return -ENOMEM;
3526 	}
3527 	return 0;
3528 }
3529 
3530 static void bnxt_free_tpa_info(struct bnxt *bp)
3531 {
3532 	int i, j;
3533 
3534 	for (i = 0; i < bp->rx_nr_rings; i++) {
3535 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3536 
3537 		kfree(rxr->rx_tpa_idx_map);
3538 		rxr->rx_tpa_idx_map = NULL;
3539 		if (rxr->rx_tpa) {
3540 			for (j = 0; j < bp->max_tpa; j++) {
3541 				kfree(rxr->rx_tpa[j].agg_arr);
3542 				rxr->rx_tpa[j].agg_arr = NULL;
3543 			}
3544 		}
3545 		kfree(rxr->rx_tpa);
3546 		rxr->rx_tpa = NULL;
3547 	}
3548 }
3549 
3550 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3551 {
3552 	int i, j;
3553 
3554 	bp->max_tpa = MAX_TPA;
3555 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3556 		if (!bp->max_tpa_v2)
3557 			return 0;
3558 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3559 	}
3560 
3561 	for (i = 0; i < bp->rx_nr_rings; i++) {
3562 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3563 		struct rx_agg_cmp *agg;
3564 
3565 		rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3566 				      GFP_KERNEL);
3567 		if (!rxr->rx_tpa)
3568 			return -ENOMEM;
3569 
3570 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3571 			continue;
3572 		for (j = 0; j < bp->max_tpa; j++) {
3573 			agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3574 			if (!agg)
3575 				return -ENOMEM;
3576 			rxr->rx_tpa[j].agg_arr = agg;
3577 		}
3578 		rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3579 					      GFP_KERNEL);
3580 		if (!rxr->rx_tpa_idx_map)
3581 			return -ENOMEM;
3582 	}
3583 	return 0;
3584 }
3585 
3586 static void bnxt_free_rx_rings(struct bnxt *bp)
3587 {
3588 	int i;
3589 
3590 	if (!bp->rx_ring)
3591 		return;
3592 
3593 	bnxt_free_tpa_info(bp);
3594 	for (i = 0; i < bp->rx_nr_rings; i++) {
3595 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3596 		struct bnxt_ring_struct *ring;
3597 
3598 		if (rxr->xdp_prog)
3599 			bpf_prog_put(rxr->xdp_prog);
3600 
3601 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3602 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3603 
3604 		page_pool_destroy(rxr->page_pool);
3605 		rxr->page_pool = NULL;
3606 
3607 		kfree(rxr->rx_agg_bmap);
3608 		rxr->rx_agg_bmap = NULL;
3609 
3610 		ring = &rxr->rx_ring_struct;
3611 		bnxt_free_ring(bp, &ring->ring_mem);
3612 
3613 		ring = &rxr->rx_agg_ring_struct;
3614 		bnxt_free_ring(bp, &ring->ring_mem);
3615 	}
3616 }
3617 
3618 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3619 				   struct bnxt_rx_ring_info *rxr,
3620 				   int numa_node)
3621 {
3622 	struct page_pool_params pp = { 0 };
3623 
3624 	pp.pool_size = bp->rx_agg_ring_size;
3625 	if (BNXT_RX_PAGE_MODE(bp))
3626 		pp.pool_size += bp->rx_ring_size;
3627 	pp.nid = numa_node;
3628 	pp.napi = &rxr->bnapi->napi;
3629 	pp.netdev = bp->dev;
3630 	pp.dev = &bp->pdev->dev;
3631 	pp.dma_dir = bp->rx_dir;
3632 	pp.max_len = PAGE_SIZE;
3633 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3634 
3635 	rxr->page_pool = page_pool_create(&pp);
3636 	if (IS_ERR(rxr->page_pool)) {
3637 		int err = PTR_ERR(rxr->page_pool);
3638 
3639 		rxr->page_pool = NULL;
3640 		return err;
3641 	}
3642 	return 0;
3643 }
3644 
3645 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3646 {
3647 	int numa_node = dev_to_node(&bp->pdev->dev);
3648 	int i, rc = 0, agg_rings = 0, cpu;
3649 
3650 	if (!bp->rx_ring)
3651 		return -ENOMEM;
3652 
3653 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3654 		agg_rings = 1;
3655 
3656 	for (i = 0; i < bp->rx_nr_rings; i++) {
3657 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3658 		struct bnxt_ring_struct *ring;
3659 		int cpu_node;
3660 
3661 		ring = &rxr->rx_ring_struct;
3662 
3663 		cpu = cpumask_local_spread(i, numa_node);
3664 		cpu_node = cpu_to_node(cpu);
3665 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3666 			   i, cpu_node);
3667 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3668 		if (rc)
3669 			return rc;
3670 
3671 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3672 		if (rc < 0)
3673 			return rc;
3674 
3675 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3676 						MEM_TYPE_PAGE_POOL,
3677 						rxr->page_pool);
3678 		if (rc) {
3679 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3680 			return rc;
3681 		}
3682 
3683 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3684 		if (rc)
3685 			return rc;
3686 
3687 		ring->grp_idx = i;
3688 		if (agg_rings) {
3689 			u16 mem_size;
3690 
3691 			ring = &rxr->rx_agg_ring_struct;
3692 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3693 			if (rc)
3694 				return rc;
3695 
3696 			ring->grp_idx = i;
3697 			rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3698 			mem_size = rxr->rx_agg_bmap_size / 8;
3699 			rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3700 			if (!rxr->rx_agg_bmap)
3701 				return -ENOMEM;
3702 		}
3703 	}
3704 	if (bp->flags & BNXT_FLAG_TPA)
3705 		rc = bnxt_alloc_tpa_info(bp);
3706 	return rc;
3707 }
3708 
3709 static void bnxt_free_tx_rings(struct bnxt *bp)
3710 {
3711 	int i;
3712 	struct pci_dev *pdev = bp->pdev;
3713 
3714 	if (!bp->tx_ring)
3715 		return;
3716 
3717 	for (i = 0; i < bp->tx_nr_rings; i++) {
3718 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3719 		struct bnxt_ring_struct *ring;
3720 
3721 		if (txr->tx_push) {
3722 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3723 					  txr->tx_push, txr->tx_push_mapping);
3724 			txr->tx_push = NULL;
3725 		}
3726 
3727 		ring = &txr->tx_ring_struct;
3728 
3729 		bnxt_free_ring(bp, &ring->ring_mem);
3730 	}
3731 }
3732 
3733 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3734 	((tc) * (bp)->tx_nr_rings_per_tc)
3735 
3736 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3737 	((tx) % (bp)->tx_nr_rings_per_tc)
3738 
3739 #define BNXT_RING_TO_TC(bp, tx)		\
3740 	((tx) / (bp)->tx_nr_rings_per_tc)
3741 
3742 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3743 {
3744 	int i, j, rc;
3745 	struct pci_dev *pdev = bp->pdev;
3746 
3747 	bp->tx_push_size = 0;
3748 	if (bp->tx_push_thresh) {
3749 		int push_size;
3750 
3751 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3752 					bp->tx_push_thresh);
3753 
3754 		if (push_size > 256) {
3755 			push_size = 0;
3756 			bp->tx_push_thresh = 0;
3757 		}
3758 
3759 		bp->tx_push_size = push_size;
3760 	}
3761 
3762 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3763 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3764 		struct bnxt_ring_struct *ring;
3765 		u8 qidx;
3766 
3767 		ring = &txr->tx_ring_struct;
3768 
3769 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3770 		if (rc)
3771 			return rc;
3772 
3773 		ring->grp_idx = txr->bnapi->index;
3774 		if (bp->tx_push_size) {
3775 			dma_addr_t mapping;
3776 
3777 			/* One pre-allocated DMA buffer to backup
3778 			 * TX push operation
3779 			 */
3780 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3781 						bp->tx_push_size,
3782 						&txr->tx_push_mapping,
3783 						GFP_KERNEL);
3784 
3785 			if (!txr->tx_push)
3786 				return -ENOMEM;
3787 
3788 			mapping = txr->tx_push_mapping +
3789 				sizeof(struct tx_push_bd);
3790 			txr->data_mapping = cpu_to_le64(mapping);
3791 		}
3792 		qidx = bp->tc_to_qidx[j];
3793 		ring->queue_id = bp->q_info[qidx].queue_id;
3794 		spin_lock_init(&txr->xdp_tx_lock);
3795 		if (i < bp->tx_nr_rings_xdp)
3796 			continue;
3797 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
3798 			j++;
3799 	}
3800 	return 0;
3801 }
3802 
3803 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
3804 {
3805 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3806 
3807 	kfree(cpr->cp_desc_ring);
3808 	cpr->cp_desc_ring = NULL;
3809 	ring->ring_mem.pg_arr = NULL;
3810 	kfree(cpr->cp_desc_mapping);
3811 	cpr->cp_desc_mapping = NULL;
3812 	ring->ring_mem.dma_arr = NULL;
3813 }
3814 
3815 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
3816 {
3817 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
3818 	if (!cpr->cp_desc_ring)
3819 		return -ENOMEM;
3820 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
3821 				       GFP_KERNEL);
3822 	if (!cpr->cp_desc_mapping)
3823 		return -ENOMEM;
3824 	return 0;
3825 }
3826 
3827 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
3828 {
3829 	int i;
3830 
3831 	if (!bp->bnapi)
3832 		return;
3833 	for (i = 0; i < bp->cp_nr_rings; i++) {
3834 		struct bnxt_napi *bnapi = bp->bnapi[i];
3835 
3836 		if (!bnapi)
3837 			continue;
3838 		bnxt_free_cp_arrays(&bnapi->cp_ring);
3839 	}
3840 }
3841 
3842 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
3843 {
3844 	int i, n = bp->cp_nr_pages;
3845 
3846 	for (i = 0; i < bp->cp_nr_rings; i++) {
3847 		struct bnxt_napi *bnapi = bp->bnapi[i];
3848 		int rc;
3849 
3850 		if (!bnapi)
3851 			continue;
3852 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
3853 		if (rc)
3854 			return rc;
3855 	}
3856 	return 0;
3857 }
3858 
3859 static void bnxt_free_cp_rings(struct bnxt *bp)
3860 {
3861 	int i;
3862 
3863 	if (!bp->bnapi)
3864 		return;
3865 
3866 	for (i = 0; i < bp->cp_nr_rings; i++) {
3867 		struct bnxt_napi *bnapi = bp->bnapi[i];
3868 		struct bnxt_cp_ring_info *cpr;
3869 		struct bnxt_ring_struct *ring;
3870 		int j;
3871 
3872 		if (!bnapi)
3873 			continue;
3874 
3875 		cpr = &bnapi->cp_ring;
3876 		ring = &cpr->cp_ring_struct;
3877 
3878 		bnxt_free_ring(bp, &ring->ring_mem);
3879 
3880 		if (!cpr->cp_ring_arr)
3881 			continue;
3882 
3883 		for (j = 0; j < cpr->cp_ring_count; j++) {
3884 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
3885 
3886 			ring = &cpr2->cp_ring_struct;
3887 			bnxt_free_ring(bp, &ring->ring_mem);
3888 			bnxt_free_cp_arrays(cpr2);
3889 		}
3890 		kfree(cpr->cp_ring_arr);
3891 		cpr->cp_ring_arr = NULL;
3892 		cpr->cp_ring_count = 0;
3893 	}
3894 }
3895 
3896 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
3897 				  struct bnxt_cp_ring_info *cpr)
3898 {
3899 	struct bnxt_ring_mem_info *rmem;
3900 	struct bnxt_ring_struct *ring;
3901 	int rc;
3902 
3903 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
3904 	if (rc) {
3905 		bnxt_free_cp_arrays(cpr);
3906 		return -ENOMEM;
3907 	}
3908 	ring = &cpr->cp_ring_struct;
3909 	rmem = &ring->ring_mem;
3910 	rmem->nr_pages = bp->cp_nr_pages;
3911 	rmem->page_size = HW_CMPD_RING_SIZE;
3912 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
3913 	rmem->dma_arr = cpr->cp_desc_mapping;
3914 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
3915 	rc = bnxt_alloc_ring(bp, rmem);
3916 	if (rc) {
3917 		bnxt_free_ring(bp, rmem);
3918 		bnxt_free_cp_arrays(cpr);
3919 	}
3920 	return rc;
3921 }
3922 
3923 static int bnxt_alloc_cp_rings(struct bnxt *bp)
3924 {
3925 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
3926 	int i, j, rc, ulp_msix;
3927 	int tcs = bp->num_tc;
3928 
3929 	if (!tcs)
3930 		tcs = 1;
3931 	ulp_msix = bnxt_get_ulp_msix_num(bp);
3932 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
3933 		struct bnxt_napi *bnapi = bp->bnapi[i];
3934 		struct bnxt_cp_ring_info *cpr, *cpr2;
3935 		struct bnxt_ring_struct *ring;
3936 		int cp_count = 0, k;
3937 		int rx = 0, tx = 0;
3938 
3939 		if (!bnapi)
3940 			continue;
3941 
3942 		cpr = &bnapi->cp_ring;
3943 		cpr->bnapi = bnapi;
3944 		ring = &cpr->cp_ring_struct;
3945 
3946 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3947 		if (rc)
3948 			return rc;
3949 
3950 		ring->map_idx = ulp_msix + i;
3951 
3952 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3953 			continue;
3954 
3955 		if (i < bp->rx_nr_rings) {
3956 			cp_count++;
3957 			rx = 1;
3958 		}
3959 		if (i < bp->tx_nr_rings_xdp) {
3960 			cp_count++;
3961 			tx = 1;
3962 		} else if ((sh && i < bp->tx_nr_rings) ||
3963 			 (!sh && i >= bp->rx_nr_rings)) {
3964 			cp_count += tcs;
3965 			tx = 1;
3966 		}
3967 
3968 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
3969 					   GFP_KERNEL);
3970 		if (!cpr->cp_ring_arr)
3971 			return -ENOMEM;
3972 		cpr->cp_ring_count = cp_count;
3973 
3974 		for (k = 0; k < cp_count; k++) {
3975 			cpr2 = &cpr->cp_ring_arr[k];
3976 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
3977 			if (rc)
3978 				return rc;
3979 			cpr2->bnapi = bnapi;
3980 			cpr2->sw_stats = cpr->sw_stats;
3981 			cpr2->cp_idx = k;
3982 			if (!k && rx) {
3983 				bp->rx_ring[i].rx_cpr = cpr2;
3984 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
3985 			} else {
3986 				int n, tc = k - rx;
3987 
3988 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
3989 				bp->tx_ring[n].tx_cpr = cpr2;
3990 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
3991 			}
3992 		}
3993 		if (tx)
3994 			j++;
3995 	}
3996 	return 0;
3997 }
3998 
3999 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4000 				     struct bnxt_rx_ring_info *rxr)
4001 {
4002 	struct bnxt_ring_mem_info *rmem;
4003 	struct bnxt_ring_struct *ring;
4004 
4005 	ring = &rxr->rx_ring_struct;
4006 	rmem = &ring->ring_mem;
4007 	rmem->nr_pages = bp->rx_nr_pages;
4008 	rmem->page_size = HW_RXBD_RING_SIZE;
4009 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4010 	rmem->dma_arr = rxr->rx_desc_mapping;
4011 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4012 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4013 
4014 	ring = &rxr->rx_agg_ring_struct;
4015 	rmem = &ring->ring_mem;
4016 	rmem->nr_pages = bp->rx_agg_nr_pages;
4017 	rmem->page_size = HW_RXBD_RING_SIZE;
4018 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4019 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4020 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4021 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4022 }
4023 
4024 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4025 				      struct bnxt_rx_ring_info *rxr)
4026 {
4027 	struct bnxt_ring_mem_info *rmem;
4028 	struct bnxt_ring_struct *ring;
4029 	int i;
4030 
4031 	rxr->page_pool->p.napi = NULL;
4032 	rxr->page_pool = NULL;
4033 
4034 	ring = &rxr->rx_ring_struct;
4035 	rmem = &ring->ring_mem;
4036 	rmem->pg_tbl = NULL;
4037 	rmem->pg_tbl_map = 0;
4038 	for (i = 0; i < rmem->nr_pages; i++) {
4039 		rmem->pg_arr[i] = NULL;
4040 		rmem->dma_arr[i] = 0;
4041 	}
4042 	*rmem->vmem = NULL;
4043 
4044 	ring = &rxr->rx_agg_ring_struct;
4045 	rmem = &ring->ring_mem;
4046 	rmem->pg_tbl = NULL;
4047 	rmem->pg_tbl_map = 0;
4048 	for (i = 0; i < rmem->nr_pages; i++) {
4049 		rmem->pg_arr[i] = NULL;
4050 		rmem->dma_arr[i] = 0;
4051 	}
4052 	*rmem->vmem = NULL;
4053 }
4054 
4055 static void bnxt_init_ring_struct(struct bnxt *bp)
4056 {
4057 	int i, j;
4058 
4059 	for (i = 0; i < bp->cp_nr_rings; i++) {
4060 		struct bnxt_napi *bnapi = bp->bnapi[i];
4061 		struct bnxt_ring_mem_info *rmem;
4062 		struct bnxt_cp_ring_info *cpr;
4063 		struct bnxt_rx_ring_info *rxr;
4064 		struct bnxt_tx_ring_info *txr;
4065 		struct bnxt_ring_struct *ring;
4066 
4067 		if (!bnapi)
4068 			continue;
4069 
4070 		cpr = &bnapi->cp_ring;
4071 		ring = &cpr->cp_ring_struct;
4072 		rmem = &ring->ring_mem;
4073 		rmem->nr_pages = bp->cp_nr_pages;
4074 		rmem->page_size = HW_CMPD_RING_SIZE;
4075 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4076 		rmem->dma_arr = cpr->cp_desc_mapping;
4077 		rmem->vmem_size = 0;
4078 
4079 		rxr = bnapi->rx_ring;
4080 		if (!rxr)
4081 			goto skip_rx;
4082 
4083 		ring = &rxr->rx_ring_struct;
4084 		rmem = &ring->ring_mem;
4085 		rmem->nr_pages = bp->rx_nr_pages;
4086 		rmem->page_size = HW_RXBD_RING_SIZE;
4087 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4088 		rmem->dma_arr = rxr->rx_desc_mapping;
4089 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4090 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4091 
4092 		ring = &rxr->rx_agg_ring_struct;
4093 		rmem = &ring->ring_mem;
4094 		rmem->nr_pages = bp->rx_agg_nr_pages;
4095 		rmem->page_size = HW_RXBD_RING_SIZE;
4096 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4097 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4098 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4099 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4100 
4101 skip_rx:
4102 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4103 			ring = &txr->tx_ring_struct;
4104 			rmem = &ring->ring_mem;
4105 			rmem->nr_pages = bp->tx_nr_pages;
4106 			rmem->page_size = HW_TXBD_RING_SIZE;
4107 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4108 			rmem->dma_arr = txr->tx_desc_mapping;
4109 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4110 			rmem->vmem = (void **)&txr->tx_buf_ring;
4111 		}
4112 	}
4113 }
4114 
4115 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4116 {
4117 	int i;
4118 	u32 prod;
4119 	struct rx_bd **rx_buf_ring;
4120 
4121 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4122 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4123 		int j;
4124 		struct rx_bd *rxbd;
4125 
4126 		rxbd = rx_buf_ring[i];
4127 		if (!rxbd)
4128 			continue;
4129 
4130 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4131 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4132 			rxbd->rx_bd_opaque = prod;
4133 		}
4134 	}
4135 }
4136 
4137 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4138 				       struct bnxt_rx_ring_info *rxr,
4139 				       int ring_nr)
4140 {
4141 	u32 prod;
4142 	int i;
4143 
4144 	prod = rxr->rx_prod;
4145 	for (i = 0; i < bp->rx_ring_size; i++) {
4146 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4147 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4148 				    ring_nr, i, bp->rx_ring_size);
4149 			break;
4150 		}
4151 		prod = NEXT_RX(prod);
4152 	}
4153 	rxr->rx_prod = prod;
4154 }
4155 
4156 static void bnxt_alloc_one_rx_ring_page(struct bnxt *bp,
4157 					struct bnxt_rx_ring_info *rxr,
4158 					int ring_nr)
4159 {
4160 	u32 prod;
4161 	int i;
4162 
4163 	prod = rxr->rx_agg_prod;
4164 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4165 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
4166 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4167 				    ring_nr, i, bp->rx_ring_size);
4168 			break;
4169 		}
4170 		prod = NEXT_RX_AGG(prod);
4171 	}
4172 	rxr->rx_agg_prod = prod;
4173 }
4174 
4175 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4176 {
4177 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4178 	int i;
4179 
4180 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4181 
4182 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4183 		return 0;
4184 
4185 	bnxt_alloc_one_rx_ring_page(bp, rxr, ring_nr);
4186 
4187 	if (rxr->rx_tpa) {
4188 		dma_addr_t mapping;
4189 		u8 *data;
4190 
4191 		for (i = 0; i < bp->max_tpa; i++) {
4192 			data = __bnxt_alloc_rx_frag(bp, &mapping, GFP_KERNEL);
4193 			if (!data)
4194 				return -ENOMEM;
4195 
4196 			rxr->rx_tpa[i].data = data;
4197 			rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4198 			rxr->rx_tpa[i].mapping = mapping;
4199 		}
4200 	}
4201 	return 0;
4202 }
4203 
4204 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4205 				       struct bnxt_rx_ring_info *rxr)
4206 {
4207 	struct bnxt_ring_struct *ring;
4208 	u32 type;
4209 
4210 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4211 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4212 
4213 	if (NET_IP_ALIGN == 2)
4214 		type |= RX_BD_FLAGS_SOP;
4215 
4216 	ring = &rxr->rx_ring_struct;
4217 	bnxt_init_rxbd_pages(ring, type);
4218 	ring->fw_ring_id = INVALID_HW_RING_ID;
4219 }
4220 
4221 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4222 					   struct bnxt_rx_ring_info *rxr)
4223 {
4224 	struct bnxt_ring_struct *ring;
4225 	u32 type;
4226 
4227 	ring = &rxr->rx_agg_ring_struct;
4228 	ring->fw_ring_id = INVALID_HW_RING_ID;
4229 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4230 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4231 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4232 
4233 		bnxt_init_rxbd_pages(ring, type);
4234 	}
4235 }
4236 
4237 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4238 {
4239 	struct bnxt_rx_ring_info *rxr;
4240 
4241 	rxr = &bp->rx_ring[ring_nr];
4242 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4243 
4244 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4245 			     &rxr->bnapi->napi);
4246 
4247 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4248 		bpf_prog_add(bp->xdp_prog, 1);
4249 		rxr->xdp_prog = bp->xdp_prog;
4250 	}
4251 
4252 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4253 
4254 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4255 }
4256 
4257 static void bnxt_init_cp_rings(struct bnxt *bp)
4258 {
4259 	int i, j;
4260 
4261 	for (i = 0; i < bp->cp_nr_rings; i++) {
4262 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4263 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4264 
4265 		ring->fw_ring_id = INVALID_HW_RING_ID;
4266 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4267 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4268 		if (!cpr->cp_ring_arr)
4269 			continue;
4270 		for (j = 0; j < cpr->cp_ring_count; j++) {
4271 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4272 
4273 			ring = &cpr2->cp_ring_struct;
4274 			ring->fw_ring_id = INVALID_HW_RING_ID;
4275 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4276 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4277 		}
4278 	}
4279 }
4280 
4281 static int bnxt_init_rx_rings(struct bnxt *bp)
4282 {
4283 	int i, rc = 0;
4284 
4285 	if (BNXT_RX_PAGE_MODE(bp)) {
4286 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4287 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4288 	} else {
4289 		bp->rx_offset = BNXT_RX_OFFSET;
4290 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4291 	}
4292 
4293 	for (i = 0; i < bp->rx_nr_rings; i++) {
4294 		rc = bnxt_init_one_rx_ring(bp, i);
4295 		if (rc)
4296 			break;
4297 	}
4298 
4299 	return rc;
4300 }
4301 
4302 static int bnxt_init_tx_rings(struct bnxt *bp)
4303 {
4304 	u16 i;
4305 
4306 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4307 				   BNXT_MIN_TX_DESC_CNT);
4308 
4309 	for (i = 0; i < bp->tx_nr_rings; i++) {
4310 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4311 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4312 
4313 		ring->fw_ring_id = INVALID_HW_RING_ID;
4314 
4315 		if (i >= bp->tx_nr_rings_xdp)
4316 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4317 					     NETDEV_QUEUE_TYPE_TX,
4318 					     &txr->bnapi->napi);
4319 	}
4320 
4321 	return 0;
4322 }
4323 
4324 static void bnxt_free_ring_grps(struct bnxt *bp)
4325 {
4326 	kfree(bp->grp_info);
4327 	bp->grp_info = NULL;
4328 }
4329 
4330 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4331 {
4332 	int i;
4333 
4334 	if (irq_re_init) {
4335 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4336 				       sizeof(struct bnxt_ring_grp_info),
4337 				       GFP_KERNEL);
4338 		if (!bp->grp_info)
4339 			return -ENOMEM;
4340 	}
4341 	for (i = 0; i < bp->cp_nr_rings; i++) {
4342 		if (irq_re_init)
4343 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4344 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4345 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4346 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4347 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4348 	}
4349 	return 0;
4350 }
4351 
4352 static void bnxt_free_vnics(struct bnxt *bp)
4353 {
4354 	kfree(bp->vnic_info);
4355 	bp->vnic_info = NULL;
4356 	bp->nr_vnics = 0;
4357 }
4358 
4359 static int bnxt_alloc_vnics(struct bnxt *bp)
4360 {
4361 	int num_vnics = 1;
4362 
4363 #ifdef CONFIG_RFS_ACCEL
4364 	if (bp->flags & BNXT_FLAG_RFS) {
4365 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4366 			num_vnics++;
4367 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4368 			num_vnics += bp->rx_nr_rings;
4369 	}
4370 #endif
4371 
4372 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4373 		num_vnics++;
4374 
4375 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4376 				GFP_KERNEL);
4377 	if (!bp->vnic_info)
4378 		return -ENOMEM;
4379 
4380 	bp->nr_vnics = num_vnics;
4381 	return 0;
4382 }
4383 
4384 static void bnxt_init_vnics(struct bnxt *bp)
4385 {
4386 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4387 	int i;
4388 
4389 	for (i = 0; i < bp->nr_vnics; i++) {
4390 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4391 		int j;
4392 
4393 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4394 		vnic->vnic_id = i;
4395 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4396 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4397 
4398 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4399 
4400 		if (bp->vnic_info[i].rss_hash_key) {
4401 			if (i == BNXT_VNIC_DEFAULT) {
4402 				u8 *key = (void *)vnic->rss_hash_key;
4403 				int k;
4404 
4405 				if (!bp->rss_hash_key_valid &&
4406 				    !bp->rss_hash_key_updated) {
4407 					get_random_bytes(bp->rss_hash_key,
4408 							 HW_HASH_KEY_SIZE);
4409 					bp->rss_hash_key_updated = true;
4410 				}
4411 
4412 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4413 				       HW_HASH_KEY_SIZE);
4414 
4415 				if (!bp->rss_hash_key_updated)
4416 					continue;
4417 
4418 				bp->rss_hash_key_updated = false;
4419 				bp->rss_hash_key_valid = true;
4420 
4421 				bp->toeplitz_prefix = 0;
4422 				for (k = 0; k < 8; k++) {
4423 					bp->toeplitz_prefix <<= 8;
4424 					bp->toeplitz_prefix |= key[k];
4425 				}
4426 			} else {
4427 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4428 				       HW_HASH_KEY_SIZE);
4429 			}
4430 		}
4431 	}
4432 }
4433 
4434 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4435 {
4436 	int pages;
4437 
4438 	pages = ring_size / desc_per_pg;
4439 
4440 	if (!pages)
4441 		return 1;
4442 
4443 	pages++;
4444 
4445 	while (pages & (pages - 1))
4446 		pages++;
4447 
4448 	return pages;
4449 }
4450 
4451 void bnxt_set_tpa_flags(struct bnxt *bp)
4452 {
4453 	bp->flags &= ~BNXT_FLAG_TPA;
4454 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4455 		return;
4456 	if (bp->dev->features & NETIF_F_LRO)
4457 		bp->flags |= BNXT_FLAG_LRO;
4458 	else if (bp->dev->features & NETIF_F_GRO_HW)
4459 		bp->flags |= BNXT_FLAG_GRO;
4460 }
4461 
4462 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4463  * be set on entry.
4464  */
4465 void bnxt_set_ring_params(struct bnxt *bp)
4466 {
4467 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4468 	u32 agg_factor = 0, agg_ring_size = 0;
4469 
4470 	/* 8 for CRC and VLAN */
4471 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4472 
4473 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4474 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4475 
4476 	bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
4477 	ring_size = bp->rx_ring_size;
4478 	bp->rx_agg_ring_size = 0;
4479 	bp->rx_agg_nr_pages = 0;
4480 
4481 	if (bp->flags & BNXT_FLAG_TPA)
4482 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4483 
4484 	bp->flags &= ~BNXT_FLAG_JUMBO;
4485 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4486 		u32 jumbo_factor;
4487 
4488 		bp->flags |= BNXT_FLAG_JUMBO;
4489 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4490 		if (jumbo_factor > agg_factor)
4491 			agg_factor = jumbo_factor;
4492 	}
4493 	if (agg_factor) {
4494 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4495 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4496 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4497 				    bp->rx_ring_size, ring_size);
4498 			bp->rx_ring_size = ring_size;
4499 		}
4500 		agg_ring_size = ring_size * agg_factor;
4501 
4502 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4503 							RX_DESC_CNT);
4504 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4505 			u32 tmp = agg_ring_size;
4506 
4507 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4508 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4509 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4510 				    tmp, agg_ring_size);
4511 		}
4512 		bp->rx_agg_ring_size = agg_ring_size;
4513 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4514 
4515 		if (BNXT_RX_PAGE_MODE(bp)) {
4516 			rx_space = PAGE_SIZE;
4517 			rx_size = PAGE_SIZE -
4518 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4519 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4520 		} else {
4521 			rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
4522 			rx_space = rx_size + NET_SKB_PAD +
4523 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4524 		}
4525 	}
4526 
4527 	bp->rx_buf_use_size = rx_size;
4528 	bp->rx_buf_size = rx_space;
4529 
4530 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4531 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4532 
4533 	ring_size = bp->tx_ring_size;
4534 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4535 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4536 
4537 	max_rx_cmpl = bp->rx_ring_size;
4538 	/* MAX TPA needs to be added because TPA_START completions are
4539 	 * immediately recycled, so the TPA completions are not bound by
4540 	 * the RX ring size.
4541 	 */
4542 	if (bp->flags & BNXT_FLAG_TPA)
4543 		max_rx_cmpl += bp->max_tpa;
4544 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4545 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4546 	bp->cp_ring_size = ring_size;
4547 
4548 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4549 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4550 		bp->cp_nr_pages = MAX_CP_PAGES;
4551 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4552 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4553 			    ring_size, bp->cp_ring_size);
4554 	}
4555 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4556 	bp->cp_ring_mask = bp->cp_bit - 1;
4557 }
4558 
4559 /* Changing allocation mode of RX rings.
4560  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4561  */
4562 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4563 {
4564 	struct net_device *dev = bp->dev;
4565 
4566 	if (page_mode) {
4567 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
4568 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4569 
4570 		if (bp->xdp_prog->aux->xdp_has_frags)
4571 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4572 		else
4573 			dev->max_mtu =
4574 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4575 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4576 			bp->flags |= BNXT_FLAG_JUMBO;
4577 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4578 		} else {
4579 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4580 			bp->rx_skb_func = bnxt_rx_page_skb;
4581 		}
4582 		bp->rx_dir = DMA_BIDIRECTIONAL;
4583 		/* Disable LRO or GRO_HW */
4584 		netdev_update_features(dev);
4585 	} else {
4586 		dev->max_mtu = bp->max_mtu;
4587 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4588 		bp->rx_dir = DMA_FROM_DEVICE;
4589 		bp->rx_skb_func = bnxt_rx_skb;
4590 	}
4591 	return 0;
4592 }
4593 
4594 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4595 {
4596 	int i;
4597 	struct bnxt_vnic_info *vnic;
4598 	struct pci_dev *pdev = bp->pdev;
4599 
4600 	if (!bp->vnic_info)
4601 		return;
4602 
4603 	for (i = 0; i < bp->nr_vnics; i++) {
4604 		vnic = &bp->vnic_info[i];
4605 
4606 		kfree(vnic->fw_grp_ids);
4607 		vnic->fw_grp_ids = NULL;
4608 
4609 		kfree(vnic->uc_list);
4610 		vnic->uc_list = NULL;
4611 
4612 		if (vnic->mc_list) {
4613 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4614 					  vnic->mc_list, vnic->mc_list_mapping);
4615 			vnic->mc_list = NULL;
4616 		}
4617 
4618 		if (vnic->rss_table) {
4619 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4620 					  vnic->rss_table,
4621 					  vnic->rss_table_dma_addr);
4622 			vnic->rss_table = NULL;
4623 		}
4624 
4625 		vnic->rss_hash_key = NULL;
4626 		vnic->flags = 0;
4627 	}
4628 }
4629 
4630 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4631 {
4632 	int i, rc = 0, size;
4633 	struct bnxt_vnic_info *vnic;
4634 	struct pci_dev *pdev = bp->pdev;
4635 	int max_rings;
4636 
4637 	for (i = 0; i < bp->nr_vnics; i++) {
4638 		vnic = &bp->vnic_info[i];
4639 
4640 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4641 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4642 
4643 			if (mem_size > 0) {
4644 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4645 				if (!vnic->uc_list) {
4646 					rc = -ENOMEM;
4647 					goto out;
4648 				}
4649 			}
4650 		}
4651 
4652 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4653 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4654 			vnic->mc_list =
4655 				dma_alloc_coherent(&pdev->dev,
4656 						   vnic->mc_list_size,
4657 						   &vnic->mc_list_mapping,
4658 						   GFP_KERNEL);
4659 			if (!vnic->mc_list) {
4660 				rc = -ENOMEM;
4661 				goto out;
4662 			}
4663 		}
4664 
4665 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4666 			goto vnic_skip_grps;
4667 
4668 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4669 			max_rings = bp->rx_nr_rings;
4670 		else
4671 			max_rings = 1;
4672 
4673 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4674 		if (!vnic->fw_grp_ids) {
4675 			rc = -ENOMEM;
4676 			goto out;
4677 		}
4678 vnic_skip_grps:
4679 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4680 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4681 			continue;
4682 
4683 		/* Allocate rss table and hash key */
4684 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4685 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4686 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4687 
4688 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4689 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4690 						     vnic->rss_table_size,
4691 						     &vnic->rss_table_dma_addr,
4692 						     GFP_KERNEL);
4693 		if (!vnic->rss_table) {
4694 			rc = -ENOMEM;
4695 			goto out;
4696 		}
4697 
4698 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4699 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4700 	}
4701 	return 0;
4702 
4703 out:
4704 	return rc;
4705 }
4706 
4707 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4708 {
4709 	struct bnxt_hwrm_wait_token *token;
4710 
4711 	dma_pool_destroy(bp->hwrm_dma_pool);
4712 	bp->hwrm_dma_pool = NULL;
4713 
4714 	rcu_read_lock();
4715 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4716 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4717 	rcu_read_unlock();
4718 }
4719 
4720 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4721 {
4722 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4723 					    BNXT_HWRM_DMA_SIZE,
4724 					    BNXT_HWRM_DMA_ALIGN, 0);
4725 	if (!bp->hwrm_dma_pool)
4726 		return -ENOMEM;
4727 
4728 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4729 
4730 	return 0;
4731 }
4732 
4733 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4734 {
4735 	kfree(stats->hw_masks);
4736 	stats->hw_masks = NULL;
4737 	kfree(stats->sw_stats);
4738 	stats->sw_stats = NULL;
4739 	if (stats->hw_stats) {
4740 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
4741 				  stats->hw_stats_map);
4742 		stats->hw_stats = NULL;
4743 	}
4744 }
4745 
4746 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
4747 				bool alloc_masks)
4748 {
4749 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
4750 					     &stats->hw_stats_map, GFP_KERNEL);
4751 	if (!stats->hw_stats)
4752 		return -ENOMEM;
4753 
4754 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
4755 	if (!stats->sw_stats)
4756 		goto stats_mem_err;
4757 
4758 	if (alloc_masks) {
4759 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
4760 		if (!stats->hw_masks)
4761 			goto stats_mem_err;
4762 	}
4763 	return 0;
4764 
4765 stats_mem_err:
4766 	bnxt_free_stats_mem(bp, stats);
4767 	return -ENOMEM;
4768 }
4769 
4770 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
4771 {
4772 	int i;
4773 
4774 	for (i = 0; i < count; i++)
4775 		mask_arr[i] = mask;
4776 }
4777 
4778 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
4779 {
4780 	int i;
4781 
4782 	for (i = 0; i < count; i++)
4783 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
4784 }
4785 
4786 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
4787 				    struct bnxt_stats_mem *stats)
4788 {
4789 	struct hwrm_func_qstats_ext_output *resp;
4790 	struct hwrm_func_qstats_ext_input *req;
4791 	__le64 *hw_masks;
4792 	int rc;
4793 
4794 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
4795 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4796 		return -EOPNOTSUPP;
4797 
4798 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
4799 	if (rc)
4800 		return rc;
4801 
4802 	req->fid = cpu_to_le16(0xffff);
4803 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4804 
4805 	resp = hwrm_req_hold(bp, req);
4806 	rc = hwrm_req_send(bp, req);
4807 	if (!rc) {
4808 		hw_masks = &resp->rx_ucast_pkts;
4809 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
4810 	}
4811 	hwrm_req_drop(bp, req);
4812 	return rc;
4813 }
4814 
4815 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
4816 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
4817 
4818 static void bnxt_init_stats(struct bnxt *bp)
4819 {
4820 	struct bnxt_napi *bnapi = bp->bnapi[0];
4821 	struct bnxt_cp_ring_info *cpr;
4822 	struct bnxt_stats_mem *stats;
4823 	__le64 *rx_stats, *tx_stats;
4824 	int rc, rx_count, tx_count;
4825 	u64 *rx_masks, *tx_masks;
4826 	u64 mask;
4827 	u8 flags;
4828 
4829 	cpr = &bnapi->cp_ring;
4830 	stats = &cpr->stats;
4831 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
4832 	if (rc) {
4833 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4834 			mask = (1ULL << 48) - 1;
4835 		else
4836 			mask = -1ULL;
4837 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
4838 	}
4839 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
4840 		stats = &bp->port_stats;
4841 		rx_stats = stats->hw_stats;
4842 		rx_masks = stats->hw_masks;
4843 		rx_count = sizeof(struct rx_port_stats) / 8;
4844 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4845 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4846 		tx_count = sizeof(struct tx_port_stats) / 8;
4847 
4848 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
4849 		rc = bnxt_hwrm_port_qstats(bp, flags);
4850 		if (rc) {
4851 			mask = (1ULL << 40) - 1;
4852 
4853 			bnxt_fill_masks(rx_masks, mask, rx_count);
4854 			bnxt_fill_masks(tx_masks, mask, tx_count);
4855 		} else {
4856 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4857 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
4858 			bnxt_hwrm_port_qstats(bp, 0);
4859 		}
4860 	}
4861 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
4862 		stats = &bp->rx_port_stats_ext;
4863 		rx_stats = stats->hw_stats;
4864 		rx_masks = stats->hw_masks;
4865 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
4866 		stats = &bp->tx_port_stats_ext;
4867 		tx_stats = stats->hw_stats;
4868 		tx_masks = stats->hw_masks;
4869 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
4870 
4871 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4872 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
4873 		if (rc) {
4874 			mask = (1ULL << 40) - 1;
4875 
4876 			bnxt_fill_masks(rx_masks, mask, rx_count);
4877 			if (tx_stats)
4878 				bnxt_fill_masks(tx_masks, mask, tx_count);
4879 		} else {
4880 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4881 			if (tx_stats)
4882 				bnxt_copy_hw_masks(tx_masks, tx_stats,
4883 						   tx_count);
4884 			bnxt_hwrm_port_qstats_ext(bp, 0);
4885 		}
4886 	}
4887 }
4888 
4889 static void bnxt_free_port_stats(struct bnxt *bp)
4890 {
4891 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
4892 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
4893 
4894 	bnxt_free_stats_mem(bp, &bp->port_stats);
4895 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
4896 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
4897 }
4898 
4899 static void bnxt_free_ring_stats(struct bnxt *bp)
4900 {
4901 	int i;
4902 
4903 	if (!bp->bnapi)
4904 		return;
4905 
4906 	for (i = 0; i < bp->cp_nr_rings; i++) {
4907 		struct bnxt_napi *bnapi = bp->bnapi[i];
4908 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4909 
4910 		bnxt_free_stats_mem(bp, &cpr->stats);
4911 
4912 		kfree(cpr->sw_stats);
4913 		cpr->sw_stats = NULL;
4914 	}
4915 }
4916 
4917 static int bnxt_alloc_stats(struct bnxt *bp)
4918 {
4919 	u32 size, i;
4920 	int rc;
4921 
4922 	size = bp->hw_ring_stats_size;
4923 
4924 	for (i = 0; i < bp->cp_nr_rings; i++) {
4925 		struct bnxt_napi *bnapi = bp->bnapi[i];
4926 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4927 
4928 		cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL);
4929 		if (!cpr->sw_stats)
4930 			return -ENOMEM;
4931 
4932 		cpr->stats.len = size;
4933 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
4934 		if (rc)
4935 			return rc;
4936 
4937 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4938 	}
4939 
4940 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
4941 		return 0;
4942 
4943 	if (bp->port_stats.hw_stats)
4944 		goto alloc_ext_stats;
4945 
4946 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
4947 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
4948 	if (rc)
4949 		return rc;
4950 
4951 	bp->flags |= BNXT_FLAG_PORT_STATS;
4952 
4953 alloc_ext_stats:
4954 	/* Display extended statistics only if FW supports it */
4955 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
4956 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
4957 			return 0;
4958 
4959 	if (bp->rx_port_stats_ext.hw_stats)
4960 		goto alloc_tx_ext_stats;
4961 
4962 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
4963 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
4964 	/* Extended stats are optional */
4965 	if (rc)
4966 		return 0;
4967 
4968 alloc_tx_ext_stats:
4969 	if (bp->tx_port_stats_ext.hw_stats)
4970 		return 0;
4971 
4972 	if (bp->hwrm_spec_code >= 0x10902 ||
4973 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
4974 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
4975 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
4976 		/* Extended stats are optional */
4977 		if (rc)
4978 			return 0;
4979 	}
4980 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
4981 	return 0;
4982 }
4983 
4984 static void bnxt_clear_ring_indices(struct bnxt *bp)
4985 {
4986 	int i, j;
4987 
4988 	if (!bp->bnapi)
4989 		return;
4990 
4991 	for (i = 0; i < bp->cp_nr_rings; i++) {
4992 		struct bnxt_napi *bnapi = bp->bnapi[i];
4993 		struct bnxt_cp_ring_info *cpr;
4994 		struct bnxt_rx_ring_info *rxr;
4995 		struct bnxt_tx_ring_info *txr;
4996 
4997 		if (!bnapi)
4998 			continue;
4999 
5000 		cpr = &bnapi->cp_ring;
5001 		cpr->cp_raw_cons = 0;
5002 
5003 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5004 			txr->tx_prod = 0;
5005 			txr->tx_cons = 0;
5006 			txr->tx_hw_cons = 0;
5007 		}
5008 
5009 		rxr = bnapi->rx_ring;
5010 		if (rxr) {
5011 			rxr->rx_prod = 0;
5012 			rxr->rx_agg_prod = 0;
5013 			rxr->rx_sw_agg_prod = 0;
5014 			rxr->rx_next_cons = 0;
5015 		}
5016 		bnapi->events = 0;
5017 	}
5018 }
5019 
5020 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5021 {
5022 	u8 type = fltr->type, flags = fltr->flags;
5023 
5024 	INIT_LIST_HEAD(&fltr->list);
5025 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5026 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5027 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5028 }
5029 
5030 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5031 {
5032 	if (!list_empty(&fltr->list))
5033 		list_del_init(&fltr->list);
5034 }
5035 
5036 void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5037 {
5038 	struct bnxt_filter_base *usr_fltr, *tmp;
5039 
5040 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5041 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5042 			continue;
5043 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5044 	}
5045 }
5046 
5047 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5048 {
5049 	hlist_del(&fltr->hash);
5050 	bnxt_del_one_usr_fltr(bp, fltr);
5051 	if (fltr->flags) {
5052 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5053 		bp->ntp_fltr_count--;
5054 	}
5055 	kfree(fltr);
5056 }
5057 
5058 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5059 {
5060 	int i;
5061 
5062 	/* Under rtnl_lock and all our NAPIs have been disabled.  It's
5063 	 * safe to delete the hash table.
5064 	 */
5065 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5066 		struct hlist_head *head;
5067 		struct hlist_node *tmp;
5068 		struct bnxt_ntuple_filter *fltr;
5069 
5070 		head = &bp->ntp_fltr_hash_tbl[i];
5071 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5072 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5073 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5074 				     !list_empty(&fltr->base.list)))
5075 				continue;
5076 			bnxt_del_fltr(bp, &fltr->base);
5077 		}
5078 	}
5079 	if (!all)
5080 		return;
5081 
5082 	bitmap_free(bp->ntp_fltr_bmap);
5083 	bp->ntp_fltr_bmap = NULL;
5084 	bp->ntp_fltr_count = 0;
5085 }
5086 
5087 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5088 {
5089 	int i, rc = 0;
5090 
5091 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5092 		return 0;
5093 
5094 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5095 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5096 
5097 	bp->ntp_fltr_count = 0;
5098 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5099 
5100 	if (!bp->ntp_fltr_bmap)
5101 		rc = -ENOMEM;
5102 
5103 	return rc;
5104 }
5105 
5106 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5107 {
5108 	int i;
5109 
5110 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5111 		struct hlist_head *head;
5112 		struct hlist_node *tmp;
5113 		struct bnxt_l2_filter *fltr;
5114 
5115 		head = &bp->l2_fltr_hash_tbl[i];
5116 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5117 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5118 				     !list_empty(&fltr->base.list)))
5119 				continue;
5120 			bnxt_del_fltr(bp, &fltr->base);
5121 		}
5122 	}
5123 }
5124 
5125 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5126 {
5127 	int i;
5128 
5129 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5130 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5131 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5132 }
5133 
5134 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5135 {
5136 	bnxt_free_vnic_attributes(bp);
5137 	bnxt_free_tx_rings(bp);
5138 	bnxt_free_rx_rings(bp);
5139 	bnxt_free_cp_rings(bp);
5140 	bnxt_free_all_cp_arrays(bp);
5141 	bnxt_free_ntp_fltrs(bp, false);
5142 	bnxt_free_l2_filters(bp, false);
5143 	if (irq_re_init) {
5144 		bnxt_free_ring_stats(bp);
5145 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5146 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5147 			bnxt_free_port_stats(bp);
5148 		bnxt_free_ring_grps(bp);
5149 		bnxt_free_vnics(bp);
5150 		kfree(bp->tx_ring_map);
5151 		bp->tx_ring_map = NULL;
5152 		kfree(bp->tx_ring);
5153 		bp->tx_ring = NULL;
5154 		kfree(bp->rx_ring);
5155 		bp->rx_ring = NULL;
5156 		kfree(bp->bnapi);
5157 		bp->bnapi = NULL;
5158 	} else {
5159 		bnxt_clear_ring_indices(bp);
5160 	}
5161 }
5162 
5163 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5164 {
5165 	int i, j, rc, size, arr_size;
5166 	void *bnapi;
5167 
5168 	if (irq_re_init) {
5169 		/* Allocate bnapi mem pointer array and mem block for
5170 		 * all queues
5171 		 */
5172 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5173 				bp->cp_nr_rings);
5174 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5175 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5176 		if (!bnapi)
5177 			return -ENOMEM;
5178 
5179 		bp->bnapi = bnapi;
5180 		bnapi += arr_size;
5181 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5182 			bp->bnapi[i] = bnapi;
5183 			bp->bnapi[i]->index = i;
5184 			bp->bnapi[i]->bp = bp;
5185 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5186 				struct bnxt_cp_ring_info *cpr =
5187 					&bp->bnapi[i]->cp_ring;
5188 
5189 				cpr->cp_ring_struct.ring_mem.flags =
5190 					BNXT_RMEM_RING_PTE_FLAG;
5191 			}
5192 		}
5193 
5194 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5195 				      sizeof(struct bnxt_rx_ring_info),
5196 				      GFP_KERNEL);
5197 		if (!bp->rx_ring)
5198 			return -ENOMEM;
5199 
5200 		for (i = 0; i < bp->rx_nr_rings; i++) {
5201 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5202 
5203 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5204 				rxr->rx_ring_struct.ring_mem.flags =
5205 					BNXT_RMEM_RING_PTE_FLAG;
5206 				rxr->rx_agg_ring_struct.ring_mem.flags =
5207 					BNXT_RMEM_RING_PTE_FLAG;
5208 			} else {
5209 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5210 			}
5211 			rxr->bnapi = bp->bnapi[i];
5212 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5213 		}
5214 
5215 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5216 				      sizeof(struct bnxt_tx_ring_info),
5217 				      GFP_KERNEL);
5218 		if (!bp->tx_ring)
5219 			return -ENOMEM;
5220 
5221 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5222 					  GFP_KERNEL);
5223 
5224 		if (!bp->tx_ring_map)
5225 			return -ENOMEM;
5226 
5227 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5228 			j = 0;
5229 		else
5230 			j = bp->rx_nr_rings;
5231 
5232 		for (i = 0; i < bp->tx_nr_rings; i++) {
5233 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5234 			struct bnxt_napi *bnapi2;
5235 
5236 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5237 				txr->tx_ring_struct.ring_mem.flags =
5238 					BNXT_RMEM_RING_PTE_FLAG;
5239 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5240 			if (i >= bp->tx_nr_rings_xdp) {
5241 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5242 
5243 				bnapi2 = bp->bnapi[k];
5244 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5245 				txr->tx_napi_idx =
5246 					BNXT_RING_TO_TC(bp, txr->txq_index);
5247 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5248 				bnapi2->tx_int = bnxt_tx_int;
5249 			} else {
5250 				bnapi2 = bp->bnapi[j];
5251 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5252 				bnapi2->tx_ring[0] = txr;
5253 				bnapi2->tx_int = bnxt_tx_int_xdp;
5254 				j++;
5255 			}
5256 			txr->bnapi = bnapi2;
5257 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5258 				txr->tx_cpr = &bnapi2->cp_ring;
5259 		}
5260 
5261 		rc = bnxt_alloc_stats(bp);
5262 		if (rc)
5263 			goto alloc_mem_err;
5264 		bnxt_init_stats(bp);
5265 
5266 		rc = bnxt_alloc_ntp_fltrs(bp);
5267 		if (rc)
5268 			goto alloc_mem_err;
5269 
5270 		rc = bnxt_alloc_vnics(bp);
5271 		if (rc)
5272 			goto alloc_mem_err;
5273 	}
5274 
5275 	rc = bnxt_alloc_all_cp_arrays(bp);
5276 	if (rc)
5277 		goto alloc_mem_err;
5278 
5279 	bnxt_init_ring_struct(bp);
5280 
5281 	rc = bnxt_alloc_rx_rings(bp);
5282 	if (rc)
5283 		goto alloc_mem_err;
5284 
5285 	rc = bnxt_alloc_tx_rings(bp);
5286 	if (rc)
5287 		goto alloc_mem_err;
5288 
5289 	rc = bnxt_alloc_cp_rings(bp);
5290 	if (rc)
5291 		goto alloc_mem_err;
5292 
5293 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5294 						  BNXT_VNIC_MCAST_FLAG |
5295 						  BNXT_VNIC_UCAST_FLAG;
5296 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5297 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5298 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5299 
5300 	rc = bnxt_alloc_vnic_attributes(bp);
5301 	if (rc)
5302 		goto alloc_mem_err;
5303 	return 0;
5304 
5305 alloc_mem_err:
5306 	bnxt_free_mem(bp, true);
5307 	return rc;
5308 }
5309 
5310 static void bnxt_disable_int(struct bnxt *bp)
5311 {
5312 	int i;
5313 
5314 	if (!bp->bnapi)
5315 		return;
5316 
5317 	for (i = 0; i < bp->cp_nr_rings; i++) {
5318 		struct bnxt_napi *bnapi = bp->bnapi[i];
5319 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5320 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5321 
5322 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5323 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5324 	}
5325 }
5326 
5327 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5328 {
5329 	struct bnxt_napi *bnapi = bp->bnapi[n];
5330 	struct bnxt_cp_ring_info *cpr;
5331 
5332 	cpr = &bnapi->cp_ring;
5333 	return cpr->cp_ring_struct.map_idx;
5334 }
5335 
5336 static void bnxt_disable_int_sync(struct bnxt *bp)
5337 {
5338 	int i;
5339 
5340 	if (!bp->irq_tbl)
5341 		return;
5342 
5343 	atomic_inc(&bp->intr_sem);
5344 
5345 	bnxt_disable_int(bp);
5346 	for (i = 0; i < bp->cp_nr_rings; i++) {
5347 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5348 
5349 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5350 	}
5351 }
5352 
5353 static void bnxt_enable_int(struct bnxt *bp)
5354 {
5355 	int i;
5356 
5357 	atomic_set(&bp->intr_sem, 0);
5358 	for (i = 0; i < bp->cp_nr_rings; i++) {
5359 		struct bnxt_napi *bnapi = bp->bnapi[i];
5360 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5361 
5362 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5363 	}
5364 }
5365 
5366 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5367 			    bool async_only)
5368 {
5369 	DECLARE_BITMAP(async_events_bmap, 256);
5370 	u32 *events = (u32 *)async_events_bmap;
5371 	struct hwrm_func_drv_rgtr_output *resp;
5372 	struct hwrm_func_drv_rgtr_input *req;
5373 	u32 flags;
5374 	int rc, i;
5375 
5376 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5377 	if (rc)
5378 		return rc;
5379 
5380 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5381 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5382 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5383 
5384 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5385 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5386 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5387 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5388 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5389 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5390 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5391 	req->flags = cpu_to_le32(flags);
5392 	req->ver_maj_8b = DRV_VER_MAJ;
5393 	req->ver_min_8b = DRV_VER_MIN;
5394 	req->ver_upd_8b = DRV_VER_UPD;
5395 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5396 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5397 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5398 
5399 	if (BNXT_PF(bp)) {
5400 		u32 data[8];
5401 		int i;
5402 
5403 		memset(data, 0, sizeof(data));
5404 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5405 			u16 cmd = bnxt_vf_req_snif[i];
5406 			unsigned int bit, idx;
5407 
5408 			idx = cmd / 32;
5409 			bit = cmd % 32;
5410 			data[idx] |= 1 << bit;
5411 		}
5412 
5413 		for (i = 0; i < 8; i++)
5414 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5415 
5416 		req->enables |=
5417 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5418 	}
5419 
5420 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5421 		req->flags |= cpu_to_le32(
5422 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5423 
5424 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5425 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5426 		u16 event_id = bnxt_async_events_arr[i];
5427 
5428 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5429 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5430 			continue;
5431 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5432 		    !bp->ptp_cfg)
5433 			continue;
5434 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5435 	}
5436 	if (bmap && bmap_size) {
5437 		for (i = 0; i < bmap_size; i++) {
5438 			if (test_bit(i, bmap))
5439 				__set_bit(i, async_events_bmap);
5440 		}
5441 	}
5442 	for (i = 0; i < 8; i++)
5443 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5444 
5445 	if (async_only)
5446 		req->enables =
5447 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5448 
5449 	resp = hwrm_req_hold(bp, req);
5450 	rc = hwrm_req_send(bp, req);
5451 	if (!rc) {
5452 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5453 		if (resp->flags &
5454 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5455 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5456 	}
5457 	hwrm_req_drop(bp, req);
5458 	return rc;
5459 }
5460 
5461 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5462 {
5463 	struct hwrm_func_drv_unrgtr_input *req;
5464 	int rc;
5465 
5466 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5467 		return 0;
5468 
5469 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5470 	if (rc)
5471 		return rc;
5472 	return hwrm_req_send(bp, req);
5473 }
5474 
5475 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5476 
5477 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5478 {
5479 	struct hwrm_tunnel_dst_port_free_input *req;
5480 	int rc;
5481 
5482 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5483 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5484 		return 0;
5485 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5486 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5487 		return 0;
5488 
5489 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5490 	if (rc)
5491 		return rc;
5492 
5493 	req->tunnel_type = tunnel_type;
5494 
5495 	switch (tunnel_type) {
5496 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5497 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5498 		bp->vxlan_port = 0;
5499 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5500 		break;
5501 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5502 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5503 		bp->nge_port = 0;
5504 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5505 		break;
5506 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5507 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5508 		bp->vxlan_gpe_port = 0;
5509 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5510 		break;
5511 	default:
5512 		break;
5513 	}
5514 
5515 	rc = hwrm_req_send(bp, req);
5516 	if (rc)
5517 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5518 			   rc);
5519 	if (bp->flags & BNXT_FLAG_TPA)
5520 		bnxt_set_tpa(bp, true);
5521 	return rc;
5522 }
5523 
5524 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5525 					   u8 tunnel_type)
5526 {
5527 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5528 	struct hwrm_tunnel_dst_port_alloc_input *req;
5529 	int rc;
5530 
5531 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5532 	if (rc)
5533 		return rc;
5534 
5535 	req->tunnel_type = tunnel_type;
5536 	req->tunnel_dst_port_val = port;
5537 
5538 	resp = hwrm_req_hold(bp, req);
5539 	rc = hwrm_req_send(bp, req);
5540 	if (rc) {
5541 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5542 			   rc);
5543 		goto err_out;
5544 	}
5545 
5546 	switch (tunnel_type) {
5547 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5548 		bp->vxlan_port = port;
5549 		bp->vxlan_fw_dst_port_id =
5550 			le16_to_cpu(resp->tunnel_dst_port_id);
5551 		break;
5552 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5553 		bp->nge_port = port;
5554 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5555 		break;
5556 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5557 		bp->vxlan_gpe_port = port;
5558 		bp->vxlan_gpe_fw_dst_port_id =
5559 			le16_to_cpu(resp->tunnel_dst_port_id);
5560 		break;
5561 	default:
5562 		break;
5563 	}
5564 	if (bp->flags & BNXT_FLAG_TPA)
5565 		bnxt_set_tpa(bp, true);
5566 
5567 err_out:
5568 	hwrm_req_drop(bp, req);
5569 	return rc;
5570 }
5571 
5572 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5573 {
5574 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5575 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5576 	int rc;
5577 
5578 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5579 	if (rc)
5580 		return rc;
5581 
5582 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5583 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5584 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5585 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5586 	}
5587 	req->mask = cpu_to_le32(vnic->rx_mask);
5588 	return hwrm_req_send_silent(bp, req);
5589 }
5590 
5591 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5592 {
5593 	if (!atomic_dec_and_test(&fltr->refcnt))
5594 		return;
5595 	spin_lock_bh(&bp->ntp_fltr_lock);
5596 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5597 		spin_unlock_bh(&bp->ntp_fltr_lock);
5598 		return;
5599 	}
5600 	hlist_del_rcu(&fltr->base.hash);
5601 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5602 	if (fltr->base.flags) {
5603 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5604 		bp->ntp_fltr_count--;
5605 	}
5606 	spin_unlock_bh(&bp->ntp_fltr_lock);
5607 	kfree_rcu(fltr, base.rcu);
5608 }
5609 
5610 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5611 						      struct bnxt_l2_key *key,
5612 						      u32 idx)
5613 {
5614 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5615 	struct bnxt_l2_filter *fltr;
5616 
5617 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5618 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5619 
5620 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5621 		    l2_key->vlan == key->vlan)
5622 			return fltr;
5623 	}
5624 	return NULL;
5625 }
5626 
5627 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5628 						    struct bnxt_l2_key *key,
5629 						    u32 idx)
5630 {
5631 	struct bnxt_l2_filter *fltr = NULL;
5632 
5633 	rcu_read_lock();
5634 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5635 	if (fltr)
5636 		atomic_inc(&fltr->refcnt);
5637 	rcu_read_unlock();
5638 	return fltr;
5639 }
5640 
5641 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5642 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5643 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5644 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5645 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5646 
5647 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5648 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5649 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5650 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5651 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5652 
5653 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5654 {
5655 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5656 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5657 			return sizeof(fkeys->addrs.v4addrs) +
5658 			       sizeof(fkeys->ports);
5659 
5660 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5661 			return sizeof(fkeys->addrs.v4addrs);
5662 	}
5663 
5664 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5665 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5666 			return sizeof(fkeys->addrs.v6addrs) +
5667 			       sizeof(fkeys->ports);
5668 
5669 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5670 			return sizeof(fkeys->addrs.v6addrs);
5671 	}
5672 
5673 	return 0;
5674 }
5675 
5676 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5677 			 const unsigned char *key)
5678 {
5679 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5680 	struct bnxt_ipv4_tuple tuple4;
5681 	struct bnxt_ipv6_tuple tuple6;
5682 	int i, j, len = 0;
5683 	u8 *four_tuple;
5684 
5685 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5686 	if (!len)
5687 		return 0;
5688 
5689 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5690 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5691 		tuple4.ports = fkeys->ports;
5692 		four_tuple = (unsigned char *)&tuple4;
5693 	} else {
5694 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5695 		tuple6.ports = fkeys->ports;
5696 		four_tuple = (unsigned char *)&tuple6;
5697 	}
5698 
5699 	for (i = 0, j = 8; i < len; i++, j++) {
5700 		u8 byte = four_tuple[i];
5701 		int bit;
5702 
5703 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5704 			if (byte & 0x80)
5705 				hash ^= prefix;
5706 		}
5707 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5708 	}
5709 
5710 	/* The valid part of the hash is in the upper 32 bits. */
5711 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5712 }
5713 
5714 #ifdef CONFIG_RFS_ACCEL
5715 static struct bnxt_l2_filter *
5716 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5717 {
5718 	struct bnxt_l2_filter *fltr;
5719 	u32 idx;
5720 
5721 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5722 	      BNXT_L2_FLTR_HASH_MASK;
5723 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5724 	return fltr;
5725 }
5726 #endif
5727 
5728 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
5729 			       struct bnxt_l2_key *key, u32 idx)
5730 {
5731 	struct hlist_head *head;
5732 
5733 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
5734 	fltr->l2_key.vlan = key->vlan;
5735 	fltr->base.type = BNXT_FLTR_TYPE_L2;
5736 	if (fltr->base.flags) {
5737 		int bit_id;
5738 
5739 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
5740 						 bp->max_fltr, 0);
5741 		if (bit_id < 0)
5742 			return -ENOMEM;
5743 		fltr->base.sw_id = (u16)bit_id;
5744 		bp->ntp_fltr_count++;
5745 	}
5746 	head = &bp->l2_fltr_hash_tbl[idx];
5747 	hlist_add_head_rcu(&fltr->base.hash, head);
5748 	bnxt_insert_usr_fltr(bp, &fltr->base);
5749 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
5750 	atomic_set(&fltr->refcnt, 1);
5751 	return 0;
5752 }
5753 
5754 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
5755 						   struct bnxt_l2_key *key,
5756 						   gfp_t gfp)
5757 {
5758 	struct bnxt_l2_filter *fltr;
5759 	u32 idx;
5760 	int rc;
5761 
5762 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5763 	      BNXT_L2_FLTR_HASH_MASK;
5764 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5765 	if (fltr)
5766 		return fltr;
5767 
5768 	fltr = kzalloc(sizeof(*fltr), gfp);
5769 	if (!fltr)
5770 		return ERR_PTR(-ENOMEM);
5771 	spin_lock_bh(&bp->ntp_fltr_lock);
5772 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5773 	spin_unlock_bh(&bp->ntp_fltr_lock);
5774 	if (rc) {
5775 		bnxt_del_l2_filter(bp, fltr);
5776 		fltr = ERR_PTR(rc);
5777 	}
5778 	return fltr;
5779 }
5780 
5781 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
5782 						struct bnxt_l2_key *key,
5783 						u16 flags)
5784 {
5785 	struct bnxt_l2_filter *fltr;
5786 	u32 idx;
5787 	int rc;
5788 
5789 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5790 	      BNXT_L2_FLTR_HASH_MASK;
5791 	spin_lock_bh(&bp->ntp_fltr_lock);
5792 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5793 	if (fltr) {
5794 		fltr = ERR_PTR(-EEXIST);
5795 		goto l2_filter_exit;
5796 	}
5797 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
5798 	if (!fltr) {
5799 		fltr = ERR_PTR(-ENOMEM);
5800 		goto l2_filter_exit;
5801 	}
5802 	fltr->base.flags = flags;
5803 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5804 	if (rc) {
5805 		spin_unlock_bh(&bp->ntp_fltr_lock);
5806 		bnxt_del_l2_filter(bp, fltr);
5807 		return ERR_PTR(rc);
5808 	}
5809 
5810 l2_filter_exit:
5811 	spin_unlock_bh(&bp->ntp_fltr_lock);
5812 	return fltr;
5813 }
5814 
5815 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
5816 {
5817 #ifdef CONFIG_BNXT_SRIOV
5818 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
5819 
5820 	return vf->fw_fid;
5821 #else
5822 	return INVALID_HW_RING_ID;
5823 #endif
5824 }
5825 
5826 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5827 {
5828 	struct hwrm_cfa_l2_filter_free_input *req;
5829 	u16 target_id = 0xffff;
5830 	int rc;
5831 
5832 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
5833 		struct bnxt_pf_info *pf = &bp->pf;
5834 
5835 		if (fltr->base.vf_idx >= pf->active_vfs)
5836 			return -EINVAL;
5837 
5838 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
5839 		if (target_id == INVALID_HW_RING_ID)
5840 			return -EINVAL;
5841 	}
5842 
5843 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
5844 	if (rc)
5845 		return rc;
5846 
5847 	req->target_id = cpu_to_le16(target_id);
5848 	req->l2_filter_id = fltr->base.filter_id;
5849 	return hwrm_req_send(bp, req);
5850 }
5851 
5852 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5853 {
5854 	struct hwrm_cfa_l2_filter_alloc_output *resp;
5855 	struct hwrm_cfa_l2_filter_alloc_input *req;
5856 	u16 target_id = 0xffff;
5857 	int rc;
5858 
5859 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
5860 		struct bnxt_pf_info *pf = &bp->pf;
5861 
5862 		if (fltr->base.vf_idx >= pf->active_vfs)
5863 			return -EINVAL;
5864 
5865 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
5866 	}
5867 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
5868 	if (rc)
5869 		return rc;
5870 
5871 	req->target_id = cpu_to_le16(target_id);
5872 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
5873 
5874 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
5875 		req->flags |=
5876 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
5877 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
5878 	req->enables =
5879 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
5880 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
5881 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
5882 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
5883 	eth_broadcast_addr(req->l2_addr_mask);
5884 
5885 	if (fltr->l2_key.vlan) {
5886 		req->enables |=
5887 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
5888 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
5889 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
5890 		req->num_vlans = 1;
5891 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
5892 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
5893 	}
5894 
5895 	resp = hwrm_req_hold(bp, req);
5896 	rc = hwrm_req_send(bp, req);
5897 	if (!rc) {
5898 		fltr->base.filter_id = resp->l2_filter_id;
5899 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
5900 	}
5901 	hwrm_req_drop(bp, req);
5902 	return rc;
5903 }
5904 
5905 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
5906 				     struct bnxt_ntuple_filter *fltr)
5907 {
5908 	struct hwrm_cfa_ntuple_filter_free_input *req;
5909 	int rc;
5910 
5911 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
5912 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
5913 	if (rc)
5914 		return rc;
5915 
5916 	req->ntuple_filter_id = fltr->base.filter_id;
5917 	return hwrm_req_send(bp, req);
5918 }
5919 
5920 #define BNXT_NTP_FLTR_FLAGS					\
5921 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
5922 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
5923 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
5924 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
5925 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
5926 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
5927 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
5928 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
5929 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
5930 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
5931 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
5932 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
5933 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
5934 
5935 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
5936 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
5937 
5938 void bnxt_fill_ipv6_mask(__be32 mask[4])
5939 {
5940 	int i;
5941 
5942 	for (i = 0; i < 4; i++)
5943 		mask[i] = cpu_to_be32(~0);
5944 }
5945 
5946 static void
5947 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
5948 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
5949 			  struct bnxt_ntuple_filter *fltr)
5950 {
5951 	struct bnxt_rss_ctx *rss_ctx, *tmp;
5952 	u16 rxq = fltr->base.rxq;
5953 
5954 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
5955 		list_for_each_entry_safe(rss_ctx, tmp, &bp->rss_ctx_list, list) {
5956 			if (rss_ctx->index == fltr->base.fw_vnic_id) {
5957 				struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
5958 
5959 				req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
5960 				break;
5961 			}
5962 		}
5963 		return;
5964 	}
5965 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
5966 		struct bnxt_vnic_info *vnic;
5967 		u32 enables;
5968 
5969 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
5970 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
5971 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
5972 		req->enables |= cpu_to_le32(enables);
5973 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
5974 	} else {
5975 		u32 flags;
5976 
5977 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
5978 		req->flags |= cpu_to_le32(flags);
5979 		req->dst_id = cpu_to_le16(rxq);
5980 	}
5981 }
5982 
5983 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
5984 				      struct bnxt_ntuple_filter *fltr)
5985 {
5986 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
5987 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
5988 	struct bnxt_flow_masks *masks = &fltr->fmasks;
5989 	struct flow_keys *keys = &fltr->fkeys;
5990 	struct bnxt_l2_filter *l2_fltr;
5991 	struct bnxt_vnic_info *vnic;
5992 	int rc;
5993 
5994 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
5995 	if (rc)
5996 		return rc;
5997 
5998 	l2_fltr = fltr->l2_fltr;
5999 	req->l2_filter_id = l2_fltr->base.filter_id;
6000 
6001 	if (fltr->base.flags & BNXT_ACT_DROP) {
6002 		req->flags =
6003 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6004 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6005 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6006 	} else {
6007 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6008 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6009 	}
6010 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6011 
6012 	req->ethertype = htons(ETH_P_IP);
6013 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6014 	req->ip_protocol = keys->basic.ip_proto;
6015 
6016 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6017 		req->ethertype = htons(ETH_P_IPV6);
6018 		req->ip_addr_type =
6019 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6020 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6021 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6022 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6023 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6024 	} else {
6025 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6026 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6027 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6028 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6029 	}
6030 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6031 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6032 		req->tunnel_type =
6033 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6034 	}
6035 
6036 	req->src_port = keys->ports.src;
6037 	req->src_port_mask = masks->ports.src;
6038 	req->dst_port = keys->ports.dst;
6039 	req->dst_port_mask = masks->ports.dst;
6040 
6041 	resp = hwrm_req_hold(bp, req);
6042 	rc = hwrm_req_send(bp, req);
6043 	if (!rc)
6044 		fltr->base.filter_id = resp->ntuple_filter_id;
6045 	hwrm_req_drop(bp, req);
6046 	return rc;
6047 }
6048 
6049 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6050 				     const u8 *mac_addr)
6051 {
6052 	struct bnxt_l2_filter *fltr;
6053 	struct bnxt_l2_key key;
6054 	int rc;
6055 
6056 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6057 	key.vlan = 0;
6058 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6059 	if (IS_ERR(fltr))
6060 		return PTR_ERR(fltr);
6061 
6062 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6063 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6064 	if (rc)
6065 		bnxt_del_l2_filter(bp, fltr);
6066 	else
6067 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6068 	return rc;
6069 }
6070 
6071 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6072 {
6073 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6074 
6075 	/* Any associated ntuple filters will also be cleared by firmware. */
6076 	for (i = 0; i < num_of_vnics; i++) {
6077 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6078 
6079 		for (j = 0; j < vnic->uc_filter_count; j++) {
6080 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6081 
6082 			bnxt_hwrm_l2_filter_free(bp, fltr);
6083 			bnxt_del_l2_filter(bp, fltr);
6084 		}
6085 		vnic->uc_filter_count = 0;
6086 	}
6087 }
6088 
6089 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6090 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6091 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6092 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6093 
6094 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6095 					   struct hwrm_vnic_tpa_cfg_input *req)
6096 {
6097 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6098 
6099 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6100 		return;
6101 
6102 	if (bp->vxlan_port)
6103 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6104 	if (bp->vxlan_gpe_port)
6105 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6106 	if (bp->nge_port)
6107 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6108 
6109 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6110 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6111 }
6112 
6113 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6114 			   u32 tpa_flags)
6115 {
6116 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6117 	struct hwrm_vnic_tpa_cfg_input *req;
6118 	int rc;
6119 
6120 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6121 		return 0;
6122 
6123 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6124 	if (rc)
6125 		return rc;
6126 
6127 	if (tpa_flags) {
6128 		u16 mss = bp->dev->mtu - 40;
6129 		u32 nsegs, n, segs = 0, flags;
6130 
6131 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6132 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6133 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6134 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6135 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6136 		if (tpa_flags & BNXT_FLAG_GRO)
6137 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6138 
6139 		req->flags = cpu_to_le32(flags);
6140 
6141 		req->enables =
6142 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6143 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6144 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6145 
6146 		/* Number of segs are log2 units, and first packet is not
6147 		 * included as part of this units.
6148 		 */
6149 		if (mss <= BNXT_RX_PAGE_SIZE) {
6150 			n = BNXT_RX_PAGE_SIZE / mss;
6151 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6152 		} else {
6153 			n = mss / BNXT_RX_PAGE_SIZE;
6154 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6155 				n++;
6156 			nsegs = (MAX_SKB_FRAGS - n) / n;
6157 		}
6158 
6159 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6160 			segs = MAX_TPA_SEGS_P5;
6161 			max_aggs = bp->max_tpa;
6162 		} else {
6163 			segs = ilog2(nsegs);
6164 		}
6165 		req->max_agg_segs = cpu_to_le16(segs);
6166 		req->max_aggs = cpu_to_le16(max_aggs);
6167 
6168 		req->min_agg_len = cpu_to_le32(512);
6169 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6170 	}
6171 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6172 
6173 	return hwrm_req_send(bp, req);
6174 }
6175 
6176 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6177 {
6178 	struct bnxt_ring_grp_info *grp_info;
6179 
6180 	grp_info = &bp->grp_info[ring->grp_idx];
6181 	return grp_info->cp_fw_ring_id;
6182 }
6183 
6184 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6185 {
6186 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6187 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6188 	else
6189 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6190 }
6191 
6192 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6193 {
6194 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6195 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6196 	else
6197 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6198 }
6199 
6200 int bnxt_alloc_rss_indir_tbl(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx)
6201 {
6202 	int entries;
6203 	u16 *tbl;
6204 
6205 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6206 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6207 	else
6208 		entries = HW_HASH_INDEX_SIZE;
6209 
6210 	bp->rss_indir_tbl_entries = entries;
6211 	tbl = kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6212 	if (!tbl)
6213 		return -ENOMEM;
6214 
6215 	if (rss_ctx)
6216 		rss_ctx->rss_indir_tbl = tbl;
6217 	else
6218 		bp->rss_indir_tbl = tbl;
6219 
6220 	return 0;
6221 }
6222 
6223 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx)
6224 {
6225 	u16 max_rings, max_entries, pad, i;
6226 	u16 *rss_indir_tbl;
6227 
6228 	if (!bp->rx_nr_rings)
6229 		return;
6230 
6231 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6232 		max_rings = bp->rx_nr_rings - 1;
6233 	else
6234 		max_rings = bp->rx_nr_rings;
6235 
6236 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6237 	if (rss_ctx)
6238 		rss_indir_tbl = &rss_ctx->rss_indir_tbl[0];
6239 	else
6240 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6241 
6242 	for (i = 0; i < max_entries; i++)
6243 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6244 
6245 	pad = bp->rss_indir_tbl_entries - max_entries;
6246 	if (pad)
6247 		memset(&rss_indir_tbl[i], 0, pad * sizeof(u16));
6248 }
6249 
6250 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6251 {
6252 	u16 i, tbl_size, max_ring = 0;
6253 
6254 	if (!bp->rss_indir_tbl)
6255 		return 0;
6256 
6257 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6258 	for (i = 0; i < tbl_size; i++)
6259 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6260 	return max_ring;
6261 }
6262 
6263 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6264 {
6265 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6266 		if (!rx_rings)
6267 			return 0;
6268 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6269 					       BNXT_RSS_TABLE_ENTRIES_P5);
6270 	}
6271 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6272 		return 2;
6273 	return 1;
6274 }
6275 
6276 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6277 {
6278 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6279 	u16 i, j;
6280 
6281 	/* Fill the RSS indirection table with ring group ids */
6282 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6283 		if (!no_rss)
6284 			j = bp->rss_indir_tbl[i];
6285 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6286 	}
6287 }
6288 
6289 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6290 				    struct bnxt_vnic_info *vnic)
6291 {
6292 	__le16 *ring_tbl = vnic->rss_table;
6293 	struct bnxt_rx_ring_info *rxr;
6294 	u16 tbl_size, i;
6295 
6296 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6297 
6298 	for (i = 0; i < tbl_size; i++) {
6299 		u16 ring_id, j;
6300 
6301 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6302 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6303 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6304 			j = vnic->rss_ctx->rss_indir_tbl[i];
6305 		else
6306 			j = bp->rss_indir_tbl[i];
6307 		rxr = &bp->rx_ring[j];
6308 
6309 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6310 		*ring_tbl++ = cpu_to_le16(ring_id);
6311 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6312 		*ring_tbl++ = cpu_to_le16(ring_id);
6313 	}
6314 }
6315 
6316 static void
6317 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6318 			 struct bnxt_vnic_info *vnic)
6319 {
6320 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6321 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6322 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6323 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6324 	} else {
6325 		bnxt_fill_hw_rss_tbl(bp, vnic);
6326 	}
6327 
6328 	if (bp->rss_hash_delta) {
6329 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6330 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6331 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6332 		else
6333 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6334 	} else {
6335 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6336 	}
6337 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6338 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6339 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6340 }
6341 
6342 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6343 				  bool set_rss)
6344 {
6345 	struct hwrm_vnic_rss_cfg_input *req;
6346 	int rc;
6347 
6348 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6349 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6350 		return 0;
6351 
6352 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6353 	if (rc)
6354 		return rc;
6355 
6356 	if (set_rss)
6357 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6358 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6359 	return hwrm_req_send(bp, req);
6360 }
6361 
6362 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6363 				     struct bnxt_vnic_info *vnic, bool set_rss)
6364 {
6365 	struct hwrm_vnic_rss_cfg_input *req;
6366 	dma_addr_t ring_tbl_map;
6367 	u32 i, nr_ctxs;
6368 	int rc;
6369 
6370 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6371 	if (rc)
6372 		return rc;
6373 
6374 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6375 	if (!set_rss)
6376 		return hwrm_req_send(bp, req);
6377 
6378 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6379 	ring_tbl_map = vnic->rss_table_dma_addr;
6380 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6381 
6382 	hwrm_req_hold(bp, req);
6383 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6384 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6385 		req->ring_table_pair_index = i;
6386 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6387 		rc = hwrm_req_send(bp, req);
6388 		if (rc)
6389 			goto exit;
6390 	}
6391 
6392 exit:
6393 	hwrm_req_drop(bp, req);
6394 	return rc;
6395 }
6396 
6397 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6398 {
6399 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6400 	struct hwrm_vnic_rss_qcfg_output *resp;
6401 	struct hwrm_vnic_rss_qcfg_input *req;
6402 
6403 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6404 		return;
6405 
6406 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6407 	/* all contexts configured to same hash_type, zero always exists */
6408 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6409 	resp = hwrm_req_hold(bp, req);
6410 	if (!hwrm_req_send(bp, req)) {
6411 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6412 		bp->rss_hash_delta = 0;
6413 	}
6414 	hwrm_req_drop(bp, req);
6415 }
6416 
6417 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6418 {
6419 	struct hwrm_vnic_plcmodes_cfg_input *req;
6420 	int rc;
6421 
6422 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6423 	if (rc)
6424 		return rc;
6425 
6426 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6427 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6428 
6429 	if (BNXT_RX_PAGE_MODE(bp)) {
6430 		req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6431 	} else {
6432 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6433 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6434 		req->enables |=
6435 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6436 		req->jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
6437 		req->hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
6438 	}
6439 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6440 	return hwrm_req_send(bp, req);
6441 }
6442 
6443 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6444 					struct bnxt_vnic_info *vnic,
6445 					u16 ctx_idx)
6446 {
6447 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6448 
6449 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6450 		return;
6451 
6452 	req->rss_cos_lb_ctx_id =
6453 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6454 
6455 	hwrm_req_send(bp, req);
6456 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6457 }
6458 
6459 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6460 {
6461 	int i, j;
6462 
6463 	for (i = 0; i < bp->nr_vnics; i++) {
6464 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6465 
6466 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6467 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6468 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6469 		}
6470 	}
6471 	bp->rsscos_nr_ctxs = 0;
6472 }
6473 
6474 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6475 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6476 {
6477 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6478 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6479 	int rc;
6480 
6481 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6482 	if (rc)
6483 		return rc;
6484 
6485 	resp = hwrm_req_hold(bp, req);
6486 	rc = hwrm_req_send(bp, req);
6487 	if (!rc)
6488 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6489 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6490 	hwrm_req_drop(bp, req);
6491 
6492 	return rc;
6493 }
6494 
6495 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6496 {
6497 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6498 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6499 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6500 }
6501 
6502 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6503 {
6504 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6505 	struct hwrm_vnic_cfg_input *req;
6506 	unsigned int ring = 0, grp_idx;
6507 	u16 def_vlan = 0;
6508 	int rc;
6509 
6510 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6511 	if (rc)
6512 		return rc;
6513 
6514 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6515 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6516 
6517 		req->default_rx_ring_id =
6518 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6519 		req->default_cmpl_ring_id =
6520 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6521 		req->enables =
6522 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6523 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6524 		goto vnic_mru;
6525 	}
6526 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6527 	/* Only RSS support for now TBD: COS & LB */
6528 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6529 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6530 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6531 					   VNIC_CFG_REQ_ENABLES_MRU);
6532 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6533 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6534 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6535 					   VNIC_CFG_REQ_ENABLES_MRU);
6536 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6537 	} else {
6538 		req->rss_rule = cpu_to_le16(0xffff);
6539 	}
6540 
6541 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6542 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6543 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6544 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6545 	} else {
6546 		req->cos_rule = cpu_to_le16(0xffff);
6547 	}
6548 
6549 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6550 		ring = 0;
6551 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6552 		ring = vnic->vnic_id - 1;
6553 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6554 		ring = bp->rx_nr_rings - 1;
6555 
6556 	grp_idx = bp->rx_ring[ring].bnapi->index;
6557 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6558 	req->lb_rule = cpu_to_le16(0xffff);
6559 vnic_mru:
6560 	req->mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + VLAN_HLEN);
6561 
6562 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6563 #ifdef CONFIG_BNXT_SRIOV
6564 	if (BNXT_VF(bp))
6565 		def_vlan = bp->vf.vlan;
6566 #endif
6567 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6568 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6569 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6570 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6571 
6572 	return hwrm_req_send(bp, req);
6573 }
6574 
6575 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6576 				    struct bnxt_vnic_info *vnic)
6577 {
6578 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6579 		struct hwrm_vnic_free_input *req;
6580 
6581 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6582 			return;
6583 
6584 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6585 
6586 		hwrm_req_send(bp, req);
6587 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6588 	}
6589 }
6590 
6591 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6592 {
6593 	u16 i;
6594 
6595 	for (i = 0; i < bp->nr_vnics; i++)
6596 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6597 }
6598 
6599 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6600 			 unsigned int start_rx_ring_idx,
6601 			 unsigned int nr_rings)
6602 {
6603 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6604 	struct hwrm_vnic_alloc_output *resp;
6605 	struct hwrm_vnic_alloc_input *req;
6606 	int rc;
6607 
6608 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6609 	if (rc)
6610 		return rc;
6611 
6612 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6613 		goto vnic_no_ring_grps;
6614 
6615 	/* map ring groups to this vnic */
6616 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6617 		grp_idx = bp->rx_ring[i].bnapi->index;
6618 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6619 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6620 				   j, nr_rings);
6621 			break;
6622 		}
6623 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6624 	}
6625 
6626 vnic_no_ring_grps:
6627 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6628 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6629 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6630 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6631 
6632 	resp = hwrm_req_hold(bp, req);
6633 	rc = hwrm_req_send(bp, req);
6634 	if (!rc)
6635 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6636 	hwrm_req_drop(bp, req);
6637 	return rc;
6638 }
6639 
6640 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6641 {
6642 	struct hwrm_vnic_qcaps_output *resp;
6643 	struct hwrm_vnic_qcaps_input *req;
6644 	int rc;
6645 
6646 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6647 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6648 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6649 	if (bp->hwrm_spec_code < 0x10600)
6650 		return 0;
6651 
6652 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6653 	if (rc)
6654 		return rc;
6655 
6656 	resp = hwrm_req_hold(bp, req);
6657 	rc = hwrm_req_send(bp, req);
6658 	if (!rc) {
6659 		u32 flags = le32_to_cpu(resp->flags);
6660 
6661 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6662 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6663 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6664 		if (flags &
6665 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6666 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6667 
6668 		/* Older P5 fw before EXT_HW_STATS support did not set
6669 		 * VLAN_STRIP_CAP properly.
6670 		 */
6671 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6672 		    (BNXT_CHIP_P5(bp) &&
6673 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6674 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6675 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6676 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6677 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6678 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6679 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6680 		if (bp->max_tpa_v2) {
6681 			if (BNXT_CHIP_P5(bp))
6682 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6683 			else
6684 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6685 		}
6686 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6687 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6688 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6689 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6690 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6691 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6692 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6693 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6694 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6695 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6696 	}
6697 	hwrm_req_drop(bp, req);
6698 	return rc;
6699 }
6700 
6701 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6702 {
6703 	struct hwrm_ring_grp_alloc_output *resp;
6704 	struct hwrm_ring_grp_alloc_input *req;
6705 	int rc;
6706 	u16 i;
6707 
6708 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6709 		return 0;
6710 
6711 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6712 	if (rc)
6713 		return rc;
6714 
6715 	resp = hwrm_req_hold(bp, req);
6716 	for (i = 0; i < bp->rx_nr_rings; i++) {
6717 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
6718 
6719 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
6720 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
6721 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
6722 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
6723 
6724 		rc = hwrm_req_send(bp, req);
6725 
6726 		if (rc)
6727 			break;
6728 
6729 		bp->grp_info[grp_idx].fw_grp_id =
6730 			le32_to_cpu(resp->ring_group_id);
6731 	}
6732 	hwrm_req_drop(bp, req);
6733 	return rc;
6734 }
6735 
6736 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
6737 {
6738 	struct hwrm_ring_grp_free_input *req;
6739 	u16 i;
6740 
6741 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
6742 		return;
6743 
6744 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
6745 		return;
6746 
6747 	hwrm_req_hold(bp, req);
6748 	for (i = 0; i < bp->cp_nr_rings; i++) {
6749 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
6750 			continue;
6751 		req->ring_group_id =
6752 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
6753 
6754 		hwrm_req_send(bp, req);
6755 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
6756 	}
6757 	hwrm_req_drop(bp, req);
6758 }
6759 
6760 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
6761 				    struct bnxt_ring_struct *ring,
6762 				    u32 ring_type, u32 map_index)
6763 {
6764 	struct hwrm_ring_alloc_output *resp;
6765 	struct hwrm_ring_alloc_input *req;
6766 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
6767 	struct bnxt_ring_grp_info *grp_info;
6768 	int rc, err = 0;
6769 	u16 ring_id;
6770 
6771 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
6772 	if (rc)
6773 		goto exit;
6774 
6775 	req->enables = 0;
6776 	if (rmem->nr_pages > 1) {
6777 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
6778 		/* Page size is in log2 units */
6779 		req->page_size = BNXT_PAGE_SHIFT;
6780 		req->page_tbl_depth = 1;
6781 	} else {
6782 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
6783 	}
6784 	req->fbo = 0;
6785 	/* Association of ring index with doorbell index and MSIX number */
6786 	req->logical_id = cpu_to_le16(map_index);
6787 
6788 	switch (ring_type) {
6789 	case HWRM_RING_ALLOC_TX: {
6790 		struct bnxt_tx_ring_info *txr;
6791 
6792 		txr = container_of(ring, struct bnxt_tx_ring_info,
6793 				   tx_ring_struct);
6794 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
6795 		/* Association of transmit ring with completion ring */
6796 		grp_info = &bp->grp_info[ring->grp_idx];
6797 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
6798 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
6799 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6800 		req->queue_id = cpu_to_le16(ring->queue_id);
6801 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
6802 			req->cmpl_coal_cnt =
6803 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
6804 		break;
6805 	}
6806 	case HWRM_RING_ALLOC_RX:
6807 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6808 		req->length = cpu_to_le32(bp->rx_ring_mask + 1);
6809 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6810 			u16 flags = 0;
6811 
6812 			/* Association of rx ring with stats context */
6813 			grp_info = &bp->grp_info[ring->grp_idx];
6814 			req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
6815 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6816 			req->enables |= cpu_to_le32(
6817 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6818 			if (NET_IP_ALIGN == 2)
6819 				flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
6820 			req->flags = cpu_to_le16(flags);
6821 		}
6822 		break;
6823 	case HWRM_RING_ALLOC_AGG:
6824 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6825 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
6826 			/* Association of agg ring with rx ring */
6827 			grp_info = &bp->grp_info[ring->grp_idx];
6828 			req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
6829 			req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
6830 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6831 			req->enables |= cpu_to_le32(
6832 				RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
6833 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6834 		} else {
6835 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6836 		}
6837 		req->length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
6838 		break;
6839 	case HWRM_RING_ALLOC_CMPL:
6840 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
6841 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
6842 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6843 			/* Association of cp ring with nq */
6844 			grp_info = &bp->grp_info[map_index];
6845 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
6846 			req->cq_handle = cpu_to_le64(ring->handle);
6847 			req->enables |= cpu_to_le32(
6848 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
6849 		} else if (bp->flags & BNXT_FLAG_USING_MSIX) {
6850 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
6851 		}
6852 		break;
6853 	case HWRM_RING_ALLOC_NQ:
6854 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
6855 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
6856 		if (bp->flags & BNXT_FLAG_USING_MSIX)
6857 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
6858 		break;
6859 	default:
6860 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
6861 			   ring_type);
6862 		return -1;
6863 	}
6864 
6865 	resp = hwrm_req_hold(bp, req);
6866 	rc = hwrm_req_send(bp, req);
6867 	err = le16_to_cpu(resp->error_code);
6868 	ring_id = le16_to_cpu(resp->ring_id);
6869 	hwrm_req_drop(bp, req);
6870 
6871 exit:
6872 	if (rc || err) {
6873 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
6874 			   ring_type, rc, err);
6875 		return -EIO;
6876 	}
6877 	ring->fw_ring_id = ring_id;
6878 	return rc;
6879 }
6880 
6881 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
6882 {
6883 	int rc;
6884 
6885 	if (BNXT_PF(bp)) {
6886 		struct hwrm_func_cfg_input *req;
6887 
6888 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
6889 		if (rc)
6890 			return rc;
6891 
6892 		req->fid = cpu_to_le16(0xffff);
6893 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
6894 		req->async_event_cr = cpu_to_le16(idx);
6895 		return hwrm_req_send(bp, req);
6896 	} else {
6897 		struct hwrm_func_vf_cfg_input *req;
6898 
6899 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
6900 		if (rc)
6901 			return rc;
6902 
6903 		req->enables =
6904 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
6905 		req->async_event_cr = cpu_to_le16(idx);
6906 		return hwrm_req_send(bp, req);
6907 	}
6908 }
6909 
6910 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
6911 			     u32 ring_type)
6912 {
6913 	switch (ring_type) {
6914 	case HWRM_RING_ALLOC_TX:
6915 		db->db_ring_mask = bp->tx_ring_mask;
6916 		break;
6917 	case HWRM_RING_ALLOC_RX:
6918 		db->db_ring_mask = bp->rx_ring_mask;
6919 		break;
6920 	case HWRM_RING_ALLOC_AGG:
6921 		db->db_ring_mask = bp->rx_agg_ring_mask;
6922 		break;
6923 	case HWRM_RING_ALLOC_CMPL:
6924 	case HWRM_RING_ALLOC_NQ:
6925 		db->db_ring_mask = bp->cp_ring_mask;
6926 		break;
6927 	}
6928 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
6929 		db->db_epoch_mask = db->db_ring_mask + 1;
6930 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
6931 	}
6932 }
6933 
6934 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
6935 			u32 map_idx, u32 xid)
6936 {
6937 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6938 		switch (ring_type) {
6939 		case HWRM_RING_ALLOC_TX:
6940 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
6941 			break;
6942 		case HWRM_RING_ALLOC_RX:
6943 		case HWRM_RING_ALLOC_AGG:
6944 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
6945 			break;
6946 		case HWRM_RING_ALLOC_CMPL:
6947 			db->db_key64 = DBR_PATH_L2;
6948 			break;
6949 		case HWRM_RING_ALLOC_NQ:
6950 			db->db_key64 = DBR_PATH_L2;
6951 			break;
6952 		}
6953 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
6954 
6955 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6956 			db->db_key64 |= DBR_VALID;
6957 
6958 		db->doorbell = bp->bar1 + bp->db_offset;
6959 	} else {
6960 		db->doorbell = bp->bar1 + map_idx * 0x80;
6961 		switch (ring_type) {
6962 		case HWRM_RING_ALLOC_TX:
6963 			db->db_key32 = DB_KEY_TX;
6964 			break;
6965 		case HWRM_RING_ALLOC_RX:
6966 		case HWRM_RING_ALLOC_AGG:
6967 			db->db_key32 = DB_KEY_RX;
6968 			break;
6969 		case HWRM_RING_ALLOC_CMPL:
6970 			db->db_key32 = DB_KEY_CP;
6971 			break;
6972 		}
6973 	}
6974 	bnxt_set_db_mask(bp, db, ring_type);
6975 }
6976 
6977 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
6978 				   struct bnxt_rx_ring_info *rxr)
6979 {
6980 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
6981 	struct bnxt_napi *bnapi = rxr->bnapi;
6982 	u32 type = HWRM_RING_ALLOC_RX;
6983 	u32 map_idx = bnapi->index;
6984 	int rc;
6985 
6986 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6987 	if (rc)
6988 		return rc;
6989 
6990 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
6991 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
6992 
6993 	return 0;
6994 }
6995 
6996 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
6997 				       struct bnxt_rx_ring_info *rxr)
6998 {
6999 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7000 	u32 type = HWRM_RING_ALLOC_AGG;
7001 	u32 grp_idx = ring->grp_idx;
7002 	u32 map_idx;
7003 	int rc;
7004 
7005 	map_idx = grp_idx + bp->rx_nr_rings;
7006 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7007 	if (rc)
7008 		return rc;
7009 
7010 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7011 		    ring->fw_ring_id);
7012 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7013 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7014 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7015 
7016 	return 0;
7017 }
7018 
7019 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7020 {
7021 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7022 	int i, rc = 0;
7023 	u32 type;
7024 
7025 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7026 		type = HWRM_RING_ALLOC_NQ;
7027 	else
7028 		type = HWRM_RING_ALLOC_CMPL;
7029 	for (i = 0; i < bp->cp_nr_rings; i++) {
7030 		struct bnxt_napi *bnapi = bp->bnapi[i];
7031 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7032 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7033 		u32 map_idx = ring->map_idx;
7034 		unsigned int vector;
7035 
7036 		vector = bp->irq_tbl[map_idx].vector;
7037 		disable_irq_nosync(vector);
7038 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7039 		if (rc) {
7040 			enable_irq(vector);
7041 			goto err_out;
7042 		}
7043 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7044 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7045 		enable_irq(vector);
7046 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7047 
7048 		if (!i) {
7049 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7050 			if (rc)
7051 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7052 		}
7053 	}
7054 
7055 	type = HWRM_RING_ALLOC_TX;
7056 	for (i = 0; i < bp->tx_nr_rings; i++) {
7057 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7058 		struct bnxt_ring_struct *ring;
7059 		u32 map_idx;
7060 
7061 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7062 			struct bnxt_cp_ring_info *cpr2 = txr->tx_cpr;
7063 			struct bnxt_napi *bnapi = txr->bnapi;
7064 			u32 type2 = HWRM_RING_ALLOC_CMPL;
7065 
7066 			ring = &cpr2->cp_ring_struct;
7067 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
7068 			map_idx = bnapi->index;
7069 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
7070 			if (rc)
7071 				goto err_out;
7072 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
7073 				    ring->fw_ring_id);
7074 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
7075 		}
7076 		ring = &txr->tx_ring_struct;
7077 		map_idx = i;
7078 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7079 		if (rc)
7080 			goto err_out;
7081 		bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
7082 	}
7083 
7084 	for (i = 0; i < bp->rx_nr_rings; i++) {
7085 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7086 
7087 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7088 		if (rc)
7089 			goto err_out;
7090 		/* If we have agg rings, post agg buffers first. */
7091 		if (!agg_rings)
7092 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7093 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7094 			struct bnxt_cp_ring_info *cpr2 = rxr->rx_cpr;
7095 			struct bnxt_napi *bnapi = rxr->bnapi;
7096 			u32 type2 = HWRM_RING_ALLOC_CMPL;
7097 			struct bnxt_ring_struct *ring;
7098 			u32 map_idx = bnapi->index;
7099 
7100 			ring = &cpr2->cp_ring_struct;
7101 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
7102 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
7103 			if (rc)
7104 				goto err_out;
7105 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
7106 				    ring->fw_ring_id);
7107 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
7108 		}
7109 	}
7110 
7111 	if (agg_rings) {
7112 		for (i = 0; i < bp->rx_nr_rings; i++) {
7113 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7114 			if (rc)
7115 				goto err_out;
7116 		}
7117 	}
7118 err_out:
7119 	return rc;
7120 }
7121 
7122 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7123 				   struct bnxt_ring_struct *ring,
7124 				   u32 ring_type, int cmpl_ring_id)
7125 {
7126 	struct hwrm_ring_free_output *resp;
7127 	struct hwrm_ring_free_input *req;
7128 	u16 error_code = 0;
7129 	int rc;
7130 
7131 	if (BNXT_NO_FW_ACCESS(bp))
7132 		return 0;
7133 
7134 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7135 	if (rc)
7136 		goto exit;
7137 
7138 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7139 	req->ring_type = ring_type;
7140 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7141 
7142 	resp = hwrm_req_hold(bp, req);
7143 	rc = hwrm_req_send(bp, req);
7144 	error_code = le16_to_cpu(resp->error_code);
7145 	hwrm_req_drop(bp, req);
7146 exit:
7147 	if (rc || error_code) {
7148 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7149 			   ring_type, rc, error_code);
7150 		return -EIO;
7151 	}
7152 	return 0;
7153 }
7154 
7155 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7156 				   struct bnxt_rx_ring_info *rxr,
7157 				   bool close_path)
7158 {
7159 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7160 	u32 grp_idx = rxr->bnapi->index;
7161 	u32 cmpl_ring_id;
7162 
7163 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7164 		return;
7165 
7166 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7167 	hwrm_ring_free_send_msg(bp, ring,
7168 				RING_FREE_REQ_RING_TYPE_RX,
7169 				close_path ? cmpl_ring_id :
7170 				INVALID_HW_RING_ID);
7171 	ring->fw_ring_id = INVALID_HW_RING_ID;
7172 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7173 }
7174 
7175 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7176 				       struct bnxt_rx_ring_info *rxr,
7177 				       bool close_path)
7178 {
7179 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7180 	u32 grp_idx = rxr->bnapi->index;
7181 	u32 type, cmpl_ring_id;
7182 
7183 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7184 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7185 	else
7186 		type = RING_FREE_REQ_RING_TYPE_RX;
7187 
7188 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7189 		return;
7190 
7191 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7192 	hwrm_ring_free_send_msg(bp, ring, type,
7193 				close_path ? cmpl_ring_id :
7194 				INVALID_HW_RING_ID);
7195 	ring->fw_ring_id = INVALID_HW_RING_ID;
7196 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7197 }
7198 
7199 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7200 {
7201 	u32 type;
7202 	int i;
7203 
7204 	if (!bp->bnapi)
7205 		return;
7206 
7207 	for (i = 0; i < bp->tx_nr_rings; i++) {
7208 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7209 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7210 
7211 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7212 			u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
7213 
7214 			hwrm_ring_free_send_msg(bp, ring,
7215 						RING_FREE_REQ_RING_TYPE_TX,
7216 						close_path ? cmpl_ring_id :
7217 						INVALID_HW_RING_ID);
7218 			ring->fw_ring_id = INVALID_HW_RING_ID;
7219 		}
7220 	}
7221 
7222 	for (i = 0; i < bp->rx_nr_rings; i++) {
7223 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7224 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7225 	}
7226 
7227 	/* The completion rings are about to be freed.  After that the
7228 	 * IRQ doorbell will not work anymore.  So we need to disable
7229 	 * IRQ here.
7230 	 */
7231 	bnxt_disable_int_sync(bp);
7232 
7233 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7234 		type = RING_FREE_REQ_RING_TYPE_NQ;
7235 	else
7236 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7237 	for (i = 0; i < bp->cp_nr_rings; i++) {
7238 		struct bnxt_napi *bnapi = bp->bnapi[i];
7239 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7240 		struct bnxt_ring_struct *ring;
7241 		int j;
7242 
7243 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++) {
7244 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
7245 
7246 			ring = &cpr2->cp_ring_struct;
7247 			if (ring->fw_ring_id == INVALID_HW_RING_ID)
7248 				continue;
7249 			hwrm_ring_free_send_msg(bp, ring,
7250 						RING_FREE_REQ_RING_TYPE_L2_CMPL,
7251 						INVALID_HW_RING_ID);
7252 			ring->fw_ring_id = INVALID_HW_RING_ID;
7253 		}
7254 		ring = &cpr->cp_ring_struct;
7255 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7256 			hwrm_ring_free_send_msg(bp, ring, type,
7257 						INVALID_HW_RING_ID);
7258 			ring->fw_ring_id = INVALID_HW_RING_ID;
7259 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7260 		}
7261 	}
7262 }
7263 
7264 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7265 			     bool shared);
7266 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7267 			   bool shared);
7268 
7269 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7270 {
7271 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7272 	struct hwrm_func_qcfg_output *resp;
7273 	struct hwrm_func_qcfg_input *req;
7274 	int rc;
7275 
7276 	if (bp->hwrm_spec_code < 0x10601)
7277 		return 0;
7278 
7279 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7280 	if (rc)
7281 		return rc;
7282 
7283 	req->fid = cpu_to_le16(0xffff);
7284 	resp = hwrm_req_hold(bp, req);
7285 	rc = hwrm_req_send(bp, req);
7286 	if (rc) {
7287 		hwrm_req_drop(bp, req);
7288 		return rc;
7289 	}
7290 
7291 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7292 	if (BNXT_NEW_RM(bp)) {
7293 		u16 cp, stats;
7294 
7295 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7296 		hw_resc->resv_hw_ring_grps =
7297 			le32_to_cpu(resp->alloc_hw_ring_grps);
7298 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7299 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7300 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7301 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7302 		hw_resc->resv_irqs = cp;
7303 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7304 			int rx = hw_resc->resv_rx_rings;
7305 			int tx = hw_resc->resv_tx_rings;
7306 
7307 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7308 				rx >>= 1;
7309 			if (cp < (rx + tx)) {
7310 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7311 				if (rc)
7312 					goto get_rings_exit;
7313 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7314 					rx <<= 1;
7315 				hw_resc->resv_rx_rings = rx;
7316 				hw_resc->resv_tx_rings = tx;
7317 			}
7318 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7319 			hw_resc->resv_hw_ring_grps = rx;
7320 		}
7321 		hw_resc->resv_cp_rings = cp;
7322 		hw_resc->resv_stat_ctxs = stats;
7323 	}
7324 get_rings_exit:
7325 	hwrm_req_drop(bp, req);
7326 	return rc;
7327 }
7328 
7329 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7330 {
7331 	struct hwrm_func_qcfg_output *resp;
7332 	struct hwrm_func_qcfg_input *req;
7333 	int rc;
7334 
7335 	if (bp->hwrm_spec_code < 0x10601)
7336 		return 0;
7337 
7338 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7339 	if (rc)
7340 		return rc;
7341 
7342 	req->fid = cpu_to_le16(fid);
7343 	resp = hwrm_req_hold(bp, req);
7344 	rc = hwrm_req_send(bp, req);
7345 	if (!rc)
7346 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7347 
7348 	hwrm_req_drop(bp, req);
7349 	return rc;
7350 }
7351 
7352 static bool bnxt_rfs_supported(struct bnxt *bp);
7353 
7354 static struct hwrm_func_cfg_input *
7355 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7356 {
7357 	struct hwrm_func_cfg_input *req;
7358 	u32 enables = 0;
7359 
7360 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7361 		return NULL;
7362 
7363 	req->fid = cpu_to_le16(0xffff);
7364 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7365 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7366 	if (BNXT_NEW_RM(bp)) {
7367 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7368 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7369 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7370 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7371 			enables |= hwr->cp_p5 ?
7372 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7373 		} else {
7374 			enables |= hwr->cp ?
7375 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7376 			enables |= hwr->grp ?
7377 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7378 		}
7379 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7380 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7381 					  0;
7382 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7383 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7384 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7385 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7386 			req->num_msix = cpu_to_le16(hwr->cp);
7387 		} else {
7388 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7389 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7390 		}
7391 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7392 		req->num_vnics = cpu_to_le16(hwr->vnic);
7393 	}
7394 	req->enables = cpu_to_le32(enables);
7395 	return req;
7396 }
7397 
7398 static struct hwrm_func_vf_cfg_input *
7399 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7400 {
7401 	struct hwrm_func_vf_cfg_input *req;
7402 	u32 enables = 0;
7403 
7404 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7405 		return NULL;
7406 
7407 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7408 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7409 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7410 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7411 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7412 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7413 		enables |= hwr->cp_p5 ?
7414 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7415 	} else {
7416 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7417 		enables |= hwr->grp ?
7418 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7419 	}
7420 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7421 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7422 
7423 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7424 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7425 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7426 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7427 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7428 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7429 	} else {
7430 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7431 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7432 	}
7433 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7434 	req->num_vnics = cpu_to_le16(hwr->vnic);
7435 
7436 	req->enables = cpu_to_le32(enables);
7437 	return req;
7438 }
7439 
7440 static int
7441 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7442 {
7443 	struct hwrm_func_cfg_input *req;
7444 	int rc;
7445 
7446 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7447 	if (!req)
7448 		return -ENOMEM;
7449 
7450 	if (!req->enables) {
7451 		hwrm_req_drop(bp, req);
7452 		return 0;
7453 	}
7454 
7455 	rc = hwrm_req_send(bp, req);
7456 	if (rc)
7457 		return rc;
7458 
7459 	if (bp->hwrm_spec_code < 0x10601)
7460 		bp->hw_resc.resv_tx_rings = hwr->tx;
7461 
7462 	return bnxt_hwrm_get_rings(bp);
7463 }
7464 
7465 static int
7466 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7467 {
7468 	struct hwrm_func_vf_cfg_input *req;
7469 	int rc;
7470 
7471 	if (!BNXT_NEW_RM(bp)) {
7472 		bp->hw_resc.resv_tx_rings = hwr->tx;
7473 		return 0;
7474 	}
7475 
7476 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7477 	if (!req)
7478 		return -ENOMEM;
7479 
7480 	rc = hwrm_req_send(bp, req);
7481 	if (rc)
7482 		return rc;
7483 
7484 	return bnxt_hwrm_get_rings(bp);
7485 }
7486 
7487 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7488 {
7489 	if (BNXT_PF(bp))
7490 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7491 	else
7492 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7493 }
7494 
7495 int bnxt_nq_rings_in_use(struct bnxt *bp)
7496 {
7497 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7498 }
7499 
7500 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7501 {
7502 	int cp;
7503 
7504 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7505 		return bnxt_nq_rings_in_use(bp);
7506 
7507 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7508 	return cp;
7509 }
7510 
7511 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7512 {
7513 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7514 }
7515 
7516 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7517 {
7518 	if (!hwr->grp)
7519 		return 0;
7520 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7521 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7522 
7523 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7524 			rss_ctx *= hwr->vnic;
7525 		return rss_ctx;
7526 	}
7527 	if (BNXT_VF(bp))
7528 		return BNXT_VF_MAX_RSS_CTX;
7529 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7530 		return hwr->grp + 1;
7531 	return 1;
7532 }
7533 
7534 /* Check if a default RSS map needs to be setup.  This function is only
7535  * used on older firmware that does not require reserving RX rings.
7536  */
7537 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7538 {
7539 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7540 
7541 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7542 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7543 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7544 		if (!netif_is_rxfh_configured(bp->dev))
7545 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7546 	}
7547 }
7548 
7549 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7550 {
7551 	if (bp->flags & BNXT_FLAG_RFS) {
7552 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7553 			return 2 + bp->num_rss_ctx;
7554 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7555 			return rx_rings + 1;
7556 	}
7557 	return 1;
7558 }
7559 
7560 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7561 {
7562 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7563 	int cp = bnxt_cp_rings_in_use(bp);
7564 	int nq = bnxt_nq_rings_in_use(bp);
7565 	int rx = bp->rx_nr_rings, stat;
7566 	int vnic, grp = rx;
7567 
7568 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7569 	    bp->hwrm_spec_code >= 0x10601)
7570 		return true;
7571 
7572 	/* Old firmware does not need RX ring reservations but we still
7573 	 * need to setup a default RSS map when needed.  With new firmware
7574 	 * we go through RX ring reservations first and then set up the
7575 	 * RSS map for the successfully reserved RX rings when needed.
7576 	 */
7577 	if (!BNXT_NEW_RM(bp)) {
7578 		bnxt_check_rss_tbl_no_rmgr(bp);
7579 		return false;
7580 	}
7581 
7582 	vnic = bnxt_get_total_vnics(bp, rx);
7583 
7584 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7585 		rx <<= 1;
7586 	stat = bnxt_get_func_stat_ctxs(bp);
7587 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7588 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7589 	    (hw_resc->resv_hw_ring_grps != grp &&
7590 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7591 		return true;
7592 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7593 	    hw_resc->resv_irqs != nq)
7594 		return true;
7595 	return false;
7596 }
7597 
7598 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7599 {
7600 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7601 
7602 	hwr->tx = hw_resc->resv_tx_rings;
7603 	if (BNXT_NEW_RM(bp)) {
7604 		hwr->rx = hw_resc->resv_rx_rings;
7605 		hwr->cp = hw_resc->resv_irqs;
7606 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7607 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7608 		hwr->grp = hw_resc->resv_hw_ring_grps;
7609 		hwr->vnic = hw_resc->resv_vnics;
7610 		hwr->stat = hw_resc->resv_stat_ctxs;
7611 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7612 	}
7613 }
7614 
7615 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7616 {
7617 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7618 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7619 }
7620 
7621 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
7622 
7623 static int __bnxt_reserve_rings(struct bnxt *bp)
7624 {
7625 	struct bnxt_hw_rings hwr = {0};
7626 	int cp = bp->cp_nr_rings;
7627 	int rx_rings, rc;
7628 	int ulp_msix = 0;
7629 	bool sh = false;
7630 	int tx_cp;
7631 
7632 	if (!bnxt_need_reserve_rings(bp))
7633 		return 0;
7634 
7635 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
7636 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
7637 		if (!ulp_msix)
7638 			bnxt_set_ulp_stat_ctxs(bp, 0);
7639 
7640 		if (ulp_msix > bp->ulp_num_msix_want)
7641 			ulp_msix = bp->ulp_num_msix_want;
7642 		hwr.cp = cp + ulp_msix;
7643 	} else {
7644 		hwr.cp = bnxt_nq_rings_in_use(bp);
7645 	}
7646 
7647 	hwr.tx = bp->tx_nr_rings;
7648 	hwr.rx = bp->rx_nr_rings;
7649 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7650 		sh = true;
7651 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7652 		hwr.cp_p5 = hwr.rx + hwr.tx;
7653 
7654 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7655 
7656 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7657 		hwr.rx <<= 1;
7658 	hwr.grp = bp->rx_nr_rings;
7659 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7660 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
7661 
7662 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
7663 	if (rc)
7664 		return rc;
7665 
7666 	bnxt_copy_reserved_rings(bp, &hwr);
7667 
7668 	rx_rings = hwr.rx;
7669 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7670 		if (hwr.rx >= 2) {
7671 			rx_rings = hwr.rx >> 1;
7672 		} else {
7673 			if (netif_running(bp->dev))
7674 				return -ENOMEM;
7675 
7676 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7677 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7678 			bp->dev->hw_features &= ~NETIF_F_LRO;
7679 			bp->dev->features &= ~NETIF_F_LRO;
7680 			bnxt_set_ring_params(bp);
7681 		}
7682 	}
7683 	rx_rings = min_t(int, rx_rings, hwr.grp);
7684 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
7685 	if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
7686 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
7687 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
7688 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
7689 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7690 		hwr.rx = rx_rings << 1;
7691 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
7692 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
7693 	bp->tx_nr_rings = hwr.tx;
7694 
7695 	/* If we cannot reserve all the RX rings, reset the RSS map only
7696 	 * if absolutely necessary
7697 	 */
7698 	if (rx_rings != bp->rx_nr_rings) {
7699 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
7700 			    rx_rings, bp->rx_nr_rings);
7701 		if (netif_is_rxfh_configured(bp->dev) &&
7702 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
7703 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
7704 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
7705 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
7706 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
7707 		}
7708 	}
7709 	bp->rx_nr_rings = rx_rings;
7710 	bp->cp_nr_rings = hwr.cp;
7711 
7712 	if (!bnxt_rings_ok(bp, &hwr))
7713 		return -ENOMEM;
7714 
7715 	if (!netif_is_rxfh_configured(bp->dev))
7716 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7717 
7718 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
7719 		int resv_msix, resv_ctx, ulp_ctxs;
7720 		struct bnxt_hw_resc *hw_resc;
7721 
7722 		hw_resc = &bp->hw_resc;
7723 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
7724 		ulp_msix = min_t(int, resv_msix, ulp_msix);
7725 		bnxt_set_ulp_msix_num(bp, ulp_msix);
7726 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
7727 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
7728 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
7729 	}
7730 
7731 	return rc;
7732 }
7733 
7734 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7735 {
7736 	struct hwrm_func_vf_cfg_input *req;
7737 	u32 flags;
7738 
7739 	if (!BNXT_NEW_RM(bp))
7740 		return 0;
7741 
7742 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7743 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
7744 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7745 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7746 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7747 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
7748 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
7749 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7750 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7751 
7752 	req->flags = cpu_to_le32(flags);
7753 	return hwrm_req_send_silent(bp, req);
7754 }
7755 
7756 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7757 {
7758 	struct hwrm_func_cfg_input *req;
7759 	u32 flags;
7760 
7761 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7762 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
7763 	if (BNXT_NEW_RM(bp)) {
7764 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7765 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7766 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7767 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
7768 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7769 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
7770 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
7771 		else
7772 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7773 	}
7774 
7775 	req->flags = cpu_to_le32(flags);
7776 	return hwrm_req_send_silent(bp, req);
7777 }
7778 
7779 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7780 {
7781 	if (bp->hwrm_spec_code < 0x10801)
7782 		return 0;
7783 
7784 	if (BNXT_PF(bp))
7785 		return bnxt_hwrm_check_pf_rings(bp, hwr);
7786 
7787 	return bnxt_hwrm_check_vf_rings(bp, hwr);
7788 }
7789 
7790 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
7791 {
7792 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7793 	struct hwrm_ring_aggint_qcaps_output *resp;
7794 	struct hwrm_ring_aggint_qcaps_input *req;
7795 	int rc;
7796 
7797 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
7798 	coal_cap->num_cmpl_dma_aggr_max = 63;
7799 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
7800 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
7801 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
7802 	coal_cap->int_lat_tmr_min_max = 65535;
7803 	coal_cap->int_lat_tmr_max_max = 65535;
7804 	coal_cap->num_cmpl_aggr_int_max = 65535;
7805 	coal_cap->timer_units = 80;
7806 
7807 	if (bp->hwrm_spec_code < 0x10902)
7808 		return;
7809 
7810 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
7811 		return;
7812 
7813 	resp = hwrm_req_hold(bp, req);
7814 	rc = hwrm_req_send_silent(bp, req);
7815 	if (!rc) {
7816 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
7817 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
7818 		coal_cap->num_cmpl_dma_aggr_max =
7819 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
7820 		coal_cap->num_cmpl_dma_aggr_during_int_max =
7821 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
7822 		coal_cap->cmpl_aggr_dma_tmr_max =
7823 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
7824 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
7825 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
7826 		coal_cap->int_lat_tmr_min_max =
7827 			le16_to_cpu(resp->int_lat_tmr_min_max);
7828 		coal_cap->int_lat_tmr_max_max =
7829 			le16_to_cpu(resp->int_lat_tmr_max_max);
7830 		coal_cap->num_cmpl_aggr_int_max =
7831 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
7832 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
7833 	}
7834 	hwrm_req_drop(bp, req);
7835 }
7836 
7837 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
7838 {
7839 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7840 
7841 	return usec * 1000 / coal_cap->timer_units;
7842 }
7843 
7844 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
7845 	struct bnxt_coal *hw_coal,
7846 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7847 {
7848 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7849 	u16 val, tmr, max, flags = hw_coal->flags;
7850 	u32 cmpl_params = coal_cap->cmpl_params;
7851 
7852 	max = hw_coal->bufs_per_record * 128;
7853 	if (hw_coal->budget)
7854 		max = hw_coal->bufs_per_record * hw_coal->budget;
7855 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
7856 
7857 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
7858 	req->num_cmpl_aggr_int = cpu_to_le16(val);
7859 
7860 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
7861 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
7862 
7863 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
7864 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
7865 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
7866 
7867 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
7868 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
7869 	req->int_lat_tmr_max = cpu_to_le16(tmr);
7870 
7871 	/* min timer set to 1/2 of interrupt timer */
7872 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
7873 		val = tmr / 2;
7874 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
7875 		req->int_lat_tmr_min = cpu_to_le16(val);
7876 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
7877 	}
7878 
7879 	/* buf timer set to 1/4 of interrupt timer */
7880 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
7881 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
7882 
7883 	if (cmpl_params &
7884 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
7885 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
7886 		val = clamp_t(u16, tmr, 1,
7887 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
7888 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
7889 		req->enables |=
7890 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
7891 	}
7892 
7893 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
7894 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
7895 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
7896 	req->flags = cpu_to_le16(flags);
7897 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
7898 }
7899 
7900 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
7901 				   struct bnxt_coal *hw_coal)
7902 {
7903 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
7904 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7905 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7906 	u32 nq_params = coal_cap->nq_params;
7907 	u16 tmr;
7908 	int rc;
7909 
7910 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
7911 		return 0;
7912 
7913 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7914 	if (rc)
7915 		return rc;
7916 
7917 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
7918 	req->flags =
7919 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
7920 
7921 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
7922 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
7923 	req->int_lat_tmr_min = cpu_to_le16(tmr);
7924 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
7925 	return hwrm_req_send(bp, req);
7926 }
7927 
7928 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
7929 {
7930 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
7931 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7932 	struct bnxt_coal coal;
7933 	int rc;
7934 
7935 	/* Tick values in micro seconds.
7936 	 * 1 coal_buf x bufs_per_record = 1 completion record.
7937 	 */
7938 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
7939 
7940 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
7941 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
7942 
7943 	if (!bnapi->rx_ring)
7944 		return -ENODEV;
7945 
7946 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7947 	if (rc)
7948 		return rc;
7949 
7950 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
7951 
7952 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
7953 
7954 	return hwrm_req_send(bp, req_rx);
7955 }
7956 
7957 static int
7958 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
7959 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7960 {
7961 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
7962 
7963 	req->ring_id = cpu_to_le16(ring_id);
7964 	return hwrm_req_send(bp, req);
7965 }
7966 
7967 static int
7968 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
7969 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7970 {
7971 	struct bnxt_tx_ring_info *txr;
7972 	int i, rc;
7973 
7974 	bnxt_for_each_napi_tx(i, bnapi, txr) {
7975 		u16 ring_id;
7976 
7977 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
7978 		req->ring_id = cpu_to_le16(ring_id);
7979 		rc = hwrm_req_send(bp, req);
7980 		if (rc)
7981 			return rc;
7982 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7983 			return 0;
7984 	}
7985 	return 0;
7986 }
7987 
7988 int bnxt_hwrm_set_coal(struct bnxt *bp)
7989 {
7990 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
7991 	int i, rc;
7992 
7993 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7994 	if (rc)
7995 		return rc;
7996 
7997 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7998 	if (rc) {
7999 		hwrm_req_drop(bp, req_rx);
8000 		return rc;
8001 	}
8002 
8003 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8004 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8005 
8006 	hwrm_req_hold(bp, req_rx);
8007 	hwrm_req_hold(bp, req_tx);
8008 	for (i = 0; i < bp->cp_nr_rings; i++) {
8009 		struct bnxt_napi *bnapi = bp->bnapi[i];
8010 		struct bnxt_coal *hw_coal;
8011 
8012 		if (!bnapi->rx_ring)
8013 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8014 		else
8015 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8016 		if (rc)
8017 			break;
8018 
8019 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8020 			continue;
8021 
8022 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8023 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8024 			if (rc)
8025 				break;
8026 		}
8027 		if (bnapi->rx_ring)
8028 			hw_coal = &bp->rx_coal;
8029 		else
8030 			hw_coal = &bp->tx_coal;
8031 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8032 	}
8033 	hwrm_req_drop(bp, req_rx);
8034 	hwrm_req_drop(bp, req_tx);
8035 	return rc;
8036 }
8037 
8038 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8039 {
8040 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8041 	struct hwrm_stat_ctx_free_input *req;
8042 	int i;
8043 
8044 	if (!bp->bnapi)
8045 		return;
8046 
8047 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8048 		return;
8049 
8050 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8051 		return;
8052 	if (BNXT_FW_MAJ(bp) <= 20) {
8053 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8054 			hwrm_req_drop(bp, req);
8055 			return;
8056 		}
8057 		hwrm_req_hold(bp, req0);
8058 	}
8059 	hwrm_req_hold(bp, req);
8060 	for (i = 0; i < bp->cp_nr_rings; i++) {
8061 		struct bnxt_napi *bnapi = bp->bnapi[i];
8062 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8063 
8064 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8065 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8066 			if (req0) {
8067 				req0->stat_ctx_id = req->stat_ctx_id;
8068 				hwrm_req_send(bp, req0);
8069 			}
8070 			hwrm_req_send(bp, req);
8071 
8072 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8073 		}
8074 	}
8075 	hwrm_req_drop(bp, req);
8076 	if (req0)
8077 		hwrm_req_drop(bp, req0);
8078 }
8079 
8080 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8081 {
8082 	struct hwrm_stat_ctx_alloc_output *resp;
8083 	struct hwrm_stat_ctx_alloc_input *req;
8084 	int rc, i;
8085 
8086 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8087 		return 0;
8088 
8089 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8090 	if (rc)
8091 		return rc;
8092 
8093 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8094 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8095 
8096 	resp = hwrm_req_hold(bp, req);
8097 	for (i = 0; i < bp->cp_nr_rings; i++) {
8098 		struct bnxt_napi *bnapi = bp->bnapi[i];
8099 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8100 
8101 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8102 
8103 		rc = hwrm_req_send(bp, req);
8104 		if (rc)
8105 			break;
8106 
8107 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8108 
8109 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8110 	}
8111 	hwrm_req_drop(bp, req);
8112 	return rc;
8113 }
8114 
8115 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8116 {
8117 	struct hwrm_func_qcfg_output *resp;
8118 	struct hwrm_func_qcfg_input *req;
8119 	u16 flags;
8120 	int rc;
8121 
8122 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8123 	if (rc)
8124 		return rc;
8125 
8126 	req->fid = cpu_to_le16(0xffff);
8127 	resp = hwrm_req_hold(bp, req);
8128 	rc = hwrm_req_send(bp, req);
8129 	if (rc)
8130 		goto func_qcfg_exit;
8131 
8132 #ifdef CONFIG_BNXT_SRIOV
8133 	if (BNXT_VF(bp)) {
8134 		struct bnxt_vf_info *vf = &bp->vf;
8135 
8136 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8137 	} else {
8138 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8139 	}
8140 #endif
8141 	flags = le16_to_cpu(resp->flags);
8142 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8143 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8144 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8145 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8146 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8147 	}
8148 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8149 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8150 
8151 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8152 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8153 
8154 	switch (resp->port_partition_type) {
8155 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8156 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8157 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8158 		bp->port_partition_type = resp->port_partition_type;
8159 		break;
8160 	}
8161 	if (bp->hwrm_spec_code < 0x10707 ||
8162 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8163 		bp->br_mode = BRIDGE_MODE_VEB;
8164 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8165 		bp->br_mode = BRIDGE_MODE_VEPA;
8166 	else
8167 		bp->br_mode = BRIDGE_MODE_UNDEF;
8168 
8169 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8170 	if (!bp->max_mtu)
8171 		bp->max_mtu = BNXT_MAX_MTU;
8172 
8173 	if (bp->db_size)
8174 		goto func_qcfg_exit;
8175 
8176 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8177 	if (BNXT_CHIP_P5(bp)) {
8178 		if (BNXT_PF(bp))
8179 			bp->db_offset = DB_PF_OFFSET_P5;
8180 		else
8181 			bp->db_offset = DB_VF_OFFSET_P5;
8182 	}
8183 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8184 				 1024);
8185 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8186 	    bp->db_size <= bp->db_offset)
8187 		bp->db_size = pci_resource_len(bp->pdev, 2);
8188 
8189 func_qcfg_exit:
8190 	hwrm_req_drop(bp, req);
8191 	return rc;
8192 }
8193 
8194 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8195 				      u8 init_val, u8 init_offset,
8196 				      bool init_mask_set)
8197 {
8198 	ctxm->init_value = init_val;
8199 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8200 	if (init_mask_set)
8201 		ctxm->init_offset = init_offset * 4;
8202 	else
8203 		ctxm->init_value = 0;
8204 }
8205 
8206 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8207 {
8208 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8209 	u16 type;
8210 
8211 	for (type = 0; type < ctx_max; type++) {
8212 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8213 		int n = 1;
8214 
8215 		if (!ctxm->max_entries)
8216 			continue;
8217 
8218 		if (ctxm->instance_bmap)
8219 			n = hweight32(ctxm->instance_bmap);
8220 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8221 		if (!ctxm->pg_info)
8222 			return -ENOMEM;
8223 	}
8224 	return 0;
8225 }
8226 
8227 #define BNXT_CTX_INIT_VALID(flags)	\
8228 	(!!((flags) &			\
8229 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8230 
8231 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8232 {
8233 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8234 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8235 	struct bnxt_ctx_mem_info *ctx;
8236 	u16 type;
8237 	int rc;
8238 
8239 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8240 	if (rc)
8241 		return rc;
8242 
8243 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8244 	if (!ctx)
8245 		return -ENOMEM;
8246 	bp->ctx = ctx;
8247 
8248 	resp = hwrm_req_hold(bp, req);
8249 
8250 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8251 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8252 		u8 init_val, init_off, i;
8253 		__le32 *p;
8254 		u32 flags;
8255 
8256 		req->type = cpu_to_le16(type);
8257 		rc = hwrm_req_send(bp, req);
8258 		if (rc)
8259 			goto ctx_done;
8260 		flags = le32_to_cpu(resp->flags);
8261 		type = le16_to_cpu(resp->next_valid_type);
8262 		if (!(flags & FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_TYPE_VALID))
8263 			continue;
8264 
8265 		ctxm->type = le16_to_cpu(resp->type);
8266 		ctxm->entry_size = le16_to_cpu(resp->entry_size);
8267 		ctxm->flags = flags;
8268 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8269 		ctxm->entry_multiple = resp->entry_multiple;
8270 		ctxm->max_entries = le32_to_cpu(resp->max_num_entries);
8271 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8272 		init_val = resp->ctx_init_value;
8273 		init_off = resp->ctx_init_offset;
8274 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8275 					  BNXT_CTX_INIT_VALID(flags));
8276 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8277 					      BNXT_MAX_SPLIT_ENTRY);
8278 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8279 		     i++, p++)
8280 			ctxm->split[i] = le32_to_cpu(*p);
8281 	}
8282 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8283 
8284 ctx_done:
8285 	hwrm_req_drop(bp, req);
8286 	return rc;
8287 }
8288 
8289 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8290 {
8291 	struct hwrm_func_backing_store_qcaps_output *resp;
8292 	struct hwrm_func_backing_store_qcaps_input *req;
8293 	int rc;
8294 
8295 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx)
8296 		return 0;
8297 
8298 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8299 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8300 
8301 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8302 	if (rc)
8303 		return rc;
8304 
8305 	resp = hwrm_req_hold(bp, req);
8306 	rc = hwrm_req_send_silent(bp, req);
8307 	if (!rc) {
8308 		struct bnxt_ctx_mem_type *ctxm;
8309 		struct bnxt_ctx_mem_info *ctx;
8310 		u8 init_val, init_idx = 0;
8311 		u16 init_mask;
8312 
8313 		ctx = bp->ctx;
8314 		if (!ctx) {
8315 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8316 			if (!ctx) {
8317 				rc = -ENOMEM;
8318 				goto ctx_err;
8319 			}
8320 			bp->ctx = ctx;
8321 		}
8322 		init_val = resp->ctx_kind_initializer;
8323 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8324 
8325 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8326 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8327 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8328 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8329 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8330 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8331 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8332 					  (init_mask & (1 << init_idx++)) != 0);
8333 
8334 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8335 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8336 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8337 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8338 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8339 					  (init_mask & (1 << init_idx++)) != 0);
8340 
8341 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8342 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8343 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8344 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8345 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8346 					  (init_mask & (1 << init_idx++)) != 0);
8347 
8348 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8349 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8350 		ctxm->max_entries = ctxm->vnic_entries +
8351 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8352 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8353 		bnxt_init_ctx_initializer(ctxm, init_val,
8354 					  resp->vnic_init_offset,
8355 					  (init_mask & (1 << init_idx++)) != 0);
8356 
8357 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8358 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8359 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8360 		bnxt_init_ctx_initializer(ctxm, init_val,
8361 					  resp->stat_init_offset,
8362 					  (init_mask & (1 << init_idx++)) != 0);
8363 
8364 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8365 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8366 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8367 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8368 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8369 		if (!ctxm->entry_multiple)
8370 			ctxm->entry_multiple = 1;
8371 
8372 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8373 
8374 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8375 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8376 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8377 		ctxm->mrav_num_entries_units =
8378 			le16_to_cpu(resp->mrav_num_entries_units);
8379 		bnxt_init_ctx_initializer(ctxm, init_val,
8380 					  resp->mrav_init_offset,
8381 					  (init_mask & (1 << init_idx++)) != 0);
8382 
8383 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8384 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8385 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8386 
8387 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8388 		if (!ctx->tqm_fp_rings_count)
8389 			ctx->tqm_fp_rings_count = bp->max_q;
8390 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8391 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8392 
8393 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8394 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8395 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8396 
8397 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8398 	} else {
8399 		rc = 0;
8400 	}
8401 ctx_err:
8402 	hwrm_req_drop(bp, req);
8403 	return rc;
8404 }
8405 
8406 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8407 				  __le64 *pg_dir)
8408 {
8409 	if (!rmem->nr_pages)
8410 		return;
8411 
8412 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8413 	if (rmem->depth >= 1) {
8414 		if (rmem->depth == 2)
8415 			*pg_attr |= 2;
8416 		else
8417 			*pg_attr |= 1;
8418 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8419 	} else {
8420 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8421 	}
8422 }
8423 
8424 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8425 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8426 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8427 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8428 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8429 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8430 
8431 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8432 {
8433 	struct hwrm_func_backing_store_cfg_input *req;
8434 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8435 	struct bnxt_ctx_pg_info *ctx_pg;
8436 	struct bnxt_ctx_mem_type *ctxm;
8437 	void **__req = (void **)&req;
8438 	u32 req_len = sizeof(*req);
8439 	__le32 *num_entries;
8440 	__le64 *pg_dir;
8441 	u32 flags = 0;
8442 	u8 *pg_attr;
8443 	u32 ena;
8444 	int rc;
8445 	int i;
8446 
8447 	if (!ctx)
8448 		return 0;
8449 
8450 	if (req_len > bp->hwrm_max_ext_req_len)
8451 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8452 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8453 	if (rc)
8454 		return rc;
8455 
8456 	req->enables = cpu_to_le32(enables);
8457 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8458 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8459 		ctx_pg = ctxm->pg_info;
8460 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8461 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8462 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8463 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8464 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8465 				      &req->qpc_pg_size_qpc_lvl,
8466 				      &req->qpc_page_dir);
8467 
8468 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8469 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8470 	}
8471 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8472 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8473 		ctx_pg = ctxm->pg_info;
8474 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8475 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8476 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8477 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8478 				      &req->srq_pg_size_srq_lvl,
8479 				      &req->srq_page_dir);
8480 	}
8481 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8482 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8483 		ctx_pg = ctxm->pg_info;
8484 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8485 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8486 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8487 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8488 				      &req->cq_pg_size_cq_lvl,
8489 				      &req->cq_page_dir);
8490 	}
8491 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8492 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8493 		ctx_pg = ctxm->pg_info;
8494 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8495 		req->vnic_num_ring_table_entries =
8496 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8497 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8498 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8499 				      &req->vnic_pg_size_vnic_lvl,
8500 				      &req->vnic_page_dir);
8501 	}
8502 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8503 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8504 		ctx_pg = ctxm->pg_info;
8505 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8506 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8507 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8508 				      &req->stat_pg_size_stat_lvl,
8509 				      &req->stat_page_dir);
8510 	}
8511 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8512 		u32 units;
8513 
8514 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8515 		ctx_pg = ctxm->pg_info;
8516 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8517 		units = ctxm->mrav_num_entries_units;
8518 		if (units) {
8519 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8520 			u32 entries;
8521 
8522 			num_mr = ctx_pg->entries - num_ah;
8523 			entries = ((num_mr / units) << 16) | (num_ah / units);
8524 			req->mrav_num_entries = cpu_to_le32(entries);
8525 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8526 		}
8527 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8528 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8529 				      &req->mrav_pg_size_mrav_lvl,
8530 				      &req->mrav_page_dir);
8531 	}
8532 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8533 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8534 		ctx_pg = ctxm->pg_info;
8535 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8536 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8537 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8538 				      &req->tim_pg_size_tim_lvl,
8539 				      &req->tim_page_dir);
8540 	}
8541 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8542 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8543 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8544 	     pg_dir = &req->tqm_sp_page_dir,
8545 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8546 	     ctx_pg = ctxm->pg_info;
8547 	     i < BNXT_MAX_TQM_RINGS;
8548 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8549 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8550 		if (!(enables & ena))
8551 			continue;
8552 
8553 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8554 		*num_entries = cpu_to_le32(ctx_pg->entries);
8555 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8556 	}
8557 	req->flags = cpu_to_le32(flags);
8558 	return hwrm_req_send(bp, req);
8559 }
8560 
8561 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8562 				  struct bnxt_ctx_pg_info *ctx_pg)
8563 {
8564 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8565 
8566 	rmem->page_size = BNXT_PAGE_SIZE;
8567 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8568 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8569 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8570 	if (rmem->depth >= 1)
8571 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8572 	return bnxt_alloc_ring(bp, rmem);
8573 }
8574 
8575 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8576 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8577 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8578 {
8579 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8580 	int rc;
8581 
8582 	if (!mem_size)
8583 		return -EINVAL;
8584 
8585 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8586 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8587 		ctx_pg->nr_pages = 0;
8588 		return -EINVAL;
8589 	}
8590 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8591 		int nr_tbls, i;
8592 
8593 		rmem->depth = 2;
8594 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8595 					     GFP_KERNEL);
8596 		if (!ctx_pg->ctx_pg_tbl)
8597 			return -ENOMEM;
8598 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8599 		rmem->nr_pages = nr_tbls;
8600 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8601 		if (rc)
8602 			return rc;
8603 		for (i = 0; i < nr_tbls; i++) {
8604 			struct bnxt_ctx_pg_info *pg_tbl;
8605 
8606 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8607 			if (!pg_tbl)
8608 				return -ENOMEM;
8609 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8610 			rmem = &pg_tbl->ring_mem;
8611 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8612 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8613 			rmem->depth = 1;
8614 			rmem->nr_pages = MAX_CTX_PAGES;
8615 			rmem->ctx_mem = ctxm;
8616 			if (i == (nr_tbls - 1)) {
8617 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8618 
8619 				if (rem)
8620 					rmem->nr_pages = rem;
8621 			}
8622 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8623 			if (rc)
8624 				break;
8625 		}
8626 	} else {
8627 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8628 		if (rmem->nr_pages > 1 || depth)
8629 			rmem->depth = 1;
8630 		rmem->ctx_mem = ctxm;
8631 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8632 	}
8633 	return rc;
8634 }
8635 
8636 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
8637 				  struct bnxt_ctx_pg_info *ctx_pg)
8638 {
8639 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8640 
8641 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
8642 	    ctx_pg->ctx_pg_tbl) {
8643 		int i, nr_tbls = rmem->nr_pages;
8644 
8645 		for (i = 0; i < nr_tbls; i++) {
8646 			struct bnxt_ctx_pg_info *pg_tbl;
8647 			struct bnxt_ring_mem_info *rmem2;
8648 
8649 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8650 			if (!pg_tbl)
8651 				continue;
8652 			rmem2 = &pg_tbl->ring_mem;
8653 			bnxt_free_ring(bp, rmem2);
8654 			ctx_pg->ctx_pg_arr[i] = NULL;
8655 			kfree(pg_tbl);
8656 			ctx_pg->ctx_pg_tbl[i] = NULL;
8657 		}
8658 		kfree(ctx_pg->ctx_pg_tbl);
8659 		ctx_pg->ctx_pg_tbl = NULL;
8660 	}
8661 	bnxt_free_ring(bp, rmem);
8662 	ctx_pg->nr_pages = 0;
8663 }
8664 
8665 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
8666 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
8667 				   u8 pg_lvl)
8668 {
8669 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8670 	int i, rc = 0, n = 1;
8671 	u32 mem_size;
8672 
8673 	if (!ctxm->entry_size || !ctx_pg)
8674 		return -EINVAL;
8675 	if (ctxm->instance_bmap)
8676 		n = hweight32(ctxm->instance_bmap);
8677 	if (ctxm->entry_multiple)
8678 		entries = roundup(entries, ctxm->entry_multiple);
8679 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
8680 	mem_size = entries * ctxm->entry_size;
8681 	for (i = 0; i < n && !rc; i++) {
8682 		ctx_pg[i].entries = entries;
8683 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
8684 					    ctxm->init_value ? ctxm : NULL);
8685 	}
8686 	return rc;
8687 }
8688 
8689 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
8690 					       struct bnxt_ctx_mem_type *ctxm,
8691 					       bool last)
8692 {
8693 	struct hwrm_func_backing_store_cfg_v2_input *req;
8694 	u32 instance_bmap = ctxm->instance_bmap;
8695 	int i, j, rc = 0, n = 1;
8696 	__le32 *p;
8697 
8698 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
8699 		return 0;
8700 
8701 	if (instance_bmap)
8702 		n = hweight32(ctxm->instance_bmap);
8703 	else
8704 		instance_bmap = 1;
8705 
8706 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
8707 	if (rc)
8708 		return rc;
8709 	hwrm_req_hold(bp, req);
8710 	req->type = cpu_to_le16(ctxm->type);
8711 	req->entry_size = cpu_to_le16(ctxm->entry_size);
8712 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
8713 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
8714 		p[i] = cpu_to_le32(ctxm->split[i]);
8715 	for (i = 0, j = 0; j < n && !rc; i++) {
8716 		struct bnxt_ctx_pg_info *ctx_pg;
8717 
8718 		if (!(instance_bmap & (1 << i)))
8719 			continue;
8720 		req->instance = cpu_to_le16(i);
8721 		ctx_pg = &ctxm->pg_info[j++];
8722 		if (!ctx_pg->entries)
8723 			continue;
8724 		req->num_entries = cpu_to_le32(ctx_pg->entries);
8725 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8726 				      &req->page_size_pbl_level,
8727 				      &req->page_dir);
8728 		if (last && j == n)
8729 			req->flags =
8730 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
8731 		rc = hwrm_req_send(bp, req);
8732 	}
8733 	hwrm_req_drop(bp, req);
8734 	return rc;
8735 }
8736 
8737 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
8738 {
8739 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8740 	struct bnxt_ctx_mem_type *ctxm;
8741 	u16 last_type;
8742 	int rc = 0;
8743 	u16 type;
8744 
8745 	if (!ena)
8746 		return 0;
8747 	else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
8748 		last_type = BNXT_CTX_MAX - 1;
8749 	else
8750 		last_type = BNXT_CTX_L2_MAX - 1;
8751 	ctx->ctx_arr[last_type].last = 1;
8752 
8753 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
8754 		ctxm = &ctx->ctx_arr[type];
8755 
8756 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
8757 		if (rc)
8758 			return rc;
8759 	}
8760 	return 0;
8761 }
8762 
8763 void bnxt_free_ctx_mem(struct bnxt *bp)
8764 {
8765 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8766 	u16 type;
8767 
8768 	if (!ctx)
8769 		return;
8770 
8771 	for (type = 0; type < BNXT_CTX_V2_MAX; type++) {
8772 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8773 		struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8774 		int i, n = 1;
8775 
8776 		if (!ctx_pg)
8777 			continue;
8778 		if (ctxm->instance_bmap)
8779 			n = hweight32(ctxm->instance_bmap);
8780 		for (i = 0; i < n; i++)
8781 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
8782 
8783 		kfree(ctx_pg);
8784 		ctxm->pg_info = NULL;
8785 	}
8786 
8787 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
8788 	kfree(ctx);
8789 	bp->ctx = NULL;
8790 }
8791 
8792 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
8793 {
8794 	struct bnxt_ctx_mem_type *ctxm;
8795 	struct bnxt_ctx_mem_info *ctx;
8796 	u32 l2_qps, qp1_qps, max_qps;
8797 	u32 ena, entries_sp, entries;
8798 	u32 srqs, max_srqs, min;
8799 	u32 num_mr, num_ah;
8800 	u32 extra_srqs = 0;
8801 	u32 extra_qps = 0;
8802 	u32 fast_qpmd_qps;
8803 	u8 pg_lvl = 1;
8804 	int i, rc;
8805 
8806 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
8807 	if (rc) {
8808 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
8809 			   rc);
8810 		return rc;
8811 	}
8812 	ctx = bp->ctx;
8813 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
8814 		return 0;
8815 
8816 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8817 	l2_qps = ctxm->qp_l2_entries;
8818 	qp1_qps = ctxm->qp_qp1_entries;
8819 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
8820 	max_qps = ctxm->max_entries;
8821 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8822 	srqs = ctxm->srq_l2_entries;
8823 	max_srqs = ctxm->max_entries;
8824 	ena = 0;
8825 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
8826 		pg_lvl = 2;
8827 		extra_qps = min_t(u32, 65536, max_qps - l2_qps - qp1_qps);
8828 		/* allocate extra qps if fw supports RoCE fast qp destroy feature */
8829 		extra_qps += fast_qpmd_qps;
8830 		extra_srqs = min_t(u32, 8192, max_srqs - srqs);
8831 		if (fast_qpmd_qps)
8832 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
8833 	}
8834 
8835 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8836 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
8837 				     pg_lvl);
8838 	if (rc)
8839 		return rc;
8840 
8841 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8842 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
8843 	if (rc)
8844 		return rc;
8845 
8846 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8847 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
8848 				     extra_qps * 2, pg_lvl);
8849 	if (rc)
8850 		return rc;
8851 
8852 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8853 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
8854 	if (rc)
8855 		return rc;
8856 
8857 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8858 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
8859 	if (rc)
8860 		return rc;
8861 
8862 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
8863 		goto skip_rdma;
8864 
8865 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8866 	/* 128K extra is needed to accommodate static AH context
8867 	 * allocation by f/w.
8868 	 */
8869 	num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
8870 	num_ah = min_t(u32, num_mr, 1024 * 128);
8871 	ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
8872 	if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
8873 		ctxm->mrav_av_entries = num_ah;
8874 
8875 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
8876 	if (rc)
8877 		return rc;
8878 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
8879 
8880 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8881 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
8882 	if (rc)
8883 		return rc;
8884 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
8885 
8886 skip_rdma:
8887 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8888 	min = ctxm->min_entries;
8889 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
8890 		     2 * (extra_qps + qp1_qps) + min;
8891 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
8892 	if (rc)
8893 		return rc;
8894 
8895 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8896 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
8897 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
8898 	if (rc)
8899 		return rc;
8900 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
8901 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
8902 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
8903 
8904 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8905 		rc = bnxt_backing_store_cfg_v2(bp, ena);
8906 	else
8907 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
8908 	if (rc) {
8909 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
8910 			   rc);
8911 		return rc;
8912 	}
8913 	ctx->flags |= BNXT_CTX_FLAG_INITED;
8914 	return 0;
8915 }
8916 
8917 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
8918 {
8919 	struct hwrm_func_resource_qcaps_output *resp;
8920 	struct hwrm_func_resource_qcaps_input *req;
8921 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8922 	int rc;
8923 
8924 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
8925 	if (rc)
8926 		return rc;
8927 
8928 	req->fid = cpu_to_le16(0xffff);
8929 	resp = hwrm_req_hold(bp, req);
8930 	rc = hwrm_req_send_silent(bp, req);
8931 	if (rc)
8932 		goto hwrm_func_resc_qcaps_exit;
8933 
8934 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
8935 	if (!all)
8936 		goto hwrm_func_resc_qcaps_exit;
8937 
8938 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
8939 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
8940 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
8941 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
8942 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
8943 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
8944 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
8945 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
8946 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
8947 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
8948 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
8949 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
8950 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
8951 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
8952 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
8953 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
8954 
8955 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8956 		u16 max_msix = le16_to_cpu(resp->max_msix);
8957 
8958 		hw_resc->max_nqs = max_msix;
8959 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
8960 	}
8961 
8962 	if (BNXT_PF(bp)) {
8963 		struct bnxt_pf_info *pf = &bp->pf;
8964 
8965 		pf->vf_resv_strategy =
8966 			le16_to_cpu(resp->vf_reservation_strategy);
8967 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
8968 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
8969 	}
8970 hwrm_func_resc_qcaps_exit:
8971 	hwrm_req_drop(bp, req);
8972 	return rc;
8973 }
8974 
8975 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
8976 {
8977 	struct hwrm_port_mac_ptp_qcfg_output *resp;
8978 	struct hwrm_port_mac_ptp_qcfg_input *req;
8979 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
8980 	bool phc_cfg;
8981 	u8 flags;
8982 	int rc;
8983 
8984 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5(bp)) {
8985 		rc = -ENODEV;
8986 		goto no_ptp;
8987 	}
8988 
8989 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
8990 	if (rc)
8991 		goto no_ptp;
8992 
8993 	req->port_id = cpu_to_le16(bp->pf.port_id);
8994 	resp = hwrm_req_hold(bp, req);
8995 	rc = hwrm_req_send(bp, req);
8996 	if (rc)
8997 		goto exit;
8998 
8999 	flags = resp->flags;
9000 	if (!(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9001 		rc = -ENODEV;
9002 		goto exit;
9003 	}
9004 	if (!ptp) {
9005 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
9006 		if (!ptp) {
9007 			rc = -ENOMEM;
9008 			goto exit;
9009 		}
9010 		ptp->bp = bp;
9011 		bp->ptp_cfg = ptp;
9012 	}
9013 	if (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK) {
9014 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9015 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9016 	} else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9017 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9018 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9019 	} else {
9020 		rc = -ENODEV;
9021 		goto exit;
9022 	}
9023 	phc_cfg = (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9024 	rc = bnxt_ptp_init(bp, phc_cfg);
9025 	if (rc)
9026 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9027 exit:
9028 	hwrm_req_drop(bp, req);
9029 	if (!rc)
9030 		return 0;
9031 
9032 no_ptp:
9033 	bnxt_ptp_clear(bp);
9034 	kfree(ptp);
9035 	bp->ptp_cfg = NULL;
9036 	return rc;
9037 }
9038 
9039 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9040 {
9041 	struct hwrm_func_qcaps_output *resp;
9042 	struct hwrm_func_qcaps_input *req;
9043 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9044 	u32 flags, flags_ext, flags_ext2;
9045 	int rc;
9046 
9047 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9048 	if (rc)
9049 		return rc;
9050 
9051 	req->fid = cpu_to_le16(0xffff);
9052 	resp = hwrm_req_hold(bp, req);
9053 	rc = hwrm_req_send(bp, req);
9054 	if (rc)
9055 		goto hwrm_func_qcaps_exit;
9056 
9057 	flags = le32_to_cpu(resp->flags);
9058 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9059 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9060 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9061 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9062 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9063 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9064 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9065 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9066 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9067 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9068 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9069 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9070 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9071 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9072 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9073 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9074 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9075 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9076 
9077 	flags_ext = le32_to_cpu(resp->flags_ext);
9078 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9079 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9080 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9081 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9082 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9083 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9084 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9085 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9086 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9087 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9088 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9089 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9090 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9091 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9092 
9093 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9094 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9095 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9096 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9097 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9098 
9099 	bp->tx_push_thresh = 0;
9100 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9101 	    BNXT_FW_MAJ(bp) > 217)
9102 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9103 
9104 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9105 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9106 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9107 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9108 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9109 	if (!hw_resc->max_hw_ring_grps)
9110 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9111 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9112 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9113 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9114 
9115 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9116 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9117 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9118 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9119 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9120 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9121 
9122 	if (BNXT_PF(bp)) {
9123 		struct bnxt_pf_info *pf = &bp->pf;
9124 
9125 		pf->fw_fid = le16_to_cpu(resp->fid);
9126 		pf->port_id = le16_to_cpu(resp->port_id);
9127 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9128 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9129 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9130 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9131 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9132 			bp->flags |= BNXT_FLAG_WOL_CAP;
9133 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9134 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9135 		} else {
9136 			bnxt_ptp_clear(bp);
9137 			kfree(bp->ptp_cfg);
9138 			bp->ptp_cfg = NULL;
9139 		}
9140 	} else {
9141 #ifdef CONFIG_BNXT_SRIOV
9142 		struct bnxt_vf_info *vf = &bp->vf;
9143 
9144 		vf->fw_fid = le16_to_cpu(resp->fid);
9145 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9146 #endif
9147 	}
9148 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9149 
9150 hwrm_func_qcaps_exit:
9151 	hwrm_req_drop(bp, req);
9152 	return rc;
9153 }
9154 
9155 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9156 {
9157 	struct hwrm_dbg_qcaps_output *resp;
9158 	struct hwrm_dbg_qcaps_input *req;
9159 	int rc;
9160 
9161 	bp->fw_dbg_cap = 0;
9162 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9163 		return;
9164 
9165 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9166 	if (rc)
9167 		return;
9168 
9169 	req->fid = cpu_to_le16(0xffff);
9170 	resp = hwrm_req_hold(bp, req);
9171 	rc = hwrm_req_send(bp, req);
9172 	if (rc)
9173 		goto hwrm_dbg_qcaps_exit;
9174 
9175 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9176 
9177 hwrm_dbg_qcaps_exit:
9178 	hwrm_req_drop(bp, req);
9179 }
9180 
9181 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9182 
9183 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9184 {
9185 	int rc;
9186 
9187 	rc = __bnxt_hwrm_func_qcaps(bp);
9188 	if (rc)
9189 		return rc;
9190 
9191 	bnxt_hwrm_dbg_qcaps(bp);
9192 
9193 	rc = bnxt_hwrm_queue_qportcfg(bp);
9194 	if (rc) {
9195 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9196 		return rc;
9197 	}
9198 	if (bp->hwrm_spec_code >= 0x10803) {
9199 		rc = bnxt_alloc_ctx_mem(bp);
9200 		if (rc)
9201 			return rc;
9202 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9203 		if (!rc)
9204 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9205 	}
9206 	return 0;
9207 }
9208 
9209 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9210 {
9211 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9212 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9213 	u32 flags;
9214 	int rc;
9215 
9216 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9217 		return 0;
9218 
9219 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9220 	if (rc)
9221 		return rc;
9222 
9223 	resp = hwrm_req_hold(bp, req);
9224 	rc = hwrm_req_send(bp, req);
9225 	if (rc)
9226 		goto hwrm_cfa_adv_qcaps_exit;
9227 
9228 	flags = le32_to_cpu(resp->flags);
9229 	if (flags &
9230 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9231 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9232 
9233 	if (flags &
9234 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9235 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9236 
9237 	if (flags &
9238 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9239 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9240 
9241 hwrm_cfa_adv_qcaps_exit:
9242 	hwrm_req_drop(bp, req);
9243 	return rc;
9244 }
9245 
9246 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9247 {
9248 	if (bp->fw_health)
9249 		return 0;
9250 
9251 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9252 	if (!bp->fw_health)
9253 		return -ENOMEM;
9254 
9255 	mutex_init(&bp->fw_health->lock);
9256 	return 0;
9257 }
9258 
9259 static int bnxt_alloc_fw_health(struct bnxt *bp)
9260 {
9261 	int rc;
9262 
9263 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9264 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9265 		return 0;
9266 
9267 	rc = __bnxt_alloc_fw_health(bp);
9268 	if (rc) {
9269 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9270 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9271 		return rc;
9272 	}
9273 
9274 	return 0;
9275 }
9276 
9277 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9278 {
9279 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9280 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9281 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9282 }
9283 
9284 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9285 {
9286 	struct bnxt_fw_health *fw_health = bp->fw_health;
9287 	u32 reg_type;
9288 
9289 	if (!fw_health)
9290 		return;
9291 
9292 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9293 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9294 		fw_health->status_reliable = false;
9295 
9296 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9297 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9298 		fw_health->resets_reliable = false;
9299 }
9300 
9301 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9302 {
9303 	void __iomem *hs;
9304 	u32 status_loc;
9305 	u32 reg_type;
9306 	u32 sig;
9307 
9308 	if (bp->fw_health)
9309 		bp->fw_health->status_reliable = false;
9310 
9311 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9312 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9313 
9314 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9315 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9316 		if (!bp->chip_num) {
9317 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9318 			bp->chip_num = readl(bp->bar0 +
9319 					     BNXT_FW_HEALTH_WIN_BASE +
9320 					     BNXT_GRC_REG_CHIP_NUM);
9321 		}
9322 		if (!BNXT_CHIP_P5_PLUS(bp))
9323 			return;
9324 
9325 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9326 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9327 	} else {
9328 		status_loc = readl(hs + offsetof(struct hcomm_status,
9329 						 fw_status_loc));
9330 	}
9331 
9332 	if (__bnxt_alloc_fw_health(bp)) {
9333 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9334 		return;
9335 	}
9336 
9337 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9338 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9339 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9340 		__bnxt_map_fw_health_reg(bp, status_loc);
9341 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9342 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9343 	}
9344 
9345 	bp->fw_health->status_reliable = true;
9346 }
9347 
9348 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9349 {
9350 	struct bnxt_fw_health *fw_health = bp->fw_health;
9351 	u32 reg_base = 0xffffffff;
9352 	int i;
9353 
9354 	bp->fw_health->status_reliable = false;
9355 	bp->fw_health->resets_reliable = false;
9356 	/* Only pre-map the monitoring GRC registers using window 3 */
9357 	for (i = 0; i < 4; i++) {
9358 		u32 reg = fw_health->regs[i];
9359 
9360 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9361 			continue;
9362 		if (reg_base == 0xffffffff)
9363 			reg_base = reg & BNXT_GRC_BASE_MASK;
9364 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9365 			return -ERANGE;
9366 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9367 	}
9368 	bp->fw_health->status_reliable = true;
9369 	bp->fw_health->resets_reliable = true;
9370 	if (reg_base == 0xffffffff)
9371 		return 0;
9372 
9373 	__bnxt_map_fw_health_reg(bp, reg_base);
9374 	return 0;
9375 }
9376 
9377 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9378 {
9379 	if (!bp->fw_health)
9380 		return;
9381 
9382 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9383 		bp->fw_health->status_reliable = true;
9384 		bp->fw_health->resets_reliable = true;
9385 	} else {
9386 		bnxt_try_map_fw_health_reg(bp);
9387 	}
9388 }
9389 
9390 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9391 {
9392 	struct bnxt_fw_health *fw_health = bp->fw_health;
9393 	struct hwrm_error_recovery_qcfg_output *resp;
9394 	struct hwrm_error_recovery_qcfg_input *req;
9395 	int rc, i;
9396 
9397 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9398 		return 0;
9399 
9400 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9401 	if (rc)
9402 		return rc;
9403 
9404 	resp = hwrm_req_hold(bp, req);
9405 	rc = hwrm_req_send(bp, req);
9406 	if (rc)
9407 		goto err_recovery_out;
9408 	fw_health->flags = le32_to_cpu(resp->flags);
9409 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
9410 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
9411 		rc = -EINVAL;
9412 		goto err_recovery_out;
9413 	}
9414 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
9415 	fw_health->master_func_wait_dsecs =
9416 		le32_to_cpu(resp->master_func_wait_period);
9417 	fw_health->normal_func_wait_dsecs =
9418 		le32_to_cpu(resp->normal_func_wait_period);
9419 	fw_health->post_reset_wait_dsecs =
9420 		le32_to_cpu(resp->master_func_wait_period_after_reset);
9421 	fw_health->post_reset_max_wait_dsecs =
9422 		le32_to_cpu(resp->max_bailout_time_after_reset);
9423 	fw_health->regs[BNXT_FW_HEALTH_REG] =
9424 		le32_to_cpu(resp->fw_health_status_reg);
9425 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
9426 		le32_to_cpu(resp->fw_heartbeat_reg);
9427 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
9428 		le32_to_cpu(resp->fw_reset_cnt_reg);
9429 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
9430 		le32_to_cpu(resp->reset_inprogress_reg);
9431 	fw_health->fw_reset_inprog_reg_mask =
9432 		le32_to_cpu(resp->reset_inprogress_reg_mask);
9433 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
9434 	if (fw_health->fw_reset_seq_cnt >= 16) {
9435 		rc = -EINVAL;
9436 		goto err_recovery_out;
9437 	}
9438 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
9439 		fw_health->fw_reset_seq_regs[i] =
9440 			le32_to_cpu(resp->reset_reg[i]);
9441 		fw_health->fw_reset_seq_vals[i] =
9442 			le32_to_cpu(resp->reset_reg_val[i]);
9443 		fw_health->fw_reset_seq_delay_msec[i] =
9444 			resp->delay_after_reset[i];
9445 	}
9446 err_recovery_out:
9447 	hwrm_req_drop(bp, req);
9448 	if (!rc)
9449 		rc = bnxt_map_fw_health_regs(bp);
9450 	if (rc)
9451 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9452 	return rc;
9453 }
9454 
9455 static int bnxt_hwrm_func_reset(struct bnxt *bp)
9456 {
9457 	struct hwrm_func_reset_input *req;
9458 	int rc;
9459 
9460 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
9461 	if (rc)
9462 		return rc;
9463 
9464 	req->enables = 0;
9465 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
9466 	return hwrm_req_send(bp, req);
9467 }
9468 
9469 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
9470 {
9471 	struct hwrm_nvm_get_dev_info_output nvm_info;
9472 
9473 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
9474 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
9475 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
9476 			 nvm_info.nvm_cfg_ver_upd);
9477 }
9478 
9479 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
9480 {
9481 	struct hwrm_queue_qportcfg_output *resp;
9482 	struct hwrm_queue_qportcfg_input *req;
9483 	u8 i, j, *qptr;
9484 	bool no_rdma;
9485 	int rc = 0;
9486 
9487 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
9488 	if (rc)
9489 		return rc;
9490 
9491 	resp = hwrm_req_hold(bp, req);
9492 	rc = hwrm_req_send(bp, req);
9493 	if (rc)
9494 		goto qportcfg_exit;
9495 
9496 	if (!resp->max_configurable_queues) {
9497 		rc = -EINVAL;
9498 		goto qportcfg_exit;
9499 	}
9500 	bp->max_tc = resp->max_configurable_queues;
9501 	bp->max_lltc = resp->max_configurable_lossless_queues;
9502 	if (bp->max_tc > BNXT_MAX_QUEUE)
9503 		bp->max_tc = BNXT_MAX_QUEUE;
9504 
9505 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
9506 	qptr = &resp->queue_id0;
9507 	for (i = 0, j = 0; i < bp->max_tc; i++) {
9508 		bp->q_info[j].queue_id = *qptr;
9509 		bp->q_ids[i] = *qptr++;
9510 		bp->q_info[j].queue_profile = *qptr++;
9511 		bp->tc_to_qidx[j] = j;
9512 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
9513 		    (no_rdma && BNXT_PF(bp)))
9514 			j++;
9515 	}
9516 	bp->max_q = bp->max_tc;
9517 	bp->max_tc = max_t(u8, j, 1);
9518 
9519 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
9520 		bp->max_tc = 1;
9521 
9522 	if (bp->max_lltc > bp->max_tc)
9523 		bp->max_lltc = bp->max_tc;
9524 
9525 qportcfg_exit:
9526 	hwrm_req_drop(bp, req);
9527 	return rc;
9528 }
9529 
9530 static int bnxt_hwrm_poll(struct bnxt *bp)
9531 {
9532 	struct hwrm_ver_get_input *req;
9533 	int rc;
9534 
9535 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9536 	if (rc)
9537 		return rc;
9538 
9539 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9540 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9541 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9542 
9543 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
9544 	rc = hwrm_req_send(bp, req);
9545 	return rc;
9546 }
9547 
9548 static int bnxt_hwrm_ver_get(struct bnxt *bp)
9549 {
9550 	struct hwrm_ver_get_output *resp;
9551 	struct hwrm_ver_get_input *req;
9552 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
9553 	u32 dev_caps_cfg, hwrm_ver;
9554 	int rc, len;
9555 
9556 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9557 	if (rc)
9558 		return rc;
9559 
9560 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
9561 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
9562 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9563 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9564 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9565 
9566 	resp = hwrm_req_hold(bp, req);
9567 	rc = hwrm_req_send(bp, req);
9568 	if (rc)
9569 		goto hwrm_ver_get_exit;
9570 
9571 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
9572 
9573 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
9574 			     resp->hwrm_intf_min_8b << 8 |
9575 			     resp->hwrm_intf_upd_8b;
9576 	if (resp->hwrm_intf_maj_8b < 1) {
9577 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
9578 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
9579 			    resp->hwrm_intf_upd_8b);
9580 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
9581 	}
9582 
9583 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
9584 			HWRM_VERSION_UPDATE;
9585 
9586 	if (bp->hwrm_spec_code > hwrm_ver)
9587 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
9588 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
9589 			 HWRM_VERSION_UPDATE);
9590 	else
9591 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
9592 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
9593 			 resp->hwrm_intf_upd_8b);
9594 
9595 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
9596 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
9597 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
9598 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
9599 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
9600 		len = FW_VER_STR_LEN;
9601 	} else {
9602 		fw_maj = resp->hwrm_fw_maj_8b;
9603 		fw_min = resp->hwrm_fw_min_8b;
9604 		fw_bld = resp->hwrm_fw_bld_8b;
9605 		fw_rsv = resp->hwrm_fw_rsvd_8b;
9606 		len = BC_HWRM_STR_LEN;
9607 	}
9608 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
9609 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
9610 		 fw_rsv);
9611 
9612 	if (strlen(resp->active_pkg_name)) {
9613 		int fw_ver_len = strlen(bp->fw_ver_str);
9614 
9615 		snprintf(bp->fw_ver_str + fw_ver_len,
9616 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
9617 			 resp->active_pkg_name);
9618 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
9619 	}
9620 
9621 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
9622 	if (!bp->hwrm_cmd_timeout)
9623 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
9624 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
9625 	if (!bp->hwrm_cmd_max_timeout)
9626 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
9627 	else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT)
9628 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n",
9629 			    bp->hwrm_cmd_max_timeout / 1000);
9630 
9631 	if (resp->hwrm_intf_maj_8b >= 1) {
9632 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
9633 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
9634 	}
9635 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
9636 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
9637 
9638 	bp->chip_num = le16_to_cpu(resp->chip_num);
9639 	bp->chip_rev = resp->chip_rev;
9640 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
9641 	    !resp->chip_metal)
9642 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
9643 
9644 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
9645 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
9646 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
9647 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
9648 
9649 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
9650 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
9651 
9652 	if (dev_caps_cfg &
9653 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
9654 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
9655 
9656 	if (dev_caps_cfg &
9657 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
9658 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
9659 
9660 	if (dev_caps_cfg &
9661 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
9662 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
9663 
9664 hwrm_ver_get_exit:
9665 	hwrm_req_drop(bp, req);
9666 	return rc;
9667 }
9668 
9669 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
9670 {
9671 	struct hwrm_fw_set_time_input *req;
9672 	struct tm tm;
9673 	time64_t now = ktime_get_real_seconds();
9674 	int rc;
9675 
9676 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
9677 	    bp->hwrm_spec_code < 0x10400)
9678 		return -EOPNOTSUPP;
9679 
9680 	time64_to_tm(now, 0, &tm);
9681 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
9682 	if (rc)
9683 		return rc;
9684 
9685 	req->year = cpu_to_le16(1900 + tm.tm_year);
9686 	req->month = 1 + tm.tm_mon;
9687 	req->day = tm.tm_mday;
9688 	req->hour = tm.tm_hour;
9689 	req->minute = tm.tm_min;
9690 	req->second = tm.tm_sec;
9691 	return hwrm_req_send(bp, req);
9692 }
9693 
9694 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
9695 {
9696 	u64 sw_tmp;
9697 
9698 	hw &= mask;
9699 	sw_tmp = (*sw & ~mask) | hw;
9700 	if (hw < (*sw & mask))
9701 		sw_tmp += mask + 1;
9702 	WRITE_ONCE(*sw, sw_tmp);
9703 }
9704 
9705 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
9706 				    int count, bool ignore_zero)
9707 {
9708 	int i;
9709 
9710 	for (i = 0; i < count; i++) {
9711 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
9712 
9713 		if (ignore_zero && !hw)
9714 			continue;
9715 
9716 		if (masks[i] == -1ULL)
9717 			sw_stats[i] = hw;
9718 		else
9719 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
9720 	}
9721 }
9722 
9723 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
9724 {
9725 	if (!stats->hw_stats)
9726 		return;
9727 
9728 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
9729 				stats->hw_masks, stats->len / 8, false);
9730 }
9731 
9732 static void bnxt_accumulate_all_stats(struct bnxt *bp)
9733 {
9734 	struct bnxt_stats_mem *ring0_stats;
9735 	bool ignore_zero = false;
9736 	int i;
9737 
9738 	/* Chip bug.  Counter intermittently becomes 0. */
9739 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
9740 		ignore_zero = true;
9741 
9742 	for (i = 0; i < bp->cp_nr_rings; i++) {
9743 		struct bnxt_napi *bnapi = bp->bnapi[i];
9744 		struct bnxt_cp_ring_info *cpr;
9745 		struct bnxt_stats_mem *stats;
9746 
9747 		cpr = &bnapi->cp_ring;
9748 		stats = &cpr->stats;
9749 		if (!i)
9750 			ring0_stats = stats;
9751 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
9752 					ring0_stats->hw_masks,
9753 					ring0_stats->len / 8, ignore_zero);
9754 	}
9755 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
9756 		struct bnxt_stats_mem *stats = &bp->port_stats;
9757 		__le64 *hw_stats = stats->hw_stats;
9758 		u64 *sw_stats = stats->sw_stats;
9759 		u64 *masks = stats->hw_masks;
9760 		int cnt;
9761 
9762 		cnt = sizeof(struct rx_port_stats) / 8;
9763 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
9764 
9765 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9766 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9767 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9768 		cnt = sizeof(struct tx_port_stats) / 8;
9769 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
9770 	}
9771 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
9772 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
9773 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
9774 	}
9775 }
9776 
9777 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
9778 {
9779 	struct hwrm_port_qstats_input *req;
9780 	struct bnxt_pf_info *pf = &bp->pf;
9781 	int rc;
9782 
9783 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
9784 		return 0;
9785 
9786 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
9787 		return -EOPNOTSUPP;
9788 
9789 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
9790 	if (rc)
9791 		return rc;
9792 
9793 	req->flags = flags;
9794 	req->port_id = cpu_to_le16(pf->port_id);
9795 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
9796 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
9797 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
9798 	return hwrm_req_send(bp, req);
9799 }
9800 
9801 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
9802 {
9803 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
9804 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
9805 	struct hwrm_port_qstats_ext_output *resp_qs;
9806 	struct hwrm_port_qstats_ext_input *req_qs;
9807 	struct bnxt_pf_info *pf = &bp->pf;
9808 	u32 tx_stat_size;
9809 	int rc;
9810 
9811 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
9812 		return 0;
9813 
9814 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
9815 		return -EOPNOTSUPP;
9816 
9817 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
9818 	if (rc)
9819 		return rc;
9820 
9821 	req_qs->flags = flags;
9822 	req_qs->port_id = cpu_to_le16(pf->port_id);
9823 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
9824 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
9825 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
9826 		       sizeof(struct tx_port_stats_ext) : 0;
9827 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
9828 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
9829 	resp_qs = hwrm_req_hold(bp, req_qs);
9830 	rc = hwrm_req_send(bp, req_qs);
9831 	if (!rc) {
9832 		bp->fw_rx_stats_ext_size =
9833 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
9834 		if (BNXT_FW_MAJ(bp) < 220 &&
9835 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
9836 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
9837 
9838 		bp->fw_tx_stats_ext_size = tx_stat_size ?
9839 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
9840 	} else {
9841 		bp->fw_rx_stats_ext_size = 0;
9842 		bp->fw_tx_stats_ext_size = 0;
9843 	}
9844 	hwrm_req_drop(bp, req_qs);
9845 
9846 	if (flags)
9847 		return rc;
9848 
9849 	if (bp->fw_tx_stats_ext_size <=
9850 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
9851 		bp->pri2cos_valid = 0;
9852 		return rc;
9853 	}
9854 
9855 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
9856 	if (rc)
9857 		return rc;
9858 
9859 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
9860 
9861 	resp_qc = hwrm_req_hold(bp, req_qc);
9862 	rc = hwrm_req_send(bp, req_qc);
9863 	if (!rc) {
9864 		u8 *pri2cos;
9865 		int i, j;
9866 
9867 		pri2cos = &resp_qc->pri0_cos_queue_id;
9868 		for (i = 0; i < 8; i++) {
9869 			u8 queue_id = pri2cos[i];
9870 			u8 queue_idx;
9871 
9872 			/* Per port queue IDs start from 0, 10, 20, etc */
9873 			queue_idx = queue_id % 10;
9874 			if (queue_idx > BNXT_MAX_QUEUE) {
9875 				bp->pri2cos_valid = false;
9876 				hwrm_req_drop(bp, req_qc);
9877 				return rc;
9878 			}
9879 			for (j = 0; j < bp->max_q; j++) {
9880 				if (bp->q_ids[j] == queue_id)
9881 					bp->pri2cos_idx[i] = queue_idx;
9882 			}
9883 		}
9884 		bp->pri2cos_valid = true;
9885 	}
9886 	hwrm_req_drop(bp, req_qc);
9887 
9888 	return rc;
9889 }
9890 
9891 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
9892 {
9893 	bnxt_hwrm_tunnel_dst_port_free(bp,
9894 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
9895 	bnxt_hwrm_tunnel_dst_port_free(bp,
9896 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
9897 }
9898 
9899 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
9900 {
9901 	int rc, i;
9902 	u32 tpa_flags = 0;
9903 
9904 	if (set_tpa)
9905 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
9906 	else if (BNXT_NO_FW_ACCESS(bp))
9907 		return 0;
9908 	for (i = 0; i < bp->nr_vnics; i++) {
9909 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
9910 		if (rc) {
9911 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
9912 				   i, rc);
9913 			return rc;
9914 		}
9915 	}
9916 	return 0;
9917 }
9918 
9919 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
9920 {
9921 	int i;
9922 
9923 	for (i = 0; i < bp->nr_vnics; i++)
9924 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
9925 }
9926 
9927 static void bnxt_clear_vnic(struct bnxt *bp)
9928 {
9929 	if (!bp->vnic_info)
9930 		return;
9931 
9932 	bnxt_hwrm_clear_vnic_filter(bp);
9933 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
9934 		/* clear all RSS setting before free vnic ctx */
9935 		bnxt_hwrm_clear_vnic_rss(bp);
9936 		bnxt_hwrm_vnic_ctx_free(bp);
9937 	}
9938 	/* before free the vnic, undo the vnic tpa settings */
9939 	if (bp->flags & BNXT_FLAG_TPA)
9940 		bnxt_set_tpa(bp, false);
9941 	bnxt_hwrm_vnic_free(bp);
9942 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
9943 		bnxt_hwrm_vnic_ctx_free(bp);
9944 }
9945 
9946 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
9947 				    bool irq_re_init)
9948 {
9949 	bnxt_clear_vnic(bp);
9950 	bnxt_hwrm_ring_free(bp, close_path);
9951 	bnxt_hwrm_ring_grp_free(bp);
9952 	if (irq_re_init) {
9953 		bnxt_hwrm_stat_ctx_free(bp);
9954 		bnxt_hwrm_free_tunnel_ports(bp);
9955 	}
9956 }
9957 
9958 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
9959 {
9960 	struct hwrm_func_cfg_input *req;
9961 	u8 evb_mode;
9962 	int rc;
9963 
9964 	if (br_mode == BRIDGE_MODE_VEB)
9965 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
9966 	else if (br_mode == BRIDGE_MODE_VEPA)
9967 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
9968 	else
9969 		return -EINVAL;
9970 
9971 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
9972 	if (rc)
9973 		return rc;
9974 
9975 	req->fid = cpu_to_le16(0xffff);
9976 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
9977 	req->evb_mode = evb_mode;
9978 	return hwrm_req_send(bp, req);
9979 }
9980 
9981 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
9982 {
9983 	struct hwrm_func_cfg_input *req;
9984 	int rc;
9985 
9986 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
9987 		return 0;
9988 
9989 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
9990 	if (rc)
9991 		return rc;
9992 
9993 	req->fid = cpu_to_le16(0xffff);
9994 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
9995 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
9996 	if (size == 128)
9997 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
9998 
9999 	return hwrm_req_send(bp, req);
10000 }
10001 
10002 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10003 {
10004 	int rc;
10005 
10006 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10007 		goto skip_rss_ctx;
10008 
10009 	/* allocate context for vnic */
10010 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10011 	if (rc) {
10012 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10013 			   vnic->vnic_id, rc);
10014 		goto vnic_setup_err;
10015 	}
10016 	bp->rsscos_nr_ctxs++;
10017 
10018 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10019 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10020 		if (rc) {
10021 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10022 				   vnic->vnic_id, rc);
10023 			goto vnic_setup_err;
10024 		}
10025 		bp->rsscos_nr_ctxs++;
10026 	}
10027 
10028 skip_rss_ctx:
10029 	/* configure default vnic, ring grp */
10030 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10031 	if (rc) {
10032 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10033 			   vnic->vnic_id, rc);
10034 		goto vnic_setup_err;
10035 	}
10036 
10037 	/* Enable RSS hashing on vnic */
10038 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10039 	if (rc) {
10040 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10041 			   vnic->vnic_id, rc);
10042 		goto vnic_setup_err;
10043 	}
10044 
10045 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10046 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10047 		if (rc) {
10048 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10049 				   vnic->vnic_id, rc);
10050 		}
10051 	}
10052 
10053 vnic_setup_err:
10054 	return rc;
10055 }
10056 
10057 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10058 {
10059 	int rc;
10060 
10061 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10062 	if (rc) {
10063 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10064 			   vnic->vnic_id, rc);
10065 		return rc;
10066 	}
10067 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10068 	if (rc)
10069 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10070 			   vnic->vnic_id, rc);
10071 	return rc;
10072 }
10073 
10074 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10075 {
10076 	int rc, i, nr_ctxs;
10077 
10078 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10079 	for (i = 0; i < nr_ctxs; i++) {
10080 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10081 		if (rc) {
10082 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10083 				   vnic->vnic_id, i, rc);
10084 			break;
10085 		}
10086 		bp->rsscos_nr_ctxs++;
10087 	}
10088 	if (i < nr_ctxs)
10089 		return -ENOMEM;
10090 
10091 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10092 	if (rc)
10093 		return rc;
10094 
10095 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10096 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10097 		if (rc) {
10098 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10099 				   vnic->vnic_id, rc);
10100 		}
10101 	}
10102 	return rc;
10103 }
10104 
10105 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10106 {
10107 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10108 		return __bnxt_setup_vnic_p5(bp, vnic);
10109 	else
10110 		return __bnxt_setup_vnic(bp, vnic);
10111 }
10112 
10113 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10114 				     struct bnxt_vnic_info *vnic,
10115 				     u16 start_rx_ring_idx, int rx_rings)
10116 {
10117 	int rc;
10118 
10119 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10120 	if (rc) {
10121 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10122 			   vnic->vnic_id, rc);
10123 		return rc;
10124 	}
10125 	return bnxt_setup_vnic(bp, vnic);
10126 }
10127 
10128 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10129 {
10130 	struct bnxt_vnic_info *vnic;
10131 	int i, rc = 0;
10132 
10133 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10134 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10135 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10136 	}
10137 
10138 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10139 		return 0;
10140 
10141 	for (i = 0; i < bp->rx_nr_rings; i++) {
10142 		u16 vnic_id = i + 1;
10143 		u16 ring_id = i;
10144 
10145 		if (vnic_id >= bp->nr_vnics)
10146 			break;
10147 
10148 		vnic = &bp->vnic_info[vnic_id];
10149 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10150 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10151 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10152 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10153 			break;
10154 	}
10155 	return rc;
10156 }
10157 
10158 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10159 			  bool all)
10160 {
10161 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10162 	struct bnxt_filter_base *usr_fltr, *tmp;
10163 	struct bnxt_ntuple_filter *ntp_fltr;
10164 	int i;
10165 
10166 	bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10167 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10168 		if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10169 			bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10170 	}
10171 	if (!all)
10172 		return;
10173 
10174 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10175 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10176 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10177 			ntp_fltr = container_of(usr_fltr,
10178 						struct bnxt_ntuple_filter,
10179 						base);
10180 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10181 			bnxt_del_ntp_filter(bp, ntp_fltr);
10182 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10183 		}
10184 	}
10185 
10186 	if (vnic->rss_table)
10187 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10188 				  vnic->rss_table,
10189 				  vnic->rss_table_dma_addr);
10190 	kfree(rss_ctx->rss_indir_tbl);
10191 	list_del(&rss_ctx->list);
10192 	bp->num_rss_ctx--;
10193 	clear_bit(rss_ctx->index, bp->rss_ctx_bmap);
10194 	kfree(rss_ctx);
10195 }
10196 
10197 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10198 {
10199 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10200 	struct bnxt_rss_ctx *rss_ctx, *tmp;
10201 
10202 	list_for_each_entry_safe(rss_ctx, tmp, &bp->rss_ctx_list, list) {
10203 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10204 
10205 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10206 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10207 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10208 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10209 				   rss_ctx->index);
10210 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10211 		}
10212 	}
10213 }
10214 
10215 struct bnxt_rss_ctx *bnxt_alloc_rss_ctx(struct bnxt *bp)
10216 {
10217 	struct bnxt_rss_ctx *rss_ctx = NULL;
10218 
10219 	rss_ctx = kzalloc(sizeof(*rss_ctx), GFP_KERNEL);
10220 	if (rss_ctx) {
10221 		rss_ctx->vnic.rss_ctx = rss_ctx;
10222 		list_add_tail(&rss_ctx->list, &bp->rss_ctx_list);
10223 		bp->num_rss_ctx++;
10224 	}
10225 	return rss_ctx;
10226 }
10227 
10228 void bnxt_clear_rss_ctxs(struct bnxt *bp, bool all)
10229 {
10230 	struct bnxt_rss_ctx *rss_ctx, *tmp;
10231 
10232 	list_for_each_entry_safe(rss_ctx, tmp, &bp->rss_ctx_list, list)
10233 		bnxt_del_one_rss_ctx(bp, rss_ctx, all);
10234 
10235 	if (all)
10236 		bitmap_free(bp->rss_ctx_bmap);
10237 }
10238 
10239 static void bnxt_init_multi_rss_ctx(struct bnxt *bp)
10240 {
10241 	bp->rss_ctx_bmap = bitmap_zalloc(BNXT_RSS_CTX_BMAP_LEN, GFP_KERNEL);
10242 	if (bp->rss_ctx_bmap) {
10243 		/* burn index 0 since we cannot have context 0 */
10244 		__set_bit(0, bp->rss_ctx_bmap);
10245 		INIT_LIST_HEAD(&bp->rss_ctx_list);
10246 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
10247 	}
10248 }
10249 
10250 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
10251 static bool bnxt_promisc_ok(struct bnxt *bp)
10252 {
10253 #ifdef CONFIG_BNXT_SRIOV
10254 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10255 		return false;
10256 #endif
10257 	return true;
10258 }
10259 
10260 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10261 {
10262 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10263 	unsigned int rc = 0;
10264 
10265 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10266 	if (rc) {
10267 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10268 			   rc);
10269 		return rc;
10270 	}
10271 
10272 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10273 	if (rc) {
10274 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10275 			   rc);
10276 		return rc;
10277 	}
10278 	return rc;
10279 }
10280 
10281 static int bnxt_cfg_rx_mode(struct bnxt *);
10282 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10283 
10284 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10285 {
10286 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10287 	int rc = 0;
10288 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10289 
10290 	if (irq_re_init) {
10291 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10292 		if (rc) {
10293 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10294 				   rc);
10295 			goto err_out;
10296 		}
10297 	}
10298 
10299 	rc = bnxt_hwrm_ring_alloc(bp);
10300 	if (rc) {
10301 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10302 		goto err_out;
10303 	}
10304 
10305 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10306 	if (rc) {
10307 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10308 		goto err_out;
10309 	}
10310 
10311 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10312 		rx_nr_rings--;
10313 
10314 	/* default vnic 0 */
10315 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10316 	if (rc) {
10317 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10318 		goto err_out;
10319 	}
10320 
10321 	if (BNXT_VF(bp))
10322 		bnxt_hwrm_func_qcfg(bp);
10323 
10324 	rc = bnxt_setup_vnic(bp, vnic);
10325 	if (rc)
10326 		goto err_out;
10327 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10328 		bnxt_hwrm_update_rss_hash_cfg(bp);
10329 
10330 	if (bp->flags & BNXT_FLAG_RFS) {
10331 		rc = bnxt_alloc_rfs_vnics(bp);
10332 		if (rc)
10333 			goto err_out;
10334 	}
10335 
10336 	if (bp->flags & BNXT_FLAG_TPA) {
10337 		rc = bnxt_set_tpa(bp, true);
10338 		if (rc)
10339 			goto err_out;
10340 	}
10341 
10342 	if (BNXT_VF(bp))
10343 		bnxt_update_vf_mac(bp);
10344 
10345 	/* Filter for default vnic 0 */
10346 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
10347 	if (rc) {
10348 		if (BNXT_VF(bp) && rc == -ENODEV)
10349 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
10350 		else
10351 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
10352 		goto err_out;
10353 	}
10354 	vnic->uc_filter_count = 1;
10355 
10356 	vnic->rx_mask = 0;
10357 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
10358 		goto skip_rx_mask;
10359 
10360 	if (bp->dev->flags & IFF_BROADCAST)
10361 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10362 
10363 	if (bp->dev->flags & IFF_PROMISC)
10364 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10365 
10366 	if (bp->dev->flags & IFF_ALLMULTI) {
10367 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10368 		vnic->mc_list_count = 0;
10369 	} else if (bp->dev->flags & IFF_MULTICAST) {
10370 		u32 mask = 0;
10371 
10372 		bnxt_mc_list_updated(bp, &mask);
10373 		vnic->rx_mask |= mask;
10374 	}
10375 
10376 	rc = bnxt_cfg_rx_mode(bp);
10377 	if (rc)
10378 		goto err_out;
10379 
10380 skip_rx_mask:
10381 	rc = bnxt_hwrm_set_coal(bp);
10382 	if (rc)
10383 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
10384 				rc);
10385 
10386 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10387 		rc = bnxt_setup_nitroa0_vnic(bp);
10388 		if (rc)
10389 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
10390 				   rc);
10391 	}
10392 
10393 	if (BNXT_VF(bp)) {
10394 		bnxt_hwrm_func_qcfg(bp);
10395 		netdev_update_features(bp->dev);
10396 	}
10397 
10398 	return 0;
10399 
10400 err_out:
10401 	bnxt_hwrm_resource_free(bp, 0, true);
10402 
10403 	return rc;
10404 }
10405 
10406 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
10407 {
10408 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
10409 	return 0;
10410 }
10411 
10412 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
10413 {
10414 	bnxt_init_cp_rings(bp);
10415 	bnxt_init_rx_rings(bp);
10416 	bnxt_init_tx_rings(bp);
10417 	bnxt_init_ring_grps(bp, irq_re_init);
10418 	bnxt_init_vnics(bp);
10419 
10420 	return bnxt_init_chip(bp, irq_re_init);
10421 }
10422 
10423 static int bnxt_set_real_num_queues(struct bnxt *bp)
10424 {
10425 	int rc;
10426 	struct net_device *dev = bp->dev;
10427 
10428 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
10429 					  bp->tx_nr_rings_xdp);
10430 	if (rc)
10431 		return rc;
10432 
10433 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
10434 	if (rc)
10435 		return rc;
10436 
10437 #ifdef CONFIG_RFS_ACCEL
10438 	if (bp->flags & BNXT_FLAG_RFS)
10439 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
10440 #endif
10441 
10442 	return rc;
10443 }
10444 
10445 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10446 			     bool shared)
10447 {
10448 	int _rx = *rx, _tx = *tx;
10449 
10450 	if (shared) {
10451 		*rx = min_t(int, _rx, max);
10452 		*tx = min_t(int, _tx, max);
10453 	} else {
10454 		if (max < 2)
10455 			return -ENOMEM;
10456 
10457 		while (_rx + _tx > max) {
10458 			if (_rx > _tx && _rx > 1)
10459 				_rx--;
10460 			else if (_tx > 1)
10461 				_tx--;
10462 		}
10463 		*rx = _rx;
10464 		*tx = _tx;
10465 	}
10466 	return 0;
10467 }
10468 
10469 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
10470 {
10471 	return (tx - tx_xdp) / tx_sets + tx_xdp;
10472 }
10473 
10474 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
10475 {
10476 	int tcs = bp->num_tc;
10477 
10478 	if (!tcs)
10479 		tcs = 1;
10480 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
10481 }
10482 
10483 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
10484 {
10485 	int tcs = bp->num_tc;
10486 
10487 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
10488 	       bp->tx_nr_rings_xdp;
10489 }
10490 
10491 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10492 			   bool sh)
10493 {
10494 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
10495 
10496 	if (tx_cp != *tx) {
10497 		int tx_saved = tx_cp, rc;
10498 
10499 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
10500 		if (rc)
10501 			return rc;
10502 		if (tx_cp != tx_saved)
10503 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
10504 		return 0;
10505 	}
10506 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
10507 }
10508 
10509 static void bnxt_setup_msix(struct bnxt *bp)
10510 {
10511 	const int len = sizeof(bp->irq_tbl[0].name);
10512 	struct net_device *dev = bp->dev;
10513 	int tcs, i;
10514 
10515 	tcs = bp->num_tc;
10516 	if (tcs) {
10517 		int i, off, count;
10518 
10519 		for (i = 0; i < tcs; i++) {
10520 			count = bp->tx_nr_rings_per_tc;
10521 			off = BNXT_TC_TO_RING_BASE(bp, i);
10522 			netdev_set_tc_queue(dev, i, count, off);
10523 		}
10524 	}
10525 
10526 	for (i = 0; i < bp->cp_nr_rings; i++) {
10527 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10528 		char *attr;
10529 
10530 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
10531 			attr = "TxRx";
10532 		else if (i < bp->rx_nr_rings)
10533 			attr = "rx";
10534 		else
10535 			attr = "tx";
10536 
10537 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
10538 			 attr, i);
10539 		bp->irq_tbl[map_idx].handler = bnxt_msix;
10540 	}
10541 }
10542 
10543 static void bnxt_setup_inta(struct bnxt *bp)
10544 {
10545 	const int len = sizeof(bp->irq_tbl[0].name);
10546 
10547 	if (bp->num_tc) {
10548 		netdev_reset_tc(bp->dev);
10549 		bp->num_tc = 0;
10550 	}
10551 
10552 	snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
10553 		 0);
10554 	bp->irq_tbl[0].handler = bnxt_inta;
10555 }
10556 
10557 static int bnxt_init_int_mode(struct bnxt *bp);
10558 
10559 static int bnxt_setup_int_mode(struct bnxt *bp)
10560 {
10561 	int rc;
10562 
10563 	if (!bp->irq_tbl) {
10564 		rc = bnxt_init_int_mode(bp);
10565 		if (rc || !bp->irq_tbl)
10566 			return rc ?: -ENODEV;
10567 	}
10568 
10569 	if (bp->flags & BNXT_FLAG_USING_MSIX)
10570 		bnxt_setup_msix(bp);
10571 	else
10572 		bnxt_setup_inta(bp);
10573 
10574 	rc = bnxt_set_real_num_queues(bp);
10575 	return rc;
10576 }
10577 
10578 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
10579 {
10580 	return bp->hw_resc.max_rsscos_ctxs;
10581 }
10582 
10583 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
10584 {
10585 	return bp->hw_resc.max_vnics;
10586 }
10587 
10588 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
10589 {
10590 	return bp->hw_resc.max_stat_ctxs;
10591 }
10592 
10593 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
10594 {
10595 	return bp->hw_resc.max_cp_rings;
10596 }
10597 
10598 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
10599 {
10600 	unsigned int cp = bp->hw_resc.max_cp_rings;
10601 
10602 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
10603 		cp -= bnxt_get_ulp_msix_num(bp);
10604 
10605 	return cp;
10606 }
10607 
10608 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
10609 {
10610 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
10611 
10612 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10613 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
10614 
10615 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
10616 }
10617 
10618 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
10619 {
10620 	bp->hw_resc.max_irqs = max_irqs;
10621 }
10622 
10623 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
10624 {
10625 	unsigned int cp;
10626 
10627 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
10628 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10629 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
10630 	else
10631 		return cp - bp->cp_nr_rings;
10632 }
10633 
10634 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
10635 {
10636 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
10637 }
10638 
10639 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
10640 {
10641 	int max_irq = bnxt_get_max_func_irqs(bp);
10642 	int total_req = bp->cp_nr_rings + num;
10643 
10644 	if (max_irq < total_req) {
10645 		num = max_irq - bp->cp_nr_rings;
10646 		if (num <= 0)
10647 			return 0;
10648 	}
10649 	return num;
10650 }
10651 
10652 static int bnxt_get_num_msix(struct bnxt *bp)
10653 {
10654 	if (!BNXT_NEW_RM(bp))
10655 		return bnxt_get_max_func_irqs(bp);
10656 
10657 	return bnxt_nq_rings_in_use(bp);
10658 }
10659 
10660 static int bnxt_init_msix(struct bnxt *bp)
10661 {
10662 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp;
10663 	struct msix_entry *msix_ent;
10664 
10665 	total_vecs = bnxt_get_num_msix(bp);
10666 	max = bnxt_get_max_func_irqs(bp);
10667 	if (total_vecs > max)
10668 		total_vecs = max;
10669 
10670 	if (!total_vecs)
10671 		return 0;
10672 
10673 	msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
10674 	if (!msix_ent)
10675 		return -ENOMEM;
10676 
10677 	for (i = 0; i < total_vecs; i++) {
10678 		msix_ent[i].entry = i;
10679 		msix_ent[i].vector = 0;
10680 	}
10681 
10682 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
10683 		min = 2;
10684 
10685 	total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
10686 	ulp_msix = bnxt_get_ulp_msix_num(bp);
10687 	if (total_vecs < 0 || total_vecs < ulp_msix) {
10688 		rc = -ENODEV;
10689 		goto msix_setup_exit;
10690 	}
10691 
10692 	bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
10693 	if (bp->irq_tbl) {
10694 		for (i = 0; i < total_vecs; i++)
10695 			bp->irq_tbl[i].vector = msix_ent[i].vector;
10696 
10697 		bp->total_irqs = total_vecs;
10698 		/* Trim rings based upon num of vectors allocated */
10699 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
10700 				     total_vecs - ulp_msix, min == 1);
10701 		if (rc)
10702 			goto msix_setup_exit;
10703 
10704 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
10705 		bp->cp_nr_rings = (min == 1) ?
10706 				  max_t(int, tx_cp, bp->rx_nr_rings) :
10707 				  tx_cp + bp->rx_nr_rings;
10708 
10709 	} else {
10710 		rc = -ENOMEM;
10711 		goto msix_setup_exit;
10712 	}
10713 	bp->flags |= BNXT_FLAG_USING_MSIX;
10714 	kfree(msix_ent);
10715 	return 0;
10716 
10717 msix_setup_exit:
10718 	netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
10719 	kfree(bp->irq_tbl);
10720 	bp->irq_tbl = NULL;
10721 	pci_disable_msix(bp->pdev);
10722 	kfree(msix_ent);
10723 	return rc;
10724 }
10725 
10726 static int bnxt_init_inta(struct bnxt *bp)
10727 {
10728 	bp->irq_tbl = kzalloc(sizeof(struct bnxt_irq), GFP_KERNEL);
10729 	if (!bp->irq_tbl)
10730 		return -ENOMEM;
10731 
10732 	bp->total_irqs = 1;
10733 	bp->rx_nr_rings = 1;
10734 	bp->tx_nr_rings = 1;
10735 	bp->cp_nr_rings = 1;
10736 	bp->flags |= BNXT_FLAG_SHARED_RINGS;
10737 	bp->irq_tbl[0].vector = bp->pdev->irq;
10738 	return 0;
10739 }
10740 
10741 static int bnxt_init_int_mode(struct bnxt *bp)
10742 {
10743 	int rc = -ENODEV;
10744 
10745 	if (bp->flags & BNXT_FLAG_MSIX_CAP)
10746 		rc = bnxt_init_msix(bp);
10747 
10748 	if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
10749 		/* fallback to INTA */
10750 		rc = bnxt_init_inta(bp);
10751 	}
10752 	return rc;
10753 }
10754 
10755 static void bnxt_clear_int_mode(struct bnxt *bp)
10756 {
10757 	if (bp->flags & BNXT_FLAG_USING_MSIX)
10758 		pci_disable_msix(bp->pdev);
10759 
10760 	kfree(bp->irq_tbl);
10761 	bp->irq_tbl = NULL;
10762 	bp->flags &= ~BNXT_FLAG_USING_MSIX;
10763 }
10764 
10765 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
10766 {
10767 	bool irq_cleared = false;
10768 	int tcs = bp->num_tc;
10769 	int irqs_required;
10770 	int rc;
10771 
10772 	if (!bnxt_need_reserve_rings(bp))
10773 		return 0;
10774 
10775 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
10776 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
10777 
10778 		if (ulp_msix > bp->ulp_num_msix_want)
10779 			ulp_msix = bp->ulp_num_msix_want;
10780 		irqs_required = ulp_msix + bp->cp_nr_rings;
10781 	} else {
10782 		irqs_required = bnxt_get_num_msix(bp);
10783 	}
10784 
10785 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
10786 		bnxt_ulp_irq_stop(bp);
10787 		bnxt_clear_int_mode(bp);
10788 		irq_cleared = true;
10789 	}
10790 	rc = __bnxt_reserve_rings(bp);
10791 	if (irq_cleared) {
10792 		if (!rc)
10793 			rc = bnxt_init_int_mode(bp);
10794 		bnxt_ulp_irq_restart(bp, rc);
10795 	}
10796 	if (rc) {
10797 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
10798 		return rc;
10799 	}
10800 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
10801 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
10802 		netdev_err(bp->dev, "tx ring reservation failure\n");
10803 		netdev_reset_tc(bp->dev);
10804 		bp->num_tc = 0;
10805 		if (bp->tx_nr_rings_xdp)
10806 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
10807 		else
10808 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
10809 		return -ENOMEM;
10810 	}
10811 	return 0;
10812 }
10813 
10814 static void bnxt_free_irq(struct bnxt *bp)
10815 {
10816 	struct bnxt_irq *irq;
10817 	int i;
10818 
10819 #ifdef CONFIG_RFS_ACCEL
10820 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
10821 	bp->dev->rx_cpu_rmap = NULL;
10822 #endif
10823 	if (!bp->irq_tbl || !bp->bnapi)
10824 		return;
10825 
10826 	for (i = 0; i < bp->cp_nr_rings; i++) {
10827 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10828 
10829 		irq = &bp->irq_tbl[map_idx];
10830 		if (irq->requested) {
10831 			if (irq->have_cpumask) {
10832 				irq_set_affinity_hint(irq->vector, NULL);
10833 				free_cpumask_var(irq->cpu_mask);
10834 				irq->have_cpumask = 0;
10835 			}
10836 			free_irq(irq->vector, bp->bnapi[i]);
10837 		}
10838 
10839 		irq->requested = 0;
10840 	}
10841 }
10842 
10843 static int bnxt_request_irq(struct bnxt *bp)
10844 {
10845 	int i, j, rc = 0;
10846 	unsigned long flags = 0;
10847 #ifdef CONFIG_RFS_ACCEL
10848 	struct cpu_rmap *rmap;
10849 #endif
10850 
10851 	rc = bnxt_setup_int_mode(bp);
10852 	if (rc) {
10853 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
10854 			   rc);
10855 		return rc;
10856 	}
10857 #ifdef CONFIG_RFS_ACCEL
10858 	rmap = bp->dev->rx_cpu_rmap;
10859 #endif
10860 	if (!(bp->flags & BNXT_FLAG_USING_MSIX))
10861 		flags = IRQF_SHARED;
10862 
10863 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
10864 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10865 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
10866 
10867 #ifdef CONFIG_RFS_ACCEL
10868 		if (rmap && bp->bnapi[i]->rx_ring) {
10869 			rc = irq_cpu_rmap_add(rmap, irq->vector);
10870 			if (rc)
10871 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
10872 					    j);
10873 			j++;
10874 		}
10875 #endif
10876 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
10877 				 bp->bnapi[i]);
10878 		if (rc)
10879 			break;
10880 
10881 		netif_napi_set_irq(&bp->bnapi[i]->napi, irq->vector);
10882 		irq->requested = 1;
10883 
10884 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
10885 			int numa_node = dev_to_node(&bp->pdev->dev);
10886 
10887 			irq->have_cpumask = 1;
10888 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
10889 					irq->cpu_mask);
10890 			rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
10891 			if (rc) {
10892 				netdev_warn(bp->dev,
10893 					    "Set affinity failed, IRQ = %d\n",
10894 					    irq->vector);
10895 				break;
10896 			}
10897 		}
10898 	}
10899 	return rc;
10900 }
10901 
10902 static void bnxt_del_napi(struct bnxt *bp)
10903 {
10904 	int i;
10905 
10906 	if (!bp->bnapi)
10907 		return;
10908 
10909 	for (i = 0; i < bp->rx_nr_rings; i++)
10910 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
10911 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
10912 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
10913 
10914 	for (i = 0; i < bp->cp_nr_rings; i++) {
10915 		struct bnxt_napi *bnapi = bp->bnapi[i];
10916 
10917 		__netif_napi_del(&bnapi->napi);
10918 	}
10919 	/* We called __netif_napi_del(), we need
10920 	 * to respect an RCU grace period before freeing napi structures.
10921 	 */
10922 	synchronize_net();
10923 }
10924 
10925 static void bnxt_init_napi(struct bnxt *bp)
10926 {
10927 	int i;
10928 	unsigned int cp_nr_rings = bp->cp_nr_rings;
10929 	struct bnxt_napi *bnapi;
10930 
10931 	if (bp->flags & BNXT_FLAG_USING_MSIX) {
10932 		int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
10933 
10934 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10935 			poll_fn = bnxt_poll_p5;
10936 		else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10937 			cp_nr_rings--;
10938 		for (i = 0; i < cp_nr_rings; i++) {
10939 			bnapi = bp->bnapi[i];
10940 			netif_napi_add(bp->dev, &bnapi->napi, poll_fn);
10941 		}
10942 		if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10943 			bnapi = bp->bnapi[cp_nr_rings];
10944 			netif_napi_add(bp->dev, &bnapi->napi,
10945 				       bnxt_poll_nitroa0);
10946 		}
10947 	} else {
10948 		bnapi = bp->bnapi[0];
10949 		netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll);
10950 	}
10951 }
10952 
10953 static void bnxt_disable_napi(struct bnxt *bp)
10954 {
10955 	int i;
10956 
10957 	if (!bp->bnapi ||
10958 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
10959 		return;
10960 
10961 	for (i = 0; i < bp->cp_nr_rings; i++) {
10962 		struct bnxt_napi *bnapi = bp->bnapi[i];
10963 		struct bnxt_cp_ring_info *cpr;
10964 
10965 		cpr = &bnapi->cp_ring;
10966 		if (bnapi->tx_fault)
10967 			cpr->sw_stats->tx.tx_resets++;
10968 		if (bnapi->in_reset)
10969 			cpr->sw_stats->rx.rx_resets++;
10970 		napi_disable(&bnapi->napi);
10971 		if (bnapi->rx_ring)
10972 			cancel_work_sync(&cpr->dim.work);
10973 	}
10974 }
10975 
10976 static void bnxt_enable_napi(struct bnxt *bp)
10977 {
10978 	int i;
10979 
10980 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
10981 	for (i = 0; i < bp->cp_nr_rings; i++) {
10982 		struct bnxt_napi *bnapi = bp->bnapi[i];
10983 		struct bnxt_cp_ring_info *cpr;
10984 
10985 		bnapi->tx_fault = 0;
10986 
10987 		cpr = &bnapi->cp_ring;
10988 		bnapi->in_reset = false;
10989 
10990 		if (bnapi->rx_ring) {
10991 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
10992 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
10993 		}
10994 		napi_enable(&bnapi->napi);
10995 	}
10996 }
10997 
10998 void bnxt_tx_disable(struct bnxt *bp)
10999 {
11000 	int i;
11001 	struct bnxt_tx_ring_info *txr;
11002 
11003 	if (bp->tx_ring) {
11004 		for (i = 0; i < bp->tx_nr_rings; i++) {
11005 			txr = &bp->tx_ring[i];
11006 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11007 		}
11008 	}
11009 	/* Make sure napi polls see @dev_state change */
11010 	synchronize_net();
11011 	/* Drop carrier first to prevent TX timeout */
11012 	netif_carrier_off(bp->dev);
11013 	/* Stop all TX queues */
11014 	netif_tx_disable(bp->dev);
11015 }
11016 
11017 void bnxt_tx_enable(struct bnxt *bp)
11018 {
11019 	int i;
11020 	struct bnxt_tx_ring_info *txr;
11021 
11022 	for (i = 0; i < bp->tx_nr_rings; i++) {
11023 		txr = &bp->tx_ring[i];
11024 		WRITE_ONCE(txr->dev_state, 0);
11025 	}
11026 	/* Make sure napi polls see @dev_state change */
11027 	synchronize_net();
11028 	netif_tx_wake_all_queues(bp->dev);
11029 	if (BNXT_LINK_IS_UP(bp))
11030 		netif_carrier_on(bp->dev);
11031 }
11032 
11033 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
11034 {
11035 	u8 active_fec = link_info->active_fec_sig_mode &
11036 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
11037 
11038 	switch (active_fec) {
11039 	default:
11040 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
11041 		return "None";
11042 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
11043 		return "Clause 74 BaseR";
11044 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
11045 		return "Clause 91 RS(528,514)";
11046 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
11047 		return "Clause 91 RS544_1XN";
11048 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
11049 		return "Clause 91 RS(544,514)";
11050 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
11051 		return "Clause 91 RS272_1XN";
11052 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
11053 		return "Clause 91 RS(272,257)";
11054 	}
11055 }
11056 
11057 void bnxt_report_link(struct bnxt *bp)
11058 {
11059 	if (BNXT_LINK_IS_UP(bp)) {
11060 		const char *signal = "";
11061 		const char *flow_ctrl;
11062 		const char *duplex;
11063 		u32 speed;
11064 		u16 fec;
11065 
11066 		netif_carrier_on(bp->dev);
11067 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
11068 		if (speed == SPEED_UNKNOWN) {
11069 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
11070 			return;
11071 		}
11072 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
11073 			duplex = "full";
11074 		else
11075 			duplex = "half";
11076 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
11077 			flow_ctrl = "ON - receive & transmit";
11078 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
11079 			flow_ctrl = "ON - transmit";
11080 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
11081 			flow_ctrl = "ON - receive";
11082 		else
11083 			flow_ctrl = "none";
11084 		if (bp->link_info.phy_qcfg_resp.option_flags &
11085 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
11086 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
11087 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
11088 			switch (sig_mode) {
11089 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
11090 				signal = "(NRZ) ";
11091 				break;
11092 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
11093 				signal = "(PAM4 56Gbps) ";
11094 				break;
11095 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
11096 				signal = "(PAM4 112Gbps) ";
11097 				break;
11098 			default:
11099 				break;
11100 			}
11101 		}
11102 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
11103 			    speed, signal, duplex, flow_ctrl);
11104 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
11105 			netdev_info(bp->dev, "EEE is %s\n",
11106 				    bp->eee.eee_active ? "active" :
11107 							 "not active");
11108 		fec = bp->link_info.fec_cfg;
11109 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
11110 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
11111 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
11112 				    bnxt_report_fec(&bp->link_info));
11113 	} else {
11114 		netif_carrier_off(bp->dev);
11115 		netdev_err(bp->dev, "NIC Link is Down\n");
11116 	}
11117 }
11118 
11119 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
11120 {
11121 	if (!resp->supported_speeds_auto_mode &&
11122 	    !resp->supported_speeds_force_mode &&
11123 	    !resp->supported_pam4_speeds_auto_mode &&
11124 	    !resp->supported_pam4_speeds_force_mode &&
11125 	    !resp->supported_speeds2_auto_mode &&
11126 	    !resp->supported_speeds2_force_mode)
11127 		return true;
11128 	return false;
11129 }
11130 
11131 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
11132 {
11133 	struct bnxt_link_info *link_info = &bp->link_info;
11134 	struct hwrm_port_phy_qcaps_output *resp;
11135 	struct hwrm_port_phy_qcaps_input *req;
11136 	int rc = 0;
11137 
11138 	if (bp->hwrm_spec_code < 0x10201)
11139 		return 0;
11140 
11141 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
11142 	if (rc)
11143 		return rc;
11144 
11145 	resp = hwrm_req_hold(bp, req);
11146 	rc = hwrm_req_send(bp, req);
11147 	if (rc)
11148 		goto hwrm_phy_qcaps_exit;
11149 
11150 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
11151 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
11152 		struct ethtool_keee *eee = &bp->eee;
11153 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
11154 
11155 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
11156 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
11157 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11158 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11159 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11160 	}
11161 
11162 	if (bp->hwrm_spec_code >= 0x10a01) {
11163 		if (bnxt_phy_qcaps_no_speed(resp)) {
11164 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11165 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11166 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11167 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11168 			netdev_info(bp->dev, "Ethernet link enabled\n");
11169 			/* Phy re-enabled, reprobe the speeds */
11170 			link_info->support_auto_speeds = 0;
11171 			link_info->support_pam4_auto_speeds = 0;
11172 			link_info->support_auto_speeds2 = 0;
11173 		}
11174 	}
11175 	if (resp->supported_speeds_auto_mode)
11176 		link_info->support_auto_speeds =
11177 			le16_to_cpu(resp->supported_speeds_auto_mode);
11178 	if (resp->supported_pam4_speeds_auto_mode)
11179 		link_info->support_pam4_auto_speeds =
11180 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11181 	if (resp->supported_speeds2_auto_mode)
11182 		link_info->support_auto_speeds2 =
11183 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11184 
11185 	bp->port_count = resp->port_cnt;
11186 
11187 hwrm_phy_qcaps_exit:
11188 	hwrm_req_drop(bp, req);
11189 	return rc;
11190 }
11191 
11192 static bool bnxt_support_dropped(u16 advertising, u16 supported)
11193 {
11194 	u16 diff = advertising ^ supported;
11195 
11196 	return ((supported | diff) != supported);
11197 }
11198 
11199 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
11200 {
11201 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
11202 
11203 	/* Check if any advertised speeds are no longer supported. The caller
11204 	 * holds the link_lock mutex, so we can modify link_info settings.
11205 	 */
11206 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11207 		if (bnxt_support_dropped(link_info->advertising,
11208 					 link_info->support_auto_speeds2)) {
11209 			link_info->advertising = link_info->support_auto_speeds2;
11210 			return true;
11211 		}
11212 		return false;
11213 	}
11214 	if (bnxt_support_dropped(link_info->advertising,
11215 				 link_info->support_auto_speeds)) {
11216 		link_info->advertising = link_info->support_auto_speeds;
11217 		return true;
11218 	}
11219 	if (bnxt_support_dropped(link_info->advertising_pam4,
11220 				 link_info->support_pam4_auto_speeds)) {
11221 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
11222 		return true;
11223 	}
11224 	return false;
11225 }
11226 
11227 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
11228 {
11229 	struct bnxt_link_info *link_info = &bp->link_info;
11230 	struct hwrm_port_phy_qcfg_output *resp;
11231 	struct hwrm_port_phy_qcfg_input *req;
11232 	u8 link_state = link_info->link_state;
11233 	bool support_changed;
11234 	int rc;
11235 
11236 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
11237 	if (rc)
11238 		return rc;
11239 
11240 	resp = hwrm_req_hold(bp, req);
11241 	rc = hwrm_req_send(bp, req);
11242 	if (rc) {
11243 		hwrm_req_drop(bp, req);
11244 		if (BNXT_VF(bp) && rc == -ENODEV) {
11245 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
11246 			rc = 0;
11247 		}
11248 		return rc;
11249 	}
11250 
11251 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
11252 	link_info->phy_link_status = resp->link;
11253 	link_info->duplex = resp->duplex_cfg;
11254 	if (bp->hwrm_spec_code >= 0x10800)
11255 		link_info->duplex = resp->duplex_state;
11256 	link_info->pause = resp->pause;
11257 	link_info->auto_mode = resp->auto_mode;
11258 	link_info->auto_pause_setting = resp->auto_pause;
11259 	link_info->lp_pause = resp->link_partner_adv_pause;
11260 	link_info->force_pause_setting = resp->force_pause;
11261 	link_info->duplex_setting = resp->duplex_cfg;
11262 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
11263 		link_info->link_speed = le16_to_cpu(resp->link_speed);
11264 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
11265 			link_info->active_lanes = resp->active_lanes;
11266 	} else {
11267 		link_info->link_speed = 0;
11268 		link_info->active_lanes = 0;
11269 	}
11270 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
11271 	link_info->force_pam4_link_speed =
11272 		le16_to_cpu(resp->force_pam4_link_speed);
11273 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
11274 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
11275 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
11276 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
11277 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
11278 	link_info->auto_pam4_link_speeds =
11279 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
11280 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
11281 	link_info->lp_auto_link_speeds =
11282 		le16_to_cpu(resp->link_partner_adv_speeds);
11283 	link_info->lp_auto_pam4_link_speeds =
11284 		resp->link_partner_pam4_adv_speeds;
11285 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
11286 	link_info->phy_ver[0] = resp->phy_maj;
11287 	link_info->phy_ver[1] = resp->phy_min;
11288 	link_info->phy_ver[2] = resp->phy_bld;
11289 	link_info->media_type = resp->media_type;
11290 	link_info->phy_type = resp->phy_type;
11291 	link_info->transceiver = resp->xcvr_pkg_type;
11292 	link_info->phy_addr = resp->eee_config_phy_addr &
11293 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
11294 	link_info->module_status = resp->module_status;
11295 
11296 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
11297 		struct ethtool_keee *eee = &bp->eee;
11298 		u16 fw_speeds;
11299 
11300 		eee->eee_active = 0;
11301 		if (resp->eee_config_phy_addr &
11302 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
11303 			eee->eee_active = 1;
11304 			fw_speeds = le16_to_cpu(
11305 				resp->link_partner_adv_eee_link_speed_mask);
11306 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
11307 		}
11308 
11309 		/* Pull initial EEE config */
11310 		if (!chng_link_state) {
11311 			if (resp->eee_config_phy_addr &
11312 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
11313 				eee->eee_enabled = 1;
11314 
11315 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
11316 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
11317 
11318 			if (resp->eee_config_phy_addr &
11319 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
11320 				__le32 tmr;
11321 
11322 				eee->tx_lpi_enabled = 1;
11323 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
11324 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
11325 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
11326 			}
11327 		}
11328 	}
11329 
11330 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
11331 	if (bp->hwrm_spec_code >= 0x10504) {
11332 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
11333 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
11334 	}
11335 	/* TODO: need to add more logic to report VF link */
11336 	if (chng_link_state) {
11337 		if (link_info->phy_link_status == BNXT_LINK_LINK)
11338 			link_info->link_state = BNXT_LINK_STATE_UP;
11339 		else
11340 			link_info->link_state = BNXT_LINK_STATE_DOWN;
11341 		if (link_state != link_info->link_state)
11342 			bnxt_report_link(bp);
11343 	} else {
11344 		/* always link down if not require to update link state */
11345 		link_info->link_state = BNXT_LINK_STATE_DOWN;
11346 	}
11347 	hwrm_req_drop(bp, req);
11348 
11349 	if (!BNXT_PHY_CFG_ABLE(bp))
11350 		return 0;
11351 
11352 	support_changed = bnxt_support_speed_dropped(link_info);
11353 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
11354 		bnxt_hwrm_set_link_setting(bp, true, false);
11355 	return 0;
11356 }
11357 
11358 static void bnxt_get_port_module_status(struct bnxt *bp)
11359 {
11360 	struct bnxt_link_info *link_info = &bp->link_info;
11361 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
11362 	u8 module_status;
11363 
11364 	if (bnxt_update_link(bp, true))
11365 		return;
11366 
11367 	module_status = link_info->module_status;
11368 	switch (module_status) {
11369 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
11370 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
11371 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
11372 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
11373 			    bp->pf.port_id);
11374 		if (bp->hwrm_spec_code >= 0x10201) {
11375 			netdev_warn(bp->dev, "Module part number %s\n",
11376 				    resp->phy_vendor_partnumber);
11377 		}
11378 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
11379 			netdev_warn(bp->dev, "TX is disabled\n");
11380 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
11381 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
11382 	}
11383 }
11384 
11385 static void
11386 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11387 {
11388 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
11389 		if (bp->hwrm_spec_code >= 0x10201)
11390 			req->auto_pause =
11391 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
11392 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11393 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
11394 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11395 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
11396 		req->enables |=
11397 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11398 	} else {
11399 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11400 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
11401 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11402 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
11403 		req->enables |=
11404 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
11405 		if (bp->hwrm_spec_code >= 0x10201) {
11406 			req->auto_pause = req->force_pause;
11407 			req->enables |= cpu_to_le32(
11408 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11409 		}
11410 	}
11411 }
11412 
11413 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11414 {
11415 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
11416 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
11417 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11418 			req->enables |=
11419 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
11420 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
11421 		} else if (bp->link_info.advertising) {
11422 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
11423 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
11424 		}
11425 		if (bp->link_info.advertising_pam4) {
11426 			req->enables |=
11427 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
11428 			req->auto_link_pam4_speed_mask =
11429 				cpu_to_le16(bp->link_info.advertising_pam4);
11430 		}
11431 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
11432 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
11433 	} else {
11434 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
11435 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11436 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
11437 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
11438 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
11439 				   (u32)bp->link_info.req_link_speed);
11440 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
11441 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11442 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
11443 		} else {
11444 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11445 		}
11446 	}
11447 
11448 	/* tell chimp that the setting takes effect immediately */
11449 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
11450 }
11451 
11452 int bnxt_hwrm_set_pause(struct bnxt *bp)
11453 {
11454 	struct hwrm_port_phy_cfg_input *req;
11455 	int rc;
11456 
11457 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11458 	if (rc)
11459 		return rc;
11460 
11461 	bnxt_hwrm_set_pause_common(bp, req);
11462 
11463 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
11464 	    bp->link_info.force_link_chng)
11465 		bnxt_hwrm_set_link_common(bp, req);
11466 
11467 	rc = hwrm_req_send(bp, req);
11468 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
11469 		/* since changing of pause setting doesn't trigger any link
11470 		 * change event, the driver needs to update the current pause
11471 		 * result upon successfully return of the phy_cfg command
11472 		 */
11473 		bp->link_info.pause =
11474 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
11475 		bp->link_info.auto_pause_setting = 0;
11476 		if (!bp->link_info.force_link_chng)
11477 			bnxt_report_link(bp);
11478 	}
11479 	bp->link_info.force_link_chng = false;
11480 	return rc;
11481 }
11482 
11483 static void bnxt_hwrm_set_eee(struct bnxt *bp,
11484 			      struct hwrm_port_phy_cfg_input *req)
11485 {
11486 	struct ethtool_keee *eee = &bp->eee;
11487 
11488 	if (eee->eee_enabled) {
11489 		u16 eee_speeds;
11490 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
11491 
11492 		if (eee->tx_lpi_enabled)
11493 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
11494 		else
11495 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
11496 
11497 		req->flags |= cpu_to_le32(flags);
11498 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
11499 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
11500 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
11501 	} else {
11502 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
11503 	}
11504 }
11505 
11506 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
11507 {
11508 	struct hwrm_port_phy_cfg_input *req;
11509 	int rc;
11510 
11511 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11512 	if (rc)
11513 		return rc;
11514 
11515 	if (set_pause)
11516 		bnxt_hwrm_set_pause_common(bp, req);
11517 
11518 	bnxt_hwrm_set_link_common(bp, req);
11519 
11520 	if (set_eee)
11521 		bnxt_hwrm_set_eee(bp, req);
11522 	return hwrm_req_send(bp, req);
11523 }
11524 
11525 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
11526 {
11527 	struct hwrm_port_phy_cfg_input *req;
11528 	int rc;
11529 
11530 	if (!BNXT_SINGLE_PF(bp))
11531 		return 0;
11532 
11533 	if (pci_num_vf(bp->pdev) &&
11534 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
11535 		return 0;
11536 
11537 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11538 	if (rc)
11539 		return rc;
11540 
11541 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
11542 	rc = hwrm_req_send(bp, req);
11543 	if (!rc) {
11544 		mutex_lock(&bp->link_lock);
11545 		/* Device is not obliged link down in certain scenarios, even
11546 		 * when forced. Setting the state unknown is consistent with
11547 		 * driver startup and will force link state to be reported
11548 		 * during subsequent open based on PORT_PHY_QCFG.
11549 		 */
11550 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
11551 		mutex_unlock(&bp->link_lock);
11552 	}
11553 	return rc;
11554 }
11555 
11556 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
11557 {
11558 #ifdef CONFIG_TEE_BNXT_FW
11559 	int rc = tee_bnxt_fw_load();
11560 
11561 	if (rc)
11562 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
11563 
11564 	return rc;
11565 #else
11566 	netdev_err(bp->dev, "OP-TEE not supported\n");
11567 	return -ENODEV;
11568 #endif
11569 }
11570 
11571 static int bnxt_try_recover_fw(struct bnxt *bp)
11572 {
11573 	if (bp->fw_health && bp->fw_health->status_reliable) {
11574 		int retry = 0, rc;
11575 		u32 sts;
11576 
11577 		do {
11578 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
11579 			rc = bnxt_hwrm_poll(bp);
11580 			if (!BNXT_FW_IS_BOOTING(sts) &&
11581 			    !BNXT_FW_IS_RECOVERING(sts))
11582 				break;
11583 			retry++;
11584 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
11585 
11586 		if (!BNXT_FW_IS_HEALTHY(sts)) {
11587 			netdev_err(bp->dev,
11588 				   "Firmware not responding, status: 0x%x\n",
11589 				   sts);
11590 			rc = -ENODEV;
11591 		}
11592 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
11593 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
11594 			return bnxt_fw_reset_via_optee(bp);
11595 		}
11596 		return rc;
11597 	}
11598 
11599 	return -ENODEV;
11600 }
11601 
11602 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
11603 {
11604 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11605 
11606 	if (!BNXT_NEW_RM(bp))
11607 		return; /* no resource reservations required */
11608 
11609 	hw_resc->resv_cp_rings = 0;
11610 	hw_resc->resv_stat_ctxs = 0;
11611 	hw_resc->resv_irqs = 0;
11612 	hw_resc->resv_tx_rings = 0;
11613 	hw_resc->resv_rx_rings = 0;
11614 	hw_resc->resv_hw_ring_grps = 0;
11615 	hw_resc->resv_vnics = 0;
11616 	hw_resc->resv_rsscos_ctxs = 0;
11617 	if (!fw_reset) {
11618 		bp->tx_nr_rings = 0;
11619 		bp->rx_nr_rings = 0;
11620 	}
11621 }
11622 
11623 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
11624 {
11625 	int rc;
11626 
11627 	if (!BNXT_NEW_RM(bp))
11628 		return 0; /* no resource reservations required */
11629 
11630 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
11631 	if (rc)
11632 		netdev_err(bp->dev, "resc_qcaps failed\n");
11633 
11634 	bnxt_clear_reservations(bp, fw_reset);
11635 
11636 	return rc;
11637 }
11638 
11639 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
11640 {
11641 	struct hwrm_func_drv_if_change_output *resp;
11642 	struct hwrm_func_drv_if_change_input *req;
11643 	bool fw_reset = !bp->irq_tbl;
11644 	bool resc_reinit = false;
11645 	int rc, retry = 0;
11646 	u32 flags = 0;
11647 
11648 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
11649 		return 0;
11650 
11651 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
11652 	if (rc)
11653 		return rc;
11654 
11655 	if (up)
11656 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
11657 	resp = hwrm_req_hold(bp, req);
11658 
11659 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
11660 	while (retry < BNXT_FW_IF_RETRY) {
11661 		rc = hwrm_req_send(bp, req);
11662 		if (rc != -EAGAIN)
11663 			break;
11664 
11665 		msleep(50);
11666 		retry++;
11667 	}
11668 
11669 	if (rc == -EAGAIN) {
11670 		hwrm_req_drop(bp, req);
11671 		return rc;
11672 	} else if (!rc) {
11673 		flags = le32_to_cpu(resp->flags);
11674 	} else if (up) {
11675 		rc = bnxt_try_recover_fw(bp);
11676 		fw_reset = true;
11677 	}
11678 	hwrm_req_drop(bp, req);
11679 	if (rc)
11680 		return rc;
11681 
11682 	if (!up) {
11683 		bnxt_inv_fw_health_reg(bp);
11684 		return 0;
11685 	}
11686 
11687 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
11688 		resc_reinit = true;
11689 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
11690 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
11691 		fw_reset = true;
11692 	else
11693 		bnxt_remap_fw_health_regs(bp);
11694 
11695 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
11696 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
11697 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
11698 		return -ENODEV;
11699 	}
11700 	if (resc_reinit || fw_reset) {
11701 		if (fw_reset) {
11702 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11703 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11704 				bnxt_ulp_irq_stop(bp);
11705 			bnxt_free_ctx_mem(bp);
11706 			bnxt_dcb_free(bp);
11707 			rc = bnxt_fw_init_one(bp);
11708 			if (rc) {
11709 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11710 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
11711 				return rc;
11712 			}
11713 			bnxt_clear_int_mode(bp);
11714 			rc = bnxt_init_int_mode(bp);
11715 			if (rc) {
11716 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11717 				netdev_err(bp->dev, "init int mode failed\n");
11718 				return rc;
11719 			}
11720 		}
11721 		rc = bnxt_cancel_reservations(bp, fw_reset);
11722 	}
11723 	return rc;
11724 }
11725 
11726 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
11727 {
11728 	struct hwrm_port_led_qcaps_output *resp;
11729 	struct hwrm_port_led_qcaps_input *req;
11730 	struct bnxt_pf_info *pf = &bp->pf;
11731 	int rc;
11732 
11733 	bp->num_leds = 0;
11734 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
11735 		return 0;
11736 
11737 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
11738 	if (rc)
11739 		return rc;
11740 
11741 	req->port_id = cpu_to_le16(pf->port_id);
11742 	resp = hwrm_req_hold(bp, req);
11743 	rc = hwrm_req_send(bp, req);
11744 	if (rc) {
11745 		hwrm_req_drop(bp, req);
11746 		return rc;
11747 	}
11748 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
11749 		int i;
11750 
11751 		bp->num_leds = resp->num_leds;
11752 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
11753 						 bp->num_leds);
11754 		for (i = 0; i < bp->num_leds; i++) {
11755 			struct bnxt_led_info *led = &bp->leds[i];
11756 			__le16 caps = led->led_state_caps;
11757 
11758 			if (!led->led_group_id ||
11759 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
11760 				bp->num_leds = 0;
11761 				break;
11762 			}
11763 		}
11764 	}
11765 	hwrm_req_drop(bp, req);
11766 	return 0;
11767 }
11768 
11769 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
11770 {
11771 	struct hwrm_wol_filter_alloc_output *resp;
11772 	struct hwrm_wol_filter_alloc_input *req;
11773 	int rc;
11774 
11775 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
11776 	if (rc)
11777 		return rc;
11778 
11779 	req->port_id = cpu_to_le16(bp->pf.port_id);
11780 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
11781 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
11782 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
11783 
11784 	resp = hwrm_req_hold(bp, req);
11785 	rc = hwrm_req_send(bp, req);
11786 	if (!rc)
11787 		bp->wol_filter_id = resp->wol_filter_id;
11788 	hwrm_req_drop(bp, req);
11789 	return rc;
11790 }
11791 
11792 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
11793 {
11794 	struct hwrm_wol_filter_free_input *req;
11795 	int rc;
11796 
11797 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
11798 	if (rc)
11799 		return rc;
11800 
11801 	req->port_id = cpu_to_le16(bp->pf.port_id);
11802 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
11803 	req->wol_filter_id = bp->wol_filter_id;
11804 
11805 	return hwrm_req_send(bp, req);
11806 }
11807 
11808 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
11809 {
11810 	struct hwrm_wol_filter_qcfg_output *resp;
11811 	struct hwrm_wol_filter_qcfg_input *req;
11812 	u16 next_handle = 0;
11813 	int rc;
11814 
11815 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
11816 	if (rc)
11817 		return rc;
11818 
11819 	req->port_id = cpu_to_le16(bp->pf.port_id);
11820 	req->handle = cpu_to_le16(handle);
11821 	resp = hwrm_req_hold(bp, req);
11822 	rc = hwrm_req_send(bp, req);
11823 	if (!rc) {
11824 		next_handle = le16_to_cpu(resp->next_handle);
11825 		if (next_handle != 0) {
11826 			if (resp->wol_type ==
11827 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
11828 				bp->wol = 1;
11829 				bp->wol_filter_id = resp->wol_filter_id;
11830 			}
11831 		}
11832 	}
11833 	hwrm_req_drop(bp, req);
11834 	return next_handle;
11835 }
11836 
11837 static void bnxt_get_wol_settings(struct bnxt *bp)
11838 {
11839 	u16 handle = 0;
11840 
11841 	bp->wol = 0;
11842 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
11843 		return;
11844 
11845 	do {
11846 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
11847 	} while (handle && handle != 0xffff);
11848 }
11849 
11850 static bool bnxt_eee_config_ok(struct bnxt *bp)
11851 {
11852 	struct ethtool_keee *eee = &bp->eee;
11853 	struct bnxt_link_info *link_info = &bp->link_info;
11854 
11855 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
11856 		return true;
11857 
11858 	if (eee->eee_enabled) {
11859 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
11860 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
11861 
11862 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
11863 
11864 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
11865 			eee->eee_enabled = 0;
11866 			return false;
11867 		}
11868 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
11869 			linkmode_and(eee->advertised, advertising,
11870 				     eee->supported);
11871 			return false;
11872 		}
11873 	}
11874 	return true;
11875 }
11876 
11877 static int bnxt_update_phy_setting(struct bnxt *bp)
11878 {
11879 	int rc;
11880 	bool update_link = false;
11881 	bool update_pause = false;
11882 	bool update_eee = false;
11883 	struct bnxt_link_info *link_info = &bp->link_info;
11884 
11885 	rc = bnxt_update_link(bp, true);
11886 	if (rc) {
11887 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
11888 			   rc);
11889 		return rc;
11890 	}
11891 	if (!BNXT_SINGLE_PF(bp))
11892 		return 0;
11893 
11894 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
11895 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
11896 	    link_info->req_flow_ctrl)
11897 		update_pause = true;
11898 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
11899 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
11900 		update_pause = true;
11901 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
11902 		if (BNXT_AUTO_MODE(link_info->auto_mode))
11903 			update_link = true;
11904 		if (bnxt_force_speed_updated(link_info))
11905 			update_link = true;
11906 		if (link_info->req_duplex != link_info->duplex_setting)
11907 			update_link = true;
11908 	} else {
11909 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
11910 			update_link = true;
11911 		if (bnxt_auto_speed_updated(link_info))
11912 			update_link = true;
11913 	}
11914 
11915 	/* The last close may have shutdown the link, so need to call
11916 	 * PHY_CFG to bring it back up.
11917 	 */
11918 	if (!BNXT_LINK_IS_UP(bp))
11919 		update_link = true;
11920 
11921 	if (!bnxt_eee_config_ok(bp))
11922 		update_eee = true;
11923 
11924 	if (update_link)
11925 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
11926 	else if (update_pause)
11927 		rc = bnxt_hwrm_set_pause(bp);
11928 	if (rc) {
11929 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
11930 			   rc);
11931 		return rc;
11932 	}
11933 
11934 	return rc;
11935 }
11936 
11937 /* Common routine to pre-map certain register block to different GRC window.
11938  * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
11939  * in PF and 3 windows in VF that can be customized to map in different
11940  * register blocks.
11941  */
11942 static void bnxt_preset_reg_win(struct bnxt *bp)
11943 {
11944 	if (BNXT_PF(bp)) {
11945 		/* CAG registers map to GRC window #4 */
11946 		writel(BNXT_CAG_REG_BASE,
11947 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
11948 	}
11949 }
11950 
11951 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
11952 
11953 static int bnxt_reinit_after_abort(struct bnxt *bp)
11954 {
11955 	int rc;
11956 
11957 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11958 		return -EBUSY;
11959 
11960 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
11961 		return -ENODEV;
11962 
11963 	rc = bnxt_fw_init_one(bp);
11964 	if (!rc) {
11965 		bnxt_clear_int_mode(bp);
11966 		rc = bnxt_init_int_mode(bp);
11967 		if (!rc) {
11968 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
11969 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11970 		}
11971 	}
11972 	return rc;
11973 }
11974 
11975 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
11976 {
11977 	struct bnxt_ntuple_filter *ntp_fltr;
11978 	struct bnxt_l2_filter *l2_fltr;
11979 
11980 	if (list_empty(&fltr->list))
11981 		return;
11982 
11983 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
11984 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
11985 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
11986 		atomic_inc(&l2_fltr->refcnt);
11987 		ntp_fltr->l2_fltr = l2_fltr;
11988 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
11989 			bnxt_del_ntp_filter(bp, ntp_fltr);
11990 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
11991 				   fltr->sw_id);
11992 		}
11993 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
11994 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
11995 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
11996 			bnxt_del_l2_filter(bp, l2_fltr);
11997 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
11998 				   fltr->sw_id);
11999 		}
12000 	}
12001 }
12002 
12003 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
12004 {
12005 	struct bnxt_filter_base *usr_fltr, *tmp;
12006 
12007 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
12008 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
12009 }
12010 
12011 static int bnxt_set_xps_mapping(struct bnxt *bp)
12012 {
12013 	int numa_node = dev_to_node(&bp->pdev->dev);
12014 	unsigned int q_idx, map_idx, cpu, i;
12015 	const struct cpumask *cpu_mask_ptr;
12016 	int nr_cpus = num_online_cpus();
12017 	cpumask_t *q_map;
12018 	int rc = 0;
12019 
12020 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
12021 	if (!q_map)
12022 		return -ENOMEM;
12023 
12024 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
12025 	 * Each TC has the same number of TX queues. The nth TX queue for each
12026 	 * TC will have the same CPU mask.
12027 	 */
12028 	for (i = 0; i < nr_cpus; i++) {
12029 		map_idx = i % bp->tx_nr_rings_per_tc;
12030 		cpu = cpumask_local_spread(i, numa_node);
12031 		cpu_mask_ptr = get_cpu_mask(cpu);
12032 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
12033 	}
12034 
12035 	/* Register CPU mask for each TX queue except the ones marked for XDP */
12036 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
12037 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
12038 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
12039 		if (rc) {
12040 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
12041 				    q_idx);
12042 			break;
12043 		}
12044 	}
12045 
12046 	kfree(q_map);
12047 
12048 	return rc;
12049 }
12050 
12051 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12052 {
12053 	int rc = 0;
12054 
12055 	bnxt_preset_reg_win(bp);
12056 	netif_carrier_off(bp->dev);
12057 	if (irq_re_init) {
12058 		/* Reserve rings now if none were reserved at driver probe. */
12059 		rc = bnxt_init_dflt_ring_mode(bp);
12060 		if (rc) {
12061 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
12062 			return rc;
12063 		}
12064 	}
12065 	rc = bnxt_reserve_rings(bp, irq_re_init);
12066 	if (rc)
12067 		return rc;
12068 	if ((bp->flags & BNXT_FLAG_RFS) &&
12069 	    !(bp->flags & BNXT_FLAG_USING_MSIX)) {
12070 		/* disable RFS if falling back to INTA */
12071 		bp->dev->hw_features &= ~NETIF_F_NTUPLE;
12072 		bp->flags &= ~BNXT_FLAG_RFS;
12073 	}
12074 
12075 	rc = bnxt_alloc_mem(bp, irq_re_init);
12076 	if (rc) {
12077 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12078 		goto open_err_free_mem;
12079 	}
12080 
12081 	if (irq_re_init) {
12082 		bnxt_init_napi(bp);
12083 		rc = bnxt_request_irq(bp);
12084 		if (rc) {
12085 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
12086 			goto open_err_irq;
12087 		}
12088 	}
12089 
12090 	rc = bnxt_init_nic(bp, irq_re_init);
12091 	if (rc) {
12092 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12093 		goto open_err_irq;
12094 	}
12095 
12096 	bnxt_enable_napi(bp);
12097 	bnxt_debug_dev_init(bp);
12098 
12099 	if (link_re_init) {
12100 		mutex_lock(&bp->link_lock);
12101 		rc = bnxt_update_phy_setting(bp);
12102 		mutex_unlock(&bp->link_lock);
12103 		if (rc) {
12104 			netdev_warn(bp->dev, "failed to update phy settings\n");
12105 			if (BNXT_SINGLE_PF(bp)) {
12106 				bp->link_info.phy_retry = true;
12107 				bp->link_info.phy_retry_expires =
12108 					jiffies + 5 * HZ;
12109 			}
12110 		}
12111 	}
12112 
12113 	if (irq_re_init) {
12114 		udp_tunnel_nic_reset_ntf(bp->dev);
12115 		rc = bnxt_set_xps_mapping(bp);
12116 		if (rc)
12117 			netdev_warn(bp->dev, "failed to set xps mapping\n");
12118 	}
12119 
12120 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
12121 		if (!static_key_enabled(&bnxt_xdp_locking_key))
12122 			static_branch_enable(&bnxt_xdp_locking_key);
12123 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
12124 		static_branch_disable(&bnxt_xdp_locking_key);
12125 	}
12126 	set_bit(BNXT_STATE_OPEN, &bp->state);
12127 	bnxt_enable_int(bp);
12128 	/* Enable TX queues */
12129 	bnxt_tx_enable(bp);
12130 	mod_timer(&bp->timer, jiffies + bp->current_interval);
12131 	/* Poll link status and check for SFP+ module status */
12132 	mutex_lock(&bp->link_lock);
12133 	bnxt_get_port_module_status(bp);
12134 	mutex_unlock(&bp->link_lock);
12135 
12136 	/* VF-reps may need to be re-opened after the PF is re-opened */
12137 	if (BNXT_PF(bp))
12138 		bnxt_vf_reps_open(bp);
12139 	if (bp->ptp_cfg)
12140 		atomic_set(&bp->ptp_cfg->tx_avail, BNXT_MAX_TX_TS);
12141 	bnxt_ptp_init_rtc(bp, true);
12142 	bnxt_ptp_cfg_tstamp_filters(bp);
12143 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12144 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
12145 	bnxt_cfg_usr_fltrs(bp);
12146 	return 0;
12147 
12148 open_err_irq:
12149 	bnxt_del_napi(bp);
12150 
12151 open_err_free_mem:
12152 	bnxt_free_skbs(bp);
12153 	bnxt_free_irq(bp);
12154 	bnxt_free_mem(bp, true);
12155 	return rc;
12156 }
12157 
12158 /* rtnl_lock held */
12159 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12160 {
12161 	int rc = 0;
12162 
12163 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12164 		rc = -EIO;
12165 	if (!rc)
12166 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12167 	if (rc) {
12168 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12169 		dev_close(bp->dev);
12170 	}
12171 	return rc;
12172 }
12173 
12174 /* rtnl_lock held, open the NIC half way by allocating all resources, but
12175  * NAPI, IRQ, and TX are not enabled.  This is mainly used for offline
12176  * self tests.
12177  */
12178 int bnxt_half_open_nic(struct bnxt *bp)
12179 {
12180 	int rc = 0;
12181 
12182 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12183 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
12184 		rc = -ENODEV;
12185 		goto half_open_err;
12186 	}
12187 
12188 	rc = bnxt_alloc_mem(bp, true);
12189 	if (rc) {
12190 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12191 		goto half_open_err;
12192 	}
12193 	bnxt_init_napi(bp);
12194 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12195 	rc = bnxt_init_nic(bp, true);
12196 	if (rc) {
12197 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12198 		bnxt_del_napi(bp);
12199 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12200 		goto half_open_err;
12201 	}
12202 	return 0;
12203 
12204 half_open_err:
12205 	bnxt_free_skbs(bp);
12206 	bnxt_free_mem(bp, true);
12207 	dev_close(bp->dev);
12208 	return rc;
12209 }
12210 
12211 /* rtnl_lock held, this call can only be made after a previous successful
12212  * call to bnxt_half_open_nic().
12213  */
12214 void bnxt_half_close_nic(struct bnxt *bp)
12215 {
12216 	bnxt_hwrm_resource_free(bp, false, true);
12217 	bnxt_del_napi(bp);
12218 	bnxt_free_skbs(bp);
12219 	bnxt_free_mem(bp, true);
12220 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12221 }
12222 
12223 void bnxt_reenable_sriov(struct bnxt *bp)
12224 {
12225 	if (BNXT_PF(bp)) {
12226 		struct bnxt_pf_info *pf = &bp->pf;
12227 		int n = pf->active_vfs;
12228 
12229 		if (n)
12230 			bnxt_cfg_hw_sriov(bp, &n, true);
12231 	}
12232 }
12233 
12234 static int bnxt_open(struct net_device *dev)
12235 {
12236 	struct bnxt *bp = netdev_priv(dev);
12237 	int rc;
12238 
12239 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12240 		rc = bnxt_reinit_after_abort(bp);
12241 		if (rc) {
12242 			if (rc == -EBUSY)
12243 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
12244 			else
12245 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
12246 			return -ENODEV;
12247 		}
12248 	}
12249 
12250 	rc = bnxt_hwrm_if_change(bp, true);
12251 	if (rc)
12252 		return rc;
12253 
12254 	rc = __bnxt_open_nic(bp, true, true);
12255 	if (rc) {
12256 		bnxt_hwrm_if_change(bp, false);
12257 	} else {
12258 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
12259 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12260 				bnxt_queue_sp_work(bp,
12261 						   BNXT_RESTART_ULP_SP_EVENT);
12262 		}
12263 	}
12264 
12265 	return rc;
12266 }
12267 
12268 static bool bnxt_drv_busy(struct bnxt *bp)
12269 {
12270 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
12271 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
12272 }
12273 
12274 static void bnxt_get_ring_stats(struct bnxt *bp,
12275 				struct rtnl_link_stats64 *stats);
12276 
12277 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
12278 			     bool link_re_init)
12279 {
12280 	/* Close the VF-reps before closing PF */
12281 	if (BNXT_PF(bp))
12282 		bnxt_vf_reps_close(bp);
12283 
12284 	/* Change device state to avoid TX queue wake up's */
12285 	bnxt_tx_disable(bp);
12286 
12287 	clear_bit(BNXT_STATE_OPEN, &bp->state);
12288 	smp_mb__after_atomic();
12289 	while (bnxt_drv_busy(bp))
12290 		msleep(20);
12291 
12292 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12293 		bnxt_clear_rss_ctxs(bp, false);
12294 	/* Flush rings and disable interrupts */
12295 	bnxt_shutdown_nic(bp, irq_re_init);
12296 
12297 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
12298 
12299 	bnxt_debug_dev_exit(bp);
12300 	bnxt_disable_napi(bp);
12301 	del_timer_sync(&bp->timer);
12302 	bnxt_free_skbs(bp);
12303 
12304 	/* Save ring stats before shutdown */
12305 	if (bp->bnapi && irq_re_init) {
12306 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
12307 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
12308 	}
12309 	if (irq_re_init) {
12310 		bnxt_free_irq(bp);
12311 		bnxt_del_napi(bp);
12312 	}
12313 	bnxt_free_mem(bp, irq_re_init);
12314 }
12315 
12316 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12317 {
12318 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12319 		/* If we get here, it means firmware reset is in progress
12320 		 * while we are trying to close.  We can safely proceed with
12321 		 * the close because we are holding rtnl_lock().  Some firmware
12322 		 * messages may fail as we proceed to close.  We set the
12323 		 * ABORT_ERR flag here so that the FW reset thread will later
12324 		 * abort when it gets the rtnl_lock() and sees the flag.
12325 		 */
12326 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
12327 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12328 	}
12329 
12330 #ifdef CONFIG_BNXT_SRIOV
12331 	if (bp->sriov_cfg) {
12332 		int rc;
12333 
12334 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
12335 						      !bp->sriov_cfg,
12336 						      BNXT_SRIOV_CFG_WAIT_TMO);
12337 		if (!rc)
12338 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
12339 		else if (rc < 0)
12340 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
12341 	}
12342 #endif
12343 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
12344 }
12345 
12346 static int bnxt_close(struct net_device *dev)
12347 {
12348 	struct bnxt *bp = netdev_priv(dev);
12349 
12350 	bnxt_close_nic(bp, true, true);
12351 	bnxt_hwrm_shutdown_link(bp);
12352 	bnxt_hwrm_if_change(bp, false);
12353 	return 0;
12354 }
12355 
12356 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
12357 				   u16 *val)
12358 {
12359 	struct hwrm_port_phy_mdio_read_output *resp;
12360 	struct hwrm_port_phy_mdio_read_input *req;
12361 	int rc;
12362 
12363 	if (bp->hwrm_spec_code < 0x10a00)
12364 		return -EOPNOTSUPP;
12365 
12366 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
12367 	if (rc)
12368 		return rc;
12369 
12370 	req->port_id = cpu_to_le16(bp->pf.port_id);
12371 	req->phy_addr = phy_addr;
12372 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12373 	if (mdio_phy_id_is_c45(phy_addr)) {
12374 		req->cl45_mdio = 1;
12375 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12376 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12377 		req->reg_addr = cpu_to_le16(reg);
12378 	}
12379 
12380 	resp = hwrm_req_hold(bp, req);
12381 	rc = hwrm_req_send(bp, req);
12382 	if (!rc)
12383 		*val = le16_to_cpu(resp->reg_data);
12384 	hwrm_req_drop(bp, req);
12385 	return rc;
12386 }
12387 
12388 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
12389 				    u16 val)
12390 {
12391 	struct hwrm_port_phy_mdio_write_input *req;
12392 	int rc;
12393 
12394 	if (bp->hwrm_spec_code < 0x10a00)
12395 		return -EOPNOTSUPP;
12396 
12397 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
12398 	if (rc)
12399 		return rc;
12400 
12401 	req->port_id = cpu_to_le16(bp->pf.port_id);
12402 	req->phy_addr = phy_addr;
12403 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12404 	if (mdio_phy_id_is_c45(phy_addr)) {
12405 		req->cl45_mdio = 1;
12406 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12407 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12408 		req->reg_addr = cpu_to_le16(reg);
12409 	}
12410 	req->reg_data = cpu_to_le16(val);
12411 
12412 	return hwrm_req_send(bp, req);
12413 }
12414 
12415 /* rtnl_lock held */
12416 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12417 {
12418 	struct mii_ioctl_data *mdio = if_mii(ifr);
12419 	struct bnxt *bp = netdev_priv(dev);
12420 	int rc;
12421 
12422 	switch (cmd) {
12423 	case SIOCGMIIPHY:
12424 		mdio->phy_id = bp->link_info.phy_addr;
12425 
12426 		fallthrough;
12427 	case SIOCGMIIREG: {
12428 		u16 mii_regval = 0;
12429 
12430 		if (!netif_running(dev))
12431 			return -EAGAIN;
12432 
12433 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
12434 					     &mii_regval);
12435 		mdio->val_out = mii_regval;
12436 		return rc;
12437 	}
12438 
12439 	case SIOCSMIIREG:
12440 		if (!netif_running(dev))
12441 			return -EAGAIN;
12442 
12443 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
12444 						mdio->val_in);
12445 
12446 	case SIOCSHWTSTAMP:
12447 		return bnxt_hwtstamp_set(dev, ifr);
12448 
12449 	case SIOCGHWTSTAMP:
12450 		return bnxt_hwtstamp_get(dev, ifr);
12451 
12452 	default:
12453 		/* do nothing */
12454 		break;
12455 	}
12456 	return -EOPNOTSUPP;
12457 }
12458 
12459 static void bnxt_get_ring_stats(struct bnxt *bp,
12460 				struct rtnl_link_stats64 *stats)
12461 {
12462 	int i;
12463 
12464 	for (i = 0; i < bp->cp_nr_rings; i++) {
12465 		struct bnxt_napi *bnapi = bp->bnapi[i];
12466 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
12467 		u64 *sw = cpr->stats.sw_stats;
12468 
12469 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
12470 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12471 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
12472 
12473 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
12474 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
12475 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
12476 
12477 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
12478 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
12479 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
12480 
12481 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
12482 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
12483 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
12484 
12485 		stats->rx_missed_errors +=
12486 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
12487 
12488 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12489 
12490 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
12491 
12492 		stats->rx_dropped +=
12493 			cpr->sw_stats->rx.rx_netpoll_discards +
12494 			cpr->sw_stats->rx.rx_oom_discards;
12495 	}
12496 }
12497 
12498 static void bnxt_add_prev_stats(struct bnxt *bp,
12499 				struct rtnl_link_stats64 *stats)
12500 {
12501 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
12502 
12503 	stats->rx_packets += prev_stats->rx_packets;
12504 	stats->tx_packets += prev_stats->tx_packets;
12505 	stats->rx_bytes += prev_stats->rx_bytes;
12506 	stats->tx_bytes += prev_stats->tx_bytes;
12507 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
12508 	stats->multicast += prev_stats->multicast;
12509 	stats->rx_dropped += prev_stats->rx_dropped;
12510 	stats->tx_dropped += prev_stats->tx_dropped;
12511 }
12512 
12513 static void
12514 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
12515 {
12516 	struct bnxt *bp = netdev_priv(dev);
12517 
12518 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
12519 	/* Make sure bnxt_close_nic() sees that we are reading stats before
12520 	 * we check the BNXT_STATE_OPEN flag.
12521 	 */
12522 	smp_mb__after_atomic();
12523 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12524 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12525 		*stats = bp->net_stats_prev;
12526 		return;
12527 	}
12528 
12529 	bnxt_get_ring_stats(bp, stats);
12530 	bnxt_add_prev_stats(bp, stats);
12531 
12532 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
12533 		u64 *rx = bp->port_stats.sw_stats;
12534 		u64 *tx = bp->port_stats.sw_stats +
12535 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
12536 
12537 		stats->rx_crc_errors =
12538 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
12539 		stats->rx_frame_errors =
12540 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
12541 		stats->rx_length_errors =
12542 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
12543 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
12544 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
12545 		stats->rx_errors =
12546 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
12547 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
12548 		stats->collisions =
12549 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
12550 		stats->tx_fifo_errors =
12551 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
12552 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
12553 	}
12554 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12555 }
12556 
12557 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
12558 					struct bnxt_total_ring_err_stats *stats,
12559 					struct bnxt_cp_ring_info *cpr)
12560 {
12561 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
12562 	u64 *hw_stats = cpr->stats.sw_stats;
12563 
12564 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
12565 	stats->rx_total_resets += sw_stats->rx.rx_resets;
12566 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
12567 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
12568 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
12569 	stats->rx_total_ring_discards +=
12570 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
12571 	stats->tx_total_resets += sw_stats->tx.tx_resets;
12572 	stats->tx_total_ring_discards +=
12573 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
12574 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
12575 }
12576 
12577 void bnxt_get_ring_err_stats(struct bnxt *bp,
12578 			     struct bnxt_total_ring_err_stats *stats)
12579 {
12580 	int i;
12581 
12582 	for (i = 0; i < bp->cp_nr_rings; i++)
12583 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
12584 }
12585 
12586 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
12587 {
12588 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12589 	struct net_device *dev = bp->dev;
12590 	struct netdev_hw_addr *ha;
12591 	u8 *haddr;
12592 	int mc_count = 0;
12593 	bool update = false;
12594 	int off = 0;
12595 
12596 	netdev_for_each_mc_addr(ha, dev) {
12597 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
12598 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12599 			vnic->mc_list_count = 0;
12600 			return false;
12601 		}
12602 		haddr = ha->addr;
12603 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
12604 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
12605 			update = true;
12606 		}
12607 		off += ETH_ALEN;
12608 		mc_count++;
12609 	}
12610 	if (mc_count)
12611 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
12612 
12613 	if (mc_count != vnic->mc_list_count) {
12614 		vnic->mc_list_count = mc_count;
12615 		update = true;
12616 	}
12617 	return update;
12618 }
12619 
12620 static bool bnxt_uc_list_updated(struct bnxt *bp)
12621 {
12622 	struct net_device *dev = bp->dev;
12623 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12624 	struct netdev_hw_addr *ha;
12625 	int off = 0;
12626 
12627 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
12628 		return true;
12629 
12630 	netdev_for_each_uc_addr(ha, dev) {
12631 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
12632 			return true;
12633 
12634 		off += ETH_ALEN;
12635 	}
12636 	return false;
12637 }
12638 
12639 static void bnxt_set_rx_mode(struct net_device *dev)
12640 {
12641 	struct bnxt *bp = netdev_priv(dev);
12642 	struct bnxt_vnic_info *vnic;
12643 	bool mc_update = false;
12644 	bool uc_update;
12645 	u32 mask;
12646 
12647 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
12648 		return;
12649 
12650 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12651 	mask = vnic->rx_mask;
12652 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
12653 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
12654 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
12655 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
12656 
12657 	if (dev->flags & IFF_PROMISC)
12658 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12659 
12660 	uc_update = bnxt_uc_list_updated(bp);
12661 
12662 	if (dev->flags & IFF_BROADCAST)
12663 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
12664 	if (dev->flags & IFF_ALLMULTI) {
12665 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12666 		vnic->mc_list_count = 0;
12667 	} else if (dev->flags & IFF_MULTICAST) {
12668 		mc_update = bnxt_mc_list_updated(bp, &mask);
12669 	}
12670 
12671 	if (mask != vnic->rx_mask || uc_update || mc_update) {
12672 		vnic->rx_mask = mask;
12673 
12674 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
12675 	}
12676 }
12677 
12678 static int bnxt_cfg_rx_mode(struct bnxt *bp)
12679 {
12680 	struct net_device *dev = bp->dev;
12681 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12682 	struct netdev_hw_addr *ha;
12683 	int i, off = 0, rc;
12684 	bool uc_update;
12685 
12686 	netif_addr_lock_bh(dev);
12687 	uc_update = bnxt_uc_list_updated(bp);
12688 	netif_addr_unlock_bh(dev);
12689 
12690 	if (!uc_update)
12691 		goto skip_uc;
12692 
12693 	for (i = 1; i < vnic->uc_filter_count; i++) {
12694 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
12695 
12696 		bnxt_hwrm_l2_filter_free(bp, fltr);
12697 		bnxt_del_l2_filter(bp, fltr);
12698 	}
12699 
12700 	vnic->uc_filter_count = 1;
12701 
12702 	netif_addr_lock_bh(dev);
12703 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
12704 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12705 	} else {
12706 		netdev_for_each_uc_addr(ha, dev) {
12707 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
12708 			off += ETH_ALEN;
12709 			vnic->uc_filter_count++;
12710 		}
12711 	}
12712 	netif_addr_unlock_bh(dev);
12713 
12714 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
12715 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
12716 		if (rc) {
12717 			if (BNXT_VF(bp) && rc == -ENODEV) {
12718 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
12719 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
12720 				else
12721 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
12722 				rc = 0;
12723 			} else {
12724 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
12725 			}
12726 			vnic->uc_filter_count = i;
12727 			return rc;
12728 		}
12729 	}
12730 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
12731 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
12732 
12733 skip_uc:
12734 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
12735 	    !bnxt_promisc_ok(bp))
12736 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12737 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
12738 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
12739 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
12740 			    rc);
12741 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
12742 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12743 		vnic->mc_list_count = 0;
12744 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
12745 	}
12746 	if (rc)
12747 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
12748 			   rc);
12749 
12750 	return rc;
12751 }
12752 
12753 static bool bnxt_can_reserve_rings(struct bnxt *bp)
12754 {
12755 #ifdef CONFIG_BNXT_SRIOV
12756 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
12757 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12758 
12759 		/* No minimum rings were provisioned by the PF.  Don't
12760 		 * reserve rings by default when device is down.
12761 		 */
12762 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
12763 			return true;
12764 
12765 		if (!netif_running(bp->dev))
12766 			return false;
12767 	}
12768 #endif
12769 	return true;
12770 }
12771 
12772 /* If the chip and firmware supports RFS */
12773 static bool bnxt_rfs_supported(struct bnxt *bp)
12774 {
12775 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
12776 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
12777 			return true;
12778 		return false;
12779 	}
12780 	/* 212 firmware is broken for aRFS */
12781 	if (BNXT_FW_MAJ(bp) == 212)
12782 		return false;
12783 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
12784 		return true;
12785 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
12786 		return true;
12787 	return false;
12788 }
12789 
12790 /* If runtime conditions support RFS */
12791 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
12792 {
12793 	struct bnxt_hw_rings hwr = {0};
12794 	int max_vnics, max_rss_ctxs;
12795 
12796 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
12797 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
12798 		return bnxt_rfs_supported(bp);
12799 
12800 	if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
12801 		return false;
12802 
12803 	hwr.grp = bp->rx_nr_rings;
12804 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
12805 	if (new_rss_ctx)
12806 		hwr.vnic++;
12807 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
12808 	max_vnics = bnxt_get_max_func_vnics(bp);
12809 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
12810 
12811 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
12812 		if (bp->rx_nr_rings > 1)
12813 			netdev_warn(bp->dev,
12814 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
12815 				    min(max_rss_ctxs - 1, max_vnics - 1));
12816 		return false;
12817 	}
12818 
12819 	if (!BNXT_NEW_RM(bp))
12820 		return true;
12821 
12822 	if (hwr.vnic == bp->hw_resc.resv_vnics &&
12823 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
12824 		return true;
12825 
12826 	bnxt_hwrm_reserve_rings(bp, &hwr);
12827 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
12828 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
12829 		return true;
12830 
12831 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
12832 	hwr.vnic = 1;
12833 	hwr.rss_ctx = 0;
12834 	bnxt_hwrm_reserve_rings(bp, &hwr);
12835 	return false;
12836 }
12837 
12838 static netdev_features_t bnxt_fix_features(struct net_device *dev,
12839 					   netdev_features_t features)
12840 {
12841 	struct bnxt *bp = netdev_priv(dev);
12842 	netdev_features_t vlan_features;
12843 
12844 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
12845 		features &= ~NETIF_F_NTUPLE;
12846 
12847 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
12848 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
12849 
12850 	if (!(features & NETIF_F_GRO))
12851 		features &= ~NETIF_F_GRO_HW;
12852 
12853 	if (features & NETIF_F_GRO_HW)
12854 		features &= ~NETIF_F_LRO;
12855 
12856 	/* Both CTAG and STAG VLAN accelaration on the RX side have to be
12857 	 * turned on or off together.
12858 	 */
12859 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
12860 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
12861 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
12862 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
12863 		else if (vlan_features)
12864 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
12865 	}
12866 #ifdef CONFIG_BNXT_SRIOV
12867 	if (BNXT_VF(bp) && bp->vf.vlan)
12868 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
12869 #endif
12870 	return features;
12871 }
12872 
12873 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
12874 				bool link_re_init, u32 flags, bool update_tpa)
12875 {
12876 	bnxt_close_nic(bp, irq_re_init, link_re_init);
12877 	bp->flags = flags;
12878 	if (update_tpa)
12879 		bnxt_set_ring_params(bp);
12880 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
12881 }
12882 
12883 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
12884 {
12885 	bool update_tpa = false, update_ntuple = false;
12886 	struct bnxt *bp = netdev_priv(dev);
12887 	u32 flags = bp->flags;
12888 	u32 changes;
12889 	int rc = 0;
12890 	bool re_init = false;
12891 
12892 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
12893 	if (features & NETIF_F_GRO_HW)
12894 		flags |= BNXT_FLAG_GRO;
12895 	else if (features & NETIF_F_LRO)
12896 		flags |= BNXT_FLAG_LRO;
12897 
12898 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
12899 		flags &= ~BNXT_FLAG_TPA;
12900 
12901 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
12902 		flags |= BNXT_FLAG_STRIP_VLAN;
12903 
12904 	if (features & NETIF_F_NTUPLE)
12905 		flags |= BNXT_FLAG_RFS;
12906 	else
12907 		bnxt_clear_usr_fltrs(bp, true);
12908 
12909 	changes = flags ^ bp->flags;
12910 	if (changes & BNXT_FLAG_TPA) {
12911 		update_tpa = true;
12912 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
12913 		    (flags & BNXT_FLAG_TPA) == 0 ||
12914 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
12915 			re_init = true;
12916 	}
12917 
12918 	if (changes & ~BNXT_FLAG_TPA)
12919 		re_init = true;
12920 
12921 	if (changes & BNXT_FLAG_RFS)
12922 		update_ntuple = true;
12923 
12924 	if (flags != bp->flags) {
12925 		u32 old_flags = bp->flags;
12926 
12927 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12928 			bp->flags = flags;
12929 			if (update_tpa)
12930 				bnxt_set_ring_params(bp);
12931 			return rc;
12932 		}
12933 
12934 		if (update_ntuple)
12935 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
12936 
12937 		if (re_init)
12938 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
12939 
12940 		if (update_tpa) {
12941 			bp->flags = flags;
12942 			rc = bnxt_set_tpa(bp,
12943 					  (flags & BNXT_FLAG_TPA) ?
12944 					  true : false);
12945 			if (rc)
12946 				bp->flags = old_flags;
12947 		}
12948 	}
12949 	return rc;
12950 }
12951 
12952 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
12953 			      u8 **nextp)
12954 {
12955 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
12956 	struct hop_jumbo_hdr *jhdr;
12957 	int hdr_count = 0;
12958 	u8 *nexthdr;
12959 	int start;
12960 
12961 	/* Check that there are at most 2 IPv6 extension headers, no
12962 	 * fragment header, and each is <= 64 bytes.
12963 	 */
12964 	start = nw_off + sizeof(*ip6h);
12965 	nexthdr = &ip6h->nexthdr;
12966 	while (ipv6_ext_hdr(*nexthdr)) {
12967 		struct ipv6_opt_hdr *hp;
12968 		int hdrlen;
12969 
12970 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
12971 		    *nexthdr == NEXTHDR_FRAGMENT)
12972 			return false;
12973 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
12974 					  skb_headlen(skb), NULL);
12975 		if (!hp)
12976 			return false;
12977 		if (*nexthdr == NEXTHDR_AUTH)
12978 			hdrlen = ipv6_authlen(hp);
12979 		else
12980 			hdrlen = ipv6_optlen(hp);
12981 
12982 		if (hdrlen > 64)
12983 			return false;
12984 
12985 		/* The ext header may be a hop-by-hop header inserted for
12986 		 * big TCP purposes. This will be removed before sending
12987 		 * from NIC, so do not count it.
12988 		 */
12989 		if (*nexthdr == NEXTHDR_HOP) {
12990 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
12991 				goto increment_hdr;
12992 
12993 			jhdr = (struct hop_jumbo_hdr *)hp;
12994 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
12995 			    jhdr->nexthdr != IPPROTO_TCP)
12996 				goto increment_hdr;
12997 
12998 			goto next_hdr;
12999 		}
13000 increment_hdr:
13001 		hdr_count++;
13002 next_hdr:
13003 		nexthdr = &hp->nexthdr;
13004 		start += hdrlen;
13005 	}
13006 	if (nextp) {
13007 		/* Caller will check inner protocol */
13008 		if (skb->encapsulation) {
13009 			*nextp = nexthdr;
13010 			return true;
13011 		}
13012 		*nextp = NULL;
13013 	}
13014 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
13015 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
13016 }
13017 
13018 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
13019 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
13020 {
13021 	struct udphdr *uh = udp_hdr(skb);
13022 	__be16 udp_port = uh->dest;
13023 
13024 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
13025 	    udp_port != bp->vxlan_gpe_port)
13026 		return false;
13027 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
13028 		struct ethhdr *eh = inner_eth_hdr(skb);
13029 
13030 		switch (eh->h_proto) {
13031 		case htons(ETH_P_IP):
13032 			return true;
13033 		case htons(ETH_P_IPV6):
13034 			return bnxt_exthdr_check(bp, skb,
13035 						 skb_inner_network_offset(skb),
13036 						 NULL);
13037 		}
13038 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
13039 		return true;
13040 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
13041 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13042 					 NULL);
13043 	}
13044 	return false;
13045 }
13046 
13047 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
13048 {
13049 	switch (l4_proto) {
13050 	case IPPROTO_UDP:
13051 		return bnxt_udp_tunl_check(bp, skb);
13052 	case IPPROTO_IPIP:
13053 		return true;
13054 	case IPPROTO_GRE: {
13055 		switch (skb->inner_protocol) {
13056 		default:
13057 			return false;
13058 		case htons(ETH_P_IP):
13059 			return true;
13060 		case htons(ETH_P_IPV6):
13061 			fallthrough;
13062 		}
13063 	}
13064 	case IPPROTO_IPV6:
13065 		/* Check ext headers of inner ipv6 */
13066 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13067 					 NULL);
13068 	}
13069 	return false;
13070 }
13071 
13072 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
13073 					     struct net_device *dev,
13074 					     netdev_features_t features)
13075 {
13076 	struct bnxt *bp = netdev_priv(dev);
13077 	u8 *l4_proto;
13078 
13079 	features = vlan_features_check(skb, features);
13080 	switch (vlan_get_protocol(skb)) {
13081 	case htons(ETH_P_IP):
13082 		if (!skb->encapsulation)
13083 			return features;
13084 		l4_proto = &ip_hdr(skb)->protocol;
13085 		if (bnxt_tunl_check(bp, skb, *l4_proto))
13086 			return features;
13087 		break;
13088 	case htons(ETH_P_IPV6):
13089 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
13090 				       &l4_proto))
13091 			break;
13092 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
13093 			return features;
13094 		break;
13095 	}
13096 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13097 }
13098 
13099 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
13100 			 u32 *reg_buf)
13101 {
13102 	struct hwrm_dbg_read_direct_output *resp;
13103 	struct hwrm_dbg_read_direct_input *req;
13104 	__le32 *dbg_reg_buf;
13105 	dma_addr_t mapping;
13106 	int rc, i;
13107 
13108 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
13109 	if (rc)
13110 		return rc;
13111 
13112 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
13113 					 &mapping);
13114 	if (!dbg_reg_buf) {
13115 		rc = -ENOMEM;
13116 		goto dbg_rd_reg_exit;
13117 	}
13118 
13119 	req->host_dest_addr = cpu_to_le64(mapping);
13120 
13121 	resp = hwrm_req_hold(bp, req);
13122 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
13123 	req->read_len32 = cpu_to_le32(num_words);
13124 
13125 	rc = hwrm_req_send(bp, req);
13126 	if (rc || resp->error_code) {
13127 		rc = -EIO;
13128 		goto dbg_rd_reg_exit;
13129 	}
13130 	for (i = 0; i < num_words; i++)
13131 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
13132 
13133 dbg_rd_reg_exit:
13134 	hwrm_req_drop(bp, req);
13135 	return rc;
13136 }
13137 
13138 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
13139 				       u32 ring_id, u32 *prod, u32 *cons)
13140 {
13141 	struct hwrm_dbg_ring_info_get_output *resp;
13142 	struct hwrm_dbg_ring_info_get_input *req;
13143 	int rc;
13144 
13145 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
13146 	if (rc)
13147 		return rc;
13148 
13149 	req->ring_type = ring_type;
13150 	req->fw_ring_id = cpu_to_le32(ring_id);
13151 	resp = hwrm_req_hold(bp, req);
13152 	rc = hwrm_req_send(bp, req);
13153 	if (!rc) {
13154 		*prod = le32_to_cpu(resp->producer_index);
13155 		*cons = le32_to_cpu(resp->consumer_index);
13156 	}
13157 	hwrm_req_drop(bp, req);
13158 	return rc;
13159 }
13160 
13161 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13162 {
13163 	struct bnxt_tx_ring_info *txr;
13164 	int i = bnapi->index, j;
13165 
13166 	bnxt_for_each_napi_tx(j, bnapi, txr)
13167 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13168 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13169 			    txr->tx_cons);
13170 }
13171 
13172 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
13173 {
13174 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
13175 	int i = bnapi->index;
13176 
13177 	if (!rxr)
13178 		return;
13179 
13180 	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",
13181 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
13182 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
13183 		    rxr->rx_sw_agg_prod);
13184 }
13185 
13186 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
13187 {
13188 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13189 	int i = bnapi->index;
13190 
13191 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
13192 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
13193 }
13194 
13195 static void bnxt_dbg_dump_states(struct bnxt *bp)
13196 {
13197 	int i;
13198 	struct bnxt_napi *bnapi;
13199 
13200 	for (i = 0; i < bp->cp_nr_rings; i++) {
13201 		bnapi = bp->bnapi[i];
13202 		if (netif_msg_drv(bp)) {
13203 			bnxt_dump_tx_sw_state(bnapi);
13204 			bnxt_dump_rx_sw_state(bnapi);
13205 			bnxt_dump_cp_sw_state(bnapi);
13206 		}
13207 	}
13208 }
13209 
13210 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
13211 {
13212 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
13213 	struct hwrm_ring_reset_input *req;
13214 	struct bnxt_napi *bnapi = rxr->bnapi;
13215 	struct bnxt_cp_ring_info *cpr;
13216 	u16 cp_ring_id;
13217 	int rc;
13218 
13219 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
13220 	if (rc)
13221 		return rc;
13222 
13223 	cpr = &bnapi->cp_ring;
13224 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
13225 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
13226 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
13227 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
13228 	return hwrm_req_send_silent(bp, req);
13229 }
13230 
13231 static void bnxt_reset_task(struct bnxt *bp, bool silent)
13232 {
13233 	if (!silent)
13234 		bnxt_dbg_dump_states(bp);
13235 	if (netif_running(bp->dev)) {
13236 		bnxt_close_nic(bp, !silent, false);
13237 		bnxt_open_nic(bp, !silent, false);
13238 	}
13239 }
13240 
13241 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
13242 {
13243 	struct bnxt *bp = netdev_priv(dev);
13244 
13245 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
13246 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
13247 }
13248 
13249 static void bnxt_fw_health_check(struct bnxt *bp)
13250 {
13251 	struct bnxt_fw_health *fw_health = bp->fw_health;
13252 	struct pci_dev *pdev = bp->pdev;
13253 	u32 val;
13254 
13255 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13256 		return;
13257 
13258 	/* Make sure it is enabled before checking the tmr_counter. */
13259 	smp_rmb();
13260 	if (fw_health->tmr_counter) {
13261 		fw_health->tmr_counter--;
13262 		return;
13263 	}
13264 
13265 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13266 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
13267 		fw_health->arrests++;
13268 		goto fw_reset;
13269 	}
13270 
13271 	fw_health->last_fw_heartbeat = val;
13272 
13273 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13274 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
13275 		fw_health->discoveries++;
13276 		goto fw_reset;
13277 	}
13278 
13279 	fw_health->tmr_counter = fw_health->tmr_multiplier;
13280 	return;
13281 
13282 fw_reset:
13283 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
13284 }
13285 
13286 static void bnxt_timer(struct timer_list *t)
13287 {
13288 	struct bnxt *bp = from_timer(bp, t, timer);
13289 	struct net_device *dev = bp->dev;
13290 
13291 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
13292 		return;
13293 
13294 	if (atomic_read(&bp->intr_sem) != 0)
13295 		goto bnxt_restart_timer;
13296 
13297 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
13298 		bnxt_fw_health_check(bp);
13299 
13300 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
13301 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
13302 
13303 	if (bnxt_tc_flower_enabled(bp))
13304 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
13305 
13306 #ifdef CONFIG_RFS_ACCEL
13307 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
13308 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
13309 #endif /*CONFIG_RFS_ACCEL*/
13310 
13311 	if (bp->link_info.phy_retry) {
13312 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
13313 			bp->link_info.phy_retry = false;
13314 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
13315 		} else {
13316 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
13317 		}
13318 	}
13319 
13320 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13321 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13322 
13323 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
13324 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
13325 
13326 bnxt_restart_timer:
13327 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13328 }
13329 
13330 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
13331 {
13332 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
13333 	 * set.  If the device is being closed, bnxt_close() may be holding
13334 	 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear.  So we
13335 	 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
13336 	 */
13337 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13338 	rtnl_lock();
13339 }
13340 
13341 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
13342 {
13343 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13344 	rtnl_unlock();
13345 }
13346 
13347 /* Only called from bnxt_sp_task() */
13348 static void bnxt_reset(struct bnxt *bp, bool silent)
13349 {
13350 	bnxt_rtnl_lock_sp(bp);
13351 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
13352 		bnxt_reset_task(bp, silent);
13353 	bnxt_rtnl_unlock_sp(bp);
13354 }
13355 
13356 /* Only called from bnxt_sp_task() */
13357 static void bnxt_rx_ring_reset(struct bnxt *bp)
13358 {
13359 	int i;
13360 
13361 	bnxt_rtnl_lock_sp(bp);
13362 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13363 		bnxt_rtnl_unlock_sp(bp);
13364 		return;
13365 	}
13366 	/* Disable and flush TPA before resetting the RX ring */
13367 	if (bp->flags & BNXT_FLAG_TPA)
13368 		bnxt_set_tpa(bp, false);
13369 	for (i = 0; i < bp->rx_nr_rings; i++) {
13370 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
13371 		struct bnxt_cp_ring_info *cpr;
13372 		int rc;
13373 
13374 		if (!rxr->bnapi->in_reset)
13375 			continue;
13376 
13377 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
13378 		if (rc) {
13379 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
13380 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
13381 			else
13382 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
13383 					    rc);
13384 			bnxt_reset_task(bp, true);
13385 			break;
13386 		}
13387 		bnxt_free_one_rx_ring_skbs(bp, i);
13388 		rxr->rx_prod = 0;
13389 		rxr->rx_agg_prod = 0;
13390 		rxr->rx_sw_agg_prod = 0;
13391 		rxr->rx_next_cons = 0;
13392 		rxr->bnapi->in_reset = false;
13393 		bnxt_alloc_one_rx_ring(bp, i);
13394 		cpr = &rxr->bnapi->cp_ring;
13395 		cpr->sw_stats->rx.rx_resets++;
13396 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
13397 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
13398 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
13399 	}
13400 	if (bp->flags & BNXT_FLAG_TPA)
13401 		bnxt_set_tpa(bp, true);
13402 	bnxt_rtnl_unlock_sp(bp);
13403 }
13404 
13405 static void bnxt_fw_fatal_close(struct bnxt *bp)
13406 {
13407 	bnxt_tx_disable(bp);
13408 	bnxt_disable_napi(bp);
13409 	bnxt_disable_int_sync(bp);
13410 	bnxt_free_irq(bp);
13411 	bnxt_clear_int_mode(bp);
13412 	pci_disable_device(bp->pdev);
13413 }
13414 
13415 static void bnxt_fw_reset_close(struct bnxt *bp)
13416 {
13417 	/* When firmware is in fatal state, quiesce device and disable
13418 	 * bus master to prevent any potential bad DMAs before freeing
13419 	 * kernel memory.
13420 	 */
13421 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
13422 		u16 val = 0;
13423 
13424 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
13425 		if (val == 0xffff)
13426 			bp->fw_reset_min_dsecs = 0;
13427 		bnxt_fw_fatal_close(bp);
13428 	}
13429 	__bnxt_close_nic(bp, true, false);
13430 	bnxt_vf_reps_free(bp);
13431 	bnxt_clear_int_mode(bp);
13432 	bnxt_hwrm_func_drv_unrgtr(bp);
13433 	if (pci_is_enabled(bp->pdev))
13434 		pci_disable_device(bp->pdev);
13435 	bnxt_free_ctx_mem(bp);
13436 }
13437 
13438 static bool is_bnxt_fw_ok(struct bnxt *bp)
13439 {
13440 	struct bnxt_fw_health *fw_health = bp->fw_health;
13441 	bool no_heartbeat = false, has_reset = false;
13442 	u32 val;
13443 
13444 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13445 	if (val == fw_health->last_fw_heartbeat)
13446 		no_heartbeat = true;
13447 
13448 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13449 	if (val != fw_health->last_fw_reset_cnt)
13450 		has_reset = true;
13451 
13452 	if (!no_heartbeat && has_reset)
13453 		return true;
13454 
13455 	return false;
13456 }
13457 
13458 /* rtnl_lock is acquired before calling this function */
13459 static void bnxt_force_fw_reset(struct bnxt *bp)
13460 {
13461 	struct bnxt_fw_health *fw_health = bp->fw_health;
13462 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13463 	u32 wait_dsecs;
13464 
13465 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
13466 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13467 		return;
13468 
13469 	if (ptp) {
13470 		spin_lock_bh(&ptp->ptp_lock);
13471 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13472 		spin_unlock_bh(&ptp->ptp_lock);
13473 	} else {
13474 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13475 	}
13476 	bnxt_fw_reset_close(bp);
13477 	wait_dsecs = fw_health->master_func_wait_dsecs;
13478 	if (fw_health->primary) {
13479 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
13480 			wait_dsecs = 0;
13481 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
13482 	} else {
13483 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
13484 		wait_dsecs = fw_health->normal_func_wait_dsecs;
13485 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13486 	}
13487 
13488 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
13489 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
13490 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
13491 }
13492 
13493 void bnxt_fw_exception(struct bnxt *bp)
13494 {
13495 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
13496 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
13497 	bnxt_ulp_stop(bp);
13498 	bnxt_rtnl_lock_sp(bp);
13499 	bnxt_force_fw_reset(bp);
13500 	bnxt_rtnl_unlock_sp(bp);
13501 }
13502 
13503 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
13504  * < 0 on error.
13505  */
13506 static int bnxt_get_registered_vfs(struct bnxt *bp)
13507 {
13508 #ifdef CONFIG_BNXT_SRIOV
13509 	int rc;
13510 
13511 	if (!BNXT_PF(bp))
13512 		return 0;
13513 
13514 	rc = bnxt_hwrm_func_qcfg(bp);
13515 	if (rc) {
13516 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
13517 		return rc;
13518 	}
13519 	if (bp->pf.registered_vfs)
13520 		return bp->pf.registered_vfs;
13521 	if (bp->sriov_cfg)
13522 		return 1;
13523 #endif
13524 	return 0;
13525 }
13526 
13527 void bnxt_fw_reset(struct bnxt *bp)
13528 {
13529 	bnxt_ulp_stop(bp);
13530 	bnxt_rtnl_lock_sp(bp);
13531 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
13532 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13533 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13534 		int n = 0, tmo;
13535 
13536 		if (ptp) {
13537 			spin_lock_bh(&ptp->ptp_lock);
13538 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13539 			spin_unlock_bh(&ptp->ptp_lock);
13540 		} else {
13541 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13542 		}
13543 		if (bp->pf.active_vfs &&
13544 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
13545 			n = bnxt_get_registered_vfs(bp);
13546 		if (n < 0) {
13547 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
13548 				   n);
13549 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13550 			dev_close(bp->dev);
13551 			goto fw_reset_exit;
13552 		} else if (n > 0) {
13553 			u16 vf_tmo_dsecs = n * 10;
13554 
13555 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
13556 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
13557 			bp->fw_reset_state =
13558 				BNXT_FW_RESET_STATE_POLL_VF;
13559 			bnxt_queue_fw_reset_work(bp, HZ / 10);
13560 			goto fw_reset_exit;
13561 		}
13562 		bnxt_fw_reset_close(bp);
13563 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
13564 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
13565 			tmo = HZ / 10;
13566 		} else {
13567 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13568 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
13569 		}
13570 		bnxt_queue_fw_reset_work(bp, tmo);
13571 	}
13572 fw_reset_exit:
13573 	bnxt_rtnl_unlock_sp(bp);
13574 }
13575 
13576 static void bnxt_chk_missed_irq(struct bnxt *bp)
13577 {
13578 	int i;
13579 
13580 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13581 		return;
13582 
13583 	for (i = 0; i < bp->cp_nr_rings; i++) {
13584 		struct bnxt_napi *bnapi = bp->bnapi[i];
13585 		struct bnxt_cp_ring_info *cpr;
13586 		u32 fw_ring_id;
13587 		int j;
13588 
13589 		if (!bnapi)
13590 			continue;
13591 
13592 		cpr = &bnapi->cp_ring;
13593 		for (j = 0; j < cpr->cp_ring_count; j++) {
13594 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
13595 			u32 val[2];
13596 
13597 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
13598 				continue;
13599 
13600 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
13601 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
13602 				continue;
13603 			}
13604 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
13605 			bnxt_dbg_hwrm_ring_info_get(bp,
13606 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
13607 				fw_ring_id, &val[0], &val[1]);
13608 			cpr->sw_stats->cmn.missed_irqs++;
13609 		}
13610 	}
13611 }
13612 
13613 static void bnxt_cfg_ntp_filters(struct bnxt *);
13614 
13615 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
13616 {
13617 	struct bnxt_link_info *link_info = &bp->link_info;
13618 
13619 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
13620 		link_info->autoneg = BNXT_AUTONEG_SPEED;
13621 		if (bp->hwrm_spec_code >= 0x10201) {
13622 			if (link_info->auto_pause_setting &
13623 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
13624 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13625 		} else {
13626 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13627 		}
13628 		bnxt_set_auto_speed(link_info);
13629 	} else {
13630 		bnxt_set_force_speed(link_info);
13631 		link_info->req_duplex = link_info->duplex_setting;
13632 	}
13633 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
13634 		link_info->req_flow_ctrl =
13635 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
13636 	else
13637 		link_info->req_flow_ctrl = link_info->force_pause_setting;
13638 }
13639 
13640 static void bnxt_fw_echo_reply(struct bnxt *bp)
13641 {
13642 	struct bnxt_fw_health *fw_health = bp->fw_health;
13643 	struct hwrm_func_echo_response_input *req;
13644 	int rc;
13645 
13646 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
13647 	if (rc)
13648 		return;
13649 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
13650 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
13651 	hwrm_req_send(bp, req);
13652 }
13653 
13654 static void bnxt_ulp_restart(struct bnxt *bp)
13655 {
13656 	bnxt_ulp_stop(bp);
13657 	bnxt_ulp_start(bp, 0);
13658 }
13659 
13660 static void bnxt_sp_task(struct work_struct *work)
13661 {
13662 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
13663 
13664 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13665 	smp_mb__after_atomic();
13666 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13667 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13668 		return;
13669 	}
13670 
13671 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
13672 		bnxt_ulp_restart(bp);
13673 		bnxt_reenable_sriov(bp);
13674 	}
13675 
13676 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
13677 		bnxt_cfg_rx_mode(bp);
13678 
13679 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
13680 		bnxt_cfg_ntp_filters(bp);
13681 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
13682 		bnxt_hwrm_exec_fwd_req(bp);
13683 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
13684 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
13685 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
13686 		bnxt_hwrm_port_qstats(bp, 0);
13687 		bnxt_hwrm_port_qstats_ext(bp, 0);
13688 		bnxt_accumulate_all_stats(bp);
13689 	}
13690 
13691 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
13692 		int rc;
13693 
13694 		mutex_lock(&bp->link_lock);
13695 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
13696 				       &bp->sp_event))
13697 			bnxt_hwrm_phy_qcaps(bp);
13698 
13699 		rc = bnxt_update_link(bp, true);
13700 		if (rc)
13701 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
13702 				   rc);
13703 
13704 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
13705 				       &bp->sp_event))
13706 			bnxt_init_ethtool_link_settings(bp);
13707 		mutex_unlock(&bp->link_lock);
13708 	}
13709 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
13710 		int rc;
13711 
13712 		mutex_lock(&bp->link_lock);
13713 		rc = bnxt_update_phy_setting(bp);
13714 		mutex_unlock(&bp->link_lock);
13715 		if (rc) {
13716 			netdev_warn(bp->dev, "update phy settings retry failed\n");
13717 		} else {
13718 			bp->link_info.phy_retry = false;
13719 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
13720 		}
13721 	}
13722 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
13723 		mutex_lock(&bp->link_lock);
13724 		bnxt_get_port_module_status(bp);
13725 		mutex_unlock(&bp->link_lock);
13726 	}
13727 
13728 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
13729 		bnxt_tc_flow_stats_work(bp);
13730 
13731 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
13732 		bnxt_chk_missed_irq(bp);
13733 
13734 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
13735 		bnxt_fw_echo_reply(bp);
13736 
13737 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
13738 		bnxt_hwmon_notify_event(bp);
13739 
13740 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
13741 	 * must be the last functions to be called before exiting.
13742 	 */
13743 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
13744 		bnxt_reset(bp, false);
13745 
13746 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
13747 		bnxt_reset(bp, true);
13748 
13749 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
13750 		bnxt_rx_ring_reset(bp);
13751 
13752 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
13753 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
13754 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
13755 			bnxt_devlink_health_fw_report(bp);
13756 		else
13757 			bnxt_fw_reset(bp);
13758 	}
13759 
13760 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
13761 		if (!is_bnxt_fw_ok(bp))
13762 			bnxt_devlink_health_fw_report(bp);
13763 	}
13764 
13765 	smp_mb__before_atomic();
13766 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13767 }
13768 
13769 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
13770 				int *max_cp);
13771 
13772 /* Under rtnl_lock */
13773 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
13774 		     int tx_xdp)
13775 {
13776 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
13777 	struct bnxt_hw_rings hwr = {0};
13778 	int rx_rings = rx;
13779 
13780 	if (tcs)
13781 		tx_sets = tcs;
13782 
13783 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
13784 
13785 	if (max_rx < rx_rings)
13786 		return -ENOMEM;
13787 
13788 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
13789 		rx_rings <<= 1;
13790 
13791 	hwr.rx = rx_rings;
13792 	hwr.tx = tx * tx_sets + tx_xdp;
13793 	if (max_tx < hwr.tx)
13794 		return -ENOMEM;
13795 
13796 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
13797 
13798 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
13799 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
13800 	if (max_cp < hwr.cp)
13801 		return -ENOMEM;
13802 	hwr.stat = hwr.cp;
13803 	if (BNXT_NEW_RM(bp)) {
13804 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
13805 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
13806 		hwr.grp = rx;
13807 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13808 	}
13809 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
13810 		hwr.cp_p5 = hwr.tx + rx;
13811 	return bnxt_hwrm_check_rings(bp, &hwr);
13812 }
13813 
13814 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
13815 {
13816 	if (bp->bar2) {
13817 		pci_iounmap(pdev, bp->bar2);
13818 		bp->bar2 = NULL;
13819 	}
13820 
13821 	if (bp->bar1) {
13822 		pci_iounmap(pdev, bp->bar1);
13823 		bp->bar1 = NULL;
13824 	}
13825 
13826 	if (bp->bar0) {
13827 		pci_iounmap(pdev, bp->bar0);
13828 		bp->bar0 = NULL;
13829 	}
13830 }
13831 
13832 static void bnxt_cleanup_pci(struct bnxt *bp)
13833 {
13834 	bnxt_unmap_bars(bp, bp->pdev);
13835 	pci_release_regions(bp->pdev);
13836 	if (pci_is_enabled(bp->pdev))
13837 		pci_disable_device(bp->pdev);
13838 }
13839 
13840 static void bnxt_init_dflt_coal(struct bnxt *bp)
13841 {
13842 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
13843 	struct bnxt_coal *coal;
13844 	u16 flags = 0;
13845 
13846 	if (coal_cap->cmpl_params &
13847 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
13848 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
13849 
13850 	/* Tick values in micro seconds.
13851 	 * 1 coal_buf x bufs_per_record = 1 completion record.
13852 	 */
13853 	coal = &bp->rx_coal;
13854 	coal->coal_ticks = 10;
13855 	coal->coal_bufs = 30;
13856 	coal->coal_ticks_irq = 1;
13857 	coal->coal_bufs_irq = 2;
13858 	coal->idle_thresh = 50;
13859 	coal->bufs_per_record = 2;
13860 	coal->budget = 64;		/* NAPI budget */
13861 	coal->flags = flags;
13862 
13863 	coal = &bp->tx_coal;
13864 	coal->coal_ticks = 28;
13865 	coal->coal_bufs = 30;
13866 	coal->coal_ticks_irq = 2;
13867 	coal->coal_bufs_irq = 2;
13868 	coal->bufs_per_record = 1;
13869 	coal->flags = flags;
13870 
13871 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
13872 }
13873 
13874 /* FW that pre-reserves 1 VNIC per function */
13875 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
13876 {
13877 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
13878 
13879 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13880 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
13881 		return true;
13882 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13883 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
13884 		return true;
13885 	return false;
13886 }
13887 
13888 static int bnxt_fw_init_one_p1(struct bnxt *bp)
13889 {
13890 	int rc;
13891 
13892 	bp->fw_cap = 0;
13893 	rc = bnxt_hwrm_ver_get(bp);
13894 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
13895 	 * so wait before continuing with recovery.
13896 	 */
13897 	if (rc)
13898 		msleep(100);
13899 	bnxt_try_map_fw_health_reg(bp);
13900 	if (rc) {
13901 		rc = bnxt_try_recover_fw(bp);
13902 		if (rc)
13903 			return rc;
13904 		rc = bnxt_hwrm_ver_get(bp);
13905 		if (rc)
13906 			return rc;
13907 	}
13908 
13909 	bnxt_nvm_cfg_ver_get(bp);
13910 
13911 	rc = bnxt_hwrm_func_reset(bp);
13912 	if (rc)
13913 		return -ENODEV;
13914 
13915 	bnxt_hwrm_fw_set_time(bp);
13916 	return 0;
13917 }
13918 
13919 static int bnxt_fw_init_one_p2(struct bnxt *bp)
13920 {
13921 	int rc;
13922 
13923 	/* Get the MAX capabilities for this function */
13924 	rc = bnxt_hwrm_func_qcaps(bp);
13925 	if (rc) {
13926 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
13927 			   rc);
13928 		return -ENODEV;
13929 	}
13930 
13931 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
13932 	if (rc)
13933 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
13934 			    rc);
13935 
13936 	if (bnxt_alloc_fw_health(bp)) {
13937 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
13938 	} else {
13939 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
13940 		if (rc)
13941 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
13942 				    rc);
13943 	}
13944 
13945 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
13946 	if (rc)
13947 		return -ENODEV;
13948 
13949 	if (bnxt_fw_pre_resv_vnics(bp))
13950 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
13951 
13952 	bnxt_hwrm_func_qcfg(bp);
13953 	bnxt_hwrm_vnic_qcaps(bp);
13954 	bnxt_hwrm_port_led_qcaps(bp);
13955 	bnxt_ethtool_init(bp);
13956 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
13957 		__bnxt_hwrm_ptp_qcfg(bp);
13958 	bnxt_dcb_init(bp);
13959 	bnxt_hwmon_init(bp);
13960 	return 0;
13961 }
13962 
13963 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
13964 {
13965 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
13966 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
13967 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
13968 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
13969 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
13970 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
13971 		bp->rss_hash_delta = bp->rss_hash_cfg;
13972 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
13973 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
13974 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
13975 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
13976 	}
13977 }
13978 
13979 static void bnxt_set_dflt_rfs(struct bnxt *bp)
13980 {
13981 	struct net_device *dev = bp->dev;
13982 
13983 	dev->hw_features &= ~NETIF_F_NTUPLE;
13984 	dev->features &= ~NETIF_F_NTUPLE;
13985 	bp->flags &= ~BNXT_FLAG_RFS;
13986 	if (bnxt_rfs_supported(bp)) {
13987 		dev->hw_features |= NETIF_F_NTUPLE;
13988 		if (bnxt_rfs_capable(bp, false)) {
13989 			bp->flags |= BNXT_FLAG_RFS;
13990 			dev->features |= NETIF_F_NTUPLE;
13991 		}
13992 	}
13993 }
13994 
13995 static void bnxt_fw_init_one_p3(struct bnxt *bp)
13996 {
13997 	struct pci_dev *pdev = bp->pdev;
13998 
13999 	bnxt_set_dflt_rss_hash_type(bp);
14000 	bnxt_set_dflt_rfs(bp);
14001 
14002 	bnxt_get_wol_settings(bp);
14003 	if (bp->flags & BNXT_FLAG_WOL_CAP)
14004 		device_set_wakeup_enable(&pdev->dev, bp->wol);
14005 	else
14006 		device_set_wakeup_capable(&pdev->dev, false);
14007 
14008 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
14009 	bnxt_hwrm_coal_params_qcaps(bp);
14010 }
14011 
14012 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
14013 
14014 int bnxt_fw_init_one(struct bnxt *bp)
14015 {
14016 	int rc;
14017 
14018 	rc = bnxt_fw_init_one_p1(bp);
14019 	if (rc) {
14020 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
14021 		return rc;
14022 	}
14023 	rc = bnxt_fw_init_one_p2(bp);
14024 	if (rc) {
14025 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
14026 		return rc;
14027 	}
14028 	rc = bnxt_probe_phy(bp, false);
14029 	if (rc)
14030 		return rc;
14031 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
14032 	if (rc)
14033 		return rc;
14034 
14035 	bnxt_fw_init_one_p3(bp);
14036 	return 0;
14037 }
14038 
14039 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
14040 {
14041 	struct bnxt_fw_health *fw_health = bp->fw_health;
14042 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
14043 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
14044 	u32 reg_type, reg_off, delay_msecs;
14045 
14046 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
14047 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
14048 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
14049 	switch (reg_type) {
14050 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
14051 		pci_write_config_dword(bp->pdev, reg_off, val);
14052 		break;
14053 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
14054 		writel(reg_off & BNXT_GRC_BASE_MASK,
14055 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
14056 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
14057 		fallthrough;
14058 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
14059 		writel(val, bp->bar0 + reg_off);
14060 		break;
14061 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
14062 		writel(val, bp->bar1 + reg_off);
14063 		break;
14064 	}
14065 	if (delay_msecs) {
14066 		pci_read_config_dword(bp->pdev, 0, &val);
14067 		msleep(delay_msecs);
14068 	}
14069 }
14070 
14071 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
14072 {
14073 	struct hwrm_func_qcfg_output *resp;
14074 	struct hwrm_func_qcfg_input *req;
14075 	bool result = true; /* firmware will enforce if unknown */
14076 
14077 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
14078 		return result;
14079 
14080 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
14081 		return result;
14082 
14083 	req->fid = cpu_to_le16(0xffff);
14084 	resp = hwrm_req_hold(bp, req);
14085 	if (!hwrm_req_send(bp, req))
14086 		result = !!(le16_to_cpu(resp->flags) &
14087 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
14088 	hwrm_req_drop(bp, req);
14089 	return result;
14090 }
14091 
14092 static void bnxt_reset_all(struct bnxt *bp)
14093 {
14094 	struct bnxt_fw_health *fw_health = bp->fw_health;
14095 	int i, rc;
14096 
14097 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14098 		bnxt_fw_reset_via_optee(bp);
14099 		bp->fw_reset_timestamp = jiffies;
14100 		return;
14101 	}
14102 
14103 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
14104 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
14105 			bnxt_fw_reset_writel(bp, i);
14106 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
14107 		struct hwrm_fw_reset_input *req;
14108 
14109 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
14110 		if (!rc) {
14111 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
14112 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
14113 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
14114 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
14115 			rc = hwrm_req_send(bp, req);
14116 		}
14117 		if (rc != -ENODEV)
14118 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
14119 	}
14120 	bp->fw_reset_timestamp = jiffies;
14121 }
14122 
14123 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
14124 {
14125 	return time_after(jiffies, bp->fw_reset_timestamp +
14126 			  (bp->fw_reset_max_dsecs * HZ / 10));
14127 }
14128 
14129 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
14130 {
14131 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14132 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
14133 		bnxt_dl_health_fw_status_update(bp, false);
14134 	bp->fw_reset_state = 0;
14135 	dev_close(bp->dev);
14136 }
14137 
14138 static void bnxt_fw_reset_task(struct work_struct *work)
14139 {
14140 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
14141 	int rc = 0;
14142 
14143 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14144 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
14145 		return;
14146 	}
14147 
14148 	switch (bp->fw_reset_state) {
14149 	case BNXT_FW_RESET_STATE_POLL_VF: {
14150 		int n = bnxt_get_registered_vfs(bp);
14151 		int tmo;
14152 
14153 		if (n < 0) {
14154 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
14155 				   n, jiffies_to_msecs(jiffies -
14156 				   bp->fw_reset_timestamp));
14157 			goto fw_reset_abort;
14158 		} else if (n > 0) {
14159 			if (bnxt_fw_reset_timeout(bp)) {
14160 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14161 				bp->fw_reset_state = 0;
14162 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
14163 					   n);
14164 				goto ulp_start;
14165 			}
14166 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14167 			return;
14168 		}
14169 		bp->fw_reset_timestamp = jiffies;
14170 		rtnl_lock();
14171 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
14172 			bnxt_fw_reset_abort(bp, rc);
14173 			rtnl_unlock();
14174 			goto ulp_start;
14175 		}
14176 		bnxt_fw_reset_close(bp);
14177 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14178 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14179 			tmo = HZ / 10;
14180 		} else {
14181 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14182 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14183 		}
14184 		rtnl_unlock();
14185 		bnxt_queue_fw_reset_work(bp, tmo);
14186 		return;
14187 	}
14188 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
14189 		u32 val;
14190 
14191 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14192 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
14193 		    !bnxt_fw_reset_timeout(bp)) {
14194 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14195 			return;
14196 		}
14197 
14198 		if (!bp->fw_health->primary) {
14199 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
14200 
14201 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14202 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14203 			return;
14204 		}
14205 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14206 	}
14207 		fallthrough;
14208 	case BNXT_FW_RESET_STATE_RESET_FW:
14209 		bnxt_reset_all(bp);
14210 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14211 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
14212 		return;
14213 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
14214 		bnxt_inv_fw_health_reg(bp);
14215 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
14216 		    !bp->fw_reset_min_dsecs) {
14217 			u16 val;
14218 
14219 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14220 			if (val == 0xffff) {
14221 				if (bnxt_fw_reset_timeout(bp)) {
14222 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
14223 					rc = -ETIMEDOUT;
14224 					goto fw_reset_abort;
14225 				}
14226 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
14227 				return;
14228 			}
14229 		}
14230 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14231 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
14232 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
14233 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
14234 			bnxt_dl_remote_reload(bp);
14235 		if (pci_enable_device(bp->pdev)) {
14236 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
14237 			rc = -ENODEV;
14238 			goto fw_reset_abort;
14239 		}
14240 		pci_set_master(bp->pdev);
14241 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
14242 		fallthrough;
14243 	case BNXT_FW_RESET_STATE_POLL_FW:
14244 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
14245 		rc = bnxt_hwrm_poll(bp);
14246 		if (rc) {
14247 			if (bnxt_fw_reset_timeout(bp)) {
14248 				netdev_err(bp->dev, "Firmware reset aborted\n");
14249 				goto fw_reset_abort_status;
14250 			}
14251 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14252 			return;
14253 		}
14254 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
14255 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
14256 		fallthrough;
14257 	case BNXT_FW_RESET_STATE_OPENING:
14258 		while (!rtnl_trylock()) {
14259 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14260 			return;
14261 		}
14262 		rc = bnxt_open(bp->dev);
14263 		if (rc) {
14264 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
14265 			bnxt_fw_reset_abort(bp, rc);
14266 			rtnl_unlock();
14267 			goto ulp_start;
14268 		}
14269 
14270 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
14271 		    bp->fw_health->enabled) {
14272 			bp->fw_health->last_fw_reset_cnt =
14273 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14274 		}
14275 		bp->fw_reset_state = 0;
14276 		/* Make sure fw_reset_state is 0 before clearing the flag */
14277 		smp_mb__before_atomic();
14278 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14279 		bnxt_ptp_reapply_pps(bp);
14280 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
14281 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
14282 			bnxt_dl_health_fw_recovery_done(bp);
14283 			bnxt_dl_health_fw_status_update(bp, true);
14284 		}
14285 		rtnl_unlock();
14286 		bnxt_ulp_start(bp, 0);
14287 		bnxt_reenable_sriov(bp);
14288 		rtnl_lock();
14289 		bnxt_vf_reps_alloc(bp);
14290 		bnxt_vf_reps_open(bp);
14291 		rtnl_unlock();
14292 		break;
14293 	}
14294 	return;
14295 
14296 fw_reset_abort_status:
14297 	if (bp->fw_health->status_reliable ||
14298 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
14299 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14300 
14301 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
14302 	}
14303 fw_reset_abort:
14304 	rtnl_lock();
14305 	bnxt_fw_reset_abort(bp, rc);
14306 	rtnl_unlock();
14307 ulp_start:
14308 	bnxt_ulp_start(bp, rc);
14309 }
14310 
14311 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
14312 {
14313 	int rc;
14314 	struct bnxt *bp = netdev_priv(dev);
14315 
14316 	SET_NETDEV_DEV(dev, &pdev->dev);
14317 
14318 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
14319 	rc = pci_enable_device(pdev);
14320 	if (rc) {
14321 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
14322 		goto init_err;
14323 	}
14324 
14325 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
14326 		dev_err(&pdev->dev,
14327 			"Cannot find PCI device base address, aborting\n");
14328 		rc = -ENODEV;
14329 		goto init_err_disable;
14330 	}
14331 
14332 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
14333 	if (rc) {
14334 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
14335 		goto init_err_disable;
14336 	}
14337 
14338 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
14339 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
14340 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
14341 		rc = -EIO;
14342 		goto init_err_release;
14343 	}
14344 
14345 	pci_set_master(pdev);
14346 
14347 	bp->dev = dev;
14348 	bp->pdev = pdev;
14349 
14350 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
14351 	 * determines the BAR size.
14352 	 */
14353 	bp->bar0 = pci_ioremap_bar(pdev, 0);
14354 	if (!bp->bar0) {
14355 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
14356 		rc = -ENOMEM;
14357 		goto init_err_release;
14358 	}
14359 
14360 	bp->bar2 = pci_ioremap_bar(pdev, 4);
14361 	if (!bp->bar2) {
14362 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
14363 		rc = -ENOMEM;
14364 		goto init_err_release;
14365 	}
14366 
14367 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
14368 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
14369 
14370 	spin_lock_init(&bp->ntp_fltr_lock);
14371 #if BITS_PER_LONG == 32
14372 	spin_lock_init(&bp->db_lock);
14373 #endif
14374 
14375 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
14376 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
14377 
14378 	timer_setup(&bp->timer, bnxt_timer, 0);
14379 	bp->current_interval = BNXT_TIMER_INTERVAL;
14380 
14381 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
14382 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
14383 
14384 	clear_bit(BNXT_STATE_OPEN, &bp->state);
14385 	return 0;
14386 
14387 init_err_release:
14388 	bnxt_unmap_bars(bp, pdev);
14389 	pci_release_regions(pdev);
14390 
14391 init_err_disable:
14392 	pci_disable_device(pdev);
14393 
14394 init_err:
14395 	return rc;
14396 }
14397 
14398 /* rtnl_lock held */
14399 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
14400 {
14401 	struct sockaddr *addr = p;
14402 	struct bnxt *bp = netdev_priv(dev);
14403 	int rc = 0;
14404 
14405 	if (!is_valid_ether_addr(addr->sa_data))
14406 		return -EADDRNOTAVAIL;
14407 
14408 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
14409 		return 0;
14410 
14411 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
14412 	if (rc)
14413 		return rc;
14414 
14415 	eth_hw_addr_set(dev, addr->sa_data);
14416 	bnxt_clear_usr_fltrs(bp, true);
14417 	if (netif_running(dev)) {
14418 		bnxt_close_nic(bp, false, false);
14419 		rc = bnxt_open_nic(bp, false, false);
14420 	}
14421 
14422 	return rc;
14423 }
14424 
14425 /* rtnl_lock held */
14426 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
14427 {
14428 	struct bnxt *bp = netdev_priv(dev);
14429 
14430 	if (netif_running(dev))
14431 		bnxt_close_nic(bp, true, false);
14432 
14433 	WRITE_ONCE(dev->mtu, new_mtu);
14434 	bnxt_set_ring_params(bp);
14435 
14436 	if (netif_running(dev))
14437 		return bnxt_open_nic(bp, true, false);
14438 
14439 	return 0;
14440 }
14441 
14442 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
14443 {
14444 	struct bnxt *bp = netdev_priv(dev);
14445 	bool sh = false;
14446 	int rc, tx_cp;
14447 
14448 	if (tc > bp->max_tc) {
14449 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
14450 			   tc, bp->max_tc);
14451 		return -EINVAL;
14452 	}
14453 
14454 	if (bp->num_tc == tc)
14455 		return 0;
14456 
14457 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
14458 		sh = true;
14459 
14460 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
14461 			      sh, tc, bp->tx_nr_rings_xdp);
14462 	if (rc)
14463 		return rc;
14464 
14465 	/* Needs to close the device and do hw resource re-allocations */
14466 	if (netif_running(bp->dev))
14467 		bnxt_close_nic(bp, true, false);
14468 
14469 	if (tc) {
14470 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
14471 		netdev_set_num_tc(dev, tc);
14472 		bp->num_tc = tc;
14473 	} else {
14474 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
14475 		netdev_reset_tc(dev);
14476 		bp->num_tc = 0;
14477 	}
14478 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
14479 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
14480 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
14481 			       tx_cp + bp->rx_nr_rings;
14482 
14483 	if (netif_running(bp->dev))
14484 		return bnxt_open_nic(bp, true, false);
14485 
14486 	return 0;
14487 }
14488 
14489 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
14490 				  void *cb_priv)
14491 {
14492 	struct bnxt *bp = cb_priv;
14493 
14494 	if (!bnxt_tc_flower_enabled(bp) ||
14495 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
14496 		return -EOPNOTSUPP;
14497 
14498 	switch (type) {
14499 	case TC_SETUP_CLSFLOWER:
14500 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
14501 	default:
14502 		return -EOPNOTSUPP;
14503 	}
14504 }
14505 
14506 LIST_HEAD(bnxt_block_cb_list);
14507 
14508 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
14509 			 void *type_data)
14510 {
14511 	struct bnxt *bp = netdev_priv(dev);
14512 
14513 	switch (type) {
14514 	case TC_SETUP_BLOCK:
14515 		return flow_block_cb_setup_simple(type_data,
14516 						  &bnxt_block_cb_list,
14517 						  bnxt_setup_tc_block_cb,
14518 						  bp, bp, true);
14519 	case TC_SETUP_QDISC_MQPRIO: {
14520 		struct tc_mqprio_qopt *mqprio = type_data;
14521 
14522 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
14523 
14524 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
14525 	}
14526 	default:
14527 		return -EOPNOTSUPP;
14528 	}
14529 }
14530 
14531 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
14532 			    const struct sk_buff *skb)
14533 {
14534 	struct bnxt_vnic_info *vnic;
14535 
14536 	if (skb)
14537 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
14538 
14539 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
14540 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
14541 }
14542 
14543 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
14544 			   u32 idx)
14545 {
14546 	struct hlist_head *head;
14547 	int bit_id;
14548 
14549 	spin_lock_bh(&bp->ntp_fltr_lock);
14550 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
14551 	if (bit_id < 0) {
14552 		spin_unlock_bh(&bp->ntp_fltr_lock);
14553 		return -ENOMEM;
14554 	}
14555 
14556 	fltr->base.sw_id = (u16)bit_id;
14557 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
14558 	fltr->base.flags |= BNXT_ACT_RING_DST;
14559 	head = &bp->ntp_fltr_hash_tbl[idx];
14560 	hlist_add_head_rcu(&fltr->base.hash, head);
14561 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
14562 	bnxt_insert_usr_fltr(bp, &fltr->base);
14563 	bp->ntp_fltr_count++;
14564 	spin_unlock_bh(&bp->ntp_fltr_lock);
14565 	return 0;
14566 }
14567 
14568 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
14569 			    struct bnxt_ntuple_filter *f2)
14570 {
14571 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
14572 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
14573 	struct flow_keys *keys1 = &f1->fkeys;
14574 	struct flow_keys *keys2 = &f2->fkeys;
14575 
14576 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
14577 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
14578 		return false;
14579 
14580 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
14581 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
14582 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
14583 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
14584 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
14585 			return false;
14586 	} else {
14587 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
14588 				     &keys2->addrs.v6addrs.src) ||
14589 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
14590 				     &masks2->addrs.v6addrs.src) ||
14591 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
14592 				     &keys2->addrs.v6addrs.dst) ||
14593 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
14594 				     &masks2->addrs.v6addrs.dst))
14595 			return false;
14596 	}
14597 
14598 	return keys1->ports.src == keys2->ports.src &&
14599 	       masks1->ports.src == masks2->ports.src &&
14600 	       keys1->ports.dst == keys2->ports.dst &&
14601 	       masks1->ports.dst == masks2->ports.dst &&
14602 	       keys1->control.flags == keys2->control.flags &&
14603 	       f1->l2_fltr == f2->l2_fltr;
14604 }
14605 
14606 struct bnxt_ntuple_filter *
14607 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
14608 				struct bnxt_ntuple_filter *fltr, u32 idx)
14609 {
14610 	struct bnxt_ntuple_filter *f;
14611 	struct hlist_head *head;
14612 
14613 	head = &bp->ntp_fltr_hash_tbl[idx];
14614 	hlist_for_each_entry_rcu(f, head, base.hash) {
14615 		if (bnxt_fltr_match(f, fltr))
14616 			return f;
14617 	}
14618 	return NULL;
14619 }
14620 
14621 #ifdef CONFIG_RFS_ACCEL
14622 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
14623 			      u16 rxq_index, u32 flow_id)
14624 {
14625 	struct bnxt *bp = netdev_priv(dev);
14626 	struct bnxt_ntuple_filter *fltr, *new_fltr;
14627 	struct flow_keys *fkeys;
14628 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
14629 	struct bnxt_l2_filter *l2_fltr;
14630 	int rc = 0, idx;
14631 	u32 flags;
14632 
14633 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
14634 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
14635 		atomic_inc(&l2_fltr->refcnt);
14636 	} else {
14637 		struct bnxt_l2_key key;
14638 
14639 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
14640 		key.vlan = 0;
14641 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
14642 		if (!l2_fltr)
14643 			return -EINVAL;
14644 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
14645 			bnxt_del_l2_filter(bp, l2_fltr);
14646 			return -EINVAL;
14647 		}
14648 	}
14649 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
14650 	if (!new_fltr) {
14651 		bnxt_del_l2_filter(bp, l2_fltr);
14652 		return -ENOMEM;
14653 	}
14654 
14655 	fkeys = &new_fltr->fkeys;
14656 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
14657 		rc = -EPROTONOSUPPORT;
14658 		goto err_free;
14659 	}
14660 
14661 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
14662 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
14663 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
14664 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
14665 		rc = -EPROTONOSUPPORT;
14666 		goto err_free;
14667 	}
14668 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
14669 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
14670 		if (bp->hwrm_spec_code < 0x10601) {
14671 			rc = -EPROTONOSUPPORT;
14672 			goto err_free;
14673 		}
14674 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
14675 	}
14676 	flags = fkeys->control.flags;
14677 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
14678 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
14679 		rc = -EPROTONOSUPPORT;
14680 		goto err_free;
14681 	}
14682 	new_fltr->l2_fltr = l2_fltr;
14683 
14684 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
14685 	rcu_read_lock();
14686 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
14687 	if (fltr) {
14688 		rc = fltr->base.sw_id;
14689 		rcu_read_unlock();
14690 		goto err_free;
14691 	}
14692 	rcu_read_unlock();
14693 
14694 	new_fltr->flow_id = flow_id;
14695 	new_fltr->base.rxq = rxq_index;
14696 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
14697 	if (!rc) {
14698 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14699 		return new_fltr->base.sw_id;
14700 	}
14701 
14702 err_free:
14703 	bnxt_del_l2_filter(bp, l2_fltr);
14704 	kfree(new_fltr);
14705 	return rc;
14706 }
14707 #endif
14708 
14709 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
14710 {
14711 	spin_lock_bh(&bp->ntp_fltr_lock);
14712 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
14713 		spin_unlock_bh(&bp->ntp_fltr_lock);
14714 		return;
14715 	}
14716 	hlist_del_rcu(&fltr->base.hash);
14717 	bnxt_del_one_usr_fltr(bp, &fltr->base);
14718 	bp->ntp_fltr_count--;
14719 	spin_unlock_bh(&bp->ntp_fltr_lock);
14720 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
14721 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
14722 	kfree_rcu(fltr, base.rcu);
14723 }
14724 
14725 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
14726 {
14727 #ifdef CONFIG_RFS_ACCEL
14728 	int i;
14729 
14730 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
14731 		struct hlist_head *head;
14732 		struct hlist_node *tmp;
14733 		struct bnxt_ntuple_filter *fltr;
14734 		int rc;
14735 
14736 		head = &bp->ntp_fltr_hash_tbl[i];
14737 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
14738 			bool del = false;
14739 
14740 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
14741 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
14742 					continue;
14743 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
14744 							fltr->flow_id,
14745 							fltr->base.sw_id)) {
14746 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
14747 									 fltr);
14748 					del = true;
14749 				}
14750 			} else {
14751 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
14752 								       fltr);
14753 				if (rc)
14754 					del = true;
14755 				else
14756 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
14757 			}
14758 
14759 			if (del)
14760 				bnxt_del_ntp_filter(bp, fltr);
14761 		}
14762 	}
14763 #endif
14764 }
14765 
14766 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
14767 				    unsigned int entry, struct udp_tunnel_info *ti)
14768 {
14769 	struct bnxt *bp = netdev_priv(netdev);
14770 	unsigned int cmd;
14771 
14772 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
14773 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
14774 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
14775 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
14776 	else
14777 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
14778 
14779 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
14780 }
14781 
14782 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
14783 				      unsigned int entry, struct udp_tunnel_info *ti)
14784 {
14785 	struct bnxt *bp = netdev_priv(netdev);
14786 	unsigned int cmd;
14787 
14788 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
14789 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
14790 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
14791 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
14792 	else
14793 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
14794 
14795 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
14796 }
14797 
14798 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
14799 	.set_port	= bnxt_udp_tunnel_set_port,
14800 	.unset_port	= bnxt_udp_tunnel_unset_port,
14801 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
14802 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
14803 	.tables		= {
14804 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
14805 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
14806 	},
14807 }, bnxt_udp_tunnels_p7 = {
14808 	.set_port	= bnxt_udp_tunnel_set_port,
14809 	.unset_port	= bnxt_udp_tunnel_unset_port,
14810 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
14811 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
14812 	.tables		= {
14813 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
14814 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
14815 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
14816 	},
14817 };
14818 
14819 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
14820 			       struct net_device *dev, u32 filter_mask,
14821 			       int nlflags)
14822 {
14823 	struct bnxt *bp = netdev_priv(dev);
14824 
14825 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
14826 				       nlflags, filter_mask, NULL);
14827 }
14828 
14829 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
14830 			       u16 flags, struct netlink_ext_ack *extack)
14831 {
14832 	struct bnxt *bp = netdev_priv(dev);
14833 	struct nlattr *attr, *br_spec;
14834 	int rem, rc = 0;
14835 
14836 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
14837 		return -EOPNOTSUPP;
14838 
14839 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
14840 	if (!br_spec)
14841 		return -EINVAL;
14842 
14843 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
14844 		u16 mode;
14845 
14846 		mode = nla_get_u16(attr);
14847 		if (mode == bp->br_mode)
14848 			break;
14849 
14850 		rc = bnxt_hwrm_set_br_mode(bp, mode);
14851 		if (!rc)
14852 			bp->br_mode = mode;
14853 		break;
14854 	}
14855 	return rc;
14856 }
14857 
14858 int bnxt_get_port_parent_id(struct net_device *dev,
14859 			    struct netdev_phys_item_id *ppid)
14860 {
14861 	struct bnxt *bp = netdev_priv(dev);
14862 
14863 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
14864 		return -EOPNOTSUPP;
14865 
14866 	/* The PF and it's VF-reps only support the switchdev framework */
14867 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
14868 		return -EOPNOTSUPP;
14869 
14870 	ppid->id_len = sizeof(bp->dsn);
14871 	memcpy(ppid->id, bp->dsn, ppid->id_len);
14872 
14873 	return 0;
14874 }
14875 
14876 static const struct net_device_ops bnxt_netdev_ops = {
14877 	.ndo_open		= bnxt_open,
14878 	.ndo_start_xmit		= bnxt_start_xmit,
14879 	.ndo_stop		= bnxt_close,
14880 	.ndo_get_stats64	= bnxt_get_stats64,
14881 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
14882 	.ndo_eth_ioctl		= bnxt_ioctl,
14883 	.ndo_validate_addr	= eth_validate_addr,
14884 	.ndo_set_mac_address	= bnxt_change_mac_addr,
14885 	.ndo_change_mtu		= bnxt_change_mtu,
14886 	.ndo_fix_features	= bnxt_fix_features,
14887 	.ndo_set_features	= bnxt_set_features,
14888 	.ndo_features_check	= bnxt_features_check,
14889 	.ndo_tx_timeout		= bnxt_tx_timeout,
14890 #ifdef CONFIG_BNXT_SRIOV
14891 	.ndo_get_vf_config	= bnxt_get_vf_config,
14892 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
14893 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
14894 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
14895 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
14896 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
14897 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
14898 #endif
14899 	.ndo_setup_tc           = bnxt_setup_tc,
14900 #ifdef CONFIG_RFS_ACCEL
14901 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
14902 #endif
14903 	.ndo_bpf		= bnxt_xdp,
14904 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
14905 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
14906 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
14907 };
14908 
14909 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
14910 				    struct netdev_queue_stats_rx *stats)
14911 {
14912 	struct bnxt *bp = netdev_priv(dev);
14913 	struct bnxt_cp_ring_info *cpr;
14914 	u64 *sw;
14915 
14916 	cpr = &bp->bnapi[i]->cp_ring;
14917 	sw = cpr->stats.sw_stats;
14918 
14919 	stats->packets = 0;
14920 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
14921 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
14922 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
14923 
14924 	stats->bytes = 0;
14925 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
14926 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
14927 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
14928 
14929 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
14930 }
14931 
14932 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
14933 				    struct netdev_queue_stats_tx *stats)
14934 {
14935 	struct bnxt *bp = netdev_priv(dev);
14936 	struct bnxt_napi *bnapi;
14937 	u64 *sw;
14938 
14939 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
14940 	sw = bnapi->cp_ring.stats.sw_stats;
14941 
14942 	stats->packets = 0;
14943 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
14944 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
14945 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
14946 
14947 	stats->bytes = 0;
14948 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
14949 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
14950 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
14951 }
14952 
14953 static void bnxt_get_base_stats(struct net_device *dev,
14954 				struct netdev_queue_stats_rx *rx,
14955 				struct netdev_queue_stats_tx *tx)
14956 {
14957 	struct bnxt *bp = netdev_priv(dev);
14958 
14959 	rx->packets = bp->net_stats_prev.rx_packets;
14960 	rx->bytes = bp->net_stats_prev.rx_bytes;
14961 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
14962 
14963 	tx->packets = bp->net_stats_prev.tx_packets;
14964 	tx->bytes = bp->net_stats_prev.tx_bytes;
14965 }
14966 
14967 static const struct netdev_stat_ops bnxt_stat_ops = {
14968 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
14969 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
14970 	.get_base_stats		= bnxt_get_base_stats,
14971 };
14972 
14973 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
14974 {
14975 	u16 mem_size;
14976 
14977 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
14978 	mem_size = rxr->rx_agg_bmap_size / 8;
14979 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
14980 	if (!rxr->rx_agg_bmap)
14981 		return -ENOMEM;
14982 
14983 	return 0;
14984 }
14985 
14986 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx)
14987 {
14988 	struct bnxt_rx_ring_info *rxr, *clone;
14989 	struct bnxt *bp = netdev_priv(dev);
14990 	struct bnxt_ring_struct *ring;
14991 	int rc;
14992 
14993 	rxr = &bp->rx_ring[idx];
14994 	clone = qmem;
14995 	memcpy(clone, rxr, sizeof(*rxr));
14996 	bnxt_init_rx_ring_struct(bp, clone);
14997 	bnxt_reset_rx_ring_struct(bp, clone);
14998 
14999 	clone->rx_prod = 0;
15000 	clone->rx_agg_prod = 0;
15001 	clone->rx_sw_agg_prod = 0;
15002 	clone->rx_next_cons = 0;
15003 
15004 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
15005 	if (rc)
15006 		return rc;
15007 
15008 	ring = &clone->rx_ring_struct;
15009 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15010 	if (rc)
15011 		goto err_free_rx_ring;
15012 
15013 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
15014 		ring = &clone->rx_agg_ring_struct;
15015 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15016 		if (rc)
15017 			goto err_free_rx_agg_ring;
15018 
15019 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
15020 		if (rc)
15021 			goto err_free_rx_agg_ring;
15022 	}
15023 
15024 	bnxt_init_one_rx_ring_rxbd(bp, clone);
15025 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
15026 
15027 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
15028 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15029 		bnxt_alloc_one_rx_ring_page(bp, clone, idx);
15030 
15031 	return 0;
15032 
15033 err_free_rx_agg_ring:
15034 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
15035 err_free_rx_ring:
15036 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
15037 	clone->page_pool->p.napi = NULL;
15038 	page_pool_destroy(clone->page_pool);
15039 	clone->page_pool = NULL;
15040 	return rc;
15041 }
15042 
15043 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
15044 {
15045 	struct bnxt_rx_ring_info *rxr = qmem;
15046 	struct bnxt *bp = netdev_priv(dev);
15047 	struct bnxt_ring_struct *ring;
15048 
15049 	bnxt_free_one_rx_ring(bp, rxr);
15050 	bnxt_free_one_rx_agg_ring(bp, rxr);
15051 
15052 	/* At this point, this NAPI instance has another page pool associated
15053 	 * with it. Disconnect here before freeing the old page pool to avoid
15054 	 * warnings.
15055 	 */
15056 	rxr->page_pool->p.napi = NULL;
15057 	page_pool_destroy(rxr->page_pool);
15058 	rxr->page_pool = NULL;
15059 
15060 	ring = &rxr->rx_ring_struct;
15061 	bnxt_free_ring(bp, &ring->ring_mem);
15062 
15063 	ring = &rxr->rx_agg_ring_struct;
15064 	bnxt_free_ring(bp, &ring->ring_mem);
15065 
15066 	kfree(rxr->rx_agg_bmap);
15067 	rxr->rx_agg_bmap = NULL;
15068 }
15069 
15070 static void bnxt_copy_rx_ring(struct bnxt *bp,
15071 			      struct bnxt_rx_ring_info *dst,
15072 			      struct bnxt_rx_ring_info *src)
15073 {
15074 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
15075 	struct bnxt_ring_struct *dst_ring, *src_ring;
15076 	int i;
15077 
15078 	dst_ring = &dst->rx_ring_struct;
15079 	dst_rmem = &dst_ring->ring_mem;
15080 	src_ring = &src->rx_ring_struct;
15081 	src_rmem = &src_ring->ring_mem;
15082 
15083 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15084 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15085 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15086 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15087 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15088 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15089 
15090 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15091 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15092 	*dst_rmem->vmem = *src_rmem->vmem;
15093 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15094 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15095 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15096 	}
15097 
15098 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
15099 		return;
15100 
15101 	dst_ring = &dst->rx_agg_ring_struct;
15102 	dst_rmem = &dst_ring->ring_mem;
15103 	src_ring = &src->rx_agg_ring_struct;
15104 	src_rmem = &src_ring->ring_mem;
15105 
15106 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15107 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15108 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15109 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15110 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15111 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15112 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
15113 
15114 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15115 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15116 	*dst_rmem->vmem = *src_rmem->vmem;
15117 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15118 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15119 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15120 	}
15121 
15122 	dst->rx_agg_bmap = src->rx_agg_bmap;
15123 }
15124 
15125 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx)
15126 {
15127 	struct bnxt *bp = netdev_priv(dev);
15128 	struct bnxt_rx_ring_info *rxr, *clone;
15129 	struct bnxt_cp_ring_info *cpr;
15130 	int rc;
15131 
15132 	rxr = &bp->rx_ring[idx];
15133 	clone = qmem;
15134 
15135 	rxr->rx_prod = clone->rx_prod;
15136 	rxr->rx_agg_prod = clone->rx_agg_prod;
15137 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
15138 	rxr->rx_next_cons = clone->rx_next_cons;
15139 	rxr->page_pool = clone->page_pool;
15140 
15141 	bnxt_copy_rx_ring(bp, rxr, clone);
15142 
15143 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
15144 	if (rc)
15145 		return rc;
15146 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
15147 	if (rc)
15148 		goto err_free_hwrm_rx_ring;
15149 
15150 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
15151 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15152 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
15153 
15154 	napi_enable(&rxr->bnapi->napi);
15155 
15156 	cpr = &rxr->bnapi->cp_ring;
15157 	cpr->sw_stats->rx.rx_resets++;
15158 
15159 	return 0;
15160 
15161 err_free_hwrm_rx_ring:
15162 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15163 	return rc;
15164 }
15165 
15166 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
15167 {
15168 	struct bnxt *bp = netdev_priv(dev);
15169 	struct bnxt_rx_ring_info *rxr;
15170 
15171 	rxr = &bp->rx_ring[idx];
15172 	napi_disable(&rxr->bnapi->napi);
15173 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15174 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
15175 	rxr->rx_next_cons = 0;
15176 
15177 	memcpy(qmem, rxr, sizeof(*rxr));
15178 	bnxt_init_rx_ring_struct(bp, qmem);
15179 
15180 	return 0;
15181 }
15182 
15183 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
15184 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
15185 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
15186 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
15187 	.ndo_queue_start	= bnxt_queue_start,
15188 	.ndo_queue_stop		= bnxt_queue_stop,
15189 };
15190 
15191 static void bnxt_remove_one(struct pci_dev *pdev)
15192 {
15193 	struct net_device *dev = pci_get_drvdata(pdev);
15194 	struct bnxt *bp = netdev_priv(dev);
15195 
15196 	if (BNXT_PF(bp))
15197 		bnxt_sriov_disable(bp);
15198 
15199 	bnxt_rdma_aux_device_del(bp);
15200 
15201 	bnxt_ptp_clear(bp);
15202 	unregister_netdev(dev);
15203 
15204 	bnxt_rdma_aux_device_uninit(bp);
15205 
15206 	bnxt_free_l2_filters(bp, true);
15207 	bnxt_free_ntp_fltrs(bp, true);
15208 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
15209 		bnxt_clear_rss_ctxs(bp, true);
15210 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15211 	/* Flush any pending tasks */
15212 	cancel_work_sync(&bp->sp_task);
15213 	cancel_delayed_work_sync(&bp->fw_reset_task);
15214 	bp->sp_event = 0;
15215 
15216 	bnxt_dl_fw_reporters_destroy(bp);
15217 	bnxt_dl_unregister(bp);
15218 	bnxt_shutdown_tc(bp);
15219 
15220 	bnxt_clear_int_mode(bp);
15221 	bnxt_hwrm_func_drv_unrgtr(bp);
15222 	bnxt_free_hwrm_resources(bp);
15223 	bnxt_hwmon_uninit(bp);
15224 	bnxt_ethtool_free(bp);
15225 	bnxt_dcb_free(bp);
15226 	kfree(bp->ptp_cfg);
15227 	bp->ptp_cfg = NULL;
15228 	kfree(bp->fw_health);
15229 	bp->fw_health = NULL;
15230 	bnxt_cleanup_pci(bp);
15231 	bnxt_free_ctx_mem(bp);
15232 	kfree(bp->rss_indir_tbl);
15233 	bp->rss_indir_tbl = NULL;
15234 	bnxt_free_port_stats(bp);
15235 	free_netdev(dev);
15236 }
15237 
15238 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
15239 {
15240 	int rc = 0;
15241 	struct bnxt_link_info *link_info = &bp->link_info;
15242 
15243 	bp->phy_flags = 0;
15244 	rc = bnxt_hwrm_phy_qcaps(bp);
15245 	if (rc) {
15246 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
15247 			   rc);
15248 		return rc;
15249 	}
15250 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
15251 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
15252 	else
15253 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
15254 	if (!fw_dflt)
15255 		return 0;
15256 
15257 	mutex_lock(&bp->link_lock);
15258 	rc = bnxt_update_link(bp, false);
15259 	if (rc) {
15260 		mutex_unlock(&bp->link_lock);
15261 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
15262 			   rc);
15263 		return rc;
15264 	}
15265 
15266 	/* Older firmware does not have supported_auto_speeds, so assume
15267 	 * that all supported speeds can be autonegotiated.
15268 	 */
15269 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
15270 		link_info->support_auto_speeds = link_info->support_speeds;
15271 
15272 	bnxt_init_ethtool_link_settings(bp);
15273 	mutex_unlock(&bp->link_lock);
15274 	return 0;
15275 }
15276 
15277 static int bnxt_get_max_irq(struct pci_dev *pdev)
15278 {
15279 	u16 ctrl;
15280 
15281 	if (!pdev->msix_cap)
15282 		return 1;
15283 
15284 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
15285 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
15286 }
15287 
15288 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15289 				int *max_cp)
15290 {
15291 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
15292 	int max_ring_grps = 0, max_irq;
15293 
15294 	*max_tx = hw_resc->max_tx_rings;
15295 	*max_rx = hw_resc->max_rx_rings;
15296 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
15297 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
15298 			bnxt_get_ulp_msix_num_in_use(bp),
15299 			hw_resc->max_stat_ctxs -
15300 			bnxt_get_ulp_stat_ctxs_in_use(bp));
15301 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
15302 		*max_cp = min_t(int, *max_cp, max_irq);
15303 	max_ring_grps = hw_resc->max_hw_ring_grps;
15304 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
15305 		*max_cp -= 1;
15306 		*max_rx -= 2;
15307 	}
15308 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15309 		*max_rx >>= 1;
15310 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
15311 		int rc;
15312 
15313 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
15314 		if (rc) {
15315 			*max_rx = 0;
15316 			*max_tx = 0;
15317 		}
15318 		/* On P5 chips, max_cp output param should be available NQs */
15319 		*max_cp = max_irq;
15320 	}
15321 	*max_rx = min_t(int, *max_rx, max_ring_grps);
15322 }
15323 
15324 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
15325 {
15326 	int rx, tx, cp;
15327 
15328 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
15329 	*max_rx = rx;
15330 	*max_tx = tx;
15331 	if (!rx || !tx || !cp)
15332 		return -ENOMEM;
15333 
15334 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
15335 }
15336 
15337 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15338 			       bool shared)
15339 {
15340 	int rc;
15341 
15342 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
15343 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
15344 		/* Not enough rings, try disabling agg rings. */
15345 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
15346 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
15347 		if (rc) {
15348 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
15349 			bp->flags |= BNXT_FLAG_AGG_RINGS;
15350 			return rc;
15351 		}
15352 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
15353 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
15354 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
15355 		bnxt_set_ring_params(bp);
15356 	}
15357 
15358 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
15359 		int max_cp, max_stat, max_irq;
15360 
15361 		/* Reserve minimum resources for RoCE */
15362 		max_cp = bnxt_get_max_func_cp_rings(bp);
15363 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
15364 		max_irq = bnxt_get_max_func_irqs(bp);
15365 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
15366 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
15367 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
15368 			return 0;
15369 
15370 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
15371 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
15372 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
15373 		max_cp = min_t(int, max_cp, max_irq);
15374 		max_cp = min_t(int, max_cp, max_stat);
15375 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
15376 		if (rc)
15377 			rc = 0;
15378 	}
15379 	return rc;
15380 }
15381 
15382 /* In initial default shared ring setting, each shared ring must have a
15383  * RX/TX ring pair.
15384  */
15385 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
15386 {
15387 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
15388 	bp->rx_nr_rings = bp->cp_nr_rings;
15389 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
15390 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15391 }
15392 
15393 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
15394 {
15395 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
15396 	int avail_msix;
15397 
15398 	if (!bnxt_can_reserve_rings(bp))
15399 		return 0;
15400 
15401 	if (sh)
15402 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
15403 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
15404 	/* Reduce default rings on multi-port cards so that total default
15405 	 * rings do not exceed CPU count.
15406 	 */
15407 	if (bp->port_count > 1) {
15408 		int max_rings =
15409 			max_t(int, num_online_cpus() / bp->port_count, 1);
15410 
15411 		dflt_rings = min_t(int, dflt_rings, max_rings);
15412 	}
15413 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
15414 	if (rc)
15415 		return rc;
15416 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
15417 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
15418 	if (sh)
15419 		bnxt_trim_dflt_sh_rings(bp);
15420 	else
15421 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
15422 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15423 
15424 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
15425 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
15426 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
15427 
15428 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
15429 		bnxt_set_dflt_ulp_stat_ctxs(bp);
15430 	}
15431 
15432 	rc = __bnxt_reserve_rings(bp);
15433 	if (rc && rc != -ENODEV)
15434 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
15435 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15436 	if (sh)
15437 		bnxt_trim_dflt_sh_rings(bp);
15438 
15439 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
15440 	if (bnxt_need_reserve_rings(bp)) {
15441 		rc = __bnxt_reserve_rings(bp);
15442 		if (rc && rc != -ENODEV)
15443 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
15444 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15445 	}
15446 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
15447 		bp->rx_nr_rings++;
15448 		bp->cp_nr_rings++;
15449 	}
15450 	if (rc) {
15451 		bp->tx_nr_rings = 0;
15452 		bp->rx_nr_rings = 0;
15453 	}
15454 	return rc;
15455 }
15456 
15457 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
15458 {
15459 	int rc;
15460 
15461 	if (bp->tx_nr_rings)
15462 		return 0;
15463 
15464 	bnxt_ulp_irq_stop(bp);
15465 	bnxt_clear_int_mode(bp);
15466 	rc = bnxt_set_dflt_rings(bp, true);
15467 	if (rc) {
15468 		if (BNXT_VF(bp) && rc == -ENODEV)
15469 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
15470 		else
15471 			netdev_err(bp->dev, "Not enough rings available.\n");
15472 		goto init_dflt_ring_err;
15473 	}
15474 	rc = bnxt_init_int_mode(bp);
15475 	if (rc)
15476 		goto init_dflt_ring_err;
15477 
15478 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15479 
15480 	bnxt_set_dflt_rfs(bp);
15481 
15482 init_dflt_ring_err:
15483 	bnxt_ulp_irq_restart(bp, rc);
15484 	return rc;
15485 }
15486 
15487 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
15488 {
15489 	int rc;
15490 
15491 	ASSERT_RTNL();
15492 	bnxt_hwrm_func_qcaps(bp);
15493 
15494 	if (netif_running(bp->dev))
15495 		__bnxt_close_nic(bp, true, false);
15496 
15497 	bnxt_ulp_irq_stop(bp);
15498 	bnxt_clear_int_mode(bp);
15499 	rc = bnxt_init_int_mode(bp);
15500 	bnxt_ulp_irq_restart(bp, rc);
15501 
15502 	if (netif_running(bp->dev)) {
15503 		if (rc)
15504 			dev_close(bp->dev);
15505 		else
15506 			rc = bnxt_open_nic(bp, true, false);
15507 	}
15508 
15509 	return rc;
15510 }
15511 
15512 static int bnxt_init_mac_addr(struct bnxt *bp)
15513 {
15514 	int rc = 0;
15515 
15516 	if (BNXT_PF(bp)) {
15517 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
15518 	} else {
15519 #ifdef CONFIG_BNXT_SRIOV
15520 		struct bnxt_vf_info *vf = &bp->vf;
15521 		bool strict_approval = true;
15522 
15523 		if (is_valid_ether_addr(vf->mac_addr)) {
15524 			/* overwrite netdev dev_addr with admin VF MAC */
15525 			eth_hw_addr_set(bp->dev, vf->mac_addr);
15526 			/* Older PF driver or firmware may not approve this
15527 			 * correctly.
15528 			 */
15529 			strict_approval = false;
15530 		} else {
15531 			eth_hw_addr_random(bp->dev);
15532 		}
15533 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
15534 #endif
15535 	}
15536 	return rc;
15537 }
15538 
15539 static void bnxt_vpd_read_info(struct bnxt *bp)
15540 {
15541 	struct pci_dev *pdev = bp->pdev;
15542 	unsigned int vpd_size, kw_len;
15543 	int pos, size;
15544 	u8 *vpd_data;
15545 
15546 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
15547 	if (IS_ERR(vpd_data)) {
15548 		pci_warn(pdev, "Unable to read VPD\n");
15549 		return;
15550 	}
15551 
15552 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
15553 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
15554 	if (pos < 0)
15555 		goto read_sn;
15556 
15557 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
15558 	memcpy(bp->board_partno, &vpd_data[pos], size);
15559 
15560 read_sn:
15561 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
15562 					   PCI_VPD_RO_KEYWORD_SERIALNO,
15563 					   &kw_len);
15564 	if (pos < 0)
15565 		goto exit;
15566 
15567 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
15568 	memcpy(bp->board_serialno, &vpd_data[pos], size);
15569 exit:
15570 	kfree(vpd_data);
15571 }
15572 
15573 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
15574 {
15575 	struct pci_dev *pdev = bp->pdev;
15576 	u64 qword;
15577 
15578 	qword = pci_get_dsn(pdev);
15579 	if (!qword) {
15580 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
15581 		return -EOPNOTSUPP;
15582 	}
15583 
15584 	put_unaligned_le64(qword, dsn);
15585 
15586 	bp->flags |= BNXT_FLAG_DSN_VALID;
15587 	return 0;
15588 }
15589 
15590 static int bnxt_map_db_bar(struct bnxt *bp)
15591 {
15592 	if (!bp->db_size)
15593 		return -ENODEV;
15594 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
15595 	if (!bp->bar1)
15596 		return -ENOMEM;
15597 	return 0;
15598 }
15599 
15600 void bnxt_print_device_info(struct bnxt *bp)
15601 {
15602 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
15603 		    board_info[bp->board_idx].name,
15604 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
15605 
15606 	pcie_print_link_status(bp->pdev);
15607 }
15608 
15609 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
15610 {
15611 	struct bnxt_hw_resc *hw_resc;
15612 	struct net_device *dev;
15613 	struct bnxt *bp;
15614 	int rc, max_irqs;
15615 
15616 	if (pci_is_bridge(pdev))
15617 		return -ENODEV;
15618 
15619 	/* Clear any pending DMA transactions from crash kernel
15620 	 * while loading driver in capture kernel.
15621 	 */
15622 	if (is_kdump_kernel()) {
15623 		pci_clear_master(pdev);
15624 		pcie_flr(pdev);
15625 	}
15626 
15627 	max_irqs = bnxt_get_max_irq(pdev);
15628 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
15629 				 max_irqs);
15630 	if (!dev)
15631 		return -ENOMEM;
15632 
15633 	bp = netdev_priv(dev);
15634 	bp->board_idx = ent->driver_data;
15635 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
15636 	bnxt_set_max_func_irqs(bp, max_irqs);
15637 
15638 	if (bnxt_vf_pciid(bp->board_idx))
15639 		bp->flags |= BNXT_FLAG_VF;
15640 
15641 	/* No devlink port registration in case of a VF */
15642 	if (BNXT_PF(bp))
15643 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
15644 
15645 	if (pdev->msix_cap)
15646 		bp->flags |= BNXT_FLAG_MSIX_CAP;
15647 
15648 	rc = bnxt_init_board(pdev, dev);
15649 	if (rc < 0)
15650 		goto init_err_free;
15651 
15652 	dev->netdev_ops = &bnxt_netdev_ops;
15653 	dev->stat_ops = &bnxt_stat_ops;
15654 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
15655 	dev->ethtool_ops = &bnxt_ethtool_ops;
15656 	dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
15657 	pci_set_drvdata(pdev, dev);
15658 
15659 	rc = bnxt_alloc_hwrm_resources(bp);
15660 	if (rc)
15661 		goto init_err_pci_clean;
15662 
15663 	mutex_init(&bp->hwrm_cmd_lock);
15664 	mutex_init(&bp->link_lock);
15665 
15666 	rc = bnxt_fw_init_one_p1(bp);
15667 	if (rc)
15668 		goto init_err_pci_clean;
15669 
15670 	if (BNXT_PF(bp))
15671 		bnxt_vpd_read_info(bp);
15672 
15673 	if (BNXT_CHIP_P5_PLUS(bp)) {
15674 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
15675 		if (BNXT_CHIP_P7(bp))
15676 			bp->flags |= BNXT_FLAG_CHIP_P7;
15677 	}
15678 
15679 	rc = bnxt_alloc_rss_indir_tbl(bp, NULL);
15680 	if (rc)
15681 		goto init_err_pci_clean;
15682 
15683 	rc = bnxt_fw_init_one_p2(bp);
15684 	if (rc)
15685 		goto init_err_pci_clean;
15686 
15687 	rc = bnxt_map_db_bar(bp);
15688 	if (rc) {
15689 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
15690 			rc);
15691 		goto init_err_pci_clean;
15692 	}
15693 
15694 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
15695 			   NETIF_F_TSO | NETIF_F_TSO6 |
15696 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
15697 			   NETIF_F_GSO_IPXIP4 |
15698 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
15699 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
15700 			   NETIF_F_RXCSUM | NETIF_F_GRO;
15701 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
15702 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
15703 
15704 	if (BNXT_SUPPORTS_TPA(bp))
15705 		dev->hw_features |= NETIF_F_LRO;
15706 
15707 	dev->hw_enc_features =
15708 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
15709 			NETIF_F_TSO | NETIF_F_TSO6 |
15710 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
15711 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
15712 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
15713 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
15714 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
15715 	if (bp->flags & BNXT_FLAG_CHIP_P7)
15716 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
15717 	else
15718 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
15719 
15720 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
15721 				    NETIF_F_GSO_GRE_CSUM;
15722 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
15723 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
15724 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
15725 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
15726 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
15727 	if (BNXT_SUPPORTS_TPA(bp))
15728 		dev->hw_features |= NETIF_F_GRO_HW;
15729 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
15730 	if (dev->features & NETIF_F_GRO_HW)
15731 		dev->features &= ~NETIF_F_LRO;
15732 	dev->priv_flags |= IFF_UNICAST_FLT;
15733 
15734 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
15735 	if (bp->tso_max_segs)
15736 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
15737 
15738 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
15739 			    NETDEV_XDP_ACT_RX_SG;
15740 
15741 #ifdef CONFIG_BNXT_SRIOV
15742 	init_waitqueue_head(&bp->sriov_cfg_wait);
15743 #endif
15744 	if (BNXT_SUPPORTS_TPA(bp)) {
15745 		bp->gro_func = bnxt_gro_func_5730x;
15746 		if (BNXT_CHIP_P4(bp))
15747 			bp->gro_func = bnxt_gro_func_5731x;
15748 		else if (BNXT_CHIP_P5_PLUS(bp))
15749 			bp->gro_func = bnxt_gro_func_5750x;
15750 	}
15751 	if (!BNXT_CHIP_P4_PLUS(bp))
15752 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
15753 
15754 	rc = bnxt_init_mac_addr(bp);
15755 	if (rc) {
15756 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
15757 		rc = -EADDRNOTAVAIL;
15758 		goto init_err_pci_clean;
15759 	}
15760 
15761 	if (BNXT_PF(bp)) {
15762 		/* Read the adapter's DSN to use as the eswitch switch_id */
15763 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
15764 	}
15765 
15766 	/* MTU range: 60 - FW defined max */
15767 	dev->min_mtu = ETH_ZLEN;
15768 	dev->max_mtu = bp->max_mtu;
15769 
15770 	rc = bnxt_probe_phy(bp, true);
15771 	if (rc)
15772 		goto init_err_pci_clean;
15773 
15774 	hw_resc = &bp->hw_resc;
15775 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
15776 		       BNXT_L2_FLTR_MAX_FLTR;
15777 	/* Older firmware may not report these filters properly */
15778 	if (bp->max_fltr < BNXT_MAX_FLTR)
15779 		bp->max_fltr = BNXT_MAX_FLTR;
15780 	bnxt_init_l2_fltr_tbl(bp);
15781 	bnxt_set_rx_skb_mode(bp, false);
15782 	bnxt_set_tpa_flags(bp);
15783 	bnxt_set_ring_params(bp);
15784 	bnxt_rdma_aux_device_init(bp);
15785 	rc = bnxt_set_dflt_rings(bp, true);
15786 	if (rc) {
15787 		if (BNXT_VF(bp) && rc == -ENODEV) {
15788 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
15789 		} else {
15790 			netdev_err(bp->dev, "Not enough rings available.\n");
15791 			rc = -ENOMEM;
15792 		}
15793 		goto init_err_pci_clean;
15794 	}
15795 
15796 	bnxt_fw_init_one_p3(bp);
15797 
15798 	bnxt_init_dflt_coal(bp);
15799 
15800 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
15801 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
15802 
15803 	rc = bnxt_init_int_mode(bp);
15804 	if (rc)
15805 		goto init_err_pci_clean;
15806 
15807 	/* No TC has been set yet and rings may have been trimmed due to
15808 	 * limited MSIX, so we re-initialize the TX rings per TC.
15809 	 */
15810 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15811 
15812 	if (BNXT_PF(bp)) {
15813 		if (!bnxt_pf_wq) {
15814 			bnxt_pf_wq =
15815 				create_singlethread_workqueue("bnxt_pf_wq");
15816 			if (!bnxt_pf_wq) {
15817 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
15818 				rc = -ENOMEM;
15819 				goto init_err_pci_clean;
15820 			}
15821 		}
15822 		rc = bnxt_init_tc(bp);
15823 		if (rc)
15824 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
15825 				   rc);
15826 	}
15827 
15828 	bnxt_inv_fw_health_reg(bp);
15829 	rc = bnxt_dl_register(bp);
15830 	if (rc)
15831 		goto init_err_dl;
15832 
15833 	INIT_LIST_HEAD(&bp->usr_fltr_list);
15834 
15835 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
15836 		bnxt_init_multi_rss_ctx(bp);
15837 
15838 
15839 	rc = register_netdev(dev);
15840 	if (rc)
15841 		goto init_err_cleanup;
15842 
15843 	bnxt_dl_fw_reporters_create(bp);
15844 
15845 	bnxt_rdma_aux_device_add(bp);
15846 
15847 	bnxt_print_device_info(bp);
15848 
15849 	pci_save_state(pdev);
15850 
15851 	return 0;
15852 init_err_cleanup:
15853 	bnxt_rdma_aux_device_uninit(bp);
15854 	bnxt_dl_unregister(bp);
15855 init_err_dl:
15856 	bnxt_shutdown_tc(bp);
15857 	bnxt_clear_int_mode(bp);
15858 
15859 init_err_pci_clean:
15860 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
15861 		bnxt_clear_rss_ctxs(bp, true);
15862 	bnxt_hwrm_func_drv_unrgtr(bp);
15863 	bnxt_free_hwrm_resources(bp);
15864 	bnxt_hwmon_uninit(bp);
15865 	bnxt_ethtool_free(bp);
15866 	bnxt_ptp_clear(bp);
15867 	kfree(bp->ptp_cfg);
15868 	bp->ptp_cfg = NULL;
15869 	kfree(bp->fw_health);
15870 	bp->fw_health = NULL;
15871 	bnxt_cleanup_pci(bp);
15872 	bnxt_free_ctx_mem(bp);
15873 	kfree(bp->rss_indir_tbl);
15874 	bp->rss_indir_tbl = NULL;
15875 
15876 init_err_free:
15877 	free_netdev(dev);
15878 	return rc;
15879 }
15880 
15881 static void bnxt_shutdown(struct pci_dev *pdev)
15882 {
15883 	struct net_device *dev = pci_get_drvdata(pdev);
15884 	struct bnxt *bp;
15885 
15886 	if (!dev)
15887 		return;
15888 
15889 	rtnl_lock();
15890 	bp = netdev_priv(dev);
15891 	if (!bp)
15892 		goto shutdown_exit;
15893 
15894 	if (netif_running(dev))
15895 		dev_close(dev);
15896 
15897 	bnxt_clear_int_mode(bp);
15898 	pci_disable_device(pdev);
15899 
15900 	if (system_state == SYSTEM_POWER_OFF) {
15901 		pci_wake_from_d3(pdev, bp->wol);
15902 		pci_set_power_state(pdev, PCI_D3hot);
15903 	}
15904 
15905 shutdown_exit:
15906 	rtnl_unlock();
15907 }
15908 
15909 #ifdef CONFIG_PM_SLEEP
15910 static int bnxt_suspend(struct device *device)
15911 {
15912 	struct net_device *dev = dev_get_drvdata(device);
15913 	struct bnxt *bp = netdev_priv(dev);
15914 	int rc = 0;
15915 
15916 	bnxt_ulp_stop(bp);
15917 
15918 	rtnl_lock();
15919 	if (netif_running(dev)) {
15920 		netif_device_detach(dev);
15921 		rc = bnxt_close(dev);
15922 	}
15923 	bnxt_hwrm_func_drv_unrgtr(bp);
15924 	pci_disable_device(bp->pdev);
15925 	bnxt_free_ctx_mem(bp);
15926 	rtnl_unlock();
15927 	return rc;
15928 }
15929 
15930 static int bnxt_resume(struct device *device)
15931 {
15932 	struct net_device *dev = dev_get_drvdata(device);
15933 	struct bnxt *bp = netdev_priv(dev);
15934 	int rc = 0;
15935 
15936 	rtnl_lock();
15937 	rc = pci_enable_device(bp->pdev);
15938 	if (rc) {
15939 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
15940 			   rc);
15941 		goto resume_exit;
15942 	}
15943 	pci_set_master(bp->pdev);
15944 	if (bnxt_hwrm_ver_get(bp)) {
15945 		rc = -ENODEV;
15946 		goto resume_exit;
15947 	}
15948 	rc = bnxt_hwrm_func_reset(bp);
15949 	if (rc) {
15950 		rc = -EBUSY;
15951 		goto resume_exit;
15952 	}
15953 
15954 	rc = bnxt_hwrm_func_qcaps(bp);
15955 	if (rc)
15956 		goto resume_exit;
15957 
15958 	bnxt_clear_reservations(bp, true);
15959 
15960 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
15961 		rc = -ENODEV;
15962 		goto resume_exit;
15963 	}
15964 
15965 	bnxt_get_wol_settings(bp);
15966 	if (netif_running(dev)) {
15967 		rc = bnxt_open(dev);
15968 		if (!rc)
15969 			netif_device_attach(dev);
15970 	}
15971 
15972 resume_exit:
15973 	rtnl_unlock();
15974 	bnxt_ulp_start(bp, rc);
15975 	if (!rc)
15976 		bnxt_reenable_sriov(bp);
15977 	return rc;
15978 }
15979 
15980 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
15981 #define BNXT_PM_OPS (&bnxt_pm_ops)
15982 
15983 #else
15984 
15985 #define BNXT_PM_OPS NULL
15986 
15987 #endif /* CONFIG_PM_SLEEP */
15988 
15989 /**
15990  * bnxt_io_error_detected - called when PCI error is detected
15991  * @pdev: Pointer to PCI device
15992  * @state: The current pci connection state
15993  *
15994  * This function is called after a PCI bus error affecting
15995  * this device has been detected.
15996  */
15997 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
15998 					       pci_channel_state_t state)
15999 {
16000 	struct net_device *netdev = pci_get_drvdata(pdev);
16001 	struct bnxt *bp = netdev_priv(netdev);
16002 	bool abort = false;
16003 
16004 	netdev_info(netdev, "PCI I/O error detected\n");
16005 
16006 	bnxt_ulp_stop(bp);
16007 
16008 	rtnl_lock();
16009 	netif_device_detach(netdev);
16010 
16011 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
16012 		netdev_err(bp->dev, "Firmware reset already in progress\n");
16013 		abort = true;
16014 	}
16015 
16016 	if (abort || state == pci_channel_io_perm_failure) {
16017 		rtnl_unlock();
16018 		return PCI_ERS_RESULT_DISCONNECT;
16019 	}
16020 
16021 	/* Link is not reliable anymore if state is pci_channel_io_frozen
16022 	 * so we disable bus master to prevent any potential bad DMAs before
16023 	 * freeing kernel memory.
16024 	 */
16025 	if (state == pci_channel_io_frozen) {
16026 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
16027 		bnxt_fw_fatal_close(bp);
16028 	}
16029 
16030 	if (netif_running(netdev))
16031 		__bnxt_close_nic(bp, true, true);
16032 
16033 	if (pci_is_enabled(pdev))
16034 		pci_disable_device(pdev);
16035 	bnxt_free_ctx_mem(bp);
16036 	rtnl_unlock();
16037 
16038 	/* Request a slot slot reset. */
16039 	return PCI_ERS_RESULT_NEED_RESET;
16040 }
16041 
16042 /**
16043  * bnxt_io_slot_reset - called after the pci bus has been reset.
16044  * @pdev: Pointer to PCI device
16045  *
16046  * Restart the card from scratch, as if from a cold-boot.
16047  * At this point, the card has exprienced a hard reset,
16048  * followed by fixups by BIOS, and has its config space
16049  * set up identically to what it was at cold boot.
16050  */
16051 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
16052 {
16053 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
16054 	struct net_device *netdev = pci_get_drvdata(pdev);
16055 	struct bnxt *bp = netdev_priv(netdev);
16056 	int retry = 0;
16057 	int err = 0;
16058 	int off;
16059 
16060 	netdev_info(bp->dev, "PCI Slot Reset\n");
16061 
16062 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16063 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
16064 		msleep(900);
16065 
16066 	rtnl_lock();
16067 
16068 	if (pci_enable_device(pdev)) {
16069 		dev_err(&pdev->dev,
16070 			"Cannot re-enable PCI device after reset.\n");
16071 	} else {
16072 		pci_set_master(pdev);
16073 		/* Upon fatal error, our device internal logic that latches to
16074 		 * BAR value is getting reset and will restore only upon
16075 		 * rewritting the BARs.
16076 		 *
16077 		 * As pci_restore_state() does not re-write the BARs if the
16078 		 * value is same as saved value earlier, driver needs to
16079 		 * write the BARs to 0 to force restore, in case of fatal error.
16080 		 */
16081 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
16082 				       &bp->state)) {
16083 			for (off = PCI_BASE_ADDRESS_0;
16084 			     off <= PCI_BASE_ADDRESS_5; off += 4)
16085 				pci_write_config_dword(bp->pdev, off, 0);
16086 		}
16087 		pci_restore_state(pdev);
16088 		pci_save_state(pdev);
16089 
16090 		bnxt_inv_fw_health_reg(bp);
16091 		bnxt_try_map_fw_health_reg(bp);
16092 
16093 		/* In some PCIe AER scenarios, firmware may take up to
16094 		 * 10 seconds to become ready in the worst case.
16095 		 */
16096 		do {
16097 			err = bnxt_try_recover_fw(bp);
16098 			if (!err)
16099 				break;
16100 			retry++;
16101 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
16102 
16103 		if (err) {
16104 			dev_err(&pdev->dev, "Firmware not ready\n");
16105 			goto reset_exit;
16106 		}
16107 
16108 		err = bnxt_hwrm_func_reset(bp);
16109 		if (!err)
16110 			result = PCI_ERS_RESULT_RECOVERED;
16111 
16112 		bnxt_ulp_irq_stop(bp);
16113 		bnxt_clear_int_mode(bp);
16114 		err = bnxt_init_int_mode(bp);
16115 		bnxt_ulp_irq_restart(bp, err);
16116 	}
16117 
16118 reset_exit:
16119 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16120 	bnxt_clear_reservations(bp, true);
16121 	rtnl_unlock();
16122 
16123 	return result;
16124 }
16125 
16126 /**
16127  * bnxt_io_resume - called when traffic can start flowing again.
16128  * @pdev: Pointer to PCI device
16129  *
16130  * This callback is called when the error recovery driver tells
16131  * us that its OK to resume normal operation.
16132  */
16133 static void bnxt_io_resume(struct pci_dev *pdev)
16134 {
16135 	struct net_device *netdev = pci_get_drvdata(pdev);
16136 	struct bnxt *bp = netdev_priv(netdev);
16137 	int err;
16138 
16139 	netdev_info(bp->dev, "PCI Slot Resume\n");
16140 	rtnl_lock();
16141 
16142 	err = bnxt_hwrm_func_qcaps(bp);
16143 	if (!err && netif_running(netdev))
16144 		err = bnxt_open(netdev);
16145 
16146 	if (!err)
16147 		netif_device_attach(netdev);
16148 
16149 	rtnl_unlock();
16150 	bnxt_ulp_start(bp, err);
16151 	if (!err)
16152 		bnxt_reenable_sriov(bp);
16153 }
16154 
16155 static const struct pci_error_handlers bnxt_err_handler = {
16156 	.error_detected	= bnxt_io_error_detected,
16157 	.slot_reset	= bnxt_io_slot_reset,
16158 	.resume		= bnxt_io_resume
16159 };
16160 
16161 static struct pci_driver bnxt_pci_driver = {
16162 	.name		= DRV_MODULE_NAME,
16163 	.id_table	= bnxt_pci_tbl,
16164 	.probe		= bnxt_init_one,
16165 	.remove		= bnxt_remove_one,
16166 	.shutdown	= bnxt_shutdown,
16167 	.driver.pm	= BNXT_PM_OPS,
16168 	.err_handler	= &bnxt_err_handler,
16169 #if defined(CONFIG_BNXT_SRIOV)
16170 	.sriov_configure = bnxt_sriov_configure,
16171 #endif
16172 };
16173 
16174 static int __init bnxt_init(void)
16175 {
16176 	int err;
16177 
16178 	bnxt_debug_init();
16179 	err = pci_register_driver(&bnxt_pci_driver);
16180 	if (err) {
16181 		bnxt_debug_exit();
16182 		return err;
16183 	}
16184 
16185 	return 0;
16186 }
16187 
16188 static void __exit bnxt_exit(void)
16189 {
16190 	pci_unregister_driver(&bnxt_pci_driver);
16191 	if (bnxt_pf_wq)
16192 		destroy_workqueue(bnxt_pf_wq);
16193 	bnxt_debug_exit();
16194 }
16195 
16196 module_init(bnxt_init);
16197 module_exit(bnxt_exit);
16198