xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision 2696e451dfb07f92d0e995ef456bd9110a48806a)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  * Copyright (c) 2016-2019 Broadcom Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  */
10 
11 #include <linux/module.h>
12 
13 #include <linux/stringify.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/pci.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/bitops.h>
27 #include <linux/io.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/byteorder.h>
31 #include <asm/page.h>
32 #include <linux/time.h>
33 #include <linux/mii.h>
34 #include <linux/mdio.h>
35 #include <linux/if.h>
36 #include <linux/if_vlan.h>
37 #include <linux/if_bridge.h>
38 #include <linux/rtc.h>
39 #include <linux/bpf.h>
40 #include <net/gro.h>
41 #include <net/ip.h>
42 #include <net/tcp.h>
43 #include <net/udp.h>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
46 #include <net/udp_tunnel.h>
47 #include <linux/workqueue.h>
48 #include <linux/prefetch.h>
49 #include <linux/cache.h>
50 #include <linux/log2.h>
51 #include <linux/bitmap.h>
52 #include <linux/cpu_rmap.h>
53 #include <linux/cpumask.h>
54 #include <net/pkt_cls.h>
55 #include <net/page_pool/helpers.h>
56 #include <linux/align.h>
57 #include <net/netdev_queues.h>
58 
59 #include "bnxt_hsi.h"
60 #include "bnxt.h"
61 #include "bnxt_hwrm.h"
62 #include "bnxt_ulp.h"
63 #include "bnxt_sriov.h"
64 #include "bnxt_ethtool.h"
65 #include "bnxt_dcb.h"
66 #include "bnxt_xdp.h"
67 #include "bnxt_ptp.h"
68 #include "bnxt_vfr.h"
69 #include "bnxt_tc.h"
70 #include "bnxt_devlink.h"
71 #include "bnxt_debugfs.h"
72 #include "bnxt_coredump.h"
73 #include "bnxt_hwmon.h"
74 
75 #define BNXT_TX_TIMEOUT		(5 * HZ)
76 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
77 				 NETIF_MSG_TX_ERR)
78 
79 MODULE_LICENSE("GPL");
80 MODULE_DESCRIPTION("Broadcom NetXtreme network driver");
81 
82 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
83 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
84 #define BNXT_RX_COPY_THRESH 256
85 
86 #define BNXT_TX_PUSH_THRESH 164
87 
88 /* indexed by enum board_idx */
89 static const struct {
90 	char *name;
91 } board_info[] = {
92 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
93 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
94 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
95 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
96 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
97 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
98 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
99 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
100 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
101 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
102 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
103 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
104 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
105 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
106 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
108 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
109 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
110 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
111 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
112 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
113 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
114 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
115 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
116 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
117 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
118 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
119 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
120 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
121 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
122 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
123 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
124 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
125 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
126 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
127 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
128 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
129 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
130 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
131 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
132 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
133 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
134 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
135 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
136 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
137 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
138 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
139 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
140 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
141 	[NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" },
142 };
143 
144 static const struct pci_device_id bnxt_pci_tbl[] = {
145 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
146 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
147 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
148 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
149 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
150 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
151 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
152 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
153 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
155 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
159 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
160 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
164 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
165 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
166 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
167 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
168 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
169 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
170 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
171 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
172 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
173 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
174 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
175 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
176 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
178 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
179 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
180 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
181 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
182 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
183 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
184 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
185 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
187 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
188 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
189 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
190 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
191 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
192 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
193 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
194 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
195 #ifdef CONFIG_BNXT_SRIOV
196 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
197 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
198 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
199 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
200 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
201 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
202 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
203 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
204 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
205 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
206 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
207 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
208 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
209 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
210 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
211 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
212 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
214 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
215 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
216 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
217 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
218 #endif
219 	{ 0 }
220 };
221 
222 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
223 
224 static const u16 bnxt_vf_req_snif[] = {
225 	HWRM_FUNC_CFG,
226 	HWRM_FUNC_VF_CFG,
227 	HWRM_PORT_PHY_QCFG,
228 	HWRM_CFA_L2_FILTER_ALLOC,
229 };
230 
231 static const u16 bnxt_async_events_arr[] = {
232 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
233 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
234 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
235 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
236 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
237 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
238 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
239 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
240 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
241 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
242 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
243 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
244 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
245 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
246 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
247 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
248 };
249 
250 static struct workqueue_struct *bnxt_pf_wq;
251 
252 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
253 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
254 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
255 
256 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
257 	.ports = {
258 		.src = 0,
259 		.dst = 0,
260 	},
261 	.addrs = {
262 		.v6addrs = {
263 			.src = BNXT_IPV6_MASK_NONE,
264 			.dst = BNXT_IPV6_MASK_NONE,
265 		},
266 	},
267 };
268 
269 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
270 	.ports = {
271 		.src = cpu_to_be16(0xffff),
272 		.dst = cpu_to_be16(0xffff),
273 	},
274 	.addrs = {
275 		.v6addrs = {
276 			.src = BNXT_IPV6_MASK_ALL,
277 			.dst = BNXT_IPV6_MASK_ALL,
278 		},
279 	},
280 };
281 
282 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
283 	.ports = {
284 		.src = cpu_to_be16(0xffff),
285 		.dst = cpu_to_be16(0xffff),
286 	},
287 	.addrs = {
288 		.v4addrs = {
289 			.src = cpu_to_be32(0xffffffff),
290 			.dst = cpu_to_be32(0xffffffff),
291 		},
292 	},
293 };
294 
295 static bool bnxt_vf_pciid(enum board_idx idx)
296 {
297 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
298 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
299 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
300 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF);
301 }
302 
303 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
304 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
305 
306 #define BNXT_DB_CQ(db, idx)						\
307 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
308 
309 #define BNXT_DB_NQ_P5(db, idx)						\
310 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
311 		    (db)->doorbell)
312 
313 #define BNXT_DB_NQ_P7(db, idx)						\
314 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
315 		    DB_RING_IDX(db, idx), (db)->doorbell)
316 
317 #define BNXT_DB_CQ_ARM(db, idx)						\
318 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
319 
320 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
321 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
322 		    DB_RING_IDX(db, idx), (db)->doorbell)
323 
324 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
325 {
326 	if (bp->flags & BNXT_FLAG_CHIP_P7)
327 		BNXT_DB_NQ_P7(db, idx);
328 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
329 		BNXT_DB_NQ_P5(db, idx);
330 	else
331 		BNXT_DB_CQ(db, idx);
332 }
333 
334 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
335 {
336 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
337 		BNXT_DB_NQ_ARM_P5(db, idx);
338 	else
339 		BNXT_DB_CQ_ARM(db, idx);
340 }
341 
342 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
343 {
344 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
345 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
346 			    DB_RING_IDX(db, idx), db->doorbell);
347 	else
348 		BNXT_DB_CQ(db, idx);
349 }
350 
351 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
352 {
353 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
354 		return;
355 
356 	if (BNXT_PF(bp))
357 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
358 	else
359 		schedule_delayed_work(&bp->fw_reset_task, delay);
360 }
361 
362 static void __bnxt_queue_sp_work(struct bnxt *bp)
363 {
364 	if (BNXT_PF(bp))
365 		queue_work(bnxt_pf_wq, &bp->sp_task);
366 	else
367 		schedule_work(&bp->sp_task);
368 }
369 
370 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
371 {
372 	set_bit(event, &bp->sp_event);
373 	__bnxt_queue_sp_work(bp);
374 }
375 
376 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
377 {
378 	if (!rxr->bnapi->in_reset) {
379 		rxr->bnapi->in_reset = true;
380 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
381 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
382 		else
383 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
384 		__bnxt_queue_sp_work(bp);
385 	}
386 	rxr->rx_next_cons = 0xffff;
387 }
388 
389 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
390 			  u16 curr)
391 {
392 	struct bnxt_napi *bnapi = txr->bnapi;
393 
394 	if (bnapi->tx_fault)
395 		return;
396 
397 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
398 		   txr->txq_index, txr->tx_hw_cons,
399 		   txr->tx_cons, txr->tx_prod, curr);
400 	WARN_ON_ONCE(1);
401 	bnapi->tx_fault = 1;
402 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
403 }
404 
405 const u16 bnxt_lhint_arr[] = {
406 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
407 	TX_BD_FLAGS_LHINT_512_TO_1023,
408 	TX_BD_FLAGS_LHINT_1024_TO_2047,
409 	TX_BD_FLAGS_LHINT_1024_TO_2047,
410 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
411 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
412 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
413 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
414 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
415 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
416 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
417 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
418 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
419 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
420 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
421 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
422 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
423 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
424 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
425 };
426 
427 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
428 {
429 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
430 
431 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
432 		return 0;
433 
434 	return md_dst->u.port_info.port_id;
435 }
436 
437 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
438 			     u16 prod)
439 {
440 	/* Sync BD data before updating doorbell */
441 	wmb();
442 	bnxt_db_write(bp, &txr->tx_db, prod);
443 	txr->kick_pending = 0;
444 }
445 
446 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
447 {
448 	struct bnxt *bp = netdev_priv(dev);
449 	struct tx_bd *txbd, *txbd0;
450 	struct tx_bd_ext *txbd1;
451 	struct netdev_queue *txq;
452 	int i;
453 	dma_addr_t mapping;
454 	unsigned int length, pad = 0;
455 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
456 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
457 	struct pci_dev *pdev = bp->pdev;
458 	u16 prod, last_frag, txts_prod;
459 	struct bnxt_tx_ring_info *txr;
460 	struct bnxt_sw_tx_bd *tx_buf;
461 	__le32 lflags = 0;
462 
463 	i = skb_get_queue_mapping(skb);
464 	if (unlikely(i >= bp->tx_nr_rings)) {
465 		dev_kfree_skb_any(skb);
466 		dev_core_stats_tx_dropped_inc(dev);
467 		return NETDEV_TX_OK;
468 	}
469 
470 	txq = netdev_get_tx_queue(dev, i);
471 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
472 	prod = txr->tx_prod;
473 
474 	free_size = bnxt_tx_avail(bp, txr);
475 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
476 		/* We must have raced with NAPI cleanup */
477 		if (net_ratelimit() && txr->kick_pending)
478 			netif_warn(bp, tx_err, dev,
479 				   "bnxt: ring busy w/ flush pending!\n");
480 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
481 					bp->tx_wake_thresh))
482 			return NETDEV_TX_BUSY;
483 	}
484 
485 	if (unlikely(ipv6_hopopt_jumbo_remove(skb)))
486 		goto tx_free;
487 
488 	length = skb->len;
489 	len = skb_headlen(skb);
490 	last_frag = skb_shinfo(skb)->nr_frags;
491 
492 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
493 
494 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
495 	tx_buf->skb = skb;
496 	tx_buf->nr_frags = last_frag;
497 
498 	vlan_tag_flags = 0;
499 	cfa_action = bnxt_xmit_get_cfa_action(skb);
500 	if (skb_vlan_tag_present(skb)) {
501 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
502 				 skb_vlan_tag_get(skb);
503 		/* Currently supports 8021Q, 8021AD vlan offloads
504 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
505 		 */
506 		if (skb->vlan_proto == htons(ETH_P_8021Q))
507 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
508 	}
509 
510 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp &&
511 	    ptp->tx_tstamp_en) {
512 		if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) {
513 			lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
514 			tx_buf->is_ts_pkt = 1;
515 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
516 		} else if (!skb_is_gso(skb)) {
517 			u16 seq_id, hdr_off;
518 
519 			if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) &&
520 			    !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) {
521 				if (vlan_tag_flags)
522 					hdr_off += VLAN_HLEN;
523 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
524 				tx_buf->is_ts_pkt = 1;
525 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
526 
527 				ptp->txts_req[txts_prod].tx_seqid = seq_id;
528 				ptp->txts_req[txts_prod].tx_hdr_off = hdr_off;
529 				tx_buf->txts_prod = txts_prod;
530 			}
531 		}
532 	}
533 	if (unlikely(skb->no_fcs))
534 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
535 
536 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
537 	    !lflags) {
538 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
539 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
540 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
541 		void __iomem *db = txr->tx_db.doorbell;
542 		void *pdata = tx_push_buf->data;
543 		u64 *end;
544 		int j, push_len;
545 
546 		/* Set COAL_NOW to be ready quickly for the next push */
547 		tx_push->tx_bd_len_flags_type =
548 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
549 					TX_BD_TYPE_LONG_TX_BD |
550 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
551 					TX_BD_FLAGS_COAL_NOW |
552 					TX_BD_FLAGS_PACKET_END |
553 					(2 << TX_BD_FLAGS_BD_CNT_SHIFT));
554 
555 		if (skb->ip_summed == CHECKSUM_PARTIAL)
556 			tx_push1->tx_bd_hsize_lflags =
557 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
558 		else
559 			tx_push1->tx_bd_hsize_lflags = 0;
560 
561 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
562 		tx_push1->tx_bd_cfa_action =
563 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
564 
565 		end = pdata + length;
566 		end = PTR_ALIGN(end, 8) - 1;
567 		*end = 0;
568 
569 		skb_copy_from_linear_data(skb, pdata, len);
570 		pdata += len;
571 		for (j = 0; j < last_frag; j++) {
572 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
573 			void *fptr;
574 
575 			fptr = skb_frag_address_safe(frag);
576 			if (!fptr)
577 				goto normal_tx;
578 
579 			memcpy(pdata, fptr, skb_frag_size(frag));
580 			pdata += skb_frag_size(frag);
581 		}
582 
583 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
584 		txbd->tx_bd_haddr = txr->data_mapping;
585 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
586 		prod = NEXT_TX(prod);
587 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
588 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
589 		memcpy(txbd, tx_push1, sizeof(*txbd));
590 		prod = NEXT_TX(prod);
591 		tx_push->doorbell =
592 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
593 				    DB_RING_IDX(&txr->tx_db, prod));
594 		WRITE_ONCE(txr->tx_prod, prod);
595 
596 		tx_buf->is_push = 1;
597 		netdev_tx_sent_queue(txq, skb->len);
598 		wmb();	/* Sync is_push and byte queue before pushing data */
599 
600 		push_len = (length + sizeof(*tx_push) + 7) / 8;
601 		if (push_len > 16) {
602 			__iowrite64_copy(db, tx_push_buf, 16);
603 			__iowrite32_copy(db + 4, tx_push_buf + 1,
604 					 (push_len - 16) << 1);
605 		} else {
606 			__iowrite64_copy(db, tx_push_buf, push_len);
607 		}
608 
609 		goto tx_done;
610 	}
611 
612 normal_tx:
613 	if (length < BNXT_MIN_PKT_SIZE) {
614 		pad = BNXT_MIN_PKT_SIZE - length;
615 		if (skb_pad(skb, pad))
616 			/* SKB already freed. */
617 			goto tx_kick_pending;
618 		length = BNXT_MIN_PKT_SIZE;
619 	}
620 
621 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
622 
623 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
624 		goto tx_free;
625 
626 	dma_unmap_addr_set(tx_buf, mapping, mapping);
627 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
628 		((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
629 
630 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
631 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
632 
633 	prod = NEXT_TX(prod);
634 	txbd1 = (struct tx_bd_ext *)
635 		&txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
636 
637 	txbd1->tx_bd_hsize_lflags = lflags;
638 	if (skb_is_gso(skb)) {
639 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
640 		u32 hdr_len;
641 
642 		if (skb->encapsulation) {
643 			if (udp_gso)
644 				hdr_len = skb_inner_transport_offset(skb) +
645 					  sizeof(struct udphdr);
646 			else
647 				hdr_len = skb_inner_tcp_all_headers(skb);
648 		} else if (udp_gso) {
649 			hdr_len = skb_transport_offset(skb) +
650 				  sizeof(struct udphdr);
651 		} else {
652 			hdr_len = skb_tcp_all_headers(skb);
653 		}
654 
655 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
656 					TX_BD_FLAGS_T_IPID |
657 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
658 		length = skb_shinfo(skb)->gso_size;
659 		txbd1->tx_bd_mss = cpu_to_le32(length);
660 		length += hdr_len;
661 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
662 		txbd1->tx_bd_hsize_lflags |=
663 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
664 		txbd1->tx_bd_mss = 0;
665 	}
666 
667 	length >>= 9;
668 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
669 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
670 				     skb->len);
671 		i = 0;
672 		goto tx_dma_error;
673 	}
674 	flags |= bnxt_lhint_arr[length];
675 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
676 
677 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
678 	txbd1->tx_bd_cfa_action =
679 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
680 	txbd0 = txbd;
681 	for (i = 0; i < last_frag; i++) {
682 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
683 
684 		prod = NEXT_TX(prod);
685 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
686 
687 		len = skb_frag_size(frag);
688 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
689 					   DMA_TO_DEVICE);
690 
691 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
692 			goto tx_dma_error;
693 
694 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
695 		dma_unmap_addr_set(tx_buf, mapping, mapping);
696 
697 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
698 
699 		flags = len << TX_BD_LEN_SHIFT;
700 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
701 	}
702 
703 	flags &= ~TX_BD_LEN;
704 	txbd->tx_bd_len_flags_type =
705 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
706 			    TX_BD_FLAGS_PACKET_END);
707 
708 	netdev_tx_sent_queue(txq, skb->len);
709 
710 	skb_tx_timestamp(skb);
711 
712 	prod = NEXT_TX(prod);
713 	WRITE_ONCE(txr->tx_prod, prod);
714 
715 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
716 		bnxt_txr_db_kick(bp, txr, prod);
717 	} else {
718 		if (free_size >= bp->tx_wake_thresh)
719 			txbd0->tx_bd_len_flags_type |=
720 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
721 		txr->kick_pending = 1;
722 	}
723 
724 tx_done:
725 
726 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
727 		if (netdev_xmit_more() && !tx_buf->is_push) {
728 			txbd0->tx_bd_len_flags_type &=
729 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
730 			bnxt_txr_db_kick(bp, txr, prod);
731 		}
732 
733 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
734 				   bp->tx_wake_thresh);
735 	}
736 	return NETDEV_TX_OK;
737 
738 tx_dma_error:
739 	last_frag = i;
740 
741 	/* start back at beginning and unmap skb */
742 	prod = txr->tx_prod;
743 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
744 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
745 			 skb_headlen(skb), DMA_TO_DEVICE);
746 	prod = NEXT_TX(prod);
747 
748 	/* unmap remaining mapped pages */
749 	for (i = 0; i < last_frag; i++) {
750 		prod = NEXT_TX(prod);
751 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
752 		dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
753 			       skb_frag_size(&skb_shinfo(skb)->frags[i]),
754 			       DMA_TO_DEVICE);
755 	}
756 
757 tx_free:
758 	dev_kfree_skb_any(skb);
759 tx_kick_pending:
760 	if (BNXT_TX_PTP_IS_SET(lflags)) {
761 		txr->tx_buf_ring[txr->tx_prod].is_ts_pkt = 0;
762 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
763 		if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
764 			/* set SKB to err so PTP worker will clean up */
765 			ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO);
766 	}
767 	if (txr->kick_pending)
768 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
769 	txr->tx_buf_ring[txr->tx_prod].skb = NULL;
770 	dev_core_stats_tx_dropped_inc(dev);
771 	return NETDEV_TX_OK;
772 }
773 
774 /* Returns true if some remaining TX packets not processed. */
775 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
776 			  int budget)
777 {
778 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
779 	struct pci_dev *pdev = bp->pdev;
780 	u16 hw_cons = txr->tx_hw_cons;
781 	unsigned int tx_bytes = 0;
782 	u16 cons = txr->tx_cons;
783 	int tx_pkts = 0;
784 	bool rc = false;
785 
786 	while (RING_TX(bp, cons) != hw_cons) {
787 		struct bnxt_sw_tx_bd *tx_buf;
788 		struct sk_buff *skb;
789 		bool is_ts_pkt;
790 		int j, last;
791 
792 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
793 		skb = tx_buf->skb;
794 
795 		if (unlikely(!skb)) {
796 			bnxt_sched_reset_txr(bp, txr, cons);
797 			return rc;
798 		}
799 
800 		is_ts_pkt = tx_buf->is_ts_pkt;
801 		if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) {
802 			rc = true;
803 			break;
804 		}
805 
806 		cons = NEXT_TX(cons);
807 		tx_pkts++;
808 		tx_bytes += skb->len;
809 		tx_buf->skb = NULL;
810 		tx_buf->is_ts_pkt = 0;
811 
812 		if (tx_buf->is_push) {
813 			tx_buf->is_push = 0;
814 			goto next_tx_int;
815 		}
816 
817 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
818 				 skb_headlen(skb), DMA_TO_DEVICE);
819 		last = tx_buf->nr_frags;
820 
821 		for (j = 0; j < last; j++) {
822 			cons = NEXT_TX(cons);
823 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
824 			dma_unmap_page(
825 				&pdev->dev,
826 				dma_unmap_addr(tx_buf, mapping),
827 				skb_frag_size(&skb_shinfo(skb)->frags[j]),
828 				DMA_TO_DEVICE);
829 		}
830 		if (unlikely(is_ts_pkt)) {
831 			if (BNXT_CHIP_P5(bp)) {
832 				/* PTP worker takes ownership of the skb */
833 				bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod);
834 				skb = NULL;
835 			}
836 		}
837 
838 next_tx_int:
839 		cons = NEXT_TX(cons);
840 
841 		dev_consume_skb_any(skb);
842 	}
843 
844 	WRITE_ONCE(txr->tx_cons, cons);
845 
846 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
847 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
848 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
849 
850 	return rc;
851 }
852 
853 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
854 {
855 	struct bnxt_tx_ring_info *txr;
856 	bool more = false;
857 	int i;
858 
859 	bnxt_for_each_napi_tx(i, bnapi, txr) {
860 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
861 			more |= __bnxt_tx_int(bp, txr, budget);
862 	}
863 	if (!more)
864 		bnapi->events &= ~BNXT_TX_CMP_EVENT;
865 }
866 
867 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
868 					 struct bnxt_rx_ring_info *rxr,
869 					 unsigned int *offset,
870 					 gfp_t gfp)
871 {
872 	struct page *page;
873 
874 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
875 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
876 						BNXT_RX_PAGE_SIZE);
877 	} else {
878 		page = page_pool_dev_alloc_pages(rxr->page_pool);
879 		*offset = 0;
880 	}
881 	if (!page)
882 		return NULL;
883 
884 	*mapping = page_pool_get_dma_addr(page) + *offset;
885 	return page;
886 }
887 
888 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
889 				       gfp_t gfp)
890 {
891 	u8 *data;
892 	struct pci_dev *pdev = bp->pdev;
893 
894 	if (gfp == GFP_ATOMIC)
895 		data = napi_alloc_frag(bp->rx_buf_size);
896 	else
897 		data = netdev_alloc_frag(bp->rx_buf_size);
898 	if (!data)
899 		return NULL;
900 
901 	*mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
902 					bp->rx_buf_use_size, bp->rx_dir,
903 					DMA_ATTR_WEAK_ORDERING);
904 
905 	if (dma_mapping_error(&pdev->dev, *mapping)) {
906 		skb_free_frag(data);
907 		data = NULL;
908 	}
909 	return data;
910 }
911 
912 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
913 		       u16 prod, gfp_t gfp)
914 {
915 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
916 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
917 	dma_addr_t mapping;
918 
919 	if (BNXT_RX_PAGE_MODE(bp)) {
920 		unsigned int offset;
921 		struct page *page =
922 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
923 
924 		if (!page)
925 			return -ENOMEM;
926 
927 		mapping += bp->rx_dma_offset;
928 		rx_buf->data = page;
929 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
930 	} else {
931 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, gfp);
932 
933 		if (!data)
934 			return -ENOMEM;
935 
936 		rx_buf->data = data;
937 		rx_buf->data_ptr = data + bp->rx_offset;
938 	}
939 	rx_buf->mapping = mapping;
940 
941 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
942 	return 0;
943 }
944 
945 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
946 {
947 	u16 prod = rxr->rx_prod;
948 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
949 	struct bnxt *bp = rxr->bnapi->bp;
950 	struct rx_bd *cons_bd, *prod_bd;
951 
952 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
953 	cons_rx_buf = &rxr->rx_buf_ring[cons];
954 
955 	prod_rx_buf->data = data;
956 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
957 
958 	prod_rx_buf->mapping = cons_rx_buf->mapping;
959 
960 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
961 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
962 
963 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
964 }
965 
966 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
967 {
968 	u16 next, max = rxr->rx_agg_bmap_size;
969 
970 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
971 	if (next >= max)
972 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
973 	return next;
974 }
975 
976 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
977 				     struct bnxt_rx_ring_info *rxr,
978 				     u16 prod, gfp_t gfp)
979 {
980 	struct rx_bd *rxbd =
981 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
982 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
983 	struct page *page;
984 	dma_addr_t mapping;
985 	u16 sw_prod = rxr->rx_sw_agg_prod;
986 	unsigned int offset = 0;
987 
988 	page = __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
989 
990 	if (!page)
991 		return -ENOMEM;
992 
993 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
994 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
995 
996 	__set_bit(sw_prod, rxr->rx_agg_bmap);
997 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
998 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
999 
1000 	rx_agg_buf->page = page;
1001 	rx_agg_buf->offset = offset;
1002 	rx_agg_buf->mapping = mapping;
1003 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1004 	rxbd->rx_bd_opaque = sw_prod;
1005 	return 0;
1006 }
1007 
1008 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1009 				       struct bnxt_cp_ring_info *cpr,
1010 				       u16 cp_cons, u16 curr)
1011 {
1012 	struct rx_agg_cmp *agg;
1013 
1014 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1015 	agg = (struct rx_agg_cmp *)
1016 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1017 	return agg;
1018 }
1019 
1020 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1021 					      struct bnxt_rx_ring_info *rxr,
1022 					      u16 agg_id, u16 curr)
1023 {
1024 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1025 
1026 	return &tpa_info->agg_arr[curr];
1027 }
1028 
1029 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1030 				   u16 start, u32 agg_bufs, bool tpa)
1031 {
1032 	struct bnxt_napi *bnapi = cpr->bnapi;
1033 	struct bnxt *bp = bnapi->bp;
1034 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1035 	u16 prod = rxr->rx_agg_prod;
1036 	u16 sw_prod = rxr->rx_sw_agg_prod;
1037 	bool p5_tpa = false;
1038 	u32 i;
1039 
1040 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1041 		p5_tpa = true;
1042 
1043 	for (i = 0; i < agg_bufs; i++) {
1044 		u16 cons;
1045 		struct rx_agg_cmp *agg;
1046 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1047 		struct rx_bd *prod_bd;
1048 		struct page *page;
1049 
1050 		if (p5_tpa)
1051 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1052 		else
1053 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1054 		cons = agg->rx_agg_cmp_opaque;
1055 		__clear_bit(cons, rxr->rx_agg_bmap);
1056 
1057 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1058 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1059 
1060 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1061 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1062 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1063 
1064 		/* It is possible for sw_prod to be equal to cons, so
1065 		 * set cons_rx_buf->page to NULL first.
1066 		 */
1067 		page = cons_rx_buf->page;
1068 		cons_rx_buf->page = NULL;
1069 		prod_rx_buf->page = page;
1070 		prod_rx_buf->offset = cons_rx_buf->offset;
1071 
1072 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1073 
1074 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1075 
1076 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1077 		prod_bd->rx_bd_opaque = sw_prod;
1078 
1079 		prod = NEXT_RX_AGG(prod);
1080 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1081 	}
1082 	rxr->rx_agg_prod = prod;
1083 	rxr->rx_sw_agg_prod = sw_prod;
1084 }
1085 
1086 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1087 					      struct bnxt_rx_ring_info *rxr,
1088 					      u16 cons, void *data, u8 *data_ptr,
1089 					      dma_addr_t dma_addr,
1090 					      unsigned int offset_and_len)
1091 {
1092 	unsigned int len = offset_and_len & 0xffff;
1093 	struct page *page = data;
1094 	u16 prod = rxr->rx_prod;
1095 	struct sk_buff *skb;
1096 	int err;
1097 
1098 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1099 	if (unlikely(err)) {
1100 		bnxt_reuse_rx_data(rxr, cons, data);
1101 		return NULL;
1102 	}
1103 	dma_addr -= bp->rx_dma_offset;
1104 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1105 				bp->rx_dir);
1106 	skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE);
1107 	if (!skb) {
1108 		page_pool_recycle_direct(rxr->page_pool, page);
1109 		return NULL;
1110 	}
1111 	skb_mark_for_recycle(skb);
1112 	skb_reserve(skb, bp->rx_offset);
1113 	__skb_put(skb, len);
1114 
1115 	return skb;
1116 }
1117 
1118 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1119 					struct bnxt_rx_ring_info *rxr,
1120 					u16 cons, void *data, u8 *data_ptr,
1121 					dma_addr_t dma_addr,
1122 					unsigned int offset_and_len)
1123 {
1124 	unsigned int payload = offset_and_len >> 16;
1125 	unsigned int len = offset_and_len & 0xffff;
1126 	skb_frag_t *frag;
1127 	struct page *page = data;
1128 	u16 prod = rxr->rx_prod;
1129 	struct sk_buff *skb;
1130 	int off, err;
1131 
1132 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1133 	if (unlikely(err)) {
1134 		bnxt_reuse_rx_data(rxr, cons, data);
1135 		return NULL;
1136 	}
1137 	dma_addr -= bp->rx_dma_offset;
1138 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1139 				bp->rx_dir);
1140 
1141 	if (unlikely(!payload))
1142 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1143 
1144 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1145 	if (!skb) {
1146 		page_pool_recycle_direct(rxr->page_pool, page);
1147 		return NULL;
1148 	}
1149 
1150 	skb_mark_for_recycle(skb);
1151 	off = (void *)data_ptr - page_address(page);
1152 	skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE);
1153 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1154 	       payload + NET_IP_ALIGN);
1155 
1156 	frag = &skb_shinfo(skb)->frags[0];
1157 	skb_frag_size_sub(frag, payload);
1158 	skb_frag_off_add(frag, payload);
1159 	skb->data_len -= payload;
1160 	skb->tail += payload;
1161 
1162 	return skb;
1163 }
1164 
1165 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1166 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1167 				   void *data, u8 *data_ptr,
1168 				   dma_addr_t dma_addr,
1169 				   unsigned int offset_and_len)
1170 {
1171 	u16 prod = rxr->rx_prod;
1172 	struct sk_buff *skb;
1173 	int err;
1174 
1175 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1176 	if (unlikely(err)) {
1177 		bnxt_reuse_rx_data(rxr, cons, data);
1178 		return NULL;
1179 	}
1180 
1181 	skb = napi_build_skb(data, bp->rx_buf_size);
1182 	dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1183 			       bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
1184 	if (!skb) {
1185 		skb_free_frag(data);
1186 		return NULL;
1187 	}
1188 
1189 	skb_reserve(skb, bp->rx_offset);
1190 	skb_put(skb, offset_and_len & 0xffff);
1191 	return skb;
1192 }
1193 
1194 static u32 __bnxt_rx_agg_pages(struct bnxt *bp,
1195 			       struct bnxt_cp_ring_info *cpr,
1196 			       struct skb_shared_info *shinfo,
1197 			       u16 idx, u32 agg_bufs, bool tpa,
1198 			       struct xdp_buff *xdp)
1199 {
1200 	struct bnxt_napi *bnapi = cpr->bnapi;
1201 	struct pci_dev *pdev = bp->pdev;
1202 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1203 	u16 prod = rxr->rx_agg_prod;
1204 	u32 i, total_frag_len = 0;
1205 	bool p5_tpa = false;
1206 
1207 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1208 		p5_tpa = true;
1209 
1210 	for (i = 0; i < agg_bufs; i++) {
1211 		skb_frag_t *frag = &shinfo->frags[i];
1212 		u16 cons, frag_len;
1213 		struct rx_agg_cmp *agg;
1214 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1215 		struct page *page;
1216 		dma_addr_t mapping;
1217 
1218 		if (p5_tpa)
1219 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1220 		else
1221 			agg = bnxt_get_agg(bp, cpr, idx, i);
1222 		cons = agg->rx_agg_cmp_opaque;
1223 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1224 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1225 
1226 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1227 		skb_frag_fill_page_desc(frag, cons_rx_buf->page,
1228 					cons_rx_buf->offset, frag_len);
1229 		shinfo->nr_frags = i + 1;
1230 		__clear_bit(cons, rxr->rx_agg_bmap);
1231 
1232 		/* It is possible for bnxt_alloc_rx_page() to allocate
1233 		 * a sw_prod index that equals the cons index, so we
1234 		 * need to clear the cons entry now.
1235 		 */
1236 		mapping = cons_rx_buf->mapping;
1237 		page = cons_rx_buf->page;
1238 		cons_rx_buf->page = NULL;
1239 
1240 		if (xdp && page_is_pfmemalloc(page))
1241 			xdp_buff_set_frag_pfmemalloc(xdp);
1242 
1243 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1244 			--shinfo->nr_frags;
1245 			cons_rx_buf->page = page;
1246 
1247 			/* Update prod since possibly some pages have been
1248 			 * allocated already.
1249 			 */
1250 			rxr->rx_agg_prod = prod;
1251 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1252 			return 0;
1253 		}
1254 
1255 		dma_sync_single_for_cpu(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1256 					bp->rx_dir);
1257 
1258 		total_frag_len += frag_len;
1259 		prod = NEXT_RX_AGG(prod);
1260 	}
1261 	rxr->rx_agg_prod = prod;
1262 	return total_frag_len;
1263 }
1264 
1265 static struct sk_buff *bnxt_rx_agg_pages_skb(struct bnxt *bp,
1266 					     struct bnxt_cp_ring_info *cpr,
1267 					     struct sk_buff *skb, u16 idx,
1268 					     u32 agg_bufs, bool tpa)
1269 {
1270 	struct skb_shared_info *shinfo = skb_shinfo(skb);
1271 	u32 total_frag_len = 0;
1272 
1273 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo, idx,
1274 					     agg_bufs, tpa, NULL);
1275 	if (!total_frag_len) {
1276 		skb_mark_for_recycle(skb);
1277 		dev_kfree_skb(skb);
1278 		return NULL;
1279 	}
1280 
1281 	skb->data_len += total_frag_len;
1282 	skb->len += total_frag_len;
1283 	skb->truesize += BNXT_RX_PAGE_SIZE * agg_bufs;
1284 	return skb;
1285 }
1286 
1287 static u32 bnxt_rx_agg_pages_xdp(struct bnxt *bp,
1288 				 struct bnxt_cp_ring_info *cpr,
1289 				 struct xdp_buff *xdp, u16 idx,
1290 				 u32 agg_bufs, bool tpa)
1291 {
1292 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1293 	u32 total_frag_len = 0;
1294 
1295 	if (!xdp_buff_has_frags(xdp))
1296 		shinfo->nr_frags = 0;
1297 
1298 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo,
1299 					     idx, agg_bufs, tpa, xdp);
1300 	if (total_frag_len) {
1301 		xdp_buff_set_frags_flag(xdp);
1302 		shinfo->nr_frags = agg_bufs;
1303 		shinfo->xdp_frags_size = total_frag_len;
1304 	}
1305 	return total_frag_len;
1306 }
1307 
1308 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1309 			       u8 agg_bufs, u32 *raw_cons)
1310 {
1311 	u16 last;
1312 	struct rx_agg_cmp *agg;
1313 
1314 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1315 	last = RING_CMP(*raw_cons);
1316 	agg = (struct rx_agg_cmp *)
1317 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1318 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1319 }
1320 
1321 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1322 				      unsigned int len,
1323 				      dma_addr_t mapping)
1324 {
1325 	struct bnxt *bp = bnapi->bp;
1326 	struct pci_dev *pdev = bp->pdev;
1327 	struct sk_buff *skb;
1328 
1329 	skb = napi_alloc_skb(&bnapi->napi, len);
1330 	if (!skb)
1331 		return NULL;
1332 
1333 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1334 				bp->rx_dir);
1335 
1336 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1337 	       len + NET_IP_ALIGN);
1338 
1339 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1340 				   bp->rx_dir);
1341 
1342 	skb_put(skb, len);
1343 
1344 	return skb;
1345 }
1346 
1347 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1348 				     unsigned int len,
1349 				     dma_addr_t mapping)
1350 {
1351 	return bnxt_copy_data(bnapi, data, len, mapping);
1352 }
1353 
1354 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1355 				     struct xdp_buff *xdp,
1356 				     unsigned int len,
1357 				     dma_addr_t mapping)
1358 {
1359 	unsigned int metasize = 0;
1360 	u8 *data = xdp->data;
1361 	struct sk_buff *skb;
1362 
1363 	len = xdp->data_end - xdp->data_meta;
1364 	metasize = xdp->data - xdp->data_meta;
1365 	data = xdp->data_meta;
1366 
1367 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1368 	if (!skb)
1369 		return skb;
1370 
1371 	if (metasize) {
1372 		skb_metadata_set(skb, metasize);
1373 		__skb_pull(skb, metasize);
1374 	}
1375 
1376 	return skb;
1377 }
1378 
1379 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1380 			   u32 *raw_cons, void *cmp)
1381 {
1382 	struct rx_cmp *rxcmp = cmp;
1383 	u32 tmp_raw_cons = *raw_cons;
1384 	u8 cmp_type, agg_bufs = 0;
1385 
1386 	cmp_type = RX_CMP_TYPE(rxcmp);
1387 
1388 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1389 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1390 			    RX_CMP_AGG_BUFS) >>
1391 			   RX_CMP_AGG_BUFS_SHIFT;
1392 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1393 		struct rx_tpa_end_cmp *tpa_end = cmp;
1394 
1395 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1396 			return 0;
1397 
1398 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1399 	}
1400 
1401 	if (agg_bufs) {
1402 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1403 			return -EBUSY;
1404 	}
1405 	*raw_cons = tmp_raw_cons;
1406 	return 0;
1407 }
1408 
1409 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1410 {
1411 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1412 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1413 
1414 	if (test_bit(idx, map->agg_idx_bmap))
1415 		idx = find_first_zero_bit(map->agg_idx_bmap,
1416 					  BNXT_AGG_IDX_BMAP_SIZE);
1417 	__set_bit(idx, map->agg_idx_bmap);
1418 	map->agg_id_tbl[agg_id] = idx;
1419 	return idx;
1420 }
1421 
1422 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1423 {
1424 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1425 
1426 	__clear_bit(idx, map->agg_idx_bmap);
1427 }
1428 
1429 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1430 {
1431 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1432 
1433 	return map->agg_id_tbl[agg_id];
1434 }
1435 
1436 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1437 			      struct rx_tpa_start_cmp *tpa_start,
1438 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1439 {
1440 	tpa_info->cfa_code_valid = 1;
1441 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1442 	tpa_info->vlan_valid = 0;
1443 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1444 		tpa_info->vlan_valid = 1;
1445 		tpa_info->metadata =
1446 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1447 	}
1448 }
1449 
1450 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1451 				 struct rx_tpa_start_cmp *tpa_start,
1452 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1453 {
1454 	tpa_info->vlan_valid = 0;
1455 	if (TPA_START_VLAN_VALID(tpa_start)) {
1456 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1457 		u32 vlan_proto = ETH_P_8021Q;
1458 
1459 		tpa_info->vlan_valid = 1;
1460 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1461 			vlan_proto = ETH_P_8021AD;
1462 		tpa_info->metadata = vlan_proto << 16 |
1463 				     TPA_START_METADATA0_TCI(tpa_start1);
1464 	}
1465 }
1466 
1467 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1468 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1469 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1470 {
1471 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1472 	struct bnxt_tpa_info *tpa_info;
1473 	u16 cons, prod, agg_id;
1474 	struct rx_bd *prod_bd;
1475 	dma_addr_t mapping;
1476 
1477 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1478 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1479 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1480 	} else {
1481 		agg_id = TPA_START_AGG_ID(tpa_start);
1482 	}
1483 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1484 	prod = rxr->rx_prod;
1485 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1486 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1487 	tpa_info = &rxr->rx_tpa[agg_id];
1488 
1489 	if (unlikely(cons != rxr->rx_next_cons ||
1490 		     TPA_START_ERROR(tpa_start))) {
1491 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1492 			    cons, rxr->rx_next_cons,
1493 			    TPA_START_ERROR_CODE(tpa_start1));
1494 		bnxt_sched_reset_rxr(bp, rxr);
1495 		return;
1496 	}
1497 	prod_rx_buf->data = tpa_info->data;
1498 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1499 
1500 	mapping = tpa_info->mapping;
1501 	prod_rx_buf->mapping = mapping;
1502 
1503 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1504 
1505 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1506 
1507 	tpa_info->data = cons_rx_buf->data;
1508 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1509 	cons_rx_buf->data = NULL;
1510 	tpa_info->mapping = cons_rx_buf->mapping;
1511 
1512 	tpa_info->len =
1513 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1514 				RX_TPA_START_CMP_LEN_SHIFT;
1515 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1516 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1517 		tpa_info->gso_type = SKB_GSO_TCPV4;
1518 		if (TPA_START_IS_IPV6(tpa_start1))
1519 			tpa_info->gso_type = SKB_GSO_TCPV6;
1520 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1521 		else if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP &&
1522 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1523 			tpa_info->gso_type = SKB_GSO_TCPV6;
1524 		tpa_info->rss_hash =
1525 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1526 	} else {
1527 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1528 		tpa_info->gso_type = 0;
1529 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1530 	}
1531 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1532 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1533 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1534 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1535 	else
1536 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1537 	tpa_info->agg_count = 0;
1538 
1539 	rxr->rx_prod = NEXT_RX(prod);
1540 	cons = RING_RX(bp, NEXT_RX(cons));
1541 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1542 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1543 
1544 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1545 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1546 	cons_rx_buf->data = NULL;
1547 }
1548 
1549 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1550 {
1551 	if (agg_bufs)
1552 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1553 }
1554 
1555 #ifdef CONFIG_INET
1556 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1557 {
1558 	struct udphdr *uh = NULL;
1559 
1560 	if (ip_proto == htons(ETH_P_IP)) {
1561 		struct iphdr *iph = (struct iphdr *)skb->data;
1562 
1563 		if (iph->protocol == IPPROTO_UDP)
1564 			uh = (struct udphdr *)(iph + 1);
1565 	} else {
1566 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1567 
1568 		if (iph->nexthdr == IPPROTO_UDP)
1569 			uh = (struct udphdr *)(iph + 1);
1570 	}
1571 	if (uh) {
1572 		if (uh->check)
1573 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1574 		else
1575 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1576 	}
1577 }
1578 #endif
1579 
1580 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1581 					   int payload_off, int tcp_ts,
1582 					   struct sk_buff *skb)
1583 {
1584 #ifdef CONFIG_INET
1585 	struct tcphdr *th;
1586 	int len, nw_off;
1587 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1588 	u32 hdr_info = tpa_info->hdr_info;
1589 	bool loopback = false;
1590 
1591 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1592 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1593 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1594 
1595 	/* If the packet is an internal loopback packet, the offsets will
1596 	 * have an extra 4 bytes.
1597 	 */
1598 	if (inner_mac_off == 4) {
1599 		loopback = true;
1600 	} else if (inner_mac_off > 4) {
1601 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1602 					    ETH_HLEN - 2));
1603 
1604 		/* We only support inner iPv4/ipv6.  If we don't see the
1605 		 * correct protocol ID, it must be a loopback packet where
1606 		 * the offsets are off by 4.
1607 		 */
1608 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1609 			loopback = true;
1610 	}
1611 	if (loopback) {
1612 		/* internal loopback packet, subtract all offsets by 4 */
1613 		inner_ip_off -= 4;
1614 		inner_mac_off -= 4;
1615 		outer_ip_off -= 4;
1616 	}
1617 
1618 	nw_off = inner_ip_off - ETH_HLEN;
1619 	skb_set_network_header(skb, nw_off);
1620 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1621 		struct ipv6hdr *iph = ipv6_hdr(skb);
1622 
1623 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1624 		len = skb->len - skb_transport_offset(skb);
1625 		th = tcp_hdr(skb);
1626 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1627 	} else {
1628 		struct iphdr *iph = ip_hdr(skb);
1629 
1630 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1631 		len = skb->len - skb_transport_offset(skb);
1632 		th = tcp_hdr(skb);
1633 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1634 	}
1635 
1636 	if (inner_mac_off) { /* tunnel */
1637 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1638 					    ETH_HLEN - 2));
1639 
1640 		bnxt_gro_tunnel(skb, proto);
1641 	}
1642 #endif
1643 	return skb;
1644 }
1645 
1646 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1647 					   int payload_off, int tcp_ts,
1648 					   struct sk_buff *skb)
1649 {
1650 #ifdef CONFIG_INET
1651 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1652 	u32 hdr_info = tpa_info->hdr_info;
1653 	int iphdr_len, nw_off;
1654 
1655 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1656 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1657 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1658 
1659 	nw_off = inner_ip_off - ETH_HLEN;
1660 	skb_set_network_header(skb, nw_off);
1661 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1662 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1663 	skb_set_transport_header(skb, nw_off + iphdr_len);
1664 
1665 	if (inner_mac_off) { /* tunnel */
1666 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1667 					    ETH_HLEN - 2));
1668 
1669 		bnxt_gro_tunnel(skb, proto);
1670 	}
1671 #endif
1672 	return skb;
1673 }
1674 
1675 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1676 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1677 
1678 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1679 					   int payload_off, int tcp_ts,
1680 					   struct sk_buff *skb)
1681 {
1682 #ifdef CONFIG_INET
1683 	struct tcphdr *th;
1684 	int len, nw_off, tcp_opt_len = 0;
1685 
1686 	if (tcp_ts)
1687 		tcp_opt_len = 12;
1688 
1689 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1690 		struct iphdr *iph;
1691 
1692 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1693 			 ETH_HLEN;
1694 		skb_set_network_header(skb, nw_off);
1695 		iph = ip_hdr(skb);
1696 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1697 		len = skb->len - skb_transport_offset(skb);
1698 		th = tcp_hdr(skb);
1699 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1700 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1701 		struct ipv6hdr *iph;
1702 
1703 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1704 			 ETH_HLEN;
1705 		skb_set_network_header(skb, nw_off);
1706 		iph = ipv6_hdr(skb);
1707 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1708 		len = skb->len - skb_transport_offset(skb);
1709 		th = tcp_hdr(skb);
1710 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1711 	} else {
1712 		dev_kfree_skb_any(skb);
1713 		return NULL;
1714 	}
1715 
1716 	if (nw_off) /* tunnel */
1717 		bnxt_gro_tunnel(skb, skb->protocol);
1718 #endif
1719 	return skb;
1720 }
1721 
1722 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1723 					   struct bnxt_tpa_info *tpa_info,
1724 					   struct rx_tpa_end_cmp *tpa_end,
1725 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1726 					   struct sk_buff *skb)
1727 {
1728 #ifdef CONFIG_INET
1729 	int payload_off;
1730 	u16 segs;
1731 
1732 	segs = TPA_END_TPA_SEGS(tpa_end);
1733 	if (segs == 1)
1734 		return skb;
1735 
1736 	NAPI_GRO_CB(skb)->count = segs;
1737 	skb_shinfo(skb)->gso_size =
1738 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1739 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1740 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1741 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1742 	else
1743 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1744 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1745 	if (likely(skb))
1746 		tcp_gro_complete(skb);
1747 #endif
1748 	return skb;
1749 }
1750 
1751 /* Given the cfa_code of a received packet determine which
1752  * netdev (vf-rep or PF) the packet is destined to.
1753  */
1754 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1755 {
1756 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1757 
1758 	/* if vf-rep dev is NULL, the must belongs to the PF */
1759 	return dev ? dev : bp->dev;
1760 }
1761 
1762 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1763 					   struct bnxt_cp_ring_info *cpr,
1764 					   u32 *raw_cons,
1765 					   struct rx_tpa_end_cmp *tpa_end,
1766 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1767 					   u8 *event)
1768 {
1769 	struct bnxt_napi *bnapi = cpr->bnapi;
1770 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1771 	struct net_device *dev = bp->dev;
1772 	u8 *data_ptr, agg_bufs;
1773 	unsigned int len;
1774 	struct bnxt_tpa_info *tpa_info;
1775 	dma_addr_t mapping;
1776 	struct sk_buff *skb;
1777 	u16 idx = 0, agg_id;
1778 	void *data;
1779 	bool gro;
1780 
1781 	if (unlikely(bnapi->in_reset)) {
1782 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1783 
1784 		if (rc < 0)
1785 			return ERR_PTR(-EBUSY);
1786 		return NULL;
1787 	}
1788 
1789 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1790 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1791 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1792 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1793 		tpa_info = &rxr->rx_tpa[agg_id];
1794 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1795 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1796 				    agg_bufs, tpa_info->agg_count);
1797 			agg_bufs = tpa_info->agg_count;
1798 		}
1799 		tpa_info->agg_count = 0;
1800 		*event |= BNXT_AGG_EVENT;
1801 		bnxt_free_agg_idx(rxr, agg_id);
1802 		idx = agg_id;
1803 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1804 	} else {
1805 		agg_id = TPA_END_AGG_ID(tpa_end);
1806 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1807 		tpa_info = &rxr->rx_tpa[agg_id];
1808 		idx = RING_CMP(*raw_cons);
1809 		if (agg_bufs) {
1810 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1811 				return ERR_PTR(-EBUSY);
1812 
1813 			*event |= BNXT_AGG_EVENT;
1814 			idx = NEXT_CMP(idx);
1815 		}
1816 		gro = !!TPA_END_GRO(tpa_end);
1817 	}
1818 	data = tpa_info->data;
1819 	data_ptr = tpa_info->data_ptr;
1820 	prefetch(data_ptr);
1821 	len = tpa_info->len;
1822 	mapping = tpa_info->mapping;
1823 
1824 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1825 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1826 		if (agg_bufs > MAX_SKB_FRAGS)
1827 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1828 				    agg_bufs, (int)MAX_SKB_FRAGS);
1829 		return NULL;
1830 	}
1831 
1832 	if (len <= bp->rx_copy_thresh) {
1833 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1834 		if (!skb) {
1835 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1836 			cpr->sw_stats->rx.rx_oom_discards += 1;
1837 			return NULL;
1838 		}
1839 	} else {
1840 		u8 *new_data;
1841 		dma_addr_t new_mapping;
1842 
1843 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, GFP_ATOMIC);
1844 		if (!new_data) {
1845 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1846 			cpr->sw_stats->rx.rx_oom_discards += 1;
1847 			return NULL;
1848 		}
1849 
1850 		tpa_info->data = new_data;
1851 		tpa_info->data_ptr = new_data + bp->rx_offset;
1852 		tpa_info->mapping = new_mapping;
1853 
1854 		skb = napi_build_skb(data, bp->rx_buf_size);
1855 		dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1856 				       bp->rx_buf_use_size, bp->rx_dir,
1857 				       DMA_ATTR_WEAK_ORDERING);
1858 
1859 		if (!skb) {
1860 			skb_free_frag(data);
1861 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1862 			cpr->sw_stats->rx.rx_oom_discards += 1;
1863 			return NULL;
1864 		}
1865 		skb_reserve(skb, bp->rx_offset);
1866 		skb_put(skb, len);
1867 	}
1868 
1869 	if (agg_bufs) {
1870 		skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, idx, agg_bufs, true);
1871 		if (!skb) {
1872 			/* Page reuse already handled by bnxt_rx_pages(). */
1873 			cpr->sw_stats->rx.rx_oom_discards += 1;
1874 			return NULL;
1875 		}
1876 	}
1877 
1878 	if (tpa_info->cfa_code_valid)
1879 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1880 	skb->protocol = eth_type_trans(skb, dev);
1881 
1882 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1883 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1884 
1885 	if (tpa_info->vlan_valid &&
1886 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1887 		__be16 vlan_proto = htons(tpa_info->metadata >>
1888 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1889 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1890 
1891 		if (eth_type_vlan(vlan_proto)) {
1892 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1893 		} else {
1894 			dev_kfree_skb(skb);
1895 			return NULL;
1896 		}
1897 	}
1898 
1899 	skb_checksum_none_assert(skb);
1900 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1901 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1902 		skb->csum_level =
1903 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1904 	}
1905 
1906 	if (gro)
1907 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1908 
1909 	return skb;
1910 }
1911 
1912 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1913 			 struct rx_agg_cmp *rx_agg)
1914 {
1915 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1916 	struct bnxt_tpa_info *tpa_info;
1917 
1918 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1919 	tpa_info = &rxr->rx_tpa[agg_id];
1920 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1921 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1922 }
1923 
1924 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1925 			     struct sk_buff *skb)
1926 {
1927 	skb_mark_for_recycle(skb);
1928 
1929 	if (skb->dev != bp->dev) {
1930 		/* this packet belongs to a vf-rep */
1931 		bnxt_vf_rep_rx(bp, skb);
1932 		return;
1933 	}
1934 	skb_record_rx_queue(skb, bnapi->index);
1935 	napi_gro_receive(&bnapi->napi, skb);
1936 }
1937 
1938 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
1939 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
1940 {
1941 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
1942 
1943 	if (BNXT_PTP_RX_TS_VALID(flags))
1944 		goto ts_valid;
1945 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
1946 		return false;
1947 
1948 ts_valid:
1949 	*cmpl_ts = ts;
1950 	return true;
1951 }
1952 
1953 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
1954 				    struct rx_cmp *rxcmp,
1955 				    struct rx_cmp_ext *rxcmp1)
1956 {
1957 	__be16 vlan_proto;
1958 	u16 vtag;
1959 
1960 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1961 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
1962 		u32 meta_data;
1963 
1964 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
1965 			return skb;
1966 
1967 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1968 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1969 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
1970 		if (eth_type_vlan(vlan_proto))
1971 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1972 		else
1973 			goto vlan_err;
1974 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
1975 		if (RX_CMP_VLAN_VALID(rxcmp)) {
1976 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
1977 
1978 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
1979 				vlan_proto = htons(ETH_P_8021Q);
1980 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
1981 				vlan_proto = htons(ETH_P_8021AD);
1982 			else
1983 				goto vlan_err;
1984 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
1985 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1986 		}
1987 	}
1988 	return skb;
1989 vlan_err:
1990 	dev_kfree_skb(skb);
1991 	return NULL;
1992 }
1993 
1994 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp,
1995 					   struct rx_cmp *rxcmp)
1996 {
1997 	u8 ext_op;
1998 
1999 	ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp);
2000 	switch (ext_op) {
2001 	case EXT_OP_INNER_4:
2002 	case EXT_OP_OUTER_4:
2003 	case EXT_OP_INNFL_3:
2004 	case EXT_OP_OUTFL_3:
2005 		return PKT_HASH_TYPE_L4;
2006 	default:
2007 		return PKT_HASH_TYPE_L3;
2008 	}
2009 }
2010 
2011 /* returns the following:
2012  * 1       - 1 packet successfully received
2013  * 0       - successful TPA_START, packet not completed yet
2014  * -EBUSY  - completion ring does not have all the agg buffers yet
2015  * -ENOMEM - packet aborted due to out of memory
2016  * -EIO    - packet aborted due to hw error indicated in BD
2017  */
2018 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2019 		       u32 *raw_cons, u8 *event)
2020 {
2021 	struct bnxt_napi *bnapi = cpr->bnapi;
2022 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2023 	struct net_device *dev = bp->dev;
2024 	struct rx_cmp *rxcmp;
2025 	struct rx_cmp_ext *rxcmp1;
2026 	u32 tmp_raw_cons = *raw_cons;
2027 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2028 	struct bnxt_sw_rx_bd *rx_buf;
2029 	unsigned int len;
2030 	u8 *data_ptr, agg_bufs, cmp_type;
2031 	bool xdp_active = false;
2032 	dma_addr_t dma_addr;
2033 	struct sk_buff *skb;
2034 	struct xdp_buff xdp;
2035 	u32 flags, misc;
2036 	u32 cmpl_ts;
2037 	void *data;
2038 	int rc = 0;
2039 
2040 	rxcmp = (struct rx_cmp *)
2041 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2042 
2043 	cmp_type = RX_CMP_TYPE(rxcmp);
2044 
2045 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2046 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2047 		goto next_rx_no_prod_no_len;
2048 	}
2049 
2050 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2051 	cp_cons = RING_CMP(tmp_raw_cons);
2052 	rxcmp1 = (struct rx_cmp_ext *)
2053 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2054 
2055 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2056 		return -EBUSY;
2057 
2058 	/* The valid test of the entry must be done first before
2059 	 * reading any further.
2060 	 */
2061 	dma_rmb();
2062 	prod = rxr->rx_prod;
2063 
2064 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2065 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2066 		bnxt_tpa_start(bp, rxr, cmp_type,
2067 			       (struct rx_tpa_start_cmp *)rxcmp,
2068 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2069 
2070 		*event |= BNXT_RX_EVENT;
2071 		goto next_rx_no_prod_no_len;
2072 
2073 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2074 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2075 				   (struct rx_tpa_end_cmp *)rxcmp,
2076 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2077 
2078 		if (IS_ERR(skb))
2079 			return -EBUSY;
2080 
2081 		rc = -ENOMEM;
2082 		if (likely(skb)) {
2083 			bnxt_deliver_skb(bp, bnapi, skb);
2084 			rc = 1;
2085 		}
2086 		*event |= BNXT_RX_EVENT;
2087 		goto next_rx_no_prod_no_len;
2088 	}
2089 
2090 	cons = rxcmp->rx_cmp_opaque;
2091 	if (unlikely(cons != rxr->rx_next_cons)) {
2092 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2093 
2094 		/* 0xffff is forced error, don't print it */
2095 		if (rxr->rx_next_cons != 0xffff)
2096 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2097 				    cons, rxr->rx_next_cons);
2098 		bnxt_sched_reset_rxr(bp, rxr);
2099 		if (rc1)
2100 			return rc1;
2101 		goto next_rx_no_prod_no_len;
2102 	}
2103 	rx_buf = &rxr->rx_buf_ring[cons];
2104 	data = rx_buf->data;
2105 	data_ptr = rx_buf->data_ptr;
2106 	prefetch(data_ptr);
2107 
2108 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2109 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2110 
2111 	if (agg_bufs) {
2112 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2113 			return -EBUSY;
2114 
2115 		cp_cons = NEXT_CMP(cp_cons);
2116 		*event |= BNXT_AGG_EVENT;
2117 	}
2118 	*event |= BNXT_RX_EVENT;
2119 
2120 	rx_buf->data = NULL;
2121 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2122 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2123 
2124 		bnxt_reuse_rx_data(rxr, cons, data);
2125 		if (agg_bufs)
2126 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2127 					       false);
2128 
2129 		rc = -EIO;
2130 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2131 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2132 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2133 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2134 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2135 						 rx_err);
2136 				bnxt_sched_reset_rxr(bp, rxr);
2137 			}
2138 		}
2139 		goto next_rx_no_len;
2140 	}
2141 
2142 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2143 	len = flags >> RX_CMP_LEN_SHIFT;
2144 	dma_addr = rx_buf->mapping;
2145 
2146 	if (bnxt_xdp_attached(bp, rxr)) {
2147 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2148 		if (agg_bufs) {
2149 			u32 frag_len = bnxt_rx_agg_pages_xdp(bp, cpr, &xdp,
2150 							     cp_cons, agg_bufs,
2151 							     false);
2152 			if (!frag_len)
2153 				goto oom_next_rx;
2154 		}
2155 		xdp_active = true;
2156 	}
2157 
2158 	if (xdp_active) {
2159 		if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) {
2160 			rc = 1;
2161 			goto next_rx;
2162 		}
2163 	}
2164 
2165 	if (len <= bp->rx_copy_thresh) {
2166 		if (!xdp_active)
2167 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2168 		else
2169 			skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr);
2170 		bnxt_reuse_rx_data(rxr, cons, data);
2171 		if (!skb) {
2172 			if (agg_bufs) {
2173 				if (!xdp_active)
2174 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2175 							       agg_bufs, false);
2176 				else
2177 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2178 			}
2179 			goto oom_next_rx;
2180 		}
2181 	} else {
2182 		u32 payload;
2183 
2184 		if (rx_buf->data_ptr == data_ptr)
2185 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2186 		else
2187 			payload = 0;
2188 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2189 				      payload | len);
2190 		if (!skb)
2191 			goto oom_next_rx;
2192 	}
2193 
2194 	if (agg_bufs) {
2195 		if (!xdp_active) {
2196 			skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, cp_cons, agg_bufs, false);
2197 			if (!skb)
2198 				goto oom_next_rx;
2199 		} else {
2200 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, rxr->page_pool, &xdp, rxcmp1);
2201 			if (!skb) {
2202 				/* we should be able to free the old skb here */
2203 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2204 				goto oom_next_rx;
2205 			}
2206 		}
2207 	}
2208 
2209 	if (RX_CMP_HASH_VALID(rxcmp)) {
2210 		enum pkt_hash_types type;
2211 
2212 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2213 			type = bnxt_rss_ext_op(bp, rxcmp);
2214 		} else {
2215 			u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
2216 
2217 			/* RSS profiles 1 and 3 with extract code 0 for inner
2218 			 * 4-tuple
2219 			 */
2220 			if (hash_type != 1 && hash_type != 3)
2221 				type = PKT_HASH_TYPE_L3;
2222 			else
2223 				type = PKT_HASH_TYPE_L4;
2224 		}
2225 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2226 	}
2227 
2228 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2229 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2230 	skb->protocol = eth_type_trans(skb, dev);
2231 
2232 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2233 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2234 		if (!skb)
2235 			goto next_rx;
2236 	}
2237 
2238 	skb_checksum_none_assert(skb);
2239 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2240 		if (dev->features & NETIF_F_RXCSUM) {
2241 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2242 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2243 		}
2244 	} else {
2245 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2246 			if (dev->features & NETIF_F_RXCSUM)
2247 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2248 		}
2249 	}
2250 
2251 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2252 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2253 			u64 ns, ts;
2254 
2255 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2256 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2257 
2258 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2259 				memset(skb_hwtstamps(skb), 0,
2260 				       sizeof(*skb_hwtstamps(skb)));
2261 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2262 			}
2263 		}
2264 	}
2265 	bnxt_deliver_skb(bp, bnapi, skb);
2266 	rc = 1;
2267 
2268 next_rx:
2269 	cpr->rx_packets += 1;
2270 	cpr->rx_bytes += len;
2271 
2272 next_rx_no_len:
2273 	rxr->rx_prod = NEXT_RX(prod);
2274 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2275 
2276 next_rx_no_prod_no_len:
2277 	*raw_cons = tmp_raw_cons;
2278 
2279 	return rc;
2280 
2281 oom_next_rx:
2282 	cpr->sw_stats->rx.rx_oom_discards += 1;
2283 	rc = -ENOMEM;
2284 	goto next_rx;
2285 }
2286 
2287 /* In netpoll mode, if we are using a combined completion ring, we need to
2288  * discard the rx packets and recycle the buffers.
2289  */
2290 static int bnxt_force_rx_discard(struct bnxt *bp,
2291 				 struct bnxt_cp_ring_info *cpr,
2292 				 u32 *raw_cons, u8 *event)
2293 {
2294 	u32 tmp_raw_cons = *raw_cons;
2295 	struct rx_cmp_ext *rxcmp1;
2296 	struct rx_cmp *rxcmp;
2297 	u16 cp_cons;
2298 	u8 cmp_type;
2299 	int rc;
2300 
2301 	cp_cons = RING_CMP(tmp_raw_cons);
2302 	rxcmp = (struct rx_cmp *)
2303 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2304 
2305 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2306 	cp_cons = RING_CMP(tmp_raw_cons);
2307 	rxcmp1 = (struct rx_cmp_ext *)
2308 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2309 
2310 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2311 		return -EBUSY;
2312 
2313 	/* The valid test of the entry must be done first before
2314 	 * reading any further.
2315 	 */
2316 	dma_rmb();
2317 	cmp_type = RX_CMP_TYPE(rxcmp);
2318 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2319 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2320 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2321 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2322 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2323 		struct rx_tpa_end_cmp_ext *tpa_end1;
2324 
2325 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2326 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2327 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2328 	}
2329 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2330 	if (rc && rc != -EBUSY)
2331 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2332 	return rc;
2333 }
2334 
2335 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2336 {
2337 	struct bnxt_fw_health *fw_health = bp->fw_health;
2338 	u32 reg = fw_health->regs[reg_idx];
2339 	u32 reg_type, reg_off, val = 0;
2340 
2341 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2342 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2343 	switch (reg_type) {
2344 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2345 		pci_read_config_dword(bp->pdev, reg_off, &val);
2346 		break;
2347 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2348 		reg_off = fw_health->mapped_regs[reg_idx];
2349 		fallthrough;
2350 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2351 		val = readl(bp->bar0 + reg_off);
2352 		break;
2353 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2354 		val = readl(bp->bar1 + reg_off);
2355 		break;
2356 	}
2357 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2358 		val &= fw_health->fw_reset_inprog_reg_mask;
2359 	return val;
2360 }
2361 
2362 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2363 {
2364 	int i;
2365 
2366 	for (i = 0; i < bp->rx_nr_rings; i++) {
2367 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2368 		struct bnxt_ring_grp_info *grp_info;
2369 
2370 		grp_info = &bp->grp_info[grp_idx];
2371 		if (grp_info->agg_fw_ring_id == ring_id)
2372 			return grp_idx;
2373 	}
2374 	return INVALID_HW_RING_ID;
2375 }
2376 
2377 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2378 {
2379 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2380 
2381 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2382 		return link_info->force_link_speed2;
2383 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2384 		return link_info->force_pam4_link_speed;
2385 	return link_info->force_link_speed;
2386 }
2387 
2388 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2389 {
2390 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2391 
2392 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2393 		link_info->req_link_speed = link_info->force_link_speed2;
2394 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2395 		switch (link_info->req_link_speed) {
2396 		case BNXT_LINK_SPEED_50GB_PAM4:
2397 		case BNXT_LINK_SPEED_100GB_PAM4:
2398 		case BNXT_LINK_SPEED_200GB_PAM4:
2399 		case BNXT_LINK_SPEED_400GB_PAM4:
2400 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2401 			break;
2402 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2403 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2404 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2405 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2406 			break;
2407 		default:
2408 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2409 		}
2410 		return;
2411 	}
2412 	link_info->req_link_speed = link_info->force_link_speed;
2413 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2414 	if (link_info->force_pam4_link_speed) {
2415 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2416 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2417 	}
2418 }
2419 
2420 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2421 {
2422 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2423 
2424 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2425 		link_info->advertising = link_info->auto_link_speeds2;
2426 		return;
2427 	}
2428 	link_info->advertising = link_info->auto_link_speeds;
2429 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2430 }
2431 
2432 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2433 {
2434 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2435 
2436 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2437 		if (link_info->req_link_speed != link_info->force_link_speed2)
2438 			return true;
2439 		return false;
2440 	}
2441 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2442 	    link_info->req_link_speed != link_info->force_link_speed)
2443 		return true;
2444 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2445 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2446 		return true;
2447 	return false;
2448 }
2449 
2450 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2451 {
2452 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2453 
2454 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2455 		if (link_info->advertising != link_info->auto_link_speeds2)
2456 			return true;
2457 		return false;
2458 	}
2459 	if (link_info->advertising != link_info->auto_link_speeds ||
2460 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2461 		return true;
2462 	return false;
2463 }
2464 
2465 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2466 	((data2) &							\
2467 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2468 
2469 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2470 	(((data2) &							\
2471 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2472 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2473 
2474 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2475 	((data1) &							\
2476 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2477 
2478 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2479 	(((data1) &							\
2480 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2481 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2482 
2483 /* Return true if the workqueue has to be scheduled */
2484 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2485 {
2486 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2487 
2488 	switch (err_type) {
2489 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2490 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2491 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2492 		break;
2493 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2494 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2495 		break;
2496 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2497 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2498 		break;
2499 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2500 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2501 		char *threshold_type;
2502 		bool notify = false;
2503 		char *dir_str;
2504 
2505 		switch (type) {
2506 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2507 			threshold_type = "warning";
2508 			break;
2509 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2510 			threshold_type = "critical";
2511 			break;
2512 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2513 			threshold_type = "fatal";
2514 			break;
2515 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2516 			threshold_type = "shutdown";
2517 			break;
2518 		default:
2519 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2520 			return false;
2521 		}
2522 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2523 			dir_str = "above";
2524 			notify = true;
2525 		} else {
2526 			dir_str = "below";
2527 		}
2528 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2529 			    dir_str, threshold_type);
2530 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2531 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2532 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2533 		if (notify) {
2534 			bp->thermal_threshold_type = type;
2535 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2536 			return true;
2537 		}
2538 		return false;
2539 	}
2540 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2541 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2542 		break;
2543 	default:
2544 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2545 			   err_type);
2546 		break;
2547 	}
2548 	return false;
2549 }
2550 
2551 #define BNXT_GET_EVENT_PORT(data)	\
2552 	((data) &			\
2553 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2554 
2555 #define BNXT_EVENT_RING_TYPE(data2)	\
2556 	((data2) &			\
2557 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2558 
2559 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2560 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2561 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2562 
2563 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2564 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2565 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2566 
2567 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2568 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2569 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2570 
2571 #define BNXT_PHC_BITS	48
2572 
2573 static int bnxt_async_event_process(struct bnxt *bp,
2574 				    struct hwrm_async_event_cmpl *cmpl)
2575 {
2576 	u16 event_id = le16_to_cpu(cmpl->event_id);
2577 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2578 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2579 
2580 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2581 		   event_id, data1, data2);
2582 
2583 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2584 	switch (event_id) {
2585 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2586 		struct bnxt_link_info *link_info = &bp->link_info;
2587 
2588 		if (BNXT_VF(bp))
2589 			goto async_event_process_exit;
2590 
2591 		/* print unsupported speed warning in forced speed mode only */
2592 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2593 		    (data1 & 0x20000)) {
2594 			u16 fw_speed = bnxt_get_force_speed(link_info);
2595 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2596 
2597 			if (speed != SPEED_UNKNOWN)
2598 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2599 					    speed);
2600 		}
2601 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2602 	}
2603 		fallthrough;
2604 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2605 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2606 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2607 		fallthrough;
2608 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2609 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2610 		break;
2611 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2612 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2613 		break;
2614 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2615 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2616 
2617 		if (BNXT_VF(bp))
2618 			break;
2619 
2620 		if (bp->pf.port_id != port_id)
2621 			break;
2622 
2623 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2624 		break;
2625 	}
2626 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2627 		if (BNXT_PF(bp))
2628 			goto async_event_process_exit;
2629 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2630 		break;
2631 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2632 		char *type_str = "Solicited";
2633 
2634 		if (!bp->fw_health)
2635 			goto async_event_process_exit;
2636 
2637 		bp->fw_reset_timestamp = jiffies;
2638 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2639 		if (!bp->fw_reset_min_dsecs)
2640 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2641 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2642 		if (!bp->fw_reset_max_dsecs)
2643 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2644 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2645 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2646 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2647 			type_str = "Fatal";
2648 			bp->fw_health->fatalities++;
2649 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2650 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2651 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2652 			type_str = "Non-fatal";
2653 			bp->fw_health->survivals++;
2654 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2655 		}
2656 		netif_warn(bp, hw, bp->dev,
2657 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2658 			   type_str, data1, data2,
2659 			   bp->fw_reset_min_dsecs * 100,
2660 			   bp->fw_reset_max_dsecs * 100);
2661 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2662 		break;
2663 	}
2664 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2665 		struct bnxt_fw_health *fw_health = bp->fw_health;
2666 		char *status_desc = "healthy";
2667 		u32 status;
2668 
2669 		if (!fw_health)
2670 			goto async_event_process_exit;
2671 
2672 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2673 			fw_health->enabled = false;
2674 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2675 			break;
2676 		}
2677 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2678 		fw_health->tmr_multiplier =
2679 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2680 				     bp->current_interval * 10);
2681 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2682 		if (!fw_health->enabled)
2683 			fw_health->last_fw_heartbeat =
2684 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2685 		fw_health->last_fw_reset_cnt =
2686 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2687 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2688 		if (status != BNXT_FW_STATUS_HEALTHY)
2689 			status_desc = "unhealthy";
2690 		netif_info(bp, drv, bp->dev,
2691 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2692 			   fw_health->primary ? "primary" : "backup", status,
2693 			   status_desc, fw_health->last_fw_reset_cnt);
2694 		if (!fw_health->enabled) {
2695 			/* Make sure tmr_counter is set and visible to
2696 			 * bnxt_health_check() before setting enabled to true.
2697 			 */
2698 			smp_wmb();
2699 			fw_health->enabled = true;
2700 		}
2701 		goto async_event_process_exit;
2702 	}
2703 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2704 		netif_notice(bp, hw, bp->dev,
2705 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2706 			     data1, data2);
2707 		goto async_event_process_exit;
2708 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2709 		struct bnxt_rx_ring_info *rxr;
2710 		u16 grp_idx;
2711 
2712 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2713 			goto async_event_process_exit;
2714 
2715 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2716 			    BNXT_EVENT_RING_TYPE(data2), data1);
2717 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2718 			goto async_event_process_exit;
2719 
2720 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2721 		if (grp_idx == INVALID_HW_RING_ID) {
2722 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2723 				    data1);
2724 			goto async_event_process_exit;
2725 		}
2726 		rxr = bp->bnapi[grp_idx]->rx_ring;
2727 		bnxt_sched_reset_rxr(bp, rxr);
2728 		goto async_event_process_exit;
2729 	}
2730 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2731 		struct bnxt_fw_health *fw_health = bp->fw_health;
2732 
2733 		netif_notice(bp, hw, bp->dev,
2734 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2735 			     data1, data2);
2736 		if (fw_health) {
2737 			fw_health->echo_req_data1 = data1;
2738 			fw_health->echo_req_data2 = data2;
2739 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2740 			break;
2741 		}
2742 		goto async_event_process_exit;
2743 	}
2744 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2745 		bnxt_ptp_pps_event(bp, data1, data2);
2746 		goto async_event_process_exit;
2747 	}
2748 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2749 		if (bnxt_event_error_report(bp, data1, data2))
2750 			break;
2751 		goto async_event_process_exit;
2752 	}
2753 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2754 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2755 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2756 			if (BNXT_PTP_USE_RTC(bp)) {
2757 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2758 				unsigned long flags;
2759 				u64 ns;
2760 
2761 				if (!ptp)
2762 					goto async_event_process_exit;
2763 
2764 				bnxt_ptp_update_current_time(bp);
2765 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2766 				       BNXT_PHC_BITS) | ptp->current_time);
2767 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2768 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2769 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2770 			}
2771 			break;
2772 		}
2773 		goto async_event_process_exit;
2774 	}
2775 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2776 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2777 
2778 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2779 		goto async_event_process_exit;
2780 	}
2781 	default:
2782 		goto async_event_process_exit;
2783 	}
2784 	__bnxt_queue_sp_work(bp);
2785 async_event_process_exit:
2786 	return 0;
2787 }
2788 
2789 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2790 {
2791 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2792 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2793 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2794 				(struct hwrm_fwd_req_cmpl *)txcmp;
2795 
2796 	switch (cmpl_type) {
2797 	case CMPL_BASE_TYPE_HWRM_DONE:
2798 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2799 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2800 		break;
2801 
2802 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2803 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2804 
2805 		if ((vf_id < bp->pf.first_vf_id) ||
2806 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2807 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2808 				   vf_id);
2809 			return -EINVAL;
2810 		}
2811 
2812 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2813 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2814 		break;
2815 
2816 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2817 		bnxt_async_event_process(bp,
2818 					 (struct hwrm_async_event_cmpl *)txcmp);
2819 		break;
2820 
2821 	default:
2822 		break;
2823 	}
2824 
2825 	return 0;
2826 }
2827 
2828 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2829 {
2830 	struct bnxt_napi *bnapi = dev_instance;
2831 	struct bnxt *bp = bnapi->bp;
2832 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2833 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2834 
2835 	cpr->event_ctr++;
2836 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2837 	napi_schedule(&bnapi->napi);
2838 	return IRQ_HANDLED;
2839 }
2840 
2841 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2842 {
2843 	u32 raw_cons = cpr->cp_raw_cons;
2844 	u16 cons = RING_CMP(raw_cons);
2845 	struct tx_cmp *txcmp;
2846 
2847 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2848 
2849 	return TX_CMP_VALID(txcmp, raw_cons);
2850 }
2851 
2852 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2853 			    int budget)
2854 {
2855 	struct bnxt_napi *bnapi = cpr->bnapi;
2856 	u32 raw_cons = cpr->cp_raw_cons;
2857 	u32 cons;
2858 	int rx_pkts = 0;
2859 	u8 event = 0;
2860 	struct tx_cmp *txcmp;
2861 
2862 	cpr->has_more_work = 0;
2863 	cpr->had_work_done = 1;
2864 	while (1) {
2865 		u8 cmp_type;
2866 		int rc;
2867 
2868 		cons = RING_CMP(raw_cons);
2869 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2870 
2871 		if (!TX_CMP_VALID(txcmp, raw_cons))
2872 			break;
2873 
2874 		/* The valid test of the entry must be done first before
2875 		 * reading any further.
2876 		 */
2877 		dma_rmb();
2878 		cmp_type = TX_CMP_TYPE(txcmp);
2879 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
2880 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
2881 			u32 opaque = txcmp->tx_cmp_opaque;
2882 			struct bnxt_tx_ring_info *txr;
2883 			u16 tx_freed;
2884 
2885 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
2886 			event |= BNXT_TX_CMP_EVENT;
2887 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
2888 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
2889 			else
2890 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
2891 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
2892 				   bp->tx_ring_mask;
2893 			/* return full budget so NAPI will complete. */
2894 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
2895 				rx_pkts = budget;
2896 				raw_cons = NEXT_RAW_CMP(raw_cons);
2897 				if (budget)
2898 					cpr->has_more_work = 1;
2899 				break;
2900 			}
2901 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
2902 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
2903 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
2904 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2905 			if (likely(budget))
2906 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2907 			else
2908 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2909 							   &event);
2910 			if (likely(rc >= 0))
2911 				rx_pkts += rc;
2912 			/* Increment rx_pkts when rc is -ENOMEM to count towards
2913 			 * the NAPI budget.  Otherwise, we may potentially loop
2914 			 * here forever if we consistently cannot allocate
2915 			 * buffers.
2916 			 */
2917 			else if (rc == -ENOMEM && budget)
2918 				rx_pkts++;
2919 			else if (rc == -EBUSY)	/* partial completion */
2920 				break;
2921 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
2922 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
2923 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
2924 			bnxt_hwrm_handler(bp, txcmp);
2925 		}
2926 		raw_cons = NEXT_RAW_CMP(raw_cons);
2927 
2928 		if (rx_pkts && rx_pkts == budget) {
2929 			cpr->has_more_work = 1;
2930 			break;
2931 		}
2932 	}
2933 
2934 	if (event & BNXT_REDIRECT_EVENT) {
2935 		xdp_do_flush();
2936 		event &= ~BNXT_REDIRECT_EVENT;
2937 	}
2938 
2939 	if (event & BNXT_TX_EVENT) {
2940 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
2941 		u16 prod = txr->tx_prod;
2942 
2943 		/* Sync BD data before updating doorbell */
2944 		wmb();
2945 
2946 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
2947 		event &= ~BNXT_TX_EVENT;
2948 	}
2949 
2950 	cpr->cp_raw_cons = raw_cons;
2951 	bnapi->events |= event;
2952 	return rx_pkts;
2953 }
2954 
2955 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
2956 				  int budget)
2957 {
2958 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
2959 		bnapi->tx_int(bp, bnapi, budget);
2960 
2961 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
2962 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2963 
2964 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2965 		bnapi->events &= ~BNXT_RX_EVENT;
2966 	}
2967 	if (bnapi->events & BNXT_AGG_EVENT) {
2968 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2969 
2970 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2971 		bnapi->events &= ~BNXT_AGG_EVENT;
2972 	}
2973 }
2974 
2975 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2976 			  int budget)
2977 {
2978 	struct bnxt_napi *bnapi = cpr->bnapi;
2979 	int rx_pkts;
2980 
2981 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
2982 
2983 	/* ACK completion ring before freeing tx ring and producing new
2984 	 * buffers in rx/agg rings to prevent overflowing the completion
2985 	 * ring.
2986 	 */
2987 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
2988 
2989 	__bnxt_poll_work_done(bp, bnapi, budget);
2990 	return rx_pkts;
2991 }
2992 
2993 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
2994 {
2995 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2996 	struct bnxt *bp = bnapi->bp;
2997 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2998 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2999 	struct tx_cmp *txcmp;
3000 	struct rx_cmp_ext *rxcmp1;
3001 	u32 cp_cons, tmp_raw_cons;
3002 	u32 raw_cons = cpr->cp_raw_cons;
3003 	bool flush_xdp = false;
3004 	u32 rx_pkts = 0;
3005 	u8 event = 0;
3006 
3007 	while (1) {
3008 		int rc;
3009 
3010 		cp_cons = RING_CMP(raw_cons);
3011 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3012 
3013 		if (!TX_CMP_VALID(txcmp, raw_cons))
3014 			break;
3015 
3016 		/* The valid test of the entry must be done first before
3017 		 * reading any further.
3018 		 */
3019 		dma_rmb();
3020 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3021 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3022 			cp_cons = RING_CMP(tmp_raw_cons);
3023 			rxcmp1 = (struct rx_cmp_ext *)
3024 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3025 
3026 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3027 				break;
3028 
3029 			/* force an error to recycle the buffer */
3030 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3031 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3032 
3033 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3034 			if (likely(rc == -EIO) && budget)
3035 				rx_pkts++;
3036 			else if (rc == -EBUSY)	/* partial completion */
3037 				break;
3038 			if (event & BNXT_REDIRECT_EVENT)
3039 				flush_xdp = true;
3040 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3041 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3042 			bnxt_hwrm_handler(bp, txcmp);
3043 		} else {
3044 			netdev_err(bp->dev,
3045 				   "Invalid completion received on special ring\n");
3046 		}
3047 		raw_cons = NEXT_RAW_CMP(raw_cons);
3048 
3049 		if (rx_pkts == budget)
3050 			break;
3051 	}
3052 
3053 	cpr->cp_raw_cons = raw_cons;
3054 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3055 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3056 
3057 	if (event & BNXT_AGG_EVENT)
3058 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3059 	if (flush_xdp)
3060 		xdp_do_flush();
3061 
3062 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3063 		napi_complete_done(napi, rx_pkts);
3064 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3065 	}
3066 	return rx_pkts;
3067 }
3068 
3069 static int bnxt_poll(struct napi_struct *napi, int budget)
3070 {
3071 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3072 	struct bnxt *bp = bnapi->bp;
3073 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3074 	int work_done = 0;
3075 
3076 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3077 		napi_complete(napi);
3078 		return 0;
3079 	}
3080 	while (1) {
3081 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3082 
3083 		if (work_done >= budget) {
3084 			if (!budget)
3085 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3086 			break;
3087 		}
3088 
3089 		if (!bnxt_has_work(bp, cpr)) {
3090 			if (napi_complete_done(napi, work_done))
3091 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3092 			break;
3093 		}
3094 	}
3095 	if (bp->flags & BNXT_FLAG_DIM) {
3096 		struct dim_sample dim_sample = {};
3097 
3098 		dim_update_sample(cpr->event_ctr,
3099 				  cpr->rx_packets,
3100 				  cpr->rx_bytes,
3101 				  &dim_sample);
3102 		net_dim(&cpr->dim, &dim_sample);
3103 	}
3104 	return work_done;
3105 }
3106 
3107 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3108 {
3109 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3110 	int i, work_done = 0;
3111 
3112 	for (i = 0; i < cpr->cp_ring_count; i++) {
3113 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3114 
3115 		if (cpr2->had_nqe_notify) {
3116 			work_done += __bnxt_poll_work(bp, cpr2,
3117 						      budget - work_done);
3118 			cpr->has_more_work |= cpr2->has_more_work;
3119 		}
3120 	}
3121 	return work_done;
3122 }
3123 
3124 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3125 				 u64 dbr_type, int budget)
3126 {
3127 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3128 	int i;
3129 
3130 	for (i = 0; i < cpr->cp_ring_count; i++) {
3131 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3132 		struct bnxt_db_info *db;
3133 
3134 		if (cpr2->had_work_done) {
3135 			u32 tgl = 0;
3136 
3137 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3138 				cpr2->had_nqe_notify = 0;
3139 				tgl = cpr2->toggle;
3140 			}
3141 			db = &cpr2->cp_db;
3142 			bnxt_writeq(bp,
3143 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3144 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3145 				    db->doorbell);
3146 			cpr2->had_work_done = 0;
3147 		}
3148 	}
3149 	__bnxt_poll_work_done(bp, bnapi, budget);
3150 }
3151 
3152 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3153 {
3154 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3155 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3156 	struct bnxt_cp_ring_info *cpr_rx;
3157 	u32 raw_cons = cpr->cp_raw_cons;
3158 	struct bnxt *bp = bnapi->bp;
3159 	struct nqe_cn *nqcmp;
3160 	int work_done = 0;
3161 	u32 cons;
3162 
3163 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3164 		napi_complete(napi);
3165 		return 0;
3166 	}
3167 	if (cpr->has_more_work) {
3168 		cpr->has_more_work = 0;
3169 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3170 	}
3171 	while (1) {
3172 		u16 type;
3173 
3174 		cons = RING_CMP(raw_cons);
3175 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3176 
3177 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3178 			if (cpr->has_more_work)
3179 				break;
3180 
3181 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3182 					     budget);
3183 			cpr->cp_raw_cons = raw_cons;
3184 			if (napi_complete_done(napi, work_done))
3185 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3186 						  cpr->cp_raw_cons);
3187 			goto poll_done;
3188 		}
3189 
3190 		/* The valid test of the entry must be done first before
3191 		 * reading any further.
3192 		 */
3193 		dma_rmb();
3194 
3195 		type = le16_to_cpu(nqcmp->type);
3196 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3197 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3198 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3199 			struct bnxt_cp_ring_info *cpr2;
3200 
3201 			/* No more budget for RX work */
3202 			if (budget && work_done >= budget &&
3203 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3204 				break;
3205 
3206 			idx = BNXT_NQ_HDL_IDX(idx);
3207 			cpr2 = &cpr->cp_ring_arr[idx];
3208 			cpr2->had_nqe_notify = 1;
3209 			cpr2->toggle = NQE_CN_TOGGLE(type);
3210 			work_done += __bnxt_poll_work(bp, cpr2,
3211 						      budget - work_done);
3212 			cpr->has_more_work |= cpr2->has_more_work;
3213 		} else {
3214 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3215 		}
3216 		raw_cons = NEXT_RAW_CMP(raw_cons);
3217 	}
3218 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3219 	if (raw_cons != cpr->cp_raw_cons) {
3220 		cpr->cp_raw_cons = raw_cons;
3221 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3222 	}
3223 poll_done:
3224 	cpr_rx = &cpr->cp_ring_arr[0];
3225 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3226 	    (bp->flags & BNXT_FLAG_DIM)) {
3227 		struct dim_sample dim_sample = {};
3228 
3229 		dim_update_sample(cpr->event_ctr,
3230 				  cpr_rx->rx_packets,
3231 				  cpr_rx->rx_bytes,
3232 				  &dim_sample);
3233 		net_dim(&cpr->dim, &dim_sample);
3234 	}
3235 	return work_done;
3236 }
3237 
3238 static void bnxt_free_tx_skbs(struct bnxt *bp)
3239 {
3240 	int i, max_idx;
3241 	struct pci_dev *pdev = bp->pdev;
3242 
3243 	if (!bp->tx_ring)
3244 		return;
3245 
3246 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3247 	for (i = 0; i < bp->tx_nr_rings; i++) {
3248 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3249 		int j;
3250 
3251 		if (!txr->tx_buf_ring)
3252 			continue;
3253 
3254 		for (j = 0; j < max_idx;) {
3255 			struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
3256 			struct sk_buff *skb;
3257 			int k, last;
3258 
3259 			if (i < bp->tx_nr_rings_xdp &&
3260 			    tx_buf->action == XDP_REDIRECT) {
3261 				dma_unmap_single(&pdev->dev,
3262 					dma_unmap_addr(tx_buf, mapping),
3263 					dma_unmap_len(tx_buf, len),
3264 					DMA_TO_DEVICE);
3265 				xdp_return_frame(tx_buf->xdpf);
3266 				tx_buf->action = 0;
3267 				tx_buf->xdpf = NULL;
3268 				j++;
3269 				continue;
3270 			}
3271 
3272 			skb = tx_buf->skb;
3273 			if (!skb) {
3274 				j++;
3275 				continue;
3276 			}
3277 
3278 			tx_buf->skb = NULL;
3279 
3280 			if (tx_buf->is_push) {
3281 				dev_kfree_skb(skb);
3282 				j += 2;
3283 				continue;
3284 			}
3285 
3286 			dma_unmap_single(&pdev->dev,
3287 					 dma_unmap_addr(tx_buf, mapping),
3288 					 skb_headlen(skb),
3289 					 DMA_TO_DEVICE);
3290 
3291 			last = tx_buf->nr_frags;
3292 			j += 2;
3293 			for (k = 0; k < last; k++, j++) {
3294 				int ring_idx = j & bp->tx_ring_mask;
3295 				skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
3296 
3297 				tx_buf = &txr->tx_buf_ring[ring_idx];
3298 				dma_unmap_page(
3299 					&pdev->dev,
3300 					dma_unmap_addr(tx_buf, mapping),
3301 					skb_frag_size(frag), DMA_TO_DEVICE);
3302 			}
3303 			dev_kfree_skb(skb);
3304 		}
3305 		netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
3306 	}
3307 }
3308 
3309 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3310 {
3311 	struct pci_dev *pdev = bp->pdev;
3312 	int i, max_idx;
3313 
3314 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3315 
3316 	for (i = 0; i < max_idx; i++) {
3317 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3318 		dma_addr_t mapping = rx_buf->mapping;
3319 		void *data = rx_buf->data;
3320 
3321 		if (!data)
3322 			continue;
3323 
3324 		rx_buf->data = NULL;
3325 		if (BNXT_RX_PAGE_MODE(bp)) {
3326 			page_pool_recycle_direct(rxr->page_pool, data);
3327 		} else {
3328 			dma_unmap_single_attrs(&pdev->dev, mapping,
3329 					       bp->rx_buf_use_size, bp->rx_dir,
3330 					       DMA_ATTR_WEAK_ORDERING);
3331 			skb_free_frag(data);
3332 		}
3333 	}
3334 }
3335 
3336 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3337 {
3338 	int i, max_idx;
3339 
3340 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3341 
3342 	for (i = 0; i < max_idx; i++) {
3343 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3344 		struct page *page = rx_agg_buf->page;
3345 
3346 		if (!page)
3347 			continue;
3348 
3349 		rx_agg_buf->page = NULL;
3350 		__clear_bit(i, rxr->rx_agg_bmap);
3351 
3352 		page_pool_recycle_direct(rxr->page_pool, page);
3353 	}
3354 }
3355 
3356 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, int ring_nr)
3357 {
3358 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
3359 	struct pci_dev *pdev = bp->pdev;
3360 	struct bnxt_tpa_idx_map *map;
3361 	int i;
3362 
3363 	if (!rxr->rx_tpa)
3364 		goto skip_rx_tpa_free;
3365 
3366 	for (i = 0; i < bp->max_tpa; i++) {
3367 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3368 		u8 *data = tpa_info->data;
3369 
3370 		if (!data)
3371 			continue;
3372 
3373 		dma_unmap_single_attrs(&pdev->dev, tpa_info->mapping,
3374 				       bp->rx_buf_use_size, bp->rx_dir,
3375 				       DMA_ATTR_WEAK_ORDERING);
3376 
3377 		tpa_info->data = NULL;
3378 
3379 		skb_free_frag(data);
3380 	}
3381 
3382 skip_rx_tpa_free:
3383 	if (!rxr->rx_buf_ring)
3384 		goto skip_rx_buf_free;
3385 
3386 	bnxt_free_one_rx_ring(bp, rxr);
3387 
3388 skip_rx_buf_free:
3389 	if (!rxr->rx_agg_ring)
3390 		goto skip_rx_agg_free;
3391 
3392 	bnxt_free_one_rx_agg_ring(bp, rxr);
3393 
3394 skip_rx_agg_free:
3395 	map = rxr->rx_tpa_idx_map;
3396 	if (map)
3397 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3398 }
3399 
3400 static void bnxt_free_rx_skbs(struct bnxt *bp)
3401 {
3402 	int i;
3403 
3404 	if (!bp->rx_ring)
3405 		return;
3406 
3407 	for (i = 0; i < bp->rx_nr_rings; i++)
3408 		bnxt_free_one_rx_ring_skbs(bp, i);
3409 }
3410 
3411 static void bnxt_free_skbs(struct bnxt *bp)
3412 {
3413 	bnxt_free_tx_skbs(bp);
3414 	bnxt_free_rx_skbs(bp);
3415 }
3416 
3417 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3418 {
3419 	u8 init_val = ctxm->init_value;
3420 	u16 offset = ctxm->init_offset;
3421 	u8 *p2 = p;
3422 	int i;
3423 
3424 	if (!init_val)
3425 		return;
3426 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3427 		memset(p, init_val, len);
3428 		return;
3429 	}
3430 	for (i = 0; i < len; i += ctxm->entry_size)
3431 		*(p2 + i + offset) = init_val;
3432 }
3433 
3434 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3435 {
3436 	struct pci_dev *pdev = bp->pdev;
3437 	int i;
3438 
3439 	if (!rmem->pg_arr)
3440 		goto skip_pages;
3441 
3442 	for (i = 0; i < rmem->nr_pages; i++) {
3443 		if (!rmem->pg_arr[i])
3444 			continue;
3445 
3446 		dma_free_coherent(&pdev->dev, rmem->page_size,
3447 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3448 
3449 		rmem->pg_arr[i] = NULL;
3450 	}
3451 skip_pages:
3452 	if (rmem->pg_tbl) {
3453 		size_t pg_tbl_size = rmem->nr_pages * 8;
3454 
3455 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3456 			pg_tbl_size = rmem->page_size;
3457 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3458 				  rmem->pg_tbl, rmem->pg_tbl_map);
3459 		rmem->pg_tbl = NULL;
3460 	}
3461 	if (rmem->vmem_size && *rmem->vmem) {
3462 		vfree(*rmem->vmem);
3463 		*rmem->vmem = NULL;
3464 	}
3465 }
3466 
3467 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3468 {
3469 	struct pci_dev *pdev = bp->pdev;
3470 	u64 valid_bit = 0;
3471 	int i;
3472 
3473 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3474 		valid_bit = PTU_PTE_VALID;
3475 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3476 		size_t pg_tbl_size = rmem->nr_pages * 8;
3477 
3478 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3479 			pg_tbl_size = rmem->page_size;
3480 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3481 						  &rmem->pg_tbl_map,
3482 						  GFP_KERNEL);
3483 		if (!rmem->pg_tbl)
3484 			return -ENOMEM;
3485 	}
3486 
3487 	for (i = 0; i < rmem->nr_pages; i++) {
3488 		u64 extra_bits = valid_bit;
3489 
3490 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3491 						     rmem->page_size,
3492 						     &rmem->dma_arr[i],
3493 						     GFP_KERNEL);
3494 		if (!rmem->pg_arr[i])
3495 			return -ENOMEM;
3496 
3497 		if (rmem->ctx_mem)
3498 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3499 					  rmem->page_size);
3500 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3501 			if (i == rmem->nr_pages - 2 &&
3502 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3503 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3504 			else if (i == rmem->nr_pages - 1 &&
3505 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3506 				extra_bits |= PTU_PTE_LAST;
3507 			rmem->pg_tbl[i] =
3508 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3509 		}
3510 	}
3511 
3512 	if (rmem->vmem_size) {
3513 		*rmem->vmem = vzalloc(rmem->vmem_size);
3514 		if (!(*rmem->vmem))
3515 			return -ENOMEM;
3516 	}
3517 	return 0;
3518 }
3519 
3520 static void bnxt_free_tpa_info(struct bnxt *bp)
3521 {
3522 	int i, j;
3523 
3524 	for (i = 0; i < bp->rx_nr_rings; i++) {
3525 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3526 
3527 		kfree(rxr->rx_tpa_idx_map);
3528 		rxr->rx_tpa_idx_map = NULL;
3529 		if (rxr->rx_tpa) {
3530 			for (j = 0; j < bp->max_tpa; j++) {
3531 				kfree(rxr->rx_tpa[j].agg_arr);
3532 				rxr->rx_tpa[j].agg_arr = NULL;
3533 			}
3534 		}
3535 		kfree(rxr->rx_tpa);
3536 		rxr->rx_tpa = NULL;
3537 	}
3538 }
3539 
3540 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3541 {
3542 	int i, j;
3543 
3544 	bp->max_tpa = MAX_TPA;
3545 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3546 		if (!bp->max_tpa_v2)
3547 			return 0;
3548 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3549 	}
3550 
3551 	for (i = 0; i < bp->rx_nr_rings; i++) {
3552 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3553 		struct rx_agg_cmp *agg;
3554 
3555 		rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3556 				      GFP_KERNEL);
3557 		if (!rxr->rx_tpa)
3558 			return -ENOMEM;
3559 
3560 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3561 			continue;
3562 		for (j = 0; j < bp->max_tpa; j++) {
3563 			agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3564 			if (!agg)
3565 				return -ENOMEM;
3566 			rxr->rx_tpa[j].agg_arr = agg;
3567 		}
3568 		rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3569 					      GFP_KERNEL);
3570 		if (!rxr->rx_tpa_idx_map)
3571 			return -ENOMEM;
3572 	}
3573 	return 0;
3574 }
3575 
3576 static void bnxt_free_rx_rings(struct bnxt *bp)
3577 {
3578 	int i;
3579 
3580 	if (!bp->rx_ring)
3581 		return;
3582 
3583 	bnxt_free_tpa_info(bp);
3584 	for (i = 0; i < bp->rx_nr_rings; i++) {
3585 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3586 		struct bnxt_ring_struct *ring;
3587 
3588 		if (rxr->xdp_prog)
3589 			bpf_prog_put(rxr->xdp_prog);
3590 
3591 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3592 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3593 
3594 		page_pool_destroy(rxr->page_pool);
3595 		rxr->page_pool = NULL;
3596 
3597 		kfree(rxr->rx_agg_bmap);
3598 		rxr->rx_agg_bmap = NULL;
3599 
3600 		ring = &rxr->rx_ring_struct;
3601 		bnxt_free_ring(bp, &ring->ring_mem);
3602 
3603 		ring = &rxr->rx_agg_ring_struct;
3604 		bnxt_free_ring(bp, &ring->ring_mem);
3605 	}
3606 }
3607 
3608 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3609 				   struct bnxt_rx_ring_info *rxr,
3610 				   int numa_node)
3611 {
3612 	struct page_pool_params pp = { 0 };
3613 
3614 	pp.pool_size = bp->rx_agg_ring_size;
3615 	if (BNXT_RX_PAGE_MODE(bp))
3616 		pp.pool_size += bp->rx_ring_size;
3617 	pp.nid = numa_node;
3618 	pp.napi = &rxr->bnapi->napi;
3619 	pp.netdev = bp->dev;
3620 	pp.dev = &bp->pdev->dev;
3621 	pp.dma_dir = bp->rx_dir;
3622 	pp.max_len = PAGE_SIZE;
3623 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3624 
3625 	rxr->page_pool = page_pool_create(&pp);
3626 	if (IS_ERR(rxr->page_pool)) {
3627 		int err = PTR_ERR(rxr->page_pool);
3628 
3629 		rxr->page_pool = NULL;
3630 		return err;
3631 	}
3632 	return 0;
3633 }
3634 
3635 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3636 {
3637 	int numa_node = dev_to_node(&bp->pdev->dev);
3638 	int i, rc = 0, agg_rings = 0, cpu;
3639 
3640 	if (!bp->rx_ring)
3641 		return -ENOMEM;
3642 
3643 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3644 		agg_rings = 1;
3645 
3646 	for (i = 0; i < bp->rx_nr_rings; i++) {
3647 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3648 		struct bnxt_ring_struct *ring;
3649 		int cpu_node;
3650 
3651 		ring = &rxr->rx_ring_struct;
3652 
3653 		cpu = cpumask_local_spread(i, numa_node);
3654 		cpu_node = cpu_to_node(cpu);
3655 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3656 			   i, cpu_node);
3657 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3658 		if (rc)
3659 			return rc;
3660 
3661 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3662 		if (rc < 0)
3663 			return rc;
3664 
3665 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3666 						MEM_TYPE_PAGE_POOL,
3667 						rxr->page_pool);
3668 		if (rc) {
3669 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3670 			return rc;
3671 		}
3672 
3673 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3674 		if (rc)
3675 			return rc;
3676 
3677 		ring->grp_idx = i;
3678 		if (agg_rings) {
3679 			u16 mem_size;
3680 
3681 			ring = &rxr->rx_agg_ring_struct;
3682 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3683 			if (rc)
3684 				return rc;
3685 
3686 			ring->grp_idx = i;
3687 			rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3688 			mem_size = rxr->rx_agg_bmap_size / 8;
3689 			rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3690 			if (!rxr->rx_agg_bmap)
3691 				return -ENOMEM;
3692 		}
3693 	}
3694 	if (bp->flags & BNXT_FLAG_TPA)
3695 		rc = bnxt_alloc_tpa_info(bp);
3696 	return rc;
3697 }
3698 
3699 static void bnxt_free_tx_rings(struct bnxt *bp)
3700 {
3701 	int i;
3702 	struct pci_dev *pdev = bp->pdev;
3703 
3704 	if (!bp->tx_ring)
3705 		return;
3706 
3707 	for (i = 0; i < bp->tx_nr_rings; i++) {
3708 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3709 		struct bnxt_ring_struct *ring;
3710 
3711 		if (txr->tx_push) {
3712 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3713 					  txr->tx_push, txr->tx_push_mapping);
3714 			txr->tx_push = NULL;
3715 		}
3716 
3717 		ring = &txr->tx_ring_struct;
3718 
3719 		bnxt_free_ring(bp, &ring->ring_mem);
3720 	}
3721 }
3722 
3723 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3724 	((tc) * (bp)->tx_nr_rings_per_tc)
3725 
3726 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3727 	((tx) % (bp)->tx_nr_rings_per_tc)
3728 
3729 #define BNXT_RING_TO_TC(bp, tx)		\
3730 	((tx) / (bp)->tx_nr_rings_per_tc)
3731 
3732 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3733 {
3734 	int i, j, rc;
3735 	struct pci_dev *pdev = bp->pdev;
3736 
3737 	bp->tx_push_size = 0;
3738 	if (bp->tx_push_thresh) {
3739 		int push_size;
3740 
3741 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3742 					bp->tx_push_thresh);
3743 
3744 		if (push_size > 256) {
3745 			push_size = 0;
3746 			bp->tx_push_thresh = 0;
3747 		}
3748 
3749 		bp->tx_push_size = push_size;
3750 	}
3751 
3752 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3753 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3754 		struct bnxt_ring_struct *ring;
3755 		u8 qidx;
3756 
3757 		ring = &txr->tx_ring_struct;
3758 
3759 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3760 		if (rc)
3761 			return rc;
3762 
3763 		ring->grp_idx = txr->bnapi->index;
3764 		if (bp->tx_push_size) {
3765 			dma_addr_t mapping;
3766 
3767 			/* One pre-allocated DMA buffer to backup
3768 			 * TX push operation
3769 			 */
3770 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3771 						bp->tx_push_size,
3772 						&txr->tx_push_mapping,
3773 						GFP_KERNEL);
3774 
3775 			if (!txr->tx_push)
3776 				return -ENOMEM;
3777 
3778 			mapping = txr->tx_push_mapping +
3779 				sizeof(struct tx_push_bd);
3780 			txr->data_mapping = cpu_to_le64(mapping);
3781 		}
3782 		qidx = bp->tc_to_qidx[j];
3783 		ring->queue_id = bp->q_info[qidx].queue_id;
3784 		spin_lock_init(&txr->xdp_tx_lock);
3785 		if (i < bp->tx_nr_rings_xdp)
3786 			continue;
3787 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
3788 			j++;
3789 	}
3790 	return 0;
3791 }
3792 
3793 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
3794 {
3795 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3796 
3797 	kfree(cpr->cp_desc_ring);
3798 	cpr->cp_desc_ring = NULL;
3799 	ring->ring_mem.pg_arr = NULL;
3800 	kfree(cpr->cp_desc_mapping);
3801 	cpr->cp_desc_mapping = NULL;
3802 	ring->ring_mem.dma_arr = NULL;
3803 }
3804 
3805 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
3806 {
3807 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
3808 	if (!cpr->cp_desc_ring)
3809 		return -ENOMEM;
3810 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
3811 				       GFP_KERNEL);
3812 	if (!cpr->cp_desc_mapping)
3813 		return -ENOMEM;
3814 	return 0;
3815 }
3816 
3817 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
3818 {
3819 	int i;
3820 
3821 	if (!bp->bnapi)
3822 		return;
3823 	for (i = 0; i < bp->cp_nr_rings; i++) {
3824 		struct bnxt_napi *bnapi = bp->bnapi[i];
3825 
3826 		if (!bnapi)
3827 			continue;
3828 		bnxt_free_cp_arrays(&bnapi->cp_ring);
3829 	}
3830 }
3831 
3832 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
3833 {
3834 	int i, n = bp->cp_nr_pages;
3835 
3836 	for (i = 0; i < bp->cp_nr_rings; i++) {
3837 		struct bnxt_napi *bnapi = bp->bnapi[i];
3838 		int rc;
3839 
3840 		if (!bnapi)
3841 			continue;
3842 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
3843 		if (rc)
3844 			return rc;
3845 	}
3846 	return 0;
3847 }
3848 
3849 static void bnxt_free_cp_rings(struct bnxt *bp)
3850 {
3851 	int i;
3852 
3853 	if (!bp->bnapi)
3854 		return;
3855 
3856 	for (i = 0; i < bp->cp_nr_rings; i++) {
3857 		struct bnxt_napi *bnapi = bp->bnapi[i];
3858 		struct bnxt_cp_ring_info *cpr;
3859 		struct bnxt_ring_struct *ring;
3860 		int j;
3861 
3862 		if (!bnapi)
3863 			continue;
3864 
3865 		cpr = &bnapi->cp_ring;
3866 		ring = &cpr->cp_ring_struct;
3867 
3868 		bnxt_free_ring(bp, &ring->ring_mem);
3869 
3870 		if (!cpr->cp_ring_arr)
3871 			continue;
3872 
3873 		for (j = 0; j < cpr->cp_ring_count; j++) {
3874 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
3875 
3876 			ring = &cpr2->cp_ring_struct;
3877 			bnxt_free_ring(bp, &ring->ring_mem);
3878 			bnxt_free_cp_arrays(cpr2);
3879 		}
3880 		kfree(cpr->cp_ring_arr);
3881 		cpr->cp_ring_arr = NULL;
3882 		cpr->cp_ring_count = 0;
3883 	}
3884 }
3885 
3886 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
3887 				  struct bnxt_cp_ring_info *cpr)
3888 {
3889 	struct bnxt_ring_mem_info *rmem;
3890 	struct bnxt_ring_struct *ring;
3891 	int rc;
3892 
3893 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
3894 	if (rc) {
3895 		bnxt_free_cp_arrays(cpr);
3896 		return -ENOMEM;
3897 	}
3898 	ring = &cpr->cp_ring_struct;
3899 	rmem = &ring->ring_mem;
3900 	rmem->nr_pages = bp->cp_nr_pages;
3901 	rmem->page_size = HW_CMPD_RING_SIZE;
3902 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
3903 	rmem->dma_arr = cpr->cp_desc_mapping;
3904 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
3905 	rc = bnxt_alloc_ring(bp, rmem);
3906 	if (rc) {
3907 		bnxt_free_ring(bp, rmem);
3908 		bnxt_free_cp_arrays(cpr);
3909 	}
3910 	return rc;
3911 }
3912 
3913 static int bnxt_alloc_cp_rings(struct bnxt *bp)
3914 {
3915 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
3916 	int i, j, rc, ulp_msix;
3917 	int tcs = bp->num_tc;
3918 
3919 	if (!tcs)
3920 		tcs = 1;
3921 	ulp_msix = bnxt_get_ulp_msix_num(bp);
3922 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
3923 		struct bnxt_napi *bnapi = bp->bnapi[i];
3924 		struct bnxt_cp_ring_info *cpr, *cpr2;
3925 		struct bnxt_ring_struct *ring;
3926 		int cp_count = 0, k;
3927 		int rx = 0, tx = 0;
3928 
3929 		if (!bnapi)
3930 			continue;
3931 
3932 		cpr = &bnapi->cp_ring;
3933 		cpr->bnapi = bnapi;
3934 		ring = &cpr->cp_ring_struct;
3935 
3936 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3937 		if (rc)
3938 			return rc;
3939 
3940 		ring->map_idx = ulp_msix + i;
3941 
3942 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3943 			continue;
3944 
3945 		if (i < bp->rx_nr_rings) {
3946 			cp_count++;
3947 			rx = 1;
3948 		}
3949 		if (i < bp->tx_nr_rings_xdp) {
3950 			cp_count++;
3951 			tx = 1;
3952 		} else if ((sh && i < bp->tx_nr_rings) ||
3953 			 (!sh && i >= bp->rx_nr_rings)) {
3954 			cp_count += tcs;
3955 			tx = 1;
3956 		}
3957 
3958 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
3959 					   GFP_KERNEL);
3960 		if (!cpr->cp_ring_arr)
3961 			return -ENOMEM;
3962 		cpr->cp_ring_count = cp_count;
3963 
3964 		for (k = 0; k < cp_count; k++) {
3965 			cpr2 = &cpr->cp_ring_arr[k];
3966 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
3967 			if (rc)
3968 				return rc;
3969 			cpr2->bnapi = bnapi;
3970 			cpr2->sw_stats = cpr->sw_stats;
3971 			cpr2->cp_idx = k;
3972 			if (!k && rx) {
3973 				bp->rx_ring[i].rx_cpr = cpr2;
3974 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
3975 			} else {
3976 				int n, tc = k - rx;
3977 
3978 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
3979 				bp->tx_ring[n].tx_cpr = cpr2;
3980 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
3981 			}
3982 		}
3983 		if (tx)
3984 			j++;
3985 	}
3986 	return 0;
3987 }
3988 
3989 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
3990 				     struct bnxt_rx_ring_info *rxr)
3991 {
3992 	struct bnxt_ring_mem_info *rmem;
3993 	struct bnxt_ring_struct *ring;
3994 
3995 	ring = &rxr->rx_ring_struct;
3996 	rmem = &ring->ring_mem;
3997 	rmem->nr_pages = bp->rx_nr_pages;
3998 	rmem->page_size = HW_RXBD_RING_SIZE;
3999 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4000 	rmem->dma_arr = rxr->rx_desc_mapping;
4001 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4002 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4003 
4004 	ring = &rxr->rx_agg_ring_struct;
4005 	rmem = &ring->ring_mem;
4006 	rmem->nr_pages = bp->rx_agg_nr_pages;
4007 	rmem->page_size = HW_RXBD_RING_SIZE;
4008 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4009 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4010 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4011 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4012 }
4013 
4014 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4015 				      struct bnxt_rx_ring_info *rxr)
4016 {
4017 	struct bnxt_ring_mem_info *rmem;
4018 	struct bnxt_ring_struct *ring;
4019 	int i;
4020 
4021 	rxr->page_pool->p.napi = NULL;
4022 	rxr->page_pool = NULL;
4023 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4024 
4025 	ring = &rxr->rx_ring_struct;
4026 	rmem = &ring->ring_mem;
4027 	rmem->pg_tbl = NULL;
4028 	rmem->pg_tbl_map = 0;
4029 	for (i = 0; i < rmem->nr_pages; i++) {
4030 		rmem->pg_arr[i] = NULL;
4031 		rmem->dma_arr[i] = 0;
4032 	}
4033 	*rmem->vmem = NULL;
4034 
4035 	ring = &rxr->rx_agg_ring_struct;
4036 	rmem = &ring->ring_mem;
4037 	rmem->pg_tbl = NULL;
4038 	rmem->pg_tbl_map = 0;
4039 	for (i = 0; i < rmem->nr_pages; i++) {
4040 		rmem->pg_arr[i] = NULL;
4041 		rmem->dma_arr[i] = 0;
4042 	}
4043 	*rmem->vmem = NULL;
4044 }
4045 
4046 static void bnxt_init_ring_struct(struct bnxt *bp)
4047 {
4048 	int i, j;
4049 
4050 	for (i = 0; i < bp->cp_nr_rings; i++) {
4051 		struct bnxt_napi *bnapi = bp->bnapi[i];
4052 		struct bnxt_ring_mem_info *rmem;
4053 		struct bnxt_cp_ring_info *cpr;
4054 		struct bnxt_rx_ring_info *rxr;
4055 		struct bnxt_tx_ring_info *txr;
4056 		struct bnxt_ring_struct *ring;
4057 
4058 		if (!bnapi)
4059 			continue;
4060 
4061 		cpr = &bnapi->cp_ring;
4062 		ring = &cpr->cp_ring_struct;
4063 		rmem = &ring->ring_mem;
4064 		rmem->nr_pages = bp->cp_nr_pages;
4065 		rmem->page_size = HW_CMPD_RING_SIZE;
4066 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4067 		rmem->dma_arr = cpr->cp_desc_mapping;
4068 		rmem->vmem_size = 0;
4069 
4070 		rxr = bnapi->rx_ring;
4071 		if (!rxr)
4072 			goto skip_rx;
4073 
4074 		ring = &rxr->rx_ring_struct;
4075 		rmem = &ring->ring_mem;
4076 		rmem->nr_pages = bp->rx_nr_pages;
4077 		rmem->page_size = HW_RXBD_RING_SIZE;
4078 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4079 		rmem->dma_arr = rxr->rx_desc_mapping;
4080 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4081 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4082 
4083 		ring = &rxr->rx_agg_ring_struct;
4084 		rmem = &ring->ring_mem;
4085 		rmem->nr_pages = bp->rx_agg_nr_pages;
4086 		rmem->page_size = HW_RXBD_RING_SIZE;
4087 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4088 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4089 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4090 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4091 
4092 skip_rx:
4093 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4094 			ring = &txr->tx_ring_struct;
4095 			rmem = &ring->ring_mem;
4096 			rmem->nr_pages = bp->tx_nr_pages;
4097 			rmem->page_size = HW_TXBD_RING_SIZE;
4098 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4099 			rmem->dma_arr = txr->tx_desc_mapping;
4100 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4101 			rmem->vmem = (void **)&txr->tx_buf_ring;
4102 		}
4103 	}
4104 }
4105 
4106 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4107 {
4108 	int i;
4109 	u32 prod;
4110 	struct rx_bd **rx_buf_ring;
4111 
4112 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4113 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4114 		int j;
4115 		struct rx_bd *rxbd;
4116 
4117 		rxbd = rx_buf_ring[i];
4118 		if (!rxbd)
4119 			continue;
4120 
4121 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4122 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4123 			rxbd->rx_bd_opaque = prod;
4124 		}
4125 	}
4126 }
4127 
4128 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4129 				       struct bnxt_rx_ring_info *rxr,
4130 				       int ring_nr)
4131 {
4132 	u32 prod;
4133 	int i;
4134 
4135 	prod = rxr->rx_prod;
4136 	for (i = 0; i < bp->rx_ring_size; i++) {
4137 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4138 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4139 				    ring_nr, i, bp->rx_ring_size);
4140 			break;
4141 		}
4142 		prod = NEXT_RX(prod);
4143 	}
4144 	rxr->rx_prod = prod;
4145 }
4146 
4147 static void bnxt_alloc_one_rx_ring_page(struct bnxt *bp,
4148 					struct bnxt_rx_ring_info *rxr,
4149 					int ring_nr)
4150 {
4151 	u32 prod;
4152 	int i;
4153 
4154 	prod = rxr->rx_agg_prod;
4155 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4156 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
4157 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4158 				    ring_nr, i, bp->rx_ring_size);
4159 			break;
4160 		}
4161 		prod = NEXT_RX_AGG(prod);
4162 	}
4163 	rxr->rx_agg_prod = prod;
4164 }
4165 
4166 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4167 {
4168 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4169 	int i;
4170 
4171 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4172 
4173 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4174 		return 0;
4175 
4176 	bnxt_alloc_one_rx_ring_page(bp, rxr, ring_nr);
4177 
4178 	if (rxr->rx_tpa) {
4179 		dma_addr_t mapping;
4180 		u8 *data;
4181 
4182 		for (i = 0; i < bp->max_tpa; i++) {
4183 			data = __bnxt_alloc_rx_frag(bp, &mapping, GFP_KERNEL);
4184 			if (!data)
4185 				return -ENOMEM;
4186 
4187 			rxr->rx_tpa[i].data = data;
4188 			rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4189 			rxr->rx_tpa[i].mapping = mapping;
4190 		}
4191 	}
4192 	return 0;
4193 }
4194 
4195 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4196 				       struct bnxt_rx_ring_info *rxr)
4197 {
4198 	struct bnxt_ring_struct *ring;
4199 	u32 type;
4200 
4201 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4202 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4203 
4204 	if (NET_IP_ALIGN == 2)
4205 		type |= RX_BD_FLAGS_SOP;
4206 
4207 	ring = &rxr->rx_ring_struct;
4208 	bnxt_init_rxbd_pages(ring, type);
4209 	ring->fw_ring_id = INVALID_HW_RING_ID;
4210 }
4211 
4212 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4213 					   struct bnxt_rx_ring_info *rxr)
4214 {
4215 	struct bnxt_ring_struct *ring;
4216 	u32 type;
4217 
4218 	ring = &rxr->rx_agg_ring_struct;
4219 	ring->fw_ring_id = INVALID_HW_RING_ID;
4220 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4221 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4222 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4223 
4224 		bnxt_init_rxbd_pages(ring, type);
4225 	}
4226 }
4227 
4228 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4229 {
4230 	struct bnxt_rx_ring_info *rxr;
4231 
4232 	rxr = &bp->rx_ring[ring_nr];
4233 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4234 
4235 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4236 			     &rxr->bnapi->napi);
4237 
4238 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4239 		bpf_prog_add(bp->xdp_prog, 1);
4240 		rxr->xdp_prog = bp->xdp_prog;
4241 	}
4242 
4243 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4244 
4245 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4246 }
4247 
4248 static void bnxt_init_cp_rings(struct bnxt *bp)
4249 {
4250 	int i, j;
4251 
4252 	for (i = 0; i < bp->cp_nr_rings; i++) {
4253 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4254 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4255 
4256 		ring->fw_ring_id = INVALID_HW_RING_ID;
4257 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4258 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4259 		if (!cpr->cp_ring_arr)
4260 			continue;
4261 		for (j = 0; j < cpr->cp_ring_count; j++) {
4262 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4263 
4264 			ring = &cpr2->cp_ring_struct;
4265 			ring->fw_ring_id = INVALID_HW_RING_ID;
4266 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4267 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4268 		}
4269 	}
4270 }
4271 
4272 static int bnxt_init_rx_rings(struct bnxt *bp)
4273 {
4274 	int i, rc = 0;
4275 
4276 	if (BNXT_RX_PAGE_MODE(bp)) {
4277 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4278 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4279 	} else {
4280 		bp->rx_offset = BNXT_RX_OFFSET;
4281 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4282 	}
4283 
4284 	for (i = 0; i < bp->rx_nr_rings; i++) {
4285 		rc = bnxt_init_one_rx_ring(bp, i);
4286 		if (rc)
4287 			break;
4288 	}
4289 
4290 	return rc;
4291 }
4292 
4293 static int bnxt_init_tx_rings(struct bnxt *bp)
4294 {
4295 	u16 i;
4296 
4297 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4298 				   BNXT_MIN_TX_DESC_CNT);
4299 
4300 	for (i = 0; i < bp->tx_nr_rings; i++) {
4301 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4302 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4303 
4304 		ring->fw_ring_id = INVALID_HW_RING_ID;
4305 
4306 		if (i >= bp->tx_nr_rings_xdp)
4307 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4308 					     NETDEV_QUEUE_TYPE_TX,
4309 					     &txr->bnapi->napi);
4310 	}
4311 
4312 	return 0;
4313 }
4314 
4315 static void bnxt_free_ring_grps(struct bnxt *bp)
4316 {
4317 	kfree(bp->grp_info);
4318 	bp->grp_info = NULL;
4319 }
4320 
4321 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4322 {
4323 	int i;
4324 
4325 	if (irq_re_init) {
4326 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4327 				       sizeof(struct bnxt_ring_grp_info),
4328 				       GFP_KERNEL);
4329 		if (!bp->grp_info)
4330 			return -ENOMEM;
4331 	}
4332 	for (i = 0; i < bp->cp_nr_rings; i++) {
4333 		if (irq_re_init)
4334 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4335 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4336 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4337 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4338 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4339 	}
4340 	return 0;
4341 }
4342 
4343 static void bnxt_free_vnics(struct bnxt *bp)
4344 {
4345 	kfree(bp->vnic_info);
4346 	bp->vnic_info = NULL;
4347 	bp->nr_vnics = 0;
4348 }
4349 
4350 static int bnxt_alloc_vnics(struct bnxt *bp)
4351 {
4352 	int num_vnics = 1;
4353 
4354 #ifdef CONFIG_RFS_ACCEL
4355 	if (bp->flags & BNXT_FLAG_RFS) {
4356 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4357 			num_vnics++;
4358 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4359 			num_vnics += bp->rx_nr_rings;
4360 	}
4361 #endif
4362 
4363 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4364 		num_vnics++;
4365 
4366 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4367 				GFP_KERNEL);
4368 	if (!bp->vnic_info)
4369 		return -ENOMEM;
4370 
4371 	bp->nr_vnics = num_vnics;
4372 	return 0;
4373 }
4374 
4375 static void bnxt_init_vnics(struct bnxt *bp)
4376 {
4377 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4378 	int i;
4379 
4380 	for (i = 0; i < bp->nr_vnics; i++) {
4381 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4382 		int j;
4383 
4384 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4385 		vnic->vnic_id = i;
4386 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4387 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4388 
4389 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4390 
4391 		if (bp->vnic_info[i].rss_hash_key) {
4392 			if (i == BNXT_VNIC_DEFAULT) {
4393 				u8 *key = (void *)vnic->rss_hash_key;
4394 				int k;
4395 
4396 				if (!bp->rss_hash_key_valid &&
4397 				    !bp->rss_hash_key_updated) {
4398 					get_random_bytes(bp->rss_hash_key,
4399 							 HW_HASH_KEY_SIZE);
4400 					bp->rss_hash_key_updated = true;
4401 				}
4402 
4403 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4404 				       HW_HASH_KEY_SIZE);
4405 
4406 				if (!bp->rss_hash_key_updated)
4407 					continue;
4408 
4409 				bp->rss_hash_key_updated = false;
4410 				bp->rss_hash_key_valid = true;
4411 
4412 				bp->toeplitz_prefix = 0;
4413 				for (k = 0; k < 8; k++) {
4414 					bp->toeplitz_prefix <<= 8;
4415 					bp->toeplitz_prefix |= key[k];
4416 				}
4417 			} else {
4418 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4419 				       HW_HASH_KEY_SIZE);
4420 			}
4421 		}
4422 	}
4423 }
4424 
4425 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4426 {
4427 	int pages;
4428 
4429 	pages = ring_size / desc_per_pg;
4430 
4431 	if (!pages)
4432 		return 1;
4433 
4434 	pages++;
4435 
4436 	while (pages & (pages - 1))
4437 		pages++;
4438 
4439 	return pages;
4440 }
4441 
4442 void bnxt_set_tpa_flags(struct bnxt *bp)
4443 {
4444 	bp->flags &= ~BNXT_FLAG_TPA;
4445 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4446 		return;
4447 	if (bp->dev->features & NETIF_F_LRO)
4448 		bp->flags |= BNXT_FLAG_LRO;
4449 	else if (bp->dev->features & NETIF_F_GRO_HW)
4450 		bp->flags |= BNXT_FLAG_GRO;
4451 }
4452 
4453 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4454  * be set on entry.
4455  */
4456 void bnxt_set_ring_params(struct bnxt *bp)
4457 {
4458 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4459 	u32 agg_factor = 0, agg_ring_size = 0;
4460 
4461 	/* 8 for CRC and VLAN */
4462 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4463 
4464 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4465 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4466 
4467 	bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
4468 	ring_size = bp->rx_ring_size;
4469 	bp->rx_agg_ring_size = 0;
4470 	bp->rx_agg_nr_pages = 0;
4471 
4472 	if (bp->flags & BNXT_FLAG_TPA)
4473 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4474 
4475 	bp->flags &= ~BNXT_FLAG_JUMBO;
4476 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4477 		u32 jumbo_factor;
4478 
4479 		bp->flags |= BNXT_FLAG_JUMBO;
4480 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4481 		if (jumbo_factor > agg_factor)
4482 			agg_factor = jumbo_factor;
4483 	}
4484 	if (agg_factor) {
4485 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4486 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4487 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4488 				    bp->rx_ring_size, ring_size);
4489 			bp->rx_ring_size = ring_size;
4490 		}
4491 		agg_ring_size = ring_size * agg_factor;
4492 
4493 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4494 							RX_DESC_CNT);
4495 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4496 			u32 tmp = agg_ring_size;
4497 
4498 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4499 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4500 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4501 				    tmp, agg_ring_size);
4502 		}
4503 		bp->rx_agg_ring_size = agg_ring_size;
4504 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4505 
4506 		if (BNXT_RX_PAGE_MODE(bp)) {
4507 			rx_space = PAGE_SIZE;
4508 			rx_size = PAGE_SIZE -
4509 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4510 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4511 		} else {
4512 			rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
4513 			rx_space = rx_size + NET_SKB_PAD +
4514 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4515 		}
4516 	}
4517 
4518 	bp->rx_buf_use_size = rx_size;
4519 	bp->rx_buf_size = rx_space;
4520 
4521 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4522 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4523 
4524 	ring_size = bp->tx_ring_size;
4525 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4526 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4527 
4528 	max_rx_cmpl = bp->rx_ring_size;
4529 	/* MAX TPA needs to be added because TPA_START completions are
4530 	 * immediately recycled, so the TPA completions are not bound by
4531 	 * the RX ring size.
4532 	 */
4533 	if (bp->flags & BNXT_FLAG_TPA)
4534 		max_rx_cmpl += bp->max_tpa;
4535 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4536 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4537 	bp->cp_ring_size = ring_size;
4538 
4539 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4540 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4541 		bp->cp_nr_pages = MAX_CP_PAGES;
4542 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4543 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4544 			    ring_size, bp->cp_ring_size);
4545 	}
4546 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4547 	bp->cp_ring_mask = bp->cp_bit - 1;
4548 }
4549 
4550 /* Changing allocation mode of RX rings.
4551  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4552  */
4553 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4554 {
4555 	struct net_device *dev = bp->dev;
4556 
4557 	if (page_mode) {
4558 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
4559 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4560 
4561 		if (bp->xdp_prog->aux->xdp_has_frags)
4562 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4563 		else
4564 			dev->max_mtu =
4565 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4566 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4567 			bp->flags |= BNXT_FLAG_JUMBO;
4568 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4569 		} else {
4570 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4571 			bp->rx_skb_func = bnxt_rx_page_skb;
4572 		}
4573 		bp->rx_dir = DMA_BIDIRECTIONAL;
4574 		/* Disable LRO or GRO_HW */
4575 		netdev_update_features(dev);
4576 	} else {
4577 		dev->max_mtu = bp->max_mtu;
4578 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4579 		bp->rx_dir = DMA_FROM_DEVICE;
4580 		bp->rx_skb_func = bnxt_rx_skb;
4581 	}
4582 	return 0;
4583 }
4584 
4585 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4586 {
4587 	int i;
4588 	struct bnxt_vnic_info *vnic;
4589 	struct pci_dev *pdev = bp->pdev;
4590 
4591 	if (!bp->vnic_info)
4592 		return;
4593 
4594 	for (i = 0; i < bp->nr_vnics; i++) {
4595 		vnic = &bp->vnic_info[i];
4596 
4597 		kfree(vnic->fw_grp_ids);
4598 		vnic->fw_grp_ids = NULL;
4599 
4600 		kfree(vnic->uc_list);
4601 		vnic->uc_list = NULL;
4602 
4603 		if (vnic->mc_list) {
4604 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4605 					  vnic->mc_list, vnic->mc_list_mapping);
4606 			vnic->mc_list = NULL;
4607 		}
4608 
4609 		if (vnic->rss_table) {
4610 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4611 					  vnic->rss_table,
4612 					  vnic->rss_table_dma_addr);
4613 			vnic->rss_table = NULL;
4614 		}
4615 
4616 		vnic->rss_hash_key = NULL;
4617 		vnic->flags = 0;
4618 	}
4619 }
4620 
4621 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4622 {
4623 	int i, rc = 0, size;
4624 	struct bnxt_vnic_info *vnic;
4625 	struct pci_dev *pdev = bp->pdev;
4626 	int max_rings;
4627 
4628 	for (i = 0; i < bp->nr_vnics; i++) {
4629 		vnic = &bp->vnic_info[i];
4630 
4631 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4632 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4633 
4634 			if (mem_size > 0) {
4635 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4636 				if (!vnic->uc_list) {
4637 					rc = -ENOMEM;
4638 					goto out;
4639 				}
4640 			}
4641 		}
4642 
4643 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4644 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4645 			vnic->mc_list =
4646 				dma_alloc_coherent(&pdev->dev,
4647 						   vnic->mc_list_size,
4648 						   &vnic->mc_list_mapping,
4649 						   GFP_KERNEL);
4650 			if (!vnic->mc_list) {
4651 				rc = -ENOMEM;
4652 				goto out;
4653 			}
4654 		}
4655 
4656 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4657 			goto vnic_skip_grps;
4658 
4659 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4660 			max_rings = bp->rx_nr_rings;
4661 		else
4662 			max_rings = 1;
4663 
4664 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4665 		if (!vnic->fw_grp_ids) {
4666 			rc = -ENOMEM;
4667 			goto out;
4668 		}
4669 vnic_skip_grps:
4670 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4671 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4672 			continue;
4673 
4674 		/* Allocate rss table and hash key */
4675 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4676 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4677 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4678 
4679 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4680 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4681 						     vnic->rss_table_size,
4682 						     &vnic->rss_table_dma_addr,
4683 						     GFP_KERNEL);
4684 		if (!vnic->rss_table) {
4685 			rc = -ENOMEM;
4686 			goto out;
4687 		}
4688 
4689 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4690 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4691 	}
4692 	return 0;
4693 
4694 out:
4695 	return rc;
4696 }
4697 
4698 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4699 {
4700 	struct bnxt_hwrm_wait_token *token;
4701 
4702 	dma_pool_destroy(bp->hwrm_dma_pool);
4703 	bp->hwrm_dma_pool = NULL;
4704 
4705 	rcu_read_lock();
4706 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4707 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4708 	rcu_read_unlock();
4709 }
4710 
4711 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4712 {
4713 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4714 					    BNXT_HWRM_DMA_SIZE,
4715 					    BNXT_HWRM_DMA_ALIGN, 0);
4716 	if (!bp->hwrm_dma_pool)
4717 		return -ENOMEM;
4718 
4719 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4720 
4721 	return 0;
4722 }
4723 
4724 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4725 {
4726 	kfree(stats->hw_masks);
4727 	stats->hw_masks = NULL;
4728 	kfree(stats->sw_stats);
4729 	stats->sw_stats = NULL;
4730 	if (stats->hw_stats) {
4731 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
4732 				  stats->hw_stats_map);
4733 		stats->hw_stats = NULL;
4734 	}
4735 }
4736 
4737 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
4738 				bool alloc_masks)
4739 {
4740 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
4741 					     &stats->hw_stats_map, GFP_KERNEL);
4742 	if (!stats->hw_stats)
4743 		return -ENOMEM;
4744 
4745 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
4746 	if (!stats->sw_stats)
4747 		goto stats_mem_err;
4748 
4749 	if (alloc_masks) {
4750 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
4751 		if (!stats->hw_masks)
4752 			goto stats_mem_err;
4753 	}
4754 	return 0;
4755 
4756 stats_mem_err:
4757 	bnxt_free_stats_mem(bp, stats);
4758 	return -ENOMEM;
4759 }
4760 
4761 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
4762 {
4763 	int i;
4764 
4765 	for (i = 0; i < count; i++)
4766 		mask_arr[i] = mask;
4767 }
4768 
4769 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
4770 {
4771 	int i;
4772 
4773 	for (i = 0; i < count; i++)
4774 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
4775 }
4776 
4777 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
4778 				    struct bnxt_stats_mem *stats)
4779 {
4780 	struct hwrm_func_qstats_ext_output *resp;
4781 	struct hwrm_func_qstats_ext_input *req;
4782 	__le64 *hw_masks;
4783 	int rc;
4784 
4785 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
4786 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4787 		return -EOPNOTSUPP;
4788 
4789 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
4790 	if (rc)
4791 		return rc;
4792 
4793 	req->fid = cpu_to_le16(0xffff);
4794 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4795 
4796 	resp = hwrm_req_hold(bp, req);
4797 	rc = hwrm_req_send(bp, req);
4798 	if (!rc) {
4799 		hw_masks = &resp->rx_ucast_pkts;
4800 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
4801 	}
4802 	hwrm_req_drop(bp, req);
4803 	return rc;
4804 }
4805 
4806 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
4807 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
4808 
4809 static void bnxt_init_stats(struct bnxt *bp)
4810 {
4811 	struct bnxt_napi *bnapi = bp->bnapi[0];
4812 	struct bnxt_cp_ring_info *cpr;
4813 	struct bnxt_stats_mem *stats;
4814 	__le64 *rx_stats, *tx_stats;
4815 	int rc, rx_count, tx_count;
4816 	u64 *rx_masks, *tx_masks;
4817 	u64 mask;
4818 	u8 flags;
4819 
4820 	cpr = &bnapi->cp_ring;
4821 	stats = &cpr->stats;
4822 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
4823 	if (rc) {
4824 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4825 			mask = (1ULL << 48) - 1;
4826 		else
4827 			mask = -1ULL;
4828 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
4829 	}
4830 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
4831 		stats = &bp->port_stats;
4832 		rx_stats = stats->hw_stats;
4833 		rx_masks = stats->hw_masks;
4834 		rx_count = sizeof(struct rx_port_stats) / 8;
4835 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4836 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4837 		tx_count = sizeof(struct tx_port_stats) / 8;
4838 
4839 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
4840 		rc = bnxt_hwrm_port_qstats(bp, flags);
4841 		if (rc) {
4842 			mask = (1ULL << 40) - 1;
4843 
4844 			bnxt_fill_masks(rx_masks, mask, rx_count);
4845 			bnxt_fill_masks(tx_masks, mask, tx_count);
4846 		} else {
4847 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4848 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
4849 			bnxt_hwrm_port_qstats(bp, 0);
4850 		}
4851 	}
4852 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
4853 		stats = &bp->rx_port_stats_ext;
4854 		rx_stats = stats->hw_stats;
4855 		rx_masks = stats->hw_masks;
4856 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
4857 		stats = &bp->tx_port_stats_ext;
4858 		tx_stats = stats->hw_stats;
4859 		tx_masks = stats->hw_masks;
4860 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
4861 
4862 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4863 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
4864 		if (rc) {
4865 			mask = (1ULL << 40) - 1;
4866 
4867 			bnxt_fill_masks(rx_masks, mask, rx_count);
4868 			if (tx_stats)
4869 				bnxt_fill_masks(tx_masks, mask, tx_count);
4870 		} else {
4871 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4872 			if (tx_stats)
4873 				bnxt_copy_hw_masks(tx_masks, tx_stats,
4874 						   tx_count);
4875 			bnxt_hwrm_port_qstats_ext(bp, 0);
4876 		}
4877 	}
4878 }
4879 
4880 static void bnxt_free_port_stats(struct bnxt *bp)
4881 {
4882 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
4883 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
4884 
4885 	bnxt_free_stats_mem(bp, &bp->port_stats);
4886 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
4887 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
4888 }
4889 
4890 static void bnxt_free_ring_stats(struct bnxt *bp)
4891 {
4892 	int i;
4893 
4894 	if (!bp->bnapi)
4895 		return;
4896 
4897 	for (i = 0; i < bp->cp_nr_rings; i++) {
4898 		struct bnxt_napi *bnapi = bp->bnapi[i];
4899 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4900 
4901 		bnxt_free_stats_mem(bp, &cpr->stats);
4902 
4903 		kfree(cpr->sw_stats);
4904 		cpr->sw_stats = NULL;
4905 	}
4906 }
4907 
4908 static int bnxt_alloc_stats(struct bnxt *bp)
4909 {
4910 	u32 size, i;
4911 	int rc;
4912 
4913 	size = bp->hw_ring_stats_size;
4914 
4915 	for (i = 0; i < bp->cp_nr_rings; i++) {
4916 		struct bnxt_napi *bnapi = bp->bnapi[i];
4917 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4918 
4919 		cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL);
4920 		if (!cpr->sw_stats)
4921 			return -ENOMEM;
4922 
4923 		cpr->stats.len = size;
4924 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
4925 		if (rc)
4926 			return rc;
4927 
4928 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4929 	}
4930 
4931 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
4932 		return 0;
4933 
4934 	if (bp->port_stats.hw_stats)
4935 		goto alloc_ext_stats;
4936 
4937 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
4938 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
4939 	if (rc)
4940 		return rc;
4941 
4942 	bp->flags |= BNXT_FLAG_PORT_STATS;
4943 
4944 alloc_ext_stats:
4945 	/* Display extended statistics only if FW supports it */
4946 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
4947 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
4948 			return 0;
4949 
4950 	if (bp->rx_port_stats_ext.hw_stats)
4951 		goto alloc_tx_ext_stats;
4952 
4953 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
4954 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
4955 	/* Extended stats are optional */
4956 	if (rc)
4957 		return 0;
4958 
4959 alloc_tx_ext_stats:
4960 	if (bp->tx_port_stats_ext.hw_stats)
4961 		return 0;
4962 
4963 	if (bp->hwrm_spec_code >= 0x10902 ||
4964 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
4965 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
4966 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
4967 		/* Extended stats are optional */
4968 		if (rc)
4969 			return 0;
4970 	}
4971 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
4972 	return 0;
4973 }
4974 
4975 static void bnxt_clear_ring_indices(struct bnxt *bp)
4976 {
4977 	int i, j;
4978 
4979 	if (!bp->bnapi)
4980 		return;
4981 
4982 	for (i = 0; i < bp->cp_nr_rings; i++) {
4983 		struct bnxt_napi *bnapi = bp->bnapi[i];
4984 		struct bnxt_cp_ring_info *cpr;
4985 		struct bnxt_rx_ring_info *rxr;
4986 		struct bnxt_tx_ring_info *txr;
4987 
4988 		if (!bnapi)
4989 			continue;
4990 
4991 		cpr = &bnapi->cp_ring;
4992 		cpr->cp_raw_cons = 0;
4993 
4994 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4995 			txr->tx_prod = 0;
4996 			txr->tx_cons = 0;
4997 			txr->tx_hw_cons = 0;
4998 		}
4999 
5000 		rxr = bnapi->rx_ring;
5001 		if (rxr) {
5002 			rxr->rx_prod = 0;
5003 			rxr->rx_agg_prod = 0;
5004 			rxr->rx_sw_agg_prod = 0;
5005 			rxr->rx_next_cons = 0;
5006 		}
5007 		bnapi->events = 0;
5008 	}
5009 }
5010 
5011 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5012 {
5013 	u8 type = fltr->type, flags = fltr->flags;
5014 
5015 	INIT_LIST_HEAD(&fltr->list);
5016 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5017 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5018 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5019 }
5020 
5021 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5022 {
5023 	if (!list_empty(&fltr->list))
5024 		list_del_init(&fltr->list);
5025 }
5026 
5027 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5028 {
5029 	struct bnxt_filter_base *usr_fltr, *tmp;
5030 
5031 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5032 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5033 			continue;
5034 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5035 	}
5036 }
5037 
5038 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5039 {
5040 	hlist_del(&fltr->hash);
5041 	bnxt_del_one_usr_fltr(bp, fltr);
5042 	if (fltr->flags) {
5043 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5044 		bp->ntp_fltr_count--;
5045 	}
5046 	kfree(fltr);
5047 }
5048 
5049 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5050 {
5051 	int i;
5052 
5053 	/* Under rtnl_lock and all our NAPIs have been disabled.  It's
5054 	 * safe to delete the hash table.
5055 	 */
5056 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5057 		struct hlist_head *head;
5058 		struct hlist_node *tmp;
5059 		struct bnxt_ntuple_filter *fltr;
5060 
5061 		head = &bp->ntp_fltr_hash_tbl[i];
5062 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5063 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5064 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5065 				     !list_empty(&fltr->base.list)))
5066 				continue;
5067 			bnxt_del_fltr(bp, &fltr->base);
5068 		}
5069 	}
5070 	if (!all)
5071 		return;
5072 
5073 	bitmap_free(bp->ntp_fltr_bmap);
5074 	bp->ntp_fltr_bmap = NULL;
5075 	bp->ntp_fltr_count = 0;
5076 }
5077 
5078 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5079 {
5080 	int i, rc = 0;
5081 
5082 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5083 		return 0;
5084 
5085 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5086 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5087 
5088 	bp->ntp_fltr_count = 0;
5089 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5090 
5091 	if (!bp->ntp_fltr_bmap)
5092 		rc = -ENOMEM;
5093 
5094 	return rc;
5095 }
5096 
5097 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5098 {
5099 	int i;
5100 
5101 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5102 		struct hlist_head *head;
5103 		struct hlist_node *tmp;
5104 		struct bnxt_l2_filter *fltr;
5105 
5106 		head = &bp->l2_fltr_hash_tbl[i];
5107 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5108 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5109 				     !list_empty(&fltr->base.list)))
5110 				continue;
5111 			bnxt_del_fltr(bp, &fltr->base);
5112 		}
5113 	}
5114 }
5115 
5116 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5117 {
5118 	int i;
5119 
5120 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5121 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5122 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5123 }
5124 
5125 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5126 {
5127 	bnxt_free_vnic_attributes(bp);
5128 	bnxt_free_tx_rings(bp);
5129 	bnxt_free_rx_rings(bp);
5130 	bnxt_free_cp_rings(bp);
5131 	bnxt_free_all_cp_arrays(bp);
5132 	bnxt_free_ntp_fltrs(bp, false);
5133 	bnxt_free_l2_filters(bp, false);
5134 	if (irq_re_init) {
5135 		bnxt_free_ring_stats(bp);
5136 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5137 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5138 			bnxt_free_port_stats(bp);
5139 		bnxt_free_ring_grps(bp);
5140 		bnxt_free_vnics(bp);
5141 		kfree(bp->tx_ring_map);
5142 		bp->tx_ring_map = NULL;
5143 		kfree(bp->tx_ring);
5144 		bp->tx_ring = NULL;
5145 		kfree(bp->rx_ring);
5146 		bp->rx_ring = NULL;
5147 		kfree(bp->bnapi);
5148 		bp->bnapi = NULL;
5149 	} else {
5150 		bnxt_clear_ring_indices(bp);
5151 	}
5152 }
5153 
5154 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5155 {
5156 	int i, j, rc, size, arr_size;
5157 	void *bnapi;
5158 
5159 	if (irq_re_init) {
5160 		/* Allocate bnapi mem pointer array and mem block for
5161 		 * all queues
5162 		 */
5163 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5164 				bp->cp_nr_rings);
5165 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5166 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5167 		if (!bnapi)
5168 			return -ENOMEM;
5169 
5170 		bp->bnapi = bnapi;
5171 		bnapi += arr_size;
5172 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5173 			bp->bnapi[i] = bnapi;
5174 			bp->bnapi[i]->index = i;
5175 			bp->bnapi[i]->bp = bp;
5176 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5177 				struct bnxt_cp_ring_info *cpr =
5178 					&bp->bnapi[i]->cp_ring;
5179 
5180 				cpr->cp_ring_struct.ring_mem.flags =
5181 					BNXT_RMEM_RING_PTE_FLAG;
5182 			}
5183 		}
5184 
5185 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5186 				      sizeof(struct bnxt_rx_ring_info),
5187 				      GFP_KERNEL);
5188 		if (!bp->rx_ring)
5189 			return -ENOMEM;
5190 
5191 		for (i = 0; i < bp->rx_nr_rings; i++) {
5192 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5193 
5194 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5195 				rxr->rx_ring_struct.ring_mem.flags =
5196 					BNXT_RMEM_RING_PTE_FLAG;
5197 				rxr->rx_agg_ring_struct.ring_mem.flags =
5198 					BNXT_RMEM_RING_PTE_FLAG;
5199 			} else {
5200 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5201 			}
5202 			rxr->bnapi = bp->bnapi[i];
5203 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5204 		}
5205 
5206 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5207 				      sizeof(struct bnxt_tx_ring_info),
5208 				      GFP_KERNEL);
5209 		if (!bp->tx_ring)
5210 			return -ENOMEM;
5211 
5212 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5213 					  GFP_KERNEL);
5214 
5215 		if (!bp->tx_ring_map)
5216 			return -ENOMEM;
5217 
5218 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5219 			j = 0;
5220 		else
5221 			j = bp->rx_nr_rings;
5222 
5223 		for (i = 0; i < bp->tx_nr_rings; i++) {
5224 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5225 			struct bnxt_napi *bnapi2;
5226 
5227 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5228 				txr->tx_ring_struct.ring_mem.flags =
5229 					BNXT_RMEM_RING_PTE_FLAG;
5230 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5231 			if (i >= bp->tx_nr_rings_xdp) {
5232 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5233 
5234 				bnapi2 = bp->bnapi[k];
5235 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5236 				txr->tx_napi_idx =
5237 					BNXT_RING_TO_TC(bp, txr->txq_index);
5238 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5239 				bnapi2->tx_int = bnxt_tx_int;
5240 			} else {
5241 				bnapi2 = bp->bnapi[j];
5242 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5243 				bnapi2->tx_ring[0] = txr;
5244 				bnapi2->tx_int = bnxt_tx_int_xdp;
5245 				j++;
5246 			}
5247 			txr->bnapi = bnapi2;
5248 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5249 				txr->tx_cpr = &bnapi2->cp_ring;
5250 		}
5251 
5252 		rc = bnxt_alloc_stats(bp);
5253 		if (rc)
5254 			goto alloc_mem_err;
5255 		bnxt_init_stats(bp);
5256 
5257 		rc = bnxt_alloc_ntp_fltrs(bp);
5258 		if (rc)
5259 			goto alloc_mem_err;
5260 
5261 		rc = bnxt_alloc_vnics(bp);
5262 		if (rc)
5263 			goto alloc_mem_err;
5264 	}
5265 
5266 	rc = bnxt_alloc_all_cp_arrays(bp);
5267 	if (rc)
5268 		goto alloc_mem_err;
5269 
5270 	bnxt_init_ring_struct(bp);
5271 
5272 	rc = bnxt_alloc_rx_rings(bp);
5273 	if (rc)
5274 		goto alloc_mem_err;
5275 
5276 	rc = bnxt_alloc_tx_rings(bp);
5277 	if (rc)
5278 		goto alloc_mem_err;
5279 
5280 	rc = bnxt_alloc_cp_rings(bp);
5281 	if (rc)
5282 		goto alloc_mem_err;
5283 
5284 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5285 						  BNXT_VNIC_MCAST_FLAG |
5286 						  BNXT_VNIC_UCAST_FLAG;
5287 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5288 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5289 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5290 
5291 	rc = bnxt_alloc_vnic_attributes(bp);
5292 	if (rc)
5293 		goto alloc_mem_err;
5294 	return 0;
5295 
5296 alloc_mem_err:
5297 	bnxt_free_mem(bp, true);
5298 	return rc;
5299 }
5300 
5301 static void bnxt_disable_int(struct bnxt *bp)
5302 {
5303 	int i;
5304 
5305 	if (!bp->bnapi)
5306 		return;
5307 
5308 	for (i = 0; i < bp->cp_nr_rings; i++) {
5309 		struct bnxt_napi *bnapi = bp->bnapi[i];
5310 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5311 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5312 
5313 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5314 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5315 	}
5316 }
5317 
5318 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5319 {
5320 	struct bnxt_napi *bnapi = bp->bnapi[n];
5321 	struct bnxt_cp_ring_info *cpr;
5322 
5323 	cpr = &bnapi->cp_ring;
5324 	return cpr->cp_ring_struct.map_idx;
5325 }
5326 
5327 static void bnxt_disable_int_sync(struct bnxt *bp)
5328 {
5329 	int i;
5330 
5331 	if (!bp->irq_tbl)
5332 		return;
5333 
5334 	atomic_inc(&bp->intr_sem);
5335 
5336 	bnxt_disable_int(bp);
5337 	for (i = 0; i < bp->cp_nr_rings; i++) {
5338 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5339 
5340 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5341 	}
5342 }
5343 
5344 static void bnxt_enable_int(struct bnxt *bp)
5345 {
5346 	int i;
5347 
5348 	atomic_set(&bp->intr_sem, 0);
5349 	for (i = 0; i < bp->cp_nr_rings; i++) {
5350 		struct bnxt_napi *bnapi = bp->bnapi[i];
5351 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5352 
5353 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5354 	}
5355 }
5356 
5357 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5358 			    bool async_only)
5359 {
5360 	DECLARE_BITMAP(async_events_bmap, 256);
5361 	u32 *events = (u32 *)async_events_bmap;
5362 	struct hwrm_func_drv_rgtr_output *resp;
5363 	struct hwrm_func_drv_rgtr_input *req;
5364 	u32 flags;
5365 	int rc, i;
5366 
5367 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5368 	if (rc)
5369 		return rc;
5370 
5371 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5372 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5373 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5374 
5375 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5376 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5377 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5378 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5379 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5380 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5381 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5382 	req->flags = cpu_to_le32(flags);
5383 	req->ver_maj_8b = DRV_VER_MAJ;
5384 	req->ver_min_8b = DRV_VER_MIN;
5385 	req->ver_upd_8b = DRV_VER_UPD;
5386 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5387 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5388 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5389 
5390 	if (BNXT_PF(bp)) {
5391 		u32 data[8];
5392 		int i;
5393 
5394 		memset(data, 0, sizeof(data));
5395 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5396 			u16 cmd = bnxt_vf_req_snif[i];
5397 			unsigned int bit, idx;
5398 
5399 			idx = cmd / 32;
5400 			bit = cmd % 32;
5401 			data[idx] |= 1 << bit;
5402 		}
5403 
5404 		for (i = 0; i < 8; i++)
5405 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5406 
5407 		req->enables |=
5408 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5409 	}
5410 
5411 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5412 		req->flags |= cpu_to_le32(
5413 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5414 
5415 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5416 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5417 		u16 event_id = bnxt_async_events_arr[i];
5418 
5419 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5420 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5421 			continue;
5422 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5423 		    !bp->ptp_cfg)
5424 			continue;
5425 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5426 	}
5427 	if (bmap && bmap_size) {
5428 		for (i = 0; i < bmap_size; i++) {
5429 			if (test_bit(i, bmap))
5430 				__set_bit(i, async_events_bmap);
5431 		}
5432 	}
5433 	for (i = 0; i < 8; i++)
5434 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5435 
5436 	if (async_only)
5437 		req->enables =
5438 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5439 
5440 	resp = hwrm_req_hold(bp, req);
5441 	rc = hwrm_req_send(bp, req);
5442 	if (!rc) {
5443 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5444 		if (resp->flags &
5445 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5446 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5447 	}
5448 	hwrm_req_drop(bp, req);
5449 	return rc;
5450 }
5451 
5452 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5453 {
5454 	struct hwrm_func_drv_unrgtr_input *req;
5455 	int rc;
5456 
5457 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5458 		return 0;
5459 
5460 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5461 	if (rc)
5462 		return rc;
5463 	return hwrm_req_send(bp, req);
5464 }
5465 
5466 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5467 
5468 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5469 {
5470 	struct hwrm_tunnel_dst_port_free_input *req;
5471 	int rc;
5472 
5473 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5474 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5475 		return 0;
5476 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5477 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5478 		return 0;
5479 
5480 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5481 	if (rc)
5482 		return rc;
5483 
5484 	req->tunnel_type = tunnel_type;
5485 
5486 	switch (tunnel_type) {
5487 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5488 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5489 		bp->vxlan_port = 0;
5490 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5491 		break;
5492 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5493 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5494 		bp->nge_port = 0;
5495 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5496 		break;
5497 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5498 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5499 		bp->vxlan_gpe_port = 0;
5500 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5501 		break;
5502 	default:
5503 		break;
5504 	}
5505 
5506 	rc = hwrm_req_send(bp, req);
5507 	if (rc)
5508 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5509 			   rc);
5510 	if (bp->flags & BNXT_FLAG_TPA)
5511 		bnxt_set_tpa(bp, true);
5512 	return rc;
5513 }
5514 
5515 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5516 					   u8 tunnel_type)
5517 {
5518 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5519 	struct hwrm_tunnel_dst_port_alloc_input *req;
5520 	int rc;
5521 
5522 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5523 	if (rc)
5524 		return rc;
5525 
5526 	req->tunnel_type = tunnel_type;
5527 	req->tunnel_dst_port_val = port;
5528 
5529 	resp = hwrm_req_hold(bp, req);
5530 	rc = hwrm_req_send(bp, req);
5531 	if (rc) {
5532 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5533 			   rc);
5534 		goto err_out;
5535 	}
5536 
5537 	switch (tunnel_type) {
5538 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5539 		bp->vxlan_port = port;
5540 		bp->vxlan_fw_dst_port_id =
5541 			le16_to_cpu(resp->tunnel_dst_port_id);
5542 		break;
5543 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5544 		bp->nge_port = port;
5545 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5546 		break;
5547 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5548 		bp->vxlan_gpe_port = port;
5549 		bp->vxlan_gpe_fw_dst_port_id =
5550 			le16_to_cpu(resp->tunnel_dst_port_id);
5551 		break;
5552 	default:
5553 		break;
5554 	}
5555 	if (bp->flags & BNXT_FLAG_TPA)
5556 		bnxt_set_tpa(bp, true);
5557 
5558 err_out:
5559 	hwrm_req_drop(bp, req);
5560 	return rc;
5561 }
5562 
5563 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5564 {
5565 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5566 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5567 	int rc;
5568 
5569 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5570 	if (rc)
5571 		return rc;
5572 
5573 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5574 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5575 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5576 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5577 	}
5578 	req->mask = cpu_to_le32(vnic->rx_mask);
5579 	return hwrm_req_send_silent(bp, req);
5580 }
5581 
5582 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5583 {
5584 	if (!atomic_dec_and_test(&fltr->refcnt))
5585 		return;
5586 	spin_lock_bh(&bp->ntp_fltr_lock);
5587 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5588 		spin_unlock_bh(&bp->ntp_fltr_lock);
5589 		return;
5590 	}
5591 	hlist_del_rcu(&fltr->base.hash);
5592 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5593 	if (fltr->base.flags) {
5594 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5595 		bp->ntp_fltr_count--;
5596 	}
5597 	spin_unlock_bh(&bp->ntp_fltr_lock);
5598 	kfree_rcu(fltr, base.rcu);
5599 }
5600 
5601 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5602 						      struct bnxt_l2_key *key,
5603 						      u32 idx)
5604 {
5605 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5606 	struct bnxt_l2_filter *fltr;
5607 
5608 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5609 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5610 
5611 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5612 		    l2_key->vlan == key->vlan)
5613 			return fltr;
5614 	}
5615 	return NULL;
5616 }
5617 
5618 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5619 						    struct bnxt_l2_key *key,
5620 						    u32 idx)
5621 {
5622 	struct bnxt_l2_filter *fltr = NULL;
5623 
5624 	rcu_read_lock();
5625 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5626 	if (fltr)
5627 		atomic_inc(&fltr->refcnt);
5628 	rcu_read_unlock();
5629 	return fltr;
5630 }
5631 
5632 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5633 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5634 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5635 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5636 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5637 
5638 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5639 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5640 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5641 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5642 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5643 
5644 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5645 {
5646 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5647 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5648 			return sizeof(fkeys->addrs.v4addrs) +
5649 			       sizeof(fkeys->ports);
5650 
5651 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5652 			return sizeof(fkeys->addrs.v4addrs);
5653 	}
5654 
5655 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5656 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5657 			return sizeof(fkeys->addrs.v6addrs) +
5658 			       sizeof(fkeys->ports);
5659 
5660 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5661 			return sizeof(fkeys->addrs.v6addrs);
5662 	}
5663 
5664 	return 0;
5665 }
5666 
5667 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5668 			 const unsigned char *key)
5669 {
5670 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5671 	struct bnxt_ipv4_tuple tuple4;
5672 	struct bnxt_ipv6_tuple tuple6;
5673 	int i, j, len = 0;
5674 	u8 *four_tuple;
5675 
5676 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5677 	if (!len)
5678 		return 0;
5679 
5680 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5681 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5682 		tuple4.ports = fkeys->ports;
5683 		four_tuple = (unsigned char *)&tuple4;
5684 	} else {
5685 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5686 		tuple6.ports = fkeys->ports;
5687 		four_tuple = (unsigned char *)&tuple6;
5688 	}
5689 
5690 	for (i = 0, j = 8; i < len; i++, j++) {
5691 		u8 byte = four_tuple[i];
5692 		int bit;
5693 
5694 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5695 			if (byte & 0x80)
5696 				hash ^= prefix;
5697 		}
5698 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5699 	}
5700 
5701 	/* The valid part of the hash is in the upper 32 bits. */
5702 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5703 }
5704 
5705 #ifdef CONFIG_RFS_ACCEL
5706 static struct bnxt_l2_filter *
5707 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5708 {
5709 	struct bnxt_l2_filter *fltr;
5710 	u32 idx;
5711 
5712 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5713 	      BNXT_L2_FLTR_HASH_MASK;
5714 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5715 	return fltr;
5716 }
5717 #endif
5718 
5719 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
5720 			       struct bnxt_l2_key *key, u32 idx)
5721 {
5722 	struct hlist_head *head;
5723 
5724 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
5725 	fltr->l2_key.vlan = key->vlan;
5726 	fltr->base.type = BNXT_FLTR_TYPE_L2;
5727 	if (fltr->base.flags) {
5728 		int bit_id;
5729 
5730 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
5731 						 bp->max_fltr, 0);
5732 		if (bit_id < 0)
5733 			return -ENOMEM;
5734 		fltr->base.sw_id = (u16)bit_id;
5735 		bp->ntp_fltr_count++;
5736 	}
5737 	head = &bp->l2_fltr_hash_tbl[idx];
5738 	hlist_add_head_rcu(&fltr->base.hash, head);
5739 	bnxt_insert_usr_fltr(bp, &fltr->base);
5740 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
5741 	atomic_set(&fltr->refcnt, 1);
5742 	return 0;
5743 }
5744 
5745 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
5746 						   struct bnxt_l2_key *key,
5747 						   gfp_t gfp)
5748 {
5749 	struct bnxt_l2_filter *fltr;
5750 	u32 idx;
5751 	int rc;
5752 
5753 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5754 	      BNXT_L2_FLTR_HASH_MASK;
5755 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5756 	if (fltr)
5757 		return fltr;
5758 
5759 	fltr = kzalloc(sizeof(*fltr), gfp);
5760 	if (!fltr)
5761 		return ERR_PTR(-ENOMEM);
5762 	spin_lock_bh(&bp->ntp_fltr_lock);
5763 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5764 	spin_unlock_bh(&bp->ntp_fltr_lock);
5765 	if (rc) {
5766 		bnxt_del_l2_filter(bp, fltr);
5767 		fltr = ERR_PTR(rc);
5768 	}
5769 	return fltr;
5770 }
5771 
5772 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
5773 						struct bnxt_l2_key *key,
5774 						u16 flags)
5775 {
5776 	struct bnxt_l2_filter *fltr;
5777 	u32 idx;
5778 	int rc;
5779 
5780 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5781 	      BNXT_L2_FLTR_HASH_MASK;
5782 	spin_lock_bh(&bp->ntp_fltr_lock);
5783 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5784 	if (fltr) {
5785 		fltr = ERR_PTR(-EEXIST);
5786 		goto l2_filter_exit;
5787 	}
5788 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
5789 	if (!fltr) {
5790 		fltr = ERR_PTR(-ENOMEM);
5791 		goto l2_filter_exit;
5792 	}
5793 	fltr->base.flags = flags;
5794 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5795 	if (rc) {
5796 		spin_unlock_bh(&bp->ntp_fltr_lock);
5797 		bnxt_del_l2_filter(bp, fltr);
5798 		return ERR_PTR(rc);
5799 	}
5800 
5801 l2_filter_exit:
5802 	spin_unlock_bh(&bp->ntp_fltr_lock);
5803 	return fltr;
5804 }
5805 
5806 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
5807 {
5808 #ifdef CONFIG_BNXT_SRIOV
5809 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
5810 
5811 	return vf->fw_fid;
5812 #else
5813 	return INVALID_HW_RING_ID;
5814 #endif
5815 }
5816 
5817 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5818 {
5819 	struct hwrm_cfa_l2_filter_free_input *req;
5820 	u16 target_id = 0xffff;
5821 	int rc;
5822 
5823 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
5824 		struct bnxt_pf_info *pf = &bp->pf;
5825 
5826 		if (fltr->base.vf_idx >= pf->active_vfs)
5827 			return -EINVAL;
5828 
5829 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
5830 		if (target_id == INVALID_HW_RING_ID)
5831 			return -EINVAL;
5832 	}
5833 
5834 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
5835 	if (rc)
5836 		return rc;
5837 
5838 	req->target_id = cpu_to_le16(target_id);
5839 	req->l2_filter_id = fltr->base.filter_id;
5840 	return hwrm_req_send(bp, req);
5841 }
5842 
5843 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5844 {
5845 	struct hwrm_cfa_l2_filter_alloc_output *resp;
5846 	struct hwrm_cfa_l2_filter_alloc_input *req;
5847 	u16 target_id = 0xffff;
5848 	int rc;
5849 
5850 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
5851 		struct bnxt_pf_info *pf = &bp->pf;
5852 
5853 		if (fltr->base.vf_idx >= pf->active_vfs)
5854 			return -EINVAL;
5855 
5856 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
5857 	}
5858 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
5859 	if (rc)
5860 		return rc;
5861 
5862 	req->target_id = cpu_to_le16(target_id);
5863 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
5864 
5865 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
5866 		req->flags |=
5867 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
5868 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
5869 	req->enables =
5870 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
5871 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
5872 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
5873 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
5874 	eth_broadcast_addr(req->l2_addr_mask);
5875 
5876 	if (fltr->l2_key.vlan) {
5877 		req->enables |=
5878 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
5879 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
5880 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
5881 		req->num_vlans = 1;
5882 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
5883 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
5884 	}
5885 
5886 	resp = hwrm_req_hold(bp, req);
5887 	rc = hwrm_req_send(bp, req);
5888 	if (!rc) {
5889 		fltr->base.filter_id = resp->l2_filter_id;
5890 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
5891 	}
5892 	hwrm_req_drop(bp, req);
5893 	return rc;
5894 }
5895 
5896 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
5897 				     struct bnxt_ntuple_filter *fltr)
5898 {
5899 	struct hwrm_cfa_ntuple_filter_free_input *req;
5900 	int rc;
5901 
5902 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
5903 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
5904 	if (rc)
5905 		return rc;
5906 
5907 	req->ntuple_filter_id = fltr->base.filter_id;
5908 	return hwrm_req_send(bp, req);
5909 }
5910 
5911 #define BNXT_NTP_FLTR_FLAGS					\
5912 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
5913 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
5914 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
5915 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
5916 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
5917 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
5918 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
5919 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
5920 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
5921 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
5922 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
5923 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
5924 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
5925 
5926 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
5927 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
5928 
5929 void bnxt_fill_ipv6_mask(__be32 mask[4])
5930 {
5931 	int i;
5932 
5933 	for (i = 0; i < 4; i++)
5934 		mask[i] = cpu_to_be32(~0);
5935 }
5936 
5937 static void
5938 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
5939 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
5940 			  struct bnxt_ntuple_filter *fltr)
5941 {
5942 	u16 rxq = fltr->base.rxq;
5943 
5944 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
5945 		struct ethtool_rxfh_context *ctx;
5946 		struct bnxt_rss_ctx *rss_ctx;
5947 		struct bnxt_vnic_info *vnic;
5948 
5949 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
5950 			      fltr->base.fw_vnic_id);
5951 		if (ctx) {
5952 			rss_ctx = ethtool_rxfh_context_priv(ctx);
5953 			vnic = &rss_ctx->vnic;
5954 
5955 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
5956 		}
5957 		return;
5958 	}
5959 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
5960 		struct bnxt_vnic_info *vnic;
5961 		u32 enables;
5962 
5963 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
5964 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
5965 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
5966 		req->enables |= cpu_to_le32(enables);
5967 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
5968 	} else {
5969 		u32 flags;
5970 
5971 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
5972 		req->flags |= cpu_to_le32(flags);
5973 		req->dst_id = cpu_to_le16(rxq);
5974 	}
5975 }
5976 
5977 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
5978 				      struct bnxt_ntuple_filter *fltr)
5979 {
5980 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
5981 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
5982 	struct bnxt_flow_masks *masks = &fltr->fmasks;
5983 	struct flow_keys *keys = &fltr->fkeys;
5984 	struct bnxt_l2_filter *l2_fltr;
5985 	struct bnxt_vnic_info *vnic;
5986 	int rc;
5987 
5988 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
5989 	if (rc)
5990 		return rc;
5991 
5992 	l2_fltr = fltr->l2_fltr;
5993 	req->l2_filter_id = l2_fltr->base.filter_id;
5994 
5995 	if (fltr->base.flags & BNXT_ACT_DROP) {
5996 		req->flags =
5997 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
5998 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
5999 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6000 	} else {
6001 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6002 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6003 	}
6004 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6005 
6006 	req->ethertype = htons(ETH_P_IP);
6007 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6008 	req->ip_protocol = keys->basic.ip_proto;
6009 
6010 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6011 		req->ethertype = htons(ETH_P_IPV6);
6012 		req->ip_addr_type =
6013 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6014 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6015 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6016 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6017 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6018 	} else {
6019 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6020 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6021 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6022 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6023 	}
6024 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6025 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6026 		req->tunnel_type =
6027 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6028 	}
6029 
6030 	req->src_port = keys->ports.src;
6031 	req->src_port_mask = masks->ports.src;
6032 	req->dst_port = keys->ports.dst;
6033 	req->dst_port_mask = masks->ports.dst;
6034 
6035 	resp = hwrm_req_hold(bp, req);
6036 	rc = hwrm_req_send(bp, req);
6037 	if (!rc)
6038 		fltr->base.filter_id = resp->ntuple_filter_id;
6039 	hwrm_req_drop(bp, req);
6040 	return rc;
6041 }
6042 
6043 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6044 				     const u8 *mac_addr)
6045 {
6046 	struct bnxt_l2_filter *fltr;
6047 	struct bnxt_l2_key key;
6048 	int rc;
6049 
6050 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6051 	key.vlan = 0;
6052 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6053 	if (IS_ERR(fltr))
6054 		return PTR_ERR(fltr);
6055 
6056 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6057 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6058 	if (rc)
6059 		bnxt_del_l2_filter(bp, fltr);
6060 	else
6061 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6062 	return rc;
6063 }
6064 
6065 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6066 {
6067 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6068 
6069 	/* Any associated ntuple filters will also be cleared by firmware. */
6070 	for (i = 0; i < num_of_vnics; i++) {
6071 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6072 
6073 		for (j = 0; j < vnic->uc_filter_count; j++) {
6074 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6075 
6076 			bnxt_hwrm_l2_filter_free(bp, fltr);
6077 			bnxt_del_l2_filter(bp, fltr);
6078 		}
6079 		vnic->uc_filter_count = 0;
6080 	}
6081 }
6082 
6083 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6084 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6085 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6086 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6087 
6088 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6089 					   struct hwrm_vnic_tpa_cfg_input *req)
6090 {
6091 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6092 
6093 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6094 		return;
6095 
6096 	if (bp->vxlan_port)
6097 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6098 	if (bp->vxlan_gpe_port)
6099 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6100 	if (bp->nge_port)
6101 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6102 
6103 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6104 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6105 }
6106 
6107 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6108 			   u32 tpa_flags)
6109 {
6110 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6111 	struct hwrm_vnic_tpa_cfg_input *req;
6112 	int rc;
6113 
6114 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6115 		return 0;
6116 
6117 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6118 	if (rc)
6119 		return rc;
6120 
6121 	if (tpa_flags) {
6122 		u16 mss = bp->dev->mtu - 40;
6123 		u32 nsegs, n, segs = 0, flags;
6124 
6125 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6126 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6127 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6128 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6129 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6130 		if (tpa_flags & BNXT_FLAG_GRO)
6131 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6132 
6133 		req->flags = cpu_to_le32(flags);
6134 
6135 		req->enables =
6136 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6137 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6138 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6139 
6140 		/* Number of segs are log2 units, and first packet is not
6141 		 * included as part of this units.
6142 		 */
6143 		if (mss <= BNXT_RX_PAGE_SIZE) {
6144 			n = BNXT_RX_PAGE_SIZE / mss;
6145 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6146 		} else {
6147 			n = mss / BNXT_RX_PAGE_SIZE;
6148 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6149 				n++;
6150 			nsegs = (MAX_SKB_FRAGS - n) / n;
6151 		}
6152 
6153 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6154 			segs = MAX_TPA_SEGS_P5;
6155 			max_aggs = bp->max_tpa;
6156 		} else {
6157 			segs = ilog2(nsegs);
6158 		}
6159 		req->max_agg_segs = cpu_to_le16(segs);
6160 		req->max_aggs = cpu_to_le16(max_aggs);
6161 
6162 		req->min_agg_len = cpu_to_le32(512);
6163 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6164 	}
6165 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6166 
6167 	return hwrm_req_send(bp, req);
6168 }
6169 
6170 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6171 {
6172 	struct bnxt_ring_grp_info *grp_info;
6173 
6174 	grp_info = &bp->grp_info[ring->grp_idx];
6175 	return grp_info->cp_fw_ring_id;
6176 }
6177 
6178 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6179 {
6180 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6181 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6182 	else
6183 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6184 }
6185 
6186 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6187 {
6188 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6189 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6190 	else
6191 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6192 }
6193 
6194 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6195 {
6196 	int entries;
6197 
6198 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6199 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6200 	else
6201 		entries = HW_HASH_INDEX_SIZE;
6202 
6203 	bp->rss_indir_tbl_entries = entries;
6204 	bp->rss_indir_tbl =
6205 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6206 	if (!bp->rss_indir_tbl)
6207 		return -ENOMEM;
6208 
6209 	return 0;
6210 }
6211 
6212 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6213 				 struct ethtool_rxfh_context *rss_ctx)
6214 {
6215 	u16 max_rings, max_entries, pad, i;
6216 	u32 *rss_indir_tbl;
6217 
6218 	if (!bp->rx_nr_rings)
6219 		return;
6220 
6221 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6222 		max_rings = bp->rx_nr_rings - 1;
6223 	else
6224 		max_rings = bp->rx_nr_rings;
6225 
6226 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6227 	if (rss_ctx)
6228 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6229 	else
6230 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6231 
6232 	for (i = 0; i < max_entries; i++)
6233 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6234 
6235 	pad = bp->rss_indir_tbl_entries - max_entries;
6236 	if (pad)
6237 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6238 }
6239 
6240 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6241 {
6242 	u32 i, tbl_size, max_ring = 0;
6243 
6244 	if (!bp->rss_indir_tbl)
6245 		return 0;
6246 
6247 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6248 	for (i = 0; i < tbl_size; i++)
6249 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6250 	return max_ring;
6251 }
6252 
6253 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6254 {
6255 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6256 		if (!rx_rings)
6257 			return 0;
6258 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6259 					       BNXT_RSS_TABLE_ENTRIES_P5);
6260 	}
6261 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6262 		return 2;
6263 	return 1;
6264 }
6265 
6266 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6267 {
6268 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6269 	u16 i, j;
6270 
6271 	/* Fill the RSS indirection table with ring group ids */
6272 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6273 		if (!no_rss)
6274 			j = bp->rss_indir_tbl[i];
6275 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6276 	}
6277 }
6278 
6279 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6280 				    struct bnxt_vnic_info *vnic)
6281 {
6282 	__le16 *ring_tbl = vnic->rss_table;
6283 	struct bnxt_rx_ring_info *rxr;
6284 	u16 tbl_size, i;
6285 
6286 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6287 
6288 	for (i = 0; i < tbl_size; i++) {
6289 		u16 ring_id, j;
6290 
6291 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6292 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6293 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6294 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6295 		else
6296 			j = bp->rss_indir_tbl[i];
6297 		rxr = &bp->rx_ring[j];
6298 
6299 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6300 		*ring_tbl++ = cpu_to_le16(ring_id);
6301 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6302 		*ring_tbl++ = cpu_to_le16(ring_id);
6303 	}
6304 }
6305 
6306 static void
6307 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6308 			 struct bnxt_vnic_info *vnic)
6309 {
6310 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6311 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6312 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6313 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6314 	} else {
6315 		bnxt_fill_hw_rss_tbl(bp, vnic);
6316 	}
6317 
6318 	if (bp->rss_hash_delta) {
6319 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6320 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6321 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6322 		else
6323 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6324 	} else {
6325 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6326 	}
6327 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6328 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6329 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6330 }
6331 
6332 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6333 				  bool set_rss)
6334 {
6335 	struct hwrm_vnic_rss_cfg_input *req;
6336 	int rc;
6337 
6338 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6339 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6340 		return 0;
6341 
6342 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6343 	if (rc)
6344 		return rc;
6345 
6346 	if (set_rss)
6347 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6348 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6349 	return hwrm_req_send(bp, req);
6350 }
6351 
6352 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6353 				     struct bnxt_vnic_info *vnic, bool set_rss)
6354 {
6355 	struct hwrm_vnic_rss_cfg_input *req;
6356 	dma_addr_t ring_tbl_map;
6357 	u32 i, nr_ctxs;
6358 	int rc;
6359 
6360 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6361 	if (rc)
6362 		return rc;
6363 
6364 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6365 	if (!set_rss)
6366 		return hwrm_req_send(bp, req);
6367 
6368 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6369 	ring_tbl_map = vnic->rss_table_dma_addr;
6370 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6371 
6372 	hwrm_req_hold(bp, req);
6373 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6374 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6375 		req->ring_table_pair_index = i;
6376 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6377 		rc = hwrm_req_send(bp, req);
6378 		if (rc)
6379 			goto exit;
6380 	}
6381 
6382 exit:
6383 	hwrm_req_drop(bp, req);
6384 	return rc;
6385 }
6386 
6387 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6388 {
6389 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6390 	struct hwrm_vnic_rss_qcfg_output *resp;
6391 	struct hwrm_vnic_rss_qcfg_input *req;
6392 
6393 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6394 		return;
6395 
6396 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6397 	/* all contexts configured to same hash_type, zero always exists */
6398 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6399 	resp = hwrm_req_hold(bp, req);
6400 	if (!hwrm_req_send(bp, req)) {
6401 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6402 		bp->rss_hash_delta = 0;
6403 	}
6404 	hwrm_req_drop(bp, req);
6405 }
6406 
6407 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6408 {
6409 	struct hwrm_vnic_plcmodes_cfg_input *req;
6410 	int rc;
6411 
6412 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6413 	if (rc)
6414 		return rc;
6415 
6416 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6417 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6418 
6419 	if (BNXT_RX_PAGE_MODE(bp)) {
6420 		req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6421 	} else {
6422 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6423 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6424 		req->enables |=
6425 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6426 		req->jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
6427 		req->hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
6428 	}
6429 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6430 	return hwrm_req_send(bp, req);
6431 }
6432 
6433 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6434 					struct bnxt_vnic_info *vnic,
6435 					u16 ctx_idx)
6436 {
6437 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6438 
6439 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6440 		return;
6441 
6442 	req->rss_cos_lb_ctx_id =
6443 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6444 
6445 	hwrm_req_send(bp, req);
6446 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6447 }
6448 
6449 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6450 {
6451 	int i, j;
6452 
6453 	for (i = 0; i < bp->nr_vnics; i++) {
6454 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6455 
6456 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6457 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6458 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6459 		}
6460 	}
6461 	bp->rsscos_nr_ctxs = 0;
6462 }
6463 
6464 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6465 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6466 {
6467 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6468 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6469 	int rc;
6470 
6471 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6472 	if (rc)
6473 		return rc;
6474 
6475 	resp = hwrm_req_hold(bp, req);
6476 	rc = hwrm_req_send(bp, req);
6477 	if (!rc)
6478 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6479 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6480 	hwrm_req_drop(bp, req);
6481 
6482 	return rc;
6483 }
6484 
6485 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6486 {
6487 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6488 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6489 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6490 }
6491 
6492 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6493 {
6494 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6495 	struct hwrm_vnic_cfg_input *req;
6496 	unsigned int ring = 0, grp_idx;
6497 	u16 def_vlan = 0;
6498 	int rc;
6499 
6500 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6501 	if (rc)
6502 		return rc;
6503 
6504 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6505 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6506 
6507 		req->default_rx_ring_id =
6508 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6509 		req->default_cmpl_ring_id =
6510 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6511 		req->enables =
6512 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6513 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6514 		goto vnic_mru;
6515 	}
6516 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6517 	/* Only RSS support for now TBD: COS & LB */
6518 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6519 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6520 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6521 					   VNIC_CFG_REQ_ENABLES_MRU);
6522 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6523 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6524 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6525 					   VNIC_CFG_REQ_ENABLES_MRU);
6526 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6527 	} else {
6528 		req->rss_rule = cpu_to_le16(0xffff);
6529 	}
6530 
6531 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6532 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6533 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6534 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6535 	} else {
6536 		req->cos_rule = cpu_to_le16(0xffff);
6537 	}
6538 
6539 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6540 		ring = 0;
6541 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6542 		ring = vnic->vnic_id - 1;
6543 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6544 		ring = bp->rx_nr_rings - 1;
6545 
6546 	grp_idx = bp->rx_ring[ring].bnapi->index;
6547 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6548 	req->lb_rule = cpu_to_le16(0xffff);
6549 vnic_mru:
6550 	vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
6551 	req->mru = cpu_to_le16(vnic->mru);
6552 
6553 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6554 #ifdef CONFIG_BNXT_SRIOV
6555 	if (BNXT_VF(bp))
6556 		def_vlan = bp->vf.vlan;
6557 #endif
6558 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6559 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6560 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6561 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6562 
6563 	return hwrm_req_send(bp, req);
6564 }
6565 
6566 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6567 				    struct bnxt_vnic_info *vnic)
6568 {
6569 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6570 		struct hwrm_vnic_free_input *req;
6571 
6572 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6573 			return;
6574 
6575 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6576 
6577 		hwrm_req_send(bp, req);
6578 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6579 	}
6580 }
6581 
6582 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6583 {
6584 	u16 i;
6585 
6586 	for (i = 0; i < bp->nr_vnics; i++)
6587 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6588 }
6589 
6590 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6591 			 unsigned int start_rx_ring_idx,
6592 			 unsigned int nr_rings)
6593 {
6594 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6595 	struct hwrm_vnic_alloc_output *resp;
6596 	struct hwrm_vnic_alloc_input *req;
6597 	int rc;
6598 
6599 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6600 	if (rc)
6601 		return rc;
6602 
6603 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6604 		goto vnic_no_ring_grps;
6605 
6606 	/* map ring groups to this vnic */
6607 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6608 		grp_idx = bp->rx_ring[i].bnapi->index;
6609 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6610 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6611 				   j, nr_rings);
6612 			break;
6613 		}
6614 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6615 	}
6616 
6617 vnic_no_ring_grps:
6618 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6619 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6620 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6621 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6622 
6623 	resp = hwrm_req_hold(bp, req);
6624 	rc = hwrm_req_send(bp, req);
6625 	if (!rc)
6626 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6627 	hwrm_req_drop(bp, req);
6628 	return rc;
6629 }
6630 
6631 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6632 {
6633 	struct hwrm_vnic_qcaps_output *resp;
6634 	struct hwrm_vnic_qcaps_input *req;
6635 	int rc;
6636 
6637 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6638 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6639 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6640 	if (bp->hwrm_spec_code < 0x10600)
6641 		return 0;
6642 
6643 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6644 	if (rc)
6645 		return rc;
6646 
6647 	resp = hwrm_req_hold(bp, req);
6648 	rc = hwrm_req_send(bp, req);
6649 	if (!rc) {
6650 		u32 flags = le32_to_cpu(resp->flags);
6651 
6652 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6653 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6654 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6655 		if (flags &
6656 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6657 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6658 
6659 		/* Older P5 fw before EXT_HW_STATS support did not set
6660 		 * VLAN_STRIP_CAP properly.
6661 		 */
6662 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6663 		    (BNXT_CHIP_P5(bp) &&
6664 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6665 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6666 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6667 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6668 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6669 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6670 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6671 		if (bp->max_tpa_v2) {
6672 			if (BNXT_CHIP_P5(bp))
6673 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6674 			else
6675 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6676 		}
6677 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6678 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6679 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6680 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6681 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6682 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6683 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6684 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6685 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6686 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6687 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
6688 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
6689 	}
6690 	hwrm_req_drop(bp, req);
6691 	return rc;
6692 }
6693 
6694 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6695 {
6696 	struct hwrm_ring_grp_alloc_output *resp;
6697 	struct hwrm_ring_grp_alloc_input *req;
6698 	int rc;
6699 	u16 i;
6700 
6701 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6702 		return 0;
6703 
6704 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6705 	if (rc)
6706 		return rc;
6707 
6708 	resp = hwrm_req_hold(bp, req);
6709 	for (i = 0; i < bp->rx_nr_rings; i++) {
6710 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
6711 
6712 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
6713 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
6714 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
6715 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
6716 
6717 		rc = hwrm_req_send(bp, req);
6718 
6719 		if (rc)
6720 			break;
6721 
6722 		bp->grp_info[grp_idx].fw_grp_id =
6723 			le32_to_cpu(resp->ring_group_id);
6724 	}
6725 	hwrm_req_drop(bp, req);
6726 	return rc;
6727 }
6728 
6729 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
6730 {
6731 	struct hwrm_ring_grp_free_input *req;
6732 	u16 i;
6733 
6734 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
6735 		return;
6736 
6737 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
6738 		return;
6739 
6740 	hwrm_req_hold(bp, req);
6741 	for (i = 0; i < bp->cp_nr_rings; i++) {
6742 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
6743 			continue;
6744 		req->ring_group_id =
6745 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
6746 
6747 		hwrm_req_send(bp, req);
6748 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
6749 	}
6750 	hwrm_req_drop(bp, req);
6751 }
6752 
6753 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
6754 				    struct bnxt_ring_struct *ring,
6755 				    u32 ring_type, u32 map_index)
6756 {
6757 	struct hwrm_ring_alloc_output *resp;
6758 	struct hwrm_ring_alloc_input *req;
6759 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
6760 	struct bnxt_ring_grp_info *grp_info;
6761 	int rc, err = 0;
6762 	u16 ring_id;
6763 
6764 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
6765 	if (rc)
6766 		goto exit;
6767 
6768 	req->enables = 0;
6769 	if (rmem->nr_pages > 1) {
6770 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
6771 		/* Page size is in log2 units */
6772 		req->page_size = BNXT_PAGE_SHIFT;
6773 		req->page_tbl_depth = 1;
6774 	} else {
6775 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
6776 	}
6777 	req->fbo = 0;
6778 	/* Association of ring index with doorbell index and MSIX number */
6779 	req->logical_id = cpu_to_le16(map_index);
6780 
6781 	switch (ring_type) {
6782 	case HWRM_RING_ALLOC_TX: {
6783 		struct bnxt_tx_ring_info *txr;
6784 		u16 flags = 0;
6785 
6786 		txr = container_of(ring, struct bnxt_tx_ring_info,
6787 				   tx_ring_struct);
6788 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
6789 		/* Association of transmit ring with completion ring */
6790 		grp_info = &bp->grp_info[ring->grp_idx];
6791 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
6792 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
6793 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6794 		req->queue_id = cpu_to_le16(ring->queue_id);
6795 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
6796 			req->cmpl_coal_cnt =
6797 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
6798 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
6799 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
6800 		req->flags = cpu_to_le16(flags);
6801 		break;
6802 	}
6803 	case HWRM_RING_ALLOC_RX:
6804 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6805 		req->length = cpu_to_le32(bp->rx_ring_mask + 1);
6806 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6807 			u16 flags = 0;
6808 
6809 			/* Association of rx ring with stats context */
6810 			grp_info = &bp->grp_info[ring->grp_idx];
6811 			req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
6812 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6813 			req->enables |= cpu_to_le32(
6814 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6815 			if (NET_IP_ALIGN == 2)
6816 				flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
6817 			req->flags = cpu_to_le16(flags);
6818 		}
6819 		break;
6820 	case HWRM_RING_ALLOC_AGG:
6821 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6822 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
6823 			/* Association of agg ring with rx ring */
6824 			grp_info = &bp->grp_info[ring->grp_idx];
6825 			req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
6826 			req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
6827 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6828 			req->enables |= cpu_to_le32(
6829 				RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
6830 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6831 		} else {
6832 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6833 		}
6834 		req->length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
6835 		break;
6836 	case HWRM_RING_ALLOC_CMPL:
6837 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
6838 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
6839 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6840 			/* Association of cp ring with nq */
6841 			grp_info = &bp->grp_info[map_index];
6842 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
6843 			req->cq_handle = cpu_to_le64(ring->handle);
6844 			req->enables |= cpu_to_le32(
6845 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
6846 		} else {
6847 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
6848 		}
6849 		break;
6850 	case HWRM_RING_ALLOC_NQ:
6851 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
6852 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
6853 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
6854 		break;
6855 	default:
6856 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
6857 			   ring_type);
6858 		return -1;
6859 	}
6860 
6861 	resp = hwrm_req_hold(bp, req);
6862 	rc = hwrm_req_send(bp, req);
6863 	err = le16_to_cpu(resp->error_code);
6864 	ring_id = le16_to_cpu(resp->ring_id);
6865 	hwrm_req_drop(bp, req);
6866 
6867 exit:
6868 	if (rc || err) {
6869 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
6870 			   ring_type, rc, err);
6871 		return -EIO;
6872 	}
6873 	ring->fw_ring_id = ring_id;
6874 	return rc;
6875 }
6876 
6877 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
6878 {
6879 	int rc;
6880 
6881 	if (BNXT_PF(bp)) {
6882 		struct hwrm_func_cfg_input *req;
6883 
6884 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
6885 		if (rc)
6886 			return rc;
6887 
6888 		req->fid = cpu_to_le16(0xffff);
6889 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
6890 		req->async_event_cr = cpu_to_le16(idx);
6891 		return hwrm_req_send(bp, req);
6892 	} else {
6893 		struct hwrm_func_vf_cfg_input *req;
6894 
6895 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
6896 		if (rc)
6897 			return rc;
6898 
6899 		req->enables =
6900 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
6901 		req->async_event_cr = cpu_to_le16(idx);
6902 		return hwrm_req_send(bp, req);
6903 	}
6904 }
6905 
6906 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
6907 			     u32 ring_type)
6908 {
6909 	switch (ring_type) {
6910 	case HWRM_RING_ALLOC_TX:
6911 		db->db_ring_mask = bp->tx_ring_mask;
6912 		break;
6913 	case HWRM_RING_ALLOC_RX:
6914 		db->db_ring_mask = bp->rx_ring_mask;
6915 		break;
6916 	case HWRM_RING_ALLOC_AGG:
6917 		db->db_ring_mask = bp->rx_agg_ring_mask;
6918 		break;
6919 	case HWRM_RING_ALLOC_CMPL:
6920 	case HWRM_RING_ALLOC_NQ:
6921 		db->db_ring_mask = bp->cp_ring_mask;
6922 		break;
6923 	}
6924 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
6925 		db->db_epoch_mask = db->db_ring_mask + 1;
6926 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
6927 	}
6928 }
6929 
6930 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
6931 			u32 map_idx, u32 xid)
6932 {
6933 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6934 		switch (ring_type) {
6935 		case HWRM_RING_ALLOC_TX:
6936 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
6937 			break;
6938 		case HWRM_RING_ALLOC_RX:
6939 		case HWRM_RING_ALLOC_AGG:
6940 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
6941 			break;
6942 		case HWRM_RING_ALLOC_CMPL:
6943 			db->db_key64 = DBR_PATH_L2;
6944 			break;
6945 		case HWRM_RING_ALLOC_NQ:
6946 			db->db_key64 = DBR_PATH_L2;
6947 			break;
6948 		}
6949 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
6950 
6951 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6952 			db->db_key64 |= DBR_VALID;
6953 
6954 		db->doorbell = bp->bar1 + bp->db_offset;
6955 	} else {
6956 		db->doorbell = bp->bar1 + map_idx * 0x80;
6957 		switch (ring_type) {
6958 		case HWRM_RING_ALLOC_TX:
6959 			db->db_key32 = DB_KEY_TX;
6960 			break;
6961 		case HWRM_RING_ALLOC_RX:
6962 		case HWRM_RING_ALLOC_AGG:
6963 			db->db_key32 = DB_KEY_RX;
6964 			break;
6965 		case HWRM_RING_ALLOC_CMPL:
6966 			db->db_key32 = DB_KEY_CP;
6967 			break;
6968 		}
6969 	}
6970 	bnxt_set_db_mask(bp, db, ring_type);
6971 }
6972 
6973 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
6974 				   struct bnxt_rx_ring_info *rxr)
6975 {
6976 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
6977 	struct bnxt_napi *bnapi = rxr->bnapi;
6978 	u32 type = HWRM_RING_ALLOC_RX;
6979 	u32 map_idx = bnapi->index;
6980 	int rc;
6981 
6982 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6983 	if (rc)
6984 		return rc;
6985 
6986 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
6987 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
6988 
6989 	return 0;
6990 }
6991 
6992 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
6993 				       struct bnxt_rx_ring_info *rxr)
6994 {
6995 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
6996 	u32 type = HWRM_RING_ALLOC_AGG;
6997 	u32 grp_idx = ring->grp_idx;
6998 	u32 map_idx;
6999 	int rc;
7000 
7001 	map_idx = grp_idx + bp->rx_nr_rings;
7002 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7003 	if (rc)
7004 		return rc;
7005 
7006 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7007 		    ring->fw_ring_id);
7008 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7009 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7010 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7011 
7012 	return 0;
7013 }
7014 
7015 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7016 {
7017 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7018 	int i, rc = 0;
7019 	u32 type;
7020 
7021 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7022 		type = HWRM_RING_ALLOC_NQ;
7023 	else
7024 		type = HWRM_RING_ALLOC_CMPL;
7025 	for (i = 0; i < bp->cp_nr_rings; i++) {
7026 		struct bnxt_napi *bnapi = bp->bnapi[i];
7027 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7028 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7029 		u32 map_idx = ring->map_idx;
7030 		unsigned int vector;
7031 
7032 		vector = bp->irq_tbl[map_idx].vector;
7033 		disable_irq_nosync(vector);
7034 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7035 		if (rc) {
7036 			enable_irq(vector);
7037 			goto err_out;
7038 		}
7039 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7040 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7041 		enable_irq(vector);
7042 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7043 
7044 		if (!i) {
7045 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7046 			if (rc)
7047 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7048 		}
7049 	}
7050 
7051 	type = HWRM_RING_ALLOC_TX;
7052 	for (i = 0; i < bp->tx_nr_rings; i++) {
7053 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7054 		struct bnxt_ring_struct *ring;
7055 		u32 map_idx;
7056 
7057 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7058 			struct bnxt_cp_ring_info *cpr2 = txr->tx_cpr;
7059 			struct bnxt_napi *bnapi = txr->bnapi;
7060 			u32 type2 = HWRM_RING_ALLOC_CMPL;
7061 
7062 			ring = &cpr2->cp_ring_struct;
7063 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
7064 			map_idx = bnapi->index;
7065 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
7066 			if (rc)
7067 				goto err_out;
7068 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
7069 				    ring->fw_ring_id);
7070 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
7071 		}
7072 		ring = &txr->tx_ring_struct;
7073 		map_idx = i;
7074 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7075 		if (rc)
7076 			goto err_out;
7077 		bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
7078 	}
7079 
7080 	for (i = 0; i < bp->rx_nr_rings; i++) {
7081 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7082 
7083 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7084 		if (rc)
7085 			goto err_out;
7086 		/* If we have agg rings, post agg buffers first. */
7087 		if (!agg_rings)
7088 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7089 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7090 			struct bnxt_cp_ring_info *cpr2 = rxr->rx_cpr;
7091 			struct bnxt_napi *bnapi = rxr->bnapi;
7092 			u32 type2 = HWRM_RING_ALLOC_CMPL;
7093 			struct bnxt_ring_struct *ring;
7094 			u32 map_idx = bnapi->index;
7095 
7096 			ring = &cpr2->cp_ring_struct;
7097 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
7098 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
7099 			if (rc)
7100 				goto err_out;
7101 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
7102 				    ring->fw_ring_id);
7103 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
7104 		}
7105 	}
7106 
7107 	if (agg_rings) {
7108 		for (i = 0; i < bp->rx_nr_rings; i++) {
7109 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7110 			if (rc)
7111 				goto err_out;
7112 		}
7113 	}
7114 err_out:
7115 	return rc;
7116 }
7117 
7118 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7119 				   struct bnxt_ring_struct *ring,
7120 				   u32 ring_type, int cmpl_ring_id)
7121 {
7122 	struct hwrm_ring_free_output *resp;
7123 	struct hwrm_ring_free_input *req;
7124 	u16 error_code = 0;
7125 	int rc;
7126 
7127 	if (BNXT_NO_FW_ACCESS(bp))
7128 		return 0;
7129 
7130 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7131 	if (rc)
7132 		goto exit;
7133 
7134 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7135 	req->ring_type = ring_type;
7136 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7137 
7138 	resp = hwrm_req_hold(bp, req);
7139 	rc = hwrm_req_send(bp, req);
7140 	error_code = le16_to_cpu(resp->error_code);
7141 	hwrm_req_drop(bp, req);
7142 exit:
7143 	if (rc || error_code) {
7144 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7145 			   ring_type, rc, error_code);
7146 		return -EIO;
7147 	}
7148 	return 0;
7149 }
7150 
7151 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7152 				   struct bnxt_rx_ring_info *rxr,
7153 				   bool close_path)
7154 {
7155 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7156 	u32 grp_idx = rxr->bnapi->index;
7157 	u32 cmpl_ring_id;
7158 
7159 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7160 		return;
7161 
7162 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7163 	hwrm_ring_free_send_msg(bp, ring,
7164 				RING_FREE_REQ_RING_TYPE_RX,
7165 				close_path ? cmpl_ring_id :
7166 				INVALID_HW_RING_ID);
7167 	ring->fw_ring_id = INVALID_HW_RING_ID;
7168 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7169 }
7170 
7171 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7172 				       struct bnxt_rx_ring_info *rxr,
7173 				       bool close_path)
7174 {
7175 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7176 	u32 grp_idx = rxr->bnapi->index;
7177 	u32 type, cmpl_ring_id;
7178 
7179 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7180 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7181 	else
7182 		type = RING_FREE_REQ_RING_TYPE_RX;
7183 
7184 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7185 		return;
7186 
7187 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7188 	hwrm_ring_free_send_msg(bp, ring, type,
7189 				close_path ? cmpl_ring_id :
7190 				INVALID_HW_RING_ID);
7191 	ring->fw_ring_id = INVALID_HW_RING_ID;
7192 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7193 }
7194 
7195 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7196 {
7197 	u32 type;
7198 	int i;
7199 
7200 	if (!bp->bnapi)
7201 		return;
7202 
7203 	for (i = 0; i < bp->tx_nr_rings; i++) {
7204 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7205 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7206 
7207 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7208 			u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
7209 
7210 			hwrm_ring_free_send_msg(bp, ring,
7211 						RING_FREE_REQ_RING_TYPE_TX,
7212 						close_path ? cmpl_ring_id :
7213 						INVALID_HW_RING_ID);
7214 			ring->fw_ring_id = INVALID_HW_RING_ID;
7215 		}
7216 	}
7217 
7218 	for (i = 0; i < bp->rx_nr_rings; i++) {
7219 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7220 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7221 	}
7222 
7223 	/* The completion rings are about to be freed.  After that the
7224 	 * IRQ doorbell will not work anymore.  So we need to disable
7225 	 * IRQ here.
7226 	 */
7227 	bnxt_disable_int_sync(bp);
7228 
7229 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7230 		type = RING_FREE_REQ_RING_TYPE_NQ;
7231 	else
7232 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7233 	for (i = 0; i < bp->cp_nr_rings; i++) {
7234 		struct bnxt_napi *bnapi = bp->bnapi[i];
7235 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7236 		struct bnxt_ring_struct *ring;
7237 		int j;
7238 
7239 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++) {
7240 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
7241 
7242 			ring = &cpr2->cp_ring_struct;
7243 			if (ring->fw_ring_id == INVALID_HW_RING_ID)
7244 				continue;
7245 			hwrm_ring_free_send_msg(bp, ring,
7246 						RING_FREE_REQ_RING_TYPE_L2_CMPL,
7247 						INVALID_HW_RING_ID);
7248 			ring->fw_ring_id = INVALID_HW_RING_ID;
7249 		}
7250 		ring = &cpr->cp_ring_struct;
7251 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7252 			hwrm_ring_free_send_msg(bp, ring, type,
7253 						INVALID_HW_RING_ID);
7254 			ring->fw_ring_id = INVALID_HW_RING_ID;
7255 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7256 		}
7257 	}
7258 }
7259 
7260 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7261 			     bool shared);
7262 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7263 			   bool shared);
7264 
7265 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7266 {
7267 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7268 	struct hwrm_func_qcfg_output *resp;
7269 	struct hwrm_func_qcfg_input *req;
7270 	int rc;
7271 
7272 	if (bp->hwrm_spec_code < 0x10601)
7273 		return 0;
7274 
7275 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7276 	if (rc)
7277 		return rc;
7278 
7279 	req->fid = cpu_to_le16(0xffff);
7280 	resp = hwrm_req_hold(bp, req);
7281 	rc = hwrm_req_send(bp, req);
7282 	if (rc) {
7283 		hwrm_req_drop(bp, req);
7284 		return rc;
7285 	}
7286 
7287 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7288 	if (BNXT_NEW_RM(bp)) {
7289 		u16 cp, stats;
7290 
7291 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7292 		hw_resc->resv_hw_ring_grps =
7293 			le32_to_cpu(resp->alloc_hw_ring_grps);
7294 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7295 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7296 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7297 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7298 		hw_resc->resv_irqs = cp;
7299 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7300 			int rx = hw_resc->resv_rx_rings;
7301 			int tx = hw_resc->resv_tx_rings;
7302 
7303 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7304 				rx >>= 1;
7305 			if (cp < (rx + tx)) {
7306 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7307 				if (rc)
7308 					goto get_rings_exit;
7309 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7310 					rx <<= 1;
7311 				hw_resc->resv_rx_rings = rx;
7312 				hw_resc->resv_tx_rings = tx;
7313 			}
7314 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7315 			hw_resc->resv_hw_ring_grps = rx;
7316 		}
7317 		hw_resc->resv_cp_rings = cp;
7318 		hw_resc->resv_stat_ctxs = stats;
7319 	}
7320 get_rings_exit:
7321 	hwrm_req_drop(bp, req);
7322 	return rc;
7323 }
7324 
7325 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7326 {
7327 	struct hwrm_func_qcfg_output *resp;
7328 	struct hwrm_func_qcfg_input *req;
7329 	int rc;
7330 
7331 	if (bp->hwrm_spec_code < 0x10601)
7332 		return 0;
7333 
7334 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7335 	if (rc)
7336 		return rc;
7337 
7338 	req->fid = cpu_to_le16(fid);
7339 	resp = hwrm_req_hold(bp, req);
7340 	rc = hwrm_req_send(bp, req);
7341 	if (!rc)
7342 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7343 
7344 	hwrm_req_drop(bp, req);
7345 	return rc;
7346 }
7347 
7348 static bool bnxt_rfs_supported(struct bnxt *bp);
7349 
7350 static struct hwrm_func_cfg_input *
7351 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7352 {
7353 	struct hwrm_func_cfg_input *req;
7354 	u32 enables = 0;
7355 
7356 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7357 		return NULL;
7358 
7359 	req->fid = cpu_to_le16(0xffff);
7360 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7361 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7362 	if (BNXT_NEW_RM(bp)) {
7363 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7364 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7365 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7366 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7367 			enables |= hwr->cp_p5 ?
7368 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7369 		} else {
7370 			enables |= hwr->cp ?
7371 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7372 			enables |= hwr->grp ?
7373 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7374 		}
7375 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7376 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7377 					  0;
7378 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7379 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7380 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7381 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7382 			req->num_msix = cpu_to_le16(hwr->cp);
7383 		} else {
7384 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7385 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7386 		}
7387 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7388 		req->num_vnics = cpu_to_le16(hwr->vnic);
7389 	}
7390 	req->enables = cpu_to_le32(enables);
7391 	return req;
7392 }
7393 
7394 static struct hwrm_func_vf_cfg_input *
7395 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7396 {
7397 	struct hwrm_func_vf_cfg_input *req;
7398 	u32 enables = 0;
7399 
7400 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7401 		return NULL;
7402 
7403 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7404 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7405 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7406 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7407 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7408 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7409 		enables |= hwr->cp_p5 ?
7410 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7411 	} else {
7412 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7413 		enables |= hwr->grp ?
7414 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7415 	}
7416 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7417 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7418 
7419 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7420 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7421 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7422 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7423 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7424 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7425 	} else {
7426 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7427 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7428 	}
7429 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7430 	req->num_vnics = cpu_to_le16(hwr->vnic);
7431 
7432 	req->enables = cpu_to_le32(enables);
7433 	return req;
7434 }
7435 
7436 static int
7437 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7438 {
7439 	struct hwrm_func_cfg_input *req;
7440 	int rc;
7441 
7442 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7443 	if (!req)
7444 		return -ENOMEM;
7445 
7446 	if (!req->enables) {
7447 		hwrm_req_drop(bp, req);
7448 		return 0;
7449 	}
7450 
7451 	rc = hwrm_req_send(bp, req);
7452 	if (rc)
7453 		return rc;
7454 
7455 	if (bp->hwrm_spec_code < 0x10601)
7456 		bp->hw_resc.resv_tx_rings = hwr->tx;
7457 
7458 	return bnxt_hwrm_get_rings(bp);
7459 }
7460 
7461 static int
7462 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7463 {
7464 	struct hwrm_func_vf_cfg_input *req;
7465 	int rc;
7466 
7467 	if (!BNXT_NEW_RM(bp)) {
7468 		bp->hw_resc.resv_tx_rings = hwr->tx;
7469 		return 0;
7470 	}
7471 
7472 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7473 	if (!req)
7474 		return -ENOMEM;
7475 
7476 	rc = hwrm_req_send(bp, req);
7477 	if (rc)
7478 		return rc;
7479 
7480 	return bnxt_hwrm_get_rings(bp);
7481 }
7482 
7483 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7484 {
7485 	if (BNXT_PF(bp))
7486 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7487 	else
7488 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7489 }
7490 
7491 int bnxt_nq_rings_in_use(struct bnxt *bp)
7492 {
7493 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7494 }
7495 
7496 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7497 {
7498 	int cp;
7499 
7500 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7501 		return bnxt_nq_rings_in_use(bp);
7502 
7503 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7504 	return cp;
7505 }
7506 
7507 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7508 {
7509 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7510 }
7511 
7512 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7513 {
7514 	if (!hwr->grp)
7515 		return 0;
7516 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7517 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7518 
7519 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7520 			rss_ctx *= hwr->vnic;
7521 		return rss_ctx;
7522 	}
7523 	if (BNXT_VF(bp))
7524 		return BNXT_VF_MAX_RSS_CTX;
7525 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7526 		return hwr->grp + 1;
7527 	return 1;
7528 }
7529 
7530 /* Check if a default RSS map needs to be setup.  This function is only
7531  * used on older firmware that does not require reserving RX rings.
7532  */
7533 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7534 {
7535 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7536 
7537 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7538 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7539 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7540 		if (!netif_is_rxfh_configured(bp->dev))
7541 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7542 	}
7543 }
7544 
7545 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7546 {
7547 	if (bp->flags & BNXT_FLAG_RFS) {
7548 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7549 			return 2 + bp->num_rss_ctx;
7550 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7551 			return rx_rings + 1;
7552 	}
7553 	return 1;
7554 }
7555 
7556 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7557 {
7558 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7559 	int cp = bnxt_cp_rings_in_use(bp);
7560 	int nq = bnxt_nq_rings_in_use(bp);
7561 	int rx = bp->rx_nr_rings, stat;
7562 	int vnic, grp = rx;
7563 
7564 	/* Old firmware does not need RX ring reservations but we still
7565 	 * need to setup a default RSS map when needed.  With new firmware
7566 	 * we go through RX ring reservations first and then set up the
7567 	 * RSS map for the successfully reserved RX rings when needed.
7568 	 */
7569 	if (!BNXT_NEW_RM(bp))
7570 		bnxt_check_rss_tbl_no_rmgr(bp);
7571 
7572 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7573 	    bp->hwrm_spec_code >= 0x10601)
7574 		return true;
7575 
7576 	if (!BNXT_NEW_RM(bp))
7577 		return false;
7578 
7579 	vnic = bnxt_get_total_vnics(bp, rx);
7580 
7581 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7582 		rx <<= 1;
7583 	stat = bnxt_get_func_stat_ctxs(bp);
7584 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7585 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7586 	    (hw_resc->resv_hw_ring_grps != grp &&
7587 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7588 		return true;
7589 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7590 	    hw_resc->resv_irqs != nq)
7591 		return true;
7592 	return false;
7593 }
7594 
7595 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7596 {
7597 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7598 
7599 	hwr->tx = hw_resc->resv_tx_rings;
7600 	if (BNXT_NEW_RM(bp)) {
7601 		hwr->rx = hw_resc->resv_rx_rings;
7602 		hwr->cp = hw_resc->resv_irqs;
7603 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7604 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7605 		hwr->grp = hw_resc->resv_hw_ring_grps;
7606 		hwr->vnic = hw_resc->resv_vnics;
7607 		hwr->stat = hw_resc->resv_stat_ctxs;
7608 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7609 	}
7610 }
7611 
7612 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7613 {
7614 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7615 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7616 }
7617 
7618 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
7619 
7620 static int __bnxt_reserve_rings(struct bnxt *bp)
7621 {
7622 	struct bnxt_hw_rings hwr = {0};
7623 	int rx_rings, old_rx_rings, rc;
7624 	int cp = bp->cp_nr_rings;
7625 	int ulp_msix = 0;
7626 	bool sh = false;
7627 	int tx_cp;
7628 
7629 	if (!bnxt_need_reserve_rings(bp))
7630 		return 0;
7631 
7632 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
7633 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
7634 		if (!ulp_msix)
7635 			bnxt_set_ulp_stat_ctxs(bp, 0);
7636 
7637 		if (ulp_msix > bp->ulp_num_msix_want)
7638 			ulp_msix = bp->ulp_num_msix_want;
7639 		hwr.cp = cp + ulp_msix;
7640 	} else {
7641 		hwr.cp = bnxt_nq_rings_in_use(bp);
7642 	}
7643 
7644 	hwr.tx = bp->tx_nr_rings;
7645 	hwr.rx = bp->rx_nr_rings;
7646 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7647 		sh = true;
7648 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7649 		hwr.cp_p5 = hwr.rx + hwr.tx;
7650 
7651 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7652 
7653 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7654 		hwr.rx <<= 1;
7655 	hwr.grp = bp->rx_nr_rings;
7656 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7657 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
7658 	old_rx_rings = bp->hw_resc.resv_rx_rings;
7659 
7660 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
7661 	if (rc)
7662 		return rc;
7663 
7664 	bnxt_copy_reserved_rings(bp, &hwr);
7665 
7666 	rx_rings = hwr.rx;
7667 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7668 		if (hwr.rx >= 2) {
7669 			rx_rings = hwr.rx >> 1;
7670 		} else {
7671 			if (netif_running(bp->dev))
7672 				return -ENOMEM;
7673 
7674 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7675 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7676 			bp->dev->hw_features &= ~NETIF_F_LRO;
7677 			bp->dev->features &= ~NETIF_F_LRO;
7678 			bnxt_set_ring_params(bp);
7679 		}
7680 	}
7681 	rx_rings = min_t(int, rx_rings, hwr.grp);
7682 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
7683 	if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
7684 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
7685 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
7686 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
7687 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7688 		hwr.rx = rx_rings << 1;
7689 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
7690 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
7691 	bp->tx_nr_rings = hwr.tx;
7692 
7693 	/* If we cannot reserve all the RX rings, reset the RSS map only
7694 	 * if absolutely necessary
7695 	 */
7696 	if (rx_rings != bp->rx_nr_rings) {
7697 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
7698 			    rx_rings, bp->rx_nr_rings);
7699 		if (netif_is_rxfh_configured(bp->dev) &&
7700 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
7701 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
7702 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
7703 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
7704 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
7705 		}
7706 	}
7707 	bp->rx_nr_rings = rx_rings;
7708 	bp->cp_nr_rings = hwr.cp;
7709 
7710 	if (!bnxt_rings_ok(bp, &hwr))
7711 		return -ENOMEM;
7712 
7713 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
7714 	    !netif_is_rxfh_configured(bp->dev))
7715 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7716 
7717 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
7718 		int resv_msix, resv_ctx, ulp_ctxs;
7719 		struct bnxt_hw_resc *hw_resc;
7720 
7721 		hw_resc = &bp->hw_resc;
7722 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
7723 		ulp_msix = min_t(int, resv_msix, ulp_msix);
7724 		bnxt_set_ulp_msix_num(bp, ulp_msix);
7725 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
7726 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
7727 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
7728 	}
7729 
7730 	return rc;
7731 }
7732 
7733 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7734 {
7735 	struct hwrm_func_vf_cfg_input *req;
7736 	u32 flags;
7737 
7738 	if (!BNXT_NEW_RM(bp))
7739 		return 0;
7740 
7741 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7742 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
7743 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7744 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7745 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7746 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
7747 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
7748 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7749 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7750 
7751 	req->flags = cpu_to_le32(flags);
7752 	return hwrm_req_send_silent(bp, req);
7753 }
7754 
7755 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7756 {
7757 	struct hwrm_func_cfg_input *req;
7758 	u32 flags;
7759 
7760 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7761 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
7762 	if (BNXT_NEW_RM(bp)) {
7763 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7764 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7765 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7766 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
7767 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7768 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
7769 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
7770 		else
7771 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7772 	}
7773 
7774 	req->flags = cpu_to_le32(flags);
7775 	return hwrm_req_send_silent(bp, req);
7776 }
7777 
7778 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7779 {
7780 	if (bp->hwrm_spec_code < 0x10801)
7781 		return 0;
7782 
7783 	if (BNXT_PF(bp))
7784 		return bnxt_hwrm_check_pf_rings(bp, hwr);
7785 
7786 	return bnxt_hwrm_check_vf_rings(bp, hwr);
7787 }
7788 
7789 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
7790 {
7791 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7792 	struct hwrm_ring_aggint_qcaps_output *resp;
7793 	struct hwrm_ring_aggint_qcaps_input *req;
7794 	int rc;
7795 
7796 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
7797 	coal_cap->num_cmpl_dma_aggr_max = 63;
7798 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
7799 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
7800 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
7801 	coal_cap->int_lat_tmr_min_max = 65535;
7802 	coal_cap->int_lat_tmr_max_max = 65535;
7803 	coal_cap->num_cmpl_aggr_int_max = 65535;
7804 	coal_cap->timer_units = 80;
7805 
7806 	if (bp->hwrm_spec_code < 0x10902)
7807 		return;
7808 
7809 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
7810 		return;
7811 
7812 	resp = hwrm_req_hold(bp, req);
7813 	rc = hwrm_req_send_silent(bp, req);
7814 	if (!rc) {
7815 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
7816 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
7817 		coal_cap->num_cmpl_dma_aggr_max =
7818 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
7819 		coal_cap->num_cmpl_dma_aggr_during_int_max =
7820 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
7821 		coal_cap->cmpl_aggr_dma_tmr_max =
7822 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
7823 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
7824 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
7825 		coal_cap->int_lat_tmr_min_max =
7826 			le16_to_cpu(resp->int_lat_tmr_min_max);
7827 		coal_cap->int_lat_tmr_max_max =
7828 			le16_to_cpu(resp->int_lat_tmr_max_max);
7829 		coal_cap->num_cmpl_aggr_int_max =
7830 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
7831 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
7832 	}
7833 	hwrm_req_drop(bp, req);
7834 }
7835 
7836 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
7837 {
7838 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7839 
7840 	return usec * 1000 / coal_cap->timer_units;
7841 }
7842 
7843 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
7844 	struct bnxt_coal *hw_coal,
7845 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7846 {
7847 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7848 	u16 val, tmr, max, flags = hw_coal->flags;
7849 	u32 cmpl_params = coal_cap->cmpl_params;
7850 
7851 	max = hw_coal->bufs_per_record * 128;
7852 	if (hw_coal->budget)
7853 		max = hw_coal->bufs_per_record * hw_coal->budget;
7854 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
7855 
7856 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
7857 	req->num_cmpl_aggr_int = cpu_to_le16(val);
7858 
7859 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
7860 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
7861 
7862 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
7863 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
7864 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
7865 
7866 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
7867 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
7868 	req->int_lat_tmr_max = cpu_to_le16(tmr);
7869 
7870 	/* min timer set to 1/2 of interrupt timer */
7871 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
7872 		val = tmr / 2;
7873 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
7874 		req->int_lat_tmr_min = cpu_to_le16(val);
7875 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
7876 	}
7877 
7878 	/* buf timer set to 1/4 of interrupt timer */
7879 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
7880 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
7881 
7882 	if (cmpl_params &
7883 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
7884 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
7885 		val = clamp_t(u16, tmr, 1,
7886 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
7887 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
7888 		req->enables |=
7889 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
7890 	}
7891 
7892 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
7893 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
7894 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
7895 	req->flags = cpu_to_le16(flags);
7896 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
7897 }
7898 
7899 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
7900 				   struct bnxt_coal *hw_coal)
7901 {
7902 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
7903 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7904 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7905 	u32 nq_params = coal_cap->nq_params;
7906 	u16 tmr;
7907 	int rc;
7908 
7909 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
7910 		return 0;
7911 
7912 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7913 	if (rc)
7914 		return rc;
7915 
7916 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
7917 	req->flags =
7918 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
7919 
7920 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
7921 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
7922 	req->int_lat_tmr_min = cpu_to_le16(tmr);
7923 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
7924 	return hwrm_req_send(bp, req);
7925 }
7926 
7927 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
7928 {
7929 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
7930 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7931 	struct bnxt_coal coal;
7932 	int rc;
7933 
7934 	/* Tick values in micro seconds.
7935 	 * 1 coal_buf x bufs_per_record = 1 completion record.
7936 	 */
7937 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
7938 
7939 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
7940 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
7941 
7942 	if (!bnapi->rx_ring)
7943 		return -ENODEV;
7944 
7945 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7946 	if (rc)
7947 		return rc;
7948 
7949 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
7950 
7951 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
7952 
7953 	return hwrm_req_send(bp, req_rx);
7954 }
7955 
7956 static int
7957 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
7958 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7959 {
7960 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
7961 
7962 	req->ring_id = cpu_to_le16(ring_id);
7963 	return hwrm_req_send(bp, req);
7964 }
7965 
7966 static int
7967 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
7968 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7969 {
7970 	struct bnxt_tx_ring_info *txr;
7971 	int i, rc;
7972 
7973 	bnxt_for_each_napi_tx(i, bnapi, txr) {
7974 		u16 ring_id;
7975 
7976 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
7977 		req->ring_id = cpu_to_le16(ring_id);
7978 		rc = hwrm_req_send(bp, req);
7979 		if (rc)
7980 			return rc;
7981 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7982 			return 0;
7983 	}
7984 	return 0;
7985 }
7986 
7987 int bnxt_hwrm_set_coal(struct bnxt *bp)
7988 {
7989 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
7990 	int i, rc;
7991 
7992 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7993 	if (rc)
7994 		return rc;
7995 
7996 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7997 	if (rc) {
7998 		hwrm_req_drop(bp, req_rx);
7999 		return rc;
8000 	}
8001 
8002 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8003 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8004 
8005 	hwrm_req_hold(bp, req_rx);
8006 	hwrm_req_hold(bp, req_tx);
8007 	for (i = 0; i < bp->cp_nr_rings; i++) {
8008 		struct bnxt_napi *bnapi = bp->bnapi[i];
8009 		struct bnxt_coal *hw_coal;
8010 
8011 		if (!bnapi->rx_ring)
8012 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8013 		else
8014 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8015 		if (rc)
8016 			break;
8017 
8018 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8019 			continue;
8020 
8021 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8022 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8023 			if (rc)
8024 				break;
8025 		}
8026 		if (bnapi->rx_ring)
8027 			hw_coal = &bp->rx_coal;
8028 		else
8029 			hw_coal = &bp->tx_coal;
8030 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8031 	}
8032 	hwrm_req_drop(bp, req_rx);
8033 	hwrm_req_drop(bp, req_tx);
8034 	return rc;
8035 }
8036 
8037 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8038 {
8039 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8040 	struct hwrm_stat_ctx_free_input *req;
8041 	int i;
8042 
8043 	if (!bp->bnapi)
8044 		return;
8045 
8046 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8047 		return;
8048 
8049 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8050 		return;
8051 	if (BNXT_FW_MAJ(bp) <= 20) {
8052 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8053 			hwrm_req_drop(bp, req);
8054 			return;
8055 		}
8056 		hwrm_req_hold(bp, req0);
8057 	}
8058 	hwrm_req_hold(bp, req);
8059 	for (i = 0; i < bp->cp_nr_rings; i++) {
8060 		struct bnxt_napi *bnapi = bp->bnapi[i];
8061 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8062 
8063 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8064 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8065 			if (req0) {
8066 				req0->stat_ctx_id = req->stat_ctx_id;
8067 				hwrm_req_send(bp, req0);
8068 			}
8069 			hwrm_req_send(bp, req);
8070 
8071 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8072 		}
8073 	}
8074 	hwrm_req_drop(bp, req);
8075 	if (req0)
8076 		hwrm_req_drop(bp, req0);
8077 }
8078 
8079 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8080 {
8081 	struct hwrm_stat_ctx_alloc_output *resp;
8082 	struct hwrm_stat_ctx_alloc_input *req;
8083 	int rc, i;
8084 
8085 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8086 		return 0;
8087 
8088 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8089 	if (rc)
8090 		return rc;
8091 
8092 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8093 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8094 
8095 	resp = hwrm_req_hold(bp, req);
8096 	for (i = 0; i < bp->cp_nr_rings; i++) {
8097 		struct bnxt_napi *bnapi = bp->bnapi[i];
8098 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8099 
8100 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8101 
8102 		rc = hwrm_req_send(bp, req);
8103 		if (rc)
8104 			break;
8105 
8106 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8107 
8108 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8109 	}
8110 	hwrm_req_drop(bp, req);
8111 	return rc;
8112 }
8113 
8114 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8115 {
8116 	struct hwrm_func_qcfg_output *resp;
8117 	struct hwrm_func_qcfg_input *req;
8118 	u16 flags;
8119 	int rc;
8120 
8121 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8122 	if (rc)
8123 		return rc;
8124 
8125 	req->fid = cpu_to_le16(0xffff);
8126 	resp = hwrm_req_hold(bp, req);
8127 	rc = hwrm_req_send(bp, req);
8128 	if (rc)
8129 		goto func_qcfg_exit;
8130 
8131 #ifdef CONFIG_BNXT_SRIOV
8132 	if (BNXT_VF(bp)) {
8133 		struct bnxt_vf_info *vf = &bp->vf;
8134 
8135 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8136 	} else {
8137 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8138 	}
8139 #endif
8140 	flags = le16_to_cpu(resp->flags);
8141 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8142 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8143 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8144 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8145 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8146 	}
8147 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8148 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8149 
8150 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8151 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8152 
8153 	switch (resp->port_partition_type) {
8154 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8155 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8156 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8157 		bp->port_partition_type = resp->port_partition_type;
8158 		break;
8159 	}
8160 	if (bp->hwrm_spec_code < 0x10707 ||
8161 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8162 		bp->br_mode = BRIDGE_MODE_VEB;
8163 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8164 		bp->br_mode = BRIDGE_MODE_VEPA;
8165 	else
8166 		bp->br_mode = BRIDGE_MODE_UNDEF;
8167 
8168 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8169 	if (!bp->max_mtu)
8170 		bp->max_mtu = BNXT_MAX_MTU;
8171 
8172 	if (bp->db_size)
8173 		goto func_qcfg_exit;
8174 
8175 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8176 	if (BNXT_CHIP_P5(bp)) {
8177 		if (BNXT_PF(bp))
8178 			bp->db_offset = DB_PF_OFFSET_P5;
8179 		else
8180 			bp->db_offset = DB_VF_OFFSET_P5;
8181 	}
8182 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8183 				 1024);
8184 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8185 	    bp->db_size <= bp->db_offset)
8186 		bp->db_size = pci_resource_len(bp->pdev, 2);
8187 
8188 func_qcfg_exit:
8189 	hwrm_req_drop(bp, req);
8190 	return rc;
8191 }
8192 
8193 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8194 				      u8 init_val, u8 init_offset,
8195 				      bool init_mask_set)
8196 {
8197 	ctxm->init_value = init_val;
8198 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8199 	if (init_mask_set)
8200 		ctxm->init_offset = init_offset * 4;
8201 	else
8202 		ctxm->init_value = 0;
8203 }
8204 
8205 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8206 {
8207 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8208 	u16 type;
8209 
8210 	for (type = 0; type < ctx_max; type++) {
8211 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8212 		int n = 1;
8213 
8214 		if (!ctxm->max_entries)
8215 			continue;
8216 
8217 		if (ctxm->instance_bmap)
8218 			n = hweight32(ctxm->instance_bmap);
8219 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8220 		if (!ctxm->pg_info)
8221 			return -ENOMEM;
8222 	}
8223 	return 0;
8224 }
8225 
8226 #define BNXT_CTX_INIT_VALID(flags)	\
8227 	(!!((flags) &			\
8228 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8229 
8230 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8231 {
8232 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8233 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8234 	struct bnxt_ctx_mem_info *ctx;
8235 	u16 type;
8236 	int rc;
8237 
8238 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8239 	if (rc)
8240 		return rc;
8241 
8242 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8243 	if (!ctx)
8244 		return -ENOMEM;
8245 	bp->ctx = ctx;
8246 
8247 	resp = hwrm_req_hold(bp, req);
8248 
8249 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8250 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8251 		u8 init_val, init_off, i;
8252 		__le32 *p;
8253 		u32 flags;
8254 
8255 		req->type = cpu_to_le16(type);
8256 		rc = hwrm_req_send(bp, req);
8257 		if (rc)
8258 			goto ctx_done;
8259 		flags = le32_to_cpu(resp->flags);
8260 		type = le16_to_cpu(resp->next_valid_type);
8261 		if (!(flags & FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_TYPE_VALID))
8262 			continue;
8263 
8264 		ctxm->type = le16_to_cpu(resp->type);
8265 		ctxm->entry_size = le16_to_cpu(resp->entry_size);
8266 		ctxm->flags = flags;
8267 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8268 		ctxm->entry_multiple = resp->entry_multiple;
8269 		ctxm->max_entries = le32_to_cpu(resp->max_num_entries);
8270 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8271 		init_val = resp->ctx_init_value;
8272 		init_off = resp->ctx_init_offset;
8273 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8274 					  BNXT_CTX_INIT_VALID(flags));
8275 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8276 					      BNXT_MAX_SPLIT_ENTRY);
8277 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8278 		     i++, p++)
8279 			ctxm->split[i] = le32_to_cpu(*p);
8280 	}
8281 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8282 
8283 ctx_done:
8284 	hwrm_req_drop(bp, req);
8285 	return rc;
8286 }
8287 
8288 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8289 {
8290 	struct hwrm_func_backing_store_qcaps_output *resp;
8291 	struct hwrm_func_backing_store_qcaps_input *req;
8292 	int rc;
8293 
8294 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx)
8295 		return 0;
8296 
8297 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8298 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8299 
8300 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8301 	if (rc)
8302 		return rc;
8303 
8304 	resp = hwrm_req_hold(bp, req);
8305 	rc = hwrm_req_send_silent(bp, req);
8306 	if (!rc) {
8307 		struct bnxt_ctx_mem_type *ctxm;
8308 		struct bnxt_ctx_mem_info *ctx;
8309 		u8 init_val, init_idx = 0;
8310 		u16 init_mask;
8311 
8312 		ctx = bp->ctx;
8313 		if (!ctx) {
8314 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8315 			if (!ctx) {
8316 				rc = -ENOMEM;
8317 				goto ctx_err;
8318 			}
8319 			bp->ctx = ctx;
8320 		}
8321 		init_val = resp->ctx_kind_initializer;
8322 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8323 
8324 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8325 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8326 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8327 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8328 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8329 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8330 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8331 					  (init_mask & (1 << init_idx++)) != 0);
8332 
8333 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8334 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8335 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8336 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8337 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8338 					  (init_mask & (1 << init_idx++)) != 0);
8339 
8340 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8341 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8342 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8343 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8344 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8345 					  (init_mask & (1 << init_idx++)) != 0);
8346 
8347 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8348 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8349 		ctxm->max_entries = ctxm->vnic_entries +
8350 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8351 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8352 		bnxt_init_ctx_initializer(ctxm, init_val,
8353 					  resp->vnic_init_offset,
8354 					  (init_mask & (1 << init_idx++)) != 0);
8355 
8356 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8357 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8358 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8359 		bnxt_init_ctx_initializer(ctxm, init_val,
8360 					  resp->stat_init_offset,
8361 					  (init_mask & (1 << init_idx++)) != 0);
8362 
8363 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8364 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8365 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8366 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8367 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8368 		if (!ctxm->entry_multiple)
8369 			ctxm->entry_multiple = 1;
8370 
8371 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8372 
8373 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8374 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8375 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8376 		ctxm->mrav_num_entries_units =
8377 			le16_to_cpu(resp->mrav_num_entries_units);
8378 		bnxt_init_ctx_initializer(ctxm, init_val,
8379 					  resp->mrav_init_offset,
8380 					  (init_mask & (1 << init_idx++)) != 0);
8381 
8382 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8383 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8384 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8385 
8386 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8387 		if (!ctx->tqm_fp_rings_count)
8388 			ctx->tqm_fp_rings_count = bp->max_q;
8389 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8390 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8391 
8392 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8393 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8394 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8395 
8396 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8397 	} else {
8398 		rc = 0;
8399 	}
8400 ctx_err:
8401 	hwrm_req_drop(bp, req);
8402 	return rc;
8403 }
8404 
8405 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8406 				  __le64 *pg_dir)
8407 {
8408 	if (!rmem->nr_pages)
8409 		return;
8410 
8411 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8412 	if (rmem->depth >= 1) {
8413 		if (rmem->depth == 2)
8414 			*pg_attr |= 2;
8415 		else
8416 			*pg_attr |= 1;
8417 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8418 	} else {
8419 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8420 	}
8421 }
8422 
8423 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8424 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8425 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8426 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8427 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8428 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8429 
8430 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8431 {
8432 	struct hwrm_func_backing_store_cfg_input *req;
8433 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8434 	struct bnxt_ctx_pg_info *ctx_pg;
8435 	struct bnxt_ctx_mem_type *ctxm;
8436 	void **__req = (void **)&req;
8437 	u32 req_len = sizeof(*req);
8438 	__le32 *num_entries;
8439 	__le64 *pg_dir;
8440 	u32 flags = 0;
8441 	u8 *pg_attr;
8442 	u32 ena;
8443 	int rc;
8444 	int i;
8445 
8446 	if (!ctx)
8447 		return 0;
8448 
8449 	if (req_len > bp->hwrm_max_ext_req_len)
8450 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8451 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8452 	if (rc)
8453 		return rc;
8454 
8455 	req->enables = cpu_to_le32(enables);
8456 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8457 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8458 		ctx_pg = ctxm->pg_info;
8459 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8460 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8461 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8462 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8463 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8464 				      &req->qpc_pg_size_qpc_lvl,
8465 				      &req->qpc_page_dir);
8466 
8467 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8468 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8469 	}
8470 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8471 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8472 		ctx_pg = ctxm->pg_info;
8473 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8474 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8475 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8476 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8477 				      &req->srq_pg_size_srq_lvl,
8478 				      &req->srq_page_dir);
8479 	}
8480 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8481 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8482 		ctx_pg = ctxm->pg_info;
8483 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8484 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8485 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8486 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8487 				      &req->cq_pg_size_cq_lvl,
8488 				      &req->cq_page_dir);
8489 	}
8490 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8491 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8492 		ctx_pg = ctxm->pg_info;
8493 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8494 		req->vnic_num_ring_table_entries =
8495 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8496 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8497 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8498 				      &req->vnic_pg_size_vnic_lvl,
8499 				      &req->vnic_page_dir);
8500 	}
8501 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8502 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8503 		ctx_pg = ctxm->pg_info;
8504 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8505 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8506 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8507 				      &req->stat_pg_size_stat_lvl,
8508 				      &req->stat_page_dir);
8509 	}
8510 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8511 		u32 units;
8512 
8513 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8514 		ctx_pg = ctxm->pg_info;
8515 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8516 		units = ctxm->mrav_num_entries_units;
8517 		if (units) {
8518 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8519 			u32 entries;
8520 
8521 			num_mr = ctx_pg->entries - num_ah;
8522 			entries = ((num_mr / units) << 16) | (num_ah / units);
8523 			req->mrav_num_entries = cpu_to_le32(entries);
8524 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8525 		}
8526 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8527 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8528 				      &req->mrav_pg_size_mrav_lvl,
8529 				      &req->mrav_page_dir);
8530 	}
8531 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8532 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8533 		ctx_pg = ctxm->pg_info;
8534 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8535 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8536 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8537 				      &req->tim_pg_size_tim_lvl,
8538 				      &req->tim_page_dir);
8539 	}
8540 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8541 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8542 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8543 	     pg_dir = &req->tqm_sp_page_dir,
8544 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8545 	     ctx_pg = ctxm->pg_info;
8546 	     i < BNXT_MAX_TQM_RINGS;
8547 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8548 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8549 		if (!(enables & ena))
8550 			continue;
8551 
8552 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8553 		*num_entries = cpu_to_le32(ctx_pg->entries);
8554 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8555 	}
8556 	req->flags = cpu_to_le32(flags);
8557 	return hwrm_req_send(bp, req);
8558 }
8559 
8560 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8561 				  struct bnxt_ctx_pg_info *ctx_pg)
8562 {
8563 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8564 
8565 	rmem->page_size = BNXT_PAGE_SIZE;
8566 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8567 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8568 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8569 	if (rmem->depth >= 1)
8570 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8571 	return bnxt_alloc_ring(bp, rmem);
8572 }
8573 
8574 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8575 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8576 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8577 {
8578 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8579 	int rc;
8580 
8581 	if (!mem_size)
8582 		return -EINVAL;
8583 
8584 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8585 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8586 		ctx_pg->nr_pages = 0;
8587 		return -EINVAL;
8588 	}
8589 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8590 		int nr_tbls, i;
8591 
8592 		rmem->depth = 2;
8593 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8594 					     GFP_KERNEL);
8595 		if (!ctx_pg->ctx_pg_tbl)
8596 			return -ENOMEM;
8597 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8598 		rmem->nr_pages = nr_tbls;
8599 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8600 		if (rc)
8601 			return rc;
8602 		for (i = 0; i < nr_tbls; i++) {
8603 			struct bnxt_ctx_pg_info *pg_tbl;
8604 
8605 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8606 			if (!pg_tbl)
8607 				return -ENOMEM;
8608 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8609 			rmem = &pg_tbl->ring_mem;
8610 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8611 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8612 			rmem->depth = 1;
8613 			rmem->nr_pages = MAX_CTX_PAGES;
8614 			rmem->ctx_mem = ctxm;
8615 			if (i == (nr_tbls - 1)) {
8616 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8617 
8618 				if (rem)
8619 					rmem->nr_pages = rem;
8620 			}
8621 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8622 			if (rc)
8623 				break;
8624 		}
8625 	} else {
8626 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8627 		if (rmem->nr_pages > 1 || depth)
8628 			rmem->depth = 1;
8629 		rmem->ctx_mem = ctxm;
8630 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8631 	}
8632 	return rc;
8633 }
8634 
8635 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
8636 				  struct bnxt_ctx_pg_info *ctx_pg)
8637 {
8638 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8639 
8640 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
8641 	    ctx_pg->ctx_pg_tbl) {
8642 		int i, nr_tbls = rmem->nr_pages;
8643 
8644 		for (i = 0; i < nr_tbls; i++) {
8645 			struct bnxt_ctx_pg_info *pg_tbl;
8646 			struct bnxt_ring_mem_info *rmem2;
8647 
8648 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8649 			if (!pg_tbl)
8650 				continue;
8651 			rmem2 = &pg_tbl->ring_mem;
8652 			bnxt_free_ring(bp, rmem2);
8653 			ctx_pg->ctx_pg_arr[i] = NULL;
8654 			kfree(pg_tbl);
8655 			ctx_pg->ctx_pg_tbl[i] = NULL;
8656 		}
8657 		kfree(ctx_pg->ctx_pg_tbl);
8658 		ctx_pg->ctx_pg_tbl = NULL;
8659 	}
8660 	bnxt_free_ring(bp, rmem);
8661 	ctx_pg->nr_pages = 0;
8662 }
8663 
8664 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
8665 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
8666 				   u8 pg_lvl)
8667 {
8668 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8669 	int i, rc = 0, n = 1;
8670 	u32 mem_size;
8671 
8672 	if (!ctxm->entry_size || !ctx_pg)
8673 		return -EINVAL;
8674 	if (ctxm->instance_bmap)
8675 		n = hweight32(ctxm->instance_bmap);
8676 	if (ctxm->entry_multiple)
8677 		entries = roundup(entries, ctxm->entry_multiple);
8678 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
8679 	mem_size = entries * ctxm->entry_size;
8680 	for (i = 0; i < n && !rc; i++) {
8681 		ctx_pg[i].entries = entries;
8682 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
8683 					    ctxm->init_value ? ctxm : NULL);
8684 	}
8685 	return rc;
8686 }
8687 
8688 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
8689 					       struct bnxt_ctx_mem_type *ctxm,
8690 					       bool last)
8691 {
8692 	struct hwrm_func_backing_store_cfg_v2_input *req;
8693 	u32 instance_bmap = ctxm->instance_bmap;
8694 	int i, j, rc = 0, n = 1;
8695 	__le32 *p;
8696 
8697 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
8698 		return 0;
8699 
8700 	if (instance_bmap)
8701 		n = hweight32(ctxm->instance_bmap);
8702 	else
8703 		instance_bmap = 1;
8704 
8705 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
8706 	if (rc)
8707 		return rc;
8708 	hwrm_req_hold(bp, req);
8709 	req->type = cpu_to_le16(ctxm->type);
8710 	req->entry_size = cpu_to_le16(ctxm->entry_size);
8711 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
8712 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
8713 		p[i] = cpu_to_le32(ctxm->split[i]);
8714 	for (i = 0, j = 0; j < n && !rc; i++) {
8715 		struct bnxt_ctx_pg_info *ctx_pg;
8716 
8717 		if (!(instance_bmap & (1 << i)))
8718 			continue;
8719 		req->instance = cpu_to_le16(i);
8720 		ctx_pg = &ctxm->pg_info[j++];
8721 		if (!ctx_pg->entries)
8722 			continue;
8723 		req->num_entries = cpu_to_le32(ctx_pg->entries);
8724 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8725 				      &req->page_size_pbl_level,
8726 				      &req->page_dir);
8727 		if (last && j == n)
8728 			req->flags =
8729 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
8730 		rc = hwrm_req_send(bp, req);
8731 	}
8732 	hwrm_req_drop(bp, req);
8733 	return rc;
8734 }
8735 
8736 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
8737 {
8738 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8739 	struct bnxt_ctx_mem_type *ctxm;
8740 	u16 last_type;
8741 	int rc = 0;
8742 	u16 type;
8743 
8744 	if (!ena)
8745 		return 0;
8746 	else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
8747 		last_type = BNXT_CTX_MAX - 1;
8748 	else
8749 		last_type = BNXT_CTX_L2_MAX - 1;
8750 	ctx->ctx_arr[last_type].last = 1;
8751 
8752 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
8753 		ctxm = &ctx->ctx_arr[type];
8754 
8755 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
8756 		if (rc)
8757 			return rc;
8758 	}
8759 	return 0;
8760 }
8761 
8762 void bnxt_free_ctx_mem(struct bnxt *bp)
8763 {
8764 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8765 	u16 type;
8766 
8767 	if (!ctx)
8768 		return;
8769 
8770 	for (type = 0; type < BNXT_CTX_V2_MAX; type++) {
8771 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8772 		struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8773 		int i, n = 1;
8774 
8775 		if (!ctx_pg)
8776 			continue;
8777 		if (ctxm->instance_bmap)
8778 			n = hweight32(ctxm->instance_bmap);
8779 		for (i = 0; i < n; i++)
8780 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
8781 
8782 		kfree(ctx_pg);
8783 		ctxm->pg_info = NULL;
8784 	}
8785 
8786 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
8787 	kfree(ctx);
8788 	bp->ctx = NULL;
8789 }
8790 
8791 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
8792 {
8793 	struct bnxt_ctx_mem_type *ctxm;
8794 	struct bnxt_ctx_mem_info *ctx;
8795 	u32 l2_qps, qp1_qps, max_qps;
8796 	u32 ena, entries_sp, entries;
8797 	u32 srqs, max_srqs, min;
8798 	u32 num_mr, num_ah;
8799 	u32 extra_srqs = 0;
8800 	u32 extra_qps = 0;
8801 	u32 fast_qpmd_qps;
8802 	u8 pg_lvl = 1;
8803 	int i, rc;
8804 
8805 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
8806 	if (rc) {
8807 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
8808 			   rc);
8809 		return rc;
8810 	}
8811 	ctx = bp->ctx;
8812 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
8813 		return 0;
8814 
8815 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8816 	l2_qps = ctxm->qp_l2_entries;
8817 	qp1_qps = ctxm->qp_qp1_entries;
8818 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
8819 	max_qps = ctxm->max_entries;
8820 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8821 	srqs = ctxm->srq_l2_entries;
8822 	max_srqs = ctxm->max_entries;
8823 	ena = 0;
8824 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
8825 		pg_lvl = 2;
8826 		extra_qps = min_t(u32, 65536, max_qps - l2_qps - qp1_qps);
8827 		/* allocate extra qps if fw supports RoCE fast qp destroy feature */
8828 		extra_qps += fast_qpmd_qps;
8829 		extra_srqs = min_t(u32, 8192, max_srqs - srqs);
8830 		if (fast_qpmd_qps)
8831 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
8832 	}
8833 
8834 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8835 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
8836 				     pg_lvl);
8837 	if (rc)
8838 		return rc;
8839 
8840 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8841 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
8842 	if (rc)
8843 		return rc;
8844 
8845 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8846 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
8847 				     extra_qps * 2, pg_lvl);
8848 	if (rc)
8849 		return rc;
8850 
8851 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8852 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
8853 	if (rc)
8854 		return rc;
8855 
8856 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8857 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
8858 	if (rc)
8859 		return rc;
8860 
8861 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
8862 		goto skip_rdma;
8863 
8864 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8865 	/* 128K extra is needed to accommodate static AH context
8866 	 * allocation by f/w.
8867 	 */
8868 	num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
8869 	num_ah = min_t(u32, num_mr, 1024 * 128);
8870 	ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
8871 	if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
8872 		ctxm->mrav_av_entries = num_ah;
8873 
8874 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
8875 	if (rc)
8876 		return rc;
8877 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
8878 
8879 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8880 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
8881 	if (rc)
8882 		return rc;
8883 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
8884 
8885 skip_rdma:
8886 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8887 	min = ctxm->min_entries;
8888 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
8889 		     2 * (extra_qps + qp1_qps) + min;
8890 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
8891 	if (rc)
8892 		return rc;
8893 
8894 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8895 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
8896 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
8897 	if (rc)
8898 		return rc;
8899 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
8900 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
8901 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
8902 
8903 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8904 		rc = bnxt_backing_store_cfg_v2(bp, ena);
8905 	else
8906 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
8907 	if (rc) {
8908 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
8909 			   rc);
8910 		return rc;
8911 	}
8912 	ctx->flags |= BNXT_CTX_FLAG_INITED;
8913 	return 0;
8914 }
8915 
8916 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
8917 {
8918 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
8919 	u16 page_attr;
8920 	int rc;
8921 
8922 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
8923 		return 0;
8924 
8925 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
8926 	if (rc)
8927 		return rc;
8928 
8929 	if (BNXT_PAGE_SIZE == 0x2000)
8930 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
8931 	else if (BNXT_PAGE_SIZE == 0x10000)
8932 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
8933 	else
8934 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
8935 	req->pg_size_lvl = cpu_to_le16(page_attr |
8936 				       bp->fw_crash_mem->ring_mem.depth);
8937 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
8938 	req->size = cpu_to_le32(bp->fw_crash_len);
8939 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
8940 	return hwrm_req_send(bp, req);
8941 }
8942 
8943 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
8944 {
8945 	if (bp->fw_crash_mem) {
8946 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
8947 		kfree(bp->fw_crash_mem);
8948 		bp->fw_crash_mem = NULL;
8949 	}
8950 }
8951 
8952 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
8953 {
8954 	u32 mem_size = 0;
8955 	int rc;
8956 
8957 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
8958 		return 0;
8959 
8960 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
8961 	if (rc)
8962 		return rc;
8963 
8964 	mem_size = round_up(mem_size, 4);
8965 
8966 	/* keep and use the existing pages */
8967 	if (bp->fw_crash_mem &&
8968 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
8969 		goto alloc_done;
8970 
8971 	if (bp->fw_crash_mem)
8972 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
8973 	else
8974 		bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem),
8975 					   GFP_KERNEL);
8976 	if (!bp->fw_crash_mem)
8977 		return -ENOMEM;
8978 
8979 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
8980 	if (rc) {
8981 		bnxt_free_crash_dump_mem(bp);
8982 		return rc;
8983 	}
8984 
8985 alloc_done:
8986 	bp->fw_crash_len = mem_size;
8987 	return 0;
8988 }
8989 
8990 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
8991 {
8992 	struct hwrm_func_resource_qcaps_output *resp;
8993 	struct hwrm_func_resource_qcaps_input *req;
8994 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8995 	int rc;
8996 
8997 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
8998 	if (rc)
8999 		return rc;
9000 
9001 	req->fid = cpu_to_le16(0xffff);
9002 	resp = hwrm_req_hold(bp, req);
9003 	rc = hwrm_req_send_silent(bp, req);
9004 	if (rc)
9005 		goto hwrm_func_resc_qcaps_exit;
9006 
9007 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9008 	if (!all)
9009 		goto hwrm_func_resc_qcaps_exit;
9010 
9011 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9012 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9013 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9014 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9015 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9016 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9017 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9018 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9019 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9020 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9021 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9022 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9023 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9024 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9025 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9026 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9027 
9028 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9029 		u16 max_msix = le16_to_cpu(resp->max_msix);
9030 
9031 		hw_resc->max_nqs = max_msix;
9032 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9033 	}
9034 
9035 	if (BNXT_PF(bp)) {
9036 		struct bnxt_pf_info *pf = &bp->pf;
9037 
9038 		pf->vf_resv_strategy =
9039 			le16_to_cpu(resp->vf_reservation_strategy);
9040 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9041 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9042 	}
9043 hwrm_func_resc_qcaps_exit:
9044 	hwrm_req_drop(bp, req);
9045 	return rc;
9046 }
9047 
9048 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9049 {
9050 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9051 	struct hwrm_port_mac_ptp_qcfg_input *req;
9052 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9053 	bool phc_cfg;
9054 	u8 flags;
9055 	int rc;
9056 
9057 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9058 		rc = -ENODEV;
9059 		goto no_ptp;
9060 	}
9061 
9062 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9063 	if (rc)
9064 		goto no_ptp;
9065 
9066 	req->port_id = cpu_to_le16(bp->pf.port_id);
9067 	resp = hwrm_req_hold(bp, req);
9068 	rc = hwrm_req_send(bp, req);
9069 	if (rc)
9070 		goto exit;
9071 
9072 	flags = resp->flags;
9073 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9074 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9075 		rc = -ENODEV;
9076 		goto exit;
9077 	}
9078 	if (!ptp) {
9079 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
9080 		if (!ptp) {
9081 			rc = -ENOMEM;
9082 			goto exit;
9083 		}
9084 		ptp->bp = bp;
9085 		bp->ptp_cfg = ptp;
9086 	}
9087 
9088 	if (flags &
9089 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9090 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9091 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9092 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9093 	} else if (BNXT_CHIP_P5(bp)) {
9094 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9095 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9096 	} else {
9097 		rc = -ENODEV;
9098 		goto exit;
9099 	}
9100 	phc_cfg = (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9101 	rc = bnxt_ptp_init(bp, phc_cfg);
9102 	if (rc)
9103 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9104 exit:
9105 	hwrm_req_drop(bp, req);
9106 	if (!rc)
9107 		return 0;
9108 
9109 no_ptp:
9110 	bnxt_ptp_clear(bp);
9111 	kfree(ptp);
9112 	bp->ptp_cfg = NULL;
9113 	return rc;
9114 }
9115 
9116 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9117 {
9118 	struct hwrm_func_qcaps_output *resp;
9119 	struct hwrm_func_qcaps_input *req;
9120 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9121 	u32 flags, flags_ext, flags_ext2;
9122 	int rc;
9123 
9124 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9125 	if (rc)
9126 		return rc;
9127 
9128 	req->fid = cpu_to_le16(0xffff);
9129 	resp = hwrm_req_hold(bp, req);
9130 	rc = hwrm_req_send(bp, req);
9131 	if (rc)
9132 		goto hwrm_func_qcaps_exit;
9133 
9134 	flags = le32_to_cpu(resp->flags);
9135 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9136 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9137 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9138 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9139 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9140 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9141 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9142 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9143 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9144 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9145 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9146 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9147 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9148 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9149 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9150 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9151 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9152 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9153 
9154 	flags_ext = le32_to_cpu(resp->flags_ext);
9155 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9156 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9157 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9158 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9159 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9160 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9161 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9162 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9163 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9164 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9165 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9166 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9167 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9168 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9169 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9170 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9171 
9172 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9173 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9174 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9175 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9176 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9177 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9178 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9179 
9180 	bp->tx_push_thresh = 0;
9181 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9182 	    BNXT_FW_MAJ(bp) > 217)
9183 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9184 
9185 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9186 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9187 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9188 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9189 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9190 	if (!hw_resc->max_hw_ring_grps)
9191 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9192 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9193 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9194 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9195 
9196 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9197 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9198 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9199 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9200 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9201 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9202 
9203 	if (BNXT_PF(bp)) {
9204 		struct bnxt_pf_info *pf = &bp->pf;
9205 
9206 		pf->fw_fid = le16_to_cpu(resp->fid);
9207 		pf->port_id = le16_to_cpu(resp->port_id);
9208 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9209 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9210 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9211 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9212 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9213 			bp->flags |= BNXT_FLAG_WOL_CAP;
9214 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9215 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9216 		} else {
9217 			bnxt_ptp_clear(bp);
9218 			kfree(bp->ptp_cfg);
9219 			bp->ptp_cfg = NULL;
9220 		}
9221 	} else {
9222 #ifdef CONFIG_BNXT_SRIOV
9223 		struct bnxt_vf_info *vf = &bp->vf;
9224 
9225 		vf->fw_fid = le16_to_cpu(resp->fid);
9226 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9227 #endif
9228 	}
9229 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9230 
9231 hwrm_func_qcaps_exit:
9232 	hwrm_req_drop(bp, req);
9233 	return rc;
9234 }
9235 
9236 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9237 {
9238 	struct hwrm_dbg_qcaps_output *resp;
9239 	struct hwrm_dbg_qcaps_input *req;
9240 	int rc;
9241 
9242 	bp->fw_dbg_cap = 0;
9243 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9244 		return;
9245 
9246 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9247 	if (rc)
9248 		return;
9249 
9250 	req->fid = cpu_to_le16(0xffff);
9251 	resp = hwrm_req_hold(bp, req);
9252 	rc = hwrm_req_send(bp, req);
9253 	if (rc)
9254 		goto hwrm_dbg_qcaps_exit;
9255 
9256 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9257 
9258 hwrm_dbg_qcaps_exit:
9259 	hwrm_req_drop(bp, req);
9260 }
9261 
9262 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9263 
9264 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9265 {
9266 	int rc;
9267 
9268 	rc = __bnxt_hwrm_func_qcaps(bp);
9269 	if (rc)
9270 		return rc;
9271 
9272 	bnxt_hwrm_dbg_qcaps(bp);
9273 
9274 	rc = bnxt_hwrm_queue_qportcfg(bp);
9275 	if (rc) {
9276 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9277 		return rc;
9278 	}
9279 	if (bp->hwrm_spec_code >= 0x10803) {
9280 		rc = bnxt_alloc_ctx_mem(bp);
9281 		if (rc)
9282 			return rc;
9283 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9284 		if (!rc)
9285 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9286 	}
9287 	return 0;
9288 }
9289 
9290 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9291 {
9292 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9293 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9294 	u32 flags;
9295 	int rc;
9296 
9297 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9298 		return 0;
9299 
9300 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9301 	if (rc)
9302 		return rc;
9303 
9304 	resp = hwrm_req_hold(bp, req);
9305 	rc = hwrm_req_send(bp, req);
9306 	if (rc)
9307 		goto hwrm_cfa_adv_qcaps_exit;
9308 
9309 	flags = le32_to_cpu(resp->flags);
9310 	if (flags &
9311 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9312 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9313 
9314 	if (flags &
9315 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9316 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9317 
9318 	if (flags &
9319 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9320 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9321 
9322 hwrm_cfa_adv_qcaps_exit:
9323 	hwrm_req_drop(bp, req);
9324 	return rc;
9325 }
9326 
9327 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9328 {
9329 	if (bp->fw_health)
9330 		return 0;
9331 
9332 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9333 	if (!bp->fw_health)
9334 		return -ENOMEM;
9335 
9336 	mutex_init(&bp->fw_health->lock);
9337 	return 0;
9338 }
9339 
9340 static int bnxt_alloc_fw_health(struct bnxt *bp)
9341 {
9342 	int rc;
9343 
9344 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9345 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9346 		return 0;
9347 
9348 	rc = __bnxt_alloc_fw_health(bp);
9349 	if (rc) {
9350 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9351 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9352 		return rc;
9353 	}
9354 
9355 	return 0;
9356 }
9357 
9358 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9359 {
9360 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9361 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9362 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9363 }
9364 
9365 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9366 {
9367 	struct bnxt_fw_health *fw_health = bp->fw_health;
9368 	u32 reg_type;
9369 
9370 	if (!fw_health)
9371 		return;
9372 
9373 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9374 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9375 		fw_health->status_reliable = false;
9376 
9377 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9378 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9379 		fw_health->resets_reliable = false;
9380 }
9381 
9382 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9383 {
9384 	void __iomem *hs;
9385 	u32 status_loc;
9386 	u32 reg_type;
9387 	u32 sig;
9388 
9389 	if (bp->fw_health)
9390 		bp->fw_health->status_reliable = false;
9391 
9392 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9393 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9394 
9395 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9396 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9397 		if (!bp->chip_num) {
9398 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9399 			bp->chip_num = readl(bp->bar0 +
9400 					     BNXT_FW_HEALTH_WIN_BASE +
9401 					     BNXT_GRC_REG_CHIP_NUM);
9402 		}
9403 		if (!BNXT_CHIP_P5_PLUS(bp))
9404 			return;
9405 
9406 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9407 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9408 	} else {
9409 		status_loc = readl(hs + offsetof(struct hcomm_status,
9410 						 fw_status_loc));
9411 	}
9412 
9413 	if (__bnxt_alloc_fw_health(bp)) {
9414 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9415 		return;
9416 	}
9417 
9418 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9419 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9420 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9421 		__bnxt_map_fw_health_reg(bp, status_loc);
9422 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9423 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9424 	}
9425 
9426 	bp->fw_health->status_reliable = true;
9427 }
9428 
9429 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9430 {
9431 	struct bnxt_fw_health *fw_health = bp->fw_health;
9432 	u32 reg_base = 0xffffffff;
9433 	int i;
9434 
9435 	bp->fw_health->status_reliable = false;
9436 	bp->fw_health->resets_reliable = false;
9437 	/* Only pre-map the monitoring GRC registers using window 3 */
9438 	for (i = 0; i < 4; i++) {
9439 		u32 reg = fw_health->regs[i];
9440 
9441 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9442 			continue;
9443 		if (reg_base == 0xffffffff)
9444 			reg_base = reg & BNXT_GRC_BASE_MASK;
9445 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9446 			return -ERANGE;
9447 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9448 	}
9449 	bp->fw_health->status_reliable = true;
9450 	bp->fw_health->resets_reliable = true;
9451 	if (reg_base == 0xffffffff)
9452 		return 0;
9453 
9454 	__bnxt_map_fw_health_reg(bp, reg_base);
9455 	return 0;
9456 }
9457 
9458 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9459 {
9460 	if (!bp->fw_health)
9461 		return;
9462 
9463 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9464 		bp->fw_health->status_reliable = true;
9465 		bp->fw_health->resets_reliable = true;
9466 	} else {
9467 		bnxt_try_map_fw_health_reg(bp);
9468 	}
9469 }
9470 
9471 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9472 {
9473 	struct bnxt_fw_health *fw_health = bp->fw_health;
9474 	struct hwrm_error_recovery_qcfg_output *resp;
9475 	struct hwrm_error_recovery_qcfg_input *req;
9476 	int rc, i;
9477 
9478 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9479 		return 0;
9480 
9481 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9482 	if (rc)
9483 		return rc;
9484 
9485 	resp = hwrm_req_hold(bp, req);
9486 	rc = hwrm_req_send(bp, req);
9487 	if (rc)
9488 		goto err_recovery_out;
9489 	fw_health->flags = le32_to_cpu(resp->flags);
9490 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
9491 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
9492 		rc = -EINVAL;
9493 		goto err_recovery_out;
9494 	}
9495 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
9496 	fw_health->master_func_wait_dsecs =
9497 		le32_to_cpu(resp->master_func_wait_period);
9498 	fw_health->normal_func_wait_dsecs =
9499 		le32_to_cpu(resp->normal_func_wait_period);
9500 	fw_health->post_reset_wait_dsecs =
9501 		le32_to_cpu(resp->master_func_wait_period_after_reset);
9502 	fw_health->post_reset_max_wait_dsecs =
9503 		le32_to_cpu(resp->max_bailout_time_after_reset);
9504 	fw_health->regs[BNXT_FW_HEALTH_REG] =
9505 		le32_to_cpu(resp->fw_health_status_reg);
9506 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
9507 		le32_to_cpu(resp->fw_heartbeat_reg);
9508 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
9509 		le32_to_cpu(resp->fw_reset_cnt_reg);
9510 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
9511 		le32_to_cpu(resp->reset_inprogress_reg);
9512 	fw_health->fw_reset_inprog_reg_mask =
9513 		le32_to_cpu(resp->reset_inprogress_reg_mask);
9514 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
9515 	if (fw_health->fw_reset_seq_cnt >= 16) {
9516 		rc = -EINVAL;
9517 		goto err_recovery_out;
9518 	}
9519 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
9520 		fw_health->fw_reset_seq_regs[i] =
9521 			le32_to_cpu(resp->reset_reg[i]);
9522 		fw_health->fw_reset_seq_vals[i] =
9523 			le32_to_cpu(resp->reset_reg_val[i]);
9524 		fw_health->fw_reset_seq_delay_msec[i] =
9525 			resp->delay_after_reset[i];
9526 	}
9527 err_recovery_out:
9528 	hwrm_req_drop(bp, req);
9529 	if (!rc)
9530 		rc = bnxt_map_fw_health_regs(bp);
9531 	if (rc)
9532 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9533 	return rc;
9534 }
9535 
9536 static int bnxt_hwrm_func_reset(struct bnxt *bp)
9537 {
9538 	struct hwrm_func_reset_input *req;
9539 	int rc;
9540 
9541 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
9542 	if (rc)
9543 		return rc;
9544 
9545 	req->enables = 0;
9546 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
9547 	return hwrm_req_send(bp, req);
9548 }
9549 
9550 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
9551 {
9552 	struct hwrm_nvm_get_dev_info_output nvm_info;
9553 
9554 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
9555 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
9556 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
9557 			 nvm_info.nvm_cfg_ver_upd);
9558 }
9559 
9560 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
9561 {
9562 	struct hwrm_queue_qportcfg_output *resp;
9563 	struct hwrm_queue_qportcfg_input *req;
9564 	u8 i, j, *qptr;
9565 	bool no_rdma;
9566 	int rc = 0;
9567 
9568 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
9569 	if (rc)
9570 		return rc;
9571 
9572 	resp = hwrm_req_hold(bp, req);
9573 	rc = hwrm_req_send(bp, req);
9574 	if (rc)
9575 		goto qportcfg_exit;
9576 
9577 	if (!resp->max_configurable_queues) {
9578 		rc = -EINVAL;
9579 		goto qportcfg_exit;
9580 	}
9581 	bp->max_tc = resp->max_configurable_queues;
9582 	bp->max_lltc = resp->max_configurable_lossless_queues;
9583 	if (bp->max_tc > BNXT_MAX_QUEUE)
9584 		bp->max_tc = BNXT_MAX_QUEUE;
9585 
9586 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
9587 	qptr = &resp->queue_id0;
9588 	for (i = 0, j = 0; i < bp->max_tc; i++) {
9589 		bp->q_info[j].queue_id = *qptr;
9590 		bp->q_ids[i] = *qptr++;
9591 		bp->q_info[j].queue_profile = *qptr++;
9592 		bp->tc_to_qidx[j] = j;
9593 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
9594 		    (no_rdma && BNXT_PF(bp)))
9595 			j++;
9596 	}
9597 	bp->max_q = bp->max_tc;
9598 	bp->max_tc = max_t(u8, j, 1);
9599 
9600 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
9601 		bp->max_tc = 1;
9602 
9603 	if (bp->max_lltc > bp->max_tc)
9604 		bp->max_lltc = bp->max_tc;
9605 
9606 qportcfg_exit:
9607 	hwrm_req_drop(bp, req);
9608 	return rc;
9609 }
9610 
9611 static int bnxt_hwrm_poll(struct bnxt *bp)
9612 {
9613 	struct hwrm_ver_get_input *req;
9614 	int rc;
9615 
9616 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9617 	if (rc)
9618 		return rc;
9619 
9620 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9621 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9622 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9623 
9624 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
9625 	rc = hwrm_req_send(bp, req);
9626 	return rc;
9627 }
9628 
9629 static int bnxt_hwrm_ver_get(struct bnxt *bp)
9630 {
9631 	struct hwrm_ver_get_output *resp;
9632 	struct hwrm_ver_get_input *req;
9633 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
9634 	u32 dev_caps_cfg, hwrm_ver;
9635 	int rc, len;
9636 
9637 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9638 	if (rc)
9639 		return rc;
9640 
9641 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
9642 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
9643 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9644 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9645 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9646 
9647 	resp = hwrm_req_hold(bp, req);
9648 	rc = hwrm_req_send(bp, req);
9649 	if (rc)
9650 		goto hwrm_ver_get_exit;
9651 
9652 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
9653 
9654 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
9655 			     resp->hwrm_intf_min_8b << 8 |
9656 			     resp->hwrm_intf_upd_8b;
9657 	if (resp->hwrm_intf_maj_8b < 1) {
9658 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
9659 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
9660 			    resp->hwrm_intf_upd_8b);
9661 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
9662 	}
9663 
9664 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
9665 			HWRM_VERSION_UPDATE;
9666 
9667 	if (bp->hwrm_spec_code > hwrm_ver)
9668 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
9669 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
9670 			 HWRM_VERSION_UPDATE);
9671 	else
9672 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
9673 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
9674 			 resp->hwrm_intf_upd_8b);
9675 
9676 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
9677 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
9678 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
9679 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
9680 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
9681 		len = FW_VER_STR_LEN;
9682 	} else {
9683 		fw_maj = resp->hwrm_fw_maj_8b;
9684 		fw_min = resp->hwrm_fw_min_8b;
9685 		fw_bld = resp->hwrm_fw_bld_8b;
9686 		fw_rsv = resp->hwrm_fw_rsvd_8b;
9687 		len = BC_HWRM_STR_LEN;
9688 	}
9689 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
9690 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
9691 		 fw_rsv);
9692 
9693 	if (strlen(resp->active_pkg_name)) {
9694 		int fw_ver_len = strlen(bp->fw_ver_str);
9695 
9696 		snprintf(bp->fw_ver_str + fw_ver_len,
9697 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
9698 			 resp->active_pkg_name);
9699 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
9700 	}
9701 
9702 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
9703 	if (!bp->hwrm_cmd_timeout)
9704 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
9705 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
9706 	if (!bp->hwrm_cmd_max_timeout)
9707 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
9708 	else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT)
9709 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n",
9710 			    bp->hwrm_cmd_max_timeout / 1000);
9711 
9712 	if (resp->hwrm_intf_maj_8b >= 1) {
9713 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
9714 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
9715 	}
9716 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
9717 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
9718 
9719 	bp->chip_num = le16_to_cpu(resp->chip_num);
9720 	bp->chip_rev = resp->chip_rev;
9721 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
9722 	    !resp->chip_metal)
9723 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
9724 
9725 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
9726 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
9727 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
9728 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
9729 
9730 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
9731 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
9732 
9733 	if (dev_caps_cfg &
9734 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
9735 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
9736 
9737 	if (dev_caps_cfg &
9738 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
9739 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
9740 
9741 	if (dev_caps_cfg &
9742 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
9743 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
9744 
9745 hwrm_ver_get_exit:
9746 	hwrm_req_drop(bp, req);
9747 	return rc;
9748 }
9749 
9750 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
9751 {
9752 	struct hwrm_fw_set_time_input *req;
9753 	struct tm tm;
9754 	time64_t now = ktime_get_real_seconds();
9755 	int rc;
9756 
9757 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
9758 	    bp->hwrm_spec_code < 0x10400)
9759 		return -EOPNOTSUPP;
9760 
9761 	time64_to_tm(now, 0, &tm);
9762 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
9763 	if (rc)
9764 		return rc;
9765 
9766 	req->year = cpu_to_le16(1900 + tm.tm_year);
9767 	req->month = 1 + tm.tm_mon;
9768 	req->day = tm.tm_mday;
9769 	req->hour = tm.tm_hour;
9770 	req->minute = tm.tm_min;
9771 	req->second = tm.tm_sec;
9772 	return hwrm_req_send(bp, req);
9773 }
9774 
9775 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
9776 {
9777 	u64 sw_tmp;
9778 
9779 	hw &= mask;
9780 	sw_tmp = (*sw & ~mask) | hw;
9781 	if (hw < (*sw & mask))
9782 		sw_tmp += mask + 1;
9783 	WRITE_ONCE(*sw, sw_tmp);
9784 }
9785 
9786 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
9787 				    int count, bool ignore_zero)
9788 {
9789 	int i;
9790 
9791 	for (i = 0; i < count; i++) {
9792 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
9793 
9794 		if (ignore_zero && !hw)
9795 			continue;
9796 
9797 		if (masks[i] == -1ULL)
9798 			sw_stats[i] = hw;
9799 		else
9800 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
9801 	}
9802 }
9803 
9804 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
9805 {
9806 	if (!stats->hw_stats)
9807 		return;
9808 
9809 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
9810 				stats->hw_masks, stats->len / 8, false);
9811 }
9812 
9813 static void bnxt_accumulate_all_stats(struct bnxt *bp)
9814 {
9815 	struct bnxt_stats_mem *ring0_stats;
9816 	bool ignore_zero = false;
9817 	int i;
9818 
9819 	/* Chip bug.  Counter intermittently becomes 0. */
9820 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
9821 		ignore_zero = true;
9822 
9823 	for (i = 0; i < bp->cp_nr_rings; i++) {
9824 		struct bnxt_napi *bnapi = bp->bnapi[i];
9825 		struct bnxt_cp_ring_info *cpr;
9826 		struct bnxt_stats_mem *stats;
9827 
9828 		cpr = &bnapi->cp_ring;
9829 		stats = &cpr->stats;
9830 		if (!i)
9831 			ring0_stats = stats;
9832 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
9833 					ring0_stats->hw_masks,
9834 					ring0_stats->len / 8, ignore_zero);
9835 	}
9836 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
9837 		struct bnxt_stats_mem *stats = &bp->port_stats;
9838 		__le64 *hw_stats = stats->hw_stats;
9839 		u64 *sw_stats = stats->sw_stats;
9840 		u64 *masks = stats->hw_masks;
9841 		int cnt;
9842 
9843 		cnt = sizeof(struct rx_port_stats) / 8;
9844 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
9845 
9846 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9847 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9848 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9849 		cnt = sizeof(struct tx_port_stats) / 8;
9850 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
9851 	}
9852 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
9853 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
9854 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
9855 	}
9856 }
9857 
9858 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
9859 {
9860 	struct hwrm_port_qstats_input *req;
9861 	struct bnxt_pf_info *pf = &bp->pf;
9862 	int rc;
9863 
9864 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
9865 		return 0;
9866 
9867 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
9868 		return -EOPNOTSUPP;
9869 
9870 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
9871 	if (rc)
9872 		return rc;
9873 
9874 	req->flags = flags;
9875 	req->port_id = cpu_to_le16(pf->port_id);
9876 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
9877 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
9878 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
9879 	return hwrm_req_send(bp, req);
9880 }
9881 
9882 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
9883 {
9884 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
9885 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
9886 	struct hwrm_port_qstats_ext_output *resp_qs;
9887 	struct hwrm_port_qstats_ext_input *req_qs;
9888 	struct bnxt_pf_info *pf = &bp->pf;
9889 	u32 tx_stat_size;
9890 	int rc;
9891 
9892 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
9893 		return 0;
9894 
9895 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
9896 		return -EOPNOTSUPP;
9897 
9898 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
9899 	if (rc)
9900 		return rc;
9901 
9902 	req_qs->flags = flags;
9903 	req_qs->port_id = cpu_to_le16(pf->port_id);
9904 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
9905 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
9906 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
9907 		       sizeof(struct tx_port_stats_ext) : 0;
9908 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
9909 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
9910 	resp_qs = hwrm_req_hold(bp, req_qs);
9911 	rc = hwrm_req_send(bp, req_qs);
9912 	if (!rc) {
9913 		bp->fw_rx_stats_ext_size =
9914 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
9915 		if (BNXT_FW_MAJ(bp) < 220 &&
9916 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
9917 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
9918 
9919 		bp->fw_tx_stats_ext_size = tx_stat_size ?
9920 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
9921 	} else {
9922 		bp->fw_rx_stats_ext_size = 0;
9923 		bp->fw_tx_stats_ext_size = 0;
9924 	}
9925 	hwrm_req_drop(bp, req_qs);
9926 
9927 	if (flags)
9928 		return rc;
9929 
9930 	if (bp->fw_tx_stats_ext_size <=
9931 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
9932 		bp->pri2cos_valid = 0;
9933 		return rc;
9934 	}
9935 
9936 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
9937 	if (rc)
9938 		return rc;
9939 
9940 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
9941 
9942 	resp_qc = hwrm_req_hold(bp, req_qc);
9943 	rc = hwrm_req_send(bp, req_qc);
9944 	if (!rc) {
9945 		u8 *pri2cos;
9946 		int i, j;
9947 
9948 		pri2cos = &resp_qc->pri0_cos_queue_id;
9949 		for (i = 0; i < 8; i++) {
9950 			u8 queue_id = pri2cos[i];
9951 			u8 queue_idx;
9952 
9953 			/* Per port queue IDs start from 0, 10, 20, etc */
9954 			queue_idx = queue_id % 10;
9955 			if (queue_idx > BNXT_MAX_QUEUE) {
9956 				bp->pri2cos_valid = false;
9957 				hwrm_req_drop(bp, req_qc);
9958 				return rc;
9959 			}
9960 			for (j = 0; j < bp->max_q; j++) {
9961 				if (bp->q_ids[j] == queue_id)
9962 					bp->pri2cos_idx[i] = queue_idx;
9963 			}
9964 		}
9965 		bp->pri2cos_valid = true;
9966 	}
9967 	hwrm_req_drop(bp, req_qc);
9968 
9969 	return rc;
9970 }
9971 
9972 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
9973 {
9974 	bnxt_hwrm_tunnel_dst_port_free(bp,
9975 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
9976 	bnxt_hwrm_tunnel_dst_port_free(bp,
9977 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
9978 }
9979 
9980 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
9981 {
9982 	int rc, i;
9983 	u32 tpa_flags = 0;
9984 
9985 	if (set_tpa)
9986 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
9987 	else if (BNXT_NO_FW_ACCESS(bp))
9988 		return 0;
9989 	for (i = 0; i < bp->nr_vnics; i++) {
9990 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
9991 		if (rc) {
9992 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
9993 				   i, rc);
9994 			return rc;
9995 		}
9996 	}
9997 	return 0;
9998 }
9999 
10000 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10001 {
10002 	int i;
10003 
10004 	for (i = 0; i < bp->nr_vnics; i++)
10005 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10006 }
10007 
10008 static void bnxt_clear_vnic(struct bnxt *bp)
10009 {
10010 	if (!bp->vnic_info)
10011 		return;
10012 
10013 	bnxt_hwrm_clear_vnic_filter(bp);
10014 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10015 		/* clear all RSS setting before free vnic ctx */
10016 		bnxt_hwrm_clear_vnic_rss(bp);
10017 		bnxt_hwrm_vnic_ctx_free(bp);
10018 	}
10019 	/* before free the vnic, undo the vnic tpa settings */
10020 	if (bp->flags & BNXT_FLAG_TPA)
10021 		bnxt_set_tpa(bp, false);
10022 	bnxt_hwrm_vnic_free(bp);
10023 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10024 		bnxt_hwrm_vnic_ctx_free(bp);
10025 }
10026 
10027 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10028 				    bool irq_re_init)
10029 {
10030 	bnxt_clear_vnic(bp);
10031 	bnxt_hwrm_ring_free(bp, close_path);
10032 	bnxt_hwrm_ring_grp_free(bp);
10033 	if (irq_re_init) {
10034 		bnxt_hwrm_stat_ctx_free(bp);
10035 		bnxt_hwrm_free_tunnel_ports(bp);
10036 	}
10037 }
10038 
10039 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10040 {
10041 	struct hwrm_func_cfg_input *req;
10042 	u8 evb_mode;
10043 	int rc;
10044 
10045 	if (br_mode == BRIDGE_MODE_VEB)
10046 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10047 	else if (br_mode == BRIDGE_MODE_VEPA)
10048 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10049 	else
10050 		return -EINVAL;
10051 
10052 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10053 	if (rc)
10054 		return rc;
10055 
10056 	req->fid = cpu_to_le16(0xffff);
10057 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10058 	req->evb_mode = evb_mode;
10059 	return hwrm_req_send(bp, req);
10060 }
10061 
10062 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10063 {
10064 	struct hwrm_func_cfg_input *req;
10065 	int rc;
10066 
10067 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10068 		return 0;
10069 
10070 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10071 	if (rc)
10072 		return rc;
10073 
10074 	req->fid = cpu_to_le16(0xffff);
10075 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10076 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10077 	if (size == 128)
10078 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10079 
10080 	return hwrm_req_send(bp, req);
10081 }
10082 
10083 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10084 {
10085 	int rc;
10086 
10087 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10088 		goto skip_rss_ctx;
10089 
10090 	/* allocate context for vnic */
10091 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10092 	if (rc) {
10093 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10094 			   vnic->vnic_id, rc);
10095 		goto vnic_setup_err;
10096 	}
10097 	bp->rsscos_nr_ctxs++;
10098 
10099 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10100 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10101 		if (rc) {
10102 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10103 				   vnic->vnic_id, rc);
10104 			goto vnic_setup_err;
10105 		}
10106 		bp->rsscos_nr_ctxs++;
10107 	}
10108 
10109 skip_rss_ctx:
10110 	/* configure default vnic, ring grp */
10111 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10112 	if (rc) {
10113 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10114 			   vnic->vnic_id, rc);
10115 		goto vnic_setup_err;
10116 	}
10117 
10118 	/* Enable RSS hashing on vnic */
10119 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10120 	if (rc) {
10121 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10122 			   vnic->vnic_id, rc);
10123 		goto vnic_setup_err;
10124 	}
10125 
10126 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10127 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10128 		if (rc) {
10129 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10130 				   vnic->vnic_id, rc);
10131 		}
10132 	}
10133 
10134 vnic_setup_err:
10135 	return rc;
10136 }
10137 
10138 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10139 			  u8 valid)
10140 {
10141 	struct hwrm_vnic_update_input *req;
10142 	int rc;
10143 
10144 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10145 	if (rc)
10146 		return rc;
10147 
10148 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10149 
10150 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10151 		req->mru = cpu_to_le16(vnic->mru);
10152 
10153 	req->enables = cpu_to_le32(valid);
10154 
10155 	return hwrm_req_send(bp, req);
10156 }
10157 
10158 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10159 {
10160 	int rc;
10161 
10162 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10163 	if (rc) {
10164 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10165 			   vnic->vnic_id, rc);
10166 		return rc;
10167 	}
10168 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10169 	if (rc)
10170 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10171 			   vnic->vnic_id, rc);
10172 	return rc;
10173 }
10174 
10175 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10176 {
10177 	int rc, i, nr_ctxs;
10178 
10179 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10180 	for (i = 0; i < nr_ctxs; i++) {
10181 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10182 		if (rc) {
10183 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10184 				   vnic->vnic_id, i, rc);
10185 			break;
10186 		}
10187 		bp->rsscos_nr_ctxs++;
10188 	}
10189 	if (i < nr_ctxs)
10190 		return -ENOMEM;
10191 
10192 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10193 	if (rc)
10194 		return rc;
10195 
10196 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10197 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10198 		if (rc) {
10199 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10200 				   vnic->vnic_id, rc);
10201 		}
10202 	}
10203 	return rc;
10204 }
10205 
10206 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10207 {
10208 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10209 		return __bnxt_setup_vnic_p5(bp, vnic);
10210 	else
10211 		return __bnxt_setup_vnic(bp, vnic);
10212 }
10213 
10214 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10215 				     struct bnxt_vnic_info *vnic,
10216 				     u16 start_rx_ring_idx, int rx_rings)
10217 {
10218 	int rc;
10219 
10220 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10221 	if (rc) {
10222 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10223 			   vnic->vnic_id, rc);
10224 		return rc;
10225 	}
10226 	return bnxt_setup_vnic(bp, vnic);
10227 }
10228 
10229 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10230 {
10231 	struct bnxt_vnic_info *vnic;
10232 	int i, rc = 0;
10233 
10234 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10235 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10236 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10237 	}
10238 
10239 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10240 		return 0;
10241 
10242 	for (i = 0; i < bp->rx_nr_rings; i++) {
10243 		u16 vnic_id = i + 1;
10244 		u16 ring_id = i;
10245 
10246 		if (vnic_id >= bp->nr_vnics)
10247 			break;
10248 
10249 		vnic = &bp->vnic_info[vnic_id];
10250 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10251 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10252 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10253 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10254 			break;
10255 	}
10256 	return rc;
10257 }
10258 
10259 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10260 			  bool all)
10261 {
10262 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10263 	struct bnxt_filter_base *usr_fltr, *tmp;
10264 	struct bnxt_ntuple_filter *ntp_fltr;
10265 	int i;
10266 
10267 	if (netif_running(bp->dev)) {
10268 		bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10269 		for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10270 			if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10271 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10272 		}
10273 	}
10274 	if (!all)
10275 		return;
10276 
10277 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10278 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10279 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10280 			ntp_fltr = container_of(usr_fltr,
10281 						struct bnxt_ntuple_filter,
10282 						base);
10283 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10284 			bnxt_del_ntp_filter(bp, ntp_fltr);
10285 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10286 		}
10287 	}
10288 
10289 	if (vnic->rss_table)
10290 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10291 				  vnic->rss_table,
10292 				  vnic->rss_table_dma_addr);
10293 	bp->num_rss_ctx--;
10294 }
10295 
10296 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10297 {
10298 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10299 	struct ethtool_rxfh_context *ctx;
10300 	unsigned long context;
10301 
10302 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10303 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10304 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10305 
10306 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10307 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10308 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10309 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10310 				   rss_ctx->index);
10311 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10312 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
10313 		}
10314 	}
10315 }
10316 
10317 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
10318 {
10319 	struct ethtool_rxfh_context *ctx;
10320 	unsigned long context;
10321 
10322 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10323 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10324 
10325 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
10326 	}
10327 }
10328 
10329 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
10330 static bool bnxt_promisc_ok(struct bnxt *bp)
10331 {
10332 #ifdef CONFIG_BNXT_SRIOV
10333 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10334 		return false;
10335 #endif
10336 	return true;
10337 }
10338 
10339 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10340 {
10341 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10342 	unsigned int rc = 0;
10343 
10344 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10345 	if (rc) {
10346 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10347 			   rc);
10348 		return rc;
10349 	}
10350 
10351 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10352 	if (rc) {
10353 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10354 			   rc);
10355 		return rc;
10356 	}
10357 	return rc;
10358 }
10359 
10360 static int bnxt_cfg_rx_mode(struct bnxt *);
10361 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10362 
10363 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10364 {
10365 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10366 	int rc = 0;
10367 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10368 
10369 	if (irq_re_init) {
10370 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10371 		if (rc) {
10372 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10373 				   rc);
10374 			goto err_out;
10375 		}
10376 	}
10377 
10378 	rc = bnxt_hwrm_ring_alloc(bp);
10379 	if (rc) {
10380 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10381 		goto err_out;
10382 	}
10383 
10384 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10385 	if (rc) {
10386 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10387 		goto err_out;
10388 	}
10389 
10390 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10391 		rx_nr_rings--;
10392 
10393 	/* default vnic 0 */
10394 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10395 	if (rc) {
10396 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10397 		goto err_out;
10398 	}
10399 
10400 	if (BNXT_VF(bp))
10401 		bnxt_hwrm_func_qcfg(bp);
10402 
10403 	rc = bnxt_setup_vnic(bp, vnic);
10404 	if (rc)
10405 		goto err_out;
10406 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10407 		bnxt_hwrm_update_rss_hash_cfg(bp);
10408 
10409 	if (bp->flags & BNXT_FLAG_RFS) {
10410 		rc = bnxt_alloc_rfs_vnics(bp);
10411 		if (rc)
10412 			goto err_out;
10413 	}
10414 
10415 	if (bp->flags & BNXT_FLAG_TPA) {
10416 		rc = bnxt_set_tpa(bp, true);
10417 		if (rc)
10418 			goto err_out;
10419 	}
10420 
10421 	if (BNXT_VF(bp))
10422 		bnxt_update_vf_mac(bp);
10423 
10424 	/* Filter for default vnic 0 */
10425 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
10426 	if (rc) {
10427 		if (BNXT_VF(bp) && rc == -ENODEV)
10428 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
10429 		else
10430 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
10431 		goto err_out;
10432 	}
10433 	vnic->uc_filter_count = 1;
10434 
10435 	vnic->rx_mask = 0;
10436 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
10437 		goto skip_rx_mask;
10438 
10439 	if (bp->dev->flags & IFF_BROADCAST)
10440 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10441 
10442 	if (bp->dev->flags & IFF_PROMISC)
10443 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10444 
10445 	if (bp->dev->flags & IFF_ALLMULTI) {
10446 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10447 		vnic->mc_list_count = 0;
10448 	} else if (bp->dev->flags & IFF_MULTICAST) {
10449 		u32 mask = 0;
10450 
10451 		bnxt_mc_list_updated(bp, &mask);
10452 		vnic->rx_mask |= mask;
10453 	}
10454 
10455 	rc = bnxt_cfg_rx_mode(bp);
10456 	if (rc)
10457 		goto err_out;
10458 
10459 skip_rx_mask:
10460 	rc = bnxt_hwrm_set_coal(bp);
10461 	if (rc)
10462 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
10463 				rc);
10464 
10465 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10466 		rc = bnxt_setup_nitroa0_vnic(bp);
10467 		if (rc)
10468 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
10469 				   rc);
10470 	}
10471 
10472 	if (BNXT_VF(bp)) {
10473 		bnxt_hwrm_func_qcfg(bp);
10474 		netdev_update_features(bp->dev);
10475 	}
10476 
10477 	return 0;
10478 
10479 err_out:
10480 	bnxt_hwrm_resource_free(bp, 0, true);
10481 
10482 	return rc;
10483 }
10484 
10485 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
10486 {
10487 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
10488 	return 0;
10489 }
10490 
10491 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
10492 {
10493 	bnxt_init_cp_rings(bp);
10494 	bnxt_init_rx_rings(bp);
10495 	bnxt_init_tx_rings(bp);
10496 	bnxt_init_ring_grps(bp, irq_re_init);
10497 	bnxt_init_vnics(bp);
10498 
10499 	return bnxt_init_chip(bp, irq_re_init);
10500 }
10501 
10502 static int bnxt_set_real_num_queues(struct bnxt *bp)
10503 {
10504 	int rc;
10505 	struct net_device *dev = bp->dev;
10506 
10507 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
10508 					  bp->tx_nr_rings_xdp);
10509 	if (rc)
10510 		return rc;
10511 
10512 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
10513 	if (rc)
10514 		return rc;
10515 
10516 #ifdef CONFIG_RFS_ACCEL
10517 	if (bp->flags & BNXT_FLAG_RFS)
10518 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
10519 #endif
10520 
10521 	return rc;
10522 }
10523 
10524 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10525 			     bool shared)
10526 {
10527 	int _rx = *rx, _tx = *tx;
10528 
10529 	if (shared) {
10530 		*rx = min_t(int, _rx, max);
10531 		*tx = min_t(int, _tx, max);
10532 	} else {
10533 		if (max < 2)
10534 			return -ENOMEM;
10535 
10536 		while (_rx + _tx > max) {
10537 			if (_rx > _tx && _rx > 1)
10538 				_rx--;
10539 			else if (_tx > 1)
10540 				_tx--;
10541 		}
10542 		*rx = _rx;
10543 		*tx = _tx;
10544 	}
10545 	return 0;
10546 }
10547 
10548 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
10549 {
10550 	return (tx - tx_xdp) / tx_sets + tx_xdp;
10551 }
10552 
10553 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
10554 {
10555 	int tcs = bp->num_tc;
10556 
10557 	if (!tcs)
10558 		tcs = 1;
10559 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
10560 }
10561 
10562 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
10563 {
10564 	int tcs = bp->num_tc;
10565 
10566 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
10567 	       bp->tx_nr_rings_xdp;
10568 }
10569 
10570 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10571 			   bool sh)
10572 {
10573 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
10574 
10575 	if (tx_cp != *tx) {
10576 		int tx_saved = tx_cp, rc;
10577 
10578 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
10579 		if (rc)
10580 			return rc;
10581 		if (tx_cp != tx_saved)
10582 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
10583 		return 0;
10584 	}
10585 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
10586 }
10587 
10588 static void bnxt_setup_msix(struct bnxt *bp)
10589 {
10590 	const int len = sizeof(bp->irq_tbl[0].name);
10591 	struct net_device *dev = bp->dev;
10592 	int tcs, i;
10593 
10594 	tcs = bp->num_tc;
10595 	if (tcs) {
10596 		int i, off, count;
10597 
10598 		for (i = 0; i < tcs; i++) {
10599 			count = bp->tx_nr_rings_per_tc;
10600 			off = BNXT_TC_TO_RING_BASE(bp, i);
10601 			netdev_set_tc_queue(dev, i, count, off);
10602 		}
10603 	}
10604 
10605 	for (i = 0; i < bp->cp_nr_rings; i++) {
10606 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10607 		char *attr;
10608 
10609 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
10610 			attr = "TxRx";
10611 		else if (i < bp->rx_nr_rings)
10612 			attr = "rx";
10613 		else
10614 			attr = "tx";
10615 
10616 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
10617 			 attr, i);
10618 		bp->irq_tbl[map_idx].handler = bnxt_msix;
10619 	}
10620 }
10621 
10622 static int bnxt_init_int_mode(struct bnxt *bp);
10623 
10624 static int bnxt_change_msix(struct bnxt *bp, int total)
10625 {
10626 	struct msi_map map;
10627 	int i;
10628 
10629 	/* add MSIX to the end if needed */
10630 	for (i = bp->total_irqs; i < total; i++) {
10631 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
10632 		if (map.index < 0)
10633 			return bp->total_irqs;
10634 		bp->irq_tbl[i].vector = map.virq;
10635 		bp->total_irqs++;
10636 	}
10637 
10638 	/* trim MSIX from the end if needed */
10639 	for (i = bp->total_irqs; i > total; i--) {
10640 		map.index = i - 1;
10641 		map.virq = bp->irq_tbl[i - 1].vector;
10642 		pci_msix_free_irq(bp->pdev, map);
10643 		bp->total_irqs--;
10644 	}
10645 	return bp->total_irqs;
10646 }
10647 
10648 static int bnxt_setup_int_mode(struct bnxt *bp)
10649 {
10650 	int rc;
10651 
10652 	if (!bp->irq_tbl) {
10653 		rc = bnxt_init_int_mode(bp);
10654 		if (rc || !bp->irq_tbl)
10655 			return rc ?: -ENODEV;
10656 	}
10657 
10658 	bnxt_setup_msix(bp);
10659 
10660 	rc = bnxt_set_real_num_queues(bp);
10661 	return rc;
10662 }
10663 
10664 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
10665 {
10666 	return bp->hw_resc.max_rsscos_ctxs;
10667 }
10668 
10669 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
10670 {
10671 	return bp->hw_resc.max_vnics;
10672 }
10673 
10674 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
10675 {
10676 	return bp->hw_resc.max_stat_ctxs;
10677 }
10678 
10679 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
10680 {
10681 	return bp->hw_resc.max_cp_rings;
10682 }
10683 
10684 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
10685 {
10686 	unsigned int cp = bp->hw_resc.max_cp_rings;
10687 
10688 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
10689 		cp -= bnxt_get_ulp_msix_num(bp);
10690 
10691 	return cp;
10692 }
10693 
10694 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
10695 {
10696 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
10697 
10698 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10699 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
10700 
10701 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
10702 }
10703 
10704 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
10705 {
10706 	bp->hw_resc.max_irqs = max_irqs;
10707 }
10708 
10709 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
10710 {
10711 	unsigned int cp;
10712 
10713 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
10714 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10715 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
10716 	else
10717 		return cp - bp->cp_nr_rings;
10718 }
10719 
10720 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
10721 {
10722 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
10723 }
10724 
10725 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
10726 {
10727 	int max_irq = bnxt_get_max_func_irqs(bp);
10728 	int total_req = bp->cp_nr_rings + num;
10729 
10730 	if (max_irq < total_req) {
10731 		num = max_irq - bp->cp_nr_rings;
10732 		if (num <= 0)
10733 			return 0;
10734 	}
10735 	return num;
10736 }
10737 
10738 static int bnxt_get_num_msix(struct bnxt *bp)
10739 {
10740 	if (!BNXT_NEW_RM(bp))
10741 		return bnxt_get_max_func_irqs(bp);
10742 
10743 	return bnxt_nq_rings_in_use(bp);
10744 }
10745 
10746 static int bnxt_init_int_mode(struct bnxt *bp)
10747 {
10748 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
10749 
10750 	total_vecs = bnxt_get_num_msix(bp);
10751 	max = bnxt_get_max_func_irqs(bp);
10752 	if (total_vecs > max)
10753 		total_vecs = max;
10754 
10755 	if (!total_vecs)
10756 		return 0;
10757 
10758 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
10759 		min = 2;
10760 
10761 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
10762 					   PCI_IRQ_MSIX);
10763 	ulp_msix = bnxt_get_ulp_msix_num(bp);
10764 	if (total_vecs < 0 || total_vecs < ulp_msix) {
10765 		rc = -ENODEV;
10766 		goto msix_setup_exit;
10767 	}
10768 
10769 	tbl_size = total_vecs;
10770 	if (pci_msix_can_alloc_dyn(bp->pdev))
10771 		tbl_size = max;
10772 	bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL);
10773 	if (bp->irq_tbl) {
10774 		for (i = 0; i < total_vecs; i++)
10775 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
10776 
10777 		bp->total_irqs = total_vecs;
10778 		/* Trim rings based upon num of vectors allocated */
10779 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
10780 				     total_vecs - ulp_msix, min == 1);
10781 		if (rc)
10782 			goto msix_setup_exit;
10783 
10784 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
10785 		bp->cp_nr_rings = (min == 1) ?
10786 				  max_t(int, tx_cp, bp->rx_nr_rings) :
10787 				  tx_cp + bp->rx_nr_rings;
10788 
10789 	} else {
10790 		rc = -ENOMEM;
10791 		goto msix_setup_exit;
10792 	}
10793 	return 0;
10794 
10795 msix_setup_exit:
10796 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
10797 	kfree(bp->irq_tbl);
10798 	bp->irq_tbl = NULL;
10799 	pci_free_irq_vectors(bp->pdev);
10800 	return rc;
10801 }
10802 
10803 static void bnxt_clear_int_mode(struct bnxt *bp)
10804 {
10805 	pci_free_irq_vectors(bp->pdev);
10806 
10807 	kfree(bp->irq_tbl);
10808 	bp->irq_tbl = NULL;
10809 }
10810 
10811 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
10812 {
10813 	bool irq_cleared = false;
10814 	bool irq_change = 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 		irq_change = true;
10834 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
10835 			bnxt_ulp_irq_stop(bp);
10836 			bnxt_clear_int_mode(bp);
10837 			irq_cleared = true;
10838 		}
10839 	}
10840 	rc = __bnxt_reserve_rings(bp);
10841 	if (irq_cleared) {
10842 		if (!rc)
10843 			rc = bnxt_init_int_mode(bp);
10844 		bnxt_ulp_irq_restart(bp, rc);
10845 	} else if (irq_change && !rc) {
10846 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
10847 			rc = -ENOSPC;
10848 	}
10849 	if (rc) {
10850 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
10851 		return rc;
10852 	}
10853 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
10854 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
10855 		netdev_err(bp->dev, "tx ring reservation failure\n");
10856 		netdev_reset_tc(bp->dev);
10857 		bp->num_tc = 0;
10858 		if (bp->tx_nr_rings_xdp)
10859 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
10860 		else
10861 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
10862 		return -ENOMEM;
10863 	}
10864 	return 0;
10865 }
10866 
10867 static void bnxt_free_irq(struct bnxt *bp)
10868 {
10869 	struct bnxt_irq *irq;
10870 	int i;
10871 
10872 #ifdef CONFIG_RFS_ACCEL
10873 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
10874 	bp->dev->rx_cpu_rmap = NULL;
10875 #endif
10876 	if (!bp->irq_tbl || !bp->bnapi)
10877 		return;
10878 
10879 	for (i = 0; i < bp->cp_nr_rings; i++) {
10880 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10881 
10882 		irq = &bp->irq_tbl[map_idx];
10883 		if (irq->requested) {
10884 			if (irq->have_cpumask) {
10885 				irq_set_affinity_hint(irq->vector, NULL);
10886 				free_cpumask_var(irq->cpu_mask);
10887 				irq->have_cpumask = 0;
10888 			}
10889 			free_irq(irq->vector, bp->bnapi[i]);
10890 		}
10891 
10892 		irq->requested = 0;
10893 	}
10894 }
10895 
10896 static int bnxt_request_irq(struct bnxt *bp)
10897 {
10898 	int i, j, rc = 0;
10899 	unsigned long flags = 0;
10900 #ifdef CONFIG_RFS_ACCEL
10901 	struct cpu_rmap *rmap;
10902 #endif
10903 
10904 	rc = bnxt_setup_int_mode(bp);
10905 	if (rc) {
10906 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
10907 			   rc);
10908 		return rc;
10909 	}
10910 #ifdef CONFIG_RFS_ACCEL
10911 	rmap = bp->dev->rx_cpu_rmap;
10912 #endif
10913 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
10914 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10915 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
10916 
10917 #ifdef CONFIG_RFS_ACCEL
10918 		if (rmap && bp->bnapi[i]->rx_ring) {
10919 			rc = irq_cpu_rmap_add(rmap, irq->vector);
10920 			if (rc)
10921 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
10922 					    j);
10923 			j++;
10924 		}
10925 #endif
10926 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
10927 				 bp->bnapi[i]);
10928 		if (rc)
10929 			break;
10930 
10931 		netif_napi_set_irq(&bp->bnapi[i]->napi, irq->vector);
10932 		irq->requested = 1;
10933 
10934 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
10935 			int numa_node = dev_to_node(&bp->pdev->dev);
10936 
10937 			irq->have_cpumask = 1;
10938 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
10939 					irq->cpu_mask);
10940 			rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
10941 			if (rc) {
10942 				netdev_warn(bp->dev,
10943 					    "Set affinity failed, IRQ = %d\n",
10944 					    irq->vector);
10945 				break;
10946 			}
10947 		}
10948 	}
10949 	return rc;
10950 }
10951 
10952 static void bnxt_del_napi(struct bnxt *bp)
10953 {
10954 	int i;
10955 
10956 	if (!bp->bnapi)
10957 		return;
10958 
10959 	for (i = 0; i < bp->rx_nr_rings; i++)
10960 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
10961 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
10962 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
10963 
10964 	for (i = 0; i < bp->cp_nr_rings; i++) {
10965 		struct bnxt_napi *bnapi = bp->bnapi[i];
10966 
10967 		__netif_napi_del(&bnapi->napi);
10968 	}
10969 	/* We called __netif_napi_del(), we need
10970 	 * to respect an RCU grace period before freeing napi structures.
10971 	 */
10972 	synchronize_net();
10973 }
10974 
10975 static void bnxt_init_napi(struct bnxt *bp)
10976 {
10977 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
10978 	unsigned int cp_nr_rings = bp->cp_nr_rings;
10979 	struct bnxt_napi *bnapi;
10980 	int i;
10981 
10982 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10983 		poll_fn = bnxt_poll_p5;
10984 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10985 		cp_nr_rings--;
10986 	for (i = 0; i < cp_nr_rings; i++) {
10987 		bnapi = bp->bnapi[i];
10988 		netif_napi_add_config(bp->dev, &bnapi->napi, poll_fn,
10989 				      bnapi->index);
10990 	}
10991 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10992 		bnapi = bp->bnapi[cp_nr_rings];
10993 		netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
10994 	}
10995 }
10996 
10997 static void bnxt_disable_napi(struct bnxt *bp)
10998 {
10999 	int i;
11000 
11001 	if (!bp->bnapi ||
11002 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11003 		return;
11004 
11005 	for (i = 0; i < bp->cp_nr_rings; i++) {
11006 		struct bnxt_napi *bnapi = bp->bnapi[i];
11007 		struct bnxt_cp_ring_info *cpr;
11008 
11009 		cpr = &bnapi->cp_ring;
11010 		if (bnapi->tx_fault)
11011 			cpr->sw_stats->tx.tx_resets++;
11012 		if (bnapi->in_reset)
11013 			cpr->sw_stats->rx.rx_resets++;
11014 		napi_disable(&bnapi->napi);
11015 		if (bnapi->rx_ring)
11016 			cancel_work_sync(&cpr->dim.work);
11017 	}
11018 }
11019 
11020 static void bnxt_enable_napi(struct bnxt *bp)
11021 {
11022 	int i;
11023 
11024 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11025 	for (i = 0; i < bp->cp_nr_rings; i++) {
11026 		struct bnxt_napi *bnapi = bp->bnapi[i];
11027 		struct bnxt_cp_ring_info *cpr;
11028 
11029 		bnapi->tx_fault = 0;
11030 
11031 		cpr = &bnapi->cp_ring;
11032 		bnapi->in_reset = false;
11033 
11034 		if (bnapi->rx_ring) {
11035 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
11036 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
11037 		}
11038 		napi_enable(&bnapi->napi);
11039 	}
11040 }
11041 
11042 void bnxt_tx_disable(struct bnxt *bp)
11043 {
11044 	int i;
11045 	struct bnxt_tx_ring_info *txr;
11046 
11047 	if (bp->tx_ring) {
11048 		for (i = 0; i < bp->tx_nr_rings; i++) {
11049 			txr = &bp->tx_ring[i];
11050 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11051 		}
11052 	}
11053 	/* Make sure napi polls see @dev_state change */
11054 	synchronize_net();
11055 	/* Drop carrier first to prevent TX timeout */
11056 	netif_carrier_off(bp->dev);
11057 	/* Stop all TX queues */
11058 	netif_tx_disable(bp->dev);
11059 }
11060 
11061 void bnxt_tx_enable(struct bnxt *bp)
11062 {
11063 	int i;
11064 	struct bnxt_tx_ring_info *txr;
11065 
11066 	for (i = 0; i < bp->tx_nr_rings; i++) {
11067 		txr = &bp->tx_ring[i];
11068 		WRITE_ONCE(txr->dev_state, 0);
11069 	}
11070 	/* Make sure napi polls see @dev_state change */
11071 	synchronize_net();
11072 	netif_tx_wake_all_queues(bp->dev);
11073 	if (BNXT_LINK_IS_UP(bp))
11074 		netif_carrier_on(bp->dev);
11075 }
11076 
11077 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
11078 {
11079 	u8 active_fec = link_info->active_fec_sig_mode &
11080 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
11081 
11082 	switch (active_fec) {
11083 	default:
11084 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
11085 		return "None";
11086 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
11087 		return "Clause 74 BaseR";
11088 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
11089 		return "Clause 91 RS(528,514)";
11090 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
11091 		return "Clause 91 RS544_1XN";
11092 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
11093 		return "Clause 91 RS(544,514)";
11094 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
11095 		return "Clause 91 RS272_1XN";
11096 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
11097 		return "Clause 91 RS(272,257)";
11098 	}
11099 }
11100 
11101 void bnxt_report_link(struct bnxt *bp)
11102 {
11103 	if (BNXT_LINK_IS_UP(bp)) {
11104 		const char *signal = "";
11105 		const char *flow_ctrl;
11106 		const char *duplex;
11107 		u32 speed;
11108 		u16 fec;
11109 
11110 		netif_carrier_on(bp->dev);
11111 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
11112 		if (speed == SPEED_UNKNOWN) {
11113 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
11114 			return;
11115 		}
11116 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
11117 			duplex = "full";
11118 		else
11119 			duplex = "half";
11120 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
11121 			flow_ctrl = "ON - receive & transmit";
11122 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
11123 			flow_ctrl = "ON - transmit";
11124 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
11125 			flow_ctrl = "ON - receive";
11126 		else
11127 			flow_ctrl = "none";
11128 		if (bp->link_info.phy_qcfg_resp.option_flags &
11129 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
11130 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
11131 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
11132 			switch (sig_mode) {
11133 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
11134 				signal = "(NRZ) ";
11135 				break;
11136 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
11137 				signal = "(PAM4 56Gbps) ";
11138 				break;
11139 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
11140 				signal = "(PAM4 112Gbps) ";
11141 				break;
11142 			default:
11143 				break;
11144 			}
11145 		}
11146 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
11147 			    speed, signal, duplex, flow_ctrl);
11148 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
11149 			netdev_info(bp->dev, "EEE is %s\n",
11150 				    bp->eee.eee_active ? "active" :
11151 							 "not active");
11152 		fec = bp->link_info.fec_cfg;
11153 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
11154 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
11155 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
11156 				    bnxt_report_fec(&bp->link_info));
11157 	} else {
11158 		netif_carrier_off(bp->dev);
11159 		netdev_err(bp->dev, "NIC Link is Down\n");
11160 	}
11161 }
11162 
11163 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
11164 {
11165 	if (!resp->supported_speeds_auto_mode &&
11166 	    !resp->supported_speeds_force_mode &&
11167 	    !resp->supported_pam4_speeds_auto_mode &&
11168 	    !resp->supported_pam4_speeds_force_mode &&
11169 	    !resp->supported_speeds2_auto_mode &&
11170 	    !resp->supported_speeds2_force_mode)
11171 		return true;
11172 	return false;
11173 }
11174 
11175 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
11176 {
11177 	struct bnxt_link_info *link_info = &bp->link_info;
11178 	struct hwrm_port_phy_qcaps_output *resp;
11179 	struct hwrm_port_phy_qcaps_input *req;
11180 	int rc = 0;
11181 
11182 	if (bp->hwrm_spec_code < 0x10201)
11183 		return 0;
11184 
11185 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
11186 	if (rc)
11187 		return rc;
11188 
11189 	resp = hwrm_req_hold(bp, req);
11190 	rc = hwrm_req_send(bp, req);
11191 	if (rc)
11192 		goto hwrm_phy_qcaps_exit;
11193 
11194 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
11195 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
11196 		struct ethtool_keee *eee = &bp->eee;
11197 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
11198 
11199 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
11200 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
11201 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11202 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11203 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11204 	}
11205 
11206 	if (bp->hwrm_spec_code >= 0x10a01) {
11207 		if (bnxt_phy_qcaps_no_speed(resp)) {
11208 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11209 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11210 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11211 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11212 			netdev_info(bp->dev, "Ethernet link enabled\n");
11213 			/* Phy re-enabled, reprobe the speeds */
11214 			link_info->support_auto_speeds = 0;
11215 			link_info->support_pam4_auto_speeds = 0;
11216 			link_info->support_auto_speeds2 = 0;
11217 		}
11218 	}
11219 	if (resp->supported_speeds_auto_mode)
11220 		link_info->support_auto_speeds =
11221 			le16_to_cpu(resp->supported_speeds_auto_mode);
11222 	if (resp->supported_pam4_speeds_auto_mode)
11223 		link_info->support_pam4_auto_speeds =
11224 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11225 	if (resp->supported_speeds2_auto_mode)
11226 		link_info->support_auto_speeds2 =
11227 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11228 
11229 	bp->port_count = resp->port_cnt;
11230 
11231 hwrm_phy_qcaps_exit:
11232 	hwrm_req_drop(bp, req);
11233 	return rc;
11234 }
11235 
11236 static bool bnxt_support_dropped(u16 advertising, u16 supported)
11237 {
11238 	u16 diff = advertising ^ supported;
11239 
11240 	return ((supported | diff) != supported);
11241 }
11242 
11243 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
11244 {
11245 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
11246 
11247 	/* Check if any advertised speeds are no longer supported. The caller
11248 	 * holds the link_lock mutex, so we can modify link_info settings.
11249 	 */
11250 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11251 		if (bnxt_support_dropped(link_info->advertising,
11252 					 link_info->support_auto_speeds2)) {
11253 			link_info->advertising = link_info->support_auto_speeds2;
11254 			return true;
11255 		}
11256 		return false;
11257 	}
11258 	if (bnxt_support_dropped(link_info->advertising,
11259 				 link_info->support_auto_speeds)) {
11260 		link_info->advertising = link_info->support_auto_speeds;
11261 		return true;
11262 	}
11263 	if (bnxt_support_dropped(link_info->advertising_pam4,
11264 				 link_info->support_pam4_auto_speeds)) {
11265 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
11266 		return true;
11267 	}
11268 	return false;
11269 }
11270 
11271 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
11272 {
11273 	struct bnxt_link_info *link_info = &bp->link_info;
11274 	struct hwrm_port_phy_qcfg_output *resp;
11275 	struct hwrm_port_phy_qcfg_input *req;
11276 	u8 link_state = link_info->link_state;
11277 	bool support_changed;
11278 	int rc;
11279 
11280 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
11281 	if (rc)
11282 		return rc;
11283 
11284 	resp = hwrm_req_hold(bp, req);
11285 	rc = hwrm_req_send(bp, req);
11286 	if (rc) {
11287 		hwrm_req_drop(bp, req);
11288 		if (BNXT_VF(bp) && rc == -ENODEV) {
11289 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
11290 			rc = 0;
11291 		}
11292 		return rc;
11293 	}
11294 
11295 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
11296 	link_info->phy_link_status = resp->link;
11297 	link_info->duplex = resp->duplex_cfg;
11298 	if (bp->hwrm_spec_code >= 0x10800)
11299 		link_info->duplex = resp->duplex_state;
11300 	link_info->pause = resp->pause;
11301 	link_info->auto_mode = resp->auto_mode;
11302 	link_info->auto_pause_setting = resp->auto_pause;
11303 	link_info->lp_pause = resp->link_partner_adv_pause;
11304 	link_info->force_pause_setting = resp->force_pause;
11305 	link_info->duplex_setting = resp->duplex_cfg;
11306 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
11307 		link_info->link_speed = le16_to_cpu(resp->link_speed);
11308 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
11309 			link_info->active_lanes = resp->active_lanes;
11310 	} else {
11311 		link_info->link_speed = 0;
11312 		link_info->active_lanes = 0;
11313 	}
11314 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
11315 	link_info->force_pam4_link_speed =
11316 		le16_to_cpu(resp->force_pam4_link_speed);
11317 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
11318 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
11319 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
11320 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
11321 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
11322 	link_info->auto_pam4_link_speeds =
11323 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
11324 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
11325 	link_info->lp_auto_link_speeds =
11326 		le16_to_cpu(resp->link_partner_adv_speeds);
11327 	link_info->lp_auto_pam4_link_speeds =
11328 		resp->link_partner_pam4_adv_speeds;
11329 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
11330 	link_info->phy_ver[0] = resp->phy_maj;
11331 	link_info->phy_ver[1] = resp->phy_min;
11332 	link_info->phy_ver[2] = resp->phy_bld;
11333 	link_info->media_type = resp->media_type;
11334 	link_info->phy_type = resp->phy_type;
11335 	link_info->transceiver = resp->xcvr_pkg_type;
11336 	link_info->phy_addr = resp->eee_config_phy_addr &
11337 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
11338 	link_info->module_status = resp->module_status;
11339 
11340 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
11341 		struct ethtool_keee *eee = &bp->eee;
11342 		u16 fw_speeds;
11343 
11344 		eee->eee_active = 0;
11345 		if (resp->eee_config_phy_addr &
11346 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
11347 			eee->eee_active = 1;
11348 			fw_speeds = le16_to_cpu(
11349 				resp->link_partner_adv_eee_link_speed_mask);
11350 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
11351 		}
11352 
11353 		/* Pull initial EEE config */
11354 		if (!chng_link_state) {
11355 			if (resp->eee_config_phy_addr &
11356 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
11357 				eee->eee_enabled = 1;
11358 
11359 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
11360 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
11361 
11362 			if (resp->eee_config_phy_addr &
11363 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
11364 				__le32 tmr;
11365 
11366 				eee->tx_lpi_enabled = 1;
11367 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
11368 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
11369 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
11370 			}
11371 		}
11372 	}
11373 
11374 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
11375 	if (bp->hwrm_spec_code >= 0x10504) {
11376 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
11377 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
11378 	}
11379 	/* TODO: need to add more logic to report VF link */
11380 	if (chng_link_state) {
11381 		if (link_info->phy_link_status == BNXT_LINK_LINK)
11382 			link_info->link_state = BNXT_LINK_STATE_UP;
11383 		else
11384 			link_info->link_state = BNXT_LINK_STATE_DOWN;
11385 		if (link_state != link_info->link_state)
11386 			bnxt_report_link(bp);
11387 	} else {
11388 		/* always link down if not require to update link state */
11389 		link_info->link_state = BNXT_LINK_STATE_DOWN;
11390 	}
11391 	hwrm_req_drop(bp, req);
11392 
11393 	if (!BNXT_PHY_CFG_ABLE(bp))
11394 		return 0;
11395 
11396 	support_changed = bnxt_support_speed_dropped(link_info);
11397 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
11398 		bnxt_hwrm_set_link_setting(bp, true, false);
11399 	return 0;
11400 }
11401 
11402 static void bnxt_get_port_module_status(struct bnxt *bp)
11403 {
11404 	struct bnxt_link_info *link_info = &bp->link_info;
11405 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
11406 	u8 module_status;
11407 
11408 	if (bnxt_update_link(bp, true))
11409 		return;
11410 
11411 	module_status = link_info->module_status;
11412 	switch (module_status) {
11413 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
11414 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
11415 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
11416 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
11417 			    bp->pf.port_id);
11418 		if (bp->hwrm_spec_code >= 0x10201) {
11419 			netdev_warn(bp->dev, "Module part number %s\n",
11420 				    resp->phy_vendor_partnumber);
11421 		}
11422 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
11423 			netdev_warn(bp->dev, "TX is disabled\n");
11424 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
11425 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
11426 	}
11427 }
11428 
11429 static void
11430 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11431 {
11432 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
11433 		if (bp->hwrm_spec_code >= 0x10201)
11434 			req->auto_pause =
11435 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
11436 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11437 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
11438 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11439 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
11440 		req->enables |=
11441 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11442 	} else {
11443 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11444 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
11445 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11446 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
11447 		req->enables |=
11448 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
11449 		if (bp->hwrm_spec_code >= 0x10201) {
11450 			req->auto_pause = req->force_pause;
11451 			req->enables |= cpu_to_le32(
11452 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11453 		}
11454 	}
11455 }
11456 
11457 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11458 {
11459 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
11460 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
11461 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11462 			req->enables |=
11463 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
11464 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
11465 		} else if (bp->link_info.advertising) {
11466 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
11467 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
11468 		}
11469 		if (bp->link_info.advertising_pam4) {
11470 			req->enables |=
11471 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
11472 			req->auto_link_pam4_speed_mask =
11473 				cpu_to_le16(bp->link_info.advertising_pam4);
11474 		}
11475 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
11476 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
11477 	} else {
11478 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
11479 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11480 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
11481 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
11482 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
11483 				   (u32)bp->link_info.req_link_speed);
11484 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
11485 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11486 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
11487 		} else {
11488 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11489 		}
11490 	}
11491 
11492 	/* tell chimp that the setting takes effect immediately */
11493 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
11494 }
11495 
11496 int bnxt_hwrm_set_pause(struct bnxt *bp)
11497 {
11498 	struct hwrm_port_phy_cfg_input *req;
11499 	int rc;
11500 
11501 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11502 	if (rc)
11503 		return rc;
11504 
11505 	bnxt_hwrm_set_pause_common(bp, req);
11506 
11507 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
11508 	    bp->link_info.force_link_chng)
11509 		bnxt_hwrm_set_link_common(bp, req);
11510 
11511 	rc = hwrm_req_send(bp, req);
11512 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
11513 		/* since changing of pause setting doesn't trigger any link
11514 		 * change event, the driver needs to update the current pause
11515 		 * result upon successfully return of the phy_cfg command
11516 		 */
11517 		bp->link_info.pause =
11518 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
11519 		bp->link_info.auto_pause_setting = 0;
11520 		if (!bp->link_info.force_link_chng)
11521 			bnxt_report_link(bp);
11522 	}
11523 	bp->link_info.force_link_chng = false;
11524 	return rc;
11525 }
11526 
11527 static void bnxt_hwrm_set_eee(struct bnxt *bp,
11528 			      struct hwrm_port_phy_cfg_input *req)
11529 {
11530 	struct ethtool_keee *eee = &bp->eee;
11531 
11532 	if (eee->eee_enabled) {
11533 		u16 eee_speeds;
11534 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
11535 
11536 		if (eee->tx_lpi_enabled)
11537 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
11538 		else
11539 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
11540 
11541 		req->flags |= cpu_to_le32(flags);
11542 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
11543 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
11544 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
11545 	} else {
11546 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
11547 	}
11548 }
11549 
11550 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
11551 {
11552 	struct hwrm_port_phy_cfg_input *req;
11553 	int rc;
11554 
11555 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11556 	if (rc)
11557 		return rc;
11558 
11559 	if (set_pause)
11560 		bnxt_hwrm_set_pause_common(bp, req);
11561 
11562 	bnxt_hwrm_set_link_common(bp, req);
11563 
11564 	if (set_eee)
11565 		bnxt_hwrm_set_eee(bp, req);
11566 	return hwrm_req_send(bp, req);
11567 }
11568 
11569 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
11570 {
11571 	struct hwrm_port_phy_cfg_input *req;
11572 	int rc;
11573 
11574 	if (!BNXT_SINGLE_PF(bp))
11575 		return 0;
11576 
11577 	if (pci_num_vf(bp->pdev) &&
11578 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
11579 		return 0;
11580 
11581 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11582 	if (rc)
11583 		return rc;
11584 
11585 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
11586 	rc = hwrm_req_send(bp, req);
11587 	if (!rc) {
11588 		mutex_lock(&bp->link_lock);
11589 		/* Device is not obliged link down in certain scenarios, even
11590 		 * when forced. Setting the state unknown is consistent with
11591 		 * driver startup and will force link state to be reported
11592 		 * during subsequent open based on PORT_PHY_QCFG.
11593 		 */
11594 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
11595 		mutex_unlock(&bp->link_lock);
11596 	}
11597 	return rc;
11598 }
11599 
11600 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
11601 {
11602 #ifdef CONFIG_TEE_BNXT_FW
11603 	int rc = tee_bnxt_fw_load();
11604 
11605 	if (rc)
11606 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
11607 
11608 	return rc;
11609 #else
11610 	netdev_err(bp->dev, "OP-TEE not supported\n");
11611 	return -ENODEV;
11612 #endif
11613 }
11614 
11615 static int bnxt_try_recover_fw(struct bnxt *bp)
11616 {
11617 	if (bp->fw_health && bp->fw_health->status_reliable) {
11618 		int retry = 0, rc;
11619 		u32 sts;
11620 
11621 		do {
11622 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
11623 			rc = bnxt_hwrm_poll(bp);
11624 			if (!BNXT_FW_IS_BOOTING(sts) &&
11625 			    !BNXT_FW_IS_RECOVERING(sts))
11626 				break;
11627 			retry++;
11628 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
11629 
11630 		if (!BNXT_FW_IS_HEALTHY(sts)) {
11631 			netdev_err(bp->dev,
11632 				   "Firmware not responding, status: 0x%x\n",
11633 				   sts);
11634 			rc = -ENODEV;
11635 		}
11636 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
11637 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
11638 			return bnxt_fw_reset_via_optee(bp);
11639 		}
11640 		return rc;
11641 	}
11642 
11643 	return -ENODEV;
11644 }
11645 
11646 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
11647 {
11648 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11649 
11650 	if (!BNXT_NEW_RM(bp))
11651 		return; /* no resource reservations required */
11652 
11653 	hw_resc->resv_cp_rings = 0;
11654 	hw_resc->resv_stat_ctxs = 0;
11655 	hw_resc->resv_irqs = 0;
11656 	hw_resc->resv_tx_rings = 0;
11657 	hw_resc->resv_rx_rings = 0;
11658 	hw_resc->resv_hw_ring_grps = 0;
11659 	hw_resc->resv_vnics = 0;
11660 	hw_resc->resv_rsscos_ctxs = 0;
11661 	if (!fw_reset) {
11662 		bp->tx_nr_rings = 0;
11663 		bp->rx_nr_rings = 0;
11664 	}
11665 }
11666 
11667 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
11668 {
11669 	int rc;
11670 
11671 	if (!BNXT_NEW_RM(bp))
11672 		return 0; /* no resource reservations required */
11673 
11674 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
11675 	if (rc)
11676 		netdev_err(bp->dev, "resc_qcaps failed\n");
11677 
11678 	bnxt_clear_reservations(bp, fw_reset);
11679 
11680 	return rc;
11681 }
11682 
11683 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
11684 {
11685 	struct hwrm_func_drv_if_change_output *resp;
11686 	struct hwrm_func_drv_if_change_input *req;
11687 	bool fw_reset = !bp->irq_tbl;
11688 	bool resc_reinit = false;
11689 	int rc, retry = 0;
11690 	u32 flags = 0;
11691 
11692 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
11693 		return 0;
11694 
11695 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
11696 	if (rc)
11697 		return rc;
11698 
11699 	if (up)
11700 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
11701 	resp = hwrm_req_hold(bp, req);
11702 
11703 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
11704 	while (retry < BNXT_FW_IF_RETRY) {
11705 		rc = hwrm_req_send(bp, req);
11706 		if (rc != -EAGAIN)
11707 			break;
11708 
11709 		msleep(50);
11710 		retry++;
11711 	}
11712 
11713 	if (rc == -EAGAIN) {
11714 		hwrm_req_drop(bp, req);
11715 		return rc;
11716 	} else if (!rc) {
11717 		flags = le32_to_cpu(resp->flags);
11718 	} else if (up) {
11719 		rc = bnxt_try_recover_fw(bp);
11720 		fw_reset = true;
11721 	}
11722 	hwrm_req_drop(bp, req);
11723 	if (rc)
11724 		return rc;
11725 
11726 	if (!up) {
11727 		bnxt_inv_fw_health_reg(bp);
11728 		return 0;
11729 	}
11730 
11731 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
11732 		resc_reinit = true;
11733 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
11734 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
11735 		fw_reset = true;
11736 	else
11737 		bnxt_remap_fw_health_regs(bp);
11738 
11739 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
11740 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
11741 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
11742 		return -ENODEV;
11743 	}
11744 	if (resc_reinit || fw_reset) {
11745 		if (fw_reset) {
11746 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11747 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11748 				bnxt_ulp_irq_stop(bp);
11749 			bnxt_free_ctx_mem(bp);
11750 			bnxt_dcb_free(bp);
11751 			rc = bnxt_fw_init_one(bp);
11752 			if (rc) {
11753 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11754 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
11755 				return rc;
11756 			}
11757 			bnxt_clear_int_mode(bp);
11758 			rc = bnxt_init_int_mode(bp);
11759 			if (rc) {
11760 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11761 				netdev_err(bp->dev, "init int mode failed\n");
11762 				return rc;
11763 			}
11764 		}
11765 		rc = bnxt_cancel_reservations(bp, fw_reset);
11766 	}
11767 	return rc;
11768 }
11769 
11770 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
11771 {
11772 	struct hwrm_port_led_qcaps_output *resp;
11773 	struct hwrm_port_led_qcaps_input *req;
11774 	struct bnxt_pf_info *pf = &bp->pf;
11775 	int rc;
11776 
11777 	bp->num_leds = 0;
11778 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
11779 		return 0;
11780 
11781 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
11782 	if (rc)
11783 		return rc;
11784 
11785 	req->port_id = cpu_to_le16(pf->port_id);
11786 	resp = hwrm_req_hold(bp, req);
11787 	rc = hwrm_req_send(bp, req);
11788 	if (rc) {
11789 		hwrm_req_drop(bp, req);
11790 		return rc;
11791 	}
11792 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
11793 		int i;
11794 
11795 		bp->num_leds = resp->num_leds;
11796 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
11797 						 bp->num_leds);
11798 		for (i = 0; i < bp->num_leds; i++) {
11799 			struct bnxt_led_info *led = &bp->leds[i];
11800 			__le16 caps = led->led_state_caps;
11801 
11802 			if (!led->led_group_id ||
11803 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
11804 				bp->num_leds = 0;
11805 				break;
11806 			}
11807 		}
11808 	}
11809 	hwrm_req_drop(bp, req);
11810 	return 0;
11811 }
11812 
11813 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
11814 {
11815 	struct hwrm_wol_filter_alloc_output *resp;
11816 	struct hwrm_wol_filter_alloc_input *req;
11817 	int rc;
11818 
11819 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
11820 	if (rc)
11821 		return rc;
11822 
11823 	req->port_id = cpu_to_le16(bp->pf.port_id);
11824 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
11825 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
11826 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
11827 
11828 	resp = hwrm_req_hold(bp, req);
11829 	rc = hwrm_req_send(bp, req);
11830 	if (!rc)
11831 		bp->wol_filter_id = resp->wol_filter_id;
11832 	hwrm_req_drop(bp, req);
11833 	return rc;
11834 }
11835 
11836 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
11837 {
11838 	struct hwrm_wol_filter_free_input *req;
11839 	int rc;
11840 
11841 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
11842 	if (rc)
11843 		return rc;
11844 
11845 	req->port_id = cpu_to_le16(bp->pf.port_id);
11846 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
11847 	req->wol_filter_id = bp->wol_filter_id;
11848 
11849 	return hwrm_req_send(bp, req);
11850 }
11851 
11852 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
11853 {
11854 	struct hwrm_wol_filter_qcfg_output *resp;
11855 	struct hwrm_wol_filter_qcfg_input *req;
11856 	u16 next_handle = 0;
11857 	int rc;
11858 
11859 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
11860 	if (rc)
11861 		return rc;
11862 
11863 	req->port_id = cpu_to_le16(bp->pf.port_id);
11864 	req->handle = cpu_to_le16(handle);
11865 	resp = hwrm_req_hold(bp, req);
11866 	rc = hwrm_req_send(bp, req);
11867 	if (!rc) {
11868 		next_handle = le16_to_cpu(resp->next_handle);
11869 		if (next_handle != 0) {
11870 			if (resp->wol_type ==
11871 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
11872 				bp->wol = 1;
11873 				bp->wol_filter_id = resp->wol_filter_id;
11874 			}
11875 		}
11876 	}
11877 	hwrm_req_drop(bp, req);
11878 	return next_handle;
11879 }
11880 
11881 static void bnxt_get_wol_settings(struct bnxt *bp)
11882 {
11883 	u16 handle = 0;
11884 
11885 	bp->wol = 0;
11886 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
11887 		return;
11888 
11889 	do {
11890 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
11891 	} while (handle && handle != 0xffff);
11892 }
11893 
11894 static bool bnxt_eee_config_ok(struct bnxt *bp)
11895 {
11896 	struct ethtool_keee *eee = &bp->eee;
11897 	struct bnxt_link_info *link_info = &bp->link_info;
11898 
11899 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
11900 		return true;
11901 
11902 	if (eee->eee_enabled) {
11903 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
11904 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
11905 
11906 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
11907 
11908 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
11909 			eee->eee_enabled = 0;
11910 			return false;
11911 		}
11912 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
11913 			linkmode_and(eee->advertised, advertising,
11914 				     eee->supported);
11915 			return false;
11916 		}
11917 	}
11918 	return true;
11919 }
11920 
11921 static int bnxt_update_phy_setting(struct bnxt *bp)
11922 {
11923 	int rc;
11924 	bool update_link = false;
11925 	bool update_pause = false;
11926 	bool update_eee = false;
11927 	struct bnxt_link_info *link_info = &bp->link_info;
11928 
11929 	rc = bnxt_update_link(bp, true);
11930 	if (rc) {
11931 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
11932 			   rc);
11933 		return rc;
11934 	}
11935 	if (!BNXT_SINGLE_PF(bp))
11936 		return 0;
11937 
11938 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
11939 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
11940 	    link_info->req_flow_ctrl)
11941 		update_pause = true;
11942 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
11943 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
11944 		update_pause = true;
11945 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
11946 		if (BNXT_AUTO_MODE(link_info->auto_mode))
11947 			update_link = true;
11948 		if (bnxt_force_speed_updated(link_info))
11949 			update_link = true;
11950 		if (link_info->req_duplex != link_info->duplex_setting)
11951 			update_link = true;
11952 	} else {
11953 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
11954 			update_link = true;
11955 		if (bnxt_auto_speed_updated(link_info))
11956 			update_link = true;
11957 	}
11958 
11959 	/* The last close may have shutdown the link, so need to call
11960 	 * PHY_CFG to bring it back up.
11961 	 */
11962 	if (!BNXT_LINK_IS_UP(bp))
11963 		update_link = true;
11964 
11965 	if (!bnxt_eee_config_ok(bp))
11966 		update_eee = true;
11967 
11968 	if (update_link)
11969 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
11970 	else if (update_pause)
11971 		rc = bnxt_hwrm_set_pause(bp);
11972 	if (rc) {
11973 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
11974 			   rc);
11975 		return rc;
11976 	}
11977 
11978 	return rc;
11979 }
11980 
11981 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
11982 
11983 static int bnxt_reinit_after_abort(struct bnxt *bp)
11984 {
11985 	int rc;
11986 
11987 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11988 		return -EBUSY;
11989 
11990 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
11991 		return -ENODEV;
11992 
11993 	rc = bnxt_fw_init_one(bp);
11994 	if (!rc) {
11995 		bnxt_clear_int_mode(bp);
11996 		rc = bnxt_init_int_mode(bp);
11997 		if (!rc) {
11998 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
11999 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12000 		}
12001 	}
12002 	return rc;
12003 }
12004 
12005 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
12006 {
12007 	struct bnxt_ntuple_filter *ntp_fltr;
12008 	struct bnxt_l2_filter *l2_fltr;
12009 
12010 	if (list_empty(&fltr->list))
12011 		return;
12012 
12013 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
12014 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
12015 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
12016 		atomic_inc(&l2_fltr->refcnt);
12017 		ntp_fltr->l2_fltr = l2_fltr;
12018 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
12019 			bnxt_del_ntp_filter(bp, ntp_fltr);
12020 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
12021 				   fltr->sw_id);
12022 		}
12023 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
12024 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
12025 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
12026 			bnxt_del_l2_filter(bp, l2_fltr);
12027 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
12028 				   fltr->sw_id);
12029 		}
12030 	}
12031 }
12032 
12033 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
12034 {
12035 	struct bnxt_filter_base *usr_fltr, *tmp;
12036 
12037 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
12038 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
12039 }
12040 
12041 static int bnxt_set_xps_mapping(struct bnxt *bp)
12042 {
12043 	int numa_node = dev_to_node(&bp->pdev->dev);
12044 	unsigned int q_idx, map_idx, cpu, i;
12045 	const struct cpumask *cpu_mask_ptr;
12046 	int nr_cpus = num_online_cpus();
12047 	cpumask_t *q_map;
12048 	int rc = 0;
12049 
12050 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
12051 	if (!q_map)
12052 		return -ENOMEM;
12053 
12054 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
12055 	 * Each TC has the same number of TX queues. The nth TX queue for each
12056 	 * TC will have the same CPU mask.
12057 	 */
12058 	for (i = 0; i < nr_cpus; i++) {
12059 		map_idx = i % bp->tx_nr_rings_per_tc;
12060 		cpu = cpumask_local_spread(i, numa_node);
12061 		cpu_mask_ptr = get_cpu_mask(cpu);
12062 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
12063 	}
12064 
12065 	/* Register CPU mask for each TX queue except the ones marked for XDP */
12066 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
12067 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
12068 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
12069 		if (rc) {
12070 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
12071 				    q_idx);
12072 			break;
12073 		}
12074 	}
12075 
12076 	kfree(q_map);
12077 
12078 	return rc;
12079 }
12080 
12081 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12082 {
12083 	int rc = 0;
12084 
12085 	netif_carrier_off(bp->dev);
12086 	if (irq_re_init) {
12087 		/* Reserve rings now if none were reserved at driver probe. */
12088 		rc = bnxt_init_dflt_ring_mode(bp);
12089 		if (rc) {
12090 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
12091 			return rc;
12092 		}
12093 	}
12094 	rc = bnxt_reserve_rings(bp, irq_re_init);
12095 	if (rc)
12096 		return rc;
12097 
12098 	rc = bnxt_alloc_mem(bp, irq_re_init);
12099 	if (rc) {
12100 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12101 		goto open_err_free_mem;
12102 	}
12103 
12104 	if (irq_re_init) {
12105 		bnxt_init_napi(bp);
12106 		rc = bnxt_request_irq(bp);
12107 		if (rc) {
12108 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
12109 			goto open_err_irq;
12110 		}
12111 	}
12112 
12113 	rc = bnxt_init_nic(bp, irq_re_init);
12114 	if (rc) {
12115 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12116 		goto open_err_irq;
12117 	}
12118 
12119 	bnxt_enable_napi(bp);
12120 	bnxt_debug_dev_init(bp);
12121 
12122 	if (link_re_init) {
12123 		mutex_lock(&bp->link_lock);
12124 		rc = bnxt_update_phy_setting(bp);
12125 		mutex_unlock(&bp->link_lock);
12126 		if (rc) {
12127 			netdev_warn(bp->dev, "failed to update phy settings\n");
12128 			if (BNXT_SINGLE_PF(bp)) {
12129 				bp->link_info.phy_retry = true;
12130 				bp->link_info.phy_retry_expires =
12131 					jiffies + 5 * HZ;
12132 			}
12133 		}
12134 	}
12135 
12136 	if (irq_re_init) {
12137 		udp_tunnel_nic_reset_ntf(bp->dev);
12138 		rc = bnxt_set_xps_mapping(bp);
12139 		if (rc)
12140 			netdev_warn(bp->dev, "failed to set xps mapping\n");
12141 	}
12142 
12143 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
12144 		if (!static_key_enabled(&bnxt_xdp_locking_key))
12145 			static_branch_enable(&bnxt_xdp_locking_key);
12146 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
12147 		static_branch_disable(&bnxt_xdp_locking_key);
12148 	}
12149 	set_bit(BNXT_STATE_OPEN, &bp->state);
12150 	bnxt_enable_int(bp);
12151 	/* Enable TX queues */
12152 	bnxt_tx_enable(bp);
12153 	mod_timer(&bp->timer, jiffies + bp->current_interval);
12154 	/* Poll link status and check for SFP+ module status */
12155 	mutex_lock(&bp->link_lock);
12156 	bnxt_get_port_module_status(bp);
12157 	mutex_unlock(&bp->link_lock);
12158 
12159 	/* VF-reps may need to be re-opened after the PF is re-opened */
12160 	if (BNXT_PF(bp))
12161 		bnxt_vf_reps_open(bp);
12162 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
12163 		WRITE_ONCE(bp->ptp_cfg->tx_avail, BNXT_MAX_TX_TS);
12164 	bnxt_ptp_init_rtc(bp, true);
12165 	bnxt_ptp_cfg_tstamp_filters(bp);
12166 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12167 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
12168 	bnxt_cfg_usr_fltrs(bp);
12169 	return 0;
12170 
12171 open_err_irq:
12172 	bnxt_del_napi(bp);
12173 
12174 open_err_free_mem:
12175 	bnxt_free_skbs(bp);
12176 	bnxt_free_irq(bp);
12177 	bnxt_free_mem(bp, true);
12178 	return rc;
12179 }
12180 
12181 /* rtnl_lock held */
12182 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12183 {
12184 	int rc = 0;
12185 
12186 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12187 		rc = -EIO;
12188 	if (!rc)
12189 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12190 	if (rc) {
12191 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12192 		dev_close(bp->dev);
12193 	}
12194 	return rc;
12195 }
12196 
12197 /* rtnl_lock held, open the NIC half way by allocating all resources, but
12198  * NAPI, IRQ, and TX are not enabled.  This is mainly used for offline
12199  * self tests.
12200  */
12201 int bnxt_half_open_nic(struct bnxt *bp)
12202 {
12203 	int rc = 0;
12204 
12205 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12206 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
12207 		rc = -ENODEV;
12208 		goto half_open_err;
12209 	}
12210 
12211 	rc = bnxt_alloc_mem(bp, true);
12212 	if (rc) {
12213 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12214 		goto half_open_err;
12215 	}
12216 	bnxt_init_napi(bp);
12217 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12218 	rc = bnxt_init_nic(bp, true);
12219 	if (rc) {
12220 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12221 		bnxt_del_napi(bp);
12222 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12223 		goto half_open_err;
12224 	}
12225 	return 0;
12226 
12227 half_open_err:
12228 	bnxt_free_skbs(bp);
12229 	bnxt_free_mem(bp, true);
12230 	dev_close(bp->dev);
12231 	return rc;
12232 }
12233 
12234 /* rtnl_lock held, this call can only be made after a previous successful
12235  * call to bnxt_half_open_nic().
12236  */
12237 void bnxt_half_close_nic(struct bnxt *bp)
12238 {
12239 	bnxt_hwrm_resource_free(bp, false, true);
12240 	bnxt_del_napi(bp);
12241 	bnxt_free_skbs(bp);
12242 	bnxt_free_mem(bp, true);
12243 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12244 }
12245 
12246 void bnxt_reenable_sriov(struct bnxt *bp)
12247 {
12248 	if (BNXT_PF(bp)) {
12249 		struct bnxt_pf_info *pf = &bp->pf;
12250 		int n = pf->active_vfs;
12251 
12252 		if (n)
12253 			bnxt_cfg_hw_sriov(bp, &n, true);
12254 	}
12255 }
12256 
12257 static int bnxt_open(struct net_device *dev)
12258 {
12259 	struct bnxt *bp = netdev_priv(dev);
12260 	int rc;
12261 
12262 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12263 		rc = bnxt_reinit_after_abort(bp);
12264 		if (rc) {
12265 			if (rc == -EBUSY)
12266 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
12267 			else
12268 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
12269 			return -ENODEV;
12270 		}
12271 	}
12272 
12273 	rc = bnxt_hwrm_if_change(bp, true);
12274 	if (rc)
12275 		return rc;
12276 
12277 	rc = __bnxt_open_nic(bp, true, true);
12278 	if (rc) {
12279 		bnxt_hwrm_if_change(bp, false);
12280 	} else {
12281 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
12282 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12283 				bnxt_queue_sp_work(bp,
12284 						   BNXT_RESTART_ULP_SP_EVENT);
12285 		}
12286 	}
12287 
12288 	return rc;
12289 }
12290 
12291 static bool bnxt_drv_busy(struct bnxt *bp)
12292 {
12293 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
12294 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
12295 }
12296 
12297 static void bnxt_get_ring_stats(struct bnxt *bp,
12298 				struct rtnl_link_stats64 *stats);
12299 
12300 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
12301 			     bool link_re_init)
12302 {
12303 	/* Close the VF-reps before closing PF */
12304 	if (BNXT_PF(bp))
12305 		bnxt_vf_reps_close(bp);
12306 
12307 	/* Change device state to avoid TX queue wake up's */
12308 	bnxt_tx_disable(bp);
12309 
12310 	clear_bit(BNXT_STATE_OPEN, &bp->state);
12311 	smp_mb__after_atomic();
12312 	while (bnxt_drv_busy(bp))
12313 		msleep(20);
12314 
12315 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12316 		bnxt_clear_rss_ctxs(bp);
12317 	/* Flush rings and disable interrupts */
12318 	bnxt_shutdown_nic(bp, irq_re_init);
12319 
12320 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
12321 
12322 	bnxt_debug_dev_exit(bp);
12323 	bnxt_disable_napi(bp);
12324 	del_timer_sync(&bp->timer);
12325 	bnxt_free_skbs(bp);
12326 
12327 	/* Save ring stats before shutdown */
12328 	if (bp->bnapi && irq_re_init) {
12329 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
12330 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
12331 	}
12332 	if (irq_re_init) {
12333 		bnxt_free_irq(bp);
12334 		bnxt_del_napi(bp);
12335 	}
12336 	bnxt_free_mem(bp, irq_re_init);
12337 }
12338 
12339 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12340 {
12341 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12342 		/* If we get here, it means firmware reset is in progress
12343 		 * while we are trying to close.  We can safely proceed with
12344 		 * the close because we are holding rtnl_lock().  Some firmware
12345 		 * messages may fail as we proceed to close.  We set the
12346 		 * ABORT_ERR flag here so that the FW reset thread will later
12347 		 * abort when it gets the rtnl_lock() and sees the flag.
12348 		 */
12349 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
12350 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12351 	}
12352 
12353 #ifdef CONFIG_BNXT_SRIOV
12354 	if (bp->sriov_cfg) {
12355 		int rc;
12356 
12357 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
12358 						      !bp->sriov_cfg,
12359 						      BNXT_SRIOV_CFG_WAIT_TMO);
12360 		if (!rc)
12361 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
12362 		else if (rc < 0)
12363 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
12364 	}
12365 #endif
12366 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
12367 }
12368 
12369 static int bnxt_close(struct net_device *dev)
12370 {
12371 	struct bnxt *bp = netdev_priv(dev);
12372 
12373 	bnxt_close_nic(bp, true, true);
12374 	bnxt_hwrm_shutdown_link(bp);
12375 	bnxt_hwrm_if_change(bp, false);
12376 	return 0;
12377 }
12378 
12379 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
12380 				   u16 *val)
12381 {
12382 	struct hwrm_port_phy_mdio_read_output *resp;
12383 	struct hwrm_port_phy_mdio_read_input *req;
12384 	int rc;
12385 
12386 	if (bp->hwrm_spec_code < 0x10a00)
12387 		return -EOPNOTSUPP;
12388 
12389 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
12390 	if (rc)
12391 		return rc;
12392 
12393 	req->port_id = cpu_to_le16(bp->pf.port_id);
12394 	req->phy_addr = phy_addr;
12395 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12396 	if (mdio_phy_id_is_c45(phy_addr)) {
12397 		req->cl45_mdio = 1;
12398 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12399 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12400 		req->reg_addr = cpu_to_le16(reg);
12401 	}
12402 
12403 	resp = hwrm_req_hold(bp, req);
12404 	rc = hwrm_req_send(bp, req);
12405 	if (!rc)
12406 		*val = le16_to_cpu(resp->reg_data);
12407 	hwrm_req_drop(bp, req);
12408 	return rc;
12409 }
12410 
12411 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
12412 				    u16 val)
12413 {
12414 	struct hwrm_port_phy_mdio_write_input *req;
12415 	int rc;
12416 
12417 	if (bp->hwrm_spec_code < 0x10a00)
12418 		return -EOPNOTSUPP;
12419 
12420 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
12421 	if (rc)
12422 		return rc;
12423 
12424 	req->port_id = cpu_to_le16(bp->pf.port_id);
12425 	req->phy_addr = phy_addr;
12426 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12427 	if (mdio_phy_id_is_c45(phy_addr)) {
12428 		req->cl45_mdio = 1;
12429 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12430 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12431 		req->reg_addr = cpu_to_le16(reg);
12432 	}
12433 	req->reg_data = cpu_to_le16(val);
12434 
12435 	return hwrm_req_send(bp, req);
12436 }
12437 
12438 /* rtnl_lock held */
12439 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12440 {
12441 	struct mii_ioctl_data *mdio = if_mii(ifr);
12442 	struct bnxt *bp = netdev_priv(dev);
12443 	int rc;
12444 
12445 	switch (cmd) {
12446 	case SIOCGMIIPHY:
12447 		mdio->phy_id = bp->link_info.phy_addr;
12448 
12449 		fallthrough;
12450 	case SIOCGMIIREG: {
12451 		u16 mii_regval = 0;
12452 
12453 		if (!netif_running(dev))
12454 			return -EAGAIN;
12455 
12456 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
12457 					     &mii_regval);
12458 		mdio->val_out = mii_regval;
12459 		return rc;
12460 	}
12461 
12462 	case SIOCSMIIREG:
12463 		if (!netif_running(dev))
12464 			return -EAGAIN;
12465 
12466 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
12467 						mdio->val_in);
12468 
12469 	case SIOCSHWTSTAMP:
12470 		return bnxt_hwtstamp_set(dev, ifr);
12471 
12472 	case SIOCGHWTSTAMP:
12473 		return bnxt_hwtstamp_get(dev, ifr);
12474 
12475 	default:
12476 		/* do nothing */
12477 		break;
12478 	}
12479 	return -EOPNOTSUPP;
12480 }
12481 
12482 static void bnxt_get_ring_stats(struct bnxt *bp,
12483 				struct rtnl_link_stats64 *stats)
12484 {
12485 	int i;
12486 
12487 	for (i = 0; i < bp->cp_nr_rings; i++) {
12488 		struct bnxt_napi *bnapi = bp->bnapi[i];
12489 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
12490 		u64 *sw = cpr->stats.sw_stats;
12491 
12492 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
12493 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12494 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
12495 
12496 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
12497 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
12498 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
12499 
12500 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
12501 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
12502 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
12503 
12504 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
12505 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
12506 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
12507 
12508 		stats->rx_missed_errors +=
12509 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
12510 
12511 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12512 
12513 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
12514 
12515 		stats->rx_dropped +=
12516 			cpr->sw_stats->rx.rx_netpoll_discards +
12517 			cpr->sw_stats->rx.rx_oom_discards;
12518 	}
12519 }
12520 
12521 static void bnxt_add_prev_stats(struct bnxt *bp,
12522 				struct rtnl_link_stats64 *stats)
12523 {
12524 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
12525 
12526 	stats->rx_packets += prev_stats->rx_packets;
12527 	stats->tx_packets += prev_stats->tx_packets;
12528 	stats->rx_bytes += prev_stats->rx_bytes;
12529 	stats->tx_bytes += prev_stats->tx_bytes;
12530 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
12531 	stats->multicast += prev_stats->multicast;
12532 	stats->rx_dropped += prev_stats->rx_dropped;
12533 	stats->tx_dropped += prev_stats->tx_dropped;
12534 }
12535 
12536 static void
12537 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
12538 {
12539 	struct bnxt *bp = netdev_priv(dev);
12540 
12541 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
12542 	/* Make sure bnxt_close_nic() sees that we are reading stats before
12543 	 * we check the BNXT_STATE_OPEN flag.
12544 	 */
12545 	smp_mb__after_atomic();
12546 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12547 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12548 		*stats = bp->net_stats_prev;
12549 		return;
12550 	}
12551 
12552 	bnxt_get_ring_stats(bp, stats);
12553 	bnxt_add_prev_stats(bp, stats);
12554 
12555 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
12556 		u64 *rx = bp->port_stats.sw_stats;
12557 		u64 *tx = bp->port_stats.sw_stats +
12558 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
12559 
12560 		stats->rx_crc_errors =
12561 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
12562 		stats->rx_frame_errors =
12563 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
12564 		stats->rx_length_errors =
12565 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
12566 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
12567 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
12568 		stats->rx_errors =
12569 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
12570 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
12571 		stats->collisions =
12572 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
12573 		stats->tx_fifo_errors =
12574 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
12575 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
12576 	}
12577 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12578 }
12579 
12580 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
12581 					struct bnxt_total_ring_err_stats *stats,
12582 					struct bnxt_cp_ring_info *cpr)
12583 {
12584 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
12585 	u64 *hw_stats = cpr->stats.sw_stats;
12586 
12587 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
12588 	stats->rx_total_resets += sw_stats->rx.rx_resets;
12589 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
12590 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
12591 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
12592 	stats->rx_total_ring_discards +=
12593 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
12594 	stats->tx_total_resets += sw_stats->tx.tx_resets;
12595 	stats->tx_total_ring_discards +=
12596 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
12597 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
12598 }
12599 
12600 void bnxt_get_ring_err_stats(struct bnxt *bp,
12601 			     struct bnxt_total_ring_err_stats *stats)
12602 {
12603 	int i;
12604 
12605 	for (i = 0; i < bp->cp_nr_rings; i++)
12606 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
12607 }
12608 
12609 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
12610 {
12611 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12612 	struct net_device *dev = bp->dev;
12613 	struct netdev_hw_addr *ha;
12614 	u8 *haddr;
12615 	int mc_count = 0;
12616 	bool update = false;
12617 	int off = 0;
12618 
12619 	netdev_for_each_mc_addr(ha, dev) {
12620 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
12621 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12622 			vnic->mc_list_count = 0;
12623 			return false;
12624 		}
12625 		haddr = ha->addr;
12626 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
12627 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
12628 			update = true;
12629 		}
12630 		off += ETH_ALEN;
12631 		mc_count++;
12632 	}
12633 	if (mc_count)
12634 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
12635 
12636 	if (mc_count != vnic->mc_list_count) {
12637 		vnic->mc_list_count = mc_count;
12638 		update = true;
12639 	}
12640 	return update;
12641 }
12642 
12643 static bool bnxt_uc_list_updated(struct bnxt *bp)
12644 {
12645 	struct net_device *dev = bp->dev;
12646 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12647 	struct netdev_hw_addr *ha;
12648 	int off = 0;
12649 
12650 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
12651 		return true;
12652 
12653 	netdev_for_each_uc_addr(ha, dev) {
12654 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
12655 			return true;
12656 
12657 		off += ETH_ALEN;
12658 	}
12659 	return false;
12660 }
12661 
12662 static void bnxt_set_rx_mode(struct net_device *dev)
12663 {
12664 	struct bnxt *bp = netdev_priv(dev);
12665 	struct bnxt_vnic_info *vnic;
12666 	bool mc_update = false;
12667 	bool uc_update;
12668 	u32 mask;
12669 
12670 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
12671 		return;
12672 
12673 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12674 	mask = vnic->rx_mask;
12675 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
12676 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
12677 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
12678 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
12679 
12680 	if (dev->flags & IFF_PROMISC)
12681 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12682 
12683 	uc_update = bnxt_uc_list_updated(bp);
12684 
12685 	if (dev->flags & IFF_BROADCAST)
12686 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
12687 	if (dev->flags & IFF_ALLMULTI) {
12688 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12689 		vnic->mc_list_count = 0;
12690 	} else if (dev->flags & IFF_MULTICAST) {
12691 		mc_update = bnxt_mc_list_updated(bp, &mask);
12692 	}
12693 
12694 	if (mask != vnic->rx_mask || uc_update || mc_update) {
12695 		vnic->rx_mask = mask;
12696 
12697 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
12698 	}
12699 }
12700 
12701 static int bnxt_cfg_rx_mode(struct bnxt *bp)
12702 {
12703 	struct net_device *dev = bp->dev;
12704 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12705 	struct netdev_hw_addr *ha;
12706 	int i, off = 0, rc;
12707 	bool uc_update;
12708 
12709 	netif_addr_lock_bh(dev);
12710 	uc_update = bnxt_uc_list_updated(bp);
12711 	netif_addr_unlock_bh(dev);
12712 
12713 	if (!uc_update)
12714 		goto skip_uc;
12715 
12716 	for (i = 1; i < vnic->uc_filter_count; i++) {
12717 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
12718 
12719 		bnxt_hwrm_l2_filter_free(bp, fltr);
12720 		bnxt_del_l2_filter(bp, fltr);
12721 	}
12722 
12723 	vnic->uc_filter_count = 1;
12724 
12725 	netif_addr_lock_bh(dev);
12726 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
12727 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12728 	} else {
12729 		netdev_for_each_uc_addr(ha, dev) {
12730 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
12731 			off += ETH_ALEN;
12732 			vnic->uc_filter_count++;
12733 		}
12734 	}
12735 	netif_addr_unlock_bh(dev);
12736 
12737 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
12738 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
12739 		if (rc) {
12740 			if (BNXT_VF(bp) && rc == -ENODEV) {
12741 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
12742 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
12743 				else
12744 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
12745 				rc = 0;
12746 			} else {
12747 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
12748 			}
12749 			vnic->uc_filter_count = i;
12750 			return rc;
12751 		}
12752 	}
12753 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
12754 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
12755 
12756 skip_uc:
12757 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
12758 	    !bnxt_promisc_ok(bp))
12759 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12760 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
12761 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
12762 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
12763 			    rc);
12764 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
12765 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12766 		vnic->mc_list_count = 0;
12767 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
12768 	}
12769 	if (rc)
12770 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
12771 			   rc);
12772 
12773 	return rc;
12774 }
12775 
12776 static bool bnxt_can_reserve_rings(struct bnxt *bp)
12777 {
12778 #ifdef CONFIG_BNXT_SRIOV
12779 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
12780 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12781 
12782 		/* No minimum rings were provisioned by the PF.  Don't
12783 		 * reserve rings by default when device is down.
12784 		 */
12785 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
12786 			return true;
12787 
12788 		if (!netif_running(bp->dev))
12789 			return false;
12790 	}
12791 #endif
12792 	return true;
12793 }
12794 
12795 /* If the chip and firmware supports RFS */
12796 static bool bnxt_rfs_supported(struct bnxt *bp)
12797 {
12798 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
12799 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
12800 			return true;
12801 		return false;
12802 	}
12803 	/* 212 firmware is broken for aRFS */
12804 	if (BNXT_FW_MAJ(bp) == 212)
12805 		return false;
12806 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
12807 		return true;
12808 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
12809 		return true;
12810 	return false;
12811 }
12812 
12813 /* If runtime conditions support RFS */
12814 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
12815 {
12816 	struct bnxt_hw_rings hwr = {0};
12817 	int max_vnics, max_rss_ctxs;
12818 
12819 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
12820 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
12821 		return bnxt_rfs_supported(bp);
12822 
12823 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
12824 		return false;
12825 
12826 	hwr.grp = bp->rx_nr_rings;
12827 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
12828 	if (new_rss_ctx)
12829 		hwr.vnic++;
12830 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
12831 	max_vnics = bnxt_get_max_func_vnics(bp);
12832 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
12833 
12834 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
12835 		if (bp->rx_nr_rings > 1)
12836 			netdev_warn(bp->dev,
12837 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
12838 				    min(max_rss_ctxs - 1, max_vnics - 1));
12839 		return false;
12840 	}
12841 
12842 	if (!BNXT_NEW_RM(bp))
12843 		return true;
12844 
12845 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
12846 	 * issue that will mess up the default VNIC if we reduce the
12847 	 * reservations.
12848 	 */
12849 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
12850 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
12851 		return true;
12852 
12853 	bnxt_hwrm_reserve_rings(bp, &hwr);
12854 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
12855 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
12856 		return true;
12857 
12858 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
12859 	hwr.vnic = 1;
12860 	hwr.rss_ctx = 0;
12861 	bnxt_hwrm_reserve_rings(bp, &hwr);
12862 	return false;
12863 }
12864 
12865 static netdev_features_t bnxt_fix_features(struct net_device *dev,
12866 					   netdev_features_t features)
12867 {
12868 	struct bnxt *bp = netdev_priv(dev);
12869 	netdev_features_t vlan_features;
12870 
12871 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
12872 		features &= ~NETIF_F_NTUPLE;
12873 
12874 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
12875 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
12876 
12877 	if (!(features & NETIF_F_GRO))
12878 		features &= ~NETIF_F_GRO_HW;
12879 
12880 	if (features & NETIF_F_GRO_HW)
12881 		features &= ~NETIF_F_LRO;
12882 
12883 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
12884 	 * turned on or off together.
12885 	 */
12886 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
12887 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
12888 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
12889 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
12890 		else if (vlan_features)
12891 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
12892 	}
12893 #ifdef CONFIG_BNXT_SRIOV
12894 	if (BNXT_VF(bp) && bp->vf.vlan)
12895 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
12896 #endif
12897 	return features;
12898 }
12899 
12900 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
12901 				bool link_re_init, u32 flags, bool update_tpa)
12902 {
12903 	bnxt_close_nic(bp, irq_re_init, link_re_init);
12904 	bp->flags = flags;
12905 	if (update_tpa)
12906 		bnxt_set_ring_params(bp);
12907 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
12908 }
12909 
12910 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
12911 {
12912 	bool update_tpa = false, update_ntuple = false;
12913 	struct bnxt *bp = netdev_priv(dev);
12914 	u32 flags = bp->flags;
12915 	u32 changes;
12916 	int rc = 0;
12917 	bool re_init = false;
12918 
12919 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
12920 	if (features & NETIF_F_GRO_HW)
12921 		flags |= BNXT_FLAG_GRO;
12922 	else if (features & NETIF_F_LRO)
12923 		flags |= BNXT_FLAG_LRO;
12924 
12925 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
12926 		flags &= ~BNXT_FLAG_TPA;
12927 
12928 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
12929 		flags |= BNXT_FLAG_STRIP_VLAN;
12930 
12931 	if (features & NETIF_F_NTUPLE)
12932 		flags |= BNXT_FLAG_RFS;
12933 	else
12934 		bnxt_clear_usr_fltrs(bp, true);
12935 
12936 	changes = flags ^ bp->flags;
12937 	if (changes & BNXT_FLAG_TPA) {
12938 		update_tpa = true;
12939 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
12940 		    (flags & BNXT_FLAG_TPA) == 0 ||
12941 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
12942 			re_init = true;
12943 	}
12944 
12945 	if (changes & ~BNXT_FLAG_TPA)
12946 		re_init = true;
12947 
12948 	if (changes & BNXT_FLAG_RFS)
12949 		update_ntuple = true;
12950 
12951 	if (flags != bp->flags) {
12952 		u32 old_flags = bp->flags;
12953 
12954 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12955 			bp->flags = flags;
12956 			if (update_tpa)
12957 				bnxt_set_ring_params(bp);
12958 			return rc;
12959 		}
12960 
12961 		if (update_ntuple)
12962 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
12963 
12964 		if (re_init)
12965 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
12966 
12967 		if (update_tpa) {
12968 			bp->flags = flags;
12969 			rc = bnxt_set_tpa(bp,
12970 					  (flags & BNXT_FLAG_TPA) ?
12971 					  true : false);
12972 			if (rc)
12973 				bp->flags = old_flags;
12974 		}
12975 	}
12976 	return rc;
12977 }
12978 
12979 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
12980 			      u8 **nextp)
12981 {
12982 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
12983 	struct hop_jumbo_hdr *jhdr;
12984 	int hdr_count = 0;
12985 	u8 *nexthdr;
12986 	int start;
12987 
12988 	/* Check that there are at most 2 IPv6 extension headers, no
12989 	 * fragment header, and each is <= 64 bytes.
12990 	 */
12991 	start = nw_off + sizeof(*ip6h);
12992 	nexthdr = &ip6h->nexthdr;
12993 	while (ipv6_ext_hdr(*nexthdr)) {
12994 		struct ipv6_opt_hdr *hp;
12995 		int hdrlen;
12996 
12997 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
12998 		    *nexthdr == NEXTHDR_FRAGMENT)
12999 			return false;
13000 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
13001 					  skb_headlen(skb), NULL);
13002 		if (!hp)
13003 			return false;
13004 		if (*nexthdr == NEXTHDR_AUTH)
13005 			hdrlen = ipv6_authlen(hp);
13006 		else
13007 			hdrlen = ipv6_optlen(hp);
13008 
13009 		if (hdrlen > 64)
13010 			return false;
13011 
13012 		/* The ext header may be a hop-by-hop header inserted for
13013 		 * big TCP purposes. This will be removed before sending
13014 		 * from NIC, so do not count it.
13015 		 */
13016 		if (*nexthdr == NEXTHDR_HOP) {
13017 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
13018 				goto increment_hdr;
13019 
13020 			jhdr = (struct hop_jumbo_hdr *)hp;
13021 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
13022 			    jhdr->nexthdr != IPPROTO_TCP)
13023 				goto increment_hdr;
13024 
13025 			goto next_hdr;
13026 		}
13027 increment_hdr:
13028 		hdr_count++;
13029 next_hdr:
13030 		nexthdr = &hp->nexthdr;
13031 		start += hdrlen;
13032 	}
13033 	if (nextp) {
13034 		/* Caller will check inner protocol */
13035 		if (skb->encapsulation) {
13036 			*nextp = nexthdr;
13037 			return true;
13038 		}
13039 		*nextp = NULL;
13040 	}
13041 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
13042 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
13043 }
13044 
13045 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
13046 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
13047 {
13048 	struct udphdr *uh = udp_hdr(skb);
13049 	__be16 udp_port = uh->dest;
13050 
13051 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
13052 	    udp_port != bp->vxlan_gpe_port)
13053 		return false;
13054 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
13055 		struct ethhdr *eh = inner_eth_hdr(skb);
13056 
13057 		switch (eh->h_proto) {
13058 		case htons(ETH_P_IP):
13059 			return true;
13060 		case htons(ETH_P_IPV6):
13061 			return bnxt_exthdr_check(bp, skb,
13062 						 skb_inner_network_offset(skb),
13063 						 NULL);
13064 		}
13065 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
13066 		return true;
13067 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
13068 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13069 					 NULL);
13070 	}
13071 	return false;
13072 }
13073 
13074 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
13075 {
13076 	switch (l4_proto) {
13077 	case IPPROTO_UDP:
13078 		return bnxt_udp_tunl_check(bp, skb);
13079 	case IPPROTO_IPIP:
13080 		return true;
13081 	case IPPROTO_GRE: {
13082 		switch (skb->inner_protocol) {
13083 		default:
13084 			return false;
13085 		case htons(ETH_P_IP):
13086 			return true;
13087 		case htons(ETH_P_IPV6):
13088 			fallthrough;
13089 		}
13090 	}
13091 	case IPPROTO_IPV6:
13092 		/* Check ext headers of inner ipv6 */
13093 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13094 					 NULL);
13095 	}
13096 	return false;
13097 }
13098 
13099 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
13100 					     struct net_device *dev,
13101 					     netdev_features_t features)
13102 {
13103 	struct bnxt *bp = netdev_priv(dev);
13104 	u8 *l4_proto;
13105 
13106 	features = vlan_features_check(skb, features);
13107 	switch (vlan_get_protocol(skb)) {
13108 	case htons(ETH_P_IP):
13109 		if (!skb->encapsulation)
13110 			return features;
13111 		l4_proto = &ip_hdr(skb)->protocol;
13112 		if (bnxt_tunl_check(bp, skb, *l4_proto))
13113 			return features;
13114 		break;
13115 	case htons(ETH_P_IPV6):
13116 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
13117 				       &l4_proto))
13118 			break;
13119 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
13120 			return features;
13121 		break;
13122 	}
13123 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13124 }
13125 
13126 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
13127 			 u32 *reg_buf)
13128 {
13129 	struct hwrm_dbg_read_direct_output *resp;
13130 	struct hwrm_dbg_read_direct_input *req;
13131 	__le32 *dbg_reg_buf;
13132 	dma_addr_t mapping;
13133 	int rc, i;
13134 
13135 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
13136 	if (rc)
13137 		return rc;
13138 
13139 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
13140 					 &mapping);
13141 	if (!dbg_reg_buf) {
13142 		rc = -ENOMEM;
13143 		goto dbg_rd_reg_exit;
13144 	}
13145 
13146 	req->host_dest_addr = cpu_to_le64(mapping);
13147 
13148 	resp = hwrm_req_hold(bp, req);
13149 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
13150 	req->read_len32 = cpu_to_le32(num_words);
13151 
13152 	rc = hwrm_req_send(bp, req);
13153 	if (rc || resp->error_code) {
13154 		rc = -EIO;
13155 		goto dbg_rd_reg_exit;
13156 	}
13157 	for (i = 0; i < num_words; i++)
13158 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
13159 
13160 dbg_rd_reg_exit:
13161 	hwrm_req_drop(bp, req);
13162 	return rc;
13163 }
13164 
13165 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
13166 				       u32 ring_id, u32 *prod, u32 *cons)
13167 {
13168 	struct hwrm_dbg_ring_info_get_output *resp;
13169 	struct hwrm_dbg_ring_info_get_input *req;
13170 	int rc;
13171 
13172 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
13173 	if (rc)
13174 		return rc;
13175 
13176 	req->ring_type = ring_type;
13177 	req->fw_ring_id = cpu_to_le32(ring_id);
13178 	resp = hwrm_req_hold(bp, req);
13179 	rc = hwrm_req_send(bp, req);
13180 	if (!rc) {
13181 		*prod = le32_to_cpu(resp->producer_index);
13182 		*cons = le32_to_cpu(resp->consumer_index);
13183 	}
13184 	hwrm_req_drop(bp, req);
13185 	return rc;
13186 }
13187 
13188 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13189 {
13190 	struct bnxt_tx_ring_info *txr;
13191 	int i = bnapi->index, j;
13192 
13193 	bnxt_for_each_napi_tx(j, bnapi, txr)
13194 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13195 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13196 			    txr->tx_cons);
13197 }
13198 
13199 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
13200 {
13201 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
13202 	int i = bnapi->index;
13203 
13204 	if (!rxr)
13205 		return;
13206 
13207 	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",
13208 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
13209 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
13210 		    rxr->rx_sw_agg_prod);
13211 }
13212 
13213 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
13214 {
13215 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13216 	int i = bnapi->index;
13217 
13218 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
13219 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
13220 }
13221 
13222 static void bnxt_dbg_dump_states(struct bnxt *bp)
13223 {
13224 	int i;
13225 	struct bnxt_napi *bnapi;
13226 
13227 	for (i = 0; i < bp->cp_nr_rings; i++) {
13228 		bnapi = bp->bnapi[i];
13229 		if (netif_msg_drv(bp)) {
13230 			bnxt_dump_tx_sw_state(bnapi);
13231 			bnxt_dump_rx_sw_state(bnapi);
13232 			bnxt_dump_cp_sw_state(bnapi);
13233 		}
13234 	}
13235 }
13236 
13237 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
13238 {
13239 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
13240 	struct hwrm_ring_reset_input *req;
13241 	struct bnxt_napi *bnapi = rxr->bnapi;
13242 	struct bnxt_cp_ring_info *cpr;
13243 	u16 cp_ring_id;
13244 	int rc;
13245 
13246 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
13247 	if (rc)
13248 		return rc;
13249 
13250 	cpr = &bnapi->cp_ring;
13251 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
13252 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
13253 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
13254 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
13255 	return hwrm_req_send_silent(bp, req);
13256 }
13257 
13258 static void bnxt_reset_task(struct bnxt *bp, bool silent)
13259 {
13260 	if (!silent)
13261 		bnxt_dbg_dump_states(bp);
13262 	if (netif_running(bp->dev)) {
13263 		bnxt_close_nic(bp, !silent, false);
13264 		bnxt_open_nic(bp, !silent, false);
13265 	}
13266 }
13267 
13268 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
13269 {
13270 	struct bnxt *bp = netdev_priv(dev);
13271 
13272 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
13273 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
13274 }
13275 
13276 static void bnxt_fw_health_check(struct bnxt *bp)
13277 {
13278 	struct bnxt_fw_health *fw_health = bp->fw_health;
13279 	struct pci_dev *pdev = bp->pdev;
13280 	u32 val;
13281 
13282 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13283 		return;
13284 
13285 	/* Make sure it is enabled before checking the tmr_counter. */
13286 	smp_rmb();
13287 	if (fw_health->tmr_counter) {
13288 		fw_health->tmr_counter--;
13289 		return;
13290 	}
13291 
13292 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13293 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
13294 		fw_health->arrests++;
13295 		goto fw_reset;
13296 	}
13297 
13298 	fw_health->last_fw_heartbeat = val;
13299 
13300 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13301 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
13302 		fw_health->discoveries++;
13303 		goto fw_reset;
13304 	}
13305 
13306 	fw_health->tmr_counter = fw_health->tmr_multiplier;
13307 	return;
13308 
13309 fw_reset:
13310 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
13311 }
13312 
13313 static void bnxt_timer(struct timer_list *t)
13314 {
13315 	struct bnxt *bp = from_timer(bp, t, timer);
13316 	struct net_device *dev = bp->dev;
13317 
13318 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
13319 		return;
13320 
13321 	if (atomic_read(&bp->intr_sem) != 0)
13322 		goto bnxt_restart_timer;
13323 
13324 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
13325 		bnxt_fw_health_check(bp);
13326 
13327 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
13328 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
13329 
13330 	if (bnxt_tc_flower_enabled(bp))
13331 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
13332 
13333 #ifdef CONFIG_RFS_ACCEL
13334 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
13335 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
13336 #endif /*CONFIG_RFS_ACCEL*/
13337 
13338 	if (bp->link_info.phy_retry) {
13339 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
13340 			bp->link_info.phy_retry = false;
13341 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
13342 		} else {
13343 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
13344 		}
13345 	}
13346 
13347 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13348 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13349 
13350 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
13351 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
13352 
13353 bnxt_restart_timer:
13354 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13355 }
13356 
13357 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
13358 {
13359 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
13360 	 * set.  If the device is being closed, bnxt_close() may be holding
13361 	 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear.  So we
13362 	 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
13363 	 */
13364 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13365 	rtnl_lock();
13366 }
13367 
13368 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
13369 {
13370 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13371 	rtnl_unlock();
13372 }
13373 
13374 /* Only called from bnxt_sp_task() */
13375 static void bnxt_reset(struct bnxt *bp, bool silent)
13376 {
13377 	bnxt_rtnl_lock_sp(bp);
13378 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
13379 		bnxt_reset_task(bp, silent);
13380 	bnxt_rtnl_unlock_sp(bp);
13381 }
13382 
13383 /* Only called from bnxt_sp_task() */
13384 static void bnxt_rx_ring_reset(struct bnxt *bp)
13385 {
13386 	int i;
13387 
13388 	bnxt_rtnl_lock_sp(bp);
13389 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13390 		bnxt_rtnl_unlock_sp(bp);
13391 		return;
13392 	}
13393 	/* Disable and flush TPA before resetting the RX ring */
13394 	if (bp->flags & BNXT_FLAG_TPA)
13395 		bnxt_set_tpa(bp, false);
13396 	for (i = 0; i < bp->rx_nr_rings; i++) {
13397 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
13398 		struct bnxt_cp_ring_info *cpr;
13399 		int rc;
13400 
13401 		if (!rxr->bnapi->in_reset)
13402 			continue;
13403 
13404 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
13405 		if (rc) {
13406 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
13407 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
13408 			else
13409 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
13410 					    rc);
13411 			bnxt_reset_task(bp, true);
13412 			break;
13413 		}
13414 		bnxt_free_one_rx_ring_skbs(bp, i);
13415 		rxr->rx_prod = 0;
13416 		rxr->rx_agg_prod = 0;
13417 		rxr->rx_sw_agg_prod = 0;
13418 		rxr->rx_next_cons = 0;
13419 		rxr->bnapi->in_reset = false;
13420 		bnxt_alloc_one_rx_ring(bp, i);
13421 		cpr = &rxr->bnapi->cp_ring;
13422 		cpr->sw_stats->rx.rx_resets++;
13423 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
13424 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
13425 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
13426 	}
13427 	if (bp->flags & BNXT_FLAG_TPA)
13428 		bnxt_set_tpa(bp, true);
13429 	bnxt_rtnl_unlock_sp(bp);
13430 }
13431 
13432 static void bnxt_fw_fatal_close(struct bnxt *bp)
13433 {
13434 	bnxt_tx_disable(bp);
13435 	bnxt_disable_napi(bp);
13436 	bnxt_disable_int_sync(bp);
13437 	bnxt_free_irq(bp);
13438 	bnxt_clear_int_mode(bp);
13439 	pci_disable_device(bp->pdev);
13440 }
13441 
13442 static void bnxt_fw_reset_close(struct bnxt *bp)
13443 {
13444 	/* When firmware is in fatal state, quiesce device and disable
13445 	 * bus master to prevent any potential bad DMAs before freeing
13446 	 * kernel memory.
13447 	 */
13448 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
13449 		u16 val = 0;
13450 
13451 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
13452 		if (val == 0xffff)
13453 			bp->fw_reset_min_dsecs = 0;
13454 		bnxt_fw_fatal_close(bp);
13455 	}
13456 	__bnxt_close_nic(bp, true, false);
13457 	bnxt_vf_reps_free(bp);
13458 	bnxt_clear_int_mode(bp);
13459 	bnxt_hwrm_func_drv_unrgtr(bp);
13460 	if (pci_is_enabled(bp->pdev))
13461 		pci_disable_device(bp->pdev);
13462 	bnxt_free_ctx_mem(bp);
13463 }
13464 
13465 static bool is_bnxt_fw_ok(struct bnxt *bp)
13466 {
13467 	struct bnxt_fw_health *fw_health = bp->fw_health;
13468 	bool no_heartbeat = false, has_reset = false;
13469 	u32 val;
13470 
13471 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13472 	if (val == fw_health->last_fw_heartbeat)
13473 		no_heartbeat = true;
13474 
13475 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13476 	if (val != fw_health->last_fw_reset_cnt)
13477 		has_reset = true;
13478 
13479 	if (!no_heartbeat && has_reset)
13480 		return true;
13481 
13482 	return false;
13483 }
13484 
13485 /* rtnl_lock is acquired before calling this function */
13486 static void bnxt_force_fw_reset(struct bnxt *bp)
13487 {
13488 	struct bnxt_fw_health *fw_health = bp->fw_health;
13489 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13490 	u32 wait_dsecs;
13491 
13492 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
13493 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13494 		return;
13495 
13496 	/* we have to serialize with bnxt_refclk_read()*/
13497 	if (ptp) {
13498 		unsigned long flags;
13499 
13500 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
13501 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13502 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
13503 	} else {
13504 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13505 	}
13506 	bnxt_fw_reset_close(bp);
13507 	wait_dsecs = fw_health->master_func_wait_dsecs;
13508 	if (fw_health->primary) {
13509 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
13510 			wait_dsecs = 0;
13511 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
13512 	} else {
13513 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
13514 		wait_dsecs = fw_health->normal_func_wait_dsecs;
13515 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13516 	}
13517 
13518 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
13519 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
13520 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
13521 }
13522 
13523 void bnxt_fw_exception(struct bnxt *bp)
13524 {
13525 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
13526 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
13527 	bnxt_ulp_stop(bp);
13528 	bnxt_rtnl_lock_sp(bp);
13529 	bnxt_force_fw_reset(bp);
13530 	bnxt_rtnl_unlock_sp(bp);
13531 }
13532 
13533 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
13534  * < 0 on error.
13535  */
13536 static int bnxt_get_registered_vfs(struct bnxt *bp)
13537 {
13538 #ifdef CONFIG_BNXT_SRIOV
13539 	int rc;
13540 
13541 	if (!BNXT_PF(bp))
13542 		return 0;
13543 
13544 	rc = bnxt_hwrm_func_qcfg(bp);
13545 	if (rc) {
13546 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
13547 		return rc;
13548 	}
13549 	if (bp->pf.registered_vfs)
13550 		return bp->pf.registered_vfs;
13551 	if (bp->sriov_cfg)
13552 		return 1;
13553 #endif
13554 	return 0;
13555 }
13556 
13557 void bnxt_fw_reset(struct bnxt *bp)
13558 {
13559 	bnxt_ulp_stop(bp);
13560 	bnxt_rtnl_lock_sp(bp);
13561 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
13562 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13563 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13564 		int n = 0, tmo;
13565 
13566 		/* we have to serialize with bnxt_refclk_read()*/
13567 		if (ptp) {
13568 			unsigned long flags;
13569 
13570 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
13571 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13572 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
13573 		} else {
13574 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13575 		}
13576 		if (bp->pf.active_vfs &&
13577 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
13578 			n = bnxt_get_registered_vfs(bp);
13579 		if (n < 0) {
13580 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
13581 				   n);
13582 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13583 			dev_close(bp->dev);
13584 			goto fw_reset_exit;
13585 		} else if (n > 0) {
13586 			u16 vf_tmo_dsecs = n * 10;
13587 
13588 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
13589 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
13590 			bp->fw_reset_state =
13591 				BNXT_FW_RESET_STATE_POLL_VF;
13592 			bnxt_queue_fw_reset_work(bp, HZ / 10);
13593 			goto fw_reset_exit;
13594 		}
13595 		bnxt_fw_reset_close(bp);
13596 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
13597 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
13598 			tmo = HZ / 10;
13599 		} else {
13600 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13601 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
13602 		}
13603 		bnxt_queue_fw_reset_work(bp, tmo);
13604 	}
13605 fw_reset_exit:
13606 	bnxt_rtnl_unlock_sp(bp);
13607 }
13608 
13609 static void bnxt_chk_missed_irq(struct bnxt *bp)
13610 {
13611 	int i;
13612 
13613 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13614 		return;
13615 
13616 	for (i = 0; i < bp->cp_nr_rings; i++) {
13617 		struct bnxt_napi *bnapi = bp->bnapi[i];
13618 		struct bnxt_cp_ring_info *cpr;
13619 		u32 fw_ring_id;
13620 		int j;
13621 
13622 		if (!bnapi)
13623 			continue;
13624 
13625 		cpr = &bnapi->cp_ring;
13626 		for (j = 0; j < cpr->cp_ring_count; j++) {
13627 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
13628 			u32 val[2];
13629 
13630 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
13631 				continue;
13632 
13633 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
13634 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
13635 				continue;
13636 			}
13637 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
13638 			bnxt_dbg_hwrm_ring_info_get(bp,
13639 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
13640 				fw_ring_id, &val[0], &val[1]);
13641 			cpr->sw_stats->cmn.missed_irqs++;
13642 		}
13643 	}
13644 }
13645 
13646 static void bnxt_cfg_ntp_filters(struct bnxt *);
13647 
13648 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
13649 {
13650 	struct bnxt_link_info *link_info = &bp->link_info;
13651 
13652 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
13653 		link_info->autoneg = BNXT_AUTONEG_SPEED;
13654 		if (bp->hwrm_spec_code >= 0x10201) {
13655 			if (link_info->auto_pause_setting &
13656 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
13657 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13658 		} else {
13659 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13660 		}
13661 		bnxt_set_auto_speed(link_info);
13662 	} else {
13663 		bnxt_set_force_speed(link_info);
13664 		link_info->req_duplex = link_info->duplex_setting;
13665 	}
13666 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
13667 		link_info->req_flow_ctrl =
13668 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
13669 	else
13670 		link_info->req_flow_ctrl = link_info->force_pause_setting;
13671 }
13672 
13673 static void bnxt_fw_echo_reply(struct bnxt *bp)
13674 {
13675 	struct bnxt_fw_health *fw_health = bp->fw_health;
13676 	struct hwrm_func_echo_response_input *req;
13677 	int rc;
13678 
13679 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
13680 	if (rc)
13681 		return;
13682 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
13683 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
13684 	hwrm_req_send(bp, req);
13685 }
13686 
13687 static void bnxt_ulp_restart(struct bnxt *bp)
13688 {
13689 	bnxt_ulp_stop(bp);
13690 	bnxt_ulp_start(bp, 0);
13691 }
13692 
13693 static void bnxt_sp_task(struct work_struct *work)
13694 {
13695 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
13696 
13697 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13698 	smp_mb__after_atomic();
13699 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13700 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13701 		return;
13702 	}
13703 
13704 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
13705 		bnxt_ulp_restart(bp);
13706 		bnxt_reenable_sriov(bp);
13707 	}
13708 
13709 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
13710 		bnxt_cfg_rx_mode(bp);
13711 
13712 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
13713 		bnxt_cfg_ntp_filters(bp);
13714 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
13715 		bnxt_hwrm_exec_fwd_req(bp);
13716 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
13717 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
13718 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
13719 		bnxt_hwrm_port_qstats(bp, 0);
13720 		bnxt_hwrm_port_qstats_ext(bp, 0);
13721 		bnxt_accumulate_all_stats(bp);
13722 	}
13723 
13724 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
13725 		int rc;
13726 
13727 		mutex_lock(&bp->link_lock);
13728 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
13729 				       &bp->sp_event))
13730 			bnxt_hwrm_phy_qcaps(bp);
13731 
13732 		rc = bnxt_update_link(bp, true);
13733 		if (rc)
13734 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
13735 				   rc);
13736 
13737 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
13738 				       &bp->sp_event))
13739 			bnxt_init_ethtool_link_settings(bp);
13740 		mutex_unlock(&bp->link_lock);
13741 	}
13742 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
13743 		int rc;
13744 
13745 		mutex_lock(&bp->link_lock);
13746 		rc = bnxt_update_phy_setting(bp);
13747 		mutex_unlock(&bp->link_lock);
13748 		if (rc) {
13749 			netdev_warn(bp->dev, "update phy settings retry failed\n");
13750 		} else {
13751 			bp->link_info.phy_retry = false;
13752 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
13753 		}
13754 	}
13755 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
13756 		mutex_lock(&bp->link_lock);
13757 		bnxt_get_port_module_status(bp);
13758 		mutex_unlock(&bp->link_lock);
13759 	}
13760 
13761 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
13762 		bnxt_tc_flow_stats_work(bp);
13763 
13764 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
13765 		bnxt_chk_missed_irq(bp);
13766 
13767 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
13768 		bnxt_fw_echo_reply(bp);
13769 
13770 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
13771 		bnxt_hwmon_notify_event(bp);
13772 
13773 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
13774 	 * must be the last functions to be called before exiting.
13775 	 */
13776 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
13777 		bnxt_reset(bp, false);
13778 
13779 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
13780 		bnxt_reset(bp, true);
13781 
13782 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
13783 		bnxt_rx_ring_reset(bp);
13784 
13785 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
13786 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
13787 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
13788 			bnxt_devlink_health_fw_report(bp);
13789 		else
13790 			bnxt_fw_reset(bp);
13791 	}
13792 
13793 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
13794 		if (!is_bnxt_fw_ok(bp))
13795 			bnxt_devlink_health_fw_report(bp);
13796 	}
13797 
13798 	smp_mb__before_atomic();
13799 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13800 }
13801 
13802 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
13803 				int *max_cp);
13804 
13805 /* Under rtnl_lock */
13806 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
13807 		     int tx_xdp)
13808 {
13809 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
13810 	struct bnxt_hw_rings hwr = {0};
13811 	int rx_rings = rx;
13812 	int rc;
13813 
13814 	if (tcs)
13815 		tx_sets = tcs;
13816 
13817 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
13818 
13819 	if (max_rx < rx_rings)
13820 		return -ENOMEM;
13821 
13822 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
13823 		rx_rings <<= 1;
13824 
13825 	hwr.rx = rx_rings;
13826 	hwr.tx = tx * tx_sets + tx_xdp;
13827 	if (max_tx < hwr.tx)
13828 		return -ENOMEM;
13829 
13830 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
13831 
13832 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
13833 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
13834 	if (max_cp < hwr.cp)
13835 		return -ENOMEM;
13836 	hwr.stat = hwr.cp;
13837 	if (BNXT_NEW_RM(bp)) {
13838 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
13839 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
13840 		hwr.grp = rx;
13841 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13842 	}
13843 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
13844 		hwr.cp_p5 = hwr.tx + rx;
13845 	rc = bnxt_hwrm_check_rings(bp, &hwr);
13846 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
13847 		if (!bnxt_ulp_registered(bp->edev)) {
13848 			hwr.cp += bnxt_get_ulp_msix_num(bp);
13849 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
13850 		}
13851 		if (hwr.cp > bp->total_irqs) {
13852 			int total_msix = bnxt_change_msix(bp, hwr.cp);
13853 
13854 			if (total_msix < hwr.cp) {
13855 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
13856 					    hwr.cp, total_msix);
13857 				rc = -ENOSPC;
13858 			}
13859 		}
13860 	}
13861 	return rc;
13862 }
13863 
13864 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
13865 {
13866 	if (bp->bar2) {
13867 		pci_iounmap(pdev, bp->bar2);
13868 		bp->bar2 = NULL;
13869 	}
13870 
13871 	if (bp->bar1) {
13872 		pci_iounmap(pdev, bp->bar1);
13873 		bp->bar1 = NULL;
13874 	}
13875 
13876 	if (bp->bar0) {
13877 		pci_iounmap(pdev, bp->bar0);
13878 		bp->bar0 = NULL;
13879 	}
13880 }
13881 
13882 static void bnxt_cleanup_pci(struct bnxt *bp)
13883 {
13884 	bnxt_unmap_bars(bp, bp->pdev);
13885 	pci_release_regions(bp->pdev);
13886 	if (pci_is_enabled(bp->pdev))
13887 		pci_disable_device(bp->pdev);
13888 }
13889 
13890 static void bnxt_init_dflt_coal(struct bnxt *bp)
13891 {
13892 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
13893 	struct bnxt_coal *coal;
13894 	u16 flags = 0;
13895 
13896 	if (coal_cap->cmpl_params &
13897 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
13898 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
13899 
13900 	/* Tick values in micro seconds.
13901 	 * 1 coal_buf x bufs_per_record = 1 completion record.
13902 	 */
13903 	coal = &bp->rx_coal;
13904 	coal->coal_ticks = 10;
13905 	coal->coal_bufs = 30;
13906 	coal->coal_ticks_irq = 1;
13907 	coal->coal_bufs_irq = 2;
13908 	coal->idle_thresh = 50;
13909 	coal->bufs_per_record = 2;
13910 	coal->budget = 64;		/* NAPI budget */
13911 	coal->flags = flags;
13912 
13913 	coal = &bp->tx_coal;
13914 	coal->coal_ticks = 28;
13915 	coal->coal_bufs = 30;
13916 	coal->coal_ticks_irq = 2;
13917 	coal->coal_bufs_irq = 2;
13918 	coal->bufs_per_record = 1;
13919 	coal->flags = flags;
13920 
13921 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
13922 }
13923 
13924 /* FW that pre-reserves 1 VNIC per function */
13925 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
13926 {
13927 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
13928 
13929 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13930 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
13931 		return true;
13932 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13933 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
13934 		return true;
13935 	return false;
13936 }
13937 
13938 static int bnxt_fw_init_one_p1(struct bnxt *bp)
13939 {
13940 	int rc;
13941 
13942 	bp->fw_cap = 0;
13943 	rc = bnxt_hwrm_ver_get(bp);
13944 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
13945 	 * so wait before continuing with recovery.
13946 	 */
13947 	if (rc)
13948 		msleep(100);
13949 	bnxt_try_map_fw_health_reg(bp);
13950 	if (rc) {
13951 		rc = bnxt_try_recover_fw(bp);
13952 		if (rc)
13953 			return rc;
13954 		rc = bnxt_hwrm_ver_get(bp);
13955 		if (rc)
13956 			return rc;
13957 	}
13958 
13959 	bnxt_nvm_cfg_ver_get(bp);
13960 
13961 	rc = bnxt_hwrm_func_reset(bp);
13962 	if (rc)
13963 		return -ENODEV;
13964 
13965 	bnxt_hwrm_fw_set_time(bp);
13966 	return 0;
13967 }
13968 
13969 static int bnxt_fw_init_one_p2(struct bnxt *bp)
13970 {
13971 	int rc;
13972 
13973 	/* Get the MAX capabilities for this function */
13974 	rc = bnxt_hwrm_func_qcaps(bp);
13975 	if (rc) {
13976 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
13977 			   rc);
13978 		return -ENODEV;
13979 	}
13980 
13981 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
13982 	if (rc)
13983 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
13984 			    rc);
13985 
13986 	if (bnxt_alloc_fw_health(bp)) {
13987 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
13988 	} else {
13989 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
13990 		if (rc)
13991 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
13992 				    rc);
13993 	}
13994 
13995 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
13996 	if (rc)
13997 		return -ENODEV;
13998 
13999 	rc = bnxt_alloc_crash_dump_mem(bp);
14000 	if (rc)
14001 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
14002 			    rc);
14003 	if (!rc) {
14004 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
14005 		if (rc) {
14006 			bnxt_free_crash_dump_mem(bp);
14007 			netdev_warn(bp->dev,
14008 				    "hwrm crash dump mem failure rc: %d\n", rc);
14009 		}
14010 	}
14011 
14012 	if (bnxt_fw_pre_resv_vnics(bp))
14013 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
14014 
14015 	bnxt_hwrm_func_qcfg(bp);
14016 	bnxt_hwrm_vnic_qcaps(bp);
14017 	bnxt_hwrm_port_led_qcaps(bp);
14018 	bnxt_ethtool_init(bp);
14019 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
14020 		__bnxt_hwrm_ptp_qcfg(bp);
14021 	bnxt_dcb_init(bp);
14022 	bnxt_hwmon_init(bp);
14023 	return 0;
14024 }
14025 
14026 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
14027 {
14028 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
14029 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
14030 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
14031 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
14032 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
14033 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
14034 		bp->rss_hash_delta = bp->rss_hash_cfg;
14035 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
14036 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
14037 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
14038 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
14039 	}
14040 }
14041 
14042 static void bnxt_set_dflt_rfs(struct bnxt *bp)
14043 {
14044 	struct net_device *dev = bp->dev;
14045 
14046 	dev->hw_features &= ~NETIF_F_NTUPLE;
14047 	dev->features &= ~NETIF_F_NTUPLE;
14048 	bp->flags &= ~BNXT_FLAG_RFS;
14049 	if (bnxt_rfs_supported(bp)) {
14050 		dev->hw_features |= NETIF_F_NTUPLE;
14051 		if (bnxt_rfs_capable(bp, false)) {
14052 			bp->flags |= BNXT_FLAG_RFS;
14053 			dev->features |= NETIF_F_NTUPLE;
14054 		}
14055 	}
14056 }
14057 
14058 static void bnxt_fw_init_one_p3(struct bnxt *bp)
14059 {
14060 	struct pci_dev *pdev = bp->pdev;
14061 
14062 	bnxt_set_dflt_rss_hash_type(bp);
14063 	bnxt_set_dflt_rfs(bp);
14064 
14065 	bnxt_get_wol_settings(bp);
14066 	if (bp->flags & BNXT_FLAG_WOL_CAP)
14067 		device_set_wakeup_enable(&pdev->dev, bp->wol);
14068 	else
14069 		device_set_wakeup_capable(&pdev->dev, false);
14070 
14071 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
14072 	bnxt_hwrm_coal_params_qcaps(bp);
14073 }
14074 
14075 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
14076 
14077 int bnxt_fw_init_one(struct bnxt *bp)
14078 {
14079 	int rc;
14080 
14081 	rc = bnxt_fw_init_one_p1(bp);
14082 	if (rc) {
14083 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
14084 		return rc;
14085 	}
14086 	rc = bnxt_fw_init_one_p2(bp);
14087 	if (rc) {
14088 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
14089 		return rc;
14090 	}
14091 	rc = bnxt_probe_phy(bp, false);
14092 	if (rc)
14093 		return rc;
14094 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
14095 	if (rc)
14096 		return rc;
14097 
14098 	bnxt_fw_init_one_p3(bp);
14099 	return 0;
14100 }
14101 
14102 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
14103 {
14104 	struct bnxt_fw_health *fw_health = bp->fw_health;
14105 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
14106 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
14107 	u32 reg_type, reg_off, delay_msecs;
14108 
14109 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
14110 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
14111 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
14112 	switch (reg_type) {
14113 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
14114 		pci_write_config_dword(bp->pdev, reg_off, val);
14115 		break;
14116 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
14117 		writel(reg_off & BNXT_GRC_BASE_MASK,
14118 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
14119 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
14120 		fallthrough;
14121 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
14122 		writel(val, bp->bar0 + reg_off);
14123 		break;
14124 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
14125 		writel(val, bp->bar1 + reg_off);
14126 		break;
14127 	}
14128 	if (delay_msecs) {
14129 		pci_read_config_dword(bp->pdev, 0, &val);
14130 		msleep(delay_msecs);
14131 	}
14132 }
14133 
14134 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
14135 {
14136 	struct hwrm_func_qcfg_output *resp;
14137 	struct hwrm_func_qcfg_input *req;
14138 	bool result = true; /* firmware will enforce if unknown */
14139 
14140 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
14141 		return result;
14142 
14143 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
14144 		return result;
14145 
14146 	req->fid = cpu_to_le16(0xffff);
14147 	resp = hwrm_req_hold(bp, req);
14148 	if (!hwrm_req_send(bp, req))
14149 		result = !!(le16_to_cpu(resp->flags) &
14150 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
14151 	hwrm_req_drop(bp, req);
14152 	return result;
14153 }
14154 
14155 static void bnxt_reset_all(struct bnxt *bp)
14156 {
14157 	struct bnxt_fw_health *fw_health = bp->fw_health;
14158 	int i, rc;
14159 
14160 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14161 		bnxt_fw_reset_via_optee(bp);
14162 		bp->fw_reset_timestamp = jiffies;
14163 		return;
14164 	}
14165 
14166 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
14167 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
14168 			bnxt_fw_reset_writel(bp, i);
14169 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
14170 		struct hwrm_fw_reset_input *req;
14171 
14172 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
14173 		if (!rc) {
14174 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
14175 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
14176 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
14177 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
14178 			rc = hwrm_req_send(bp, req);
14179 		}
14180 		if (rc != -ENODEV)
14181 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
14182 	}
14183 	bp->fw_reset_timestamp = jiffies;
14184 }
14185 
14186 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
14187 {
14188 	return time_after(jiffies, bp->fw_reset_timestamp +
14189 			  (bp->fw_reset_max_dsecs * HZ / 10));
14190 }
14191 
14192 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
14193 {
14194 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14195 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
14196 		bnxt_dl_health_fw_status_update(bp, false);
14197 	bp->fw_reset_state = 0;
14198 	dev_close(bp->dev);
14199 }
14200 
14201 static void bnxt_fw_reset_task(struct work_struct *work)
14202 {
14203 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
14204 	int rc = 0;
14205 
14206 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14207 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
14208 		return;
14209 	}
14210 
14211 	switch (bp->fw_reset_state) {
14212 	case BNXT_FW_RESET_STATE_POLL_VF: {
14213 		int n = bnxt_get_registered_vfs(bp);
14214 		int tmo;
14215 
14216 		if (n < 0) {
14217 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
14218 				   n, jiffies_to_msecs(jiffies -
14219 				   bp->fw_reset_timestamp));
14220 			goto fw_reset_abort;
14221 		} else if (n > 0) {
14222 			if (bnxt_fw_reset_timeout(bp)) {
14223 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14224 				bp->fw_reset_state = 0;
14225 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
14226 					   n);
14227 				goto ulp_start;
14228 			}
14229 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14230 			return;
14231 		}
14232 		bp->fw_reset_timestamp = jiffies;
14233 		rtnl_lock();
14234 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
14235 			bnxt_fw_reset_abort(bp, rc);
14236 			rtnl_unlock();
14237 			goto ulp_start;
14238 		}
14239 		bnxt_fw_reset_close(bp);
14240 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14241 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14242 			tmo = HZ / 10;
14243 		} else {
14244 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14245 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14246 		}
14247 		rtnl_unlock();
14248 		bnxt_queue_fw_reset_work(bp, tmo);
14249 		return;
14250 	}
14251 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
14252 		u32 val;
14253 
14254 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14255 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
14256 		    !bnxt_fw_reset_timeout(bp)) {
14257 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14258 			return;
14259 		}
14260 
14261 		if (!bp->fw_health->primary) {
14262 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
14263 
14264 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14265 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14266 			return;
14267 		}
14268 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14269 	}
14270 		fallthrough;
14271 	case BNXT_FW_RESET_STATE_RESET_FW:
14272 		bnxt_reset_all(bp);
14273 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14274 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
14275 		return;
14276 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
14277 		bnxt_inv_fw_health_reg(bp);
14278 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
14279 		    !bp->fw_reset_min_dsecs) {
14280 			u16 val;
14281 
14282 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14283 			if (val == 0xffff) {
14284 				if (bnxt_fw_reset_timeout(bp)) {
14285 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
14286 					rc = -ETIMEDOUT;
14287 					goto fw_reset_abort;
14288 				}
14289 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
14290 				return;
14291 			}
14292 		}
14293 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14294 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
14295 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
14296 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
14297 			bnxt_dl_remote_reload(bp);
14298 		if (pci_enable_device(bp->pdev)) {
14299 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
14300 			rc = -ENODEV;
14301 			goto fw_reset_abort;
14302 		}
14303 		pci_set_master(bp->pdev);
14304 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
14305 		fallthrough;
14306 	case BNXT_FW_RESET_STATE_POLL_FW:
14307 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
14308 		rc = bnxt_hwrm_poll(bp);
14309 		if (rc) {
14310 			if (bnxt_fw_reset_timeout(bp)) {
14311 				netdev_err(bp->dev, "Firmware reset aborted\n");
14312 				goto fw_reset_abort_status;
14313 			}
14314 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14315 			return;
14316 		}
14317 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
14318 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
14319 		fallthrough;
14320 	case BNXT_FW_RESET_STATE_OPENING:
14321 		while (!rtnl_trylock()) {
14322 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14323 			return;
14324 		}
14325 		rc = bnxt_open(bp->dev);
14326 		if (rc) {
14327 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
14328 			bnxt_fw_reset_abort(bp, rc);
14329 			rtnl_unlock();
14330 			goto ulp_start;
14331 		}
14332 
14333 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
14334 		    bp->fw_health->enabled) {
14335 			bp->fw_health->last_fw_reset_cnt =
14336 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14337 		}
14338 		bp->fw_reset_state = 0;
14339 		/* Make sure fw_reset_state is 0 before clearing the flag */
14340 		smp_mb__before_atomic();
14341 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14342 		bnxt_ptp_reapply_pps(bp);
14343 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
14344 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
14345 			bnxt_dl_health_fw_recovery_done(bp);
14346 			bnxt_dl_health_fw_status_update(bp, true);
14347 		}
14348 		rtnl_unlock();
14349 		bnxt_ulp_start(bp, 0);
14350 		bnxt_reenable_sriov(bp);
14351 		rtnl_lock();
14352 		bnxt_vf_reps_alloc(bp);
14353 		bnxt_vf_reps_open(bp);
14354 		rtnl_unlock();
14355 		break;
14356 	}
14357 	return;
14358 
14359 fw_reset_abort_status:
14360 	if (bp->fw_health->status_reliable ||
14361 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
14362 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14363 
14364 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
14365 	}
14366 fw_reset_abort:
14367 	rtnl_lock();
14368 	bnxt_fw_reset_abort(bp, rc);
14369 	rtnl_unlock();
14370 ulp_start:
14371 	bnxt_ulp_start(bp, rc);
14372 }
14373 
14374 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
14375 {
14376 	int rc;
14377 	struct bnxt *bp = netdev_priv(dev);
14378 
14379 	SET_NETDEV_DEV(dev, &pdev->dev);
14380 
14381 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
14382 	rc = pci_enable_device(pdev);
14383 	if (rc) {
14384 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
14385 		goto init_err;
14386 	}
14387 
14388 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
14389 		dev_err(&pdev->dev,
14390 			"Cannot find PCI device base address, aborting\n");
14391 		rc = -ENODEV;
14392 		goto init_err_disable;
14393 	}
14394 
14395 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
14396 	if (rc) {
14397 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
14398 		goto init_err_disable;
14399 	}
14400 
14401 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
14402 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
14403 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
14404 		rc = -EIO;
14405 		goto init_err_release;
14406 	}
14407 
14408 	pci_set_master(pdev);
14409 
14410 	bp->dev = dev;
14411 	bp->pdev = pdev;
14412 
14413 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
14414 	 * determines the BAR size.
14415 	 */
14416 	bp->bar0 = pci_ioremap_bar(pdev, 0);
14417 	if (!bp->bar0) {
14418 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
14419 		rc = -ENOMEM;
14420 		goto init_err_release;
14421 	}
14422 
14423 	bp->bar2 = pci_ioremap_bar(pdev, 4);
14424 	if (!bp->bar2) {
14425 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
14426 		rc = -ENOMEM;
14427 		goto init_err_release;
14428 	}
14429 
14430 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
14431 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
14432 
14433 	spin_lock_init(&bp->ntp_fltr_lock);
14434 #if BITS_PER_LONG == 32
14435 	spin_lock_init(&bp->db_lock);
14436 #endif
14437 
14438 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
14439 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
14440 
14441 	timer_setup(&bp->timer, bnxt_timer, 0);
14442 	bp->current_interval = BNXT_TIMER_INTERVAL;
14443 
14444 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
14445 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
14446 
14447 	clear_bit(BNXT_STATE_OPEN, &bp->state);
14448 	return 0;
14449 
14450 init_err_release:
14451 	bnxt_unmap_bars(bp, pdev);
14452 	pci_release_regions(pdev);
14453 
14454 init_err_disable:
14455 	pci_disable_device(pdev);
14456 
14457 init_err:
14458 	return rc;
14459 }
14460 
14461 /* rtnl_lock held */
14462 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
14463 {
14464 	struct sockaddr *addr = p;
14465 	struct bnxt *bp = netdev_priv(dev);
14466 	int rc = 0;
14467 
14468 	if (!is_valid_ether_addr(addr->sa_data))
14469 		return -EADDRNOTAVAIL;
14470 
14471 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
14472 		return 0;
14473 
14474 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
14475 	if (rc)
14476 		return rc;
14477 
14478 	eth_hw_addr_set(dev, addr->sa_data);
14479 	bnxt_clear_usr_fltrs(bp, true);
14480 	if (netif_running(dev)) {
14481 		bnxt_close_nic(bp, false, false);
14482 		rc = bnxt_open_nic(bp, false, false);
14483 	}
14484 
14485 	return rc;
14486 }
14487 
14488 /* rtnl_lock held */
14489 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
14490 {
14491 	struct bnxt *bp = netdev_priv(dev);
14492 
14493 	if (netif_running(dev))
14494 		bnxt_close_nic(bp, true, false);
14495 
14496 	WRITE_ONCE(dev->mtu, new_mtu);
14497 	bnxt_set_ring_params(bp);
14498 
14499 	if (netif_running(dev))
14500 		return bnxt_open_nic(bp, true, false);
14501 
14502 	return 0;
14503 }
14504 
14505 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
14506 {
14507 	struct bnxt *bp = netdev_priv(dev);
14508 	bool sh = false;
14509 	int rc, tx_cp;
14510 
14511 	if (tc > bp->max_tc) {
14512 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
14513 			   tc, bp->max_tc);
14514 		return -EINVAL;
14515 	}
14516 
14517 	if (bp->num_tc == tc)
14518 		return 0;
14519 
14520 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
14521 		sh = true;
14522 
14523 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
14524 			      sh, tc, bp->tx_nr_rings_xdp);
14525 	if (rc)
14526 		return rc;
14527 
14528 	/* Needs to close the device and do hw resource re-allocations */
14529 	if (netif_running(bp->dev))
14530 		bnxt_close_nic(bp, true, false);
14531 
14532 	if (tc) {
14533 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
14534 		netdev_set_num_tc(dev, tc);
14535 		bp->num_tc = tc;
14536 	} else {
14537 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
14538 		netdev_reset_tc(dev);
14539 		bp->num_tc = 0;
14540 	}
14541 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
14542 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
14543 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
14544 			       tx_cp + bp->rx_nr_rings;
14545 
14546 	if (netif_running(bp->dev))
14547 		return bnxt_open_nic(bp, true, false);
14548 
14549 	return 0;
14550 }
14551 
14552 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
14553 				  void *cb_priv)
14554 {
14555 	struct bnxt *bp = cb_priv;
14556 
14557 	if (!bnxt_tc_flower_enabled(bp) ||
14558 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
14559 		return -EOPNOTSUPP;
14560 
14561 	switch (type) {
14562 	case TC_SETUP_CLSFLOWER:
14563 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
14564 	default:
14565 		return -EOPNOTSUPP;
14566 	}
14567 }
14568 
14569 LIST_HEAD(bnxt_block_cb_list);
14570 
14571 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
14572 			 void *type_data)
14573 {
14574 	struct bnxt *bp = netdev_priv(dev);
14575 
14576 	switch (type) {
14577 	case TC_SETUP_BLOCK:
14578 		return flow_block_cb_setup_simple(type_data,
14579 						  &bnxt_block_cb_list,
14580 						  bnxt_setup_tc_block_cb,
14581 						  bp, bp, true);
14582 	case TC_SETUP_QDISC_MQPRIO: {
14583 		struct tc_mqprio_qopt *mqprio = type_data;
14584 
14585 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
14586 
14587 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
14588 	}
14589 	default:
14590 		return -EOPNOTSUPP;
14591 	}
14592 }
14593 
14594 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
14595 			    const struct sk_buff *skb)
14596 {
14597 	struct bnxt_vnic_info *vnic;
14598 
14599 	if (skb)
14600 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
14601 
14602 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
14603 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
14604 }
14605 
14606 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
14607 			   u32 idx)
14608 {
14609 	struct hlist_head *head;
14610 	int bit_id;
14611 
14612 	spin_lock_bh(&bp->ntp_fltr_lock);
14613 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
14614 	if (bit_id < 0) {
14615 		spin_unlock_bh(&bp->ntp_fltr_lock);
14616 		return -ENOMEM;
14617 	}
14618 
14619 	fltr->base.sw_id = (u16)bit_id;
14620 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
14621 	fltr->base.flags |= BNXT_ACT_RING_DST;
14622 	head = &bp->ntp_fltr_hash_tbl[idx];
14623 	hlist_add_head_rcu(&fltr->base.hash, head);
14624 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
14625 	bnxt_insert_usr_fltr(bp, &fltr->base);
14626 	bp->ntp_fltr_count++;
14627 	spin_unlock_bh(&bp->ntp_fltr_lock);
14628 	return 0;
14629 }
14630 
14631 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
14632 			    struct bnxt_ntuple_filter *f2)
14633 {
14634 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
14635 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
14636 	struct flow_keys *keys1 = &f1->fkeys;
14637 	struct flow_keys *keys2 = &f2->fkeys;
14638 
14639 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
14640 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
14641 		return false;
14642 
14643 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
14644 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
14645 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
14646 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
14647 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
14648 			return false;
14649 	} else {
14650 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
14651 				     &keys2->addrs.v6addrs.src) ||
14652 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
14653 				     &masks2->addrs.v6addrs.src) ||
14654 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
14655 				     &keys2->addrs.v6addrs.dst) ||
14656 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
14657 				     &masks2->addrs.v6addrs.dst))
14658 			return false;
14659 	}
14660 
14661 	return keys1->ports.src == keys2->ports.src &&
14662 	       masks1->ports.src == masks2->ports.src &&
14663 	       keys1->ports.dst == keys2->ports.dst &&
14664 	       masks1->ports.dst == masks2->ports.dst &&
14665 	       keys1->control.flags == keys2->control.flags &&
14666 	       f1->l2_fltr == f2->l2_fltr;
14667 }
14668 
14669 struct bnxt_ntuple_filter *
14670 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
14671 				struct bnxt_ntuple_filter *fltr, u32 idx)
14672 {
14673 	struct bnxt_ntuple_filter *f;
14674 	struct hlist_head *head;
14675 
14676 	head = &bp->ntp_fltr_hash_tbl[idx];
14677 	hlist_for_each_entry_rcu(f, head, base.hash) {
14678 		if (bnxt_fltr_match(f, fltr))
14679 			return f;
14680 	}
14681 	return NULL;
14682 }
14683 
14684 #ifdef CONFIG_RFS_ACCEL
14685 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
14686 			      u16 rxq_index, u32 flow_id)
14687 {
14688 	struct bnxt *bp = netdev_priv(dev);
14689 	struct bnxt_ntuple_filter *fltr, *new_fltr;
14690 	struct flow_keys *fkeys;
14691 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
14692 	struct bnxt_l2_filter *l2_fltr;
14693 	int rc = 0, idx;
14694 	u32 flags;
14695 
14696 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
14697 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
14698 		atomic_inc(&l2_fltr->refcnt);
14699 	} else {
14700 		struct bnxt_l2_key key;
14701 
14702 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
14703 		key.vlan = 0;
14704 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
14705 		if (!l2_fltr)
14706 			return -EINVAL;
14707 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
14708 			bnxt_del_l2_filter(bp, l2_fltr);
14709 			return -EINVAL;
14710 		}
14711 	}
14712 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
14713 	if (!new_fltr) {
14714 		bnxt_del_l2_filter(bp, l2_fltr);
14715 		return -ENOMEM;
14716 	}
14717 
14718 	fkeys = &new_fltr->fkeys;
14719 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
14720 		rc = -EPROTONOSUPPORT;
14721 		goto err_free;
14722 	}
14723 
14724 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
14725 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
14726 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
14727 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
14728 		rc = -EPROTONOSUPPORT;
14729 		goto err_free;
14730 	}
14731 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
14732 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
14733 		if (bp->hwrm_spec_code < 0x10601) {
14734 			rc = -EPROTONOSUPPORT;
14735 			goto err_free;
14736 		}
14737 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
14738 	}
14739 	flags = fkeys->control.flags;
14740 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
14741 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
14742 		rc = -EPROTONOSUPPORT;
14743 		goto err_free;
14744 	}
14745 	new_fltr->l2_fltr = l2_fltr;
14746 
14747 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
14748 	rcu_read_lock();
14749 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
14750 	if (fltr) {
14751 		rc = fltr->base.sw_id;
14752 		rcu_read_unlock();
14753 		goto err_free;
14754 	}
14755 	rcu_read_unlock();
14756 
14757 	new_fltr->flow_id = flow_id;
14758 	new_fltr->base.rxq = rxq_index;
14759 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
14760 	if (!rc) {
14761 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14762 		return new_fltr->base.sw_id;
14763 	}
14764 
14765 err_free:
14766 	bnxt_del_l2_filter(bp, l2_fltr);
14767 	kfree(new_fltr);
14768 	return rc;
14769 }
14770 #endif
14771 
14772 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
14773 {
14774 	spin_lock_bh(&bp->ntp_fltr_lock);
14775 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
14776 		spin_unlock_bh(&bp->ntp_fltr_lock);
14777 		return;
14778 	}
14779 	hlist_del_rcu(&fltr->base.hash);
14780 	bnxt_del_one_usr_fltr(bp, &fltr->base);
14781 	bp->ntp_fltr_count--;
14782 	spin_unlock_bh(&bp->ntp_fltr_lock);
14783 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
14784 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
14785 	kfree_rcu(fltr, base.rcu);
14786 }
14787 
14788 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
14789 {
14790 #ifdef CONFIG_RFS_ACCEL
14791 	int i;
14792 
14793 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
14794 		struct hlist_head *head;
14795 		struct hlist_node *tmp;
14796 		struct bnxt_ntuple_filter *fltr;
14797 		int rc;
14798 
14799 		head = &bp->ntp_fltr_hash_tbl[i];
14800 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
14801 			bool del = false;
14802 
14803 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
14804 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
14805 					continue;
14806 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
14807 							fltr->flow_id,
14808 							fltr->base.sw_id)) {
14809 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
14810 									 fltr);
14811 					del = true;
14812 				}
14813 			} else {
14814 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
14815 								       fltr);
14816 				if (rc)
14817 					del = true;
14818 				else
14819 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
14820 			}
14821 
14822 			if (del)
14823 				bnxt_del_ntp_filter(bp, fltr);
14824 		}
14825 	}
14826 #endif
14827 }
14828 
14829 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
14830 				    unsigned int entry, struct udp_tunnel_info *ti)
14831 {
14832 	struct bnxt *bp = netdev_priv(netdev);
14833 	unsigned int cmd;
14834 
14835 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
14836 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
14837 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
14838 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
14839 	else
14840 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
14841 
14842 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
14843 }
14844 
14845 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
14846 				      unsigned int entry, struct udp_tunnel_info *ti)
14847 {
14848 	struct bnxt *bp = netdev_priv(netdev);
14849 	unsigned int cmd;
14850 
14851 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
14852 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
14853 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
14854 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
14855 	else
14856 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
14857 
14858 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
14859 }
14860 
14861 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
14862 	.set_port	= bnxt_udp_tunnel_set_port,
14863 	.unset_port	= bnxt_udp_tunnel_unset_port,
14864 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
14865 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
14866 	.tables		= {
14867 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
14868 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
14869 	},
14870 }, bnxt_udp_tunnels_p7 = {
14871 	.set_port	= bnxt_udp_tunnel_set_port,
14872 	.unset_port	= bnxt_udp_tunnel_unset_port,
14873 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
14874 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
14875 	.tables		= {
14876 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
14877 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
14878 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
14879 	},
14880 };
14881 
14882 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
14883 			       struct net_device *dev, u32 filter_mask,
14884 			       int nlflags)
14885 {
14886 	struct bnxt *bp = netdev_priv(dev);
14887 
14888 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
14889 				       nlflags, filter_mask, NULL);
14890 }
14891 
14892 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
14893 			       u16 flags, struct netlink_ext_ack *extack)
14894 {
14895 	struct bnxt *bp = netdev_priv(dev);
14896 	struct nlattr *attr, *br_spec;
14897 	int rem, rc = 0;
14898 
14899 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
14900 		return -EOPNOTSUPP;
14901 
14902 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
14903 	if (!br_spec)
14904 		return -EINVAL;
14905 
14906 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
14907 		u16 mode;
14908 
14909 		mode = nla_get_u16(attr);
14910 		if (mode == bp->br_mode)
14911 			break;
14912 
14913 		rc = bnxt_hwrm_set_br_mode(bp, mode);
14914 		if (!rc)
14915 			bp->br_mode = mode;
14916 		break;
14917 	}
14918 	return rc;
14919 }
14920 
14921 int bnxt_get_port_parent_id(struct net_device *dev,
14922 			    struct netdev_phys_item_id *ppid)
14923 {
14924 	struct bnxt *bp = netdev_priv(dev);
14925 
14926 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
14927 		return -EOPNOTSUPP;
14928 
14929 	/* The PF and it's VF-reps only support the switchdev framework */
14930 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
14931 		return -EOPNOTSUPP;
14932 
14933 	ppid->id_len = sizeof(bp->dsn);
14934 	memcpy(ppid->id, bp->dsn, ppid->id_len);
14935 
14936 	return 0;
14937 }
14938 
14939 static const struct net_device_ops bnxt_netdev_ops = {
14940 	.ndo_open		= bnxt_open,
14941 	.ndo_start_xmit		= bnxt_start_xmit,
14942 	.ndo_stop		= bnxt_close,
14943 	.ndo_get_stats64	= bnxt_get_stats64,
14944 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
14945 	.ndo_eth_ioctl		= bnxt_ioctl,
14946 	.ndo_validate_addr	= eth_validate_addr,
14947 	.ndo_set_mac_address	= bnxt_change_mac_addr,
14948 	.ndo_change_mtu		= bnxt_change_mtu,
14949 	.ndo_fix_features	= bnxt_fix_features,
14950 	.ndo_set_features	= bnxt_set_features,
14951 	.ndo_features_check	= bnxt_features_check,
14952 	.ndo_tx_timeout		= bnxt_tx_timeout,
14953 #ifdef CONFIG_BNXT_SRIOV
14954 	.ndo_get_vf_config	= bnxt_get_vf_config,
14955 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
14956 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
14957 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
14958 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
14959 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
14960 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
14961 #endif
14962 	.ndo_setup_tc           = bnxt_setup_tc,
14963 #ifdef CONFIG_RFS_ACCEL
14964 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
14965 #endif
14966 	.ndo_bpf		= bnxt_xdp,
14967 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
14968 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
14969 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
14970 };
14971 
14972 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
14973 				    struct netdev_queue_stats_rx *stats)
14974 {
14975 	struct bnxt *bp = netdev_priv(dev);
14976 	struct bnxt_cp_ring_info *cpr;
14977 	u64 *sw;
14978 
14979 	cpr = &bp->bnapi[i]->cp_ring;
14980 	sw = cpr->stats.sw_stats;
14981 
14982 	stats->packets = 0;
14983 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
14984 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
14985 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
14986 
14987 	stats->bytes = 0;
14988 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
14989 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
14990 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
14991 
14992 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
14993 }
14994 
14995 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
14996 				    struct netdev_queue_stats_tx *stats)
14997 {
14998 	struct bnxt *bp = netdev_priv(dev);
14999 	struct bnxt_napi *bnapi;
15000 	u64 *sw;
15001 
15002 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
15003 	sw = bnapi->cp_ring.stats.sw_stats;
15004 
15005 	stats->packets = 0;
15006 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
15007 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
15008 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
15009 
15010 	stats->bytes = 0;
15011 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
15012 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
15013 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
15014 }
15015 
15016 static void bnxt_get_base_stats(struct net_device *dev,
15017 				struct netdev_queue_stats_rx *rx,
15018 				struct netdev_queue_stats_tx *tx)
15019 {
15020 	struct bnxt *bp = netdev_priv(dev);
15021 
15022 	rx->packets = bp->net_stats_prev.rx_packets;
15023 	rx->bytes = bp->net_stats_prev.rx_bytes;
15024 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
15025 
15026 	tx->packets = bp->net_stats_prev.tx_packets;
15027 	tx->bytes = bp->net_stats_prev.tx_bytes;
15028 }
15029 
15030 static const struct netdev_stat_ops bnxt_stat_ops = {
15031 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
15032 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
15033 	.get_base_stats		= bnxt_get_base_stats,
15034 };
15035 
15036 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
15037 {
15038 	u16 mem_size;
15039 
15040 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
15041 	mem_size = rxr->rx_agg_bmap_size / 8;
15042 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
15043 	if (!rxr->rx_agg_bmap)
15044 		return -ENOMEM;
15045 
15046 	return 0;
15047 }
15048 
15049 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx)
15050 {
15051 	struct bnxt_rx_ring_info *rxr, *clone;
15052 	struct bnxt *bp = netdev_priv(dev);
15053 	struct bnxt_ring_struct *ring;
15054 	int rc;
15055 
15056 	rxr = &bp->rx_ring[idx];
15057 	clone = qmem;
15058 	memcpy(clone, rxr, sizeof(*rxr));
15059 	bnxt_init_rx_ring_struct(bp, clone);
15060 	bnxt_reset_rx_ring_struct(bp, clone);
15061 
15062 	clone->rx_prod = 0;
15063 	clone->rx_agg_prod = 0;
15064 	clone->rx_sw_agg_prod = 0;
15065 	clone->rx_next_cons = 0;
15066 
15067 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
15068 	if (rc)
15069 		return rc;
15070 
15071 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
15072 	if (rc < 0)
15073 		goto err_page_pool_destroy;
15074 
15075 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
15076 					MEM_TYPE_PAGE_POOL,
15077 					clone->page_pool);
15078 	if (rc)
15079 		goto err_rxq_info_unreg;
15080 
15081 	ring = &clone->rx_ring_struct;
15082 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15083 	if (rc)
15084 		goto err_free_rx_ring;
15085 
15086 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
15087 		ring = &clone->rx_agg_ring_struct;
15088 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15089 		if (rc)
15090 			goto err_free_rx_agg_ring;
15091 
15092 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
15093 		if (rc)
15094 			goto err_free_rx_agg_ring;
15095 	}
15096 
15097 	bnxt_init_one_rx_ring_rxbd(bp, clone);
15098 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
15099 
15100 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
15101 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15102 		bnxt_alloc_one_rx_ring_page(bp, clone, idx);
15103 
15104 	return 0;
15105 
15106 err_free_rx_agg_ring:
15107 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
15108 err_free_rx_ring:
15109 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
15110 err_rxq_info_unreg:
15111 	xdp_rxq_info_unreg(&clone->xdp_rxq);
15112 err_page_pool_destroy:
15113 	clone->page_pool->p.napi = NULL;
15114 	page_pool_destroy(clone->page_pool);
15115 	clone->page_pool = NULL;
15116 	return rc;
15117 }
15118 
15119 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
15120 {
15121 	struct bnxt_rx_ring_info *rxr = qmem;
15122 	struct bnxt *bp = netdev_priv(dev);
15123 	struct bnxt_ring_struct *ring;
15124 
15125 	bnxt_free_one_rx_ring(bp, rxr);
15126 	bnxt_free_one_rx_agg_ring(bp, rxr);
15127 
15128 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
15129 
15130 	page_pool_destroy(rxr->page_pool);
15131 	rxr->page_pool = NULL;
15132 
15133 	ring = &rxr->rx_ring_struct;
15134 	bnxt_free_ring(bp, &ring->ring_mem);
15135 
15136 	ring = &rxr->rx_agg_ring_struct;
15137 	bnxt_free_ring(bp, &ring->ring_mem);
15138 
15139 	kfree(rxr->rx_agg_bmap);
15140 	rxr->rx_agg_bmap = NULL;
15141 }
15142 
15143 static void bnxt_copy_rx_ring(struct bnxt *bp,
15144 			      struct bnxt_rx_ring_info *dst,
15145 			      struct bnxt_rx_ring_info *src)
15146 {
15147 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
15148 	struct bnxt_ring_struct *dst_ring, *src_ring;
15149 	int i;
15150 
15151 	dst_ring = &dst->rx_ring_struct;
15152 	dst_rmem = &dst_ring->ring_mem;
15153 	src_ring = &src->rx_ring_struct;
15154 	src_rmem = &src_ring->ring_mem;
15155 
15156 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15157 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15158 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15159 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15160 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15161 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15162 
15163 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15164 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15165 	*dst_rmem->vmem = *src_rmem->vmem;
15166 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15167 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15168 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15169 	}
15170 
15171 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
15172 		return;
15173 
15174 	dst_ring = &dst->rx_agg_ring_struct;
15175 	dst_rmem = &dst_ring->ring_mem;
15176 	src_ring = &src->rx_agg_ring_struct;
15177 	src_rmem = &src_ring->ring_mem;
15178 
15179 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15180 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15181 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15182 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15183 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15184 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15185 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
15186 
15187 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15188 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15189 	*dst_rmem->vmem = *src_rmem->vmem;
15190 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15191 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15192 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15193 	}
15194 
15195 	dst->rx_agg_bmap = src->rx_agg_bmap;
15196 }
15197 
15198 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx)
15199 {
15200 	struct bnxt *bp = netdev_priv(dev);
15201 	struct bnxt_rx_ring_info *rxr, *clone;
15202 	struct bnxt_cp_ring_info *cpr;
15203 	struct bnxt_vnic_info *vnic;
15204 	int i, rc;
15205 
15206 	rxr = &bp->rx_ring[idx];
15207 	clone = qmem;
15208 
15209 	rxr->rx_prod = clone->rx_prod;
15210 	rxr->rx_agg_prod = clone->rx_agg_prod;
15211 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
15212 	rxr->rx_next_cons = clone->rx_next_cons;
15213 	rxr->page_pool = clone->page_pool;
15214 	rxr->xdp_rxq = clone->xdp_rxq;
15215 
15216 	bnxt_copy_rx_ring(bp, rxr, clone);
15217 
15218 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
15219 	if (rc)
15220 		return rc;
15221 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
15222 	if (rc)
15223 		goto err_free_hwrm_rx_ring;
15224 
15225 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
15226 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15227 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
15228 
15229 	cpr = &rxr->bnapi->cp_ring;
15230 	cpr->sw_stats->rx.rx_resets++;
15231 
15232 	for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) {
15233 		vnic = &bp->vnic_info[i];
15234 		vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
15235 		bnxt_hwrm_vnic_update(bp, vnic,
15236 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15237 	}
15238 
15239 	return 0;
15240 
15241 err_free_hwrm_rx_ring:
15242 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15243 	return rc;
15244 }
15245 
15246 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
15247 {
15248 	struct bnxt *bp = netdev_priv(dev);
15249 	struct bnxt_rx_ring_info *rxr;
15250 	struct bnxt_vnic_info *vnic;
15251 	int i;
15252 
15253 	for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) {
15254 		vnic = &bp->vnic_info[i];
15255 		vnic->mru = 0;
15256 		bnxt_hwrm_vnic_update(bp, vnic,
15257 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15258 	}
15259 
15260 	rxr = &bp->rx_ring[idx];
15261 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15262 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
15263 	rxr->rx_next_cons = 0;
15264 	page_pool_disable_direct_recycling(rxr->page_pool);
15265 
15266 	memcpy(qmem, rxr, sizeof(*rxr));
15267 	bnxt_init_rx_ring_struct(bp, qmem);
15268 
15269 	return 0;
15270 }
15271 
15272 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
15273 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
15274 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
15275 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
15276 	.ndo_queue_start	= bnxt_queue_start,
15277 	.ndo_queue_stop		= bnxt_queue_stop,
15278 };
15279 
15280 static void bnxt_remove_one(struct pci_dev *pdev)
15281 {
15282 	struct net_device *dev = pci_get_drvdata(pdev);
15283 	struct bnxt *bp = netdev_priv(dev);
15284 
15285 	if (BNXT_PF(bp))
15286 		bnxt_sriov_disable(bp);
15287 
15288 	bnxt_rdma_aux_device_del(bp);
15289 
15290 	bnxt_ptp_clear(bp);
15291 	unregister_netdev(dev);
15292 
15293 	bnxt_rdma_aux_device_uninit(bp);
15294 
15295 	bnxt_free_l2_filters(bp, true);
15296 	bnxt_free_ntp_fltrs(bp, true);
15297 	WARN_ON(bp->num_rss_ctx);
15298 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15299 	/* Flush any pending tasks */
15300 	cancel_work_sync(&bp->sp_task);
15301 	cancel_delayed_work_sync(&bp->fw_reset_task);
15302 	bp->sp_event = 0;
15303 
15304 	bnxt_dl_fw_reporters_destroy(bp);
15305 	bnxt_dl_unregister(bp);
15306 	bnxt_shutdown_tc(bp);
15307 
15308 	bnxt_clear_int_mode(bp);
15309 	bnxt_hwrm_func_drv_unrgtr(bp);
15310 	bnxt_free_hwrm_resources(bp);
15311 	bnxt_hwmon_uninit(bp);
15312 	bnxt_ethtool_free(bp);
15313 	bnxt_dcb_free(bp);
15314 	kfree(bp->ptp_cfg);
15315 	bp->ptp_cfg = NULL;
15316 	kfree(bp->fw_health);
15317 	bp->fw_health = NULL;
15318 	bnxt_cleanup_pci(bp);
15319 	bnxt_free_ctx_mem(bp);
15320 	bnxt_free_crash_dump_mem(bp);
15321 	kfree(bp->rss_indir_tbl);
15322 	bp->rss_indir_tbl = NULL;
15323 	bnxt_free_port_stats(bp);
15324 	free_netdev(dev);
15325 }
15326 
15327 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
15328 {
15329 	int rc = 0;
15330 	struct bnxt_link_info *link_info = &bp->link_info;
15331 
15332 	bp->phy_flags = 0;
15333 	rc = bnxt_hwrm_phy_qcaps(bp);
15334 	if (rc) {
15335 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
15336 			   rc);
15337 		return rc;
15338 	}
15339 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
15340 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
15341 	else
15342 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
15343 	if (!fw_dflt)
15344 		return 0;
15345 
15346 	mutex_lock(&bp->link_lock);
15347 	rc = bnxt_update_link(bp, false);
15348 	if (rc) {
15349 		mutex_unlock(&bp->link_lock);
15350 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
15351 			   rc);
15352 		return rc;
15353 	}
15354 
15355 	/* Older firmware does not have supported_auto_speeds, so assume
15356 	 * that all supported speeds can be autonegotiated.
15357 	 */
15358 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
15359 		link_info->support_auto_speeds = link_info->support_speeds;
15360 
15361 	bnxt_init_ethtool_link_settings(bp);
15362 	mutex_unlock(&bp->link_lock);
15363 	return 0;
15364 }
15365 
15366 static int bnxt_get_max_irq(struct pci_dev *pdev)
15367 {
15368 	u16 ctrl;
15369 
15370 	if (!pdev->msix_cap)
15371 		return 1;
15372 
15373 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
15374 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
15375 }
15376 
15377 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15378 				int *max_cp)
15379 {
15380 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
15381 	int max_ring_grps = 0, max_irq;
15382 
15383 	*max_tx = hw_resc->max_tx_rings;
15384 	*max_rx = hw_resc->max_rx_rings;
15385 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
15386 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
15387 			bnxt_get_ulp_msix_num_in_use(bp),
15388 			hw_resc->max_stat_ctxs -
15389 			bnxt_get_ulp_stat_ctxs_in_use(bp));
15390 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
15391 		*max_cp = min_t(int, *max_cp, max_irq);
15392 	max_ring_grps = hw_resc->max_hw_ring_grps;
15393 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
15394 		*max_cp -= 1;
15395 		*max_rx -= 2;
15396 	}
15397 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15398 		*max_rx >>= 1;
15399 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
15400 		int rc;
15401 
15402 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
15403 		if (rc) {
15404 			*max_rx = 0;
15405 			*max_tx = 0;
15406 		}
15407 		/* On P5 chips, max_cp output param should be available NQs */
15408 		*max_cp = max_irq;
15409 	}
15410 	*max_rx = min_t(int, *max_rx, max_ring_grps);
15411 }
15412 
15413 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
15414 {
15415 	int rx, tx, cp;
15416 
15417 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
15418 	*max_rx = rx;
15419 	*max_tx = tx;
15420 	if (!rx || !tx || !cp)
15421 		return -ENOMEM;
15422 
15423 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
15424 }
15425 
15426 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15427 			       bool shared)
15428 {
15429 	int rc;
15430 
15431 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
15432 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
15433 		/* Not enough rings, try disabling agg rings. */
15434 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
15435 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
15436 		if (rc) {
15437 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
15438 			bp->flags |= BNXT_FLAG_AGG_RINGS;
15439 			return rc;
15440 		}
15441 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
15442 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
15443 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
15444 		bnxt_set_ring_params(bp);
15445 	}
15446 
15447 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
15448 		int max_cp, max_stat, max_irq;
15449 
15450 		/* Reserve minimum resources for RoCE */
15451 		max_cp = bnxt_get_max_func_cp_rings(bp);
15452 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
15453 		max_irq = bnxt_get_max_func_irqs(bp);
15454 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
15455 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
15456 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
15457 			return 0;
15458 
15459 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
15460 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
15461 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
15462 		max_cp = min_t(int, max_cp, max_irq);
15463 		max_cp = min_t(int, max_cp, max_stat);
15464 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
15465 		if (rc)
15466 			rc = 0;
15467 	}
15468 	return rc;
15469 }
15470 
15471 /* In initial default shared ring setting, each shared ring must have a
15472  * RX/TX ring pair.
15473  */
15474 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
15475 {
15476 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
15477 	bp->rx_nr_rings = bp->cp_nr_rings;
15478 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
15479 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15480 }
15481 
15482 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
15483 {
15484 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
15485 	int avail_msix;
15486 
15487 	if (!bnxt_can_reserve_rings(bp))
15488 		return 0;
15489 
15490 	if (sh)
15491 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
15492 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
15493 	/* Reduce default rings on multi-port cards so that total default
15494 	 * rings do not exceed CPU count.
15495 	 */
15496 	if (bp->port_count > 1) {
15497 		int max_rings =
15498 			max_t(int, num_online_cpus() / bp->port_count, 1);
15499 
15500 		dflt_rings = min_t(int, dflt_rings, max_rings);
15501 	}
15502 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
15503 	if (rc)
15504 		return rc;
15505 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
15506 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
15507 	if (sh)
15508 		bnxt_trim_dflt_sh_rings(bp);
15509 	else
15510 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
15511 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15512 
15513 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
15514 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
15515 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
15516 
15517 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
15518 		bnxt_set_dflt_ulp_stat_ctxs(bp);
15519 	}
15520 
15521 	rc = __bnxt_reserve_rings(bp);
15522 	if (rc && rc != -ENODEV)
15523 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
15524 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15525 	if (sh)
15526 		bnxt_trim_dflt_sh_rings(bp);
15527 
15528 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
15529 	if (bnxt_need_reserve_rings(bp)) {
15530 		rc = __bnxt_reserve_rings(bp);
15531 		if (rc && rc != -ENODEV)
15532 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
15533 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15534 	}
15535 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
15536 		bp->rx_nr_rings++;
15537 		bp->cp_nr_rings++;
15538 	}
15539 	if (rc) {
15540 		bp->tx_nr_rings = 0;
15541 		bp->rx_nr_rings = 0;
15542 	}
15543 	return rc;
15544 }
15545 
15546 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
15547 {
15548 	int rc;
15549 
15550 	if (bp->tx_nr_rings)
15551 		return 0;
15552 
15553 	bnxt_ulp_irq_stop(bp);
15554 	bnxt_clear_int_mode(bp);
15555 	rc = bnxt_set_dflt_rings(bp, true);
15556 	if (rc) {
15557 		if (BNXT_VF(bp) && rc == -ENODEV)
15558 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
15559 		else
15560 			netdev_err(bp->dev, "Not enough rings available.\n");
15561 		goto init_dflt_ring_err;
15562 	}
15563 	rc = bnxt_init_int_mode(bp);
15564 	if (rc)
15565 		goto init_dflt_ring_err;
15566 
15567 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15568 
15569 	bnxt_set_dflt_rfs(bp);
15570 
15571 init_dflt_ring_err:
15572 	bnxt_ulp_irq_restart(bp, rc);
15573 	return rc;
15574 }
15575 
15576 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
15577 {
15578 	int rc;
15579 
15580 	ASSERT_RTNL();
15581 	bnxt_hwrm_func_qcaps(bp);
15582 
15583 	if (netif_running(bp->dev))
15584 		__bnxt_close_nic(bp, true, false);
15585 
15586 	bnxt_ulp_irq_stop(bp);
15587 	bnxt_clear_int_mode(bp);
15588 	rc = bnxt_init_int_mode(bp);
15589 	bnxt_ulp_irq_restart(bp, rc);
15590 
15591 	if (netif_running(bp->dev)) {
15592 		if (rc)
15593 			dev_close(bp->dev);
15594 		else
15595 			rc = bnxt_open_nic(bp, true, false);
15596 	}
15597 
15598 	return rc;
15599 }
15600 
15601 static int bnxt_init_mac_addr(struct bnxt *bp)
15602 {
15603 	int rc = 0;
15604 
15605 	if (BNXT_PF(bp)) {
15606 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
15607 	} else {
15608 #ifdef CONFIG_BNXT_SRIOV
15609 		struct bnxt_vf_info *vf = &bp->vf;
15610 		bool strict_approval = true;
15611 
15612 		if (is_valid_ether_addr(vf->mac_addr)) {
15613 			/* overwrite netdev dev_addr with admin VF MAC */
15614 			eth_hw_addr_set(bp->dev, vf->mac_addr);
15615 			/* Older PF driver or firmware may not approve this
15616 			 * correctly.
15617 			 */
15618 			strict_approval = false;
15619 		} else {
15620 			eth_hw_addr_random(bp->dev);
15621 		}
15622 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
15623 #endif
15624 	}
15625 	return rc;
15626 }
15627 
15628 static void bnxt_vpd_read_info(struct bnxt *bp)
15629 {
15630 	struct pci_dev *pdev = bp->pdev;
15631 	unsigned int vpd_size, kw_len;
15632 	int pos, size;
15633 	u8 *vpd_data;
15634 
15635 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
15636 	if (IS_ERR(vpd_data)) {
15637 		pci_warn(pdev, "Unable to read VPD\n");
15638 		return;
15639 	}
15640 
15641 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
15642 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
15643 	if (pos < 0)
15644 		goto read_sn;
15645 
15646 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
15647 	memcpy(bp->board_partno, &vpd_data[pos], size);
15648 
15649 read_sn:
15650 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
15651 					   PCI_VPD_RO_KEYWORD_SERIALNO,
15652 					   &kw_len);
15653 	if (pos < 0)
15654 		goto exit;
15655 
15656 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
15657 	memcpy(bp->board_serialno, &vpd_data[pos], size);
15658 exit:
15659 	kfree(vpd_data);
15660 }
15661 
15662 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
15663 {
15664 	struct pci_dev *pdev = bp->pdev;
15665 	u64 qword;
15666 
15667 	qword = pci_get_dsn(pdev);
15668 	if (!qword) {
15669 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
15670 		return -EOPNOTSUPP;
15671 	}
15672 
15673 	put_unaligned_le64(qword, dsn);
15674 
15675 	bp->flags |= BNXT_FLAG_DSN_VALID;
15676 	return 0;
15677 }
15678 
15679 static int bnxt_map_db_bar(struct bnxt *bp)
15680 {
15681 	if (!bp->db_size)
15682 		return -ENODEV;
15683 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
15684 	if (!bp->bar1)
15685 		return -ENOMEM;
15686 	return 0;
15687 }
15688 
15689 void bnxt_print_device_info(struct bnxt *bp)
15690 {
15691 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
15692 		    board_info[bp->board_idx].name,
15693 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
15694 
15695 	pcie_print_link_status(bp->pdev);
15696 }
15697 
15698 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
15699 {
15700 	struct bnxt_hw_resc *hw_resc;
15701 	struct net_device *dev;
15702 	struct bnxt *bp;
15703 	int rc, max_irqs;
15704 
15705 	if (pci_is_bridge(pdev))
15706 		return -ENODEV;
15707 
15708 	if (!pdev->msix_cap) {
15709 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
15710 		return -ENODEV;
15711 	}
15712 
15713 	/* Clear any pending DMA transactions from crash kernel
15714 	 * while loading driver in capture kernel.
15715 	 */
15716 	if (is_kdump_kernel()) {
15717 		pci_clear_master(pdev);
15718 		pcie_flr(pdev);
15719 	}
15720 
15721 	max_irqs = bnxt_get_max_irq(pdev);
15722 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
15723 				 max_irqs);
15724 	if (!dev)
15725 		return -ENOMEM;
15726 
15727 	bp = netdev_priv(dev);
15728 	bp->board_idx = ent->driver_data;
15729 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
15730 	bnxt_set_max_func_irqs(bp, max_irqs);
15731 
15732 	if (bnxt_vf_pciid(bp->board_idx))
15733 		bp->flags |= BNXT_FLAG_VF;
15734 
15735 	/* No devlink port registration in case of a VF */
15736 	if (BNXT_PF(bp))
15737 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
15738 
15739 	rc = bnxt_init_board(pdev, dev);
15740 	if (rc < 0)
15741 		goto init_err_free;
15742 
15743 	dev->netdev_ops = &bnxt_netdev_ops;
15744 	dev->stat_ops = &bnxt_stat_ops;
15745 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
15746 	dev->ethtool_ops = &bnxt_ethtool_ops;
15747 	pci_set_drvdata(pdev, dev);
15748 
15749 	rc = bnxt_alloc_hwrm_resources(bp);
15750 	if (rc)
15751 		goto init_err_pci_clean;
15752 
15753 	mutex_init(&bp->hwrm_cmd_lock);
15754 	mutex_init(&bp->link_lock);
15755 
15756 	rc = bnxt_fw_init_one_p1(bp);
15757 	if (rc)
15758 		goto init_err_pci_clean;
15759 
15760 	if (BNXT_PF(bp))
15761 		bnxt_vpd_read_info(bp);
15762 
15763 	if (BNXT_CHIP_P5_PLUS(bp)) {
15764 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
15765 		if (BNXT_CHIP_P7(bp))
15766 			bp->flags |= BNXT_FLAG_CHIP_P7;
15767 	}
15768 
15769 	rc = bnxt_alloc_rss_indir_tbl(bp);
15770 	if (rc)
15771 		goto init_err_pci_clean;
15772 
15773 	rc = bnxt_fw_init_one_p2(bp);
15774 	if (rc)
15775 		goto init_err_pci_clean;
15776 
15777 	rc = bnxt_map_db_bar(bp);
15778 	if (rc) {
15779 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
15780 			rc);
15781 		goto init_err_pci_clean;
15782 	}
15783 
15784 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
15785 			   NETIF_F_TSO | NETIF_F_TSO6 |
15786 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
15787 			   NETIF_F_GSO_IPXIP4 |
15788 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
15789 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
15790 			   NETIF_F_RXCSUM | NETIF_F_GRO;
15791 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
15792 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
15793 
15794 	if (BNXT_SUPPORTS_TPA(bp))
15795 		dev->hw_features |= NETIF_F_LRO;
15796 
15797 	dev->hw_enc_features =
15798 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
15799 			NETIF_F_TSO | NETIF_F_TSO6 |
15800 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
15801 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
15802 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
15803 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
15804 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
15805 	if (bp->flags & BNXT_FLAG_CHIP_P7)
15806 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
15807 	else
15808 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
15809 
15810 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
15811 				    NETIF_F_GSO_GRE_CSUM;
15812 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
15813 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
15814 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
15815 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
15816 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
15817 	if (BNXT_SUPPORTS_TPA(bp))
15818 		dev->hw_features |= NETIF_F_GRO_HW;
15819 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
15820 	if (dev->features & NETIF_F_GRO_HW)
15821 		dev->features &= ~NETIF_F_LRO;
15822 	dev->priv_flags |= IFF_UNICAST_FLT;
15823 
15824 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
15825 	if (bp->tso_max_segs)
15826 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
15827 
15828 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
15829 			    NETDEV_XDP_ACT_RX_SG;
15830 
15831 #ifdef CONFIG_BNXT_SRIOV
15832 	init_waitqueue_head(&bp->sriov_cfg_wait);
15833 #endif
15834 	if (BNXT_SUPPORTS_TPA(bp)) {
15835 		bp->gro_func = bnxt_gro_func_5730x;
15836 		if (BNXT_CHIP_P4(bp))
15837 			bp->gro_func = bnxt_gro_func_5731x;
15838 		else if (BNXT_CHIP_P5_PLUS(bp))
15839 			bp->gro_func = bnxt_gro_func_5750x;
15840 	}
15841 	if (!BNXT_CHIP_P4_PLUS(bp))
15842 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
15843 
15844 	rc = bnxt_init_mac_addr(bp);
15845 	if (rc) {
15846 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
15847 		rc = -EADDRNOTAVAIL;
15848 		goto init_err_pci_clean;
15849 	}
15850 
15851 	if (BNXT_PF(bp)) {
15852 		/* Read the adapter's DSN to use as the eswitch switch_id */
15853 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
15854 	}
15855 
15856 	/* MTU range: 60 - FW defined max */
15857 	dev->min_mtu = ETH_ZLEN;
15858 	dev->max_mtu = bp->max_mtu;
15859 
15860 	rc = bnxt_probe_phy(bp, true);
15861 	if (rc)
15862 		goto init_err_pci_clean;
15863 
15864 	hw_resc = &bp->hw_resc;
15865 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
15866 		       BNXT_L2_FLTR_MAX_FLTR;
15867 	/* Older firmware may not report these filters properly */
15868 	if (bp->max_fltr < BNXT_MAX_FLTR)
15869 		bp->max_fltr = BNXT_MAX_FLTR;
15870 	bnxt_init_l2_fltr_tbl(bp);
15871 	bnxt_set_rx_skb_mode(bp, false);
15872 	bnxt_set_tpa_flags(bp);
15873 	bnxt_set_ring_params(bp);
15874 	bnxt_rdma_aux_device_init(bp);
15875 	rc = bnxt_set_dflt_rings(bp, true);
15876 	if (rc) {
15877 		if (BNXT_VF(bp) && rc == -ENODEV) {
15878 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
15879 		} else {
15880 			netdev_err(bp->dev, "Not enough rings available.\n");
15881 			rc = -ENOMEM;
15882 		}
15883 		goto init_err_pci_clean;
15884 	}
15885 
15886 	bnxt_fw_init_one_p3(bp);
15887 
15888 	bnxt_init_dflt_coal(bp);
15889 
15890 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
15891 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
15892 
15893 	rc = bnxt_init_int_mode(bp);
15894 	if (rc)
15895 		goto init_err_pci_clean;
15896 
15897 	/* No TC has been set yet and rings may have been trimmed due to
15898 	 * limited MSIX, so we re-initialize the TX rings per TC.
15899 	 */
15900 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15901 
15902 	if (BNXT_PF(bp)) {
15903 		if (!bnxt_pf_wq) {
15904 			bnxt_pf_wq =
15905 				create_singlethread_workqueue("bnxt_pf_wq");
15906 			if (!bnxt_pf_wq) {
15907 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
15908 				rc = -ENOMEM;
15909 				goto init_err_pci_clean;
15910 			}
15911 		}
15912 		rc = bnxt_init_tc(bp);
15913 		if (rc)
15914 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
15915 				   rc);
15916 	}
15917 
15918 	bnxt_inv_fw_health_reg(bp);
15919 	rc = bnxt_dl_register(bp);
15920 	if (rc)
15921 		goto init_err_dl;
15922 
15923 	INIT_LIST_HEAD(&bp->usr_fltr_list);
15924 
15925 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
15926 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
15927 	if (BNXT_SUPPORTS_QUEUE_API(bp))
15928 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
15929 
15930 	rc = register_netdev(dev);
15931 	if (rc)
15932 		goto init_err_cleanup;
15933 
15934 	bnxt_dl_fw_reporters_create(bp);
15935 
15936 	bnxt_rdma_aux_device_add(bp);
15937 
15938 	bnxt_print_device_info(bp);
15939 
15940 	pci_save_state(pdev);
15941 
15942 	return 0;
15943 init_err_cleanup:
15944 	bnxt_rdma_aux_device_uninit(bp);
15945 	bnxt_dl_unregister(bp);
15946 init_err_dl:
15947 	bnxt_shutdown_tc(bp);
15948 	bnxt_clear_int_mode(bp);
15949 
15950 init_err_pci_clean:
15951 	bnxt_hwrm_func_drv_unrgtr(bp);
15952 	bnxt_free_hwrm_resources(bp);
15953 	bnxt_hwmon_uninit(bp);
15954 	bnxt_ethtool_free(bp);
15955 	bnxt_ptp_clear(bp);
15956 	kfree(bp->ptp_cfg);
15957 	bp->ptp_cfg = NULL;
15958 	kfree(bp->fw_health);
15959 	bp->fw_health = NULL;
15960 	bnxt_cleanup_pci(bp);
15961 	bnxt_free_ctx_mem(bp);
15962 	bnxt_free_crash_dump_mem(bp);
15963 	kfree(bp->rss_indir_tbl);
15964 	bp->rss_indir_tbl = NULL;
15965 
15966 init_err_free:
15967 	free_netdev(dev);
15968 	return rc;
15969 }
15970 
15971 static void bnxt_shutdown(struct pci_dev *pdev)
15972 {
15973 	struct net_device *dev = pci_get_drvdata(pdev);
15974 	struct bnxt *bp;
15975 
15976 	if (!dev)
15977 		return;
15978 
15979 	rtnl_lock();
15980 	bp = netdev_priv(dev);
15981 	if (!bp)
15982 		goto shutdown_exit;
15983 
15984 	if (netif_running(dev))
15985 		dev_close(dev);
15986 
15987 	bnxt_clear_int_mode(bp);
15988 	pci_disable_device(pdev);
15989 
15990 	if (system_state == SYSTEM_POWER_OFF) {
15991 		pci_wake_from_d3(pdev, bp->wol);
15992 		pci_set_power_state(pdev, PCI_D3hot);
15993 	}
15994 
15995 shutdown_exit:
15996 	rtnl_unlock();
15997 }
15998 
15999 #ifdef CONFIG_PM_SLEEP
16000 static int bnxt_suspend(struct device *device)
16001 {
16002 	struct net_device *dev = dev_get_drvdata(device);
16003 	struct bnxt *bp = netdev_priv(dev);
16004 	int rc = 0;
16005 
16006 	bnxt_ulp_stop(bp);
16007 
16008 	rtnl_lock();
16009 	if (netif_running(dev)) {
16010 		netif_device_detach(dev);
16011 		rc = bnxt_close(dev);
16012 	}
16013 	bnxt_hwrm_func_drv_unrgtr(bp);
16014 	pci_disable_device(bp->pdev);
16015 	bnxt_free_ctx_mem(bp);
16016 	rtnl_unlock();
16017 	return rc;
16018 }
16019 
16020 static int bnxt_resume(struct device *device)
16021 {
16022 	struct net_device *dev = dev_get_drvdata(device);
16023 	struct bnxt *bp = netdev_priv(dev);
16024 	int rc = 0;
16025 
16026 	rtnl_lock();
16027 	rc = pci_enable_device(bp->pdev);
16028 	if (rc) {
16029 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
16030 			   rc);
16031 		goto resume_exit;
16032 	}
16033 	pci_set_master(bp->pdev);
16034 	if (bnxt_hwrm_ver_get(bp)) {
16035 		rc = -ENODEV;
16036 		goto resume_exit;
16037 	}
16038 	rc = bnxt_hwrm_func_reset(bp);
16039 	if (rc) {
16040 		rc = -EBUSY;
16041 		goto resume_exit;
16042 	}
16043 
16044 	rc = bnxt_hwrm_func_qcaps(bp);
16045 	if (rc)
16046 		goto resume_exit;
16047 
16048 	bnxt_clear_reservations(bp, true);
16049 
16050 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
16051 		rc = -ENODEV;
16052 		goto resume_exit;
16053 	}
16054 	if (bp->fw_crash_mem)
16055 		bnxt_hwrm_crash_dump_mem_cfg(bp);
16056 
16057 	bnxt_get_wol_settings(bp);
16058 	if (netif_running(dev)) {
16059 		rc = bnxt_open(dev);
16060 		if (!rc)
16061 			netif_device_attach(dev);
16062 	}
16063 
16064 resume_exit:
16065 	rtnl_unlock();
16066 	bnxt_ulp_start(bp, rc);
16067 	if (!rc)
16068 		bnxt_reenable_sriov(bp);
16069 	return rc;
16070 }
16071 
16072 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
16073 #define BNXT_PM_OPS (&bnxt_pm_ops)
16074 
16075 #else
16076 
16077 #define BNXT_PM_OPS NULL
16078 
16079 #endif /* CONFIG_PM_SLEEP */
16080 
16081 /**
16082  * bnxt_io_error_detected - called when PCI error is detected
16083  * @pdev: Pointer to PCI device
16084  * @state: The current pci connection state
16085  *
16086  * This function is called after a PCI bus error affecting
16087  * this device has been detected.
16088  */
16089 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
16090 					       pci_channel_state_t state)
16091 {
16092 	struct net_device *netdev = pci_get_drvdata(pdev);
16093 	struct bnxt *bp = netdev_priv(netdev);
16094 	bool abort = false;
16095 
16096 	netdev_info(netdev, "PCI I/O error detected\n");
16097 
16098 	bnxt_ulp_stop(bp);
16099 
16100 	rtnl_lock();
16101 	netif_device_detach(netdev);
16102 
16103 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
16104 		netdev_err(bp->dev, "Firmware reset already in progress\n");
16105 		abort = true;
16106 	}
16107 
16108 	if (abort || state == pci_channel_io_perm_failure) {
16109 		rtnl_unlock();
16110 		return PCI_ERS_RESULT_DISCONNECT;
16111 	}
16112 
16113 	/* Link is not reliable anymore if state is pci_channel_io_frozen
16114 	 * so we disable bus master to prevent any potential bad DMAs before
16115 	 * freeing kernel memory.
16116 	 */
16117 	if (state == pci_channel_io_frozen) {
16118 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
16119 		bnxt_fw_fatal_close(bp);
16120 	}
16121 
16122 	if (netif_running(netdev))
16123 		__bnxt_close_nic(bp, true, true);
16124 
16125 	if (pci_is_enabled(pdev))
16126 		pci_disable_device(pdev);
16127 	bnxt_free_ctx_mem(bp);
16128 	rtnl_unlock();
16129 
16130 	/* Request a slot slot reset. */
16131 	return PCI_ERS_RESULT_NEED_RESET;
16132 }
16133 
16134 /**
16135  * bnxt_io_slot_reset - called after the pci bus has been reset.
16136  * @pdev: Pointer to PCI device
16137  *
16138  * Restart the card from scratch, as if from a cold-boot.
16139  * At this point, the card has experienced a hard reset,
16140  * followed by fixups by BIOS, and has its config space
16141  * set up identically to what it was at cold boot.
16142  */
16143 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
16144 {
16145 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
16146 	struct net_device *netdev = pci_get_drvdata(pdev);
16147 	struct bnxt *bp = netdev_priv(netdev);
16148 	int retry = 0;
16149 	int err = 0;
16150 	int off;
16151 
16152 	netdev_info(bp->dev, "PCI Slot Reset\n");
16153 
16154 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16155 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
16156 		msleep(900);
16157 
16158 	rtnl_lock();
16159 
16160 	if (pci_enable_device(pdev)) {
16161 		dev_err(&pdev->dev,
16162 			"Cannot re-enable PCI device after reset.\n");
16163 	} else {
16164 		pci_set_master(pdev);
16165 		/* Upon fatal error, our device internal logic that latches to
16166 		 * BAR value is getting reset and will restore only upon
16167 		 * rewriting the BARs.
16168 		 *
16169 		 * As pci_restore_state() does not re-write the BARs if the
16170 		 * value is same as saved value earlier, driver needs to
16171 		 * write the BARs to 0 to force restore, in case of fatal error.
16172 		 */
16173 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
16174 				       &bp->state)) {
16175 			for (off = PCI_BASE_ADDRESS_0;
16176 			     off <= PCI_BASE_ADDRESS_5; off += 4)
16177 				pci_write_config_dword(bp->pdev, off, 0);
16178 		}
16179 		pci_restore_state(pdev);
16180 		pci_save_state(pdev);
16181 
16182 		bnxt_inv_fw_health_reg(bp);
16183 		bnxt_try_map_fw_health_reg(bp);
16184 
16185 		/* In some PCIe AER scenarios, firmware may take up to
16186 		 * 10 seconds to become ready in the worst case.
16187 		 */
16188 		do {
16189 			err = bnxt_try_recover_fw(bp);
16190 			if (!err)
16191 				break;
16192 			retry++;
16193 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
16194 
16195 		if (err) {
16196 			dev_err(&pdev->dev, "Firmware not ready\n");
16197 			goto reset_exit;
16198 		}
16199 
16200 		err = bnxt_hwrm_func_reset(bp);
16201 		if (!err)
16202 			result = PCI_ERS_RESULT_RECOVERED;
16203 
16204 		bnxt_ulp_irq_stop(bp);
16205 		bnxt_clear_int_mode(bp);
16206 		err = bnxt_init_int_mode(bp);
16207 		bnxt_ulp_irq_restart(bp, err);
16208 	}
16209 
16210 reset_exit:
16211 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16212 	bnxt_clear_reservations(bp, true);
16213 	rtnl_unlock();
16214 
16215 	return result;
16216 }
16217 
16218 /**
16219  * bnxt_io_resume - called when traffic can start flowing again.
16220  * @pdev: Pointer to PCI device
16221  *
16222  * This callback is called when the error recovery driver tells
16223  * us that its OK to resume normal operation.
16224  */
16225 static void bnxt_io_resume(struct pci_dev *pdev)
16226 {
16227 	struct net_device *netdev = pci_get_drvdata(pdev);
16228 	struct bnxt *bp = netdev_priv(netdev);
16229 	int err;
16230 
16231 	netdev_info(bp->dev, "PCI Slot Resume\n");
16232 	rtnl_lock();
16233 
16234 	err = bnxt_hwrm_func_qcaps(bp);
16235 	if (!err && netif_running(netdev))
16236 		err = bnxt_open(netdev);
16237 
16238 	if (!err)
16239 		netif_device_attach(netdev);
16240 
16241 	rtnl_unlock();
16242 	bnxt_ulp_start(bp, err);
16243 	if (!err)
16244 		bnxt_reenable_sriov(bp);
16245 }
16246 
16247 static const struct pci_error_handlers bnxt_err_handler = {
16248 	.error_detected	= bnxt_io_error_detected,
16249 	.slot_reset	= bnxt_io_slot_reset,
16250 	.resume		= bnxt_io_resume
16251 };
16252 
16253 static struct pci_driver bnxt_pci_driver = {
16254 	.name		= DRV_MODULE_NAME,
16255 	.id_table	= bnxt_pci_tbl,
16256 	.probe		= bnxt_init_one,
16257 	.remove		= bnxt_remove_one,
16258 	.shutdown	= bnxt_shutdown,
16259 	.driver.pm	= BNXT_PM_OPS,
16260 	.err_handler	= &bnxt_err_handler,
16261 #if defined(CONFIG_BNXT_SRIOV)
16262 	.sriov_configure = bnxt_sriov_configure,
16263 #endif
16264 };
16265 
16266 static int __init bnxt_init(void)
16267 {
16268 	int err;
16269 
16270 	bnxt_debug_init();
16271 	err = pci_register_driver(&bnxt_pci_driver);
16272 	if (err) {
16273 		bnxt_debug_exit();
16274 		return err;
16275 	}
16276 
16277 	return 0;
16278 }
16279 
16280 static void __exit bnxt_exit(void)
16281 {
16282 	pci_unregister_driver(&bnxt_pci_driver);
16283 	if (bnxt_pf_wq)
16284 		destroy_workqueue(bnxt_pf_wq);
16285 	bnxt_debug_exit();
16286 }
16287 
16288 module_init(bnxt_init);
16289 module_exit(bnxt_exit);
16290