xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision 2a52ca7c98960aafb0eca9ef96b2d0c932171357)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  * Copyright (c) 2016-2019 Broadcom Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  */
10 
11 #include <linux/module.h>
12 
13 #include <linux/stringify.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/pci.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/bitops.h>
27 #include <linux/io.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/byteorder.h>
31 #include <asm/page.h>
32 #include <linux/time.h>
33 #include <linux/mii.h>
34 #include <linux/mdio.h>
35 #include <linux/if.h>
36 #include <linux/if_vlan.h>
37 #include <linux/if_bridge.h>
38 #include <linux/rtc.h>
39 #include <linux/bpf.h>
40 #include <net/gro.h>
41 #include <net/ip.h>
42 #include <net/tcp.h>
43 #include <net/udp.h>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
46 #include <net/udp_tunnel.h>
47 #include <linux/workqueue.h>
48 #include <linux/prefetch.h>
49 #include <linux/cache.h>
50 #include <linux/log2.h>
51 #include <linux/bitmap.h>
52 #include <linux/cpu_rmap.h>
53 #include <linux/cpumask.h>
54 #include <net/pkt_cls.h>
55 #include <net/page_pool/helpers.h>
56 #include <linux/align.h>
57 #include <net/netdev_queues.h>
58 
59 #include "bnxt_hsi.h"
60 #include "bnxt.h"
61 #include "bnxt_hwrm.h"
62 #include "bnxt_ulp.h"
63 #include "bnxt_sriov.h"
64 #include "bnxt_ethtool.h"
65 #include "bnxt_dcb.h"
66 #include "bnxt_xdp.h"
67 #include "bnxt_ptp.h"
68 #include "bnxt_vfr.h"
69 #include "bnxt_tc.h"
70 #include "bnxt_devlink.h"
71 #include "bnxt_debugfs.h"
72 #include "bnxt_hwmon.h"
73 
74 #define BNXT_TX_TIMEOUT		(5 * HZ)
75 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
76 				 NETIF_MSG_TX_ERR)
77 
78 MODULE_LICENSE("GPL");
79 MODULE_DESCRIPTION("Broadcom NetXtreme network driver");
80 
81 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
82 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
83 #define BNXT_RX_COPY_THRESH 256
84 
85 #define BNXT_TX_PUSH_THRESH 164
86 
87 /* indexed by enum board_idx */
88 static const struct {
89 	char *name;
90 } board_info[] = {
91 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
92 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
93 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
94 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
95 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
96 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
97 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
98 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
99 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
100 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
101 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
102 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
103 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
104 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
105 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
106 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
108 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
109 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
110 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
111 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
112 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
113 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
114 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
115 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
116 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
117 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
118 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
119 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
120 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
121 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
122 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
123 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
124 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
125 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
126 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
127 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
128 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
129 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
130 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
131 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
132 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
133 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
134 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
135 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
136 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
137 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
138 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
139 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
140 	[NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" },
141 };
142 
143 static const struct pci_device_id bnxt_pci_tbl[] = {
144 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
145 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
146 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
147 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
148 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
149 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
150 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
151 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
152 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
153 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
155 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
159 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
160 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
164 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
165 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
166 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
167 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
168 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
169 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
170 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
171 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
172 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
173 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
174 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
175 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
176 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
178 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
179 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
180 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
181 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
182 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
183 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
184 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
185 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
187 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
188 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
189 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
190 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
191 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
192 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
193 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
194 #ifdef CONFIG_BNXT_SRIOV
195 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
196 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
197 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
198 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
199 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
200 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
201 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
202 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
203 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
204 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
205 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
206 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
207 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
208 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
209 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
210 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
211 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
212 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
214 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
215 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
216 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
217 #endif
218 	{ 0 }
219 };
220 
221 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
222 
223 static const u16 bnxt_vf_req_snif[] = {
224 	HWRM_FUNC_CFG,
225 	HWRM_FUNC_VF_CFG,
226 	HWRM_PORT_PHY_QCFG,
227 	HWRM_CFA_L2_FILTER_ALLOC,
228 };
229 
230 static const u16 bnxt_async_events_arr[] = {
231 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
232 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
233 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
234 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
235 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
236 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
237 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
238 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
239 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
240 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
241 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
242 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
243 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
244 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
245 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
246 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
247 };
248 
249 static struct workqueue_struct *bnxt_pf_wq;
250 
251 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
252 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
253 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
254 
255 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
256 	.ports = {
257 		.src = 0,
258 		.dst = 0,
259 	},
260 	.addrs = {
261 		.v6addrs = {
262 			.src = BNXT_IPV6_MASK_NONE,
263 			.dst = BNXT_IPV6_MASK_NONE,
264 		},
265 	},
266 };
267 
268 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
269 	.ports = {
270 		.src = cpu_to_be16(0xffff),
271 		.dst = cpu_to_be16(0xffff),
272 	},
273 	.addrs = {
274 		.v6addrs = {
275 			.src = BNXT_IPV6_MASK_ALL,
276 			.dst = BNXT_IPV6_MASK_ALL,
277 		},
278 	},
279 };
280 
281 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
282 	.ports = {
283 		.src = cpu_to_be16(0xffff),
284 		.dst = cpu_to_be16(0xffff),
285 	},
286 	.addrs = {
287 		.v4addrs = {
288 			.src = cpu_to_be32(0xffffffff),
289 			.dst = cpu_to_be32(0xffffffff),
290 		},
291 	},
292 };
293 
294 static bool bnxt_vf_pciid(enum board_idx idx)
295 {
296 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
297 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
298 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
299 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF);
300 }
301 
302 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
303 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
304 #define DB_CP_IRQ_DIS_FLAGS	(DB_KEY_CP | DB_IRQ_DIS)
305 
306 #define BNXT_CP_DB_IRQ_DIS(db)						\
307 		writel(DB_CP_IRQ_DIS_FLAGS, db)
308 
309 #define BNXT_DB_CQ(db, idx)						\
310 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
311 
312 #define BNXT_DB_NQ_P5(db, idx)						\
313 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
314 		    (db)->doorbell)
315 
316 #define BNXT_DB_NQ_P7(db, idx)						\
317 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
318 		    DB_RING_IDX(db, idx), (db)->doorbell)
319 
320 #define BNXT_DB_CQ_ARM(db, idx)						\
321 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
322 
323 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
324 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
325 		    DB_RING_IDX(db, idx), (db)->doorbell)
326 
327 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
328 {
329 	if (bp->flags & BNXT_FLAG_CHIP_P7)
330 		BNXT_DB_NQ_P7(db, idx);
331 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
332 		BNXT_DB_NQ_P5(db, idx);
333 	else
334 		BNXT_DB_CQ(db, idx);
335 }
336 
337 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
338 {
339 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
340 		BNXT_DB_NQ_ARM_P5(db, idx);
341 	else
342 		BNXT_DB_CQ_ARM(db, idx);
343 }
344 
345 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
346 {
347 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
348 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
349 			    DB_RING_IDX(db, idx), db->doorbell);
350 	else
351 		BNXT_DB_CQ(db, idx);
352 }
353 
354 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
355 {
356 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
357 		return;
358 
359 	if (BNXT_PF(bp))
360 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
361 	else
362 		schedule_delayed_work(&bp->fw_reset_task, delay);
363 }
364 
365 static void __bnxt_queue_sp_work(struct bnxt *bp)
366 {
367 	if (BNXT_PF(bp))
368 		queue_work(bnxt_pf_wq, &bp->sp_task);
369 	else
370 		schedule_work(&bp->sp_task);
371 }
372 
373 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
374 {
375 	set_bit(event, &bp->sp_event);
376 	__bnxt_queue_sp_work(bp);
377 }
378 
379 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
380 {
381 	if (!rxr->bnapi->in_reset) {
382 		rxr->bnapi->in_reset = true;
383 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
384 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
385 		else
386 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
387 		__bnxt_queue_sp_work(bp);
388 	}
389 	rxr->rx_next_cons = 0xffff;
390 }
391 
392 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
393 			  u16 curr)
394 {
395 	struct bnxt_napi *bnapi = txr->bnapi;
396 
397 	if (bnapi->tx_fault)
398 		return;
399 
400 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
401 		   txr->txq_index, txr->tx_hw_cons,
402 		   txr->tx_cons, txr->tx_prod, curr);
403 	WARN_ON_ONCE(1);
404 	bnapi->tx_fault = 1;
405 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
406 }
407 
408 const u16 bnxt_lhint_arr[] = {
409 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
410 	TX_BD_FLAGS_LHINT_512_TO_1023,
411 	TX_BD_FLAGS_LHINT_1024_TO_2047,
412 	TX_BD_FLAGS_LHINT_1024_TO_2047,
413 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
414 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
415 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
416 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
417 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
418 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
419 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
420 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
421 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
422 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
423 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
424 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
425 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
426 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
427 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
428 };
429 
430 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
431 {
432 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
433 
434 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
435 		return 0;
436 
437 	return md_dst->u.port_info.port_id;
438 }
439 
440 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
441 			     u16 prod)
442 {
443 	/* Sync BD data before updating doorbell */
444 	wmb();
445 	bnxt_db_write(bp, &txr->tx_db, prod);
446 	txr->kick_pending = 0;
447 }
448 
449 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
450 {
451 	struct bnxt *bp = netdev_priv(dev);
452 	struct tx_bd *txbd, *txbd0;
453 	struct tx_bd_ext *txbd1;
454 	struct netdev_queue *txq;
455 	int i;
456 	dma_addr_t mapping;
457 	unsigned int length, pad = 0;
458 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
459 	u16 prod, last_frag;
460 	struct pci_dev *pdev = bp->pdev;
461 	struct bnxt_tx_ring_info *txr;
462 	struct bnxt_sw_tx_bd *tx_buf;
463 	__le32 lflags = 0;
464 
465 	i = skb_get_queue_mapping(skb);
466 	if (unlikely(i >= bp->tx_nr_rings)) {
467 		dev_kfree_skb_any(skb);
468 		dev_core_stats_tx_dropped_inc(dev);
469 		return NETDEV_TX_OK;
470 	}
471 
472 	txq = netdev_get_tx_queue(dev, i);
473 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
474 	prod = txr->tx_prod;
475 
476 	free_size = bnxt_tx_avail(bp, txr);
477 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
478 		/* We must have raced with NAPI cleanup */
479 		if (net_ratelimit() && txr->kick_pending)
480 			netif_warn(bp, tx_err, dev,
481 				   "bnxt: ring busy w/ flush pending!\n");
482 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
483 					bp->tx_wake_thresh))
484 			return NETDEV_TX_BUSY;
485 	}
486 
487 	if (unlikely(ipv6_hopopt_jumbo_remove(skb)))
488 		goto tx_free;
489 
490 	length = skb->len;
491 	len = skb_headlen(skb);
492 	last_frag = skb_shinfo(skb)->nr_frags;
493 
494 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
495 
496 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
497 	tx_buf->skb = skb;
498 	tx_buf->nr_frags = last_frag;
499 
500 	vlan_tag_flags = 0;
501 	cfa_action = bnxt_xmit_get_cfa_action(skb);
502 	if (skb_vlan_tag_present(skb)) {
503 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
504 				 skb_vlan_tag_get(skb);
505 		/* Currently supports 8021Q, 8021AD vlan offloads
506 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
507 		 */
508 		if (skb->vlan_proto == htons(ETH_P_8021Q))
509 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
510 	}
511 
512 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
513 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
514 
515 		if (ptp && ptp->tx_tstamp_en && !skb_is_gso(skb)) {
516 			if (atomic_dec_if_positive(&ptp->tx_avail) < 0) {
517 				atomic64_inc(&ptp->stats.ts_err);
518 				goto tx_no_ts;
519 			}
520 			if (!bnxt_ptp_parse(skb, &ptp->tx_seqid,
521 					    &ptp->tx_hdr_off)) {
522 				if (vlan_tag_flags)
523 					ptp->tx_hdr_off += VLAN_HLEN;
524 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
525 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
526 			} else {
527 				atomic_inc(&bp->ptp_cfg->tx_avail);
528 			}
529 		}
530 	}
531 
532 tx_no_ts:
533 	if (unlikely(skb->no_fcs))
534 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
535 
536 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
537 	    !lflags) {
538 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
539 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
540 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
541 		void __iomem *db = txr->tx_db.doorbell;
542 		void *pdata = tx_push_buf->data;
543 		u64 *end;
544 		int j, push_len;
545 
546 		/* Set COAL_NOW to be ready quickly for the next push */
547 		tx_push->tx_bd_len_flags_type =
548 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
549 					TX_BD_TYPE_LONG_TX_BD |
550 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
551 					TX_BD_FLAGS_COAL_NOW |
552 					TX_BD_FLAGS_PACKET_END |
553 					(2 << TX_BD_FLAGS_BD_CNT_SHIFT));
554 
555 		if (skb->ip_summed == CHECKSUM_PARTIAL)
556 			tx_push1->tx_bd_hsize_lflags =
557 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
558 		else
559 			tx_push1->tx_bd_hsize_lflags = 0;
560 
561 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
562 		tx_push1->tx_bd_cfa_action =
563 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
564 
565 		end = pdata + length;
566 		end = PTR_ALIGN(end, 8) - 1;
567 		*end = 0;
568 
569 		skb_copy_from_linear_data(skb, pdata, len);
570 		pdata += len;
571 		for (j = 0; j < last_frag; j++) {
572 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
573 			void *fptr;
574 
575 			fptr = skb_frag_address_safe(frag);
576 			if (!fptr)
577 				goto normal_tx;
578 
579 			memcpy(pdata, fptr, skb_frag_size(frag));
580 			pdata += skb_frag_size(frag);
581 		}
582 
583 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
584 		txbd->tx_bd_haddr = txr->data_mapping;
585 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
586 		prod = NEXT_TX(prod);
587 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
588 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
589 		memcpy(txbd, tx_push1, sizeof(*txbd));
590 		prod = NEXT_TX(prod);
591 		tx_push->doorbell =
592 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
593 				    DB_RING_IDX(&txr->tx_db, prod));
594 		WRITE_ONCE(txr->tx_prod, prod);
595 
596 		tx_buf->is_push = 1;
597 		netdev_tx_sent_queue(txq, skb->len);
598 		wmb();	/* Sync is_push and byte queue before pushing data */
599 
600 		push_len = (length + sizeof(*tx_push) + 7) / 8;
601 		if (push_len > 16) {
602 			__iowrite64_copy(db, tx_push_buf, 16);
603 			__iowrite32_copy(db + 4, tx_push_buf + 1,
604 					 (push_len - 16) << 1);
605 		} else {
606 			__iowrite64_copy(db, tx_push_buf, push_len);
607 		}
608 
609 		goto tx_done;
610 	}
611 
612 normal_tx:
613 	if (length < BNXT_MIN_PKT_SIZE) {
614 		pad = BNXT_MIN_PKT_SIZE - length;
615 		if (skb_pad(skb, pad))
616 			/* SKB already freed. */
617 			goto tx_kick_pending;
618 		length = BNXT_MIN_PKT_SIZE;
619 	}
620 
621 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
622 
623 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
624 		goto tx_free;
625 
626 	dma_unmap_addr_set(tx_buf, mapping, mapping);
627 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
628 		((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
629 
630 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
631 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
632 
633 	prod = NEXT_TX(prod);
634 	txbd1 = (struct tx_bd_ext *)
635 		&txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
636 
637 	txbd1->tx_bd_hsize_lflags = lflags;
638 	if (skb_is_gso(skb)) {
639 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
640 		u32 hdr_len;
641 
642 		if (skb->encapsulation) {
643 			if (udp_gso)
644 				hdr_len = skb_inner_transport_offset(skb) +
645 					  sizeof(struct udphdr);
646 			else
647 				hdr_len = skb_inner_tcp_all_headers(skb);
648 		} else if (udp_gso) {
649 			hdr_len = skb_transport_offset(skb) +
650 				  sizeof(struct udphdr);
651 		} else {
652 			hdr_len = skb_tcp_all_headers(skb);
653 		}
654 
655 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
656 					TX_BD_FLAGS_T_IPID |
657 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
658 		length = skb_shinfo(skb)->gso_size;
659 		txbd1->tx_bd_mss = cpu_to_le32(length);
660 		length += hdr_len;
661 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
662 		txbd1->tx_bd_hsize_lflags |=
663 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
664 		txbd1->tx_bd_mss = 0;
665 	}
666 
667 	length >>= 9;
668 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
669 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
670 				     skb->len);
671 		i = 0;
672 		goto tx_dma_error;
673 	}
674 	flags |= bnxt_lhint_arr[length];
675 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
676 
677 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
678 	txbd1->tx_bd_cfa_action =
679 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
680 	txbd0 = txbd;
681 	for (i = 0; i < last_frag; i++) {
682 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
683 
684 		prod = NEXT_TX(prod);
685 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
686 
687 		len = skb_frag_size(frag);
688 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
689 					   DMA_TO_DEVICE);
690 
691 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
692 			goto tx_dma_error;
693 
694 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
695 		dma_unmap_addr_set(tx_buf, mapping, mapping);
696 
697 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
698 
699 		flags = len << TX_BD_LEN_SHIFT;
700 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
701 	}
702 
703 	flags &= ~TX_BD_LEN;
704 	txbd->tx_bd_len_flags_type =
705 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
706 			    TX_BD_FLAGS_PACKET_END);
707 
708 	netdev_tx_sent_queue(txq, skb->len);
709 
710 	skb_tx_timestamp(skb);
711 
712 	prod = NEXT_TX(prod);
713 	WRITE_ONCE(txr->tx_prod, prod);
714 
715 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
716 		bnxt_txr_db_kick(bp, txr, prod);
717 	} else {
718 		if (free_size >= bp->tx_wake_thresh)
719 			txbd0->tx_bd_len_flags_type |=
720 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
721 		txr->kick_pending = 1;
722 	}
723 
724 tx_done:
725 
726 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
727 		if (netdev_xmit_more() && !tx_buf->is_push) {
728 			txbd0->tx_bd_len_flags_type &=
729 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
730 			bnxt_txr_db_kick(bp, txr, prod);
731 		}
732 
733 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
734 				   bp->tx_wake_thresh);
735 	}
736 	return NETDEV_TX_OK;
737 
738 tx_dma_error:
739 	if (BNXT_TX_PTP_IS_SET(lflags)) {
740 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
741 		atomic_inc(&bp->ptp_cfg->tx_avail);
742 	}
743 
744 	last_frag = i;
745 
746 	/* start back at beginning and unmap skb */
747 	prod = txr->tx_prod;
748 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
749 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
750 			 skb_headlen(skb), DMA_TO_DEVICE);
751 	prod = NEXT_TX(prod);
752 
753 	/* unmap remaining mapped pages */
754 	for (i = 0; i < last_frag; i++) {
755 		prod = NEXT_TX(prod);
756 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
757 		dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
758 			       skb_frag_size(&skb_shinfo(skb)->frags[i]),
759 			       DMA_TO_DEVICE);
760 	}
761 
762 tx_free:
763 	dev_kfree_skb_any(skb);
764 tx_kick_pending:
765 	if (txr->kick_pending)
766 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
767 	txr->tx_buf_ring[txr->tx_prod].skb = NULL;
768 	dev_core_stats_tx_dropped_inc(dev);
769 	return NETDEV_TX_OK;
770 }
771 
772 static void __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
773 			  int budget)
774 {
775 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
776 	struct pci_dev *pdev = bp->pdev;
777 	u16 hw_cons = txr->tx_hw_cons;
778 	unsigned int tx_bytes = 0;
779 	u16 cons = txr->tx_cons;
780 	int tx_pkts = 0;
781 
782 	while (RING_TX(bp, cons) != hw_cons) {
783 		struct bnxt_sw_tx_bd *tx_buf;
784 		struct sk_buff *skb;
785 		int j, last;
786 
787 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
788 		cons = NEXT_TX(cons);
789 		skb = tx_buf->skb;
790 		tx_buf->skb = NULL;
791 
792 		if (unlikely(!skb)) {
793 			bnxt_sched_reset_txr(bp, txr, cons);
794 			return;
795 		}
796 
797 		tx_pkts++;
798 		tx_bytes += skb->len;
799 
800 		if (tx_buf->is_push) {
801 			tx_buf->is_push = 0;
802 			goto next_tx_int;
803 		}
804 
805 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
806 				 skb_headlen(skb), DMA_TO_DEVICE);
807 		last = tx_buf->nr_frags;
808 
809 		for (j = 0; j < last; j++) {
810 			cons = NEXT_TX(cons);
811 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
812 			dma_unmap_page(
813 				&pdev->dev,
814 				dma_unmap_addr(tx_buf, mapping),
815 				skb_frag_size(&skb_shinfo(skb)->frags[j]),
816 				DMA_TO_DEVICE);
817 		}
818 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
819 			if (BNXT_CHIP_P5(bp)) {
820 				/* PTP worker takes ownership of the skb */
821 				if (!bnxt_get_tx_ts_p5(bp, skb)) {
822 					skb = NULL;
823 				} else {
824 					atomic64_inc(&bp->ptp_cfg->stats.ts_err);
825 					atomic_inc(&bp->ptp_cfg->tx_avail);
826 				}
827 			}
828 		}
829 
830 next_tx_int:
831 		cons = NEXT_TX(cons);
832 
833 		dev_consume_skb_any(skb);
834 	}
835 
836 	WRITE_ONCE(txr->tx_cons, cons);
837 
838 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
839 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
840 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
841 }
842 
843 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
844 {
845 	struct bnxt_tx_ring_info *txr;
846 	int i;
847 
848 	bnxt_for_each_napi_tx(i, bnapi, txr) {
849 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
850 			__bnxt_tx_int(bp, txr, budget);
851 	}
852 	bnapi->events &= ~BNXT_TX_CMP_EVENT;
853 }
854 
855 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
856 					 struct bnxt_rx_ring_info *rxr,
857 					 unsigned int *offset,
858 					 gfp_t gfp)
859 {
860 	struct page *page;
861 
862 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
863 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
864 						BNXT_RX_PAGE_SIZE);
865 	} else {
866 		page = page_pool_dev_alloc_pages(rxr->page_pool);
867 		*offset = 0;
868 	}
869 	if (!page)
870 		return NULL;
871 
872 	*mapping = page_pool_get_dma_addr(page) + *offset;
873 	return page;
874 }
875 
876 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
877 				       gfp_t gfp)
878 {
879 	u8 *data;
880 	struct pci_dev *pdev = bp->pdev;
881 
882 	if (gfp == GFP_ATOMIC)
883 		data = napi_alloc_frag(bp->rx_buf_size);
884 	else
885 		data = netdev_alloc_frag(bp->rx_buf_size);
886 	if (!data)
887 		return NULL;
888 
889 	*mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
890 					bp->rx_buf_use_size, bp->rx_dir,
891 					DMA_ATTR_WEAK_ORDERING);
892 
893 	if (dma_mapping_error(&pdev->dev, *mapping)) {
894 		skb_free_frag(data);
895 		data = NULL;
896 	}
897 	return data;
898 }
899 
900 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
901 		       u16 prod, gfp_t gfp)
902 {
903 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
904 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
905 	dma_addr_t mapping;
906 
907 	if (BNXT_RX_PAGE_MODE(bp)) {
908 		unsigned int offset;
909 		struct page *page =
910 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
911 
912 		if (!page)
913 			return -ENOMEM;
914 
915 		mapping += bp->rx_dma_offset;
916 		rx_buf->data = page;
917 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
918 	} else {
919 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, gfp);
920 
921 		if (!data)
922 			return -ENOMEM;
923 
924 		rx_buf->data = data;
925 		rx_buf->data_ptr = data + bp->rx_offset;
926 	}
927 	rx_buf->mapping = mapping;
928 
929 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
930 	return 0;
931 }
932 
933 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
934 {
935 	u16 prod = rxr->rx_prod;
936 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
937 	struct bnxt *bp = rxr->bnapi->bp;
938 	struct rx_bd *cons_bd, *prod_bd;
939 
940 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
941 	cons_rx_buf = &rxr->rx_buf_ring[cons];
942 
943 	prod_rx_buf->data = data;
944 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
945 
946 	prod_rx_buf->mapping = cons_rx_buf->mapping;
947 
948 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
949 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
950 
951 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
952 }
953 
954 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
955 {
956 	u16 next, max = rxr->rx_agg_bmap_size;
957 
958 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
959 	if (next >= max)
960 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
961 	return next;
962 }
963 
964 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
965 				     struct bnxt_rx_ring_info *rxr,
966 				     u16 prod, gfp_t gfp)
967 {
968 	struct rx_bd *rxbd =
969 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
970 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
971 	struct page *page;
972 	dma_addr_t mapping;
973 	u16 sw_prod = rxr->rx_sw_agg_prod;
974 	unsigned int offset = 0;
975 
976 	page = __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
977 
978 	if (!page)
979 		return -ENOMEM;
980 
981 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
982 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
983 
984 	__set_bit(sw_prod, rxr->rx_agg_bmap);
985 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
986 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
987 
988 	rx_agg_buf->page = page;
989 	rx_agg_buf->offset = offset;
990 	rx_agg_buf->mapping = mapping;
991 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
992 	rxbd->rx_bd_opaque = sw_prod;
993 	return 0;
994 }
995 
996 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
997 				       struct bnxt_cp_ring_info *cpr,
998 				       u16 cp_cons, u16 curr)
999 {
1000 	struct rx_agg_cmp *agg;
1001 
1002 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1003 	agg = (struct rx_agg_cmp *)
1004 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1005 	return agg;
1006 }
1007 
1008 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1009 					      struct bnxt_rx_ring_info *rxr,
1010 					      u16 agg_id, u16 curr)
1011 {
1012 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1013 
1014 	return &tpa_info->agg_arr[curr];
1015 }
1016 
1017 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1018 				   u16 start, u32 agg_bufs, bool tpa)
1019 {
1020 	struct bnxt_napi *bnapi = cpr->bnapi;
1021 	struct bnxt *bp = bnapi->bp;
1022 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1023 	u16 prod = rxr->rx_agg_prod;
1024 	u16 sw_prod = rxr->rx_sw_agg_prod;
1025 	bool p5_tpa = false;
1026 	u32 i;
1027 
1028 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1029 		p5_tpa = true;
1030 
1031 	for (i = 0; i < agg_bufs; i++) {
1032 		u16 cons;
1033 		struct rx_agg_cmp *agg;
1034 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1035 		struct rx_bd *prod_bd;
1036 		struct page *page;
1037 
1038 		if (p5_tpa)
1039 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1040 		else
1041 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1042 		cons = agg->rx_agg_cmp_opaque;
1043 		__clear_bit(cons, rxr->rx_agg_bmap);
1044 
1045 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1046 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1047 
1048 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1049 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1050 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1051 
1052 		/* It is possible for sw_prod to be equal to cons, so
1053 		 * set cons_rx_buf->page to NULL first.
1054 		 */
1055 		page = cons_rx_buf->page;
1056 		cons_rx_buf->page = NULL;
1057 		prod_rx_buf->page = page;
1058 		prod_rx_buf->offset = cons_rx_buf->offset;
1059 
1060 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1061 
1062 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1063 
1064 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1065 		prod_bd->rx_bd_opaque = sw_prod;
1066 
1067 		prod = NEXT_RX_AGG(prod);
1068 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1069 	}
1070 	rxr->rx_agg_prod = prod;
1071 	rxr->rx_sw_agg_prod = sw_prod;
1072 }
1073 
1074 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1075 					      struct bnxt_rx_ring_info *rxr,
1076 					      u16 cons, void *data, u8 *data_ptr,
1077 					      dma_addr_t dma_addr,
1078 					      unsigned int offset_and_len)
1079 {
1080 	unsigned int len = offset_and_len & 0xffff;
1081 	struct page *page = data;
1082 	u16 prod = rxr->rx_prod;
1083 	struct sk_buff *skb;
1084 	int err;
1085 
1086 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1087 	if (unlikely(err)) {
1088 		bnxt_reuse_rx_data(rxr, cons, data);
1089 		return NULL;
1090 	}
1091 	dma_addr -= bp->rx_dma_offset;
1092 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1093 				bp->rx_dir);
1094 	skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE);
1095 	if (!skb) {
1096 		page_pool_recycle_direct(rxr->page_pool, page);
1097 		return NULL;
1098 	}
1099 	skb_mark_for_recycle(skb);
1100 	skb_reserve(skb, bp->rx_offset);
1101 	__skb_put(skb, len);
1102 
1103 	return skb;
1104 }
1105 
1106 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1107 					struct bnxt_rx_ring_info *rxr,
1108 					u16 cons, void *data, u8 *data_ptr,
1109 					dma_addr_t dma_addr,
1110 					unsigned int offset_and_len)
1111 {
1112 	unsigned int payload = offset_and_len >> 16;
1113 	unsigned int len = offset_and_len & 0xffff;
1114 	skb_frag_t *frag;
1115 	struct page *page = data;
1116 	u16 prod = rxr->rx_prod;
1117 	struct sk_buff *skb;
1118 	int off, err;
1119 
1120 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1121 	if (unlikely(err)) {
1122 		bnxt_reuse_rx_data(rxr, cons, data);
1123 		return NULL;
1124 	}
1125 	dma_addr -= bp->rx_dma_offset;
1126 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1127 				bp->rx_dir);
1128 
1129 	if (unlikely(!payload))
1130 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1131 
1132 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1133 	if (!skb) {
1134 		page_pool_recycle_direct(rxr->page_pool, page);
1135 		return NULL;
1136 	}
1137 
1138 	skb_mark_for_recycle(skb);
1139 	off = (void *)data_ptr - page_address(page);
1140 	skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE);
1141 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1142 	       payload + NET_IP_ALIGN);
1143 
1144 	frag = &skb_shinfo(skb)->frags[0];
1145 	skb_frag_size_sub(frag, payload);
1146 	skb_frag_off_add(frag, payload);
1147 	skb->data_len -= payload;
1148 	skb->tail += payload;
1149 
1150 	return skb;
1151 }
1152 
1153 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1154 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1155 				   void *data, u8 *data_ptr,
1156 				   dma_addr_t dma_addr,
1157 				   unsigned int offset_and_len)
1158 {
1159 	u16 prod = rxr->rx_prod;
1160 	struct sk_buff *skb;
1161 	int err;
1162 
1163 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1164 	if (unlikely(err)) {
1165 		bnxt_reuse_rx_data(rxr, cons, data);
1166 		return NULL;
1167 	}
1168 
1169 	skb = napi_build_skb(data, bp->rx_buf_size);
1170 	dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1171 			       bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
1172 	if (!skb) {
1173 		skb_free_frag(data);
1174 		return NULL;
1175 	}
1176 
1177 	skb_reserve(skb, bp->rx_offset);
1178 	skb_put(skb, offset_and_len & 0xffff);
1179 	return skb;
1180 }
1181 
1182 static u32 __bnxt_rx_agg_pages(struct bnxt *bp,
1183 			       struct bnxt_cp_ring_info *cpr,
1184 			       struct skb_shared_info *shinfo,
1185 			       u16 idx, u32 agg_bufs, bool tpa,
1186 			       struct xdp_buff *xdp)
1187 {
1188 	struct bnxt_napi *bnapi = cpr->bnapi;
1189 	struct pci_dev *pdev = bp->pdev;
1190 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1191 	u16 prod = rxr->rx_agg_prod;
1192 	u32 i, total_frag_len = 0;
1193 	bool p5_tpa = false;
1194 
1195 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1196 		p5_tpa = true;
1197 
1198 	for (i = 0; i < agg_bufs; i++) {
1199 		skb_frag_t *frag = &shinfo->frags[i];
1200 		u16 cons, frag_len;
1201 		struct rx_agg_cmp *agg;
1202 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1203 		struct page *page;
1204 		dma_addr_t mapping;
1205 
1206 		if (p5_tpa)
1207 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1208 		else
1209 			agg = bnxt_get_agg(bp, cpr, idx, i);
1210 		cons = agg->rx_agg_cmp_opaque;
1211 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1212 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1213 
1214 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1215 		skb_frag_fill_page_desc(frag, cons_rx_buf->page,
1216 					cons_rx_buf->offset, frag_len);
1217 		shinfo->nr_frags = i + 1;
1218 		__clear_bit(cons, rxr->rx_agg_bmap);
1219 
1220 		/* It is possible for bnxt_alloc_rx_page() to allocate
1221 		 * a sw_prod index that equals the cons index, so we
1222 		 * need to clear the cons entry now.
1223 		 */
1224 		mapping = cons_rx_buf->mapping;
1225 		page = cons_rx_buf->page;
1226 		cons_rx_buf->page = NULL;
1227 
1228 		if (xdp && page_is_pfmemalloc(page))
1229 			xdp_buff_set_frag_pfmemalloc(xdp);
1230 
1231 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1232 			--shinfo->nr_frags;
1233 			cons_rx_buf->page = page;
1234 
1235 			/* Update prod since possibly some pages have been
1236 			 * allocated already.
1237 			 */
1238 			rxr->rx_agg_prod = prod;
1239 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1240 			return 0;
1241 		}
1242 
1243 		dma_sync_single_for_cpu(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1244 					bp->rx_dir);
1245 
1246 		total_frag_len += frag_len;
1247 		prod = NEXT_RX_AGG(prod);
1248 	}
1249 	rxr->rx_agg_prod = prod;
1250 	return total_frag_len;
1251 }
1252 
1253 static struct sk_buff *bnxt_rx_agg_pages_skb(struct bnxt *bp,
1254 					     struct bnxt_cp_ring_info *cpr,
1255 					     struct sk_buff *skb, u16 idx,
1256 					     u32 agg_bufs, bool tpa)
1257 {
1258 	struct skb_shared_info *shinfo = skb_shinfo(skb);
1259 	u32 total_frag_len = 0;
1260 
1261 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo, idx,
1262 					     agg_bufs, tpa, NULL);
1263 	if (!total_frag_len) {
1264 		skb_mark_for_recycle(skb);
1265 		dev_kfree_skb(skb);
1266 		return NULL;
1267 	}
1268 
1269 	skb->data_len += total_frag_len;
1270 	skb->len += total_frag_len;
1271 	skb->truesize += BNXT_RX_PAGE_SIZE * agg_bufs;
1272 	return skb;
1273 }
1274 
1275 static u32 bnxt_rx_agg_pages_xdp(struct bnxt *bp,
1276 				 struct bnxt_cp_ring_info *cpr,
1277 				 struct xdp_buff *xdp, u16 idx,
1278 				 u32 agg_bufs, bool tpa)
1279 {
1280 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1281 	u32 total_frag_len = 0;
1282 
1283 	if (!xdp_buff_has_frags(xdp))
1284 		shinfo->nr_frags = 0;
1285 
1286 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo,
1287 					     idx, agg_bufs, tpa, xdp);
1288 	if (total_frag_len) {
1289 		xdp_buff_set_frags_flag(xdp);
1290 		shinfo->nr_frags = agg_bufs;
1291 		shinfo->xdp_frags_size = total_frag_len;
1292 	}
1293 	return total_frag_len;
1294 }
1295 
1296 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1297 			       u8 agg_bufs, u32 *raw_cons)
1298 {
1299 	u16 last;
1300 	struct rx_agg_cmp *agg;
1301 
1302 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1303 	last = RING_CMP(*raw_cons);
1304 	agg = (struct rx_agg_cmp *)
1305 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1306 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1307 }
1308 
1309 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1310 				      unsigned int len,
1311 				      dma_addr_t mapping)
1312 {
1313 	struct bnxt *bp = bnapi->bp;
1314 	struct pci_dev *pdev = bp->pdev;
1315 	struct sk_buff *skb;
1316 
1317 	skb = napi_alloc_skb(&bnapi->napi, len);
1318 	if (!skb)
1319 		return NULL;
1320 
1321 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1322 				bp->rx_dir);
1323 
1324 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1325 	       len + NET_IP_ALIGN);
1326 
1327 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1328 				   bp->rx_dir);
1329 
1330 	skb_put(skb, len);
1331 
1332 	return skb;
1333 }
1334 
1335 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1336 				     unsigned int len,
1337 				     dma_addr_t mapping)
1338 {
1339 	return bnxt_copy_data(bnapi, data, len, mapping);
1340 }
1341 
1342 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1343 				     struct xdp_buff *xdp,
1344 				     unsigned int len,
1345 				     dma_addr_t mapping)
1346 {
1347 	unsigned int metasize = 0;
1348 	u8 *data = xdp->data;
1349 	struct sk_buff *skb;
1350 
1351 	len = xdp->data_end - xdp->data_meta;
1352 	metasize = xdp->data - xdp->data_meta;
1353 	data = xdp->data_meta;
1354 
1355 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1356 	if (!skb)
1357 		return skb;
1358 
1359 	if (metasize) {
1360 		skb_metadata_set(skb, metasize);
1361 		__skb_pull(skb, metasize);
1362 	}
1363 
1364 	return skb;
1365 }
1366 
1367 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1368 			   u32 *raw_cons, void *cmp)
1369 {
1370 	struct rx_cmp *rxcmp = cmp;
1371 	u32 tmp_raw_cons = *raw_cons;
1372 	u8 cmp_type, agg_bufs = 0;
1373 
1374 	cmp_type = RX_CMP_TYPE(rxcmp);
1375 
1376 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1377 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1378 			    RX_CMP_AGG_BUFS) >>
1379 			   RX_CMP_AGG_BUFS_SHIFT;
1380 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1381 		struct rx_tpa_end_cmp *tpa_end = cmp;
1382 
1383 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1384 			return 0;
1385 
1386 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1387 	}
1388 
1389 	if (agg_bufs) {
1390 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1391 			return -EBUSY;
1392 	}
1393 	*raw_cons = tmp_raw_cons;
1394 	return 0;
1395 }
1396 
1397 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1398 {
1399 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1400 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1401 
1402 	if (test_bit(idx, map->agg_idx_bmap))
1403 		idx = find_first_zero_bit(map->agg_idx_bmap,
1404 					  BNXT_AGG_IDX_BMAP_SIZE);
1405 	__set_bit(idx, map->agg_idx_bmap);
1406 	map->agg_id_tbl[agg_id] = idx;
1407 	return idx;
1408 }
1409 
1410 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1411 {
1412 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1413 
1414 	__clear_bit(idx, map->agg_idx_bmap);
1415 }
1416 
1417 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1418 {
1419 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1420 
1421 	return map->agg_id_tbl[agg_id];
1422 }
1423 
1424 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1425 			      struct rx_tpa_start_cmp *tpa_start,
1426 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1427 {
1428 	tpa_info->cfa_code_valid = 1;
1429 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1430 	tpa_info->vlan_valid = 0;
1431 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1432 		tpa_info->vlan_valid = 1;
1433 		tpa_info->metadata =
1434 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1435 	}
1436 }
1437 
1438 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1439 				 struct rx_tpa_start_cmp *tpa_start,
1440 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1441 {
1442 	tpa_info->vlan_valid = 0;
1443 	if (TPA_START_VLAN_VALID(tpa_start)) {
1444 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1445 		u32 vlan_proto = ETH_P_8021Q;
1446 
1447 		tpa_info->vlan_valid = 1;
1448 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1449 			vlan_proto = ETH_P_8021AD;
1450 		tpa_info->metadata = vlan_proto << 16 |
1451 				     TPA_START_METADATA0_TCI(tpa_start1);
1452 	}
1453 }
1454 
1455 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1456 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1457 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1458 {
1459 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1460 	struct bnxt_tpa_info *tpa_info;
1461 	u16 cons, prod, agg_id;
1462 	struct rx_bd *prod_bd;
1463 	dma_addr_t mapping;
1464 
1465 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1466 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1467 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1468 	} else {
1469 		agg_id = TPA_START_AGG_ID(tpa_start);
1470 	}
1471 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1472 	prod = rxr->rx_prod;
1473 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1474 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1475 	tpa_info = &rxr->rx_tpa[agg_id];
1476 
1477 	if (unlikely(cons != rxr->rx_next_cons ||
1478 		     TPA_START_ERROR(tpa_start))) {
1479 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1480 			    cons, rxr->rx_next_cons,
1481 			    TPA_START_ERROR_CODE(tpa_start1));
1482 		bnxt_sched_reset_rxr(bp, rxr);
1483 		return;
1484 	}
1485 	prod_rx_buf->data = tpa_info->data;
1486 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1487 
1488 	mapping = tpa_info->mapping;
1489 	prod_rx_buf->mapping = mapping;
1490 
1491 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1492 
1493 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1494 
1495 	tpa_info->data = cons_rx_buf->data;
1496 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1497 	cons_rx_buf->data = NULL;
1498 	tpa_info->mapping = cons_rx_buf->mapping;
1499 
1500 	tpa_info->len =
1501 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1502 				RX_TPA_START_CMP_LEN_SHIFT;
1503 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1504 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1505 		tpa_info->gso_type = SKB_GSO_TCPV4;
1506 		if (TPA_START_IS_IPV6(tpa_start1))
1507 			tpa_info->gso_type = SKB_GSO_TCPV6;
1508 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1509 		else if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP &&
1510 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1511 			tpa_info->gso_type = SKB_GSO_TCPV6;
1512 		tpa_info->rss_hash =
1513 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1514 	} else {
1515 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1516 		tpa_info->gso_type = 0;
1517 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1518 	}
1519 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1520 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1521 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1522 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1523 	else
1524 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1525 	tpa_info->agg_count = 0;
1526 
1527 	rxr->rx_prod = NEXT_RX(prod);
1528 	cons = RING_RX(bp, NEXT_RX(cons));
1529 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1530 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1531 
1532 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1533 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1534 	cons_rx_buf->data = NULL;
1535 }
1536 
1537 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1538 {
1539 	if (agg_bufs)
1540 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1541 }
1542 
1543 #ifdef CONFIG_INET
1544 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1545 {
1546 	struct udphdr *uh = NULL;
1547 
1548 	if (ip_proto == htons(ETH_P_IP)) {
1549 		struct iphdr *iph = (struct iphdr *)skb->data;
1550 
1551 		if (iph->protocol == IPPROTO_UDP)
1552 			uh = (struct udphdr *)(iph + 1);
1553 	} else {
1554 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1555 
1556 		if (iph->nexthdr == IPPROTO_UDP)
1557 			uh = (struct udphdr *)(iph + 1);
1558 	}
1559 	if (uh) {
1560 		if (uh->check)
1561 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1562 		else
1563 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1564 	}
1565 }
1566 #endif
1567 
1568 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1569 					   int payload_off, int tcp_ts,
1570 					   struct sk_buff *skb)
1571 {
1572 #ifdef CONFIG_INET
1573 	struct tcphdr *th;
1574 	int len, nw_off;
1575 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1576 	u32 hdr_info = tpa_info->hdr_info;
1577 	bool loopback = false;
1578 
1579 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1580 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1581 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1582 
1583 	/* If the packet is an internal loopback packet, the offsets will
1584 	 * have an extra 4 bytes.
1585 	 */
1586 	if (inner_mac_off == 4) {
1587 		loopback = true;
1588 	} else if (inner_mac_off > 4) {
1589 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1590 					    ETH_HLEN - 2));
1591 
1592 		/* We only support inner iPv4/ipv6.  If we don't see the
1593 		 * correct protocol ID, it must be a loopback packet where
1594 		 * the offsets are off by 4.
1595 		 */
1596 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1597 			loopback = true;
1598 	}
1599 	if (loopback) {
1600 		/* internal loopback packet, subtract all offsets by 4 */
1601 		inner_ip_off -= 4;
1602 		inner_mac_off -= 4;
1603 		outer_ip_off -= 4;
1604 	}
1605 
1606 	nw_off = inner_ip_off - ETH_HLEN;
1607 	skb_set_network_header(skb, nw_off);
1608 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1609 		struct ipv6hdr *iph = ipv6_hdr(skb);
1610 
1611 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1612 		len = skb->len - skb_transport_offset(skb);
1613 		th = tcp_hdr(skb);
1614 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1615 	} else {
1616 		struct iphdr *iph = ip_hdr(skb);
1617 
1618 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1619 		len = skb->len - skb_transport_offset(skb);
1620 		th = tcp_hdr(skb);
1621 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1622 	}
1623 
1624 	if (inner_mac_off) { /* tunnel */
1625 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1626 					    ETH_HLEN - 2));
1627 
1628 		bnxt_gro_tunnel(skb, proto);
1629 	}
1630 #endif
1631 	return skb;
1632 }
1633 
1634 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1635 					   int payload_off, int tcp_ts,
1636 					   struct sk_buff *skb)
1637 {
1638 #ifdef CONFIG_INET
1639 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1640 	u32 hdr_info = tpa_info->hdr_info;
1641 	int iphdr_len, nw_off;
1642 
1643 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1644 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1645 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1646 
1647 	nw_off = inner_ip_off - ETH_HLEN;
1648 	skb_set_network_header(skb, nw_off);
1649 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1650 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1651 	skb_set_transport_header(skb, nw_off + iphdr_len);
1652 
1653 	if (inner_mac_off) { /* tunnel */
1654 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1655 					    ETH_HLEN - 2));
1656 
1657 		bnxt_gro_tunnel(skb, proto);
1658 	}
1659 #endif
1660 	return skb;
1661 }
1662 
1663 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1664 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1665 
1666 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1667 					   int payload_off, int tcp_ts,
1668 					   struct sk_buff *skb)
1669 {
1670 #ifdef CONFIG_INET
1671 	struct tcphdr *th;
1672 	int len, nw_off, tcp_opt_len = 0;
1673 
1674 	if (tcp_ts)
1675 		tcp_opt_len = 12;
1676 
1677 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1678 		struct iphdr *iph;
1679 
1680 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1681 			 ETH_HLEN;
1682 		skb_set_network_header(skb, nw_off);
1683 		iph = ip_hdr(skb);
1684 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1685 		len = skb->len - skb_transport_offset(skb);
1686 		th = tcp_hdr(skb);
1687 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1688 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1689 		struct ipv6hdr *iph;
1690 
1691 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1692 			 ETH_HLEN;
1693 		skb_set_network_header(skb, nw_off);
1694 		iph = ipv6_hdr(skb);
1695 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1696 		len = skb->len - skb_transport_offset(skb);
1697 		th = tcp_hdr(skb);
1698 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1699 	} else {
1700 		dev_kfree_skb_any(skb);
1701 		return NULL;
1702 	}
1703 
1704 	if (nw_off) /* tunnel */
1705 		bnxt_gro_tunnel(skb, skb->protocol);
1706 #endif
1707 	return skb;
1708 }
1709 
1710 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1711 					   struct bnxt_tpa_info *tpa_info,
1712 					   struct rx_tpa_end_cmp *tpa_end,
1713 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1714 					   struct sk_buff *skb)
1715 {
1716 #ifdef CONFIG_INET
1717 	int payload_off;
1718 	u16 segs;
1719 
1720 	segs = TPA_END_TPA_SEGS(tpa_end);
1721 	if (segs == 1)
1722 		return skb;
1723 
1724 	NAPI_GRO_CB(skb)->count = segs;
1725 	skb_shinfo(skb)->gso_size =
1726 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1727 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1728 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1729 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1730 	else
1731 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1732 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1733 	if (likely(skb))
1734 		tcp_gro_complete(skb);
1735 #endif
1736 	return skb;
1737 }
1738 
1739 /* Given the cfa_code of a received packet determine which
1740  * netdev (vf-rep or PF) the packet is destined to.
1741  */
1742 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1743 {
1744 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1745 
1746 	/* if vf-rep dev is NULL, the must belongs to the PF */
1747 	return dev ? dev : bp->dev;
1748 }
1749 
1750 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1751 					   struct bnxt_cp_ring_info *cpr,
1752 					   u32 *raw_cons,
1753 					   struct rx_tpa_end_cmp *tpa_end,
1754 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1755 					   u8 *event)
1756 {
1757 	struct bnxt_napi *bnapi = cpr->bnapi;
1758 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1759 	struct net_device *dev = bp->dev;
1760 	u8 *data_ptr, agg_bufs;
1761 	unsigned int len;
1762 	struct bnxt_tpa_info *tpa_info;
1763 	dma_addr_t mapping;
1764 	struct sk_buff *skb;
1765 	u16 idx = 0, agg_id;
1766 	void *data;
1767 	bool gro;
1768 
1769 	if (unlikely(bnapi->in_reset)) {
1770 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1771 
1772 		if (rc < 0)
1773 			return ERR_PTR(-EBUSY);
1774 		return NULL;
1775 	}
1776 
1777 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1778 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1779 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1780 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1781 		tpa_info = &rxr->rx_tpa[agg_id];
1782 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1783 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1784 				    agg_bufs, tpa_info->agg_count);
1785 			agg_bufs = tpa_info->agg_count;
1786 		}
1787 		tpa_info->agg_count = 0;
1788 		*event |= BNXT_AGG_EVENT;
1789 		bnxt_free_agg_idx(rxr, agg_id);
1790 		idx = agg_id;
1791 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1792 	} else {
1793 		agg_id = TPA_END_AGG_ID(tpa_end);
1794 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1795 		tpa_info = &rxr->rx_tpa[agg_id];
1796 		idx = RING_CMP(*raw_cons);
1797 		if (agg_bufs) {
1798 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1799 				return ERR_PTR(-EBUSY);
1800 
1801 			*event |= BNXT_AGG_EVENT;
1802 			idx = NEXT_CMP(idx);
1803 		}
1804 		gro = !!TPA_END_GRO(tpa_end);
1805 	}
1806 	data = tpa_info->data;
1807 	data_ptr = tpa_info->data_ptr;
1808 	prefetch(data_ptr);
1809 	len = tpa_info->len;
1810 	mapping = tpa_info->mapping;
1811 
1812 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1813 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1814 		if (agg_bufs > MAX_SKB_FRAGS)
1815 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1816 				    agg_bufs, (int)MAX_SKB_FRAGS);
1817 		return NULL;
1818 	}
1819 
1820 	if (len <= bp->rx_copy_thresh) {
1821 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1822 		if (!skb) {
1823 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1824 			cpr->sw_stats->rx.rx_oom_discards += 1;
1825 			return NULL;
1826 		}
1827 	} else {
1828 		u8 *new_data;
1829 		dma_addr_t new_mapping;
1830 
1831 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, GFP_ATOMIC);
1832 		if (!new_data) {
1833 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1834 			cpr->sw_stats->rx.rx_oom_discards += 1;
1835 			return NULL;
1836 		}
1837 
1838 		tpa_info->data = new_data;
1839 		tpa_info->data_ptr = new_data + bp->rx_offset;
1840 		tpa_info->mapping = new_mapping;
1841 
1842 		skb = napi_build_skb(data, bp->rx_buf_size);
1843 		dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1844 				       bp->rx_buf_use_size, bp->rx_dir,
1845 				       DMA_ATTR_WEAK_ORDERING);
1846 
1847 		if (!skb) {
1848 			skb_free_frag(data);
1849 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1850 			cpr->sw_stats->rx.rx_oom_discards += 1;
1851 			return NULL;
1852 		}
1853 		skb_reserve(skb, bp->rx_offset);
1854 		skb_put(skb, len);
1855 	}
1856 
1857 	if (agg_bufs) {
1858 		skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, idx, agg_bufs, true);
1859 		if (!skb) {
1860 			/* Page reuse already handled by bnxt_rx_pages(). */
1861 			cpr->sw_stats->rx.rx_oom_discards += 1;
1862 			return NULL;
1863 		}
1864 	}
1865 
1866 	if (tpa_info->cfa_code_valid)
1867 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1868 	skb->protocol = eth_type_trans(skb, dev);
1869 
1870 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1871 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1872 
1873 	if (tpa_info->vlan_valid &&
1874 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1875 		__be16 vlan_proto = htons(tpa_info->metadata >>
1876 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1877 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1878 
1879 		if (eth_type_vlan(vlan_proto)) {
1880 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1881 		} else {
1882 			dev_kfree_skb(skb);
1883 			return NULL;
1884 		}
1885 	}
1886 
1887 	skb_checksum_none_assert(skb);
1888 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1889 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1890 		skb->csum_level =
1891 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1892 	}
1893 
1894 	if (gro)
1895 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1896 
1897 	return skb;
1898 }
1899 
1900 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1901 			 struct rx_agg_cmp *rx_agg)
1902 {
1903 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1904 	struct bnxt_tpa_info *tpa_info;
1905 
1906 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1907 	tpa_info = &rxr->rx_tpa[agg_id];
1908 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1909 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1910 }
1911 
1912 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1913 			     struct sk_buff *skb)
1914 {
1915 	skb_mark_for_recycle(skb);
1916 
1917 	if (skb->dev != bp->dev) {
1918 		/* this packet belongs to a vf-rep */
1919 		bnxt_vf_rep_rx(bp, skb);
1920 		return;
1921 	}
1922 	skb_record_rx_queue(skb, bnapi->index);
1923 	napi_gro_receive(&bnapi->napi, skb);
1924 }
1925 
1926 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
1927 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
1928 {
1929 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
1930 
1931 	if (BNXT_PTP_RX_TS_VALID(flags))
1932 		goto ts_valid;
1933 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
1934 		return false;
1935 
1936 ts_valid:
1937 	*cmpl_ts = ts;
1938 	return true;
1939 }
1940 
1941 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
1942 				    struct rx_cmp *rxcmp,
1943 				    struct rx_cmp_ext *rxcmp1)
1944 {
1945 	__be16 vlan_proto;
1946 	u16 vtag;
1947 
1948 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1949 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
1950 		u32 meta_data;
1951 
1952 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
1953 			return skb;
1954 
1955 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1956 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1957 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
1958 		if (eth_type_vlan(vlan_proto))
1959 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1960 		else
1961 			goto vlan_err;
1962 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
1963 		if (RX_CMP_VLAN_VALID(rxcmp)) {
1964 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
1965 
1966 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
1967 				vlan_proto = htons(ETH_P_8021Q);
1968 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
1969 				vlan_proto = htons(ETH_P_8021AD);
1970 			else
1971 				goto vlan_err;
1972 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
1973 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1974 		}
1975 	}
1976 	return skb;
1977 vlan_err:
1978 	dev_kfree_skb(skb);
1979 	return NULL;
1980 }
1981 
1982 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp,
1983 					   struct rx_cmp *rxcmp)
1984 {
1985 	u8 ext_op;
1986 
1987 	ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp);
1988 	switch (ext_op) {
1989 	case EXT_OP_INNER_4:
1990 	case EXT_OP_OUTER_4:
1991 	case EXT_OP_INNFL_3:
1992 	case EXT_OP_OUTFL_3:
1993 		return PKT_HASH_TYPE_L4;
1994 	default:
1995 		return PKT_HASH_TYPE_L3;
1996 	}
1997 }
1998 
1999 /* returns the following:
2000  * 1       - 1 packet successfully received
2001  * 0       - successful TPA_START, packet not completed yet
2002  * -EBUSY  - completion ring does not have all the agg buffers yet
2003  * -ENOMEM - packet aborted due to out of memory
2004  * -EIO    - packet aborted due to hw error indicated in BD
2005  */
2006 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2007 		       u32 *raw_cons, u8 *event)
2008 {
2009 	struct bnxt_napi *bnapi = cpr->bnapi;
2010 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2011 	struct net_device *dev = bp->dev;
2012 	struct rx_cmp *rxcmp;
2013 	struct rx_cmp_ext *rxcmp1;
2014 	u32 tmp_raw_cons = *raw_cons;
2015 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2016 	struct bnxt_sw_rx_bd *rx_buf;
2017 	unsigned int len;
2018 	u8 *data_ptr, agg_bufs, cmp_type;
2019 	bool xdp_active = false;
2020 	dma_addr_t dma_addr;
2021 	struct sk_buff *skb;
2022 	struct xdp_buff xdp;
2023 	u32 flags, misc;
2024 	u32 cmpl_ts;
2025 	void *data;
2026 	int rc = 0;
2027 
2028 	rxcmp = (struct rx_cmp *)
2029 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2030 
2031 	cmp_type = RX_CMP_TYPE(rxcmp);
2032 
2033 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2034 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2035 		goto next_rx_no_prod_no_len;
2036 	}
2037 
2038 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2039 	cp_cons = RING_CMP(tmp_raw_cons);
2040 	rxcmp1 = (struct rx_cmp_ext *)
2041 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2042 
2043 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2044 		return -EBUSY;
2045 
2046 	/* The valid test of the entry must be done first before
2047 	 * reading any further.
2048 	 */
2049 	dma_rmb();
2050 	prod = rxr->rx_prod;
2051 
2052 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2053 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2054 		bnxt_tpa_start(bp, rxr, cmp_type,
2055 			       (struct rx_tpa_start_cmp *)rxcmp,
2056 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2057 
2058 		*event |= BNXT_RX_EVENT;
2059 		goto next_rx_no_prod_no_len;
2060 
2061 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2062 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2063 				   (struct rx_tpa_end_cmp *)rxcmp,
2064 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2065 
2066 		if (IS_ERR(skb))
2067 			return -EBUSY;
2068 
2069 		rc = -ENOMEM;
2070 		if (likely(skb)) {
2071 			bnxt_deliver_skb(bp, bnapi, skb);
2072 			rc = 1;
2073 		}
2074 		*event |= BNXT_RX_EVENT;
2075 		goto next_rx_no_prod_no_len;
2076 	}
2077 
2078 	cons = rxcmp->rx_cmp_opaque;
2079 	if (unlikely(cons != rxr->rx_next_cons)) {
2080 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2081 
2082 		/* 0xffff is forced error, don't print it */
2083 		if (rxr->rx_next_cons != 0xffff)
2084 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2085 				    cons, rxr->rx_next_cons);
2086 		bnxt_sched_reset_rxr(bp, rxr);
2087 		if (rc1)
2088 			return rc1;
2089 		goto next_rx_no_prod_no_len;
2090 	}
2091 	rx_buf = &rxr->rx_buf_ring[cons];
2092 	data = rx_buf->data;
2093 	data_ptr = rx_buf->data_ptr;
2094 	prefetch(data_ptr);
2095 
2096 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2097 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2098 
2099 	if (agg_bufs) {
2100 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2101 			return -EBUSY;
2102 
2103 		cp_cons = NEXT_CMP(cp_cons);
2104 		*event |= BNXT_AGG_EVENT;
2105 	}
2106 	*event |= BNXT_RX_EVENT;
2107 
2108 	rx_buf->data = NULL;
2109 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2110 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2111 
2112 		bnxt_reuse_rx_data(rxr, cons, data);
2113 		if (agg_bufs)
2114 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2115 					       false);
2116 
2117 		rc = -EIO;
2118 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2119 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2120 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2121 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2122 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2123 						 rx_err);
2124 				bnxt_sched_reset_rxr(bp, rxr);
2125 			}
2126 		}
2127 		goto next_rx_no_len;
2128 	}
2129 
2130 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2131 	len = flags >> RX_CMP_LEN_SHIFT;
2132 	dma_addr = rx_buf->mapping;
2133 
2134 	if (bnxt_xdp_attached(bp, rxr)) {
2135 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2136 		if (agg_bufs) {
2137 			u32 frag_len = bnxt_rx_agg_pages_xdp(bp, cpr, &xdp,
2138 							     cp_cons, agg_bufs,
2139 							     false);
2140 			if (!frag_len)
2141 				goto oom_next_rx;
2142 		}
2143 		xdp_active = true;
2144 	}
2145 
2146 	if (xdp_active) {
2147 		if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) {
2148 			rc = 1;
2149 			goto next_rx;
2150 		}
2151 	}
2152 
2153 	if (len <= bp->rx_copy_thresh) {
2154 		if (!xdp_active)
2155 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2156 		else
2157 			skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr);
2158 		bnxt_reuse_rx_data(rxr, cons, data);
2159 		if (!skb) {
2160 			if (agg_bufs) {
2161 				if (!xdp_active)
2162 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2163 							       agg_bufs, false);
2164 				else
2165 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2166 			}
2167 			goto oom_next_rx;
2168 		}
2169 	} else {
2170 		u32 payload;
2171 
2172 		if (rx_buf->data_ptr == data_ptr)
2173 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2174 		else
2175 			payload = 0;
2176 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2177 				      payload | len);
2178 		if (!skb)
2179 			goto oom_next_rx;
2180 	}
2181 
2182 	if (agg_bufs) {
2183 		if (!xdp_active) {
2184 			skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, cp_cons, agg_bufs, false);
2185 			if (!skb)
2186 				goto oom_next_rx;
2187 		} else {
2188 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, rxr->page_pool, &xdp, rxcmp1);
2189 			if (!skb) {
2190 				/* we should be able to free the old skb here */
2191 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2192 				goto oom_next_rx;
2193 			}
2194 		}
2195 	}
2196 
2197 	if (RX_CMP_HASH_VALID(rxcmp)) {
2198 		enum pkt_hash_types type;
2199 
2200 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2201 			type = bnxt_rss_ext_op(bp, rxcmp);
2202 		} else {
2203 			u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
2204 
2205 			/* RSS profiles 1 and 3 with extract code 0 for inner
2206 			 * 4-tuple
2207 			 */
2208 			if (hash_type != 1 && hash_type != 3)
2209 				type = PKT_HASH_TYPE_L3;
2210 			else
2211 				type = PKT_HASH_TYPE_L4;
2212 		}
2213 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2214 	}
2215 
2216 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2217 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2218 	skb->protocol = eth_type_trans(skb, dev);
2219 
2220 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2221 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2222 		if (!skb)
2223 			goto next_rx;
2224 	}
2225 
2226 	skb_checksum_none_assert(skb);
2227 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2228 		if (dev->features & NETIF_F_RXCSUM) {
2229 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2230 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2231 		}
2232 	} else {
2233 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2234 			if (dev->features & NETIF_F_RXCSUM)
2235 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2236 		}
2237 	}
2238 
2239 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2240 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2241 			u64 ns, ts;
2242 
2243 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2244 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2245 
2246 				spin_lock_bh(&ptp->ptp_lock);
2247 				ns = timecounter_cyc2time(&ptp->tc, ts);
2248 				spin_unlock_bh(&ptp->ptp_lock);
2249 				memset(skb_hwtstamps(skb), 0,
2250 				       sizeof(*skb_hwtstamps(skb)));
2251 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2252 			}
2253 		}
2254 	}
2255 	bnxt_deliver_skb(bp, bnapi, skb);
2256 	rc = 1;
2257 
2258 next_rx:
2259 	cpr->rx_packets += 1;
2260 	cpr->rx_bytes += len;
2261 
2262 next_rx_no_len:
2263 	rxr->rx_prod = NEXT_RX(prod);
2264 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2265 
2266 next_rx_no_prod_no_len:
2267 	*raw_cons = tmp_raw_cons;
2268 
2269 	return rc;
2270 
2271 oom_next_rx:
2272 	cpr->sw_stats->rx.rx_oom_discards += 1;
2273 	rc = -ENOMEM;
2274 	goto next_rx;
2275 }
2276 
2277 /* In netpoll mode, if we are using a combined completion ring, we need to
2278  * discard the rx packets and recycle the buffers.
2279  */
2280 static int bnxt_force_rx_discard(struct bnxt *bp,
2281 				 struct bnxt_cp_ring_info *cpr,
2282 				 u32 *raw_cons, u8 *event)
2283 {
2284 	u32 tmp_raw_cons = *raw_cons;
2285 	struct rx_cmp_ext *rxcmp1;
2286 	struct rx_cmp *rxcmp;
2287 	u16 cp_cons;
2288 	u8 cmp_type;
2289 	int rc;
2290 
2291 	cp_cons = RING_CMP(tmp_raw_cons);
2292 	rxcmp = (struct rx_cmp *)
2293 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2294 
2295 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2296 	cp_cons = RING_CMP(tmp_raw_cons);
2297 	rxcmp1 = (struct rx_cmp_ext *)
2298 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2299 
2300 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2301 		return -EBUSY;
2302 
2303 	/* The valid test of the entry must be done first before
2304 	 * reading any further.
2305 	 */
2306 	dma_rmb();
2307 	cmp_type = RX_CMP_TYPE(rxcmp);
2308 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2309 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2310 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2311 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2312 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2313 		struct rx_tpa_end_cmp_ext *tpa_end1;
2314 
2315 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2316 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2317 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2318 	}
2319 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2320 	if (rc && rc != -EBUSY)
2321 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2322 	return rc;
2323 }
2324 
2325 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2326 {
2327 	struct bnxt_fw_health *fw_health = bp->fw_health;
2328 	u32 reg = fw_health->regs[reg_idx];
2329 	u32 reg_type, reg_off, val = 0;
2330 
2331 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2332 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2333 	switch (reg_type) {
2334 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2335 		pci_read_config_dword(bp->pdev, reg_off, &val);
2336 		break;
2337 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2338 		reg_off = fw_health->mapped_regs[reg_idx];
2339 		fallthrough;
2340 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2341 		val = readl(bp->bar0 + reg_off);
2342 		break;
2343 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2344 		val = readl(bp->bar1 + reg_off);
2345 		break;
2346 	}
2347 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2348 		val &= fw_health->fw_reset_inprog_reg_mask;
2349 	return val;
2350 }
2351 
2352 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2353 {
2354 	int i;
2355 
2356 	for (i = 0; i < bp->rx_nr_rings; i++) {
2357 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2358 		struct bnxt_ring_grp_info *grp_info;
2359 
2360 		grp_info = &bp->grp_info[grp_idx];
2361 		if (grp_info->agg_fw_ring_id == ring_id)
2362 			return grp_idx;
2363 	}
2364 	return INVALID_HW_RING_ID;
2365 }
2366 
2367 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2368 {
2369 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2370 
2371 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2372 		return link_info->force_link_speed2;
2373 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2374 		return link_info->force_pam4_link_speed;
2375 	return link_info->force_link_speed;
2376 }
2377 
2378 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2379 {
2380 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2381 
2382 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2383 		link_info->req_link_speed = link_info->force_link_speed2;
2384 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2385 		switch (link_info->req_link_speed) {
2386 		case BNXT_LINK_SPEED_50GB_PAM4:
2387 		case BNXT_LINK_SPEED_100GB_PAM4:
2388 		case BNXT_LINK_SPEED_200GB_PAM4:
2389 		case BNXT_LINK_SPEED_400GB_PAM4:
2390 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2391 			break;
2392 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2393 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2394 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2395 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2396 			break;
2397 		default:
2398 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2399 		}
2400 		return;
2401 	}
2402 	link_info->req_link_speed = link_info->force_link_speed;
2403 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2404 	if (link_info->force_pam4_link_speed) {
2405 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2406 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2407 	}
2408 }
2409 
2410 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2411 {
2412 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2413 
2414 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2415 		link_info->advertising = link_info->auto_link_speeds2;
2416 		return;
2417 	}
2418 	link_info->advertising = link_info->auto_link_speeds;
2419 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2420 }
2421 
2422 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2423 {
2424 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2425 
2426 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2427 		if (link_info->req_link_speed != link_info->force_link_speed2)
2428 			return true;
2429 		return false;
2430 	}
2431 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2432 	    link_info->req_link_speed != link_info->force_link_speed)
2433 		return true;
2434 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2435 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2436 		return true;
2437 	return false;
2438 }
2439 
2440 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2441 {
2442 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2443 
2444 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2445 		if (link_info->advertising != link_info->auto_link_speeds2)
2446 			return true;
2447 		return false;
2448 	}
2449 	if (link_info->advertising != link_info->auto_link_speeds ||
2450 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2451 		return true;
2452 	return false;
2453 }
2454 
2455 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2456 	((data2) &							\
2457 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2458 
2459 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2460 	(((data2) &							\
2461 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2462 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2463 
2464 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2465 	((data1) &							\
2466 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2467 
2468 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2469 	(((data1) &							\
2470 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2471 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2472 
2473 /* Return true if the workqueue has to be scheduled */
2474 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2475 {
2476 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2477 
2478 	switch (err_type) {
2479 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2480 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2481 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2482 		break;
2483 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2484 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2485 		break;
2486 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2487 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2488 		break;
2489 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2490 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2491 		char *threshold_type;
2492 		bool notify = false;
2493 		char *dir_str;
2494 
2495 		switch (type) {
2496 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2497 			threshold_type = "warning";
2498 			break;
2499 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2500 			threshold_type = "critical";
2501 			break;
2502 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2503 			threshold_type = "fatal";
2504 			break;
2505 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2506 			threshold_type = "shutdown";
2507 			break;
2508 		default:
2509 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2510 			return false;
2511 		}
2512 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2513 			dir_str = "above";
2514 			notify = true;
2515 		} else {
2516 			dir_str = "below";
2517 		}
2518 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2519 			    dir_str, threshold_type);
2520 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2521 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2522 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2523 		if (notify) {
2524 			bp->thermal_threshold_type = type;
2525 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2526 			return true;
2527 		}
2528 		return false;
2529 	}
2530 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2531 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2532 		break;
2533 	default:
2534 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2535 			   err_type);
2536 		break;
2537 	}
2538 	return false;
2539 }
2540 
2541 #define BNXT_GET_EVENT_PORT(data)	\
2542 	((data) &			\
2543 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2544 
2545 #define BNXT_EVENT_RING_TYPE(data2)	\
2546 	((data2) &			\
2547 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2548 
2549 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2550 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2551 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2552 
2553 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2554 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2555 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2556 
2557 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2558 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2559 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2560 
2561 #define BNXT_PHC_BITS	48
2562 
2563 static int bnxt_async_event_process(struct bnxt *bp,
2564 				    struct hwrm_async_event_cmpl *cmpl)
2565 {
2566 	u16 event_id = le16_to_cpu(cmpl->event_id);
2567 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2568 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2569 
2570 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2571 		   event_id, data1, data2);
2572 
2573 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2574 	switch (event_id) {
2575 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2576 		struct bnxt_link_info *link_info = &bp->link_info;
2577 
2578 		if (BNXT_VF(bp))
2579 			goto async_event_process_exit;
2580 
2581 		/* print unsupported speed warning in forced speed mode only */
2582 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2583 		    (data1 & 0x20000)) {
2584 			u16 fw_speed = bnxt_get_force_speed(link_info);
2585 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2586 
2587 			if (speed != SPEED_UNKNOWN)
2588 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2589 					    speed);
2590 		}
2591 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2592 	}
2593 		fallthrough;
2594 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2595 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2596 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2597 		fallthrough;
2598 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2599 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2600 		break;
2601 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2602 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2603 		break;
2604 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2605 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2606 
2607 		if (BNXT_VF(bp))
2608 			break;
2609 
2610 		if (bp->pf.port_id != port_id)
2611 			break;
2612 
2613 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2614 		break;
2615 	}
2616 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2617 		if (BNXT_PF(bp))
2618 			goto async_event_process_exit;
2619 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2620 		break;
2621 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2622 		char *type_str = "Solicited";
2623 
2624 		if (!bp->fw_health)
2625 			goto async_event_process_exit;
2626 
2627 		bp->fw_reset_timestamp = jiffies;
2628 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2629 		if (!bp->fw_reset_min_dsecs)
2630 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2631 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2632 		if (!bp->fw_reset_max_dsecs)
2633 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2634 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2635 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2636 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2637 			type_str = "Fatal";
2638 			bp->fw_health->fatalities++;
2639 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2640 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2641 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2642 			type_str = "Non-fatal";
2643 			bp->fw_health->survivals++;
2644 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2645 		}
2646 		netif_warn(bp, hw, bp->dev,
2647 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2648 			   type_str, data1, data2,
2649 			   bp->fw_reset_min_dsecs * 100,
2650 			   bp->fw_reset_max_dsecs * 100);
2651 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2652 		break;
2653 	}
2654 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2655 		struct bnxt_fw_health *fw_health = bp->fw_health;
2656 		char *status_desc = "healthy";
2657 		u32 status;
2658 
2659 		if (!fw_health)
2660 			goto async_event_process_exit;
2661 
2662 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2663 			fw_health->enabled = false;
2664 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2665 			break;
2666 		}
2667 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2668 		fw_health->tmr_multiplier =
2669 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2670 				     bp->current_interval * 10);
2671 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2672 		if (!fw_health->enabled)
2673 			fw_health->last_fw_heartbeat =
2674 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2675 		fw_health->last_fw_reset_cnt =
2676 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2677 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2678 		if (status != BNXT_FW_STATUS_HEALTHY)
2679 			status_desc = "unhealthy";
2680 		netif_info(bp, drv, bp->dev,
2681 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2682 			   fw_health->primary ? "primary" : "backup", status,
2683 			   status_desc, fw_health->last_fw_reset_cnt);
2684 		if (!fw_health->enabled) {
2685 			/* Make sure tmr_counter is set and visible to
2686 			 * bnxt_health_check() before setting enabled to true.
2687 			 */
2688 			smp_wmb();
2689 			fw_health->enabled = true;
2690 		}
2691 		goto async_event_process_exit;
2692 	}
2693 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2694 		netif_notice(bp, hw, bp->dev,
2695 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2696 			     data1, data2);
2697 		goto async_event_process_exit;
2698 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2699 		struct bnxt_rx_ring_info *rxr;
2700 		u16 grp_idx;
2701 
2702 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2703 			goto async_event_process_exit;
2704 
2705 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2706 			    BNXT_EVENT_RING_TYPE(data2), data1);
2707 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2708 			goto async_event_process_exit;
2709 
2710 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2711 		if (grp_idx == INVALID_HW_RING_ID) {
2712 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2713 				    data1);
2714 			goto async_event_process_exit;
2715 		}
2716 		rxr = bp->bnapi[grp_idx]->rx_ring;
2717 		bnxt_sched_reset_rxr(bp, rxr);
2718 		goto async_event_process_exit;
2719 	}
2720 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2721 		struct bnxt_fw_health *fw_health = bp->fw_health;
2722 
2723 		netif_notice(bp, hw, bp->dev,
2724 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2725 			     data1, data2);
2726 		if (fw_health) {
2727 			fw_health->echo_req_data1 = data1;
2728 			fw_health->echo_req_data2 = data2;
2729 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2730 			break;
2731 		}
2732 		goto async_event_process_exit;
2733 	}
2734 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2735 		bnxt_ptp_pps_event(bp, data1, data2);
2736 		goto async_event_process_exit;
2737 	}
2738 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2739 		if (bnxt_event_error_report(bp, data1, data2))
2740 			break;
2741 		goto async_event_process_exit;
2742 	}
2743 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2744 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2745 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2746 			if (BNXT_PTP_USE_RTC(bp)) {
2747 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2748 				u64 ns;
2749 
2750 				if (!ptp)
2751 					goto async_event_process_exit;
2752 
2753 				spin_lock_bh(&ptp->ptp_lock);
2754 				bnxt_ptp_update_current_time(bp);
2755 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2756 				       BNXT_PHC_BITS) | ptp->current_time);
2757 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2758 				spin_unlock_bh(&ptp->ptp_lock);
2759 			}
2760 			break;
2761 		}
2762 		goto async_event_process_exit;
2763 	}
2764 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2765 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2766 
2767 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2768 		goto async_event_process_exit;
2769 	}
2770 	default:
2771 		goto async_event_process_exit;
2772 	}
2773 	__bnxt_queue_sp_work(bp);
2774 async_event_process_exit:
2775 	return 0;
2776 }
2777 
2778 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2779 {
2780 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2781 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2782 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2783 				(struct hwrm_fwd_req_cmpl *)txcmp;
2784 
2785 	switch (cmpl_type) {
2786 	case CMPL_BASE_TYPE_HWRM_DONE:
2787 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2788 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2789 		break;
2790 
2791 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2792 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2793 
2794 		if ((vf_id < bp->pf.first_vf_id) ||
2795 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2796 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2797 				   vf_id);
2798 			return -EINVAL;
2799 		}
2800 
2801 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2802 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2803 		break;
2804 
2805 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2806 		bnxt_async_event_process(bp,
2807 					 (struct hwrm_async_event_cmpl *)txcmp);
2808 		break;
2809 
2810 	default:
2811 		break;
2812 	}
2813 
2814 	return 0;
2815 }
2816 
2817 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2818 {
2819 	struct bnxt_napi *bnapi = dev_instance;
2820 	struct bnxt *bp = bnapi->bp;
2821 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2822 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2823 
2824 	cpr->event_ctr++;
2825 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2826 	napi_schedule(&bnapi->napi);
2827 	return IRQ_HANDLED;
2828 }
2829 
2830 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2831 {
2832 	u32 raw_cons = cpr->cp_raw_cons;
2833 	u16 cons = RING_CMP(raw_cons);
2834 	struct tx_cmp *txcmp;
2835 
2836 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2837 
2838 	return TX_CMP_VALID(txcmp, raw_cons);
2839 }
2840 
2841 static irqreturn_t bnxt_inta(int irq, void *dev_instance)
2842 {
2843 	struct bnxt_napi *bnapi = dev_instance;
2844 	struct bnxt *bp = bnapi->bp;
2845 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2846 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2847 	u32 int_status;
2848 
2849 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2850 
2851 	if (!bnxt_has_work(bp, cpr)) {
2852 		int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
2853 		/* return if erroneous interrupt */
2854 		if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
2855 			return IRQ_NONE;
2856 	}
2857 
2858 	/* disable ring IRQ */
2859 	BNXT_CP_DB_IRQ_DIS(cpr->cp_db.doorbell);
2860 
2861 	/* Return here if interrupt is shared and is disabled. */
2862 	if (unlikely(atomic_read(&bp->intr_sem) != 0))
2863 		return IRQ_HANDLED;
2864 
2865 	napi_schedule(&bnapi->napi);
2866 	return IRQ_HANDLED;
2867 }
2868 
2869 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2870 			    int budget)
2871 {
2872 	struct bnxt_napi *bnapi = cpr->bnapi;
2873 	u32 raw_cons = cpr->cp_raw_cons;
2874 	u32 cons;
2875 	int rx_pkts = 0;
2876 	u8 event = 0;
2877 	struct tx_cmp *txcmp;
2878 
2879 	cpr->has_more_work = 0;
2880 	cpr->had_work_done = 1;
2881 	while (1) {
2882 		u8 cmp_type;
2883 		int rc;
2884 
2885 		cons = RING_CMP(raw_cons);
2886 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2887 
2888 		if (!TX_CMP_VALID(txcmp, raw_cons))
2889 			break;
2890 
2891 		/* The valid test of the entry must be done first before
2892 		 * reading any further.
2893 		 */
2894 		dma_rmb();
2895 		cmp_type = TX_CMP_TYPE(txcmp);
2896 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
2897 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
2898 			u32 opaque = txcmp->tx_cmp_opaque;
2899 			struct bnxt_tx_ring_info *txr;
2900 			u16 tx_freed;
2901 
2902 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
2903 			event |= BNXT_TX_CMP_EVENT;
2904 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
2905 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
2906 			else
2907 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
2908 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
2909 				   bp->tx_ring_mask;
2910 			/* return full budget so NAPI will complete. */
2911 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
2912 				rx_pkts = budget;
2913 				raw_cons = NEXT_RAW_CMP(raw_cons);
2914 				if (budget)
2915 					cpr->has_more_work = 1;
2916 				break;
2917 			}
2918 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
2919 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2920 			if (likely(budget))
2921 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2922 			else
2923 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2924 							   &event);
2925 			if (likely(rc >= 0))
2926 				rx_pkts += rc;
2927 			/* Increment rx_pkts when rc is -ENOMEM to count towards
2928 			 * the NAPI budget.  Otherwise, we may potentially loop
2929 			 * here forever if we consistently cannot allocate
2930 			 * buffers.
2931 			 */
2932 			else if (rc == -ENOMEM && budget)
2933 				rx_pkts++;
2934 			else if (rc == -EBUSY)	/* partial completion */
2935 				break;
2936 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
2937 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
2938 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
2939 			bnxt_hwrm_handler(bp, txcmp);
2940 		}
2941 		raw_cons = NEXT_RAW_CMP(raw_cons);
2942 
2943 		if (rx_pkts && rx_pkts == budget) {
2944 			cpr->has_more_work = 1;
2945 			break;
2946 		}
2947 	}
2948 
2949 	if (event & BNXT_REDIRECT_EVENT)
2950 		xdp_do_flush();
2951 
2952 	if (event & BNXT_TX_EVENT) {
2953 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
2954 		u16 prod = txr->tx_prod;
2955 
2956 		/* Sync BD data before updating doorbell */
2957 		wmb();
2958 
2959 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
2960 	}
2961 
2962 	cpr->cp_raw_cons = raw_cons;
2963 	bnapi->events |= event;
2964 	return rx_pkts;
2965 }
2966 
2967 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
2968 				  int budget)
2969 {
2970 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
2971 		bnapi->tx_int(bp, bnapi, budget);
2972 
2973 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
2974 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2975 
2976 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2977 	}
2978 	if (bnapi->events & BNXT_AGG_EVENT) {
2979 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2980 
2981 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2982 	}
2983 	bnapi->events &= BNXT_TX_CMP_EVENT;
2984 }
2985 
2986 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2987 			  int budget)
2988 {
2989 	struct bnxt_napi *bnapi = cpr->bnapi;
2990 	int rx_pkts;
2991 
2992 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
2993 
2994 	/* ACK completion ring before freeing tx ring and producing new
2995 	 * buffers in rx/agg rings to prevent overflowing the completion
2996 	 * ring.
2997 	 */
2998 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
2999 
3000 	__bnxt_poll_work_done(bp, bnapi, budget);
3001 	return rx_pkts;
3002 }
3003 
3004 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3005 {
3006 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3007 	struct bnxt *bp = bnapi->bp;
3008 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3009 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3010 	struct tx_cmp *txcmp;
3011 	struct rx_cmp_ext *rxcmp1;
3012 	u32 cp_cons, tmp_raw_cons;
3013 	u32 raw_cons = cpr->cp_raw_cons;
3014 	bool flush_xdp = false;
3015 	u32 rx_pkts = 0;
3016 	u8 event = 0;
3017 
3018 	while (1) {
3019 		int rc;
3020 
3021 		cp_cons = RING_CMP(raw_cons);
3022 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3023 
3024 		if (!TX_CMP_VALID(txcmp, raw_cons))
3025 			break;
3026 
3027 		/* The valid test of the entry must be done first before
3028 		 * reading any further.
3029 		 */
3030 		dma_rmb();
3031 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3032 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3033 			cp_cons = RING_CMP(tmp_raw_cons);
3034 			rxcmp1 = (struct rx_cmp_ext *)
3035 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3036 
3037 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3038 				break;
3039 
3040 			/* force an error to recycle the buffer */
3041 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3042 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3043 
3044 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3045 			if (likely(rc == -EIO) && budget)
3046 				rx_pkts++;
3047 			else if (rc == -EBUSY)	/* partial completion */
3048 				break;
3049 			if (event & BNXT_REDIRECT_EVENT)
3050 				flush_xdp = true;
3051 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3052 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3053 			bnxt_hwrm_handler(bp, txcmp);
3054 		} else {
3055 			netdev_err(bp->dev,
3056 				   "Invalid completion received on special ring\n");
3057 		}
3058 		raw_cons = NEXT_RAW_CMP(raw_cons);
3059 
3060 		if (rx_pkts == budget)
3061 			break;
3062 	}
3063 
3064 	cpr->cp_raw_cons = raw_cons;
3065 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3066 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3067 
3068 	if (event & BNXT_AGG_EVENT)
3069 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3070 	if (flush_xdp)
3071 		xdp_do_flush();
3072 
3073 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3074 		napi_complete_done(napi, rx_pkts);
3075 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3076 	}
3077 	return rx_pkts;
3078 }
3079 
3080 static int bnxt_poll(struct napi_struct *napi, int budget)
3081 {
3082 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3083 	struct bnxt *bp = bnapi->bp;
3084 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3085 	int work_done = 0;
3086 
3087 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3088 		napi_complete(napi);
3089 		return 0;
3090 	}
3091 	while (1) {
3092 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3093 
3094 		if (work_done >= budget) {
3095 			if (!budget)
3096 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3097 			break;
3098 		}
3099 
3100 		if (!bnxt_has_work(bp, cpr)) {
3101 			if (napi_complete_done(napi, work_done))
3102 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3103 			break;
3104 		}
3105 	}
3106 	if (bp->flags & BNXT_FLAG_DIM) {
3107 		struct dim_sample dim_sample = {};
3108 
3109 		dim_update_sample(cpr->event_ctr,
3110 				  cpr->rx_packets,
3111 				  cpr->rx_bytes,
3112 				  &dim_sample);
3113 		net_dim(&cpr->dim, dim_sample);
3114 	}
3115 	return work_done;
3116 }
3117 
3118 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3119 {
3120 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3121 	int i, work_done = 0;
3122 
3123 	for (i = 0; i < cpr->cp_ring_count; i++) {
3124 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3125 
3126 		if (cpr2->had_nqe_notify) {
3127 			work_done += __bnxt_poll_work(bp, cpr2,
3128 						      budget - work_done);
3129 			cpr->has_more_work |= cpr2->has_more_work;
3130 		}
3131 	}
3132 	return work_done;
3133 }
3134 
3135 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3136 				 u64 dbr_type, int budget)
3137 {
3138 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3139 	int i;
3140 
3141 	for (i = 0; i < cpr->cp_ring_count; i++) {
3142 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3143 		struct bnxt_db_info *db;
3144 
3145 		if (cpr2->had_work_done) {
3146 			u32 tgl = 0;
3147 
3148 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3149 				cpr2->had_nqe_notify = 0;
3150 				tgl = cpr2->toggle;
3151 			}
3152 			db = &cpr2->cp_db;
3153 			bnxt_writeq(bp,
3154 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3155 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3156 				    db->doorbell);
3157 			cpr2->had_work_done = 0;
3158 		}
3159 	}
3160 	__bnxt_poll_work_done(bp, bnapi, budget);
3161 }
3162 
3163 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3164 {
3165 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3166 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3167 	struct bnxt_cp_ring_info *cpr_rx;
3168 	u32 raw_cons = cpr->cp_raw_cons;
3169 	struct bnxt *bp = bnapi->bp;
3170 	struct nqe_cn *nqcmp;
3171 	int work_done = 0;
3172 	u32 cons;
3173 
3174 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3175 		napi_complete(napi);
3176 		return 0;
3177 	}
3178 	if (cpr->has_more_work) {
3179 		cpr->has_more_work = 0;
3180 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3181 	}
3182 	while (1) {
3183 		u16 type;
3184 
3185 		cons = RING_CMP(raw_cons);
3186 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3187 
3188 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3189 			if (cpr->has_more_work)
3190 				break;
3191 
3192 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3193 					     budget);
3194 			cpr->cp_raw_cons = raw_cons;
3195 			if (napi_complete_done(napi, work_done))
3196 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3197 						  cpr->cp_raw_cons);
3198 			goto poll_done;
3199 		}
3200 
3201 		/* The valid test of the entry must be done first before
3202 		 * reading any further.
3203 		 */
3204 		dma_rmb();
3205 
3206 		type = le16_to_cpu(nqcmp->type);
3207 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3208 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3209 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3210 			struct bnxt_cp_ring_info *cpr2;
3211 
3212 			/* No more budget for RX work */
3213 			if (budget && work_done >= budget &&
3214 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3215 				break;
3216 
3217 			idx = BNXT_NQ_HDL_IDX(idx);
3218 			cpr2 = &cpr->cp_ring_arr[idx];
3219 			cpr2->had_nqe_notify = 1;
3220 			cpr2->toggle = NQE_CN_TOGGLE(type);
3221 			work_done += __bnxt_poll_work(bp, cpr2,
3222 						      budget - work_done);
3223 			cpr->has_more_work |= cpr2->has_more_work;
3224 		} else {
3225 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3226 		}
3227 		raw_cons = NEXT_RAW_CMP(raw_cons);
3228 	}
3229 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3230 	if (raw_cons != cpr->cp_raw_cons) {
3231 		cpr->cp_raw_cons = raw_cons;
3232 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3233 	}
3234 poll_done:
3235 	cpr_rx = &cpr->cp_ring_arr[0];
3236 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3237 	    (bp->flags & BNXT_FLAG_DIM)) {
3238 		struct dim_sample dim_sample = {};
3239 
3240 		dim_update_sample(cpr->event_ctr,
3241 				  cpr_rx->rx_packets,
3242 				  cpr_rx->rx_bytes,
3243 				  &dim_sample);
3244 		net_dim(&cpr->dim, dim_sample);
3245 	}
3246 	return work_done;
3247 }
3248 
3249 static void bnxt_free_tx_skbs(struct bnxt *bp)
3250 {
3251 	int i, max_idx;
3252 	struct pci_dev *pdev = bp->pdev;
3253 
3254 	if (!bp->tx_ring)
3255 		return;
3256 
3257 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3258 	for (i = 0; i < bp->tx_nr_rings; i++) {
3259 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3260 		int j;
3261 
3262 		if (!txr->tx_buf_ring)
3263 			continue;
3264 
3265 		for (j = 0; j < max_idx;) {
3266 			struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
3267 			struct sk_buff *skb;
3268 			int k, last;
3269 
3270 			if (i < bp->tx_nr_rings_xdp &&
3271 			    tx_buf->action == XDP_REDIRECT) {
3272 				dma_unmap_single(&pdev->dev,
3273 					dma_unmap_addr(tx_buf, mapping),
3274 					dma_unmap_len(tx_buf, len),
3275 					DMA_TO_DEVICE);
3276 				xdp_return_frame(tx_buf->xdpf);
3277 				tx_buf->action = 0;
3278 				tx_buf->xdpf = NULL;
3279 				j++;
3280 				continue;
3281 			}
3282 
3283 			skb = tx_buf->skb;
3284 			if (!skb) {
3285 				j++;
3286 				continue;
3287 			}
3288 
3289 			tx_buf->skb = NULL;
3290 
3291 			if (tx_buf->is_push) {
3292 				dev_kfree_skb(skb);
3293 				j += 2;
3294 				continue;
3295 			}
3296 
3297 			dma_unmap_single(&pdev->dev,
3298 					 dma_unmap_addr(tx_buf, mapping),
3299 					 skb_headlen(skb),
3300 					 DMA_TO_DEVICE);
3301 
3302 			last = tx_buf->nr_frags;
3303 			j += 2;
3304 			for (k = 0; k < last; k++, j++) {
3305 				int ring_idx = j & bp->tx_ring_mask;
3306 				skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
3307 
3308 				tx_buf = &txr->tx_buf_ring[ring_idx];
3309 				dma_unmap_page(
3310 					&pdev->dev,
3311 					dma_unmap_addr(tx_buf, mapping),
3312 					skb_frag_size(frag), DMA_TO_DEVICE);
3313 			}
3314 			dev_kfree_skb(skb);
3315 		}
3316 		netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
3317 	}
3318 }
3319 
3320 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, int ring_nr)
3321 {
3322 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
3323 	struct pci_dev *pdev = bp->pdev;
3324 	struct bnxt_tpa_idx_map *map;
3325 	int i, max_idx, max_agg_idx;
3326 
3327 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3328 	max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3329 	if (!rxr->rx_tpa)
3330 		goto skip_rx_tpa_free;
3331 
3332 	for (i = 0; i < bp->max_tpa; i++) {
3333 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3334 		u8 *data = tpa_info->data;
3335 
3336 		if (!data)
3337 			continue;
3338 
3339 		dma_unmap_single_attrs(&pdev->dev, tpa_info->mapping,
3340 				       bp->rx_buf_use_size, bp->rx_dir,
3341 				       DMA_ATTR_WEAK_ORDERING);
3342 
3343 		tpa_info->data = NULL;
3344 
3345 		skb_free_frag(data);
3346 	}
3347 
3348 skip_rx_tpa_free:
3349 	if (!rxr->rx_buf_ring)
3350 		goto skip_rx_buf_free;
3351 
3352 	for (i = 0; i < max_idx; i++) {
3353 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3354 		dma_addr_t mapping = rx_buf->mapping;
3355 		void *data = rx_buf->data;
3356 
3357 		if (!data)
3358 			continue;
3359 
3360 		rx_buf->data = NULL;
3361 		if (BNXT_RX_PAGE_MODE(bp)) {
3362 			page_pool_recycle_direct(rxr->page_pool, data);
3363 		} else {
3364 			dma_unmap_single_attrs(&pdev->dev, mapping,
3365 					       bp->rx_buf_use_size, bp->rx_dir,
3366 					       DMA_ATTR_WEAK_ORDERING);
3367 			skb_free_frag(data);
3368 		}
3369 	}
3370 
3371 skip_rx_buf_free:
3372 	if (!rxr->rx_agg_ring)
3373 		goto skip_rx_agg_free;
3374 
3375 	for (i = 0; i < max_agg_idx; i++) {
3376 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3377 		struct page *page = rx_agg_buf->page;
3378 
3379 		if (!page)
3380 			continue;
3381 
3382 		rx_agg_buf->page = NULL;
3383 		__clear_bit(i, rxr->rx_agg_bmap);
3384 
3385 		page_pool_recycle_direct(rxr->page_pool, page);
3386 	}
3387 
3388 skip_rx_agg_free:
3389 	map = rxr->rx_tpa_idx_map;
3390 	if (map)
3391 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3392 }
3393 
3394 static void bnxt_free_rx_skbs(struct bnxt *bp)
3395 {
3396 	int i;
3397 
3398 	if (!bp->rx_ring)
3399 		return;
3400 
3401 	for (i = 0; i < bp->rx_nr_rings; i++)
3402 		bnxt_free_one_rx_ring_skbs(bp, i);
3403 }
3404 
3405 static void bnxt_free_skbs(struct bnxt *bp)
3406 {
3407 	bnxt_free_tx_skbs(bp);
3408 	bnxt_free_rx_skbs(bp);
3409 }
3410 
3411 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3412 {
3413 	u8 init_val = ctxm->init_value;
3414 	u16 offset = ctxm->init_offset;
3415 	u8 *p2 = p;
3416 	int i;
3417 
3418 	if (!init_val)
3419 		return;
3420 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3421 		memset(p, init_val, len);
3422 		return;
3423 	}
3424 	for (i = 0; i < len; i += ctxm->entry_size)
3425 		*(p2 + i + offset) = init_val;
3426 }
3427 
3428 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3429 {
3430 	struct pci_dev *pdev = bp->pdev;
3431 	int i;
3432 
3433 	if (!rmem->pg_arr)
3434 		goto skip_pages;
3435 
3436 	for (i = 0; i < rmem->nr_pages; i++) {
3437 		if (!rmem->pg_arr[i])
3438 			continue;
3439 
3440 		dma_free_coherent(&pdev->dev, rmem->page_size,
3441 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3442 
3443 		rmem->pg_arr[i] = NULL;
3444 	}
3445 skip_pages:
3446 	if (rmem->pg_tbl) {
3447 		size_t pg_tbl_size = rmem->nr_pages * 8;
3448 
3449 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3450 			pg_tbl_size = rmem->page_size;
3451 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3452 				  rmem->pg_tbl, rmem->pg_tbl_map);
3453 		rmem->pg_tbl = NULL;
3454 	}
3455 	if (rmem->vmem_size && *rmem->vmem) {
3456 		vfree(*rmem->vmem);
3457 		*rmem->vmem = NULL;
3458 	}
3459 }
3460 
3461 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3462 {
3463 	struct pci_dev *pdev = bp->pdev;
3464 	u64 valid_bit = 0;
3465 	int i;
3466 
3467 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3468 		valid_bit = PTU_PTE_VALID;
3469 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3470 		size_t pg_tbl_size = rmem->nr_pages * 8;
3471 
3472 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3473 			pg_tbl_size = rmem->page_size;
3474 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3475 						  &rmem->pg_tbl_map,
3476 						  GFP_KERNEL);
3477 		if (!rmem->pg_tbl)
3478 			return -ENOMEM;
3479 	}
3480 
3481 	for (i = 0; i < rmem->nr_pages; i++) {
3482 		u64 extra_bits = valid_bit;
3483 
3484 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3485 						     rmem->page_size,
3486 						     &rmem->dma_arr[i],
3487 						     GFP_KERNEL);
3488 		if (!rmem->pg_arr[i])
3489 			return -ENOMEM;
3490 
3491 		if (rmem->ctx_mem)
3492 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3493 					  rmem->page_size);
3494 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3495 			if (i == rmem->nr_pages - 2 &&
3496 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3497 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3498 			else if (i == rmem->nr_pages - 1 &&
3499 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3500 				extra_bits |= PTU_PTE_LAST;
3501 			rmem->pg_tbl[i] =
3502 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3503 		}
3504 	}
3505 
3506 	if (rmem->vmem_size) {
3507 		*rmem->vmem = vzalloc(rmem->vmem_size);
3508 		if (!(*rmem->vmem))
3509 			return -ENOMEM;
3510 	}
3511 	return 0;
3512 }
3513 
3514 static void bnxt_free_tpa_info(struct bnxt *bp)
3515 {
3516 	int i, j;
3517 
3518 	for (i = 0; i < bp->rx_nr_rings; i++) {
3519 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3520 
3521 		kfree(rxr->rx_tpa_idx_map);
3522 		rxr->rx_tpa_idx_map = NULL;
3523 		if (rxr->rx_tpa) {
3524 			for (j = 0; j < bp->max_tpa; j++) {
3525 				kfree(rxr->rx_tpa[j].agg_arr);
3526 				rxr->rx_tpa[j].agg_arr = NULL;
3527 			}
3528 		}
3529 		kfree(rxr->rx_tpa);
3530 		rxr->rx_tpa = NULL;
3531 	}
3532 }
3533 
3534 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3535 {
3536 	int i, j;
3537 
3538 	bp->max_tpa = MAX_TPA;
3539 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3540 		if (!bp->max_tpa_v2)
3541 			return 0;
3542 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3543 	}
3544 
3545 	for (i = 0; i < bp->rx_nr_rings; i++) {
3546 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3547 		struct rx_agg_cmp *agg;
3548 
3549 		rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3550 				      GFP_KERNEL);
3551 		if (!rxr->rx_tpa)
3552 			return -ENOMEM;
3553 
3554 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3555 			continue;
3556 		for (j = 0; j < bp->max_tpa; j++) {
3557 			agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3558 			if (!agg)
3559 				return -ENOMEM;
3560 			rxr->rx_tpa[j].agg_arr = agg;
3561 		}
3562 		rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3563 					      GFP_KERNEL);
3564 		if (!rxr->rx_tpa_idx_map)
3565 			return -ENOMEM;
3566 	}
3567 	return 0;
3568 }
3569 
3570 static void bnxt_free_rx_rings(struct bnxt *bp)
3571 {
3572 	int i;
3573 
3574 	if (!bp->rx_ring)
3575 		return;
3576 
3577 	bnxt_free_tpa_info(bp);
3578 	for (i = 0; i < bp->rx_nr_rings; i++) {
3579 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3580 		struct bnxt_ring_struct *ring;
3581 
3582 		if (rxr->xdp_prog)
3583 			bpf_prog_put(rxr->xdp_prog);
3584 
3585 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3586 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3587 
3588 		page_pool_destroy(rxr->page_pool);
3589 		rxr->page_pool = NULL;
3590 
3591 		kfree(rxr->rx_agg_bmap);
3592 		rxr->rx_agg_bmap = NULL;
3593 
3594 		ring = &rxr->rx_ring_struct;
3595 		bnxt_free_ring(bp, &ring->ring_mem);
3596 
3597 		ring = &rxr->rx_agg_ring_struct;
3598 		bnxt_free_ring(bp, &ring->ring_mem);
3599 	}
3600 }
3601 
3602 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3603 				   struct bnxt_rx_ring_info *rxr,
3604 				   int numa_node)
3605 {
3606 	struct page_pool_params pp = { 0 };
3607 
3608 	pp.pool_size = bp->rx_agg_ring_size;
3609 	if (BNXT_RX_PAGE_MODE(bp))
3610 		pp.pool_size += bp->rx_ring_size;
3611 	pp.nid = numa_node;
3612 	pp.napi = &rxr->bnapi->napi;
3613 	pp.netdev = bp->dev;
3614 	pp.dev = &bp->pdev->dev;
3615 	pp.dma_dir = bp->rx_dir;
3616 	pp.max_len = PAGE_SIZE;
3617 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3618 
3619 	rxr->page_pool = page_pool_create(&pp);
3620 	if (IS_ERR(rxr->page_pool)) {
3621 		int err = PTR_ERR(rxr->page_pool);
3622 
3623 		rxr->page_pool = NULL;
3624 		return err;
3625 	}
3626 	return 0;
3627 }
3628 
3629 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3630 {
3631 	int numa_node = dev_to_node(&bp->pdev->dev);
3632 	int i, rc = 0, agg_rings = 0, cpu;
3633 
3634 	if (!bp->rx_ring)
3635 		return -ENOMEM;
3636 
3637 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3638 		agg_rings = 1;
3639 
3640 	for (i = 0; i < bp->rx_nr_rings; i++) {
3641 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3642 		struct bnxt_ring_struct *ring;
3643 		int cpu_node;
3644 
3645 		ring = &rxr->rx_ring_struct;
3646 
3647 		cpu = cpumask_local_spread(i, numa_node);
3648 		cpu_node = cpu_to_node(cpu);
3649 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3650 			   i, cpu_node);
3651 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3652 		if (rc)
3653 			return rc;
3654 
3655 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3656 		if (rc < 0)
3657 			return rc;
3658 
3659 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3660 						MEM_TYPE_PAGE_POOL,
3661 						rxr->page_pool);
3662 		if (rc) {
3663 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3664 			return rc;
3665 		}
3666 
3667 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3668 		if (rc)
3669 			return rc;
3670 
3671 		ring->grp_idx = i;
3672 		if (agg_rings) {
3673 			u16 mem_size;
3674 
3675 			ring = &rxr->rx_agg_ring_struct;
3676 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3677 			if (rc)
3678 				return rc;
3679 
3680 			ring->grp_idx = i;
3681 			rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3682 			mem_size = rxr->rx_agg_bmap_size / 8;
3683 			rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3684 			if (!rxr->rx_agg_bmap)
3685 				return -ENOMEM;
3686 		}
3687 	}
3688 	if (bp->flags & BNXT_FLAG_TPA)
3689 		rc = bnxt_alloc_tpa_info(bp);
3690 	return rc;
3691 }
3692 
3693 static void bnxt_free_tx_rings(struct bnxt *bp)
3694 {
3695 	int i;
3696 	struct pci_dev *pdev = bp->pdev;
3697 
3698 	if (!bp->tx_ring)
3699 		return;
3700 
3701 	for (i = 0; i < bp->tx_nr_rings; i++) {
3702 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3703 		struct bnxt_ring_struct *ring;
3704 
3705 		if (txr->tx_push) {
3706 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3707 					  txr->tx_push, txr->tx_push_mapping);
3708 			txr->tx_push = NULL;
3709 		}
3710 
3711 		ring = &txr->tx_ring_struct;
3712 
3713 		bnxt_free_ring(bp, &ring->ring_mem);
3714 	}
3715 }
3716 
3717 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3718 	((tc) * (bp)->tx_nr_rings_per_tc)
3719 
3720 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3721 	((tx) % (bp)->tx_nr_rings_per_tc)
3722 
3723 #define BNXT_RING_TO_TC(bp, tx)		\
3724 	((tx) / (bp)->tx_nr_rings_per_tc)
3725 
3726 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3727 {
3728 	int i, j, rc;
3729 	struct pci_dev *pdev = bp->pdev;
3730 
3731 	bp->tx_push_size = 0;
3732 	if (bp->tx_push_thresh) {
3733 		int push_size;
3734 
3735 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3736 					bp->tx_push_thresh);
3737 
3738 		if (push_size > 256) {
3739 			push_size = 0;
3740 			bp->tx_push_thresh = 0;
3741 		}
3742 
3743 		bp->tx_push_size = push_size;
3744 	}
3745 
3746 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3747 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3748 		struct bnxt_ring_struct *ring;
3749 		u8 qidx;
3750 
3751 		ring = &txr->tx_ring_struct;
3752 
3753 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3754 		if (rc)
3755 			return rc;
3756 
3757 		ring->grp_idx = txr->bnapi->index;
3758 		if (bp->tx_push_size) {
3759 			dma_addr_t mapping;
3760 
3761 			/* One pre-allocated DMA buffer to backup
3762 			 * TX push operation
3763 			 */
3764 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3765 						bp->tx_push_size,
3766 						&txr->tx_push_mapping,
3767 						GFP_KERNEL);
3768 
3769 			if (!txr->tx_push)
3770 				return -ENOMEM;
3771 
3772 			mapping = txr->tx_push_mapping +
3773 				sizeof(struct tx_push_bd);
3774 			txr->data_mapping = cpu_to_le64(mapping);
3775 		}
3776 		qidx = bp->tc_to_qidx[j];
3777 		ring->queue_id = bp->q_info[qidx].queue_id;
3778 		spin_lock_init(&txr->xdp_tx_lock);
3779 		if (i < bp->tx_nr_rings_xdp)
3780 			continue;
3781 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
3782 			j++;
3783 	}
3784 	return 0;
3785 }
3786 
3787 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
3788 {
3789 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3790 
3791 	kfree(cpr->cp_desc_ring);
3792 	cpr->cp_desc_ring = NULL;
3793 	ring->ring_mem.pg_arr = NULL;
3794 	kfree(cpr->cp_desc_mapping);
3795 	cpr->cp_desc_mapping = NULL;
3796 	ring->ring_mem.dma_arr = NULL;
3797 }
3798 
3799 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
3800 {
3801 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
3802 	if (!cpr->cp_desc_ring)
3803 		return -ENOMEM;
3804 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
3805 				       GFP_KERNEL);
3806 	if (!cpr->cp_desc_mapping)
3807 		return -ENOMEM;
3808 	return 0;
3809 }
3810 
3811 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
3812 {
3813 	int i;
3814 
3815 	if (!bp->bnapi)
3816 		return;
3817 	for (i = 0; i < bp->cp_nr_rings; i++) {
3818 		struct bnxt_napi *bnapi = bp->bnapi[i];
3819 
3820 		if (!bnapi)
3821 			continue;
3822 		bnxt_free_cp_arrays(&bnapi->cp_ring);
3823 	}
3824 }
3825 
3826 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
3827 {
3828 	int i, n = bp->cp_nr_pages;
3829 
3830 	for (i = 0; i < bp->cp_nr_rings; i++) {
3831 		struct bnxt_napi *bnapi = bp->bnapi[i];
3832 		int rc;
3833 
3834 		if (!bnapi)
3835 			continue;
3836 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
3837 		if (rc)
3838 			return rc;
3839 	}
3840 	return 0;
3841 }
3842 
3843 static void bnxt_free_cp_rings(struct bnxt *bp)
3844 {
3845 	int i;
3846 
3847 	if (!bp->bnapi)
3848 		return;
3849 
3850 	for (i = 0; i < bp->cp_nr_rings; i++) {
3851 		struct bnxt_napi *bnapi = bp->bnapi[i];
3852 		struct bnxt_cp_ring_info *cpr;
3853 		struct bnxt_ring_struct *ring;
3854 		int j;
3855 
3856 		if (!bnapi)
3857 			continue;
3858 
3859 		cpr = &bnapi->cp_ring;
3860 		ring = &cpr->cp_ring_struct;
3861 
3862 		bnxt_free_ring(bp, &ring->ring_mem);
3863 
3864 		if (!cpr->cp_ring_arr)
3865 			continue;
3866 
3867 		for (j = 0; j < cpr->cp_ring_count; j++) {
3868 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
3869 
3870 			ring = &cpr2->cp_ring_struct;
3871 			bnxt_free_ring(bp, &ring->ring_mem);
3872 			bnxt_free_cp_arrays(cpr2);
3873 		}
3874 		kfree(cpr->cp_ring_arr);
3875 		cpr->cp_ring_arr = NULL;
3876 		cpr->cp_ring_count = 0;
3877 	}
3878 }
3879 
3880 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
3881 				  struct bnxt_cp_ring_info *cpr)
3882 {
3883 	struct bnxt_ring_mem_info *rmem;
3884 	struct bnxt_ring_struct *ring;
3885 	int rc;
3886 
3887 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
3888 	if (rc) {
3889 		bnxt_free_cp_arrays(cpr);
3890 		return -ENOMEM;
3891 	}
3892 	ring = &cpr->cp_ring_struct;
3893 	rmem = &ring->ring_mem;
3894 	rmem->nr_pages = bp->cp_nr_pages;
3895 	rmem->page_size = HW_CMPD_RING_SIZE;
3896 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
3897 	rmem->dma_arr = cpr->cp_desc_mapping;
3898 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
3899 	rc = bnxt_alloc_ring(bp, rmem);
3900 	if (rc) {
3901 		bnxt_free_ring(bp, rmem);
3902 		bnxt_free_cp_arrays(cpr);
3903 	}
3904 	return rc;
3905 }
3906 
3907 static int bnxt_alloc_cp_rings(struct bnxt *bp)
3908 {
3909 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
3910 	int i, j, rc, ulp_msix;
3911 	int tcs = bp->num_tc;
3912 
3913 	if (!tcs)
3914 		tcs = 1;
3915 	ulp_msix = bnxt_get_ulp_msix_num(bp);
3916 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
3917 		struct bnxt_napi *bnapi = bp->bnapi[i];
3918 		struct bnxt_cp_ring_info *cpr, *cpr2;
3919 		struct bnxt_ring_struct *ring;
3920 		int cp_count = 0, k;
3921 		int rx = 0, tx = 0;
3922 
3923 		if (!bnapi)
3924 			continue;
3925 
3926 		cpr = &bnapi->cp_ring;
3927 		cpr->bnapi = bnapi;
3928 		ring = &cpr->cp_ring_struct;
3929 
3930 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3931 		if (rc)
3932 			return rc;
3933 
3934 		ring->map_idx = ulp_msix + i;
3935 
3936 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3937 			continue;
3938 
3939 		if (i < bp->rx_nr_rings) {
3940 			cp_count++;
3941 			rx = 1;
3942 		}
3943 		if (i < bp->tx_nr_rings_xdp) {
3944 			cp_count++;
3945 			tx = 1;
3946 		} else if ((sh && i < bp->tx_nr_rings) ||
3947 			 (!sh && i >= bp->rx_nr_rings)) {
3948 			cp_count += tcs;
3949 			tx = 1;
3950 		}
3951 
3952 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
3953 					   GFP_KERNEL);
3954 		if (!cpr->cp_ring_arr)
3955 			return -ENOMEM;
3956 		cpr->cp_ring_count = cp_count;
3957 
3958 		for (k = 0; k < cp_count; k++) {
3959 			cpr2 = &cpr->cp_ring_arr[k];
3960 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
3961 			if (rc)
3962 				return rc;
3963 			cpr2->bnapi = bnapi;
3964 			cpr2->sw_stats = cpr->sw_stats;
3965 			cpr2->cp_idx = k;
3966 			if (!k && rx) {
3967 				bp->rx_ring[i].rx_cpr = cpr2;
3968 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
3969 			} else {
3970 				int n, tc = k - rx;
3971 
3972 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
3973 				bp->tx_ring[n].tx_cpr = cpr2;
3974 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
3975 			}
3976 		}
3977 		if (tx)
3978 			j++;
3979 	}
3980 	return 0;
3981 }
3982 
3983 static void bnxt_init_ring_struct(struct bnxt *bp)
3984 {
3985 	int i, j;
3986 
3987 	for (i = 0; i < bp->cp_nr_rings; i++) {
3988 		struct bnxt_napi *bnapi = bp->bnapi[i];
3989 		struct bnxt_ring_mem_info *rmem;
3990 		struct bnxt_cp_ring_info *cpr;
3991 		struct bnxt_rx_ring_info *rxr;
3992 		struct bnxt_tx_ring_info *txr;
3993 		struct bnxt_ring_struct *ring;
3994 
3995 		if (!bnapi)
3996 			continue;
3997 
3998 		cpr = &bnapi->cp_ring;
3999 		ring = &cpr->cp_ring_struct;
4000 		rmem = &ring->ring_mem;
4001 		rmem->nr_pages = bp->cp_nr_pages;
4002 		rmem->page_size = HW_CMPD_RING_SIZE;
4003 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4004 		rmem->dma_arr = cpr->cp_desc_mapping;
4005 		rmem->vmem_size = 0;
4006 
4007 		rxr = bnapi->rx_ring;
4008 		if (!rxr)
4009 			goto skip_rx;
4010 
4011 		ring = &rxr->rx_ring_struct;
4012 		rmem = &ring->ring_mem;
4013 		rmem->nr_pages = bp->rx_nr_pages;
4014 		rmem->page_size = HW_RXBD_RING_SIZE;
4015 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4016 		rmem->dma_arr = rxr->rx_desc_mapping;
4017 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4018 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4019 
4020 		ring = &rxr->rx_agg_ring_struct;
4021 		rmem = &ring->ring_mem;
4022 		rmem->nr_pages = bp->rx_agg_nr_pages;
4023 		rmem->page_size = HW_RXBD_RING_SIZE;
4024 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4025 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4026 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4027 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4028 
4029 skip_rx:
4030 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4031 			ring = &txr->tx_ring_struct;
4032 			rmem = &ring->ring_mem;
4033 			rmem->nr_pages = bp->tx_nr_pages;
4034 			rmem->page_size = HW_TXBD_RING_SIZE;
4035 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4036 			rmem->dma_arr = txr->tx_desc_mapping;
4037 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4038 			rmem->vmem = (void **)&txr->tx_buf_ring;
4039 		}
4040 	}
4041 }
4042 
4043 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4044 {
4045 	int i;
4046 	u32 prod;
4047 	struct rx_bd **rx_buf_ring;
4048 
4049 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4050 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4051 		int j;
4052 		struct rx_bd *rxbd;
4053 
4054 		rxbd = rx_buf_ring[i];
4055 		if (!rxbd)
4056 			continue;
4057 
4058 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4059 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4060 			rxbd->rx_bd_opaque = prod;
4061 		}
4062 	}
4063 }
4064 
4065 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4066 {
4067 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4068 	struct net_device *dev = bp->dev;
4069 	u32 prod;
4070 	int i;
4071 
4072 	prod = rxr->rx_prod;
4073 	for (i = 0; i < bp->rx_ring_size; i++) {
4074 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4075 			netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
4076 				    ring_nr, i, bp->rx_ring_size);
4077 			break;
4078 		}
4079 		prod = NEXT_RX(prod);
4080 	}
4081 	rxr->rx_prod = prod;
4082 
4083 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4084 		return 0;
4085 
4086 	prod = rxr->rx_agg_prod;
4087 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4088 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
4089 			netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
4090 				    ring_nr, i, bp->rx_ring_size);
4091 			break;
4092 		}
4093 		prod = NEXT_RX_AGG(prod);
4094 	}
4095 	rxr->rx_agg_prod = prod;
4096 
4097 	if (rxr->rx_tpa) {
4098 		dma_addr_t mapping;
4099 		u8 *data;
4100 
4101 		for (i = 0; i < bp->max_tpa; i++) {
4102 			data = __bnxt_alloc_rx_frag(bp, &mapping, GFP_KERNEL);
4103 			if (!data)
4104 				return -ENOMEM;
4105 
4106 			rxr->rx_tpa[i].data = data;
4107 			rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4108 			rxr->rx_tpa[i].mapping = mapping;
4109 		}
4110 	}
4111 	return 0;
4112 }
4113 
4114 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4115 {
4116 	struct bnxt_rx_ring_info *rxr;
4117 	struct bnxt_ring_struct *ring;
4118 	u32 type;
4119 
4120 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4121 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4122 
4123 	if (NET_IP_ALIGN == 2)
4124 		type |= RX_BD_FLAGS_SOP;
4125 
4126 	rxr = &bp->rx_ring[ring_nr];
4127 	ring = &rxr->rx_ring_struct;
4128 	bnxt_init_rxbd_pages(ring, type);
4129 
4130 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4131 			     &rxr->bnapi->napi);
4132 
4133 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4134 		bpf_prog_add(bp->xdp_prog, 1);
4135 		rxr->xdp_prog = bp->xdp_prog;
4136 	}
4137 	ring->fw_ring_id = INVALID_HW_RING_ID;
4138 
4139 	ring = &rxr->rx_agg_ring_struct;
4140 	ring->fw_ring_id = INVALID_HW_RING_ID;
4141 
4142 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4143 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4144 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4145 
4146 		bnxt_init_rxbd_pages(ring, type);
4147 	}
4148 
4149 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4150 }
4151 
4152 static void bnxt_init_cp_rings(struct bnxt *bp)
4153 {
4154 	int i, j;
4155 
4156 	for (i = 0; i < bp->cp_nr_rings; i++) {
4157 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4158 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4159 
4160 		ring->fw_ring_id = INVALID_HW_RING_ID;
4161 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4162 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4163 		if (!cpr->cp_ring_arr)
4164 			continue;
4165 		for (j = 0; j < cpr->cp_ring_count; j++) {
4166 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4167 
4168 			ring = &cpr2->cp_ring_struct;
4169 			ring->fw_ring_id = INVALID_HW_RING_ID;
4170 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4171 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4172 		}
4173 	}
4174 }
4175 
4176 static int bnxt_init_rx_rings(struct bnxt *bp)
4177 {
4178 	int i, rc = 0;
4179 
4180 	if (BNXT_RX_PAGE_MODE(bp)) {
4181 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4182 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4183 	} else {
4184 		bp->rx_offset = BNXT_RX_OFFSET;
4185 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4186 	}
4187 
4188 	for (i = 0; i < bp->rx_nr_rings; i++) {
4189 		rc = bnxt_init_one_rx_ring(bp, i);
4190 		if (rc)
4191 			break;
4192 	}
4193 
4194 	return rc;
4195 }
4196 
4197 static int bnxt_init_tx_rings(struct bnxt *bp)
4198 {
4199 	u16 i;
4200 
4201 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4202 				   BNXT_MIN_TX_DESC_CNT);
4203 
4204 	for (i = 0; i < bp->tx_nr_rings; i++) {
4205 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4206 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4207 
4208 		ring->fw_ring_id = INVALID_HW_RING_ID;
4209 
4210 		if (i >= bp->tx_nr_rings_xdp)
4211 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4212 					     NETDEV_QUEUE_TYPE_TX,
4213 					     &txr->bnapi->napi);
4214 	}
4215 
4216 	return 0;
4217 }
4218 
4219 static void bnxt_free_ring_grps(struct bnxt *bp)
4220 {
4221 	kfree(bp->grp_info);
4222 	bp->grp_info = NULL;
4223 }
4224 
4225 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4226 {
4227 	int i;
4228 
4229 	if (irq_re_init) {
4230 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4231 				       sizeof(struct bnxt_ring_grp_info),
4232 				       GFP_KERNEL);
4233 		if (!bp->grp_info)
4234 			return -ENOMEM;
4235 	}
4236 	for (i = 0; i < bp->cp_nr_rings; i++) {
4237 		if (irq_re_init)
4238 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4239 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4240 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4241 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4242 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4243 	}
4244 	return 0;
4245 }
4246 
4247 static void bnxt_free_vnics(struct bnxt *bp)
4248 {
4249 	kfree(bp->vnic_info);
4250 	bp->vnic_info = NULL;
4251 	bp->nr_vnics = 0;
4252 }
4253 
4254 static int bnxt_alloc_vnics(struct bnxt *bp)
4255 {
4256 	int num_vnics = 1;
4257 
4258 #ifdef CONFIG_RFS_ACCEL
4259 	if (bp->flags & BNXT_FLAG_RFS) {
4260 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4261 			num_vnics++;
4262 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4263 			num_vnics += bp->rx_nr_rings;
4264 	}
4265 #endif
4266 
4267 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4268 		num_vnics++;
4269 
4270 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4271 				GFP_KERNEL);
4272 	if (!bp->vnic_info)
4273 		return -ENOMEM;
4274 
4275 	bp->nr_vnics = num_vnics;
4276 	return 0;
4277 }
4278 
4279 static void bnxt_init_vnics(struct bnxt *bp)
4280 {
4281 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4282 	int i;
4283 
4284 	for (i = 0; i < bp->nr_vnics; i++) {
4285 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4286 		int j;
4287 
4288 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4289 		vnic->vnic_id = i;
4290 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4291 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4292 
4293 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4294 
4295 		if (bp->vnic_info[i].rss_hash_key) {
4296 			if (i == BNXT_VNIC_DEFAULT) {
4297 				u8 *key = (void *)vnic->rss_hash_key;
4298 				int k;
4299 
4300 				if (!bp->rss_hash_key_valid &&
4301 				    !bp->rss_hash_key_updated) {
4302 					get_random_bytes(bp->rss_hash_key,
4303 							 HW_HASH_KEY_SIZE);
4304 					bp->rss_hash_key_updated = true;
4305 				}
4306 
4307 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4308 				       HW_HASH_KEY_SIZE);
4309 
4310 				if (!bp->rss_hash_key_updated)
4311 					continue;
4312 
4313 				bp->rss_hash_key_updated = false;
4314 				bp->rss_hash_key_valid = true;
4315 
4316 				bp->toeplitz_prefix = 0;
4317 				for (k = 0; k < 8; k++) {
4318 					bp->toeplitz_prefix <<= 8;
4319 					bp->toeplitz_prefix |= key[k];
4320 				}
4321 			} else {
4322 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4323 				       HW_HASH_KEY_SIZE);
4324 			}
4325 		}
4326 	}
4327 }
4328 
4329 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4330 {
4331 	int pages;
4332 
4333 	pages = ring_size / desc_per_pg;
4334 
4335 	if (!pages)
4336 		return 1;
4337 
4338 	pages++;
4339 
4340 	while (pages & (pages - 1))
4341 		pages++;
4342 
4343 	return pages;
4344 }
4345 
4346 void bnxt_set_tpa_flags(struct bnxt *bp)
4347 {
4348 	bp->flags &= ~BNXT_FLAG_TPA;
4349 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4350 		return;
4351 	if (bp->dev->features & NETIF_F_LRO)
4352 		bp->flags |= BNXT_FLAG_LRO;
4353 	else if (bp->dev->features & NETIF_F_GRO_HW)
4354 		bp->flags |= BNXT_FLAG_GRO;
4355 }
4356 
4357 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4358  * be set on entry.
4359  */
4360 void bnxt_set_ring_params(struct bnxt *bp)
4361 {
4362 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4363 	u32 agg_factor = 0, agg_ring_size = 0;
4364 
4365 	/* 8 for CRC and VLAN */
4366 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4367 
4368 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4369 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4370 
4371 	bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
4372 	ring_size = bp->rx_ring_size;
4373 	bp->rx_agg_ring_size = 0;
4374 	bp->rx_agg_nr_pages = 0;
4375 
4376 	if (bp->flags & BNXT_FLAG_TPA)
4377 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4378 
4379 	bp->flags &= ~BNXT_FLAG_JUMBO;
4380 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4381 		u32 jumbo_factor;
4382 
4383 		bp->flags |= BNXT_FLAG_JUMBO;
4384 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4385 		if (jumbo_factor > agg_factor)
4386 			agg_factor = jumbo_factor;
4387 	}
4388 	if (agg_factor) {
4389 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4390 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4391 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4392 				    bp->rx_ring_size, ring_size);
4393 			bp->rx_ring_size = ring_size;
4394 		}
4395 		agg_ring_size = ring_size * agg_factor;
4396 
4397 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4398 							RX_DESC_CNT);
4399 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4400 			u32 tmp = agg_ring_size;
4401 
4402 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4403 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4404 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4405 				    tmp, agg_ring_size);
4406 		}
4407 		bp->rx_agg_ring_size = agg_ring_size;
4408 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4409 
4410 		if (BNXT_RX_PAGE_MODE(bp)) {
4411 			rx_space = PAGE_SIZE;
4412 			rx_size = PAGE_SIZE -
4413 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4414 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4415 		} else {
4416 			rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
4417 			rx_space = rx_size + NET_SKB_PAD +
4418 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4419 		}
4420 	}
4421 
4422 	bp->rx_buf_use_size = rx_size;
4423 	bp->rx_buf_size = rx_space;
4424 
4425 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4426 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4427 
4428 	ring_size = bp->tx_ring_size;
4429 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4430 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4431 
4432 	max_rx_cmpl = bp->rx_ring_size;
4433 	/* MAX TPA needs to be added because TPA_START completions are
4434 	 * immediately recycled, so the TPA completions are not bound by
4435 	 * the RX ring size.
4436 	 */
4437 	if (bp->flags & BNXT_FLAG_TPA)
4438 		max_rx_cmpl += bp->max_tpa;
4439 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4440 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4441 	bp->cp_ring_size = ring_size;
4442 
4443 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4444 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4445 		bp->cp_nr_pages = MAX_CP_PAGES;
4446 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4447 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4448 			    ring_size, bp->cp_ring_size);
4449 	}
4450 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4451 	bp->cp_ring_mask = bp->cp_bit - 1;
4452 }
4453 
4454 /* Changing allocation mode of RX rings.
4455  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4456  */
4457 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4458 {
4459 	struct net_device *dev = bp->dev;
4460 
4461 	if (page_mode) {
4462 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
4463 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4464 
4465 		if (bp->xdp_prog->aux->xdp_has_frags)
4466 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4467 		else
4468 			dev->max_mtu =
4469 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4470 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4471 			bp->flags |= BNXT_FLAG_JUMBO;
4472 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4473 		} else {
4474 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4475 			bp->rx_skb_func = bnxt_rx_page_skb;
4476 		}
4477 		bp->rx_dir = DMA_BIDIRECTIONAL;
4478 		/* Disable LRO or GRO_HW */
4479 		netdev_update_features(dev);
4480 	} else {
4481 		dev->max_mtu = bp->max_mtu;
4482 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4483 		bp->rx_dir = DMA_FROM_DEVICE;
4484 		bp->rx_skb_func = bnxt_rx_skb;
4485 	}
4486 	return 0;
4487 }
4488 
4489 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4490 {
4491 	int i;
4492 	struct bnxt_vnic_info *vnic;
4493 	struct pci_dev *pdev = bp->pdev;
4494 
4495 	if (!bp->vnic_info)
4496 		return;
4497 
4498 	for (i = 0; i < bp->nr_vnics; i++) {
4499 		vnic = &bp->vnic_info[i];
4500 
4501 		kfree(vnic->fw_grp_ids);
4502 		vnic->fw_grp_ids = NULL;
4503 
4504 		kfree(vnic->uc_list);
4505 		vnic->uc_list = NULL;
4506 
4507 		if (vnic->mc_list) {
4508 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4509 					  vnic->mc_list, vnic->mc_list_mapping);
4510 			vnic->mc_list = NULL;
4511 		}
4512 
4513 		if (vnic->rss_table) {
4514 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4515 					  vnic->rss_table,
4516 					  vnic->rss_table_dma_addr);
4517 			vnic->rss_table = NULL;
4518 		}
4519 
4520 		vnic->rss_hash_key = NULL;
4521 		vnic->flags = 0;
4522 	}
4523 }
4524 
4525 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4526 {
4527 	int i, rc = 0, size;
4528 	struct bnxt_vnic_info *vnic;
4529 	struct pci_dev *pdev = bp->pdev;
4530 	int max_rings;
4531 
4532 	for (i = 0; i < bp->nr_vnics; i++) {
4533 		vnic = &bp->vnic_info[i];
4534 
4535 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4536 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4537 
4538 			if (mem_size > 0) {
4539 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4540 				if (!vnic->uc_list) {
4541 					rc = -ENOMEM;
4542 					goto out;
4543 				}
4544 			}
4545 		}
4546 
4547 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4548 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4549 			vnic->mc_list =
4550 				dma_alloc_coherent(&pdev->dev,
4551 						   vnic->mc_list_size,
4552 						   &vnic->mc_list_mapping,
4553 						   GFP_KERNEL);
4554 			if (!vnic->mc_list) {
4555 				rc = -ENOMEM;
4556 				goto out;
4557 			}
4558 		}
4559 
4560 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4561 			goto vnic_skip_grps;
4562 
4563 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4564 			max_rings = bp->rx_nr_rings;
4565 		else
4566 			max_rings = 1;
4567 
4568 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4569 		if (!vnic->fw_grp_ids) {
4570 			rc = -ENOMEM;
4571 			goto out;
4572 		}
4573 vnic_skip_grps:
4574 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4575 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4576 			continue;
4577 
4578 		/* Allocate rss table and hash key */
4579 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4580 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4581 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4582 
4583 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4584 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4585 						     vnic->rss_table_size,
4586 						     &vnic->rss_table_dma_addr,
4587 						     GFP_KERNEL);
4588 		if (!vnic->rss_table) {
4589 			rc = -ENOMEM;
4590 			goto out;
4591 		}
4592 
4593 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4594 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4595 	}
4596 	return 0;
4597 
4598 out:
4599 	return rc;
4600 }
4601 
4602 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4603 {
4604 	struct bnxt_hwrm_wait_token *token;
4605 
4606 	dma_pool_destroy(bp->hwrm_dma_pool);
4607 	bp->hwrm_dma_pool = NULL;
4608 
4609 	rcu_read_lock();
4610 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4611 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4612 	rcu_read_unlock();
4613 }
4614 
4615 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4616 {
4617 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4618 					    BNXT_HWRM_DMA_SIZE,
4619 					    BNXT_HWRM_DMA_ALIGN, 0);
4620 	if (!bp->hwrm_dma_pool)
4621 		return -ENOMEM;
4622 
4623 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4624 
4625 	return 0;
4626 }
4627 
4628 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4629 {
4630 	kfree(stats->hw_masks);
4631 	stats->hw_masks = NULL;
4632 	kfree(stats->sw_stats);
4633 	stats->sw_stats = NULL;
4634 	if (stats->hw_stats) {
4635 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
4636 				  stats->hw_stats_map);
4637 		stats->hw_stats = NULL;
4638 	}
4639 }
4640 
4641 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
4642 				bool alloc_masks)
4643 {
4644 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
4645 					     &stats->hw_stats_map, GFP_KERNEL);
4646 	if (!stats->hw_stats)
4647 		return -ENOMEM;
4648 
4649 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
4650 	if (!stats->sw_stats)
4651 		goto stats_mem_err;
4652 
4653 	if (alloc_masks) {
4654 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
4655 		if (!stats->hw_masks)
4656 			goto stats_mem_err;
4657 	}
4658 	return 0;
4659 
4660 stats_mem_err:
4661 	bnxt_free_stats_mem(bp, stats);
4662 	return -ENOMEM;
4663 }
4664 
4665 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
4666 {
4667 	int i;
4668 
4669 	for (i = 0; i < count; i++)
4670 		mask_arr[i] = mask;
4671 }
4672 
4673 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
4674 {
4675 	int i;
4676 
4677 	for (i = 0; i < count; i++)
4678 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
4679 }
4680 
4681 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
4682 				    struct bnxt_stats_mem *stats)
4683 {
4684 	struct hwrm_func_qstats_ext_output *resp;
4685 	struct hwrm_func_qstats_ext_input *req;
4686 	__le64 *hw_masks;
4687 	int rc;
4688 
4689 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
4690 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4691 		return -EOPNOTSUPP;
4692 
4693 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
4694 	if (rc)
4695 		return rc;
4696 
4697 	req->fid = cpu_to_le16(0xffff);
4698 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4699 
4700 	resp = hwrm_req_hold(bp, req);
4701 	rc = hwrm_req_send(bp, req);
4702 	if (!rc) {
4703 		hw_masks = &resp->rx_ucast_pkts;
4704 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
4705 	}
4706 	hwrm_req_drop(bp, req);
4707 	return rc;
4708 }
4709 
4710 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
4711 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
4712 
4713 static void bnxt_init_stats(struct bnxt *bp)
4714 {
4715 	struct bnxt_napi *bnapi = bp->bnapi[0];
4716 	struct bnxt_cp_ring_info *cpr;
4717 	struct bnxt_stats_mem *stats;
4718 	__le64 *rx_stats, *tx_stats;
4719 	int rc, rx_count, tx_count;
4720 	u64 *rx_masks, *tx_masks;
4721 	u64 mask;
4722 	u8 flags;
4723 
4724 	cpr = &bnapi->cp_ring;
4725 	stats = &cpr->stats;
4726 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
4727 	if (rc) {
4728 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4729 			mask = (1ULL << 48) - 1;
4730 		else
4731 			mask = -1ULL;
4732 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
4733 	}
4734 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
4735 		stats = &bp->port_stats;
4736 		rx_stats = stats->hw_stats;
4737 		rx_masks = stats->hw_masks;
4738 		rx_count = sizeof(struct rx_port_stats) / 8;
4739 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4740 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4741 		tx_count = sizeof(struct tx_port_stats) / 8;
4742 
4743 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
4744 		rc = bnxt_hwrm_port_qstats(bp, flags);
4745 		if (rc) {
4746 			mask = (1ULL << 40) - 1;
4747 
4748 			bnxt_fill_masks(rx_masks, mask, rx_count);
4749 			bnxt_fill_masks(tx_masks, mask, tx_count);
4750 		} else {
4751 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4752 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
4753 			bnxt_hwrm_port_qstats(bp, 0);
4754 		}
4755 	}
4756 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
4757 		stats = &bp->rx_port_stats_ext;
4758 		rx_stats = stats->hw_stats;
4759 		rx_masks = stats->hw_masks;
4760 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
4761 		stats = &bp->tx_port_stats_ext;
4762 		tx_stats = stats->hw_stats;
4763 		tx_masks = stats->hw_masks;
4764 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
4765 
4766 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4767 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
4768 		if (rc) {
4769 			mask = (1ULL << 40) - 1;
4770 
4771 			bnxt_fill_masks(rx_masks, mask, rx_count);
4772 			if (tx_stats)
4773 				bnxt_fill_masks(tx_masks, mask, tx_count);
4774 		} else {
4775 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4776 			if (tx_stats)
4777 				bnxt_copy_hw_masks(tx_masks, tx_stats,
4778 						   tx_count);
4779 			bnxt_hwrm_port_qstats_ext(bp, 0);
4780 		}
4781 	}
4782 }
4783 
4784 static void bnxt_free_port_stats(struct bnxt *bp)
4785 {
4786 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
4787 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
4788 
4789 	bnxt_free_stats_mem(bp, &bp->port_stats);
4790 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
4791 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
4792 }
4793 
4794 static void bnxt_free_ring_stats(struct bnxt *bp)
4795 {
4796 	int i;
4797 
4798 	if (!bp->bnapi)
4799 		return;
4800 
4801 	for (i = 0; i < bp->cp_nr_rings; i++) {
4802 		struct bnxt_napi *bnapi = bp->bnapi[i];
4803 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4804 
4805 		bnxt_free_stats_mem(bp, &cpr->stats);
4806 
4807 		kfree(cpr->sw_stats);
4808 		cpr->sw_stats = NULL;
4809 	}
4810 }
4811 
4812 static int bnxt_alloc_stats(struct bnxt *bp)
4813 {
4814 	u32 size, i;
4815 	int rc;
4816 
4817 	size = bp->hw_ring_stats_size;
4818 
4819 	for (i = 0; i < bp->cp_nr_rings; i++) {
4820 		struct bnxt_napi *bnapi = bp->bnapi[i];
4821 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4822 
4823 		cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL);
4824 		if (!cpr->sw_stats)
4825 			return -ENOMEM;
4826 
4827 		cpr->stats.len = size;
4828 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
4829 		if (rc)
4830 			return rc;
4831 
4832 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4833 	}
4834 
4835 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
4836 		return 0;
4837 
4838 	if (bp->port_stats.hw_stats)
4839 		goto alloc_ext_stats;
4840 
4841 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
4842 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
4843 	if (rc)
4844 		return rc;
4845 
4846 	bp->flags |= BNXT_FLAG_PORT_STATS;
4847 
4848 alloc_ext_stats:
4849 	/* Display extended statistics only if FW supports it */
4850 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
4851 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
4852 			return 0;
4853 
4854 	if (bp->rx_port_stats_ext.hw_stats)
4855 		goto alloc_tx_ext_stats;
4856 
4857 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
4858 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
4859 	/* Extended stats are optional */
4860 	if (rc)
4861 		return 0;
4862 
4863 alloc_tx_ext_stats:
4864 	if (bp->tx_port_stats_ext.hw_stats)
4865 		return 0;
4866 
4867 	if (bp->hwrm_spec_code >= 0x10902 ||
4868 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
4869 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
4870 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
4871 		/* Extended stats are optional */
4872 		if (rc)
4873 			return 0;
4874 	}
4875 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
4876 	return 0;
4877 }
4878 
4879 static void bnxt_clear_ring_indices(struct bnxt *bp)
4880 {
4881 	int i, j;
4882 
4883 	if (!bp->bnapi)
4884 		return;
4885 
4886 	for (i = 0; i < bp->cp_nr_rings; i++) {
4887 		struct bnxt_napi *bnapi = bp->bnapi[i];
4888 		struct bnxt_cp_ring_info *cpr;
4889 		struct bnxt_rx_ring_info *rxr;
4890 		struct bnxt_tx_ring_info *txr;
4891 
4892 		if (!bnapi)
4893 			continue;
4894 
4895 		cpr = &bnapi->cp_ring;
4896 		cpr->cp_raw_cons = 0;
4897 
4898 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4899 			txr->tx_prod = 0;
4900 			txr->tx_cons = 0;
4901 			txr->tx_hw_cons = 0;
4902 		}
4903 
4904 		rxr = bnapi->rx_ring;
4905 		if (rxr) {
4906 			rxr->rx_prod = 0;
4907 			rxr->rx_agg_prod = 0;
4908 			rxr->rx_sw_agg_prod = 0;
4909 			rxr->rx_next_cons = 0;
4910 		}
4911 		bnapi->events = 0;
4912 	}
4913 }
4914 
4915 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
4916 {
4917 	u8 type = fltr->type, flags = fltr->flags;
4918 
4919 	INIT_LIST_HEAD(&fltr->list);
4920 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
4921 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
4922 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
4923 }
4924 
4925 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
4926 {
4927 	if (!list_empty(&fltr->list))
4928 		list_del_init(&fltr->list);
4929 }
4930 
4931 void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
4932 {
4933 	struct bnxt_filter_base *usr_fltr, *tmp;
4934 
4935 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
4936 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
4937 			continue;
4938 		bnxt_del_one_usr_fltr(bp, usr_fltr);
4939 	}
4940 }
4941 
4942 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
4943 {
4944 	hlist_del(&fltr->hash);
4945 	bnxt_del_one_usr_fltr(bp, fltr);
4946 	if (fltr->flags) {
4947 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
4948 		bp->ntp_fltr_count--;
4949 	}
4950 	kfree(fltr);
4951 }
4952 
4953 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
4954 {
4955 	int i;
4956 
4957 	/* Under rtnl_lock and all our NAPIs have been disabled.  It's
4958 	 * safe to delete the hash table.
4959 	 */
4960 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
4961 		struct hlist_head *head;
4962 		struct hlist_node *tmp;
4963 		struct bnxt_ntuple_filter *fltr;
4964 
4965 		head = &bp->ntp_fltr_hash_tbl[i];
4966 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
4967 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
4968 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
4969 				     !list_empty(&fltr->base.list)))
4970 				continue;
4971 			bnxt_del_fltr(bp, &fltr->base);
4972 		}
4973 	}
4974 	if (!all)
4975 		return;
4976 
4977 	bitmap_free(bp->ntp_fltr_bmap);
4978 	bp->ntp_fltr_bmap = NULL;
4979 	bp->ntp_fltr_count = 0;
4980 }
4981 
4982 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
4983 {
4984 	int i, rc = 0;
4985 
4986 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
4987 		return 0;
4988 
4989 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
4990 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
4991 
4992 	bp->ntp_fltr_count = 0;
4993 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
4994 
4995 	if (!bp->ntp_fltr_bmap)
4996 		rc = -ENOMEM;
4997 
4998 	return rc;
4999 }
5000 
5001 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5002 {
5003 	int i;
5004 
5005 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5006 		struct hlist_head *head;
5007 		struct hlist_node *tmp;
5008 		struct bnxt_l2_filter *fltr;
5009 
5010 		head = &bp->l2_fltr_hash_tbl[i];
5011 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5012 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5013 				     !list_empty(&fltr->base.list)))
5014 				continue;
5015 			bnxt_del_fltr(bp, &fltr->base);
5016 		}
5017 	}
5018 }
5019 
5020 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5021 {
5022 	int i;
5023 
5024 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5025 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5026 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5027 }
5028 
5029 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5030 {
5031 	bnxt_free_vnic_attributes(bp);
5032 	bnxt_free_tx_rings(bp);
5033 	bnxt_free_rx_rings(bp);
5034 	bnxt_free_cp_rings(bp);
5035 	bnxt_free_all_cp_arrays(bp);
5036 	bnxt_free_ntp_fltrs(bp, false);
5037 	bnxt_free_l2_filters(bp, false);
5038 	if (irq_re_init) {
5039 		bnxt_free_ring_stats(bp);
5040 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5041 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5042 			bnxt_free_port_stats(bp);
5043 		bnxt_free_ring_grps(bp);
5044 		bnxt_free_vnics(bp);
5045 		kfree(bp->tx_ring_map);
5046 		bp->tx_ring_map = NULL;
5047 		kfree(bp->tx_ring);
5048 		bp->tx_ring = NULL;
5049 		kfree(bp->rx_ring);
5050 		bp->rx_ring = NULL;
5051 		kfree(bp->bnapi);
5052 		bp->bnapi = NULL;
5053 	} else {
5054 		bnxt_clear_ring_indices(bp);
5055 	}
5056 }
5057 
5058 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5059 {
5060 	int i, j, rc, size, arr_size;
5061 	void *bnapi;
5062 
5063 	if (irq_re_init) {
5064 		/* Allocate bnapi mem pointer array and mem block for
5065 		 * all queues
5066 		 */
5067 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5068 				bp->cp_nr_rings);
5069 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5070 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5071 		if (!bnapi)
5072 			return -ENOMEM;
5073 
5074 		bp->bnapi = bnapi;
5075 		bnapi += arr_size;
5076 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5077 			bp->bnapi[i] = bnapi;
5078 			bp->bnapi[i]->index = i;
5079 			bp->bnapi[i]->bp = bp;
5080 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5081 				struct bnxt_cp_ring_info *cpr =
5082 					&bp->bnapi[i]->cp_ring;
5083 
5084 				cpr->cp_ring_struct.ring_mem.flags =
5085 					BNXT_RMEM_RING_PTE_FLAG;
5086 			}
5087 		}
5088 
5089 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5090 				      sizeof(struct bnxt_rx_ring_info),
5091 				      GFP_KERNEL);
5092 		if (!bp->rx_ring)
5093 			return -ENOMEM;
5094 
5095 		for (i = 0; i < bp->rx_nr_rings; i++) {
5096 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5097 
5098 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5099 				rxr->rx_ring_struct.ring_mem.flags =
5100 					BNXT_RMEM_RING_PTE_FLAG;
5101 				rxr->rx_agg_ring_struct.ring_mem.flags =
5102 					BNXT_RMEM_RING_PTE_FLAG;
5103 			} else {
5104 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5105 			}
5106 			rxr->bnapi = bp->bnapi[i];
5107 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5108 		}
5109 
5110 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5111 				      sizeof(struct bnxt_tx_ring_info),
5112 				      GFP_KERNEL);
5113 		if (!bp->tx_ring)
5114 			return -ENOMEM;
5115 
5116 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5117 					  GFP_KERNEL);
5118 
5119 		if (!bp->tx_ring_map)
5120 			return -ENOMEM;
5121 
5122 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5123 			j = 0;
5124 		else
5125 			j = bp->rx_nr_rings;
5126 
5127 		for (i = 0; i < bp->tx_nr_rings; i++) {
5128 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5129 			struct bnxt_napi *bnapi2;
5130 
5131 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5132 				txr->tx_ring_struct.ring_mem.flags =
5133 					BNXT_RMEM_RING_PTE_FLAG;
5134 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5135 			if (i >= bp->tx_nr_rings_xdp) {
5136 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5137 
5138 				bnapi2 = bp->bnapi[k];
5139 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5140 				txr->tx_napi_idx =
5141 					BNXT_RING_TO_TC(bp, txr->txq_index);
5142 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5143 				bnapi2->tx_int = bnxt_tx_int;
5144 			} else {
5145 				bnapi2 = bp->bnapi[j];
5146 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5147 				bnapi2->tx_ring[0] = txr;
5148 				bnapi2->tx_int = bnxt_tx_int_xdp;
5149 				j++;
5150 			}
5151 			txr->bnapi = bnapi2;
5152 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5153 				txr->tx_cpr = &bnapi2->cp_ring;
5154 		}
5155 
5156 		rc = bnxt_alloc_stats(bp);
5157 		if (rc)
5158 			goto alloc_mem_err;
5159 		bnxt_init_stats(bp);
5160 
5161 		rc = bnxt_alloc_ntp_fltrs(bp);
5162 		if (rc)
5163 			goto alloc_mem_err;
5164 
5165 		rc = bnxt_alloc_vnics(bp);
5166 		if (rc)
5167 			goto alloc_mem_err;
5168 	}
5169 
5170 	rc = bnxt_alloc_all_cp_arrays(bp);
5171 	if (rc)
5172 		goto alloc_mem_err;
5173 
5174 	bnxt_init_ring_struct(bp);
5175 
5176 	rc = bnxt_alloc_rx_rings(bp);
5177 	if (rc)
5178 		goto alloc_mem_err;
5179 
5180 	rc = bnxt_alloc_tx_rings(bp);
5181 	if (rc)
5182 		goto alloc_mem_err;
5183 
5184 	rc = bnxt_alloc_cp_rings(bp);
5185 	if (rc)
5186 		goto alloc_mem_err;
5187 
5188 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5189 						  BNXT_VNIC_MCAST_FLAG |
5190 						  BNXT_VNIC_UCAST_FLAG;
5191 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5192 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5193 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5194 
5195 	rc = bnxt_alloc_vnic_attributes(bp);
5196 	if (rc)
5197 		goto alloc_mem_err;
5198 	return 0;
5199 
5200 alloc_mem_err:
5201 	bnxt_free_mem(bp, true);
5202 	return rc;
5203 }
5204 
5205 static void bnxt_disable_int(struct bnxt *bp)
5206 {
5207 	int i;
5208 
5209 	if (!bp->bnapi)
5210 		return;
5211 
5212 	for (i = 0; i < bp->cp_nr_rings; i++) {
5213 		struct bnxt_napi *bnapi = bp->bnapi[i];
5214 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5215 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5216 
5217 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5218 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5219 	}
5220 }
5221 
5222 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5223 {
5224 	struct bnxt_napi *bnapi = bp->bnapi[n];
5225 	struct bnxt_cp_ring_info *cpr;
5226 
5227 	cpr = &bnapi->cp_ring;
5228 	return cpr->cp_ring_struct.map_idx;
5229 }
5230 
5231 static void bnxt_disable_int_sync(struct bnxt *bp)
5232 {
5233 	int i;
5234 
5235 	if (!bp->irq_tbl)
5236 		return;
5237 
5238 	atomic_inc(&bp->intr_sem);
5239 
5240 	bnxt_disable_int(bp);
5241 	for (i = 0; i < bp->cp_nr_rings; i++) {
5242 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5243 
5244 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5245 	}
5246 }
5247 
5248 static void bnxt_enable_int(struct bnxt *bp)
5249 {
5250 	int i;
5251 
5252 	atomic_set(&bp->intr_sem, 0);
5253 	for (i = 0; i < bp->cp_nr_rings; i++) {
5254 		struct bnxt_napi *bnapi = bp->bnapi[i];
5255 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5256 
5257 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5258 	}
5259 }
5260 
5261 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5262 			    bool async_only)
5263 {
5264 	DECLARE_BITMAP(async_events_bmap, 256);
5265 	u32 *events = (u32 *)async_events_bmap;
5266 	struct hwrm_func_drv_rgtr_output *resp;
5267 	struct hwrm_func_drv_rgtr_input *req;
5268 	u32 flags;
5269 	int rc, i;
5270 
5271 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5272 	if (rc)
5273 		return rc;
5274 
5275 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5276 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5277 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5278 
5279 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5280 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5281 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5282 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5283 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5284 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5285 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5286 	req->flags = cpu_to_le32(flags);
5287 	req->ver_maj_8b = DRV_VER_MAJ;
5288 	req->ver_min_8b = DRV_VER_MIN;
5289 	req->ver_upd_8b = DRV_VER_UPD;
5290 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5291 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5292 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5293 
5294 	if (BNXT_PF(bp)) {
5295 		u32 data[8];
5296 		int i;
5297 
5298 		memset(data, 0, sizeof(data));
5299 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5300 			u16 cmd = bnxt_vf_req_snif[i];
5301 			unsigned int bit, idx;
5302 
5303 			idx = cmd / 32;
5304 			bit = cmd % 32;
5305 			data[idx] |= 1 << bit;
5306 		}
5307 
5308 		for (i = 0; i < 8; i++)
5309 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5310 
5311 		req->enables |=
5312 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5313 	}
5314 
5315 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5316 		req->flags |= cpu_to_le32(
5317 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5318 
5319 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5320 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5321 		u16 event_id = bnxt_async_events_arr[i];
5322 
5323 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5324 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5325 			continue;
5326 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5327 		    !bp->ptp_cfg)
5328 			continue;
5329 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5330 	}
5331 	if (bmap && bmap_size) {
5332 		for (i = 0; i < bmap_size; i++) {
5333 			if (test_bit(i, bmap))
5334 				__set_bit(i, async_events_bmap);
5335 		}
5336 	}
5337 	for (i = 0; i < 8; i++)
5338 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5339 
5340 	if (async_only)
5341 		req->enables =
5342 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5343 
5344 	resp = hwrm_req_hold(bp, req);
5345 	rc = hwrm_req_send(bp, req);
5346 	if (!rc) {
5347 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5348 		if (resp->flags &
5349 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5350 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5351 	}
5352 	hwrm_req_drop(bp, req);
5353 	return rc;
5354 }
5355 
5356 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5357 {
5358 	struct hwrm_func_drv_unrgtr_input *req;
5359 	int rc;
5360 
5361 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5362 		return 0;
5363 
5364 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5365 	if (rc)
5366 		return rc;
5367 	return hwrm_req_send(bp, req);
5368 }
5369 
5370 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5371 
5372 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5373 {
5374 	struct hwrm_tunnel_dst_port_free_input *req;
5375 	int rc;
5376 
5377 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5378 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5379 		return 0;
5380 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5381 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5382 		return 0;
5383 
5384 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5385 	if (rc)
5386 		return rc;
5387 
5388 	req->tunnel_type = tunnel_type;
5389 
5390 	switch (tunnel_type) {
5391 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5392 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5393 		bp->vxlan_port = 0;
5394 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5395 		break;
5396 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5397 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5398 		bp->nge_port = 0;
5399 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5400 		break;
5401 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5402 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5403 		bp->vxlan_gpe_port = 0;
5404 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5405 		break;
5406 	default:
5407 		break;
5408 	}
5409 
5410 	rc = hwrm_req_send(bp, req);
5411 	if (rc)
5412 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5413 			   rc);
5414 	if (bp->flags & BNXT_FLAG_TPA)
5415 		bnxt_set_tpa(bp, true);
5416 	return rc;
5417 }
5418 
5419 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5420 					   u8 tunnel_type)
5421 {
5422 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5423 	struct hwrm_tunnel_dst_port_alloc_input *req;
5424 	int rc;
5425 
5426 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5427 	if (rc)
5428 		return rc;
5429 
5430 	req->tunnel_type = tunnel_type;
5431 	req->tunnel_dst_port_val = port;
5432 
5433 	resp = hwrm_req_hold(bp, req);
5434 	rc = hwrm_req_send(bp, req);
5435 	if (rc) {
5436 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5437 			   rc);
5438 		goto err_out;
5439 	}
5440 
5441 	switch (tunnel_type) {
5442 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5443 		bp->vxlan_port = port;
5444 		bp->vxlan_fw_dst_port_id =
5445 			le16_to_cpu(resp->tunnel_dst_port_id);
5446 		break;
5447 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5448 		bp->nge_port = port;
5449 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5450 		break;
5451 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5452 		bp->vxlan_gpe_port = port;
5453 		bp->vxlan_gpe_fw_dst_port_id =
5454 			le16_to_cpu(resp->tunnel_dst_port_id);
5455 		break;
5456 	default:
5457 		break;
5458 	}
5459 	if (bp->flags & BNXT_FLAG_TPA)
5460 		bnxt_set_tpa(bp, true);
5461 
5462 err_out:
5463 	hwrm_req_drop(bp, req);
5464 	return rc;
5465 }
5466 
5467 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5468 {
5469 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5470 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5471 	int rc;
5472 
5473 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5474 	if (rc)
5475 		return rc;
5476 
5477 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5478 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5479 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5480 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5481 	}
5482 	req->mask = cpu_to_le32(vnic->rx_mask);
5483 	return hwrm_req_send_silent(bp, req);
5484 }
5485 
5486 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5487 {
5488 	if (!atomic_dec_and_test(&fltr->refcnt))
5489 		return;
5490 	spin_lock_bh(&bp->ntp_fltr_lock);
5491 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5492 		spin_unlock_bh(&bp->ntp_fltr_lock);
5493 		return;
5494 	}
5495 	hlist_del_rcu(&fltr->base.hash);
5496 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5497 	if (fltr->base.flags) {
5498 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5499 		bp->ntp_fltr_count--;
5500 	}
5501 	spin_unlock_bh(&bp->ntp_fltr_lock);
5502 	kfree_rcu(fltr, base.rcu);
5503 }
5504 
5505 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5506 						      struct bnxt_l2_key *key,
5507 						      u32 idx)
5508 {
5509 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5510 	struct bnxt_l2_filter *fltr;
5511 
5512 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5513 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5514 
5515 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5516 		    l2_key->vlan == key->vlan)
5517 			return fltr;
5518 	}
5519 	return NULL;
5520 }
5521 
5522 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5523 						    struct bnxt_l2_key *key,
5524 						    u32 idx)
5525 {
5526 	struct bnxt_l2_filter *fltr = NULL;
5527 
5528 	rcu_read_lock();
5529 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5530 	if (fltr)
5531 		atomic_inc(&fltr->refcnt);
5532 	rcu_read_unlock();
5533 	return fltr;
5534 }
5535 
5536 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5537 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5538 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5539 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5540 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5541 
5542 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5543 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5544 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5545 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5546 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5547 
5548 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5549 {
5550 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5551 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5552 			return sizeof(fkeys->addrs.v4addrs) +
5553 			       sizeof(fkeys->ports);
5554 
5555 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5556 			return sizeof(fkeys->addrs.v4addrs);
5557 	}
5558 
5559 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5560 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5561 			return sizeof(fkeys->addrs.v6addrs) +
5562 			       sizeof(fkeys->ports);
5563 
5564 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5565 			return sizeof(fkeys->addrs.v6addrs);
5566 	}
5567 
5568 	return 0;
5569 }
5570 
5571 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5572 			 const unsigned char *key)
5573 {
5574 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5575 	struct bnxt_ipv4_tuple tuple4;
5576 	struct bnxt_ipv6_tuple tuple6;
5577 	int i, j, len = 0;
5578 	u8 *four_tuple;
5579 
5580 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5581 	if (!len)
5582 		return 0;
5583 
5584 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5585 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5586 		tuple4.ports = fkeys->ports;
5587 		four_tuple = (unsigned char *)&tuple4;
5588 	} else {
5589 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5590 		tuple6.ports = fkeys->ports;
5591 		four_tuple = (unsigned char *)&tuple6;
5592 	}
5593 
5594 	for (i = 0, j = 8; i < len; i++, j++) {
5595 		u8 byte = four_tuple[i];
5596 		int bit;
5597 
5598 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5599 			if (byte & 0x80)
5600 				hash ^= prefix;
5601 		}
5602 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5603 	}
5604 
5605 	/* The valid part of the hash is in the upper 32 bits. */
5606 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5607 }
5608 
5609 #ifdef CONFIG_RFS_ACCEL
5610 static struct bnxt_l2_filter *
5611 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5612 {
5613 	struct bnxt_l2_filter *fltr;
5614 	u32 idx;
5615 
5616 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5617 	      BNXT_L2_FLTR_HASH_MASK;
5618 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5619 	return fltr;
5620 }
5621 #endif
5622 
5623 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
5624 			       struct bnxt_l2_key *key, u32 idx)
5625 {
5626 	struct hlist_head *head;
5627 
5628 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
5629 	fltr->l2_key.vlan = key->vlan;
5630 	fltr->base.type = BNXT_FLTR_TYPE_L2;
5631 	if (fltr->base.flags) {
5632 		int bit_id;
5633 
5634 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
5635 						 bp->max_fltr, 0);
5636 		if (bit_id < 0)
5637 			return -ENOMEM;
5638 		fltr->base.sw_id = (u16)bit_id;
5639 		bp->ntp_fltr_count++;
5640 	}
5641 	head = &bp->l2_fltr_hash_tbl[idx];
5642 	hlist_add_head_rcu(&fltr->base.hash, head);
5643 	bnxt_insert_usr_fltr(bp, &fltr->base);
5644 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
5645 	atomic_set(&fltr->refcnt, 1);
5646 	return 0;
5647 }
5648 
5649 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
5650 						   struct bnxt_l2_key *key,
5651 						   gfp_t gfp)
5652 {
5653 	struct bnxt_l2_filter *fltr;
5654 	u32 idx;
5655 	int rc;
5656 
5657 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5658 	      BNXT_L2_FLTR_HASH_MASK;
5659 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5660 	if (fltr)
5661 		return fltr;
5662 
5663 	fltr = kzalloc(sizeof(*fltr), gfp);
5664 	if (!fltr)
5665 		return ERR_PTR(-ENOMEM);
5666 	spin_lock_bh(&bp->ntp_fltr_lock);
5667 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5668 	spin_unlock_bh(&bp->ntp_fltr_lock);
5669 	if (rc) {
5670 		bnxt_del_l2_filter(bp, fltr);
5671 		fltr = ERR_PTR(rc);
5672 	}
5673 	return fltr;
5674 }
5675 
5676 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
5677 						struct bnxt_l2_key *key,
5678 						u16 flags)
5679 {
5680 	struct bnxt_l2_filter *fltr;
5681 	u32 idx;
5682 	int rc;
5683 
5684 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5685 	      BNXT_L2_FLTR_HASH_MASK;
5686 	spin_lock_bh(&bp->ntp_fltr_lock);
5687 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5688 	if (fltr) {
5689 		fltr = ERR_PTR(-EEXIST);
5690 		goto l2_filter_exit;
5691 	}
5692 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
5693 	if (!fltr) {
5694 		fltr = ERR_PTR(-ENOMEM);
5695 		goto l2_filter_exit;
5696 	}
5697 	fltr->base.flags = flags;
5698 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5699 	if (rc) {
5700 		spin_unlock_bh(&bp->ntp_fltr_lock);
5701 		bnxt_del_l2_filter(bp, fltr);
5702 		return ERR_PTR(rc);
5703 	}
5704 
5705 l2_filter_exit:
5706 	spin_unlock_bh(&bp->ntp_fltr_lock);
5707 	return fltr;
5708 }
5709 
5710 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
5711 {
5712 #ifdef CONFIG_BNXT_SRIOV
5713 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
5714 
5715 	return vf->fw_fid;
5716 #else
5717 	return INVALID_HW_RING_ID;
5718 #endif
5719 }
5720 
5721 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5722 {
5723 	struct hwrm_cfa_l2_filter_free_input *req;
5724 	u16 target_id = 0xffff;
5725 	int rc;
5726 
5727 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
5728 		struct bnxt_pf_info *pf = &bp->pf;
5729 
5730 		if (fltr->base.vf_idx >= pf->active_vfs)
5731 			return -EINVAL;
5732 
5733 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
5734 		if (target_id == INVALID_HW_RING_ID)
5735 			return -EINVAL;
5736 	}
5737 
5738 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
5739 	if (rc)
5740 		return rc;
5741 
5742 	req->target_id = cpu_to_le16(target_id);
5743 	req->l2_filter_id = fltr->base.filter_id;
5744 	return hwrm_req_send(bp, req);
5745 }
5746 
5747 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5748 {
5749 	struct hwrm_cfa_l2_filter_alloc_output *resp;
5750 	struct hwrm_cfa_l2_filter_alloc_input *req;
5751 	u16 target_id = 0xffff;
5752 	int rc;
5753 
5754 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
5755 		struct bnxt_pf_info *pf = &bp->pf;
5756 
5757 		if (fltr->base.vf_idx >= pf->active_vfs)
5758 			return -EINVAL;
5759 
5760 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
5761 	}
5762 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
5763 	if (rc)
5764 		return rc;
5765 
5766 	req->target_id = cpu_to_le16(target_id);
5767 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
5768 
5769 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
5770 		req->flags |=
5771 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
5772 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
5773 	req->enables =
5774 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
5775 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
5776 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
5777 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
5778 	eth_broadcast_addr(req->l2_addr_mask);
5779 
5780 	if (fltr->l2_key.vlan) {
5781 		req->enables |=
5782 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
5783 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
5784 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
5785 		req->num_vlans = 1;
5786 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
5787 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
5788 	}
5789 
5790 	resp = hwrm_req_hold(bp, req);
5791 	rc = hwrm_req_send(bp, req);
5792 	if (!rc) {
5793 		fltr->base.filter_id = resp->l2_filter_id;
5794 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
5795 	}
5796 	hwrm_req_drop(bp, req);
5797 	return rc;
5798 }
5799 
5800 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
5801 				     struct bnxt_ntuple_filter *fltr)
5802 {
5803 	struct hwrm_cfa_ntuple_filter_free_input *req;
5804 	int rc;
5805 
5806 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
5807 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
5808 	if (rc)
5809 		return rc;
5810 
5811 	req->ntuple_filter_id = fltr->base.filter_id;
5812 	return hwrm_req_send(bp, req);
5813 }
5814 
5815 #define BNXT_NTP_FLTR_FLAGS					\
5816 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
5817 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
5818 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
5819 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
5820 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
5821 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
5822 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
5823 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
5824 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
5825 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
5826 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
5827 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
5828 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
5829 
5830 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
5831 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
5832 
5833 void bnxt_fill_ipv6_mask(__be32 mask[4])
5834 {
5835 	int i;
5836 
5837 	for (i = 0; i < 4; i++)
5838 		mask[i] = cpu_to_be32(~0);
5839 }
5840 
5841 static void
5842 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
5843 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
5844 			  struct bnxt_ntuple_filter *fltr)
5845 {
5846 	struct bnxt_rss_ctx *rss_ctx, *tmp;
5847 	u16 rxq = fltr->base.rxq;
5848 
5849 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
5850 		list_for_each_entry_safe(rss_ctx, tmp, &bp->rss_ctx_list, list) {
5851 			if (rss_ctx->index == fltr->base.fw_vnic_id) {
5852 				struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
5853 
5854 				req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
5855 				break;
5856 			}
5857 		}
5858 		return;
5859 	}
5860 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
5861 		struct bnxt_vnic_info *vnic;
5862 		u32 enables;
5863 
5864 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
5865 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
5866 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
5867 		req->enables |= cpu_to_le32(enables);
5868 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
5869 	} else {
5870 		u32 flags;
5871 
5872 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
5873 		req->flags |= cpu_to_le32(flags);
5874 		req->dst_id = cpu_to_le16(rxq);
5875 	}
5876 }
5877 
5878 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
5879 				      struct bnxt_ntuple_filter *fltr)
5880 {
5881 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
5882 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
5883 	struct bnxt_flow_masks *masks = &fltr->fmasks;
5884 	struct flow_keys *keys = &fltr->fkeys;
5885 	struct bnxt_l2_filter *l2_fltr;
5886 	struct bnxt_vnic_info *vnic;
5887 	int rc;
5888 
5889 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
5890 	if (rc)
5891 		return rc;
5892 
5893 	l2_fltr = fltr->l2_fltr;
5894 	req->l2_filter_id = l2_fltr->base.filter_id;
5895 
5896 	if (fltr->base.flags & BNXT_ACT_DROP) {
5897 		req->flags =
5898 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
5899 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
5900 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
5901 	} else {
5902 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
5903 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
5904 	}
5905 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
5906 
5907 	req->ethertype = htons(ETH_P_IP);
5908 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
5909 	req->ip_protocol = keys->basic.ip_proto;
5910 
5911 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
5912 		req->ethertype = htons(ETH_P_IPV6);
5913 		req->ip_addr_type =
5914 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
5915 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
5916 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
5917 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
5918 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
5919 	} else {
5920 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
5921 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
5922 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
5923 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
5924 	}
5925 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
5926 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
5927 		req->tunnel_type =
5928 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
5929 	}
5930 
5931 	req->src_port = keys->ports.src;
5932 	req->src_port_mask = masks->ports.src;
5933 	req->dst_port = keys->ports.dst;
5934 	req->dst_port_mask = masks->ports.dst;
5935 
5936 	resp = hwrm_req_hold(bp, req);
5937 	rc = hwrm_req_send(bp, req);
5938 	if (!rc)
5939 		fltr->base.filter_id = resp->ntuple_filter_id;
5940 	hwrm_req_drop(bp, req);
5941 	return rc;
5942 }
5943 
5944 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
5945 				     const u8 *mac_addr)
5946 {
5947 	struct bnxt_l2_filter *fltr;
5948 	struct bnxt_l2_key key;
5949 	int rc;
5950 
5951 	ether_addr_copy(key.dst_mac_addr, mac_addr);
5952 	key.vlan = 0;
5953 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
5954 	if (IS_ERR(fltr))
5955 		return PTR_ERR(fltr);
5956 
5957 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
5958 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
5959 	if (rc)
5960 		bnxt_del_l2_filter(bp, fltr);
5961 	else
5962 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
5963 	return rc;
5964 }
5965 
5966 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
5967 {
5968 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
5969 
5970 	/* Any associated ntuple filters will also be cleared by firmware. */
5971 	for (i = 0; i < num_of_vnics; i++) {
5972 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
5973 
5974 		for (j = 0; j < vnic->uc_filter_count; j++) {
5975 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
5976 
5977 			bnxt_hwrm_l2_filter_free(bp, fltr);
5978 			bnxt_del_l2_filter(bp, fltr);
5979 		}
5980 		vnic->uc_filter_count = 0;
5981 	}
5982 }
5983 
5984 #define BNXT_DFLT_TUNL_TPA_BMAP				\
5985 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
5986 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
5987 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
5988 
5989 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
5990 					   struct hwrm_vnic_tpa_cfg_input *req)
5991 {
5992 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
5993 
5994 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
5995 		return;
5996 
5997 	if (bp->vxlan_port)
5998 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
5999 	if (bp->vxlan_gpe_port)
6000 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6001 	if (bp->nge_port)
6002 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6003 
6004 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6005 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6006 }
6007 
6008 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6009 			   u32 tpa_flags)
6010 {
6011 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6012 	struct hwrm_vnic_tpa_cfg_input *req;
6013 	int rc;
6014 
6015 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6016 		return 0;
6017 
6018 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6019 	if (rc)
6020 		return rc;
6021 
6022 	if (tpa_flags) {
6023 		u16 mss = bp->dev->mtu - 40;
6024 		u32 nsegs, n, segs = 0, flags;
6025 
6026 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6027 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6028 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6029 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6030 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6031 		if (tpa_flags & BNXT_FLAG_GRO)
6032 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6033 
6034 		req->flags = cpu_to_le32(flags);
6035 
6036 		req->enables =
6037 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6038 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6039 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6040 
6041 		/* Number of segs are log2 units, and first packet is not
6042 		 * included as part of this units.
6043 		 */
6044 		if (mss <= BNXT_RX_PAGE_SIZE) {
6045 			n = BNXT_RX_PAGE_SIZE / mss;
6046 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6047 		} else {
6048 			n = mss / BNXT_RX_PAGE_SIZE;
6049 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6050 				n++;
6051 			nsegs = (MAX_SKB_FRAGS - n) / n;
6052 		}
6053 
6054 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6055 			segs = MAX_TPA_SEGS_P5;
6056 			max_aggs = bp->max_tpa;
6057 		} else {
6058 			segs = ilog2(nsegs);
6059 		}
6060 		req->max_agg_segs = cpu_to_le16(segs);
6061 		req->max_aggs = cpu_to_le16(max_aggs);
6062 
6063 		req->min_agg_len = cpu_to_le32(512);
6064 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6065 	}
6066 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6067 
6068 	return hwrm_req_send(bp, req);
6069 }
6070 
6071 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6072 {
6073 	struct bnxt_ring_grp_info *grp_info;
6074 
6075 	grp_info = &bp->grp_info[ring->grp_idx];
6076 	return grp_info->cp_fw_ring_id;
6077 }
6078 
6079 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6080 {
6081 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6082 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6083 	else
6084 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6085 }
6086 
6087 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6088 {
6089 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6090 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6091 	else
6092 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6093 }
6094 
6095 int bnxt_alloc_rss_indir_tbl(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx)
6096 {
6097 	int entries;
6098 	u16 *tbl;
6099 
6100 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6101 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6102 	else
6103 		entries = HW_HASH_INDEX_SIZE;
6104 
6105 	bp->rss_indir_tbl_entries = entries;
6106 	tbl = kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6107 	if (!tbl)
6108 		return -ENOMEM;
6109 
6110 	if (rss_ctx)
6111 		rss_ctx->rss_indir_tbl = tbl;
6112 	else
6113 		bp->rss_indir_tbl = tbl;
6114 
6115 	return 0;
6116 }
6117 
6118 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx)
6119 {
6120 	u16 max_rings, max_entries, pad, i;
6121 	u16 *rss_indir_tbl;
6122 
6123 	if (!bp->rx_nr_rings)
6124 		return;
6125 
6126 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6127 		max_rings = bp->rx_nr_rings - 1;
6128 	else
6129 		max_rings = bp->rx_nr_rings;
6130 
6131 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6132 	if (rss_ctx)
6133 		rss_indir_tbl = &rss_ctx->rss_indir_tbl[0];
6134 	else
6135 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6136 
6137 	for (i = 0; i < max_entries; i++)
6138 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6139 
6140 	pad = bp->rss_indir_tbl_entries - max_entries;
6141 	if (pad)
6142 		memset(&rss_indir_tbl[i], 0, pad * sizeof(u16));
6143 }
6144 
6145 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6146 {
6147 	u16 i, tbl_size, max_ring = 0;
6148 
6149 	if (!bp->rss_indir_tbl)
6150 		return 0;
6151 
6152 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6153 	for (i = 0; i < tbl_size; i++)
6154 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6155 	return max_ring;
6156 }
6157 
6158 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6159 {
6160 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6161 		if (!rx_rings)
6162 			return 0;
6163 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6164 					       BNXT_RSS_TABLE_ENTRIES_P5);
6165 	}
6166 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6167 		return 2;
6168 	return 1;
6169 }
6170 
6171 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6172 {
6173 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6174 	u16 i, j;
6175 
6176 	/* Fill the RSS indirection table with ring group ids */
6177 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6178 		if (!no_rss)
6179 			j = bp->rss_indir_tbl[i];
6180 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6181 	}
6182 }
6183 
6184 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6185 				    struct bnxt_vnic_info *vnic)
6186 {
6187 	__le16 *ring_tbl = vnic->rss_table;
6188 	struct bnxt_rx_ring_info *rxr;
6189 	u16 tbl_size, i;
6190 
6191 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6192 
6193 	for (i = 0; i < tbl_size; i++) {
6194 		u16 ring_id, j;
6195 
6196 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6197 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6198 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6199 			j = vnic->rss_ctx->rss_indir_tbl[i];
6200 		else
6201 			j = bp->rss_indir_tbl[i];
6202 		rxr = &bp->rx_ring[j];
6203 
6204 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6205 		*ring_tbl++ = cpu_to_le16(ring_id);
6206 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6207 		*ring_tbl++ = cpu_to_le16(ring_id);
6208 	}
6209 }
6210 
6211 static void
6212 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6213 			 struct bnxt_vnic_info *vnic)
6214 {
6215 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6216 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6217 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6218 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6219 	} else {
6220 		bnxt_fill_hw_rss_tbl(bp, vnic);
6221 	}
6222 
6223 	if (bp->rss_hash_delta) {
6224 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6225 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6226 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6227 		else
6228 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6229 	} else {
6230 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6231 	}
6232 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6233 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6234 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6235 }
6236 
6237 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6238 				  bool set_rss)
6239 {
6240 	struct hwrm_vnic_rss_cfg_input *req;
6241 	int rc;
6242 
6243 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6244 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6245 		return 0;
6246 
6247 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6248 	if (rc)
6249 		return rc;
6250 
6251 	if (set_rss)
6252 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6253 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6254 	return hwrm_req_send(bp, req);
6255 }
6256 
6257 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6258 				     struct bnxt_vnic_info *vnic, bool set_rss)
6259 {
6260 	struct hwrm_vnic_rss_cfg_input *req;
6261 	dma_addr_t ring_tbl_map;
6262 	u32 i, nr_ctxs;
6263 	int rc;
6264 
6265 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6266 	if (rc)
6267 		return rc;
6268 
6269 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6270 	if (!set_rss)
6271 		return hwrm_req_send(bp, req);
6272 
6273 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6274 	ring_tbl_map = vnic->rss_table_dma_addr;
6275 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6276 
6277 	hwrm_req_hold(bp, req);
6278 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6279 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6280 		req->ring_table_pair_index = i;
6281 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6282 		rc = hwrm_req_send(bp, req);
6283 		if (rc)
6284 			goto exit;
6285 	}
6286 
6287 exit:
6288 	hwrm_req_drop(bp, req);
6289 	return rc;
6290 }
6291 
6292 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6293 {
6294 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6295 	struct hwrm_vnic_rss_qcfg_output *resp;
6296 	struct hwrm_vnic_rss_qcfg_input *req;
6297 
6298 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6299 		return;
6300 
6301 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6302 	/* all contexts configured to same hash_type, zero always exists */
6303 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6304 	resp = hwrm_req_hold(bp, req);
6305 	if (!hwrm_req_send(bp, req)) {
6306 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6307 		bp->rss_hash_delta = 0;
6308 	}
6309 	hwrm_req_drop(bp, req);
6310 }
6311 
6312 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6313 {
6314 	struct hwrm_vnic_plcmodes_cfg_input *req;
6315 	int rc;
6316 
6317 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6318 	if (rc)
6319 		return rc;
6320 
6321 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6322 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6323 
6324 	if (BNXT_RX_PAGE_MODE(bp)) {
6325 		req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6326 	} else {
6327 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6328 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6329 		req->enables |=
6330 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6331 		req->jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
6332 		req->hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
6333 	}
6334 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6335 	return hwrm_req_send(bp, req);
6336 }
6337 
6338 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6339 					struct bnxt_vnic_info *vnic,
6340 					u16 ctx_idx)
6341 {
6342 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6343 
6344 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6345 		return;
6346 
6347 	req->rss_cos_lb_ctx_id =
6348 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6349 
6350 	hwrm_req_send(bp, req);
6351 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6352 }
6353 
6354 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6355 {
6356 	int i, j;
6357 
6358 	for (i = 0; i < bp->nr_vnics; i++) {
6359 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6360 
6361 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6362 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6363 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6364 		}
6365 	}
6366 	bp->rsscos_nr_ctxs = 0;
6367 }
6368 
6369 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6370 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6371 {
6372 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6373 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6374 	int rc;
6375 
6376 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6377 	if (rc)
6378 		return rc;
6379 
6380 	resp = hwrm_req_hold(bp, req);
6381 	rc = hwrm_req_send(bp, req);
6382 	if (!rc)
6383 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6384 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6385 	hwrm_req_drop(bp, req);
6386 
6387 	return rc;
6388 }
6389 
6390 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6391 {
6392 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6393 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6394 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6395 }
6396 
6397 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6398 {
6399 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6400 	struct hwrm_vnic_cfg_input *req;
6401 	unsigned int ring = 0, grp_idx;
6402 	u16 def_vlan = 0;
6403 	int rc;
6404 
6405 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6406 	if (rc)
6407 		return rc;
6408 
6409 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6410 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6411 
6412 		req->default_rx_ring_id =
6413 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6414 		req->default_cmpl_ring_id =
6415 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6416 		req->enables =
6417 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6418 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6419 		goto vnic_mru;
6420 	}
6421 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6422 	/* Only RSS support for now TBD: COS & LB */
6423 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6424 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6425 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6426 					   VNIC_CFG_REQ_ENABLES_MRU);
6427 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6428 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6429 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6430 					   VNIC_CFG_REQ_ENABLES_MRU);
6431 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6432 	} else {
6433 		req->rss_rule = cpu_to_le16(0xffff);
6434 	}
6435 
6436 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6437 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6438 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6439 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6440 	} else {
6441 		req->cos_rule = cpu_to_le16(0xffff);
6442 	}
6443 
6444 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6445 		ring = 0;
6446 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6447 		ring = vnic->vnic_id - 1;
6448 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6449 		ring = bp->rx_nr_rings - 1;
6450 
6451 	grp_idx = bp->rx_ring[ring].bnapi->index;
6452 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6453 	req->lb_rule = cpu_to_le16(0xffff);
6454 vnic_mru:
6455 	req->mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + VLAN_HLEN);
6456 
6457 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6458 #ifdef CONFIG_BNXT_SRIOV
6459 	if (BNXT_VF(bp))
6460 		def_vlan = bp->vf.vlan;
6461 #endif
6462 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6463 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6464 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6465 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6466 
6467 	return hwrm_req_send(bp, req);
6468 }
6469 
6470 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6471 				    struct bnxt_vnic_info *vnic)
6472 {
6473 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6474 		struct hwrm_vnic_free_input *req;
6475 
6476 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6477 			return;
6478 
6479 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6480 
6481 		hwrm_req_send(bp, req);
6482 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6483 	}
6484 }
6485 
6486 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6487 {
6488 	u16 i;
6489 
6490 	for (i = 0; i < bp->nr_vnics; i++)
6491 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6492 }
6493 
6494 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6495 			 unsigned int start_rx_ring_idx,
6496 			 unsigned int nr_rings)
6497 {
6498 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6499 	struct hwrm_vnic_alloc_output *resp;
6500 	struct hwrm_vnic_alloc_input *req;
6501 	int rc;
6502 
6503 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6504 	if (rc)
6505 		return rc;
6506 
6507 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6508 		goto vnic_no_ring_grps;
6509 
6510 	/* map ring groups to this vnic */
6511 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6512 		grp_idx = bp->rx_ring[i].bnapi->index;
6513 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6514 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6515 				   j, nr_rings);
6516 			break;
6517 		}
6518 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6519 	}
6520 
6521 vnic_no_ring_grps:
6522 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6523 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6524 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6525 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6526 
6527 	resp = hwrm_req_hold(bp, req);
6528 	rc = hwrm_req_send(bp, req);
6529 	if (!rc)
6530 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6531 	hwrm_req_drop(bp, req);
6532 	return rc;
6533 }
6534 
6535 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6536 {
6537 	struct hwrm_vnic_qcaps_output *resp;
6538 	struct hwrm_vnic_qcaps_input *req;
6539 	int rc;
6540 
6541 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6542 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6543 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6544 	if (bp->hwrm_spec_code < 0x10600)
6545 		return 0;
6546 
6547 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6548 	if (rc)
6549 		return rc;
6550 
6551 	resp = hwrm_req_hold(bp, req);
6552 	rc = hwrm_req_send(bp, req);
6553 	if (!rc) {
6554 		u32 flags = le32_to_cpu(resp->flags);
6555 
6556 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6557 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6558 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6559 		if (flags &
6560 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6561 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6562 
6563 		/* Older P5 fw before EXT_HW_STATS support did not set
6564 		 * VLAN_STRIP_CAP properly.
6565 		 */
6566 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6567 		    (BNXT_CHIP_P5(bp) &&
6568 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6569 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6570 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6571 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6572 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6573 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6574 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6575 		if (bp->max_tpa_v2) {
6576 			if (BNXT_CHIP_P5(bp))
6577 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6578 			else
6579 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6580 		}
6581 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6582 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6583 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6584 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6585 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6586 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6587 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6588 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6589 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6590 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6591 	}
6592 	hwrm_req_drop(bp, req);
6593 	return rc;
6594 }
6595 
6596 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6597 {
6598 	struct hwrm_ring_grp_alloc_output *resp;
6599 	struct hwrm_ring_grp_alloc_input *req;
6600 	int rc;
6601 	u16 i;
6602 
6603 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6604 		return 0;
6605 
6606 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6607 	if (rc)
6608 		return rc;
6609 
6610 	resp = hwrm_req_hold(bp, req);
6611 	for (i = 0; i < bp->rx_nr_rings; i++) {
6612 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
6613 
6614 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
6615 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
6616 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
6617 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
6618 
6619 		rc = hwrm_req_send(bp, req);
6620 
6621 		if (rc)
6622 			break;
6623 
6624 		bp->grp_info[grp_idx].fw_grp_id =
6625 			le32_to_cpu(resp->ring_group_id);
6626 	}
6627 	hwrm_req_drop(bp, req);
6628 	return rc;
6629 }
6630 
6631 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
6632 {
6633 	struct hwrm_ring_grp_free_input *req;
6634 	u16 i;
6635 
6636 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
6637 		return;
6638 
6639 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
6640 		return;
6641 
6642 	hwrm_req_hold(bp, req);
6643 	for (i = 0; i < bp->cp_nr_rings; i++) {
6644 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
6645 			continue;
6646 		req->ring_group_id =
6647 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
6648 
6649 		hwrm_req_send(bp, req);
6650 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
6651 	}
6652 	hwrm_req_drop(bp, req);
6653 }
6654 
6655 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
6656 				    struct bnxt_ring_struct *ring,
6657 				    u32 ring_type, u32 map_index)
6658 {
6659 	struct hwrm_ring_alloc_output *resp;
6660 	struct hwrm_ring_alloc_input *req;
6661 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
6662 	struct bnxt_ring_grp_info *grp_info;
6663 	int rc, err = 0;
6664 	u16 ring_id;
6665 
6666 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
6667 	if (rc)
6668 		goto exit;
6669 
6670 	req->enables = 0;
6671 	if (rmem->nr_pages > 1) {
6672 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
6673 		/* Page size is in log2 units */
6674 		req->page_size = BNXT_PAGE_SHIFT;
6675 		req->page_tbl_depth = 1;
6676 	} else {
6677 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
6678 	}
6679 	req->fbo = 0;
6680 	/* Association of ring index with doorbell index and MSIX number */
6681 	req->logical_id = cpu_to_le16(map_index);
6682 
6683 	switch (ring_type) {
6684 	case HWRM_RING_ALLOC_TX: {
6685 		struct bnxt_tx_ring_info *txr;
6686 
6687 		txr = container_of(ring, struct bnxt_tx_ring_info,
6688 				   tx_ring_struct);
6689 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
6690 		/* Association of transmit ring with completion ring */
6691 		grp_info = &bp->grp_info[ring->grp_idx];
6692 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
6693 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
6694 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6695 		req->queue_id = cpu_to_le16(ring->queue_id);
6696 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
6697 			req->cmpl_coal_cnt =
6698 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
6699 		break;
6700 	}
6701 	case HWRM_RING_ALLOC_RX:
6702 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6703 		req->length = cpu_to_le32(bp->rx_ring_mask + 1);
6704 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6705 			u16 flags = 0;
6706 
6707 			/* Association of rx ring with stats context */
6708 			grp_info = &bp->grp_info[ring->grp_idx];
6709 			req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
6710 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6711 			req->enables |= cpu_to_le32(
6712 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6713 			if (NET_IP_ALIGN == 2)
6714 				flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
6715 			req->flags = cpu_to_le16(flags);
6716 		}
6717 		break;
6718 	case HWRM_RING_ALLOC_AGG:
6719 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6720 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
6721 			/* Association of agg ring with rx ring */
6722 			grp_info = &bp->grp_info[ring->grp_idx];
6723 			req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
6724 			req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
6725 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6726 			req->enables |= cpu_to_le32(
6727 				RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
6728 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6729 		} else {
6730 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6731 		}
6732 		req->length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
6733 		break;
6734 	case HWRM_RING_ALLOC_CMPL:
6735 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
6736 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
6737 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6738 			/* Association of cp ring with nq */
6739 			grp_info = &bp->grp_info[map_index];
6740 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
6741 			req->cq_handle = cpu_to_le64(ring->handle);
6742 			req->enables |= cpu_to_le32(
6743 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
6744 		} else if (bp->flags & BNXT_FLAG_USING_MSIX) {
6745 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
6746 		}
6747 		break;
6748 	case HWRM_RING_ALLOC_NQ:
6749 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
6750 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
6751 		if (bp->flags & BNXT_FLAG_USING_MSIX)
6752 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
6753 		break;
6754 	default:
6755 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
6756 			   ring_type);
6757 		return -1;
6758 	}
6759 
6760 	resp = hwrm_req_hold(bp, req);
6761 	rc = hwrm_req_send(bp, req);
6762 	err = le16_to_cpu(resp->error_code);
6763 	ring_id = le16_to_cpu(resp->ring_id);
6764 	hwrm_req_drop(bp, req);
6765 
6766 exit:
6767 	if (rc || err) {
6768 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
6769 			   ring_type, rc, err);
6770 		return -EIO;
6771 	}
6772 	ring->fw_ring_id = ring_id;
6773 	return rc;
6774 }
6775 
6776 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
6777 {
6778 	int rc;
6779 
6780 	if (BNXT_PF(bp)) {
6781 		struct hwrm_func_cfg_input *req;
6782 
6783 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
6784 		if (rc)
6785 			return rc;
6786 
6787 		req->fid = cpu_to_le16(0xffff);
6788 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
6789 		req->async_event_cr = cpu_to_le16(idx);
6790 		return hwrm_req_send(bp, req);
6791 	} else {
6792 		struct hwrm_func_vf_cfg_input *req;
6793 
6794 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
6795 		if (rc)
6796 			return rc;
6797 
6798 		req->enables =
6799 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
6800 		req->async_event_cr = cpu_to_le16(idx);
6801 		return hwrm_req_send(bp, req);
6802 	}
6803 }
6804 
6805 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
6806 			     u32 ring_type)
6807 {
6808 	switch (ring_type) {
6809 	case HWRM_RING_ALLOC_TX:
6810 		db->db_ring_mask = bp->tx_ring_mask;
6811 		break;
6812 	case HWRM_RING_ALLOC_RX:
6813 		db->db_ring_mask = bp->rx_ring_mask;
6814 		break;
6815 	case HWRM_RING_ALLOC_AGG:
6816 		db->db_ring_mask = bp->rx_agg_ring_mask;
6817 		break;
6818 	case HWRM_RING_ALLOC_CMPL:
6819 	case HWRM_RING_ALLOC_NQ:
6820 		db->db_ring_mask = bp->cp_ring_mask;
6821 		break;
6822 	}
6823 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
6824 		db->db_epoch_mask = db->db_ring_mask + 1;
6825 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
6826 	}
6827 }
6828 
6829 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
6830 			u32 map_idx, u32 xid)
6831 {
6832 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6833 		switch (ring_type) {
6834 		case HWRM_RING_ALLOC_TX:
6835 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
6836 			break;
6837 		case HWRM_RING_ALLOC_RX:
6838 		case HWRM_RING_ALLOC_AGG:
6839 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
6840 			break;
6841 		case HWRM_RING_ALLOC_CMPL:
6842 			db->db_key64 = DBR_PATH_L2;
6843 			break;
6844 		case HWRM_RING_ALLOC_NQ:
6845 			db->db_key64 = DBR_PATH_L2;
6846 			break;
6847 		}
6848 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
6849 
6850 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6851 			db->db_key64 |= DBR_VALID;
6852 
6853 		db->doorbell = bp->bar1 + bp->db_offset;
6854 	} else {
6855 		db->doorbell = bp->bar1 + map_idx * 0x80;
6856 		switch (ring_type) {
6857 		case HWRM_RING_ALLOC_TX:
6858 			db->db_key32 = DB_KEY_TX;
6859 			break;
6860 		case HWRM_RING_ALLOC_RX:
6861 		case HWRM_RING_ALLOC_AGG:
6862 			db->db_key32 = DB_KEY_RX;
6863 			break;
6864 		case HWRM_RING_ALLOC_CMPL:
6865 			db->db_key32 = DB_KEY_CP;
6866 			break;
6867 		}
6868 	}
6869 	bnxt_set_db_mask(bp, db, ring_type);
6870 }
6871 
6872 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
6873 {
6874 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
6875 	int i, rc = 0;
6876 	u32 type;
6877 
6878 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6879 		type = HWRM_RING_ALLOC_NQ;
6880 	else
6881 		type = HWRM_RING_ALLOC_CMPL;
6882 	for (i = 0; i < bp->cp_nr_rings; i++) {
6883 		struct bnxt_napi *bnapi = bp->bnapi[i];
6884 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6885 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
6886 		u32 map_idx = ring->map_idx;
6887 		unsigned int vector;
6888 
6889 		vector = bp->irq_tbl[map_idx].vector;
6890 		disable_irq_nosync(vector);
6891 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6892 		if (rc) {
6893 			enable_irq(vector);
6894 			goto err_out;
6895 		}
6896 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
6897 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
6898 		enable_irq(vector);
6899 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
6900 
6901 		if (!i) {
6902 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
6903 			if (rc)
6904 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
6905 		}
6906 	}
6907 
6908 	type = HWRM_RING_ALLOC_TX;
6909 	for (i = 0; i < bp->tx_nr_rings; i++) {
6910 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
6911 		struct bnxt_ring_struct *ring;
6912 		u32 map_idx;
6913 
6914 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6915 			struct bnxt_cp_ring_info *cpr2 = txr->tx_cpr;
6916 			struct bnxt_napi *bnapi = txr->bnapi;
6917 			u32 type2 = HWRM_RING_ALLOC_CMPL;
6918 
6919 			ring = &cpr2->cp_ring_struct;
6920 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
6921 			map_idx = bnapi->index;
6922 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
6923 			if (rc)
6924 				goto err_out;
6925 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
6926 				    ring->fw_ring_id);
6927 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
6928 		}
6929 		ring = &txr->tx_ring_struct;
6930 		map_idx = i;
6931 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6932 		if (rc)
6933 			goto err_out;
6934 		bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
6935 	}
6936 
6937 	type = HWRM_RING_ALLOC_RX;
6938 	for (i = 0; i < bp->rx_nr_rings; i++) {
6939 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
6940 		struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
6941 		struct bnxt_napi *bnapi = rxr->bnapi;
6942 		u32 map_idx = bnapi->index;
6943 
6944 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6945 		if (rc)
6946 			goto err_out;
6947 		bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
6948 		/* If we have agg rings, post agg buffers first. */
6949 		if (!agg_rings)
6950 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
6951 		bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
6952 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6953 			struct bnxt_cp_ring_info *cpr2 = rxr->rx_cpr;
6954 			u32 type2 = HWRM_RING_ALLOC_CMPL;
6955 
6956 			ring = &cpr2->cp_ring_struct;
6957 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
6958 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
6959 			if (rc)
6960 				goto err_out;
6961 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
6962 				    ring->fw_ring_id);
6963 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
6964 		}
6965 	}
6966 
6967 	if (agg_rings) {
6968 		type = HWRM_RING_ALLOC_AGG;
6969 		for (i = 0; i < bp->rx_nr_rings; i++) {
6970 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
6971 			struct bnxt_ring_struct *ring =
6972 						&rxr->rx_agg_ring_struct;
6973 			u32 grp_idx = ring->grp_idx;
6974 			u32 map_idx = grp_idx + bp->rx_nr_rings;
6975 
6976 			rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6977 			if (rc)
6978 				goto err_out;
6979 
6980 			bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
6981 				    ring->fw_ring_id);
6982 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
6983 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
6984 			bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
6985 		}
6986 	}
6987 err_out:
6988 	return rc;
6989 }
6990 
6991 static int hwrm_ring_free_send_msg(struct bnxt *bp,
6992 				   struct bnxt_ring_struct *ring,
6993 				   u32 ring_type, int cmpl_ring_id)
6994 {
6995 	struct hwrm_ring_free_output *resp;
6996 	struct hwrm_ring_free_input *req;
6997 	u16 error_code = 0;
6998 	int rc;
6999 
7000 	if (BNXT_NO_FW_ACCESS(bp))
7001 		return 0;
7002 
7003 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7004 	if (rc)
7005 		goto exit;
7006 
7007 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7008 	req->ring_type = ring_type;
7009 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7010 
7011 	resp = hwrm_req_hold(bp, req);
7012 	rc = hwrm_req_send(bp, req);
7013 	error_code = le16_to_cpu(resp->error_code);
7014 	hwrm_req_drop(bp, req);
7015 exit:
7016 	if (rc || error_code) {
7017 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7018 			   ring_type, rc, error_code);
7019 		return -EIO;
7020 	}
7021 	return 0;
7022 }
7023 
7024 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7025 {
7026 	u32 type;
7027 	int i;
7028 
7029 	if (!bp->bnapi)
7030 		return;
7031 
7032 	for (i = 0; i < bp->tx_nr_rings; i++) {
7033 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7034 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7035 
7036 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7037 			u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
7038 
7039 			hwrm_ring_free_send_msg(bp, ring,
7040 						RING_FREE_REQ_RING_TYPE_TX,
7041 						close_path ? cmpl_ring_id :
7042 						INVALID_HW_RING_ID);
7043 			ring->fw_ring_id = INVALID_HW_RING_ID;
7044 		}
7045 	}
7046 
7047 	for (i = 0; i < bp->rx_nr_rings; i++) {
7048 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7049 		struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7050 		u32 grp_idx = rxr->bnapi->index;
7051 
7052 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7053 			u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7054 
7055 			hwrm_ring_free_send_msg(bp, ring,
7056 						RING_FREE_REQ_RING_TYPE_RX,
7057 						close_path ? cmpl_ring_id :
7058 						INVALID_HW_RING_ID);
7059 			ring->fw_ring_id = INVALID_HW_RING_ID;
7060 			bp->grp_info[grp_idx].rx_fw_ring_id =
7061 				INVALID_HW_RING_ID;
7062 		}
7063 	}
7064 
7065 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7066 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7067 	else
7068 		type = RING_FREE_REQ_RING_TYPE_RX;
7069 	for (i = 0; i < bp->rx_nr_rings; i++) {
7070 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7071 		struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7072 		u32 grp_idx = rxr->bnapi->index;
7073 
7074 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7075 			u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7076 
7077 			hwrm_ring_free_send_msg(bp, ring, type,
7078 						close_path ? cmpl_ring_id :
7079 						INVALID_HW_RING_ID);
7080 			ring->fw_ring_id = INVALID_HW_RING_ID;
7081 			bp->grp_info[grp_idx].agg_fw_ring_id =
7082 				INVALID_HW_RING_ID;
7083 		}
7084 	}
7085 
7086 	/* The completion rings are about to be freed.  After that the
7087 	 * IRQ doorbell will not work anymore.  So we need to disable
7088 	 * IRQ here.
7089 	 */
7090 	bnxt_disable_int_sync(bp);
7091 
7092 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7093 		type = RING_FREE_REQ_RING_TYPE_NQ;
7094 	else
7095 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7096 	for (i = 0; i < bp->cp_nr_rings; i++) {
7097 		struct bnxt_napi *bnapi = bp->bnapi[i];
7098 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7099 		struct bnxt_ring_struct *ring;
7100 		int j;
7101 
7102 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++) {
7103 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
7104 
7105 			ring = &cpr2->cp_ring_struct;
7106 			if (ring->fw_ring_id == INVALID_HW_RING_ID)
7107 				continue;
7108 			hwrm_ring_free_send_msg(bp, ring,
7109 						RING_FREE_REQ_RING_TYPE_L2_CMPL,
7110 						INVALID_HW_RING_ID);
7111 			ring->fw_ring_id = INVALID_HW_RING_ID;
7112 		}
7113 		ring = &cpr->cp_ring_struct;
7114 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7115 			hwrm_ring_free_send_msg(bp, ring, type,
7116 						INVALID_HW_RING_ID);
7117 			ring->fw_ring_id = INVALID_HW_RING_ID;
7118 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7119 		}
7120 	}
7121 }
7122 
7123 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7124 			     bool shared);
7125 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7126 			   bool shared);
7127 
7128 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7129 {
7130 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7131 	struct hwrm_func_qcfg_output *resp;
7132 	struct hwrm_func_qcfg_input *req;
7133 	int rc;
7134 
7135 	if (bp->hwrm_spec_code < 0x10601)
7136 		return 0;
7137 
7138 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7139 	if (rc)
7140 		return rc;
7141 
7142 	req->fid = cpu_to_le16(0xffff);
7143 	resp = hwrm_req_hold(bp, req);
7144 	rc = hwrm_req_send(bp, req);
7145 	if (rc) {
7146 		hwrm_req_drop(bp, req);
7147 		return rc;
7148 	}
7149 
7150 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7151 	if (BNXT_NEW_RM(bp)) {
7152 		u16 cp, stats;
7153 
7154 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7155 		hw_resc->resv_hw_ring_grps =
7156 			le32_to_cpu(resp->alloc_hw_ring_grps);
7157 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7158 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7159 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7160 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7161 		hw_resc->resv_irqs = cp;
7162 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7163 			int rx = hw_resc->resv_rx_rings;
7164 			int tx = hw_resc->resv_tx_rings;
7165 
7166 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7167 				rx >>= 1;
7168 			if (cp < (rx + tx)) {
7169 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7170 				if (rc)
7171 					goto get_rings_exit;
7172 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7173 					rx <<= 1;
7174 				hw_resc->resv_rx_rings = rx;
7175 				hw_resc->resv_tx_rings = tx;
7176 			}
7177 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7178 			hw_resc->resv_hw_ring_grps = rx;
7179 		}
7180 		hw_resc->resv_cp_rings = cp;
7181 		hw_resc->resv_stat_ctxs = stats;
7182 	}
7183 get_rings_exit:
7184 	hwrm_req_drop(bp, req);
7185 	return rc;
7186 }
7187 
7188 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7189 {
7190 	struct hwrm_func_qcfg_output *resp;
7191 	struct hwrm_func_qcfg_input *req;
7192 	int rc;
7193 
7194 	if (bp->hwrm_spec_code < 0x10601)
7195 		return 0;
7196 
7197 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7198 	if (rc)
7199 		return rc;
7200 
7201 	req->fid = cpu_to_le16(fid);
7202 	resp = hwrm_req_hold(bp, req);
7203 	rc = hwrm_req_send(bp, req);
7204 	if (!rc)
7205 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7206 
7207 	hwrm_req_drop(bp, req);
7208 	return rc;
7209 }
7210 
7211 static bool bnxt_rfs_supported(struct bnxt *bp);
7212 
7213 static struct hwrm_func_cfg_input *
7214 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7215 {
7216 	struct hwrm_func_cfg_input *req;
7217 	u32 enables = 0;
7218 
7219 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7220 		return NULL;
7221 
7222 	req->fid = cpu_to_le16(0xffff);
7223 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7224 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7225 	if (BNXT_NEW_RM(bp)) {
7226 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7227 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7228 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7229 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7230 			enables |= hwr->cp_p5 ?
7231 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7232 		} else {
7233 			enables |= hwr->cp ?
7234 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7235 			enables |= hwr->grp ?
7236 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7237 		}
7238 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7239 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7240 					  0;
7241 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7242 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7243 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7244 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7245 			req->num_msix = cpu_to_le16(hwr->cp);
7246 		} else {
7247 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7248 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7249 		}
7250 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7251 		req->num_vnics = cpu_to_le16(hwr->vnic);
7252 	}
7253 	req->enables = cpu_to_le32(enables);
7254 	return req;
7255 }
7256 
7257 static struct hwrm_func_vf_cfg_input *
7258 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7259 {
7260 	struct hwrm_func_vf_cfg_input *req;
7261 	u32 enables = 0;
7262 
7263 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7264 		return NULL;
7265 
7266 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7267 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7268 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7269 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7270 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7271 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7272 		enables |= hwr->cp_p5 ?
7273 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7274 	} else {
7275 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7276 		enables |= hwr->grp ?
7277 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7278 	}
7279 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7280 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7281 
7282 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7283 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7284 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7285 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7286 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7287 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7288 	} else {
7289 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7290 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7291 	}
7292 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7293 	req->num_vnics = cpu_to_le16(hwr->vnic);
7294 
7295 	req->enables = cpu_to_le32(enables);
7296 	return req;
7297 }
7298 
7299 static int
7300 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7301 {
7302 	struct hwrm_func_cfg_input *req;
7303 	int rc;
7304 
7305 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7306 	if (!req)
7307 		return -ENOMEM;
7308 
7309 	if (!req->enables) {
7310 		hwrm_req_drop(bp, req);
7311 		return 0;
7312 	}
7313 
7314 	rc = hwrm_req_send(bp, req);
7315 	if (rc)
7316 		return rc;
7317 
7318 	if (bp->hwrm_spec_code < 0x10601)
7319 		bp->hw_resc.resv_tx_rings = hwr->tx;
7320 
7321 	return bnxt_hwrm_get_rings(bp);
7322 }
7323 
7324 static int
7325 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7326 {
7327 	struct hwrm_func_vf_cfg_input *req;
7328 	int rc;
7329 
7330 	if (!BNXT_NEW_RM(bp)) {
7331 		bp->hw_resc.resv_tx_rings = hwr->tx;
7332 		return 0;
7333 	}
7334 
7335 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7336 	if (!req)
7337 		return -ENOMEM;
7338 
7339 	rc = hwrm_req_send(bp, req);
7340 	if (rc)
7341 		return rc;
7342 
7343 	return bnxt_hwrm_get_rings(bp);
7344 }
7345 
7346 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7347 {
7348 	if (BNXT_PF(bp))
7349 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7350 	else
7351 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7352 }
7353 
7354 int bnxt_nq_rings_in_use(struct bnxt *bp)
7355 {
7356 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7357 }
7358 
7359 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7360 {
7361 	int cp;
7362 
7363 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7364 		return bnxt_nq_rings_in_use(bp);
7365 
7366 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7367 	return cp;
7368 }
7369 
7370 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7371 {
7372 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7373 }
7374 
7375 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7376 {
7377 	if (!hwr->grp)
7378 		return 0;
7379 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7380 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7381 
7382 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7383 			rss_ctx *= hwr->vnic;
7384 		return rss_ctx;
7385 	}
7386 	if (BNXT_VF(bp))
7387 		return BNXT_VF_MAX_RSS_CTX;
7388 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7389 		return hwr->grp + 1;
7390 	return 1;
7391 }
7392 
7393 /* Check if a default RSS map needs to be setup.  This function is only
7394  * used on older firmware that does not require reserving RX rings.
7395  */
7396 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7397 {
7398 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7399 
7400 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7401 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7402 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7403 		if (!netif_is_rxfh_configured(bp->dev))
7404 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7405 	}
7406 }
7407 
7408 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7409 {
7410 	if (bp->flags & BNXT_FLAG_RFS) {
7411 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7412 			return 2 + bp->num_rss_ctx;
7413 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7414 			return rx_rings + 1;
7415 	}
7416 	return 1;
7417 }
7418 
7419 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7420 {
7421 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7422 	int cp = bnxt_cp_rings_in_use(bp);
7423 	int nq = bnxt_nq_rings_in_use(bp);
7424 	int rx = bp->rx_nr_rings, stat;
7425 	int vnic, grp = rx;
7426 
7427 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7428 	    bp->hwrm_spec_code >= 0x10601)
7429 		return true;
7430 
7431 	/* Old firmware does not need RX ring reservations but we still
7432 	 * need to setup a default RSS map when needed.  With new firmware
7433 	 * we go through RX ring reservations first and then set up the
7434 	 * RSS map for the successfully reserved RX rings when needed.
7435 	 */
7436 	if (!BNXT_NEW_RM(bp)) {
7437 		bnxt_check_rss_tbl_no_rmgr(bp);
7438 		return false;
7439 	}
7440 
7441 	vnic = bnxt_get_total_vnics(bp, rx);
7442 
7443 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7444 		rx <<= 1;
7445 	stat = bnxt_get_func_stat_ctxs(bp);
7446 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7447 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7448 	    (hw_resc->resv_hw_ring_grps != grp &&
7449 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7450 		return true;
7451 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7452 	    hw_resc->resv_irqs != nq)
7453 		return true;
7454 	return false;
7455 }
7456 
7457 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7458 {
7459 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7460 
7461 	hwr->tx = hw_resc->resv_tx_rings;
7462 	if (BNXT_NEW_RM(bp)) {
7463 		hwr->rx = hw_resc->resv_rx_rings;
7464 		hwr->cp = hw_resc->resv_irqs;
7465 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7466 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7467 		hwr->grp = hw_resc->resv_hw_ring_grps;
7468 		hwr->vnic = hw_resc->resv_vnics;
7469 		hwr->stat = hw_resc->resv_stat_ctxs;
7470 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7471 	}
7472 }
7473 
7474 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7475 {
7476 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7477 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7478 }
7479 
7480 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
7481 
7482 static int __bnxt_reserve_rings(struct bnxt *bp)
7483 {
7484 	struct bnxt_hw_rings hwr = {0};
7485 	int cp = bp->cp_nr_rings;
7486 	int rx_rings, rc;
7487 	int ulp_msix = 0;
7488 	bool sh = false;
7489 	int tx_cp;
7490 
7491 	if (!bnxt_need_reserve_rings(bp))
7492 		return 0;
7493 
7494 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
7495 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
7496 		if (!ulp_msix)
7497 			bnxt_set_ulp_stat_ctxs(bp, 0);
7498 
7499 		if (ulp_msix > bp->ulp_num_msix_want)
7500 			ulp_msix = bp->ulp_num_msix_want;
7501 		hwr.cp = cp + ulp_msix;
7502 	} else {
7503 		hwr.cp = bnxt_nq_rings_in_use(bp);
7504 	}
7505 
7506 	hwr.tx = bp->tx_nr_rings;
7507 	hwr.rx = bp->rx_nr_rings;
7508 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7509 		sh = true;
7510 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7511 		hwr.cp_p5 = hwr.rx + hwr.tx;
7512 
7513 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7514 
7515 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7516 		hwr.rx <<= 1;
7517 	hwr.grp = bp->rx_nr_rings;
7518 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7519 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
7520 
7521 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
7522 	if (rc)
7523 		return rc;
7524 
7525 	bnxt_copy_reserved_rings(bp, &hwr);
7526 
7527 	rx_rings = hwr.rx;
7528 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7529 		if (hwr.rx >= 2) {
7530 			rx_rings = hwr.rx >> 1;
7531 		} else {
7532 			if (netif_running(bp->dev))
7533 				return -ENOMEM;
7534 
7535 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7536 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7537 			bp->dev->hw_features &= ~NETIF_F_LRO;
7538 			bp->dev->features &= ~NETIF_F_LRO;
7539 			bnxt_set_ring_params(bp);
7540 		}
7541 	}
7542 	rx_rings = min_t(int, rx_rings, hwr.grp);
7543 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
7544 	if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
7545 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
7546 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
7547 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
7548 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7549 		hwr.rx = rx_rings << 1;
7550 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
7551 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
7552 	bp->tx_nr_rings = hwr.tx;
7553 
7554 	/* If we cannot reserve all the RX rings, reset the RSS map only
7555 	 * if absolutely necessary
7556 	 */
7557 	if (rx_rings != bp->rx_nr_rings) {
7558 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
7559 			    rx_rings, bp->rx_nr_rings);
7560 		if (netif_is_rxfh_configured(bp->dev) &&
7561 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
7562 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
7563 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
7564 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
7565 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
7566 		}
7567 	}
7568 	bp->rx_nr_rings = rx_rings;
7569 	bp->cp_nr_rings = hwr.cp;
7570 
7571 	if (!bnxt_rings_ok(bp, &hwr))
7572 		return -ENOMEM;
7573 
7574 	if (!netif_is_rxfh_configured(bp->dev))
7575 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7576 
7577 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
7578 		int resv_msix, resv_ctx, ulp_ctxs;
7579 		struct bnxt_hw_resc *hw_resc;
7580 
7581 		hw_resc = &bp->hw_resc;
7582 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
7583 		ulp_msix = min_t(int, resv_msix, ulp_msix);
7584 		bnxt_set_ulp_msix_num(bp, ulp_msix);
7585 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
7586 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
7587 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
7588 	}
7589 
7590 	return rc;
7591 }
7592 
7593 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7594 {
7595 	struct hwrm_func_vf_cfg_input *req;
7596 	u32 flags;
7597 
7598 	if (!BNXT_NEW_RM(bp))
7599 		return 0;
7600 
7601 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7602 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
7603 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7604 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7605 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7606 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
7607 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
7608 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7609 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7610 
7611 	req->flags = cpu_to_le32(flags);
7612 	return hwrm_req_send_silent(bp, req);
7613 }
7614 
7615 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7616 {
7617 	struct hwrm_func_cfg_input *req;
7618 	u32 flags;
7619 
7620 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7621 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
7622 	if (BNXT_NEW_RM(bp)) {
7623 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7624 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7625 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7626 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
7627 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7628 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
7629 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
7630 		else
7631 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7632 	}
7633 
7634 	req->flags = cpu_to_le32(flags);
7635 	return hwrm_req_send_silent(bp, req);
7636 }
7637 
7638 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7639 {
7640 	if (bp->hwrm_spec_code < 0x10801)
7641 		return 0;
7642 
7643 	if (BNXT_PF(bp))
7644 		return bnxt_hwrm_check_pf_rings(bp, hwr);
7645 
7646 	return bnxt_hwrm_check_vf_rings(bp, hwr);
7647 }
7648 
7649 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
7650 {
7651 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7652 	struct hwrm_ring_aggint_qcaps_output *resp;
7653 	struct hwrm_ring_aggint_qcaps_input *req;
7654 	int rc;
7655 
7656 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
7657 	coal_cap->num_cmpl_dma_aggr_max = 63;
7658 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
7659 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
7660 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
7661 	coal_cap->int_lat_tmr_min_max = 65535;
7662 	coal_cap->int_lat_tmr_max_max = 65535;
7663 	coal_cap->num_cmpl_aggr_int_max = 65535;
7664 	coal_cap->timer_units = 80;
7665 
7666 	if (bp->hwrm_spec_code < 0x10902)
7667 		return;
7668 
7669 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
7670 		return;
7671 
7672 	resp = hwrm_req_hold(bp, req);
7673 	rc = hwrm_req_send_silent(bp, req);
7674 	if (!rc) {
7675 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
7676 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
7677 		coal_cap->num_cmpl_dma_aggr_max =
7678 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
7679 		coal_cap->num_cmpl_dma_aggr_during_int_max =
7680 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
7681 		coal_cap->cmpl_aggr_dma_tmr_max =
7682 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
7683 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
7684 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
7685 		coal_cap->int_lat_tmr_min_max =
7686 			le16_to_cpu(resp->int_lat_tmr_min_max);
7687 		coal_cap->int_lat_tmr_max_max =
7688 			le16_to_cpu(resp->int_lat_tmr_max_max);
7689 		coal_cap->num_cmpl_aggr_int_max =
7690 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
7691 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
7692 	}
7693 	hwrm_req_drop(bp, req);
7694 }
7695 
7696 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
7697 {
7698 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7699 
7700 	return usec * 1000 / coal_cap->timer_units;
7701 }
7702 
7703 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
7704 	struct bnxt_coal *hw_coal,
7705 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7706 {
7707 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7708 	u16 val, tmr, max, flags = hw_coal->flags;
7709 	u32 cmpl_params = coal_cap->cmpl_params;
7710 
7711 	max = hw_coal->bufs_per_record * 128;
7712 	if (hw_coal->budget)
7713 		max = hw_coal->bufs_per_record * hw_coal->budget;
7714 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
7715 
7716 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
7717 	req->num_cmpl_aggr_int = cpu_to_le16(val);
7718 
7719 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
7720 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
7721 
7722 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
7723 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
7724 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
7725 
7726 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
7727 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
7728 	req->int_lat_tmr_max = cpu_to_le16(tmr);
7729 
7730 	/* min timer set to 1/2 of interrupt timer */
7731 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
7732 		val = tmr / 2;
7733 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
7734 		req->int_lat_tmr_min = cpu_to_le16(val);
7735 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
7736 	}
7737 
7738 	/* buf timer set to 1/4 of interrupt timer */
7739 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
7740 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
7741 
7742 	if (cmpl_params &
7743 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
7744 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
7745 		val = clamp_t(u16, tmr, 1,
7746 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
7747 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
7748 		req->enables |=
7749 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
7750 	}
7751 
7752 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
7753 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
7754 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
7755 	req->flags = cpu_to_le16(flags);
7756 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
7757 }
7758 
7759 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
7760 				   struct bnxt_coal *hw_coal)
7761 {
7762 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
7763 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7764 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7765 	u32 nq_params = coal_cap->nq_params;
7766 	u16 tmr;
7767 	int rc;
7768 
7769 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
7770 		return 0;
7771 
7772 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7773 	if (rc)
7774 		return rc;
7775 
7776 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
7777 	req->flags =
7778 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
7779 
7780 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
7781 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
7782 	req->int_lat_tmr_min = cpu_to_le16(tmr);
7783 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
7784 	return hwrm_req_send(bp, req);
7785 }
7786 
7787 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
7788 {
7789 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
7790 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7791 	struct bnxt_coal coal;
7792 	int rc;
7793 
7794 	/* Tick values in micro seconds.
7795 	 * 1 coal_buf x bufs_per_record = 1 completion record.
7796 	 */
7797 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
7798 
7799 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
7800 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
7801 
7802 	if (!bnapi->rx_ring)
7803 		return -ENODEV;
7804 
7805 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7806 	if (rc)
7807 		return rc;
7808 
7809 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
7810 
7811 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
7812 
7813 	return hwrm_req_send(bp, req_rx);
7814 }
7815 
7816 static int
7817 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
7818 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7819 {
7820 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
7821 
7822 	req->ring_id = cpu_to_le16(ring_id);
7823 	return hwrm_req_send(bp, req);
7824 }
7825 
7826 static int
7827 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
7828 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7829 {
7830 	struct bnxt_tx_ring_info *txr;
7831 	int i, rc;
7832 
7833 	bnxt_for_each_napi_tx(i, bnapi, txr) {
7834 		u16 ring_id;
7835 
7836 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
7837 		req->ring_id = cpu_to_le16(ring_id);
7838 		rc = hwrm_req_send(bp, req);
7839 		if (rc)
7840 			return rc;
7841 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7842 			return 0;
7843 	}
7844 	return 0;
7845 }
7846 
7847 int bnxt_hwrm_set_coal(struct bnxt *bp)
7848 {
7849 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
7850 	int i, rc;
7851 
7852 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7853 	if (rc)
7854 		return rc;
7855 
7856 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
7857 	if (rc) {
7858 		hwrm_req_drop(bp, req_rx);
7859 		return rc;
7860 	}
7861 
7862 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
7863 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
7864 
7865 	hwrm_req_hold(bp, req_rx);
7866 	hwrm_req_hold(bp, req_tx);
7867 	for (i = 0; i < bp->cp_nr_rings; i++) {
7868 		struct bnxt_napi *bnapi = bp->bnapi[i];
7869 		struct bnxt_coal *hw_coal;
7870 
7871 		if (!bnapi->rx_ring)
7872 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
7873 		else
7874 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
7875 		if (rc)
7876 			break;
7877 
7878 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7879 			continue;
7880 
7881 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
7882 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
7883 			if (rc)
7884 				break;
7885 		}
7886 		if (bnapi->rx_ring)
7887 			hw_coal = &bp->rx_coal;
7888 		else
7889 			hw_coal = &bp->tx_coal;
7890 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
7891 	}
7892 	hwrm_req_drop(bp, req_rx);
7893 	hwrm_req_drop(bp, req_tx);
7894 	return rc;
7895 }
7896 
7897 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
7898 {
7899 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
7900 	struct hwrm_stat_ctx_free_input *req;
7901 	int i;
7902 
7903 	if (!bp->bnapi)
7904 		return;
7905 
7906 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
7907 		return;
7908 
7909 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
7910 		return;
7911 	if (BNXT_FW_MAJ(bp) <= 20) {
7912 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
7913 			hwrm_req_drop(bp, req);
7914 			return;
7915 		}
7916 		hwrm_req_hold(bp, req0);
7917 	}
7918 	hwrm_req_hold(bp, req);
7919 	for (i = 0; i < bp->cp_nr_rings; i++) {
7920 		struct bnxt_napi *bnapi = bp->bnapi[i];
7921 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7922 
7923 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
7924 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
7925 			if (req0) {
7926 				req0->stat_ctx_id = req->stat_ctx_id;
7927 				hwrm_req_send(bp, req0);
7928 			}
7929 			hwrm_req_send(bp, req);
7930 
7931 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
7932 		}
7933 	}
7934 	hwrm_req_drop(bp, req);
7935 	if (req0)
7936 		hwrm_req_drop(bp, req0);
7937 }
7938 
7939 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
7940 {
7941 	struct hwrm_stat_ctx_alloc_output *resp;
7942 	struct hwrm_stat_ctx_alloc_input *req;
7943 	int rc, i;
7944 
7945 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
7946 		return 0;
7947 
7948 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
7949 	if (rc)
7950 		return rc;
7951 
7952 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
7953 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
7954 
7955 	resp = hwrm_req_hold(bp, req);
7956 	for (i = 0; i < bp->cp_nr_rings; i++) {
7957 		struct bnxt_napi *bnapi = bp->bnapi[i];
7958 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7959 
7960 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
7961 
7962 		rc = hwrm_req_send(bp, req);
7963 		if (rc)
7964 			break;
7965 
7966 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
7967 
7968 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
7969 	}
7970 	hwrm_req_drop(bp, req);
7971 	return rc;
7972 }
7973 
7974 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
7975 {
7976 	struct hwrm_func_qcfg_output *resp;
7977 	struct hwrm_func_qcfg_input *req;
7978 	u16 flags;
7979 	int rc;
7980 
7981 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7982 	if (rc)
7983 		return rc;
7984 
7985 	req->fid = cpu_to_le16(0xffff);
7986 	resp = hwrm_req_hold(bp, req);
7987 	rc = hwrm_req_send(bp, req);
7988 	if (rc)
7989 		goto func_qcfg_exit;
7990 
7991 #ifdef CONFIG_BNXT_SRIOV
7992 	if (BNXT_VF(bp)) {
7993 		struct bnxt_vf_info *vf = &bp->vf;
7994 
7995 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
7996 	} else {
7997 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
7998 	}
7999 #endif
8000 	flags = le16_to_cpu(resp->flags);
8001 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8002 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8003 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8004 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8005 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8006 	}
8007 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8008 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8009 
8010 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8011 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8012 
8013 	switch (resp->port_partition_type) {
8014 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8015 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8016 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8017 		bp->port_partition_type = resp->port_partition_type;
8018 		break;
8019 	}
8020 	if (bp->hwrm_spec_code < 0x10707 ||
8021 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8022 		bp->br_mode = BRIDGE_MODE_VEB;
8023 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8024 		bp->br_mode = BRIDGE_MODE_VEPA;
8025 	else
8026 		bp->br_mode = BRIDGE_MODE_UNDEF;
8027 
8028 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8029 	if (!bp->max_mtu)
8030 		bp->max_mtu = BNXT_MAX_MTU;
8031 
8032 	if (bp->db_size)
8033 		goto func_qcfg_exit;
8034 
8035 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8036 	if (BNXT_CHIP_P5(bp)) {
8037 		if (BNXT_PF(bp))
8038 			bp->db_offset = DB_PF_OFFSET_P5;
8039 		else
8040 			bp->db_offset = DB_VF_OFFSET_P5;
8041 	}
8042 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8043 				 1024);
8044 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8045 	    bp->db_size <= bp->db_offset)
8046 		bp->db_size = pci_resource_len(bp->pdev, 2);
8047 
8048 func_qcfg_exit:
8049 	hwrm_req_drop(bp, req);
8050 	return rc;
8051 }
8052 
8053 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8054 				      u8 init_val, u8 init_offset,
8055 				      bool init_mask_set)
8056 {
8057 	ctxm->init_value = init_val;
8058 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8059 	if (init_mask_set)
8060 		ctxm->init_offset = init_offset * 4;
8061 	else
8062 		ctxm->init_value = 0;
8063 }
8064 
8065 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8066 {
8067 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8068 	u16 type;
8069 
8070 	for (type = 0; type < ctx_max; type++) {
8071 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8072 		int n = 1;
8073 
8074 		if (!ctxm->max_entries)
8075 			continue;
8076 
8077 		if (ctxm->instance_bmap)
8078 			n = hweight32(ctxm->instance_bmap);
8079 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8080 		if (!ctxm->pg_info)
8081 			return -ENOMEM;
8082 	}
8083 	return 0;
8084 }
8085 
8086 #define BNXT_CTX_INIT_VALID(flags)	\
8087 	(!!((flags) &			\
8088 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8089 
8090 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8091 {
8092 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8093 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8094 	struct bnxt_ctx_mem_info *ctx;
8095 	u16 type;
8096 	int rc;
8097 
8098 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8099 	if (rc)
8100 		return rc;
8101 
8102 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8103 	if (!ctx)
8104 		return -ENOMEM;
8105 	bp->ctx = ctx;
8106 
8107 	resp = hwrm_req_hold(bp, req);
8108 
8109 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8110 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8111 		u8 init_val, init_off, i;
8112 		__le32 *p;
8113 		u32 flags;
8114 
8115 		req->type = cpu_to_le16(type);
8116 		rc = hwrm_req_send(bp, req);
8117 		if (rc)
8118 			goto ctx_done;
8119 		flags = le32_to_cpu(resp->flags);
8120 		type = le16_to_cpu(resp->next_valid_type);
8121 		if (!(flags & FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_TYPE_VALID))
8122 			continue;
8123 
8124 		ctxm->type = le16_to_cpu(resp->type);
8125 		ctxm->entry_size = le16_to_cpu(resp->entry_size);
8126 		ctxm->flags = flags;
8127 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8128 		ctxm->entry_multiple = resp->entry_multiple;
8129 		ctxm->max_entries = le32_to_cpu(resp->max_num_entries);
8130 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8131 		init_val = resp->ctx_init_value;
8132 		init_off = resp->ctx_init_offset;
8133 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8134 					  BNXT_CTX_INIT_VALID(flags));
8135 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8136 					      BNXT_MAX_SPLIT_ENTRY);
8137 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8138 		     i++, p++)
8139 			ctxm->split[i] = le32_to_cpu(*p);
8140 	}
8141 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8142 
8143 ctx_done:
8144 	hwrm_req_drop(bp, req);
8145 	return rc;
8146 }
8147 
8148 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8149 {
8150 	struct hwrm_func_backing_store_qcaps_output *resp;
8151 	struct hwrm_func_backing_store_qcaps_input *req;
8152 	int rc;
8153 
8154 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx)
8155 		return 0;
8156 
8157 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8158 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8159 
8160 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8161 	if (rc)
8162 		return rc;
8163 
8164 	resp = hwrm_req_hold(bp, req);
8165 	rc = hwrm_req_send_silent(bp, req);
8166 	if (!rc) {
8167 		struct bnxt_ctx_mem_type *ctxm;
8168 		struct bnxt_ctx_mem_info *ctx;
8169 		u8 init_val, init_idx = 0;
8170 		u16 init_mask;
8171 
8172 		ctx = bp->ctx;
8173 		if (!ctx) {
8174 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8175 			if (!ctx) {
8176 				rc = -ENOMEM;
8177 				goto ctx_err;
8178 			}
8179 			bp->ctx = ctx;
8180 		}
8181 		init_val = resp->ctx_kind_initializer;
8182 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8183 
8184 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8185 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8186 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8187 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8188 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8189 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8190 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8191 					  (init_mask & (1 << init_idx++)) != 0);
8192 
8193 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8194 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8195 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8196 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8197 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8198 					  (init_mask & (1 << init_idx++)) != 0);
8199 
8200 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8201 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8202 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8203 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8204 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8205 					  (init_mask & (1 << init_idx++)) != 0);
8206 
8207 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8208 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8209 		ctxm->max_entries = ctxm->vnic_entries +
8210 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8211 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8212 		bnxt_init_ctx_initializer(ctxm, init_val,
8213 					  resp->vnic_init_offset,
8214 					  (init_mask & (1 << init_idx++)) != 0);
8215 
8216 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8217 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8218 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8219 		bnxt_init_ctx_initializer(ctxm, init_val,
8220 					  resp->stat_init_offset,
8221 					  (init_mask & (1 << init_idx++)) != 0);
8222 
8223 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8224 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8225 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8226 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8227 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8228 		if (!ctxm->entry_multiple)
8229 			ctxm->entry_multiple = 1;
8230 
8231 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8232 
8233 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8234 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8235 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8236 		ctxm->mrav_num_entries_units =
8237 			le16_to_cpu(resp->mrav_num_entries_units);
8238 		bnxt_init_ctx_initializer(ctxm, init_val,
8239 					  resp->mrav_init_offset,
8240 					  (init_mask & (1 << init_idx++)) != 0);
8241 
8242 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8243 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8244 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8245 
8246 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8247 		if (!ctx->tqm_fp_rings_count)
8248 			ctx->tqm_fp_rings_count = bp->max_q;
8249 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8250 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8251 
8252 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8253 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8254 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8255 
8256 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8257 	} else {
8258 		rc = 0;
8259 	}
8260 ctx_err:
8261 	hwrm_req_drop(bp, req);
8262 	return rc;
8263 }
8264 
8265 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8266 				  __le64 *pg_dir)
8267 {
8268 	if (!rmem->nr_pages)
8269 		return;
8270 
8271 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8272 	if (rmem->depth >= 1) {
8273 		if (rmem->depth == 2)
8274 			*pg_attr |= 2;
8275 		else
8276 			*pg_attr |= 1;
8277 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8278 	} else {
8279 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8280 	}
8281 }
8282 
8283 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8284 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8285 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8286 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8287 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8288 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8289 
8290 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8291 {
8292 	struct hwrm_func_backing_store_cfg_input *req;
8293 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8294 	struct bnxt_ctx_pg_info *ctx_pg;
8295 	struct bnxt_ctx_mem_type *ctxm;
8296 	void **__req = (void **)&req;
8297 	u32 req_len = sizeof(*req);
8298 	__le32 *num_entries;
8299 	__le64 *pg_dir;
8300 	u32 flags = 0;
8301 	u8 *pg_attr;
8302 	u32 ena;
8303 	int rc;
8304 	int i;
8305 
8306 	if (!ctx)
8307 		return 0;
8308 
8309 	if (req_len > bp->hwrm_max_ext_req_len)
8310 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8311 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8312 	if (rc)
8313 		return rc;
8314 
8315 	req->enables = cpu_to_le32(enables);
8316 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8317 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8318 		ctx_pg = ctxm->pg_info;
8319 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8320 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8321 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8322 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8323 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8324 				      &req->qpc_pg_size_qpc_lvl,
8325 				      &req->qpc_page_dir);
8326 
8327 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8328 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8329 	}
8330 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8331 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8332 		ctx_pg = ctxm->pg_info;
8333 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8334 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8335 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8336 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8337 				      &req->srq_pg_size_srq_lvl,
8338 				      &req->srq_page_dir);
8339 	}
8340 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8341 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8342 		ctx_pg = ctxm->pg_info;
8343 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8344 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8345 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8346 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8347 				      &req->cq_pg_size_cq_lvl,
8348 				      &req->cq_page_dir);
8349 	}
8350 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8351 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8352 		ctx_pg = ctxm->pg_info;
8353 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8354 		req->vnic_num_ring_table_entries =
8355 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8356 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8357 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8358 				      &req->vnic_pg_size_vnic_lvl,
8359 				      &req->vnic_page_dir);
8360 	}
8361 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8362 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8363 		ctx_pg = ctxm->pg_info;
8364 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8365 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8366 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8367 				      &req->stat_pg_size_stat_lvl,
8368 				      &req->stat_page_dir);
8369 	}
8370 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8371 		u32 units;
8372 
8373 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8374 		ctx_pg = ctxm->pg_info;
8375 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8376 		units = ctxm->mrav_num_entries_units;
8377 		if (units) {
8378 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8379 			u32 entries;
8380 
8381 			num_mr = ctx_pg->entries - num_ah;
8382 			entries = ((num_mr / units) << 16) | (num_ah / units);
8383 			req->mrav_num_entries = cpu_to_le32(entries);
8384 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8385 		}
8386 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8387 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8388 				      &req->mrav_pg_size_mrav_lvl,
8389 				      &req->mrav_page_dir);
8390 	}
8391 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8392 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8393 		ctx_pg = ctxm->pg_info;
8394 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8395 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8396 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8397 				      &req->tim_pg_size_tim_lvl,
8398 				      &req->tim_page_dir);
8399 	}
8400 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8401 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8402 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8403 	     pg_dir = &req->tqm_sp_page_dir,
8404 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8405 	     ctx_pg = ctxm->pg_info;
8406 	     i < BNXT_MAX_TQM_RINGS;
8407 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8408 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8409 		if (!(enables & ena))
8410 			continue;
8411 
8412 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8413 		*num_entries = cpu_to_le32(ctx_pg->entries);
8414 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8415 	}
8416 	req->flags = cpu_to_le32(flags);
8417 	return hwrm_req_send(bp, req);
8418 }
8419 
8420 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8421 				  struct bnxt_ctx_pg_info *ctx_pg)
8422 {
8423 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8424 
8425 	rmem->page_size = BNXT_PAGE_SIZE;
8426 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8427 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8428 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8429 	if (rmem->depth >= 1)
8430 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8431 	return bnxt_alloc_ring(bp, rmem);
8432 }
8433 
8434 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8435 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8436 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8437 {
8438 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8439 	int rc;
8440 
8441 	if (!mem_size)
8442 		return -EINVAL;
8443 
8444 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8445 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8446 		ctx_pg->nr_pages = 0;
8447 		return -EINVAL;
8448 	}
8449 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8450 		int nr_tbls, i;
8451 
8452 		rmem->depth = 2;
8453 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8454 					     GFP_KERNEL);
8455 		if (!ctx_pg->ctx_pg_tbl)
8456 			return -ENOMEM;
8457 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8458 		rmem->nr_pages = nr_tbls;
8459 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8460 		if (rc)
8461 			return rc;
8462 		for (i = 0; i < nr_tbls; i++) {
8463 			struct bnxt_ctx_pg_info *pg_tbl;
8464 
8465 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8466 			if (!pg_tbl)
8467 				return -ENOMEM;
8468 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8469 			rmem = &pg_tbl->ring_mem;
8470 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8471 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8472 			rmem->depth = 1;
8473 			rmem->nr_pages = MAX_CTX_PAGES;
8474 			rmem->ctx_mem = ctxm;
8475 			if (i == (nr_tbls - 1)) {
8476 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8477 
8478 				if (rem)
8479 					rmem->nr_pages = rem;
8480 			}
8481 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8482 			if (rc)
8483 				break;
8484 		}
8485 	} else {
8486 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8487 		if (rmem->nr_pages > 1 || depth)
8488 			rmem->depth = 1;
8489 		rmem->ctx_mem = ctxm;
8490 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8491 	}
8492 	return rc;
8493 }
8494 
8495 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
8496 				  struct bnxt_ctx_pg_info *ctx_pg)
8497 {
8498 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8499 
8500 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
8501 	    ctx_pg->ctx_pg_tbl) {
8502 		int i, nr_tbls = rmem->nr_pages;
8503 
8504 		for (i = 0; i < nr_tbls; i++) {
8505 			struct bnxt_ctx_pg_info *pg_tbl;
8506 			struct bnxt_ring_mem_info *rmem2;
8507 
8508 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8509 			if (!pg_tbl)
8510 				continue;
8511 			rmem2 = &pg_tbl->ring_mem;
8512 			bnxt_free_ring(bp, rmem2);
8513 			ctx_pg->ctx_pg_arr[i] = NULL;
8514 			kfree(pg_tbl);
8515 			ctx_pg->ctx_pg_tbl[i] = NULL;
8516 		}
8517 		kfree(ctx_pg->ctx_pg_tbl);
8518 		ctx_pg->ctx_pg_tbl = NULL;
8519 	}
8520 	bnxt_free_ring(bp, rmem);
8521 	ctx_pg->nr_pages = 0;
8522 }
8523 
8524 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
8525 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
8526 				   u8 pg_lvl)
8527 {
8528 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8529 	int i, rc = 0, n = 1;
8530 	u32 mem_size;
8531 
8532 	if (!ctxm->entry_size || !ctx_pg)
8533 		return -EINVAL;
8534 	if (ctxm->instance_bmap)
8535 		n = hweight32(ctxm->instance_bmap);
8536 	if (ctxm->entry_multiple)
8537 		entries = roundup(entries, ctxm->entry_multiple);
8538 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
8539 	mem_size = entries * ctxm->entry_size;
8540 	for (i = 0; i < n && !rc; i++) {
8541 		ctx_pg[i].entries = entries;
8542 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
8543 					    ctxm->init_value ? ctxm : NULL);
8544 	}
8545 	return rc;
8546 }
8547 
8548 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
8549 					       struct bnxt_ctx_mem_type *ctxm,
8550 					       bool last)
8551 {
8552 	struct hwrm_func_backing_store_cfg_v2_input *req;
8553 	u32 instance_bmap = ctxm->instance_bmap;
8554 	int i, j, rc = 0, n = 1;
8555 	__le32 *p;
8556 
8557 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
8558 		return 0;
8559 
8560 	if (instance_bmap)
8561 		n = hweight32(ctxm->instance_bmap);
8562 	else
8563 		instance_bmap = 1;
8564 
8565 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
8566 	if (rc)
8567 		return rc;
8568 	hwrm_req_hold(bp, req);
8569 	req->type = cpu_to_le16(ctxm->type);
8570 	req->entry_size = cpu_to_le16(ctxm->entry_size);
8571 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
8572 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
8573 		p[i] = cpu_to_le32(ctxm->split[i]);
8574 	for (i = 0, j = 0; j < n && !rc; i++) {
8575 		struct bnxt_ctx_pg_info *ctx_pg;
8576 
8577 		if (!(instance_bmap & (1 << i)))
8578 			continue;
8579 		req->instance = cpu_to_le16(i);
8580 		ctx_pg = &ctxm->pg_info[j++];
8581 		if (!ctx_pg->entries)
8582 			continue;
8583 		req->num_entries = cpu_to_le32(ctx_pg->entries);
8584 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8585 				      &req->page_size_pbl_level,
8586 				      &req->page_dir);
8587 		if (last && j == n)
8588 			req->flags =
8589 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
8590 		rc = hwrm_req_send(bp, req);
8591 	}
8592 	hwrm_req_drop(bp, req);
8593 	return rc;
8594 }
8595 
8596 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
8597 {
8598 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8599 	struct bnxt_ctx_mem_type *ctxm;
8600 	u16 last_type;
8601 	int rc = 0;
8602 	u16 type;
8603 
8604 	if (!ena)
8605 		return 0;
8606 	else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
8607 		last_type = BNXT_CTX_MAX - 1;
8608 	else
8609 		last_type = BNXT_CTX_L2_MAX - 1;
8610 	ctx->ctx_arr[last_type].last = 1;
8611 
8612 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
8613 		ctxm = &ctx->ctx_arr[type];
8614 
8615 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
8616 		if (rc)
8617 			return rc;
8618 	}
8619 	return 0;
8620 }
8621 
8622 void bnxt_free_ctx_mem(struct bnxt *bp)
8623 {
8624 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8625 	u16 type;
8626 
8627 	if (!ctx)
8628 		return;
8629 
8630 	for (type = 0; type < BNXT_CTX_V2_MAX; type++) {
8631 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8632 		struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8633 		int i, n = 1;
8634 
8635 		if (!ctx_pg)
8636 			continue;
8637 		if (ctxm->instance_bmap)
8638 			n = hweight32(ctxm->instance_bmap);
8639 		for (i = 0; i < n; i++)
8640 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
8641 
8642 		kfree(ctx_pg);
8643 		ctxm->pg_info = NULL;
8644 	}
8645 
8646 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
8647 	kfree(ctx);
8648 	bp->ctx = NULL;
8649 }
8650 
8651 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
8652 {
8653 	struct bnxt_ctx_mem_type *ctxm;
8654 	struct bnxt_ctx_mem_info *ctx;
8655 	u32 l2_qps, qp1_qps, max_qps;
8656 	u32 ena, entries_sp, entries;
8657 	u32 srqs, max_srqs, min;
8658 	u32 num_mr, num_ah;
8659 	u32 extra_srqs = 0;
8660 	u32 extra_qps = 0;
8661 	u32 fast_qpmd_qps;
8662 	u8 pg_lvl = 1;
8663 	int i, rc;
8664 
8665 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
8666 	if (rc) {
8667 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
8668 			   rc);
8669 		return rc;
8670 	}
8671 	ctx = bp->ctx;
8672 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
8673 		return 0;
8674 
8675 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8676 	l2_qps = ctxm->qp_l2_entries;
8677 	qp1_qps = ctxm->qp_qp1_entries;
8678 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
8679 	max_qps = ctxm->max_entries;
8680 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8681 	srqs = ctxm->srq_l2_entries;
8682 	max_srqs = ctxm->max_entries;
8683 	ena = 0;
8684 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
8685 		pg_lvl = 2;
8686 		extra_qps = min_t(u32, 65536, max_qps - l2_qps - qp1_qps);
8687 		/* allocate extra qps if fw supports RoCE fast qp destroy feature */
8688 		extra_qps += fast_qpmd_qps;
8689 		extra_srqs = min_t(u32, 8192, max_srqs - srqs);
8690 		if (fast_qpmd_qps)
8691 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
8692 	}
8693 
8694 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8695 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
8696 				     pg_lvl);
8697 	if (rc)
8698 		return rc;
8699 
8700 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8701 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
8702 	if (rc)
8703 		return rc;
8704 
8705 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8706 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
8707 				     extra_qps * 2, pg_lvl);
8708 	if (rc)
8709 		return rc;
8710 
8711 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8712 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
8713 	if (rc)
8714 		return rc;
8715 
8716 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8717 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
8718 	if (rc)
8719 		return rc;
8720 
8721 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
8722 		goto skip_rdma;
8723 
8724 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8725 	/* 128K extra is needed to accommodate static AH context
8726 	 * allocation by f/w.
8727 	 */
8728 	num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
8729 	num_ah = min_t(u32, num_mr, 1024 * 128);
8730 	ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
8731 	if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
8732 		ctxm->mrav_av_entries = num_ah;
8733 
8734 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
8735 	if (rc)
8736 		return rc;
8737 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
8738 
8739 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8740 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
8741 	if (rc)
8742 		return rc;
8743 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
8744 
8745 skip_rdma:
8746 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8747 	min = ctxm->min_entries;
8748 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
8749 		     2 * (extra_qps + qp1_qps) + min;
8750 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
8751 	if (rc)
8752 		return rc;
8753 
8754 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8755 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
8756 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
8757 	if (rc)
8758 		return rc;
8759 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
8760 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
8761 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
8762 
8763 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8764 		rc = bnxt_backing_store_cfg_v2(bp, ena);
8765 	else
8766 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
8767 	if (rc) {
8768 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
8769 			   rc);
8770 		return rc;
8771 	}
8772 	ctx->flags |= BNXT_CTX_FLAG_INITED;
8773 	return 0;
8774 }
8775 
8776 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
8777 {
8778 	struct hwrm_func_resource_qcaps_output *resp;
8779 	struct hwrm_func_resource_qcaps_input *req;
8780 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8781 	int rc;
8782 
8783 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
8784 	if (rc)
8785 		return rc;
8786 
8787 	req->fid = cpu_to_le16(0xffff);
8788 	resp = hwrm_req_hold(bp, req);
8789 	rc = hwrm_req_send_silent(bp, req);
8790 	if (rc)
8791 		goto hwrm_func_resc_qcaps_exit;
8792 
8793 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
8794 	if (!all)
8795 		goto hwrm_func_resc_qcaps_exit;
8796 
8797 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
8798 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
8799 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
8800 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
8801 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
8802 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
8803 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
8804 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
8805 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
8806 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
8807 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
8808 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
8809 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
8810 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
8811 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
8812 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
8813 
8814 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8815 		u16 max_msix = le16_to_cpu(resp->max_msix);
8816 
8817 		hw_resc->max_nqs = max_msix;
8818 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
8819 	}
8820 
8821 	if (BNXT_PF(bp)) {
8822 		struct bnxt_pf_info *pf = &bp->pf;
8823 
8824 		pf->vf_resv_strategy =
8825 			le16_to_cpu(resp->vf_reservation_strategy);
8826 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
8827 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
8828 	}
8829 hwrm_func_resc_qcaps_exit:
8830 	hwrm_req_drop(bp, req);
8831 	return rc;
8832 }
8833 
8834 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
8835 {
8836 	struct hwrm_port_mac_ptp_qcfg_output *resp;
8837 	struct hwrm_port_mac_ptp_qcfg_input *req;
8838 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
8839 	bool phc_cfg;
8840 	u8 flags;
8841 	int rc;
8842 
8843 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5(bp)) {
8844 		rc = -ENODEV;
8845 		goto no_ptp;
8846 	}
8847 
8848 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
8849 	if (rc)
8850 		goto no_ptp;
8851 
8852 	req->port_id = cpu_to_le16(bp->pf.port_id);
8853 	resp = hwrm_req_hold(bp, req);
8854 	rc = hwrm_req_send(bp, req);
8855 	if (rc)
8856 		goto exit;
8857 
8858 	flags = resp->flags;
8859 	if (!(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
8860 		rc = -ENODEV;
8861 		goto exit;
8862 	}
8863 	if (!ptp) {
8864 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
8865 		if (!ptp) {
8866 			rc = -ENOMEM;
8867 			goto exit;
8868 		}
8869 		ptp->bp = bp;
8870 		bp->ptp_cfg = ptp;
8871 	}
8872 	if (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK) {
8873 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
8874 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
8875 	} else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8876 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
8877 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
8878 	} else {
8879 		rc = -ENODEV;
8880 		goto exit;
8881 	}
8882 	phc_cfg = (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
8883 	rc = bnxt_ptp_init(bp, phc_cfg);
8884 	if (rc)
8885 		netdev_warn(bp->dev, "PTP initialization failed.\n");
8886 exit:
8887 	hwrm_req_drop(bp, req);
8888 	if (!rc)
8889 		return 0;
8890 
8891 no_ptp:
8892 	bnxt_ptp_clear(bp);
8893 	kfree(ptp);
8894 	bp->ptp_cfg = NULL;
8895 	return rc;
8896 }
8897 
8898 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
8899 {
8900 	struct hwrm_func_qcaps_output *resp;
8901 	struct hwrm_func_qcaps_input *req;
8902 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8903 	u32 flags, flags_ext, flags_ext2;
8904 	int rc;
8905 
8906 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
8907 	if (rc)
8908 		return rc;
8909 
8910 	req->fid = cpu_to_le16(0xffff);
8911 	resp = hwrm_req_hold(bp, req);
8912 	rc = hwrm_req_send(bp, req);
8913 	if (rc)
8914 		goto hwrm_func_qcaps_exit;
8915 
8916 	flags = le32_to_cpu(resp->flags);
8917 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
8918 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
8919 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
8920 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
8921 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
8922 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
8923 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
8924 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
8925 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
8926 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
8927 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
8928 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
8929 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
8930 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
8931 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
8932 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
8933 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
8934 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
8935 
8936 	flags_ext = le32_to_cpu(resp->flags_ext);
8937 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
8938 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
8939 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
8940 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
8941 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
8942 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
8943 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
8944 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
8945 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
8946 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
8947 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
8948 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
8949 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
8950 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
8951 
8952 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
8953 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
8954 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
8955 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
8956 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
8957 
8958 	bp->tx_push_thresh = 0;
8959 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
8960 	    BNXT_FW_MAJ(bp) > 217)
8961 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
8962 
8963 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
8964 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
8965 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
8966 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
8967 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
8968 	if (!hw_resc->max_hw_ring_grps)
8969 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
8970 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
8971 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
8972 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
8973 
8974 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
8975 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
8976 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
8977 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
8978 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
8979 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
8980 
8981 	if (BNXT_PF(bp)) {
8982 		struct bnxt_pf_info *pf = &bp->pf;
8983 
8984 		pf->fw_fid = le16_to_cpu(resp->fid);
8985 		pf->port_id = le16_to_cpu(resp->port_id);
8986 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
8987 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
8988 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
8989 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
8990 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
8991 			bp->flags |= BNXT_FLAG_WOL_CAP;
8992 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
8993 			bp->fw_cap |= BNXT_FW_CAP_PTP;
8994 		} else {
8995 			bnxt_ptp_clear(bp);
8996 			kfree(bp->ptp_cfg);
8997 			bp->ptp_cfg = NULL;
8998 		}
8999 	} else {
9000 #ifdef CONFIG_BNXT_SRIOV
9001 		struct bnxt_vf_info *vf = &bp->vf;
9002 
9003 		vf->fw_fid = le16_to_cpu(resp->fid);
9004 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9005 #endif
9006 	}
9007 
9008 hwrm_func_qcaps_exit:
9009 	hwrm_req_drop(bp, req);
9010 	return rc;
9011 }
9012 
9013 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9014 {
9015 	struct hwrm_dbg_qcaps_output *resp;
9016 	struct hwrm_dbg_qcaps_input *req;
9017 	int rc;
9018 
9019 	bp->fw_dbg_cap = 0;
9020 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9021 		return;
9022 
9023 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9024 	if (rc)
9025 		return;
9026 
9027 	req->fid = cpu_to_le16(0xffff);
9028 	resp = hwrm_req_hold(bp, req);
9029 	rc = hwrm_req_send(bp, req);
9030 	if (rc)
9031 		goto hwrm_dbg_qcaps_exit;
9032 
9033 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9034 
9035 hwrm_dbg_qcaps_exit:
9036 	hwrm_req_drop(bp, req);
9037 }
9038 
9039 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9040 
9041 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9042 {
9043 	int rc;
9044 
9045 	rc = __bnxt_hwrm_func_qcaps(bp);
9046 	if (rc)
9047 		return rc;
9048 
9049 	bnxt_hwrm_dbg_qcaps(bp);
9050 
9051 	rc = bnxt_hwrm_queue_qportcfg(bp);
9052 	if (rc) {
9053 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9054 		return rc;
9055 	}
9056 	if (bp->hwrm_spec_code >= 0x10803) {
9057 		rc = bnxt_alloc_ctx_mem(bp);
9058 		if (rc)
9059 			return rc;
9060 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9061 		if (!rc)
9062 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9063 	}
9064 	return 0;
9065 }
9066 
9067 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9068 {
9069 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9070 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9071 	u32 flags;
9072 	int rc;
9073 
9074 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9075 		return 0;
9076 
9077 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9078 	if (rc)
9079 		return rc;
9080 
9081 	resp = hwrm_req_hold(bp, req);
9082 	rc = hwrm_req_send(bp, req);
9083 	if (rc)
9084 		goto hwrm_cfa_adv_qcaps_exit;
9085 
9086 	flags = le32_to_cpu(resp->flags);
9087 	if (flags &
9088 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9089 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9090 
9091 	if (flags &
9092 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9093 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9094 
9095 	if (flags &
9096 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9097 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9098 
9099 hwrm_cfa_adv_qcaps_exit:
9100 	hwrm_req_drop(bp, req);
9101 	return rc;
9102 }
9103 
9104 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9105 {
9106 	if (bp->fw_health)
9107 		return 0;
9108 
9109 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9110 	if (!bp->fw_health)
9111 		return -ENOMEM;
9112 
9113 	mutex_init(&bp->fw_health->lock);
9114 	return 0;
9115 }
9116 
9117 static int bnxt_alloc_fw_health(struct bnxt *bp)
9118 {
9119 	int rc;
9120 
9121 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9122 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9123 		return 0;
9124 
9125 	rc = __bnxt_alloc_fw_health(bp);
9126 	if (rc) {
9127 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9128 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9129 		return rc;
9130 	}
9131 
9132 	return 0;
9133 }
9134 
9135 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9136 {
9137 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9138 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9139 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9140 }
9141 
9142 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9143 {
9144 	struct bnxt_fw_health *fw_health = bp->fw_health;
9145 	u32 reg_type;
9146 
9147 	if (!fw_health)
9148 		return;
9149 
9150 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9151 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9152 		fw_health->status_reliable = false;
9153 
9154 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9155 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9156 		fw_health->resets_reliable = false;
9157 }
9158 
9159 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9160 {
9161 	void __iomem *hs;
9162 	u32 status_loc;
9163 	u32 reg_type;
9164 	u32 sig;
9165 
9166 	if (bp->fw_health)
9167 		bp->fw_health->status_reliable = false;
9168 
9169 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9170 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9171 
9172 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9173 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9174 		if (!bp->chip_num) {
9175 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9176 			bp->chip_num = readl(bp->bar0 +
9177 					     BNXT_FW_HEALTH_WIN_BASE +
9178 					     BNXT_GRC_REG_CHIP_NUM);
9179 		}
9180 		if (!BNXT_CHIP_P5_PLUS(bp))
9181 			return;
9182 
9183 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9184 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9185 	} else {
9186 		status_loc = readl(hs + offsetof(struct hcomm_status,
9187 						 fw_status_loc));
9188 	}
9189 
9190 	if (__bnxt_alloc_fw_health(bp)) {
9191 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9192 		return;
9193 	}
9194 
9195 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9196 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9197 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9198 		__bnxt_map_fw_health_reg(bp, status_loc);
9199 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9200 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9201 	}
9202 
9203 	bp->fw_health->status_reliable = true;
9204 }
9205 
9206 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9207 {
9208 	struct bnxt_fw_health *fw_health = bp->fw_health;
9209 	u32 reg_base = 0xffffffff;
9210 	int i;
9211 
9212 	bp->fw_health->status_reliable = false;
9213 	bp->fw_health->resets_reliable = false;
9214 	/* Only pre-map the monitoring GRC registers using window 3 */
9215 	for (i = 0; i < 4; i++) {
9216 		u32 reg = fw_health->regs[i];
9217 
9218 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9219 			continue;
9220 		if (reg_base == 0xffffffff)
9221 			reg_base = reg & BNXT_GRC_BASE_MASK;
9222 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9223 			return -ERANGE;
9224 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9225 	}
9226 	bp->fw_health->status_reliable = true;
9227 	bp->fw_health->resets_reliable = true;
9228 	if (reg_base == 0xffffffff)
9229 		return 0;
9230 
9231 	__bnxt_map_fw_health_reg(bp, reg_base);
9232 	return 0;
9233 }
9234 
9235 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9236 {
9237 	if (!bp->fw_health)
9238 		return;
9239 
9240 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9241 		bp->fw_health->status_reliable = true;
9242 		bp->fw_health->resets_reliable = true;
9243 	} else {
9244 		bnxt_try_map_fw_health_reg(bp);
9245 	}
9246 }
9247 
9248 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9249 {
9250 	struct bnxt_fw_health *fw_health = bp->fw_health;
9251 	struct hwrm_error_recovery_qcfg_output *resp;
9252 	struct hwrm_error_recovery_qcfg_input *req;
9253 	int rc, i;
9254 
9255 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9256 		return 0;
9257 
9258 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9259 	if (rc)
9260 		return rc;
9261 
9262 	resp = hwrm_req_hold(bp, req);
9263 	rc = hwrm_req_send(bp, req);
9264 	if (rc)
9265 		goto err_recovery_out;
9266 	fw_health->flags = le32_to_cpu(resp->flags);
9267 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
9268 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
9269 		rc = -EINVAL;
9270 		goto err_recovery_out;
9271 	}
9272 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
9273 	fw_health->master_func_wait_dsecs =
9274 		le32_to_cpu(resp->master_func_wait_period);
9275 	fw_health->normal_func_wait_dsecs =
9276 		le32_to_cpu(resp->normal_func_wait_period);
9277 	fw_health->post_reset_wait_dsecs =
9278 		le32_to_cpu(resp->master_func_wait_period_after_reset);
9279 	fw_health->post_reset_max_wait_dsecs =
9280 		le32_to_cpu(resp->max_bailout_time_after_reset);
9281 	fw_health->regs[BNXT_FW_HEALTH_REG] =
9282 		le32_to_cpu(resp->fw_health_status_reg);
9283 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
9284 		le32_to_cpu(resp->fw_heartbeat_reg);
9285 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
9286 		le32_to_cpu(resp->fw_reset_cnt_reg);
9287 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
9288 		le32_to_cpu(resp->reset_inprogress_reg);
9289 	fw_health->fw_reset_inprog_reg_mask =
9290 		le32_to_cpu(resp->reset_inprogress_reg_mask);
9291 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
9292 	if (fw_health->fw_reset_seq_cnt >= 16) {
9293 		rc = -EINVAL;
9294 		goto err_recovery_out;
9295 	}
9296 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
9297 		fw_health->fw_reset_seq_regs[i] =
9298 			le32_to_cpu(resp->reset_reg[i]);
9299 		fw_health->fw_reset_seq_vals[i] =
9300 			le32_to_cpu(resp->reset_reg_val[i]);
9301 		fw_health->fw_reset_seq_delay_msec[i] =
9302 			resp->delay_after_reset[i];
9303 	}
9304 err_recovery_out:
9305 	hwrm_req_drop(bp, req);
9306 	if (!rc)
9307 		rc = bnxt_map_fw_health_regs(bp);
9308 	if (rc)
9309 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9310 	return rc;
9311 }
9312 
9313 static int bnxt_hwrm_func_reset(struct bnxt *bp)
9314 {
9315 	struct hwrm_func_reset_input *req;
9316 	int rc;
9317 
9318 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
9319 	if (rc)
9320 		return rc;
9321 
9322 	req->enables = 0;
9323 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
9324 	return hwrm_req_send(bp, req);
9325 }
9326 
9327 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
9328 {
9329 	struct hwrm_nvm_get_dev_info_output nvm_info;
9330 
9331 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
9332 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
9333 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
9334 			 nvm_info.nvm_cfg_ver_upd);
9335 }
9336 
9337 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
9338 {
9339 	struct hwrm_queue_qportcfg_output *resp;
9340 	struct hwrm_queue_qportcfg_input *req;
9341 	u8 i, j, *qptr;
9342 	bool no_rdma;
9343 	int rc = 0;
9344 
9345 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
9346 	if (rc)
9347 		return rc;
9348 
9349 	resp = hwrm_req_hold(bp, req);
9350 	rc = hwrm_req_send(bp, req);
9351 	if (rc)
9352 		goto qportcfg_exit;
9353 
9354 	if (!resp->max_configurable_queues) {
9355 		rc = -EINVAL;
9356 		goto qportcfg_exit;
9357 	}
9358 	bp->max_tc = resp->max_configurable_queues;
9359 	bp->max_lltc = resp->max_configurable_lossless_queues;
9360 	if (bp->max_tc > BNXT_MAX_QUEUE)
9361 		bp->max_tc = BNXT_MAX_QUEUE;
9362 
9363 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
9364 	qptr = &resp->queue_id0;
9365 	for (i = 0, j = 0; i < bp->max_tc; i++) {
9366 		bp->q_info[j].queue_id = *qptr;
9367 		bp->q_ids[i] = *qptr++;
9368 		bp->q_info[j].queue_profile = *qptr++;
9369 		bp->tc_to_qidx[j] = j;
9370 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
9371 		    (no_rdma && BNXT_PF(bp)))
9372 			j++;
9373 	}
9374 	bp->max_q = bp->max_tc;
9375 	bp->max_tc = max_t(u8, j, 1);
9376 
9377 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
9378 		bp->max_tc = 1;
9379 
9380 	if (bp->max_lltc > bp->max_tc)
9381 		bp->max_lltc = bp->max_tc;
9382 
9383 qportcfg_exit:
9384 	hwrm_req_drop(bp, req);
9385 	return rc;
9386 }
9387 
9388 static int bnxt_hwrm_poll(struct bnxt *bp)
9389 {
9390 	struct hwrm_ver_get_input *req;
9391 	int rc;
9392 
9393 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9394 	if (rc)
9395 		return rc;
9396 
9397 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9398 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9399 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9400 
9401 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
9402 	rc = hwrm_req_send(bp, req);
9403 	return rc;
9404 }
9405 
9406 static int bnxt_hwrm_ver_get(struct bnxt *bp)
9407 {
9408 	struct hwrm_ver_get_output *resp;
9409 	struct hwrm_ver_get_input *req;
9410 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
9411 	u32 dev_caps_cfg, hwrm_ver;
9412 	int rc, len;
9413 
9414 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9415 	if (rc)
9416 		return rc;
9417 
9418 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
9419 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
9420 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9421 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9422 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9423 
9424 	resp = hwrm_req_hold(bp, req);
9425 	rc = hwrm_req_send(bp, req);
9426 	if (rc)
9427 		goto hwrm_ver_get_exit;
9428 
9429 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
9430 
9431 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
9432 			     resp->hwrm_intf_min_8b << 8 |
9433 			     resp->hwrm_intf_upd_8b;
9434 	if (resp->hwrm_intf_maj_8b < 1) {
9435 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
9436 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
9437 			    resp->hwrm_intf_upd_8b);
9438 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
9439 	}
9440 
9441 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
9442 			HWRM_VERSION_UPDATE;
9443 
9444 	if (bp->hwrm_spec_code > hwrm_ver)
9445 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
9446 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
9447 			 HWRM_VERSION_UPDATE);
9448 	else
9449 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
9450 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
9451 			 resp->hwrm_intf_upd_8b);
9452 
9453 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
9454 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
9455 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
9456 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
9457 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
9458 		len = FW_VER_STR_LEN;
9459 	} else {
9460 		fw_maj = resp->hwrm_fw_maj_8b;
9461 		fw_min = resp->hwrm_fw_min_8b;
9462 		fw_bld = resp->hwrm_fw_bld_8b;
9463 		fw_rsv = resp->hwrm_fw_rsvd_8b;
9464 		len = BC_HWRM_STR_LEN;
9465 	}
9466 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
9467 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
9468 		 fw_rsv);
9469 
9470 	if (strlen(resp->active_pkg_name)) {
9471 		int fw_ver_len = strlen(bp->fw_ver_str);
9472 
9473 		snprintf(bp->fw_ver_str + fw_ver_len,
9474 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
9475 			 resp->active_pkg_name);
9476 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
9477 	}
9478 
9479 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
9480 	if (!bp->hwrm_cmd_timeout)
9481 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
9482 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
9483 	if (!bp->hwrm_cmd_max_timeout)
9484 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
9485 	else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT)
9486 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n",
9487 			    bp->hwrm_cmd_max_timeout / 1000);
9488 
9489 	if (resp->hwrm_intf_maj_8b >= 1) {
9490 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
9491 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
9492 	}
9493 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
9494 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
9495 
9496 	bp->chip_num = le16_to_cpu(resp->chip_num);
9497 	bp->chip_rev = resp->chip_rev;
9498 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
9499 	    !resp->chip_metal)
9500 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
9501 
9502 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
9503 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
9504 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
9505 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
9506 
9507 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
9508 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
9509 
9510 	if (dev_caps_cfg &
9511 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
9512 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
9513 
9514 	if (dev_caps_cfg &
9515 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
9516 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
9517 
9518 	if (dev_caps_cfg &
9519 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
9520 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
9521 
9522 hwrm_ver_get_exit:
9523 	hwrm_req_drop(bp, req);
9524 	return rc;
9525 }
9526 
9527 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
9528 {
9529 	struct hwrm_fw_set_time_input *req;
9530 	struct tm tm;
9531 	time64_t now = ktime_get_real_seconds();
9532 	int rc;
9533 
9534 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
9535 	    bp->hwrm_spec_code < 0x10400)
9536 		return -EOPNOTSUPP;
9537 
9538 	time64_to_tm(now, 0, &tm);
9539 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
9540 	if (rc)
9541 		return rc;
9542 
9543 	req->year = cpu_to_le16(1900 + tm.tm_year);
9544 	req->month = 1 + tm.tm_mon;
9545 	req->day = tm.tm_mday;
9546 	req->hour = tm.tm_hour;
9547 	req->minute = tm.tm_min;
9548 	req->second = tm.tm_sec;
9549 	return hwrm_req_send(bp, req);
9550 }
9551 
9552 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
9553 {
9554 	u64 sw_tmp;
9555 
9556 	hw &= mask;
9557 	sw_tmp = (*sw & ~mask) | hw;
9558 	if (hw < (*sw & mask))
9559 		sw_tmp += mask + 1;
9560 	WRITE_ONCE(*sw, sw_tmp);
9561 }
9562 
9563 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
9564 				    int count, bool ignore_zero)
9565 {
9566 	int i;
9567 
9568 	for (i = 0; i < count; i++) {
9569 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
9570 
9571 		if (ignore_zero && !hw)
9572 			continue;
9573 
9574 		if (masks[i] == -1ULL)
9575 			sw_stats[i] = hw;
9576 		else
9577 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
9578 	}
9579 }
9580 
9581 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
9582 {
9583 	if (!stats->hw_stats)
9584 		return;
9585 
9586 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
9587 				stats->hw_masks, stats->len / 8, false);
9588 }
9589 
9590 static void bnxt_accumulate_all_stats(struct bnxt *bp)
9591 {
9592 	struct bnxt_stats_mem *ring0_stats;
9593 	bool ignore_zero = false;
9594 	int i;
9595 
9596 	/* Chip bug.  Counter intermittently becomes 0. */
9597 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
9598 		ignore_zero = true;
9599 
9600 	for (i = 0; i < bp->cp_nr_rings; i++) {
9601 		struct bnxt_napi *bnapi = bp->bnapi[i];
9602 		struct bnxt_cp_ring_info *cpr;
9603 		struct bnxt_stats_mem *stats;
9604 
9605 		cpr = &bnapi->cp_ring;
9606 		stats = &cpr->stats;
9607 		if (!i)
9608 			ring0_stats = stats;
9609 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
9610 					ring0_stats->hw_masks,
9611 					ring0_stats->len / 8, ignore_zero);
9612 	}
9613 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
9614 		struct bnxt_stats_mem *stats = &bp->port_stats;
9615 		__le64 *hw_stats = stats->hw_stats;
9616 		u64 *sw_stats = stats->sw_stats;
9617 		u64 *masks = stats->hw_masks;
9618 		int cnt;
9619 
9620 		cnt = sizeof(struct rx_port_stats) / 8;
9621 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
9622 
9623 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9624 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9625 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9626 		cnt = sizeof(struct tx_port_stats) / 8;
9627 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
9628 	}
9629 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
9630 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
9631 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
9632 	}
9633 }
9634 
9635 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
9636 {
9637 	struct hwrm_port_qstats_input *req;
9638 	struct bnxt_pf_info *pf = &bp->pf;
9639 	int rc;
9640 
9641 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
9642 		return 0;
9643 
9644 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
9645 		return -EOPNOTSUPP;
9646 
9647 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
9648 	if (rc)
9649 		return rc;
9650 
9651 	req->flags = flags;
9652 	req->port_id = cpu_to_le16(pf->port_id);
9653 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
9654 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
9655 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
9656 	return hwrm_req_send(bp, req);
9657 }
9658 
9659 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
9660 {
9661 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
9662 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
9663 	struct hwrm_port_qstats_ext_output *resp_qs;
9664 	struct hwrm_port_qstats_ext_input *req_qs;
9665 	struct bnxt_pf_info *pf = &bp->pf;
9666 	u32 tx_stat_size;
9667 	int rc;
9668 
9669 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
9670 		return 0;
9671 
9672 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
9673 		return -EOPNOTSUPP;
9674 
9675 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
9676 	if (rc)
9677 		return rc;
9678 
9679 	req_qs->flags = flags;
9680 	req_qs->port_id = cpu_to_le16(pf->port_id);
9681 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
9682 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
9683 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
9684 		       sizeof(struct tx_port_stats_ext) : 0;
9685 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
9686 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
9687 	resp_qs = hwrm_req_hold(bp, req_qs);
9688 	rc = hwrm_req_send(bp, req_qs);
9689 	if (!rc) {
9690 		bp->fw_rx_stats_ext_size =
9691 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
9692 		if (BNXT_FW_MAJ(bp) < 220 &&
9693 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
9694 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
9695 
9696 		bp->fw_tx_stats_ext_size = tx_stat_size ?
9697 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
9698 	} else {
9699 		bp->fw_rx_stats_ext_size = 0;
9700 		bp->fw_tx_stats_ext_size = 0;
9701 	}
9702 	hwrm_req_drop(bp, req_qs);
9703 
9704 	if (flags)
9705 		return rc;
9706 
9707 	if (bp->fw_tx_stats_ext_size <=
9708 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
9709 		bp->pri2cos_valid = 0;
9710 		return rc;
9711 	}
9712 
9713 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
9714 	if (rc)
9715 		return rc;
9716 
9717 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
9718 
9719 	resp_qc = hwrm_req_hold(bp, req_qc);
9720 	rc = hwrm_req_send(bp, req_qc);
9721 	if (!rc) {
9722 		u8 *pri2cos;
9723 		int i, j;
9724 
9725 		pri2cos = &resp_qc->pri0_cos_queue_id;
9726 		for (i = 0; i < 8; i++) {
9727 			u8 queue_id = pri2cos[i];
9728 			u8 queue_idx;
9729 
9730 			/* Per port queue IDs start from 0, 10, 20, etc */
9731 			queue_idx = queue_id % 10;
9732 			if (queue_idx > BNXT_MAX_QUEUE) {
9733 				bp->pri2cos_valid = false;
9734 				hwrm_req_drop(bp, req_qc);
9735 				return rc;
9736 			}
9737 			for (j = 0; j < bp->max_q; j++) {
9738 				if (bp->q_ids[j] == queue_id)
9739 					bp->pri2cos_idx[i] = queue_idx;
9740 			}
9741 		}
9742 		bp->pri2cos_valid = true;
9743 	}
9744 	hwrm_req_drop(bp, req_qc);
9745 
9746 	return rc;
9747 }
9748 
9749 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
9750 {
9751 	bnxt_hwrm_tunnel_dst_port_free(bp,
9752 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
9753 	bnxt_hwrm_tunnel_dst_port_free(bp,
9754 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
9755 }
9756 
9757 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
9758 {
9759 	int rc, i;
9760 	u32 tpa_flags = 0;
9761 
9762 	if (set_tpa)
9763 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
9764 	else if (BNXT_NO_FW_ACCESS(bp))
9765 		return 0;
9766 	for (i = 0; i < bp->nr_vnics; i++) {
9767 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
9768 		if (rc) {
9769 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
9770 				   i, rc);
9771 			return rc;
9772 		}
9773 	}
9774 	return 0;
9775 }
9776 
9777 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
9778 {
9779 	int i;
9780 
9781 	for (i = 0; i < bp->nr_vnics; i++)
9782 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
9783 }
9784 
9785 static void bnxt_clear_vnic(struct bnxt *bp)
9786 {
9787 	if (!bp->vnic_info)
9788 		return;
9789 
9790 	bnxt_hwrm_clear_vnic_filter(bp);
9791 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
9792 		/* clear all RSS setting before free vnic ctx */
9793 		bnxt_hwrm_clear_vnic_rss(bp);
9794 		bnxt_hwrm_vnic_ctx_free(bp);
9795 	}
9796 	/* before free the vnic, undo the vnic tpa settings */
9797 	if (bp->flags & BNXT_FLAG_TPA)
9798 		bnxt_set_tpa(bp, false);
9799 	bnxt_hwrm_vnic_free(bp);
9800 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
9801 		bnxt_hwrm_vnic_ctx_free(bp);
9802 }
9803 
9804 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
9805 				    bool irq_re_init)
9806 {
9807 	bnxt_clear_vnic(bp);
9808 	bnxt_hwrm_ring_free(bp, close_path);
9809 	bnxt_hwrm_ring_grp_free(bp);
9810 	if (irq_re_init) {
9811 		bnxt_hwrm_stat_ctx_free(bp);
9812 		bnxt_hwrm_free_tunnel_ports(bp);
9813 	}
9814 }
9815 
9816 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
9817 {
9818 	struct hwrm_func_cfg_input *req;
9819 	u8 evb_mode;
9820 	int rc;
9821 
9822 	if (br_mode == BRIDGE_MODE_VEB)
9823 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
9824 	else if (br_mode == BRIDGE_MODE_VEPA)
9825 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
9826 	else
9827 		return -EINVAL;
9828 
9829 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
9830 	if (rc)
9831 		return rc;
9832 
9833 	req->fid = cpu_to_le16(0xffff);
9834 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
9835 	req->evb_mode = evb_mode;
9836 	return hwrm_req_send(bp, req);
9837 }
9838 
9839 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
9840 {
9841 	struct hwrm_func_cfg_input *req;
9842 	int rc;
9843 
9844 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
9845 		return 0;
9846 
9847 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
9848 	if (rc)
9849 		return rc;
9850 
9851 	req->fid = cpu_to_le16(0xffff);
9852 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
9853 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
9854 	if (size == 128)
9855 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
9856 
9857 	return hwrm_req_send(bp, req);
9858 }
9859 
9860 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
9861 {
9862 	int rc;
9863 
9864 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
9865 		goto skip_rss_ctx;
9866 
9867 	/* allocate context for vnic */
9868 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
9869 	if (rc) {
9870 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
9871 			   vnic->vnic_id, rc);
9872 		goto vnic_setup_err;
9873 	}
9874 	bp->rsscos_nr_ctxs++;
9875 
9876 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
9877 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
9878 		if (rc) {
9879 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
9880 				   vnic->vnic_id, rc);
9881 			goto vnic_setup_err;
9882 		}
9883 		bp->rsscos_nr_ctxs++;
9884 	}
9885 
9886 skip_rss_ctx:
9887 	/* configure default vnic, ring grp */
9888 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
9889 	if (rc) {
9890 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
9891 			   vnic->vnic_id, rc);
9892 		goto vnic_setup_err;
9893 	}
9894 
9895 	/* Enable RSS hashing on vnic */
9896 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
9897 	if (rc) {
9898 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
9899 			   vnic->vnic_id, rc);
9900 		goto vnic_setup_err;
9901 	}
9902 
9903 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
9904 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
9905 		if (rc) {
9906 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
9907 				   vnic->vnic_id, rc);
9908 		}
9909 	}
9910 
9911 vnic_setup_err:
9912 	return rc;
9913 }
9914 
9915 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
9916 {
9917 	int rc;
9918 
9919 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
9920 	if (rc) {
9921 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
9922 			   vnic->vnic_id, rc);
9923 		return rc;
9924 	}
9925 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
9926 	if (rc)
9927 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
9928 			   vnic->vnic_id, rc);
9929 	return rc;
9930 }
9931 
9932 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
9933 {
9934 	int rc, i, nr_ctxs;
9935 
9936 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
9937 	for (i = 0; i < nr_ctxs; i++) {
9938 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
9939 		if (rc) {
9940 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
9941 				   vnic->vnic_id, i, rc);
9942 			break;
9943 		}
9944 		bp->rsscos_nr_ctxs++;
9945 	}
9946 	if (i < nr_ctxs)
9947 		return -ENOMEM;
9948 
9949 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
9950 	if (rc)
9951 		return rc;
9952 
9953 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
9954 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
9955 		if (rc) {
9956 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
9957 				   vnic->vnic_id, rc);
9958 		}
9959 	}
9960 	return rc;
9961 }
9962 
9963 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
9964 {
9965 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
9966 		return __bnxt_setup_vnic_p5(bp, vnic);
9967 	else
9968 		return __bnxt_setup_vnic(bp, vnic);
9969 }
9970 
9971 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
9972 				     struct bnxt_vnic_info *vnic,
9973 				     u16 start_rx_ring_idx, int rx_rings)
9974 {
9975 	int rc;
9976 
9977 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
9978 	if (rc) {
9979 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
9980 			   vnic->vnic_id, rc);
9981 		return rc;
9982 	}
9983 	return bnxt_setup_vnic(bp, vnic);
9984 }
9985 
9986 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
9987 {
9988 	struct bnxt_vnic_info *vnic;
9989 	int i, rc = 0;
9990 
9991 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
9992 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
9993 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
9994 	}
9995 
9996 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
9997 		return 0;
9998 
9999 	for (i = 0; i < bp->rx_nr_rings; i++) {
10000 		u16 vnic_id = i + 1;
10001 		u16 ring_id = i;
10002 
10003 		if (vnic_id >= bp->nr_vnics)
10004 			break;
10005 
10006 		vnic = &bp->vnic_info[vnic_id];
10007 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10008 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10009 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10010 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10011 			break;
10012 	}
10013 	return rc;
10014 }
10015 
10016 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10017 			  bool all)
10018 {
10019 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10020 	struct bnxt_filter_base *usr_fltr, *tmp;
10021 	struct bnxt_ntuple_filter *ntp_fltr;
10022 	int i;
10023 
10024 	bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10025 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10026 		if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10027 			bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10028 	}
10029 	if (!all)
10030 		return;
10031 
10032 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10033 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10034 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10035 			ntp_fltr = container_of(usr_fltr,
10036 						struct bnxt_ntuple_filter,
10037 						base);
10038 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10039 			bnxt_del_ntp_filter(bp, ntp_fltr);
10040 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10041 		}
10042 	}
10043 
10044 	if (vnic->rss_table)
10045 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10046 				  vnic->rss_table,
10047 				  vnic->rss_table_dma_addr);
10048 	kfree(rss_ctx->rss_indir_tbl);
10049 	list_del(&rss_ctx->list);
10050 	bp->num_rss_ctx--;
10051 	clear_bit(rss_ctx->index, bp->rss_ctx_bmap);
10052 	kfree(rss_ctx);
10053 }
10054 
10055 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10056 {
10057 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10058 	struct bnxt_rss_ctx *rss_ctx, *tmp;
10059 
10060 	list_for_each_entry_safe(rss_ctx, tmp, &bp->rss_ctx_list, list) {
10061 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10062 
10063 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10064 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10065 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10066 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10067 				   rss_ctx->index);
10068 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10069 		}
10070 	}
10071 }
10072 
10073 struct bnxt_rss_ctx *bnxt_alloc_rss_ctx(struct bnxt *bp)
10074 {
10075 	struct bnxt_rss_ctx *rss_ctx = NULL;
10076 
10077 	rss_ctx = kzalloc(sizeof(*rss_ctx), GFP_KERNEL);
10078 	if (rss_ctx) {
10079 		rss_ctx->vnic.rss_ctx = rss_ctx;
10080 		list_add_tail(&rss_ctx->list, &bp->rss_ctx_list);
10081 		bp->num_rss_ctx++;
10082 	}
10083 	return rss_ctx;
10084 }
10085 
10086 void bnxt_clear_rss_ctxs(struct bnxt *bp, bool all)
10087 {
10088 	struct bnxt_rss_ctx *rss_ctx, *tmp;
10089 
10090 	list_for_each_entry_safe(rss_ctx, tmp, &bp->rss_ctx_list, list)
10091 		bnxt_del_one_rss_ctx(bp, rss_ctx, all);
10092 
10093 	if (all)
10094 		bitmap_free(bp->rss_ctx_bmap);
10095 }
10096 
10097 static void bnxt_init_multi_rss_ctx(struct bnxt *bp)
10098 {
10099 	bp->rss_ctx_bmap = bitmap_zalloc(BNXT_RSS_CTX_BMAP_LEN, GFP_KERNEL);
10100 	if (bp->rss_ctx_bmap) {
10101 		/* burn index 0 since we cannot have context 0 */
10102 		__set_bit(0, bp->rss_ctx_bmap);
10103 		INIT_LIST_HEAD(&bp->rss_ctx_list);
10104 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
10105 	}
10106 }
10107 
10108 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
10109 static bool bnxt_promisc_ok(struct bnxt *bp)
10110 {
10111 #ifdef CONFIG_BNXT_SRIOV
10112 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10113 		return false;
10114 #endif
10115 	return true;
10116 }
10117 
10118 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10119 {
10120 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10121 	unsigned int rc = 0;
10122 
10123 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10124 	if (rc) {
10125 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10126 			   rc);
10127 		return rc;
10128 	}
10129 
10130 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10131 	if (rc) {
10132 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10133 			   rc);
10134 		return rc;
10135 	}
10136 	return rc;
10137 }
10138 
10139 static int bnxt_cfg_rx_mode(struct bnxt *);
10140 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10141 
10142 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10143 {
10144 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10145 	int rc = 0;
10146 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10147 
10148 	if (irq_re_init) {
10149 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10150 		if (rc) {
10151 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10152 				   rc);
10153 			goto err_out;
10154 		}
10155 	}
10156 
10157 	rc = bnxt_hwrm_ring_alloc(bp);
10158 	if (rc) {
10159 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10160 		goto err_out;
10161 	}
10162 
10163 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10164 	if (rc) {
10165 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10166 		goto err_out;
10167 	}
10168 
10169 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10170 		rx_nr_rings--;
10171 
10172 	/* default vnic 0 */
10173 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10174 	if (rc) {
10175 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10176 		goto err_out;
10177 	}
10178 
10179 	if (BNXT_VF(bp))
10180 		bnxt_hwrm_func_qcfg(bp);
10181 
10182 	rc = bnxt_setup_vnic(bp, vnic);
10183 	if (rc)
10184 		goto err_out;
10185 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10186 		bnxt_hwrm_update_rss_hash_cfg(bp);
10187 
10188 	if (bp->flags & BNXT_FLAG_RFS) {
10189 		rc = bnxt_alloc_rfs_vnics(bp);
10190 		if (rc)
10191 			goto err_out;
10192 	}
10193 
10194 	if (bp->flags & BNXT_FLAG_TPA) {
10195 		rc = bnxt_set_tpa(bp, true);
10196 		if (rc)
10197 			goto err_out;
10198 	}
10199 
10200 	if (BNXT_VF(bp))
10201 		bnxt_update_vf_mac(bp);
10202 
10203 	/* Filter for default vnic 0 */
10204 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
10205 	if (rc) {
10206 		if (BNXT_VF(bp) && rc == -ENODEV)
10207 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
10208 		else
10209 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
10210 		goto err_out;
10211 	}
10212 	vnic->uc_filter_count = 1;
10213 
10214 	vnic->rx_mask = 0;
10215 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
10216 		goto skip_rx_mask;
10217 
10218 	if (bp->dev->flags & IFF_BROADCAST)
10219 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10220 
10221 	if (bp->dev->flags & IFF_PROMISC)
10222 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10223 
10224 	if (bp->dev->flags & IFF_ALLMULTI) {
10225 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10226 		vnic->mc_list_count = 0;
10227 	} else if (bp->dev->flags & IFF_MULTICAST) {
10228 		u32 mask = 0;
10229 
10230 		bnxt_mc_list_updated(bp, &mask);
10231 		vnic->rx_mask |= mask;
10232 	}
10233 
10234 	rc = bnxt_cfg_rx_mode(bp);
10235 	if (rc)
10236 		goto err_out;
10237 
10238 skip_rx_mask:
10239 	rc = bnxt_hwrm_set_coal(bp);
10240 	if (rc)
10241 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
10242 				rc);
10243 
10244 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10245 		rc = bnxt_setup_nitroa0_vnic(bp);
10246 		if (rc)
10247 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
10248 				   rc);
10249 	}
10250 
10251 	if (BNXT_VF(bp)) {
10252 		bnxt_hwrm_func_qcfg(bp);
10253 		netdev_update_features(bp->dev);
10254 	}
10255 
10256 	return 0;
10257 
10258 err_out:
10259 	bnxt_hwrm_resource_free(bp, 0, true);
10260 
10261 	return rc;
10262 }
10263 
10264 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
10265 {
10266 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
10267 	return 0;
10268 }
10269 
10270 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
10271 {
10272 	bnxt_init_cp_rings(bp);
10273 	bnxt_init_rx_rings(bp);
10274 	bnxt_init_tx_rings(bp);
10275 	bnxt_init_ring_grps(bp, irq_re_init);
10276 	bnxt_init_vnics(bp);
10277 
10278 	return bnxt_init_chip(bp, irq_re_init);
10279 }
10280 
10281 static int bnxt_set_real_num_queues(struct bnxt *bp)
10282 {
10283 	int rc;
10284 	struct net_device *dev = bp->dev;
10285 
10286 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
10287 					  bp->tx_nr_rings_xdp);
10288 	if (rc)
10289 		return rc;
10290 
10291 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
10292 	if (rc)
10293 		return rc;
10294 
10295 #ifdef CONFIG_RFS_ACCEL
10296 	if (bp->flags & BNXT_FLAG_RFS)
10297 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
10298 #endif
10299 
10300 	return rc;
10301 }
10302 
10303 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10304 			     bool shared)
10305 {
10306 	int _rx = *rx, _tx = *tx;
10307 
10308 	if (shared) {
10309 		*rx = min_t(int, _rx, max);
10310 		*tx = min_t(int, _tx, max);
10311 	} else {
10312 		if (max < 2)
10313 			return -ENOMEM;
10314 
10315 		while (_rx + _tx > max) {
10316 			if (_rx > _tx && _rx > 1)
10317 				_rx--;
10318 			else if (_tx > 1)
10319 				_tx--;
10320 		}
10321 		*rx = _rx;
10322 		*tx = _tx;
10323 	}
10324 	return 0;
10325 }
10326 
10327 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
10328 {
10329 	return (tx - tx_xdp) / tx_sets + tx_xdp;
10330 }
10331 
10332 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
10333 {
10334 	int tcs = bp->num_tc;
10335 
10336 	if (!tcs)
10337 		tcs = 1;
10338 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
10339 }
10340 
10341 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
10342 {
10343 	int tcs = bp->num_tc;
10344 
10345 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
10346 	       bp->tx_nr_rings_xdp;
10347 }
10348 
10349 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10350 			   bool sh)
10351 {
10352 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
10353 
10354 	if (tx_cp != *tx) {
10355 		int tx_saved = tx_cp, rc;
10356 
10357 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
10358 		if (rc)
10359 			return rc;
10360 		if (tx_cp != tx_saved)
10361 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
10362 		return 0;
10363 	}
10364 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
10365 }
10366 
10367 static void bnxt_setup_msix(struct bnxt *bp)
10368 {
10369 	const int len = sizeof(bp->irq_tbl[0].name);
10370 	struct net_device *dev = bp->dev;
10371 	int tcs, i;
10372 
10373 	tcs = bp->num_tc;
10374 	if (tcs) {
10375 		int i, off, count;
10376 
10377 		for (i = 0; i < tcs; i++) {
10378 			count = bp->tx_nr_rings_per_tc;
10379 			off = BNXT_TC_TO_RING_BASE(bp, i);
10380 			netdev_set_tc_queue(dev, i, count, off);
10381 		}
10382 	}
10383 
10384 	for (i = 0; i < bp->cp_nr_rings; i++) {
10385 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10386 		char *attr;
10387 
10388 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
10389 			attr = "TxRx";
10390 		else if (i < bp->rx_nr_rings)
10391 			attr = "rx";
10392 		else
10393 			attr = "tx";
10394 
10395 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
10396 			 attr, i);
10397 		bp->irq_tbl[map_idx].handler = bnxt_msix;
10398 	}
10399 }
10400 
10401 static void bnxt_setup_inta(struct bnxt *bp)
10402 {
10403 	const int len = sizeof(bp->irq_tbl[0].name);
10404 
10405 	if (bp->num_tc) {
10406 		netdev_reset_tc(bp->dev);
10407 		bp->num_tc = 0;
10408 	}
10409 
10410 	snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
10411 		 0);
10412 	bp->irq_tbl[0].handler = bnxt_inta;
10413 }
10414 
10415 static int bnxt_init_int_mode(struct bnxt *bp);
10416 
10417 static int bnxt_setup_int_mode(struct bnxt *bp)
10418 {
10419 	int rc;
10420 
10421 	if (!bp->irq_tbl) {
10422 		rc = bnxt_init_int_mode(bp);
10423 		if (rc || !bp->irq_tbl)
10424 			return rc ?: -ENODEV;
10425 	}
10426 
10427 	if (bp->flags & BNXT_FLAG_USING_MSIX)
10428 		bnxt_setup_msix(bp);
10429 	else
10430 		bnxt_setup_inta(bp);
10431 
10432 	rc = bnxt_set_real_num_queues(bp);
10433 	return rc;
10434 }
10435 
10436 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
10437 {
10438 	return bp->hw_resc.max_rsscos_ctxs;
10439 }
10440 
10441 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
10442 {
10443 	return bp->hw_resc.max_vnics;
10444 }
10445 
10446 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
10447 {
10448 	return bp->hw_resc.max_stat_ctxs;
10449 }
10450 
10451 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
10452 {
10453 	return bp->hw_resc.max_cp_rings;
10454 }
10455 
10456 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
10457 {
10458 	unsigned int cp = bp->hw_resc.max_cp_rings;
10459 
10460 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
10461 		cp -= bnxt_get_ulp_msix_num(bp);
10462 
10463 	return cp;
10464 }
10465 
10466 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
10467 {
10468 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
10469 
10470 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10471 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
10472 
10473 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
10474 }
10475 
10476 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
10477 {
10478 	bp->hw_resc.max_irqs = max_irqs;
10479 }
10480 
10481 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
10482 {
10483 	unsigned int cp;
10484 
10485 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
10486 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10487 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
10488 	else
10489 		return cp - bp->cp_nr_rings;
10490 }
10491 
10492 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
10493 {
10494 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
10495 }
10496 
10497 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
10498 {
10499 	int max_irq = bnxt_get_max_func_irqs(bp);
10500 	int total_req = bp->cp_nr_rings + num;
10501 
10502 	if (max_irq < total_req) {
10503 		num = max_irq - bp->cp_nr_rings;
10504 		if (num <= 0)
10505 			return 0;
10506 	}
10507 	return num;
10508 }
10509 
10510 static int bnxt_get_num_msix(struct bnxt *bp)
10511 {
10512 	if (!BNXT_NEW_RM(bp))
10513 		return bnxt_get_max_func_irqs(bp);
10514 
10515 	return bnxt_nq_rings_in_use(bp);
10516 }
10517 
10518 static int bnxt_init_msix(struct bnxt *bp)
10519 {
10520 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp;
10521 	struct msix_entry *msix_ent;
10522 
10523 	total_vecs = bnxt_get_num_msix(bp);
10524 	max = bnxt_get_max_func_irqs(bp);
10525 	if (total_vecs > max)
10526 		total_vecs = max;
10527 
10528 	if (!total_vecs)
10529 		return 0;
10530 
10531 	msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
10532 	if (!msix_ent)
10533 		return -ENOMEM;
10534 
10535 	for (i = 0; i < total_vecs; i++) {
10536 		msix_ent[i].entry = i;
10537 		msix_ent[i].vector = 0;
10538 	}
10539 
10540 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
10541 		min = 2;
10542 
10543 	total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
10544 	ulp_msix = bnxt_get_ulp_msix_num(bp);
10545 	if (total_vecs < 0 || total_vecs < ulp_msix) {
10546 		rc = -ENODEV;
10547 		goto msix_setup_exit;
10548 	}
10549 
10550 	bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
10551 	if (bp->irq_tbl) {
10552 		for (i = 0; i < total_vecs; i++)
10553 			bp->irq_tbl[i].vector = msix_ent[i].vector;
10554 
10555 		bp->total_irqs = total_vecs;
10556 		/* Trim rings based upon num of vectors allocated */
10557 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
10558 				     total_vecs - ulp_msix, min == 1);
10559 		if (rc)
10560 			goto msix_setup_exit;
10561 
10562 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
10563 		bp->cp_nr_rings = (min == 1) ?
10564 				  max_t(int, tx_cp, bp->rx_nr_rings) :
10565 				  tx_cp + bp->rx_nr_rings;
10566 
10567 	} else {
10568 		rc = -ENOMEM;
10569 		goto msix_setup_exit;
10570 	}
10571 	bp->flags |= BNXT_FLAG_USING_MSIX;
10572 	kfree(msix_ent);
10573 	return 0;
10574 
10575 msix_setup_exit:
10576 	netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
10577 	kfree(bp->irq_tbl);
10578 	bp->irq_tbl = NULL;
10579 	pci_disable_msix(bp->pdev);
10580 	kfree(msix_ent);
10581 	return rc;
10582 }
10583 
10584 static int bnxt_init_inta(struct bnxt *bp)
10585 {
10586 	bp->irq_tbl = kzalloc(sizeof(struct bnxt_irq), GFP_KERNEL);
10587 	if (!bp->irq_tbl)
10588 		return -ENOMEM;
10589 
10590 	bp->total_irqs = 1;
10591 	bp->rx_nr_rings = 1;
10592 	bp->tx_nr_rings = 1;
10593 	bp->cp_nr_rings = 1;
10594 	bp->flags |= BNXT_FLAG_SHARED_RINGS;
10595 	bp->irq_tbl[0].vector = bp->pdev->irq;
10596 	return 0;
10597 }
10598 
10599 static int bnxt_init_int_mode(struct bnxt *bp)
10600 {
10601 	int rc = -ENODEV;
10602 
10603 	if (bp->flags & BNXT_FLAG_MSIX_CAP)
10604 		rc = bnxt_init_msix(bp);
10605 
10606 	if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
10607 		/* fallback to INTA */
10608 		rc = bnxt_init_inta(bp);
10609 	}
10610 	return rc;
10611 }
10612 
10613 static void bnxt_clear_int_mode(struct bnxt *bp)
10614 {
10615 	if (bp->flags & BNXT_FLAG_USING_MSIX)
10616 		pci_disable_msix(bp->pdev);
10617 
10618 	kfree(bp->irq_tbl);
10619 	bp->irq_tbl = NULL;
10620 	bp->flags &= ~BNXT_FLAG_USING_MSIX;
10621 }
10622 
10623 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
10624 {
10625 	bool irq_cleared = false;
10626 	int tcs = bp->num_tc;
10627 	int irqs_required;
10628 	int rc;
10629 
10630 	if (!bnxt_need_reserve_rings(bp))
10631 		return 0;
10632 
10633 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
10634 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
10635 
10636 		if (ulp_msix > bp->ulp_num_msix_want)
10637 			ulp_msix = bp->ulp_num_msix_want;
10638 		irqs_required = ulp_msix + bp->cp_nr_rings;
10639 	} else {
10640 		irqs_required = bnxt_get_num_msix(bp);
10641 	}
10642 
10643 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
10644 		bnxt_ulp_irq_stop(bp);
10645 		bnxt_clear_int_mode(bp);
10646 		irq_cleared = true;
10647 	}
10648 	rc = __bnxt_reserve_rings(bp);
10649 	if (irq_cleared) {
10650 		if (!rc)
10651 			rc = bnxt_init_int_mode(bp);
10652 		bnxt_ulp_irq_restart(bp, rc);
10653 	}
10654 	if (rc) {
10655 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
10656 		return rc;
10657 	}
10658 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
10659 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
10660 		netdev_err(bp->dev, "tx ring reservation failure\n");
10661 		netdev_reset_tc(bp->dev);
10662 		bp->num_tc = 0;
10663 		if (bp->tx_nr_rings_xdp)
10664 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
10665 		else
10666 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
10667 		return -ENOMEM;
10668 	}
10669 	return 0;
10670 }
10671 
10672 static void bnxt_free_irq(struct bnxt *bp)
10673 {
10674 	struct bnxt_irq *irq;
10675 	int i;
10676 
10677 #ifdef CONFIG_RFS_ACCEL
10678 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
10679 	bp->dev->rx_cpu_rmap = NULL;
10680 #endif
10681 	if (!bp->irq_tbl || !bp->bnapi)
10682 		return;
10683 
10684 	for (i = 0; i < bp->cp_nr_rings; i++) {
10685 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10686 
10687 		irq = &bp->irq_tbl[map_idx];
10688 		if (irq->requested) {
10689 			if (irq->have_cpumask) {
10690 				irq_set_affinity_hint(irq->vector, NULL);
10691 				free_cpumask_var(irq->cpu_mask);
10692 				irq->have_cpumask = 0;
10693 			}
10694 			free_irq(irq->vector, bp->bnapi[i]);
10695 		}
10696 
10697 		irq->requested = 0;
10698 	}
10699 }
10700 
10701 static int bnxt_request_irq(struct bnxt *bp)
10702 {
10703 	int i, j, rc = 0;
10704 	unsigned long flags = 0;
10705 #ifdef CONFIG_RFS_ACCEL
10706 	struct cpu_rmap *rmap;
10707 #endif
10708 
10709 	rc = bnxt_setup_int_mode(bp);
10710 	if (rc) {
10711 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
10712 			   rc);
10713 		return rc;
10714 	}
10715 #ifdef CONFIG_RFS_ACCEL
10716 	rmap = bp->dev->rx_cpu_rmap;
10717 #endif
10718 	if (!(bp->flags & BNXT_FLAG_USING_MSIX))
10719 		flags = IRQF_SHARED;
10720 
10721 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
10722 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10723 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
10724 
10725 #ifdef CONFIG_RFS_ACCEL
10726 		if (rmap && bp->bnapi[i]->rx_ring) {
10727 			rc = irq_cpu_rmap_add(rmap, irq->vector);
10728 			if (rc)
10729 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
10730 					    j);
10731 			j++;
10732 		}
10733 #endif
10734 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
10735 				 bp->bnapi[i]);
10736 		if (rc)
10737 			break;
10738 
10739 		netif_napi_set_irq(&bp->bnapi[i]->napi, irq->vector);
10740 		irq->requested = 1;
10741 
10742 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
10743 			int numa_node = dev_to_node(&bp->pdev->dev);
10744 
10745 			irq->have_cpumask = 1;
10746 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
10747 					irq->cpu_mask);
10748 			rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
10749 			if (rc) {
10750 				netdev_warn(bp->dev,
10751 					    "Set affinity failed, IRQ = %d\n",
10752 					    irq->vector);
10753 				break;
10754 			}
10755 		}
10756 	}
10757 	return rc;
10758 }
10759 
10760 static void bnxt_del_napi(struct bnxt *bp)
10761 {
10762 	int i;
10763 
10764 	if (!bp->bnapi)
10765 		return;
10766 
10767 	for (i = 0; i < bp->rx_nr_rings; i++)
10768 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
10769 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
10770 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
10771 
10772 	for (i = 0; i < bp->cp_nr_rings; i++) {
10773 		struct bnxt_napi *bnapi = bp->bnapi[i];
10774 
10775 		__netif_napi_del(&bnapi->napi);
10776 	}
10777 	/* We called __netif_napi_del(), we need
10778 	 * to respect an RCU grace period before freeing napi structures.
10779 	 */
10780 	synchronize_net();
10781 }
10782 
10783 static void bnxt_init_napi(struct bnxt *bp)
10784 {
10785 	int i;
10786 	unsigned int cp_nr_rings = bp->cp_nr_rings;
10787 	struct bnxt_napi *bnapi;
10788 
10789 	if (bp->flags & BNXT_FLAG_USING_MSIX) {
10790 		int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
10791 
10792 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10793 			poll_fn = bnxt_poll_p5;
10794 		else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10795 			cp_nr_rings--;
10796 		for (i = 0; i < cp_nr_rings; i++) {
10797 			bnapi = bp->bnapi[i];
10798 			netif_napi_add(bp->dev, &bnapi->napi, poll_fn);
10799 		}
10800 		if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10801 			bnapi = bp->bnapi[cp_nr_rings];
10802 			netif_napi_add(bp->dev, &bnapi->napi,
10803 				       bnxt_poll_nitroa0);
10804 		}
10805 	} else {
10806 		bnapi = bp->bnapi[0];
10807 		netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll);
10808 	}
10809 }
10810 
10811 static void bnxt_disable_napi(struct bnxt *bp)
10812 {
10813 	int i;
10814 
10815 	if (!bp->bnapi ||
10816 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
10817 		return;
10818 
10819 	for (i = 0; i < bp->cp_nr_rings; i++) {
10820 		struct bnxt_napi *bnapi = bp->bnapi[i];
10821 		struct bnxt_cp_ring_info *cpr;
10822 
10823 		cpr = &bnapi->cp_ring;
10824 		if (bnapi->tx_fault)
10825 			cpr->sw_stats->tx.tx_resets++;
10826 		if (bnapi->in_reset)
10827 			cpr->sw_stats->rx.rx_resets++;
10828 		napi_disable(&bnapi->napi);
10829 		if (bnapi->rx_ring)
10830 			cancel_work_sync(&cpr->dim.work);
10831 	}
10832 }
10833 
10834 static void bnxt_enable_napi(struct bnxt *bp)
10835 {
10836 	int i;
10837 
10838 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
10839 	for (i = 0; i < bp->cp_nr_rings; i++) {
10840 		struct bnxt_napi *bnapi = bp->bnapi[i];
10841 		struct bnxt_cp_ring_info *cpr;
10842 
10843 		bnapi->tx_fault = 0;
10844 
10845 		cpr = &bnapi->cp_ring;
10846 		bnapi->in_reset = false;
10847 
10848 		if (bnapi->rx_ring) {
10849 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
10850 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
10851 		}
10852 		napi_enable(&bnapi->napi);
10853 	}
10854 }
10855 
10856 void bnxt_tx_disable(struct bnxt *bp)
10857 {
10858 	int i;
10859 	struct bnxt_tx_ring_info *txr;
10860 
10861 	if (bp->tx_ring) {
10862 		for (i = 0; i < bp->tx_nr_rings; i++) {
10863 			txr = &bp->tx_ring[i];
10864 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
10865 		}
10866 	}
10867 	/* Make sure napi polls see @dev_state change */
10868 	synchronize_net();
10869 	/* Drop carrier first to prevent TX timeout */
10870 	netif_carrier_off(bp->dev);
10871 	/* Stop all TX queues */
10872 	netif_tx_disable(bp->dev);
10873 }
10874 
10875 void bnxt_tx_enable(struct bnxt *bp)
10876 {
10877 	int i;
10878 	struct bnxt_tx_ring_info *txr;
10879 
10880 	for (i = 0; i < bp->tx_nr_rings; i++) {
10881 		txr = &bp->tx_ring[i];
10882 		WRITE_ONCE(txr->dev_state, 0);
10883 	}
10884 	/* Make sure napi polls see @dev_state change */
10885 	synchronize_net();
10886 	netif_tx_wake_all_queues(bp->dev);
10887 	if (BNXT_LINK_IS_UP(bp))
10888 		netif_carrier_on(bp->dev);
10889 }
10890 
10891 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
10892 {
10893 	u8 active_fec = link_info->active_fec_sig_mode &
10894 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
10895 
10896 	switch (active_fec) {
10897 	default:
10898 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
10899 		return "None";
10900 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
10901 		return "Clause 74 BaseR";
10902 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
10903 		return "Clause 91 RS(528,514)";
10904 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
10905 		return "Clause 91 RS544_1XN";
10906 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
10907 		return "Clause 91 RS(544,514)";
10908 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
10909 		return "Clause 91 RS272_1XN";
10910 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
10911 		return "Clause 91 RS(272,257)";
10912 	}
10913 }
10914 
10915 void bnxt_report_link(struct bnxt *bp)
10916 {
10917 	if (BNXT_LINK_IS_UP(bp)) {
10918 		const char *signal = "";
10919 		const char *flow_ctrl;
10920 		const char *duplex;
10921 		u32 speed;
10922 		u16 fec;
10923 
10924 		netif_carrier_on(bp->dev);
10925 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
10926 		if (speed == SPEED_UNKNOWN) {
10927 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
10928 			return;
10929 		}
10930 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
10931 			duplex = "full";
10932 		else
10933 			duplex = "half";
10934 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
10935 			flow_ctrl = "ON - receive & transmit";
10936 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
10937 			flow_ctrl = "ON - transmit";
10938 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
10939 			flow_ctrl = "ON - receive";
10940 		else
10941 			flow_ctrl = "none";
10942 		if (bp->link_info.phy_qcfg_resp.option_flags &
10943 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
10944 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
10945 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
10946 			switch (sig_mode) {
10947 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
10948 				signal = "(NRZ) ";
10949 				break;
10950 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
10951 				signal = "(PAM4 56Gbps) ";
10952 				break;
10953 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
10954 				signal = "(PAM4 112Gbps) ";
10955 				break;
10956 			default:
10957 				break;
10958 			}
10959 		}
10960 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
10961 			    speed, signal, duplex, flow_ctrl);
10962 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
10963 			netdev_info(bp->dev, "EEE is %s\n",
10964 				    bp->eee.eee_active ? "active" :
10965 							 "not active");
10966 		fec = bp->link_info.fec_cfg;
10967 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
10968 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
10969 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
10970 				    bnxt_report_fec(&bp->link_info));
10971 	} else {
10972 		netif_carrier_off(bp->dev);
10973 		netdev_err(bp->dev, "NIC Link is Down\n");
10974 	}
10975 }
10976 
10977 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
10978 {
10979 	if (!resp->supported_speeds_auto_mode &&
10980 	    !resp->supported_speeds_force_mode &&
10981 	    !resp->supported_pam4_speeds_auto_mode &&
10982 	    !resp->supported_pam4_speeds_force_mode &&
10983 	    !resp->supported_speeds2_auto_mode &&
10984 	    !resp->supported_speeds2_force_mode)
10985 		return true;
10986 	return false;
10987 }
10988 
10989 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
10990 {
10991 	struct bnxt_link_info *link_info = &bp->link_info;
10992 	struct hwrm_port_phy_qcaps_output *resp;
10993 	struct hwrm_port_phy_qcaps_input *req;
10994 	int rc = 0;
10995 
10996 	if (bp->hwrm_spec_code < 0x10201)
10997 		return 0;
10998 
10999 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
11000 	if (rc)
11001 		return rc;
11002 
11003 	resp = hwrm_req_hold(bp, req);
11004 	rc = hwrm_req_send(bp, req);
11005 	if (rc)
11006 		goto hwrm_phy_qcaps_exit;
11007 
11008 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
11009 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
11010 		struct ethtool_keee *eee = &bp->eee;
11011 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
11012 
11013 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
11014 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
11015 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11016 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11017 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11018 	}
11019 
11020 	if (bp->hwrm_spec_code >= 0x10a01) {
11021 		if (bnxt_phy_qcaps_no_speed(resp)) {
11022 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11023 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11024 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11025 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11026 			netdev_info(bp->dev, "Ethernet link enabled\n");
11027 			/* Phy re-enabled, reprobe the speeds */
11028 			link_info->support_auto_speeds = 0;
11029 			link_info->support_pam4_auto_speeds = 0;
11030 			link_info->support_auto_speeds2 = 0;
11031 		}
11032 	}
11033 	if (resp->supported_speeds_auto_mode)
11034 		link_info->support_auto_speeds =
11035 			le16_to_cpu(resp->supported_speeds_auto_mode);
11036 	if (resp->supported_pam4_speeds_auto_mode)
11037 		link_info->support_pam4_auto_speeds =
11038 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11039 	if (resp->supported_speeds2_auto_mode)
11040 		link_info->support_auto_speeds2 =
11041 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11042 
11043 	bp->port_count = resp->port_cnt;
11044 
11045 hwrm_phy_qcaps_exit:
11046 	hwrm_req_drop(bp, req);
11047 	return rc;
11048 }
11049 
11050 static bool bnxt_support_dropped(u16 advertising, u16 supported)
11051 {
11052 	u16 diff = advertising ^ supported;
11053 
11054 	return ((supported | diff) != supported);
11055 }
11056 
11057 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
11058 {
11059 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
11060 
11061 	/* Check if any advertised speeds are no longer supported. The caller
11062 	 * holds the link_lock mutex, so we can modify link_info settings.
11063 	 */
11064 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11065 		if (bnxt_support_dropped(link_info->advertising,
11066 					 link_info->support_auto_speeds2)) {
11067 			link_info->advertising = link_info->support_auto_speeds2;
11068 			return true;
11069 		}
11070 		return false;
11071 	}
11072 	if (bnxt_support_dropped(link_info->advertising,
11073 				 link_info->support_auto_speeds)) {
11074 		link_info->advertising = link_info->support_auto_speeds;
11075 		return true;
11076 	}
11077 	if (bnxt_support_dropped(link_info->advertising_pam4,
11078 				 link_info->support_pam4_auto_speeds)) {
11079 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
11080 		return true;
11081 	}
11082 	return false;
11083 }
11084 
11085 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
11086 {
11087 	struct bnxt_link_info *link_info = &bp->link_info;
11088 	struct hwrm_port_phy_qcfg_output *resp;
11089 	struct hwrm_port_phy_qcfg_input *req;
11090 	u8 link_state = link_info->link_state;
11091 	bool support_changed;
11092 	int rc;
11093 
11094 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
11095 	if (rc)
11096 		return rc;
11097 
11098 	resp = hwrm_req_hold(bp, req);
11099 	rc = hwrm_req_send(bp, req);
11100 	if (rc) {
11101 		hwrm_req_drop(bp, req);
11102 		if (BNXT_VF(bp) && rc == -ENODEV) {
11103 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
11104 			rc = 0;
11105 		}
11106 		return rc;
11107 	}
11108 
11109 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
11110 	link_info->phy_link_status = resp->link;
11111 	link_info->duplex = resp->duplex_cfg;
11112 	if (bp->hwrm_spec_code >= 0x10800)
11113 		link_info->duplex = resp->duplex_state;
11114 	link_info->pause = resp->pause;
11115 	link_info->auto_mode = resp->auto_mode;
11116 	link_info->auto_pause_setting = resp->auto_pause;
11117 	link_info->lp_pause = resp->link_partner_adv_pause;
11118 	link_info->force_pause_setting = resp->force_pause;
11119 	link_info->duplex_setting = resp->duplex_cfg;
11120 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
11121 		link_info->link_speed = le16_to_cpu(resp->link_speed);
11122 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
11123 			link_info->active_lanes = resp->active_lanes;
11124 	} else {
11125 		link_info->link_speed = 0;
11126 		link_info->active_lanes = 0;
11127 	}
11128 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
11129 	link_info->force_pam4_link_speed =
11130 		le16_to_cpu(resp->force_pam4_link_speed);
11131 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
11132 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
11133 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
11134 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
11135 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
11136 	link_info->auto_pam4_link_speeds =
11137 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
11138 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
11139 	link_info->lp_auto_link_speeds =
11140 		le16_to_cpu(resp->link_partner_adv_speeds);
11141 	link_info->lp_auto_pam4_link_speeds =
11142 		resp->link_partner_pam4_adv_speeds;
11143 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
11144 	link_info->phy_ver[0] = resp->phy_maj;
11145 	link_info->phy_ver[1] = resp->phy_min;
11146 	link_info->phy_ver[2] = resp->phy_bld;
11147 	link_info->media_type = resp->media_type;
11148 	link_info->phy_type = resp->phy_type;
11149 	link_info->transceiver = resp->xcvr_pkg_type;
11150 	link_info->phy_addr = resp->eee_config_phy_addr &
11151 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
11152 	link_info->module_status = resp->module_status;
11153 
11154 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
11155 		struct ethtool_keee *eee = &bp->eee;
11156 		u16 fw_speeds;
11157 
11158 		eee->eee_active = 0;
11159 		if (resp->eee_config_phy_addr &
11160 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
11161 			eee->eee_active = 1;
11162 			fw_speeds = le16_to_cpu(
11163 				resp->link_partner_adv_eee_link_speed_mask);
11164 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
11165 		}
11166 
11167 		/* Pull initial EEE config */
11168 		if (!chng_link_state) {
11169 			if (resp->eee_config_phy_addr &
11170 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
11171 				eee->eee_enabled = 1;
11172 
11173 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
11174 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
11175 
11176 			if (resp->eee_config_phy_addr &
11177 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
11178 				__le32 tmr;
11179 
11180 				eee->tx_lpi_enabled = 1;
11181 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
11182 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
11183 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
11184 			}
11185 		}
11186 	}
11187 
11188 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
11189 	if (bp->hwrm_spec_code >= 0x10504) {
11190 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
11191 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
11192 	}
11193 	/* TODO: need to add more logic to report VF link */
11194 	if (chng_link_state) {
11195 		if (link_info->phy_link_status == BNXT_LINK_LINK)
11196 			link_info->link_state = BNXT_LINK_STATE_UP;
11197 		else
11198 			link_info->link_state = BNXT_LINK_STATE_DOWN;
11199 		if (link_state != link_info->link_state)
11200 			bnxt_report_link(bp);
11201 	} else {
11202 		/* always link down if not require to update link state */
11203 		link_info->link_state = BNXT_LINK_STATE_DOWN;
11204 	}
11205 	hwrm_req_drop(bp, req);
11206 
11207 	if (!BNXT_PHY_CFG_ABLE(bp))
11208 		return 0;
11209 
11210 	support_changed = bnxt_support_speed_dropped(link_info);
11211 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
11212 		bnxt_hwrm_set_link_setting(bp, true, false);
11213 	return 0;
11214 }
11215 
11216 static void bnxt_get_port_module_status(struct bnxt *bp)
11217 {
11218 	struct bnxt_link_info *link_info = &bp->link_info;
11219 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
11220 	u8 module_status;
11221 
11222 	if (bnxt_update_link(bp, true))
11223 		return;
11224 
11225 	module_status = link_info->module_status;
11226 	switch (module_status) {
11227 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
11228 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
11229 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
11230 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
11231 			    bp->pf.port_id);
11232 		if (bp->hwrm_spec_code >= 0x10201) {
11233 			netdev_warn(bp->dev, "Module part number %s\n",
11234 				    resp->phy_vendor_partnumber);
11235 		}
11236 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
11237 			netdev_warn(bp->dev, "TX is disabled\n");
11238 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
11239 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
11240 	}
11241 }
11242 
11243 static void
11244 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11245 {
11246 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
11247 		if (bp->hwrm_spec_code >= 0x10201)
11248 			req->auto_pause =
11249 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
11250 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11251 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
11252 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11253 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
11254 		req->enables |=
11255 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11256 	} else {
11257 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11258 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
11259 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11260 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
11261 		req->enables |=
11262 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
11263 		if (bp->hwrm_spec_code >= 0x10201) {
11264 			req->auto_pause = req->force_pause;
11265 			req->enables |= cpu_to_le32(
11266 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11267 		}
11268 	}
11269 }
11270 
11271 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11272 {
11273 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
11274 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
11275 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11276 			req->enables |=
11277 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
11278 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
11279 		} else if (bp->link_info.advertising) {
11280 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
11281 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
11282 		}
11283 		if (bp->link_info.advertising_pam4) {
11284 			req->enables |=
11285 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
11286 			req->auto_link_pam4_speed_mask =
11287 				cpu_to_le16(bp->link_info.advertising_pam4);
11288 		}
11289 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
11290 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
11291 	} else {
11292 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
11293 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11294 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
11295 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
11296 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
11297 				   (u32)bp->link_info.req_link_speed);
11298 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
11299 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11300 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
11301 		} else {
11302 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11303 		}
11304 	}
11305 
11306 	/* tell chimp that the setting takes effect immediately */
11307 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
11308 }
11309 
11310 int bnxt_hwrm_set_pause(struct bnxt *bp)
11311 {
11312 	struct hwrm_port_phy_cfg_input *req;
11313 	int rc;
11314 
11315 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11316 	if (rc)
11317 		return rc;
11318 
11319 	bnxt_hwrm_set_pause_common(bp, req);
11320 
11321 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
11322 	    bp->link_info.force_link_chng)
11323 		bnxt_hwrm_set_link_common(bp, req);
11324 
11325 	rc = hwrm_req_send(bp, req);
11326 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
11327 		/* since changing of pause setting doesn't trigger any link
11328 		 * change event, the driver needs to update the current pause
11329 		 * result upon successfully return of the phy_cfg command
11330 		 */
11331 		bp->link_info.pause =
11332 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
11333 		bp->link_info.auto_pause_setting = 0;
11334 		if (!bp->link_info.force_link_chng)
11335 			bnxt_report_link(bp);
11336 	}
11337 	bp->link_info.force_link_chng = false;
11338 	return rc;
11339 }
11340 
11341 static void bnxt_hwrm_set_eee(struct bnxt *bp,
11342 			      struct hwrm_port_phy_cfg_input *req)
11343 {
11344 	struct ethtool_keee *eee = &bp->eee;
11345 
11346 	if (eee->eee_enabled) {
11347 		u16 eee_speeds;
11348 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
11349 
11350 		if (eee->tx_lpi_enabled)
11351 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
11352 		else
11353 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
11354 
11355 		req->flags |= cpu_to_le32(flags);
11356 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
11357 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
11358 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
11359 	} else {
11360 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
11361 	}
11362 }
11363 
11364 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
11365 {
11366 	struct hwrm_port_phy_cfg_input *req;
11367 	int rc;
11368 
11369 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11370 	if (rc)
11371 		return rc;
11372 
11373 	if (set_pause)
11374 		bnxt_hwrm_set_pause_common(bp, req);
11375 
11376 	bnxt_hwrm_set_link_common(bp, req);
11377 
11378 	if (set_eee)
11379 		bnxt_hwrm_set_eee(bp, req);
11380 	return hwrm_req_send(bp, req);
11381 }
11382 
11383 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
11384 {
11385 	struct hwrm_port_phy_cfg_input *req;
11386 	int rc;
11387 
11388 	if (!BNXT_SINGLE_PF(bp))
11389 		return 0;
11390 
11391 	if (pci_num_vf(bp->pdev) &&
11392 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
11393 		return 0;
11394 
11395 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11396 	if (rc)
11397 		return rc;
11398 
11399 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
11400 	rc = hwrm_req_send(bp, req);
11401 	if (!rc) {
11402 		mutex_lock(&bp->link_lock);
11403 		/* Device is not obliged link down in certain scenarios, even
11404 		 * when forced. Setting the state unknown is consistent with
11405 		 * driver startup and will force link state to be reported
11406 		 * during subsequent open based on PORT_PHY_QCFG.
11407 		 */
11408 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
11409 		mutex_unlock(&bp->link_lock);
11410 	}
11411 	return rc;
11412 }
11413 
11414 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
11415 {
11416 #ifdef CONFIG_TEE_BNXT_FW
11417 	int rc = tee_bnxt_fw_load();
11418 
11419 	if (rc)
11420 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
11421 
11422 	return rc;
11423 #else
11424 	netdev_err(bp->dev, "OP-TEE not supported\n");
11425 	return -ENODEV;
11426 #endif
11427 }
11428 
11429 static int bnxt_try_recover_fw(struct bnxt *bp)
11430 {
11431 	if (bp->fw_health && bp->fw_health->status_reliable) {
11432 		int retry = 0, rc;
11433 		u32 sts;
11434 
11435 		do {
11436 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
11437 			rc = bnxt_hwrm_poll(bp);
11438 			if (!BNXT_FW_IS_BOOTING(sts) &&
11439 			    !BNXT_FW_IS_RECOVERING(sts))
11440 				break;
11441 			retry++;
11442 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
11443 
11444 		if (!BNXT_FW_IS_HEALTHY(sts)) {
11445 			netdev_err(bp->dev,
11446 				   "Firmware not responding, status: 0x%x\n",
11447 				   sts);
11448 			rc = -ENODEV;
11449 		}
11450 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
11451 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
11452 			return bnxt_fw_reset_via_optee(bp);
11453 		}
11454 		return rc;
11455 	}
11456 
11457 	return -ENODEV;
11458 }
11459 
11460 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
11461 {
11462 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11463 
11464 	if (!BNXT_NEW_RM(bp))
11465 		return; /* no resource reservations required */
11466 
11467 	hw_resc->resv_cp_rings = 0;
11468 	hw_resc->resv_stat_ctxs = 0;
11469 	hw_resc->resv_irqs = 0;
11470 	hw_resc->resv_tx_rings = 0;
11471 	hw_resc->resv_rx_rings = 0;
11472 	hw_resc->resv_hw_ring_grps = 0;
11473 	hw_resc->resv_vnics = 0;
11474 	hw_resc->resv_rsscos_ctxs = 0;
11475 	if (!fw_reset) {
11476 		bp->tx_nr_rings = 0;
11477 		bp->rx_nr_rings = 0;
11478 	}
11479 }
11480 
11481 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
11482 {
11483 	int rc;
11484 
11485 	if (!BNXT_NEW_RM(bp))
11486 		return 0; /* no resource reservations required */
11487 
11488 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
11489 	if (rc)
11490 		netdev_err(bp->dev, "resc_qcaps failed\n");
11491 
11492 	bnxt_clear_reservations(bp, fw_reset);
11493 
11494 	return rc;
11495 }
11496 
11497 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
11498 {
11499 	struct hwrm_func_drv_if_change_output *resp;
11500 	struct hwrm_func_drv_if_change_input *req;
11501 	bool fw_reset = !bp->irq_tbl;
11502 	bool resc_reinit = false;
11503 	int rc, retry = 0;
11504 	u32 flags = 0;
11505 
11506 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
11507 		return 0;
11508 
11509 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
11510 	if (rc)
11511 		return rc;
11512 
11513 	if (up)
11514 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
11515 	resp = hwrm_req_hold(bp, req);
11516 
11517 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
11518 	while (retry < BNXT_FW_IF_RETRY) {
11519 		rc = hwrm_req_send(bp, req);
11520 		if (rc != -EAGAIN)
11521 			break;
11522 
11523 		msleep(50);
11524 		retry++;
11525 	}
11526 
11527 	if (rc == -EAGAIN) {
11528 		hwrm_req_drop(bp, req);
11529 		return rc;
11530 	} else if (!rc) {
11531 		flags = le32_to_cpu(resp->flags);
11532 	} else if (up) {
11533 		rc = bnxt_try_recover_fw(bp);
11534 		fw_reset = true;
11535 	}
11536 	hwrm_req_drop(bp, req);
11537 	if (rc)
11538 		return rc;
11539 
11540 	if (!up) {
11541 		bnxt_inv_fw_health_reg(bp);
11542 		return 0;
11543 	}
11544 
11545 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
11546 		resc_reinit = true;
11547 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
11548 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
11549 		fw_reset = true;
11550 	else
11551 		bnxt_remap_fw_health_regs(bp);
11552 
11553 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
11554 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
11555 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
11556 		return -ENODEV;
11557 	}
11558 	if (resc_reinit || fw_reset) {
11559 		if (fw_reset) {
11560 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11561 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11562 				bnxt_ulp_irq_stop(bp);
11563 			bnxt_free_ctx_mem(bp);
11564 			bnxt_dcb_free(bp);
11565 			rc = bnxt_fw_init_one(bp);
11566 			if (rc) {
11567 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11568 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
11569 				return rc;
11570 			}
11571 			bnxt_clear_int_mode(bp);
11572 			rc = bnxt_init_int_mode(bp);
11573 			if (rc) {
11574 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11575 				netdev_err(bp->dev, "init int mode failed\n");
11576 				return rc;
11577 			}
11578 		}
11579 		rc = bnxt_cancel_reservations(bp, fw_reset);
11580 	}
11581 	return rc;
11582 }
11583 
11584 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
11585 {
11586 	struct hwrm_port_led_qcaps_output *resp;
11587 	struct hwrm_port_led_qcaps_input *req;
11588 	struct bnxt_pf_info *pf = &bp->pf;
11589 	int rc;
11590 
11591 	bp->num_leds = 0;
11592 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
11593 		return 0;
11594 
11595 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
11596 	if (rc)
11597 		return rc;
11598 
11599 	req->port_id = cpu_to_le16(pf->port_id);
11600 	resp = hwrm_req_hold(bp, req);
11601 	rc = hwrm_req_send(bp, req);
11602 	if (rc) {
11603 		hwrm_req_drop(bp, req);
11604 		return rc;
11605 	}
11606 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
11607 		int i;
11608 
11609 		bp->num_leds = resp->num_leds;
11610 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
11611 						 bp->num_leds);
11612 		for (i = 0; i < bp->num_leds; i++) {
11613 			struct bnxt_led_info *led = &bp->leds[i];
11614 			__le16 caps = led->led_state_caps;
11615 
11616 			if (!led->led_group_id ||
11617 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
11618 				bp->num_leds = 0;
11619 				break;
11620 			}
11621 		}
11622 	}
11623 	hwrm_req_drop(bp, req);
11624 	return 0;
11625 }
11626 
11627 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
11628 {
11629 	struct hwrm_wol_filter_alloc_output *resp;
11630 	struct hwrm_wol_filter_alloc_input *req;
11631 	int rc;
11632 
11633 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
11634 	if (rc)
11635 		return rc;
11636 
11637 	req->port_id = cpu_to_le16(bp->pf.port_id);
11638 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
11639 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
11640 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
11641 
11642 	resp = hwrm_req_hold(bp, req);
11643 	rc = hwrm_req_send(bp, req);
11644 	if (!rc)
11645 		bp->wol_filter_id = resp->wol_filter_id;
11646 	hwrm_req_drop(bp, req);
11647 	return rc;
11648 }
11649 
11650 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
11651 {
11652 	struct hwrm_wol_filter_free_input *req;
11653 	int rc;
11654 
11655 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
11656 	if (rc)
11657 		return rc;
11658 
11659 	req->port_id = cpu_to_le16(bp->pf.port_id);
11660 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
11661 	req->wol_filter_id = bp->wol_filter_id;
11662 
11663 	return hwrm_req_send(bp, req);
11664 }
11665 
11666 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
11667 {
11668 	struct hwrm_wol_filter_qcfg_output *resp;
11669 	struct hwrm_wol_filter_qcfg_input *req;
11670 	u16 next_handle = 0;
11671 	int rc;
11672 
11673 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
11674 	if (rc)
11675 		return rc;
11676 
11677 	req->port_id = cpu_to_le16(bp->pf.port_id);
11678 	req->handle = cpu_to_le16(handle);
11679 	resp = hwrm_req_hold(bp, req);
11680 	rc = hwrm_req_send(bp, req);
11681 	if (!rc) {
11682 		next_handle = le16_to_cpu(resp->next_handle);
11683 		if (next_handle != 0) {
11684 			if (resp->wol_type ==
11685 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
11686 				bp->wol = 1;
11687 				bp->wol_filter_id = resp->wol_filter_id;
11688 			}
11689 		}
11690 	}
11691 	hwrm_req_drop(bp, req);
11692 	return next_handle;
11693 }
11694 
11695 static void bnxt_get_wol_settings(struct bnxt *bp)
11696 {
11697 	u16 handle = 0;
11698 
11699 	bp->wol = 0;
11700 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
11701 		return;
11702 
11703 	do {
11704 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
11705 	} while (handle && handle != 0xffff);
11706 }
11707 
11708 static bool bnxt_eee_config_ok(struct bnxt *bp)
11709 {
11710 	struct ethtool_keee *eee = &bp->eee;
11711 	struct bnxt_link_info *link_info = &bp->link_info;
11712 
11713 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
11714 		return true;
11715 
11716 	if (eee->eee_enabled) {
11717 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
11718 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
11719 
11720 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
11721 
11722 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
11723 			eee->eee_enabled = 0;
11724 			return false;
11725 		}
11726 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
11727 			linkmode_and(eee->advertised, advertising,
11728 				     eee->supported);
11729 			return false;
11730 		}
11731 	}
11732 	return true;
11733 }
11734 
11735 static int bnxt_update_phy_setting(struct bnxt *bp)
11736 {
11737 	int rc;
11738 	bool update_link = false;
11739 	bool update_pause = false;
11740 	bool update_eee = false;
11741 	struct bnxt_link_info *link_info = &bp->link_info;
11742 
11743 	rc = bnxt_update_link(bp, true);
11744 	if (rc) {
11745 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
11746 			   rc);
11747 		return rc;
11748 	}
11749 	if (!BNXT_SINGLE_PF(bp))
11750 		return 0;
11751 
11752 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
11753 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
11754 	    link_info->req_flow_ctrl)
11755 		update_pause = true;
11756 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
11757 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
11758 		update_pause = true;
11759 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
11760 		if (BNXT_AUTO_MODE(link_info->auto_mode))
11761 			update_link = true;
11762 		if (bnxt_force_speed_updated(link_info))
11763 			update_link = true;
11764 		if (link_info->req_duplex != link_info->duplex_setting)
11765 			update_link = true;
11766 	} else {
11767 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
11768 			update_link = true;
11769 		if (bnxt_auto_speed_updated(link_info))
11770 			update_link = true;
11771 	}
11772 
11773 	/* The last close may have shutdown the link, so need to call
11774 	 * PHY_CFG to bring it back up.
11775 	 */
11776 	if (!BNXT_LINK_IS_UP(bp))
11777 		update_link = true;
11778 
11779 	if (!bnxt_eee_config_ok(bp))
11780 		update_eee = true;
11781 
11782 	if (update_link)
11783 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
11784 	else if (update_pause)
11785 		rc = bnxt_hwrm_set_pause(bp);
11786 	if (rc) {
11787 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
11788 			   rc);
11789 		return rc;
11790 	}
11791 
11792 	return rc;
11793 }
11794 
11795 /* Common routine to pre-map certain register block to different GRC window.
11796  * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
11797  * in PF and 3 windows in VF that can be customized to map in different
11798  * register blocks.
11799  */
11800 static void bnxt_preset_reg_win(struct bnxt *bp)
11801 {
11802 	if (BNXT_PF(bp)) {
11803 		/* CAG registers map to GRC window #4 */
11804 		writel(BNXT_CAG_REG_BASE,
11805 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
11806 	}
11807 }
11808 
11809 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
11810 
11811 static int bnxt_reinit_after_abort(struct bnxt *bp)
11812 {
11813 	int rc;
11814 
11815 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11816 		return -EBUSY;
11817 
11818 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
11819 		return -ENODEV;
11820 
11821 	rc = bnxt_fw_init_one(bp);
11822 	if (!rc) {
11823 		bnxt_clear_int_mode(bp);
11824 		rc = bnxt_init_int_mode(bp);
11825 		if (!rc) {
11826 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
11827 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
11828 		}
11829 	}
11830 	return rc;
11831 }
11832 
11833 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
11834 {
11835 	struct bnxt_ntuple_filter *ntp_fltr;
11836 	struct bnxt_l2_filter *l2_fltr;
11837 
11838 	if (list_empty(&fltr->list))
11839 		return;
11840 
11841 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
11842 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
11843 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
11844 		atomic_inc(&l2_fltr->refcnt);
11845 		ntp_fltr->l2_fltr = l2_fltr;
11846 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
11847 			bnxt_del_ntp_filter(bp, ntp_fltr);
11848 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
11849 				   fltr->sw_id);
11850 		}
11851 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
11852 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
11853 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
11854 			bnxt_del_l2_filter(bp, l2_fltr);
11855 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
11856 				   fltr->sw_id);
11857 		}
11858 	}
11859 }
11860 
11861 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
11862 {
11863 	struct bnxt_filter_base *usr_fltr, *tmp;
11864 
11865 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
11866 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
11867 }
11868 
11869 static int bnxt_set_xps_mapping(struct bnxt *bp)
11870 {
11871 	int numa_node = dev_to_node(&bp->pdev->dev);
11872 	unsigned int q_idx, map_idx, cpu, i;
11873 	const struct cpumask *cpu_mask_ptr;
11874 	int nr_cpus = num_online_cpus();
11875 	cpumask_t *q_map;
11876 	int rc = 0;
11877 
11878 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
11879 	if (!q_map)
11880 		return -ENOMEM;
11881 
11882 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
11883 	 * Each TC has the same number of TX queues. The nth TX queue for each
11884 	 * TC will have the same CPU mask.
11885 	 */
11886 	for (i = 0; i < nr_cpus; i++) {
11887 		map_idx = i % bp->tx_nr_rings_per_tc;
11888 		cpu = cpumask_local_spread(i, numa_node);
11889 		cpu_mask_ptr = get_cpu_mask(cpu);
11890 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
11891 	}
11892 
11893 	/* Register CPU mask for each TX queue except the ones marked for XDP */
11894 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
11895 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
11896 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
11897 		if (rc) {
11898 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
11899 				    q_idx);
11900 			break;
11901 		}
11902 	}
11903 
11904 	kfree(q_map);
11905 
11906 	return rc;
11907 }
11908 
11909 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
11910 {
11911 	int rc = 0;
11912 
11913 	bnxt_preset_reg_win(bp);
11914 	netif_carrier_off(bp->dev);
11915 	if (irq_re_init) {
11916 		/* Reserve rings now if none were reserved at driver probe. */
11917 		rc = bnxt_init_dflt_ring_mode(bp);
11918 		if (rc) {
11919 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
11920 			return rc;
11921 		}
11922 	}
11923 	rc = bnxt_reserve_rings(bp, irq_re_init);
11924 	if (rc)
11925 		return rc;
11926 	if ((bp->flags & BNXT_FLAG_RFS) &&
11927 	    !(bp->flags & BNXT_FLAG_USING_MSIX)) {
11928 		/* disable RFS if falling back to INTA */
11929 		bp->dev->hw_features &= ~NETIF_F_NTUPLE;
11930 		bp->flags &= ~BNXT_FLAG_RFS;
11931 	}
11932 
11933 	rc = bnxt_alloc_mem(bp, irq_re_init);
11934 	if (rc) {
11935 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
11936 		goto open_err_free_mem;
11937 	}
11938 
11939 	if (irq_re_init) {
11940 		bnxt_init_napi(bp);
11941 		rc = bnxt_request_irq(bp);
11942 		if (rc) {
11943 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
11944 			goto open_err_irq;
11945 		}
11946 	}
11947 
11948 	rc = bnxt_init_nic(bp, irq_re_init);
11949 	if (rc) {
11950 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
11951 		goto open_err_irq;
11952 	}
11953 
11954 	bnxt_enable_napi(bp);
11955 	bnxt_debug_dev_init(bp);
11956 
11957 	if (link_re_init) {
11958 		mutex_lock(&bp->link_lock);
11959 		rc = bnxt_update_phy_setting(bp);
11960 		mutex_unlock(&bp->link_lock);
11961 		if (rc) {
11962 			netdev_warn(bp->dev, "failed to update phy settings\n");
11963 			if (BNXT_SINGLE_PF(bp)) {
11964 				bp->link_info.phy_retry = true;
11965 				bp->link_info.phy_retry_expires =
11966 					jiffies + 5 * HZ;
11967 			}
11968 		}
11969 	}
11970 
11971 	if (irq_re_init) {
11972 		udp_tunnel_nic_reset_ntf(bp->dev);
11973 		rc = bnxt_set_xps_mapping(bp);
11974 		if (rc)
11975 			netdev_warn(bp->dev, "failed to set xps mapping\n");
11976 	}
11977 
11978 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
11979 		if (!static_key_enabled(&bnxt_xdp_locking_key))
11980 			static_branch_enable(&bnxt_xdp_locking_key);
11981 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
11982 		static_branch_disable(&bnxt_xdp_locking_key);
11983 	}
11984 	set_bit(BNXT_STATE_OPEN, &bp->state);
11985 	bnxt_enable_int(bp);
11986 	/* Enable TX queues */
11987 	bnxt_tx_enable(bp);
11988 	mod_timer(&bp->timer, jiffies + bp->current_interval);
11989 	/* Poll link status and check for SFP+ module status */
11990 	mutex_lock(&bp->link_lock);
11991 	bnxt_get_port_module_status(bp);
11992 	mutex_unlock(&bp->link_lock);
11993 
11994 	/* VF-reps may need to be re-opened after the PF is re-opened */
11995 	if (BNXT_PF(bp))
11996 		bnxt_vf_reps_open(bp);
11997 	if (bp->ptp_cfg)
11998 		atomic_set(&bp->ptp_cfg->tx_avail, BNXT_MAX_TX_TS);
11999 	bnxt_ptp_init_rtc(bp, true);
12000 	bnxt_ptp_cfg_tstamp_filters(bp);
12001 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12002 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
12003 	bnxt_cfg_usr_fltrs(bp);
12004 	return 0;
12005 
12006 open_err_irq:
12007 	bnxt_del_napi(bp);
12008 
12009 open_err_free_mem:
12010 	bnxt_free_skbs(bp);
12011 	bnxt_free_irq(bp);
12012 	bnxt_free_mem(bp, true);
12013 	return rc;
12014 }
12015 
12016 /* rtnl_lock held */
12017 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12018 {
12019 	int rc = 0;
12020 
12021 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12022 		rc = -EIO;
12023 	if (!rc)
12024 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12025 	if (rc) {
12026 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12027 		dev_close(bp->dev);
12028 	}
12029 	return rc;
12030 }
12031 
12032 /* rtnl_lock held, open the NIC half way by allocating all resources, but
12033  * NAPI, IRQ, and TX are not enabled.  This is mainly used for offline
12034  * self tests.
12035  */
12036 int bnxt_half_open_nic(struct bnxt *bp)
12037 {
12038 	int rc = 0;
12039 
12040 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12041 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
12042 		rc = -ENODEV;
12043 		goto half_open_err;
12044 	}
12045 
12046 	rc = bnxt_alloc_mem(bp, true);
12047 	if (rc) {
12048 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12049 		goto half_open_err;
12050 	}
12051 	bnxt_init_napi(bp);
12052 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12053 	rc = bnxt_init_nic(bp, true);
12054 	if (rc) {
12055 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12056 		bnxt_del_napi(bp);
12057 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12058 		goto half_open_err;
12059 	}
12060 	return 0;
12061 
12062 half_open_err:
12063 	bnxt_free_skbs(bp);
12064 	bnxt_free_mem(bp, true);
12065 	dev_close(bp->dev);
12066 	return rc;
12067 }
12068 
12069 /* rtnl_lock held, this call can only be made after a previous successful
12070  * call to bnxt_half_open_nic().
12071  */
12072 void bnxt_half_close_nic(struct bnxt *bp)
12073 {
12074 	bnxt_hwrm_resource_free(bp, false, true);
12075 	bnxt_del_napi(bp);
12076 	bnxt_free_skbs(bp);
12077 	bnxt_free_mem(bp, true);
12078 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12079 }
12080 
12081 void bnxt_reenable_sriov(struct bnxt *bp)
12082 {
12083 	if (BNXT_PF(bp)) {
12084 		struct bnxt_pf_info *pf = &bp->pf;
12085 		int n = pf->active_vfs;
12086 
12087 		if (n)
12088 			bnxt_cfg_hw_sriov(bp, &n, true);
12089 	}
12090 }
12091 
12092 static int bnxt_open(struct net_device *dev)
12093 {
12094 	struct bnxt *bp = netdev_priv(dev);
12095 	int rc;
12096 
12097 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12098 		rc = bnxt_reinit_after_abort(bp);
12099 		if (rc) {
12100 			if (rc == -EBUSY)
12101 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
12102 			else
12103 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
12104 			return -ENODEV;
12105 		}
12106 	}
12107 
12108 	rc = bnxt_hwrm_if_change(bp, true);
12109 	if (rc)
12110 		return rc;
12111 
12112 	rc = __bnxt_open_nic(bp, true, true);
12113 	if (rc) {
12114 		bnxt_hwrm_if_change(bp, false);
12115 	} else {
12116 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
12117 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12118 				bnxt_queue_sp_work(bp,
12119 						   BNXT_RESTART_ULP_SP_EVENT);
12120 		}
12121 	}
12122 
12123 	return rc;
12124 }
12125 
12126 static bool bnxt_drv_busy(struct bnxt *bp)
12127 {
12128 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
12129 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
12130 }
12131 
12132 static void bnxt_get_ring_stats(struct bnxt *bp,
12133 				struct rtnl_link_stats64 *stats);
12134 
12135 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
12136 			     bool link_re_init)
12137 {
12138 	/* Close the VF-reps before closing PF */
12139 	if (BNXT_PF(bp))
12140 		bnxt_vf_reps_close(bp);
12141 
12142 	/* Change device state to avoid TX queue wake up's */
12143 	bnxt_tx_disable(bp);
12144 
12145 	clear_bit(BNXT_STATE_OPEN, &bp->state);
12146 	smp_mb__after_atomic();
12147 	while (bnxt_drv_busy(bp))
12148 		msleep(20);
12149 
12150 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12151 		bnxt_clear_rss_ctxs(bp, false);
12152 	/* Flush rings and disable interrupts */
12153 	bnxt_shutdown_nic(bp, irq_re_init);
12154 
12155 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
12156 
12157 	bnxt_debug_dev_exit(bp);
12158 	bnxt_disable_napi(bp);
12159 	del_timer_sync(&bp->timer);
12160 	bnxt_free_skbs(bp);
12161 
12162 	/* Save ring stats before shutdown */
12163 	if (bp->bnapi && irq_re_init) {
12164 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
12165 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
12166 	}
12167 	if (irq_re_init) {
12168 		bnxt_free_irq(bp);
12169 		bnxt_del_napi(bp);
12170 	}
12171 	bnxt_free_mem(bp, irq_re_init);
12172 }
12173 
12174 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12175 {
12176 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12177 		/* If we get here, it means firmware reset is in progress
12178 		 * while we are trying to close.  We can safely proceed with
12179 		 * the close because we are holding rtnl_lock().  Some firmware
12180 		 * messages may fail as we proceed to close.  We set the
12181 		 * ABORT_ERR flag here so that the FW reset thread will later
12182 		 * abort when it gets the rtnl_lock() and sees the flag.
12183 		 */
12184 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
12185 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12186 	}
12187 
12188 #ifdef CONFIG_BNXT_SRIOV
12189 	if (bp->sriov_cfg) {
12190 		int rc;
12191 
12192 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
12193 						      !bp->sriov_cfg,
12194 						      BNXT_SRIOV_CFG_WAIT_TMO);
12195 		if (!rc)
12196 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
12197 		else if (rc < 0)
12198 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
12199 	}
12200 #endif
12201 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
12202 }
12203 
12204 static int bnxt_close(struct net_device *dev)
12205 {
12206 	struct bnxt *bp = netdev_priv(dev);
12207 
12208 	bnxt_close_nic(bp, true, true);
12209 	bnxt_hwrm_shutdown_link(bp);
12210 	bnxt_hwrm_if_change(bp, false);
12211 	return 0;
12212 }
12213 
12214 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
12215 				   u16 *val)
12216 {
12217 	struct hwrm_port_phy_mdio_read_output *resp;
12218 	struct hwrm_port_phy_mdio_read_input *req;
12219 	int rc;
12220 
12221 	if (bp->hwrm_spec_code < 0x10a00)
12222 		return -EOPNOTSUPP;
12223 
12224 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
12225 	if (rc)
12226 		return rc;
12227 
12228 	req->port_id = cpu_to_le16(bp->pf.port_id);
12229 	req->phy_addr = phy_addr;
12230 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12231 	if (mdio_phy_id_is_c45(phy_addr)) {
12232 		req->cl45_mdio = 1;
12233 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12234 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12235 		req->reg_addr = cpu_to_le16(reg);
12236 	}
12237 
12238 	resp = hwrm_req_hold(bp, req);
12239 	rc = hwrm_req_send(bp, req);
12240 	if (!rc)
12241 		*val = le16_to_cpu(resp->reg_data);
12242 	hwrm_req_drop(bp, req);
12243 	return rc;
12244 }
12245 
12246 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
12247 				    u16 val)
12248 {
12249 	struct hwrm_port_phy_mdio_write_input *req;
12250 	int rc;
12251 
12252 	if (bp->hwrm_spec_code < 0x10a00)
12253 		return -EOPNOTSUPP;
12254 
12255 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
12256 	if (rc)
12257 		return rc;
12258 
12259 	req->port_id = cpu_to_le16(bp->pf.port_id);
12260 	req->phy_addr = phy_addr;
12261 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12262 	if (mdio_phy_id_is_c45(phy_addr)) {
12263 		req->cl45_mdio = 1;
12264 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12265 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12266 		req->reg_addr = cpu_to_le16(reg);
12267 	}
12268 	req->reg_data = cpu_to_le16(val);
12269 
12270 	return hwrm_req_send(bp, req);
12271 }
12272 
12273 /* rtnl_lock held */
12274 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12275 {
12276 	struct mii_ioctl_data *mdio = if_mii(ifr);
12277 	struct bnxt *bp = netdev_priv(dev);
12278 	int rc;
12279 
12280 	switch (cmd) {
12281 	case SIOCGMIIPHY:
12282 		mdio->phy_id = bp->link_info.phy_addr;
12283 
12284 		fallthrough;
12285 	case SIOCGMIIREG: {
12286 		u16 mii_regval = 0;
12287 
12288 		if (!netif_running(dev))
12289 			return -EAGAIN;
12290 
12291 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
12292 					     &mii_regval);
12293 		mdio->val_out = mii_regval;
12294 		return rc;
12295 	}
12296 
12297 	case SIOCSMIIREG:
12298 		if (!netif_running(dev))
12299 			return -EAGAIN;
12300 
12301 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
12302 						mdio->val_in);
12303 
12304 	case SIOCSHWTSTAMP:
12305 		return bnxt_hwtstamp_set(dev, ifr);
12306 
12307 	case SIOCGHWTSTAMP:
12308 		return bnxt_hwtstamp_get(dev, ifr);
12309 
12310 	default:
12311 		/* do nothing */
12312 		break;
12313 	}
12314 	return -EOPNOTSUPP;
12315 }
12316 
12317 static void bnxt_get_ring_stats(struct bnxt *bp,
12318 				struct rtnl_link_stats64 *stats)
12319 {
12320 	int i;
12321 
12322 	for (i = 0; i < bp->cp_nr_rings; i++) {
12323 		struct bnxt_napi *bnapi = bp->bnapi[i];
12324 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
12325 		u64 *sw = cpr->stats.sw_stats;
12326 
12327 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
12328 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12329 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
12330 
12331 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
12332 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
12333 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
12334 
12335 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
12336 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
12337 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
12338 
12339 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
12340 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
12341 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
12342 
12343 		stats->rx_missed_errors +=
12344 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
12345 
12346 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12347 
12348 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
12349 
12350 		stats->rx_dropped +=
12351 			cpr->sw_stats->rx.rx_netpoll_discards +
12352 			cpr->sw_stats->rx.rx_oom_discards;
12353 	}
12354 }
12355 
12356 static void bnxt_add_prev_stats(struct bnxt *bp,
12357 				struct rtnl_link_stats64 *stats)
12358 {
12359 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
12360 
12361 	stats->rx_packets += prev_stats->rx_packets;
12362 	stats->tx_packets += prev_stats->tx_packets;
12363 	stats->rx_bytes += prev_stats->rx_bytes;
12364 	stats->tx_bytes += prev_stats->tx_bytes;
12365 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
12366 	stats->multicast += prev_stats->multicast;
12367 	stats->rx_dropped += prev_stats->rx_dropped;
12368 	stats->tx_dropped += prev_stats->tx_dropped;
12369 }
12370 
12371 static void
12372 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
12373 {
12374 	struct bnxt *bp = netdev_priv(dev);
12375 
12376 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
12377 	/* Make sure bnxt_close_nic() sees that we are reading stats before
12378 	 * we check the BNXT_STATE_OPEN flag.
12379 	 */
12380 	smp_mb__after_atomic();
12381 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12382 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12383 		*stats = bp->net_stats_prev;
12384 		return;
12385 	}
12386 
12387 	bnxt_get_ring_stats(bp, stats);
12388 	bnxt_add_prev_stats(bp, stats);
12389 
12390 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
12391 		u64 *rx = bp->port_stats.sw_stats;
12392 		u64 *tx = bp->port_stats.sw_stats +
12393 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
12394 
12395 		stats->rx_crc_errors =
12396 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
12397 		stats->rx_frame_errors =
12398 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
12399 		stats->rx_length_errors =
12400 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
12401 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
12402 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
12403 		stats->rx_errors =
12404 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
12405 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
12406 		stats->collisions =
12407 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
12408 		stats->tx_fifo_errors =
12409 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
12410 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
12411 	}
12412 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12413 }
12414 
12415 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
12416 					struct bnxt_total_ring_err_stats *stats,
12417 					struct bnxt_cp_ring_info *cpr)
12418 {
12419 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
12420 	u64 *hw_stats = cpr->stats.sw_stats;
12421 
12422 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
12423 	stats->rx_total_resets += sw_stats->rx.rx_resets;
12424 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
12425 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
12426 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
12427 	stats->rx_total_ring_discards +=
12428 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
12429 	stats->tx_total_resets += sw_stats->tx.tx_resets;
12430 	stats->tx_total_ring_discards +=
12431 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
12432 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
12433 }
12434 
12435 void bnxt_get_ring_err_stats(struct bnxt *bp,
12436 			     struct bnxt_total_ring_err_stats *stats)
12437 {
12438 	int i;
12439 
12440 	for (i = 0; i < bp->cp_nr_rings; i++)
12441 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
12442 }
12443 
12444 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
12445 {
12446 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12447 	struct net_device *dev = bp->dev;
12448 	struct netdev_hw_addr *ha;
12449 	u8 *haddr;
12450 	int mc_count = 0;
12451 	bool update = false;
12452 	int off = 0;
12453 
12454 	netdev_for_each_mc_addr(ha, dev) {
12455 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
12456 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12457 			vnic->mc_list_count = 0;
12458 			return false;
12459 		}
12460 		haddr = ha->addr;
12461 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
12462 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
12463 			update = true;
12464 		}
12465 		off += ETH_ALEN;
12466 		mc_count++;
12467 	}
12468 	if (mc_count)
12469 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
12470 
12471 	if (mc_count != vnic->mc_list_count) {
12472 		vnic->mc_list_count = mc_count;
12473 		update = true;
12474 	}
12475 	return update;
12476 }
12477 
12478 static bool bnxt_uc_list_updated(struct bnxt *bp)
12479 {
12480 	struct net_device *dev = bp->dev;
12481 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12482 	struct netdev_hw_addr *ha;
12483 	int off = 0;
12484 
12485 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
12486 		return true;
12487 
12488 	netdev_for_each_uc_addr(ha, dev) {
12489 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
12490 			return true;
12491 
12492 		off += ETH_ALEN;
12493 	}
12494 	return false;
12495 }
12496 
12497 static void bnxt_set_rx_mode(struct net_device *dev)
12498 {
12499 	struct bnxt *bp = netdev_priv(dev);
12500 	struct bnxt_vnic_info *vnic;
12501 	bool mc_update = false;
12502 	bool uc_update;
12503 	u32 mask;
12504 
12505 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
12506 		return;
12507 
12508 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12509 	mask = vnic->rx_mask;
12510 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
12511 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
12512 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
12513 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
12514 
12515 	if (dev->flags & IFF_PROMISC)
12516 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12517 
12518 	uc_update = bnxt_uc_list_updated(bp);
12519 
12520 	if (dev->flags & IFF_BROADCAST)
12521 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
12522 	if (dev->flags & IFF_ALLMULTI) {
12523 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12524 		vnic->mc_list_count = 0;
12525 	} else if (dev->flags & IFF_MULTICAST) {
12526 		mc_update = bnxt_mc_list_updated(bp, &mask);
12527 	}
12528 
12529 	if (mask != vnic->rx_mask || uc_update || mc_update) {
12530 		vnic->rx_mask = mask;
12531 
12532 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
12533 	}
12534 }
12535 
12536 static int bnxt_cfg_rx_mode(struct bnxt *bp)
12537 {
12538 	struct net_device *dev = bp->dev;
12539 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12540 	struct netdev_hw_addr *ha;
12541 	int i, off = 0, rc;
12542 	bool uc_update;
12543 
12544 	netif_addr_lock_bh(dev);
12545 	uc_update = bnxt_uc_list_updated(bp);
12546 	netif_addr_unlock_bh(dev);
12547 
12548 	if (!uc_update)
12549 		goto skip_uc;
12550 
12551 	for (i = 1; i < vnic->uc_filter_count; i++) {
12552 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
12553 
12554 		bnxt_hwrm_l2_filter_free(bp, fltr);
12555 		bnxt_del_l2_filter(bp, fltr);
12556 	}
12557 
12558 	vnic->uc_filter_count = 1;
12559 
12560 	netif_addr_lock_bh(dev);
12561 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
12562 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12563 	} else {
12564 		netdev_for_each_uc_addr(ha, dev) {
12565 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
12566 			off += ETH_ALEN;
12567 			vnic->uc_filter_count++;
12568 		}
12569 	}
12570 	netif_addr_unlock_bh(dev);
12571 
12572 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
12573 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
12574 		if (rc) {
12575 			if (BNXT_VF(bp) && rc == -ENODEV) {
12576 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
12577 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
12578 				else
12579 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
12580 				rc = 0;
12581 			} else {
12582 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
12583 			}
12584 			vnic->uc_filter_count = i;
12585 			return rc;
12586 		}
12587 	}
12588 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
12589 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
12590 
12591 skip_uc:
12592 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
12593 	    !bnxt_promisc_ok(bp))
12594 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12595 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
12596 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
12597 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
12598 			    rc);
12599 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
12600 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12601 		vnic->mc_list_count = 0;
12602 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
12603 	}
12604 	if (rc)
12605 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
12606 			   rc);
12607 
12608 	return rc;
12609 }
12610 
12611 static bool bnxt_can_reserve_rings(struct bnxt *bp)
12612 {
12613 #ifdef CONFIG_BNXT_SRIOV
12614 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
12615 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12616 
12617 		/* No minimum rings were provisioned by the PF.  Don't
12618 		 * reserve rings by default when device is down.
12619 		 */
12620 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
12621 			return true;
12622 
12623 		if (!netif_running(bp->dev))
12624 			return false;
12625 	}
12626 #endif
12627 	return true;
12628 }
12629 
12630 /* If the chip and firmware supports RFS */
12631 static bool bnxt_rfs_supported(struct bnxt *bp)
12632 {
12633 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
12634 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
12635 			return true;
12636 		return false;
12637 	}
12638 	/* 212 firmware is broken for aRFS */
12639 	if (BNXT_FW_MAJ(bp) == 212)
12640 		return false;
12641 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
12642 		return true;
12643 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
12644 		return true;
12645 	return false;
12646 }
12647 
12648 /* If runtime conditions support RFS */
12649 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
12650 {
12651 	struct bnxt_hw_rings hwr = {0};
12652 	int max_vnics, max_rss_ctxs;
12653 
12654 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
12655 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
12656 		return bnxt_rfs_supported(bp);
12657 
12658 	if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
12659 		return false;
12660 
12661 	hwr.grp = bp->rx_nr_rings;
12662 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
12663 	if (new_rss_ctx)
12664 		hwr.vnic++;
12665 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
12666 	max_vnics = bnxt_get_max_func_vnics(bp);
12667 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
12668 
12669 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
12670 		if (bp->rx_nr_rings > 1)
12671 			netdev_warn(bp->dev,
12672 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
12673 				    min(max_rss_ctxs - 1, max_vnics - 1));
12674 		return false;
12675 	}
12676 
12677 	if (!BNXT_NEW_RM(bp))
12678 		return true;
12679 
12680 	if (hwr.vnic == bp->hw_resc.resv_vnics &&
12681 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
12682 		return true;
12683 
12684 	bnxt_hwrm_reserve_rings(bp, &hwr);
12685 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
12686 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
12687 		return true;
12688 
12689 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
12690 	hwr.vnic = 1;
12691 	hwr.rss_ctx = 0;
12692 	bnxt_hwrm_reserve_rings(bp, &hwr);
12693 	return false;
12694 }
12695 
12696 static netdev_features_t bnxt_fix_features(struct net_device *dev,
12697 					   netdev_features_t features)
12698 {
12699 	struct bnxt *bp = netdev_priv(dev);
12700 	netdev_features_t vlan_features;
12701 
12702 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
12703 		features &= ~NETIF_F_NTUPLE;
12704 
12705 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
12706 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
12707 
12708 	if (!(features & NETIF_F_GRO))
12709 		features &= ~NETIF_F_GRO_HW;
12710 
12711 	if (features & NETIF_F_GRO_HW)
12712 		features &= ~NETIF_F_LRO;
12713 
12714 	/* Both CTAG and STAG VLAN accelaration on the RX side have to be
12715 	 * turned on or off together.
12716 	 */
12717 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
12718 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
12719 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
12720 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
12721 		else if (vlan_features)
12722 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
12723 	}
12724 #ifdef CONFIG_BNXT_SRIOV
12725 	if (BNXT_VF(bp) && bp->vf.vlan)
12726 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
12727 #endif
12728 	return features;
12729 }
12730 
12731 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
12732 				bool link_re_init, u32 flags, bool update_tpa)
12733 {
12734 	bnxt_close_nic(bp, irq_re_init, link_re_init);
12735 	bp->flags = flags;
12736 	if (update_tpa)
12737 		bnxt_set_ring_params(bp);
12738 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
12739 }
12740 
12741 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
12742 {
12743 	bool update_tpa = false, update_ntuple = false;
12744 	struct bnxt *bp = netdev_priv(dev);
12745 	u32 flags = bp->flags;
12746 	u32 changes;
12747 	int rc = 0;
12748 	bool re_init = false;
12749 
12750 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
12751 	if (features & NETIF_F_GRO_HW)
12752 		flags |= BNXT_FLAG_GRO;
12753 	else if (features & NETIF_F_LRO)
12754 		flags |= BNXT_FLAG_LRO;
12755 
12756 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
12757 		flags &= ~BNXT_FLAG_TPA;
12758 
12759 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
12760 		flags |= BNXT_FLAG_STRIP_VLAN;
12761 
12762 	if (features & NETIF_F_NTUPLE)
12763 		flags |= BNXT_FLAG_RFS;
12764 	else
12765 		bnxt_clear_usr_fltrs(bp, true);
12766 
12767 	changes = flags ^ bp->flags;
12768 	if (changes & BNXT_FLAG_TPA) {
12769 		update_tpa = true;
12770 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
12771 		    (flags & BNXT_FLAG_TPA) == 0 ||
12772 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
12773 			re_init = true;
12774 	}
12775 
12776 	if (changes & ~BNXT_FLAG_TPA)
12777 		re_init = true;
12778 
12779 	if (changes & BNXT_FLAG_RFS)
12780 		update_ntuple = true;
12781 
12782 	if (flags != bp->flags) {
12783 		u32 old_flags = bp->flags;
12784 
12785 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12786 			bp->flags = flags;
12787 			if (update_tpa)
12788 				bnxt_set_ring_params(bp);
12789 			return rc;
12790 		}
12791 
12792 		if (update_ntuple)
12793 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
12794 
12795 		if (re_init)
12796 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
12797 
12798 		if (update_tpa) {
12799 			bp->flags = flags;
12800 			rc = bnxt_set_tpa(bp,
12801 					  (flags & BNXT_FLAG_TPA) ?
12802 					  true : false);
12803 			if (rc)
12804 				bp->flags = old_flags;
12805 		}
12806 	}
12807 	return rc;
12808 }
12809 
12810 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
12811 			      u8 **nextp)
12812 {
12813 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
12814 	struct hop_jumbo_hdr *jhdr;
12815 	int hdr_count = 0;
12816 	u8 *nexthdr;
12817 	int start;
12818 
12819 	/* Check that there are at most 2 IPv6 extension headers, no
12820 	 * fragment header, and each is <= 64 bytes.
12821 	 */
12822 	start = nw_off + sizeof(*ip6h);
12823 	nexthdr = &ip6h->nexthdr;
12824 	while (ipv6_ext_hdr(*nexthdr)) {
12825 		struct ipv6_opt_hdr *hp;
12826 		int hdrlen;
12827 
12828 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
12829 		    *nexthdr == NEXTHDR_FRAGMENT)
12830 			return false;
12831 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
12832 					  skb_headlen(skb), NULL);
12833 		if (!hp)
12834 			return false;
12835 		if (*nexthdr == NEXTHDR_AUTH)
12836 			hdrlen = ipv6_authlen(hp);
12837 		else
12838 			hdrlen = ipv6_optlen(hp);
12839 
12840 		if (hdrlen > 64)
12841 			return false;
12842 
12843 		/* The ext header may be a hop-by-hop header inserted for
12844 		 * big TCP purposes. This will be removed before sending
12845 		 * from NIC, so do not count it.
12846 		 */
12847 		if (*nexthdr == NEXTHDR_HOP) {
12848 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
12849 				goto increment_hdr;
12850 
12851 			jhdr = (struct hop_jumbo_hdr *)hp;
12852 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
12853 			    jhdr->nexthdr != IPPROTO_TCP)
12854 				goto increment_hdr;
12855 
12856 			goto next_hdr;
12857 		}
12858 increment_hdr:
12859 		hdr_count++;
12860 next_hdr:
12861 		nexthdr = &hp->nexthdr;
12862 		start += hdrlen;
12863 	}
12864 	if (nextp) {
12865 		/* Caller will check inner protocol */
12866 		if (skb->encapsulation) {
12867 			*nextp = nexthdr;
12868 			return true;
12869 		}
12870 		*nextp = NULL;
12871 	}
12872 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
12873 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
12874 }
12875 
12876 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
12877 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
12878 {
12879 	struct udphdr *uh = udp_hdr(skb);
12880 	__be16 udp_port = uh->dest;
12881 
12882 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
12883 	    udp_port != bp->vxlan_gpe_port)
12884 		return false;
12885 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
12886 		struct ethhdr *eh = inner_eth_hdr(skb);
12887 
12888 		switch (eh->h_proto) {
12889 		case htons(ETH_P_IP):
12890 			return true;
12891 		case htons(ETH_P_IPV6):
12892 			return bnxt_exthdr_check(bp, skb,
12893 						 skb_inner_network_offset(skb),
12894 						 NULL);
12895 		}
12896 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
12897 		return true;
12898 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
12899 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
12900 					 NULL);
12901 	}
12902 	return false;
12903 }
12904 
12905 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
12906 {
12907 	switch (l4_proto) {
12908 	case IPPROTO_UDP:
12909 		return bnxt_udp_tunl_check(bp, skb);
12910 	case IPPROTO_IPIP:
12911 		return true;
12912 	case IPPROTO_GRE: {
12913 		switch (skb->inner_protocol) {
12914 		default:
12915 			return false;
12916 		case htons(ETH_P_IP):
12917 			return true;
12918 		case htons(ETH_P_IPV6):
12919 			fallthrough;
12920 		}
12921 	}
12922 	case IPPROTO_IPV6:
12923 		/* Check ext headers of inner ipv6 */
12924 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
12925 					 NULL);
12926 	}
12927 	return false;
12928 }
12929 
12930 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
12931 					     struct net_device *dev,
12932 					     netdev_features_t features)
12933 {
12934 	struct bnxt *bp = netdev_priv(dev);
12935 	u8 *l4_proto;
12936 
12937 	features = vlan_features_check(skb, features);
12938 	switch (vlan_get_protocol(skb)) {
12939 	case htons(ETH_P_IP):
12940 		if (!skb->encapsulation)
12941 			return features;
12942 		l4_proto = &ip_hdr(skb)->protocol;
12943 		if (bnxt_tunl_check(bp, skb, *l4_proto))
12944 			return features;
12945 		break;
12946 	case htons(ETH_P_IPV6):
12947 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
12948 				       &l4_proto))
12949 			break;
12950 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
12951 			return features;
12952 		break;
12953 	}
12954 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
12955 }
12956 
12957 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
12958 			 u32 *reg_buf)
12959 {
12960 	struct hwrm_dbg_read_direct_output *resp;
12961 	struct hwrm_dbg_read_direct_input *req;
12962 	__le32 *dbg_reg_buf;
12963 	dma_addr_t mapping;
12964 	int rc, i;
12965 
12966 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
12967 	if (rc)
12968 		return rc;
12969 
12970 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
12971 					 &mapping);
12972 	if (!dbg_reg_buf) {
12973 		rc = -ENOMEM;
12974 		goto dbg_rd_reg_exit;
12975 	}
12976 
12977 	req->host_dest_addr = cpu_to_le64(mapping);
12978 
12979 	resp = hwrm_req_hold(bp, req);
12980 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
12981 	req->read_len32 = cpu_to_le32(num_words);
12982 
12983 	rc = hwrm_req_send(bp, req);
12984 	if (rc || resp->error_code) {
12985 		rc = -EIO;
12986 		goto dbg_rd_reg_exit;
12987 	}
12988 	for (i = 0; i < num_words; i++)
12989 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
12990 
12991 dbg_rd_reg_exit:
12992 	hwrm_req_drop(bp, req);
12993 	return rc;
12994 }
12995 
12996 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
12997 				       u32 ring_id, u32 *prod, u32 *cons)
12998 {
12999 	struct hwrm_dbg_ring_info_get_output *resp;
13000 	struct hwrm_dbg_ring_info_get_input *req;
13001 	int rc;
13002 
13003 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
13004 	if (rc)
13005 		return rc;
13006 
13007 	req->ring_type = ring_type;
13008 	req->fw_ring_id = cpu_to_le32(ring_id);
13009 	resp = hwrm_req_hold(bp, req);
13010 	rc = hwrm_req_send(bp, req);
13011 	if (!rc) {
13012 		*prod = le32_to_cpu(resp->producer_index);
13013 		*cons = le32_to_cpu(resp->consumer_index);
13014 	}
13015 	hwrm_req_drop(bp, req);
13016 	return rc;
13017 }
13018 
13019 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13020 {
13021 	struct bnxt_tx_ring_info *txr;
13022 	int i = bnapi->index, j;
13023 
13024 	bnxt_for_each_napi_tx(j, bnapi, txr)
13025 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13026 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13027 			    txr->tx_cons);
13028 }
13029 
13030 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
13031 {
13032 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
13033 	int i = bnapi->index;
13034 
13035 	if (!rxr)
13036 		return;
13037 
13038 	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",
13039 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
13040 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
13041 		    rxr->rx_sw_agg_prod);
13042 }
13043 
13044 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
13045 {
13046 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13047 	int i = bnapi->index;
13048 
13049 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
13050 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
13051 }
13052 
13053 static void bnxt_dbg_dump_states(struct bnxt *bp)
13054 {
13055 	int i;
13056 	struct bnxt_napi *bnapi;
13057 
13058 	for (i = 0; i < bp->cp_nr_rings; i++) {
13059 		bnapi = bp->bnapi[i];
13060 		if (netif_msg_drv(bp)) {
13061 			bnxt_dump_tx_sw_state(bnapi);
13062 			bnxt_dump_rx_sw_state(bnapi);
13063 			bnxt_dump_cp_sw_state(bnapi);
13064 		}
13065 	}
13066 }
13067 
13068 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
13069 {
13070 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
13071 	struct hwrm_ring_reset_input *req;
13072 	struct bnxt_napi *bnapi = rxr->bnapi;
13073 	struct bnxt_cp_ring_info *cpr;
13074 	u16 cp_ring_id;
13075 	int rc;
13076 
13077 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
13078 	if (rc)
13079 		return rc;
13080 
13081 	cpr = &bnapi->cp_ring;
13082 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
13083 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
13084 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
13085 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
13086 	return hwrm_req_send_silent(bp, req);
13087 }
13088 
13089 static void bnxt_reset_task(struct bnxt *bp, bool silent)
13090 {
13091 	if (!silent)
13092 		bnxt_dbg_dump_states(bp);
13093 	if (netif_running(bp->dev)) {
13094 		bnxt_close_nic(bp, !silent, false);
13095 		bnxt_open_nic(bp, !silent, false);
13096 	}
13097 }
13098 
13099 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
13100 {
13101 	struct bnxt *bp = netdev_priv(dev);
13102 
13103 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
13104 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
13105 }
13106 
13107 static void bnxt_fw_health_check(struct bnxt *bp)
13108 {
13109 	struct bnxt_fw_health *fw_health = bp->fw_health;
13110 	struct pci_dev *pdev = bp->pdev;
13111 	u32 val;
13112 
13113 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13114 		return;
13115 
13116 	/* Make sure it is enabled before checking the tmr_counter. */
13117 	smp_rmb();
13118 	if (fw_health->tmr_counter) {
13119 		fw_health->tmr_counter--;
13120 		return;
13121 	}
13122 
13123 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13124 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
13125 		fw_health->arrests++;
13126 		goto fw_reset;
13127 	}
13128 
13129 	fw_health->last_fw_heartbeat = val;
13130 
13131 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13132 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
13133 		fw_health->discoveries++;
13134 		goto fw_reset;
13135 	}
13136 
13137 	fw_health->tmr_counter = fw_health->tmr_multiplier;
13138 	return;
13139 
13140 fw_reset:
13141 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
13142 }
13143 
13144 static void bnxt_timer(struct timer_list *t)
13145 {
13146 	struct bnxt *bp = from_timer(bp, t, timer);
13147 	struct net_device *dev = bp->dev;
13148 
13149 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
13150 		return;
13151 
13152 	if (atomic_read(&bp->intr_sem) != 0)
13153 		goto bnxt_restart_timer;
13154 
13155 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
13156 		bnxt_fw_health_check(bp);
13157 
13158 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
13159 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
13160 
13161 	if (bnxt_tc_flower_enabled(bp))
13162 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
13163 
13164 #ifdef CONFIG_RFS_ACCEL
13165 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
13166 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
13167 #endif /*CONFIG_RFS_ACCEL*/
13168 
13169 	if (bp->link_info.phy_retry) {
13170 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
13171 			bp->link_info.phy_retry = false;
13172 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
13173 		} else {
13174 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
13175 		}
13176 	}
13177 
13178 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13179 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13180 
13181 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
13182 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
13183 
13184 bnxt_restart_timer:
13185 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13186 }
13187 
13188 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
13189 {
13190 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
13191 	 * set.  If the device is being closed, bnxt_close() may be holding
13192 	 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear.  So we
13193 	 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
13194 	 */
13195 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13196 	rtnl_lock();
13197 }
13198 
13199 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
13200 {
13201 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13202 	rtnl_unlock();
13203 }
13204 
13205 /* Only called from bnxt_sp_task() */
13206 static void bnxt_reset(struct bnxt *bp, bool silent)
13207 {
13208 	bnxt_rtnl_lock_sp(bp);
13209 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
13210 		bnxt_reset_task(bp, silent);
13211 	bnxt_rtnl_unlock_sp(bp);
13212 }
13213 
13214 /* Only called from bnxt_sp_task() */
13215 static void bnxt_rx_ring_reset(struct bnxt *bp)
13216 {
13217 	int i;
13218 
13219 	bnxt_rtnl_lock_sp(bp);
13220 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13221 		bnxt_rtnl_unlock_sp(bp);
13222 		return;
13223 	}
13224 	/* Disable and flush TPA before resetting the RX ring */
13225 	if (bp->flags & BNXT_FLAG_TPA)
13226 		bnxt_set_tpa(bp, false);
13227 	for (i = 0; i < bp->rx_nr_rings; i++) {
13228 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
13229 		struct bnxt_cp_ring_info *cpr;
13230 		int rc;
13231 
13232 		if (!rxr->bnapi->in_reset)
13233 			continue;
13234 
13235 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
13236 		if (rc) {
13237 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
13238 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
13239 			else
13240 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
13241 					    rc);
13242 			bnxt_reset_task(bp, true);
13243 			break;
13244 		}
13245 		bnxt_free_one_rx_ring_skbs(bp, i);
13246 		rxr->rx_prod = 0;
13247 		rxr->rx_agg_prod = 0;
13248 		rxr->rx_sw_agg_prod = 0;
13249 		rxr->rx_next_cons = 0;
13250 		rxr->bnapi->in_reset = false;
13251 		bnxt_alloc_one_rx_ring(bp, i);
13252 		cpr = &rxr->bnapi->cp_ring;
13253 		cpr->sw_stats->rx.rx_resets++;
13254 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
13255 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
13256 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
13257 	}
13258 	if (bp->flags & BNXT_FLAG_TPA)
13259 		bnxt_set_tpa(bp, true);
13260 	bnxt_rtnl_unlock_sp(bp);
13261 }
13262 
13263 static void bnxt_fw_fatal_close(struct bnxt *bp)
13264 {
13265 	bnxt_tx_disable(bp);
13266 	bnxt_disable_napi(bp);
13267 	bnxt_disable_int_sync(bp);
13268 	bnxt_free_irq(bp);
13269 	bnxt_clear_int_mode(bp);
13270 	pci_disable_device(bp->pdev);
13271 }
13272 
13273 static void bnxt_fw_reset_close(struct bnxt *bp)
13274 {
13275 	/* When firmware is in fatal state, quiesce device and disable
13276 	 * bus master to prevent any potential bad DMAs before freeing
13277 	 * kernel memory.
13278 	 */
13279 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
13280 		u16 val = 0;
13281 
13282 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
13283 		if (val == 0xffff)
13284 			bp->fw_reset_min_dsecs = 0;
13285 		bnxt_fw_fatal_close(bp);
13286 	}
13287 	__bnxt_close_nic(bp, true, false);
13288 	bnxt_vf_reps_free(bp);
13289 	bnxt_clear_int_mode(bp);
13290 	bnxt_hwrm_func_drv_unrgtr(bp);
13291 	if (pci_is_enabled(bp->pdev))
13292 		pci_disable_device(bp->pdev);
13293 	bnxt_free_ctx_mem(bp);
13294 }
13295 
13296 static bool is_bnxt_fw_ok(struct bnxt *bp)
13297 {
13298 	struct bnxt_fw_health *fw_health = bp->fw_health;
13299 	bool no_heartbeat = false, has_reset = false;
13300 	u32 val;
13301 
13302 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13303 	if (val == fw_health->last_fw_heartbeat)
13304 		no_heartbeat = true;
13305 
13306 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13307 	if (val != fw_health->last_fw_reset_cnt)
13308 		has_reset = true;
13309 
13310 	if (!no_heartbeat && has_reset)
13311 		return true;
13312 
13313 	return false;
13314 }
13315 
13316 /* rtnl_lock is acquired before calling this function */
13317 static void bnxt_force_fw_reset(struct bnxt *bp)
13318 {
13319 	struct bnxt_fw_health *fw_health = bp->fw_health;
13320 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13321 	u32 wait_dsecs;
13322 
13323 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
13324 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13325 		return;
13326 
13327 	if (ptp) {
13328 		spin_lock_bh(&ptp->ptp_lock);
13329 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13330 		spin_unlock_bh(&ptp->ptp_lock);
13331 	} else {
13332 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13333 	}
13334 	bnxt_fw_reset_close(bp);
13335 	wait_dsecs = fw_health->master_func_wait_dsecs;
13336 	if (fw_health->primary) {
13337 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
13338 			wait_dsecs = 0;
13339 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
13340 	} else {
13341 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
13342 		wait_dsecs = fw_health->normal_func_wait_dsecs;
13343 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13344 	}
13345 
13346 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
13347 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
13348 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
13349 }
13350 
13351 void bnxt_fw_exception(struct bnxt *bp)
13352 {
13353 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
13354 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
13355 	bnxt_ulp_stop(bp);
13356 	bnxt_rtnl_lock_sp(bp);
13357 	bnxt_force_fw_reset(bp);
13358 	bnxt_rtnl_unlock_sp(bp);
13359 }
13360 
13361 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
13362  * < 0 on error.
13363  */
13364 static int bnxt_get_registered_vfs(struct bnxt *bp)
13365 {
13366 #ifdef CONFIG_BNXT_SRIOV
13367 	int rc;
13368 
13369 	if (!BNXT_PF(bp))
13370 		return 0;
13371 
13372 	rc = bnxt_hwrm_func_qcfg(bp);
13373 	if (rc) {
13374 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
13375 		return rc;
13376 	}
13377 	if (bp->pf.registered_vfs)
13378 		return bp->pf.registered_vfs;
13379 	if (bp->sriov_cfg)
13380 		return 1;
13381 #endif
13382 	return 0;
13383 }
13384 
13385 void bnxt_fw_reset(struct bnxt *bp)
13386 {
13387 	bnxt_ulp_stop(bp);
13388 	bnxt_rtnl_lock_sp(bp);
13389 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
13390 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13391 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13392 		int n = 0, tmo;
13393 
13394 		if (ptp) {
13395 			spin_lock_bh(&ptp->ptp_lock);
13396 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13397 			spin_unlock_bh(&ptp->ptp_lock);
13398 		} else {
13399 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13400 		}
13401 		if (bp->pf.active_vfs &&
13402 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
13403 			n = bnxt_get_registered_vfs(bp);
13404 		if (n < 0) {
13405 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
13406 				   n);
13407 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13408 			dev_close(bp->dev);
13409 			goto fw_reset_exit;
13410 		} else if (n > 0) {
13411 			u16 vf_tmo_dsecs = n * 10;
13412 
13413 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
13414 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
13415 			bp->fw_reset_state =
13416 				BNXT_FW_RESET_STATE_POLL_VF;
13417 			bnxt_queue_fw_reset_work(bp, HZ / 10);
13418 			goto fw_reset_exit;
13419 		}
13420 		bnxt_fw_reset_close(bp);
13421 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
13422 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
13423 			tmo = HZ / 10;
13424 		} else {
13425 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13426 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
13427 		}
13428 		bnxt_queue_fw_reset_work(bp, tmo);
13429 	}
13430 fw_reset_exit:
13431 	bnxt_rtnl_unlock_sp(bp);
13432 }
13433 
13434 static void bnxt_chk_missed_irq(struct bnxt *bp)
13435 {
13436 	int i;
13437 
13438 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13439 		return;
13440 
13441 	for (i = 0; i < bp->cp_nr_rings; i++) {
13442 		struct bnxt_napi *bnapi = bp->bnapi[i];
13443 		struct bnxt_cp_ring_info *cpr;
13444 		u32 fw_ring_id;
13445 		int j;
13446 
13447 		if (!bnapi)
13448 			continue;
13449 
13450 		cpr = &bnapi->cp_ring;
13451 		for (j = 0; j < cpr->cp_ring_count; j++) {
13452 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
13453 			u32 val[2];
13454 
13455 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
13456 				continue;
13457 
13458 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
13459 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
13460 				continue;
13461 			}
13462 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
13463 			bnxt_dbg_hwrm_ring_info_get(bp,
13464 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
13465 				fw_ring_id, &val[0], &val[1]);
13466 			cpr->sw_stats->cmn.missed_irqs++;
13467 		}
13468 	}
13469 }
13470 
13471 static void bnxt_cfg_ntp_filters(struct bnxt *);
13472 
13473 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
13474 {
13475 	struct bnxt_link_info *link_info = &bp->link_info;
13476 
13477 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
13478 		link_info->autoneg = BNXT_AUTONEG_SPEED;
13479 		if (bp->hwrm_spec_code >= 0x10201) {
13480 			if (link_info->auto_pause_setting &
13481 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
13482 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13483 		} else {
13484 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13485 		}
13486 		bnxt_set_auto_speed(link_info);
13487 	} else {
13488 		bnxt_set_force_speed(link_info);
13489 		link_info->req_duplex = link_info->duplex_setting;
13490 	}
13491 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
13492 		link_info->req_flow_ctrl =
13493 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
13494 	else
13495 		link_info->req_flow_ctrl = link_info->force_pause_setting;
13496 }
13497 
13498 static void bnxt_fw_echo_reply(struct bnxt *bp)
13499 {
13500 	struct bnxt_fw_health *fw_health = bp->fw_health;
13501 	struct hwrm_func_echo_response_input *req;
13502 	int rc;
13503 
13504 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
13505 	if (rc)
13506 		return;
13507 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
13508 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
13509 	hwrm_req_send(bp, req);
13510 }
13511 
13512 static void bnxt_ulp_restart(struct bnxt *bp)
13513 {
13514 	bnxt_ulp_stop(bp);
13515 	bnxt_ulp_start(bp, 0);
13516 }
13517 
13518 static void bnxt_sp_task(struct work_struct *work)
13519 {
13520 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
13521 
13522 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13523 	smp_mb__after_atomic();
13524 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13525 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13526 		return;
13527 	}
13528 
13529 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
13530 		bnxt_ulp_restart(bp);
13531 		bnxt_reenable_sriov(bp);
13532 	}
13533 
13534 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
13535 		bnxt_cfg_rx_mode(bp);
13536 
13537 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
13538 		bnxt_cfg_ntp_filters(bp);
13539 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
13540 		bnxt_hwrm_exec_fwd_req(bp);
13541 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
13542 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
13543 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
13544 		bnxt_hwrm_port_qstats(bp, 0);
13545 		bnxt_hwrm_port_qstats_ext(bp, 0);
13546 		bnxt_accumulate_all_stats(bp);
13547 	}
13548 
13549 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
13550 		int rc;
13551 
13552 		mutex_lock(&bp->link_lock);
13553 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
13554 				       &bp->sp_event))
13555 			bnxt_hwrm_phy_qcaps(bp);
13556 
13557 		rc = bnxt_update_link(bp, true);
13558 		if (rc)
13559 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
13560 				   rc);
13561 
13562 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
13563 				       &bp->sp_event))
13564 			bnxt_init_ethtool_link_settings(bp);
13565 		mutex_unlock(&bp->link_lock);
13566 	}
13567 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
13568 		int rc;
13569 
13570 		mutex_lock(&bp->link_lock);
13571 		rc = bnxt_update_phy_setting(bp);
13572 		mutex_unlock(&bp->link_lock);
13573 		if (rc) {
13574 			netdev_warn(bp->dev, "update phy settings retry failed\n");
13575 		} else {
13576 			bp->link_info.phy_retry = false;
13577 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
13578 		}
13579 	}
13580 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
13581 		mutex_lock(&bp->link_lock);
13582 		bnxt_get_port_module_status(bp);
13583 		mutex_unlock(&bp->link_lock);
13584 	}
13585 
13586 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
13587 		bnxt_tc_flow_stats_work(bp);
13588 
13589 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
13590 		bnxt_chk_missed_irq(bp);
13591 
13592 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
13593 		bnxt_fw_echo_reply(bp);
13594 
13595 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
13596 		bnxt_hwmon_notify_event(bp);
13597 
13598 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
13599 	 * must be the last functions to be called before exiting.
13600 	 */
13601 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
13602 		bnxt_reset(bp, false);
13603 
13604 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
13605 		bnxt_reset(bp, true);
13606 
13607 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
13608 		bnxt_rx_ring_reset(bp);
13609 
13610 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
13611 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
13612 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
13613 			bnxt_devlink_health_fw_report(bp);
13614 		else
13615 			bnxt_fw_reset(bp);
13616 	}
13617 
13618 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
13619 		if (!is_bnxt_fw_ok(bp))
13620 			bnxt_devlink_health_fw_report(bp);
13621 	}
13622 
13623 	smp_mb__before_atomic();
13624 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13625 }
13626 
13627 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
13628 				int *max_cp);
13629 
13630 /* Under rtnl_lock */
13631 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
13632 		     int tx_xdp)
13633 {
13634 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
13635 	struct bnxt_hw_rings hwr = {0};
13636 	int rx_rings = rx;
13637 
13638 	if (tcs)
13639 		tx_sets = tcs;
13640 
13641 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
13642 
13643 	if (max_rx < rx_rings)
13644 		return -ENOMEM;
13645 
13646 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
13647 		rx_rings <<= 1;
13648 
13649 	hwr.rx = rx_rings;
13650 	hwr.tx = tx * tx_sets + tx_xdp;
13651 	if (max_tx < hwr.tx)
13652 		return -ENOMEM;
13653 
13654 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
13655 
13656 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
13657 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
13658 	if (max_cp < hwr.cp)
13659 		return -ENOMEM;
13660 	hwr.stat = hwr.cp;
13661 	if (BNXT_NEW_RM(bp)) {
13662 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
13663 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
13664 		hwr.grp = rx;
13665 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13666 	}
13667 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
13668 		hwr.cp_p5 = hwr.tx + rx;
13669 	return bnxt_hwrm_check_rings(bp, &hwr);
13670 }
13671 
13672 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
13673 {
13674 	if (bp->bar2) {
13675 		pci_iounmap(pdev, bp->bar2);
13676 		bp->bar2 = NULL;
13677 	}
13678 
13679 	if (bp->bar1) {
13680 		pci_iounmap(pdev, bp->bar1);
13681 		bp->bar1 = NULL;
13682 	}
13683 
13684 	if (bp->bar0) {
13685 		pci_iounmap(pdev, bp->bar0);
13686 		bp->bar0 = NULL;
13687 	}
13688 }
13689 
13690 static void bnxt_cleanup_pci(struct bnxt *bp)
13691 {
13692 	bnxt_unmap_bars(bp, bp->pdev);
13693 	pci_release_regions(bp->pdev);
13694 	if (pci_is_enabled(bp->pdev))
13695 		pci_disable_device(bp->pdev);
13696 }
13697 
13698 static void bnxt_init_dflt_coal(struct bnxt *bp)
13699 {
13700 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
13701 	struct bnxt_coal *coal;
13702 	u16 flags = 0;
13703 
13704 	if (coal_cap->cmpl_params &
13705 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
13706 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
13707 
13708 	/* Tick values in micro seconds.
13709 	 * 1 coal_buf x bufs_per_record = 1 completion record.
13710 	 */
13711 	coal = &bp->rx_coal;
13712 	coal->coal_ticks = 10;
13713 	coal->coal_bufs = 30;
13714 	coal->coal_ticks_irq = 1;
13715 	coal->coal_bufs_irq = 2;
13716 	coal->idle_thresh = 50;
13717 	coal->bufs_per_record = 2;
13718 	coal->budget = 64;		/* NAPI budget */
13719 	coal->flags = flags;
13720 
13721 	coal = &bp->tx_coal;
13722 	coal->coal_ticks = 28;
13723 	coal->coal_bufs = 30;
13724 	coal->coal_ticks_irq = 2;
13725 	coal->coal_bufs_irq = 2;
13726 	coal->bufs_per_record = 1;
13727 	coal->flags = flags;
13728 
13729 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
13730 }
13731 
13732 /* FW that pre-reserves 1 VNIC per function */
13733 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
13734 {
13735 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
13736 
13737 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13738 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
13739 		return true;
13740 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13741 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
13742 		return true;
13743 	return false;
13744 }
13745 
13746 static int bnxt_fw_init_one_p1(struct bnxt *bp)
13747 {
13748 	int rc;
13749 
13750 	bp->fw_cap = 0;
13751 	rc = bnxt_hwrm_ver_get(bp);
13752 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
13753 	 * so wait before continuing with recovery.
13754 	 */
13755 	if (rc)
13756 		msleep(100);
13757 	bnxt_try_map_fw_health_reg(bp);
13758 	if (rc) {
13759 		rc = bnxt_try_recover_fw(bp);
13760 		if (rc)
13761 			return rc;
13762 		rc = bnxt_hwrm_ver_get(bp);
13763 		if (rc)
13764 			return rc;
13765 	}
13766 
13767 	bnxt_nvm_cfg_ver_get(bp);
13768 
13769 	rc = bnxt_hwrm_func_reset(bp);
13770 	if (rc)
13771 		return -ENODEV;
13772 
13773 	bnxt_hwrm_fw_set_time(bp);
13774 	return 0;
13775 }
13776 
13777 static int bnxt_fw_init_one_p2(struct bnxt *bp)
13778 {
13779 	int rc;
13780 
13781 	/* Get the MAX capabilities for this function */
13782 	rc = bnxt_hwrm_func_qcaps(bp);
13783 	if (rc) {
13784 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
13785 			   rc);
13786 		return -ENODEV;
13787 	}
13788 
13789 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
13790 	if (rc)
13791 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
13792 			    rc);
13793 
13794 	if (bnxt_alloc_fw_health(bp)) {
13795 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
13796 	} else {
13797 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
13798 		if (rc)
13799 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
13800 				    rc);
13801 	}
13802 
13803 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
13804 	if (rc)
13805 		return -ENODEV;
13806 
13807 	if (bnxt_fw_pre_resv_vnics(bp))
13808 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
13809 
13810 	bnxt_hwrm_func_qcfg(bp);
13811 	bnxt_hwrm_vnic_qcaps(bp);
13812 	bnxt_hwrm_port_led_qcaps(bp);
13813 	bnxt_ethtool_init(bp);
13814 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
13815 		__bnxt_hwrm_ptp_qcfg(bp);
13816 	bnxt_dcb_init(bp);
13817 	bnxt_hwmon_init(bp);
13818 	return 0;
13819 }
13820 
13821 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
13822 {
13823 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
13824 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
13825 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
13826 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
13827 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
13828 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
13829 		bp->rss_hash_delta = bp->rss_hash_cfg;
13830 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
13831 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
13832 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
13833 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
13834 	}
13835 }
13836 
13837 static void bnxt_set_dflt_rfs(struct bnxt *bp)
13838 {
13839 	struct net_device *dev = bp->dev;
13840 
13841 	dev->hw_features &= ~NETIF_F_NTUPLE;
13842 	dev->features &= ~NETIF_F_NTUPLE;
13843 	bp->flags &= ~BNXT_FLAG_RFS;
13844 	if (bnxt_rfs_supported(bp)) {
13845 		dev->hw_features |= NETIF_F_NTUPLE;
13846 		if (bnxt_rfs_capable(bp, false)) {
13847 			bp->flags |= BNXT_FLAG_RFS;
13848 			dev->features |= NETIF_F_NTUPLE;
13849 		}
13850 	}
13851 }
13852 
13853 static void bnxt_fw_init_one_p3(struct bnxt *bp)
13854 {
13855 	struct pci_dev *pdev = bp->pdev;
13856 
13857 	bnxt_set_dflt_rss_hash_type(bp);
13858 	bnxt_set_dflt_rfs(bp);
13859 
13860 	bnxt_get_wol_settings(bp);
13861 	if (bp->flags & BNXT_FLAG_WOL_CAP)
13862 		device_set_wakeup_enable(&pdev->dev, bp->wol);
13863 	else
13864 		device_set_wakeup_capable(&pdev->dev, false);
13865 
13866 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
13867 	bnxt_hwrm_coal_params_qcaps(bp);
13868 }
13869 
13870 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
13871 
13872 int bnxt_fw_init_one(struct bnxt *bp)
13873 {
13874 	int rc;
13875 
13876 	rc = bnxt_fw_init_one_p1(bp);
13877 	if (rc) {
13878 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
13879 		return rc;
13880 	}
13881 	rc = bnxt_fw_init_one_p2(bp);
13882 	if (rc) {
13883 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
13884 		return rc;
13885 	}
13886 	rc = bnxt_probe_phy(bp, false);
13887 	if (rc)
13888 		return rc;
13889 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
13890 	if (rc)
13891 		return rc;
13892 
13893 	bnxt_fw_init_one_p3(bp);
13894 	return 0;
13895 }
13896 
13897 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
13898 {
13899 	struct bnxt_fw_health *fw_health = bp->fw_health;
13900 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
13901 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
13902 	u32 reg_type, reg_off, delay_msecs;
13903 
13904 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
13905 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
13906 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
13907 	switch (reg_type) {
13908 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
13909 		pci_write_config_dword(bp->pdev, reg_off, val);
13910 		break;
13911 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
13912 		writel(reg_off & BNXT_GRC_BASE_MASK,
13913 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
13914 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
13915 		fallthrough;
13916 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
13917 		writel(val, bp->bar0 + reg_off);
13918 		break;
13919 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
13920 		writel(val, bp->bar1 + reg_off);
13921 		break;
13922 	}
13923 	if (delay_msecs) {
13924 		pci_read_config_dword(bp->pdev, 0, &val);
13925 		msleep(delay_msecs);
13926 	}
13927 }
13928 
13929 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
13930 {
13931 	struct hwrm_func_qcfg_output *resp;
13932 	struct hwrm_func_qcfg_input *req;
13933 	bool result = true; /* firmware will enforce if unknown */
13934 
13935 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
13936 		return result;
13937 
13938 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
13939 		return result;
13940 
13941 	req->fid = cpu_to_le16(0xffff);
13942 	resp = hwrm_req_hold(bp, req);
13943 	if (!hwrm_req_send(bp, req))
13944 		result = !!(le16_to_cpu(resp->flags) &
13945 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
13946 	hwrm_req_drop(bp, req);
13947 	return result;
13948 }
13949 
13950 static void bnxt_reset_all(struct bnxt *bp)
13951 {
13952 	struct bnxt_fw_health *fw_health = bp->fw_health;
13953 	int i, rc;
13954 
13955 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
13956 		bnxt_fw_reset_via_optee(bp);
13957 		bp->fw_reset_timestamp = jiffies;
13958 		return;
13959 	}
13960 
13961 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
13962 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
13963 			bnxt_fw_reset_writel(bp, i);
13964 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
13965 		struct hwrm_fw_reset_input *req;
13966 
13967 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
13968 		if (!rc) {
13969 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
13970 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
13971 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
13972 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
13973 			rc = hwrm_req_send(bp, req);
13974 		}
13975 		if (rc != -ENODEV)
13976 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
13977 	}
13978 	bp->fw_reset_timestamp = jiffies;
13979 }
13980 
13981 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
13982 {
13983 	return time_after(jiffies, bp->fw_reset_timestamp +
13984 			  (bp->fw_reset_max_dsecs * HZ / 10));
13985 }
13986 
13987 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
13988 {
13989 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13990 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
13991 		bnxt_dl_health_fw_status_update(bp, false);
13992 	bp->fw_reset_state = 0;
13993 	dev_close(bp->dev);
13994 }
13995 
13996 static void bnxt_fw_reset_task(struct work_struct *work)
13997 {
13998 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
13999 	int rc = 0;
14000 
14001 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14002 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
14003 		return;
14004 	}
14005 
14006 	switch (bp->fw_reset_state) {
14007 	case BNXT_FW_RESET_STATE_POLL_VF: {
14008 		int n = bnxt_get_registered_vfs(bp);
14009 		int tmo;
14010 
14011 		if (n < 0) {
14012 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
14013 				   n, jiffies_to_msecs(jiffies -
14014 				   bp->fw_reset_timestamp));
14015 			goto fw_reset_abort;
14016 		} else if (n > 0) {
14017 			if (bnxt_fw_reset_timeout(bp)) {
14018 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14019 				bp->fw_reset_state = 0;
14020 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
14021 					   n);
14022 				goto ulp_start;
14023 			}
14024 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14025 			return;
14026 		}
14027 		bp->fw_reset_timestamp = jiffies;
14028 		rtnl_lock();
14029 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
14030 			bnxt_fw_reset_abort(bp, rc);
14031 			rtnl_unlock();
14032 			goto ulp_start;
14033 		}
14034 		bnxt_fw_reset_close(bp);
14035 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14036 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14037 			tmo = HZ / 10;
14038 		} else {
14039 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14040 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14041 		}
14042 		rtnl_unlock();
14043 		bnxt_queue_fw_reset_work(bp, tmo);
14044 		return;
14045 	}
14046 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
14047 		u32 val;
14048 
14049 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14050 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
14051 		    !bnxt_fw_reset_timeout(bp)) {
14052 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14053 			return;
14054 		}
14055 
14056 		if (!bp->fw_health->primary) {
14057 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
14058 
14059 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14060 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14061 			return;
14062 		}
14063 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14064 	}
14065 		fallthrough;
14066 	case BNXT_FW_RESET_STATE_RESET_FW:
14067 		bnxt_reset_all(bp);
14068 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14069 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
14070 		return;
14071 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
14072 		bnxt_inv_fw_health_reg(bp);
14073 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
14074 		    !bp->fw_reset_min_dsecs) {
14075 			u16 val;
14076 
14077 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14078 			if (val == 0xffff) {
14079 				if (bnxt_fw_reset_timeout(bp)) {
14080 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
14081 					rc = -ETIMEDOUT;
14082 					goto fw_reset_abort;
14083 				}
14084 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
14085 				return;
14086 			}
14087 		}
14088 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14089 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
14090 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
14091 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
14092 			bnxt_dl_remote_reload(bp);
14093 		if (pci_enable_device(bp->pdev)) {
14094 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
14095 			rc = -ENODEV;
14096 			goto fw_reset_abort;
14097 		}
14098 		pci_set_master(bp->pdev);
14099 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
14100 		fallthrough;
14101 	case BNXT_FW_RESET_STATE_POLL_FW:
14102 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
14103 		rc = bnxt_hwrm_poll(bp);
14104 		if (rc) {
14105 			if (bnxt_fw_reset_timeout(bp)) {
14106 				netdev_err(bp->dev, "Firmware reset aborted\n");
14107 				goto fw_reset_abort_status;
14108 			}
14109 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14110 			return;
14111 		}
14112 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
14113 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
14114 		fallthrough;
14115 	case BNXT_FW_RESET_STATE_OPENING:
14116 		while (!rtnl_trylock()) {
14117 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14118 			return;
14119 		}
14120 		rc = bnxt_open(bp->dev);
14121 		if (rc) {
14122 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
14123 			bnxt_fw_reset_abort(bp, rc);
14124 			rtnl_unlock();
14125 			goto ulp_start;
14126 		}
14127 
14128 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
14129 		    bp->fw_health->enabled) {
14130 			bp->fw_health->last_fw_reset_cnt =
14131 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14132 		}
14133 		bp->fw_reset_state = 0;
14134 		/* Make sure fw_reset_state is 0 before clearing the flag */
14135 		smp_mb__before_atomic();
14136 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14137 		bnxt_ptp_reapply_pps(bp);
14138 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
14139 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
14140 			bnxt_dl_health_fw_recovery_done(bp);
14141 			bnxt_dl_health_fw_status_update(bp, true);
14142 		}
14143 		rtnl_unlock();
14144 		bnxt_ulp_start(bp, 0);
14145 		bnxt_reenable_sriov(bp);
14146 		rtnl_lock();
14147 		bnxt_vf_reps_alloc(bp);
14148 		bnxt_vf_reps_open(bp);
14149 		rtnl_unlock();
14150 		break;
14151 	}
14152 	return;
14153 
14154 fw_reset_abort_status:
14155 	if (bp->fw_health->status_reliable ||
14156 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
14157 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14158 
14159 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
14160 	}
14161 fw_reset_abort:
14162 	rtnl_lock();
14163 	bnxt_fw_reset_abort(bp, rc);
14164 	rtnl_unlock();
14165 ulp_start:
14166 	bnxt_ulp_start(bp, rc);
14167 }
14168 
14169 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
14170 {
14171 	int rc;
14172 	struct bnxt *bp = netdev_priv(dev);
14173 
14174 	SET_NETDEV_DEV(dev, &pdev->dev);
14175 
14176 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
14177 	rc = pci_enable_device(pdev);
14178 	if (rc) {
14179 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
14180 		goto init_err;
14181 	}
14182 
14183 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
14184 		dev_err(&pdev->dev,
14185 			"Cannot find PCI device base address, aborting\n");
14186 		rc = -ENODEV;
14187 		goto init_err_disable;
14188 	}
14189 
14190 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
14191 	if (rc) {
14192 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
14193 		goto init_err_disable;
14194 	}
14195 
14196 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
14197 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
14198 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
14199 		rc = -EIO;
14200 		goto init_err_release;
14201 	}
14202 
14203 	pci_set_master(pdev);
14204 
14205 	bp->dev = dev;
14206 	bp->pdev = pdev;
14207 
14208 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
14209 	 * determines the BAR size.
14210 	 */
14211 	bp->bar0 = pci_ioremap_bar(pdev, 0);
14212 	if (!bp->bar0) {
14213 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
14214 		rc = -ENOMEM;
14215 		goto init_err_release;
14216 	}
14217 
14218 	bp->bar2 = pci_ioremap_bar(pdev, 4);
14219 	if (!bp->bar2) {
14220 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
14221 		rc = -ENOMEM;
14222 		goto init_err_release;
14223 	}
14224 
14225 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
14226 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
14227 
14228 	spin_lock_init(&bp->ntp_fltr_lock);
14229 #if BITS_PER_LONG == 32
14230 	spin_lock_init(&bp->db_lock);
14231 #endif
14232 
14233 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
14234 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
14235 
14236 	timer_setup(&bp->timer, bnxt_timer, 0);
14237 	bp->current_interval = BNXT_TIMER_INTERVAL;
14238 
14239 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
14240 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
14241 
14242 	clear_bit(BNXT_STATE_OPEN, &bp->state);
14243 	return 0;
14244 
14245 init_err_release:
14246 	bnxt_unmap_bars(bp, pdev);
14247 	pci_release_regions(pdev);
14248 
14249 init_err_disable:
14250 	pci_disable_device(pdev);
14251 
14252 init_err:
14253 	return rc;
14254 }
14255 
14256 /* rtnl_lock held */
14257 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
14258 {
14259 	struct sockaddr *addr = p;
14260 	struct bnxt *bp = netdev_priv(dev);
14261 	int rc = 0;
14262 
14263 	if (!is_valid_ether_addr(addr->sa_data))
14264 		return -EADDRNOTAVAIL;
14265 
14266 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
14267 		return 0;
14268 
14269 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
14270 	if (rc)
14271 		return rc;
14272 
14273 	eth_hw_addr_set(dev, addr->sa_data);
14274 	bnxt_clear_usr_fltrs(bp, true);
14275 	if (netif_running(dev)) {
14276 		bnxt_close_nic(bp, false, false);
14277 		rc = bnxt_open_nic(bp, false, false);
14278 	}
14279 
14280 	return rc;
14281 }
14282 
14283 /* rtnl_lock held */
14284 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
14285 {
14286 	struct bnxt *bp = netdev_priv(dev);
14287 
14288 	if (netif_running(dev))
14289 		bnxt_close_nic(bp, true, false);
14290 
14291 	WRITE_ONCE(dev->mtu, new_mtu);
14292 	bnxt_set_ring_params(bp);
14293 
14294 	if (netif_running(dev))
14295 		return bnxt_open_nic(bp, true, false);
14296 
14297 	return 0;
14298 }
14299 
14300 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
14301 {
14302 	struct bnxt *bp = netdev_priv(dev);
14303 	bool sh = false;
14304 	int rc, tx_cp;
14305 
14306 	if (tc > bp->max_tc) {
14307 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
14308 			   tc, bp->max_tc);
14309 		return -EINVAL;
14310 	}
14311 
14312 	if (bp->num_tc == tc)
14313 		return 0;
14314 
14315 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
14316 		sh = true;
14317 
14318 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
14319 			      sh, tc, bp->tx_nr_rings_xdp);
14320 	if (rc)
14321 		return rc;
14322 
14323 	/* Needs to close the device and do hw resource re-allocations */
14324 	if (netif_running(bp->dev))
14325 		bnxt_close_nic(bp, true, false);
14326 
14327 	if (tc) {
14328 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
14329 		netdev_set_num_tc(dev, tc);
14330 		bp->num_tc = tc;
14331 	} else {
14332 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
14333 		netdev_reset_tc(dev);
14334 		bp->num_tc = 0;
14335 	}
14336 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
14337 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
14338 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
14339 			       tx_cp + bp->rx_nr_rings;
14340 
14341 	if (netif_running(bp->dev))
14342 		return bnxt_open_nic(bp, true, false);
14343 
14344 	return 0;
14345 }
14346 
14347 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
14348 				  void *cb_priv)
14349 {
14350 	struct bnxt *bp = cb_priv;
14351 
14352 	if (!bnxt_tc_flower_enabled(bp) ||
14353 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
14354 		return -EOPNOTSUPP;
14355 
14356 	switch (type) {
14357 	case TC_SETUP_CLSFLOWER:
14358 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
14359 	default:
14360 		return -EOPNOTSUPP;
14361 	}
14362 }
14363 
14364 LIST_HEAD(bnxt_block_cb_list);
14365 
14366 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
14367 			 void *type_data)
14368 {
14369 	struct bnxt *bp = netdev_priv(dev);
14370 
14371 	switch (type) {
14372 	case TC_SETUP_BLOCK:
14373 		return flow_block_cb_setup_simple(type_data,
14374 						  &bnxt_block_cb_list,
14375 						  bnxt_setup_tc_block_cb,
14376 						  bp, bp, true);
14377 	case TC_SETUP_QDISC_MQPRIO: {
14378 		struct tc_mqprio_qopt *mqprio = type_data;
14379 
14380 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
14381 
14382 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
14383 	}
14384 	default:
14385 		return -EOPNOTSUPP;
14386 	}
14387 }
14388 
14389 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
14390 			    const struct sk_buff *skb)
14391 {
14392 	struct bnxt_vnic_info *vnic;
14393 
14394 	if (skb)
14395 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
14396 
14397 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
14398 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
14399 }
14400 
14401 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
14402 			   u32 idx)
14403 {
14404 	struct hlist_head *head;
14405 	int bit_id;
14406 
14407 	spin_lock_bh(&bp->ntp_fltr_lock);
14408 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
14409 	if (bit_id < 0) {
14410 		spin_unlock_bh(&bp->ntp_fltr_lock);
14411 		return -ENOMEM;
14412 	}
14413 
14414 	fltr->base.sw_id = (u16)bit_id;
14415 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
14416 	fltr->base.flags |= BNXT_ACT_RING_DST;
14417 	head = &bp->ntp_fltr_hash_tbl[idx];
14418 	hlist_add_head_rcu(&fltr->base.hash, head);
14419 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
14420 	bnxt_insert_usr_fltr(bp, &fltr->base);
14421 	bp->ntp_fltr_count++;
14422 	spin_unlock_bh(&bp->ntp_fltr_lock);
14423 	return 0;
14424 }
14425 
14426 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
14427 			    struct bnxt_ntuple_filter *f2)
14428 {
14429 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
14430 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
14431 	struct flow_keys *keys1 = &f1->fkeys;
14432 	struct flow_keys *keys2 = &f2->fkeys;
14433 
14434 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
14435 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
14436 		return false;
14437 
14438 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
14439 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
14440 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
14441 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
14442 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
14443 			return false;
14444 	} else {
14445 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
14446 				     &keys2->addrs.v6addrs.src) ||
14447 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
14448 				     &masks2->addrs.v6addrs.src) ||
14449 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
14450 				     &keys2->addrs.v6addrs.dst) ||
14451 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
14452 				     &masks2->addrs.v6addrs.dst))
14453 			return false;
14454 	}
14455 
14456 	return keys1->ports.src == keys2->ports.src &&
14457 	       masks1->ports.src == masks2->ports.src &&
14458 	       keys1->ports.dst == keys2->ports.dst &&
14459 	       masks1->ports.dst == masks2->ports.dst &&
14460 	       keys1->control.flags == keys2->control.flags &&
14461 	       f1->l2_fltr == f2->l2_fltr;
14462 }
14463 
14464 struct bnxt_ntuple_filter *
14465 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
14466 				struct bnxt_ntuple_filter *fltr, u32 idx)
14467 {
14468 	struct bnxt_ntuple_filter *f;
14469 	struct hlist_head *head;
14470 
14471 	head = &bp->ntp_fltr_hash_tbl[idx];
14472 	hlist_for_each_entry_rcu(f, head, base.hash) {
14473 		if (bnxt_fltr_match(f, fltr))
14474 			return f;
14475 	}
14476 	return NULL;
14477 }
14478 
14479 #ifdef CONFIG_RFS_ACCEL
14480 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
14481 			      u16 rxq_index, u32 flow_id)
14482 {
14483 	struct bnxt *bp = netdev_priv(dev);
14484 	struct bnxt_ntuple_filter *fltr, *new_fltr;
14485 	struct flow_keys *fkeys;
14486 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
14487 	struct bnxt_l2_filter *l2_fltr;
14488 	int rc = 0, idx;
14489 	u32 flags;
14490 
14491 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
14492 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
14493 		atomic_inc(&l2_fltr->refcnt);
14494 	} else {
14495 		struct bnxt_l2_key key;
14496 
14497 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
14498 		key.vlan = 0;
14499 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
14500 		if (!l2_fltr)
14501 			return -EINVAL;
14502 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
14503 			bnxt_del_l2_filter(bp, l2_fltr);
14504 			return -EINVAL;
14505 		}
14506 	}
14507 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
14508 	if (!new_fltr) {
14509 		bnxt_del_l2_filter(bp, l2_fltr);
14510 		return -ENOMEM;
14511 	}
14512 
14513 	fkeys = &new_fltr->fkeys;
14514 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
14515 		rc = -EPROTONOSUPPORT;
14516 		goto err_free;
14517 	}
14518 
14519 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
14520 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
14521 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
14522 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
14523 		rc = -EPROTONOSUPPORT;
14524 		goto err_free;
14525 	}
14526 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
14527 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
14528 		if (bp->hwrm_spec_code < 0x10601) {
14529 			rc = -EPROTONOSUPPORT;
14530 			goto err_free;
14531 		}
14532 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
14533 	}
14534 	flags = fkeys->control.flags;
14535 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
14536 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
14537 		rc = -EPROTONOSUPPORT;
14538 		goto err_free;
14539 	}
14540 	new_fltr->l2_fltr = l2_fltr;
14541 
14542 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
14543 	rcu_read_lock();
14544 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
14545 	if (fltr) {
14546 		rc = fltr->base.sw_id;
14547 		rcu_read_unlock();
14548 		goto err_free;
14549 	}
14550 	rcu_read_unlock();
14551 
14552 	new_fltr->flow_id = flow_id;
14553 	new_fltr->base.rxq = rxq_index;
14554 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
14555 	if (!rc) {
14556 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14557 		return new_fltr->base.sw_id;
14558 	}
14559 
14560 err_free:
14561 	bnxt_del_l2_filter(bp, l2_fltr);
14562 	kfree(new_fltr);
14563 	return rc;
14564 }
14565 #endif
14566 
14567 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
14568 {
14569 	spin_lock_bh(&bp->ntp_fltr_lock);
14570 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
14571 		spin_unlock_bh(&bp->ntp_fltr_lock);
14572 		return;
14573 	}
14574 	hlist_del_rcu(&fltr->base.hash);
14575 	bnxt_del_one_usr_fltr(bp, &fltr->base);
14576 	bp->ntp_fltr_count--;
14577 	spin_unlock_bh(&bp->ntp_fltr_lock);
14578 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
14579 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
14580 	kfree_rcu(fltr, base.rcu);
14581 }
14582 
14583 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
14584 {
14585 #ifdef CONFIG_RFS_ACCEL
14586 	int i;
14587 
14588 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
14589 		struct hlist_head *head;
14590 		struct hlist_node *tmp;
14591 		struct bnxt_ntuple_filter *fltr;
14592 		int rc;
14593 
14594 		head = &bp->ntp_fltr_hash_tbl[i];
14595 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
14596 			bool del = false;
14597 
14598 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
14599 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
14600 					continue;
14601 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
14602 							fltr->flow_id,
14603 							fltr->base.sw_id)) {
14604 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
14605 									 fltr);
14606 					del = true;
14607 				}
14608 			} else {
14609 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
14610 								       fltr);
14611 				if (rc)
14612 					del = true;
14613 				else
14614 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
14615 			}
14616 
14617 			if (del)
14618 				bnxt_del_ntp_filter(bp, fltr);
14619 		}
14620 	}
14621 #endif
14622 }
14623 
14624 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
14625 				    unsigned int entry, struct udp_tunnel_info *ti)
14626 {
14627 	struct bnxt *bp = netdev_priv(netdev);
14628 	unsigned int cmd;
14629 
14630 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
14631 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
14632 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
14633 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
14634 	else
14635 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
14636 
14637 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
14638 }
14639 
14640 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
14641 				      unsigned int entry, struct udp_tunnel_info *ti)
14642 {
14643 	struct bnxt *bp = netdev_priv(netdev);
14644 	unsigned int cmd;
14645 
14646 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
14647 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
14648 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
14649 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
14650 	else
14651 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
14652 
14653 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
14654 }
14655 
14656 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
14657 	.set_port	= bnxt_udp_tunnel_set_port,
14658 	.unset_port	= bnxt_udp_tunnel_unset_port,
14659 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
14660 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
14661 	.tables		= {
14662 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
14663 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
14664 	},
14665 }, bnxt_udp_tunnels_p7 = {
14666 	.set_port	= bnxt_udp_tunnel_set_port,
14667 	.unset_port	= bnxt_udp_tunnel_unset_port,
14668 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
14669 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
14670 	.tables		= {
14671 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
14672 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
14673 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
14674 	},
14675 };
14676 
14677 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
14678 			       struct net_device *dev, u32 filter_mask,
14679 			       int nlflags)
14680 {
14681 	struct bnxt *bp = netdev_priv(dev);
14682 
14683 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
14684 				       nlflags, filter_mask, NULL);
14685 }
14686 
14687 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
14688 			       u16 flags, struct netlink_ext_ack *extack)
14689 {
14690 	struct bnxt *bp = netdev_priv(dev);
14691 	struct nlattr *attr, *br_spec;
14692 	int rem, rc = 0;
14693 
14694 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
14695 		return -EOPNOTSUPP;
14696 
14697 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
14698 	if (!br_spec)
14699 		return -EINVAL;
14700 
14701 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
14702 		u16 mode;
14703 
14704 		mode = nla_get_u16(attr);
14705 		if (mode == bp->br_mode)
14706 			break;
14707 
14708 		rc = bnxt_hwrm_set_br_mode(bp, mode);
14709 		if (!rc)
14710 			bp->br_mode = mode;
14711 		break;
14712 	}
14713 	return rc;
14714 }
14715 
14716 int bnxt_get_port_parent_id(struct net_device *dev,
14717 			    struct netdev_phys_item_id *ppid)
14718 {
14719 	struct bnxt *bp = netdev_priv(dev);
14720 
14721 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
14722 		return -EOPNOTSUPP;
14723 
14724 	/* The PF and it's VF-reps only support the switchdev framework */
14725 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
14726 		return -EOPNOTSUPP;
14727 
14728 	ppid->id_len = sizeof(bp->dsn);
14729 	memcpy(ppid->id, bp->dsn, ppid->id_len);
14730 
14731 	return 0;
14732 }
14733 
14734 static const struct net_device_ops bnxt_netdev_ops = {
14735 	.ndo_open		= bnxt_open,
14736 	.ndo_start_xmit		= bnxt_start_xmit,
14737 	.ndo_stop		= bnxt_close,
14738 	.ndo_get_stats64	= bnxt_get_stats64,
14739 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
14740 	.ndo_eth_ioctl		= bnxt_ioctl,
14741 	.ndo_validate_addr	= eth_validate_addr,
14742 	.ndo_set_mac_address	= bnxt_change_mac_addr,
14743 	.ndo_change_mtu		= bnxt_change_mtu,
14744 	.ndo_fix_features	= bnxt_fix_features,
14745 	.ndo_set_features	= bnxt_set_features,
14746 	.ndo_features_check	= bnxt_features_check,
14747 	.ndo_tx_timeout		= bnxt_tx_timeout,
14748 #ifdef CONFIG_BNXT_SRIOV
14749 	.ndo_get_vf_config	= bnxt_get_vf_config,
14750 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
14751 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
14752 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
14753 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
14754 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
14755 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
14756 #endif
14757 	.ndo_setup_tc           = bnxt_setup_tc,
14758 #ifdef CONFIG_RFS_ACCEL
14759 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
14760 #endif
14761 	.ndo_bpf		= bnxt_xdp,
14762 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
14763 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
14764 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
14765 };
14766 
14767 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
14768 				    struct netdev_queue_stats_rx *stats)
14769 {
14770 	struct bnxt *bp = netdev_priv(dev);
14771 	struct bnxt_cp_ring_info *cpr;
14772 	u64 *sw;
14773 
14774 	cpr = &bp->bnapi[i]->cp_ring;
14775 	sw = cpr->stats.sw_stats;
14776 
14777 	stats->packets = 0;
14778 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
14779 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
14780 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
14781 
14782 	stats->bytes = 0;
14783 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
14784 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
14785 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
14786 
14787 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
14788 }
14789 
14790 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
14791 				    struct netdev_queue_stats_tx *stats)
14792 {
14793 	struct bnxt *bp = netdev_priv(dev);
14794 	struct bnxt_napi *bnapi;
14795 	u64 *sw;
14796 
14797 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
14798 	sw = bnapi->cp_ring.stats.sw_stats;
14799 
14800 	stats->packets = 0;
14801 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
14802 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
14803 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
14804 
14805 	stats->bytes = 0;
14806 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
14807 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
14808 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
14809 }
14810 
14811 static void bnxt_get_base_stats(struct net_device *dev,
14812 				struct netdev_queue_stats_rx *rx,
14813 				struct netdev_queue_stats_tx *tx)
14814 {
14815 	struct bnxt *bp = netdev_priv(dev);
14816 
14817 	rx->packets = bp->net_stats_prev.rx_packets;
14818 	rx->bytes = bp->net_stats_prev.rx_bytes;
14819 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
14820 
14821 	tx->packets = bp->net_stats_prev.tx_packets;
14822 	tx->bytes = bp->net_stats_prev.tx_bytes;
14823 }
14824 
14825 static const struct netdev_stat_ops bnxt_stat_ops = {
14826 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
14827 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
14828 	.get_base_stats		= bnxt_get_base_stats,
14829 };
14830 
14831 static void bnxt_remove_one(struct pci_dev *pdev)
14832 {
14833 	struct net_device *dev = pci_get_drvdata(pdev);
14834 	struct bnxt *bp = netdev_priv(dev);
14835 
14836 	if (BNXT_PF(bp))
14837 		bnxt_sriov_disable(bp);
14838 
14839 	bnxt_rdma_aux_device_del(bp);
14840 
14841 	bnxt_ptp_clear(bp);
14842 	unregister_netdev(dev);
14843 
14844 	bnxt_rdma_aux_device_uninit(bp);
14845 
14846 	bnxt_free_l2_filters(bp, true);
14847 	bnxt_free_ntp_fltrs(bp, true);
14848 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
14849 		bnxt_clear_rss_ctxs(bp, true);
14850 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14851 	/* Flush any pending tasks */
14852 	cancel_work_sync(&bp->sp_task);
14853 	cancel_delayed_work_sync(&bp->fw_reset_task);
14854 	bp->sp_event = 0;
14855 
14856 	bnxt_dl_fw_reporters_destroy(bp);
14857 	bnxt_dl_unregister(bp);
14858 	bnxt_shutdown_tc(bp);
14859 
14860 	bnxt_clear_int_mode(bp);
14861 	bnxt_hwrm_func_drv_unrgtr(bp);
14862 	bnxt_free_hwrm_resources(bp);
14863 	bnxt_hwmon_uninit(bp);
14864 	bnxt_ethtool_free(bp);
14865 	bnxt_dcb_free(bp);
14866 	kfree(bp->ptp_cfg);
14867 	bp->ptp_cfg = NULL;
14868 	kfree(bp->fw_health);
14869 	bp->fw_health = NULL;
14870 	bnxt_cleanup_pci(bp);
14871 	bnxt_free_ctx_mem(bp);
14872 	kfree(bp->rss_indir_tbl);
14873 	bp->rss_indir_tbl = NULL;
14874 	bnxt_free_port_stats(bp);
14875 	free_netdev(dev);
14876 }
14877 
14878 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
14879 {
14880 	int rc = 0;
14881 	struct bnxt_link_info *link_info = &bp->link_info;
14882 
14883 	bp->phy_flags = 0;
14884 	rc = bnxt_hwrm_phy_qcaps(bp);
14885 	if (rc) {
14886 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
14887 			   rc);
14888 		return rc;
14889 	}
14890 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
14891 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
14892 	else
14893 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
14894 	if (!fw_dflt)
14895 		return 0;
14896 
14897 	mutex_lock(&bp->link_lock);
14898 	rc = bnxt_update_link(bp, false);
14899 	if (rc) {
14900 		mutex_unlock(&bp->link_lock);
14901 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
14902 			   rc);
14903 		return rc;
14904 	}
14905 
14906 	/* Older firmware does not have supported_auto_speeds, so assume
14907 	 * that all supported speeds can be autonegotiated.
14908 	 */
14909 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
14910 		link_info->support_auto_speeds = link_info->support_speeds;
14911 
14912 	bnxt_init_ethtool_link_settings(bp);
14913 	mutex_unlock(&bp->link_lock);
14914 	return 0;
14915 }
14916 
14917 static int bnxt_get_max_irq(struct pci_dev *pdev)
14918 {
14919 	u16 ctrl;
14920 
14921 	if (!pdev->msix_cap)
14922 		return 1;
14923 
14924 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
14925 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
14926 }
14927 
14928 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14929 				int *max_cp)
14930 {
14931 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
14932 	int max_ring_grps = 0, max_irq;
14933 
14934 	*max_tx = hw_resc->max_tx_rings;
14935 	*max_rx = hw_resc->max_rx_rings;
14936 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
14937 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
14938 			bnxt_get_ulp_msix_num_in_use(bp),
14939 			hw_resc->max_stat_ctxs -
14940 			bnxt_get_ulp_stat_ctxs_in_use(bp));
14941 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14942 		*max_cp = min_t(int, *max_cp, max_irq);
14943 	max_ring_grps = hw_resc->max_hw_ring_grps;
14944 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
14945 		*max_cp -= 1;
14946 		*max_rx -= 2;
14947 	}
14948 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14949 		*max_rx >>= 1;
14950 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
14951 		int rc;
14952 
14953 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
14954 		if (rc) {
14955 			*max_rx = 0;
14956 			*max_tx = 0;
14957 		}
14958 		/* On P5 chips, max_cp output param should be available NQs */
14959 		*max_cp = max_irq;
14960 	}
14961 	*max_rx = min_t(int, *max_rx, max_ring_grps);
14962 }
14963 
14964 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
14965 {
14966 	int rx, tx, cp;
14967 
14968 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
14969 	*max_rx = rx;
14970 	*max_tx = tx;
14971 	if (!rx || !tx || !cp)
14972 		return -ENOMEM;
14973 
14974 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
14975 }
14976 
14977 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14978 			       bool shared)
14979 {
14980 	int rc;
14981 
14982 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
14983 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
14984 		/* Not enough rings, try disabling agg rings. */
14985 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
14986 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
14987 		if (rc) {
14988 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
14989 			bp->flags |= BNXT_FLAG_AGG_RINGS;
14990 			return rc;
14991 		}
14992 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
14993 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
14994 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
14995 		bnxt_set_ring_params(bp);
14996 	}
14997 
14998 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
14999 		int max_cp, max_stat, max_irq;
15000 
15001 		/* Reserve minimum resources for RoCE */
15002 		max_cp = bnxt_get_max_func_cp_rings(bp);
15003 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
15004 		max_irq = bnxt_get_max_func_irqs(bp);
15005 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
15006 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
15007 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
15008 			return 0;
15009 
15010 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
15011 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
15012 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
15013 		max_cp = min_t(int, max_cp, max_irq);
15014 		max_cp = min_t(int, max_cp, max_stat);
15015 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
15016 		if (rc)
15017 			rc = 0;
15018 	}
15019 	return rc;
15020 }
15021 
15022 /* In initial default shared ring setting, each shared ring must have a
15023  * RX/TX ring pair.
15024  */
15025 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
15026 {
15027 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
15028 	bp->rx_nr_rings = bp->cp_nr_rings;
15029 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
15030 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15031 }
15032 
15033 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
15034 {
15035 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
15036 	int avail_msix;
15037 
15038 	if (!bnxt_can_reserve_rings(bp))
15039 		return 0;
15040 
15041 	if (sh)
15042 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
15043 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
15044 	/* Reduce default rings on multi-port cards so that total default
15045 	 * rings do not exceed CPU count.
15046 	 */
15047 	if (bp->port_count > 1) {
15048 		int max_rings =
15049 			max_t(int, num_online_cpus() / bp->port_count, 1);
15050 
15051 		dflt_rings = min_t(int, dflt_rings, max_rings);
15052 	}
15053 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
15054 	if (rc)
15055 		return rc;
15056 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
15057 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
15058 	if (sh)
15059 		bnxt_trim_dflt_sh_rings(bp);
15060 	else
15061 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
15062 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15063 
15064 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
15065 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
15066 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
15067 
15068 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
15069 		bnxt_set_dflt_ulp_stat_ctxs(bp);
15070 	}
15071 
15072 	rc = __bnxt_reserve_rings(bp);
15073 	if (rc && rc != -ENODEV)
15074 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
15075 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15076 	if (sh)
15077 		bnxt_trim_dflt_sh_rings(bp);
15078 
15079 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
15080 	if (bnxt_need_reserve_rings(bp)) {
15081 		rc = __bnxt_reserve_rings(bp);
15082 		if (rc && rc != -ENODEV)
15083 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
15084 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15085 	}
15086 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
15087 		bp->rx_nr_rings++;
15088 		bp->cp_nr_rings++;
15089 	}
15090 	if (rc) {
15091 		bp->tx_nr_rings = 0;
15092 		bp->rx_nr_rings = 0;
15093 	}
15094 	return rc;
15095 }
15096 
15097 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
15098 {
15099 	int rc;
15100 
15101 	if (bp->tx_nr_rings)
15102 		return 0;
15103 
15104 	bnxt_ulp_irq_stop(bp);
15105 	bnxt_clear_int_mode(bp);
15106 	rc = bnxt_set_dflt_rings(bp, true);
15107 	if (rc) {
15108 		if (BNXT_VF(bp) && rc == -ENODEV)
15109 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
15110 		else
15111 			netdev_err(bp->dev, "Not enough rings available.\n");
15112 		goto init_dflt_ring_err;
15113 	}
15114 	rc = bnxt_init_int_mode(bp);
15115 	if (rc)
15116 		goto init_dflt_ring_err;
15117 
15118 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15119 
15120 	bnxt_set_dflt_rfs(bp);
15121 
15122 init_dflt_ring_err:
15123 	bnxt_ulp_irq_restart(bp, rc);
15124 	return rc;
15125 }
15126 
15127 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
15128 {
15129 	int rc;
15130 
15131 	ASSERT_RTNL();
15132 	bnxt_hwrm_func_qcaps(bp);
15133 
15134 	if (netif_running(bp->dev))
15135 		__bnxt_close_nic(bp, true, false);
15136 
15137 	bnxt_ulp_irq_stop(bp);
15138 	bnxt_clear_int_mode(bp);
15139 	rc = bnxt_init_int_mode(bp);
15140 	bnxt_ulp_irq_restart(bp, rc);
15141 
15142 	if (netif_running(bp->dev)) {
15143 		if (rc)
15144 			dev_close(bp->dev);
15145 		else
15146 			rc = bnxt_open_nic(bp, true, false);
15147 	}
15148 
15149 	return rc;
15150 }
15151 
15152 static int bnxt_init_mac_addr(struct bnxt *bp)
15153 {
15154 	int rc = 0;
15155 
15156 	if (BNXT_PF(bp)) {
15157 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
15158 	} else {
15159 #ifdef CONFIG_BNXT_SRIOV
15160 		struct bnxt_vf_info *vf = &bp->vf;
15161 		bool strict_approval = true;
15162 
15163 		if (is_valid_ether_addr(vf->mac_addr)) {
15164 			/* overwrite netdev dev_addr with admin VF MAC */
15165 			eth_hw_addr_set(bp->dev, vf->mac_addr);
15166 			/* Older PF driver or firmware may not approve this
15167 			 * correctly.
15168 			 */
15169 			strict_approval = false;
15170 		} else {
15171 			eth_hw_addr_random(bp->dev);
15172 		}
15173 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
15174 #endif
15175 	}
15176 	return rc;
15177 }
15178 
15179 static void bnxt_vpd_read_info(struct bnxt *bp)
15180 {
15181 	struct pci_dev *pdev = bp->pdev;
15182 	unsigned int vpd_size, kw_len;
15183 	int pos, size;
15184 	u8 *vpd_data;
15185 
15186 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
15187 	if (IS_ERR(vpd_data)) {
15188 		pci_warn(pdev, "Unable to read VPD\n");
15189 		return;
15190 	}
15191 
15192 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
15193 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
15194 	if (pos < 0)
15195 		goto read_sn;
15196 
15197 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
15198 	memcpy(bp->board_partno, &vpd_data[pos], size);
15199 
15200 read_sn:
15201 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
15202 					   PCI_VPD_RO_KEYWORD_SERIALNO,
15203 					   &kw_len);
15204 	if (pos < 0)
15205 		goto exit;
15206 
15207 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
15208 	memcpy(bp->board_serialno, &vpd_data[pos], size);
15209 exit:
15210 	kfree(vpd_data);
15211 }
15212 
15213 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
15214 {
15215 	struct pci_dev *pdev = bp->pdev;
15216 	u64 qword;
15217 
15218 	qword = pci_get_dsn(pdev);
15219 	if (!qword) {
15220 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
15221 		return -EOPNOTSUPP;
15222 	}
15223 
15224 	put_unaligned_le64(qword, dsn);
15225 
15226 	bp->flags |= BNXT_FLAG_DSN_VALID;
15227 	return 0;
15228 }
15229 
15230 static int bnxt_map_db_bar(struct bnxt *bp)
15231 {
15232 	if (!bp->db_size)
15233 		return -ENODEV;
15234 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
15235 	if (!bp->bar1)
15236 		return -ENOMEM;
15237 	return 0;
15238 }
15239 
15240 void bnxt_print_device_info(struct bnxt *bp)
15241 {
15242 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
15243 		    board_info[bp->board_idx].name,
15244 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
15245 
15246 	pcie_print_link_status(bp->pdev);
15247 }
15248 
15249 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
15250 {
15251 	struct bnxt_hw_resc *hw_resc;
15252 	struct net_device *dev;
15253 	struct bnxt *bp;
15254 	int rc, max_irqs;
15255 
15256 	if (pci_is_bridge(pdev))
15257 		return -ENODEV;
15258 
15259 	/* Clear any pending DMA transactions from crash kernel
15260 	 * while loading driver in capture kernel.
15261 	 */
15262 	if (is_kdump_kernel()) {
15263 		pci_clear_master(pdev);
15264 		pcie_flr(pdev);
15265 	}
15266 
15267 	max_irqs = bnxt_get_max_irq(pdev);
15268 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
15269 				 max_irqs);
15270 	if (!dev)
15271 		return -ENOMEM;
15272 
15273 	bp = netdev_priv(dev);
15274 	bp->board_idx = ent->driver_data;
15275 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
15276 	bnxt_set_max_func_irqs(bp, max_irqs);
15277 
15278 	if (bnxt_vf_pciid(bp->board_idx))
15279 		bp->flags |= BNXT_FLAG_VF;
15280 
15281 	/* No devlink port registration in case of a VF */
15282 	if (BNXT_PF(bp))
15283 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
15284 
15285 	if (pdev->msix_cap)
15286 		bp->flags |= BNXT_FLAG_MSIX_CAP;
15287 
15288 	rc = bnxt_init_board(pdev, dev);
15289 	if (rc < 0)
15290 		goto init_err_free;
15291 
15292 	dev->netdev_ops = &bnxt_netdev_ops;
15293 	dev->stat_ops = &bnxt_stat_ops;
15294 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
15295 	dev->ethtool_ops = &bnxt_ethtool_ops;
15296 	pci_set_drvdata(pdev, dev);
15297 
15298 	rc = bnxt_alloc_hwrm_resources(bp);
15299 	if (rc)
15300 		goto init_err_pci_clean;
15301 
15302 	mutex_init(&bp->hwrm_cmd_lock);
15303 	mutex_init(&bp->link_lock);
15304 
15305 	rc = bnxt_fw_init_one_p1(bp);
15306 	if (rc)
15307 		goto init_err_pci_clean;
15308 
15309 	if (BNXT_PF(bp))
15310 		bnxt_vpd_read_info(bp);
15311 
15312 	if (BNXT_CHIP_P5_PLUS(bp)) {
15313 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
15314 		if (BNXT_CHIP_P7(bp))
15315 			bp->flags |= BNXT_FLAG_CHIP_P7;
15316 	}
15317 
15318 	rc = bnxt_alloc_rss_indir_tbl(bp, NULL);
15319 	if (rc)
15320 		goto init_err_pci_clean;
15321 
15322 	rc = bnxt_fw_init_one_p2(bp);
15323 	if (rc)
15324 		goto init_err_pci_clean;
15325 
15326 	rc = bnxt_map_db_bar(bp);
15327 	if (rc) {
15328 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
15329 			rc);
15330 		goto init_err_pci_clean;
15331 	}
15332 
15333 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
15334 			   NETIF_F_TSO | NETIF_F_TSO6 |
15335 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
15336 			   NETIF_F_GSO_IPXIP4 |
15337 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
15338 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
15339 			   NETIF_F_RXCSUM | NETIF_F_GRO;
15340 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
15341 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
15342 
15343 	if (BNXT_SUPPORTS_TPA(bp))
15344 		dev->hw_features |= NETIF_F_LRO;
15345 
15346 	dev->hw_enc_features =
15347 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
15348 			NETIF_F_TSO | NETIF_F_TSO6 |
15349 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
15350 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
15351 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
15352 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
15353 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
15354 	if (bp->flags & BNXT_FLAG_CHIP_P7)
15355 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
15356 	else
15357 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
15358 
15359 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
15360 				    NETIF_F_GSO_GRE_CSUM;
15361 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
15362 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
15363 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
15364 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
15365 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
15366 	if (BNXT_SUPPORTS_TPA(bp))
15367 		dev->hw_features |= NETIF_F_GRO_HW;
15368 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
15369 	if (dev->features & NETIF_F_GRO_HW)
15370 		dev->features &= ~NETIF_F_LRO;
15371 	dev->priv_flags |= IFF_UNICAST_FLT;
15372 
15373 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
15374 
15375 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
15376 			    NETDEV_XDP_ACT_RX_SG;
15377 
15378 #ifdef CONFIG_BNXT_SRIOV
15379 	init_waitqueue_head(&bp->sriov_cfg_wait);
15380 #endif
15381 	if (BNXT_SUPPORTS_TPA(bp)) {
15382 		bp->gro_func = bnxt_gro_func_5730x;
15383 		if (BNXT_CHIP_P4(bp))
15384 			bp->gro_func = bnxt_gro_func_5731x;
15385 		else if (BNXT_CHIP_P5_PLUS(bp))
15386 			bp->gro_func = bnxt_gro_func_5750x;
15387 	}
15388 	if (!BNXT_CHIP_P4_PLUS(bp))
15389 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
15390 
15391 	rc = bnxt_init_mac_addr(bp);
15392 	if (rc) {
15393 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
15394 		rc = -EADDRNOTAVAIL;
15395 		goto init_err_pci_clean;
15396 	}
15397 
15398 	if (BNXT_PF(bp)) {
15399 		/* Read the adapter's DSN to use as the eswitch switch_id */
15400 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
15401 	}
15402 
15403 	/* MTU range: 60 - FW defined max */
15404 	dev->min_mtu = ETH_ZLEN;
15405 	dev->max_mtu = bp->max_mtu;
15406 
15407 	rc = bnxt_probe_phy(bp, true);
15408 	if (rc)
15409 		goto init_err_pci_clean;
15410 
15411 	hw_resc = &bp->hw_resc;
15412 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
15413 		       BNXT_L2_FLTR_MAX_FLTR;
15414 	/* Older firmware may not report these filters properly */
15415 	if (bp->max_fltr < BNXT_MAX_FLTR)
15416 		bp->max_fltr = BNXT_MAX_FLTR;
15417 	bnxt_init_l2_fltr_tbl(bp);
15418 	bnxt_set_rx_skb_mode(bp, false);
15419 	bnxt_set_tpa_flags(bp);
15420 	bnxt_set_ring_params(bp);
15421 	bnxt_rdma_aux_device_init(bp);
15422 	rc = bnxt_set_dflt_rings(bp, true);
15423 	if (rc) {
15424 		if (BNXT_VF(bp) && rc == -ENODEV) {
15425 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
15426 		} else {
15427 			netdev_err(bp->dev, "Not enough rings available.\n");
15428 			rc = -ENOMEM;
15429 		}
15430 		goto init_err_pci_clean;
15431 	}
15432 
15433 	bnxt_fw_init_one_p3(bp);
15434 
15435 	bnxt_init_dflt_coal(bp);
15436 
15437 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
15438 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
15439 
15440 	rc = bnxt_init_int_mode(bp);
15441 	if (rc)
15442 		goto init_err_pci_clean;
15443 
15444 	/* No TC has been set yet and rings may have been trimmed due to
15445 	 * limited MSIX, so we re-initialize the TX rings per TC.
15446 	 */
15447 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15448 
15449 	if (BNXT_PF(bp)) {
15450 		if (!bnxt_pf_wq) {
15451 			bnxt_pf_wq =
15452 				create_singlethread_workqueue("bnxt_pf_wq");
15453 			if (!bnxt_pf_wq) {
15454 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
15455 				rc = -ENOMEM;
15456 				goto init_err_pci_clean;
15457 			}
15458 		}
15459 		rc = bnxt_init_tc(bp);
15460 		if (rc)
15461 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
15462 				   rc);
15463 	}
15464 
15465 	bnxt_inv_fw_health_reg(bp);
15466 	rc = bnxt_dl_register(bp);
15467 	if (rc)
15468 		goto init_err_dl;
15469 
15470 	INIT_LIST_HEAD(&bp->usr_fltr_list);
15471 
15472 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
15473 		bnxt_init_multi_rss_ctx(bp);
15474 
15475 
15476 	rc = register_netdev(dev);
15477 	if (rc)
15478 		goto init_err_cleanup;
15479 
15480 	bnxt_dl_fw_reporters_create(bp);
15481 
15482 	bnxt_rdma_aux_device_add(bp);
15483 
15484 	bnxt_print_device_info(bp);
15485 
15486 	pci_save_state(pdev);
15487 
15488 	return 0;
15489 init_err_cleanup:
15490 	bnxt_rdma_aux_device_uninit(bp);
15491 	bnxt_dl_unregister(bp);
15492 init_err_dl:
15493 	bnxt_shutdown_tc(bp);
15494 	bnxt_clear_int_mode(bp);
15495 
15496 init_err_pci_clean:
15497 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
15498 		bnxt_clear_rss_ctxs(bp, true);
15499 	bnxt_hwrm_func_drv_unrgtr(bp);
15500 	bnxt_free_hwrm_resources(bp);
15501 	bnxt_hwmon_uninit(bp);
15502 	bnxt_ethtool_free(bp);
15503 	bnxt_ptp_clear(bp);
15504 	kfree(bp->ptp_cfg);
15505 	bp->ptp_cfg = NULL;
15506 	kfree(bp->fw_health);
15507 	bp->fw_health = NULL;
15508 	bnxt_cleanup_pci(bp);
15509 	bnxt_free_ctx_mem(bp);
15510 	kfree(bp->rss_indir_tbl);
15511 	bp->rss_indir_tbl = NULL;
15512 
15513 init_err_free:
15514 	free_netdev(dev);
15515 	return rc;
15516 }
15517 
15518 static void bnxt_shutdown(struct pci_dev *pdev)
15519 {
15520 	struct net_device *dev = pci_get_drvdata(pdev);
15521 	struct bnxt *bp;
15522 
15523 	if (!dev)
15524 		return;
15525 
15526 	rtnl_lock();
15527 	bp = netdev_priv(dev);
15528 	if (!bp)
15529 		goto shutdown_exit;
15530 
15531 	if (netif_running(dev))
15532 		dev_close(dev);
15533 
15534 	bnxt_clear_int_mode(bp);
15535 	pci_disable_device(pdev);
15536 
15537 	if (system_state == SYSTEM_POWER_OFF) {
15538 		pci_wake_from_d3(pdev, bp->wol);
15539 		pci_set_power_state(pdev, PCI_D3hot);
15540 	}
15541 
15542 shutdown_exit:
15543 	rtnl_unlock();
15544 }
15545 
15546 #ifdef CONFIG_PM_SLEEP
15547 static int bnxt_suspend(struct device *device)
15548 {
15549 	struct net_device *dev = dev_get_drvdata(device);
15550 	struct bnxt *bp = netdev_priv(dev);
15551 	int rc = 0;
15552 
15553 	bnxt_ulp_stop(bp);
15554 
15555 	rtnl_lock();
15556 	if (netif_running(dev)) {
15557 		netif_device_detach(dev);
15558 		rc = bnxt_close(dev);
15559 	}
15560 	bnxt_hwrm_func_drv_unrgtr(bp);
15561 	pci_disable_device(bp->pdev);
15562 	bnxt_free_ctx_mem(bp);
15563 	rtnl_unlock();
15564 	return rc;
15565 }
15566 
15567 static int bnxt_resume(struct device *device)
15568 {
15569 	struct net_device *dev = dev_get_drvdata(device);
15570 	struct bnxt *bp = netdev_priv(dev);
15571 	int rc = 0;
15572 
15573 	rtnl_lock();
15574 	rc = pci_enable_device(bp->pdev);
15575 	if (rc) {
15576 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
15577 			   rc);
15578 		goto resume_exit;
15579 	}
15580 	pci_set_master(bp->pdev);
15581 	if (bnxt_hwrm_ver_get(bp)) {
15582 		rc = -ENODEV;
15583 		goto resume_exit;
15584 	}
15585 	rc = bnxt_hwrm_func_reset(bp);
15586 	if (rc) {
15587 		rc = -EBUSY;
15588 		goto resume_exit;
15589 	}
15590 
15591 	rc = bnxt_hwrm_func_qcaps(bp);
15592 	if (rc)
15593 		goto resume_exit;
15594 
15595 	bnxt_clear_reservations(bp, true);
15596 
15597 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
15598 		rc = -ENODEV;
15599 		goto resume_exit;
15600 	}
15601 
15602 	bnxt_get_wol_settings(bp);
15603 	if (netif_running(dev)) {
15604 		rc = bnxt_open(dev);
15605 		if (!rc)
15606 			netif_device_attach(dev);
15607 	}
15608 
15609 resume_exit:
15610 	rtnl_unlock();
15611 	bnxt_ulp_start(bp, rc);
15612 	if (!rc)
15613 		bnxt_reenable_sriov(bp);
15614 	return rc;
15615 }
15616 
15617 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
15618 #define BNXT_PM_OPS (&bnxt_pm_ops)
15619 
15620 #else
15621 
15622 #define BNXT_PM_OPS NULL
15623 
15624 #endif /* CONFIG_PM_SLEEP */
15625 
15626 /**
15627  * bnxt_io_error_detected - called when PCI error is detected
15628  * @pdev: Pointer to PCI device
15629  * @state: The current pci connection state
15630  *
15631  * This function is called after a PCI bus error affecting
15632  * this device has been detected.
15633  */
15634 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
15635 					       pci_channel_state_t state)
15636 {
15637 	struct net_device *netdev = pci_get_drvdata(pdev);
15638 	struct bnxt *bp = netdev_priv(netdev);
15639 	bool abort = false;
15640 
15641 	netdev_info(netdev, "PCI I/O error detected\n");
15642 
15643 	bnxt_ulp_stop(bp);
15644 
15645 	rtnl_lock();
15646 	netif_device_detach(netdev);
15647 
15648 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
15649 		netdev_err(bp->dev, "Firmware reset already in progress\n");
15650 		abort = true;
15651 	}
15652 
15653 	if (abort || state == pci_channel_io_perm_failure) {
15654 		rtnl_unlock();
15655 		return PCI_ERS_RESULT_DISCONNECT;
15656 	}
15657 
15658 	/* Link is not reliable anymore if state is pci_channel_io_frozen
15659 	 * so we disable bus master to prevent any potential bad DMAs before
15660 	 * freeing kernel memory.
15661 	 */
15662 	if (state == pci_channel_io_frozen) {
15663 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
15664 		bnxt_fw_fatal_close(bp);
15665 	}
15666 
15667 	if (netif_running(netdev))
15668 		__bnxt_close_nic(bp, true, true);
15669 
15670 	if (pci_is_enabled(pdev))
15671 		pci_disable_device(pdev);
15672 	bnxt_free_ctx_mem(bp);
15673 	rtnl_unlock();
15674 
15675 	/* Request a slot slot reset. */
15676 	return PCI_ERS_RESULT_NEED_RESET;
15677 }
15678 
15679 /**
15680  * bnxt_io_slot_reset - called after the pci bus has been reset.
15681  * @pdev: Pointer to PCI device
15682  *
15683  * Restart the card from scratch, as if from a cold-boot.
15684  * At this point, the card has exprienced a hard reset,
15685  * followed by fixups by BIOS, and has its config space
15686  * set up identically to what it was at cold boot.
15687  */
15688 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
15689 {
15690 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
15691 	struct net_device *netdev = pci_get_drvdata(pdev);
15692 	struct bnxt *bp = netdev_priv(netdev);
15693 	int retry = 0;
15694 	int err = 0;
15695 	int off;
15696 
15697 	netdev_info(bp->dev, "PCI Slot Reset\n");
15698 
15699 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
15700 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
15701 		msleep(900);
15702 
15703 	rtnl_lock();
15704 
15705 	if (pci_enable_device(pdev)) {
15706 		dev_err(&pdev->dev,
15707 			"Cannot re-enable PCI device after reset.\n");
15708 	} else {
15709 		pci_set_master(pdev);
15710 		/* Upon fatal error, our device internal logic that latches to
15711 		 * BAR value is getting reset and will restore only upon
15712 		 * rewritting the BARs.
15713 		 *
15714 		 * As pci_restore_state() does not re-write the BARs if the
15715 		 * value is same as saved value earlier, driver needs to
15716 		 * write the BARs to 0 to force restore, in case of fatal error.
15717 		 */
15718 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
15719 				       &bp->state)) {
15720 			for (off = PCI_BASE_ADDRESS_0;
15721 			     off <= PCI_BASE_ADDRESS_5; off += 4)
15722 				pci_write_config_dword(bp->pdev, off, 0);
15723 		}
15724 		pci_restore_state(pdev);
15725 		pci_save_state(pdev);
15726 
15727 		bnxt_inv_fw_health_reg(bp);
15728 		bnxt_try_map_fw_health_reg(bp);
15729 
15730 		/* In some PCIe AER scenarios, firmware may take up to
15731 		 * 10 seconds to become ready in the worst case.
15732 		 */
15733 		do {
15734 			err = bnxt_try_recover_fw(bp);
15735 			if (!err)
15736 				break;
15737 			retry++;
15738 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
15739 
15740 		if (err) {
15741 			dev_err(&pdev->dev, "Firmware not ready\n");
15742 			goto reset_exit;
15743 		}
15744 
15745 		err = bnxt_hwrm_func_reset(bp);
15746 		if (!err)
15747 			result = PCI_ERS_RESULT_RECOVERED;
15748 
15749 		bnxt_ulp_irq_stop(bp);
15750 		bnxt_clear_int_mode(bp);
15751 		err = bnxt_init_int_mode(bp);
15752 		bnxt_ulp_irq_restart(bp, err);
15753 	}
15754 
15755 reset_exit:
15756 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15757 	bnxt_clear_reservations(bp, true);
15758 	rtnl_unlock();
15759 
15760 	return result;
15761 }
15762 
15763 /**
15764  * bnxt_io_resume - called when traffic can start flowing again.
15765  * @pdev: Pointer to PCI device
15766  *
15767  * This callback is called when the error recovery driver tells
15768  * us that its OK to resume normal operation.
15769  */
15770 static void bnxt_io_resume(struct pci_dev *pdev)
15771 {
15772 	struct net_device *netdev = pci_get_drvdata(pdev);
15773 	struct bnxt *bp = netdev_priv(netdev);
15774 	int err;
15775 
15776 	netdev_info(bp->dev, "PCI Slot Resume\n");
15777 	rtnl_lock();
15778 
15779 	err = bnxt_hwrm_func_qcaps(bp);
15780 	if (!err && netif_running(netdev))
15781 		err = bnxt_open(netdev);
15782 
15783 	if (!err)
15784 		netif_device_attach(netdev);
15785 
15786 	rtnl_unlock();
15787 	bnxt_ulp_start(bp, err);
15788 	if (!err)
15789 		bnxt_reenable_sriov(bp);
15790 }
15791 
15792 static const struct pci_error_handlers bnxt_err_handler = {
15793 	.error_detected	= bnxt_io_error_detected,
15794 	.slot_reset	= bnxt_io_slot_reset,
15795 	.resume		= bnxt_io_resume
15796 };
15797 
15798 static struct pci_driver bnxt_pci_driver = {
15799 	.name		= DRV_MODULE_NAME,
15800 	.id_table	= bnxt_pci_tbl,
15801 	.probe		= bnxt_init_one,
15802 	.remove		= bnxt_remove_one,
15803 	.shutdown	= bnxt_shutdown,
15804 	.driver.pm	= BNXT_PM_OPS,
15805 	.err_handler	= &bnxt_err_handler,
15806 #if defined(CONFIG_BNXT_SRIOV)
15807 	.sriov_configure = bnxt_sriov_configure,
15808 #endif
15809 };
15810 
15811 static int __init bnxt_init(void)
15812 {
15813 	int err;
15814 
15815 	bnxt_debug_init();
15816 	err = pci_register_driver(&bnxt_pci_driver);
15817 	if (err) {
15818 		bnxt_debug_exit();
15819 		return err;
15820 	}
15821 
15822 	return 0;
15823 }
15824 
15825 static void __exit bnxt_exit(void)
15826 {
15827 	pci_unregister_driver(&bnxt_pci_driver);
15828 	if (bnxt_pf_wq)
15829 		destroy_workqueue(bnxt_pf_wq);
15830 	bnxt_debug_exit();
15831 }
15832 
15833 module_init(bnxt_init);
15834 module_exit(bnxt_exit);
15835