xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision a79993b5fce69e97f900bb975f6127e25cebf130)
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_coredump.h"
73 #include "bnxt_hwmon.h"
74 
75 #define BNXT_TX_TIMEOUT		(5 * HZ)
76 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
77 				 NETIF_MSG_TX_ERR)
78 
79 MODULE_LICENSE("GPL");
80 MODULE_DESCRIPTION("Broadcom NetXtreme network driver");
81 
82 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
83 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
84 #define BNXT_RX_COPY_THRESH 256
85 
86 #define BNXT_TX_PUSH_THRESH 164
87 
88 /* indexed by enum board_idx */
89 static const struct {
90 	char *name;
91 } board_info[] = {
92 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
93 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
94 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
95 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
96 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
97 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
98 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
99 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
100 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
101 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
102 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
103 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
104 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
105 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
106 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
108 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
109 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
110 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
111 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
112 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
113 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
114 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
115 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
116 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
117 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
118 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
119 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
120 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
121 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
122 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
123 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
124 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
125 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
126 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
127 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
128 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
129 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
130 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
131 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
132 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
133 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
134 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
135 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
136 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
137 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
138 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
139 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
140 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
141 	[NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" },
142 };
143 
144 static const struct pci_device_id bnxt_pci_tbl[] = {
145 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
146 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
147 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
148 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
149 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
150 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
151 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
152 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
153 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
155 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
159 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
160 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
164 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
165 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
166 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
167 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
168 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
169 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
170 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
171 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
172 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
173 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
174 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
175 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
176 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
178 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
179 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
180 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
181 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
182 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
183 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
184 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
185 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
187 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
188 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
189 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
190 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
191 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
192 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
193 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
194 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
195 #ifdef CONFIG_BNXT_SRIOV
196 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
197 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
198 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
199 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
200 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
201 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
202 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
203 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
204 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
205 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
206 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
207 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
208 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
209 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
210 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
211 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
212 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
214 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
215 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
216 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
217 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
218 #endif
219 	{ 0 }
220 };
221 
222 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
223 
224 static const u16 bnxt_vf_req_snif[] = {
225 	HWRM_FUNC_CFG,
226 	HWRM_FUNC_VF_CFG,
227 	HWRM_PORT_PHY_QCFG,
228 	HWRM_CFA_L2_FILTER_ALLOC,
229 };
230 
231 static const u16 bnxt_async_events_arr[] = {
232 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
233 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
234 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
235 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
236 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
237 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
238 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
239 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
240 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
241 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
242 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
243 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
244 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
245 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
246 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
247 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
248 };
249 
250 static struct workqueue_struct *bnxt_pf_wq;
251 
252 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
253 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
254 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
255 
256 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
257 	.ports = {
258 		.src = 0,
259 		.dst = 0,
260 	},
261 	.addrs = {
262 		.v6addrs = {
263 			.src = BNXT_IPV6_MASK_NONE,
264 			.dst = BNXT_IPV6_MASK_NONE,
265 		},
266 	},
267 };
268 
269 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
270 	.ports = {
271 		.src = cpu_to_be16(0xffff),
272 		.dst = cpu_to_be16(0xffff),
273 	},
274 	.addrs = {
275 		.v6addrs = {
276 			.src = BNXT_IPV6_MASK_ALL,
277 			.dst = BNXT_IPV6_MASK_ALL,
278 		},
279 	},
280 };
281 
282 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
283 	.ports = {
284 		.src = cpu_to_be16(0xffff),
285 		.dst = cpu_to_be16(0xffff),
286 	},
287 	.addrs = {
288 		.v4addrs = {
289 			.src = cpu_to_be32(0xffffffff),
290 			.dst = cpu_to_be32(0xffffffff),
291 		},
292 	},
293 };
294 
295 static bool bnxt_vf_pciid(enum board_idx idx)
296 {
297 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
298 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
299 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
300 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF);
301 }
302 
303 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
304 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
305 
306 #define BNXT_DB_CQ(db, idx)						\
307 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
308 
309 #define BNXT_DB_NQ_P5(db, idx)						\
310 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
311 		    (db)->doorbell)
312 
313 #define BNXT_DB_NQ_P7(db, idx)						\
314 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
315 		    DB_RING_IDX(db, idx), (db)->doorbell)
316 
317 #define BNXT_DB_CQ_ARM(db, idx)						\
318 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
319 
320 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
321 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
322 		    DB_RING_IDX(db, idx), (db)->doorbell)
323 
324 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
325 {
326 	if (bp->flags & BNXT_FLAG_CHIP_P7)
327 		BNXT_DB_NQ_P7(db, idx);
328 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
329 		BNXT_DB_NQ_P5(db, idx);
330 	else
331 		BNXT_DB_CQ(db, idx);
332 }
333 
334 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
335 {
336 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
337 		BNXT_DB_NQ_ARM_P5(db, idx);
338 	else
339 		BNXT_DB_CQ_ARM(db, idx);
340 }
341 
342 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
343 {
344 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
345 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
346 			    DB_RING_IDX(db, idx), db->doorbell);
347 	else
348 		BNXT_DB_CQ(db, idx);
349 }
350 
351 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
352 {
353 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
354 		return;
355 
356 	if (BNXT_PF(bp))
357 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
358 	else
359 		schedule_delayed_work(&bp->fw_reset_task, delay);
360 }
361 
362 static void __bnxt_queue_sp_work(struct bnxt *bp)
363 {
364 	if (BNXT_PF(bp))
365 		queue_work(bnxt_pf_wq, &bp->sp_task);
366 	else
367 		schedule_work(&bp->sp_task);
368 }
369 
370 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
371 {
372 	set_bit(event, &bp->sp_event);
373 	__bnxt_queue_sp_work(bp);
374 }
375 
376 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
377 {
378 	if (!rxr->bnapi->in_reset) {
379 		rxr->bnapi->in_reset = true;
380 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
381 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
382 		else
383 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
384 		__bnxt_queue_sp_work(bp);
385 	}
386 	rxr->rx_next_cons = 0xffff;
387 }
388 
389 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
390 			  u16 curr)
391 {
392 	struct bnxt_napi *bnapi = txr->bnapi;
393 
394 	if (bnapi->tx_fault)
395 		return;
396 
397 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
398 		   txr->txq_index, txr->tx_hw_cons,
399 		   txr->tx_cons, txr->tx_prod, curr);
400 	WARN_ON_ONCE(1);
401 	bnapi->tx_fault = 1;
402 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
403 }
404 
405 const u16 bnxt_lhint_arr[] = {
406 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
407 	TX_BD_FLAGS_LHINT_512_TO_1023,
408 	TX_BD_FLAGS_LHINT_1024_TO_2047,
409 	TX_BD_FLAGS_LHINT_1024_TO_2047,
410 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
411 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
412 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
413 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
414 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
415 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
416 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
417 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
418 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
419 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
420 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
421 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
422 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
423 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
424 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
425 };
426 
427 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
428 {
429 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
430 
431 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
432 		return 0;
433 
434 	return md_dst->u.port_info.port_id;
435 }
436 
437 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
438 			     u16 prod)
439 {
440 	/* Sync BD data before updating doorbell */
441 	wmb();
442 	bnxt_db_write(bp, &txr->tx_db, prod);
443 	txr->kick_pending = 0;
444 }
445 
446 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
447 {
448 	struct bnxt *bp = netdev_priv(dev);
449 	struct tx_bd *txbd, *txbd0;
450 	struct tx_bd_ext *txbd1;
451 	struct netdev_queue *txq;
452 	int i;
453 	dma_addr_t mapping;
454 	unsigned int length, pad = 0;
455 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
456 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
457 	struct pci_dev *pdev = bp->pdev;
458 	u16 prod, last_frag, txts_prod;
459 	struct bnxt_tx_ring_info *txr;
460 	struct bnxt_sw_tx_bd *tx_buf;
461 	__le32 lflags = 0;
462 
463 	i = skb_get_queue_mapping(skb);
464 	if (unlikely(i >= bp->tx_nr_rings)) {
465 		dev_kfree_skb_any(skb);
466 		dev_core_stats_tx_dropped_inc(dev);
467 		return NETDEV_TX_OK;
468 	}
469 
470 	txq = netdev_get_tx_queue(dev, i);
471 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
472 	prod = txr->tx_prod;
473 
474 	free_size = bnxt_tx_avail(bp, txr);
475 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
476 		/* We must have raced with NAPI cleanup */
477 		if (net_ratelimit() && txr->kick_pending)
478 			netif_warn(bp, tx_err, dev,
479 				   "bnxt: ring busy w/ flush pending!\n");
480 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
481 					bp->tx_wake_thresh))
482 			return NETDEV_TX_BUSY;
483 	}
484 
485 	if (unlikely(ipv6_hopopt_jumbo_remove(skb)))
486 		goto tx_free;
487 
488 	length = skb->len;
489 	len = skb_headlen(skb);
490 	last_frag = skb_shinfo(skb)->nr_frags;
491 
492 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
493 
494 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
495 	tx_buf->skb = skb;
496 	tx_buf->nr_frags = last_frag;
497 
498 	vlan_tag_flags = 0;
499 	cfa_action = bnxt_xmit_get_cfa_action(skb);
500 	if (skb_vlan_tag_present(skb)) {
501 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
502 				 skb_vlan_tag_get(skb);
503 		/* Currently supports 8021Q, 8021AD vlan offloads
504 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
505 		 */
506 		if (skb->vlan_proto == htons(ETH_P_8021Q))
507 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
508 	}
509 
510 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp &&
511 	    ptp->tx_tstamp_en) {
512 		if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) {
513 			lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
514 			tx_buf->is_ts_pkt = 1;
515 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
516 		} else if (!skb_is_gso(skb)) {
517 			u16 seq_id, hdr_off;
518 
519 			if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) &&
520 			    !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) {
521 				if (vlan_tag_flags)
522 					hdr_off += VLAN_HLEN;
523 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
524 				tx_buf->is_ts_pkt = 1;
525 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
526 
527 				ptp->txts_req[txts_prod].tx_seqid = seq_id;
528 				ptp->txts_req[txts_prod].tx_hdr_off = hdr_off;
529 				tx_buf->txts_prod = txts_prod;
530 			}
531 		}
532 	}
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 		txr->tx_buf_ring[txr->tx_prod].is_ts_pkt = 0;
762 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
763 		if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
764 			/* set SKB to err so PTP worker will clean up */
765 			ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO);
766 	}
767 	if (txr->kick_pending)
768 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
769 	txr->tx_buf_ring[txr->tx_prod].skb = NULL;
770 	dev_core_stats_tx_dropped_inc(dev);
771 	return NETDEV_TX_OK;
772 }
773 
774 /* Returns true if some remaining TX packets not processed. */
775 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
776 			  int budget)
777 {
778 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
779 	struct pci_dev *pdev = bp->pdev;
780 	u16 hw_cons = txr->tx_hw_cons;
781 	unsigned int tx_bytes = 0;
782 	u16 cons = txr->tx_cons;
783 	int tx_pkts = 0;
784 	bool rc = false;
785 
786 	while (RING_TX(bp, cons) != hw_cons) {
787 		struct bnxt_sw_tx_bd *tx_buf;
788 		struct sk_buff *skb;
789 		bool is_ts_pkt;
790 		int j, last;
791 
792 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
793 		skb = tx_buf->skb;
794 
795 		if (unlikely(!skb)) {
796 			bnxt_sched_reset_txr(bp, txr, cons);
797 			return rc;
798 		}
799 
800 		is_ts_pkt = tx_buf->is_ts_pkt;
801 		if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) {
802 			rc = true;
803 			break;
804 		}
805 
806 		cons = NEXT_TX(cons);
807 		tx_pkts++;
808 		tx_bytes += skb->len;
809 		tx_buf->skb = NULL;
810 		tx_buf->is_ts_pkt = 0;
811 
812 		if (tx_buf->is_push) {
813 			tx_buf->is_push = 0;
814 			goto next_tx_int;
815 		}
816 
817 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
818 				 skb_headlen(skb), DMA_TO_DEVICE);
819 		last = tx_buf->nr_frags;
820 
821 		for (j = 0; j < last; j++) {
822 			cons = NEXT_TX(cons);
823 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
824 			dma_unmap_page(
825 				&pdev->dev,
826 				dma_unmap_addr(tx_buf, mapping),
827 				skb_frag_size(&skb_shinfo(skb)->frags[j]),
828 				DMA_TO_DEVICE);
829 		}
830 		if (unlikely(is_ts_pkt)) {
831 			if (BNXT_CHIP_P5(bp)) {
832 				/* PTP worker takes ownership of the skb */
833 				bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod);
834 				skb = NULL;
835 			}
836 		}
837 
838 next_tx_int:
839 		cons = NEXT_TX(cons);
840 
841 		dev_consume_skb_any(skb);
842 	}
843 
844 	WRITE_ONCE(txr->tx_cons, cons);
845 
846 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
847 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
848 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
849 
850 	return rc;
851 }
852 
853 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
854 {
855 	struct bnxt_tx_ring_info *txr;
856 	bool more = false;
857 	int i;
858 
859 	bnxt_for_each_napi_tx(i, bnapi, txr) {
860 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
861 			more |= __bnxt_tx_int(bp, txr, budget);
862 	}
863 	if (!more)
864 		bnapi->events &= ~BNXT_TX_CMP_EVENT;
865 }
866 
867 static bool bnxt_separate_head_pool(void)
868 {
869 	return PAGE_SIZE > BNXT_RX_PAGE_SIZE;
870 }
871 
872 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
873 					 struct bnxt_rx_ring_info *rxr,
874 					 unsigned int *offset,
875 					 gfp_t gfp)
876 {
877 	struct page *page;
878 
879 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
880 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
881 						BNXT_RX_PAGE_SIZE);
882 	} else {
883 		page = page_pool_dev_alloc_pages(rxr->page_pool);
884 		*offset = 0;
885 	}
886 	if (!page)
887 		return NULL;
888 
889 	*mapping = page_pool_get_dma_addr(page) + *offset;
890 	return page;
891 }
892 
893 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
894 				       struct bnxt_rx_ring_info *rxr,
895 				       gfp_t gfp)
896 {
897 	unsigned int offset;
898 	struct page *page;
899 
900 	page = page_pool_alloc_frag(rxr->head_pool, &offset,
901 				    bp->rx_buf_size, gfp);
902 	if (!page)
903 		return NULL;
904 
905 	*mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset;
906 	return page_address(page) + offset;
907 }
908 
909 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
910 		       u16 prod, gfp_t gfp)
911 {
912 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
913 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
914 	dma_addr_t mapping;
915 
916 	if (BNXT_RX_PAGE_MODE(bp)) {
917 		unsigned int offset;
918 		struct page *page =
919 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
920 
921 		if (!page)
922 			return -ENOMEM;
923 
924 		mapping += bp->rx_dma_offset;
925 		rx_buf->data = page;
926 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
927 	} else {
928 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp);
929 
930 		if (!data)
931 			return -ENOMEM;
932 
933 		rx_buf->data = data;
934 		rx_buf->data_ptr = data + bp->rx_offset;
935 	}
936 	rx_buf->mapping = mapping;
937 
938 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
939 	return 0;
940 }
941 
942 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
943 {
944 	u16 prod = rxr->rx_prod;
945 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
946 	struct bnxt *bp = rxr->bnapi->bp;
947 	struct rx_bd *cons_bd, *prod_bd;
948 
949 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
950 	cons_rx_buf = &rxr->rx_buf_ring[cons];
951 
952 	prod_rx_buf->data = data;
953 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
954 
955 	prod_rx_buf->mapping = cons_rx_buf->mapping;
956 
957 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
958 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
959 
960 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
961 }
962 
963 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
964 {
965 	u16 next, max = rxr->rx_agg_bmap_size;
966 
967 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
968 	if (next >= max)
969 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
970 	return next;
971 }
972 
973 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
974 				     struct bnxt_rx_ring_info *rxr,
975 				     u16 prod, gfp_t gfp)
976 {
977 	struct rx_bd *rxbd =
978 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
979 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
980 	struct page *page;
981 	dma_addr_t mapping;
982 	u16 sw_prod = rxr->rx_sw_agg_prod;
983 	unsigned int offset = 0;
984 
985 	page = __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
986 
987 	if (!page)
988 		return -ENOMEM;
989 
990 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
991 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
992 
993 	__set_bit(sw_prod, rxr->rx_agg_bmap);
994 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
995 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
996 
997 	rx_agg_buf->page = page;
998 	rx_agg_buf->offset = offset;
999 	rx_agg_buf->mapping = mapping;
1000 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1001 	rxbd->rx_bd_opaque = sw_prod;
1002 	return 0;
1003 }
1004 
1005 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1006 				       struct bnxt_cp_ring_info *cpr,
1007 				       u16 cp_cons, u16 curr)
1008 {
1009 	struct rx_agg_cmp *agg;
1010 
1011 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1012 	agg = (struct rx_agg_cmp *)
1013 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1014 	return agg;
1015 }
1016 
1017 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1018 					      struct bnxt_rx_ring_info *rxr,
1019 					      u16 agg_id, u16 curr)
1020 {
1021 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1022 
1023 	return &tpa_info->agg_arr[curr];
1024 }
1025 
1026 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1027 				   u16 start, u32 agg_bufs, bool tpa)
1028 {
1029 	struct bnxt_napi *bnapi = cpr->bnapi;
1030 	struct bnxt *bp = bnapi->bp;
1031 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1032 	u16 prod = rxr->rx_agg_prod;
1033 	u16 sw_prod = rxr->rx_sw_agg_prod;
1034 	bool p5_tpa = false;
1035 	u32 i;
1036 
1037 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1038 		p5_tpa = true;
1039 
1040 	for (i = 0; i < agg_bufs; i++) {
1041 		u16 cons;
1042 		struct rx_agg_cmp *agg;
1043 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1044 		struct rx_bd *prod_bd;
1045 		struct page *page;
1046 
1047 		if (p5_tpa)
1048 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1049 		else
1050 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1051 		cons = agg->rx_agg_cmp_opaque;
1052 		__clear_bit(cons, rxr->rx_agg_bmap);
1053 
1054 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1055 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1056 
1057 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1058 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1059 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1060 
1061 		/* It is possible for sw_prod to be equal to cons, so
1062 		 * set cons_rx_buf->page to NULL first.
1063 		 */
1064 		page = cons_rx_buf->page;
1065 		cons_rx_buf->page = NULL;
1066 		prod_rx_buf->page = page;
1067 		prod_rx_buf->offset = cons_rx_buf->offset;
1068 
1069 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1070 
1071 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1072 
1073 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1074 		prod_bd->rx_bd_opaque = sw_prod;
1075 
1076 		prod = NEXT_RX_AGG(prod);
1077 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1078 	}
1079 	rxr->rx_agg_prod = prod;
1080 	rxr->rx_sw_agg_prod = sw_prod;
1081 }
1082 
1083 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1084 					      struct bnxt_rx_ring_info *rxr,
1085 					      u16 cons, void *data, u8 *data_ptr,
1086 					      dma_addr_t dma_addr,
1087 					      unsigned int offset_and_len)
1088 {
1089 	unsigned int len = offset_and_len & 0xffff;
1090 	struct page *page = data;
1091 	u16 prod = rxr->rx_prod;
1092 	struct sk_buff *skb;
1093 	int err;
1094 
1095 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1096 	if (unlikely(err)) {
1097 		bnxt_reuse_rx_data(rxr, cons, data);
1098 		return NULL;
1099 	}
1100 	dma_addr -= bp->rx_dma_offset;
1101 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1102 				bp->rx_dir);
1103 	skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE);
1104 	if (!skb) {
1105 		page_pool_recycle_direct(rxr->page_pool, page);
1106 		return NULL;
1107 	}
1108 	skb_mark_for_recycle(skb);
1109 	skb_reserve(skb, bp->rx_offset);
1110 	__skb_put(skb, len);
1111 
1112 	return skb;
1113 }
1114 
1115 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1116 					struct bnxt_rx_ring_info *rxr,
1117 					u16 cons, void *data, u8 *data_ptr,
1118 					dma_addr_t dma_addr,
1119 					unsigned int offset_and_len)
1120 {
1121 	unsigned int payload = offset_and_len >> 16;
1122 	unsigned int len = offset_and_len & 0xffff;
1123 	skb_frag_t *frag;
1124 	struct page *page = data;
1125 	u16 prod = rxr->rx_prod;
1126 	struct sk_buff *skb;
1127 	int off, err;
1128 
1129 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1130 	if (unlikely(err)) {
1131 		bnxt_reuse_rx_data(rxr, cons, data);
1132 		return NULL;
1133 	}
1134 	dma_addr -= bp->rx_dma_offset;
1135 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1136 				bp->rx_dir);
1137 
1138 	if (unlikely(!payload))
1139 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1140 
1141 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1142 	if (!skb) {
1143 		page_pool_recycle_direct(rxr->page_pool, page);
1144 		return NULL;
1145 	}
1146 
1147 	skb_mark_for_recycle(skb);
1148 	off = (void *)data_ptr - page_address(page);
1149 	skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE);
1150 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1151 	       payload + NET_IP_ALIGN);
1152 
1153 	frag = &skb_shinfo(skb)->frags[0];
1154 	skb_frag_size_sub(frag, payload);
1155 	skb_frag_off_add(frag, payload);
1156 	skb->data_len -= payload;
1157 	skb->tail += payload;
1158 
1159 	return skb;
1160 }
1161 
1162 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1163 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1164 				   void *data, u8 *data_ptr,
1165 				   dma_addr_t dma_addr,
1166 				   unsigned int offset_and_len)
1167 {
1168 	u16 prod = rxr->rx_prod;
1169 	struct sk_buff *skb;
1170 	int err;
1171 
1172 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1173 	if (unlikely(err)) {
1174 		bnxt_reuse_rx_data(rxr, cons, data);
1175 		return NULL;
1176 	}
1177 
1178 	skb = napi_build_skb(data, bp->rx_buf_size);
1179 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1180 				bp->rx_dir);
1181 	if (!skb) {
1182 		page_pool_free_va(rxr->head_pool, data, true);
1183 		return NULL;
1184 	}
1185 
1186 	skb_mark_for_recycle(skb);
1187 	skb_reserve(skb, bp->rx_offset);
1188 	skb_put(skb, offset_and_len & 0xffff);
1189 	return skb;
1190 }
1191 
1192 static u32 __bnxt_rx_agg_pages(struct bnxt *bp,
1193 			       struct bnxt_cp_ring_info *cpr,
1194 			       struct skb_shared_info *shinfo,
1195 			       u16 idx, u32 agg_bufs, bool tpa,
1196 			       struct xdp_buff *xdp)
1197 {
1198 	struct bnxt_napi *bnapi = cpr->bnapi;
1199 	struct pci_dev *pdev = bp->pdev;
1200 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1201 	u16 prod = rxr->rx_agg_prod;
1202 	u32 i, total_frag_len = 0;
1203 	bool p5_tpa = false;
1204 
1205 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1206 		p5_tpa = true;
1207 
1208 	for (i = 0; i < agg_bufs; i++) {
1209 		skb_frag_t *frag = &shinfo->frags[i];
1210 		u16 cons, frag_len;
1211 		struct rx_agg_cmp *agg;
1212 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1213 		struct page *page;
1214 		dma_addr_t mapping;
1215 
1216 		if (p5_tpa)
1217 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1218 		else
1219 			agg = bnxt_get_agg(bp, cpr, idx, i);
1220 		cons = agg->rx_agg_cmp_opaque;
1221 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1222 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1223 
1224 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1225 		skb_frag_fill_page_desc(frag, cons_rx_buf->page,
1226 					cons_rx_buf->offset, frag_len);
1227 		shinfo->nr_frags = i + 1;
1228 		__clear_bit(cons, rxr->rx_agg_bmap);
1229 
1230 		/* It is possible for bnxt_alloc_rx_page() to allocate
1231 		 * a sw_prod index that equals the cons index, so we
1232 		 * need to clear the cons entry now.
1233 		 */
1234 		mapping = cons_rx_buf->mapping;
1235 		page = cons_rx_buf->page;
1236 		cons_rx_buf->page = NULL;
1237 
1238 		if (xdp && page_is_pfmemalloc(page))
1239 			xdp_buff_set_frag_pfmemalloc(xdp);
1240 
1241 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1242 			--shinfo->nr_frags;
1243 			cons_rx_buf->page = page;
1244 
1245 			/* Update prod since possibly some pages have been
1246 			 * allocated already.
1247 			 */
1248 			rxr->rx_agg_prod = prod;
1249 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1250 			return 0;
1251 		}
1252 
1253 		dma_sync_single_for_cpu(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1254 					bp->rx_dir);
1255 
1256 		total_frag_len += frag_len;
1257 		prod = NEXT_RX_AGG(prod);
1258 	}
1259 	rxr->rx_agg_prod = prod;
1260 	return total_frag_len;
1261 }
1262 
1263 static struct sk_buff *bnxt_rx_agg_pages_skb(struct bnxt *bp,
1264 					     struct bnxt_cp_ring_info *cpr,
1265 					     struct sk_buff *skb, u16 idx,
1266 					     u32 agg_bufs, bool tpa)
1267 {
1268 	struct skb_shared_info *shinfo = skb_shinfo(skb);
1269 	u32 total_frag_len = 0;
1270 
1271 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo, idx,
1272 					     agg_bufs, tpa, NULL);
1273 	if (!total_frag_len) {
1274 		skb_mark_for_recycle(skb);
1275 		dev_kfree_skb(skb);
1276 		return NULL;
1277 	}
1278 
1279 	skb->data_len += total_frag_len;
1280 	skb->len += total_frag_len;
1281 	skb->truesize += BNXT_RX_PAGE_SIZE * agg_bufs;
1282 	return skb;
1283 }
1284 
1285 static u32 bnxt_rx_agg_pages_xdp(struct bnxt *bp,
1286 				 struct bnxt_cp_ring_info *cpr,
1287 				 struct xdp_buff *xdp, u16 idx,
1288 				 u32 agg_bufs, bool tpa)
1289 {
1290 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1291 	u32 total_frag_len = 0;
1292 
1293 	if (!xdp_buff_has_frags(xdp))
1294 		shinfo->nr_frags = 0;
1295 
1296 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo,
1297 					     idx, agg_bufs, tpa, xdp);
1298 	if (total_frag_len) {
1299 		xdp_buff_set_frags_flag(xdp);
1300 		shinfo->nr_frags = agg_bufs;
1301 		shinfo->xdp_frags_size = total_frag_len;
1302 	}
1303 	return total_frag_len;
1304 }
1305 
1306 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1307 			       u8 agg_bufs, u32 *raw_cons)
1308 {
1309 	u16 last;
1310 	struct rx_agg_cmp *agg;
1311 
1312 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1313 	last = RING_CMP(*raw_cons);
1314 	agg = (struct rx_agg_cmp *)
1315 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1316 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1317 }
1318 
1319 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1320 				      unsigned int len,
1321 				      dma_addr_t mapping)
1322 {
1323 	struct bnxt *bp = bnapi->bp;
1324 	struct pci_dev *pdev = bp->pdev;
1325 	struct sk_buff *skb;
1326 
1327 	skb = napi_alloc_skb(&bnapi->napi, len);
1328 	if (!skb)
1329 		return NULL;
1330 
1331 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1332 				bp->rx_dir);
1333 
1334 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1335 	       len + NET_IP_ALIGN);
1336 
1337 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1338 				   bp->rx_dir);
1339 
1340 	skb_put(skb, len);
1341 
1342 	return skb;
1343 }
1344 
1345 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1346 				     unsigned int len,
1347 				     dma_addr_t mapping)
1348 {
1349 	return bnxt_copy_data(bnapi, data, len, mapping);
1350 }
1351 
1352 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1353 				     struct xdp_buff *xdp,
1354 				     unsigned int len,
1355 				     dma_addr_t mapping)
1356 {
1357 	unsigned int metasize = 0;
1358 	u8 *data = xdp->data;
1359 	struct sk_buff *skb;
1360 
1361 	len = xdp->data_end - xdp->data_meta;
1362 	metasize = xdp->data - xdp->data_meta;
1363 	data = xdp->data_meta;
1364 
1365 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1366 	if (!skb)
1367 		return skb;
1368 
1369 	if (metasize) {
1370 		skb_metadata_set(skb, metasize);
1371 		__skb_pull(skb, metasize);
1372 	}
1373 
1374 	return skb;
1375 }
1376 
1377 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1378 			   u32 *raw_cons, void *cmp)
1379 {
1380 	struct rx_cmp *rxcmp = cmp;
1381 	u32 tmp_raw_cons = *raw_cons;
1382 	u8 cmp_type, agg_bufs = 0;
1383 
1384 	cmp_type = RX_CMP_TYPE(rxcmp);
1385 
1386 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1387 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1388 			    RX_CMP_AGG_BUFS) >>
1389 			   RX_CMP_AGG_BUFS_SHIFT;
1390 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1391 		struct rx_tpa_end_cmp *tpa_end = cmp;
1392 
1393 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1394 			return 0;
1395 
1396 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1397 	}
1398 
1399 	if (agg_bufs) {
1400 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1401 			return -EBUSY;
1402 	}
1403 	*raw_cons = tmp_raw_cons;
1404 	return 0;
1405 }
1406 
1407 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1408 {
1409 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1410 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1411 
1412 	if (test_bit(idx, map->agg_idx_bmap))
1413 		idx = find_first_zero_bit(map->agg_idx_bmap,
1414 					  BNXT_AGG_IDX_BMAP_SIZE);
1415 	__set_bit(idx, map->agg_idx_bmap);
1416 	map->agg_id_tbl[agg_id] = idx;
1417 	return idx;
1418 }
1419 
1420 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1421 {
1422 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1423 
1424 	__clear_bit(idx, map->agg_idx_bmap);
1425 }
1426 
1427 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1428 {
1429 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1430 
1431 	return map->agg_id_tbl[agg_id];
1432 }
1433 
1434 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1435 			      struct rx_tpa_start_cmp *tpa_start,
1436 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1437 {
1438 	tpa_info->cfa_code_valid = 1;
1439 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1440 	tpa_info->vlan_valid = 0;
1441 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1442 		tpa_info->vlan_valid = 1;
1443 		tpa_info->metadata =
1444 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1445 	}
1446 }
1447 
1448 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1449 				 struct rx_tpa_start_cmp *tpa_start,
1450 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1451 {
1452 	tpa_info->vlan_valid = 0;
1453 	if (TPA_START_VLAN_VALID(tpa_start)) {
1454 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1455 		u32 vlan_proto = ETH_P_8021Q;
1456 
1457 		tpa_info->vlan_valid = 1;
1458 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1459 			vlan_proto = ETH_P_8021AD;
1460 		tpa_info->metadata = vlan_proto << 16 |
1461 				     TPA_START_METADATA0_TCI(tpa_start1);
1462 	}
1463 }
1464 
1465 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1466 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1467 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1468 {
1469 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1470 	struct bnxt_tpa_info *tpa_info;
1471 	u16 cons, prod, agg_id;
1472 	struct rx_bd *prod_bd;
1473 	dma_addr_t mapping;
1474 
1475 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1476 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1477 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1478 	} else {
1479 		agg_id = TPA_START_AGG_ID(tpa_start);
1480 	}
1481 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1482 	prod = rxr->rx_prod;
1483 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1484 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1485 	tpa_info = &rxr->rx_tpa[agg_id];
1486 
1487 	if (unlikely(cons != rxr->rx_next_cons ||
1488 		     TPA_START_ERROR(tpa_start))) {
1489 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1490 			    cons, rxr->rx_next_cons,
1491 			    TPA_START_ERROR_CODE(tpa_start1));
1492 		bnxt_sched_reset_rxr(bp, rxr);
1493 		return;
1494 	}
1495 	prod_rx_buf->data = tpa_info->data;
1496 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1497 
1498 	mapping = tpa_info->mapping;
1499 	prod_rx_buf->mapping = mapping;
1500 
1501 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1502 
1503 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1504 
1505 	tpa_info->data = cons_rx_buf->data;
1506 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1507 	cons_rx_buf->data = NULL;
1508 	tpa_info->mapping = cons_rx_buf->mapping;
1509 
1510 	tpa_info->len =
1511 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1512 				RX_TPA_START_CMP_LEN_SHIFT;
1513 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1514 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1515 		tpa_info->gso_type = SKB_GSO_TCPV4;
1516 		if (TPA_START_IS_IPV6(tpa_start1))
1517 			tpa_info->gso_type = SKB_GSO_TCPV6;
1518 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1519 		else if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP &&
1520 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1521 			tpa_info->gso_type = SKB_GSO_TCPV6;
1522 		tpa_info->rss_hash =
1523 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1524 	} else {
1525 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1526 		tpa_info->gso_type = 0;
1527 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1528 	}
1529 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1530 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1531 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1532 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1533 	else
1534 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1535 	tpa_info->agg_count = 0;
1536 
1537 	rxr->rx_prod = NEXT_RX(prod);
1538 	cons = RING_RX(bp, NEXT_RX(cons));
1539 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1540 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1541 
1542 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1543 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1544 	cons_rx_buf->data = NULL;
1545 }
1546 
1547 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1548 {
1549 	if (agg_bufs)
1550 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1551 }
1552 
1553 #ifdef CONFIG_INET
1554 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1555 {
1556 	struct udphdr *uh = NULL;
1557 
1558 	if (ip_proto == htons(ETH_P_IP)) {
1559 		struct iphdr *iph = (struct iphdr *)skb->data;
1560 
1561 		if (iph->protocol == IPPROTO_UDP)
1562 			uh = (struct udphdr *)(iph + 1);
1563 	} else {
1564 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1565 
1566 		if (iph->nexthdr == IPPROTO_UDP)
1567 			uh = (struct udphdr *)(iph + 1);
1568 	}
1569 	if (uh) {
1570 		if (uh->check)
1571 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1572 		else
1573 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1574 	}
1575 }
1576 #endif
1577 
1578 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1579 					   int payload_off, int tcp_ts,
1580 					   struct sk_buff *skb)
1581 {
1582 #ifdef CONFIG_INET
1583 	struct tcphdr *th;
1584 	int len, nw_off;
1585 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1586 	u32 hdr_info = tpa_info->hdr_info;
1587 	bool loopback = false;
1588 
1589 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1590 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1591 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1592 
1593 	/* If the packet is an internal loopback packet, the offsets will
1594 	 * have an extra 4 bytes.
1595 	 */
1596 	if (inner_mac_off == 4) {
1597 		loopback = true;
1598 	} else if (inner_mac_off > 4) {
1599 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1600 					    ETH_HLEN - 2));
1601 
1602 		/* We only support inner iPv4/ipv6.  If we don't see the
1603 		 * correct protocol ID, it must be a loopback packet where
1604 		 * the offsets are off by 4.
1605 		 */
1606 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1607 			loopback = true;
1608 	}
1609 	if (loopback) {
1610 		/* internal loopback packet, subtract all offsets by 4 */
1611 		inner_ip_off -= 4;
1612 		inner_mac_off -= 4;
1613 		outer_ip_off -= 4;
1614 	}
1615 
1616 	nw_off = inner_ip_off - ETH_HLEN;
1617 	skb_set_network_header(skb, nw_off);
1618 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1619 		struct ipv6hdr *iph = ipv6_hdr(skb);
1620 
1621 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1622 		len = skb->len - skb_transport_offset(skb);
1623 		th = tcp_hdr(skb);
1624 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1625 	} else {
1626 		struct iphdr *iph = ip_hdr(skb);
1627 
1628 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1629 		len = skb->len - skb_transport_offset(skb);
1630 		th = tcp_hdr(skb);
1631 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1632 	}
1633 
1634 	if (inner_mac_off) { /* tunnel */
1635 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1636 					    ETH_HLEN - 2));
1637 
1638 		bnxt_gro_tunnel(skb, proto);
1639 	}
1640 #endif
1641 	return skb;
1642 }
1643 
1644 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1645 					   int payload_off, int tcp_ts,
1646 					   struct sk_buff *skb)
1647 {
1648 #ifdef CONFIG_INET
1649 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1650 	u32 hdr_info = tpa_info->hdr_info;
1651 	int iphdr_len, nw_off;
1652 
1653 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1654 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1655 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1656 
1657 	nw_off = inner_ip_off - ETH_HLEN;
1658 	skb_set_network_header(skb, nw_off);
1659 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1660 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1661 	skb_set_transport_header(skb, nw_off + iphdr_len);
1662 
1663 	if (inner_mac_off) { /* tunnel */
1664 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1665 					    ETH_HLEN - 2));
1666 
1667 		bnxt_gro_tunnel(skb, proto);
1668 	}
1669 #endif
1670 	return skb;
1671 }
1672 
1673 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1674 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1675 
1676 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1677 					   int payload_off, int tcp_ts,
1678 					   struct sk_buff *skb)
1679 {
1680 #ifdef CONFIG_INET
1681 	struct tcphdr *th;
1682 	int len, nw_off, tcp_opt_len = 0;
1683 
1684 	if (tcp_ts)
1685 		tcp_opt_len = 12;
1686 
1687 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1688 		struct iphdr *iph;
1689 
1690 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1691 			 ETH_HLEN;
1692 		skb_set_network_header(skb, nw_off);
1693 		iph = ip_hdr(skb);
1694 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1695 		len = skb->len - skb_transport_offset(skb);
1696 		th = tcp_hdr(skb);
1697 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1698 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1699 		struct ipv6hdr *iph;
1700 
1701 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1702 			 ETH_HLEN;
1703 		skb_set_network_header(skb, nw_off);
1704 		iph = ipv6_hdr(skb);
1705 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1706 		len = skb->len - skb_transport_offset(skb);
1707 		th = tcp_hdr(skb);
1708 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1709 	} else {
1710 		dev_kfree_skb_any(skb);
1711 		return NULL;
1712 	}
1713 
1714 	if (nw_off) /* tunnel */
1715 		bnxt_gro_tunnel(skb, skb->protocol);
1716 #endif
1717 	return skb;
1718 }
1719 
1720 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1721 					   struct bnxt_tpa_info *tpa_info,
1722 					   struct rx_tpa_end_cmp *tpa_end,
1723 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1724 					   struct sk_buff *skb)
1725 {
1726 #ifdef CONFIG_INET
1727 	int payload_off;
1728 	u16 segs;
1729 
1730 	segs = TPA_END_TPA_SEGS(tpa_end);
1731 	if (segs == 1)
1732 		return skb;
1733 
1734 	NAPI_GRO_CB(skb)->count = segs;
1735 	skb_shinfo(skb)->gso_size =
1736 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1737 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1738 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1739 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1740 	else
1741 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1742 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1743 	if (likely(skb))
1744 		tcp_gro_complete(skb);
1745 #endif
1746 	return skb;
1747 }
1748 
1749 /* Given the cfa_code of a received packet determine which
1750  * netdev (vf-rep or PF) the packet is destined to.
1751  */
1752 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1753 {
1754 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1755 
1756 	/* if vf-rep dev is NULL, the must belongs to the PF */
1757 	return dev ? dev : bp->dev;
1758 }
1759 
1760 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1761 					   struct bnxt_cp_ring_info *cpr,
1762 					   u32 *raw_cons,
1763 					   struct rx_tpa_end_cmp *tpa_end,
1764 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1765 					   u8 *event)
1766 {
1767 	struct bnxt_napi *bnapi = cpr->bnapi;
1768 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1769 	struct net_device *dev = bp->dev;
1770 	u8 *data_ptr, agg_bufs;
1771 	unsigned int len;
1772 	struct bnxt_tpa_info *tpa_info;
1773 	dma_addr_t mapping;
1774 	struct sk_buff *skb;
1775 	u16 idx = 0, agg_id;
1776 	void *data;
1777 	bool gro;
1778 
1779 	if (unlikely(bnapi->in_reset)) {
1780 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1781 
1782 		if (rc < 0)
1783 			return ERR_PTR(-EBUSY);
1784 		return NULL;
1785 	}
1786 
1787 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1788 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1789 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1790 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1791 		tpa_info = &rxr->rx_tpa[agg_id];
1792 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1793 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1794 				    agg_bufs, tpa_info->agg_count);
1795 			agg_bufs = tpa_info->agg_count;
1796 		}
1797 		tpa_info->agg_count = 0;
1798 		*event |= BNXT_AGG_EVENT;
1799 		bnxt_free_agg_idx(rxr, agg_id);
1800 		idx = agg_id;
1801 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1802 	} else {
1803 		agg_id = TPA_END_AGG_ID(tpa_end);
1804 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1805 		tpa_info = &rxr->rx_tpa[agg_id];
1806 		idx = RING_CMP(*raw_cons);
1807 		if (agg_bufs) {
1808 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1809 				return ERR_PTR(-EBUSY);
1810 
1811 			*event |= BNXT_AGG_EVENT;
1812 			idx = NEXT_CMP(idx);
1813 		}
1814 		gro = !!TPA_END_GRO(tpa_end);
1815 	}
1816 	data = tpa_info->data;
1817 	data_ptr = tpa_info->data_ptr;
1818 	prefetch(data_ptr);
1819 	len = tpa_info->len;
1820 	mapping = tpa_info->mapping;
1821 
1822 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1823 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1824 		if (agg_bufs > MAX_SKB_FRAGS)
1825 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1826 				    agg_bufs, (int)MAX_SKB_FRAGS);
1827 		return NULL;
1828 	}
1829 
1830 	if (len <= bp->rx_copy_thresh) {
1831 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1832 		if (!skb) {
1833 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1834 			cpr->sw_stats->rx.rx_oom_discards += 1;
1835 			return NULL;
1836 		}
1837 	} else {
1838 		u8 *new_data;
1839 		dma_addr_t new_mapping;
1840 
1841 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr,
1842 						GFP_ATOMIC);
1843 		if (!new_data) {
1844 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1845 			cpr->sw_stats->rx.rx_oom_discards += 1;
1846 			return NULL;
1847 		}
1848 
1849 		tpa_info->data = new_data;
1850 		tpa_info->data_ptr = new_data + bp->rx_offset;
1851 		tpa_info->mapping = new_mapping;
1852 
1853 		skb = napi_build_skb(data, bp->rx_buf_size);
1854 		dma_sync_single_for_cpu(&bp->pdev->dev, mapping,
1855 					bp->rx_buf_use_size, bp->rx_dir);
1856 
1857 		if (!skb) {
1858 			page_pool_free_va(rxr->head_pool, data, true);
1859 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1860 			cpr->sw_stats->rx.rx_oom_discards += 1;
1861 			return NULL;
1862 		}
1863 		skb_mark_for_recycle(skb);
1864 		skb_reserve(skb, bp->rx_offset);
1865 		skb_put(skb, len);
1866 	}
1867 
1868 	if (agg_bufs) {
1869 		skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, idx, agg_bufs, true);
1870 		if (!skb) {
1871 			/* Page reuse already handled by bnxt_rx_pages(). */
1872 			cpr->sw_stats->rx.rx_oom_discards += 1;
1873 			return NULL;
1874 		}
1875 	}
1876 
1877 	if (tpa_info->cfa_code_valid)
1878 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1879 	skb->protocol = eth_type_trans(skb, dev);
1880 
1881 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1882 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1883 
1884 	if (tpa_info->vlan_valid &&
1885 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1886 		__be16 vlan_proto = htons(tpa_info->metadata >>
1887 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1888 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1889 
1890 		if (eth_type_vlan(vlan_proto)) {
1891 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1892 		} else {
1893 			dev_kfree_skb(skb);
1894 			return NULL;
1895 		}
1896 	}
1897 
1898 	skb_checksum_none_assert(skb);
1899 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1900 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1901 		skb->csum_level =
1902 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1903 	}
1904 
1905 	if (gro)
1906 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1907 
1908 	return skb;
1909 }
1910 
1911 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1912 			 struct rx_agg_cmp *rx_agg)
1913 {
1914 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1915 	struct bnxt_tpa_info *tpa_info;
1916 
1917 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1918 	tpa_info = &rxr->rx_tpa[agg_id];
1919 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1920 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1921 }
1922 
1923 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1924 			     struct sk_buff *skb)
1925 {
1926 	skb_mark_for_recycle(skb);
1927 
1928 	if (skb->dev != bp->dev) {
1929 		/* this packet belongs to a vf-rep */
1930 		bnxt_vf_rep_rx(bp, skb);
1931 		return;
1932 	}
1933 	skb_record_rx_queue(skb, bnapi->index);
1934 	napi_gro_receive(&bnapi->napi, skb);
1935 }
1936 
1937 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
1938 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
1939 {
1940 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
1941 
1942 	if (BNXT_PTP_RX_TS_VALID(flags))
1943 		goto ts_valid;
1944 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
1945 		return false;
1946 
1947 ts_valid:
1948 	*cmpl_ts = ts;
1949 	return true;
1950 }
1951 
1952 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
1953 				    struct rx_cmp *rxcmp,
1954 				    struct rx_cmp_ext *rxcmp1)
1955 {
1956 	__be16 vlan_proto;
1957 	u16 vtag;
1958 
1959 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1960 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
1961 		u32 meta_data;
1962 
1963 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
1964 			return skb;
1965 
1966 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1967 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1968 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
1969 		if (eth_type_vlan(vlan_proto))
1970 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1971 		else
1972 			goto vlan_err;
1973 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
1974 		if (RX_CMP_VLAN_VALID(rxcmp)) {
1975 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
1976 
1977 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
1978 				vlan_proto = htons(ETH_P_8021Q);
1979 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
1980 				vlan_proto = htons(ETH_P_8021AD);
1981 			else
1982 				goto vlan_err;
1983 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
1984 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1985 		}
1986 	}
1987 	return skb;
1988 vlan_err:
1989 	dev_kfree_skb(skb);
1990 	return NULL;
1991 }
1992 
1993 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp,
1994 					   struct rx_cmp *rxcmp)
1995 {
1996 	u8 ext_op;
1997 
1998 	ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp);
1999 	switch (ext_op) {
2000 	case EXT_OP_INNER_4:
2001 	case EXT_OP_OUTER_4:
2002 	case EXT_OP_INNFL_3:
2003 	case EXT_OP_OUTFL_3:
2004 		return PKT_HASH_TYPE_L4;
2005 	default:
2006 		return PKT_HASH_TYPE_L3;
2007 	}
2008 }
2009 
2010 /* returns the following:
2011  * 1       - 1 packet successfully received
2012  * 0       - successful TPA_START, packet not completed yet
2013  * -EBUSY  - completion ring does not have all the agg buffers yet
2014  * -ENOMEM - packet aborted due to out of memory
2015  * -EIO    - packet aborted due to hw error indicated in BD
2016  */
2017 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2018 		       u32 *raw_cons, u8 *event)
2019 {
2020 	struct bnxt_napi *bnapi = cpr->bnapi;
2021 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2022 	struct net_device *dev = bp->dev;
2023 	struct rx_cmp *rxcmp;
2024 	struct rx_cmp_ext *rxcmp1;
2025 	u32 tmp_raw_cons = *raw_cons;
2026 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2027 	struct bnxt_sw_rx_bd *rx_buf;
2028 	unsigned int len;
2029 	u8 *data_ptr, agg_bufs, cmp_type;
2030 	bool xdp_active = false;
2031 	dma_addr_t dma_addr;
2032 	struct sk_buff *skb;
2033 	struct xdp_buff xdp;
2034 	u32 flags, misc;
2035 	u32 cmpl_ts;
2036 	void *data;
2037 	int rc = 0;
2038 
2039 	rxcmp = (struct rx_cmp *)
2040 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2041 
2042 	cmp_type = RX_CMP_TYPE(rxcmp);
2043 
2044 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2045 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2046 		goto next_rx_no_prod_no_len;
2047 	}
2048 
2049 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2050 	cp_cons = RING_CMP(tmp_raw_cons);
2051 	rxcmp1 = (struct rx_cmp_ext *)
2052 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2053 
2054 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2055 		return -EBUSY;
2056 
2057 	/* The valid test of the entry must be done first before
2058 	 * reading any further.
2059 	 */
2060 	dma_rmb();
2061 	prod = rxr->rx_prod;
2062 
2063 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2064 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2065 		bnxt_tpa_start(bp, rxr, cmp_type,
2066 			       (struct rx_tpa_start_cmp *)rxcmp,
2067 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2068 
2069 		*event |= BNXT_RX_EVENT;
2070 		goto next_rx_no_prod_no_len;
2071 
2072 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2073 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2074 				   (struct rx_tpa_end_cmp *)rxcmp,
2075 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2076 
2077 		if (IS_ERR(skb))
2078 			return -EBUSY;
2079 
2080 		rc = -ENOMEM;
2081 		if (likely(skb)) {
2082 			bnxt_deliver_skb(bp, bnapi, skb);
2083 			rc = 1;
2084 		}
2085 		*event |= BNXT_RX_EVENT;
2086 		goto next_rx_no_prod_no_len;
2087 	}
2088 
2089 	cons = rxcmp->rx_cmp_opaque;
2090 	if (unlikely(cons != rxr->rx_next_cons)) {
2091 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2092 
2093 		/* 0xffff is forced error, don't print it */
2094 		if (rxr->rx_next_cons != 0xffff)
2095 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2096 				    cons, rxr->rx_next_cons);
2097 		bnxt_sched_reset_rxr(bp, rxr);
2098 		if (rc1)
2099 			return rc1;
2100 		goto next_rx_no_prod_no_len;
2101 	}
2102 	rx_buf = &rxr->rx_buf_ring[cons];
2103 	data = rx_buf->data;
2104 	data_ptr = rx_buf->data_ptr;
2105 	prefetch(data_ptr);
2106 
2107 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2108 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2109 
2110 	if (agg_bufs) {
2111 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2112 			return -EBUSY;
2113 
2114 		cp_cons = NEXT_CMP(cp_cons);
2115 		*event |= BNXT_AGG_EVENT;
2116 	}
2117 	*event |= BNXT_RX_EVENT;
2118 
2119 	rx_buf->data = NULL;
2120 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2121 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2122 
2123 		bnxt_reuse_rx_data(rxr, cons, data);
2124 		if (agg_bufs)
2125 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2126 					       false);
2127 
2128 		rc = -EIO;
2129 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2130 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2131 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2132 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2133 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2134 						 rx_err);
2135 				bnxt_sched_reset_rxr(bp, rxr);
2136 			}
2137 		}
2138 		goto next_rx_no_len;
2139 	}
2140 
2141 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2142 	len = flags >> RX_CMP_LEN_SHIFT;
2143 	dma_addr = rx_buf->mapping;
2144 
2145 	if (bnxt_xdp_attached(bp, rxr)) {
2146 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2147 		if (agg_bufs) {
2148 			u32 frag_len = bnxt_rx_agg_pages_xdp(bp, cpr, &xdp,
2149 							     cp_cons, agg_bufs,
2150 							     false);
2151 			if (!frag_len)
2152 				goto oom_next_rx;
2153 		}
2154 		xdp_active = true;
2155 	}
2156 
2157 	if (xdp_active) {
2158 		if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) {
2159 			rc = 1;
2160 			goto next_rx;
2161 		}
2162 	}
2163 
2164 	if (len <= bp->rx_copy_thresh) {
2165 		if (!xdp_active)
2166 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2167 		else
2168 			skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr);
2169 		bnxt_reuse_rx_data(rxr, cons, data);
2170 		if (!skb) {
2171 			if (agg_bufs) {
2172 				if (!xdp_active)
2173 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2174 							       agg_bufs, false);
2175 				else
2176 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2177 			}
2178 			goto oom_next_rx;
2179 		}
2180 	} else {
2181 		u32 payload;
2182 
2183 		if (rx_buf->data_ptr == data_ptr)
2184 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2185 		else
2186 			payload = 0;
2187 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2188 				      payload | len);
2189 		if (!skb)
2190 			goto oom_next_rx;
2191 	}
2192 
2193 	if (agg_bufs) {
2194 		if (!xdp_active) {
2195 			skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, cp_cons, agg_bufs, false);
2196 			if (!skb)
2197 				goto oom_next_rx;
2198 		} else {
2199 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, rxr->page_pool, &xdp, rxcmp1);
2200 			if (!skb) {
2201 				/* we should be able to free the old skb here */
2202 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2203 				goto oom_next_rx;
2204 			}
2205 		}
2206 	}
2207 
2208 	if (RX_CMP_HASH_VALID(rxcmp)) {
2209 		enum pkt_hash_types type;
2210 
2211 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2212 			type = bnxt_rss_ext_op(bp, rxcmp);
2213 		} else {
2214 			u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
2215 
2216 			/* RSS profiles 1 and 3 with extract code 0 for inner
2217 			 * 4-tuple
2218 			 */
2219 			if (hash_type != 1 && hash_type != 3)
2220 				type = PKT_HASH_TYPE_L3;
2221 			else
2222 				type = PKT_HASH_TYPE_L4;
2223 		}
2224 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2225 	}
2226 
2227 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2228 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2229 	skb->protocol = eth_type_trans(skb, dev);
2230 
2231 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2232 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2233 		if (!skb)
2234 			goto next_rx;
2235 	}
2236 
2237 	skb_checksum_none_assert(skb);
2238 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2239 		if (dev->features & NETIF_F_RXCSUM) {
2240 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2241 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2242 		}
2243 	} else {
2244 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2245 			if (dev->features & NETIF_F_RXCSUM)
2246 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2247 		}
2248 	}
2249 
2250 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2251 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2252 			u64 ns, ts;
2253 
2254 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2255 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2256 
2257 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2258 				memset(skb_hwtstamps(skb), 0,
2259 				       sizeof(*skb_hwtstamps(skb)));
2260 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2261 			}
2262 		}
2263 	}
2264 	bnxt_deliver_skb(bp, bnapi, skb);
2265 	rc = 1;
2266 
2267 next_rx:
2268 	cpr->rx_packets += 1;
2269 	cpr->rx_bytes += len;
2270 
2271 next_rx_no_len:
2272 	rxr->rx_prod = NEXT_RX(prod);
2273 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2274 
2275 next_rx_no_prod_no_len:
2276 	*raw_cons = tmp_raw_cons;
2277 
2278 	return rc;
2279 
2280 oom_next_rx:
2281 	cpr->sw_stats->rx.rx_oom_discards += 1;
2282 	rc = -ENOMEM;
2283 	goto next_rx;
2284 }
2285 
2286 /* In netpoll mode, if we are using a combined completion ring, we need to
2287  * discard the rx packets and recycle the buffers.
2288  */
2289 static int bnxt_force_rx_discard(struct bnxt *bp,
2290 				 struct bnxt_cp_ring_info *cpr,
2291 				 u32 *raw_cons, u8 *event)
2292 {
2293 	u32 tmp_raw_cons = *raw_cons;
2294 	struct rx_cmp_ext *rxcmp1;
2295 	struct rx_cmp *rxcmp;
2296 	u16 cp_cons;
2297 	u8 cmp_type;
2298 	int rc;
2299 
2300 	cp_cons = RING_CMP(tmp_raw_cons);
2301 	rxcmp = (struct rx_cmp *)
2302 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2303 
2304 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2305 	cp_cons = RING_CMP(tmp_raw_cons);
2306 	rxcmp1 = (struct rx_cmp_ext *)
2307 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2308 
2309 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2310 		return -EBUSY;
2311 
2312 	/* The valid test of the entry must be done first before
2313 	 * reading any further.
2314 	 */
2315 	dma_rmb();
2316 	cmp_type = RX_CMP_TYPE(rxcmp);
2317 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2318 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2319 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2320 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2321 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2322 		struct rx_tpa_end_cmp_ext *tpa_end1;
2323 
2324 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2325 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2326 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2327 	}
2328 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2329 	if (rc && rc != -EBUSY)
2330 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2331 	return rc;
2332 }
2333 
2334 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2335 {
2336 	struct bnxt_fw_health *fw_health = bp->fw_health;
2337 	u32 reg = fw_health->regs[reg_idx];
2338 	u32 reg_type, reg_off, val = 0;
2339 
2340 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2341 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2342 	switch (reg_type) {
2343 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2344 		pci_read_config_dword(bp->pdev, reg_off, &val);
2345 		break;
2346 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2347 		reg_off = fw_health->mapped_regs[reg_idx];
2348 		fallthrough;
2349 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2350 		val = readl(bp->bar0 + reg_off);
2351 		break;
2352 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2353 		val = readl(bp->bar1 + reg_off);
2354 		break;
2355 	}
2356 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2357 		val &= fw_health->fw_reset_inprog_reg_mask;
2358 	return val;
2359 }
2360 
2361 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2362 {
2363 	int i;
2364 
2365 	for (i = 0; i < bp->rx_nr_rings; i++) {
2366 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2367 		struct bnxt_ring_grp_info *grp_info;
2368 
2369 		grp_info = &bp->grp_info[grp_idx];
2370 		if (grp_info->agg_fw_ring_id == ring_id)
2371 			return grp_idx;
2372 	}
2373 	return INVALID_HW_RING_ID;
2374 }
2375 
2376 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2377 {
2378 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2379 
2380 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2381 		return link_info->force_link_speed2;
2382 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2383 		return link_info->force_pam4_link_speed;
2384 	return link_info->force_link_speed;
2385 }
2386 
2387 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2388 {
2389 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2390 
2391 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2392 		link_info->req_link_speed = link_info->force_link_speed2;
2393 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2394 		switch (link_info->req_link_speed) {
2395 		case BNXT_LINK_SPEED_50GB_PAM4:
2396 		case BNXT_LINK_SPEED_100GB_PAM4:
2397 		case BNXT_LINK_SPEED_200GB_PAM4:
2398 		case BNXT_LINK_SPEED_400GB_PAM4:
2399 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2400 			break;
2401 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2402 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2403 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2404 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2405 			break;
2406 		default:
2407 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2408 		}
2409 		return;
2410 	}
2411 	link_info->req_link_speed = link_info->force_link_speed;
2412 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2413 	if (link_info->force_pam4_link_speed) {
2414 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2415 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2416 	}
2417 }
2418 
2419 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2420 {
2421 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2422 
2423 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2424 		link_info->advertising = link_info->auto_link_speeds2;
2425 		return;
2426 	}
2427 	link_info->advertising = link_info->auto_link_speeds;
2428 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2429 }
2430 
2431 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2432 {
2433 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2434 
2435 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2436 		if (link_info->req_link_speed != link_info->force_link_speed2)
2437 			return true;
2438 		return false;
2439 	}
2440 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2441 	    link_info->req_link_speed != link_info->force_link_speed)
2442 		return true;
2443 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2444 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2445 		return true;
2446 	return false;
2447 }
2448 
2449 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2450 {
2451 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2452 
2453 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2454 		if (link_info->advertising != link_info->auto_link_speeds2)
2455 			return true;
2456 		return false;
2457 	}
2458 	if (link_info->advertising != link_info->auto_link_speeds ||
2459 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2460 		return true;
2461 	return false;
2462 }
2463 
2464 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2465 	((data2) &							\
2466 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2467 
2468 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2469 	(((data2) &							\
2470 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2471 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2472 
2473 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2474 	((data1) &							\
2475 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2476 
2477 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2478 	(((data1) &							\
2479 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2480 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2481 
2482 /* Return true if the workqueue has to be scheduled */
2483 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2484 {
2485 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2486 
2487 	switch (err_type) {
2488 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2489 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2490 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2491 		break;
2492 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2493 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2494 		break;
2495 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2496 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2497 		break;
2498 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2499 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2500 		char *threshold_type;
2501 		bool notify = false;
2502 		char *dir_str;
2503 
2504 		switch (type) {
2505 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2506 			threshold_type = "warning";
2507 			break;
2508 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2509 			threshold_type = "critical";
2510 			break;
2511 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2512 			threshold_type = "fatal";
2513 			break;
2514 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2515 			threshold_type = "shutdown";
2516 			break;
2517 		default:
2518 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2519 			return false;
2520 		}
2521 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2522 			dir_str = "above";
2523 			notify = true;
2524 		} else {
2525 			dir_str = "below";
2526 		}
2527 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2528 			    dir_str, threshold_type);
2529 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2530 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2531 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2532 		if (notify) {
2533 			bp->thermal_threshold_type = type;
2534 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2535 			return true;
2536 		}
2537 		return false;
2538 	}
2539 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2540 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2541 		break;
2542 	default:
2543 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2544 			   err_type);
2545 		break;
2546 	}
2547 	return false;
2548 }
2549 
2550 #define BNXT_GET_EVENT_PORT(data)	\
2551 	((data) &			\
2552 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2553 
2554 #define BNXT_EVENT_RING_TYPE(data2)	\
2555 	((data2) &			\
2556 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2557 
2558 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2559 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2560 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2561 
2562 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2563 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2564 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2565 
2566 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2567 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2568 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2569 
2570 #define BNXT_PHC_BITS	48
2571 
2572 static int bnxt_async_event_process(struct bnxt *bp,
2573 				    struct hwrm_async_event_cmpl *cmpl)
2574 {
2575 	u16 event_id = le16_to_cpu(cmpl->event_id);
2576 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2577 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2578 
2579 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2580 		   event_id, data1, data2);
2581 
2582 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2583 	switch (event_id) {
2584 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2585 		struct bnxt_link_info *link_info = &bp->link_info;
2586 
2587 		if (BNXT_VF(bp))
2588 			goto async_event_process_exit;
2589 
2590 		/* print unsupported speed warning in forced speed mode only */
2591 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2592 		    (data1 & 0x20000)) {
2593 			u16 fw_speed = bnxt_get_force_speed(link_info);
2594 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2595 
2596 			if (speed != SPEED_UNKNOWN)
2597 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2598 					    speed);
2599 		}
2600 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2601 	}
2602 		fallthrough;
2603 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2604 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2605 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2606 		fallthrough;
2607 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2608 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2609 		break;
2610 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2611 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2612 		break;
2613 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2614 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2615 
2616 		if (BNXT_VF(bp))
2617 			break;
2618 
2619 		if (bp->pf.port_id != port_id)
2620 			break;
2621 
2622 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2623 		break;
2624 	}
2625 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2626 		if (BNXT_PF(bp))
2627 			goto async_event_process_exit;
2628 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2629 		break;
2630 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2631 		char *type_str = "Solicited";
2632 
2633 		if (!bp->fw_health)
2634 			goto async_event_process_exit;
2635 
2636 		bp->fw_reset_timestamp = jiffies;
2637 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2638 		if (!bp->fw_reset_min_dsecs)
2639 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2640 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2641 		if (!bp->fw_reset_max_dsecs)
2642 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2643 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2644 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2645 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2646 			type_str = "Fatal";
2647 			bp->fw_health->fatalities++;
2648 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2649 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2650 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2651 			type_str = "Non-fatal";
2652 			bp->fw_health->survivals++;
2653 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2654 		}
2655 		netif_warn(bp, hw, bp->dev,
2656 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2657 			   type_str, data1, data2,
2658 			   bp->fw_reset_min_dsecs * 100,
2659 			   bp->fw_reset_max_dsecs * 100);
2660 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2661 		break;
2662 	}
2663 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2664 		struct bnxt_fw_health *fw_health = bp->fw_health;
2665 		char *status_desc = "healthy";
2666 		u32 status;
2667 
2668 		if (!fw_health)
2669 			goto async_event_process_exit;
2670 
2671 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2672 			fw_health->enabled = false;
2673 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2674 			break;
2675 		}
2676 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2677 		fw_health->tmr_multiplier =
2678 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2679 				     bp->current_interval * 10);
2680 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2681 		if (!fw_health->enabled)
2682 			fw_health->last_fw_heartbeat =
2683 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2684 		fw_health->last_fw_reset_cnt =
2685 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2686 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2687 		if (status != BNXT_FW_STATUS_HEALTHY)
2688 			status_desc = "unhealthy";
2689 		netif_info(bp, drv, bp->dev,
2690 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2691 			   fw_health->primary ? "primary" : "backup", status,
2692 			   status_desc, fw_health->last_fw_reset_cnt);
2693 		if (!fw_health->enabled) {
2694 			/* Make sure tmr_counter is set and visible to
2695 			 * bnxt_health_check() before setting enabled to true.
2696 			 */
2697 			smp_wmb();
2698 			fw_health->enabled = true;
2699 		}
2700 		goto async_event_process_exit;
2701 	}
2702 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2703 		netif_notice(bp, hw, bp->dev,
2704 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2705 			     data1, data2);
2706 		goto async_event_process_exit;
2707 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2708 		struct bnxt_rx_ring_info *rxr;
2709 		u16 grp_idx;
2710 
2711 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2712 			goto async_event_process_exit;
2713 
2714 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2715 			    BNXT_EVENT_RING_TYPE(data2), data1);
2716 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2717 			goto async_event_process_exit;
2718 
2719 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2720 		if (grp_idx == INVALID_HW_RING_ID) {
2721 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2722 				    data1);
2723 			goto async_event_process_exit;
2724 		}
2725 		rxr = bp->bnapi[grp_idx]->rx_ring;
2726 		bnxt_sched_reset_rxr(bp, rxr);
2727 		goto async_event_process_exit;
2728 	}
2729 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2730 		struct bnxt_fw_health *fw_health = bp->fw_health;
2731 
2732 		netif_notice(bp, hw, bp->dev,
2733 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2734 			     data1, data2);
2735 		if (fw_health) {
2736 			fw_health->echo_req_data1 = data1;
2737 			fw_health->echo_req_data2 = data2;
2738 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2739 			break;
2740 		}
2741 		goto async_event_process_exit;
2742 	}
2743 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2744 		bnxt_ptp_pps_event(bp, data1, data2);
2745 		goto async_event_process_exit;
2746 	}
2747 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2748 		if (bnxt_event_error_report(bp, data1, data2))
2749 			break;
2750 		goto async_event_process_exit;
2751 	}
2752 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2753 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2754 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2755 			if (BNXT_PTP_USE_RTC(bp)) {
2756 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2757 				unsigned long flags;
2758 				u64 ns;
2759 
2760 				if (!ptp)
2761 					goto async_event_process_exit;
2762 
2763 				bnxt_ptp_update_current_time(bp);
2764 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2765 				       BNXT_PHC_BITS) | ptp->current_time);
2766 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2767 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2768 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2769 			}
2770 			break;
2771 		}
2772 		goto async_event_process_exit;
2773 	}
2774 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2775 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2776 
2777 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2778 		goto async_event_process_exit;
2779 	}
2780 	default:
2781 		goto async_event_process_exit;
2782 	}
2783 	__bnxt_queue_sp_work(bp);
2784 async_event_process_exit:
2785 	return 0;
2786 }
2787 
2788 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2789 {
2790 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2791 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2792 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2793 				(struct hwrm_fwd_req_cmpl *)txcmp;
2794 
2795 	switch (cmpl_type) {
2796 	case CMPL_BASE_TYPE_HWRM_DONE:
2797 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2798 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2799 		break;
2800 
2801 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2802 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2803 
2804 		if ((vf_id < bp->pf.first_vf_id) ||
2805 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2806 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2807 				   vf_id);
2808 			return -EINVAL;
2809 		}
2810 
2811 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2812 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2813 		break;
2814 
2815 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2816 		bnxt_async_event_process(bp,
2817 					 (struct hwrm_async_event_cmpl *)txcmp);
2818 		break;
2819 
2820 	default:
2821 		break;
2822 	}
2823 
2824 	return 0;
2825 }
2826 
2827 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2828 {
2829 	struct bnxt_napi *bnapi = dev_instance;
2830 	struct bnxt *bp = bnapi->bp;
2831 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2832 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2833 
2834 	cpr->event_ctr++;
2835 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2836 	napi_schedule(&bnapi->napi);
2837 	return IRQ_HANDLED;
2838 }
2839 
2840 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2841 {
2842 	u32 raw_cons = cpr->cp_raw_cons;
2843 	u16 cons = RING_CMP(raw_cons);
2844 	struct tx_cmp *txcmp;
2845 
2846 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2847 
2848 	return TX_CMP_VALID(txcmp, raw_cons);
2849 }
2850 
2851 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2852 			    int budget)
2853 {
2854 	struct bnxt_napi *bnapi = cpr->bnapi;
2855 	u32 raw_cons = cpr->cp_raw_cons;
2856 	u32 cons;
2857 	int rx_pkts = 0;
2858 	u8 event = 0;
2859 	struct tx_cmp *txcmp;
2860 
2861 	cpr->has_more_work = 0;
2862 	cpr->had_work_done = 1;
2863 	while (1) {
2864 		u8 cmp_type;
2865 		int rc;
2866 
2867 		cons = RING_CMP(raw_cons);
2868 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2869 
2870 		if (!TX_CMP_VALID(txcmp, raw_cons))
2871 			break;
2872 
2873 		/* The valid test of the entry must be done first before
2874 		 * reading any further.
2875 		 */
2876 		dma_rmb();
2877 		cmp_type = TX_CMP_TYPE(txcmp);
2878 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
2879 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
2880 			u32 opaque = txcmp->tx_cmp_opaque;
2881 			struct bnxt_tx_ring_info *txr;
2882 			u16 tx_freed;
2883 
2884 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
2885 			event |= BNXT_TX_CMP_EVENT;
2886 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
2887 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
2888 			else
2889 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
2890 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
2891 				   bp->tx_ring_mask;
2892 			/* return full budget so NAPI will complete. */
2893 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
2894 				rx_pkts = budget;
2895 				raw_cons = NEXT_RAW_CMP(raw_cons);
2896 				if (budget)
2897 					cpr->has_more_work = 1;
2898 				break;
2899 			}
2900 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
2901 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
2902 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
2903 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2904 			if (likely(budget))
2905 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2906 			else
2907 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2908 							   &event);
2909 			if (likely(rc >= 0))
2910 				rx_pkts += rc;
2911 			/* Increment rx_pkts when rc is -ENOMEM to count towards
2912 			 * the NAPI budget.  Otherwise, we may potentially loop
2913 			 * here forever if we consistently cannot allocate
2914 			 * buffers.
2915 			 */
2916 			else if (rc == -ENOMEM && budget)
2917 				rx_pkts++;
2918 			else if (rc == -EBUSY)	/* partial completion */
2919 				break;
2920 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
2921 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
2922 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
2923 			bnxt_hwrm_handler(bp, txcmp);
2924 		}
2925 		raw_cons = NEXT_RAW_CMP(raw_cons);
2926 
2927 		if (rx_pkts && rx_pkts == budget) {
2928 			cpr->has_more_work = 1;
2929 			break;
2930 		}
2931 	}
2932 
2933 	if (event & BNXT_REDIRECT_EVENT) {
2934 		xdp_do_flush();
2935 		event &= ~BNXT_REDIRECT_EVENT;
2936 	}
2937 
2938 	if (event & BNXT_TX_EVENT) {
2939 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
2940 		u16 prod = txr->tx_prod;
2941 
2942 		/* Sync BD data before updating doorbell */
2943 		wmb();
2944 
2945 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
2946 		event &= ~BNXT_TX_EVENT;
2947 	}
2948 
2949 	cpr->cp_raw_cons = raw_cons;
2950 	bnapi->events |= event;
2951 	return rx_pkts;
2952 }
2953 
2954 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
2955 				  int budget)
2956 {
2957 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
2958 		bnapi->tx_int(bp, bnapi, budget);
2959 
2960 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
2961 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2962 
2963 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2964 		bnapi->events &= ~BNXT_RX_EVENT;
2965 	}
2966 	if (bnapi->events & BNXT_AGG_EVENT) {
2967 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2968 
2969 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2970 		bnapi->events &= ~BNXT_AGG_EVENT;
2971 	}
2972 }
2973 
2974 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2975 			  int budget)
2976 {
2977 	struct bnxt_napi *bnapi = cpr->bnapi;
2978 	int rx_pkts;
2979 
2980 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
2981 
2982 	/* ACK completion ring before freeing tx ring and producing new
2983 	 * buffers in rx/agg rings to prevent overflowing the completion
2984 	 * ring.
2985 	 */
2986 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
2987 
2988 	__bnxt_poll_work_done(bp, bnapi, budget);
2989 	return rx_pkts;
2990 }
2991 
2992 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
2993 {
2994 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2995 	struct bnxt *bp = bnapi->bp;
2996 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2997 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2998 	struct tx_cmp *txcmp;
2999 	struct rx_cmp_ext *rxcmp1;
3000 	u32 cp_cons, tmp_raw_cons;
3001 	u32 raw_cons = cpr->cp_raw_cons;
3002 	bool flush_xdp = false;
3003 	u32 rx_pkts = 0;
3004 	u8 event = 0;
3005 
3006 	while (1) {
3007 		int rc;
3008 
3009 		cp_cons = RING_CMP(raw_cons);
3010 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3011 
3012 		if (!TX_CMP_VALID(txcmp, raw_cons))
3013 			break;
3014 
3015 		/* The valid test of the entry must be done first before
3016 		 * reading any further.
3017 		 */
3018 		dma_rmb();
3019 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3020 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3021 			cp_cons = RING_CMP(tmp_raw_cons);
3022 			rxcmp1 = (struct rx_cmp_ext *)
3023 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3024 
3025 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3026 				break;
3027 
3028 			/* force an error to recycle the buffer */
3029 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3030 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3031 
3032 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3033 			if (likely(rc == -EIO) && budget)
3034 				rx_pkts++;
3035 			else if (rc == -EBUSY)	/* partial completion */
3036 				break;
3037 			if (event & BNXT_REDIRECT_EVENT)
3038 				flush_xdp = true;
3039 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3040 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3041 			bnxt_hwrm_handler(bp, txcmp);
3042 		} else {
3043 			netdev_err(bp->dev,
3044 				   "Invalid completion received on special ring\n");
3045 		}
3046 		raw_cons = NEXT_RAW_CMP(raw_cons);
3047 
3048 		if (rx_pkts == budget)
3049 			break;
3050 	}
3051 
3052 	cpr->cp_raw_cons = raw_cons;
3053 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3054 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3055 
3056 	if (event & BNXT_AGG_EVENT)
3057 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3058 	if (flush_xdp)
3059 		xdp_do_flush();
3060 
3061 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3062 		napi_complete_done(napi, rx_pkts);
3063 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3064 	}
3065 	return rx_pkts;
3066 }
3067 
3068 static int bnxt_poll(struct napi_struct *napi, int budget)
3069 {
3070 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3071 	struct bnxt *bp = bnapi->bp;
3072 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3073 	int work_done = 0;
3074 
3075 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3076 		napi_complete(napi);
3077 		return 0;
3078 	}
3079 	while (1) {
3080 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3081 
3082 		if (work_done >= budget) {
3083 			if (!budget)
3084 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3085 			break;
3086 		}
3087 
3088 		if (!bnxt_has_work(bp, cpr)) {
3089 			if (napi_complete_done(napi, work_done))
3090 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3091 			break;
3092 		}
3093 	}
3094 	if (bp->flags & BNXT_FLAG_DIM) {
3095 		struct dim_sample dim_sample = {};
3096 
3097 		dim_update_sample(cpr->event_ctr,
3098 				  cpr->rx_packets,
3099 				  cpr->rx_bytes,
3100 				  &dim_sample);
3101 		net_dim(&cpr->dim, &dim_sample);
3102 	}
3103 	return work_done;
3104 }
3105 
3106 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3107 {
3108 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3109 	int i, work_done = 0;
3110 
3111 	for (i = 0; i < cpr->cp_ring_count; i++) {
3112 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3113 
3114 		if (cpr2->had_nqe_notify) {
3115 			work_done += __bnxt_poll_work(bp, cpr2,
3116 						      budget - work_done);
3117 			cpr->has_more_work |= cpr2->has_more_work;
3118 		}
3119 	}
3120 	return work_done;
3121 }
3122 
3123 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3124 				 u64 dbr_type, int budget)
3125 {
3126 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3127 	int i;
3128 
3129 	for (i = 0; i < cpr->cp_ring_count; i++) {
3130 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3131 		struct bnxt_db_info *db;
3132 
3133 		if (cpr2->had_work_done) {
3134 			u32 tgl = 0;
3135 
3136 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3137 				cpr2->had_nqe_notify = 0;
3138 				tgl = cpr2->toggle;
3139 			}
3140 			db = &cpr2->cp_db;
3141 			bnxt_writeq(bp,
3142 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3143 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3144 				    db->doorbell);
3145 			cpr2->had_work_done = 0;
3146 		}
3147 	}
3148 	__bnxt_poll_work_done(bp, bnapi, budget);
3149 }
3150 
3151 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3152 {
3153 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3154 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3155 	struct bnxt_cp_ring_info *cpr_rx;
3156 	u32 raw_cons = cpr->cp_raw_cons;
3157 	struct bnxt *bp = bnapi->bp;
3158 	struct nqe_cn *nqcmp;
3159 	int work_done = 0;
3160 	u32 cons;
3161 
3162 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3163 		napi_complete(napi);
3164 		return 0;
3165 	}
3166 	if (cpr->has_more_work) {
3167 		cpr->has_more_work = 0;
3168 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3169 	}
3170 	while (1) {
3171 		u16 type;
3172 
3173 		cons = RING_CMP(raw_cons);
3174 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3175 
3176 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3177 			if (cpr->has_more_work)
3178 				break;
3179 
3180 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3181 					     budget);
3182 			cpr->cp_raw_cons = raw_cons;
3183 			if (napi_complete_done(napi, work_done))
3184 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3185 						  cpr->cp_raw_cons);
3186 			goto poll_done;
3187 		}
3188 
3189 		/* The valid test of the entry must be done first before
3190 		 * reading any further.
3191 		 */
3192 		dma_rmb();
3193 
3194 		type = le16_to_cpu(nqcmp->type);
3195 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3196 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3197 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3198 			struct bnxt_cp_ring_info *cpr2;
3199 
3200 			/* No more budget for RX work */
3201 			if (budget && work_done >= budget &&
3202 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3203 				break;
3204 
3205 			idx = BNXT_NQ_HDL_IDX(idx);
3206 			cpr2 = &cpr->cp_ring_arr[idx];
3207 			cpr2->had_nqe_notify = 1;
3208 			cpr2->toggle = NQE_CN_TOGGLE(type);
3209 			work_done += __bnxt_poll_work(bp, cpr2,
3210 						      budget - work_done);
3211 			cpr->has_more_work |= cpr2->has_more_work;
3212 		} else {
3213 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3214 		}
3215 		raw_cons = NEXT_RAW_CMP(raw_cons);
3216 	}
3217 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3218 	if (raw_cons != cpr->cp_raw_cons) {
3219 		cpr->cp_raw_cons = raw_cons;
3220 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3221 	}
3222 poll_done:
3223 	cpr_rx = &cpr->cp_ring_arr[0];
3224 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3225 	    (bp->flags & BNXT_FLAG_DIM)) {
3226 		struct dim_sample dim_sample = {};
3227 
3228 		dim_update_sample(cpr->event_ctr,
3229 				  cpr_rx->rx_packets,
3230 				  cpr_rx->rx_bytes,
3231 				  &dim_sample);
3232 		net_dim(&cpr->dim, &dim_sample);
3233 	}
3234 	return work_done;
3235 }
3236 
3237 static void bnxt_free_tx_skbs(struct bnxt *bp)
3238 {
3239 	int i, max_idx;
3240 	struct pci_dev *pdev = bp->pdev;
3241 
3242 	if (!bp->tx_ring)
3243 		return;
3244 
3245 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3246 	for (i = 0; i < bp->tx_nr_rings; i++) {
3247 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3248 		int j;
3249 
3250 		if (!txr->tx_buf_ring)
3251 			continue;
3252 
3253 		for (j = 0; j < max_idx;) {
3254 			struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
3255 			struct sk_buff *skb;
3256 			int k, last;
3257 
3258 			if (i < bp->tx_nr_rings_xdp &&
3259 			    tx_buf->action == XDP_REDIRECT) {
3260 				dma_unmap_single(&pdev->dev,
3261 					dma_unmap_addr(tx_buf, mapping),
3262 					dma_unmap_len(tx_buf, len),
3263 					DMA_TO_DEVICE);
3264 				xdp_return_frame(tx_buf->xdpf);
3265 				tx_buf->action = 0;
3266 				tx_buf->xdpf = NULL;
3267 				j++;
3268 				continue;
3269 			}
3270 
3271 			skb = tx_buf->skb;
3272 			if (!skb) {
3273 				j++;
3274 				continue;
3275 			}
3276 
3277 			tx_buf->skb = NULL;
3278 
3279 			if (tx_buf->is_push) {
3280 				dev_kfree_skb(skb);
3281 				j += 2;
3282 				continue;
3283 			}
3284 
3285 			dma_unmap_single(&pdev->dev,
3286 					 dma_unmap_addr(tx_buf, mapping),
3287 					 skb_headlen(skb),
3288 					 DMA_TO_DEVICE);
3289 
3290 			last = tx_buf->nr_frags;
3291 			j += 2;
3292 			for (k = 0; k < last; k++, j++) {
3293 				int ring_idx = j & bp->tx_ring_mask;
3294 				skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
3295 
3296 				tx_buf = &txr->tx_buf_ring[ring_idx];
3297 				dma_unmap_page(
3298 					&pdev->dev,
3299 					dma_unmap_addr(tx_buf, mapping),
3300 					skb_frag_size(frag), DMA_TO_DEVICE);
3301 			}
3302 			dev_kfree_skb(skb);
3303 		}
3304 		netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
3305 	}
3306 }
3307 
3308 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3309 {
3310 	int i, max_idx;
3311 
3312 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3313 
3314 	for (i = 0; i < max_idx; i++) {
3315 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3316 		void *data = rx_buf->data;
3317 
3318 		if (!data)
3319 			continue;
3320 
3321 		rx_buf->data = NULL;
3322 		if (BNXT_RX_PAGE_MODE(bp))
3323 			page_pool_recycle_direct(rxr->page_pool, data);
3324 		else
3325 			page_pool_free_va(rxr->head_pool, data, true);
3326 	}
3327 }
3328 
3329 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3330 {
3331 	int i, max_idx;
3332 
3333 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3334 
3335 	for (i = 0; i < max_idx; i++) {
3336 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3337 		struct page *page = rx_agg_buf->page;
3338 
3339 		if (!page)
3340 			continue;
3341 
3342 		rx_agg_buf->page = NULL;
3343 		__clear_bit(i, rxr->rx_agg_bmap);
3344 
3345 		page_pool_recycle_direct(rxr->page_pool, page);
3346 	}
3347 }
3348 
3349 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, int ring_nr)
3350 {
3351 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
3352 	struct bnxt_tpa_idx_map *map;
3353 	int i;
3354 
3355 	if (!rxr->rx_tpa)
3356 		goto skip_rx_tpa_free;
3357 
3358 	for (i = 0; i < bp->max_tpa; i++) {
3359 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3360 		u8 *data = tpa_info->data;
3361 
3362 		if (!data)
3363 			continue;
3364 
3365 		tpa_info->data = NULL;
3366 		page_pool_free_va(rxr->head_pool, data, false);
3367 	}
3368 
3369 skip_rx_tpa_free:
3370 	if (!rxr->rx_buf_ring)
3371 		goto skip_rx_buf_free;
3372 
3373 	bnxt_free_one_rx_ring(bp, rxr);
3374 
3375 skip_rx_buf_free:
3376 	if (!rxr->rx_agg_ring)
3377 		goto skip_rx_agg_free;
3378 
3379 	bnxt_free_one_rx_agg_ring(bp, rxr);
3380 
3381 skip_rx_agg_free:
3382 	map = rxr->rx_tpa_idx_map;
3383 	if (map)
3384 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3385 }
3386 
3387 static void bnxt_free_rx_skbs(struct bnxt *bp)
3388 {
3389 	int i;
3390 
3391 	if (!bp->rx_ring)
3392 		return;
3393 
3394 	for (i = 0; i < bp->rx_nr_rings; i++)
3395 		bnxt_free_one_rx_ring_skbs(bp, i);
3396 }
3397 
3398 static void bnxt_free_skbs(struct bnxt *bp)
3399 {
3400 	bnxt_free_tx_skbs(bp);
3401 	bnxt_free_rx_skbs(bp);
3402 }
3403 
3404 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3405 {
3406 	u8 init_val = ctxm->init_value;
3407 	u16 offset = ctxm->init_offset;
3408 	u8 *p2 = p;
3409 	int i;
3410 
3411 	if (!init_val)
3412 		return;
3413 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3414 		memset(p, init_val, len);
3415 		return;
3416 	}
3417 	for (i = 0; i < len; i += ctxm->entry_size)
3418 		*(p2 + i + offset) = init_val;
3419 }
3420 
3421 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3422 {
3423 	struct pci_dev *pdev = bp->pdev;
3424 	int i;
3425 
3426 	if (!rmem->pg_arr)
3427 		goto skip_pages;
3428 
3429 	for (i = 0; i < rmem->nr_pages; i++) {
3430 		if (!rmem->pg_arr[i])
3431 			continue;
3432 
3433 		dma_free_coherent(&pdev->dev, rmem->page_size,
3434 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3435 
3436 		rmem->pg_arr[i] = NULL;
3437 	}
3438 skip_pages:
3439 	if (rmem->pg_tbl) {
3440 		size_t pg_tbl_size = rmem->nr_pages * 8;
3441 
3442 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3443 			pg_tbl_size = rmem->page_size;
3444 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3445 				  rmem->pg_tbl, rmem->pg_tbl_map);
3446 		rmem->pg_tbl = NULL;
3447 	}
3448 	if (rmem->vmem_size && *rmem->vmem) {
3449 		vfree(*rmem->vmem);
3450 		*rmem->vmem = NULL;
3451 	}
3452 }
3453 
3454 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3455 {
3456 	struct pci_dev *pdev = bp->pdev;
3457 	u64 valid_bit = 0;
3458 	int i;
3459 
3460 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3461 		valid_bit = PTU_PTE_VALID;
3462 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !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 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3468 						  &rmem->pg_tbl_map,
3469 						  GFP_KERNEL);
3470 		if (!rmem->pg_tbl)
3471 			return -ENOMEM;
3472 	}
3473 
3474 	for (i = 0; i < rmem->nr_pages; i++) {
3475 		u64 extra_bits = valid_bit;
3476 
3477 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3478 						     rmem->page_size,
3479 						     &rmem->dma_arr[i],
3480 						     GFP_KERNEL);
3481 		if (!rmem->pg_arr[i])
3482 			return -ENOMEM;
3483 
3484 		if (rmem->ctx_mem)
3485 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3486 					  rmem->page_size);
3487 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3488 			if (i == rmem->nr_pages - 2 &&
3489 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3490 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3491 			else if (i == rmem->nr_pages - 1 &&
3492 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3493 				extra_bits |= PTU_PTE_LAST;
3494 			rmem->pg_tbl[i] =
3495 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3496 		}
3497 	}
3498 
3499 	if (rmem->vmem_size) {
3500 		*rmem->vmem = vzalloc(rmem->vmem_size);
3501 		if (!(*rmem->vmem))
3502 			return -ENOMEM;
3503 	}
3504 	return 0;
3505 }
3506 
3507 static void bnxt_free_tpa_info(struct bnxt *bp)
3508 {
3509 	int i, j;
3510 
3511 	for (i = 0; i < bp->rx_nr_rings; i++) {
3512 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3513 
3514 		kfree(rxr->rx_tpa_idx_map);
3515 		rxr->rx_tpa_idx_map = NULL;
3516 		if (rxr->rx_tpa) {
3517 			for (j = 0; j < bp->max_tpa; j++) {
3518 				kfree(rxr->rx_tpa[j].agg_arr);
3519 				rxr->rx_tpa[j].agg_arr = NULL;
3520 			}
3521 		}
3522 		kfree(rxr->rx_tpa);
3523 		rxr->rx_tpa = NULL;
3524 	}
3525 }
3526 
3527 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3528 {
3529 	int i, j;
3530 
3531 	bp->max_tpa = MAX_TPA;
3532 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3533 		if (!bp->max_tpa_v2)
3534 			return 0;
3535 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3536 	}
3537 
3538 	for (i = 0; i < bp->rx_nr_rings; i++) {
3539 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3540 		struct rx_agg_cmp *agg;
3541 
3542 		rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3543 				      GFP_KERNEL);
3544 		if (!rxr->rx_tpa)
3545 			return -ENOMEM;
3546 
3547 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3548 			continue;
3549 		for (j = 0; j < bp->max_tpa; j++) {
3550 			agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3551 			if (!agg)
3552 				return -ENOMEM;
3553 			rxr->rx_tpa[j].agg_arr = agg;
3554 		}
3555 		rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3556 					      GFP_KERNEL);
3557 		if (!rxr->rx_tpa_idx_map)
3558 			return -ENOMEM;
3559 	}
3560 	return 0;
3561 }
3562 
3563 static void bnxt_free_rx_rings(struct bnxt *bp)
3564 {
3565 	int i;
3566 
3567 	if (!bp->rx_ring)
3568 		return;
3569 
3570 	bnxt_free_tpa_info(bp);
3571 	for (i = 0; i < bp->rx_nr_rings; i++) {
3572 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3573 		struct bnxt_ring_struct *ring;
3574 
3575 		if (rxr->xdp_prog)
3576 			bpf_prog_put(rxr->xdp_prog);
3577 
3578 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3579 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3580 
3581 		page_pool_destroy(rxr->page_pool);
3582 		if (rxr->page_pool != rxr->head_pool)
3583 			page_pool_destroy(rxr->head_pool);
3584 		rxr->page_pool = rxr->head_pool = NULL;
3585 
3586 		kfree(rxr->rx_agg_bmap);
3587 		rxr->rx_agg_bmap = NULL;
3588 
3589 		ring = &rxr->rx_ring_struct;
3590 		bnxt_free_ring(bp, &ring->ring_mem);
3591 
3592 		ring = &rxr->rx_agg_ring_struct;
3593 		bnxt_free_ring(bp, &ring->ring_mem);
3594 	}
3595 }
3596 
3597 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3598 				   struct bnxt_rx_ring_info *rxr,
3599 				   int numa_node)
3600 {
3601 	struct page_pool_params pp = { 0 };
3602 	struct page_pool *pool;
3603 
3604 	pp.pool_size = bp->rx_agg_ring_size;
3605 	if (BNXT_RX_PAGE_MODE(bp))
3606 		pp.pool_size += bp->rx_ring_size;
3607 	pp.nid = numa_node;
3608 	pp.napi = &rxr->bnapi->napi;
3609 	pp.netdev = bp->dev;
3610 	pp.dev = &bp->pdev->dev;
3611 	pp.dma_dir = bp->rx_dir;
3612 	pp.max_len = PAGE_SIZE;
3613 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3614 
3615 	pool = page_pool_create(&pp);
3616 	if (IS_ERR(pool))
3617 		return PTR_ERR(pool);
3618 	rxr->page_pool = pool;
3619 
3620 	if (bnxt_separate_head_pool()) {
3621 		pp.pool_size = max(bp->rx_ring_size, 1024);
3622 		pool = page_pool_create(&pp);
3623 		if (IS_ERR(pool))
3624 			goto err_destroy_pp;
3625 	}
3626 	rxr->head_pool = pool;
3627 
3628 	return 0;
3629 
3630 err_destroy_pp:
3631 	page_pool_destroy(rxr->page_pool);
3632 	rxr->page_pool = NULL;
3633 	return PTR_ERR(pool);
3634 }
3635 
3636 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3637 {
3638 	int numa_node = dev_to_node(&bp->pdev->dev);
3639 	int i, rc = 0, agg_rings = 0, cpu;
3640 
3641 	if (!bp->rx_ring)
3642 		return -ENOMEM;
3643 
3644 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3645 		agg_rings = 1;
3646 
3647 	for (i = 0; i < bp->rx_nr_rings; i++) {
3648 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3649 		struct bnxt_ring_struct *ring;
3650 		int cpu_node;
3651 
3652 		ring = &rxr->rx_ring_struct;
3653 
3654 		cpu = cpumask_local_spread(i, numa_node);
3655 		cpu_node = cpu_to_node(cpu);
3656 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3657 			   i, cpu_node);
3658 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3659 		if (rc)
3660 			return rc;
3661 
3662 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3663 		if (rc < 0)
3664 			return rc;
3665 
3666 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3667 						MEM_TYPE_PAGE_POOL,
3668 						rxr->page_pool);
3669 		if (rc) {
3670 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3671 			return rc;
3672 		}
3673 
3674 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3675 		if (rc)
3676 			return rc;
3677 
3678 		ring->grp_idx = i;
3679 		if (agg_rings) {
3680 			u16 mem_size;
3681 
3682 			ring = &rxr->rx_agg_ring_struct;
3683 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3684 			if (rc)
3685 				return rc;
3686 
3687 			ring->grp_idx = i;
3688 			rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3689 			mem_size = rxr->rx_agg_bmap_size / 8;
3690 			rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3691 			if (!rxr->rx_agg_bmap)
3692 				return -ENOMEM;
3693 		}
3694 	}
3695 	if (bp->flags & BNXT_FLAG_TPA)
3696 		rc = bnxt_alloc_tpa_info(bp);
3697 	return rc;
3698 }
3699 
3700 static void bnxt_free_tx_rings(struct bnxt *bp)
3701 {
3702 	int i;
3703 	struct pci_dev *pdev = bp->pdev;
3704 
3705 	if (!bp->tx_ring)
3706 		return;
3707 
3708 	for (i = 0; i < bp->tx_nr_rings; i++) {
3709 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3710 		struct bnxt_ring_struct *ring;
3711 
3712 		if (txr->tx_push) {
3713 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3714 					  txr->tx_push, txr->tx_push_mapping);
3715 			txr->tx_push = NULL;
3716 		}
3717 
3718 		ring = &txr->tx_ring_struct;
3719 
3720 		bnxt_free_ring(bp, &ring->ring_mem);
3721 	}
3722 }
3723 
3724 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3725 	((tc) * (bp)->tx_nr_rings_per_tc)
3726 
3727 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3728 	((tx) % (bp)->tx_nr_rings_per_tc)
3729 
3730 #define BNXT_RING_TO_TC(bp, tx)		\
3731 	((tx) / (bp)->tx_nr_rings_per_tc)
3732 
3733 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3734 {
3735 	int i, j, rc;
3736 	struct pci_dev *pdev = bp->pdev;
3737 
3738 	bp->tx_push_size = 0;
3739 	if (bp->tx_push_thresh) {
3740 		int push_size;
3741 
3742 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3743 					bp->tx_push_thresh);
3744 
3745 		if (push_size > 256) {
3746 			push_size = 0;
3747 			bp->tx_push_thresh = 0;
3748 		}
3749 
3750 		bp->tx_push_size = push_size;
3751 	}
3752 
3753 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3754 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3755 		struct bnxt_ring_struct *ring;
3756 		u8 qidx;
3757 
3758 		ring = &txr->tx_ring_struct;
3759 
3760 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3761 		if (rc)
3762 			return rc;
3763 
3764 		ring->grp_idx = txr->bnapi->index;
3765 		if (bp->tx_push_size) {
3766 			dma_addr_t mapping;
3767 
3768 			/* One pre-allocated DMA buffer to backup
3769 			 * TX push operation
3770 			 */
3771 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3772 						bp->tx_push_size,
3773 						&txr->tx_push_mapping,
3774 						GFP_KERNEL);
3775 
3776 			if (!txr->tx_push)
3777 				return -ENOMEM;
3778 
3779 			mapping = txr->tx_push_mapping +
3780 				sizeof(struct tx_push_bd);
3781 			txr->data_mapping = cpu_to_le64(mapping);
3782 		}
3783 		qidx = bp->tc_to_qidx[j];
3784 		ring->queue_id = bp->q_info[qidx].queue_id;
3785 		spin_lock_init(&txr->xdp_tx_lock);
3786 		if (i < bp->tx_nr_rings_xdp)
3787 			continue;
3788 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
3789 			j++;
3790 	}
3791 	return 0;
3792 }
3793 
3794 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
3795 {
3796 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3797 
3798 	kfree(cpr->cp_desc_ring);
3799 	cpr->cp_desc_ring = NULL;
3800 	ring->ring_mem.pg_arr = NULL;
3801 	kfree(cpr->cp_desc_mapping);
3802 	cpr->cp_desc_mapping = NULL;
3803 	ring->ring_mem.dma_arr = NULL;
3804 }
3805 
3806 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
3807 {
3808 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
3809 	if (!cpr->cp_desc_ring)
3810 		return -ENOMEM;
3811 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
3812 				       GFP_KERNEL);
3813 	if (!cpr->cp_desc_mapping)
3814 		return -ENOMEM;
3815 	return 0;
3816 }
3817 
3818 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
3819 {
3820 	int i;
3821 
3822 	if (!bp->bnapi)
3823 		return;
3824 	for (i = 0; i < bp->cp_nr_rings; i++) {
3825 		struct bnxt_napi *bnapi = bp->bnapi[i];
3826 
3827 		if (!bnapi)
3828 			continue;
3829 		bnxt_free_cp_arrays(&bnapi->cp_ring);
3830 	}
3831 }
3832 
3833 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
3834 {
3835 	int i, n = bp->cp_nr_pages;
3836 
3837 	for (i = 0; i < bp->cp_nr_rings; i++) {
3838 		struct bnxt_napi *bnapi = bp->bnapi[i];
3839 		int rc;
3840 
3841 		if (!bnapi)
3842 			continue;
3843 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
3844 		if (rc)
3845 			return rc;
3846 	}
3847 	return 0;
3848 }
3849 
3850 static void bnxt_free_cp_rings(struct bnxt *bp)
3851 {
3852 	int i;
3853 
3854 	if (!bp->bnapi)
3855 		return;
3856 
3857 	for (i = 0; i < bp->cp_nr_rings; i++) {
3858 		struct bnxt_napi *bnapi = bp->bnapi[i];
3859 		struct bnxt_cp_ring_info *cpr;
3860 		struct bnxt_ring_struct *ring;
3861 		int j;
3862 
3863 		if (!bnapi)
3864 			continue;
3865 
3866 		cpr = &bnapi->cp_ring;
3867 		ring = &cpr->cp_ring_struct;
3868 
3869 		bnxt_free_ring(bp, &ring->ring_mem);
3870 
3871 		if (!cpr->cp_ring_arr)
3872 			continue;
3873 
3874 		for (j = 0; j < cpr->cp_ring_count; j++) {
3875 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
3876 
3877 			ring = &cpr2->cp_ring_struct;
3878 			bnxt_free_ring(bp, &ring->ring_mem);
3879 			bnxt_free_cp_arrays(cpr2);
3880 		}
3881 		kfree(cpr->cp_ring_arr);
3882 		cpr->cp_ring_arr = NULL;
3883 		cpr->cp_ring_count = 0;
3884 	}
3885 }
3886 
3887 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
3888 				  struct bnxt_cp_ring_info *cpr)
3889 {
3890 	struct bnxt_ring_mem_info *rmem;
3891 	struct bnxt_ring_struct *ring;
3892 	int rc;
3893 
3894 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
3895 	if (rc) {
3896 		bnxt_free_cp_arrays(cpr);
3897 		return -ENOMEM;
3898 	}
3899 	ring = &cpr->cp_ring_struct;
3900 	rmem = &ring->ring_mem;
3901 	rmem->nr_pages = bp->cp_nr_pages;
3902 	rmem->page_size = HW_CMPD_RING_SIZE;
3903 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
3904 	rmem->dma_arr = cpr->cp_desc_mapping;
3905 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
3906 	rc = bnxt_alloc_ring(bp, rmem);
3907 	if (rc) {
3908 		bnxt_free_ring(bp, rmem);
3909 		bnxt_free_cp_arrays(cpr);
3910 	}
3911 	return rc;
3912 }
3913 
3914 static int bnxt_alloc_cp_rings(struct bnxt *bp)
3915 {
3916 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
3917 	int i, j, rc, ulp_msix;
3918 	int tcs = bp->num_tc;
3919 
3920 	if (!tcs)
3921 		tcs = 1;
3922 	ulp_msix = bnxt_get_ulp_msix_num(bp);
3923 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
3924 		struct bnxt_napi *bnapi = bp->bnapi[i];
3925 		struct bnxt_cp_ring_info *cpr, *cpr2;
3926 		struct bnxt_ring_struct *ring;
3927 		int cp_count = 0, k;
3928 		int rx = 0, tx = 0;
3929 
3930 		if (!bnapi)
3931 			continue;
3932 
3933 		cpr = &bnapi->cp_ring;
3934 		cpr->bnapi = bnapi;
3935 		ring = &cpr->cp_ring_struct;
3936 
3937 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3938 		if (rc)
3939 			return rc;
3940 
3941 		ring->map_idx = ulp_msix + i;
3942 
3943 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3944 			continue;
3945 
3946 		if (i < bp->rx_nr_rings) {
3947 			cp_count++;
3948 			rx = 1;
3949 		}
3950 		if (i < bp->tx_nr_rings_xdp) {
3951 			cp_count++;
3952 			tx = 1;
3953 		} else if ((sh && i < bp->tx_nr_rings) ||
3954 			 (!sh && i >= bp->rx_nr_rings)) {
3955 			cp_count += tcs;
3956 			tx = 1;
3957 		}
3958 
3959 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
3960 					   GFP_KERNEL);
3961 		if (!cpr->cp_ring_arr)
3962 			return -ENOMEM;
3963 		cpr->cp_ring_count = cp_count;
3964 
3965 		for (k = 0; k < cp_count; k++) {
3966 			cpr2 = &cpr->cp_ring_arr[k];
3967 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
3968 			if (rc)
3969 				return rc;
3970 			cpr2->bnapi = bnapi;
3971 			cpr2->sw_stats = cpr->sw_stats;
3972 			cpr2->cp_idx = k;
3973 			if (!k && rx) {
3974 				bp->rx_ring[i].rx_cpr = cpr2;
3975 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
3976 			} else {
3977 				int n, tc = k - rx;
3978 
3979 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
3980 				bp->tx_ring[n].tx_cpr = cpr2;
3981 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
3982 			}
3983 		}
3984 		if (tx)
3985 			j++;
3986 	}
3987 	return 0;
3988 }
3989 
3990 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
3991 				     struct bnxt_rx_ring_info *rxr)
3992 {
3993 	struct bnxt_ring_mem_info *rmem;
3994 	struct bnxt_ring_struct *ring;
3995 
3996 	ring = &rxr->rx_ring_struct;
3997 	rmem = &ring->ring_mem;
3998 	rmem->nr_pages = bp->rx_nr_pages;
3999 	rmem->page_size = HW_RXBD_RING_SIZE;
4000 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4001 	rmem->dma_arr = rxr->rx_desc_mapping;
4002 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4003 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4004 
4005 	ring = &rxr->rx_agg_ring_struct;
4006 	rmem = &ring->ring_mem;
4007 	rmem->nr_pages = bp->rx_agg_nr_pages;
4008 	rmem->page_size = HW_RXBD_RING_SIZE;
4009 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4010 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4011 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4012 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4013 }
4014 
4015 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4016 				      struct bnxt_rx_ring_info *rxr)
4017 {
4018 	struct bnxt_ring_mem_info *rmem;
4019 	struct bnxt_ring_struct *ring;
4020 	int i;
4021 
4022 	rxr->page_pool->p.napi = NULL;
4023 	rxr->page_pool = NULL;
4024 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4025 
4026 	ring = &rxr->rx_ring_struct;
4027 	rmem = &ring->ring_mem;
4028 	rmem->pg_tbl = NULL;
4029 	rmem->pg_tbl_map = 0;
4030 	for (i = 0; i < rmem->nr_pages; i++) {
4031 		rmem->pg_arr[i] = NULL;
4032 		rmem->dma_arr[i] = 0;
4033 	}
4034 	*rmem->vmem = NULL;
4035 
4036 	ring = &rxr->rx_agg_ring_struct;
4037 	rmem = &ring->ring_mem;
4038 	rmem->pg_tbl = NULL;
4039 	rmem->pg_tbl_map = 0;
4040 	for (i = 0; i < rmem->nr_pages; i++) {
4041 		rmem->pg_arr[i] = NULL;
4042 		rmem->dma_arr[i] = 0;
4043 	}
4044 	*rmem->vmem = NULL;
4045 }
4046 
4047 static void bnxt_init_ring_struct(struct bnxt *bp)
4048 {
4049 	int i, j;
4050 
4051 	for (i = 0; i < bp->cp_nr_rings; i++) {
4052 		struct bnxt_napi *bnapi = bp->bnapi[i];
4053 		struct bnxt_ring_mem_info *rmem;
4054 		struct bnxt_cp_ring_info *cpr;
4055 		struct bnxt_rx_ring_info *rxr;
4056 		struct bnxt_tx_ring_info *txr;
4057 		struct bnxt_ring_struct *ring;
4058 
4059 		if (!bnapi)
4060 			continue;
4061 
4062 		cpr = &bnapi->cp_ring;
4063 		ring = &cpr->cp_ring_struct;
4064 		rmem = &ring->ring_mem;
4065 		rmem->nr_pages = bp->cp_nr_pages;
4066 		rmem->page_size = HW_CMPD_RING_SIZE;
4067 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4068 		rmem->dma_arr = cpr->cp_desc_mapping;
4069 		rmem->vmem_size = 0;
4070 
4071 		rxr = bnapi->rx_ring;
4072 		if (!rxr)
4073 			goto skip_rx;
4074 
4075 		ring = &rxr->rx_ring_struct;
4076 		rmem = &ring->ring_mem;
4077 		rmem->nr_pages = bp->rx_nr_pages;
4078 		rmem->page_size = HW_RXBD_RING_SIZE;
4079 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4080 		rmem->dma_arr = rxr->rx_desc_mapping;
4081 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4082 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4083 
4084 		ring = &rxr->rx_agg_ring_struct;
4085 		rmem = &ring->ring_mem;
4086 		rmem->nr_pages = bp->rx_agg_nr_pages;
4087 		rmem->page_size = HW_RXBD_RING_SIZE;
4088 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4089 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4090 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4091 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4092 
4093 skip_rx:
4094 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4095 			ring = &txr->tx_ring_struct;
4096 			rmem = &ring->ring_mem;
4097 			rmem->nr_pages = bp->tx_nr_pages;
4098 			rmem->page_size = HW_TXBD_RING_SIZE;
4099 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4100 			rmem->dma_arr = txr->tx_desc_mapping;
4101 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4102 			rmem->vmem = (void **)&txr->tx_buf_ring;
4103 		}
4104 	}
4105 }
4106 
4107 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4108 {
4109 	int i;
4110 	u32 prod;
4111 	struct rx_bd **rx_buf_ring;
4112 
4113 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4114 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4115 		int j;
4116 		struct rx_bd *rxbd;
4117 
4118 		rxbd = rx_buf_ring[i];
4119 		if (!rxbd)
4120 			continue;
4121 
4122 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4123 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4124 			rxbd->rx_bd_opaque = prod;
4125 		}
4126 	}
4127 }
4128 
4129 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4130 				       struct bnxt_rx_ring_info *rxr,
4131 				       int ring_nr)
4132 {
4133 	u32 prod;
4134 	int i;
4135 
4136 	prod = rxr->rx_prod;
4137 	for (i = 0; i < bp->rx_ring_size; i++) {
4138 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4139 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4140 				    ring_nr, i, bp->rx_ring_size);
4141 			break;
4142 		}
4143 		prod = NEXT_RX(prod);
4144 	}
4145 	rxr->rx_prod = prod;
4146 }
4147 
4148 static void bnxt_alloc_one_rx_ring_page(struct bnxt *bp,
4149 					struct bnxt_rx_ring_info *rxr,
4150 					int ring_nr)
4151 {
4152 	u32 prod;
4153 	int i;
4154 
4155 	prod = rxr->rx_agg_prod;
4156 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4157 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
4158 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4159 				    ring_nr, i, bp->rx_ring_size);
4160 			break;
4161 		}
4162 		prod = NEXT_RX_AGG(prod);
4163 	}
4164 	rxr->rx_agg_prod = prod;
4165 }
4166 
4167 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4168 {
4169 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4170 	int i;
4171 
4172 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4173 
4174 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4175 		return 0;
4176 
4177 	bnxt_alloc_one_rx_ring_page(bp, rxr, ring_nr);
4178 
4179 	if (rxr->rx_tpa) {
4180 		dma_addr_t mapping;
4181 		u8 *data;
4182 
4183 		for (i = 0; i < bp->max_tpa; i++) {
4184 			data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4185 						    GFP_KERNEL);
4186 			if (!data)
4187 				return -ENOMEM;
4188 
4189 			rxr->rx_tpa[i].data = data;
4190 			rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4191 			rxr->rx_tpa[i].mapping = mapping;
4192 		}
4193 	}
4194 	return 0;
4195 }
4196 
4197 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4198 				       struct bnxt_rx_ring_info *rxr)
4199 {
4200 	struct bnxt_ring_struct *ring;
4201 	u32 type;
4202 
4203 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4204 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4205 
4206 	if (NET_IP_ALIGN == 2)
4207 		type |= RX_BD_FLAGS_SOP;
4208 
4209 	ring = &rxr->rx_ring_struct;
4210 	bnxt_init_rxbd_pages(ring, type);
4211 	ring->fw_ring_id = INVALID_HW_RING_ID;
4212 }
4213 
4214 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4215 					   struct bnxt_rx_ring_info *rxr)
4216 {
4217 	struct bnxt_ring_struct *ring;
4218 	u32 type;
4219 
4220 	ring = &rxr->rx_agg_ring_struct;
4221 	ring->fw_ring_id = INVALID_HW_RING_ID;
4222 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4223 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4224 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4225 
4226 		bnxt_init_rxbd_pages(ring, type);
4227 	}
4228 }
4229 
4230 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4231 {
4232 	struct bnxt_rx_ring_info *rxr;
4233 
4234 	rxr = &bp->rx_ring[ring_nr];
4235 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4236 
4237 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4238 			     &rxr->bnapi->napi);
4239 
4240 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4241 		bpf_prog_add(bp->xdp_prog, 1);
4242 		rxr->xdp_prog = bp->xdp_prog;
4243 	}
4244 
4245 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4246 
4247 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4248 }
4249 
4250 static void bnxt_init_cp_rings(struct bnxt *bp)
4251 {
4252 	int i, j;
4253 
4254 	for (i = 0; i < bp->cp_nr_rings; i++) {
4255 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4256 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4257 
4258 		ring->fw_ring_id = INVALID_HW_RING_ID;
4259 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4260 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4261 		if (!cpr->cp_ring_arr)
4262 			continue;
4263 		for (j = 0; j < cpr->cp_ring_count; j++) {
4264 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4265 
4266 			ring = &cpr2->cp_ring_struct;
4267 			ring->fw_ring_id = INVALID_HW_RING_ID;
4268 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4269 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4270 		}
4271 	}
4272 }
4273 
4274 static int bnxt_init_rx_rings(struct bnxt *bp)
4275 {
4276 	int i, rc = 0;
4277 
4278 	if (BNXT_RX_PAGE_MODE(bp)) {
4279 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4280 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4281 	} else {
4282 		bp->rx_offset = BNXT_RX_OFFSET;
4283 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4284 	}
4285 
4286 	for (i = 0; i < bp->rx_nr_rings; i++) {
4287 		rc = bnxt_init_one_rx_ring(bp, i);
4288 		if (rc)
4289 			break;
4290 	}
4291 
4292 	return rc;
4293 }
4294 
4295 static int bnxt_init_tx_rings(struct bnxt *bp)
4296 {
4297 	u16 i;
4298 
4299 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4300 				   BNXT_MIN_TX_DESC_CNT);
4301 
4302 	for (i = 0; i < bp->tx_nr_rings; i++) {
4303 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4304 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4305 
4306 		ring->fw_ring_id = INVALID_HW_RING_ID;
4307 
4308 		if (i >= bp->tx_nr_rings_xdp)
4309 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4310 					     NETDEV_QUEUE_TYPE_TX,
4311 					     &txr->bnapi->napi);
4312 	}
4313 
4314 	return 0;
4315 }
4316 
4317 static void bnxt_free_ring_grps(struct bnxt *bp)
4318 {
4319 	kfree(bp->grp_info);
4320 	bp->grp_info = NULL;
4321 }
4322 
4323 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4324 {
4325 	int i;
4326 
4327 	if (irq_re_init) {
4328 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4329 				       sizeof(struct bnxt_ring_grp_info),
4330 				       GFP_KERNEL);
4331 		if (!bp->grp_info)
4332 			return -ENOMEM;
4333 	}
4334 	for (i = 0; i < bp->cp_nr_rings; i++) {
4335 		if (irq_re_init)
4336 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4337 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4338 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4339 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4340 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4341 	}
4342 	return 0;
4343 }
4344 
4345 static void bnxt_free_vnics(struct bnxt *bp)
4346 {
4347 	kfree(bp->vnic_info);
4348 	bp->vnic_info = NULL;
4349 	bp->nr_vnics = 0;
4350 }
4351 
4352 static int bnxt_alloc_vnics(struct bnxt *bp)
4353 {
4354 	int num_vnics = 1;
4355 
4356 #ifdef CONFIG_RFS_ACCEL
4357 	if (bp->flags & BNXT_FLAG_RFS) {
4358 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4359 			num_vnics++;
4360 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4361 			num_vnics += bp->rx_nr_rings;
4362 	}
4363 #endif
4364 
4365 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4366 		num_vnics++;
4367 
4368 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4369 				GFP_KERNEL);
4370 	if (!bp->vnic_info)
4371 		return -ENOMEM;
4372 
4373 	bp->nr_vnics = num_vnics;
4374 	return 0;
4375 }
4376 
4377 static void bnxt_init_vnics(struct bnxt *bp)
4378 {
4379 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4380 	int i;
4381 
4382 	for (i = 0; i < bp->nr_vnics; i++) {
4383 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4384 		int j;
4385 
4386 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4387 		vnic->vnic_id = i;
4388 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4389 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4390 
4391 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4392 
4393 		if (bp->vnic_info[i].rss_hash_key) {
4394 			if (i == BNXT_VNIC_DEFAULT) {
4395 				u8 *key = (void *)vnic->rss_hash_key;
4396 				int k;
4397 
4398 				if (!bp->rss_hash_key_valid &&
4399 				    !bp->rss_hash_key_updated) {
4400 					get_random_bytes(bp->rss_hash_key,
4401 							 HW_HASH_KEY_SIZE);
4402 					bp->rss_hash_key_updated = true;
4403 				}
4404 
4405 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4406 				       HW_HASH_KEY_SIZE);
4407 
4408 				if (!bp->rss_hash_key_updated)
4409 					continue;
4410 
4411 				bp->rss_hash_key_updated = false;
4412 				bp->rss_hash_key_valid = true;
4413 
4414 				bp->toeplitz_prefix = 0;
4415 				for (k = 0; k < 8; k++) {
4416 					bp->toeplitz_prefix <<= 8;
4417 					bp->toeplitz_prefix |= key[k];
4418 				}
4419 			} else {
4420 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4421 				       HW_HASH_KEY_SIZE);
4422 			}
4423 		}
4424 	}
4425 }
4426 
4427 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4428 {
4429 	int pages;
4430 
4431 	pages = ring_size / desc_per_pg;
4432 
4433 	if (!pages)
4434 		return 1;
4435 
4436 	pages++;
4437 
4438 	while (pages & (pages - 1))
4439 		pages++;
4440 
4441 	return pages;
4442 }
4443 
4444 void bnxt_set_tpa_flags(struct bnxt *bp)
4445 {
4446 	bp->flags &= ~BNXT_FLAG_TPA;
4447 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4448 		return;
4449 	if (bp->dev->features & NETIF_F_LRO)
4450 		bp->flags |= BNXT_FLAG_LRO;
4451 	else if (bp->dev->features & NETIF_F_GRO_HW)
4452 		bp->flags |= BNXT_FLAG_GRO;
4453 }
4454 
4455 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4456  * be set on entry.
4457  */
4458 void bnxt_set_ring_params(struct bnxt *bp)
4459 {
4460 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4461 	u32 agg_factor = 0, agg_ring_size = 0;
4462 
4463 	/* 8 for CRC and VLAN */
4464 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4465 
4466 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4467 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4468 
4469 	bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
4470 	ring_size = bp->rx_ring_size;
4471 	bp->rx_agg_ring_size = 0;
4472 	bp->rx_agg_nr_pages = 0;
4473 
4474 	if (bp->flags & BNXT_FLAG_TPA)
4475 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4476 
4477 	bp->flags &= ~BNXT_FLAG_JUMBO;
4478 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4479 		u32 jumbo_factor;
4480 
4481 		bp->flags |= BNXT_FLAG_JUMBO;
4482 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4483 		if (jumbo_factor > agg_factor)
4484 			agg_factor = jumbo_factor;
4485 	}
4486 	if (agg_factor) {
4487 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4488 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4489 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4490 				    bp->rx_ring_size, ring_size);
4491 			bp->rx_ring_size = ring_size;
4492 		}
4493 		agg_ring_size = ring_size * agg_factor;
4494 
4495 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4496 							RX_DESC_CNT);
4497 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4498 			u32 tmp = agg_ring_size;
4499 
4500 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4501 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4502 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4503 				    tmp, agg_ring_size);
4504 		}
4505 		bp->rx_agg_ring_size = agg_ring_size;
4506 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4507 
4508 		if (BNXT_RX_PAGE_MODE(bp)) {
4509 			rx_space = PAGE_SIZE;
4510 			rx_size = PAGE_SIZE -
4511 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4512 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4513 		} else {
4514 			rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
4515 			rx_space = rx_size + NET_SKB_PAD +
4516 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4517 		}
4518 	}
4519 
4520 	bp->rx_buf_use_size = rx_size;
4521 	bp->rx_buf_size = rx_space;
4522 
4523 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4524 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4525 
4526 	ring_size = bp->tx_ring_size;
4527 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4528 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4529 
4530 	max_rx_cmpl = bp->rx_ring_size;
4531 	/* MAX TPA needs to be added because TPA_START completions are
4532 	 * immediately recycled, so the TPA completions are not bound by
4533 	 * the RX ring size.
4534 	 */
4535 	if (bp->flags & BNXT_FLAG_TPA)
4536 		max_rx_cmpl += bp->max_tpa;
4537 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4538 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4539 	bp->cp_ring_size = ring_size;
4540 
4541 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4542 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4543 		bp->cp_nr_pages = MAX_CP_PAGES;
4544 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4545 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4546 			    ring_size, bp->cp_ring_size);
4547 	}
4548 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4549 	bp->cp_ring_mask = bp->cp_bit - 1;
4550 }
4551 
4552 /* Changing allocation mode of RX rings.
4553  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4554  */
4555 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4556 {
4557 	struct net_device *dev = bp->dev;
4558 
4559 	if (page_mode) {
4560 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
4561 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4562 
4563 		if (bp->xdp_prog->aux->xdp_has_frags)
4564 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4565 		else
4566 			dev->max_mtu =
4567 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4568 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4569 			bp->flags |= BNXT_FLAG_JUMBO;
4570 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4571 		} else {
4572 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4573 			bp->rx_skb_func = bnxt_rx_page_skb;
4574 		}
4575 		bp->rx_dir = DMA_BIDIRECTIONAL;
4576 		/* Disable LRO or GRO_HW */
4577 		netdev_update_features(dev);
4578 	} else {
4579 		dev->max_mtu = bp->max_mtu;
4580 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4581 		bp->rx_dir = DMA_FROM_DEVICE;
4582 		bp->rx_skb_func = bnxt_rx_skb;
4583 	}
4584 	return 0;
4585 }
4586 
4587 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4588 {
4589 	int i;
4590 	struct bnxt_vnic_info *vnic;
4591 	struct pci_dev *pdev = bp->pdev;
4592 
4593 	if (!bp->vnic_info)
4594 		return;
4595 
4596 	for (i = 0; i < bp->nr_vnics; i++) {
4597 		vnic = &bp->vnic_info[i];
4598 
4599 		kfree(vnic->fw_grp_ids);
4600 		vnic->fw_grp_ids = NULL;
4601 
4602 		kfree(vnic->uc_list);
4603 		vnic->uc_list = NULL;
4604 
4605 		if (vnic->mc_list) {
4606 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4607 					  vnic->mc_list, vnic->mc_list_mapping);
4608 			vnic->mc_list = NULL;
4609 		}
4610 
4611 		if (vnic->rss_table) {
4612 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4613 					  vnic->rss_table,
4614 					  vnic->rss_table_dma_addr);
4615 			vnic->rss_table = NULL;
4616 		}
4617 
4618 		vnic->rss_hash_key = NULL;
4619 		vnic->flags = 0;
4620 	}
4621 }
4622 
4623 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4624 {
4625 	int i, rc = 0, size;
4626 	struct bnxt_vnic_info *vnic;
4627 	struct pci_dev *pdev = bp->pdev;
4628 	int max_rings;
4629 
4630 	for (i = 0; i < bp->nr_vnics; i++) {
4631 		vnic = &bp->vnic_info[i];
4632 
4633 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4634 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4635 
4636 			if (mem_size > 0) {
4637 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4638 				if (!vnic->uc_list) {
4639 					rc = -ENOMEM;
4640 					goto out;
4641 				}
4642 			}
4643 		}
4644 
4645 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4646 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4647 			vnic->mc_list =
4648 				dma_alloc_coherent(&pdev->dev,
4649 						   vnic->mc_list_size,
4650 						   &vnic->mc_list_mapping,
4651 						   GFP_KERNEL);
4652 			if (!vnic->mc_list) {
4653 				rc = -ENOMEM;
4654 				goto out;
4655 			}
4656 		}
4657 
4658 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4659 			goto vnic_skip_grps;
4660 
4661 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4662 			max_rings = bp->rx_nr_rings;
4663 		else
4664 			max_rings = 1;
4665 
4666 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4667 		if (!vnic->fw_grp_ids) {
4668 			rc = -ENOMEM;
4669 			goto out;
4670 		}
4671 vnic_skip_grps:
4672 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4673 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4674 			continue;
4675 
4676 		/* Allocate rss table and hash key */
4677 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4678 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4679 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4680 
4681 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4682 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4683 						     vnic->rss_table_size,
4684 						     &vnic->rss_table_dma_addr,
4685 						     GFP_KERNEL);
4686 		if (!vnic->rss_table) {
4687 			rc = -ENOMEM;
4688 			goto out;
4689 		}
4690 
4691 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4692 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4693 	}
4694 	return 0;
4695 
4696 out:
4697 	return rc;
4698 }
4699 
4700 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4701 {
4702 	struct bnxt_hwrm_wait_token *token;
4703 
4704 	dma_pool_destroy(bp->hwrm_dma_pool);
4705 	bp->hwrm_dma_pool = NULL;
4706 
4707 	rcu_read_lock();
4708 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4709 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4710 	rcu_read_unlock();
4711 }
4712 
4713 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4714 {
4715 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4716 					    BNXT_HWRM_DMA_SIZE,
4717 					    BNXT_HWRM_DMA_ALIGN, 0);
4718 	if (!bp->hwrm_dma_pool)
4719 		return -ENOMEM;
4720 
4721 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4722 
4723 	return 0;
4724 }
4725 
4726 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4727 {
4728 	kfree(stats->hw_masks);
4729 	stats->hw_masks = NULL;
4730 	kfree(stats->sw_stats);
4731 	stats->sw_stats = NULL;
4732 	if (stats->hw_stats) {
4733 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
4734 				  stats->hw_stats_map);
4735 		stats->hw_stats = NULL;
4736 	}
4737 }
4738 
4739 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
4740 				bool alloc_masks)
4741 {
4742 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
4743 					     &stats->hw_stats_map, GFP_KERNEL);
4744 	if (!stats->hw_stats)
4745 		return -ENOMEM;
4746 
4747 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
4748 	if (!stats->sw_stats)
4749 		goto stats_mem_err;
4750 
4751 	if (alloc_masks) {
4752 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
4753 		if (!stats->hw_masks)
4754 			goto stats_mem_err;
4755 	}
4756 	return 0;
4757 
4758 stats_mem_err:
4759 	bnxt_free_stats_mem(bp, stats);
4760 	return -ENOMEM;
4761 }
4762 
4763 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
4764 {
4765 	int i;
4766 
4767 	for (i = 0; i < count; i++)
4768 		mask_arr[i] = mask;
4769 }
4770 
4771 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
4772 {
4773 	int i;
4774 
4775 	for (i = 0; i < count; i++)
4776 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
4777 }
4778 
4779 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
4780 				    struct bnxt_stats_mem *stats)
4781 {
4782 	struct hwrm_func_qstats_ext_output *resp;
4783 	struct hwrm_func_qstats_ext_input *req;
4784 	__le64 *hw_masks;
4785 	int rc;
4786 
4787 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
4788 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4789 		return -EOPNOTSUPP;
4790 
4791 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
4792 	if (rc)
4793 		return rc;
4794 
4795 	req->fid = cpu_to_le16(0xffff);
4796 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4797 
4798 	resp = hwrm_req_hold(bp, req);
4799 	rc = hwrm_req_send(bp, req);
4800 	if (!rc) {
4801 		hw_masks = &resp->rx_ucast_pkts;
4802 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
4803 	}
4804 	hwrm_req_drop(bp, req);
4805 	return rc;
4806 }
4807 
4808 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
4809 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
4810 
4811 static void bnxt_init_stats(struct bnxt *bp)
4812 {
4813 	struct bnxt_napi *bnapi = bp->bnapi[0];
4814 	struct bnxt_cp_ring_info *cpr;
4815 	struct bnxt_stats_mem *stats;
4816 	__le64 *rx_stats, *tx_stats;
4817 	int rc, rx_count, tx_count;
4818 	u64 *rx_masks, *tx_masks;
4819 	u64 mask;
4820 	u8 flags;
4821 
4822 	cpr = &bnapi->cp_ring;
4823 	stats = &cpr->stats;
4824 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
4825 	if (rc) {
4826 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4827 			mask = (1ULL << 48) - 1;
4828 		else
4829 			mask = -1ULL;
4830 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
4831 	}
4832 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
4833 		stats = &bp->port_stats;
4834 		rx_stats = stats->hw_stats;
4835 		rx_masks = stats->hw_masks;
4836 		rx_count = sizeof(struct rx_port_stats) / 8;
4837 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4838 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4839 		tx_count = sizeof(struct tx_port_stats) / 8;
4840 
4841 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
4842 		rc = bnxt_hwrm_port_qstats(bp, flags);
4843 		if (rc) {
4844 			mask = (1ULL << 40) - 1;
4845 
4846 			bnxt_fill_masks(rx_masks, mask, rx_count);
4847 			bnxt_fill_masks(tx_masks, mask, tx_count);
4848 		} else {
4849 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4850 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
4851 			bnxt_hwrm_port_qstats(bp, 0);
4852 		}
4853 	}
4854 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
4855 		stats = &bp->rx_port_stats_ext;
4856 		rx_stats = stats->hw_stats;
4857 		rx_masks = stats->hw_masks;
4858 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
4859 		stats = &bp->tx_port_stats_ext;
4860 		tx_stats = stats->hw_stats;
4861 		tx_masks = stats->hw_masks;
4862 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
4863 
4864 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4865 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
4866 		if (rc) {
4867 			mask = (1ULL << 40) - 1;
4868 
4869 			bnxt_fill_masks(rx_masks, mask, rx_count);
4870 			if (tx_stats)
4871 				bnxt_fill_masks(tx_masks, mask, tx_count);
4872 		} else {
4873 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4874 			if (tx_stats)
4875 				bnxt_copy_hw_masks(tx_masks, tx_stats,
4876 						   tx_count);
4877 			bnxt_hwrm_port_qstats_ext(bp, 0);
4878 		}
4879 	}
4880 }
4881 
4882 static void bnxt_free_port_stats(struct bnxt *bp)
4883 {
4884 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
4885 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
4886 
4887 	bnxt_free_stats_mem(bp, &bp->port_stats);
4888 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
4889 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
4890 }
4891 
4892 static void bnxt_free_ring_stats(struct bnxt *bp)
4893 {
4894 	int i;
4895 
4896 	if (!bp->bnapi)
4897 		return;
4898 
4899 	for (i = 0; i < bp->cp_nr_rings; i++) {
4900 		struct bnxt_napi *bnapi = bp->bnapi[i];
4901 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4902 
4903 		bnxt_free_stats_mem(bp, &cpr->stats);
4904 
4905 		kfree(cpr->sw_stats);
4906 		cpr->sw_stats = NULL;
4907 	}
4908 }
4909 
4910 static int bnxt_alloc_stats(struct bnxt *bp)
4911 {
4912 	u32 size, i;
4913 	int rc;
4914 
4915 	size = bp->hw_ring_stats_size;
4916 
4917 	for (i = 0; i < bp->cp_nr_rings; i++) {
4918 		struct bnxt_napi *bnapi = bp->bnapi[i];
4919 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4920 
4921 		cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL);
4922 		if (!cpr->sw_stats)
4923 			return -ENOMEM;
4924 
4925 		cpr->stats.len = size;
4926 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
4927 		if (rc)
4928 			return rc;
4929 
4930 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4931 	}
4932 
4933 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
4934 		return 0;
4935 
4936 	if (bp->port_stats.hw_stats)
4937 		goto alloc_ext_stats;
4938 
4939 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
4940 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
4941 	if (rc)
4942 		return rc;
4943 
4944 	bp->flags |= BNXT_FLAG_PORT_STATS;
4945 
4946 alloc_ext_stats:
4947 	/* Display extended statistics only if FW supports it */
4948 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
4949 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
4950 			return 0;
4951 
4952 	if (bp->rx_port_stats_ext.hw_stats)
4953 		goto alloc_tx_ext_stats;
4954 
4955 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
4956 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
4957 	/* Extended stats are optional */
4958 	if (rc)
4959 		return 0;
4960 
4961 alloc_tx_ext_stats:
4962 	if (bp->tx_port_stats_ext.hw_stats)
4963 		return 0;
4964 
4965 	if (bp->hwrm_spec_code >= 0x10902 ||
4966 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
4967 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
4968 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
4969 		/* Extended stats are optional */
4970 		if (rc)
4971 			return 0;
4972 	}
4973 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
4974 	return 0;
4975 }
4976 
4977 static void bnxt_clear_ring_indices(struct bnxt *bp)
4978 {
4979 	int i, j;
4980 
4981 	if (!bp->bnapi)
4982 		return;
4983 
4984 	for (i = 0; i < bp->cp_nr_rings; i++) {
4985 		struct bnxt_napi *bnapi = bp->bnapi[i];
4986 		struct bnxt_cp_ring_info *cpr;
4987 		struct bnxt_rx_ring_info *rxr;
4988 		struct bnxt_tx_ring_info *txr;
4989 
4990 		if (!bnapi)
4991 			continue;
4992 
4993 		cpr = &bnapi->cp_ring;
4994 		cpr->cp_raw_cons = 0;
4995 
4996 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4997 			txr->tx_prod = 0;
4998 			txr->tx_cons = 0;
4999 			txr->tx_hw_cons = 0;
5000 		}
5001 
5002 		rxr = bnapi->rx_ring;
5003 		if (rxr) {
5004 			rxr->rx_prod = 0;
5005 			rxr->rx_agg_prod = 0;
5006 			rxr->rx_sw_agg_prod = 0;
5007 			rxr->rx_next_cons = 0;
5008 		}
5009 		bnapi->events = 0;
5010 	}
5011 }
5012 
5013 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5014 {
5015 	u8 type = fltr->type, flags = fltr->flags;
5016 
5017 	INIT_LIST_HEAD(&fltr->list);
5018 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5019 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5020 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5021 }
5022 
5023 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5024 {
5025 	if (!list_empty(&fltr->list))
5026 		list_del_init(&fltr->list);
5027 }
5028 
5029 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5030 {
5031 	struct bnxt_filter_base *usr_fltr, *tmp;
5032 
5033 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5034 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5035 			continue;
5036 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5037 	}
5038 }
5039 
5040 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5041 {
5042 	hlist_del(&fltr->hash);
5043 	bnxt_del_one_usr_fltr(bp, fltr);
5044 	if (fltr->flags) {
5045 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5046 		bp->ntp_fltr_count--;
5047 	}
5048 	kfree(fltr);
5049 }
5050 
5051 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5052 {
5053 	int i;
5054 
5055 	/* Under rtnl_lock and all our NAPIs have been disabled.  It's
5056 	 * safe to delete the hash table.
5057 	 */
5058 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5059 		struct hlist_head *head;
5060 		struct hlist_node *tmp;
5061 		struct bnxt_ntuple_filter *fltr;
5062 
5063 		head = &bp->ntp_fltr_hash_tbl[i];
5064 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5065 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5066 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5067 				     !list_empty(&fltr->base.list)))
5068 				continue;
5069 			bnxt_del_fltr(bp, &fltr->base);
5070 		}
5071 	}
5072 	if (!all)
5073 		return;
5074 
5075 	bitmap_free(bp->ntp_fltr_bmap);
5076 	bp->ntp_fltr_bmap = NULL;
5077 	bp->ntp_fltr_count = 0;
5078 }
5079 
5080 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5081 {
5082 	int i, rc = 0;
5083 
5084 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5085 		return 0;
5086 
5087 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5088 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5089 
5090 	bp->ntp_fltr_count = 0;
5091 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5092 
5093 	if (!bp->ntp_fltr_bmap)
5094 		rc = -ENOMEM;
5095 
5096 	return rc;
5097 }
5098 
5099 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5100 {
5101 	int i;
5102 
5103 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5104 		struct hlist_head *head;
5105 		struct hlist_node *tmp;
5106 		struct bnxt_l2_filter *fltr;
5107 
5108 		head = &bp->l2_fltr_hash_tbl[i];
5109 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5110 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5111 				     !list_empty(&fltr->base.list)))
5112 				continue;
5113 			bnxt_del_fltr(bp, &fltr->base);
5114 		}
5115 	}
5116 }
5117 
5118 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5119 {
5120 	int i;
5121 
5122 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5123 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5124 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5125 }
5126 
5127 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5128 {
5129 	bnxt_free_vnic_attributes(bp);
5130 	bnxt_free_tx_rings(bp);
5131 	bnxt_free_rx_rings(bp);
5132 	bnxt_free_cp_rings(bp);
5133 	bnxt_free_all_cp_arrays(bp);
5134 	bnxt_free_ntp_fltrs(bp, false);
5135 	bnxt_free_l2_filters(bp, false);
5136 	if (irq_re_init) {
5137 		bnxt_free_ring_stats(bp);
5138 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5139 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5140 			bnxt_free_port_stats(bp);
5141 		bnxt_free_ring_grps(bp);
5142 		bnxt_free_vnics(bp);
5143 		kfree(bp->tx_ring_map);
5144 		bp->tx_ring_map = NULL;
5145 		kfree(bp->tx_ring);
5146 		bp->tx_ring = NULL;
5147 		kfree(bp->rx_ring);
5148 		bp->rx_ring = NULL;
5149 		kfree(bp->bnapi);
5150 		bp->bnapi = NULL;
5151 	} else {
5152 		bnxt_clear_ring_indices(bp);
5153 	}
5154 }
5155 
5156 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5157 {
5158 	int i, j, rc, size, arr_size;
5159 	void *bnapi;
5160 
5161 	if (irq_re_init) {
5162 		/* Allocate bnapi mem pointer array and mem block for
5163 		 * all queues
5164 		 */
5165 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5166 				bp->cp_nr_rings);
5167 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5168 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5169 		if (!bnapi)
5170 			return -ENOMEM;
5171 
5172 		bp->bnapi = bnapi;
5173 		bnapi += arr_size;
5174 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5175 			bp->bnapi[i] = bnapi;
5176 			bp->bnapi[i]->index = i;
5177 			bp->bnapi[i]->bp = bp;
5178 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5179 				struct bnxt_cp_ring_info *cpr =
5180 					&bp->bnapi[i]->cp_ring;
5181 
5182 				cpr->cp_ring_struct.ring_mem.flags =
5183 					BNXT_RMEM_RING_PTE_FLAG;
5184 			}
5185 		}
5186 
5187 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5188 				      sizeof(struct bnxt_rx_ring_info),
5189 				      GFP_KERNEL);
5190 		if (!bp->rx_ring)
5191 			return -ENOMEM;
5192 
5193 		for (i = 0; i < bp->rx_nr_rings; i++) {
5194 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5195 
5196 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5197 				rxr->rx_ring_struct.ring_mem.flags =
5198 					BNXT_RMEM_RING_PTE_FLAG;
5199 				rxr->rx_agg_ring_struct.ring_mem.flags =
5200 					BNXT_RMEM_RING_PTE_FLAG;
5201 			} else {
5202 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5203 			}
5204 			rxr->bnapi = bp->bnapi[i];
5205 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5206 		}
5207 
5208 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5209 				      sizeof(struct bnxt_tx_ring_info),
5210 				      GFP_KERNEL);
5211 		if (!bp->tx_ring)
5212 			return -ENOMEM;
5213 
5214 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5215 					  GFP_KERNEL);
5216 
5217 		if (!bp->tx_ring_map)
5218 			return -ENOMEM;
5219 
5220 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5221 			j = 0;
5222 		else
5223 			j = bp->rx_nr_rings;
5224 
5225 		for (i = 0; i < bp->tx_nr_rings; i++) {
5226 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5227 			struct bnxt_napi *bnapi2;
5228 
5229 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5230 				txr->tx_ring_struct.ring_mem.flags =
5231 					BNXT_RMEM_RING_PTE_FLAG;
5232 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5233 			if (i >= bp->tx_nr_rings_xdp) {
5234 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5235 
5236 				bnapi2 = bp->bnapi[k];
5237 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5238 				txr->tx_napi_idx =
5239 					BNXT_RING_TO_TC(bp, txr->txq_index);
5240 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5241 				bnapi2->tx_int = bnxt_tx_int;
5242 			} else {
5243 				bnapi2 = bp->bnapi[j];
5244 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5245 				bnapi2->tx_ring[0] = txr;
5246 				bnapi2->tx_int = bnxt_tx_int_xdp;
5247 				j++;
5248 			}
5249 			txr->bnapi = bnapi2;
5250 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5251 				txr->tx_cpr = &bnapi2->cp_ring;
5252 		}
5253 
5254 		rc = bnxt_alloc_stats(bp);
5255 		if (rc)
5256 			goto alloc_mem_err;
5257 		bnxt_init_stats(bp);
5258 
5259 		rc = bnxt_alloc_ntp_fltrs(bp);
5260 		if (rc)
5261 			goto alloc_mem_err;
5262 
5263 		rc = bnxt_alloc_vnics(bp);
5264 		if (rc)
5265 			goto alloc_mem_err;
5266 	}
5267 
5268 	rc = bnxt_alloc_all_cp_arrays(bp);
5269 	if (rc)
5270 		goto alloc_mem_err;
5271 
5272 	bnxt_init_ring_struct(bp);
5273 
5274 	rc = bnxt_alloc_rx_rings(bp);
5275 	if (rc)
5276 		goto alloc_mem_err;
5277 
5278 	rc = bnxt_alloc_tx_rings(bp);
5279 	if (rc)
5280 		goto alloc_mem_err;
5281 
5282 	rc = bnxt_alloc_cp_rings(bp);
5283 	if (rc)
5284 		goto alloc_mem_err;
5285 
5286 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5287 						  BNXT_VNIC_MCAST_FLAG |
5288 						  BNXT_VNIC_UCAST_FLAG;
5289 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5290 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5291 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5292 
5293 	rc = bnxt_alloc_vnic_attributes(bp);
5294 	if (rc)
5295 		goto alloc_mem_err;
5296 	return 0;
5297 
5298 alloc_mem_err:
5299 	bnxt_free_mem(bp, true);
5300 	return rc;
5301 }
5302 
5303 static void bnxt_disable_int(struct bnxt *bp)
5304 {
5305 	int i;
5306 
5307 	if (!bp->bnapi)
5308 		return;
5309 
5310 	for (i = 0; i < bp->cp_nr_rings; i++) {
5311 		struct bnxt_napi *bnapi = bp->bnapi[i];
5312 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5313 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5314 
5315 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5316 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5317 	}
5318 }
5319 
5320 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5321 {
5322 	struct bnxt_napi *bnapi = bp->bnapi[n];
5323 	struct bnxt_cp_ring_info *cpr;
5324 
5325 	cpr = &bnapi->cp_ring;
5326 	return cpr->cp_ring_struct.map_idx;
5327 }
5328 
5329 static void bnxt_disable_int_sync(struct bnxt *bp)
5330 {
5331 	int i;
5332 
5333 	if (!bp->irq_tbl)
5334 		return;
5335 
5336 	atomic_inc(&bp->intr_sem);
5337 
5338 	bnxt_disable_int(bp);
5339 	for (i = 0; i < bp->cp_nr_rings; i++) {
5340 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5341 
5342 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5343 	}
5344 }
5345 
5346 static void bnxt_enable_int(struct bnxt *bp)
5347 {
5348 	int i;
5349 
5350 	atomic_set(&bp->intr_sem, 0);
5351 	for (i = 0; i < bp->cp_nr_rings; i++) {
5352 		struct bnxt_napi *bnapi = bp->bnapi[i];
5353 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5354 
5355 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5356 	}
5357 }
5358 
5359 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5360 			    bool async_only)
5361 {
5362 	DECLARE_BITMAP(async_events_bmap, 256);
5363 	u32 *events = (u32 *)async_events_bmap;
5364 	struct hwrm_func_drv_rgtr_output *resp;
5365 	struct hwrm_func_drv_rgtr_input *req;
5366 	u32 flags;
5367 	int rc, i;
5368 
5369 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5370 	if (rc)
5371 		return rc;
5372 
5373 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5374 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5375 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5376 
5377 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5378 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5379 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5380 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5381 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5382 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5383 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5384 	req->flags = cpu_to_le32(flags);
5385 	req->ver_maj_8b = DRV_VER_MAJ;
5386 	req->ver_min_8b = DRV_VER_MIN;
5387 	req->ver_upd_8b = DRV_VER_UPD;
5388 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5389 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5390 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5391 
5392 	if (BNXT_PF(bp)) {
5393 		u32 data[8];
5394 		int i;
5395 
5396 		memset(data, 0, sizeof(data));
5397 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5398 			u16 cmd = bnxt_vf_req_snif[i];
5399 			unsigned int bit, idx;
5400 
5401 			idx = cmd / 32;
5402 			bit = cmd % 32;
5403 			data[idx] |= 1 << bit;
5404 		}
5405 
5406 		for (i = 0; i < 8; i++)
5407 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5408 
5409 		req->enables |=
5410 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5411 	}
5412 
5413 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5414 		req->flags |= cpu_to_le32(
5415 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5416 
5417 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5418 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5419 		u16 event_id = bnxt_async_events_arr[i];
5420 
5421 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5422 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5423 			continue;
5424 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5425 		    !bp->ptp_cfg)
5426 			continue;
5427 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5428 	}
5429 	if (bmap && bmap_size) {
5430 		for (i = 0; i < bmap_size; i++) {
5431 			if (test_bit(i, bmap))
5432 				__set_bit(i, async_events_bmap);
5433 		}
5434 	}
5435 	for (i = 0; i < 8; i++)
5436 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5437 
5438 	if (async_only)
5439 		req->enables =
5440 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5441 
5442 	resp = hwrm_req_hold(bp, req);
5443 	rc = hwrm_req_send(bp, req);
5444 	if (!rc) {
5445 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5446 		if (resp->flags &
5447 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5448 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5449 	}
5450 	hwrm_req_drop(bp, req);
5451 	return rc;
5452 }
5453 
5454 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5455 {
5456 	struct hwrm_func_drv_unrgtr_input *req;
5457 	int rc;
5458 
5459 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5460 		return 0;
5461 
5462 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5463 	if (rc)
5464 		return rc;
5465 	return hwrm_req_send(bp, req);
5466 }
5467 
5468 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5469 
5470 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5471 {
5472 	struct hwrm_tunnel_dst_port_free_input *req;
5473 	int rc;
5474 
5475 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5476 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5477 		return 0;
5478 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5479 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5480 		return 0;
5481 
5482 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5483 	if (rc)
5484 		return rc;
5485 
5486 	req->tunnel_type = tunnel_type;
5487 
5488 	switch (tunnel_type) {
5489 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5490 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5491 		bp->vxlan_port = 0;
5492 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5493 		break;
5494 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5495 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5496 		bp->nge_port = 0;
5497 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5498 		break;
5499 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5500 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5501 		bp->vxlan_gpe_port = 0;
5502 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5503 		break;
5504 	default:
5505 		break;
5506 	}
5507 
5508 	rc = hwrm_req_send(bp, req);
5509 	if (rc)
5510 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5511 			   rc);
5512 	if (bp->flags & BNXT_FLAG_TPA)
5513 		bnxt_set_tpa(bp, true);
5514 	return rc;
5515 }
5516 
5517 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5518 					   u8 tunnel_type)
5519 {
5520 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5521 	struct hwrm_tunnel_dst_port_alloc_input *req;
5522 	int rc;
5523 
5524 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5525 	if (rc)
5526 		return rc;
5527 
5528 	req->tunnel_type = tunnel_type;
5529 	req->tunnel_dst_port_val = port;
5530 
5531 	resp = hwrm_req_hold(bp, req);
5532 	rc = hwrm_req_send(bp, req);
5533 	if (rc) {
5534 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5535 			   rc);
5536 		goto err_out;
5537 	}
5538 
5539 	switch (tunnel_type) {
5540 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5541 		bp->vxlan_port = port;
5542 		bp->vxlan_fw_dst_port_id =
5543 			le16_to_cpu(resp->tunnel_dst_port_id);
5544 		break;
5545 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5546 		bp->nge_port = port;
5547 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5548 		break;
5549 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5550 		bp->vxlan_gpe_port = port;
5551 		bp->vxlan_gpe_fw_dst_port_id =
5552 			le16_to_cpu(resp->tunnel_dst_port_id);
5553 		break;
5554 	default:
5555 		break;
5556 	}
5557 	if (bp->flags & BNXT_FLAG_TPA)
5558 		bnxt_set_tpa(bp, true);
5559 
5560 err_out:
5561 	hwrm_req_drop(bp, req);
5562 	return rc;
5563 }
5564 
5565 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5566 {
5567 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5568 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5569 	int rc;
5570 
5571 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5572 	if (rc)
5573 		return rc;
5574 
5575 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5576 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5577 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5578 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5579 	}
5580 	req->mask = cpu_to_le32(vnic->rx_mask);
5581 	return hwrm_req_send_silent(bp, req);
5582 }
5583 
5584 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5585 {
5586 	if (!atomic_dec_and_test(&fltr->refcnt))
5587 		return;
5588 	spin_lock_bh(&bp->ntp_fltr_lock);
5589 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5590 		spin_unlock_bh(&bp->ntp_fltr_lock);
5591 		return;
5592 	}
5593 	hlist_del_rcu(&fltr->base.hash);
5594 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5595 	if (fltr->base.flags) {
5596 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5597 		bp->ntp_fltr_count--;
5598 	}
5599 	spin_unlock_bh(&bp->ntp_fltr_lock);
5600 	kfree_rcu(fltr, base.rcu);
5601 }
5602 
5603 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5604 						      struct bnxt_l2_key *key,
5605 						      u32 idx)
5606 {
5607 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5608 	struct bnxt_l2_filter *fltr;
5609 
5610 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5611 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5612 
5613 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5614 		    l2_key->vlan == key->vlan)
5615 			return fltr;
5616 	}
5617 	return NULL;
5618 }
5619 
5620 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5621 						    struct bnxt_l2_key *key,
5622 						    u32 idx)
5623 {
5624 	struct bnxt_l2_filter *fltr = NULL;
5625 
5626 	rcu_read_lock();
5627 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5628 	if (fltr)
5629 		atomic_inc(&fltr->refcnt);
5630 	rcu_read_unlock();
5631 	return fltr;
5632 }
5633 
5634 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5635 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5636 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5637 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5638 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5639 
5640 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5641 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5642 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5643 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5644 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5645 
5646 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5647 {
5648 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5649 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5650 			return sizeof(fkeys->addrs.v4addrs) +
5651 			       sizeof(fkeys->ports);
5652 
5653 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5654 			return sizeof(fkeys->addrs.v4addrs);
5655 	}
5656 
5657 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5658 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5659 			return sizeof(fkeys->addrs.v6addrs) +
5660 			       sizeof(fkeys->ports);
5661 
5662 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5663 			return sizeof(fkeys->addrs.v6addrs);
5664 	}
5665 
5666 	return 0;
5667 }
5668 
5669 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5670 			 const unsigned char *key)
5671 {
5672 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5673 	struct bnxt_ipv4_tuple tuple4;
5674 	struct bnxt_ipv6_tuple tuple6;
5675 	int i, j, len = 0;
5676 	u8 *four_tuple;
5677 
5678 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5679 	if (!len)
5680 		return 0;
5681 
5682 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5683 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5684 		tuple4.ports = fkeys->ports;
5685 		four_tuple = (unsigned char *)&tuple4;
5686 	} else {
5687 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5688 		tuple6.ports = fkeys->ports;
5689 		four_tuple = (unsigned char *)&tuple6;
5690 	}
5691 
5692 	for (i = 0, j = 8; i < len; i++, j++) {
5693 		u8 byte = four_tuple[i];
5694 		int bit;
5695 
5696 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5697 			if (byte & 0x80)
5698 				hash ^= prefix;
5699 		}
5700 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5701 	}
5702 
5703 	/* The valid part of the hash is in the upper 32 bits. */
5704 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5705 }
5706 
5707 #ifdef CONFIG_RFS_ACCEL
5708 static struct bnxt_l2_filter *
5709 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5710 {
5711 	struct bnxt_l2_filter *fltr;
5712 	u32 idx;
5713 
5714 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5715 	      BNXT_L2_FLTR_HASH_MASK;
5716 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5717 	return fltr;
5718 }
5719 #endif
5720 
5721 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
5722 			       struct bnxt_l2_key *key, u32 idx)
5723 {
5724 	struct hlist_head *head;
5725 
5726 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
5727 	fltr->l2_key.vlan = key->vlan;
5728 	fltr->base.type = BNXT_FLTR_TYPE_L2;
5729 	if (fltr->base.flags) {
5730 		int bit_id;
5731 
5732 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
5733 						 bp->max_fltr, 0);
5734 		if (bit_id < 0)
5735 			return -ENOMEM;
5736 		fltr->base.sw_id = (u16)bit_id;
5737 		bp->ntp_fltr_count++;
5738 	}
5739 	head = &bp->l2_fltr_hash_tbl[idx];
5740 	hlist_add_head_rcu(&fltr->base.hash, head);
5741 	bnxt_insert_usr_fltr(bp, &fltr->base);
5742 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
5743 	atomic_set(&fltr->refcnt, 1);
5744 	return 0;
5745 }
5746 
5747 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
5748 						   struct bnxt_l2_key *key,
5749 						   gfp_t gfp)
5750 {
5751 	struct bnxt_l2_filter *fltr;
5752 	u32 idx;
5753 	int rc;
5754 
5755 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5756 	      BNXT_L2_FLTR_HASH_MASK;
5757 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5758 	if (fltr)
5759 		return fltr;
5760 
5761 	fltr = kzalloc(sizeof(*fltr), gfp);
5762 	if (!fltr)
5763 		return ERR_PTR(-ENOMEM);
5764 	spin_lock_bh(&bp->ntp_fltr_lock);
5765 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5766 	spin_unlock_bh(&bp->ntp_fltr_lock);
5767 	if (rc) {
5768 		bnxt_del_l2_filter(bp, fltr);
5769 		fltr = ERR_PTR(rc);
5770 	}
5771 	return fltr;
5772 }
5773 
5774 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
5775 						struct bnxt_l2_key *key,
5776 						u16 flags)
5777 {
5778 	struct bnxt_l2_filter *fltr;
5779 	u32 idx;
5780 	int rc;
5781 
5782 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5783 	      BNXT_L2_FLTR_HASH_MASK;
5784 	spin_lock_bh(&bp->ntp_fltr_lock);
5785 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5786 	if (fltr) {
5787 		fltr = ERR_PTR(-EEXIST);
5788 		goto l2_filter_exit;
5789 	}
5790 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
5791 	if (!fltr) {
5792 		fltr = ERR_PTR(-ENOMEM);
5793 		goto l2_filter_exit;
5794 	}
5795 	fltr->base.flags = flags;
5796 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5797 	if (rc) {
5798 		spin_unlock_bh(&bp->ntp_fltr_lock);
5799 		bnxt_del_l2_filter(bp, fltr);
5800 		return ERR_PTR(rc);
5801 	}
5802 
5803 l2_filter_exit:
5804 	spin_unlock_bh(&bp->ntp_fltr_lock);
5805 	return fltr;
5806 }
5807 
5808 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
5809 {
5810 #ifdef CONFIG_BNXT_SRIOV
5811 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
5812 
5813 	return vf->fw_fid;
5814 #else
5815 	return INVALID_HW_RING_ID;
5816 #endif
5817 }
5818 
5819 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5820 {
5821 	struct hwrm_cfa_l2_filter_free_input *req;
5822 	u16 target_id = 0xffff;
5823 	int rc;
5824 
5825 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
5826 		struct bnxt_pf_info *pf = &bp->pf;
5827 
5828 		if (fltr->base.vf_idx >= pf->active_vfs)
5829 			return -EINVAL;
5830 
5831 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
5832 		if (target_id == INVALID_HW_RING_ID)
5833 			return -EINVAL;
5834 	}
5835 
5836 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
5837 	if (rc)
5838 		return rc;
5839 
5840 	req->target_id = cpu_to_le16(target_id);
5841 	req->l2_filter_id = fltr->base.filter_id;
5842 	return hwrm_req_send(bp, req);
5843 }
5844 
5845 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5846 {
5847 	struct hwrm_cfa_l2_filter_alloc_output *resp;
5848 	struct hwrm_cfa_l2_filter_alloc_input *req;
5849 	u16 target_id = 0xffff;
5850 	int rc;
5851 
5852 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
5853 		struct bnxt_pf_info *pf = &bp->pf;
5854 
5855 		if (fltr->base.vf_idx >= pf->active_vfs)
5856 			return -EINVAL;
5857 
5858 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
5859 	}
5860 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
5861 	if (rc)
5862 		return rc;
5863 
5864 	req->target_id = cpu_to_le16(target_id);
5865 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
5866 
5867 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
5868 		req->flags |=
5869 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
5870 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
5871 	req->enables =
5872 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
5873 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
5874 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
5875 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
5876 	eth_broadcast_addr(req->l2_addr_mask);
5877 
5878 	if (fltr->l2_key.vlan) {
5879 		req->enables |=
5880 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
5881 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
5882 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
5883 		req->num_vlans = 1;
5884 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
5885 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
5886 	}
5887 
5888 	resp = hwrm_req_hold(bp, req);
5889 	rc = hwrm_req_send(bp, req);
5890 	if (!rc) {
5891 		fltr->base.filter_id = resp->l2_filter_id;
5892 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
5893 	}
5894 	hwrm_req_drop(bp, req);
5895 	return rc;
5896 }
5897 
5898 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
5899 				     struct bnxt_ntuple_filter *fltr)
5900 {
5901 	struct hwrm_cfa_ntuple_filter_free_input *req;
5902 	int rc;
5903 
5904 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
5905 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
5906 	if (rc)
5907 		return rc;
5908 
5909 	req->ntuple_filter_id = fltr->base.filter_id;
5910 	return hwrm_req_send(bp, req);
5911 }
5912 
5913 #define BNXT_NTP_FLTR_FLAGS					\
5914 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
5915 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
5916 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
5917 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
5918 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
5919 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
5920 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
5921 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
5922 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
5923 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
5924 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
5925 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
5926 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
5927 
5928 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
5929 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
5930 
5931 void bnxt_fill_ipv6_mask(__be32 mask[4])
5932 {
5933 	int i;
5934 
5935 	for (i = 0; i < 4; i++)
5936 		mask[i] = cpu_to_be32(~0);
5937 }
5938 
5939 static void
5940 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
5941 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
5942 			  struct bnxt_ntuple_filter *fltr)
5943 {
5944 	u16 rxq = fltr->base.rxq;
5945 
5946 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
5947 		struct ethtool_rxfh_context *ctx;
5948 		struct bnxt_rss_ctx *rss_ctx;
5949 		struct bnxt_vnic_info *vnic;
5950 
5951 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
5952 			      fltr->base.fw_vnic_id);
5953 		if (ctx) {
5954 			rss_ctx = ethtool_rxfh_context_priv(ctx);
5955 			vnic = &rss_ctx->vnic;
5956 
5957 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
5958 		}
5959 		return;
5960 	}
5961 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
5962 		struct bnxt_vnic_info *vnic;
5963 		u32 enables;
5964 
5965 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
5966 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
5967 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
5968 		req->enables |= cpu_to_le32(enables);
5969 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
5970 	} else {
5971 		u32 flags;
5972 
5973 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
5974 		req->flags |= cpu_to_le32(flags);
5975 		req->dst_id = cpu_to_le16(rxq);
5976 	}
5977 }
5978 
5979 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
5980 				      struct bnxt_ntuple_filter *fltr)
5981 {
5982 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
5983 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
5984 	struct bnxt_flow_masks *masks = &fltr->fmasks;
5985 	struct flow_keys *keys = &fltr->fkeys;
5986 	struct bnxt_l2_filter *l2_fltr;
5987 	struct bnxt_vnic_info *vnic;
5988 	int rc;
5989 
5990 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
5991 	if (rc)
5992 		return rc;
5993 
5994 	l2_fltr = fltr->l2_fltr;
5995 	req->l2_filter_id = l2_fltr->base.filter_id;
5996 
5997 	if (fltr->base.flags & BNXT_ACT_DROP) {
5998 		req->flags =
5999 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6000 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6001 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6002 	} else {
6003 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6004 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6005 	}
6006 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6007 
6008 	req->ethertype = htons(ETH_P_IP);
6009 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6010 	req->ip_protocol = keys->basic.ip_proto;
6011 
6012 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6013 		req->ethertype = htons(ETH_P_IPV6);
6014 		req->ip_addr_type =
6015 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6016 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6017 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6018 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6019 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6020 	} else {
6021 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6022 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6023 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6024 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6025 	}
6026 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6027 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6028 		req->tunnel_type =
6029 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6030 	}
6031 
6032 	req->src_port = keys->ports.src;
6033 	req->src_port_mask = masks->ports.src;
6034 	req->dst_port = keys->ports.dst;
6035 	req->dst_port_mask = masks->ports.dst;
6036 
6037 	resp = hwrm_req_hold(bp, req);
6038 	rc = hwrm_req_send(bp, req);
6039 	if (!rc)
6040 		fltr->base.filter_id = resp->ntuple_filter_id;
6041 	hwrm_req_drop(bp, req);
6042 	return rc;
6043 }
6044 
6045 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6046 				     const u8 *mac_addr)
6047 {
6048 	struct bnxt_l2_filter *fltr;
6049 	struct bnxt_l2_key key;
6050 	int rc;
6051 
6052 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6053 	key.vlan = 0;
6054 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6055 	if (IS_ERR(fltr))
6056 		return PTR_ERR(fltr);
6057 
6058 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6059 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6060 	if (rc)
6061 		bnxt_del_l2_filter(bp, fltr);
6062 	else
6063 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6064 	return rc;
6065 }
6066 
6067 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6068 {
6069 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6070 
6071 	/* Any associated ntuple filters will also be cleared by firmware. */
6072 	for (i = 0; i < num_of_vnics; i++) {
6073 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6074 
6075 		for (j = 0; j < vnic->uc_filter_count; j++) {
6076 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6077 
6078 			bnxt_hwrm_l2_filter_free(bp, fltr);
6079 			bnxt_del_l2_filter(bp, fltr);
6080 		}
6081 		vnic->uc_filter_count = 0;
6082 	}
6083 }
6084 
6085 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6086 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6087 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6088 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6089 
6090 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6091 					   struct hwrm_vnic_tpa_cfg_input *req)
6092 {
6093 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6094 
6095 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6096 		return;
6097 
6098 	if (bp->vxlan_port)
6099 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6100 	if (bp->vxlan_gpe_port)
6101 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6102 	if (bp->nge_port)
6103 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6104 
6105 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6106 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6107 }
6108 
6109 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6110 			   u32 tpa_flags)
6111 {
6112 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6113 	struct hwrm_vnic_tpa_cfg_input *req;
6114 	int rc;
6115 
6116 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6117 		return 0;
6118 
6119 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6120 	if (rc)
6121 		return rc;
6122 
6123 	if (tpa_flags) {
6124 		u16 mss = bp->dev->mtu - 40;
6125 		u32 nsegs, n, segs = 0, flags;
6126 
6127 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6128 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6129 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6130 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6131 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6132 		if (tpa_flags & BNXT_FLAG_GRO)
6133 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6134 
6135 		req->flags = cpu_to_le32(flags);
6136 
6137 		req->enables =
6138 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6139 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6140 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6141 
6142 		/* Number of segs are log2 units, and first packet is not
6143 		 * included as part of this units.
6144 		 */
6145 		if (mss <= BNXT_RX_PAGE_SIZE) {
6146 			n = BNXT_RX_PAGE_SIZE / mss;
6147 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6148 		} else {
6149 			n = mss / BNXT_RX_PAGE_SIZE;
6150 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6151 				n++;
6152 			nsegs = (MAX_SKB_FRAGS - n) / n;
6153 		}
6154 
6155 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6156 			segs = MAX_TPA_SEGS_P5;
6157 			max_aggs = bp->max_tpa;
6158 		} else {
6159 			segs = ilog2(nsegs);
6160 		}
6161 		req->max_agg_segs = cpu_to_le16(segs);
6162 		req->max_aggs = cpu_to_le16(max_aggs);
6163 
6164 		req->min_agg_len = cpu_to_le32(512);
6165 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6166 	}
6167 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6168 
6169 	return hwrm_req_send(bp, req);
6170 }
6171 
6172 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6173 {
6174 	struct bnxt_ring_grp_info *grp_info;
6175 
6176 	grp_info = &bp->grp_info[ring->grp_idx];
6177 	return grp_info->cp_fw_ring_id;
6178 }
6179 
6180 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6181 {
6182 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6183 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6184 	else
6185 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6186 }
6187 
6188 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6189 {
6190 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6191 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6192 	else
6193 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6194 }
6195 
6196 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6197 {
6198 	int entries;
6199 
6200 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6201 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6202 	else
6203 		entries = HW_HASH_INDEX_SIZE;
6204 
6205 	bp->rss_indir_tbl_entries = entries;
6206 	bp->rss_indir_tbl =
6207 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6208 	if (!bp->rss_indir_tbl)
6209 		return -ENOMEM;
6210 
6211 	return 0;
6212 }
6213 
6214 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6215 				 struct ethtool_rxfh_context *rss_ctx)
6216 {
6217 	u16 max_rings, max_entries, pad, i;
6218 	u32 *rss_indir_tbl;
6219 
6220 	if (!bp->rx_nr_rings)
6221 		return;
6222 
6223 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6224 		max_rings = bp->rx_nr_rings - 1;
6225 	else
6226 		max_rings = bp->rx_nr_rings;
6227 
6228 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6229 	if (rss_ctx)
6230 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6231 	else
6232 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6233 
6234 	for (i = 0; i < max_entries; i++)
6235 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6236 
6237 	pad = bp->rss_indir_tbl_entries - max_entries;
6238 	if (pad)
6239 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6240 }
6241 
6242 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6243 {
6244 	u32 i, tbl_size, max_ring = 0;
6245 
6246 	if (!bp->rss_indir_tbl)
6247 		return 0;
6248 
6249 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6250 	for (i = 0; i < tbl_size; i++)
6251 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6252 	return max_ring;
6253 }
6254 
6255 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6256 {
6257 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6258 		if (!rx_rings)
6259 			return 0;
6260 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6261 					       BNXT_RSS_TABLE_ENTRIES_P5);
6262 	}
6263 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6264 		return 2;
6265 	return 1;
6266 }
6267 
6268 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6269 {
6270 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6271 	u16 i, j;
6272 
6273 	/* Fill the RSS indirection table with ring group ids */
6274 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6275 		if (!no_rss)
6276 			j = bp->rss_indir_tbl[i];
6277 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6278 	}
6279 }
6280 
6281 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6282 				    struct bnxt_vnic_info *vnic)
6283 {
6284 	__le16 *ring_tbl = vnic->rss_table;
6285 	struct bnxt_rx_ring_info *rxr;
6286 	u16 tbl_size, i;
6287 
6288 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6289 
6290 	for (i = 0; i < tbl_size; i++) {
6291 		u16 ring_id, j;
6292 
6293 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6294 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6295 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6296 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6297 		else
6298 			j = bp->rss_indir_tbl[i];
6299 		rxr = &bp->rx_ring[j];
6300 
6301 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6302 		*ring_tbl++ = cpu_to_le16(ring_id);
6303 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6304 		*ring_tbl++ = cpu_to_le16(ring_id);
6305 	}
6306 }
6307 
6308 static void
6309 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6310 			 struct bnxt_vnic_info *vnic)
6311 {
6312 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6313 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6314 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6315 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6316 	} else {
6317 		bnxt_fill_hw_rss_tbl(bp, vnic);
6318 	}
6319 
6320 	if (bp->rss_hash_delta) {
6321 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6322 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6323 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6324 		else
6325 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6326 	} else {
6327 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6328 	}
6329 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6330 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6331 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6332 }
6333 
6334 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6335 				  bool set_rss)
6336 {
6337 	struct hwrm_vnic_rss_cfg_input *req;
6338 	int rc;
6339 
6340 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6341 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6342 		return 0;
6343 
6344 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6345 	if (rc)
6346 		return rc;
6347 
6348 	if (set_rss)
6349 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6350 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6351 	return hwrm_req_send(bp, req);
6352 }
6353 
6354 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6355 				     struct bnxt_vnic_info *vnic, bool set_rss)
6356 {
6357 	struct hwrm_vnic_rss_cfg_input *req;
6358 	dma_addr_t ring_tbl_map;
6359 	u32 i, nr_ctxs;
6360 	int rc;
6361 
6362 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6363 	if (rc)
6364 		return rc;
6365 
6366 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6367 	if (!set_rss)
6368 		return hwrm_req_send(bp, req);
6369 
6370 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6371 	ring_tbl_map = vnic->rss_table_dma_addr;
6372 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6373 
6374 	hwrm_req_hold(bp, req);
6375 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6376 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6377 		req->ring_table_pair_index = i;
6378 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6379 		rc = hwrm_req_send(bp, req);
6380 		if (rc)
6381 			goto exit;
6382 	}
6383 
6384 exit:
6385 	hwrm_req_drop(bp, req);
6386 	return rc;
6387 }
6388 
6389 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6390 {
6391 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6392 	struct hwrm_vnic_rss_qcfg_output *resp;
6393 	struct hwrm_vnic_rss_qcfg_input *req;
6394 
6395 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6396 		return;
6397 
6398 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6399 	/* all contexts configured to same hash_type, zero always exists */
6400 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6401 	resp = hwrm_req_hold(bp, req);
6402 	if (!hwrm_req_send(bp, req)) {
6403 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6404 		bp->rss_hash_delta = 0;
6405 	}
6406 	hwrm_req_drop(bp, req);
6407 }
6408 
6409 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6410 {
6411 	struct hwrm_vnic_plcmodes_cfg_input *req;
6412 	int rc;
6413 
6414 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6415 	if (rc)
6416 		return rc;
6417 
6418 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6419 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6420 
6421 	if (BNXT_RX_PAGE_MODE(bp)) {
6422 		req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6423 	} else {
6424 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6425 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6426 		req->enables |=
6427 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6428 		req->jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
6429 		req->hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
6430 	}
6431 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6432 	return hwrm_req_send(bp, req);
6433 }
6434 
6435 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6436 					struct bnxt_vnic_info *vnic,
6437 					u16 ctx_idx)
6438 {
6439 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6440 
6441 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6442 		return;
6443 
6444 	req->rss_cos_lb_ctx_id =
6445 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6446 
6447 	hwrm_req_send(bp, req);
6448 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6449 }
6450 
6451 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6452 {
6453 	int i, j;
6454 
6455 	for (i = 0; i < bp->nr_vnics; i++) {
6456 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6457 
6458 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6459 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6460 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6461 		}
6462 	}
6463 	bp->rsscos_nr_ctxs = 0;
6464 }
6465 
6466 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6467 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6468 {
6469 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6470 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6471 	int rc;
6472 
6473 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6474 	if (rc)
6475 		return rc;
6476 
6477 	resp = hwrm_req_hold(bp, req);
6478 	rc = hwrm_req_send(bp, req);
6479 	if (!rc)
6480 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6481 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6482 	hwrm_req_drop(bp, req);
6483 
6484 	return rc;
6485 }
6486 
6487 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6488 {
6489 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6490 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6491 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6492 }
6493 
6494 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6495 {
6496 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6497 	struct hwrm_vnic_cfg_input *req;
6498 	unsigned int ring = 0, grp_idx;
6499 	u16 def_vlan = 0;
6500 	int rc;
6501 
6502 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6503 	if (rc)
6504 		return rc;
6505 
6506 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6507 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6508 
6509 		req->default_rx_ring_id =
6510 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6511 		req->default_cmpl_ring_id =
6512 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6513 		req->enables =
6514 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6515 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6516 		goto vnic_mru;
6517 	}
6518 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6519 	/* Only RSS support for now TBD: COS & LB */
6520 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6521 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6522 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6523 					   VNIC_CFG_REQ_ENABLES_MRU);
6524 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6525 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6526 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6527 					   VNIC_CFG_REQ_ENABLES_MRU);
6528 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6529 	} else {
6530 		req->rss_rule = cpu_to_le16(0xffff);
6531 	}
6532 
6533 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6534 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6535 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6536 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6537 	} else {
6538 		req->cos_rule = cpu_to_le16(0xffff);
6539 	}
6540 
6541 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6542 		ring = 0;
6543 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6544 		ring = vnic->vnic_id - 1;
6545 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6546 		ring = bp->rx_nr_rings - 1;
6547 
6548 	grp_idx = bp->rx_ring[ring].bnapi->index;
6549 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6550 	req->lb_rule = cpu_to_le16(0xffff);
6551 vnic_mru:
6552 	vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
6553 	req->mru = cpu_to_le16(vnic->mru);
6554 
6555 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6556 #ifdef CONFIG_BNXT_SRIOV
6557 	if (BNXT_VF(bp))
6558 		def_vlan = bp->vf.vlan;
6559 #endif
6560 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6561 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6562 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6563 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6564 
6565 	return hwrm_req_send(bp, req);
6566 }
6567 
6568 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6569 				    struct bnxt_vnic_info *vnic)
6570 {
6571 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6572 		struct hwrm_vnic_free_input *req;
6573 
6574 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6575 			return;
6576 
6577 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6578 
6579 		hwrm_req_send(bp, req);
6580 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6581 	}
6582 }
6583 
6584 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6585 {
6586 	u16 i;
6587 
6588 	for (i = 0; i < bp->nr_vnics; i++)
6589 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6590 }
6591 
6592 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6593 			 unsigned int start_rx_ring_idx,
6594 			 unsigned int nr_rings)
6595 {
6596 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6597 	struct hwrm_vnic_alloc_output *resp;
6598 	struct hwrm_vnic_alloc_input *req;
6599 	int rc;
6600 
6601 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6602 	if (rc)
6603 		return rc;
6604 
6605 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6606 		goto vnic_no_ring_grps;
6607 
6608 	/* map ring groups to this vnic */
6609 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6610 		grp_idx = bp->rx_ring[i].bnapi->index;
6611 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6612 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6613 				   j, nr_rings);
6614 			break;
6615 		}
6616 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6617 	}
6618 
6619 vnic_no_ring_grps:
6620 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6621 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6622 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6623 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6624 
6625 	resp = hwrm_req_hold(bp, req);
6626 	rc = hwrm_req_send(bp, req);
6627 	if (!rc)
6628 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6629 	hwrm_req_drop(bp, req);
6630 	return rc;
6631 }
6632 
6633 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6634 {
6635 	struct hwrm_vnic_qcaps_output *resp;
6636 	struct hwrm_vnic_qcaps_input *req;
6637 	int rc;
6638 
6639 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6640 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6641 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6642 	if (bp->hwrm_spec_code < 0x10600)
6643 		return 0;
6644 
6645 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6646 	if (rc)
6647 		return rc;
6648 
6649 	resp = hwrm_req_hold(bp, req);
6650 	rc = hwrm_req_send(bp, req);
6651 	if (!rc) {
6652 		u32 flags = le32_to_cpu(resp->flags);
6653 
6654 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6655 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6656 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6657 		if (flags &
6658 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6659 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6660 
6661 		/* Older P5 fw before EXT_HW_STATS support did not set
6662 		 * VLAN_STRIP_CAP properly.
6663 		 */
6664 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6665 		    (BNXT_CHIP_P5(bp) &&
6666 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6667 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6668 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6669 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6670 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6671 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6672 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6673 		if (bp->max_tpa_v2) {
6674 			if (BNXT_CHIP_P5(bp))
6675 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6676 			else
6677 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6678 		}
6679 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6680 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6681 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6682 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6683 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6684 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6685 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6686 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6687 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6688 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6689 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
6690 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
6691 	}
6692 	hwrm_req_drop(bp, req);
6693 	return rc;
6694 }
6695 
6696 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6697 {
6698 	struct hwrm_ring_grp_alloc_output *resp;
6699 	struct hwrm_ring_grp_alloc_input *req;
6700 	int rc;
6701 	u16 i;
6702 
6703 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6704 		return 0;
6705 
6706 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6707 	if (rc)
6708 		return rc;
6709 
6710 	resp = hwrm_req_hold(bp, req);
6711 	for (i = 0; i < bp->rx_nr_rings; i++) {
6712 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
6713 
6714 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
6715 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
6716 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
6717 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
6718 
6719 		rc = hwrm_req_send(bp, req);
6720 
6721 		if (rc)
6722 			break;
6723 
6724 		bp->grp_info[grp_idx].fw_grp_id =
6725 			le32_to_cpu(resp->ring_group_id);
6726 	}
6727 	hwrm_req_drop(bp, req);
6728 	return rc;
6729 }
6730 
6731 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
6732 {
6733 	struct hwrm_ring_grp_free_input *req;
6734 	u16 i;
6735 
6736 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
6737 		return;
6738 
6739 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
6740 		return;
6741 
6742 	hwrm_req_hold(bp, req);
6743 	for (i = 0; i < bp->cp_nr_rings; i++) {
6744 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
6745 			continue;
6746 		req->ring_group_id =
6747 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
6748 
6749 		hwrm_req_send(bp, req);
6750 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
6751 	}
6752 	hwrm_req_drop(bp, req);
6753 }
6754 
6755 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
6756 				    struct bnxt_ring_struct *ring,
6757 				    u32 ring_type, u32 map_index)
6758 {
6759 	struct hwrm_ring_alloc_output *resp;
6760 	struct hwrm_ring_alloc_input *req;
6761 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
6762 	struct bnxt_ring_grp_info *grp_info;
6763 	int rc, err = 0;
6764 	u16 ring_id;
6765 
6766 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
6767 	if (rc)
6768 		goto exit;
6769 
6770 	req->enables = 0;
6771 	if (rmem->nr_pages > 1) {
6772 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
6773 		/* Page size is in log2 units */
6774 		req->page_size = BNXT_PAGE_SHIFT;
6775 		req->page_tbl_depth = 1;
6776 	} else {
6777 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
6778 	}
6779 	req->fbo = 0;
6780 	/* Association of ring index with doorbell index and MSIX number */
6781 	req->logical_id = cpu_to_le16(map_index);
6782 
6783 	switch (ring_type) {
6784 	case HWRM_RING_ALLOC_TX: {
6785 		struct bnxt_tx_ring_info *txr;
6786 		u16 flags = 0;
6787 
6788 		txr = container_of(ring, struct bnxt_tx_ring_info,
6789 				   tx_ring_struct);
6790 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
6791 		/* Association of transmit ring with completion ring */
6792 		grp_info = &bp->grp_info[ring->grp_idx];
6793 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
6794 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
6795 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6796 		req->queue_id = cpu_to_le16(ring->queue_id);
6797 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
6798 			req->cmpl_coal_cnt =
6799 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
6800 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
6801 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
6802 		req->flags = cpu_to_le16(flags);
6803 		break;
6804 	}
6805 	case HWRM_RING_ALLOC_RX:
6806 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6807 		req->length = cpu_to_le32(bp->rx_ring_mask + 1);
6808 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6809 			u16 flags = 0;
6810 
6811 			/* Association of rx ring with stats context */
6812 			grp_info = &bp->grp_info[ring->grp_idx];
6813 			req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
6814 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6815 			req->enables |= cpu_to_le32(
6816 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6817 			if (NET_IP_ALIGN == 2)
6818 				flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
6819 			req->flags = cpu_to_le16(flags);
6820 		}
6821 		break;
6822 	case HWRM_RING_ALLOC_AGG:
6823 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6824 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
6825 			/* Association of agg ring with rx ring */
6826 			grp_info = &bp->grp_info[ring->grp_idx];
6827 			req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
6828 			req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
6829 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6830 			req->enables |= cpu_to_le32(
6831 				RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
6832 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6833 		} else {
6834 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6835 		}
6836 		req->length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
6837 		break;
6838 	case HWRM_RING_ALLOC_CMPL:
6839 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
6840 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
6841 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6842 			/* Association of cp ring with nq */
6843 			grp_info = &bp->grp_info[map_index];
6844 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
6845 			req->cq_handle = cpu_to_le64(ring->handle);
6846 			req->enables |= cpu_to_le32(
6847 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
6848 		} else {
6849 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
6850 		}
6851 		break;
6852 	case HWRM_RING_ALLOC_NQ:
6853 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
6854 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
6855 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
6856 		break;
6857 	default:
6858 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
6859 			   ring_type);
6860 		return -1;
6861 	}
6862 
6863 	resp = hwrm_req_hold(bp, req);
6864 	rc = hwrm_req_send(bp, req);
6865 	err = le16_to_cpu(resp->error_code);
6866 	ring_id = le16_to_cpu(resp->ring_id);
6867 	hwrm_req_drop(bp, req);
6868 
6869 exit:
6870 	if (rc || err) {
6871 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
6872 			   ring_type, rc, err);
6873 		return -EIO;
6874 	}
6875 	ring->fw_ring_id = ring_id;
6876 	return rc;
6877 }
6878 
6879 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
6880 {
6881 	int rc;
6882 
6883 	if (BNXT_PF(bp)) {
6884 		struct hwrm_func_cfg_input *req;
6885 
6886 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
6887 		if (rc)
6888 			return rc;
6889 
6890 		req->fid = cpu_to_le16(0xffff);
6891 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
6892 		req->async_event_cr = cpu_to_le16(idx);
6893 		return hwrm_req_send(bp, req);
6894 	} else {
6895 		struct hwrm_func_vf_cfg_input *req;
6896 
6897 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
6898 		if (rc)
6899 			return rc;
6900 
6901 		req->enables =
6902 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
6903 		req->async_event_cr = cpu_to_le16(idx);
6904 		return hwrm_req_send(bp, req);
6905 	}
6906 }
6907 
6908 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
6909 			     u32 ring_type)
6910 {
6911 	switch (ring_type) {
6912 	case HWRM_RING_ALLOC_TX:
6913 		db->db_ring_mask = bp->tx_ring_mask;
6914 		break;
6915 	case HWRM_RING_ALLOC_RX:
6916 		db->db_ring_mask = bp->rx_ring_mask;
6917 		break;
6918 	case HWRM_RING_ALLOC_AGG:
6919 		db->db_ring_mask = bp->rx_agg_ring_mask;
6920 		break;
6921 	case HWRM_RING_ALLOC_CMPL:
6922 	case HWRM_RING_ALLOC_NQ:
6923 		db->db_ring_mask = bp->cp_ring_mask;
6924 		break;
6925 	}
6926 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
6927 		db->db_epoch_mask = db->db_ring_mask + 1;
6928 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
6929 	}
6930 }
6931 
6932 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
6933 			u32 map_idx, u32 xid)
6934 {
6935 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6936 		switch (ring_type) {
6937 		case HWRM_RING_ALLOC_TX:
6938 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
6939 			break;
6940 		case HWRM_RING_ALLOC_RX:
6941 		case HWRM_RING_ALLOC_AGG:
6942 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
6943 			break;
6944 		case HWRM_RING_ALLOC_CMPL:
6945 			db->db_key64 = DBR_PATH_L2;
6946 			break;
6947 		case HWRM_RING_ALLOC_NQ:
6948 			db->db_key64 = DBR_PATH_L2;
6949 			break;
6950 		}
6951 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
6952 
6953 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6954 			db->db_key64 |= DBR_VALID;
6955 
6956 		db->doorbell = bp->bar1 + bp->db_offset;
6957 	} else {
6958 		db->doorbell = bp->bar1 + map_idx * 0x80;
6959 		switch (ring_type) {
6960 		case HWRM_RING_ALLOC_TX:
6961 			db->db_key32 = DB_KEY_TX;
6962 			break;
6963 		case HWRM_RING_ALLOC_RX:
6964 		case HWRM_RING_ALLOC_AGG:
6965 			db->db_key32 = DB_KEY_RX;
6966 			break;
6967 		case HWRM_RING_ALLOC_CMPL:
6968 			db->db_key32 = DB_KEY_CP;
6969 			break;
6970 		}
6971 	}
6972 	bnxt_set_db_mask(bp, db, ring_type);
6973 }
6974 
6975 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
6976 				   struct bnxt_rx_ring_info *rxr)
6977 {
6978 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
6979 	struct bnxt_napi *bnapi = rxr->bnapi;
6980 	u32 type = HWRM_RING_ALLOC_RX;
6981 	u32 map_idx = bnapi->index;
6982 	int rc;
6983 
6984 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6985 	if (rc)
6986 		return rc;
6987 
6988 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
6989 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
6990 
6991 	return 0;
6992 }
6993 
6994 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
6995 				       struct bnxt_rx_ring_info *rxr)
6996 {
6997 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
6998 	u32 type = HWRM_RING_ALLOC_AGG;
6999 	u32 grp_idx = ring->grp_idx;
7000 	u32 map_idx;
7001 	int rc;
7002 
7003 	map_idx = grp_idx + bp->rx_nr_rings;
7004 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7005 	if (rc)
7006 		return rc;
7007 
7008 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7009 		    ring->fw_ring_id);
7010 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7011 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7012 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7013 
7014 	return 0;
7015 }
7016 
7017 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7018 {
7019 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7020 	int i, rc = 0;
7021 	u32 type;
7022 
7023 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7024 		type = HWRM_RING_ALLOC_NQ;
7025 	else
7026 		type = HWRM_RING_ALLOC_CMPL;
7027 	for (i = 0; i < bp->cp_nr_rings; i++) {
7028 		struct bnxt_napi *bnapi = bp->bnapi[i];
7029 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7030 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7031 		u32 map_idx = ring->map_idx;
7032 		unsigned int vector;
7033 
7034 		vector = bp->irq_tbl[map_idx].vector;
7035 		disable_irq_nosync(vector);
7036 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7037 		if (rc) {
7038 			enable_irq(vector);
7039 			goto err_out;
7040 		}
7041 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7042 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7043 		enable_irq(vector);
7044 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7045 
7046 		if (!i) {
7047 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7048 			if (rc)
7049 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7050 		}
7051 	}
7052 
7053 	type = HWRM_RING_ALLOC_TX;
7054 	for (i = 0; i < bp->tx_nr_rings; i++) {
7055 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7056 		struct bnxt_ring_struct *ring;
7057 		u32 map_idx;
7058 
7059 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7060 			struct bnxt_cp_ring_info *cpr2 = txr->tx_cpr;
7061 			struct bnxt_napi *bnapi = txr->bnapi;
7062 			u32 type2 = HWRM_RING_ALLOC_CMPL;
7063 
7064 			ring = &cpr2->cp_ring_struct;
7065 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
7066 			map_idx = bnapi->index;
7067 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
7068 			if (rc)
7069 				goto err_out;
7070 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
7071 				    ring->fw_ring_id);
7072 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
7073 		}
7074 		ring = &txr->tx_ring_struct;
7075 		map_idx = i;
7076 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7077 		if (rc)
7078 			goto err_out;
7079 		bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
7080 	}
7081 
7082 	for (i = 0; i < bp->rx_nr_rings; i++) {
7083 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7084 
7085 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7086 		if (rc)
7087 			goto err_out;
7088 		/* If we have agg rings, post agg buffers first. */
7089 		if (!agg_rings)
7090 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7091 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7092 			struct bnxt_cp_ring_info *cpr2 = rxr->rx_cpr;
7093 			struct bnxt_napi *bnapi = rxr->bnapi;
7094 			u32 type2 = HWRM_RING_ALLOC_CMPL;
7095 			struct bnxt_ring_struct *ring;
7096 			u32 map_idx = bnapi->index;
7097 
7098 			ring = &cpr2->cp_ring_struct;
7099 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
7100 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
7101 			if (rc)
7102 				goto err_out;
7103 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
7104 				    ring->fw_ring_id);
7105 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
7106 		}
7107 	}
7108 
7109 	if (agg_rings) {
7110 		for (i = 0; i < bp->rx_nr_rings; i++) {
7111 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7112 			if (rc)
7113 				goto err_out;
7114 		}
7115 	}
7116 err_out:
7117 	return rc;
7118 }
7119 
7120 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7121 				   struct bnxt_ring_struct *ring,
7122 				   u32 ring_type, int cmpl_ring_id)
7123 {
7124 	struct hwrm_ring_free_output *resp;
7125 	struct hwrm_ring_free_input *req;
7126 	u16 error_code = 0;
7127 	int rc;
7128 
7129 	if (BNXT_NO_FW_ACCESS(bp))
7130 		return 0;
7131 
7132 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7133 	if (rc)
7134 		goto exit;
7135 
7136 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7137 	req->ring_type = ring_type;
7138 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7139 
7140 	resp = hwrm_req_hold(bp, req);
7141 	rc = hwrm_req_send(bp, req);
7142 	error_code = le16_to_cpu(resp->error_code);
7143 	hwrm_req_drop(bp, req);
7144 exit:
7145 	if (rc || error_code) {
7146 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7147 			   ring_type, rc, error_code);
7148 		return -EIO;
7149 	}
7150 	return 0;
7151 }
7152 
7153 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7154 				   struct bnxt_rx_ring_info *rxr,
7155 				   bool close_path)
7156 {
7157 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7158 	u32 grp_idx = rxr->bnapi->index;
7159 	u32 cmpl_ring_id;
7160 
7161 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7162 		return;
7163 
7164 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7165 	hwrm_ring_free_send_msg(bp, ring,
7166 				RING_FREE_REQ_RING_TYPE_RX,
7167 				close_path ? cmpl_ring_id :
7168 				INVALID_HW_RING_ID);
7169 	ring->fw_ring_id = INVALID_HW_RING_ID;
7170 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7171 }
7172 
7173 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7174 				       struct bnxt_rx_ring_info *rxr,
7175 				       bool close_path)
7176 {
7177 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7178 	u32 grp_idx = rxr->bnapi->index;
7179 	u32 type, cmpl_ring_id;
7180 
7181 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7182 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7183 	else
7184 		type = RING_FREE_REQ_RING_TYPE_RX;
7185 
7186 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7187 		return;
7188 
7189 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7190 	hwrm_ring_free_send_msg(bp, ring, type,
7191 				close_path ? cmpl_ring_id :
7192 				INVALID_HW_RING_ID);
7193 	ring->fw_ring_id = INVALID_HW_RING_ID;
7194 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7195 }
7196 
7197 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7198 {
7199 	u32 type;
7200 	int i;
7201 
7202 	if (!bp->bnapi)
7203 		return;
7204 
7205 	for (i = 0; i < bp->tx_nr_rings; i++) {
7206 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7207 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7208 
7209 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7210 			u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
7211 
7212 			hwrm_ring_free_send_msg(bp, ring,
7213 						RING_FREE_REQ_RING_TYPE_TX,
7214 						close_path ? cmpl_ring_id :
7215 						INVALID_HW_RING_ID);
7216 			ring->fw_ring_id = INVALID_HW_RING_ID;
7217 		}
7218 	}
7219 
7220 	for (i = 0; i < bp->rx_nr_rings; i++) {
7221 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7222 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7223 	}
7224 
7225 	/* The completion rings are about to be freed.  After that the
7226 	 * IRQ doorbell will not work anymore.  So we need to disable
7227 	 * IRQ here.
7228 	 */
7229 	bnxt_disable_int_sync(bp);
7230 
7231 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7232 		type = RING_FREE_REQ_RING_TYPE_NQ;
7233 	else
7234 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7235 	for (i = 0; i < bp->cp_nr_rings; i++) {
7236 		struct bnxt_napi *bnapi = bp->bnapi[i];
7237 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7238 		struct bnxt_ring_struct *ring;
7239 		int j;
7240 
7241 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++) {
7242 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
7243 
7244 			ring = &cpr2->cp_ring_struct;
7245 			if (ring->fw_ring_id == INVALID_HW_RING_ID)
7246 				continue;
7247 			hwrm_ring_free_send_msg(bp, ring,
7248 						RING_FREE_REQ_RING_TYPE_L2_CMPL,
7249 						INVALID_HW_RING_ID);
7250 			ring->fw_ring_id = INVALID_HW_RING_ID;
7251 		}
7252 		ring = &cpr->cp_ring_struct;
7253 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7254 			hwrm_ring_free_send_msg(bp, ring, type,
7255 						INVALID_HW_RING_ID);
7256 			ring->fw_ring_id = INVALID_HW_RING_ID;
7257 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7258 		}
7259 	}
7260 }
7261 
7262 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7263 			     bool shared);
7264 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7265 			   bool shared);
7266 
7267 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7268 {
7269 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7270 	struct hwrm_func_qcfg_output *resp;
7271 	struct hwrm_func_qcfg_input *req;
7272 	int rc;
7273 
7274 	if (bp->hwrm_spec_code < 0x10601)
7275 		return 0;
7276 
7277 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7278 	if (rc)
7279 		return rc;
7280 
7281 	req->fid = cpu_to_le16(0xffff);
7282 	resp = hwrm_req_hold(bp, req);
7283 	rc = hwrm_req_send(bp, req);
7284 	if (rc) {
7285 		hwrm_req_drop(bp, req);
7286 		return rc;
7287 	}
7288 
7289 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7290 	if (BNXT_NEW_RM(bp)) {
7291 		u16 cp, stats;
7292 
7293 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7294 		hw_resc->resv_hw_ring_grps =
7295 			le32_to_cpu(resp->alloc_hw_ring_grps);
7296 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7297 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7298 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7299 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7300 		hw_resc->resv_irqs = cp;
7301 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7302 			int rx = hw_resc->resv_rx_rings;
7303 			int tx = hw_resc->resv_tx_rings;
7304 
7305 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7306 				rx >>= 1;
7307 			if (cp < (rx + tx)) {
7308 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7309 				if (rc)
7310 					goto get_rings_exit;
7311 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7312 					rx <<= 1;
7313 				hw_resc->resv_rx_rings = rx;
7314 				hw_resc->resv_tx_rings = tx;
7315 			}
7316 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7317 			hw_resc->resv_hw_ring_grps = rx;
7318 		}
7319 		hw_resc->resv_cp_rings = cp;
7320 		hw_resc->resv_stat_ctxs = stats;
7321 	}
7322 get_rings_exit:
7323 	hwrm_req_drop(bp, req);
7324 	return rc;
7325 }
7326 
7327 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7328 {
7329 	struct hwrm_func_qcfg_output *resp;
7330 	struct hwrm_func_qcfg_input *req;
7331 	int rc;
7332 
7333 	if (bp->hwrm_spec_code < 0x10601)
7334 		return 0;
7335 
7336 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7337 	if (rc)
7338 		return rc;
7339 
7340 	req->fid = cpu_to_le16(fid);
7341 	resp = hwrm_req_hold(bp, req);
7342 	rc = hwrm_req_send(bp, req);
7343 	if (!rc)
7344 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7345 
7346 	hwrm_req_drop(bp, req);
7347 	return rc;
7348 }
7349 
7350 static bool bnxt_rfs_supported(struct bnxt *bp);
7351 
7352 static struct hwrm_func_cfg_input *
7353 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7354 {
7355 	struct hwrm_func_cfg_input *req;
7356 	u32 enables = 0;
7357 
7358 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7359 		return NULL;
7360 
7361 	req->fid = cpu_to_le16(0xffff);
7362 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7363 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7364 	if (BNXT_NEW_RM(bp)) {
7365 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7366 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7367 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7368 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7369 			enables |= hwr->cp_p5 ?
7370 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7371 		} else {
7372 			enables |= hwr->cp ?
7373 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7374 			enables |= hwr->grp ?
7375 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7376 		}
7377 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7378 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7379 					  0;
7380 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7381 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7382 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7383 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7384 			req->num_msix = cpu_to_le16(hwr->cp);
7385 		} else {
7386 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7387 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7388 		}
7389 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7390 		req->num_vnics = cpu_to_le16(hwr->vnic);
7391 	}
7392 	req->enables = cpu_to_le32(enables);
7393 	return req;
7394 }
7395 
7396 static struct hwrm_func_vf_cfg_input *
7397 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7398 {
7399 	struct hwrm_func_vf_cfg_input *req;
7400 	u32 enables = 0;
7401 
7402 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7403 		return NULL;
7404 
7405 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7406 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7407 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7408 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7409 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7410 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7411 		enables |= hwr->cp_p5 ?
7412 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7413 	} else {
7414 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7415 		enables |= hwr->grp ?
7416 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7417 	}
7418 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7419 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7420 
7421 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7422 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7423 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7424 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7425 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7426 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7427 	} else {
7428 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7429 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7430 	}
7431 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7432 	req->num_vnics = cpu_to_le16(hwr->vnic);
7433 
7434 	req->enables = cpu_to_le32(enables);
7435 	return req;
7436 }
7437 
7438 static int
7439 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7440 {
7441 	struct hwrm_func_cfg_input *req;
7442 	int rc;
7443 
7444 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7445 	if (!req)
7446 		return -ENOMEM;
7447 
7448 	if (!req->enables) {
7449 		hwrm_req_drop(bp, req);
7450 		return 0;
7451 	}
7452 
7453 	rc = hwrm_req_send(bp, req);
7454 	if (rc)
7455 		return rc;
7456 
7457 	if (bp->hwrm_spec_code < 0x10601)
7458 		bp->hw_resc.resv_tx_rings = hwr->tx;
7459 
7460 	return bnxt_hwrm_get_rings(bp);
7461 }
7462 
7463 static int
7464 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7465 {
7466 	struct hwrm_func_vf_cfg_input *req;
7467 	int rc;
7468 
7469 	if (!BNXT_NEW_RM(bp)) {
7470 		bp->hw_resc.resv_tx_rings = hwr->tx;
7471 		return 0;
7472 	}
7473 
7474 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7475 	if (!req)
7476 		return -ENOMEM;
7477 
7478 	rc = hwrm_req_send(bp, req);
7479 	if (rc)
7480 		return rc;
7481 
7482 	return bnxt_hwrm_get_rings(bp);
7483 }
7484 
7485 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7486 {
7487 	if (BNXT_PF(bp))
7488 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7489 	else
7490 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7491 }
7492 
7493 int bnxt_nq_rings_in_use(struct bnxt *bp)
7494 {
7495 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7496 }
7497 
7498 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7499 {
7500 	int cp;
7501 
7502 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7503 		return bnxt_nq_rings_in_use(bp);
7504 
7505 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7506 	return cp;
7507 }
7508 
7509 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7510 {
7511 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7512 }
7513 
7514 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7515 {
7516 	if (!hwr->grp)
7517 		return 0;
7518 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7519 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7520 
7521 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7522 			rss_ctx *= hwr->vnic;
7523 		return rss_ctx;
7524 	}
7525 	if (BNXT_VF(bp))
7526 		return BNXT_VF_MAX_RSS_CTX;
7527 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7528 		return hwr->grp + 1;
7529 	return 1;
7530 }
7531 
7532 /* Check if a default RSS map needs to be setup.  This function is only
7533  * used on older firmware that does not require reserving RX rings.
7534  */
7535 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7536 {
7537 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7538 
7539 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7540 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7541 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7542 		if (!netif_is_rxfh_configured(bp->dev))
7543 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7544 	}
7545 }
7546 
7547 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7548 {
7549 	if (bp->flags & BNXT_FLAG_RFS) {
7550 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7551 			return 2 + bp->num_rss_ctx;
7552 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7553 			return rx_rings + 1;
7554 	}
7555 	return 1;
7556 }
7557 
7558 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7559 {
7560 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7561 	int cp = bnxt_cp_rings_in_use(bp);
7562 	int nq = bnxt_nq_rings_in_use(bp);
7563 	int rx = bp->rx_nr_rings, stat;
7564 	int vnic, grp = rx;
7565 
7566 	/* Old firmware does not need RX ring reservations but we still
7567 	 * need to setup a default RSS map when needed.  With new firmware
7568 	 * we go through RX ring reservations first and then set up the
7569 	 * RSS map for the successfully reserved RX rings when needed.
7570 	 */
7571 	if (!BNXT_NEW_RM(bp))
7572 		bnxt_check_rss_tbl_no_rmgr(bp);
7573 
7574 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7575 	    bp->hwrm_spec_code >= 0x10601)
7576 		return true;
7577 
7578 	if (!BNXT_NEW_RM(bp))
7579 		return false;
7580 
7581 	vnic = bnxt_get_total_vnics(bp, rx);
7582 
7583 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7584 		rx <<= 1;
7585 	stat = bnxt_get_func_stat_ctxs(bp);
7586 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7587 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7588 	    (hw_resc->resv_hw_ring_grps != grp &&
7589 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7590 		return true;
7591 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7592 	    hw_resc->resv_irqs != nq)
7593 		return true;
7594 	return false;
7595 }
7596 
7597 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7598 {
7599 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7600 
7601 	hwr->tx = hw_resc->resv_tx_rings;
7602 	if (BNXT_NEW_RM(bp)) {
7603 		hwr->rx = hw_resc->resv_rx_rings;
7604 		hwr->cp = hw_resc->resv_irqs;
7605 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7606 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7607 		hwr->grp = hw_resc->resv_hw_ring_grps;
7608 		hwr->vnic = hw_resc->resv_vnics;
7609 		hwr->stat = hw_resc->resv_stat_ctxs;
7610 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7611 	}
7612 }
7613 
7614 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7615 {
7616 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7617 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7618 }
7619 
7620 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
7621 
7622 static int __bnxt_reserve_rings(struct bnxt *bp)
7623 {
7624 	struct bnxt_hw_rings hwr = {0};
7625 	int rx_rings, old_rx_rings, rc;
7626 	int cp = bp->cp_nr_rings;
7627 	int ulp_msix = 0;
7628 	bool sh = false;
7629 	int tx_cp;
7630 
7631 	if (!bnxt_need_reserve_rings(bp))
7632 		return 0;
7633 
7634 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
7635 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
7636 		if (!ulp_msix)
7637 			bnxt_set_ulp_stat_ctxs(bp, 0);
7638 
7639 		if (ulp_msix > bp->ulp_num_msix_want)
7640 			ulp_msix = bp->ulp_num_msix_want;
7641 		hwr.cp = cp + ulp_msix;
7642 	} else {
7643 		hwr.cp = bnxt_nq_rings_in_use(bp);
7644 	}
7645 
7646 	hwr.tx = bp->tx_nr_rings;
7647 	hwr.rx = bp->rx_nr_rings;
7648 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7649 		sh = true;
7650 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7651 		hwr.cp_p5 = hwr.rx + hwr.tx;
7652 
7653 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7654 
7655 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7656 		hwr.rx <<= 1;
7657 	hwr.grp = bp->rx_nr_rings;
7658 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7659 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
7660 	old_rx_rings = bp->hw_resc.resv_rx_rings;
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 (old_rx_rings != bp->hw_resc.resv_rx_rings &&
7716 	    !netif_is_rxfh_configured(bp->dev))
7717 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7718 
7719 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
7720 		int resv_msix, resv_ctx, ulp_ctxs;
7721 		struct bnxt_hw_resc *hw_resc;
7722 
7723 		hw_resc = &bp->hw_resc;
7724 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
7725 		ulp_msix = min_t(int, resv_msix, ulp_msix);
7726 		bnxt_set_ulp_msix_num(bp, ulp_msix);
7727 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
7728 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
7729 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
7730 	}
7731 
7732 	return rc;
7733 }
7734 
7735 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7736 {
7737 	struct hwrm_func_vf_cfg_input *req;
7738 	u32 flags;
7739 
7740 	if (!BNXT_NEW_RM(bp))
7741 		return 0;
7742 
7743 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7744 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
7745 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7746 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7747 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7748 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
7749 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
7750 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7751 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7752 
7753 	req->flags = cpu_to_le32(flags);
7754 	return hwrm_req_send_silent(bp, req);
7755 }
7756 
7757 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7758 {
7759 	struct hwrm_func_cfg_input *req;
7760 	u32 flags;
7761 
7762 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7763 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
7764 	if (BNXT_NEW_RM(bp)) {
7765 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7766 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7767 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7768 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
7769 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7770 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
7771 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
7772 		else
7773 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7774 	}
7775 
7776 	req->flags = cpu_to_le32(flags);
7777 	return hwrm_req_send_silent(bp, req);
7778 }
7779 
7780 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7781 {
7782 	if (bp->hwrm_spec_code < 0x10801)
7783 		return 0;
7784 
7785 	if (BNXT_PF(bp))
7786 		return bnxt_hwrm_check_pf_rings(bp, hwr);
7787 
7788 	return bnxt_hwrm_check_vf_rings(bp, hwr);
7789 }
7790 
7791 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
7792 {
7793 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7794 	struct hwrm_ring_aggint_qcaps_output *resp;
7795 	struct hwrm_ring_aggint_qcaps_input *req;
7796 	int rc;
7797 
7798 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
7799 	coal_cap->num_cmpl_dma_aggr_max = 63;
7800 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
7801 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
7802 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
7803 	coal_cap->int_lat_tmr_min_max = 65535;
7804 	coal_cap->int_lat_tmr_max_max = 65535;
7805 	coal_cap->num_cmpl_aggr_int_max = 65535;
7806 	coal_cap->timer_units = 80;
7807 
7808 	if (bp->hwrm_spec_code < 0x10902)
7809 		return;
7810 
7811 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
7812 		return;
7813 
7814 	resp = hwrm_req_hold(bp, req);
7815 	rc = hwrm_req_send_silent(bp, req);
7816 	if (!rc) {
7817 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
7818 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
7819 		coal_cap->num_cmpl_dma_aggr_max =
7820 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
7821 		coal_cap->num_cmpl_dma_aggr_during_int_max =
7822 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
7823 		coal_cap->cmpl_aggr_dma_tmr_max =
7824 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
7825 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
7826 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
7827 		coal_cap->int_lat_tmr_min_max =
7828 			le16_to_cpu(resp->int_lat_tmr_min_max);
7829 		coal_cap->int_lat_tmr_max_max =
7830 			le16_to_cpu(resp->int_lat_tmr_max_max);
7831 		coal_cap->num_cmpl_aggr_int_max =
7832 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
7833 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
7834 	}
7835 	hwrm_req_drop(bp, req);
7836 }
7837 
7838 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
7839 {
7840 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7841 
7842 	return usec * 1000 / coal_cap->timer_units;
7843 }
7844 
7845 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
7846 	struct bnxt_coal *hw_coal,
7847 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7848 {
7849 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7850 	u16 val, tmr, max, flags = hw_coal->flags;
7851 	u32 cmpl_params = coal_cap->cmpl_params;
7852 
7853 	max = hw_coal->bufs_per_record * 128;
7854 	if (hw_coal->budget)
7855 		max = hw_coal->bufs_per_record * hw_coal->budget;
7856 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
7857 
7858 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
7859 	req->num_cmpl_aggr_int = cpu_to_le16(val);
7860 
7861 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
7862 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
7863 
7864 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
7865 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
7866 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
7867 
7868 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
7869 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
7870 	req->int_lat_tmr_max = cpu_to_le16(tmr);
7871 
7872 	/* min timer set to 1/2 of interrupt timer */
7873 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
7874 		val = tmr / 2;
7875 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
7876 		req->int_lat_tmr_min = cpu_to_le16(val);
7877 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
7878 	}
7879 
7880 	/* buf timer set to 1/4 of interrupt timer */
7881 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
7882 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
7883 
7884 	if (cmpl_params &
7885 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
7886 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
7887 		val = clamp_t(u16, tmr, 1,
7888 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
7889 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
7890 		req->enables |=
7891 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
7892 	}
7893 
7894 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
7895 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
7896 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
7897 	req->flags = cpu_to_le16(flags);
7898 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
7899 }
7900 
7901 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
7902 				   struct bnxt_coal *hw_coal)
7903 {
7904 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
7905 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7906 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7907 	u32 nq_params = coal_cap->nq_params;
7908 	u16 tmr;
7909 	int rc;
7910 
7911 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
7912 		return 0;
7913 
7914 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7915 	if (rc)
7916 		return rc;
7917 
7918 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
7919 	req->flags =
7920 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
7921 
7922 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
7923 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
7924 	req->int_lat_tmr_min = cpu_to_le16(tmr);
7925 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
7926 	return hwrm_req_send(bp, req);
7927 }
7928 
7929 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
7930 {
7931 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
7932 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7933 	struct bnxt_coal coal;
7934 	int rc;
7935 
7936 	/* Tick values in micro seconds.
7937 	 * 1 coal_buf x bufs_per_record = 1 completion record.
7938 	 */
7939 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
7940 
7941 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
7942 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
7943 
7944 	if (!bnapi->rx_ring)
7945 		return -ENODEV;
7946 
7947 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7948 	if (rc)
7949 		return rc;
7950 
7951 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
7952 
7953 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
7954 
7955 	return hwrm_req_send(bp, req_rx);
7956 }
7957 
7958 static int
7959 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
7960 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7961 {
7962 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
7963 
7964 	req->ring_id = cpu_to_le16(ring_id);
7965 	return hwrm_req_send(bp, req);
7966 }
7967 
7968 static int
7969 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
7970 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7971 {
7972 	struct bnxt_tx_ring_info *txr;
7973 	int i, rc;
7974 
7975 	bnxt_for_each_napi_tx(i, bnapi, txr) {
7976 		u16 ring_id;
7977 
7978 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
7979 		req->ring_id = cpu_to_le16(ring_id);
7980 		rc = hwrm_req_send(bp, req);
7981 		if (rc)
7982 			return rc;
7983 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7984 			return 0;
7985 	}
7986 	return 0;
7987 }
7988 
7989 int bnxt_hwrm_set_coal(struct bnxt *bp)
7990 {
7991 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
7992 	int i, rc;
7993 
7994 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7995 	if (rc)
7996 		return rc;
7997 
7998 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7999 	if (rc) {
8000 		hwrm_req_drop(bp, req_rx);
8001 		return rc;
8002 	}
8003 
8004 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8005 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8006 
8007 	hwrm_req_hold(bp, req_rx);
8008 	hwrm_req_hold(bp, req_tx);
8009 	for (i = 0; i < bp->cp_nr_rings; i++) {
8010 		struct bnxt_napi *bnapi = bp->bnapi[i];
8011 		struct bnxt_coal *hw_coal;
8012 
8013 		if (!bnapi->rx_ring)
8014 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8015 		else
8016 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8017 		if (rc)
8018 			break;
8019 
8020 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8021 			continue;
8022 
8023 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8024 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8025 			if (rc)
8026 				break;
8027 		}
8028 		if (bnapi->rx_ring)
8029 			hw_coal = &bp->rx_coal;
8030 		else
8031 			hw_coal = &bp->tx_coal;
8032 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8033 	}
8034 	hwrm_req_drop(bp, req_rx);
8035 	hwrm_req_drop(bp, req_tx);
8036 	return rc;
8037 }
8038 
8039 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8040 {
8041 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8042 	struct hwrm_stat_ctx_free_input *req;
8043 	int i;
8044 
8045 	if (!bp->bnapi)
8046 		return;
8047 
8048 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8049 		return;
8050 
8051 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8052 		return;
8053 	if (BNXT_FW_MAJ(bp) <= 20) {
8054 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8055 			hwrm_req_drop(bp, req);
8056 			return;
8057 		}
8058 		hwrm_req_hold(bp, req0);
8059 	}
8060 	hwrm_req_hold(bp, req);
8061 	for (i = 0; i < bp->cp_nr_rings; i++) {
8062 		struct bnxt_napi *bnapi = bp->bnapi[i];
8063 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8064 
8065 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8066 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8067 			if (req0) {
8068 				req0->stat_ctx_id = req->stat_ctx_id;
8069 				hwrm_req_send(bp, req0);
8070 			}
8071 			hwrm_req_send(bp, req);
8072 
8073 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8074 		}
8075 	}
8076 	hwrm_req_drop(bp, req);
8077 	if (req0)
8078 		hwrm_req_drop(bp, req0);
8079 }
8080 
8081 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8082 {
8083 	struct hwrm_stat_ctx_alloc_output *resp;
8084 	struct hwrm_stat_ctx_alloc_input *req;
8085 	int rc, i;
8086 
8087 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8088 		return 0;
8089 
8090 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8091 	if (rc)
8092 		return rc;
8093 
8094 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8095 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8096 
8097 	resp = hwrm_req_hold(bp, req);
8098 	for (i = 0; i < bp->cp_nr_rings; i++) {
8099 		struct bnxt_napi *bnapi = bp->bnapi[i];
8100 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8101 
8102 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8103 
8104 		rc = hwrm_req_send(bp, req);
8105 		if (rc)
8106 			break;
8107 
8108 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8109 
8110 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8111 	}
8112 	hwrm_req_drop(bp, req);
8113 	return rc;
8114 }
8115 
8116 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8117 {
8118 	struct hwrm_func_qcfg_output *resp;
8119 	struct hwrm_func_qcfg_input *req;
8120 	u16 flags;
8121 	int rc;
8122 
8123 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8124 	if (rc)
8125 		return rc;
8126 
8127 	req->fid = cpu_to_le16(0xffff);
8128 	resp = hwrm_req_hold(bp, req);
8129 	rc = hwrm_req_send(bp, req);
8130 	if (rc)
8131 		goto func_qcfg_exit;
8132 
8133 #ifdef CONFIG_BNXT_SRIOV
8134 	if (BNXT_VF(bp)) {
8135 		struct bnxt_vf_info *vf = &bp->vf;
8136 
8137 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8138 	} else {
8139 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8140 	}
8141 #endif
8142 	flags = le16_to_cpu(resp->flags);
8143 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8144 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8145 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8146 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8147 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8148 	}
8149 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8150 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8151 
8152 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8153 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8154 
8155 	switch (resp->port_partition_type) {
8156 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8157 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8158 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8159 		bp->port_partition_type = resp->port_partition_type;
8160 		break;
8161 	}
8162 	if (bp->hwrm_spec_code < 0x10707 ||
8163 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8164 		bp->br_mode = BRIDGE_MODE_VEB;
8165 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8166 		bp->br_mode = BRIDGE_MODE_VEPA;
8167 	else
8168 		bp->br_mode = BRIDGE_MODE_UNDEF;
8169 
8170 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8171 	if (!bp->max_mtu)
8172 		bp->max_mtu = BNXT_MAX_MTU;
8173 
8174 	if (bp->db_size)
8175 		goto func_qcfg_exit;
8176 
8177 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8178 	if (BNXT_CHIP_P5(bp)) {
8179 		if (BNXT_PF(bp))
8180 			bp->db_offset = DB_PF_OFFSET_P5;
8181 		else
8182 			bp->db_offset = DB_VF_OFFSET_P5;
8183 	}
8184 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8185 				 1024);
8186 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8187 	    bp->db_size <= bp->db_offset)
8188 		bp->db_size = pci_resource_len(bp->pdev, 2);
8189 
8190 func_qcfg_exit:
8191 	hwrm_req_drop(bp, req);
8192 	return rc;
8193 }
8194 
8195 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8196 				      u8 init_val, u8 init_offset,
8197 				      bool init_mask_set)
8198 {
8199 	ctxm->init_value = init_val;
8200 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8201 	if (init_mask_set)
8202 		ctxm->init_offset = init_offset * 4;
8203 	else
8204 		ctxm->init_value = 0;
8205 }
8206 
8207 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8208 {
8209 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8210 	u16 type;
8211 
8212 	for (type = 0; type < ctx_max; type++) {
8213 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8214 		int n = 1;
8215 
8216 		if (!ctxm->max_entries)
8217 			continue;
8218 
8219 		if (ctxm->instance_bmap)
8220 			n = hweight32(ctxm->instance_bmap);
8221 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8222 		if (!ctxm->pg_info)
8223 			return -ENOMEM;
8224 	}
8225 	return 0;
8226 }
8227 
8228 #define BNXT_CTX_INIT_VALID(flags)	\
8229 	(!!((flags) &			\
8230 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8231 
8232 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8233 {
8234 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8235 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8236 	struct bnxt_ctx_mem_info *ctx;
8237 	u16 type;
8238 	int rc;
8239 
8240 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8241 	if (rc)
8242 		return rc;
8243 
8244 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8245 	if (!ctx)
8246 		return -ENOMEM;
8247 	bp->ctx = ctx;
8248 
8249 	resp = hwrm_req_hold(bp, req);
8250 
8251 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8252 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8253 		u8 init_val, init_off, i;
8254 		__le32 *p;
8255 		u32 flags;
8256 
8257 		req->type = cpu_to_le16(type);
8258 		rc = hwrm_req_send(bp, req);
8259 		if (rc)
8260 			goto ctx_done;
8261 		flags = le32_to_cpu(resp->flags);
8262 		type = le16_to_cpu(resp->next_valid_type);
8263 		if (!(flags & FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_TYPE_VALID))
8264 			continue;
8265 
8266 		ctxm->type = le16_to_cpu(resp->type);
8267 		ctxm->entry_size = le16_to_cpu(resp->entry_size);
8268 		ctxm->flags = flags;
8269 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8270 		ctxm->entry_multiple = resp->entry_multiple;
8271 		ctxm->max_entries = le32_to_cpu(resp->max_num_entries);
8272 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8273 		init_val = resp->ctx_init_value;
8274 		init_off = resp->ctx_init_offset;
8275 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8276 					  BNXT_CTX_INIT_VALID(flags));
8277 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8278 					      BNXT_MAX_SPLIT_ENTRY);
8279 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8280 		     i++, p++)
8281 			ctxm->split[i] = le32_to_cpu(*p);
8282 	}
8283 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8284 
8285 ctx_done:
8286 	hwrm_req_drop(bp, req);
8287 	return rc;
8288 }
8289 
8290 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8291 {
8292 	struct hwrm_func_backing_store_qcaps_output *resp;
8293 	struct hwrm_func_backing_store_qcaps_input *req;
8294 	int rc;
8295 
8296 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx)
8297 		return 0;
8298 
8299 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8300 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8301 
8302 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8303 	if (rc)
8304 		return rc;
8305 
8306 	resp = hwrm_req_hold(bp, req);
8307 	rc = hwrm_req_send_silent(bp, req);
8308 	if (!rc) {
8309 		struct bnxt_ctx_mem_type *ctxm;
8310 		struct bnxt_ctx_mem_info *ctx;
8311 		u8 init_val, init_idx = 0;
8312 		u16 init_mask;
8313 
8314 		ctx = bp->ctx;
8315 		if (!ctx) {
8316 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8317 			if (!ctx) {
8318 				rc = -ENOMEM;
8319 				goto ctx_err;
8320 			}
8321 			bp->ctx = ctx;
8322 		}
8323 		init_val = resp->ctx_kind_initializer;
8324 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8325 
8326 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8327 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8328 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8329 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8330 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8331 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8332 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8333 					  (init_mask & (1 << init_idx++)) != 0);
8334 
8335 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8336 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8337 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8338 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8339 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8340 					  (init_mask & (1 << init_idx++)) != 0);
8341 
8342 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8343 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8344 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8345 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8346 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8347 					  (init_mask & (1 << init_idx++)) != 0);
8348 
8349 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8350 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8351 		ctxm->max_entries = ctxm->vnic_entries +
8352 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8353 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8354 		bnxt_init_ctx_initializer(ctxm, init_val,
8355 					  resp->vnic_init_offset,
8356 					  (init_mask & (1 << init_idx++)) != 0);
8357 
8358 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8359 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8360 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8361 		bnxt_init_ctx_initializer(ctxm, init_val,
8362 					  resp->stat_init_offset,
8363 					  (init_mask & (1 << init_idx++)) != 0);
8364 
8365 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8366 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8367 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8368 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8369 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8370 		if (!ctxm->entry_multiple)
8371 			ctxm->entry_multiple = 1;
8372 
8373 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8374 
8375 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8376 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8377 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8378 		ctxm->mrav_num_entries_units =
8379 			le16_to_cpu(resp->mrav_num_entries_units);
8380 		bnxt_init_ctx_initializer(ctxm, init_val,
8381 					  resp->mrav_init_offset,
8382 					  (init_mask & (1 << init_idx++)) != 0);
8383 
8384 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8385 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8386 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8387 
8388 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8389 		if (!ctx->tqm_fp_rings_count)
8390 			ctx->tqm_fp_rings_count = bp->max_q;
8391 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8392 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8393 
8394 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8395 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8396 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8397 
8398 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8399 	} else {
8400 		rc = 0;
8401 	}
8402 ctx_err:
8403 	hwrm_req_drop(bp, req);
8404 	return rc;
8405 }
8406 
8407 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8408 				  __le64 *pg_dir)
8409 {
8410 	if (!rmem->nr_pages)
8411 		return;
8412 
8413 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8414 	if (rmem->depth >= 1) {
8415 		if (rmem->depth == 2)
8416 			*pg_attr |= 2;
8417 		else
8418 			*pg_attr |= 1;
8419 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8420 	} else {
8421 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8422 	}
8423 }
8424 
8425 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8426 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8427 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8428 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8429 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8430 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8431 
8432 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8433 {
8434 	struct hwrm_func_backing_store_cfg_input *req;
8435 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8436 	struct bnxt_ctx_pg_info *ctx_pg;
8437 	struct bnxt_ctx_mem_type *ctxm;
8438 	void **__req = (void **)&req;
8439 	u32 req_len = sizeof(*req);
8440 	__le32 *num_entries;
8441 	__le64 *pg_dir;
8442 	u32 flags = 0;
8443 	u8 *pg_attr;
8444 	u32 ena;
8445 	int rc;
8446 	int i;
8447 
8448 	if (!ctx)
8449 		return 0;
8450 
8451 	if (req_len > bp->hwrm_max_ext_req_len)
8452 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8453 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8454 	if (rc)
8455 		return rc;
8456 
8457 	req->enables = cpu_to_le32(enables);
8458 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8459 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8460 		ctx_pg = ctxm->pg_info;
8461 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8462 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8463 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8464 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8465 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8466 				      &req->qpc_pg_size_qpc_lvl,
8467 				      &req->qpc_page_dir);
8468 
8469 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8470 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8471 	}
8472 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8473 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8474 		ctx_pg = ctxm->pg_info;
8475 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8476 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8477 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8478 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8479 				      &req->srq_pg_size_srq_lvl,
8480 				      &req->srq_page_dir);
8481 	}
8482 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8483 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8484 		ctx_pg = ctxm->pg_info;
8485 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8486 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8487 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8488 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8489 				      &req->cq_pg_size_cq_lvl,
8490 				      &req->cq_page_dir);
8491 	}
8492 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8493 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8494 		ctx_pg = ctxm->pg_info;
8495 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8496 		req->vnic_num_ring_table_entries =
8497 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8498 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8499 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8500 				      &req->vnic_pg_size_vnic_lvl,
8501 				      &req->vnic_page_dir);
8502 	}
8503 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8504 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8505 		ctx_pg = ctxm->pg_info;
8506 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8507 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8508 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8509 				      &req->stat_pg_size_stat_lvl,
8510 				      &req->stat_page_dir);
8511 	}
8512 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8513 		u32 units;
8514 
8515 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8516 		ctx_pg = ctxm->pg_info;
8517 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8518 		units = ctxm->mrav_num_entries_units;
8519 		if (units) {
8520 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8521 			u32 entries;
8522 
8523 			num_mr = ctx_pg->entries - num_ah;
8524 			entries = ((num_mr / units) << 16) | (num_ah / units);
8525 			req->mrav_num_entries = cpu_to_le32(entries);
8526 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8527 		}
8528 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8529 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8530 				      &req->mrav_pg_size_mrav_lvl,
8531 				      &req->mrav_page_dir);
8532 	}
8533 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8534 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8535 		ctx_pg = ctxm->pg_info;
8536 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8537 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8538 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8539 				      &req->tim_pg_size_tim_lvl,
8540 				      &req->tim_page_dir);
8541 	}
8542 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8543 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8544 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8545 	     pg_dir = &req->tqm_sp_page_dir,
8546 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8547 	     ctx_pg = ctxm->pg_info;
8548 	     i < BNXT_MAX_TQM_RINGS;
8549 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8550 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8551 		if (!(enables & ena))
8552 			continue;
8553 
8554 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8555 		*num_entries = cpu_to_le32(ctx_pg->entries);
8556 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8557 	}
8558 	req->flags = cpu_to_le32(flags);
8559 	return hwrm_req_send(bp, req);
8560 }
8561 
8562 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8563 				  struct bnxt_ctx_pg_info *ctx_pg)
8564 {
8565 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8566 
8567 	rmem->page_size = BNXT_PAGE_SIZE;
8568 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8569 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8570 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8571 	if (rmem->depth >= 1)
8572 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8573 	return bnxt_alloc_ring(bp, rmem);
8574 }
8575 
8576 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8577 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8578 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8579 {
8580 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8581 	int rc;
8582 
8583 	if (!mem_size)
8584 		return -EINVAL;
8585 
8586 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8587 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8588 		ctx_pg->nr_pages = 0;
8589 		return -EINVAL;
8590 	}
8591 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8592 		int nr_tbls, i;
8593 
8594 		rmem->depth = 2;
8595 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8596 					     GFP_KERNEL);
8597 		if (!ctx_pg->ctx_pg_tbl)
8598 			return -ENOMEM;
8599 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8600 		rmem->nr_pages = nr_tbls;
8601 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8602 		if (rc)
8603 			return rc;
8604 		for (i = 0; i < nr_tbls; i++) {
8605 			struct bnxt_ctx_pg_info *pg_tbl;
8606 
8607 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8608 			if (!pg_tbl)
8609 				return -ENOMEM;
8610 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8611 			rmem = &pg_tbl->ring_mem;
8612 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8613 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8614 			rmem->depth = 1;
8615 			rmem->nr_pages = MAX_CTX_PAGES;
8616 			rmem->ctx_mem = ctxm;
8617 			if (i == (nr_tbls - 1)) {
8618 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8619 
8620 				if (rem)
8621 					rmem->nr_pages = rem;
8622 			}
8623 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8624 			if (rc)
8625 				break;
8626 		}
8627 	} else {
8628 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8629 		if (rmem->nr_pages > 1 || depth)
8630 			rmem->depth = 1;
8631 		rmem->ctx_mem = ctxm;
8632 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8633 	}
8634 	return rc;
8635 }
8636 
8637 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
8638 				  struct bnxt_ctx_pg_info *ctx_pg)
8639 {
8640 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8641 
8642 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
8643 	    ctx_pg->ctx_pg_tbl) {
8644 		int i, nr_tbls = rmem->nr_pages;
8645 
8646 		for (i = 0; i < nr_tbls; i++) {
8647 			struct bnxt_ctx_pg_info *pg_tbl;
8648 			struct bnxt_ring_mem_info *rmem2;
8649 
8650 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8651 			if (!pg_tbl)
8652 				continue;
8653 			rmem2 = &pg_tbl->ring_mem;
8654 			bnxt_free_ring(bp, rmem2);
8655 			ctx_pg->ctx_pg_arr[i] = NULL;
8656 			kfree(pg_tbl);
8657 			ctx_pg->ctx_pg_tbl[i] = NULL;
8658 		}
8659 		kfree(ctx_pg->ctx_pg_tbl);
8660 		ctx_pg->ctx_pg_tbl = NULL;
8661 	}
8662 	bnxt_free_ring(bp, rmem);
8663 	ctx_pg->nr_pages = 0;
8664 }
8665 
8666 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
8667 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
8668 				   u8 pg_lvl)
8669 {
8670 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8671 	int i, rc = 0, n = 1;
8672 	u32 mem_size;
8673 
8674 	if (!ctxm->entry_size || !ctx_pg)
8675 		return -EINVAL;
8676 	if (ctxm->instance_bmap)
8677 		n = hweight32(ctxm->instance_bmap);
8678 	if (ctxm->entry_multiple)
8679 		entries = roundup(entries, ctxm->entry_multiple);
8680 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
8681 	mem_size = entries * ctxm->entry_size;
8682 	for (i = 0; i < n && !rc; i++) {
8683 		ctx_pg[i].entries = entries;
8684 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
8685 					    ctxm->init_value ? ctxm : NULL);
8686 	}
8687 	return rc;
8688 }
8689 
8690 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
8691 					       struct bnxt_ctx_mem_type *ctxm,
8692 					       bool last)
8693 {
8694 	struct hwrm_func_backing_store_cfg_v2_input *req;
8695 	u32 instance_bmap = ctxm->instance_bmap;
8696 	int i, j, rc = 0, n = 1;
8697 	__le32 *p;
8698 
8699 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
8700 		return 0;
8701 
8702 	if (instance_bmap)
8703 		n = hweight32(ctxm->instance_bmap);
8704 	else
8705 		instance_bmap = 1;
8706 
8707 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
8708 	if (rc)
8709 		return rc;
8710 	hwrm_req_hold(bp, req);
8711 	req->type = cpu_to_le16(ctxm->type);
8712 	req->entry_size = cpu_to_le16(ctxm->entry_size);
8713 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
8714 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
8715 		p[i] = cpu_to_le32(ctxm->split[i]);
8716 	for (i = 0, j = 0; j < n && !rc; i++) {
8717 		struct bnxt_ctx_pg_info *ctx_pg;
8718 
8719 		if (!(instance_bmap & (1 << i)))
8720 			continue;
8721 		req->instance = cpu_to_le16(i);
8722 		ctx_pg = &ctxm->pg_info[j++];
8723 		if (!ctx_pg->entries)
8724 			continue;
8725 		req->num_entries = cpu_to_le32(ctx_pg->entries);
8726 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8727 				      &req->page_size_pbl_level,
8728 				      &req->page_dir);
8729 		if (last && j == n)
8730 			req->flags =
8731 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
8732 		rc = hwrm_req_send(bp, req);
8733 	}
8734 	hwrm_req_drop(bp, req);
8735 	return rc;
8736 }
8737 
8738 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
8739 {
8740 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8741 	struct bnxt_ctx_mem_type *ctxm;
8742 	u16 last_type;
8743 	int rc = 0;
8744 	u16 type;
8745 
8746 	if (!ena)
8747 		return 0;
8748 	else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
8749 		last_type = BNXT_CTX_MAX - 1;
8750 	else
8751 		last_type = BNXT_CTX_L2_MAX - 1;
8752 	ctx->ctx_arr[last_type].last = 1;
8753 
8754 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
8755 		ctxm = &ctx->ctx_arr[type];
8756 
8757 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
8758 		if (rc)
8759 			return rc;
8760 	}
8761 	return 0;
8762 }
8763 
8764 void bnxt_free_ctx_mem(struct bnxt *bp)
8765 {
8766 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8767 	u16 type;
8768 
8769 	if (!ctx)
8770 		return;
8771 
8772 	for (type = 0; type < BNXT_CTX_V2_MAX; type++) {
8773 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8774 		struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8775 		int i, n = 1;
8776 
8777 		if (!ctx_pg)
8778 			continue;
8779 		if (ctxm->instance_bmap)
8780 			n = hweight32(ctxm->instance_bmap);
8781 		for (i = 0; i < n; i++)
8782 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
8783 
8784 		kfree(ctx_pg);
8785 		ctxm->pg_info = NULL;
8786 	}
8787 
8788 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
8789 	kfree(ctx);
8790 	bp->ctx = NULL;
8791 }
8792 
8793 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
8794 {
8795 	struct bnxt_ctx_mem_type *ctxm;
8796 	struct bnxt_ctx_mem_info *ctx;
8797 	u32 l2_qps, qp1_qps, max_qps;
8798 	u32 ena, entries_sp, entries;
8799 	u32 srqs, max_srqs, min;
8800 	u32 num_mr, num_ah;
8801 	u32 extra_srqs = 0;
8802 	u32 extra_qps = 0;
8803 	u32 fast_qpmd_qps;
8804 	u8 pg_lvl = 1;
8805 	int i, rc;
8806 
8807 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
8808 	if (rc) {
8809 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
8810 			   rc);
8811 		return rc;
8812 	}
8813 	ctx = bp->ctx;
8814 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
8815 		return 0;
8816 
8817 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8818 	l2_qps = ctxm->qp_l2_entries;
8819 	qp1_qps = ctxm->qp_qp1_entries;
8820 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
8821 	max_qps = ctxm->max_entries;
8822 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8823 	srqs = ctxm->srq_l2_entries;
8824 	max_srqs = ctxm->max_entries;
8825 	ena = 0;
8826 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
8827 		pg_lvl = 2;
8828 		extra_qps = min_t(u32, 65536, max_qps - l2_qps - qp1_qps);
8829 		/* allocate extra qps if fw supports RoCE fast qp destroy feature */
8830 		extra_qps += fast_qpmd_qps;
8831 		extra_srqs = min_t(u32, 8192, max_srqs - srqs);
8832 		if (fast_qpmd_qps)
8833 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
8834 	}
8835 
8836 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8837 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
8838 				     pg_lvl);
8839 	if (rc)
8840 		return rc;
8841 
8842 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8843 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
8844 	if (rc)
8845 		return rc;
8846 
8847 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8848 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
8849 				     extra_qps * 2, pg_lvl);
8850 	if (rc)
8851 		return rc;
8852 
8853 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8854 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
8855 	if (rc)
8856 		return rc;
8857 
8858 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8859 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
8860 	if (rc)
8861 		return rc;
8862 
8863 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
8864 		goto skip_rdma;
8865 
8866 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8867 	/* 128K extra is needed to accommodate static AH context
8868 	 * allocation by f/w.
8869 	 */
8870 	num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
8871 	num_ah = min_t(u32, num_mr, 1024 * 128);
8872 	ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
8873 	if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
8874 		ctxm->mrav_av_entries = num_ah;
8875 
8876 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
8877 	if (rc)
8878 		return rc;
8879 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
8880 
8881 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8882 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
8883 	if (rc)
8884 		return rc;
8885 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
8886 
8887 skip_rdma:
8888 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8889 	min = ctxm->min_entries;
8890 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
8891 		     2 * (extra_qps + qp1_qps) + min;
8892 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
8893 	if (rc)
8894 		return rc;
8895 
8896 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8897 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
8898 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
8899 	if (rc)
8900 		return rc;
8901 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
8902 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
8903 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
8904 
8905 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8906 		rc = bnxt_backing_store_cfg_v2(bp, ena);
8907 	else
8908 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
8909 	if (rc) {
8910 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
8911 			   rc);
8912 		return rc;
8913 	}
8914 	ctx->flags |= BNXT_CTX_FLAG_INITED;
8915 	return 0;
8916 }
8917 
8918 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
8919 {
8920 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
8921 	u16 page_attr;
8922 	int rc;
8923 
8924 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
8925 		return 0;
8926 
8927 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
8928 	if (rc)
8929 		return rc;
8930 
8931 	if (BNXT_PAGE_SIZE == 0x2000)
8932 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
8933 	else if (BNXT_PAGE_SIZE == 0x10000)
8934 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
8935 	else
8936 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
8937 	req->pg_size_lvl = cpu_to_le16(page_attr |
8938 				       bp->fw_crash_mem->ring_mem.depth);
8939 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
8940 	req->size = cpu_to_le32(bp->fw_crash_len);
8941 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
8942 	return hwrm_req_send(bp, req);
8943 }
8944 
8945 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
8946 {
8947 	if (bp->fw_crash_mem) {
8948 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
8949 		kfree(bp->fw_crash_mem);
8950 		bp->fw_crash_mem = NULL;
8951 	}
8952 }
8953 
8954 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
8955 {
8956 	u32 mem_size = 0;
8957 	int rc;
8958 
8959 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
8960 		return 0;
8961 
8962 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
8963 	if (rc)
8964 		return rc;
8965 
8966 	mem_size = round_up(mem_size, 4);
8967 
8968 	/* keep and use the existing pages */
8969 	if (bp->fw_crash_mem &&
8970 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
8971 		goto alloc_done;
8972 
8973 	if (bp->fw_crash_mem)
8974 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
8975 	else
8976 		bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem),
8977 					   GFP_KERNEL);
8978 	if (!bp->fw_crash_mem)
8979 		return -ENOMEM;
8980 
8981 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
8982 	if (rc) {
8983 		bnxt_free_crash_dump_mem(bp);
8984 		return rc;
8985 	}
8986 
8987 alloc_done:
8988 	bp->fw_crash_len = mem_size;
8989 	return 0;
8990 }
8991 
8992 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
8993 {
8994 	struct hwrm_func_resource_qcaps_output *resp;
8995 	struct hwrm_func_resource_qcaps_input *req;
8996 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8997 	int rc;
8998 
8999 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9000 	if (rc)
9001 		return rc;
9002 
9003 	req->fid = cpu_to_le16(0xffff);
9004 	resp = hwrm_req_hold(bp, req);
9005 	rc = hwrm_req_send_silent(bp, req);
9006 	if (rc)
9007 		goto hwrm_func_resc_qcaps_exit;
9008 
9009 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9010 	if (!all)
9011 		goto hwrm_func_resc_qcaps_exit;
9012 
9013 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9014 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9015 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9016 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9017 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9018 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9019 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9020 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9021 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9022 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9023 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9024 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9025 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9026 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9027 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9028 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9029 
9030 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9031 		u16 max_msix = le16_to_cpu(resp->max_msix);
9032 
9033 		hw_resc->max_nqs = max_msix;
9034 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9035 	}
9036 
9037 	if (BNXT_PF(bp)) {
9038 		struct bnxt_pf_info *pf = &bp->pf;
9039 
9040 		pf->vf_resv_strategy =
9041 			le16_to_cpu(resp->vf_reservation_strategy);
9042 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9043 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9044 	}
9045 hwrm_func_resc_qcaps_exit:
9046 	hwrm_req_drop(bp, req);
9047 	return rc;
9048 }
9049 
9050 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9051 {
9052 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9053 	struct hwrm_port_mac_ptp_qcfg_input *req;
9054 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9055 	bool phc_cfg;
9056 	u8 flags;
9057 	int rc;
9058 
9059 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9060 		rc = -ENODEV;
9061 		goto no_ptp;
9062 	}
9063 
9064 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9065 	if (rc)
9066 		goto no_ptp;
9067 
9068 	req->port_id = cpu_to_le16(bp->pf.port_id);
9069 	resp = hwrm_req_hold(bp, req);
9070 	rc = hwrm_req_send(bp, req);
9071 	if (rc)
9072 		goto exit;
9073 
9074 	flags = resp->flags;
9075 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9076 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9077 		rc = -ENODEV;
9078 		goto exit;
9079 	}
9080 	if (!ptp) {
9081 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
9082 		if (!ptp) {
9083 			rc = -ENOMEM;
9084 			goto exit;
9085 		}
9086 		ptp->bp = bp;
9087 		bp->ptp_cfg = ptp;
9088 	}
9089 
9090 	if (flags &
9091 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9092 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9093 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9094 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9095 	} else if (BNXT_CHIP_P5(bp)) {
9096 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9097 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9098 	} else {
9099 		rc = -ENODEV;
9100 		goto exit;
9101 	}
9102 	phc_cfg = (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9103 	rc = bnxt_ptp_init(bp, phc_cfg);
9104 	if (rc)
9105 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9106 exit:
9107 	hwrm_req_drop(bp, req);
9108 	if (!rc)
9109 		return 0;
9110 
9111 no_ptp:
9112 	bnxt_ptp_clear(bp);
9113 	kfree(ptp);
9114 	bp->ptp_cfg = NULL;
9115 	return rc;
9116 }
9117 
9118 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9119 {
9120 	struct hwrm_func_qcaps_output *resp;
9121 	struct hwrm_func_qcaps_input *req;
9122 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9123 	u32 flags, flags_ext, flags_ext2;
9124 	int rc;
9125 
9126 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9127 	if (rc)
9128 		return rc;
9129 
9130 	req->fid = cpu_to_le16(0xffff);
9131 	resp = hwrm_req_hold(bp, req);
9132 	rc = hwrm_req_send(bp, req);
9133 	if (rc)
9134 		goto hwrm_func_qcaps_exit;
9135 
9136 	flags = le32_to_cpu(resp->flags);
9137 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9138 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9139 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9140 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9141 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9142 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9143 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9144 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9145 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9146 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9147 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9148 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9149 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9150 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9151 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9152 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9153 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9154 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9155 
9156 	flags_ext = le32_to_cpu(resp->flags_ext);
9157 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9158 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9159 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9160 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9161 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9162 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9163 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9164 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9165 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9166 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9167 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9168 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9169 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9170 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9171 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9172 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9173 
9174 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9175 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9176 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9177 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9178 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9179 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9180 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9181 
9182 	bp->tx_push_thresh = 0;
9183 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9184 	    BNXT_FW_MAJ(bp) > 217)
9185 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9186 
9187 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9188 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9189 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9190 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9191 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9192 	if (!hw_resc->max_hw_ring_grps)
9193 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9194 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9195 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9196 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9197 
9198 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9199 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9200 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9201 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9202 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9203 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9204 
9205 	if (BNXT_PF(bp)) {
9206 		struct bnxt_pf_info *pf = &bp->pf;
9207 
9208 		pf->fw_fid = le16_to_cpu(resp->fid);
9209 		pf->port_id = le16_to_cpu(resp->port_id);
9210 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9211 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9212 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9213 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9214 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9215 			bp->flags |= BNXT_FLAG_WOL_CAP;
9216 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9217 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9218 		} else {
9219 			bnxt_ptp_clear(bp);
9220 			kfree(bp->ptp_cfg);
9221 			bp->ptp_cfg = NULL;
9222 		}
9223 	} else {
9224 #ifdef CONFIG_BNXT_SRIOV
9225 		struct bnxt_vf_info *vf = &bp->vf;
9226 
9227 		vf->fw_fid = le16_to_cpu(resp->fid);
9228 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9229 #endif
9230 	}
9231 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9232 
9233 hwrm_func_qcaps_exit:
9234 	hwrm_req_drop(bp, req);
9235 	return rc;
9236 }
9237 
9238 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9239 {
9240 	struct hwrm_dbg_qcaps_output *resp;
9241 	struct hwrm_dbg_qcaps_input *req;
9242 	int rc;
9243 
9244 	bp->fw_dbg_cap = 0;
9245 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9246 		return;
9247 
9248 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9249 	if (rc)
9250 		return;
9251 
9252 	req->fid = cpu_to_le16(0xffff);
9253 	resp = hwrm_req_hold(bp, req);
9254 	rc = hwrm_req_send(bp, req);
9255 	if (rc)
9256 		goto hwrm_dbg_qcaps_exit;
9257 
9258 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9259 
9260 hwrm_dbg_qcaps_exit:
9261 	hwrm_req_drop(bp, req);
9262 }
9263 
9264 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9265 
9266 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9267 {
9268 	int rc;
9269 
9270 	rc = __bnxt_hwrm_func_qcaps(bp);
9271 	if (rc)
9272 		return rc;
9273 
9274 	bnxt_hwrm_dbg_qcaps(bp);
9275 
9276 	rc = bnxt_hwrm_queue_qportcfg(bp);
9277 	if (rc) {
9278 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9279 		return rc;
9280 	}
9281 	if (bp->hwrm_spec_code >= 0x10803) {
9282 		rc = bnxt_alloc_ctx_mem(bp);
9283 		if (rc)
9284 			return rc;
9285 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9286 		if (!rc)
9287 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9288 	}
9289 	return 0;
9290 }
9291 
9292 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9293 {
9294 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9295 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9296 	u32 flags;
9297 	int rc;
9298 
9299 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9300 		return 0;
9301 
9302 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9303 	if (rc)
9304 		return rc;
9305 
9306 	resp = hwrm_req_hold(bp, req);
9307 	rc = hwrm_req_send(bp, req);
9308 	if (rc)
9309 		goto hwrm_cfa_adv_qcaps_exit;
9310 
9311 	flags = le32_to_cpu(resp->flags);
9312 	if (flags &
9313 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9314 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9315 
9316 	if (flags &
9317 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9318 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9319 
9320 	if (flags &
9321 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9322 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9323 
9324 hwrm_cfa_adv_qcaps_exit:
9325 	hwrm_req_drop(bp, req);
9326 	return rc;
9327 }
9328 
9329 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9330 {
9331 	if (bp->fw_health)
9332 		return 0;
9333 
9334 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9335 	if (!bp->fw_health)
9336 		return -ENOMEM;
9337 
9338 	mutex_init(&bp->fw_health->lock);
9339 	return 0;
9340 }
9341 
9342 static int bnxt_alloc_fw_health(struct bnxt *bp)
9343 {
9344 	int rc;
9345 
9346 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9347 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9348 		return 0;
9349 
9350 	rc = __bnxt_alloc_fw_health(bp);
9351 	if (rc) {
9352 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9353 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9354 		return rc;
9355 	}
9356 
9357 	return 0;
9358 }
9359 
9360 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9361 {
9362 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9363 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9364 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9365 }
9366 
9367 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9368 {
9369 	struct bnxt_fw_health *fw_health = bp->fw_health;
9370 	u32 reg_type;
9371 
9372 	if (!fw_health)
9373 		return;
9374 
9375 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9376 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9377 		fw_health->status_reliable = false;
9378 
9379 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9380 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9381 		fw_health->resets_reliable = false;
9382 }
9383 
9384 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9385 {
9386 	void __iomem *hs;
9387 	u32 status_loc;
9388 	u32 reg_type;
9389 	u32 sig;
9390 
9391 	if (bp->fw_health)
9392 		bp->fw_health->status_reliable = false;
9393 
9394 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9395 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9396 
9397 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9398 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9399 		if (!bp->chip_num) {
9400 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9401 			bp->chip_num = readl(bp->bar0 +
9402 					     BNXT_FW_HEALTH_WIN_BASE +
9403 					     BNXT_GRC_REG_CHIP_NUM);
9404 		}
9405 		if (!BNXT_CHIP_P5_PLUS(bp))
9406 			return;
9407 
9408 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9409 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9410 	} else {
9411 		status_loc = readl(hs + offsetof(struct hcomm_status,
9412 						 fw_status_loc));
9413 	}
9414 
9415 	if (__bnxt_alloc_fw_health(bp)) {
9416 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9417 		return;
9418 	}
9419 
9420 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9421 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9422 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9423 		__bnxt_map_fw_health_reg(bp, status_loc);
9424 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9425 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9426 	}
9427 
9428 	bp->fw_health->status_reliable = true;
9429 }
9430 
9431 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9432 {
9433 	struct bnxt_fw_health *fw_health = bp->fw_health;
9434 	u32 reg_base = 0xffffffff;
9435 	int i;
9436 
9437 	bp->fw_health->status_reliable = false;
9438 	bp->fw_health->resets_reliable = false;
9439 	/* Only pre-map the monitoring GRC registers using window 3 */
9440 	for (i = 0; i < 4; i++) {
9441 		u32 reg = fw_health->regs[i];
9442 
9443 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9444 			continue;
9445 		if (reg_base == 0xffffffff)
9446 			reg_base = reg & BNXT_GRC_BASE_MASK;
9447 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9448 			return -ERANGE;
9449 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9450 	}
9451 	bp->fw_health->status_reliable = true;
9452 	bp->fw_health->resets_reliable = true;
9453 	if (reg_base == 0xffffffff)
9454 		return 0;
9455 
9456 	__bnxt_map_fw_health_reg(bp, reg_base);
9457 	return 0;
9458 }
9459 
9460 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9461 {
9462 	if (!bp->fw_health)
9463 		return;
9464 
9465 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9466 		bp->fw_health->status_reliable = true;
9467 		bp->fw_health->resets_reliable = true;
9468 	} else {
9469 		bnxt_try_map_fw_health_reg(bp);
9470 	}
9471 }
9472 
9473 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9474 {
9475 	struct bnxt_fw_health *fw_health = bp->fw_health;
9476 	struct hwrm_error_recovery_qcfg_output *resp;
9477 	struct hwrm_error_recovery_qcfg_input *req;
9478 	int rc, i;
9479 
9480 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9481 		return 0;
9482 
9483 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9484 	if (rc)
9485 		return rc;
9486 
9487 	resp = hwrm_req_hold(bp, req);
9488 	rc = hwrm_req_send(bp, req);
9489 	if (rc)
9490 		goto err_recovery_out;
9491 	fw_health->flags = le32_to_cpu(resp->flags);
9492 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
9493 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
9494 		rc = -EINVAL;
9495 		goto err_recovery_out;
9496 	}
9497 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
9498 	fw_health->master_func_wait_dsecs =
9499 		le32_to_cpu(resp->master_func_wait_period);
9500 	fw_health->normal_func_wait_dsecs =
9501 		le32_to_cpu(resp->normal_func_wait_period);
9502 	fw_health->post_reset_wait_dsecs =
9503 		le32_to_cpu(resp->master_func_wait_period_after_reset);
9504 	fw_health->post_reset_max_wait_dsecs =
9505 		le32_to_cpu(resp->max_bailout_time_after_reset);
9506 	fw_health->regs[BNXT_FW_HEALTH_REG] =
9507 		le32_to_cpu(resp->fw_health_status_reg);
9508 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
9509 		le32_to_cpu(resp->fw_heartbeat_reg);
9510 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
9511 		le32_to_cpu(resp->fw_reset_cnt_reg);
9512 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
9513 		le32_to_cpu(resp->reset_inprogress_reg);
9514 	fw_health->fw_reset_inprog_reg_mask =
9515 		le32_to_cpu(resp->reset_inprogress_reg_mask);
9516 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
9517 	if (fw_health->fw_reset_seq_cnt >= 16) {
9518 		rc = -EINVAL;
9519 		goto err_recovery_out;
9520 	}
9521 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
9522 		fw_health->fw_reset_seq_regs[i] =
9523 			le32_to_cpu(resp->reset_reg[i]);
9524 		fw_health->fw_reset_seq_vals[i] =
9525 			le32_to_cpu(resp->reset_reg_val[i]);
9526 		fw_health->fw_reset_seq_delay_msec[i] =
9527 			resp->delay_after_reset[i];
9528 	}
9529 err_recovery_out:
9530 	hwrm_req_drop(bp, req);
9531 	if (!rc)
9532 		rc = bnxt_map_fw_health_regs(bp);
9533 	if (rc)
9534 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9535 	return rc;
9536 }
9537 
9538 static int bnxt_hwrm_func_reset(struct bnxt *bp)
9539 {
9540 	struct hwrm_func_reset_input *req;
9541 	int rc;
9542 
9543 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
9544 	if (rc)
9545 		return rc;
9546 
9547 	req->enables = 0;
9548 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
9549 	return hwrm_req_send(bp, req);
9550 }
9551 
9552 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
9553 {
9554 	struct hwrm_nvm_get_dev_info_output nvm_info;
9555 
9556 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
9557 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
9558 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
9559 			 nvm_info.nvm_cfg_ver_upd);
9560 }
9561 
9562 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
9563 {
9564 	struct hwrm_queue_qportcfg_output *resp;
9565 	struct hwrm_queue_qportcfg_input *req;
9566 	u8 i, j, *qptr;
9567 	bool no_rdma;
9568 	int rc = 0;
9569 
9570 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
9571 	if (rc)
9572 		return rc;
9573 
9574 	resp = hwrm_req_hold(bp, req);
9575 	rc = hwrm_req_send(bp, req);
9576 	if (rc)
9577 		goto qportcfg_exit;
9578 
9579 	if (!resp->max_configurable_queues) {
9580 		rc = -EINVAL;
9581 		goto qportcfg_exit;
9582 	}
9583 	bp->max_tc = resp->max_configurable_queues;
9584 	bp->max_lltc = resp->max_configurable_lossless_queues;
9585 	if (bp->max_tc > BNXT_MAX_QUEUE)
9586 		bp->max_tc = BNXT_MAX_QUEUE;
9587 
9588 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
9589 	qptr = &resp->queue_id0;
9590 	for (i = 0, j = 0; i < bp->max_tc; i++) {
9591 		bp->q_info[j].queue_id = *qptr;
9592 		bp->q_ids[i] = *qptr++;
9593 		bp->q_info[j].queue_profile = *qptr++;
9594 		bp->tc_to_qidx[j] = j;
9595 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
9596 		    (no_rdma && BNXT_PF(bp)))
9597 			j++;
9598 	}
9599 	bp->max_q = bp->max_tc;
9600 	bp->max_tc = max_t(u8, j, 1);
9601 
9602 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
9603 		bp->max_tc = 1;
9604 
9605 	if (bp->max_lltc > bp->max_tc)
9606 		bp->max_lltc = bp->max_tc;
9607 
9608 qportcfg_exit:
9609 	hwrm_req_drop(bp, req);
9610 	return rc;
9611 }
9612 
9613 static int bnxt_hwrm_poll(struct bnxt *bp)
9614 {
9615 	struct hwrm_ver_get_input *req;
9616 	int rc;
9617 
9618 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9619 	if (rc)
9620 		return rc;
9621 
9622 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9623 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9624 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9625 
9626 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
9627 	rc = hwrm_req_send(bp, req);
9628 	return rc;
9629 }
9630 
9631 static int bnxt_hwrm_ver_get(struct bnxt *bp)
9632 {
9633 	struct hwrm_ver_get_output *resp;
9634 	struct hwrm_ver_get_input *req;
9635 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
9636 	u32 dev_caps_cfg, hwrm_ver;
9637 	int rc, len;
9638 
9639 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9640 	if (rc)
9641 		return rc;
9642 
9643 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
9644 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
9645 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9646 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9647 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9648 
9649 	resp = hwrm_req_hold(bp, req);
9650 	rc = hwrm_req_send(bp, req);
9651 	if (rc)
9652 		goto hwrm_ver_get_exit;
9653 
9654 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
9655 
9656 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
9657 			     resp->hwrm_intf_min_8b << 8 |
9658 			     resp->hwrm_intf_upd_8b;
9659 	if (resp->hwrm_intf_maj_8b < 1) {
9660 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
9661 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
9662 			    resp->hwrm_intf_upd_8b);
9663 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
9664 	}
9665 
9666 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
9667 			HWRM_VERSION_UPDATE;
9668 
9669 	if (bp->hwrm_spec_code > hwrm_ver)
9670 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
9671 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
9672 			 HWRM_VERSION_UPDATE);
9673 	else
9674 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
9675 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
9676 			 resp->hwrm_intf_upd_8b);
9677 
9678 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
9679 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
9680 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
9681 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
9682 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
9683 		len = FW_VER_STR_LEN;
9684 	} else {
9685 		fw_maj = resp->hwrm_fw_maj_8b;
9686 		fw_min = resp->hwrm_fw_min_8b;
9687 		fw_bld = resp->hwrm_fw_bld_8b;
9688 		fw_rsv = resp->hwrm_fw_rsvd_8b;
9689 		len = BC_HWRM_STR_LEN;
9690 	}
9691 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
9692 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
9693 		 fw_rsv);
9694 
9695 	if (strlen(resp->active_pkg_name)) {
9696 		int fw_ver_len = strlen(bp->fw_ver_str);
9697 
9698 		snprintf(bp->fw_ver_str + fw_ver_len,
9699 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
9700 			 resp->active_pkg_name);
9701 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
9702 	}
9703 
9704 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
9705 	if (!bp->hwrm_cmd_timeout)
9706 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
9707 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
9708 	if (!bp->hwrm_cmd_max_timeout)
9709 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
9710 	else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT)
9711 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n",
9712 			    bp->hwrm_cmd_max_timeout / 1000);
9713 
9714 	if (resp->hwrm_intf_maj_8b >= 1) {
9715 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
9716 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
9717 	}
9718 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
9719 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
9720 
9721 	bp->chip_num = le16_to_cpu(resp->chip_num);
9722 	bp->chip_rev = resp->chip_rev;
9723 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
9724 	    !resp->chip_metal)
9725 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
9726 
9727 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
9728 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
9729 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
9730 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
9731 
9732 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
9733 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
9734 
9735 	if (dev_caps_cfg &
9736 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
9737 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
9738 
9739 	if (dev_caps_cfg &
9740 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
9741 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
9742 
9743 	if (dev_caps_cfg &
9744 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
9745 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
9746 
9747 hwrm_ver_get_exit:
9748 	hwrm_req_drop(bp, req);
9749 	return rc;
9750 }
9751 
9752 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
9753 {
9754 	struct hwrm_fw_set_time_input *req;
9755 	struct tm tm;
9756 	time64_t now = ktime_get_real_seconds();
9757 	int rc;
9758 
9759 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
9760 	    bp->hwrm_spec_code < 0x10400)
9761 		return -EOPNOTSUPP;
9762 
9763 	time64_to_tm(now, 0, &tm);
9764 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
9765 	if (rc)
9766 		return rc;
9767 
9768 	req->year = cpu_to_le16(1900 + tm.tm_year);
9769 	req->month = 1 + tm.tm_mon;
9770 	req->day = tm.tm_mday;
9771 	req->hour = tm.tm_hour;
9772 	req->minute = tm.tm_min;
9773 	req->second = tm.tm_sec;
9774 	return hwrm_req_send(bp, req);
9775 }
9776 
9777 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
9778 {
9779 	u64 sw_tmp;
9780 
9781 	hw &= mask;
9782 	sw_tmp = (*sw & ~mask) | hw;
9783 	if (hw < (*sw & mask))
9784 		sw_tmp += mask + 1;
9785 	WRITE_ONCE(*sw, sw_tmp);
9786 }
9787 
9788 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
9789 				    int count, bool ignore_zero)
9790 {
9791 	int i;
9792 
9793 	for (i = 0; i < count; i++) {
9794 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
9795 
9796 		if (ignore_zero && !hw)
9797 			continue;
9798 
9799 		if (masks[i] == -1ULL)
9800 			sw_stats[i] = hw;
9801 		else
9802 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
9803 	}
9804 }
9805 
9806 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
9807 {
9808 	if (!stats->hw_stats)
9809 		return;
9810 
9811 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
9812 				stats->hw_masks, stats->len / 8, false);
9813 }
9814 
9815 static void bnxt_accumulate_all_stats(struct bnxt *bp)
9816 {
9817 	struct bnxt_stats_mem *ring0_stats;
9818 	bool ignore_zero = false;
9819 	int i;
9820 
9821 	/* Chip bug.  Counter intermittently becomes 0. */
9822 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
9823 		ignore_zero = true;
9824 
9825 	for (i = 0; i < bp->cp_nr_rings; i++) {
9826 		struct bnxt_napi *bnapi = bp->bnapi[i];
9827 		struct bnxt_cp_ring_info *cpr;
9828 		struct bnxt_stats_mem *stats;
9829 
9830 		cpr = &bnapi->cp_ring;
9831 		stats = &cpr->stats;
9832 		if (!i)
9833 			ring0_stats = stats;
9834 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
9835 					ring0_stats->hw_masks,
9836 					ring0_stats->len / 8, ignore_zero);
9837 	}
9838 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
9839 		struct bnxt_stats_mem *stats = &bp->port_stats;
9840 		__le64 *hw_stats = stats->hw_stats;
9841 		u64 *sw_stats = stats->sw_stats;
9842 		u64 *masks = stats->hw_masks;
9843 		int cnt;
9844 
9845 		cnt = sizeof(struct rx_port_stats) / 8;
9846 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
9847 
9848 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9849 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9850 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9851 		cnt = sizeof(struct tx_port_stats) / 8;
9852 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
9853 	}
9854 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
9855 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
9856 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
9857 	}
9858 }
9859 
9860 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
9861 {
9862 	struct hwrm_port_qstats_input *req;
9863 	struct bnxt_pf_info *pf = &bp->pf;
9864 	int rc;
9865 
9866 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
9867 		return 0;
9868 
9869 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
9870 		return -EOPNOTSUPP;
9871 
9872 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
9873 	if (rc)
9874 		return rc;
9875 
9876 	req->flags = flags;
9877 	req->port_id = cpu_to_le16(pf->port_id);
9878 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
9879 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
9880 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
9881 	return hwrm_req_send(bp, req);
9882 }
9883 
9884 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
9885 {
9886 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
9887 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
9888 	struct hwrm_port_qstats_ext_output *resp_qs;
9889 	struct hwrm_port_qstats_ext_input *req_qs;
9890 	struct bnxt_pf_info *pf = &bp->pf;
9891 	u32 tx_stat_size;
9892 	int rc;
9893 
9894 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
9895 		return 0;
9896 
9897 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
9898 		return -EOPNOTSUPP;
9899 
9900 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
9901 	if (rc)
9902 		return rc;
9903 
9904 	req_qs->flags = flags;
9905 	req_qs->port_id = cpu_to_le16(pf->port_id);
9906 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
9907 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
9908 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
9909 		       sizeof(struct tx_port_stats_ext) : 0;
9910 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
9911 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
9912 	resp_qs = hwrm_req_hold(bp, req_qs);
9913 	rc = hwrm_req_send(bp, req_qs);
9914 	if (!rc) {
9915 		bp->fw_rx_stats_ext_size =
9916 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
9917 		if (BNXT_FW_MAJ(bp) < 220 &&
9918 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
9919 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
9920 
9921 		bp->fw_tx_stats_ext_size = tx_stat_size ?
9922 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
9923 	} else {
9924 		bp->fw_rx_stats_ext_size = 0;
9925 		bp->fw_tx_stats_ext_size = 0;
9926 	}
9927 	hwrm_req_drop(bp, req_qs);
9928 
9929 	if (flags)
9930 		return rc;
9931 
9932 	if (bp->fw_tx_stats_ext_size <=
9933 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
9934 		bp->pri2cos_valid = 0;
9935 		return rc;
9936 	}
9937 
9938 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
9939 	if (rc)
9940 		return rc;
9941 
9942 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
9943 
9944 	resp_qc = hwrm_req_hold(bp, req_qc);
9945 	rc = hwrm_req_send(bp, req_qc);
9946 	if (!rc) {
9947 		u8 *pri2cos;
9948 		int i, j;
9949 
9950 		pri2cos = &resp_qc->pri0_cos_queue_id;
9951 		for (i = 0; i < 8; i++) {
9952 			u8 queue_id = pri2cos[i];
9953 			u8 queue_idx;
9954 
9955 			/* Per port queue IDs start from 0, 10, 20, etc */
9956 			queue_idx = queue_id % 10;
9957 			if (queue_idx > BNXT_MAX_QUEUE) {
9958 				bp->pri2cos_valid = false;
9959 				hwrm_req_drop(bp, req_qc);
9960 				return rc;
9961 			}
9962 			for (j = 0; j < bp->max_q; j++) {
9963 				if (bp->q_ids[j] == queue_id)
9964 					bp->pri2cos_idx[i] = queue_idx;
9965 			}
9966 		}
9967 		bp->pri2cos_valid = true;
9968 	}
9969 	hwrm_req_drop(bp, req_qc);
9970 
9971 	return rc;
9972 }
9973 
9974 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
9975 {
9976 	bnxt_hwrm_tunnel_dst_port_free(bp,
9977 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
9978 	bnxt_hwrm_tunnel_dst_port_free(bp,
9979 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
9980 }
9981 
9982 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
9983 {
9984 	int rc, i;
9985 	u32 tpa_flags = 0;
9986 
9987 	if (set_tpa)
9988 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
9989 	else if (BNXT_NO_FW_ACCESS(bp))
9990 		return 0;
9991 	for (i = 0; i < bp->nr_vnics; i++) {
9992 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
9993 		if (rc) {
9994 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
9995 				   i, rc);
9996 			return rc;
9997 		}
9998 	}
9999 	return 0;
10000 }
10001 
10002 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10003 {
10004 	int i;
10005 
10006 	for (i = 0; i < bp->nr_vnics; i++)
10007 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10008 }
10009 
10010 static void bnxt_clear_vnic(struct bnxt *bp)
10011 {
10012 	if (!bp->vnic_info)
10013 		return;
10014 
10015 	bnxt_hwrm_clear_vnic_filter(bp);
10016 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10017 		/* clear all RSS setting before free vnic ctx */
10018 		bnxt_hwrm_clear_vnic_rss(bp);
10019 		bnxt_hwrm_vnic_ctx_free(bp);
10020 	}
10021 	/* before free the vnic, undo the vnic tpa settings */
10022 	if (bp->flags & BNXT_FLAG_TPA)
10023 		bnxt_set_tpa(bp, false);
10024 	bnxt_hwrm_vnic_free(bp);
10025 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10026 		bnxt_hwrm_vnic_ctx_free(bp);
10027 }
10028 
10029 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10030 				    bool irq_re_init)
10031 {
10032 	bnxt_clear_vnic(bp);
10033 	bnxt_hwrm_ring_free(bp, close_path);
10034 	bnxt_hwrm_ring_grp_free(bp);
10035 	if (irq_re_init) {
10036 		bnxt_hwrm_stat_ctx_free(bp);
10037 		bnxt_hwrm_free_tunnel_ports(bp);
10038 	}
10039 }
10040 
10041 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10042 {
10043 	struct hwrm_func_cfg_input *req;
10044 	u8 evb_mode;
10045 	int rc;
10046 
10047 	if (br_mode == BRIDGE_MODE_VEB)
10048 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10049 	else if (br_mode == BRIDGE_MODE_VEPA)
10050 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10051 	else
10052 		return -EINVAL;
10053 
10054 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10055 	if (rc)
10056 		return rc;
10057 
10058 	req->fid = cpu_to_le16(0xffff);
10059 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10060 	req->evb_mode = evb_mode;
10061 	return hwrm_req_send(bp, req);
10062 }
10063 
10064 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10065 {
10066 	struct hwrm_func_cfg_input *req;
10067 	int rc;
10068 
10069 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10070 		return 0;
10071 
10072 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10073 	if (rc)
10074 		return rc;
10075 
10076 	req->fid = cpu_to_le16(0xffff);
10077 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10078 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10079 	if (size == 128)
10080 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10081 
10082 	return hwrm_req_send(bp, req);
10083 }
10084 
10085 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10086 {
10087 	int rc;
10088 
10089 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10090 		goto skip_rss_ctx;
10091 
10092 	/* allocate context for vnic */
10093 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10094 	if (rc) {
10095 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10096 			   vnic->vnic_id, rc);
10097 		goto vnic_setup_err;
10098 	}
10099 	bp->rsscos_nr_ctxs++;
10100 
10101 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10102 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10103 		if (rc) {
10104 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10105 				   vnic->vnic_id, rc);
10106 			goto vnic_setup_err;
10107 		}
10108 		bp->rsscos_nr_ctxs++;
10109 	}
10110 
10111 skip_rss_ctx:
10112 	/* configure default vnic, ring grp */
10113 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10114 	if (rc) {
10115 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10116 			   vnic->vnic_id, rc);
10117 		goto vnic_setup_err;
10118 	}
10119 
10120 	/* Enable RSS hashing on vnic */
10121 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10122 	if (rc) {
10123 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10124 			   vnic->vnic_id, rc);
10125 		goto vnic_setup_err;
10126 	}
10127 
10128 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10129 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10130 		if (rc) {
10131 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10132 				   vnic->vnic_id, rc);
10133 		}
10134 	}
10135 
10136 vnic_setup_err:
10137 	return rc;
10138 }
10139 
10140 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10141 			  u8 valid)
10142 {
10143 	struct hwrm_vnic_update_input *req;
10144 	int rc;
10145 
10146 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10147 	if (rc)
10148 		return rc;
10149 
10150 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10151 
10152 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10153 		req->mru = cpu_to_le16(vnic->mru);
10154 
10155 	req->enables = cpu_to_le32(valid);
10156 
10157 	return hwrm_req_send(bp, req);
10158 }
10159 
10160 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10161 {
10162 	int rc;
10163 
10164 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10165 	if (rc) {
10166 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10167 			   vnic->vnic_id, rc);
10168 		return rc;
10169 	}
10170 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10171 	if (rc)
10172 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10173 			   vnic->vnic_id, rc);
10174 	return rc;
10175 }
10176 
10177 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10178 {
10179 	int rc, i, nr_ctxs;
10180 
10181 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10182 	for (i = 0; i < nr_ctxs; i++) {
10183 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10184 		if (rc) {
10185 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10186 				   vnic->vnic_id, i, rc);
10187 			break;
10188 		}
10189 		bp->rsscos_nr_ctxs++;
10190 	}
10191 	if (i < nr_ctxs)
10192 		return -ENOMEM;
10193 
10194 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10195 	if (rc)
10196 		return rc;
10197 
10198 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10199 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10200 		if (rc) {
10201 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10202 				   vnic->vnic_id, rc);
10203 		}
10204 	}
10205 	return rc;
10206 }
10207 
10208 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10209 {
10210 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10211 		return __bnxt_setup_vnic_p5(bp, vnic);
10212 	else
10213 		return __bnxt_setup_vnic(bp, vnic);
10214 }
10215 
10216 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10217 				     struct bnxt_vnic_info *vnic,
10218 				     u16 start_rx_ring_idx, int rx_rings)
10219 {
10220 	int rc;
10221 
10222 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10223 	if (rc) {
10224 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10225 			   vnic->vnic_id, rc);
10226 		return rc;
10227 	}
10228 	return bnxt_setup_vnic(bp, vnic);
10229 }
10230 
10231 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10232 {
10233 	struct bnxt_vnic_info *vnic;
10234 	int i, rc = 0;
10235 
10236 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10237 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10238 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10239 	}
10240 
10241 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10242 		return 0;
10243 
10244 	for (i = 0; i < bp->rx_nr_rings; i++) {
10245 		u16 vnic_id = i + 1;
10246 		u16 ring_id = i;
10247 
10248 		if (vnic_id >= bp->nr_vnics)
10249 			break;
10250 
10251 		vnic = &bp->vnic_info[vnic_id];
10252 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10253 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10254 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10255 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10256 			break;
10257 	}
10258 	return rc;
10259 }
10260 
10261 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10262 			  bool all)
10263 {
10264 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10265 	struct bnxt_filter_base *usr_fltr, *tmp;
10266 	struct bnxt_ntuple_filter *ntp_fltr;
10267 	int i;
10268 
10269 	if (netif_running(bp->dev)) {
10270 		bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10271 		for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10272 			if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10273 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10274 		}
10275 	}
10276 	if (!all)
10277 		return;
10278 
10279 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10280 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10281 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10282 			ntp_fltr = container_of(usr_fltr,
10283 						struct bnxt_ntuple_filter,
10284 						base);
10285 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10286 			bnxt_del_ntp_filter(bp, ntp_fltr);
10287 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10288 		}
10289 	}
10290 
10291 	if (vnic->rss_table)
10292 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10293 				  vnic->rss_table,
10294 				  vnic->rss_table_dma_addr);
10295 	bp->num_rss_ctx--;
10296 }
10297 
10298 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10299 {
10300 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10301 	struct ethtool_rxfh_context *ctx;
10302 	unsigned long context;
10303 
10304 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10305 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10306 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10307 
10308 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10309 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10310 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10311 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10312 				   rss_ctx->index);
10313 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10314 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
10315 		}
10316 	}
10317 }
10318 
10319 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
10320 {
10321 	struct ethtool_rxfh_context *ctx;
10322 	unsigned long context;
10323 
10324 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10325 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10326 
10327 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
10328 	}
10329 }
10330 
10331 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
10332 static bool bnxt_promisc_ok(struct bnxt *bp)
10333 {
10334 #ifdef CONFIG_BNXT_SRIOV
10335 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10336 		return false;
10337 #endif
10338 	return true;
10339 }
10340 
10341 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10342 {
10343 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10344 	unsigned int rc = 0;
10345 
10346 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10347 	if (rc) {
10348 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10349 			   rc);
10350 		return rc;
10351 	}
10352 
10353 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10354 	if (rc) {
10355 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10356 			   rc);
10357 		return rc;
10358 	}
10359 	return rc;
10360 }
10361 
10362 static int bnxt_cfg_rx_mode(struct bnxt *);
10363 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10364 
10365 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10366 {
10367 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10368 	int rc = 0;
10369 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10370 
10371 	if (irq_re_init) {
10372 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10373 		if (rc) {
10374 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10375 				   rc);
10376 			goto err_out;
10377 		}
10378 	}
10379 
10380 	rc = bnxt_hwrm_ring_alloc(bp);
10381 	if (rc) {
10382 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10383 		goto err_out;
10384 	}
10385 
10386 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10387 	if (rc) {
10388 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10389 		goto err_out;
10390 	}
10391 
10392 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10393 		rx_nr_rings--;
10394 
10395 	/* default vnic 0 */
10396 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10397 	if (rc) {
10398 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10399 		goto err_out;
10400 	}
10401 
10402 	if (BNXT_VF(bp))
10403 		bnxt_hwrm_func_qcfg(bp);
10404 
10405 	rc = bnxt_setup_vnic(bp, vnic);
10406 	if (rc)
10407 		goto err_out;
10408 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10409 		bnxt_hwrm_update_rss_hash_cfg(bp);
10410 
10411 	if (bp->flags & BNXT_FLAG_RFS) {
10412 		rc = bnxt_alloc_rfs_vnics(bp);
10413 		if (rc)
10414 			goto err_out;
10415 	}
10416 
10417 	if (bp->flags & BNXT_FLAG_TPA) {
10418 		rc = bnxt_set_tpa(bp, true);
10419 		if (rc)
10420 			goto err_out;
10421 	}
10422 
10423 	if (BNXT_VF(bp))
10424 		bnxt_update_vf_mac(bp);
10425 
10426 	/* Filter for default vnic 0 */
10427 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
10428 	if (rc) {
10429 		if (BNXT_VF(bp) && rc == -ENODEV)
10430 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
10431 		else
10432 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
10433 		goto err_out;
10434 	}
10435 	vnic->uc_filter_count = 1;
10436 
10437 	vnic->rx_mask = 0;
10438 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
10439 		goto skip_rx_mask;
10440 
10441 	if (bp->dev->flags & IFF_BROADCAST)
10442 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10443 
10444 	if (bp->dev->flags & IFF_PROMISC)
10445 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10446 
10447 	if (bp->dev->flags & IFF_ALLMULTI) {
10448 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10449 		vnic->mc_list_count = 0;
10450 	} else if (bp->dev->flags & IFF_MULTICAST) {
10451 		u32 mask = 0;
10452 
10453 		bnxt_mc_list_updated(bp, &mask);
10454 		vnic->rx_mask |= mask;
10455 	}
10456 
10457 	rc = bnxt_cfg_rx_mode(bp);
10458 	if (rc)
10459 		goto err_out;
10460 
10461 skip_rx_mask:
10462 	rc = bnxt_hwrm_set_coal(bp);
10463 	if (rc)
10464 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
10465 				rc);
10466 
10467 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10468 		rc = bnxt_setup_nitroa0_vnic(bp);
10469 		if (rc)
10470 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
10471 				   rc);
10472 	}
10473 
10474 	if (BNXT_VF(bp)) {
10475 		bnxt_hwrm_func_qcfg(bp);
10476 		netdev_update_features(bp->dev);
10477 	}
10478 
10479 	return 0;
10480 
10481 err_out:
10482 	bnxt_hwrm_resource_free(bp, 0, true);
10483 
10484 	return rc;
10485 }
10486 
10487 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
10488 {
10489 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
10490 	return 0;
10491 }
10492 
10493 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
10494 {
10495 	bnxt_init_cp_rings(bp);
10496 	bnxt_init_rx_rings(bp);
10497 	bnxt_init_tx_rings(bp);
10498 	bnxt_init_ring_grps(bp, irq_re_init);
10499 	bnxt_init_vnics(bp);
10500 
10501 	return bnxt_init_chip(bp, irq_re_init);
10502 }
10503 
10504 static int bnxt_set_real_num_queues(struct bnxt *bp)
10505 {
10506 	int rc;
10507 	struct net_device *dev = bp->dev;
10508 
10509 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
10510 					  bp->tx_nr_rings_xdp);
10511 	if (rc)
10512 		return rc;
10513 
10514 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
10515 	if (rc)
10516 		return rc;
10517 
10518 #ifdef CONFIG_RFS_ACCEL
10519 	if (bp->flags & BNXT_FLAG_RFS)
10520 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
10521 #endif
10522 
10523 	return rc;
10524 }
10525 
10526 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10527 			     bool shared)
10528 {
10529 	int _rx = *rx, _tx = *tx;
10530 
10531 	if (shared) {
10532 		*rx = min_t(int, _rx, max);
10533 		*tx = min_t(int, _tx, max);
10534 	} else {
10535 		if (max < 2)
10536 			return -ENOMEM;
10537 
10538 		while (_rx + _tx > max) {
10539 			if (_rx > _tx && _rx > 1)
10540 				_rx--;
10541 			else if (_tx > 1)
10542 				_tx--;
10543 		}
10544 		*rx = _rx;
10545 		*tx = _tx;
10546 	}
10547 	return 0;
10548 }
10549 
10550 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
10551 {
10552 	return (tx - tx_xdp) / tx_sets + tx_xdp;
10553 }
10554 
10555 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
10556 {
10557 	int tcs = bp->num_tc;
10558 
10559 	if (!tcs)
10560 		tcs = 1;
10561 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
10562 }
10563 
10564 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
10565 {
10566 	int tcs = bp->num_tc;
10567 
10568 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
10569 	       bp->tx_nr_rings_xdp;
10570 }
10571 
10572 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10573 			   bool sh)
10574 {
10575 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
10576 
10577 	if (tx_cp != *tx) {
10578 		int tx_saved = tx_cp, rc;
10579 
10580 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
10581 		if (rc)
10582 			return rc;
10583 		if (tx_cp != tx_saved)
10584 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
10585 		return 0;
10586 	}
10587 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
10588 }
10589 
10590 static void bnxt_setup_msix(struct bnxt *bp)
10591 {
10592 	const int len = sizeof(bp->irq_tbl[0].name);
10593 	struct net_device *dev = bp->dev;
10594 	int tcs, i;
10595 
10596 	tcs = bp->num_tc;
10597 	if (tcs) {
10598 		int i, off, count;
10599 
10600 		for (i = 0; i < tcs; i++) {
10601 			count = bp->tx_nr_rings_per_tc;
10602 			off = BNXT_TC_TO_RING_BASE(bp, i);
10603 			netdev_set_tc_queue(dev, i, count, off);
10604 		}
10605 	}
10606 
10607 	for (i = 0; i < bp->cp_nr_rings; i++) {
10608 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10609 		char *attr;
10610 
10611 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
10612 			attr = "TxRx";
10613 		else if (i < bp->rx_nr_rings)
10614 			attr = "rx";
10615 		else
10616 			attr = "tx";
10617 
10618 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
10619 			 attr, i);
10620 		bp->irq_tbl[map_idx].handler = bnxt_msix;
10621 	}
10622 }
10623 
10624 static int bnxt_init_int_mode(struct bnxt *bp);
10625 
10626 static int bnxt_change_msix(struct bnxt *bp, int total)
10627 {
10628 	struct msi_map map;
10629 	int i;
10630 
10631 	/* add MSIX to the end if needed */
10632 	for (i = bp->total_irqs; i < total; i++) {
10633 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
10634 		if (map.index < 0)
10635 			return bp->total_irqs;
10636 		bp->irq_tbl[i].vector = map.virq;
10637 		bp->total_irqs++;
10638 	}
10639 
10640 	/* trim MSIX from the end if needed */
10641 	for (i = bp->total_irqs; i > total; i--) {
10642 		map.index = i - 1;
10643 		map.virq = bp->irq_tbl[i - 1].vector;
10644 		pci_msix_free_irq(bp->pdev, map);
10645 		bp->total_irqs--;
10646 	}
10647 	return bp->total_irqs;
10648 }
10649 
10650 static int bnxt_setup_int_mode(struct bnxt *bp)
10651 {
10652 	int rc;
10653 
10654 	if (!bp->irq_tbl) {
10655 		rc = bnxt_init_int_mode(bp);
10656 		if (rc || !bp->irq_tbl)
10657 			return rc ?: -ENODEV;
10658 	}
10659 
10660 	bnxt_setup_msix(bp);
10661 
10662 	rc = bnxt_set_real_num_queues(bp);
10663 	return rc;
10664 }
10665 
10666 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
10667 {
10668 	return bp->hw_resc.max_rsscos_ctxs;
10669 }
10670 
10671 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
10672 {
10673 	return bp->hw_resc.max_vnics;
10674 }
10675 
10676 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
10677 {
10678 	return bp->hw_resc.max_stat_ctxs;
10679 }
10680 
10681 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
10682 {
10683 	return bp->hw_resc.max_cp_rings;
10684 }
10685 
10686 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
10687 {
10688 	unsigned int cp = bp->hw_resc.max_cp_rings;
10689 
10690 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
10691 		cp -= bnxt_get_ulp_msix_num(bp);
10692 
10693 	return cp;
10694 }
10695 
10696 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
10697 {
10698 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
10699 
10700 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10701 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
10702 
10703 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
10704 }
10705 
10706 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
10707 {
10708 	bp->hw_resc.max_irqs = max_irqs;
10709 }
10710 
10711 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
10712 {
10713 	unsigned int cp;
10714 
10715 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
10716 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10717 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
10718 	else
10719 		return cp - bp->cp_nr_rings;
10720 }
10721 
10722 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
10723 {
10724 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
10725 }
10726 
10727 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
10728 {
10729 	int max_irq = bnxt_get_max_func_irqs(bp);
10730 	int total_req = bp->cp_nr_rings + num;
10731 
10732 	if (max_irq < total_req) {
10733 		num = max_irq - bp->cp_nr_rings;
10734 		if (num <= 0)
10735 			return 0;
10736 	}
10737 	return num;
10738 }
10739 
10740 static int bnxt_get_num_msix(struct bnxt *bp)
10741 {
10742 	if (!BNXT_NEW_RM(bp))
10743 		return bnxt_get_max_func_irqs(bp);
10744 
10745 	return bnxt_nq_rings_in_use(bp);
10746 }
10747 
10748 static int bnxt_init_int_mode(struct bnxt *bp)
10749 {
10750 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
10751 
10752 	total_vecs = bnxt_get_num_msix(bp);
10753 	max = bnxt_get_max_func_irqs(bp);
10754 	if (total_vecs > max)
10755 		total_vecs = max;
10756 
10757 	if (!total_vecs)
10758 		return 0;
10759 
10760 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
10761 		min = 2;
10762 
10763 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
10764 					   PCI_IRQ_MSIX);
10765 	ulp_msix = bnxt_get_ulp_msix_num(bp);
10766 	if (total_vecs < 0 || total_vecs < ulp_msix) {
10767 		rc = -ENODEV;
10768 		goto msix_setup_exit;
10769 	}
10770 
10771 	tbl_size = total_vecs;
10772 	if (pci_msix_can_alloc_dyn(bp->pdev))
10773 		tbl_size = max;
10774 	bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL);
10775 	if (bp->irq_tbl) {
10776 		for (i = 0; i < total_vecs; i++)
10777 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
10778 
10779 		bp->total_irqs = total_vecs;
10780 		/* Trim rings based upon num of vectors allocated */
10781 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
10782 				     total_vecs - ulp_msix, min == 1);
10783 		if (rc)
10784 			goto msix_setup_exit;
10785 
10786 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
10787 		bp->cp_nr_rings = (min == 1) ?
10788 				  max_t(int, tx_cp, bp->rx_nr_rings) :
10789 				  tx_cp + bp->rx_nr_rings;
10790 
10791 	} else {
10792 		rc = -ENOMEM;
10793 		goto msix_setup_exit;
10794 	}
10795 	return 0;
10796 
10797 msix_setup_exit:
10798 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
10799 	kfree(bp->irq_tbl);
10800 	bp->irq_tbl = NULL;
10801 	pci_free_irq_vectors(bp->pdev);
10802 	return rc;
10803 }
10804 
10805 static void bnxt_clear_int_mode(struct bnxt *bp)
10806 {
10807 	pci_free_irq_vectors(bp->pdev);
10808 
10809 	kfree(bp->irq_tbl);
10810 	bp->irq_tbl = NULL;
10811 }
10812 
10813 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
10814 {
10815 	bool irq_cleared = false;
10816 	bool irq_change = false;
10817 	int tcs = bp->num_tc;
10818 	int irqs_required;
10819 	int rc;
10820 
10821 	if (!bnxt_need_reserve_rings(bp))
10822 		return 0;
10823 
10824 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
10825 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
10826 
10827 		if (ulp_msix > bp->ulp_num_msix_want)
10828 			ulp_msix = bp->ulp_num_msix_want;
10829 		irqs_required = ulp_msix + bp->cp_nr_rings;
10830 	} else {
10831 		irqs_required = bnxt_get_num_msix(bp);
10832 	}
10833 
10834 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
10835 		irq_change = true;
10836 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
10837 			bnxt_ulp_irq_stop(bp);
10838 			bnxt_clear_int_mode(bp);
10839 			irq_cleared = true;
10840 		}
10841 	}
10842 	rc = __bnxt_reserve_rings(bp);
10843 	if (irq_cleared) {
10844 		if (!rc)
10845 			rc = bnxt_init_int_mode(bp);
10846 		bnxt_ulp_irq_restart(bp, rc);
10847 	} else if (irq_change && !rc) {
10848 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
10849 			rc = -ENOSPC;
10850 	}
10851 	if (rc) {
10852 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
10853 		return rc;
10854 	}
10855 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
10856 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
10857 		netdev_err(bp->dev, "tx ring reservation failure\n");
10858 		netdev_reset_tc(bp->dev);
10859 		bp->num_tc = 0;
10860 		if (bp->tx_nr_rings_xdp)
10861 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
10862 		else
10863 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
10864 		return -ENOMEM;
10865 	}
10866 	return 0;
10867 }
10868 
10869 static void bnxt_free_irq(struct bnxt *bp)
10870 {
10871 	struct bnxt_irq *irq;
10872 	int i;
10873 
10874 #ifdef CONFIG_RFS_ACCEL
10875 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
10876 	bp->dev->rx_cpu_rmap = NULL;
10877 #endif
10878 	if (!bp->irq_tbl || !bp->bnapi)
10879 		return;
10880 
10881 	for (i = 0; i < bp->cp_nr_rings; i++) {
10882 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10883 
10884 		irq = &bp->irq_tbl[map_idx];
10885 		if (irq->requested) {
10886 			if (irq->have_cpumask) {
10887 				irq_update_affinity_hint(irq->vector, NULL);
10888 				free_cpumask_var(irq->cpu_mask);
10889 				irq->have_cpumask = 0;
10890 			}
10891 			free_irq(irq->vector, bp->bnapi[i]);
10892 		}
10893 
10894 		irq->requested = 0;
10895 	}
10896 }
10897 
10898 static int bnxt_request_irq(struct bnxt *bp)
10899 {
10900 	int i, j, rc = 0;
10901 	unsigned long flags = 0;
10902 #ifdef CONFIG_RFS_ACCEL
10903 	struct cpu_rmap *rmap;
10904 #endif
10905 
10906 	rc = bnxt_setup_int_mode(bp);
10907 	if (rc) {
10908 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
10909 			   rc);
10910 		return rc;
10911 	}
10912 #ifdef CONFIG_RFS_ACCEL
10913 	rmap = bp->dev->rx_cpu_rmap;
10914 #endif
10915 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
10916 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10917 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
10918 
10919 #ifdef CONFIG_RFS_ACCEL
10920 		if (rmap && bp->bnapi[i]->rx_ring) {
10921 			rc = irq_cpu_rmap_add(rmap, irq->vector);
10922 			if (rc)
10923 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
10924 					    j);
10925 			j++;
10926 		}
10927 #endif
10928 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
10929 				 bp->bnapi[i]);
10930 		if (rc)
10931 			break;
10932 
10933 		netif_napi_set_irq(&bp->bnapi[i]->napi, irq->vector);
10934 		irq->requested = 1;
10935 
10936 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
10937 			int numa_node = dev_to_node(&bp->pdev->dev);
10938 
10939 			irq->have_cpumask = 1;
10940 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
10941 					irq->cpu_mask);
10942 			rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
10943 			if (rc) {
10944 				netdev_warn(bp->dev,
10945 					    "Update affinity hint failed, IRQ = %d\n",
10946 					    irq->vector);
10947 				break;
10948 			}
10949 		}
10950 	}
10951 	return rc;
10952 }
10953 
10954 static void bnxt_del_napi(struct bnxt *bp)
10955 {
10956 	int i;
10957 
10958 	if (!bp->bnapi)
10959 		return;
10960 
10961 	for (i = 0; i < bp->rx_nr_rings; i++)
10962 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
10963 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
10964 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
10965 
10966 	for (i = 0; i < bp->cp_nr_rings; i++) {
10967 		struct bnxt_napi *bnapi = bp->bnapi[i];
10968 
10969 		__netif_napi_del(&bnapi->napi);
10970 	}
10971 	/* We called __netif_napi_del(), we need
10972 	 * to respect an RCU grace period before freeing napi structures.
10973 	 */
10974 	synchronize_net();
10975 }
10976 
10977 static void bnxt_init_napi(struct bnxt *bp)
10978 {
10979 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
10980 	unsigned int cp_nr_rings = bp->cp_nr_rings;
10981 	struct bnxt_napi *bnapi;
10982 	int i;
10983 
10984 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10985 		poll_fn = bnxt_poll_p5;
10986 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10987 		cp_nr_rings--;
10988 	for (i = 0; i < cp_nr_rings; i++) {
10989 		bnapi = bp->bnapi[i];
10990 		netif_napi_add_config(bp->dev, &bnapi->napi, poll_fn,
10991 				      bnapi->index);
10992 	}
10993 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10994 		bnapi = bp->bnapi[cp_nr_rings];
10995 		netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
10996 	}
10997 }
10998 
10999 static void bnxt_disable_napi(struct bnxt *bp)
11000 {
11001 	int i;
11002 
11003 	if (!bp->bnapi ||
11004 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11005 		return;
11006 
11007 	for (i = 0; i < bp->cp_nr_rings; i++) {
11008 		struct bnxt_napi *bnapi = bp->bnapi[i];
11009 		struct bnxt_cp_ring_info *cpr;
11010 
11011 		cpr = &bnapi->cp_ring;
11012 		if (bnapi->tx_fault)
11013 			cpr->sw_stats->tx.tx_resets++;
11014 		if (bnapi->in_reset)
11015 			cpr->sw_stats->rx.rx_resets++;
11016 		napi_disable(&bnapi->napi);
11017 		if (bnapi->rx_ring)
11018 			cancel_work_sync(&cpr->dim.work);
11019 	}
11020 }
11021 
11022 static void bnxt_enable_napi(struct bnxt *bp)
11023 {
11024 	int i;
11025 
11026 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11027 	for (i = 0; i < bp->cp_nr_rings; i++) {
11028 		struct bnxt_napi *bnapi = bp->bnapi[i];
11029 		struct bnxt_cp_ring_info *cpr;
11030 
11031 		bnapi->tx_fault = 0;
11032 
11033 		cpr = &bnapi->cp_ring;
11034 		bnapi->in_reset = false;
11035 
11036 		if (bnapi->rx_ring) {
11037 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
11038 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
11039 		}
11040 		napi_enable(&bnapi->napi);
11041 	}
11042 }
11043 
11044 void bnxt_tx_disable(struct bnxt *bp)
11045 {
11046 	int i;
11047 	struct bnxt_tx_ring_info *txr;
11048 
11049 	if (bp->tx_ring) {
11050 		for (i = 0; i < bp->tx_nr_rings; i++) {
11051 			txr = &bp->tx_ring[i];
11052 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11053 		}
11054 	}
11055 	/* Make sure napi polls see @dev_state change */
11056 	synchronize_net();
11057 	/* Drop carrier first to prevent TX timeout */
11058 	netif_carrier_off(bp->dev);
11059 	/* Stop all TX queues */
11060 	netif_tx_disable(bp->dev);
11061 }
11062 
11063 void bnxt_tx_enable(struct bnxt *bp)
11064 {
11065 	int i;
11066 	struct bnxt_tx_ring_info *txr;
11067 
11068 	for (i = 0; i < bp->tx_nr_rings; i++) {
11069 		txr = &bp->tx_ring[i];
11070 		WRITE_ONCE(txr->dev_state, 0);
11071 	}
11072 	/* Make sure napi polls see @dev_state change */
11073 	synchronize_net();
11074 	netif_tx_wake_all_queues(bp->dev);
11075 	if (BNXT_LINK_IS_UP(bp))
11076 		netif_carrier_on(bp->dev);
11077 }
11078 
11079 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
11080 {
11081 	u8 active_fec = link_info->active_fec_sig_mode &
11082 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
11083 
11084 	switch (active_fec) {
11085 	default:
11086 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
11087 		return "None";
11088 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
11089 		return "Clause 74 BaseR";
11090 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
11091 		return "Clause 91 RS(528,514)";
11092 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
11093 		return "Clause 91 RS544_1XN";
11094 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
11095 		return "Clause 91 RS(544,514)";
11096 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
11097 		return "Clause 91 RS272_1XN";
11098 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
11099 		return "Clause 91 RS(272,257)";
11100 	}
11101 }
11102 
11103 void bnxt_report_link(struct bnxt *bp)
11104 {
11105 	if (BNXT_LINK_IS_UP(bp)) {
11106 		const char *signal = "";
11107 		const char *flow_ctrl;
11108 		const char *duplex;
11109 		u32 speed;
11110 		u16 fec;
11111 
11112 		netif_carrier_on(bp->dev);
11113 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
11114 		if (speed == SPEED_UNKNOWN) {
11115 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
11116 			return;
11117 		}
11118 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
11119 			duplex = "full";
11120 		else
11121 			duplex = "half";
11122 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
11123 			flow_ctrl = "ON - receive & transmit";
11124 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
11125 			flow_ctrl = "ON - transmit";
11126 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
11127 			flow_ctrl = "ON - receive";
11128 		else
11129 			flow_ctrl = "none";
11130 		if (bp->link_info.phy_qcfg_resp.option_flags &
11131 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
11132 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
11133 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
11134 			switch (sig_mode) {
11135 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
11136 				signal = "(NRZ) ";
11137 				break;
11138 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
11139 				signal = "(PAM4 56Gbps) ";
11140 				break;
11141 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
11142 				signal = "(PAM4 112Gbps) ";
11143 				break;
11144 			default:
11145 				break;
11146 			}
11147 		}
11148 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
11149 			    speed, signal, duplex, flow_ctrl);
11150 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
11151 			netdev_info(bp->dev, "EEE is %s\n",
11152 				    bp->eee.eee_active ? "active" :
11153 							 "not active");
11154 		fec = bp->link_info.fec_cfg;
11155 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
11156 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
11157 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
11158 				    bnxt_report_fec(&bp->link_info));
11159 	} else {
11160 		netif_carrier_off(bp->dev);
11161 		netdev_err(bp->dev, "NIC Link is Down\n");
11162 	}
11163 }
11164 
11165 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
11166 {
11167 	if (!resp->supported_speeds_auto_mode &&
11168 	    !resp->supported_speeds_force_mode &&
11169 	    !resp->supported_pam4_speeds_auto_mode &&
11170 	    !resp->supported_pam4_speeds_force_mode &&
11171 	    !resp->supported_speeds2_auto_mode &&
11172 	    !resp->supported_speeds2_force_mode)
11173 		return true;
11174 	return false;
11175 }
11176 
11177 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
11178 {
11179 	struct bnxt_link_info *link_info = &bp->link_info;
11180 	struct hwrm_port_phy_qcaps_output *resp;
11181 	struct hwrm_port_phy_qcaps_input *req;
11182 	int rc = 0;
11183 
11184 	if (bp->hwrm_spec_code < 0x10201)
11185 		return 0;
11186 
11187 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
11188 	if (rc)
11189 		return rc;
11190 
11191 	resp = hwrm_req_hold(bp, req);
11192 	rc = hwrm_req_send(bp, req);
11193 	if (rc)
11194 		goto hwrm_phy_qcaps_exit;
11195 
11196 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
11197 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
11198 		struct ethtool_keee *eee = &bp->eee;
11199 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
11200 
11201 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
11202 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
11203 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11204 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11205 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11206 	}
11207 
11208 	if (bp->hwrm_spec_code >= 0x10a01) {
11209 		if (bnxt_phy_qcaps_no_speed(resp)) {
11210 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11211 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11212 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11213 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11214 			netdev_info(bp->dev, "Ethernet link enabled\n");
11215 			/* Phy re-enabled, reprobe the speeds */
11216 			link_info->support_auto_speeds = 0;
11217 			link_info->support_pam4_auto_speeds = 0;
11218 			link_info->support_auto_speeds2 = 0;
11219 		}
11220 	}
11221 	if (resp->supported_speeds_auto_mode)
11222 		link_info->support_auto_speeds =
11223 			le16_to_cpu(resp->supported_speeds_auto_mode);
11224 	if (resp->supported_pam4_speeds_auto_mode)
11225 		link_info->support_pam4_auto_speeds =
11226 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11227 	if (resp->supported_speeds2_auto_mode)
11228 		link_info->support_auto_speeds2 =
11229 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11230 
11231 	bp->port_count = resp->port_cnt;
11232 
11233 hwrm_phy_qcaps_exit:
11234 	hwrm_req_drop(bp, req);
11235 	return rc;
11236 }
11237 
11238 static bool bnxt_support_dropped(u16 advertising, u16 supported)
11239 {
11240 	u16 diff = advertising ^ supported;
11241 
11242 	return ((supported | diff) != supported);
11243 }
11244 
11245 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
11246 {
11247 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
11248 
11249 	/* Check if any advertised speeds are no longer supported. The caller
11250 	 * holds the link_lock mutex, so we can modify link_info settings.
11251 	 */
11252 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11253 		if (bnxt_support_dropped(link_info->advertising,
11254 					 link_info->support_auto_speeds2)) {
11255 			link_info->advertising = link_info->support_auto_speeds2;
11256 			return true;
11257 		}
11258 		return false;
11259 	}
11260 	if (bnxt_support_dropped(link_info->advertising,
11261 				 link_info->support_auto_speeds)) {
11262 		link_info->advertising = link_info->support_auto_speeds;
11263 		return true;
11264 	}
11265 	if (bnxt_support_dropped(link_info->advertising_pam4,
11266 				 link_info->support_pam4_auto_speeds)) {
11267 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
11268 		return true;
11269 	}
11270 	return false;
11271 }
11272 
11273 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
11274 {
11275 	struct bnxt_link_info *link_info = &bp->link_info;
11276 	struct hwrm_port_phy_qcfg_output *resp;
11277 	struct hwrm_port_phy_qcfg_input *req;
11278 	u8 link_state = link_info->link_state;
11279 	bool support_changed;
11280 	int rc;
11281 
11282 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
11283 	if (rc)
11284 		return rc;
11285 
11286 	resp = hwrm_req_hold(bp, req);
11287 	rc = hwrm_req_send(bp, req);
11288 	if (rc) {
11289 		hwrm_req_drop(bp, req);
11290 		if (BNXT_VF(bp) && rc == -ENODEV) {
11291 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
11292 			rc = 0;
11293 		}
11294 		return rc;
11295 	}
11296 
11297 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
11298 	link_info->phy_link_status = resp->link;
11299 	link_info->duplex = resp->duplex_cfg;
11300 	if (bp->hwrm_spec_code >= 0x10800)
11301 		link_info->duplex = resp->duplex_state;
11302 	link_info->pause = resp->pause;
11303 	link_info->auto_mode = resp->auto_mode;
11304 	link_info->auto_pause_setting = resp->auto_pause;
11305 	link_info->lp_pause = resp->link_partner_adv_pause;
11306 	link_info->force_pause_setting = resp->force_pause;
11307 	link_info->duplex_setting = resp->duplex_cfg;
11308 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
11309 		link_info->link_speed = le16_to_cpu(resp->link_speed);
11310 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
11311 			link_info->active_lanes = resp->active_lanes;
11312 	} else {
11313 		link_info->link_speed = 0;
11314 		link_info->active_lanes = 0;
11315 	}
11316 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
11317 	link_info->force_pam4_link_speed =
11318 		le16_to_cpu(resp->force_pam4_link_speed);
11319 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
11320 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
11321 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
11322 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
11323 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
11324 	link_info->auto_pam4_link_speeds =
11325 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
11326 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
11327 	link_info->lp_auto_link_speeds =
11328 		le16_to_cpu(resp->link_partner_adv_speeds);
11329 	link_info->lp_auto_pam4_link_speeds =
11330 		resp->link_partner_pam4_adv_speeds;
11331 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
11332 	link_info->phy_ver[0] = resp->phy_maj;
11333 	link_info->phy_ver[1] = resp->phy_min;
11334 	link_info->phy_ver[2] = resp->phy_bld;
11335 	link_info->media_type = resp->media_type;
11336 	link_info->phy_type = resp->phy_type;
11337 	link_info->transceiver = resp->xcvr_pkg_type;
11338 	link_info->phy_addr = resp->eee_config_phy_addr &
11339 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
11340 	link_info->module_status = resp->module_status;
11341 
11342 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
11343 		struct ethtool_keee *eee = &bp->eee;
11344 		u16 fw_speeds;
11345 
11346 		eee->eee_active = 0;
11347 		if (resp->eee_config_phy_addr &
11348 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
11349 			eee->eee_active = 1;
11350 			fw_speeds = le16_to_cpu(
11351 				resp->link_partner_adv_eee_link_speed_mask);
11352 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
11353 		}
11354 
11355 		/* Pull initial EEE config */
11356 		if (!chng_link_state) {
11357 			if (resp->eee_config_phy_addr &
11358 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
11359 				eee->eee_enabled = 1;
11360 
11361 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
11362 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
11363 
11364 			if (resp->eee_config_phy_addr &
11365 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
11366 				__le32 tmr;
11367 
11368 				eee->tx_lpi_enabled = 1;
11369 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
11370 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
11371 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
11372 			}
11373 		}
11374 	}
11375 
11376 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
11377 	if (bp->hwrm_spec_code >= 0x10504) {
11378 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
11379 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
11380 	}
11381 	/* TODO: need to add more logic to report VF link */
11382 	if (chng_link_state) {
11383 		if (link_info->phy_link_status == BNXT_LINK_LINK)
11384 			link_info->link_state = BNXT_LINK_STATE_UP;
11385 		else
11386 			link_info->link_state = BNXT_LINK_STATE_DOWN;
11387 		if (link_state != link_info->link_state)
11388 			bnxt_report_link(bp);
11389 	} else {
11390 		/* always link down if not require to update link state */
11391 		link_info->link_state = BNXT_LINK_STATE_DOWN;
11392 	}
11393 	hwrm_req_drop(bp, req);
11394 
11395 	if (!BNXT_PHY_CFG_ABLE(bp))
11396 		return 0;
11397 
11398 	support_changed = bnxt_support_speed_dropped(link_info);
11399 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
11400 		bnxt_hwrm_set_link_setting(bp, true, false);
11401 	return 0;
11402 }
11403 
11404 static void bnxt_get_port_module_status(struct bnxt *bp)
11405 {
11406 	struct bnxt_link_info *link_info = &bp->link_info;
11407 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
11408 	u8 module_status;
11409 
11410 	if (bnxt_update_link(bp, true))
11411 		return;
11412 
11413 	module_status = link_info->module_status;
11414 	switch (module_status) {
11415 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
11416 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
11417 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
11418 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
11419 			    bp->pf.port_id);
11420 		if (bp->hwrm_spec_code >= 0x10201) {
11421 			netdev_warn(bp->dev, "Module part number %s\n",
11422 				    resp->phy_vendor_partnumber);
11423 		}
11424 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
11425 			netdev_warn(bp->dev, "TX is disabled\n");
11426 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
11427 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
11428 	}
11429 }
11430 
11431 static void
11432 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11433 {
11434 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
11435 		if (bp->hwrm_spec_code >= 0x10201)
11436 			req->auto_pause =
11437 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
11438 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11439 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
11440 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11441 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
11442 		req->enables |=
11443 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11444 	} else {
11445 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11446 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
11447 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11448 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
11449 		req->enables |=
11450 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
11451 		if (bp->hwrm_spec_code >= 0x10201) {
11452 			req->auto_pause = req->force_pause;
11453 			req->enables |= cpu_to_le32(
11454 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11455 		}
11456 	}
11457 }
11458 
11459 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11460 {
11461 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
11462 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
11463 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11464 			req->enables |=
11465 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
11466 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
11467 		} else if (bp->link_info.advertising) {
11468 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
11469 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
11470 		}
11471 		if (bp->link_info.advertising_pam4) {
11472 			req->enables |=
11473 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
11474 			req->auto_link_pam4_speed_mask =
11475 				cpu_to_le16(bp->link_info.advertising_pam4);
11476 		}
11477 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
11478 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
11479 	} else {
11480 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
11481 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11482 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
11483 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
11484 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
11485 				   (u32)bp->link_info.req_link_speed);
11486 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
11487 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11488 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
11489 		} else {
11490 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11491 		}
11492 	}
11493 
11494 	/* tell chimp that the setting takes effect immediately */
11495 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
11496 }
11497 
11498 int bnxt_hwrm_set_pause(struct bnxt *bp)
11499 {
11500 	struct hwrm_port_phy_cfg_input *req;
11501 	int rc;
11502 
11503 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11504 	if (rc)
11505 		return rc;
11506 
11507 	bnxt_hwrm_set_pause_common(bp, req);
11508 
11509 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
11510 	    bp->link_info.force_link_chng)
11511 		bnxt_hwrm_set_link_common(bp, req);
11512 
11513 	rc = hwrm_req_send(bp, req);
11514 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
11515 		/* since changing of pause setting doesn't trigger any link
11516 		 * change event, the driver needs to update the current pause
11517 		 * result upon successfully return of the phy_cfg command
11518 		 */
11519 		bp->link_info.pause =
11520 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
11521 		bp->link_info.auto_pause_setting = 0;
11522 		if (!bp->link_info.force_link_chng)
11523 			bnxt_report_link(bp);
11524 	}
11525 	bp->link_info.force_link_chng = false;
11526 	return rc;
11527 }
11528 
11529 static void bnxt_hwrm_set_eee(struct bnxt *bp,
11530 			      struct hwrm_port_phy_cfg_input *req)
11531 {
11532 	struct ethtool_keee *eee = &bp->eee;
11533 
11534 	if (eee->eee_enabled) {
11535 		u16 eee_speeds;
11536 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
11537 
11538 		if (eee->tx_lpi_enabled)
11539 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
11540 		else
11541 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
11542 
11543 		req->flags |= cpu_to_le32(flags);
11544 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
11545 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
11546 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
11547 	} else {
11548 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
11549 	}
11550 }
11551 
11552 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
11553 {
11554 	struct hwrm_port_phy_cfg_input *req;
11555 	int rc;
11556 
11557 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11558 	if (rc)
11559 		return rc;
11560 
11561 	if (set_pause)
11562 		bnxt_hwrm_set_pause_common(bp, req);
11563 
11564 	bnxt_hwrm_set_link_common(bp, req);
11565 
11566 	if (set_eee)
11567 		bnxt_hwrm_set_eee(bp, req);
11568 	return hwrm_req_send(bp, req);
11569 }
11570 
11571 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
11572 {
11573 	struct hwrm_port_phy_cfg_input *req;
11574 	int rc;
11575 
11576 	if (!BNXT_SINGLE_PF(bp))
11577 		return 0;
11578 
11579 	if (pci_num_vf(bp->pdev) &&
11580 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
11581 		return 0;
11582 
11583 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11584 	if (rc)
11585 		return rc;
11586 
11587 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
11588 	rc = hwrm_req_send(bp, req);
11589 	if (!rc) {
11590 		mutex_lock(&bp->link_lock);
11591 		/* Device is not obliged link down in certain scenarios, even
11592 		 * when forced. Setting the state unknown is consistent with
11593 		 * driver startup and will force link state to be reported
11594 		 * during subsequent open based on PORT_PHY_QCFG.
11595 		 */
11596 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
11597 		mutex_unlock(&bp->link_lock);
11598 	}
11599 	return rc;
11600 }
11601 
11602 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
11603 {
11604 #ifdef CONFIG_TEE_BNXT_FW
11605 	int rc = tee_bnxt_fw_load();
11606 
11607 	if (rc)
11608 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
11609 
11610 	return rc;
11611 #else
11612 	netdev_err(bp->dev, "OP-TEE not supported\n");
11613 	return -ENODEV;
11614 #endif
11615 }
11616 
11617 static int bnxt_try_recover_fw(struct bnxt *bp)
11618 {
11619 	if (bp->fw_health && bp->fw_health->status_reliable) {
11620 		int retry = 0, rc;
11621 		u32 sts;
11622 
11623 		do {
11624 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
11625 			rc = bnxt_hwrm_poll(bp);
11626 			if (!BNXT_FW_IS_BOOTING(sts) &&
11627 			    !BNXT_FW_IS_RECOVERING(sts))
11628 				break;
11629 			retry++;
11630 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
11631 
11632 		if (!BNXT_FW_IS_HEALTHY(sts)) {
11633 			netdev_err(bp->dev,
11634 				   "Firmware not responding, status: 0x%x\n",
11635 				   sts);
11636 			rc = -ENODEV;
11637 		}
11638 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
11639 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
11640 			return bnxt_fw_reset_via_optee(bp);
11641 		}
11642 		return rc;
11643 	}
11644 
11645 	return -ENODEV;
11646 }
11647 
11648 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
11649 {
11650 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11651 
11652 	if (!BNXT_NEW_RM(bp))
11653 		return; /* no resource reservations required */
11654 
11655 	hw_resc->resv_cp_rings = 0;
11656 	hw_resc->resv_stat_ctxs = 0;
11657 	hw_resc->resv_irqs = 0;
11658 	hw_resc->resv_tx_rings = 0;
11659 	hw_resc->resv_rx_rings = 0;
11660 	hw_resc->resv_hw_ring_grps = 0;
11661 	hw_resc->resv_vnics = 0;
11662 	hw_resc->resv_rsscos_ctxs = 0;
11663 	if (!fw_reset) {
11664 		bp->tx_nr_rings = 0;
11665 		bp->rx_nr_rings = 0;
11666 	}
11667 }
11668 
11669 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
11670 {
11671 	int rc;
11672 
11673 	if (!BNXT_NEW_RM(bp))
11674 		return 0; /* no resource reservations required */
11675 
11676 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
11677 	if (rc)
11678 		netdev_err(bp->dev, "resc_qcaps failed\n");
11679 
11680 	bnxt_clear_reservations(bp, fw_reset);
11681 
11682 	return rc;
11683 }
11684 
11685 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
11686 {
11687 	struct hwrm_func_drv_if_change_output *resp;
11688 	struct hwrm_func_drv_if_change_input *req;
11689 	bool fw_reset = !bp->irq_tbl;
11690 	bool resc_reinit = false;
11691 	int rc, retry = 0;
11692 	u32 flags = 0;
11693 
11694 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
11695 		return 0;
11696 
11697 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
11698 	if (rc)
11699 		return rc;
11700 
11701 	if (up)
11702 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
11703 	resp = hwrm_req_hold(bp, req);
11704 
11705 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
11706 	while (retry < BNXT_FW_IF_RETRY) {
11707 		rc = hwrm_req_send(bp, req);
11708 		if (rc != -EAGAIN)
11709 			break;
11710 
11711 		msleep(50);
11712 		retry++;
11713 	}
11714 
11715 	if (rc == -EAGAIN) {
11716 		hwrm_req_drop(bp, req);
11717 		return rc;
11718 	} else if (!rc) {
11719 		flags = le32_to_cpu(resp->flags);
11720 	} else if (up) {
11721 		rc = bnxt_try_recover_fw(bp);
11722 		fw_reset = true;
11723 	}
11724 	hwrm_req_drop(bp, req);
11725 	if (rc)
11726 		return rc;
11727 
11728 	if (!up) {
11729 		bnxt_inv_fw_health_reg(bp);
11730 		return 0;
11731 	}
11732 
11733 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
11734 		resc_reinit = true;
11735 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
11736 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
11737 		fw_reset = true;
11738 	else
11739 		bnxt_remap_fw_health_regs(bp);
11740 
11741 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
11742 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
11743 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
11744 		return -ENODEV;
11745 	}
11746 	if (resc_reinit || fw_reset) {
11747 		if (fw_reset) {
11748 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11749 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11750 				bnxt_ulp_irq_stop(bp);
11751 			bnxt_free_ctx_mem(bp);
11752 			bnxt_dcb_free(bp);
11753 			rc = bnxt_fw_init_one(bp);
11754 			if (rc) {
11755 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11756 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
11757 				return rc;
11758 			}
11759 			bnxt_clear_int_mode(bp);
11760 			rc = bnxt_init_int_mode(bp);
11761 			if (rc) {
11762 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11763 				netdev_err(bp->dev, "init int mode failed\n");
11764 				return rc;
11765 			}
11766 		}
11767 		rc = bnxt_cancel_reservations(bp, fw_reset);
11768 	}
11769 	return rc;
11770 }
11771 
11772 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
11773 {
11774 	struct hwrm_port_led_qcaps_output *resp;
11775 	struct hwrm_port_led_qcaps_input *req;
11776 	struct bnxt_pf_info *pf = &bp->pf;
11777 	int rc;
11778 
11779 	bp->num_leds = 0;
11780 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
11781 		return 0;
11782 
11783 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
11784 	if (rc)
11785 		return rc;
11786 
11787 	req->port_id = cpu_to_le16(pf->port_id);
11788 	resp = hwrm_req_hold(bp, req);
11789 	rc = hwrm_req_send(bp, req);
11790 	if (rc) {
11791 		hwrm_req_drop(bp, req);
11792 		return rc;
11793 	}
11794 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
11795 		int i;
11796 
11797 		bp->num_leds = resp->num_leds;
11798 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
11799 						 bp->num_leds);
11800 		for (i = 0; i < bp->num_leds; i++) {
11801 			struct bnxt_led_info *led = &bp->leds[i];
11802 			__le16 caps = led->led_state_caps;
11803 
11804 			if (!led->led_group_id ||
11805 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
11806 				bp->num_leds = 0;
11807 				break;
11808 			}
11809 		}
11810 	}
11811 	hwrm_req_drop(bp, req);
11812 	return 0;
11813 }
11814 
11815 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
11816 {
11817 	struct hwrm_wol_filter_alloc_output *resp;
11818 	struct hwrm_wol_filter_alloc_input *req;
11819 	int rc;
11820 
11821 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
11822 	if (rc)
11823 		return rc;
11824 
11825 	req->port_id = cpu_to_le16(bp->pf.port_id);
11826 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
11827 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
11828 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
11829 
11830 	resp = hwrm_req_hold(bp, req);
11831 	rc = hwrm_req_send(bp, req);
11832 	if (!rc)
11833 		bp->wol_filter_id = resp->wol_filter_id;
11834 	hwrm_req_drop(bp, req);
11835 	return rc;
11836 }
11837 
11838 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
11839 {
11840 	struct hwrm_wol_filter_free_input *req;
11841 	int rc;
11842 
11843 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
11844 	if (rc)
11845 		return rc;
11846 
11847 	req->port_id = cpu_to_le16(bp->pf.port_id);
11848 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
11849 	req->wol_filter_id = bp->wol_filter_id;
11850 
11851 	return hwrm_req_send(bp, req);
11852 }
11853 
11854 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
11855 {
11856 	struct hwrm_wol_filter_qcfg_output *resp;
11857 	struct hwrm_wol_filter_qcfg_input *req;
11858 	u16 next_handle = 0;
11859 	int rc;
11860 
11861 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
11862 	if (rc)
11863 		return rc;
11864 
11865 	req->port_id = cpu_to_le16(bp->pf.port_id);
11866 	req->handle = cpu_to_le16(handle);
11867 	resp = hwrm_req_hold(bp, req);
11868 	rc = hwrm_req_send(bp, req);
11869 	if (!rc) {
11870 		next_handle = le16_to_cpu(resp->next_handle);
11871 		if (next_handle != 0) {
11872 			if (resp->wol_type ==
11873 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
11874 				bp->wol = 1;
11875 				bp->wol_filter_id = resp->wol_filter_id;
11876 			}
11877 		}
11878 	}
11879 	hwrm_req_drop(bp, req);
11880 	return next_handle;
11881 }
11882 
11883 static void bnxt_get_wol_settings(struct bnxt *bp)
11884 {
11885 	u16 handle = 0;
11886 
11887 	bp->wol = 0;
11888 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
11889 		return;
11890 
11891 	do {
11892 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
11893 	} while (handle && handle != 0xffff);
11894 }
11895 
11896 static bool bnxt_eee_config_ok(struct bnxt *bp)
11897 {
11898 	struct ethtool_keee *eee = &bp->eee;
11899 	struct bnxt_link_info *link_info = &bp->link_info;
11900 
11901 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
11902 		return true;
11903 
11904 	if (eee->eee_enabled) {
11905 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
11906 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
11907 
11908 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
11909 
11910 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
11911 			eee->eee_enabled = 0;
11912 			return false;
11913 		}
11914 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
11915 			linkmode_and(eee->advertised, advertising,
11916 				     eee->supported);
11917 			return false;
11918 		}
11919 	}
11920 	return true;
11921 }
11922 
11923 static int bnxt_update_phy_setting(struct bnxt *bp)
11924 {
11925 	int rc;
11926 	bool update_link = false;
11927 	bool update_pause = false;
11928 	bool update_eee = false;
11929 	struct bnxt_link_info *link_info = &bp->link_info;
11930 
11931 	rc = bnxt_update_link(bp, true);
11932 	if (rc) {
11933 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
11934 			   rc);
11935 		return rc;
11936 	}
11937 	if (!BNXT_SINGLE_PF(bp))
11938 		return 0;
11939 
11940 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
11941 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
11942 	    link_info->req_flow_ctrl)
11943 		update_pause = true;
11944 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
11945 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
11946 		update_pause = true;
11947 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
11948 		if (BNXT_AUTO_MODE(link_info->auto_mode))
11949 			update_link = true;
11950 		if (bnxt_force_speed_updated(link_info))
11951 			update_link = true;
11952 		if (link_info->req_duplex != link_info->duplex_setting)
11953 			update_link = true;
11954 	} else {
11955 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
11956 			update_link = true;
11957 		if (bnxt_auto_speed_updated(link_info))
11958 			update_link = true;
11959 	}
11960 
11961 	/* The last close may have shutdown the link, so need to call
11962 	 * PHY_CFG to bring it back up.
11963 	 */
11964 	if (!BNXT_LINK_IS_UP(bp))
11965 		update_link = true;
11966 
11967 	if (!bnxt_eee_config_ok(bp))
11968 		update_eee = true;
11969 
11970 	if (update_link)
11971 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
11972 	else if (update_pause)
11973 		rc = bnxt_hwrm_set_pause(bp);
11974 	if (rc) {
11975 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
11976 			   rc);
11977 		return rc;
11978 	}
11979 
11980 	return rc;
11981 }
11982 
11983 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
11984 
11985 static int bnxt_reinit_after_abort(struct bnxt *bp)
11986 {
11987 	int rc;
11988 
11989 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11990 		return -EBUSY;
11991 
11992 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
11993 		return -ENODEV;
11994 
11995 	rc = bnxt_fw_init_one(bp);
11996 	if (!rc) {
11997 		bnxt_clear_int_mode(bp);
11998 		rc = bnxt_init_int_mode(bp);
11999 		if (!rc) {
12000 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12001 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12002 		}
12003 	}
12004 	return rc;
12005 }
12006 
12007 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
12008 {
12009 	struct bnxt_ntuple_filter *ntp_fltr;
12010 	struct bnxt_l2_filter *l2_fltr;
12011 
12012 	if (list_empty(&fltr->list))
12013 		return;
12014 
12015 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
12016 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
12017 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
12018 		atomic_inc(&l2_fltr->refcnt);
12019 		ntp_fltr->l2_fltr = l2_fltr;
12020 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
12021 			bnxt_del_ntp_filter(bp, ntp_fltr);
12022 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
12023 				   fltr->sw_id);
12024 		}
12025 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
12026 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
12027 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
12028 			bnxt_del_l2_filter(bp, l2_fltr);
12029 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
12030 				   fltr->sw_id);
12031 		}
12032 	}
12033 }
12034 
12035 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
12036 {
12037 	struct bnxt_filter_base *usr_fltr, *tmp;
12038 
12039 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
12040 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
12041 }
12042 
12043 static int bnxt_set_xps_mapping(struct bnxt *bp)
12044 {
12045 	int numa_node = dev_to_node(&bp->pdev->dev);
12046 	unsigned int q_idx, map_idx, cpu, i;
12047 	const struct cpumask *cpu_mask_ptr;
12048 	int nr_cpus = num_online_cpus();
12049 	cpumask_t *q_map;
12050 	int rc = 0;
12051 
12052 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
12053 	if (!q_map)
12054 		return -ENOMEM;
12055 
12056 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
12057 	 * Each TC has the same number of TX queues. The nth TX queue for each
12058 	 * TC will have the same CPU mask.
12059 	 */
12060 	for (i = 0; i < nr_cpus; i++) {
12061 		map_idx = i % bp->tx_nr_rings_per_tc;
12062 		cpu = cpumask_local_spread(i, numa_node);
12063 		cpu_mask_ptr = get_cpu_mask(cpu);
12064 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
12065 	}
12066 
12067 	/* Register CPU mask for each TX queue except the ones marked for XDP */
12068 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
12069 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
12070 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
12071 		if (rc) {
12072 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
12073 				    q_idx);
12074 			break;
12075 		}
12076 	}
12077 
12078 	kfree(q_map);
12079 
12080 	return rc;
12081 }
12082 
12083 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12084 {
12085 	int rc = 0;
12086 
12087 	netif_carrier_off(bp->dev);
12088 	if (irq_re_init) {
12089 		/* Reserve rings now if none were reserved at driver probe. */
12090 		rc = bnxt_init_dflt_ring_mode(bp);
12091 		if (rc) {
12092 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
12093 			return rc;
12094 		}
12095 	}
12096 	rc = bnxt_reserve_rings(bp, irq_re_init);
12097 	if (rc)
12098 		return rc;
12099 
12100 	rc = bnxt_alloc_mem(bp, irq_re_init);
12101 	if (rc) {
12102 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12103 		goto open_err_free_mem;
12104 	}
12105 
12106 	if (irq_re_init) {
12107 		bnxt_init_napi(bp);
12108 		rc = bnxt_request_irq(bp);
12109 		if (rc) {
12110 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
12111 			goto open_err_irq;
12112 		}
12113 	}
12114 
12115 	rc = bnxt_init_nic(bp, irq_re_init);
12116 	if (rc) {
12117 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12118 		goto open_err_irq;
12119 	}
12120 
12121 	bnxt_enable_napi(bp);
12122 	bnxt_debug_dev_init(bp);
12123 
12124 	if (link_re_init) {
12125 		mutex_lock(&bp->link_lock);
12126 		rc = bnxt_update_phy_setting(bp);
12127 		mutex_unlock(&bp->link_lock);
12128 		if (rc) {
12129 			netdev_warn(bp->dev, "failed to update phy settings\n");
12130 			if (BNXT_SINGLE_PF(bp)) {
12131 				bp->link_info.phy_retry = true;
12132 				bp->link_info.phy_retry_expires =
12133 					jiffies + 5 * HZ;
12134 			}
12135 		}
12136 	}
12137 
12138 	if (irq_re_init) {
12139 		udp_tunnel_nic_reset_ntf(bp->dev);
12140 		rc = bnxt_set_xps_mapping(bp);
12141 		if (rc)
12142 			netdev_warn(bp->dev, "failed to set xps mapping\n");
12143 	}
12144 
12145 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
12146 		if (!static_key_enabled(&bnxt_xdp_locking_key))
12147 			static_branch_enable(&bnxt_xdp_locking_key);
12148 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
12149 		static_branch_disable(&bnxt_xdp_locking_key);
12150 	}
12151 	set_bit(BNXT_STATE_OPEN, &bp->state);
12152 	bnxt_enable_int(bp);
12153 	/* Enable TX queues */
12154 	bnxt_tx_enable(bp);
12155 	mod_timer(&bp->timer, jiffies + bp->current_interval);
12156 	/* Poll link status and check for SFP+ module status */
12157 	mutex_lock(&bp->link_lock);
12158 	bnxt_get_port_module_status(bp);
12159 	mutex_unlock(&bp->link_lock);
12160 
12161 	/* VF-reps may need to be re-opened after the PF is re-opened */
12162 	if (BNXT_PF(bp))
12163 		bnxt_vf_reps_open(bp);
12164 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
12165 		WRITE_ONCE(bp->ptp_cfg->tx_avail, BNXT_MAX_TX_TS);
12166 	bnxt_ptp_init_rtc(bp, true);
12167 	bnxt_ptp_cfg_tstamp_filters(bp);
12168 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12169 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
12170 	bnxt_cfg_usr_fltrs(bp);
12171 	return 0;
12172 
12173 open_err_irq:
12174 	bnxt_del_napi(bp);
12175 
12176 open_err_free_mem:
12177 	bnxt_free_skbs(bp);
12178 	bnxt_free_irq(bp);
12179 	bnxt_free_mem(bp, true);
12180 	return rc;
12181 }
12182 
12183 /* rtnl_lock held */
12184 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12185 {
12186 	int rc = 0;
12187 
12188 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12189 		rc = -EIO;
12190 	if (!rc)
12191 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12192 	if (rc) {
12193 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12194 		dev_close(bp->dev);
12195 	}
12196 	return rc;
12197 }
12198 
12199 /* rtnl_lock held, open the NIC half way by allocating all resources, but
12200  * NAPI, IRQ, and TX are not enabled.  This is mainly used for offline
12201  * self tests.
12202  */
12203 int bnxt_half_open_nic(struct bnxt *bp)
12204 {
12205 	int rc = 0;
12206 
12207 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12208 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
12209 		rc = -ENODEV;
12210 		goto half_open_err;
12211 	}
12212 
12213 	rc = bnxt_alloc_mem(bp, true);
12214 	if (rc) {
12215 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12216 		goto half_open_err;
12217 	}
12218 	bnxt_init_napi(bp);
12219 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12220 	rc = bnxt_init_nic(bp, true);
12221 	if (rc) {
12222 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12223 		bnxt_del_napi(bp);
12224 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12225 		goto half_open_err;
12226 	}
12227 	return 0;
12228 
12229 half_open_err:
12230 	bnxt_free_skbs(bp);
12231 	bnxt_free_mem(bp, true);
12232 	dev_close(bp->dev);
12233 	return rc;
12234 }
12235 
12236 /* rtnl_lock held, this call can only be made after a previous successful
12237  * call to bnxt_half_open_nic().
12238  */
12239 void bnxt_half_close_nic(struct bnxt *bp)
12240 {
12241 	bnxt_hwrm_resource_free(bp, false, true);
12242 	bnxt_del_napi(bp);
12243 	bnxt_free_skbs(bp);
12244 	bnxt_free_mem(bp, true);
12245 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12246 }
12247 
12248 void bnxt_reenable_sriov(struct bnxt *bp)
12249 {
12250 	if (BNXT_PF(bp)) {
12251 		struct bnxt_pf_info *pf = &bp->pf;
12252 		int n = pf->active_vfs;
12253 
12254 		if (n)
12255 			bnxt_cfg_hw_sriov(bp, &n, true);
12256 	}
12257 }
12258 
12259 static int bnxt_open(struct net_device *dev)
12260 {
12261 	struct bnxt *bp = netdev_priv(dev);
12262 	int rc;
12263 
12264 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12265 		rc = bnxt_reinit_after_abort(bp);
12266 		if (rc) {
12267 			if (rc == -EBUSY)
12268 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
12269 			else
12270 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
12271 			return -ENODEV;
12272 		}
12273 	}
12274 
12275 	rc = bnxt_hwrm_if_change(bp, true);
12276 	if (rc)
12277 		return rc;
12278 
12279 	rc = __bnxt_open_nic(bp, true, true);
12280 	if (rc) {
12281 		bnxt_hwrm_if_change(bp, false);
12282 	} else {
12283 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
12284 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12285 				bnxt_queue_sp_work(bp,
12286 						   BNXT_RESTART_ULP_SP_EVENT);
12287 		}
12288 	}
12289 
12290 	return rc;
12291 }
12292 
12293 static bool bnxt_drv_busy(struct bnxt *bp)
12294 {
12295 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
12296 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
12297 }
12298 
12299 static void bnxt_get_ring_stats(struct bnxt *bp,
12300 				struct rtnl_link_stats64 *stats);
12301 
12302 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
12303 			     bool link_re_init)
12304 {
12305 	/* Close the VF-reps before closing PF */
12306 	if (BNXT_PF(bp))
12307 		bnxt_vf_reps_close(bp);
12308 
12309 	/* Change device state to avoid TX queue wake up's */
12310 	bnxt_tx_disable(bp);
12311 
12312 	clear_bit(BNXT_STATE_OPEN, &bp->state);
12313 	smp_mb__after_atomic();
12314 	while (bnxt_drv_busy(bp))
12315 		msleep(20);
12316 
12317 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12318 		bnxt_clear_rss_ctxs(bp);
12319 	/* Flush rings and disable interrupts */
12320 	bnxt_shutdown_nic(bp, irq_re_init);
12321 
12322 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
12323 
12324 	bnxt_debug_dev_exit(bp);
12325 	bnxt_disable_napi(bp);
12326 	del_timer_sync(&bp->timer);
12327 	bnxt_free_skbs(bp);
12328 
12329 	/* Save ring stats before shutdown */
12330 	if (bp->bnapi && irq_re_init) {
12331 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
12332 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
12333 	}
12334 	if (irq_re_init) {
12335 		bnxt_free_irq(bp);
12336 		bnxt_del_napi(bp);
12337 	}
12338 	bnxt_free_mem(bp, irq_re_init);
12339 }
12340 
12341 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12342 {
12343 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12344 		/* If we get here, it means firmware reset is in progress
12345 		 * while we are trying to close.  We can safely proceed with
12346 		 * the close because we are holding rtnl_lock().  Some firmware
12347 		 * messages may fail as we proceed to close.  We set the
12348 		 * ABORT_ERR flag here so that the FW reset thread will later
12349 		 * abort when it gets the rtnl_lock() and sees the flag.
12350 		 */
12351 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
12352 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12353 	}
12354 
12355 #ifdef CONFIG_BNXT_SRIOV
12356 	if (bp->sriov_cfg) {
12357 		int rc;
12358 
12359 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
12360 						      !bp->sriov_cfg,
12361 						      BNXT_SRIOV_CFG_WAIT_TMO);
12362 		if (!rc)
12363 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
12364 		else if (rc < 0)
12365 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
12366 	}
12367 #endif
12368 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
12369 }
12370 
12371 static int bnxt_close(struct net_device *dev)
12372 {
12373 	struct bnxt *bp = netdev_priv(dev);
12374 
12375 	bnxt_close_nic(bp, true, true);
12376 	bnxt_hwrm_shutdown_link(bp);
12377 	bnxt_hwrm_if_change(bp, false);
12378 	return 0;
12379 }
12380 
12381 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
12382 				   u16 *val)
12383 {
12384 	struct hwrm_port_phy_mdio_read_output *resp;
12385 	struct hwrm_port_phy_mdio_read_input *req;
12386 	int rc;
12387 
12388 	if (bp->hwrm_spec_code < 0x10a00)
12389 		return -EOPNOTSUPP;
12390 
12391 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
12392 	if (rc)
12393 		return rc;
12394 
12395 	req->port_id = cpu_to_le16(bp->pf.port_id);
12396 	req->phy_addr = phy_addr;
12397 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12398 	if (mdio_phy_id_is_c45(phy_addr)) {
12399 		req->cl45_mdio = 1;
12400 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12401 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12402 		req->reg_addr = cpu_to_le16(reg);
12403 	}
12404 
12405 	resp = hwrm_req_hold(bp, req);
12406 	rc = hwrm_req_send(bp, req);
12407 	if (!rc)
12408 		*val = le16_to_cpu(resp->reg_data);
12409 	hwrm_req_drop(bp, req);
12410 	return rc;
12411 }
12412 
12413 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
12414 				    u16 val)
12415 {
12416 	struct hwrm_port_phy_mdio_write_input *req;
12417 	int rc;
12418 
12419 	if (bp->hwrm_spec_code < 0x10a00)
12420 		return -EOPNOTSUPP;
12421 
12422 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
12423 	if (rc)
12424 		return rc;
12425 
12426 	req->port_id = cpu_to_le16(bp->pf.port_id);
12427 	req->phy_addr = phy_addr;
12428 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12429 	if (mdio_phy_id_is_c45(phy_addr)) {
12430 		req->cl45_mdio = 1;
12431 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12432 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12433 		req->reg_addr = cpu_to_le16(reg);
12434 	}
12435 	req->reg_data = cpu_to_le16(val);
12436 
12437 	return hwrm_req_send(bp, req);
12438 }
12439 
12440 /* rtnl_lock held */
12441 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12442 {
12443 	struct mii_ioctl_data *mdio = if_mii(ifr);
12444 	struct bnxt *bp = netdev_priv(dev);
12445 	int rc;
12446 
12447 	switch (cmd) {
12448 	case SIOCGMIIPHY:
12449 		mdio->phy_id = bp->link_info.phy_addr;
12450 
12451 		fallthrough;
12452 	case SIOCGMIIREG: {
12453 		u16 mii_regval = 0;
12454 
12455 		if (!netif_running(dev))
12456 			return -EAGAIN;
12457 
12458 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
12459 					     &mii_regval);
12460 		mdio->val_out = mii_regval;
12461 		return rc;
12462 	}
12463 
12464 	case SIOCSMIIREG:
12465 		if (!netif_running(dev))
12466 			return -EAGAIN;
12467 
12468 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
12469 						mdio->val_in);
12470 
12471 	case SIOCSHWTSTAMP:
12472 		return bnxt_hwtstamp_set(dev, ifr);
12473 
12474 	case SIOCGHWTSTAMP:
12475 		return bnxt_hwtstamp_get(dev, ifr);
12476 
12477 	default:
12478 		/* do nothing */
12479 		break;
12480 	}
12481 	return -EOPNOTSUPP;
12482 }
12483 
12484 static void bnxt_get_ring_stats(struct bnxt *bp,
12485 				struct rtnl_link_stats64 *stats)
12486 {
12487 	int i;
12488 
12489 	for (i = 0; i < bp->cp_nr_rings; i++) {
12490 		struct bnxt_napi *bnapi = bp->bnapi[i];
12491 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
12492 		u64 *sw = cpr->stats.sw_stats;
12493 
12494 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
12495 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12496 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
12497 
12498 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
12499 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
12500 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
12501 
12502 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
12503 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
12504 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
12505 
12506 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
12507 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
12508 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
12509 
12510 		stats->rx_missed_errors +=
12511 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
12512 
12513 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12514 
12515 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
12516 
12517 		stats->rx_dropped +=
12518 			cpr->sw_stats->rx.rx_netpoll_discards +
12519 			cpr->sw_stats->rx.rx_oom_discards;
12520 	}
12521 }
12522 
12523 static void bnxt_add_prev_stats(struct bnxt *bp,
12524 				struct rtnl_link_stats64 *stats)
12525 {
12526 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
12527 
12528 	stats->rx_packets += prev_stats->rx_packets;
12529 	stats->tx_packets += prev_stats->tx_packets;
12530 	stats->rx_bytes += prev_stats->rx_bytes;
12531 	stats->tx_bytes += prev_stats->tx_bytes;
12532 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
12533 	stats->multicast += prev_stats->multicast;
12534 	stats->rx_dropped += prev_stats->rx_dropped;
12535 	stats->tx_dropped += prev_stats->tx_dropped;
12536 }
12537 
12538 static void
12539 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
12540 {
12541 	struct bnxt *bp = netdev_priv(dev);
12542 
12543 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
12544 	/* Make sure bnxt_close_nic() sees that we are reading stats before
12545 	 * we check the BNXT_STATE_OPEN flag.
12546 	 */
12547 	smp_mb__after_atomic();
12548 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12549 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12550 		*stats = bp->net_stats_prev;
12551 		return;
12552 	}
12553 
12554 	bnxt_get_ring_stats(bp, stats);
12555 	bnxt_add_prev_stats(bp, stats);
12556 
12557 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
12558 		u64 *rx = bp->port_stats.sw_stats;
12559 		u64 *tx = bp->port_stats.sw_stats +
12560 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
12561 
12562 		stats->rx_crc_errors =
12563 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
12564 		stats->rx_frame_errors =
12565 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
12566 		stats->rx_length_errors =
12567 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
12568 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
12569 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
12570 		stats->rx_errors =
12571 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
12572 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
12573 		stats->collisions =
12574 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
12575 		stats->tx_fifo_errors =
12576 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
12577 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
12578 	}
12579 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12580 }
12581 
12582 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
12583 					struct bnxt_total_ring_err_stats *stats,
12584 					struct bnxt_cp_ring_info *cpr)
12585 {
12586 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
12587 	u64 *hw_stats = cpr->stats.sw_stats;
12588 
12589 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
12590 	stats->rx_total_resets += sw_stats->rx.rx_resets;
12591 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
12592 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
12593 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
12594 	stats->rx_total_ring_discards +=
12595 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
12596 	stats->tx_total_resets += sw_stats->tx.tx_resets;
12597 	stats->tx_total_ring_discards +=
12598 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
12599 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
12600 }
12601 
12602 void bnxt_get_ring_err_stats(struct bnxt *bp,
12603 			     struct bnxt_total_ring_err_stats *stats)
12604 {
12605 	int i;
12606 
12607 	for (i = 0; i < bp->cp_nr_rings; i++)
12608 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
12609 }
12610 
12611 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
12612 {
12613 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12614 	struct net_device *dev = bp->dev;
12615 	struct netdev_hw_addr *ha;
12616 	u8 *haddr;
12617 	int mc_count = 0;
12618 	bool update = false;
12619 	int off = 0;
12620 
12621 	netdev_for_each_mc_addr(ha, dev) {
12622 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
12623 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12624 			vnic->mc_list_count = 0;
12625 			return false;
12626 		}
12627 		haddr = ha->addr;
12628 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
12629 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
12630 			update = true;
12631 		}
12632 		off += ETH_ALEN;
12633 		mc_count++;
12634 	}
12635 	if (mc_count)
12636 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
12637 
12638 	if (mc_count != vnic->mc_list_count) {
12639 		vnic->mc_list_count = mc_count;
12640 		update = true;
12641 	}
12642 	return update;
12643 }
12644 
12645 static bool bnxt_uc_list_updated(struct bnxt *bp)
12646 {
12647 	struct net_device *dev = bp->dev;
12648 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12649 	struct netdev_hw_addr *ha;
12650 	int off = 0;
12651 
12652 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
12653 		return true;
12654 
12655 	netdev_for_each_uc_addr(ha, dev) {
12656 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
12657 			return true;
12658 
12659 		off += ETH_ALEN;
12660 	}
12661 	return false;
12662 }
12663 
12664 static void bnxt_set_rx_mode(struct net_device *dev)
12665 {
12666 	struct bnxt *bp = netdev_priv(dev);
12667 	struct bnxt_vnic_info *vnic;
12668 	bool mc_update = false;
12669 	bool uc_update;
12670 	u32 mask;
12671 
12672 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
12673 		return;
12674 
12675 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12676 	mask = vnic->rx_mask;
12677 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
12678 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
12679 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
12680 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
12681 
12682 	if (dev->flags & IFF_PROMISC)
12683 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12684 
12685 	uc_update = bnxt_uc_list_updated(bp);
12686 
12687 	if (dev->flags & IFF_BROADCAST)
12688 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
12689 	if (dev->flags & IFF_ALLMULTI) {
12690 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12691 		vnic->mc_list_count = 0;
12692 	} else if (dev->flags & IFF_MULTICAST) {
12693 		mc_update = bnxt_mc_list_updated(bp, &mask);
12694 	}
12695 
12696 	if (mask != vnic->rx_mask || uc_update || mc_update) {
12697 		vnic->rx_mask = mask;
12698 
12699 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
12700 	}
12701 }
12702 
12703 static int bnxt_cfg_rx_mode(struct bnxt *bp)
12704 {
12705 	struct net_device *dev = bp->dev;
12706 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12707 	struct netdev_hw_addr *ha;
12708 	int i, off = 0, rc;
12709 	bool uc_update;
12710 
12711 	netif_addr_lock_bh(dev);
12712 	uc_update = bnxt_uc_list_updated(bp);
12713 	netif_addr_unlock_bh(dev);
12714 
12715 	if (!uc_update)
12716 		goto skip_uc;
12717 
12718 	for (i = 1; i < vnic->uc_filter_count; i++) {
12719 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
12720 
12721 		bnxt_hwrm_l2_filter_free(bp, fltr);
12722 		bnxt_del_l2_filter(bp, fltr);
12723 	}
12724 
12725 	vnic->uc_filter_count = 1;
12726 
12727 	netif_addr_lock_bh(dev);
12728 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
12729 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12730 	} else {
12731 		netdev_for_each_uc_addr(ha, dev) {
12732 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
12733 			off += ETH_ALEN;
12734 			vnic->uc_filter_count++;
12735 		}
12736 	}
12737 	netif_addr_unlock_bh(dev);
12738 
12739 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
12740 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
12741 		if (rc) {
12742 			if (BNXT_VF(bp) && rc == -ENODEV) {
12743 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
12744 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
12745 				else
12746 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
12747 				rc = 0;
12748 			} else {
12749 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
12750 			}
12751 			vnic->uc_filter_count = i;
12752 			return rc;
12753 		}
12754 	}
12755 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
12756 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
12757 
12758 skip_uc:
12759 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
12760 	    !bnxt_promisc_ok(bp))
12761 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12762 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
12763 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
12764 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
12765 			    rc);
12766 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
12767 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12768 		vnic->mc_list_count = 0;
12769 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
12770 	}
12771 	if (rc)
12772 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
12773 			   rc);
12774 
12775 	return rc;
12776 }
12777 
12778 static bool bnxt_can_reserve_rings(struct bnxt *bp)
12779 {
12780 #ifdef CONFIG_BNXT_SRIOV
12781 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
12782 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12783 
12784 		/* No minimum rings were provisioned by the PF.  Don't
12785 		 * reserve rings by default when device is down.
12786 		 */
12787 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
12788 			return true;
12789 
12790 		if (!netif_running(bp->dev))
12791 			return false;
12792 	}
12793 #endif
12794 	return true;
12795 }
12796 
12797 /* If the chip and firmware supports RFS */
12798 static bool bnxt_rfs_supported(struct bnxt *bp)
12799 {
12800 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
12801 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
12802 			return true;
12803 		return false;
12804 	}
12805 	/* 212 firmware is broken for aRFS */
12806 	if (BNXT_FW_MAJ(bp) == 212)
12807 		return false;
12808 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
12809 		return true;
12810 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
12811 		return true;
12812 	return false;
12813 }
12814 
12815 /* If runtime conditions support RFS */
12816 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
12817 {
12818 	struct bnxt_hw_rings hwr = {0};
12819 	int max_vnics, max_rss_ctxs;
12820 
12821 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
12822 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
12823 		return bnxt_rfs_supported(bp);
12824 
12825 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
12826 		return false;
12827 
12828 	hwr.grp = bp->rx_nr_rings;
12829 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
12830 	if (new_rss_ctx)
12831 		hwr.vnic++;
12832 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
12833 	max_vnics = bnxt_get_max_func_vnics(bp);
12834 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
12835 
12836 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
12837 		if (bp->rx_nr_rings > 1)
12838 			netdev_warn(bp->dev,
12839 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
12840 				    min(max_rss_ctxs - 1, max_vnics - 1));
12841 		return false;
12842 	}
12843 
12844 	if (!BNXT_NEW_RM(bp))
12845 		return true;
12846 
12847 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
12848 	 * issue that will mess up the default VNIC if we reduce the
12849 	 * reservations.
12850 	 */
12851 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
12852 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
12853 		return true;
12854 
12855 	bnxt_hwrm_reserve_rings(bp, &hwr);
12856 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
12857 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
12858 		return true;
12859 
12860 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
12861 	hwr.vnic = 1;
12862 	hwr.rss_ctx = 0;
12863 	bnxt_hwrm_reserve_rings(bp, &hwr);
12864 	return false;
12865 }
12866 
12867 static netdev_features_t bnxt_fix_features(struct net_device *dev,
12868 					   netdev_features_t features)
12869 {
12870 	struct bnxt *bp = netdev_priv(dev);
12871 	netdev_features_t vlan_features;
12872 
12873 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
12874 		features &= ~NETIF_F_NTUPLE;
12875 
12876 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
12877 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
12878 
12879 	if (!(features & NETIF_F_GRO))
12880 		features &= ~NETIF_F_GRO_HW;
12881 
12882 	if (features & NETIF_F_GRO_HW)
12883 		features &= ~NETIF_F_LRO;
12884 
12885 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
12886 	 * turned on or off together.
12887 	 */
12888 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
12889 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
12890 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
12891 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
12892 		else if (vlan_features)
12893 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
12894 	}
12895 #ifdef CONFIG_BNXT_SRIOV
12896 	if (BNXT_VF(bp) && bp->vf.vlan)
12897 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
12898 #endif
12899 	return features;
12900 }
12901 
12902 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
12903 				bool link_re_init, u32 flags, bool update_tpa)
12904 {
12905 	bnxt_close_nic(bp, irq_re_init, link_re_init);
12906 	bp->flags = flags;
12907 	if (update_tpa)
12908 		bnxt_set_ring_params(bp);
12909 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
12910 }
12911 
12912 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
12913 {
12914 	bool update_tpa = false, update_ntuple = false;
12915 	struct bnxt *bp = netdev_priv(dev);
12916 	u32 flags = bp->flags;
12917 	u32 changes;
12918 	int rc = 0;
12919 	bool re_init = false;
12920 
12921 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
12922 	if (features & NETIF_F_GRO_HW)
12923 		flags |= BNXT_FLAG_GRO;
12924 	else if (features & NETIF_F_LRO)
12925 		flags |= BNXT_FLAG_LRO;
12926 
12927 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
12928 		flags &= ~BNXT_FLAG_TPA;
12929 
12930 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
12931 		flags |= BNXT_FLAG_STRIP_VLAN;
12932 
12933 	if (features & NETIF_F_NTUPLE)
12934 		flags |= BNXT_FLAG_RFS;
12935 	else
12936 		bnxt_clear_usr_fltrs(bp, true);
12937 
12938 	changes = flags ^ bp->flags;
12939 	if (changes & BNXT_FLAG_TPA) {
12940 		update_tpa = true;
12941 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
12942 		    (flags & BNXT_FLAG_TPA) == 0 ||
12943 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
12944 			re_init = true;
12945 	}
12946 
12947 	if (changes & ~BNXT_FLAG_TPA)
12948 		re_init = true;
12949 
12950 	if (changes & BNXT_FLAG_RFS)
12951 		update_ntuple = true;
12952 
12953 	if (flags != bp->flags) {
12954 		u32 old_flags = bp->flags;
12955 
12956 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12957 			bp->flags = flags;
12958 			if (update_tpa)
12959 				bnxt_set_ring_params(bp);
12960 			return rc;
12961 		}
12962 
12963 		if (update_ntuple)
12964 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
12965 
12966 		if (re_init)
12967 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
12968 
12969 		if (update_tpa) {
12970 			bp->flags = flags;
12971 			rc = bnxt_set_tpa(bp,
12972 					  (flags & BNXT_FLAG_TPA) ?
12973 					  true : false);
12974 			if (rc)
12975 				bp->flags = old_flags;
12976 		}
12977 	}
12978 	return rc;
12979 }
12980 
12981 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
12982 			      u8 **nextp)
12983 {
12984 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
12985 	struct hop_jumbo_hdr *jhdr;
12986 	int hdr_count = 0;
12987 	u8 *nexthdr;
12988 	int start;
12989 
12990 	/* Check that there are at most 2 IPv6 extension headers, no
12991 	 * fragment header, and each is <= 64 bytes.
12992 	 */
12993 	start = nw_off + sizeof(*ip6h);
12994 	nexthdr = &ip6h->nexthdr;
12995 	while (ipv6_ext_hdr(*nexthdr)) {
12996 		struct ipv6_opt_hdr *hp;
12997 		int hdrlen;
12998 
12999 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
13000 		    *nexthdr == NEXTHDR_FRAGMENT)
13001 			return false;
13002 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
13003 					  skb_headlen(skb), NULL);
13004 		if (!hp)
13005 			return false;
13006 		if (*nexthdr == NEXTHDR_AUTH)
13007 			hdrlen = ipv6_authlen(hp);
13008 		else
13009 			hdrlen = ipv6_optlen(hp);
13010 
13011 		if (hdrlen > 64)
13012 			return false;
13013 
13014 		/* The ext header may be a hop-by-hop header inserted for
13015 		 * big TCP purposes. This will be removed before sending
13016 		 * from NIC, so do not count it.
13017 		 */
13018 		if (*nexthdr == NEXTHDR_HOP) {
13019 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
13020 				goto increment_hdr;
13021 
13022 			jhdr = (struct hop_jumbo_hdr *)hp;
13023 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
13024 			    jhdr->nexthdr != IPPROTO_TCP)
13025 				goto increment_hdr;
13026 
13027 			goto next_hdr;
13028 		}
13029 increment_hdr:
13030 		hdr_count++;
13031 next_hdr:
13032 		nexthdr = &hp->nexthdr;
13033 		start += hdrlen;
13034 	}
13035 	if (nextp) {
13036 		/* Caller will check inner protocol */
13037 		if (skb->encapsulation) {
13038 			*nextp = nexthdr;
13039 			return true;
13040 		}
13041 		*nextp = NULL;
13042 	}
13043 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
13044 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
13045 }
13046 
13047 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
13048 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
13049 {
13050 	struct udphdr *uh = udp_hdr(skb);
13051 	__be16 udp_port = uh->dest;
13052 
13053 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
13054 	    udp_port != bp->vxlan_gpe_port)
13055 		return false;
13056 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
13057 		struct ethhdr *eh = inner_eth_hdr(skb);
13058 
13059 		switch (eh->h_proto) {
13060 		case htons(ETH_P_IP):
13061 			return true;
13062 		case htons(ETH_P_IPV6):
13063 			return bnxt_exthdr_check(bp, skb,
13064 						 skb_inner_network_offset(skb),
13065 						 NULL);
13066 		}
13067 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
13068 		return true;
13069 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
13070 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13071 					 NULL);
13072 	}
13073 	return false;
13074 }
13075 
13076 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
13077 {
13078 	switch (l4_proto) {
13079 	case IPPROTO_UDP:
13080 		return bnxt_udp_tunl_check(bp, skb);
13081 	case IPPROTO_IPIP:
13082 		return true;
13083 	case IPPROTO_GRE: {
13084 		switch (skb->inner_protocol) {
13085 		default:
13086 			return false;
13087 		case htons(ETH_P_IP):
13088 			return true;
13089 		case htons(ETH_P_IPV6):
13090 			fallthrough;
13091 		}
13092 	}
13093 	case IPPROTO_IPV6:
13094 		/* Check ext headers of inner ipv6 */
13095 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13096 					 NULL);
13097 	}
13098 	return false;
13099 }
13100 
13101 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
13102 					     struct net_device *dev,
13103 					     netdev_features_t features)
13104 {
13105 	struct bnxt *bp = netdev_priv(dev);
13106 	u8 *l4_proto;
13107 
13108 	features = vlan_features_check(skb, features);
13109 	switch (vlan_get_protocol(skb)) {
13110 	case htons(ETH_P_IP):
13111 		if (!skb->encapsulation)
13112 			return features;
13113 		l4_proto = &ip_hdr(skb)->protocol;
13114 		if (bnxt_tunl_check(bp, skb, *l4_proto))
13115 			return features;
13116 		break;
13117 	case htons(ETH_P_IPV6):
13118 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
13119 				       &l4_proto))
13120 			break;
13121 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
13122 			return features;
13123 		break;
13124 	}
13125 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13126 }
13127 
13128 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
13129 			 u32 *reg_buf)
13130 {
13131 	struct hwrm_dbg_read_direct_output *resp;
13132 	struct hwrm_dbg_read_direct_input *req;
13133 	__le32 *dbg_reg_buf;
13134 	dma_addr_t mapping;
13135 	int rc, i;
13136 
13137 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
13138 	if (rc)
13139 		return rc;
13140 
13141 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
13142 					 &mapping);
13143 	if (!dbg_reg_buf) {
13144 		rc = -ENOMEM;
13145 		goto dbg_rd_reg_exit;
13146 	}
13147 
13148 	req->host_dest_addr = cpu_to_le64(mapping);
13149 
13150 	resp = hwrm_req_hold(bp, req);
13151 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
13152 	req->read_len32 = cpu_to_le32(num_words);
13153 
13154 	rc = hwrm_req_send(bp, req);
13155 	if (rc || resp->error_code) {
13156 		rc = -EIO;
13157 		goto dbg_rd_reg_exit;
13158 	}
13159 	for (i = 0; i < num_words; i++)
13160 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
13161 
13162 dbg_rd_reg_exit:
13163 	hwrm_req_drop(bp, req);
13164 	return rc;
13165 }
13166 
13167 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
13168 				       u32 ring_id, u32 *prod, u32 *cons)
13169 {
13170 	struct hwrm_dbg_ring_info_get_output *resp;
13171 	struct hwrm_dbg_ring_info_get_input *req;
13172 	int rc;
13173 
13174 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
13175 	if (rc)
13176 		return rc;
13177 
13178 	req->ring_type = ring_type;
13179 	req->fw_ring_id = cpu_to_le32(ring_id);
13180 	resp = hwrm_req_hold(bp, req);
13181 	rc = hwrm_req_send(bp, req);
13182 	if (!rc) {
13183 		*prod = le32_to_cpu(resp->producer_index);
13184 		*cons = le32_to_cpu(resp->consumer_index);
13185 	}
13186 	hwrm_req_drop(bp, req);
13187 	return rc;
13188 }
13189 
13190 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13191 {
13192 	struct bnxt_tx_ring_info *txr;
13193 	int i = bnapi->index, j;
13194 
13195 	bnxt_for_each_napi_tx(j, bnapi, txr)
13196 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13197 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13198 			    txr->tx_cons);
13199 }
13200 
13201 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
13202 {
13203 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
13204 	int i = bnapi->index;
13205 
13206 	if (!rxr)
13207 		return;
13208 
13209 	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",
13210 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
13211 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
13212 		    rxr->rx_sw_agg_prod);
13213 }
13214 
13215 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
13216 {
13217 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13218 	int i = bnapi->index;
13219 
13220 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
13221 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
13222 }
13223 
13224 static void bnxt_dbg_dump_states(struct bnxt *bp)
13225 {
13226 	int i;
13227 	struct bnxt_napi *bnapi;
13228 
13229 	for (i = 0; i < bp->cp_nr_rings; i++) {
13230 		bnapi = bp->bnapi[i];
13231 		if (netif_msg_drv(bp)) {
13232 			bnxt_dump_tx_sw_state(bnapi);
13233 			bnxt_dump_rx_sw_state(bnapi);
13234 			bnxt_dump_cp_sw_state(bnapi);
13235 		}
13236 	}
13237 }
13238 
13239 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
13240 {
13241 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
13242 	struct hwrm_ring_reset_input *req;
13243 	struct bnxt_napi *bnapi = rxr->bnapi;
13244 	struct bnxt_cp_ring_info *cpr;
13245 	u16 cp_ring_id;
13246 	int rc;
13247 
13248 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
13249 	if (rc)
13250 		return rc;
13251 
13252 	cpr = &bnapi->cp_ring;
13253 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
13254 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
13255 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
13256 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
13257 	return hwrm_req_send_silent(bp, req);
13258 }
13259 
13260 static void bnxt_reset_task(struct bnxt *bp, bool silent)
13261 {
13262 	if (!silent)
13263 		bnxt_dbg_dump_states(bp);
13264 	if (netif_running(bp->dev)) {
13265 		bnxt_close_nic(bp, !silent, false);
13266 		bnxt_open_nic(bp, !silent, false);
13267 	}
13268 }
13269 
13270 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
13271 {
13272 	struct bnxt *bp = netdev_priv(dev);
13273 
13274 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
13275 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
13276 }
13277 
13278 static void bnxt_fw_health_check(struct bnxt *bp)
13279 {
13280 	struct bnxt_fw_health *fw_health = bp->fw_health;
13281 	struct pci_dev *pdev = bp->pdev;
13282 	u32 val;
13283 
13284 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13285 		return;
13286 
13287 	/* Make sure it is enabled before checking the tmr_counter. */
13288 	smp_rmb();
13289 	if (fw_health->tmr_counter) {
13290 		fw_health->tmr_counter--;
13291 		return;
13292 	}
13293 
13294 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13295 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
13296 		fw_health->arrests++;
13297 		goto fw_reset;
13298 	}
13299 
13300 	fw_health->last_fw_heartbeat = val;
13301 
13302 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13303 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
13304 		fw_health->discoveries++;
13305 		goto fw_reset;
13306 	}
13307 
13308 	fw_health->tmr_counter = fw_health->tmr_multiplier;
13309 	return;
13310 
13311 fw_reset:
13312 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
13313 }
13314 
13315 static void bnxt_timer(struct timer_list *t)
13316 {
13317 	struct bnxt *bp = from_timer(bp, t, timer);
13318 	struct net_device *dev = bp->dev;
13319 
13320 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
13321 		return;
13322 
13323 	if (atomic_read(&bp->intr_sem) != 0)
13324 		goto bnxt_restart_timer;
13325 
13326 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
13327 		bnxt_fw_health_check(bp);
13328 
13329 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
13330 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
13331 
13332 	if (bnxt_tc_flower_enabled(bp))
13333 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
13334 
13335 #ifdef CONFIG_RFS_ACCEL
13336 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
13337 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
13338 #endif /*CONFIG_RFS_ACCEL*/
13339 
13340 	if (bp->link_info.phy_retry) {
13341 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
13342 			bp->link_info.phy_retry = false;
13343 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
13344 		} else {
13345 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
13346 		}
13347 	}
13348 
13349 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13350 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13351 
13352 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
13353 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
13354 
13355 bnxt_restart_timer:
13356 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13357 }
13358 
13359 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
13360 {
13361 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
13362 	 * set.  If the device is being closed, bnxt_close() may be holding
13363 	 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear.  So we
13364 	 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
13365 	 */
13366 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13367 	rtnl_lock();
13368 }
13369 
13370 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
13371 {
13372 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13373 	rtnl_unlock();
13374 }
13375 
13376 /* Only called from bnxt_sp_task() */
13377 static void bnxt_reset(struct bnxt *bp, bool silent)
13378 {
13379 	bnxt_rtnl_lock_sp(bp);
13380 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
13381 		bnxt_reset_task(bp, silent);
13382 	bnxt_rtnl_unlock_sp(bp);
13383 }
13384 
13385 /* Only called from bnxt_sp_task() */
13386 static void bnxt_rx_ring_reset(struct bnxt *bp)
13387 {
13388 	int i;
13389 
13390 	bnxt_rtnl_lock_sp(bp);
13391 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13392 		bnxt_rtnl_unlock_sp(bp);
13393 		return;
13394 	}
13395 	/* Disable and flush TPA before resetting the RX ring */
13396 	if (bp->flags & BNXT_FLAG_TPA)
13397 		bnxt_set_tpa(bp, false);
13398 	for (i = 0; i < bp->rx_nr_rings; i++) {
13399 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
13400 		struct bnxt_cp_ring_info *cpr;
13401 		int rc;
13402 
13403 		if (!rxr->bnapi->in_reset)
13404 			continue;
13405 
13406 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
13407 		if (rc) {
13408 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
13409 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
13410 			else
13411 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
13412 					    rc);
13413 			bnxt_reset_task(bp, true);
13414 			break;
13415 		}
13416 		bnxt_free_one_rx_ring_skbs(bp, i);
13417 		rxr->rx_prod = 0;
13418 		rxr->rx_agg_prod = 0;
13419 		rxr->rx_sw_agg_prod = 0;
13420 		rxr->rx_next_cons = 0;
13421 		rxr->bnapi->in_reset = false;
13422 		bnxt_alloc_one_rx_ring(bp, i);
13423 		cpr = &rxr->bnapi->cp_ring;
13424 		cpr->sw_stats->rx.rx_resets++;
13425 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
13426 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
13427 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
13428 	}
13429 	if (bp->flags & BNXT_FLAG_TPA)
13430 		bnxt_set_tpa(bp, true);
13431 	bnxt_rtnl_unlock_sp(bp);
13432 }
13433 
13434 static void bnxt_fw_fatal_close(struct bnxt *bp)
13435 {
13436 	bnxt_tx_disable(bp);
13437 	bnxt_disable_napi(bp);
13438 	bnxt_disable_int_sync(bp);
13439 	bnxt_free_irq(bp);
13440 	bnxt_clear_int_mode(bp);
13441 	pci_disable_device(bp->pdev);
13442 }
13443 
13444 static void bnxt_fw_reset_close(struct bnxt *bp)
13445 {
13446 	/* When firmware is in fatal state, quiesce device and disable
13447 	 * bus master to prevent any potential bad DMAs before freeing
13448 	 * kernel memory.
13449 	 */
13450 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
13451 		u16 val = 0;
13452 
13453 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
13454 		if (val == 0xffff)
13455 			bp->fw_reset_min_dsecs = 0;
13456 		bnxt_fw_fatal_close(bp);
13457 	}
13458 	__bnxt_close_nic(bp, true, false);
13459 	bnxt_vf_reps_free(bp);
13460 	bnxt_clear_int_mode(bp);
13461 	bnxt_hwrm_func_drv_unrgtr(bp);
13462 	if (pci_is_enabled(bp->pdev))
13463 		pci_disable_device(bp->pdev);
13464 	bnxt_free_ctx_mem(bp);
13465 }
13466 
13467 static bool is_bnxt_fw_ok(struct bnxt *bp)
13468 {
13469 	struct bnxt_fw_health *fw_health = bp->fw_health;
13470 	bool no_heartbeat = false, has_reset = false;
13471 	u32 val;
13472 
13473 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13474 	if (val == fw_health->last_fw_heartbeat)
13475 		no_heartbeat = true;
13476 
13477 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13478 	if (val != fw_health->last_fw_reset_cnt)
13479 		has_reset = true;
13480 
13481 	if (!no_heartbeat && has_reset)
13482 		return true;
13483 
13484 	return false;
13485 }
13486 
13487 /* rtnl_lock is acquired before calling this function */
13488 static void bnxt_force_fw_reset(struct bnxt *bp)
13489 {
13490 	struct bnxt_fw_health *fw_health = bp->fw_health;
13491 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13492 	u32 wait_dsecs;
13493 
13494 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
13495 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13496 		return;
13497 
13498 	/* we have to serialize with bnxt_refclk_read()*/
13499 	if (ptp) {
13500 		unsigned long flags;
13501 
13502 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
13503 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13504 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
13505 	} else {
13506 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13507 	}
13508 	bnxt_fw_reset_close(bp);
13509 	wait_dsecs = fw_health->master_func_wait_dsecs;
13510 	if (fw_health->primary) {
13511 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
13512 			wait_dsecs = 0;
13513 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
13514 	} else {
13515 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
13516 		wait_dsecs = fw_health->normal_func_wait_dsecs;
13517 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13518 	}
13519 
13520 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
13521 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
13522 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
13523 }
13524 
13525 void bnxt_fw_exception(struct bnxt *bp)
13526 {
13527 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
13528 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
13529 	bnxt_ulp_stop(bp);
13530 	bnxt_rtnl_lock_sp(bp);
13531 	bnxt_force_fw_reset(bp);
13532 	bnxt_rtnl_unlock_sp(bp);
13533 }
13534 
13535 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
13536  * < 0 on error.
13537  */
13538 static int bnxt_get_registered_vfs(struct bnxt *bp)
13539 {
13540 #ifdef CONFIG_BNXT_SRIOV
13541 	int rc;
13542 
13543 	if (!BNXT_PF(bp))
13544 		return 0;
13545 
13546 	rc = bnxt_hwrm_func_qcfg(bp);
13547 	if (rc) {
13548 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
13549 		return rc;
13550 	}
13551 	if (bp->pf.registered_vfs)
13552 		return bp->pf.registered_vfs;
13553 	if (bp->sriov_cfg)
13554 		return 1;
13555 #endif
13556 	return 0;
13557 }
13558 
13559 void bnxt_fw_reset(struct bnxt *bp)
13560 {
13561 	bnxt_ulp_stop(bp);
13562 	bnxt_rtnl_lock_sp(bp);
13563 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
13564 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13565 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13566 		int n = 0, tmo;
13567 
13568 		/* we have to serialize with bnxt_refclk_read()*/
13569 		if (ptp) {
13570 			unsigned long flags;
13571 
13572 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
13573 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13574 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
13575 		} else {
13576 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13577 		}
13578 		if (bp->pf.active_vfs &&
13579 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
13580 			n = bnxt_get_registered_vfs(bp);
13581 		if (n < 0) {
13582 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
13583 				   n);
13584 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13585 			dev_close(bp->dev);
13586 			goto fw_reset_exit;
13587 		} else if (n > 0) {
13588 			u16 vf_tmo_dsecs = n * 10;
13589 
13590 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
13591 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
13592 			bp->fw_reset_state =
13593 				BNXT_FW_RESET_STATE_POLL_VF;
13594 			bnxt_queue_fw_reset_work(bp, HZ / 10);
13595 			goto fw_reset_exit;
13596 		}
13597 		bnxt_fw_reset_close(bp);
13598 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
13599 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
13600 			tmo = HZ / 10;
13601 		} else {
13602 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13603 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
13604 		}
13605 		bnxt_queue_fw_reset_work(bp, tmo);
13606 	}
13607 fw_reset_exit:
13608 	bnxt_rtnl_unlock_sp(bp);
13609 }
13610 
13611 static void bnxt_chk_missed_irq(struct bnxt *bp)
13612 {
13613 	int i;
13614 
13615 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13616 		return;
13617 
13618 	for (i = 0; i < bp->cp_nr_rings; i++) {
13619 		struct bnxt_napi *bnapi = bp->bnapi[i];
13620 		struct bnxt_cp_ring_info *cpr;
13621 		u32 fw_ring_id;
13622 		int j;
13623 
13624 		if (!bnapi)
13625 			continue;
13626 
13627 		cpr = &bnapi->cp_ring;
13628 		for (j = 0; j < cpr->cp_ring_count; j++) {
13629 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
13630 			u32 val[2];
13631 
13632 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
13633 				continue;
13634 
13635 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
13636 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
13637 				continue;
13638 			}
13639 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
13640 			bnxt_dbg_hwrm_ring_info_get(bp,
13641 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
13642 				fw_ring_id, &val[0], &val[1]);
13643 			cpr->sw_stats->cmn.missed_irqs++;
13644 		}
13645 	}
13646 }
13647 
13648 static void bnxt_cfg_ntp_filters(struct bnxt *);
13649 
13650 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
13651 {
13652 	struct bnxt_link_info *link_info = &bp->link_info;
13653 
13654 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
13655 		link_info->autoneg = BNXT_AUTONEG_SPEED;
13656 		if (bp->hwrm_spec_code >= 0x10201) {
13657 			if (link_info->auto_pause_setting &
13658 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
13659 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13660 		} else {
13661 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13662 		}
13663 		bnxt_set_auto_speed(link_info);
13664 	} else {
13665 		bnxt_set_force_speed(link_info);
13666 		link_info->req_duplex = link_info->duplex_setting;
13667 	}
13668 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
13669 		link_info->req_flow_ctrl =
13670 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
13671 	else
13672 		link_info->req_flow_ctrl = link_info->force_pause_setting;
13673 }
13674 
13675 static void bnxt_fw_echo_reply(struct bnxt *bp)
13676 {
13677 	struct bnxt_fw_health *fw_health = bp->fw_health;
13678 	struct hwrm_func_echo_response_input *req;
13679 	int rc;
13680 
13681 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
13682 	if (rc)
13683 		return;
13684 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
13685 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
13686 	hwrm_req_send(bp, req);
13687 }
13688 
13689 static void bnxt_ulp_restart(struct bnxt *bp)
13690 {
13691 	bnxt_ulp_stop(bp);
13692 	bnxt_ulp_start(bp, 0);
13693 }
13694 
13695 static void bnxt_sp_task(struct work_struct *work)
13696 {
13697 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
13698 
13699 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13700 	smp_mb__after_atomic();
13701 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13702 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13703 		return;
13704 	}
13705 
13706 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
13707 		bnxt_ulp_restart(bp);
13708 		bnxt_reenable_sriov(bp);
13709 	}
13710 
13711 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
13712 		bnxt_cfg_rx_mode(bp);
13713 
13714 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
13715 		bnxt_cfg_ntp_filters(bp);
13716 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
13717 		bnxt_hwrm_exec_fwd_req(bp);
13718 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
13719 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
13720 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
13721 		bnxt_hwrm_port_qstats(bp, 0);
13722 		bnxt_hwrm_port_qstats_ext(bp, 0);
13723 		bnxt_accumulate_all_stats(bp);
13724 	}
13725 
13726 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
13727 		int rc;
13728 
13729 		mutex_lock(&bp->link_lock);
13730 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
13731 				       &bp->sp_event))
13732 			bnxt_hwrm_phy_qcaps(bp);
13733 
13734 		rc = bnxt_update_link(bp, true);
13735 		if (rc)
13736 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
13737 				   rc);
13738 
13739 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
13740 				       &bp->sp_event))
13741 			bnxt_init_ethtool_link_settings(bp);
13742 		mutex_unlock(&bp->link_lock);
13743 	}
13744 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
13745 		int rc;
13746 
13747 		mutex_lock(&bp->link_lock);
13748 		rc = bnxt_update_phy_setting(bp);
13749 		mutex_unlock(&bp->link_lock);
13750 		if (rc) {
13751 			netdev_warn(bp->dev, "update phy settings retry failed\n");
13752 		} else {
13753 			bp->link_info.phy_retry = false;
13754 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
13755 		}
13756 	}
13757 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
13758 		mutex_lock(&bp->link_lock);
13759 		bnxt_get_port_module_status(bp);
13760 		mutex_unlock(&bp->link_lock);
13761 	}
13762 
13763 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
13764 		bnxt_tc_flow_stats_work(bp);
13765 
13766 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
13767 		bnxt_chk_missed_irq(bp);
13768 
13769 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
13770 		bnxt_fw_echo_reply(bp);
13771 
13772 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
13773 		bnxt_hwmon_notify_event(bp);
13774 
13775 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
13776 	 * must be the last functions to be called before exiting.
13777 	 */
13778 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
13779 		bnxt_reset(bp, false);
13780 
13781 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
13782 		bnxt_reset(bp, true);
13783 
13784 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
13785 		bnxt_rx_ring_reset(bp);
13786 
13787 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
13788 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
13789 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
13790 			bnxt_devlink_health_fw_report(bp);
13791 		else
13792 			bnxt_fw_reset(bp);
13793 	}
13794 
13795 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
13796 		if (!is_bnxt_fw_ok(bp))
13797 			bnxt_devlink_health_fw_report(bp);
13798 	}
13799 
13800 	smp_mb__before_atomic();
13801 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13802 }
13803 
13804 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
13805 				int *max_cp);
13806 
13807 /* Under rtnl_lock */
13808 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
13809 		     int tx_xdp)
13810 {
13811 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
13812 	struct bnxt_hw_rings hwr = {0};
13813 	int rx_rings = rx;
13814 	int rc;
13815 
13816 	if (tcs)
13817 		tx_sets = tcs;
13818 
13819 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
13820 
13821 	if (max_rx < rx_rings)
13822 		return -ENOMEM;
13823 
13824 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
13825 		rx_rings <<= 1;
13826 
13827 	hwr.rx = rx_rings;
13828 	hwr.tx = tx * tx_sets + tx_xdp;
13829 	if (max_tx < hwr.tx)
13830 		return -ENOMEM;
13831 
13832 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
13833 
13834 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
13835 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
13836 	if (max_cp < hwr.cp)
13837 		return -ENOMEM;
13838 	hwr.stat = hwr.cp;
13839 	if (BNXT_NEW_RM(bp)) {
13840 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
13841 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
13842 		hwr.grp = rx;
13843 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13844 	}
13845 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
13846 		hwr.cp_p5 = hwr.tx + rx;
13847 	rc = bnxt_hwrm_check_rings(bp, &hwr);
13848 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
13849 		if (!bnxt_ulp_registered(bp->edev)) {
13850 			hwr.cp += bnxt_get_ulp_msix_num(bp);
13851 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
13852 		}
13853 		if (hwr.cp > bp->total_irqs) {
13854 			int total_msix = bnxt_change_msix(bp, hwr.cp);
13855 
13856 			if (total_msix < hwr.cp) {
13857 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
13858 					    hwr.cp, total_msix);
13859 				rc = -ENOSPC;
13860 			}
13861 		}
13862 	}
13863 	return rc;
13864 }
13865 
13866 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
13867 {
13868 	if (bp->bar2) {
13869 		pci_iounmap(pdev, bp->bar2);
13870 		bp->bar2 = NULL;
13871 	}
13872 
13873 	if (bp->bar1) {
13874 		pci_iounmap(pdev, bp->bar1);
13875 		bp->bar1 = NULL;
13876 	}
13877 
13878 	if (bp->bar0) {
13879 		pci_iounmap(pdev, bp->bar0);
13880 		bp->bar0 = NULL;
13881 	}
13882 }
13883 
13884 static void bnxt_cleanup_pci(struct bnxt *bp)
13885 {
13886 	bnxt_unmap_bars(bp, bp->pdev);
13887 	pci_release_regions(bp->pdev);
13888 	if (pci_is_enabled(bp->pdev))
13889 		pci_disable_device(bp->pdev);
13890 }
13891 
13892 static void bnxt_init_dflt_coal(struct bnxt *bp)
13893 {
13894 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
13895 	struct bnxt_coal *coal;
13896 	u16 flags = 0;
13897 
13898 	if (coal_cap->cmpl_params &
13899 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
13900 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
13901 
13902 	/* Tick values in micro seconds.
13903 	 * 1 coal_buf x bufs_per_record = 1 completion record.
13904 	 */
13905 	coal = &bp->rx_coal;
13906 	coal->coal_ticks = 10;
13907 	coal->coal_bufs = 30;
13908 	coal->coal_ticks_irq = 1;
13909 	coal->coal_bufs_irq = 2;
13910 	coal->idle_thresh = 50;
13911 	coal->bufs_per_record = 2;
13912 	coal->budget = 64;		/* NAPI budget */
13913 	coal->flags = flags;
13914 
13915 	coal = &bp->tx_coal;
13916 	coal->coal_ticks = 28;
13917 	coal->coal_bufs = 30;
13918 	coal->coal_ticks_irq = 2;
13919 	coal->coal_bufs_irq = 2;
13920 	coal->bufs_per_record = 1;
13921 	coal->flags = flags;
13922 
13923 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
13924 }
13925 
13926 /* FW that pre-reserves 1 VNIC per function */
13927 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
13928 {
13929 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
13930 
13931 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13932 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
13933 		return true;
13934 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13935 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
13936 		return true;
13937 	return false;
13938 }
13939 
13940 static int bnxt_fw_init_one_p1(struct bnxt *bp)
13941 {
13942 	int rc;
13943 
13944 	bp->fw_cap = 0;
13945 	rc = bnxt_hwrm_ver_get(bp);
13946 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
13947 	 * so wait before continuing with recovery.
13948 	 */
13949 	if (rc)
13950 		msleep(100);
13951 	bnxt_try_map_fw_health_reg(bp);
13952 	if (rc) {
13953 		rc = bnxt_try_recover_fw(bp);
13954 		if (rc)
13955 			return rc;
13956 		rc = bnxt_hwrm_ver_get(bp);
13957 		if (rc)
13958 			return rc;
13959 	}
13960 
13961 	bnxt_nvm_cfg_ver_get(bp);
13962 
13963 	rc = bnxt_hwrm_func_reset(bp);
13964 	if (rc)
13965 		return -ENODEV;
13966 
13967 	bnxt_hwrm_fw_set_time(bp);
13968 	return 0;
13969 }
13970 
13971 static int bnxt_fw_init_one_p2(struct bnxt *bp)
13972 {
13973 	int rc;
13974 
13975 	/* Get the MAX capabilities for this function */
13976 	rc = bnxt_hwrm_func_qcaps(bp);
13977 	if (rc) {
13978 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
13979 			   rc);
13980 		return -ENODEV;
13981 	}
13982 
13983 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
13984 	if (rc)
13985 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
13986 			    rc);
13987 
13988 	if (bnxt_alloc_fw_health(bp)) {
13989 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
13990 	} else {
13991 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
13992 		if (rc)
13993 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
13994 				    rc);
13995 	}
13996 
13997 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
13998 	if (rc)
13999 		return -ENODEV;
14000 
14001 	rc = bnxt_alloc_crash_dump_mem(bp);
14002 	if (rc)
14003 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
14004 			    rc);
14005 	if (!rc) {
14006 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
14007 		if (rc) {
14008 			bnxt_free_crash_dump_mem(bp);
14009 			netdev_warn(bp->dev,
14010 				    "hwrm crash dump mem failure rc: %d\n", rc);
14011 		}
14012 	}
14013 
14014 	if (bnxt_fw_pre_resv_vnics(bp))
14015 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
14016 
14017 	bnxt_hwrm_func_qcfg(bp);
14018 	bnxt_hwrm_vnic_qcaps(bp);
14019 	bnxt_hwrm_port_led_qcaps(bp);
14020 	bnxt_ethtool_init(bp);
14021 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
14022 		__bnxt_hwrm_ptp_qcfg(bp);
14023 	bnxt_dcb_init(bp);
14024 	bnxt_hwmon_init(bp);
14025 	return 0;
14026 }
14027 
14028 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
14029 {
14030 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
14031 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
14032 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
14033 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
14034 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
14035 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
14036 		bp->rss_hash_delta = bp->rss_hash_cfg;
14037 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
14038 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
14039 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
14040 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
14041 	}
14042 }
14043 
14044 static void bnxt_set_dflt_rfs(struct bnxt *bp)
14045 {
14046 	struct net_device *dev = bp->dev;
14047 
14048 	dev->hw_features &= ~NETIF_F_NTUPLE;
14049 	dev->features &= ~NETIF_F_NTUPLE;
14050 	bp->flags &= ~BNXT_FLAG_RFS;
14051 	if (bnxt_rfs_supported(bp)) {
14052 		dev->hw_features |= NETIF_F_NTUPLE;
14053 		if (bnxt_rfs_capable(bp, false)) {
14054 			bp->flags |= BNXT_FLAG_RFS;
14055 			dev->features |= NETIF_F_NTUPLE;
14056 		}
14057 	}
14058 }
14059 
14060 static void bnxt_fw_init_one_p3(struct bnxt *bp)
14061 {
14062 	struct pci_dev *pdev = bp->pdev;
14063 
14064 	bnxt_set_dflt_rss_hash_type(bp);
14065 	bnxt_set_dflt_rfs(bp);
14066 
14067 	bnxt_get_wol_settings(bp);
14068 	if (bp->flags & BNXT_FLAG_WOL_CAP)
14069 		device_set_wakeup_enable(&pdev->dev, bp->wol);
14070 	else
14071 		device_set_wakeup_capable(&pdev->dev, false);
14072 
14073 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
14074 	bnxt_hwrm_coal_params_qcaps(bp);
14075 }
14076 
14077 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
14078 
14079 int bnxt_fw_init_one(struct bnxt *bp)
14080 {
14081 	int rc;
14082 
14083 	rc = bnxt_fw_init_one_p1(bp);
14084 	if (rc) {
14085 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
14086 		return rc;
14087 	}
14088 	rc = bnxt_fw_init_one_p2(bp);
14089 	if (rc) {
14090 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
14091 		return rc;
14092 	}
14093 	rc = bnxt_probe_phy(bp, false);
14094 	if (rc)
14095 		return rc;
14096 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
14097 	if (rc)
14098 		return rc;
14099 
14100 	bnxt_fw_init_one_p3(bp);
14101 	return 0;
14102 }
14103 
14104 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
14105 {
14106 	struct bnxt_fw_health *fw_health = bp->fw_health;
14107 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
14108 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
14109 	u32 reg_type, reg_off, delay_msecs;
14110 
14111 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
14112 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
14113 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
14114 	switch (reg_type) {
14115 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
14116 		pci_write_config_dword(bp->pdev, reg_off, val);
14117 		break;
14118 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
14119 		writel(reg_off & BNXT_GRC_BASE_MASK,
14120 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
14121 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
14122 		fallthrough;
14123 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
14124 		writel(val, bp->bar0 + reg_off);
14125 		break;
14126 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
14127 		writel(val, bp->bar1 + reg_off);
14128 		break;
14129 	}
14130 	if (delay_msecs) {
14131 		pci_read_config_dword(bp->pdev, 0, &val);
14132 		msleep(delay_msecs);
14133 	}
14134 }
14135 
14136 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
14137 {
14138 	struct hwrm_func_qcfg_output *resp;
14139 	struct hwrm_func_qcfg_input *req;
14140 	bool result = true; /* firmware will enforce if unknown */
14141 
14142 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
14143 		return result;
14144 
14145 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
14146 		return result;
14147 
14148 	req->fid = cpu_to_le16(0xffff);
14149 	resp = hwrm_req_hold(bp, req);
14150 	if (!hwrm_req_send(bp, req))
14151 		result = !!(le16_to_cpu(resp->flags) &
14152 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
14153 	hwrm_req_drop(bp, req);
14154 	return result;
14155 }
14156 
14157 static void bnxt_reset_all(struct bnxt *bp)
14158 {
14159 	struct bnxt_fw_health *fw_health = bp->fw_health;
14160 	int i, rc;
14161 
14162 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14163 		bnxt_fw_reset_via_optee(bp);
14164 		bp->fw_reset_timestamp = jiffies;
14165 		return;
14166 	}
14167 
14168 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
14169 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
14170 			bnxt_fw_reset_writel(bp, i);
14171 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
14172 		struct hwrm_fw_reset_input *req;
14173 
14174 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
14175 		if (!rc) {
14176 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
14177 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
14178 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
14179 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
14180 			rc = hwrm_req_send(bp, req);
14181 		}
14182 		if (rc != -ENODEV)
14183 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
14184 	}
14185 	bp->fw_reset_timestamp = jiffies;
14186 }
14187 
14188 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
14189 {
14190 	return time_after(jiffies, bp->fw_reset_timestamp +
14191 			  (bp->fw_reset_max_dsecs * HZ / 10));
14192 }
14193 
14194 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
14195 {
14196 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14197 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
14198 		bnxt_dl_health_fw_status_update(bp, false);
14199 	bp->fw_reset_state = 0;
14200 	dev_close(bp->dev);
14201 }
14202 
14203 static void bnxt_fw_reset_task(struct work_struct *work)
14204 {
14205 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
14206 	int rc = 0;
14207 
14208 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14209 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
14210 		return;
14211 	}
14212 
14213 	switch (bp->fw_reset_state) {
14214 	case BNXT_FW_RESET_STATE_POLL_VF: {
14215 		int n = bnxt_get_registered_vfs(bp);
14216 		int tmo;
14217 
14218 		if (n < 0) {
14219 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
14220 				   n, jiffies_to_msecs(jiffies -
14221 				   bp->fw_reset_timestamp));
14222 			goto fw_reset_abort;
14223 		} else if (n > 0) {
14224 			if (bnxt_fw_reset_timeout(bp)) {
14225 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14226 				bp->fw_reset_state = 0;
14227 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
14228 					   n);
14229 				goto ulp_start;
14230 			}
14231 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14232 			return;
14233 		}
14234 		bp->fw_reset_timestamp = jiffies;
14235 		rtnl_lock();
14236 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
14237 			bnxt_fw_reset_abort(bp, rc);
14238 			rtnl_unlock();
14239 			goto ulp_start;
14240 		}
14241 		bnxt_fw_reset_close(bp);
14242 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14243 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14244 			tmo = HZ / 10;
14245 		} else {
14246 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14247 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14248 		}
14249 		rtnl_unlock();
14250 		bnxt_queue_fw_reset_work(bp, tmo);
14251 		return;
14252 	}
14253 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
14254 		u32 val;
14255 
14256 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14257 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
14258 		    !bnxt_fw_reset_timeout(bp)) {
14259 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14260 			return;
14261 		}
14262 
14263 		if (!bp->fw_health->primary) {
14264 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
14265 
14266 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14267 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14268 			return;
14269 		}
14270 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14271 	}
14272 		fallthrough;
14273 	case BNXT_FW_RESET_STATE_RESET_FW:
14274 		bnxt_reset_all(bp);
14275 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14276 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
14277 		return;
14278 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
14279 		bnxt_inv_fw_health_reg(bp);
14280 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
14281 		    !bp->fw_reset_min_dsecs) {
14282 			u16 val;
14283 
14284 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14285 			if (val == 0xffff) {
14286 				if (bnxt_fw_reset_timeout(bp)) {
14287 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
14288 					rc = -ETIMEDOUT;
14289 					goto fw_reset_abort;
14290 				}
14291 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
14292 				return;
14293 			}
14294 		}
14295 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14296 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
14297 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
14298 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
14299 			bnxt_dl_remote_reload(bp);
14300 		if (pci_enable_device(bp->pdev)) {
14301 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
14302 			rc = -ENODEV;
14303 			goto fw_reset_abort;
14304 		}
14305 		pci_set_master(bp->pdev);
14306 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
14307 		fallthrough;
14308 	case BNXT_FW_RESET_STATE_POLL_FW:
14309 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
14310 		rc = bnxt_hwrm_poll(bp);
14311 		if (rc) {
14312 			if (bnxt_fw_reset_timeout(bp)) {
14313 				netdev_err(bp->dev, "Firmware reset aborted\n");
14314 				goto fw_reset_abort_status;
14315 			}
14316 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14317 			return;
14318 		}
14319 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
14320 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
14321 		fallthrough;
14322 	case BNXT_FW_RESET_STATE_OPENING:
14323 		while (!rtnl_trylock()) {
14324 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14325 			return;
14326 		}
14327 		rc = bnxt_open(bp->dev);
14328 		if (rc) {
14329 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
14330 			bnxt_fw_reset_abort(bp, rc);
14331 			rtnl_unlock();
14332 			goto ulp_start;
14333 		}
14334 
14335 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
14336 		    bp->fw_health->enabled) {
14337 			bp->fw_health->last_fw_reset_cnt =
14338 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14339 		}
14340 		bp->fw_reset_state = 0;
14341 		/* Make sure fw_reset_state is 0 before clearing the flag */
14342 		smp_mb__before_atomic();
14343 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14344 		bnxt_ptp_reapply_pps(bp);
14345 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
14346 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
14347 			bnxt_dl_health_fw_recovery_done(bp);
14348 			bnxt_dl_health_fw_status_update(bp, true);
14349 		}
14350 		rtnl_unlock();
14351 		bnxt_ulp_start(bp, 0);
14352 		bnxt_reenable_sriov(bp);
14353 		rtnl_lock();
14354 		bnxt_vf_reps_alloc(bp);
14355 		bnxt_vf_reps_open(bp);
14356 		rtnl_unlock();
14357 		break;
14358 	}
14359 	return;
14360 
14361 fw_reset_abort_status:
14362 	if (bp->fw_health->status_reliable ||
14363 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
14364 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14365 
14366 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
14367 	}
14368 fw_reset_abort:
14369 	rtnl_lock();
14370 	bnxt_fw_reset_abort(bp, rc);
14371 	rtnl_unlock();
14372 ulp_start:
14373 	bnxt_ulp_start(bp, rc);
14374 }
14375 
14376 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
14377 {
14378 	int rc;
14379 	struct bnxt *bp = netdev_priv(dev);
14380 
14381 	SET_NETDEV_DEV(dev, &pdev->dev);
14382 
14383 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
14384 	rc = pci_enable_device(pdev);
14385 	if (rc) {
14386 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
14387 		goto init_err;
14388 	}
14389 
14390 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
14391 		dev_err(&pdev->dev,
14392 			"Cannot find PCI device base address, aborting\n");
14393 		rc = -ENODEV;
14394 		goto init_err_disable;
14395 	}
14396 
14397 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
14398 	if (rc) {
14399 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
14400 		goto init_err_disable;
14401 	}
14402 
14403 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
14404 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
14405 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
14406 		rc = -EIO;
14407 		goto init_err_release;
14408 	}
14409 
14410 	pci_set_master(pdev);
14411 
14412 	bp->dev = dev;
14413 	bp->pdev = pdev;
14414 
14415 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
14416 	 * determines the BAR size.
14417 	 */
14418 	bp->bar0 = pci_ioremap_bar(pdev, 0);
14419 	if (!bp->bar0) {
14420 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
14421 		rc = -ENOMEM;
14422 		goto init_err_release;
14423 	}
14424 
14425 	bp->bar2 = pci_ioremap_bar(pdev, 4);
14426 	if (!bp->bar2) {
14427 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
14428 		rc = -ENOMEM;
14429 		goto init_err_release;
14430 	}
14431 
14432 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
14433 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
14434 
14435 	spin_lock_init(&bp->ntp_fltr_lock);
14436 #if BITS_PER_LONG == 32
14437 	spin_lock_init(&bp->db_lock);
14438 #endif
14439 
14440 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
14441 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
14442 
14443 	timer_setup(&bp->timer, bnxt_timer, 0);
14444 	bp->current_interval = BNXT_TIMER_INTERVAL;
14445 
14446 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
14447 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
14448 
14449 	clear_bit(BNXT_STATE_OPEN, &bp->state);
14450 	return 0;
14451 
14452 init_err_release:
14453 	bnxt_unmap_bars(bp, pdev);
14454 	pci_release_regions(pdev);
14455 
14456 init_err_disable:
14457 	pci_disable_device(pdev);
14458 
14459 init_err:
14460 	return rc;
14461 }
14462 
14463 /* rtnl_lock held */
14464 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
14465 {
14466 	struct sockaddr *addr = p;
14467 	struct bnxt *bp = netdev_priv(dev);
14468 	int rc = 0;
14469 
14470 	if (!is_valid_ether_addr(addr->sa_data))
14471 		return -EADDRNOTAVAIL;
14472 
14473 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
14474 		return 0;
14475 
14476 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
14477 	if (rc)
14478 		return rc;
14479 
14480 	eth_hw_addr_set(dev, addr->sa_data);
14481 	bnxt_clear_usr_fltrs(bp, true);
14482 	if (netif_running(dev)) {
14483 		bnxt_close_nic(bp, false, false);
14484 		rc = bnxt_open_nic(bp, false, false);
14485 	}
14486 
14487 	return rc;
14488 }
14489 
14490 /* rtnl_lock held */
14491 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
14492 {
14493 	struct bnxt *bp = netdev_priv(dev);
14494 
14495 	if (netif_running(dev))
14496 		bnxt_close_nic(bp, true, false);
14497 
14498 	WRITE_ONCE(dev->mtu, new_mtu);
14499 	bnxt_set_ring_params(bp);
14500 
14501 	if (netif_running(dev))
14502 		return bnxt_open_nic(bp, true, false);
14503 
14504 	return 0;
14505 }
14506 
14507 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
14508 {
14509 	struct bnxt *bp = netdev_priv(dev);
14510 	bool sh = false;
14511 	int rc, tx_cp;
14512 
14513 	if (tc > bp->max_tc) {
14514 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
14515 			   tc, bp->max_tc);
14516 		return -EINVAL;
14517 	}
14518 
14519 	if (bp->num_tc == tc)
14520 		return 0;
14521 
14522 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
14523 		sh = true;
14524 
14525 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
14526 			      sh, tc, bp->tx_nr_rings_xdp);
14527 	if (rc)
14528 		return rc;
14529 
14530 	/* Needs to close the device and do hw resource re-allocations */
14531 	if (netif_running(bp->dev))
14532 		bnxt_close_nic(bp, true, false);
14533 
14534 	if (tc) {
14535 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
14536 		netdev_set_num_tc(dev, tc);
14537 		bp->num_tc = tc;
14538 	} else {
14539 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
14540 		netdev_reset_tc(dev);
14541 		bp->num_tc = 0;
14542 	}
14543 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
14544 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
14545 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
14546 			       tx_cp + bp->rx_nr_rings;
14547 
14548 	if (netif_running(bp->dev))
14549 		return bnxt_open_nic(bp, true, false);
14550 
14551 	return 0;
14552 }
14553 
14554 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
14555 				  void *cb_priv)
14556 {
14557 	struct bnxt *bp = cb_priv;
14558 
14559 	if (!bnxt_tc_flower_enabled(bp) ||
14560 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
14561 		return -EOPNOTSUPP;
14562 
14563 	switch (type) {
14564 	case TC_SETUP_CLSFLOWER:
14565 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
14566 	default:
14567 		return -EOPNOTSUPP;
14568 	}
14569 }
14570 
14571 LIST_HEAD(bnxt_block_cb_list);
14572 
14573 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
14574 			 void *type_data)
14575 {
14576 	struct bnxt *bp = netdev_priv(dev);
14577 
14578 	switch (type) {
14579 	case TC_SETUP_BLOCK:
14580 		return flow_block_cb_setup_simple(type_data,
14581 						  &bnxt_block_cb_list,
14582 						  bnxt_setup_tc_block_cb,
14583 						  bp, bp, true);
14584 	case TC_SETUP_QDISC_MQPRIO: {
14585 		struct tc_mqprio_qopt *mqprio = type_data;
14586 
14587 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
14588 
14589 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
14590 	}
14591 	default:
14592 		return -EOPNOTSUPP;
14593 	}
14594 }
14595 
14596 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
14597 			    const struct sk_buff *skb)
14598 {
14599 	struct bnxt_vnic_info *vnic;
14600 
14601 	if (skb)
14602 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
14603 
14604 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
14605 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
14606 }
14607 
14608 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
14609 			   u32 idx)
14610 {
14611 	struct hlist_head *head;
14612 	int bit_id;
14613 
14614 	spin_lock_bh(&bp->ntp_fltr_lock);
14615 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
14616 	if (bit_id < 0) {
14617 		spin_unlock_bh(&bp->ntp_fltr_lock);
14618 		return -ENOMEM;
14619 	}
14620 
14621 	fltr->base.sw_id = (u16)bit_id;
14622 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
14623 	fltr->base.flags |= BNXT_ACT_RING_DST;
14624 	head = &bp->ntp_fltr_hash_tbl[idx];
14625 	hlist_add_head_rcu(&fltr->base.hash, head);
14626 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
14627 	bnxt_insert_usr_fltr(bp, &fltr->base);
14628 	bp->ntp_fltr_count++;
14629 	spin_unlock_bh(&bp->ntp_fltr_lock);
14630 	return 0;
14631 }
14632 
14633 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
14634 			    struct bnxt_ntuple_filter *f2)
14635 {
14636 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
14637 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
14638 	struct flow_keys *keys1 = &f1->fkeys;
14639 	struct flow_keys *keys2 = &f2->fkeys;
14640 
14641 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
14642 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
14643 		return false;
14644 
14645 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
14646 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
14647 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
14648 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
14649 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
14650 			return false;
14651 	} else {
14652 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
14653 				     &keys2->addrs.v6addrs.src) ||
14654 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
14655 				     &masks2->addrs.v6addrs.src) ||
14656 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
14657 				     &keys2->addrs.v6addrs.dst) ||
14658 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
14659 				     &masks2->addrs.v6addrs.dst))
14660 			return false;
14661 	}
14662 
14663 	return keys1->ports.src == keys2->ports.src &&
14664 	       masks1->ports.src == masks2->ports.src &&
14665 	       keys1->ports.dst == keys2->ports.dst &&
14666 	       masks1->ports.dst == masks2->ports.dst &&
14667 	       keys1->control.flags == keys2->control.flags &&
14668 	       f1->l2_fltr == f2->l2_fltr;
14669 }
14670 
14671 struct bnxt_ntuple_filter *
14672 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
14673 				struct bnxt_ntuple_filter *fltr, u32 idx)
14674 {
14675 	struct bnxt_ntuple_filter *f;
14676 	struct hlist_head *head;
14677 
14678 	head = &bp->ntp_fltr_hash_tbl[idx];
14679 	hlist_for_each_entry_rcu(f, head, base.hash) {
14680 		if (bnxt_fltr_match(f, fltr))
14681 			return f;
14682 	}
14683 	return NULL;
14684 }
14685 
14686 #ifdef CONFIG_RFS_ACCEL
14687 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
14688 			      u16 rxq_index, u32 flow_id)
14689 {
14690 	struct bnxt *bp = netdev_priv(dev);
14691 	struct bnxt_ntuple_filter *fltr, *new_fltr;
14692 	struct flow_keys *fkeys;
14693 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
14694 	struct bnxt_l2_filter *l2_fltr;
14695 	int rc = 0, idx;
14696 	u32 flags;
14697 
14698 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
14699 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
14700 		atomic_inc(&l2_fltr->refcnt);
14701 	} else {
14702 		struct bnxt_l2_key key;
14703 
14704 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
14705 		key.vlan = 0;
14706 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
14707 		if (!l2_fltr)
14708 			return -EINVAL;
14709 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
14710 			bnxt_del_l2_filter(bp, l2_fltr);
14711 			return -EINVAL;
14712 		}
14713 	}
14714 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
14715 	if (!new_fltr) {
14716 		bnxt_del_l2_filter(bp, l2_fltr);
14717 		return -ENOMEM;
14718 	}
14719 
14720 	fkeys = &new_fltr->fkeys;
14721 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
14722 		rc = -EPROTONOSUPPORT;
14723 		goto err_free;
14724 	}
14725 
14726 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
14727 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
14728 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
14729 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
14730 		rc = -EPROTONOSUPPORT;
14731 		goto err_free;
14732 	}
14733 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
14734 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
14735 		if (bp->hwrm_spec_code < 0x10601) {
14736 			rc = -EPROTONOSUPPORT;
14737 			goto err_free;
14738 		}
14739 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
14740 	}
14741 	flags = fkeys->control.flags;
14742 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
14743 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
14744 		rc = -EPROTONOSUPPORT;
14745 		goto err_free;
14746 	}
14747 	new_fltr->l2_fltr = l2_fltr;
14748 
14749 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
14750 	rcu_read_lock();
14751 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
14752 	if (fltr) {
14753 		rc = fltr->base.sw_id;
14754 		rcu_read_unlock();
14755 		goto err_free;
14756 	}
14757 	rcu_read_unlock();
14758 
14759 	new_fltr->flow_id = flow_id;
14760 	new_fltr->base.rxq = rxq_index;
14761 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
14762 	if (!rc) {
14763 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14764 		return new_fltr->base.sw_id;
14765 	}
14766 
14767 err_free:
14768 	bnxt_del_l2_filter(bp, l2_fltr);
14769 	kfree(new_fltr);
14770 	return rc;
14771 }
14772 #endif
14773 
14774 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
14775 {
14776 	spin_lock_bh(&bp->ntp_fltr_lock);
14777 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
14778 		spin_unlock_bh(&bp->ntp_fltr_lock);
14779 		return;
14780 	}
14781 	hlist_del_rcu(&fltr->base.hash);
14782 	bnxt_del_one_usr_fltr(bp, &fltr->base);
14783 	bp->ntp_fltr_count--;
14784 	spin_unlock_bh(&bp->ntp_fltr_lock);
14785 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
14786 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
14787 	kfree_rcu(fltr, base.rcu);
14788 }
14789 
14790 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
14791 {
14792 #ifdef CONFIG_RFS_ACCEL
14793 	int i;
14794 
14795 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
14796 		struct hlist_head *head;
14797 		struct hlist_node *tmp;
14798 		struct bnxt_ntuple_filter *fltr;
14799 		int rc;
14800 
14801 		head = &bp->ntp_fltr_hash_tbl[i];
14802 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
14803 			bool del = false;
14804 
14805 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
14806 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
14807 					continue;
14808 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
14809 							fltr->flow_id,
14810 							fltr->base.sw_id)) {
14811 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
14812 									 fltr);
14813 					del = true;
14814 				}
14815 			} else {
14816 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
14817 								       fltr);
14818 				if (rc)
14819 					del = true;
14820 				else
14821 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
14822 			}
14823 
14824 			if (del)
14825 				bnxt_del_ntp_filter(bp, fltr);
14826 		}
14827 	}
14828 #endif
14829 }
14830 
14831 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
14832 				    unsigned int entry, struct udp_tunnel_info *ti)
14833 {
14834 	struct bnxt *bp = netdev_priv(netdev);
14835 	unsigned int cmd;
14836 
14837 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
14838 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
14839 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
14840 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
14841 	else
14842 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
14843 
14844 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
14845 }
14846 
14847 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
14848 				      unsigned int entry, struct udp_tunnel_info *ti)
14849 {
14850 	struct bnxt *bp = netdev_priv(netdev);
14851 	unsigned int cmd;
14852 
14853 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
14854 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
14855 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
14856 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
14857 	else
14858 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
14859 
14860 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
14861 }
14862 
14863 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
14864 	.set_port	= bnxt_udp_tunnel_set_port,
14865 	.unset_port	= bnxt_udp_tunnel_unset_port,
14866 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
14867 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
14868 	.tables		= {
14869 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
14870 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
14871 	},
14872 }, bnxt_udp_tunnels_p7 = {
14873 	.set_port	= bnxt_udp_tunnel_set_port,
14874 	.unset_port	= bnxt_udp_tunnel_unset_port,
14875 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
14876 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
14877 	.tables		= {
14878 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
14879 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
14880 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
14881 	},
14882 };
14883 
14884 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
14885 			       struct net_device *dev, u32 filter_mask,
14886 			       int nlflags)
14887 {
14888 	struct bnxt *bp = netdev_priv(dev);
14889 
14890 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
14891 				       nlflags, filter_mask, NULL);
14892 }
14893 
14894 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
14895 			       u16 flags, struct netlink_ext_ack *extack)
14896 {
14897 	struct bnxt *bp = netdev_priv(dev);
14898 	struct nlattr *attr, *br_spec;
14899 	int rem, rc = 0;
14900 
14901 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
14902 		return -EOPNOTSUPP;
14903 
14904 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
14905 	if (!br_spec)
14906 		return -EINVAL;
14907 
14908 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
14909 		u16 mode;
14910 
14911 		mode = nla_get_u16(attr);
14912 		if (mode == bp->br_mode)
14913 			break;
14914 
14915 		rc = bnxt_hwrm_set_br_mode(bp, mode);
14916 		if (!rc)
14917 			bp->br_mode = mode;
14918 		break;
14919 	}
14920 	return rc;
14921 }
14922 
14923 int bnxt_get_port_parent_id(struct net_device *dev,
14924 			    struct netdev_phys_item_id *ppid)
14925 {
14926 	struct bnxt *bp = netdev_priv(dev);
14927 
14928 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
14929 		return -EOPNOTSUPP;
14930 
14931 	/* The PF and it's VF-reps only support the switchdev framework */
14932 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
14933 		return -EOPNOTSUPP;
14934 
14935 	ppid->id_len = sizeof(bp->dsn);
14936 	memcpy(ppid->id, bp->dsn, ppid->id_len);
14937 
14938 	return 0;
14939 }
14940 
14941 static const struct net_device_ops bnxt_netdev_ops = {
14942 	.ndo_open		= bnxt_open,
14943 	.ndo_start_xmit		= bnxt_start_xmit,
14944 	.ndo_stop		= bnxt_close,
14945 	.ndo_get_stats64	= bnxt_get_stats64,
14946 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
14947 	.ndo_eth_ioctl		= bnxt_ioctl,
14948 	.ndo_validate_addr	= eth_validate_addr,
14949 	.ndo_set_mac_address	= bnxt_change_mac_addr,
14950 	.ndo_change_mtu		= bnxt_change_mtu,
14951 	.ndo_fix_features	= bnxt_fix_features,
14952 	.ndo_set_features	= bnxt_set_features,
14953 	.ndo_features_check	= bnxt_features_check,
14954 	.ndo_tx_timeout		= bnxt_tx_timeout,
14955 #ifdef CONFIG_BNXT_SRIOV
14956 	.ndo_get_vf_config	= bnxt_get_vf_config,
14957 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
14958 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
14959 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
14960 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
14961 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
14962 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
14963 #endif
14964 	.ndo_setup_tc           = bnxt_setup_tc,
14965 #ifdef CONFIG_RFS_ACCEL
14966 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
14967 #endif
14968 	.ndo_bpf		= bnxt_xdp,
14969 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
14970 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
14971 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
14972 };
14973 
14974 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
14975 				    struct netdev_queue_stats_rx *stats)
14976 {
14977 	struct bnxt *bp = netdev_priv(dev);
14978 	struct bnxt_cp_ring_info *cpr;
14979 	u64 *sw;
14980 
14981 	cpr = &bp->bnapi[i]->cp_ring;
14982 	sw = cpr->stats.sw_stats;
14983 
14984 	stats->packets = 0;
14985 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
14986 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
14987 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
14988 
14989 	stats->bytes = 0;
14990 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
14991 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
14992 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
14993 
14994 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
14995 }
14996 
14997 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
14998 				    struct netdev_queue_stats_tx *stats)
14999 {
15000 	struct bnxt *bp = netdev_priv(dev);
15001 	struct bnxt_napi *bnapi;
15002 	u64 *sw;
15003 
15004 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
15005 	sw = bnapi->cp_ring.stats.sw_stats;
15006 
15007 	stats->packets = 0;
15008 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
15009 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
15010 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
15011 
15012 	stats->bytes = 0;
15013 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
15014 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
15015 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
15016 }
15017 
15018 static void bnxt_get_base_stats(struct net_device *dev,
15019 				struct netdev_queue_stats_rx *rx,
15020 				struct netdev_queue_stats_tx *tx)
15021 {
15022 	struct bnxt *bp = netdev_priv(dev);
15023 
15024 	rx->packets = bp->net_stats_prev.rx_packets;
15025 	rx->bytes = bp->net_stats_prev.rx_bytes;
15026 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
15027 
15028 	tx->packets = bp->net_stats_prev.tx_packets;
15029 	tx->bytes = bp->net_stats_prev.tx_bytes;
15030 }
15031 
15032 static const struct netdev_stat_ops bnxt_stat_ops = {
15033 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
15034 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
15035 	.get_base_stats		= bnxt_get_base_stats,
15036 };
15037 
15038 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
15039 {
15040 	u16 mem_size;
15041 
15042 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
15043 	mem_size = rxr->rx_agg_bmap_size / 8;
15044 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
15045 	if (!rxr->rx_agg_bmap)
15046 		return -ENOMEM;
15047 
15048 	return 0;
15049 }
15050 
15051 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx)
15052 {
15053 	struct bnxt_rx_ring_info *rxr, *clone;
15054 	struct bnxt *bp = netdev_priv(dev);
15055 	struct bnxt_ring_struct *ring;
15056 	int rc;
15057 
15058 	rxr = &bp->rx_ring[idx];
15059 	clone = qmem;
15060 	memcpy(clone, rxr, sizeof(*rxr));
15061 	bnxt_init_rx_ring_struct(bp, clone);
15062 	bnxt_reset_rx_ring_struct(bp, clone);
15063 
15064 	clone->rx_prod = 0;
15065 	clone->rx_agg_prod = 0;
15066 	clone->rx_sw_agg_prod = 0;
15067 	clone->rx_next_cons = 0;
15068 
15069 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
15070 	if (rc)
15071 		return rc;
15072 
15073 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
15074 	if (rc < 0)
15075 		goto err_page_pool_destroy;
15076 
15077 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
15078 					MEM_TYPE_PAGE_POOL,
15079 					clone->page_pool);
15080 	if (rc)
15081 		goto err_rxq_info_unreg;
15082 
15083 	ring = &clone->rx_ring_struct;
15084 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15085 	if (rc)
15086 		goto err_free_rx_ring;
15087 
15088 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
15089 		ring = &clone->rx_agg_ring_struct;
15090 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15091 		if (rc)
15092 			goto err_free_rx_agg_ring;
15093 
15094 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
15095 		if (rc)
15096 			goto err_free_rx_agg_ring;
15097 	}
15098 
15099 	bnxt_init_one_rx_ring_rxbd(bp, clone);
15100 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
15101 
15102 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
15103 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15104 		bnxt_alloc_one_rx_ring_page(bp, clone, idx);
15105 
15106 	return 0;
15107 
15108 err_free_rx_agg_ring:
15109 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
15110 err_free_rx_ring:
15111 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
15112 err_rxq_info_unreg:
15113 	xdp_rxq_info_unreg(&clone->xdp_rxq);
15114 err_page_pool_destroy:
15115 	clone->page_pool->p.napi = NULL;
15116 	page_pool_destroy(clone->page_pool);
15117 	clone->page_pool = NULL;
15118 	return rc;
15119 }
15120 
15121 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
15122 {
15123 	struct bnxt_rx_ring_info *rxr = qmem;
15124 	struct bnxt *bp = netdev_priv(dev);
15125 	struct bnxt_ring_struct *ring;
15126 
15127 	bnxt_free_one_rx_ring(bp, rxr);
15128 	bnxt_free_one_rx_agg_ring(bp, rxr);
15129 
15130 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
15131 
15132 	page_pool_destroy(rxr->page_pool);
15133 	rxr->page_pool = NULL;
15134 
15135 	ring = &rxr->rx_ring_struct;
15136 	bnxt_free_ring(bp, &ring->ring_mem);
15137 
15138 	ring = &rxr->rx_agg_ring_struct;
15139 	bnxt_free_ring(bp, &ring->ring_mem);
15140 
15141 	kfree(rxr->rx_agg_bmap);
15142 	rxr->rx_agg_bmap = NULL;
15143 }
15144 
15145 static void bnxt_copy_rx_ring(struct bnxt *bp,
15146 			      struct bnxt_rx_ring_info *dst,
15147 			      struct bnxt_rx_ring_info *src)
15148 {
15149 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
15150 	struct bnxt_ring_struct *dst_ring, *src_ring;
15151 	int i;
15152 
15153 	dst_ring = &dst->rx_ring_struct;
15154 	dst_rmem = &dst_ring->ring_mem;
15155 	src_ring = &src->rx_ring_struct;
15156 	src_rmem = &src_ring->ring_mem;
15157 
15158 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15159 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15160 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15161 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15162 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15163 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15164 
15165 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15166 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15167 	*dst_rmem->vmem = *src_rmem->vmem;
15168 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15169 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15170 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15171 	}
15172 
15173 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
15174 		return;
15175 
15176 	dst_ring = &dst->rx_agg_ring_struct;
15177 	dst_rmem = &dst_ring->ring_mem;
15178 	src_ring = &src->rx_agg_ring_struct;
15179 	src_rmem = &src_ring->ring_mem;
15180 
15181 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15182 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15183 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15184 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15185 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15186 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15187 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
15188 
15189 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15190 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15191 	*dst_rmem->vmem = *src_rmem->vmem;
15192 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15193 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15194 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15195 	}
15196 
15197 	dst->rx_agg_bmap = src->rx_agg_bmap;
15198 }
15199 
15200 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx)
15201 {
15202 	struct bnxt *bp = netdev_priv(dev);
15203 	struct bnxt_rx_ring_info *rxr, *clone;
15204 	struct bnxt_cp_ring_info *cpr;
15205 	struct bnxt_vnic_info *vnic;
15206 	int i, rc;
15207 
15208 	rxr = &bp->rx_ring[idx];
15209 	clone = qmem;
15210 
15211 	rxr->rx_prod = clone->rx_prod;
15212 	rxr->rx_agg_prod = clone->rx_agg_prod;
15213 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
15214 	rxr->rx_next_cons = clone->rx_next_cons;
15215 	rxr->page_pool = clone->page_pool;
15216 	rxr->xdp_rxq = clone->xdp_rxq;
15217 
15218 	bnxt_copy_rx_ring(bp, rxr, clone);
15219 
15220 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
15221 	if (rc)
15222 		return rc;
15223 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
15224 	if (rc)
15225 		goto err_free_hwrm_rx_ring;
15226 
15227 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
15228 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15229 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
15230 
15231 	cpr = &rxr->bnapi->cp_ring;
15232 	cpr->sw_stats->rx.rx_resets++;
15233 
15234 	for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) {
15235 		vnic = &bp->vnic_info[i];
15236 		vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
15237 		bnxt_hwrm_vnic_update(bp, vnic,
15238 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15239 	}
15240 
15241 	return 0;
15242 
15243 err_free_hwrm_rx_ring:
15244 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15245 	return rc;
15246 }
15247 
15248 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
15249 {
15250 	struct bnxt *bp = netdev_priv(dev);
15251 	struct bnxt_rx_ring_info *rxr;
15252 	struct bnxt_vnic_info *vnic;
15253 	int i;
15254 
15255 	for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) {
15256 		vnic = &bp->vnic_info[i];
15257 		vnic->mru = 0;
15258 		bnxt_hwrm_vnic_update(bp, vnic,
15259 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15260 	}
15261 
15262 	rxr = &bp->rx_ring[idx];
15263 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15264 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
15265 	rxr->rx_next_cons = 0;
15266 	page_pool_disable_direct_recycling(rxr->page_pool);
15267 
15268 	memcpy(qmem, rxr, sizeof(*rxr));
15269 	bnxt_init_rx_ring_struct(bp, qmem);
15270 
15271 	return 0;
15272 }
15273 
15274 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
15275 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
15276 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
15277 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
15278 	.ndo_queue_start	= bnxt_queue_start,
15279 	.ndo_queue_stop		= bnxt_queue_stop,
15280 };
15281 
15282 static void bnxt_remove_one(struct pci_dev *pdev)
15283 {
15284 	struct net_device *dev = pci_get_drvdata(pdev);
15285 	struct bnxt *bp = netdev_priv(dev);
15286 
15287 	if (BNXT_PF(bp))
15288 		bnxt_sriov_disable(bp);
15289 
15290 	bnxt_rdma_aux_device_del(bp);
15291 
15292 	bnxt_ptp_clear(bp);
15293 	unregister_netdev(dev);
15294 
15295 	bnxt_rdma_aux_device_uninit(bp);
15296 
15297 	bnxt_free_l2_filters(bp, true);
15298 	bnxt_free_ntp_fltrs(bp, true);
15299 	WARN_ON(bp->num_rss_ctx);
15300 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15301 	/* Flush any pending tasks */
15302 	cancel_work_sync(&bp->sp_task);
15303 	cancel_delayed_work_sync(&bp->fw_reset_task);
15304 	bp->sp_event = 0;
15305 
15306 	bnxt_dl_fw_reporters_destroy(bp);
15307 	bnxt_dl_unregister(bp);
15308 	bnxt_shutdown_tc(bp);
15309 
15310 	bnxt_clear_int_mode(bp);
15311 	bnxt_hwrm_func_drv_unrgtr(bp);
15312 	bnxt_free_hwrm_resources(bp);
15313 	bnxt_hwmon_uninit(bp);
15314 	bnxt_ethtool_free(bp);
15315 	bnxt_dcb_free(bp);
15316 	kfree(bp->ptp_cfg);
15317 	bp->ptp_cfg = NULL;
15318 	kfree(bp->fw_health);
15319 	bp->fw_health = NULL;
15320 	bnxt_cleanup_pci(bp);
15321 	bnxt_free_ctx_mem(bp);
15322 	bnxt_free_crash_dump_mem(bp);
15323 	kfree(bp->rss_indir_tbl);
15324 	bp->rss_indir_tbl = NULL;
15325 	bnxt_free_port_stats(bp);
15326 	free_netdev(dev);
15327 }
15328 
15329 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
15330 {
15331 	int rc = 0;
15332 	struct bnxt_link_info *link_info = &bp->link_info;
15333 
15334 	bp->phy_flags = 0;
15335 	rc = bnxt_hwrm_phy_qcaps(bp);
15336 	if (rc) {
15337 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
15338 			   rc);
15339 		return rc;
15340 	}
15341 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
15342 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
15343 	else
15344 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
15345 	if (!fw_dflt)
15346 		return 0;
15347 
15348 	mutex_lock(&bp->link_lock);
15349 	rc = bnxt_update_link(bp, false);
15350 	if (rc) {
15351 		mutex_unlock(&bp->link_lock);
15352 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
15353 			   rc);
15354 		return rc;
15355 	}
15356 
15357 	/* Older firmware does not have supported_auto_speeds, so assume
15358 	 * that all supported speeds can be autonegotiated.
15359 	 */
15360 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
15361 		link_info->support_auto_speeds = link_info->support_speeds;
15362 
15363 	bnxt_init_ethtool_link_settings(bp);
15364 	mutex_unlock(&bp->link_lock);
15365 	return 0;
15366 }
15367 
15368 static int bnxt_get_max_irq(struct pci_dev *pdev)
15369 {
15370 	u16 ctrl;
15371 
15372 	if (!pdev->msix_cap)
15373 		return 1;
15374 
15375 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
15376 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
15377 }
15378 
15379 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15380 				int *max_cp)
15381 {
15382 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
15383 	int max_ring_grps = 0, max_irq;
15384 
15385 	*max_tx = hw_resc->max_tx_rings;
15386 	*max_rx = hw_resc->max_rx_rings;
15387 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
15388 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
15389 			bnxt_get_ulp_msix_num_in_use(bp),
15390 			hw_resc->max_stat_ctxs -
15391 			bnxt_get_ulp_stat_ctxs_in_use(bp));
15392 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
15393 		*max_cp = min_t(int, *max_cp, max_irq);
15394 	max_ring_grps = hw_resc->max_hw_ring_grps;
15395 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
15396 		*max_cp -= 1;
15397 		*max_rx -= 2;
15398 	}
15399 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15400 		*max_rx >>= 1;
15401 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
15402 		int rc;
15403 
15404 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
15405 		if (rc) {
15406 			*max_rx = 0;
15407 			*max_tx = 0;
15408 		}
15409 		/* On P5 chips, max_cp output param should be available NQs */
15410 		*max_cp = max_irq;
15411 	}
15412 	*max_rx = min_t(int, *max_rx, max_ring_grps);
15413 }
15414 
15415 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
15416 {
15417 	int rx, tx, cp;
15418 
15419 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
15420 	*max_rx = rx;
15421 	*max_tx = tx;
15422 	if (!rx || !tx || !cp)
15423 		return -ENOMEM;
15424 
15425 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
15426 }
15427 
15428 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15429 			       bool shared)
15430 {
15431 	int rc;
15432 
15433 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
15434 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
15435 		/* Not enough rings, try disabling agg rings. */
15436 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
15437 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
15438 		if (rc) {
15439 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
15440 			bp->flags |= BNXT_FLAG_AGG_RINGS;
15441 			return rc;
15442 		}
15443 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
15444 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
15445 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
15446 		bnxt_set_ring_params(bp);
15447 	}
15448 
15449 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
15450 		int max_cp, max_stat, max_irq;
15451 
15452 		/* Reserve minimum resources for RoCE */
15453 		max_cp = bnxt_get_max_func_cp_rings(bp);
15454 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
15455 		max_irq = bnxt_get_max_func_irqs(bp);
15456 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
15457 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
15458 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
15459 			return 0;
15460 
15461 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
15462 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
15463 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
15464 		max_cp = min_t(int, max_cp, max_irq);
15465 		max_cp = min_t(int, max_cp, max_stat);
15466 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
15467 		if (rc)
15468 			rc = 0;
15469 	}
15470 	return rc;
15471 }
15472 
15473 /* In initial default shared ring setting, each shared ring must have a
15474  * RX/TX ring pair.
15475  */
15476 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
15477 {
15478 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
15479 	bp->rx_nr_rings = bp->cp_nr_rings;
15480 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
15481 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15482 }
15483 
15484 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
15485 {
15486 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
15487 	int avail_msix;
15488 
15489 	if (!bnxt_can_reserve_rings(bp))
15490 		return 0;
15491 
15492 	if (sh)
15493 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
15494 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
15495 	/* Reduce default rings on multi-port cards so that total default
15496 	 * rings do not exceed CPU count.
15497 	 */
15498 	if (bp->port_count > 1) {
15499 		int max_rings =
15500 			max_t(int, num_online_cpus() / bp->port_count, 1);
15501 
15502 		dflt_rings = min_t(int, dflt_rings, max_rings);
15503 	}
15504 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
15505 	if (rc)
15506 		return rc;
15507 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
15508 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
15509 	if (sh)
15510 		bnxt_trim_dflt_sh_rings(bp);
15511 	else
15512 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
15513 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15514 
15515 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
15516 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
15517 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
15518 
15519 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
15520 		bnxt_set_dflt_ulp_stat_ctxs(bp);
15521 	}
15522 
15523 	rc = __bnxt_reserve_rings(bp);
15524 	if (rc && rc != -ENODEV)
15525 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
15526 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15527 	if (sh)
15528 		bnxt_trim_dflt_sh_rings(bp);
15529 
15530 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
15531 	if (bnxt_need_reserve_rings(bp)) {
15532 		rc = __bnxt_reserve_rings(bp);
15533 		if (rc && rc != -ENODEV)
15534 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
15535 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15536 	}
15537 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
15538 		bp->rx_nr_rings++;
15539 		bp->cp_nr_rings++;
15540 	}
15541 	if (rc) {
15542 		bp->tx_nr_rings = 0;
15543 		bp->rx_nr_rings = 0;
15544 	}
15545 	return rc;
15546 }
15547 
15548 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
15549 {
15550 	int rc;
15551 
15552 	if (bp->tx_nr_rings)
15553 		return 0;
15554 
15555 	bnxt_ulp_irq_stop(bp);
15556 	bnxt_clear_int_mode(bp);
15557 	rc = bnxt_set_dflt_rings(bp, true);
15558 	if (rc) {
15559 		if (BNXT_VF(bp) && rc == -ENODEV)
15560 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
15561 		else
15562 			netdev_err(bp->dev, "Not enough rings available.\n");
15563 		goto init_dflt_ring_err;
15564 	}
15565 	rc = bnxt_init_int_mode(bp);
15566 	if (rc)
15567 		goto init_dflt_ring_err;
15568 
15569 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15570 
15571 	bnxt_set_dflt_rfs(bp);
15572 
15573 init_dflt_ring_err:
15574 	bnxt_ulp_irq_restart(bp, rc);
15575 	return rc;
15576 }
15577 
15578 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
15579 {
15580 	int rc;
15581 
15582 	ASSERT_RTNL();
15583 	bnxt_hwrm_func_qcaps(bp);
15584 
15585 	if (netif_running(bp->dev))
15586 		__bnxt_close_nic(bp, true, false);
15587 
15588 	bnxt_ulp_irq_stop(bp);
15589 	bnxt_clear_int_mode(bp);
15590 	rc = bnxt_init_int_mode(bp);
15591 	bnxt_ulp_irq_restart(bp, rc);
15592 
15593 	if (netif_running(bp->dev)) {
15594 		if (rc)
15595 			dev_close(bp->dev);
15596 		else
15597 			rc = bnxt_open_nic(bp, true, false);
15598 	}
15599 
15600 	return rc;
15601 }
15602 
15603 static int bnxt_init_mac_addr(struct bnxt *bp)
15604 {
15605 	int rc = 0;
15606 
15607 	if (BNXT_PF(bp)) {
15608 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
15609 	} else {
15610 #ifdef CONFIG_BNXT_SRIOV
15611 		struct bnxt_vf_info *vf = &bp->vf;
15612 		bool strict_approval = true;
15613 
15614 		if (is_valid_ether_addr(vf->mac_addr)) {
15615 			/* overwrite netdev dev_addr with admin VF MAC */
15616 			eth_hw_addr_set(bp->dev, vf->mac_addr);
15617 			/* Older PF driver or firmware may not approve this
15618 			 * correctly.
15619 			 */
15620 			strict_approval = false;
15621 		} else {
15622 			eth_hw_addr_random(bp->dev);
15623 		}
15624 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
15625 #endif
15626 	}
15627 	return rc;
15628 }
15629 
15630 static void bnxt_vpd_read_info(struct bnxt *bp)
15631 {
15632 	struct pci_dev *pdev = bp->pdev;
15633 	unsigned int vpd_size, kw_len;
15634 	int pos, size;
15635 	u8 *vpd_data;
15636 
15637 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
15638 	if (IS_ERR(vpd_data)) {
15639 		pci_warn(pdev, "Unable to read VPD\n");
15640 		return;
15641 	}
15642 
15643 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
15644 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
15645 	if (pos < 0)
15646 		goto read_sn;
15647 
15648 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
15649 	memcpy(bp->board_partno, &vpd_data[pos], size);
15650 
15651 read_sn:
15652 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
15653 					   PCI_VPD_RO_KEYWORD_SERIALNO,
15654 					   &kw_len);
15655 	if (pos < 0)
15656 		goto exit;
15657 
15658 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
15659 	memcpy(bp->board_serialno, &vpd_data[pos], size);
15660 exit:
15661 	kfree(vpd_data);
15662 }
15663 
15664 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
15665 {
15666 	struct pci_dev *pdev = bp->pdev;
15667 	u64 qword;
15668 
15669 	qword = pci_get_dsn(pdev);
15670 	if (!qword) {
15671 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
15672 		return -EOPNOTSUPP;
15673 	}
15674 
15675 	put_unaligned_le64(qword, dsn);
15676 
15677 	bp->flags |= BNXT_FLAG_DSN_VALID;
15678 	return 0;
15679 }
15680 
15681 static int bnxt_map_db_bar(struct bnxt *bp)
15682 {
15683 	if (!bp->db_size)
15684 		return -ENODEV;
15685 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
15686 	if (!bp->bar1)
15687 		return -ENOMEM;
15688 	return 0;
15689 }
15690 
15691 void bnxt_print_device_info(struct bnxt *bp)
15692 {
15693 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
15694 		    board_info[bp->board_idx].name,
15695 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
15696 
15697 	pcie_print_link_status(bp->pdev);
15698 }
15699 
15700 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
15701 {
15702 	struct bnxt_hw_resc *hw_resc;
15703 	struct net_device *dev;
15704 	struct bnxt *bp;
15705 	int rc, max_irqs;
15706 
15707 	if (pci_is_bridge(pdev))
15708 		return -ENODEV;
15709 
15710 	if (!pdev->msix_cap) {
15711 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
15712 		return -ENODEV;
15713 	}
15714 
15715 	/* Clear any pending DMA transactions from crash kernel
15716 	 * while loading driver in capture kernel.
15717 	 */
15718 	if (is_kdump_kernel()) {
15719 		pci_clear_master(pdev);
15720 		pcie_flr(pdev);
15721 	}
15722 
15723 	max_irqs = bnxt_get_max_irq(pdev);
15724 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
15725 				 max_irqs);
15726 	if (!dev)
15727 		return -ENOMEM;
15728 
15729 	bp = netdev_priv(dev);
15730 	bp->board_idx = ent->driver_data;
15731 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
15732 	bnxt_set_max_func_irqs(bp, max_irqs);
15733 
15734 	if (bnxt_vf_pciid(bp->board_idx))
15735 		bp->flags |= BNXT_FLAG_VF;
15736 
15737 	/* No devlink port registration in case of a VF */
15738 	if (BNXT_PF(bp))
15739 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
15740 
15741 	rc = bnxt_init_board(pdev, dev);
15742 	if (rc < 0)
15743 		goto init_err_free;
15744 
15745 	dev->netdev_ops = &bnxt_netdev_ops;
15746 	dev->stat_ops = &bnxt_stat_ops;
15747 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
15748 	dev->ethtool_ops = &bnxt_ethtool_ops;
15749 	pci_set_drvdata(pdev, dev);
15750 
15751 	rc = bnxt_alloc_hwrm_resources(bp);
15752 	if (rc)
15753 		goto init_err_pci_clean;
15754 
15755 	mutex_init(&bp->hwrm_cmd_lock);
15756 	mutex_init(&bp->link_lock);
15757 
15758 	rc = bnxt_fw_init_one_p1(bp);
15759 	if (rc)
15760 		goto init_err_pci_clean;
15761 
15762 	if (BNXT_PF(bp))
15763 		bnxt_vpd_read_info(bp);
15764 
15765 	if (BNXT_CHIP_P5_PLUS(bp)) {
15766 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
15767 		if (BNXT_CHIP_P7(bp))
15768 			bp->flags |= BNXT_FLAG_CHIP_P7;
15769 	}
15770 
15771 	rc = bnxt_alloc_rss_indir_tbl(bp);
15772 	if (rc)
15773 		goto init_err_pci_clean;
15774 
15775 	rc = bnxt_fw_init_one_p2(bp);
15776 	if (rc)
15777 		goto init_err_pci_clean;
15778 
15779 	rc = bnxt_map_db_bar(bp);
15780 	if (rc) {
15781 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
15782 			rc);
15783 		goto init_err_pci_clean;
15784 	}
15785 
15786 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
15787 			   NETIF_F_TSO | NETIF_F_TSO6 |
15788 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
15789 			   NETIF_F_GSO_IPXIP4 |
15790 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
15791 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
15792 			   NETIF_F_RXCSUM | NETIF_F_GRO;
15793 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
15794 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
15795 
15796 	if (BNXT_SUPPORTS_TPA(bp))
15797 		dev->hw_features |= NETIF_F_LRO;
15798 
15799 	dev->hw_enc_features =
15800 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
15801 			NETIF_F_TSO | NETIF_F_TSO6 |
15802 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
15803 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
15804 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
15805 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
15806 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
15807 	if (bp->flags & BNXT_FLAG_CHIP_P7)
15808 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
15809 	else
15810 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
15811 
15812 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
15813 				    NETIF_F_GSO_GRE_CSUM;
15814 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
15815 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
15816 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
15817 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
15818 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
15819 	if (BNXT_SUPPORTS_TPA(bp))
15820 		dev->hw_features |= NETIF_F_GRO_HW;
15821 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
15822 	if (dev->features & NETIF_F_GRO_HW)
15823 		dev->features &= ~NETIF_F_LRO;
15824 	dev->priv_flags |= IFF_UNICAST_FLT;
15825 
15826 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
15827 	if (bp->tso_max_segs)
15828 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
15829 
15830 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
15831 			    NETDEV_XDP_ACT_RX_SG;
15832 
15833 #ifdef CONFIG_BNXT_SRIOV
15834 	init_waitqueue_head(&bp->sriov_cfg_wait);
15835 #endif
15836 	if (BNXT_SUPPORTS_TPA(bp)) {
15837 		bp->gro_func = bnxt_gro_func_5730x;
15838 		if (BNXT_CHIP_P4(bp))
15839 			bp->gro_func = bnxt_gro_func_5731x;
15840 		else if (BNXT_CHIP_P5_PLUS(bp))
15841 			bp->gro_func = bnxt_gro_func_5750x;
15842 	}
15843 	if (!BNXT_CHIP_P4_PLUS(bp))
15844 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
15845 
15846 	rc = bnxt_init_mac_addr(bp);
15847 	if (rc) {
15848 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
15849 		rc = -EADDRNOTAVAIL;
15850 		goto init_err_pci_clean;
15851 	}
15852 
15853 	if (BNXT_PF(bp)) {
15854 		/* Read the adapter's DSN to use as the eswitch switch_id */
15855 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
15856 	}
15857 
15858 	/* MTU range: 60 - FW defined max */
15859 	dev->min_mtu = ETH_ZLEN;
15860 	dev->max_mtu = bp->max_mtu;
15861 
15862 	rc = bnxt_probe_phy(bp, true);
15863 	if (rc)
15864 		goto init_err_pci_clean;
15865 
15866 	hw_resc = &bp->hw_resc;
15867 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
15868 		       BNXT_L2_FLTR_MAX_FLTR;
15869 	/* Older firmware may not report these filters properly */
15870 	if (bp->max_fltr < BNXT_MAX_FLTR)
15871 		bp->max_fltr = BNXT_MAX_FLTR;
15872 	bnxt_init_l2_fltr_tbl(bp);
15873 	bnxt_set_rx_skb_mode(bp, false);
15874 	bnxt_set_tpa_flags(bp);
15875 	bnxt_set_ring_params(bp);
15876 	bnxt_rdma_aux_device_init(bp);
15877 	rc = bnxt_set_dflt_rings(bp, true);
15878 	if (rc) {
15879 		if (BNXT_VF(bp) && rc == -ENODEV) {
15880 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
15881 		} else {
15882 			netdev_err(bp->dev, "Not enough rings available.\n");
15883 			rc = -ENOMEM;
15884 		}
15885 		goto init_err_pci_clean;
15886 	}
15887 
15888 	bnxt_fw_init_one_p3(bp);
15889 
15890 	bnxt_init_dflt_coal(bp);
15891 
15892 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
15893 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
15894 
15895 	rc = bnxt_init_int_mode(bp);
15896 	if (rc)
15897 		goto init_err_pci_clean;
15898 
15899 	/* No TC has been set yet and rings may have been trimmed due to
15900 	 * limited MSIX, so we re-initialize the TX rings per TC.
15901 	 */
15902 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15903 
15904 	if (BNXT_PF(bp)) {
15905 		if (!bnxt_pf_wq) {
15906 			bnxt_pf_wq =
15907 				create_singlethread_workqueue("bnxt_pf_wq");
15908 			if (!bnxt_pf_wq) {
15909 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
15910 				rc = -ENOMEM;
15911 				goto init_err_pci_clean;
15912 			}
15913 		}
15914 		rc = bnxt_init_tc(bp);
15915 		if (rc)
15916 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
15917 				   rc);
15918 	}
15919 
15920 	bnxt_inv_fw_health_reg(bp);
15921 	rc = bnxt_dl_register(bp);
15922 	if (rc)
15923 		goto init_err_dl;
15924 
15925 	INIT_LIST_HEAD(&bp->usr_fltr_list);
15926 
15927 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
15928 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
15929 	if (BNXT_SUPPORTS_QUEUE_API(bp))
15930 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
15931 
15932 	rc = register_netdev(dev);
15933 	if (rc)
15934 		goto init_err_cleanup;
15935 
15936 	bnxt_dl_fw_reporters_create(bp);
15937 
15938 	bnxt_rdma_aux_device_add(bp);
15939 
15940 	bnxt_print_device_info(bp);
15941 
15942 	pci_save_state(pdev);
15943 
15944 	return 0;
15945 init_err_cleanup:
15946 	bnxt_rdma_aux_device_uninit(bp);
15947 	bnxt_dl_unregister(bp);
15948 init_err_dl:
15949 	bnxt_shutdown_tc(bp);
15950 	bnxt_clear_int_mode(bp);
15951 
15952 init_err_pci_clean:
15953 	bnxt_hwrm_func_drv_unrgtr(bp);
15954 	bnxt_free_hwrm_resources(bp);
15955 	bnxt_hwmon_uninit(bp);
15956 	bnxt_ethtool_free(bp);
15957 	bnxt_ptp_clear(bp);
15958 	kfree(bp->ptp_cfg);
15959 	bp->ptp_cfg = NULL;
15960 	kfree(bp->fw_health);
15961 	bp->fw_health = NULL;
15962 	bnxt_cleanup_pci(bp);
15963 	bnxt_free_ctx_mem(bp);
15964 	bnxt_free_crash_dump_mem(bp);
15965 	kfree(bp->rss_indir_tbl);
15966 	bp->rss_indir_tbl = NULL;
15967 
15968 init_err_free:
15969 	free_netdev(dev);
15970 	return rc;
15971 }
15972 
15973 static void bnxt_shutdown(struct pci_dev *pdev)
15974 {
15975 	struct net_device *dev = pci_get_drvdata(pdev);
15976 	struct bnxt *bp;
15977 
15978 	if (!dev)
15979 		return;
15980 
15981 	rtnl_lock();
15982 	bp = netdev_priv(dev);
15983 	if (!bp)
15984 		goto shutdown_exit;
15985 
15986 	if (netif_running(dev))
15987 		dev_close(dev);
15988 
15989 	bnxt_clear_int_mode(bp);
15990 	pci_disable_device(pdev);
15991 
15992 	if (system_state == SYSTEM_POWER_OFF) {
15993 		pci_wake_from_d3(pdev, bp->wol);
15994 		pci_set_power_state(pdev, PCI_D3hot);
15995 	}
15996 
15997 shutdown_exit:
15998 	rtnl_unlock();
15999 }
16000 
16001 #ifdef CONFIG_PM_SLEEP
16002 static int bnxt_suspend(struct device *device)
16003 {
16004 	struct net_device *dev = dev_get_drvdata(device);
16005 	struct bnxt *bp = netdev_priv(dev);
16006 	int rc = 0;
16007 
16008 	bnxt_ulp_stop(bp);
16009 
16010 	rtnl_lock();
16011 	if (netif_running(dev)) {
16012 		netif_device_detach(dev);
16013 		rc = bnxt_close(dev);
16014 	}
16015 	bnxt_hwrm_func_drv_unrgtr(bp);
16016 	pci_disable_device(bp->pdev);
16017 	bnxt_free_ctx_mem(bp);
16018 	rtnl_unlock();
16019 	return rc;
16020 }
16021 
16022 static int bnxt_resume(struct device *device)
16023 {
16024 	struct net_device *dev = dev_get_drvdata(device);
16025 	struct bnxt *bp = netdev_priv(dev);
16026 	int rc = 0;
16027 
16028 	rtnl_lock();
16029 	rc = pci_enable_device(bp->pdev);
16030 	if (rc) {
16031 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
16032 			   rc);
16033 		goto resume_exit;
16034 	}
16035 	pci_set_master(bp->pdev);
16036 	if (bnxt_hwrm_ver_get(bp)) {
16037 		rc = -ENODEV;
16038 		goto resume_exit;
16039 	}
16040 	rc = bnxt_hwrm_func_reset(bp);
16041 	if (rc) {
16042 		rc = -EBUSY;
16043 		goto resume_exit;
16044 	}
16045 
16046 	rc = bnxt_hwrm_func_qcaps(bp);
16047 	if (rc)
16048 		goto resume_exit;
16049 
16050 	bnxt_clear_reservations(bp, true);
16051 
16052 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
16053 		rc = -ENODEV;
16054 		goto resume_exit;
16055 	}
16056 	if (bp->fw_crash_mem)
16057 		bnxt_hwrm_crash_dump_mem_cfg(bp);
16058 
16059 	bnxt_get_wol_settings(bp);
16060 	if (netif_running(dev)) {
16061 		rc = bnxt_open(dev);
16062 		if (!rc)
16063 			netif_device_attach(dev);
16064 	}
16065 
16066 resume_exit:
16067 	rtnl_unlock();
16068 	bnxt_ulp_start(bp, rc);
16069 	if (!rc)
16070 		bnxt_reenable_sriov(bp);
16071 	return rc;
16072 }
16073 
16074 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
16075 #define BNXT_PM_OPS (&bnxt_pm_ops)
16076 
16077 #else
16078 
16079 #define BNXT_PM_OPS NULL
16080 
16081 #endif /* CONFIG_PM_SLEEP */
16082 
16083 /**
16084  * bnxt_io_error_detected - called when PCI error is detected
16085  * @pdev: Pointer to PCI device
16086  * @state: The current pci connection state
16087  *
16088  * This function is called after a PCI bus error affecting
16089  * this device has been detected.
16090  */
16091 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
16092 					       pci_channel_state_t state)
16093 {
16094 	struct net_device *netdev = pci_get_drvdata(pdev);
16095 	struct bnxt *bp = netdev_priv(netdev);
16096 	bool abort = false;
16097 
16098 	netdev_info(netdev, "PCI I/O error detected\n");
16099 
16100 	bnxt_ulp_stop(bp);
16101 
16102 	rtnl_lock();
16103 	netif_device_detach(netdev);
16104 
16105 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
16106 		netdev_err(bp->dev, "Firmware reset already in progress\n");
16107 		abort = true;
16108 	}
16109 
16110 	if (abort || state == pci_channel_io_perm_failure) {
16111 		rtnl_unlock();
16112 		return PCI_ERS_RESULT_DISCONNECT;
16113 	}
16114 
16115 	/* Link is not reliable anymore if state is pci_channel_io_frozen
16116 	 * so we disable bus master to prevent any potential bad DMAs before
16117 	 * freeing kernel memory.
16118 	 */
16119 	if (state == pci_channel_io_frozen) {
16120 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
16121 		bnxt_fw_fatal_close(bp);
16122 	}
16123 
16124 	if (netif_running(netdev))
16125 		__bnxt_close_nic(bp, true, true);
16126 
16127 	if (pci_is_enabled(pdev))
16128 		pci_disable_device(pdev);
16129 	bnxt_free_ctx_mem(bp);
16130 	rtnl_unlock();
16131 
16132 	/* Request a slot slot reset. */
16133 	return PCI_ERS_RESULT_NEED_RESET;
16134 }
16135 
16136 /**
16137  * bnxt_io_slot_reset - called after the pci bus has been reset.
16138  * @pdev: Pointer to PCI device
16139  *
16140  * Restart the card from scratch, as if from a cold-boot.
16141  * At this point, the card has experienced a hard reset,
16142  * followed by fixups by BIOS, and has its config space
16143  * set up identically to what it was at cold boot.
16144  */
16145 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
16146 {
16147 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
16148 	struct net_device *netdev = pci_get_drvdata(pdev);
16149 	struct bnxt *bp = netdev_priv(netdev);
16150 	int retry = 0;
16151 	int err = 0;
16152 	int off;
16153 
16154 	netdev_info(bp->dev, "PCI Slot Reset\n");
16155 
16156 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16157 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
16158 		msleep(900);
16159 
16160 	rtnl_lock();
16161 
16162 	if (pci_enable_device(pdev)) {
16163 		dev_err(&pdev->dev,
16164 			"Cannot re-enable PCI device after reset.\n");
16165 	} else {
16166 		pci_set_master(pdev);
16167 		/* Upon fatal error, our device internal logic that latches to
16168 		 * BAR value is getting reset and will restore only upon
16169 		 * rewriting the BARs.
16170 		 *
16171 		 * As pci_restore_state() does not re-write the BARs if the
16172 		 * value is same as saved value earlier, driver needs to
16173 		 * write the BARs to 0 to force restore, in case of fatal error.
16174 		 */
16175 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
16176 				       &bp->state)) {
16177 			for (off = PCI_BASE_ADDRESS_0;
16178 			     off <= PCI_BASE_ADDRESS_5; off += 4)
16179 				pci_write_config_dword(bp->pdev, off, 0);
16180 		}
16181 		pci_restore_state(pdev);
16182 		pci_save_state(pdev);
16183 
16184 		bnxt_inv_fw_health_reg(bp);
16185 		bnxt_try_map_fw_health_reg(bp);
16186 
16187 		/* In some PCIe AER scenarios, firmware may take up to
16188 		 * 10 seconds to become ready in the worst case.
16189 		 */
16190 		do {
16191 			err = bnxt_try_recover_fw(bp);
16192 			if (!err)
16193 				break;
16194 			retry++;
16195 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
16196 
16197 		if (err) {
16198 			dev_err(&pdev->dev, "Firmware not ready\n");
16199 			goto reset_exit;
16200 		}
16201 
16202 		err = bnxt_hwrm_func_reset(bp);
16203 		if (!err)
16204 			result = PCI_ERS_RESULT_RECOVERED;
16205 
16206 		bnxt_ulp_irq_stop(bp);
16207 		bnxt_clear_int_mode(bp);
16208 		err = bnxt_init_int_mode(bp);
16209 		bnxt_ulp_irq_restart(bp, err);
16210 	}
16211 
16212 reset_exit:
16213 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16214 	bnxt_clear_reservations(bp, true);
16215 	rtnl_unlock();
16216 
16217 	return result;
16218 }
16219 
16220 /**
16221  * bnxt_io_resume - called when traffic can start flowing again.
16222  * @pdev: Pointer to PCI device
16223  *
16224  * This callback is called when the error recovery driver tells
16225  * us that its OK to resume normal operation.
16226  */
16227 static void bnxt_io_resume(struct pci_dev *pdev)
16228 {
16229 	struct net_device *netdev = pci_get_drvdata(pdev);
16230 	struct bnxt *bp = netdev_priv(netdev);
16231 	int err;
16232 
16233 	netdev_info(bp->dev, "PCI Slot Resume\n");
16234 	rtnl_lock();
16235 
16236 	err = bnxt_hwrm_func_qcaps(bp);
16237 	if (!err && netif_running(netdev))
16238 		err = bnxt_open(netdev);
16239 
16240 	if (!err)
16241 		netif_device_attach(netdev);
16242 
16243 	rtnl_unlock();
16244 	bnxt_ulp_start(bp, err);
16245 	if (!err)
16246 		bnxt_reenable_sriov(bp);
16247 }
16248 
16249 static const struct pci_error_handlers bnxt_err_handler = {
16250 	.error_detected	= bnxt_io_error_detected,
16251 	.slot_reset	= bnxt_io_slot_reset,
16252 	.resume		= bnxt_io_resume
16253 };
16254 
16255 static struct pci_driver bnxt_pci_driver = {
16256 	.name		= DRV_MODULE_NAME,
16257 	.id_table	= bnxt_pci_tbl,
16258 	.probe		= bnxt_init_one,
16259 	.remove		= bnxt_remove_one,
16260 	.shutdown	= bnxt_shutdown,
16261 	.driver.pm	= BNXT_PM_OPS,
16262 	.err_handler	= &bnxt_err_handler,
16263 #if defined(CONFIG_BNXT_SRIOV)
16264 	.sriov_configure = bnxt_sriov_configure,
16265 #endif
16266 };
16267 
16268 static int __init bnxt_init(void)
16269 {
16270 	int err;
16271 
16272 	bnxt_debug_init();
16273 	err = pci_register_driver(&bnxt_pci_driver);
16274 	if (err) {
16275 		bnxt_debug_exit();
16276 		return err;
16277 	}
16278 
16279 	return 0;
16280 }
16281 
16282 static void __exit bnxt_exit(void)
16283 {
16284 	pci_unregister_driver(&bnxt_pci_driver);
16285 	if (bnxt_pf_wq)
16286 		destroy_workqueue(bnxt_pf_wq);
16287 	bnxt_debug_exit();
16288 }
16289 
16290 module_init(bnxt_init);
16291 module_exit(bnxt_exit);
16292