xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision d8d9ba8dc9c77358cd7ea73e4e44e8952c9baf35)
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/ip.h>
41 #include <net/tcp.h>
42 #include <net/udp.h>
43 #include <net/checksum.h>
44 #include <net/ip6_checksum.h>
45 #include <net/udp_tunnel.h>
46 #include <linux/workqueue.h>
47 #include <linux/prefetch.h>
48 #include <linux/cache.h>
49 #include <linux/log2.h>
50 #include <linux/aer.h>
51 #include <linux/bitmap.h>
52 #include <linux/ptp_clock_kernel.h>
53 #include <linux/timecounter.h>
54 #include <linux/cpu_rmap.h>
55 #include <linux/cpumask.h>
56 #include <net/pkt_cls.h>
57 #include <linux/hwmon.h>
58 #include <linux/hwmon-sysfs.h>
59 #include <net/page_pool.h>
60 
61 #include "bnxt_hsi.h"
62 #include "bnxt.h"
63 #include "bnxt_ulp.h"
64 #include "bnxt_sriov.h"
65 #include "bnxt_ethtool.h"
66 #include "bnxt_dcb.h"
67 #include "bnxt_xdp.h"
68 #include "bnxt_ptp.h"
69 #include "bnxt_vfr.h"
70 #include "bnxt_tc.h"
71 #include "bnxt_devlink.h"
72 #include "bnxt_debugfs.h"
73 
74 #define BNXT_TX_TIMEOUT		(5 * HZ)
75 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW)
76 
77 MODULE_LICENSE("GPL");
78 MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
79 
80 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
81 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
82 #define BNXT_RX_COPY_THRESH 256
83 
84 #define BNXT_TX_PUSH_THRESH 164
85 
86 enum board_idx {
87 	BCM57301,
88 	BCM57302,
89 	BCM57304,
90 	BCM57417_NPAR,
91 	BCM58700,
92 	BCM57311,
93 	BCM57312,
94 	BCM57402,
95 	BCM57404,
96 	BCM57406,
97 	BCM57402_NPAR,
98 	BCM57407,
99 	BCM57412,
100 	BCM57414,
101 	BCM57416,
102 	BCM57417,
103 	BCM57412_NPAR,
104 	BCM57314,
105 	BCM57417_SFP,
106 	BCM57416_SFP,
107 	BCM57404_NPAR,
108 	BCM57406_NPAR,
109 	BCM57407_SFP,
110 	BCM57407_NPAR,
111 	BCM57414_NPAR,
112 	BCM57416_NPAR,
113 	BCM57452,
114 	BCM57454,
115 	BCM5745x_NPAR,
116 	BCM57508,
117 	BCM57504,
118 	BCM57502,
119 	BCM57508_NPAR,
120 	BCM57504_NPAR,
121 	BCM57502_NPAR,
122 	BCM58802,
123 	BCM58804,
124 	BCM58808,
125 	NETXTREME_E_VF,
126 	NETXTREME_C_VF,
127 	NETXTREME_S_VF,
128 	NETXTREME_C_VF_HV,
129 	NETXTREME_E_VF_HV,
130 	NETXTREME_E_P5_VF,
131 	NETXTREME_E_P5_VF_HV,
132 };
133 
134 /* indexed by enum above */
135 static const struct {
136 	char *name;
137 } board_info[] = {
138 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
139 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
140 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
141 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
142 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
143 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
144 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
145 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
146 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
147 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
148 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
149 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
150 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
151 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
152 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
153 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
154 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
155 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
156 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
157 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
158 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
159 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
160 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
161 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
162 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
163 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
164 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
165 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
166 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
167 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
168 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
169 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
170 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
171 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
172 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
173 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
174 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
175 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
176 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
177 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
178 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
179 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
180 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
181 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
182 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
183 };
184 
185 static const struct pci_device_id bnxt_pci_tbl[] = {
186 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
187 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
188 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
189 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
190 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
191 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
192 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
193 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
194 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
195 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
196 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
197 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
198 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
199 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
200 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
201 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
202 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
203 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
204 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
205 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
206 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
207 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
208 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
209 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
210 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
211 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
212 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
213 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
214 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
215 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
216 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
217 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
218 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
219 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
220 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
221 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
222 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
223 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
224 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57508_NPAR },
225 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
226 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57502_NPAR },
227 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57508_NPAR },
228 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
229 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57502_NPAR },
230 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
231 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
232 #ifdef CONFIG_BNXT_SRIOV
233 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
234 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
235 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
236 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
237 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
238 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
239 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
240 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
241 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
242 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
243 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
244 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
245 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
246 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
247 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
248 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
249 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
250 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
251 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
252 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
253 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
254 #endif
255 	{ 0 }
256 };
257 
258 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
259 
260 static const u16 bnxt_vf_req_snif[] = {
261 	HWRM_FUNC_CFG,
262 	HWRM_FUNC_VF_CFG,
263 	HWRM_PORT_PHY_QCFG,
264 	HWRM_CFA_L2_FILTER_ALLOC,
265 };
266 
267 static const u16 bnxt_async_events_arr[] = {
268 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
269 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
270 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
271 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
272 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
273 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
274 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
275 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
276 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
277 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
278 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
279 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
280 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
281 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
282 };
283 
284 static struct workqueue_struct *bnxt_pf_wq;
285 
286 static bool bnxt_vf_pciid(enum board_idx idx)
287 {
288 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
289 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
290 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
291 		idx == NETXTREME_E_P5_VF_HV);
292 }
293 
294 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
295 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
296 #define DB_CP_IRQ_DIS_FLAGS	(DB_KEY_CP | DB_IRQ_DIS)
297 
298 #define BNXT_CP_DB_IRQ_DIS(db)						\
299 		writel(DB_CP_IRQ_DIS_FLAGS, db)
300 
301 #define BNXT_DB_CQ(db, idx)						\
302 	writel(DB_CP_FLAGS | RING_CMP(idx), (db)->doorbell)
303 
304 #define BNXT_DB_NQ_P5(db, idx)						\
305 	writeq((db)->db_key64 | DBR_TYPE_NQ | RING_CMP(idx), (db)->doorbell)
306 
307 #define BNXT_DB_CQ_ARM(db, idx)						\
308 	writel(DB_CP_REARM_FLAGS | RING_CMP(idx), (db)->doorbell)
309 
310 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
311 	writeq((db)->db_key64 | DBR_TYPE_NQ_ARM | RING_CMP(idx), (db)->doorbell)
312 
313 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
314 {
315 	if (bp->flags & BNXT_FLAG_CHIP_P5)
316 		BNXT_DB_NQ_P5(db, idx);
317 	else
318 		BNXT_DB_CQ(db, idx);
319 }
320 
321 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
322 {
323 	if (bp->flags & BNXT_FLAG_CHIP_P5)
324 		BNXT_DB_NQ_ARM_P5(db, idx);
325 	else
326 		BNXT_DB_CQ_ARM(db, idx);
327 }
328 
329 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
330 {
331 	if (bp->flags & BNXT_FLAG_CHIP_P5)
332 		writeq(db->db_key64 | DBR_TYPE_CQ_ARMALL | RING_CMP(idx),
333 		       db->doorbell);
334 	else
335 		BNXT_DB_CQ(db, idx);
336 }
337 
338 const u16 bnxt_lhint_arr[] = {
339 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
340 	TX_BD_FLAGS_LHINT_512_TO_1023,
341 	TX_BD_FLAGS_LHINT_1024_TO_2047,
342 	TX_BD_FLAGS_LHINT_1024_TO_2047,
343 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
344 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
345 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
346 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
347 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
348 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
349 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
350 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
351 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
352 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
353 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
354 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
355 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
356 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
357 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
358 };
359 
360 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
361 {
362 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
363 
364 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
365 		return 0;
366 
367 	return md_dst->u.port_info.port_id;
368 }
369 
370 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
371 {
372 	struct bnxt *bp = netdev_priv(dev);
373 	struct tx_bd *txbd;
374 	struct tx_bd_ext *txbd1;
375 	struct netdev_queue *txq;
376 	int i;
377 	dma_addr_t mapping;
378 	unsigned int length, pad = 0;
379 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
380 	u16 prod, last_frag;
381 	struct pci_dev *pdev = bp->pdev;
382 	struct bnxt_tx_ring_info *txr;
383 	struct bnxt_sw_tx_bd *tx_buf;
384 	__le32 lflags = 0;
385 
386 	i = skb_get_queue_mapping(skb);
387 	if (unlikely(i >= bp->tx_nr_rings)) {
388 		dev_kfree_skb_any(skb);
389 		return NETDEV_TX_OK;
390 	}
391 
392 	txq = netdev_get_tx_queue(dev, i);
393 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
394 	prod = txr->tx_prod;
395 
396 	free_size = bnxt_tx_avail(bp, txr);
397 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
398 		netif_tx_stop_queue(txq);
399 		return NETDEV_TX_BUSY;
400 	}
401 
402 	length = skb->len;
403 	len = skb_headlen(skb);
404 	last_frag = skb_shinfo(skb)->nr_frags;
405 
406 	txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
407 
408 	txbd->tx_bd_opaque = prod;
409 
410 	tx_buf = &txr->tx_buf_ring[prod];
411 	tx_buf->skb = skb;
412 	tx_buf->nr_frags = last_frag;
413 
414 	vlan_tag_flags = 0;
415 	cfa_action = bnxt_xmit_get_cfa_action(skb);
416 	if (skb_vlan_tag_present(skb)) {
417 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
418 				 skb_vlan_tag_get(skb);
419 		/* Currently supports 8021Q, 8021AD vlan offloads
420 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
421 		 */
422 		if (skb->vlan_proto == htons(ETH_P_8021Q))
423 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
424 	}
425 
426 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
427 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
428 
429 		if (ptp && ptp->tx_tstamp_en && !skb_is_gso(skb) &&
430 		    atomic_dec_if_positive(&ptp->tx_avail) >= 0) {
431 			if (!bnxt_ptp_parse(skb, &ptp->tx_seqid,
432 					    &ptp->tx_hdr_off)) {
433 				if (vlan_tag_flags)
434 					ptp->tx_hdr_off += VLAN_HLEN;
435 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
436 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
437 			} else {
438 				atomic_inc(&bp->ptp_cfg->tx_avail);
439 			}
440 		}
441 	}
442 
443 	if (unlikely(skb->no_fcs))
444 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
445 
446 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
447 	    !lflags) {
448 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
449 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
450 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
451 		void __iomem *db = txr->tx_db.doorbell;
452 		void *pdata = tx_push_buf->data;
453 		u64 *end;
454 		int j, push_len;
455 
456 		/* Set COAL_NOW to be ready quickly for the next push */
457 		tx_push->tx_bd_len_flags_type =
458 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
459 					TX_BD_TYPE_LONG_TX_BD |
460 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
461 					TX_BD_FLAGS_COAL_NOW |
462 					TX_BD_FLAGS_PACKET_END |
463 					(2 << TX_BD_FLAGS_BD_CNT_SHIFT));
464 
465 		if (skb->ip_summed == CHECKSUM_PARTIAL)
466 			tx_push1->tx_bd_hsize_lflags =
467 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
468 		else
469 			tx_push1->tx_bd_hsize_lflags = 0;
470 
471 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
472 		tx_push1->tx_bd_cfa_action =
473 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
474 
475 		end = pdata + length;
476 		end = PTR_ALIGN(end, 8) - 1;
477 		*end = 0;
478 
479 		skb_copy_from_linear_data(skb, pdata, len);
480 		pdata += len;
481 		for (j = 0; j < last_frag; j++) {
482 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
483 			void *fptr;
484 
485 			fptr = skb_frag_address_safe(frag);
486 			if (!fptr)
487 				goto normal_tx;
488 
489 			memcpy(pdata, fptr, skb_frag_size(frag));
490 			pdata += skb_frag_size(frag);
491 		}
492 
493 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
494 		txbd->tx_bd_haddr = txr->data_mapping;
495 		prod = NEXT_TX(prod);
496 		txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
497 		memcpy(txbd, tx_push1, sizeof(*txbd));
498 		prod = NEXT_TX(prod);
499 		tx_push->doorbell =
500 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
501 		txr->tx_prod = prod;
502 
503 		tx_buf->is_push = 1;
504 		netdev_tx_sent_queue(txq, skb->len);
505 		wmb();	/* Sync is_push and byte queue before pushing data */
506 
507 		push_len = (length + sizeof(*tx_push) + 7) / 8;
508 		if (push_len > 16) {
509 			__iowrite64_copy(db, tx_push_buf, 16);
510 			__iowrite32_copy(db + 4, tx_push_buf + 1,
511 					 (push_len - 16) << 1);
512 		} else {
513 			__iowrite64_copy(db, tx_push_buf, push_len);
514 		}
515 
516 		goto tx_done;
517 	}
518 
519 normal_tx:
520 	if (length < BNXT_MIN_PKT_SIZE) {
521 		pad = BNXT_MIN_PKT_SIZE - length;
522 		if (skb_pad(skb, pad)) {
523 			/* SKB already freed. */
524 			tx_buf->skb = NULL;
525 			return NETDEV_TX_OK;
526 		}
527 		length = BNXT_MIN_PKT_SIZE;
528 	}
529 
530 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
531 
532 	if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
533 		dev_kfree_skb_any(skb);
534 		tx_buf->skb = NULL;
535 		return NETDEV_TX_OK;
536 	}
537 
538 	dma_unmap_addr_set(tx_buf, mapping, mapping);
539 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
540 		((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
541 
542 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
543 
544 	prod = NEXT_TX(prod);
545 	txbd1 = (struct tx_bd_ext *)
546 		&txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
547 
548 	txbd1->tx_bd_hsize_lflags = lflags;
549 	if (skb_is_gso(skb)) {
550 		u32 hdr_len;
551 
552 		if (skb->encapsulation)
553 			hdr_len = skb_inner_network_offset(skb) +
554 				skb_inner_network_header_len(skb) +
555 				inner_tcp_hdrlen(skb);
556 		else
557 			hdr_len = skb_transport_offset(skb) +
558 				tcp_hdrlen(skb);
559 
560 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
561 					TX_BD_FLAGS_T_IPID |
562 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
563 		length = skb_shinfo(skb)->gso_size;
564 		txbd1->tx_bd_mss = cpu_to_le32(length);
565 		length += hdr_len;
566 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
567 		txbd1->tx_bd_hsize_lflags |=
568 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
569 		txbd1->tx_bd_mss = 0;
570 	}
571 
572 	length >>= 9;
573 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
574 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
575 				     skb->len);
576 		i = 0;
577 		goto tx_dma_error;
578 	}
579 	flags |= bnxt_lhint_arr[length];
580 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
581 
582 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
583 	txbd1->tx_bd_cfa_action =
584 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
585 	for (i = 0; i < last_frag; i++) {
586 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
587 
588 		prod = NEXT_TX(prod);
589 		txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
590 
591 		len = skb_frag_size(frag);
592 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
593 					   DMA_TO_DEVICE);
594 
595 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
596 			goto tx_dma_error;
597 
598 		tx_buf = &txr->tx_buf_ring[prod];
599 		dma_unmap_addr_set(tx_buf, mapping, mapping);
600 
601 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
602 
603 		flags = len << TX_BD_LEN_SHIFT;
604 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
605 	}
606 
607 	flags &= ~TX_BD_LEN;
608 	txbd->tx_bd_len_flags_type =
609 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
610 			    TX_BD_FLAGS_PACKET_END);
611 
612 	netdev_tx_sent_queue(txq, skb->len);
613 
614 	skb_tx_timestamp(skb);
615 
616 	/* Sync BD data before updating doorbell */
617 	wmb();
618 
619 	prod = NEXT_TX(prod);
620 	txr->tx_prod = prod;
621 
622 	if (!netdev_xmit_more() || netif_xmit_stopped(txq))
623 		bnxt_db_write(bp, &txr->tx_db, prod);
624 
625 tx_done:
626 
627 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
628 		if (netdev_xmit_more() && !tx_buf->is_push)
629 			bnxt_db_write(bp, &txr->tx_db, prod);
630 
631 		netif_tx_stop_queue(txq);
632 
633 		/* netif_tx_stop_queue() must be done before checking
634 		 * tx index in bnxt_tx_avail() below, because in
635 		 * bnxt_tx_int(), we update tx index before checking for
636 		 * netif_tx_queue_stopped().
637 		 */
638 		smp_mb();
639 		if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
640 			netif_tx_wake_queue(txq);
641 	}
642 	return NETDEV_TX_OK;
643 
644 tx_dma_error:
645 	if (BNXT_TX_PTP_IS_SET(lflags))
646 		atomic_inc(&bp->ptp_cfg->tx_avail);
647 
648 	last_frag = i;
649 
650 	/* start back at beginning and unmap skb */
651 	prod = txr->tx_prod;
652 	tx_buf = &txr->tx_buf_ring[prod];
653 	tx_buf->skb = NULL;
654 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
655 			 skb_headlen(skb), PCI_DMA_TODEVICE);
656 	prod = NEXT_TX(prod);
657 
658 	/* unmap remaining mapped pages */
659 	for (i = 0; i < last_frag; i++) {
660 		prod = NEXT_TX(prod);
661 		tx_buf = &txr->tx_buf_ring[prod];
662 		dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
663 			       skb_frag_size(&skb_shinfo(skb)->frags[i]),
664 			       PCI_DMA_TODEVICE);
665 	}
666 
667 	dev_kfree_skb_any(skb);
668 	return NETDEV_TX_OK;
669 }
670 
671 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
672 {
673 	struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
674 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
675 	u16 cons = txr->tx_cons;
676 	struct pci_dev *pdev = bp->pdev;
677 	int i;
678 	unsigned int tx_bytes = 0;
679 
680 	for (i = 0; i < nr_pkts; i++) {
681 		struct bnxt_sw_tx_bd *tx_buf;
682 		bool compl_deferred = false;
683 		struct sk_buff *skb;
684 		int j, last;
685 
686 		tx_buf = &txr->tx_buf_ring[cons];
687 		cons = NEXT_TX(cons);
688 		skb = tx_buf->skb;
689 		tx_buf->skb = NULL;
690 
691 		if (tx_buf->is_push) {
692 			tx_buf->is_push = 0;
693 			goto next_tx_int;
694 		}
695 
696 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
697 				 skb_headlen(skb), PCI_DMA_TODEVICE);
698 		last = tx_buf->nr_frags;
699 
700 		for (j = 0; j < last; j++) {
701 			cons = NEXT_TX(cons);
702 			tx_buf = &txr->tx_buf_ring[cons];
703 			dma_unmap_page(
704 				&pdev->dev,
705 				dma_unmap_addr(tx_buf, mapping),
706 				skb_frag_size(&skb_shinfo(skb)->frags[j]),
707 				PCI_DMA_TODEVICE);
708 		}
709 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
710 			if (bp->flags & BNXT_FLAG_CHIP_P5) {
711 				if (!bnxt_get_tx_ts_p5(bp, skb))
712 					compl_deferred = true;
713 				else
714 					atomic_inc(&bp->ptp_cfg->tx_avail);
715 			}
716 		}
717 
718 next_tx_int:
719 		cons = NEXT_TX(cons);
720 
721 		tx_bytes += skb->len;
722 		if (!compl_deferred)
723 			dev_kfree_skb_any(skb);
724 	}
725 
726 	netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
727 	txr->tx_cons = cons;
728 
729 	/* Need to make the tx_cons update visible to bnxt_start_xmit()
730 	 * before checking for netif_tx_queue_stopped().  Without the
731 	 * memory barrier, there is a small possibility that bnxt_start_xmit()
732 	 * will miss it and cause the queue to be stopped forever.
733 	 */
734 	smp_mb();
735 
736 	if (unlikely(netif_tx_queue_stopped(txq)) &&
737 	    (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
738 		__netif_tx_lock(txq, smp_processor_id());
739 		if (netif_tx_queue_stopped(txq) &&
740 		    bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
741 		    txr->dev_state != BNXT_DEV_STATE_CLOSING)
742 			netif_tx_wake_queue(txq);
743 		__netif_tx_unlock(txq);
744 	}
745 }
746 
747 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
748 					 struct bnxt_rx_ring_info *rxr,
749 					 gfp_t gfp)
750 {
751 	struct device *dev = &bp->pdev->dev;
752 	struct page *page;
753 
754 	page = page_pool_dev_alloc_pages(rxr->page_pool);
755 	if (!page)
756 		return NULL;
757 
758 	*mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir,
759 				      DMA_ATTR_WEAK_ORDERING);
760 	if (dma_mapping_error(dev, *mapping)) {
761 		page_pool_recycle_direct(rxr->page_pool, page);
762 		return NULL;
763 	}
764 	*mapping += bp->rx_dma_offset;
765 	return page;
766 }
767 
768 static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
769 				       gfp_t gfp)
770 {
771 	u8 *data;
772 	struct pci_dev *pdev = bp->pdev;
773 
774 	data = kmalloc(bp->rx_buf_size, gfp);
775 	if (!data)
776 		return NULL;
777 
778 	*mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
779 					bp->rx_buf_use_size, bp->rx_dir,
780 					DMA_ATTR_WEAK_ORDERING);
781 
782 	if (dma_mapping_error(&pdev->dev, *mapping)) {
783 		kfree(data);
784 		data = NULL;
785 	}
786 	return data;
787 }
788 
789 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
790 		       u16 prod, gfp_t gfp)
791 {
792 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
793 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
794 	dma_addr_t mapping;
795 
796 	if (BNXT_RX_PAGE_MODE(bp)) {
797 		struct page *page =
798 			__bnxt_alloc_rx_page(bp, &mapping, rxr, gfp);
799 
800 		if (!page)
801 			return -ENOMEM;
802 
803 		rx_buf->data = page;
804 		rx_buf->data_ptr = page_address(page) + bp->rx_offset;
805 	} else {
806 		u8 *data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
807 
808 		if (!data)
809 			return -ENOMEM;
810 
811 		rx_buf->data = data;
812 		rx_buf->data_ptr = data + bp->rx_offset;
813 	}
814 	rx_buf->mapping = mapping;
815 
816 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
817 	return 0;
818 }
819 
820 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
821 {
822 	u16 prod = rxr->rx_prod;
823 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
824 	struct rx_bd *cons_bd, *prod_bd;
825 
826 	prod_rx_buf = &rxr->rx_buf_ring[prod];
827 	cons_rx_buf = &rxr->rx_buf_ring[cons];
828 
829 	prod_rx_buf->data = data;
830 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
831 
832 	prod_rx_buf->mapping = cons_rx_buf->mapping;
833 
834 	prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
835 	cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
836 
837 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
838 }
839 
840 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
841 {
842 	u16 next, max = rxr->rx_agg_bmap_size;
843 
844 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
845 	if (next >= max)
846 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
847 	return next;
848 }
849 
850 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
851 				     struct bnxt_rx_ring_info *rxr,
852 				     u16 prod, gfp_t gfp)
853 {
854 	struct rx_bd *rxbd =
855 		&rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
856 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
857 	struct pci_dev *pdev = bp->pdev;
858 	struct page *page;
859 	dma_addr_t mapping;
860 	u16 sw_prod = rxr->rx_sw_agg_prod;
861 	unsigned int offset = 0;
862 
863 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
864 		page = rxr->rx_page;
865 		if (!page) {
866 			page = alloc_page(gfp);
867 			if (!page)
868 				return -ENOMEM;
869 			rxr->rx_page = page;
870 			rxr->rx_page_offset = 0;
871 		}
872 		offset = rxr->rx_page_offset;
873 		rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
874 		if (rxr->rx_page_offset == PAGE_SIZE)
875 			rxr->rx_page = NULL;
876 		else
877 			get_page(page);
878 	} else {
879 		page = alloc_page(gfp);
880 		if (!page)
881 			return -ENOMEM;
882 	}
883 
884 	mapping = dma_map_page_attrs(&pdev->dev, page, offset,
885 				     BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
886 				     DMA_ATTR_WEAK_ORDERING);
887 	if (dma_mapping_error(&pdev->dev, mapping)) {
888 		__free_page(page);
889 		return -EIO;
890 	}
891 
892 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
893 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
894 
895 	__set_bit(sw_prod, rxr->rx_agg_bmap);
896 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
897 	rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
898 
899 	rx_agg_buf->page = page;
900 	rx_agg_buf->offset = offset;
901 	rx_agg_buf->mapping = mapping;
902 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
903 	rxbd->rx_bd_opaque = sw_prod;
904 	return 0;
905 }
906 
907 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
908 				       struct bnxt_cp_ring_info *cpr,
909 				       u16 cp_cons, u16 curr)
910 {
911 	struct rx_agg_cmp *agg;
912 
913 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
914 	agg = (struct rx_agg_cmp *)
915 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
916 	return agg;
917 }
918 
919 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
920 					      struct bnxt_rx_ring_info *rxr,
921 					      u16 agg_id, u16 curr)
922 {
923 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
924 
925 	return &tpa_info->agg_arr[curr];
926 }
927 
928 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
929 				   u16 start, u32 agg_bufs, bool tpa)
930 {
931 	struct bnxt_napi *bnapi = cpr->bnapi;
932 	struct bnxt *bp = bnapi->bp;
933 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
934 	u16 prod = rxr->rx_agg_prod;
935 	u16 sw_prod = rxr->rx_sw_agg_prod;
936 	bool p5_tpa = false;
937 	u32 i;
938 
939 	if ((bp->flags & BNXT_FLAG_CHIP_P5) && tpa)
940 		p5_tpa = true;
941 
942 	for (i = 0; i < agg_bufs; i++) {
943 		u16 cons;
944 		struct rx_agg_cmp *agg;
945 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
946 		struct rx_bd *prod_bd;
947 		struct page *page;
948 
949 		if (p5_tpa)
950 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
951 		else
952 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
953 		cons = agg->rx_agg_cmp_opaque;
954 		__clear_bit(cons, rxr->rx_agg_bmap);
955 
956 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
957 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
958 
959 		__set_bit(sw_prod, rxr->rx_agg_bmap);
960 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
961 		cons_rx_buf = &rxr->rx_agg_ring[cons];
962 
963 		/* It is possible for sw_prod to be equal to cons, so
964 		 * set cons_rx_buf->page to NULL first.
965 		 */
966 		page = cons_rx_buf->page;
967 		cons_rx_buf->page = NULL;
968 		prod_rx_buf->page = page;
969 		prod_rx_buf->offset = cons_rx_buf->offset;
970 
971 		prod_rx_buf->mapping = cons_rx_buf->mapping;
972 
973 		prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
974 
975 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
976 		prod_bd->rx_bd_opaque = sw_prod;
977 
978 		prod = NEXT_RX_AGG(prod);
979 		sw_prod = NEXT_RX_AGG(sw_prod);
980 	}
981 	rxr->rx_agg_prod = prod;
982 	rxr->rx_sw_agg_prod = sw_prod;
983 }
984 
985 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
986 					struct bnxt_rx_ring_info *rxr,
987 					u16 cons, void *data, u8 *data_ptr,
988 					dma_addr_t dma_addr,
989 					unsigned int offset_and_len)
990 {
991 	unsigned int payload = offset_and_len >> 16;
992 	unsigned int len = offset_and_len & 0xffff;
993 	skb_frag_t *frag;
994 	struct page *page = data;
995 	u16 prod = rxr->rx_prod;
996 	struct sk_buff *skb;
997 	int off, err;
998 
999 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1000 	if (unlikely(err)) {
1001 		bnxt_reuse_rx_data(rxr, cons, data);
1002 		return NULL;
1003 	}
1004 	dma_addr -= bp->rx_dma_offset;
1005 	dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
1006 			     DMA_ATTR_WEAK_ORDERING);
1007 	page_pool_release_page(rxr->page_pool, page);
1008 
1009 	if (unlikely(!payload))
1010 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1011 
1012 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1013 	if (!skb) {
1014 		__free_page(page);
1015 		return NULL;
1016 	}
1017 
1018 	off = (void *)data_ptr - page_address(page);
1019 	skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
1020 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1021 	       payload + NET_IP_ALIGN);
1022 
1023 	frag = &skb_shinfo(skb)->frags[0];
1024 	skb_frag_size_sub(frag, payload);
1025 	skb_frag_off_add(frag, payload);
1026 	skb->data_len -= payload;
1027 	skb->tail += payload;
1028 
1029 	return skb;
1030 }
1031 
1032 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1033 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1034 				   void *data, u8 *data_ptr,
1035 				   dma_addr_t dma_addr,
1036 				   unsigned int offset_and_len)
1037 {
1038 	u16 prod = rxr->rx_prod;
1039 	struct sk_buff *skb;
1040 	int err;
1041 
1042 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1043 	if (unlikely(err)) {
1044 		bnxt_reuse_rx_data(rxr, cons, data);
1045 		return NULL;
1046 	}
1047 
1048 	skb = build_skb(data, 0);
1049 	dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1050 			       bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
1051 	if (!skb) {
1052 		kfree(data);
1053 		return NULL;
1054 	}
1055 
1056 	skb_reserve(skb, bp->rx_offset);
1057 	skb_put(skb, offset_and_len & 0xffff);
1058 	return skb;
1059 }
1060 
1061 static struct sk_buff *bnxt_rx_pages(struct bnxt *bp,
1062 				     struct bnxt_cp_ring_info *cpr,
1063 				     struct sk_buff *skb, u16 idx,
1064 				     u32 agg_bufs, bool tpa)
1065 {
1066 	struct bnxt_napi *bnapi = cpr->bnapi;
1067 	struct pci_dev *pdev = bp->pdev;
1068 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1069 	u16 prod = rxr->rx_agg_prod;
1070 	bool p5_tpa = false;
1071 	u32 i;
1072 
1073 	if ((bp->flags & BNXT_FLAG_CHIP_P5) && tpa)
1074 		p5_tpa = true;
1075 
1076 	for (i = 0; i < agg_bufs; i++) {
1077 		u16 cons, frag_len;
1078 		struct rx_agg_cmp *agg;
1079 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1080 		struct page *page;
1081 		dma_addr_t mapping;
1082 
1083 		if (p5_tpa)
1084 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1085 		else
1086 			agg = bnxt_get_agg(bp, cpr, idx, i);
1087 		cons = agg->rx_agg_cmp_opaque;
1088 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1089 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1090 
1091 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1092 		skb_fill_page_desc(skb, i, cons_rx_buf->page,
1093 				   cons_rx_buf->offset, frag_len);
1094 		__clear_bit(cons, rxr->rx_agg_bmap);
1095 
1096 		/* It is possible for bnxt_alloc_rx_page() to allocate
1097 		 * a sw_prod index that equals the cons index, so we
1098 		 * need to clear the cons entry now.
1099 		 */
1100 		mapping = cons_rx_buf->mapping;
1101 		page = cons_rx_buf->page;
1102 		cons_rx_buf->page = NULL;
1103 
1104 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1105 			struct skb_shared_info *shinfo;
1106 			unsigned int nr_frags;
1107 
1108 			shinfo = skb_shinfo(skb);
1109 			nr_frags = --shinfo->nr_frags;
1110 			__skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
1111 
1112 			dev_kfree_skb(skb);
1113 
1114 			cons_rx_buf->page = page;
1115 
1116 			/* Update prod since possibly some pages have been
1117 			 * allocated already.
1118 			 */
1119 			rxr->rx_agg_prod = prod;
1120 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1121 			return NULL;
1122 		}
1123 
1124 		dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1125 				     PCI_DMA_FROMDEVICE,
1126 				     DMA_ATTR_WEAK_ORDERING);
1127 
1128 		skb->data_len += frag_len;
1129 		skb->len += frag_len;
1130 		skb->truesize += PAGE_SIZE;
1131 
1132 		prod = NEXT_RX_AGG(prod);
1133 	}
1134 	rxr->rx_agg_prod = prod;
1135 	return skb;
1136 }
1137 
1138 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1139 			       u8 agg_bufs, u32 *raw_cons)
1140 {
1141 	u16 last;
1142 	struct rx_agg_cmp *agg;
1143 
1144 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1145 	last = RING_CMP(*raw_cons);
1146 	agg = (struct rx_agg_cmp *)
1147 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1148 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1149 }
1150 
1151 static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1152 					    unsigned int len,
1153 					    dma_addr_t mapping)
1154 {
1155 	struct bnxt *bp = bnapi->bp;
1156 	struct pci_dev *pdev = bp->pdev;
1157 	struct sk_buff *skb;
1158 
1159 	skb = napi_alloc_skb(&bnapi->napi, len);
1160 	if (!skb)
1161 		return NULL;
1162 
1163 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1164 				bp->rx_dir);
1165 
1166 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1167 	       len + NET_IP_ALIGN);
1168 
1169 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1170 				   bp->rx_dir);
1171 
1172 	skb_put(skb, len);
1173 	return skb;
1174 }
1175 
1176 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1177 			   u32 *raw_cons, void *cmp)
1178 {
1179 	struct rx_cmp *rxcmp = cmp;
1180 	u32 tmp_raw_cons = *raw_cons;
1181 	u8 cmp_type, agg_bufs = 0;
1182 
1183 	cmp_type = RX_CMP_TYPE(rxcmp);
1184 
1185 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1186 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1187 			    RX_CMP_AGG_BUFS) >>
1188 			   RX_CMP_AGG_BUFS_SHIFT;
1189 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1190 		struct rx_tpa_end_cmp *tpa_end = cmp;
1191 
1192 		if (bp->flags & BNXT_FLAG_CHIP_P5)
1193 			return 0;
1194 
1195 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1196 	}
1197 
1198 	if (agg_bufs) {
1199 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1200 			return -EBUSY;
1201 	}
1202 	*raw_cons = tmp_raw_cons;
1203 	return 0;
1204 }
1205 
1206 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
1207 {
1208 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
1209 		return;
1210 
1211 	if (BNXT_PF(bp))
1212 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
1213 	else
1214 		schedule_delayed_work(&bp->fw_reset_task, delay);
1215 }
1216 
1217 static void bnxt_queue_sp_work(struct bnxt *bp)
1218 {
1219 	if (BNXT_PF(bp))
1220 		queue_work(bnxt_pf_wq, &bp->sp_task);
1221 	else
1222 		schedule_work(&bp->sp_task);
1223 }
1224 
1225 static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
1226 {
1227 	if (!rxr->bnapi->in_reset) {
1228 		rxr->bnapi->in_reset = true;
1229 		if (bp->flags & BNXT_FLAG_CHIP_P5)
1230 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
1231 		else
1232 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
1233 		bnxt_queue_sp_work(bp);
1234 	}
1235 	rxr->rx_next_cons = 0xffff;
1236 }
1237 
1238 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1239 {
1240 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1241 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1242 
1243 	if (test_bit(idx, map->agg_idx_bmap))
1244 		idx = find_first_zero_bit(map->agg_idx_bmap,
1245 					  BNXT_AGG_IDX_BMAP_SIZE);
1246 	__set_bit(idx, map->agg_idx_bmap);
1247 	map->agg_id_tbl[agg_id] = idx;
1248 	return idx;
1249 }
1250 
1251 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1252 {
1253 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1254 
1255 	__clear_bit(idx, map->agg_idx_bmap);
1256 }
1257 
1258 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1259 {
1260 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1261 
1262 	return map->agg_id_tbl[agg_id];
1263 }
1264 
1265 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1266 			   struct rx_tpa_start_cmp *tpa_start,
1267 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1268 {
1269 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1270 	struct bnxt_tpa_info *tpa_info;
1271 	u16 cons, prod, agg_id;
1272 	struct rx_bd *prod_bd;
1273 	dma_addr_t mapping;
1274 
1275 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
1276 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1277 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1278 	} else {
1279 		agg_id = TPA_START_AGG_ID(tpa_start);
1280 	}
1281 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1282 	prod = rxr->rx_prod;
1283 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1284 	prod_rx_buf = &rxr->rx_buf_ring[prod];
1285 	tpa_info = &rxr->rx_tpa[agg_id];
1286 
1287 	if (unlikely(cons != rxr->rx_next_cons ||
1288 		     TPA_START_ERROR(tpa_start))) {
1289 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1290 			    cons, rxr->rx_next_cons,
1291 			    TPA_START_ERROR_CODE(tpa_start1));
1292 		bnxt_sched_reset(bp, rxr);
1293 		return;
1294 	}
1295 	/* Store cfa_code in tpa_info to use in tpa_end
1296 	 * completion processing.
1297 	 */
1298 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1299 	prod_rx_buf->data = tpa_info->data;
1300 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1301 
1302 	mapping = tpa_info->mapping;
1303 	prod_rx_buf->mapping = mapping;
1304 
1305 	prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1306 
1307 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1308 
1309 	tpa_info->data = cons_rx_buf->data;
1310 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1311 	cons_rx_buf->data = NULL;
1312 	tpa_info->mapping = cons_rx_buf->mapping;
1313 
1314 	tpa_info->len =
1315 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1316 				RX_TPA_START_CMP_LEN_SHIFT;
1317 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1318 		u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1319 
1320 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1321 		tpa_info->gso_type = SKB_GSO_TCPV4;
1322 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1323 		if (hash_type == 3 || TPA_START_IS_IPV6(tpa_start1))
1324 			tpa_info->gso_type = SKB_GSO_TCPV6;
1325 		tpa_info->rss_hash =
1326 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1327 	} else {
1328 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1329 		tpa_info->gso_type = 0;
1330 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1331 	}
1332 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1333 	tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1334 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1335 	tpa_info->agg_count = 0;
1336 
1337 	rxr->rx_prod = NEXT_RX(prod);
1338 	cons = NEXT_RX(cons);
1339 	rxr->rx_next_cons = NEXT_RX(cons);
1340 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1341 
1342 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1343 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1344 	cons_rx_buf->data = NULL;
1345 }
1346 
1347 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1348 {
1349 	if (agg_bufs)
1350 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1351 }
1352 
1353 #ifdef CONFIG_INET
1354 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1355 {
1356 	struct udphdr *uh = NULL;
1357 
1358 	if (ip_proto == htons(ETH_P_IP)) {
1359 		struct iphdr *iph = (struct iphdr *)skb->data;
1360 
1361 		if (iph->protocol == IPPROTO_UDP)
1362 			uh = (struct udphdr *)(iph + 1);
1363 	} else {
1364 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1365 
1366 		if (iph->nexthdr == IPPROTO_UDP)
1367 			uh = (struct udphdr *)(iph + 1);
1368 	}
1369 	if (uh) {
1370 		if (uh->check)
1371 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1372 		else
1373 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1374 	}
1375 }
1376 #endif
1377 
1378 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1379 					   int payload_off, int tcp_ts,
1380 					   struct sk_buff *skb)
1381 {
1382 #ifdef CONFIG_INET
1383 	struct tcphdr *th;
1384 	int len, nw_off;
1385 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1386 	u32 hdr_info = tpa_info->hdr_info;
1387 	bool loopback = false;
1388 
1389 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1390 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1391 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1392 
1393 	/* If the packet is an internal loopback packet, the offsets will
1394 	 * have an extra 4 bytes.
1395 	 */
1396 	if (inner_mac_off == 4) {
1397 		loopback = true;
1398 	} else if (inner_mac_off > 4) {
1399 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1400 					    ETH_HLEN - 2));
1401 
1402 		/* We only support inner iPv4/ipv6.  If we don't see the
1403 		 * correct protocol ID, it must be a loopback packet where
1404 		 * the offsets are off by 4.
1405 		 */
1406 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1407 			loopback = true;
1408 	}
1409 	if (loopback) {
1410 		/* internal loopback packet, subtract all offsets by 4 */
1411 		inner_ip_off -= 4;
1412 		inner_mac_off -= 4;
1413 		outer_ip_off -= 4;
1414 	}
1415 
1416 	nw_off = inner_ip_off - ETH_HLEN;
1417 	skb_set_network_header(skb, nw_off);
1418 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1419 		struct ipv6hdr *iph = ipv6_hdr(skb);
1420 
1421 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1422 		len = skb->len - skb_transport_offset(skb);
1423 		th = tcp_hdr(skb);
1424 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1425 	} else {
1426 		struct iphdr *iph = ip_hdr(skb);
1427 
1428 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1429 		len = skb->len - skb_transport_offset(skb);
1430 		th = tcp_hdr(skb);
1431 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1432 	}
1433 
1434 	if (inner_mac_off) { /* tunnel */
1435 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1436 					    ETH_HLEN - 2));
1437 
1438 		bnxt_gro_tunnel(skb, proto);
1439 	}
1440 #endif
1441 	return skb;
1442 }
1443 
1444 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1445 					   int payload_off, int tcp_ts,
1446 					   struct sk_buff *skb)
1447 {
1448 #ifdef CONFIG_INET
1449 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1450 	u32 hdr_info = tpa_info->hdr_info;
1451 	int iphdr_len, nw_off;
1452 
1453 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1454 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1455 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1456 
1457 	nw_off = inner_ip_off - ETH_HLEN;
1458 	skb_set_network_header(skb, nw_off);
1459 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1460 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1461 	skb_set_transport_header(skb, nw_off + iphdr_len);
1462 
1463 	if (inner_mac_off) { /* tunnel */
1464 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1465 					    ETH_HLEN - 2));
1466 
1467 		bnxt_gro_tunnel(skb, proto);
1468 	}
1469 #endif
1470 	return skb;
1471 }
1472 
1473 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1474 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1475 
1476 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1477 					   int payload_off, int tcp_ts,
1478 					   struct sk_buff *skb)
1479 {
1480 #ifdef CONFIG_INET
1481 	struct tcphdr *th;
1482 	int len, nw_off, tcp_opt_len = 0;
1483 
1484 	if (tcp_ts)
1485 		tcp_opt_len = 12;
1486 
1487 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1488 		struct iphdr *iph;
1489 
1490 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1491 			 ETH_HLEN;
1492 		skb_set_network_header(skb, nw_off);
1493 		iph = ip_hdr(skb);
1494 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1495 		len = skb->len - skb_transport_offset(skb);
1496 		th = tcp_hdr(skb);
1497 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1498 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1499 		struct ipv6hdr *iph;
1500 
1501 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1502 			 ETH_HLEN;
1503 		skb_set_network_header(skb, nw_off);
1504 		iph = ipv6_hdr(skb);
1505 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1506 		len = skb->len - skb_transport_offset(skb);
1507 		th = tcp_hdr(skb);
1508 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1509 	} else {
1510 		dev_kfree_skb_any(skb);
1511 		return NULL;
1512 	}
1513 
1514 	if (nw_off) /* tunnel */
1515 		bnxt_gro_tunnel(skb, skb->protocol);
1516 #endif
1517 	return skb;
1518 }
1519 
1520 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1521 					   struct bnxt_tpa_info *tpa_info,
1522 					   struct rx_tpa_end_cmp *tpa_end,
1523 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1524 					   struct sk_buff *skb)
1525 {
1526 #ifdef CONFIG_INET
1527 	int payload_off;
1528 	u16 segs;
1529 
1530 	segs = TPA_END_TPA_SEGS(tpa_end);
1531 	if (segs == 1)
1532 		return skb;
1533 
1534 	NAPI_GRO_CB(skb)->count = segs;
1535 	skb_shinfo(skb)->gso_size =
1536 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1537 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1538 	if (bp->flags & BNXT_FLAG_CHIP_P5)
1539 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1540 	else
1541 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1542 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1543 	if (likely(skb))
1544 		tcp_gro_complete(skb);
1545 #endif
1546 	return skb;
1547 }
1548 
1549 /* Given the cfa_code of a received packet determine which
1550  * netdev (vf-rep or PF) the packet is destined to.
1551  */
1552 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1553 {
1554 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1555 
1556 	/* if vf-rep dev is NULL, the must belongs to the PF */
1557 	return dev ? dev : bp->dev;
1558 }
1559 
1560 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1561 					   struct bnxt_cp_ring_info *cpr,
1562 					   u32 *raw_cons,
1563 					   struct rx_tpa_end_cmp *tpa_end,
1564 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1565 					   u8 *event)
1566 {
1567 	struct bnxt_napi *bnapi = cpr->bnapi;
1568 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1569 	u8 *data_ptr, agg_bufs;
1570 	unsigned int len;
1571 	struct bnxt_tpa_info *tpa_info;
1572 	dma_addr_t mapping;
1573 	struct sk_buff *skb;
1574 	u16 idx = 0, agg_id;
1575 	void *data;
1576 	bool gro;
1577 
1578 	if (unlikely(bnapi->in_reset)) {
1579 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1580 
1581 		if (rc < 0)
1582 			return ERR_PTR(-EBUSY);
1583 		return NULL;
1584 	}
1585 
1586 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
1587 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1588 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1589 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1590 		tpa_info = &rxr->rx_tpa[agg_id];
1591 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1592 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1593 				    agg_bufs, tpa_info->agg_count);
1594 			agg_bufs = tpa_info->agg_count;
1595 		}
1596 		tpa_info->agg_count = 0;
1597 		*event |= BNXT_AGG_EVENT;
1598 		bnxt_free_agg_idx(rxr, agg_id);
1599 		idx = agg_id;
1600 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1601 	} else {
1602 		agg_id = TPA_END_AGG_ID(tpa_end);
1603 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1604 		tpa_info = &rxr->rx_tpa[agg_id];
1605 		idx = RING_CMP(*raw_cons);
1606 		if (agg_bufs) {
1607 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1608 				return ERR_PTR(-EBUSY);
1609 
1610 			*event |= BNXT_AGG_EVENT;
1611 			idx = NEXT_CMP(idx);
1612 		}
1613 		gro = !!TPA_END_GRO(tpa_end);
1614 	}
1615 	data = tpa_info->data;
1616 	data_ptr = tpa_info->data_ptr;
1617 	prefetch(data_ptr);
1618 	len = tpa_info->len;
1619 	mapping = tpa_info->mapping;
1620 
1621 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1622 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1623 		if (agg_bufs > MAX_SKB_FRAGS)
1624 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1625 				    agg_bufs, (int)MAX_SKB_FRAGS);
1626 		return NULL;
1627 	}
1628 
1629 	if (len <= bp->rx_copy_thresh) {
1630 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1631 		if (!skb) {
1632 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1633 			return NULL;
1634 		}
1635 	} else {
1636 		u8 *new_data;
1637 		dma_addr_t new_mapping;
1638 
1639 		new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1640 		if (!new_data) {
1641 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1642 			return NULL;
1643 		}
1644 
1645 		tpa_info->data = new_data;
1646 		tpa_info->data_ptr = new_data + bp->rx_offset;
1647 		tpa_info->mapping = new_mapping;
1648 
1649 		skb = build_skb(data, 0);
1650 		dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1651 				       bp->rx_buf_use_size, bp->rx_dir,
1652 				       DMA_ATTR_WEAK_ORDERING);
1653 
1654 		if (!skb) {
1655 			kfree(data);
1656 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1657 			return NULL;
1658 		}
1659 		skb_reserve(skb, bp->rx_offset);
1660 		skb_put(skb, len);
1661 	}
1662 
1663 	if (agg_bufs) {
1664 		skb = bnxt_rx_pages(bp, cpr, skb, idx, agg_bufs, true);
1665 		if (!skb) {
1666 			/* Page reuse already handled by bnxt_rx_pages(). */
1667 			return NULL;
1668 		}
1669 	}
1670 
1671 	skb->protocol =
1672 		eth_type_trans(skb, bnxt_get_pkt_dev(bp, tpa_info->cfa_code));
1673 
1674 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1675 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1676 
1677 	if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1678 	    (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1679 		__be16 vlan_proto = htons(tpa_info->metadata >>
1680 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1681 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1682 
1683 		if (eth_type_vlan(vlan_proto)) {
1684 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1685 		} else {
1686 			dev_kfree_skb(skb);
1687 			return NULL;
1688 		}
1689 	}
1690 
1691 	skb_checksum_none_assert(skb);
1692 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1693 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1694 		skb->csum_level =
1695 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1696 	}
1697 
1698 	if (gro)
1699 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1700 
1701 	return skb;
1702 }
1703 
1704 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1705 			 struct rx_agg_cmp *rx_agg)
1706 {
1707 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1708 	struct bnxt_tpa_info *tpa_info;
1709 
1710 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1711 	tpa_info = &rxr->rx_tpa[agg_id];
1712 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1713 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1714 }
1715 
1716 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1717 			     struct sk_buff *skb)
1718 {
1719 	if (skb->dev != bp->dev) {
1720 		/* this packet belongs to a vf-rep */
1721 		bnxt_vf_rep_rx(bp, skb);
1722 		return;
1723 	}
1724 	skb_record_rx_queue(skb, bnapi->index);
1725 	napi_gro_receive(&bnapi->napi, skb);
1726 }
1727 
1728 /* returns the following:
1729  * 1       - 1 packet successfully received
1730  * 0       - successful TPA_START, packet not completed yet
1731  * -EBUSY  - completion ring does not have all the agg buffers yet
1732  * -ENOMEM - packet aborted due to out of memory
1733  * -EIO    - packet aborted due to hw error indicated in BD
1734  */
1735 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1736 		       u32 *raw_cons, u8 *event)
1737 {
1738 	struct bnxt_napi *bnapi = cpr->bnapi;
1739 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1740 	struct net_device *dev = bp->dev;
1741 	struct rx_cmp *rxcmp;
1742 	struct rx_cmp_ext *rxcmp1;
1743 	u32 tmp_raw_cons = *raw_cons;
1744 	u16 cfa_code, cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1745 	struct bnxt_sw_rx_bd *rx_buf;
1746 	unsigned int len;
1747 	u8 *data_ptr, agg_bufs, cmp_type;
1748 	dma_addr_t dma_addr;
1749 	struct sk_buff *skb;
1750 	u32 flags, misc;
1751 	void *data;
1752 	int rc = 0;
1753 
1754 	rxcmp = (struct rx_cmp *)
1755 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1756 
1757 	cmp_type = RX_CMP_TYPE(rxcmp);
1758 
1759 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
1760 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
1761 		goto next_rx_no_prod_no_len;
1762 	}
1763 
1764 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1765 	cp_cons = RING_CMP(tmp_raw_cons);
1766 	rxcmp1 = (struct rx_cmp_ext *)
1767 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1768 
1769 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1770 		return -EBUSY;
1771 
1772 	prod = rxr->rx_prod;
1773 
1774 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1775 		bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1776 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
1777 
1778 		*event |= BNXT_RX_EVENT;
1779 		goto next_rx_no_prod_no_len;
1780 
1781 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1782 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
1783 				   (struct rx_tpa_end_cmp *)rxcmp,
1784 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
1785 
1786 		if (IS_ERR(skb))
1787 			return -EBUSY;
1788 
1789 		rc = -ENOMEM;
1790 		if (likely(skb)) {
1791 			bnxt_deliver_skb(bp, bnapi, skb);
1792 			rc = 1;
1793 		}
1794 		*event |= BNXT_RX_EVENT;
1795 		goto next_rx_no_prod_no_len;
1796 	}
1797 
1798 	cons = rxcmp->rx_cmp_opaque;
1799 	if (unlikely(cons != rxr->rx_next_cons)) {
1800 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
1801 
1802 		/* 0xffff is forced error, don't print it */
1803 		if (rxr->rx_next_cons != 0xffff)
1804 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
1805 				    cons, rxr->rx_next_cons);
1806 		bnxt_sched_reset(bp, rxr);
1807 		if (rc1)
1808 			return rc1;
1809 		goto next_rx_no_prod_no_len;
1810 	}
1811 	rx_buf = &rxr->rx_buf_ring[cons];
1812 	data = rx_buf->data;
1813 	data_ptr = rx_buf->data_ptr;
1814 	prefetch(data_ptr);
1815 
1816 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1817 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
1818 
1819 	if (agg_bufs) {
1820 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1821 			return -EBUSY;
1822 
1823 		cp_cons = NEXT_CMP(cp_cons);
1824 		*event |= BNXT_AGG_EVENT;
1825 	}
1826 	*event |= BNXT_RX_EVENT;
1827 
1828 	rx_buf->data = NULL;
1829 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1830 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
1831 
1832 		bnxt_reuse_rx_data(rxr, cons, data);
1833 		if (agg_bufs)
1834 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
1835 					       false);
1836 
1837 		rc = -EIO;
1838 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
1839 			bnapi->cp_ring.sw_stats.rx.rx_buf_errors++;
1840 			if (!(bp->flags & BNXT_FLAG_CHIP_P5) &&
1841 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
1842 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
1843 						 rx_err);
1844 				bnxt_sched_reset(bp, rxr);
1845 			}
1846 		}
1847 		goto next_rx_no_len;
1848 	}
1849 
1850 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
1851 	len = flags >> RX_CMP_LEN_SHIFT;
1852 	dma_addr = rx_buf->mapping;
1853 
1854 	if (bnxt_rx_xdp(bp, rxr, cons, data, &data_ptr, &len, event)) {
1855 		rc = 1;
1856 		goto next_rx;
1857 	}
1858 
1859 	if (len <= bp->rx_copy_thresh) {
1860 		skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
1861 		bnxt_reuse_rx_data(rxr, cons, data);
1862 		if (!skb) {
1863 			if (agg_bufs)
1864 				bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
1865 						       agg_bufs, false);
1866 			rc = -ENOMEM;
1867 			goto next_rx;
1868 		}
1869 	} else {
1870 		u32 payload;
1871 
1872 		if (rx_buf->data_ptr == data_ptr)
1873 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
1874 		else
1875 			payload = 0;
1876 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
1877 				      payload | len);
1878 		if (!skb) {
1879 			rc = -ENOMEM;
1880 			goto next_rx;
1881 		}
1882 	}
1883 
1884 	if (agg_bufs) {
1885 		skb = bnxt_rx_pages(bp, cpr, skb, cp_cons, agg_bufs, false);
1886 		if (!skb) {
1887 			rc = -ENOMEM;
1888 			goto next_rx;
1889 		}
1890 	}
1891 
1892 	if (RX_CMP_HASH_VALID(rxcmp)) {
1893 		u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1894 		enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1895 
1896 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1897 		if (hash_type != 1 && hash_type != 3)
1898 			type = PKT_HASH_TYPE_L3;
1899 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1900 	}
1901 
1902 	cfa_code = RX_CMP_CFA_CODE(rxcmp1);
1903 	skb->protocol = eth_type_trans(skb, bnxt_get_pkt_dev(bp, cfa_code));
1904 
1905 	if ((rxcmp1->rx_cmp_flags2 &
1906 	     cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1907 	    (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1908 		u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1909 		u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1910 		__be16 vlan_proto = htons(meta_data >>
1911 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1912 
1913 		if (eth_type_vlan(vlan_proto)) {
1914 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1915 		} else {
1916 			dev_kfree_skb(skb);
1917 			goto next_rx;
1918 		}
1919 	}
1920 
1921 	skb_checksum_none_assert(skb);
1922 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
1923 		if (dev->features & NETIF_F_RXCSUM) {
1924 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1925 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1926 		}
1927 	} else {
1928 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1929 			if (dev->features & NETIF_F_RXCSUM)
1930 				bnapi->cp_ring.sw_stats.rx.rx_l4_csum_errors++;
1931 		}
1932 	}
1933 
1934 	if (unlikely((flags & RX_CMP_FLAGS_ITYPES_MASK) ==
1935 		     RX_CMP_FLAGS_ITYPE_PTP_W_TS)) {
1936 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
1937 			u32 cmpl_ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
1938 			u64 ns, ts;
1939 
1940 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
1941 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
1942 
1943 				spin_lock_bh(&ptp->ptp_lock);
1944 				ns = timecounter_cyc2time(&ptp->tc, ts);
1945 				spin_unlock_bh(&ptp->ptp_lock);
1946 				memset(skb_hwtstamps(skb), 0,
1947 				       sizeof(*skb_hwtstamps(skb)));
1948 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
1949 			}
1950 		}
1951 	}
1952 	bnxt_deliver_skb(bp, bnapi, skb);
1953 	rc = 1;
1954 
1955 next_rx:
1956 	cpr->rx_packets += 1;
1957 	cpr->rx_bytes += len;
1958 
1959 next_rx_no_len:
1960 	rxr->rx_prod = NEXT_RX(prod);
1961 	rxr->rx_next_cons = NEXT_RX(cons);
1962 
1963 next_rx_no_prod_no_len:
1964 	*raw_cons = tmp_raw_cons;
1965 
1966 	return rc;
1967 }
1968 
1969 /* In netpoll mode, if we are using a combined completion ring, we need to
1970  * discard the rx packets and recycle the buffers.
1971  */
1972 static int bnxt_force_rx_discard(struct bnxt *bp,
1973 				 struct bnxt_cp_ring_info *cpr,
1974 				 u32 *raw_cons, u8 *event)
1975 {
1976 	u32 tmp_raw_cons = *raw_cons;
1977 	struct rx_cmp_ext *rxcmp1;
1978 	struct rx_cmp *rxcmp;
1979 	u16 cp_cons;
1980 	u8 cmp_type;
1981 
1982 	cp_cons = RING_CMP(tmp_raw_cons);
1983 	rxcmp = (struct rx_cmp *)
1984 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1985 
1986 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1987 	cp_cons = RING_CMP(tmp_raw_cons);
1988 	rxcmp1 = (struct rx_cmp_ext *)
1989 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1990 
1991 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1992 		return -EBUSY;
1993 
1994 	cmp_type = RX_CMP_TYPE(rxcmp);
1995 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1996 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1997 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1998 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1999 		struct rx_tpa_end_cmp_ext *tpa_end1;
2000 
2001 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2002 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2003 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2004 	}
2005 	return bnxt_rx_pkt(bp, cpr, raw_cons, event);
2006 }
2007 
2008 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2009 {
2010 	struct bnxt_fw_health *fw_health = bp->fw_health;
2011 	u32 reg = fw_health->regs[reg_idx];
2012 	u32 reg_type, reg_off, val = 0;
2013 
2014 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2015 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2016 	switch (reg_type) {
2017 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2018 		pci_read_config_dword(bp->pdev, reg_off, &val);
2019 		break;
2020 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2021 		reg_off = fw_health->mapped_regs[reg_idx];
2022 		fallthrough;
2023 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2024 		val = readl(bp->bar0 + reg_off);
2025 		break;
2026 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2027 		val = readl(bp->bar1 + reg_off);
2028 		break;
2029 	}
2030 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2031 		val &= fw_health->fw_reset_inprog_reg_mask;
2032 	return val;
2033 }
2034 
2035 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2036 {
2037 	int i;
2038 
2039 	for (i = 0; i < bp->rx_nr_rings; i++) {
2040 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2041 		struct bnxt_ring_grp_info *grp_info;
2042 
2043 		grp_info = &bp->grp_info[grp_idx];
2044 		if (grp_info->agg_fw_ring_id == ring_id)
2045 			return grp_idx;
2046 	}
2047 	return INVALID_HW_RING_ID;
2048 }
2049 
2050 static void bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2051 {
2052 	switch (BNXT_EVENT_ERROR_REPORT_TYPE(data1)) {
2053 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2054 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2055 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2056 		break;
2057 	default:
2058 		netdev_err(bp->dev, "FW reported unknown error type\n");
2059 		break;
2060 	}
2061 }
2062 
2063 #define BNXT_GET_EVENT_PORT(data)	\
2064 	((data) &			\
2065 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2066 
2067 #define BNXT_EVENT_RING_TYPE(data2)	\
2068 	((data2) &			\
2069 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2070 
2071 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2072 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2073 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2074 
2075 static int bnxt_async_event_process(struct bnxt *bp,
2076 				    struct hwrm_async_event_cmpl *cmpl)
2077 {
2078 	u16 event_id = le16_to_cpu(cmpl->event_id);
2079 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2080 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2081 
2082 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2083 	switch (event_id) {
2084 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2085 		struct bnxt_link_info *link_info = &bp->link_info;
2086 
2087 		if (BNXT_VF(bp))
2088 			goto async_event_process_exit;
2089 
2090 		/* print unsupported speed warning in forced speed mode only */
2091 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2092 		    (data1 & 0x20000)) {
2093 			u16 fw_speed = link_info->force_link_speed;
2094 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2095 
2096 			if (speed != SPEED_UNKNOWN)
2097 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2098 					    speed);
2099 		}
2100 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2101 	}
2102 		fallthrough;
2103 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2104 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2105 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2106 		fallthrough;
2107 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2108 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2109 		break;
2110 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2111 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2112 		break;
2113 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2114 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2115 
2116 		if (BNXT_VF(bp))
2117 			break;
2118 
2119 		if (bp->pf.port_id != port_id)
2120 			break;
2121 
2122 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2123 		break;
2124 	}
2125 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2126 		if (BNXT_PF(bp))
2127 			goto async_event_process_exit;
2128 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2129 		break;
2130 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2131 		char *fatal_str = "non-fatal";
2132 
2133 		if (!bp->fw_health)
2134 			goto async_event_process_exit;
2135 
2136 		bp->fw_reset_timestamp = jiffies;
2137 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2138 		if (!bp->fw_reset_min_dsecs)
2139 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2140 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2141 		if (!bp->fw_reset_max_dsecs)
2142 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2143 		if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2144 			fatal_str = "fatal";
2145 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2146 		}
2147 		netif_warn(bp, hw, bp->dev,
2148 			   "Firmware %s reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2149 			   fatal_str, data1, data2,
2150 			   bp->fw_reset_min_dsecs * 100,
2151 			   bp->fw_reset_max_dsecs * 100);
2152 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2153 		break;
2154 	}
2155 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2156 		struct bnxt_fw_health *fw_health = bp->fw_health;
2157 
2158 		if (!fw_health)
2159 			goto async_event_process_exit;
2160 
2161 		fw_health->enabled = EVENT_DATA1_RECOVERY_ENABLED(data1);
2162 		fw_health->master = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2163 		if (!fw_health->enabled) {
2164 			netif_info(bp, drv, bp->dev,
2165 				   "Error recovery info: error recovery[0]\n");
2166 			break;
2167 		}
2168 		fw_health->tmr_multiplier =
2169 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2170 				     bp->current_interval * 10);
2171 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2172 		fw_health->last_fw_heartbeat =
2173 			bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2174 		fw_health->last_fw_reset_cnt =
2175 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2176 		netif_info(bp, drv, bp->dev,
2177 			   "Error recovery info: error recovery[1], master[%d], reset count[%u], health status: 0x%x\n",
2178 			   fw_health->master, fw_health->last_fw_reset_cnt,
2179 			   bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG));
2180 		goto async_event_process_exit;
2181 	}
2182 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2183 		netif_notice(bp, hw, bp->dev,
2184 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2185 			     data1, data2);
2186 		goto async_event_process_exit;
2187 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2188 		struct bnxt_rx_ring_info *rxr;
2189 		u16 grp_idx;
2190 
2191 		if (bp->flags & BNXT_FLAG_CHIP_P5)
2192 			goto async_event_process_exit;
2193 
2194 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2195 			    BNXT_EVENT_RING_TYPE(data2), data1);
2196 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2197 			goto async_event_process_exit;
2198 
2199 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2200 		if (grp_idx == INVALID_HW_RING_ID) {
2201 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2202 				    data1);
2203 			goto async_event_process_exit;
2204 		}
2205 		rxr = bp->bnapi[grp_idx]->rx_ring;
2206 		bnxt_sched_reset(bp, rxr);
2207 		goto async_event_process_exit;
2208 	}
2209 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2210 		struct bnxt_fw_health *fw_health = bp->fw_health;
2211 
2212 		netif_notice(bp, hw, bp->dev,
2213 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2214 			     data1, data2);
2215 		if (fw_health) {
2216 			fw_health->echo_req_data1 = data1;
2217 			fw_health->echo_req_data2 = data2;
2218 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2219 			break;
2220 		}
2221 		goto async_event_process_exit;
2222 	}
2223 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2224 		bnxt_ptp_pps_event(bp, data1, data2);
2225 		goto async_event_process_exit;
2226 	}
2227 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2228 		bnxt_event_error_report(bp, data1, data2);
2229 		goto async_event_process_exit;
2230 	}
2231 	default:
2232 		goto async_event_process_exit;
2233 	}
2234 	bnxt_queue_sp_work(bp);
2235 async_event_process_exit:
2236 	bnxt_ulp_async_events(bp, cmpl);
2237 	return 0;
2238 }
2239 
2240 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2241 {
2242 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2243 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2244 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2245 				(struct hwrm_fwd_req_cmpl *)txcmp;
2246 
2247 	switch (cmpl_type) {
2248 	case CMPL_BASE_TYPE_HWRM_DONE:
2249 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2250 		if (seq_id == bp->hwrm_intr_seq_id)
2251 			bp->hwrm_intr_seq_id = (u16)~bp->hwrm_intr_seq_id;
2252 		else
2253 			netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
2254 		break;
2255 
2256 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2257 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2258 
2259 		if ((vf_id < bp->pf.first_vf_id) ||
2260 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2261 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2262 				   vf_id);
2263 			return -EINVAL;
2264 		}
2265 
2266 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2267 		set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
2268 		bnxt_queue_sp_work(bp);
2269 		break;
2270 
2271 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2272 		bnxt_async_event_process(bp,
2273 					 (struct hwrm_async_event_cmpl *)txcmp);
2274 		break;
2275 
2276 	default:
2277 		break;
2278 	}
2279 
2280 	return 0;
2281 }
2282 
2283 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2284 {
2285 	struct bnxt_napi *bnapi = dev_instance;
2286 	struct bnxt *bp = bnapi->bp;
2287 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2288 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2289 
2290 	cpr->event_ctr++;
2291 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2292 	napi_schedule(&bnapi->napi);
2293 	return IRQ_HANDLED;
2294 }
2295 
2296 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2297 {
2298 	u32 raw_cons = cpr->cp_raw_cons;
2299 	u16 cons = RING_CMP(raw_cons);
2300 	struct tx_cmp *txcmp;
2301 
2302 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2303 
2304 	return TX_CMP_VALID(txcmp, raw_cons);
2305 }
2306 
2307 static irqreturn_t bnxt_inta(int irq, void *dev_instance)
2308 {
2309 	struct bnxt_napi *bnapi = dev_instance;
2310 	struct bnxt *bp = bnapi->bp;
2311 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2312 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2313 	u32 int_status;
2314 
2315 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2316 
2317 	if (!bnxt_has_work(bp, cpr)) {
2318 		int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
2319 		/* return if erroneous interrupt */
2320 		if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
2321 			return IRQ_NONE;
2322 	}
2323 
2324 	/* disable ring IRQ */
2325 	BNXT_CP_DB_IRQ_DIS(cpr->cp_db.doorbell);
2326 
2327 	/* Return here if interrupt is shared and is disabled. */
2328 	if (unlikely(atomic_read(&bp->intr_sem) != 0))
2329 		return IRQ_HANDLED;
2330 
2331 	napi_schedule(&bnapi->napi);
2332 	return IRQ_HANDLED;
2333 }
2334 
2335 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2336 			    int budget)
2337 {
2338 	struct bnxt_napi *bnapi = cpr->bnapi;
2339 	u32 raw_cons = cpr->cp_raw_cons;
2340 	u32 cons;
2341 	int tx_pkts = 0;
2342 	int rx_pkts = 0;
2343 	u8 event = 0;
2344 	struct tx_cmp *txcmp;
2345 
2346 	cpr->has_more_work = 0;
2347 	cpr->had_work_done = 1;
2348 	while (1) {
2349 		int rc;
2350 
2351 		cons = RING_CMP(raw_cons);
2352 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2353 
2354 		if (!TX_CMP_VALID(txcmp, raw_cons))
2355 			break;
2356 
2357 		/* The valid test of the entry must be done first before
2358 		 * reading any further.
2359 		 */
2360 		dma_rmb();
2361 		if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
2362 			tx_pkts++;
2363 			/* return full budget so NAPI will complete. */
2364 			if (unlikely(tx_pkts > bp->tx_wake_thresh)) {
2365 				rx_pkts = budget;
2366 				raw_cons = NEXT_RAW_CMP(raw_cons);
2367 				if (budget)
2368 					cpr->has_more_work = 1;
2369 				break;
2370 			}
2371 		} else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
2372 			if (likely(budget))
2373 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2374 			else
2375 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2376 							   &event);
2377 			if (likely(rc >= 0))
2378 				rx_pkts += rc;
2379 			/* Increment rx_pkts when rc is -ENOMEM to count towards
2380 			 * the NAPI budget.  Otherwise, we may potentially loop
2381 			 * here forever if we consistently cannot allocate
2382 			 * buffers.
2383 			 */
2384 			else if (rc == -ENOMEM && budget)
2385 				rx_pkts++;
2386 			else if (rc == -EBUSY)	/* partial completion */
2387 				break;
2388 		} else if (unlikely((TX_CMP_TYPE(txcmp) ==
2389 				     CMPL_BASE_TYPE_HWRM_DONE) ||
2390 				    (TX_CMP_TYPE(txcmp) ==
2391 				     CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
2392 				    (TX_CMP_TYPE(txcmp) ==
2393 				     CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
2394 			bnxt_hwrm_handler(bp, txcmp);
2395 		}
2396 		raw_cons = NEXT_RAW_CMP(raw_cons);
2397 
2398 		if (rx_pkts && rx_pkts == budget) {
2399 			cpr->has_more_work = 1;
2400 			break;
2401 		}
2402 	}
2403 
2404 	if (event & BNXT_REDIRECT_EVENT)
2405 		xdp_do_flush_map();
2406 
2407 	if (event & BNXT_TX_EVENT) {
2408 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
2409 		u16 prod = txr->tx_prod;
2410 
2411 		/* Sync BD data before updating doorbell */
2412 		wmb();
2413 
2414 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
2415 	}
2416 
2417 	cpr->cp_raw_cons = raw_cons;
2418 	bnapi->tx_pkts += tx_pkts;
2419 	bnapi->events |= event;
2420 	return rx_pkts;
2421 }
2422 
2423 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi)
2424 {
2425 	if (bnapi->tx_pkts) {
2426 		bnapi->tx_int(bp, bnapi, bnapi->tx_pkts);
2427 		bnapi->tx_pkts = 0;
2428 	}
2429 
2430 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
2431 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2432 
2433 		if (bnapi->events & BNXT_AGG_EVENT)
2434 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2435 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2436 	}
2437 	bnapi->events = 0;
2438 }
2439 
2440 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2441 			  int budget)
2442 {
2443 	struct bnxt_napi *bnapi = cpr->bnapi;
2444 	int rx_pkts;
2445 
2446 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
2447 
2448 	/* ACK completion ring before freeing tx ring and producing new
2449 	 * buffers in rx/agg rings to prevent overflowing the completion
2450 	 * ring.
2451 	 */
2452 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
2453 
2454 	__bnxt_poll_work_done(bp, bnapi);
2455 	return rx_pkts;
2456 }
2457 
2458 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
2459 {
2460 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2461 	struct bnxt *bp = bnapi->bp;
2462 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2463 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2464 	struct tx_cmp *txcmp;
2465 	struct rx_cmp_ext *rxcmp1;
2466 	u32 cp_cons, tmp_raw_cons;
2467 	u32 raw_cons = cpr->cp_raw_cons;
2468 	u32 rx_pkts = 0;
2469 	u8 event = 0;
2470 
2471 	while (1) {
2472 		int rc;
2473 
2474 		cp_cons = RING_CMP(raw_cons);
2475 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2476 
2477 		if (!TX_CMP_VALID(txcmp, raw_cons))
2478 			break;
2479 
2480 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
2481 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
2482 			cp_cons = RING_CMP(tmp_raw_cons);
2483 			rxcmp1 = (struct rx_cmp_ext *)
2484 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2485 
2486 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2487 				break;
2488 
2489 			/* force an error to recycle the buffer */
2490 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2491 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2492 
2493 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2494 			if (likely(rc == -EIO) && budget)
2495 				rx_pkts++;
2496 			else if (rc == -EBUSY)	/* partial completion */
2497 				break;
2498 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
2499 				    CMPL_BASE_TYPE_HWRM_DONE)) {
2500 			bnxt_hwrm_handler(bp, txcmp);
2501 		} else {
2502 			netdev_err(bp->dev,
2503 				   "Invalid completion received on special ring\n");
2504 		}
2505 		raw_cons = NEXT_RAW_CMP(raw_cons);
2506 
2507 		if (rx_pkts == budget)
2508 			break;
2509 	}
2510 
2511 	cpr->cp_raw_cons = raw_cons;
2512 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
2513 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2514 
2515 	if (event & BNXT_AGG_EVENT)
2516 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2517 
2518 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
2519 		napi_complete_done(napi, rx_pkts);
2520 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2521 	}
2522 	return rx_pkts;
2523 }
2524 
2525 static int bnxt_poll(struct napi_struct *napi, int budget)
2526 {
2527 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2528 	struct bnxt *bp = bnapi->bp;
2529 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2530 	int work_done = 0;
2531 
2532 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
2533 		napi_complete(napi);
2534 		return 0;
2535 	}
2536 	while (1) {
2537 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
2538 
2539 		if (work_done >= budget) {
2540 			if (!budget)
2541 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2542 			break;
2543 		}
2544 
2545 		if (!bnxt_has_work(bp, cpr)) {
2546 			if (napi_complete_done(napi, work_done))
2547 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2548 			break;
2549 		}
2550 	}
2551 	if (bp->flags & BNXT_FLAG_DIM) {
2552 		struct dim_sample dim_sample = {};
2553 
2554 		dim_update_sample(cpr->event_ctr,
2555 				  cpr->rx_packets,
2556 				  cpr->rx_bytes,
2557 				  &dim_sample);
2558 		net_dim(&cpr->dim, dim_sample);
2559 	}
2560 	return work_done;
2561 }
2562 
2563 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
2564 {
2565 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2566 	int i, work_done = 0;
2567 
2568 	for (i = 0; i < 2; i++) {
2569 		struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i];
2570 
2571 		if (cpr2) {
2572 			work_done += __bnxt_poll_work(bp, cpr2,
2573 						      budget - work_done);
2574 			cpr->has_more_work |= cpr2->has_more_work;
2575 		}
2576 	}
2577 	return work_done;
2578 }
2579 
2580 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
2581 				 u64 dbr_type)
2582 {
2583 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2584 	int i;
2585 
2586 	for (i = 0; i < 2; i++) {
2587 		struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i];
2588 		struct bnxt_db_info *db;
2589 
2590 		if (cpr2 && cpr2->had_work_done) {
2591 			db = &cpr2->cp_db;
2592 			writeq(db->db_key64 | dbr_type |
2593 			       RING_CMP(cpr2->cp_raw_cons), db->doorbell);
2594 			cpr2->had_work_done = 0;
2595 		}
2596 	}
2597 	__bnxt_poll_work_done(bp, bnapi);
2598 }
2599 
2600 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
2601 {
2602 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2603 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2604 	u32 raw_cons = cpr->cp_raw_cons;
2605 	struct bnxt *bp = bnapi->bp;
2606 	struct nqe_cn *nqcmp;
2607 	int work_done = 0;
2608 	u32 cons;
2609 
2610 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
2611 		napi_complete(napi);
2612 		return 0;
2613 	}
2614 	if (cpr->has_more_work) {
2615 		cpr->has_more_work = 0;
2616 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
2617 	}
2618 	while (1) {
2619 		cons = RING_CMP(raw_cons);
2620 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2621 
2622 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
2623 			if (cpr->has_more_work)
2624 				break;
2625 
2626 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL);
2627 			cpr->cp_raw_cons = raw_cons;
2628 			if (napi_complete_done(napi, work_done))
2629 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
2630 						  cpr->cp_raw_cons);
2631 			return work_done;
2632 		}
2633 
2634 		/* The valid test of the entry must be done first before
2635 		 * reading any further.
2636 		 */
2637 		dma_rmb();
2638 
2639 		if (nqcmp->type == cpu_to_le16(NQ_CN_TYPE_CQ_NOTIFICATION)) {
2640 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
2641 			struct bnxt_cp_ring_info *cpr2;
2642 
2643 			cpr2 = cpr->cp_ring_arr[idx];
2644 			work_done += __bnxt_poll_work(bp, cpr2,
2645 						      budget - work_done);
2646 			cpr->has_more_work |= cpr2->has_more_work;
2647 		} else {
2648 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
2649 		}
2650 		raw_cons = NEXT_RAW_CMP(raw_cons);
2651 	}
2652 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ);
2653 	if (raw_cons != cpr->cp_raw_cons) {
2654 		cpr->cp_raw_cons = raw_cons;
2655 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
2656 	}
2657 	return work_done;
2658 }
2659 
2660 static void bnxt_free_tx_skbs(struct bnxt *bp)
2661 {
2662 	int i, max_idx;
2663 	struct pci_dev *pdev = bp->pdev;
2664 
2665 	if (!bp->tx_ring)
2666 		return;
2667 
2668 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
2669 	for (i = 0; i < bp->tx_nr_rings; i++) {
2670 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2671 		int j;
2672 
2673 		for (j = 0; j < max_idx;) {
2674 			struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
2675 			struct sk_buff *skb;
2676 			int k, last;
2677 
2678 			if (i < bp->tx_nr_rings_xdp &&
2679 			    tx_buf->action == XDP_REDIRECT) {
2680 				dma_unmap_single(&pdev->dev,
2681 					dma_unmap_addr(tx_buf, mapping),
2682 					dma_unmap_len(tx_buf, len),
2683 					PCI_DMA_TODEVICE);
2684 				xdp_return_frame(tx_buf->xdpf);
2685 				tx_buf->action = 0;
2686 				tx_buf->xdpf = NULL;
2687 				j++;
2688 				continue;
2689 			}
2690 
2691 			skb = tx_buf->skb;
2692 			if (!skb) {
2693 				j++;
2694 				continue;
2695 			}
2696 
2697 			tx_buf->skb = NULL;
2698 
2699 			if (tx_buf->is_push) {
2700 				dev_kfree_skb(skb);
2701 				j += 2;
2702 				continue;
2703 			}
2704 
2705 			dma_unmap_single(&pdev->dev,
2706 					 dma_unmap_addr(tx_buf, mapping),
2707 					 skb_headlen(skb),
2708 					 PCI_DMA_TODEVICE);
2709 
2710 			last = tx_buf->nr_frags;
2711 			j += 2;
2712 			for (k = 0; k < last; k++, j++) {
2713 				int ring_idx = j & bp->tx_ring_mask;
2714 				skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2715 
2716 				tx_buf = &txr->tx_buf_ring[ring_idx];
2717 				dma_unmap_page(
2718 					&pdev->dev,
2719 					dma_unmap_addr(tx_buf, mapping),
2720 					skb_frag_size(frag), PCI_DMA_TODEVICE);
2721 			}
2722 			dev_kfree_skb(skb);
2723 		}
2724 		netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
2725 	}
2726 }
2727 
2728 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, int ring_nr)
2729 {
2730 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
2731 	struct pci_dev *pdev = bp->pdev;
2732 	struct bnxt_tpa_idx_map *map;
2733 	int i, max_idx, max_agg_idx;
2734 
2735 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
2736 	max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
2737 	if (!rxr->rx_tpa)
2738 		goto skip_rx_tpa_free;
2739 
2740 	for (i = 0; i < bp->max_tpa; i++) {
2741 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
2742 		u8 *data = tpa_info->data;
2743 
2744 		if (!data)
2745 			continue;
2746 
2747 		dma_unmap_single_attrs(&pdev->dev, tpa_info->mapping,
2748 				       bp->rx_buf_use_size, bp->rx_dir,
2749 				       DMA_ATTR_WEAK_ORDERING);
2750 
2751 		tpa_info->data = NULL;
2752 
2753 		kfree(data);
2754 	}
2755 
2756 skip_rx_tpa_free:
2757 	for (i = 0; i < max_idx; i++) {
2758 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
2759 		dma_addr_t mapping = rx_buf->mapping;
2760 		void *data = rx_buf->data;
2761 
2762 		if (!data)
2763 			continue;
2764 
2765 		rx_buf->data = NULL;
2766 		if (BNXT_RX_PAGE_MODE(bp)) {
2767 			mapping -= bp->rx_dma_offset;
2768 			dma_unmap_page_attrs(&pdev->dev, mapping, PAGE_SIZE,
2769 					     bp->rx_dir,
2770 					     DMA_ATTR_WEAK_ORDERING);
2771 			page_pool_recycle_direct(rxr->page_pool, data);
2772 		} else {
2773 			dma_unmap_single_attrs(&pdev->dev, mapping,
2774 					       bp->rx_buf_use_size, bp->rx_dir,
2775 					       DMA_ATTR_WEAK_ORDERING);
2776 			kfree(data);
2777 		}
2778 	}
2779 	for (i = 0; i < max_agg_idx; i++) {
2780 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
2781 		struct page *page = rx_agg_buf->page;
2782 
2783 		if (!page)
2784 			continue;
2785 
2786 		dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
2787 				     BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
2788 				     DMA_ATTR_WEAK_ORDERING);
2789 
2790 		rx_agg_buf->page = NULL;
2791 		__clear_bit(i, rxr->rx_agg_bmap);
2792 
2793 		__free_page(page);
2794 	}
2795 	if (rxr->rx_page) {
2796 		__free_page(rxr->rx_page);
2797 		rxr->rx_page = NULL;
2798 	}
2799 	map = rxr->rx_tpa_idx_map;
2800 	if (map)
2801 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
2802 }
2803 
2804 static void bnxt_free_rx_skbs(struct bnxt *bp)
2805 {
2806 	int i;
2807 
2808 	if (!bp->rx_ring)
2809 		return;
2810 
2811 	for (i = 0; i < bp->rx_nr_rings; i++)
2812 		bnxt_free_one_rx_ring_skbs(bp, i);
2813 }
2814 
2815 static void bnxt_free_skbs(struct bnxt *bp)
2816 {
2817 	bnxt_free_tx_skbs(bp);
2818 	bnxt_free_rx_skbs(bp);
2819 }
2820 
2821 static void bnxt_init_ctx_mem(struct bnxt_mem_init *mem_init, void *p, int len)
2822 {
2823 	u8 init_val = mem_init->init_val;
2824 	u16 offset = mem_init->offset;
2825 	u8 *p2 = p;
2826 	int i;
2827 
2828 	if (!init_val)
2829 		return;
2830 	if (offset == BNXT_MEM_INVALID_OFFSET) {
2831 		memset(p, init_val, len);
2832 		return;
2833 	}
2834 	for (i = 0; i < len; i += mem_init->size)
2835 		*(p2 + i + offset) = init_val;
2836 }
2837 
2838 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
2839 {
2840 	struct pci_dev *pdev = bp->pdev;
2841 	int i;
2842 
2843 	for (i = 0; i < rmem->nr_pages; i++) {
2844 		if (!rmem->pg_arr[i])
2845 			continue;
2846 
2847 		dma_free_coherent(&pdev->dev, rmem->page_size,
2848 				  rmem->pg_arr[i], rmem->dma_arr[i]);
2849 
2850 		rmem->pg_arr[i] = NULL;
2851 	}
2852 	if (rmem->pg_tbl) {
2853 		size_t pg_tbl_size = rmem->nr_pages * 8;
2854 
2855 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
2856 			pg_tbl_size = rmem->page_size;
2857 		dma_free_coherent(&pdev->dev, pg_tbl_size,
2858 				  rmem->pg_tbl, rmem->pg_tbl_map);
2859 		rmem->pg_tbl = NULL;
2860 	}
2861 	if (rmem->vmem_size && *rmem->vmem) {
2862 		vfree(*rmem->vmem);
2863 		*rmem->vmem = NULL;
2864 	}
2865 }
2866 
2867 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
2868 {
2869 	struct pci_dev *pdev = bp->pdev;
2870 	u64 valid_bit = 0;
2871 	int i;
2872 
2873 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
2874 		valid_bit = PTU_PTE_VALID;
2875 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
2876 		size_t pg_tbl_size = rmem->nr_pages * 8;
2877 
2878 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
2879 			pg_tbl_size = rmem->page_size;
2880 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
2881 						  &rmem->pg_tbl_map,
2882 						  GFP_KERNEL);
2883 		if (!rmem->pg_tbl)
2884 			return -ENOMEM;
2885 	}
2886 
2887 	for (i = 0; i < rmem->nr_pages; i++) {
2888 		u64 extra_bits = valid_bit;
2889 
2890 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2891 						     rmem->page_size,
2892 						     &rmem->dma_arr[i],
2893 						     GFP_KERNEL);
2894 		if (!rmem->pg_arr[i])
2895 			return -ENOMEM;
2896 
2897 		if (rmem->mem_init)
2898 			bnxt_init_ctx_mem(rmem->mem_init, rmem->pg_arr[i],
2899 					  rmem->page_size);
2900 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
2901 			if (i == rmem->nr_pages - 2 &&
2902 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
2903 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
2904 			else if (i == rmem->nr_pages - 1 &&
2905 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
2906 				extra_bits |= PTU_PTE_LAST;
2907 			rmem->pg_tbl[i] =
2908 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
2909 		}
2910 	}
2911 
2912 	if (rmem->vmem_size) {
2913 		*rmem->vmem = vzalloc(rmem->vmem_size);
2914 		if (!(*rmem->vmem))
2915 			return -ENOMEM;
2916 	}
2917 	return 0;
2918 }
2919 
2920 static void bnxt_free_tpa_info(struct bnxt *bp)
2921 {
2922 	int i;
2923 
2924 	for (i = 0; i < bp->rx_nr_rings; i++) {
2925 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2926 
2927 		kfree(rxr->rx_tpa_idx_map);
2928 		rxr->rx_tpa_idx_map = NULL;
2929 		if (rxr->rx_tpa) {
2930 			kfree(rxr->rx_tpa[0].agg_arr);
2931 			rxr->rx_tpa[0].agg_arr = NULL;
2932 		}
2933 		kfree(rxr->rx_tpa);
2934 		rxr->rx_tpa = NULL;
2935 	}
2936 }
2937 
2938 static int bnxt_alloc_tpa_info(struct bnxt *bp)
2939 {
2940 	int i, j, total_aggs = 0;
2941 
2942 	bp->max_tpa = MAX_TPA;
2943 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
2944 		if (!bp->max_tpa_v2)
2945 			return 0;
2946 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
2947 		total_aggs = bp->max_tpa * MAX_SKB_FRAGS;
2948 	}
2949 
2950 	for (i = 0; i < bp->rx_nr_rings; i++) {
2951 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2952 		struct rx_agg_cmp *agg;
2953 
2954 		rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
2955 				      GFP_KERNEL);
2956 		if (!rxr->rx_tpa)
2957 			return -ENOMEM;
2958 
2959 		if (!(bp->flags & BNXT_FLAG_CHIP_P5))
2960 			continue;
2961 		agg = kcalloc(total_aggs, sizeof(*agg), GFP_KERNEL);
2962 		rxr->rx_tpa[0].agg_arr = agg;
2963 		if (!agg)
2964 			return -ENOMEM;
2965 		for (j = 1; j < bp->max_tpa; j++)
2966 			rxr->rx_tpa[j].agg_arr = agg + j * MAX_SKB_FRAGS;
2967 		rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
2968 					      GFP_KERNEL);
2969 		if (!rxr->rx_tpa_idx_map)
2970 			return -ENOMEM;
2971 	}
2972 	return 0;
2973 }
2974 
2975 static void bnxt_free_rx_rings(struct bnxt *bp)
2976 {
2977 	int i;
2978 
2979 	if (!bp->rx_ring)
2980 		return;
2981 
2982 	bnxt_free_tpa_info(bp);
2983 	for (i = 0; i < bp->rx_nr_rings; i++) {
2984 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2985 		struct bnxt_ring_struct *ring;
2986 
2987 		if (rxr->xdp_prog)
2988 			bpf_prog_put(rxr->xdp_prog);
2989 
2990 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
2991 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
2992 
2993 		page_pool_destroy(rxr->page_pool);
2994 		rxr->page_pool = NULL;
2995 
2996 		kfree(rxr->rx_agg_bmap);
2997 		rxr->rx_agg_bmap = NULL;
2998 
2999 		ring = &rxr->rx_ring_struct;
3000 		bnxt_free_ring(bp, &ring->ring_mem);
3001 
3002 		ring = &rxr->rx_agg_ring_struct;
3003 		bnxt_free_ring(bp, &ring->ring_mem);
3004 	}
3005 }
3006 
3007 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3008 				   struct bnxt_rx_ring_info *rxr)
3009 {
3010 	struct page_pool_params pp = { 0 };
3011 
3012 	pp.pool_size = bp->rx_ring_size;
3013 	pp.nid = dev_to_node(&bp->pdev->dev);
3014 	pp.dev = &bp->pdev->dev;
3015 	pp.dma_dir = DMA_BIDIRECTIONAL;
3016 
3017 	rxr->page_pool = page_pool_create(&pp);
3018 	if (IS_ERR(rxr->page_pool)) {
3019 		int err = PTR_ERR(rxr->page_pool);
3020 
3021 		rxr->page_pool = NULL;
3022 		return err;
3023 	}
3024 	return 0;
3025 }
3026 
3027 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3028 {
3029 	int i, rc = 0, agg_rings = 0;
3030 
3031 	if (!bp->rx_ring)
3032 		return -ENOMEM;
3033 
3034 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3035 		agg_rings = 1;
3036 
3037 	for (i = 0; i < bp->rx_nr_rings; i++) {
3038 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3039 		struct bnxt_ring_struct *ring;
3040 
3041 		ring = &rxr->rx_ring_struct;
3042 
3043 		rc = bnxt_alloc_rx_page_pool(bp, rxr);
3044 		if (rc)
3045 			return rc;
3046 
3047 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3048 		if (rc < 0)
3049 			return rc;
3050 
3051 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3052 						MEM_TYPE_PAGE_POOL,
3053 						rxr->page_pool);
3054 		if (rc) {
3055 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3056 			return rc;
3057 		}
3058 
3059 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3060 		if (rc)
3061 			return rc;
3062 
3063 		ring->grp_idx = i;
3064 		if (agg_rings) {
3065 			u16 mem_size;
3066 
3067 			ring = &rxr->rx_agg_ring_struct;
3068 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3069 			if (rc)
3070 				return rc;
3071 
3072 			ring->grp_idx = i;
3073 			rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3074 			mem_size = rxr->rx_agg_bmap_size / 8;
3075 			rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3076 			if (!rxr->rx_agg_bmap)
3077 				return -ENOMEM;
3078 		}
3079 	}
3080 	if (bp->flags & BNXT_FLAG_TPA)
3081 		rc = bnxt_alloc_tpa_info(bp);
3082 	return rc;
3083 }
3084 
3085 static void bnxt_free_tx_rings(struct bnxt *bp)
3086 {
3087 	int i;
3088 	struct pci_dev *pdev = bp->pdev;
3089 
3090 	if (!bp->tx_ring)
3091 		return;
3092 
3093 	for (i = 0; i < bp->tx_nr_rings; i++) {
3094 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3095 		struct bnxt_ring_struct *ring;
3096 
3097 		if (txr->tx_push) {
3098 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3099 					  txr->tx_push, txr->tx_push_mapping);
3100 			txr->tx_push = NULL;
3101 		}
3102 
3103 		ring = &txr->tx_ring_struct;
3104 
3105 		bnxt_free_ring(bp, &ring->ring_mem);
3106 	}
3107 }
3108 
3109 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3110 {
3111 	int i, j, rc;
3112 	struct pci_dev *pdev = bp->pdev;
3113 
3114 	bp->tx_push_size = 0;
3115 	if (bp->tx_push_thresh) {
3116 		int push_size;
3117 
3118 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3119 					bp->tx_push_thresh);
3120 
3121 		if (push_size > 256) {
3122 			push_size = 0;
3123 			bp->tx_push_thresh = 0;
3124 		}
3125 
3126 		bp->tx_push_size = push_size;
3127 	}
3128 
3129 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3130 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3131 		struct bnxt_ring_struct *ring;
3132 		u8 qidx;
3133 
3134 		ring = &txr->tx_ring_struct;
3135 
3136 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3137 		if (rc)
3138 			return rc;
3139 
3140 		ring->grp_idx = txr->bnapi->index;
3141 		if (bp->tx_push_size) {
3142 			dma_addr_t mapping;
3143 
3144 			/* One pre-allocated DMA buffer to backup
3145 			 * TX push operation
3146 			 */
3147 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3148 						bp->tx_push_size,
3149 						&txr->tx_push_mapping,
3150 						GFP_KERNEL);
3151 
3152 			if (!txr->tx_push)
3153 				return -ENOMEM;
3154 
3155 			mapping = txr->tx_push_mapping +
3156 				sizeof(struct tx_push_bd);
3157 			txr->data_mapping = cpu_to_le64(mapping);
3158 		}
3159 		qidx = bp->tc_to_qidx[j];
3160 		ring->queue_id = bp->q_info[qidx].queue_id;
3161 		if (i < bp->tx_nr_rings_xdp)
3162 			continue;
3163 		if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
3164 			j++;
3165 	}
3166 	return 0;
3167 }
3168 
3169 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
3170 {
3171 	kfree(cpr->cp_desc_ring);
3172 	cpr->cp_desc_ring = NULL;
3173 	kfree(cpr->cp_desc_mapping);
3174 	cpr->cp_desc_mapping = NULL;
3175 }
3176 
3177 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
3178 {
3179 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
3180 	if (!cpr->cp_desc_ring)
3181 		return -ENOMEM;
3182 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
3183 				       GFP_KERNEL);
3184 	if (!cpr->cp_desc_mapping)
3185 		return -ENOMEM;
3186 	return 0;
3187 }
3188 
3189 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
3190 {
3191 	int i;
3192 
3193 	if (!bp->bnapi)
3194 		return;
3195 	for (i = 0; i < bp->cp_nr_rings; i++) {
3196 		struct bnxt_napi *bnapi = bp->bnapi[i];
3197 
3198 		if (!bnapi)
3199 			continue;
3200 		bnxt_free_cp_arrays(&bnapi->cp_ring);
3201 	}
3202 }
3203 
3204 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
3205 {
3206 	int i, n = bp->cp_nr_pages;
3207 
3208 	for (i = 0; i < bp->cp_nr_rings; i++) {
3209 		struct bnxt_napi *bnapi = bp->bnapi[i];
3210 		int rc;
3211 
3212 		if (!bnapi)
3213 			continue;
3214 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
3215 		if (rc)
3216 			return rc;
3217 	}
3218 	return 0;
3219 }
3220 
3221 static void bnxt_free_cp_rings(struct bnxt *bp)
3222 {
3223 	int i;
3224 
3225 	if (!bp->bnapi)
3226 		return;
3227 
3228 	for (i = 0; i < bp->cp_nr_rings; i++) {
3229 		struct bnxt_napi *bnapi = bp->bnapi[i];
3230 		struct bnxt_cp_ring_info *cpr;
3231 		struct bnxt_ring_struct *ring;
3232 		int j;
3233 
3234 		if (!bnapi)
3235 			continue;
3236 
3237 		cpr = &bnapi->cp_ring;
3238 		ring = &cpr->cp_ring_struct;
3239 
3240 		bnxt_free_ring(bp, &ring->ring_mem);
3241 
3242 		for (j = 0; j < 2; j++) {
3243 			struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
3244 
3245 			if (cpr2) {
3246 				ring = &cpr2->cp_ring_struct;
3247 				bnxt_free_ring(bp, &ring->ring_mem);
3248 				bnxt_free_cp_arrays(cpr2);
3249 				kfree(cpr2);
3250 				cpr->cp_ring_arr[j] = NULL;
3251 			}
3252 		}
3253 	}
3254 }
3255 
3256 static struct bnxt_cp_ring_info *bnxt_alloc_cp_sub_ring(struct bnxt *bp)
3257 {
3258 	struct bnxt_ring_mem_info *rmem;
3259 	struct bnxt_ring_struct *ring;
3260 	struct bnxt_cp_ring_info *cpr;
3261 	int rc;
3262 
3263 	cpr = kzalloc(sizeof(*cpr), GFP_KERNEL);
3264 	if (!cpr)
3265 		return NULL;
3266 
3267 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
3268 	if (rc) {
3269 		bnxt_free_cp_arrays(cpr);
3270 		kfree(cpr);
3271 		return NULL;
3272 	}
3273 	ring = &cpr->cp_ring_struct;
3274 	rmem = &ring->ring_mem;
3275 	rmem->nr_pages = bp->cp_nr_pages;
3276 	rmem->page_size = HW_CMPD_RING_SIZE;
3277 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
3278 	rmem->dma_arr = cpr->cp_desc_mapping;
3279 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
3280 	rc = bnxt_alloc_ring(bp, rmem);
3281 	if (rc) {
3282 		bnxt_free_ring(bp, rmem);
3283 		bnxt_free_cp_arrays(cpr);
3284 		kfree(cpr);
3285 		cpr = NULL;
3286 	}
3287 	return cpr;
3288 }
3289 
3290 static int bnxt_alloc_cp_rings(struct bnxt *bp)
3291 {
3292 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
3293 	int i, rc, ulp_base_vec, ulp_msix;
3294 
3295 	ulp_msix = bnxt_get_ulp_msix_num(bp);
3296 	ulp_base_vec = bnxt_get_ulp_msix_base(bp);
3297 	for (i = 0; i < bp->cp_nr_rings; i++) {
3298 		struct bnxt_napi *bnapi = bp->bnapi[i];
3299 		struct bnxt_cp_ring_info *cpr;
3300 		struct bnxt_ring_struct *ring;
3301 
3302 		if (!bnapi)
3303 			continue;
3304 
3305 		cpr = &bnapi->cp_ring;
3306 		cpr->bnapi = bnapi;
3307 		ring = &cpr->cp_ring_struct;
3308 
3309 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3310 		if (rc)
3311 			return rc;
3312 
3313 		if (ulp_msix && i >= ulp_base_vec)
3314 			ring->map_idx = i + ulp_msix;
3315 		else
3316 			ring->map_idx = i;
3317 
3318 		if (!(bp->flags & BNXT_FLAG_CHIP_P5))
3319 			continue;
3320 
3321 		if (i < bp->rx_nr_rings) {
3322 			struct bnxt_cp_ring_info *cpr2 =
3323 				bnxt_alloc_cp_sub_ring(bp);
3324 
3325 			cpr->cp_ring_arr[BNXT_RX_HDL] = cpr2;
3326 			if (!cpr2)
3327 				return -ENOMEM;
3328 			cpr2->bnapi = bnapi;
3329 		}
3330 		if ((sh && i < bp->tx_nr_rings) ||
3331 		    (!sh && i >= bp->rx_nr_rings)) {
3332 			struct bnxt_cp_ring_info *cpr2 =
3333 				bnxt_alloc_cp_sub_ring(bp);
3334 
3335 			cpr->cp_ring_arr[BNXT_TX_HDL] = cpr2;
3336 			if (!cpr2)
3337 				return -ENOMEM;
3338 			cpr2->bnapi = bnapi;
3339 		}
3340 	}
3341 	return 0;
3342 }
3343 
3344 static void bnxt_init_ring_struct(struct bnxt *bp)
3345 {
3346 	int i;
3347 
3348 	for (i = 0; i < bp->cp_nr_rings; i++) {
3349 		struct bnxt_napi *bnapi = bp->bnapi[i];
3350 		struct bnxt_ring_mem_info *rmem;
3351 		struct bnxt_cp_ring_info *cpr;
3352 		struct bnxt_rx_ring_info *rxr;
3353 		struct bnxt_tx_ring_info *txr;
3354 		struct bnxt_ring_struct *ring;
3355 
3356 		if (!bnapi)
3357 			continue;
3358 
3359 		cpr = &bnapi->cp_ring;
3360 		ring = &cpr->cp_ring_struct;
3361 		rmem = &ring->ring_mem;
3362 		rmem->nr_pages = bp->cp_nr_pages;
3363 		rmem->page_size = HW_CMPD_RING_SIZE;
3364 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
3365 		rmem->dma_arr = cpr->cp_desc_mapping;
3366 		rmem->vmem_size = 0;
3367 
3368 		rxr = bnapi->rx_ring;
3369 		if (!rxr)
3370 			goto skip_rx;
3371 
3372 		ring = &rxr->rx_ring_struct;
3373 		rmem = &ring->ring_mem;
3374 		rmem->nr_pages = bp->rx_nr_pages;
3375 		rmem->page_size = HW_RXBD_RING_SIZE;
3376 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
3377 		rmem->dma_arr = rxr->rx_desc_mapping;
3378 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
3379 		rmem->vmem = (void **)&rxr->rx_buf_ring;
3380 
3381 		ring = &rxr->rx_agg_ring_struct;
3382 		rmem = &ring->ring_mem;
3383 		rmem->nr_pages = bp->rx_agg_nr_pages;
3384 		rmem->page_size = HW_RXBD_RING_SIZE;
3385 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
3386 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
3387 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
3388 		rmem->vmem = (void **)&rxr->rx_agg_ring;
3389 
3390 skip_rx:
3391 		txr = bnapi->tx_ring;
3392 		if (!txr)
3393 			continue;
3394 
3395 		ring = &txr->tx_ring_struct;
3396 		rmem = &ring->ring_mem;
3397 		rmem->nr_pages = bp->tx_nr_pages;
3398 		rmem->page_size = HW_RXBD_RING_SIZE;
3399 		rmem->pg_arr = (void **)txr->tx_desc_ring;
3400 		rmem->dma_arr = txr->tx_desc_mapping;
3401 		rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
3402 		rmem->vmem = (void **)&txr->tx_buf_ring;
3403 	}
3404 }
3405 
3406 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
3407 {
3408 	int i;
3409 	u32 prod;
3410 	struct rx_bd **rx_buf_ring;
3411 
3412 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
3413 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
3414 		int j;
3415 		struct rx_bd *rxbd;
3416 
3417 		rxbd = rx_buf_ring[i];
3418 		if (!rxbd)
3419 			continue;
3420 
3421 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
3422 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
3423 			rxbd->rx_bd_opaque = prod;
3424 		}
3425 	}
3426 }
3427 
3428 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
3429 {
3430 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
3431 	struct net_device *dev = bp->dev;
3432 	u32 prod;
3433 	int i;
3434 
3435 	prod = rxr->rx_prod;
3436 	for (i = 0; i < bp->rx_ring_size; i++) {
3437 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
3438 			netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
3439 				    ring_nr, i, bp->rx_ring_size);
3440 			break;
3441 		}
3442 		prod = NEXT_RX(prod);
3443 	}
3444 	rxr->rx_prod = prod;
3445 
3446 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
3447 		return 0;
3448 
3449 	prod = rxr->rx_agg_prod;
3450 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
3451 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
3452 			netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
3453 				    ring_nr, i, bp->rx_ring_size);
3454 			break;
3455 		}
3456 		prod = NEXT_RX_AGG(prod);
3457 	}
3458 	rxr->rx_agg_prod = prod;
3459 
3460 	if (rxr->rx_tpa) {
3461 		dma_addr_t mapping;
3462 		u8 *data;
3463 
3464 		for (i = 0; i < bp->max_tpa; i++) {
3465 			data = __bnxt_alloc_rx_data(bp, &mapping, GFP_KERNEL);
3466 			if (!data)
3467 				return -ENOMEM;
3468 
3469 			rxr->rx_tpa[i].data = data;
3470 			rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
3471 			rxr->rx_tpa[i].mapping = mapping;
3472 		}
3473 	}
3474 	return 0;
3475 }
3476 
3477 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
3478 {
3479 	struct bnxt_rx_ring_info *rxr;
3480 	struct bnxt_ring_struct *ring;
3481 	u32 type;
3482 
3483 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
3484 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
3485 
3486 	if (NET_IP_ALIGN == 2)
3487 		type |= RX_BD_FLAGS_SOP;
3488 
3489 	rxr = &bp->rx_ring[ring_nr];
3490 	ring = &rxr->rx_ring_struct;
3491 	bnxt_init_rxbd_pages(ring, type);
3492 
3493 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
3494 		bpf_prog_add(bp->xdp_prog, 1);
3495 		rxr->xdp_prog = bp->xdp_prog;
3496 	}
3497 	ring->fw_ring_id = INVALID_HW_RING_ID;
3498 
3499 	ring = &rxr->rx_agg_ring_struct;
3500 	ring->fw_ring_id = INVALID_HW_RING_ID;
3501 
3502 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
3503 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
3504 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
3505 
3506 		bnxt_init_rxbd_pages(ring, type);
3507 	}
3508 
3509 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
3510 }
3511 
3512 static void bnxt_init_cp_rings(struct bnxt *bp)
3513 {
3514 	int i, j;
3515 
3516 	for (i = 0; i < bp->cp_nr_rings; i++) {
3517 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
3518 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3519 
3520 		ring->fw_ring_id = INVALID_HW_RING_ID;
3521 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
3522 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
3523 		for (j = 0; j < 2; j++) {
3524 			struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
3525 
3526 			if (!cpr2)
3527 				continue;
3528 
3529 			ring = &cpr2->cp_ring_struct;
3530 			ring->fw_ring_id = INVALID_HW_RING_ID;
3531 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
3532 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
3533 		}
3534 	}
3535 }
3536 
3537 static int bnxt_init_rx_rings(struct bnxt *bp)
3538 {
3539 	int i, rc = 0;
3540 
3541 	if (BNXT_RX_PAGE_MODE(bp)) {
3542 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
3543 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
3544 	} else {
3545 		bp->rx_offset = BNXT_RX_OFFSET;
3546 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
3547 	}
3548 
3549 	for (i = 0; i < bp->rx_nr_rings; i++) {
3550 		rc = bnxt_init_one_rx_ring(bp, i);
3551 		if (rc)
3552 			break;
3553 	}
3554 
3555 	return rc;
3556 }
3557 
3558 static int bnxt_init_tx_rings(struct bnxt *bp)
3559 {
3560 	u16 i;
3561 
3562 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
3563 				   MAX_SKB_FRAGS + 1);
3564 
3565 	for (i = 0; i < bp->tx_nr_rings; i++) {
3566 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3567 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
3568 
3569 		ring->fw_ring_id = INVALID_HW_RING_ID;
3570 	}
3571 
3572 	return 0;
3573 }
3574 
3575 static void bnxt_free_ring_grps(struct bnxt *bp)
3576 {
3577 	kfree(bp->grp_info);
3578 	bp->grp_info = NULL;
3579 }
3580 
3581 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
3582 {
3583 	int i;
3584 
3585 	if (irq_re_init) {
3586 		bp->grp_info = kcalloc(bp->cp_nr_rings,
3587 				       sizeof(struct bnxt_ring_grp_info),
3588 				       GFP_KERNEL);
3589 		if (!bp->grp_info)
3590 			return -ENOMEM;
3591 	}
3592 	for (i = 0; i < bp->cp_nr_rings; i++) {
3593 		if (irq_re_init)
3594 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
3595 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
3596 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
3597 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
3598 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
3599 	}
3600 	return 0;
3601 }
3602 
3603 static void bnxt_free_vnics(struct bnxt *bp)
3604 {
3605 	kfree(bp->vnic_info);
3606 	bp->vnic_info = NULL;
3607 	bp->nr_vnics = 0;
3608 }
3609 
3610 static int bnxt_alloc_vnics(struct bnxt *bp)
3611 {
3612 	int num_vnics = 1;
3613 
3614 #ifdef CONFIG_RFS_ACCEL
3615 	if ((bp->flags & (BNXT_FLAG_RFS | BNXT_FLAG_CHIP_P5)) == BNXT_FLAG_RFS)
3616 		num_vnics += bp->rx_nr_rings;
3617 #endif
3618 
3619 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3620 		num_vnics++;
3621 
3622 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
3623 				GFP_KERNEL);
3624 	if (!bp->vnic_info)
3625 		return -ENOMEM;
3626 
3627 	bp->nr_vnics = num_vnics;
3628 	return 0;
3629 }
3630 
3631 static void bnxt_init_vnics(struct bnxt *bp)
3632 {
3633 	int i;
3634 
3635 	for (i = 0; i < bp->nr_vnics; i++) {
3636 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3637 		int j;
3638 
3639 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
3640 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
3641 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
3642 
3643 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
3644 
3645 		if (bp->vnic_info[i].rss_hash_key) {
3646 			if (i == 0)
3647 				prandom_bytes(vnic->rss_hash_key,
3648 					      HW_HASH_KEY_SIZE);
3649 			else
3650 				memcpy(vnic->rss_hash_key,
3651 				       bp->vnic_info[0].rss_hash_key,
3652 				       HW_HASH_KEY_SIZE);
3653 		}
3654 	}
3655 }
3656 
3657 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
3658 {
3659 	int pages;
3660 
3661 	pages = ring_size / desc_per_pg;
3662 
3663 	if (!pages)
3664 		return 1;
3665 
3666 	pages++;
3667 
3668 	while (pages & (pages - 1))
3669 		pages++;
3670 
3671 	return pages;
3672 }
3673 
3674 void bnxt_set_tpa_flags(struct bnxt *bp)
3675 {
3676 	bp->flags &= ~BNXT_FLAG_TPA;
3677 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
3678 		return;
3679 	if (bp->dev->features & NETIF_F_LRO)
3680 		bp->flags |= BNXT_FLAG_LRO;
3681 	else if (bp->dev->features & NETIF_F_GRO_HW)
3682 		bp->flags |= BNXT_FLAG_GRO;
3683 }
3684 
3685 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
3686  * be set on entry.
3687  */
3688 void bnxt_set_ring_params(struct bnxt *bp)
3689 {
3690 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
3691 	u32 agg_factor = 0, agg_ring_size = 0;
3692 
3693 	/* 8 for CRC and VLAN */
3694 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
3695 
3696 	rx_space = rx_size + NET_SKB_PAD +
3697 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3698 
3699 	bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
3700 	ring_size = bp->rx_ring_size;
3701 	bp->rx_agg_ring_size = 0;
3702 	bp->rx_agg_nr_pages = 0;
3703 
3704 	if (bp->flags & BNXT_FLAG_TPA)
3705 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
3706 
3707 	bp->flags &= ~BNXT_FLAG_JUMBO;
3708 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
3709 		u32 jumbo_factor;
3710 
3711 		bp->flags |= BNXT_FLAG_JUMBO;
3712 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
3713 		if (jumbo_factor > agg_factor)
3714 			agg_factor = jumbo_factor;
3715 	}
3716 	if (agg_factor) {
3717 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
3718 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
3719 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
3720 				    bp->rx_ring_size, ring_size);
3721 			bp->rx_ring_size = ring_size;
3722 		}
3723 		agg_ring_size = ring_size * agg_factor;
3724 
3725 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
3726 							RX_DESC_CNT);
3727 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
3728 			u32 tmp = agg_ring_size;
3729 
3730 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
3731 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
3732 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
3733 				    tmp, agg_ring_size);
3734 		}
3735 		bp->rx_agg_ring_size = agg_ring_size;
3736 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
3737 		rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
3738 		rx_space = rx_size + NET_SKB_PAD +
3739 			SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3740 	}
3741 
3742 	bp->rx_buf_use_size = rx_size;
3743 	bp->rx_buf_size = rx_space;
3744 
3745 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
3746 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
3747 
3748 	ring_size = bp->tx_ring_size;
3749 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
3750 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
3751 
3752 	max_rx_cmpl = bp->rx_ring_size;
3753 	/* MAX TPA needs to be added because TPA_START completions are
3754 	 * immediately recycled, so the TPA completions are not bound by
3755 	 * the RX ring size.
3756 	 */
3757 	if (bp->flags & BNXT_FLAG_TPA)
3758 		max_rx_cmpl += bp->max_tpa;
3759 	/* RX and TPA completions are 32-byte, all others are 16-byte */
3760 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
3761 	bp->cp_ring_size = ring_size;
3762 
3763 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
3764 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
3765 		bp->cp_nr_pages = MAX_CP_PAGES;
3766 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
3767 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
3768 			    ring_size, bp->cp_ring_size);
3769 	}
3770 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
3771 	bp->cp_ring_mask = bp->cp_bit - 1;
3772 }
3773 
3774 /* Changing allocation mode of RX rings.
3775  * TODO: Update when extending xdp_rxq_info to support allocation modes.
3776  */
3777 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
3778 {
3779 	if (page_mode) {
3780 		if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
3781 			return -EOPNOTSUPP;
3782 		bp->dev->max_mtu =
3783 			min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
3784 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
3785 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
3786 		bp->rx_dir = DMA_BIDIRECTIONAL;
3787 		bp->rx_skb_func = bnxt_rx_page_skb;
3788 		/* Disable LRO or GRO_HW */
3789 		netdev_update_features(bp->dev);
3790 	} else {
3791 		bp->dev->max_mtu = bp->max_mtu;
3792 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
3793 		bp->rx_dir = DMA_FROM_DEVICE;
3794 		bp->rx_skb_func = bnxt_rx_skb;
3795 	}
3796 	return 0;
3797 }
3798 
3799 static void bnxt_free_vnic_attributes(struct bnxt *bp)
3800 {
3801 	int i;
3802 	struct bnxt_vnic_info *vnic;
3803 	struct pci_dev *pdev = bp->pdev;
3804 
3805 	if (!bp->vnic_info)
3806 		return;
3807 
3808 	for (i = 0; i < bp->nr_vnics; i++) {
3809 		vnic = &bp->vnic_info[i];
3810 
3811 		kfree(vnic->fw_grp_ids);
3812 		vnic->fw_grp_ids = NULL;
3813 
3814 		kfree(vnic->uc_list);
3815 		vnic->uc_list = NULL;
3816 
3817 		if (vnic->mc_list) {
3818 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
3819 					  vnic->mc_list, vnic->mc_list_mapping);
3820 			vnic->mc_list = NULL;
3821 		}
3822 
3823 		if (vnic->rss_table) {
3824 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
3825 					  vnic->rss_table,
3826 					  vnic->rss_table_dma_addr);
3827 			vnic->rss_table = NULL;
3828 		}
3829 
3830 		vnic->rss_hash_key = NULL;
3831 		vnic->flags = 0;
3832 	}
3833 }
3834 
3835 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
3836 {
3837 	int i, rc = 0, size;
3838 	struct bnxt_vnic_info *vnic;
3839 	struct pci_dev *pdev = bp->pdev;
3840 	int max_rings;
3841 
3842 	for (i = 0; i < bp->nr_vnics; i++) {
3843 		vnic = &bp->vnic_info[i];
3844 
3845 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
3846 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
3847 
3848 			if (mem_size > 0) {
3849 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
3850 				if (!vnic->uc_list) {
3851 					rc = -ENOMEM;
3852 					goto out;
3853 				}
3854 			}
3855 		}
3856 
3857 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
3858 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
3859 			vnic->mc_list =
3860 				dma_alloc_coherent(&pdev->dev,
3861 						   vnic->mc_list_size,
3862 						   &vnic->mc_list_mapping,
3863 						   GFP_KERNEL);
3864 			if (!vnic->mc_list) {
3865 				rc = -ENOMEM;
3866 				goto out;
3867 			}
3868 		}
3869 
3870 		if (bp->flags & BNXT_FLAG_CHIP_P5)
3871 			goto vnic_skip_grps;
3872 
3873 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
3874 			max_rings = bp->rx_nr_rings;
3875 		else
3876 			max_rings = 1;
3877 
3878 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
3879 		if (!vnic->fw_grp_ids) {
3880 			rc = -ENOMEM;
3881 			goto out;
3882 		}
3883 vnic_skip_grps:
3884 		if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
3885 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
3886 			continue;
3887 
3888 		/* Allocate rss table and hash key */
3889 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
3890 		if (bp->flags & BNXT_FLAG_CHIP_P5)
3891 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
3892 
3893 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
3894 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
3895 						     vnic->rss_table_size,
3896 						     &vnic->rss_table_dma_addr,
3897 						     GFP_KERNEL);
3898 		if (!vnic->rss_table) {
3899 			rc = -ENOMEM;
3900 			goto out;
3901 		}
3902 
3903 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
3904 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
3905 	}
3906 	return 0;
3907 
3908 out:
3909 	return rc;
3910 }
3911 
3912 static void bnxt_free_hwrm_resources(struct bnxt *bp)
3913 {
3914 	struct pci_dev *pdev = bp->pdev;
3915 
3916 	if (bp->hwrm_cmd_resp_addr) {
3917 		dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
3918 				  bp->hwrm_cmd_resp_dma_addr);
3919 		bp->hwrm_cmd_resp_addr = NULL;
3920 	}
3921 
3922 	if (bp->hwrm_cmd_kong_resp_addr) {
3923 		dma_free_coherent(&pdev->dev, PAGE_SIZE,
3924 				  bp->hwrm_cmd_kong_resp_addr,
3925 				  bp->hwrm_cmd_kong_resp_dma_addr);
3926 		bp->hwrm_cmd_kong_resp_addr = NULL;
3927 	}
3928 }
3929 
3930 static int bnxt_alloc_kong_hwrm_resources(struct bnxt *bp)
3931 {
3932 	struct pci_dev *pdev = bp->pdev;
3933 
3934 	if (bp->hwrm_cmd_kong_resp_addr)
3935 		return 0;
3936 
3937 	bp->hwrm_cmd_kong_resp_addr =
3938 		dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3939 				   &bp->hwrm_cmd_kong_resp_dma_addr,
3940 				   GFP_KERNEL);
3941 	if (!bp->hwrm_cmd_kong_resp_addr)
3942 		return -ENOMEM;
3943 
3944 	return 0;
3945 }
3946 
3947 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
3948 {
3949 	struct pci_dev *pdev = bp->pdev;
3950 
3951 	bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3952 						   &bp->hwrm_cmd_resp_dma_addr,
3953 						   GFP_KERNEL);
3954 	if (!bp->hwrm_cmd_resp_addr)
3955 		return -ENOMEM;
3956 
3957 	return 0;
3958 }
3959 
3960 static void bnxt_free_hwrm_short_cmd_req(struct bnxt *bp)
3961 {
3962 	if (bp->hwrm_short_cmd_req_addr) {
3963 		struct pci_dev *pdev = bp->pdev;
3964 
3965 		dma_free_coherent(&pdev->dev, bp->hwrm_max_ext_req_len,
3966 				  bp->hwrm_short_cmd_req_addr,
3967 				  bp->hwrm_short_cmd_req_dma_addr);
3968 		bp->hwrm_short_cmd_req_addr = NULL;
3969 	}
3970 }
3971 
3972 static int bnxt_alloc_hwrm_short_cmd_req(struct bnxt *bp)
3973 {
3974 	struct pci_dev *pdev = bp->pdev;
3975 
3976 	if (bp->hwrm_short_cmd_req_addr)
3977 		return 0;
3978 
3979 	bp->hwrm_short_cmd_req_addr =
3980 		dma_alloc_coherent(&pdev->dev, bp->hwrm_max_ext_req_len,
3981 				   &bp->hwrm_short_cmd_req_dma_addr,
3982 				   GFP_KERNEL);
3983 	if (!bp->hwrm_short_cmd_req_addr)
3984 		return -ENOMEM;
3985 
3986 	return 0;
3987 }
3988 
3989 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
3990 {
3991 	kfree(stats->hw_masks);
3992 	stats->hw_masks = NULL;
3993 	kfree(stats->sw_stats);
3994 	stats->sw_stats = NULL;
3995 	if (stats->hw_stats) {
3996 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
3997 				  stats->hw_stats_map);
3998 		stats->hw_stats = NULL;
3999 	}
4000 }
4001 
4002 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
4003 				bool alloc_masks)
4004 {
4005 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
4006 					     &stats->hw_stats_map, GFP_KERNEL);
4007 	if (!stats->hw_stats)
4008 		return -ENOMEM;
4009 
4010 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
4011 	if (!stats->sw_stats)
4012 		goto stats_mem_err;
4013 
4014 	if (alloc_masks) {
4015 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
4016 		if (!stats->hw_masks)
4017 			goto stats_mem_err;
4018 	}
4019 	return 0;
4020 
4021 stats_mem_err:
4022 	bnxt_free_stats_mem(bp, stats);
4023 	return -ENOMEM;
4024 }
4025 
4026 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
4027 {
4028 	int i;
4029 
4030 	for (i = 0; i < count; i++)
4031 		mask_arr[i] = mask;
4032 }
4033 
4034 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
4035 {
4036 	int i;
4037 
4038 	for (i = 0; i < count; i++)
4039 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
4040 }
4041 
4042 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
4043 				    struct bnxt_stats_mem *stats)
4044 {
4045 	struct hwrm_func_qstats_ext_output *resp = bp->hwrm_cmd_resp_addr;
4046 	struct hwrm_func_qstats_ext_input req = {0};
4047 	__le64 *hw_masks;
4048 	int rc;
4049 
4050 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
4051 	    !(bp->flags & BNXT_FLAG_CHIP_P5))
4052 		return -EOPNOTSUPP;
4053 
4054 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QSTATS_EXT, -1, -1);
4055 	req.fid = cpu_to_le16(0xffff);
4056 	req.flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4057 	mutex_lock(&bp->hwrm_cmd_lock);
4058 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4059 	if (rc)
4060 		goto qstat_exit;
4061 
4062 	hw_masks = &resp->rx_ucast_pkts;
4063 	bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
4064 
4065 qstat_exit:
4066 	mutex_unlock(&bp->hwrm_cmd_lock);
4067 	return rc;
4068 }
4069 
4070 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
4071 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
4072 
4073 static void bnxt_init_stats(struct bnxt *bp)
4074 {
4075 	struct bnxt_napi *bnapi = bp->bnapi[0];
4076 	struct bnxt_cp_ring_info *cpr;
4077 	struct bnxt_stats_mem *stats;
4078 	__le64 *rx_stats, *tx_stats;
4079 	int rc, rx_count, tx_count;
4080 	u64 *rx_masks, *tx_masks;
4081 	u64 mask;
4082 	u8 flags;
4083 
4084 	cpr = &bnapi->cp_ring;
4085 	stats = &cpr->stats;
4086 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
4087 	if (rc) {
4088 		if (bp->flags & BNXT_FLAG_CHIP_P5)
4089 			mask = (1ULL << 48) - 1;
4090 		else
4091 			mask = -1ULL;
4092 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
4093 	}
4094 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
4095 		stats = &bp->port_stats;
4096 		rx_stats = stats->hw_stats;
4097 		rx_masks = stats->hw_masks;
4098 		rx_count = sizeof(struct rx_port_stats) / 8;
4099 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4100 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4101 		tx_count = sizeof(struct tx_port_stats) / 8;
4102 
4103 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
4104 		rc = bnxt_hwrm_port_qstats(bp, flags);
4105 		if (rc) {
4106 			mask = (1ULL << 40) - 1;
4107 
4108 			bnxt_fill_masks(rx_masks, mask, rx_count);
4109 			bnxt_fill_masks(tx_masks, mask, tx_count);
4110 		} else {
4111 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4112 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
4113 			bnxt_hwrm_port_qstats(bp, 0);
4114 		}
4115 	}
4116 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
4117 		stats = &bp->rx_port_stats_ext;
4118 		rx_stats = stats->hw_stats;
4119 		rx_masks = stats->hw_masks;
4120 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
4121 		stats = &bp->tx_port_stats_ext;
4122 		tx_stats = stats->hw_stats;
4123 		tx_masks = stats->hw_masks;
4124 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
4125 
4126 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4127 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
4128 		if (rc) {
4129 			mask = (1ULL << 40) - 1;
4130 
4131 			bnxt_fill_masks(rx_masks, mask, rx_count);
4132 			if (tx_stats)
4133 				bnxt_fill_masks(tx_masks, mask, tx_count);
4134 		} else {
4135 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4136 			if (tx_stats)
4137 				bnxt_copy_hw_masks(tx_masks, tx_stats,
4138 						   tx_count);
4139 			bnxt_hwrm_port_qstats_ext(bp, 0);
4140 		}
4141 	}
4142 }
4143 
4144 static void bnxt_free_port_stats(struct bnxt *bp)
4145 {
4146 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
4147 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
4148 
4149 	bnxt_free_stats_mem(bp, &bp->port_stats);
4150 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
4151 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
4152 }
4153 
4154 static void bnxt_free_ring_stats(struct bnxt *bp)
4155 {
4156 	int i;
4157 
4158 	if (!bp->bnapi)
4159 		return;
4160 
4161 	for (i = 0; i < bp->cp_nr_rings; i++) {
4162 		struct bnxt_napi *bnapi = bp->bnapi[i];
4163 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4164 
4165 		bnxt_free_stats_mem(bp, &cpr->stats);
4166 	}
4167 }
4168 
4169 static int bnxt_alloc_stats(struct bnxt *bp)
4170 {
4171 	u32 size, i;
4172 	int rc;
4173 
4174 	size = bp->hw_ring_stats_size;
4175 
4176 	for (i = 0; i < bp->cp_nr_rings; i++) {
4177 		struct bnxt_napi *bnapi = bp->bnapi[i];
4178 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4179 
4180 		cpr->stats.len = size;
4181 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
4182 		if (rc)
4183 			return rc;
4184 
4185 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4186 	}
4187 
4188 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
4189 		return 0;
4190 
4191 	if (bp->port_stats.hw_stats)
4192 		goto alloc_ext_stats;
4193 
4194 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
4195 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
4196 	if (rc)
4197 		return rc;
4198 
4199 	bp->flags |= BNXT_FLAG_PORT_STATS;
4200 
4201 alloc_ext_stats:
4202 	/* Display extended statistics only if FW supports it */
4203 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
4204 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
4205 			return 0;
4206 
4207 	if (bp->rx_port_stats_ext.hw_stats)
4208 		goto alloc_tx_ext_stats;
4209 
4210 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
4211 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
4212 	/* Extended stats are optional */
4213 	if (rc)
4214 		return 0;
4215 
4216 alloc_tx_ext_stats:
4217 	if (bp->tx_port_stats_ext.hw_stats)
4218 		return 0;
4219 
4220 	if (bp->hwrm_spec_code >= 0x10902 ||
4221 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
4222 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
4223 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
4224 		/* Extended stats are optional */
4225 		if (rc)
4226 			return 0;
4227 	}
4228 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
4229 	return 0;
4230 }
4231 
4232 static void bnxt_clear_ring_indices(struct bnxt *bp)
4233 {
4234 	int i;
4235 
4236 	if (!bp->bnapi)
4237 		return;
4238 
4239 	for (i = 0; i < bp->cp_nr_rings; i++) {
4240 		struct bnxt_napi *bnapi = bp->bnapi[i];
4241 		struct bnxt_cp_ring_info *cpr;
4242 		struct bnxt_rx_ring_info *rxr;
4243 		struct bnxt_tx_ring_info *txr;
4244 
4245 		if (!bnapi)
4246 			continue;
4247 
4248 		cpr = &bnapi->cp_ring;
4249 		cpr->cp_raw_cons = 0;
4250 
4251 		txr = bnapi->tx_ring;
4252 		if (txr) {
4253 			txr->tx_prod = 0;
4254 			txr->tx_cons = 0;
4255 		}
4256 
4257 		rxr = bnapi->rx_ring;
4258 		if (rxr) {
4259 			rxr->rx_prod = 0;
4260 			rxr->rx_agg_prod = 0;
4261 			rxr->rx_sw_agg_prod = 0;
4262 			rxr->rx_next_cons = 0;
4263 		}
4264 	}
4265 }
4266 
4267 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
4268 {
4269 #ifdef CONFIG_RFS_ACCEL
4270 	int i;
4271 
4272 	/* Under rtnl_lock and all our NAPIs have been disabled.  It's
4273 	 * safe to delete the hash table.
4274 	 */
4275 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
4276 		struct hlist_head *head;
4277 		struct hlist_node *tmp;
4278 		struct bnxt_ntuple_filter *fltr;
4279 
4280 		head = &bp->ntp_fltr_hash_tbl[i];
4281 		hlist_for_each_entry_safe(fltr, tmp, head, hash) {
4282 			hlist_del(&fltr->hash);
4283 			kfree(fltr);
4284 		}
4285 	}
4286 	if (irq_reinit) {
4287 		kfree(bp->ntp_fltr_bmap);
4288 		bp->ntp_fltr_bmap = NULL;
4289 	}
4290 	bp->ntp_fltr_count = 0;
4291 #endif
4292 }
4293 
4294 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
4295 {
4296 #ifdef CONFIG_RFS_ACCEL
4297 	int i, rc = 0;
4298 
4299 	if (!(bp->flags & BNXT_FLAG_RFS))
4300 		return 0;
4301 
4302 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
4303 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
4304 
4305 	bp->ntp_fltr_count = 0;
4306 	bp->ntp_fltr_bmap = kcalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
4307 				    sizeof(long),
4308 				    GFP_KERNEL);
4309 
4310 	if (!bp->ntp_fltr_bmap)
4311 		rc = -ENOMEM;
4312 
4313 	return rc;
4314 #else
4315 	return 0;
4316 #endif
4317 }
4318 
4319 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
4320 {
4321 	bnxt_free_vnic_attributes(bp);
4322 	bnxt_free_tx_rings(bp);
4323 	bnxt_free_rx_rings(bp);
4324 	bnxt_free_cp_rings(bp);
4325 	bnxt_free_all_cp_arrays(bp);
4326 	bnxt_free_ntp_fltrs(bp, irq_re_init);
4327 	if (irq_re_init) {
4328 		bnxt_free_ring_stats(bp);
4329 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
4330 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
4331 			bnxt_free_port_stats(bp);
4332 		bnxt_free_ring_grps(bp);
4333 		bnxt_free_vnics(bp);
4334 		kfree(bp->tx_ring_map);
4335 		bp->tx_ring_map = NULL;
4336 		kfree(bp->tx_ring);
4337 		bp->tx_ring = NULL;
4338 		kfree(bp->rx_ring);
4339 		bp->rx_ring = NULL;
4340 		kfree(bp->bnapi);
4341 		bp->bnapi = NULL;
4342 	} else {
4343 		bnxt_clear_ring_indices(bp);
4344 	}
4345 }
4346 
4347 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
4348 {
4349 	int i, j, rc, size, arr_size;
4350 	void *bnapi;
4351 
4352 	if (irq_re_init) {
4353 		/* Allocate bnapi mem pointer array and mem block for
4354 		 * all queues
4355 		 */
4356 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
4357 				bp->cp_nr_rings);
4358 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
4359 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
4360 		if (!bnapi)
4361 			return -ENOMEM;
4362 
4363 		bp->bnapi = bnapi;
4364 		bnapi += arr_size;
4365 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
4366 			bp->bnapi[i] = bnapi;
4367 			bp->bnapi[i]->index = i;
4368 			bp->bnapi[i]->bp = bp;
4369 			if (bp->flags & BNXT_FLAG_CHIP_P5) {
4370 				struct bnxt_cp_ring_info *cpr =
4371 					&bp->bnapi[i]->cp_ring;
4372 
4373 				cpr->cp_ring_struct.ring_mem.flags =
4374 					BNXT_RMEM_RING_PTE_FLAG;
4375 			}
4376 		}
4377 
4378 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
4379 				      sizeof(struct bnxt_rx_ring_info),
4380 				      GFP_KERNEL);
4381 		if (!bp->rx_ring)
4382 			return -ENOMEM;
4383 
4384 		for (i = 0; i < bp->rx_nr_rings; i++) {
4385 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
4386 
4387 			if (bp->flags & BNXT_FLAG_CHIP_P5) {
4388 				rxr->rx_ring_struct.ring_mem.flags =
4389 					BNXT_RMEM_RING_PTE_FLAG;
4390 				rxr->rx_agg_ring_struct.ring_mem.flags =
4391 					BNXT_RMEM_RING_PTE_FLAG;
4392 			}
4393 			rxr->bnapi = bp->bnapi[i];
4394 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
4395 		}
4396 
4397 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
4398 				      sizeof(struct bnxt_tx_ring_info),
4399 				      GFP_KERNEL);
4400 		if (!bp->tx_ring)
4401 			return -ENOMEM;
4402 
4403 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
4404 					  GFP_KERNEL);
4405 
4406 		if (!bp->tx_ring_map)
4407 			return -ENOMEM;
4408 
4409 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
4410 			j = 0;
4411 		else
4412 			j = bp->rx_nr_rings;
4413 
4414 		for (i = 0; i < bp->tx_nr_rings; i++, j++) {
4415 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4416 
4417 			if (bp->flags & BNXT_FLAG_CHIP_P5)
4418 				txr->tx_ring_struct.ring_mem.flags =
4419 					BNXT_RMEM_RING_PTE_FLAG;
4420 			txr->bnapi = bp->bnapi[j];
4421 			bp->bnapi[j]->tx_ring = txr;
4422 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
4423 			if (i >= bp->tx_nr_rings_xdp) {
4424 				txr->txq_index = i - bp->tx_nr_rings_xdp;
4425 				bp->bnapi[j]->tx_int = bnxt_tx_int;
4426 			} else {
4427 				bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
4428 				bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
4429 			}
4430 		}
4431 
4432 		rc = bnxt_alloc_stats(bp);
4433 		if (rc)
4434 			goto alloc_mem_err;
4435 		bnxt_init_stats(bp);
4436 
4437 		rc = bnxt_alloc_ntp_fltrs(bp);
4438 		if (rc)
4439 			goto alloc_mem_err;
4440 
4441 		rc = bnxt_alloc_vnics(bp);
4442 		if (rc)
4443 			goto alloc_mem_err;
4444 	}
4445 
4446 	rc = bnxt_alloc_all_cp_arrays(bp);
4447 	if (rc)
4448 		goto alloc_mem_err;
4449 
4450 	bnxt_init_ring_struct(bp);
4451 
4452 	rc = bnxt_alloc_rx_rings(bp);
4453 	if (rc)
4454 		goto alloc_mem_err;
4455 
4456 	rc = bnxt_alloc_tx_rings(bp);
4457 	if (rc)
4458 		goto alloc_mem_err;
4459 
4460 	rc = bnxt_alloc_cp_rings(bp);
4461 	if (rc)
4462 		goto alloc_mem_err;
4463 
4464 	bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
4465 				  BNXT_VNIC_UCAST_FLAG;
4466 	rc = bnxt_alloc_vnic_attributes(bp);
4467 	if (rc)
4468 		goto alloc_mem_err;
4469 	return 0;
4470 
4471 alloc_mem_err:
4472 	bnxt_free_mem(bp, true);
4473 	return rc;
4474 }
4475 
4476 static void bnxt_disable_int(struct bnxt *bp)
4477 {
4478 	int i;
4479 
4480 	if (!bp->bnapi)
4481 		return;
4482 
4483 	for (i = 0; i < bp->cp_nr_rings; i++) {
4484 		struct bnxt_napi *bnapi = bp->bnapi[i];
4485 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4486 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4487 
4488 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
4489 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
4490 	}
4491 }
4492 
4493 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
4494 {
4495 	struct bnxt_napi *bnapi = bp->bnapi[n];
4496 	struct bnxt_cp_ring_info *cpr;
4497 
4498 	cpr = &bnapi->cp_ring;
4499 	return cpr->cp_ring_struct.map_idx;
4500 }
4501 
4502 static void bnxt_disable_int_sync(struct bnxt *bp)
4503 {
4504 	int i;
4505 
4506 	if (!bp->irq_tbl)
4507 		return;
4508 
4509 	atomic_inc(&bp->intr_sem);
4510 
4511 	bnxt_disable_int(bp);
4512 	for (i = 0; i < bp->cp_nr_rings; i++) {
4513 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
4514 
4515 		synchronize_irq(bp->irq_tbl[map_idx].vector);
4516 	}
4517 }
4518 
4519 static void bnxt_enable_int(struct bnxt *bp)
4520 {
4521 	int i;
4522 
4523 	atomic_set(&bp->intr_sem, 0);
4524 	for (i = 0; i < bp->cp_nr_rings; i++) {
4525 		struct bnxt_napi *bnapi = bp->bnapi[i];
4526 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4527 
4528 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
4529 	}
4530 }
4531 
4532 void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
4533 			    u16 cmpl_ring, u16 target_id)
4534 {
4535 	struct input *req = request;
4536 
4537 	req->req_type = cpu_to_le16(req_type);
4538 	req->cmpl_ring = cpu_to_le16(cmpl_ring);
4539 	req->target_id = cpu_to_le16(target_id);
4540 	if (bnxt_kong_hwrm_message(bp, req))
4541 		req->resp_addr = cpu_to_le64(bp->hwrm_cmd_kong_resp_dma_addr);
4542 	else
4543 		req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
4544 }
4545 
4546 static int bnxt_hwrm_to_stderr(u32 hwrm_err)
4547 {
4548 	switch (hwrm_err) {
4549 	case HWRM_ERR_CODE_SUCCESS:
4550 		return 0;
4551 	case HWRM_ERR_CODE_RESOURCE_LOCKED:
4552 		return -EROFS;
4553 	case HWRM_ERR_CODE_RESOURCE_ACCESS_DENIED:
4554 		return -EACCES;
4555 	case HWRM_ERR_CODE_RESOURCE_ALLOC_ERROR:
4556 		return -ENOSPC;
4557 	case HWRM_ERR_CODE_INVALID_PARAMS:
4558 	case HWRM_ERR_CODE_INVALID_FLAGS:
4559 	case HWRM_ERR_CODE_INVALID_ENABLES:
4560 	case HWRM_ERR_CODE_UNSUPPORTED_TLV:
4561 	case HWRM_ERR_CODE_UNSUPPORTED_OPTION_ERR:
4562 		return -EINVAL;
4563 	case HWRM_ERR_CODE_NO_BUFFER:
4564 		return -ENOMEM;
4565 	case HWRM_ERR_CODE_HOT_RESET_PROGRESS:
4566 	case HWRM_ERR_CODE_BUSY:
4567 		return -EAGAIN;
4568 	case HWRM_ERR_CODE_CMD_NOT_SUPPORTED:
4569 		return -EOPNOTSUPP;
4570 	default:
4571 		return -EIO;
4572 	}
4573 }
4574 
4575 static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
4576 				 int timeout, bool silent)
4577 {
4578 	int i, intr_process, rc, tmo_count;
4579 	struct input *req = msg;
4580 	u32 *data = msg;
4581 	u8 *valid;
4582 	u16 cp_ring_id, len = 0;
4583 	struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
4584 	u16 max_req_len = BNXT_HWRM_MAX_REQ_LEN;
4585 	struct hwrm_short_input short_input = {0};
4586 	u32 doorbell_offset = BNXT_GRCPF_REG_CHIMP_COMM_TRIGGER;
4587 	u32 bar_offset = BNXT_GRCPF_REG_CHIMP_COMM;
4588 	u16 dst = BNXT_HWRM_CHNL_CHIMP;
4589 
4590 	if (BNXT_NO_FW_ACCESS(bp) &&
4591 	    le16_to_cpu(req->req_type) != HWRM_FUNC_RESET)
4592 		return -EBUSY;
4593 
4594 	if (msg_len > BNXT_HWRM_MAX_REQ_LEN) {
4595 		if (msg_len > bp->hwrm_max_ext_req_len ||
4596 		    !bp->hwrm_short_cmd_req_addr)
4597 			return -EINVAL;
4598 	}
4599 
4600 	if (bnxt_hwrm_kong_chnl(bp, req)) {
4601 		dst = BNXT_HWRM_CHNL_KONG;
4602 		bar_offset = BNXT_GRCPF_REG_KONG_COMM;
4603 		doorbell_offset = BNXT_GRCPF_REG_KONG_COMM_TRIGGER;
4604 		resp = bp->hwrm_cmd_kong_resp_addr;
4605 	}
4606 
4607 	memset(resp, 0, PAGE_SIZE);
4608 	cp_ring_id = le16_to_cpu(req->cmpl_ring);
4609 	intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
4610 
4611 	req->seq_id = cpu_to_le16(bnxt_get_hwrm_seq_id(bp, dst));
4612 	/* currently supports only one outstanding message */
4613 	if (intr_process)
4614 		bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
4615 
4616 	if ((bp->fw_cap & BNXT_FW_CAP_SHORT_CMD) ||
4617 	    msg_len > BNXT_HWRM_MAX_REQ_LEN) {
4618 		void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
4619 		u16 max_msg_len;
4620 
4621 		/* Set boundary for maximum extended request length for short
4622 		 * cmd format. If passed up from device use the max supported
4623 		 * internal req length.
4624 		 */
4625 		max_msg_len = bp->hwrm_max_ext_req_len;
4626 
4627 		memcpy(short_cmd_req, req, msg_len);
4628 		if (msg_len < max_msg_len)
4629 			memset(short_cmd_req + msg_len, 0,
4630 			       max_msg_len - msg_len);
4631 
4632 		short_input.req_type = req->req_type;
4633 		short_input.signature =
4634 				cpu_to_le16(SHORT_REQ_SIGNATURE_SHORT_CMD);
4635 		short_input.size = cpu_to_le16(msg_len);
4636 		short_input.req_addr =
4637 			cpu_to_le64(bp->hwrm_short_cmd_req_dma_addr);
4638 
4639 		data = (u32 *)&short_input;
4640 		msg_len = sizeof(short_input);
4641 
4642 		/* Sync memory write before updating doorbell */
4643 		wmb();
4644 
4645 		max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
4646 	}
4647 
4648 	/* Write request msg to hwrm channel */
4649 	__iowrite32_copy(bp->bar0 + bar_offset, data, msg_len / 4);
4650 
4651 	for (i = msg_len; i < max_req_len; i += 4)
4652 		writel(0, bp->bar0 + bar_offset + i);
4653 
4654 	/* Ring channel doorbell */
4655 	writel(1, bp->bar0 + doorbell_offset);
4656 
4657 	if (!pci_is_enabled(bp->pdev))
4658 		return -ENODEV;
4659 
4660 	if (!timeout)
4661 		timeout = DFLT_HWRM_CMD_TIMEOUT;
4662 	/* Limit timeout to an upper limit */
4663 	timeout = min(timeout, HWRM_CMD_MAX_TIMEOUT);
4664 	/* convert timeout to usec */
4665 	timeout *= 1000;
4666 
4667 	i = 0;
4668 	/* Short timeout for the first few iterations:
4669 	 * number of loops = number of loops for short timeout +
4670 	 * number of loops for standard timeout.
4671 	 */
4672 	tmo_count = HWRM_SHORT_TIMEOUT_COUNTER;
4673 	timeout = timeout - HWRM_SHORT_MIN_TIMEOUT * HWRM_SHORT_TIMEOUT_COUNTER;
4674 	tmo_count += DIV_ROUND_UP(timeout, HWRM_MIN_TIMEOUT);
4675 
4676 	if (intr_process) {
4677 		u16 seq_id = bp->hwrm_intr_seq_id;
4678 
4679 		/* Wait until hwrm response cmpl interrupt is processed */
4680 		while (bp->hwrm_intr_seq_id != (u16)~seq_id &&
4681 		       i++ < tmo_count) {
4682 			/* Abort the wait for completion if the FW health
4683 			 * check has failed.
4684 			 */
4685 			if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
4686 				return -EBUSY;
4687 			/* on first few passes, just barely sleep */
4688 			if (i < HWRM_SHORT_TIMEOUT_COUNTER) {
4689 				usleep_range(HWRM_SHORT_MIN_TIMEOUT,
4690 					     HWRM_SHORT_MAX_TIMEOUT);
4691 			} else {
4692 				if (HWRM_WAIT_MUST_ABORT(bp, req))
4693 					break;
4694 				usleep_range(HWRM_MIN_TIMEOUT,
4695 					     HWRM_MAX_TIMEOUT);
4696 			}
4697 		}
4698 
4699 		if (bp->hwrm_intr_seq_id != (u16)~seq_id) {
4700 			if (!silent)
4701 				netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
4702 					   le16_to_cpu(req->req_type));
4703 			return -EBUSY;
4704 		}
4705 		len = le16_to_cpu(resp->resp_len);
4706 		valid = ((u8 *)resp) + len - 1;
4707 	} else {
4708 		int j;
4709 
4710 		/* Check if response len is updated */
4711 		for (i = 0; i < tmo_count; i++) {
4712 			/* Abort the wait for completion if the FW health
4713 			 * check has failed.
4714 			 */
4715 			if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
4716 				return -EBUSY;
4717 			len = le16_to_cpu(resp->resp_len);
4718 			if (len)
4719 				break;
4720 			/* on first few passes, just barely sleep */
4721 			if (i < HWRM_SHORT_TIMEOUT_COUNTER) {
4722 				usleep_range(HWRM_SHORT_MIN_TIMEOUT,
4723 					     HWRM_SHORT_MAX_TIMEOUT);
4724 			} else {
4725 				if (HWRM_WAIT_MUST_ABORT(bp, req))
4726 					goto timeout_abort;
4727 				usleep_range(HWRM_MIN_TIMEOUT,
4728 					     HWRM_MAX_TIMEOUT);
4729 			}
4730 		}
4731 
4732 		if (i >= tmo_count) {
4733 timeout_abort:
4734 			if (!silent)
4735 				netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
4736 					   HWRM_TOTAL_TIMEOUT(i),
4737 					   le16_to_cpu(req->req_type),
4738 					   le16_to_cpu(req->seq_id), len);
4739 			return -EBUSY;
4740 		}
4741 
4742 		/* Last byte of resp contains valid bit */
4743 		valid = ((u8 *)resp) + len - 1;
4744 		for (j = 0; j < HWRM_VALID_BIT_DELAY_USEC; j++) {
4745 			/* make sure we read from updated DMA memory */
4746 			dma_rmb();
4747 			if (*valid)
4748 				break;
4749 			usleep_range(1, 5);
4750 		}
4751 
4752 		if (j >= HWRM_VALID_BIT_DELAY_USEC) {
4753 			if (!silent)
4754 				netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
4755 					   HWRM_TOTAL_TIMEOUT(i),
4756 					   le16_to_cpu(req->req_type),
4757 					   le16_to_cpu(req->seq_id), len,
4758 					   *valid);
4759 			return -EBUSY;
4760 		}
4761 	}
4762 
4763 	/* Zero valid bit for compatibility.  Valid bit in an older spec
4764 	 * may become a new field in a newer spec.  We must make sure that
4765 	 * a new field not implemented by old spec will read zero.
4766 	 */
4767 	*valid = 0;
4768 	rc = le16_to_cpu(resp->error_code);
4769 	if (rc && !silent)
4770 		netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
4771 			   le16_to_cpu(resp->req_type),
4772 			   le16_to_cpu(resp->seq_id), rc);
4773 	return bnxt_hwrm_to_stderr(rc);
4774 }
4775 
4776 int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
4777 {
4778 	return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
4779 }
4780 
4781 int _hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
4782 			      int timeout)
4783 {
4784 	return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
4785 }
4786 
4787 int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
4788 {
4789 	int rc;
4790 
4791 	mutex_lock(&bp->hwrm_cmd_lock);
4792 	rc = _hwrm_send_message(bp, msg, msg_len, timeout);
4793 	mutex_unlock(&bp->hwrm_cmd_lock);
4794 	return rc;
4795 }
4796 
4797 int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
4798 			     int timeout)
4799 {
4800 	int rc;
4801 
4802 	mutex_lock(&bp->hwrm_cmd_lock);
4803 	rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
4804 	mutex_unlock(&bp->hwrm_cmd_lock);
4805 	return rc;
4806 }
4807 
4808 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
4809 			    bool async_only)
4810 {
4811 	struct hwrm_func_drv_rgtr_output *resp = bp->hwrm_cmd_resp_addr;
4812 	struct hwrm_func_drv_rgtr_input req = {0};
4813 	DECLARE_BITMAP(async_events_bmap, 256);
4814 	u32 *events = (u32 *)async_events_bmap;
4815 	u32 flags;
4816 	int rc, i;
4817 
4818 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
4819 
4820 	req.enables =
4821 		cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
4822 			    FUNC_DRV_RGTR_REQ_ENABLES_VER |
4823 			    FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
4824 
4825 	req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
4826 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
4827 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
4828 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
4829 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
4830 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
4831 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
4832 	req.flags = cpu_to_le32(flags);
4833 	req.ver_maj_8b = DRV_VER_MAJ;
4834 	req.ver_min_8b = DRV_VER_MIN;
4835 	req.ver_upd_8b = DRV_VER_UPD;
4836 	req.ver_maj = cpu_to_le16(DRV_VER_MAJ);
4837 	req.ver_min = cpu_to_le16(DRV_VER_MIN);
4838 	req.ver_upd = cpu_to_le16(DRV_VER_UPD);
4839 
4840 	if (BNXT_PF(bp)) {
4841 		u32 data[8];
4842 		int i;
4843 
4844 		memset(data, 0, sizeof(data));
4845 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
4846 			u16 cmd = bnxt_vf_req_snif[i];
4847 			unsigned int bit, idx;
4848 
4849 			idx = cmd / 32;
4850 			bit = cmd % 32;
4851 			data[idx] |= 1 << bit;
4852 		}
4853 
4854 		for (i = 0; i < 8; i++)
4855 			req.vf_req_fwd[i] = cpu_to_le32(data[i]);
4856 
4857 		req.enables |=
4858 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
4859 	}
4860 
4861 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
4862 		req.flags |= cpu_to_le32(
4863 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
4864 
4865 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
4866 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
4867 		u16 event_id = bnxt_async_events_arr[i];
4868 
4869 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
4870 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
4871 			continue;
4872 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
4873 	}
4874 	if (bmap && bmap_size) {
4875 		for (i = 0; i < bmap_size; i++) {
4876 			if (test_bit(i, bmap))
4877 				__set_bit(i, async_events_bmap);
4878 		}
4879 	}
4880 	for (i = 0; i < 8; i++)
4881 		req.async_event_fwd[i] |= cpu_to_le32(events[i]);
4882 
4883 	if (async_only)
4884 		req.enables =
4885 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
4886 
4887 	mutex_lock(&bp->hwrm_cmd_lock);
4888 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4889 	if (!rc) {
4890 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
4891 		if (resp->flags &
4892 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
4893 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
4894 	}
4895 	mutex_unlock(&bp->hwrm_cmd_lock);
4896 	return rc;
4897 }
4898 
4899 static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
4900 {
4901 	struct hwrm_func_drv_unrgtr_input req = {0};
4902 
4903 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
4904 		return 0;
4905 
4906 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
4907 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4908 }
4909 
4910 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
4911 {
4912 	u32 rc = 0;
4913 	struct hwrm_tunnel_dst_port_free_input req = {0};
4914 
4915 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
4916 	req.tunnel_type = tunnel_type;
4917 
4918 	switch (tunnel_type) {
4919 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
4920 		req.tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
4921 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
4922 		break;
4923 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
4924 		req.tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
4925 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
4926 		break;
4927 	default:
4928 		break;
4929 	}
4930 
4931 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4932 	if (rc)
4933 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
4934 			   rc);
4935 	return rc;
4936 }
4937 
4938 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
4939 					   u8 tunnel_type)
4940 {
4941 	u32 rc = 0;
4942 	struct hwrm_tunnel_dst_port_alloc_input req = {0};
4943 	struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4944 
4945 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
4946 
4947 	req.tunnel_type = tunnel_type;
4948 	req.tunnel_dst_port_val = port;
4949 
4950 	mutex_lock(&bp->hwrm_cmd_lock);
4951 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4952 	if (rc) {
4953 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
4954 			   rc);
4955 		goto err_out;
4956 	}
4957 
4958 	switch (tunnel_type) {
4959 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
4960 		bp->vxlan_fw_dst_port_id =
4961 			le16_to_cpu(resp->tunnel_dst_port_id);
4962 		break;
4963 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
4964 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
4965 		break;
4966 	default:
4967 		break;
4968 	}
4969 
4970 err_out:
4971 	mutex_unlock(&bp->hwrm_cmd_lock);
4972 	return rc;
4973 }
4974 
4975 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
4976 {
4977 	struct hwrm_cfa_l2_set_rx_mask_input req = {0};
4978 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4979 
4980 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
4981 	req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
4982 
4983 	req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
4984 	req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
4985 	req.mask = cpu_to_le32(vnic->rx_mask);
4986 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4987 }
4988 
4989 #ifdef CONFIG_RFS_ACCEL
4990 static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
4991 					    struct bnxt_ntuple_filter *fltr)
4992 {
4993 	struct hwrm_cfa_ntuple_filter_free_input req = {0};
4994 
4995 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
4996 	req.ntuple_filter_id = fltr->filter_id;
4997 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4998 }
4999 
5000 #define BNXT_NTP_FLTR_FLAGS					\
5001 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
5002 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
5003 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR |	\
5004 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
5005 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
5006 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
5007 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
5008 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
5009 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
5010 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
5011 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
5012 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
5013 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
5014 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
5015 
5016 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
5017 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
5018 
5019 static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
5020 					     struct bnxt_ntuple_filter *fltr)
5021 {
5022 	struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
5023 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
5024 	struct flow_keys *keys = &fltr->fkeys;
5025 	struct bnxt_vnic_info *vnic;
5026 	u32 flags = 0;
5027 	int rc = 0;
5028 
5029 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
5030 	req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
5031 
5032 	if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
5033 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
5034 		req.dst_id = cpu_to_le16(fltr->rxq);
5035 	} else {
5036 		vnic = &bp->vnic_info[fltr->rxq + 1];
5037 		req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
5038 	}
5039 	req.flags = cpu_to_le32(flags);
5040 	req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
5041 
5042 	req.ethertype = htons(ETH_P_IP);
5043 	memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
5044 	req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
5045 	req.ip_protocol = keys->basic.ip_proto;
5046 
5047 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
5048 		int i;
5049 
5050 		req.ethertype = htons(ETH_P_IPV6);
5051 		req.ip_addr_type =
5052 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
5053 		*(struct in6_addr *)&req.src_ipaddr[0] =
5054 			keys->addrs.v6addrs.src;
5055 		*(struct in6_addr *)&req.dst_ipaddr[0] =
5056 			keys->addrs.v6addrs.dst;
5057 		for (i = 0; i < 4; i++) {
5058 			req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
5059 			req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
5060 		}
5061 	} else {
5062 		req.src_ipaddr[0] = keys->addrs.v4addrs.src;
5063 		req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
5064 		req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
5065 		req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
5066 	}
5067 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
5068 		req.enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
5069 		req.tunnel_type =
5070 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
5071 	}
5072 
5073 	req.src_port = keys->ports.src;
5074 	req.src_port_mask = cpu_to_be16(0xffff);
5075 	req.dst_port = keys->ports.dst;
5076 	req.dst_port_mask = cpu_to_be16(0xffff);
5077 
5078 	mutex_lock(&bp->hwrm_cmd_lock);
5079 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5080 	if (!rc) {
5081 		resp = bnxt_get_hwrm_resp_addr(bp, &req);
5082 		fltr->filter_id = resp->ntuple_filter_id;
5083 	}
5084 	mutex_unlock(&bp->hwrm_cmd_lock);
5085 	return rc;
5086 }
5087 #endif
5088 
5089 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
5090 				     u8 *mac_addr)
5091 {
5092 	u32 rc = 0;
5093 	struct hwrm_cfa_l2_filter_alloc_input req = {0};
5094 	struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5095 
5096 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
5097 	req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
5098 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
5099 		req.flags |=
5100 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
5101 	req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
5102 	req.enables =
5103 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
5104 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
5105 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
5106 	memcpy(req.l2_addr, mac_addr, ETH_ALEN);
5107 	req.l2_addr_mask[0] = 0xff;
5108 	req.l2_addr_mask[1] = 0xff;
5109 	req.l2_addr_mask[2] = 0xff;
5110 	req.l2_addr_mask[3] = 0xff;
5111 	req.l2_addr_mask[4] = 0xff;
5112 	req.l2_addr_mask[5] = 0xff;
5113 
5114 	mutex_lock(&bp->hwrm_cmd_lock);
5115 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5116 	if (!rc)
5117 		bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
5118 							resp->l2_filter_id;
5119 	mutex_unlock(&bp->hwrm_cmd_lock);
5120 	return rc;
5121 }
5122 
5123 static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
5124 {
5125 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
5126 	int rc = 0;
5127 
5128 	/* Any associated ntuple filters will also be cleared by firmware. */
5129 	mutex_lock(&bp->hwrm_cmd_lock);
5130 	for (i = 0; i < num_of_vnics; i++) {
5131 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
5132 
5133 		for (j = 0; j < vnic->uc_filter_count; j++) {
5134 			struct hwrm_cfa_l2_filter_free_input req = {0};
5135 
5136 			bnxt_hwrm_cmd_hdr_init(bp, &req,
5137 					       HWRM_CFA_L2_FILTER_FREE, -1, -1);
5138 
5139 			req.l2_filter_id = vnic->fw_l2_filter_id[j];
5140 
5141 			rc = _hwrm_send_message(bp, &req, sizeof(req),
5142 						HWRM_CMD_TIMEOUT);
5143 		}
5144 		vnic->uc_filter_count = 0;
5145 	}
5146 	mutex_unlock(&bp->hwrm_cmd_lock);
5147 
5148 	return rc;
5149 }
5150 
5151 static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
5152 {
5153 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5154 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
5155 	struct hwrm_vnic_tpa_cfg_input req = {0};
5156 
5157 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
5158 		return 0;
5159 
5160 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
5161 
5162 	if (tpa_flags) {
5163 		u16 mss = bp->dev->mtu - 40;
5164 		u32 nsegs, n, segs = 0, flags;
5165 
5166 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
5167 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
5168 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
5169 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
5170 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
5171 		if (tpa_flags & BNXT_FLAG_GRO)
5172 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
5173 
5174 		req.flags = cpu_to_le32(flags);
5175 
5176 		req.enables =
5177 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
5178 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
5179 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
5180 
5181 		/* Number of segs are log2 units, and first packet is not
5182 		 * included as part of this units.
5183 		 */
5184 		if (mss <= BNXT_RX_PAGE_SIZE) {
5185 			n = BNXT_RX_PAGE_SIZE / mss;
5186 			nsegs = (MAX_SKB_FRAGS - 1) * n;
5187 		} else {
5188 			n = mss / BNXT_RX_PAGE_SIZE;
5189 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
5190 				n++;
5191 			nsegs = (MAX_SKB_FRAGS - n) / n;
5192 		}
5193 
5194 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5195 			segs = MAX_TPA_SEGS_P5;
5196 			max_aggs = bp->max_tpa;
5197 		} else {
5198 			segs = ilog2(nsegs);
5199 		}
5200 		req.max_agg_segs = cpu_to_le16(segs);
5201 		req.max_aggs = cpu_to_le16(max_aggs);
5202 
5203 		req.min_agg_len = cpu_to_le32(512);
5204 	}
5205 	req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5206 
5207 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5208 }
5209 
5210 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
5211 {
5212 	struct bnxt_ring_grp_info *grp_info;
5213 
5214 	grp_info = &bp->grp_info[ring->grp_idx];
5215 	return grp_info->cp_fw_ring_id;
5216 }
5217 
5218 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
5219 {
5220 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
5221 		struct bnxt_napi *bnapi = rxr->bnapi;
5222 		struct bnxt_cp_ring_info *cpr;
5223 
5224 		cpr = bnapi->cp_ring.cp_ring_arr[BNXT_RX_HDL];
5225 		return cpr->cp_ring_struct.fw_ring_id;
5226 	} else {
5227 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
5228 	}
5229 }
5230 
5231 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
5232 {
5233 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
5234 		struct bnxt_napi *bnapi = txr->bnapi;
5235 		struct bnxt_cp_ring_info *cpr;
5236 
5237 		cpr = bnapi->cp_ring.cp_ring_arr[BNXT_TX_HDL];
5238 		return cpr->cp_ring_struct.fw_ring_id;
5239 	} else {
5240 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
5241 	}
5242 }
5243 
5244 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
5245 {
5246 	int entries;
5247 
5248 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5249 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
5250 	else
5251 		entries = HW_HASH_INDEX_SIZE;
5252 
5253 	bp->rss_indir_tbl_entries = entries;
5254 	bp->rss_indir_tbl = kmalloc_array(entries, sizeof(*bp->rss_indir_tbl),
5255 					  GFP_KERNEL);
5256 	if (!bp->rss_indir_tbl)
5257 		return -ENOMEM;
5258 	return 0;
5259 }
5260 
5261 static void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp)
5262 {
5263 	u16 max_rings, max_entries, pad, i;
5264 
5265 	if (!bp->rx_nr_rings)
5266 		return;
5267 
5268 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5269 		max_rings = bp->rx_nr_rings - 1;
5270 	else
5271 		max_rings = bp->rx_nr_rings;
5272 
5273 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
5274 
5275 	for (i = 0; i < max_entries; i++)
5276 		bp->rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
5277 
5278 	pad = bp->rss_indir_tbl_entries - max_entries;
5279 	if (pad)
5280 		memset(&bp->rss_indir_tbl[i], 0, pad * sizeof(u16));
5281 }
5282 
5283 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
5284 {
5285 	u16 i, tbl_size, max_ring = 0;
5286 
5287 	if (!bp->rss_indir_tbl)
5288 		return 0;
5289 
5290 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
5291 	for (i = 0; i < tbl_size; i++)
5292 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
5293 	return max_ring;
5294 }
5295 
5296 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
5297 {
5298 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5299 		return DIV_ROUND_UP(rx_rings, BNXT_RSS_TABLE_ENTRIES_P5);
5300 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5301 		return 2;
5302 	return 1;
5303 }
5304 
5305 static void __bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
5306 {
5307 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
5308 	u16 i, j;
5309 
5310 	/* Fill the RSS indirection table with ring group ids */
5311 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
5312 		if (!no_rss)
5313 			j = bp->rss_indir_tbl[i];
5314 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
5315 	}
5316 }
5317 
5318 static void __bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
5319 				      struct bnxt_vnic_info *vnic)
5320 {
5321 	__le16 *ring_tbl = vnic->rss_table;
5322 	struct bnxt_rx_ring_info *rxr;
5323 	u16 tbl_size, i;
5324 
5325 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
5326 
5327 	for (i = 0; i < tbl_size; i++) {
5328 		u16 ring_id, j;
5329 
5330 		j = bp->rss_indir_tbl[i];
5331 		rxr = &bp->rx_ring[j];
5332 
5333 		ring_id = rxr->rx_ring_struct.fw_ring_id;
5334 		*ring_tbl++ = cpu_to_le16(ring_id);
5335 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5336 		*ring_tbl++ = cpu_to_le16(ring_id);
5337 	}
5338 }
5339 
5340 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
5341 {
5342 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5343 		__bnxt_fill_hw_rss_tbl_p5(bp, vnic);
5344 	else
5345 		__bnxt_fill_hw_rss_tbl(bp, vnic);
5346 }
5347 
5348 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
5349 {
5350 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5351 	struct hwrm_vnic_rss_cfg_input req = {0};
5352 
5353 	if ((bp->flags & BNXT_FLAG_CHIP_P5) ||
5354 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
5355 		return 0;
5356 
5357 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
5358 	if (set_rss) {
5359 		bnxt_fill_hw_rss_tbl(bp, vnic);
5360 		req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
5361 		req.hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
5362 		req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
5363 		req.hash_key_tbl_addr =
5364 			cpu_to_le64(vnic->rss_hash_key_dma_addr);
5365 	}
5366 	req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
5367 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5368 }
5369 
5370 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp, u16 vnic_id, bool set_rss)
5371 {
5372 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5373 	struct hwrm_vnic_rss_cfg_input req = {0};
5374 	dma_addr_t ring_tbl_map;
5375 	u32 i, nr_ctxs;
5376 
5377 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
5378 	req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5379 	if (!set_rss) {
5380 		hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5381 		return 0;
5382 	}
5383 	bnxt_fill_hw_rss_tbl(bp, vnic);
5384 	req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
5385 	req.hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
5386 	req.hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
5387 	ring_tbl_map = vnic->rss_table_dma_addr;
5388 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
5389 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
5390 		int rc;
5391 
5392 		req.ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
5393 		req.ring_table_pair_index = i;
5394 		req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
5395 		rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5396 		if (rc)
5397 			return rc;
5398 	}
5399 	return 0;
5400 }
5401 
5402 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
5403 {
5404 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5405 	struct hwrm_vnic_plcmodes_cfg_input req = {0};
5406 
5407 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
5408 	req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
5409 				VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
5410 				VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
5411 	req.enables =
5412 		cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
5413 			    VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
5414 	/* thresholds not implemented in firmware yet */
5415 	req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
5416 	req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
5417 	req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5418 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5419 }
5420 
5421 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
5422 					u16 ctx_idx)
5423 {
5424 	struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
5425 
5426 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
5427 	req.rss_cos_lb_ctx_id =
5428 		cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
5429 
5430 	hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5431 	bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
5432 }
5433 
5434 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
5435 {
5436 	int i, j;
5437 
5438 	for (i = 0; i < bp->nr_vnics; i++) {
5439 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
5440 
5441 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
5442 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
5443 				bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
5444 		}
5445 	}
5446 	bp->rsscos_nr_ctxs = 0;
5447 }
5448 
5449 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
5450 {
5451 	int rc;
5452 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
5453 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
5454 						bp->hwrm_cmd_resp_addr;
5455 
5456 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
5457 			       -1);
5458 
5459 	mutex_lock(&bp->hwrm_cmd_lock);
5460 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5461 	if (!rc)
5462 		bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
5463 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
5464 	mutex_unlock(&bp->hwrm_cmd_lock);
5465 
5466 	return rc;
5467 }
5468 
5469 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
5470 {
5471 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
5472 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
5473 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
5474 }
5475 
5476 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
5477 {
5478 	unsigned int ring = 0, grp_idx;
5479 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5480 	struct hwrm_vnic_cfg_input req = {0};
5481 	u16 def_vlan = 0;
5482 
5483 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
5484 
5485 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
5486 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
5487 
5488 		req.default_rx_ring_id =
5489 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
5490 		req.default_cmpl_ring_id =
5491 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
5492 		req.enables =
5493 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
5494 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
5495 		goto vnic_mru;
5496 	}
5497 	req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
5498 	/* Only RSS support for now TBD: COS & LB */
5499 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
5500 		req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
5501 		req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
5502 					   VNIC_CFG_REQ_ENABLES_MRU);
5503 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
5504 		req.rss_rule =
5505 			cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
5506 		req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
5507 					   VNIC_CFG_REQ_ENABLES_MRU);
5508 		req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
5509 	} else {
5510 		req.rss_rule = cpu_to_le16(0xffff);
5511 	}
5512 
5513 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
5514 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
5515 		req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
5516 		req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
5517 	} else {
5518 		req.cos_rule = cpu_to_le16(0xffff);
5519 	}
5520 
5521 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
5522 		ring = 0;
5523 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
5524 		ring = vnic_id - 1;
5525 	else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
5526 		ring = bp->rx_nr_rings - 1;
5527 
5528 	grp_idx = bp->rx_ring[ring].bnapi->index;
5529 	req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
5530 	req.lb_rule = cpu_to_le16(0xffff);
5531 vnic_mru:
5532 	req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + VLAN_HLEN);
5533 
5534 	req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5535 #ifdef CONFIG_BNXT_SRIOV
5536 	if (BNXT_VF(bp))
5537 		def_vlan = bp->vf.vlan;
5538 #endif
5539 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
5540 		req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
5541 	if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
5542 		req.flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
5543 
5544 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5545 }
5546 
5547 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
5548 {
5549 	if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
5550 		struct hwrm_vnic_free_input req = {0};
5551 
5552 		bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
5553 		req.vnic_id =
5554 			cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
5555 
5556 		hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5557 		bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
5558 	}
5559 }
5560 
5561 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
5562 {
5563 	u16 i;
5564 
5565 	for (i = 0; i < bp->nr_vnics; i++)
5566 		bnxt_hwrm_vnic_free_one(bp, i);
5567 }
5568 
5569 static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
5570 				unsigned int start_rx_ring_idx,
5571 				unsigned int nr_rings)
5572 {
5573 	int rc = 0;
5574 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
5575 	struct hwrm_vnic_alloc_input req = {0};
5576 	struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5577 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5578 
5579 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5580 		goto vnic_no_ring_grps;
5581 
5582 	/* map ring groups to this vnic */
5583 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
5584 		grp_idx = bp->rx_ring[i].bnapi->index;
5585 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
5586 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
5587 				   j, nr_rings);
5588 			break;
5589 		}
5590 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
5591 	}
5592 
5593 vnic_no_ring_grps:
5594 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
5595 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
5596 	if (vnic_id == 0)
5597 		req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
5598 
5599 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
5600 
5601 	mutex_lock(&bp->hwrm_cmd_lock);
5602 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5603 	if (!rc)
5604 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
5605 	mutex_unlock(&bp->hwrm_cmd_lock);
5606 	return rc;
5607 }
5608 
5609 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
5610 {
5611 	struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5612 	struct hwrm_vnic_qcaps_input req = {0};
5613 	int rc;
5614 
5615 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
5616 	bp->flags &= ~(BNXT_FLAG_NEW_RSS_CAP | BNXT_FLAG_ROCE_MIRROR_CAP);
5617 	if (bp->hwrm_spec_code < 0x10600)
5618 		return 0;
5619 
5620 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
5621 	mutex_lock(&bp->hwrm_cmd_lock);
5622 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5623 	if (!rc) {
5624 		u32 flags = le32_to_cpu(resp->flags);
5625 
5626 		if (!(bp->flags & BNXT_FLAG_CHIP_P5) &&
5627 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
5628 			bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
5629 		if (flags &
5630 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
5631 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
5632 
5633 		/* Older P5 fw before EXT_HW_STATS support did not set
5634 		 * VLAN_STRIP_CAP properly.
5635 		 */
5636 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
5637 		    (BNXT_CHIP_P5_THOR(bp) &&
5638 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
5639 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
5640 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
5641 		if (bp->max_tpa_v2) {
5642 			if (BNXT_CHIP_P5_THOR(bp))
5643 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
5644 			else
5645 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5_SR2;
5646 		}
5647 	}
5648 	mutex_unlock(&bp->hwrm_cmd_lock);
5649 	return rc;
5650 }
5651 
5652 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
5653 {
5654 	u16 i;
5655 	u32 rc = 0;
5656 
5657 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5658 		return 0;
5659 
5660 	mutex_lock(&bp->hwrm_cmd_lock);
5661 	for (i = 0; i < bp->rx_nr_rings; i++) {
5662 		struct hwrm_ring_grp_alloc_input req = {0};
5663 		struct hwrm_ring_grp_alloc_output *resp =
5664 					bp->hwrm_cmd_resp_addr;
5665 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
5666 
5667 		bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
5668 
5669 		req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
5670 		req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
5671 		req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
5672 		req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
5673 
5674 		rc = _hwrm_send_message(bp, &req, sizeof(req),
5675 					HWRM_CMD_TIMEOUT);
5676 		if (rc)
5677 			break;
5678 
5679 		bp->grp_info[grp_idx].fw_grp_id =
5680 			le32_to_cpu(resp->ring_group_id);
5681 	}
5682 	mutex_unlock(&bp->hwrm_cmd_lock);
5683 	return rc;
5684 }
5685 
5686 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
5687 {
5688 	u16 i;
5689 	struct hwrm_ring_grp_free_input req = {0};
5690 
5691 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5))
5692 		return;
5693 
5694 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
5695 
5696 	mutex_lock(&bp->hwrm_cmd_lock);
5697 	for (i = 0; i < bp->cp_nr_rings; i++) {
5698 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
5699 			continue;
5700 		req.ring_group_id =
5701 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
5702 
5703 		_hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5704 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
5705 	}
5706 	mutex_unlock(&bp->hwrm_cmd_lock);
5707 }
5708 
5709 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
5710 				    struct bnxt_ring_struct *ring,
5711 				    u32 ring_type, u32 map_index)
5712 {
5713 	int rc = 0, err = 0;
5714 	struct hwrm_ring_alloc_input req = {0};
5715 	struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5716 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
5717 	struct bnxt_ring_grp_info *grp_info;
5718 	u16 ring_id;
5719 
5720 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
5721 
5722 	req.enables = 0;
5723 	if (rmem->nr_pages > 1) {
5724 		req.page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
5725 		/* Page size is in log2 units */
5726 		req.page_size = BNXT_PAGE_SHIFT;
5727 		req.page_tbl_depth = 1;
5728 	} else {
5729 		req.page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
5730 	}
5731 	req.fbo = 0;
5732 	/* Association of ring index with doorbell index and MSIX number */
5733 	req.logical_id = cpu_to_le16(map_index);
5734 
5735 	switch (ring_type) {
5736 	case HWRM_RING_ALLOC_TX: {
5737 		struct bnxt_tx_ring_info *txr;
5738 
5739 		txr = container_of(ring, struct bnxt_tx_ring_info,
5740 				   tx_ring_struct);
5741 		req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
5742 		/* Association of transmit ring with completion ring */
5743 		grp_info = &bp->grp_info[ring->grp_idx];
5744 		req.cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
5745 		req.length = cpu_to_le32(bp->tx_ring_mask + 1);
5746 		req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5747 		req.queue_id = cpu_to_le16(ring->queue_id);
5748 		break;
5749 	}
5750 	case HWRM_RING_ALLOC_RX:
5751 		req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
5752 		req.length = cpu_to_le32(bp->rx_ring_mask + 1);
5753 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5754 			u16 flags = 0;
5755 
5756 			/* Association of rx ring with stats context */
5757 			grp_info = &bp->grp_info[ring->grp_idx];
5758 			req.rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
5759 			req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5760 			req.enables |= cpu_to_le32(
5761 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
5762 			if (NET_IP_ALIGN == 2)
5763 				flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
5764 			req.flags = cpu_to_le16(flags);
5765 		}
5766 		break;
5767 	case HWRM_RING_ALLOC_AGG:
5768 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5769 			req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
5770 			/* Association of agg ring with rx ring */
5771 			grp_info = &bp->grp_info[ring->grp_idx];
5772 			req.rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
5773 			req.rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
5774 			req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5775 			req.enables |= cpu_to_le32(
5776 				RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
5777 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
5778 		} else {
5779 			req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
5780 		}
5781 		req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
5782 		break;
5783 	case HWRM_RING_ALLOC_CMPL:
5784 		req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
5785 		req.length = cpu_to_le32(bp->cp_ring_mask + 1);
5786 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5787 			/* Association of cp ring with nq */
5788 			grp_info = &bp->grp_info[map_index];
5789 			req.nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
5790 			req.cq_handle = cpu_to_le64(ring->handle);
5791 			req.enables |= cpu_to_le32(
5792 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
5793 		} else if (bp->flags & BNXT_FLAG_USING_MSIX) {
5794 			req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
5795 		}
5796 		break;
5797 	case HWRM_RING_ALLOC_NQ:
5798 		req.ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
5799 		req.length = cpu_to_le32(bp->cp_ring_mask + 1);
5800 		if (bp->flags & BNXT_FLAG_USING_MSIX)
5801 			req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
5802 		break;
5803 	default:
5804 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
5805 			   ring_type);
5806 		return -1;
5807 	}
5808 
5809 	mutex_lock(&bp->hwrm_cmd_lock);
5810 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5811 	err = le16_to_cpu(resp->error_code);
5812 	ring_id = le16_to_cpu(resp->ring_id);
5813 	mutex_unlock(&bp->hwrm_cmd_lock);
5814 
5815 	if (rc || err) {
5816 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
5817 			   ring_type, rc, err);
5818 		return -EIO;
5819 	}
5820 	ring->fw_ring_id = ring_id;
5821 	return rc;
5822 }
5823 
5824 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
5825 {
5826 	int rc;
5827 
5828 	if (BNXT_PF(bp)) {
5829 		struct hwrm_func_cfg_input req = {0};
5830 
5831 		bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
5832 		req.fid = cpu_to_le16(0xffff);
5833 		req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
5834 		req.async_event_cr = cpu_to_le16(idx);
5835 		rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5836 	} else {
5837 		struct hwrm_func_vf_cfg_input req = {0};
5838 
5839 		bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
5840 		req.enables =
5841 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
5842 		req.async_event_cr = cpu_to_le16(idx);
5843 		rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5844 	}
5845 	return rc;
5846 }
5847 
5848 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
5849 			u32 map_idx, u32 xid)
5850 {
5851 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
5852 		if (BNXT_PF(bp))
5853 			db->doorbell = bp->bar1 + DB_PF_OFFSET_P5;
5854 		else
5855 			db->doorbell = bp->bar1 + DB_VF_OFFSET_P5;
5856 		switch (ring_type) {
5857 		case HWRM_RING_ALLOC_TX:
5858 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
5859 			break;
5860 		case HWRM_RING_ALLOC_RX:
5861 		case HWRM_RING_ALLOC_AGG:
5862 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
5863 			break;
5864 		case HWRM_RING_ALLOC_CMPL:
5865 			db->db_key64 = DBR_PATH_L2;
5866 			break;
5867 		case HWRM_RING_ALLOC_NQ:
5868 			db->db_key64 = DBR_PATH_L2;
5869 			break;
5870 		}
5871 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
5872 	} else {
5873 		db->doorbell = bp->bar1 + map_idx * 0x80;
5874 		switch (ring_type) {
5875 		case HWRM_RING_ALLOC_TX:
5876 			db->db_key32 = DB_KEY_TX;
5877 			break;
5878 		case HWRM_RING_ALLOC_RX:
5879 		case HWRM_RING_ALLOC_AGG:
5880 			db->db_key32 = DB_KEY_RX;
5881 			break;
5882 		case HWRM_RING_ALLOC_CMPL:
5883 			db->db_key32 = DB_KEY_CP;
5884 			break;
5885 		}
5886 	}
5887 }
5888 
5889 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
5890 {
5891 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
5892 	int i, rc = 0;
5893 	u32 type;
5894 
5895 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5896 		type = HWRM_RING_ALLOC_NQ;
5897 	else
5898 		type = HWRM_RING_ALLOC_CMPL;
5899 	for (i = 0; i < bp->cp_nr_rings; i++) {
5900 		struct bnxt_napi *bnapi = bp->bnapi[i];
5901 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5902 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5903 		u32 map_idx = ring->map_idx;
5904 		unsigned int vector;
5905 
5906 		vector = bp->irq_tbl[map_idx].vector;
5907 		disable_irq_nosync(vector);
5908 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5909 		if (rc) {
5910 			enable_irq(vector);
5911 			goto err_out;
5912 		}
5913 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
5914 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5915 		enable_irq(vector);
5916 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
5917 
5918 		if (!i) {
5919 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
5920 			if (rc)
5921 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
5922 		}
5923 	}
5924 
5925 	type = HWRM_RING_ALLOC_TX;
5926 	for (i = 0; i < bp->tx_nr_rings; i++) {
5927 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5928 		struct bnxt_ring_struct *ring;
5929 		u32 map_idx;
5930 
5931 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5932 			struct bnxt_napi *bnapi = txr->bnapi;
5933 			struct bnxt_cp_ring_info *cpr, *cpr2;
5934 			u32 type2 = HWRM_RING_ALLOC_CMPL;
5935 
5936 			cpr = &bnapi->cp_ring;
5937 			cpr2 = cpr->cp_ring_arr[BNXT_TX_HDL];
5938 			ring = &cpr2->cp_ring_struct;
5939 			ring->handle = BNXT_TX_HDL;
5940 			map_idx = bnapi->index;
5941 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
5942 			if (rc)
5943 				goto err_out;
5944 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
5945 				    ring->fw_ring_id);
5946 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
5947 		}
5948 		ring = &txr->tx_ring_struct;
5949 		map_idx = i;
5950 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5951 		if (rc)
5952 			goto err_out;
5953 		bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
5954 	}
5955 
5956 	type = HWRM_RING_ALLOC_RX;
5957 	for (i = 0; i < bp->rx_nr_rings; i++) {
5958 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5959 		struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
5960 		struct bnxt_napi *bnapi = rxr->bnapi;
5961 		u32 map_idx = bnapi->index;
5962 
5963 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5964 		if (rc)
5965 			goto err_out;
5966 		bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
5967 		/* If we have agg rings, post agg buffers first. */
5968 		if (!agg_rings)
5969 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
5970 		bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
5971 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5972 			struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5973 			u32 type2 = HWRM_RING_ALLOC_CMPL;
5974 			struct bnxt_cp_ring_info *cpr2;
5975 
5976 			cpr2 = cpr->cp_ring_arr[BNXT_RX_HDL];
5977 			ring = &cpr2->cp_ring_struct;
5978 			ring->handle = BNXT_RX_HDL;
5979 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
5980 			if (rc)
5981 				goto err_out;
5982 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
5983 				    ring->fw_ring_id);
5984 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
5985 		}
5986 	}
5987 
5988 	if (agg_rings) {
5989 		type = HWRM_RING_ALLOC_AGG;
5990 		for (i = 0; i < bp->rx_nr_rings; i++) {
5991 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5992 			struct bnxt_ring_struct *ring =
5993 						&rxr->rx_agg_ring_struct;
5994 			u32 grp_idx = ring->grp_idx;
5995 			u32 map_idx = grp_idx + bp->rx_nr_rings;
5996 
5997 			rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5998 			if (rc)
5999 				goto err_out;
6000 
6001 			bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
6002 				    ring->fw_ring_id);
6003 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
6004 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
6005 			bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
6006 		}
6007 	}
6008 err_out:
6009 	return rc;
6010 }
6011 
6012 static int hwrm_ring_free_send_msg(struct bnxt *bp,
6013 				   struct bnxt_ring_struct *ring,
6014 				   u32 ring_type, int cmpl_ring_id)
6015 {
6016 	int rc;
6017 	struct hwrm_ring_free_input req = {0};
6018 	struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
6019 	u16 error_code;
6020 
6021 	if (BNXT_NO_FW_ACCESS(bp))
6022 		return 0;
6023 
6024 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
6025 	req.ring_type = ring_type;
6026 	req.ring_id = cpu_to_le16(ring->fw_ring_id);
6027 
6028 	mutex_lock(&bp->hwrm_cmd_lock);
6029 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6030 	error_code = le16_to_cpu(resp->error_code);
6031 	mutex_unlock(&bp->hwrm_cmd_lock);
6032 
6033 	if (rc || error_code) {
6034 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
6035 			   ring_type, rc, error_code);
6036 		return -EIO;
6037 	}
6038 	return 0;
6039 }
6040 
6041 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
6042 {
6043 	u32 type;
6044 	int i;
6045 
6046 	if (!bp->bnapi)
6047 		return;
6048 
6049 	for (i = 0; i < bp->tx_nr_rings; i++) {
6050 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
6051 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
6052 
6053 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
6054 			u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
6055 
6056 			hwrm_ring_free_send_msg(bp, ring,
6057 						RING_FREE_REQ_RING_TYPE_TX,
6058 						close_path ? cmpl_ring_id :
6059 						INVALID_HW_RING_ID);
6060 			ring->fw_ring_id = INVALID_HW_RING_ID;
6061 		}
6062 	}
6063 
6064 	for (i = 0; i < bp->rx_nr_rings; i++) {
6065 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
6066 		struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
6067 		u32 grp_idx = rxr->bnapi->index;
6068 
6069 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
6070 			u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6071 
6072 			hwrm_ring_free_send_msg(bp, ring,
6073 						RING_FREE_REQ_RING_TYPE_RX,
6074 						close_path ? cmpl_ring_id :
6075 						INVALID_HW_RING_ID);
6076 			ring->fw_ring_id = INVALID_HW_RING_ID;
6077 			bp->grp_info[grp_idx].rx_fw_ring_id =
6078 				INVALID_HW_RING_ID;
6079 		}
6080 	}
6081 
6082 	if (bp->flags & BNXT_FLAG_CHIP_P5)
6083 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
6084 	else
6085 		type = RING_FREE_REQ_RING_TYPE_RX;
6086 	for (i = 0; i < bp->rx_nr_rings; i++) {
6087 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
6088 		struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
6089 		u32 grp_idx = rxr->bnapi->index;
6090 
6091 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
6092 			u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6093 
6094 			hwrm_ring_free_send_msg(bp, ring, type,
6095 						close_path ? cmpl_ring_id :
6096 						INVALID_HW_RING_ID);
6097 			ring->fw_ring_id = INVALID_HW_RING_ID;
6098 			bp->grp_info[grp_idx].agg_fw_ring_id =
6099 				INVALID_HW_RING_ID;
6100 		}
6101 	}
6102 
6103 	/* The completion rings are about to be freed.  After that the
6104 	 * IRQ doorbell will not work anymore.  So we need to disable
6105 	 * IRQ here.
6106 	 */
6107 	bnxt_disable_int_sync(bp);
6108 
6109 	if (bp->flags & BNXT_FLAG_CHIP_P5)
6110 		type = RING_FREE_REQ_RING_TYPE_NQ;
6111 	else
6112 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
6113 	for (i = 0; i < bp->cp_nr_rings; i++) {
6114 		struct bnxt_napi *bnapi = bp->bnapi[i];
6115 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6116 		struct bnxt_ring_struct *ring;
6117 		int j;
6118 
6119 		for (j = 0; j < 2; j++) {
6120 			struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
6121 
6122 			if (cpr2) {
6123 				ring = &cpr2->cp_ring_struct;
6124 				if (ring->fw_ring_id == INVALID_HW_RING_ID)
6125 					continue;
6126 				hwrm_ring_free_send_msg(bp, ring,
6127 					RING_FREE_REQ_RING_TYPE_L2_CMPL,
6128 					INVALID_HW_RING_ID);
6129 				ring->fw_ring_id = INVALID_HW_RING_ID;
6130 			}
6131 		}
6132 		ring = &cpr->cp_ring_struct;
6133 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
6134 			hwrm_ring_free_send_msg(bp, ring, type,
6135 						INVALID_HW_RING_ID);
6136 			ring->fw_ring_id = INVALID_HW_RING_ID;
6137 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
6138 		}
6139 	}
6140 }
6141 
6142 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
6143 			   bool shared);
6144 
6145 static int bnxt_hwrm_get_rings(struct bnxt *bp)
6146 {
6147 	struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6148 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6149 	struct hwrm_func_qcfg_input req = {0};
6150 	int rc;
6151 
6152 	if (bp->hwrm_spec_code < 0x10601)
6153 		return 0;
6154 
6155 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
6156 	req.fid = cpu_to_le16(0xffff);
6157 	mutex_lock(&bp->hwrm_cmd_lock);
6158 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6159 	if (rc) {
6160 		mutex_unlock(&bp->hwrm_cmd_lock);
6161 		return rc;
6162 	}
6163 
6164 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
6165 	if (BNXT_NEW_RM(bp)) {
6166 		u16 cp, stats;
6167 
6168 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
6169 		hw_resc->resv_hw_ring_grps =
6170 			le32_to_cpu(resp->alloc_hw_ring_grps);
6171 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
6172 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
6173 		stats = le16_to_cpu(resp->alloc_stat_ctx);
6174 		hw_resc->resv_irqs = cp;
6175 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
6176 			int rx = hw_resc->resv_rx_rings;
6177 			int tx = hw_resc->resv_tx_rings;
6178 
6179 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
6180 				rx >>= 1;
6181 			if (cp < (rx + tx)) {
6182 				bnxt_trim_rings(bp, &rx, &tx, cp, false);
6183 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
6184 					rx <<= 1;
6185 				hw_resc->resv_rx_rings = rx;
6186 				hw_resc->resv_tx_rings = tx;
6187 			}
6188 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
6189 			hw_resc->resv_hw_ring_grps = rx;
6190 		}
6191 		hw_resc->resv_cp_rings = cp;
6192 		hw_resc->resv_stat_ctxs = stats;
6193 	}
6194 	mutex_unlock(&bp->hwrm_cmd_lock);
6195 	return 0;
6196 }
6197 
6198 /* Caller must hold bp->hwrm_cmd_lock */
6199 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
6200 {
6201 	struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6202 	struct hwrm_func_qcfg_input req = {0};
6203 	int rc;
6204 
6205 	if (bp->hwrm_spec_code < 0x10601)
6206 		return 0;
6207 
6208 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
6209 	req.fid = cpu_to_le16(fid);
6210 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6211 	if (!rc)
6212 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
6213 
6214 	return rc;
6215 }
6216 
6217 static bool bnxt_rfs_supported(struct bnxt *bp);
6218 
6219 static void
6220 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct hwrm_func_cfg_input *req,
6221 			     int tx_rings, int rx_rings, int ring_grps,
6222 			     int cp_rings, int stats, int vnics)
6223 {
6224 	u32 enables = 0;
6225 
6226 	bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_CFG, -1, -1);
6227 	req->fid = cpu_to_le16(0xffff);
6228 	enables |= tx_rings ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
6229 	req->num_tx_rings = cpu_to_le16(tx_rings);
6230 	if (BNXT_NEW_RM(bp)) {
6231 		enables |= rx_rings ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
6232 		enables |= stats ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
6233 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
6234 			enables |= cp_rings ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
6235 			enables |= tx_rings + ring_grps ?
6236 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6237 			enables |= rx_rings ?
6238 				FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6239 		} else {
6240 			enables |= cp_rings ?
6241 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6242 			enables |= ring_grps ?
6243 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS |
6244 				   FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6245 		}
6246 		enables |= vnics ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
6247 
6248 		req->num_rx_rings = cpu_to_le16(rx_rings);
6249 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
6250 			req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
6251 			req->num_msix = cpu_to_le16(cp_rings);
6252 			req->num_rsscos_ctxs =
6253 				cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
6254 		} else {
6255 			req->num_cmpl_rings = cpu_to_le16(cp_rings);
6256 			req->num_hw_ring_grps = cpu_to_le16(ring_grps);
6257 			req->num_rsscos_ctxs = cpu_to_le16(1);
6258 			if (!(bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
6259 			    bnxt_rfs_supported(bp))
6260 				req->num_rsscos_ctxs =
6261 					cpu_to_le16(ring_grps + 1);
6262 		}
6263 		req->num_stat_ctxs = cpu_to_le16(stats);
6264 		req->num_vnics = cpu_to_le16(vnics);
6265 	}
6266 	req->enables = cpu_to_le32(enables);
6267 }
6268 
6269 static void
6270 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp,
6271 			     struct hwrm_func_vf_cfg_input *req, int tx_rings,
6272 			     int rx_rings, int ring_grps, int cp_rings,
6273 			     int stats, int vnics)
6274 {
6275 	u32 enables = 0;
6276 
6277 	bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_VF_CFG, -1, -1);
6278 	enables |= tx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
6279 	enables |= rx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
6280 			      FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6281 	enables |= stats ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
6282 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
6283 		enables |= tx_rings + ring_grps ?
6284 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6285 	} else {
6286 		enables |= cp_rings ?
6287 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6288 		enables |= ring_grps ?
6289 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
6290 	}
6291 	enables |= vnics ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
6292 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
6293 
6294 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
6295 	req->num_tx_rings = cpu_to_le16(tx_rings);
6296 	req->num_rx_rings = cpu_to_le16(rx_rings);
6297 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
6298 		req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
6299 		req->num_rsscos_ctxs = cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
6300 	} else {
6301 		req->num_cmpl_rings = cpu_to_le16(cp_rings);
6302 		req->num_hw_ring_grps = cpu_to_le16(ring_grps);
6303 		req->num_rsscos_ctxs = cpu_to_le16(BNXT_VF_MAX_RSS_CTX);
6304 	}
6305 	req->num_stat_ctxs = cpu_to_le16(stats);
6306 	req->num_vnics = cpu_to_le16(vnics);
6307 
6308 	req->enables = cpu_to_le32(enables);
6309 }
6310 
6311 static int
6312 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6313 			   int ring_grps, int cp_rings, int stats, int vnics)
6314 {
6315 	struct hwrm_func_cfg_input req = {0};
6316 	int rc;
6317 
6318 	__bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
6319 				     cp_rings, stats, vnics);
6320 	if (!req.enables)
6321 		return 0;
6322 
6323 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6324 	if (rc)
6325 		return rc;
6326 
6327 	if (bp->hwrm_spec_code < 0x10601)
6328 		bp->hw_resc.resv_tx_rings = tx_rings;
6329 
6330 	return bnxt_hwrm_get_rings(bp);
6331 }
6332 
6333 static int
6334 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6335 			   int ring_grps, int cp_rings, int stats, int vnics)
6336 {
6337 	struct hwrm_func_vf_cfg_input req = {0};
6338 	int rc;
6339 
6340 	if (!BNXT_NEW_RM(bp)) {
6341 		bp->hw_resc.resv_tx_rings = tx_rings;
6342 		return 0;
6343 	}
6344 
6345 	__bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
6346 				     cp_rings, stats, vnics);
6347 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6348 	if (rc)
6349 		return rc;
6350 
6351 	return bnxt_hwrm_get_rings(bp);
6352 }
6353 
6354 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, int tx, int rx, int grp,
6355 				   int cp, int stat, int vnic)
6356 {
6357 	if (BNXT_PF(bp))
6358 		return bnxt_hwrm_reserve_pf_rings(bp, tx, rx, grp, cp, stat,
6359 						  vnic);
6360 	else
6361 		return bnxt_hwrm_reserve_vf_rings(bp, tx, rx, grp, cp, stat,
6362 						  vnic);
6363 }
6364 
6365 int bnxt_nq_rings_in_use(struct bnxt *bp)
6366 {
6367 	int cp = bp->cp_nr_rings;
6368 	int ulp_msix, ulp_base;
6369 
6370 	ulp_msix = bnxt_get_ulp_msix_num(bp);
6371 	if (ulp_msix) {
6372 		ulp_base = bnxt_get_ulp_msix_base(bp);
6373 		cp += ulp_msix;
6374 		if ((ulp_base + ulp_msix) > cp)
6375 			cp = ulp_base + ulp_msix;
6376 	}
6377 	return cp;
6378 }
6379 
6380 static int bnxt_cp_rings_in_use(struct bnxt *bp)
6381 {
6382 	int cp;
6383 
6384 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6385 		return bnxt_nq_rings_in_use(bp);
6386 
6387 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
6388 	return cp;
6389 }
6390 
6391 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
6392 {
6393 	int ulp_stat = bnxt_get_ulp_stat_ctxs(bp);
6394 	int cp = bp->cp_nr_rings;
6395 
6396 	if (!ulp_stat)
6397 		return cp;
6398 
6399 	if (bnxt_nq_rings_in_use(bp) > cp + bnxt_get_ulp_msix_num(bp))
6400 		return bnxt_get_ulp_msix_base(bp) + ulp_stat;
6401 
6402 	return cp + ulp_stat;
6403 }
6404 
6405 /* Check if a default RSS map needs to be setup.  This function is only
6406  * used on older firmware that does not require reserving RX rings.
6407  */
6408 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
6409 {
6410 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6411 
6412 	/* The RSS map is valid for RX rings set to resv_rx_rings */
6413 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
6414 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
6415 		if (!netif_is_rxfh_configured(bp->dev))
6416 			bnxt_set_dflt_rss_indir_tbl(bp);
6417 	}
6418 }
6419 
6420 static bool bnxt_need_reserve_rings(struct bnxt *bp)
6421 {
6422 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6423 	int cp = bnxt_cp_rings_in_use(bp);
6424 	int nq = bnxt_nq_rings_in_use(bp);
6425 	int rx = bp->rx_nr_rings, stat;
6426 	int vnic = 1, grp = rx;
6427 
6428 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
6429 	    bp->hwrm_spec_code >= 0x10601)
6430 		return true;
6431 
6432 	/* Old firmware does not need RX ring reservations but we still
6433 	 * need to setup a default RSS map when needed.  With new firmware
6434 	 * we go through RX ring reservations first and then set up the
6435 	 * RSS map for the successfully reserved RX rings when needed.
6436 	 */
6437 	if (!BNXT_NEW_RM(bp)) {
6438 		bnxt_check_rss_tbl_no_rmgr(bp);
6439 		return false;
6440 	}
6441 	if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
6442 		vnic = rx + 1;
6443 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
6444 		rx <<= 1;
6445 	stat = bnxt_get_func_stat_ctxs(bp);
6446 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
6447 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
6448 	    (hw_resc->resv_hw_ring_grps != grp &&
6449 	     !(bp->flags & BNXT_FLAG_CHIP_P5)))
6450 		return true;
6451 	if ((bp->flags & BNXT_FLAG_CHIP_P5) && BNXT_PF(bp) &&
6452 	    hw_resc->resv_irqs != nq)
6453 		return true;
6454 	return false;
6455 }
6456 
6457 static int __bnxt_reserve_rings(struct bnxt *bp)
6458 {
6459 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6460 	int cp = bnxt_nq_rings_in_use(bp);
6461 	int tx = bp->tx_nr_rings;
6462 	int rx = bp->rx_nr_rings;
6463 	int grp, rx_rings, rc;
6464 	int vnic = 1, stat;
6465 	bool sh = false;
6466 
6467 	if (!bnxt_need_reserve_rings(bp))
6468 		return 0;
6469 
6470 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
6471 		sh = true;
6472 	if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
6473 		vnic = rx + 1;
6474 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
6475 		rx <<= 1;
6476 	grp = bp->rx_nr_rings;
6477 	stat = bnxt_get_func_stat_ctxs(bp);
6478 
6479 	rc = bnxt_hwrm_reserve_rings(bp, tx, rx, grp, cp, stat, vnic);
6480 	if (rc)
6481 		return rc;
6482 
6483 	tx = hw_resc->resv_tx_rings;
6484 	if (BNXT_NEW_RM(bp)) {
6485 		rx = hw_resc->resv_rx_rings;
6486 		cp = hw_resc->resv_irqs;
6487 		grp = hw_resc->resv_hw_ring_grps;
6488 		vnic = hw_resc->resv_vnics;
6489 		stat = hw_resc->resv_stat_ctxs;
6490 	}
6491 
6492 	rx_rings = rx;
6493 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
6494 		if (rx >= 2) {
6495 			rx_rings = rx >> 1;
6496 		} else {
6497 			if (netif_running(bp->dev))
6498 				return -ENOMEM;
6499 
6500 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
6501 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
6502 			bp->dev->hw_features &= ~NETIF_F_LRO;
6503 			bp->dev->features &= ~NETIF_F_LRO;
6504 			bnxt_set_ring_params(bp);
6505 		}
6506 	}
6507 	rx_rings = min_t(int, rx_rings, grp);
6508 	cp = min_t(int, cp, bp->cp_nr_rings);
6509 	if (stat > bnxt_get_ulp_stat_ctxs(bp))
6510 		stat -= bnxt_get_ulp_stat_ctxs(bp);
6511 	cp = min_t(int, cp, stat);
6512 	rc = bnxt_trim_rings(bp, &rx_rings, &tx, cp, sh);
6513 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
6514 		rx = rx_rings << 1;
6515 	cp = sh ? max_t(int, tx, rx_rings) : tx + rx_rings;
6516 	bp->tx_nr_rings = tx;
6517 
6518 	/* If we cannot reserve all the RX rings, reset the RSS map only
6519 	 * if absolutely necessary
6520 	 */
6521 	if (rx_rings != bp->rx_nr_rings) {
6522 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
6523 			    rx_rings, bp->rx_nr_rings);
6524 		if ((bp->dev->priv_flags & IFF_RXFH_CONFIGURED) &&
6525 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
6526 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
6527 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
6528 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
6529 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
6530 		}
6531 	}
6532 	bp->rx_nr_rings = rx_rings;
6533 	bp->cp_nr_rings = cp;
6534 
6535 	if (!tx || !rx || !cp || !grp || !vnic || !stat)
6536 		return -ENOMEM;
6537 
6538 	if (!netif_is_rxfh_configured(bp->dev))
6539 		bnxt_set_dflt_rss_indir_tbl(bp);
6540 
6541 	return rc;
6542 }
6543 
6544 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6545 				    int ring_grps, int cp_rings, int stats,
6546 				    int vnics)
6547 {
6548 	struct hwrm_func_vf_cfg_input req = {0};
6549 	u32 flags;
6550 
6551 	if (!BNXT_NEW_RM(bp))
6552 		return 0;
6553 
6554 	__bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
6555 				     cp_rings, stats, vnics);
6556 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
6557 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
6558 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
6559 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
6560 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
6561 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
6562 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6563 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
6564 
6565 	req.flags = cpu_to_le32(flags);
6566 	return hwrm_send_message_silent(bp, &req, sizeof(req),
6567 					HWRM_CMD_TIMEOUT);
6568 }
6569 
6570 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6571 				    int ring_grps, int cp_rings, int stats,
6572 				    int vnics)
6573 {
6574 	struct hwrm_func_cfg_input req = {0};
6575 	u32 flags;
6576 
6577 	__bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
6578 				     cp_rings, stats, vnics);
6579 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
6580 	if (BNXT_NEW_RM(bp)) {
6581 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
6582 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
6583 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
6584 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
6585 		if (bp->flags & BNXT_FLAG_CHIP_P5)
6586 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
6587 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
6588 		else
6589 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
6590 	}
6591 
6592 	req.flags = cpu_to_le32(flags);
6593 	return hwrm_send_message_silent(bp, &req, sizeof(req),
6594 					HWRM_CMD_TIMEOUT);
6595 }
6596 
6597 static int bnxt_hwrm_check_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6598 				 int ring_grps, int cp_rings, int stats,
6599 				 int vnics)
6600 {
6601 	if (bp->hwrm_spec_code < 0x10801)
6602 		return 0;
6603 
6604 	if (BNXT_PF(bp))
6605 		return bnxt_hwrm_check_pf_rings(bp, tx_rings, rx_rings,
6606 						ring_grps, cp_rings, stats,
6607 						vnics);
6608 
6609 	return bnxt_hwrm_check_vf_rings(bp, tx_rings, rx_rings, ring_grps,
6610 					cp_rings, stats, vnics);
6611 }
6612 
6613 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
6614 {
6615 	struct hwrm_ring_aggint_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
6616 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6617 	struct hwrm_ring_aggint_qcaps_input req = {0};
6618 	int rc;
6619 
6620 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
6621 	coal_cap->num_cmpl_dma_aggr_max = 63;
6622 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
6623 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
6624 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
6625 	coal_cap->int_lat_tmr_min_max = 65535;
6626 	coal_cap->int_lat_tmr_max_max = 65535;
6627 	coal_cap->num_cmpl_aggr_int_max = 65535;
6628 	coal_cap->timer_units = 80;
6629 
6630 	if (bp->hwrm_spec_code < 0x10902)
6631 		return;
6632 
6633 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_AGGINT_QCAPS, -1, -1);
6634 	mutex_lock(&bp->hwrm_cmd_lock);
6635 	rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6636 	if (!rc) {
6637 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
6638 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
6639 		coal_cap->num_cmpl_dma_aggr_max =
6640 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
6641 		coal_cap->num_cmpl_dma_aggr_during_int_max =
6642 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
6643 		coal_cap->cmpl_aggr_dma_tmr_max =
6644 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
6645 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
6646 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
6647 		coal_cap->int_lat_tmr_min_max =
6648 			le16_to_cpu(resp->int_lat_tmr_min_max);
6649 		coal_cap->int_lat_tmr_max_max =
6650 			le16_to_cpu(resp->int_lat_tmr_max_max);
6651 		coal_cap->num_cmpl_aggr_int_max =
6652 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
6653 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
6654 	}
6655 	mutex_unlock(&bp->hwrm_cmd_lock);
6656 }
6657 
6658 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
6659 {
6660 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6661 
6662 	return usec * 1000 / coal_cap->timer_units;
6663 }
6664 
6665 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
6666 	struct bnxt_coal *hw_coal,
6667 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
6668 {
6669 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6670 	u32 cmpl_params = coal_cap->cmpl_params;
6671 	u16 val, tmr, max, flags = 0;
6672 
6673 	max = hw_coal->bufs_per_record * 128;
6674 	if (hw_coal->budget)
6675 		max = hw_coal->bufs_per_record * hw_coal->budget;
6676 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
6677 
6678 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
6679 	req->num_cmpl_aggr_int = cpu_to_le16(val);
6680 
6681 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
6682 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
6683 
6684 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
6685 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
6686 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
6687 
6688 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
6689 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
6690 	req->int_lat_tmr_max = cpu_to_le16(tmr);
6691 
6692 	/* min timer set to 1/2 of interrupt timer */
6693 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
6694 		val = tmr / 2;
6695 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
6696 		req->int_lat_tmr_min = cpu_to_le16(val);
6697 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
6698 	}
6699 
6700 	/* buf timer set to 1/4 of interrupt timer */
6701 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
6702 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
6703 
6704 	if (cmpl_params &
6705 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
6706 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
6707 		val = clamp_t(u16, tmr, 1,
6708 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
6709 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
6710 		req->enables |=
6711 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
6712 	}
6713 
6714 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
6715 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
6716 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
6717 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
6718 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
6719 	req->flags = cpu_to_le16(flags);
6720 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
6721 }
6722 
6723 /* Caller holds bp->hwrm_cmd_lock */
6724 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
6725 				   struct bnxt_coal *hw_coal)
6726 {
6727 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req = {0};
6728 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6729 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6730 	u32 nq_params = coal_cap->nq_params;
6731 	u16 tmr;
6732 
6733 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
6734 		return 0;
6735 
6736 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS,
6737 			       -1, -1);
6738 	req.ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
6739 	req.flags =
6740 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
6741 
6742 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
6743 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
6744 	req.int_lat_tmr_min = cpu_to_le16(tmr);
6745 	req.enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
6746 	return _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6747 }
6748 
6749 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
6750 {
6751 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0};
6752 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6753 	struct bnxt_coal coal;
6754 
6755 	/* Tick values in micro seconds.
6756 	 * 1 coal_buf x bufs_per_record = 1 completion record.
6757 	 */
6758 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
6759 
6760 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
6761 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
6762 
6763 	if (!bnapi->rx_ring)
6764 		return -ENODEV;
6765 
6766 	bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
6767 			       HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
6768 
6769 	bnxt_hwrm_set_coal_params(bp, &coal, &req_rx);
6770 
6771 	req_rx.ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
6772 
6773 	return hwrm_send_message(bp, &req_rx, sizeof(req_rx),
6774 				 HWRM_CMD_TIMEOUT);
6775 }
6776 
6777 int bnxt_hwrm_set_coal(struct bnxt *bp)
6778 {
6779 	int i, rc = 0;
6780 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
6781 							   req_tx = {0}, *req;
6782 
6783 	bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
6784 			       HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
6785 	bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
6786 			       HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
6787 
6788 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, &req_rx);
6789 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, &req_tx);
6790 
6791 	mutex_lock(&bp->hwrm_cmd_lock);
6792 	for (i = 0; i < bp->cp_nr_rings; i++) {
6793 		struct bnxt_napi *bnapi = bp->bnapi[i];
6794 		struct bnxt_coal *hw_coal;
6795 		u16 ring_id;
6796 
6797 		req = &req_rx;
6798 		if (!bnapi->rx_ring) {
6799 			ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
6800 			req = &req_tx;
6801 		} else {
6802 			ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
6803 		}
6804 		req->ring_id = cpu_to_le16(ring_id);
6805 
6806 		rc = _hwrm_send_message(bp, req, sizeof(*req),
6807 					HWRM_CMD_TIMEOUT);
6808 		if (rc)
6809 			break;
6810 
6811 		if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6812 			continue;
6813 
6814 		if (bnapi->rx_ring && bnapi->tx_ring) {
6815 			req = &req_tx;
6816 			ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
6817 			req->ring_id = cpu_to_le16(ring_id);
6818 			rc = _hwrm_send_message(bp, req, sizeof(*req),
6819 						HWRM_CMD_TIMEOUT);
6820 			if (rc)
6821 				break;
6822 		}
6823 		if (bnapi->rx_ring)
6824 			hw_coal = &bp->rx_coal;
6825 		else
6826 			hw_coal = &bp->tx_coal;
6827 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
6828 	}
6829 	mutex_unlock(&bp->hwrm_cmd_lock);
6830 	return rc;
6831 }
6832 
6833 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
6834 {
6835 	struct hwrm_stat_ctx_clr_stats_input req0 = {0};
6836 	struct hwrm_stat_ctx_free_input req = {0};
6837 	int i;
6838 
6839 	if (!bp->bnapi)
6840 		return;
6841 
6842 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6843 		return;
6844 
6845 	bnxt_hwrm_cmd_hdr_init(bp, &req0, HWRM_STAT_CTX_CLR_STATS, -1, -1);
6846 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
6847 
6848 	mutex_lock(&bp->hwrm_cmd_lock);
6849 	for (i = 0; i < bp->cp_nr_rings; i++) {
6850 		struct bnxt_napi *bnapi = bp->bnapi[i];
6851 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6852 
6853 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
6854 			req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
6855 			if (BNXT_FW_MAJ(bp) <= 20) {
6856 				req0.stat_ctx_id = req.stat_ctx_id;
6857 				_hwrm_send_message(bp, &req0, sizeof(req0),
6858 						   HWRM_CMD_TIMEOUT);
6859 			}
6860 			_hwrm_send_message(bp, &req, sizeof(req),
6861 					   HWRM_CMD_TIMEOUT);
6862 
6863 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
6864 		}
6865 	}
6866 	mutex_unlock(&bp->hwrm_cmd_lock);
6867 }
6868 
6869 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
6870 {
6871 	int rc = 0, i;
6872 	struct hwrm_stat_ctx_alloc_input req = {0};
6873 	struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
6874 
6875 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6876 		return 0;
6877 
6878 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
6879 
6880 	req.stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
6881 	req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
6882 
6883 	mutex_lock(&bp->hwrm_cmd_lock);
6884 	for (i = 0; i < bp->cp_nr_rings; i++) {
6885 		struct bnxt_napi *bnapi = bp->bnapi[i];
6886 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6887 
6888 		req.stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
6889 
6890 		rc = _hwrm_send_message(bp, &req, sizeof(req),
6891 					HWRM_CMD_TIMEOUT);
6892 		if (rc)
6893 			break;
6894 
6895 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
6896 
6897 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
6898 	}
6899 	mutex_unlock(&bp->hwrm_cmd_lock);
6900 	return rc;
6901 }
6902 
6903 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
6904 {
6905 	struct hwrm_func_qcfg_input req = {0};
6906 	struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6907 	u32 min_db_offset = 0;
6908 	u16 flags;
6909 	int rc;
6910 
6911 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
6912 	req.fid = cpu_to_le16(0xffff);
6913 	mutex_lock(&bp->hwrm_cmd_lock);
6914 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6915 	if (rc)
6916 		goto func_qcfg_exit;
6917 
6918 #ifdef CONFIG_BNXT_SRIOV
6919 	if (BNXT_VF(bp)) {
6920 		struct bnxt_vf_info *vf = &bp->vf;
6921 
6922 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
6923 	} else {
6924 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
6925 	}
6926 #endif
6927 	flags = le16_to_cpu(resp->flags);
6928 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
6929 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
6930 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
6931 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
6932 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
6933 	}
6934 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
6935 		bp->flags |= BNXT_FLAG_MULTI_HOST;
6936 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
6937 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
6938 
6939 	switch (resp->port_partition_type) {
6940 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
6941 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
6942 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
6943 		bp->port_partition_type = resp->port_partition_type;
6944 		break;
6945 	}
6946 	if (bp->hwrm_spec_code < 0x10707 ||
6947 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
6948 		bp->br_mode = BRIDGE_MODE_VEB;
6949 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
6950 		bp->br_mode = BRIDGE_MODE_VEPA;
6951 	else
6952 		bp->br_mode = BRIDGE_MODE_UNDEF;
6953 
6954 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
6955 	if (!bp->max_mtu)
6956 		bp->max_mtu = BNXT_MAX_MTU;
6957 
6958 	if (bp->db_size)
6959 		goto func_qcfg_exit;
6960 
6961 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
6962 		if (BNXT_PF(bp))
6963 			min_db_offset = DB_PF_OFFSET_P5;
6964 		else
6965 			min_db_offset = DB_VF_OFFSET_P5;
6966 	}
6967 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
6968 				 1024);
6969 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
6970 	    bp->db_size <= min_db_offset)
6971 		bp->db_size = pci_resource_len(bp->pdev, 2);
6972 
6973 func_qcfg_exit:
6974 	mutex_unlock(&bp->hwrm_cmd_lock);
6975 	return rc;
6976 }
6977 
6978 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_info *ctx,
6979 			struct hwrm_func_backing_store_qcaps_output *resp)
6980 {
6981 	struct bnxt_mem_init *mem_init;
6982 	u16 init_mask;
6983 	u8 init_val;
6984 	u8 *offset;
6985 	int i;
6986 
6987 	init_val = resp->ctx_kind_initializer;
6988 	init_mask = le16_to_cpu(resp->ctx_init_mask);
6989 	offset = &resp->qp_init_offset;
6990 	mem_init = &ctx->mem_init[BNXT_CTX_MEM_INIT_QP];
6991 	for (i = 0; i < BNXT_CTX_MEM_INIT_MAX; i++, mem_init++, offset++) {
6992 		mem_init->init_val = init_val;
6993 		mem_init->offset = BNXT_MEM_INVALID_OFFSET;
6994 		if (!init_mask)
6995 			continue;
6996 		if (i == BNXT_CTX_MEM_INIT_STAT)
6997 			offset = &resp->stat_init_offset;
6998 		if (init_mask & (1 << i))
6999 			mem_init->offset = *offset * 4;
7000 		else
7001 			mem_init->init_val = 0;
7002 	}
7003 	ctx->mem_init[BNXT_CTX_MEM_INIT_QP].size = ctx->qp_entry_size;
7004 	ctx->mem_init[BNXT_CTX_MEM_INIT_SRQ].size = ctx->srq_entry_size;
7005 	ctx->mem_init[BNXT_CTX_MEM_INIT_CQ].size = ctx->cq_entry_size;
7006 	ctx->mem_init[BNXT_CTX_MEM_INIT_VNIC].size = ctx->vnic_entry_size;
7007 	ctx->mem_init[BNXT_CTX_MEM_INIT_STAT].size = ctx->stat_entry_size;
7008 	ctx->mem_init[BNXT_CTX_MEM_INIT_MRAV].size = ctx->mrav_entry_size;
7009 }
7010 
7011 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
7012 {
7013 	struct hwrm_func_backing_store_qcaps_input req = {0};
7014 	struct hwrm_func_backing_store_qcaps_output *resp =
7015 		bp->hwrm_cmd_resp_addr;
7016 	int rc;
7017 
7018 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx)
7019 		return 0;
7020 
7021 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BACKING_STORE_QCAPS, -1, -1);
7022 	mutex_lock(&bp->hwrm_cmd_lock);
7023 	rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7024 	if (!rc) {
7025 		struct bnxt_ctx_pg_info *ctx_pg;
7026 		struct bnxt_ctx_mem_info *ctx;
7027 		int i, tqm_rings;
7028 
7029 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
7030 		if (!ctx) {
7031 			rc = -ENOMEM;
7032 			goto ctx_err;
7033 		}
7034 		ctx->qp_max_entries = le32_to_cpu(resp->qp_max_entries);
7035 		ctx->qp_min_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
7036 		ctx->qp_max_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
7037 		ctx->qp_entry_size = le16_to_cpu(resp->qp_entry_size);
7038 		ctx->srq_max_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
7039 		ctx->srq_max_entries = le32_to_cpu(resp->srq_max_entries);
7040 		ctx->srq_entry_size = le16_to_cpu(resp->srq_entry_size);
7041 		ctx->cq_max_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
7042 		ctx->cq_max_entries = le32_to_cpu(resp->cq_max_entries);
7043 		ctx->cq_entry_size = le16_to_cpu(resp->cq_entry_size);
7044 		ctx->vnic_max_vnic_entries =
7045 			le16_to_cpu(resp->vnic_max_vnic_entries);
7046 		ctx->vnic_max_ring_table_entries =
7047 			le16_to_cpu(resp->vnic_max_ring_table_entries);
7048 		ctx->vnic_entry_size = le16_to_cpu(resp->vnic_entry_size);
7049 		ctx->stat_max_entries = le32_to_cpu(resp->stat_max_entries);
7050 		ctx->stat_entry_size = le16_to_cpu(resp->stat_entry_size);
7051 		ctx->tqm_entry_size = le16_to_cpu(resp->tqm_entry_size);
7052 		ctx->tqm_min_entries_per_ring =
7053 			le32_to_cpu(resp->tqm_min_entries_per_ring);
7054 		ctx->tqm_max_entries_per_ring =
7055 			le32_to_cpu(resp->tqm_max_entries_per_ring);
7056 		ctx->tqm_entries_multiple = resp->tqm_entries_multiple;
7057 		if (!ctx->tqm_entries_multiple)
7058 			ctx->tqm_entries_multiple = 1;
7059 		ctx->mrav_max_entries = le32_to_cpu(resp->mrav_max_entries);
7060 		ctx->mrav_entry_size = le16_to_cpu(resp->mrav_entry_size);
7061 		ctx->mrav_num_entries_units =
7062 			le16_to_cpu(resp->mrav_num_entries_units);
7063 		ctx->tim_entry_size = le16_to_cpu(resp->tim_entry_size);
7064 		ctx->tim_max_entries = le32_to_cpu(resp->tim_max_entries);
7065 
7066 		bnxt_init_ctx_initializer(ctx, resp);
7067 
7068 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
7069 		if (!ctx->tqm_fp_rings_count)
7070 			ctx->tqm_fp_rings_count = bp->max_q;
7071 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
7072 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
7073 
7074 		tqm_rings = ctx->tqm_fp_rings_count + BNXT_MAX_TQM_SP_RINGS;
7075 		ctx_pg = kcalloc(tqm_rings, sizeof(*ctx_pg), GFP_KERNEL);
7076 		if (!ctx_pg) {
7077 			kfree(ctx);
7078 			rc = -ENOMEM;
7079 			goto ctx_err;
7080 		}
7081 		for (i = 0; i < tqm_rings; i++, ctx_pg++)
7082 			ctx->tqm_mem[i] = ctx_pg;
7083 		bp->ctx = ctx;
7084 	} else {
7085 		rc = 0;
7086 	}
7087 ctx_err:
7088 	mutex_unlock(&bp->hwrm_cmd_lock);
7089 	return rc;
7090 }
7091 
7092 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
7093 				  __le64 *pg_dir)
7094 {
7095 	if (!rmem->nr_pages)
7096 		return;
7097 
7098 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
7099 	if (rmem->depth >= 1) {
7100 		if (rmem->depth == 2)
7101 			*pg_attr |= 2;
7102 		else
7103 			*pg_attr |= 1;
7104 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
7105 	} else {
7106 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
7107 	}
7108 }
7109 
7110 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
7111 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
7112 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
7113 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
7114 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
7115 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
7116 
7117 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
7118 {
7119 	struct hwrm_func_backing_store_cfg_input req = {0};
7120 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
7121 	struct bnxt_ctx_pg_info *ctx_pg;
7122 	u32 req_len = sizeof(req);
7123 	__le32 *num_entries;
7124 	__le64 *pg_dir;
7125 	u32 flags = 0;
7126 	u8 *pg_attr;
7127 	u32 ena;
7128 	int i;
7129 
7130 	if (!ctx)
7131 		return 0;
7132 
7133 	if (req_len > bp->hwrm_max_ext_req_len)
7134 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
7135 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BACKING_STORE_CFG, -1, -1);
7136 	req.enables = cpu_to_le32(enables);
7137 
7138 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
7139 		ctx_pg = &ctx->qp_mem;
7140 		req.qp_num_entries = cpu_to_le32(ctx_pg->entries);
7141 		req.qp_num_qp1_entries = cpu_to_le16(ctx->qp_min_qp1_entries);
7142 		req.qp_num_l2_entries = cpu_to_le16(ctx->qp_max_l2_entries);
7143 		req.qp_entry_size = cpu_to_le16(ctx->qp_entry_size);
7144 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7145 				      &req.qpc_pg_size_qpc_lvl,
7146 				      &req.qpc_page_dir);
7147 	}
7148 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
7149 		ctx_pg = &ctx->srq_mem;
7150 		req.srq_num_entries = cpu_to_le32(ctx_pg->entries);
7151 		req.srq_num_l2_entries = cpu_to_le16(ctx->srq_max_l2_entries);
7152 		req.srq_entry_size = cpu_to_le16(ctx->srq_entry_size);
7153 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7154 				      &req.srq_pg_size_srq_lvl,
7155 				      &req.srq_page_dir);
7156 	}
7157 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
7158 		ctx_pg = &ctx->cq_mem;
7159 		req.cq_num_entries = cpu_to_le32(ctx_pg->entries);
7160 		req.cq_num_l2_entries = cpu_to_le16(ctx->cq_max_l2_entries);
7161 		req.cq_entry_size = cpu_to_le16(ctx->cq_entry_size);
7162 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, &req.cq_pg_size_cq_lvl,
7163 				      &req.cq_page_dir);
7164 	}
7165 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
7166 		ctx_pg = &ctx->vnic_mem;
7167 		req.vnic_num_vnic_entries =
7168 			cpu_to_le16(ctx->vnic_max_vnic_entries);
7169 		req.vnic_num_ring_table_entries =
7170 			cpu_to_le16(ctx->vnic_max_ring_table_entries);
7171 		req.vnic_entry_size = cpu_to_le16(ctx->vnic_entry_size);
7172 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7173 				      &req.vnic_pg_size_vnic_lvl,
7174 				      &req.vnic_page_dir);
7175 	}
7176 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
7177 		ctx_pg = &ctx->stat_mem;
7178 		req.stat_num_entries = cpu_to_le32(ctx->stat_max_entries);
7179 		req.stat_entry_size = cpu_to_le16(ctx->stat_entry_size);
7180 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7181 				      &req.stat_pg_size_stat_lvl,
7182 				      &req.stat_page_dir);
7183 	}
7184 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
7185 		ctx_pg = &ctx->mrav_mem;
7186 		req.mrav_num_entries = cpu_to_le32(ctx_pg->entries);
7187 		if (ctx->mrav_num_entries_units)
7188 			flags |=
7189 			FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
7190 		req.mrav_entry_size = cpu_to_le16(ctx->mrav_entry_size);
7191 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7192 				      &req.mrav_pg_size_mrav_lvl,
7193 				      &req.mrav_page_dir);
7194 	}
7195 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
7196 		ctx_pg = &ctx->tim_mem;
7197 		req.tim_num_entries = cpu_to_le32(ctx_pg->entries);
7198 		req.tim_entry_size = cpu_to_le16(ctx->tim_entry_size);
7199 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7200 				      &req.tim_pg_size_tim_lvl,
7201 				      &req.tim_page_dir);
7202 	}
7203 	for (i = 0, num_entries = &req.tqm_sp_num_entries,
7204 	     pg_attr = &req.tqm_sp_pg_size_tqm_sp_lvl,
7205 	     pg_dir = &req.tqm_sp_page_dir,
7206 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP;
7207 	     i < BNXT_MAX_TQM_RINGS;
7208 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
7209 		if (!(enables & ena))
7210 			continue;
7211 
7212 		req.tqm_entry_size = cpu_to_le16(ctx->tqm_entry_size);
7213 		ctx_pg = ctx->tqm_mem[i];
7214 		*num_entries = cpu_to_le32(ctx_pg->entries);
7215 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
7216 	}
7217 	req.flags = cpu_to_le32(flags);
7218 	return hwrm_send_message(bp, &req, req_len, HWRM_CMD_TIMEOUT);
7219 }
7220 
7221 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
7222 				  struct bnxt_ctx_pg_info *ctx_pg)
7223 {
7224 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7225 
7226 	rmem->page_size = BNXT_PAGE_SIZE;
7227 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
7228 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
7229 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
7230 	if (rmem->depth >= 1)
7231 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
7232 	return bnxt_alloc_ring(bp, rmem);
7233 }
7234 
7235 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
7236 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
7237 				  u8 depth, struct bnxt_mem_init *mem_init)
7238 {
7239 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7240 	int rc;
7241 
7242 	if (!mem_size)
7243 		return -EINVAL;
7244 
7245 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
7246 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
7247 		ctx_pg->nr_pages = 0;
7248 		return -EINVAL;
7249 	}
7250 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
7251 		int nr_tbls, i;
7252 
7253 		rmem->depth = 2;
7254 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
7255 					     GFP_KERNEL);
7256 		if (!ctx_pg->ctx_pg_tbl)
7257 			return -ENOMEM;
7258 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
7259 		rmem->nr_pages = nr_tbls;
7260 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
7261 		if (rc)
7262 			return rc;
7263 		for (i = 0; i < nr_tbls; i++) {
7264 			struct bnxt_ctx_pg_info *pg_tbl;
7265 
7266 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
7267 			if (!pg_tbl)
7268 				return -ENOMEM;
7269 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
7270 			rmem = &pg_tbl->ring_mem;
7271 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
7272 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
7273 			rmem->depth = 1;
7274 			rmem->nr_pages = MAX_CTX_PAGES;
7275 			rmem->mem_init = mem_init;
7276 			if (i == (nr_tbls - 1)) {
7277 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
7278 
7279 				if (rem)
7280 					rmem->nr_pages = rem;
7281 			}
7282 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
7283 			if (rc)
7284 				break;
7285 		}
7286 	} else {
7287 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
7288 		if (rmem->nr_pages > 1 || depth)
7289 			rmem->depth = 1;
7290 		rmem->mem_init = mem_init;
7291 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
7292 	}
7293 	return rc;
7294 }
7295 
7296 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
7297 				  struct bnxt_ctx_pg_info *ctx_pg)
7298 {
7299 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7300 
7301 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
7302 	    ctx_pg->ctx_pg_tbl) {
7303 		int i, nr_tbls = rmem->nr_pages;
7304 
7305 		for (i = 0; i < nr_tbls; i++) {
7306 			struct bnxt_ctx_pg_info *pg_tbl;
7307 			struct bnxt_ring_mem_info *rmem2;
7308 
7309 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
7310 			if (!pg_tbl)
7311 				continue;
7312 			rmem2 = &pg_tbl->ring_mem;
7313 			bnxt_free_ring(bp, rmem2);
7314 			ctx_pg->ctx_pg_arr[i] = NULL;
7315 			kfree(pg_tbl);
7316 			ctx_pg->ctx_pg_tbl[i] = NULL;
7317 		}
7318 		kfree(ctx_pg->ctx_pg_tbl);
7319 		ctx_pg->ctx_pg_tbl = NULL;
7320 	}
7321 	bnxt_free_ring(bp, rmem);
7322 	ctx_pg->nr_pages = 0;
7323 }
7324 
7325 static void bnxt_free_ctx_mem(struct bnxt *bp)
7326 {
7327 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
7328 	int i;
7329 
7330 	if (!ctx)
7331 		return;
7332 
7333 	if (ctx->tqm_mem[0]) {
7334 		for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
7335 			bnxt_free_ctx_pg_tbls(bp, ctx->tqm_mem[i]);
7336 		kfree(ctx->tqm_mem[0]);
7337 		ctx->tqm_mem[0] = NULL;
7338 	}
7339 
7340 	bnxt_free_ctx_pg_tbls(bp, &ctx->tim_mem);
7341 	bnxt_free_ctx_pg_tbls(bp, &ctx->mrav_mem);
7342 	bnxt_free_ctx_pg_tbls(bp, &ctx->stat_mem);
7343 	bnxt_free_ctx_pg_tbls(bp, &ctx->vnic_mem);
7344 	bnxt_free_ctx_pg_tbls(bp, &ctx->cq_mem);
7345 	bnxt_free_ctx_pg_tbls(bp, &ctx->srq_mem);
7346 	bnxt_free_ctx_pg_tbls(bp, &ctx->qp_mem);
7347 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
7348 }
7349 
7350 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
7351 {
7352 	struct bnxt_ctx_pg_info *ctx_pg;
7353 	struct bnxt_ctx_mem_info *ctx;
7354 	struct bnxt_mem_init *init;
7355 	u32 mem_size, ena, entries;
7356 	u32 entries_sp, min;
7357 	u32 num_mr, num_ah;
7358 	u32 extra_srqs = 0;
7359 	u32 extra_qps = 0;
7360 	u8 pg_lvl = 1;
7361 	int i, rc;
7362 
7363 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
7364 	if (rc) {
7365 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
7366 			   rc);
7367 		return rc;
7368 	}
7369 	ctx = bp->ctx;
7370 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
7371 		return 0;
7372 
7373 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
7374 		pg_lvl = 2;
7375 		extra_qps = 65536;
7376 		extra_srqs = 8192;
7377 	}
7378 
7379 	ctx_pg = &ctx->qp_mem;
7380 	ctx_pg->entries = ctx->qp_min_qp1_entries + ctx->qp_max_l2_entries +
7381 			  extra_qps;
7382 	if (ctx->qp_entry_size) {
7383 		mem_size = ctx->qp_entry_size * ctx_pg->entries;
7384 		init = &ctx->mem_init[BNXT_CTX_MEM_INIT_QP];
7385 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, init);
7386 		if (rc)
7387 			return rc;
7388 	}
7389 
7390 	ctx_pg = &ctx->srq_mem;
7391 	ctx_pg->entries = ctx->srq_max_l2_entries + extra_srqs;
7392 	if (ctx->srq_entry_size) {
7393 		mem_size = ctx->srq_entry_size * ctx_pg->entries;
7394 		init = &ctx->mem_init[BNXT_CTX_MEM_INIT_SRQ];
7395 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, init);
7396 		if (rc)
7397 			return rc;
7398 	}
7399 
7400 	ctx_pg = &ctx->cq_mem;
7401 	ctx_pg->entries = ctx->cq_max_l2_entries + extra_qps * 2;
7402 	if (ctx->cq_entry_size) {
7403 		mem_size = ctx->cq_entry_size * ctx_pg->entries;
7404 		init = &ctx->mem_init[BNXT_CTX_MEM_INIT_CQ];
7405 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, init);
7406 		if (rc)
7407 			return rc;
7408 	}
7409 
7410 	ctx_pg = &ctx->vnic_mem;
7411 	ctx_pg->entries = ctx->vnic_max_vnic_entries +
7412 			  ctx->vnic_max_ring_table_entries;
7413 	if (ctx->vnic_entry_size) {
7414 		mem_size = ctx->vnic_entry_size * ctx_pg->entries;
7415 		init = &ctx->mem_init[BNXT_CTX_MEM_INIT_VNIC];
7416 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, init);
7417 		if (rc)
7418 			return rc;
7419 	}
7420 
7421 	ctx_pg = &ctx->stat_mem;
7422 	ctx_pg->entries = ctx->stat_max_entries;
7423 	if (ctx->stat_entry_size) {
7424 		mem_size = ctx->stat_entry_size * ctx_pg->entries;
7425 		init = &ctx->mem_init[BNXT_CTX_MEM_INIT_STAT];
7426 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, init);
7427 		if (rc)
7428 			return rc;
7429 	}
7430 
7431 	ena = 0;
7432 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
7433 		goto skip_rdma;
7434 
7435 	ctx_pg = &ctx->mrav_mem;
7436 	/* 128K extra is needed to accommodate static AH context
7437 	 * allocation by f/w.
7438 	 */
7439 	num_mr = 1024 * 256;
7440 	num_ah = 1024 * 128;
7441 	ctx_pg->entries = num_mr + num_ah;
7442 	if (ctx->mrav_entry_size) {
7443 		mem_size = ctx->mrav_entry_size * ctx_pg->entries;
7444 		init = &ctx->mem_init[BNXT_CTX_MEM_INIT_MRAV];
7445 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 2, init);
7446 		if (rc)
7447 			return rc;
7448 	}
7449 	ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
7450 	if (ctx->mrav_num_entries_units)
7451 		ctx_pg->entries =
7452 			((num_mr / ctx->mrav_num_entries_units) << 16) |
7453 			 (num_ah / ctx->mrav_num_entries_units);
7454 
7455 	ctx_pg = &ctx->tim_mem;
7456 	ctx_pg->entries = ctx->qp_mem.entries;
7457 	if (ctx->tim_entry_size) {
7458 		mem_size = ctx->tim_entry_size * ctx_pg->entries;
7459 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, NULL);
7460 		if (rc)
7461 			return rc;
7462 	}
7463 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
7464 
7465 skip_rdma:
7466 	min = ctx->tqm_min_entries_per_ring;
7467 	entries_sp = ctx->vnic_max_vnic_entries + ctx->qp_max_l2_entries +
7468 		     2 * (extra_qps + ctx->qp_min_qp1_entries) + min;
7469 	entries_sp = roundup(entries_sp, ctx->tqm_entries_multiple);
7470 	entries = ctx->qp_max_l2_entries + 2 * (extra_qps + ctx->qp_min_qp1_entries);
7471 	entries = roundup(entries, ctx->tqm_entries_multiple);
7472 	entries = clamp_t(u32, entries, min, ctx->tqm_max_entries_per_ring);
7473 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++) {
7474 		ctx_pg = ctx->tqm_mem[i];
7475 		ctx_pg->entries = i ? entries : entries_sp;
7476 		if (ctx->tqm_entry_size) {
7477 			mem_size = ctx->tqm_entry_size * ctx_pg->entries;
7478 			rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1,
7479 						    NULL);
7480 			if (rc)
7481 				return rc;
7482 		}
7483 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
7484 	}
7485 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
7486 	rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
7487 	if (rc) {
7488 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
7489 			   rc);
7490 		return rc;
7491 	}
7492 	ctx->flags |= BNXT_CTX_FLAG_INITED;
7493 	return 0;
7494 }
7495 
7496 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
7497 {
7498 	struct hwrm_func_resource_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
7499 	struct hwrm_func_resource_qcaps_input req = {0};
7500 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7501 	int rc;
7502 
7503 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESOURCE_QCAPS, -1, -1);
7504 	req.fid = cpu_to_le16(0xffff);
7505 
7506 	mutex_lock(&bp->hwrm_cmd_lock);
7507 	rc = _hwrm_send_message_silent(bp, &req, sizeof(req),
7508 				       HWRM_CMD_TIMEOUT);
7509 	if (rc)
7510 		goto hwrm_func_resc_qcaps_exit;
7511 
7512 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
7513 	if (!all)
7514 		goto hwrm_func_resc_qcaps_exit;
7515 
7516 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
7517 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
7518 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
7519 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
7520 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
7521 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
7522 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
7523 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
7524 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
7525 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
7526 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
7527 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
7528 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
7529 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
7530 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
7531 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
7532 
7533 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
7534 		u16 max_msix = le16_to_cpu(resp->max_msix);
7535 
7536 		hw_resc->max_nqs = max_msix;
7537 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
7538 	}
7539 
7540 	if (BNXT_PF(bp)) {
7541 		struct bnxt_pf_info *pf = &bp->pf;
7542 
7543 		pf->vf_resv_strategy =
7544 			le16_to_cpu(resp->vf_reservation_strategy);
7545 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
7546 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
7547 	}
7548 hwrm_func_resc_qcaps_exit:
7549 	mutex_unlock(&bp->hwrm_cmd_lock);
7550 	return rc;
7551 }
7552 
7553 /* bp->hwrm_cmd_lock already held. */
7554 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
7555 {
7556 	struct hwrm_port_mac_ptp_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
7557 	struct hwrm_port_mac_ptp_qcfg_input req = {0};
7558 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
7559 	u8 flags;
7560 	int rc;
7561 
7562 	if (bp->hwrm_spec_code < 0x10801) {
7563 		rc = -ENODEV;
7564 		goto no_ptp;
7565 	}
7566 
7567 	req.port_id = cpu_to_le16(bp->pf.port_id);
7568 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_MAC_PTP_QCFG, -1, -1);
7569 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7570 	if (rc)
7571 		goto no_ptp;
7572 
7573 	flags = resp->flags;
7574 	if (!(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
7575 		rc = -ENODEV;
7576 		goto no_ptp;
7577 	}
7578 	if (!ptp) {
7579 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
7580 		if (!ptp)
7581 			return -ENOMEM;
7582 		ptp->bp = bp;
7583 		bp->ptp_cfg = ptp;
7584 	}
7585 	if (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK) {
7586 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
7587 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
7588 	} else if (bp->flags & BNXT_FLAG_CHIP_P5) {
7589 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
7590 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
7591 	} else {
7592 		rc = -ENODEV;
7593 		goto no_ptp;
7594 	}
7595 	rc = bnxt_ptp_init(bp);
7596 	if (!rc)
7597 		return 0;
7598 
7599 	netdev_warn(bp->dev, "PTP initialization failed.\n");
7600 
7601 no_ptp:
7602 	bnxt_ptp_clear(bp);
7603 	kfree(ptp);
7604 	bp->ptp_cfg = NULL;
7605 	return rc;
7606 }
7607 
7608 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
7609 {
7610 	int rc = 0;
7611 	struct hwrm_func_qcaps_input req = {0};
7612 	struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
7613 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7614 	u32 flags, flags_ext;
7615 
7616 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
7617 	req.fid = cpu_to_le16(0xffff);
7618 
7619 	mutex_lock(&bp->hwrm_cmd_lock);
7620 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7621 	if (rc)
7622 		goto hwrm_func_qcaps_exit;
7623 
7624 	flags = le32_to_cpu(resp->flags);
7625 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
7626 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
7627 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
7628 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
7629 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
7630 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
7631 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
7632 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
7633 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
7634 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
7635 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
7636 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
7637 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
7638 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
7639 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
7640 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
7641 
7642 	flags_ext = le32_to_cpu(resp->flags_ext);
7643 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
7644 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
7645 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
7646 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
7647 
7648 	bp->tx_push_thresh = 0;
7649 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
7650 	    BNXT_FW_MAJ(bp) > 217)
7651 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
7652 
7653 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
7654 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
7655 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
7656 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
7657 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
7658 	if (!hw_resc->max_hw_ring_grps)
7659 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
7660 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
7661 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
7662 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
7663 
7664 	if (BNXT_PF(bp)) {
7665 		struct bnxt_pf_info *pf = &bp->pf;
7666 
7667 		pf->fw_fid = le16_to_cpu(resp->fid);
7668 		pf->port_id = le16_to_cpu(resp->port_id);
7669 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
7670 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
7671 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
7672 		pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
7673 		pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
7674 		pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
7675 		pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
7676 		pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
7677 		pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
7678 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
7679 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
7680 			bp->flags |= BNXT_FLAG_WOL_CAP;
7681 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
7682 			__bnxt_hwrm_ptp_qcfg(bp);
7683 		} else {
7684 			bnxt_ptp_clear(bp);
7685 			kfree(bp->ptp_cfg);
7686 			bp->ptp_cfg = NULL;
7687 		}
7688 	} else {
7689 #ifdef CONFIG_BNXT_SRIOV
7690 		struct bnxt_vf_info *vf = &bp->vf;
7691 
7692 		vf->fw_fid = le16_to_cpu(resp->fid);
7693 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
7694 #endif
7695 	}
7696 
7697 hwrm_func_qcaps_exit:
7698 	mutex_unlock(&bp->hwrm_cmd_lock);
7699 	return rc;
7700 }
7701 
7702 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
7703 
7704 static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
7705 {
7706 	int rc;
7707 
7708 	rc = __bnxt_hwrm_func_qcaps(bp);
7709 	if (rc)
7710 		return rc;
7711 	rc = bnxt_hwrm_queue_qportcfg(bp);
7712 	if (rc) {
7713 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
7714 		return rc;
7715 	}
7716 	if (bp->hwrm_spec_code >= 0x10803) {
7717 		rc = bnxt_alloc_ctx_mem(bp);
7718 		if (rc)
7719 			return rc;
7720 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
7721 		if (!rc)
7722 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
7723 	}
7724 	return 0;
7725 }
7726 
7727 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
7728 {
7729 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input req = {0};
7730 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
7731 	int rc = 0;
7732 	u32 flags;
7733 
7734 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
7735 		return 0;
7736 
7737 	resp = bp->hwrm_cmd_resp_addr;
7738 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS, -1, -1);
7739 
7740 	mutex_lock(&bp->hwrm_cmd_lock);
7741 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7742 	if (rc)
7743 		goto hwrm_cfa_adv_qcaps_exit;
7744 
7745 	flags = le32_to_cpu(resp->flags);
7746 	if (flags &
7747 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
7748 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
7749 
7750 hwrm_cfa_adv_qcaps_exit:
7751 	mutex_unlock(&bp->hwrm_cmd_lock);
7752 	return rc;
7753 }
7754 
7755 static int __bnxt_alloc_fw_health(struct bnxt *bp)
7756 {
7757 	if (bp->fw_health)
7758 		return 0;
7759 
7760 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
7761 	if (!bp->fw_health)
7762 		return -ENOMEM;
7763 
7764 	return 0;
7765 }
7766 
7767 static int bnxt_alloc_fw_health(struct bnxt *bp)
7768 {
7769 	int rc;
7770 
7771 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
7772 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
7773 		return 0;
7774 
7775 	rc = __bnxt_alloc_fw_health(bp);
7776 	if (rc) {
7777 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
7778 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
7779 		return rc;
7780 	}
7781 
7782 	return 0;
7783 }
7784 
7785 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
7786 {
7787 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
7788 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
7789 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
7790 }
7791 
7792 bool bnxt_is_fw_healthy(struct bnxt *bp)
7793 {
7794 	if (bp->fw_health && bp->fw_health->status_reliable) {
7795 		u32 fw_status;
7796 
7797 		fw_status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
7798 		if (fw_status && !BNXT_FW_IS_HEALTHY(fw_status))
7799 			return false;
7800 	}
7801 
7802 	return true;
7803 }
7804 
7805 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
7806 {
7807 	struct bnxt_fw_health *fw_health = bp->fw_health;
7808 	u32 reg_type;
7809 
7810 	if (!fw_health || !fw_health->status_reliable)
7811 		return;
7812 
7813 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
7814 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
7815 		fw_health->status_reliable = false;
7816 }
7817 
7818 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
7819 {
7820 	void __iomem *hs;
7821 	u32 status_loc;
7822 	u32 reg_type;
7823 	u32 sig;
7824 
7825 	if (bp->fw_health)
7826 		bp->fw_health->status_reliable = false;
7827 
7828 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
7829 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
7830 
7831 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
7832 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
7833 		if (!bp->chip_num) {
7834 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
7835 			bp->chip_num = readl(bp->bar0 +
7836 					     BNXT_FW_HEALTH_WIN_BASE +
7837 					     BNXT_GRC_REG_CHIP_NUM);
7838 		}
7839 		if (!BNXT_CHIP_P5(bp))
7840 			return;
7841 
7842 		status_loc = BNXT_GRC_REG_STATUS_P5 |
7843 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
7844 	} else {
7845 		status_loc = readl(hs + offsetof(struct hcomm_status,
7846 						 fw_status_loc));
7847 	}
7848 
7849 	if (__bnxt_alloc_fw_health(bp)) {
7850 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
7851 		return;
7852 	}
7853 
7854 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
7855 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
7856 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
7857 		__bnxt_map_fw_health_reg(bp, status_loc);
7858 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
7859 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
7860 	}
7861 
7862 	bp->fw_health->status_reliable = true;
7863 }
7864 
7865 static int bnxt_map_fw_health_regs(struct bnxt *bp)
7866 {
7867 	struct bnxt_fw_health *fw_health = bp->fw_health;
7868 	u32 reg_base = 0xffffffff;
7869 	int i;
7870 
7871 	bp->fw_health->status_reliable = false;
7872 	/* Only pre-map the monitoring GRC registers using window 3 */
7873 	for (i = 0; i < 4; i++) {
7874 		u32 reg = fw_health->regs[i];
7875 
7876 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
7877 			continue;
7878 		if (reg_base == 0xffffffff)
7879 			reg_base = reg & BNXT_GRC_BASE_MASK;
7880 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
7881 			return -ERANGE;
7882 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
7883 	}
7884 	bp->fw_health->status_reliable = true;
7885 	if (reg_base == 0xffffffff)
7886 		return 0;
7887 
7888 	__bnxt_map_fw_health_reg(bp, reg_base);
7889 	return 0;
7890 }
7891 
7892 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
7893 {
7894 	struct hwrm_error_recovery_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
7895 	struct bnxt_fw_health *fw_health = bp->fw_health;
7896 	struct hwrm_error_recovery_qcfg_input req = {0};
7897 	int rc, i;
7898 
7899 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
7900 		return 0;
7901 
7902 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_ERROR_RECOVERY_QCFG, -1, -1);
7903 	mutex_lock(&bp->hwrm_cmd_lock);
7904 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7905 	if (rc)
7906 		goto err_recovery_out;
7907 	fw_health->flags = le32_to_cpu(resp->flags);
7908 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
7909 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
7910 		rc = -EINVAL;
7911 		goto err_recovery_out;
7912 	}
7913 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
7914 	fw_health->master_func_wait_dsecs =
7915 		le32_to_cpu(resp->master_func_wait_period);
7916 	fw_health->normal_func_wait_dsecs =
7917 		le32_to_cpu(resp->normal_func_wait_period);
7918 	fw_health->post_reset_wait_dsecs =
7919 		le32_to_cpu(resp->master_func_wait_period_after_reset);
7920 	fw_health->post_reset_max_wait_dsecs =
7921 		le32_to_cpu(resp->max_bailout_time_after_reset);
7922 	fw_health->regs[BNXT_FW_HEALTH_REG] =
7923 		le32_to_cpu(resp->fw_health_status_reg);
7924 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
7925 		le32_to_cpu(resp->fw_heartbeat_reg);
7926 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
7927 		le32_to_cpu(resp->fw_reset_cnt_reg);
7928 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
7929 		le32_to_cpu(resp->reset_inprogress_reg);
7930 	fw_health->fw_reset_inprog_reg_mask =
7931 		le32_to_cpu(resp->reset_inprogress_reg_mask);
7932 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
7933 	if (fw_health->fw_reset_seq_cnt >= 16) {
7934 		rc = -EINVAL;
7935 		goto err_recovery_out;
7936 	}
7937 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
7938 		fw_health->fw_reset_seq_regs[i] =
7939 			le32_to_cpu(resp->reset_reg[i]);
7940 		fw_health->fw_reset_seq_vals[i] =
7941 			le32_to_cpu(resp->reset_reg_val[i]);
7942 		fw_health->fw_reset_seq_delay_msec[i] =
7943 			resp->delay_after_reset[i];
7944 	}
7945 err_recovery_out:
7946 	mutex_unlock(&bp->hwrm_cmd_lock);
7947 	if (!rc)
7948 		rc = bnxt_map_fw_health_regs(bp);
7949 	if (rc)
7950 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
7951 	return rc;
7952 }
7953 
7954 static int bnxt_hwrm_func_reset(struct bnxt *bp)
7955 {
7956 	struct hwrm_func_reset_input req = {0};
7957 
7958 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
7959 	req.enables = 0;
7960 
7961 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
7962 }
7963 
7964 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
7965 {
7966 	struct hwrm_nvm_get_dev_info_output nvm_info;
7967 
7968 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
7969 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
7970 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
7971 			 nvm_info.nvm_cfg_ver_upd);
7972 }
7973 
7974 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
7975 {
7976 	int rc = 0;
7977 	struct hwrm_queue_qportcfg_input req = {0};
7978 	struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
7979 	u8 i, j, *qptr;
7980 	bool no_rdma;
7981 
7982 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
7983 
7984 	mutex_lock(&bp->hwrm_cmd_lock);
7985 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7986 	if (rc)
7987 		goto qportcfg_exit;
7988 
7989 	if (!resp->max_configurable_queues) {
7990 		rc = -EINVAL;
7991 		goto qportcfg_exit;
7992 	}
7993 	bp->max_tc = resp->max_configurable_queues;
7994 	bp->max_lltc = resp->max_configurable_lossless_queues;
7995 	if (bp->max_tc > BNXT_MAX_QUEUE)
7996 		bp->max_tc = BNXT_MAX_QUEUE;
7997 
7998 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
7999 	qptr = &resp->queue_id0;
8000 	for (i = 0, j = 0; i < bp->max_tc; i++) {
8001 		bp->q_info[j].queue_id = *qptr;
8002 		bp->q_ids[i] = *qptr++;
8003 		bp->q_info[j].queue_profile = *qptr++;
8004 		bp->tc_to_qidx[j] = j;
8005 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
8006 		    (no_rdma && BNXT_PF(bp)))
8007 			j++;
8008 	}
8009 	bp->max_q = bp->max_tc;
8010 	bp->max_tc = max_t(u8, j, 1);
8011 
8012 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
8013 		bp->max_tc = 1;
8014 
8015 	if (bp->max_lltc > bp->max_tc)
8016 		bp->max_lltc = bp->max_tc;
8017 
8018 qportcfg_exit:
8019 	mutex_unlock(&bp->hwrm_cmd_lock);
8020 	return rc;
8021 }
8022 
8023 static int __bnxt_hwrm_ver_get(struct bnxt *bp, bool silent)
8024 {
8025 	struct hwrm_ver_get_input req = {0};
8026 	int rc;
8027 
8028 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
8029 	req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
8030 	req.hwrm_intf_min = HWRM_VERSION_MINOR;
8031 	req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
8032 
8033 	rc = bnxt_hwrm_do_send_msg(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT,
8034 				   silent);
8035 	return rc;
8036 }
8037 
8038 static int bnxt_hwrm_ver_get(struct bnxt *bp)
8039 {
8040 	struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
8041 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
8042 	u32 dev_caps_cfg, hwrm_ver;
8043 	int rc, len;
8044 
8045 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
8046 	mutex_lock(&bp->hwrm_cmd_lock);
8047 	rc = __bnxt_hwrm_ver_get(bp, false);
8048 	if (rc)
8049 		goto hwrm_ver_get_exit;
8050 
8051 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
8052 
8053 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
8054 			     resp->hwrm_intf_min_8b << 8 |
8055 			     resp->hwrm_intf_upd_8b;
8056 	if (resp->hwrm_intf_maj_8b < 1) {
8057 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
8058 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
8059 			    resp->hwrm_intf_upd_8b);
8060 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
8061 	}
8062 
8063 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
8064 			HWRM_VERSION_UPDATE;
8065 
8066 	if (bp->hwrm_spec_code > hwrm_ver)
8067 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
8068 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
8069 			 HWRM_VERSION_UPDATE);
8070 	else
8071 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
8072 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
8073 			 resp->hwrm_intf_upd_8b);
8074 
8075 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
8076 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
8077 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
8078 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
8079 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
8080 		len = FW_VER_STR_LEN;
8081 	} else {
8082 		fw_maj = resp->hwrm_fw_maj_8b;
8083 		fw_min = resp->hwrm_fw_min_8b;
8084 		fw_bld = resp->hwrm_fw_bld_8b;
8085 		fw_rsv = resp->hwrm_fw_rsvd_8b;
8086 		len = BC_HWRM_STR_LEN;
8087 	}
8088 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
8089 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
8090 		 fw_rsv);
8091 
8092 	if (strlen(resp->active_pkg_name)) {
8093 		int fw_ver_len = strlen(bp->fw_ver_str);
8094 
8095 		snprintf(bp->fw_ver_str + fw_ver_len,
8096 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
8097 			 resp->active_pkg_name);
8098 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
8099 	}
8100 
8101 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
8102 	if (!bp->hwrm_cmd_timeout)
8103 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
8104 
8105 	if (resp->hwrm_intf_maj_8b >= 1) {
8106 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
8107 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
8108 	}
8109 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
8110 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
8111 
8112 	bp->chip_num = le16_to_cpu(resp->chip_num);
8113 	bp->chip_rev = resp->chip_rev;
8114 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
8115 	    !resp->chip_metal)
8116 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
8117 
8118 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
8119 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
8120 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
8121 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
8122 
8123 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
8124 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
8125 
8126 	if (dev_caps_cfg &
8127 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
8128 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
8129 
8130 	if (dev_caps_cfg &
8131 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
8132 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
8133 
8134 	if (dev_caps_cfg &
8135 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
8136 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
8137 
8138 hwrm_ver_get_exit:
8139 	mutex_unlock(&bp->hwrm_cmd_lock);
8140 	return rc;
8141 }
8142 
8143 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
8144 {
8145 	struct hwrm_fw_set_time_input req = {0};
8146 	struct tm tm;
8147 	time64_t now = ktime_get_real_seconds();
8148 
8149 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
8150 	    bp->hwrm_spec_code < 0x10400)
8151 		return -EOPNOTSUPP;
8152 
8153 	time64_to_tm(now, 0, &tm);
8154 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
8155 	req.year = cpu_to_le16(1900 + tm.tm_year);
8156 	req.month = 1 + tm.tm_mon;
8157 	req.day = tm.tm_mday;
8158 	req.hour = tm.tm_hour;
8159 	req.minute = tm.tm_min;
8160 	req.second = tm.tm_sec;
8161 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8162 }
8163 
8164 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
8165 {
8166 	u64 sw_tmp;
8167 
8168 	hw &= mask;
8169 	sw_tmp = (*sw & ~mask) | hw;
8170 	if (hw < (*sw & mask))
8171 		sw_tmp += mask + 1;
8172 	WRITE_ONCE(*sw, sw_tmp);
8173 }
8174 
8175 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
8176 				    int count, bool ignore_zero)
8177 {
8178 	int i;
8179 
8180 	for (i = 0; i < count; i++) {
8181 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
8182 
8183 		if (ignore_zero && !hw)
8184 			continue;
8185 
8186 		if (masks[i] == -1ULL)
8187 			sw_stats[i] = hw;
8188 		else
8189 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
8190 	}
8191 }
8192 
8193 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
8194 {
8195 	if (!stats->hw_stats)
8196 		return;
8197 
8198 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
8199 				stats->hw_masks, stats->len / 8, false);
8200 }
8201 
8202 static void bnxt_accumulate_all_stats(struct bnxt *bp)
8203 {
8204 	struct bnxt_stats_mem *ring0_stats;
8205 	bool ignore_zero = false;
8206 	int i;
8207 
8208 	/* Chip bug.  Counter intermittently becomes 0. */
8209 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8210 		ignore_zero = true;
8211 
8212 	for (i = 0; i < bp->cp_nr_rings; i++) {
8213 		struct bnxt_napi *bnapi = bp->bnapi[i];
8214 		struct bnxt_cp_ring_info *cpr;
8215 		struct bnxt_stats_mem *stats;
8216 
8217 		cpr = &bnapi->cp_ring;
8218 		stats = &cpr->stats;
8219 		if (!i)
8220 			ring0_stats = stats;
8221 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
8222 					ring0_stats->hw_masks,
8223 					ring0_stats->len / 8, ignore_zero);
8224 	}
8225 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
8226 		struct bnxt_stats_mem *stats = &bp->port_stats;
8227 		__le64 *hw_stats = stats->hw_stats;
8228 		u64 *sw_stats = stats->sw_stats;
8229 		u64 *masks = stats->hw_masks;
8230 		int cnt;
8231 
8232 		cnt = sizeof(struct rx_port_stats) / 8;
8233 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
8234 
8235 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
8236 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
8237 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
8238 		cnt = sizeof(struct tx_port_stats) / 8;
8239 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
8240 	}
8241 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
8242 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
8243 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
8244 	}
8245 }
8246 
8247 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
8248 {
8249 	struct bnxt_pf_info *pf = &bp->pf;
8250 	struct hwrm_port_qstats_input req = {0};
8251 
8252 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
8253 		return 0;
8254 
8255 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
8256 		return -EOPNOTSUPP;
8257 
8258 	req.flags = flags;
8259 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
8260 	req.port_id = cpu_to_le16(pf->port_id);
8261 	req.tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
8262 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
8263 	req.rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
8264 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8265 }
8266 
8267 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
8268 {
8269 	struct hwrm_port_qstats_ext_output *resp = bp->hwrm_cmd_resp_addr;
8270 	struct hwrm_queue_pri2cos_qcfg_input req2 = {0};
8271 	struct hwrm_port_qstats_ext_input req = {0};
8272 	struct bnxt_pf_info *pf = &bp->pf;
8273 	u32 tx_stat_size;
8274 	int rc;
8275 
8276 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
8277 		return 0;
8278 
8279 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
8280 		return -EOPNOTSUPP;
8281 
8282 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS_EXT, -1, -1);
8283 	req.flags = flags;
8284 	req.port_id = cpu_to_le16(pf->port_id);
8285 	req.rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
8286 	req.rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
8287 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
8288 		       sizeof(struct tx_port_stats_ext) : 0;
8289 	req.tx_stat_size = cpu_to_le16(tx_stat_size);
8290 	req.tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
8291 	mutex_lock(&bp->hwrm_cmd_lock);
8292 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8293 	if (!rc) {
8294 		bp->fw_rx_stats_ext_size = le16_to_cpu(resp->rx_stat_size) / 8;
8295 		bp->fw_tx_stats_ext_size = tx_stat_size ?
8296 			le16_to_cpu(resp->tx_stat_size) / 8 : 0;
8297 	} else {
8298 		bp->fw_rx_stats_ext_size = 0;
8299 		bp->fw_tx_stats_ext_size = 0;
8300 	}
8301 	if (flags)
8302 		goto qstats_done;
8303 
8304 	if (bp->fw_tx_stats_ext_size <=
8305 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
8306 		mutex_unlock(&bp->hwrm_cmd_lock);
8307 		bp->pri2cos_valid = 0;
8308 		return rc;
8309 	}
8310 
8311 	bnxt_hwrm_cmd_hdr_init(bp, &req2, HWRM_QUEUE_PRI2COS_QCFG, -1, -1);
8312 	req2.flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
8313 
8314 	rc = _hwrm_send_message(bp, &req2, sizeof(req2), HWRM_CMD_TIMEOUT);
8315 	if (!rc) {
8316 		struct hwrm_queue_pri2cos_qcfg_output *resp2;
8317 		u8 *pri2cos;
8318 		int i, j;
8319 
8320 		resp2 = bp->hwrm_cmd_resp_addr;
8321 		pri2cos = &resp2->pri0_cos_queue_id;
8322 		for (i = 0; i < 8; i++) {
8323 			u8 queue_id = pri2cos[i];
8324 			u8 queue_idx;
8325 
8326 			/* Per port queue IDs start from 0, 10, 20, etc */
8327 			queue_idx = queue_id % 10;
8328 			if (queue_idx > BNXT_MAX_QUEUE) {
8329 				bp->pri2cos_valid = false;
8330 				goto qstats_done;
8331 			}
8332 			for (j = 0; j < bp->max_q; j++) {
8333 				if (bp->q_ids[j] == queue_id)
8334 					bp->pri2cos_idx[i] = queue_idx;
8335 			}
8336 		}
8337 		bp->pri2cos_valid = 1;
8338 	}
8339 qstats_done:
8340 	mutex_unlock(&bp->hwrm_cmd_lock);
8341 	return rc;
8342 }
8343 
8344 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
8345 {
8346 	if (bp->vxlan_fw_dst_port_id != INVALID_HW_RING_ID)
8347 		bnxt_hwrm_tunnel_dst_port_free(
8348 			bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
8349 	if (bp->nge_fw_dst_port_id != INVALID_HW_RING_ID)
8350 		bnxt_hwrm_tunnel_dst_port_free(
8351 			bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
8352 }
8353 
8354 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
8355 {
8356 	int rc, i;
8357 	u32 tpa_flags = 0;
8358 
8359 	if (set_tpa)
8360 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
8361 	else if (BNXT_NO_FW_ACCESS(bp))
8362 		return 0;
8363 	for (i = 0; i < bp->nr_vnics; i++) {
8364 		rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
8365 		if (rc) {
8366 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
8367 				   i, rc);
8368 			return rc;
8369 		}
8370 	}
8371 	return 0;
8372 }
8373 
8374 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
8375 {
8376 	int i;
8377 
8378 	for (i = 0; i < bp->nr_vnics; i++)
8379 		bnxt_hwrm_vnic_set_rss(bp, i, false);
8380 }
8381 
8382 static void bnxt_clear_vnic(struct bnxt *bp)
8383 {
8384 	if (!bp->vnic_info)
8385 		return;
8386 
8387 	bnxt_hwrm_clear_vnic_filter(bp);
8388 	if (!(bp->flags & BNXT_FLAG_CHIP_P5)) {
8389 		/* clear all RSS setting before free vnic ctx */
8390 		bnxt_hwrm_clear_vnic_rss(bp);
8391 		bnxt_hwrm_vnic_ctx_free(bp);
8392 	}
8393 	/* before free the vnic, undo the vnic tpa settings */
8394 	if (bp->flags & BNXT_FLAG_TPA)
8395 		bnxt_set_tpa(bp, false);
8396 	bnxt_hwrm_vnic_free(bp);
8397 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8398 		bnxt_hwrm_vnic_ctx_free(bp);
8399 }
8400 
8401 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
8402 				    bool irq_re_init)
8403 {
8404 	bnxt_clear_vnic(bp);
8405 	bnxt_hwrm_ring_free(bp, close_path);
8406 	bnxt_hwrm_ring_grp_free(bp);
8407 	if (irq_re_init) {
8408 		bnxt_hwrm_stat_ctx_free(bp);
8409 		bnxt_hwrm_free_tunnel_ports(bp);
8410 	}
8411 }
8412 
8413 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
8414 {
8415 	struct hwrm_func_cfg_input req = {0};
8416 
8417 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
8418 	req.fid = cpu_to_le16(0xffff);
8419 	req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
8420 	if (br_mode == BRIDGE_MODE_VEB)
8421 		req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
8422 	else if (br_mode == BRIDGE_MODE_VEPA)
8423 		req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
8424 	else
8425 		return -EINVAL;
8426 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8427 }
8428 
8429 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
8430 {
8431 	struct hwrm_func_cfg_input req = {0};
8432 
8433 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
8434 		return 0;
8435 
8436 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
8437 	req.fid = cpu_to_le16(0xffff);
8438 	req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
8439 	req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
8440 	if (size == 128)
8441 		req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
8442 
8443 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8444 }
8445 
8446 static int __bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
8447 {
8448 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
8449 	int rc;
8450 
8451 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
8452 		goto skip_rss_ctx;
8453 
8454 	/* allocate context for vnic */
8455 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
8456 	if (rc) {
8457 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
8458 			   vnic_id, rc);
8459 		goto vnic_setup_err;
8460 	}
8461 	bp->rsscos_nr_ctxs++;
8462 
8463 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8464 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
8465 		if (rc) {
8466 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
8467 				   vnic_id, rc);
8468 			goto vnic_setup_err;
8469 		}
8470 		bp->rsscos_nr_ctxs++;
8471 	}
8472 
8473 skip_rss_ctx:
8474 	/* configure default vnic, ring grp */
8475 	rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
8476 	if (rc) {
8477 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
8478 			   vnic_id, rc);
8479 		goto vnic_setup_err;
8480 	}
8481 
8482 	/* Enable RSS hashing on vnic */
8483 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
8484 	if (rc) {
8485 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
8486 			   vnic_id, rc);
8487 		goto vnic_setup_err;
8488 	}
8489 
8490 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8491 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
8492 		if (rc) {
8493 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
8494 				   vnic_id, rc);
8495 		}
8496 	}
8497 
8498 vnic_setup_err:
8499 	return rc;
8500 }
8501 
8502 static int __bnxt_setup_vnic_p5(struct bnxt *bp, u16 vnic_id)
8503 {
8504 	int rc, i, nr_ctxs;
8505 
8506 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
8507 	for (i = 0; i < nr_ctxs; i++) {
8508 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, i);
8509 		if (rc) {
8510 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
8511 				   vnic_id, i, rc);
8512 			break;
8513 		}
8514 		bp->rsscos_nr_ctxs++;
8515 	}
8516 	if (i < nr_ctxs)
8517 		return -ENOMEM;
8518 
8519 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic_id, true);
8520 	if (rc) {
8521 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
8522 			   vnic_id, rc);
8523 		return rc;
8524 	}
8525 	rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
8526 	if (rc) {
8527 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
8528 			   vnic_id, rc);
8529 		return rc;
8530 	}
8531 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8532 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
8533 		if (rc) {
8534 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
8535 				   vnic_id, rc);
8536 		}
8537 	}
8538 	return rc;
8539 }
8540 
8541 static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
8542 {
8543 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8544 		return __bnxt_setup_vnic_p5(bp, vnic_id);
8545 	else
8546 		return __bnxt_setup_vnic(bp, vnic_id);
8547 }
8548 
8549 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
8550 {
8551 #ifdef CONFIG_RFS_ACCEL
8552 	int i, rc = 0;
8553 
8554 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8555 		return 0;
8556 
8557 	for (i = 0; i < bp->rx_nr_rings; i++) {
8558 		struct bnxt_vnic_info *vnic;
8559 		u16 vnic_id = i + 1;
8560 		u16 ring_id = i;
8561 
8562 		if (vnic_id >= bp->nr_vnics)
8563 			break;
8564 
8565 		vnic = &bp->vnic_info[vnic_id];
8566 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
8567 		if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
8568 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
8569 		rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
8570 		if (rc) {
8571 			netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
8572 				   vnic_id, rc);
8573 			break;
8574 		}
8575 		rc = bnxt_setup_vnic(bp, vnic_id);
8576 		if (rc)
8577 			break;
8578 	}
8579 	return rc;
8580 #else
8581 	return 0;
8582 #endif
8583 }
8584 
8585 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
8586 static bool bnxt_promisc_ok(struct bnxt *bp)
8587 {
8588 #ifdef CONFIG_BNXT_SRIOV
8589 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
8590 		return false;
8591 #endif
8592 	return true;
8593 }
8594 
8595 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
8596 {
8597 	unsigned int rc = 0;
8598 
8599 	rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
8600 	if (rc) {
8601 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
8602 			   rc);
8603 		return rc;
8604 	}
8605 
8606 	rc = bnxt_hwrm_vnic_cfg(bp, 1);
8607 	if (rc) {
8608 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
8609 			   rc);
8610 		return rc;
8611 	}
8612 	return rc;
8613 }
8614 
8615 static int bnxt_cfg_rx_mode(struct bnxt *);
8616 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
8617 
8618 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
8619 {
8620 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
8621 	int rc = 0;
8622 	unsigned int rx_nr_rings = bp->rx_nr_rings;
8623 
8624 	if (irq_re_init) {
8625 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
8626 		if (rc) {
8627 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
8628 				   rc);
8629 			goto err_out;
8630 		}
8631 	}
8632 
8633 	rc = bnxt_hwrm_ring_alloc(bp);
8634 	if (rc) {
8635 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
8636 		goto err_out;
8637 	}
8638 
8639 	rc = bnxt_hwrm_ring_grp_alloc(bp);
8640 	if (rc) {
8641 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
8642 		goto err_out;
8643 	}
8644 
8645 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8646 		rx_nr_rings--;
8647 
8648 	/* default vnic 0 */
8649 	rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
8650 	if (rc) {
8651 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
8652 		goto err_out;
8653 	}
8654 
8655 	rc = bnxt_setup_vnic(bp, 0);
8656 	if (rc)
8657 		goto err_out;
8658 
8659 	if (bp->flags & BNXT_FLAG_RFS) {
8660 		rc = bnxt_alloc_rfs_vnics(bp);
8661 		if (rc)
8662 			goto err_out;
8663 	}
8664 
8665 	if (bp->flags & BNXT_FLAG_TPA) {
8666 		rc = bnxt_set_tpa(bp, true);
8667 		if (rc)
8668 			goto err_out;
8669 	}
8670 
8671 	if (BNXT_VF(bp))
8672 		bnxt_update_vf_mac(bp);
8673 
8674 	/* Filter for default vnic 0 */
8675 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
8676 	if (rc) {
8677 		netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
8678 		goto err_out;
8679 	}
8680 	vnic->uc_filter_count = 1;
8681 
8682 	vnic->rx_mask = 0;
8683 	if (bp->dev->flags & IFF_BROADCAST)
8684 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
8685 
8686 	if (bp->dev->flags & IFF_PROMISC)
8687 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
8688 
8689 	if (bp->dev->flags & IFF_ALLMULTI) {
8690 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
8691 		vnic->mc_list_count = 0;
8692 	} else {
8693 		u32 mask = 0;
8694 
8695 		bnxt_mc_list_updated(bp, &mask);
8696 		vnic->rx_mask |= mask;
8697 	}
8698 
8699 	rc = bnxt_cfg_rx_mode(bp);
8700 	if (rc)
8701 		goto err_out;
8702 
8703 	rc = bnxt_hwrm_set_coal(bp);
8704 	if (rc)
8705 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
8706 				rc);
8707 
8708 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8709 		rc = bnxt_setup_nitroa0_vnic(bp);
8710 		if (rc)
8711 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
8712 				   rc);
8713 	}
8714 
8715 	if (BNXT_VF(bp)) {
8716 		bnxt_hwrm_func_qcfg(bp);
8717 		netdev_update_features(bp->dev);
8718 	}
8719 
8720 	return 0;
8721 
8722 err_out:
8723 	bnxt_hwrm_resource_free(bp, 0, true);
8724 
8725 	return rc;
8726 }
8727 
8728 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
8729 {
8730 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
8731 	return 0;
8732 }
8733 
8734 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
8735 {
8736 	bnxt_init_cp_rings(bp);
8737 	bnxt_init_rx_rings(bp);
8738 	bnxt_init_tx_rings(bp);
8739 	bnxt_init_ring_grps(bp, irq_re_init);
8740 	bnxt_init_vnics(bp);
8741 
8742 	return bnxt_init_chip(bp, irq_re_init);
8743 }
8744 
8745 static int bnxt_set_real_num_queues(struct bnxt *bp)
8746 {
8747 	int rc;
8748 	struct net_device *dev = bp->dev;
8749 
8750 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
8751 					  bp->tx_nr_rings_xdp);
8752 	if (rc)
8753 		return rc;
8754 
8755 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
8756 	if (rc)
8757 		return rc;
8758 
8759 #ifdef CONFIG_RFS_ACCEL
8760 	if (bp->flags & BNXT_FLAG_RFS)
8761 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
8762 #endif
8763 
8764 	return rc;
8765 }
8766 
8767 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
8768 			   bool shared)
8769 {
8770 	int _rx = *rx, _tx = *tx;
8771 
8772 	if (shared) {
8773 		*rx = min_t(int, _rx, max);
8774 		*tx = min_t(int, _tx, max);
8775 	} else {
8776 		if (max < 2)
8777 			return -ENOMEM;
8778 
8779 		while (_rx + _tx > max) {
8780 			if (_rx > _tx && _rx > 1)
8781 				_rx--;
8782 			else if (_tx > 1)
8783 				_tx--;
8784 		}
8785 		*rx = _rx;
8786 		*tx = _tx;
8787 	}
8788 	return 0;
8789 }
8790 
8791 static void bnxt_setup_msix(struct bnxt *bp)
8792 {
8793 	const int len = sizeof(bp->irq_tbl[0].name);
8794 	struct net_device *dev = bp->dev;
8795 	int tcs, i;
8796 
8797 	tcs = netdev_get_num_tc(dev);
8798 	if (tcs) {
8799 		int i, off, count;
8800 
8801 		for (i = 0; i < tcs; i++) {
8802 			count = bp->tx_nr_rings_per_tc;
8803 			off = i * count;
8804 			netdev_set_tc_queue(dev, i, count, off);
8805 		}
8806 	}
8807 
8808 	for (i = 0; i < bp->cp_nr_rings; i++) {
8809 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
8810 		char *attr;
8811 
8812 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
8813 			attr = "TxRx";
8814 		else if (i < bp->rx_nr_rings)
8815 			attr = "rx";
8816 		else
8817 			attr = "tx";
8818 
8819 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
8820 			 attr, i);
8821 		bp->irq_tbl[map_idx].handler = bnxt_msix;
8822 	}
8823 }
8824 
8825 static void bnxt_setup_inta(struct bnxt *bp)
8826 {
8827 	const int len = sizeof(bp->irq_tbl[0].name);
8828 
8829 	if (netdev_get_num_tc(bp->dev))
8830 		netdev_reset_tc(bp->dev);
8831 
8832 	snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
8833 		 0);
8834 	bp->irq_tbl[0].handler = bnxt_inta;
8835 }
8836 
8837 static int bnxt_init_int_mode(struct bnxt *bp);
8838 
8839 static int bnxt_setup_int_mode(struct bnxt *bp)
8840 {
8841 	int rc;
8842 
8843 	if (!bp->irq_tbl) {
8844 		rc = bnxt_init_int_mode(bp);
8845 		if (rc || !bp->irq_tbl)
8846 			return rc ?: -ENODEV;
8847 	}
8848 
8849 	if (bp->flags & BNXT_FLAG_USING_MSIX)
8850 		bnxt_setup_msix(bp);
8851 	else
8852 		bnxt_setup_inta(bp);
8853 
8854 	rc = bnxt_set_real_num_queues(bp);
8855 	return rc;
8856 }
8857 
8858 #ifdef CONFIG_RFS_ACCEL
8859 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
8860 {
8861 	return bp->hw_resc.max_rsscos_ctxs;
8862 }
8863 
8864 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
8865 {
8866 	return bp->hw_resc.max_vnics;
8867 }
8868 #endif
8869 
8870 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
8871 {
8872 	return bp->hw_resc.max_stat_ctxs;
8873 }
8874 
8875 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
8876 {
8877 	return bp->hw_resc.max_cp_rings;
8878 }
8879 
8880 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
8881 {
8882 	unsigned int cp = bp->hw_resc.max_cp_rings;
8883 
8884 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
8885 		cp -= bnxt_get_ulp_msix_num(bp);
8886 
8887 	return cp;
8888 }
8889 
8890 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
8891 {
8892 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8893 
8894 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8895 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
8896 
8897 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
8898 }
8899 
8900 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
8901 {
8902 	bp->hw_resc.max_irqs = max_irqs;
8903 }
8904 
8905 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
8906 {
8907 	unsigned int cp;
8908 
8909 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
8910 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8911 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
8912 	else
8913 		return cp - bp->cp_nr_rings;
8914 }
8915 
8916 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
8917 {
8918 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
8919 }
8920 
8921 int bnxt_get_avail_msix(struct bnxt *bp, int num)
8922 {
8923 	int max_cp = bnxt_get_max_func_cp_rings(bp);
8924 	int max_irq = bnxt_get_max_func_irqs(bp);
8925 	int total_req = bp->cp_nr_rings + num;
8926 	int max_idx, avail_msix;
8927 
8928 	max_idx = bp->total_irqs;
8929 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
8930 		max_idx = min_t(int, bp->total_irqs, max_cp);
8931 	avail_msix = max_idx - bp->cp_nr_rings;
8932 	if (!BNXT_NEW_RM(bp) || avail_msix >= num)
8933 		return avail_msix;
8934 
8935 	if (max_irq < total_req) {
8936 		num = max_irq - bp->cp_nr_rings;
8937 		if (num <= 0)
8938 			return 0;
8939 	}
8940 	return num;
8941 }
8942 
8943 static int bnxt_get_num_msix(struct bnxt *bp)
8944 {
8945 	if (!BNXT_NEW_RM(bp))
8946 		return bnxt_get_max_func_irqs(bp);
8947 
8948 	return bnxt_nq_rings_in_use(bp);
8949 }
8950 
8951 static int bnxt_init_msix(struct bnxt *bp)
8952 {
8953 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix;
8954 	struct msix_entry *msix_ent;
8955 
8956 	total_vecs = bnxt_get_num_msix(bp);
8957 	max = bnxt_get_max_func_irqs(bp);
8958 	if (total_vecs > max)
8959 		total_vecs = max;
8960 
8961 	if (!total_vecs)
8962 		return 0;
8963 
8964 	msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
8965 	if (!msix_ent)
8966 		return -ENOMEM;
8967 
8968 	for (i = 0; i < total_vecs; i++) {
8969 		msix_ent[i].entry = i;
8970 		msix_ent[i].vector = 0;
8971 	}
8972 
8973 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
8974 		min = 2;
8975 
8976 	total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
8977 	ulp_msix = bnxt_get_ulp_msix_num(bp);
8978 	if (total_vecs < 0 || total_vecs < ulp_msix) {
8979 		rc = -ENODEV;
8980 		goto msix_setup_exit;
8981 	}
8982 
8983 	bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
8984 	if (bp->irq_tbl) {
8985 		for (i = 0; i < total_vecs; i++)
8986 			bp->irq_tbl[i].vector = msix_ent[i].vector;
8987 
8988 		bp->total_irqs = total_vecs;
8989 		/* Trim rings based upon num of vectors allocated */
8990 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
8991 				     total_vecs - ulp_msix, min == 1);
8992 		if (rc)
8993 			goto msix_setup_exit;
8994 
8995 		bp->cp_nr_rings = (min == 1) ?
8996 				  max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
8997 				  bp->tx_nr_rings + bp->rx_nr_rings;
8998 
8999 	} else {
9000 		rc = -ENOMEM;
9001 		goto msix_setup_exit;
9002 	}
9003 	bp->flags |= BNXT_FLAG_USING_MSIX;
9004 	kfree(msix_ent);
9005 	return 0;
9006 
9007 msix_setup_exit:
9008 	netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
9009 	kfree(bp->irq_tbl);
9010 	bp->irq_tbl = NULL;
9011 	pci_disable_msix(bp->pdev);
9012 	kfree(msix_ent);
9013 	return rc;
9014 }
9015 
9016 static int bnxt_init_inta(struct bnxt *bp)
9017 {
9018 	bp->irq_tbl = kzalloc(sizeof(struct bnxt_irq), GFP_KERNEL);
9019 	if (!bp->irq_tbl)
9020 		return -ENOMEM;
9021 
9022 	bp->total_irqs = 1;
9023 	bp->rx_nr_rings = 1;
9024 	bp->tx_nr_rings = 1;
9025 	bp->cp_nr_rings = 1;
9026 	bp->flags |= BNXT_FLAG_SHARED_RINGS;
9027 	bp->irq_tbl[0].vector = bp->pdev->irq;
9028 	return 0;
9029 }
9030 
9031 static int bnxt_init_int_mode(struct bnxt *bp)
9032 {
9033 	int rc = -ENODEV;
9034 
9035 	if (bp->flags & BNXT_FLAG_MSIX_CAP)
9036 		rc = bnxt_init_msix(bp);
9037 
9038 	if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
9039 		/* fallback to INTA */
9040 		rc = bnxt_init_inta(bp);
9041 	}
9042 	return rc;
9043 }
9044 
9045 static void bnxt_clear_int_mode(struct bnxt *bp)
9046 {
9047 	if (bp->flags & BNXT_FLAG_USING_MSIX)
9048 		pci_disable_msix(bp->pdev);
9049 
9050 	kfree(bp->irq_tbl);
9051 	bp->irq_tbl = NULL;
9052 	bp->flags &= ~BNXT_FLAG_USING_MSIX;
9053 }
9054 
9055 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
9056 {
9057 	int tcs = netdev_get_num_tc(bp->dev);
9058 	bool irq_cleared = false;
9059 	int rc;
9060 
9061 	if (!bnxt_need_reserve_rings(bp))
9062 		return 0;
9063 
9064 	if (irq_re_init && BNXT_NEW_RM(bp) &&
9065 	    bnxt_get_num_msix(bp) != bp->total_irqs) {
9066 		bnxt_ulp_irq_stop(bp);
9067 		bnxt_clear_int_mode(bp);
9068 		irq_cleared = true;
9069 	}
9070 	rc = __bnxt_reserve_rings(bp);
9071 	if (irq_cleared) {
9072 		if (!rc)
9073 			rc = bnxt_init_int_mode(bp);
9074 		bnxt_ulp_irq_restart(bp, rc);
9075 	}
9076 	if (rc) {
9077 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
9078 		return rc;
9079 	}
9080 	if (tcs && (bp->tx_nr_rings_per_tc * tcs != bp->tx_nr_rings)) {
9081 		netdev_err(bp->dev, "tx ring reservation failure\n");
9082 		netdev_reset_tc(bp->dev);
9083 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
9084 		return -ENOMEM;
9085 	}
9086 	return 0;
9087 }
9088 
9089 static void bnxt_free_irq(struct bnxt *bp)
9090 {
9091 	struct bnxt_irq *irq;
9092 	int i;
9093 
9094 #ifdef CONFIG_RFS_ACCEL
9095 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
9096 	bp->dev->rx_cpu_rmap = NULL;
9097 #endif
9098 	if (!bp->irq_tbl || !bp->bnapi)
9099 		return;
9100 
9101 	for (i = 0; i < bp->cp_nr_rings; i++) {
9102 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
9103 
9104 		irq = &bp->irq_tbl[map_idx];
9105 		if (irq->requested) {
9106 			if (irq->have_cpumask) {
9107 				irq_set_affinity_hint(irq->vector, NULL);
9108 				free_cpumask_var(irq->cpu_mask);
9109 				irq->have_cpumask = 0;
9110 			}
9111 			free_irq(irq->vector, bp->bnapi[i]);
9112 		}
9113 
9114 		irq->requested = 0;
9115 	}
9116 }
9117 
9118 static int bnxt_request_irq(struct bnxt *bp)
9119 {
9120 	int i, j, rc = 0;
9121 	unsigned long flags = 0;
9122 #ifdef CONFIG_RFS_ACCEL
9123 	struct cpu_rmap *rmap;
9124 #endif
9125 
9126 	rc = bnxt_setup_int_mode(bp);
9127 	if (rc) {
9128 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
9129 			   rc);
9130 		return rc;
9131 	}
9132 #ifdef CONFIG_RFS_ACCEL
9133 	rmap = bp->dev->rx_cpu_rmap;
9134 #endif
9135 	if (!(bp->flags & BNXT_FLAG_USING_MSIX))
9136 		flags = IRQF_SHARED;
9137 
9138 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
9139 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
9140 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
9141 
9142 #ifdef CONFIG_RFS_ACCEL
9143 		if (rmap && bp->bnapi[i]->rx_ring) {
9144 			rc = irq_cpu_rmap_add(rmap, irq->vector);
9145 			if (rc)
9146 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
9147 					    j);
9148 			j++;
9149 		}
9150 #endif
9151 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
9152 				 bp->bnapi[i]);
9153 		if (rc)
9154 			break;
9155 
9156 		irq->requested = 1;
9157 
9158 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
9159 			int numa_node = dev_to_node(&bp->pdev->dev);
9160 
9161 			irq->have_cpumask = 1;
9162 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
9163 					irq->cpu_mask);
9164 			rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
9165 			if (rc) {
9166 				netdev_warn(bp->dev,
9167 					    "Set affinity failed, IRQ = %d\n",
9168 					    irq->vector);
9169 				break;
9170 			}
9171 		}
9172 	}
9173 	return rc;
9174 }
9175 
9176 static void bnxt_del_napi(struct bnxt *bp)
9177 {
9178 	int i;
9179 
9180 	if (!bp->bnapi)
9181 		return;
9182 
9183 	for (i = 0; i < bp->cp_nr_rings; i++) {
9184 		struct bnxt_napi *bnapi = bp->bnapi[i];
9185 
9186 		__netif_napi_del(&bnapi->napi);
9187 	}
9188 	/* We called __netif_napi_del(), we need
9189 	 * to respect an RCU grace period before freeing napi structures.
9190 	 */
9191 	synchronize_net();
9192 }
9193 
9194 static void bnxt_init_napi(struct bnxt *bp)
9195 {
9196 	int i;
9197 	unsigned int cp_nr_rings = bp->cp_nr_rings;
9198 	struct bnxt_napi *bnapi;
9199 
9200 	if (bp->flags & BNXT_FLAG_USING_MSIX) {
9201 		int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
9202 
9203 		if (bp->flags & BNXT_FLAG_CHIP_P5)
9204 			poll_fn = bnxt_poll_p5;
9205 		else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
9206 			cp_nr_rings--;
9207 		for (i = 0; i < cp_nr_rings; i++) {
9208 			bnapi = bp->bnapi[i];
9209 			netif_napi_add(bp->dev, &bnapi->napi, poll_fn, 64);
9210 		}
9211 		if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
9212 			bnapi = bp->bnapi[cp_nr_rings];
9213 			netif_napi_add(bp->dev, &bnapi->napi,
9214 				       bnxt_poll_nitroa0, 64);
9215 		}
9216 	} else {
9217 		bnapi = bp->bnapi[0];
9218 		netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
9219 	}
9220 }
9221 
9222 static void bnxt_disable_napi(struct bnxt *bp)
9223 {
9224 	int i;
9225 
9226 	if (!bp->bnapi ||
9227 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
9228 		return;
9229 
9230 	for (i = 0; i < bp->cp_nr_rings; i++) {
9231 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
9232 
9233 		if (bp->bnapi[i]->rx_ring)
9234 			cancel_work_sync(&cpr->dim.work);
9235 
9236 		napi_disable(&bp->bnapi[i]->napi);
9237 	}
9238 }
9239 
9240 static void bnxt_enable_napi(struct bnxt *bp)
9241 {
9242 	int i;
9243 
9244 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
9245 	for (i = 0; i < bp->cp_nr_rings; i++) {
9246 		struct bnxt_napi *bnapi = bp->bnapi[i];
9247 		struct bnxt_cp_ring_info *cpr;
9248 
9249 		cpr = &bnapi->cp_ring;
9250 		if (bnapi->in_reset)
9251 			cpr->sw_stats.rx.rx_resets++;
9252 		bnapi->in_reset = false;
9253 
9254 		if (bnapi->rx_ring) {
9255 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
9256 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
9257 		}
9258 		napi_enable(&bnapi->napi);
9259 	}
9260 }
9261 
9262 void bnxt_tx_disable(struct bnxt *bp)
9263 {
9264 	int i;
9265 	struct bnxt_tx_ring_info *txr;
9266 
9267 	if (bp->tx_ring) {
9268 		for (i = 0; i < bp->tx_nr_rings; i++) {
9269 			txr = &bp->tx_ring[i];
9270 			txr->dev_state = BNXT_DEV_STATE_CLOSING;
9271 		}
9272 	}
9273 	/* Drop carrier first to prevent TX timeout */
9274 	netif_carrier_off(bp->dev);
9275 	/* Stop all TX queues */
9276 	netif_tx_disable(bp->dev);
9277 }
9278 
9279 void bnxt_tx_enable(struct bnxt *bp)
9280 {
9281 	int i;
9282 	struct bnxt_tx_ring_info *txr;
9283 
9284 	for (i = 0; i < bp->tx_nr_rings; i++) {
9285 		txr = &bp->tx_ring[i];
9286 		txr->dev_state = 0;
9287 	}
9288 	netif_tx_wake_all_queues(bp->dev);
9289 	if (bp->link_info.link_up)
9290 		netif_carrier_on(bp->dev);
9291 }
9292 
9293 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
9294 {
9295 	u8 active_fec = link_info->active_fec_sig_mode &
9296 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
9297 
9298 	switch (active_fec) {
9299 	default:
9300 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
9301 		return "None";
9302 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
9303 		return "Clause 74 BaseR";
9304 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
9305 		return "Clause 91 RS(528,514)";
9306 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
9307 		return "Clause 91 RS544_1XN";
9308 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
9309 		return "Clause 91 RS(544,514)";
9310 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
9311 		return "Clause 91 RS272_1XN";
9312 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
9313 		return "Clause 91 RS(272,257)";
9314 	}
9315 }
9316 
9317 static void bnxt_report_link(struct bnxt *bp)
9318 {
9319 	if (bp->link_info.link_up) {
9320 		const char *signal = "";
9321 		const char *flow_ctrl;
9322 		const char *duplex;
9323 		u32 speed;
9324 		u16 fec;
9325 
9326 		netif_carrier_on(bp->dev);
9327 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
9328 		if (speed == SPEED_UNKNOWN) {
9329 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
9330 			return;
9331 		}
9332 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
9333 			duplex = "full";
9334 		else
9335 			duplex = "half";
9336 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
9337 			flow_ctrl = "ON - receive & transmit";
9338 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
9339 			flow_ctrl = "ON - transmit";
9340 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
9341 			flow_ctrl = "ON - receive";
9342 		else
9343 			flow_ctrl = "none";
9344 		if (bp->link_info.phy_qcfg_resp.option_flags &
9345 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
9346 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
9347 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
9348 			switch (sig_mode) {
9349 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
9350 				signal = "(NRZ) ";
9351 				break;
9352 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
9353 				signal = "(PAM4) ";
9354 				break;
9355 			default:
9356 				break;
9357 			}
9358 		}
9359 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
9360 			    speed, signal, duplex, flow_ctrl);
9361 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
9362 			netdev_info(bp->dev, "EEE is %s\n",
9363 				    bp->eee.eee_active ? "active" :
9364 							 "not active");
9365 		fec = bp->link_info.fec_cfg;
9366 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
9367 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
9368 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
9369 				    bnxt_report_fec(&bp->link_info));
9370 	} else {
9371 		netif_carrier_off(bp->dev);
9372 		netdev_err(bp->dev, "NIC Link is Down\n");
9373 	}
9374 }
9375 
9376 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
9377 {
9378 	if (!resp->supported_speeds_auto_mode &&
9379 	    !resp->supported_speeds_force_mode &&
9380 	    !resp->supported_pam4_speeds_auto_mode &&
9381 	    !resp->supported_pam4_speeds_force_mode)
9382 		return true;
9383 	return false;
9384 }
9385 
9386 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
9387 {
9388 	int rc = 0;
9389 	struct hwrm_port_phy_qcaps_input req = {0};
9390 	struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
9391 	struct bnxt_link_info *link_info = &bp->link_info;
9392 
9393 	if (bp->hwrm_spec_code < 0x10201)
9394 		return 0;
9395 
9396 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
9397 
9398 	mutex_lock(&bp->hwrm_cmd_lock);
9399 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9400 	if (rc)
9401 		goto hwrm_phy_qcaps_exit;
9402 
9403 	bp->phy_flags = resp->flags;
9404 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
9405 		struct ethtool_eee *eee = &bp->eee;
9406 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
9407 
9408 		eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9409 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
9410 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
9411 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
9412 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
9413 	}
9414 
9415 	if (bp->hwrm_spec_code >= 0x10a01) {
9416 		if (bnxt_phy_qcaps_no_speed(resp)) {
9417 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
9418 			netdev_warn(bp->dev, "Ethernet link disabled\n");
9419 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
9420 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
9421 			netdev_info(bp->dev, "Ethernet link enabled\n");
9422 			/* Phy re-enabled, reprobe the speeds */
9423 			link_info->support_auto_speeds = 0;
9424 			link_info->support_pam4_auto_speeds = 0;
9425 		}
9426 	}
9427 	if (resp->supported_speeds_auto_mode)
9428 		link_info->support_auto_speeds =
9429 			le16_to_cpu(resp->supported_speeds_auto_mode);
9430 	if (resp->supported_pam4_speeds_auto_mode)
9431 		link_info->support_pam4_auto_speeds =
9432 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
9433 
9434 	bp->port_count = resp->port_cnt;
9435 
9436 hwrm_phy_qcaps_exit:
9437 	mutex_unlock(&bp->hwrm_cmd_lock);
9438 	return rc;
9439 }
9440 
9441 static bool bnxt_support_dropped(u16 advertising, u16 supported)
9442 {
9443 	u16 diff = advertising ^ supported;
9444 
9445 	return ((supported | diff) != supported);
9446 }
9447 
9448 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
9449 {
9450 	int rc = 0;
9451 	struct bnxt_link_info *link_info = &bp->link_info;
9452 	struct hwrm_port_phy_qcfg_input req = {0};
9453 	struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
9454 	u8 link_up = link_info->link_up;
9455 	bool support_changed = false;
9456 
9457 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
9458 
9459 	mutex_lock(&bp->hwrm_cmd_lock);
9460 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9461 	if (rc) {
9462 		mutex_unlock(&bp->hwrm_cmd_lock);
9463 		return rc;
9464 	}
9465 
9466 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
9467 	link_info->phy_link_status = resp->link;
9468 	link_info->duplex = resp->duplex_cfg;
9469 	if (bp->hwrm_spec_code >= 0x10800)
9470 		link_info->duplex = resp->duplex_state;
9471 	link_info->pause = resp->pause;
9472 	link_info->auto_mode = resp->auto_mode;
9473 	link_info->auto_pause_setting = resp->auto_pause;
9474 	link_info->lp_pause = resp->link_partner_adv_pause;
9475 	link_info->force_pause_setting = resp->force_pause;
9476 	link_info->duplex_setting = resp->duplex_cfg;
9477 	if (link_info->phy_link_status == BNXT_LINK_LINK)
9478 		link_info->link_speed = le16_to_cpu(resp->link_speed);
9479 	else
9480 		link_info->link_speed = 0;
9481 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
9482 	link_info->force_pam4_link_speed =
9483 		le16_to_cpu(resp->force_pam4_link_speed);
9484 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
9485 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
9486 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
9487 	link_info->auto_pam4_link_speeds =
9488 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
9489 	link_info->lp_auto_link_speeds =
9490 		le16_to_cpu(resp->link_partner_adv_speeds);
9491 	link_info->lp_auto_pam4_link_speeds =
9492 		resp->link_partner_pam4_adv_speeds;
9493 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
9494 	link_info->phy_ver[0] = resp->phy_maj;
9495 	link_info->phy_ver[1] = resp->phy_min;
9496 	link_info->phy_ver[2] = resp->phy_bld;
9497 	link_info->media_type = resp->media_type;
9498 	link_info->phy_type = resp->phy_type;
9499 	link_info->transceiver = resp->xcvr_pkg_type;
9500 	link_info->phy_addr = resp->eee_config_phy_addr &
9501 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
9502 	link_info->module_status = resp->module_status;
9503 
9504 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
9505 		struct ethtool_eee *eee = &bp->eee;
9506 		u16 fw_speeds;
9507 
9508 		eee->eee_active = 0;
9509 		if (resp->eee_config_phy_addr &
9510 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
9511 			eee->eee_active = 1;
9512 			fw_speeds = le16_to_cpu(
9513 				resp->link_partner_adv_eee_link_speed_mask);
9514 			eee->lp_advertised =
9515 				_bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9516 		}
9517 
9518 		/* Pull initial EEE config */
9519 		if (!chng_link_state) {
9520 			if (resp->eee_config_phy_addr &
9521 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
9522 				eee->eee_enabled = 1;
9523 
9524 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
9525 			eee->advertised =
9526 				_bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9527 
9528 			if (resp->eee_config_phy_addr &
9529 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
9530 				__le32 tmr;
9531 
9532 				eee->tx_lpi_enabled = 1;
9533 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
9534 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
9535 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
9536 			}
9537 		}
9538 	}
9539 
9540 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
9541 	if (bp->hwrm_spec_code >= 0x10504) {
9542 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
9543 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
9544 	}
9545 	/* TODO: need to add more logic to report VF link */
9546 	if (chng_link_state) {
9547 		if (link_info->phy_link_status == BNXT_LINK_LINK)
9548 			link_info->link_up = 1;
9549 		else
9550 			link_info->link_up = 0;
9551 		if (link_up != link_info->link_up)
9552 			bnxt_report_link(bp);
9553 	} else {
9554 		/* alwasy link down if not require to update link state */
9555 		link_info->link_up = 0;
9556 	}
9557 	mutex_unlock(&bp->hwrm_cmd_lock);
9558 
9559 	if (!BNXT_PHY_CFG_ABLE(bp))
9560 		return 0;
9561 
9562 	/* Check if any advertised speeds are no longer supported. The caller
9563 	 * holds the link_lock mutex, so we can modify link_info settings.
9564 	 */
9565 	if (bnxt_support_dropped(link_info->advertising,
9566 				 link_info->support_auto_speeds)) {
9567 		link_info->advertising = link_info->support_auto_speeds;
9568 		support_changed = true;
9569 	}
9570 	if (bnxt_support_dropped(link_info->advertising_pam4,
9571 				 link_info->support_pam4_auto_speeds)) {
9572 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
9573 		support_changed = true;
9574 	}
9575 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
9576 		bnxt_hwrm_set_link_setting(bp, true, false);
9577 	return 0;
9578 }
9579 
9580 static void bnxt_get_port_module_status(struct bnxt *bp)
9581 {
9582 	struct bnxt_link_info *link_info = &bp->link_info;
9583 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
9584 	u8 module_status;
9585 
9586 	if (bnxt_update_link(bp, true))
9587 		return;
9588 
9589 	module_status = link_info->module_status;
9590 	switch (module_status) {
9591 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
9592 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
9593 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
9594 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
9595 			    bp->pf.port_id);
9596 		if (bp->hwrm_spec_code >= 0x10201) {
9597 			netdev_warn(bp->dev, "Module part number %s\n",
9598 				    resp->phy_vendor_partnumber);
9599 		}
9600 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
9601 			netdev_warn(bp->dev, "TX is disabled\n");
9602 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
9603 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
9604 	}
9605 }
9606 
9607 static void
9608 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
9609 {
9610 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
9611 		if (bp->hwrm_spec_code >= 0x10201)
9612 			req->auto_pause =
9613 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
9614 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
9615 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
9616 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
9617 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
9618 		req->enables |=
9619 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
9620 	} else {
9621 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
9622 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
9623 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
9624 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
9625 		req->enables |=
9626 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
9627 		if (bp->hwrm_spec_code >= 0x10201) {
9628 			req->auto_pause = req->force_pause;
9629 			req->enables |= cpu_to_le32(
9630 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
9631 		}
9632 	}
9633 }
9634 
9635 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
9636 {
9637 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
9638 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
9639 		if (bp->link_info.advertising) {
9640 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
9641 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
9642 		}
9643 		if (bp->link_info.advertising_pam4) {
9644 			req->enables |=
9645 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
9646 			req->auto_link_pam4_speed_mask =
9647 				cpu_to_le16(bp->link_info.advertising_pam4);
9648 		}
9649 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
9650 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
9651 	} else {
9652 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
9653 		if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
9654 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
9655 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
9656 		} else {
9657 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
9658 		}
9659 	}
9660 
9661 	/* tell chimp that the setting takes effect immediately */
9662 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
9663 }
9664 
9665 int bnxt_hwrm_set_pause(struct bnxt *bp)
9666 {
9667 	struct hwrm_port_phy_cfg_input req = {0};
9668 	int rc;
9669 
9670 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
9671 	bnxt_hwrm_set_pause_common(bp, &req);
9672 
9673 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
9674 	    bp->link_info.force_link_chng)
9675 		bnxt_hwrm_set_link_common(bp, &req);
9676 
9677 	mutex_lock(&bp->hwrm_cmd_lock);
9678 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9679 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
9680 		/* since changing of pause setting doesn't trigger any link
9681 		 * change event, the driver needs to update the current pause
9682 		 * result upon successfully return of the phy_cfg command
9683 		 */
9684 		bp->link_info.pause =
9685 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
9686 		bp->link_info.auto_pause_setting = 0;
9687 		if (!bp->link_info.force_link_chng)
9688 			bnxt_report_link(bp);
9689 	}
9690 	bp->link_info.force_link_chng = false;
9691 	mutex_unlock(&bp->hwrm_cmd_lock);
9692 	return rc;
9693 }
9694 
9695 static void bnxt_hwrm_set_eee(struct bnxt *bp,
9696 			      struct hwrm_port_phy_cfg_input *req)
9697 {
9698 	struct ethtool_eee *eee = &bp->eee;
9699 
9700 	if (eee->eee_enabled) {
9701 		u16 eee_speeds;
9702 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
9703 
9704 		if (eee->tx_lpi_enabled)
9705 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
9706 		else
9707 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
9708 
9709 		req->flags |= cpu_to_le32(flags);
9710 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
9711 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
9712 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
9713 	} else {
9714 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
9715 	}
9716 }
9717 
9718 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
9719 {
9720 	struct hwrm_port_phy_cfg_input req = {0};
9721 
9722 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
9723 	if (set_pause)
9724 		bnxt_hwrm_set_pause_common(bp, &req);
9725 
9726 	bnxt_hwrm_set_link_common(bp, &req);
9727 
9728 	if (set_eee)
9729 		bnxt_hwrm_set_eee(bp, &req);
9730 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9731 }
9732 
9733 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
9734 {
9735 	struct hwrm_port_phy_cfg_input req = {0};
9736 
9737 	if (!BNXT_SINGLE_PF(bp))
9738 		return 0;
9739 
9740 	if (pci_num_vf(bp->pdev) &&
9741 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
9742 		return 0;
9743 
9744 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
9745 	req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
9746 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9747 }
9748 
9749 static int bnxt_fw_init_one(struct bnxt *bp);
9750 
9751 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
9752 {
9753 #ifdef CONFIG_TEE_BNXT_FW
9754 	int rc = tee_bnxt_fw_load();
9755 
9756 	if (rc)
9757 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
9758 
9759 	return rc;
9760 #else
9761 	netdev_err(bp->dev, "OP-TEE not supported\n");
9762 	return -ENODEV;
9763 #endif
9764 }
9765 
9766 static int bnxt_try_recover_fw(struct bnxt *bp)
9767 {
9768 	if (bp->fw_health && bp->fw_health->status_reliable) {
9769 		int retry = 0, rc;
9770 		u32 sts;
9771 
9772 		mutex_lock(&bp->hwrm_cmd_lock);
9773 		do {
9774 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
9775 			rc = __bnxt_hwrm_ver_get(bp, true);
9776 			if (!BNXT_FW_IS_BOOTING(sts) &&
9777 			    !BNXT_FW_IS_RECOVERING(sts))
9778 				break;
9779 			retry++;
9780 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
9781 		mutex_unlock(&bp->hwrm_cmd_lock);
9782 
9783 		if (!BNXT_FW_IS_HEALTHY(sts)) {
9784 			netdev_err(bp->dev,
9785 				   "Firmware not responding, status: 0x%x\n",
9786 				   sts);
9787 			rc = -ENODEV;
9788 		}
9789 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
9790 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
9791 			return bnxt_fw_reset_via_optee(bp);
9792 		}
9793 		return rc;
9794 	}
9795 
9796 	return -ENODEV;
9797 }
9798 
9799 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
9800 {
9801 	struct hwrm_func_drv_if_change_output *resp = bp->hwrm_cmd_resp_addr;
9802 	struct hwrm_func_drv_if_change_input req = {0};
9803 	bool fw_reset = !bp->irq_tbl;
9804 	bool resc_reinit = false;
9805 	int rc, retry = 0;
9806 	u32 flags = 0;
9807 
9808 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
9809 		return 0;
9810 
9811 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_IF_CHANGE, -1, -1);
9812 	if (up)
9813 		req.flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
9814 	mutex_lock(&bp->hwrm_cmd_lock);
9815 	while (retry < BNXT_FW_IF_RETRY) {
9816 		rc = _hwrm_send_message(bp, &req, sizeof(req),
9817 					HWRM_CMD_TIMEOUT);
9818 		if (rc != -EAGAIN)
9819 			break;
9820 
9821 		msleep(50);
9822 		retry++;
9823 	}
9824 	if (!rc)
9825 		flags = le32_to_cpu(resp->flags);
9826 	mutex_unlock(&bp->hwrm_cmd_lock);
9827 
9828 	if (rc == -EAGAIN)
9829 		return rc;
9830 	if (rc && up) {
9831 		rc = bnxt_try_recover_fw(bp);
9832 		fw_reset = true;
9833 	}
9834 	if (rc)
9835 		return rc;
9836 
9837 	if (!up) {
9838 		bnxt_inv_fw_health_reg(bp);
9839 		return 0;
9840 	}
9841 
9842 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
9843 		resc_reinit = true;
9844 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE)
9845 		fw_reset = true;
9846 	else if (bp->fw_health && !bp->fw_health->status_reliable)
9847 		bnxt_try_map_fw_health_reg(bp);
9848 
9849 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
9850 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
9851 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
9852 		return -ENODEV;
9853 	}
9854 	if (resc_reinit || fw_reset) {
9855 		if (fw_reset) {
9856 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
9857 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
9858 				bnxt_ulp_stop(bp);
9859 			bnxt_free_ctx_mem(bp);
9860 			kfree(bp->ctx);
9861 			bp->ctx = NULL;
9862 			bnxt_dcb_free(bp);
9863 			rc = bnxt_fw_init_one(bp);
9864 			if (rc) {
9865 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
9866 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
9867 				return rc;
9868 			}
9869 			bnxt_clear_int_mode(bp);
9870 			rc = bnxt_init_int_mode(bp);
9871 			if (rc) {
9872 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
9873 				netdev_err(bp->dev, "init int mode failed\n");
9874 				return rc;
9875 			}
9876 		}
9877 		if (BNXT_NEW_RM(bp)) {
9878 			struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9879 
9880 			rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9881 			if (rc)
9882 				netdev_err(bp->dev, "resc_qcaps failed\n");
9883 
9884 			hw_resc->resv_cp_rings = 0;
9885 			hw_resc->resv_stat_ctxs = 0;
9886 			hw_resc->resv_irqs = 0;
9887 			hw_resc->resv_tx_rings = 0;
9888 			hw_resc->resv_rx_rings = 0;
9889 			hw_resc->resv_hw_ring_grps = 0;
9890 			hw_resc->resv_vnics = 0;
9891 			if (!fw_reset) {
9892 				bp->tx_nr_rings = 0;
9893 				bp->rx_nr_rings = 0;
9894 			}
9895 		}
9896 	}
9897 	return rc;
9898 }
9899 
9900 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
9901 {
9902 	struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
9903 	struct hwrm_port_led_qcaps_input req = {0};
9904 	struct bnxt_pf_info *pf = &bp->pf;
9905 	int rc;
9906 
9907 	bp->num_leds = 0;
9908 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
9909 		return 0;
9910 
9911 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
9912 	req.port_id = cpu_to_le16(pf->port_id);
9913 	mutex_lock(&bp->hwrm_cmd_lock);
9914 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9915 	if (rc) {
9916 		mutex_unlock(&bp->hwrm_cmd_lock);
9917 		return rc;
9918 	}
9919 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
9920 		int i;
9921 
9922 		bp->num_leds = resp->num_leds;
9923 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
9924 						 bp->num_leds);
9925 		for (i = 0; i < bp->num_leds; i++) {
9926 			struct bnxt_led_info *led = &bp->leds[i];
9927 			__le16 caps = led->led_state_caps;
9928 
9929 			if (!led->led_group_id ||
9930 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
9931 				bp->num_leds = 0;
9932 				break;
9933 			}
9934 		}
9935 	}
9936 	mutex_unlock(&bp->hwrm_cmd_lock);
9937 	return 0;
9938 }
9939 
9940 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
9941 {
9942 	struct hwrm_wol_filter_alloc_input req = {0};
9943 	struct hwrm_wol_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
9944 	int rc;
9945 
9946 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_ALLOC, -1, -1);
9947 	req.port_id = cpu_to_le16(bp->pf.port_id);
9948 	req.wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
9949 	req.enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
9950 	memcpy(req.mac_address, bp->dev->dev_addr, ETH_ALEN);
9951 	mutex_lock(&bp->hwrm_cmd_lock);
9952 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9953 	if (!rc)
9954 		bp->wol_filter_id = resp->wol_filter_id;
9955 	mutex_unlock(&bp->hwrm_cmd_lock);
9956 	return rc;
9957 }
9958 
9959 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
9960 {
9961 	struct hwrm_wol_filter_free_input req = {0};
9962 
9963 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_FREE, -1, -1);
9964 	req.port_id = cpu_to_le16(bp->pf.port_id);
9965 	req.enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
9966 	req.wol_filter_id = bp->wol_filter_id;
9967 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9968 }
9969 
9970 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
9971 {
9972 	struct hwrm_wol_filter_qcfg_input req = {0};
9973 	struct hwrm_wol_filter_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
9974 	u16 next_handle = 0;
9975 	int rc;
9976 
9977 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_QCFG, -1, -1);
9978 	req.port_id = cpu_to_le16(bp->pf.port_id);
9979 	req.handle = cpu_to_le16(handle);
9980 	mutex_lock(&bp->hwrm_cmd_lock);
9981 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9982 	if (!rc) {
9983 		next_handle = le16_to_cpu(resp->next_handle);
9984 		if (next_handle != 0) {
9985 			if (resp->wol_type ==
9986 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
9987 				bp->wol = 1;
9988 				bp->wol_filter_id = resp->wol_filter_id;
9989 			}
9990 		}
9991 	}
9992 	mutex_unlock(&bp->hwrm_cmd_lock);
9993 	return next_handle;
9994 }
9995 
9996 static void bnxt_get_wol_settings(struct bnxt *bp)
9997 {
9998 	u16 handle = 0;
9999 
10000 	bp->wol = 0;
10001 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
10002 		return;
10003 
10004 	do {
10005 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
10006 	} while (handle && handle != 0xffff);
10007 }
10008 
10009 #ifdef CONFIG_BNXT_HWMON
10010 static ssize_t bnxt_show_temp(struct device *dev,
10011 			      struct device_attribute *devattr, char *buf)
10012 {
10013 	struct hwrm_temp_monitor_query_input req = {0};
10014 	struct hwrm_temp_monitor_query_output *resp;
10015 	struct bnxt *bp = dev_get_drvdata(dev);
10016 	u32 len = 0;
10017 	int rc;
10018 
10019 	resp = bp->hwrm_cmd_resp_addr;
10020 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TEMP_MONITOR_QUERY, -1, -1);
10021 	mutex_lock(&bp->hwrm_cmd_lock);
10022 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
10023 	if (!rc)
10024 		len = sprintf(buf, "%u\n", resp->temp * 1000); /* display millidegree */
10025 	mutex_unlock(&bp->hwrm_cmd_lock);
10026 	if (rc)
10027 		return rc;
10028 	return len;
10029 }
10030 static SENSOR_DEVICE_ATTR(temp1_input, 0444, bnxt_show_temp, NULL, 0);
10031 
10032 static struct attribute *bnxt_attrs[] = {
10033 	&sensor_dev_attr_temp1_input.dev_attr.attr,
10034 	NULL
10035 };
10036 ATTRIBUTE_GROUPS(bnxt);
10037 
10038 static void bnxt_hwmon_close(struct bnxt *bp)
10039 {
10040 	if (bp->hwmon_dev) {
10041 		hwmon_device_unregister(bp->hwmon_dev);
10042 		bp->hwmon_dev = NULL;
10043 	}
10044 }
10045 
10046 static void bnxt_hwmon_open(struct bnxt *bp)
10047 {
10048 	struct hwrm_temp_monitor_query_input req = {0};
10049 	struct pci_dev *pdev = bp->pdev;
10050 	int rc;
10051 
10052 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TEMP_MONITOR_QUERY, -1, -1);
10053 	rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
10054 	if (rc == -EACCES || rc == -EOPNOTSUPP) {
10055 		bnxt_hwmon_close(bp);
10056 		return;
10057 	}
10058 
10059 	if (bp->hwmon_dev)
10060 		return;
10061 
10062 	bp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev,
10063 							  DRV_MODULE_NAME, bp,
10064 							  bnxt_groups);
10065 	if (IS_ERR(bp->hwmon_dev)) {
10066 		bp->hwmon_dev = NULL;
10067 		dev_warn(&pdev->dev, "Cannot register hwmon device\n");
10068 	}
10069 }
10070 #else
10071 static void bnxt_hwmon_close(struct bnxt *bp)
10072 {
10073 }
10074 
10075 static void bnxt_hwmon_open(struct bnxt *bp)
10076 {
10077 }
10078 #endif
10079 
10080 static bool bnxt_eee_config_ok(struct bnxt *bp)
10081 {
10082 	struct ethtool_eee *eee = &bp->eee;
10083 	struct bnxt_link_info *link_info = &bp->link_info;
10084 
10085 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
10086 		return true;
10087 
10088 	if (eee->eee_enabled) {
10089 		u32 advertising =
10090 			_bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
10091 
10092 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
10093 			eee->eee_enabled = 0;
10094 			return false;
10095 		}
10096 		if (eee->advertised & ~advertising) {
10097 			eee->advertised = advertising & eee->supported;
10098 			return false;
10099 		}
10100 	}
10101 	return true;
10102 }
10103 
10104 static int bnxt_update_phy_setting(struct bnxt *bp)
10105 {
10106 	int rc;
10107 	bool update_link = false;
10108 	bool update_pause = false;
10109 	bool update_eee = false;
10110 	struct bnxt_link_info *link_info = &bp->link_info;
10111 
10112 	rc = bnxt_update_link(bp, true);
10113 	if (rc) {
10114 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
10115 			   rc);
10116 		return rc;
10117 	}
10118 	if (!BNXT_SINGLE_PF(bp))
10119 		return 0;
10120 
10121 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
10122 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
10123 	    link_info->req_flow_ctrl)
10124 		update_pause = true;
10125 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
10126 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
10127 		update_pause = true;
10128 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
10129 		if (BNXT_AUTO_MODE(link_info->auto_mode))
10130 			update_link = true;
10131 		if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
10132 		    link_info->req_link_speed != link_info->force_link_speed)
10133 			update_link = true;
10134 		else if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
10135 			 link_info->req_link_speed != link_info->force_pam4_link_speed)
10136 			update_link = true;
10137 		if (link_info->req_duplex != link_info->duplex_setting)
10138 			update_link = true;
10139 	} else {
10140 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
10141 			update_link = true;
10142 		if (link_info->advertising != link_info->auto_link_speeds ||
10143 		    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
10144 			update_link = true;
10145 	}
10146 
10147 	/* The last close may have shutdown the link, so need to call
10148 	 * PHY_CFG to bring it back up.
10149 	 */
10150 	if (!bp->link_info.link_up)
10151 		update_link = true;
10152 
10153 	if (!bnxt_eee_config_ok(bp))
10154 		update_eee = true;
10155 
10156 	if (update_link)
10157 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
10158 	else if (update_pause)
10159 		rc = bnxt_hwrm_set_pause(bp);
10160 	if (rc) {
10161 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
10162 			   rc);
10163 		return rc;
10164 	}
10165 
10166 	return rc;
10167 }
10168 
10169 /* Common routine to pre-map certain register block to different GRC window.
10170  * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
10171  * in PF and 3 windows in VF that can be customized to map in different
10172  * register blocks.
10173  */
10174 static void bnxt_preset_reg_win(struct bnxt *bp)
10175 {
10176 	if (BNXT_PF(bp)) {
10177 		/* CAG registers map to GRC window #4 */
10178 		writel(BNXT_CAG_REG_BASE,
10179 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
10180 	}
10181 }
10182 
10183 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
10184 
10185 static int bnxt_reinit_after_abort(struct bnxt *bp)
10186 {
10187 	int rc;
10188 
10189 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
10190 		return -EBUSY;
10191 
10192 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
10193 		return -ENODEV;
10194 
10195 	rc = bnxt_fw_init_one(bp);
10196 	if (!rc) {
10197 		bnxt_clear_int_mode(bp);
10198 		rc = bnxt_init_int_mode(bp);
10199 		if (!rc) {
10200 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
10201 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
10202 		}
10203 	}
10204 	return rc;
10205 }
10206 
10207 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
10208 {
10209 	int rc = 0;
10210 
10211 	bnxt_preset_reg_win(bp);
10212 	netif_carrier_off(bp->dev);
10213 	if (irq_re_init) {
10214 		/* Reserve rings now if none were reserved at driver probe. */
10215 		rc = bnxt_init_dflt_ring_mode(bp);
10216 		if (rc) {
10217 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
10218 			return rc;
10219 		}
10220 	}
10221 	rc = bnxt_reserve_rings(bp, irq_re_init);
10222 	if (rc)
10223 		return rc;
10224 	if ((bp->flags & BNXT_FLAG_RFS) &&
10225 	    !(bp->flags & BNXT_FLAG_USING_MSIX)) {
10226 		/* disable RFS if falling back to INTA */
10227 		bp->dev->hw_features &= ~NETIF_F_NTUPLE;
10228 		bp->flags &= ~BNXT_FLAG_RFS;
10229 	}
10230 
10231 	rc = bnxt_alloc_mem(bp, irq_re_init);
10232 	if (rc) {
10233 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
10234 		goto open_err_free_mem;
10235 	}
10236 
10237 	if (irq_re_init) {
10238 		bnxt_init_napi(bp);
10239 		rc = bnxt_request_irq(bp);
10240 		if (rc) {
10241 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
10242 			goto open_err_irq;
10243 		}
10244 	}
10245 
10246 	rc = bnxt_init_nic(bp, irq_re_init);
10247 	if (rc) {
10248 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
10249 		goto open_err_irq;
10250 	}
10251 
10252 	bnxt_enable_napi(bp);
10253 	bnxt_debug_dev_init(bp);
10254 
10255 	if (link_re_init) {
10256 		mutex_lock(&bp->link_lock);
10257 		rc = bnxt_update_phy_setting(bp);
10258 		mutex_unlock(&bp->link_lock);
10259 		if (rc) {
10260 			netdev_warn(bp->dev, "failed to update phy settings\n");
10261 			if (BNXT_SINGLE_PF(bp)) {
10262 				bp->link_info.phy_retry = true;
10263 				bp->link_info.phy_retry_expires =
10264 					jiffies + 5 * HZ;
10265 			}
10266 		}
10267 	}
10268 
10269 	if (irq_re_init)
10270 		udp_tunnel_nic_reset_ntf(bp->dev);
10271 
10272 	set_bit(BNXT_STATE_OPEN, &bp->state);
10273 	bnxt_enable_int(bp);
10274 	/* Enable TX queues */
10275 	bnxt_tx_enable(bp);
10276 	mod_timer(&bp->timer, jiffies + bp->current_interval);
10277 	/* Poll link status and check for SFP+ module status */
10278 	bnxt_get_port_module_status(bp);
10279 
10280 	/* VF-reps may need to be re-opened after the PF is re-opened */
10281 	if (BNXT_PF(bp))
10282 		bnxt_vf_reps_open(bp);
10283 	return 0;
10284 
10285 open_err_irq:
10286 	bnxt_del_napi(bp);
10287 
10288 open_err_free_mem:
10289 	bnxt_free_skbs(bp);
10290 	bnxt_free_irq(bp);
10291 	bnxt_free_mem(bp, true);
10292 	return rc;
10293 }
10294 
10295 /* rtnl_lock held */
10296 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
10297 {
10298 	int rc = 0;
10299 
10300 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
10301 		rc = -EIO;
10302 	if (!rc)
10303 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
10304 	if (rc) {
10305 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
10306 		dev_close(bp->dev);
10307 	}
10308 	return rc;
10309 }
10310 
10311 /* rtnl_lock held, open the NIC half way by allocating all resources, but
10312  * NAPI, IRQ, and TX are not enabled.  This is mainly used for offline
10313  * self tests.
10314  */
10315 int bnxt_half_open_nic(struct bnxt *bp)
10316 {
10317 	int rc = 0;
10318 
10319 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
10320 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
10321 		rc = -ENODEV;
10322 		goto half_open_err;
10323 	}
10324 
10325 	rc = bnxt_alloc_mem(bp, false);
10326 	if (rc) {
10327 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
10328 		goto half_open_err;
10329 	}
10330 	rc = bnxt_init_nic(bp, false);
10331 	if (rc) {
10332 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
10333 		goto half_open_err;
10334 	}
10335 	return 0;
10336 
10337 half_open_err:
10338 	bnxt_free_skbs(bp);
10339 	bnxt_free_mem(bp, false);
10340 	dev_close(bp->dev);
10341 	return rc;
10342 }
10343 
10344 /* rtnl_lock held, this call can only be made after a previous successful
10345  * call to bnxt_half_open_nic().
10346  */
10347 void bnxt_half_close_nic(struct bnxt *bp)
10348 {
10349 	bnxt_hwrm_resource_free(bp, false, false);
10350 	bnxt_free_skbs(bp);
10351 	bnxt_free_mem(bp, false);
10352 }
10353 
10354 static void bnxt_reenable_sriov(struct bnxt *bp)
10355 {
10356 	if (BNXT_PF(bp)) {
10357 		struct bnxt_pf_info *pf = &bp->pf;
10358 		int n = pf->active_vfs;
10359 
10360 		if (n)
10361 			bnxt_cfg_hw_sriov(bp, &n, true);
10362 	}
10363 }
10364 
10365 static int bnxt_open(struct net_device *dev)
10366 {
10367 	struct bnxt *bp = netdev_priv(dev);
10368 	int rc;
10369 
10370 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
10371 		rc = bnxt_reinit_after_abort(bp);
10372 		if (rc) {
10373 			if (rc == -EBUSY)
10374 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
10375 			else
10376 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
10377 			return -ENODEV;
10378 		}
10379 	}
10380 
10381 	rc = bnxt_hwrm_if_change(bp, true);
10382 	if (rc)
10383 		return rc;
10384 
10385 	rc = __bnxt_open_nic(bp, true, true);
10386 	if (rc) {
10387 		bnxt_hwrm_if_change(bp, false);
10388 	} else {
10389 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
10390 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
10391 				bnxt_ulp_start(bp, 0);
10392 				bnxt_reenable_sriov(bp);
10393 			}
10394 		}
10395 		bnxt_hwmon_open(bp);
10396 	}
10397 
10398 	return rc;
10399 }
10400 
10401 static bool bnxt_drv_busy(struct bnxt *bp)
10402 {
10403 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
10404 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
10405 }
10406 
10407 static void bnxt_get_ring_stats(struct bnxt *bp,
10408 				struct rtnl_link_stats64 *stats);
10409 
10410 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
10411 			     bool link_re_init)
10412 {
10413 	/* Close the VF-reps before closing PF */
10414 	if (BNXT_PF(bp))
10415 		bnxt_vf_reps_close(bp);
10416 
10417 	/* Change device state to avoid TX queue wake up's */
10418 	bnxt_tx_disable(bp);
10419 
10420 	clear_bit(BNXT_STATE_OPEN, &bp->state);
10421 	smp_mb__after_atomic();
10422 	while (bnxt_drv_busy(bp))
10423 		msleep(20);
10424 
10425 	/* Flush rings and and disable interrupts */
10426 	bnxt_shutdown_nic(bp, irq_re_init);
10427 
10428 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
10429 
10430 	bnxt_debug_dev_exit(bp);
10431 	bnxt_disable_napi(bp);
10432 	del_timer_sync(&bp->timer);
10433 	bnxt_free_skbs(bp);
10434 
10435 	/* Save ring stats before shutdown */
10436 	if (bp->bnapi && irq_re_init)
10437 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
10438 	if (irq_re_init) {
10439 		bnxt_free_irq(bp);
10440 		bnxt_del_napi(bp);
10441 	}
10442 	bnxt_free_mem(bp, irq_re_init);
10443 }
10444 
10445 int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
10446 {
10447 	int rc = 0;
10448 
10449 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
10450 		/* If we get here, it means firmware reset is in progress
10451 		 * while we are trying to close.  We can safely proceed with
10452 		 * the close because we are holding rtnl_lock().  Some firmware
10453 		 * messages may fail as we proceed to close.  We set the
10454 		 * ABORT_ERR flag here so that the FW reset thread will later
10455 		 * abort when it gets the rtnl_lock() and sees the flag.
10456 		 */
10457 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
10458 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
10459 	}
10460 
10461 #ifdef CONFIG_BNXT_SRIOV
10462 	if (bp->sriov_cfg) {
10463 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
10464 						      !bp->sriov_cfg,
10465 						      BNXT_SRIOV_CFG_WAIT_TMO);
10466 		if (rc)
10467 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
10468 	}
10469 #endif
10470 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
10471 	return rc;
10472 }
10473 
10474 static int bnxt_close(struct net_device *dev)
10475 {
10476 	struct bnxt *bp = netdev_priv(dev);
10477 
10478 	bnxt_hwmon_close(bp);
10479 	bnxt_close_nic(bp, true, true);
10480 	bnxt_hwrm_shutdown_link(bp);
10481 	bnxt_hwrm_if_change(bp, false);
10482 	return 0;
10483 }
10484 
10485 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
10486 				   u16 *val)
10487 {
10488 	struct hwrm_port_phy_mdio_read_output *resp = bp->hwrm_cmd_resp_addr;
10489 	struct hwrm_port_phy_mdio_read_input req = {0};
10490 	int rc;
10491 
10492 	if (bp->hwrm_spec_code < 0x10a00)
10493 		return -EOPNOTSUPP;
10494 
10495 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_MDIO_READ, -1, -1);
10496 	req.port_id = cpu_to_le16(bp->pf.port_id);
10497 	req.phy_addr = phy_addr;
10498 	req.reg_addr = cpu_to_le16(reg & 0x1f);
10499 	if (mdio_phy_id_is_c45(phy_addr)) {
10500 		req.cl45_mdio = 1;
10501 		req.phy_addr = mdio_phy_id_prtad(phy_addr);
10502 		req.dev_addr = mdio_phy_id_devad(phy_addr);
10503 		req.reg_addr = cpu_to_le16(reg);
10504 	}
10505 
10506 	mutex_lock(&bp->hwrm_cmd_lock);
10507 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
10508 	if (!rc)
10509 		*val = le16_to_cpu(resp->reg_data);
10510 	mutex_unlock(&bp->hwrm_cmd_lock);
10511 	return rc;
10512 }
10513 
10514 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
10515 				    u16 val)
10516 {
10517 	struct hwrm_port_phy_mdio_write_input req = {0};
10518 
10519 	if (bp->hwrm_spec_code < 0x10a00)
10520 		return -EOPNOTSUPP;
10521 
10522 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_MDIO_WRITE, -1, -1);
10523 	req.port_id = cpu_to_le16(bp->pf.port_id);
10524 	req.phy_addr = phy_addr;
10525 	req.reg_addr = cpu_to_le16(reg & 0x1f);
10526 	if (mdio_phy_id_is_c45(phy_addr)) {
10527 		req.cl45_mdio = 1;
10528 		req.phy_addr = mdio_phy_id_prtad(phy_addr);
10529 		req.dev_addr = mdio_phy_id_devad(phy_addr);
10530 		req.reg_addr = cpu_to_le16(reg);
10531 	}
10532 	req.reg_data = cpu_to_le16(val);
10533 
10534 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
10535 }
10536 
10537 /* rtnl_lock held */
10538 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
10539 {
10540 	struct mii_ioctl_data *mdio = if_mii(ifr);
10541 	struct bnxt *bp = netdev_priv(dev);
10542 	int rc;
10543 
10544 	switch (cmd) {
10545 	case SIOCGMIIPHY:
10546 		mdio->phy_id = bp->link_info.phy_addr;
10547 
10548 		fallthrough;
10549 	case SIOCGMIIREG: {
10550 		u16 mii_regval = 0;
10551 
10552 		if (!netif_running(dev))
10553 			return -EAGAIN;
10554 
10555 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
10556 					     &mii_regval);
10557 		mdio->val_out = mii_regval;
10558 		return rc;
10559 	}
10560 
10561 	case SIOCSMIIREG:
10562 		if (!netif_running(dev))
10563 			return -EAGAIN;
10564 
10565 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
10566 						mdio->val_in);
10567 
10568 	case SIOCSHWTSTAMP:
10569 		return bnxt_hwtstamp_set(dev, ifr);
10570 
10571 	case SIOCGHWTSTAMP:
10572 		return bnxt_hwtstamp_get(dev, ifr);
10573 
10574 	default:
10575 		/* do nothing */
10576 		break;
10577 	}
10578 	return -EOPNOTSUPP;
10579 }
10580 
10581 static void bnxt_get_ring_stats(struct bnxt *bp,
10582 				struct rtnl_link_stats64 *stats)
10583 {
10584 	int i;
10585 
10586 	for (i = 0; i < bp->cp_nr_rings; i++) {
10587 		struct bnxt_napi *bnapi = bp->bnapi[i];
10588 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
10589 		u64 *sw = cpr->stats.sw_stats;
10590 
10591 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
10592 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
10593 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
10594 
10595 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
10596 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
10597 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
10598 
10599 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
10600 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
10601 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
10602 
10603 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
10604 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
10605 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
10606 
10607 		stats->rx_missed_errors +=
10608 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
10609 
10610 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
10611 
10612 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
10613 	}
10614 }
10615 
10616 static void bnxt_add_prev_stats(struct bnxt *bp,
10617 				struct rtnl_link_stats64 *stats)
10618 {
10619 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
10620 
10621 	stats->rx_packets += prev_stats->rx_packets;
10622 	stats->tx_packets += prev_stats->tx_packets;
10623 	stats->rx_bytes += prev_stats->rx_bytes;
10624 	stats->tx_bytes += prev_stats->tx_bytes;
10625 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
10626 	stats->multicast += prev_stats->multicast;
10627 	stats->tx_dropped += prev_stats->tx_dropped;
10628 }
10629 
10630 static void
10631 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
10632 {
10633 	struct bnxt *bp = netdev_priv(dev);
10634 
10635 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
10636 	/* Make sure bnxt_close_nic() sees that we are reading stats before
10637 	 * we check the BNXT_STATE_OPEN flag.
10638 	 */
10639 	smp_mb__after_atomic();
10640 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
10641 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
10642 		*stats = bp->net_stats_prev;
10643 		return;
10644 	}
10645 
10646 	bnxt_get_ring_stats(bp, stats);
10647 	bnxt_add_prev_stats(bp, stats);
10648 
10649 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10650 		u64 *rx = bp->port_stats.sw_stats;
10651 		u64 *tx = bp->port_stats.sw_stats +
10652 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10653 
10654 		stats->rx_crc_errors =
10655 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
10656 		stats->rx_frame_errors =
10657 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
10658 		stats->rx_length_errors =
10659 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
10660 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
10661 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
10662 		stats->rx_errors =
10663 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
10664 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
10665 		stats->collisions =
10666 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
10667 		stats->tx_fifo_errors =
10668 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
10669 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
10670 	}
10671 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
10672 }
10673 
10674 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
10675 {
10676 	struct net_device *dev = bp->dev;
10677 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
10678 	struct netdev_hw_addr *ha;
10679 	u8 *haddr;
10680 	int mc_count = 0;
10681 	bool update = false;
10682 	int off = 0;
10683 
10684 	netdev_for_each_mc_addr(ha, dev) {
10685 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
10686 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10687 			vnic->mc_list_count = 0;
10688 			return false;
10689 		}
10690 		haddr = ha->addr;
10691 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
10692 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
10693 			update = true;
10694 		}
10695 		off += ETH_ALEN;
10696 		mc_count++;
10697 	}
10698 	if (mc_count)
10699 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
10700 
10701 	if (mc_count != vnic->mc_list_count) {
10702 		vnic->mc_list_count = mc_count;
10703 		update = true;
10704 	}
10705 	return update;
10706 }
10707 
10708 static bool bnxt_uc_list_updated(struct bnxt *bp)
10709 {
10710 	struct net_device *dev = bp->dev;
10711 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
10712 	struct netdev_hw_addr *ha;
10713 	int off = 0;
10714 
10715 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
10716 		return true;
10717 
10718 	netdev_for_each_uc_addr(ha, dev) {
10719 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
10720 			return true;
10721 
10722 		off += ETH_ALEN;
10723 	}
10724 	return false;
10725 }
10726 
10727 static void bnxt_set_rx_mode(struct net_device *dev)
10728 {
10729 	struct bnxt *bp = netdev_priv(dev);
10730 	struct bnxt_vnic_info *vnic;
10731 	bool mc_update = false;
10732 	bool uc_update;
10733 	u32 mask;
10734 
10735 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
10736 		return;
10737 
10738 	vnic = &bp->vnic_info[0];
10739 	mask = vnic->rx_mask;
10740 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
10741 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
10742 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
10743 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
10744 
10745 	if (dev->flags & IFF_PROMISC)
10746 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10747 
10748 	uc_update = bnxt_uc_list_updated(bp);
10749 
10750 	if (dev->flags & IFF_BROADCAST)
10751 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10752 	if (dev->flags & IFF_ALLMULTI) {
10753 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10754 		vnic->mc_list_count = 0;
10755 	} else {
10756 		mc_update = bnxt_mc_list_updated(bp, &mask);
10757 	}
10758 
10759 	if (mask != vnic->rx_mask || uc_update || mc_update) {
10760 		vnic->rx_mask = mask;
10761 
10762 		set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
10763 		bnxt_queue_sp_work(bp);
10764 	}
10765 }
10766 
10767 static int bnxt_cfg_rx_mode(struct bnxt *bp)
10768 {
10769 	struct net_device *dev = bp->dev;
10770 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
10771 	struct netdev_hw_addr *ha;
10772 	int i, off = 0, rc;
10773 	bool uc_update;
10774 
10775 	netif_addr_lock_bh(dev);
10776 	uc_update = bnxt_uc_list_updated(bp);
10777 	netif_addr_unlock_bh(dev);
10778 
10779 	if (!uc_update)
10780 		goto skip_uc;
10781 
10782 	mutex_lock(&bp->hwrm_cmd_lock);
10783 	for (i = 1; i < vnic->uc_filter_count; i++) {
10784 		struct hwrm_cfa_l2_filter_free_input req = {0};
10785 
10786 		bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
10787 				       -1);
10788 
10789 		req.l2_filter_id = vnic->fw_l2_filter_id[i];
10790 
10791 		rc = _hwrm_send_message(bp, &req, sizeof(req),
10792 					HWRM_CMD_TIMEOUT);
10793 	}
10794 	mutex_unlock(&bp->hwrm_cmd_lock);
10795 
10796 	vnic->uc_filter_count = 1;
10797 
10798 	netif_addr_lock_bh(dev);
10799 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
10800 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10801 	} else {
10802 		netdev_for_each_uc_addr(ha, dev) {
10803 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
10804 			off += ETH_ALEN;
10805 			vnic->uc_filter_count++;
10806 		}
10807 	}
10808 	netif_addr_unlock_bh(dev);
10809 
10810 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
10811 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
10812 		if (rc) {
10813 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
10814 				   rc);
10815 			vnic->uc_filter_count = i;
10816 			return rc;
10817 		}
10818 	}
10819 
10820 skip_uc:
10821 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
10822 	    !bnxt_promisc_ok(bp))
10823 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10824 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
10825 	if (rc && vnic->mc_list_count) {
10826 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
10827 			    rc);
10828 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10829 		vnic->mc_list_count = 0;
10830 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
10831 	}
10832 	if (rc)
10833 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
10834 			   rc);
10835 
10836 	return rc;
10837 }
10838 
10839 static bool bnxt_can_reserve_rings(struct bnxt *bp)
10840 {
10841 #ifdef CONFIG_BNXT_SRIOV
10842 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
10843 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
10844 
10845 		/* No minimum rings were provisioned by the PF.  Don't
10846 		 * reserve rings by default when device is down.
10847 		 */
10848 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
10849 			return true;
10850 
10851 		if (!netif_running(bp->dev))
10852 			return false;
10853 	}
10854 #endif
10855 	return true;
10856 }
10857 
10858 /* If the chip and firmware supports RFS */
10859 static bool bnxt_rfs_supported(struct bnxt *bp)
10860 {
10861 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
10862 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
10863 			return true;
10864 		return false;
10865 	}
10866 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
10867 		return true;
10868 	if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
10869 		return true;
10870 	return false;
10871 }
10872 
10873 /* If runtime conditions support RFS */
10874 static bool bnxt_rfs_capable(struct bnxt *bp)
10875 {
10876 #ifdef CONFIG_RFS_ACCEL
10877 	int vnics, max_vnics, max_rss_ctxs;
10878 
10879 	if (bp->flags & BNXT_FLAG_CHIP_P5)
10880 		return bnxt_rfs_supported(bp);
10881 	if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp))
10882 		return false;
10883 
10884 	vnics = 1 + bp->rx_nr_rings;
10885 	max_vnics = bnxt_get_max_func_vnics(bp);
10886 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
10887 
10888 	/* RSS contexts not a limiting factor */
10889 	if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
10890 		max_rss_ctxs = max_vnics;
10891 	if (vnics > max_vnics || vnics > max_rss_ctxs) {
10892 		if (bp->rx_nr_rings > 1)
10893 			netdev_warn(bp->dev,
10894 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
10895 				    min(max_rss_ctxs - 1, max_vnics - 1));
10896 		return false;
10897 	}
10898 
10899 	if (!BNXT_NEW_RM(bp))
10900 		return true;
10901 
10902 	if (vnics == bp->hw_resc.resv_vnics)
10903 		return true;
10904 
10905 	bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, vnics);
10906 	if (vnics <= bp->hw_resc.resv_vnics)
10907 		return true;
10908 
10909 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
10910 	bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, 1);
10911 	return false;
10912 #else
10913 	return false;
10914 #endif
10915 }
10916 
10917 static netdev_features_t bnxt_fix_features(struct net_device *dev,
10918 					   netdev_features_t features)
10919 {
10920 	struct bnxt *bp = netdev_priv(dev);
10921 	netdev_features_t vlan_features;
10922 
10923 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
10924 		features &= ~NETIF_F_NTUPLE;
10925 
10926 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
10927 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
10928 
10929 	if (!(features & NETIF_F_GRO))
10930 		features &= ~NETIF_F_GRO_HW;
10931 
10932 	if (features & NETIF_F_GRO_HW)
10933 		features &= ~NETIF_F_LRO;
10934 
10935 	/* Both CTAG and STAG VLAN accelaration on the RX side have to be
10936 	 * turned on or off together.
10937 	 */
10938 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
10939 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
10940 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
10941 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
10942 		else if (vlan_features)
10943 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
10944 	}
10945 #ifdef CONFIG_BNXT_SRIOV
10946 	if (BNXT_VF(bp) && bp->vf.vlan)
10947 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
10948 #endif
10949 	return features;
10950 }
10951 
10952 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
10953 {
10954 	struct bnxt *bp = netdev_priv(dev);
10955 	u32 flags = bp->flags;
10956 	u32 changes;
10957 	int rc = 0;
10958 	bool re_init = false;
10959 	bool update_tpa = false;
10960 
10961 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
10962 	if (features & NETIF_F_GRO_HW)
10963 		flags |= BNXT_FLAG_GRO;
10964 	else if (features & NETIF_F_LRO)
10965 		flags |= BNXT_FLAG_LRO;
10966 
10967 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
10968 		flags &= ~BNXT_FLAG_TPA;
10969 
10970 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
10971 		flags |= BNXT_FLAG_STRIP_VLAN;
10972 
10973 	if (features & NETIF_F_NTUPLE)
10974 		flags |= BNXT_FLAG_RFS;
10975 
10976 	changes = flags ^ bp->flags;
10977 	if (changes & BNXT_FLAG_TPA) {
10978 		update_tpa = true;
10979 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
10980 		    (flags & BNXT_FLAG_TPA) == 0 ||
10981 		    (bp->flags & BNXT_FLAG_CHIP_P5))
10982 			re_init = true;
10983 	}
10984 
10985 	if (changes & ~BNXT_FLAG_TPA)
10986 		re_init = true;
10987 
10988 	if (flags != bp->flags) {
10989 		u32 old_flags = bp->flags;
10990 
10991 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
10992 			bp->flags = flags;
10993 			if (update_tpa)
10994 				bnxt_set_ring_params(bp);
10995 			return rc;
10996 		}
10997 
10998 		if (re_init) {
10999 			bnxt_close_nic(bp, false, false);
11000 			bp->flags = flags;
11001 			if (update_tpa)
11002 				bnxt_set_ring_params(bp);
11003 
11004 			return bnxt_open_nic(bp, false, false);
11005 		}
11006 		if (update_tpa) {
11007 			bp->flags = flags;
11008 			rc = bnxt_set_tpa(bp,
11009 					  (flags & BNXT_FLAG_TPA) ?
11010 					  true : false);
11011 			if (rc)
11012 				bp->flags = old_flags;
11013 		}
11014 	}
11015 	return rc;
11016 }
11017 
11018 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
11019 			      u8 **nextp)
11020 {
11021 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
11022 	int hdr_count = 0;
11023 	u8 *nexthdr;
11024 	int start;
11025 
11026 	/* Check that there are at most 2 IPv6 extension headers, no
11027 	 * fragment header, and each is <= 64 bytes.
11028 	 */
11029 	start = nw_off + sizeof(*ip6h);
11030 	nexthdr = &ip6h->nexthdr;
11031 	while (ipv6_ext_hdr(*nexthdr)) {
11032 		struct ipv6_opt_hdr *hp;
11033 		int hdrlen;
11034 
11035 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
11036 		    *nexthdr == NEXTHDR_FRAGMENT)
11037 			return false;
11038 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
11039 					  skb_headlen(skb), NULL);
11040 		if (!hp)
11041 			return false;
11042 		if (*nexthdr == NEXTHDR_AUTH)
11043 			hdrlen = ipv6_authlen(hp);
11044 		else
11045 			hdrlen = ipv6_optlen(hp);
11046 
11047 		if (hdrlen > 64)
11048 			return false;
11049 		nexthdr = &hp->nexthdr;
11050 		start += hdrlen;
11051 		hdr_count++;
11052 	}
11053 	if (nextp) {
11054 		/* Caller will check inner protocol */
11055 		if (skb->encapsulation) {
11056 			*nextp = nexthdr;
11057 			return true;
11058 		}
11059 		*nextp = NULL;
11060 	}
11061 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
11062 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
11063 }
11064 
11065 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
11066 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
11067 {
11068 	struct udphdr *uh = udp_hdr(skb);
11069 	__be16 udp_port = uh->dest;
11070 
11071 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port)
11072 		return false;
11073 	if (skb->inner_protocol_type == ENCAP_TYPE_ETHER) {
11074 		struct ethhdr *eh = inner_eth_hdr(skb);
11075 
11076 		switch (eh->h_proto) {
11077 		case htons(ETH_P_IP):
11078 			return true;
11079 		case htons(ETH_P_IPV6):
11080 			return bnxt_exthdr_check(bp, skb,
11081 						 skb_inner_network_offset(skb),
11082 						 NULL);
11083 		}
11084 	}
11085 	return false;
11086 }
11087 
11088 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
11089 {
11090 	switch (l4_proto) {
11091 	case IPPROTO_UDP:
11092 		return bnxt_udp_tunl_check(bp, skb);
11093 	case IPPROTO_IPIP:
11094 		return true;
11095 	case IPPROTO_GRE: {
11096 		switch (skb->inner_protocol) {
11097 		default:
11098 			return false;
11099 		case htons(ETH_P_IP):
11100 			return true;
11101 		case htons(ETH_P_IPV6):
11102 			fallthrough;
11103 		}
11104 	}
11105 	case IPPROTO_IPV6:
11106 		/* Check ext headers of inner ipv6 */
11107 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
11108 					 NULL);
11109 	}
11110 	return false;
11111 }
11112 
11113 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
11114 					     struct net_device *dev,
11115 					     netdev_features_t features)
11116 {
11117 	struct bnxt *bp = netdev_priv(dev);
11118 	u8 *l4_proto;
11119 
11120 	features = vlan_features_check(skb, features);
11121 	switch (vlan_get_protocol(skb)) {
11122 	case htons(ETH_P_IP):
11123 		if (!skb->encapsulation)
11124 			return features;
11125 		l4_proto = &ip_hdr(skb)->protocol;
11126 		if (bnxt_tunl_check(bp, skb, *l4_proto))
11127 			return features;
11128 		break;
11129 	case htons(ETH_P_IPV6):
11130 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
11131 				       &l4_proto))
11132 			break;
11133 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
11134 			return features;
11135 		break;
11136 	}
11137 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
11138 }
11139 
11140 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
11141 			 u32 *reg_buf)
11142 {
11143 	struct hwrm_dbg_read_direct_output *resp = bp->hwrm_cmd_resp_addr;
11144 	struct hwrm_dbg_read_direct_input req = {0};
11145 	__le32 *dbg_reg_buf;
11146 	dma_addr_t mapping;
11147 	int rc, i;
11148 
11149 	dbg_reg_buf = dma_alloc_coherent(&bp->pdev->dev, num_words * 4,
11150 					 &mapping, GFP_KERNEL);
11151 	if (!dbg_reg_buf)
11152 		return -ENOMEM;
11153 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_READ_DIRECT, -1, -1);
11154 	req.host_dest_addr = cpu_to_le64(mapping);
11155 	req.read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
11156 	req.read_len32 = cpu_to_le32(num_words);
11157 	mutex_lock(&bp->hwrm_cmd_lock);
11158 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
11159 	if (rc || resp->error_code) {
11160 		rc = -EIO;
11161 		goto dbg_rd_reg_exit;
11162 	}
11163 	for (i = 0; i < num_words; i++)
11164 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
11165 
11166 dbg_rd_reg_exit:
11167 	mutex_unlock(&bp->hwrm_cmd_lock);
11168 	dma_free_coherent(&bp->pdev->dev, num_words * 4, dbg_reg_buf, mapping);
11169 	return rc;
11170 }
11171 
11172 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
11173 				       u32 ring_id, u32 *prod, u32 *cons)
11174 {
11175 	struct hwrm_dbg_ring_info_get_output *resp = bp->hwrm_cmd_resp_addr;
11176 	struct hwrm_dbg_ring_info_get_input req = {0};
11177 	int rc;
11178 
11179 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_RING_INFO_GET, -1, -1);
11180 	req.ring_type = ring_type;
11181 	req.fw_ring_id = cpu_to_le32(ring_id);
11182 	mutex_lock(&bp->hwrm_cmd_lock);
11183 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
11184 	if (!rc) {
11185 		*prod = le32_to_cpu(resp->producer_index);
11186 		*cons = le32_to_cpu(resp->consumer_index);
11187 	}
11188 	mutex_unlock(&bp->hwrm_cmd_lock);
11189 	return rc;
11190 }
11191 
11192 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
11193 {
11194 	struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
11195 	int i = bnapi->index;
11196 
11197 	if (!txr)
11198 		return;
11199 
11200 	netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
11201 		    i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
11202 		    txr->tx_cons);
11203 }
11204 
11205 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
11206 {
11207 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
11208 	int i = bnapi->index;
11209 
11210 	if (!rxr)
11211 		return;
11212 
11213 	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",
11214 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
11215 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
11216 		    rxr->rx_sw_agg_prod);
11217 }
11218 
11219 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
11220 {
11221 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
11222 	int i = bnapi->index;
11223 
11224 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
11225 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
11226 }
11227 
11228 static void bnxt_dbg_dump_states(struct bnxt *bp)
11229 {
11230 	int i;
11231 	struct bnxt_napi *bnapi;
11232 
11233 	for (i = 0; i < bp->cp_nr_rings; i++) {
11234 		bnapi = bp->bnapi[i];
11235 		if (netif_msg_drv(bp)) {
11236 			bnxt_dump_tx_sw_state(bnapi);
11237 			bnxt_dump_rx_sw_state(bnapi);
11238 			bnxt_dump_cp_sw_state(bnapi);
11239 		}
11240 	}
11241 }
11242 
11243 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
11244 {
11245 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
11246 	struct hwrm_ring_reset_input req = {0};
11247 	struct bnxt_napi *bnapi = rxr->bnapi;
11248 	struct bnxt_cp_ring_info *cpr;
11249 	u16 cp_ring_id;
11250 
11251 	cpr = &bnapi->cp_ring;
11252 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
11253 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_RESET, cp_ring_id, -1);
11254 	req.ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
11255 	req.ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
11256 	return hwrm_send_message_silent(bp, &req, sizeof(req),
11257 					HWRM_CMD_TIMEOUT);
11258 }
11259 
11260 static void bnxt_reset_task(struct bnxt *bp, bool silent)
11261 {
11262 	if (!silent)
11263 		bnxt_dbg_dump_states(bp);
11264 	if (netif_running(bp->dev)) {
11265 		int rc;
11266 
11267 		if (silent) {
11268 			bnxt_close_nic(bp, false, false);
11269 			bnxt_open_nic(bp, false, false);
11270 		} else {
11271 			bnxt_ulp_stop(bp);
11272 			bnxt_close_nic(bp, true, false);
11273 			rc = bnxt_open_nic(bp, true, false);
11274 			bnxt_ulp_start(bp, rc);
11275 		}
11276 	}
11277 }
11278 
11279 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
11280 {
11281 	struct bnxt *bp = netdev_priv(dev);
11282 
11283 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
11284 	set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
11285 	bnxt_queue_sp_work(bp);
11286 }
11287 
11288 static void bnxt_fw_health_check(struct bnxt *bp)
11289 {
11290 	struct bnxt_fw_health *fw_health = bp->fw_health;
11291 	u32 val;
11292 
11293 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11294 		return;
11295 
11296 	if (fw_health->tmr_counter) {
11297 		fw_health->tmr_counter--;
11298 		return;
11299 	}
11300 
11301 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
11302 	if (val == fw_health->last_fw_heartbeat)
11303 		goto fw_reset;
11304 
11305 	fw_health->last_fw_heartbeat = val;
11306 
11307 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
11308 	if (val != fw_health->last_fw_reset_cnt)
11309 		goto fw_reset;
11310 
11311 	fw_health->tmr_counter = fw_health->tmr_multiplier;
11312 	return;
11313 
11314 fw_reset:
11315 	set_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event);
11316 	bnxt_queue_sp_work(bp);
11317 }
11318 
11319 static void bnxt_timer(struct timer_list *t)
11320 {
11321 	struct bnxt *bp = from_timer(bp, t, timer);
11322 	struct net_device *dev = bp->dev;
11323 
11324 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
11325 		return;
11326 
11327 	if (atomic_read(&bp->intr_sem) != 0)
11328 		goto bnxt_restart_timer;
11329 
11330 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
11331 		bnxt_fw_health_check(bp);
11332 
11333 	if (bp->link_info.link_up && bp->stats_coal_ticks) {
11334 		set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
11335 		bnxt_queue_sp_work(bp);
11336 	}
11337 
11338 	if (bnxt_tc_flower_enabled(bp)) {
11339 		set_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event);
11340 		bnxt_queue_sp_work(bp);
11341 	}
11342 
11343 #ifdef CONFIG_RFS_ACCEL
11344 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count) {
11345 		set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
11346 		bnxt_queue_sp_work(bp);
11347 	}
11348 #endif /*CONFIG_RFS_ACCEL*/
11349 
11350 	if (bp->link_info.phy_retry) {
11351 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
11352 			bp->link_info.phy_retry = false;
11353 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
11354 		} else {
11355 			set_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event);
11356 			bnxt_queue_sp_work(bp);
11357 		}
11358 	}
11359 
11360 	if ((bp->flags & BNXT_FLAG_CHIP_P5) && !bp->chip_rev &&
11361 	    netif_carrier_ok(dev)) {
11362 		set_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event);
11363 		bnxt_queue_sp_work(bp);
11364 	}
11365 bnxt_restart_timer:
11366 	mod_timer(&bp->timer, jiffies + bp->current_interval);
11367 }
11368 
11369 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
11370 {
11371 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
11372 	 * set.  If the device is being closed, bnxt_close() may be holding
11373 	 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear.  So we
11374 	 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
11375 	 */
11376 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11377 	rtnl_lock();
11378 }
11379 
11380 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
11381 {
11382 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11383 	rtnl_unlock();
11384 }
11385 
11386 /* Only called from bnxt_sp_task() */
11387 static void bnxt_reset(struct bnxt *bp, bool silent)
11388 {
11389 	bnxt_rtnl_lock_sp(bp);
11390 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
11391 		bnxt_reset_task(bp, silent);
11392 	bnxt_rtnl_unlock_sp(bp);
11393 }
11394 
11395 /* Only called from bnxt_sp_task() */
11396 static void bnxt_rx_ring_reset(struct bnxt *bp)
11397 {
11398 	int i;
11399 
11400 	bnxt_rtnl_lock_sp(bp);
11401 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
11402 		bnxt_rtnl_unlock_sp(bp);
11403 		return;
11404 	}
11405 	/* Disable and flush TPA before resetting the RX ring */
11406 	if (bp->flags & BNXT_FLAG_TPA)
11407 		bnxt_set_tpa(bp, false);
11408 	for (i = 0; i < bp->rx_nr_rings; i++) {
11409 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
11410 		struct bnxt_cp_ring_info *cpr;
11411 		int rc;
11412 
11413 		if (!rxr->bnapi->in_reset)
11414 			continue;
11415 
11416 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
11417 		if (rc) {
11418 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
11419 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
11420 			else
11421 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
11422 					    rc);
11423 			bnxt_reset_task(bp, true);
11424 			break;
11425 		}
11426 		bnxt_free_one_rx_ring_skbs(bp, i);
11427 		rxr->rx_prod = 0;
11428 		rxr->rx_agg_prod = 0;
11429 		rxr->rx_sw_agg_prod = 0;
11430 		rxr->rx_next_cons = 0;
11431 		rxr->bnapi->in_reset = false;
11432 		bnxt_alloc_one_rx_ring(bp, i);
11433 		cpr = &rxr->bnapi->cp_ring;
11434 		cpr->sw_stats.rx.rx_resets++;
11435 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
11436 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
11437 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
11438 	}
11439 	if (bp->flags & BNXT_FLAG_TPA)
11440 		bnxt_set_tpa(bp, true);
11441 	bnxt_rtnl_unlock_sp(bp);
11442 }
11443 
11444 static void bnxt_fw_reset_close(struct bnxt *bp)
11445 {
11446 	bnxt_ulp_stop(bp);
11447 	/* When firmware is in fatal state, quiesce device and disable
11448 	 * bus master to prevent any potential bad DMAs before freeing
11449 	 * kernel memory.
11450 	 */
11451 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
11452 		u16 val = 0;
11453 
11454 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
11455 		if (val == 0xffff)
11456 			bp->fw_reset_min_dsecs = 0;
11457 		bnxt_tx_disable(bp);
11458 		bnxt_disable_napi(bp);
11459 		bnxt_disable_int_sync(bp);
11460 		bnxt_free_irq(bp);
11461 		bnxt_clear_int_mode(bp);
11462 		pci_disable_device(bp->pdev);
11463 	}
11464 	__bnxt_close_nic(bp, true, false);
11465 	bnxt_vf_reps_free(bp);
11466 	bnxt_clear_int_mode(bp);
11467 	bnxt_hwrm_func_drv_unrgtr(bp);
11468 	if (pci_is_enabled(bp->pdev))
11469 		pci_disable_device(bp->pdev);
11470 	bnxt_free_ctx_mem(bp);
11471 	kfree(bp->ctx);
11472 	bp->ctx = NULL;
11473 }
11474 
11475 static bool is_bnxt_fw_ok(struct bnxt *bp)
11476 {
11477 	struct bnxt_fw_health *fw_health = bp->fw_health;
11478 	bool no_heartbeat = false, has_reset = false;
11479 	u32 val;
11480 
11481 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
11482 	if (val == fw_health->last_fw_heartbeat)
11483 		no_heartbeat = true;
11484 
11485 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
11486 	if (val != fw_health->last_fw_reset_cnt)
11487 		has_reset = true;
11488 
11489 	if (!no_heartbeat && has_reset)
11490 		return true;
11491 
11492 	return false;
11493 }
11494 
11495 /* rtnl_lock is acquired before calling this function */
11496 static void bnxt_force_fw_reset(struct bnxt *bp)
11497 {
11498 	struct bnxt_fw_health *fw_health = bp->fw_health;
11499 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
11500 	u32 wait_dsecs;
11501 
11502 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
11503 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11504 		return;
11505 
11506 	if (ptp) {
11507 		spin_lock_bh(&ptp->ptp_lock);
11508 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11509 		spin_unlock_bh(&ptp->ptp_lock);
11510 	} else {
11511 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11512 	}
11513 	bnxt_fw_reset_close(bp);
11514 	wait_dsecs = fw_health->master_func_wait_dsecs;
11515 	if (fw_health->master) {
11516 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
11517 			wait_dsecs = 0;
11518 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
11519 	} else {
11520 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
11521 		wait_dsecs = fw_health->normal_func_wait_dsecs;
11522 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11523 	}
11524 
11525 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
11526 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
11527 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
11528 }
11529 
11530 void bnxt_fw_exception(struct bnxt *bp)
11531 {
11532 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
11533 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
11534 	bnxt_rtnl_lock_sp(bp);
11535 	bnxt_force_fw_reset(bp);
11536 	bnxt_rtnl_unlock_sp(bp);
11537 }
11538 
11539 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
11540  * < 0 on error.
11541  */
11542 static int bnxt_get_registered_vfs(struct bnxt *bp)
11543 {
11544 #ifdef CONFIG_BNXT_SRIOV
11545 	int rc;
11546 
11547 	if (!BNXT_PF(bp))
11548 		return 0;
11549 
11550 	rc = bnxt_hwrm_func_qcfg(bp);
11551 	if (rc) {
11552 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
11553 		return rc;
11554 	}
11555 	if (bp->pf.registered_vfs)
11556 		return bp->pf.registered_vfs;
11557 	if (bp->sriov_cfg)
11558 		return 1;
11559 #endif
11560 	return 0;
11561 }
11562 
11563 void bnxt_fw_reset(struct bnxt *bp)
11564 {
11565 	bnxt_rtnl_lock_sp(bp);
11566 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
11567 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
11568 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
11569 		int n = 0, tmo;
11570 
11571 		if (ptp) {
11572 			spin_lock_bh(&ptp->ptp_lock);
11573 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11574 			spin_unlock_bh(&ptp->ptp_lock);
11575 		} else {
11576 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11577 		}
11578 		if (bp->pf.active_vfs &&
11579 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
11580 			n = bnxt_get_registered_vfs(bp);
11581 		if (n < 0) {
11582 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
11583 				   n);
11584 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11585 			dev_close(bp->dev);
11586 			goto fw_reset_exit;
11587 		} else if (n > 0) {
11588 			u16 vf_tmo_dsecs = n * 10;
11589 
11590 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
11591 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
11592 			bp->fw_reset_state =
11593 				BNXT_FW_RESET_STATE_POLL_VF;
11594 			bnxt_queue_fw_reset_work(bp, HZ / 10);
11595 			goto fw_reset_exit;
11596 		}
11597 		bnxt_fw_reset_close(bp);
11598 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
11599 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
11600 			tmo = HZ / 10;
11601 		} else {
11602 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11603 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
11604 		}
11605 		bnxt_queue_fw_reset_work(bp, tmo);
11606 	}
11607 fw_reset_exit:
11608 	bnxt_rtnl_unlock_sp(bp);
11609 }
11610 
11611 static void bnxt_chk_missed_irq(struct bnxt *bp)
11612 {
11613 	int i;
11614 
11615 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
11616 		return;
11617 
11618 	for (i = 0; i < bp->cp_nr_rings; i++) {
11619 		struct bnxt_napi *bnapi = bp->bnapi[i];
11620 		struct bnxt_cp_ring_info *cpr;
11621 		u32 fw_ring_id;
11622 		int j;
11623 
11624 		if (!bnapi)
11625 			continue;
11626 
11627 		cpr = &bnapi->cp_ring;
11628 		for (j = 0; j < 2; j++) {
11629 			struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
11630 			u32 val[2];
11631 
11632 			if (!cpr2 || cpr2->has_more_work ||
11633 			    !bnxt_has_work(bp, cpr2))
11634 				continue;
11635 
11636 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
11637 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
11638 				continue;
11639 			}
11640 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
11641 			bnxt_dbg_hwrm_ring_info_get(bp,
11642 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
11643 				fw_ring_id, &val[0], &val[1]);
11644 			cpr->sw_stats.cmn.missed_irqs++;
11645 		}
11646 	}
11647 }
11648 
11649 static void bnxt_cfg_ntp_filters(struct bnxt *);
11650 
11651 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
11652 {
11653 	struct bnxt_link_info *link_info = &bp->link_info;
11654 
11655 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
11656 		link_info->autoneg = BNXT_AUTONEG_SPEED;
11657 		if (bp->hwrm_spec_code >= 0x10201) {
11658 			if (link_info->auto_pause_setting &
11659 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
11660 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
11661 		} else {
11662 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
11663 		}
11664 		link_info->advertising = link_info->auto_link_speeds;
11665 		link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
11666 	} else {
11667 		link_info->req_link_speed = link_info->force_link_speed;
11668 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
11669 		if (link_info->force_pam4_link_speed) {
11670 			link_info->req_link_speed =
11671 				link_info->force_pam4_link_speed;
11672 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
11673 		}
11674 		link_info->req_duplex = link_info->duplex_setting;
11675 	}
11676 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
11677 		link_info->req_flow_ctrl =
11678 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
11679 	else
11680 		link_info->req_flow_ctrl = link_info->force_pause_setting;
11681 }
11682 
11683 static void bnxt_fw_echo_reply(struct bnxt *bp)
11684 {
11685 	struct bnxt_fw_health *fw_health = bp->fw_health;
11686 	struct hwrm_func_echo_response_input req = {0};
11687 
11688 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_ECHO_RESPONSE, -1, -1);
11689 	req.event_data1 = cpu_to_le32(fw_health->echo_req_data1);
11690 	req.event_data2 = cpu_to_le32(fw_health->echo_req_data2);
11691 	hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
11692 }
11693 
11694 static void bnxt_sp_task(struct work_struct *work)
11695 {
11696 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
11697 
11698 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11699 	smp_mb__after_atomic();
11700 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
11701 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11702 		return;
11703 	}
11704 
11705 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
11706 		bnxt_cfg_rx_mode(bp);
11707 
11708 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
11709 		bnxt_cfg_ntp_filters(bp);
11710 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
11711 		bnxt_hwrm_exec_fwd_req(bp);
11712 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
11713 		bnxt_hwrm_port_qstats(bp, 0);
11714 		bnxt_hwrm_port_qstats_ext(bp, 0);
11715 		bnxt_accumulate_all_stats(bp);
11716 	}
11717 
11718 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
11719 		int rc;
11720 
11721 		mutex_lock(&bp->link_lock);
11722 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
11723 				       &bp->sp_event))
11724 			bnxt_hwrm_phy_qcaps(bp);
11725 
11726 		rc = bnxt_update_link(bp, true);
11727 		if (rc)
11728 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
11729 				   rc);
11730 
11731 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
11732 				       &bp->sp_event))
11733 			bnxt_init_ethtool_link_settings(bp);
11734 		mutex_unlock(&bp->link_lock);
11735 	}
11736 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
11737 		int rc;
11738 
11739 		mutex_lock(&bp->link_lock);
11740 		rc = bnxt_update_phy_setting(bp);
11741 		mutex_unlock(&bp->link_lock);
11742 		if (rc) {
11743 			netdev_warn(bp->dev, "update phy settings retry failed\n");
11744 		} else {
11745 			bp->link_info.phy_retry = false;
11746 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
11747 		}
11748 	}
11749 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
11750 		mutex_lock(&bp->link_lock);
11751 		bnxt_get_port_module_status(bp);
11752 		mutex_unlock(&bp->link_lock);
11753 	}
11754 
11755 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
11756 		bnxt_tc_flow_stats_work(bp);
11757 
11758 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
11759 		bnxt_chk_missed_irq(bp);
11760 
11761 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
11762 		bnxt_fw_echo_reply(bp);
11763 
11764 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
11765 	 * must be the last functions to be called before exiting.
11766 	 */
11767 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
11768 		bnxt_reset(bp, false);
11769 
11770 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
11771 		bnxt_reset(bp, true);
11772 
11773 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
11774 		bnxt_rx_ring_reset(bp);
11775 
11776 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event))
11777 		bnxt_devlink_health_report(bp, BNXT_FW_RESET_NOTIFY_SP_EVENT);
11778 
11779 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
11780 		if (!is_bnxt_fw_ok(bp))
11781 			bnxt_devlink_health_report(bp,
11782 						   BNXT_FW_EXCEPTION_SP_EVENT);
11783 	}
11784 
11785 	smp_mb__before_atomic();
11786 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11787 }
11788 
11789 /* Under rtnl_lock */
11790 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
11791 		     int tx_xdp)
11792 {
11793 	int max_rx, max_tx, tx_sets = 1;
11794 	int tx_rings_needed, stats;
11795 	int rx_rings = rx;
11796 	int cp, vnics, rc;
11797 
11798 	if (tcs)
11799 		tx_sets = tcs;
11800 
11801 	rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
11802 	if (rc)
11803 		return rc;
11804 
11805 	if (max_rx < rx)
11806 		return -ENOMEM;
11807 
11808 	tx_rings_needed = tx * tx_sets + tx_xdp;
11809 	if (max_tx < tx_rings_needed)
11810 		return -ENOMEM;
11811 
11812 	vnics = 1;
11813 	if ((bp->flags & (BNXT_FLAG_RFS | BNXT_FLAG_CHIP_P5)) == BNXT_FLAG_RFS)
11814 		vnics += rx_rings;
11815 
11816 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
11817 		rx_rings <<= 1;
11818 	cp = sh ? max_t(int, tx_rings_needed, rx) : tx_rings_needed + rx;
11819 	stats = cp;
11820 	if (BNXT_NEW_RM(bp)) {
11821 		cp += bnxt_get_ulp_msix_num(bp);
11822 		stats += bnxt_get_ulp_stat_ctxs(bp);
11823 	}
11824 	return bnxt_hwrm_check_rings(bp, tx_rings_needed, rx_rings, rx, cp,
11825 				     stats, vnics);
11826 }
11827 
11828 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
11829 {
11830 	if (bp->bar2) {
11831 		pci_iounmap(pdev, bp->bar2);
11832 		bp->bar2 = NULL;
11833 	}
11834 
11835 	if (bp->bar1) {
11836 		pci_iounmap(pdev, bp->bar1);
11837 		bp->bar1 = NULL;
11838 	}
11839 
11840 	if (bp->bar0) {
11841 		pci_iounmap(pdev, bp->bar0);
11842 		bp->bar0 = NULL;
11843 	}
11844 }
11845 
11846 static void bnxt_cleanup_pci(struct bnxt *bp)
11847 {
11848 	bnxt_unmap_bars(bp, bp->pdev);
11849 	pci_release_regions(bp->pdev);
11850 	if (pci_is_enabled(bp->pdev))
11851 		pci_disable_device(bp->pdev);
11852 }
11853 
11854 static void bnxt_init_dflt_coal(struct bnxt *bp)
11855 {
11856 	struct bnxt_coal *coal;
11857 
11858 	/* Tick values in micro seconds.
11859 	 * 1 coal_buf x bufs_per_record = 1 completion record.
11860 	 */
11861 	coal = &bp->rx_coal;
11862 	coal->coal_ticks = 10;
11863 	coal->coal_bufs = 30;
11864 	coal->coal_ticks_irq = 1;
11865 	coal->coal_bufs_irq = 2;
11866 	coal->idle_thresh = 50;
11867 	coal->bufs_per_record = 2;
11868 	coal->budget = 64;		/* NAPI budget */
11869 
11870 	coal = &bp->tx_coal;
11871 	coal->coal_ticks = 28;
11872 	coal->coal_bufs = 30;
11873 	coal->coal_ticks_irq = 2;
11874 	coal->coal_bufs_irq = 2;
11875 	coal->bufs_per_record = 1;
11876 
11877 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
11878 }
11879 
11880 static int bnxt_fw_init_one_p1(struct bnxt *bp)
11881 {
11882 	int rc;
11883 
11884 	bp->fw_cap = 0;
11885 	rc = bnxt_hwrm_ver_get(bp);
11886 	bnxt_try_map_fw_health_reg(bp);
11887 	if (rc) {
11888 		rc = bnxt_try_recover_fw(bp);
11889 		if (rc)
11890 			return rc;
11891 		rc = bnxt_hwrm_ver_get(bp);
11892 		if (rc)
11893 			return rc;
11894 	}
11895 
11896 	if (bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL) {
11897 		rc = bnxt_alloc_kong_hwrm_resources(bp);
11898 		if (rc)
11899 			bp->fw_cap &= ~BNXT_FW_CAP_KONG_MB_CHNL;
11900 	}
11901 
11902 	if ((bp->fw_cap & BNXT_FW_CAP_SHORT_CMD) ||
11903 	    bp->hwrm_max_ext_req_len > BNXT_HWRM_MAX_REQ_LEN) {
11904 		rc = bnxt_alloc_hwrm_short_cmd_req(bp);
11905 		if (rc)
11906 			return rc;
11907 	}
11908 	bnxt_nvm_cfg_ver_get(bp);
11909 
11910 	rc = bnxt_hwrm_func_reset(bp);
11911 	if (rc)
11912 		return -ENODEV;
11913 
11914 	bnxt_hwrm_fw_set_time(bp);
11915 	return 0;
11916 }
11917 
11918 static int bnxt_fw_init_one_p2(struct bnxt *bp)
11919 {
11920 	int rc;
11921 
11922 	/* Get the MAX capabilities for this function */
11923 	rc = bnxt_hwrm_func_qcaps(bp);
11924 	if (rc) {
11925 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
11926 			   rc);
11927 		return -ENODEV;
11928 	}
11929 
11930 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
11931 	if (rc)
11932 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
11933 			    rc);
11934 
11935 	if (bnxt_alloc_fw_health(bp)) {
11936 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
11937 	} else {
11938 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
11939 		if (rc)
11940 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
11941 				    rc);
11942 	}
11943 
11944 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
11945 	if (rc)
11946 		return -ENODEV;
11947 
11948 	bnxt_hwrm_func_qcfg(bp);
11949 	bnxt_hwrm_vnic_qcaps(bp);
11950 	bnxt_hwrm_port_led_qcaps(bp);
11951 	bnxt_ethtool_init(bp);
11952 	bnxt_dcb_init(bp);
11953 	return 0;
11954 }
11955 
11956 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
11957 {
11958 	bp->flags &= ~BNXT_FLAG_UDP_RSS_CAP;
11959 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
11960 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
11961 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
11962 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
11963 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
11964 		bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
11965 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
11966 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
11967 	}
11968 }
11969 
11970 static void bnxt_set_dflt_rfs(struct bnxt *bp)
11971 {
11972 	struct net_device *dev = bp->dev;
11973 
11974 	dev->hw_features &= ~NETIF_F_NTUPLE;
11975 	dev->features &= ~NETIF_F_NTUPLE;
11976 	bp->flags &= ~BNXT_FLAG_RFS;
11977 	if (bnxt_rfs_supported(bp)) {
11978 		dev->hw_features |= NETIF_F_NTUPLE;
11979 		if (bnxt_rfs_capable(bp)) {
11980 			bp->flags |= BNXT_FLAG_RFS;
11981 			dev->features |= NETIF_F_NTUPLE;
11982 		}
11983 	}
11984 }
11985 
11986 static void bnxt_fw_init_one_p3(struct bnxt *bp)
11987 {
11988 	struct pci_dev *pdev = bp->pdev;
11989 
11990 	bnxt_set_dflt_rss_hash_type(bp);
11991 	bnxt_set_dflt_rfs(bp);
11992 
11993 	bnxt_get_wol_settings(bp);
11994 	if (bp->flags & BNXT_FLAG_WOL_CAP)
11995 		device_set_wakeup_enable(&pdev->dev, bp->wol);
11996 	else
11997 		device_set_wakeup_capable(&pdev->dev, false);
11998 
11999 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
12000 	bnxt_hwrm_coal_params_qcaps(bp);
12001 }
12002 
12003 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
12004 
12005 static int bnxt_fw_init_one(struct bnxt *bp)
12006 {
12007 	int rc;
12008 
12009 	rc = bnxt_fw_init_one_p1(bp);
12010 	if (rc) {
12011 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
12012 		return rc;
12013 	}
12014 	rc = bnxt_fw_init_one_p2(bp);
12015 	if (rc) {
12016 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
12017 		return rc;
12018 	}
12019 	rc = bnxt_probe_phy(bp, false);
12020 	if (rc)
12021 		return rc;
12022 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
12023 	if (rc)
12024 		return rc;
12025 
12026 	/* In case fw capabilities have changed, destroy the unneeded
12027 	 * reporters and create newly capable ones.
12028 	 */
12029 	bnxt_dl_fw_reporters_destroy(bp, false);
12030 	bnxt_dl_fw_reporters_create(bp);
12031 	bnxt_fw_init_one_p3(bp);
12032 	return 0;
12033 }
12034 
12035 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
12036 {
12037 	struct bnxt_fw_health *fw_health = bp->fw_health;
12038 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
12039 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
12040 	u32 reg_type, reg_off, delay_msecs;
12041 
12042 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
12043 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
12044 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
12045 	switch (reg_type) {
12046 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
12047 		pci_write_config_dword(bp->pdev, reg_off, val);
12048 		break;
12049 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
12050 		writel(reg_off & BNXT_GRC_BASE_MASK,
12051 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
12052 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
12053 		fallthrough;
12054 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
12055 		writel(val, bp->bar0 + reg_off);
12056 		break;
12057 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
12058 		writel(val, bp->bar1 + reg_off);
12059 		break;
12060 	}
12061 	if (delay_msecs) {
12062 		pci_read_config_dword(bp->pdev, 0, &val);
12063 		msleep(delay_msecs);
12064 	}
12065 }
12066 
12067 static void bnxt_reset_all(struct bnxt *bp)
12068 {
12069 	struct bnxt_fw_health *fw_health = bp->fw_health;
12070 	int i, rc;
12071 
12072 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
12073 		bnxt_fw_reset_via_optee(bp);
12074 		bp->fw_reset_timestamp = jiffies;
12075 		return;
12076 	}
12077 
12078 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
12079 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
12080 			bnxt_fw_reset_writel(bp, i);
12081 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
12082 		struct hwrm_fw_reset_input req = {0};
12083 
12084 		bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_RESET, -1, -1);
12085 		req.resp_addr = cpu_to_le64(bp->hwrm_cmd_kong_resp_dma_addr);
12086 		req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
12087 		req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
12088 		req.flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
12089 		rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
12090 		if (rc != -ENODEV)
12091 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
12092 	}
12093 	bp->fw_reset_timestamp = jiffies;
12094 }
12095 
12096 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
12097 {
12098 	return time_after(jiffies, bp->fw_reset_timestamp +
12099 			  (bp->fw_reset_max_dsecs * HZ / 10));
12100 }
12101 
12102 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
12103 {
12104 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
12105 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF) {
12106 		bnxt_ulp_start(bp, rc);
12107 		bnxt_dl_health_status_update(bp, false);
12108 	}
12109 	bp->fw_reset_state = 0;
12110 	dev_close(bp->dev);
12111 }
12112 
12113 static void bnxt_fw_reset_task(struct work_struct *work)
12114 {
12115 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
12116 	int rc = 0;
12117 
12118 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12119 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
12120 		return;
12121 	}
12122 
12123 	switch (bp->fw_reset_state) {
12124 	case BNXT_FW_RESET_STATE_POLL_VF: {
12125 		int n = bnxt_get_registered_vfs(bp);
12126 		int tmo;
12127 
12128 		if (n < 0) {
12129 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
12130 				   n, jiffies_to_msecs(jiffies -
12131 				   bp->fw_reset_timestamp));
12132 			goto fw_reset_abort;
12133 		} else if (n > 0) {
12134 			if (bnxt_fw_reset_timeout(bp)) {
12135 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
12136 				bp->fw_reset_state = 0;
12137 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
12138 					   n);
12139 				return;
12140 			}
12141 			bnxt_queue_fw_reset_work(bp, HZ / 10);
12142 			return;
12143 		}
12144 		bp->fw_reset_timestamp = jiffies;
12145 		rtnl_lock();
12146 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12147 			bnxt_fw_reset_abort(bp, rc);
12148 			rtnl_unlock();
12149 			return;
12150 		}
12151 		bnxt_fw_reset_close(bp);
12152 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
12153 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
12154 			tmo = HZ / 10;
12155 		} else {
12156 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
12157 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
12158 		}
12159 		rtnl_unlock();
12160 		bnxt_queue_fw_reset_work(bp, tmo);
12161 		return;
12162 	}
12163 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
12164 		u32 val;
12165 
12166 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12167 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
12168 		    !bnxt_fw_reset_timeout(bp)) {
12169 			bnxt_queue_fw_reset_work(bp, HZ / 5);
12170 			return;
12171 		}
12172 
12173 		if (!bp->fw_health->master) {
12174 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
12175 
12176 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
12177 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
12178 			return;
12179 		}
12180 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
12181 	}
12182 		fallthrough;
12183 	case BNXT_FW_RESET_STATE_RESET_FW:
12184 		bnxt_reset_all(bp);
12185 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
12186 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
12187 		return;
12188 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
12189 		bnxt_inv_fw_health_reg(bp);
12190 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
12191 		    !bp->fw_reset_min_dsecs) {
12192 			u16 val;
12193 
12194 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
12195 			if (val == 0xffff) {
12196 				if (bnxt_fw_reset_timeout(bp)) {
12197 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
12198 					rc = -ETIMEDOUT;
12199 					goto fw_reset_abort;
12200 				}
12201 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
12202 				return;
12203 			}
12204 		}
12205 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
12206 		if (pci_enable_device(bp->pdev)) {
12207 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
12208 			rc = -ENODEV;
12209 			goto fw_reset_abort;
12210 		}
12211 		pci_set_master(bp->pdev);
12212 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
12213 		fallthrough;
12214 	case BNXT_FW_RESET_STATE_POLL_FW:
12215 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
12216 		rc = __bnxt_hwrm_ver_get(bp, true);
12217 		if (rc) {
12218 			if (bnxt_fw_reset_timeout(bp)) {
12219 				netdev_err(bp->dev, "Firmware reset aborted\n");
12220 				goto fw_reset_abort_status;
12221 			}
12222 			bnxt_queue_fw_reset_work(bp, HZ / 5);
12223 			return;
12224 		}
12225 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
12226 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
12227 		fallthrough;
12228 	case BNXT_FW_RESET_STATE_OPENING:
12229 		while (!rtnl_trylock()) {
12230 			bnxt_queue_fw_reset_work(bp, HZ / 10);
12231 			return;
12232 		}
12233 		rc = bnxt_open(bp->dev);
12234 		if (rc) {
12235 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
12236 			bnxt_fw_reset_abort(bp, rc);
12237 			rtnl_unlock();
12238 			return;
12239 		}
12240 
12241 		bp->fw_reset_state = 0;
12242 		/* Make sure fw_reset_state is 0 before clearing the flag */
12243 		smp_mb__before_atomic();
12244 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
12245 		bnxt_ulp_start(bp, 0);
12246 		bnxt_reenable_sriov(bp);
12247 		bnxt_vf_reps_alloc(bp);
12248 		bnxt_vf_reps_open(bp);
12249 		bnxt_ptp_reapply_pps(bp);
12250 		bnxt_dl_health_recovery_done(bp);
12251 		bnxt_dl_health_status_update(bp, true);
12252 		rtnl_unlock();
12253 		break;
12254 	}
12255 	return;
12256 
12257 fw_reset_abort_status:
12258 	if (bp->fw_health->status_reliable ||
12259 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
12260 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12261 
12262 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
12263 	}
12264 fw_reset_abort:
12265 	rtnl_lock();
12266 	bnxt_fw_reset_abort(bp, rc);
12267 	rtnl_unlock();
12268 }
12269 
12270 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
12271 {
12272 	int rc;
12273 	struct bnxt *bp = netdev_priv(dev);
12274 
12275 	SET_NETDEV_DEV(dev, &pdev->dev);
12276 
12277 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
12278 	rc = pci_enable_device(pdev);
12279 	if (rc) {
12280 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
12281 		goto init_err;
12282 	}
12283 
12284 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
12285 		dev_err(&pdev->dev,
12286 			"Cannot find PCI device base address, aborting\n");
12287 		rc = -ENODEV;
12288 		goto init_err_disable;
12289 	}
12290 
12291 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
12292 	if (rc) {
12293 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
12294 		goto init_err_disable;
12295 	}
12296 
12297 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
12298 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
12299 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
12300 		rc = -EIO;
12301 		goto init_err_release;
12302 	}
12303 
12304 	pci_set_master(pdev);
12305 
12306 	bp->dev = dev;
12307 	bp->pdev = pdev;
12308 
12309 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
12310 	 * determines the BAR size.
12311 	 */
12312 	bp->bar0 = pci_ioremap_bar(pdev, 0);
12313 	if (!bp->bar0) {
12314 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
12315 		rc = -ENOMEM;
12316 		goto init_err_release;
12317 	}
12318 
12319 	bp->bar2 = pci_ioremap_bar(pdev, 4);
12320 	if (!bp->bar2) {
12321 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
12322 		rc = -ENOMEM;
12323 		goto init_err_release;
12324 	}
12325 
12326 	pci_enable_pcie_error_reporting(pdev);
12327 
12328 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
12329 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
12330 
12331 	spin_lock_init(&bp->ntp_fltr_lock);
12332 #if BITS_PER_LONG == 32
12333 	spin_lock_init(&bp->db_lock);
12334 #endif
12335 
12336 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
12337 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
12338 
12339 	bnxt_init_dflt_coal(bp);
12340 
12341 	timer_setup(&bp->timer, bnxt_timer, 0);
12342 	bp->current_interval = BNXT_TIMER_INTERVAL;
12343 
12344 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
12345 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
12346 
12347 	clear_bit(BNXT_STATE_OPEN, &bp->state);
12348 	return 0;
12349 
12350 init_err_release:
12351 	bnxt_unmap_bars(bp, pdev);
12352 	pci_release_regions(pdev);
12353 
12354 init_err_disable:
12355 	pci_disable_device(pdev);
12356 
12357 init_err:
12358 	return rc;
12359 }
12360 
12361 /* rtnl_lock held */
12362 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
12363 {
12364 	struct sockaddr *addr = p;
12365 	struct bnxt *bp = netdev_priv(dev);
12366 	int rc = 0;
12367 
12368 	if (!is_valid_ether_addr(addr->sa_data))
12369 		return -EADDRNOTAVAIL;
12370 
12371 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
12372 		return 0;
12373 
12374 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
12375 	if (rc)
12376 		return rc;
12377 
12378 	memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
12379 	if (netif_running(dev)) {
12380 		bnxt_close_nic(bp, false, false);
12381 		rc = bnxt_open_nic(bp, false, false);
12382 	}
12383 
12384 	return rc;
12385 }
12386 
12387 /* rtnl_lock held */
12388 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
12389 {
12390 	struct bnxt *bp = netdev_priv(dev);
12391 
12392 	if (netif_running(dev))
12393 		bnxt_close_nic(bp, true, false);
12394 
12395 	dev->mtu = new_mtu;
12396 	bnxt_set_ring_params(bp);
12397 
12398 	if (netif_running(dev))
12399 		return bnxt_open_nic(bp, true, false);
12400 
12401 	return 0;
12402 }
12403 
12404 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
12405 {
12406 	struct bnxt *bp = netdev_priv(dev);
12407 	bool sh = false;
12408 	int rc;
12409 
12410 	if (tc > bp->max_tc) {
12411 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
12412 			   tc, bp->max_tc);
12413 		return -EINVAL;
12414 	}
12415 
12416 	if (netdev_get_num_tc(dev) == tc)
12417 		return 0;
12418 
12419 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
12420 		sh = true;
12421 
12422 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
12423 			      sh, tc, bp->tx_nr_rings_xdp);
12424 	if (rc)
12425 		return rc;
12426 
12427 	/* Needs to close the device and do hw resource re-allocations */
12428 	if (netif_running(bp->dev))
12429 		bnxt_close_nic(bp, true, false);
12430 
12431 	if (tc) {
12432 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
12433 		netdev_set_num_tc(dev, tc);
12434 	} else {
12435 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
12436 		netdev_reset_tc(dev);
12437 	}
12438 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
12439 	bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
12440 			       bp->tx_nr_rings + bp->rx_nr_rings;
12441 
12442 	if (netif_running(bp->dev))
12443 		return bnxt_open_nic(bp, true, false);
12444 
12445 	return 0;
12446 }
12447 
12448 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
12449 				  void *cb_priv)
12450 {
12451 	struct bnxt *bp = cb_priv;
12452 
12453 	if (!bnxt_tc_flower_enabled(bp) ||
12454 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
12455 		return -EOPNOTSUPP;
12456 
12457 	switch (type) {
12458 	case TC_SETUP_CLSFLOWER:
12459 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
12460 	default:
12461 		return -EOPNOTSUPP;
12462 	}
12463 }
12464 
12465 LIST_HEAD(bnxt_block_cb_list);
12466 
12467 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
12468 			 void *type_data)
12469 {
12470 	struct bnxt *bp = netdev_priv(dev);
12471 
12472 	switch (type) {
12473 	case TC_SETUP_BLOCK:
12474 		return flow_block_cb_setup_simple(type_data,
12475 						  &bnxt_block_cb_list,
12476 						  bnxt_setup_tc_block_cb,
12477 						  bp, bp, true);
12478 	case TC_SETUP_QDISC_MQPRIO: {
12479 		struct tc_mqprio_qopt *mqprio = type_data;
12480 
12481 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
12482 
12483 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
12484 	}
12485 	default:
12486 		return -EOPNOTSUPP;
12487 	}
12488 }
12489 
12490 #ifdef CONFIG_RFS_ACCEL
12491 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
12492 			    struct bnxt_ntuple_filter *f2)
12493 {
12494 	struct flow_keys *keys1 = &f1->fkeys;
12495 	struct flow_keys *keys2 = &f2->fkeys;
12496 
12497 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
12498 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
12499 		return false;
12500 
12501 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
12502 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
12503 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst)
12504 			return false;
12505 	} else {
12506 		if (memcmp(&keys1->addrs.v6addrs.src, &keys2->addrs.v6addrs.src,
12507 			   sizeof(keys1->addrs.v6addrs.src)) ||
12508 		    memcmp(&keys1->addrs.v6addrs.dst, &keys2->addrs.v6addrs.dst,
12509 			   sizeof(keys1->addrs.v6addrs.dst)))
12510 			return false;
12511 	}
12512 
12513 	if (keys1->ports.ports == keys2->ports.ports &&
12514 	    keys1->control.flags == keys2->control.flags &&
12515 	    ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
12516 	    ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
12517 		return true;
12518 
12519 	return false;
12520 }
12521 
12522 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
12523 			      u16 rxq_index, u32 flow_id)
12524 {
12525 	struct bnxt *bp = netdev_priv(dev);
12526 	struct bnxt_ntuple_filter *fltr, *new_fltr;
12527 	struct flow_keys *fkeys;
12528 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
12529 	int rc = 0, idx, bit_id, l2_idx = 0;
12530 	struct hlist_head *head;
12531 	u32 flags;
12532 
12533 	if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
12534 		struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
12535 		int off = 0, j;
12536 
12537 		netif_addr_lock_bh(dev);
12538 		for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
12539 			if (ether_addr_equal(eth->h_dest,
12540 					     vnic->uc_list + off)) {
12541 				l2_idx = j + 1;
12542 				break;
12543 			}
12544 		}
12545 		netif_addr_unlock_bh(dev);
12546 		if (!l2_idx)
12547 			return -EINVAL;
12548 	}
12549 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
12550 	if (!new_fltr)
12551 		return -ENOMEM;
12552 
12553 	fkeys = &new_fltr->fkeys;
12554 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
12555 		rc = -EPROTONOSUPPORT;
12556 		goto err_free;
12557 	}
12558 
12559 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
12560 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
12561 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
12562 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
12563 		rc = -EPROTONOSUPPORT;
12564 		goto err_free;
12565 	}
12566 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
12567 	    bp->hwrm_spec_code < 0x10601) {
12568 		rc = -EPROTONOSUPPORT;
12569 		goto err_free;
12570 	}
12571 	flags = fkeys->control.flags;
12572 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
12573 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
12574 		rc = -EPROTONOSUPPORT;
12575 		goto err_free;
12576 	}
12577 
12578 	memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
12579 	memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
12580 
12581 	idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
12582 	head = &bp->ntp_fltr_hash_tbl[idx];
12583 	rcu_read_lock();
12584 	hlist_for_each_entry_rcu(fltr, head, hash) {
12585 		if (bnxt_fltr_match(fltr, new_fltr)) {
12586 			rcu_read_unlock();
12587 			rc = 0;
12588 			goto err_free;
12589 		}
12590 	}
12591 	rcu_read_unlock();
12592 
12593 	spin_lock_bh(&bp->ntp_fltr_lock);
12594 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
12595 					 BNXT_NTP_FLTR_MAX_FLTR, 0);
12596 	if (bit_id < 0) {
12597 		spin_unlock_bh(&bp->ntp_fltr_lock);
12598 		rc = -ENOMEM;
12599 		goto err_free;
12600 	}
12601 
12602 	new_fltr->sw_id = (u16)bit_id;
12603 	new_fltr->flow_id = flow_id;
12604 	new_fltr->l2_fltr_idx = l2_idx;
12605 	new_fltr->rxq = rxq_index;
12606 	hlist_add_head_rcu(&new_fltr->hash, head);
12607 	bp->ntp_fltr_count++;
12608 	spin_unlock_bh(&bp->ntp_fltr_lock);
12609 
12610 	set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
12611 	bnxt_queue_sp_work(bp);
12612 
12613 	return new_fltr->sw_id;
12614 
12615 err_free:
12616 	kfree(new_fltr);
12617 	return rc;
12618 }
12619 
12620 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
12621 {
12622 	int i;
12623 
12624 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
12625 		struct hlist_head *head;
12626 		struct hlist_node *tmp;
12627 		struct bnxt_ntuple_filter *fltr;
12628 		int rc;
12629 
12630 		head = &bp->ntp_fltr_hash_tbl[i];
12631 		hlist_for_each_entry_safe(fltr, tmp, head, hash) {
12632 			bool del = false;
12633 
12634 			if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
12635 				if (rps_may_expire_flow(bp->dev, fltr->rxq,
12636 							fltr->flow_id,
12637 							fltr->sw_id)) {
12638 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
12639 									 fltr);
12640 					del = true;
12641 				}
12642 			} else {
12643 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
12644 								       fltr);
12645 				if (rc)
12646 					del = true;
12647 				else
12648 					set_bit(BNXT_FLTR_VALID, &fltr->state);
12649 			}
12650 
12651 			if (del) {
12652 				spin_lock_bh(&bp->ntp_fltr_lock);
12653 				hlist_del_rcu(&fltr->hash);
12654 				bp->ntp_fltr_count--;
12655 				spin_unlock_bh(&bp->ntp_fltr_lock);
12656 				synchronize_rcu();
12657 				clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
12658 				kfree(fltr);
12659 			}
12660 		}
12661 	}
12662 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
12663 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
12664 }
12665 
12666 #else
12667 
12668 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
12669 {
12670 }
12671 
12672 #endif /* CONFIG_RFS_ACCEL */
12673 
12674 static int bnxt_udp_tunnel_sync(struct net_device *netdev, unsigned int table)
12675 {
12676 	struct bnxt *bp = netdev_priv(netdev);
12677 	struct udp_tunnel_info ti;
12678 	unsigned int cmd;
12679 
12680 	udp_tunnel_nic_get_port(netdev, table, 0, &ti);
12681 	if (ti.type == UDP_TUNNEL_TYPE_VXLAN) {
12682 		bp->vxlan_port = ti.port;
12683 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
12684 	} else {
12685 		bp->nge_port = ti.port;
12686 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
12687 	}
12688 
12689 	if (ti.port)
12690 		return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti.port, cmd);
12691 
12692 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
12693 }
12694 
12695 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
12696 	.sync_table	= bnxt_udp_tunnel_sync,
12697 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
12698 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
12699 	.tables		= {
12700 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
12701 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
12702 	},
12703 };
12704 
12705 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
12706 			       struct net_device *dev, u32 filter_mask,
12707 			       int nlflags)
12708 {
12709 	struct bnxt *bp = netdev_priv(dev);
12710 
12711 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
12712 				       nlflags, filter_mask, NULL);
12713 }
12714 
12715 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
12716 			       u16 flags, struct netlink_ext_ack *extack)
12717 {
12718 	struct bnxt *bp = netdev_priv(dev);
12719 	struct nlattr *attr, *br_spec;
12720 	int rem, rc = 0;
12721 
12722 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
12723 		return -EOPNOTSUPP;
12724 
12725 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
12726 	if (!br_spec)
12727 		return -EINVAL;
12728 
12729 	nla_for_each_nested(attr, br_spec, rem) {
12730 		u16 mode;
12731 
12732 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
12733 			continue;
12734 
12735 		if (nla_len(attr) < sizeof(mode))
12736 			return -EINVAL;
12737 
12738 		mode = nla_get_u16(attr);
12739 		if (mode == bp->br_mode)
12740 			break;
12741 
12742 		rc = bnxt_hwrm_set_br_mode(bp, mode);
12743 		if (!rc)
12744 			bp->br_mode = mode;
12745 		break;
12746 	}
12747 	return rc;
12748 }
12749 
12750 int bnxt_get_port_parent_id(struct net_device *dev,
12751 			    struct netdev_phys_item_id *ppid)
12752 {
12753 	struct bnxt *bp = netdev_priv(dev);
12754 
12755 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
12756 		return -EOPNOTSUPP;
12757 
12758 	/* The PF and it's VF-reps only support the switchdev framework */
12759 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
12760 		return -EOPNOTSUPP;
12761 
12762 	ppid->id_len = sizeof(bp->dsn);
12763 	memcpy(ppid->id, bp->dsn, ppid->id_len);
12764 
12765 	return 0;
12766 }
12767 
12768 static struct devlink_port *bnxt_get_devlink_port(struct net_device *dev)
12769 {
12770 	struct bnxt *bp = netdev_priv(dev);
12771 
12772 	return &bp->dl_port;
12773 }
12774 
12775 static const struct net_device_ops bnxt_netdev_ops = {
12776 	.ndo_open		= bnxt_open,
12777 	.ndo_start_xmit		= bnxt_start_xmit,
12778 	.ndo_stop		= bnxt_close,
12779 	.ndo_get_stats64	= bnxt_get_stats64,
12780 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
12781 	.ndo_eth_ioctl		= bnxt_ioctl,
12782 	.ndo_validate_addr	= eth_validate_addr,
12783 	.ndo_set_mac_address	= bnxt_change_mac_addr,
12784 	.ndo_change_mtu		= bnxt_change_mtu,
12785 	.ndo_fix_features	= bnxt_fix_features,
12786 	.ndo_set_features	= bnxt_set_features,
12787 	.ndo_features_check	= bnxt_features_check,
12788 	.ndo_tx_timeout		= bnxt_tx_timeout,
12789 #ifdef CONFIG_BNXT_SRIOV
12790 	.ndo_get_vf_config	= bnxt_get_vf_config,
12791 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
12792 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
12793 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
12794 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
12795 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
12796 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
12797 #endif
12798 	.ndo_setup_tc           = bnxt_setup_tc,
12799 #ifdef CONFIG_RFS_ACCEL
12800 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
12801 #endif
12802 	.ndo_bpf		= bnxt_xdp,
12803 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
12804 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
12805 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
12806 	.ndo_get_devlink_port	= bnxt_get_devlink_port,
12807 };
12808 
12809 static void bnxt_remove_one(struct pci_dev *pdev)
12810 {
12811 	struct net_device *dev = pci_get_drvdata(pdev);
12812 	struct bnxt *bp = netdev_priv(dev);
12813 
12814 	if (BNXT_PF(bp))
12815 		bnxt_sriov_disable(bp);
12816 
12817 	if (BNXT_PF(bp))
12818 		devlink_port_type_clear(&bp->dl_port);
12819 
12820 	bnxt_ptp_clear(bp);
12821 	pci_disable_pcie_error_reporting(pdev);
12822 	unregister_netdev(dev);
12823 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
12824 	/* Flush any pending tasks */
12825 	cancel_work_sync(&bp->sp_task);
12826 	cancel_delayed_work_sync(&bp->fw_reset_task);
12827 	bp->sp_event = 0;
12828 
12829 	bnxt_dl_fw_reporters_destroy(bp, true);
12830 	bnxt_dl_unregister(bp);
12831 	bnxt_shutdown_tc(bp);
12832 
12833 	bnxt_clear_int_mode(bp);
12834 	bnxt_hwrm_func_drv_unrgtr(bp);
12835 	bnxt_free_hwrm_resources(bp);
12836 	bnxt_free_hwrm_short_cmd_req(bp);
12837 	bnxt_ethtool_free(bp);
12838 	bnxt_dcb_free(bp);
12839 	kfree(bp->edev);
12840 	bp->edev = NULL;
12841 	kfree(bp->ptp_cfg);
12842 	bp->ptp_cfg = NULL;
12843 	kfree(bp->fw_health);
12844 	bp->fw_health = NULL;
12845 	bnxt_cleanup_pci(bp);
12846 	bnxt_free_ctx_mem(bp);
12847 	kfree(bp->ctx);
12848 	bp->ctx = NULL;
12849 	kfree(bp->rss_indir_tbl);
12850 	bp->rss_indir_tbl = NULL;
12851 	bnxt_free_port_stats(bp);
12852 	free_netdev(dev);
12853 }
12854 
12855 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
12856 {
12857 	int rc = 0;
12858 	struct bnxt_link_info *link_info = &bp->link_info;
12859 
12860 	bp->phy_flags = 0;
12861 	rc = bnxt_hwrm_phy_qcaps(bp);
12862 	if (rc) {
12863 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
12864 			   rc);
12865 		return rc;
12866 	}
12867 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
12868 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
12869 	else
12870 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
12871 	if (!fw_dflt)
12872 		return 0;
12873 
12874 	rc = bnxt_update_link(bp, false);
12875 	if (rc) {
12876 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
12877 			   rc);
12878 		return rc;
12879 	}
12880 
12881 	/* Older firmware does not have supported_auto_speeds, so assume
12882 	 * that all supported speeds can be autonegotiated.
12883 	 */
12884 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
12885 		link_info->support_auto_speeds = link_info->support_speeds;
12886 
12887 	bnxt_init_ethtool_link_settings(bp);
12888 	return 0;
12889 }
12890 
12891 static int bnxt_get_max_irq(struct pci_dev *pdev)
12892 {
12893 	u16 ctrl;
12894 
12895 	if (!pdev->msix_cap)
12896 		return 1;
12897 
12898 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
12899 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
12900 }
12901 
12902 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
12903 				int *max_cp)
12904 {
12905 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12906 	int max_ring_grps = 0, max_irq;
12907 
12908 	*max_tx = hw_resc->max_tx_rings;
12909 	*max_rx = hw_resc->max_rx_rings;
12910 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
12911 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
12912 			bnxt_get_ulp_msix_num(bp),
12913 			hw_resc->max_stat_ctxs - bnxt_get_ulp_stat_ctxs(bp));
12914 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
12915 		*max_cp = min_t(int, *max_cp, max_irq);
12916 	max_ring_grps = hw_resc->max_hw_ring_grps;
12917 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
12918 		*max_cp -= 1;
12919 		*max_rx -= 2;
12920 	}
12921 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
12922 		*max_rx >>= 1;
12923 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
12924 		bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
12925 		/* On P5 chips, max_cp output param should be available NQs */
12926 		*max_cp = max_irq;
12927 	}
12928 	*max_rx = min_t(int, *max_rx, max_ring_grps);
12929 }
12930 
12931 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
12932 {
12933 	int rx, tx, cp;
12934 
12935 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
12936 	*max_rx = rx;
12937 	*max_tx = tx;
12938 	if (!rx || !tx || !cp)
12939 		return -ENOMEM;
12940 
12941 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
12942 }
12943 
12944 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
12945 			       bool shared)
12946 {
12947 	int rc;
12948 
12949 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
12950 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
12951 		/* Not enough rings, try disabling agg rings. */
12952 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
12953 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
12954 		if (rc) {
12955 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
12956 			bp->flags |= BNXT_FLAG_AGG_RINGS;
12957 			return rc;
12958 		}
12959 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
12960 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
12961 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
12962 		bnxt_set_ring_params(bp);
12963 	}
12964 
12965 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
12966 		int max_cp, max_stat, max_irq;
12967 
12968 		/* Reserve minimum resources for RoCE */
12969 		max_cp = bnxt_get_max_func_cp_rings(bp);
12970 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
12971 		max_irq = bnxt_get_max_func_irqs(bp);
12972 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
12973 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
12974 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
12975 			return 0;
12976 
12977 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
12978 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
12979 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
12980 		max_cp = min_t(int, max_cp, max_irq);
12981 		max_cp = min_t(int, max_cp, max_stat);
12982 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
12983 		if (rc)
12984 			rc = 0;
12985 	}
12986 	return rc;
12987 }
12988 
12989 /* In initial default shared ring setting, each shared ring must have a
12990  * RX/TX ring pair.
12991  */
12992 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
12993 {
12994 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
12995 	bp->rx_nr_rings = bp->cp_nr_rings;
12996 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
12997 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
12998 }
12999 
13000 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
13001 {
13002 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
13003 
13004 	if (!bnxt_can_reserve_rings(bp))
13005 		return 0;
13006 
13007 	if (sh)
13008 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
13009 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
13010 	/* Reduce default rings on multi-port cards so that total default
13011 	 * rings do not exceed CPU count.
13012 	 */
13013 	if (bp->port_count > 1) {
13014 		int max_rings =
13015 			max_t(int, num_online_cpus() / bp->port_count, 1);
13016 
13017 		dflt_rings = min_t(int, dflt_rings, max_rings);
13018 	}
13019 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
13020 	if (rc)
13021 		return rc;
13022 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
13023 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
13024 	if (sh)
13025 		bnxt_trim_dflt_sh_rings(bp);
13026 	else
13027 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
13028 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
13029 
13030 	rc = __bnxt_reserve_rings(bp);
13031 	if (rc)
13032 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
13033 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
13034 	if (sh)
13035 		bnxt_trim_dflt_sh_rings(bp);
13036 
13037 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
13038 	if (bnxt_need_reserve_rings(bp)) {
13039 		rc = __bnxt_reserve_rings(bp);
13040 		if (rc)
13041 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
13042 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
13043 	}
13044 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
13045 		bp->rx_nr_rings++;
13046 		bp->cp_nr_rings++;
13047 	}
13048 	if (rc) {
13049 		bp->tx_nr_rings = 0;
13050 		bp->rx_nr_rings = 0;
13051 	}
13052 	return rc;
13053 }
13054 
13055 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
13056 {
13057 	int rc;
13058 
13059 	if (bp->tx_nr_rings)
13060 		return 0;
13061 
13062 	bnxt_ulp_irq_stop(bp);
13063 	bnxt_clear_int_mode(bp);
13064 	rc = bnxt_set_dflt_rings(bp, true);
13065 	if (rc) {
13066 		netdev_err(bp->dev, "Not enough rings available.\n");
13067 		goto init_dflt_ring_err;
13068 	}
13069 	rc = bnxt_init_int_mode(bp);
13070 	if (rc)
13071 		goto init_dflt_ring_err;
13072 
13073 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
13074 	if (bnxt_rfs_supported(bp) && bnxt_rfs_capable(bp)) {
13075 		bp->flags |= BNXT_FLAG_RFS;
13076 		bp->dev->features |= NETIF_F_NTUPLE;
13077 	}
13078 init_dflt_ring_err:
13079 	bnxt_ulp_irq_restart(bp, rc);
13080 	return rc;
13081 }
13082 
13083 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
13084 {
13085 	int rc;
13086 
13087 	ASSERT_RTNL();
13088 	bnxt_hwrm_func_qcaps(bp);
13089 
13090 	if (netif_running(bp->dev))
13091 		__bnxt_close_nic(bp, true, false);
13092 
13093 	bnxt_ulp_irq_stop(bp);
13094 	bnxt_clear_int_mode(bp);
13095 	rc = bnxt_init_int_mode(bp);
13096 	bnxt_ulp_irq_restart(bp, rc);
13097 
13098 	if (netif_running(bp->dev)) {
13099 		if (rc)
13100 			dev_close(bp->dev);
13101 		else
13102 			rc = bnxt_open_nic(bp, true, false);
13103 	}
13104 
13105 	return rc;
13106 }
13107 
13108 static int bnxt_init_mac_addr(struct bnxt *bp)
13109 {
13110 	int rc = 0;
13111 
13112 	if (BNXT_PF(bp)) {
13113 		memcpy(bp->dev->dev_addr, bp->pf.mac_addr, ETH_ALEN);
13114 	} else {
13115 #ifdef CONFIG_BNXT_SRIOV
13116 		struct bnxt_vf_info *vf = &bp->vf;
13117 		bool strict_approval = true;
13118 
13119 		if (is_valid_ether_addr(vf->mac_addr)) {
13120 			/* overwrite netdev dev_addr with admin VF MAC */
13121 			memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
13122 			/* Older PF driver or firmware may not approve this
13123 			 * correctly.
13124 			 */
13125 			strict_approval = false;
13126 		} else {
13127 			eth_hw_addr_random(bp->dev);
13128 		}
13129 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
13130 #endif
13131 	}
13132 	return rc;
13133 }
13134 
13135 #define BNXT_VPD_LEN	512
13136 static void bnxt_vpd_read_info(struct bnxt *bp)
13137 {
13138 	struct pci_dev *pdev = bp->pdev;
13139 	int i, len, pos, ro_size, size;
13140 	ssize_t vpd_size;
13141 	u8 *vpd_data;
13142 
13143 	vpd_data = kmalloc(BNXT_VPD_LEN, GFP_KERNEL);
13144 	if (!vpd_data)
13145 		return;
13146 
13147 	vpd_size = pci_read_vpd(pdev, 0, BNXT_VPD_LEN, vpd_data);
13148 	if (vpd_size <= 0) {
13149 		netdev_err(bp->dev, "Unable to read VPD\n");
13150 		goto exit;
13151 	}
13152 
13153 	i = pci_vpd_find_tag(vpd_data, vpd_size, PCI_VPD_LRDT_RO_DATA);
13154 	if (i < 0) {
13155 		netdev_err(bp->dev, "VPD READ-Only not found\n");
13156 		goto exit;
13157 	}
13158 
13159 	ro_size = pci_vpd_lrdt_size(&vpd_data[i]);
13160 	i += PCI_VPD_LRDT_TAG_SIZE;
13161 	if (i + ro_size > vpd_size)
13162 		goto exit;
13163 
13164 	pos = pci_vpd_find_info_keyword(vpd_data, i, ro_size,
13165 					PCI_VPD_RO_KEYWORD_PARTNO);
13166 	if (pos < 0)
13167 		goto read_sn;
13168 
13169 	len = pci_vpd_info_field_size(&vpd_data[pos]);
13170 	pos += PCI_VPD_INFO_FLD_HDR_SIZE;
13171 	if (len + pos > vpd_size)
13172 		goto read_sn;
13173 
13174 	size = min(len, BNXT_VPD_FLD_LEN - 1);
13175 	memcpy(bp->board_partno, &vpd_data[pos], size);
13176 
13177 read_sn:
13178 	pos = pci_vpd_find_info_keyword(vpd_data, i, ro_size,
13179 					PCI_VPD_RO_KEYWORD_SERIALNO);
13180 	if (pos < 0)
13181 		goto exit;
13182 
13183 	len = pci_vpd_info_field_size(&vpd_data[pos]);
13184 	pos += PCI_VPD_INFO_FLD_HDR_SIZE;
13185 	if (len + pos > vpd_size)
13186 		goto exit;
13187 
13188 	size = min(len, BNXT_VPD_FLD_LEN - 1);
13189 	memcpy(bp->board_serialno, &vpd_data[pos], size);
13190 exit:
13191 	kfree(vpd_data);
13192 }
13193 
13194 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
13195 {
13196 	struct pci_dev *pdev = bp->pdev;
13197 	u64 qword;
13198 
13199 	qword = pci_get_dsn(pdev);
13200 	if (!qword) {
13201 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
13202 		return -EOPNOTSUPP;
13203 	}
13204 
13205 	put_unaligned_le64(qword, dsn);
13206 
13207 	bp->flags |= BNXT_FLAG_DSN_VALID;
13208 	return 0;
13209 }
13210 
13211 static int bnxt_map_db_bar(struct bnxt *bp)
13212 {
13213 	if (!bp->db_size)
13214 		return -ENODEV;
13215 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
13216 	if (!bp->bar1)
13217 		return -ENOMEM;
13218 	return 0;
13219 }
13220 
13221 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
13222 {
13223 	struct net_device *dev;
13224 	struct bnxt *bp;
13225 	int rc, max_irqs;
13226 
13227 	if (pci_is_bridge(pdev))
13228 		return -ENODEV;
13229 
13230 	/* Clear any pending DMA transactions from crash kernel
13231 	 * while loading driver in capture kernel.
13232 	 */
13233 	if (is_kdump_kernel()) {
13234 		pci_clear_master(pdev);
13235 		pcie_flr(pdev);
13236 	}
13237 
13238 	max_irqs = bnxt_get_max_irq(pdev);
13239 	dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
13240 	if (!dev)
13241 		return -ENOMEM;
13242 
13243 	bp = netdev_priv(dev);
13244 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
13245 	bnxt_set_max_func_irqs(bp, max_irqs);
13246 
13247 	if (bnxt_vf_pciid(ent->driver_data))
13248 		bp->flags |= BNXT_FLAG_VF;
13249 
13250 	if (pdev->msix_cap)
13251 		bp->flags |= BNXT_FLAG_MSIX_CAP;
13252 
13253 	rc = bnxt_init_board(pdev, dev);
13254 	if (rc < 0)
13255 		goto init_err_free;
13256 
13257 	dev->netdev_ops = &bnxt_netdev_ops;
13258 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
13259 	dev->ethtool_ops = &bnxt_ethtool_ops;
13260 	pci_set_drvdata(pdev, dev);
13261 
13262 	rc = bnxt_alloc_hwrm_resources(bp);
13263 	if (rc)
13264 		goto init_err_pci_clean;
13265 
13266 	mutex_init(&bp->hwrm_cmd_lock);
13267 	mutex_init(&bp->link_lock);
13268 
13269 	rc = bnxt_fw_init_one_p1(bp);
13270 	if (rc)
13271 		goto init_err_pci_clean;
13272 
13273 	if (BNXT_PF(bp))
13274 		bnxt_vpd_read_info(bp);
13275 
13276 	if (BNXT_CHIP_P5(bp)) {
13277 		bp->flags |= BNXT_FLAG_CHIP_P5;
13278 		if (BNXT_CHIP_SR2(bp))
13279 			bp->flags |= BNXT_FLAG_CHIP_SR2;
13280 	}
13281 
13282 	rc = bnxt_alloc_rss_indir_tbl(bp);
13283 	if (rc)
13284 		goto init_err_pci_clean;
13285 
13286 	rc = bnxt_fw_init_one_p2(bp);
13287 	if (rc)
13288 		goto init_err_pci_clean;
13289 
13290 	rc = bnxt_map_db_bar(bp);
13291 	if (rc) {
13292 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
13293 			rc);
13294 		goto init_err_pci_clean;
13295 	}
13296 
13297 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
13298 			   NETIF_F_TSO | NETIF_F_TSO6 |
13299 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
13300 			   NETIF_F_GSO_IPXIP4 |
13301 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
13302 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
13303 			   NETIF_F_RXCSUM | NETIF_F_GRO;
13304 
13305 	if (BNXT_SUPPORTS_TPA(bp))
13306 		dev->hw_features |= NETIF_F_LRO;
13307 
13308 	dev->hw_enc_features =
13309 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
13310 			NETIF_F_TSO | NETIF_F_TSO6 |
13311 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
13312 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
13313 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
13314 	dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
13315 
13316 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
13317 				    NETIF_F_GSO_GRE_CSUM;
13318 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
13319 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
13320 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13321 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
13322 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
13323 	if (BNXT_SUPPORTS_TPA(bp))
13324 		dev->hw_features |= NETIF_F_GRO_HW;
13325 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
13326 	if (dev->features & NETIF_F_GRO_HW)
13327 		dev->features &= ~NETIF_F_LRO;
13328 	dev->priv_flags |= IFF_UNICAST_FLT;
13329 
13330 #ifdef CONFIG_BNXT_SRIOV
13331 	init_waitqueue_head(&bp->sriov_cfg_wait);
13332 	mutex_init(&bp->sriov_lock);
13333 #endif
13334 	if (BNXT_SUPPORTS_TPA(bp)) {
13335 		bp->gro_func = bnxt_gro_func_5730x;
13336 		if (BNXT_CHIP_P4(bp))
13337 			bp->gro_func = bnxt_gro_func_5731x;
13338 		else if (BNXT_CHIP_P5(bp))
13339 			bp->gro_func = bnxt_gro_func_5750x;
13340 	}
13341 	if (!BNXT_CHIP_P4_PLUS(bp))
13342 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
13343 
13344 	rc = bnxt_init_mac_addr(bp);
13345 	if (rc) {
13346 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
13347 		rc = -EADDRNOTAVAIL;
13348 		goto init_err_pci_clean;
13349 	}
13350 
13351 	if (BNXT_PF(bp)) {
13352 		/* Read the adapter's DSN to use as the eswitch switch_id */
13353 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
13354 	}
13355 
13356 	/* MTU range: 60 - FW defined max */
13357 	dev->min_mtu = ETH_ZLEN;
13358 	dev->max_mtu = bp->max_mtu;
13359 
13360 	rc = bnxt_probe_phy(bp, true);
13361 	if (rc)
13362 		goto init_err_pci_clean;
13363 
13364 	bnxt_set_rx_skb_mode(bp, false);
13365 	bnxt_set_tpa_flags(bp);
13366 	bnxt_set_ring_params(bp);
13367 	rc = bnxt_set_dflt_rings(bp, true);
13368 	if (rc) {
13369 		netdev_err(bp->dev, "Not enough rings available.\n");
13370 		rc = -ENOMEM;
13371 		goto init_err_pci_clean;
13372 	}
13373 
13374 	bnxt_fw_init_one_p3(bp);
13375 
13376 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13377 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
13378 
13379 	rc = bnxt_init_int_mode(bp);
13380 	if (rc)
13381 		goto init_err_pci_clean;
13382 
13383 	/* No TC has been set yet and rings may have been trimmed due to
13384 	 * limited MSIX, so we re-initialize the TX rings per TC.
13385 	 */
13386 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
13387 
13388 	if (BNXT_PF(bp)) {
13389 		if (!bnxt_pf_wq) {
13390 			bnxt_pf_wq =
13391 				create_singlethread_workqueue("bnxt_pf_wq");
13392 			if (!bnxt_pf_wq) {
13393 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
13394 				rc = -ENOMEM;
13395 				goto init_err_pci_clean;
13396 			}
13397 		}
13398 		rc = bnxt_init_tc(bp);
13399 		if (rc)
13400 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
13401 				   rc);
13402 	}
13403 
13404 	bnxt_inv_fw_health_reg(bp);
13405 	bnxt_dl_register(bp);
13406 
13407 	rc = register_netdev(dev);
13408 	if (rc)
13409 		goto init_err_cleanup;
13410 
13411 	if (BNXT_PF(bp))
13412 		devlink_port_type_eth_set(&bp->dl_port, bp->dev);
13413 	bnxt_dl_fw_reporters_create(bp);
13414 
13415 	netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
13416 		    board_info[ent->driver_data].name,
13417 		    (long)pci_resource_start(pdev, 0), dev->dev_addr);
13418 	pcie_print_link_status(pdev);
13419 
13420 	pci_save_state(pdev);
13421 	return 0;
13422 
13423 init_err_cleanup:
13424 	bnxt_dl_unregister(bp);
13425 	bnxt_shutdown_tc(bp);
13426 	bnxt_clear_int_mode(bp);
13427 
13428 init_err_pci_clean:
13429 	bnxt_hwrm_func_drv_unrgtr(bp);
13430 	bnxt_free_hwrm_short_cmd_req(bp);
13431 	bnxt_free_hwrm_resources(bp);
13432 	bnxt_ethtool_free(bp);
13433 	bnxt_ptp_clear(bp);
13434 	kfree(bp->ptp_cfg);
13435 	bp->ptp_cfg = NULL;
13436 	kfree(bp->fw_health);
13437 	bp->fw_health = NULL;
13438 	bnxt_cleanup_pci(bp);
13439 	bnxt_free_ctx_mem(bp);
13440 	kfree(bp->ctx);
13441 	bp->ctx = NULL;
13442 	kfree(bp->rss_indir_tbl);
13443 	bp->rss_indir_tbl = NULL;
13444 
13445 init_err_free:
13446 	free_netdev(dev);
13447 	return rc;
13448 }
13449 
13450 static void bnxt_shutdown(struct pci_dev *pdev)
13451 {
13452 	struct net_device *dev = pci_get_drvdata(pdev);
13453 	struct bnxt *bp;
13454 
13455 	if (!dev)
13456 		return;
13457 
13458 	rtnl_lock();
13459 	bp = netdev_priv(dev);
13460 	if (!bp)
13461 		goto shutdown_exit;
13462 
13463 	if (netif_running(dev))
13464 		dev_close(dev);
13465 
13466 	bnxt_ulp_shutdown(bp);
13467 	bnxt_clear_int_mode(bp);
13468 	pci_disable_device(pdev);
13469 
13470 	if (system_state == SYSTEM_POWER_OFF) {
13471 		pci_wake_from_d3(pdev, bp->wol);
13472 		pci_set_power_state(pdev, PCI_D3hot);
13473 	}
13474 
13475 shutdown_exit:
13476 	rtnl_unlock();
13477 }
13478 
13479 #ifdef CONFIG_PM_SLEEP
13480 static int bnxt_suspend(struct device *device)
13481 {
13482 	struct net_device *dev = dev_get_drvdata(device);
13483 	struct bnxt *bp = netdev_priv(dev);
13484 	int rc = 0;
13485 
13486 	rtnl_lock();
13487 	bnxt_ulp_stop(bp);
13488 	if (netif_running(dev)) {
13489 		netif_device_detach(dev);
13490 		rc = bnxt_close(dev);
13491 	}
13492 	bnxt_hwrm_func_drv_unrgtr(bp);
13493 	pci_disable_device(bp->pdev);
13494 	bnxt_free_ctx_mem(bp);
13495 	kfree(bp->ctx);
13496 	bp->ctx = NULL;
13497 	rtnl_unlock();
13498 	return rc;
13499 }
13500 
13501 static int bnxt_resume(struct device *device)
13502 {
13503 	struct net_device *dev = dev_get_drvdata(device);
13504 	struct bnxt *bp = netdev_priv(dev);
13505 	int rc = 0;
13506 
13507 	rtnl_lock();
13508 	rc = pci_enable_device(bp->pdev);
13509 	if (rc) {
13510 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
13511 			   rc);
13512 		goto resume_exit;
13513 	}
13514 	pci_set_master(bp->pdev);
13515 	if (bnxt_hwrm_ver_get(bp)) {
13516 		rc = -ENODEV;
13517 		goto resume_exit;
13518 	}
13519 	rc = bnxt_hwrm_func_reset(bp);
13520 	if (rc) {
13521 		rc = -EBUSY;
13522 		goto resume_exit;
13523 	}
13524 
13525 	rc = bnxt_hwrm_func_qcaps(bp);
13526 	if (rc)
13527 		goto resume_exit;
13528 
13529 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
13530 		rc = -ENODEV;
13531 		goto resume_exit;
13532 	}
13533 
13534 	bnxt_get_wol_settings(bp);
13535 	if (netif_running(dev)) {
13536 		rc = bnxt_open(dev);
13537 		if (!rc)
13538 			netif_device_attach(dev);
13539 	}
13540 
13541 resume_exit:
13542 	bnxt_ulp_start(bp, rc);
13543 	if (!rc)
13544 		bnxt_reenable_sriov(bp);
13545 	rtnl_unlock();
13546 	return rc;
13547 }
13548 
13549 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
13550 #define BNXT_PM_OPS (&bnxt_pm_ops)
13551 
13552 #else
13553 
13554 #define BNXT_PM_OPS NULL
13555 
13556 #endif /* CONFIG_PM_SLEEP */
13557 
13558 /**
13559  * bnxt_io_error_detected - called when PCI error is detected
13560  * @pdev: Pointer to PCI device
13561  * @state: The current pci connection state
13562  *
13563  * This function is called after a PCI bus error affecting
13564  * this device has been detected.
13565  */
13566 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
13567 					       pci_channel_state_t state)
13568 {
13569 	struct net_device *netdev = pci_get_drvdata(pdev);
13570 	struct bnxt *bp = netdev_priv(netdev);
13571 
13572 	netdev_info(netdev, "PCI I/O error detected\n");
13573 
13574 	rtnl_lock();
13575 	netif_device_detach(netdev);
13576 
13577 	bnxt_ulp_stop(bp);
13578 
13579 	if (state == pci_channel_io_perm_failure) {
13580 		rtnl_unlock();
13581 		return PCI_ERS_RESULT_DISCONNECT;
13582 	}
13583 
13584 	if (state == pci_channel_io_frozen)
13585 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
13586 
13587 	if (netif_running(netdev))
13588 		bnxt_close(netdev);
13589 
13590 	if (pci_is_enabled(pdev))
13591 		pci_disable_device(pdev);
13592 	bnxt_free_ctx_mem(bp);
13593 	kfree(bp->ctx);
13594 	bp->ctx = NULL;
13595 	rtnl_unlock();
13596 
13597 	/* Request a slot slot reset. */
13598 	return PCI_ERS_RESULT_NEED_RESET;
13599 }
13600 
13601 /**
13602  * bnxt_io_slot_reset - called after the pci bus has been reset.
13603  * @pdev: Pointer to PCI device
13604  *
13605  * Restart the card from scratch, as if from a cold-boot.
13606  * At this point, the card has exprienced a hard reset,
13607  * followed by fixups by BIOS, and has its config space
13608  * set up identically to what it was at cold boot.
13609  */
13610 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
13611 {
13612 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
13613 	struct net_device *netdev = pci_get_drvdata(pdev);
13614 	struct bnxt *bp = netdev_priv(netdev);
13615 	int err = 0, off;
13616 
13617 	netdev_info(bp->dev, "PCI Slot Reset\n");
13618 
13619 	rtnl_lock();
13620 
13621 	if (pci_enable_device(pdev)) {
13622 		dev_err(&pdev->dev,
13623 			"Cannot re-enable PCI device after reset.\n");
13624 	} else {
13625 		pci_set_master(pdev);
13626 		/* Upon fatal error, our device internal logic that latches to
13627 		 * BAR value is getting reset and will restore only upon
13628 		 * rewritting the BARs.
13629 		 *
13630 		 * As pci_restore_state() does not re-write the BARs if the
13631 		 * value is same as saved value earlier, driver needs to
13632 		 * write the BARs to 0 to force restore, in case of fatal error.
13633 		 */
13634 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
13635 				       &bp->state)) {
13636 			for (off = PCI_BASE_ADDRESS_0;
13637 			     off <= PCI_BASE_ADDRESS_5; off += 4)
13638 				pci_write_config_dword(bp->pdev, off, 0);
13639 		}
13640 		pci_restore_state(pdev);
13641 		pci_save_state(pdev);
13642 
13643 		err = bnxt_hwrm_func_reset(bp);
13644 		if (!err)
13645 			result = PCI_ERS_RESULT_RECOVERED;
13646 	}
13647 
13648 	rtnl_unlock();
13649 
13650 	return result;
13651 }
13652 
13653 /**
13654  * bnxt_io_resume - called when traffic can start flowing again.
13655  * @pdev: Pointer to PCI device
13656  *
13657  * This callback is called when the error recovery driver tells
13658  * us that its OK to resume normal operation.
13659  */
13660 static void bnxt_io_resume(struct pci_dev *pdev)
13661 {
13662 	struct net_device *netdev = pci_get_drvdata(pdev);
13663 	struct bnxt *bp = netdev_priv(netdev);
13664 	int err;
13665 
13666 	netdev_info(bp->dev, "PCI Slot Resume\n");
13667 	rtnl_lock();
13668 
13669 	err = bnxt_hwrm_func_qcaps(bp);
13670 	if (!err && netif_running(netdev))
13671 		err = bnxt_open(netdev);
13672 
13673 	bnxt_ulp_start(bp, err);
13674 	if (!err) {
13675 		bnxt_reenable_sriov(bp);
13676 		netif_device_attach(netdev);
13677 	}
13678 
13679 	rtnl_unlock();
13680 }
13681 
13682 static const struct pci_error_handlers bnxt_err_handler = {
13683 	.error_detected	= bnxt_io_error_detected,
13684 	.slot_reset	= bnxt_io_slot_reset,
13685 	.resume		= bnxt_io_resume
13686 };
13687 
13688 static struct pci_driver bnxt_pci_driver = {
13689 	.name		= DRV_MODULE_NAME,
13690 	.id_table	= bnxt_pci_tbl,
13691 	.probe		= bnxt_init_one,
13692 	.remove		= bnxt_remove_one,
13693 	.shutdown	= bnxt_shutdown,
13694 	.driver.pm	= BNXT_PM_OPS,
13695 	.err_handler	= &bnxt_err_handler,
13696 #if defined(CONFIG_BNXT_SRIOV)
13697 	.sriov_configure = bnxt_sriov_configure,
13698 #endif
13699 };
13700 
13701 static int __init bnxt_init(void)
13702 {
13703 	bnxt_debug_init();
13704 	return pci_register_driver(&bnxt_pci_driver);
13705 }
13706 
13707 static void __exit bnxt_exit(void)
13708 {
13709 	pci_unregister_driver(&bnxt_pci_driver);
13710 	if (bnxt_pf_wq)
13711 		destroy_workqueue(bnxt_pf_wq);
13712 	bnxt_debug_exit();
13713 }
13714 
13715 module_init(bnxt_init);
13716 module_exit(bnxt_exit);
13717