xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision 5e724cb688a207ae7a348d57f9ea77f475998883)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  * Copyright (c) 2016-2019 Broadcom Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  */
10 
11 #include <linux/module.h>
12 
13 #include <linux/stringify.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/pci.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/bitops.h>
27 #include <linux/io.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/byteorder.h>
31 #include <asm/page.h>
32 #include <linux/time.h>
33 #include <linux/mii.h>
34 #include <linux/mdio.h>
35 #include <linux/if.h>
36 #include <linux/if_vlan.h>
37 #include <linux/if_bridge.h>
38 #include <linux/rtc.h>
39 #include <linux/bpf.h>
40 #include <net/gro.h>
41 #include <net/ip.h>
42 #include <net/tcp.h>
43 #include <net/udp.h>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
46 #include <net/udp_tunnel.h>
47 #include <linux/workqueue.h>
48 #include <linux/prefetch.h>
49 #include <linux/cache.h>
50 #include <linux/log2.h>
51 #include <linux/bitmap.h>
52 #include <linux/cpu_rmap.h>
53 #include <linux/cpumask.h>
54 #include <net/pkt_cls.h>
55 #include <net/page_pool/helpers.h>
56 #include <linux/align.h>
57 #include <net/netdev_queues.h>
58 
59 #include "bnxt_hsi.h"
60 #include "bnxt.h"
61 #include "bnxt_hwrm.h"
62 #include "bnxt_ulp.h"
63 #include "bnxt_sriov.h"
64 #include "bnxt_ethtool.h"
65 #include "bnxt_dcb.h"
66 #include "bnxt_xdp.h"
67 #include "bnxt_ptp.h"
68 #include "bnxt_vfr.h"
69 #include "bnxt_tc.h"
70 #include "bnxt_devlink.h"
71 #include "bnxt_debugfs.h"
72 #include "bnxt_hwmon.h"
73 
74 #define BNXT_TX_TIMEOUT		(5 * HZ)
75 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
76 				 NETIF_MSG_TX_ERR)
77 
78 MODULE_LICENSE("GPL");
79 MODULE_DESCRIPTION("Broadcom NetXtreme network driver");
80 
81 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
82 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
83 #define BNXT_RX_COPY_THRESH 256
84 
85 #define BNXT_TX_PUSH_THRESH 164
86 
87 /* indexed by enum board_idx */
88 static const struct {
89 	char *name;
90 } board_info[] = {
91 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
92 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
93 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
94 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
95 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
96 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
97 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
98 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
99 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
100 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
101 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
102 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
103 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
104 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
105 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
106 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
108 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
109 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
110 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
111 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
112 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
113 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
114 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
115 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
116 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
117 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
118 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
119 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
120 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
121 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
122 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
123 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
124 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
125 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
126 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
127 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
128 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
129 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
130 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
131 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
132 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
133 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
134 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
135 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
136 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
137 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
138 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
139 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
140 	[NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" },
141 };
142 
143 static const struct pci_device_id bnxt_pci_tbl[] = {
144 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
145 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
146 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
147 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
148 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
149 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
150 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
151 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
152 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
153 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
155 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
159 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
160 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
164 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
165 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
166 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
167 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
168 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
169 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
170 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
171 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
172 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
173 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
174 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
175 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
176 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
178 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
179 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
180 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
181 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
182 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
183 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
184 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
185 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
187 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
188 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
189 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
190 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
191 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
192 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
193 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
194 #ifdef CONFIG_BNXT_SRIOV
195 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
196 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
197 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
198 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
199 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
200 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
201 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
202 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
203 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
204 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
205 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
206 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
207 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
208 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
209 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
210 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
211 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
212 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
214 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
215 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
216 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
217 #endif
218 	{ 0 }
219 };
220 
221 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
222 
223 static const u16 bnxt_vf_req_snif[] = {
224 	HWRM_FUNC_CFG,
225 	HWRM_FUNC_VF_CFG,
226 	HWRM_PORT_PHY_QCFG,
227 	HWRM_CFA_L2_FILTER_ALLOC,
228 };
229 
230 static const u16 bnxt_async_events_arr[] = {
231 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
232 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
233 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
234 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
235 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
236 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
237 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
238 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
239 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
240 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
241 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
242 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
243 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
244 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
245 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
246 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
247 };
248 
249 static struct workqueue_struct *bnxt_pf_wq;
250 
251 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
252 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
253 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
254 
255 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
256 	.ports = {
257 		.src = 0,
258 		.dst = 0,
259 	},
260 	.addrs = {
261 		.v6addrs = {
262 			.src = BNXT_IPV6_MASK_NONE,
263 			.dst = BNXT_IPV6_MASK_NONE,
264 		},
265 	},
266 };
267 
268 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
269 	.ports = {
270 		.src = cpu_to_be16(0xffff),
271 		.dst = cpu_to_be16(0xffff),
272 	},
273 	.addrs = {
274 		.v6addrs = {
275 			.src = BNXT_IPV6_MASK_ALL,
276 			.dst = BNXT_IPV6_MASK_ALL,
277 		},
278 	},
279 };
280 
281 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
282 	.ports = {
283 		.src = cpu_to_be16(0xffff),
284 		.dst = cpu_to_be16(0xffff),
285 	},
286 	.addrs = {
287 		.v4addrs = {
288 			.src = cpu_to_be32(0xffffffff),
289 			.dst = cpu_to_be32(0xffffffff),
290 		},
291 	},
292 };
293 
294 static bool bnxt_vf_pciid(enum board_idx idx)
295 {
296 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
297 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
298 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
299 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF);
300 }
301 
302 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
303 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
304 #define DB_CP_IRQ_DIS_FLAGS	(DB_KEY_CP | DB_IRQ_DIS)
305 
306 #define BNXT_CP_DB_IRQ_DIS(db)						\
307 		writel(DB_CP_IRQ_DIS_FLAGS, db)
308 
309 #define BNXT_DB_CQ(db, idx)						\
310 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
311 
312 #define BNXT_DB_NQ_P5(db, idx)						\
313 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
314 		    (db)->doorbell)
315 
316 #define BNXT_DB_NQ_P7(db, idx)						\
317 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
318 		    DB_RING_IDX(db, idx), (db)->doorbell)
319 
320 #define BNXT_DB_CQ_ARM(db, idx)						\
321 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
322 
323 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
324 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
325 		    DB_RING_IDX(db, idx), (db)->doorbell)
326 
327 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
328 {
329 	if (bp->flags & BNXT_FLAG_CHIP_P7)
330 		BNXT_DB_NQ_P7(db, idx);
331 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
332 		BNXT_DB_NQ_P5(db, idx);
333 	else
334 		BNXT_DB_CQ(db, idx);
335 }
336 
337 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
338 {
339 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
340 		BNXT_DB_NQ_ARM_P5(db, idx);
341 	else
342 		BNXT_DB_CQ_ARM(db, idx);
343 }
344 
345 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
346 {
347 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
348 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
349 			    DB_RING_IDX(db, idx), db->doorbell);
350 	else
351 		BNXT_DB_CQ(db, idx);
352 }
353 
354 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
355 {
356 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
357 		return;
358 
359 	if (BNXT_PF(bp))
360 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
361 	else
362 		schedule_delayed_work(&bp->fw_reset_task, delay);
363 }
364 
365 static void __bnxt_queue_sp_work(struct bnxt *bp)
366 {
367 	if (BNXT_PF(bp))
368 		queue_work(bnxt_pf_wq, &bp->sp_task);
369 	else
370 		schedule_work(&bp->sp_task);
371 }
372 
373 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
374 {
375 	set_bit(event, &bp->sp_event);
376 	__bnxt_queue_sp_work(bp);
377 }
378 
379 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
380 {
381 	if (!rxr->bnapi->in_reset) {
382 		rxr->bnapi->in_reset = true;
383 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
384 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
385 		else
386 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
387 		__bnxt_queue_sp_work(bp);
388 	}
389 	rxr->rx_next_cons = 0xffff;
390 }
391 
392 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
393 			  u16 curr)
394 {
395 	struct bnxt_napi *bnapi = txr->bnapi;
396 
397 	if (bnapi->tx_fault)
398 		return;
399 
400 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
401 		   txr->txq_index, txr->tx_hw_cons,
402 		   txr->tx_cons, txr->tx_prod, curr);
403 	WARN_ON_ONCE(1);
404 	bnapi->tx_fault = 1;
405 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
406 }
407 
408 const u16 bnxt_lhint_arr[] = {
409 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
410 	TX_BD_FLAGS_LHINT_512_TO_1023,
411 	TX_BD_FLAGS_LHINT_1024_TO_2047,
412 	TX_BD_FLAGS_LHINT_1024_TO_2047,
413 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
414 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
415 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
416 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
417 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
418 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
419 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
420 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
421 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
422 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
423 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
424 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
425 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
426 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
427 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
428 };
429 
430 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
431 {
432 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
433 
434 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
435 		return 0;
436 
437 	return md_dst->u.port_info.port_id;
438 }
439 
440 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
441 			     u16 prod)
442 {
443 	/* Sync BD data before updating doorbell */
444 	wmb();
445 	bnxt_db_write(bp, &txr->tx_db, prod);
446 	txr->kick_pending = 0;
447 }
448 
449 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
450 {
451 	struct bnxt *bp = netdev_priv(dev);
452 	struct tx_bd *txbd, *txbd0;
453 	struct tx_bd_ext *txbd1;
454 	struct netdev_queue *txq;
455 	int i;
456 	dma_addr_t mapping;
457 	unsigned int length, pad = 0;
458 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
459 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
460 	struct pci_dev *pdev = bp->pdev;
461 	u16 prod, last_frag, txts_prod;
462 	struct bnxt_tx_ring_info *txr;
463 	struct bnxt_sw_tx_bd *tx_buf;
464 	__le32 lflags = 0;
465 
466 	i = skb_get_queue_mapping(skb);
467 	if (unlikely(i >= bp->tx_nr_rings)) {
468 		dev_kfree_skb_any(skb);
469 		dev_core_stats_tx_dropped_inc(dev);
470 		return NETDEV_TX_OK;
471 	}
472 
473 	txq = netdev_get_tx_queue(dev, i);
474 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
475 	prod = txr->tx_prod;
476 
477 	free_size = bnxt_tx_avail(bp, txr);
478 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
479 		/* We must have raced with NAPI cleanup */
480 		if (net_ratelimit() && txr->kick_pending)
481 			netif_warn(bp, tx_err, dev,
482 				   "bnxt: ring busy w/ flush pending!\n");
483 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
484 					bp->tx_wake_thresh))
485 			return NETDEV_TX_BUSY;
486 	}
487 
488 	if (unlikely(ipv6_hopopt_jumbo_remove(skb)))
489 		goto tx_free;
490 
491 	length = skb->len;
492 	len = skb_headlen(skb);
493 	last_frag = skb_shinfo(skb)->nr_frags;
494 
495 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
496 
497 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
498 	tx_buf->skb = skb;
499 	tx_buf->nr_frags = last_frag;
500 
501 	vlan_tag_flags = 0;
502 	cfa_action = bnxt_xmit_get_cfa_action(skb);
503 	if (skb_vlan_tag_present(skb)) {
504 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
505 				 skb_vlan_tag_get(skb);
506 		/* Currently supports 8021Q, 8021AD vlan offloads
507 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
508 		 */
509 		if (skb->vlan_proto == htons(ETH_P_8021Q))
510 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
511 	}
512 
513 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp &&
514 	    ptp->tx_tstamp_en) {
515 		if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) {
516 			lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
517 			tx_buf->is_ts_pkt = 1;
518 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
519 		} else if (!skb_is_gso(skb)) {
520 			u16 seq_id, hdr_off;
521 
522 			if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) &&
523 			    !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) {
524 				if (vlan_tag_flags)
525 					hdr_off += VLAN_HLEN;
526 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
527 				tx_buf->is_ts_pkt = 1;
528 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
529 
530 				ptp->txts_req[txts_prod].tx_seqid = seq_id;
531 				ptp->txts_req[txts_prod].tx_hdr_off = hdr_off;
532 				tx_buf->txts_prod = txts_prod;
533 			}
534 		}
535 	}
536 	if (unlikely(skb->no_fcs))
537 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
538 
539 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
540 	    !lflags) {
541 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
542 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
543 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
544 		void __iomem *db = txr->tx_db.doorbell;
545 		void *pdata = tx_push_buf->data;
546 		u64 *end;
547 		int j, push_len;
548 
549 		/* Set COAL_NOW to be ready quickly for the next push */
550 		tx_push->tx_bd_len_flags_type =
551 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
552 					TX_BD_TYPE_LONG_TX_BD |
553 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
554 					TX_BD_FLAGS_COAL_NOW |
555 					TX_BD_FLAGS_PACKET_END |
556 					(2 << TX_BD_FLAGS_BD_CNT_SHIFT));
557 
558 		if (skb->ip_summed == CHECKSUM_PARTIAL)
559 			tx_push1->tx_bd_hsize_lflags =
560 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
561 		else
562 			tx_push1->tx_bd_hsize_lflags = 0;
563 
564 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
565 		tx_push1->tx_bd_cfa_action =
566 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
567 
568 		end = pdata + length;
569 		end = PTR_ALIGN(end, 8) - 1;
570 		*end = 0;
571 
572 		skb_copy_from_linear_data(skb, pdata, len);
573 		pdata += len;
574 		for (j = 0; j < last_frag; j++) {
575 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
576 			void *fptr;
577 
578 			fptr = skb_frag_address_safe(frag);
579 			if (!fptr)
580 				goto normal_tx;
581 
582 			memcpy(pdata, fptr, skb_frag_size(frag));
583 			pdata += skb_frag_size(frag);
584 		}
585 
586 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
587 		txbd->tx_bd_haddr = txr->data_mapping;
588 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
589 		prod = NEXT_TX(prod);
590 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
591 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
592 		memcpy(txbd, tx_push1, sizeof(*txbd));
593 		prod = NEXT_TX(prod);
594 		tx_push->doorbell =
595 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
596 				    DB_RING_IDX(&txr->tx_db, prod));
597 		WRITE_ONCE(txr->tx_prod, prod);
598 
599 		tx_buf->is_push = 1;
600 		netdev_tx_sent_queue(txq, skb->len);
601 		wmb();	/* Sync is_push and byte queue before pushing data */
602 
603 		push_len = (length + sizeof(*tx_push) + 7) / 8;
604 		if (push_len > 16) {
605 			__iowrite64_copy(db, tx_push_buf, 16);
606 			__iowrite32_copy(db + 4, tx_push_buf + 1,
607 					 (push_len - 16) << 1);
608 		} else {
609 			__iowrite64_copy(db, tx_push_buf, push_len);
610 		}
611 
612 		goto tx_done;
613 	}
614 
615 normal_tx:
616 	if (length < BNXT_MIN_PKT_SIZE) {
617 		pad = BNXT_MIN_PKT_SIZE - length;
618 		if (skb_pad(skb, pad))
619 			/* SKB already freed. */
620 			goto tx_kick_pending;
621 		length = BNXT_MIN_PKT_SIZE;
622 	}
623 
624 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
625 
626 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
627 		goto tx_free;
628 
629 	dma_unmap_addr_set(tx_buf, mapping, mapping);
630 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
631 		((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
632 
633 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
634 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
635 
636 	prod = NEXT_TX(prod);
637 	txbd1 = (struct tx_bd_ext *)
638 		&txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
639 
640 	txbd1->tx_bd_hsize_lflags = lflags;
641 	if (skb_is_gso(skb)) {
642 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
643 		u32 hdr_len;
644 
645 		if (skb->encapsulation) {
646 			if (udp_gso)
647 				hdr_len = skb_inner_transport_offset(skb) +
648 					  sizeof(struct udphdr);
649 			else
650 				hdr_len = skb_inner_tcp_all_headers(skb);
651 		} else if (udp_gso) {
652 			hdr_len = skb_transport_offset(skb) +
653 				  sizeof(struct udphdr);
654 		} else {
655 			hdr_len = skb_tcp_all_headers(skb);
656 		}
657 
658 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
659 					TX_BD_FLAGS_T_IPID |
660 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
661 		length = skb_shinfo(skb)->gso_size;
662 		txbd1->tx_bd_mss = cpu_to_le32(length);
663 		length += hdr_len;
664 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
665 		txbd1->tx_bd_hsize_lflags |=
666 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
667 		txbd1->tx_bd_mss = 0;
668 	}
669 
670 	length >>= 9;
671 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
672 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
673 				     skb->len);
674 		i = 0;
675 		goto tx_dma_error;
676 	}
677 	flags |= bnxt_lhint_arr[length];
678 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
679 
680 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
681 	txbd1->tx_bd_cfa_action =
682 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
683 	txbd0 = txbd;
684 	for (i = 0; i < last_frag; i++) {
685 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
686 
687 		prod = NEXT_TX(prod);
688 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
689 
690 		len = skb_frag_size(frag);
691 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
692 					   DMA_TO_DEVICE);
693 
694 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
695 			goto tx_dma_error;
696 
697 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
698 		dma_unmap_addr_set(tx_buf, mapping, mapping);
699 
700 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
701 
702 		flags = len << TX_BD_LEN_SHIFT;
703 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
704 	}
705 
706 	flags &= ~TX_BD_LEN;
707 	txbd->tx_bd_len_flags_type =
708 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
709 			    TX_BD_FLAGS_PACKET_END);
710 
711 	netdev_tx_sent_queue(txq, skb->len);
712 
713 	skb_tx_timestamp(skb);
714 
715 	prod = NEXT_TX(prod);
716 	WRITE_ONCE(txr->tx_prod, prod);
717 
718 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
719 		bnxt_txr_db_kick(bp, txr, prod);
720 	} else {
721 		if (free_size >= bp->tx_wake_thresh)
722 			txbd0->tx_bd_len_flags_type |=
723 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
724 		txr->kick_pending = 1;
725 	}
726 
727 tx_done:
728 
729 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
730 		if (netdev_xmit_more() && !tx_buf->is_push) {
731 			txbd0->tx_bd_len_flags_type &=
732 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
733 			bnxt_txr_db_kick(bp, txr, prod);
734 		}
735 
736 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
737 				   bp->tx_wake_thresh);
738 	}
739 	return NETDEV_TX_OK;
740 
741 tx_dma_error:
742 	last_frag = i;
743 
744 	/* start back at beginning and unmap skb */
745 	prod = txr->tx_prod;
746 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
747 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
748 			 skb_headlen(skb), DMA_TO_DEVICE);
749 	prod = NEXT_TX(prod);
750 
751 	/* unmap remaining mapped pages */
752 	for (i = 0; i < last_frag; i++) {
753 		prod = NEXT_TX(prod);
754 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
755 		dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
756 			       skb_frag_size(&skb_shinfo(skb)->frags[i]),
757 			       DMA_TO_DEVICE);
758 	}
759 
760 tx_free:
761 	dev_kfree_skb_any(skb);
762 tx_kick_pending:
763 	if (BNXT_TX_PTP_IS_SET(lflags)) {
764 		txr->tx_buf_ring[txr->tx_prod].is_ts_pkt = 0;
765 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
766 		if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
767 			/* set SKB to err so PTP worker will clean up */
768 			ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO);
769 	}
770 	if (txr->kick_pending)
771 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
772 	txr->tx_buf_ring[txr->tx_prod].skb = NULL;
773 	dev_core_stats_tx_dropped_inc(dev);
774 	return NETDEV_TX_OK;
775 }
776 
777 /* Returns true if some remaining TX packets not processed. */
778 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
779 			  int budget)
780 {
781 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
782 	struct pci_dev *pdev = bp->pdev;
783 	u16 hw_cons = txr->tx_hw_cons;
784 	unsigned int tx_bytes = 0;
785 	u16 cons = txr->tx_cons;
786 	int tx_pkts = 0;
787 	bool rc = false;
788 
789 	while (RING_TX(bp, cons) != hw_cons) {
790 		struct bnxt_sw_tx_bd *tx_buf;
791 		struct sk_buff *skb;
792 		bool is_ts_pkt;
793 		int j, last;
794 
795 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
796 		skb = tx_buf->skb;
797 
798 		if (unlikely(!skb)) {
799 			bnxt_sched_reset_txr(bp, txr, cons);
800 			return rc;
801 		}
802 
803 		is_ts_pkt = tx_buf->is_ts_pkt;
804 		if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) {
805 			rc = true;
806 			break;
807 		}
808 
809 		cons = NEXT_TX(cons);
810 		tx_pkts++;
811 		tx_bytes += skb->len;
812 		tx_buf->skb = NULL;
813 		tx_buf->is_ts_pkt = 0;
814 
815 		if (tx_buf->is_push) {
816 			tx_buf->is_push = 0;
817 			goto next_tx_int;
818 		}
819 
820 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
821 				 skb_headlen(skb), DMA_TO_DEVICE);
822 		last = tx_buf->nr_frags;
823 
824 		for (j = 0; j < last; j++) {
825 			cons = NEXT_TX(cons);
826 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
827 			dma_unmap_page(
828 				&pdev->dev,
829 				dma_unmap_addr(tx_buf, mapping),
830 				skb_frag_size(&skb_shinfo(skb)->frags[j]),
831 				DMA_TO_DEVICE);
832 		}
833 		if (unlikely(is_ts_pkt)) {
834 			if (BNXT_CHIP_P5(bp)) {
835 				/* PTP worker takes ownership of the skb */
836 				bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod);
837 				skb = NULL;
838 			}
839 		}
840 
841 next_tx_int:
842 		cons = NEXT_TX(cons);
843 
844 		dev_consume_skb_any(skb);
845 	}
846 
847 	WRITE_ONCE(txr->tx_cons, cons);
848 
849 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
850 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
851 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
852 
853 	return rc;
854 }
855 
856 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
857 {
858 	struct bnxt_tx_ring_info *txr;
859 	bool more = false;
860 	int i;
861 
862 	bnxt_for_each_napi_tx(i, bnapi, txr) {
863 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
864 			more |= __bnxt_tx_int(bp, txr, budget);
865 	}
866 	if (!more)
867 		bnapi->events &= ~BNXT_TX_CMP_EVENT;
868 }
869 
870 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
871 					 struct bnxt_rx_ring_info *rxr,
872 					 unsigned int *offset,
873 					 gfp_t gfp)
874 {
875 	struct page *page;
876 
877 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
878 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
879 						BNXT_RX_PAGE_SIZE);
880 	} else {
881 		page = page_pool_dev_alloc_pages(rxr->page_pool);
882 		*offset = 0;
883 	}
884 	if (!page)
885 		return NULL;
886 
887 	*mapping = page_pool_get_dma_addr(page) + *offset;
888 	return page;
889 }
890 
891 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
892 				       gfp_t gfp)
893 {
894 	u8 *data;
895 	struct pci_dev *pdev = bp->pdev;
896 
897 	if (gfp == GFP_ATOMIC)
898 		data = napi_alloc_frag(bp->rx_buf_size);
899 	else
900 		data = netdev_alloc_frag(bp->rx_buf_size);
901 	if (!data)
902 		return NULL;
903 
904 	*mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
905 					bp->rx_buf_use_size, bp->rx_dir,
906 					DMA_ATTR_WEAK_ORDERING);
907 
908 	if (dma_mapping_error(&pdev->dev, *mapping)) {
909 		skb_free_frag(data);
910 		data = NULL;
911 	}
912 	return data;
913 }
914 
915 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
916 		       u16 prod, gfp_t gfp)
917 {
918 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
919 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
920 	dma_addr_t mapping;
921 
922 	if (BNXT_RX_PAGE_MODE(bp)) {
923 		unsigned int offset;
924 		struct page *page =
925 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
926 
927 		if (!page)
928 			return -ENOMEM;
929 
930 		mapping += bp->rx_dma_offset;
931 		rx_buf->data = page;
932 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
933 	} else {
934 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, gfp);
935 
936 		if (!data)
937 			return -ENOMEM;
938 
939 		rx_buf->data = data;
940 		rx_buf->data_ptr = data + bp->rx_offset;
941 	}
942 	rx_buf->mapping = mapping;
943 
944 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
945 	return 0;
946 }
947 
948 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
949 {
950 	u16 prod = rxr->rx_prod;
951 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
952 	struct bnxt *bp = rxr->bnapi->bp;
953 	struct rx_bd *cons_bd, *prod_bd;
954 
955 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
956 	cons_rx_buf = &rxr->rx_buf_ring[cons];
957 
958 	prod_rx_buf->data = data;
959 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
960 
961 	prod_rx_buf->mapping = cons_rx_buf->mapping;
962 
963 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
964 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
965 
966 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
967 }
968 
969 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
970 {
971 	u16 next, max = rxr->rx_agg_bmap_size;
972 
973 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
974 	if (next >= max)
975 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
976 	return next;
977 }
978 
979 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
980 				     struct bnxt_rx_ring_info *rxr,
981 				     u16 prod, gfp_t gfp)
982 {
983 	struct rx_bd *rxbd =
984 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
985 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
986 	struct page *page;
987 	dma_addr_t mapping;
988 	u16 sw_prod = rxr->rx_sw_agg_prod;
989 	unsigned int offset = 0;
990 
991 	page = __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
992 
993 	if (!page)
994 		return -ENOMEM;
995 
996 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
997 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
998 
999 	__set_bit(sw_prod, rxr->rx_agg_bmap);
1000 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
1001 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1002 
1003 	rx_agg_buf->page = page;
1004 	rx_agg_buf->offset = offset;
1005 	rx_agg_buf->mapping = mapping;
1006 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1007 	rxbd->rx_bd_opaque = sw_prod;
1008 	return 0;
1009 }
1010 
1011 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1012 				       struct bnxt_cp_ring_info *cpr,
1013 				       u16 cp_cons, u16 curr)
1014 {
1015 	struct rx_agg_cmp *agg;
1016 
1017 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1018 	agg = (struct rx_agg_cmp *)
1019 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1020 	return agg;
1021 }
1022 
1023 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1024 					      struct bnxt_rx_ring_info *rxr,
1025 					      u16 agg_id, u16 curr)
1026 {
1027 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1028 
1029 	return &tpa_info->agg_arr[curr];
1030 }
1031 
1032 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1033 				   u16 start, u32 agg_bufs, bool tpa)
1034 {
1035 	struct bnxt_napi *bnapi = cpr->bnapi;
1036 	struct bnxt *bp = bnapi->bp;
1037 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1038 	u16 prod = rxr->rx_agg_prod;
1039 	u16 sw_prod = rxr->rx_sw_agg_prod;
1040 	bool p5_tpa = false;
1041 	u32 i;
1042 
1043 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1044 		p5_tpa = true;
1045 
1046 	for (i = 0; i < agg_bufs; i++) {
1047 		u16 cons;
1048 		struct rx_agg_cmp *agg;
1049 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1050 		struct rx_bd *prod_bd;
1051 		struct page *page;
1052 
1053 		if (p5_tpa)
1054 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1055 		else
1056 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1057 		cons = agg->rx_agg_cmp_opaque;
1058 		__clear_bit(cons, rxr->rx_agg_bmap);
1059 
1060 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1061 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1062 
1063 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1064 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1065 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1066 
1067 		/* It is possible for sw_prod to be equal to cons, so
1068 		 * set cons_rx_buf->page to NULL first.
1069 		 */
1070 		page = cons_rx_buf->page;
1071 		cons_rx_buf->page = NULL;
1072 		prod_rx_buf->page = page;
1073 		prod_rx_buf->offset = cons_rx_buf->offset;
1074 
1075 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1076 
1077 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1078 
1079 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1080 		prod_bd->rx_bd_opaque = sw_prod;
1081 
1082 		prod = NEXT_RX_AGG(prod);
1083 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1084 	}
1085 	rxr->rx_agg_prod = prod;
1086 	rxr->rx_sw_agg_prod = sw_prod;
1087 }
1088 
1089 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1090 					      struct bnxt_rx_ring_info *rxr,
1091 					      u16 cons, void *data, u8 *data_ptr,
1092 					      dma_addr_t dma_addr,
1093 					      unsigned int offset_and_len)
1094 {
1095 	unsigned int len = offset_and_len & 0xffff;
1096 	struct page *page = data;
1097 	u16 prod = rxr->rx_prod;
1098 	struct sk_buff *skb;
1099 	int err;
1100 
1101 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1102 	if (unlikely(err)) {
1103 		bnxt_reuse_rx_data(rxr, cons, data);
1104 		return NULL;
1105 	}
1106 	dma_addr -= bp->rx_dma_offset;
1107 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1108 				bp->rx_dir);
1109 	skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE);
1110 	if (!skb) {
1111 		page_pool_recycle_direct(rxr->page_pool, page);
1112 		return NULL;
1113 	}
1114 	skb_mark_for_recycle(skb);
1115 	skb_reserve(skb, bp->rx_offset);
1116 	__skb_put(skb, len);
1117 
1118 	return skb;
1119 }
1120 
1121 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1122 					struct bnxt_rx_ring_info *rxr,
1123 					u16 cons, void *data, u8 *data_ptr,
1124 					dma_addr_t dma_addr,
1125 					unsigned int offset_and_len)
1126 {
1127 	unsigned int payload = offset_and_len >> 16;
1128 	unsigned int len = offset_and_len & 0xffff;
1129 	skb_frag_t *frag;
1130 	struct page *page = data;
1131 	u16 prod = rxr->rx_prod;
1132 	struct sk_buff *skb;
1133 	int off, err;
1134 
1135 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1136 	if (unlikely(err)) {
1137 		bnxt_reuse_rx_data(rxr, cons, data);
1138 		return NULL;
1139 	}
1140 	dma_addr -= bp->rx_dma_offset;
1141 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1142 				bp->rx_dir);
1143 
1144 	if (unlikely(!payload))
1145 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1146 
1147 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1148 	if (!skb) {
1149 		page_pool_recycle_direct(rxr->page_pool, page);
1150 		return NULL;
1151 	}
1152 
1153 	skb_mark_for_recycle(skb);
1154 	off = (void *)data_ptr - page_address(page);
1155 	skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE);
1156 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1157 	       payload + NET_IP_ALIGN);
1158 
1159 	frag = &skb_shinfo(skb)->frags[0];
1160 	skb_frag_size_sub(frag, payload);
1161 	skb_frag_off_add(frag, payload);
1162 	skb->data_len -= payload;
1163 	skb->tail += payload;
1164 
1165 	return skb;
1166 }
1167 
1168 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1169 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1170 				   void *data, u8 *data_ptr,
1171 				   dma_addr_t dma_addr,
1172 				   unsigned int offset_and_len)
1173 {
1174 	u16 prod = rxr->rx_prod;
1175 	struct sk_buff *skb;
1176 	int err;
1177 
1178 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1179 	if (unlikely(err)) {
1180 		bnxt_reuse_rx_data(rxr, cons, data);
1181 		return NULL;
1182 	}
1183 
1184 	skb = napi_build_skb(data, bp->rx_buf_size);
1185 	dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1186 			       bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
1187 	if (!skb) {
1188 		skb_free_frag(data);
1189 		return NULL;
1190 	}
1191 
1192 	skb_reserve(skb, bp->rx_offset);
1193 	skb_put(skb, offset_and_len & 0xffff);
1194 	return skb;
1195 }
1196 
1197 static u32 __bnxt_rx_agg_pages(struct bnxt *bp,
1198 			       struct bnxt_cp_ring_info *cpr,
1199 			       struct skb_shared_info *shinfo,
1200 			       u16 idx, u32 agg_bufs, bool tpa,
1201 			       struct xdp_buff *xdp)
1202 {
1203 	struct bnxt_napi *bnapi = cpr->bnapi;
1204 	struct pci_dev *pdev = bp->pdev;
1205 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1206 	u16 prod = rxr->rx_agg_prod;
1207 	u32 i, total_frag_len = 0;
1208 	bool p5_tpa = false;
1209 
1210 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1211 		p5_tpa = true;
1212 
1213 	for (i = 0; i < agg_bufs; i++) {
1214 		skb_frag_t *frag = &shinfo->frags[i];
1215 		u16 cons, frag_len;
1216 		struct rx_agg_cmp *agg;
1217 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1218 		struct page *page;
1219 		dma_addr_t mapping;
1220 
1221 		if (p5_tpa)
1222 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1223 		else
1224 			agg = bnxt_get_agg(bp, cpr, idx, i);
1225 		cons = agg->rx_agg_cmp_opaque;
1226 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1227 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1228 
1229 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1230 		skb_frag_fill_page_desc(frag, cons_rx_buf->page,
1231 					cons_rx_buf->offset, frag_len);
1232 		shinfo->nr_frags = i + 1;
1233 		__clear_bit(cons, rxr->rx_agg_bmap);
1234 
1235 		/* It is possible for bnxt_alloc_rx_page() to allocate
1236 		 * a sw_prod index that equals the cons index, so we
1237 		 * need to clear the cons entry now.
1238 		 */
1239 		mapping = cons_rx_buf->mapping;
1240 		page = cons_rx_buf->page;
1241 		cons_rx_buf->page = NULL;
1242 
1243 		if (xdp && page_is_pfmemalloc(page))
1244 			xdp_buff_set_frag_pfmemalloc(xdp);
1245 
1246 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1247 			--shinfo->nr_frags;
1248 			cons_rx_buf->page = page;
1249 
1250 			/* Update prod since possibly some pages have been
1251 			 * allocated already.
1252 			 */
1253 			rxr->rx_agg_prod = prod;
1254 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1255 			return 0;
1256 		}
1257 
1258 		dma_sync_single_for_cpu(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1259 					bp->rx_dir);
1260 
1261 		total_frag_len += frag_len;
1262 		prod = NEXT_RX_AGG(prod);
1263 	}
1264 	rxr->rx_agg_prod = prod;
1265 	return total_frag_len;
1266 }
1267 
1268 static struct sk_buff *bnxt_rx_agg_pages_skb(struct bnxt *bp,
1269 					     struct bnxt_cp_ring_info *cpr,
1270 					     struct sk_buff *skb, u16 idx,
1271 					     u32 agg_bufs, bool tpa)
1272 {
1273 	struct skb_shared_info *shinfo = skb_shinfo(skb);
1274 	u32 total_frag_len = 0;
1275 
1276 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo, idx,
1277 					     agg_bufs, tpa, NULL);
1278 	if (!total_frag_len) {
1279 		skb_mark_for_recycle(skb);
1280 		dev_kfree_skb(skb);
1281 		return NULL;
1282 	}
1283 
1284 	skb->data_len += total_frag_len;
1285 	skb->len += total_frag_len;
1286 	skb->truesize += BNXT_RX_PAGE_SIZE * agg_bufs;
1287 	return skb;
1288 }
1289 
1290 static u32 bnxt_rx_agg_pages_xdp(struct bnxt *bp,
1291 				 struct bnxt_cp_ring_info *cpr,
1292 				 struct xdp_buff *xdp, u16 idx,
1293 				 u32 agg_bufs, bool tpa)
1294 {
1295 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1296 	u32 total_frag_len = 0;
1297 
1298 	if (!xdp_buff_has_frags(xdp))
1299 		shinfo->nr_frags = 0;
1300 
1301 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo,
1302 					     idx, agg_bufs, tpa, xdp);
1303 	if (total_frag_len) {
1304 		xdp_buff_set_frags_flag(xdp);
1305 		shinfo->nr_frags = agg_bufs;
1306 		shinfo->xdp_frags_size = total_frag_len;
1307 	}
1308 	return total_frag_len;
1309 }
1310 
1311 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1312 			       u8 agg_bufs, u32 *raw_cons)
1313 {
1314 	u16 last;
1315 	struct rx_agg_cmp *agg;
1316 
1317 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1318 	last = RING_CMP(*raw_cons);
1319 	agg = (struct rx_agg_cmp *)
1320 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1321 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1322 }
1323 
1324 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1325 				      unsigned int len,
1326 				      dma_addr_t mapping)
1327 {
1328 	struct bnxt *bp = bnapi->bp;
1329 	struct pci_dev *pdev = bp->pdev;
1330 	struct sk_buff *skb;
1331 
1332 	skb = napi_alloc_skb(&bnapi->napi, len);
1333 	if (!skb)
1334 		return NULL;
1335 
1336 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1337 				bp->rx_dir);
1338 
1339 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1340 	       len + NET_IP_ALIGN);
1341 
1342 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1343 				   bp->rx_dir);
1344 
1345 	skb_put(skb, len);
1346 
1347 	return skb;
1348 }
1349 
1350 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1351 				     unsigned int len,
1352 				     dma_addr_t mapping)
1353 {
1354 	return bnxt_copy_data(bnapi, data, len, mapping);
1355 }
1356 
1357 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1358 				     struct xdp_buff *xdp,
1359 				     unsigned int len,
1360 				     dma_addr_t mapping)
1361 {
1362 	unsigned int metasize = 0;
1363 	u8 *data = xdp->data;
1364 	struct sk_buff *skb;
1365 
1366 	len = xdp->data_end - xdp->data_meta;
1367 	metasize = xdp->data - xdp->data_meta;
1368 	data = xdp->data_meta;
1369 
1370 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1371 	if (!skb)
1372 		return skb;
1373 
1374 	if (metasize) {
1375 		skb_metadata_set(skb, metasize);
1376 		__skb_pull(skb, metasize);
1377 	}
1378 
1379 	return skb;
1380 }
1381 
1382 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1383 			   u32 *raw_cons, void *cmp)
1384 {
1385 	struct rx_cmp *rxcmp = cmp;
1386 	u32 tmp_raw_cons = *raw_cons;
1387 	u8 cmp_type, agg_bufs = 0;
1388 
1389 	cmp_type = RX_CMP_TYPE(rxcmp);
1390 
1391 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1392 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1393 			    RX_CMP_AGG_BUFS) >>
1394 			   RX_CMP_AGG_BUFS_SHIFT;
1395 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1396 		struct rx_tpa_end_cmp *tpa_end = cmp;
1397 
1398 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1399 			return 0;
1400 
1401 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1402 	}
1403 
1404 	if (agg_bufs) {
1405 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1406 			return -EBUSY;
1407 	}
1408 	*raw_cons = tmp_raw_cons;
1409 	return 0;
1410 }
1411 
1412 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1413 {
1414 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1415 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1416 
1417 	if (test_bit(idx, map->agg_idx_bmap))
1418 		idx = find_first_zero_bit(map->agg_idx_bmap,
1419 					  BNXT_AGG_IDX_BMAP_SIZE);
1420 	__set_bit(idx, map->agg_idx_bmap);
1421 	map->agg_id_tbl[agg_id] = idx;
1422 	return idx;
1423 }
1424 
1425 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1426 {
1427 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1428 
1429 	__clear_bit(idx, map->agg_idx_bmap);
1430 }
1431 
1432 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1433 {
1434 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1435 
1436 	return map->agg_id_tbl[agg_id];
1437 }
1438 
1439 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1440 			      struct rx_tpa_start_cmp *tpa_start,
1441 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1442 {
1443 	tpa_info->cfa_code_valid = 1;
1444 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1445 	tpa_info->vlan_valid = 0;
1446 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1447 		tpa_info->vlan_valid = 1;
1448 		tpa_info->metadata =
1449 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1450 	}
1451 }
1452 
1453 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1454 				 struct rx_tpa_start_cmp *tpa_start,
1455 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1456 {
1457 	tpa_info->vlan_valid = 0;
1458 	if (TPA_START_VLAN_VALID(tpa_start)) {
1459 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1460 		u32 vlan_proto = ETH_P_8021Q;
1461 
1462 		tpa_info->vlan_valid = 1;
1463 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1464 			vlan_proto = ETH_P_8021AD;
1465 		tpa_info->metadata = vlan_proto << 16 |
1466 				     TPA_START_METADATA0_TCI(tpa_start1);
1467 	}
1468 }
1469 
1470 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1471 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1472 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1473 {
1474 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1475 	struct bnxt_tpa_info *tpa_info;
1476 	u16 cons, prod, agg_id;
1477 	struct rx_bd *prod_bd;
1478 	dma_addr_t mapping;
1479 
1480 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1481 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1482 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1483 	} else {
1484 		agg_id = TPA_START_AGG_ID(tpa_start);
1485 	}
1486 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1487 	prod = rxr->rx_prod;
1488 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1489 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1490 	tpa_info = &rxr->rx_tpa[agg_id];
1491 
1492 	if (unlikely(cons != rxr->rx_next_cons ||
1493 		     TPA_START_ERROR(tpa_start))) {
1494 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1495 			    cons, rxr->rx_next_cons,
1496 			    TPA_START_ERROR_CODE(tpa_start1));
1497 		bnxt_sched_reset_rxr(bp, rxr);
1498 		return;
1499 	}
1500 	prod_rx_buf->data = tpa_info->data;
1501 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1502 
1503 	mapping = tpa_info->mapping;
1504 	prod_rx_buf->mapping = mapping;
1505 
1506 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1507 
1508 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1509 
1510 	tpa_info->data = cons_rx_buf->data;
1511 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1512 	cons_rx_buf->data = NULL;
1513 	tpa_info->mapping = cons_rx_buf->mapping;
1514 
1515 	tpa_info->len =
1516 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1517 				RX_TPA_START_CMP_LEN_SHIFT;
1518 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1519 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1520 		tpa_info->gso_type = SKB_GSO_TCPV4;
1521 		if (TPA_START_IS_IPV6(tpa_start1))
1522 			tpa_info->gso_type = SKB_GSO_TCPV6;
1523 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1524 		else if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP &&
1525 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1526 			tpa_info->gso_type = SKB_GSO_TCPV6;
1527 		tpa_info->rss_hash =
1528 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1529 	} else {
1530 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1531 		tpa_info->gso_type = 0;
1532 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1533 	}
1534 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1535 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1536 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1537 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1538 	else
1539 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1540 	tpa_info->agg_count = 0;
1541 
1542 	rxr->rx_prod = NEXT_RX(prod);
1543 	cons = RING_RX(bp, NEXT_RX(cons));
1544 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1545 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1546 
1547 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1548 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1549 	cons_rx_buf->data = NULL;
1550 }
1551 
1552 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1553 {
1554 	if (agg_bufs)
1555 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1556 }
1557 
1558 #ifdef CONFIG_INET
1559 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1560 {
1561 	struct udphdr *uh = NULL;
1562 
1563 	if (ip_proto == htons(ETH_P_IP)) {
1564 		struct iphdr *iph = (struct iphdr *)skb->data;
1565 
1566 		if (iph->protocol == IPPROTO_UDP)
1567 			uh = (struct udphdr *)(iph + 1);
1568 	} else {
1569 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1570 
1571 		if (iph->nexthdr == IPPROTO_UDP)
1572 			uh = (struct udphdr *)(iph + 1);
1573 	}
1574 	if (uh) {
1575 		if (uh->check)
1576 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1577 		else
1578 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1579 	}
1580 }
1581 #endif
1582 
1583 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1584 					   int payload_off, int tcp_ts,
1585 					   struct sk_buff *skb)
1586 {
1587 #ifdef CONFIG_INET
1588 	struct tcphdr *th;
1589 	int len, nw_off;
1590 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1591 	u32 hdr_info = tpa_info->hdr_info;
1592 	bool loopback = false;
1593 
1594 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1595 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1596 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1597 
1598 	/* If the packet is an internal loopback packet, the offsets will
1599 	 * have an extra 4 bytes.
1600 	 */
1601 	if (inner_mac_off == 4) {
1602 		loopback = true;
1603 	} else if (inner_mac_off > 4) {
1604 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1605 					    ETH_HLEN - 2));
1606 
1607 		/* We only support inner iPv4/ipv6.  If we don't see the
1608 		 * correct protocol ID, it must be a loopback packet where
1609 		 * the offsets are off by 4.
1610 		 */
1611 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1612 			loopback = true;
1613 	}
1614 	if (loopback) {
1615 		/* internal loopback packet, subtract all offsets by 4 */
1616 		inner_ip_off -= 4;
1617 		inner_mac_off -= 4;
1618 		outer_ip_off -= 4;
1619 	}
1620 
1621 	nw_off = inner_ip_off - ETH_HLEN;
1622 	skb_set_network_header(skb, nw_off);
1623 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1624 		struct ipv6hdr *iph = ipv6_hdr(skb);
1625 
1626 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1627 		len = skb->len - skb_transport_offset(skb);
1628 		th = tcp_hdr(skb);
1629 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1630 	} else {
1631 		struct iphdr *iph = ip_hdr(skb);
1632 
1633 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1634 		len = skb->len - skb_transport_offset(skb);
1635 		th = tcp_hdr(skb);
1636 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1637 	}
1638 
1639 	if (inner_mac_off) { /* tunnel */
1640 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1641 					    ETH_HLEN - 2));
1642 
1643 		bnxt_gro_tunnel(skb, proto);
1644 	}
1645 #endif
1646 	return skb;
1647 }
1648 
1649 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1650 					   int payload_off, int tcp_ts,
1651 					   struct sk_buff *skb)
1652 {
1653 #ifdef CONFIG_INET
1654 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1655 	u32 hdr_info = tpa_info->hdr_info;
1656 	int iphdr_len, nw_off;
1657 
1658 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1659 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1660 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1661 
1662 	nw_off = inner_ip_off - ETH_HLEN;
1663 	skb_set_network_header(skb, nw_off);
1664 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1665 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1666 	skb_set_transport_header(skb, nw_off + iphdr_len);
1667 
1668 	if (inner_mac_off) { /* tunnel */
1669 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1670 					    ETH_HLEN - 2));
1671 
1672 		bnxt_gro_tunnel(skb, proto);
1673 	}
1674 #endif
1675 	return skb;
1676 }
1677 
1678 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1679 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1680 
1681 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1682 					   int payload_off, int tcp_ts,
1683 					   struct sk_buff *skb)
1684 {
1685 #ifdef CONFIG_INET
1686 	struct tcphdr *th;
1687 	int len, nw_off, tcp_opt_len = 0;
1688 
1689 	if (tcp_ts)
1690 		tcp_opt_len = 12;
1691 
1692 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1693 		struct iphdr *iph;
1694 
1695 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1696 			 ETH_HLEN;
1697 		skb_set_network_header(skb, nw_off);
1698 		iph = ip_hdr(skb);
1699 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1700 		len = skb->len - skb_transport_offset(skb);
1701 		th = tcp_hdr(skb);
1702 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1703 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1704 		struct ipv6hdr *iph;
1705 
1706 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1707 			 ETH_HLEN;
1708 		skb_set_network_header(skb, nw_off);
1709 		iph = ipv6_hdr(skb);
1710 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1711 		len = skb->len - skb_transport_offset(skb);
1712 		th = tcp_hdr(skb);
1713 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1714 	} else {
1715 		dev_kfree_skb_any(skb);
1716 		return NULL;
1717 	}
1718 
1719 	if (nw_off) /* tunnel */
1720 		bnxt_gro_tunnel(skb, skb->protocol);
1721 #endif
1722 	return skb;
1723 }
1724 
1725 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1726 					   struct bnxt_tpa_info *tpa_info,
1727 					   struct rx_tpa_end_cmp *tpa_end,
1728 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1729 					   struct sk_buff *skb)
1730 {
1731 #ifdef CONFIG_INET
1732 	int payload_off;
1733 	u16 segs;
1734 
1735 	segs = TPA_END_TPA_SEGS(tpa_end);
1736 	if (segs == 1)
1737 		return skb;
1738 
1739 	NAPI_GRO_CB(skb)->count = segs;
1740 	skb_shinfo(skb)->gso_size =
1741 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1742 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1743 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1744 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1745 	else
1746 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1747 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1748 	if (likely(skb))
1749 		tcp_gro_complete(skb);
1750 #endif
1751 	return skb;
1752 }
1753 
1754 /* Given the cfa_code of a received packet determine which
1755  * netdev (vf-rep or PF) the packet is destined to.
1756  */
1757 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1758 {
1759 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1760 
1761 	/* if vf-rep dev is NULL, the must belongs to the PF */
1762 	return dev ? dev : bp->dev;
1763 }
1764 
1765 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1766 					   struct bnxt_cp_ring_info *cpr,
1767 					   u32 *raw_cons,
1768 					   struct rx_tpa_end_cmp *tpa_end,
1769 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1770 					   u8 *event)
1771 {
1772 	struct bnxt_napi *bnapi = cpr->bnapi;
1773 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1774 	struct net_device *dev = bp->dev;
1775 	u8 *data_ptr, agg_bufs;
1776 	unsigned int len;
1777 	struct bnxt_tpa_info *tpa_info;
1778 	dma_addr_t mapping;
1779 	struct sk_buff *skb;
1780 	u16 idx = 0, agg_id;
1781 	void *data;
1782 	bool gro;
1783 
1784 	if (unlikely(bnapi->in_reset)) {
1785 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1786 
1787 		if (rc < 0)
1788 			return ERR_PTR(-EBUSY);
1789 		return NULL;
1790 	}
1791 
1792 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1793 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1794 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1795 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1796 		tpa_info = &rxr->rx_tpa[agg_id];
1797 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1798 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1799 				    agg_bufs, tpa_info->agg_count);
1800 			agg_bufs = tpa_info->agg_count;
1801 		}
1802 		tpa_info->agg_count = 0;
1803 		*event |= BNXT_AGG_EVENT;
1804 		bnxt_free_agg_idx(rxr, agg_id);
1805 		idx = agg_id;
1806 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1807 	} else {
1808 		agg_id = TPA_END_AGG_ID(tpa_end);
1809 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1810 		tpa_info = &rxr->rx_tpa[agg_id];
1811 		idx = RING_CMP(*raw_cons);
1812 		if (agg_bufs) {
1813 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1814 				return ERR_PTR(-EBUSY);
1815 
1816 			*event |= BNXT_AGG_EVENT;
1817 			idx = NEXT_CMP(idx);
1818 		}
1819 		gro = !!TPA_END_GRO(tpa_end);
1820 	}
1821 	data = tpa_info->data;
1822 	data_ptr = tpa_info->data_ptr;
1823 	prefetch(data_ptr);
1824 	len = tpa_info->len;
1825 	mapping = tpa_info->mapping;
1826 
1827 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1828 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1829 		if (agg_bufs > MAX_SKB_FRAGS)
1830 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1831 				    agg_bufs, (int)MAX_SKB_FRAGS);
1832 		return NULL;
1833 	}
1834 
1835 	if (len <= bp->rx_copy_thresh) {
1836 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1837 		if (!skb) {
1838 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1839 			cpr->sw_stats->rx.rx_oom_discards += 1;
1840 			return NULL;
1841 		}
1842 	} else {
1843 		u8 *new_data;
1844 		dma_addr_t new_mapping;
1845 
1846 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, GFP_ATOMIC);
1847 		if (!new_data) {
1848 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1849 			cpr->sw_stats->rx.rx_oom_discards += 1;
1850 			return NULL;
1851 		}
1852 
1853 		tpa_info->data = new_data;
1854 		tpa_info->data_ptr = new_data + bp->rx_offset;
1855 		tpa_info->mapping = new_mapping;
1856 
1857 		skb = napi_build_skb(data, bp->rx_buf_size);
1858 		dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1859 				       bp->rx_buf_use_size, bp->rx_dir,
1860 				       DMA_ATTR_WEAK_ORDERING);
1861 
1862 		if (!skb) {
1863 			skb_free_frag(data);
1864 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1865 			cpr->sw_stats->rx.rx_oom_discards += 1;
1866 			return NULL;
1867 		}
1868 		skb_reserve(skb, bp->rx_offset);
1869 		skb_put(skb, len);
1870 	}
1871 
1872 	if (agg_bufs) {
1873 		skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, idx, agg_bufs, true);
1874 		if (!skb) {
1875 			/* Page reuse already handled by bnxt_rx_pages(). */
1876 			cpr->sw_stats->rx.rx_oom_discards += 1;
1877 			return NULL;
1878 		}
1879 	}
1880 
1881 	if (tpa_info->cfa_code_valid)
1882 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1883 	skb->protocol = eth_type_trans(skb, dev);
1884 
1885 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1886 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1887 
1888 	if (tpa_info->vlan_valid &&
1889 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1890 		__be16 vlan_proto = htons(tpa_info->metadata >>
1891 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1892 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1893 
1894 		if (eth_type_vlan(vlan_proto)) {
1895 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1896 		} else {
1897 			dev_kfree_skb(skb);
1898 			return NULL;
1899 		}
1900 	}
1901 
1902 	skb_checksum_none_assert(skb);
1903 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1904 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1905 		skb->csum_level =
1906 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1907 	}
1908 
1909 	if (gro)
1910 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1911 
1912 	return skb;
1913 }
1914 
1915 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1916 			 struct rx_agg_cmp *rx_agg)
1917 {
1918 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1919 	struct bnxt_tpa_info *tpa_info;
1920 
1921 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1922 	tpa_info = &rxr->rx_tpa[agg_id];
1923 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1924 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1925 }
1926 
1927 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1928 			     struct sk_buff *skb)
1929 {
1930 	skb_mark_for_recycle(skb);
1931 
1932 	if (skb->dev != bp->dev) {
1933 		/* this packet belongs to a vf-rep */
1934 		bnxt_vf_rep_rx(bp, skb);
1935 		return;
1936 	}
1937 	skb_record_rx_queue(skb, bnapi->index);
1938 	napi_gro_receive(&bnapi->napi, skb);
1939 }
1940 
1941 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
1942 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
1943 {
1944 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
1945 
1946 	if (BNXT_PTP_RX_TS_VALID(flags))
1947 		goto ts_valid;
1948 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
1949 		return false;
1950 
1951 ts_valid:
1952 	*cmpl_ts = ts;
1953 	return true;
1954 }
1955 
1956 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
1957 				    struct rx_cmp *rxcmp,
1958 				    struct rx_cmp_ext *rxcmp1)
1959 {
1960 	__be16 vlan_proto;
1961 	u16 vtag;
1962 
1963 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1964 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
1965 		u32 meta_data;
1966 
1967 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
1968 			return skb;
1969 
1970 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1971 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1972 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
1973 		if (eth_type_vlan(vlan_proto))
1974 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1975 		else
1976 			goto vlan_err;
1977 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
1978 		if (RX_CMP_VLAN_VALID(rxcmp)) {
1979 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
1980 
1981 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
1982 				vlan_proto = htons(ETH_P_8021Q);
1983 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
1984 				vlan_proto = htons(ETH_P_8021AD);
1985 			else
1986 				goto vlan_err;
1987 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
1988 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1989 		}
1990 	}
1991 	return skb;
1992 vlan_err:
1993 	dev_kfree_skb(skb);
1994 	return NULL;
1995 }
1996 
1997 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp,
1998 					   struct rx_cmp *rxcmp)
1999 {
2000 	u8 ext_op;
2001 
2002 	ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp);
2003 	switch (ext_op) {
2004 	case EXT_OP_INNER_4:
2005 	case EXT_OP_OUTER_4:
2006 	case EXT_OP_INNFL_3:
2007 	case EXT_OP_OUTFL_3:
2008 		return PKT_HASH_TYPE_L4;
2009 	default:
2010 		return PKT_HASH_TYPE_L3;
2011 	}
2012 }
2013 
2014 /* returns the following:
2015  * 1       - 1 packet successfully received
2016  * 0       - successful TPA_START, packet not completed yet
2017  * -EBUSY  - completion ring does not have all the agg buffers yet
2018  * -ENOMEM - packet aborted due to out of memory
2019  * -EIO    - packet aborted due to hw error indicated in BD
2020  */
2021 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2022 		       u32 *raw_cons, u8 *event)
2023 {
2024 	struct bnxt_napi *bnapi = cpr->bnapi;
2025 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2026 	struct net_device *dev = bp->dev;
2027 	struct rx_cmp *rxcmp;
2028 	struct rx_cmp_ext *rxcmp1;
2029 	u32 tmp_raw_cons = *raw_cons;
2030 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2031 	struct bnxt_sw_rx_bd *rx_buf;
2032 	unsigned int len;
2033 	u8 *data_ptr, agg_bufs, cmp_type;
2034 	bool xdp_active = false;
2035 	dma_addr_t dma_addr;
2036 	struct sk_buff *skb;
2037 	struct xdp_buff xdp;
2038 	u32 flags, misc;
2039 	u32 cmpl_ts;
2040 	void *data;
2041 	int rc = 0;
2042 
2043 	rxcmp = (struct rx_cmp *)
2044 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2045 
2046 	cmp_type = RX_CMP_TYPE(rxcmp);
2047 
2048 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2049 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2050 		goto next_rx_no_prod_no_len;
2051 	}
2052 
2053 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2054 	cp_cons = RING_CMP(tmp_raw_cons);
2055 	rxcmp1 = (struct rx_cmp_ext *)
2056 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2057 
2058 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2059 		return -EBUSY;
2060 
2061 	/* The valid test of the entry must be done first before
2062 	 * reading any further.
2063 	 */
2064 	dma_rmb();
2065 	prod = rxr->rx_prod;
2066 
2067 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2068 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2069 		bnxt_tpa_start(bp, rxr, cmp_type,
2070 			       (struct rx_tpa_start_cmp *)rxcmp,
2071 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2072 
2073 		*event |= BNXT_RX_EVENT;
2074 		goto next_rx_no_prod_no_len;
2075 
2076 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2077 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2078 				   (struct rx_tpa_end_cmp *)rxcmp,
2079 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2080 
2081 		if (IS_ERR(skb))
2082 			return -EBUSY;
2083 
2084 		rc = -ENOMEM;
2085 		if (likely(skb)) {
2086 			bnxt_deliver_skb(bp, bnapi, skb);
2087 			rc = 1;
2088 		}
2089 		*event |= BNXT_RX_EVENT;
2090 		goto next_rx_no_prod_no_len;
2091 	}
2092 
2093 	cons = rxcmp->rx_cmp_opaque;
2094 	if (unlikely(cons != rxr->rx_next_cons)) {
2095 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2096 
2097 		/* 0xffff is forced error, don't print it */
2098 		if (rxr->rx_next_cons != 0xffff)
2099 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2100 				    cons, rxr->rx_next_cons);
2101 		bnxt_sched_reset_rxr(bp, rxr);
2102 		if (rc1)
2103 			return rc1;
2104 		goto next_rx_no_prod_no_len;
2105 	}
2106 	rx_buf = &rxr->rx_buf_ring[cons];
2107 	data = rx_buf->data;
2108 	data_ptr = rx_buf->data_ptr;
2109 	prefetch(data_ptr);
2110 
2111 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2112 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2113 
2114 	if (agg_bufs) {
2115 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2116 			return -EBUSY;
2117 
2118 		cp_cons = NEXT_CMP(cp_cons);
2119 		*event |= BNXT_AGG_EVENT;
2120 	}
2121 	*event |= BNXT_RX_EVENT;
2122 
2123 	rx_buf->data = NULL;
2124 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2125 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2126 
2127 		bnxt_reuse_rx_data(rxr, cons, data);
2128 		if (agg_bufs)
2129 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2130 					       false);
2131 
2132 		rc = -EIO;
2133 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2134 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2135 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2136 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2137 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2138 						 rx_err);
2139 				bnxt_sched_reset_rxr(bp, rxr);
2140 			}
2141 		}
2142 		goto next_rx_no_len;
2143 	}
2144 
2145 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2146 	len = flags >> RX_CMP_LEN_SHIFT;
2147 	dma_addr = rx_buf->mapping;
2148 
2149 	if (bnxt_xdp_attached(bp, rxr)) {
2150 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2151 		if (agg_bufs) {
2152 			u32 frag_len = bnxt_rx_agg_pages_xdp(bp, cpr, &xdp,
2153 							     cp_cons, agg_bufs,
2154 							     false);
2155 			if (!frag_len)
2156 				goto oom_next_rx;
2157 		}
2158 		xdp_active = true;
2159 	}
2160 
2161 	if (xdp_active) {
2162 		if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) {
2163 			rc = 1;
2164 			goto next_rx;
2165 		}
2166 	}
2167 
2168 	if (len <= bp->rx_copy_thresh) {
2169 		if (!xdp_active)
2170 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2171 		else
2172 			skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr);
2173 		bnxt_reuse_rx_data(rxr, cons, data);
2174 		if (!skb) {
2175 			if (agg_bufs) {
2176 				if (!xdp_active)
2177 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2178 							       agg_bufs, false);
2179 				else
2180 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2181 			}
2182 			goto oom_next_rx;
2183 		}
2184 	} else {
2185 		u32 payload;
2186 
2187 		if (rx_buf->data_ptr == data_ptr)
2188 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2189 		else
2190 			payload = 0;
2191 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2192 				      payload | len);
2193 		if (!skb)
2194 			goto oom_next_rx;
2195 	}
2196 
2197 	if (agg_bufs) {
2198 		if (!xdp_active) {
2199 			skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, cp_cons, agg_bufs, false);
2200 			if (!skb)
2201 				goto oom_next_rx;
2202 		} else {
2203 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, rxr->page_pool, &xdp, rxcmp1);
2204 			if (!skb) {
2205 				/* we should be able to free the old skb here */
2206 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2207 				goto oom_next_rx;
2208 			}
2209 		}
2210 	}
2211 
2212 	if (RX_CMP_HASH_VALID(rxcmp)) {
2213 		enum pkt_hash_types type;
2214 
2215 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2216 			type = bnxt_rss_ext_op(bp, rxcmp);
2217 		} else {
2218 			u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
2219 
2220 			/* RSS profiles 1 and 3 with extract code 0 for inner
2221 			 * 4-tuple
2222 			 */
2223 			if (hash_type != 1 && hash_type != 3)
2224 				type = PKT_HASH_TYPE_L3;
2225 			else
2226 				type = PKT_HASH_TYPE_L4;
2227 		}
2228 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2229 	}
2230 
2231 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2232 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2233 	skb->protocol = eth_type_trans(skb, dev);
2234 
2235 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2236 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2237 		if (!skb)
2238 			goto next_rx;
2239 	}
2240 
2241 	skb_checksum_none_assert(skb);
2242 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2243 		if (dev->features & NETIF_F_RXCSUM) {
2244 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2245 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2246 		}
2247 	} else {
2248 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2249 			if (dev->features & NETIF_F_RXCSUM)
2250 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2251 		}
2252 	}
2253 
2254 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2255 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2256 			u64 ns, ts;
2257 
2258 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2259 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2260 
2261 				spin_lock_bh(&ptp->ptp_lock);
2262 				ns = timecounter_cyc2time(&ptp->tc, ts);
2263 				spin_unlock_bh(&ptp->ptp_lock);
2264 				memset(skb_hwtstamps(skb), 0,
2265 				       sizeof(*skb_hwtstamps(skb)));
2266 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2267 			}
2268 		}
2269 	}
2270 	bnxt_deliver_skb(bp, bnapi, skb);
2271 	rc = 1;
2272 
2273 next_rx:
2274 	cpr->rx_packets += 1;
2275 	cpr->rx_bytes += len;
2276 
2277 next_rx_no_len:
2278 	rxr->rx_prod = NEXT_RX(prod);
2279 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2280 
2281 next_rx_no_prod_no_len:
2282 	*raw_cons = tmp_raw_cons;
2283 
2284 	return rc;
2285 
2286 oom_next_rx:
2287 	cpr->sw_stats->rx.rx_oom_discards += 1;
2288 	rc = -ENOMEM;
2289 	goto next_rx;
2290 }
2291 
2292 /* In netpoll mode, if we are using a combined completion ring, we need to
2293  * discard the rx packets and recycle the buffers.
2294  */
2295 static int bnxt_force_rx_discard(struct bnxt *bp,
2296 				 struct bnxt_cp_ring_info *cpr,
2297 				 u32 *raw_cons, u8 *event)
2298 {
2299 	u32 tmp_raw_cons = *raw_cons;
2300 	struct rx_cmp_ext *rxcmp1;
2301 	struct rx_cmp *rxcmp;
2302 	u16 cp_cons;
2303 	u8 cmp_type;
2304 	int rc;
2305 
2306 	cp_cons = RING_CMP(tmp_raw_cons);
2307 	rxcmp = (struct rx_cmp *)
2308 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2309 
2310 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2311 	cp_cons = RING_CMP(tmp_raw_cons);
2312 	rxcmp1 = (struct rx_cmp_ext *)
2313 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2314 
2315 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2316 		return -EBUSY;
2317 
2318 	/* The valid test of the entry must be done first before
2319 	 * reading any further.
2320 	 */
2321 	dma_rmb();
2322 	cmp_type = RX_CMP_TYPE(rxcmp);
2323 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2324 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2325 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2326 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2327 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2328 		struct rx_tpa_end_cmp_ext *tpa_end1;
2329 
2330 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2331 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2332 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2333 	}
2334 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2335 	if (rc && rc != -EBUSY)
2336 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2337 	return rc;
2338 }
2339 
2340 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2341 {
2342 	struct bnxt_fw_health *fw_health = bp->fw_health;
2343 	u32 reg = fw_health->regs[reg_idx];
2344 	u32 reg_type, reg_off, val = 0;
2345 
2346 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2347 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2348 	switch (reg_type) {
2349 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2350 		pci_read_config_dword(bp->pdev, reg_off, &val);
2351 		break;
2352 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2353 		reg_off = fw_health->mapped_regs[reg_idx];
2354 		fallthrough;
2355 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2356 		val = readl(bp->bar0 + reg_off);
2357 		break;
2358 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2359 		val = readl(bp->bar1 + reg_off);
2360 		break;
2361 	}
2362 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2363 		val &= fw_health->fw_reset_inprog_reg_mask;
2364 	return val;
2365 }
2366 
2367 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2368 {
2369 	int i;
2370 
2371 	for (i = 0; i < bp->rx_nr_rings; i++) {
2372 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2373 		struct bnxt_ring_grp_info *grp_info;
2374 
2375 		grp_info = &bp->grp_info[grp_idx];
2376 		if (grp_info->agg_fw_ring_id == ring_id)
2377 			return grp_idx;
2378 	}
2379 	return INVALID_HW_RING_ID;
2380 }
2381 
2382 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2383 {
2384 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2385 
2386 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2387 		return link_info->force_link_speed2;
2388 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2389 		return link_info->force_pam4_link_speed;
2390 	return link_info->force_link_speed;
2391 }
2392 
2393 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2394 {
2395 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2396 
2397 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2398 		link_info->req_link_speed = link_info->force_link_speed2;
2399 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2400 		switch (link_info->req_link_speed) {
2401 		case BNXT_LINK_SPEED_50GB_PAM4:
2402 		case BNXT_LINK_SPEED_100GB_PAM4:
2403 		case BNXT_LINK_SPEED_200GB_PAM4:
2404 		case BNXT_LINK_SPEED_400GB_PAM4:
2405 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2406 			break;
2407 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2408 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2409 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2410 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2411 			break;
2412 		default:
2413 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2414 		}
2415 		return;
2416 	}
2417 	link_info->req_link_speed = link_info->force_link_speed;
2418 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2419 	if (link_info->force_pam4_link_speed) {
2420 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2421 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2422 	}
2423 }
2424 
2425 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2426 {
2427 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2428 
2429 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2430 		link_info->advertising = link_info->auto_link_speeds2;
2431 		return;
2432 	}
2433 	link_info->advertising = link_info->auto_link_speeds;
2434 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2435 }
2436 
2437 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2438 {
2439 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2440 
2441 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2442 		if (link_info->req_link_speed != link_info->force_link_speed2)
2443 			return true;
2444 		return false;
2445 	}
2446 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2447 	    link_info->req_link_speed != link_info->force_link_speed)
2448 		return true;
2449 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2450 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2451 		return true;
2452 	return false;
2453 }
2454 
2455 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2456 {
2457 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2458 
2459 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2460 		if (link_info->advertising != link_info->auto_link_speeds2)
2461 			return true;
2462 		return false;
2463 	}
2464 	if (link_info->advertising != link_info->auto_link_speeds ||
2465 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2466 		return true;
2467 	return false;
2468 }
2469 
2470 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2471 	((data2) &							\
2472 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2473 
2474 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2475 	(((data2) &							\
2476 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2477 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2478 
2479 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2480 	((data1) &							\
2481 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2482 
2483 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2484 	(((data1) &							\
2485 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2486 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2487 
2488 /* Return true if the workqueue has to be scheduled */
2489 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2490 {
2491 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2492 
2493 	switch (err_type) {
2494 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2495 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2496 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2497 		break;
2498 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2499 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2500 		break;
2501 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2502 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2503 		break;
2504 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2505 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2506 		char *threshold_type;
2507 		bool notify = false;
2508 		char *dir_str;
2509 
2510 		switch (type) {
2511 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2512 			threshold_type = "warning";
2513 			break;
2514 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2515 			threshold_type = "critical";
2516 			break;
2517 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2518 			threshold_type = "fatal";
2519 			break;
2520 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2521 			threshold_type = "shutdown";
2522 			break;
2523 		default:
2524 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2525 			return false;
2526 		}
2527 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2528 			dir_str = "above";
2529 			notify = true;
2530 		} else {
2531 			dir_str = "below";
2532 		}
2533 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2534 			    dir_str, threshold_type);
2535 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2536 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2537 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2538 		if (notify) {
2539 			bp->thermal_threshold_type = type;
2540 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2541 			return true;
2542 		}
2543 		return false;
2544 	}
2545 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2546 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2547 		break;
2548 	default:
2549 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2550 			   err_type);
2551 		break;
2552 	}
2553 	return false;
2554 }
2555 
2556 #define BNXT_GET_EVENT_PORT(data)	\
2557 	((data) &			\
2558 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2559 
2560 #define BNXT_EVENT_RING_TYPE(data2)	\
2561 	((data2) &			\
2562 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2563 
2564 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2565 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2566 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2567 
2568 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2569 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2570 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2571 
2572 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2573 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2574 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2575 
2576 #define BNXT_PHC_BITS	48
2577 
2578 static int bnxt_async_event_process(struct bnxt *bp,
2579 				    struct hwrm_async_event_cmpl *cmpl)
2580 {
2581 	u16 event_id = le16_to_cpu(cmpl->event_id);
2582 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2583 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2584 
2585 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2586 		   event_id, data1, data2);
2587 
2588 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2589 	switch (event_id) {
2590 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2591 		struct bnxt_link_info *link_info = &bp->link_info;
2592 
2593 		if (BNXT_VF(bp))
2594 			goto async_event_process_exit;
2595 
2596 		/* print unsupported speed warning in forced speed mode only */
2597 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2598 		    (data1 & 0x20000)) {
2599 			u16 fw_speed = bnxt_get_force_speed(link_info);
2600 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2601 
2602 			if (speed != SPEED_UNKNOWN)
2603 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2604 					    speed);
2605 		}
2606 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2607 	}
2608 		fallthrough;
2609 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2610 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2611 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2612 		fallthrough;
2613 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2614 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2615 		break;
2616 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2617 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2618 		break;
2619 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2620 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2621 
2622 		if (BNXT_VF(bp))
2623 			break;
2624 
2625 		if (bp->pf.port_id != port_id)
2626 			break;
2627 
2628 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2629 		break;
2630 	}
2631 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2632 		if (BNXT_PF(bp))
2633 			goto async_event_process_exit;
2634 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2635 		break;
2636 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2637 		char *type_str = "Solicited";
2638 
2639 		if (!bp->fw_health)
2640 			goto async_event_process_exit;
2641 
2642 		bp->fw_reset_timestamp = jiffies;
2643 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2644 		if (!bp->fw_reset_min_dsecs)
2645 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2646 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2647 		if (!bp->fw_reset_max_dsecs)
2648 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2649 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2650 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2651 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2652 			type_str = "Fatal";
2653 			bp->fw_health->fatalities++;
2654 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2655 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2656 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2657 			type_str = "Non-fatal";
2658 			bp->fw_health->survivals++;
2659 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2660 		}
2661 		netif_warn(bp, hw, bp->dev,
2662 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2663 			   type_str, data1, data2,
2664 			   bp->fw_reset_min_dsecs * 100,
2665 			   bp->fw_reset_max_dsecs * 100);
2666 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2667 		break;
2668 	}
2669 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2670 		struct bnxt_fw_health *fw_health = bp->fw_health;
2671 		char *status_desc = "healthy";
2672 		u32 status;
2673 
2674 		if (!fw_health)
2675 			goto async_event_process_exit;
2676 
2677 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2678 			fw_health->enabled = false;
2679 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2680 			break;
2681 		}
2682 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2683 		fw_health->tmr_multiplier =
2684 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2685 				     bp->current_interval * 10);
2686 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2687 		if (!fw_health->enabled)
2688 			fw_health->last_fw_heartbeat =
2689 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2690 		fw_health->last_fw_reset_cnt =
2691 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2692 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2693 		if (status != BNXT_FW_STATUS_HEALTHY)
2694 			status_desc = "unhealthy";
2695 		netif_info(bp, drv, bp->dev,
2696 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2697 			   fw_health->primary ? "primary" : "backup", status,
2698 			   status_desc, fw_health->last_fw_reset_cnt);
2699 		if (!fw_health->enabled) {
2700 			/* Make sure tmr_counter is set and visible to
2701 			 * bnxt_health_check() before setting enabled to true.
2702 			 */
2703 			smp_wmb();
2704 			fw_health->enabled = true;
2705 		}
2706 		goto async_event_process_exit;
2707 	}
2708 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2709 		netif_notice(bp, hw, bp->dev,
2710 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2711 			     data1, data2);
2712 		goto async_event_process_exit;
2713 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2714 		struct bnxt_rx_ring_info *rxr;
2715 		u16 grp_idx;
2716 
2717 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2718 			goto async_event_process_exit;
2719 
2720 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2721 			    BNXT_EVENT_RING_TYPE(data2), data1);
2722 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2723 			goto async_event_process_exit;
2724 
2725 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2726 		if (grp_idx == INVALID_HW_RING_ID) {
2727 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2728 				    data1);
2729 			goto async_event_process_exit;
2730 		}
2731 		rxr = bp->bnapi[grp_idx]->rx_ring;
2732 		bnxt_sched_reset_rxr(bp, rxr);
2733 		goto async_event_process_exit;
2734 	}
2735 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2736 		struct bnxt_fw_health *fw_health = bp->fw_health;
2737 
2738 		netif_notice(bp, hw, bp->dev,
2739 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2740 			     data1, data2);
2741 		if (fw_health) {
2742 			fw_health->echo_req_data1 = data1;
2743 			fw_health->echo_req_data2 = data2;
2744 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2745 			break;
2746 		}
2747 		goto async_event_process_exit;
2748 	}
2749 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2750 		bnxt_ptp_pps_event(bp, data1, data2);
2751 		goto async_event_process_exit;
2752 	}
2753 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2754 		if (bnxt_event_error_report(bp, data1, data2))
2755 			break;
2756 		goto async_event_process_exit;
2757 	}
2758 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2759 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2760 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2761 			if (BNXT_PTP_USE_RTC(bp)) {
2762 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2763 				u64 ns;
2764 
2765 				if (!ptp)
2766 					goto async_event_process_exit;
2767 
2768 				spin_lock_bh(&ptp->ptp_lock);
2769 				bnxt_ptp_update_current_time(bp);
2770 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2771 				       BNXT_PHC_BITS) | ptp->current_time);
2772 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2773 				spin_unlock_bh(&ptp->ptp_lock);
2774 			}
2775 			break;
2776 		}
2777 		goto async_event_process_exit;
2778 	}
2779 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2780 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2781 
2782 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2783 		goto async_event_process_exit;
2784 	}
2785 	default:
2786 		goto async_event_process_exit;
2787 	}
2788 	__bnxt_queue_sp_work(bp);
2789 async_event_process_exit:
2790 	return 0;
2791 }
2792 
2793 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2794 {
2795 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2796 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2797 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2798 				(struct hwrm_fwd_req_cmpl *)txcmp;
2799 
2800 	switch (cmpl_type) {
2801 	case CMPL_BASE_TYPE_HWRM_DONE:
2802 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2803 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2804 		break;
2805 
2806 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2807 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2808 
2809 		if ((vf_id < bp->pf.first_vf_id) ||
2810 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2811 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2812 				   vf_id);
2813 			return -EINVAL;
2814 		}
2815 
2816 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2817 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2818 		break;
2819 
2820 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2821 		bnxt_async_event_process(bp,
2822 					 (struct hwrm_async_event_cmpl *)txcmp);
2823 		break;
2824 
2825 	default:
2826 		break;
2827 	}
2828 
2829 	return 0;
2830 }
2831 
2832 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2833 {
2834 	struct bnxt_napi *bnapi = dev_instance;
2835 	struct bnxt *bp = bnapi->bp;
2836 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2837 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2838 
2839 	cpr->event_ctr++;
2840 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2841 	napi_schedule(&bnapi->napi);
2842 	return IRQ_HANDLED;
2843 }
2844 
2845 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2846 {
2847 	u32 raw_cons = cpr->cp_raw_cons;
2848 	u16 cons = RING_CMP(raw_cons);
2849 	struct tx_cmp *txcmp;
2850 
2851 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2852 
2853 	return TX_CMP_VALID(txcmp, raw_cons);
2854 }
2855 
2856 static irqreturn_t bnxt_inta(int irq, void *dev_instance)
2857 {
2858 	struct bnxt_napi *bnapi = dev_instance;
2859 	struct bnxt *bp = bnapi->bp;
2860 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2861 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2862 	u32 int_status;
2863 
2864 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2865 
2866 	if (!bnxt_has_work(bp, cpr)) {
2867 		int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
2868 		/* return if erroneous interrupt */
2869 		if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
2870 			return IRQ_NONE;
2871 	}
2872 
2873 	/* disable ring IRQ */
2874 	BNXT_CP_DB_IRQ_DIS(cpr->cp_db.doorbell);
2875 
2876 	/* Return here if interrupt is shared and is disabled. */
2877 	if (unlikely(atomic_read(&bp->intr_sem) != 0))
2878 		return IRQ_HANDLED;
2879 
2880 	napi_schedule(&bnapi->napi);
2881 	return IRQ_HANDLED;
2882 }
2883 
2884 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2885 			    int budget)
2886 {
2887 	struct bnxt_napi *bnapi = cpr->bnapi;
2888 	u32 raw_cons = cpr->cp_raw_cons;
2889 	u32 cons;
2890 	int rx_pkts = 0;
2891 	u8 event = 0;
2892 	struct tx_cmp *txcmp;
2893 
2894 	cpr->has_more_work = 0;
2895 	cpr->had_work_done = 1;
2896 	while (1) {
2897 		u8 cmp_type;
2898 		int rc;
2899 
2900 		cons = RING_CMP(raw_cons);
2901 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2902 
2903 		if (!TX_CMP_VALID(txcmp, raw_cons))
2904 			break;
2905 
2906 		/* The valid test of the entry must be done first before
2907 		 * reading any further.
2908 		 */
2909 		dma_rmb();
2910 		cmp_type = TX_CMP_TYPE(txcmp);
2911 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
2912 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
2913 			u32 opaque = txcmp->tx_cmp_opaque;
2914 			struct bnxt_tx_ring_info *txr;
2915 			u16 tx_freed;
2916 
2917 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
2918 			event |= BNXT_TX_CMP_EVENT;
2919 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
2920 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
2921 			else
2922 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
2923 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
2924 				   bp->tx_ring_mask;
2925 			/* return full budget so NAPI will complete. */
2926 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
2927 				rx_pkts = budget;
2928 				raw_cons = NEXT_RAW_CMP(raw_cons);
2929 				if (budget)
2930 					cpr->has_more_work = 1;
2931 				break;
2932 			}
2933 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
2934 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
2935 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
2936 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2937 			if (likely(budget))
2938 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2939 			else
2940 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2941 							   &event);
2942 			if (likely(rc >= 0))
2943 				rx_pkts += rc;
2944 			/* Increment rx_pkts when rc is -ENOMEM to count towards
2945 			 * the NAPI budget.  Otherwise, we may potentially loop
2946 			 * here forever if we consistently cannot allocate
2947 			 * buffers.
2948 			 */
2949 			else if (rc == -ENOMEM && budget)
2950 				rx_pkts++;
2951 			else if (rc == -EBUSY)	/* partial completion */
2952 				break;
2953 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
2954 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
2955 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
2956 			bnxt_hwrm_handler(bp, txcmp);
2957 		}
2958 		raw_cons = NEXT_RAW_CMP(raw_cons);
2959 
2960 		if (rx_pkts && rx_pkts == budget) {
2961 			cpr->has_more_work = 1;
2962 			break;
2963 		}
2964 	}
2965 
2966 	if (event & BNXT_REDIRECT_EVENT) {
2967 		xdp_do_flush();
2968 		event &= ~BNXT_REDIRECT_EVENT;
2969 	}
2970 
2971 	if (event & BNXT_TX_EVENT) {
2972 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
2973 		u16 prod = txr->tx_prod;
2974 
2975 		/* Sync BD data before updating doorbell */
2976 		wmb();
2977 
2978 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
2979 		event &= ~BNXT_TX_EVENT;
2980 	}
2981 
2982 	cpr->cp_raw_cons = raw_cons;
2983 	bnapi->events |= event;
2984 	return rx_pkts;
2985 }
2986 
2987 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
2988 				  int budget)
2989 {
2990 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
2991 		bnapi->tx_int(bp, bnapi, budget);
2992 
2993 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
2994 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2995 
2996 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2997 		bnapi->events &= ~BNXT_RX_EVENT;
2998 	}
2999 	if (bnapi->events & BNXT_AGG_EVENT) {
3000 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3001 
3002 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3003 		bnapi->events &= ~BNXT_AGG_EVENT;
3004 	}
3005 }
3006 
3007 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3008 			  int budget)
3009 {
3010 	struct bnxt_napi *bnapi = cpr->bnapi;
3011 	int rx_pkts;
3012 
3013 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3014 
3015 	/* ACK completion ring before freeing tx ring and producing new
3016 	 * buffers in rx/agg rings to prevent overflowing the completion
3017 	 * ring.
3018 	 */
3019 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3020 
3021 	__bnxt_poll_work_done(bp, bnapi, budget);
3022 	return rx_pkts;
3023 }
3024 
3025 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3026 {
3027 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3028 	struct bnxt *bp = bnapi->bp;
3029 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3030 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3031 	struct tx_cmp *txcmp;
3032 	struct rx_cmp_ext *rxcmp1;
3033 	u32 cp_cons, tmp_raw_cons;
3034 	u32 raw_cons = cpr->cp_raw_cons;
3035 	bool flush_xdp = false;
3036 	u32 rx_pkts = 0;
3037 	u8 event = 0;
3038 
3039 	while (1) {
3040 		int rc;
3041 
3042 		cp_cons = RING_CMP(raw_cons);
3043 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3044 
3045 		if (!TX_CMP_VALID(txcmp, raw_cons))
3046 			break;
3047 
3048 		/* The valid test of the entry must be done first before
3049 		 * reading any further.
3050 		 */
3051 		dma_rmb();
3052 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3053 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3054 			cp_cons = RING_CMP(tmp_raw_cons);
3055 			rxcmp1 = (struct rx_cmp_ext *)
3056 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3057 
3058 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3059 				break;
3060 
3061 			/* force an error to recycle the buffer */
3062 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3063 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3064 
3065 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3066 			if (likely(rc == -EIO) && budget)
3067 				rx_pkts++;
3068 			else if (rc == -EBUSY)	/* partial completion */
3069 				break;
3070 			if (event & BNXT_REDIRECT_EVENT)
3071 				flush_xdp = true;
3072 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3073 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3074 			bnxt_hwrm_handler(bp, txcmp);
3075 		} else {
3076 			netdev_err(bp->dev,
3077 				   "Invalid completion received on special ring\n");
3078 		}
3079 		raw_cons = NEXT_RAW_CMP(raw_cons);
3080 
3081 		if (rx_pkts == budget)
3082 			break;
3083 	}
3084 
3085 	cpr->cp_raw_cons = raw_cons;
3086 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3087 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3088 
3089 	if (event & BNXT_AGG_EVENT)
3090 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3091 	if (flush_xdp)
3092 		xdp_do_flush();
3093 
3094 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3095 		napi_complete_done(napi, rx_pkts);
3096 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3097 	}
3098 	return rx_pkts;
3099 }
3100 
3101 static int bnxt_poll(struct napi_struct *napi, int budget)
3102 {
3103 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3104 	struct bnxt *bp = bnapi->bp;
3105 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3106 	int work_done = 0;
3107 
3108 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3109 		napi_complete(napi);
3110 		return 0;
3111 	}
3112 	while (1) {
3113 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3114 
3115 		if (work_done >= budget) {
3116 			if (!budget)
3117 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3118 			break;
3119 		}
3120 
3121 		if (!bnxt_has_work(bp, cpr)) {
3122 			if (napi_complete_done(napi, work_done))
3123 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3124 			break;
3125 		}
3126 	}
3127 	if (bp->flags & BNXT_FLAG_DIM) {
3128 		struct dim_sample dim_sample = {};
3129 
3130 		dim_update_sample(cpr->event_ctr,
3131 				  cpr->rx_packets,
3132 				  cpr->rx_bytes,
3133 				  &dim_sample);
3134 		net_dim(&cpr->dim, dim_sample);
3135 	}
3136 	return work_done;
3137 }
3138 
3139 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3140 {
3141 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3142 	int i, work_done = 0;
3143 
3144 	for (i = 0; i < cpr->cp_ring_count; i++) {
3145 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3146 
3147 		if (cpr2->had_nqe_notify) {
3148 			work_done += __bnxt_poll_work(bp, cpr2,
3149 						      budget - work_done);
3150 			cpr->has_more_work |= cpr2->has_more_work;
3151 		}
3152 	}
3153 	return work_done;
3154 }
3155 
3156 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3157 				 u64 dbr_type, int budget)
3158 {
3159 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3160 	int i;
3161 
3162 	for (i = 0; i < cpr->cp_ring_count; i++) {
3163 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3164 		struct bnxt_db_info *db;
3165 
3166 		if (cpr2->had_work_done) {
3167 			u32 tgl = 0;
3168 
3169 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3170 				cpr2->had_nqe_notify = 0;
3171 				tgl = cpr2->toggle;
3172 			}
3173 			db = &cpr2->cp_db;
3174 			bnxt_writeq(bp,
3175 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3176 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3177 				    db->doorbell);
3178 			cpr2->had_work_done = 0;
3179 		}
3180 	}
3181 	__bnxt_poll_work_done(bp, bnapi, budget);
3182 }
3183 
3184 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3185 {
3186 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3187 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3188 	struct bnxt_cp_ring_info *cpr_rx;
3189 	u32 raw_cons = cpr->cp_raw_cons;
3190 	struct bnxt *bp = bnapi->bp;
3191 	struct nqe_cn *nqcmp;
3192 	int work_done = 0;
3193 	u32 cons;
3194 
3195 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3196 		napi_complete(napi);
3197 		return 0;
3198 	}
3199 	if (cpr->has_more_work) {
3200 		cpr->has_more_work = 0;
3201 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3202 	}
3203 	while (1) {
3204 		u16 type;
3205 
3206 		cons = RING_CMP(raw_cons);
3207 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3208 
3209 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3210 			if (cpr->has_more_work)
3211 				break;
3212 
3213 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3214 					     budget);
3215 			cpr->cp_raw_cons = raw_cons;
3216 			if (napi_complete_done(napi, work_done))
3217 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3218 						  cpr->cp_raw_cons);
3219 			goto poll_done;
3220 		}
3221 
3222 		/* The valid test of the entry must be done first before
3223 		 * reading any further.
3224 		 */
3225 		dma_rmb();
3226 
3227 		type = le16_to_cpu(nqcmp->type);
3228 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3229 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3230 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3231 			struct bnxt_cp_ring_info *cpr2;
3232 
3233 			/* No more budget for RX work */
3234 			if (budget && work_done >= budget &&
3235 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3236 				break;
3237 
3238 			idx = BNXT_NQ_HDL_IDX(idx);
3239 			cpr2 = &cpr->cp_ring_arr[idx];
3240 			cpr2->had_nqe_notify = 1;
3241 			cpr2->toggle = NQE_CN_TOGGLE(type);
3242 			work_done += __bnxt_poll_work(bp, cpr2,
3243 						      budget - work_done);
3244 			cpr->has_more_work |= cpr2->has_more_work;
3245 		} else {
3246 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3247 		}
3248 		raw_cons = NEXT_RAW_CMP(raw_cons);
3249 	}
3250 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3251 	if (raw_cons != cpr->cp_raw_cons) {
3252 		cpr->cp_raw_cons = raw_cons;
3253 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3254 	}
3255 poll_done:
3256 	cpr_rx = &cpr->cp_ring_arr[0];
3257 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3258 	    (bp->flags & BNXT_FLAG_DIM)) {
3259 		struct dim_sample dim_sample = {};
3260 
3261 		dim_update_sample(cpr->event_ctr,
3262 				  cpr_rx->rx_packets,
3263 				  cpr_rx->rx_bytes,
3264 				  &dim_sample);
3265 		net_dim(&cpr->dim, dim_sample);
3266 	}
3267 	return work_done;
3268 }
3269 
3270 static void bnxt_free_tx_skbs(struct bnxt *bp)
3271 {
3272 	int i, max_idx;
3273 	struct pci_dev *pdev = bp->pdev;
3274 
3275 	if (!bp->tx_ring)
3276 		return;
3277 
3278 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3279 	for (i = 0; i < bp->tx_nr_rings; i++) {
3280 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3281 		int j;
3282 
3283 		if (!txr->tx_buf_ring)
3284 			continue;
3285 
3286 		for (j = 0; j < max_idx;) {
3287 			struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
3288 			struct sk_buff *skb;
3289 			int k, last;
3290 
3291 			if (i < bp->tx_nr_rings_xdp &&
3292 			    tx_buf->action == XDP_REDIRECT) {
3293 				dma_unmap_single(&pdev->dev,
3294 					dma_unmap_addr(tx_buf, mapping),
3295 					dma_unmap_len(tx_buf, len),
3296 					DMA_TO_DEVICE);
3297 				xdp_return_frame(tx_buf->xdpf);
3298 				tx_buf->action = 0;
3299 				tx_buf->xdpf = NULL;
3300 				j++;
3301 				continue;
3302 			}
3303 
3304 			skb = tx_buf->skb;
3305 			if (!skb) {
3306 				j++;
3307 				continue;
3308 			}
3309 
3310 			tx_buf->skb = NULL;
3311 
3312 			if (tx_buf->is_push) {
3313 				dev_kfree_skb(skb);
3314 				j += 2;
3315 				continue;
3316 			}
3317 
3318 			dma_unmap_single(&pdev->dev,
3319 					 dma_unmap_addr(tx_buf, mapping),
3320 					 skb_headlen(skb),
3321 					 DMA_TO_DEVICE);
3322 
3323 			last = tx_buf->nr_frags;
3324 			j += 2;
3325 			for (k = 0; k < last; k++, j++) {
3326 				int ring_idx = j & bp->tx_ring_mask;
3327 				skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
3328 
3329 				tx_buf = &txr->tx_buf_ring[ring_idx];
3330 				dma_unmap_page(
3331 					&pdev->dev,
3332 					dma_unmap_addr(tx_buf, mapping),
3333 					skb_frag_size(frag), DMA_TO_DEVICE);
3334 			}
3335 			dev_kfree_skb(skb);
3336 		}
3337 		netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
3338 	}
3339 }
3340 
3341 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3342 {
3343 	struct pci_dev *pdev = bp->pdev;
3344 	int i, max_idx;
3345 
3346 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3347 
3348 	for (i = 0; i < max_idx; i++) {
3349 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3350 		dma_addr_t mapping = rx_buf->mapping;
3351 		void *data = rx_buf->data;
3352 
3353 		if (!data)
3354 			continue;
3355 
3356 		rx_buf->data = NULL;
3357 		if (BNXT_RX_PAGE_MODE(bp)) {
3358 			page_pool_recycle_direct(rxr->page_pool, data);
3359 		} else {
3360 			dma_unmap_single_attrs(&pdev->dev, mapping,
3361 					       bp->rx_buf_use_size, bp->rx_dir,
3362 					       DMA_ATTR_WEAK_ORDERING);
3363 			skb_free_frag(data);
3364 		}
3365 	}
3366 }
3367 
3368 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3369 {
3370 	int i, max_idx;
3371 
3372 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3373 
3374 	for (i = 0; i < max_idx; i++) {
3375 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3376 		struct page *page = rx_agg_buf->page;
3377 
3378 		if (!page)
3379 			continue;
3380 
3381 		rx_agg_buf->page = NULL;
3382 		__clear_bit(i, rxr->rx_agg_bmap);
3383 
3384 		page_pool_recycle_direct(rxr->page_pool, page);
3385 	}
3386 }
3387 
3388 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, int ring_nr)
3389 {
3390 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
3391 	struct pci_dev *pdev = bp->pdev;
3392 	struct bnxt_tpa_idx_map *map;
3393 	int i;
3394 
3395 	if (!rxr->rx_tpa)
3396 		goto skip_rx_tpa_free;
3397 
3398 	for (i = 0; i < bp->max_tpa; i++) {
3399 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3400 		u8 *data = tpa_info->data;
3401 
3402 		if (!data)
3403 			continue;
3404 
3405 		dma_unmap_single_attrs(&pdev->dev, tpa_info->mapping,
3406 				       bp->rx_buf_use_size, bp->rx_dir,
3407 				       DMA_ATTR_WEAK_ORDERING);
3408 
3409 		tpa_info->data = NULL;
3410 
3411 		skb_free_frag(data);
3412 	}
3413 
3414 skip_rx_tpa_free:
3415 	if (!rxr->rx_buf_ring)
3416 		goto skip_rx_buf_free;
3417 
3418 	bnxt_free_one_rx_ring(bp, rxr);
3419 
3420 skip_rx_buf_free:
3421 	if (!rxr->rx_agg_ring)
3422 		goto skip_rx_agg_free;
3423 
3424 	bnxt_free_one_rx_agg_ring(bp, rxr);
3425 
3426 skip_rx_agg_free:
3427 	map = rxr->rx_tpa_idx_map;
3428 	if (map)
3429 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3430 }
3431 
3432 static void bnxt_free_rx_skbs(struct bnxt *bp)
3433 {
3434 	int i;
3435 
3436 	if (!bp->rx_ring)
3437 		return;
3438 
3439 	for (i = 0; i < bp->rx_nr_rings; i++)
3440 		bnxt_free_one_rx_ring_skbs(bp, i);
3441 }
3442 
3443 static void bnxt_free_skbs(struct bnxt *bp)
3444 {
3445 	bnxt_free_tx_skbs(bp);
3446 	bnxt_free_rx_skbs(bp);
3447 }
3448 
3449 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3450 {
3451 	u8 init_val = ctxm->init_value;
3452 	u16 offset = ctxm->init_offset;
3453 	u8 *p2 = p;
3454 	int i;
3455 
3456 	if (!init_val)
3457 		return;
3458 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3459 		memset(p, init_val, len);
3460 		return;
3461 	}
3462 	for (i = 0; i < len; i += ctxm->entry_size)
3463 		*(p2 + i + offset) = init_val;
3464 }
3465 
3466 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3467 {
3468 	struct pci_dev *pdev = bp->pdev;
3469 	int i;
3470 
3471 	if (!rmem->pg_arr)
3472 		goto skip_pages;
3473 
3474 	for (i = 0; i < rmem->nr_pages; i++) {
3475 		if (!rmem->pg_arr[i])
3476 			continue;
3477 
3478 		dma_free_coherent(&pdev->dev, rmem->page_size,
3479 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3480 
3481 		rmem->pg_arr[i] = NULL;
3482 	}
3483 skip_pages:
3484 	if (rmem->pg_tbl) {
3485 		size_t pg_tbl_size = rmem->nr_pages * 8;
3486 
3487 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3488 			pg_tbl_size = rmem->page_size;
3489 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3490 				  rmem->pg_tbl, rmem->pg_tbl_map);
3491 		rmem->pg_tbl = NULL;
3492 	}
3493 	if (rmem->vmem_size && *rmem->vmem) {
3494 		vfree(*rmem->vmem);
3495 		*rmem->vmem = NULL;
3496 	}
3497 }
3498 
3499 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3500 {
3501 	struct pci_dev *pdev = bp->pdev;
3502 	u64 valid_bit = 0;
3503 	int i;
3504 
3505 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3506 		valid_bit = PTU_PTE_VALID;
3507 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3508 		size_t pg_tbl_size = rmem->nr_pages * 8;
3509 
3510 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3511 			pg_tbl_size = rmem->page_size;
3512 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3513 						  &rmem->pg_tbl_map,
3514 						  GFP_KERNEL);
3515 		if (!rmem->pg_tbl)
3516 			return -ENOMEM;
3517 	}
3518 
3519 	for (i = 0; i < rmem->nr_pages; i++) {
3520 		u64 extra_bits = valid_bit;
3521 
3522 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3523 						     rmem->page_size,
3524 						     &rmem->dma_arr[i],
3525 						     GFP_KERNEL);
3526 		if (!rmem->pg_arr[i])
3527 			return -ENOMEM;
3528 
3529 		if (rmem->ctx_mem)
3530 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3531 					  rmem->page_size);
3532 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3533 			if (i == rmem->nr_pages - 2 &&
3534 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3535 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3536 			else if (i == rmem->nr_pages - 1 &&
3537 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3538 				extra_bits |= PTU_PTE_LAST;
3539 			rmem->pg_tbl[i] =
3540 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3541 		}
3542 	}
3543 
3544 	if (rmem->vmem_size) {
3545 		*rmem->vmem = vzalloc(rmem->vmem_size);
3546 		if (!(*rmem->vmem))
3547 			return -ENOMEM;
3548 	}
3549 	return 0;
3550 }
3551 
3552 static void bnxt_free_tpa_info(struct bnxt *bp)
3553 {
3554 	int i, j;
3555 
3556 	for (i = 0; i < bp->rx_nr_rings; i++) {
3557 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3558 
3559 		kfree(rxr->rx_tpa_idx_map);
3560 		rxr->rx_tpa_idx_map = NULL;
3561 		if (rxr->rx_tpa) {
3562 			for (j = 0; j < bp->max_tpa; j++) {
3563 				kfree(rxr->rx_tpa[j].agg_arr);
3564 				rxr->rx_tpa[j].agg_arr = NULL;
3565 			}
3566 		}
3567 		kfree(rxr->rx_tpa);
3568 		rxr->rx_tpa = NULL;
3569 	}
3570 }
3571 
3572 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3573 {
3574 	int i, j;
3575 
3576 	bp->max_tpa = MAX_TPA;
3577 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3578 		if (!bp->max_tpa_v2)
3579 			return 0;
3580 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3581 	}
3582 
3583 	for (i = 0; i < bp->rx_nr_rings; i++) {
3584 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3585 		struct rx_agg_cmp *agg;
3586 
3587 		rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3588 				      GFP_KERNEL);
3589 		if (!rxr->rx_tpa)
3590 			return -ENOMEM;
3591 
3592 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3593 			continue;
3594 		for (j = 0; j < bp->max_tpa; j++) {
3595 			agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3596 			if (!agg)
3597 				return -ENOMEM;
3598 			rxr->rx_tpa[j].agg_arr = agg;
3599 		}
3600 		rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3601 					      GFP_KERNEL);
3602 		if (!rxr->rx_tpa_idx_map)
3603 			return -ENOMEM;
3604 	}
3605 	return 0;
3606 }
3607 
3608 static void bnxt_free_rx_rings(struct bnxt *bp)
3609 {
3610 	int i;
3611 
3612 	if (!bp->rx_ring)
3613 		return;
3614 
3615 	bnxt_free_tpa_info(bp);
3616 	for (i = 0; i < bp->rx_nr_rings; i++) {
3617 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3618 		struct bnxt_ring_struct *ring;
3619 
3620 		if (rxr->xdp_prog)
3621 			bpf_prog_put(rxr->xdp_prog);
3622 
3623 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3624 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3625 
3626 		page_pool_destroy(rxr->page_pool);
3627 		rxr->page_pool = NULL;
3628 
3629 		kfree(rxr->rx_agg_bmap);
3630 		rxr->rx_agg_bmap = NULL;
3631 
3632 		ring = &rxr->rx_ring_struct;
3633 		bnxt_free_ring(bp, &ring->ring_mem);
3634 
3635 		ring = &rxr->rx_agg_ring_struct;
3636 		bnxt_free_ring(bp, &ring->ring_mem);
3637 	}
3638 }
3639 
3640 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3641 				   struct bnxt_rx_ring_info *rxr,
3642 				   int numa_node)
3643 {
3644 	struct page_pool_params pp = { 0 };
3645 
3646 	pp.pool_size = bp->rx_agg_ring_size;
3647 	if (BNXT_RX_PAGE_MODE(bp))
3648 		pp.pool_size += bp->rx_ring_size;
3649 	pp.nid = numa_node;
3650 	pp.napi = &rxr->bnapi->napi;
3651 	pp.netdev = bp->dev;
3652 	pp.dev = &bp->pdev->dev;
3653 	pp.dma_dir = bp->rx_dir;
3654 	pp.max_len = PAGE_SIZE;
3655 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3656 
3657 	rxr->page_pool = page_pool_create(&pp);
3658 	if (IS_ERR(rxr->page_pool)) {
3659 		int err = PTR_ERR(rxr->page_pool);
3660 
3661 		rxr->page_pool = NULL;
3662 		return err;
3663 	}
3664 	return 0;
3665 }
3666 
3667 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3668 {
3669 	int numa_node = dev_to_node(&bp->pdev->dev);
3670 	int i, rc = 0, agg_rings = 0, cpu;
3671 
3672 	if (!bp->rx_ring)
3673 		return -ENOMEM;
3674 
3675 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3676 		agg_rings = 1;
3677 
3678 	for (i = 0; i < bp->rx_nr_rings; i++) {
3679 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3680 		struct bnxt_ring_struct *ring;
3681 		int cpu_node;
3682 
3683 		ring = &rxr->rx_ring_struct;
3684 
3685 		cpu = cpumask_local_spread(i, numa_node);
3686 		cpu_node = cpu_to_node(cpu);
3687 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3688 			   i, cpu_node);
3689 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3690 		if (rc)
3691 			return rc;
3692 
3693 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3694 		if (rc < 0)
3695 			return rc;
3696 
3697 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3698 						MEM_TYPE_PAGE_POOL,
3699 						rxr->page_pool);
3700 		if (rc) {
3701 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3702 			return rc;
3703 		}
3704 
3705 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3706 		if (rc)
3707 			return rc;
3708 
3709 		ring->grp_idx = i;
3710 		if (agg_rings) {
3711 			u16 mem_size;
3712 
3713 			ring = &rxr->rx_agg_ring_struct;
3714 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3715 			if (rc)
3716 				return rc;
3717 
3718 			ring->grp_idx = i;
3719 			rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3720 			mem_size = rxr->rx_agg_bmap_size / 8;
3721 			rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3722 			if (!rxr->rx_agg_bmap)
3723 				return -ENOMEM;
3724 		}
3725 	}
3726 	if (bp->flags & BNXT_FLAG_TPA)
3727 		rc = bnxt_alloc_tpa_info(bp);
3728 	return rc;
3729 }
3730 
3731 static void bnxt_free_tx_rings(struct bnxt *bp)
3732 {
3733 	int i;
3734 	struct pci_dev *pdev = bp->pdev;
3735 
3736 	if (!bp->tx_ring)
3737 		return;
3738 
3739 	for (i = 0; i < bp->tx_nr_rings; i++) {
3740 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3741 		struct bnxt_ring_struct *ring;
3742 
3743 		if (txr->tx_push) {
3744 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3745 					  txr->tx_push, txr->tx_push_mapping);
3746 			txr->tx_push = NULL;
3747 		}
3748 
3749 		ring = &txr->tx_ring_struct;
3750 
3751 		bnxt_free_ring(bp, &ring->ring_mem);
3752 	}
3753 }
3754 
3755 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3756 	((tc) * (bp)->tx_nr_rings_per_tc)
3757 
3758 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3759 	((tx) % (bp)->tx_nr_rings_per_tc)
3760 
3761 #define BNXT_RING_TO_TC(bp, tx)		\
3762 	((tx) / (bp)->tx_nr_rings_per_tc)
3763 
3764 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3765 {
3766 	int i, j, rc;
3767 	struct pci_dev *pdev = bp->pdev;
3768 
3769 	bp->tx_push_size = 0;
3770 	if (bp->tx_push_thresh) {
3771 		int push_size;
3772 
3773 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3774 					bp->tx_push_thresh);
3775 
3776 		if (push_size > 256) {
3777 			push_size = 0;
3778 			bp->tx_push_thresh = 0;
3779 		}
3780 
3781 		bp->tx_push_size = push_size;
3782 	}
3783 
3784 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3785 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3786 		struct bnxt_ring_struct *ring;
3787 		u8 qidx;
3788 
3789 		ring = &txr->tx_ring_struct;
3790 
3791 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3792 		if (rc)
3793 			return rc;
3794 
3795 		ring->grp_idx = txr->bnapi->index;
3796 		if (bp->tx_push_size) {
3797 			dma_addr_t mapping;
3798 
3799 			/* One pre-allocated DMA buffer to backup
3800 			 * TX push operation
3801 			 */
3802 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3803 						bp->tx_push_size,
3804 						&txr->tx_push_mapping,
3805 						GFP_KERNEL);
3806 
3807 			if (!txr->tx_push)
3808 				return -ENOMEM;
3809 
3810 			mapping = txr->tx_push_mapping +
3811 				sizeof(struct tx_push_bd);
3812 			txr->data_mapping = cpu_to_le64(mapping);
3813 		}
3814 		qidx = bp->tc_to_qidx[j];
3815 		ring->queue_id = bp->q_info[qidx].queue_id;
3816 		spin_lock_init(&txr->xdp_tx_lock);
3817 		if (i < bp->tx_nr_rings_xdp)
3818 			continue;
3819 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
3820 			j++;
3821 	}
3822 	return 0;
3823 }
3824 
3825 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
3826 {
3827 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3828 
3829 	kfree(cpr->cp_desc_ring);
3830 	cpr->cp_desc_ring = NULL;
3831 	ring->ring_mem.pg_arr = NULL;
3832 	kfree(cpr->cp_desc_mapping);
3833 	cpr->cp_desc_mapping = NULL;
3834 	ring->ring_mem.dma_arr = NULL;
3835 }
3836 
3837 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
3838 {
3839 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
3840 	if (!cpr->cp_desc_ring)
3841 		return -ENOMEM;
3842 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
3843 				       GFP_KERNEL);
3844 	if (!cpr->cp_desc_mapping)
3845 		return -ENOMEM;
3846 	return 0;
3847 }
3848 
3849 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
3850 {
3851 	int i;
3852 
3853 	if (!bp->bnapi)
3854 		return;
3855 	for (i = 0; i < bp->cp_nr_rings; i++) {
3856 		struct bnxt_napi *bnapi = bp->bnapi[i];
3857 
3858 		if (!bnapi)
3859 			continue;
3860 		bnxt_free_cp_arrays(&bnapi->cp_ring);
3861 	}
3862 }
3863 
3864 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
3865 {
3866 	int i, n = bp->cp_nr_pages;
3867 
3868 	for (i = 0; i < bp->cp_nr_rings; i++) {
3869 		struct bnxt_napi *bnapi = bp->bnapi[i];
3870 		int rc;
3871 
3872 		if (!bnapi)
3873 			continue;
3874 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
3875 		if (rc)
3876 			return rc;
3877 	}
3878 	return 0;
3879 }
3880 
3881 static void bnxt_free_cp_rings(struct bnxt *bp)
3882 {
3883 	int i;
3884 
3885 	if (!bp->bnapi)
3886 		return;
3887 
3888 	for (i = 0; i < bp->cp_nr_rings; i++) {
3889 		struct bnxt_napi *bnapi = bp->bnapi[i];
3890 		struct bnxt_cp_ring_info *cpr;
3891 		struct bnxt_ring_struct *ring;
3892 		int j;
3893 
3894 		if (!bnapi)
3895 			continue;
3896 
3897 		cpr = &bnapi->cp_ring;
3898 		ring = &cpr->cp_ring_struct;
3899 
3900 		bnxt_free_ring(bp, &ring->ring_mem);
3901 
3902 		if (!cpr->cp_ring_arr)
3903 			continue;
3904 
3905 		for (j = 0; j < cpr->cp_ring_count; j++) {
3906 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
3907 
3908 			ring = &cpr2->cp_ring_struct;
3909 			bnxt_free_ring(bp, &ring->ring_mem);
3910 			bnxt_free_cp_arrays(cpr2);
3911 		}
3912 		kfree(cpr->cp_ring_arr);
3913 		cpr->cp_ring_arr = NULL;
3914 		cpr->cp_ring_count = 0;
3915 	}
3916 }
3917 
3918 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
3919 				  struct bnxt_cp_ring_info *cpr)
3920 {
3921 	struct bnxt_ring_mem_info *rmem;
3922 	struct bnxt_ring_struct *ring;
3923 	int rc;
3924 
3925 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
3926 	if (rc) {
3927 		bnxt_free_cp_arrays(cpr);
3928 		return -ENOMEM;
3929 	}
3930 	ring = &cpr->cp_ring_struct;
3931 	rmem = &ring->ring_mem;
3932 	rmem->nr_pages = bp->cp_nr_pages;
3933 	rmem->page_size = HW_CMPD_RING_SIZE;
3934 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
3935 	rmem->dma_arr = cpr->cp_desc_mapping;
3936 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
3937 	rc = bnxt_alloc_ring(bp, rmem);
3938 	if (rc) {
3939 		bnxt_free_ring(bp, rmem);
3940 		bnxt_free_cp_arrays(cpr);
3941 	}
3942 	return rc;
3943 }
3944 
3945 static int bnxt_alloc_cp_rings(struct bnxt *bp)
3946 {
3947 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
3948 	int i, j, rc, ulp_msix;
3949 	int tcs = bp->num_tc;
3950 
3951 	if (!tcs)
3952 		tcs = 1;
3953 	ulp_msix = bnxt_get_ulp_msix_num(bp);
3954 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
3955 		struct bnxt_napi *bnapi = bp->bnapi[i];
3956 		struct bnxt_cp_ring_info *cpr, *cpr2;
3957 		struct bnxt_ring_struct *ring;
3958 		int cp_count = 0, k;
3959 		int rx = 0, tx = 0;
3960 
3961 		if (!bnapi)
3962 			continue;
3963 
3964 		cpr = &bnapi->cp_ring;
3965 		cpr->bnapi = bnapi;
3966 		ring = &cpr->cp_ring_struct;
3967 
3968 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3969 		if (rc)
3970 			return rc;
3971 
3972 		ring->map_idx = ulp_msix + i;
3973 
3974 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3975 			continue;
3976 
3977 		if (i < bp->rx_nr_rings) {
3978 			cp_count++;
3979 			rx = 1;
3980 		}
3981 		if (i < bp->tx_nr_rings_xdp) {
3982 			cp_count++;
3983 			tx = 1;
3984 		} else if ((sh && i < bp->tx_nr_rings) ||
3985 			 (!sh && i >= bp->rx_nr_rings)) {
3986 			cp_count += tcs;
3987 			tx = 1;
3988 		}
3989 
3990 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
3991 					   GFP_KERNEL);
3992 		if (!cpr->cp_ring_arr)
3993 			return -ENOMEM;
3994 		cpr->cp_ring_count = cp_count;
3995 
3996 		for (k = 0; k < cp_count; k++) {
3997 			cpr2 = &cpr->cp_ring_arr[k];
3998 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
3999 			if (rc)
4000 				return rc;
4001 			cpr2->bnapi = bnapi;
4002 			cpr2->sw_stats = cpr->sw_stats;
4003 			cpr2->cp_idx = k;
4004 			if (!k && rx) {
4005 				bp->rx_ring[i].rx_cpr = cpr2;
4006 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4007 			} else {
4008 				int n, tc = k - rx;
4009 
4010 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4011 				bp->tx_ring[n].tx_cpr = cpr2;
4012 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4013 			}
4014 		}
4015 		if (tx)
4016 			j++;
4017 	}
4018 	return 0;
4019 }
4020 
4021 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4022 				     struct bnxt_rx_ring_info *rxr)
4023 {
4024 	struct bnxt_ring_mem_info *rmem;
4025 	struct bnxt_ring_struct *ring;
4026 
4027 	ring = &rxr->rx_ring_struct;
4028 	rmem = &ring->ring_mem;
4029 	rmem->nr_pages = bp->rx_nr_pages;
4030 	rmem->page_size = HW_RXBD_RING_SIZE;
4031 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4032 	rmem->dma_arr = rxr->rx_desc_mapping;
4033 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4034 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4035 
4036 	ring = &rxr->rx_agg_ring_struct;
4037 	rmem = &ring->ring_mem;
4038 	rmem->nr_pages = bp->rx_agg_nr_pages;
4039 	rmem->page_size = HW_RXBD_RING_SIZE;
4040 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4041 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4042 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4043 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4044 }
4045 
4046 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4047 				      struct bnxt_rx_ring_info *rxr)
4048 {
4049 	struct bnxt_ring_mem_info *rmem;
4050 	struct bnxt_ring_struct *ring;
4051 	int i;
4052 
4053 	rxr->page_pool->p.napi = NULL;
4054 	rxr->page_pool = NULL;
4055 
4056 	ring = &rxr->rx_ring_struct;
4057 	rmem = &ring->ring_mem;
4058 	rmem->pg_tbl = NULL;
4059 	rmem->pg_tbl_map = 0;
4060 	for (i = 0; i < rmem->nr_pages; i++) {
4061 		rmem->pg_arr[i] = NULL;
4062 		rmem->dma_arr[i] = 0;
4063 	}
4064 	*rmem->vmem = NULL;
4065 
4066 	ring = &rxr->rx_agg_ring_struct;
4067 	rmem = &ring->ring_mem;
4068 	rmem->pg_tbl = NULL;
4069 	rmem->pg_tbl_map = 0;
4070 	for (i = 0; i < rmem->nr_pages; i++) {
4071 		rmem->pg_arr[i] = NULL;
4072 		rmem->dma_arr[i] = 0;
4073 	}
4074 	*rmem->vmem = NULL;
4075 }
4076 
4077 static void bnxt_init_ring_struct(struct bnxt *bp)
4078 {
4079 	int i, j;
4080 
4081 	for (i = 0; i < bp->cp_nr_rings; i++) {
4082 		struct bnxt_napi *bnapi = bp->bnapi[i];
4083 		struct bnxt_ring_mem_info *rmem;
4084 		struct bnxt_cp_ring_info *cpr;
4085 		struct bnxt_rx_ring_info *rxr;
4086 		struct bnxt_tx_ring_info *txr;
4087 		struct bnxt_ring_struct *ring;
4088 
4089 		if (!bnapi)
4090 			continue;
4091 
4092 		cpr = &bnapi->cp_ring;
4093 		ring = &cpr->cp_ring_struct;
4094 		rmem = &ring->ring_mem;
4095 		rmem->nr_pages = bp->cp_nr_pages;
4096 		rmem->page_size = HW_CMPD_RING_SIZE;
4097 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4098 		rmem->dma_arr = cpr->cp_desc_mapping;
4099 		rmem->vmem_size = 0;
4100 
4101 		rxr = bnapi->rx_ring;
4102 		if (!rxr)
4103 			goto skip_rx;
4104 
4105 		ring = &rxr->rx_ring_struct;
4106 		rmem = &ring->ring_mem;
4107 		rmem->nr_pages = bp->rx_nr_pages;
4108 		rmem->page_size = HW_RXBD_RING_SIZE;
4109 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4110 		rmem->dma_arr = rxr->rx_desc_mapping;
4111 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4112 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4113 
4114 		ring = &rxr->rx_agg_ring_struct;
4115 		rmem = &ring->ring_mem;
4116 		rmem->nr_pages = bp->rx_agg_nr_pages;
4117 		rmem->page_size = HW_RXBD_RING_SIZE;
4118 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4119 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4120 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4121 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4122 
4123 skip_rx:
4124 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4125 			ring = &txr->tx_ring_struct;
4126 			rmem = &ring->ring_mem;
4127 			rmem->nr_pages = bp->tx_nr_pages;
4128 			rmem->page_size = HW_TXBD_RING_SIZE;
4129 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4130 			rmem->dma_arr = txr->tx_desc_mapping;
4131 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4132 			rmem->vmem = (void **)&txr->tx_buf_ring;
4133 		}
4134 	}
4135 }
4136 
4137 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4138 {
4139 	int i;
4140 	u32 prod;
4141 	struct rx_bd **rx_buf_ring;
4142 
4143 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4144 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4145 		int j;
4146 		struct rx_bd *rxbd;
4147 
4148 		rxbd = rx_buf_ring[i];
4149 		if (!rxbd)
4150 			continue;
4151 
4152 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4153 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4154 			rxbd->rx_bd_opaque = prod;
4155 		}
4156 	}
4157 }
4158 
4159 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4160 				       struct bnxt_rx_ring_info *rxr,
4161 				       int ring_nr)
4162 {
4163 	u32 prod;
4164 	int i;
4165 
4166 	prod = rxr->rx_prod;
4167 	for (i = 0; i < bp->rx_ring_size; i++) {
4168 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4169 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4170 				    ring_nr, i, bp->rx_ring_size);
4171 			break;
4172 		}
4173 		prod = NEXT_RX(prod);
4174 	}
4175 	rxr->rx_prod = prod;
4176 }
4177 
4178 static void bnxt_alloc_one_rx_ring_page(struct bnxt *bp,
4179 					struct bnxt_rx_ring_info *rxr,
4180 					int ring_nr)
4181 {
4182 	u32 prod;
4183 	int i;
4184 
4185 	prod = rxr->rx_agg_prod;
4186 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4187 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
4188 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4189 				    ring_nr, i, bp->rx_ring_size);
4190 			break;
4191 		}
4192 		prod = NEXT_RX_AGG(prod);
4193 	}
4194 	rxr->rx_agg_prod = prod;
4195 }
4196 
4197 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4198 {
4199 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4200 	int i;
4201 
4202 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4203 
4204 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4205 		return 0;
4206 
4207 	bnxt_alloc_one_rx_ring_page(bp, rxr, ring_nr);
4208 
4209 	if (rxr->rx_tpa) {
4210 		dma_addr_t mapping;
4211 		u8 *data;
4212 
4213 		for (i = 0; i < bp->max_tpa; i++) {
4214 			data = __bnxt_alloc_rx_frag(bp, &mapping, GFP_KERNEL);
4215 			if (!data)
4216 				return -ENOMEM;
4217 
4218 			rxr->rx_tpa[i].data = data;
4219 			rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4220 			rxr->rx_tpa[i].mapping = mapping;
4221 		}
4222 	}
4223 	return 0;
4224 }
4225 
4226 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4227 				       struct bnxt_rx_ring_info *rxr)
4228 {
4229 	struct bnxt_ring_struct *ring;
4230 	u32 type;
4231 
4232 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4233 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4234 
4235 	if (NET_IP_ALIGN == 2)
4236 		type |= RX_BD_FLAGS_SOP;
4237 
4238 	ring = &rxr->rx_ring_struct;
4239 	bnxt_init_rxbd_pages(ring, type);
4240 	ring->fw_ring_id = INVALID_HW_RING_ID;
4241 }
4242 
4243 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4244 					   struct bnxt_rx_ring_info *rxr)
4245 {
4246 	struct bnxt_ring_struct *ring;
4247 	u32 type;
4248 
4249 	ring = &rxr->rx_agg_ring_struct;
4250 	ring->fw_ring_id = INVALID_HW_RING_ID;
4251 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4252 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4253 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4254 
4255 		bnxt_init_rxbd_pages(ring, type);
4256 	}
4257 }
4258 
4259 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4260 {
4261 	struct bnxt_rx_ring_info *rxr;
4262 
4263 	rxr = &bp->rx_ring[ring_nr];
4264 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4265 
4266 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4267 			     &rxr->bnapi->napi);
4268 
4269 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4270 		bpf_prog_add(bp->xdp_prog, 1);
4271 		rxr->xdp_prog = bp->xdp_prog;
4272 	}
4273 
4274 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4275 
4276 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4277 }
4278 
4279 static void bnxt_init_cp_rings(struct bnxt *bp)
4280 {
4281 	int i, j;
4282 
4283 	for (i = 0; i < bp->cp_nr_rings; i++) {
4284 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4285 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4286 
4287 		ring->fw_ring_id = INVALID_HW_RING_ID;
4288 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4289 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4290 		if (!cpr->cp_ring_arr)
4291 			continue;
4292 		for (j = 0; j < cpr->cp_ring_count; j++) {
4293 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4294 
4295 			ring = &cpr2->cp_ring_struct;
4296 			ring->fw_ring_id = INVALID_HW_RING_ID;
4297 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4298 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4299 		}
4300 	}
4301 }
4302 
4303 static int bnxt_init_rx_rings(struct bnxt *bp)
4304 {
4305 	int i, rc = 0;
4306 
4307 	if (BNXT_RX_PAGE_MODE(bp)) {
4308 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4309 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4310 	} else {
4311 		bp->rx_offset = BNXT_RX_OFFSET;
4312 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4313 	}
4314 
4315 	for (i = 0; i < bp->rx_nr_rings; i++) {
4316 		rc = bnxt_init_one_rx_ring(bp, i);
4317 		if (rc)
4318 			break;
4319 	}
4320 
4321 	return rc;
4322 }
4323 
4324 static int bnxt_init_tx_rings(struct bnxt *bp)
4325 {
4326 	u16 i;
4327 
4328 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4329 				   BNXT_MIN_TX_DESC_CNT);
4330 
4331 	for (i = 0; i < bp->tx_nr_rings; i++) {
4332 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4333 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4334 
4335 		ring->fw_ring_id = INVALID_HW_RING_ID;
4336 
4337 		if (i >= bp->tx_nr_rings_xdp)
4338 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4339 					     NETDEV_QUEUE_TYPE_TX,
4340 					     &txr->bnapi->napi);
4341 	}
4342 
4343 	return 0;
4344 }
4345 
4346 static void bnxt_free_ring_grps(struct bnxt *bp)
4347 {
4348 	kfree(bp->grp_info);
4349 	bp->grp_info = NULL;
4350 }
4351 
4352 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4353 {
4354 	int i;
4355 
4356 	if (irq_re_init) {
4357 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4358 				       sizeof(struct bnxt_ring_grp_info),
4359 				       GFP_KERNEL);
4360 		if (!bp->grp_info)
4361 			return -ENOMEM;
4362 	}
4363 	for (i = 0; i < bp->cp_nr_rings; i++) {
4364 		if (irq_re_init)
4365 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4366 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4367 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4368 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4369 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4370 	}
4371 	return 0;
4372 }
4373 
4374 static void bnxt_free_vnics(struct bnxt *bp)
4375 {
4376 	kfree(bp->vnic_info);
4377 	bp->vnic_info = NULL;
4378 	bp->nr_vnics = 0;
4379 }
4380 
4381 static int bnxt_alloc_vnics(struct bnxt *bp)
4382 {
4383 	int num_vnics = 1;
4384 
4385 #ifdef CONFIG_RFS_ACCEL
4386 	if (bp->flags & BNXT_FLAG_RFS) {
4387 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4388 			num_vnics++;
4389 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4390 			num_vnics += bp->rx_nr_rings;
4391 	}
4392 #endif
4393 
4394 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4395 		num_vnics++;
4396 
4397 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4398 				GFP_KERNEL);
4399 	if (!bp->vnic_info)
4400 		return -ENOMEM;
4401 
4402 	bp->nr_vnics = num_vnics;
4403 	return 0;
4404 }
4405 
4406 static void bnxt_init_vnics(struct bnxt *bp)
4407 {
4408 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4409 	int i;
4410 
4411 	for (i = 0; i < bp->nr_vnics; i++) {
4412 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4413 		int j;
4414 
4415 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4416 		vnic->vnic_id = i;
4417 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4418 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4419 
4420 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4421 
4422 		if (bp->vnic_info[i].rss_hash_key) {
4423 			if (i == BNXT_VNIC_DEFAULT) {
4424 				u8 *key = (void *)vnic->rss_hash_key;
4425 				int k;
4426 
4427 				if (!bp->rss_hash_key_valid &&
4428 				    !bp->rss_hash_key_updated) {
4429 					get_random_bytes(bp->rss_hash_key,
4430 							 HW_HASH_KEY_SIZE);
4431 					bp->rss_hash_key_updated = true;
4432 				}
4433 
4434 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4435 				       HW_HASH_KEY_SIZE);
4436 
4437 				if (!bp->rss_hash_key_updated)
4438 					continue;
4439 
4440 				bp->rss_hash_key_updated = false;
4441 				bp->rss_hash_key_valid = true;
4442 
4443 				bp->toeplitz_prefix = 0;
4444 				for (k = 0; k < 8; k++) {
4445 					bp->toeplitz_prefix <<= 8;
4446 					bp->toeplitz_prefix |= key[k];
4447 				}
4448 			} else {
4449 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4450 				       HW_HASH_KEY_SIZE);
4451 			}
4452 		}
4453 	}
4454 }
4455 
4456 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4457 {
4458 	int pages;
4459 
4460 	pages = ring_size / desc_per_pg;
4461 
4462 	if (!pages)
4463 		return 1;
4464 
4465 	pages++;
4466 
4467 	while (pages & (pages - 1))
4468 		pages++;
4469 
4470 	return pages;
4471 }
4472 
4473 void bnxt_set_tpa_flags(struct bnxt *bp)
4474 {
4475 	bp->flags &= ~BNXT_FLAG_TPA;
4476 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4477 		return;
4478 	if (bp->dev->features & NETIF_F_LRO)
4479 		bp->flags |= BNXT_FLAG_LRO;
4480 	else if (bp->dev->features & NETIF_F_GRO_HW)
4481 		bp->flags |= BNXT_FLAG_GRO;
4482 }
4483 
4484 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4485  * be set on entry.
4486  */
4487 void bnxt_set_ring_params(struct bnxt *bp)
4488 {
4489 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4490 	u32 agg_factor = 0, agg_ring_size = 0;
4491 
4492 	/* 8 for CRC and VLAN */
4493 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4494 
4495 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4496 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4497 
4498 	bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
4499 	ring_size = bp->rx_ring_size;
4500 	bp->rx_agg_ring_size = 0;
4501 	bp->rx_agg_nr_pages = 0;
4502 
4503 	if (bp->flags & BNXT_FLAG_TPA)
4504 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4505 
4506 	bp->flags &= ~BNXT_FLAG_JUMBO;
4507 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4508 		u32 jumbo_factor;
4509 
4510 		bp->flags |= BNXT_FLAG_JUMBO;
4511 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4512 		if (jumbo_factor > agg_factor)
4513 			agg_factor = jumbo_factor;
4514 	}
4515 	if (agg_factor) {
4516 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4517 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4518 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4519 				    bp->rx_ring_size, ring_size);
4520 			bp->rx_ring_size = ring_size;
4521 		}
4522 		agg_ring_size = ring_size * agg_factor;
4523 
4524 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4525 							RX_DESC_CNT);
4526 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4527 			u32 tmp = agg_ring_size;
4528 
4529 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4530 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4531 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4532 				    tmp, agg_ring_size);
4533 		}
4534 		bp->rx_agg_ring_size = agg_ring_size;
4535 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4536 
4537 		if (BNXT_RX_PAGE_MODE(bp)) {
4538 			rx_space = PAGE_SIZE;
4539 			rx_size = PAGE_SIZE -
4540 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4541 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4542 		} else {
4543 			rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
4544 			rx_space = rx_size + NET_SKB_PAD +
4545 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4546 		}
4547 	}
4548 
4549 	bp->rx_buf_use_size = rx_size;
4550 	bp->rx_buf_size = rx_space;
4551 
4552 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4553 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4554 
4555 	ring_size = bp->tx_ring_size;
4556 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4557 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4558 
4559 	max_rx_cmpl = bp->rx_ring_size;
4560 	/* MAX TPA needs to be added because TPA_START completions are
4561 	 * immediately recycled, so the TPA completions are not bound by
4562 	 * the RX ring size.
4563 	 */
4564 	if (bp->flags & BNXT_FLAG_TPA)
4565 		max_rx_cmpl += bp->max_tpa;
4566 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4567 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4568 	bp->cp_ring_size = ring_size;
4569 
4570 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4571 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4572 		bp->cp_nr_pages = MAX_CP_PAGES;
4573 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4574 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4575 			    ring_size, bp->cp_ring_size);
4576 	}
4577 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4578 	bp->cp_ring_mask = bp->cp_bit - 1;
4579 }
4580 
4581 /* Changing allocation mode of RX rings.
4582  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4583  */
4584 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4585 {
4586 	struct net_device *dev = bp->dev;
4587 
4588 	if (page_mode) {
4589 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
4590 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4591 
4592 		if (bp->xdp_prog->aux->xdp_has_frags)
4593 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4594 		else
4595 			dev->max_mtu =
4596 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4597 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4598 			bp->flags |= BNXT_FLAG_JUMBO;
4599 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4600 		} else {
4601 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4602 			bp->rx_skb_func = bnxt_rx_page_skb;
4603 		}
4604 		bp->rx_dir = DMA_BIDIRECTIONAL;
4605 		/* Disable LRO or GRO_HW */
4606 		netdev_update_features(dev);
4607 	} else {
4608 		dev->max_mtu = bp->max_mtu;
4609 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4610 		bp->rx_dir = DMA_FROM_DEVICE;
4611 		bp->rx_skb_func = bnxt_rx_skb;
4612 	}
4613 	return 0;
4614 }
4615 
4616 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4617 {
4618 	int i;
4619 	struct bnxt_vnic_info *vnic;
4620 	struct pci_dev *pdev = bp->pdev;
4621 
4622 	if (!bp->vnic_info)
4623 		return;
4624 
4625 	for (i = 0; i < bp->nr_vnics; i++) {
4626 		vnic = &bp->vnic_info[i];
4627 
4628 		kfree(vnic->fw_grp_ids);
4629 		vnic->fw_grp_ids = NULL;
4630 
4631 		kfree(vnic->uc_list);
4632 		vnic->uc_list = NULL;
4633 
4634 		if (vnic->mc_list) {
4635 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4636 					  vnic->mc_list, vnic->mc_list_mapping);
4637 			vnic->mc_list = NULL;
4638 		}
4639 
4640 		if (vnic->rss_table) {
4641 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4642 					  vnic->rss_table,
4643 					  vnic->rss_table_dma_addr);
4644 			vnic->rss_table = NULL;
4645 		}
4646 
4647 		vnic->rss_hash_key = NULL;
4648 		vnic->flags = 0;
4649 	}
4650 }
4651 
4652 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4653 {
4654 	int i, rc = 0, size;
4655 	struct bnxt_vnic_info *vnic;
4656 	struct pci_dev *pdev = bp->pdev;
4657 	int max_rings;
4658 
4659 	for (i = 0; i < bp->nr_vnics; i++) {
4660 		vnic = &bp->vnic_info[i];
4661 
4662 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4663 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4664 
4665 			if (mem_size > 0) {
4666 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4667 				if (!vnic->uc_list) {
4668 					rc = -ENOMEM;
4669 					goto out;
4670 				}
4671 			}
4672 		}
4673 
4674 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4675 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4676 			vnic->mc_list =
4677 				dma_alloc_coherent(&pdev->dev,
4678 						   vnic->mc_list_size,
4679 						   &vnic->mc_list_mapping,
4680 						   GFP_KERNEL);
4681 			if (!vnic->mc_list) {
4682 				rc = -ENOMEM;
4683 				goto out;
4684 			}
4685 		}
4686 
4687 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4688 			goto vnic_skip_grps;
4689 
4690 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4691 			max_rings = bp->rx_nr_rings;
4692 		else
4693 			max_rings = 1;
4694 
4695 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4696 		if (!vnic->fw_grp_ids) {
4697 			rc = -ENOMEM;
4698 			goto out;
4699 		}
4700 vnic_skip_grps:
4701 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4702 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4703 			continue;
4704 
4705 		/* Allocate rss table and hash key */
4706 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4707 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4708 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4709 
4710 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4711 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4712 						     vnic->rss_table_size,
4713 						     &vnic->rss_table_dma_addr,
4714 						     GFP_KERNEL);
4715 		if (!vnic->rss_table) {
4716 			rc = -ENOMEM;
4717 			goto out;
4718 		}
4719 
4720 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4721 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4722 	}
4723 	return 0;
4724 
4725 out:
4726 	return rc;
4727 }
4728 
4729 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4730 {
4731 	struct bnxt_hwrm_wait_token *token;
4732 
4733 	dma_pool_destroy(bp->hwrm_dma_pool);
4734 	bp->hwrm_dma_pool = NULL;
4735 
4736 	rcu_read_lock();
4737 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4738 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4739 	rcu_read_unlock();
4740 }
4741 
4742 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4743 {
4744 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4745 					    BNXT_HWRM_DMA_SIZE,
4746 					    BNXT_HWRM_DMA_ALIGN, 0);
4747 	if (!bp->hwrm_dma_pool)
4748 		return -ENOMEM;
4749 
4750 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4751 
4752 	return 0;
4753 }
4754 
4755 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4756 {
4757 	kfree(stats->hw_masks);
4758 	stats->hw_masks = NULL;
4759 	kfree(stats->sw_stats);
4760 	stats->sw_stats = NULL;
4761 	if (stats->hw_stats) {
4762 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
4763 				  stats->hw_stats_map);
4764 		stats->hw_stats = NULL;
4765 	}
4766 }
4767 
4768 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
4769 				bool alloc_masks)
4770 {
4771 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
4772 					     &stats->hw_stats_map, GFP_KERNEL);
4773 	if (!stats->hw_stats)
4774 		return -ENOMEM;
4775 
4776 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
4777 	if (!stats->sw_stats)
4778 		goto stats_mem_err;
4779 
4780 	if (alloc_masks) {
4781 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
4782 		if (!stats->hw_masks)
4783 			goto stats_mem_err;
4784 	}
4785 	return 0;
4786 
4787 stats_mem_err:
4788 	bnxt_free_stats_mem(bp, stats);
4789 	return -ENOMEM;
4790 }
4791 
4792 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
4793 {
4794 	int i;
4795 
4796 	for (i = 0; i < count; i++)
4797 		mask_arr[i] = mask;
4798 }
4799 
4800 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
4801 {
4802 	int i;
4803 
4804 	for (i = 0; i < count; i++)
4805 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
4806 }
4807 
4808 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
4809 				    struct bnxt_stats_mem *stats)
4810 {
4811 	struct hwrm_func_qstats_ext_output *resp;
4812 	struct hwrm_func_qstats_ext_input *req;
4813 	__le64 *hw_masks;
4814 	int rc;
4815 
4816 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
4817 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4818 		return -EOPNOTSUPP;
4819 
4820 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
4821 	if (rc)
4822 		return rc;
4823 
4824 	req->fid = cpu_to_le16(0xffff);
4825 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4826 
4827 	resp = hwrm_req_hold(bp, req);
4828 	rc = hwrm_req_send(bp, req);
4829 	if (!rc) {
4830 		hw_masks = &resp->rx_ucast_pkts;
4831 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
4832 	}
4833 	hwrm_req_drop(bp, req);
4834 	return rc;
4835 }
4836 
4837 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
4838 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
4839 
4840 static void bnxt_init_stats(struct bnxt *bp)
4841 {
4842 	struct bnxt_napi *bnapi = bp->bnapi[0];
4843 	struct bnxt_cp_ring_info *cpr;
4844 	struct bnxt_stats_mem *stats;
4845 	__le64 *rx_stats, *tx_stats;
4846 	int rc, rx_count, tx_count;
4847 	u64 *rx_masks, *tx_masks;
4848 	u64 mask;
4849 	u8 flags;
4850 
4851 	cpr = &bnapi->cp_ring;
4852 	stats = &cpr->stats;
4853 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
4854 	if (rc) {
4855 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4856 			mask = (1ULL << 48) - 1;
4857 		else
4858 			mask = -1ULL;
4859 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
4860 	}
4861 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
4862 		stats = &bp->port_stats;
4863 		rx_stats = stats->hw_stats;
4864 		rx_masks = stats->hw_masks;
4865 		rx_count = sizeof(struct rx_port_stats) / 8;
4866 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4867 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4868 		tx_count = sizeof(struct tx_port_stats) / 8;
4869 
4870 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
4871 		rc = bnxt_hwrm_port_qstats(bp, flags);
4872 		if (rc) {
4873 			mask = (1ULL << 40) - 1;
4874 
4875 			bnxt_fill_masks(rx_masks, mask, rx_count);
4876 			bnxt_fill_masks(tx_masks, mask, tx_count);
4877 		} else {
4878 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4879 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
4880 			bnxt_hwrm_port_qstats(bp, 0);
4881 		}
4882 	}
4883 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
4884 		stats = &bp->rx_port_stats_ext;
4885 		rx_stats = stats->hw_stats;
4886 		rx_masks = stats->hw_masks;
4887 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
4888 		stats = &bp->tx_port_stats_ext;
4889 		tx_stats = stats->hw_stats;
4890 		tx_masks = stats->hw_masks;
4891 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
4892 
4893 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4894 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
4895 		if (rc) {
4896 			mask = (1ULL << 40) - 1;
4897 
4898 			bnxt_fill_masks(rx_masks, mask, rx_count);
4899 			if (tx_stats)
4900 				bnxt_fill_masks(tx_masks, mask, tx_count);
4901 		} else {
4902 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4903 			if (tx_stats)
4904 				bnxt_copy_hw_masks(tx_masks, tx_stats,
4905 						   tx_count);
4906 			bnxt_hwrm_port_qstats_ext(bp, 0);
4907 		}
4908 	}
4909 }
4910 
4911 static void bnxt_free_port_stats(struct bnxt *bp)
4912 {
4913 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
4914 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
4915 
4916 	bnxt_free_stats_mem(bp, &bp->port_stats);
4917 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
4918 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
4919 }
4920 
4921 static void bnxt_free_ring_stats(struct bnxt *bp)
4922 {
4923 	int i;
4924 
4925 	if (!bp->bnapi)
4926 		return;
4927 
4928 	for (i = 0; i < bp->cp_nr_rings; i++) {
4929 		struct bnxt_napi *bnapi = bp->bnapi[i];
4930 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4931 
4932 		bnxt_free_stats_mem(bp, &cpr->stats);
4933 
4934 		kfree(cpr->sw_stats);
4935 		cpr->sw_stats = NULL;
4936 	}
4937 }
4938 
4939 static int bnxt_alloc_stats(struct bnxt *bp)
4940 {
4941 	u32 size, i;
4942 	int rc;
4943 
4944 	size = bp->hw_ring_stats_size;
4945 
4946 	for (i = 0; i < bp->cp_nr_rings; i++) {
4947 		struct bnxt_napi *bnapi = bp->bnapi[i];
4948 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4949 
4950 		cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL);
4951 		if (!cpr->sw_stats)
4952 			return -ENOMEM;
4953 
4954 		cpr->stats.len = size;
4955 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
4956 		if (rc)
4957 			return rc;
4958 
4959 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4960 	}
4961 
4962 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
4963 		return 0;
4964 
4965 	if (bp->port_stats.hw_stats)
4966 		goto alloc_ext_stats;
4967 
4968 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
4969 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
4970 	if (rc)
4971 		return rc;
4972 
4973 	bp->flags |= BNXT_FLAG_PORT_STATS;
4974 
4975 alloc_ext_stats:
4976 	/* Display extended statistics only if FW supports it */
4977 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
4978 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
4979 			return 0;
4980 
4981 	if (bp->rx_port_stats_ext.hw_stats)
4982 		goto alloc_tx_ext_stats;
4983 
4984 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
4985 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
4986 	/* Extended stats are optional */
4987 	if (rc)
4988 		return 0;
4989 
4990 alloc_tx_ext_stats:
4991 	if (bp->tx_port_stats_ext.hw_stats)
4992 		return 0;
4993 
4994 	if (bp->hwrm_spec_code >= 0x10902 ||
4995 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
4996 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
4997 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
4998 		/* Extended stats are optional */
4999 		if (rc)
5000 			return 0;
5001 	}
5002 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5003 	return 0;
5004 }
5005 
5006 static void bnxt_clear_ring_indices(struct bnxt *bp)
5007 {
5008 	int i, j;
5009 
5010 	if (!bp->bnapi)
5011 		return;
5012 
5013 	for (i = 0; i < bp->cp_nr_rings; i++) {
5014 		struct bnxt_napi *bnapi = bp->bnapi[i];
5015 		struct bnxt_cp_ring_info *cpr;
5016 		struct bnxt_rx_ring_info *rxr;
5017 		struct bnxt_tx_ring_info *txr;
5018 
5019 		if (!bnapi)
5020 			continue;
5021 
5022 		cpr = &bnapi->cp_ring;
5023 		cpr->cp_raw_cons = 0;
5024 
5025 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5026 			txr->tx_prod = 0;
5027 			txr->tx_cons = 0;
5028 			txr->tx_hw_cons = 0;
5029 		}
5030 
5031 		rxr = bnapi->rx_ring;
5032 		if (rxr) {
5033 			rxr->rx_prod = 0;
5034 			rxr->rx_agg_prod = 0;
5035 			rxr->rx_sw_agg_prod = 0;
5036 			rxr->rx_next_cons = 0;
5037 		}
5038 		bnapi->events = 0;
5039 	}
5040 }
5041 
5042 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5043 {
5044 	u8 type = fltr->type, flags = fltr->flags;
5045 
5046 	INIT_LIST_HEAD(&fltr->list);
5047 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5048 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5049 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5050 }
5051 
5052 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5053 {
5054 	if (!list_empty(&fltr->list))
5055 		list_del_init(&fltr->list);
5056 }
5057 
5058 void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5059 {
5060 	struct bnxt_filter_base *usr_fltr, *tmp;
5061 
5062 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5063 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5064 			continue;
5065 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5066 	}
5067 }
5068 
5069 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5070 {
5071 	hlist_del(&fltr->hash);
5072 	bnxt_del_one_usr_fltr(bp, fltr);
5073 	if (fltr->flags) {
5074 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5075 		bp->ntp_fltr_count--;
5076 	}
5077 	kfree(fltr);
5078 }
5079 
5080 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5081 {
5082 	int i;
5083 
5084 	/* Under rtnl_lock and all our NAPIs have been disabled.  It's
5085 	 * safe to delete the hash table.
5086 	 */
5087 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5088 		struct hlist_head *head;
5089 		struct hlist_node *tmp;
5090 		struct bnxt_ntuple_filter *fltr;
5091 
5092 		head = &bp->ntp_fltr_hash_tbl[i];
5093 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5094 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5095 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5096 				     !list_empty(&fltr->base.list)))
5097 				continue;
5098 			bnxt_del_fltr(bp, &fltr->base);
5099 		}
5100 	}
5101 	if (!all)
5102 		return;
5103 
5104 	bitmap_free(bp->ntp_fltr_bmap);
5105 	bp->ntp_fltr_bmap = NULL;
5106 	bp->ntp_fltr_count = 0;
5107 }
5108 
5109 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5110 {
5111 	int i, rc = 0;
5112 
5113 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5114 		return 0;
5115 
5116 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5117 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5118 
5119 	bp->ntp_fltr_count = 0;
5120 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5121 
5122 	if (!bp->ntp_fltr_bmap)
5123 		rc = -ENOMEM;
5124 
5125 	return rc;
5126 }
5127 
5128 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5129 {
5130 	int i;
5131 
5132 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5133 		struct hlist_head *head;
5134 		struct hlist_node *tmp;
5135 		struct bnxt_l2_filter *fltr;
5136 
5137 		head = &bp->l2_fltr_hash_tbl[i];
5138 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5139 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5140 				     !list_empty(&fltr->base.list)))
5141 				continue;
5142 			bnxt_del_fltr(bp, &fltr->base);
5143 		}
5144 	}
5145 }
5146 
5147 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5148 {
5149 	int i;
5150 
5151 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5152 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5153 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5154 }
5155 
5156 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5157 {
5158 	bnxt_free_vnic_attributes(bp);
5159 	bnxt_free_tx_rings(bp);
5160 	bnxt_free_rx_rings(bp);
5161 	bnxt_free_cp_rings(bp);
5162 	bnxt_free_all_cp_arrays(bp);
5163 	bnxt_free_ntp_fltrs(bp, false);
5164 	bnxt_free_l2_filters(bp, false);
5165 	if (irq_re_init) {
5166 		bnxt_free_ring_stats(bp);
5167 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5168 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5169 			bnxt_free_port_stats(bp);
5170 		bnxt_free_ring_grps(bp);
5171 		bnxt_free_vnics(bp);
5172 		kfree(bp->tx_ring_map);
5173 		bp->tx_ring_map = NULL;
5174 		kfree(bp->tx_ring);
5175 		bp->tx_ring = NULL;
5176 		kfree(bp->rx_ring);
5177 		bp->rx_ring = NULL;
5178 		kfree(bp->bnapi);
5179 		bp->bnapi = NULL;
5180 	} else {
5181 		bnxt_clear_ring_indices(bp);
5182 	}
5183 }
5184 
5185 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5186 {
5187 	int i, j, rc, size, arr_size;
5188 	void *bnapi;
5189 
5190 	if (irq_re_init) {
5191 		/* Allocate bnapi mem pointer array and mem block for
5192 		 * all queues
5193 		 */
5194 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5195 				bp->cp_nr_rings);
5196 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5197 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5198 		if (!bnapi)
5199 			return -ENOMEM;
5200 
5201 		bp->bnapi = bnapi;
5202 		bnapi += arr_size;
5203 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5204 			bp->bnapi[i] = bnapi;
5205 			bp->bnapi[i]->index = i;
5206 			bp->bnapi[i]->bp = bp;
5207 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5208 				struct bnxt_cp_ring_info *cpr =
5209 					&bp->bnapi[i]->cp_ring;
5210 
5211 				cpr->cp_ring_struct.ring_mem.flags =
5212 					BNXT_RMEM_RING_PTE_FLAG;
5213 			}
5214 		}
5215 
5216 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5217 				      sizeof(struct bnxt_rx_ring_info),
5218 				      GFP_KERNEL);
5219 		if (!bp->rx_ring)
5220 			return -ENOMEM;
5221 
5222 		for (i = 0; i < bp->rx_nr_rings; i++) {
5223 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5224 
5225 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5226 				rxr->rx_ring_struct.ring_mem.flags =
5227 					BNXT_RMEM_RING_PTE_FLAG;
5228 				rxr->rx_agg_ring_struct.ring_mem.flags =
5229 					BNXT_RMEM_RING_PTE_FLAG;
5230 			} else {
5231 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5232 			}
5233 			rxr->bnapi = bp->bnapi[i];
5234 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5235 		}
5236 
5237 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5238 				      sizeof(struct bnxt_tx_ring_info),
5239 				      GFP_KERNEL);
5240 		if (!bp->tx_ring)
5241 			return -ENOMEM;
5242 
5243 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5244 					  GFP_KERNEL);
5245 
5246 		if (!bp->tx_ring_map)
5247 			return -ENOMEM;
5248 
5249 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5250 			j = 0;
5251 		else
5252 			j = bp->rx_nr_rings;
5253 
5254 		for (i = 0; i < bp->tx_nr_rings; i++) {
5255 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5256 			struct bnxt_napi *bnapi2;
5257 
5258 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5259 				txr->tx_ring_struct.ring_mem.flags =
5260 					BNXT_RMEM_RING_PTE_FLAG;
5261 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5262 			if (i >= bp->tx_nr_rings_xdp) {
5263 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5264 
5265 				bnapi2 = bp->bnapi[k];
5266 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5267 				txr->tx_napi_idx =
5268 					BNXT_RING_TO_TC(bp, txr->txq_index);
5269 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5270 				bnapi2->tx_int = bnxt_tx_int;
5271 			} else {
5272 				bnapi2 = bp->bnapi[j];
5273 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5274 				bnapi2->tx_ring[0] = txr;
5275 				bnapi2->tx_int = bnxt_tx_int_xdp;
5276 				j++;
5277 			}
5278 			txr->bnapi = bnapi2;
5279 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5280 				txr->tx_cpr = &bnapi2->cp_ring;
5281 		}
5282 
5283 		rc = bnxt_alloc_stats(bp);
5284 		if (rc)
5285 			goto alloc_mem_err;
5286 		bnxt_init_stats(bp);
5287 
5288 		rc = bnxt_alloc_ntp_fltrs(bp);
5289 		if (rc)
5290 			goto alloc_mem_err;
5291 
5292 		rc = bnxt_alloc_vnics(bp);
5293 		if (rc)
5294 			goto alloc_mem_err;
5295 	}
5296 
5297 	rc = bnxt_alloc_all_cp_arrays(bp);
5298 	if (rc)
5299 		goto alloc_mem_err;
5300 
5301 	bnxt_init_ring_struct(bp);
5302 
5303 	rc = bnxt_alloc_rx_rings(bp);
5304 	if (rc)
5305 		goto alloc_mem_err;
5306 
5307 	rc = bnxt_alloc_tx_rings(bp);
5308 	if (rc)
5309 		goto alloc_mem_err;
5310 
5311 	rc = bnxt_alloc_cp_rings(bp);
5312 	if (rc)
5313 		goto alloc_mem_err;
5314 
5315 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5316 						  BNXT_VNIC_MCAST_FLAG |
5317 						  BNXT_VNIC_UCAST_FLAG;
5318 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5319 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5320 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5321 
5322 	rc = bnxt_alloc_vnic_attributes(bp);
5323 	if (rc)
5324 		goto alloc_mem_err;
5325 	return 0;
5326 
5327 alloc_mem_err:
5328 	bnxt_free_mem(bp, true);
5329 	return rc;
5330 }
5331 
5332 static void bnxt_disable_int(struct bnxt *bp)
5333 {
5334 	int i;
5335 
5336 	if (!bp->bnapi)
5337 		return;
5338 
5339 	for (i = 0; i < bp->cp_nr_rings; i++) {
5340 		struct bnxt_napi *bnapi = bp->bnapi[i];
5341 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5342 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5343 
5344 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5345 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5346 	}
5347 }
5348 
5349 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5350 {
5351 	struct bnxt_napi *bnapi = bp->bnapi[n];
5352 	struct bnxt_cp_ring_info *cpr;
5353 
5354 	cpr = &bnapi->cp_ring;
5355 	return cpr->cp_ring_struct.map_idx;
5356 }
5357 
5358 static void bnxt_disable_int_sync(struct bnxt *bp)
5359 {
5360 	int i;
5361 
5362 	if (!bp->irq_tbl)
5363 		return;
5364 
5365 	atomic_inc(&bp->intr_sem);
5366 
5367 	bnxt_disable_int(bp);
5368 	for (i = 0; i < bp->cp_nr_rings; i++) {
5369 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5370 
5371 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5372 	}
5373 }
5374 
5375 static void bnxt_enable_int(struct bnxt *bp)
5376 {
5377 	int i;
5378 
5379 	atomic_set(&bp->intr_sem, 0);
5380 	for (i = 0; i < bp->cp_nr_rings; i++) {
5381 		struct bnxt_napi *bnapi = bp->bnapi[i];
5382 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5383 
5384 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5385 	}
5386 }
5387 
5388 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5389 			    bool async_only)
5390 {
5391 	DECLARE_BITMAP(async_events_bmap, 256);
5392 	u32 *events = (u32 *)async_events_bmap;
5393 	struct hwrm_func_drv_rgtr_output *resp;
5394 	struct hwrm_func_drv_rgtr_input *req;
5395 	u32 flags;
5396 	int rc, i;
5397 
5398 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5399 	if (rc)
5400 		return rc;
5401 
5402 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5403 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5404 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5405 
5406 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5407 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5408 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5409 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5410 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5411 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5412 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5413 	req->flags = cpu_to_le32(flags);
5414 	req->ver_maj_8b = DRV_VER_MAJ;
5415 	req->ver_min_8b = DRV_VER_MIN;
5416 	req->ver_upd_8b = DRV_VER_UPD;
5417 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5418 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5419 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5420 
5421 	if (BNXT_PF(bp)) {
5422 		u32 data[8];
5423 		int i;
5424 
5425 		memset(data, 0, sizeof(data));
5426 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5427 			u16 cmd = bnxt_vf_req_snif[i];
5428 			unsigned int bit, idx;
5429 
5430 			idx = cmd / 32;
5431 			bit = cmd % 32;
5432 			data[idx] |= 1 << bit;
5433 		}
5434 
5435 		for (i = 0; i < 8; i++)
5436 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5437 
5438 		req->enables |=
5439 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5440 	}
5441 
5442 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5443 		req->flags |= cpu_to_le32(
5444 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5445 
5446 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5447 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5448 		u16 event_id = bnxt_async_events_arr[i];
5449 
5450 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5451 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5452 			continue;
5453 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5454 		    !bp->ptp_cfg)
5455 			continue;
5456 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5457 	}
5458 	if (bmap && bmap_size) {
5459 		for (i = 0; i < bmap_size; i++) {
5460 			if (test_bit(i, bmap))
5461 				__set_bit(i, async_events_bmap);
5462 		}
5463 	}
5464 	for (i = 0; i < 8; i++)
5465 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5466 
5467 	if (async_only)
5468 		req->enables =
5469 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5470 
5471 	resp = hwrm_req_hold(bp, req);
5472 	rc = hwrm_req_send(bp, req);
5473 	if (!rc) {
5474 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5475 		if (resp->flags &
5476 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5477 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5478 	}
5479 	hwrm_req_drop(bp, req);
5480 	return rc;
5481 }
5482 
5483 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5484 {
5485 	struct hwrm_func_drv_unrgtr_input *req;
5486 	int rc;
5487 
5488 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5489 		return 0;
5490 
5491 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5492 	if (rc)
5493 		return rc;
5494 	return hwrm_req_send(bp, req);
5495 }
5496 
5497 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5498 
5499 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5500 {
5501 	struct hwrm_tunnel_dst_port_free_input *req;
5502 	int rc;
5503 
5504 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5505 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5506 		return 0;
5507 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5508 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5509 		return 0;
5510 
5511 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5512 	if (rc)
5513 		return rc;
5514 
5515 	req->tunnel_type = tunnel_type;
5516 
5517 	switch (tunnel_type) {
5518 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5519 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5520 		bp->vxlan_port = 0;
5521 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5522 		break;
5523 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5524 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5525 		bp->nge_port = 0;
5526 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5527 		break;
5528 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5529 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5530 		bp->vxlan_gpe_port = 0;
5531 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5532 		break;
5533 	default:
5534 		break;
5535 	}
5536 
5537 	rc = hwrm_req_send(bp, req);
5538 	if (rc)
5539 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5540 			   rc);
5541 	if (bp->flags & BNXT_FLAG_TPA)
5542 		bnxt_set_tpa(bp, true);
5543 	return rc;
5544 }
5545 
5546 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5547 					   u8 tunnel_type)
5548 {
5549 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5550 	struct hwrm_tunnel_dst_port_alloc_input *req;
5551 	int rc;
5552 
5553 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5554 	if (rc)
5555 		return rc;
5556 
5557 	req->tunnel_type = tunnel_type;
5558 	req->tunnel_dst_port_val = port;
5559 
5560 	resp = hwrm_req_hold(bp, req);
5561 	rc = hwrm_req_send(bp, req);
5562 	if (rc) {
5563 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5564 			   rc);
5565 		goto err_out;
5566 	}
5567 
5568 	switch (tunnel_type) {
5569 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5570 		bp->vxlan_port = port;
5571 		bp->vxlan_fw_dst_port_id =
5572 			le16_to_cpu(resp->tunnel_dst_port_id);
5573 		break;
5574 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5575 		bp->nge_port = port;
5576 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5577 		break;
5578 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5579 		bp->vxlan_gpe_port = port;
5580 		bp->vxlan_gpe_fw_dst_port_id =
5581 			le16_to_cpu(resp->tunnel_dst_port_id);
5582 		break;
5583 	default:
5584 		break;
5585 	}
5586 	if (bp->flags & BNXT_FLAG_TPA)
5587 		bnxt_set_tpa(bp, true);
5588 
5589 err_out:
5590 	hwrm_req_drop(bp, req);
5591 	return rc;
5592 }
5593 
5594 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5595 {
5596 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5597 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5598 	int rc;
5599 
5600 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5601 	if (rc)
5602 		return rc;
5603 
5604 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5605 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5606 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5607 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5608 	}
5609 	req->mask = cpu_to_le32(vnic->rx_mask);
5610 	return hwrm_req_send_silent(bp, req);
5611 }
5612 
5613 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5614 {
5615 	if (!atomic_dec_and_test(&fltr->refcnt))
5616 		return;
5617 	spin_lock_bh(&bp->ntp_fltr_lock);
5618 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5619 		spin_unlock_bh(&bp->ntp_fltr_lock);
5620 		return;
5621 	}
5622 	hlist_del_rcu(&fltr->base.hash);
5623 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5624 	if (fltr->base.flags) {
5625 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5626 		bp->ntp_fltr_count--;
5627 	}
5628 	spin_unlock_bh(&bp->ntp_fltr_lock);
5629 	kfree_rcu(fltr, base.rcu);
5630 }
5631 
5632 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5633 						      struct bnxt_l2_key *key,
5634 						      u32 idx)
5635 {
5636 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5637 	struct bnxt_l2_filter *fltr;
5638 
5639 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5640 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5641 
5642 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5643 		    l2_key->vlan == key->vlan)
5644 			return fltr;
5645 	}
5646 	return NULL;
5647 }
5648 
5649 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5650 						    struct bnxt_l2_key *key,
5651 						    u32 idx)
5652 {
5653 	struct bnxt_l2_filter *fltr = NULL;
5654 
5655 	rcu_read_lock();
5656 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5657 	if (fltr)
5658 		atomic_inc(&fltr->refcnt);
5659 	rcu_read_unlock();
5660 	return fltr;
5661 }
5662 
5663 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5664 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5665 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5666 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5667 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5668 
5669 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5670 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5671 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5672 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5673 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5674 
5675 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5676 {
5677 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5678 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5679 			return sizeof(fkeys->addrs.v4addrs) +
5680 			       sizeof(fkeys->ports);
5681 
5682 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5683 			return sizeof(fkeys->addrs.v4addrs);
5684 	}
5685 
5686 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5687 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5688 			return sizeof(fkeys->addrs.v6addrs) +
5689 			       sizeof(fkeys->ports);
5690 
5691 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5692 			return sizeof(fkeys->addrs.v6addrs);
5693 	}
5694 
5695 	return 0;
5696 }
5697 
5698 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5699 			 const unsigned char *key)
5700 {
5701 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5702 	struct bnxt_ipv4_tuple tuple4;
5703 	struct bnxt_ipv6_tuple tuple6;
5704 	int i, j, len = 0;
5705 	u8 *four_tuple;
5706 
5707 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5708 	if (!len)
5709 		return 0;
5710 
5711 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5712 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5713 		tuple4.ports = fkeys->ports;
5714 		four_tuple = (unsigned char *)&tuple4;
5715 	} else {
5716 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5717 		tuple6.ports = fkeys->ports;
5718 		four_tuple = (unsigned char *)&tuple6;
5719 	}
5720 
5721 	for (i = 0, j = 8; i < len; i++, j++) {
5722 		u8 byte = four_tuple[i];
5723 		int bit;
5724 
5725 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5726 			if (byte & 0x80)
5727 				hash ^= prefix;
5728 		}
5729 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5730 	}
5731 
5732 	/* The valid part of the hash is in the upper 32 bits. */
5733 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5734 }
5735 
5736 #ifdef CONFIG_RFS_ACCEL
5737 static struct bnxt_l2_filter *
5738 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5739 {
5740 	struct bnxt_l2_filter *fltr;
5741 	u32 idx;
5742 
5743 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5744 	      BNXT_L2_FLTR_HASH_MASK;
5745 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5746 	return fltr;
5747 }
5748 #endif
5749 
5750 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
5751 			       struct bnxt_l2_key *key, u32 idx)
5752 {
5753 	struct hlist_head *head;
5754 
5755 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
5756 	fltr->l2_key.vlan = key->vlan;
5757 	fltr->base.type = BNXT_FLTR_TYPE_L2;
5758 	if (fltr->base.flags) {
5759 		int bit_id;
5760 
5761 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
5762 						 bp->max_fltr, 0);
5763 		if (bit_id < 0)
5764 			return -ENOMEM;
5765 		fltr->base.sw_id = (u16)bit_id;
5766 		bp->ntp_fltr_count++;
5767 	}
5768 	head = &bp->l2_fltr_hash_tbl[idx];
5769 	hlist_add_head_rcu(&fltr->base.hash, head);
5770 	bnxt_insert_usr_fltr(bp, &fltr->base);
5771 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
5772 	atomic_set(&fltr->refcnt, 1);
5773 	return 0;
5774 }
5775 
5776 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
5777 						   struct bnxt_l2_key *key,
5778 						   gfp_t gfp)
5779 {
5780 	struct bnxt_l2_filter *fltr;
5781 	u32 idx;
5782 	int rc;
5783 
5784 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5785 	      BNXT_L2_FLTR_HASH_MASK;
5786 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5787 	if (fltr)
5788 		return fltr;
5789 
5790 	fltr = kzalloc(sizeof(*fltr), gfp);
5791 	if (!fltr)
5792 		return ERR_PTR(-ENOMEM);
5793 	spin_lock_bh(&bp->ntp_fltr_lock);
5794 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5795 	spin_unlock_bh(&bp->ntp_fltr_lock);
5796 	if (rc) {
5797 		bnxt_del_l2_filter(bp, fltr);
5798 		fltr = ERR_PTR(rc);
5799 	}
5800 	return fltr;
5801 }
5802 
5803 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
5804 						struct bnxt_l2_key *key,
5805 						u16 flags)
5806 {
5807 	struct bnxt_l2_filter *fltr;
5808 	u32 idx;
5809 	int rc;
5810 
5811 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5812 	      BNXT_L2_FLTR_HASH_MASK;
5813 	spin_lock_bh(&bp->ntp_fltr_lock);
5814 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5815 	if (fltr) {
5816 		fltr = ERR_PTR(-EEXIST);
5817 		goto l2_filter_exit;
5818 	}
5819 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
5820 	if (!fltr) {
5821 		fltr = ERR_PTR(-ENOMEM);
5822 		goto l2_filter_exit;
5823 	}
5824 	fltr->base.flags = flags;
5825 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5826 	if (rc) {
5827 		spin_unlock_bh(&bp->ntp_fltr_lock);
5828 		bnxt_del_l2_filter(bp, fltr);
5829 		return ERR_PTR(rc);
5830 	}
5831 
5832 l2_filter_exit:
5833 	spin_unlock_bh(&bp->ntp_fltr_lock);
5834 	return fltr;
5835 }
5836 
5837 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
5838 {
5839 #ifdef CONFIG_BNXT_SRIOV
5840 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
5841 
5842 	return vf->fw_fid;
5843 #else
5844 	return INVALID_HW_RING_ID;
5845 #endif
5846 }
5847 
5848 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5849 {
5850 	struct hwrm_cfa_l2_filter_free_input *req;
5851 	u16 target_id = 0xffff;
5852 	int rc;
5853 
5854 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
5855 		struct bnxt_pf_info *pf = &bp->pf;
5856 
5857 		if (fltr->base.vf_idx >= pf->active_vfs)
5858 			return -EINVAL;
5859 
5860 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
5861 		if (target_id == INVALID_HW_RING_ID)
5862 			return -EINVAL;
5863 	}
5864 
5865 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
5866 	if (rc)
5867 		return rc;
5868 
5869 	req->target_id = cpu_to_le16(target_id);
5870 	req->l2_filter_id = fltr->base.filter_id;
5871 	return hwrm_req_send(bp, req);
5872 }
5873 
5874 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5875 {
5876 	struct hwrm_cfa_l2_filter_alloc_output *resp;
5877 	struct hwrm_cfa_l2_filter_alloc_input *req;
5878 	u16 target_id = 0xffff;
5879 	int rc;
5880 
5881 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
5882 		struct bnxt_pf_info *pf = &bp->pf;
5883 
5884 		if (fltr->base.vf_idx >= pf->active_vfs)
5885 			return -EINVAL;
5886 
5887 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
5888 	}
5889 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
5890 	if (rc)
5891 		return rc;
5892 
5893 	req->target_id = cpu_to_le16(target_id);
5894 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
5895 
5896 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
5897 		req->flags |=
5898 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
5899 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
5900 	req->enables =
5901 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
5902 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
5903 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
5904 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
5905 	eth_broadcast_addr(req->l2_addr_mask);
5906 
5907 	if (fltr->l2_key.vlan) {
5908 		req->enables |=
5909 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
5910 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
5911 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
5912 		req->num_vlans = 1;
5913 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
5914 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
5915 	}
5916 
5917 	resp = hwrm_req_hold(bp, req);
5918 	rc = hwrm_req_send(bp, req);
5919 	if (!rc) {
5920 		fltr->base.filter_id = resp->l2_filter_id;
5921 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
5922 	}
5923 	hwrm_req_drop(bp, req);
5924 	return rc;
5925 }
5926 
5927 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
5928 				     struct bnxt_ntuple_filter *fltr)
5929 {
5930 	struct hwrm_cfa_ntuple_filter_free_input *req;
5931 	int rc;
5932 
5933 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
5934 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
5935 	if (rc)
5936 		return rc;
5937 
5938 	req->ntuple_filter_id = fltr->base.filter_id;
5939 	return hwrm_req_send(bp, req);
5940 }
5941 
5942 #define BNXT_NTP_FLTR_FLAGS					\
5943 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
5944 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
5945 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
5946 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
5947 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
5948 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
5949 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
5950 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
5951 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
5952 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
5953 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
5954 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
5955 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
5956 
5957 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
5958 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
5959 
5960 void bnxt_fill_ipv6_mask(__be32 mask[4])
5961 {
5962 	int i;
5963 
5964 	for (i = 0; i < 4; i++)
5965 		mask[i] = cpu_to_be32(~0);
5966 }
5967 
5968 static void
5969 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
5970 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
5971 			  struct bnxt_ntuple_filter *fltr)
5972 {
5973 	struct bnxt_rss_ctx *rss_ctx, *tmp;
5974 	u16 rxq = fltr->base.rxq;
5975 
5976 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
5977 		list_for_each_entry_safe(rss_ctx, tmp, &bp->rss_ctx_list, list) {
5978 			if (rss_ctx->index == fltr->base.fw_vnic_id) {
5979 				struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
5980 
5981 				req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
5982 				break;
5983 			}
5984 		}
5985 		return;
5986 	}
5987 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
5988 		struct bnxt_vnic_info *vnic;
5989 		u32 enables;
5990 
5991 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
5992 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
5993 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
5994 		req->enables |= cpu_to_le32(enables);
5995 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
5996 	} else {
5997 		u32 flags;
5998 
5999 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6000 		req->flags |= cpu_to_le32(flags);
6001 		req->dst_id = cpu_to_le16(rxq);
6002 	}
6003 }
6004 
6005 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6006 				      struct bnxt_ntuple_filter *fltr)
6007 {
6008 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6009 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6010 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6011 	struct flow_keys *keys = &fltr->fkeys;
6012 	struct bnxt_l2_filter *l2_fltr;
6013 	struct bnxt_vnic_info *vnic;
6014 	int rc;
6015 
6016 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6017 	if (rc)
6018 		return rc;
6019 
6020 	l2_fltr = fltr->l2_fltr;
6021 	req->l2_filter_id = l2_fltr->base.filter_id;
6022 
6023 	if (fltr->base.flags & BNXT_ACT_DROP) {
6024 		req->flags =
6025 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6026 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6027 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6028 	} else {
6029 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6030 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6031 	}
6032 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6033 
6034 	req->ethertype = htons(ETH_P_IP);
6035 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6036 	req->ip_protocol = keys->basic.ip_proto;
6037 
6038 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6039 		req->ethertype = htons(ETH_P_IPV6);
6040 		req->ip_addr_type =
6041 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6042 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6043 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6044 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6045 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6046 	} else {
6047 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6048 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6049 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6050 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6051 	}
6052 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6053 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6054 		req->tunnel_type =
6055 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6056 	}
6057 
6058 	req->src_port = keys->ports.src;
6059 	req->src_port_mask = masks->ports.src;
6060 	req->dst_port = keys->ports.dst;
6061 	req->dst_port_mask = masks->ports.dst;
6062 
6063 	resp = hwrm_req_hold(bp, req);
6064 	rc = hwrm_req_send(bp, req);
6065 	if (!rc)
6066 		fltr->base.filter_id = resp->ntuple_filter_id;
6067 	hwrm_req_drop(bp, req);
6068 	return rc;
6069 }
6070 
6071 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6072 				     const u8 *mac_addr)
6073 {
6074 	struct bnxt_l2_filter *fltr;
6075 	struct bnxt_l2_key key;
6076 	int rc;
6077 
6078 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6079 	key.vlan = 0;
6080 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6081 	if (IS_ERR(fltr))
6082 		return PTR_ERR(fltr);
6083 
6084 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6085 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6086 	if (rc)
6087 		bnxt_del_l2_filter(bp, fltr);
6088 	else
6089 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6090 	return rc;
6091 }
6092 
6093 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6094 {
6095 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6096 
6097 	/* Any associated ntuple filters will also be cleared by firmware. */
6098 	for (i = 0; i < num_of_vnics; i++) {
6099 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6100 
6101 		for (j = 0; j < vnic->uc_filter_count; j++) {
6102 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6103 
6104 			bnxt_hwrm_l2_filter_free(bp, fltr);
6105 			bnxt_del_l2_filter(bp, fltr);
6106 		}
6107 		vnic->uc_filter_count = 0;
6108 	}
6109 }
6110 
6111 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6112 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6113 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6114 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6115 
6116 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6117 					   struct hwrm_vnic_tpa_cfg_input *req)
6118 {
6119 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6120 
6121 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6122 		return;
6123 
6124 	if (bp->vxlan_port)
6125 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6126 	if (bp->vxlan_gpe_port)
6127 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6128 	if (bp->nge_port)
6129 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6130 
6131 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6132 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6133 }
6134 
6135 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6136 			   u32 tpa_flags)
6137 {
6138 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6139 	struct hwrm_vnic_tpa_cfg_input *req;
6140 	int rc;
6141 
6142 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6143 		return 0;
6144 
6145 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6146 	if (rc)
6147 		return rc;
6148 
6149 	if (tpa_flags) {
6150 		u16 mss = bp->dev->mtu - 40;
6151 		u32 nsegs, n, segs = 0, flags;
6152 
6153 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6154 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6155 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6156 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6157 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6158 		if (tpa_flags & BNXT_FLAG_GRO)
6159 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6160 
6161 		req->flags = cpu_to_le32(flags);
6162 
6163 		req->enables =
6164 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6165 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6166 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6167 
6168 		/* Number of segs are log2 units, and first packet is not
6169 		 * included as part of this units.
6170 		 */
6171 		if (mss <= BNXT_RX_PAGE_SIZE) {
6172 			n = BNXT_RX_PAGE_SIZE / mss;
6173 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6174 		} else {
6175 			n = mss / BNXT_RX_PAGE_SIZE;
6176 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6177 				n++;
6178 			nsegs = (MAX_SKB_FRAGS - n) / n;
6179 		}
6180 
6181 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6182 			segs = MAX_TPA_SEGS_P5;
6183 			max_aggs = bp->max_tpa;
6184 		} else {
6185 			segs = ilog2(nsegs);
6186 		}
6187 		req->max_agg_segs = cpu_to_le16(segs);
6188 		req->max_aggs = cpu_to_le16(max_aggs);
6189 
6190 		req->min_agg_len = cpu_to_le32(512);
6191 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6192 	}
6193 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6194 
6195 	return hwrm_req_send(bp, req);
6196 }
6197 
6198 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6199 {
6200 	struct bnxt_ring_grp_info *grp_info;
6201 
6202 	grp_info = &bp->grp_info[ring->grp_idx];
6203 	return grp_info->cp_fw_ring_id;
6204 }
6205 
6206 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6207 {
6208 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6209 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6210 	else
6211 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6212 }
6213 
6214 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6215 {
6216 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6217 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6218 	else
6219 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6220 }
6221 
6222 int bnxt_alloc_rss_indir_tbl(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx)
6223 {
6224 	int entries;
6225 	u16 *tbl;
6226 
6227 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6228 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6229 	else
6230 		entries = HW_HASH_INDEX_SIZE;
6231 
6232 	bp->rss_indir_tbl_entries = entries;
6233 	tbl = kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6234 	if (!tbl)
6235 		return -ENOMEM;
6236 
6237 	if (rss_ctx)
6238 		rss_ctx->rss_indir_tbl = tbl;
6239 	else
6240 		bp->rss_indir_tbl = tbl;
6241 
6242 	return 0;
6243 }
6244 
6245 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx)
6246 {
6247 	u16 max_rings, max_entries, pad, i;
6248 	u16 *rss_indir_tbl;
6249 
6250 	if (!bp->rx_nr_rings)
6251 		return;
6252 
6253 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6254 		max_rings = bp->rx_nr_rings - 1;
6255 	else
6256 		max_rings = bp->rx_nr_rings;
6257 
6258 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6259 	if (rss_ctx)
6260 		rss_indir_tbl = &rss_ctx->rss_indir_tbl[0];
6261 	else
6262 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6263 
6264 	for (i = 0; i < max_entries; i++)
6265 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6266 
6267 	pad = bp->rss_indir_tbl_entries - max_entries;
6268 	if (pad)
6269 		memset(&rss_indir_tbl[i], 0, pad * sizeof(u16));
6270 }
6271 
6272 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6273 {
6274 	u16 i, tbl_size, max_ring = 0;
6275 
6276 	if (!bp->rss_indir_tbl)
6277 		return 0;
6278 
6279 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6280 	for (i = 0; i < tbl_size; i++)
6281 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6282 	return max_ring;
6283 }
6284 
6285 u16 bnxt_get_max_rss_ctx_ring(struct bnxt *bp)
6286 {
6287 	u16 i, tbl_size, max_ring = 0;
6288 	struct bnxt_rss_ctx *rss_ctx;
6289 
6290 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6291 
6292 	list_for_each_entry(rss_ctx, &bp->rss_ctx_list, list) {
6293 		for (i = 0; i < tbl_size; i++)
6294 			max_ring = max(max_ring, rss_ctx->rss_indir_tbl[i]);
6295 	}
6296 
6297 	return max_ring;
6298 }
6299 
6300 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6301 {
6302 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6303 		if (!rx_rings)
6304 			return 0;
6305 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6306 					       BNXT_RSS_TABLE_ENTRIES_P5);
6307 	}
6308 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6309 		return 2;
6310 	return 1;
6311 }
6312 
6313 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6314 {
6315 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6316 	u16 i, j;
6317 
6318 	/* Fill the RSS indirection table with ring group ids */
6319 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6320 		if (!no_rss)
6321 			j = bp->rss_indir_tbl[i];
6322 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6323 	}
6324 }
6325 
6326 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6327 				    struct bnxt_vnic_info *vnic)
6328 {
6329 	__le16 *ring_tbl = vnic->rss_table;
6330 	struct bnxt_rx_ring_info *rxr;
6331 	u16 tbl_size, i;
6332 
6333 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6334 
6335 	for (i = 0; i < tbl_size; i++) {
6336 		u16 ring_id, j;
6337 
6338 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6339 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6340 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6341 			j = vnic->rss_ctx->rss_indir_tbl[i];
6342 		else
6343 			j = bp->rss_indir_tbl[i];
6344 		rxr = &bp->rx_ring[j];
6345 
6346 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6347 		*ring_tbl++ = cpu_to_le16(ring_id);
6348 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6349 		*ring_tbl++ = cpu_to_le16(ring_id);
6350 	}
6351 }
6352 
6353 static void
6354 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6355 			 struct bnxt_vnic_info *vnic)
6356 {
6357 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6358 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6359 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6360 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6361 	} else {
6362 		bnxt_fill_hw_rss_tbl(bp, vnic);
6363 	}
6364 
6365 	if (bp->rss_hash_delta) {
6366 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6367 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6368 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6369 		else
6370 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6371 	} else {
6372 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6373 	}
6374 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6375 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6376 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6377 }
6378 
6379 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6380 				  bool set_rss)
6381 {
6382 	struct hwrm_vnic_rss_cfg_input *req;
6383 	int rc;
6384 
6385 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6386 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6387 		return 0;
6388 
6389 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6390 	if (rc)
6391 		return rc;
6392 
6393 	if (set_rss)
6394 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6395 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6396 	return hwrm_req_send(bp, req);
6397 }
6398 
6399 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6400 				     struct bnxt_vnic_info *vnic, bool set_rss)
6401 {
6402 	struct hwrm_vnic_rss_cfg_input *req;
6403 	dma_addr_t ring_tbl_map;
6404 	u32 i, nr_ctxs;
6405 	int rc;
6406 
6407 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6408 	if (rc)
6409 		return rc;
6410 
6411 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6412 	if (!set_rss)
6413 		return hwrm_req_send(bp, req);
6414 
6415 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6416 	ring_tbl_map = vnic->rss_table_dma_addr;
6417 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6418 
6419 	hwrm_req_hold(bp, req);
6420 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6421 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6422 		req->ring_table_pair_index = i;
6423 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6424 		rc = hwrm_req_send(bp, req);
6425 		if (rc)
6426 			goto exit;
6427 	}
6428 
6429 exit:
6430 	hwrm_req_drop(bp, req);
6431 	return rc;
6432 }
6433 
6434 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6435 {
6436 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6437 	struct hwrm_vnic_rss_qcfg_output *resp;
6438 	struct hwrm_vnic_rss_qcfg_input *req;
6439 
6440 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6441 		return;
6442 
6443 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6444 	/* all contexts configured to same hash_type, zero always exists */
6445 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6446 	resp = hwrm_req_hold(bp, req);
6447 	if (!hwrm_req_send(bp, req)) {
6448 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6449 		bp->rss_hash_delta = 0;
6450 	}
6451 	hwrm_req_drop(bp, req);
6452 }
6453 
6454 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6455 {
6456 	struct hwrm_vnic_plcmodes_cfg_input *req;
6457 	int rc;
6458 
6459 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6460 	if (rc)
6461 		return rc;
6462 
6463 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6464 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6465 
6466 	if (BNXT_RX_PAGE_MODE(bp)) {
6467 		req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6468 	} else {
6469 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6470 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6471 		req->enables |=
6472 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6473 		req->jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
6474 		req->hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
6475 	}
6476 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6477 	return hwrm_req_send(bp, req);
6478 }
6479 
6480 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6481 					struct bnxt_vnic_info *vnic,
6482 					u16 ctx_idx)
6483 {
6484 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6485 
6486 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6487 		return;
6488 
6489 	req->rss_cos_lb_ctx_id =
6490 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6491 
6492 	hwrm_req_send(bp, req);
6493 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6494 }
6495 
6496 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6497 {
6498 	int i, j;
6499 
6500 	for (i = 0; i < bp->nr_vnics; i++) {
6501 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6502 
6503 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6504 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6505 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6506 		}
6507 	}
6508 	bp->rsscos_nr_ctxs = 0;
6509 }
6510 
6511 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6512 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6513 {
6514 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6515 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6516 	int rc;
6517 
6518 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6519 	if (rc)
6520 		return rc;
6521 
6522 	resp = hwrm_req_hold(bp, req);
6523 	rc = hwrm_req_send(bp, req);
6524 	if (!rc)
6525 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6526 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6527 	hwrm_req_drop(bp, req);
6528 
6529 	return rc;
6530 }
6531 
6532 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6533 {
6534 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6535 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6536 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6537 }
6538 
6539 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6540 {
6541 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6542 	struct hwrm_vnic_cfg_input *req;
6543 	unsigned int ring = 0, grp_idx;
6544 	u16 def_vlan = 0;
6545 	int rc;
6546 
6547 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6548 	if (rc)
6549 		return rc;
6550 
6551 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6552 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6553 
6554 		req->default_rx_ring_id =
6555 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6556 		req->default_cmpl_ring_id =
6557 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6558 		req->enables =
6559 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6560 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6561 		goto vnic_mru;
6562 	}
6563 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6564 	/* Only RSS support for now TBD: COS & LB */
6565 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6566 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6567 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6568 					   VNIC_CFG_REQ_ENABLES_MRU);
6569 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6570 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6571 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6572 					   VNIC_CFG_REQ_ENABLES_MRU);
6573 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6574 	} else {
6575 		req->rss_rule = cpu_to_le16(0xffff);
6576 	}
6577 
6578 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6579 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6580 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6581 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6582 	} else {
6583 		req->cos_rule = cpu_to_le16(0xffff);
6584 	}
6585 
6586 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6587 		ring = 0;
6588 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6589 		ring = vnic->vnic_id - 1;
6590 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6591 		ring = bp->rx_nr_rings - 1;
6592 
6593 	grp_idx = bp->rx_ring[ring].bnapi->index;
6594 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6595 	req->lb_rule = cpu_to_le16(0xffff);
6596 vnic_mru:
6597 	req->mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + VLAN_HLEN);
6598 
6599 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6600 #ifdef CONFIG_BNXT_SRIOV
6601 	if (BNXT_VF(bp))
6602 		def_vlan = bp->vf.vlan;
6603 #endif
6604 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6605 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6606 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6607 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6608 
6609 	return hwrm_req_send(bp, req);
6610 }
6611 
6612 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6613 				    struct bnxt_vnic_info *vnic)
6614 {
6615 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6616 		struct hwrm_vnic_free_input *req;
6617 
6618 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6619 			return;
6620 
6621 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6622 
6623 		hwrm_req_send(bp, req);
6624 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6625 	}
6626 }
6627 
6628 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6629 {
6630 	u16 i;
6631 
6632 	for (i = 0; i < bp->nr_vnics; i++)
6633 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6634 }
6635 
6636 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6637 			 unsigned int start_rx_ring_idx,
6638 			 unsigned int nr_rings)
6639 {
6640 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6641 	struct hwrm_vnic_alloc_output *resp;
6642 	struct hwrm_vnic_alloc_input *req;
6643 	int rc;
6644 
6645 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6646 	if (rc)
6647 		return rc;
6648 
6649 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6650 		goto vnic_no_ring_grps;
6651 
6652 	/* map ring groups to this vnic */
6653 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6654 		grp_idx = bp->rx_ring[i].bnapi->index;
6655 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6656 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6657 				   j, nr_rings);
6658 			break;
6659 		}
6660 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6661 	}
6662 
6663 vnic_no_ring_grps:
6664 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6665 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6666 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6667 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6668 
6669 	resp = hwrm_req_hold(bp, req);
6670 	rc = hwrm_req_send(bp, req);
6671 	if (!rc)
6672 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6673 	hwrm_req_drop(bp, req);
6674 	return rc;
6675 }
6676 
6677 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6678 {
6679 	struct hwrm_vnic_qcaps_output *resp;
6680 	struct hwrm_vnic_qcaps_input *req;
6681 	int rc;
6682 
6683 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6684 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6685 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6686 	if (bp->hwrm_spec_code < 0x10600)
6687 		return 0;
6688 
6689 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6690 	if (rc)
6691 		return rc;
6692 
6693 	resp = hwrm_req_hold(bp, req);
6694 	rc = hwrm_req_send(bp, req);
6695 	if (!rc) {
6696 		u32 flags = le32_to_cpu(resp->flags);
6697 
6698 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6699 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6700 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6701 		if (flags &
6702 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6703 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6704 
6705 		/* Older P5 fw before EXT_HW_STATS support did not set
6706 		 * VLAN_STRIP_CAP properly.
6707 		 */
6708 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6709 		    (BNXT_CHIP_P5(bp) &&
6710 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6711 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6712 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6713 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6714 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6715 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6716 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6717 		if (bp->max_tpa_v2) {
6718 			if (BNXT_CHIP_P5(bp))
6719 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6720 			else
6721 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6722 		}
6723 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6724 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6725 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6726 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6727 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6728 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6729 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6730 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6731 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6732 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6733 	}
6734 	hwrm_req_drop(bp, req);
6735 	return rc;
6736 }
6737 
6738 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6739 {
6740 	struct hwrm_ring_grp_alloc_output *resp;
6741 	struct hwrm_ring_grp_alloc_input *req;
6742 	int rc;
6743 	u16 i;
6744 
6745 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6746 		return 0;
6747 
6748 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6749 	if (rc)
6750 		return rc;
6751 
6752 	resp = hwrm_req_hold(bp, req);
6753 	for (i = 0; i < bp->rx_nr_rings; i++) {
6754 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
6755 
6756 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
6757 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
6758 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
6759 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
6760 
6761 		rc = hwrm_req_send(bp, req);
6762 
6763 		if (rc)
6764 			break;
6765 
6766 		bp->grp_info[grp_idx].fw_grp_id =
6767 			le32_to_cpu(resp->ring_group_id);
6768 	}
6769 	hwrm_req_drop(bp, req);
6770 	return rc;
6771 }
6772 
6773 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
6774 {
6775 	struct hwrm_ring_grp_free_input *req;
6776 	u16 i;
6777 
6778 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
6779 		return;
6780 
6781 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
6782 		return;
6783 
6784 	hwrm_req_hold(bp, req);
6785 	for (i = 0; i < bp->cp_nr_rings; i++) {
6786 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
6787 			continue;
6788 		req->ring_group_id =
6789 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
6790 
6791 		hwrm_req_send(bp, req);
6792 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
6793 	}
6794 	hwrm_req_drop(bp, req);
6795 }
6796 
6797 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
6798 				    struct bnxt_ring_struct *ring,
6799 				    u32 ring_type, u32 map_index)
6800 {
6801 	struct hwrm_ring_alloc_output *resp;
6802 	struct hwrm_ring_alloc_input *req;
6803 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
6804 	struct bnxt_ring_grp_info *grp_info;
6805 	int rc, err = 0;
6806 	u16 ring_id;
6807 
6808 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
6809 	if (rc)
6810 		goto exit;
6811 
6812 	req->enables = 0;
6813 	if (rmem->nr_pages > 1) {
6814 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
6815 		/* Page size is in log2 units */
6816 		req->page_size = BNXT_PAGE_SHIFT;
6817 		req->page_tbl_depth = 1;
6818 	} else {
6819 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
6820 	}
6821 	req->fbo = 0;
6822 	/* Association of ring index with doorbell index and MSIX number */
6823 	req->logical_id = cpu_to_le16(map_index);
6824 
6825 	switch (ring_type) {
6826 	case HWRM_RING_ALLOC_TX: {
6827 		struct bnxt_tx_ring_info *txr;
6828 		u16 flags = 0;
6829 
6830 		txr = container_of(ring, struct bnxt_tx_ring_info,
6831 				   tx_ring_struct);
6832 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
6833 		/* Association of transmit ring with completion ring */
6834 		grp_info = &bp->grp_info[ring->grp_idx];
6835 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
6836 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
6837 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6838 		req->queue_id = cpu_to_le16(ring->queue_id);
6839 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
6840 			req->cmpl_coal_cnt =
6841 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
6842 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
6843 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
6844 		req->flags = cpu_to_le16(flags);
6845 		break;
6846 	}
6847 	case HWRM_RING_ALLOC_RX:
6848 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6849 		req->length = cpu_to_le32(bp->rx_ring_mask + 1);
6850 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6851 			u16 flags = 0;
6852 
6853 			/* Association of rx ring with stats context */
6854 			grp_info = &bp->grp_info[ring->grp_idx];
6855 			req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
6856 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6857 			req->enables |= cpu_to_le32(
6858 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6859 			if (NET_IP_ALIGN == 2)
6860 				flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
6861 			req->flags = cpu_to_le16(flags);
6862 		}
6863 		break;
6864 	case HWRM_RING_ALLOC_AGG:
6865 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6866 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
6867 			/* Association of agg ring with rx ring */
6868 			grp_info = &bp->grp_info[ring->grp_idx];
6869 			req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
6870 			req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
6871 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6872 			req->enables |= cpu_to_le32(
6873 				RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
6874 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6875 		} else {
6876 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6877 		}
6878 		req->length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
6879 		break;
6880 	case HWRM_RING_ALLOC_CMPL:
6881 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
6882 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
6883 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6884 			/* Association of cp ring with nq */
6885 			grp_info = &bp->grp_info[map_index];
6886 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
6887 			req->cq_handle = cpu_to_le64(ring->handle);
6888 			req->enables |= cpu_to_le32(
6889 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
6890 		} else if (bp->flags & BNXT_FLAG_USING_MSIX) {
6891 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
6892 		}
6893 		break;
6894 	case HWRM_RING_ALLOC_NQ:
6895 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
6896 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
6897 		if (bp->flags & BNXT_FLAG_USING_MSIX)
6898 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
6899 		break;
6900 	default:
6901 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
6902 			   ring_type);
6903 		return -1;
6904 	}
6905 
6906 	resp = hwrm_req_hold(bp, req);
6907 	rc = hwrm_req_send(bp, req);
6908 	err = le16_to_cpu(resp->error_code);
6909 	ring_id = le16_to_cpu(resp->ring_id);
6910 	hwrm_req_drop(bp, req);
6911 
6912 exit:
6913 	if (rc || err) {
6914 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
6915 			   ring_type, rc, err);
6916 		return -EIO;
6917 	}
6918 	ring->fw_ring_id = ring_id;
6919 	return rc;
6920 }
6921 
6922 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
6923 {
6924 	int rc;
6925 
6926 	if (BNXT_PF(bp)) {
6927 		struct hwrm_func_cfg_input *req;
6928 
6929 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
6930 		if (rc)
6931 			return rc;
6932 
6933 		req->fid = cpu_to_le16(0xffff);
6934 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
6935 		req->async_event_cr = cpu_to_le16(idx);
6936 		return hwrm_req_send(bp, req);
6937 	} else {
6938 		struct hwrm_func_vf_cfg_input *req;
6939 
6940 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
6941 		if (rc)
6942 			return rc;
6943 
6944 		req->enables =
6945 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
6946 		req->async_event_cr = cpu_to_le16(idx);
6947 		return hwrm_req_send(bp, req);
6948 	}
6949 }
6950 
6951 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
6952 			     u32 ring_type)
6953 {
6954 	switch (ring_type) {
6955 	case HWRM_RING_ALLOC_TX:
6956 		db->db_ring_mask = bp->tx_ring_mask;
6957 		break;
6958 	case HWRM_RING_ALLOC_RX:
6959 		db->db_ring_mask = bp->rx_ring_mask;
6960 		break;
6961 	case HWRM_RING_ALLOC_AGG:
6962 		db->db_ring_mask = bp->rx_agg_ring_mask;
6963 		break;
6964 	case HWRM_RING_ALLOC_CMPL:
6965 	case HWRM_RING_ALLOC_NQ:
6966 		db->db_ring_mask = bp->cp_ring_mask;
6967 		break;
6968 	}
6969 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
6970 		db->db_epoch_mask = db->db_ring_mask + 1;
6971 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
6972 	}
6973 }
6974 
6975 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
6976 			u32 map_idx, u32 xid)
6977 {
6978 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6979 		switch (ring_type) {
6980 		case HWRM_RING_ALLOC_TX:
6981 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
6982 			break;
6983 		case HWRM_RING_ALLOC_RX:
6984 		case HWRM_RING_ALLOC_AGG:
6985 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
6986 			break;
6987 		case HWRM_RING_ALLOC_CMPL:
6988 			db->db_key64 = DBR_PATH_L2;
6989 			break;
6990 		case HWRM_RING_ALLOC_NQ:
6991 			db->db_key64 = DBR_PATH_L2;
6992 			break;
6993 		}
6994 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
6995 
6996 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6997 			db->db_key64 |= DBR_VALID;
6998 
6999 		db->doorbell = bp->bar1 + bp->db_offset;
7000 	} else {
7001 		db->doorbell = bp->bar1 + map_idx * 0x80;
7002 		switch (ring_type) {
7003 		case HWRM_RING_ALLOC_TX:
7004 			db->db_key32 = DB_KEY_TX;
7005 			break;
7006 		case HWRM_RING_ALLOC_RX:
7007 		case HWRM_RING_ALLOC_AGG:
7008 			db->db_key32 = DB_KEY_RX;
7009 			break;
7010 		case HWRM_RING_ALLOC_CMPL:
7011 			db->db_key32 = DB_KEY_CP;
7012 			break;
7013 		}
7014 	}
7015 	bnxt_set_db_mask(bp, db, ring_type);
7016 }
7017 
7018 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7019 				   struct bnxt_rx_ring_info *rxr)
7020 {
7021 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7022 	struct bnxt_napi *bnapi = rxr->bnapi;
7023 	u32 type = HWRM_RING_ALLOC_RX;
7024 	u32 map_idx = bnapi->index;
7025 	int rc;
7026 
7027 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7028 	if (rc)
7029 		return rc;
7030 
7031 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7032 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7033 
7034 	return 0;
7035 }
7036 
7037 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7038 				       struct bnxt_rx_ring_info *rxr)
7039 {
7040 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7041 	u32 type = HWRM_RING_ALLOC_AGG;
7042 	u32 grp_idx = ring->grp_idx;
7043 	u32 map_idx;
7044 	int rc;
7045 
7046 	map_idx = grp_idx + bp->rx_nr_rings;
7047 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7048 	if (rc)
7049 		return rc;
7050 
7051 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7052 		    ring->fw_ring_id);
7053 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7054 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7055 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7056 
7057 	return 0;
7058 }
7059 
7060 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7061 {
7062 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7063 	int i, rc = 0;
7064 	u32 type;
7065 
7066 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7067 		type = HWRM_RING_ALLOC_NQ;
7068 	else
7069 		type = HWRM_RING_ALLOC_CMPL;
7070 	for (i = 0; i < bp->cp_nr_rings; i++) {
7071 		struct bnxt_napi *bnapi = bp->bnapi[i];
7072 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7073 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7074 		u32 map_idx = ring->map_idx;
7075 		unsigned int vector;
7076 
7077 		vector = bp->irq_tbl[map_idx].vector;
7078 		disable_irq_nosync(vector);
7079 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7080 		if (rc) {
7081 			enable_irq(vector);
7082 			goto err_out;
7083 		}
7084 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7085 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7086 		enable_irq(vector);
7087 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7088 
7089 		if (!i) {
7090 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7091 			if (rc)
7092 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7093 		}
7094 	}
7095 
7096 	type = HWRM_RING_ALLOC_TX;
7097 	for (i = 0; i < bp->tx_nr_rings; i++) {
7098 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7099 		struct bnxt_ring_struct *ring;
7100 		u32 map_idx;
7101 
7102 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7103 			struct bnxt_cp_ring_info *cpr2 = txr->tx_cpr;
7104 			struct bnxt_napi *bnapi = txr->bnapi;
7105 			u32 type2 = HWRM_RING_ALLOC_CMPL;
7106 
7107 			ring = &cpr2->cp_ring_struct;
7108 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
7109 			map_idx = bnapi->index;
7110 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
7111 			if (rc)
7112 				goto err_out;
7113 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
7114 				    ring->fw_ring_id);
7115 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
7116 		}
7117 		ring = &txr->tx_ring_struct;
7118 		map_idx = i;
7119 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7120 		if (rc)
7121 			goto err_out;
7122 		bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
7123 	}
7124 
7125 	for (i = 0; i < bp->rx_nr_rings; i++) {
7126 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7127 
7128 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7129 		if (rc)
7130 			goto err_out;
7131 		/* If we have agg rings, post agg buffers first. */
7132 		if (!agg_rings)
7133 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7134 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7135 			struct bnxt_cp_ring_info *cpr2 = rxr->rx_cpr;
7136 			struct bnxt_napi *bnapi = rxr->bnapi;
7137 			u32 type2 = HWRM_RING_ALLOC_CMPL;
7138 			struct bnxt_ring_struct *ring;
7139 			u32 map_idx = bnapi->index;
7140 
7141 			ring = &cpr2->cp_ring_struct;
7142 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
7143 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
7144 			if (rc)
7145 				goto err_out;
7146 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
7147 				    ring->fw_ring_id);
7148 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
7149 		}
7150 	}
7151 
7152 	if (agg_rings) {
7153 		for (i = 0; i < bp->rx_nr_rings; i++) {
7154 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7155 			if (rc)
7156 				goto err_out;
7157 		}
7158 	}
7159 err_out:
7160 	return rc;
7161 }
7162 
7163 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7164 				   struct bnxt_ring_struct *ring,
7165 				   u32 ring_type, int cmpl_ring_id)
7166 {
7167 	struct hwrm_ring_free_output *resp;
7168 	struct hwrm_ring_free_input *req;
7169 	u16 error_code = 0;
7170 	int rc;
7171 
7172 	if (BNXT_NO_FW_ACCESS(bp))
7173 		return 0;
7174 
7175 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7176 	if (rc)
7177 		goto exit;
7178 
7179 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7180 	req->ring_type = ring_type;
7181 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7182 
7183 	resp = hwrm_req_hold(bp, req);
7184 	rc = hwrm_req_send(bp, req);
7185 	error_code = le16_to_cpu(resp->error_code);
7186 	hwrm_req_drop(bp, req);
7187 exit:
7188 	if (rc || error_code) {
7189 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7190 			   ring_type, rc, error_code);
7191 		return -EIO;
7192 	}
7193 	return 0;
7194 }
7195 
7196 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7197 				   struct bnxt_rx_ring_info *rxr,
7198 				   bool close_path)
7199 {
7200 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7201 	u32 grp_idx = rxr->bnapi->index;
7202 	u32 cmpl_ring_id;
7203 
7204 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7205 		return;
7206 
7207 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7208 	hwrm_ring_free_send_msg(bp, ring,
7209 				RING_FREE_REQ_RING_TYPE_RX,
7210 				close_path ? cmpl_ring_id :
7211 				INVALID_HW_RING_ID);
7212 	ring->fw_ring_id = INVALID_HW_RING_ID;
7213 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7214 }
7215 
7216 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7217 				       struct bnxt_rx_ring_info *rxr,
7218 				       bool close_path)
7219 {
7220 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7221 	u32 grp_idx = rxr->bnapi->index;
7222 	u32 type, cmpl_ring_id;
7223 
7224 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7225 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7226 	else
7227 		type = RING_FREE_REQ_RING_TYPE_RX;
7228 
7229 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7230 		return;
7231 
7232 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7233 	hwrm_ring_free_send_msg(bp, ring, type,
7234 				close_path ? cmpl_ring_id :
7235 				INVALID_HW_RING_ID);
7236 	ring->fw_ring_id = INVALID_HW_RING_ID;
7237 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7238 }
7239 
7240 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7241 {
7242 	u32 type;
7243 	int i;
7244 
7245 	if (!bp->bnapi)
7246 		return;
7247 
7248 	for (i = 0; i < bp->tx_nr_rings; i++) {
7249 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7250 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7251 
7252 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7253 			u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
7254 
7255 			hwrm_ring_free_send_msg(bp, ring,
7256 						RING_FREE_REQ_RING_TYPE_TX,
7257 						close_path ? cmpl_ring_id :
7258 						INVALID_HW_RING_ID);
7259 			ring->fw_ring_id = INVALID_HW_RING_ID;
7260 		}
7261 	}
7262 
7263 	for (i = 0; i < bp->rx_nr_rings; i++) {
7264 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7265 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7266 	}
7267 
7268 	/* The completion rings are about to be freed.  After that the
7269 	 * IRQ doorbell will not work anymore.  So we need to disable
7270 	 * IRQ here.
7271 	 */
7272 	bnxt_disable_int_sync(bp);
7273 
7274 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7275 		type = RING_FREE_REQ_RING_TYPE_NQ;
7276 	else
7277 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7278 	for (i = 0; i < bp->cp_nr_rings; i++) {
7279 		struct bnxt_napi *bnapi = bp->bnapi[i];
7280 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7281 		struct bnxt_ring_struct *ring;
7282 		int j;
7283 
7284 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++) {
7285 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
7286 
7287 			ring = &cpr2->cp_ring_struct;
7288 			if (ring->fw_ring_id == INVALID_HW_RING_ID)
7289 				continue;
7290 			hwrm_ring_free_send_msg(bp, ring,
7291 						RING_FREE_REQ_RING_TYPE_L2_CMPL,
7292 						INVALID_HW_RING_ID);
7293 			ring->fw_ring_id = INVALID_HW_RING_ID;
7294 		}
7295 		ring = &cpr->cp_ring_struct;
7296 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7297 			hwrm_ring_free_send_msg(bp, ring, type,
7298 						INVALID_HW_RING_ID);
7299 			ring->fw_ring_id = INVALID_HW_RING_ID;
7300 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7301 		}
7302 	}
7303 }
7304 
7305 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7306 			     bool shared);
7307 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7308 			   bool shared);
7309 
7310 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7311 {
7312 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7313 	struct hwrm_func_qcfg_output *resp;
7314 	struct hwrm_func_qcfg_input *req;
7315 	int rc;
7316 
7317 	if (bp->hwrm_spec_code < 0x10601)
7318 		return 0;
7319 
7320 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7321 	if (rc)
7322 		return rc;
7323 
7324 	req->fid = cpu_to_le16(0xffff);
7325 	resp = hwrm_req_hold(bp, req);
7326 	rc = hwrm_req_send(bp, req);
7327 	if (rc) {
7328 		hwrm_req_drop(bp, req);
7329 		return rc;
7330 	}
7331 
7332 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7333 	if (BNXT_NEW_RM(bp)) {
7334 		u16 cp, stats;
7335 
7336 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7337 		hw_resc->resv_hw_ring_grps =
7338 			le32_to_cpu(resp->alloc_hw_ring_grps);
7339 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7340 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7341 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7342 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7343 		hw_resc->resv_irqs = cp;
7344 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7345 			int rx = hw_resc->resv_rx_rings;
7346 			int tx = hw_resc->resv_tx_rings;
7347 
7348 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7349 				rx >>= 1;
7350 			if (cp < (rx + tx)) {
7351 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7352 				if (rc)
7353 					goto get_rings_exit;
7354 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7355 					rx <<= 1;
7356 				hw_resc->resv_rx_rings = rx;
7357 				hw_resc->resv_tx_rings = tx;
7358 			}
7359 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7360 			hw_resc->resv_hw_ring_grps = rx;
7361 		}
7362 		hw_resc->resv_cp_rings = cp;
7363 		hw_resc->resv_stat_ctxs = stats;
7364 	}
7365 get_rings_exit:
7366 	hwrm_req_drop(bp, req);
7367 	return rc;
7368 }
7369 
7370 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7371 {
7372 	struct hwrm_func_qcfg_output *resp;
7373 	struct hwrm_func_qcfg_input *req;
7374 	int rc;
7375 
7376 	if (bp->hwrm_spec_code < 0x10601)
7377 		return 0;
7378 
7379 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7380 	if (rc)
7381 		return rc;
7382 
7383 	req->fid = cpu_to_le16(fid);
7384 	resp = hwrm_req_hold(bp, req);
7385 	rc = hwrm_req_send(bp, req);
7386 	if (!rc)
7387 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7388 
7389 	hwrm_req_drop(bp, req);
7390 	return rc;
7391 }
7392 
7393 static bool bnxt_rfs_supported(struct bnxt *bp);
7394 
7395 static struct hwrm_func_cfg_input *
7396 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7397 {
7398 	struct hwrm_func_cfg_input *req;
7399 	u32 enables = 0;
7400 
7401 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7402 		return NULL;
7403 
7404 	req->fid = cpu_to_le16(0xffff);
7405 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7406 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7407 	if (BNXT_NEW_RM(bp)) {
7408 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7409 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7410 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7411 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7412 			enables |= hwr->cp_p5 ?
7413 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7414 		} else {
7415 			enables |= hwr->cp ?
7416 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7417 			enables |= hwr->grp ?
7418 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7419 		}
7420 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7421 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7422 					  0;
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 			req->num_msix = cpu_to_le16(hwr->cp);
7428 		} else {
7429 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7430 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7431 		}
7432 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7433 		req->num_vnics = cpu_to_le16(hwr->vnic);
7434 	}
7435 	req->enables = cpu_to_le32(enables);
7436 	return req;
7437 }
7438 
7439 static struct hwrm_func_vf_cfg_input *
7440 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7441 {
7442 	struct hwrm_func_vf_cfg_input *req;
7443 	u32 enables = 0;
7444 
7445 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7446 		return NULL;
7447 
7448 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7449 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7450 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7451 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7452 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7453 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7454 		enables |= hwr->cp_p5 ?
7455 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7456 	} else {
7457 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7458 		enables |= hwr->grp ?
7459 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7460 	}
7461 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7462 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7463 
7464 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7465 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7466 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7467 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7468 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7469 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7470 	} else {
7471 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7472 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7473 	}
7474 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7475 	req->num_vnics = cpu_to_le16(hwr->vnic);
7476 
7477 	req->enables = cpu_to_le32(enables);
7478 	return req;
7479 }
7480 
7481 static int
7482 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7483 {
7484 	struct hwrm_func_cfg_input *req;
7485 	int rc;
7486 
7487 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7488 	if (!req)
7489 		return -ENOMEM;
7490 
7491 	if (!req->enables) {
7492 		hwrm_req_drop(bp, req);
7493 		return 0;
7494 	}
7495 
7496 	rc = hwrm_req_send(bp, req);
7497 	if (rc)
7498 		return rc;
7499 
7500 	if (bp->hwrm_spec_code < 0x10601)
7501 		bp->hw_resc.resv_tx_rings = hwr->tx;
7502 
7503 	return bnxt_hwrm_get_rings(bp);
7504 }
7505 
7506 static int
7507 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7508 {
7509 	struct hwrm_func_vf_cfg_input *req;
7510 	int rc;
7511 
7512 	if (!BNXT_NEW_RM(bp)) {
7513 		bp->hw_resc.resv_tx_rings = hwr->tx;
7514 		return 0;
7515 	}
7516 
7517 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7518 	if (!req)
7519 		return -ENOMEM;
7520 
7521 	rc = hwrm_req_send(bp, req);
7522 	if (rc)
7523 		return rc;
7524 
7525 	return bnxt_hwrm_get_rings(bp);
7526 }
7527 
7528 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7529 {
7530 	if (BNXT_PF(bp))
7531 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7532 	else
7533 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7534 }
7535 
7536 int bnxt_nq_rings_in_use(struct bnxt *bp)
7537 {
7538 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7539 }
7540 
7541 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7542 {
7543 	int cp;
7544 
7545 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7546 		return bnxt_nq_rings_in_use(bp);
7547 
7548 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7549 	return cp;
7550 }
7551 
7552 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7553 {
7554 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7555 }
7556 
7557 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7558 {
7559 	if (!hwr->grp)
7560 		return 0;
7561 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7562 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7563 
7564 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7565 			rss_ctx *= hwr->vnic;
7566 		return rss_ctx;
7567 	}
7568 	if (BNXT_VF(bp))
7569 		return BNXT_VF_MAX_RSS_CTX;
7570 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7571 		return hwr->grp + 1;
7572 	return 1;
7573 }
7574 
7575 /* Check if a default RSS map needs to be setup.  This function is only
7576  * used on older firmware that does not require reserving RX rings.
7577  */
7578 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7579 {
7580 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7581 
7582 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7583 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7584 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7585 		if (!netif_is_rxfh_configured(bp->dev))
7586 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7587 	}
7588 }
7589 
7590 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7591 {
7592 	if (bp->flags & BNXT_FLAG_RFS) {
7593 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7594 			return 2 + bp->num_rss_ctx;
7595 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7596 			return rx_rings + 1;
7597 	}
7598 	return 1;
7599 }
7600 
7601 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7602 {
7603 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7604 	int cp = bnxt_cp_rings_in_use(bp);
7605 	int nq = bnxt_nq_rings_in_use(bp);
7606 	int rx = bp->rx_nr_rings, stat;
7607 	int vnic, grp = rx;
7608 
7609 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7610 	    bp->hwrm_spec_code >= 0x10601)
7611 		return true;
7612 
7613 	/* Old firmware does not need RX ring reservations but we still
7614 	 * need to setup a default RSS map when needed.  With new firmware
7615 	 * we go through RX ring reservations first and then set up the
7616 	 * RSS map for the successfully reserved RX rings when needed.
7617 	 */
7618 	if (!BNXT_NEW_RM(bp)) {
7619 		bnxt_check_rss_tbl_no_rmgr(bp);
7620 		return false;
7621 	}
7622 
7623 	vnic = bnxt_get_total_vnics(bp, rx);
7624 
7625 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7626 		rx <<= 1;
7627 	stat = bnxt_get_func_stat_ctxs(bp);
7628 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7629 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7630 	    (hw_resc->resv_hw_ring_grps != grp &&
7631 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7632 		return true;
7633 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7634 	    hw_resc->resv_irqs != nq)
7635 		return true;
7636 	return false;
7637 }
7638 
7639 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7640 {
7641 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7642 
7643 	hwr->tx = hw_resc->resv_tx_rings;
7644 	if (BNXT_NEW_RM(bp)) {
7645 		hwr->rx = hw_resc->resv_rx_rings;
7646 		hwr->cp = hw_resc->resv_irqs;
7647 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7648 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7649 		hwr->grp = hw_resc->resv_hw_ring_grps;
7650 		hwr->vnic = hw_resc->resv_vnics;
7651 		hwr->stat = hw_resc->resv_stat_ctxs;
7652 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7653 	}
7654 }
7655 
7656 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7657 {
7658 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7659 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7660 }
7661 
7662 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
7663 
7664 static int __bnxt_reserve_rings(struct bnxt *bp)
7665 {
7666 	struct bnxt_hw_rings hwr = {0};
7667 	int cp = bp->cp_nr_rings;
7668 	int rx_rings, rc;
7669 	int ulp_msix = 0;
7670 	bool sh = false;
7671 	int tx_cp;
7672 
7673 	if (!bnxt_need_reserve_rings(bp))
7674 		return 0;
7675 
7676 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
7677 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
7678 		if (!ulp_msix)
7679 			bnxt_set_ulp_stat_ctxs(bp, 0);
7680 
7681 		if (ulp_msix > bp->ulp_num_msix_want)
7682 			ulp_msix = bp->ulp_num_msix_want;
7683 		hwr.cp = cp + ulp_msix;
7684 	} else {
7685 		hwr.cp = bnxt_nq_rings_in_use(bp);
7686 	}
7687 
7688 	hwr.tx = bp->tx_nr_rings;
7689 	hwr.rx = bp->rx_nr_rings;
7690 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7691 		sh = true;
7692 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7693 		hwr.cp_p5 = hwr.rx + hwr.tx;
7694 
7695 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7696 
7697 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7698 		hwr.rx <<= 1;
7699 	hwr.grp = bp->rx_nr_rings;
7700 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7701 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
7702 
7703 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
7704 	if (rc)
7705 		return rc;
7706 
7707 	bnxt_copy_reserved_rings(bp, &hwr);
7708 
7709 	rx_rings = hwr.rx;
7710 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7711 		if (hwr.rx >= 2) {
7712 			rx_rings = hwr.rx >> 1;
7713 		} else {
7714 			if (netif_running(bp->dev))
7715 				return -ENOMEM;
7716 
7717 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7718 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7719 			bp->dev->hw_features &= ~NETIF_F_LRO;
7720 			bp->dev->features &= ~NETIF_F_LRO;
7721 			bnxt_set_ring_params(bp);
7722 		}
7723 	}
7724 	rx_rings = min_t(int, rx_rings, hwr.grp);
7725 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
7726 	if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
7727 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
7728 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
7729 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
7730 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7731 		hwr.rx = rx_rings << 1;
7732 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
7733 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
7734 	bp->tx_nr_rings = hwr.tx;
7735 
7736 	/* If we cannot reserve all the RX rings, reset the RSS map only
7737 	 * if absolutely necessary
7738 	 */
7739 	if (rx_rings != bp->rx_nr_rings) {
7740 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
7741 			    rx_rings, bp->rx_nr_rings);
7742 		if (netif_is_rxfh_configured(bp->dev) &&
7743 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
7744 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
7745 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
7746 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
7747 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
7748 		}
7749 	}
7750 	bp->rx_nr_rings = rx_rings;
7751 	bp->cp_nr_rings = hwr.cp;
7752 
7753 	if (!bnxt_rings_ok(bp, &hwr))
7754 		return -ENOMEM;
7755 
7756 	if (!netif_is_rxfh_configured(bp->dev))
7757 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7758 
7759 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
7760 		int resv_msix, resv_ctx, ulp_ctxs;
7761 		struct bnxt_hw_resc *hw_resc;
7762 
7763 		hw_resc = &bp->hw_resc;
7764 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
7765 		ulp_msix = min_t(int, resv_msix, ulp_msix);
7766 		bnxt_set_ulp_msix_num(bp, ulp_msix);
7767 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
7768 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
7769 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
7770 	}
7771 
7772 	return rc;
7773 }
7774 
7775 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7776 {
7777 	struct hwrm_func_vf_cfg_input *req;
7778 	u32 flags;
7779 
7780 	if (!BNXT_NEW_RM(bp))
7781 		return 0;
7782 
7783 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7784 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
7785 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7786 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7787 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7788 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
7789 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
7790 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7791 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7792 
7793 	req->flags = cpu_to_le32(flags);
7794 	return hwrm_req_send_silent(bp, req);
7795 }
7796 
7797 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7798 {
7799 	struct hwrm_func_cfg_input *req;
7800 	u32 flags;
7801 
7802 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7803 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
7804 	if (BNXT_NEW_RM(bp)) {
7805 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7806 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7807 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7808 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
7809 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7810 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
7811 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
7812 		else
7813 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7814 	}
7815 
7816 	req->flags = cpu_to_le32(flags);
7817 	return hwrm_req_send_silent(bp, req);
7818 }
7819 
7820 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7821 {
7822 	if (bp->hwrm_spec_code < 0x10801)
7823 		return 0;
7824 
7825 	if (BNXT_PF(bp))
7826 		return bnxt_hwrm_check_pf_rings(bp, hwr);
7827 
7828 	return bnxt_hwrm_check_vf_rings(bp, hwr);
7829 }
7830 
7831 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
7832 {
7833 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7834 	struct hwrm_ring_aggint_qcaps_output *resp;
7835 	struct hwrm_ring_aggint_qcaps_input *req;
7836 	int rc;
7837 
7838 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
7839 	coal_cap->num_cmpl_dma_aggr_max = 63;
7840 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
7841 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
7842 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
7843 	coal_cap->int_lat_tmr_min_max = 65535;
7844 	coal_cap->int_lat_tmr_max_max = 65535;
7845 	coal_cap->num_cmpl_aggr_int_max = 65535;
7846 	coal_cap->timer_units = 80;
7847 
7848 	if (bp->hwrm_spec_code < 0x10902)
7849 		return;
7850 
7851 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
7852 		return;
7853 
7854 	resp = hwrm_req_hold(bp, req);
7855 	rc = hwrm_req_send_silent(bp, req);
7856 	if (!rc) {
7857 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
7858 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
7859 		coal_cap->num_cmpl_dma_aggr_max =
7860 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
7861 		coal_cap->num_cmpl_dma_aggr_during_int_max =
7862 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
7863 		coal_cap->cmpl_aggr_dma_tmr_max =
7864 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
7865 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
7866 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
7867 		coal_cap->int_lat_tmr_min_max =
7868 			le16_to_cpu(resp->int_lat_tmr_min_max);
7869 		coal_cap->int_lat_tmr_max_max =
7870 			le16_to_cpu(resp->int_lat_tmr_max_max);
7871 		coal_cap->num_cmpl_aggr_int_max =
7872 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
7873 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
7874 	}
7875 	hwrm_req_drop(bp, req);
7876 }
7877 
7878 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
7879 {
7880 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7881 
7882 	return usec * 1000 / coal_cap->timer_units;
7883 }
7884 
7885 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
7886 	struct bnxt_coal *hw_coal,
7887 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7888 {
7889 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7890 	u16 val, tmr, max, flags = hw_coal->flags;
7891 	u32 cmpl_params = coal_cap->cmpl_params;
7892 
7893 	max = hw_coal->bufs_per_record * 128;
7894 	if (hw_coal->budget)
7895 		max = hw_coal->bufs_per_record * hw_coal->budget;
7896 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
7897 
7898 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
7899 	req->num_cmpl_aggr_int = cpu_to_le16(val);
7900 
7901 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
7902 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
7903 
7904 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
7905 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
7906 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
7907 
7908 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
7909 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
7910 	req->int_lat_tmr_max = cpu_to_le16(tmr);
7911 
7912 	/* min timer set to 1/2 of interrupt timer */
7913 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
7914 		val = tmr / 2;
7915 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
7916 		req->int_lat_tmr_min = cpu_to_le16(val);
7917 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
7918 	}
7919 
7920 	/* buf timer set to 1/4 of interrupt timer */
7921 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
7922 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
7923 
7924 	if (cmpl_params &
7925 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
7926 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
7927 		val = clamp_t(u16, tmr, 1,
7928 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
7929 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
7930 		req->enables |=
7931 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
7932 	}
7933 
7934 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
7935 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
7936 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
7937 	req->flags = cpu_to_le16(flags);
7938 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
7939 }
7940 
7941 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
7942 				   struct bnxt_coal *hw_coal)
7943 {
7944 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
7945 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7946 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7947 	u32 nq_params = coal_cap->nq_params;
7948 	u16 tmr;
7949 	int rc;
7950 
7951 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
7952 		return 0;
7953 
7954 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7955 	if (rc)
7956 		return rc;
7957 
7958 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
7959 	req->flags =
7960 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
7961 
7962 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
7963 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
7964 	req->int_lat_tmr_min = cpu_to_le16(tmr);
7965 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
7966 	return hwrm_req_send(bp, req);
7967 }
7968 
7969 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
7970 {
7971 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
7972 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7973 	struct bnxt_coal coal;
7974 	int rc;
7975 
7976 	/* Tick values in micro seconds.
7977 	 * 1 coal_buf x bufs_per_record = 1 completion record.
7978 	 */
7979 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
7980 
7981 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
7982 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
7983 
7984 	if (!bnapi->rx_ring)
7985 		return -ENODEV;
7986 
7987 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7988 	if (rc)
7989 		return rc;
7990 
7991 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
7992 
7993 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
7994 
7995 	return hwrm_req_send(bp, req_rx);
7996 }
7997 
7998 static int
7999 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8000 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8001 {
8002 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8003 
8004 	req->ring_id = cpu_to_le16(ring_id);
8005 	return hwrm_req_send(bp, req);
8006 }
8007 
8008 static int
8009 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8010 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8011 {
8012 	struct bnxt_tx_ring_info *txr;
8013 	int i, rc;
8014 
8015 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8016 		u16 ring_id;
8017 
8018 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8019 		req->ring_id = cpu_to_le16(ring_id);
8020 		rc = hwrm_req_send(bp, req);
8021 		if (rc)
8022 			return rc;
8023 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8024 			return 0;
8025 	}
8026 	return 0;
8027 }
8028 
8029 int bnxt_hwrm_set_coal(struct bnxt *bp)
8030 {
8031 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8032 	int i, rc;
8033 
8034 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8035 	if (rc)
8036 		return rc;
8037 
8038 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8039 	if (rc) {
8040 		hwrm_req_drop(bp, req_rx);
8041 		return rc;
8042 	}
8043 
8044 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8045 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8046 
8047 	hwrm_req_hold(bp, req_rx);
8048 	hwrm_req_hold(bp, req_tx);
8049 	for (i = 0; i < bp->cp_nr_rings; i++) {
8050 		struct bnxt_napi *bnapi = bp->bnapi[i];
8051 		struct bnxt_coal *hw_coal;
8052 
8053 		if (!bnapi->rx_ring)
8054 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8055 		else
8056 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8057 		if (rc)
8058 			break;
8059 
8060 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8061 			continue;
8062 
8063 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8064 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8065 			if (rc)
8066 				break;
8067 		}
8068 		if (bnapi->rx_ring)
8069 			hw_coal = &bp->rx_coal;
8070 		else
8071 			hw_coal = &bp->tx_coal;
8072 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8073 	}
8074 	hwrm_req_drop(bp, req_rx);
8075 	hwrm_req_drop(bp, req_tx);
8076 	return rc;
8077 }
8078 
8079 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8080 {
8081 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8082 	struct hwrm_stat_ctx_free_input *req;
8083 	int i;
8084 
8085 	if (!bp->bnapi)
8086 		return;
8087 
8088 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8089 		return;
8090 
8091 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8092 		return;
8093 	if (BNXT_FW_MAJ(bp) <= 20) {
8094 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8095 			hwrm_req_drop(bp, req);
8096 			return;
8097 		}
8098 		hwrm_req_hold(bp, req0);
8099 	}
8100 	hwrm_req_hold(bp, req);
8101 	for (i = 0; i < bp->cp_nr_rings; i++) {
8102 		struct bnxt_napi *bnapi = bp->bnapi[i];
8103 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8104 
8105 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8106 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8107 			if (req0) {
8108 				req0->stat_ctx_id = req->stat_ctx_id;
8109 				hwrm_req_send(bp, req0);
8110 			}
8111 			hwrm_req_send(bp, req);
8112 
8113 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8114 		}
8115 	}
8116 	hwrm_req_drop(bp, req);
8117 	if (req0)
8118 		hwrm_req_drop(bp, req0);
8119 }
8120 
8121 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8122 {
8123 	struct hwrm_stat_ctx_alloc_output *resp;
8124 	struct hwrm_stat_ctx_alloc_input *req;
8125 	int rc, i;
8126 
8127 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8128 		return 0;
8129 
8130 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8131 	if (rc)
8132 		return rc;
8133 
8134 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8135 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8136 
8137 	resp = hwrm_req_hold(bp, req);
8138 	for (i = 0; i < bp->cp_nr_rings; i++) {
8139 		struct bnxt_napi *bnapi = bp->bnapi[i];
8140 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8141 
8142 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8143 
8144 		rc = hwrm_req_send(bp, req);
8145 		if (rc)
8146 			break;
8147 
8148 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8149 
8150 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8151 	}
8152 	hwrm_req_drop(bp, req);
8153 	return rc;
8154 }
8155 
8156 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8157 {
8158 	struct hwrm_func_qcfg_output *resp;
8159 	struct hwrm_func_qcfg_input *req;
8160 	u16 flags;
8161 	int rc;
8162 
8163 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8164 	if (rc)
8165 		return rc;
8166 
8167 	req->fid = cpu_to_le16(0xffff);
8168 	resp = hwrm_req_hold(bp, req);
8169 	rc = hwrm_req_send(bp, req);
8170 	if (rc)
8171 		goto func_qcfg_exit;
8172 
8173 #ifdef CONFIG_BNXT_SRIOV
8174 	if (BNXT_VF(bp)) {
8175 		struct bnxt_vf_info *vf = &bp->vf;
8176 
8177 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8178 	} else {
8179 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8180 	}
8181 #endif
8182 	flags = le16_to_cpu(resp->flags);
8183 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8184 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8185 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8186 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8187 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8188 	}
8189 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8190 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8191 
8192 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8193 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8194 
8195 	switch (resp->port_partition_type) {
8196 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8197 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8198 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8199 		bp->port_partition_type = resp->port_partition_type;
8200 		break;
8201 	}
8202 	if (bp->hwrm_spec_code < 0x10707 ||
8203 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8204 		bp->br_mode = BRIDGE_MODE_VEB;
8205 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8206 		bp->br_mode = BRIDGE_MODE_VEPA;
8207 	else
8208 		bp->br_mode = BRIDGE_MODE_UNDEF;
8209 
8210 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8211 	if (!bp->max_mtu)
8212 		bp->max_mtu = BNXT_MAX_MTU;
8213 
8214 	if (bp->db_size)
8215 		goto func_qcfg_exit;
8216 
8217 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8218 	if (BNXT_CHIP_P5(bp)) {
8219 		if (BNXT_PF(bp))
8220 			bp->db_offset = DB_PF_OFFSET_P5;
8221 		else
8222 			bp->db_offset = DB_VF_OFFSET_P5;
8223 	}
8224 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8225 				 1024);
8226 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8227 	    bp->db_size <= bp->db_offset)
8228 		bp->db_size = pci_resource_len(bp->pdev, 2);
8229 
8230 func_qcfg_exit:
8231 	hwrm_req_drop(bp, req);
8232 	return rc;
8233 }
8234 
8235 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8236 				      u8 init_val, u8 init_offset,
8237 				      bool init_mask_set)
8238 {
8239 	ctxm->init_value = init_val;
8240 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8241 	if (init_mask_set)
8242 		ctxm->init_offset = init_offset * 4;
8243 	else
8244 		ctxm->init_value = 0;
8245 }
8246 
8247 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8248 {
8249 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8250 	u16 type;
8251 
8252 	for (type = 0; type < ctx_max; type++) {
8253 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8254 		int n = 1;
8255 
8256 		if (!ctxm->max_entries)
8257 			continue;
8258 
8259 		if (ctxm->instance_bmap)
8260 			n = hweight32(ctxm->instance_bmap);
8261 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8262 		if (!ctxm->pg_info)
8263 			return -ENOMEM;
8264 	}
8265 	return 0;
8266 }
8267 
8268 #define BNXT_CTX_INIT_VALID(flags)	\
8269 	(!!((flags) &			\
8270 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8271 
8272 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8273 {
8274 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8275 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8276 	struct bnxt_ctx_mem_info *ctx;
8277 	u16 type;
8278 	int rc;
8279 
8280 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8281 	if (rc)
8282 		return rc;
8283 
8284 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8285 	if (!ctx)
8286 		return -ENOMEM;
8287 	bp->ctx = ctx;
8288 
8289 	resp = hwrm_req_hold(bp, req);
8290 
8291 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8292 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8293 		u8 init_val, init_off, i;
8294 		__le32 *p;
8295 		u32 flags;
8296 
8297 		req->type = cpu_to_le16(type);
8298 		rc = hwrm_req_send(bp, req);
8299 		if (rc)
8300 			goto ctx_done;
8301 		flags = le32_to_cpu(resp->flags);
8302 		type = le16_to_cpu(resp->next_valid_type);
8303 		if (!(flags & FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_TYPE_VALID))
8304 			continue;
8305 
8306 		ctxm->type = le16_to_cpu(resp->type);
8307 		ctxm->entry_size = le16_to_cpu(resp->entry_size);
8308 		ctxm->flags = flags;
8309 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8310 		ctxm->entry_multiple = resp->entry_multiple;
8311 		ctxm->max_entries = le32_to_cpu(resp->max_num_entries);
8312 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8313 		init_val = resp->ctx_init_value;
8314 		init_off = resp->ctx_init_offset;
8315 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8316 					  BNXT_CTX_INIT_VALID(flags));
8317 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8318 					      BNXT_MAX_SPLIT_ENTRY);
8319 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8320 		     i++, p++)
8321 			ctxm->split[i] = le32_to_cpu(*p);
8322 	}
8323 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8324 
8325 ctx_done:
8326 	hwrm_req_drop(bp, req);
8327 	return rc;
8328 }
8329 
8330 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8331 {
8332 	struct hwrm_func_backing_store_qcaps_output *resp;
8333 	struct hwrm_func_backing_store_qcaps_input *req;
8334 	int rc;
8335 
8336 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx)
8337 		return 0;
8338 
8339 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8340 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8341 
8342 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8343 	if (rc)
8344 		return rc;
8345 
8346 	resp = hwrm_req_hold(bp, req);
8347 	rc = hwrm_req_send_silent(bp, req);
8348 	if (!rc) {
8349 		struct bnxt_ctx_mem_type *ctxm;
8350 		struct bnxt_ctx_mem_info *ctx;
8351 		u8 init_val, init_idx = 0;
8352 		u16 init_mask;
8353 
8354 		ctx = bp->ctx;
8355 		if (!ctx) {
8356 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8357 			if (!ctx) {
8358 				rc = -ENOMEM;
8359 				goto ctx_err;
8360 			}
8361 			bp->ctx = ctx;
8362 		}
8363 		init_val = resp->ctx_kind_initializer;
8364 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8365 
8366 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8367 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8368 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8369 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8370 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8371 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8372 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8373 					  (init_mask & (1 << init_idx++)) != 0);
8374 
8375 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8376 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8377 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8378 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8379 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8380 					  (init_mask & (1 << init_idx++)) != 0);
8381 
8382 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8383 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8384 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8385 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8386 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8387 					  (init_mask & (1 << init_idx++)) != 0);
8388 
8389 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8390 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8391 		ctxm->max_entries = ctxm->vnic_entries +
8392 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8393 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8394 		bnxt_init_ctx_initializer(ctxm, init_val,
8395 					  resp->vnic_init_offset,
8396 					  (init_mask & (1 << init_idx++)) != 0);
8397 
8398 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8399 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8400 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8401 		bnxt_init_ctx_initializer(ctxm, init_val,
8402 					  resp->stat_init_offset,
8403 					  (init_mask & (1 << init_idx++)) != 0);
8404 
8405 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8406 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8407 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8408 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8409 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8410 		if (!ctxm->entry_multiple)
8411 			ctxm->entry_multiple = 1;
8412 
8413 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8414 
8415 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8416 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8417 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8418 		ctxm->mrav_num_entries_units =
8419 			le16_to_cpu(resp->mrav_num_entries_units);
8420 		bnxt_init_ctx_initializer(ctxm, init_val,
8421 					  resp->mrav_init_offset,
8422 					  (init_mask & (1 << init_idx++)) != 0);
8423 
8424 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8425 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8426 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8427 
8428 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8429 		if (!ctx->tqm_fp_rings_count)
8430 			ctx->tqm_fp_rings_count = bp->max_q;
8431 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8432 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8433 
8434 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8435 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8436 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8437 
8438 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8439 	} else {
8440 		rc = 0;
8441 	}
8442 ctx_err:
8443 	hwrm_req_drop(bp, req);
8444 	return rc;
8445 }
8446 
8447 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8448 				  __le64 *pg_dir)
8449 {
8450 	if (!rmem->nr_pages)
8451 		return;
8452 
8453 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8454 	if (rmem->depth >= 1) {
8455 		if (rmem->depth == 2)
8456 			*pg_attr |= 2;
8457 		else
8458 			*pg_attr |= 1;
8459 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8460 	} else {
8461 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8462 	}
8463 }
8464 
8465 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8466 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8467 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8468 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8469 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8470 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8471 
8472 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8473 {
8474 	struct hwrm_func_backing_store_cfg_input *req;
8475 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8476 	struct bnxt_ctx_pg_info *ctx_pg;
8477 	struct bnxt_ctx_mem_type *ctxm;
8478 	void **__req = (void **)&req;
8479 	u32 req_len = sizeof(*req);
8480 	__le32 *num_entries;
8481 	__le64 *pg_dir;
8482 	u32 flags = 0;
8483 	u8 *pg_attr;
8484 	u32 ena;
8485 	int rc;
8486 	int i;
8487 
8488 	if (!ctx)
8489 		return 0;
8490 
8491 	if (req_len > bp->hwrm_max_ext_req_len)
8492 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8493 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8494 	if (rc)
8495 		return rc;
8496 
8497 	req->enables = cpu_to_le32(enables);
8498 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8499 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8500 		ctx_pg = ctxm->pg_info;
8501 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8502 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8503 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8504 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8505 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8506 				      &req->qpc_pg_size_qpc_lvl,
8507 				      &req->qpc_page_dir);
8508 
8509 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8510 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8511 	}
8512 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8513 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8514 		ctx_pg = ctxm->pg_info;
8515 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8516 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8517 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8518 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8519 				      &req->srq_pg_size_srq_lvl,
8520 				      &req->srq_page_dir);
8521 	}
8522 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8523 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8524 		ctx_pg = ctxm->pg_info;
8525 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8526 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8527 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8528 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8529 				      &req->cq_pg_size_cq_lvl,
8530 				      &req->cq_page_dir);
8531 	}
8532 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8533 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8534 		ctx_pg = ctxm->pg_info;
8535 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8536 		req->vnic_num_ring_table_entries =
8537 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8538 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8539 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8540 				      &req->vnic_pg_size_vnic_lvl,
8541 				      &req->vnic_page_dir);
8542 	}
8543 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8544 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8545 		ctx_pg = ctxm->pg_info;
8546 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8547 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8548 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8549 				      &req->stat_pg_size_stat_lvl,
8550 				      &req->stat_page_dir);
8551 	}
8552 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8553 		u32 units;
8554 
8555 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8556 		ctx_pg = ctxm->pg_info;
8557 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8558 		units = ctxm->mrav_num_entries_units;
8559 		if (units) {
8560 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8561 			u32 entries;
8562 
8563 			num_mr = ctx_pg->entries - num_ah;
8564 			entries = ((num_mr / units) << 16) | (num_ah / units);
8565 			req->mrav_num_entries = cpu_to_le32(entries);
8566 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8567 		}
8568 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8569 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8570 				      &req->mrav_pg_size_mrav_lvl,
8571 				      &req->mrav_page_dir);
8572 	}
8573 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8574 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8575 		ctx_pg = ctxm->pg_info;
8576 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8577 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8578 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8579 				      &req->tim_pg_size_tim_lvl,
8580 				      &req->tim_page_dir);
8581 	}
8582 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8583 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8584 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8585 	     pg_dir = &req->tqm_sp_page_dir,
8586 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8587 	     ctx_pg = ctxm->pg_info;
8588 	     i < BNXT_MAX_TQM_RINGS;
8589 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8590 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8591 		if (!(enables & ena))
8592 			continue;
8593 
8594 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8595 		*num_entries = cpu_to_le32(ctx_pg->entries);
8596 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8597 	}
8598 	req->flags = cpu_to_le32(flags);
8599 	return hwrm_req_send(bp, req);
8600 }
8601 
8602 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8603 				  struct bnxt_ctx_pg_info *ctx_pg)
8604 {
8605 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8606 
8607 	rmem->page_size = BNXT_PAGE_SIZE;
8608 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8609 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8610 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8611 	if (rmem->depth >= 1)
8612 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8613 	return bnxt_alloc_ring(bp, rmem);
8614 }
8615 
8616 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8617 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8618 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8619 {
8620 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8621 	int rc;
8622 
8623 	if (!mem_size)
8624 		return -EINVAL;
8625 
8626 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8627 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8628 		ctx_pg->nr_pages = 0;
8629 		return -EINVAL;
8630 	}
8631 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8632 		int nr_tbls, i;
8633 
8634 		rmem->depth = 2;
8635 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8636 					     GFP_KERNEL);
8637 		if (!ctx_pg->ctx_pg_tbl)
8638 			return -ENOMEM;
8639 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8640 		rmem->nr_pages = nr_tbls;
8641 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8642 		if (rc)
8643 			return rc;
8644 		for (i = 0; i < nr_tbls; i++) {
8645 			struct bnxt_ctx_pg_info *pg_tbl;
8646 
8647 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8648 			if (!pg_tbl)
8649 				return -ENOMEM;
8650 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8651 			rmem = &pg_tbl->ring_mem;
8652 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8653 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8654 			rmem->depth = 1;
8655 			rmem->nr_pages = MAX_CTX_PAGES;
8656 			rmem->ctx_mem = ctxm;
8657 			if (i == (nr_tbls - 1)) {
8658 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8659 
8660 				if (rem)
8661 					rmem->nr_pages = rem;
8662 			}
8663 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8664 			if (rc)
8665 				break;
8666 		}
8667 	} else {
8668 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8669 		if (rmem->nr_pages > 1 || depth)
8670 			rmem->depth = 1;
8671 		rmem->ctx_mem = ctxm;
8672 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8673 	}
8674 	return rc;
8675 }
8676 
8677 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
8678 				  struct bnxt_ctx_pg_info *ctx_pg)
8679 {
8680 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8681 
8682 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
8683 	    ctx_pg->ctx_pg_tbl) {
8684 		int i, nr_tbls = rmem->nr_pages;
8685 
8686 		for (i = 0; i < nr_tbls; i++) {
8687 			struct bnxt_ctx_pg_info *pg_tbl;
8688 			struct bnxt_ring_mem_info *rmem2;
8689 
8690 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8691 			if (!pg_tbl)
8692 				continue;
8693 			rmem2 = &pg_tbl->ring_mem;
8694 			bnxt_free_ring(bp, rmem2);
8695 			ctx_pg->ctx_pg_arr[i] = NULL;
8696 			kfree(pg_tbl);
8697 			ctx_pg->ctx_pg_tbl[i] = NULL;
8698 		}
8699 		kfree(ctx_pg->ctx_pg_tbl);
8700 		ctx_pg->ctx_pg_tbl = NULL;
8701 	}
8702 	bnxt_free_ring(bp, rmem);
8703 	ctx_pg->nr_pages = 0;
8704 }
8705 
8706 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
8707 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
8708 				   u8 pg_lvl)
8709 {
8710 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8711 	int i, rc = 0, n = 1;
8712 	u32 mem_size;
8713 
8714 	if (!ctxm->entry_size || !ctx_pg)
8715 		return -EINVAL;
8716 	if (ctxm->instance_bmap)
8717 		n = hweight32(ctxm->instance_bmap);
8718 	if (ctxm->entry_multiple)
8719 		entries = roundup(entries, ctxm->entry_multiple);
8720 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
8721 	mem_size = entries * ctxm->entry_size;
8722 	for (i = 0; i < n && !rc; i++) {
8723 		ctx_pg[i].entries = entries;
8724 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
8725 					    ctxm->init_value ? ctxm : NULL);
8726 	}
8727 	return rc;
8728 }
8729 
8730 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
8731 					       struct bnxt_ctx_mem_type *ctxm,
8732 					       bool last)
8733 {
8734 	struct hwrm_func_backing_store_cfg_v2_input *req;
8735 	u32 instance_bmap = ctxm->instance_bmap;
8736 	int i, j, rc = 0, n = 1;
8737 	__le32 *p;
8738 
8739 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
8740 		return 0;
8741 
8742 	if (instance_bmap)
8743 		n = hweight32(ctxm->instance_bmap);
8744 	else
8745 		instance_bmap = 1;
8746 
8747 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
8748 	if (rc)
8749 		return rc;
8750 	hwrm_req_hold(bp, req);
8751 	req->type = cpu_to_le16(ctxm->type);
8752 	req->entry_size = cpu_to_le16(ctxm->entry_size);
8753 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
8754 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
8755 		p[i] = cpu_to_le32(ctxm->split[i]);
8756 	for (i = 0, j = 0; j < n && !rc; i++) {
8757 		struct bnxt_ctx_pg_info *ctx_pg;
8758 
8759 		if (!(instance_bmap & (1 << i)))
8760 			continue;
8761 		req->instance = cpu_to_le16(i);
8762 		ctx_pg = &ctxm->pg_info[j++];
8763 		if (!ctx_pg->entries)
8764 			continue;
8765 		req->num_entries = cpu_to_le32(ctx_pg->entries);
8766 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8767 				      &req->page_size_pbl_level,
8768 				      &req->page_dir);
8769 		if (last && j == n)
8770 			req->flags =
8771 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
8772 		rc = hwrm_req_send(bp, req);
8773 	}
8774 	hwrm_req_drop(bp, req);
8775 	return rc;
8776 }
8777 
8778 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
8779 {
8780 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8781 	struct bnxt_ctx_mem_type *ctxm;
8782 	u16 last_type;
8783 	int rc = 0;
8784 	u16 type;
8785 
8786 	if (!ena)
8787 		return 0;
8788 	else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
8789 		last_type = BNXT_CTX_MAX - 1;
8790 	else
8791 		last_type = BNXT_CTX_L2_MAX - 1;
8792 	ctx->ctx_arr[last_type].last = 1;
8793 
8794 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
8795 		ctxm = &ctx->ctx_arr[type];
8796 
8797 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
8798 		if (rc)
8799 			return rc;
8800 	}
8801 	return 0;
8802 }
8803 
8804 void bnxt_free_ctx_mem(struct bnxt *bp)
8805 {
8806 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8807 	u16 type;
8808 
8809 	if (!ctx)
8810 		return;
8811 
8812 	for (type = 0; type < BNXT_CTX_V2_MAX; type++) {
8813 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8814 		struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8815 		int i, n = 1;
8816 
8817 		if (!ctx_pg)
8818 			continue;
8819 		if (ctxm->instance_bmap)
8820 			n = hweight32(ctxm->instance_bmap);
8821 		for (i = 0; i < n; i++)
8822 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
8823 
8824 		kfree(ctx_pg);
8825 		ctxm->pg_info = NULL;
8826 	}
8827 
8828 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
8829 	kfree(ctx);
8830 	bp->ctx = NULL;
8831 }
8832 
8833 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
8834 {
8835 	struct bnxt_ctx_mem_type *ctxm;
8836 	struct bnxt_ctx_mem_info *ctx;
8837 	u32 l2_qps, qp1_qps, max_qps;
8838 	u32 ena, entries_sp, entries;
8839 	u32 srqs, max_srqs, min;
8840 	u32 num_mr, num_ah;
8841 	u32 extra_srqs = 0;
8842 	u32 extra_qps = 0;
8843 	u32 fast_qpmd_qps;
8844 	u8 pg_lvl = 1;
8845 	int i, rc;
8846 
8847 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
8848 	if (rc) {
8849 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
8850 			   rc);
8851 		return rc;
8852 	}
8853 	ctx = bp->ctx;
8854 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
8855 		return 0;
8856 
8857 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8858 	l2_qps = ctxm->qp_l2_entries;
8859 	qp1_qps = ctxm->qp_qp1_entries;
8860 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
8861 	max_qps = ctxm->max_entries;
8862 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8863 	srqs = ctxm->srq_l2_entries;
8864 	max_srqs = ctxm->max_entries;
8865 	ena = 0;
8866 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
8867 		pg_lvl = 2;
8868 		extra_qps = min_t(u32, 65536, max_qps - l2_qps - qp1_qps);
8869 		/* allocate extra qps if fw supports RoCE fast qp destroy feature */
8870 		extra_qps += fast_qpmd_qps;
8871 		extra_srqs = min_t(u32, 8192, max_srqs - srqs);
8872 		if (fast_qpmd_qps)
8873 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
8874 	}
8875 
8876 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8877 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
8878 				     pg_lvl);
8879 	if (rc)
8880 		return rc;
8881 
8882 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8883 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
8884 	if (rc)
8885 		return rc;
8886 
8887 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8888 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
8889 				     extra_qps * 2, pg_lvl);
8890 	if (rc)
8891 		return rc;
8892 
8893 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8894 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
8895 	if (rc)
8896 		return rc;
8897 
8898 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8899 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
8900 	if (rc)
8901 		return rc;
8902 
8903 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
8904 		goto skip_rdma;
8905 
8906 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8907 	/* 128K extra is needed to accommodate static AH context
8908 	 * allocation by f/w.
8909 	 */
8910 	num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
8911 	num_ah = min_t(u32, num_mr, 1024 * 128);
8912 	ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
8913 	if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
8914 		ctxm->mrav_av_entries = num_ah;
8915 
8916 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
8917 	if (rc)
8918 		return rc;
8919 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
8920 
8921 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8922 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
8923 	if (rc)
8924 		return rc;
8925 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
8926 
8927 skip_rdma:
8928 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8929 	min = ctxm->min_entries;
8930 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
8931 		     2 * (extra_qps + qp1_qps) + min;
8932 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
8933 	if (rc)
8934 		return rc;
8935 
8936 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8937 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
8938 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
8939 	if (rc)
8940 		return rc;
8941 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
8942 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
8943 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
8944 
8945 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8946 		rc = bnxt_backing_store_cfg_v2(bp, ena);
8947 	else
8948 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
8949 	if (rc) {
8950 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
8951 			   rc);
8952 		return rc;
8953 	}
8954 	ctx->flags |= BNXT_CTX_FLAG_INITED;
8955 	return 0;
8956 }
8957 
8958 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
8959 {
8960 	struct hwrm_func_resource_qcaps_output *resp;
8961 	struct hwrm_func_resource_qcaps_input *req;
8962 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8963 	int rc;
8964 
8965 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
8966 	if (rc)
8967 		return rc;
8968 
8969 	req->fid = cpu_to_le16(0xffff);
8970 	resp = hwrm_req_hold(bp, req);
8971 	rc = hwrm_req_send_silent(bp, req);
8972 	if (rc)
8973 		goto hwrm_func_resc_qcaps_exit;
8974 
8975 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
8976 	if (!all)
8977 		goto hwrm_func_resc_qcaps_exit;
8978 
8979 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
8980 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
8981 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
8982 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
8983 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
8984 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
8985 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
8986 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
8987 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
8988 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
8989 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
8990 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
8991 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
8992 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
8993 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
8994 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
8995 
8996 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8997 		u16 max_msix = le16_to_cpu(resp->max_msix);
8998 
8999 		hw_resc->max_nqs = max_msix;
9000 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9001 	}
9002 
9003 	if (BNXT_PF(bp)) {
9004 		struct bnxt_pf_info *pf = &bp->pf;
9005 
9006 		pf->vf_resv_strategy =
9007 			le16_to_cpu(resp->vf_reservation_strategy);
9008 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9009 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9010 	}
9011 hwrm_func_resc_qcaps_exit:
9012 	hwrm_req_drop(bp, req);
9013 	return rc;
9014 }
9015 
9016 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9017 {
9018 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9019 	struct hwrm_port_mac_ptp_qcfg_input *req;
9020 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9021 	bool phc_cfg;
9022 	u8 flags;
9023 	int rc;
9024 
9025 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9026 		rc = -ENODEV;
9027 		goto no_ptp;
9028 	}
9029 
9030 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9031 	if (rc)
9032 		goto no_ptp;
9033 
9034 	req->port_id = cpu_to_le16(bp->pf.port_id);
9035 	resp = hwrm_req_hold(bp, req);
9036 	rc = hwrm_req_send(bp, req);
9037 	if (rc)
9038 		goto exit;
9039 
9040 	flags = resp->flags;
9041 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9042 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9043 		rc = -ENODEV;
9044 		goto exit;
9045 	}
9046 	if (!ptp) {
9047 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
9048 		if (!ptp) {
9049 			rc = -ENOMEM;
9050 			goto exit;
9051 		}
9052 		ptp->bp = bp;
9053 		bp->ptp_cfg = ptp;
9054 	}
9055 
9056 	if (flags &
9057 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9058 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9059 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9060 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9061 	} else if (BNXT_CHIP_P5(bp)) {
9062 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9063 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9064 	} else {
9065 		rc = -ENODEV;
9066 		goto exit;
9067 	}
9068 	phc_cfg = (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9069 	rc = bnxt_ptp_init(bp, phc_cfg);
9070 	if (rc)
9071 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9072 exit:
9073 	hwrm_req_drop(bp, req);
9074 	if (!rc)
9075 		return 0;
9076 
9077 no_ptp:
9078 	bnxt_ptp_clear(bp);
9079 	kfree(ptp);
9080 	bp->ptp_cfg = NULL;
9081 	return rc;
9082 }
9083 
9084 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9085 {
9086 	struct hwrm_func_qcaps_output *resp;
9087 	struct hwrm_func_qcaps_input *req;
9088 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9089 	u32 flags, flags_ext, flags_ext2;
9090 	int rc;
9091 
9092 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9093 	if (rc)
9094 		return rc;
9095 
9096 	req->fid = cpu_to_le16(0xffff);
9097 	resp = hwrm_req_hold(bp, req);
9098 	rc = hwrm_req_send(bp, req);
9099 	if (rc)
9100 		goto hwrm_func_qcaps_exit;
9101 
9102 	flags = le32_to_cpu(resp->flags);
9103 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9104 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9105 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9106 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9107 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9108 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9109 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9110 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9111 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9112 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9113 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9114 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9115 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9116 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9117 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9118 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9119 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9120 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9121 
9122 	flags_ext = le32_to_cpu(resp->flags_ext);
9123 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9124 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9125 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9126 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9127 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9128 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9129 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9130 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9131 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9132 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9133 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9134 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9135 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9136 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9137 
9138 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9139 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9140 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9141 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9142 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9143 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9144 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9145 
9146 	bp->tx_push_thresh = 0;
9147 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9148 	    BNXT_FW_MAJ(bp) > 217)
9149 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9150 
9151 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9152 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9153 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9154 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9155 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9156 	if (!hw_resc->max_hw_ring_grps)
9157 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9158 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9159 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9160 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9161 
9162 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9163 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9164 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9165 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9166 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9167 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9168 
9169 	if (BNXT_PF(bp)) {
9170 		struct bnxt_pf_info *pf = &bp->pf;
9171 
9172 		pf->fw_fid = le16_to_cpu(resp->fid);
9173 		pf->port_id = le16_to_cpu(resp->port_id);
9174 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9175 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9176 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9177 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9178 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9179 			bp->flags |= BNXT_FLAG_WOL_CAP;
9180 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9181 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9182 		} else {
9183 			bnxt_ptp_clear(bp);
9184 			kfree(bp->ptp_cfg);
9185 			bp->ptp_cfg = NULL;
9186 		}
9187 	} else {
9188 #ifdef CONFIG_BNXT_SRIOV
9189 		struct bnxt_vf_info *vf = &bp->vf;
9190 
9191 		vf->fw_fid = le16_to_cpu(resp->fid);
9192 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9193 #endif
9194 	}
9195 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9196 
9197 hwrm_func_qcaps_exit:
9198 	hwrm_req_drop(bp, req);
9199 	return rc;
9200 }
9201 
9202 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9203 {
9204 	struct hwrm_dbg_qcaps_output *resp;
9205 	struct hwrm_dbg_qcaps_input *req;
9206 	int rc;
9207 
9208 	bp->fw_dbg_cap = 0;
9209 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9210 		return;
9211 
9212 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9213 	if (rc)
9214 		return;
9215 
9216 	req->fid = cpu_to_le16(0xffff);
9217 	resp = hwrm_req_hold(bp, req);
9218 	rc = hwrm_req_send(bp, req);
9219 	if (rc)
9220 		goto hwrm_dbg_qcaps_exit;
9221 
9222 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9223 
9224 hwrm_dbg_qcaps_exit:
9225 	hwrm_req_drop(bp, req);
9226 }
9227 
9228 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9229 
9230 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9231 {
9232 	int rc;
9233 
9234 	rc = __bnxt_hwrm_func_qcaps(bp);
9235 	if (rc)
9236 		return rc;
9237 
9238 	bnxt_hwrm_dbg_qcaps(bp);
9239 
9240 	rc = bnxt_hwrm_queue_qportcfg(bp);
9241 	if (rc) {
9242 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9243 		return rc;
9244 	}
9245 	if (bp->hwrm_spec_code >= 0x10803) {
9246 		rc = bnxt_alloc_ctx_mem(bp);
9247 		if (rc)
9248 			return rc;
9249 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9250 		if (!rc)
9251 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9252 	}
9253 	return 0;
9254 }
9255 
9256 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9257 {
9258 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9259 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9260 	u32 flags;
9261 	int rc;
9262 
9263 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9264 		return 0;
9265 
9266 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9267 	if (rc)
9268 		return rc;
9269 
9270 	resp = hwrm_req_hold(bp, req);
9271 	rc = hwrm_req_send(bp, req);
9272 	if (rc)
9273 		goto hwrm_cfa_adv_qcaps_exit;
9274 
9275 	flags = le32_to_cpu(resp->flags);
9276 	if (flags &
9277 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9278 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9279 
9280 	if (flags &
9281 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9282 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9283 
9284 	if (flags &
9285 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9286 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9287 
9288 hwrm_cfa_adv_qcaps_exit:
9289 	hwrm_req_drop(bp, req);
9290 	return rc;
9291 }
9292 
9293 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9294 {
9295 	if (bp->fw_health)
9296 		return 0;
9297 
9298 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9299 	if (!bp->fw_health)
9300 		return -ENOMEM;
9301 
9302 	mutex_init(&bp->fw_health->lock);
9303 	return 0;
9304 }
9305 
9306 static int bnxt_alloc_fw_health(struct bnxt *bp)
9307 {
9308 	int rc;
9309 
9310 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9311 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9312 		return 0;
9313 
9314 	rc = __bnxt_alloc_fw_health(bp);
9315 	if (rc) {
9316 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9317 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9318 		return rc;
9319 	}
9320 
9321 	return 0;
9322 }
9323 
9324 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9325 {
9326 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9327 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9328 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9329 }
9330 
9331 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9332 {
9333 	struct bnxt_fw_health *fw_health = bp->fw_health;
9334 	u32 reg_type;
9335 
9336 	if (!fw_health)
9337 		return;
9338 
9339 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9340 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9341 		fw_health->status_reliable = false;
9342 
9343 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9344 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9345 		fw_health->resets_reliable = false;
9346 }
9347 
9348 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9349 {
9350 	void __iomem *hs;
9351 	u32 status_loc;
9352 	u32 reg_type;
9353 	u32 sig;
9354 
9355 	if (bp->fw_health)
9356 		bp->fw_health->status_reliable = false;
9357 
9358 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9359 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9360 
9361 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9362 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9363 		if (!bp->chip_num) {
9364 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9365 			bp->chip_num = readl(bp->bar0 +
9366 					     BNXT_FW_HEALTH_WIN_BASE +
9367 					     BNXT_GRC_REG_CHIP_NUM);
9368 		}
9369 		if (!BNXT_CHIP_P5_PLUS(bp))
9370 			return;
9371 
9372 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9373 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9374 	} else {
9375 		status_loc = readl(hs + offsetof(struct hcomm_status,
9376 						 fw_status_loc));
9377 	}
9378 
9379 	if (__bnxt_alloc_fw_health(bp)) {
9380 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9381 		return;
9382 	}
9383 
9384 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9385 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9386 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9387 		__bnxt_map_fw_health_reg(bp, status_loc);
9388 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9389 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9390 	}
9391 
9392 	bp->fw_health->status_reliable = true;
9393 }
9394 
9395 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9396 {
9397 	struct bnxt_fw_health *fw_health = bp->fw_health;
9398 	u32 reg_base = 0xffffffff;
9399 	int i;
9400 
9401 	bp->fw_health->status_reliable = false;
9402 	bp->fw_health->resets_reliable = false;
9403 	/* Only pre-map the monitoring GRC registers using window 3 */
9404 	for (i = 0; i < 4; i++) {
9405 		u32 reg = fw_health->regs[i];
9406 
9407 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9408 			continue;
9409 		if (reg_base == 0xffffffff)
9410 			reg_base = reg & BNXT_GRC_BASE_MASK;
9411 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9412 			return -ERANGE;
9413 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9414 	}
9415 	bp->fw_health->status_reliable = true;
9416 	bp->fw_health->resets_reliable = true;
9417 	if (reg_base == 0xffffffff)
9418 		return 0;
9419 
9420 	__bnxt_map_fw_health_reg(bp, reg_base);
9421 	return 0;
9422 }
9423 
9424 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9425 {
9426 	if (!bp->fw_health)
9427 		return;
9428 
9429 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9430 		bp->fw_health->status_reliable = true;
9431 		bp->fw_health->resets_reliable = true;
9432 	} else {
9433 		bnxt_try_map_fw_health_reg(bp);
9434 	}
9435 }
9436 
9437 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9438 {
9439 	struct bnxt_fw_health *fw_health = bp->fw_health;
9440 	struct hwrm_error_recovery_qcfg_output *resp;
9441 	struct hwrm_error_recovery_qcfg_input *req;
9442 	int rc, i;
9443 
9444 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9445 		return 0;
9446 
9447 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9448 	if (rc)
9449 		return rc;
9450 
9451 	resp = hwrm_req_hold(bp, req);
9452 	rc = hwrm_req_send(bp, req);
9453 	if (rc)
9454 		goto err_recovery_out;
9455 	fw_health->flags = le32_to_cpu(resp->flags);
9456 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
9457 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
9458 		rc = -EINVAL;
9459 		goto err_recovery_out;
9460 	}
9461 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
9462 	fw_health->master_func_wait_dsecs =
9463 		le32_to_cpu(resp->master_func_wait_period);
9464 	fw_health->normal_func_wait_dsecs =
9465 		le32_to_cpu(resp->normal_func_wait_period);
9466 	fw_health->post_reset_wait_dsecs =
9467 		le32_to_cpu(resp->master_func_wait_period_after_reset);
9468 	fw_health->post_reset_max_wait_dsecs =
9469 		le32_to_cpu(resp->max_bailout_time_after_reset);
9470 	fw_health->regs[BNXT_FW_HEALTH_REG] =
9471 		le32_to_cpu(resp->fw_health_status_reg);
9472 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
9473 		le32_to_cpu(resp->fw_heartbeat_reg);
9474 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
9475 		le32_to_cpu(resp->fw_reset_cnt_reg);
9476 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
9477 		le32_to_cpu(resp->reset_inprogress_reg);
9478 	fw_health->fw_reset_inprog_reg_mask =
9479 		le32_to_cpu(resp->reset_inprogress_reg_mask);
9480 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
9481 	if (fw_health->fw_reset_seq_cnt >= 16) {
9482 		rc = -EINVAL;
9483 		goto err_recovery_out;
9484 	}
9485 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
9486 		fw_health->fw_reset_seq_regs[i] =
9487 			le32_to_cpu(resp->reset_reg[i]);
9488 		fw_health->fw_reset_seq_vals[i] =
9489 			le32_to_cpu(resp->reset_reg_val[i]);
9490 		fw_health->fw_reset_seq_delay_msec[i] =
9491 			resp->delay_after_reset[i];
9492 	}
9493 err_recovery_out:
9494 	hwrm_req_drop(bp, req);
9495 	if (!rc)
9496 		rc = bnxt_map_fw_health_regs(bp);
9497 	if (rc)
9498 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9499 	return rc;
9500 }
9501 
9502 static int bnxt_hwrm_func_reset(struct bnxt *bp)
9503 {
9504 	struct hwrm_func_reset_input *req;
9505 	int rc;
9506 
9507 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
9508 	if (rc)
9509 		return rc;
9510 
9511 	req->enables = 0;
9512 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
9513 	return hwrm_req_send(bp, req);
9514 }
9515 
9516 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
9517 {
9518 	struct hwrm_nvm_get_dev_info_output nvm_info;
9519 
9520 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
9521 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
9522 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
9523 			 nvm_info.nvm_cfg_ver_upd);
9524 }
9525 
9526 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
9527 {
9528 	struct hwrm_queue_qportcfg_output *resp;
9529 	struct hwrm_queue_qportcfg_input *req;
9530 	u8 i, j, *qptr;
9531 	bool no_rdma;
9532 	int rc = 0;
9533 
9534 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
9535 	if (rc)
9536 		return rc;
9537 
9538 	resp = hwrm_req_hold(bp, req);
9539 	rc = hwrm_req_send(bp, req);
9540 	if (rc)
9541 		goto qportcfg_exit;
9542 
9543 	if (!resp->max_configurable_queues) {
9544 		rc = -EINVAL;
9545 		goto qportcfg_exit;
9546 	}
9547 	bp->max_tc = resp->max_configurable_queues;
9548 	bp->max_lltc = resp->max_configurable_lossless_queues;
9549 	if (bp->max_tc > BNXT_MAX_QUEUE)
9550 		bp->max_tc = BNXT_MAX_QUEUE;
9551 
9552 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
9553 	qptr = &resp->queue_id0;
9554 	for (i = 0, j = 0; i < bp->max_tc; i++) {
9555 		bp->q_info[j].queue_id = *qptr;
9556 		bp->q_ids[i] = *qptr++;
9557 		bp->q_info[j].queue_profile = *qptr++;
9558 		bp->tc_to_qidx[j] = j;
9559 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
9560 		    (no_rdma && BNXT_PF(bp)))
9561 			j++;
9562 	}
9563 	bp->max_q = bp->max_tc;
9564 	bp->max_tc = max_t(u8, j, 1);
9565 
9566 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
9567 		bp->max_tc = 1;
9568 
9569 	if (bp->max_lltc > bp->max_tc)
9570 		bp->max_lltc = bp->max_tc;
9571 
9572 qportcfg_exit:
9573 	hwrm_req_drop(bp, req);
9574 	return rc;
9575 }
9576 
9577 static int bnxt_hwrm_poll(struct bnxt *bp)
9578 {
9579 	struct hwrm_ver_get_input *req;
9580 	int rc;
9581 
9582 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9583 	if (rc)
9584 		return rc;
9585 
9586 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9587 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9588 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9589 
9590 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
9591 	rc = hwrm_req_send(bp, req);
9592 	return rc;
9593 }
9594 
9595 static int bnxt_hwrm_ver_get(struct bnxt *bp)
9596 {
9597 	struct hwrm_ver_get_output *resp;
9598 	struct hwrm_ver_get_input *req;
9599 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
9600 	u32 dev_caps_cfg, hwrm_ver;
9601 	int rc, len;
9602 
9603 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9604 	if (rc)
9605 		return rc;
9606 
9607 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
9608 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
9609 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9610 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9611 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9612 
9613 	resp = hwrm_req_hold(bp, req);
9614 	rc = hwrm_req_send(bp, req);
9615 	if (rc)
9616 		goto hwrm_ver_get_exit;
9617 
9618 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
9619 
9620 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
9621 			     resp->hwrm_intf_min_8b << 8 |
9622 			     resp->hwrm_intf_upd_8b;
9623 	if (resp->hwrm_intf_maj_8b < 1) {
9624 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
9625 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
9626 			    resp->hwrm_intf_upd_8b);
9627 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
9628 	}
9629 
9630 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
9631 			HWRM_VERSION_UPDATE;
9632 
9633 	if (bp->hwrm_spec_code > hwrm_ver)
9634 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
9635 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
9636 			 HWRM_VERSION_UPDATE);
9637 	else
9638 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
9639 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
9640 			 resp->hwrm_intf_upd_8b);
9641 
9642 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
9643 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
9644 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
9645 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
9646 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
9647 		len = FW_VER_STR_LEN;
9648 	} else {
9649 		fw_maj = resp->hwrm_fw_maj_8b;
9650 		fw_min = resp->hwrm_fw_min_8b;
9651 		fw_bld = resp->hwrm_fw_bld_8b;
9652 		fw_rsv = resp->hwrm_fw_rsvd_8b;
9653 		len = BC_HWRM_STR_LEN;
9654 	}
9655 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
9656 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
9657 		 fw_rsv);
9658 
9659 	if (strlen(resp->active_pkg_name)) {
9660 		int fw_ver_len = strlen(bp->fw_ver_str);
9661 
9662 		snprintf(bp->fw_ver_str + fw_ver_len,
9663 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
9664 			 resp->active_pkg_name);
9665 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
9666 	}
9667 
9668 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
9669 	if (!bp->hwrm_cmd_timeout)
9670 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
9671 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
9672 	if (!bp->hwrm_cmd_max_timeout)
9673 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
9674 	else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT)
9675 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n",
9676 			    bp->hwrm_cmd_max_timeout / 1000);
9677 
9678 	if (resp->hwrm_intf_maj_8b >= 1) {
9679 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
9680 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
9681 	}
9682 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
9683 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
9684 
9685 	bp->chip_num = le16_to_cpu(resp->chip_num);
9686 	bp->chip_rev = resp->chip_rev;
9687 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
9688 	    !resp->chip_metal)
9689 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
9690 
9691 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
9692 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
9693 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
9694 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
9695 
9696 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
9697 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
9698 
9699 	if (dev_caps_cfg &
9700 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
9701 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
9702 
9703 	if (dev_caps_cfg &
9704 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
9705 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
9706 
9707 	if (dev_caps_cfg &
9708 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
9709 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
9710 
9711 hwrm_ver_get_exit:
9712 	hwrm_req_drop(bp, req);
9713 	return rc;
9714 }
9715 
9716 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
9717 {
9718 	struct hwrm_fw_set_time_input *req;
9719 	struct tm tm;
9720 	time64_t now = ktime_get_real_seconds();
9721 	int rc;
9722 
9723 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
9724 	    bp->hwrm_spec_code < 0x10400)
9725 		return -EOPNOTSUPP;
9726 
9727 	time64_to_tm(now, 0, &tm);
9728 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
9729 	if (rc)
9730 		return rc;
9731 
9732 	req->year = cpu_to_le16(1900 + tm.tm_year);
9733 	req->month = 1 + tm.tm_mon;
9734 	req->day = tm.tm_mday;
9735 	req->hour = tm.tm_hour;
9736 	req->minute = tm.tm_min;
9737 	req->second = tm.tm_sec;
9738 	return hwrm_req_send(bp, req);
9739 }
9740 
9741 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
9742 {
9743 	u64 sw_tmp;
9744 
9745 	hw &= mask;
9746 	sw_tmp = (*sw & ~mask) | hw;
9747 	if (hw < (*sw & mask))
9748 		sw_tmp += mask + 1;
9749 	WRITE_ONCE(*sw, sw_tmp);
9750 }
9751 
9752 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
9753 				    int count, bool ignore_zero)
9754 {
9755 	int i;
9756 
9757 	for (i = 0; i < count; i++) {
9758 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
9759 
9760 		if (ignore_zero && !hw)
9761 			continue;
9762 
9763 		if (masks[i] == -1ULL)
9764 			sw_stats[i] = hw;
9765 		else
9766 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
9767 	}
9768 }
9769 
9770 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
9771 {
9772 	if (!stats->hw_stats)
9773 		return;
9774 
9775 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
9776 				stats->hw_masks, stats->len / 8, false);
9777 }
9778 
9779 static void bnxt_accumulate_all_stats(struct bnxt *bp)
9780 {
9781 	struct bnxt_stats_mem *ring0_stats;
9782 	bool ignore_zero = false;
9783 	int i;
9784 
9785 	/* Chip bug.  Counter intermittently becomes 0. */
9786 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
9787 		ignore_zero = true;
9788 
9789 	for (i = 0; i < bp->cp_nr_rings; i++) {
9790 		struct bnxt_napi *bnapi = bp->bnapi[i];
9791 		struct bnxt_cp_ring_info *cpr;
9792 		struct bnxt_stats_mem *stats;
9793 
9794 		cpr = &bnapi->cp_ring;
9795 		stats = &cpr->stats;
9796 		if (!i)
9797 			ring0_stats = stats;
9798 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
9799 					ring0_stats->hw_masks,
9800 					ring0_stats->len / 8, ignore_zero);
9801 	}
9802 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
9803 		struct bnxt_stats_mem *stats = &bp->port_stats;
9804 		__le64 *hw_stats = stats->hw_stats;
9805 		u64 *sw_stats = stats->sw_stats;
9806 		u64 *masks = stats->hw_masks;
9807 		int cnt;
9808 
9809 		cnt = sizeof(struct rx_port_stats) / 8;
9810 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
9811 
9812 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9813 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9814 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9815 		cnt = sizeof(struct tx_port_stats) / 8;
9816 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
9817 	}
9818 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
9819 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
9820 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
9821 	}
9822 }
9823 
9824 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
9825 {
9826 	struct hwrm_port_qstats_input *req;
9827 	struct bnxt_pf_info *pf = &bp->pf;
9828 	int rc;
9829 
9830 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
9831 		return 0;
9832 
9833 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
9834 		return -EOPNOTSUPP;
9835 
9836 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
9837 	if (rc)
9838 		return rc;
9839 
9840 	req->flags = flags;
9841 	req->port_id = cpu_to_le16(pf->port_id);
9842 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
9843 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
9844 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
9845 	return hwrm_req_send(bp, req);
9846 }
9847 
9848 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
9849 {
9850 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
9851 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
9852 	struct hwrm_port_qstats_ext_output *resp_qs;
9853 	struct hwrm_port_qstats_ext_input *req_qs;
9854 	struct bnxt_pf_info *pf = &bp->pf;
9855 	u32 tx_stat_size;
9856 	int rc;
9857 
9858 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
9859 		return 0;
9860 
9861 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
9862 		return -EOPNOTSUPP;
9863 
9864 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
9865 	if (rc)
9866 		return rc;
9867 
9868 	req_qs->flags = flags;
9869 	req_qs->port_id = cpu_to_le16(pf->port_id);
9870 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
9871 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
9872 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
9873 		       sizeof(struct tx_port_stats_ext) : 0;
9874 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
9875 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
9876 	resp_qs = hwrm_req_hold(bp, req_qs);
9877 	rc = hwrm_req_send(bp, req_qs);
9878 	if (!rc) {
9879 		bp->fw_rx_stats_ext_size =
9880 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
9881 		if (BNXT_FW_MAJ(bp) < 220 &&
9882 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
9883 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
9884 
9885 		bp->fw_tx_stats_ext_size = tx_stat_size ?
9886 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
9887 	} else {
9888 		bp->fw_rx_stats_ext_size = 0;
9889 		bp->fw_tx_stats_ext_size = 0;
9890 	}
9891 	hwrm_req_drop(bp, req_qs);
9892 
9893 	if (flags)
9894 		return rc;
9895 
9896 	if (bp->fw_tx_stats_ext_size <=
9897 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
9898 		bp->pri2cos_valid = 0;
9899 		return rc;
9900 	}
9901 
9902 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
9903 	if (rc)
9904 		return rc;
9905 
9906 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
9907 
9908 	resp_qc = hwrm_req_hold(bp, req_qc);
9909 	rc = hwrm_req_send(bp, req_qc);
9910 	if (!rc) {
9911 		u8 *pri2cos;
9912 		int i, j;
9913 
9914 		pri2cos = &resp_qc->pri0_cos_queue_id;
9915 		for (i = 0; i < 8; i++) {
9916 			u8 queue_id = pri2cos[i];
9917 			u8 queue_idx;
9918 
9919 			/* Per port queue IDs start from 0, 10, 20, etc */
9920 			queue_idx = queue_id % 10;
9921 			if (queue_idx > BNXT_MAX_QUEUE) {
9922 				bp->pri2cos_valid = false;
9923 				hwrm_req_drop(bp, req_qc);
9924 				return rc;
9925 			}
9926 			for (j = 0; j < bp->max_q; j++) {
9927 				if (bp->q_ids[j] == queue_id)
9928 					bp->pri2cos_idx[i] = queue_idx;
9929 			}
9930 		}
9931 		bp->pri2cos_valid = true;
9932 	}
9933 	hwrm_req_drop(bp, req_qc);
9934 
9935 	return rc;
9936 }
9937 
9938 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
9939 {
9940 	bnxt_hwrm_tunnel_dst_port_free(bp,
9941 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
9942 	bnxt_hwrm_tunnel_dst_port_free(bp,
9943 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
9944 }
9945 
9946 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
9947 {
9948 	int rc, i;
9949 	u32 tpa_flags = 0;
9950 
9951 	if (set_tpa)
9952 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
9953 	else if (BNXT_NO_FW_ACCESS(bp))
9954 		return 0;
9955 	for (i = 0; i < bp->nr_vnics; i++) {
9956 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
9957 		if (rc) {
9958 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
9959 				   i, rc);
9960 			return rc;
9961 		}
9962 	}
9963 	return 0;
9964 }
9965 
9966 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
9967 {
9968 	int i;
9969 
9970 	for (i = 0; i < bp->nr_vnics; i++)
9971 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
9972 }
9973 
9974 static void bnxt_clear_vnic(struct bnxt *bp)
9975 {
9976 	if (!bp->vnic_info)
9977 		return;
9978 
9979 	bnxt_hwrm_clear_vnic_filter(bp);
9980 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
9981 		/* clear all RSS setting before free vnic ctx */
9982 		bnxt_hwrm_clear_vnic_rss(bp);
9983 		bnxt_hwrm_vnic_ctx_free(bp);
9984 	}
9985 	/* before free the vnic, undo the vnic tpa settings */
9986 	if (bp->flags & BNXT_FLAG_TPA)
9987 		bnxt_set_tpa(bp, false);
9988 	bnxt_hwrm_vnic_free(bp);
9989 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
9990 		bnxt_hwrm_vnic_ctx_free(bp);
9991 }
9992 
9993 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
9994 				    bool irq_re_init)
9995 {
9996 	bnxt_clear_vnic(bp);
9997 	bnxt_hwrm_ring_free(bp, close_path);
9998 	bnxt_hwrm_ring_grp_free(bp);
9999 	if (irq_re_init) {
10000 		bnxt_hwrm_stat_ctx_free(bp);
10001 		bnxt_hwrm_free_tunnel_ports(bp);
10002 	}
10003 }
10004 
10005 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10006 {
10007 	struct hwrm_func_cfg_input *req;
10008 	u8 evb_mode;
10009 	int rc;
10010 
10011 	if (br_mode == BRIDGE_MODE_VEB)
10012 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10013 	else if (br_mode == BRIDGE_MODE_VEPA)
10014 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10015 	else
10016 		return -EINVAL;
10017 
10018 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10019 	if (rc)
10020 		return rc;
10021 
10022 	req->fid = cpu_to_le16(0xffff);
10023 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10024 	req->evb_mode = evb_mode;
10025 	return hwrm_req_send(bp, req);
10026 }
10027 
10028 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10029 {
10030 	struct hwrm_func_cfg_input *req;
10031 	int rc;
10032 
10033 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10034 		return 0;
10035 
10036 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10037 	if (rc)
10038 		return rc;
10039 
10040 	req->fid = cpu_to_le16(0xffff);
10041 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10042 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10043 	if (size == 128)
10044 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10045 
10046 	return hwrm_req_send(bp, req);
10047 }
10048 
10049 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10050 {
10051 	int rc;
10052 
10053 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10054 		goto skip_rss_ctx;
10055 
10056 	/* allocate context for vnic */
10057 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10058 	if (rc) {
10059 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10060 			   vnic->vnic_id, rc);
10061 		goto vnic_setup_err;
10062 	}
10063 	bp->rsscos_nr_ctxs++;
10064 
10065 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10066 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10067 		if (rc) {
10068 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10069 				   vnic->vnic_id, rc);
10070 			goto vnic_setup_err;
10071 		}
10072 		bp->rsscos_nr_ctxs++;
10073 	}
10074 
10075 skip_rss_ctx:
10076 	/* configure default vnic, ring grp */
10077 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10078 	if (rc) {
10079 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10080 			   vnic->vnic_id, rc);
10081 		goto vnic_setup_err;
10082 	}
10083 
10084 	/* Enable RSS hashing on vnic */
10085 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10086 	if (rc) {
10087 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10088 			   vnic->vnic_id, rc);
10089 		goto vnic_setup_err;
10090 	}
10091 
10092 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10093 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10094 		if (rc) {
10095 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10096 				   vnic->vnic_id, rc);
10097 		}
10098 	}
10099 
10100 vnic_setup_err:
10101 	return rc;
10102 }
10103 
10104 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10105 {
10106 	int rc;
10107 
10108 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10109 	if (rc) {
10110 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10111 			   vnic->vnic_id, rc);
10112 		return rc;
10113 	}
10114 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10115 	if (rc)
10116 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10117 			   vnic->vnic_id, rc);
10118 	return rc;
10119 }
10120 
10121 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10122 {
10123 	int rc, i, nr_ctxs;
10124 
10125 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10126 	for (i = 0; i < nr_ctxs; i++) {
10127 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10128 		if (rc) {
10129 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10130 				   vnic->vnic_id, i, rc);
10131 			break;
10132 		}
10133 		bp->rsscos_nr_ctxs++;
10134 	}
10135 	if (i < nr_ctxs)
10136 		return -ENOMEM;
10137 
10138 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10139 	if (rc)
10140 		return rc;
10141 
10142 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10143 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10144 		if (rc) {
10145 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10146 				   vnic->vnic_id, rc);
10147 		}
10148 	}
10149 	return rc;
10150 }
10151 
10152 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10153 {
10154 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10155 		return __bnxt_setup_vnic_p5(bp, vnic);
10156 	else
10157 		return __bnxt_setup_vnic(bp, vnic);
10158 }
10159 
10160 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10161 				     struct bnxt_vnic_info *vnic,
10162 				     u16 start_rx_ring_idx, int rx_rings)
10163 {
10164 	int rc;
10165 
10166 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10167 	if (rc) {
10168 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10169 			   vnic->vnic_id, rc);
10170 		return rc;
10171 	}
10172 	return bnxt_setup_vnic(bp, vnic);
10173 }
10174 
10175 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10176 {
10177 	struct bnxt_vnic_info *vnic;
10178 	int i, rc = 0;
10179 
10180 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10181 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10182 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10183 	}
10184 
10185 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10186 		return 0;
10187 
10188 	for (i = 0; i < bp->rx_nr_rings; i++) {
10189 		u16 vnic_id = i + 1;
10190 		u16 ring_id = i;
10191 
10192 		if (vnic_id >= bp->nr_vnics)
10193 			break;
10194 
10195 		vnic = &bp->vnic_info[vnic_id];
10196 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10197 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10198 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10199 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10200 			break;
10201 	}
10202 	return rc;
10203 }
10204 
10205 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10206 			  bool all)
10207 {
10208 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10209 	struct bnxt_filter_base *usr_fltr, *tmp;
10210 	struct bnxt_ntuple_filter *ntp_fltr;
10211 	int i;
10212 
10213 	bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10214 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10215 		if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10216 			bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10217 	}
10218 	if (!all)
10219 		return;
10220 
10221 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10222 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10223 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10224 			ntp_fltr = container_of(usr_fltr,
10225 						struct bnxt_ntuple_filter,
10226 						base);
10227 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10228 			bnxt_del_ntp_filter(bp, ntp_fltr);
10229 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10230 		}
10231 	}
10232 
10233 	if (vnic->rss_table)
10234 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10235 				  vnic->rss_table,
10236 				  vnic->rss_table_dma_addr);
10237 	kfree(rss_ctx->rss_indir_tbl);
10238 	list_del(&rss_ctx->list);
10239 	bp->num_rss_ctx--;
10240 	clear_bit(rss_ctx->index, bp->rss_ctx_bmap);
10241 	kfree(rss_ctx);
10242 }
10243 
10244 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10245 {
10246 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10247 	struct bnxt_rss_ctx *rss_ctx, *tmp;
10248 
10249 	list_for_each_entry_safe(rss_ctx, tmp, &bp->rss_ctx_list, list) {
10250 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10251 
10252 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10253 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10254 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10255 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10256 				   rss_ctx->index);
10257 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10258 		}
10259 	}
10260 }
10261 
10262 struct bnxt_rss_ctx *bnxt_alloc_rss_ctx(struct bnxt *bp)
10263 {
10264 	struct bnxt_rss_ctx *rss_ctx = NULL;
10265 
10266 	rss_ctx = kzalloc(sizeof(*rss_ctx), GFP_KERNEL);
10267 	if (rss_ctx) {
10268 		rss_ctx->vnic.rss_ctx = rss_ctx;
10269 		list_add_tail(&rss_ctx->list, &bp->rss_ctx_list);
10270 		bp->num_rss_ctx++;
10271 	}
10272 	return rss_ctx;
10273 }
10274 
10275 void bnxt_clear_rss_ctxs(struct bnxt *bp, bool all)
10276 {
10277 	struct bnxt_rss_ctx *rss_ctx, *tmp;
10278 
10279 	list_for_each_entry_safe(rss_ctx, tmp, &bp->rss_ctx_list, list)
10280 		bnxt_del_one_rss_ctx(bp, rss_ctx, all);
10281 
10282 	if (all)
10283 		bitmap_free(bp->rss_ctx_bmap);
10284 }
10285 
10286 static void bnxt_init_multi_rss_ctx(struct bnxt *bp)
10287 {
10288 	bp->rss_ctx_bmap = bitmap_zalloc(BNXT_RSS_CTX_BMAP_LEN, GFP_KERNEL);
10289 	if (bp->rss_ctx_bmap) {
10290 		/* burn index 0 since we cannot have context 0 */
10291 		__set_bit(0, bp->rss_ctx_bmap);
10292 		INIT_LIST_HEAD(&bp->rss_ctx_list);
10293 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
10294 	}
10295 }
10296 
10297 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
10298 static bool bnxt_promisc_ok(struct bnxt *bp)
10299 {
10300 #ifdef CONFIG_BNXT_SRIOV
10301 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10302 		return false;
10303 #endif
10304 	return true;
10305 }
10306 
10307 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10308 {
10309 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10310 	unsigned int rc = 0;
10311 
10312 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10313 	if (rc) {
10314 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10315 			   rc);
10316 		return rc;
10317 	}
10318 
10319 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10320 	if (rc) {
10321 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10322 			   rc);
10323 		return rc;
10324 	}
10325 	return rc;
10326 }
10327 
10328 static int bnxt_cfg_rx_mode(struct bnxt *);
10329 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10330 
10331 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10332 {
10333 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10334 	int rc = 0;
10335 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10336 
10337 	if (irq_re_init) {
10338 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10339 		if (rc) {
10340 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10341 				   rc);
10342 			goto err_out;
10343 		}
10344 	}
10345 
10346 	rc = bnxt_hwrm_ring_alloc(bp);
10347 	if (rc) {
10348 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10349 		goto err_out;
10350 	}
10351 
10352 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10353 	if (rc) {
10354 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10355 		goto err_out;
10356 	}
10357 
10358 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10359 		rx_nr_rings--;
10360 
10361 	/* default vnic 0 */
10362 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10363 	if (rc) {
10364 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10365 		goto err_out;
10366 	}
10367 
10368 	if (BNXT_VF(bp))
10369 		bnxt_hwrm_func_qcfg(bp);
10370 
10371 	rc = bnxt_setup_vnic(bp, vnic);
10372 	if (rc)
10373 		goto err_out;
10374 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10375 		bnxt_hwrm_update_rss_hash_cfg(bp);
10376 
10377 	if (bp->flags & BNXT_FLAG_RFS) {
10378 		rc = bnxt_alloc_rfs_vnics(bp);
10379 		if (rc)
10380 			goto err_out;
10381 	}
10382 
10383 	if (bp->flags & BNXT_FLAG_TPA) {
10384 		rc = bnxt_set_tpa(bp, true);
10385 		if (rc)
10386 			goto err_out;
10387 	}
10388 
10389 	if (BNXT_VF(bp))
10390 		bnxt_update_vf_mac(bp);
10391 
10392 	/* Filter for default vnic 0 */
10393 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
10394 	if (rc) {
10395 		if (BNXT_VF(bp) && rc == -ENODEV)
10396 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
10397 		else
10398 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
10399 		goto err_out;
10400 	}
10401 	vnic->uc_filter_count = 1;
10402 
10403 	vnic->rx_mask = 0;
10404 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
10405 		goto skip_rx_mask;
10406 
10407 	if (bp->dev->flags & IFF_BROADCAST)
10408 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10409 
10410 	if (bp->dev->flags & IFF_PROMISC)
10411 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10412 
10413 	if (bp->dev->flags & IFF_ALLMULTI) {
10414 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10415 		vnic->mc_list_count = 0;
10416 	} else if (bp->dev->flags & IFF_MULTICAST) {
10417 		u32 mask = 0;
10418 
10419 		bnxt_mc_list_updated(bp, &mask);
10420 		vnic->rx_mask |= mask;
10421 	}
10422 
10423 	rc = bnxt_cfg_rx_mode(bp);
10424 	if (rc)
10425 		goto err_out;
10426 
10427 skip_rx_mask:
10428 	rc = bnxt_hwrm_set_coal(bp);
10429 	if (rc)
10430 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
10431 				rc);
10432 
10433 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10434 		rc = bnxt_setup_nitroa0_vnic(bp);
10435 		if (rc)
10436 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
10437 				   rc);
10438 	}
10439 
10440 	if (BNXT_VF(bp)) {
10441 		bnxt_hwrm_func_qcfg(bp);
10442 		netdev_update_features(bp->dev);
10443 	}
10444 
10445 	return 0;
10446 
10447 err_out:
10448 	bnxt_hwrm_resource_free(bp, 0, true);
10449 
10450 	return rc;
10451 }
10452 
10453 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
10454 {
10455 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
10456 	return 0;
10457 }
10458 
10459 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
10460 {
10461 	bnxt_init_cp_rings(bp);
10462 	bnxt_init_rx_rings(bp);
10463 	bnxt_init_tx_rings(bp);
10464 	bnxt_init_ring_grps(bp, irq_re_init);
10465 	bnxt_init_vnics(bp);
10466 
10467 	return bnxt_init_chip(bp, irq_re_init);
10468 }
10469 
10470 static int bnxt_set_real_num_queues(struct bnxt *bp)
10471 {
10472 	int rc;
10473 	struct net_device *dev = bp->dev;
10474 
10475 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
10476 					  bp->tx_nr_rings_xdp);
10477 	if (rc)
10478 		return rc;
10479 
10480 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
10481 	if (rc)
10482 		return rc;
10483 
10484 #ifdef CONFIG_RFS_ACCEL
10485 	if (bp->flags & BNXT_FLAG_RFS)
10486 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
10487 #endif
10488 
10489 	return rc;
10490 }
10491 
10492 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10493 			     bool shared)
10494 {
10495 	int _rx = *rx, _tx = *tx;
10496 
10497 	if (shared) {
10498 		*rx = min_t(int, _rx, max);
10499 		*tx = min_t(int, _tx, max);
10500 	} else {
10501 		if (max < 2)
10502 			return -ENOMEM;
10503 
10504 		while (_rx + _tx > max) {
10505 			if (_rx > _tx && _rx > 1)
10506 				_rx--;
10507 			else if (_tx > 1)
10508 				_tx--;
10509 		}
10510 		*rx = _rx;
10511 		*tx = _tx;
10512 	}
10513 	return 0;
10514 }
10515 
10516 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
10517 {
10518 	return (tx - tx_xdp) / tx_sets + tx_xdp;
10519 }
10520 
10521 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
10522 {
10523 	int tcs = bp->num_tc;
10524 
10525 	if (!tcs)
10526 		tcs = 1;
10527 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
10528 }
10529 
10530 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
10531 {
10532 	int tcs = bp->num_tc;
10533 
10534 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
10535 	       bp->tx_nr_rings_xdp;
10536 }
10537 
10538 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10539 			   bool sh)
10540 {
10541 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
10542 
10543 	if (tx_cp != *tx) {
10544 		int tx_saved = tx_cp, rc;
10545 
10546 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
10547 		if (rc)
10548 			return rc;
10549 		if (tx_cp != tx_saved)
10550 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
10551 		return 0;
10552 	}
10553 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
10554 }
10555 
10556 static void bnxt_setup_msix(struct bnxt *bp)
10557 {
10558 	const int len = sizeof(bp->irq_tbl[0].name);
10559 	struct net_device *dev = bp->dev;
10560 	int tcs, i;
10561 
10562 	tcs = bp->num_tc;
10563 	if (tcs) {
10564 		int i, off, count;
10565 
10566 		for (i = 0; i < tcs; i++) {
10567 			count = bp->tx_nr_rings_per_tc;
10568 			off = BNXT_TC_TO_RING_BASE(bp, i);
10569 			netdev_set_tc_queue(dev, i, count, off);
10570 		}
10571 	}
10572 
10573 	for (i = 0; i < bp->cp_nr_rings; i++) {
10574 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10575 		char *attr;
10576 
10577 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
10578 			attr = "TxRx";
10579 		else if (i < bp->rx_nr_rings)
10580 			attr = "rx";
10581 		else
10582 			attr = "tx";
10583 
10584 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
10585 			 attr, i);
10586 		bp->irq_tbl[map_idx].handler = bnxt_msix;
10587 	}
10588 }
10589 
10590 static void bnxt_setup_inta(struct bnxt *bp)
10591 {
10592 	const int len = sizeof(bp->irq_tbl[0].name);
10593 
10594 	if (bp->num_tc) {
10595 		netdev_reset_tc(bp->dev);
10596 		bp->num_tc = 0;
10597 	}
10598 
10599 	snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
10600 		 0);
10601 	bp->irq_tbl[0].handler = bnxt_inta;
10602 }
10603 
10604 static int bnxt_init_int_mode(struct bnxt *bp);
10605 
10606 static int bnxt_setup_int_mode(struct bnxt *bp)
10607 {
10608 	int rc;
10609 
10610 	if (!bp->irq_tbl) {
10611 		rc = bnxt_init_int_mode(bp);
10612 		if (rc || !bp->irq_tbl)
10613 			return rc ?: -ENODEV;
10614 	}
10615 
10616 	if (bp->flags & BNXT_FLAG_USING_MSIX)
10617 		bnxt_setup_msix(bp);
10618 	else
10619 		bnxt_setup_inta(bp);
10620 
10621 	rc = bnxt_set_real_num_queues(bp);
10622 	return rc;
10623 }
10624 
10625 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
10626 {
10627 	return bp->hw_resc.max_rsscos_ctxs;
10628 }
10629 
10630 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
10631 {
10632 	return bp->hw_resc.max_vnics;
10633 }
10634 
10635 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
10636 {
10637 	return bp->hw_resc.max_stat_ctxs;
10638 }
10639 
10640 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
10641 {
10642 	return bp->hw_resc.max_cp_rings;
10643 }
10644 
10645 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
10646 {
10647 	unsigned int cp = bp->hw_resc.max_cp_rings;
10648 
10649 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
10650 		cp -= bnxt_get_ulp_msix_num(bp);
10651 
10652 	return cp;
10653 }
10654 
10655 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
10656 {
10657 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
10658 
10659 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10660 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
10661 
10662 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
10663 }
10664 
10665 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
10666 {
10667 	bp->hw_resc.max_irqs = max_irqs;
10668 }
10669 
10670 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
10671 {
10672 	unsigned int cp;
10673 
10674 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
10675 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10676 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
10677 	else
10678 		return cp - bp->cp_nr_rings;
10679 }
10680 
10681 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
10682 {
10683 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
10684 }
10685 
10686 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
10687 {
10688 	int max_irq = bnxt_get_max_func_irqs(bp);
10689 	int total_req = bp->cp_nr_rings + num;
10690 
10691 	if (max_irq < total_req) {
10692 		num = max_irq - bp->cp_nr_rings;
10693 		if (num <= 0)
10694 			return 0;
10695 	}
10696 	return num;
10697 }
10698 
10699 static int bnxt_get_num_msix(struct bnxt *bp)
10700 {
10701 	if (!BNXT_NEW_RM(bp))
10702 		return bnxt_get_max_func_irqs(bp);
10703 
10704 	return bnxt_nq_rings_in_use(bp);
10705 }
10706 
10707 static int bnxt_init_msix(struct bnxt *bp)
10708 {
10709 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp;
10710 	struct msix_entry *msix_ent;
10711 
10712 	total_vecs = bnxt_get_num_msix(bp);
10713 	max = bnxt_get_max_func_irqs(bp);
10714 	if (total_vecs > max)
10715 		total_vecs = max;
10716 
10717 	if (!total_vecs)
10718 		return 0;
10719 
10720 	msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
10721 	if (!msix_ent)
10722 		return -ENOMEM;
10723 
10724 	for (i = 0; i < total_vecs; i++) {
10725 		msix_ent[i].entry = i;
10726 		msix_ent[i].vector = 0;
10727 	}
10728 
10729 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
10730 		min = 2;
10731 
10732 	total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
10733 	ulp_msix = bnxt_get_ulp_msix_num(bp);
10734 	if (total_vecs < 0 || total_vecs < ulp_msix) {
10735 		rc = -ENODEV;
10736 		goto msix_setup_exit;
10737 	}
10738 
10739 	bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
10740 	if (bp->irq_tbl) {
10741 		for (i = 0; i < total_vecs; i++)
10742 			bp->irq_tbl[i].vector = msix_ent[i].vector;
10743 
10744 		bp->total_irqs = total_vecs;
10745 		/* Trim rings based upon num of vectors allocated */
10746 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
10747 				     total_vecs - ulp_msix, min == 1);
10748 		if (rc)
10749 			goto msix_setup_exit;
10750 
10751 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
10752 		bp->cp_nr_rings = (min == 1) ?
10753 				  max_t(int, tx_cp, bp->rx_nr_rings) :
10754 				  tx_cp + bp->rx_nr_rings;
10755 
10756 	} else {
10757 		rc = -ENOMEM;
10758 		goto msix_setup_exit;
10759 	}
10760 	bp->flags |= BNXT_FLAG_USING_MSIX;
10761 	kfree(msix_ent);
10762 	return 0;
10763 
10764 msix_setup_exit:
10765 	netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
10766 	kfree(bp->irq_tbl);
10767 	bp->irq_tbl = NULL;
10768 	pci_disable_msix(bp->pdev);
10769 	kfree(msix_ent);
10770 	return rc;
10771 }
10772 
10773 static int bnxt_init_inta(struct bnxt *bp)
10774 {
10775 	bp->irq_tbl = kzalloc(sizeof(struct bnxt_irq), GFP_KERNEL);
10776 	if (!bp->irq_tbl)
10777 		return -ENOMEM;
10778 
10779 	bp->total_irqs = 1;
10780 	bp->rx_nr_rings = 1;
10781 	bp->tx_nr_rings = 1;
10782 	bp->cp_nr_rings = 1;
10783 	bp->flags |= BNXT_FLAG_SHARED_RINGS;
10784 	bp->irq_tbl[0].vector = bp->pdev->irq;
10785 	return 0;
10786 }
10787 
10788 static int bnxt_init_int_mode(struct bnxt *bp)
10789 {
10790 	int rc = -ENODEV;
10791 
10792 	if (bp->flags & BNXT_FLAG_MSIX_CAP)
10793 		rc = bnxt_init_msix(bp);
10794 
10795 	if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
10796 		/* fallback to INTA */
10797 		rc = bnxt_init_inta(bp);
10798 	}
10799 	return rc;
10800 }
10801 
10802 static void bnxt_clear_int_mode(struct bnxt *bp)
10803 {
10804 	if (bp->flags & BNXT_FLAG_USING_MSIX)
10805 		pci_disable_msix(bp->pdev);
10806 
10807 	kfree(bp->irq_tbl);
10808 	bp->irq_tbl = NULL;
10809 	bp->flags &= ~BNXT_FLAG_USING_MSIX;
10810 }
10811 
10812 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
10813 {
10814 	bool irq_cleared = false;
10815 	int tcs = bp->num_tc;
10816 	int irqs_required;
10817 	int rc;
10818 
10819 	if (!bnxt_need_reserve_rings(bp))
10820 		return 0;
10821 
10822 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
10823 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
10824 
10825 		if (ulp_msix > bp->ulp_num_msix_want)
10826 			ulp_msix = bp->ulp_num_msix_want;
10827 		irqs_required = ulp_msix + bp->cp_nr_rings;
10828 	} else {
10829 		irqs_required = bnxt_get_num_msix(bp);
10830 	}
10831 
10832 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
10833 		bnxt_ulp_irq_stop(bp);
10834 		bnxt_clear_int_mode(bp);
10835 		irq_cleared = true;
10836 	}
10837 	rc = __bnxt_reserve_rings(bp);
10838 	if (irq_cleared) {
10839 		if (!rc)
10840 			rc = bnxt_init_int_mode(bp);
10841 		bnxt_ulp_irq_restart(bp, rc);
10842 	}
10843 	if (rc) {
10844 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
10845 		return rc;
10846 	}
10847 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
10848 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
10849 		netdev_err(bp->dev, "tx ring reservation failure\n");
10850 		netdev_reset_tc(bp->dev);
10851 		bp->num_tc = 0;
10852 		if (bp->tx_nr_rings_xdp)
10853 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
10854 		else
10855 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
10856 		return -ENOMEM;
10857 	}
10858 	return 0;
10859 }
10860 
10861 static void bnxt_free_irq(struct bnxt *bp)
10862 {
10863 	struct bnxt_irq *irq;
10864 	int i;
10865 
10866 #ifdef CONFIG_RFS_ACCEL
10867 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
10868 	bp->dev->rx_cpu_rmap = NULL;
10869 #endif
10870 	if (!bp->irq_tbl || !bp->bnapi)
10871 		return;
10872 
10873 	for (i = 0; i < bp->cp_nr_rings; i++) {
10874 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10875 
10876 		irq = &bp->irq_tbl[map_idx];
10877 		if (irq->requested) {
10878 			if (irq->have_cpumask) {
10879 				irq_set_affinity_hint(irq->vector, NULL);
10880 				free_cpumask_var(irq->cpu_mask);
10881 				irq->have_cpumask = 0;
10882 			}
10883 			free_irq(irq->vector, bp->bnapi[i]);
10884 		}
10885 
10886 		irq->requested = 0;
10887 	}
10888 }
10889 
10890 static int bnxt_request_irq(struct bnxt *bp)
10891 {
10892 	int i, j, rc = 0;
10893 	unsigned long flags = 0;
10894 #ifdef CONFIG_RFS_ACCEL
10895 	struct cpu_rmap *rmap;
10896 #endif
10897 
10898 	rc = bnxt_setup_int_mode(bp);
10899 	if (rc) {
10900 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
10901 			   rc);
10902 		return rc;
10903 	}
10904 #ifdef CONFIG_RFS_ACCEL
10905 	rmap = bp->dev->rx_cpu_rmap;
10906 #endif
10907 	if (!(bp->flags & BNXT_FLAG_USING_MSIX))
10908 		flags = IRQF_SHARED;
10909 
10910 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
10911 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10912 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
10913 
10914 #ifdef CONFIG_RFS_ACCEL
10915 		if (rmap && bp->bnapi[i]->rx_ring) {
10916 			rc = irq_cpu_rmap_add(rmap, irq->vector);
10917 			if (rc)
10918 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
10919 					    j);
10920 			j++;
10921 		}
10922 #endif
10923 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
10924 				 bp->bnapi[i]);
10925 		if (rc)
10926 			break;
10927 
10928 		netif_napi_set_irq(&bp->bnapi[i]->napi, irq->vector);
10929 		irq->requested = 1;
10930 
10931 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
10932 			int numa_node = dev_to_node(&bp->pdev->dev);
10933 
10934 			irq->have_cpumask = 1;
10935 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
10936 					irq->cpu_mask);
10937 			rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
10938 			if (rc) {
10939 				netdev_warn(bp->dev,
10940 					    "Set affinity failed, IRQ = %d\n",
10941 					    irq->vector);
10942 				break;
10943 			}
10944 		}
10945 	}
10946 	return rc;
10947 }
10948 
10949 static void bnxt_del_napi(struct bnxt *bp)
10950 {
10951 	int i;
10952 
10953 	if (!bp->bnapi)
10954 		return;
10955 
10956 	for (i = 0; i < bp->rx_nr_rings; i++)
10957 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
10958 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
10959 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
10960 
10961 	for (i = 0; i < bp->cp_nr_rings; i++) {
10962 		struct bnxt_napi *bnapi = bp->bnapi[i];
10963 
10964 		__netif_napi_del(&bnapi->napi);
10965 	}
10966 	/* We called __netif_napi_del(), we need
10967 	 * to respect an RCU grace period before freeing napi structures.
10968 	 */
10969 	synchronize_net();
10970 }
10971 
10972 static void bnxt_init_napi(struct bnxt *bp)
10973 {
10974 	int i;
10975 	unsigned int cp_nr_rings = bp->cp_nr_rings;
10976 	struct bnxt_napi *bnapi;
10977 
10978 	if (bp->flags & BNXT_FLAG_USING_MSIX) {
10979 		int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
10980 
10981 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10982 			poll_fn = bnxt_poll_p5;
10983 		else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10984 			cp_nr_rings--;
10985 		for (i = 0; i < cp_nr_rings; i++) {
10986 			bnapi = bp->bnapi[i];
10987 			netif_napi_add(bp->dev, &bnapi->napi, poll_fn);
10988 		}
10989 		if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10990 			bnapi = bp->bnapi[cp_nr_rings];
10991 			netif_napi_add(bp->dev, &bnapi->napi,
10992 				       bnxt_poll_nitroa0);
10993 		}
10994 	} else {
10995 		bnapi = bp->bnapi[0];
10996 		netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll);
10997 	}
10998 }
10999 
11000 static void bnxt_disable_napi(struct bnxt *bp)
11001 {
11002 	int i;
11003 
11004 	if (!bp->bnapi ||
11005 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11006 		return;
11007 
11008 	for (i = 0; i < bp->cp_nr_rings; i++) {
11009 		struct bnxt_napi *bnapi = bp->bnapi[i];
11010 		struct bnxt_cp_ring_info *cpr;
11011 
11012 		cpr = &bnapi->cp_ring;
11013 		if (bnapi->tx_fault)
11014 			cpr->sw_stats->tx.tx_resets++;
11015 		if (bnapi->in_reset)
11016 			cpr->sw_stats->rx.rx_resets++;
11017 		napi_disable(&bnapi->napi);
11018 		if (bnapi->rx_ring)
11019 			cancel_work_sync(&cpr->dim.work);
11020 	}
11021 }
11022 
11023 static void bnxt_enable_napi(struct bnxt *bp)
11024 {
11025 	int i;
11026 
11027 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11028 	for (i = 0; i < bp->cp_nr_rings; i++) {
11029 		struct bnxt_napi *bnapi = bp->bnapi[i];
11030 		struct bnxt_cp_ring_info *cpr;
11031 
11032 		bnapi->tx_fault = 0;
11033 
11034 		cpr = &bnapi->cp_ring;
11035 		bnapi->in_reset = false;
11036 
11037 		if (bnapi->rx_ring) {
11038 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
11039 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
11040 		}
11041 		napi_enable(&bnapi->napi);
11042 	}
11043 }
11044 
11045 void bnxt_tx_disable(struct bnxt *bp)
11046 {
11047 	int i;
11048 	struct bnxt_tx_ring_info *txr;
11049 
11050 	if (bp->tx_ring) {
11051 		for (i = 0; i < bp->tx_nr_rings; i++) {
11052 			txr = &bp->tx_ring[i];
11053 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11054 		}
11055 	}
11056 	/* Make sure napi polls see @dev_state change */
11057 	synchronize_net();
11058 	/* Drop carrier first to prevent TX timeout */
11059 	netif_carrier_off(bp->dev);
11060 	/* Stop all TX queues */
11061 	netif_tx_disable(bp->dev);
11062 }
11063 
11064 void bnxt_tx_enable(struct bnxt *bp)
11065 {
11066 	int i;
11067 	struct bnxt_tx_ring_info *txr;
11068 
11069 	for (i = 0; i < bp->tx_nr_rings; i++) {
11070 		txr = &bp->tx_ring[i];
11071 		WRITE_ONCE(txr->dev_state, 0);
11072 	}
11073 	/* Make sure napi polls see @dev_state change */
11074 	synchronize_net();
11075 	netif_tx_wake_all_queues(bp->dev);
11076 	if (BNXT_LINK_IS_UP(bp))
11077 		netif_carrier_on(bp->dev);
11078 }
11079 
11080 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
11081 {
11082 	u8 active_fec = link_info->active_fec_sig_mode &
11083 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
11084 
11085 	switch (active_fec) {
11086 	default:
11087 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
11088 		return "None";
11089 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
11090 		return "Clause 74 BaseR";
11091 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
11092 		return "Clause 91 RS(528,514)";
11093 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
11094 		return "Clause 91 RS544_1XN";
11095 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
11096 		return "Clause 91 RS(544,514)";
11097 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
11098 		return "Clause 91 RS272_1XN";
11099 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
11100 		return "Clause 91 RS(272,257)";
11101 	}
11102 }
11103 
11104 void bnxt_report_link(struct bnxt *bp)
11105 {
11106 	if (BNXT_LINK_IS_UP(bp)) {
11107 		const char *signal = "";
11108 		const char *flow_ctrl;
11109 		const char *duplex;
11110 		u32 speed;
11111 		u16 fec;
11112 
11113 		netif_carrier_on(bp->dev);
11114 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
11115 		if (speed == SPEED_UNKNOWN) {
11116 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
11117 			return;
11118 		}
11119 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
11120 			duplex = "full";
11121 		else
11122 			duplex = "half";
11123 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
11124 			flow_ctrl = "ON - receive & transmit";
11125 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
11126 			flow_ctrl = "ON - transmit";
11127 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
11128 			flow_ctrl = "ON - receive";
11129 		else
11130 			flow_ctrl = "none";
11131 		if (bp->link_info.phy_qcfg_resp.option_flags &
11132 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
11133 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
11134 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
11135 			switch (sig_mode) {
11136 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
11137 				signal = "(NRZ) ";
11138 				break;
11139 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
11140 				signal = "(PAM4 56Gbps) ";
11141 				break;
11142 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
11143 				signal = "(PAM4 112Gbps) ";
11144 				break;
11145 			default:
11146 				break;
11147 			}
11148 		}
11149 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
11150 			    speed, signal, duplex, flow_ctrl);
11151 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
11152 			netdev_info(bp->dev, "EEE is %s\n",
11153 				    bp->eee.eee_active ? "active" :
11154 							 "not active");
11155 		fec = bp->link_info.fec_cfg;
11156 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
11157 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
11158 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
11159 				    bnxt_report_fec(&bp->link_info));
11160 	} else {
11161 		netif_carrier_off(bp->dev);
11162 		netdev_err(bp->dev, "NIC Link is Down\n");
11163 	}
11164 }
11165 
11166 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
11167 {
11168 	if (!resp->supported_speeds_auto_mode &&
11169 	    !resp->supported_speeds_force_mode &&
11170 	    !resp->supported_pam4_speeds_auto_mode &&
11171 	    !resp->supported_pam4_speeds_force_mode &&
11172 	    !resp->supported_speeds2_auto_mode &&
11173 	    !resp->supported_speeds2_force_mode)
11174 		return true;
11175 	return false;
11176 }
11177 
11178 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
11179 {
11180 	struct bnxt_link_info *link_info = &bp->link_info;
11181 	struct hwrm_port_phy_qcaps_output *resp;
11182 	struct hwrm_port_phy_qcaps_input *req;
11183 	int rc = 0;
11184 
11185 	if (bp->hwrm_spec_code < 0x10201)
11186 		return 0;
11187 
11188 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
11189 	if (rc)
11190 		return rc;
11191 
11192 	resp = hwrm_req_hold(bp, req);
11193 	rc = hwrm_req_send(bp, req);
11194 	if (rc)
11195 		goto hwrm_phy_qcaps_exit;
11196 
11197 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
11198 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
11199 		struct ethtool_keee *eee = &bp->eee;
11200 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
11201 
11202 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
11203 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
11204 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11205 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11206 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11207 	}
11208 
11209 	if (bp->hwrm_spec_code >= 0x10a01) {
11210 		if (bnxt_phy_qcaps_no_speed(resp)) {
11211 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11212 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11213 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11214 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11215 			netdev_info(bp->dev, "Ethernet link enabled\n");
11216 			/* Phy re-enabled, reprobe the speeds */
11217 			link_info->support_auto_speeds = 0;
11218 			link_info->support_pam4_auto_speeds = 0;
11219 			link_info->support_auto_speeds2 = 0;
11220 		}
11221 	}
11222 	if (resp->supported_speeds_auto_mode)
11223 		link_info->support_auto_speeds =
11224 			le16_to_cpu(resp->supported_speeds_auto_mode);
11225 	if (resp->supported_pam4_speeds_auto_mode)
11226 		link_info->support_pam4_auto_speeds =
11227 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11228 	if (resp->supported_speeds2_auto_mode)
11229 		link_info->support_auto_speeds2 =
11230 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11231 
11232 	bp->port_count = resp->port_cnt;
11233 
11234 hwrm_phy_qcaps_exit:
11235 	hwrm_req_drop(bp, req);
11236 	return rc;
11237 }
11238 
11239 static bool bnxt_support_dropped(u16 advertising, u16 supported)
11240 {
11241 	u16 diff = advertising ^ supported;
11242 
11243 	return ((supported | diff) != supported);
11244 }
11245 
11246 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
11247 {
11248 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
11249 
11250 	/* Check if any advertised speeds are no longer supported. The caller
11251 	 * holds the link_lock mutex, so we can modify link_info settings.
11252 	 */
11253 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11254 		if (bnxt_support_dropped(link_info->advertising,
11255 					 link_info->support_auto_speeds2)) {
11256 			link_info->advertising = link_info->support_auto_speeds2;
11257 			return true;
11258 		}
11259 		return false;
11260 	}
11261 	if (bnxt_support_dropped(link_info->advertising,
11262 				 link_info->support_auto_speeds)) {
11263 		link_info->advertising = link_info->support_auto_speeds;
11264 		return true;
11265 	}
11266 	if (bnxt_support_dropped(link_info->advertising_pam4,
11267 				 link_info->support_pam4_auto_speeds)) {
11268 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
11269 		return true;
11270 	}
11271 	return false;
11272 }
11273 
11274 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
11275 {
11276 	struct bnxt_link_info *link_info = &bp->link_info;
11277 	struct hwrm_port_phy_qcfg_output *resp;
11278 	struct hwrm_port_phy_qcfg_input *req;
11279 	u8 link_state = link_info->link_state;
11280 	bool support_changed;
11281 	int rc;
11282 
11283 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
11284 	if (rc)
11285 		return rc;
11286 
11287 	resp = hwrm_req_hold(bp, req);
11288 	rc = hwrm_req_send(bp, req);
11289 	if (rc) {
11290 		hwrm_req_drop(bp, req);
11291 		if (BNXT_VF(bp) && rc == -ENODEV) {
11292 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
11293 			rc = 0;
11294 		}
11295 		return rc;
11296 	}
11297 
11298 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
11299 	link_info->phy_link_status = resp->link;
11300 	link_info->duplex = resp->duplex_cfg;
11301 	if (bp->hwrm_spec_code >= 0x10800)
11302 		link_info->duplex = resp->duplex_state;
11303 	link_info->pause = resp->pause;
11304 	link_info->auto_mode = resp->auto_mode;
11305 	link_info->auto_pause_setting = resp->auto_pause;
11306 	link_info->lp_pause = resp->link_partner_adv_pause;
11307 	link_info->force_pause_setting = resp->force_pause;
11308 	link_info->duplex_setting = resp->duplex_cfg;
11309 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
11310 		link_info->link_speed = le16_to_cpu(resp->link_speed);
11311 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
11312 			link_info->active_lanes = resp->active_lanes;
11313 	} else {
11314 		link_info->link_speed = 0;
11315 		link_info->active_lanes = 0;
11316 	}
11317 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
11318 	link_info->force_pam4_link_speed =
11319 		le16_to_cpu(resp->force_pam4_link_speed);
11320 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
11321 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
11322 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
11323 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
11324 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
11325 	link_info->auto_pam4_link_speeds =
11326 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
11327 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
11328 	link_info->lp_auto_link_speeds =
11329 		le16_to_cpu(resp->link_partner_adv_speeds);
11330 	link_info->lp_auto_pam4_link_speeds =
11331 		resp->link_partner_pam4_adv_speeds;
11332 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
11333 	link_info->phy_ver[0] = resp->phy_maj;
11334 	link_info->phy_ver[1] = resp->phy_min;
11335 	link_info->phy_ver[2] = resp->phy_bld;
11336 	link_info->media_type = resp->media_type;
11337 	link_info->phy_type = resp->phy_type;
11338 	link_info->transceiver = resp->xcvr_pkg_type;
11339 	link_info->phy_addr = resp->eee_config_phy_addr &
11340 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
11341 	link_info->module_status = resp->module_status;
11342 
11343 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
11344 		struct ethtool_keee *eee = &bp->eee;
11345 		u16 fw_speeds;
11346 
11347 		eee->eee_active = 0;
11348 		if (resp->eee_config_phy_addr &
11349 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
11350 			eee->eee_active = 1;
11351 			fw_speeds = le16_to_cpu(
11352 				resp->link_partner_adv_eee_link_speed_mask);
11353 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
11354 		}
11355 
11356 		/* Pull initial EEE config */
11357 		if (!chng_link_state) {
11358 			if (resp->eee_config_phy_addr &
11359 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
11360 				eee->eee_enabled = 1;
11361 
11362 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
11363 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
11364 
11365 			if (resp->eee_config_phy_addr &
11366 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
11367 				__le32 tmr;
11368 
11369 				eee->tx_lpi_enabled = 1;
11370 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
11371 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
11372 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
11373 			}
11374 		}
11375 	}
11376 
11377 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
11378 	if (bp->hwrm_spec_code >= 0x10504) {
11379 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
11380 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
11381 	}
11382 	/* TODO: need to add more logic to report VF link */
11383 	if (chng_link_state) {
11384 		if (link_info->phy_link_status == BNXT_LINK_LINK)
11385 			link_info->link_state = BNXT_LINK_STATE_UP;
11386 		else
11387 			link_info->link_state = BNXT_LINK_STATE_DOWN;
11388 		if (link_state != link_info->link_state)
11389 			bnxt_report_link(bp);
11390 	} else {
11391 		/* always link down if not require to update link state */
11392 		link_info->link_state = BNXT_LINK_STATE_DOWN;
11393 	}
11394 	hwrm_req_drop(bp, req);
11395 
11396 	if (!BNXT_PHY_CFG_ABLE(bp))
11397 		return 0;
11398 
11399 	support_changed = bnxt_support_speed_dropped(link_info);
11400 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
11401 		bnxt_hwrm_set_link_setting(bp, true, false);
11402 	return 0;
11403 }
11404 
11405 static void bnxt_get_port_module_status(struct bnxt *bp)
11406 {
11407 	struct bnxt_link_info *link_info = &bp->link_info;
11408 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
11409 	u8 module_status;
11410 
11411 	if (bnxt_update_link(bp, true))
11412 		return;
11413 
11414 	module_status = link_info->module_status;
11415 	switch (module_status) {
11416 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
11417 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
11418 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
11419 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
11420 			    bp->pf.port_id);
11421 		if (bp->hwrm_spec_code >= 0x10201) {
11422 			netdev_warn(bp->dev, "Module part number %s\n",
11423 				    resp->phy_vendor_partnumber);
11424 		}
11425 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
11426 			netdev_warn(bp->dev, "TX is disabled\n");
11427 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
11428 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
11429 	}
11430 }
11431 
11432 static void
11433 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11434 {
11435 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
11436 		if (bp->hwrm_spec_code >= 0x10201)
11437 			req->auto_pause =
11438 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
11439 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11440 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
11441 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11442 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
11443 		req->enables |=
11444 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11445 	} else {
11446 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11447 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
11448 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11449 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
11450 		req->enables |=
11451 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
11452 		if (bp->hwrm_spec_code >= 0x10201) {
11453 			req->auto_pause = req->force_pause;
11454 			req->enables |= cpu_to_le32(
11455 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11456 		}
11457 	}
11458 }
11459 
11460 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11461 {
11462 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
11463 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
11464 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11465 			req->enables |=
11466 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
11467 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
11468 		} else if (bp->link_info.advertising) {
11469 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
11470 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
11471 		}
11472 		if (bp->link_info.advertising_pam4) {
11473 			req->enables |=
11474 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
11475 			req->auto_link_pam4_speed_mask =
11476 				cpu_to_le16(bp->link_info.advertising_pam4);
11477 		}
11478 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
11479 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
11480 	} else {
11481 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
11482 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11483 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
11484 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
11485 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
11486 				   (u32)bp->link_info.req_link_speed);
11487 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
11488 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11489 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
11490 		} else {
11491 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11492 		}
11493 	}
11494 
11495 	/* tell chimp that the setting takes effect immediately */
11496 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
11497 }
11498 
11499 int bnxt_hwrm_set_pause(struct bnxt *bp)
11500 {
11501 	struct hwrm_port_phy_cfg_input *req;
11502 	int rc;
11503 
11504 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11505 	if (rc)
11506 		return rc;
11507 
11508 	bnxt_hwrm_set_pause_common(bp, req);
11509 
11510 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
11511 	    bp->link_info.force_link_chng)
11512 		bnxt_hwrm_set_link_common(bp, req);
11513 
11514 	rc = hwrm_req_send(bp, req);
11515 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
11516 		/* since changing of pause setting doesn't trigger any link
11517 		 * change event, the driver needs to update the current pause
11518 		 * result upon successfully return of the phy_cfg command
11519 		 */
11520 		bp->link_info.pause =
11521 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
11522 		bp->link_info.auto_pause_setting = 0;
11523 		if (!bp->link_info.force_link_chng)
11524 			bnxt_report_link(bp);
11525 	}
11526 	bp->link_info.force_link_chng = false;
11527 	return rc;
11528 }
11529 
11530 static void bnxt_hwrm_set_eee(struct bnxt *bp,
11531 			      struct hwrm_port_phy_cfg_input *req)
11532 {
11533 	struct ethtool_keee *eee = &bp->eee;
11534 
11535 	if (eee->eee_enabled) {
11536 		u16 eee_speeds;
11537 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
11538 
11539 		if (eee->tx_lpi_enabled)
11540 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
11541 		else
11542 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
11543 
11544 		req->flags |= cpu_to_le32(flags);
11545 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
11546 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
11547 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
11548 	} else {
11549 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
11550 	}
11551 }
11552 
11553 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
11554 {
11555 	struct hwrm_port_phy_cfg_input *req;
11556 	int rc;
11557 
11558 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11559 	if (rc)
11560 		return rc;
11561 
11562 	if (set_pause)
11563 		bnxt_hwrm_set_pause_common(bp, req);
11564 
11565 	bnxt_hwrm_set_link_common(bp, req);
11566 
11567 	if (set_eee)
11568 		bnxt_hwrm_set_eee(bp, req);
11569 	return hwrm_req_send(bp, req);
11570 }
11571 
11572 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
11573 {
11574 	struct hwrm_port_phy_cfg_input *req;
11575 	int rc;
11576 
11577 	if (!BNXT_SINGLE_PF(bp))
11578 		return 0;
11579 
11580 	if (pci_num_vf(bp->pdev) &&
11581 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
11582 		return 0;
11583 
11584 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11585 	if (rc)
11586 		return rc;
11587 
11588 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
11589 	rc = hwrm_req_send(bp, req);
11590 	if (!rc) {
11591 		mutex_lock(&bp->link_lock);
11592 		/* Device is not obliged link down in certain scenarios, even
11593 		 * when forced. Setting the state unknown is consistent with
11594 		 * driver startup and will force link state to be reported
11595 		 * during subsequent open based on PORT_PHY_QCFG.
11596 		 */
11597 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
11598 		mutex_unlock(&bp->link_lock);
11599 	}
11600 	return rc;
11601 }
11602 
11603 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
11604 {
11605 #ifdef CONFIG_TEE_BNXT_FW
11606 	int rc = tee_bnxt_fw_load();
11607 
11608 	if (rc)
11609 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
11610 
11611 	return rc;
11612 #else
11613 	netdev_err(bp->dev, "OP-TEE not supported\n");
11614 	return -ENODEV;
11615 #endif
11616 }
11617 
11618 static int bnxt_try_recover_fw(struct bnxt *bp)
11619 {
11620 	if (bp->fw_health && bp->fw_health->status_reliable) {
11621 		int retry = 0, rc;
11622 		u32 sts;
11623 
11624 		do {
11625 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
11626 			rc = bnxt_hwrm_poll(bp);
11627 			if (!BNXT_FW_IS_BOOTING(sts) &&
11628 			    !BNXT_FW_IS_RECOVERING(sts))
11629 				break;
11630 			retry++;
11631 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
11632 
11633 		if (!BNXT_FW_IS_HEALTHY(sts)) {
11634 			netdev_err(bp->dev,
11635 				   "Firmware not responding, status: 0x%x\n",
11636 				   sts);
11637 			rc = -ENODEV;
11638 		}
11639 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
11640 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
11641 			return bnxt_fw_reset_via_optee(bp);
11642 		}
11643 		return rc;
11644 	}
11645 
11646 	return -ENODEV;
11647 }
11648 
11649 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
11650 {
11651 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11652 
11653 	if (!BNXT_NEW_RM(bp))
11654 		return; /* no resource reservations required */
11655 
11656 	hw_resc->resv_cp_rings = 0;
11657 	hw_resc->resv_stat_ctxs = 0;
11658 	hw_resc->resv_irqs = 0;
11659 	hw_resc->resv_tx_rings = 0;
11660 	hw_resc->resv_rx_rings = 0;
11661 	hw_resc->resv_hw_ring_grps = 0;
11662 	hw_resc->resv_vnics = 0;
11663 	hw_resc->resv_rsscos_ctxs = 0;
11664 	if (!fw_reset) {
11665 		bp->tx_nr_rings = 0;
11666 		bp->rx_nr_rings = 0;
11667 	}
11668 }
11669 
11670 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
11671 {
11672 	int rc;
11673 
11674 	if (!BNXT_NEW_RM(bp))
11675 		return 0; /* no resource reservations required */
11676 
11677 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
11678 	if (rc)
11679 		netdev_err(bp->dev, "resc_qcaps failed\n");
11680 
11681 	bnxt_clear_reservations(bp, fw_reset);
11682 
11683 	return rc;
11684 }
11685 
11686 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
11687 {
11688 	struct hwrm_func_drv_if_change_output *resp;
11689 	struct hwrm_func_drv_if_change_input *req;
11690 	bool fw_reset = !bp->irq_tbl;
11691 	bool resc_reinit = false;
11692 	int rc, retry = 0;
11693 	u32 flags = 0;
11694 
11695 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
11696 		return 0;
11697 
11698 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
11699 	if (rc)
11700 		return rc;
11701 
11702 	if (up)
11703 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
11704 	resp = hwrm_req_hold(bp, req);
11705 
11706 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
11707 	while (retry < BNXT_FW_IF_RETRY) {
11708 		rc = hwrm_req_send(bp, req);
11709 		if (rc != -EAGAIN)
11710 			break;
11711 
11712 		msleep(50);
11713 		retry++;
11714 	}
11715 
11716 	if (rc == -EAGAIN) {
11717 		hwrm_req_drop(bp, req);
11718 		return rc;
11719 	} else if (!rc) {
11720 		flags = le32_to_cpu(resp->flags);
11721 	} else if (up) {
11722 		rc = bnxt_try_recover_fw(bp);
11723 		fw_reset = true;
11724 	}
11725 	hwrm_req_drop(bp, req);
11726 	if (rc)
11727 		return rc;
11728 
11729 	if (!up) {
11730 		bnxt_inv_fw_health_reg(bp);
11731 		return 0;
11732 	}
11733 
11734 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
11735 		resc_reinit = true;
11736 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
11737 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
11738 		fw_reset = true;
11739 	else
11740 		bnxt_remap_fw_health_regs(bp);
11741 
11742 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
11743 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
11744 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
11745 		return -ENODEV;
11746 	}
11747 	if (resc_reinit || fw_reset) {
11748 		if (fw_reset) {
11749 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11750 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11751 				bnxt_ulp_irq_stop(bp);
11752 			bnxt_free_ctx_mem(bp);
11753 			bnxt_dcb_free(bp);
11754 			rc = bnxt_fw_init_one(bp);
11755 			if (rc) {
11756 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11757 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
11758 				return rc;
11759 			}
11760 			bnxt_clear_int_mode(bp);
11761 			rc = bnxt_init_int_mode(bp);
11762 			if (rc) {
11763 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11764 				netdev_err(bp->dev, "init int mode failed\n");
11765 				return rc;
11766 			}
11767 		}
11768 		rc = bnxt_cancel_reservations(bp, fw_reset);
11769 	}
11770 	return rc;
11771 }
11772 
11773 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
11774 {
11775 	struct hwrm_port_led_qcaps_output *resp;
11776 	struct hwrm_port_led_qcaps_input *req;
11777 	struct bnxt_pf_info *pf = &bp->pf;
11778 	int rc;
11779 
11780 	bp->num_leds = 0;
11781 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
11782 		return 0;
11783 
11784 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
11785 	if (rc)
11786 		return rc;
11787 
11788 	req->port_id = cpu_to_le16(pf->port_id);
11789 	resp = hwrm_req_hold(bp, req);
11790 	rc = hwrm_req_send(bp, req);
11791 	if (rc) {
11792 		hwrm_req_drop(bp, req);
11793 		return rc;
11794 	}
11795 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
11796 		int i;
11797 
11798 		bp->num_leds = resp->num_leds;
11799 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
11800 						 bp->num_leds);
11801 		for (i = 0; i < bp->num_leds; i++) {
11802 			struct bnxt_led_info *led = &bp->leds[i];
11803 			__le16 caps = led->led_state_caps;
11804 
11805 			if (!led->led_group_id ||
11806 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
11807 				bp->num_leds = 0;
11808 				break;
11809 			}
11810 		}
11811 	}
11812 	hwrm_req_drop(bp, req);
11813 	return 0;
11814 }
11815 
11816 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
11817 {
11818 	struct hwrm_wol_filter_alloc_output *resp;
11819 	struct hwrm_wol_filter_alloc_input *req;
11820 	int rc;
11821 
11822 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
11823 	if (rc)
11824 		return rc;
11825 
11826 	req->port_id = cpu_to_le16(bp->pf.port_id);
11827 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
11828 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
11829 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
11830 
11831 	resp = hwrm_req_hold(bp, req);
11832 	rc = hwrm_req_send(bp, req);
11833 	if (!rc)
11834 		bp->wol_filter_id = resp->wol_filter_id;
11835 	hwrm_req_drop(bp, req);
11836 	return rc;
11837 }
11838 
11839 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
11840 {
11841 	struct hwrm_wol_filter_free_input *req;
11842 	int rc;
11843 
11844 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
11845 	if (rc)
11846 		return rc;
11847 
11848 	req->port_id = cpu_to_le16(bp->pf.port_id);
11849 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
11850 	req->wol_filter_id = bp->wol_filter_id;
11851 
11852 	return hwrm_req_send(bp, req);
11853 }
11854 
11855 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
11856 {
11857 	struct hwrm_wol_filter_qcfg_output *resp;
11858 	struct hwrm_wol_filter_qcfg_input *req;
11859 	u16 next_handle = 0;
11860 	int rc;
11861 
11862 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
11863 	if (rc)
11864 		return rc;
11865 
11866 	req->port_id = cpu_to_le16(bp->pf.port_id);
11867 	req->handle = cpu_to_le16(handle);
11868 	resp = hwrm_req_hold(bp, req);
11869 	rc = hwrm_req_send(bp, req);
11870 	if (!rc) {
11871 		next_handle = le16_to_cpu(resp->next_handle);
11872 		if (next_handle != 0) {
11873 			if (resp->wol_type ==
11874 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
11875 				bp->wol = 1;
11876 				bp->wol_filter_id = resp->wol_filter_id;
11877 			}
11878 		}
11879 	}
11880 	hwrm_req_drop(bp, req);
11881 	return next_handle;
11882 }
11883 
11884 static void bnxt_get_wol_settings(struct bnxt *bp)
11885 {
11886 	u16 handle = 0;
11887 
11888 	bp->wol = 0;
11889 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
11890 		return;
11891 
11892 	do {
11893 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
11894 	} while (handle && handle != 0xffff);
11895 }
11896 
11897 static bool bnxt_eee_config_ok(struct bnxt *bp)
11898 {
11899 	struct ethtool_keee *eee = &bp->eee;
11900 	struct bnxt_link_info *link_info = &bp->link_info;
11901 
11902 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
11903 		return true;
11904 
11905 	if (eee->eee_enabled) {
11906 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
11907 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
11908 
11909 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
11910 
11911 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
11912 			eee->eee_enabled = 0;
11913 			return false;
11914 		}
11915 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
11916 			linkmode_and(eee->advertised, advertising,
11917 				     eee->supported);
11918 			return false;
11919 		}
11920 	}
11921 	return true;
11922 }
11923 
11924 static int bnxt_update_phy_setting(struct bnxt *bp)
11925 {
11926 	int rc;
11927 	bool update_link = false;
11928 	bool update_pause = false;
11929 	bool update_eee = false;
11930 	struct bnxt_link_info *link_info = &bp->link_info;
11931 
11932 	rc = bnxt_update_link(bp, true);
11933 	if (rc) {
11934 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
11935 			   rc);
11936 		return rc;
11937 	}
11938 	if (!BNXT_SINGLE_PF(bp))
11939 		return 0;
11940 
11941 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
11942 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
11943 	    link_info->req_flow_ctrl)
11944 		update_pause = true;
11945 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
11946 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
11947 		update_pause = true;
11948 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
11949 		if (BNXT_AUTO_MODE(link_info->auto_mode))
11950 			update_link = true;
11951 		if (bnxt_force_speed_updated(link_info))
11952 			update_link = true;
11953 		if (link_info->req_duplex != link_info->duplex_setting)
11954 			update_link = true;
11955 	} else {
11956 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
11957 			update_link = true;
11958 		if (bnxt_auto_speed_updated(link_info))
11959 			update_link = true;
11960 	}
11961 
11962 	/* The last close may have shutdown the link, so need to call
11963 	 * PHY_CFG to bring it back up.
11964 	 */
11965 	if (!BNXT_LINK_IS_UP(bp))
11966 		update_link = true;
11967 
11968 	if (!bnxt_eee_config_ok(bp))
11969 		update_eee = true;
11970 
11971 	if (update_link)
11972 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
11973 	else if (update_pause)
11974 		rc = bnxt_hwrm_set_pause(bp);
11975 	if (rc) {
11976 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
11977 			   rc);
11978 		return rc;
11979 	}
11980 
11981 	return rc;
11982 }
11983 
11984 /* Common routine to pre-map certain register block to different GRC window.
11985  * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
11986  * in PF and 3 windows in VF that can be customized to map in different
11987  * register blocks.
11988  */
11989 static void bnxt_preset_reg_win(struct bnxt *bp)
11990 {
11991 	if (BNXT_PF(bp)) {
11992 		/* CAG registers map to GRC window #4 */
11993 		writel(BNXT_CAG_REG_BASE,
11994 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
11995 	}
11996 }
11997 
11998 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
11999 
12000 static int bnxt_reinit_after_abort(struct bnxt *bp)
12001 {
12002 	int rc;
12003 
12004 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12005 		return -EBUSY;
12006 
12007 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
12008 		return -ENODEV;
12009 
12010 	rc = bnxt_fw_init_one(bp);
12011 	if (!rc) {
12012 		bnxt_clear_int_mode(bp);
12013 		rc = bnxt_init_int_mode(bp);
12014 		if (!rc) {
12015 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12016 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12017 		}
12018 	}
12019 	return rc;
12020 }
12021 
12022 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
12023 {
12024 	struct bnxt_ntuple_filter *ntp_fltr;
12025 	struct bnxt_l2_filter *l2_fltr;
12026 
12027 	if (list_empty(&fltr->list))
12028 		return;
12029 
12030 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
12031 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
12032 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
12033 		atomic_inc(&l2_fltr->refcnt);
12034 		ntp_fltr->l2_fltr = l2_fltr;
12035 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
12036 			bnxt_del_ntp_filter(bp, ntp_fltr);
12037 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
12038 				   fltr->sw_id);
12039 		}
12040 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
12041 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
12042 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
12043 			bnxt_del_l2_filter(bp, l2_fltr);
12044 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
12045 				   fltr->sw_id);
12046 		}
12047 	}
12048 }
12049 
12050 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
12051 {
12052 	struct bnxt_filter_base *usr_fltr, *tmp;
12053 
12054 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
12055 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
12056 }
12057 
12058 static int bnxt_set_xps_mapping(struct bnxt *bp)
12059 {
12060 	int numa_node = dev_to_node(&bp->pdev->dev);
12061 	unsigned int q_idx, map_idx, cpu, i;
12062 	const struct cpumask *cpu_mask_ptr;
12063 	int nr_cpus = num_online_cpus();
12064 	cpumask_t *q_map;
12065 	int rc = 0;
12066 
12067 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
12068 	if (!q_map)
12069 		return -ENOMEM;
12070 
12071 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
12072 	 * Each TC has the same number of TX queues. The nth TX queue for each
12073 	 * TC will have the same CPU mask.
12074 	 */
12075 	for (i = 0; i < nr_cpus; i++) {
12076 		map_idx = i % bp->tx_nr_rings_per_tc;
12077 		cpu = cpumask_local_spread(i, numa_node);
12078 		cpu_mask_ptr = get_cpu_mask(cpu);
12079 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
12080 	}
12081 
12082 	/* Register CPU mask for each TX queue except the ones marked for XDP */
12083 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
12084 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
12085 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
12086 		if (rc) {
12087 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
12088 				    q_idx);
12089 			break;
12090 		}
12091 	}
12092 
12093 	kfree(q_map);
12094 
12095 	return rc;
12096 }
12097 
12098 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12099 {
12100 	int rc = 0;
12101 
12102 	bnxt_preset_reg_win(bp);
12103 	netif_carrier_off(bp->dev);
12104 	if (irq_re_init) {
12105 		/* Reserve rings now if none were reserved at driver probe. */
12106 		rc = bnxt_init_dflt_ring_mode(bp);
12107 		if (rc) {
12108 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
12109 			return rc;
12110 		}
12111 	}
12112 	rc = bnxt_reserve_rings(bp, irq_re_init);
12113 	if (rc)
12114 		return rc;
12115 	if ((bp->flags & BNXT_FLAG_RFS) &&
12116 	    !(bp->flags & BNXT_FLAG_USING_MSIX)) {
12117 		/* disable RFS if falling back to INTA */
12118 		bp->dev->hw_features &= ~NETIF_F_NTUPLE;
12119 		bp->flags &= ~BNXT_FLAG_RFS;
12120 	}
12121 
12122 	rc = bnxt_alloc_mem(bp, irq_re_init);
12123 	if (rc) {
12124 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12125 		goto open_err_free_mem;
12126 	}
12127 
12128 	if (irq_re_init) {
12129 		bnxt_init_napi(bp);
12130 		rc = bnxt_request_irq(bp);
12131 		if (rc) {
12132 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
12133 			goto open_err_irq;
12134 		}
12135 	}
12136 
12137 	rc = bnxt_init_nic(bp, irq_re_init);
12138 	if (rc) {
12139 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12140 		goto open_err_irq;
12141 	}
12142 
12143 	bnxt_enable_napi(bp);
12144 	bnxt_debug_dev_init(bp);
12145 
12146 	if (link_re_init) {
12147 		mutex_lock(&bp->link_lock);
12148 		rc = bnxt_update_phy_setting(bp);
12149 		mutex_unlock(&bp->link_lock);
12150 		if (rc) {
12151 			netdev_warn(bp->dev, "failed to update phy settings\n");
12152 			if (BNXT_SINGLE_PF(bp)) {
12153 				bp->link_info.phy_retry = true;
12154 				bp->link_info.phy_retry_expires =
12155 					jiffies + 5 * HZ;
12156 			}
12157 		}
12158 	}
12159 
12160 	if (irq_re_init) {
12161 		udp_tunnel_nic_reset_ntf(bp->dev);
12162 		rc = bnxt_set_xps_mapping(bp);
12163 		if (rc)
12164 			netdev_warn(bp->dev, "failed to set xps mapping\n");
12165 	}
12166 
12167 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
12168 		if (!static_key_enabled(&bnxt_xdp_locking_key))
12169 			static_branch_enable(&bnxt_xdp_locking_key);
12170 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
12171 		static_branch_disable(&bnxt_xdp_locking_key);
12172 	}
12173 	set_bit(BNXT_STATE_OPEN, &bp->state);
12174 	bnxt_enable_int(bp);
12175 	/* Enable TX queues */
12176 	bnxt_tx_enable(bp);
12177 	mod_timer(&bp->timer, jiffies + bp->current_interval);
12178 	/* Poll link status and check for SFP+ module status */
12179 	mutex_lock(&bp->link_lock);
12180 	bnxt_get_port_module_status(bp);
12181 	mutex_unlock(&bp->link_lock);
12182 
12183 	/* VF-reps may need to be re-opened after the PF is re-opened */
12184 	if (BNXT_PF(bp))
12185 		bnxt_vf_reps_open(bp);
12186 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
12187 		WRITE_ONCE(bp->ptp_cfg->tx_avail, BNXT_MAX_TX_TS);
12188 	bnxt_ptp_init_rtc(bp, true);
12189 	bnxt_ptp_cfg_tstamp_filters(bp);
12190 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12191 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
12192 	bnxt_cfg_usr_fltrs(bp);
12193 	return 0;
12194 
12195 open_err_irq:
12196 	bnxt_del_napi(bp);
12197 
12198 open_err_free_mem:
12199 	bnxt_free_skbs(bp);
12200 	bnxt_free_irq(bp);
12201 	bnxt_free_mem(bp, true);
12202 	return rc;
12203 }
12204 
12205 /* rtnl_lock held */
12206 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12207 {
12208 	int rc = 0;
12209 
12210 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12211 		rc = -EIO;
12212 	if (!rc)
12213 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12214 	if (rc) {
12215 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12216 		dev_close(bp->dev);
12217 	}
12218 	return rc;
12219 }
12220 
12221 /* rtnl_lock held, open the NIC half way by allocating all resources, but
12222  * NAPI, IRQ, and TX are not enabled.  This is mainly used for offline
12223  * self tests.
12224  */
12225 int bnxt_half_open_nic(struct bnxt *bp)
12226 {
12227 	int rc = 0;
12228 
12229 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12230 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
12231 		rc = -ENODEV;
12232 		goto half_open_err;
12233 	}
12234 
12235 	rc = bnxt_alloc_mem(bp, true);
12236 	if (rc) {
12237 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12238 		goto half_open_err;
12239 	}
12240 	bnxt_init_napi(bp);
12241 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12242 	rc = bnxt_init_nic(bp, true);
12243 	if (rc) {
12244 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12245 		bnxt_del_napi(bp);
12246 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12247 		goto half_open_err;
12248 	}
12249 	return 0;
12250 
12251 half_open_err:
12252 	bnxt_free_skbs(bp);
12253 	bnxt_free_mem(bp, true);
12254 	dev_close(bp->dev);
12255 	return rc;
12256 }
12257 
12258 /* rtnl_lock held, this call can only be made after a previous successful
12259  * call to bnxt_half_open_nic().
12260  */
12261 void bnxt_half_close_nic(struct bnxt *bp)
12262 {
12263 	bnxt_hwrm_resource_free(bp, false, true);
12264 	bnxt_del_napi(bp);
12265 	bnxt_free_skbs(bp);
12266 	bnxt_free_mem(bp, true);
12267 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12268 }
12269 
12270 void bnxt_reenable_sriov(struct bnxt *bp)
12271 {
12272 	if (BNXT_PF(bp)) {
12273 		struct bnxt_pf_info *pf = &bp->pf;
12274 		int n = pf->active_vfs;
12275 
12276 		if (n)
12277 			bnxt_cfg_hw_sriov(bp, &n, true);
12278 	}
12279 }
12280 
12281 static int bnxt_open(struct net_device *dev)
12282 {
12283 	struct bnxt *bp = netdev_priv(dev);
12284 	int rc;
12285 
12286 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12287 		rc = bnxt_reinit_after_abort(bp);
12288 		if (rc) {
12289 			if (rc == -EBUSY)
12290 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
12291 			else
12292 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
12293 			return -ENODEV;
12294 		}
12295 	}
12296 
12297 	rc = bnxt_hwrm_if_change(bp, true);
12298 	if (rc)
12299 		return rc;
12300 
12301 	rc = __bnxt_open_nic(bp, true, true);
12302 	if (rc) {
12303 		bnxt_hwrm_if_change(bp, false);
12304 	} else {
12305 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
12306 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12307 				bnxt_queue_sp_work(bp,
12308 						   BNXT_RESTART_ULP_SP_EVENT);
12309 		}
12310 	}
12311 
12312 	return rc;
12313 }
12314 
12315 static bool bnxt_drv_busy(struct bnxt *bp)
12316 {
12317 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
12318 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
12319 }
12320 
12321 static void bnxt_get_ring_stats(struct bnxt *bp,
12322 				struct rtnl_link_stats64 *stats);
12323 
12324 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
12325 			     bool link_re_init)
12326 {
12327 	/* Close the VF-reps before closing PF */
12328 	if (BNXT_PF(bp))
12329 		bnxt_vf_reps_close(bp);
12330 
12331 	/* Change device state to avoid TX queue wake up's */
12332 	bnxt_tx_disable(bp);
12333 
12334 	clear_bit(BNXT_STATE_OPEN, &bp->state);
12335 	smp_mb__after_atomic();
12336 	while (bnxt_drv_busy(bp))
12337 		msleep(20);
12338 
12339 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12340 		bnxt_clear_rss_ctxs(bp, false);
12341 	/* Flush rings and disable interrupts */
12342 	bnxt_shutdown_nic(bp, irq_re_init);
12343 
12344 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
12345 
12346 	bnxt_debug_dev_exit(bp);
12347 	bnxt_disable_napi(bp);
12348 	del_timer_sync(&bp->timer);
12349 	bnxt_free_skbs(bp);
12350 
12351 	/* Save ring stats before shutdown */
12352 	if (bp->bnapi && irq_re_init) {
12353 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
12354 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
12355 	}
12356 	if (irq_re_init) {
12357 		bnxt_free_irq(bp);
12358 		bnxt_del_napi(bp);
12359 	}
12360 	bnxt_free_mem(bp, irq_re_init);
12361 }
12362 
12363 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12364 {
12365 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12366 		/* If we get here, it means firmware reset is in progress
12367 		 * while we are trying to close.  We can safely proceed with
12368 		 * the close because we are holding rtnl_lock().  Some firmware
12369 		 * messages may fail as we proceed to close.  We set the
12370 		 * ABORT_ERR flag here so that the FW reset thread will later
12371 		 * abort when it gets the rtnl_lock() and sees the flag.
12372 		 */
12373 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
12374 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12375 	}
12376 
12377 #ifdef CONFIG_BNXT_SRIOV
12378 	if (bp->sriov_cfg) {
12379 		int rc;
12380 
12381 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
12382 						      !bp->sriov_cfg,
12383 						      BNXT_SRIOV_CFG_WAIT_TMO);
12384 		if (!rc)
12385 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
12386 		else if (rc < 0)
12387 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
12388 	}
12389 #endif
12390 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
12391 }
12392 
12393 static int bnxt_close(struct net_device *dev)
12394 {
12395 	struct bnxt *bp = netdev_priv(dev);
12396 
12397 	bnxt_close_nic(bp, true, true);
12398 	bnxt_hwrm_shutdown_link(bp);
12399 	bnxt_hwrm_if_change(bp, false);
12400 	return 0;
12401 }
12402 
12403 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
12404 				   u16 *val)
12405 {
12406 	struct hwrm_port_phy_mdio_read_output *resp;
12407 	struct hwrm_port_phy_mdio_read_input *req;
12408 	int rc;
12409 
12410 	if (bp->hwrm_spec_code < 0x10a00)
12411 		return -EOPNOTSUPP;
12412 
12413 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
12414 	if (rc)
12415 		return rc;
12416 
12417 	req->port_id = cpu_to_le16(bp->pf.port_id);
12418 	req->phy_addr = phy_addr;
12419 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12420 	if (mdio_phy_id_is_c45(phy_addr)) {
12421 		req->cl45_mdio = 1;
12422 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12423 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12424 		req->reg_addr = cpu_to_le16(reg);
12425 	}
12426 
12427 	resp = hwrm_req_hold(bp, req);
12428 	rc = hwrm_req_send(bp, req);
12429 	if (!rc)
12430 		*val = le16_to_cpu(resp->reg_data);
12431 	hwrm_req_drop(bp, req);
12432 	return rc;
12433 }
12434 
12435 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
12436 				    u16 val)
12437 {
12438 	struct hwrm_port_phy_mdio_write_input *req;
12439 	int rc;
12440 
12441 	if (bp->hwrm_spec_code < 0x10a00)
12442 		return -EOPNOTSUPP;
12443 
12444 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
12445 	if (rc)
12446 		return rc;
12447 
12448 	req->port_id = cpu_to_le16(bp->pf.port_id);
12449 	req->phy_addr = phy_addr;
12450 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12451 	if (mdio_phy_id_is_c45(phy_addr)) {
12452 		req->cl45_mdio = 1;
12453 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12454 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12455 		req->reg_addr = cpu_to_le16(reg);
12456 	}
12457 	req->reg_data = cpu_to_le16(val);
12458 
12459 	return hwrm_req_send(bp, req);
12460 }
12461 
12462 /* rtnl_lock held */
12463 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12464 {
12465 	struct mii_ioctl_data *mdio = if_mii(ifr);
12466 	struct bnxt *bp = netdev_priv(dev);
12467 	int rc;
12468 
12469 	switch (cmd) {
12470 	case SIOCGMIIPHY:
12471 		mdio->phy_id = bp->link_info.phy_addr;
12472 
12473 		fallthrough;
12474 	case SIOCGMIIREG: {
12475 		u16 mii_regval = 0;
12476 
12477 		if (!netif_running(dev))
12478 			return -EAGAIN;
12479 
12480 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
12481 					     &mii_regval);
12482 		mdio->val_out = mii_regval;
12483 		return rc;
12484 	}
12485 
12486 	case SIOCSMIIREG:
12487 		if (!netif_running(dev))
12488 			return -EAGAIN;
12489 
12490 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
12491 						mdio->val_in);
12492 
12493 	case SIOCSHWTSTAMP:
12494 		return bnxt_hwtstamp_set(dev, ifr);
12495 
12496 	case SIOCGHWTSTAMP:
12497 		return bnxt_hwtstamp_get(dev, ifr);
12498 
12499 	default:
12500 		/* do nothing */
12501 		break;
12502 	}
12503 	return -EOPNOTSUPP;
12504 }
12505 
12506 static void bnxt_get_ring_stats(struct bnxt *bp,
12507 				struct rtnl_link_stats64 *stats)
12508 {
12509 	int i;
12510 
12511 	for (i = 0; i < bp->cp_nr_rings; i++) {
12512 		struct bnxt_napi *bnapi = bp->bnapi[i];
12513 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
12514 		u64 *sw = cpr->stats.sw_stats;
12515 
12516 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
12517 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12518 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
12519 
12520 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
12521 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
12522 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
12523 
12524 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
12525 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
12526 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
12527 
12528 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
12529 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
12530 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
12531 
12532 		stats->rx_missed_errors +=
12533 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
12534 
12535 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12536 
12537 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
12538 
12539 		stats->rx_dropped +=
12540 			cpr->sw_stats->rx.rx_netpoll_discards +
12541 			cpr->sw_stats->rx.rx_oom_discards;
12542 	}
12543 }
12544 
12545 static void bnxt_add_prev_stats(struct bnxt *bp,
12546 				struct rtnl_link_stats64 *stats)
12547 {
12548 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
12549 
12550 	stats->rx_packets += prev_stats->rx_packets;
12551 	stats->tx_packets += prev_stats->tx_packets;
12552 	stats->rx_bytes += prev_stats->rx_bytes;
12553 	stats->tx_bytes += prev_stats->tx_bytes;
12554 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
12555 	stats->multicast += prev_stats->multicast;
12556 	stats->rx_dropped += prev_stats->rx_dropped;
12557 	stats->tx_dropped += prev_stats->tx_dropped;
12558 }
12559 
12560 static void
12561 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
12562 {
12563 	struct bnxt *bp = netdev_priv(dev);
12564 
12565 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
12566 	/* Make sure bnxt_close_nic() sees that we are reading stats before
12567 	 * we check the BNXT_STATE_OPEN flag.
12568 	 */
12569 	smp_mb__after_atomic();
12570 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12571 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12572 		*stats = bp->net_stats_prev;
12573 		return;
12574 	}
12575 
12576 	bnxt_get_ring_stats(bp, stats);
12577 	bnxt_add_prev_stats(bp, stats);
12578 
12579 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
12580 		u64 *rx = bp->port_stats.sw_stats;
12581 		u64 *tx = bp->port_stats.sw_stats +
12582 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
12583 
12584 		stats->rx_crc_errors =
12585 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
12586 		stats->rx_frame_errors =
12587 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
12588 		stats->rx_length_errors =
12589 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
12590 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
12591 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
12592 		stats->rx_errors =
12593 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
12594 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
12595 		stats->collisions =
12596 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
12597 		stats->tx_fifo_errors =
12598 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
12599 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
12600 	}
12601 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12602 }
12603 
12604 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
12605 					struct bnxt_total_ring_err_stats *stats,
12606 					struct bnxt_cp_ring_info *cpr)
12607 {
12608 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
12609 	u64 *hw_stats = cpr->stats.sw_stats;
12610 
12611 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
12612 	stats->rx_total_resets += sw_stats->rx.rx_resets;
12613 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
12614 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
12615 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
12616 	stats->rx_total_ring_discards +=
12617 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
12618 	stats->tx_total_resets += sw_stats->tx.tx_resets;
12619 	stats->tx_total_ring_discards +=
12620 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
12621 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
12622 }
12623 
12624 void bnxt_get_ring_err_stats(struct bnxt *bp,
12625 			     struct bnxt_total_ring_err_stats *stats)
12626 {
12627 	int i;
12628 
12629 	for (i = 0; i < bp->cp_nr_rings; i++)
12630 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
12631 }
12632 
12633 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
12634 {
12635 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12636 	struct net_device *dev = bp->dev;
12637 	struct netdev_hw_addr *ha;
12638 	u8 *haddr;
12639 	int mc_count = 0;
12640 	bool update = false;
12641 	int off = 0;
12642 
12643 	netdev_for_each_mc_addr(ha, dev) {
12644 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
12645 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12646 			vnic->mc_list_count = 0;
12647 			return false;
12648 		}
12649 		haddr = ha->addr;
12650 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
12651 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
12652 			update = true;
12653 		}
12654 		off += ETH_ALEN;
12655 		mc_count++;
12656 	}
12657 	if (mc_count)
12658 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
12659 
12660 	if (mc_count != vnic->mc_list_count) {
12661 		vnic->mc_list_count = mc_count;
12662 		update = true;
12663 	}
12664 	return update;
12665 }
12666 
12667 static bool bnxt_uc_list_updated(struct bnxt *bp)
12668 {
12669 	struct net_device *dev = bp->dev;
12670 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12671 	struct netdev_hw_addr *ha;
12672 	int off = 0;
12673 
12674 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
12675 		return true;
12676 
12677 	netdev_for_each_uc_addr(ha, dev) {
12678 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
12679 			return true;
12680 
12681 		off += ETH_ALEN;
12682 	}
12683 	return false;
12684 }
12685 
12686 static void bnxt_set_rx_mode(struct net_device *dev)
12687 {
12688 	struct bnxt *bp = netdev_priv(dev);
12689 	struct bnxt_vnic_info *vnic;
12690 	bool mc_update = false;
12691 	bool uc_update;
12692 	u32 mask;
12693 
12694 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
12695 		return;
12696 
12697 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12698 	mask = vnic->rx_mask;
12699 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
12700 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
12701 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
12702 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
12703 
12704 	if (dev->flags & IFF_PROMISC)
12705 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12706 
12707 	uc_update = bnxt_uc_list_updated(bp);
12708 
12709 	if (dev->flags & IFF_BROADCAST)
12710 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
12711 	if (dev->flags & IFF_ALLMULTI) {
12712 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12713 		vnic->mc_list_count = 0;
12714 	} else if (dev->flags & IFF_MULTICAST) {
12715 		mc_update = bnxt_mc_list_updated(bp, &mask);
12716 	}
12717 
12718 	if (mask != vnic->rx_mask || uc_update || mc_update) {
12719 		vnic->rx_mask = mask;
12720 
12721 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
12722 	}
12723 }
12724 
12725 static int bnxt_cfg_rx_mode(struct bnxt *bp)
12726 {
12727 	struct net_device *dev = bp->dev;
12728 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12729 	struct netdev_hw_addr *ha;
12730 	int i, off = 0, rc;
12731 	bool uc_update;
12732 
12733 	netif_addr_lock_bh(dev);
12734 	uc_update = bnxt_uc_list_updated(bp);
12735 	netif_addr_unlock_bh(dev);
12736 
12737 	if (!uc_update)
12738 		goto skip_uc;
12739 
12740 	for (i = 1; i < vnic->uc_filter_count; i++) {
12741 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
12742 
12743 		bnxt_hwrm_l2_filter_free(bp, fltr);
12744 		bnxt_del_l2_filter(bp, fltr);
12745 	}
12746 
12747 	vnic->uc_filter_count = 1;
12748 
12749 	netif_addr_lock_bh(dev);
12750 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
12751 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12752 	} else {
12753 		netdev_for_each_uc_addr(ha, dev) {
12754 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
12755 			off += ETH_ALEN;
12756 			vnic->uc_filter_count++;
12757 		}
12758 	}
12759 	netif_addr_unlock_bh(dev);
12760 
12761 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
12762 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
12763 		if (rc) {
12764 			if (BNXT_VF(bp) && rc == -ENODEV) {
12765 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
12766 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
12767 				else
12768 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
12769 				rc = 0;
12770 			} else {
12771 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
12772 			}
12773 			vnic->uc_filter_count = i;
12774 			return rc;
12775 		}
12776 	}
12777 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
12778 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
12779 
12780 skip_uc:
12781 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
12782 	    !bnxt_promisc_ok(bp))
12783 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12784 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
12785 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
12786 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
12787 			    rc);
12788 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
12789 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12790 		vnic->mc_list_count = 0;
12791 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
12792 	}
12793 	if (rc)
12794 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
12795 			   rc);
12796 
12797 	return rc;
12798 }
12799 
12800 static bool bnxt_can_reserve_rings(struct bnxt *bp)
12801 {
12802 #ifdef CONFIG_BNXT_SRIOV
12803 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
12804 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12805 
12806 		/* No minimum rings were provisioned by the PF.  Don't
12807 		 * reserve rings by default when device is down.
12808 		 */
12809 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
12810 			return true;
12811 
12812 		if (!netif_running(bp->dev))
12813 			return false;
12814 	}
12815 #endif
12816 	return true;
12817 }
12818 
12819 /* If the chip and firmware supports RFS */
12820 static bool bnxt_rfs_supported(struct bnxt *bp)
12821 {
12822 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
12823 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
12824 			return true;
12825 		return false;
12826 	}
12827 	/* 212 firmware is broken for aRFS */
12828 	if (BNXT_FW_MAJ(bp) == 212)
12829 		return false;
12830 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
12831 		return true;
12832 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
12833 		return true;
12834 	return false;
12835 }
12836 
12837 /* If runtime conditions support RFS */
12838 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
12839 {
12840 	struct bnxt_hw_rings hwr = {0};
12841 	int max_vnics, max_rss_ctxs;
12842 
12843 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
12844 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
12845 		return bnxt_rfs_supported(bp);
12846 
12847 	if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
12848 		return false;
12849 
12850 	hwr.grp = bp->rx_nr_rings;
12851 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
12852 	if (new_rss_ctx)
12853 		hwr.vnic++;
12854 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
12855 	max_vnics = bnxt_get_max_func_vnics(bp);
12856 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
12857 
12858 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
12859 		if (bp->rx_nr_rings > 1)
12860 			netdev_warn(bp->dev,
12861 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
12862 				    min(max_rss_ctxs - 1, max_vnics - 1));
12863 		return false;
12864 	}
12865 
12866 	if (!BNXT_NEW_RM(bp))
12867 		return true;
12868 
12869 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
12870 	 * issue that will mess up the default VNIC if we reduce the
12871 	 * reservations.
12872 	 */
12873 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
12874 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
12875 		return true;
12876 
12877 	bnxt_hwrm_reserve_rings(bp, &hwr);
12878 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
12879 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
12880 		return true;
12881 
12882 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
12883 	hwr.vnic = 1;
12884 	hwr.rss_ctx = 0;
12885 	bnxt_hwrm_reserve_rings(bp, &hwr);
12886 	return false;
12887 }
12888 
12889 static netdev_features_t bnxt_fix_features(struct net_device *dev,
12890 					   netdev_features_t features)
12891 {
12892 	struct bnxt *bp = netdev_priv(dev);
12893 	netdev_features_t vlan_features;
12894 
12895 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
12896 		features &= ~NETIF_F_NTUPLE;
12897 
12898 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
12899 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
12900 
12901 	if (!(features & NETIF_F_GRO))
12902 		features &= ~NETIF_F_GRO_HW;
12903 
12904 	if (features & NETIF_F_GRO_HW)
12905 		features &= ~NETIF_F_LRO;
12906 
12907 	/* Both CTAG and STAG VLAN accelaration on the RX side have to be
12908 	 * turned on or off together.
12909 	 */
12910 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
12911 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
12912 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
12913 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
12914 		else if (vlan_features)
12915 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
12916 	}
12917 #ifdef CONFIG_BNXT_SRIOV
12918 	if (BNXT_VF(bp) && bp->vf.vlan)
12919 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
12920 #endif
12921 	return features;
12922 }
12923 
12924 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
12925 				bool link_re_init, u32 flags, bool update_tpa)
12926 {
12927 	bnxt_close_nic(bp, irq_re_init, link_re_init);
12928 	bp->flags = flags;
12929 	if (update_tpa)
12930 		bnxt_set_ring_params(bp);
12931 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
12932 }
12933 
12934 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
12935 {
12936 	bool update_tpa = false, update_ntuple = false;
12937 	struct bnxt *bp = netdev_priv(dev);
12938 	u32 flags = bp->flags;
12939 	u32 changes;
12940 	int rc = 0;
12941 	bool re_init = false;
12942 
12943 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
12944 	if (features & NETIF_F_GRO_HW)
12945 		flags |= BNXT_FLAG_GRO;
12946 	else if (features & NETIF_F_LRO)
12947 		flags |= BNXT_FLAG_LRO;
12948 
12949 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
12950 		flags &= ~BNXT_FLAG_TPA;
12951 
12952 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
12953 		flags |= BNXT_FLAG_STRIP_VLAN;
12954 
12955 	if (features & NETIF_F_NTUPLE)
12956 		flags |= BNXT_FLAG_RFS;
12957 	else
12958 		bnxt_clear_usr_fltrs(bp, true);
12959 
12960 	changes = flags ^ bp->flags;
12961 	if (changes & BNXT_FLAG_TPA) {
12962 		update_tpa = true;
12963 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
12964 		    (flags & BNXT_FLAG_TPA) == 0 ||
12965 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
12966 			re_init = true;
12967 	}
12968 
12969 	if (changes & ~BNXT_FLAG_TPA)
12970 		re_init = true;
12971 
12972 	if (changes & BNXT_FLAG_RFS)
12973 		update_ntuple = true;
12974 
12975 	if (flags != bp->flags) {
12976 		u32 old_flags = bp->flags;
12977 
12978 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12979 			bp->flags = flags;
12980 			if (update_tpa)
12981 				bnxt_set_ring_params(bp);
12982 			return rc;
12983 		}
12984 
12985 		if (update_ntuple)
12986 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
12987 
12988 		if (re_init)
12989 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
12990 
12991 		if (update_tpa) {
12992 			bp->flags = flags;
12993 			rc = bnxt_set_tpa(bp,
12994 					  (flags & BNXT_FLAG_TPA) ?
12995 					  true : false);
12996 			if (rc)
12997 				bp->flags = old_flags;
12998 		}
12999 	}
13000 	return rc;
13001 }
13002 
13003 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
13004 			      u8 **nextp)
13005 {
13006 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
13007 	struct hop_jumbo_hdr *jhdr;
13008 	int hdr_count = 0;
13009 	u8 *nexthdr;
13010 	int start;
13011 
13012 	/* Check that there are at most 2 IPv6 extension headers, no
13013 	 * fragment header, and each is <= 64 bytes.
13014 	 */
13015 	start = nw_off + sizeof(*ip6h);
13016 	nexthdr = &ip6h->nexthdr;
13017 	while (ipv6_ext_hdr(*nexthdr)) {
13018 		struct ipv6_opt_hdr *hp;
13019 		int hdrlen;
13020 
13021 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
13022 		    *nexthdr == NEXTHDR_FRAGMENT)
13023 			return false;
13024 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
13025 					  skb_headlen(skb), NULL);
13026 		if (!hp)
13027 			return false;
13028 		if (*nexthdr == NEXTHDR_AUTH)
13029 			hdrlen = ipv6_authlen(hp);
13030 		else
13031 			hdrlen = ipv6_optlen(hp);
13032 
13033 		if (hdrlen > 64)
13034 			return false;
13035 
13036 		/* The ext header may be a hop-by-hop header inserted for
13037 		 * big TCP purposes. This will be removed before sending
13038 		 * from NIC, so do not count it.
13039 		 */
13040 		if (*nexthdr == NEXTHDR_HOP) {
13041 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
13042 				goto increment_hdr;
13043 
13044 			jhdr = (struct hop_jumbo_hdr *)hp;
13045 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
13046 			    jhdr->nexthdr != IPPROTO_TCP)
13047 				goto increment_hdr;
13048 
13049 			goto next_hdr;
13050 		}
13051 increment_hdr:
13052 		hdr_count++;
13053 next_hdr:
13054 		nexthdr = &hp->nexthdr;
13055 		start += hdrlen;
13056 	}
13057 	if (nextp) {
13058 		/* Caller will check inner protocol */
13059 		if (skb->encapsulation) {
13060 			*nextp = nexthdr;
13061 			return true;
13062 		}
13063 		*nextp = NULL;
13064 	}
13065 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
13066 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
13067 }
13068 
13069 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
13070 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
13071 {
13072 	struct udphdr *uh = udp_hdr(skb);
13073 	__be16 udp_port = uh->dest;
13074 
13075 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
13076 	    udp_port != bp->vxlan_gpe_port)
13077 		return false;
13078 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
13079 		struct ethhdr *eh = inner_eth_hdr(skb);
13080 
13081 		switch (eh->h_proto) {
13082 		case htons(ETH_P_IP):
13083 			return true;
13084 		case htons(ETH_P_IPV6):
13085 			return bnxt_exthdr_check(bp, skb,
13086 						 skb_inner_network_offset(skb),
13087 						 NULL);
13088 		}
13089 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
13090 		return true;
13091 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
13092 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13093 					 NULL);
13094 	}
13095 	return false;
13096 }
13097 
13098 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
13099 {
13100 	switch (l4_proto) {
13101 	case IPPROTO_UDP:
13102 		return bnxt_udp_tunl_check(bp, skb);
13103 	case IPPROTO_IPIP:
13104 		return true;
13105 	case IPPROTO_GRE: {
13106 		switch (skb->inner_protocol) {
13107 		default:
13108 			return false;
13109 		case htons(ETH_P_IP):
13110 			return true;
13111 		case htons(ETH_P_IPV6):
13112 			fallthrough;
13113 		}
13114 	}
13115 	case IPPROTO_IPV6:
13116 		/* Check ext headers of inner ipv6 */
13117 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13118 					 NULL);
13119 	}
13120 	return false;
13121 }
13122 
13123 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
13124 					     struct net_device *dev,
13125 					     netdev_features_t features)
13126 {
13127 	struct bnxt *bp = netdev_priv(dev);
13128 	u8 *l4_proto;
13129 
13130 	features = vlan_features_check(skb, features);
13131 	switch (vlan_get_protocol(skb)) {
13132 	case htons(ETH_P_IP):
13133 		if (!skb->encapsulation)
13134 			return features;
13135 		l4_proto = &ip_hdr(skb)->protocol;
13136 		if (bnxt_tunl_check(bp, skb, *l4_proto))
13137 			return features;
13138 		break;
13139 	case htons(ETH_P_IPV6):
13140 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
13141 				       &l4_proto))
13142 			break;
13143 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
13144 			return features;
13145 		break;
13146 	}
13147 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13148 }
13149 
13150 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
13151 			 u32 *reg_buf)
13152 {
13153 	struct hwrm_dbg_read_direct_output *resp;
13154 	struct hwrm_dbg_read_direct_input *req;
13155 	__le32 *dbg_reg_buf;
13156 	dma_addr_t mapping;
13157 	int rc, i;
13158 
13159 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
13160 	if (rc)
13161 		return rc;
13162 
13163 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
13164 					 &mapping);
13165 	if (!dbg_reg_buf) {
13166 		rc = -ENOMEM;
13167 		goto dbg_rd_reg_exit;
13168 	}
13169 
13170 	req->host_dest_addr = cpu_to_le64(mapping);
13171 
13172 	resp = hwrm_req_hold(bp, req);
13173 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
13174 	req->read_len32 = cpu_to_le32(num_words);
13175 
13176 	rc = hwrm_req_send(bp, req);
13177 	if (rc || resp->error_code) {
13178 		rc = -EIO;
13179 		goto dbg_rd_reg_exit;
13180 	}
13181 	for (i = 0; i < num_words; i++)
13182 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
13183 
13184 dbg_rd_reg_exit:
13185 	hwrm_req_drop(bp, req);
13186 	return rc;
13187 }
13188 
13189 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
13190 				       u32 ring_id, u32 *prod, u32 *cons)
13191 {
13192 	struct hwrm_dbg_ring_info_get_output *resp;
13193 	struct hwrm_dbg_ring_info_get_input *req;
13194 	int rc;
13195 
13196 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
13197 	if (rc)
13198 		return rc;
13199 
13200 	req->ring_type = ring_type;
13201 	req->fw_ring_id = cpu_to_le32(ring_id);
13202 	resp = hwrm_req_hold(bp, req);
13203 	rc = hwrm_req_send(bp, req);
13204 	if (!rc) {
13205 		*prod = le32_to_cpu(resp->producer_index);
13206 		*cons = le32_to_cpu(resp->consumer_index);
13207 	}
13208 	hwrm_req_drop(bp, req);
13209 	return rc;
13210 }
13211 
13212 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13213 {
13214 	struct bnxt_tx_ring_info *txr;
13215 	int i = bnapi->index, j;
13216 
13217 	bnxt_for_each_napi_tx(j, bnapi, txr)
13218 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13219 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13220 			    txr->tx_cons);
13221 }
13222 
13223 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
13224 {
13225 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
13226 	int i = bnapi->index;
13227 
13228 	if (!rxr)
13229 		return;
13230 
13231 	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",
13232 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
13233 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
13234 		    rxr->rx_sw_agg_prod);
13235 }
13236 
13237 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
13238 {
13239 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13240 	int i = bnapi->index;
13241 
13242 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
13243 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
13244 }
13245 
13246 static void bnxt_dbg_dump_states(struct bnxt *bp)
13247 {
13248 	int i;
13249 	struct bnxt_napi *bnapi;
13250 
13251 	for (i = 0; i < bp->cp_nr_rings; i++) {
13252 		bnapi = bp->bnapi[i];
13253 		if (netif_msg_drv(bp)) {
13254 			bnxt_dump_tx_sw_state(bnapi);
13255 			bnxt_dump_rx_sw_state(bnapi);
13256 			bnxt_dump_cp_sw_state(bnapi);
13257 		}
13258 	}
13259 }
13260 
13261 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
13262 {
13263 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
13264 	struct hwrm_ring_reset_input *req;
13265 	struct bnxt_napi *bnapi = rxr->bnapi;
13266 	struct bnxt_cp_ring_info *cpr;
13267 	u16 cp_ring_id;
13268 	int rc;
13269 
13270 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
13271 	if (rc)
13272 		return rc;
13273 
13274 	cpr = &bnapi->cp_ring;
13275 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
13276 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
13277 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
13278 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
13279 	return hwrm_req_send_silent(bp, req);
13280 }
13281 
13282 static void bnxt_reset_task(struct bnxt *bp, bool silent)
13283 {
13284 	if (!silent)
13285 		bnxt_dbg_dump_states(bp);
13286 	if (netif_running(bp->dev)) {
13287 		bnxt_close_nic(bp, !silent, false);
13288 		bnxt_open_nic(bp, !silent, false);
13289 	}
13290 }
13291 
13292 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
13293 {
13294 	struct bnxt *bp = netdev_priv(dev);
13295 
13296 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
13297 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
13298 }
13299 
13300 static void bnxt_fw_health_check(struct bnxt *bp)
13301 {
13302 	struct bnxt_fw_health *fw_health = bp->fw_health;
13303 	struct pci_dev *pdev = bp->pdev;
13304 	u32 val;
13305 
13306 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13307 		return;
13308 
13309 	/* Make sure it is enabled before checking the tmr_counter. */
13310 	smp_rmb();
13311 	if (fw_health->tmr_counter) {
13312 		fw_health->tmr_counter--;
13313 		return;
13314 	}
13315 
13316 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13317 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
13318 		fw_health->arrests++;
13319 		goto fw_reset;
13320 	}
13321 
13322 	fw_health->last_fw_heartbeat = val;
13323 
13324 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13325 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
13326 		fw_health->discoveries++;
13327 		goto fw_reset;
13328 	}
13329 
13330 	fw_health->tmr_counter = fw_health->tmr_multiplier;
13331 	return;
13332 
13333 fw_reset:
13334 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
13335 }
13336 
13337 static void bnxt_timer(struct timer_list *t)
13338 {
13339 	struct bnxt *bp = from_timer(bp, t, timer);
13340 	struct net_device *dev = bp->dev;
13341 
13342 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
13343 		return;
13344 
13345 	if (atomic_read(&bp->intr_sem) != 0)
13346 		goto bnxt_restart_timer;
13347 
13348 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
13349 		bnxt_fw_health_check(bp);
13350 
13351 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
13352 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
13353 
13354 	if (bnxt_tc_flower_enabled(bp))
13355 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
13356 
13357 #ifdef CONFIG_RFS_ACCEL
13358 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
13359 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
13360 #endif /*CONFIG_RFS_ACCEL*/
13361 
13362 	if (bp->link_info.phy_retry) {
13363 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
13364 			bp->link_info.phy_retry = false;
13365 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
13366 		} else {
13367 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
13368 		}
13369 	}
13370 
13371 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13372 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13373 
13374 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
13375 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
13376 
13377 bnxt_restart_timer:
13378 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13379 }
13380 
13381 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
13382 {
13383 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
13384 	 * set.  If the device is being closed, bnxt_close() may be holding
13385 	 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear.  So we
13386 	 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
13387 	 */
13388 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13389 	rtnl_lock();
13390 }
13391 
13392 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
13393 {
13394 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13395 	rtnl_unlock();
13396 }
13397 
13398 /* Only called from bnxt_sp_task() */
13399 static void bnxt_reset(struct bnxt *bp, bool silent)
13400 {
13401 	bnxt_rtnl_lock_sp(bp);
13402 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
13403 		bnxt_reset_task(bp, silent);
13404 	bnxt_rtnl_unlock_sp(bp);
13405 }
13406 
13407 /* Only called from bnxt_sp_task() */
13408 static void bnxt_rx_ring_reset(struct bnxt *bp)
13409 {
13410 	int i;
13411 
13412 	bnxt_rtnl_lock_sp(bp);
13413 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13414 		bnxt_rtnl_unlock_sp(bp);
13415 		return;
13416 	}
13417 	/* Disable and flush TPA before resetting the RX ring */
13418 	if (bp->flags & BNXT_FLAG_TPA)
13419 		bnxt_set_tpa(bp, false);
13420 	for (i = 0; i < bp->rx_nr_rings; i++) {
13421 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
13422 		struct bnxt_cp_ring_info *cpr;
13423 		int rc;
13424 
13425 		if (!rxr->bnapi->in_reset)
13426 			continue;
13427 
13428 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
13429 		if (rc) {
13430 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
13431 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
13432 			else
13433 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
13434 					    rc);
13435 			bnxt_reset_task(bp, true);
13436 			break;
13437 		}
13438 		bnxt_free_one_rx_ring_skbs(bp, i);
13439 		rxr->rx_prod = 0;
13440 		rxr->rx_agg_prod = 0;
13441 		rxr->rx_sw_agg_prod = 0;
13442 		rxr->rx_next_cons = 0;
13443 		rxr->bnapi->in_reset = false;
13444 		bnxt_alloc_one_rx_ring(bp, i);
13445 		cpr = &rxr->bnapi->cp_ring;
13446 		cpr->sw_stats->rx.rx_resets++;
13447 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
13448 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
13449 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
13450 	}
13451 	if (bp->flags & BNXT_FLAG_TPA)
13452 		bnxt_set_tpa(bp, true);
13453 	bnxt_rtnl_unlock_sp(bp);
13454 }
13455 
13456 static void bnxt_fw_fatal_close(struct bnxt *bp)
13457 {
13458 	bnxt_tx_disable(bp);
13459 	bnxt_disable_napi(bp);
13460 	bnxt_disable_int_sync(bp);
13461 	bnxt_free_irq(bp);
13462 	bnxt_clear_int_mode(bp);
13463 	pci_disable_device(bp->pdev);
13464 }
13465 
13466 static void bnxt_fw_reset_close(struct bnxt *bp)
13467 {
13468 	/* When firmware is in fatal state, quiesce device and disable
13469 	 * bus master to prevent any potential bad DMAs before freeing
13470 	 * kernel memory.
13471 	 */
13472 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
13473 		u16 val = 0;
13474 
13475 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
13476 		if (val == 0xffff)
13477 			bp->fw_reset_min_dsecs = 0;
13478 		bnxt_fw_fatal_close(bp);
13479 	}
13480 	__bnxt_close_nic(bp, true, false);
13481 	bnxt_vf_reps_free(bp);
13482 	bnxt_clear_int_mode(bp);
13483 	bnxt_hwrm_func_drv_unrgtr(bp);
13484 	if (pci_is_enabled(bp->pdev))
13485 		pci_disable_device(bp->pdev);
13486 	bnxt_free_ctx_mem(bp);
13487 }
13488 
13489 static bool is_bnxt_fw_ok(struct bnxt *bp)
13490 {
13491 	struct bnxt_fw_health *fw_health = bp->fw_health;
13492 	bool no_heartbeat = false, has_reset = false;
13493 	u32 val;
13494 
13495 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13496 	if (val == fw_health->last_fw_heartbeat)
13497 		no_heartbeat = true;
13498 
13499 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13500 	if (val != fw_health->last_fw_reset_cnt)
13501 		has_reset = true;
13502 
13503 	if (!no_heartbeat && has_reset)
13504 		return true;
13505 
13506 	return false;
13507 }
13508 
13509 /* rtnl_lock is acquired before calling this function */
13510 static void bnxt_force_fw_reset(struct bnxt *bp)
13511 {
13512 	struct bnxt_fw_health *fw_health = bp->fw_health;
13513 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13514 	u32 wait_dsecs;
13515 
13516 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
13517 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13518 		return;
13519 
13520 	if (ptp) {
13521 		spin_lock_bh(&ptp->ptp_lock);
13522 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13523 		spin_unlock_bh(&ptp->ptp_lock);
13524 	} else {
13525 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13526 	}
13527 	bnxt_fw_reset_close(bp);
13528 	wait_dsecs = fw_health->master_func_wait_dsecs;
13529 	if (fw_health->primary) {
13530 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
13531 			wait_dsecs = 0;
13532 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
13533 	} else {
13534 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
13535 		wait_dsecs = fw_health->normal_func_wait_dsecs;
13536 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13537 	}
13538 
13539 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
13540 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
13541 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
13542 }
13543 
13544 void bnxt_fw_exception(struct bnxt *bp)
13545 {
13546 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
13547 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
13548 	bnxt_ulp_stop(bp);
13549 	bnxt_rtnl_lock_sp(bp);
13550 	bnxt_force_fw_reset(bp);
13551 	bnxt_rtnl_unlock_sp(bp);
13552 }
13553 
13554 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
13555  * < 0 on error.
13556  */
13557 static int bnxt_get_registered_vfs(struct bnxt *bp)
13558 {
13559 #ifdef CONFIG_BNXT_SRIOV
13560 	int rc;
13561 
13562 	if (!BNXT_PF(bp))
13563 		return 0;
13564 
13565 	rc = bnxt_hwrm_func_qcfg(bp);
13566 	if (rc) {
13567 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
13568 		return rc;
13569 	}
13570 	if (bp->pf.registered_vfs)
13571 		return bp->pf.registered_vfs;
13572 	if (bp->sriov_cfg)
13573 		return 1;
13574 #endif
13575 	return 0;
13576 }
13577 
13578 void bnxt_fw_reset(struct bnxt *bp)
13579 {
13580 	bnxt_ulp_stop(bp);
13581 	bnxt_rtnl_lock_sp(bp);
13582 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
13583 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13584 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13585 		int n = 0, tmo;
13586 
13587 		if (ptp) {
13588 			spin_lock_bh(&ptp->ptp_lock);
13589 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13590 			spin_unlock_bh(&ptp->ptp_lock);
13591 		} else {
13592 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13593 		}
13594 		if (bp->pf.active_vfs &&
13595 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
13596 			n = bnxt_get_registered_vfs(bp);
13597 		if (n < 0) {
13598 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
13599 				   n);
13600 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13601 			dev_close(bp->dev);
13602 			goto fw_reset_exit;
13603 		} else if (n > 0) {
13604 			u16 vf_tmo_dsecs = n * 10;
13605 
13606 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
13607 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
13608 			bp->fw_reset_state =
13609 				BNXT_FW_RESET_STATE_POLL_VF;
13610 			bnxt_queue_fw_reset_work(bp, HZ / 10);
13611 			goto fw_reset_exit;
13612 		}
13613 		bnxt_fw_reset_close(bp);
13614 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
13615 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
13616 			tmo = HZ / 10;
13617 		} else {
13618 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13619 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
13620 		}
13621 		bnxt_queue_fw_reset_work(bp, tmo);
13622 	}
13623 fw_reset_exit:
13624 	bnxt_rtnl_unlock_sp(bp);
13625 }
13626 
13627 static void bnxt_chk_missed_irq(struct bnxt *bp)
13628 {
13629 	int i;
13630 
13631 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13632 		return;
13633 
13634 	for (i = 0; i < bp->cp_nr_rings; i++) {
13635 		struct bnxt_napi *bnapi = bp->bnapi[i];
13636 		struct bnxt_cp_ring_info *cpr;
13637 		u32 fw_ring_id;
13638 		int j;
13639 
13640 		if (!bnapi)
13641 			continue;
13642 
13643 		cpr = &bnapi->cp_ring;
13644 		for (j = 0; j < cpr->cp_ring_count; j++) {
13645 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
13646 			u32 val[2];
13647 
13648 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
13649 				continue;
13650 
13651 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
13652 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
13653 				continue;
13654 			}
13655 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
13656 			bnxt_dbg_hwrm_ring_info_get(bp,
13657 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
13658 				fw_ring_id, &val[0], &val[1]);
13659 			cpr->sw_stats->cmn.missed_irqs++;
13660 		}
13661 	}
13662 }
13663 
13664 static void bnxt_cfg_ntp_filters(struct bnxt *);
13665 
13666 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
13667 {
13668 	struct bnxt_link_info *link_info = &bp->link_info;
13669 
13670 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
13671 		link_info->autoneg = BNXT_AUTONEG_SPEED;
13672 		if (bp->hwrm_spec_code >= 0x10201) {
13673 			if (link_info->auto_pause_setting &
13674 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
13675 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13676 		} else {
13677 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13678 		}
13679 		bnxt_set_auto_speed(link_info);
13680 	} else {
13681 		bnxt_set_force_speed(link_info);
13682 		link_info->req_duplex = link_info->duplex_setting;
13683 	}
13684 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
13685 		link_info->req_flow_ctrl =
13686 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
13687 	else
13688 		link_info->req_flow_ctrl = link_info->force_pause_setting;
13689 }
13690 
13691 static void bnxt_fw_echo_reply(struct bnxt *bp)
13692 {
13693 	struct bnxt_fw_health *fw_health = bp->fw_health;
13694 	struct hwrm_func_echo_response_input *req;
13695 	int rc;
13696 
13697 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
13698 	if (rc)
13699 		return;
13700 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
13701 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
13702 	hwrm_req_send(bp, req);
13703 }
13704 
13705 static void bnxt_ulp_restart(struct bnxt *bp)
13706 {
13707 	bnxt_ulp_stop(bp);
13708 	bnxt_ulp_start(bp, 0);
13709 }
13710 
13711 static void bnxt_sp_task(struct work_struct *work)
13712 {
13713 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
13714 
13715 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13716 	smp_mb__after_atomic();
13717 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13718 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13719 		return;
13720 	}
13721 
13722 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
13723 		bnxt_ulp_restart(bp);
13724 		bnxt_reenable_sriov(bp);
13725 	}
13726 
13727 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
13728 		bnxt_cfg_rx_mode(bp);
13729 
13730 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
13731 		bnxt_cfg_ntp_filters(bp);
13732 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
13733 		bnxt_hwrm_exec_fwd_req(bp);
13734 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
13735 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
13736 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
13737 		bnxt_hwrm_port_qstats(bp, 0);
13738 		bnxt_hwrm_port_qstats_ext(bp, 0);
13739 		bnxt_accumulate_all_stats(bp);
13740 	}
13741 
13742 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
13743 		int rc;
13744 
13745 		mutex_lock(&bp->link_lock);
13746 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
13747 				       &bp->sp_event))
13748 			bnxt_hwrm_phy_qcaps(bp);
13749 
13750 		rc = bnxt_update_link(bp, true);
13751 		if (rc)
13752 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
13753 				   rc);
13754 
13755 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
13756 				       &bp->sp_event))
13757 			bnxt_init_ethtool_link_settings(bp);
13758 		mutex_unlock(&bp->link_lock);
13759 	}
13760 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
13761 		int rc;
13762 
13763 		mutex_lock(&bp->link_lock);
13764 		rc = bnxt_update_phy_setting(bp);
13765 		mutex_unlock(&bp->link_lock);
13766 		if (rc) {
13767 			netdev_warn(bp->dev, "update phy settings retry failed\n");
13768 		} else {
13769 			bp->link_info.phy_retry = false;
13770 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
13771 		}
13772 	}
13773 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
13774 		mutex_lock(&bp->link_lock);
13775 		bnxt_get_port_module_status(bp);
13776 		mutex_unlock(&bp->link_lock);
13777 	}
13778 
13779 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
13780 		bnxt_tc_flow_stats_work(bp);
13781 
13782 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
13783 		bnxt_chk_missed_irq(bp);
13784 
13785 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
13786 		bnxt_fw_echo_reply(bp);
13787 
13788 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
13789 		bnxt_hwmon_notify_event(bp);
13790 
13791 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
13792 	 * must be the last functions to be called before exiting.
13793 	 */
13794 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
13795 		bnxt_reset(bp, false);
13796 
13797 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
13798 		bnxt_reset(bp, true);
13799 
13800 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
13801 		bnxt_rx_ring_reset(bp);
13802 
13803 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
13804 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
13805 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
13806 			bnxt_devlink_health_fw_report(bp);
13807 		else
13808 			bnxt_fw_reset(bp);
13809 	}
13810 
13811 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
13812 		if (!is_bnxt_fw_ok(bp))
13813 			bnxt_devlink_health_fw_report(bp);
13814 	}
13815 
13816 	smp_mb__before_atomic();
13817 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13818 }
13819 
13820 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
13821 				int *max_cp);
13822 
13823 /* Under rtnl_lock */
13824 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
13825 		     int tx_xdp)
13826 {
13827 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
13828 	struct bnxt_hw_rings hwr = {0};
13829 	int rx_rings = rx;
13830 
13831 	if (tcs)
13832 		tx_sets = tcs;
13833 
13834 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
13835 
13836 	if (max_rx < rx_rings)
13837 		return -ENOMEM;
13838 
13839 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
13840 		rx_rings <<= 1;
13841 
13842 	hwr.rx = rx_rings;
13843 	hwr.tx = tx * tx_sets + tx_xdp;
13844 	if (max_tx < hwr.tx)
13845 		return -ENOMEM;
13846 
13847 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
13848 
13849 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
13850 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
13851 	if (max_cp < hwr.cp)
13852 		return -ENOMEM;
13853 	hwr.stat = hwr.cp;
13854 	if (BNXT_NEW_RM(bp)) {
13855 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
13856 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
13857 		hwr.grp = rx;
13858 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13859 	}
13860 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
13861 		hwr.cp_p5 = hwr.tx + rx;
13862 	return bnxt_hwrm_check_rings(bp, &hwr);
13863 }
13864 
13865 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
13866 {
13867 	if (bp->bar2) {
13868 		pci_iounmap(pdev, bp->bar2);
13869 		bp->bar2 = NULL;
13870 	}
13871 
13872 	if (bp->bar1) {
13873 		pci_iounmap(pdev, bp->bar1);
13874 		bp->bar1 = NULL;
13875 	}
13876 
13877 	if (bp->bar0) {
13878 		pci_iounmap(pdev, bp->bar0);
13879 		bp->bar0 = NULL;
13880 	}
13881 }
13882 
13883 static void bnxt_cleanup_pci(struct bnxt *bp)
13884 {
13885 	bnxt_unmap_bars(bp, bp->pdev);
13886 	pci_release_regions(bp->pdev);
13887 	if (pci_is_enabled(bp->pdev))
13888 		pci_disable_device(bp->pdev);
13889 }
13890 
13891 static void bnxt_init_dflt_coal(struct bnxt *bp)
13892 {
13893 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
13894 	struct bnxt_coal *coal;
13895 	u16 flags = 0;
13896 
13897 	if (coal_cap->cmpl_params &
13898 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
13899 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
13900 
13901 	/* Tick values in micro seconds.
13902 	 * 1 coal_buf x bufs_per_record = 1 completion record.
13903 	 */
13904 	coal = &bp->rx_coal;
13905 	coal->coal_ticks = 10;
13906 	coal->coal_bufs = 30;
13907 	coal->coal_ticks_irq = 1;
13908 	coal->coal_bufs_irq = 2;
13909 	coal->idle_thresh = 50;
13910 	coal->bufs_per_record = 2;
13911 	coal->budget = 64;		/* NAPI budget */
13912 	coal->flags = flags;
13913 
13914 	coal = &bp->tx_coal;
13915 	coal->coal_ticks = 28;
13916 	coal->coal_bufs = 30;
13917 	coal->coal_ticks_irq = 2;
13918 	coal->coal_bufs_irq = 2;
13919 	coal->bufs_per_record = 1;
13920 	coal->flags = flags;
13921 
13922 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
13923 }
13924 
13925 /* FW that pre-reserves 1 VNIC per function */
13926 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
13927 {
13928 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
13929 
13930 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13931 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
13932 		return true;
13933 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13934 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
13935 		return true;
13936 	return false;
13937 }
13938 
13939 static int bnxt_fw_init_one_p1(struct bnxt *bp)
13940 {
13941 	int rc;
13942 
13943 	bp->fw_cap = 0;
13944 	rc = bnxt_hwrm_ver_get(bp);
13945 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
13946 	 * so wait before continuing with recovery.
13947 	 */
13948 	if (rc)
13949 		msleep(100);
13950 	bnxt_try_map_fw_health_reg(bp);
13951 	if (rc) {
13952 		rc = bnxt_try_recover_fw(bp);
13953 		if (rc)
13954 			return rc;
13955 		rc = bnxt_hwrm_ver_get(bp);
13956 		if (rc)
13957 			return rc;
13958 	}
13959 
13960 	bnxt_nvm_cfg_ver_get(bp);
13961 
13962 	rc = bnxt_hwrm_func_reset(bp);
13963 	if (rc)
13964 		return -ENODEV;
13965 
13966 	bnxt_hwrm_fw_set_time(bp);
13967 	return 0;
13968 }
13969 
13970 static int bnxt_fw_init_one_p2(struct bnxt *bp)
13971 {
13972 	int rc;
13973 
13974 	/* Get the MAX capabilities for this function */
13975 	rc = bnxt_hwrm_func_qcaps(bp);
13976 	if (rc) {
13977 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
13978 			   rc);
13979 		return -ENODEV;
13980 	}
13981 
13982 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
13983 	if (rc)
13984 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
13985 			    rc);
13986 
13987 	if (bnxt_alloc_fw_health(bp)) {
13988 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
13989 	} else {
13990 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
13991 		if (rc)
13992 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
13993 				    rc);
13994 	}
13995 
13996 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
13997 	if (rc)
13998 		return -ENODEV;
13999 
14000 	if (bnxt_fw_pre_resv_vnics(bp))
14001 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
14002 
14003 	bnxt_hwrm_func_qcfg(bp);
14004 	bnxt_hwrm_vnic_qcaps(bp);
14005 	bnxt_hwrm_port_led_qcaps(bp);
14006 	bnxt_ethtool_init(bp);
14007 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
14008 		__bnxt_hwrm_ptp_qcfg(bp);
14009 	bnxt_dcb_init(bp);
14010 	bnxt_hwmon_init(bp);
14011 	return 0;
14012 }
14013 
14014 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
14015 {
14016 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
14017 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
14018 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
14019 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
14020 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
14021 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
14022 		bp->rss_hash_delta = bp->rss_hash_cfg;
14023 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
14024 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
14025 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
14026 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
14027 	}
14028 }
14029 
14030 static void bnxt_set_dflt_rfs(struct bnxt *bp)
14031 {
14032 	struct net_device *dev = bp->dev;
14033 
14034 	dev->hw_features &= ~NETIF_F_NTUPLE;
14035 	dev->features &= ~NETIF_F_NTUPLE;
14036 	bp->flags &= ~BNXT_FLAG_RFS;
14037 	if (bnxt_rfs_supported(bp)) {
14038 		dev->hw_features |= NETIF_F_NTUPLE;
14039 		if (bnxt_rfs_capable(bp, false)) {
14040 			bp->flags |= BNXT_FLAG_RFS;
14041 			dev->features |= NETIF_F_NTUPLE;
14042 		}
14043 	}
14044 }
14045 
14046 static void bnxt_fw_init_one_p3(struct bnxt *bp)
14047 {
14048 	struct pci_dev *pdev = bp->pdev;
14049 
14050 	bnxt_set_dflt_rss_hash_type(bp);
14051 	bnxt_set_dflt_rfs(bp);
14052 
14053 	bnxt_get_wol_settings(bp);
14054 	if (bp->flags & BNXT_FLAG_WOL_CAP)
14055 		device_set_wakeup_enable(&pdev->dev, bp->wol);
14056 	else
14057 		device_set_wakeup_capable(&pdev->dev, false);
14058 
14059 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
14060 	bnxt_hwrm_coal_params_qcaps(bp);
14061 }
14062 
14063 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
14064 
14065 int bnxt_fw_init_one(struct bnxt *bp)
14066 {
14067 	int rc;
14068 
14069 	rc = bnxt_fw_init_one_p1(bp);
14070 	if (rc) {
14071 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
14072 		return rc;
14073 	}
14074 	rc = bnxt_fw_init_one_p2(bp);
14075 	if (rc) {
14076 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
14077 		return rc;
14078 	}
14079 	rc = bnxt_probe_phy(bp, false);
14080 	if (rc)
14081 		return rc;
14082 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
14083 	if (rc)
14084 		return rc;
14085 
14086 	bnxt_fw_init_one_p3(bp);
14087 	return 0;
14088 }
14089 
14090 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
14091 {
14092 	struct bnxt_fw_health *fw_health = bp->fw_health;
14093 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
14094 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
14095 	u32 reg_type, reg_off, delay_msecs;
14096 
14097 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
14098 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
14099 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
14100 	switch (reg_type) {
14101 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
14102 		pci_write_config_dword(bp->pdev, reg_off, val);
14103 		break;
14104 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
14105 		writel(reg_off & BNXT_GRC_BASE_MASK,
14106 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
14107 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
14108 		fallthrough;
14109 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
14110 		writel(val, bp->bar0 + reg_off);
14111 		break;
14112 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
14113 		writel(val, bp->bar1 + reg_off);
14114 		break;
14115 	}
14116 	if (delay_msecs) {
14117 		pci_read_config_dword(bp->pdev, 0, &val);
14118 		msleep(delay_msecs);
14119 	}
14120 }
14121 
14122 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
14123 {
14124 	struct hwrm_func_qcfg_output *resp;
14125 	struct hwrm_func_qcfg_input *req;
14126 	bool result = true; /* firmware will enforce if unknown */
14127 
14128 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
14129 		return result;
14130 
14131 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
14132 		return result;
14133 
14134 	req->fid = cpu_to_le16(0xffff);
14135 	resp = hwrm_req_hold(bp, req);
14136 	if (!hwrm_req_send(bp, req))
14137 		result = !!(le16_to_cpu(resp->flags) &
14138 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
14139 	hwrm_req_drop(bp, req);
14140 	return result;
14141 }
14142 
14143 static void bnxt_reset_all(struct bnxt *bp)
14144 {
14145 	struct bnxt_fw_health *fw_health = bp->fw_health;
14146 	int i, rc;
14147 
14148 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14149 		bnxt_fw_reset_via_optee(bp);
14150 		bp->fw_reset_timestamp = jiffies;
14151 		return;
14152 	}
14153 
14154 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
14155 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
14156 			bnxt_fw_reset_writel(bp, i);
14157 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
14158 		struct hwrm_fw_reset_input *req;
14159 
14160 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
14161 		if (!rc) {
14162 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
14163 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
14164 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
14165 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
14166 			rc = hwrm_req_send(bp, req);
14167 		}
14168 		if (rc != -ENODEV)
14169 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
14170 	}
14171 	bp->fw_reset_timestamp = jiffies;
14172 }
14173 
14174 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
14175 {
14176 	return time_after(jiffies, bp->fw_reset_timestamp +
14177 			  (bp->fw_reset_max_dsecs * HZ / 10));
14178 }
14179 
14180 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
14181 {
14182 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14183 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
14184 		bnxt_dl_health_fw_status_update(bp, false);
14185 	bp->fw_reset_state = 0;
14186 	dev_close(bp->dev);
14187 }
14188 
14189 static void bnxt_fw_reset_task(struct work_struct *work)
14190 {
14191 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
14192 	int rc = 0;
14193 
14194 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14195 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
14196 		return;
14197 	}
14198 
14199 	switch (bp->fw_reset_state) {
14200 	case BNXT_FW_RESET_STATE_POLL_VF: {
14201 		int n = bnxt_get_registered_vfs(bp);
14202 		int tmo;
14203 
14204 		if (n < 0) {
14205 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
14206 				   n, jiffies_to_msecs(jiffies -
14207 				   bp->fw_reset_timestamp));
14208 			goto fw_reset_abort;
14209 		} else if (n > 0) {
14210 			if (bnxt_fw_reset_timeout(bp)) {
14211 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14212 				bp->fw_reset_state = 0;
14213 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
14214 					   n);
14215 				goto ulp_start;
14216 			}
14217 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14218 			return;
14219 		}
14220 		bp->fw_reset_timestamp = jiffies;
14221 		rtnl_lock();
14222 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
14223 			bnxt_fw_reset_abort(bp, rc);
14224 			rtnl_unlock();
14225 			goto ulp_start;
14226 		}
14227 		bnxt_fw_reset_close(bp);
14228 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14229 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14230 			tmo = HZ / 10;
14231 		} else {
14232 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14233 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14234 		}
14235 		rtnl_unlock();
14236 		bnxt_queue_fw_reset_work(bp, tmo);
14237 		return;
14238 	}
14239 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
14240 		u32 val;
14241 
14242 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14243 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
14244 		    !bnxt_fw_reset_timeout(bp)) {
14245 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14246 			return;
14247 		}
14248 
14249 		if (!bp->fw_health->primary) {
14250 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
14251 
14252 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14253 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14254 			return;
14255 		}
14256 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14257 	}
14258 		fallthrough;
14259 	case BNXT_FW_RESET_STATE_RESET_FW:
14260 		bnxt_reset_all(bp);
14261 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14262 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
14263 		return;
14264 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
14265 		bnxt_inv_fw_health_reg(bp);
14266 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
14267 		    !bp->fw_reset_min_dsecs) {
14268 			u16 val;
14269 
14270 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14271 			if (val == 0xffff) {
14272 				if (bnxt_fw_reset_timeout(bp)) {
14273 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
14274 					rc = -ETIMEDOUT;
14275 					goto fw_reset_abort;
14276 				}
14277 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
14278 				return;
14279 			}
14280 		}
14281 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14282 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
14283 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
14284 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
14285 			bnxt_dl_remote_reload(bp);
14286 		if (pci_enable_device(bp->pdev)) {
14287 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
14288 			rc = -ENODEV;
14289 			goto fw_reset_abort;
14290 		}
14291 		pci_set_master(bp->pdev);
14292 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
14293 		fallthrough;
14294 	case BNXT_FW_RESET_STATE_POLL_FW:
14295 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
14296 		rc = bnxt_hwrm_poll(bp);
14297 		if (rc) {
14298 			if (bnxt_fw_reset_timeout(bp)) {
14299 				netdev_err(bp->dev, "Firmware reset aborted\n");
14300 				goto fw_reset_abort_status;
14301 			}
14302 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14303 			return;
14304 		}
14305 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
14306 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
14307 		fallthrough;
14308 	case BNXT_FW_RESET_STATE_OPENING:
14309 		while (!rtnl_trylock()) {
14310 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14311 			return;
14312 		}
14313 		rc = bnxt_open(bp->dev);
14314 		if (rc) {
14315 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
14316 			bnxt_fw_reset_abort(bp, rc);
14317 			rtnl_unlock();
14318 			goto ulp_start;
14319 		}
14320 
14321 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
14322 		    bp->fw_health->enabled) {
14323 			bp->fw_health->last_fw_reset_cnt =
14324 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14325 		}
14326 		bp->fw_reset_state = 0;
14327 		/* Make sure fw_reset_state is 0 before clearing the flag */
14328 		smp_mb__before_atomic();
14329 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14330 		bnxt_ptp_reapply_pps(bp);
14331 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
14332 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
14333 			bnxt_dl_health_fw_recovery_done(bp);
14334 			bnxt_dl_health_fw_status_update(bp, true);
14335 		}
14336 		rtnl_unlock();
14337 		bnxt_ulp_start(bp, 0);
14338 		bnxt_reenable_sriov(bp);
14339 		rtnl_lock();
14340 		bnxt_vf_reps_alloc(bp);
14341 		bnxt_vf_reps_open(bp);
14342 		rtnl_unlock();
14343 		break;
14344 	}
14345 	return;
14346 
14347 fw_reset_abort_status:
14348 	if (bp->fw_health->status_reliable ||
14349 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
14350 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14351 
14352 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
14353 	}
14354 fw_reset_abort:
14355 	rtnl_lock();
14356 	bnxt_fw_reset_abort(bp, rc);
14357 	rtnl_unlock();
14358 ulp_start:
14359 	bnxt_ulp_start(bp, rc);
14360 }
14361 
14362 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
14363 {
14364 	int rc;
14365 	struct bnxt *bp = netdev_priv(dev);
14366 
14367 	SET_NETDEV_DEV(dev, &pdev->dev);
14368 
14369 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
14370 	rc = pci_enable_device(pdev);
14371 	if (rc) {
14372 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
14373 		goto init_err;
14374 	}
14375 
14376 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
14377 		dev_err(&pdev->dev,
14378 			"Cannot find PCI device base address, aborting\n");
14379 		rc = -ENODEV;
14380 		goto init_err_disable;
14381 	}
14382 
14383 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
14384 	if (rc) {
14385 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
14386 		goto init_err_disable;
14387 	}
14388 
14389 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
14390 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
14391 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
14392 		rc = -EIO;
14393 		goto init_err_release;
14394 	}
14395 
14396 	pci_set_master(pdev);
14397 
14398 	bp->dev = dev;
14399 	bp->pdev = pdev;
14400 
14401 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
14402 	 * determines the BAR size.
14403 	 */
14404 	bp->bar0 = pci_ioremap_bar(pdev, 0);
14405 	if (!bp->bar0) {
14406 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
14407 		rc = -ENOMEM;
14408 		goto init_err_release;
14409 	}
14410 
14411 	bp->bar2 = pci_ioremap_bar(pdev, 4);
14412 	if (!bp->bar2) {
14413 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
14414 		rc = -ENOMEM;
14415 		goto init_err_release;
14416 	}
14417 
14418 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
14419 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
14420 
14421 	spin_lock_init(&bp->ntp_fltr_lock);
14422 #if BITS_PER_LONG == 32
14423 	spin_lock_init(&bp->db_lock);
14424 #endif
14425 
14426 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
14427 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
14428 
14429 	timer_setup(&bp->timer, bnxt_timer, 0);
14430 	bp->current_interval = BNXT_TIMER_INTERVAL;
14431 
14432 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
14433 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
14434 
14435 	clear_bit(BNXT_STATE_OPEN, &bp->state);
14436 	return 0;
14437 
14438 init_err_release:
14439 	bnxt_unmap_bars(bp, pdev);
14440 	pci_release_regions(pdev);
14441 
14442 init_err_disable:
14443 	pci_disable_device(pdev);
14444 
14445 init_err:
14446 	return rc;
14447 }
14448 
14449 /* rtnl_lock held */
14450 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
14451 {
14452 	struct sockaddr *addr = p;
14453 	struct bnxt *bp = netdev_priv(dev);
14454 	int rc = 0;
14455 
14456 	if (!is_valid_ether_addr(addr->sa_data))
14457 		return -EADDRNOTAVAIL;
14458 
14459 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
14460 		return 0;
14461 
14462 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
14463 	if (rc)
14464 		return rc;
14465 
14466 	eth_hw_addr_set(dev, addr->sa_data);
14467 	bnxt_clear_usr_fltrs(bp, true);
14468 	if (netif_running(dev)) {
14469 		bnxt_close_nic(bp, false, false);
14470 		rc = bnxt_open_nic(bp, false, false);
14471 	}
14472 
14473 	return rc;
14474 }
14475 
14476 /* rtnl_lock held */
14477 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
14478 {
14479 	struct bnxt *bp = netdev_priv(dev);
14480 
14481 	if (netif_running(dev))
14482 		bnxt_close_nic(bp, true, false);
14483 
14484 	WRITE_ONCE(dev->mtu, new_mtu);
14485 	bnxt_set_ring_params(bp);
14486 
14487 	if (netif_running(dev))
14488 		return bnxt_open_nic(bp, true, false);
14489 
14490 	return 0;
14491 }
14492 
14493 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
14494 {
14495 	struct bnxt *bp = netdev_priv(dev);
14496 	bool sh = false;
14497 	int rc, tx_cp;
14498 
14499 	if (tc > bp->max_tc) {
14500 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
14501 			   tc, bp->max_tc);
14502 		return -EINVAL;
14503 	}
14504 
14505 	if (bp->num_tc == tc)
14506 		return 0;
14507 
14508 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
14509 		sh = true;
14510 
14511 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
14512 			      sh, tc, bp->tx_nr_rings_xdp);
14513 	if (rc)
14514 		return rc;
14515 
14516 	/* Needs to close the device and do hw resource re-allocations */
14517 	if (netif_running(bp->dev))
14518 		bnxt_close_nic(bp, true, false);
14519 
14520 	if (tc) {
14521 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
14522 		netdev_set_num_tc(dev, tc);
14523 		bp->num_tc = tc;
14524 	} else {
14525 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
14526 		netdev_reset_tc(dev);
14527 		bp->num_tc = 0;
14528 	}
14529 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
14530 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
14531 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
14532 			       tx_cp + bp->rx_nr_rings;
14533 
14534 	if (netif_running(bp->dev))
14535 		return bnxt_open_nic(bp, true, false);
14536 
14537 	return 0;
14538 }
14539 
14540 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
14541 				  void *cb_priv)
14542 {
14543 	struct bnxt *bp = cb_priv;
14544 
14545 	if (!bnxt_tc_flower_enabled(bp) ||
14546 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
14547 		return -EOPNOTSUPP;
14548 
14549 	switch (type) {
14550 	case TC_SETUP_CLSFLOWER:
14551 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
14552 	default:
14553 		return -EOPNOTSUPP;
14554 	}
14555 }
14556 
14557 LIST_HEAD(bnxt_block_cb_list);
14558 
14559 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
14560 			 void *type_data)
14561 {
14562 	struct bnxt *bp = netdev_priv(dev);
14563 
14564 	switch (type) {
14565 	case TC_SETUP_BLOCK:
14566 		return flow_block_cb_setup_simple(type_data,
14567 						  &bnxt_block_cb_list,
14568 						  bnxt_setup_tc_block_cb,
14569 						  bp, bp, true);
14570 	case TC_SETUP_QDISC_MQPRIO: {
14571 		struct tc_mqprio_qopt *mqprio = type_data;
14572 
14573 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
14574 
14575 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
14576 	}
14577 	default:
14578 		return -EOPNOTSUPP;
14579 	}
14580 }
14581 
14582 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
14583 			    const struct sk_buff *skb)
14584 {
14585 	struct bnxt_vnic_info *vnic;
14586 
14587 	if (skb)
14588 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
14589 
14590 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
14591 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
14592 }
14593 
14594 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
14595 			   u32 idx)
14596 {
14597 	struct hlist_head *head;
14598 	int bit_id;
14599 
14600 	spin_lock_bh(&bp->ntp_fltr_lock);
14601 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
14602 	if (bit_id < 0) {
14603 		spin_unlock_bh(&bp->ntp_fltr_lock);
14604 		return -ENOMEM;
14605 	}
14606 
14607 	fltr->base.sw_id = (u16)bit_id;
14608 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
14609 	fltr->base.flags |= BNXT_ACT_RING_DST;
14610 	head = &bp->ntp_fltr_hash_tbl[idx];
14611 	hlist_add_head_rcu(&fltr->base.hash, head);
14612 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
14613 	bnxt_insert_usr_fltr(bp, &fltr->base);
14614 	bp->ntp_fltr_count++;
14615 	spin_unlock_bh(&bp->ntp_fltr_lock);
14616 	return 0;
14617 }
14618 
14619 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
14620 			    struct bnxt_ntuple_filter *f2)
14621 {
14622 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
14623 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
14624 	struct flow_keys *keys1 = &f1->fkeys;
14625 	struct flow_keys *keys2 = &f2->fkeys;
14626 
14627 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
14628 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
14629 		return false;
14630 
14631 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
14632 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
14633 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
14634 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
14635 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
14636 			return false;
14637 	} else {
14638 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
14639 				     &keys2->addrs.v6addrs.src) ||
14640 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
14641 				     &masks2->addrs.v6addrs.src) ||
14642 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
14643 				     &keys2->addrs.v6addrs.dst) ||
14644 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
14645 				     &masks2->addrs.v6addrs.dst))
14646 			return false;
14647 	}
14648 
14649 	return keys1->ports.src == keys2->ports.src &&
14650 	       masks1->ports.src == masks2->ports.src &&
14651 	       keys1->ports.dst == keys2->ports.dst &&
14652 	       masks1->ports.dst == masks2->ports.dst &&
14653 	       keys1->control.flags == keys2->control.flags &&
14654 	       f1->l2_fltr == f2->l2_fltr;
14655 }
14656 
14657 struct bnxt_ntuple_filter *
14658 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
14659 				struct bnxt_ntuple_filter *fltr, u32 idx)
14660 {
14661 	struct bnxt_ntuple_filter *f;
14662 	struct hlist_head *head;
14663 
14664 	head = &bp->ntp_fltr_hash_tbl[idx];
14665 	hlist_for_each_entry_rcu(f, head, base.hash) {
14666 		if (bnxt_fltr_match(f, fltr))
14667 			return f;
14668 	}
14669 	return NULL;
14670 }
14671 
14672 #ifdef CONFIG_RFS_ACCEL
14673 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
14674 			      u16 rxq_index, u32 flow_id)
14675 {
14676 	struct bnxt *bp = netdev_priv(dev);
14677 	struct bnxt_ntuple_filter *fltr, *new_fltr;
14678 	struct flow_keys *fkeys;
14679 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
14680 	struct bnxt_l2_filter *l2_fltr;
14681 	int rc = 0, idx;
14682 	u32 flags;
14683 
14684 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
14685 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
14686 		atomic_inc(&l2_fltr->refcnt);
14687 	} else {
14688 		struct bnxt_l2_key key;
14689 
14690 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
14691 		key.vlan = 0;
14692 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
14693 		if (!l2_fltr)
14694 			return -EINVAL;
14695 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
14696 			bnxt_del_l2_filter(bp, l2_fltr);
14697 			return -EINVAL;
14698 		}
14699 	}
14700 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
14701 	if (!new_fltr) {
14702 		bnxt_del_l2_filter(bp, l2_fltr);
14703 		return -ENOMEM;
14704 	}
14705 
14706 	fkeys = &new_fltr->fkeys;
14707 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
14708 		rc = -EPROTONOSUPPORT;
14709 		goto err_free;
14710 	}
14711 
14712 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
14713 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
14714 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
14715 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
14716 		rc = -EPROTONOSUPPORT;
14717 		goto err_free;
14718 	}
14719 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
14720 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
14721 		if (bp->hwrm_spec_code < 0x10601) {
14722 			rc = -EPROTONOSUPPORT;
14723 			goto err_free;
14724 		}
14725 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
14726 	}
14727 	flags = fkeys->control.flags;
14728 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
14729 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
14730 		rc = -EPROTONOSUPPORT;
14731 		goto err_free;
14732 	}
14733 	new_fltr->l2_fltr = l2_fltr;
14734 
14735 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
14736 	rcu_read_lock();
14737 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
14738 	if (fltr) {
14739 		rc = fltr->base.sw_id;
14740 		rcu_read_unlock();
14741 		goto err_free;
14742 	}
14743 	rcu_read_unlock();
14744 
14745 	new_fltr->flow_id = flow_id;
14746 	new_fltr->base.rxq = rxq_index;
14747 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
14748 	if (!rc) {
14749 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14750 		return new_fltr->base.sw_id;
14751 	}
14752 
14753 err_free:
14754 	bnxt_del_l2_filter(bp, l2_fltr);
14755 	kfree(new_fltr);
14756 	return rc;
14757 }
14758 #endif
14759 
14760 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
14761 {
14762 	spin_lock_bh(&bp->ntp_fltr_lock);
14763 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
14764 		spin_unlock_bh(&bp->ntp_fltr_lock);
14765 		return;
14766 	}
14767 	hlist_del_rcu(&fltr->base.hash);
14768 	bnxt_del_one_usr_fltr(bp, &fltr->base);
14769 	bp->ntp_fltr_count--;
14770 	spin_unlock_bh(&bp->ntp_fltr_lock);
14771 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
14772 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
14773 	kfree_rcu(fltr, base.rcu);
14774 }
14775 
14776 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
14777 {
14778 #ifdef CONFIG_RFS_ACCEL
14779 	int i;
14780 
14781 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
14782 		struct hlist_head *head;
14783 		struct hlist_node *tmp;
14784 		struct bnxt_ntuple_filter *fltr;
14785 		int rc;
14786 
14787 		head = &bp->ntp_fltr_hash_tbl[i];
14788 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
14789 			bool del = false;
14790 
14791 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
14792 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
14793 					continue;
14794 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
14795 							fltr->flow_id,
14796 							fltr->base.sw_id)) {
14797 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
14798 									 fltr);
14799 					del = true;
14800 				}
14801 			} else {
14802 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
14803 								       fltr);
14804 				if (rc)
14805 					del = true;
14806 				else
14807 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
14808 			}
14809 
14810 			if (del)
14811 				bnxt_del_ntp_filter(bp, fltr);
14812 		}
14813 	}
14814 #endif
14815 }
14816 
14817 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
14818 				    unsigned int entry, struct udp_tunnel_info *ti)
14819 {
14820 	struct bnxt *bp = netdev_priv(netdev);
14821 	unsigned int cmd;
14822 
14823 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
14824 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
14825 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
14826 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
14827 	else
14828 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
14829 
14830 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
14831 }
14832 
14833 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
14834 				      unsigned int entry, struct udp_tunnel_info *ti)
14835 {
14836 	struct bnxt *bp = netdev_priv(netdev);
14837 	unsigned int cmd;
14838 
14839 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
14840 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
14841 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
14842 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
14843 	else
14844 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
14845 
14846 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
14847 }
14848 
14849 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
14850 	.set_port	= bnxt_udp_tunnel_set_port,
14851 	.unset_port	= bnxt_udp_tunnel_unset_port,
14852 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
14853 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
14854 	.tables		= {
14855 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
14856 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
14857 	},
14858 }, bnxt_udp_tunnels_p7 = {
14859 	.set_port	= bnxt_udp_tunnel_set_port,
14860 	.unset_port	= bnxt_udp_tunnel_unset_port,
14861 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
14862 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
14863 	.tables		= {
14864 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
14865 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
14866 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
14867 	},
14868 };
14869 
14870 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
14871 			       struct net_device *dev, u32 filter_mask,
14872 			       int nlflags)
14873 {
14874 	struct bnxt *bp = netdev_priv(dev);
14875 
14876 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
14877 				       nlflags, filter_mask, NULL);
14878 }
14879 
14880 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
14881 			       u16 flags, struct netlink_ext_ack *extack)
14882 {
14883 	struct bnxt *bp = netdev_priv(dev);
14884 	struct nlattr *attr, *br_spec;
14885 	int rem, rc = 0;
14886 
14887 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
14888 		return -EOPNOTSUPP;
14889 
14890 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
14891 	if (!br_spec)
14892 		return -EINVAL;
14893 
14894 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
14895 		u16 mode;
14896 
14897 		mode = nla_get_u16(attr);
14898 		if (mode == bp->br_mode)
14899 			break;
14900 
14901 		rc = bnxt_hwrm_set_br_mode(bp, mode);
14902 		if (!rc)
14903 			bp->br_mode = mode;
14904 		break;
14905 	}
14906 	return rc;
14907 }
14908 
14909 int bnxt_get_port_parent_id(struct net_device *dev,
14910 			    struct netdev_phys_item_id *ppid)
14911 {
14912 	struct bnxt *bp = netdev_priv(dev);
14913 
14914 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
14915 		return -EOPNOTSUPP;
14916 
14917 	/* The PF and it's VF-reps only support the switchdev framework */
14918 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
14919 		return -EOPNOTSUPP;
14920 
14921 	ppid->id_len = sizeof(bp->dsn);
14922 	memcpy(ppid->id, bp->dsn, ppid->id_len);
14923 
14924 	return 0;
14925 }
14926 
14927 static const struct net_device_ops bnxt_netdev_ops = {
14928 	.ndo_open		= bnxt_open,
14929 	.ndo_start_xmit		= bnxt_start_xmit,
14930 	.ndo_stop		= bnxt_close,
14931 	.ndo_get_stats64	= bnxt_get_stats64,
14932 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
14933 	.ndo_eth_ioctl		= bnxt_ioctl,
14934 	.ndo_validate_addr	= eth_validate_addr,
14935 	.ndo_set_mac_address	= bnxt_change_mac_addr,
14936 	.ndo_change_mtu		= bnxt_change_mtu,
14937 	.ndo_fix_features	= bnxt_fix_features,
14938 	.ndo_set_features	= bnxt_set_features,
14939 	.ndo_features_check	= bnxt_features_check,
14940 	.ndo_tx_timeout		= bnxt_tx_timeout,
14941 #ifdef CONFIG_BNXT_SRIOV
14942 	.ndo_get_vf_config	= bnxt_get_vf_config,
14943 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
14944 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
14945 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
14946 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
14947 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
14948 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
14949 #endif
14950 	.ndo_setup_tc           = bnxt_setup_tc,
14951 #ifdef CONFIG_RFS_ACCEL
14952 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
14953 #endif
14954 	.ndo_bpf		= bnxt_xdp,
14955 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
14956 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
14957 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
14958 };
14959 
14960 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
14961 				    struct netdev_queue_stats_rx *stats)
14962 {
14963 	struct bnxt *bp = netdev_priv(dev);
14964 	struct bnxt_cp_ring_info *cpr;
14965 	u64 *sw;
14966 
14967 	cpr = &bp->bnapi[i]->cp_ring;
14968 	sw = cpr->stats.sw_stats;
14969 
14970 	stats->packets = 0;
14971 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
14972 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
14973 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
14974 
14975 	stats->bytes = 0;
14976 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
14977 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
14978 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
14979 
14980 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
14981 }
14982 
14983 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
14984 				    struct netdev_queue_stats_tx *stats)
14985 {
14986 	struct bnxt *bp = netdev_priv(dev);
14987 	struct bnxt_napi *bnapi;
14988 	u64 *sw;
14989 
14990 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
14991 	sw = bnapi->cp_ring.stats.sw_stats;
14992 
14993 	stats->packets = 0;
14994 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
14995 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
14996 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
14997 
14998 	stats->bytes = 0;
14999 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
15000 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
15001 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
15002 }
15003 
15004 static void bnxt_get_base_stats(struct net_device *dev,
15005 				struct netdev_queue_stats_rx *rx,
15006 				struct netdev_queue_stats_tx *tx)
15007 {
15008 	struct bnxt *bp = netdev_priv(dev);
15009 
15010 	rx->packets = bp->net_stats_prev.rx_packets;
15011 	rx->bytes = bp->net_stats_prev.rx_bytes;
15012 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
15013 
15014 	tx->packets = bp->net_stats_prev.tx_packets;
15015 	tx->bytes = bp->net_stats_prev.tx_bytes;
15016 }
15017 
15018 static const struct netdev_stat_ops bnxt_stat_ops = {
15019 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
15020 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
15021 	.get_base_stats		= bnxt_get_base_stats,
15022 };
15023 
15024 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
15025 {
15026 	u16 mem_size;
15027 
15028 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
15029 	mem_size = rxr->rx_agg_bmap_size / 8;
15030 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
15031 	if (!rxr->rx_agg_bmap)
15032 		return -ENOMEM;
15033 
15034 	return 0;
15035 }
15036 
15037 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx)
15038 {
15039 	struct bnxt_rx_ring_info *rxr, *clone;
15040 	struct bnxt *bp = netdev_priv(dev);
15041 	struct bnxt_ring_struct *ring;
15042 	int rc;
15043 
15044 	rxr = &bp->rx_ring[idx];
15045 	clone = qmem;
15046 	memcpy(clone, rxr, sizeof(*rxr));
15047 	bnxt_init_rx_ring_struct(bp, clone);
15048 	bnxt_reset_rx_ring_struct(bp, clone);
15049 
15050 	clone->rx_prod = 0;
15051 	clone->rx_agg_prod = 0;
15052 	clone->rx_sw_agg_prod = 0;
15053 	clone->rx_next_cons = 0;
15054 
15055 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
15056 	if (rc)
15057 		return rc;
15058 
15059 	ring = &clone->rx_ring_struct;
15060 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15061 	if (rc)
15062 		goto err_free_rx_ring;
15063 
15064 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
15065 		ring = &clone->rx_agg_ring_struct;
15066 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15067 		if (rc)
15068 			goto err_free_rx_agg_ring;
15069 
15070 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
15071 		if (rc)
15072 			goto err_free_rx_agg_ring;
15073 	}
15074 
15075 	bnxt_init_one_rx_ring_rxbd(bp, clone);
15076 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
15077 
15078 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
15079 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15080 		bnxt_alloc_one_rx_ring_page(bp, clone, idx);
15081 
15082 	return 0;
15083 
15084 err_free_rx_agg_ring:
15085 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
15086 err_free_rx_ring:
15087 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
15088 	clone->page_pool->p.napi = NULL;
15089 	page_pool_destroy(clone->page_pool);
15090 	clone->page_pool = NULL;
15091 	return rc;
15092 }
15093 
15094 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
15095 {
15096 	struct bnxt_rx_ring_info *rxr = qmem;
15097 	struct bnxt *bp = netdev_priv(dev);
15098 	struct bnxt_ring_struct *ring;
15099 
15100 	bnxt_free_one_rx_ring(bp, rxr);
15101 	bnxt_free_one_rx_agg_ring(bp, rxr);
15102 
15103 	page_pool_destroy(rxr->page_pool);
15104 	rxr->page_pool = NULL;
15105 
15106 	ring = &rxr->rx_ring_struct;
15107 	bnxt_free_ring(bp, &ring->ring_mem);
15108 
15109 	ring = &rxr->rx_agg_ring_struct;
15110 	bnxt_free_ring(bp, &ring->ring_mem);
15111 
15112 	kfree(rxr->rx_agg_bmap);
15113 	rxr->rx_agg_bmap = NULL;
15114 }
15115 
15116 static void bnxt_copy_rx_ring(struct bnxt *bp,
15117 			      struct bnxt_rx_ring_info *dst,
15118 			      struct bnxt_rx_ring_info *src)
15119 {
15120 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
15121 	struct bnxt_ring_struct *dst_ring, *src_ring;
15122 	int i;
15123 
15124 	dst_ring = &dst->rx_ring_struct;
15125 	dst_rmem = &dst_ring->ring_mem;
15126 	src_ring = &src->rx_ring_struct;
15127 	src_rmem = &src_ring->ring_mem;
15128 
15129 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15130 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15131 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15132 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15133 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15134 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15135 
15136 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15137 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15138 	*dst_rmem->vmem = *src_rmem->vmem;
15139 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15140 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15141 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15142 	}
15143 
15144 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
15145 		return;
15146 
15147 	dst_ring = &dst->rx_agg_ring_struct;
15148 	dst_rmem = &dst_ring->ring_mem;
15149 	src_ring = &src->rx_agg_ring_struct;
15150 	src_rmem = &src_ring->ring_mem;
15151 
15152 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15153 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15154 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15155 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15156 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15157 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15158 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
15159 
15160 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15161 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15162 	*dst_rmem->vmem = *src_rmem->vmem;
15163 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15164 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15165 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15166 	}
15167 
15168 	dst->rx_agg_bmap = src->rx_agg_bmap;
15169 }
15170 
15171 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx)
15172 {
15173 	struct bnxt *bp = netdev_priv(dev);
15174 	struct bnxt_rx_ring_info *rxr, *clone;
15175 	struct bnxt_cp_ring_info *cpr;
15176 	int rc;
15177 
15178 	rxr = &bp->rx_ring[idx];
15179 	clone = qmem;
15180 
15181 	rxr->rx_prod = clone->rx_prod;
15182 	rxr->rx_agg_prod = clone->rx_agg_prod;
15183 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
15184 	rxr->rx_next_cons = clone->rx_next_cons;
15185 	rxr->page_pool = clone->page_pool;
15186 
15187 	bnxt_copy_rx_ring(bp, rxr, clone);
15188 
15189 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
15190 	if (rc)
15191 		return rc;
15192 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
15193 	if (rc)
15194 		goto err_free_hwrm_rx_ring;
15195 
15196 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
15197 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15198 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
15199 
15200 	napi_enable(&rxr->bnapi->napi);
15201 
15202 	cpr = &rxr->bnapi->cp_ring;
15203 	cpr->sw_stats->rx.rx_resets++;
15204 
15205 	return 0;
15206 
15207 err_free_hwrm_rx_ring:
15208 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15209 	return rc;
15210 }
15211 
15212 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
15213 {
15214 	struct bnxt *bp = netdev_priv(dev);
15215 	struct bnxt_rx_ring_info *rxr;
15216 
15217 	rxr = &bp->rx_ring[idx];
15218 	napi_disable(&rxr->bnapi->napi);
15219 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15220 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
15221 	rxr->rx_next_cons = 0;
15222 	page_pool_disable_direct_recycling(rxr->page_pool);
15223 
15224 	memcpy(qmem, rxr, sizeof(*rxr));
15225 	bnxt_init_rx_ring_struct(bp, qmem);
15226 
15227 	return 0;
15228 }
15229 
15230 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
15231 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
15232 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
15233 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
15234 	.ndo_queue_start	= bnxt_queue_start,
15235 	.ndo_queue_stop		= bnxt_queue_stop,
15236 };
15237 
15238 static void bnxt_remove_one(struct pci_dev *pdev)
15239 {
15240 	struct net_device *dev = pci_get_drvdata(pdev);
15241 	struct bnxt *bp = netdev_priv(dev);
15242 
15243 	if (BNXT_PF(bp))
15244 		bnxt_sriov_disable(bp);
15245 
15246 	bnxt_rdma_aux_device_del(bp);
15247 
15248 	bnxt_ptp_clear(bp);
15249 	unregister_netdev(dev);
15250 
15251 	bnxt_rdma_aux_device_uninit(bp);
15252 
15253 	bnxt_free_l2_filters(bp, true);
15254 	bnxt_free_ntp_fltrs(bp, true);
15255 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
15256 		bnxt_clear_rss_ctxs(bp, true);
15257 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15258 	/* Flush any pending tasks */
15259 	cancel_work_sync(&bp->sp_task);
15260 	cancel_delayed_work_sync(&bp->fw_reset_task);
15261 	bp->sp_event = 0;
15262 
15263 	bnxt_dl_fw_reporters_destroy(bp);
15264 	bnxt_dl_unregister(bp);
15265 	bnxt_shutdown_tc(bp);
15266 
15267 	bnxt_clear_int_mode(bp);
15268 	bnxt_hwrm_func_drv_unrgtr(bp);
15269 	bnxt_free_hwrm_resources(bp);
15270 	bnxt_hwmon_uninit(bp);
15271 	bnxt_ethtool_free(bp);
15272 	bnxt_dcb_free(bp);
15273 	kfree(bp->ptp_cfg);
15274 	bp->ptp_cfg = NULL;
15275 	kfree(bp->fw_health);
15276 	bp->fw_health = NULL;
15277 	bnxt_cleanup_pci(bp);
15278 	bnxt_free_ctx_mem(bp);
15279 	kfree(bp->rss_indir_tbl);
15280 	bp->rss_indir_tbl = NULL;
15281 	bnxt_free_port_stats(bp);
15282 	free_netdev(dev);
15283 }
15284 
15285 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
15286 {
15287 	int rc = 0;
15288 	struct bnxt_link_info *link_info = &bp->link_info;
15289 
15290 	bp->phy_flags = 0;
15291 	rc = bnxt_hwrm_phy_qcaps(bp);
15292 	if (rc) {
15293 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
15294 			   rc);
15295 		return rc;
15296 	}
15297 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
15298 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
15299 	else
15300 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
15301 	if (!fw_dflt)
15302 		return 0;
15303 
15304 	mutex_lock(&bp->link_lock);
15305 	rc = bnxt_update_link(bp, false);
15306 	if (rc) {
15307 		mutex_unlock(&bp->link_lock);
15308 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
15309 			   rc);
15310 		return rc;
15311 	}
15312 
15313 	/* Older firmware does not have supported_auto_speeds, so assume
15314 	 * that all supported speeds can be autonegotiated.
15315 	 */
15316 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
15317 		link_info->support_auto_speeds = link_info->support_speeds;
15318 
15319 	bnxt_init_ethtool_link_settings(bp);
15320 	mutex_unlock(&bp->link_lock);
15321 	return 0;
15322 }
15323 
15324 static int bnxt_get_max_irq(struct pci_dev *pdev)
15325 {
15326 	u16 ctrl;
15327 
15328 	if (!pdev->msix_cap)
15329 		return 1;
15330 
15331 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
15332 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
15333 }
15334 
15335 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15336 				int *max_cp)
15337 {
15338 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
15339 	int max_ring_grps = 0, max_irq;
15340 
15341 	*max_tx = hw_resc->max_tx_rings;
15342 	*max_rx = hw_resc->max_rx_rings;
15343 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
15344 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
15345 			bnxt_get_ulp_msix_num_in_use(bp),
15346 			hw_resc->max_stat_ctxs -
15347 			bnxt_get_ulp_stat_ctxs_in_use(bp));
15348 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
15349 		*max_cp = min_t(int, *max_cp, max_irq);
15350 	max_ring_grps = hw_resc->max_hw_ring_grps;
15351 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
15352 		*max_cp -= 1;
15353 		*max_rx -= 2;
15354 	}
15355 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15356 		*max_rx >>= 1;
15357 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
15358 		int rc;
15359 
15360 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
15361 		if (rc) {
15362 			*max_rx = 0;
15363 			*max_tx = 0;
15364 		}
15365 		/* On P5 chips, max_cp output param should be available NQs */
15366 		*max_cp = max_irq;
15367 	}
15368 	*max_rx = min_t(int, *max_rx, max_ring_grps);
15369 }
15370 
15371 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
15372 {
15373 	int rx, tx, cp;
15374 
15375 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
15376 	*max_rx = rx;
15377 	*max_tx = tx;
15378 	if (!rx || !tx || !cp)
15379 		return -ENOMEM;
15380 
15381 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
15382 }
15383 
15384 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15385 			       bool shared)
15386 {
15387 	int rc;
15388 
15389 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
15390 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
15391 		/* Not enough rings, try disabling agg rings. */
15392 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
15393 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
15394 		if (rc) {
15395 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
15396 			bp->flags |= BNXT_FLAG_AGG_RINGS;
15397 			return rc;
15398 		}
15399 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
15400 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
15401 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
15402 		bnxt_set_ring_params(bp);
15403 	}
15404 
15405 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
15406 		int max_cp, max_stat, max_irq;
15407 
15408 		/* Reserve minimum resources for RoCE */
15409 		max_cp = bnxt_get_max_func_cp_rings(bp);
15410 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
15411 		max_irq = bnxt_get_max_func_irqs(bp);
15412 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
15413 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
15414 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
15415 			return 0;
15416 
15417 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
15418 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
15419 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
15420 		max_cp = min_t(int, max_cp, max_irq);
15421 		max_cp = min_t(int, max_cp, max_stat);
15422 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
15423 		if (rc)
15424 			rc = 0;
15425 	}
15426 	return rc;
15427 }
15428 
15429 /* In initial default shared ring setting, each shared ring must have a
15430  * RX/TX ring pair.
15431  */
15432 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
15433 {
15434 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
15435 	bp->rx_nr_rings = bp->cp_nr_rings;
15436 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
15437 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15438 }
15439 
15440 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
15441 {
15442 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
15443 	int avail_msix;
15444 
15445 	if (!bnxt_can_reserve_rings(bp))
15446 		return 0;
15447 
15448 	if (sh)
15449 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
15450 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
15451 	/* Reduce default rings on multi-port cards so that total default
15452 	 * rings do not exceed CPU count.
15453 	 */
15454 	if (bp->port_count > 1) {
15455 		int max_rings =
15456 			max_t(int, num_online_cpus() / bp->port_count, 1);
15457 
15458 		dflt_rings = min_t(int, dflt_rings, max_rings);
15459 	}
15460 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
15461 	if (rc)
15462 		return rc;
15463 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
15464 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
15465 	if (sh)
15466 		bnxt_trim_dflt_sh_rings(bp);
15467 	else
15468 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
15469 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15470 
15471 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
15472 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
15473 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
15474 
15475 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
15476 		bnxt_set_dflt_ulp_stat_ctxs(bp);
15477 	}
15478 
15479 	rc = __bnxt_reserve_rings(bp);
15480 	if (rc && rc != -ENODEV)
15481 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
15482 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15483 	if (sh)
15484 		bnxt_trim_dflt_sh_rings(bp);
15485 
15486 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
15487 	if (bnxt_need_reserve_rings(bp)) {
15488 		rc = __bnxt_reserve_rings(bp);
15489 		if (rc && rc != -ENODEV)
15490 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
15491 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15492 	}
15493 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
15494 		bp->rx_nr_rings++;
15495 		bp->cp_nr_rings++;
15496 	}
15497 	if (rc) {
15498 		bp->tx_nr_rings = 0;
15499 		bp->rx_nr_rings = 0;
15500 	}
15501 	return rc;
15502 }
15503 
15504 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
15505 {
15506 	int rc;
15507 
15508 	if (bp->tx_nr_rings)
15509 		return 0;
15510 
15511 	bnxt_ulp_irq_stop(bp);
15512 	bnxt_clear_int_mode(bp);
15513 	rc = bnxt_set_dflt_rings(bp, true);
15514 	if (rc) {
15515 		if (BNXT_VF(bp) && rc == -ENODEV)
15516 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
15517 		else
15518 			netdev_err(bp->dev, "Not enough rings available.\n");
15519 		goto init_dflt_ring_err;
15520 	}
15521 	rc = bnxt_init_int_mode(bp);
15522 	if (rc)
15523 		goto init_dflt_ring_err;
15524 
15525 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15526 
15527 	bnxt_set_dflt_rfs(bp);
15528 
15529 init_dflt_ring_err:
15530 	bnxt_ulp_irq_restart(bp, rc);
15531 	return rc;
15532 }
15533 
15534 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
15535 {
15536 	int rc;
15537 
15538 	ASSERT_RTNL();
15539 	bnxt_hwrm_func_qcaps(bp);
15540 
15541 	if (netif_running(bp->dev))
15542 		__bnxt_close_nic(bp, true, false);
15543 
15544 	bnxt_ulp_irq_stop(bp);
15545 	bnxt_clear_int_mode(bp);
15546 	rc = bnxt_init_int_mode(bp);
15547 	bnxt_ulp_irq_restart(bp, rc);
15548 
15549 	if (netif_running(bp->dev)) {
15550 		if (rc)
15551 			dev_close(bp->dev);
15552 		else
15553 			rc = bnxt_open_nic(bp, true, false);
15554 	}
15555 
15556 	return rc;
15557 }
15558 
15559 static int bnxt_init_mac_addr(struct bnxt *bp)
15560 {
15561 	int rc = 0;
15562 
15563 	if (BNXT_PF(bp)) {
15564 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
15565 	} else {
15566 #ifdef CONFIG_BNXT_SRIOV
15567 		struct bnxt_vf_info *vf = &bp->vf;
15568 		bool strict_approval = true;
15569 
15570 		if (is_valid_ether_addr(vf->mac_addr)) {
15571 			/* overwrite netdev dev_addr with admin VF MAC */
15572 			eth_hw_addr_set(bp->dev, vf->mac_addr);
15573 			/* Older PF driver or firmware may not approve this
15574 			 * correctly.
15575 			 */
15576 			strict_approval = false;
15577 		} else {
15578 			eth_hw_addr_random(bp->dev);
15579 		}
15580 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
15581 #endif
15582 	}
15583 	return rc;
15584 }
15585 
15586 static void bnxt_vpd_read_info(struct bnxt *bp)
15587 {
15588 	struct pci_dev *pdev = bp->pdev;
15589 	unsigned int vpd_size, kw_len;
15590 	int pos, size;
15591 	u8 *vpd_data;
15592 
15593 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
15594 	if (IS_ERR(vpd_data)) {
15595 		pci_warn(pdev, "Unable to read VPD\n");
15596 		return;
15597 	}
15598 
15599 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
15600 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
15601 	if (pos < 0)
15602 		goto read_sn;
15603 
15604 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
15605 	memcpy(bp->board_partno, &vpd_data[pos], size);
15606 
15607 read_sn:
15608 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
15609 					   PCI_VPD_RO_KEYWORD_SERIALNO,
15610 					   &kw_len);
15611 	if (pos < 0)
15612 		goto exit;
15613 
15614 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
15615 	memcpy(bp->board_serialno, &vpd_data[pos], size);
15616 exit:
15617 	kfree(vpd_data);
15618 }
15619 
15620 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
15621 {
15622 	struct pci_dev *pdev = bp->pdev;
15623 	u64 qword;
15624 
15625 	qword = pci_get_dsn(pdev);
15626 	if (!qword) {
15627 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
15628 		return -EOPNOTSUPP;
15629 	}
15630 
15631 	put_unaligned_le64(qword, dsn);
15632 
15633 	bp->flags |= BNXT_FLAG_DSN_VALID;
15634 	return 0;
15635 }
15636 
15637 static int bnxt_map_db_bar(struct bnxt *bp)
15638 {
15639 	if (!bp->db_size)
15640 		return -ENODEV;
15641 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
15642 	if (!bp->bar1)
15643 		return -ENOMEM;
15644 	return 0;
15645 }
15646 
15647 void bnxt_print_device_info(struct bnxt *bp)
15648 {
15649 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
15650 		    board_info[bp->board_idx].name,
15651 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
15652 
15653 	pcie_print_link_status(bp->pdev);
15654 }
15655 
15656 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
15657 {
15658 	struct bnxt_hw_resc *hw_resc;
15659 	struct net_device *dev;
15660 	struct bnxt *bp;
15661 	int rc, max_irqs;
15662 
15663 	if (pci_is_bridge(pdev))
15664 		return -ENODEV;
15665 
15666 	/* Clear any pending DMA transactions from crash kernel
15667 	 * while loading driver in capture kernel.
15668 	 */
15669 	if (is_kdump_kernel()) {
15670 		pci_clear_master(pdev);
15671 		pcie_flr(pdev);
15672 	}
15673 
15674 	max_irqs = bnxt_get_max_irq(pdev);
15675 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
15676 				 max_irqs);
15677 	if (!dev)
15678 		return -ENOMEM;
15679 
15680 	bp = netdev_priv(dev);
15681 	bp->board_idx = ent->driver_data;
15682 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
15683 	bnxt_set_max_func_irqs(bp, max_irqs);
15684 
15685 	if (bnxt_vf_pciid(bp->board_idx))
15686 		bp->flags |= BNXT_FLAG_VF;
15687 
15688 	/* No devlink port registration in case of a VF */
15689 	if (BNXT_PF(bp))
15690 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
15691 
15692 	if (pdev->msix_cap)
15693 		bp->flags |= BNXT_FLAG_MSIX_CAP;
15694 
15695 	rc = bnxt_init_board(pdev, dev);
15696 	if (rc < 0)
15697 		goto init_err_free;
15698 
15699 	dev->netdev_ops = &bnxt_netdev_ops;
15700 	dev->stat_ops = &bnxt_stat_ops;
15701 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
15702 	dev->ethtool_ops = &bnxt_ethtool_ops;
15703 	dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
15704 	pci_set_drvdata(pdev, dev);
15705 
15706 	rc = bnxt_alloc_hwrm_resources(bp);
15707 	if (rc)
15708 		goto init_err_pci_clean;
15709 
15710 	mutex_init(&bp->hwrm_cmd_lock);
15711 	mutex_init(&bp->link_lock);
15712 
15713 	rc = bnxt_fw_init_one_p1(bp);
15714 	if (rc)
15715 		goto init_err_pci_clean;
15716 
15717 	if (BNXT_PF(bp))
15718 		bnxt_vpd_read_info(bp);
15719 
15720 	if (BNXT_CHIP_P5_PLUS(bp)) {
15721 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
15722 		if (BNXT_CHIP_P7(bp))
15723 			bp->flags |= BNXT_FLAG_CHIP_P7;
15724 	}
15725 
15726 	rc = bnxt_alloc_rss_indir_tbl(bp, NULL);
15727 	if (rc)
15728 		goto init_err_pci_clean;
15729 
15730 	rc = bnxt_fw_init_one_p2(bp);
15731 	if (rc)
15732 		goto init_err_pci_clean;
15733 
15734 	rc = bnxt_map_db_bar(bp);
15735 	if (rc) {
15736 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
15737 			rc);
15738 		goto init_err_pci_clean;
15739 	}
15740 
15741 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
15742 			   NETIF_F_TSO | NETIF_F_TSO6 |
15743 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
15744 			   NETIF_F_GSO_IPXIP4 |
15745 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
15746 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
15747 			   NETIF_F_RXCSUM | NETIF_F_GRO;
15748 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
15749 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
15750 
15751 	if (BNXT_SUPPORTS_TPA(bp))
15752 		dev->hw_features |= NETIF_F_LRO;
15753 
15754 	dev->hw_enc_features =
15755 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
15756 			NETIF_F_TSO | NETIF_F_TSO6 |
15757 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
15758 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
15759 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
15760 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
15761 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
15762 	if (bp->flags & BNXT_FLAG_CHIP_P7)
15763 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
15764 	else
15765 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
15766 
15767 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
15768 				    NETIF_F_GSO_GRE_CSUM;
15769 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
15770 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
15771 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
15772 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
15773 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
15774 	if (BNXT_SUPPORTS_TPA(bp))
15775 		dev->hw_features |= NETIF_F_GRO_HW;
15776 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
15777 	if (dev->features & NETIF_F_GRO_HW)
15778 		dev->features &= ~NETIF_F_LRO;
15779 	dev->priv_flags |= IFF_UNICAST_FLT;
15780 
15781 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
15782 	if (bp->tso_max_segs)
15783 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
15784 
15785 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
15786 			    NETDEV_XDP_ACT_RX_SG;
15787 
15788 #ifdef CONFIG_BNXT_SRIOV
15789 	init_waitqueue_head(&bp->sriov_cfg_wait);
15790 #endif
15791 	if (BNXT_SUPPORTS_TPA(bp)) {
15792 		bp->gro_func = bnxt_gro_func_5730x;
15793 		if (BNXT_CHIP_P4(bp))
15794 			bp->gro_func = bnxt_gro_func_5731x;
15795 		else if (BNXT_CHIP_P5_PLUS(bp))
15796 			bp->gro_func = bnxt_gro_func_5750x;
15797 	}
15798 	if (!BNXT_CHIP_P4_PLUS(bp))
15799 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
15800 
15801 	rc = bnxt_init_mac_addr(bp);
15802 	if (rc) {
15803 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
15804 		rc = -EADDRNOTAVAIL;
15805 		goto init_err_pci_clean;
15806 	}
15807 
15808 	if (BNXT_PF(bp)) {
15809 		/* Read the adapter's DSN to use as the eswitch switch_id */
15810 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
15811 	}
15812 
15813 	/* MTU range: 60 - FW defined max */
15814 	dev->min_mtu = ETH_ZLEN;
15815 	dev->max_mtu = bp->max_mtu;
15816 
15817 	rc = bnxt_probe_phy(bp, true);
15818 	if (rc)
15819 		goto init_err_pci_clean;
15820 
15821 	hw_resc = &bp->hw_resc;
15822 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
15823 		       BNXT_L2_FLTR_MAX_FLTR;
15824 	/* Older firmware may not report these filters properly */
15825 	if (bp->max_fltr < BNXT_MAX_FLTR)
15826 		bp->max_fltr = BNXT_MAX_FLTR;
15827 	bnxt_init_l2_fltr_tbl(bp);
15828 	bnxt_set_rx_skb_mode(bp, false);
15829 	bnxt_set_tpa_flags(bp);
15830 	bnxt_set_ring_params(bp);
15831 	bnxt_rdma_aux_device_init(bp);
15832 	rc = bnxt_set_dflt_rings(bp, true);
15833 	if (rc) {
15834 		if (BNXT_VF(bp) && rc == -ENODEV) {
15835 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
15836 		} else {
15837 			netdev_err(bp->dev, "Not enough rings available.\n");
15838 			rc = -ENOMEM;
15839 		}
15840 		goto init_err_pci_clean;
15841 	}
15842 
15843 	bnxt_fw_init_one_p3(bp);
15844 
15845 	bnxt_init_dflt_coal(bp);
15846 
15847 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
15848 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
15849 
15850 	rc = bnxt_init_int_mode(bp);
15851 	if (rc)
15852 		goto init_err_pci_clean;
15853 
15854 	/* No TC has been set yet and rings may have been trimmed due to
15855 	 * limited MSIX, so we re-initialize the TX rings per TC.
15856 	 */
15857 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15858 
15859 	if (BNXT_PF(bp)) {
15860 		if (!bnxt_pf_wq) {
15861 			bnxt_pf_wq =
15862 				create_singlethread_workqueue("bnxt_pf_wq");
15863 			if (!bnxt_pf_wq) {
15864 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
15865 				rc = -ENOMEM;
15866 				goto init_err_pci_clean;
15867 			}
15868 		}
15869 		rc = bnxt_init_tc(bp);
15870 		if (rc)
15871 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
15872 				   rc);
15873 	}
15874 
15875 	bnxt_inv_fw_health_reg(bp);
15876 	rc = bnxt_dl_register(bp);
15877 	if (rc)
15878 		goto init_err_dl;
15879 
15880 	INIT_LIST_HEAD(&bp->usr_fltr_list);
15881 
15882 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
15883 		bnxt_init_multi_rss_ctx(bp);
15884 
15885 
15886 	rc = register_netdev(dev);
15887 	if (rc)
15888 		goto init_err_cleanup;
15889 
15890 	bnxt_dl_fw_reporters_create(bp);
15891 
15892 	bnxt_rdma_aux_device_add(bp);
15893 
15894 	bnxt_print_device_info(bp);
15895 
15896 	pci_save_state(pdev);
15897 
15898 	return 0;
15899 init_err_cleanup:
15900 	bnxt_rdma_aux_device_uninit(bp);
15901 	bnxt_dl_unregister(bp);
15902 init_err_dl:
15903 	bnxt_shutdown_tc(bp);
15904 	bnxt_clear_int_mode(bp);
15905 
15906 init_err_pci_clean:
15907 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
15908 		bnxt_clear_rss_ctxs(bp, true);
15909 	bnxt_hwrm_func_drv_unrgtr(bp);
15910 	bnxt_free_hwrm_resources(bp);
15911 	bnxt_hwmon_uninit(bp);
15912 	bnxt_ethtool_free(bp);
15913 	bnxt_ptp_clear(bp);
15914 	kfree(bp->ptp_cfg);
15915 	bp->ptp_cfg = NULL;
15916 	kfree(bp->fw_health);
15917 	bp->fw_health = NULL;
15918 	bnxt_cleanup_pci(bp);
15919 	bnxt_free_ctx_mem(bp);
15920 	kfree(bp->rss_indir_tbl);
15921 	bp->rss_indir_tbl = NULL;
15922 
15923 init_err_free:
15924 	free_netdev(dev);
15925 	return rc;
15926 }
15927 
15928 static void bnxt_shutdown(struct pci_dev *pdev)
15929 {
15930 	struct net_device *dev = pci_get_drvdata(pdev);
15931 	struct bnxt *bp;
15932 
15933 	if (!dev)
15934 		return;
15935 
15936 	rtnl_lock();
15937 	bp = netdev_priv(dev);
15938 	if (!bp)
15939 		goto shutdown_exit;
15940 
15941 	if (netif_running(dev))
15942 		dev_close(dev);
15943 
15944 	bnxt_clear_int_mode(bp);
15945 	pci_disable_device(pdev);
15946 
15947 	if (system_state == SYSTEM_POWER_OFF) {
15948 		pci_wake_from_d3(pdev, bp->wol);
15949 		pci_set_power_state(pdev, PCI_D3hot);
15950 	}
15951 
15952 shutdown_exit:
15953 	rtnl_unlock();
15954 }
15955 
15956 #ifdef CONFIG_PM_SLEEP
15957 static int bnxt_suspend(struct device *device)
15958 {
15959 	struct net_device *dev = dev_get_drvdata(device);
15960 	struct bnxt *bp = netdev_priv(dev);
15961 	int rc = 0;
15962 
15963 	bnxt_ulp_stop(bp);
15964 
15965 	rtnl_lock();
15966 	if (netif_running(dev)) {
15967 		netif_device_detach(dev);
15968 		rc = bnxt_close(dev);
15969 	}
15970 	bnxt_hwrm_func_drv_unrgtr(bp);
15971 	pci_disable_device(bp->pdev);
15972 	bnxt_free_ctx_mem(bp);
15973 	rtnl_unlock();
15974 	return rc;
15975 }
15976 
15977 static int bnxt_resume(struct device *device)
15978 {
15979 	struct net_device *dev = dev_get_drvdata(device);
15980 	struct bnxt *bp = netdev_priv(dev);
15981 	int rc = 0;
15982 
15983 	rtnl_lock();
15984 	rc = pci_enable_device(bp->pdev);
15985 	if (rc) {
15986 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
15987 			   rc);
15988 		goto resume_exit;
15989 	}
15990 	pci_set_master(bp->pdev);
15991 	if (bnxt_hwrm_ver_get(bp)) {
15992 		rc = -ENODEV;
15993 		goto resume_exit;
15994 	}
15995 	rc = bnxt_hwrm_func_reset(bp);
15996 	if (rc) {
15997 		rc = -EBUSY;
15998 		goto resume_exit;
15999 	}
16000 
16001 	rc = bnxt_hwrm_func_qcaps(bp);
16002 	if (rc)
16003 		goto resume_exit;
16004 
16005 	bnxt_clear_reservations(bp, true);
16006 
16007 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
16008 		rc = -ENODEV;
16009 		goto resume_exit;
16010 	}
16011 
16012 	bnxt_get_wol_settings(bp);
16013 	if (netif_running(dev)) {
16014 		rc = bnxt_open(dev);
16015 		if (!rc)
16016 			netif_device_attach(dev);
16017 	}
16018 
16019 resume_exit:
16020 	rtnl_unlock();
16021 	bnxt_ulp_start(bp, rc);
16022 	if (!rc)
16023 		bnxt_reenable_sriov(bp);
16024 	return rc;
16025 }
16026 
16027 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
16028 #define BNXT_PM_OPS (&bnxt_pm_ops)
16029 
16030 #else
16031 
16032 #define BNXT_PM_OPS NULL
16033 
16034 #endif /* CONFIG_PM_SLEEP */
16035 
16036 /**
16037  * bnxt_io_error_detected - called when PCI error is detected
16038  * @pdev: Pointer to PCI device
16039  * @state: The current pci connection state
16040  *
16041  * This function is called after a PCI bus error affecting
16042  * this device has been detected.
16043  */
16044 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
16045 					       pci_channel_state_t state)
16046 {
16047 	struct net_device *netdev = pci_get_drvdata(pdev);
16048 	struct bnxt *bp = netdev_priv(netdev);
16049 	bool abort = false;
16050 
16051 	netdev_info(netdev, "PCI I/O error detected\n");
16052 
16053 	bnxt_ulp_stop(bp);
16054 
16055 	rtnl_lock();
16056 	netif_device_detach(netdev);
16057 
16058 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
16059 		netdev_err(bp->dev, "Firmware reset already in progress\n");
16060 		abort = true;
16061 	}
16062 
16063 	if (abort || state == pci_channel_io_perm_failure) {
16064 		rtnl_unlock();
16065 		return PCI_ERS_RESULT_DISCONNECT;
16066 	}
16067 
16068 	/* Link is not reliable anymore if state is pci_channel_io_frozen
16069 	 * so we disable bus master to prevent any potential bad DMAs before
16070 	 * freeing kernel memory.
16071 	 */
16072 	if (state == pci_channel_io_frozen) {
16073 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
16074 		bnxt_fw_fatal_close(bp);
16075 	}
16076 
16077 	if (netif_running(netdev))
16078 		__bnxt_close_nic(bp, true, true);
16079 
16080 	if (pci_is_enabled(pdev))
16081 		pci_disable_device(pdev);
16082 	bnxt_free_ctx_mem(bp);
16083 	rtnl_unlock();
16084 
16085 	/* Request a slot slot reset. */
16086 	return PCI_ERS_RESULT_NEED_RESET;
16087 }
16088 
16089 /**
16090  * bnxt_io_slot_reset - called after the pci bus has been reset.
16091  * @pdev: Pointer to PCI device
16092  *
16093  * Restart the card from scratch, as if from a cold-boot.
16094  * At this point, the card has exprienced a hard reset,
16095  * followed by fixups by BIOS, and has its config space
16096  * set up identically to what it was at cold boot.
16097  */
16098 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
16099 {
16100 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
16101 	struct net_device *netdev = pci_get_drvdata(pdev);
16102 	struct bnxt *bp = netdev_priv(netdev);
16103 	int retry = 0;
16104 	int err = 0;
16105 	int off;
16106 
16107 	netdev_info(bp->dev, "PCI Slot Reset\n");
16108 
16109 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16110 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
16111 		msleep(900);
16112 
16113 	rtnl_lock();
16114 
16115 	if (pci_enable_device(pdev)) {
16116 		dev_err(&pdev->dev,
16117 			"Cannot re-enable PCI device after reset.\n");
16118 	} else {
16119 		pci_set_master(pdev);
16120 		/* Upon fatal error, our device internal logic that latches to
16121 		 * BAR value is getting reset and will restore only upon
16122 		 * rewritting the BARs.
16123 		 *
16124 		 * As pci_restore_state() does not re-write the BARs if the
16125 		 * value is same as saved value earlier, driver needs to
16126 		 * write the BARs to 0 to force restore, in case of fatal error.
16127 		 */
16128 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
16129 				       &bp->state)) {
16130 			for (off = PCI_BASE_ADDRESS_0;
16131 			     off <= PCI_BASE_ADDRESS_5; off += 4)
16132 				pci_write_config_dword(bp->pdev, off, 0);
16133 		}
16134 		pci_restore_state(pdev);
16135 		pci_save_state(pdev);
16136 
16137 		bnxt_inv_fw_health_reg(bp);
16138 		bnxt_try_map_fw_health_reg(bp);
16139 
16140 		/* In some PCIe AER scenarios, firmware may take up to
16141 		 * 10 seconds to become ready in the worst case.
16142 		 */
16143 		do {
16144 			err = bnxt_try_recover_fw(bp);
16145 			if (!err)
16146 				break;
16147 			retry++;
16148 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
16149 
16150 		if (err) {
16151 			dev_err(&pdev->dev, "Firmware not ready\n");
16152 			goto reset_exit;
16153 		}
16154 
16155 		err = bnxt_hwrm_func_reset(bp);
16156 		if (!err)
16157 			result = PCI_ERS_RESULT_RECOVERED;
16158 
16159 		bnxt_ulp_irq_stop(bp);
16160 		bnxt_clear_int_mode(bp);
16161 		err = bnxt_init_int_mode(bp);
16162 		bnxt_ulp_irq_restart(bp, err);
16163 	}
16164 
16165 reset_exit:
16166 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16167 	bnxt_clear_reservations(bp, true);
16168 	rtnl_unlock();
16169 
16170 	return result;
16171 }
16172 
16173 /**
16174  * bnxt_io_resume - called when traffic can start flowing again.
16175  * @pdev: Pointer to PCI device
16176  *
16177  * This callback is called when the error recovery driver tells
16178  * us that its OK to resume normal operation.
16179  */
16180 static void bnxt_io_resume(struct pci_dev *pdev)
16181 {
16182 	struct net_device *netdev = pci_get_drvdata(pdev);
16183 	struct bnxt *bp = netdev_priv(netdev);
16184 	int err;
16185 
16186 	netdev_info(bp->dev, "PCI Slot Resume\n");
16187 	rtnl_lock();
16188 
16189 	err = bnxt_hwrm_func_qcaps(bp);
16190 	if (!err && netif_running(netdev))
16191 		err = bnxt_open(netdev);
16192 
16193 	if (!err)
16194 		netif_device_attach(netdev);
16195 
16196 	rtnl_unlock();
16197 	bnxt_ulp_start(bp, err);
16198 	if (!err)
16199 		bnxt_reenable_sriov(bp);
16200 }
16201 
16202 static const struct pci_error_handlers bnxt_err_handler = {
16203 	.error_detected	= bnxt_io_error_detected,
16204 	.slot_reset	= bnxt_io_slot_reset,
16205 	.resume		= bnxt_io_resume
16206 };
16207 
16208 static struct pci_driver bnxt_pci_driver = {
16209 	.name		= DRV_MODULE_NAME,
16210 	.id_table	= bnxt_pci_tbl,
16211 	.probe		= bnxt_init_one,
16212 	.remove		= bnxt_remove_one,
16213 	.shutdown	= bnxt_shutdown,
16214 	.driver.pm	= BNXT_PM_OPS,
16215 	.err_handler	= &bnxt_err_handler,
16216 #if defined(CONFIG_BNXT_SRIOV)
16217 	.sriov_configure = bnxt_sriov_configure,
16218 #endif
16219 };
16220 
16221 static int __init bnxt_init(void)
16222 {
16223 	int err;
16224 
16225 	bnxt_debug_init();
16226 	err = pci_register_driver(&bnxt_pci_driver);
16227 	if (err) {
16228 		bnxt_debug_exit();
16229 		return err;
16230 	}
16231 
16232 	return 0;
16233 }
16234 
16235 static void __exit bnxt_exit(void)
16236 {
16237 	pci_unregister_driver(&bnxt_pci_driver);
16238 	if (bnxt_pf_wq)
16239 		destroy_workqueue(bnxt_pf_wq);
16240 	bnxt_debug_exit();
16241 }
16242 
16243 module_init(bnxt_init);
16244 module_exit(bnxt_exit);
16245