xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision b86761ff6374813cdf64ffd6b95ddd1813c435d8)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  * Copyright (c) 2016-2019 Broadcom Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  */
10 
11 #include <linux/module.h>
12 
13 #include <linux/stringify.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/pci.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/bitops.h>
27 #include <linux/io.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/byteorder.h>
31 #include <asm/page.h>
32 #include <linux/time.h>
33 #include <linux/mii.h>
34 #include <linux/mdio.h>
35 #include <linux/if.h>
36 #include <linux/if_vlan.h>
37 #include <linux/if_bridge.h>
38 #include <linux/rtc.h>
39 #include <linux/bpf.h>
40 #include <net/gro.h>
41 #include <net/ip.h>
42 #include <net/tcp.h>
43 #include <net/udp.h>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
46 #include <net/udp_tunnel.h>
47 #include <linux/workqueue.h>
48 #include <linux/prefetch.h>
49 #include <linux/cache.h>
50 #include <linux/log2.h>
51 #include <linux/bitmap.h>
52 #include <linux/cpu_rmap.h>
53 #include <linux/cpumask.h>
54 #include <net/pkt_cls.h>
55 #include <net/page_pool/helpers.h>
56 #include <linux/align.h>
57 #include <net/netdev_queues.h>
58 
59 #include "bnxt_hsi.h"
60 #include "bnxt.h"
61 #include "bnxt_hwrm.h"
62 #include "bnxt_ulp.h"
63 #include "bnxt_sriov.h"
64 #include "bnxt_ethtool.h"
65 #include "bnxt_dcb.h"
66 #include "bnxt_xdp.h"
67 #include "bnxt_ptp.h"
68 #include "bnxt_vfr.h"
69 #include "bnxt_tc.h"
70 #include "bnxt_devlink.h"
71 #include "bnxt_debugfs.h"
72 #include "bnxt_hwmon.h"
73 
74 #define BNXT_TX_TIMEOUT		(5 * HZ)
75 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
76 				 NETIF_MSG_TX_ERR)
77 
78 MODULE_LICENSE("GPL");
79 MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
80 
81 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
82 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
83 #define BNXT_RX_COPY_THRESH 256
84 
85 #define BNXT_TX_PUSH_THRESH 164
86 
87 /* indexed by enum board_idx */
88 static const struct {
89 	char *name;
90 } board_info[] = {
91 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
92 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
93 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
94 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
95 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
96 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
97 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
98 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
99 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
100 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
101 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
102 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
103 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
104 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
105 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
106 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
108 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
109 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
110 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
111 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
112 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
113 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
114 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
115 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
116 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
117 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
118 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
119 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
120 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
121 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
122 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
123 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
124 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
125 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
126 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
127 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
128 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
129 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
130 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
131 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
132 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
133 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
134 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
135 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
136 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
137 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
138 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
139 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
140 };
141 
142 static const struct pci_device_id bnxt_pci_tbl[] = {
143 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
144 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
145 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
146 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
147 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
148 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
149 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
150 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
151 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
152 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
153 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
155 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
158 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
159 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
160 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
164 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
165 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
166 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
167 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
168 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
169 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
170 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
171 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
172 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
173 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
174 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
175 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
176 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
177 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
178 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
179 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
180 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
181 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
182 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
183 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
184 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
185 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
186 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
187 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
188 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
189 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
190 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
191 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
192 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
193 #ifdef CONFIG_BNXT_SRIOV
194 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
195 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
196 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
197 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
198 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
199 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
200 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
201 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
202 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
203 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
204 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
205 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
206 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
207 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
208 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
209 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
210 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
211 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
212 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
213 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
214 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
215 #endif
216 	{ 0 }
217 };
218 
219 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
220 
221 static const u16 bnxt_vf_req_snif[] = {
222 	HWRM_FUNC_CFG,
223 	HWRM_FUNC_VF_CFG,
224 	HWRM_PORT_PHY_QCFG,
225 	HWRM_CFA_L2_FILTER_ALLOC,
226 };
227 
228 static const u16 bnxt_async_events_arr[] = {
229 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
230 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
231 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
232 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
233 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
234 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
235 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
236 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
237 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
238 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
239 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
240 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
241 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
242 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
243 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
244 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
245 };
246 
247 static struct workqueue_struct *bnxt_pf_wq;
248 
249 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
250 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
251 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
252 
253 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
254 	.ports = {
255 		.src = 0,
256 		.dst = 0,
257 	},
258 	.addrs = {
259 		.v6addrs = {
260 			.src = BNXT_IPV6_MASK_NONE,
261 			.dst = BNXT_IPV6_MASK_NONE,
262 		},
263 	},
264 };
265 
266 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
267 	.ports = {
268 		.src = cpu_to_be16(0xffff),
269 		.dst = cpu_to_be16(0xffff),
270 	},
271 	.addrs = {
272 		.v6addrs = {
273 			.src = BNXT_IPV6_MASK_ALL,
274 			.dst = BNXT_IPV6_MASK_ALL,
275 		},
276 	},
277 };
278 
279 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
280 	.ports = {
281 		.src = cpu_to_be16(0xffff),
282 		.dst = cpu_to_be16(0xffff),
283 	},
284 	.addrs = {
285 		.v4addrs = {
286 			.src = cpu_to_be32(0xffffffff),
287 			.dst = cpu_to_be32(0xffffffff),
288 		},
289 	},
290 };
291 
292 static bool bnxt_vf_pciid(enum board_idx idx)
293 {
294 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
295 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
296 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
297 		idx == NETXTREME_E_P5_VF_HV);
298 }
299 
300 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
301 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
302 #define DB_CP_IRQ_DIS_FLAGS	(DB_KEY_CP | DB_IRQ_DIS)
303 
304 #define BNXT_CP_DB_IRQ_DIS(db)						\
305 		writel(DB_CP_IRQ_DIS_FLAGS, db)
306 
307 #define BNXT_DB_CQ(db, idx)						\
308 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
309 
310 #define BNXT_DB_NQ_P5(db, idx)						\
311 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
312 		    (db)->doorbell)
313 
314 #define BNXT_DB_NQ_P7(db, idx)						\
315 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
316 		    DB_RING_IDX(db, idx), (db)->doorbell)
317 
318 #define BNXT_DB_CQ_ARM(db, idx)						\
319 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
320 
321 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
322 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
323 		    DB_RING_IDX(db, idx), (db)->doorbell)
324 
325 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
326 {
327 	if (bp->flags & BNXT_FLAG_CHIP_P7)
328 		BNXT_DB_NQ_P7(db, idx);
329 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
330 		BNXT_DB_NQ_P5(db, idx);
331 	else
332 		BNXT_DB_CQ(db, idx);
333 }
334 
335 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
336 {
337 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
338 		BNXT_DB_NQ_ARM_P5(db, idx);
339 	else
340 		BNXT_DB_CQ_ARM(db, idx);
341 }
342 
343 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
344 {
345 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
346 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
347 			    DB_RING_IDX(db, idx), db->doorbell);
348 	else
349 		BNXT_DB_CQ(db, idx);
350 }
351 
352 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
353 {
354 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
355 		return;
356 
357 	if (BNXT_PF(bp))
358 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
359 	else
360 		schedule_delayed_work(&bp->fw_reset_task, delay);
361 }
362 
363 static void __bnxt_queue_sp_work(struct bnxt *bp)
364 {
365 	if (BNXT_PF(bp))
366 		queue_work(bnxt_pf_wq, &bp->sp_task);
367 	else
368 		schedule_work(&bp->sp_task);
369 }
370 
371 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
372 {
373 	set_bit(event, &bp->sp_event);
374 	__bnxt_queue_sp_work(bp);
375 }
376 
377 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
378 {
379 	if (!rxr->bnapi->in_reset) {
380 		rxr->bnapi->in_reset = true;
381 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
382 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
383 		else
384 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
385 		__bnxt_queue_sp_work(bp);
386 	}
387 	rxr->rx_next_cons = 0xffff;
388 }
389 
390 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
391 			  u16 curr)
392 {
393 	struct bnxt_napi *bnapi = txr->bnapi;
394 
395 	if (bnapi->tx_fault)
396 		return;
397 
398 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
399 		   txr->txq_index, txr->tx_hw_cons,
400 		   txr->tx_cons, txr->tx_prod, curr);
401 	WARN_ON_ONCE(1);
402 	bnapi->tx_fault = 1;
403 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
404 }
405 
406 const u16 bnxt_lhint_arr[] = {
407 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
408 	TX_BD_FLAGS_LHINT_512_TO_1023,
409 	TX_BD_FLAGS_LHINT_1024_TO_2047,
410 	TX_BD_FLAGS_LHINT_1024_TO_2047,
411 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
412 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
413 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
414 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
415 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
416 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
417 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
418 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
419 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
420 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
421 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
422 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
423 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
424 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
425 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
426 };
427 
428 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
429 {
430 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
431 
432 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
433 		return 0;
434 
435 	return md_dst->u.port_info.port_id;
436 }
437 
438 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
439 			     u16 prod)
440 {
441 	/* Sync BD data before updating doorbell */
442 	wmb();
443 	bnxt_db_write(bp, &txr->tx_db, prod);
444 	txr->kick_pending = 0;
445 }
446 
447 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
448 {
449 	struct bnxt *bp = netdev_priv(dev);
450 	struct tx_bd *txbd, *txbd0;
451 	struct tx_bd_ext *txbd1;
452 	struct netdev_queue *txq;
453 	int i;
454 	dma_addr_t mapping;
455 	unsigned int length, pad = 0;
456 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
457 	u16 prod, last_frag;
458 	struct pci_dev *pdev = bp->pdev;
459 	struct bnxt_tx_ring_info *txr;
460 	struct bnxt_sw_tx_bd *tx_buf;
461 	__le32 lflags = 0;
462 
463 	i = skb_get_queue_mapping(skb);
464 	if (unlikely(i >= bp->tx_nr_rings)) {
465 		dev_kfree_skb_any(skb);
466 		dev_core_stats_tx_dropped_inc(dev);
467 		return NETDEV_TX_OK;
468 	}
469 
470 	txq = netdev_get_tx_queue(dev, i);
471 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
472 	prod = txr->tx_prod;
473 
474 	free_size = bnxt_tx_avail(bp, txr);
475 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
476 		/* We must have raced with NAPI cleanup */
477 		if (net_ratelimit() && txr->kick_pending)
478 			netif_warn(bp, tx_err, dev,
479 				   "bnxt: ring busy w/ flush pending!\n");
480 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
481 					bp->tx_wake_thresh))
482 			return NETDEV_TX_BUSY;
483 	}
484 
485 	if (unlikely(ipv6_hopopt_jumbo_remove(skb)))
486 		goto tx_free;
487 
488 	length = skb->len;
489 	len = skb_headlen(skb);
490 	last_frag = skb_shinfo(skb)->nr_frags;
491 
492 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
493 
494 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
495 	tx_buf->skb = skb;
496 	tx_buf->nr_frags = last_frag;
497 
498 	vlan_tag_flags = 0;
499 	cfa_action = bnxt_xmit_get_cfa_action(skb);
500 	if (skb_vlan_tag_present(skb)) {
501 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
502 				 skb_vlan_tag_get(skb);
503 		/* Currently supports 8021Q, 8021AD vlan offloads
504 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
505 		 */
506 		if (skb->vlan_proto == htons(ETH_P_8021Q))
507 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
508 	}
509 
510 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
511 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
512 
513 		if (ptp && ptp->tx_tstamp_en && !skb_is_gso(skb) &&
514 		    atomic_dec_if_positive(&ptp->tx_avail) >= 0) {
515 			if (!bnxt_ptp_parse(skb, &ptp->tx_seqid,
516 					    &ptp->tx_hdr_off)) {
517 				if (vlan_tag_flags)
518 					ptp->tx_hdr_off += VLAN_HLEN;
519 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
520 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
521 			} else {
522 				atomic_inc(&bp->ptp_cfg->tx_avail);
523 			}
524 		}
525 	}
526 
527 	if (unlikely(skb->no_fcs))
528 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
529 
530 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
531 	    !lflags) {
532 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
533 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
534 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
535 		void __iomem *db = txr->tx_db.doorbell;
536 		void *pdata = tx_push_buf->data;
537 		u64 *end;
538 		int j, push_len;
539 
540 		/* Set COAL_NOW to be ready quickly for the next push */
541 		tx_push->tx_bd_len_flags_type =
542 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
543 					TX_BD_TYPE_LONG_TX_BD |
544 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
545 					TX_BD_FLAGS_COAL_NOW |
546 					TX_BD_FLAGS_PACKET_END |
547 					(2 << TX_BD_FLAGS_BD_CNT_SHIFT));
548 
549 		if (skb->ip_summed == CHECKSUM_PARTIAL)
550 			tx_push1->tx_bd_hsize_lflags =
551 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
552 		else
553 			tx_push1->tx_bd_hsize_lflags = 0;
554 
555 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
556 		tx_push1->tx_bd_cfa_action =
557 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
558 
559 		end = pdata + length;
560 		end = PTR_ALIGN(end, 8) - 1;
561 		*end = 0;
562 
563 		skb_copy_from_linear_data(skb, pdata, len);
564 		pdata += len;
565 		for (j = 0; j < last_frag; j++) {
566 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
567 			void *fptr;
568 
569 			fptr = skb_frag_address_safe(frag);
570 			if (!fptr)
571 				goto normal_tx;
572 
573 			memcpy(pdata, fptr, skb_frag_size(frag));
574 			pdata += skb_frag_size(frag);
575 		}
576 
577 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
578 		txbd->tx_bd_haddr = txr->data_mapping;
579 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
580 		prod = NEXT_TX(prod);
581 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
582 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
583 		memcpy(txbd, tx_push1, sizeof(*txbd));
584 		prod = NEXT_TX(prod);
585 		tx_push->doorbell =
586 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
587 				    DB_RING_IDX(&txr->tx_db, prod));
588 		WRITE_ONCE(txr->tx_prod, prod);
589 
590 		tx_buf->is_push = 1;
591 		netdev_tx_sent_queue(txq, skb->len);
592 		wmb();	/* Sync is_push and byte queue before pushing data */
593 
594 		push_len = (length + sizeof(*tx_push) + 7) / 8;
595 		if (push_len > 16) {
596 			__iowrite64_copy(db, tx_push_buf, 16);
597 			__iowrite32_copy(db + 4, tx_push_buf + 1,
598 					 (push_len - 16) << 1);
599 		} else {
600 			__iowrite64_copy(db, tx_push_buf, push_len);
601 		}
602 
603 		goto tx_done;
604 	}
605 
606 normal_tx:
607 	if (length < BNXT_MIN_PKT_SIZE) {
608 		pad = BNXT_MIN_PKT_SIZE - length;
609 		if (skb_pad(skb, pad))
610 			/* SKB already freed. */
611 			goto tx_kick_pending;
612 		length = BNXT_MIN_PKT_SIZE;
613 	}
614 
615 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
616 
617 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
618 		goto tx_free;
619 
620 	dma_unmap_addr_set(tx_buf, mapping, mapping);
621 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
622 		((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
623 
624 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
625 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
626 
627 	prod = NEXT_TX(prod);
628 	txbd1 = (struct tx_bd_ext *)
629 		&txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
630 
631 	txbd1->tx_bd_hsize_lflags = lflags;
632 	if (skb_is_gso(skb)) {
633 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
634 		u32 hdr_len;
635 
636 		if (skb->encapsulation) {
637 			if (udp_gso)
638 				hdr_len = skb_inner_transport_offset(skb) +
639 					  sizeof(struct udphdr);
640 			else
641 				hdr_len = skb_inner_tcp_all_headers(skb);
642 		} else if (udp_gso) {
643 			hdr_len = skb_transport_offset(skb) +
644 				  sizeof(struct udphdr);
645 		} else {
646 			hdr_len = skb_tcp_all_headers(skb);
647 		}
648 
649 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
650 					TX_BD_FLAGS_T_IPID |
651 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
652 		length = skb_shinfo(skb)->gso_size;
653 		txbd1->tx_bd_mss = cpu_to_le32(length);
654 		length += hdr_len;
655 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
656 		txbd1->tx_bd_hsize_lflags |=
657 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
658 		txbd1->tx_bd_mss = 0;
659 	}
660 
661 	length >>= 9;
662 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
663 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
664 				     skb->len);
665 		i = 0;
666 		goto tx_dma_error;
667 	}
668 	flags |= bnxt_lhint_arr[length];
669 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
670 
671 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
672 	txbd1->tx_bd_cfa_action =
673 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
674 	txbd0 = txbd;
675 	for (i = 0; i < last_frag; i++) {
676 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
677 
678 		prod = NEXT_TX(prod);
679 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
680 
681 		len = skb_frag_size(frag);
682 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
683 					   DMA_TO_DEVICE);
684 
685 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
686 			goto tx_dma_error;
687 
688 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
689 		dma_unmap_addr_set(tx_buf, mapping, mapping);
690 
691 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
692 
693 		flags = len << TX_BD_LEN_SHIFT;
694 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
695 	}
696 
697 	flags &= ~TX_BD_LEN;
698 	txbd->tx_bd_len_flags_type =
699 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
700 			    TX_BD_FLAGS_PACKET_END);
701 
702 	netdev_tx_sent_queue(txq, skb->len);
703 
704 	skb_tx_timestamp(skb);
705 
706 	prod = NEXT_TX(prod);
707 	WRITE_ONCE(txr->tx_prod, prod);
708 
709 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
710 		bnxt_txr_db_kick(bp, txr, prod);
711 	} else {
712 		if (free_size >= bp->tx_wake_thresh)
713 			txbd0->tx_bd_len_flags_type |=
714 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
715 		txr->kick_pending = 1;
716 	}
717 
718 tx_done:
719 
720 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
721 		if (netdev_xmit_more() && !tx_buf->is_push) {
722 			txbd0->tx_bd_len_flags_type &=
723 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
724 			bnxt_txr_db_kick(bp, txr, prod);
725 		}
726 
727 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
728 				   bp->tx_wake_thresh);
729 	}
730 	return NETDEV_TX_OK;
731 
732 tx_dma_error:
733 	if (BNXT_TX_PTP_IS_SET(lflags))
734 		atomic_inc(&bp->ptp_cfg->tx_avail);
735 
736 	last_frag = i;
737 
738 	/* start back at beginning and unmap skb */
739 	prod = txr->tx_prod;
740 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
741 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
742 			 skb_headlen(skb), DMA_TO_DEVICE);
743 	prod = NEXT_TX(prod);
744 
745 	/* unmap remaining mapped pages */
746 	for (i = 0; i < last_frag; i++) {
747 		prod = NEXT_TX(prod);
748 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
749 		dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
750 			       skb_frag_size(&skb_shinfo(skb)->frags[i]),
751 			       DMA_TO_DEVICE);
752 	}
753 
754 tx_free:
755 	dev_kfree_skb_any(skb);
756 tx_kick_pending:
757 	if (txr->kick_pending)
758 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
759 	txr->tx_buf_ring[txr->tx_prod].skb = NULL;
760 	dev_core_stats_tx_dropped_inc(dev);
761 	return NETDEV_TX_OK;
762 }
763 
764 static void __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
765 			  int budget)
766 {
767 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
768 	struct pci_dev *pdev = bp->pdev;
769 	u16 hw_cons = txr->tx_hw_cons;
770 	unsigned int tx_bytes = 0;
771 	u16 cons = txr->tx_cons;
772 	int tx_pkts = 0;
773 
774 	while (RING_TX(bp, cons) != hw_cons) {
775 		struct bnxt_sw_tx_bd *tx_buf;
776 		struct sk_buff *skb;
777 		int j, last;
778 
779 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
780 		cons = NEXT_TX(cons);
781 		skb = tx_buf->skb;
782 		tx_buf->skb = NULL;
783 
784 		if (unlikely(!skb)) {
785 			bnxt_sched_reset_txr(bp, txr, cons);
786 			return;
787 		}
788 
789 		tx_pkts++;
790 		tx_bytes += skb->len;
791 
792 		if (tx_buf->is_push) {
793 			tx_buf->is_push = 0;
794 			goto next_tx_int;
795 		}
796 
797 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
798 				 skb_headlen(skb), DMA_TO_DEVICE);
799 		last = tx_buf->nr_frags;
800 
801 		for (j = 0; j < last; j++) {
802 			cons = NEXT_TX(cons);
803 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
804 			dma_unmap_page(
805 				&pdev->dev,
806 				dma_unmap_addr(tx_buf, mapping),
807 				skb_frag_size(&skb_shinfo(skb)->frags[j]),
808 				DMA_TO_DEVICE);
809 		}
810 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
811 			if (BNXT_CHIP_P5(bp)) {
812 				/* PTP worker takes ownership of the skb */
813 				if (!bnxt_get_tx_ts_p5(bp, skb))
814 					skb = NULL;
815 				else
816 					atomic_inc(&bp->ptp_cfg->tx_avail);
817 			}
818 		}
819 
820 next_tx_int:
821 		cons = NEXT_TX(cons);
822 
823 		dev_consume_skb_any(skb);
824 	}
825 
826 	WRITE_ONCE(txr->tx_cons, cons);
827 
828 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
829 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
830 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
831 }
832 
833 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
834 {
835 	struct bnxt_tx_ring_info *txr;
836 	int i;
837 
838 	bnxt_for_each_napi_tx(i, bnapi, txr) {
839 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
840 			__bnxt_tx_int(bp, txr, budget);
841 	}
842 	bnapi->events &= ~BNXT_TX_CMP_EVENT;
843 }
844 
845 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
846 					 struct bnxt_rx_ring_info *rxr,
847 					 unsigned int *offset,
848 					 gfp_t gfp)
849 {
850 	struct page *page;
851 
852 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
853 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
854 						BNXT_RX_PAGE_SIZE);
855 	} else {
856 		page = page_pool_dev_alloc_pages(rxr->page_pool);
857 		*offset = 0;
858 	}
859 	if (!page)
860 		return NULL;
861 
862 	*mapping = page_pool_get_dma_addr(page) + *offset;
863 	return page;
864 }
865 
866 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
867 				       gfp_t gfp)
868 {
869 	u8 *data;
870 	struct pci_dev *pdev = bp->pdev;
871 
872 	if (gfp == GFP_ATOMIC)
873 		data = napi_alloc_frag(bp->rx_buf_size);
874 	else
875 		data = netdev_alloc_frag(bp->rx_buf_size);
876 	if (!data)
877 		return NULL;
878 
879 	*mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
880 					bp->rx_buf_use_size, bp->rx_dir,
881 					DMA_ATTR_WEAK_ORDERING);
882 
883 	if (dma_mapping_error(&pdev->dev, *mapping)) {
884 		skb_free_frag(data);
885 		data = NULL;
886 	}
887 	return data;
888 }
889 
890 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
891 		       u16 prod, gfp_t gfp)
892 {
893 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
894 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
895 	dma_addr_t mapping;
896 
897 	if (BNXT_RX_PAGE_MODE(bp)) {
898 		unsigned int offset;
899 		struct page *page =
900 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
901 
902 		if (!page)
903 			return -ENOMEM;
904 
905 		mapping += bp->rx_dma_offset;
906 		rx_buf->data = page;
907 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
908 	} else {
909 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, gfp);
910 
911 		if (!data)
912 			return -ENOMEM;
913 
914 		rx_buf->data = data;
915 		rx_buf->data_ptr = data + bp->rx_offset;
916 	}
917 	rx_buf->mapping = mapping;
918 
919 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
920 	return 0;
921 }
922 
923 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
924 {
925 	u16 prod = rxr->rx_prod;
926 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
927 	struct bnxt *bp = rxr->bnapi->bp;
928 	struct rx_bd *cons_bd, *prod_bd;
929 
930 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
931 	cons_rx_buf = &rxr->rx_buf_ring[cons];
932 
933 	prod_rx_buf->data = data;
934 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
935 
936 	prod_rx_buf->mapping = cons_rx_buf->mapping;
937 
938 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
939 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
940 
941 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
942 }
943 
944 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
945 {
946 	u16 next, max = rxr->rx_agg_bmap_size;
947 
948 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
949 	if (next >= max)
950 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
951 	return next;
952 }
953 
954 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
955 				     struct bnxt_rx_ring_info *rxr,
956 				     u16 prod, gfp_t gfp)
957 {
958 	struct rx_bd *rxbd =
959 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
960 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
961 	struct page *page;
962 	dma_addr_t mapping;
963 	u16 sw_prod = rxr->rx_sw_agg_prod;
964 	unsigned int offset = 0;
965 
966 	page = __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
967 
968 	if (!page)
969 		return -ENOMEM;
970 
971 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
972 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
973 
974 	__set_bit(sw_prod, rxr->rx_agg_bmap);
975 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
976 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
977 
978 	rx_agg_buf->page = page;
979 	rx_agg_buf->offset = offset;
980 	rx_agg_buf->mapping = mapping;
981 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
982 	rxbd->rx_bd_opaque = sw_prod;
983 	return 0;
984 }
985 
986 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
987 				       struct bnxt_cp_ring_info *cpr,
988 				       u16 cp_cons, u16 curr)
989 {
990 	struct rx_agg_cmp *agg;
991 
992 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
993 	agg = (struct rx_agg_cmp *)
994 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
995 	return agg;
996 }
997 
998 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
999 					      struct bnxt_rx_ring_info *rxr,
1000 					      u16 agg_id, u16 curr)
1001 {
1002 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1003 
1004 	return &tpa_info->agg_arr[curr];
1005 }
1006 
1007 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1008 				   u16 start, u32 agg_bufs, bool tpa)
1009 {
1010 	struct bnxt_napi *bnapi = cpr->bnapi;
1011 	struct bnxt *bp = bnapi->bp;
1012 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1013 	u16 prod = rxr->rx_agg_prod;
1014 	u16 sw_prod = rxr->rx_sw_agg_prod;
1015 	bool p5_tpa = false;
1016 	u32 i;
1017 
1018 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1019 		p5_tpa = true;
1020 
1021 	for (i = 0; i < agg_bufs; i++) {
1022 		u16 cons;
1023 		struct rx_agg_cmp *agg;
1024 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1025 		struct rx_bd *prod_bd;
1026 		struct page *page;
1027 
1028 		if (p5_tpa)
1029 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1030 		else
1031 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1032 		cons = agg->rx_agg_cmp_opaque;
1033 		__clear_bit(cons, rxr->rx_agg_bmap);
1034 
1035 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1036 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1037 
1038 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1039 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1040 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1041 
1042 		/* It is possible for sw_prod to be equal to cons, so
1043 		 * set cons_rx_buf->page to NULL first.
1044 		 */
1045 		page = cons_rx_buf->page;
1046 		cons_rx_buf->page = NULL;
1047 		prod_rx_buf->page = page;
1048 		prod_rx_buf->offset = cons_rx_buf->offset;
1049 
1050 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1051 
1052 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1053 
1054 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1055 		prod_bd->rx_bd_opaque = sw_prod;
1056 
1057 		prod = NEXT_RX_AGG(prod);
1058 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1059 	}
1060 	rxr->rx_agg_prod = prod;
1061 	rxr->rx_sw_agg_prod = sw_prod;
1062 }
1063 
1064 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1065 					      struct bnxt_rx_ring_info *rxr,
1066 					      u16 cons, void *data, u8 *data_ptr,
1067 					      dma_addr_t dma_addr,
1068 					      unsigned int offset_and_len)
1069 {
1070 	unsigned int len = offset_and_len & 0xffff;
1071 	struct page *page = data;
1072 	u16 prod = rxr->rx_prod;
1073 	struct sk_buff *skb;
1074 	int err;
1075 
1076 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1077 	if (unlikely(err)) {
1078 		bnxt_reuse_rx_data(rxr, cons, data);
1079 		return NULL;
1080 	}
1081 	dma_addr -= bp->rx_dma_offset;
1082 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1083 				bp->rx_dir);
1084 	skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE);
1085 	if (!skb) {
1086 		page_pool_recycle_direct(rxr->page_pool, page);
1087 		return NULL;
1088 	}
1089 	skb_mark_for_recycle(skb);
1090 	skb_reserve(skb, bp->rx_offset);
1091 	__skb_put(skb, len);
1092 
1093 	return skb;
1094 }
1095 
1096 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1097 					struct bnxt_rx_ring_info *rxr,
1098 					u16 cons, void *data, u8 *data_ptr,
1099 					dma_addr_t dma_addr,
1100 					unsigned int offset_and_len)
1101 {
1102 	unsigned int payload = offset_and_len >> 16;
1103 	unsigned int len = offset_and_len & 0xffff;
1104 	skb_frag_t *frag;
1105 	struct page *page = data;
1106 	u16 prod = rxr->rx_prod;
1107 	struct sk_buff *skb;
1108 	int off, err;
1109 
1110 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1111 	if (unlikely(err)) {
1112 		bnxt_reuse_rx_data(rxr, cons, data);
1113 		return NULL;
1114 	}
1115 	dma_addr -= bp->rx_dma_offset;
1116 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1117 				bp->rx_dir);
1118 
1119 	if (unlikely(!payload))
1120 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1121 
1122 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1123 	if (!skb) {
1124 		page_pool_recycle_direct(rxr->page_pool, page);
1125 		return NULL;
1126 	}
1127 
1128 	skb_mark_for_recycle(skb);
1129 	off = (void *)data_ptr - page_address(page);
1130 	skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE);
1131 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1132 	       payload + NET_IP_ALIGN);
1133 
1134 	frag = &skb_shinfo(skb)->frags[0];
1135 	skb_frag_size_sub(frag, payload);
1136 	skb_frag_off_add(frag, payload);
1137 	skb->data_len -= payload;
1138 	skb->tail += payload;
1139 
1140 	return skb;
1141 }
1142 
1143 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1144 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1145 				   void *data, u8 *data_ptr,
1146 				   dma_addr_t dma_addr,
1147 				   unsigned int offset_and_len)
1148 {
1149 	u16 prod = rxr->rx_prod;
1150 	struct sk_buff *skb;
1151 	int err;
1152 
1153 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1154 	if (unlikely(err)) {
1155 		bnxt_reuse_rx_data(rxr, cons, data);
1156 		return NULL;
1157 	}
1158 
1159 	skb = napi_build_skb(data, bp->rx_buf_size);
1160 	dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1161 			       bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
1162 	if (!skb) {
1163 		skb_free_frag(data);
1164 		return NULL;
1165 	}
1166 
1167 	skb_reserve(skb, bp->rx_offset);
1168 	skb_put(skb, offset_and_len & 0xffff);
1169 	return skb;
1170 }
1171 
1172 static u32 __bnxt_rx_agg_pages(struct bnxt *bp,
1173 			       struct bnxt_cp_ring_info *cpr,
1174 			       struct skb_shared_info *shinfo,
1175 			       u16 idx, u32 agg_bufs, bool tpa,
1176 			       struct xdp_buff *xdp)
1177 {
1178 	struct bnxt_napi *bnapi = cpr->bnapi;
1179 	struct pci_dev *pdev = bp->pdev;
1180 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1181 	u16 prod = rxr->rx_agg_prod;
1182 	u32 i, total_frag_len = 0;
1183 	bool p5_tpa = false;
1184 
1185 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1186 		p5_tpa = true;
1187 
1188 	for (i = 0; i < agg_bufs; i++) {
1189 		skb_frag_t *frag = &shinfo->frags[i];
1190 		u16 cons, frag_len;
1191 		struct rx_agg_cmp *agg;
1192 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1193 		struct page *page;
1194 		dma_addr_t mapping;
1195 
1196 		if (p5_tpa)
1197 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1198 		else
1199 			agg = bnxt_get_agg(bp, cpr, idx, i);
1200 		cons = agg->rx_agg_cmp_opaque;
1201 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1202 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1203 
1204 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1205 		skb_frag_fill_page_desc(frag, cons_rx_buf->page,
1206 					cons_rx_buf->offset, frag_len);
1207 		shinfo->nr_frags = i + 1;
1208 		__clear_bit(cons, rxr->rx_agg_bmap);
1209 
1210 		/* It is possible for bnxt_alloc_rx_page() to allocate
1211 		 * a sw_prod index that equals the cons index, so we
1212 		 * need to clear the cons entry now.
1213 		 */
1214 		mapping = cons_rx_buf->mapping;
1215 		page = cons_rx_buf->page;
1216 		cons_rx_buf->page = NULL;
1217 
1218 		if (xdp && page_is_pfmemalloc(page))
1219 			xdp_buff_set_frag_pfmemalloc(xdp);
1220 
1221 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1222 			--shinfo->nr_frags;
1223 			cons_rx_buf->page = page;
1224 
1225 			/* Update prod since possibly some pages have been
1226 			 * allocated already.
1227 			 */
1228 			rxr->rx_agg_prod = prod;
1229 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1230 			return 0;
1231 		}
1232 
1233 		dma_sync_single_for_cpu(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1234 					bp->rx_dir);
1235 
1236 		total_frag_len += frag_len;
1237 		prod = NEXT_RX_AGG(prod);
1238 	}
1239 	rxr->rx_agg_prod = prod;
1240 	return total_frag_len;
1241 }
1242 
1243 static struct sk_buff *bnxt_rx_agg_pages_skb(struct bnxt *bp,
1244 					     struct bnxt_cp_ring_info *cpr,
1245 					     struct sk_buff *skb, u16 idx,
1246 					     u32 agg_bufs, bool tpa)
1247 {
1248 	struct skb_shared_info *shinfo = skb_shinfo(skb);
1249 	u32 total_frag_len = 0;
1250 
1251 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo, idx,
1252 					     agg_bufs, tpa, NULL);
1253 	if (!total_frag_len) {
1254 		skb_mark_for_recycle(skb);
1255 		dev_kfree_skb(skb);
1256 		return NULL;
1257 	}
1258 
1259 	skb->data_len += total_frag_len;
1260 	skb->len += total_frag_len;
1261 	skb->truesize += BNXT_RX_PAGE_SIZE * agg_bufs;
1262 	return skb;
1263 }
1264 
1265 static u32 bnxt_rx_agg_pages_xdp(struct bnxt *bp,
1266 				 struct bnxt_cp_ring_info *cpr,
1267 				 struct xdp_buff *xdp, u16 idx,
1268 				 u32 agg_bufs, bool tpa)
1269 {
1270 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1271 	u32 total_frag_len = 0;
1272 
1273 	if (!xdp_buff_has_frags(xdp))
1274 		shinfo->nr_frags = 0;
1275 
1276 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo,
1277 					     idx, agg_bufs, tpa, xdp);
1278 	if (total_frag_len) {
1279 		xdp_buff_set_frags_flag(xdp);
1280 		shinfo->nr_frags = agg_bufs;
1281 		shinfo->xdp_frags_size = total_frag_len;
1282 	}
1283 	return total_frag_len;
1284 }
1285 
1286 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1287 			       u8 agg_bufs, u32 *raw_cons)
1288 {
1289 	u16 last;
1290 	struct rx_agg_cmp *agg;
1291 
1292 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1293 	last = RING_CMP(*raw_cons);
1294 	agg = (struct rx_agg_cmp *)
1295 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1296 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1297 }
1298 
1299 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1300 				      unsigned int len,
1301 				      dma_addr_t mapping)
1302 {
1303 	struct bnxt *bp = bnapi->bp;
1304 	struct pci_dev *pdev = bp->pdev;
1305 	struct sk_buff *skb;
1306 
1307 	skb = napi_alloc_skb(&bnapi->napi, len);
1308 	if (!skb)
1309 		return NULL;
1310 
1311 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1312 				bp->rx_dir);
1313 
1314 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1315 	       len + NET_IP_ALIGN);
1316 
1317 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1318 				   bp->rx_dir);
1319 
1320 	skb_put(skb, len);
1321 
1322 	return skb;
1323 }
1324 
1325 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1326 				     unsigned int len,
1327 				     dma_addr_t mapping)
1328 {
1329 	return bnxt_copy_data(bnapi, data, len, mapping);
1330 }
1331 
1332 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1333 				     struct xdp_buff *xdp,
1334 				     unsigned int len,
1335 				     dma_addr_t mapping)
1336 {
1337 	unsigned int metasize = 0;
1338 	u8 *data = xdp->data;
1339 	struct sk_buff *skb;
1340 
1341 	len = xdp->data_end - xdp->data_meta;
1342 	metasize = xdp->data - xdp->data_meta;
1343 	data = xdp->data_meta;
1344 
1345 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1346 	if (!skb)
1347 		return skb;
1348 
1349 	if (metasize) {
1350 		skb_metadata_set(skb, metasize);
1351 		__skb_pull(skb, metasize);
1352 	}
1353 
1354 	return skb;
1355 }
1356 
1357 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1358 			   u32 *raw_cons, void *cmp)
1359 {
1360 	struct rx_cmp *rxcmp = cmp;
1361 	u32 tmp_raw_cons = *raw_cons;
1362 	u8 cmp_type, agg_bufs = 0;
1363 
1364 	cmp_type = RX_CMP_TYPE(rxcmp);
1365 
1366 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1367 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1368 			    RX_CMP_AGG_BUFS) >>
1369 			   RX_CMP_AGG_BUFS_SHIFT;
1370 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1371 		struct rx_tpa_end_cmp *tpa_end = cmp;
1372 
1373 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1374 			return 0;
1375 
1376 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1377 	}
1378 
1379 	if (agg_bufs) {
1380 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1381 			return -EBUSY;
1382 	}
1383 	*raw_cons = tmp_raw_cons;
1384 	return 0;
1385 }
1386 
1387 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1388 {
1389 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1390 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1391 
1392 	if (test_bit(idx, map->agg_idx_bmap))
1393 		idx = find_first_zero_bit(map->agg_idx_bmap,
1394 					  BNXT_AGG_IDX_BMAP_SIZE);
1395 	__set_bit(idx, map->agg_idx_bmap);
1396 	map->agg_id_tbl[agg_id] = idx;
1397 	return idx;
1398 }
1399 
1400 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1401 {
1402 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1403 
1404 	__clear_bit(idx, map->agg_idx_bmap);
1405 }
1406 
1407 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1408 {
1409 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1410 
1411 	return map->agg_id_tbl[agg_id];
1412 }
1413 
1414 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1415 			      struct rx_tpa_start_cmp *tpa_start,
1416 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1417 {
1418 	tpa_info->cfa_code_valid = 1;
1419 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1420 	tpa_info->vlan_valid = 0;
1421 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1422 		tpa_info->vlan_valid = 1;
1423 		tpa_info->metadata =
1424 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1425 	}
1426 }
1427 
1428 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1429 				 struct rx_tpa_start_cmp *tpa_start,
1430 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1431 {
1432 	tpa_info->vlan_valid = 0;
1433 	if (TPA_START_VLAN_VALID(tpa_start)) {
1434 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1435 		u32 vlan_proto = ETH_P_8021Q;
1436 
1437 		tpa_info->vlan_valid = 1;
1438 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1439 			vlan_proto = ETH_P_8021AD;
1440 		tpa_info->metadata = vlan_proto << 16 |
1441 				     TPA_START_METADATA0_TCI(tpa_start1);
1442 	}
1443 }
1444 
1445 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1446 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1447 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1448 {
1449 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1450 	struct bnxt_tpa_info *tpa_info;
1451 	u16 cons, prod, agg_id;
1452 	struct rx_bd *prod_bd;
1453 	dma_addr_t mapping;
1454 
1455 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1456 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1457 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1458 	} else {
1459 		agg_id = TPA_START_AGG_ID(tpa_start);
1460 	}
1461 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1462 	prod = rxr->rx_prod;
1463 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1464 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1465 	tpa_info = &rxr->rx_tpa[agg_id];
1466 
1467 	if (unlikely(cons != rxr->rx_next_cons ||
1468 		     TPA_START_ERROR(tpa_start))) {
1469 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1470 			    cons, rxr->rx_next_cons,
1471 			    TPA_START_ERROR_CODE(tpa_start1));
1472 		bnxt_sched_reset_rxr(bp, rxr);
1473 		return;
1474 	}
1475 	prod_rx_buf->data = tpa_info->data;
1476 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1477 
1478 	mapping = tpa_info->mapping;
1479 	prod_rx_buf->mapping = mapping;
1480 
1481 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1482 
1483 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1484 
1485 	tpa_info->data = cons_rx_buf->data;
1486 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1487 	cons_rx_buf->data = NULL;
1488 	tpa_info->mapping = cons_rx_buf->mapping;
1489 
1490 	tpa_info->len =
1491 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1492 				RX_TPA_START_CMP_LEN_SHIFT;
1493 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1494 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1495 		tpa_info->gso_type = SKB_GSO_TCPV4;
1496 		if (TPA_START_IS_IPV6(tpa_start1))
1497 			tpa_info->gso_type = SKB_GSO_TCPV6;
1498 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1499 		else if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP &&
1500 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1501 			tpa_info->gso_type = SKB_GSO_TCPV6;
1502 		tpa_info->rss_hash =
1503 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1504 	} else {
1505 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1506 		tpa_info->gso_type = 0;
1507 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1508 	}
1509 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1510 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1511 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1512 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1513 	else
1514 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1515 	tpa_info->agg_count = 0;
1516 
1517 	rxr->rx_prod = NEXT_RX(prod);
1518 	cons = RING_RX(bp, NEXT_RX(cons));
1519 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1520 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1521 
1522 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1523 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1524 	cons_rx_buf->data = NULL;
1525 }
1526 
1527 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1528 {
1529 	if (agg_bufs)
1530 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1531 }
1532 
1533 #ifdef CONFIG_INET
1534 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1535 {
1536 	struct udphdr *uh = NULL;
1537 
1538 	if (ip_proto == htons(ETH_P_IP)) {
1539 		struct iphdr *iph = (struct iphdr *)skb->data;
1540 
1541 		if (iph->protocol == IPPROTO_UDP)
1542 			uh = (struct udphdr *)(iph + 1);
1543 	} else {
1544 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1545 
1546 		if (iph->nexthdr == IPPROTO_UDP)
1547 			uh = (struct udphdr *)(iph + 1);
1548 	}
1549 	if (uh) {
1550 		if (uh->check)
1551 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1552 		else
1553 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1554 	}
1555 }
1556 #endif
1557 
1558 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1559 					   int payload_off, int tcp_ts,
1560 					   struct sk_buff *skb)
1561 {
1562 #ifdef CONFIG_INET
1563 	struct tcphdr *th;
1564 	int len, nw_off;
1565 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1566 	u32 hdr_info = tpa_info->hdr_info;
1567 	bool loopback = false;
1568 
1569 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1570 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1571 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1572 
1573 	/* If the packet is an internal loopback packet, the offsets will
1574 	 * have an extra 4 bytes.
1575 	 */
1576 	if (inner_mac_off == 4) {
1577 		loopback = true;
1578 	} else if (inner_mac_off > 4) {
1579 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1580 					    ETH_HLEN - 2));
1581 
1582 		/* We only support inner iPv4/ipv6.  If we don't see the
1583 		 * correct protocol ID, it must be a loopback packet where
1584 		 * the offsets are off by 4.
1585 		 */
1586 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1587 			loopback = true;
1588 	}
1589 	if (loopback) {
1590 		/* internal loopback packet, subtract all offsets by 4 */
1591 		inner_ip_off -= 4;
1592 		inner_mac_off -= 4;
1593 		outer_ip_off -= 4;
1594 	}
1595 
1596 	nw_off = inner_ip_off - ETH_HLEN;
1597 	skb_set_network_header(skb, nw_off);
1598 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1599 		struct ipv6hdr *iph = ipv6_hdr(skb);
1600 
1601 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1602 		len = skb->len - skb_transport_offset(skb);
1603 		th = tcp_hdr(skb);
1604 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1605 	} else {
1606 		struct iphdr *iph = ip_hdr(skb);
1607 
1608 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1609 		len = skb->len - skb_transport_offset(skb);
1610 		th = tcp_hdr(skb);
1611 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1612 	}
1613 
1614 	if (inner_mac_off) { /* tunnel */
1615 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1616 					    ETH_HLEN - 2));
1617 
1618 		bnxt_gro_tunnel(skb, proto);
1619 	}
1620 #endif
1621 	return skb;
1622 }
1623 
1624 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1625 					   int payload_off, int tcp_ts,
1626 					   struct sk_buff *skb)
1627 {
1628 #ifdef CONFIG_INET
1629 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1630 	u32 hdr_info = tpa_info->hdr_info;
1631 	int iphdr_len, nw_off;
1632 
1633 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1634 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1635 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1636 
1637 	nw_off = inner_ip_off - ETH_HLEN;
1638 	skb_set_network_header(skb, nw_off);
1639 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1640 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1641 	skb_set_transport_header(skb, nw_off + iphdr_len);
1642 
1643 	if (inner_mac_off) { /* tunnel */
1644 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1645 					    ETH_HLEN - 2));
1646 
1647 		bnxt_gro_tunnel(skb, proto);
1648 	}
1649 #endif
1650 	return skb;
1651 }
1652 
1653 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1654 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1655 
1656 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1657 					   int payload_off, int tcp_ts,
1658 					   struct sk_buff *skb)
1659 {
1660 #ifdef CONFIG_INET
1661 	struct tcphdr *th;
1662 	int len, nw_off, tcp_opt_len = 0;
1663 
1664 	if (tcp_ts)
1665 		tcp_opt_len = 12;
1666 
1667 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1668 		struct iphdr *iph;
1669 
1670 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1671 			 ETH_HLEN;
1672 		skb_set_network_header(skb, nw_off);
1673 		iph = ip_hdr(skb);
1674 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1675 		len = skb->len - skb_transport_offset(skb);
1676 		th = tcp_hdr(skb);
1677 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1678 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1679 		struct ipv6hdr *iph;
1680 
1681 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1682 			 ETH_HLEN;
1683 		skb_set_network_header(skb, nw_off);
1684 		iph = ipv6_hdr(skb);
1685 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1686 		len = skb->len - skb_transport_offset(skb);
1687 		th = tcp_hdr(skb);
1688 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1689 	} else {
1690 		dev_kfree_skb_any(skb);
1691 		return NULL;
1692 	}
1693 
1694 	if (nw_off) /* tunnel */
1695 		bnxt_gro_tunnel(skb, skb->protocol);
1696 #endif
1697 	return skb;
1698 }
1699 
1700 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1701 					   struct bnxt_tpa_info *tpa_info,
1702 					   struct rx_tpa_end_cmp *tpa_end,
1703 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1704 					   struct sk_buff *skb)
1705 {
1706 #ifdef CONFIG_INET
1707 	int payload_off;
1708 	u16 segs;
1709 
1710 	segs = TPA_END_TPA_SEGS(tpa_end);
1711 	if (segs == 1)
1712 		return skb;
1713 
1714 	NAPI_GRO_CB(skb)->count = segs;
1715 	skb_shinfo(skb)->gso_size =
1716 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1717 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1718 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1719 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1720 	else
1721 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1722 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1723 	if (likely(skb))
1724 		tcp_gro_complete(skb);
1725 #endif
1726 	return skb;
1727 }
1728 
1729 /* Given the cfa_code of a received packet determine which
1730  * netdev (vf-rep or PF) the packet is destined to.
1731  */
1732 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1733 {
1734 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1735 
1736 	/* if vf-rep dev is NULL, the must belongs to the PF */
1737 	return dev ? dev : bp->dev;
1738 }
1739 
1740 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1741 					   struct bnxt_cp_ring_info *cpr,
1742 					   u32 *raw_cons,
1743 					   struct rx_tpa_end_cmp *tpa_end,
1744 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1745 					   u8 *event)
1746 {
1747 	struct bnxt_napi *bnapi = cpr->bnapi;
1748 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1749 	struct net_device *dev = bp->dev;
1750 	u8 *data_ptr, agg_bufs;
1751 	unsigned int len;
1752 	struct bnxt_tpa_info *tpa_info;
1753 	dma_addr_t mapping;
1754 	struct sk_buff *skb;
1755 	u16 idx = 0, agg_id;
1756 	void *data;
1757 	bool gro;
1758 
1759 	if (unlikely(bnapi->in_reset)) {
1760 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1761 
1762 		if (rc < 0)
1763 			return ERR_PTR(-EBUSY);
1764 		return NULL;
1765 	}
1766 
1767 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1768 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1769 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1770 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1771 		tpa_info = &rxr->rx_tpa[agg_id];
1772 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1773 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1774 				    agg_bufs, tpa_info->agg_count);
1775 			agg_bufs = tpa_info->agg_count;
1776 		}
1777 		tpa_info->agg_count = 0;
1778 		*event |= BNXT_AGG_EVENT;
1779 		bnxt_free_agg_idx(rxr, agg_id);
1780 		idx = agg_id;
1781 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1782 	} else {
1783 		agg_id = TPA_END_AGG_ID(tpa_end);
1784 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1785 		tpa_info = &rxr->rx_tpa[agg_id];
1786 		idx = RING_CMP(*raw_cons);
1787 		if (agg_bufs) {
1788 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1789 				return ERR_PTR(-EBUSY);
1790 
1791 			*event |= BNXT_AGG_EVENT;
1792 			idx = NEXT_CMP(idx);
1793 		}
1794 		gro = !!TPA_END_GRO(tpa_end);
1795 	}
1796 	data = tpa_info->data;
1797 	data_ptr = tpa_info->data_ptr;
1798 	prefetch(data_ptr);
1799 	len = tpa_info->len;
1800 	mapping = tpa_info->mapping;
1801 
1802 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1803 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1804 		if (agg_bufs > MAX_SKB_FRAGS)
1805 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1806 				    agg_bufs, (int)MAX_SKB_FRAGS);
1807 		return NULL;
1808 	}
1809 
1810 	if (len <= bp->rx_copy_thresh) {
1811 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1812 		if (!skb) {
1813 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1814 			cpr->sw_stats.rx.rx_oom_discards += 1;
1815 			return NULL;
1816 		}
1817 	} else {
1818 		u8 *new_data;
1819 		dma_addr_t new_mapping;
1820 
1821 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, GFP_ATOMIC);
1822 		if (!new_data) {
1823 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1824 			cpr->sw_stats.rx.rx_oom_discards += 1;
1825 			return NULL;
1826 		}
1827 
1828 		tpa_info->data = new_data;
1829 		tpa_info->data_ptr = new_data + bp->rx_offset;
1830 		tpa_info->mapping = new_mapping;
1831 
1832 		skb = napi_build_skb(data, bp->rx_buf_size);
1833 		dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1834 				       bp->rx_buf_use_size, bp->rx_dir,
1835 				       DMA_ATTR_WEAK_ORDERING);
1836 
1837 		if (!skb) {
1838 			skb_free_frag(data);
1839 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1840 			cpr->sw_stats.rx.rx_oom_discards += 1;
1841 			return NULL;
1842 		}
1843 		skb_reserve(skb, bp->rx_offset);
1844 		skb_put(skb, len);
1845 	}
1846 
1847 	if (agg_bufs) {
1848 		skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, idx, agg_bufs, true);
1849 		if (!skb) {
1850 			/* Page reuse already handled by bnxt_rx_pages(). */
1851 			cpr->sw_stats.rx.rx_oom_discards += 1;
1852 			return NULL;
1853 		}
1854 	}
1855 
1856 	if (tpa_info->cfa_code_valid)
1857 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1858 	skb->protocol = eth_type_trans(skb, dev);
1859 
1860 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1861 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1862 
1863 	if (tpa_info->vlan_valid &&
1864 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1865 		__be16 vlan_proto = htons(tpa_info->metadata >>
1866 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1867 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1868 
1869 		if (eth_type_vlan(vlan_proto)) {
1870 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1871 		} else {
1872 			dev_kfree_skb(skb);
1873 			return NULL;
1874 		}
1875 	}
1876 
1877 	skb_checksum_none_assert(skb);
1878 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1879 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1880 		skb->csum_level =
1881 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1882 	}
1883 
1884 	if (gro)
1885 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1886 
1887 	return skb;
1888 }
1889 
1890 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1891 			 struct rx_agg_cmp *rx_agg)
1892 {
1893 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1894 	struct bnxt_tpa_info *tpa_info;
1895 
1896 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1897 	tpa_info = &rxr->rx_tpa[agg_id];
1898 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1899 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1900 }
1901 
1902 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1903 			     struct sk_buff *skb)
1904 {
1905 	skb_mark_for_recycle(skb);
1906 
1907 	if (skb->dev != bp->dev) {
1908 		/* this packet belongs to a vf-rep */
1909 		bnxt_vf_rep_rx(bp, skb);
1910 		return;
1911 	}
1912 	skb_record_rx_queue(skb, bnapi->index);
1913 	napi_gro_receive(&bnapi->napi, skb);
1914 }
1915 
1916 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
1917 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
1918 {
1919 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
1920 
1921 	if (BNXT_PTP_RX_TS_VALID(flags))
1922 		goto ts_valid;
1923 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
1924 		return false;
1925 
1926 ts_valid:
1927 	*cmpl_ts = ts;
1928 	return true;
1929 }
1930 
1931 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
1932 				    struct rx_cmp *rxcmp,
1933 				    struct rx_cmp_ext *rxcmp1)
1934 {
1935 	__be16 vlan_proto;
1936 	u16 vtag;
1937 
1938 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1939 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
1940 		u32 meta_data;
1941 
1942 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
1943 			return skb;
1944 
1945 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1946 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1947 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
1948 		if (eth_type_vlan(vlan_proto))
1949 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1950 		else
1951 			goto vlan_err;
1952 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
1953 		if (RX_CMP_VLAN_VALID(rxcmp)) {
1954 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
1955 
1956 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
1957 				vlan_proto = htons(ETH_P_8021Q);
1958 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
1959 				vlan_proto = htons(ETH_P_8021AD);
1960 			else
1961 				goto vlan_err;
1962 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
1963 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1964 		}
1965 	}
1966 	return skb;
1967 vlan_err:
1968 	dev_kfree_skb(skb);
1969 	return NULL;
1970 }
1971 
1972 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp,
1973 					   struct rx_cmp *rxcmp)
1974 {
1975 	u8 ext_op;
1976 
1977 	ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp);
1978 	switch (ext_op) {
1979 	case EXT_OP_INNER_4:
1980 	case EXT_OP_OUTER_4:
1981 	case EXT_OP_INNFL_3:
1982 	case EXT_OP_OUTFL_3:
1983 		return PKT_HASH_TYPE_L4;
1984 	default:
1985 		return PKT_HASH_TYPE_L3;
1986 	}
1987 }
1988 
1989 /* returns the following:
1990  * 1       - 1 packet successfully received
1991  * 0       - successful TPA_START, packet not completed yet
1992  * -EBUSY  - completion ring does not have all the agg buffers yet
1993  * -ENOMEM - packet aborted due to out of memory
1994  * -EIO    - packet aborted due to hw error indicated in BD
1995  */
1996 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1997 		       u32 *raw_cons, u8 *event)
1998 {
1999 	struct bnxt_napi *bnapi = cpr->bnapi;
2000 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2001 	struct net_device *dev = bp->dev;
2002 	struct rx_cmp *rxcmp;
2003 	struct rx_cmp_ext *rxcmp1;
2004 	u32 tmp_raw_cons = *raw_cons;
2005 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2006 	struct bnxt_sw_rx_bd *rx_buf;
2007 	unsigned int len;
2008 	u8 *data_ptr, agg_bufs, cmp_type;
2009 	bool xdp_active = false;
2010 	dma_addr_t dma_addr;
2011 	struct sk_buff *skb;
2012 	struct xdp_buff xdp;
2013 	u32 flags, misc;
2014 	u32 cmpl_ts;
2015 	void *data;
2016 	int rc = 0;
2017 
2018 	rxcmp = (struct rx_cmp *)
2019 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2020 
2021 	cmp_type = RX_CMP_TYPE(rxcmp);
2022 
2023 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2024 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2025 		goto next_rx_no_prod_no_len;
2026 	}
2027 
2028 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2029 	cp_cons = RING_CMP(tmp_raw_cons);
2030 	rxcmp1 = (struct rx_cmp_ext *)
2031 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2032 
2033 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2034 		return -EBUSY;
2035 
2036 	/* The valid test of the entry must be done first before
2037 	 * reading any further.
2038 	 */
2039 	dma_rmb();
2040 	prod = rxr->rx_prod;
2041 
2042 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2043 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2044 		bnxt_tpa_start(bp, rxr, cmp_type,
2045 			       (struct rx_tpa_start_cmp *)rxcmp,
2046 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2047 
2048 		*event |= BNXT_RX_EVENT;
2049 		goto next_rx_no_prod_no_len;
2050 
2051 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2052 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2053 				   (struct rx_tpa_end_cmp *)rxcmp,
2054 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2055 
2056 		if (IS_ERR(skb))
2057 			return -EBUSY;
2058 
2059 		rc = -ENOMEM;
2060 		if (likely(skb)) {
2061 			bnxt_deliver_skb(bp, bnapi, skb);
2062 			rc = 1;
2063 		}
2064 		*event |= BNXT_RX_EVENT;
2065 		goto next_rx_no_prod_no_len;
2066 	}
2067 
2068 	cons = rxcmp->rx_cmp_opaque;
2069 	if (unlikely(cons != rxr->rx_next_cons)) {
2070 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2071 
2072 		/* 0xffff is forced error, don't print it */
2073 		if (rxr->rx_next_cons != 0xffff)
2074 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2075 				    cons, rxr->rx_next_cons);
2076 		bnxt_sched_reset_rxr(bp, rxr);
2077 		if (rc1)
2078 			return rc1;
2079 		goto next_rx_no_prod_no_len;
2080 	}
2081 	rx_buf = &rxr->rx_buf_ring[cons];
2082 	data = rx_buf->data;
2083 	data_ptr = rx_buf->data_ptr;
2084 	prefetch(data_ptr);
2085 
2086 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2087 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2088 
2089 	if (agg_bufs) {
2090 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2091 			return -EBUSY;
2092 
2093 		cp_cons = NEXT_CMP(cp_cons);
2094 		*event |= BNXT_AGG_EVENT;
2095 	}
2096 	*event |= BNXT_RX_EVENT;
2097 
2098 	rx_buf->data = NULL;
2099 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2100 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2101 
2102 		bnxt_reuse_rx_data(rxr, cons, data);
2103 		if (agg_bufs)
2104 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2105 					       false);
2106 
2107 		rc = -EIO;
2108 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2109 			bnapi->cp_ring.sw_stats.rx.rx_buf_errors++;
2110 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2111 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2112 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2113 						 rx_err);
2114 				bnxt_sched_reset_rxr(bp, rxr);
2115 			}
2116 		}
2117 		goto next_rx_no_len;
2118 	}
2119 
2120 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2121 	len = flags >> RX_CMP_LEN_SHIFT;
2122 	dma_addr = rx_buf->mapping;
2123 
2124 	if (bnxt_xdp_attached(bp, rxr)) {
2125 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2126 		if (agg_bufs) {
2127 			u32 frag_len = bnxt_rx_agg_pages_xdp(bp, cpr, &xdp,
2128 							     cp_cons, agg_bufs,
2129 							     false);
2130 			if (!frag_len) {
2131 				cpr->sw_stats.rx.rx_oom_discards += 1;
2132 				rc = -ENOMEM;
2133 				goto next_rx;
2134 			}
2135 		}
2136 		xdp_active = true;
2137 	}
2138 
2139 	if (xdp_active) {
2140 		if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) {
2141 			rc = 1;
2142 			goto next_rx;
2143 		}
2144 	}
2145 
2146 	if (len <= bp->rx_copy_thresh) {
2147 		if (!xdp_active)
2148 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2149 		else
2150 			skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr);
2151 		bnxt_reuse_rx_data(rxr, cons, data);
2152 		if (!skb) {
2153 			if (agg_bufs) {
2154 				if (!xdp_active)
2155 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2156 							       agg_bufs, false);
2157 				else
2158 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2159 			}
2160 			cpr->sw_stats.rx.rx_oom_discards += 1;
2161 			rc = -ENOMEM;
2162 			goto next_rx;
2163 		}
2164 	} else {
2165 		u32 payload;
2166 
2167 		if (rx_buf->data_ptr == data_ptr)
2168 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2169 		else
2170 			payload = 0;
2171 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2172 				      payload | len);
2173 		if (!skb) {
2174 			cpr->sw_stats.rx.rx_oom_discards += 1;
2175 			rc = -ENOMEM;
2176 			goto next_rx;
2177 		}
2178 	}
2179 
2180 	if (agg_bufs) {
2181 		if (!xdp_active) {
2182 			skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, cp_cons, agg_bufs, false);
2183 			if (!skb) {
2184 				cpr->sw_stats.rx.rx_oom_discards += 1;
2185 				rc = -ENOMEM;
2186 				goto next_rx;
2187 			}
2188 		} else {
2189 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, rxr->page_pool, &xdp, rxcmp1);
2190 			if (!skb) {
2191 				/* we should be able to free the old skb here */
2192 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2193 				cpr->sw_stats.rx.rx_oom_discards += 1;
2194 				rc = -ENOMEM;
2195 				goto next_rx;
2196 			}
2197 		}
2198 	}
2199 
2200 	if (RX_CMP_HASH_VALID(rxcmp)) {
2201 		enum pkt_hash_types type;
2202 
2203 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2204 			type = bnxt_rss_ext_op(bp, rxcmp);
2205 		} else {
2206 			u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
2207 
2208 			/* RSS profiles 1 and 3 with extract code 0 for inner
2209 			 * 4-tuple
2210 			 */
2211 			if (hash_type != 1 && hash_type != 3)
2212 				type = PKT_HASH_TYPE_L3;
2213 			else
2214 				type = PKT_HASH_TYPE_L4;
2215 		}
2216 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2217 	}
2218 
2219 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2220 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2221 	skb->protocol = eth_type_trans(skb, dev);
2222 
2223 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2224 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2225 		if (!skb)
2226 			goto next_rx;
2227 	}
2228 
2229 	skb_checksum_none_assert(skb);
2230 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2231 		if (dev->features & NETIF_F_RXCSUM) {
2232 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2233 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2234 		}
2235 	} else {
2236 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2237 			if (dev->features & NETIF_F_RXCSUM)
2238 				bnapi->cp_ring.sw_stats.rx.rx_l4_csum_errors++;
2239 		}
2240 	}
2241 
2242 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2243 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2244 			u64 ns, ts;
2245 
2246 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2247 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2248 
2249 				spin_lock_bh(&ptp->ptp_lock);
2250 				ns = timecounter_cyc2time(&ptp->tc, ts);
2251 				spin_unlock_bh(&ptp->ptp_lock);
2252 				memset(skb_hwtstamps(skb), 0,
2253 				       sizeof(*skb_hwtstamps(skb)));
2254 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2255 			}
2256 		}
2257 	}
2258 	bnxt_deliver_skb(bp, bnapi, skb);
2259 	rc = 1;
2260 
2261 next_rx:
2262 	cpr->rx_packets += 1;
2263 	cpr->rx_bytes += len;
2264 
2265 next_rx_no_len:
2266 	rxr->rx_prod = NEXT_RX(prod);
2267 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2268 
2269 next_rx_no_prod_no_len:
2270 	*raw_cons = tmp_raw_cons;
2271 
2272 	return rc;
2273 }
2274 
2275 /* In netpoll mode, if we are using a combined completion ring, we need to
2276  * discard the rx packets and recycle the buffers.
2277  */
2278 static int bnxt_force_rx_discard(struct bnxt *bp,
2279 				 struct bnxt_cp_ring_info *cpr,
2280 				 u32 *raw_cons, u8 *event)
2281 {
2282 	u32 tmp_raw_cons = *raw_cons;
2283 	struct rx_cmp_ext *rxcmp1;
2284 	struct rx_cmp *rxcmp;
2285 	u16 cp_cons;
2286 	u8 cmp_type;
2287 	int rc;
2288 
2289 	cp_cons = RING_CMP(tmp_raw_cons);
2290 	rxcmp = (struct rx_cmp *)
2291 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2292 
2293 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2294 	cp_cons = RING_CMP(tmp_raw_cons);
2295 	rxcmp1 = (struct rx_cmp_ext *)
2296 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2297 
2298 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2299 		return -EBUSY;
2300 
2301 	/* The valid test of the entry must be done first before
2302 	 * reading any further.
2303 	 */
2304 	dma_rmb();
2305 	cmp_type = RX_CMP_TYPE(rxcmp);
2306 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2307 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2308 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2309 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2310 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2311 		struct rx_tpa_end_cmp_ext *tpa_end1;
2312 
2313 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2314 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2315 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2316 	}
2317 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2318 	if (rc && rc != -EBUSY)
2319 		cpr->sw_stats.rx.rx_netpoll_discards += 1;
2320 	return rc;
2321 }
2322 
2323 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2324 {
2325 	struct bnxt_fw_health *fw_health = bp->fw_health;
2326 	u32 reg = fw_health->regs[reg_idx];
2327 	u32 reg_type, reg_off, val = 0;
2328 
2329 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2330 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2331 	switch (reg_type) {
2332 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2333 		pci_read_config_dword(bp->pdev, reg_off, &val);
2334 		break;
2335 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2336 		reg_off = fw_health->mapped_regs[reg_idx];
2337 		fallthrough;
2338 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2339 		val = readl(bp->bar0 + reg_off);
2340 		break;
2341 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2342 		val = readl(bp->bar1 + reg_off);
2343 		break;
2344 	}
2345 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2346 		val &= fw_health->fw_reset_inprog_reg_mask;
2347 	return val;
2348 }
2349 
2350 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2351 {
2352 	int i;
2353 
2354 	for (i = 0; i < bp->rx_nr_rings; i++) {
2355 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2356 		struct bnxt_ring_grp_info *grp_info;
2357 
2358 		grp_info = &bp->grp_info[grp_idx];
2359 		if (grp_info->agg_fw_ring_id == ring_id)
2360 			return grp_idx;
2361 	}
2362 	return INVALID_HW_RING_ID;
2363 }
2364 
2365 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2366 {
2367 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2368 
2369 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2370 		return link_info->force_link_speed2;
2371 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2372 		return link_info->force_pam4_link_speed;
2373 	return link_info->force_link_speed;
2374 }
2375 
2376 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2377 {
2378 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2379 
2380 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2381 		link_info->req_link_speed = link_info->force_link_speed2;
2382 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2383 		switch (link_info->req_link_speed) {
2384 		case BNXT_LINK_SPEED_50GB_PAM4:
2385 		case BNXT_LINK_SPEED_100GB_PAM4:
2386 		case BNXT_LINK_SPEED_200GB_PAM4:
2387 		case BNXT_LINK_SPEED_400GB_PAM4:
2388 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2389 			break;
2390 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2391 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2392 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2393 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2394 			break;
2395 		default:
2396 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2397 		}
2398 		return;
2399 	}
2400 	link_info->req_link_speed = link_info->force_link_speed;
2401 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2402 	if (link_info->force_pam4_link_speed) {
2403 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2404 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2405 	}
2406 }
2407 
2408 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2409 {
2410 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2411 
2412 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2413 		link_info->advertising = link_info->auto_link_speeds2;
2414 		return;
2415 	}
2416 	link_info->advertising = link_info->auto_link_speeds;
2417 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2418 }
2419 
2420 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2421 {
2422 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2423 
2424 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2425 		if (link_info->req_link_speed != link_info->force_link_speed2)
2426 			return true;
2427 		return false;
2428 	}
2429 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2430 	    link_info->req_link_speed != link_info->force_link_speed)
2431 		return true;
2432 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2433 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2434 		return true;
2435 	return false;
2436 }
2437 
2438 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2439 {
2440 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2441 
2442 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2443 		if (link_info->advertising != link_info->auto_link_speeds2)
2444 			return true;
2445 		return false;
2446 	}
2447 	if (link_info->advertising != link_info->auto_link_speeds ||
2448 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2449 		return true;
2450 	return false;
2451 }
2452 
2453 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2454 	((data2) &							\
2455 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2456 
2457 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2458 	(((data2) &							\
2459 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2460 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2461 
2462 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2463 	((data1) &							\
2464 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2465 
2466 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2467 	(((data1) &							\
2468 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2469 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2470 
2471 /* Return true if the workqueue has to be scheduled */
2472 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2473 {
2474 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2475 
2476 	switch (err_type) {
2477 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2478 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2479 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2480 		break;
2481 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2482 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2483 		break;
2484 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2485 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2486 		break;
2487 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2488 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2489 		char *threshold_type;
2490 		bool notify = false;
2491 		char *dir_str;
2492 
2493 		switch (type) {
2494 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2495 			threshold_type = "warning";
2496 			break;
2497 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2498 			threshold_type = "critical";
2499 			break;
2500 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2501 			threshold_type = "fatal";
2502 			break;
2503 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2504 			threshold_type = "shutdown";
2505 			break;
2506 		default:
2507 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2508 			return false;
2509 		}
2510 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2511 			dir_str = "above";
2512 			notify = true;
2513 		} else {
2514 			dir_str = "below";
2515 		}
2516 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2517 			    dir_str, threshold_type);
2518 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2519 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2520 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2521 		if (notify) {
2522 			bp->thermal_threshold_type = type;
2523 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2524 			return true;
2525 		}
2526 		return false;
2527 	}
2528 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2529 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2530 		break;
2531 	default:
2532 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2533 			   err_type);
2534 		break;
2535 	}
2536 	return false;
2537 }
2538 
2539 #define BNXT_GET_EVENT_PORT(data)	\
2540 	((data) &			\
2541 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2542 
2543 #define BNXT_EVENT_RING_TYPE(data2)	\
2544 	((data2) &			\
2545 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2546 
2547 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2548 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2549 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2550 
2551 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2552 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2553 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2554 
2555 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2556 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2557 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2558 
2559 #define BNXT_PHC_BITS	48
2560 
2561 static int bnxt_async_event_process(struct bnxt *bp,
2562 				    struct hwrm_async_event_cmpl *cmpl)
2563 {
2564 	u16 event_id = le16_to_cpu(cmpl->event_id);
2565 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2566 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2567 
2568 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2569 		   event_id, data1, data2);
2570 
2571 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2572 	switch (event_id) {
2573 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2574 		struct bnxt_link_info *link_info = &bp->link_info;
2575 
2576 		if (BNXT_VF(bp))
2577 			goto async_event_process_exit;
2578 
2579 		/* print unsupported speed warning in forced speed mode only */
2580 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2581 		    (data1 & 0x20000)) {
2582 			u16 fw_speed = bnxt_get_force_speed(link_info);
2583 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2584 
2585 			if (speed != SPEED_UNKNOWN)
2586 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2587 					    speed);
2588 		}
2589 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2590 	}
2591 		fallthrough;
2592 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2593 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2594 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2595 		fallthrough;
2596 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2597 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2598 		break;
2599 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2600 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2601 		break;
2602 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2603 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2604 
2605 		if (BNXT_VF(bp))
2606 			break;
2607 
2608 		if (bp->pf.port_id != port_id)
2609 			break;
2610 
2611 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2612 		break;
2613 	}
2614 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2615 		if (BNXT_PF(bp))
2616 			goto async_event_process_exit;
2617 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2618 		break;
2619 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2620 		char *type_str = "Solicited";
2621 
2622 		if (!bp->fw_health)
2623 			goto async_event_process_exit;
2624 
2625 		bp->fw_reset_timestamp = jiffies;
2626 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2627 		if (!bp->fw_reset_min_dsecs)
2628 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2629 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2630 		if (!bp->fw_reset_max_dsecs)
2631 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2632 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2633 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2634 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2635 			type_str = "Fatal";
2636 			bp->fw_health->fatalities++;
2637 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2638 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2639 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2640 			type_str = "Non-fatal";
2641 			bp->fw_health->survivals++;
2642 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2643 		}
2644 		netif_warn(bp, hw, bp->dev,
2645 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2646 			   type_str, data1, data2,
2647 			   bp->fw_reset_min_dsecs * 100,
2648 			   bp->fw_reset_max_dsecs * 100);
2649 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2650 		break;
2651 	}
2652 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2653 		struct bnxt_fw_health *fw_health = bp->fw_health;
2654 		char *status_desc = "healthy";
2655 		u32 status;
2656 
2657 		if (!fw_health)
2658 			goto async_event_process_exit;
2659 
2660 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2661 			fw_health->enabled = false;
2662 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2663 			break;
2664 		}
2665 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2666 		fw_health->tmr_multiplier =
2667 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2668 				     bp->current_interval * 10);
2669 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2670 		if (!fw_health->enabled)
2671 			fw_health->last_fw_heartbeat =
2672 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2673 		fw_health->last_fw_reset_cnt =
2674 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2675 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2676 		if (status != BNXT_FW_STATUS_HEALTHY)
2677 			status_desc = "unhealthy";
2678 		netif_info(bp, drv, bp->dev,
2679 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2680 			   fw_health->primary ? "primary" : "backup", status,
2681 			   status_desc, fw_health->last_fw_reset_cnt);
2682 		if (!fw_health->enabled) {
2683 			/* Make sure tmr_counter is set and visible to
2684 			 * bnxt_health_check() before setting enabled to true.
2685 			 */
2686 			smp_wmb();
2687 			fw_health->enabled = true;
2688 		}
2689 		goto async_event_process_exit;
2690 	}
2691 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2692 		netif_notice(bp, hw, bp->dev,
2693 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2694 			     data1, data2);
2695 		goto async_event_process_exit;
2696 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2697 		struct bnxt_rx_ring_info *rxr;
2698 		u16 grp_idx;
2699 
2700 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2701 			goto async_event_process_exit;
2702 
2703 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2704 			    BNXT_EVENT_RING_TYPE(data2), data1);
2705 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2706 			goto async_event_process_exit;
2707 
2708 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2709 		if (grp_idx == INVALID_HW_RING_ID) {
2710 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2711 				    data1);
2712 			goto async_event_process_exit;
2713 		}
2714 		rxr = bp->bnapi[grp_idx]->rx_ring;
2715 		bnxt_sched_reset_rxr(bp, rxr);
2716 		goto async_event_process_exit;
2717 	}
2718 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2719 		struct bnxt_fw_health *fw_health = bp->fw_health;
2720 
2721 		netif_notice(bp, hw, bp->dev,
2722 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2723 			     data1, data2);
2724 		if (fw_health) {
2725 			fw_health->echo_req_data1 = data1;
2726 			fw_health->echo_req_data2 = data2;
2727 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2728 			break;
2729 		}
2730 		goto async_event_process_exit;
2731 	}
2732 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2733 		bnxt_ptp_pps_event(bp, data1, data2);
2734 		goto async_event_process_exit;
2735 	}
2736 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2737 		if (bnxt_event_error_report(bp, data1, data2))
2738 			break;
2739 		goto async_event_process_exit;
2740 	}
2741 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2742 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2743 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2744 			if (BNXT_PTP_USE_RTC(bp)) {
2745 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2746 				u64 ns;
2747 
2748 				if (!ptp)
2749 					goto async_event_process_exit;
2750 
2751 				spin_lock_bh(&ptp->ptp_lock);
2752 				bnxt_ptp_update_current_time(bp);
2753 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2754 				       BNXT_PHC_BITS) | ptp->current_time);
2755 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2756 				spin_unlock_bh(&ptp->ptp_lock);
2757 			}
2758 			break;
2759 		}
2760 		goto async_event_process_exit;
2761 	}
2762 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2763 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2764 
2765 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2766 		goto async_event_process_exit;
2767 	}
2768 	default:
2769 		goto async_event_process_exit;
2770 	}
2771 	__bnxt_queue_sp_work(bp);
2772 async_event_process_exit:
2773 	return 0;
2774 }
2775 
2776 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2777 {
2778 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2779 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2780 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2781 				(struct hwrm_fwd_req_cmpl *)txcmp;
2782 
2783 	switch (cmpl_type) {
2784 	case CMPL_BASE_TYPE_HWRM_DONE:
2785 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2786 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2787 		break;
2788 
2789 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2790 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2791 
2792 		if ((vf_id < bp->pf.first_vf_id) ||
2793 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2794 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2795 				   vf_id);
2796 			return -EINVAL;
2797 		}
2798 
2799 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2800 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2801 		break;
2802 
2803 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2804 		bnxt_async_event_process(bp,
2805 					 (struct hwrm_async_event_cmpl *)txcmp);
2806 		break;
2807 
2808 	default:
2809 		break;
2810 	}
2811 
2812 	return 0;
2813 }
2814 
2815 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2816 {
2817 	struct bnxt_napi *bnapi = dev_instance;
2818 	struct bnxt *bp = bnapi->bp;
2819 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2820 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2821 
2822 	cpr->event_ctr++;
2823 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2824 	napi_schedule(&bnapi->napi);
2825 	return IRQ_HANDLED;
2826 }
2827 
2828 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2829 {
2830 	u32 raw_cons = cpr->cp_raw_cons;
2831 	u16 cons = RING_CMP(raw_cons);
2832 	struct tx_cmp *txcmp;
2833 
2834 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2835 
2836 	return TX_CMP_VALID(txcmp, raw_cons);
2837 }
2838 
2839 static irqreturn_t bnxt_inta(int irq, void *dev_instance)
2840 {
2841 	struct bnxt_napi *bnapi = dev_instance;
2842 	struct bnxt *bp = bnapi->bp;
2843 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2844 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2845 	u32 int_status;
2846 
2847 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2848 
2849 	if (!bnxt_has_work(bp, cpr)) {
2850 		int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
2851 		/* return if erroneous interrupt */
2852 		if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
2853 			return IRQ_NONE;
2854 	}
2855 
2856 	/* disable ring IRQ */
2857 	BNXT_CP_DB_IRQ_DIS(cpr->cp_db.doorbell);
2858 
2859 	/* Return here if interrupt is shared and is disabled. */
2860 	if (unlikely(atomic_read(&bp->intr_sem) != 0))
2861 		return IRQ_HANDLED;
2862 
2863 	napi_schedule(&bnapi->napi);
2864 	return IRQ_HANDLED;
2865 }
2866 
2867 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2868 			    int budget)
2869 {
2870 	struct bnxt_napi *bnapi = cpr->bnapi;
2871 	u32 raw_cons = cpr->cp_raw_cons;
2872 	u32 cons;
2873 	int rx_pkts = 0;
2874 	u8 event = 0;
2875 	struct tx_cmp *txcmp;
2876 
2877 	cpr->has_more_work = 0;
2878 	cpr->had_work_done = 1;
2879 	while (1) {
2880 		u8 cmp_type;
2881 		int rc;
2882 
2883 		cons = RING_CMP(raw_cons);
2884 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2885 
2886 		if (!TX_CMP_VALID(txcmp, raw_cons))
2887 			break;
2888 
2889 		/* The valid test of the entry must be done first before
2890 		 * reading any further.
2891 		 */
2892 		dma_rmb();
2893 		cmp_type = TX_CMP_TYPE(txcmp);
2894 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
2895 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
2896 			u32 opaque = txcmp->tx_cmp_opaque;
2897 			struct bnxt_tx_ring_info *txr;
2898 			u16 tx_freed;
2899 
2900 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
2901 			event |= BNXT_TX_CMP_EVENT;
2902 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
2903 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
2904 			else
2905 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
2906 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
2907 				   bp->tx_ring_mask;
2908 			/* return full budget so NAPI will complete. */
2909 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
2910 				rx_pkts = budget;
2911 				raw_cons = NEXT_RAW_CMP(raw_cons);
2912 				if (budget)
2913 					cpr->has_more_work = 1;
2914 				break;
2915 			}
2916 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
2917 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2918 			if (likely(budget))
2919 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2920 			else
2921 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2922 							   &event);
2923 			if (likely(rc >= 0))
2924 				rx_pkts += rc;
2925 			/* Increment rx_pkts when rc is -ENOMEM to count towards
2926 			 * the NAPI budget.  Otherwise, we may potentially loop
2927 			 * here forever if we consistently cannot allocate
2928 			 * buffers.
2929 			 */
2930 			else if (rc == -ENOMEM && budget)
2931 				rx_pkts++;
2932 			else if (rc == -EBUSY)	/* partial completion */
2933 				break;
2934 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
2935 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
2936 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
2937 			bnxt_hwrm_handler(bp, txcmp);
2938 		}
2939 		raw_cons = NEXT_RAW_CMP(raw_cons);
2940 
2941 		if (rx_pkts && rx_pkts == budget) {
2942 			cpr->has_more_work = 1;
2943 			break;
2944 		}
2945 	}
2946 
2947 	if (event & BNXT_REDIRECT_EVENT)
2948 		xdp_do_flush();
2949 
2950 	if (event & BNXT_TX_EVENT) {
2951 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
2952 		u16 prod = txr->tx_prod;
2953 
2954 		/* Sync BD data before updating doorbell */
2955 		wmb();
2956 
2957 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
2958 	}
2959 
2960 	cpr->cp_raw_cons = raw_cons;
2961 	bnapi->events |= event;
2962 	return rx_pkts;
2963 }
2964 
2965 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
2966 				  int budget)
2967 {
2968 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
2969 		bnapi->tx_int(bp, bnapi, budget);
2970 
2971 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
2972 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2973 
2974 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2975 	}
2976 	if (bnapi->events & BNXT_AGG_EVENT) {
2977 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2978 
2979 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2980 	}
2981 	bnapi->events &= BNXT_TX_CMP_EVENT;
2982 }
2983 
2984 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2985 			  int budget)
2986 {
2987 	struct bnxt_napi *bnapi = cpr->bnapi;
2988 	int rx_pkts;
2989 
2990 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
2991 
2992 	/* ACK completion ring before freeing tx ring and producing new
2993 	 * buffers in rx/agg rings to prevent overflowing the completion
2994 	 * ring.
2995 	 */
2996 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
2997 
2998 	__bnxt_poll_work_done(bp, bnapi, budget);
2999 	return rx_pkts;
3000 }
3001 
3002 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3003 {
3004 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3005 	struct bnxt *bp = bnapi->bp;
3006 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3007 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3008 	struct tx_cmp *txcmp;
3009 	struct rx_cmp_ext *rxcmp1;
3010 	u32 cp_cons, tmp_raw_cons;
3011 	u32 raw_cons = cpr->cp_raw_cons;
3012 	bool flush_xdp = false;
3013 	u32 rx_pkts = 0;
3014 	u8 event = 0;
3015 
3016 	while (1) {
3017 		int rc;
3018 
3019 		cp_cons = RING_CMP(raw_cons);
3020 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3021 
3022 		if (!TX_CMP_VALID(txcmp, raw_cons))
3023 			break;
3024 
3025 		/* The valid test of the entry must be done first before
3026 		 * reading any further.
3027 		 */
3028 		dma_rmb();
3029 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3030 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3031 			cp_cons = RING_CMP(tmp_raw_cons);
3032 			rxcmp1 = (struct rx_cmp_ext *)
3033 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3034 
3035 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3036 				break;
3037 
3038 			/* force an error to recycle the buffer */
3039 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3040 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3041 
3042 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3043 			if (likely(rc == -EIO) && budget)
3044 				rx_pkts++;
3045 			else if (rc == -EBUSY)	/* partial completion */
3046 				break;
3047 			if (event & BNXT_REDIRECT_EVENT)
3048 				flush_xdp = true;
3049 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3050 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3051 			bnxt_hwrm_handler(bp, txcmp);
3052 		} else {
3053 			netdev_err(bp->dev,
3054 				   "Invalid completion received on special ring\n");
3055 		}
3056 		raw_cons = NEXT_RAW_CMP(raw_cons);
3057 
3058 		if (rx_pkts == budget)
3059 			break;
3060 	}
3061 
3062 	cpr->cp_raw_cons = raw_cons;
3063 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3064 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3065 
3066 	if (event & BNXT_AGG_EVENT)
3067 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3068 	if (flush_xdp)
3069 		xdp_do_flush();
3070 
3071 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3072 		napi_complete_done(napi, rx_pkts);
3073 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3074 	}
3075 	return rx_pkts;
3076 }
3077 
3078 static int bnxt_poll(struct napi_struct *napi, int budget)
3079 {
3080 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3081 	struct bnxt *bp = bnapi->bp;
3082 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3083 	int work_done = 0;
3084 
3085 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3086 		napi_complete(napi);
3087 		return 0;
3088 	}
3089 	while (1) {
3090 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3091 
3092 		if (work_done >= budget) {
3093 			if (!budget)
3094 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3095 			break;
3096 		}
3097 
3098 		if (!bnxt_has_work(bp, cpr)) {
3099 			if (napi_complete_done(napi, work_done))
3100 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3101 			break;
3102 		}
3103 	}
3104 	if (bp->flags & BNXT_FLAG_DIM) {
3105 		struct dim_sample dim_sample = {};
3106 
3107 		dim_update_sample(cpr->event_ctr,
3108 				  cpr->rx_packets,
3109 				  cpr->rx_bytes,
3110 				  &dim_sample);
3111 		net_dim(&cpr->dim, dim_sample);
3112 	}
3113 	return work_done;
3114 }
3115 
3116 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3117 {
3118 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3119 	int i, work_done = 0;
3120 
3121 	for (i = 0; i < cpr->cp_ring_count; i++) {
3122 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3123 
3124 		if (cpr2->had_nqe_notify) {
3125 			work_done += __bnxt_poll_work(bp, cpr2,
3126 						      budget - work_done);
3127 			cpr->has_more_work |= cpr2->has_more_work;
3128 		}
3129 	}
3130 	return work_done;
3131 }
3132 
3133 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3134 				 u64 dbr_type, int budget)
3135 {
3136 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3137 	int i;
3138 
3139 	for (i = 0; i < cpr->cp_ring_count; i++) {
3140 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3141 		struct bnxt_db_info *db;
3142 
3143 		if (cpr2->had_work_done) {
3144 			u32 tgl = 0;
3145 
3146 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3147 				cpr2->had_nqe_notify = 0;
3148 				tgl = cpr2->toggle;
3149 			}
3150 			db = &cpr2->cp_db;
3151 			bnxt_writeq(bp,
3152 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3153 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3154 				    db->doorbell);
3155 			cpr2->had_work_done = 0;
3156 		}
3157 	}
3158 	__bnxt_poll_work_done(bp, bnapi, budget);
3159 }
3160 
3161 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3162 {
3163 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3164 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3165 	struct bnxt_cp_ring_info *cpr_rx;
3166 	u32 raw_cons = cpr->cp_raw_cons;
3167 	struct bnxt *bp = bnapi->bp;
3168 	struct nqe_cn *nqcmp;
3169 	int work_done = 0;
3170 	u32 cons;
3171 
3172 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3173 		napi_complete(napi);
3174 		return 0;
3175 	}
3176 	if (cpr->has_more_work) {
3177 		cpr->has_more_work = 0;
3178 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3179 	}
3180 	while (1) {
3181 		u16 type;
3182 
3183 		cons = RING_CMP(raw_cons);
3184 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3185 
3186 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3187 			if (cpr->has_more_work)
3188 				break;
3189 
3190 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3191 					     budget);
3192 			cpr->cp_raw_cons = raw_cons;
3193 			if (napi_complete_done(napi, work_done))
3194 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3195 						  cpr->cp_raw_cons);
3196 			goto poll_done;
3197 		}
3198 
3199 		/* The valid test of the entry must be done first before
3200 		 * reading any further.
3201 		 */
3202 		dma_rmb();
3203 
3204 		type = le16_to_cpu(nqcmp->type);
3205 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3206 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3207 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3208 			struct bnxt_cp_ring_info *cpr2;
3209 
3210 			/* No more budget for RX work */
3211 			if (budget && work_done >= budget &&
3212 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3213 				break;
3214 
3215 			idx = BNXT_NQ_HDL_IDX(idx);
3216 			cpr2 = &cpr->cp_ring_arr[idx];
3217 			cpr2->had_nqe_notify = 1;
3218 			cpr2->toggle = NQE_CN_TOGGLE(type);
3219 			work_done += __bnxt_poll_work(bp, cpr2,
3220 						      budget - work_done);
3221 			cpr->has_more_work |= cpr2->has_more_work;
3222 		} else {
3223 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3224 		}
3225 		raw_cons = NEXT_RAW_CMP(raw_cons);
3226 	}
3227 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3228 	if (raw_cons != cpr->cp_raw_cons) {
3229 		cpr->cp_raw_cons = raw_cons;
3230 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3231 	}
3232 poll_done:
3233 	cpr_rx = &cpr->cp_ring_arr[0];
3234 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3235 	    (bp->flags & BNXT_FLAG_DIM)) {
3236 		struct dim_sample dim_sample = {};
3237 
3238 		dim_update_sample(cpr->event_ctr,
3239 				  cpr_rx->rx_packets,
3240 				  cpr_rx->rx_bytes,
3241 				  &dim_sample);
3242 		net_dim(&cpr->dim, dim_sample);
3243 	}
3244 	return work_done;
3245 }
3246 
3247 static void bnxt_free_tx_skbs(struct bnxt *bp)
3248 {
3249 	int i, max_idx;
3250 	struct pci_dev *pdev = bp->pdev;
3251 
3252 	if (!bp->tx_ring)
3253 		return;
3254 
3255 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3256 	for (i = 0; i < bp->tx_nr_rings; i++) {
3257 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3258 		int j;
3259 
3260 		if (!txr->tx_buf_ring)
3261 			continue;
3262 
3263 		for (j = 0; j < max_idx;) {
3264 			struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
3265 			struct sk_buff *skb;
3266 			int k, last;
3267 
3268 			if (i < bp->tx_nr_rings_xdp &&
3269 			    tx_buf->action == XDP_REDIRECT) {
3270 				dma_unmap_single(&pdev->dev,
3271 					dma_unmap_addr(tx_buf, mapping),
3272 					dma_unmap_len(tx_buf, len),
3273 					DMA_TO_DEVICE);
3274 				xdp_return_frame(tx_buf->xdpf);
3275 				tx_buf->action = 0;
3276 				tx_buf->xdpf = NULL;
3277 				j++;
3278 				continue;
3279 			}
3280 
3281 			skb = tx_buf->skb;
3282 			if (!skb) {
3283 				j++;
3284 				continue;
3285 			}
3286 
3287 			tx_buf->skb = NULL;
3288 
3289 			if (tx_buf->is_push) {
3290 				dev_kfree_skb(skb);
3291 				j += 2;
3292 				continue;
3293 			}
3294 
3295 			dma_unmap_single(&pdev->dev,
3296 					 dma_unmap_addr(tx_buf, mapping),
3297 					 skb_headlen(skb),
3298 					 DMA_TO_DEVICE);
3299 
3300 			last = tx_buf->nr_frags;
3301 			j += 2;
3302 			for (k = 0; k < last; k++, j++) {
3303 				int ring_idx = j & bp->tx_ring_mask;
3304 				skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
3305 
3306 				tx_buf = &txr->tx_buf_ring[ring_idx];
3307 				dma_unmap_page(
3308 					&pdev->dev,
3309 					dma_unmap_addr(tx_buf, mapping),
3310 					skb_frag_size(frag), DMA_TO_DEVICE);
3311 			}
3312 			dev_kfree_skb(skb);
3313 		}
3314 		netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
3315 	}
3316 }
3317 
3318 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, int ring_nr)
3319 {
3320 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
3321 	struct pci_dev *pdev = bp->pdev;
3322 	struct bnxt_tpa_idx_map *map;
3323 	int i, max_idx, max_agg_idx;
3324 
3325 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3326 	max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3327 	if (!rxr->rx_tpa)
3328 		goto skip_rx_tpa_free;
3329 
3330 	for (i = 0; i < bp->max_tpa; i++) {
3331 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3332 		u8 *data = tpa_info->data;
3333 
3334 		if (!data)
3335 			continue;
3336 
3337 		dma_unmap_single_attrs(&pdev->dev, tpa_info->mapping,
3338 				       bp->rx_buf_use_size, bp->rx_dir,
3339 				       DMA_ATTR_WEAK_ORDERING);
3340 
3341 		tpa_info->data = NULL;
3342 
3343 		skb_free_frag(data);
3344 	}
3345 
3346 skip_rx_tpa_free:
3347 	if (!rxr->rx_buf_ring)
3348 		goto skip_rx_buf_free;
3349 
3350 	for (i = 0; i < max_idx; i++) {
3351 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3352 		dma_addr_t mapping = rx_buf->mapping;
3353 		void *data = rx_buf->data;
3354 
3355 		if (!data)
3356 			continue;
3357 
3358 		rx_buf->data = NULL;
3359 		if (BNXT_RX_PAGE_MODE(bp)) {
3360 			page_pool_recycle_direct(rxr->page_pool, data);
3361 		} else {
3362 			dma_unmap_single_attrs(&pdev->dev, mapping,
3363 					       bp->rx_buf_use_size, bp->rx_dir,
3364 					       DMA_ATTR_WEAK_ORDERING);
3365 			skb_free_frag(data);
3366 		}
3367 	}
3368 
3369 skip_rx_buf_free:
3370 	if (!rxr->rx_agg_ring)
3371 		goto skip_rx_agg_free;
3372 
3373 	for (i = 0; i < max_agg_idx; i++) {
3374 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3375 		struct page *page = rx_agg_buf->page;
3376 
3377 		if (!page)
3378 			continue;
3379 
3380 		rx_agg_buf->page = NULL;
3381 		__clear_bit(i, rxr->rx_agg_bmap);
3382 
3383 		page_pool_recycle_direct(rxr->page_pool, page);
3384 	}
3385 
3386 skip_rx_agg_free:
3387 	map = rxr->rx_tpa_idx_map;
3388 	if (map)
3389 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3390 }
3391 
3392 static void bnxt_free_rx_skbs(struct bnxt *bp)
3393 {
3394 	int i;
3395 
3396 	if (!bp->rx_ring)
3397 		return;
3398 
3399 	for (i = 0; i < bp->rx_nr_rings; i++)
3400 		bnxt_free_one_rx_ring_skbs(bp, i);
3401 }
3402 
3403 static void bnxt_free_skbs(struct bnxt *bp)
3404 {
3405 	bnxt_free_tx_skbs(bp);
3406 	bnxt_free_rx_skbs(bp);
3407 }
3408 
3409 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3410 {
3411 	u8 init_val = ctxm->init_value;
3412 	u16 offset = ctxm->init_offset;
3413 	u8 *p2 = p;
3414 	int i;
3415 
3416 	if (!init_val)
3417 		return;
3418 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3419 		memset(p, init_val, len);
3420 		return;
3421 	}
3422 	for (i = 0; i < len; i += ctxm->entry_size)
3423 		*(p2 + i + offset) = init_val;
3424 }
3425 
3426 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3427 {
3428 	struct pci_dev *pdev = bp->pdev;
3429 	int i;
3430 
3431 	if (!rmem->pg_arr)
3432 		goto skip_pages;
3433 
3434 	for (i = 0; i < rmem->nr_pages; i++) {
3435 		if (!rmem->pg_arr[i])
3436 			continue;
3437 
3438 		dma_free_coherent(&pdev->dev, rmem->page_size,
3439 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3440 
3441 		rmem->pg_arr[i] = NULL;
3442 	}
3443 skip_pages:
3444 	if (rmem->pg_tbl) {
3445 		size_t pg_tbl_size = rmem->nr_pages * 8;
3446 
3447 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3448 			pg_tbl_size = rmem->page_size;
3449 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3450 				  rmem->pg_tbl, rmem->pg_tbl_map);
3451 		rmem->pg_tbl = NULL;
3452 	}
3453 	if (rmem->vmem_size && *rmem->vmem) {
3454 		vfree(*rmem->vmem);
3455 		*rmem->vmem = NULL;
3456 	}
3457 }
3458 
3459 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3460 {
3461 	struct pci_dev *pdev = bp->pdev;
3462 	u64 valid_bit = 0;
3463 	int i;
3464 
3465 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3466 		valid_bit = PTU_PTE_VALID;
3467 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3468 		size_t pg_tbl_size = rmem->nr_pages * 8;
3469 
3470 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3471 			pg_tbl_size = rmem->page_size;
3472 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3473 						  &rmem->pg_tbl_map,
3474 						  GFP_KERNEL);
3475 		if (!rmem->pg_tbl)
3476 			return -ENOMEM;
3477 	}
3478 
3479 	for (i = 0; i < rmem->nr_pages; i++) {
3480 		u64 extra_bits = valid_bit;
3481 
3482 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3483 						     rmem->page_size,
3484 						     &rmem->dma_arr[i],
3485 						     GFP_KERNEL);
3486 		if (!rmem->pg_arr[i])
3487 			return -ENOMEM;
3488 
3489 		if (rmem->ctx_mem)
3490 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3491 					  rmem->page_size);
3492 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3493 			if (i == rmem->nr_pages - 2 &&
3494 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3495 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3496 			else if (i == rmem->nr_pages - 1 &&
3497 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3498 				extra_bits |= PTU_PTE_LAST;
3499 			rmem->pg_tbl[i] =
3500 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3501 		}
3502 	}
3503 
3504 	if (rmem->vmem_size) {
3505 		*rmem->vmem = vzalloc(rmem->vmem_size);
3506 		if (!(*rmem->vmem))
3507 			return -ENOMEM;
3508 	}
3509 	return 0;
3510 }
3511 
3512 static void bnxt_free_tpa_info(struct bnxt *bp)
3513 {
3514 	int i, j;
3515 
3516 	for (i = 0; i < bp->rx_nr_rings; i++) {
3517 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3518 
3519 		kfree(rxr->rx_tpa_idx_map);
3520 		rxr->rx_tpa_idx_map = NULL;
3521 		if (rxr->rx_tpa) {
3522 			for (j = 0; j < bp->max_tpa; j++) {
3523 				kfree(rxr->rx_tpa[j].agg_arr);
3524 				rxr->rx_tpa[j].agg_arr = NULL;
3525 			}
3526 		}
3527 		kfree(rxr->rx_tpa);
3528 		rxr->rx_tpa = NULL;
3529 	}
3530 }
3531 
3532 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3533 {
3534 	int i, j;
3535 
3536 	bp->max_tpa = MAX_TPA;
3537 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3538 		if (!bp->max_tpa_v2)
3539 			return 0;
3540 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3541 	}
3542 
3543 	for (i = 0; i < bp->rx_nr_rings; i++) {
3544 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3545 		struct rx_agg_cmp *agg;
3546 
3547 		rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3548 				      GFP_KERNEL);
3549 		if (!rxr->rx_tpa)
3550 			return -ENOMEM;
3551 
3552 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3553 			continue;
3554 		for (j = 0; j < bp->max_tpa; j++) {
3555 			agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3556 			if (!agg)
3557 				return -ENOMEM;
3558 			rxr->rx_tpa[j].agg_arr = agg;
3559 		}
3560 		rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3561 					      GFP_KERNEL);
3562 		if (!rxr->rx_tpa_idx_map)
3563 			return -ENOMEM;
3564 	}
3565 	return 0;
3566 }
3567 
3568 static void bnxt_free_rx_rings(struct bnxt *bp)
3569 {
3570 	int i;
3571 
3572 	if (!bp->rx_ring)
3573 		return;
3574 
3575 	bnxt_free_tpa_info(bp);
3576 	for (i = 0; i < bp->rx_nr_rings; i++) {
3577 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3578 		struct bnxt_ring_struct *ring;
3579 
3580 		if (rxr->xdp_prog)
3581 			bpf_prog_put(rxr->xdp_prog);
3582 
3583 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3584 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3585 
3586 		page_pool_destroy(rxr->page_pool);
3587 		rxr->page_pool = NULL;
3588 
3589 		kfree(rxr->rx_agg_bmap);
3590 		rxr->rx_agg_bmap = NULL;
3591 
3592 		ring = &rxr->rx_ring_struct;
3593 		bnxt_free_ring(bp, &ring->ring_mem);
3594 
3595 		ring = &rxr->rx_agg_ring_struct;
3596 		bnxt_free_ring(bp, &ring->ring_mem);
3597 	}
3598 }
3599 
3600 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3601 				   struct bnxt_rx_ring_info *rxr,
3602 				   int numa_node)
3603 {
3604 	struct page_pool_params pp = { 0 };
3605 
3606 	pp.pool_size = bp->rx_agg_ring_size;
3607 	if (BNXT_RX_PAGE_MODE(bp))
3608 		pp.pool_size += bp->rx_ring_size;
3609 	pp.nid = numa_node;
3610 	pp.napi = &rxr->bnapi->napi;
3611 	pp.netdev = bp->dev;
3612 	pp.dev = &bp->pdev->dev;
3613 	pp.dma_dir = bp->rx_dir;
3614 	pp.max_len = PAGE_SIZE;
3615 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3616 
3617 	rxr->page_pool = page_pool_create(&pp);
3618 	if (IS_ERR(rxr->page_pool)) {
3619 		int err = PTR_ERR(rxr->page_pool);
3620 
3621 		rxr->page_pool = NULL;
3622 		return err;
3623 	}
3624 	return 0;
3625 }
3626 
3627 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3628 {
3629 	int numa_node = dev_to_node(&bp->pdev->dev);
3630 	int i, rc = 0, agg_rings = 0, cpu;
3631 
3632 	if (!bp->rx_ring)
3633 		return -ENOMEM;
3634 
3635 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3636 		agg_rings = 1;
3637 
3638 	for (i = 0; i < bp->rx_nr_rings; i++) {
3639 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3640 		struct bnxt_ring_struct *ring;
3641 		int cpu_node;
3642 
3643 		ring = &rxr->rx_ring_struct;
3644 
3645 		cpu = cpumask_local_spread(i, numa_node);
3646 		cpu_node = cpu_to_node(cpu);
3647 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3648 			   i, cpu_node);
3649 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3650 		if (rc)
3651 			return rc;
3652 
3653 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3654 		if (rc < 0)
3655 			return rc;
3656 
3657 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3658 						MEM_TYPE_PAGE_POOL,
3659 						rxr->page_pool);
3660 		if (rc) {
3661 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3662 			return rc;
3663 		}
3664 
3665 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3666 		if (rc)
3667 			return rc;
3668 
3669 		ring->grp_idx = i;
3670 		if (agg_rings) {
3671 			u16 mem_size;
3672 
3673 			ring = &rxr->rx_agg_ring_struct;
3674 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3675 			if (rc)
3676 				return rc;
3677 
3678 			ring->grp_idx = i;
3679 			rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3680 			mem_size = rxr->rx_agg_bmap_size / 8;
3681 			rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3682 			if (!rxr->rx_agg_bmap)
3683 				return -ENOMEM;
3684 		}
3685 	}
3686 	if (bp->flags & BNXT_FLAG_TPA)
3687 		rc = bnxt_alloc_tpa_info(bp);
3688 	return rc;
3689 }
3690 
3691 static void bnxt_free_tx_rings(struct bnxt *bp)
3692 {
3693 	int i;
3694 	struct pci_dev *pdev = bp->pdev;
3695 
3696 	if (!bp->tx_ring)
3697 		return;
3698 
3699 	for (i = 0; i < bp->tx_nr_rings; i++) {
3700 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3701 		struct bnxt_ring_struct *ring;
3702 
3703 		if (txr->tx_push) {
3704 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3705 					  txr->tx_push, txr->tx_push_mapping);
3706 			txr->tx_push = NULL;
3707 		}
3708 
3709 		ring = &txr->tx_ring_struct;
3710 
3711 		bnxt_free_ring(bp, &ring->ring_mem);
3712 	}
3713 }
3714 
3715 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3716 	((tc) * (bp)->tx_nr_rings_per_tc)
3717 
3718 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3719 	((tx) % (bp)->tx_nr_rings_per_tc)
3720 
3721 #define BNXT_RING_TO_TC(bp, tx)		\
3722 	((tx) / (bp)->tx_nr_rings_per_tc)
3723 
3724 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3725 {
3726 	int i, j, rc;
3727 	struct pci_dev *pdev = bp->pdev;
3728 
3729 	bp->tx_push_size = 0;
3730 	if (bp->tx_push_thresh) {
3731 		int push_size;
3732 
3733 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3734 					bp->tx_push_thresh);
3735 
3736 		if (push_size > 256) {
3737 			push_size = 0;
3738 			bp->tx_push_thresh = 0;
3739 		}
3740 
3741 		bp->tx_push_size = push_size;
3742 	}
3743 
3744 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3745 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3746 		struct bnxt_ring_struct *ring;
3747 		u8 qidx;
3748 
3749 		ring = &txr->tx_ring_struct;
3750 
3751 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3752 		if (rc)
3753 			return rc;
3754 
3755 		ring->grp_idx = txr->bnapi->index;
3756 		if (bp->tx_push_size) {
3757 			dma_addr_t mapping;
3758 
3759 			/* One pre-allocated DMA buffer to backup
3760 			 * TX push operation
3761 			 */
3762 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3763 						bp->tx_push_size,
3764 						&txr->tx_push_mapping,
3765 						GFP_KERNEL);
3766 
3767 			if (!txr->tx_push)
3768 				return -ENOMEM;
3769 
3770 			mapping = txr->tx_push_mapping +
3771 				sizeof(struct tx_push_bd);
3772 			txr->data_mapping = cpu_to_le64(mapping);
3773 		}
3774 		qidx = bp->tc_to_qidx[j];
3775 		ring->queue_id = bp->q_info[qidx].queue_id;
3776 		spin_lock_init(&txr->xdp_tx_lock);
3777 		if (i < bp->tx_nr_rings_xdp)
3778 			continue;
3779 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
3780 			j++;
3781 	}
3782 	return 0;
3783 }
3784 
3785 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
3786 {
3787 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3788 
3789 	kfree(cpr->cp_desc_ring);
3790 	cpr->cp_desc_ring = NULL;
3791 	ring->ring_mem.pg_arr = NULL;
3792 	kfree(cpr->cp_desc_mapping);
3793 	cpr->cp_desc_mapping = NULL;
3794 	ring->ring_mem.dma_arr = NULL;
3795 }
3796 
3797 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
3798 {
3799 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
3800 	if (!cpr->cp_desc_ring)
3801 		return -ENOMEM;
3802 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
3803 				       GFP_KERNEL);
3804 	if (!cpr->cp_desc_mapping)
3805 		return -ENOMEM;
3806 	return 0;
3807 }
3808 
3809 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
3810 {
3811 	int i;
3812 
3813 	if (!bp->bnapi)
3814 		return;
3815 	for (i = 0; i < bp->cp_nr_rings; i++) {
3816 		struct bnxt_napi *bnapi = bp->bnapi[i];
3817 
3818 		if (!bnapi)
3819 			continue;
3820 		bnxt_free_cp_arrays(&bnapi->cp_ring);
3821 	}
3822 }
3823 
3824 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
3825 {
3826 	int i, n = bp->cp_nr_pages;
3827 
3828 	for (i = 0; i < bp->cp_nr_rings; i++) {
3829 		struct bnxt_napi *bnapi = bp->bnapi[i];
3830 		int rc;
3831 
3832 		if (!bnapi)
3833 			continue;
3834 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
3835 		if (rc)
3836 			return rc;
3837 	}
3838 	return 0;
3839 }
3840 
3841 static void bnxt_free_cp_rings(struct bnxt *bp)
3842 {
3843 	int i;
3844 
3845 	if (!bp->bnapi)
3846 		return;
3847 
3848 	for (i = 0; i < bp->cp_nr_rings; i++) {
3849 		struct bnxt_napi *bnapi = bp->bnapi[i];
3850 		struct bnxt_cp_ring_info *cpr;
3851 		struct bnxt_ring_struct *ring;
3852 		int j;
3853 
3854 		if (!bnapi)
3855 			continue;
3856 
3857 		cpr = &bnapi->cp_ring;
3858 		ring = &cpr->cp_ring_struct;
3859 
3860 		bnxt_free_ring(bp, &ring->ring_mem);
3861 
3862 		if (!cpr->cp_ring_arr)
3863 			continue;
3864 
3865 		for (j = 0; j < cpr->cp_ring_count; j++) {
3866 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
3867 
3868 			ring = &cpr2->cp_ring_struct;
3869 			bnxt_free_ring(bp, &ring->ring_mem);
3870 			bnxt_free_cp_arrays(cpr2);
3871 		}
3872 		kfree(cpr->cp_ring_arr);
3873 		cpr->cp_ring_arr = NULL;
3874 		cpr->cp_ring_count = 0;
3875 	}
3876 }
3877 
3878 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
3879 				  struct bnxt_cp_ring_info *cpr)
3880 {
3881 	struct bnxt_ring_mem_info *rmem;
3882 	struct bnxt_ring_struct *ring;
3883 	int rc;
3884 
3885 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
3886 	if (rc) {
3887 		bnxt_free_cp_arrays(cpr);
3888 		return -ENOMEM;
3889 	}
3890 	ring = &cpr->cp_ring_struct;
3891 	rmem = &ring->ring_mem;
3892 	rmem->nr_pages = bp->cp_nr_pages;
3893 	rmem->page_size = HW_CMPD_RING_SIZE;
3894 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
3895 	rmem->dma_arr = cpr->cp_desc_mapping;
3896 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
3897 	rc = bnxt_alloc_ring(bp, rmem);
3898 	if (rc) {
3899 		bnxt_free_ring(bp, rmem);
3900 		bnxt_free_cp_arrays(cpr);
3901 	}
3902 	return rc;
3903 }
3904 
3905 static int bnxt_alloc_cp_rings(struct bnxt *bp)
3906 {
3907 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
3908 	int i, j, rc, ulp_base_vec, ulp_msix;
3909 	int tcs = bp->num_tc;
3910 
3911 	if (!tcs)
3912 		tcs = 1;
3913 	ulp_msix = bnxt_get_ulp_msix_num(bp);
3914 	ulp_base_vec = bnxt_get_ulp_msix_base(bp);
3915 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
3916 		struct bnxt_napi *bnapi = bp->bnapi[i];
3917 		struct bnxt_cp_ring_info *cpr, *cpr2;
3918 		struct bnxt_ring_struct *ring;
3919 		int cp_count = 0, k;
3920 		int rx = 0, tx = 0;
3921 
3922 		if (!bnapi)
3923 			continue;
3924 
3925 		cpr = &bnapi->cp_ring;
3926 		cpr->bnapi = bnapi;
3927 		ring = &cpr->cp_ring_struct;
3928 
3929 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3930 		if (rc)
3931 			return rc;
3932 
3933 		if (ulp_msix && i >= ulp_base_vec)
3934 			ring->map_idx = i + ulp_msix;
3935 		else
3936 			ring->map_idx = i;
3937 
3938 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3939 			continue;
3940 
3941 		if (i < bp->rx_nr_rings) {
3942 			cp_count++;
3943 			rx = 1;
3944 		}
3945 		if (i < bp->tx_nr_rings_xdp) {
3946 			cp_count++;
3947 			tx = 1;
3948 		} else if ((sh && i < bp->tx_nr_rings) ||
3949 			 (!sh && i >= bp->rx_nr_rings)) {
3950 			cp_count += tcs;
3951 			tx = 1;
3952 		}
3953 
3954 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
3955 					   GFP_KERNEL);
3956 		if (!cpr->cp_ring_arr)
3957 			return -ENOMEM;
3958 		cpr->cp_ring_count = cp_count;
3959 
3960 		for (k = 0; k < cp_count; k++) {
3961 			cpr2 = &cpr->cp_ring_arr[k];
3962 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
3963 			if (rc)
3964 				return rc;
3965 			cpr2->bnapi = bnapi;
3966 			cpr2->cp_idx = k;
3967 			if (!k && rx) {
3968 				bp->rx_ring[i].rx_cpr = cpr2;
3969 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
3970 			} else {
3971 				int n, tc = k - rx;
3972 
3973 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
3974 				bp->tx_ring[n].tx_cpr = cpr2;
3975 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
3976 			}
3977 		}
3978 		if (tx)
3979 			j++;
3980 	}
3981 	return 0;
3982 }
3983 
3984 static void bnxt_init_ring_struct(struct bnxt *bp)
3985 {
3986 	int i, j;
3987 
3988 	for (i = 0; i < bp->cp_nr_rings; i++) {
3989 		struct bnxt_napi *bnapi = bp->bnapi[i];
3990 		struct bnxt_ring_mem_info *rmem;
3991 		struct bnxt_cp_ring_info *cpr;
3992 		struct bnxt_rx_ring_info *rxr;
3993 		struct bnxt_tx_ring_info *txr;
3994 		struct bnxt_ring_struct *ring;
3995 
3996 		if (!bnapi)
3997 			continue;
3998 
3999 		cpr = &bnapi->cp_ring;
4000 		ring = &cpr->cp_ring_struct;
4001 		rmem = &ring->ring_mem;
4002 		rmem->nr_pages = bp->cp_nr_pages;
4003 		rmem->page_size = HW_CMPD_RING_SIZE;
4004 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4005 		rmem->dma_arr = cpr->cp_desc_mapping;
4006 		rmem->vmem_size = 0;
4007 
4008 		rxr = bnapi->rx_ring;
4009 		if (!rxr)
4010 			goto skip_rx;
4011 
4012 		ring = &rxr->rx_ring_struct;
4013 		rmem = &ring->ring_mem;
4014 		rmem->nr_pages = bp->rx_nr_pages;
4015 		rmem->page_size = HW_RXBD_RING_SIZE;
4016 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4017 		rmem->dma_arr = rxr->rx_desc_mapping;
4018 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4019 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4020 
4021 		ring = &rxr->rx_agg_ring_struct;
4022 		rmem = &ring->ring_mem;
4023 		rmem->nr_pages = bp->rx_agg_nr_pages;
4024 		rmem->page_size = HW_RXBD_RING_SIZE;
4025 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4026 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4027 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4028 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4029 
4030 skip_rx:
4031 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4032 			ring = &txr->tx_ring_struct;
4033 			rmem = &ring->ring_mem;
4034 			rmem->nr_pages = bp->tx_nr_pages;
4035 			rmem->page_size = HW_TXBD_RING_SIZE;
4036 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4037 			rmem->dma_arr = txr->tx_desc_mapping;
4038 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4039 			rmem->vmem = (void **)&txr->tx_buf_ring;
4040 		}
4041 	}
4042 }
4043 
4044 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4045 {
4046 	int i;
4047 	u32 prod;
4048 	struct rx_bd **rx_buf_ring;
4049 
4050 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4051 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4052 		int j;
4053 		struct rx_bd *rxbd;
4054 
4055 		rxbd = rx_buf_ring[i];
4056 		if (!rxbd)
4057 			continue;
4058 
4059 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4060 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4061 			rxbd->rx_bd_opaque = prod;
4062 		}
4063 	}
4064 }
4065 
4066 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4067 {
4068 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4069 	struct net_device *dev = bp->dev;
4070 	u32 prod;
4071 	int i;
4072 
4073 	prod = rxr->rx_prod;
4074 	for (i = 0; i < bp->rx_ring_size; i++) {
4075 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4076 			netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
4077 				    ring_nr, i, bp->rx_ring_size);
4078 			break;
4079 		}
4080 		prod = NEXT_RX(prod);
4081 	}
4082 	rxr->rx_prod = prod;
4083 
4084 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4085 		return 0;
4086 
4087 	prod = rxr->rx_agg_prod;
4088 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4089 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
4090 			netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
4091 				    ring_nr, i, bp->rx_ring_size);
4092 			break;
4093 		}
4094 		prod = NEXT_RX_AGG(prod);
4095 	}
4096 	rxr->rx_agg_prod = prod;
4097 
4098 	if (rxr->rx_tpa) {
4099 		dma_addr_t mapping;
4100 		u8 *data;
4101 
4102 		for (i = 0; i < bp->max_tpa; i++) {
4103 			data = __bnxt_alloc_rx_frag(bp, &mapping, GFP_KERNEL);
4104 			if (!data)
4105 				return -ENOMEM;
4106 
4107 			rxr->rx_tpa[i].data = data;
4108 			rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4109 			rxr->rx_tpa[i].mapping = mapping;
4110 		}
4111 	}
4112 	return 0;
4113 }
4114 
4115 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4116 {
4117 	struct bnxt_rx_ring_info *rxr;
4118 	struct bnxt_ring_struct *ring;
4119 	u32 type;
4120 
4121 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4122 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4123 
4124 	if (NET_IP_ALIGN == 2)
4125 		type |= RX_BD_FLAGS_SOP;
4126 
4127 	rxr = &bp->rx_ring[ring_nr];
4128 	ring = &rxr->rx_ring_struct;
4129 	bnxt_init_rxbd_pages(ring, type);
4130 
4131 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4132 			     &rxr->bnapi->napi);
4133 
4134 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4135 		bpf_prog_add(bp->xdp_prog, 1);
4136 		rxr->xdp_prog = bp->xdp_prog;
4137 	}
4138 	ring->fw_ring_id = INVALID_HW_RING_ID;
4139 
4140 	ring = &rxr->rx_agg_ring_struct;
4141 	ring->fw_ring_id = INVALID_HW_RING_ID;
4142 
4143 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4144 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4145 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4146 
4147 		bnxt_init_rxbd_pages(ring, type);
4148 	}
4149 
4150 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4151 }
4152 
4153 static void bnxt_init_cp_rings(struct bnxt *bp)
4154 {
4155 	int i, j;
4156 
4157 	for (i = 0; i < bp->cp_nr_rings; i++) {
4158 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4159 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4160 
4161 		ring->fw_ring_id = INVALID_HW_RING_ID;
4162 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4163 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4164 		if (!cpr->cp_ring_arr)
4165 			continue;
4166 		for (j = 0; j < cpr->cp_ring_count; j++) {
4167 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4168 
4169 			ring = &cpr2->cp_ring_struct;
4170 			ring->fw_ring_id = INVALID_HW_RING_ID;
4171 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4172 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4173 		}
4174 	}
4175 }
4176 
4177 static int bnxt_init_rx_rings(struct bnxt *bp)
4178 {
4179 	int i, rc = 0;
4180 
4181 	if (BNXT_RX_PAGE_MODE(bp)) {
4182 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4183 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4184 	} else {
4185 		bp->rx_offset = BNXT_RX_OFFSET;
4186 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4187 	}
4188 
4189 	for (i = 0; i < bp->rx_nr_rings; i++) {
4190 		rc = bnxt_init_one_rx_ring(bp, i);
4191 		if (rc)
4192 			break;
4193 	}
4194 
4195 	return rc;
4196 }
4197 
4198 static int bnxt_init_tx_rings(struct bnxt *bp)
4199 {
4200 	u16 i;
4201 
4202 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4203 				   BNXT_MIN_TX_DESC_CNT);
4204 
4205 	for (i = 0; i < bp->tx_nr_rings; i++) {
4206 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4207 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4208 
4209 		ring->fw_ring_id = INVALID_HW_RING_ID;
4210 
4211 		if (i >= bp->tx_nr_rings_xdp)
4212 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4213 					     NETDEV_QUEUE_TYPE_TX,
4214 					     &txr->bnapi->napi);
4215 	}
4216 
4217 	return 0;
4218 }
4219 
4220 static void bnxt_free_ring_grps(struct bnxt *bp)
4221 {
4222 	kfree(bp->grp_info);
4223 	bp->grp_info = NULL;
4224 }
4225 
4226 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4227 {
4228 	int i;
4229 
4230 	if (irq_re_init) {
4231 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4232 				       sizeof(struct bnxt_ring_grp_info),
4233 				       GFP_KERNEL);
4234 		if (!bp->grp_info)
4235 			return -ENOMEM;
4236 	}
4237 	for (i = 0; i < bp->cp_nr_rings; i++) {
4238 		if (irq_re_init)
4239 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4240 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4241 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4242 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4243 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4244 	}
4245 	return 0;
4246 }
4247 
4248 static void bnxt_free_vnics(struct bnxt *bp)
4249 {
4250 	kfree(bp->vnic_info);
4251 	bp->vnic_info = NULL;
4252 	bp->nr_vnics = 0;
4253 }
4254 
4255 static int bnxt_alloc_vnics(struct bnxt *bp)
4256 {
4257 	int num_vnics = 1;
4258 
4259 #ifdef CONFIG_RFS_ACCEL
4260 	if (bp->flags & BNXT_FLAG_RFS) {
4261 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4262 			num_vnics++;
4263 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4264 			num_vnics += bp->rx_nr_rings;
4265 	}
4266 #endif
4267 
4268 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4269 		num_vnics++;
4270 
4271 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4272 				GFP_KERNEL);
4273 	if (!bp->vnic_info)
4274 		return -ENOMEM;
4275 
4276 	bp->nr_vnics = num_vnics;
4277 	return 0;
4278 }
4279 
4280 static void bnxt_init_vnics(struct bnxt *bp)
4281 {
4282 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4283 	int i;
4284 
4285 	for (i = 0; i < bp->nr_vnics; i++) {
4286 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4287 		int j;
4288 
4289 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4290 		vnic->vnic_id = i;
4291 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4292 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4293 
4294 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4295 
4296 		if (bp->vnic_info[i].rss_hash_key) {
4297 			if (i == BNXT_VNIC_DEFAULT) {
4298 				u8 *key = (void *)vnic->rss_hash_key;
4299 				int k;
4300 
4301 				if (!bp->rss_hash_key_valid &&
4302 				    !bp->rss_hash_key_updated) {
4303 					get_random_bytes(bp->rss_hash_key,
4304 							 HW_HASH_KEY_SIZE);
4305 					bp->rss_hash_key_updated = true;
4306 				}
4307 
4308 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4309 				       HW_HASH_KEY_SIZE);
4310 
4311 				if (!bp->rss_hash_key_updated)
4312 					continue;
4313 
4314 				bp->rss_hash_key_updated = false;
4315 				bp->rss_hash_key_valid = true;
4316 
4317 				bp->toeplitz_prefix = 0;
4318 				for (k = 0; k < 8; k++) {
4319 					bp->toeplitz_prefix <<= 8;
4320 					bp->toeplitz_prefix |= key[k];
4321 				}
4322 			} else {
4323 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4324 				       HW_HASH_KEY_SIZE);
4325 			}
4326 		}
4327 	}
4328 }
4329 
4330 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4331 {
4332 	int pages;
4333 
4334 	pages = ring_size / desc_per_pg;
4335 
4336 	if (!pages)
4337 		return 1;
4338 
4339 	pages++;
4340 
4341 	while (pages & (pages - 1))
4342 		pages++;
4343 
4344 	return pages;
4345 }
4346 
4347 void bnxt_set_tpa_flags(struct bnxt *bp)
4348 {
4349 	bp->flags &= ~BNXT_FLAG_TPA;
4350 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4351 		return;
4352 	if (bp->dev->features & NETIF_F_LRO)
4353 		bp->flags |= BNXT_FLAG_LRO;
4354 	else if (bp->dev->features & NETIF_F_GRO_HW)
4355 		bp->flags |= BNXT_FLAG_GRO;
4356 }
4357 
4358 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4359  * be set on entry.
4360  */
4361 void bnxt_set_ring_params(struct bnxt *bp)
4362 {
4363 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4364 	u32 agg_factor = 0, agg_ring_size = 0;
4365 
4366 	/* 8 for CRC and VLAN */
4367 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4368 
4369 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4370 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4371 
4372 	bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
4373 	ring_size = bp->rx_ring_size;
4374 	bp->rx_agg_ring_size = 0;
4375 	bp->rx_agg_nr_pages = 0;
4376 
4377 	if (bp->flags & BNXT_FLAG_TPA)
4378 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4379 
4380 	bp->flags &= ~BNXT_FLAG_JUMBO;
4381 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4382 		u32 jumbo_factor;
4383 
4384 		bp->flags |= BNXT_FLAG_JUMBO;
4385 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4386 		if (jumbo_factor > agg_factor)
4387 			agg_factor = jumbo_factor;
4388 	}
4389 	if (agg_factor) {
4390 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4391 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4392 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4393 				    bp->rx_ring_size, ring_size);
4394 			bp->rx_ring_size = ring_size;
4395 		}
4396 		agg_ring_size = ring_size * agg_factor;
4397 
4398 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4399 							RX_DESC_CNT);
4400 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4401 			u32 tmp = agg_ring_size;
4402 
4403 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4404 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4405 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4406 				    tmp, agg_ring_size);
4407 		}
4408 		bp->rx_agg_ring_size = agg_ring_size;
4409 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4410 
4411 		if (BNXT_RX_PAGE_MODE(bp)) {
4412 			rx_space = PAGE_SIZE;
4413 			rx_size = PAGE_SIZE -
4414 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4415 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4416 		} else {
4417 			rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
4418 			rx_space = rx_size + NET_SKB_PAD +
4419 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4420 		}
4421 	}
4422 
4423 	bp->rx_buf_use_size = rx_size;
4424 	bp->rx_buf_size = rx_space;
4425 
4426 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4427 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4428 
4429 	ring_size = bp->tx_ring_size;
4430 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4431 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4432 
4433 	max_rx_cmpl = bp->rx_ring_size;
4434 	/* MAX TPA needs to be added because TPA_START completions are
4435 	 * immediately recycled, so the TPA completions are not bound by
4436 	 * the RX ring size.
4437 	 */
4438 	if (bp->flags & BNXT_FLAG_TPA)
4439 		max_rx_cmpl += bp->max_tpa;
4440 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4441 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4442 	bp->cp_ring_size = ring_size;
4443 
4444 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4445 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4446 		bp->cp_nr_pages = MAX_CP_PAGES;
4447 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4448 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4449 			    ring_size, bp->cp_ring_size);
4450 	}
4451 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4452 	bp->cp_ring_mask = bp->cp_bit - 1;
4453 }
4454 
4455 /* Changing allocation mode of RX rings.
4456  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4457  */
4458 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4459 {
4460 	struct net_device *dev = bp->dev;
4461 
4462 	if (page_mode) {
4463 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
4464 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4465 
4466 		if (bp->xdp_prog->aux->xdp_has_frags)
4467 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4468 		else
4469 			dev->max_mtu =
4470 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4471 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4472 			bp->flags |= BNXT_FLAG_JUMBO;
4473 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4474 		} else {
4475 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4476 			bp->rx_skb_func = bnxt_rx_page_skb;
4477 		}
4478 		bp->rx_dir = DMA_BIDIRECTIONAL;
4479 		/* Disable LRO or GRO_HW */
4480 		netdev_update_features(dev);
4481 	} else {
4482 		dev->max_mtu = bp->max_mtu;
4483 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4484 		bp->rx_dir = DMA_FROM_DEVICE;
4485 		bp->rx_skb_func = bnxt_rx_skb;
4486 	}
4487 	return 0;
4488 }
4489 
4490 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4491 {
4492 	int i;
4493 	struct bnxt_vnic_info *vnic;
4494 	struct pci_dev *pdev = bp->pdev;
4495 
4496 	if (!bp->vnic_info)
4497 		return;
4498 
4499 	for (i = 0; i < bp->nr_vnics; i++) {
4500 		vnic = &bp->vnic_info[i];
4501 
4502 		kfree(vnic->fw_grp_ids);
4503 		vnic->fw_grp_ids = NULL;
4504 
4505 		kfree(vnic->uc_list);
4506 		vnic->uc_list = NULL;
4507 
4508 		if (vnic->mc_list) {
4509 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4510 					  vnic->mc_list, vnic->mc_list_mapping);
4511 			vnic->mc_list = NULL;
4512 		}
4513 
4514 		if (vnic->rss_table) {
4515 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4516 					  vnic->rss_table,
4517 					  vnic->rss_table_dma_addr);
4518 			vnic->rss_table = NULL;
4519 		}
4520 
4521 		vnic->rss_hash_key = NULL;
4522 		vnic->flags = 0;
4523 	}
4524 }
4525 
4526 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4527 {
4528 	int i, rc = 0, size;
4529 	struct bnxt_vnic_info *vnic;
4530 	struct pci_dev *pdev = bp->pdev;
4531 	int max_rings;
4532 
4533 	for (i = 0; i < bp->nr_vnics; i++) {
4534 		vnic = &bp->vnic_info[i];
4535 
4536 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4537 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4538 
4539 			if (mem_size > 0) {
4540 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4541 				if (!vnic->uc_list) {
4542 					rc = -ENOMEM;
4543 					goto out;
4544 				}
4545 			}
4546 		}
4547 
4548 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4549 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4550 			vnic->mc_list =
4551 				dma_alloc_coherent(&pdev->dev,
4552 						   vnic->mc_list_size,
4553 						   &vnic->mc_list_mapping,
4554 						   GFP_KERNEL);
4555 			if (!vnic->mc_list) {
4556 				rc = -ENOMEM;
4557 				goto out;
4558 			}
4559 		}
4560 
4561 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4562 			goto vnic_skip_grps;
4563 
4564 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4565 			max_rings = bp->rx_nr_rings;
4566 		else
4567 			max_rings = 1;
4568 
4569 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4570 		if (!vnic->fw_grp_ids) {
4571 			rc = -ENOMEM;
4572 			goto out;
4573 		}
4574 vnic_skip_grps:
4575 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4576 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4577 			continue;
4578 
4579 		/* Allocate rss table and hash key */
4580 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4581 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4582 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4583 
4584 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4585 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4586 						     vnic->rss_table_size,
4587 						     &vnic->rss_table_dma_addr,
4588 						     GFP_KERNEL);
4589 		if (!vnic->rss_table) {
4590 			rc = -ENOMEM;
4591 			goto out;
4592 		}
4593 
4594 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4595 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4596 	}
4597 	return 0;
4598 
4599 out:
4600 	return rc;
4601 }
4602 
4603 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4604 {
4605 	struct bnxt_hwrm_wait_token *token;
4606 
4607 	dma_pool_destroy(bp->hwrm_dma_pool);
4608 	bp->hwrm_dma_pool = NULL;
4609 
4610 	rcu_read_lock();
4611 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4612 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4613 	rcu_read_unlock();
4614 }
4615 
4616 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4617 {
4618 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4619 					    BNXT_HWRM_DMA_SIZE,
4620 					    BNXT_HWRM_DMA_ALIGN, 0);
4621 	if (!bp->hwrm_dma_pool)
4622 		return -ENOMEM;
4623 
4624 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4625 
4626 	return 0;
4627 }
4628 
4629 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4630 {
4631 	kfree(stats->hw_masks);
4632 	stats->hw_masks = NULL;
4633 	kfree(stats->sw_stats);
4634 	stats->sw_stats = NULL;
4635 	if (stats->hw_stats) {
4636 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
4637 				  stats->hw_stats_map);
4638 		stats->hw_stats = NULL;
4639 	}
4640 }
4641 
4642 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
4643 				bool alloc_masks)
4644 {
4645 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
4646 					     &stats->hw_stats_map, GFP_KERNEL);
4647 	if (!stats->hw_stats)
4648 		return -ENOMEM;
4649 
4650 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
4651 	if (!stats->sw_stats)
4652 		goto stats_mem_err;
4653 
4654 	if (alloc_masks) {
4655 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
4656 		if (!stats->hw_masks)
4657 			goto stats_mem_err;
4658 	}
4659 	return 0;
4660 
4661 stats_mem_err:
4662 	bnxt_free_stats_mem(bp, stats);
4663 	return -ENOMEM;
4664 }
4665 
4666 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
4667 {
4668 	int i;
4669 
4670 	for (i = 0; i < count; i++)
4671 		mask_arr[i] = mask;
4672 }
4673 
4674 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
4675 {
4676 	int i;
4677 
4678 	for (i = 0; i < count; i++)
4679 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
4680 }
4681 
4682 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
4683 				    struct bnxt_stats_mem *stats)
4684 {
4685 	struct hwrm_func_qstats_ext_output *resp;
4686 	struct hwrm_func_qstats_ext_input *req;
4687 	__le64 *hw_masks;
4688 	int rc;
4689 
4690 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
4691 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4692 		return -EOPNOTSUPP;
4693 
4694 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
4695 	if (rc)
4696 		return rc;
4697 
4698 	req->fid = cpu_to_le16(0xffff);
4699 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4700 
4701 	resp = hwrm_req_hold(bp, req);
4702 	rc = hwrm_req_send(bp, req);
4703 	if (!rc) {
4704 		hw_masks = &resp->rx_ucast_pkts;
4705 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
4706 	}
4707 	hwrm_req_drop(bp, req);
4708 	return rc;
4709 }
4710 
4711 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
4712 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
4713 
4714 static void bnxt_init_stats(struct bnxt *bp)
4715 {
4716 	struct bnxt_napi *bnapi = bp->bnapi[0];
4717 	struct bnxt_cp_ring_info *cpr;
4718 	struct bnxt_stats_mem *stats;
4719 	__le64 *rx_stats, *tx_stats;
4720 	int rc, rx_count, tx_count;
4721 	u64 *rx_masks, *tx_masks;
4722 	u64 mask;
4723 	u8 flags;
4724 
4725 	cpr = &bnapi->cp_ring;
4726 	stats = &cpr->stats;
4727 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
4728 	if (rc) {
4729 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4730 			mask = (1ULL << 48) - 1;
4731 		else
4732 			mask = -1ULL;
4733 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
4734 	}
4735 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
4736 		stats = &bp->port_stats;
4737 		rx_stats = stats->hw_stats;
4738 		rx_masks = stats->hw_masks;
4739 		rx_count = sizeof(struct rx_port_stats) / 8;
4740 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4741 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4742 		tx_count = sizeof(struct tx_port_stats) / 8;
4743 
4744 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
4745 		rc = bnxt_hwrm_port_qstats(bp, flags);
4746 		if (rc) {
4747 			mask = (1ULL << 40) - 1;
4748 
4749 			bnxt_fill_masks(rx_masks, mask, rx_count);
4750 			bnxt_fill_masks(tx_masks, mask, tx_count);
4751 		} else {
4752 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4753 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
4754 			bnxt_hwrm_port_qstats(bp, 0);
4755 		}
4756 	}
4757 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
4758 		stats = &bp->rx_port_stats_ext;
4759 		rx_stats = stats->hw_stats;
4760 		rx_masks = stats->hw_masks;
4761 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
4762 		stats = &bp->tx_port_stats_ext;
4763 		tx_stats = stats->hw_stats;
4764 		tx_masks = stats->hw_masks;
4765 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
4766 
4767 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4768 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
4769 		if (rc) {
4770 			mask = (1ULL << 40) - 1;
4771 
4772 			bnxt_fill_masks(rx_masks, mask, rx_count);
4773 			if (tx_stats)
4774 				bnxt_fill_masks(tx_masks, mask, tx_count);
4775 		} else {
4776 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4777 			if (tx_stats)
4778 				bnxt_copy_hw_masks(tx_masks, tx_stats,
4779 						   tx_count);
4780 			bnxt_hwrm_port_qstats_ext(bp, 0);
4781 		}
4782 	}
4783 }
4784 
4785 static void bnxt_free_port_stats(struct bnxt *bp)
4786 {
4787 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
4788 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
4789 
4790 	bnxt_free_stats_mem(bp, &bp->port_stats);
4791 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
4792 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
4793 }
4794 
4795 static void bnxt_free_ring_stats(struct bnxt *bp)
4796 {
4797 	int i;
4798 
4799 	if (!bp->bnapi)
4800 		return;
4801 
4802 	for (i = 0; i < bp->cp_nr_rings; i++) {
4803 		struct bnxt_napi *bnapi = bp->bnapi[i];
4804 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4805 
4806 		bnxt_free_stats_mem(bp, &cpr->stats);
4807 	}
4808 }
4809 
4810 static int bnxt_alloc_stats(struct bnxt *bp)
4811 {
4812 	u32 size, i;
4813 	int rc;
4814 
4815 	size = bp->hw_ring_stats_size;
4816 
4817 	for (i = 0; i < bp->cp_nr_rings; i++) {
4818 		struct bnxt_napi *bnapi = bp->bnapi[i];
4819 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4820 
4821 		cpr->stats.len = size;
4822 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
4823 		if (rc)
4824 			return rc;
4825 
4826 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4827 	}
4828 
4829 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
4830 		return 0;
4831 
4832 	if (bp->port_stats.hw_stats)
4833 		goto alloc_ext_stats;
4834 
4835 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
4836 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
4837 	if (rc)
4838 		return rc;
4839 
4840 	bp->flags |= BNXT_FLAG_PORT_STATS;
4841 
4842 alloc_ext_stats:
4843 	/* Display extended statistics only if FW supports it */
4844 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
4845 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
4846 			return 0;
4847 
4848 	if (bp->rx_port_stats_ext.hw_stats)
4849 		goto alloc_tx_ext_stats;
4850 
4851 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
4852 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
4853 	/* Extended stats are optional */
4854 	if (rc)
4855 		return 0;
4856 
4857 alloc_tx_ext_stats:
4858 	if (bp->tx_port_stats_ext.hw_stats)
4859 		return 0;
4860 
4861 	if (bp->hwrm_spec_code >= 0x10902 ||
4862 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
4863 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
4864 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
4865 		/* Extended stats are optional */
4866 		if (rc)
4867 			return 0;
4868 	}
4869 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
4870 	return 0;
4871 }
4872 
4873 static void bnxt_clear_ring_indices(struct bnxt *bp)
4874 {
4875 	int i, j;
4876 
4877 	if (!bp->bnapi)
4878 		return;
4879 
4880 	for (i = 0; i < bp->cp_nr_rings; i++) {
4881 		struct bnxt_napi *bnapi = bp->bnapi[i];
4882 		struct bnxt_cp_ring_info *cpr;
4883 		struct bnxt_rx_ring_info *rxr;
4884 		struct bnxt_tx_ring_info *txr;
4885 
4886 		if (!bnapi)
4887 			continue;
4888 
4889 		cpr = &bnapi->cp_ring;
4890 		cpr->cp_raw_cons = 0;
4891 
4892 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4893 			txr->tx_prod = 0;
4894 			txr->tx_cons = 0;
4895 			txr->tx_hw_cons = 0;
4896 		}
4897 
4898 		rxr = bnapi->rx_ring;
4899 		if (rxr) {
4900 			rxr->rx_prod = 0;
4901 			rxr->rx_agg_prod = 0;
4902 			rxr->rx_sw_agg_prod = 0;
4903 			rxr->rx_next_cons = 0;
4904 		}
4905 		bnapi->events = 0;
4906 	}
4907 }
4908 
4909 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
4910 {
4911 	u8 type = fltr->type, flags = fltr->flags;
4912 
4913 	INIT_LIST_HEAD(&fltr->list);
4914 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
4915 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
4916 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
4917 }
4918 
4919 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
4920 {
4921 	if (!list_empty(&fltr->list))
4922 		list_del_init(&fltr->list);
4923 }
4924 
4925 void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
4926 {
4927 	struct bnxt_filter_base *usr_fltr, *tmp;
4928 
4929 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
4930 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
4931 			continue;
4932 		bnxt_del_one_usr_fltr(bp, usr_fltr);
4933 	}
4934 }
4935 
4936 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
4937 {
4938 	hlist_del(&fltr->hash);
4939 	bnxt_del_one_usr_fltr(bp, fltr);
4940 	if (fltr->flags) {
4941 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
4942 		bp->ntp_fltr_count--;
4943 	}
4944 	kfree(fltr);
4945 }
4946 
4947 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
4948 {
4949 	int i;
4950 
4951 	/* Under rtnl_lock and all our NAPIs have been disabled.  It's
4952 	 * safe to delete the hash table.
4953 	 */
4954 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
4955 		struct hlist_head *head;
4956 		struct hlist_node *tmp;
4957 		struct bnxt_ntuple_filter *fltr;
4958 
4959 		head = &bp->ntp_fltr_hash_tbl[i];
4960 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
4961 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
4962 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
4963 				     !list_empty(&fltr->base.list)))
4964 				continue;
4965 			bnxt_del_fltr(bp, &fltr->base);
4966 		}
4967 	}
4968 	if (!all)
4969 		return;
4970 
4971 	bitmap_free(bp->ntp_fltr_bmap);
4972 	bp->ntp_fltr_bmap = NULL;
4973 	bp->ntp_fltr_count = 0;
4974 }
4975 
4976 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
4977 {
4978 	int i, rc = 0;
4979 
4980 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
4981 		return 0;
4982 
4983 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
4984 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
4985 
4986 	bp->ntp_fltr_count = 0;
4987 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
4988 
4989 	if (!bp->ntp_fltr_bmap)
4990 		rc = -ENOMEM;
4991 
4992 	return rc;
4993 }
4994 
4995 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
4996 {
4997 	int i;
4998 
4999 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5000 		struct hlist_head *head;
5001 		struct hlist_node *tmp;
5002 		struct bnxt_l2_filter *fltr;
5003 
5004 		head = &bp->l2_fltr_hash_tbl[i];
5005 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5006 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5007 				     !list_empty(&fltr->base.list)))
5008 				continue;
5009 			bnxt_del_fltr(bp, &fltr->base);
5010 		}
5011 	}
5012 }
5013 
5014 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5015 {
5016 	int i;
5017 
5018 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5019 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5020 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5021 }
5022 
5023 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5024 {
5025 	bnxt_free_vnic_attributes(bp);
5026 	bnxt_free_tx_rings(bp);
5027 	bnxt_free_rx_rings(bp);
5028 	bnxt_free_cp_rings(bp);
5029 	bnxt_free_all_cp_arrays(bp);
5030 	bnxt_free_ntp_fltrs(bp, false);
5031 	bnxt_free_l2_filters(bp, false);
5032 	if (irq_re_init) {
5033 		bnxt_free_ring_stats(bp);
5034 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5035 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5036 			bnxt_free_port_stats(bp);
5037 		bnxt_free_ring_grps(bp);
5038 		bnxt_free_vnics(bp);
5039 		kfree(bp->tx_ring_map);
5040 		bp->tx_ring_map = NULL;
5041 		kfree(bp->tx_ring);
5042 		bp->tx_ring = NULL;
5043 		kfree(bp->rx_ring);
5044 		bp->rx_ring = NULL;
5045 		kfree(bp->bnapi);
5046 		bp->bnapi = NULL;
5047 	} else {
5048 		bnxt_clear_ring_indices(bp);
5049 	}
5050 }
5051 
5052 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5053 {
5054 	int i, j, rc, size, arr_size;
5055 	void *bnapi;
5056 
5057 	if (irq_re_init) {
5058 		/* Allocate bnapi mem pointer array and mem block for
5059 		 * all queues
5060 		 */
5061 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5062 				bp->cp_nr_rings);
5063 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5064 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5065 		if (!bnapi)
5066 			return -ENOMEM;
5067 
5068 		bp->bnapi = bnapi;
5069 		bnapi += arr_size;
5070 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5071 			bp->bnapi[i] = bnapi;
5072 			bp->bnapi[i]->index = i;
5073 			bp->bnapi[i]->bp = bp;
5074 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5075 				struct bnxt_cp_ring_info *cpr =
5076 					&bp->bnapi[i]->cp_ring;
5077 
5078 				cpr->cp_ring_struct.ring_mem.flags =
5079 					BNXT_RMEM_RING_PTE_FLAG;
5080 			}
5081 		}
5082 
5083 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5084 				      sizeof(struct bnxt_rx_ring_info),
5085 				      GFP_KERNEL);
5086 		if (!bp->rx_ring)
5087 			return -ENOMEM;
5088 
5089 		for (i = 0; i < bp->rx_nr_rings; i++) {
5090 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5091 
5092 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5093 				rxr->rx_ring_struct.ring_mem.flags =
5094 					BNXT_RMEM_RING_PTE_FLAG;
5095 				rxr->rx_agg_ring_struct.ring_mem.flags =
5096 					BNXT_RMEM_RING_PTE_FLAG;
5097 			} else {
5098 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5099 			}
5100 			rxr->bnapi = bp->bnapi[i];
5101 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5102 		}
5103 
5104 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5105 				      sizeof(struct bnxt_tx_ring_info),
5106 				      GFP_KERNEL);
5107 		if (!bp->tx_ring)
5108 			return -ENOMEM;
5109 
5110 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5111 					  GFP_KERNEL);
5112 
5113 		if (!bp->tx_ring_map)
5114 			return -ENOMEM;
5115 
5116 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5117 			j = 0;
5118 		else
5119 			j = bp->rx_nr_rings;
5120 
5121 		for (i = 0; i < bp->tx_nr_rings; i++) {
5122 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5123 			struct bnxt_napi *bnapi2;
5124 
5125 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5126 				txr->tx_ring_struct.ring_mem.flags =
5127 					BNXT_RMEM_RING_PTE_FLAG;
5128 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5129 			if (i >= bp->tx_nr_rings_xdp) {
5130 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5131 
5132 				bnapi2 = bp->bnapi[k];
5133 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5134 				txr->tx_napi_idx =
5135 					BNXT_RING_TO_TC(bp, txr->txq_index);
5136 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5137 				bnapi2->tx_int = bnxt_tx_int;
5138 			} else {
5139 				bnapi2 = bp->bnapi[j];
5140 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5141 				bnapi2->tx_ring[0] = txr;
5142 				bnapi2->tx_int = bnxt_tx_int_xdp;
5143 				j++;
5144 			}
5145 			txr->bnapi = bnapi2;
5146 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5147 				txr->tx_cpr = &bnapi2->cp_ring;
5148 		}
5149 
5150 		rc = bnxt_alloc_stats(bp);
5151 		if (rc)
5152 			goto alloc_mem_err;
5153 		bnxt_init_stats(bp);
5154 
5155 		rc = bnxt_alloc_ntp_fltrs(bp);
5156 		if (rc)
5157 			goto alloc_mem_err;
5158 
5159 		rc = bnxt_alloc_vnics(bp);
5160 		if (rc)
5161 			goto alloc_mem_err;
5162 	}
5163 
5164 	rc = bnxt_alloc_all_cp_arrays(bp);
5165 	if (rc)
5166 		goto alloc_mem_err;
5167 
5168 	bnxt_init_ring_struct(bp);
5169 
5170 	rc = bnxt_alloc_rx_rings(bp);
5171 	if (rc)
5172 		goto alloc_mem_err;
5173 
5174 	rc = bnxt_alloc_tx_rings(bp);
5175 	if (rc)
5176 		goto alloc_mem_err;
5177 
5178 	rc = bnxt_alloc_cp_rings(bp);
5179 	if (rc)
5180 		goto alloc_mem_err;
5181 
5182 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5183 						  BNXT_VNIC_MCAST_FLAG |
5184 						  BNXT_VNIC_UCAST_FLAG;
5185 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5186 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5187 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5188 
5189 	rc = bnxt_alloc_vnic_attributes(bp);
5190 	if (rc)
5191 		goto alloc_mem_err;
5192 	return 0;
5193 
5194 alloc_mem_err:
5195 	bnxt_free_mem(bp, true);
5196 	return rc;
5197 }
5198 
5199 static void bnxt_disable_int(struct bnxt *bp)
5200 {
5201 	int i;
5202 
5203 	if (!bp->bnapi)
5204 		return;
5205 
5206 	for (i = 0; i < bp->cp_nr_rings; i++) {
5207 		struct bnxt_napi *bnapi = bp->bnapi[i];
5208 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5209 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5210 
5211 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5212 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5213 	}
5214 }
5215 
5216 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5217 {
5218 	struct bnxt_napi *bnapi = bp->bnapi[n];
5219 	struct bnxt_cp_ring_info *cpr;
5220 
5221 	cpr = &bnapi->cp_ring;
5222 	return cpr->cp_ring_struct.map_idx;
5223 }
5224 
5225 static void bnxt_disable_int_sync(struct bnxt *bp)
5226 {
5227 	int i;
5228 
5229 	if (!bp->irq_tbl)
5230 		return;
5231 
5232 	atomic_inc(&bp->intr_sem);
5233 
5234 	bnxt_disable_int(bp);
5235 	for (i = 0; i < bp->cp_nr_rings; i++) {
5236 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5237 
5238 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5239 	}
5240 }
5241 
5242 static void bnxt_enable_int(struct bnxt *bp)
5243 {
5244 	int i;
5245 
5246 	atomic_set(&bp->intr_sem, 0);
5247 	for (i = 0; i < bp->cp_nr_rings; i++) {
5248 		struct bnxt_napi *bnapi = bp->bnapi[i];
5249 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5250 
5251 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5252 	}
5253 }
5254 
5255 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5256 			    bool async_only)
5257 {
5258 	DECLARE_BITMAP(async_events_bmap, 256);
5259 	u32 *events = (u32 *)async_events_bmap;
5260 	struct hwrm_func_drv_rgtr_output *resp;
5261 	struct hwrm_func_drv_rgtr_input *req;
5262 	u32 flags;
5263 	int rc, i;
5264 
5265 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5266 	if (rc)
5267 		return rc;
5268 
5269 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5270 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5271 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5272 
5273 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5274 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5275 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5276 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5277 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5278 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5279 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5280 	req->flags = cpu_to_le32(flags);
5281 	req->ver_maj_8b = DRV_VER_MAJ;
5282 	req->ver_min_8b = DRV_VER_MIN;
5283 	req->ver_upd_8b = DRV_VER_UPD;
5284 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5285 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5286 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5287 
5288 	if (BNXT_PF(bp)) {
5289 		u32 data[8];
5290 		int i;
5291 
5292 		memset(data, 0, sizeof(data));
5293 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5294 			u16 cmd = bnxt_vf_req_snif[i];
5295 			unsigned int bit, idx;
5296 
5297 			idx = cmd / 32;
5298 			bit = cmd % 32;
5299 			data[idx] |= 1 << bit;
5300 		}
5301 
5302 		for (i = 0; i < 8; i++)
5303 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5304 
5305 		req->enables |=
5306 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5307 	}
5308 
5309 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5310 		req->flags |= cpu_to_le32(
5311 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5312 
5313 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5314 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5315 		u16 event_id = bnxt_async_events_arr[i];
5316 
5317 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5318 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5319 			continue;
5320 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5321 		    !bp->ptp_cfg)
5322 			continue;
5323 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5324 	}
5325 	if (bmap && bmap_size) {
5326 		for (i = 0; i < bmap_size; i++) {
5327 			if (test_bit(i, bmap))
5328 				__set_bit(i, async_events_bmap);
5329 		}
5330 	}
5331 	for (i = 0; i < 8; i++)
5332 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5333 
5334 	if (async_only)
5335 		req->enables =
5336 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5337 
5338 	resp = hwrm_req_hold(bp, req);
5339 	rc = hwrm_req_send(bp, req);
5340 	if (!rc) {
5341 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5342 		if (resp->flags &
5343 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5344 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5345 	}
5346 	hwrm_req_drop(bp, req);
5347 	return rc;
5348 }
5349 
5350 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5351 {
5352 	struct hwrm_func_drv_unrgtr_input *req;
5353 	int rc;
5354 
5355 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5356 		return 0;
5357 
5358 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5359 	if (rc)
5360 		return rc;
5361 	return hwrm_req_send(bp, req);
5362 }
5363 
5364 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5365 
5366 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5367 {
5368 	struct hwrm_tunnel_dst_port_free_input *req;
5369 	int rc;
5370 
5371 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5372 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5373 		return 0;
5374 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5375 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5376 		return 0;
5377 
5378 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5379 	if (rc)
5380 		return rc;
5381 
5382 	req->tunnel_type = tunnel_type;
5383 
5384 	switch (tunnel_type) {
5385 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5386 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5387 		bp->vxlan_port = 0;
5388 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5389 		break;
5390 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5391 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5392 		bp->nge_port = 0;
5393 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5394 		break;
5395 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5396 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5397 		bp->vxlan_gpe_port = 0;
5398 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5399 		break;
5400 	default:
5401 		break;
5402 	}
5403 
5404 	rc = hwrm_req_send(bp, req);
5405 	if (rc)
5406 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5407 			   rc);
5408 	if (bp->flags & BNXT_FLAG_TPA)
5409 		bnxt_set_tpa(bp, true);
5410 	return rc;
5411 }
5412 
5413 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5414 					   u8 tunnel_type)
5415 {
5416 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5417 	struct hwrm_tunnel_dst_port_alloc_input *req;
5418 	int rc;
5419 
5420 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5421 	if (rc)
5422 		return rc;
5423 
5424 	req->tunnel_type = tunnel_type;
5425 	req->tunnel_dst_port_val = port;
5426 
5427 	resp = hwrm_req_hold(bp, req);
5428 	rc = hwrm_req_send(bp, req);
5429 	if (rc) {
5430 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5431 			   rc);
5432 		goto err_out;
5433 	}
5434 
5435 	switch (tunnel_type) {
5436 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5437 		bp->vxlan_port = port;
5438 		bp->vxlan_fw_dst_port_id =
5439 			le16_to_cpu(resp->tunnel_dst_port_id);
5440 		break;
5441 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5442 		bp->nge_port = port;
5443 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5444 		break;
5445 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5446 		bp->vxlan_gpe_port = port;
5447 		bp->vxlan_gpe_fw_dst_port_id =
5448 			le16_to_cpu(resp->tunnel_dst_port_id);
5449 		break;
5450 	default:
5451 		break;
5452 	}
5453 	if (bp->flags & BNXT_FLAG_TPA)
5454 		bnxt_set_tpa(bp, true);
5455 
5456 err_out:
5457 	hwrm_req_drop(bp, req);
5458 	return rc;
5459 }
5460 
5461 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5462 {
5463 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5464 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5465 	int rc;
5466 
5467 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5468 	if (rc)
5469 		return rc;
5470 
5471 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5472 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5473 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5474 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5475 	}
5476 	req->mask = cpu_to_le32(vnic->rx_mask);
5477 	return hwrm_req_send_silent(bp, req);
5478 }
5479 
5480 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5481 {
5482 	if (!atomic_dec_and_test(&fltr->refcnt))
5483 		return;
5484 	spin_lock_bh(&bp->ntp_fltr_lock);
5485 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5486 		spin_unlock_bh(&bp->ntp_fltr_lock);
5487 		return;
5488 	}
5489 	hlist_del_rcu(&fltr->base.hash);
5490 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5491 	if (fltr->base.flags) {
5492 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5493 		bp->ntp_fltr_count--;
5494 	}
5495 	spin_unlock_bh(&bp->ntp_fltr_lock);
5496 	kfree_rcu(fltr, base.rcu);
5497 }
5498 
5499 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5500 						      struct bnxt_l2_key *key,
5501 						      u32 idx)
5502 {
5503 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5504 	struct bnxt_l2_filter *fltr;
5505 
5506 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5507 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5508 
5509 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5510 		    l2_key->vlan == key->vlan)
5511 			return fltr;
5512 	}
5513 	return NULL;
5514 }
5515 
5516 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5517 						    struct bnxt_l2_key *key,
5518 						    u32 idx)
5519 {
5520 	struct bnxt_l2_filter *fltr = NULL;
5521 
5522 	rcu_read_lock();
5523 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5524 	if (fltr)
5525 		atomic_inc(&fltr->refcnt);
5526 	rcu_read_unlock();
5527 	return fltr;
5528 }
5529 
5530 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5531 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5532 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5533 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5534 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5535 
5536 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5537 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5538 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5539 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5540 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5541 
5542 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5543 {
5544 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5545 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5546 			return sizeof(fkeys->addrs.v4addrs) +
5547 			       sizeof(fkeys->ports);
5548 
5549 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5550 			return sizeof(fkeys->addrs.v4addrs);
5551 	}
5552 
5553 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5554 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5555 			return sizeof(fkeys->addrs.v6addrs) +
5556 			       sizeof(fkeys->ports);
5557 
5558 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5559 			return sizeof(fkeys->addrs.v6addrs);
5560 	}
5561 
5562 	return 0;
5563 }
5564 
5565 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5566 			 const unsigned char *key)
5567 {
5568 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5569 	struct bnxt_ipv4_tuple tuple4;
5570 	struct bnxt_ipv6_tuple tuple6;
5571 	int i, j, len = 0;
5572 	u8 *four_tuple;
5573 
5574 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5575 	if (!len)
5576 		return 0;
5577 
5578 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5579 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5580 		tuple4.ports = fkeys->ports;
5581 		four_tuple = (unsigned char *)&tuple4;
5582 	} else {
5583 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5584 		tuple6.ports = fkeys->ports;
5585 		four_tuple = (unsigned char *)&tuple6;
5586 	}
5587 
5588 	for (i = 0, j = 8; i < len; i++, j++) {
5589 		u8 byte = four_tuple[i];
5590 		int bit;
5591 
5592 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5593 			if (byte & 0x80)
5594 				hash ^= prefix;
5595 		}
5596 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5597 	}
5598 
5599 	/* The valid part of the hash is in the upper 32 bits. */
5600 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5601 }
5602 
5603 #ifdef CONFIG_RFS_ACCEL
5604 static struct bnxt_l2_filter *
5605 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5606 {
5607 	struct bnxt_l2_filter *fltr;
5608 	u32 idx;
5609 
5610 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5611 	      BNXT_L2_FLTR_HASH_MASK;
5612 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5613 	return fltr;
5614 }
5615 #endif
5616 
5617 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
5618 			       struct bnxt_l2_key *key, u32 idx)
5619 {
5620 	struct hlist_head *head;
5621 
5622 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
5623 	fltr->l2_key.vlan = key->vlan;
5624 	fltr->base.type = BNXT_FLTR_TYPE_L2;
5625 	if (fltr->base.flags) {
5626 		int bit_id;
5627 
5628 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
5629 						 bp->max_fltr, 0);
5630 		if (bit_id < 0)
5631 			return -ENOMEM;
5632 		fltr->base.sw_id = (u16)bit_id;
5633 		bp->ntp_fltr_count++;
5634 	}
5635 	head = &bp->l2_fltr_hash_tbl[idx];
5636 	hlist_add_head_rcu(&fltr->base.hash, head);
5637 	bnxt_insert_usr_fltr(bp, &fltr->base);
5638 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
5639 	atomic_set(&fltr->refcnt, 1);
5640 	return 0;
5641 }
5642 
5643 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
5644 						   struct bnxt_l2_key *key,
5645 						   gfp_t gfp)
5646 {
5647 	struct bnxt_l2_filter *fltr;
5648 	u32 idx;
5649 	int rc;
5650 
5651 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5652 	      BNXT_L2_FLTR_HASH_MASK;
5653 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5654 	if (fltr)
5655 		return fltr;
5656 
5657 	fltr = kzalloc(sizeof(*fltr), gfp);
5658 	if (!fltr)
5659 		return ERR_PTR(-ENOMEM);
5660 	spin_lock_bh(&bp->ntp_fltr_lock);
5661 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5662 	spin_unlock_bh(&bp->ntp_fltr_lock);
5663 	if (rc) {
5664 		bnxt_del_l2_filter(bp, fltr);
5665 		fltr = ERR_PTR(rc);
5666 	}
5667 	return fltr;
5668 }
5669 
5670 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
5671 						struct bnxt_l2_key *key,
5672 						u16 flags)
5673 {
5674 	struct bnxt_l2_filter *fltr;
5675 	u32 idx;
5676 	int rc;
5677 
5678 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5679 	      BNXT_L2_FLTR_HASH_MASK;
5680 	spin_lock_bh(&bp->ntp_fltr_lock);
5681 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5682 	if (fltr) {
5683 		fltr = ERR_PTR(-EEXIST);
5684 		goto l2_filter_exit;
5685 	}
5686 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
5687 	if (!fltr) {
5688 		fltr = ERR_PTR(-ENOMEM);
5689 		goto l2_filter_exit;
5690 	}
5691 	fltr->base.flags = flags;
5692 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5693 	if (rc) {
5694 		spin_unlock_bh(&bp->ntp_fltr_lock);
5695 		bnxt_del_l2_filter(bp, fltr);
5696 		return ERR_PTR(rc);
5697 	}
5698 
5699 l2_filter_exit:
5700 	spin_unlock_bh(&bp->ntp_fltr_lock);
5701 	return fltr;
5702 }
5703 
5704 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
5705 {
5706 #ifdef CONFIG_BNXT_SRIOV
5707 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
5708 
5709 	return vf->fw_fid;
5710 #else
5711 	return INVALID_HW_RING_ID;
5712 #endif
5713 }
5714 
5715 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5716 {
5717 	struct hwrm_cfa_l2_filter_free_input *req;
5718 	u16 target_id = 0xffff;
5719 	int rc;
5720 
5721 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
5722 		struct bnxt_pf_info *pf = &bp->pf;
5723 
5724 		if (fltr->base.vf_idx >= pf->active_vfs)
5725 			return -EINVAL;
5726 
5727 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
5728 		if (target_id == INVALID_HW_RING_ID)
5729 			return -EINVAL;
5730 	}
5731 
5732 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
5733 	if (rc)
5734 		return rc;
5735 
5736 	req->target_id = cpu_to_le16(target_id);
5737 	req->l2_filter_id = fltr->base.filter_id;
5738 	return hwrm_req_send(bp, req);
5739 }
5740 
5741 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5742 {
5743 	struct hwrm_cfa_l2_filter_alloc_output *resp;
5744 	struct hwrm_cfa_l2_filter_alloc_input *req;
5745 	u16 target_id = 0xffff;
5746 	int rc;
5747 
5748 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
5749 		struct bnxt_pf_info *pf = &bp->pf;
5750 
5751 		if (fltr->base.vf_idx >= pf->active_vfs)
5752 			return -EINVAL;
5753 
5754 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
5755 	}
5756 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
5757 	if (rc)
5758 		return rc;
5759 
5760 	req->target_id = cpu_to_le16(target_id);
5761 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
5762 
5763 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
5764 		req->flags |=
5765 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
5766 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
5767 	req->enables =
5768 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
5769 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
5770 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
5771 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
5772 	eth_broadcast_addr(req->l2_addr_mask);
5773 
5774 	if (fltr->l2_key.vlan) {
5775 		req->enables |=
5776 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
5777 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
5778 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
5779 		req->num_vlans = 1;
5780 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
5781 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
5782 	}
5783 
5784 	resp = hwrm_req_hold(bp, req);
5785 	rc = hwrm_req_send(bp, req);
5786 	if (!rc) {
5787 		fltr->base.filter_id = resp->l2_filter_id;
5788 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
5789 	}
5790 	hwrm_req_drop(bp, req);
5791 	return rc;
5792 }
5793 
5794 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
5795 				     struct bnxt_ntuple_filter *fltr)
5796 {
5797 	struct hwrm_cfa_ntuple_filter_free_input *req;
5798 	int rc;
5799 
5800 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
5801 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
5802 	if (rc)
5803 		return rc;
5804 
5805 	req->ntuple_filter_id = fltr->base.filter_id;
5806 	return hwrm_req_send(bp, req);
5807 }
5808 
5809 #define BNXT_NTP_FLTR_FLAGS					\
5810 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
5811 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
5812 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
5813 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
5814 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
5815 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
5816 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
5817 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
5818 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
5819 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
5820 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
5821 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
5822 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
5823 
5824 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
5825 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
5826 
5827 void bnxt_fill_ipv6_mask(__be32 mask[4])
5828 {
5829 	int i;
5830 
5831 	for (i = 0; i < 4; i++)
5832 		mask[i] = cpu_to_be32(~0);
5833 }
5834 
5835 static void
5836 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
5837 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
5838 			  struct bnxt_ntuple_filter *fltr)
5839 {
5840 	struct bnxt_rss_ctx *rss_ctx, *tmp;
5841 	u16 rxq = fltr->base.rxq;
5842 
5843 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
5844 		list_for_each_entry_safe(rss_ctx, tmp, &bp->rss_ctx_list, list) {
5845 			if (rss_ctx->index == fltr->base.fw_vnic_id) {
5846 				struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
5847 
5848 				req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
5849 				break;
5850 			}
5851 		}
5852 		return;
5853 	}
5854 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
5855 		struct bnxt_vnic_info *vnic;
5856 		u32 enables;
5857 
5858 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
5859 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
5860 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
5861 		req->enables |= cpu_to_le32(enables);
5862 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
5863 	} else {
5864 		u32 flags;
5865 
5866 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
5867 		req->flags |= cpu_to_le32(flags);
5868 		req->dst_id = cpu_to_le16(rxq);
5869 	}
5870 }
5871 
5872 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
5873 				      struct bnxt_ntuple_filter *fltr)
5874 {
5875 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
5876 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
5877 	struct bnxt_flow_masks *masks = &fltr->fmasks;
5878 	struct flow_keys *keys = &fltr->fkeys;
5879 	struct bnxt_l2_filter *l2_fltr;
5880 	struct bnxt_vnic_info *vnic;
5881 	int rc;
5882 
5883 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
5884 	if (rc)
5885 		return rc;
5886 
5887 	l2_fltr = fltr->l2_fltr;
5888 	req->l2_filter_id = l2_fltr->base.filter_id;
5889 
5890 	if (fltr->base.flags & BNXT_ACT_DROP) {
5891 		req->flags =
5892 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
5893 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
5894 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
5895 	} else {
5896 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
5897 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
5898 	}
5899 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
5900 
5901 	req->ethertype = htons(ETH_P_IP);
5902 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
5903 	req->ip_protocol = keys->basic.ip_proto;
5904 
5905 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
5906 		req->ethertype = htons(ETH_P_IPV6);
5907 		req->ip_addr_type =
5908 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
5909 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
5910 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
5911 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
5912 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
5913 	} else {
5914 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
5915 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
5916 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
5917 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
5918 	}
5919 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
5920 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
5921 		req->tunnel_type =
5922 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
5923 	}
5924 
5925 	req->src_port = keys->ports.src;
5926 	req->src_port_mask = masks->ports.src;
5927 	req->dst_port = keys->ports.dst;
5928 	req->dst_port_mask = masks->ports.dst;
5929 
5930 	resp = hwrm_req_hold(bp, req);
5931 	rc = hwrm_req_send(bp, req);
5932 	if (!rc)
5933 		fltr->base.filter_id = resp->ntuple_filter_id;
5934 	hwrm_req_drop(bp, req);
5935 	return rc;
5936 }
5937 
5938 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
5939 				     const u8 *mac_addr)
5940 {
5941 	struct bnxt_l2_filter *fltr;
5942 	struct bnxt_l2_key key;
5943 	int rc;
5944 
5945 	ether_addr_copy(key.dst_mac_addr, mac_addr);
5946 	key.vlan = 0;
5947 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
5948 	if (IS_ERR(fltr))
5949 		return PTR_ERR(fltr);
5950 
5951 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
5952 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
5953 	if (rc)
5954 		bnxt_del_l2_filter(bp, fltr);
5955 	else
5956 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
5957 	return rc;
5958 }
5959 
5960 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
5961 {
5962 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
5963 
5964 	/* Any associated ntuple filters will also be cleared by firmware. */
5965 	for (i = 0; i < num_of_vnics; i++) {
5966 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
5967 
5968 		for (j = 0; j < vnic->uc_filter_count; j++) {
5969 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
5970 
5971 			bnxt_hwrm_l2_filter_free(bp, fltr);
5972 			bnxt_del_l2_filter(bp, fltr);
5973 		}
5974 		vnic->uc_filter_count = 0;
5975 	}
5976 }
5977 
5978 #define BNXT_DFLT_TUNL_TPA_BMAP				\
5979 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
5980 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
5981 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
5982 
5983 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
5984 					   struct hwrm_vnic_tpa_cfg_input *req)
5985 {
5986 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
5987 
5988 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
5989 		return;
5990 
5991 	if (bp->vxlan_port)
5992 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
5993 	if (bp->vxlan_gpe_port)
5994 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
5995 	if (bp->nge_port)
5996 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
5997 
5998 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
5999 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6000 }
6001 
6002 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6003 			   u32 tpa_flags)
6004 {
6005 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6006 	struct hwrm_vnic_tpa_cfg_input *req;
6007 	int rc;
6008 
6009 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6010 		return 0;
6011 
6012 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6013 	if (rc)
6014 		return rc;
6015 
6016 	if (tpa_flags) {
6017 		u16 mss = bp->dev->mtu - 40;
6018 		u32 nsegs, n, segs = 0, flags;
6019 
6020 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6021 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6022 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6023 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6024 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6025 		if (tpa_flags & BNXT_FLAG_GRO)
6026 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6027 
6028 		req->flags = cpu_to_le32(flags);
6029 
6030 		req->enables =
6031 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6032 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6033 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6034 
6035 		/* Number of segs are log2 units, and first packet is not
6036 		 * included as part of this units.
6037 		 */
6038 		if (mss <= BNXT_RX_PAGE_SIZE) {
6039 			n = BNXT_RX_PAGE_SIZE / mss;
6040 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6041 		} else {
6042 			n = mss / BNXT_RX_PAGE_SIZE;
6043 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6044 				n++;
6045 			nsegs = (MAX_SKB_FRAGS - n) / n;
6046 		}
6047 
6048 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6049 			segs = MAX_TPA_SEGS_P5;
6050 			max_aggs = bp->max_tpa;
6051 		} else {
6052 			segs = ilog2(nsegs);
6053 		}
6054 		req->max_agg_segs = cpu_to_le16(segs);
6055 		req->max_aggs = cpu_to_le16(max_aggs);
6056 
6057 		req->min_agg_len = cpu_to_le32(512);
6058 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6059 	}
6060 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6061 
6062 	return hwrm_req_send(bp, req);
6063 }
6064 
6065 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6066 {
6067 	struct bnxt_ring_grp_info *grp_info;
6068 
6069 	grp_info = &bp->grp_info[ring->grp_idx];
6070 	return grp_info->cp_fw_ring_id;
6071 }
6072 
6073 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6074 {
6075 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6076 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6077 	else
6078 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6079 }
6080 
6081 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6082 {
6083 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6084 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6085 	else
6086 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6087 }
6088 
6089 int bnxt_alloc_rss_indir_tbl(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx)
6090 {
6091 	int entries;
6092 	u16 *tbl;
6093 
6094 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6095 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6096 	else
6097 		entries = HW_HASH_INDEX_SIZE;
6098 
6099 	bp->rss_indir_tbl_entries = entries;
6100 	tbl = kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6101 	if (!tbl)
6102 		return -ENOMEM;
6103 
6104 	if (rss_ctx)
6105 		rss_ctx->rss_indir_tbl = tbl;
6106 	else
6107 		bp->rss_indir_tbl = tbl;
6108 
6109 	return 0;
6110 }
6111 
6112 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx)
6113 {
6114 	u16 max_rings, max_entries, pad, i;
6115 	u16 *rss_indir_tbl;
6116 
6117 	if (!bp->rx_nr_rings)
6118 		return;
6119 
6120 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6121 		max_rings = bp->rx_nr_rings - 1;
6122 	else
6123 		max_rings = bp->rx_nr_rings;
6124 
6125 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6126 	if (rss_ctx)
6127 		rss_indir_tbl = &rss_ctx->rss_indir_tbl[0];
6128 	else
6129 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6130 
6131 	for (i = 0; i < max_entries; i++)
6132 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6133 
6134 	pad = bp->rss_indir_tbl_entries - max_entries;
6135 	if (pad)
6136 		memset(&rss_indir_tbl[i], 0, pad * sizeof(u16));
6137 }
6138 
6139 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6140 {
6141 	u16 i, tbl_size, max_ring = 0;
6142 
6143 	if (!bp->rss_indir_tbl)
6144 		return 0;
6145 
6146 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6147 	for (i = 0; i < tbl_size; i++)
6148 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6149 	return max_ring;
6150 }
6151 
6152 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6153 {
6154 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6155 		if (!rx_rings)
6156 			return 0;
6157 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6158 					       BNXT_RSS_TABLE_ENTRIES_P5);
6159 	}
6160 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6161 		return 2;
6162 	return 1;
6163 }
6164 
6165 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6166 {
6167 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6168 	u16 i, j;
6169 
6170 	/* Fill the RSS indirection table with ring group ids */
6171 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6172 		if (!no_rss)
6173 			j = bp->rss_indir_tbl[i];
6174 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6175 	}
6176 }
6177 
6178 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6179 				    struct bnxt_vnic_info *vnic)
6180 {
6181 	__le16 *ring_tbl = vnic->rss_table;
6182 	struct bnxt_rx_ring_info *rxr;
6183 	u16 tbl_size, i;
6184 
6185 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6186 
6187 	for (i = 0; i < tbl_size; i++) {
6188 		u16 ring_id, j;
6189 
6190 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6191 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6192 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6193 			j = vnic->rss_ctx->rss_indir_tbl[i];
6194 		else
6195 			j = bp->rss_indir_tbl[i];
6196 		rxr = &bp->rx_ring[j];
6197 
6198 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6199 		*ring_tbl++ = cpu_to_le16(ring_id);
6200 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6201 		*ring_tbl++ = cpu_to_le16(ring_id);
6202 	}
6203 }
6204 
6205 static void
6206 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6207 			 struct bnxt_vnic_info *vnic)
6208 {
6209 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6210 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6211 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6212 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6213 	} else {
6214 		bnxt_fill_hw_rss_tbl(bp, vnic);
6215 	}
6216 
6217 	if (bp->rss_hash_delta) {
6218 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6219 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6220 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6221 		else
6222 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6223 	} else {
6224 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6225 	}
6226 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6227 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6228 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6229 }
6230 
6231 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6232 				  bool set_rss)
6233 {
6234 	struct hwrm_vnic_rss_cfg_input *req;
6235 	int rc;
6236 
6237 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6238 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6239 		return 0;
6240 
6241 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6242 	if (rc)
6243 		return rc;
6244 
6245 	if (set_rss)
6246 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6247 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6248 	return hwrm_req_send(bp, req);
6249 }
6250 
6251 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6252 				     struct bnxt_vnic_info *vnic, bool set_rss)
6253 {
6254 	struct hwrm_vnic_rss_cfg_input *req;
6255 	dma_addr_t ring_tbl_map;
6256 	u32 i, nr_ctxs;
6257 	int rc;
6258 
6259 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6260 	if (rc)
6261 		return rc;
6262 
6263 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6264 	if (!set_rss)
6265 		return hwrm_req_send(bp, req);
6266 
6267 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6268 	ring_tbl_map = vnic->rss_table_dma_addr;
6269 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6270 
6271 	hwrm_req_hold(bp, req);
6272 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6273 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6274 		req->ring_table_pair_index = i;
6275 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6276 		rc = hwrm_req_send(bp, req);
6277 		if (rc)
6278 			goto exit;
6279 	}
6280 
6281 exit:
6282 	hwrm_req_drop(bp, req);
6283 	return rc;
6284 }
6285 
6286 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6287 {
6288 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6289 	struct hwrm_vnic_rss_qcfg_output *resp;
6290 	struct hwrm_vnic_rss_qcfg_input *req;
6291 
6292 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6293 		return;
6294 
6295 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6296 	/* all contexts configured to same hash_type, zero always exists */
6297 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6298 	resp = hwrm_req_hold(bp, req);
6299 	if (!hwrm_req_send(bp, req)) {
6300 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6301 		bp->rss_hash_delta = 0;
6302 	}
6303 	hwrm_req_drop(bp, req);
6304 }
6305 
6306 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6307 {
6308 	struct hwrm_vnic_plcmodes_cfg_input *req;
6309 	int rc;
6310 
6311 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6312 	if (rc)
6313 		return rc;
6314 
6315 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6316 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6317 
6318 	if (BNXT_RX_PAGE_MODE(bp)) {
6319 		req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6320 	} else {
6321 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6322 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6323 		req->enables |=
6324 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6325 		req->jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
6326 		req->hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
6327 	}
6328 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6329 	return hwrm_req_send(bp, req);
6330 }
6331 
6332 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6333 					struct bnxt_vnic_info *vnic,
6334 					u16 ctx_idx)
6335 {
6336 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6337 
6338 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6339 		return;
6340 
6341 	req->rss_cos_lb_ctx_id =
6342 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6343 
6344 	hwrm_req_send(bp, req);
6345 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6346 }
6347 
6348 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6349 {
6350 	int i, j;
6351 
6352 	for (i = 0; i < bp->nr_vnics; i++) {
6353 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6354 
6355 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6356 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6357 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6358 		}
6359 	}
6360 	bp->rsscos_nr_ctxs = 0;
6361 }
6362 
6363 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6364 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6365 {
6366 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6367 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6368 	int rc;
6369 
6370 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6371 	if (rc)
6372 		return rc;
6373 
6374 	resp = hwrm_req_hold(bp, req);
6375 	rc = hwrm_req_send(bp, req);
6376 	if (!rc)
6377 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6378 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6379 	hwrm_req_drop(bp, req);
6380 
6381 	return rc;
6382 }
6383 
6384 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6385 {
6386 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6387 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6388 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6389 }
6390 
6391 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6392 {
6393 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6394 	struct hwrm_vnic_cfg_input *req;
6395 	unsigned int ring = 0, grp_idx;
6396 	u16 def_vlan = 0;
6397 	int rc;
6398 
6399 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6400 	if (rc)
6401 		return rc;
6402 
6403 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6404 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6405 
6406 		req->default_rx_ring_id =
6407 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6408 		req->default_cmpl_ring_id =
6409 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6410 		req->enables =
6411 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6412 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6413 		goto vnic_mru;
6414 	}
6415 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6416 	/* Only RSS support for now TBD: COS & LB */
6417 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6418 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6419 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6420 					   VNIC_CFG_REQ_ENABLES_MRU);
6421 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6422 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6423 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6424 					   VNIC_CFG_REQ_ENABLES_MRU);
6425 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6426 	} else {
6427 		req->rss_rule = cpu_to_le16(0xffff);
6428 	}
6429 
6430 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6431 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6432 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6433 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6434 	} else {
6435 		req->cos_rule = cpu_to_le16(0xffff);
6436 	}
6437 
6438 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6439 		ring = 0;
6440 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6441 		ring = vnic->vnic_id - 1;
6442 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6443 		ring = bp->rx_nr_rings - 1;
6444 
6445 	grp_idx = bp->rx_ring[ring].bnapi->index;
6446 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6447 	req->lb_rule = cpu_to_le16(0xffff);
6448 vnic_mru:
6449 	req->mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + VLAN_HLEN);
6450 
6451 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6452 #ifdef CONFIG_BNXT_SRIOV
6453 	if (BNXT_VF(bp))
6454 		def_vlan = bp->vf.vlan;
6455 #endif
6456 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6457 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6458 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6459 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6460 
6461 	return hwrm_req_send(bp, req);
6462 }
6463 
6464 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6465 				    struct bnxt_vnic_info *vnic)
6466 {
6467 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6468 		struct hwrm_vnic_free_input *req;
6469 
6470 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6471 			return;
6472 
6473 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6474 
6475 		hwrm_req_send(bp, req);
6476 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6477 	}
6478 }
6479 
6480 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6481 {
6482 	u16 i;
6483 
6484 	for (i = 0; i < bp->nr_vnics; i++)
6485 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6486 }
6487 
6488 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6489 			 unsigned int start_rx_ring_idx,
6490 			 unsigned int nr_rings)
6491 {
6492 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6493 	struct hwrm_vnic_alloc_output *resp;
6494 	struct hwrm_vnic_alloc_input *req;
6495 	int rc;
6496 
6497 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6498 	if (rc)
6499 		return rc;
6500 
6501 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6502 		goto vnic_no_ring_grps;
6503 
6504 	/* map ring groups to this vnic */
6505 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6506 		grp_idx = bp->rx_ring[i].bnapi->index;
6507 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6508 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6509 				   j, nr_rings);
6510 			break;
6511 		}
6512 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6513 	}
6514 
6515 vnic_no_ring_grps:
6516 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6517 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6518 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6519 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6520 
6521 	resp = hwrm_req_hold(bp, req);
6522 	rc = hwrm_req_send(bp, req);
6523 	if (!rc)
6524 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6525 	hwrm_req_drop(bp, req);
6526 	return rc;
6527 }
6528 
6529 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6530 {
6531 	struct hwrm_vnic_qcaps_output *resp;
6532 	struct hwrm_vnic_qcaps_input *req;
6533 	int rc;
6534 
6535 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6536 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6537 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6538 	if (bp->hwrm_spec_code < 0x10600)
6539 		return 0;
6540 
6541 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6542 	if (rc)
6543 		return rc;
6544 
6545 	resp = hwrm_req_hold(bp, req);
6546 	rc = hwrm_req_send(bp, req);
6547 	if (!rc) {
6548 		u32 flags = le32_to_cpu(resp->flags);
6549 
6550 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6551 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6552 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6553 		if (flags &
6554 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6555 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6556 
6557 		/* Older P5 fw before EXT_HW_STATS support did not set
6558 		 * VLAN_STRIP_CAP properly.
6559 		 */
6560 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6561 		    (BNXT_CHIP_P5(bp) &&
6562 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6563 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6564 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6565 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6566 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6567 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6568 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6569 		if (bp->max_tpa_v2) {
6570 			if (BNXT_CHIP_P5(bp))
6571 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6572 			else
6573 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6574 		}
6575 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6576 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6577 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6578 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6579 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6580 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6581 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6582 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6583 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6584 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6585 	}
6586 	hwrm_req_drop(bp, req);
6587 	return rc;
6588 }
6589 
6590 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6591 {
6592 	struct hwrm_ring_grp_alloc_output *resp;
6593 	struct hwrm_ring_grp_alloc_input *req;
6594 	int rc;
6595 	u16 i;
6596 
6597 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6598 		return 0;
6599 
6600 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6601 	if (rc)
6602 		return rc;
6603 
6604 	resp = hwrm_req_hold(bp, req);
6605 	for (i = 0; i < bp->rx_nr_rings; i++) {
6606 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
6607 
6608 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
6609 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
6610 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
6611 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
6612 
6613 		rc = hwrm_req_send(bp, req);
6614 
6615 		if (rc)
6616 			break;
6617 
6618 		bp->grp_info[grp_idx].fw_grp_id =
6619 			le32_to_cpu(resp->ring_group_id);
6620 	}
6621 	hwrm_req_drop(bp, req);
6622 	return rc;
6623 }
6624 
6625 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
6626 {
6627 	struct hwrm_ring_grp_free_input *req;
6628 	u16 i;
6629 
6630 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
6631 		return;
6632 
6633 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
6634 		return;
6635 
6636 	hwrm_req_hold(bp, req);
6637 	for (i = 0; i < bp->cp_nr_rings; i++) {
6638 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
6639 			continue;
6640 		req->ring_group_id =
6641 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
6642 
6643 		hwrm_req_send(bp, req);
6644 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
6645 	}
6646 	hwrm_req_drop(bp, req);
6647 }
6648 
6649 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
6650 				    struct bnxt_ring_struct *ring,
6651 				    u32 ring_type, u32 map_index)
6652 {
6653 	struct hwrm_ring_alloc_output *resp;
6654 	struct hwrm_ring_alloc_input *req;
6655 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
6656 	struct bnxt_ring_grp_info *grp_info;
6657 	int rc, err = 0;
6658 	u16 ring_id;
6659 
6660 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
6661 	if (rc)
6662 		goto exit;
6663 
6664 	req->enables = 0;
6665 	if (rmem->nr_pages > 1) {
6666 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
6667 		/* Page size is in log2 units */
6668 		req->page_size = BNXT_PAGE_SHIFT;
6669 		req->page_tbl_depth = 1;
6670 	} else {
6671 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
6672 	}
6673 	req->fbo = 0;
6674 	/* Association of ring index with doorbell index and MSIX number */
6675 	req->logical_id = cpu_to_le16(map_index);
6676 
6677 	switch (ring_type) {
6678 	case HWRM_RING_ALLOC_TX: {
6679 		struct bnxt_tx_ring_info *txr;
6680 
6681 		txr = container_of(ring, struct bnxt_tx_ring_info,
6682 				   tx_ring_struct);
6683 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
6684 		/* Association of transmit ring with completion ring */
6685 		grp_info = &bp->grp_info[ring->grp_idx];
6686 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
6687 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
6688 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6689 		req->queue_id = cpu_to_le16(ring->queue_id);
6690 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
6691 			req->cmpl_coal_cnt =
6692 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
6693 		break;
6694 	}
6695 	case HWRM_RING_ALLOC_RX:
6696 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6697 		req->length = cpu_to_le32(bp->rx_ring_mask + 1);
6698 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6699 			u16 flags = 0;
6700 
6701 			/* Association of rx ring with stats context */
6702 			grp_info = &bp->grp_info[ring->grp_idx];
6703 			req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
6704 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6705 			req->enables |= cpu_to_le32(
6706 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6707 			if (NET_IP_ALIGN == 2)
6708 				flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
6709 			req->flags = cpu_to_le16(flags);
6710 		}
6711 		break;
6712 	case HWRM_RING_ALLOC_AGG:
6713 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6714 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
6715 			/* Association of agg ring with rx ring */
6716 			grp_info = &bp->grp_info[ring->grp_idx];
6717 			req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
6718 			req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
6719 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6720 			req->enables |= cpu_to_le32(
6721 				RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
6722 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6723 		} else {
6724 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6725 		}
6726 		req->length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
6727 		break;
6728 	case HWRM_RING_ALLOC_CMPL:
6729 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
6730 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
6731 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6732 			/* Association of cp ring with nq */
6733 			grp_info = &bp->grp_info[map_index];
6734 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
6735 			req->cq_handle = cpu_to_le64(ring->handle);
6736 			req->enables |= cpu_to_le32(
6737 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
6738 		} else if (bp->flags & BNXT_FLAG_USING_MSIX) {
6739 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
6740 		}
6741 		break;
6742 	case HWRM_RING_ALLOC_NQ:
6743 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
6744 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
6745 		if (bp->flags & BNXT_FLAG_USING_MSIX)
6746 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
6747 		break;
6748 	default:
6749 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
6750 			   ring_type);
6751 		return -1;
6752 	}
6753 
6754 	resp = hwrm_req_hold(bp, req);
6755 	rc = hwrm_req_send(bp, req);
6756 	err = le16_to_cpu(resp->error_code);
6757 	ring_id = le16_to_cpu(resp->ring_id);
6758 	hwrm_req_drop(bp, req);
6759 
6760 exit:
6761 	if (rc || err) {
6762 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
6763 			   ring_type, rc, err);
6764 		return -EIO;
6765 	}
6766 	ring->fw_ring_id = ring_id;
6767 	return rc;
6768 }
6769 
6770 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
6771 {
6772 	int rc;
6773 
6774 	if (BNXT_PF(bp)) {
6775 		struct hwrm_func_cfg_input *req;
6776 
6777 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
6778 		if (rc)
6779 			return rc;
6780 
6781 		req->fid = cpu_to_le16(0xffff);
6782 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
6783 		req->async_event_cr = cpu_to_le16(idx);
6784 		return hwrm_req_send(bp, req);
6785 	} else {
6786 		struct hwrm_func_vf_cfg_input *req;
6787 
6788 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
6789 		if (rc)
6790 			return rc;
6791 
6792 		req->enables =
6793 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
6794 		req->async_event_cr = cpu_to_le16(idx);
6795 		return hwrm_req_send(bp, req);
6796 	}
6797 }
6798 
6799 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
6800 			     u32 ring_type)
6801 {
6802 	switch (ring_type) {
6803 	case HWRM_RING_ALLOC_TX:
6804 		db->db_ring_mask = bp->tx_ring_mask;
6805 		break;
6806 	case HWRM_RING_ALLOC_RX:
6807 		db->db_ring_mask = bp->rx_ring_mask;
6808 		break;
6809 	case HWRM_RING_ALLOC_AGG:
6810 		db->db_ring_mask = bp->rx_agg_ring_mask;
6811 		break;
6812 	case HWRM_RING_ALLOC_CMPL:
6813 	case HWRM_RING_ALLOC_NQ:
6814 		db->db_ring_mask = bp->cp_ring_mask;
6815 		break;
6816 	}
6817 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
6818 		db->db_epoch_mask = db->db_ring_mask + 1;
6819 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
6820 	}
6821 }
6822 
6823 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
6824 			u32 map_idx, u32 xid)
6825 {
6826 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6827 		switch (ring_type) {
6828 		case HWRM_RING_ALLOC_TX:
6829 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
6830 			break;
6831 		case HWRM_RING_ALLOC_RX:
6832 		case HWRM_RING_ALLOC_AGG:
6833 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
6834 			break;
6835 		case HWRM_RING_ALLOC_CMPL:
6836 			db->db_key64 = DBR_PATH_L2;
6837 			break;
6838 		case HWRM_RING_ALLOC_NQ:
6839 			db->db_key64 = DBR_PATH_L2;
6840 			break;
6841 		}
6842 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
6843 
6844 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6845 			db->db_key64 |= DBR_VALID;
6846 
6847 		db->doorbell = bp->bar1 + bp->db_offset;
6848 	} else {
6849 		db->doorbell = bp->bar1 + map_idx * 0x80;
6850 		switch (ring_type) {
6851 		case HWRM_RING_ALLOC_TX:
6852 			db->db_key32 = DB_KEY_TX;
6853 			break;
6854 		case HWRM_RING_ALLOC_RX:
6855 		case HWRM_RING_ALLOC_AGG:
6856 			db->db_key32 = DB_KEY_RX;
6857 			break;
6858 		case HWRM_RING_ALLOC_CMPL:
6859 			db->db_key32 = DB_KEY_CP;
6860 			break;
6861 		}
6862 	}
6863 	bnxt_set_db_mask(bp, db, ring_type);
6864 }
6865 
6866 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
6867 {
6868 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
6869 	int i, rc = 0;
6870 	u32 type;
6871 
6872 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6873 		type = HWRM_RING_ALLOC_NQ;
6874 	else
6875 		type = HWRM_RING_ALLOC_CMPL;
6876 	for (i = 0; i < bp->cp_nr_rings; i++) {
6877 		struct bnxt_napi *bnapi = bp->bnapi[i];
6878 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6879 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
6880 		u32 map_idx = ring->map_idx;
6881 		unsigned int vector;
6882 
6883 		vector = bp->irq_tbl[map_idx].vector;
6884 		disable_irq_nosync(vector);
6885 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6886 		if (rc) {
6887 			enable_irq(vector);
6888 			goto err_out;
6889 		}
6890 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
6891 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
6892 		enable_irq(vector);
6893 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
6894 
6895 		if (!i) {
6896 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
6897 			if (rc)
6898 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
6899 		}
6900 	}
6901 
6902 	type = HWRM_RING_ALLOC_TX;
6903 	for (i = 0; i < bp->tx_nr_rings; i++) {
6904 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
6905 		struct bnxt_ring_struct *ring;
6906 		u32 map_idx;
6907 
6908 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6909 			struct bnxt_cp_ring_info *cpr2 = txr->tx_cpr;
6910 			struct bnxt_napi *bnapi = txr->bnapi;
6911 			u32 type2 = HWRM_RING_ALLOC_CMPL;
6912 
6913 			ring = &cpr2->cp_ring_struct;
6914 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
6915 			map_idx = bnapi->index;
6916 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
6917 			if (rc)
6918 				goto err_out;
6919 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
6920 				    ring->fw_ring_id);
6921 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
6922 		}
6923 		ring = &txr->tx_ring_struct;
6924 		map_idx = i;
6925 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6926 		if (rc)
6927 			goto err_out;
6928 		bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
6929 	}
6930 
6931 	type = HWRM_RING_ALLOC_RX;
6932 	for (i = 0; i < bp->rx_nr_rings; i++) {
6933 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
6934 		struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
6935 		struct bnxt_napi *bnapi = rxr->bnapi;
6936 		u32 map_idx = bnapi->index;
6937 
6938 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6939 		if (rc)
6940 			goto err_out;
6941 		bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
6942 		/* If we have agg rings, post agg buffers first. */
6943 		if (!agg_rings)
6944 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
6945 		bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
6946 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6947 			struct bnxt_cp_ring_info *cpr2 = rxr->rx_cpr;
6948 			u32 type2 = HWRM_RING_ALLOC_CMPL;
6949 
6950 			ring = &cpr2->cp_ring_struct;
6951 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
6952 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
6953 			if (rc)
6954 				goto err_out;
6955 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
6956 				    ring->fw_ring_id);
6957 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
6958 		}
6959 	}
6960 
6961 	if (agg_rings) {
6962 		type = HWRM_RING_ALLOC_AGG;
6963 		for (i = 0; i < bp->rx_nr_rings; i++) {
6964 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
6965 			struct bnxt_ring_struct *ring =
6966 						&rxr->rx_agg_ring_struct;
6967 			u32 grp_idx = ring->grp_idx;
6968 			u32 map_idx = grp_idx + bp->rx_nr_rings;
6969 
6970 			rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6971 			if (rc)
6972 				goto err_out;
6973 
6974 			bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
6975 				    ring->fw_ring_id);
6976 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
6977 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
6978 			bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
6979 		}
6980 	}
6981 err_out:
6982 	return rc;
6983 }
6984 
6985 static int hwrm_ring_free_send_msg(struct bnxt *bp,
6986 				   struct bnxt_ring_struct *ring,
6987 				   u32 ring_type, int cmpl_ring_id)
6988 {
6989 	struct hwrm_ring_free_output *resp;
6990 	struct hwrm_ring_free_input *req;
6991 	u16 error_code = 0;
6992 	int rc;
6993 
6994 	if (BNXT_NO_FW_ACCESS(bp))
6995 		return 0;
6996 
6997 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
6998 	if (rc)
6999 		goto exit;
7000 
7001 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7002 	req->ring_type = ring_type;
7003 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7004 
7005 	resp = hwrm_req_hold(bp, req);
7006 	rc = hwrm_req_send(bp, req);
7007 	error_code = le16_to_cpu(resp->error_code);
7008 	hwrm_req_drop(bp, req);
7009 exit:
7010 	if (rc || error_code) {
7011 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7012 			   ring_type, rc, error_code);
7013 		return -EIO;
7014 	}
7015 	return 0;
7016 }
7017 
7018 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7019 {
7020 	u32 type;
7021 	int i;
7022 
7023 	if (!bp->bnapi)
7024 		return;
7025 
7026 	for (i = 0; i < bp->tx_nr_rings; i++) {
7027 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7028 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7029 
7030 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7031 			u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
7032 
7033 			hwrm_ring_free_send_msg(bp, ring,
7034 						RING_FREE_REQ_RING_TYPE_TX,
7035 						close_path ? cmpl_ring_id :
7036 						INVALID_HW_RING_ID);
7037 			ring->fw_ring_id = INVALID_HW_RING_ID;
7038 		}
7039 	}
7040 
7041 	for (i = 0; i < bp->rx_nr_rings; i++) {
7042 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7043 		struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7044 		u32 grp_idx = rxr->bnapi->index;
7045 
7046 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7047 			u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7048 
7049 			hwrm_ring_free_send_msg(bp, ring,
7050 						RING_FREE_REQ_RING_TYPE_RX,
7051 						close_path ? cmpl_ring_id :
7052 						INVALID_HW_RING_ID);
7053 			ring->fw_ring_id = INVALID_HW_RING_ID;
7054 			bp->grp_info[grp_idx].rx_fw_ring_id =
7055 				INVALID_HW_RING_ID;
7056 		}
7057 	}
7058 
7059 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7060 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7061 	else
7062 		type = RING_FREE_REQ_RING_TYPE_RX;
7063 	for (i = 0; i < bp->rx_nr_rings; i++) {
7064 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7065 		struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7066 		u32 grp_idx = rxr->bnapi->index;
7067 
7068 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7069 			u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7070 
7071 			hwrm_ring_free_send_msg(bp, ring, type,
7072 						close_path ? cmpl_ring_id :
7073 						INVALID_HW_RING_ID);
7074 			ring->fw_ring_id = INVALID_HW_RING_ID;
7075 			bp->grp_info[grp_idx].agg_fw_ring_id =
7076 				INVALID_HW_RING_ID;
7077 		}
7078 	}
7079 
7080 	/* The completion rings are about to be freed.  After that the
7081 	 * IRQ doorbell will not work anymore.  So we need to disable
7082 	 * IRQ here.
7083 	 */
7084 	bnxt_disable_int_sync(bp);
7085 
7086 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7087 		type = RING_FREE_REQ_RING_TYPE_NQ;
7088 	else
7089 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7090 	for (i = 0; i < bp->cp_nr_rings; i++) {
7091 		struct bnxt_napi *bnapi = bp->bnapi[i];
7092 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7093 		struct bnxt_ring_struct *ring;
7094 		int j;
7095 
7096 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++) {
7097 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
7098 
7099 			ring = &cpr2->cp_ring_struct;
7100 			if (ring->fw_ring_id == INVALID_HW_RING_ID)
7101 				continue;
7102 			hwrm_ring_free_send_msg(bp, ring,
7103 						RING_FREE_REQ_RING_TYPE_L2_CMPL,
7104 						INVALID_HW_RING_ID);
7105 			ring->fw_ring_id = INVALID_HW_RING_ID;
7106 		}
7107 		ring = &cpr->cp_ring_struct;
7108 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7109 			hwrm_ring_free_send_msg(bp, ring, type,
7110 						INVALID_HW_RING_ID);
7111 			ring->fw_ring_id = INVALID_HW_RING_ID;
7112 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7113 		}
7114 	}
7115 }
7116 
7117 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7118 			     bool shared);
7119 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7120 			   bool shared);
7121 
7122 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7123 {
7124 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7125 	struct hwrm_func_qcfg_output *resp;
7126 	struct hwrm_func_qcfg_input *req;
7127 	int rc;
7128 
7129 	if (bp->hwrm_spec_code < 0x10601)
7130 		return 0;
7131 
7132 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7133 	if (rc)
7134 		return rc;
7135 
7136 	req->fid = cpu_to_le16(0xffff);
7137 	resp = hwrm_req_hold(bp, req);
7138 	rc = hwrm_req_send(bp, req);
7139 	if (rc) {
7140 		hwrm_req_drop(bp, req);
7141 		return rc;
7142 	}
7143 
7144 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7145 	if (BNXT_NEW_RM(bp)) {
7146 		u16 cp, stats;
7147 
7148 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7149 		hw_resc->resv_hw_ring_grps =
7150 			le32_to_cpu(resp->alloc_hw_ring_grps);
7151 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7152 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7153 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7154 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7155 		hw_resc->resv_irqs = cp;
7156 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7157 			int rx = hw_resc->resv_rx_rings;
7158 			int tx = hw_resc->resv_tx_rings;
7159 
7160 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7161 				rx >>= 1;
7162 			if (cp < (rx + tx)) {
7163 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7164 				if (rc)
7165 					goto get_rings_exit;
7166 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7167 					rx <<= 1;
7168 				hw_resc->resv_rx_rings = rx;
7169 				hw_resc->resv_tx_rings = tx;
7170 			}
7171 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7172 			hw_resc->resv_hw_ring_grps = rx;
7173 		}
7174 		hw_resc->resv_cp_rings = cp;
7175 		hw_resc->resv_stat_ctxs = stats;
7176 	}
7177 get_rings_exit:
7178 	hwrm_req_drop(bp, req);
7179 	return rc;
7180 }
7181 
7182 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7183 {
7184 	struct hwrm_func_qcfg_output *resp;
7185 	struct hwrm_func_qcfg_input *req;
7186 	int rc;
7187 
7188 	if (bp->hwrm_spec_code < 0x10601)
7189 		return 0;
7190 
7191 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7192 	if (rc)
7193 		return rc;
7194 
7195 	req->fid = cpu_to_le16(fid);
7196 	resp = hwrm_req_hold(bp, req);
7197 	rc = hwrm_req_send(bp, req);
7198 	if (!rc)
7199 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7200 
7201 	hwrm_req_drop(bp, req);
7202 	return rc;
7203 }
7204 
7205 static bool bnxt_rfs_supported(struct bnxt *bp);
7206 
7207 static struct hwrm_func_cfg_input *
7208 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7209 {
7210 	struct hwrm_func_cfg_input *req;
7211 	u32 enables = 0;
7212 
7213 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7214 		return NULL;
7215 
7216 	req->fid = cpu_to_le16(0xffff);
7217 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7218 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7219 	if (BNXT_NEW_RM(bp)) {
7220 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7221 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7222 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7223 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7224 			enables |= hwr->cp_p5 ?
7225 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7226 		} else {
7227 			enables |= hwr->cp ?
7228 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7229 			enables |= hwr->grp ?
7230 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7231 		}
7232 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7233 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7234 					  0;
7235 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7236 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7237 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7238 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7239 			req->num_msix = cpu_to_le16(hwr->cp);
7240 		} else {
7241 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7242 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7243 		}
7244 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7245 		req->num_vnics = cpu_to_le16(hwr->vnic);
7246 	}
7247 	req->enables = cpu_to_le32(enables);
7248 	return req;
7249 }
7250 
7251 static struct hwrm_func_vf_cfg_input *
7252 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7253 {
7254 	struct hwrm_func_vf_cfg_input *req;
7255 	u32 enables = 0;
7256 
7257 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7258 		return NULL;
7259 
7260 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7261 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7262 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7263 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7264 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7265 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7266 		enables |= hwr->cp_p5 ?
7267 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7268 	} else {
7269 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7270 		enables |= hwr->grp ?
7271 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7272 	}
7273 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7274 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7275 
7276 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7277 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7278 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7279 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7280 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7281 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7282 	} else {
7283 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7284 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7285 	}
7286 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7287 	req->num_vnics = cpu_to_le16(hwr->vnic);
7288 
7289 	req->enables = cpu_to_le32(enables);
7290 	return req;
7291 }
7292 
7293 static int
7294 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7295 {
7296 	struct hwrm_func_cfg_input *req;
7297 	int rc;
7298 
7299 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7300 	if (!req)
7301 		return -ENOMEM;
7302 
7303 	if (!req->enables) {
7304 		hwrm_req_drop(bp, req);
7305 		return 0;
7306 	}
7307 
7308 	rc = hwrm_req_send(bp, req);
7309 	if (rc)
7310 		return rc;
7311 
7312 	if (bp->hwrm_spec_code < 0x10601)
7313 		bp->hw_resc.resv_tx_rings = hwr->tx;
7314 
7315 	return bnxt_hwrm_get_rings(bp);
7316 }
7317 
7318 static int
7319 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7320 {
7321 	struct hwrm_func_vf_cfg_input *req;
7322 	int rc;
7323 
7324 	if (!BNXT_NEW_RM(bp)) {
7325 		bp->hw_resc.resv_tx_rings = hwr->tx;
7326 		return 0;
7327 	}
7328 
7329 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7330 	if (!req)
7331 		return -ENOMEM;
7332 
7333 	rc = hwrm_req_send(bp, req);
7334 	if (rc)
7335 		return rc;
7336 
7337 	return bnxt_hwrm_get_rings(bp);
7338 }
7339 
7340 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7341 {
7342 	if (BNXT_PF(bp))
7343 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7344 	else
7345 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7346 }
7347 
7348 int bnxt_nq_rings_in_use(struct bnxt *bp)
7349 {
7350 	int cp = bp->cp_nr_rings;
7351 	int ulp_msix, ulp_base;
7352 
7353 	ulp_msix = bnxt_get_ulp_msix_num(bp);
7354 	if (ulp_msix) {
7355 		ulp_base = bnxt_get_ulp_msix_base(bp);
7356 		cp += ulp_msix;
7357 		if ((ulp_base + ulp_msix) > cp)
7358 			cp = ulp_base + ulp_msix;
7359 	}
7360 	return cp;
7361 }
7362 
7363 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7364 {
7365 	int cp;
7366 
7367 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7368 		return bnxt_nq_rings_in_use(bp);
7369 
7370 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7371 	return cp;
7372 }
7373 
7374 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7375 {
7376 	int ulp_stat = bnxt_get_ulp_stat_ctxs(bp);
7377 	int cp = bp->cp_nr_rings;
7378 
7379 	if (!ulp_stat)
7380 		return cp;
7381 
7382 	if (bnxt_nq_rings_in_use(bp) > cp + bnxt_get_ulp_msix_num(bp))
7383 		return bnxt_get_ulp_msix_base(bp) + ulp_stat;
7384 
7385 	return cp + ulp_stat;
7386 }
7387 
7388 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7389 {
7390 	if (!hwr->grp)
7391 		return 0;
7392 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7393 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7394 
7395 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7396 			rss_ctx *= hwr->vnic;
7397 		return rss_ctx;
7398 	}
7399 	if (BNXT_VF(bp))
7400 		return BNXT_VF_MAX_RSS_CTX;
7401 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7402 		return hwr->grp + 1;
7403 	return 1;
7404 }
7405 
7406 /* Check if a default RSS map needs to be setup.  This function is only
7407  * used on older firmware that does not require reserving RX rings.
7408  */
7409 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7410 {
7411 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7412 
7413 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7414 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7415 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7416 		if (!netif_is_rxfh_configured(bp->dev))
7417 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7418 	}
7419 }
7420 
7421 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7422 {
7423 	if (bp->flags & BNXT_FLAG_RFS) {
7424 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7425 			return 2 + bp->num_rss_ctx;
7426 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7427 			return rx_rings + 1;
7428 	}
7429 	return 1;
7430 }
7431 
7432 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7433 {
7434 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7435 	int cp = bnxt_cp_rings_in_use(bp);
7436 	int nq = bnxt_nq_rings_in_use(bp);
7437 	int rx = bp->rx_nr_rings, stat;
7438 	int vnic, grp = rx;
7439 
7440 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7441 	    bp->hwrm_spec_code >= 0x10601)
7442 		return true;
7443 
7444 	/* Old firmware does not need RX ring reservations but we still
7445 	 * need to setup a default RSS map when needed.  With new firmware
7446 	 * we go through RX ring reservations first and then set up the
7447 	 * RSS map for the successfully reserved RX rings when needed.
7448 	 */
7449 	if (!BNXT_NEW_RM(bp)) {
7450 		bnxt_check_rss_tbl_no_rmgr(bp);
7451 		return false;
7452 	}
7453 
7454 	vnic = bnxt_get_total_vnics(bp, rx);
7455 
7456 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7457 		rx <<= 1;
7458 	stat = bnxt_get_func_stat_ctxs(bp);
7459 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7460 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7461 	    (hw_resc->resv_hw_ring_grps != grp &&
7462 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7463 		return true;
7464 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7465 	    hw_resc->resv_irqs != nq)
7466 		return true;
7467 	return false;
7468 }
7469 
7470 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7471 {
7472 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7473 
7474 	hwr->tx = hw_resc->resv_tx_rings;
7475 	if (BNXT_NEW_RM(bp)) {
7476 		hwr->rx = hw_resc->resv_rx_rings;
7477 		hwr->cp = hw_resc->resv_irqs;
7478 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7479 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7480 		hwr->grp = hw_resc->resv_hw_ring_grps;
7481 		hwr->vnic = hw_resc->resv_vnics;
7482 		hwr->stat = hw_resc->resv_stat_ctxs;
7483 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7484 	}
7485 }
7486 
7487 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7488 {
7489 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7490 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7491 }
7492 
7493 static int __bnxt_reserve_rings(struct bnxt *bp)
7494 {
7495 	struct bnxt_hw_rings hwr = {0};
7496 	int rx_rings, rc;
7497 	bool sh = false;
7498 	int tx_cp;
7499 
7500 	if (!bnxt_need_reserve_rings(bp))
7501 		return 0;
7502 
7503 	hwr.cp = bnxt_nq_rings_in_use(bp);
7504 	hwr.tx = bp->tx_nr_rings;
7505 	hwr.rx = bp->rx_nr_rings;
7506 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7507 		sh = true;
7508 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7509 		hwr.cp_p5 = hwr.rx + hwr.tx;
7510 
7511 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7512 
7513 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7514 		hwr.rx <<= 1;
7515 	hwr.grp = bp->rx_nr_rings;
7516 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7517 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
7518 
7519 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
7520 	if (rc)
7521 		return rc;
7522 
7523 	bnxt_copy_reserved_rings(bp, &hwr);
7524 
7525 	rx_rings = hwr.rx;
7526 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7527 		if (hwr.rx >= 2) {
7528 			rx_rings = hwr.rx >> 1;
7529 		} else {
7530 			if (netif_running(bp->dev))
7531 				return -ENOMEM;
7532 
7533 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7534 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7535 			bp->dev->hw_features &= ~NETIF_F_LRO;
7536 			bp->dev->features &= ~NETIF_F_LRO;
7537 			bnxt_set_ring_params(bp);
7538 		}
7539 	}
7540 	rx_rings = min_t(int, rx_rings, hwr.grp);
7541 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
7542 	if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
7543 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
7544 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
7545 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
7546 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7547 		hwr.rx = rx_rings << 1;
7548 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
7549 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
7550 	bp->tx_nr_rings = hwr.tx;
7551 
7552 	/* If we cannot reserve all the RX rings, reset the RSS map only
7553 	 * if absolutely necessary
7554 	 */
7555 	if (rx_rings != bp->rx_nr_rings) {
7556 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
7557 			    rx_rings, bp->rx_nr_rings);
7558 		if (netif_is_rxfh_configured(bp->dev) &&
7559 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
7560 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
7561 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
7562 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
7563 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
7564 		}
7565 	}
7566 	bp->rx_nr_rings = rx_rings;
7567 	bp->cp_nr_rings = hwr.cp;
7568 
7569 	if (!bnxt_rings_ok(bp, &hwr))
7570 		return -ENOMEM;
7571 
7572 	if (!netif_is_rxfh_configured(bp->dev))
7573 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7574 
7575 	return rc;
7576 }
7577 
7578 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7579 {
7580 	struct hwrm_func_vf_cfg_input *req;
7581 	u32 flags;
7582 
7583 	if (!BNXT_NEW_RM(bp))
7584 		return 0;
7585 
7586 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7587 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
7588 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7589 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7590 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7591 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
7592 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
7593 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7594 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7595 
7596 	req->flags = cpu_to_le32(flags);
7597 	return hwrm_req_send_silent(bp, req);
7598 }
7599 
7600 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7601 {
7602 	struct hwrm_func_cfg_input *req;
7603 	u32 flags;
7604 
7605 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7606 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
7607 	if (BNXT_NEW_RM(bp)) {
7608 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7609 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7610 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7611 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
7612 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7613 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
7614 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
7615 		else
7616 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7617 	}
7618 
7619 	req->flags = cpu_to_le32(flags);
7620 	return hwrm_req_send_silent(bp, req);
7621 }
7622 
7623 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7624 {
7625 	if (bp->hwrm_spec_code < 0x10801)
7626 		return 0;
7627 
7628 	if (BNXT_PF(bp))
7629 		return bnxt_hwrm_check_pf_rings(bp, hwr);
7630 
7631 	return bnxt_hwrm_check_vf_rings(bp, hwr);
7632 }
7633 
7634 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
7635 {
7636 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7637 	struct hwrm_ring_aggint_qcaps_output *resp;
7638 	struct hwrm_ring_aggint_qcaps_input *req;
7639 	int rc;
7640 
7641 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
7642 	coal_cap->num_cmpl_dma_aggr_max = 63;
7643 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
7644 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
7645 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
7646 	coal_cap->int_lat_tmr_min_max = 65535;
7647 	coal_cap->int_lat_tmr_max_max = 65535;
7648 	coal_cap->num_cmpl_aggr_int_max = 65535;
7649 	coal_cap->timer_units = 80;
7650 
7651 	if (bp->hwrm_spec_code < 0x10902)
7652 		return;
7653 
7654 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
7655 		return;
7656 
7657 	resp = hwrm_req_hold(bp, req);
7658 	rc = hwrm_req_send_silent(bp, req);
7659 	if (!rc) {
7660 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
7661 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
7662 		coal_cap->num_cmpl_dma_aggr_max =
7663 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
7664 		coal_cap->num_cmpl_dma_aggr_during_int_max =
7665 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
7666 		coal_cap->cmpl_aggr_dma_tmr_max =
7667 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
7668 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
7669 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
7670 		coal_cap->int_lat_tmr_min_max =
7671 			le16_to_cpu(resp->int_lat_tmr_min_max);
7672 		coal_cap->int_lat_tmr_max_max =
7673 			le16_to_cpu(resp->int_lat_tmr_max_max);
7674 		coal_cap->num_cmpl_aggr_int_max =
7675 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
7676 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
7677 	}
7678 	hwrm_req_drop(bp, req);
7679 }
7680 
7681 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
7682 {
7683 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7684 
7685 	return usec * 1000 / coal_cap->timer_units;
7686 }
7687 
7688 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
7689 	struct bnxt_coal *hw_coal,
7690 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7691 {
7692 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7693 	u16 val, tmr, max, flags = hw_coal->flags;
7694 	u32 cmpl_params = coal_cap->cmpl_params;
7695 
7696 	max = hw_coal->bufs_per_record * 128;
7697 	if (hw_coal->budget)
7698 		max = hw_coal->bufs_per_record * hw_coal->budget;
7699 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
7700 
7701 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
7702 	req->num_cmpl_aggr_int = cpu_to_le16(val);
7703 
7704 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
7705 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
7706 
7707 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
7708 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
7709 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
7710 
7711 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
7712 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
7713 	req->int_lat_tmr_max = cpu_to_le16(tmr);
7714 
7715 	/* min timer set to 1/2 of interrupt timer */
7716 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
7717 		val = tmr / 2;
7718 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
7719 		req->int_lat_tmr_min = cpu_to_le16(val);
7720 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
7721 	}
7722 
7723 	/* buf timer set to 1/4 of interrupt timer */
7724 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
7725 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
7726 
7727 	if (cmpl_params &
7728 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
7729 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
7730 		val = clamp_t(u16, tmr, 1,
7731 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
7732 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
7733 		req->enables |=
7734 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
7735 	}
7736 
7737 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
7738 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
7739 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
7740 	req->flags = cpu_to_le16(flags);
7741 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
7742 }
7743 
7744 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
7745 				   struct bnxt_coal *hw_coal)
7746 {
7747 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
7748 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7749 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7750 	u32 nq_params = coal_cap->nq_params;
7751 	u16 tmr;
7752 	int rc;
7753 
7754 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
7755 		return 0;
7756 
7757 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7758 	if (rc)
7759 		return rc;
7760 
7761 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
7762 	req->flags =
7763 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
7764 
7765 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
7766 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
7767 	req->int_lat_tmr_min = cpu_to_le16(tmr);
7768 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
7769 	return hwrm_req_send(bp, req);
7770 }
7771 
7772 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
7773 {
7774 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
7775 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7776 	struct bnxt_coal coal;
7777 	int rc;
7778 
7779 	/* Tick values in micro seconds.
7780 	 * 1 coal_buf x bufs_per_record = 1 completion record.
7781 	 */
7782 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
7783 
7784 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
7785 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
7786 
7787 	if (!bnapi->rx_ring)
7788 		return -ENODEV;
7789 
7790 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7791 	if (rc)
7792 		return rc;
7793 
7794 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
7795 
7796 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
7797 
7798 	return hwrm_req_send(bp, req_rx);
7799 }
7800 
7801 static int
7802 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
7803 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7804 {
7805 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
7806 
7807 	req->ring_id = cpu_to_le16(ring_id);
7808 	return hwrm_req_send(bp, req);
7809 }
7810 
7811 static int
7812 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
7813 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7814 {
7815 	struct bnxt_tx_ring_info *txr;
7816 	int i, rc;
7817 
7818 	bnxt_for_each_napi_tx(i, bnapi, txr) {
7819 		u16 ring_id;
7820 
7821 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
7822 		req->ring_id = cpu_to_le16(ring_id);
7823 		rc = hwrm_req_send(bp, req);
7824 		if (rc)
7825 			return rc;
7826 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7827 			return 0;
7828 	}
7829 	return 0;
7830 }
7831 
7832 int bnxt_hwrm_set_coal(struct bnxt *bp)
7833 {
7834 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
7835 	int i, rc;
7836 
7837 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7838 	if (rc)
7839 		return rc;
7840 
7841 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7842 	if (rc) {
7843 		hwrm_req_drop(bp, req_rx);
7844 		return rc;
7845 	}
7846 
7847 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
7848 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
7849 
7850 	hwrm_req_hold(bp, req_rx);
7851 	hwrm_req_hold(bp, req_tx);
7852 	for (i = 0; i < bp->cp_nr_rings; i++) {
7853 		struct bnxt_napi *bnapi = bp->bnapi[i];
7854 		struct bnxt_coal *hw_coal;
7855 
7856 		if (!bnapi->rx_ring)
7857 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
7858 		else
7859 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
7860 		if (rc)
7861 			break;
7862 
7863 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7864 			continue;
7865 
7866 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
7867 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
7868 			if (rc)
7869 				break;
7870 		}
7871 		if (bnapi->rx_ring)
7872 			hw_coal = &bp->rx_coal;
7873 		else
7874 			hw_coal = &bp->tx_coal;
7875 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
7876 	}
7877 	hwrm_req_drop(bp, req_rx);
7878 	hwrm_req_drop(bp, req_tx);
7879 	return rc;
7880 }
7881 
7882 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
7883 {
7884 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
7885 	struct hwrm_stat_ctx_free_input *req;
7886 	int i;
7887 
7888 	if (!bp->bnapi)
7889 		return;
7890 
7891 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
7892 		return;
7893 
7894 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
7895 		return;
7896 	if (BNXT_FW_MAJ(bp) <= 20) {
7897 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
7898 			hwrm_req_drop(bp, req);
7899 			return;
7900 		}
7901 		hwrm_req_hold(bp, req0);
7902 	}
7903 	hwrm_req_hold(bp, req);
7904 	for (i = 0; i < bp->cp_nr_rings; i++) {
7905 		struct bnxt_napi *bnapi = bp->bnapi[i];
7906 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7907 
7908 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
7909 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
7910 			if (req0) {
7911 				req0->stat_ctx_id = req->stat_ctx_id;
7912 				hwrm_req_send(bp, req0);
7913 			}
7914 			hwrm_req_send(bp, req);
7915 
7916 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
7917 		}
7918 	}
7919 	hwrm_req_drop(bp, req);
7920 	if (req0)
7921 		hwrm_req_drop(bp, req0);
7922 }
7923 
7924 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
7925 {
7926 	struct hwrm_stat_ctx_alloc_output *resp;
7927 	struct hwrm_stat_ctx_alloc_input *req;
7928 	int rc, i;
7929 
7930 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
7931 		return 0;
7932 
7933 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
7934 	if (rc)
7935 		return rc;
7936 
7937 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
7938 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
7939 
7940 	resp = hwrm_req_hold(bp, req);
7941 	for (i = 0; i < bp->cp_nr_rings; i++) {
7942 		struct bnxt_napi *bnapi = bp->bnapi[i];
7943 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7944 
7945 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
7946 
7947 		rc = hwrm_req_send(bp, req);
7948 		if (rc)
7949 			break;
7950 
7951 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
7952 
7953 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
7954 	}
7955 	hwrm_req_drop(bp, req);
7956 	return rc;
7957 }
7958 
7959 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
7960 {
7961 	struct hwrm_func_qcfg_output *resp;
7962 	struct hwrm_func_qcfg_input *req;
7963 	u16 flags;
7964 	int rc;
7965 
7966 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7967 	if (rc)
7968 		return rc;
7969 
7970 	req->fid = cpu_to_le16(0xffff);
7971 	resp = hwrm_req_hold(bp, req);
7972 	rc = hwrm_req_send(bp, req);
7973 	if (rc)
7974 		goto func_qcfg_exit;
7975 
7976 #ifdef CONFIG_BNXT_SRIOV
7977 	if (BNXT_VF(bp)) {
7978 		struct bnxt_vf_info *vf = &bp->vf;
7979 
7980 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
7981 	} else {
7982 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
7983 	}
7984 #endif
7985 	flags = le16_to_cpu(resp->flags);
7986 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
7987 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
7988 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
7989 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
7990 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
7991 	}
7992 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
7993 		bp->flags |= BNXT_FLAG_MULTI_HOST;
7994 
7995 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
7996 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
7997 
7998 	switch (resp->port_partition_type) {
7999 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8000 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8001 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8002 		bp->port_partition_type = resp->port_partition_type;
8003 		break;
8004 	}
8005 	if (bp->hwrm_spec_code < 0x10707 ||
8006 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8007 		bp->br_mode = BRIDGE_MODE_VEB;
8008 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8009 		bp->br_mode = BRIDGE_MODE_VEPA;
8010 	else
8011 		bp->br_mode = BRIDGE_MODE_UNDEF;
8012 
8013 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8014 	if (!bp->max_mtu)
8015 		bp->max_mtu = BNXT_MAX_MTU;
8016 
8017 	if (bp->db_size)
8018 		goto func_qcfg_exit;
8019 
8020 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8021 	if (BNXT_CHIP_P5(bp)) {
8022 		if (BNXT_PF(bp))
8023 			bp->db_offset = DB_PF_OFFSET_P5;
8024 		else
8025 			bp->db_offset = DB_VF_OFFSET_P5;
8026 	}
8027 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8028 				 1024);
8029 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8030 	    bp->db_size <= bp->db_offset)
8031 		bp->db_size = pci_resource_len(bp->pdev, 2);
8032 
8033 func_qcfg_exit:
8034 	hwrm_req_drop(bp, req);
8035 	return rc;
8036 }
8037 
8038 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8039 				      u8 init_val, u8 init_offset,
8040 				      bool init_mask_set)
8041 {
8042 	ctxm->init_value = init_val;
8043 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8044 	if (init_mask_set)
8045 		ctxm->init_offset = init_offset * 4;
8046 	else
8047 		ctxm->init_value = 0;
8048 }
8049 
8050 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8051 {
8052 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8053 	u16 type;
8054 
8055 	for (type = 0; type < ctx_max; type++) {
8056 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8057 		int n = 1;
8058 
8059 		if (!ctxm->max_entries)
8060 			continue;
8061 
8062 		if (ctxm->instance_bmap)
8063 			n = hweight32(ctxm->instance_bmap);
8064 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8065 		if (!ctxm->pg_info)
8066 			return -ENOMEM;
8067 	}
8068 	return 0;
8069 }
8070 
8071 #define BNXT_CTX_INIT_VALID(flags)	\
8072 	(!!((flags) &			\
8073 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8074 
8075 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8076 {
8077 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8078 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8079 	struct bnxt_ctx_mem_info *ctx;
8080 	u16 type;
8081 	int rc;
8082 
8083 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8084 	if (rc)
8085 		return rc;
8086 
8087 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8088 	if (!ctx)
8089 		return -ENOMEM;
8090 	bp->ctx = ctx;
8091 
8092 	resp = hwrm_req_hold(bp, req);
8093 
8094 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8095 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8096 		u8 init_val, init_off, i;
8097 		__le32 *p;
8098 		u32 flags;
8099 
8100 		req->type = cpu_to_le16(type);
8101 		rc = hwrm_req_send(bp, req);
8102 		if (rc)
8103 			goto ctx_done;
8104 		flags = le32_to_cpu(resp->flags);
8105 		type = le16_to_cpu(resp->next_valid_type);
8106 		if (!(flags & FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_TYPE_VALID))
8107 			continue;
8108 
8109 		ctxm->type = le16_to_cpu(resp->type);
8110 		ctxm->entry_size = le16_to_cpu(resp->entry_size);
8111 		ctxm->flags = flags;
8112 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8113 		ctxm->entry_multiple = resp->entry_multiple;
8114 		ctxm->max_entries = le32_to_cpu(resp->max_num_entries);
8115 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8116 		init_val = resp->ctx_init_value;
8117 		init_off = resp->ctx_init_offset;
8118 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8119 					  BNXT_CTX_INIT_VALID(flags));
8120 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8121 					      BNXT_MAX_SPLIT_ENTRY);
8122 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8123 		     i++, p++)
8124 			ctxm->split[i] = le32_to_cpu(*p);
8125 	}
8126 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8127 
8128 ctx_done:
8129 	hwrm_req_drop(bp, req);
8130 	return rc;
8131 }
8132 
8133 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8134 {
8135 	struct hwrm_func_backing_store_qcaps_output *resp;
8136 	struct hwrm_func_backing_store_qcaps_input *req;
8137 	int rc;
8138 
8139 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx)
8140 		return 0;
8141 
8142 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8143 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8144 
8145 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8146 	if (rc)
8147 		return rc;
8148 
8149 	resp = hwrm_req_hold(bp, req);
8150 	rc = hwrm_req_send_silent(bp, req);
8151 	if (!rc) {
8152 		struct bnxt_ctx_mem_type *ctxm;
8153 		struct bnxt_ctx_mem_info *ctx;
8154 		u8 init_val, init_idx = 0;
8155 		u16 init_mask;
8156 
8157 		ctx = bp->ctx;
8158 		if (!ctx) {
8159 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8160 			if (!ctx) {
8161 				rc = -ENOMEM;
8162 				goto ctx_err;
8163 			}
8164 			bp->ctx = ctx;
8165 		}
8166 		init_val = resp->ctx_kind_initializer;
8167 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8168 
8169 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8170 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8171 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8172 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8173 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8174 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8175 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8176 					  (init_mask & (1 << init_idx++)) != 0);
8177 
8178 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8179 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8180 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8181 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8182 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8183 					  (init_mask & (1 << init_idx++)) != 0);
8184 
8185 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8186 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8187 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8188 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8189 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8190 					  (init_mask & (1 << init_idx++)) != 0);
8191 
8192 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8193 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8194 		ctxm->max_entries = ctxm->vnic_entries +
8195 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8196 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8197 		bnxt_init_ctx_initializer(ctxm, init_val,
8198 					  resp->vnic_init_offset,
8199 					  (init_mask & (1 << init_idx++)) != 0);
8200 
8201 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8202 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8203 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8204 		bnxt_init_ctx_initializer(ctxm, init_val,
8205 					  resp->stat_init_offset,
8206 					  (init_mask & (1 << init_idx++)) != 0);
8207 
8208 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8209 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8210 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8211 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8212 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8213 		if (!ctxm->entry_multiple)
8214 			ctxm->entry_multiple = 1;
8215 
8216 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8217 
8218 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8219 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8220 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8221 		ctxm->mrav_num_entries_units =
8222 			le16_to_cpu(resp->mrav_num_entries_units);
8223 		bnxt_init_ctx_initializer(ctxm, init_val,
8224 					  resp->mrav_init_offset,
8225 					  (init_mask & (1 << init_idx++)) != 0);
8226 
8227 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8228 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8229 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8230 
8231 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8232 		if (!ctx->tqm_fp_rings_count)
8233 			ctx->tqm_fp_rings_count = bp->max_q;
8234 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8235 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8236 
8237 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8238 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8239 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8240 
8241 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8242 	} else {
8243 		rc = 0;
8244 	}
8245 ctx_err:
8246 	hwrm_req_drop(bp, req);
8247 	return rc;
8248 }
8249 
8250 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8251 				  __le64 *pg_dir)
8252 {
8253 	if (!rmem->nr_pages)
8254 		return;
8255 
8256 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8257 	if (rmem->depth >= 1) {
8258 		if (rmem->depth == 2)
8259 			*pg_attr |= 2;
8260 		else
8261 			*pg_attr |= 1;
8262 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8263 	} else {
8264 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8265 	}
8266 }
8267 
8268 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8269 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8270 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8271 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8272 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8273 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8274 
8275 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8276 {
8277 	struct hwrm_func_backing_store_cfg_input *req;
8278 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8279 	struct bnxt_ctx_pg_info *ctx_pg;
8280 	struct bnxt_ctx_mem_type *ctxm;
8281 	void **__req = (void **)&req;
8282 	u32 req_len = sizeof(*req);
8283 	__le32 *num_entries;
8284 	__le64 *pg_dir;
8285 	u32 flags = 0;
8286 	u8 *pg_attr;
8287 	u32 ena;
8288 	int rc;
8289 	int i;
8290 
8291 	if (!ctx)
8292 		return 0;
8293 
8294 	if (req_len > bp->hwrm_max_ext_req_len)
8295 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8296 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8297 	if (rc)
8298 		return rc;
8299 
8300 	req->enables = cpu_to_le32(enables);
8301 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8302 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8303 		ctx_pg = ctxm->pg_info;
8304 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8305 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8306 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8307 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8308 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8309 				      &req->qpc_pg_size_qpc_lvl,
8310 				      &req->qpc_page_dir);
8311 
8312 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8313 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8314 	}
8315 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8316 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8317 		ctx_pg = ctxm->pg_info;
8318 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8319 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8320 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8321 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8322 				      &req->srq_pg_size_srq_lvl,
8323 				      &req->srq_page_dir);
8324 	}
8325 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8326 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8327 		ctx_pg = ctxm->pg_info;
8328 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8329 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8330 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8331 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8332 				      &req->cq_pg_size_cq_lvl,
8333 				      &req->cq_page_dir);
8334 	}
8335 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8336 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8337 		ctx_pg = ctxm->pg_info;
8338 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8339 		req->vnic_num_ring_table_entries =
8340 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8341 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8342 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8343 				      &req->vnic_pg_size_vnic_lvl,
8344 				      &req->vnic_page_dir);
8345 	}
8346 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8347 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8348 		ctx_pg = ctxm->pg_info;
8349 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8350 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8351 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8352 				      &req->stat_pg_size_stat_lvl,
8353 				      &req->stat_page_dir);
8354 	}
8355 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8356 		u32 units;
8357 
8358 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8359 		ctx_pg = ctxm->pg_info;
8360 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8361 		units = ctxm->mrav_num_entries_units;
8362 		if (units) {
8363 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8364 			u32 entries;
8365 
8366 			num_mr = ctx_pg->entries - num_ah;
8367 			entries = ((num_mr / units) << 16) | (num_ah / units);
8368 			req->mrav_num_entries = cpu_to_le32(entries);
8369 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8370 		}
8371 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8372 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8373 				      &req->mrav_pg_size_mrav_lvl,
8374 				      &req->mrav_page_dir);
8375 	}
8376 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8377 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8378 		ctx_pg = ctxm->pg_info;
8379 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8380 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8381 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8382 				      &req->tim_pg_size_tim_lvl,
8383 				      &req->tim_page_dir);
8384 	}
8385 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8386 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8387 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8388 	     pg_dir = &req->tqm_sp_page_dir,
8389 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8390 	     ctx_pg = ctxm->pg_info;
8391 	     i < BNXT_MAX_TQM_RINGS;
8392 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8393 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8394 		if (!(enables & ena))
8395 			continue;
8396 
8397 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8398 		*num_entries = cpu_to_le32(ctx_pg->entries);
8399 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8400 	}
8401 	req->flags = cpu_to_le32(flags);
8402 	return hwrm_req_send(bp, req);
8403 }
8404 
8405 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8406 				  struct bnxt_ctx_pg_info *ctx_pg)
8407 {
8408 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8409 
8410 	rmem->page_size = BNXT_PAGE_SIZE;
8411 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8412 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8413 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8414 	if (rmem->depth >= 1)
8415 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8416 	return bnxt_alloc_ring(bp, rmem);
8417 }
8418 
8419 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8420 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8421 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8422 {
8423 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8424 	int rc;
8425 
8426 	if (!mem_size)
8427 		return -EINVAL;
8428 
8429 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8430 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8431 		ctx_pg->nr_pages = 0;
8432 		return -EINVAL;
8433 	}
8434 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8435 		int nr_tbls, i;
8436 
8437 		rmem->depth = 2;
8438 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8439 					     GFP_KERNEL);
8440 		if (!ctx_pg->ctx_pg_tbl)
8441 			return -ENOMEM;
8442 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8443 		rmem->nr_pages = nr_tbls;
8444 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8445 		if (rc)
8446 			return rc;
8447 		for (i = 0; i < nr_tbls; i++) {
8448 			struct bnxt_ctx_pg_info *pg_tbl;
8449 
8450 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8451 			if (!pg_tbl)
8452 				return -ENOMEM;
8453 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8454 			rmem = &pg_tbl->ring_mem;
8455 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8456 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8457 			rmem->depth = 1;
8458 			rmem->nr_pages = MAX_CTX_PAGES;
8459 			rmem->ctx_mem = ctxm;
8460 			if (i == (nr_tbls - 1)) {
8461 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8462 
8463 				if (rem)
8464 					rmem->nr_pages = rem;
8465 			}
8466 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8467 			if (rc)
8468 				break;
8469 		}
8470 	} else {
8471 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8472 		if (rmem->nr_pages > 1 || depth)
8473 			rmem->depth = 1;
8474 		rmem->ctx_mem = ctxm;
8475 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8476 	}
8477 	return rc;
8478 }
8479 
8480 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
8481 				  struct bnxt_ctx_pg_info *ctx_pg)
8482 {
8483 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8484 
8485 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
8486 	    ctx_pg->ctx_pg_tbl) {
8487 		int i, nr_tbls = rmem->nr_pages;
8488 
8489 		for (i = 0; i < nr_tbls; i++) {
8490 			struct bnxt_ctx_pg_info *pg_tbl;
8491 			struct bnxt_ring_mem_info *rmem2;
8492 
8493 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8494 			if (!pg_tbl)
8495 				continue;
8496 			rmem2 = &pg_tbl->ring_mem;
8497 			bnxt_free_ring(bp, rmem2);
8498 			ctx_pg->ctx_pg_arr[i] = NULL;
8499 			kfree(pg_tbl);
8500 			ctx_pg->ctx_pg_tbl[i] = NULL;
8501 		}
8502 		kfree(ctx_pg->ctx_pg_tbl);
8503 		ctx_pg->ctx_pg_tbl = NULL;
8504 	}
8505 	bnxt_free_ring(bp, rmem);
8506 	ctx_pg->nr_pages = 0;
8507 }
8508 
8509 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
8510 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
8511 				   u8 pg_lvl)
8512 {
8513 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8514 	int i, rc = 0, n = 1;
8515 	u32 mem_size;
8516 
8517 	if (!ctxm->entry_size || !ctx_pg)
8518 		return -EINVAL;
8519 	if (ctxm->instance_bmap)
8520 		n = hweight32(ctxm->instance_bmap);
8521 	if (ctxm->entry_multiple)
8522 		entries = roundup(entries, ctxm->entry_multiple);
8523 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
8524 	mem_size = entries * ctxm->entry_size;
8525 	for (i = 0; i < n && !rc; i++) {
8526 		ctx_pg[i].entries = entries;
8527 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
8528 					    ctxm->init_value ? ctxm : NULL);
8529 	}
8530 	return rc;
8531 }
8532 
8533 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
8534 					       struct bnxt_ctx_mem_type *ctxm,
8535 					       bool last)
8536 {
8537 	struct hwrm_func_backing_store_cfg_v2_input *req;
8538 	u32 instance_bmap = ctxm->instance_bmap;
8539 	int i, j, rc = 0, n = 1;
8540 	__le32 *p;
8541 
8542 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
8543 		return 0;
8544 
8545 	if (instance_bmap)
8546 		n = hweight32(ctxm->instance_bmap);
8547 	else
8548 		instance_bmap = 1;
8549 
8550 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
8551 	if (rc)
8552 		return rc;
8553 	hwrm_req_hold(bp, req);
8554 	req->type = cpu_to_le16(ctxm->type);
8555 	req->entry_size = cpu_to_le16(ctxm->entry_size);
8556 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
8557 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
8558 		p[i] = cpu_to_le32(ctxm->split[i]);
8559 	for (i = 0, j = 0; j < n && !rc; i++) {
8560 		struct bnxt_ctx_pg_info *ctx_pg;
8561 
8562 		if (!(instance_bmap & (1 << i)))
8563 			continue;
8564 		req->instance = cpu_to_le16(i);
8565 		ctx_pg = &ctxm->pg_info[j++];
8566 		if (!ctx_pg->entries)
8567 			continue;
8568 		req->num_entries = cpu_to_le32(ctx_pg->entries);
8569 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8570 				      &req->page_size_pbl_level,
8571 				      &req->page_dir);
8572 		if (last && j == n)
8573 			req->flags =
8574 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
8575 		rc = hwrm_req_send(bp, req);
8576 	}
8577 	hwrm_req_drop(bp, req);
8578 	return rc;
8579 }
8580 
8581 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
8582 {
8583 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8584 	struct bnxt_ctx_mem_type *ctxm;
8585 	u16 last_type;
8586 	int rc = 0;
8587 	u16 type;
8588 
8589 	if (!ena)
8590 		return 0;
8591 	else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
8592 		last_type = BNXT_CTX_MAX - 1;
8593 	else
8594 		last_type = BNXT_CTX_L2_MAX - 1;
8595 	ctx->ctx_arr[last_type].last = 1;
8596 
8597 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
8598 		ctxm = &ctx->ctx_arr[type];
8599 
8600 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
8601 		if (rc)
8602 			return rc;
8603 	}
8604 	return 0;
8605 }
8606 
8607 void bnxt_free_ctx_mem(struct bnxt *bp)
8608 {
8609 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8610 	u16 type;
8611 
8612 	if (!ctx)
8613 		return;
8614 
8615 	for (type = 0; type < BNXT_CTX_V2_MAX; type++) {
8616 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8617 		struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8618 		int i, n = 1;
8619 
8620 		if (!ctx_pg)
8621 			continue;
8622 		if (ctxm->instance_bmap)
8623 			n = hweight32(ctxm->instance_bmap);
8624 		for (i = 0; i < n; i++)
8625 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
8626 
8627 		kfree(ctx_pg);
8628 		ctxm->pg_info = NULL;
8629 	}
8630 
8631 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
8632 	kfree(ctx);
8633 	bp->ctx = NULL;
8634 }
8635 
8636 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
8637 {
8638 	struct bnxt_ctx_mem_type *ctxm;
8639 	struct bnxt_ctx_mem_info *ctx;
8640 	u32 l2_qps, qp1_qps, max_qps;
8641 	u32 ena, entries_sp, entries;
8642 	u32 srqs, max_srqs, min;
8643 	u32 num_mr, num_ah;
8644 	u32 extra_srqs = 0;
8645 	u32 extra_qps = 0;
8646 	u32 fast_qpmd_qps;
8647 	u8 pg_lvl = 1;
8648 	int i, rc;
8649 
8650 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
8651 	if (rc) {
8652 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
8653 			   rc);
8654 		return rc;
8655 	}
8656 	ctx = bp->ctx;
8657 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
8658 		return 0;
8659 
8660 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8661 	l2_qps = ctxm->qp_l2_entries;
8662 	qp1_qps = ctxm->qp_qp1_entries;
8663 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
8664 	max_qps = ctxm->max_entries;
8665 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8666 	srqs = ctxm->srq_l2_entries;
8667 	max_srqs = ctxm->max_entries;
8668 	ena = 0;
8669 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
8670 		pg_lvl = 2;
8671 		extra_qps = min_t(u32, 65536, max_qps - l2_qps - qp1_qps);
8672 		/* allocate extra qps if fw supports RoCE fast qp destroy feature */
8673 		extra_qps += fast_qpmd_qps;
8674 		extra_srqs = min_t(u32, 8192, max_srqs - srqs);
8675 		if (fast_qpmd_qps)
8676 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
8677 	}
8678 
8679 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8680 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
8681 				     pg_lvl);
8682 	if (rc)
8683 		return rc;
8684 
8685 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8686 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
8687 	if (rc)
8688 		return rc;
8689 
8690 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8691 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
8692 				     extra_qps * 2, pg_lvl);
8693 	if (rc)
8694 		return rc;
8695 
8696 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8697 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
8698 	if (rc)
8699 		return rc;
8700 
8701 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8702 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
8703 	if (rc)
8704 		return rc;
8705 
8706 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
8707 		goto skip_rdma;
8708 
8709 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8710 	/* 128K extra is needed to accommodate static AH context
8711 	 * allocation by f/w.
8712 	 */
8713 	num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
8714 	num_ah = min_t(u32, num_mr, 1024 * 128);
8715 	ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
8716 	if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
8717 		ctxm->mrav_av_entries = num_ah;
8718 
8719 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
8720 	if (rc)
8721 		return rc;
8722 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
8723 
8724 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8725 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
8726 	if (rc)
8727 		return rc;
8728 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
8729 
8730 skip_rdma:
8731 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8732 	min = ctxm->min_entries;
8733 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
8734 		     2 * (extra_qps + qp1_qps) + min;
8735 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
8736 	if (rc)
8737 		return rc;
8738 
8739 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8740 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
8741 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
8742 	if (rc)
8743 		return rc;
8744 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
8745 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
8746 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
8747 
8748 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8749 		rc = bnxt_backing_store_cfg_v2(bp, ena);
8750 	else
8751 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
8752 	if (rc) {
8753 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
8754 			   rc);
8755 		return rc;
8756 	}
8757 	ctx->flags |= BNXT_CTX_FLAG_INITED;
8758 	return 0;
8759 }
8760 
8761 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
8762 {
8763 	struct hwrm_func_resource_qcaps_output *resp;
8764 	struct hwrm_func_resource_qcaps_input *req;
8765 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8766 	int rc;
8767 
8768 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
8769 	if (rc)
8770 		return rc;
8771 
8772 	req->fid = cpu_to_le16(0xffff);
8773 	resp = hwrm_req_hold(bp, req);
8774 	rc = hwrm_req_send_silent(bp, req);
8775 	if (rc)
8776 		goto hwrm_func_resc_qcaps_exit;
8777 
8778 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
8779 	if (!all)
8780 		goto hwrm_func_resc_qcaps_exit;
8781 
8782 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
8783 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
8784 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
8785 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
8786 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
8787 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
8788 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
8789 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
8790 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
8791 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
8792 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
8793 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
8794 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
8795 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
8796 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
8797 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
8798 
8799 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8800 		u16 max_msix = le16_to_cpu(resp->max_msix);
8801 
8802 		hw_resc->max_nqs = max_msix;
8803 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
8804 	}
8805 
8806 	if (BNXT_PF(bp)) {
8807 		struct bnxt_pf_info *pf = &bp->pf;
8808 
8809 		pf->vf_resv_strategy =
8810 			le16_to_cpu(resp->vf_reservation_strategy);
8811 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
8812 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
8813 	}
8814 hwrm_func_resc_qcaps_exit:
8815 	hwrm_req_drop(bp, req);
8816 	return rc;
8817 }
8818 
8819 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
8820 {
8821 	struct hwrm_port_mac_ptp_qcfg_output *resp;
8822 	struct hwrm_port_mac_ptp_qcfg_input *req;
8823 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
8824 	bool phc_cfg;
8825 	u8 flags;
8826 	int rc;
8827 
8828 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5(bp)) {
8829 		rc = -ENODEV;
8830 		goto no_ptp;
8831 	}
8832 
8833 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
8834 	if (rc)
8835 		goto no_ptp;
8836 
8837 	req->port_id = cpu_to_le16(bp->pf.port_id);
8838 	resp = hwrm_req_hold(bp, req);
8839 	rc = hwrm_req_send(bp, req);
8840 	if (rc)
8841 		goto exit;
8842 
8843 	flags = resp->flags;
8844 	if (!(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
8845 		rc = -ENODEV;
8846 		goto exit;
8847 	}
8848 	if (!ptp) {
8849 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
8850 		if (!ptp) {
8851 			rc = -ENOMEM;
8852 			goto exit;
8853 		}
8854 		ptp->bp = bp;
8855 		bp->ptp_cfg = ptp;
8856 	}
8857 	if (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK) {
8858 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
8859 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
8860 	} else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8861 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
8862 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
8863 	} else {
8864 		rc = -ENODEV;
8865 		goto exit;
8866 	}
8867 	phc_cfg = (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
8868 	rc = bnxt_ptp_init(bp, phc_cfg);
8869 	if (rc)
8870 		netdev_warn(bp->dev, "PTP initialization failed.\n");
8871 exit:
8872 	hwrm_req_drop(bp, req);
8873 	if (!rc)
8874 		return 0;
8875 
8876 no_ptp:
8877 	bnxt_ptp_clear(bp);
8878 	kfree(ptp);
8879 	bp->ptp_cfg = NULL;
8880 	return rc;
8881 }
8882 
8883 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
8884 {
8885 	struct hwrm_func_qcaps_output *resp;
8886 	struct hwrm_func_qcaps_input *req;
8887 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8888 	u32 flags, flags_ext, flags_ext2;
8889 	int rc;
8890 
8891 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
8892 	if (rc)
8893 		return rc;
8894 
8895 	req->fid = cpu_to_le16(0xffff);
8896 	resp = hwrm_req_hold(bp, req);
8897 	rc = hwrm_req_send(bp, req);
8898 	if (rc)
8899 		goto hwrm_func_qcaps_exit;
8900 
8901 	flags = le32_to_cpu(resp->flags);
8902 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
8903 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
8904 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
8905 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
8906 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
8907 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
8908 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
8909 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
8910 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
8911 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
8912 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
8913 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
8914 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
8915 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
8916 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
8917 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
8918 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
8919 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
8920 
8921 	flags_ext = le32_to_cpu(resp->flags_ext);
8922 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
8923 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
8924 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
8925 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
8926 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
8927 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
8928 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
8929 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
8930 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
8931 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
8932 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
8933 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
8934 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
8935 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
8936 
8937 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
8938 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
8939 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
8940 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
8941 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
8942 
8943 	bp->tx_push_thresh = 0;
8944 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
8945 	    BNXT_FW_MAJ(bp) > 217)
8946 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
8947 
8948 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
8949 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
8950 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
8951 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
8952 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
8953 	if (!hw_resc->max_hw_ring_grps)
8954 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
8955 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
8956 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
8957 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
8958 
8959 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
8960 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
8961 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
8962 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
8963 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
8964 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
8965 
8966 	if (BNXT_PF(bp)) {
8967 		struct bnxt_pf_info *pf = &bp->pf;
8968 
8969 		pf->fw_fid = le16_to_cpu(resp->fid);
8970 		pf->port_id = le16_to_cpu(resp->port_id);
8971 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
8972 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
8973 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
8974 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
8975 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
8976 			bp->flags |= BNXT_FLAG_WOL_CAP;
8977 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
8978 			bp->fw_cap |= BNXT_FW_CAP_PTP;
8979 		} else {
8980 			bnxt_ptp_clear(bp);
8981 			kfree(bp->ptp_cfg);
8982 			bp->ptp_cfg = NULL;
8983 		}
8984 	} else {
8985 #ifdef CONFIG_BNXT_SRIOV
8986 		struct bnxt_vf_info *vf = &bp->vf;
8987 
8988 		vf->fw_fid = le16_to_cpu(resp->fid);
8989 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
8990 #endif
8991 	}
8992 
8993 hwrm_func_qcaps_exit:
8994 	hwrm_req_drop(bp, req);
8995 	return rc;
8996 }
8997 
8998 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
8999 {
9000 	struct hwrm_dbg_qcaps_output *resp;
9001 	struct hwrm_dbg_qcaps_input *req;
9002 	int rc;
9003 
9004 	bp->fw_dbg_cap = 0;
9005 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9006 		return;
9007 
9008 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9009 	if (rc)
9010 		return;
9011 
9012 	req->fid = cpu_to_le16(0xffff);
9013 	resp = hwrm_req_hold(bp, req);
9014 	rc = hwrm_req_send(bp, req);
9015 	if (rc)
9016 		goto hwrm_dbg_qcaps_exit;
9017 
9018 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9019 
9020 hwrm_dbg_qcaps_exit:
9021 	hwrm_req_drop(bp, req);
9022 }
9023 
9024 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9025 
9026 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9027 {
9028 	int rc;
9029 
9030 	rc = __bnxt_hwrm_func_qcaps(bp);
9031 	if (rc)
9032 		return rc;
9033 
9034 	bnxt_hwrm_dbg_qcaps(bp);
9035 
9036 	rc = bnxt_hwrm_queue_qportcfg(bp);
9037 	if (rc) {
9038 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9039 		return rc;
9040 	}
9041 	if (bp->hwrm_spec_code >= 0x10803) {
9042 		rc = bnxt_alloc_ctx_mem(bp);
9043 		if (rc)
9044 			return rc;
9045 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9046 		if (!rc)
9047 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9048 	}
9049 	return 0;
9050 }
9051 
9052 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9053 {
9054 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9055 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9056 	u32 flags;
9057 	int rc;
9058 
9059 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9060 		return 0;
9061 
9062 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9063 	if (rc)
9064 		return rc;
9065 
9066 	resp = hwrm_req_hold(bp, req);
9067 	rc = hwrm_req_send(bp, req);
9068 	if (rc)
9069 		goto hwrm_cfa_adv_qcaps_exit;
9070 
9071 	flags = le32_to_cpu(resp->flags);
9072 	if (flags &
9073 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9074 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9075 
9076 	if (flags &
9077 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9078 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9079 
9080 	if (flags &
9081 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9082 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9083 
9084 hwrm_cfa_adv_qcaps_exit:
9085 	hwrm_req_drop(bp, req);
9086 	return rc;
9087 }
9088 
9089 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9090 {
9091 	if (bp->fw_health)
9092 		return 0;
9093 
9094 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9095 	if (!bp->fw_health)
9096 		return -ENOMEM;
9097 
9098 	mutex_init(&bp->fw_health->lock);
9099 	return 0;
9100 }
9101 
9102 static int bnxt_alloc_fw_health(struct bnxt *bp)
9103 {
9104 	int rc;
9105 
9106 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9107 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9108 		return 0;
9109 
9110 	rc = __bnxt_alloc_fw_health(bp);
9111 	if (rc) {
9112 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9113 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9114 		return rc;
9115 	}
9116 
9117 	return 0;
9118 }
9119 
9120 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9121 {
9122 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9123 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9124 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9125 }
9126 
9127 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9128 {
9129 	struct bnxt_fw_health *fw_health = bp->fw_health;
9130 	u32 reg_type;
9131 
9132 	if (!fw_health)
9133 		return;
9134 
9135 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9136 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9137 		fw_health->status_reliable = false;
9138 
9139 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9140 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9141 		fw_health->resets_reliable = false;
9142 }
9143 
9144 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9145 {
9146 	void __iomem *hs;
9147 	u32 status_loc;
9148 	u32 reg_type;
9149 	u32 sig;
9150 
9151 	if (bp->fw_health)
9152 		bp->fw_health->status_reliable = false;
9153 
9154 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9155 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9156 
9157 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9158 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9159 		if (!bp->chip_num) {
9160 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9161 			bp->chip_num = readl(bp->bar0 +
9162 					     BNXT_FW_HEALTH_WIN_BASE +
9163 					     BNXT_GRC_REG_CHIP_NUM);
9164 		}
9165 		if (!BNXT_CHIP_P5(bp))
9166 			return;
9167 
9168 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9169 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9170 	} else {
9171 		status_loc = readl(hs + offsetof(struct hcomm_status,
9172 						 fw_status_loc));
9173 	}
9174 
9175 	if (__bnxt_alloc_fw_health(bp)) {
9176 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9177 		return;
9178 	}
9179 
9180 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9181 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9182 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9183 		__bnxt_map_fw_health_reg(bp, status_loc);
9184 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9185 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9186 	}
9187 
9188 	bp->fw_health->status_reliable = true;
9189 }
9190 
9191 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9192 {
9193 	struct bnxt_fw_health *fw_health = bp->fw_health;
9194 	u32 reg_base = 0xffffffff;
9195 	int i;
9196 
9197 	bp->fw_health->status_reliable = false;
9198 	bp->fw_health->resets_reliable = false;
9199 	/* Only pre-map the monitoring GRC registers using window 3 */
9200 	for (i = 0; i < 4; i++) {
9201 		u32 reg = fw_health->regs[i];
9202 
9203 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9204 			continue;
9205 		if (reg_base == 0xffffffff)
9206 			reg_base = reg & BNXT_GRC_BASE_MASK;
9207 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9208 			return -ERANGE;
9209 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9210 	}
9211 	bp->fw_health->status_reliable = true;
9212 	bp->fw_health->resets_reliable = true;
9213 	if (reg_base == 0xffffffff)
9214 		return 0;
9215 
9216 	__bnxt_map_fw_health_reg(bp, reg_base);
9217 	return 0;
9218 }
9219 
9220 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9221 {
9222 	if (!bp->fw_health)
9223 		return;
9224 
9225 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9226 		bp->fw_health->status_reliable = true;
9227 		bp->fw_health->resets_reliable = true;
9228 	} else {
9229 		bnxt_try_map_fw_health_reg(bp);
9230 	}
9231 }
9232 
9233 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9234 {
9235 	struct bnxt_fw_health *fw_health = bp->fw_health;
9236 	struct hwrm_error_recovery_qcfg_output *resp;
9237 	struct hwrm_error_recovery_qcfg_input *req;
9238 	int rc, i;
9239 
9240 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9241 		return 0;
9242 
9243 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9244 	if (rc)
9245 		return rc;
9246 
9247 	resp = hwrm_req_hold(bp, req);
9248 	rc = hwrm_req_send(bp, req);
9249 	if (rc)
9250 		goto err_recovery_out;
9251 	fw_health->flags = le32_to_cpu(resp->flags);
9252 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
9253 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
9254 		rc = -EINVAL;
9255 		goto err_recovery_out;
9256 	}
9257 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
9258 	fw_health->master_func_wait_dsecs =
9259 		le32_to_cpu(resp->master_func_wait_period);
9260 	fw_health->normal_func_wait_dsecs =
9261 		le32_to_cpu(resp->normal_func_wait_period);
9262 	fw_health->post_reset_wait_dsecs =
9263 		le32_to_cpu(resp->master_func_wait_period_after_reset);
9264 	fw_health->post_reset_max_wait_dsecs =
9265 		le32_to_cpu(resp->max_bailout_time_after_reset);
9266 	fw_health->regs[BNXT_FW_HEALTH_REG] =
9267 		le32_to_cpu(resp->fw_health_status_reg);
9268 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
9269 		le32_to_cpu(resp->fw_heartbeat_reg);
9270 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
9271 		le32_to_cpu(resp->fw_reset_cnt_reg);
9272 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
9273 		le32_to_cpu(resp->reset_inprogress_reg);
9274 	fw_health->fw_reset_inprog_reg_mask =
9275 		le32_to_cpu(resp->reset_inprogress_reg_mask);
9276 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
9277 	if (fw_health->fw_reset_seq_cnt >= 16) {
9278 		rc = -EINVAL;
9279 		goto err_recovery_out;
9280 	}
9281 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
9282 		fw_health->fw_reset_seq_regs[i] =
9283 			le32_to_cpu(resp->reset_reg[i]);
9284 		fw_health->fw_reset_seq_vals[i] =
9285 			le32_to_cpu(resp->reset_reg_val[i]);
9286 		fw_health->fw_reset_seq_delay_msec[i] =
9287 			resp->delay_after_reset[i];
9288 	}
9289 err_recovery_out:
9290 	hwrm_req_drop(bp, req);
9291 	if (!rc)
9292 		rc = bnxt_map_fw_health_regs(bp);
9293 	if (rc)
9294 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9295 	return rc;
9296 }
9297 
9298 static int bnxt_hwrm_func_reset(struct bnxt *bp)
9299 {
9300 	struct hwrm_func_reset_input *req;
9301 	int rc;
9302 
9303 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
9304 	if (rc)
9305 		return rc;
9306 
9307 	req->enables = 0;
9308 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
9309 	return hwrm_req_send(bp, req);
9310 }
9311 
9312 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
9313 {
9314 	struct hwrm_nvm_get_dev_info_output nvm_info;
9315 
9316 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
9317 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
9318 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
9319 			 nvm_info.nvm_cfg_ver_upd);
9320 }
9321 
9322 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
9323 {
9324 	struct hwrm_queue_qportcfg_output *resp;
9325 	struct hwrm_queue_qportcfg_input *req;
9326 	u8 i, j, *qptr;
9327 	bool no_rdma;
9328 	int rc = 0;
9329 
9330 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
9331 	if (rc)
9332 		return rc;
9333 
9334 	resp = hwrm_req_hold(bp, req);
9335 	rc = hwrm_req_send(bp, req);
9336 	if (rc)
9337 		goto qportcfg_exit;
9338 
9339 	if (!resp->max_configurable_queues) {
9340 		rc = -EINVAL;
9341 		goto qportcfg_exit;
9342 	}
9343 	bp->max_tc = resp->max_configurable_queues;
9344 	bp->max_lltc = resp->max_configurable_lossless_queues;
9345 	if (bp->max_tc > BNXT_MAX_QUEUE)
9346 		bp->max_tc = BNXT_MAX_QUEUE;
9347 
9348 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
9349 	qptr = &resp->queue_id0;
9350 	for (i = 0, j = 0; i < bp->max_tc; i++) {
9351 		bp->q_info[j].queue_id = *qptr;
9352 		bp->q_ids[i] = *qptr++;
9353 		bp->q_info[j].queue_profile = *qptr++;
9354 		bp->tc_to_qidx[j] = j;
9355 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
9356 		    (no_rdma && BNXT_PF(bp)))
9357 			j++;
9358 	}
9359 	bp->max_q = bp->max_tc;
9360 	bp->max_tc = max_t(u8, j, 1);
9361 
9362 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
9363 		bp->max_tc = 1;
9364 
9365 	if (bp->max_lltc > bp->max_tc)
9366 		bp->max_lltc = bp->max_tc;
9367 
9368 qportcfg_exit:
9369 	hwrm_req_drop(bp, req);
9370 	return rc;
9371 }
9372 
9373 static int bnxt_hwrm_poll(struct bnxt *bp)
9374 {
9375 	struct hwrm_ver_get_input *req;
9376 	int rc;
9377 
9378 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9379 	if (rc)
9380 		return rc;
9381 
9382 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9383 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9384 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9385 
9386 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
9387 	rc = hwrm_req_send(bp, req);
9388 	return rc;
9389 }
9390 
9391 static int bnxt_hwrm_ver_get(struct bnxt *bp)
9392 {
9393 	struct hwrm_ver_get_output *resp;
9394 	struct hwrm_ver_get_input *req;
9395 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
9396 	u32 dev_caps_cfg, hwrm_ver;
9397 	int rc, len;
9398 
9399 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9400 	if (rc)
9401 		return rc;
9402 
9403 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
9404 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
9405 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9406 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9407 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9408 
9409 	resp = hwrm_req_hold(bp, req);
9410 	rc = hwrm_req_send(bp, req);
9411 	if (rc)
9412 		goto hwrm_ver_get_exit;
9413 
9414 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
9415 
9416 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
9417 			     resp->hwrm_intf_min_8b << 8 |
9418 			     resp->hwrm_intf_upd_8b;
9419 	if (resp->hwrm_intf_maj_8b < 1) {
9420 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
9421 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
9422 			    resp->hwrm_intf_upd_8b);
9423 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
9424 	}
9425 
9426 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
9427 			HWRM_VERSION_UPDATE;
9428 
9429 	if (bp->hwrm_spec_code > hwrm_ver)
9430 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
9431 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
9432 			 HWRM_VERSION_UPDATE);
9433 	else
9434 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
9435 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
9436 			 resp->hwrm_intf_upd_8b);
9437 
9438 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
9439 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
9440 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
9441 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
9442 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
9443 		len = FW_VER_STR_LEN;
9444 	} else {
9445 		fw_maj = resp->hwrm_fw_maj_8b;
9446 		fw_min = resp->hwrm_fw_min_8b;
9447 		fw_bld = resp->hwrm_fw_bld_8b;
9448 		fw_rsv = resp->hwrm_fw_rsvd_8b;
9449 		len = BC_HWRM_STR_LEN;
9450 	}
9451 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
9452 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
9453 		 fw_rsv);
9454 
9455 	if (strlen(resp->active_pkg_name)) {
9456 		int fw_ver_len = strlen(bp->fw_ver_str);
9457 
9458 		snprintf(bp->fw_ver_str + fw_ver_len,
9459 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
9460 			 resp->active_pkg_name);
9461 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
9462 	}
9463 
9464 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
9465 	if (!bp->hwrm_cmd_timeout)
9466 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
9467 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
9468 	if (!bp->hwrm_cmd_max_timeout)
9469 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
9470 	else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT)
9471 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n",
9472 			    bp->hwrm_cmd_max_timeout / 1000);
9473 
9474 	if (resp->hwrm_intf_maj_8b >= 1) {
9475 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
9476 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
9477 	}
9478 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
9479 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
9480 
9481 	bp->chip_num = le16_to_cpu(resp->chip_num);
9482 	bp->chip_rev = resp->chip_rev;
9483 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
9484 	    !resp->chip_metal)
9485 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
9486 
9487 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
9488 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
9489 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
9490 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
9491 
9492 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
9493 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
9494 
9495 	if (dev_caps_cfg &
9496 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
9497 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
9498 
9499 	if (dev_caps_cfg &
9500 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
9501 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
9502 
9503 	if (dev_caps_cfg &
9504 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
9505 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
9506 
9507 hwrm_ver_get_exit:
9508 	hwrm_req_drop(bp, req);
9509 	return rc;
9510 }
9511 
9512 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
9513 {
9514 	struct hwrm_fw_set_time_input *req;
9515 	struct tm tm;
9516 	time64_t now = ktime_get_real_seconds();
9517 	int rc;
9518 
9519 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
9520 	    bp->hwrm_spec_code < 0x10400)
9521 		return -EOPNOTSUPP;
9522 
9523 	time64_to_tm(now, 0, &tm);
9524 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
9525 	if (rc)
9526 		return rc;
9527 
9528 	req->year = cpu_to_le16(1900 + tm.tm_year);
9529 	req->month = 1 + tm.tm_mon;
9530 	req->day = tm.tm_mday;
9531 	req->hour = tm.tm_hour;
9532 	req->minute = tm.tm_min;
9533 	req->second = tm.tm_sec;
9534 	return hwrm_req_send(bp, req);
9535 }
9536 
9537 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
9538 {
9539 	u64 sw_tmp;
9540 
9541 	hw &= mask;
9542 	sw_tmp = (*sw & ~mask) | hw;
9543 	if (hw < (*sw & mask))
9544 		sw_tmp += mask + 1;
9545 	WRITE_ONCE(*sw, sw_tmp);
9546 }
9547 
9548 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
9549 				    int count, bool ignore_zero)
9550 {
9551 	int i;
9552 
9553 	for (i = 0; i < count; i++) {
9554 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
9555 
9556 		if (ignore_zero && !hw)
9557 			continue;
9558 
9559 		if (masks[i] == -1ULL)
9560 			sw_stats[i] = hw;
9561 		else
9562 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
9563 	}
9564 }
9565 
9566 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
9567 {
9568 	if (!stats->hw_stats)
9569 		return;
9570 
9571 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
9572 				stats->hw_masks, stats->len / 8, false);
9573 }
9574 
9575 static void bnxt_accumulate_all_stats(struct bnxt *bp)
9576 {
9577 	struct bnxt_stats_mem *ring0_stats;
9578 	bool ignore_zero = false;
9579 	int i;
9580 
9581 	/* Chip bug.  Counter intermittently becomes 0. */
9582 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
9583 		ignore_zero = true;
9584 
9585 	for (i = 0; i < bp->cp_nr_rings; i++) {
9586 		struct bnxt_napi *bnapi = bp->bnapi[i];
9587 		struct bnxt_cp_ring_info *cpr;
9588 		struct bnxt_stats_mem *stats;
9589 
9590 		cpr = &bnapi->cp_ring;
9591 		stats = &cpr->stats;
9592 		if (!i)
9593 			ring0_stats = stats;
9594 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
9595 					ring0_stats->hw_masks,
9596 					ring0_stats->len / 8, ignore_zero);
9597 	}
9598 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
9599 		struct bnxt_stats_mem *stats = &bp->port_stats;
9600 		__le64 *hw_stats = stats->hw_stats;
9601 		u64 *sw_stats = stats->sw_stats;
9602 		u64 *masks = stats->hw_masks;
9603 		int cnt;
9604 
9605 		cnt = sizeof(struct rx_port_stats) / 8;
9606 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
9607 
9608 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9609 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9610 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9611 		cnt = sizeof(struct tx_port_stats) / 8;
9612 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
9613 	}
9614 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
9615 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
9616 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
9617 	}
9618 }
9619 
9620 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
9621 {
9622 	struct hwrm_port_qstats_input *req;
9623 	struct bnxt_pf_info *pf = &bp->pf;
9624 	int rc;
9625 
9626 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
9627 		return 0;
9628 
9629 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
9630 		return -EOPNOTSUPP;
9631 
9632 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
9633 	if (rc)
9634 		return rc;
9635 
9636 	req->flags = flags;
9637 	req->port_id = cpu_to_le16(pf->port_id);
9638 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
9639 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
9640 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
9641 	return hwrm_req_send(bp, req);
9642 }
9643 
9644 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
9645 {
9646 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
9647 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
9648 	struct hwrm_port_qstats_ext_output *resp_qs;
9649 	struct hwrm_port_qstats_ext_input *req_qs;
9650 	struct bnxt_pf_info *pf = &bp->pf;
9651 	u32 tx_stat_size;
9652 	int rc;
9653 
9654 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
9655 		return 0;
9656 
9657 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
9658 		return -EOPNOTSUPP;
9659 
9660 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
9661 	if (rc)
9662 		return rc;
9663 
9664 	req_qs->flags = flags;
9665 	req_qs->port_id = cpu_to_le16(pf->port_id);
9666 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
9667 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
9668 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
9669 		       sizeof(struct tx_port_stats_ext) : 0;
9670 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
9671 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
9672 	resp_qs = hwrm_req_hold(bp, req_qs);
9673 	rc = hwrm_req_send(bp, req_qs);
9674 	if (!rc) {
9675 		bp->fw_rx_stats_ext_size =
9676 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
9677 		if (BNXT_FW_MAJ(bp) < 220 &&
9678 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
9679 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
9680 
9681 		bp->fw_tx_stats_ext_size = tx_stat_size ?
9682 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
9683 	} else {
9684 		bp->fw_rx_stats_ext_size = 0;
9685 		bp->fw_tx_stats_ext_size = 0;
9686 	}
9687 	hwrm_req_drop(bp, req_qs);
9688 
9689 	if (flags)
9690 		return rc;
9691 
9692 	if (bp->fw_tx_stats_ext_size <=
9693 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
9694 		bp->pri2cos_valid = 0;
9695 		return rc;
9696 	}
9697 
9698 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
9699 	if (rc)
9700 		return rc;
9701 
9702 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
9703 
9704 	resp_qc = hwrm_req_hold(bp, req_qc);
9705 	rc = hwrm_req_send(bp, req_qc);
9706 	if (!rc) {
9707 		u8 *pri2cos;
9708 		int i, j;
9709 
9710 		pri2cos = &resp_qc->pri0_cos_queue_id;
9711 		for (i = 0; i < 8; i++) {
9712 			u8 queue_id = pri2cos[i];
9713 			u8 queue_idx;
9714 
9715 			/* Per port queue IDs start from 0, 10, 20, etc */
9716 			queue_idx = queue_id % 10;
9717 			if (queue_idx > BNXT_MAX_QUEUE) {
9718 				bp->pri2cos_valid = false;
9719 				hwrm_req_drop(bp, req_qc);
9720 				return rc;
9721 			}
9722 			for (j = 0; j < bp->max_q; j++) {
9723 				if (bp->q_ids[j] == queue_id)
9724 					bp->pri2cos_idx[i] = queue_idx;
9725 			}
9726 		}
9727 		bp->pri2cos_valid = true;
9728 	}
9729 	hwrm_req_drop(bp, req_qc);
9730 
9731 	return rc;
9732 }
9733 
9734 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
9735 {
9736 	bnxt_hwrm_tunnel_dst_port_free(bp,
9737 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
9738 	bnxt_hwrm_tunnel_dst_port_free(bp,
9739 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
9740 }
9741 
9742 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
9743 {
9744 	int rc, i;
9745 	u32 tpa_flags = 0;
9746 
9747 	if (set_tpa)
9748 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
9749 	else if (BNXT_NO_FW_ACCESS(bp))
9750 		return 0;
9751 	for (i = 0; i < bp->nr_vnics; i++) {
9752 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
9753 		if (rc) {
9754 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
9755 				   i, rc);
9756 			return rc;
9757 		}
9758 	}
9759 	return 0;
9760 }
9761 
9762 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
9763 {
9764 	int i;
9765 
9766 	for (i = 0; i < bp->nr_vnics; i++)
9767 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
9768 }
9769 
9770 static void bnxt_clear_vnic(struct bnxt *bp)
9771 {
9772 	if (!bp->vnic_info)
9773 		return;
9774 
9775 	bnxt_hwrm_clear_vnic_filter(bp);
9776 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
9777 		/* clear all RSS setting before free vnic ctx */
9778 		bnxt_hwrm_clear_vnic_rss(bp);
9779 		bnxt_hwrm_vnic_ctx_free(bp);
9780 	}
9781 	/* before free the vnic, undo the vnic tpa settings */
9782 	if (bp->flags & BNXT_FLAG_TPA)
9783 		bnxt_set_tpa(bp, false);
9784 	bnxt_hwrm_vnic_free(bp);
9785 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
9786 		bnxt_hwrm_vnic_ctx_free(bp);
9787 }
9788 
9789 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
9790 				    bool irq_re_init)
9791 {
9792 	bnxt_clear_vnic(bp);
9793 	bnxt_hwrm_ring_free(bp, close_path);
9794 	bnxt_hwrm_ring_grp_free(bp);
9795 	if (irq_re_init) {
9796 		bnxt_hwrm_stat_ctx_free(bp);
9797 		bnxt_hwrm_free_tunnel_ports(bp);
9798 	}
9799 }
9800 
9801 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
9802 {
9803 	struct hwrm_func_cfg_input *req;
9804 	u8 evb_mode;
9805 	int rc;
9806 
9807 	if (br_mode == BRIDGE_MODE_VEB)
9808 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
9809 	else if (br_mode == BRIDGE_MODE_VEPA)
9810 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
9811 	else
9812 		return -EINVAL;
9813 
9814 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
9815 	if (rc)
9816 		return rc;
9817 
9818 	req->fid = cpu_to_le16(0xffff);
9819 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
9820 	req->evb_mode = evb_mode;
9821 	return hwrm_req_send(bp, req);
9822 }
9823 
9824 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
9825 {
9826 	struct hwrm_func_cfg_input *req;
9827 	int rc;
9828 
9829 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
9830 		return 0;
9831 
9832 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
9833 	if (rc)
9834 		return rc;
9835 
9836 	req->fid = cpu_to_le16(0xffff);
9837 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
9838 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
9839 	if (size == 128)
9840 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
9841 
9842 	return hwrm_req_send(bp, req);
9843 }
9844 
9845 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
9846 {
9847 	int rc;
9848 
9849 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
9850 		goto skip_rss_ctx;
9851 
9852 	/* allocate context for vnic */
9853 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
9854 	if (rc) {
9855 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
9856 			   vnic->vnic_id, rc);
9857 		goto vnic_setup_err;
9858 	}
9859 	bp->rsscos_nr_ctxs++;
9860 
9861 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
9862 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
9863 		if (rc) {
9864 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
9865 				   vnic->vnic_id, rc);
9866 			goto vnic_setup_err;
9867 		}
9868 		bp->rsscos_nr_ctxs++;
9869 	}
9870 
9871 skip_rss_ctx:
9872 	/* configure default vnic, ring grp */
9873 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
9874 	if (rc) {
9875 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
9876 			   vnic->vnic_id, rc);
9877 		goto vnic_setup_err;
9878 	}
9879 
9880 	/* Enable RSS hashing on vnic */
9881 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
9882 	if (rc) {
9883 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
9884 			   vnic->vnic_id, rc);
9885 		goto vnic_setup_err;
9886 	}
9887 
9888 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
9889 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
9890 		if (rc) {
9891 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
9892 				   vnic->vnic_id, rc);
9893 		}
9894 	}
9895 
9896 vnic_setup_err:
9897 	return rc;
9898 }
9899 
9900 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
9901 {
9902 	int rc;
9903 
9904 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
9905 	if (rc) {
9906 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
9907 			   vnic->vnic_id, rc);
9908 		return rc;
9909 	}
9910 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
9911 	if (rc)
9912 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
9913 			   vnic->vnic_id, rc);
9914 	return rc;
9915 }
9916 
9917 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
9918 {
9919 	int rc, i, nr_ctxs;
9920 
9921 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
9922 	for (i = 0; i < nr_ctxs; i++) {
9923 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
9924 		if (rc) {
9925 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
9926 				   vnic->vnic_id, i, rc);
9927 			break;
9928 		}
9929 		bp->rsscos_nr_ctxs++;
9930 	}
9931 	if (i < nr_ctxs)
9932 		return -ENOMEM;
9933 
9934 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
9935 	if (rc)
9936 		return rc;
9937 
9938 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
9939 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
9940 		if (rc) {
9941 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
9942 				   vnic->vnic_id, rc);
9943 		}
9944 	}
9945 	return rc;
9946 }
9947 
9948 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
9949 {
9950 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
9951 		return __bnxt_setup_vnic_p5(bp, vnic);
9952 	else
9953 		return __bnxt_setup_vnic(bp, vnic);
9954 }
9955 
9956 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
9957 				     struct bnxt_vnic_info *vnic,
9958 				     u16 start_rx_ring_idx, int rx_rings)
9959 {
9960 	int rc;
9961 
9962 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
9963 	if (rc) {
9964 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
9965 			   vnic->vnic_id, rc);
9966 		return rc;
9967 	}
9968 	return bnxt_setup_vnic(bp, vnic);
9969 }
9970 
9971 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
9972 {
9973 	struct bnxt_vnic_info *vnic;
9974 	int i, rc = 0;
9975 
9976 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
9977 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
9978 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
9979 	}
9980 
9981 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
9982 		return 0;
9983 
9984 	for (i = 0; i < bp->rx_nr_rings; i++) {
9985 		u16 vnic_id = i + 1;
9986 		u16 ring_id = i;
9987 
9988 		if (vnic_id >= bp->nr_vnics)
9989 			break;
9990 
9991 		vnic = &bp->vnic_info[vnic_id];
9992 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
9993 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
9994 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
9995 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
9996 			break;
9997 	}
9998 	return rc;
9999 }
10000 
10001 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10002 			  bool all)
10003 {
10004 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10005 	struct bnxt_filter_base *usr_fltr, *tmp;
10006 	struct bnxt_ntuple_filter *ntp_fltr;
10007 	int i;
10008 
10009 	bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10010 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10011 		if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10012 			bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10013 	}
10014 	if (!all)
10015 		return;
10016 
10017 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10018 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10019 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10020 			ntp_fltr = container_of(usr_fltr,
10021 						struct bnxt_ntuple_filter,
10022 						base);
10023 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10024 			bnxt_del_ntp_filter(bp, ntp_fltr);
10025 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10026 		}
10027 	}
10028 
10029 	if (vnic->rss_table)
10030 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10031 				  vnic->rss_table,
10032 				  vnic->rss_table_dma_addr);
10033 	kfree(rss_ctx->rss_indir_tbl);
10034 	list_del(&rss_ctx->list);
10035 	bp->num_rss_ctx--;
10036 	clear_bit(rss_ctx->index, bp->rss_ctx_bmap);
10037 	kfree(rss_ctx);
10038 }
10039 
10040 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10041 {
10042 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10043 	struct bnxt_rss_ctx *rss_ctx, *tmp;
10044 
10045 	list_for_each_entry_safe(rss_ctx, tmp, &bp->rss_ctx_list, list) {
10046 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10047 
10048 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10049 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10050 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10051 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10052 				   rss_ctx->index);
10053 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10054 		}
10055 	}
10056 }
10057 
10058 struct bnxt_rss_ctx *bnxt_alloc_rss_ctx(struct bnxt *bp)
10059 {
10060 	struct bnxt_rss_ctx *rss_ctx = NULL;
10061 
10062 	rss_ctx = kzalloc(sizeof(*rss_ctx), GFP_KERNEL);
10063 	if (rss_ctx) {
10064 		rss_ctx->vnic.rss_ctx = rss_ctx;
10065 		list_add_tail(&rss_ctx->list, &bp->rss_ctx_list);
10066 		bp->num_rss_ctx++;
10067 	}
10068 	return rss_ctx;
10069 }
10070 
10071 void bnxt_clear_rss_ctxs(struct bnxt *bp, bool all)
10072 {
10073 	struct bnxt_rss_ctx *rss_ctx, *tmp;
10074 
10075 	list_for_each_entry_safe(rss_ctx, tmp, &bp->rss_ctx_list, list)
10076 		bnxt_del_one_rss_ctx(bp, rss_ctx, all);
10077 
10078 	if (all)
10079 		bitmap_free(bp->rss_ctx_bmap);
10080 }
10081 
10082 static void bnxt_init_multi_rss_ctx(struct bnxt *bp)
10083 {
10084 	bp->rss_ctx_bmap = bitmap_zalloc(BNXT_RSS_CTX_BMAP_LEN, GFP_KERNEL);
10085 	if (bp->rss_ctx_bmap) {
10086 		/* burn index 0 since we cannot have context 0 */
10087 		__set_bit(0, bp->rss_ctx_bmap);
10088 		INIT_LIST_HEAD(&bp->rss_ctx_list);
10089 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
10090 	}
10091 }
10092 
10093 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
10094 static bool bnxt_promisc_ok(struct bnxt *bp)
10095 {
10096 #ifdef CONFIG_BNXT_SRIOV
10097 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10098 		return false;
10099 #endif
10100 	return true;
10101 }
10102 
10103 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10104 {
10105 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10106 	unsigned int rc = 0;
10107 
10108 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10109 	if (rc) {
10110 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10111 			   rc);
10112 		return rc;
10113 	}
10114 
10115 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10116 	if (rc) {
10117 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10118 			   rc);
10119 		return rc;
10120 	}
10121 	return rc;
10122 }
10123 
10124 static int bnxt_cfg_rx_mode(struct bnxt *);
10125 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10126 
10127 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10128 {
10129 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10130 	int rc = 0;
10131 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10132 
10133 	if (irq_re_init) {
10134 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10135 		if (rc) {
10136 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10137 				   rc);
10138 			goto err_out;
10139 		}
10140 	}
10141 
10142 	rc = bnxt_hwrm_ring_alloc(bp);
10143 	if (rc) {
10144 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10145 		goto err_out;
10146 	}
10147 
10148 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10149 	if (rc) {
10150 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10151 		goto err_out;
10152 	}
10153 
10154 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10155 		rx_nr_rings--;
10156 
10157 	/* default vnic 0 */
10158 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10159 	if (rc) {
10160 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10161 		goto err_out;
10162 	}
10163 
10164 	if (BNXT_VF(bp))
10165 		bnxt_hwrm_func_qcfg(bp);
10166 
10167 	rc = bnxt_setup_vnic(bp, vnic);
10168 	if (rc)
10169 		goto err_out;
10170 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10171 		bnxt_hwrm_update_rss_hash_cfg(bp);
10172 
10173 	if (bp->flags & BNXT_FLAG_RFS) {
10174 		rc = bnxt_alloc_rfs_vnics(bp);
10175 		if (rc)
10176 			goto err_out;
10177 	}
10178 
10179 	if (bp->flags & BNXT_FLAG_TPA) {
10180 		rc = bnxt_set_tpa(bp, true);
10181 		if (rc)
10182 			goto err_out;
10183 	}
10184 
10185 	if (BNXT_VF(bp))
10186 		bnxt_update_vf_mac(bp);
10187 
10188 	/* Filter for default vnic 0 */
10189 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
10190 	if (rc) {
10191 		if (BNXT_VF(bp) && rc == -ENODEV)
10192 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
10193 		else
10194 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
10195 		goto err_out;
10196 	}
10197 	vnic->uc_filter_count = 1;
10198 
10199 	vnic->rx_mask = 0;
10200 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
10201 		goto skip_rx_mask;
10202 
10203 	if (bp->dev->flags & IFF_BROADCAST)
10204 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10205 
10206 	if (bp->dev->flags & IFF_PROMISC)
10207 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10208 
10209 	if (bp->dev->flags & IFF_ALLMULTI) {
10210 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10211 		vnic->mc_list_count = 0;
10212 	} else if (bp->dev->flags & IFF_MULTICAST) {
10213 		u32 mask = 0;
10214 
10215 		bnxt_mc_list_updated(bp, &mask);
10216 		vnic->rx_mask |= mask;
10217 	}
10218 
10219 	rc = bnxt_cfg_rx_mode(bp);
10220 	if (rc)
10221 		goto err_out;
10222 
10223 skip_rx_mask:
10224 	rc = bnxt_hwrm_set_coal(bp);
10225 	if (rc)
10226 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
10227 				rc);
10228 
10229 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10230 		rc = bnxt_setup_nitroa0_vnic(bp);
10231 		if (rc)
10232 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
10233 				   rc);
10234 	}
10235 
10236 	if (BNXT_VF(bp)) {
10237 		bnxt_hwrm_func_qcfg(bp);
10238 		netdev_update_features(bp->dev);
10239 	}
10240 
10241 	return 0;
10242 
10243 err_out:
10244 	bnxt_hwrm_resource_free(bp, 0, true);
10245 
10246 	return rc;
10247 }
10248 
10249 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
10250 {
10251 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
10252 	return 0;
10253 }
10254 
10255 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
10256 {
10257 	bnxt_init_cp_rings(bp);
10258 	bnxt_init_rx_rings(bp);
10259 	bnxt_init_tx_rings(bp);
10260 	bnxt_init_ring_grps(bp, irq_re_init);
10261 	bnxt_init_vnics(bp);
10262 
10263 	return bnxt_init_chip(bp, irq_re_init);
10264 }
10265 
10266 static int bnxt_set_real_num_queues(struct bnxt *bp)
10267 {
10268 	int rc;
10269 	struct net_device *dev = bp->dev;
10270 
10271 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
10272 					  bp->tx_nr_rings_xdp);
10273 	if (rc)
10274 		return rc;
10275 
10276 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
10277 	if (rc)
10278 		return rc;
10279 
10280 #ifdef CONFIG_RFS_ACCEL
10281 	if (bp->flags & BNXT_FLAG_RFS)
10282 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
10283 #endif
10284 
10285 	return rc;
10286 }
10287 
10288 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10289 			     bool shared)
10290 {
10291 	int _rx = *rx, _tx = *tx;
10292 
10293 	if (shared) {
10294 		*rx = min_t(int, _rx, max);
10295 		*tx = min_t(int, _tx, max);
10296 	} else {
10297 		if (max < 2)
10298 			return -ENOMEM;
10299 
10300 		while (_rx + _tx > max) {
10301 			if (_rx > _tx && _rx > 1)
10302 				_rx--;
10303 			else if (_tx > 1)
10304 				_tx--;
10305 		}
10306 		*rx = _rx;
10307 		*tx = _tx;
10308 	}
10309 	return 0;
10310 }
10311 
10312 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
10313 {
10314 	return (tx - tx_xdp) / tx_sets + tx_xdp;
10315 }
10316 
10317 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
10318 {
10319 	int tcs = bp->num_tc;
10320 
10321 	if (!tcs)
10322 		tcs = 1;
10323 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
10324 }
10325 
10326 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
10327 {
10328 	int tcs = bp->num_tc;
10329 
10330 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
10331 	       bp->tx_nr_rings_xdp;
10332 }
10333 
10334 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10335 			   bool sh)
10336 {
10337 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
10338 
10339 	if (tx_cp != *tx) {
10340 		int tx_saved = tx_cp, rc;
10341 
10342 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
10343 		if (rc)
10344 			return rc;
10345 		if (tx_cp != tx_saved)
10346 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
10347 		return 0;
10348 	}
10349 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
10350 }
10351 
10352 static void bnxt_setup_msix(struct bnxt *bp)
10353 {
10354 	const int len = sizeof(bp->irq_tbl[0].name);
10355 	struct net_device *dev = bp->dev;
10356 	int tcs, i;
10357 
10358 	tcs = bp->num_tc;
10359 	if (tcs) {
10360 		int i, off, count;
10361 
10362 		for (i = 0; i < tcs; i++) {
10363 			count = bp->tx_nr_rings_per_tc;
10364 			off = BNXT_TC_TO_RING_BASE(bp, i);
10365 			netdev_set_tc_queue(dev, i, count, off);
10366 		}
10367 	}
10368 
10369 	for (i = 0; i < bp->cp_nr_rings; i++) {
10370 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10371 		char *attr;
10372 
10373 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
10374 			attr = "TxRx";
10375 		else if (i < bp->rx_nr_rings)
10376 			attr = "rx";
10377 		else
10378 			attr = "tx";
10379 
10380 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
10381 			 attr, i);
10382 		bp->irq_tbl[map_idx].handler = bnxt_msix;
10383 	}
10384 }
10385 
10386 static void bnxt_setup_inta(struct bnxt *bp)
10387 {
10388 	const int len = sizeof(bp->irq_tbl[0].name);
10389 
10390 	if (bp->num_tc) {
10391 		netdev_reset_tc(bp->dev);
10392 		bp->num_tc = 0;
10393 	}
10394 
10395 	snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
10396 		 0);
10397 	bp->irq_tbl[0].handler = bnxt_inta;
10398 }
10399 
10400 static int bnxt_init_int_mode(struct bnxt *bp);
10401 
10402 static int bnxt_setup_int_mode(struct bnxt *bp)
10403 {
10404 	int rc;
10405 
10406 	if (!bp->irq_tbl) {
10407 		rc = bnxt_init_int_mode(bp);
10408 		if (rc || !bp->irq_tbl)
10409 			return rc ?: -ENODEV;
10410 	}
10411 
10412 	if (bp->flags & BNXT_FLAG_USING_MSIX)
10413 		bnxt_setup_msix(bp);
10414 	else
10415 		bnxt_setup_inta(bp);
10416 
10417 	rc = bnxt_set_real_num_queues(bp);
10418 	return rc;
10419 }
10420 
10421 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
10422 {
10423 	return bp->hw_resc.max_rsscos_ctxs;
10424 }
10425 
10426 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
10427 {
10428 	return bp->hw_resc.max_vnics;
10429 }
10430 
10431 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
10432 {
10433 	return bp->hw_resc.max_stat_ctxs;
10434 }
10435 
10436 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
10437 {
10438 	return bp->hw_resc.max_cp_rings;
10439 }
10440 
10441 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
10442 {
10443 	unsigned int cp = bp->hw_resc.max_cp_rings;
10444 
10445 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
10446 		cp -= bnxt_get_ulp_msix_num(bp);
10447 
10448 	return cp;
10449 }
10450 
10451 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
10452 {
10453 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
10454 
10455 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10456 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
10457 
10458 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
10459 }
10460 
10461 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
10462 {
10463 	bp->hw_resc.max_irqs = max_irqs;
10464 }
10465 
10466 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
10467 {
10468 	unsigned int cp;
10469 
10470 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
10471 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10472 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
10473 	else
10474 		return cp - bp->cp_nr_rings;
10475 }
10476 
10477 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
10478 {
10479 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
10480 }
10481 
10482 int bnxt_get_avail_msix(struct bnxt *bp, int num)
10483 {
10484 	int max_cp = bnxt_get_max_func_cp_rings(bp);
10485 	int max_irq = bnxt_get_max_func_irqs(bp);
10486 	int total_req = bp->cp_nr_rings + num;
10487 	int max_idx, avail_msix;
10488 
10489 	max_idx = bp->total_irqs;
10490 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
10491 		max_idx = min_t(int, bp->total_irqs, max_cp);
10492 	avail_msix = max_idx - bp->cp_nr_rings;
10493 	if (!BNXT_NEW_RM(bp) || avail_msix >= num)
10494 		return avail_msix;
10495 
10496 	if (max_irq < total_req) {
10497 		num = max_irq - bp->cp_nr_rings;
10498 		if (num <= 0)
10499 			return 0;
10500 	}
10501 	return num;
10502 }
10503 
10504 static int bnxt_get_num_msix(struct bnxt *bp)
10505 {
10506 	if (!BNXT_NEW_RM(bp))
10507 		return bnxt_get_max_func_irqs(bp);
10508 
10509 	return bnxt_nq_rings_in_use(bp);
10510 }
10511 
10512 static int bnxt_init_msix(struct bnxt *bp)
10513 {
10514 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp;
10515 	struct msix_entry *msix_ent;
10516 
10517 	total_vecs = bnxt_get_num_msix(bp);
10518 	max = bnxt_get_max_func_irqs(bp);
10519 	if (total_vecs > max)
10520 		total_vecs = max;
10521 
10522 	if (!total_vecs)
10523 		return 0;
10524 
10525 	msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
10526 	if (!msix_ent)
10527 		return -ENOMEM;
10528 
10529 	for (i = 0; i < total_vecs; i++) {
10530 		msix_ent[i].entry = i;
10531 		msix_ent[i].vector = 0;
10532 	}
10533 
10534 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
10535 		min = 2;
10536 
10537 	total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
10538 	ulp_msix = bnxt_get_ulp_msix_num(bp);
10539 	if (total_vecs < 0 || total_vecs < ulp_msix) {
10540 		rc = -ENODEV;
10541 		goto msix_setup_exit;
10542 	}
10543 
10544 	bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
10545 	if (bp->irq_tbl) {
10546 		for (i = 0; i < total_vecs; i++)
10547 			bp->irq_tbl[i].vector = msix_ent[i].vector;
10548 
10549 		bp->total_irqs = total_vecs;
10550 		/* Trim rings based upon num of vectors allocated */
10551 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
10552 				     total_vecs - ulp_msix, min == 1);
10553 		if (rc)
10554 			goto msix_setup_exit;
10555 
10556 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
10557 		bp->cp_nr_rings = (min == 1) ?
10558 				  max_t(int, tx_cp, bp->rx_nr_rings) :
10559 				  tx_cp + bp->rx_nr_rings;
10560 
10561 	} else {
10562 		rc = -ENOMEM;
10563 		goto msix_setup_exit;
10564 	}
10565 	bp->flags |= BNXT_FLAG_USING_MSIX;
10566 	kfree(msix_ent);
10567 	return 0;
10568 
10569 msix_setup_exit:
10570 	netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
10571 	kfree(bp->irq_tbl);
10572 	bp->irq_tbl = NULL;
10573 	pci_disable_msix(bp->pdev);
10574 	kfree(msix_ent);
10575 	return rc;
10576 }
10577 
10578 static int bnxt_init_inta(struct bnxt *bp)
10579 {
10580 	bp->irq_tbl = kzalloc(sizeof(struct bnxt_irq), GFP_KERNEL);
10581 	if (!bp->irq_tbl)
10582 		return -ENOMEM;
10583 
10584 	bp->total_irqs = 1;
10585 	bp->rx_nr_rings = 1;
10586 	bp->tx_nr_rings = 1;
10587 	bp->cp_nr_rings = 1;
10588 	bp->flags |= BNXT_FLAG_SHARED_RINGS;
10589 	bp->irq_tbl[0].vector = bp->pdev->irq;
10590 	return 0;
10591 }
10592 
10593 static int bnxt_init_int_mode(struct bnxt *bp)
10594 {
10595 	int rc = -ENODEV;
10596 
10597 	if (bp->flags & BNXT_FLAG_MSIX_CAP)
10598 		rc = bnxt_init_msix(bp);
10599 
10600 	if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
10601 		/* fallback to INTA */
10602 		rc = bnxt_init_inta(bp);
10603 	}
10604 	return rc;
10605 }
10606 
10607 static void bnxt_clear_int_mode(struct bnxt *bp)
10608 {
10609 	if (bp->flags & BNXT_FLAG_USING_MSIX)
10610 		pci_disable_msix(bp->pdev);
10611 
10612 	kfree(bp->irq_tbl);
10613 	bp->irq_tbl = NULL;
10614 	bp->flags &= ~BNXT_FLAG_USING_MSIX;
10615 }
10616 
10617 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
10618 {
10619 	bool irq_cleared = false;
10620 	int tcs = bp->num_tc;
10621 	int rc;
10622 
10623 	if (!bnxt_need_reserve_rings(bp))
10624 		return 0;
10625 
10626 	if (irq_re_init && BNXT_NEW_RM(bp) &&
10627 	    bnxt_get_num_msix(bp) != bp->total_irqs) {
10628 		bnxt_ulp_irq_stop(bp);
10629 		bnxt_clear_int_mode(bp);
10630 		irq_cleared = true;
10631 	}
10632 	rc = __bnxt_reserve_rings(bp);
10633 	if (irq_cleared) {
10634 		if (!rc)
10635 			rc = bnxt_init_int_mode(bp);
10636 		bnxt_ulp_irq_restart(bp, rc);
10637 	}
10638 	if (rc) {
10639 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
10640 		return rc;
10641 	}
10642 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
10643 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
10644 		netdev_err(bp->dev, "tx ring reservation failure\n");
10645 		netdev_reset_tc(bp->dev);
10646 		bp->num_tc = 0;
10647 		if (bp->tx_nr_rings_xdp)
10648 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
10649 		else
10650 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
10651 		return -ENOMEM;
10652 	}
10653 	return 0;
10654 }
10655 
10656 static void bnxt_free_irq(struct bnxt *bp)
10657 {
10658 	struct bnxt_irq *irq;
10659 	int i;
10660 
10661 #ifdef CONFIG_RFS_ACCEL
10662 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
10663 	bp->dev->rx_cpu_rmap = NULL;
10664 #endif
10665 	if (!bp->irq_tbl || !bp->bnapi)
10666 		return;
10667 
10668 	for (i = 0; i < bp->cp_nr_rings; i++) {
10669 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10670 
10671 		irq = &bp->irq_tbl[map_idx];
10672 		if (irq->requested) {
10673 			if (irq->have_cpumask) {
10674 				irq_set_affinity_hint(irq->vector, NULL);
10675 				free_cpumask_var(irq->cpu_mask);
10676 				irq->have_cpumask = 0;
10677 			}
10678 			free_irq(irq->vector, bp->bnapi[i]);
10679 		}
10680 
10681 		irq->requested = 0;
10682 	}
10683 }
10684 
10685 static int bnxt_request_irq(struct bnxt *bp)
10686 {
10687 	int i, j, rc = 0;
10688 	unsigned long flags = 0;
10689 #ifdef CONFIG_RFS_ACCEL
10690 	struct cpu_rmap *rmap;
10691 #endif
10692 
10693 	rc = bnxt_setup_int_mode(bp);
10694 	if (rc) {
10695 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
10696 			   rc);
10697 		return rc;
10698 	}
10699 #ifdef CONFIG_RFS_ACCEL
10700 	rmap = bp->dev->rx_cpu_rmap;
10701 #endif
10702 	if (!(bp->flags & BNXT_FLAG_USING_MSIX))
10703 		flags = IRQF_SHARED;
10704 
10705 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
10706 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10707 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
10708 
10709 #ifdef CONFIG_RFS_ACCEL
10710 		if (rmap && bp->bnapi[i]->rx_ring) {
10711 			rc = irq_cpu_rmap_add(rmap, irq->vector);
10712 			if (rc)
10713 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
10714 					    j);
10715 			j++;
10716 		}
10717 #endif
10718 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
10719 				 bp->bnapi[i]);
10720 		if (rc)
10721 			break;
10722 
10723 		netif_napi_set_irq(&bp->bnapi[i]->napi, irq->vector);
10724 		irq->requested = 1;
10725 
10726 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
10727 			int numa_node = dev_to_node(&bp->pdev->dev);
10728 
10729 			irq->have_cpumask = 1;
10730 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
10731 					irq->cpu_mask);
10732 			rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
10733 			if (rc) {
10734 				netdev_warn(bp->dev,
10735 					    "Set affinity failed, IRQ = %d\n",
10736 					    irq->vector);
10737 				break;
10738 			}
10739 		}
10740 	}
10741 	return rc;
10742 }
10743 
10744 static void bnxt_del_napi(struct bnxt *bp)
10745 {
10746 	int i;
10747 
10748 	if (!bp->bnapi)
10749 		return;
10750 
10751 	for (i = 0; i < bp->rx_nr_rings; i++)
10752 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
10753 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
10754 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
10755 
10756 	for (i = 0; i < bp->cp_nr_rings; i++) {
10757 		struct bnxt_napi *bnapi = bp->bnapi[i];
10758 
10759 		__netif_napi_del(&bnapi->napi);
10760 	}
10761 	/* We called __netif_napi_del(), we need
10762 	 * to respect an RCU grace period before freeing napi structures.
10763 	 */
10764 	synchronize_net();
10765 }
10766 
10767 static void bnxt_init_napi(struct bnxt *bp)
10768 {
10769 	int i;
10770 	unsigned int cp_nr_rings = bp->cp_nr_rings;
10771 	struct bnxt_napi *bnapi;
10772 
10773 	if (bp->flags & BNXT_FLAG_USING_MSIX) {
10774 		int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
10775 
10776 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10777 			poll_fn = bnxt_poll_p5;
10778 		else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10779 			cp_nr_rings--;
10780 		for (i = 0; i < cp_nr_rings; i++) {
10781 			bnapi = bp->bnapi[i];
10782 			netif_napi_add(bp->dev, &bnapi->napi, poll_fn);
10783 		}
10784 		if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10785 			bnapi = bp->bnapi[cp_nr_rings];
10786 			netif_napi_add(bp->dev, &bnapi->napi,
10787 				       bnxt_poll_nitroa0);
10788 		}
10789 	} else {
10790 		bnapi = bp->bnapi[0];
10791 		netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll);
10792 	}
10793 }
10794 
10795 static void bnxt_disable_napi(struct bnxt *bp)
10796 {
10797 	int i;
10798 
10799 	if (!bp->bnapi ||
10800 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
10801 		return;
10802 
10803 	for (i = 0; i < bp->cp_nr_rings; i++) {
10804 		struct bnxt_napi *bnapi = bp->bnapi[i];
10805 		struct bnxt_cp_ring_info *cpr;
10806 
10807 		cpr = &bnapi->cp_ring;
10808 		if (bnapi->tx_fault)
10809 			cpr->sw_stats.tx.tx_resets++;
10810 		if (bnapi->in_reset)
10811 			cpr->sw_stats.rx.rx_resets++;
10812 		napi_disable(&bnapi->napi);
10813 		if (bnapi->rx_ring)
10814 			cancel_work_sync(&cpr->dim.work);
10815 	}
10816 }
10817 
10818 static void bnxt_enable_napi(struct bnxt *bp)
10819 {
10820 	int i;
10821 
10822 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
10823 	for (i = 0; i < bp->cp_nr_rings; i++) {
10824 		struct bnxt_napi *bnapi = bp->bnapi[i];
10825 		struct bnxt_cp_ring_info *cpr;
10826 
10827 		bnapi->tx_fault = 0;
10828 
10829 		cpr = &bnapi->cp_ring;
10830 		bnapi->in_reset = false;
10831 
10832 		if (bnapi->rx_ring) {
10833 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
10834 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
10835 		}
10836 		napi_enable(&bnapi->napi);
10837 	}
10838 }
10839 
10840 void bnxt_tx_disable(struct bnxt *bp)
10841 {
10842 	int i;
10843 	struct bnxt_tx_ring_info *txr;
10844 
10845 	if (bp->tx_ring) {
10846 		for (i = 0; i < bp->tx_nr_rings; i++) {
10847 			txr = &bp->tx_ring[i];
10848 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
10849 		}
10850 	}
10851 	/* Make sure napi polls see @dev_state change */
10852 	synchronize_net();
10853 	/* Drop carrier first to prevent TX timeout */
10854 	netif_carrier_off(bp->dev);
10855 	/* Stop all TX queues */
10856 	netif_tx_disable(bp->dev);
10857 }
10858 
10859 void bnxt_tx_enable(struct bnxt *bp)
10860 {
10861 	int i;
10862 	struct bnxt_tx_ring_info *txr;
10863 
10864 	for (i = 0; i < bp->tx_nr_rings; i++) {
10865 		txr = &bp->tx_ring[i];
10866 		WRITE_ONCE(txr->dev_state, 0);
10867 	}
10868 	/* Make sure napi polls see @dev_state change */
10869 	synchronize_net();
10870 	netif_tx_wake_all_queues(bp->dev);
10871 	if (BNXT_LINK_IS_UP(bp))
10872 		netif_carrier_on(bp->dev);
10873 }
10874 
10875 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
10876 {
10877 	u8 active_fec = link_info->active_fec_sig_mode &
10878 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
10879 
10880 	switch (active_fec) {
10881 	default:
10882 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
10883 		return "None";
10884 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
10885 		return "Clause 74 BaseR";
10886 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
10887 		return "Clause 91 RS(528,514)";
10888 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
10889 		return "Clause 91 RS544_1XN";
10890 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
10891 		return "Clause 91 RS(544,514)";
10892 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
10893 		return "Clause 91 RS272_1XN";
10894 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
10895 		return "Clause 91 RS(272,257)";
10896 	}
10897 }
10898 
10899 void bnxt_report_link(struct bnxt *bp)
10900 {
10901 	if (BNXT_LINK_IS_UP(bp)) {
10902 		const char *signal = "";
10903 		const char *flow_ctrl;
10904 		const char *duplex;
10905 		u32 speed;
10906 		u16 fec;
10907 
10908 		netif_carrier_on(bp->dev);
10909 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
10910 		if (speed == SPEED_UNKNOWN) {
10911 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
10912 			return;
10913 		}
10914 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
10915 			duplex = "full";
10916 		else
10917 			duplex = "half";
10918 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
10919 			flow_ctrl = "ON - receive & transmit";
10920 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
10921 			flow_ctrl = "ON - transmit";
10922 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
10923 			flow_ctrl = "ON - receive";
10924 		else
10925 			flow_ctrl = "none";
10926 		if (bp->link_info.phy_qcfg_resp.option_flags &
10927 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
10928 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
10929 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
10930 			switch (sig_mode) {
10931 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
10932 				signal = "(NRZ) ";
10933 				break;
10934 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
10935 				signal = "(PAM4 56Gbps) ";
10936 				break;
10937 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
10938 				signal = "(PAM4 112Gbps) ";
10939 				break;
10940 			default:
10941 				break;
10942 			}
10943 		}
10944 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
10945 			    speed, signal, duplex, flow_ctrl);
10946 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
10947 			netdev_info(bp->dev, "EEE is %s\n",
10948 				    bp->eee.eee_active ? "active" :
10949 							 "not active");
10950 		fec = bp->link_info.fec_cfg;
10951 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
10952 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
10953 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
10954 				    bnxt_report_fec(&bp->link_info));
10955 	} else {
10956 		netif_carrier_off(bp->dev);
10957 		netdev_err(bp->dev, "NIC Link is Down\n");
10958 	}
10959 }
10960 
10961 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
10962 {
10963 	if (!resp->supported_speeds_auto_mode &&
10964 	    !resp->supported_speeds_force_mode &&
10965 	    !resp->supported_pam4_speeds_auto_mode &&
10966 	    !resp->supported_pam4_speeds_force_mode &&
10967 	    !resp->supported_speeds2_auto_mode &&
10968 	    !resp->supported_speeds2_force_mode)
10969 		return true;
10970 	return false;
10971 }
10972 
10973 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
10974 {
10975 	struct bnxt_link_info *link_info = &bp->link_info;
10976 	struct hwrm_port_phy_qcaps_output *resp;
10977 	struct hwrm_port_phy_qcaps_input *req;
10978 	int rc = 0;
10979 
10980 	if (bp->hwrm_spec_code < 0x10201)
10981 		return 0;
10982 
10983 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
10984 	if (rc)
10985 		return rc;
10986 
10987 	resp = hwrm_req_hold(bp, req);
10988 	rc = hwrm_req_send(bp, req);
10989 	if (rc)
10990 		goto hwrm_phy_qcaps_exit;
10991 
10992 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
10993 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
10994 		struct ethtool_keee *eee = &bp->eee;
10995 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
10996 
10997 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
10998 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
10999 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11000 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11001 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11002 	}
11003 
11004 	if (bp->hwrm_spec_code >= 0x10a01) {
11005 		if (bnxt_phy_qcaps_no_speed(resp)) {
11006 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11007 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11008 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11009 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11010 			netdev_info(bp->dev, "Ethernet link enabled\n");
11011 			/* Phy re-enabled, reprobe the speeds */
11012 			link_info->support_auto_speeds = 0;
11013 			link_info->support_pam4_auto_speeds = 0;
11014 			link_info->support_auto_speeds2 = 0;
11015 		}
11016 	}
11017 	if (resp->supported_speeds_auto_mode)
11018 		link_info->support_auto_speeds =
11019 			le16_to_cpu(resp->supported_speeds_auto_mode);
11020 	if (resp->supported_pam4_speeds_auto_mode)
11021 		link_info->support_pam4_auto_speeds =
11022 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11023 	if (resp->supported_speeds2_auto_mode)
11024 		link_info->support_auto_speeds2 =
11025 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11026 
11027 	bp->port_count = resp->port_cnt;
11028 
11029 hwrm_phy_qcaps_exit:
11030 	hwrm_req_drop(bp, req);
11031 	return rc;
11032 }
11033 
11034 static bool bnxt_support_dropped(u16 advertising, u16 supported)
11035 {
11036 	u16 diff = advertising ^ supported;
11037 
11038 	return ((supported | diff) != supported);
11039 }
11040 
11041 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
11042 {
11043 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
11044 
11045 	/* Check if any advertised speeds are no longer supported. The caller
11046 	 * holds the link_lock mutex, so we can modify link_info settings.
11047 	 */
11048 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11049 		if (bnxt_support_dropped(link_info->advertising,
11050 					 link_info->support_auto_speeds2)) {
11051 			link_info->advertising = link_info->support_auto_speeds2;
11052 			return true;
11053 		}
11054 		return false;
11055 	}
11056 	if (bnxt_support_dropped(link_info->advertising,
11057 				 link_info->support_auto_speeds)) {
11058 		link_info->advertising = link_info->support_auto_speeds;
11059 		return true;
11060 	}
11061 	if (bnxt_support_dropped(link_info->advertising_pam4,
11062 				 link_info->support_pam4_auto_speeds)) {
11063 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
11064 		return true;
11065 	}
11066 	return false;
11067 }
11068 
11069 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
11070 {
11071 	struct bnxt_link_info *link_info = &bp->link_info;
11072 	struct hwrm_port_phy_qcfg_output *resp;
11073 	struct hwrm_port_phy_qcfg_input *req;
11074 	u8 link_state = link_info->link_state;
11075 	bool support_changed;
11076 	int rc;
11077 
11078 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
11079 	if (rc)
11080 		return rc;
11081 
11082 	resp = hwrm_req_hold(bp, req);
11083 	rc = hwrm_req_send(bp, req);
11084 	if (rc) {
11085 		hwrm_req_drop(bp, req);
11086 		if (BNXT_VF(bp) && rc == -ENODEV) {
11087 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
11088 			rc = 0;
11089 		}
11090 		return rc;
11091 	}
11092 
11093 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
11094 	link_info->phy_link_status = resp->link;
11095 	link_info->duplex = resp->duplex_cfg;
11096 	if (bp->hwrm_spec_code >= 0x10800)
11097 		link_info->duplex = resp->duplex_state;
11098 	link_info->pause = resp->pause;
11099 	link_info->auto_mode = resp->auto_mode;
11100 	link_info->auto_pause_setting = resp->auto_pause;
11101 	link_info->lp_pause = resp->link_partner_adv_pause;
11102 	link_info->force_pause_setting = resp->force_pause;
11103 	link_info->duplex_setting = resp->duplex_cfg;
11104 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
11105 		link_info->link_speed = le16_to_cpu(resp->link_speed);
11106 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
11107 			link_info->active_lanes = resp->active_lanes;
11108 	} else {
11109 		link_info->link_speed = 0;
11110 		link_info->active_lanes = 0;
11111 	}
11112 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
11113 	link_info->force_pam4_link_speed =
11114 		le16_to_cpu(resp->force_pam4_link_speed);
11115 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
11116 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
11117 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
11118 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
11119 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
11120 	link_info->auto_pam4_link_speeds =
11121 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
11122 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
11123 	link_info->lp_auto_link_speeds =
11124 		le16_to_cpu(resp->link_partner_adv_speeds);
11125 	link_info->lp_auto_pam4_link_speeds =
11126 		resp->link_partner_pam4_adv_speeds;
11127 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
11128 	link_info->phy_ver[0] = resp->phy_maj;
11129 	link_info->phy_ver[1] = resp->phy_min;
11130 	link_info->phy_ver[2] = resp->phy_bld;
11131 	link_info->media_type = resp->media_type;
11132 	link_info->phy_type = resp->phy_type;
11133 	link_info->transceiver = resp->xcvr_pkg_type;
11134 	link_info->phy_addr = resp->eee_config_phy_addr &
11135 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
11136 	link_info->module_status = resp->module_status;
11137 
11138 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
11139 		struct ethtool_keee *eee = &bp->eee;
11140 		u16 fw_speeds;
11141 
11142 		eee->eee_active = 0;
11143 		if (resp->eee_config_phy_addr &
11144 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
11145 			eee->eee_active = 1;
11146 			fw_speeds = le16_to_cpu(
11147 				resp->link_partner_adv_eee_link_speed_mask);
11148 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
11149 		}
11150 
11151 		/* Pull initial EEE config */
11152 		if (!chng_link_state) {
11153 			if (resp->eee_config_phy_addr &
11154 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
11155 				eee->eee_enabled = 1;
11156 
11157 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
11158 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
11159 
11160 			if (resp->eee_config_phy_addr &
11161 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
11162 				__le32 tmr;
11163 
11164 				eee->tx_lpi_enabled = 1;
11165 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
11166 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
11167 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
11168 			}
11169 		}
11170 	}
11171 
11172 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
11173 	if (bp->hwrm_spec_code >= 0x10504) {
11174 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
11175 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
11176 	}
11177 	/* TODO: need to add more logic to report VF link */
11178 	if (chng_link_state) {
11179 		if (link_info->phy_link_status == BNXT_LINK_LINK)
11180 			link_info->link_state = BNXT_LINK_STATE_UP;
11181 		else
11182 			link_info->link_state = BNXT_LINK_STATE_DOWN;
11183 		if (link_state != link_info->link_state)
11184 			bnxt_report_link(bp);
11185 	} else {
11186 		/* always link down if not require to update link state */
11187 		link_info->link_state = BNXT_LINK_STATE_DOWN;
11188 	}
11189 	hwrm_req_drop(bp, req);
11190 
11191 	if (!BNXT_PHY_CFG_ABLE(bp))
11192 		return 0;
11193 
11194 	support_changed = bnxt_support_speed_dropped(link_info);
11195 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
11196 		bnxt_hwrm_set_link_setting(bp, true, false);
11197 	return 0;
11198 }
11199 
11200 static void bnxt_get_port_module_status(struct bnxt *bp)
11201 {
11202 	struct bnxt_link_info *link_info = &bp->link_info;
11203 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
11204 	u8 module_status;
11205 
11206 	if (bnxt_update_link(bp, true))
11207 		return;
11208 
11209 	module_status = link_info->module_status;
11210 	switch (module_status) {
11211 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
11212 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
11213 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
11214 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
11215 			    bp->pf.port_id);
11216 		if (bp->hwrm_spec_code >= 0x10201) {
11217 			netdev_warn(bp->dev, "Module part number %s\n",
11218 				    resp->phy_vendor_partnumber);
11219 		}
11220 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
11221 			netdev_warn(bp->dev, "TX is disabled\n");
11222 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
11223 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
11224 	}
11225 }
11226 
11227 static void
11228 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11229 {
11230 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
11231 		if (bp->hwrm_spec_code >= 0x10201)
11232 			req->auto_pause =
11233 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
11234 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11235 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
11236 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11237 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
11238 		req->enables |=
11239 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11240 	} else {
11241 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11242 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
11243 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11244 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
11245 		req->enables |=
11246 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
11247 		if (bp->hwrm_spec_code >= 0x10201) {
11248 			req->auto_pause = req->force_pause;
11249 			req->enables |= cpu_to_le32(
11250 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11251 		}
11252 	}
11253 }
11254 
11255 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11256 {
11257 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
11258 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
11259 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11260 			req->enables |=
11261 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
11262 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
11263 		} else if (bp->link_info.advertising) {
11264 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
11265 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
11266 		}
11267 		if (bp->link_info.advertising_pam4) {
11268 			req->enables |=
11269 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
11270 			req->auto_link_pam4_speed_mask =
11271 				cpu_to_le16(bp->link_info.advertising_pam4);
11272 		}
11273 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
11274 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
11275 	} else {
11276 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
11277 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11278 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
11279 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
11280 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
11281 				   (u32)bp->link_info.req_link_speed);
11282 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
11283 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11284 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
11285 		} else {
11286 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11287 		}
11288 	}
11289 
11290 	/* tell chimp that the setting takes effect immediately */
11291 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
11292 }
11293 
11294 int bnxt_hwrm_set_pause(struct bnxt *bp)
11295 {
11296 	struct hwrm_port_phy_cfg_input *req;
11297 	int rc;
11298 
11299 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11300 	if (rc)
11301 		return rc;
11302 
11303 	bnxt_hwrm_set_pause_common(bp, req);
11304 
11305 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
11306 	    bp->link_info.force_link_chng)
11307 		bnxt_hwrm_set_link_common(bp, req);
11308 
11309 	rc = hwrm_req_send(bp, req);
11310 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
11311 		/* since changing of pause setting doesn't trigger any link
11312 		 * change event, the driver needs to update the current pause
11313 		 * result upon successfully return of the phy_cfg command
11314 		 */
11315 		bp->link_info.pause =
11316 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
11317 		bp->link_info.auto_pause_setting = 0;
11318 		if (!bp->link_info.force_link_chng)
11319 			bnxt_report_link(bp);
11320 	}
11321 	bp->link_info.force_link_chng = false;
11322 	return rc;
11323 }
11324 
11325 static void bnxt_hwrm_set_eee(struct bnxt *bp,
11326 			      struct hwrm_port_phy_cfg_input *req)
11327 {
11328 	struct ethtool_keee *eee = &bp->eee;
11329 
11330 	if (eee->eee_enabled) {
11331 		u16 eee_speeds;
11332 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
11333 
11334 		if (eee->tx_lpi_enabled)
11335 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
11336 		else
11337 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
11338 
11339 		req->flags |= cpu_to_le32(flags);
11340 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
11341 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
11342 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
11343 	} else {
11344 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
11345 	}
11346 }
11347 
11348 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
11349 {
11350 	struct hwrm_port_phy_cfg_input *req;
11351 	int rc;
11352 
11353 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11354 	if (rc)
11355 		return rc;
11356 
11357 	if (set_pause)
11358 		bnxt_hwrm_set_pause_common(bp, req);
11359 
11360 	bnxt_hwrm_set_link_common(bp, req);
11361 
11362 	if (set_eee)
11363 		bnxt_hwrm_set_eee(bp, req);
11364 	return hwrm_req_send(bp, req);
11365 }
11366 
11367 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
11368 {
11369 	struct hwrm_port_phy_cfg_input *req;
11370 	int rc;
11371 
11372 	if (!BNXT_SINGLE_PF(bp))
11373 		return 0;
11374 
11375 	if (pci_num_vf(bp->pdev) &&
11376 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
11377 		return 0;
11378 
11379 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11380 	if (rc)
11381 		return rc;
11382 
11383 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
11384 	rc = hwrm_req_send(bp, req);
11385 	if (!rc) {
11386 		mutex_lock(&bp->link_lock);
11387 		/* Device is not obliged link down in certain scenarios, even
11388 		 * when forced. Setting the state unknown is consistent with
11389 		 * driver startup and will force link state to be reported
11390 		 * during subsequent open based on PORT_PHY_QCFG.
11391 		 */
11392 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
11393 		mutex_unlock(&bp->link_lock);
11394 	}
11395 	return rc;
11396 }
11397 
11398 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
11399 {
11400 #ifdef CONFIG_TEE_BNXT_FW
11401 	int rc = tee_bnxt_fw_load();
11402 
11403 	if (rc)
11404 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
11405 
11406 	return rc;
11407 #else
11408 	netdev_err(bp->dev, "OP-TEE not supported\n");
11409 	return -ENODEV;
11410 #endif
11411 }
11412 
11413 static int bnxt_try_recover_fw(struct bnxt *bp)
11414 {
11415 	if (bp->fw_health && bp->fw_health->status_reliable) {
11416 		int retry = 0, rc;
11417 		u32 sts;
11418 
11419 		do {
11420 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
11421 			rc = bnxt_hwrm_poll(bp);
11422 			if (!BNXT_FW_IS_BOOTING(sts) &&
11423 			    !BNXT_FW_IS_RECOVERING(sts))
11424 				break;
11425 			retry++;
11426 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
11427 
11428 		if (!BNXT_FW_IS_HEALTHY(sts)) {
11429 			netdev_err(bp->dev,
11430 				   "Firmware not responding, status: 0x%x\n",
11431 				   sts);
11432 			rc = -ENODEV;
11433 		}
11434 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
11435 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
11436 			return bnxt_fw_reset_via_optee(bp);
11437 		}
11438 		return rc;
11439 	}
11440 
11441 	return -ENODEV;
11442 }
11443 
11444 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
11445 {
11446 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11447 
11448 	if (!BNXT_NEW_RM(bp))
11449 		return; /* no resource reservations required */
11450 
11451 	hw_resc->resv_cp_rings = 0;
11452 	hw_resc->resv_stat_ctxs = 0;
11453 	hw_resc->resv_irqs = 0;
11454 	hw_resc->resv_tx_rings = 0;
11455 	hw_resc->resv_rx_rings = 0;
11456 	hw_resc->resv_hw_ring_grps = 0;
11457 	hw_resc->resv_vnics = 0;
11458 	hw_resc->resv_rsscos_ctxs = 0;
11459 	if (!fw_reset) {
11460 		bp->tx_nr_rings = 0;
11461 		bp->rx_nr_rings = 0;
11462 	}
11463 }
11464 
11465 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
11466 {
11467 	int rc;
11468 
11469 	if (!BNXT_NEW_RM(bp))
11470 		return 0; /* no resource reservations required */
11471 
11472 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
11473 	if (rc)
11474 		netdev_err(bp->dev, "resc_qcaps failed\n");
11475 
11476 	bnxt_clear_reservations(bp, fw_reset);
11477 
11478 	return rc;
11479 }
11480 
11481 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
11482 {
11483 	struct hwrm_func_drv_if_change_output *resp;
11484 	struct hwrm_func_drv_if_change_input *req;
11485 	bool fw_reset = !bp->irq_tbl;
11486 	bool resc_reinit = false;
11487 	int rc, retry = 0;
11488 	u32 flags = 0;
11489 
11490 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
11491 		return 0;
11492 
11493 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
11494 	if (rc)
11495 		return rc;
11496 
11497 	if (up)
11498 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
11499 	resp = hwrm_req_hold(bp, req);
11500 
11501 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
11502 	while (retry < BNXT_FW_IF_RETRY) {
11503 		rc = hwrm_req_send(bp, req);
11504 		if (rc != -EAGAIN)
11505 			break;
11506 
11507 		msleep(50);
11508 		retry++;
11509 	}
11510 
11511 	if (rc == -EAGAIN) {
11512 		hwrm_req_drop(bp, req);
11513 		return rc;
11514 	} else if (!rc) {
11515 		flags = le32_to_cpu(resp->flags);
11516 	} else if (up) {
11517 		rc = bnxt_try_recover_fw(bp);
11518 		fw_reset = true;
11519 	}
11520 	hwrm_req_drop(bp, req);
11521 	if (rc)
11522 		return rc;
11523 
11524 	if (!up) {
11525 		bnxt_inv_fw_health_reg(bp);
11526 		return 0;
11527 	}
11528 
11529 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
11530 		resc_reinit = true;
11531 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
11532 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
11533 		fw_reset = true;
11534 	else
11535 		bnxt_remap_fw_health_regs(bp);
11536 
11537 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
11538 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
11539 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
11540 		return -ENODEV;
11541 	}
11542 	if (resc_reinit || fw_reset) {
11543 		if (fw_reset) {
11544 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11545 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11546 				bnxt_ulp_stop(bp);
11547 			bnxt_free_ctx_mem(bp);
11548 			bnxt_dcb_free(bp);
11549 			rc = bnxt_fw_init_one(bp);
11550 			if (rc) {
11551 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11552 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
11553 				return rc;
11554 			}
11555 			bnxt_clear_int_mode(bp);
11556 			rc = bnxt_init_int_mode(bp);
11557 			if (rc) {
11558 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11559 				netdev_err(bp->dev, "init int mode failed\n");
11560 				return rc;
11561 			}
11562 		}
11563 		rc = bnxt_cancel_reservations(bp, fw_reset);
11564 	}
11565 	return rc;
11566 }
11567 
11568 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
11569 {
11570 	struct hwrm_port_led_qcaps_output *resp;
11571 	struct hwrm_port_led_qcaps_input *req;
11572 	struct bnxt_pf_info *pf = &bp->pf;
11573 	int rc;
11574 
11575 	bp->num_leds = 0;
11576 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
11577 		return 0;
11578 
11579 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
11580 	if (rc)
11581 		return rc;
11582 
11583 	req->port_id = cpu_to_le16(pf->port_id);
11584 	resp = hwrm_req_hold(bp, req);
11585 	rc = hwrm_req_send(bp, req);
11586 	if (rc) {
11587 		hwrm_req_drop(bp, req);
11588 		return rc;
11589 	}
11590 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
11591 		int i;
11592 
11593 		bp->num_leds = resp->num_leds;
11594 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
11595 						 bp->num_leds);
11596 		for (i = 0; i < bp->num_leds; i++) {
11597 			struct bnxt_led_info *led = &bp->leds[i];
11598 			__le16 caps = led->led_state_caps;
11599 
11600 			if (!led->led_group_id ||
11601 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
11602 				bp->num_leds = 0;
11603 				break;
11604 			}
11605 		}
11606 	}
11607 	hwrm_req_drop(bp, req);
11608 	return 0;
11609 }
11610 
11611 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
11612 {
11613 	struct hwrm_wol_filter_alloc_output *resp;
11614 	struct hwrm_wol_filter_alloc_input *req;
11615 	int rc;
11616 
11617 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
11618 	if (rc)
11619 		return rc;
11620 
11621 	req->port_id = cpu_to_le16(bp->pf.port_id);
11622 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
11623 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
11624 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
11625 
11626 	resp = hwrm_req_hold(bp, req);
11627 	rc = hwrm_req_send(bp, req);
11628 	if (!rc)
11629 		bp->wol_filter_id = resp->wol_filter_id;
11630 	hwrm_req_drop(bp, req);
11631 	return rc;
11632 }
11633 
11634 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
11635 {
11636 	struct hwrm_wol_filter_free_input *req;
11637 	int rc;
11638 
11639 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
11640 	if (rc)
11641 		return rc;
11642 
11643 	req->port_id = cpu_to_le16(bp->pf.port_id);
11644 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
11645 	req->wol_filter_id = bp->wol_filter_id;
11646 
11647 	return hwrm_req_send(bp, req);
11648 }
11649 
11650 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
11651 {
11652 	struct hwrm_wol_filter_qcfg_output *resp;
11653 	struct hwrm_wol_filter_qcfg_input *req;
11654 	u16 next_handle = 0;
11655 	int rc;
11656 
11657 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
11658 	if (rc)
11659 		return rc;
11660 
11661 	req->port_id = cpu_to_le16(bp->pf.port_id);
11662 	req->handle = cpu_to_le16(handle);
11663 	resp = hwrm_req_hold(bp, req);
11664 	rc = hwrm_req_send(bp, req);
11665 	if (!rc) {
11666 		next_handle = le16_to_cpu(resp->next_handle);
11667 		if (next_handle != 0) {
11668 			if (resp->wol_type ==
11669 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
11670 				bp->wol = 1;
11671 				bp->wol_filter_id = resp->wol_filter_id;
11672 			}
11673 		}
11674 	}
11675 	hwrm_req_drop(bp, req);
11676 	return next_handle;
11677 }
11678 
11679 static void bnxt_get_wol_settings(struct bnxt *bp)
11680 {
11681 	u16 handle = 0;
11682 
11683 	bp->wol = 0;
11684 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
11685 		return;
11686 
11687 	do {
11688 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
11689 	} while (handle && handle != 0xffff);
11690 }
11691 
11692 static bool bnxt_eee_config_ok(struct bnxt *bp)
11693 {
11694 	struct ethtool_keee *eee = &bp->eee;
11695 	struct bnxt_link_info *link_info = &bp->link_info;
11696 
11697 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
11698 		return true;
11699 
11700 	if (eee->eee_enabled) {
11701 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
11702 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
11703 
11704 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
11705 
11706 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
11707 			eee->eee_enabled = 0;
11708 			return false;
11709 		}
11710 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
11711 			linkmode_and(eee->advertised, advertising,
11712 				     eee->supported);
11713 			return false;
11714 		}
11715 	}
11716 	return true;
11717 }
11718 
11719 static int bnxt_update_phy_setting(struct bnxt *bp)
11720 {
11721 	int rc;
11722 	bool update_link = false;
11723 	bool update_pause = false;
11724 	bool update_eee = false;
11725 	struct bnxt_link_info *link_info = &bp->link_info;
11726 
11727 	rc = bnxt_update_link(bp, true);
11728 	if (rc) {
11729 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
11730 			   rc);
11731 		return rc;
11732 	}
11733 	if (!BNXT_SINGLE_PF(bp))
11734 		return 0;
11735 
11736 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
11737 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
11738 	    link_info->req_flow_ctrl)
11739 		update_pause = true;
11740 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
11741 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
11742 		update_pause = true;
11743 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
11744 		if (BNXT_AUTO_MODE(link_info->auto_mode))
11745 			update_link = true;
11746 		if (bnxt_force_speed_updated(link_info))
11747 			update_link = true;
11748 		if (link_info->req_duplex != link_info->duplex_setting)
11749 			update_link = true;
11750 	} else {
11751 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
11752 			update_link = true;
11753 		if (bnxt_auto_speed_updated(link_info))
11754 			update_link = true;
11755 	}
11756 
11757 	/* The last close may have shutdown the link, so need to call
11758 	 * PHY_CFG to bring it back up.
11759 	 */
11760 	if (!BNXT_LINK_IS_UP(bp))
11761 		update_link = true;
11762 
11763 	if (!bnxt_eee_config_ok(bp))
11764 		update_eee = true;
11765 
11766 	if (update_link)
11767 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
11768 	else if (update_pause)
11769 		rc = bnxt_hwrm_set_pause(bp);
11770 	if (rc) {
11771 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
11772 			   rc);
11773 		return rc;
11774 	}
11775 
11776 	return rc;
11777 }
11778 
11779 /* Common routine to pre-map certain register block to different GRC window.
11780  * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
11781  * in PF and 3 windows in VF that can be customized to map in different
11782  * register blocks.
11783  */
11784 static void bnxt_preset_reg_win(struct bnxt *bp)
11785 {
11786 	if (BNXT_PF(bp)) {
11787 		/* CAG registers map to GRC window #4 */
11788 		writel(BNXT_CAG_REG_BASE,
11789 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
11790 	}
11791 }
11792 
11793 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
11794 
11795 static int bnxt_reinit_after_abort(struct bnxt *bp)
11796 {
11797 	int rc;
11798 
11799 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11800 		return -EBUSY;
11801 
11802 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
11803 		return -ENODEV;
11804 
11805 	rc = bnxt_fw_init_one(bp);
11806 	if (!rc) {
11807 		bnxt_clear_int_mode(bp);
11808 		rc = bnxt_init_int_mode(bp);
11809 		if (!rc) {
11810 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
11811 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11812 		}
11813 	}
11814 	return rc;
11815 }
11816 
11817 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
11818 {
11819 	struct bnxt_ntuple_filter *ntp_fltr;
11820 	struct bnxt_l2_filter *l2_fltr;
11821 
11822 	if (list_empty(&fltr->list))
11823 		return;
11824 
11825 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
11826 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
11827 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
11828 		atomic_inc(&l2_fltr->refcnt);
11829 		ntp_fltr->l2_fltr = l2_fltr;
11830 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
11831 			bnxt_del_ntp_filter(bp, ntp_fltr);
11832 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
11833 				   fltr->sw_id);
11834 		}
11835 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
11836 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
11837 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
11838 			bnxt_del_l2_filter(bp, l2_fltr);
11839 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
11840 				   fltr->sw_id);
11841 		}
11842 	}
11843 }
11844 
11845 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
11846 {
11847 	struct bnxt_filter_base *usr_fltr, *tmp;
11848 
11849 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
11850 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
11851 }
11852 
11853 static int bnxt_set_xps_mapping(struct bnxt *bp)
11854 {
11855 	int numa_node = dev_to_node(&bp->pdev->dev);
11856 	unsigned int q_idx, map_idx, cpu, i;
11857 	const struct cpumask *cpu_mask_ptr;
11858 	int nr_cpus = num_online_cpus();
11859 	cpumask_t *q_map;
11860 	int rc = 0;
11861 
11862 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
11863 	if (!q_map)
11864 		return -ENOMEM;
11865 
11866 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
11867 	 * Each TC has the same number of TX queues. The nth TX queue for each
11868 	 * TC will have the same CPU mask.
11869 	 */
11870 	for (i = 0; i < nr_cpus; i++) {
11871 		map_idx = i % bp->tx_nr_rings_per_tc;
11872 		cpu = cpumask_local_spread(i, numa_node);
11873 		cpu_mask_ptr = get_cpu_mask(cpu);
11874 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
11875 	}
11876 
11877 	/* Register CPU mask for each TX queue except the ones marked for XDP */
11878 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
11879 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
11880 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
11881 		if (rc) {
11882 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
11883 				    q_idx);
11884 			break;
11885 		}
11886 	}
11887 
11888 	kfree(q_map);
11889 
11890 	return rc;
11891 }
11892 
11893 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
11894 {
11895 	int rc = 0;
11896 
11897 	bnxt_preset_reg_win(bp);
11898 	netif_carrier_off(bp->dev);
11899 	if (irq_re_init) {
11900 		/* Reserve rings now if none were reserved at driver probe. */
11901 		rc = bnxt_init_dflt_ring_mode(bp);
11902 		if (rc) {
11903 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
11904 			return rc;
11905 		}
11906 	}
11907 	rc = bnxt_reserve_rings(bp, irq_re_init);
11908 	if (rc)
11909 		return rc;
11910 	if ((bp->flags & BNXT_FLAG_RFS) &&
11911 	    !(bp->flags & BNXT_FLAG_USING_MSIX)) {
11912 		/* disable RFS if falling back to INTA */
11913 		bp->dev->hw_features &= ~NETIF_F_NTUPLE;
11914 		bp->flags &= ~BNXT_FLAG_RFS;
11915 	}
11916 
11917 	rc = bnxt_alloc_mem(bp, irq_re_init);
11918 	if (rc) {
11919 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
11920 		goto open_err_free_mem;
11921 	}
11922 
11923 	if (irq_re_init) {
11924 		bnxt_init_napi(bp);
11925 		rc = bnxt_request_irq(bp);
11926 		if (rc) {
11927 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
11928 			goto open_err_irq;
11929 		}
11930 	}
11931 
11932 	rc = bnxt_init_nic(bp, irq_re_init);
11933 	if (rc) {
11934 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
11935 		goto open_err_irq;
11936 	}
11937 
11938 	bnxt_enable_napi(bp);
11939 	bnxt_debug_dev_init(bp);
11940 
11941 	if (link_re_init) {
11942 		mutex_lock(&bp->link_lock);
11943 		rc = bnxt_update_phy_setting(bp);
11944 		mutex_unlock(&bp->link_lock);
11945 		if (rc) {
11946 			netdev_warn(bp->dev, "failed to update phy settings\n");
11947 			if (BNXT_SINGLE_PF(bp)) {
11948 				bp->link_info.phy_retry = true;
11949 				bp->link_info.phy_retry_expires =
11950 					jiffies + 5 * HZ;
11951 			}
11952 		}
11953 	}
11954 
11955 	if (irq_re_init) {
11956 		udp_tunnel_nic_reset_ntf(bp->dev);
11957 		rc = bnxt_set_xps_mapping(bp);
11958 		if (rc)
11959 			netdev_warn(bp->dev, "failed to set xps mapping\n");
11960 	}
11961 
11962 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
11963 		if (!static_key_enabled(&bnxt_xdp_locking_key))
11964 			static_branch_enable(&bnxt_xdp_locking_key);
11965 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
11966 		static_branch_disable(&bnxt_xdp_locking_key);
11967 	}
11968 	set_bit(BNXT_STATE_OPEN, &bp->state);
11969 	bnxt_enable_int(bp);
11970 	/* Enable TX queues */
11971 	bnxt_tx_enable(bp);
11972 	mod_timer(&bp->timer, jiffies + bp->current_interval);
11973 	/* Poll link status and check for SFP+ module status */
11974 	mutex_lock(&bp->link_lock);
11975 	bnxt_get_port_module_status(bp);
11976 	mutex_unlock(&bp->link_lock);
11977 
11978 	/* VF-reps may need to be re-opened after the PF is re-opened */
11979 	if (BNXT_PF(bp))
11980 		bnxt_vf_reps_open(bp);
11981 	bnxt_ptp_init_rtc(bp, true);
11982 	bnxt_ptp_cfg_tstamp_filters(bp);
11983 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
11984 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
11985 	bnxt_cfg_usr_fltrs(bp);
11986 	return 0;
11987 
11988 open_err_irq:
11989 	bnxt_del_napi(bp);
11990 
11991 open_err_free_mem:
11992 	bnxt_free_skbs(bp);
11993 	bnxt_free_irq(bp);
11994 	bnxt_free_mem(bp, true);
11995 	return rc;
11996 }
11997 
11998 /* rtnl_lock held */
11999 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12000 {
12001 	int rc = 0;
12002 
12003 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12004 		rc = -EIO;
12005 	if (!rc)
12006 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12007 	if (rc) {
12008 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12009 		dev_close(bp->dev);
12010 	}
12011 	return rc;
12012 }
12013 
12014 /* rtnl_lock held, open the NIC half way by allocating all resources, but
12015  * NAPI, IRQ, and TX are not enabled.  This is mainly used for offline
12016  * self tests.
12017  */
12018 int bnxt_half_open_nic(struct bnxt *bp)
12019 {
12020 	int rc = 0;
12021 
12022 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12023 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
12024 		rc = -ENODEV;
12025 		goto half_open_err;
12026 	}
12027 
12028 	rc = bnxt_alloc_mem(bp, true);
12029 	if (rc) {
12030 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12031 		goto half_open_err;
12032 	}
12033 	bnxt_init_napi(bp);
12034 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12035 	rc = bnxt_init_nic(bp, true);
12036 	if (rc) {
12037 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12038 		bnxt_del_napi(bp);
12039 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12040 		goto half_open_err;
12041 	}
12042 	return 0;
12043 
12044 half_open_err:
12045 	bnxt_free_skbs(bp);
12046 	bnxt_free_mem(bp, true);
12047 	dev_close(bp->dev);
12048 	return rc;
12049 }
12050 
12051 /* rtnl_lock held, this call can only be made after a previous successful
12052  * call to bnxt_half_open_nic().
12053  */
12054 void bnxt_half_close_nic(struct bnxt *bp)
12055 {
12056 	bnxt_hwrm_resource_free(bp, false, true);
12057 	bnxt_del_napi(bp);
12058 	bnxt_free_skbs(bp);
12059 	bnxt_free_mem(bp, true);
12060 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12061 }
12062 
12063 void bnxt_reenable_sriov(struct bnxt *bp)
12064 {
12065 	if (BNXT_PF(bp)) {
12066 		struct bnxt_pf_info *pf = &bp->pf;
12067 		int n = pf->active_vfs;
12068 
12069 		if (n)
12070 			bnxt_cfg_hw_sriov(bp, &n, true);
12071 	}
12072 }
12073 
12074 static int bnxt_open(struct net_device *dev)
12075 {
12076 	struct bnxt *bp = netdev_priv(dev);
12077 	int rc;
12078 
12079 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12080 		rc = bnxt_reinit_after_abort(bp);
12081 		if (rc) {
12082 			if (rc == -EBUSY)
12083 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
12084 			else
12085 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
12086 			return -ENODEV;
12087 		}
12088 	}
12089 
12090 	rc = bnxt_hwrm_if_change(bp, true);
12091 	if (rc)
12092 		return rc;
12093 
12094 	rc = __bnxt_open_nic(bp, true, true);
12095 	if (rc) {
12096 		bnxt_hwrm_if_change(bp, false);
12097 	} else {
12098 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
12099 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12100 				bnxt_ulp_start(bp, 0);
12101 				bnxt_reenable_sriov(bp);
12102 			}
12103 		}
12104 	}
12105 
12106 	return rc;
12107 }
12108 
12109 static bool bnxt_drv_busy(struct bnxt *bp)
12110 {
12111 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
12112 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
12113 }
12114 
12115 static void bnxt_get_ring_stats(struct bnxt *bp,
12116 				struct rtnl_link_stats64 *stats);
12117 
12118 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
12119 			     bool link_re_init)
12120 {
12121 	/* Close the VF-reps before closing PF */
12122 	if (BNXT_PF(bp))
12123 		bnxt_vf_reps_close(bp);
12124 
12125 	/* Change device state to avoid TX queue wake up's */
12126 	bnxt_tx_disable(bp);
12127 
12128 	clear_bit(BNXT_STATE_OPEN, &bp->state);
12129 	smp_mb__after_atomic();
12130 	while (bnxt_drv_busy(bp))
12131 		msleep(20);
12132 
12133 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12134 		bnxt_clear_rss_ctxs(bp, false);
12135 	/* Flush rings and disable interrupts */
12136 	bnxt_shutdown_nic(bp, irq_re_init);
12137 
12138 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
12139 
12140 	bnxt_debug_dev_exit(bp);
12141 	bnxt_disable_napi(bp);
12142 	del_timer_sync(&bp->timer);
12143 	bnxt_free_skbs(bp);
12144 
12145 	/* Save ring stats before shutdown */
12146 	if (bp->bnapi && irq_re_init) {
12147 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
12148 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
12149 	}
12150 	if (irq_re_init) {
12151 		bnxt_free_irq(bp);
12152 		bnxt_del_napi(bp);
12153 	}
12154 	bnxt_free_mem(bp, irq_re_init);
12155 }
12156 
12157 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12158 {
12159 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12160 		/* If we get here, it means firmware reset is in progress
12161 		 * while we are trying to close.  We can safely proceed with
12162 		 * the close because we are holding rtnl_lock().  Some firmware
12163 		 * messages may fail as we proceed to close.  We set the
12164 		 * ABORT_ERR flag here so that the FW reset thread will later
12165 		 * abort when it gets the rtnl_lock() and sees the flag.
12166 		 */
12167 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
12168 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12169 	}
12170 
12171 #ifdef CONFIG_BNXT_SRIOV
12172 	if (bp->sriov_cfg) {
12173 		int rc;
12174 
12175 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
12176 						      !bp->sriov_cfg,
12177 						      BNXT_SRIOV_CFG_WAIT_TMO);
12178 		if (!rc)
12179 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
12180 		else if (rc < 0)
12181 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
12182 	}
12183 #endif
12184 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
12185 }
12186 
12187 static int bnxt_close(struct net_device *dev)
12188 {
12189 	struct bnxt *bp = netdev_priv(dev);
12190 
12191 	bnxt_close_nic(bp, true, true);
12192 	bnxt_hwrm_shutdown_link(bp);
12193 	bnxt_hwrm_if_change(bp, false);
12194 	return 0;
12195 }
12196 
12197 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
12198 				   u16 *val)
12199 {
12200 	struct hwrm_port_phy_mdio_read_output *resp;
12201 	struct hwrm_port_phy_mdio_read_input *req;
12202 	int rc;
12203 
12204 	if (bp->hwrm_spec_code < 0x10a00)
12205 		return -EOPNOTSUPP;
12206 
12207 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
12208 	if (rc)
12209 		return rc;
12210 
12211 	req->port_id = cpu_to_le16(bp->pf.port_id);
12212 	req->phy_addr = phy_addr;
12213 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12214 	if (mdio_phy_id_is_c45(phy_addr)) {
12215 		req->cl45_mdio = 1;
12216 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12217 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12218 		req->reg_addr = cpu_to_le16(reg);
12219 	}
12220 
12221 	resp = hwrm_req_hold(bp, req);
12222 	rc = hwrm_req_send(bp, req);
12223 	if (!rc)
12224 		*val = le16_to_cpu(resp->reg_data);
12225 	hwrm_req_drop(bp, req);
12226 	return rc;
12227 }
12228 
12229 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
12230 				    u16 val)
12231 {
12232 	struct hwrm_port_phy_mdio_write_input *req;
12233 	int rc;
12234 
12235 	if (bp->hwrm_spec_code < 0x10a00)
12236 		return -EOPNOTSUPP;
12237 
12238 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
12239 	if (rc)
12240 		return rc;
12241 
12242 	req->port_id = cpu_to_le16(bp->pf.port_id);
12243 	req->phy_addr = phy_addr;
12244 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12245 	if (mdio_phy_id_is_c45(phy_addr)) {
12246 		req->cl45_mdio = 1;
12247 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12248 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12249 		req->reg_addr = cpu_to_le16(reg);
12250 	}
12251 	req->reg_data = cpu_to_le16(val);
12252 
12253 	return hwrm_req_send(bp, req);
12254 }
12255 
12256 /* rtnl_lock held */
12257 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12258 {
12259 	struct mii_ioctl_data *mdio = if_mii(ifr);
12260 	struct bnxt *bp = netdev_priv(dev);
12261 	int rc;
12262 
12263 	switch (cmd) {
12264 	case SIOCGMIIPHY:
12265 		mdio->phy_id = bp->link_info.phy_addr;
12266 
12267 		fallthrough;
12268 	case SIOCGMIIREG: {
12269 		u16 mii_regval = 0;
12270 
12271 		if (!netif_running(dev))
12272 			return -EAGAIN;
12273 
12274 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
12275 					     &mii_regval);
12276 		mdio->val_out = mii_regval;
12277 		return rc;
12278 	}
12279 
12280 	case SIOCSMIIREG:
12281 		if (!netif_running(dev))
12282 			return -EAGAIN;
12283 
12284 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
12285 						mdio->val_in);
12286 
12287 	case SIOCSHWTSTAMP:
12288 		return bnxt_hwtstamp_set(dev, ifr);
12289 
12290 	case SIOCGHWTSTAMP:
12291 		return bnxt_hwtstamp_get(dev, ifr);
12292 
12293 	default:
12294 		/* do nothing */
12295 		break;
12296 	}
12297 	return -EOPNOTSUPP;
12298 }
12299 
12300 static void bnxt_get_ring_stats(struct bnxt *bp,
12301 				struct rtnl_link_stats64 *stats)
12302 {
12303 	int i;
12304 
12305 	for (i = 0; i < bp->cp_nr_rings; i++) {
12306 		struct bnxt_napi *bnapi = bp->bnapi[i];
12307 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
12308 		u64 *sw = cpr->stats.sw_stats;
12309 
12310 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
12311 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12312 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
12313 
12314 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
12315 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
12316 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
12317 
12318 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
12319 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
12320 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
12321 
12322 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
12323 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
12324 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
12325 
12326 		stats->rx_missed_errors +=
12327 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
12328 
12329 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12330 
12331 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
12332 
12333 		stats->rx_dropped +=
12334 			cpr->sw_stats.rx.rx_netpoll_discards +
12335 			cpr->sw_stats.rx.rx_oom_discards;
12336 	}
12337 }
12338 
12339 static void bnxt_add_prev_stats(struct bnxt *bp,
12340 				struct rtnl_link_stats64 *stats)
12341 {
12342 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
12343 
12344 	stats->rx_packets += prev_stats->rx_packets;
12345 	stats->tx_packets += prev_stats->tx_packets;
12346 	stats->rx_bytes += prev_stats->rx_bytes;
12347 	stats->tx_bytes += prev_stats->tx_bytes;
12348 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
12349 	stats->multicast += prev_stats->multicast;
12350 	stats->rx_dropped += prev_stats->rx_dropped;
12351 	stats->tx_dropped += prev_stats->tx_dropped;
12352 }
12353 
12354 static void
12355 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
12356 {
12357 	struct bnxt *bp = netdev_priv(dev);
12358 
12359 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
12360 	/* Make sure bnxt_close_nic() sees that we are reading stats before
12361 	 * we check the BNXT_STATE_OPEN flag.
12362 	 */
12363 	smp_mb__after_atomic();
12364 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12365 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12366 		*stats = bp->net_stats_prev;
12367 		return;
12368 	}
12369 
12370 	bnxt_get_ring_stats(bp, stats);
12371 	bnxt_add_prev_stats(bp, stats);
12372 
12373 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
12374 		u64 *rx = bp->port_stats.sw_stats;
12375 		u64 *tx = bp->port_stats.sw_stats +
12376 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
12377 
12378 		stats->rx_crc_errors =
12379 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
12380 		stats->rx_frame_errors =
12381 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
12382 		stats->rx_length_errors =
12383 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
12384 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
12385 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
12386 		stats->rx_errors =
12387 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
12388 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
12389 		stats->collisions =
12390 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
12391 		stats->tx_fifo_errors =
12392 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
12393 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
12394 	}
12395 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12396 }
12397 
12398 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
12399 					struct bnxt_total_ring_err_stats *stats,
12400 					struct bnxt_cp_ring_info *cpr)
12401 {
12402 	struct bnxt_sw_stats *sw_stats = &cpr->sw_stats;
12403 	u64 *hw_stats = cpr->stats.sw_stats;
12404 
12405 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
12406 	stats->rx_total_resets += sw_stats->rx.rx_resets;
12407 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
12408 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
12409 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
12410 	stats->rx_total_ring_discards +=
12411 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
12412 	stats->tx_total_resets += sw_stats->tx.tx_resets;
12413 	stats->tx_total_ring_discards +=
12414 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
12415 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
12416 }
12417 
12418 void bnxt_get_ring_err_stats(struct bnxt *bp,
12419 			     struct bnxt_total_ring_err_stats *stats)
12420 {
12421 	int i;
12422 
12423 	for (i = 0; i < bp->cp_nr_rings; i++)
12424 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
12425 }
12426 
12427 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
12428 {
12429 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12430 	struct net_device *dev = bp->dev;
12431 	struct netdev_hw_addr *ha;
12432 	u8 *haddr;
12433 	int mc_count = 0;
12434 	bool update = false;
12435 	int off = 0;
12436 
12437 	netdev_for_each_mc_addr(ha, dev) {
12438 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
12439 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12440 			vnic->mc_list_count = 0;
12441 			return false;
12442 		}
12443 		haddr = ha->addr;
12444 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
12445 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
12446 			update = true;
12447 		}
12448 		off += ETH_ALEN;
12449 		mc_count++;
12450 	}
12451 	if (mc_count)
12452 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
12453 
12454 	if (mc_count != vnic->mc_list_count) {
12455 		vnic->mc_list_count = mc_count;
12456 		update = true;
12457 	}
12458 	return update;
12459 }
12460 
12461 static bool bnxt_uc_list_updated(struct bnxt *bp)
12462 {
12463 	struct net_device *dev = bp->dev;
12464 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12465 	struct netdev_hw_addr *ha;
12466 	int off = 0;
12467 
12468 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
12469 		return true;
12470 
12471 	netdev_for_each_uc_addr(ha, dev) {
12472 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
12473 			return true;
12474 
12475 		off += ETH_ALEN;
12476 	}
12477 	return false;
12478 }
12479 
12480 static void bnxt_set_rx_mode(struct net_device *dev)
12481 {
12482 	struct bnxt *bp = netdev_priv(dev);
12483 	struct bnxt_vnic_info *vnic;
12484 	bool mc_update = false;
12485 	bool uc_update;
12486 	u32 mask;
12487 
12488 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
12489 		return;
12490 
12491 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12492 	mask = vnic->rx_mask;
12493 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
12494 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
12495 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
12496 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
12497 
12498 	if (dev->flags & IFF_PROMISC)
12499 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12500 
12501 	uc_update = bnxt_uc_list_updated(bp);
12502 
12503 	if (dev->flags & IFF_BROADCAST)
12504 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
12505 	if (dev->flags & IFF_ALLMULTI) {
12506 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12507 		vnic->mc_list_count = 0;
12508 	} else if (dev->flags & IFF_MULTICAST) {
12509 		mc_update = bnxt_mc_list_updated(bp, &mask);
12510 	}
12511 
12512 	if (mask != vnic->rx_mask || uc_update || mc_update) {
12513 		vnic->rx_mask = mask;
12514 
12515 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
12516 	}
12517 }
12518 
12519 static int bnxt_cfg_rx_mode(struct bnxt *bp)
12520 {
12521 	struct net_device *dev = bp->dev;
12522 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12523 	struct netdev_hw_addr *ha;
12524 	int i, off = 0, rc;
12525 	bool uc_update;
12526 
12527 	netif_addr_lock_bh(dev);
12528 	uc_update = bnxt_uc_list_updated(bp);
12529 	netif_addr_unlock_bh(dev);
12530 
12531 	if (!uc_update)
12532 		goto skip_uc;
12533 
12534 	for (i = 1; i < vnic->uc_filter_count; i++) {
12535 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
12536 
12537 		bnxt_hwrm_l2_filter_free(bp, fltr);
12538 		bnxt_del_l2_filter(bp, fltr);
12539 	}
12540 
12541 	vnic->uc_filter_count = 1;
12542 
12543 	netif_addr_lock_bh(dev);
12544 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
12545 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12546 	} else {
12547 		netdev_for_each_uc_addr(ha, dev) {
12548 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
12549 			off += ETH_ALEN;
12550 			vnic->uc_filter_count++;
12551 		}
12552 	}
12553 	netif_addr_unlock_bh(dev);
12554 
12555 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
12556 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
12557 		if (rc) {
12558 			if (BNXT_VF(bp) && rc == -ENODEV) {
12559 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
12560 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
12561 				else
12562 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
12563 				rc = 0;
12564 			} else {
12565 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
12566 			}
12567 			vnic->uc_filter_count = i;
12568 			return rc;
12569 		}
12570 	}
12571 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
12572 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
12573 
12574 skip_uc:
12575 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
12576 	    !bnxt_promisc_ok(bp))
12577 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12578 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
12579 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
12580 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
12581 			    rc);
12582 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
12583 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12584 		vnic->mc_list_count = 0;
12585 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
12586 	}
12587 	if (rc)
12588 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
12589 			   rc);
12590 
12591 	return rc;
12592 }
12593 
12594 static bool bnxt_can_reserve_rings(struct bnxt *bp)
12595 {
12596 #ifdef CONFIG_BNXT_SRIOV
12597 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
12598 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12599 
12600 		/* No minimum rings were provisioned by the PF.  Don't
12601 		 * reserve rings by default when device is down.
12602 		 */
12603 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
12604 			return true;
12605 
12606 		if (!netif_running(bp->dev))
12607 			return false;
12608 	}
12609 #endif
12610 	return true;
12611 }
12612 
12613 /* If the chip and firmware supports RFS */
12614 static bool bnxt_rfs_supported(struct bnxt *bp)
12615 {
12616 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
12617 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
12618 			return true;
12619 		return false;
12620 	}
12621 	/* 212 firmware is broken for aRFS */
12622 	if (BNXT_FW_MAJ(bp) == 212)
12623 		return false;
12624 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
12625 		return true;
12626 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
12627 		return true;
12628 	return false;
12629 }
12630 
12631 /* If runtime conditions support RFS */
12632 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
12633 {
12634 	struct bnxt_hw_rings hwr = {0};
12635 	int max_vnics, max_rss_ctxs;
12636 
12637 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
12638 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
12639 		return bnxt_rfs_supported(bp);
12640 
12641 	if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
12642 		return false;
12643 
12644 	hwr.grp = bp->rx_nr_rings;
12645 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
12646 	if (new_rss_ctx)
12647 		hwr.vnic++;
12648 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
12649 	max_vnics = bnxt_get_max_func_vnics(bp);
12650 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
12651 
12652 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
12653 		if (bp->rx_nr_rings > 1)
12654 			netdev_warn(bp->dev,
12655 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
12656 				    min(max_rss_ctxs - 1, max_vnics - 1));
12657 		return false;
12658 	}
12659 
12660 	if (!BNXT_NEW_RM(bp))
12661 		return true;
12662 
12663 	if (hwr.vnic == bp->hw_resc.resv_vnics &&
12664 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
12665 		return true;
12666 
12667 	bnxt_hwrm_reserve_rings(bp, &hwr);
12668 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
12669 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
12670 		return true;
12671 
12672 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
12673 	hwr.vnic = 1;
12674 	hwr.rss_ctx = 0;
12675 	bnxt_hwrm_reserve_rings(bp, &hwr);
12676 	return false;
12677 }
12678 
12679 static netdev_features_t bnxt_fix_features(struct net_device *dev,
12680 					   netdev_features_t features)
12681 {
12682 	struct bnxt *bp = netdev_priv(dev);
12683 	netdev_features_t vlan_features;
12684 
12685 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
12686 		features &= ~NETIF_F_NTUPLE;
12687 
12688 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
12689 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
12690 
12691 	if (!(features & NETIF_F_GRO))
12692 		features &= ~NETIF_F_GRO_HW;
12693 
12694 	if (features & NETIF_F_GRO_HW)
12695 		features &= ~NETIF_F_LRO;
12696 
12697 	/* Both CTAG and STAG VLAN accelaration on the RX side have to be
12698 	 * turned on or off together.
12699 	 */
12700 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
12701 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
12702 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
12703 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
12704 		else if (vlan_features)
12705 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
12706 	}
12707 #ifdef CONFIG_BNXT_SRIOV
12708 	if (BNXT_VF(bp) && bp->vf.vlan)
12709 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
12710 #endif
12711 	return features;
12712 }
12713 
12714 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
12715 				bool link_re_init, u32 flags, bool update_tpa)
12716 {
12717 	bnxt_close_nic(bp, irq_re_init, link_re_init);
12718 	bp->flags = flags;
12719 	if (update_tpa)
12720 		bnxt_set_ring_params(bp);
12721 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
12722 }
12723 
12724 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
12725 {
12726 	bool update_tpa = false, update_ntuple = false;
12727 	struct bnxt *bp = netdev_priv(dev);
12728 	u32 flags = bp->flags;
12729 	u32 changes;
12730 	int rc = 0;
12731 	bool re_init = false;
12732 
12733 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
12734 	if (features & NETIF_F_GRO_HW)
12735 		flags |= BNXT_FLAG_GRO;
12736 	else if (features & NETIF_F_LRO)
12737 		flags |= BNXT_FLAG_LRO;
12738 
12739 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
12740 		flags &= ~BNXT_FLAG_TPA;
12741 
12742 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
12743 		flags |= BNXT_FLAG_STRIP_VLAN;
12744 
12745 	if (features & NETIF_F_NTUPLE)
12746 		flags |= BNXT_FLAG_RFS;
12747 	else
12748 		bnxt_clear_usr_fltrs(bp, true);
12749 
12750 	changes = flags ^ bp->flags;
12751 	if (changes & BNXT_FLAG_TPA) {
12752 		update_tpa = true;
12753 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
12754 		    (flags & BNXT_FLAG_TPA) == 0 ||
12755 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
12756 			re_init = true;
12757 	}
12758 
12759 	if (changes & ~BNXT_FLAG_TPA)
12760 		re_init = true;
12761 
12762 	if (changes & BNXT_FLAG_RFS)
12763 		update_ntuple = true;
12764 
12765 	if (flags != bp->flags) {
12766 		u32 old_flags = bp->flags;
12767 
12768 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12769 			bp->flags = flags;
12770 			if (update_tpa)
12771 				bnxt_set_ring_params(bp);
12772 			return rc;
12773 		}
12774 
12775 		if (update_ntuple)
12776 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
12777 
12778 		if (re_init)
12779 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
12780 
12781 		if (update_tpa) {
12782 			bp->flags = flags;
12783 			rc = bnxt_set_tpa(bp,
12784 					  (flags & BNXT_FLAG_TPA) ?
12785 					  true : false);
12786 			if (rc)
12787 				bp->flags = old_flags;
12788 		}
12789 	}
12790 	return rc;
12791 }
12792 
12793 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
12794 			      u8 **nextp)
12795 {
12796 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
12797 	struct hop_jumbo_hdr *jhdr;
12798 	int hdr_count = 0;
12799 	u8 *nexthdr;
12800 	int start;
12801 
12802 	/* Check that there are at most 2 IPv6 extension headers, no
12803 	 * fragment header, and each is <= 64 bytes.
12804 	 */
12805 	start = nw_off + sizeof(*ip6h);
12806 	nexthdr = &ip6h->nexthdr;
12807 	while (ipv6_ext_hdr(*nexthdr)) {
12808 		struct ipv6_opt_hdr *hp;
12809 		int hdrlen;
12810 
12811 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
12812 		    *nexthdr == NEXTHDR_FRAGMENT)
12813 			return false;
12814 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
12815 					  skb_headlen(skb), NULL);
12816 		if (!hp)
12817 			return false;
12818 		if (*nexthdr == NEXTHDR_AUTH)
12819 			hdrlen = ipv6_authlen(hp);
12820 		else
12821 			hdrlen = ipv6_optlen(hp);
12822 
12823 		if (hdrlen > 64)
12824 			return false;
12825 
12826 		/* The ext header may be a hop-by-hop header inserted for
12827 		 * big TCP purposes. This will be removed before sending
12828 		 * from NIC, so do not count it.
12829 		 */
12830 		if (*nexthdr == NEXTHDR_HOP) {
12831 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
12832 				goto increment_hdr;
12833 
12834 			jhdr = (struct hop_jumbo_hdr *)hp;
12835 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
12836 			    jhdr->nexthdr != IPPROTO_TCP)
12837 				goto increment_hdr;
12838 
12839 			goto next_hdr;
12840 		}
12841 increment_hdr:
12842 		hdr_count++;
12843 next_hdr:
12844 		nexthdr = &hp->nexthdr;
12845 		start += hdrlen;
12846 	}
12847 	if (nextp) {
12848 		/* Caller will check inner protocol */
12849 		if (skb->encapsulation) {
12850 			*nextp = nexthdr;
12851 			return true;
12852 		}
12853 		*nextp = NULL;
12854 	}
12855 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
12856 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
12857 }
12858 
12859 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
12860 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
12861 {
12862 	struct udphdr *uh = udp_hdr(skb);
12863 	__be16 udp_port = uh->dest;
12864 
12865 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
12866 	    udp_port != bp->vxlan_gpe_port)
12867 		return false;
12868 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
12869 		struct ethhdr *eh = inner_eth_hdr(skb);
12870 
12871 		switch (eh->h_proto) {
12872 		case htons(ETH_P_IP):
12873 			return true;
12874 		case htons(ETH_P_IPV6):
12875 			return bnxt_exthdr_check(bp, skb,
12876 						 skb_inner_network_offset(skb),
12877 						 NULL);
12878 		}
12879 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
12880 		return true;
12881 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
12882 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
12883 					 NULL);
12884 	}
12885 	return false;
12886 }
12887 
12888 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
12889 {
12890 	switch (l4_proto) {
12891 	case IPPROTO_UDP:
12892 		return bnxt_udp_tunl_check(bp, skb);
12893 	case IPPROTO_IPIP:
12894 		return true;
12895 	case IPPROTO_GRE: {
12896 		switch (skb->inner_protocol) {
12897 		default:
12898 			return false;
12899 		case htons(ETH_P_IP):
12900 			return true;
12901 		case htons(ETH_P_IPV6):
12902 			fallthrough;
12903 		}
12904 	}
12905 	case IPPROTO_IPV6:
12906 		/* Check ext headers of inner ipv6 */
12907 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
12908 					 NULL);
12909 	}
12910 	return false;
12911 }
12912 
12913 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
12914 					     struct net_device *dev,
12915 					     netdev_features_t features)
12916 {
12917 	struct bnxt *bp = netdev_priv(dev);
12918 	u8 *l4_proto;
12919 
12920 	features = vlan_features_check(skb, features);
12921 	switch (vlan_get_protocol(skb)) {
12922 	case htons(ETH_P_IP):
12923 		if (!skb->encapsulation)
12924 			return features;
12925 		l4_proto = &ip_hdr(skb)->protocol;
12926 		if (bnxt_tunl_check(bp, skb, *l4_proto))
12927 			return features;
12928 		break;
12929 	case htons(ETH_P_IPV6):
12930 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
12931 				       &l4_proto))
12932 			break;
12933 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
12934 			return features;
12935 		break;
12936 	}
12937 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
12938 }
12939 
12940 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
12941 			 u32 *reg_buf)
12942 {
12943 	struct hwrm_dbg_read_direct_output *resp;
12944 	struct hwrm_dbg_read_direct_input *req;
12945 	__le32 *dbg_reg_buf;
12946 	dma_addr_t mapping;
12947 	int rc, i;
12948 
12949 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
12950 	if (rc)
12951 		return rc;
12952 
12953 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
12954 					 &mapping);
12955 	if (!dbg_reg_buf) {
12956 		rc = -ENOMEM;
12957 		goto dbg_rd_reg_exit;
12958 	}
12959 
12960 	req->host_dest_addr = cpu_to_le64(mapping);
12961 
12962 	resp = hwrm_req_hold(bp, req);
12963 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
12964 	req->read_len32 = cpu_to_le32(num_words);
12965 
12966 	rc = hwrm_req_send(bp, req);
12967 	if (rc || resp->error_code) {
12968 		rc = -EIO;
12969 		goto dbg_rd_reg_exit;
12970 	}
12971 	for (i = 0; i < num_words; i++)
12972 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
12973 
12974 dbg_rd_reg_exit:
12975 	hwrm_req_drop(bp, req);
12976 	return rc;
12977 }
12978 
12979 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
12980 				       u32 ring_id, u32 *prod, u32 *cons)
12981 {
12982 	struct hwrm_dbg_ring_info_get_output *resp;
12983 	struct hwrm_dbg_ring_info_get_input *req;
12984 	int rc;
12985 
12986 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
12987 	if (rc)
12988 		return rc;
12989 
12990 	req->ring_type = ring_type;
12991 	req->fw_ring_id = cpu_to_le32(ring_id);
12992 	resp = hwrm_req_hold(bp, req);
12993 	rc = hwrm_req_send(bp, req);
12994 	if (!rc) {
12995 		*prod = le32_to_cpu(resp->producer_index);
12996 		*cons = le32_to_cpu(resp->consumer_index);
12997 	}
12998 	hwrm_req_drop(bp, req);
12999 	return rc;
13000 }
13001 
13002 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13003 {
13004 	struct bnxt_tx_ring_info *txr;
13005 	int i = bnapi->index, j;
13006 
13007 	bnxt_for_each_napi_tx(j, bnapi, txr)
13008 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13009 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13010 			    txr->tx_cons);
13011 }
13012 
13013 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
13014 {
13015 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
13016 	int i = bnapi->index;
13017 
13018 	if (!rxr)
13019 		return;
13020 
13021 	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",
13022 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
13023 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
13024 		    rxr->rx_sw_agg_prod);
13025 }
13026 
13027 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
13028 {
13029 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13030 	int i = bnapi->index;
13031 
13032 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
13033 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
13034 }
13035 
13036 static void bnxt_dbg_dump_states(struct bnxt *bp)
13037 {
13038 	int i;
13039 	struct bnxt_napi *bnapi;
13040 
13041 	for (i = 0; i < bp->cp_nr_rings; i++) {
13042 		bnapi = bp->bnapi[i];
13043 		if (netif_msg_drv(bp)) {
13044 			bnxt_dump_tx_sw_state(bnapi);
13045 			bnxt_dump_rx_sw_state(bnapi);
13046 			bnxt_dump_cp_sw_state(bnapi);
13047 		}
13048 	}
13049 }
13050 
13051 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
13052 {
13053 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
13054 	struct hwrm_ring_reset_input *req;
13055 	struct bnxt_napi *bnapi = rxr->bnapi;
13056 	struct bnxt_cp_ring_info *cpr;
13057 	u16 cp_ring_id;
13058 	int rc;
13059 
13060 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
13061 	if (rc)
13062 		return rc;
13063 
13064 	cpr = &bnapi->cp_ring;
13065 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
13066 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
13067 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
13068 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
13069 	return hwrm_req_send_silent(bp, req);
13070 }
13071 
13072 static void bnxt_reset_task(struct bnxt *bp, bool silent)
13073 {
13074 	if (!silent)
13075 		bnxt_dbg_dump_states(bp);
13076 	if (netif_running(bp->dev)) {
13077 		int rc;
13078 
13079 		if (silent) {
13080 			bnxt_close_nic(bp, false, false);
13081 			bnxt_open_nic(bp, false, false);
13082 		} else {
13083 			bnxt_ulp_stop(bp);
13084 			bnxt_close_nic(bp, true, false);
13085 			rc = bnxt_open_nic(bp, true, false);
13086 			bnxt_ulp_start(bp, rc);
13087 		}
13088 	}
13089 }
13090 
13091 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
13092 {
13093 	struct bnxt *bp = netdev_priv(dev);
13094 
13095 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
13096 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
13097 }
13098 
13099 static void bnxt_fw_health_check(struct bnxt *bp)
13100 {
13101 	struct bnxt_fw_health *fw_health = bp->fw_health;
13102 	struct pci_dev *pdev = bp->pdev;
13103 	u32 val;
13104 
13105 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13106 		return;
13107 
13108 	/* Make sure it is enabled before checking the tmr_counter. */
13109 	smp_rmb();
13110 	if (fw_health->tmr_counter) {
13111 		fw_health->tmr_counter--;
13112 		return;
13113 	}
13114 
13115 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13116 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
13117 		fw_health->arrests++;
13118 		goto fw_reset;
13119 	}
13120 
13121 	fw_health->last_fw_heartbeat = val;
13122 
13123 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13124 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
13125 		fw_health->discoveries++;
13126 		goto fw_reset;
13127 	}
13128 
13129 	fw_health->tmr_counter = fw_health->tmr_multiplier;
13130 	return;
13131 
13132 fw_reset:
13133 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
13134 }
13135 
13136 static void bnxt_timer(struct timer_list *t)
13137 {
13138 	struct bnxt *bp = from_timer(bp, t, timer);
13139 	struct net_device *dev = bp->dev;
13140 
13141 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
13142 		return;
13143 
13144 	if (atomic_read(&bp->intr_sem) != 0)
13145 		goto bnxt_restart_timer;
13146 
13147 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
13148 		bnxt_fw_health_check(bp);
13149 
13150 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
13151 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
13152 
13153 	if (bnxt_tc_flower_enabled(bp))
13154 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
13155 
13156 #ifdef CONFIG_RFS_ACCEL
13157 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
13158 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
13159 #endif /*CONFIG_RFS_ACCEL*/
13160 
13161 	if (bp->link_info.phy_retry) {
13162 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
13163 			bp->link_info.phy_retry = false;
13164 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
13165 		} else {
13166 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
13167 		}
13168 	}
13169 
13170 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13171 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13172 
13173 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
13174 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
13175 
13176 bnxt_restart_timer:
13177 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13178 }
13179 
13180 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
13181 {
13182 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
13183 	 * set.  If the device is being closed, bnxt_close() may be holding
13184 	 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear.  So we
13185 	 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
13186 	 */
13187 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13188 	rtnl_lock();
13189 }
13190 
13191 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
13192 {
13193 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13194 	rtnl_unlock();
13195 }
13196 
13197 /* Only called from bnxt_sp_task() */
13198 static void bnxt_reset(struct bnxt *bp, bool silent)
13199 {
13200 	bnxt_rtnl_lock_sp(bp);
13201 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
13202 		bnxt_reset_task(bp, silent);
13203 	bnxt_rtnl_unlock_sp(bp);
13204 }
13205 
13206 /* Only called from bnxt_sp_task() */
13207 static void bnxt_rx_ring_reset(struct bnxt *bp)
13208 {
13209 	int i;
13210 
13211 	bnxt_rtnl_lock_sp(bp);
13212 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13213 		bnxt_rtnl_unlock_sp(bp);
13214 		return;
13215 	}
13216 	/* Disable and flush TPA before resetting the RX ring */
13217 	if (bp->flags & BNXT_FLAG_TPA)
13218 		bnxt_set_tpa(bp, false);
13219 	for (i = 0; i < bp->rx_nr_rings; i++) {
13220 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
13221 		struct bnxt_cp_ring_info *cpr;
13222 		int rc;
13223 
13224 		if (!rxr->bnapi->in_reset)
13225 			continue;
13226 
13227 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
13228 		if (rc) {
13229 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
13230 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
13231 			else
13232 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
13233 					    rc);
13234 			bnxt_reset_task(bp, true);
13235 			break;
13236 		}
13237 		bnxt_free_one_rx_ring_skbs(bp, i);
13238 		rxr->rx_prod = 0;
13239 		rxr->rx_agg_prod = 0;
13240 		rxr->rx_sw_agg_prod = 0;
13241 		rxr->rx_next_cons = 0;
13242 		rxr->bnapi->in_reset = false;
13243 		bnxt_alloc_one_rx_ring(bp, i);
13244 		cpr = &rxr->bnapi->cp_ring;
13245 		cpr->sw_stats.rx.rx_resets++;
13246 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
13247 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
13248 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
13249 	}
13250 	if (bp->flags & BNXT_FLAG_TPA)
13251 		bnxt_set_tpa(bp, true);
13252 	bnxt_rtnl_unlock_sp(bp);
13253 }
13254 
13255 static void bnxt_fw_reset_close(struct bnxt *bp)
13256 {
13257 	bnxt_ulp_stop(bp);
13258 	/* When firmware is in fatal state, quiesce device and disable
13259 	 * bus master to prevent any potential bad DMAs before freeing
13260 	 * kernel memory.
13261 	 */
13262 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
13263 		u16 val = 0;
13264 
13265 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
13266 		if (val == 0xffff)
13267 			bp->fw_reset_min_dsecs = 0;
13268 		bnxt_tx_disable(bp);
13269 		bnxt_disable_napi(bp);
13270 		bnxt_disable_int_sync(bp);
13271 		bnxt_free_irq(bp);
13272 		bnxt_clear_int_mode(bp);
13273 		pci_disable_device(bp->pdev);
13274 	}
13275 	__bnxt_close_nic(bp, true, false);
13276 	bnxt_vf_reps_free(bp);
13277 	bnxt_clear_int_mode(bp);
13278 	bnxt_hwrm_func_drv_unrgtr(bp);
13279 	if (pci_is_enabled(bp->pdev))
13280 		pci_disable_device(bp->pdev);
13281 	bnxt_free_ctx_mem(bp);
13282 }
13283 
13284 static bool is_bnxt_fw_ok(struct bnxt *bp)
13285 {
13286 	struct bnxt_fw_health *fw_health = bp->fw_health;
13287 	bool no_heartbeat = false, has_reset = false;
13288 	u32 val;
13289 
13290 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13291 	if (val == fw_health->last_fw_heartbeat)
13292 		no_heartbeat = true;
13293 
13294 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13295 	if (val != fw_health->last_fw_reset_cnt)
13296 		has_reset = true;
13297 
13298 	if (!no_heartbeat && has_reset)
13299 		return true;
13300 
13301 	return false;
13302 }
13303 
13304 /* rtnl_lock is acquired before calling this function */
13305 static void bnxt_force_fw_reset(struct bnxt *bp)
13306 {
13307 	struct bnxt_fw_health *fw_health = bp->fw_health;
13308 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13309 	u32 wait_dsecs;
13310 
13311 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
13312 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13313 		return;
13314 
13315 	if (ptp) {
13316 		spin_lock_bh(&ptp->ptp_lock);
13317 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13318 		spin_unlock_bh(&ptp->ptp_lock);
13319 	} else {
13320 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13321 	}
13322 	bnxt_fw_reset_close(bp);
13323 	wait_dsecs = fw_health->master_func_wait_dsecs;
13324 	if (fw_health->primary) {
13325 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
13326 			wait_dsecs = 0;
13327 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
13328 	} else {
13329 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
13330 		wait_dsecs = fw_health->normal_func_wait_dsecs;
13331 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13332 	}
13333 
13334 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
13335 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
13336 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
13337 }
13338 
13339 void bnxt_fw_exception(struct bnxt *bp)
13340 {
13341 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
13342 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
13343 	bnxt_rtnl_lock_sp(bp);
13344 	bnxt_force_fw_reset(bp);
13345 	bnxt_rtnl_unlock_sp(bp);
13346 }
13347 
13348 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
13349  * < 0 on error.
13350  */
13351 static int bnxt_get_registered_vfs(struct bnxt *bp)
13352 {
13353 #ifdef CONFIG_BNXT_SRIOV
13354 	int rc;
13355 
13356 	if (!BNXT_PF(bp))
13357 		return 0;
13358 
13359 	rc = bnxt_hwrm_func_qcfg(bp);
13360 	if (rc) {
13361 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
13362 		return rc;
13363 	}
13364 	if (bp->pf.registered_vfs)
13365 		return bp->pf.registered_vfs;
13366 	if (bp->sriov_cfg)
13367 		return 1;
13368 #endif
13369 	return 0;
13370 }
13371 
13372 void bnxt_fw_reset(struct bnxt *bp)
13373 {
13374 	bnxt_rtnl_lock_sp(bp);
13375 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
13376 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13377 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13378 		int n = 0, tmo;
13379 
13380 		if (ptp) {
13381 			spin_lock_bh(&ptp->ptp_lock);
13382 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13383 			spin_unlock_bh(&ptp->ptp_lock);
13384 		} else {
13385 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13386 		}
13387 		if (bp->pf.active_vfs &&
13388 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
13389 			n = bnxt_get_registered_vfs(bp);
13390 		if (n < 0) {
13391 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
13392 				   n);
13393 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13394 			dev_close(bp->dev);
13395 			goto fw_reset_exit;
13396 		} else if (n > 0) {
13397 			u16 vf_tmo_dsecs = n * 10;
13398 
13399 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
13400 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
13401 			bp->fw_reset_state =
13402 				BNXT_FW_RESET_STATE_POLL_VF;
13403 			bnxt_queue_fw_reset_work(bp, HZ / 10);
13404 			goto fw_reset_exit;
13405 		}
13406 		bnxt_fw_reset_close(bp);
13407 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
13408 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
13409 			tmo = HZ / 10;
13410 		} else {
13411 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13412 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
13413 		}
13414 		bnxt_queue_fw_reset_work(bp, tmo);
13415 	}
13416 fw_reset_exit:
13417 	bnxt_rtnl_unlock_sp(bp);
13418 }
13419 
13420 static void bnxt_chk_missed_irq(struct bnxt *bp)
13421 {
13422 	int i;
13423 
13424 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13425 		return;
13426 
13427 	for (i = 0; i < bp->cp_nr_rings; i++) {
13428 		struct bnxt_napi *bnapi = bp->bnapi[i];
13429 		struct bnxt_cp_ring_info *cpr;
13430 		u32 fw_ring_id;
13431 		int j;
13432 
13433 		if (!bnapi)
13434 			continue;
13435 
13436 		cpr = &bnapi->cp_ring;
13437 		for (j = 0; j < cpr->cp_ring_count; j++) {
13438 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
13439 			u32 val[2];
13440 
13441 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
13442 				continue;
13443 
13444 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
13445 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
13446 				continue;
13447 			}
13448 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
13449 			bnxt_dbg_hwrm_ring_info_get(bp,
13450 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
13451 				fw_ring_id, &val[0], &val[1]);
13452 			cpr->sw_stats.cmn.missed_irqs++;
13453 		}
13454 	}
13455 }
13456 
13457 static void bnxt_cfg_ntp_filters(struct bnxt *);
13458 
13459 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
13460 {
13461 	struct bnxt_link_info *link_info = &bp->link_info;
13462 
13463 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
13464 		link_info->autoneg = BNXT_AUTONEG_SPEED;
13465 		if (bp->hwrm_spec_code >= 0x10201) {
13466 			if (link_info->auto_pause_setting &
13467 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
13468 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13469 		} else {
13470 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13471 		}
13472 		bnxt_set_auto_speed(link_info);
13473 	} else {
13474 		bnxt_set_force_speed(link_info);
13475 		link_info->req_duplex = link_info->duplex_setting;
13476 	}
13477 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
13478 		link_info->req_flow_ctrl =
13479 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
13480 	else
13481 		link_info->req_flow_ctrl = link_info->force_pause_setting;
13482 }
13483 
13484 static void bnxt_fw_echo_reply(struct bnxt *bp)
13485 {
13486 	struct bnxt_fw_health *fw_health = bp->fw_health;
13487 	struct hwrm_func_echo_response_input *req;
13488 	int rc;
13489 
13490 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
13491 	if (rc)
13492 		return;
13493 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
13494 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
13495 	hwrm_req_send(bp, req);
13496 }
13497 
13498 static void bnxt_sp_task(struct work_struct *work)
13499 {
13500 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
13501 
13502 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13503 	smp_mb__after_atomic();
13504 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13505 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13506 		return;
13507 	}
13508 
13509 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
13510 		bnxt_cfg_rx_mode(bp);
13511 
13512 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
13513 		bnxt_cfg_ntp_filters(bp);
13514 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
13515 		bnxt_hwrm_exec_fwd_req(bp);
13516 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
13517 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
13518 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
13519 		bnxt_hwrm_port_qstats(bp, 0);
13520 		bnxt_hwrm_port_qstats_ext(bp, 0);
13521 		bnxt_accumulate_all_stats(bp);
13522 	}
13523 
13524 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
13525 		int rc;
13526 
13527 		mutex_lock(&bp->link_lock);
13528 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
13529 				       &bp->sp_event))
13530 			bnxt_hwrm_phy_qcaps(bp);
13531 
13532 		rc = bnxt_update_link(bp, true);
13533 		if (rc)
13534 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
13535 				   rc);
13536 
13537 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
13538 				       &bp->sp_event))
13539 			bnxt_init_ethtool_link_settings(bp);
13540 		mutex_unlock(&bp->link_lock);
13541 	}
13542 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
13543 		int rc;
13544 
13545 		mutex_lock(&bp->link_lock);
13546 		rc = bnxt_update_phy_setting(bp);
13547 		mutex_unlock(&bp->link_lock);
13548 		if (rc) {
13549 			netdev_warn(bp->dev, "update phy settings retry failed\n");
13550 		} else {
13551 			bp->link_info.phy_retry = false;
13552 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
13553 		}
13554 	}
13555 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
13556 		mutex_lock(&bp->link_lock);
13557 		bnxt_get_port_module_status(bp);
13558 		mutex_unlock(&bp->link_lock);
13559 	}
13560 
13561 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
13562 		bnxt_tc_flow_stats_work(bp);
13563 
13564 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
13565 		bnxt_chk_missed_irq(bp);
13566 
13567 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
13568 		bnxt_fw_echo_reply(bp);
13569 
13570 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
13571 		bnxt_hwmon_notify_event(bp);
13572 
13573 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
13574 	 * must be the last functions to be called before exiting.
13575 	 */
13576 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
13577 		bnxt_reset(bp, false);
13578 
13579 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
13580 		bnxt_reset(bp, true);
13581 
13582 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
13583 		bnxt_rx_ring_reset(bp);
13584 
13585 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
13586 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
13587 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
13588 			bnxt_devlink_health_fw_report(bp);
13589 		else
13590 			bnxt_fw_reset(bp);
13591 	}
13592 
13593 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
13594 		if (!is_bnxt_fw_ok(bp))
13595 			bnxt_devlink_health_fw_report(bp);
13596 	}
13597 
13598 	smp_mb__before_atomic();
13599 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13600 }
13601 
13602 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
13603 				int *max_cp);
13604 
13605 /* Under rtnl_lock */
13606 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
13607 		     int tx_xdp)
13608 {
13609 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
13610 	struct bnxt_hw_rings hwr = {0};
13611 	int rx_rings = rx;
13612 
13613 	if (tcs)
13614 		tx_sets = tcs;
13615 
13616 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
13617 
13618 	if (max_rx < rx_rings)
13619 		return -ENOMEM;
13620 
13621 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
13622 		rx_rings <<= 1;
13623 
13624 	hwr.rx = rx_rings;
13625 	hwr.tx = tx * tx_sets + tx_xdp;
13626 	if (max_tx < hwr.tx)
13627 		return -ENOMEM;
13628 
13629 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
13630 
13631 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
13632 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
13633 	if (max_cp < hwr.cp)
13634 		return -ENOMEM;
13635 	hwr.stat = hwr.cp;
13636 	if (BNXT_NEW_RM(bp)) {
13637 		hwr.cp += bnxt_get_ulp_msix_num(bp);
13638 		hwr.stat += bnxt_get_ulp_stat_ctxs(bp);
13639 		hwr.grp = rx;
13640 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13641 	}
13642 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
13643 		hwr.cp_p5 = hwr.tx + rx;
13644 	return bnxt_hwrm_check_rings(bp, &hwr);
13645 }
13646 
13647 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
13648 {
13649 	if (bp->bar2) {
13650 		pci_iounmap(pdev, bp->bar2);
13651 		bp->bar2 = NULL;
13652 	}
13653 
13654 	if (bp->bar1) {
13655 		pci_iounmap(pdev, bp->bar1);
13656 		bp->bar1 = NULL;
13657 	}
13658 
13659 	if (bp->bar0) {
13660 		pci_iounmap(pdev, bp->bar0);
13661 		bp->bar0 = NULL;
13662 	}
13663 }
13664 
13665 static void bnxt_cleanup_pci(struct bnxt *bp)
13666 {
13667 	bnxt_unmap_bars(bp, bp->pdev);
13668 	pci_release_regions(bp->pdev);
13669 	if (pci_is_enabled(bp->pdev))
13670 		pci_disable_device(bp->pdev);
13671 }
13672 
13673 static void bnxt_init_dflt_coal(struct bnxt *bp)
13674 {
13675 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
13676 	struct bnxt_coal *coal;
13677 	u16 flags = 0;
13678 
13679 	if (coal_cap->cmpl_params &
13680 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
13681 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
13682 
13683 	/* Tick values in micro seconds.
13684 	 * 1 coal_buf x bufs_per_record = 1 completion record.
13685 	 */
13686 	coal = &bp->rx_coal;
13687 	coal->coal_ticks = 10;
13688 	coal->coal_bufs = 30;
13689 	coal->coal_ticks_irq = 1;
13690 	coal->coal_bufs_irq = 2;
13691 	coal->idle_thresh = 50;
13692 	coal->bufs_per_record = 2;
13693 	coal->budget = 64;		/* NAPI budget */
13694 	coal->flags = flags;
13695 
13696 	coal = &bp->tx_coal;
13697 	coal->coal_ticks = 28;
13698 	coal->coal_bufs = 30;
13699 	coal->coal_ticks_irq = 2;
13700 	coal->coal_bufs_irq = 2;
13701 	coal->bufs_per_record = 1;
13702 	coal->flags = flags;
13703 
13704 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
13705 }
13706 
13707 /* FW that pre-reserves 1 VNIC per function */
13708 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
13709 {
13710 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
13711 
13712 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13713 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
13714 		return true;
13715 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13716 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
13717 		return true;
13718 	return false;
13719 }
13720 
13721 static int bnxt_fw_init_one_p1(struct bnxt *bp)
13722 {
13723 	int rc;
13724 
13725 	bp->fw_cap = 0;
13726 	rc = bnxt_hwrm_ver_get(bp);
13727 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
13728 	 * so wait before continuing with recovery.
13729 	 */
13730 	if (rc)
13731 		msleep(100);
13732 	bnxt_try_map_fw_health_reg(bp);
13733 	if (rc) {
13734 		rc = bnxt_try_recover_fw(bp);
13735 		if (rc)
13736 			return rc;
13737 		rc = bnxt_hwrm_ver_get(bp);
13738 		if (rc)
13739 			return rc;
13740 	}
13741 
13742 	bnxt_nvm_cfg_ver_get(bp);
13743 
13744 	rc = bnxt_hwrm_func_reset(bp);
13745 	if (rc)
13746 		return -ENODEV;
13747 
13748 	bnxt_hwrm_fw_set_time(bp);
13749 	return 0;
13750 }
13751 
13752 static int bnxt_fw_init_one_p2(struct bnxt *bp)
13753 {
13754 	int rc;
13755 
13756 	/* Get the MAX capabilities for this function */
13757 	rc = bnxt_hwrm_func_qcaps(bp);
13758 	if (rc) {
13759 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
13760 			   rc);
13761 		return -ENODEV;
13762 	}
13763 
13764 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
13765 	if (rc)
13766 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
13767 			    rc);
13768 
13769 	if (bnxt_alloc_fw_health(bp)) {
13770 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
13771 	} else {
13772 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
13773 		if (rc)
13774 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
13775 				    rc);
13776 	}
13777 
13778 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
13779 	if (rc)
13780 		return -ENODEV;
13781 
13782 	if (bnxt_fw_pre_resv_vnics(bp))
13783 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
13784 
13785 	bnxt_hwrm_func_qcfg(bp);
13786 	bnxt_hwrm_vnic_qcaps(bp);
13787 	bnxt_hwrm_port_led_qcaps(bp);
13788 	bnxt_ethtool_init(bp);
13789 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
13790 		__bnxt_hwrm_ptp_qcfg(bp);
13791 	bnxt_dcb_init(bp);
13792 	bnxt_hwmon_init(bp);
13793 	return 0;
13794 }
13795 
13796 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
13797 {
13798 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
13799 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
13800 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
13801 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
13802 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
13803 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
13804 		bp->rss_hash_delta = bp->rss_hash_cfg;
13805 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
13806 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
13807 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
13808 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
13809 	}
13810 }
13811 
13812 static void bnxt_set_dflt_rfs(struct bnxt *bp)
13813 {
13814 	struct net_device *dev = bp->dev;
13815 
13816 	dev->hw_features &= ~NETIF_F_NTUPLE;
13817 	dev->features &= ~NETIF_F_NTUPLE;
13818 	bp->flags &= ~BNXT_FLAG_RFS;
13819 	if (bnxt_rfs_supported(bp)) {
13820 		dev->hw_features |= NETIF_F_NTUPLE;
13821 		if (bnxt_rfs_capable(bp, false)) {
13822 			bp->flags |= BNXT_FLAG_RFS;
13823 			dev->features |= NETIF_F_NTUPLE;
13824 		}
13825 	}
13826 }
13827 
13828 static void bnxt_fw_init_one_p3(struct bnxt *bp)
13829 {
13830 	struct pci_dev *pdev = bp->pdev;
13831 
13832 	bnxt_set_dflt_rss_hash_type(bp);
13833 	bnxt_set_dflt_rfs(bp);
13834 
13835 	bnxt_get_wol_settings(bp);
13836 	if (bp->flags & BNXT_FLAG_WOL_CAP)
13837 		device_set_wakeup_enable(&pdev->dev, bp->wol);
13838 	else
13839 		device_set_wakeup_capable(&pdev->dev, false);
13840 
13841 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
13842 	bnxt_hwrm_coal_params_qcaps(bp);
13843 }
13844 
13845 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
13846 
13847 int bnxt_fw_init_one(struct bnxt *bp)
13848 {
13849 	int rc;
13850 
13851 	rc = bnxt_fw_init_one_p1(bp);
13852 	if (rc) {
13853 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
13854 		return rc;
13855 	}
13856 	rc = bnxt_fw_init_one_p2(bp);
13857 	if (rc) {
13858 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
13859 		return rc;
13860 	}
13861 	rc = bnxt_probe_phy(bp, false);
13862 	if (rc)
13863 		return rc;
13864 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
13865 	if (rc)
13866 		return rc;
13867 
13868 	bnxt_fw_init_one_p3(bp);
13869 	return 0;
13870 }
13871 
13872 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
13873 {
13874 	struct bnxt_fw_health *fw_health = bp->fw_health;
13875 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
13876 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
13877 	u32 reg_type, reg_off, delay_msecs;
13878 
13879 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
13880 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
13881 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
13882 	switch (reg_type) {
13883 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
13884 		pci_write_config_dword(bp->pdev, reg_off, val);
13885 		break;
13886 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
13887 		writel(reg_off & BNXT_GRC_BASE_MASK,
13888 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
13889 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
13890 		fallthrough;
13891 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
13892 		writel(val, bp->bar0 + reg_off);
13893 		break;
13894 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
13895 		writel(val, bp->bar1 + reg_off);
13896 		break;
13897 	}
13898 	if (delay_msecs) {
13899 		pci_read_config_dword(bp->pdev, 0, &val);
13900 		msleep(delay_msecs);
13901 	}
13902 }
13903 
13904 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
13905 {
13906 	struct hwrm_func_qcfg_output *resp;
13907 	struct hwrm_func_qcfg_input *req;
13908 	bool result = true; /* firmware will enforce if unknown */
13909 
13910 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
13911 		return result;
13912 
13913 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
13914 		return result;
13915 
13916 	req->fid = cpu_to_le16(0xffff);
13917 	resp = hwrm_req_hold(bp, req);
13918 	if (!hwrm_req_send(bp, req))
13919 		result = !!(le16_to_cpu(resp->flags) &
13920 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
13921 	hwrm_req_drop(bp, req);
13922 	return result;
13923 }
13924 
13925 static void bnxt_reset_all(struct bnxt *bp)
13926 {
13927 	struct bnxt_fw_health *fw_health = bp->fw_health;
13928 	int i, rc;
13929 
13930 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
13931 		bnxt_fw_reset_via_optee(bp);
13932 		bp->fw_reset_timestamp = jiffies;
13933 		return;
13934 	}
13935 
13936 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
13937 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
13938 			bnxt_fw_reset_writel(bp, i);
13939 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
13940 		struct hwrm_fw_reset_input *req;
13941 
13942 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
13943 		if (!rc) {
13944 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
13945 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
13946 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
13947 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
13948 			rc = hwrm_req_send(bp, req);
13949 		}
13950 		if (rc != -ENODEV)
13951 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
13952 	}
13953 	bp->fw_reset_timestamp = jiffies;
13954 }
13955 
13956 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
13957 {
13958 	return time_after(jiffies, bp->fw_reset_timestamp +
13959 			  (bp->fw_reset_max_dsecs * HZ / 10));
13960 }
13961 
13962 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
13963 {
13964 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13965 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF) {
13966 		bnxt_ulp_start(bp, rc);
13967 		bnxt_dl_health_fw_status_update(bp, false);
13968 	}
13969 	bp->fw_reset_state = 0;
13970 	dev_close(bp->dev);
13971 }
13972 
13973 static void bnxt_fw_reset_task(struct work_struct *work)
13974 {
13975 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
13976 	int rc = 0;
13977 
13978 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13979 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
13980 		return;
13981 	}
13982 
13983 	switch (bp->fw_reset_state) {
13984 	case BNXT_FW_RESET_STATE_POLL_VF: {
13985 		int n = bnxt_get_registered_vfs(bp);
13986 		int tmo;
13987 
13988 		if (n < 0) {
13989 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
13990 				   n, jiffies_to_msecs(jiffies -
13991 				   bp->fw_reset_timestamp));
13992 			goto fw_reset_abort;
13993 		} else if (n > 0) {
13994 			if (bnxt_fw_reset_timeout(bp)) {
13995 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13996 				bp->fw_reset_state = 0;
13997 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
13998 					   n);
13999 				return;
14000 			}
14001 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14002 			return;
14003 		}
14004 		bp->fw_reset_timestamp = jiffies;
14005 		rtnl_lock();
14006 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
14007 			bnxt_fw_reset_abort(bp, rc);
14008 			rtnl_unlock();
14009 			return;
14010 		}
14011 		bnxt_fw_reset_close(bp);
14012 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14013 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14014 			tmo = HZ / 10;
14015 		} else {
14016 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14017 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14018 		}
14019 		rtnl_unlock();
14020 		bnxt_queue_fw_reset_work(bp, tmo);
14021 		return;
14022 	}
14023 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
14024 		u32 val;
14025 
14026 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14027 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
14028 		    !bnxt_fw_reset_timeout(bp)) {
14029 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14030 			return;
14031 		}
14032 
14033 		if (!bp->fw_health->primary) {
14034 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
14035 
14036 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14037 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14038 			return;
14039 		}
14040 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14041 	}
14042 		fallthrough;
14043 	case BNXT_FW_RESET_STATE_RESET_FW:
14044 		bnxt_reset_all(bp);
14045 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14046 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
14047 		return;
14048 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
14049 		bnxt_inv_fw_health_reg(bp);
14050 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
14051 		    !bp->fw_reset_min_dsecs) {
14052 			u16 val;
14053 
14054 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14055 			if (val == 0xffff) {
14056 				if (bnxt_fw_reset_timeout(bp)) {
14057 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
14058 					rc = -ETIMEDOUT;
14059 					goto fw_reset_abort;
14060 				}
14061 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
14062 				return;
14063 			}
14064 		}
14065 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14066 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
14067 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
14068 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
14069 			bnxt_dl_remote_reload(bp);
14070 		if (pci_enable_device(bp->pdev)) {
14071 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
14072 			rc = -ENODEV;
14073 			goto fw_reset_abort;
14074 		}
14075 		pci_set_master(bp->pdev);
14076 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
14077 		fallthrough;
14078 	case BNXT_FW_RESET_STATE_POLL_FW:
14079 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
14080 		rc = bnxt_hwrm_poll(bp);
14081 		if (rc) {
14082 			if (bnxt_fw_reset_timeout(bp)) {
14083 				netdev_err(bp->dev, "Firmware reset aborted\n");
14084 				goto fw_reset_abort_status;
14085 			}
14086 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14087 			return;
14088 		}
14089 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
14090 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
14091 		fallthrough;
14092 	case BNXT_FW_RESET_STATE_OPENING:
14093 		while (!rtnl_trylock()) {
14094 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14095 			return;
14096 		}
14097 		rc = bnxt_open(bp->dev);
14098 		if (rc) {
14099 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
14100 			bnxt_fw_reset_abort(bp, rc);
14101 			rtnl_unlock();
14102 			return;
14103 		}
14104 
14105 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
14106 		    bp->fw_health->enabled) {
14107 			bp->fw_health->last_fw_reset_cnt =
14108 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14109 		}
14110 		bp->fw_reset_state = 0;
14111 		/* Make sure fw_reset_state is 0 before clearing the flag */
14112 		smp_mb__before_atomic();
14113 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14114 		bnxt_ulp_start(bp, 0);
14115 		bnxt_reenable_sriov(bp);
14116 		bnxt_vf_reps_alloc(bp);
14117 		bnxt_vf_reps_open(bp);
14118 		bnxt_ptp_reapply_pps(bp);
14119 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
14120 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
14121 			bnxt_dl_health_fw_recovery_done(bp);
14122 			bnxt_dl_health_fw_status_update(bp, true);
14123 		}
14124 		rtnl_unlock();
14125 		break;
14126 	}
14127 	return;
14128 
14129 fw_reset_abort_status:
14130 	if (bp->fw_health->status_reliable ||
14131 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
14132 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14133 
14134 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
14135 	}
14136 fw_reset_abort:
14137 	rtnl_lock();
14138 	bnxt_fw_reset_abort(bp, rc);
14139 	rtnl_unlock();
14140 }
14141 
14142 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
14143 {
14144 	int rc;
14145 	struct bnxt *bp = netdev_priv(dev);
14146 
14147 	SET_NETDEV_DEV(dev, &pdev->dev);
14148 
14149 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
14150 	rc = pci_enable_device(pdev);
14151 	if (rc) {
14152 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
14153 		goto init_err;
14154 	}
14155 
14156 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
14157 		dev_err(&pdev->dev,
14158 			"Cannot find PCI device base address, aborting\n");
14159 		rc = -ENODEV;
14160 		goto init_err_disable;
14161 	}
14162 
14163 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
14164 	if (rc) {
14165 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
14166 		goto init_err_disable;
14167 	}
14168 
14169 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
14170 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
14171 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
14172 		rc = -EIO;
14173 		goto init_err_release;
14174 	}
14175 
14176 	pci_set_master(pdev);
14177 
14178 	bp->dev = dev;
14179 	bp->pdev = pdev;
14180 
14181 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
14182 	 * determines the BAR size.
14183 	 */
14184 	bp->bar0 = pci_ioremap_bar(pdev, 0);
14185 	if (!bp->bar0) {
14186 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
14187 		rc = -ENOMEM;
14188 		goto init_err_release;
14189 	}
14190 
14191 	bp->bar2 = pci_ioremap_bar(pdev, 4);
14192 	if (!bp->bar2) {
14193 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
14194 		rc = -ENOMEM;
14195 		goto init_err_release;
14196 	}
14197 
14198 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
14199 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
14200 
14201 	spin_lock_init(&bp->ntp_fltr_lock);
14202 #if BITS_PER_LONG == 32
14203 	spin_lock_init(&bp->db_lock);
14204 #endif
14205 
14206 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
14207 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
14208 
14209 	timer_setup(&bp->timer, bnxt_timer, 0);
14210 	bp->current_interval = BNXT_TIMER_INTERVAL;
14211 
14212 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
14213 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
14214 
14215 	clear_bit(BNXT_STATE_OPEN, &bp->state);
14216 	return 0;
14217 
14218 init_err_release:
14219 	bnxt_unmap_bars(bp, pdev);
14220 	pci_release_regions(pdev);
14221 
14222 init_err_disable:
14223 	pci_disable_device(pdev);
14224 
14225 init_err:
14226 	return rc;
14227 }
14228 
14229 /* rtnl_lock held */
14230 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
14231 {
14232 	struct sockaddr *addr = p;
14233 	struct bnxt *bp = netdev_priv(dev);
14234 	int rc = 0;
14235 
14236 	if (!is_valid_ether_addr(addr->sa_data))
14237 		return -EADDRNOTAVAIL;
14238 
14239 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
14240 		return 0;
14241 
14242 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
14243 	if (rc)
14244 		return rc;
14245 
14246 	eth_hw_addr_set(dev, addr->sa_data);
14247 	bnxt_clear_usr_fltrs(bp, true);
14248 	if (netif_running(dev)) {
14249 		bnxt_close_nic(bp, false, false);
14250 		rc = bnxt_open_nic(bp, false, false);
14251 	}
14252 
14253 	return rc;
14254 }
14255 
14256 /* rtnl_lock held */
14257 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
14258 {
14259 	struct bnxt *bp = netdev_priv(dev);
14260 
14261 	if (netif_running(dev))
14262 		bnxt_close_nic(bp, true, false);
14263 
14264 	dev->mtu = new_mtu;
14265 	bnxt_set_ring_params(bp);
14266 
14267 	if (netif_running(dev))
14268 		return bnxt_open_nic(bp, true, false);
14269 
14270 	return 0;
14271 }
14272 
14273 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
14274 {
14275 	struct bnxt *bp = netdev_priv(dev);
14276 	bool sh = false;
14277 	int rc, tx_cp;
14278 
14279 	if (tc > bp->max_tc) {
14280 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
14281 			   tc, bp->max_tc);
14282 		return -EINVAL;
14283 	}
14284 
14285 	if (bp->num_tc == tc)
14286 		return 0;
14287 
14288 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
14289 		sh = true;
14290 
14291 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
14292 			      sh, tc, bp->tx_nr_rings_xdp);
14293 	if (rc)
14294 		return rc;
14295 
14296 	/* Needs to close the device and do hw resource re-allocations */
14297 	if (netif_running(bp->dev))
14298 		bnxt_close_nic(bp, true, false);
14299 
14300 	if (tc) {
14301 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
14302 		netdev_set_num_tc(dev, tc);
14303 		bp->num_tc = tc;
14304 	} else {
14305 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
14306 		netdev_reset_tc(dev);
14307 		bp->num_tc = 0;
14308 	}
14309 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
14310 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
14311 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
14312 			       tx_cp + bp->rx_nr_rings;
14313 
14314 	if (netif_running(bp->dev))
14315 		return bnxt_open_nic(bp, true, false);
14316 
14317 	return 0;
14318 }
14319 
14320 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
14321 				  void *cb_priv)
14322 {
14323 	struct bnxt *bp = cb_priv;
14324 
14325 	if (!bnxt_tc_flower_enabled(bp) ||
14326 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
14327 		return -EOPNOTSUPP;
14328 
14329 	switch (type) {
14330 	case TC_SETUP_CLSFLOWER:
14331 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
14332 	default:
14333 		return -EOPNOTSUPP;
14334 	}
14335 }
14336 
14337 LIST_HEAD(bnxt_block_cb_list);
14338 
14339 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
14340 			 void *type_data)
14341 {
14342 	struct bnxt *bp = netdev_priv(dev);
14343 
14344 	switch (type) {
14345 	case TC_SETUP_BLOCK:
14346 		return flow_block_cb_setup_simple(type_data,
14347 						  &bnxt_block_cb_list,
14348 						  bnxt_setup_tc_block_cb,
14349 						  bp, bp, true);
14350 	case TC_SETUP_QDISC_MQPRIO: {
14351 		struct tc_mqprio_qopt *mqprio = type_data;
14352 
14353 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
14354 
14355 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
14356 	}
14357 	default:
14358 		return -EOPNOTSUPP;
14359 	}
14360 }
14361 
14362 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
14363 			    const struct sk_buff *skb)
14364 {
14365 	struct bnxt_vnic_info *vnic;
14366 
14367 	if (skb)
14368 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
14369 
14370 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
14371 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
14372 }
14373 
14374 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
14375 			   u32 idx)
14376 {
14377 	struct hlist_head *head;
14378 	int bit_id;
14379 
14380 	spin_lock_bh(&bp->ntp_fltr_lock);
14381 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
14382 	if (bit_id < 0) {
14383 		spin_unlock_bh(&bp->ntp_fltr_lock);
14384 		return -ENOMEM;
14385 	}
14386 
14387 	fltr->base.sw_id = (u16)bit_id;
14388 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
14389 	fltr->base.flags |= BNXT_ACT_RING_DST;
14390 	head = &bp->ntp_fltr_hash_tbl[idx];
14391 	hlist_add_head_rcu(&fltr->base.hash, head);
14392 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
14393 	bnxt_insert_usr_fltr(bp, &fltr->base);
14394 	bp->ntp_fltr_count++;
14395 	spin_unlock_bh(&bp->ntp_fltr_lock);
14396 	return 0;
14397 }
14398 
14399 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
14400 			    struct bnxt_ntuple_filter *f2)
14401 {
14402 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
14403 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
14404 	struct flow_keys *keys1 = &f1->fkeys;
14405 	struct flow_keys *keys2 = &f2->fkeys;
14406 
14407 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
14408 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
14409 		return false;
14410 
14411 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
14412 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
14413 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
14414 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
14415 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
14416 			return false;
14417 	} else {
14418 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
14419 				     &keys2->addrs.v6addrs.src) ||
14420 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
14421 				     &masks2->addrs.v6addrs.src) ||
14422 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
14423 				     &keys2->addrs.v6addrs.dst) ||
14424 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
14425 				     &masks2->addrs.v6addrs.dst))
14426 			return false;
14427 	}
14428 
14429 	return keys1->ports.src == keys2->ports.src &&
14430 	       masks1->ports.src == masks2->ports.src &&
14431 	       keys1->ports.dst == keys2->ports.dst &&
14432 	       masks1->ports.dst == masks2->ports.dst &&
14433 	       keys1->control.flags == keys2->control.flags &&
14434 	       f1->l2_fltr == f2->l2_fltr;
14435 }
14436 
14437 struct bnxt_ntuple_filter *
14438 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
14439 				struct bnxt_ntuple_filter *fltr, u32 idx)
14440 {
14441 	struct bnxt_ntuple_filter *f;
14442 	struct hlist_head *head;
14443 
14444 	head = &bp->ntp_fltr_hash_tbl[idx];
14445 	hlist_for_each_entry_rcu(f, head, base.hash) {
14446 		if (bnxt_fltr_match(f, fltr))
14447 			return f;
14448 	}
14449 	return NULL;
14450 }
14451 
14452 #ifdef CONFIG_RFS_ACCEL
14453 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
14454 			      u16 rxq_index, u32 flow_id)
14455 {
14456 	struct bnxt *bp = netdev_priv(dev);
14457 	struct bnxt_ntuple_filter *fltr, *new_fltr;
14458 	struct flow_keys *fkeys;
14459 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
14460 	struct bnxt_l2_filter *l2_fltr;
14461 	int rc = 0, idx;
14462 	u32 flags;
14463 
14464 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
14465 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
14466 		atomic_inc(&l2_fltr->refcnt);
14467 	} else {
14468 		struct bnxt_l2_key key;
14469 
14470 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
14471 		key.vlan = 0;
14472 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
14473 		if (!l2_fltr)
14474 			return -EINVAL;
14475 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
14476 			bnxt_del_l2_filter(bp, l2_fltr);
14477 			return -EINVAL;
14478 		}
14479 	}
14480 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
14481 	if (!new_fltr) {
14482 		bnxt_del_l2_filter(bp, l2_fltr);
14483 		return -ENOMEM;
14484 	}
14485 
14486 	fkeys = &new_fltr->fkeys;
14487 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
14488 		rc = -EPROTONOSUPPORT;
14489 		goto err_free;
14490 	}
14491 
14492 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
14493 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
14494 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
14495 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
14496 		rc = -EPROTONOSUPPORT;
14497 		goto err_free;
14498 	}
14499 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
14500 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
14501 		if (bp->hwrm_spec_code < 0x10601) {
14502 			rc = -EPROTONOSUPPORT;
14503 			goto err_free;
14504 		}
14505 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
14506 	}
14507 	flags = fkeys->control.flags;
14508 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
14509 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
14510 		rc = -EPROTONOSUPPORT;
14511 		goto err_free;
14512 	}
14513 	new_fltr->l2_fltr = l2_fltr;
14514 
14515 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
14516 	rcu_read_lock();
14517 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
14518 	if (fltr) {
14519 		rc = fltr->base.sw_id;
14520 		rcu_read_unlock();
14521 		goto err_free;
14522 	}
14523 	rcu_read_unlock();
14524 
14525 	new_fltr->flow_id = flow_id;
14526 	new_fltr->base.rxq = rxq_index;
14527 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
14528 	if (!rc) {
14529 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14530 		return new_fltr->base.sw_id;
14531 	}
14532 
14533 err_free:
14534 	bnxt_del_l2_filter(bp, l2_fltr);
14535 	kfree(new_fltr);
14536 	return rc;
14537 }
14538 #endif
14539 
14540 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
14541 {
14542 	spin_lock_bh(&bp->ntp_fltr_lock);
14543 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
14544 		spin_unlock_bh(&bp->ntp_fltr_lock);
14545 		return;
14546 	}
14547 	hlist_del_rcu(&fltr->base.hash);
14548 	bnxt_del_one_usr_fltr(bp, &fltr->base);
14549 	bp->ntp_fltr_count--;
14550 	spin_unlock_bh(&bp->ntp_fltr_lock);
14551 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
14552 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
14553 	kfree_rcu(fltr, base.rcu);
14554 }
14555 
14556 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
14557 {
14558 #ifdef CONFIG_RFS_ACCEL
14559 	int i;
14560 
14561 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
14562 		struct hlist_head *head;
14563 		struct hlist_node *tmp;
14564 		struct bnxt_ntuple_filter *fltr;
14565 		int rc;
14566 
14567 		head = &bp->ntp_fltr_hash_tbl[i];
14568 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
14569 			bool del = false;
14570 
14571 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
14572 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
14573 					continue;
14574 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
14575 							fltr->flow_id,
14576 							fltr->base.sw_id)) {
14577 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
14578 									 fltr);
14579 					del = true;
14580 				}
14581 			} else {
14582 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
14583 								       fltr);
14584 				if (rc)
14585 					del = true;
14586 				else
14587 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
14588 			}
14589 
14590 			if (del)
14591 				bnxt_del_ntp_filter(bp, fltr);
14592 		}
14593 	}
14594 #endif
14595 }
14596 
14597 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
14598 				    unsigned int entry, struct udp_tunnel_info *ti)
14599 {
14600 	struct bnxt *bp = netdev_priv(netdev);
14601 	unsigned int cmd;
14602 
14603 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
14604 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
14605 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
14606 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
14607 	else
14608 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
14609 
14610 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
14611 }
14612 
14613 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
14614 				      unsigned int entry, struct udp_tunnel_info *ti)
14615 {
14616 	struct bnxt *bp = netdev_priv(netdev);
14617 	unsigned int cmd;
14618 
14619 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
14620 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
14621 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
14622 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
14623 	else
14624 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
14625 
14626 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
14627 }
14628 
14629 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
14630 	.set_port	= bnxt_udp_tunnel_set_port,
14631 	.unset_port	= bnxt_udp_tunnel_unset_port,
14632 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
14633 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
14634 	.tables		= {
14635 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
14636 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
14637 	},
14638 }, bnxt_udp_tunnels_p7 = {
14639 	.set_port	= bnxt_udp_tunnel_set_port,
14640 	.unset_port	= bnxt_udp_tunnel_unset_port,
14641 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
14642 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
14643 	.tables		= {
14644 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
14645 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
14646 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
14647 	},
14648 };
14649 
14650 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
14651 			       struct net_device *dev, u32 filter_mask,
14652 			       int nlflags)
14653 {
14654 	struct bnxt *bp = netdev_priv(dev);
14655 
14656 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
14657 				       nlflags, filter_mask, NULL);
14658 }
14659 
14660 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
14661 			       u16 flags, struct netlink_ext_ack *extack)
14662 {
14663 	struct bnxt *bp = netdev_priv(dev);
14664 	struct nlattr *attr, *br_spec;
14665 	int rem, rc = 0;
14666 
14667 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
14668 		return -EOPNOTSUPP;
14669 
14670 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
14671 	if (!br_spec)
14672 		return -EINVAL;
14673 
14674 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
14675 		u16 mode;
14676 
14677 		mode = nla_get_u16(attr);
14678 		if (mode == bp->br_mode)
14679 			break;
14680 
14681 		rc = bnxt_hwrm_set_br_mode(bp, mode);
14682 		if (!rc)
14683 			bp->br_mode = mode;
14684 		break;
14685 	}
14686 	return rc;
14687 }
14688 
14689 int bnxt_get_port_parent_id(struct net_device *dev,
14690 			    struct netdev_phys_item_id *ppid)
14691 {
14692 	struct bnxt *bp = netdev_priv(dev);
14693 
14694 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
14695 		return -EOPNOTSUPP;
14696 
14697 	/* The PF and it's VF-reps only support the switchdev framework */
14698 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
14699 		return -EOPNOTSUPP;
14700 
14701 	ppid->id_len = sizeof(bp->dsn);
14702 	memcpy(ppid->id, bp->dsn, ppid->id_len);
14703 
14704 	return 0;
14705 }
14706 
14707 static const struct net_device_ops bnxt_netdev_ops = {
14708 	.ndo_open		= bnxt_open,
14709 	.ndo_start_xmit		= bnxt_start_xmit,
14710 	.ndo_stop		= bnxt_close,
14711 	.ndo_get_stats64	= bnxt_get_stats64,
14712 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
14713 	.ndo_eth_ioctl		= bnxt_ioctl,
14714 	.ndo_validate_addr	= eth_validate_addr,
14715 	.ndo_set_mac_address	= bnxt_change_mac_addr,
14716 	.ndo_change_mtu		= bnxt_change_mtu,
14717 	.ndo_fix_features	= bnxt_fix_features,
14718 	.ndo_set_features	= bnxt_set_features,
14719 	.ndo_features_check	= bnxt_features_check,
14720 	.ndo_tx_timeout		= bnxt_tx_timeout,
14721 #ifdef CONFIG_BNXT_SRIOV
14722 	.ndo_get_vf_config	= bnxt_get_vf_config,
14723 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
14724 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
14725 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
14726 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
14727 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
14728 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
14729 #endif
14730 	.ndo_setup_tc           = bnxt_setup_tc,
14731 #ifdef CONFIG_RFS_ACCEL
14732 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
14733 #endif
14734 	.ndo_bpf		= bnxt_xdp,
14735 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
14736 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
14737 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
14738 };
14739 
14740 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
14741 				    struct netdev_queue_stats_rx *stats)
14742 {
14743 	struct bnxt *bp = netdev_priv(dev);
14744 	struct bnxt_cp_ring_info *cpr;
14745 	u64 *sw;
14746 
14747 	cpr = &bp->bnapi[i]->cp_ring;
14748 	sw = cpr->stats.sw_stats;
14749 
14750 	stats->packets = 0;
14751 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
14752 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
14753 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
14754 
14755 	stats->bytes = 0;
14756 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
14757 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
14758 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
14759 
14760 	stats->alloc_fail = cpr->sw_stats.rx.rx_oom_discards;
14761 }
14762 
14763 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
14764 				    struct netdev_queue_stats_tx *stats)
14765 {
14766 	struct bnxt *bp = netdev_priv(dev);
14767 	struct bnxt_napi *bnapi;
14768 	u64 *sw;
14769 
14770 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
14771 	sw = bnapi->cp_ring.stats.sw_stats;
14772 
14773 	stats->packets = 0;
14774 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
14775 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
14776 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
14777 
14778 	stats->bytes = 0;
14779 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
14780 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
14781 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
14782 }
14783 
14784 static void bnxt_get_base_stats(struct net_device *dev,
14785 				struct netdev_queue_stats_rx *rx,
14786 				struct netdev_queue_stats_tx *tx)
14787 {
14788 	struct bnxt *bp = netdev_priv(dev);
14789 
14790 	rx->packets = bp->net_stats_prev.rx_packets;
14791 	rx->bytes = bp->net_stats_prev.rx_bytes;
14792 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
14793 
14794 	tx->packets = bp->net_stats_prev.tx_packets;
14795 	tx->bytes = bp->net_stats_prev.tx_bytes;
14796 }
14797 
14798 static const struct netdev_stat_ops bnxt_stat_ops = {
14799 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
14800 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
14801 	.get_base_stats		= bnxt_get_base_stats,
14802 };
14803 
14804 static void bnxt_remove_one(struct pci_dev *pdev)
14805 {
14806 	struct net_device *dev = pci_get_drvdata(pdev);
14807 	struct bnxt *bp = netdev_priv(dev);
14808 
14809 	if (BNXT_PF(bp))
14810 		bnxt_sriov_disable(bp);
14811 
14812 	bnxt_rdma_aux_device_uninit(bp);
14813 
14814 	bnxt_ptp_clear(bp);
14815 	unregister_netdev(dev);
14816 	bnxt_free_l2_filters(bp, true);
14817 	bnxt_free_ntp_fltrs(bp, true);
14818 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
14819 		bnxt_clear_rss_ctxs(bp, true);
14820 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14821 	/* Flush any pending tasks */
14822 	cancel_work_sync(&bp->sp_task);
14823 	cancel_delayed_work_sync(&bp->fw_reset_task);
14824 	bp->sp_event = 0;
14825 
14826 	bnxt_dl_fw_reporters_destroy(bp);
14827 	bnxt_dl_unregister(bp);
14828 	bnxt_shutdown_tc(bp);
14829 
14830 	bnxt_clear_int_mode(bp);
14831 	bnxt_hwrm_func_drv_unrgtr(bp);
14832 	bnxt_free_hwrm_resources(bp);
14833 	bnxt_hwmon_uninit(bp);
14834 	bnxt_ethtool_free(bp);
14835 	bnxt_dcb_free(bp);
14836 	kfree(bp->ptp_cfg);
14837 	bp->ptp_cfg = NULL;
14838 	kfree(bp->fw_health);
14839 	bp->fw_health = NULL;
14840 	bnxt_cleanup_pci(bp);
14841 	bnxt_free_ctx_mem(bp);
14842 	kfree(bp->rss_indir_tbl);
14843 	bp->rss_indir_tbl = NULL;
14844 	bnxt_free_port_stats(bp);
14845 	free_netdev(dev);
14846 }
14847 
14848 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
14849 {
14850 	int rc = 0;
14851 	struct bnxt_link_info *link_info = &bp->link_info;
14852 
14853 	bp->phy_flags = 0;
14854 	rc = bnxt_hwrm_phy_qcaps(bp);
14855 	if (rc) {
14856 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
14857 			   rc);
14858 		return rc;
14859 	}
14860 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
14861 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
14862 	else
14863 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
14864 	if (!fw_dflt)
14865 		return 0;
14866 
14867 	mutex_lock(&bp->link_lock);
14868 	rc = bnxt_update_link(bp, false);
14869 	if (rc) {
14870 		mutex_unlock(&bp->link_lock);
14871 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
14872 			   rc);
14873 		return rc;
14874 	}
14875 
14876 	/* Older firmware does not have supported_auto_speeds, so assume
14877 	 * that all supported speeds can be autonegotiated.
14878 	 */
14879 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
14880 		link_info->support_auto_speeds = link_info->support_speeds;
14881 
14882 	bnxt_init_ethtool_link_settings(bp);
14883 	mutex_unlock(&bp->link_lock);
14884 	return 0;
14885 }
14886 
14887 static int bnxt_get_max_irq(struct pci_dev *pdev)
14888 {
14889 	u16 ctrl;
14890 
14891 	if (!pdev->msix_cap)
14892 		return 1;
14893 
14894 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
14895 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
14896 }
14897 
14898 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14899 				int *max_cp)
14900 {
14901 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
14902 	int max_ring_grps = 0, max_irq;
14903 
14904 	*max_tx = hw_resc->max_tx_rings;
14905 	*max_rx = hw_resc->max_rx_rings;
14906 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
14907 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
14908 			bnxt_get_ulp_msix_num(bp),
14909 			hw_resc->max_stat_ctxs - bnxt_get_ulp_stat_ctxs(bp));
14910 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14911 		*max_cp = min_t(int, *max_cp, max_irq);
14912 	max_ring_grps = hw_resc->max_hw_ring_grps;
14913 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
14914 		*max_cp -= 1;
14915 		*max_rx -= 2;
14916 	}
14917 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14918 		*max_rx >>= 1;
14919 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
14920 		int rc;
14921 
14922 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
14923 		if (rc) {
14924 			*max_rx = 0;
14925 			*max_tx = 0;
14926 		}
14927 		/* On P5 chips, max_cp output param should be available NQs */
14928 		*max_cp = max_irq;
14929 	}
14930 	*max_rx = min_t(int, *max_rx, max_ring_grps);
14931 }
14932 
14933 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
14934 {
14935 	int rx, tx, cp;
14936 
14937 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
14938 	*max_rx = rx;
14939 	*max_tx = tx;
14940 	if (!rx || !tx || !cp)
14941 		return -ENOMEM;
14942 
14943 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
14944 }
14945 
14946 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14947 			       bool shared)
14948 {
14949 	int rc;
14950 
14951 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
14952 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
14953 		/* Not enough rings, try disabling agg rings. */
14954 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
14955 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
14956 		if (rc) {
14957 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
14958 			bp->flags |= BNXT_FLAG_AGG_RINGS;
14959 			return rc;
14960 		}
14961 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
14962 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
14963 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
14964 		bnxt_set_ring_params(bp);
14965 	}
14966 
14967 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
14968 		int max_cp, max_stat, max_irq;
14969 
14970 		/* Reserve minimum resources for RoCE */
14971 		max_cp = bnxt_get_max_func_cp_rings(bp);
14972 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
14973 		max_irq = bnxt_get_max_func_irqs(bp);
14974 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
14975 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
14976 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
14977 			return 0;
14978 
14979 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
14980 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
14981 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
14982 		max_cp = min_t(int, max_cp, max_irq);
14983 		max_cp = min_t(int, max_cp, max_stat);
14984 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
14985 		if (rc)
14986 			rc = 0;
14987 	}
14988 	return rc;
14989 }
14990 
14991 /* In initial default shared ring setting, each shared ring must have a
14992  * RX/TX ring pair.
14993  */
14994 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
14995 {
14996 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
14997 	bp->rx_nr_rings = bp->cp_nr_rings;
14998 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
14999 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15000 }
15001 
15002 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
15003 {
15004 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
15005 
15006 	if (!bnxt_can_reserve_rings(bp))
15007 		return 0;
15008 
15009 	if (sh)
15010 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
15011 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
15012 	/* Reduce default rings on multi-port cards so that total default
15013 	 * rings do not exceed CPU count.
15014 	 */
15015 	if (bp->port_count > 1) {
15016 		int max_rings =
15017 			max_t(int, num_online_cpus() / bp->port_count, 1);
15018 
15019 		dflt_rings = min_t(int, dflt_rings, max_rings);
15020 	}
15021 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
15022 	if (rc)
15023 		return rc;
15024 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
15025 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
15026 	if (sh)
15027 		bnxt_trim_dflt_sh_rings(bp);
15028 	else
15029 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
15030 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15031 
15032 	rc = __bnxt_reserve_rings(bp);
15033 	if (rc && rc != -ENODEV)
15034 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
15035 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15036 	if (sh)
15037 		bnxt_trim_dflt_sh_rings(bp);
15038 
15039 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
15040 	if (bnxt_need_reserve_rings(bp)) {
15041 		rc = __bnxt_reserve_rings(bp);
15042 		if (rc && rc != -ENODEV)
15043 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
15044 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15045 	}
15046 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
15047 		bp->rx_nr_rings++;
15048 		bp->cp_nr_rings++;
15049 	}
15050 	if (rc) {
15051 		bp->tx_nr_rings = 0;
15052 		bp->rx_nr_rings = 0;
15053 	}
15054 	return rc;
15055 }
15056 
15057 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
15058 {
15059 	int rc;
15060 
15061 	if (bp->tx_nr_rings)
15062 		return 0;
15063 
15064 	bnxt_ulp_irq_stop(bp);
15065 	bnxt_clear_int_mode(bp);
15066 	rc = bnxt_set_dflt_rings(bp, true);
15067 	if (rc) {
15068 		if (BNXT_VF(bp) && rc == -ENODEV)
15069 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
15070 		else
15071 			netdev_err(bp->dev, "Not enough rings available.\n");
15072 		goto init_dflt_ring_err;
15073 	}
15074 	rc = bnxt_init_int_mode(bp);
15075 	if (rc)
15076 		goto init_dflt_ring_err;
15077 
15078 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15079 
15080 	bnxt_set_dflt_rfs(bp);
15081 
15082 init_dflt_ring_err:
15083 	bnxt_ulp_irq_restart(bp, rc);
15084 	return rc;
15085 }
15086 
15087 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
15088 {
15089 	int rc;
15090 
15091 	ASSERT_RTNL();
15092 	bnxt_hwrm_func_qcaps(bp);
15093 
15094 	if (netif_running(bp->dev))
15095 		__bnxt_close_nic(bp, true, false);
15096 
15097 	bnxt_ulp_irq_stop(bp);
15098 	bnxt_clear_int_mode(bp);
15099 	rc = bnxt_init_int_mode(bp);
15100 	bnxt_ulp_irq_restart(bp, rc);
15101 
15102 	if (netif_running(bp->dev)) {
15103 		if (rc)
15104 			dev_close(bp->dev);
15105 		else
15106 			rc = bnxt_open_nic(bp, true, false);
15107 	}
15108 
15109 	return rc;
15110 }
15111 
15112 static int bnxt_init_mac_addr(struct bnxt *bp)
15113 {
15114 	int rc = 0;
15115 
15116 	if (BNXT_PF(bp)) {
15117 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
15118 	} else {
15119 #ifdef CONFIG_BNXT_SRIOV
15120 		struct bnxt_vf_info *vf = &bp->vf;
15121 		bool strict_approval = true;
15122 
15123 		if (is_valid_ether_addr(vf->mac_addr)) {
15124 			/* overwrite netdev dev_addr with admin VF MAC */
15125 			eth_hw_addr_set(bp->dev, vf->mac_addr);
15126 			/* Older PF driver or firmware may not approve this
15127 			 * correctly.
15128 			 */
15129 			strict_approval = false;
15130 		} else {
15131 			eth_hw_addr_random(bp->dev);
15132 		}
15133 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
15134 #endif
15135 	}
15136 	return rc;
15137 }
15138 
15139 static void bnxt_vpd_read_info(struct bnxt *bp)
15140 {
15141 	struct pci_dev *pdev = bp->pdev;
15142 	unsigned int vpd_size, kw_len;
15143 	int pos, size;
15144 	u8 *vpd_data;
15145 
15146 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
15147 	if (IS_ERR(vpd_data)) {
15148 		pci_warn(pdev, "Unable to read VPD\n");
15149 		return;
15150 	}
15151 
15152 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
15153 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
15154 	if (pos < 0)
15155 		goto read_sn;
15156 
15157 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
15158 	memcpy(bp->board_partno, &vpd_data[pos], size);
15159 
15160 read_sn:
15161 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
15162 					   PCI_VPD_RO_KEYWORD_SERIALNO,
15163 					   &kw_len);
15164 	if (pos < 0)
15165 		goto exit;
15166 
15167 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
15168 	memcpy(bp->board_serialno, &vpd_data[pos], size);
15169 exit:
15170 	kfree(vpd_data);
15171 }
15172 
15173 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
15174 {
15175 	struct pci_dev *pdev = bp->pdev;
15176 	u64 qword;
15177 
15178 	qword = pci_get_dsn(pdev);
15179 	if (!qword) {
15180 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
15181 		return -EOPNOTSUPP;
15182 	}
15183 
15184 	put_unaligned_le64(qword, dsn);
15185 
15186 	bp->flags |= BNXT_FLAG_DSN_VALID;
15187 	return 0;
15188 }
15189 
15190 static int bnxt_map_db_bar(struct bnxt *bp)
15191 {
15192 	if (!bp->db_size)
15193 		return -ENODEV;
15194 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
15195 	if (!bp->bar1)
15196 		return -ENOMEM;
15197 	return 0;
15198 }
15199 
15200 void bnxt_print_device_info(struct bnxt *bp)
15201 {
15202 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
15203 		    board_info[bp->board_idx].name,
15204 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
15205 
15206 	pcie_print_link_status(bp->pdev);
15207 }
15208 
15209 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
15210 {
15211 	struct bnxt_hw_resc *hw_resc;
15212 	struct net_device *dev;
15213 	struct bnxt *bp;
15214 	int rc, max_irqs;
15215 
15216 	if (pci_is_bridge(pdev))
15217 		return -ENODEV;
15218 
15219 	/* Clear any pending DMA transactions from crash kernel
15220 	 * while loading driver in capture kernel.
15221 	 */
15222 	if (is_kdump_kernel()) {
15223 		pci_clear_master(pdev);
15224 		pcie_flr(pdev);
15225 	}
15226 
15227 	max_irqs = bnxt_get_max_irq(pdev);
15228 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
15229 				 max_irqs);
15230 	if (!dev)
15231 		return -ENOMEM;
15232 
15233 	bp = netdev_priv(dev);
15234 	bp->board_idx = ent->driver_data;
15235 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
15236 	bnxt_set_max_func_irqs(bp, max_irqs);
15237 
15238 	if (bnxt_vf_pciid(bp->board_idx))
15239 		bp->flags |= BNXT_FLAG_VF;
15240 
15241 	/* No devlink port registration in case of a VF */
15242 	if (BNXT_PF(bp))
15243 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
15244 
15245 	if (pdev->msix_cap)
15246 		bp->flags |= BNXT_FLAG_MSIX_CAP;
15247 
15248 	rc = bnxt_init_board(pdev, dev);
15249 	if (rc < 0)
15250 		goto init_err_free;
15251 
15252 	dev->netdev_ops = &bnxt_netdev_ops;
15253 	dev->stat_ops = &bnxt_stat_ops;
15254 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
15255 	dev->ethtool_ops = &bnxt_ethtool_ops;
15256 	pci_set_drvdata(pdev, dev);
15257 
15258 	rc = bnxt_alloc_hwrm_resources(bp);
15259 	if (rc)
15260 		goto init_err_pci_clean;
15261 
15262 	mutex_init(&bp->hwrm_cmd_lock);
15263 	mutex_init(&bp->link_lock);
15264 
15265 	rc = bnxt_fw_init_one_p1(bp);
15266 	if (rc)
15267 		goto init_err_pci_clean;
15268 
15269 	if (BNXT_PF(bp))
15270 		bnxt_vpd_read_info(bp);
15271 
15272 	if (BNXT_CHIP_P5_PLUS(bp)) {
15273 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
15274 		if (BNXT_CHIP_P7(bp))
15275 			bp->flags |= BNXT_FLAG_CHIP_P7;
15276 	}
15277 
15278 	rc = bnxt_alloc_rss_indir_tbl(bp, NULL);
15279 	if (rc)
15280 		goto init_err_pci_clean;
15281 
15282 	rc = bnxt_fw_init_one_p2(bp);
15283 	if (rc)
15284 		goto init_err_pci_clean;
15285 
15286 	rc = bnxt_map_db_bar(bp);
15287 	if (rc) {
15288 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
15289 			rc);
15290 		goto init_err_pci_clean;
15291 	}
15292 
15293 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
15294 			   NETIF_F_TSO | NETIF_F_TSO6 |
15295 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
15296 			   NETIF_F_GSO_IPXIP4 |
15297 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
15298 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
15299 			   NETIF_F_RXCSUM | NETIF_F_GRO;
15300 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
15301 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
15302 
15303 	if (BNXT_SUPPORTS_TPA(bp))
15304 		dev->hw_features |= NETIF_F_LRO;
15305 
15306 	dev->hw_enc_features =
15307 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
15308 			NETIF_F_TSO | NETIF_F_TSO6 |
15309 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
15310 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
15311 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
15312 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
15313 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
15314 	if (bp->flags & BNXT_FLAG_CHIP_P7)
15315 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
15316 	else
15317 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
15318 
15319 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
15320 				    NETIF_F_GSO_GRE_CSUM;
15321 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
15322 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
15323 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
15324 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
15325 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
15326 	if (BNXT_SUPPORTS_TPA(bp))
15327 		dev->hw_features |= NETIF_F_GRO_HW;
15328 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
15329 	if (dev->features & NETIF_F_GRO_HW)
15330 		dev->features &= ~NETIF_F_LRO;
15331 	dev->priv_flags |= IFF_UNICAST_FLT;
15332 
15333 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
15334 
15335 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
15336 			    NETDEV_XDP_ACT_RX_SG;
15337 
15338 #ifdef CONFIG_BNXT_SRIOV
15339 	init_waitqueue_head(&bp->sriov_cfg_wait);
15340 #endif
15341 	if (BNXT_SUPPORTS_TPA(bp)) {
15342 		bp->gro_func = bnxt_gro_func_5730x;
15343 		if (BNXT_CHIP_P4(bp))
15344 			bp->gro_func = bnxt_gro_func_5731x;
15345 		else if (BNXT_CHIP_P5_PLUS(bp))
15346 			bp->gro_func = bnxt_gro_func_5750x;
15347 	}
15348 	if (!BNXT_CHIP_P4_PLUS(bp))
15349 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
15350 
15351 	rc = bnxt_init_mac_addr(bp);
15352 	if (rc) {
15353 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
15354 		rc = -EADDRNOTAVAIL;
15355 		goto init_err_pci_clean;
15356 	}
15357 
15358 	if (BNXT_PF(bp)) {
15359 		/* Read the adapter's DSN to use as the eswitch switch_id */
15360 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
15361 	}
15362 
15363 	/* MTU range: 60 - FW defined max */
15364 	dev->min_mtu = ETH_ZLEN;
15365 	dev->max_mtu = bp->max_mtu;
15366 
15367 	rc = bnxt_probe_phy(bp, true);
15368 	if (rc)
15369 		goto init_err_pci_clean;
15370 
15371 	hw_resc = &bp->hw_resc;
15372 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
15373 		       BNXT_L2_FLTR_MAX_FLTR;
15374 	/* Older firmware may not report these filters properly */
15375 	if (bp->max_fltr < BNXT_MAX_FLTR)
15376 		bp->max_fltr = BNXT_MAX_FLTR;
15377 	bnxt_init_l2_fltr_tbl(bp);
15378 	bnxt_set_rx_skb_mode(bp, false);
15379 	bnxt_set_tpa_flags(bp);
15380 	bnxt_set_ring_params(bp);
15381 	rc = bnxt_set_dflt_rings(bp, true);
15382 	if (rc) {
15383 		if (BNXT_VF(bp) && rc == -ENODEV) {
15384 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
15385 		} else {
15386 			netdev_err(bp->dev, "Not enough rings available.\n");
15387 			rc = -ENOMEM;
15388 		}
15389 		goto init_err_pci_clean;
15390 	}
15391 
15392 	bnxt_fw_init_one_p3(bp);
15393 
15394 	bnxt_init_dflt_coal(bp);
15395 
15396 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
15397 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
15398 
15399 	rc = bnxt_init_int_mode(bp);
15400 	if (rc)
15401 		goto init_err_pci_clean;
15402 
15403 	/* No TC has been set yet and rings may have been trimmed due to
15404 	 * limited MSIX, so we re-initialize the TX rings per TC.
15405 	 */
15406 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15407 
15408 	if (BNXT_PF(bp)) {
15409 		if (!bnxt_pf_wq) {
15410 			bnxt_pf_wq =
15411 				create_singlethread_workqueue("bnxt_pf_wq");
15412 			if (!bnxt_pf_wq) {
15413 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
15414 				rc = -ENOMEM;
15415 				goto init_err_pci_clean;
15416 			}
15417 		}
15418 		rc = bnxt_init_tc(bp);
15419 		if (rc)
15420 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
15421 				   rc);
15422 	}
15423 
15424 	bnxt_inv_fw_health_reg(bp);
15425 	rc = bnxt_dl_register(bp);
15426 	if (rc)
15427 		goto init_err_dl;
15428 
15429 	INIT_LIST_HEAD(&bp->usr_fltr_list);
15430 
15431 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
15432 		bnxt_init_multi_rss_ctx(bp);
15433 
15434 	rc = register_netdev(dev);
15435 	if (rc)
15436 		goto init_err_cleanup;
15437 
15438 	bnxt_dl_fw_reporters_create(bp);
15439 
15440 	bnxt_rdma_aux_device_init(bp);
15441 
15442 	bnxt_print_device_info(bp);
15443 
15444 	pci_save_state(pdev);
15445 
15446 	return 0;
15447 init_err_cleanup:
15448 	bnxt_dl_unregister(bp);
15449 init_err_dl:
15450 	bnxt_shutdown_tc(bp);
15451 	bnxt_clear_int_mode(bp);
15452 
15453 init_err_pci_clean:
15454 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
15455 		bnxt_clear_rss_ctxs(bp, true);
15456 	bnxt_hwrm_func_drv_unrgtr(bp);
15457 	bnxt_free_hwrm_resources(bp);
15458 	bnxt_hwmon_uninit(bp);
15459 	bnxt_ethtool_free(bp);
15460 	bnxt_ptp_clear(bp);
15461 	kfree(bp->ptp_cfg);
15462 	bp->ptp_cfg = NULL;
15463 	kfree(bp->fw_health);
15464 	bp->fw_health = NULL;
15465 	bnxt_cleanup_pci(bp);
15466 	bnxt_free_ctx_mem(bp);
15467 	kfree(bp->rss_indir_tbl);
15468 	bp->rss_indir_tbl = NULL;
15469 
15470 init_err_free:
15471 	free_netdev(dev);
15472 	return rc;
15473 }
15474 
15475 static void bnxt_shutdown(struct pci_dev *pdev)
15476 {
15477 	struct net_device *dev = pci_get_drvdata(pdev);
15478 	struct bnxt *bp;
15479 
15480 	if (!dev)
15481 		return;
15482 
15483 	rtnl_lock();
15484 	bp = netdev_priv(dev);
15485 	if (!bp)
15486 		goto shutdown_exit;
15487 
15488 	if (netif_running(dev))
15489 		dev_close(dev);
15490 
15491 	bnxt_clear_int_mode(bp);
15492 	pci_disable_device(pdev);
15493 
15494 	if (system_state == SYSTEM_POWER_OFF) {
15495 		pci_wake_from_d3(pdev, bp->wol);
15496 		pci_set_power_state(pdev, PCI_D3hot);
15497 	}
15498 
15499 shutdown_exit:
15500 	rtnl_unlock();
15501 }
15502 
15503 #ifdef CONFIG_PM_SLEEP
15504 static int bnxt_suspend(struct device *device)
15505 {
15506 	struct net_device *dev = dev_get_drvdata(device);
15507 	struct bnxt *bp = netdev_priv(dev);
15508 	int rc = 0;
15509 
15510 	rtnl_lock();
15511 	bnxt_ulp_stop(bp);
15512 	if (netif_running(dev)) {
15513 		netif_device_detach(dev);
15514 		rc = bnxt_close(dev);
15515 	}
15516 	bnxt_hwrm_func_drv_unrgtr(bp);
15517 	pci_disable_device(bp->pdev);
15518 	bnxt_free_ctx_mem(bp);
15519 	rtnl_unlock();
15520 	return rc;
15521 }
15522 
15523 static int bnxt_resume(struct device *device)
15524 {
15525 	struct net_device *dev = dev_get_drvdata(device);
15526 	struct bnxt *bp = netdev_priv(dev);
15527 	int rc = 0;
15528 
15529 	rtnl_lock();
15530 	rc = pci_enable_device(bp->pdev);
15531 	if (rc) {
15532 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
15533 			   rc);
15534 		goto resume_exit;
15535 	}
15536 	pci_set_master(bp->pdev);
15537 	if (bnxt_hwrm_ver_get(bp)) {
15538 		rc = -ENODEV;
15539 		goto resume_exit;
15540 	}
15541 	rc = bnxt_hwrm_func_reset(bp);
15542 	if (rc) {
15543 		rc = -EBUSY;
15544 		goto resume_exit;
15545 	}
15546 
15547 	rc = bnxt_hwrm_func_qcaps(bp);
15548 	if (rc)
15549 		goto resume_exit;
15550 
15551 	bnxt_clear_reservations(bp, true);
15552 
15553 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
15554 		rc = -ENODEV;
15555 		goto resume_exit;
15556 	}
15557 
15558 	bnxt_get_wol_settings(bp);
15559 	if (netif_running(dev)) {
15560 		rc = bnxt_open(dev);
15561 		if (!rc)
15562 			netif_device_attach(dev);
15563 	}
15564 
15565 resume_exit:
15566 	bnxt_ulp_start(bp, rc);
15567 	if (!rc)
15568 		bnxt_reenable_sriov(bp);
15569 	rtnl_unlock();
15570 	return rc;
15571 }
15572 
15573 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
15574 #define BNXT_PM_OPS (&bnxt_pm_ops)
15575 
15576 #else
15577 
15578 #define BNXT_PM_OPS NULL
15579 
15580 #endif /* CONFIG_PM_SLEEP */
15581 
15582 /**
15583  * bnxt_io_error_detected - called when PCI error is detected
15584  * @pdev: Pointer to PCI device
15585  * @state: The current pci connection state
15586  *
15587  * This function is called after a PCI bus error affecting
15588  * this device has been detected.
15589  */
15590 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
15591 					       pci_channel_state_t state)
15592 {
15593 	struct net_device *netdev = pci_get_drvdata(pdev);
15594 	struct bnxt *bp = netdev_priv(netdev);
15595 
15596 	netdev_info(netdev, "PCI I/O error detected\n");
15597 
15598 	rtnl_lock();
15599 	netif_device_detach(netdev);
15600 
15601 	bnxt_ulp_stop(bp);
15602 
15603 	if (state == pci_channel_io_perm_failure) {
15604 		rtnl_unlock();
15605 		return PCI_ERS_RESULT_DISCONNECT;
15606 	}
15607 
15608 	if (state == pci_channel_io_frozen)
15609 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
15610 
15611 	if (netif_running(netdev))
15612 		bnxt_close(netdev);
15613 
15614 	if (pci_is_enabled(pdev))
15615 		pci_disable_device(pdev);
15616 	bnxt_free_ctx_mem(bp);
15617 	rtnl_unlock();
15618 
15619 	/* Request a slot slot reset. */
15620 	return PCI_ERS_RESULT_NEED_RESET;
15621 }
15622 
15623 /**
15624  * bnxt_io_slot_reset - called after the pci bus has been reset.
15625  * @pdev: Pointer to PCI device
15626  *
15627  * Restart the card from scratch, as if from a cold-boot.
15628  * At this point, the card has exprienced a hard reset,
15629  * followed by fixups by BIOS, and has its config space
15630  * set up identically to what it was at cold boot.
15631  */
15632 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
15633 {
15634 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
15635 	struct net_device *netdev = pci_get_drvdata(pdev);
15636 	struct bnxt *bp = netdev_priv(netdev);
15637 	int retry = 0;
15638 	int err = 0;
15639 	int off;
15640 
15641 	netdev_info(bp->dev, "PCI Slot Reset\n");
15642 
15643 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
15644 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
15645 		msleep(900);
15646 
15647 	rtnl_lock();
15648 
15649 	if (pci_enable_device(pdev)) {
15650 		dev_err(&pdev->dev,
15651 			"Cannot re-enable PCI device after reset.\n");
15652 	} else {
15653 		pci_set_master(pdev);
15654 		/* Upon fatal error, our device internal logic that latches to
15655 		 * BAR value is getting reset and will restore only upon
15656 		 * rewritting the BARs.
15657 		 *
15658 		 * As pci_restore_state() does not re-write the BARs if the
15659 		 * value is same as saved value earlier, driver needs to
15660 		 * write the BARs to 0 to force restore, in case of fatal error.
15661 		 */
15662 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
15663 				       &bp->state)) {
15664 			for (off = PCI_BASE_ADDRESS_0;
15665 			     off <= PCI_BASE_ADDRESS_5; off += 4)
15666 				pci_write_config_dword(bp->pdev, off, 0);
15667 		}
15668 		pci_restore_state(pdev);
15669 		pci_save_state(pdev);
15670 
15671 		bnxt_inv_fw_health_reg(bp);
15672 		bnxt_try_map_fw_health_reg(bp);
15673 
15674 		/* In some PCIe AER scenarios, firmware may take up to
15675 		 * 10 seconds to become ready in the worst case.
15676 		 */
15677 		do {
15678 			err = bnxt_try_recover_fw(bp);
15679 			if (!err)
15680 				break;
15681 			retry++;
15682 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
15683 
15684 		if (err) {
15685 			dev_err(&pdev->dev, "Firmware not ready\n");
15686 			goto reset_exit;
15687 		}
15688 
15689 		err = bnxt_hwrm_func_reset(bp);
15690 		if (!err)
15691 			result = PCI_ERS_RESULT_RECOVERED;
15692 
15693 		bnxt_ulp_irq_stop(bp);
15694 		bnxt_clear_int_mode(bp);
15695 		err = bnxt_init_int_mode(bp);
15696 		bnxt_ulp_irq_restart(bp, err);
15697 	}
15698 
15699 reset_exit:
15700 	bnxt_clear_reservations(bp, true);
15701 	rtnl_unlock();
15702 
15703 	return result;
15704 }
15705 
15706 /**
15707  * bnxt_io_resume - called when traffic can start flowing again.
15708  * @pdev: Pointer to PCI device
15709  *
15710  * This callback is called when the error recovery driver tells
15711  * us that its OK to resume normal operation.
15712  */
15713 static void bnxt_io_resume(struct pci_dev *pdev)
15714 {
15715 	struct net_device *netdev = pci_get_drvdata(pdev);
15716 	struct bnxt *bp = netdev_priv(netdev);
15717 	int err;
15718 
15719 	netdev_info(bp->dev, "PCI Slot Resume\n");
15720 	rtnl_lock();
15721 
15722 	err = bnxt_hwrm_func_qcaps(bp);
15723 	if (!err && netif_running(netdev))
15724 		err = bnxt_open(netdev);
15725 
15726 	bnxt_ulp_start(bp, err);
15727 	if (!err) {
15728 		bnxt_reenable_sriov(bp);
15729 		netif_device_attach(netdev);
15730 	}
15731 
15732 	rtnl_unlock();
15733 }
15734 
15735 static const struct pci_error_handlers bnxt_err_handler = {
15736 	.error_detected	= bnxt_io_error_detected,
15737 	.slot_reset	= bnxt_io_slot_reset,
15738 	.resume		= bnxt_io_resume
15739 };
15740 
15741 static struct pci_driver bnxt_pci_driver = {
15742 	.name		= DRV_MODULE_NAME,
15743 	.id_table	= bnxt_pci_tbl,
15744 	.probe		= bnxt_init_one,
15745 	.remove		= bnxt_remove_one,
15746 	.shutdown	= bnxt_shutdown,
15747 	.driver.pm	= BNXT_PM_OPS,
15748 	.err_handler	= &bnxt_err_handler,
15749 #if defined(CONFIG_BNXT_SRIOV)
15750 	.sriov_configure = bnxt_sriov_configure,
15751 #endif
15752 };
15753 
15754 static int __init bnxt_init(void)
15755 {
15756 	int err;
15757 
15758 	bnxt_debug_init();
15759 	err = pci_register_driver(&bnxt_pci_driver);
15760 	if (err) {
15761 		bnxt_debug_exit();
15762 		return err;
15763 	}
15764 
15765 	return 0;
15766 }
15767 
15768 static void __exit bnxt_exit(void)
15769 {
15770 	pci_unregister_driver(&bnxt_pci_driver);
15771 	if (bnxt_pf_wq)
15772 		destroy_workqueue(bnxt_pf_wq);
15773 	bnxt_debug_exit();
15774 }
15775 
15776 module_init(bnxt_init);
15777 module_exit(bnxt_exit);
15778