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