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