xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision 2aceb896ee18ae35b21b14c978d8c2ef8c7b439d)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  * Copyright (c) 2016-2019 Broadcom Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  */
10 
11 #include <linux/module.h>
12 
13 #include <linux/stringify.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/pci.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/bitops.h>
27 #include <linux/io.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/byteorder.h>
31 #include <asm/page.h>
32 #include <linux/time.h>
33 #include <linux/mii.h>
34 #include <linux/mdio.h>
35 #include <linux/if.h>
36 #include <linux/if_vlan.h>
37 #include <linux/if_bridge.h>
38 #include <linux/rtc.h>
39 #include <linux/bpf.h>
40 #include <net/gro.h>
41 #include <net/ip.h>
42 #include <net/tcp.h>
43 #include <net/udp.h>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
46 #include <net/udp_tunnel.h>
47 #include <linux/workqueue.h>
48 #include <linux/prefetch.h>
49 #include <linux/cache.h>
50 #include <linux/log2.h>
51 #include <linux/bitmap.h>
52 #include <linux/cpu_rmap.h>
53 #include <linux/cpumask.h>
54 #include <net/pkt_cls.h>
55 #include <net/page_pool/helpers.h>
56 #include <linux/align.h>
57 #include <net/netdev_queues.h>
58 
59 #include "bnxt_hsi.h"
60 #include "bnxt.h"
61 #include "bnxt_hwrm.h"
62 #include "bnxt_ulp.h"
63 #include "bnxt_sriov.h"
64 #include "bnxt_ethtool.h"
65 #include "bnxt_dcb.h"
66 #include "bnxt_xdp.h"
67 #include "bnxt_ptp.h"
68 #include "bnxt_vfr.h"
69 #include "bnxt_tc.h"
70 #include "bnxt_devlink.h"
71 #include "bnxt_debugfs.h"
72 #include "bnxt_hwmon.h"
73 
74 #define BNXT_TX_TIMEOUT		(5 * HZ)
75 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
76 				 NETIF_MSG_TX_ERR)
77 
78 MODULE_LICENSE("GPL");
79 MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
80 
81 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
82 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
83 #define BNXT_RX_COPY_THRESH 256
84 
85 #define BNXT_TX_PUSH_THRESH 164
86 
87 /* indexed by enum board_idx */
88 static const struct {
89 	char *name;
90 } board_info[] = {
91 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
92 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
93 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
94 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
95 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
96 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
97 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
98 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
99 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
100 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
101 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
102 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
103 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
104 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
105 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
106 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
108 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
109 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
110 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
111 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
112 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
113 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
114 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
115 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
116 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
117 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
118 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
119 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
120 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
121 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
122 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
123 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
124 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
125 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
126 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
127 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
128 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
129 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
130 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
131 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
132 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
133 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
134 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
135 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
136 };
137 
138 static const struct pci_device_id bnxt_pci_tbl[] = {
139 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
140 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
141 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
142 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
143 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
144 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
145 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
146 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
147 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
148 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
149 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
150 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
151 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
152 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
153 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
154 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
155 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
159 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
160 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
162 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
163 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
164 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
165 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
166 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
167 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
168 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
169 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
170 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
171 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
172 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
173 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
174 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
175 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
176 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
177 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
178 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
179 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
180 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
181 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
182 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
183 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
184 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
185 #ifdef CONFIG_BNXT_SRIOV
186 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
187 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
188 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
189 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
190 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
191 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
192 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
193 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
194 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
195 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
196 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
197 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
198 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
199 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
200 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
201 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
202 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
203 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
204 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
205 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
206 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
207 #endif
208 	{ 0 }
209 };
210 
211 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
212 
213 static const u16 bnxt_vf_req_snif[] = {
214 	HWRM_FUNC_CFG,
215 	HWRM_FUNC_VF_CFG,
216 	HWRM_PORT_PHY_QCFG,
217 	HWRM_CFA_L2_FILTER_ALLOC,
218 };
219 
220 static const u16 bnxt_async_events_arr[] = {
221 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
222 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
223 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
224 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
225 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
226 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
227 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
228 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
229 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
230 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
231 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
232 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
233 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
234 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
235 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
236 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
237 };
238 
239 static struct workqueue_struct *bnxt_pf_wq;
240 
241 static bool bnxt_vf_pciid(enum board_idx idx)
242 {
243 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
244 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
245 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
246 		idx == NETXTREME_E_P5_VF_HV);
247 }
248 
249 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
250 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
251 #define DB_CP_IRQ_DIS_FLAGS	(DB_KEY_CP | DB_IRQ_DIS)
252 
253 #define BNXT_CP_DB_IRQ_DIS(db)						\
254 		writel(DB_CP_IRQ_DIS_FLAGS, db)
255 
256 #define BNXT_DB_CQ(db, idx)						\
257 	writel(DB_CP_FLAGS | RING_CMP(idx), (db)->doorbell)
258 
259 #define BNXT_DB_NQ_P5(db, idx)						\
260 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | RING_CMP(idx),	\
261 		    (db)->doorbell)
262 
263 #define BNXT_DB_CQ_ARM(db, idx)						\
264 	writel(DB_CP_REARM_FLAGS | RING_CMP(idx), (db)->doorbell)
265 
266 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
267 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM | RING_CMP(idx),\
268 		    (db)->doorbell)
269 
270 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
271 {
272 	if (bp->flags & BNXT_FLAG_CHIP_P5)
273 		BNXT_DB_NQ_P5(db, idx);
274 	else
275 		BNXT_DB_CQ(db, idx);
276 }
277 
278 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
279 {
280 	if (bp->flags & BNXT_FLAG_CHIP_P5)
281 		BNXT_DB_NQ_ARM_P5(db, idx);
282 	else
283 		BNXT_DB_CQ_ARM(db, idx);
284 }
285 
286 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
287 {
288 	if (bp->flags & BNXT_FLAG_CHIP_P5)
289 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
290 			    RING_CMP(idx), db->doorbell);
291 	else
292 		BNXT_DB_CQ(db, idx);
293 }
294 
295 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
296 {
297 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
298 		return;
299 
300 	if (BNXT_PF(bp))
301 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
302 	else
303 		schedule_delayed_work(&bp->fw_reset_task, delay);
304 }
305 
306 static void __bnxt_queue_sp_work(struct bnxt *bp)
307 {
308 	if (BNXT_PF(bp))
309 		queue_work(bnxt_pf_wq, &bp->sp_task);
310 	else
311 		schedule_work(&bp->sp_task);
312 }
313 
314 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
315 {
316 	set_bit(event, &bp->sp_event);
317 	__bnxt_queue_sp_work(bp);
318 }
319 
320 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
321 {
322 	if (!rxr->bnapi->in_reset) {
323 		rxr->bnapi->in_reset = true;
324 		if (bp->flags & BNXT_FLAG_CHIP_P5)
325 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
326 		else
327 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
328 		__bnxt_queue_sp_work(bp);
329 	}
330 	rxr->rx_next_cons = 0xffff;
331 }
332 
333 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
334 			  int idx)
335 {
336 	struct bnxt_napi *bnapi = txr->bnapi;
337 
338 	if (bnapi->tx_fault)
339 		return;
340 
341 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_pkts:%d cons:%u prod:%u i:%d)",
342 		   txr->txq_index, bnapi->tx_pkts,
343 		   txr->tx_cons, txr->tx_prod, idx);
344 	WARN_ON_ONCE(1);
345 	bnapi->tx_fault = 1;
346 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
347 }
348 
349 const u16 bnxt_lhint_arr[] = {
350 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
351 	TX_BD_FLAGS_LHINT_512_TO_1023,
352 	TX_BD_FLAGS_LHINT_1024_TO_2047,
353 	TX_BD_FLAGS_LHINT_1024_TO_2047,
354 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
355 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
356 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
357 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
358 	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 };
370 
371 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
372 {
373 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
374 
375 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
376 		return 0;
377 
378 	return md_dst->u.port_info.port_id;
379 }
380 
381 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
382 			     u16 prod)
383 {
384 	bnxt_db_write(bp, &txr->tx_db, prod);
385 	txr->kick_pending = 0;
386 }
387 
388 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
389 {
390 	struct bnxt *bp = netdev_priv(dev);
391 	struct tx_bd *txbd;
392 	struct tx_bd_ext *txbd1;
393 	struct netdev_queue *txq;
394 	int i;
395 	dma_addr_t mapping;
396 	unsigned int length, pad = 0;
397 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
398 	u16 prod, last_frag;
399 	struct pci_dev *pdev = bp->pdev;
400 	struct bnxt_tx_ring_info *txr;
401 	struct bnxt_sw_tx_bd *tx_buf;
402 	__le32 lflags = 0;
403 
404 	i = skb_get_queue_mapping(skb);
405 	if (unlikely(i >= bp->tx_nr_rings)) {
406 		dev_kfree_skb_any(skb);
407 		dev_core_stats_tx_dropped_inc(dev);
408 		return NETDEV_TX_OK;
409 	}
410 
411 	txq = netdev_get_tx_queue(dev, i);
412 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
413 	prod = txr->tx_prod;
414 
415 	free_size = bnxt_tx_avail(bp, txr);
416 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
417 		/* We must have raced with NAPI cleanup */
418 		if (net_ratelimit() && txr->kick_pending)
419 			netif_warn(bp, tx_err, dev,
420 				   "bnxt: ring busy w/ flush pending!\n");
421 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
422 					bp->tx_wake_thresh))
423 			return NETDEV_TX_BUSY;
424 	}
425 
426 	if (unlikely(ipv6_hopopt_jumbo_remove(skb)))
427 		goto tx_free;
428 
429 	length = skb->len;
430 	len = skb_headlen(skb);
431 	last_frag = skb_shinfo(skb)->nr_frags;
432 
433 	txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
434 
435 	txbd->tx_bd_opaque = prod;
436 
437 	tx_buf = &txr->tx_buf_ring[prod];
438 	tx_buf->skb = skb;
439 	tx_buf->nr_frags = last_frag;
440 
441 	vlan_tag_flags = 0;
442 	cfa_action = bnxt_xmit_get_cfa_action(skb);
443 	if (skb_vlan_tag_present(skb)) {
444 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
445 				 skb_vlan_tag_get(skb);
446 		/* Currently supports 8021Q, 8021AD vlan offloads
447 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
448 		 */
449 		if (skb->vlan_proto == htons(ETH_P_8021Q))
450 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
451 	}
452 
453 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
454 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
455 
456 		if (ptp && ptp->tx_tstamp_en && !skb_is_gso(skb) &&
457 		    atomic_dec_if_positive(&ptp->tx_avail) >= 0) {
458 			if (!bnxt_ptp_parse(skb, &ptp->tx_seqid,
459 					    &ptp->tx_hdr_off)) {
460 				if (vlan_tag_flags)
461 					ptp->tx_hdr_off += VLAN_HLEN;
462 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
463 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
464 			} else {
465 				atomic_inc(&bp->ptp_cfg->tx_avail);
466 			}
467 		}
468 	}
469 
470 	if (unlikely(skb->no_fcs))
471 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
472 
473 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
474 	    !lflags) {
475 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
476 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
477 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
478 		void __iomem *db = txr->tx_db.doorbell;
479 		void *pdata = tx_push_buf->data;
480 		u64 *end;
481 		int j, push_len;
482 
483 		/* Set COAL_NOW to be ready quickly for the next push */
484 		tx_push->tx_bd_len_flags_type =
485 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
486 					TX_BD_TYPE_LONG_TX_BD |
487 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
488 					TX_BD_FLAGS_COAL_NOW |
489 					TX_BD_FLAGS_PACKET_END |
490 					(2 << TX_BD_FLAGS_BD_CNT_SHIFT));
491 
492 		if (skb->ip_summed == CHECKSUM_PARTIAL)
493 			tx_push1->tx_bd_hsize_lflags =
494 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
495 		else
496 			tx_push1->tx_bd_hsize_lflags = 0;
497 
498 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
499 		tx_push1->tx_bd_cfa_action =
500 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
501 
502 		end = pdata + length;
503 		end = PTR_ALIGN(end, 8) - 1;
504 		*end = 0;
505 
506 		skb_copy_from_linear_data(skb, pdata, len);
507 		pdata += len;
508 		for (j = 0; j < last_frag; j++) {
509 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
510 			void *fptr;
511 
512 			fptr = skb_frag_address_safe(frag);
513 			if (!fptr)
514 				goto normal_tx;
515 
516 			memcpy(pdata, fptr, skb_frag_size(frag));
517 			pdata += skb_frag_size(frag);
518 		}
519 
520 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
521 		txbd->tx_bd_haddr = txr->data_mapping;
522 		prod = NEXT_TX(prod);
523 		txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
524 		memcpy(txbd, tx_push1, sizeof(*txbd));
525 		prod = NEXT_TX(prod);
526 		tx_push->doorbell =
527 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
528 		WRITE_ONCE(txr->tx_prod, prod);
529 
530 		tx_buf->is_push = 1;
531 		netdev_tx_sent_queue(txq, skb->len);
532 		wmb();	/* Sync is_push and byte queue before pushing data */
533 
534 		push_len = (length + sizeof(*tx_push) + 7) / 8;
535 		if (push_len > 16) {
536 			__iowrite64_copy(db, tx_push_buf, 16);
537 			__iowrite32_copy(db + 4, tx_push_buf + 1,
538 					 (push_len - 16) << 1);
539 		} else {
540 			__iowrite64_copy(db, tx_push_buf, push_len);
541 		}
542 
543 		goto tx_done;
544 	}
545 
546 normal_tx:
547 	if (length < BNXT_MIN_PKT_SIZE) {
548 		pad = BNXT_MIN_PKT_SIZE - length;
549 		if (skb_pad(skb, pad))
550 			/* SKB already freed. */
551 			goto tx_kick_pending;
552 		length = BNXT_MIN_PKT_SIZE;
553 	}
554 
555 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
556 
557 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
558 		goto tx_free;
559 
560 	dma_unmap_addr_set(tx_buf, mapping, mapping);
561 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
562 		((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
563 
564 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
565 
566 	prod = NEXT_TX(prod);
567 	txbd1 = (struct tx_bd_ext *)
568 		&txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
569 
570 	txbd1->tx_bd_hsize_lflags = lflags;
571 	if (skb_is_gso(skb)) {
572 		u32 hdr_len;
573 
574 		if (skb->encapsulation)
575 			hdr_len = skb_inner_tcp_all_headers(skb);
576 		else
577 			hdr_len = skb_tcp_all_headers(skb);
578 
579 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
580 					TX_BD_FLAGS_T_IPID |
581 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
582 		length = skb_shinfo(skb)->gso_size;
583 		txbd1->tx_bd_mss = cpu_to_le32(length);
584 		length += hdr_len;
585 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
586 		txbd1->tx_bd_hsize_lflags |=
587 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
588 		txbd1->tx_bd_mss = 0;
589 	}
590 
591 	length >>= 9;
592 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
593 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
594 				     skb->len);
595 		i = 0;
596 		goto tx_dma_error;
597 	}
598 	flags |= bnxt_lhint_arr[length];
599 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
600 
601 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
602 	txbd1->tx_bd_cfa_action =
603 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
604 	for (i = 0; i < last_frag; i++) {
605 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
606 
607 		prod = NEXT_TX(prod);
608 		txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
609 
610 		len = skb_frag_size(frag);
611 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
612 					   DMA_TO_DEVICE);
613 
614 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
615 			goto tx_dma_error;
616 
617 		tx_buf = &txr->tx_buf_ring[prod];
618 		dma_unmap_addr_set(tx_buf, mapping, mapping);
619 
620 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
621 
622 		flags = len << TX_BD_LEN_SHIFT;
623 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
624 	}
625 
626 	flags &= ~TX_BD_LEN;
627 	txbd->tx_bd_len_flags_type =
628 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
629 			    TX_BD_FLAGS_PACKET_END);
630 
631 	netdev_tx_sent_queue(txq, skb->len);
632 
633 	skb_tx_timestamp(skb);
634 
635 	/* Sync BD data before updating doorbell */
636 	wmb();
637 
638 	prod = NEXT_TX(prod);
639 	WRITE_ONCE(txr->tx_prod, prod);
640 
641 	if (!netdev_xmit_more() || netif_xmit_stopped(txq))
642 		bnxt_txr_db_kick(bp, txr, prod);
643 	else
644 		txr->kick_pending = 1;
645 
646 tx_done:
647 
648 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
649 		if (netdev_xmit_more() && !tx_buf->is_push)
650 			bnxt_txr_db_kick(bp, txr, prod);
651 
652 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
653 				   bp->tx_wake_thresh);
654 	}
655 	return NETDEV_TX_OK;
656 
657 tx_dma_error:
658 	if (BNXT_TX_PTP_IS_SET(lflags))
659 		atomic_inc(&bp->ptp_cfg->tx_avail);
660 
661 	last_frag = i;
662 
663 	/* start back at beginning and unmap skb */
664 	prod = txr->tx_prod;
665 	tx_buf = &txr->tx_buf_ring[prod];
666 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
667 			 skb_headlen(skb), DMA_TO_DEVICE);
668 	prod = NEXT_TX(prod);
669 
670 	/* unmap remaining mapped pages */
671 	for (i = 0; i < last_frag; i++) {
672 		prod = NEXT_TX(prod);
673 		tx_buf = &txr->tx_buf_ring[prod];
674 		dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
675 			       skb_frag_size(&skb_shinfo(skb)->frags[i]),
676 			       DMA_TO_DEVICE);
677 	}
678 
679 tx_free:
680 	dev_kfree_skb_any(skb);
681 tx_kick_pending:
682 	if (txr->kick_pending)
683 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
684 	txr->tx_buf_ring[txr->tx_prod].skb = NULL;
685 	dev_core_stats_tx_dropped_inc(dev);
686 	return NETDEV_TX_OK;
687 }
688 
689 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
690 {
691 	struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
692 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
693 	u16 cons = txr->tx_cons;
694 	struct pci_dev *pdev = bp->pdev;
695 	int nr_pkts = bnapi->tx_pkts;
696 	int i;
697 	unsigned int tx_bytes = 0;
698 
699 	for (i = 0; i < nr_pkts; i++) {
700 		struct bnxt_sw_tx_bd *tx_buf;
701 		struct sk_buff *skb;
702 		int j, last;
703 
704 		tx_buf = &txr->tx_buf_ring[cons];
705 		cons = NEXT_TX(cons);
706 		skb = tx_buf->skb;
707 		tx_buf->skb = NULL;
708 
709 		if (unlikely(!skb)) {
710 			bnxt_sched_reset_txr(bp, txr, i);
711 			return;
712 		}
713 
714 		tx_bytes += skb->len;
715 
716 		if (tx_buf->is_push) {
717 			tx_buf->is_push = 0;
718 			goto next_tx_int;
719 		}
720 
721 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
722 				 skb_headlen(skb), DMA_TO_DEVICE);
723 		last = tx_buf->nr_frags;
724 
725 		for (j = 0; j < last; j++) {
726 			cons = NEXT_TX(cons);
727 			tx_buf = &txr->tx_buf_ring[cons];
728 			dma_unmap_page(
729 				&pdev->dev,
730 				dma_unmap_addr(tx_buf, mapping),
731 				skb_frag_size(&skb_shinfo(skb)->frags[j]),
732 				DMA_TO_DEVICE);
733 		}
734 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
735 			if (bp->flags & BNXT_FLAG_CHIP_P5) {
736 				/* PTP worker takes ownership of the skb */
737 				if (!bnxt_get_tx_ts_p5(bp, skb))
738 					skb = NULL;
739 				else
740 					atomic_inc(&bp->ptp_cfg->tx_avail);
741 			}
742 		}
743 
744 next_tx_int:
745 		cons = NEXT_TX(cons);
746 
747 		dev_consume_skb_any(skb);
748 	}
749 
750 	bnapi->tx_pkts = 0;
751 	WRITE_ONCE(txr->tx_cons, cons);
752 
753 	__netif_txq_completed_wake(txq, nr_pkts, tx_bytes,
754 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
755 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
756 }
757 
758 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
759 					 struct bnxt_rx_ring_info *rxr,
760 					 unsigned int *offset,
761 					 gfp_t gfp)
762 {
763 	struct page *page;
764 
765 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
766 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
767 						BNXT_RX_PAGE_SIZE);
768 	} else {
769 		page = page_pool_dev_alloc_pages(rxr->page_pool);
770 		*offset = 0;
771 	}
772 	if (!page)
773 		return NULL;
774 
775 	*mapping = page_pool_get_dma_addr(page) + *offset;
776 	return page;
777 }
778 
779 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
780 				       gfp_t gfp)
781 {
782 	u8 *data;
783 	struct pci_dev *pdev = bp->pdev;
784 
785 	if (gfp == GFP_ATOMIC)
786 		data = napi_alloc_frag(bp->rx_buf_size);
787 	else
788 		data = netdev_alloc_frag(bp->rx_buf_size);
789 	if (!data)
790 		return NULL;
791 
792 	*mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
793 					bp->rx_buf_use_size, bp->rx_dir,
794 					DMA_ATTR_WEAK_ORDERING);
795 
796 	if (dma_mapping_error(&pdev->dev, *mapping)) {
797 		skb_free_frag(data);
798 		data = NULL;
799 	}
800 	return data;
801 }
802 
803 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
804 		       u16 prod, gfp_t gfp)
805 {
806 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
807 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
808 	dma_addr_t mapping;
809 
810 	if (BNXT_RX_PAGE_MODE(bp)) {
811 		unsigned int offset;
812 		struct page *page =
813 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
814 
815 		if (!page)
816 			return -ENOMEM;
817 
818 		mapping += bp->rx_dma_offset;
819 		rx_buf->data = page;
820 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
821 	} else {
822 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, gfp);
823 
824 		if (!data)
825 			return -ENOMEM;
826 
827 		rx_buf->data = data;
828 		rx_buf->data_ptr = data + bp->rx_offset;
829 	}
830 	rx_buf->mapping = mapping;
831 
832 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
833 	return 0;
834 }
835 
836 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
837 {
838 	u16 prod = rxr->rx_prod;
839 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
840 	struct rx_bd *cons_bd, *prod_bd;
841 
842 	prod_rx_buf = &rxr->rx_buf_ring[prod];
843 	cons_rx_buf = &rxr->rx_buf_ring[cons];
844 
845 	prod_rx_buf->data = data;
846 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
847 
848 	prod_rx_buf->mapping = cons_rx_buf->mapping;
849 
850 	prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
851 	cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
852 
853 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
854 }
855 
856 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
857 {
858 	u16 next, max = rxr->rx_agg_bmap_size;
859 
860 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
861 	if (next >= max)
862 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
863 	return next;
864 }
865 
866 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
867 				     struct bnxt_rx_ring_info *rxr,
868 				     u16 prod, gfp_t gfp)
869 {
870 	struct rx_bd *rxbd =
871 		&rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
872 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
873 	struct page *page;
874 	dma_addr_t mapping;
875 	u16 sw_prod = rxr->rx_sw_agg_prod;
876 	unsigned int offset = 0;
877 
878 	page = __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
879 
880 	if (!page)
881 		return -ENOMEM;
882 
883 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
884 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
885 
886 	__set_bit(sw_prod, rxr->rx_agg_bmap);
887 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
888 	rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
889 
890 	rx_agg_buf->page = page;
891 	rx_agg_buf->offset = offset;
892 	rx_agg_buf->mapping = mapping;
893 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
894 	rxbd->rx_bd_opaque = sw_prod;
895 	return 0;
896 }
897 
898 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
899 				       struct bnxt_cp_ring_info *cpr,
900 				       u16 cp_cons, u16 curr)
901 {
902 	struct rx_agg_cmp *agg;
903 
904 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
905 	agg = (struct rx_agg_cmp *)
906 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
907 	return agg;
908 }
909 
910 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
911 					      struct bnxt_rx_ring_info *rxr,
912 					      u16 agg_id, u16 curr)
913 {
914 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
915 
916 	return &tpa_info->agg_arr[curr];
917 }
918 
919 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
920 				   u16 start, u32 agg_bufs, bool tpa)
921 {
922 	struct bnxt_napi *bnapi = cpr->bnapi;
923 	struct bnxt *bp = bnapi->bp;
924 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
925 	u16 prod = rxr->rx_agg_prod;
926 	u16 sw_prod = rxr->rx_sw_agg_prod;
927 	bool p5_tpa = false;
928 	u32 i;
929 
930 	if ((bp->flags & BNXT_FLAG_CHIP_P5) && tpa)
931 		p5_tpa = true;
932 
933 	for (i = 0; i < agg_bufs; i++) {
934 		u16 cons;
935 		struct rx_agg_cmp *agg;
936 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
937 		struct rx_bd *prod_bd;
938 		struct page *page;
939 
940 		if (p5_tpa)
941 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
942 		else
943 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
944 		cons = agg->rx_agg_cmp_opaque;
945 		__clear_bit(cons, rxr->rx_agg_bmap);
946 
947 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
948 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
949 
950 		__set_bit(sw_prod, rxr->rx_agg_bmap);
951 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
952 		cons_rx_buf = &rxr->rx_agg_ring[cons];
953 
954 		/* It is possible for sw_prod to be equal to cons, so
955 		 * set cons_rx_buf->page to NULL first.
956 		 */
957 		page = cons_rx_buf->page;
958 		cons_rx_buf->page = NULL;
959 		prod_rx_buf->page = page;
960 		prod_rx_buf->offset = cons_rx_buf->offset;
961 
962 		prod_rx_buf->mapping = cons_rx_buf->mapping;
963 
964 		prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
965 
966 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
967 		prod_bd->rx_bd_opaque = sw_prod;
968 
969 		prod = NEXT_RX_AGG(prod);
970 		sw_prod = NEXT_RX_AGG(sw_prod);
971 	}
972 	rxr->rx_agg_prod = prod;
973 	rxr->rx_sw_agg_prod = sw_prod;
974 }
975 
976 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
977 					      struct bnxt_rx_ring_info *rxr,
978 					      u16 cons, void *data, u8 *data_ptr,
979 					      dma_addr_t dma_addr,
980 					      unsigned int offset_and_len)
981 {
982 	unsigned int len = offset_and_len & 0xffff;
983 	struct page *page = data;
984 	u16 prod = rxr->rx_prod;
985 	struct sk_buff *skb;
986 	int err;
987 
988 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
989 	if (unlikely(err)) {
990 		bnxt_reuse_rx_data(rxr, cons, data);
991 		return NULL;
992 	}
993 	dma_addr -= bp->rx_dma_offset;
994 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
995 				bp->rx_dir);
996 	skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE);
997 	if (!skb) {
998 		page_pool_recycle_direct(rxr->page_pool, page);
999 		return NULL;
1000 	}
1001 	skb_mark_for_recycle(skb);
1002 	skb_reserve(skb, bp->rx_offset);
1003 	__skb_put(skb, len);
1004 
1005 	return skb;
1006 }
1007 
1008 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1009 					struct bnxt_rx_ring_info *rxr,
1010 					u16 cons, void *data, u8 *data_ptr,
1011 					dma_addr_t dma_addr,
1012 					unsigned int offset_and_len)
1013 {
1014 	unsigned int payload = offset_and_len >> 16;
1015 	unsigned int len = offset_and_len & 0xffff;
1016 	skb_frag_t *frag;
1017 	struct page *page = data;
1018 	u16 prod = rxr->rx_prod;
1019 	struct sk_buff *skb;
1020 	int off, err;
1021 
1022 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1023 	if (unlikely(err)) {
1024 		bnxt_reuse_rx_data(rxr, cons, data);
1025 		return NULL;
1026 	}
1027 	dma_addr -= bp->rx_dma_offset;
1028 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1029 				bp->rx_dir);
1030 
1031 	if (unlikely(!payload))
1032 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1033 
1034 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1035 	if (!skb) {
1036 		page_pool_recycle_direct(rxr->page_pool, page);
1037 		return NULL;
1038 	}
1039 
1040 	skb_mark_for_recycle(skb);
1041 	off = (void *)data_ptr - page_address(page);
1042 	skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE);
1043 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1044 	       payload + NET_IP_ALIGN);
1045 
1046 	frag = &skb_shinfo(skb)->frags[0];
1047 	skb_frag_size_sub(frag, payload);
1048 	skb_frag_off_add(frag, payload);
1049 	skb->data_len -= payload;
1050 	skb->tail += payload;
1051 
1052 	return skb;
1053 }
1054 
1055 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1056 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1057 				   void *data, u8 *data_ptr,
1058 				   dma_addr_t dma_addr,
1059 				   unsigned int offset_and_len)
1060 {
1061 	u16 prod = rxr->rx_prod;
1062 	struct sk_buff *skb;
1063 	int err;
1064 
1065 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1066 	if (unlikely(err)) {
1067 		bnxt_reuse_rx_data(rxr, cons, data);
1068 		return NULL;
1069 	}
1070 
1071 	skb = napi_build_skb(data, bp->rx_buf_size);
1072 	dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1073 			       bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
1074 	if (!skb) {
1075 		skb_free_frag(data);
1076 		return NULL;
1077 	}
1078 
1079 	skb_reserve(skb, bp->rx_offset);
1080 	skb_put(skb, offset_and_len & 0xffff);
1081 	return skb;
1082 }
1083 
1084 static u32 __bnxt_rx_agg_pages(struct bnxt *bp,
1085 			       struct bnxt_cp_ring_info *cpr,
1086 			       struct skb_shared_info *shinfo,
1087 			       u16 idx, u32 agg_bufs, bool tpa,
1088 			       struct xdp_buff *xdp)
1089 {
1090 	struct bnxt_napi *bnapi = cpr->bnapi;
1091 	struct pci_dev *pdev = bp->pdev;
1092 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1093 	u16 prod = rxr->rx_agg_prod;
1094 	u32 i, total_frag_len = 0;
1095 	bool p5_tpa = false;
1096 
1097 	if ((bp->flags & BNXT_FLAG_CHIP_P5) && tpa)
1098 		p5_tpa = true;
1099 
1100 	for (i = 0; i < agg_bufs; i++) {
1101 		skb_frag_t *frag = &shinfo->frags[i];
1102 		u16 cons, frag_len;
1103 		struct rx_agg_cmp *agg;
1104 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1105 		struct page *page;
1106 		dma_addr_t mapping;
1107 
1108 		if (p5_tpa)
1109 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1110 		else
1111 			agg = bnxt_get_agg(bp, cpr, idx, i);
1112 		cons = agg->rx_agg_cmp_opaque;
1113 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1114 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1115 
1116 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1117 		skb_frag_fill_page_desc(frag, cons_rx_buf->page,
1118 					cons_rx_buf->offset, frag_len);
1119 		shinfo->nr_frags = i + 1;
1120 		__clear_bit(cons, rxr->rx_agg_bmap);
1121 
1122 		/* It is possible for bnxt_alloc_rx_page() to allocate
1123 		 * a sw_prod index that equals the cons index, so we
1124 		 * need to clear the cons entry now.
1125 		 */
1126 		mapping = cons_rx_buf->mapping;
1127 		page = cons_rx_buf->page;
1128 		cons_rx_buf->page = NULL;
1129 
1130 		if (xdp && page_is_pfmemalloc(page))
1131 			xdp_buff_set_frag_pfmemalloc(xdp);
1132 
1133 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1134 			--shinfo->nr_frags;
1135 			cons_rx_buf->page = page;
1136 
1137 			/* Update prod since possibly some pages have been
1138 			 * allocated already.
1139 			 */
1140 			rxr->rx_agg_prod = prod;
1141 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1142 			return 0;
1143 		}
1144 
1145 		dma_sync_single_for_cpu(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1146 					bp->rx_dir);
1147 
1148 		total_frag_len += frag_len;
1149 		prod = NEXT_RX_AGG(prod);
1150 	}
1151 	rxr->rx_agg_prod = prod;
1152 	return total_frag_len;
1153 }
1154 
1155 static struct sk_buff *bnxt_rx_agg_pages_skb(struct bnxt *bp,
1156 					     struct bnxt_cp_ring_info *cpr,
1157 					     struct sk_buff *skb, u16 idx,
1158 					     u32 agg_bufs, bool tpa)
1159 {
1160 	struct skb_shared_info *shinfo = skb_shinfo(skb);
1161 	u32 total_frag_len = 0;
1162 
1163 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo, idx,
1164 					     agg_bufs, tpa, NULL);
1165 	if (!total_frag_len) {
1166 		skb_mark_for_recycle(skb);
1167 		dev_kfree_skb(skb);
1168 		return NULL;
1169 	}
1170 
1171 	skb->data_len += total_frag_len;
1172 	skb->len += total_frag_len;
1173 	skb->truesize += BNXT_RX_PAGE_SIZE * agg_bufs;
1174 	return skb;
1175 }
1176 
1177 static u32 bnxt_rx_agg_pages_xdp(struct bnxt *bp,
1178 				 struct bnxt_cp_ring_info *cpr,
1179 				 struct xdp_buff *xdp, u16 idx,
1180 				 u32 agg_bufs, bool tpa)
1181 {
1182 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1183 	u32 total_frag_len = 0;
1184 
1185 	if (!xdp_buff_has_frags(xdp))
1186 		shinfo->nr_frags = 0;
1187 
1188 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo,
1189 					     idx, agg_bufs, tpa, xdp);
1190 	if (total_frag_len) {
1191 		xdp_buff_set_frags_flag(xdp);
1192 		shinfo->nr_frags = agg_bufs;
1193 		shinfo->xdp_frags_size = total_frag_len;
1194 	}
1195 	return total_frag_len;
1196 }
1197 
1198 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1199 			       u8 agg_bufs, u32 *raw_cons)
1200 {
1201 	u16 last;
1202 	struct rx_agg_cmp *agg;
1203 
1204 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1205 	last = RING_CMP(*raw_cons);
1206 	agg = (struct rx_agg_cmp *)
1207 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1208 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1209 }
1210 
1211 static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1212 					    unsigned int len,
1213 					    dma_addr_t mapping)
1214 {
1215 	struct bnxt *bp = bnapi->bp;
1216 	struct pci_dev *pdev = bp->pdev;
1217 	struct sk_buff *skb;
1218 
1219 	skb = napi_alloc_skb(&bnapi->napi, len);
1220 	if (!skb)
1221 		return NULL;
1222 
1223 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1224 				bp->rx_dir);
1225 
1226 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1227 	       len + NET_IP_ALIGN);
1228 
1229 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1230 				   bp->rx_dir);
1231 
1232 	skb_put(skb, len);
1233 	return skb;
1234 }
1235 
1236 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1237 			   u32 *raw_cons, void *cmp)
1238 {
1239 	struct rx_cmp *rxcmp = cmp;
1240 	u32 tmp_raw_cons = *raw_cons;
1241 	u8 cmp_type, agg_bufs = 0;
1242 
1243 	cmp_type = RX_CMP_TYPE(rxcmp);
1244 
1245 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1246 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1247 			    RX_CMP_AGG_BUFS) >>
1248 			   RX_CMP_AGG_BUFS_SHIFT;
1249 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1250 		struct rx_tpa_end_cmp *tpa_end = cmp;
1251 
1252 		if (bp->flags & BNXT_FLAG_CHIP_P5)
1253 			return 0;
1254 
1255 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1256 	}
1257 
1258 	if (agg_bufs) {
1259 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1260 			return -EBUSY;
1261 	}
1262 	*raw_cons = tmp_raw_cons;
1263 	return 0;
1264 }
1265 
1266 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1267 {
1268 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1269 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1270 
1271 	if (test_bit(idx, map->agg_idx_bmap))
1272 		idx = find_first_zero_bit(map->agg_idx_bmap,
1273 					  BNXT_AGG_IDX_BMAP_SIZE);
1274 	__set_bit(idx, map->agg_idx_bmap);
1275 	map->agg_id_tbl[agg_id] = idx;
1276 	return idx;
1277 }
1278 
1279 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1280 {
1281 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1282 
1283 	__clear_bit(idx, map->agg_idx_bmap);
1284 }
1285 
1286 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1287 {
1288 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1289 
1290 	return map->agg_id_tbl[agg_id];
1291 }
1292 
1293 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1294 			   struct rx_tpa_start_cmp *tpa_start,
1295 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1296 {
1297 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1298 	struct bnxt_tpa_info *tpa_info;
1299 	u16 cons, prod, agg_id;
1300 	struct rx_bd *prod_bd;
1301 	dma_addr_t mapping;
1302 
1303 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
1304 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1305 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1306 	} else {
1307 		agg_id = TPA_START_AGG_ID(tpa_start);
1308 	}
1309 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1310 	prod = rxr->rx_prod;
1311 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1312 	prod_rx_buf = &rxr->rx_buf_ring[prod];
1313 	tpa_info = &rxr->rx_tpa[agg_id];
1314 
1315 	if (unlikely(cons != rxr->rx_next_cons ||
1316 		     TPA_START_ERROR(tpa_start))) {
1317 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1318 			    cons, rxr->rx_next_cons,
1319 			    TPA_START_ERROR_CODE(tpa_start1));
1320 		bnxt_sched_reset_rxr(bp, rxr);
1321 		return;
1322 	}
1323 	/* Store cfa_code in tpa_info to use in tpa_end
1324 	 * completion processing.
1325 	 */
1326 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1327 	prod_rx_buf->data = tpa_info->data;
1328 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1329 
1330 	mapping = tpa_info->mapping;
1331 	prod_rx_buf->mapping = mapping;
1332 
1333 	prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1334 
1335 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1336 
1337 	tpa_info->data = cons_rx_buf->data;
1338 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1339 	cons_rx_buf->data = NULL;
1340 	tpa_info->mapping = cons_rx_buf->mapping;
1341 
1342 	tpa_info->len =
1343 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1344 				RX_TPA_START_CMP_LEN_SHIFT;
1345 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1346 		u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1347 
1348 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1349 		tpa_info->gso_type = SKB_GSO_TCPV4;
1350 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1351 		if (hash_type == 3 || TPA_START_IS_IPV6(tpa_start1))
1352 			tpa_info->gso_type = SKB_GSO_TCPV6;
1353 		tpa_info->rss_hash =
1354 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1355 	} else {
1356 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1357 		tpa_info->gso_type = 0;
1358 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1359 	}
1360 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1361 	tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1362 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1363 	tpa_info->agg_count = 0;
1364 
1365 	rxr->rx_prod = NEXT_RX(prod);
1366 	cons = NEXT_RX(cons);
1367 	rxr->rx_next_cons = NEXT_RX(cons);
1368 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1369 
1370 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1371 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1372 	cons_rx_buf->data = NULL;
1373 }
1374 
1375 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1376 {
1377 	if (agg_bufs)
1378 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1379 }
1380 
1381 #ifdef CONFIG_INET
1382 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1383 {
1384 	struct udphdr *uh = NULL;
1385 
1386 	if (ip_proto == htons(ETH_P_IP)) {
1387 		struct iphdr *iph = (struct iphdr *)skb->data;
1388 
1389 		if (iph->protocol == IPPROTO_UDP)
1390 			uh = (struct udphdr *)(iph + 1);
1391 	} else {
1392 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1393 
1394 		if (iph->nexthdr == IPPROTO_UDP)
1395 			uh = (struct udphdr *)(iph + 1);
1396 	}
1397 	if (uh) {
1398 		if (uh->check)
1399 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1400 		else
1401 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1402 	}
1403 }
1404 #endif
1405 
1406 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1407 					   int payload_off, int tcp_ts,
1408 					   struct sk_buff *skb)
1409 {
1410 #ifdef CONFIG_INET
1411 	struct tcphdr *th;
1412 	int len, nw_off;
1413 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1414 	u32 hdr_info = tpa_info->hdr_info;
1415 	bool loopback = false;
1416 
1417 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1418 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1419 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1420 
1421 	/* If the packet is an internal loopback packet, the offsets will
1422 	 * have an extra 4 bytes.
1423 	 */
1424 	if (inner_mac_off == 4) {
1425 		loopback = true;
1426 	} else if (inner_mac_off > 4) {
1427 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1428 					    ETH_HLEN - 2));
1429 
1430 		/* We only support inner iPv4/ipv6.  If we don't see the
1431 		 * correct protocol ID, it must be a loopback packet where
1432 		 * the offsets are off by 4.
1433 		 */
1434 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1435 			loopback = true;
1436 	}
1437 	if (loopback) {
1438 		/* internal loopback packet, subtract all offsets by 4 */
1439 		inner_ip_off -= 4;
1440 		inner_mac_off -= 4;
1441 		outer_ip_off -= 4;
1442 	}
1443 
1444 	nw_off = inner_ip_off - ETH_HLEN;
1445 	skb_set_network_header(skb, nw_off);
1446 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1447 		struct ipv6hdr *iph = ipv6_hdr(skb);
1448 
1449 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1450 		len = skb->len - skb_transport_offset(skb);
1451 		th = tcp_hdr(skb);
1452 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1453 	} else {
1454 		struct iphdr *iph = ip_hdr(skb);
1455 
1456 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1457 		len = skb->len - skb_transport_offset(skb);
1458 		th = tcp_hdr(skb);
1459 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1460 	}
1461 
1462 	if (inner_mac_off) { /* tunnel */
1463 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1464 					    ETH_HLEN - 2));
1465 
1466 		bnxt_gro_tunnel(skb, proto);
1467 	}
1468 #endif
1469 	return skb;
1470 }
1471 
1472 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1473 					   int payload_off, int tcp_ts,
1474 					   struct sk_buff *skb)
1475 {
1476 #ifdef CONFIG_INET
1477 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1478 	u32 hdr_info = tpa_info->hdr_info;
1479 	int iphdr_len, nw_off;
1480 
1481 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1482 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1483 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1484 
1485 	nw_off = inner_ip_off - ETH_HLEN;
1486 	skb_set_network_header(skb, nw_off);
1487 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1488 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1489 	skb_set_transport_header(skb, nw_off + iphdr_len);
1490 
1491 	if (inner_mac_off) { /* tunnel */
1492 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1493 					    ETH_HLEN - 2));
1494 
1495 		bnxt_gro_tunnel(skb, proto);
1496 	}
1497 #endif
1498 	return skb;
1499 }
1500 
1501 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1502 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1503 
1504 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1505 					   int payload_off, int tcp_ts,
1506 					   struct sk_buff *skb)
1507 {
1508 #ifdef CONFIG_INET
1509 	struct tcphdr *th;
1510 	int len, nw_off, tcp_opt_len = 0;
1511 
1512 	if (tcp_ts)
1513 		tcp_opt_len = 12;
1514 
1515 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1516 		struct iphdr *iph;
1517 
1518 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1519 			 ETH_HLEN;
1520 		skb_set_network_header(skb, nw_off);
1521 		iph = ip_hdr(skb);
1522 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1523 		len = skb->len - skb_transport_offset(skb);
1524 		th = tcp_hdr(skb);
1525 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1526 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1527 		struct ipv6hdr *iph;
1528 
1529 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1530 			 ETH_HLEN;
1531 		skb_set_network_header(skb, nw_off);
1532 		iph = ipv6_hdr(skb);
1533 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1534 		len = skb->len - skb_transport_offset(skb);
1535 		th = tcp_hdr(skb);
1536 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1537 	} else {
1538 		dev_kfree_skb_any(skb);
1539 		return NULL;
1540 	}
1541 
1542 	if (nw_off) /* tunnel */
1543 		bnxt_gro_tunnel(skb, skb->protocol);
1544 #endif
1545 	return skb;
1546 }
1547 
1548 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1549 					   struct bnxt_tpa_info *tpa_info,
1550 					   struct rx_tpa_end_cmp *tpa_end,
1551 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1552 					   struct sk_buff *skb)
1553 {
1554 #ifdef CONFIG_INET
1555 	int payload_off;
1556 	u16 segs;
1557 
1558 	segs = TPA_END_TPA_SEGS(tpa_end);
1559 	if (segs == 1)
1560 		return skb;
1561 
1562 	NAPI_GRO_CB(skb)->count = segs;
1563 	skb_shinfo(skb)->gso_size =
1564 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1565 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1566 	if (bp->flags & BNXT_FLAG_CHIP_P5)
1567 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1568 	else
1569 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1570 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1571 	if (likely(skb))
1572 		tcp_gro_complete(skb);
1573 #endif
1574 	return skb;
1575 }
1576 
1577 /* Given the cfa_code of a received packet determine which
1578  * netdev (vf-rep or PF) the packet is destined to.
1579  */
1580 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1581 {
1582 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1583 
1584 	/* if vf-rep dev is NULL, the must belongs to the PF */
1585 	return dev ? dev : bp->dev;
1586 }
1587 
1588 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1589 					   struct bnxt_cp_ring_info *cpr,
1590 					   u32 *raw_cons,
1591 					   struct rx_tpa_end_cmp *tpa_end,
1592 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1593 					   u8 *event)
1594 {
1595 	struct bnxt_napi *bnapi = cpr->bnapi;
1596 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1597 	u8 *data_ptr, agg_bufs;
1598 	unsigned int len;
1599 	struct bnxt_tpa_info *tpa_info;
1600 	dma_addr_t mapping;
1601 	struct sk_buff *skb;
1602 	u16 idx = 0, agg_id;
1603 	void *data;
1604 	bool gro;
1605 
1606 	if (unlikely(bnapi->in_reset)) {
1607 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1608 
1609 		if (rc < 0)
1610 			return ERR_PTR(-EBUSY);
1611 		return NULL;
1612 	}
1613 
1614 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
1615 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1616 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1617 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1618 		tpa_info = &rxr->rx_tpa[agg_id];
1619 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1620 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1621 				    agg_bufs, tpa_info->agg_count);
1622 			agg_bufs = tpa_info->agg_count;
1623 		}
1624 		tpa_info->agg_count = 0;
1625 		*event |= BNXT_AGG_EVENT;
1626 		bnxt_free_agg_idx(rxr, agg_id);
1627 		idx = agg_id;
1628 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1629 	} else {
1630 		agg_id = TPA_END_AGG_ID(tpa_end);
1631 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1632 		tpa_info = &rxr->rx_tpa[agg_id];
1633 		idx = RING_CMP(*raw_cons);
1634 		if (agg_bufs) {
1635 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1636 				return ERR_PTR(-EBUSY);
1637 
1638 			*event |= BNXT_AGG_EVENT;
1639 			idx = NEXT_CMP(idx);
1640 		}
1641 		gro = !!TPA_END_GRO(tpa_end);
1642 	}
1643 	data = tpa_info->data;
1644 	data_ptr = tpa_info->data_ptr;
1645 	prefetch(data_ptr);
1646 	len = tpa_info->len;
1647 	mapping = tpa_info->mapping;
1648 
1649 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1650 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1651 		if (agg_bufs > MAX_SKB_FRAGS)
1652 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1653 				    agg_bufs, (int)MAX_SKB_FRAGS);
1654 		return NULL;
1655 	}
1656 
1657 	if (len <= bp->rx_copy_thresh) {
1658 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1659 		if (!skb) {
1660 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1661 			cpr->sw_stats.rx.rx_oom_discards += 1;
1662 			return NULL;
1663 		}
1664 	} else {
1665 		u8 *new_data;
1666 		dma_addr_t new_mapping;
1667 
1668 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, GFP_ATOMIC);
1669 		if (!new_data) {
1670 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1671 			cpr->sw_stats.rx.rx_oom_discards += 1;
1672 			return NULL;
1673 		}
1674 
1675 		tpa_info->data = new_data;
1676 		tpa_info->data_ptr = new_data + bp->rx_offset;
1677 		tpa_info->mapping = new_mapping;
1678 
1679 		skb = napi_build_skb(data, bp->rx_buf_size);
1680 		dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1681 				       bp->rx_buf_use_size, bp->rx_dir,
1682 				       DMA_ATTR_WEAK_ORDERING);
1683 
1684 		if (!skb) {
1685 			skb_free_frag(data);
1686 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1687 			cpr->sw_stats.rx.rx_oom_discards += 1;
1688 			return NULL;
1689 		}
1690 		skb_reserve(skb, bp->rx_offset);
1691 		skb_put(skb, len);
1692 	}
1693 
1694 	if (agg_bufs) {
1695 		skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, idx, agg_bufs, true);
1696 		if (!skb) {
1697 			/* Page reuse already handled by bnxt_rx_pages(). */
1698 			cpr->sw_stats.rx.rx_oom_discards += 1;
1699 			return NULL;
1700 		}
1701 	}
1702 
1703 	skb->protocol =
1704 		eth_type_trans(skb, bnxt_get_pkt_dev(bp, tpa_info->cfa_code));
1705 
1706 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1707 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1708 
1709 	if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1710 	    (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1711 		__be16 vlan_proto = htons(tpa_info->metadata >>
1712 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1713 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1714 
1715 		if (eth_type_vlan(vlan_proto)) {
1716 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1717 		} else {
1718 			dev_kfree_skb(skb);
1719 			return NULL;
1720 		}
1721 	}
1722 
1723 	skb_checksum_none_assert(skb);
1724 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1725 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1726 		skb->csum_level =
1727 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1728 	}
1729 
1730 	if (gro)
1731 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1732 
1733 	return skb;
1734 }
1735 
1736 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1737 			 struct rx_agg_cmp *rx_agg)
1738 {
1739 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1740 	struct bnxt_tpa_info *tpa_info;
1741 
1742 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1743 	tpa_info = &rxr->rx_tpa[agg_id];
1744 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1745 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1746 }
1747 
1748 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1749 			     struct sk_buff *skb)
1750 {
1751 	if (skb->dev != bp->dev) {
1752 		/* this packet belongs to a vf-rep */
1753 		bnxt_vf_rep_rx(bp, skb);
1754 		return;
1755 	}
1756 	skb_record_rx_queue(skb, bnapi->index);
1757 	skb_mark_for_recycle(skb);
1758 	napi_gro_receive(&bnapi->napi, skb);
1759 }
1760 
1761 /* returns the following:
1762  * 1       - 1 packet successfully received
1763  * 0       - successful TPA_START, packet not completed yet
1764  * -EBUSY  - completion ring does not have all the agg buffers yet
1765  * -ENOMEM - packet aborted due to out of memory
1766  * -EIO    - packet aborted due to hw error indicated in BD
1767  */
1768 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1769 		       u32 *raw_cons, u8 *event)
1770 {
1771 	struct bnxt_napi *bnapi = cpr->bnapi;
1772 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1773 	struct net_device *dev = bp->dev;
1774 	struct rx_cmp *rxcmp;
1775 	struct rx_cmp_ext *rxcmp1;
1776 	u32 tmp_raw_cons = *raw_cons;
1777 	u16 cfa_code, cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1778 	struct bnxt_sw_rx_bd *rx_buf;
1779 	unsigned int len;
1780 	u8 *data_ptr, agg_bufs, cmp_type;
1781 	bool xdp_active = false;
1782 	dma_addr_t dma_addr;
1783 	struct sk_buff *skb;
1784 	struct xdp_buff xdp;
1785 	u32 flags, misc;
1786 	void *data;
1787 	int rc = 0;
1788 
1789 	rxcmp = (struct rx_cmp *)
1790 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1791 
1792 	cmp_type = RX_CMP_TYPE(rxcmp);
1793 
1794 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
1795 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
1796 		goto next_rx_no_prod_no_len;
1797 	}
1798 
1799 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1800 	cp_cons = RING_CMP(tmp_raw_cons);
1801 	rxcmp1 = (struct rx_cmp_ext *)
1802 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1803 
1804 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1805 		return -EBUSY;
1806 
1807 	/* The valid test of the entry must be done first before
1808 	 * reading any further.
1809 	 */
1810 	dma_rmb();
1811 	prod = rxr->rx_prod;
1812 
1813 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1814 		bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1815 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
1816 
1817 		*event |= BNXT_RX_EVENT;
1818 		goto next_rx_no_prod_no_len;
1819 
1820 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1821 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
1822 				   (struct rx_tpa_end_cmp *)rxcmp,
1823 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
1824 
1825 		if (IS_ERR(skb))
1826 			return -EBUSY;
1827 
1828 		rc = -ENOMEM;
1829 		if (likely(skb)) {
1830 			bnxt_deliver_skb(bp, bnapi, skb);
1831 			rc = 1;
1832 		}
1833 		*event |= BNXT_RX_EVENT;
1834 		goto next_rx_no_prod_no_len;
1835 	}
1836 
1837 	cons = rxcmp->rx_cmp_opaque;
1838 	if (unlikely(cons != rxr->rx_next_cons)) {
1839 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
1840 
1841 		/* 0xffff is forced error, don't print it */
1842 		if (rxr->rx_next_cons != 0xffff)
1843 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
1844 				    cons, rxr->rx_next_cons);
1845 		bnxt_sched_reset_rxr(bp, rxr);
1846 		if (rc1)
1847 			return rc1;
1848 		goto next_rx_no_prod_no_len;
1849 	}
1850 	rx_buf = &rxr->rx_buf_ring[cons];
1851 	data = rx_buf->data;
1852 	data_ptr = rx_buf->data_ptr;
1853 	prefetch(data_ptr);
1854 
1855 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1856 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
1857 
1858 	if (agg_bufs) {
1859 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1860 			return -EBUSY;
1861 
1862 		cp_cons = NEXT_CMP(cp_cons);
1863 		*event |= BNXT_AGG_EVENT;
1864 	}
1865 	*event |= BNXT_RX_EVENT;
1866 
1867 	rx_buf->data = NULL;
1868 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1869 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
1870 
1871 		bnxt_reuse_rx_data(rxr, cons, data);
1872 		if (agg_bufs)
1873 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
1874 					       false);
1875 
1876 		rc = -EIO;
1877 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
1878 			bnapi->cp_ring.sw_stats.rx.rx_buf_errors++;
1879 			if (!(bp->flags & BNXT_FLAG_CHIP_P5) &&
1880 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
1881 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
1882 						 rx_err);
1883 				bnxt_sched_reset_rxr(bp, rxr);
1884 			}
1885 		}
1886 		goto next_rx_no_len;
1887 	}
1888 
1889 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
1890 	len = flags >> RX_CMP_LEN_SHIFT;
1891 	dma_addr = rx_buf->mapping;
1892 
1893 	if (bnxt_xdp_attached(bp, rxr)) {
1894 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
1895 		if (agg_bufs) {
1896 			u32 frag_len = bnxt_rx_agg_pages_xdp(bp, cpr, &xdp,
1897 							     cp_cons, agg_bufs,
1898 							     false);
1899 			if (!frag_len) {
1900 				cpr->sw_stats.rx.rx_oom_discards += 1;
1901 				rc = -ENOMEM;
1902 				goto next_rx;
1903 			}
1904 		}
1905 		xdp_active = true;
1906 	}
1907 
1908 	if (xdp_active) {
1909 		if (bnxt_rx_xdp(bp, rxr, cons, xdp, data, &data_ptr, &len, event)) {
1910 			rc = 1;
1911 			goto next_rx;
1912 		}
1913 	}
1914 
1915 	if (len <= bp->rx_copy_thresh) {
1916 		skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
1917 		bnxt_reuse_rx_data(rxr, cons, data);
1918 		if (!skb) {
1919 			if (agg_bufs) {
1920 				if (!xdp_active)
1921 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
1922 							       agg_bufs, false);
1923 				else
1924 					bnxt_xdp_buff_frags_free(rxr, &xdp);
1925 			}
1926 			cpr->sw_stats.rx.rx_oom_discards += 1;
1927 			rc = -ENOMEM;
1928 			goto next_rx;
1929 		}
1930 	} else {
1931 		u32 payload;
1932 
1933 		if (rx_buf->data_ptr == data_ptr)
1934 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
1935 		else
1936 			payload = 0;
1937 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
1938 				      payload | len);
1939 		if (!skb) {
1940 			cpr->sw_stats.rx.rx_oom_discards += 1;
1941 			rc = -ENOMEM;
1942 			goto next_rx;
1943 		}
1944 	}
1945 
1946 	if (agg_bufs) {
1947 		if (!xdp_active) {
1948 			skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, cp_cons, agg_bufs, false);
1949 			if (!skb) {
1950 				cpr->sw_stats.rx.rx_oom_discards += 1;
1951 				rc = -ENOMEM;
1952 				goto next_rx;
1953 			}
1954 		} else {
1955 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, rxr->page_pool, &xdp, rxcmp1);
1956 			if (!skb) {
1957 				/* we should be able to free the old skb here */
1958 				bnxt_xdp_buff_frags_free(rxr, &xdp);
1959 				cpr->sw_stats.rx.rx_oom_discards += 1;
1960 				rc = -ENOMEM;
1961 				goto next_rx;
1962 			}
1963 		}
1964 	}
1965 
1966 	if (RX_CMP_HASH_VALID(rxcmp)) {
1967 		u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1968 		enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1969 
1970 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1971 		if (hash_type != 1 && hash_type != 3)
1972 			type = PKT_HASH_TYPE_L3;
1973 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1974 	}
1975 
1976 	cfa_code = RX_CMP_CFA_CODE(rxcmp1);
1977 	skb->protocol = eth_type_trans(skb, bnxt_get_pkt_dev(bp, cfa_code));
1978 
1979 	if ((rxcmp1->rx_cmp_flags2 &
1980 	     cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1981 	    (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1982 		u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1983 		u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1984 		__be16 vlan_proto = htons(meta_data >>
1985 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1986 
1987 		if (eth_type_vlan(vlan_proto)) {
1988 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1989 		} else {
1990 			dev_kfree_skb(skb);
1991 			goto next_rx;
1992 		}
1993 	}
1994 
1995 	skb_checksum_none_assert(skb);
1996 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
1997 		if (dev->features & NETIF_F_RXCSUM) {
1998 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1999 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2000 		}
2001 	} else {
2002 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2003 			if (dev->features & NETIF_F_RXCSUM)
2004 				bnapi->cp_ring.sw_stats.rx.rx_l4_csum_errors++;
2005 		}
2006 	}
2007 
2008 	if (unlikely((flags & RX_CMP_FLAGS_ITYPES_MASK) ==
2009 		     RX_CMP_FLAGS_ITYPE_PTP_W_TS) || bp->ptp_all_rx_tstamp) {
2010 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
2011 			u32 cmpl_ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
2012 			u64 ns, ts;
2013 
2014 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2015 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2016 
2017 				spin_lock_bh(&ptp->ptp_lock);
2018 				ns = timecounter_cyc2time(&ptp->tc, ts);
2019 				spin_unlock_bh(&ptp->ptp_lock);
2020 				memset(skb_hwtstamps(skb), 0,
2021 				       sizeof(*skb_hwtstamps(skb)));
2022 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2023 			}
2024 		}
2025 	}
2026 	bnxt_deliver_skb(bp, bnapi, skb);
2027 	rc = 1;
2028 
2029 next_rx:
2030 	cpr->rx_packets += 1;
2031 	cpr->rx_bytes += len;
2032 
2033 next_rx_no_len:
2034 	rxr->rx_prod = NEXT_RX(prod);
2035 	rxr->rx_next_cons = NEXT_RX(cons);
2036 
2037 next_rx_no_prod_no_len:
2038 	*raw_cons = tmp_raw_cons;
2039 
2040 	return rc;
2041 }
2042 
2043 /* In netpoll mode, if we are using a combined completion ring, we need to
2044  * discard the rx packets and recycle the buffers.
2045  */
2046 static int bnxt_force_rx_discard(struct bnxt *bp,
2047 				 struct bnxt_cp_ring_info *cpr,
2048 				 u32 *raw_cons, u8 *event)
2049 {
2050 	u32 tmp_raw_cons = *raw_cons;
2051 	struct rx_cmp_ext *rxcmp1;
2052 	struct rx_cmp *rxcmp;
2053 	u16 cp_cons;
2054 	u8 cmp_type;
2055 	int rc;
2056 
2057 	cp_cons = RING_CMP(tmp_raw_cons);
2058 	rxcmp = (struct rx_cmp *)
2059 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2060 
2061 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2062 	cp_cons = RING_CMP(tmp_raw_cons);
2063 	rxcmp1 = (struct rx_cmp_ext *)
2064 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2065 
2066 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2067 		return -EBUSY;
2068 
2069 	/* The valid test of the entry must be done first before
2070 	 * reading any further.
2071 	 */
2072 	dma_rmb();
2073 	cmp_type = RX_CMP_TYPE(rxcmp);
2074 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
2075 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2076 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2077 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2078 		struct rx_tpa_end_cmp_ext *tpa_end1;
2079 
2080 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2081 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2082 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2083 	}
2084 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2085 	if (rc && rc != -EBUSY)
2086 		cpr->sw_stats.rx.rx_netpoll_discards += 1;
2087 	return rc;
2088 }
2089 
2090 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2091 {
2092 	struct bnxt_fw_health *fw_health = bp->fw_health;
2093 	u32 reg = fw_health->regs[reg_idx];
2094 	u32 reg_type, reg_off, val = 0;
2095 
2096 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2097 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2098 	switch (reg_type) {
2099 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2100 		pci_read_config_dword(bp->pdev, reg_off, &val);
2101 		break;
2102 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2103 		reg_off = fw_health->mapped_regs[reg_idx];
2104 		fallthrough;
2105 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2106 		val = readl(bp->bar0 + reg_off);
2107 		break;
2108 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2109 		val = readl(bp->bar1 + reg_off);
2110 		break;
2111 	}
2112 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2113 		val &= fw_health->fw_reset_inprog_reg_mask;
2114 	return val;
2115 }
2116 
2117 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2118 {
2119 	int i;
2120 
2121 	for (i = 0; i < bp->rx_nr_rings; i++) {
2122 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2123 		struct bnxt_ring_grp_info *grp_info;
2124 
2125 		grp_info = &bp->grp_info[grp_idx];
2126 		if (grp_info->agg_fw_ring_id == ring_id)
2127 			return grp_idx;
2128 	}
2129 	return INVALID_HW_RING_ID;
2130 }
2131 
2132 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2133 {
2134 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2135 		return link_info->force_pam4_link_speed;
2136 	return link_info->force_link_speed;
2137 }
2138 
2139 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2140 {
2141 	link_info->req_link_speed = link_info->force_link_speed;
2142 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2143 	if (link_info->force_pam4_link_speed) {
2144 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2145 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2146 	}
2147 }
2148 
2149 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2150 {
2151 	link_info->advertising = link_info->auto_link_speeds;
2152 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2153 }
2154 
2155 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2156 {
2157 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2158 	    link_info->req_link_speed != link_info->force_link_speed)
2159 		return true;
2160 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2161 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2162 		return true;
2163 	return false;
2164 }
2165 
2166 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2167 {
2168 	if (link_info->advertising != link_info->auto_link_speeds ||
2169 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2170 		return true;
2171 	return false;
2172 }
2173 
2174 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2175 	((data2) &							\
2176 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2177 
2178 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2179 	(((data2) &							\
2180 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2181 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2182 
2183 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2184 	((data1) &							\
2185 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2186 
2187 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2188 	(((data1) &							\
2189 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2190 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2191 
2192 /* Return true if the workqueue has to be scheduled */
2193 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2194 {
2195 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2196 
2197 	switch (err_type) {
2198 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2199 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2200 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2201 		break;
2202 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2203 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2204 		break;
2205 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2206 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2207 		break;
2208 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2209 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2210 		char *threshold_type;
2211 		bool notify = false;
2212 		char *dir_str;
2213 
2214 		switch (type) {
2215 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2216 			threshold_type = "warning";
2217 			break;
2218 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2219 			threshold_type = "critical";
2220 			break;
2221 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2222 			threshold_type = "fatal";
2223 			break;
2224 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2225 			threshold_type = "shutdown";
2226 			break;
2227 		default:
2228 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2229 			return false;
2230 		}
2231 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2232 			dir_str = "above";
2233 			notify = true;
2234 		} else {
2235 			dir_str = "below";
2236 		}
2237 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2238 			    dir_str, threshold_type);
2239 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2240 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2241 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2242 		if (notify) {
2243 			bp->thermal_threshold_type = type;
2244 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2245 			return true;
2246 		}
2247 		return false;
2248 	}
2249 	default:
2250 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2251 			   err_type);
2252 		break;
2253 	}
2254 	return false;
2255 }
2256 
2257 #define BNXT_GET_EVENT_PORT(data)	\
2258 	((data) &			\
2259 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2260 
2261 #define BNXT_EVENT_RING_TYPE(data2)	\
2262 	((data2) &			\
2263 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2264 
2265 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2266 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2267 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2268 
2269 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2270 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2271 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2272 
2273 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2274 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2275 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2276 
2277 #define BNXT_PHC_BITS	48
2278 
2279 static int bnxt_async_event_process(struct bnxt *bp,
2280 				    struct hwrm_async_event_cmpl *cmpl)
2281 {
2282 	u16 event_id = le16_to_cpu(cmpl->event_id);
2283 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2284 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2285 
2286 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2287 		   event_id, data1, data2);
2288 
2289 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2290 	switch (event_id) {
2291 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2292 		struct bnxt_link_info *link_info = &bp->link_info;
2293 
2294 		if (BNXT_VF(bp))
2295 			goto async_event_process_exit;
2296 
2297 		/* print unsupported speed warning in forced speed mode only */
2298 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2299 		    (data1 & 0x20000)) {
2300 			u16 fw_speed = bnxt_get_force_speed(link_info);
2301 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2302 
2303 			if (speed != SPEED_UNKNOWN)
2304 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2305 					    speed);
2306 		}
2307 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2308 	}
2309 		fallthrough;
2310 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2311 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2312 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2313 		fallthrough;
2314 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2315 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2316 		break;
2317 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2318 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2319 		break;
2320 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2321 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2322 
2323 		if (BNXT_VF(bp))
2324 			break;
2325 
2326 		if (bp->pf.port_id != port_id)
2327 			break;
2328 
2329 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2330 		break;
2331 	}
2332 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2333 		if (BNXT_PF(bp))
2334 			goto async_event_process_exit;
2335 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2336 		break;
2337 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2338 		char *type_str = "Solicited";
2339 
2340 		if (!bp->fw_health)
2341 			goto async_event_process_exit;
2342 
2343 		bp->fw_reset_timestamp = jiffies;
2344 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2345 		if (!bp->fw_reset_min_dsecs)
2346 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2347 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2348 		if (!bp->fw_reset_max_dsecs)
2349 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2350 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2351 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2352 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2353 			type_str = "Fatal";
2354 			bp->fw_health->fatalities++;
2355 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2356 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2357 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2358 			type_str = "Non-fatal";
2359 			bp->fw_health->survivals++;
2360 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2361 		}
2362 		netif_warn(bp, hw, bp->dev,
2363 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2364 			   type_str, data1, data2,
2365 			   bp->fw_reset_min_dsecs * 100,
2366 			   bp->fw_reset_max_dsecs * 100);
2367 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2368 		break;
2369 	}
2370 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2371 		struct bnxt_fw_health *fw_health = bp->fw_health;
2372 		char *status_desc = "healthy";
2373 		u32 status;
2374 
2375 		if (!fw_health)
2376 			goto async_event_process_exit;
2377 
2378 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2379 			fw_health->enabled = false;
2380 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2381 			break;
2382 		}
2383 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2384 		fw_health->tmr_multiplier =
2385 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2386 				     bp->current_interval * 10);
2387 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2388 		if (!fw_health->enabled)
2389 			fw_health->last_fw_heartbeat =
2390 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2391 		fw_health->last_fw_reset_cnt =
2392 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2393 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2394 		if (status != BNXT_FW_STATUS_HEALTHY)
2395 			status_desc = "unhealthy";
2396 		netif_info(bp, drv, bp->dev,
2397 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2398 			   fw_health->primary ? "primary" : "backup", status,
2399 			   status_desc, fw_health->last_fw_reset_cnt);
2400 		if (!fw_health->enabled) {
2401 			/* Make sure tmr_counter is set and visible to
2402 			 * bnxt_health_check() before setting enabled to true.
2403 			 */
2404 			smp_wmb();
2405 			fw_health->enabled = true;
2406 		}
2407 		goto async_event_process_exit;
2408 	}
2409 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2410 		netif_notice(bp, hw, bp->dev,
2411 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2412 			     data1, data2);
2413 		goto async_event_process_exit;
2414 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2415 		struct bnxt_rx_ring_info *rxr;
2416 		u16 grp_idx;
2417 
2418 		if (bp->flags & BNXT_FLAG_CHIP_P5)
2419 			goto async_event_process_exit;
2420 
2421 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2422 			    BNXT_EVENT_RING_TYPE(data2), data1);
2423 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2424 			goto async_event_process_exit;
2425 
2426 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2427 		if (grp_idx == INVALID_HW_RING_ID) {
2428 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2429 				    data1);
2430 			goto async_event_process_exit;
2431 		}
2432 		rxr = bp->bnapi[grp_idx]->rx_ring;
2433 		bnxt_sched_reset_rxr(bp, rxr);
2434 		goto async_event_process_exit;
2435 	}
2436 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2437 		struct bnxt_fw_health *fw_health = bp->fw_health;
2438 
2439 		netif_notice(bp, hw, bp->dev,
2440 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2441 			     data1, data2);
2442 		if (fw_health) {
2443 			fw_health->echo_req_data1 = data1;
2444 			fw_health->echo_req_data2 = data2;
2445 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2446 			break;
2447 		}
2448 		goto async_event_process_exit;
2449 	}
2450 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2451 		bnxt_ptp_pps_event(bp, data1, data2);
2452 		goto async_event_process_exit;
2453 	}
2454 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2455 		if (bnxt_event_error_report(bp, data1, data2))
2456 			break;
2457 		goto async_event_process_exit;
2458 	}
2459 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2460 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2461 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2462 			if (BNXT_PTP_USE_RTC(bp)) {
2463 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2464 				u64 ns;
2465 
2466 				if (!ptp)
2467 					goto async_event_process_exit;
2468 
2469 				spin_lock_bh(&ptp->ptp_lock);
2470 				bnxt_ptp_update_current_time(bp);
2471 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2472 				       BNXT_PHC_BITS) | ptp->current_time);
2473 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2474 				spin_unlock_bh(&ptp->ptp_lock);
2475 			}
2476 			break;
2477 		}
2478 		goto async_event_process_exit;
2479 	}
2480 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2481 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2482 
2483 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2484 		goto async_event_process_exit;
2485 	}
2486 	default:
2487 		goto async_event_process_exit;
2488 	}
2489 	__bnxt_queue_sp_work(bp);
2490 async_event_process_exit:
2491 	return 0;
2492 }
2493 
2494 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2495 {
2496 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2497 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2498 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2499 				(struct hwrm_fwd_req_cmpl *)txcmp;
2500 
2501 	switch (cmpl_type) {
2502 	case CMPL_BASE_TYPE_HWRM_DONE:
2503 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2504 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2505 		break;
2506 
2507 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2508 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2509 
2510 		if ((vf_id < bp->pf.first_vf_id) ||
2511 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2512 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2513 				   vf_id);
2514 			return -EINVAL;
2515 		}
2516 
2517 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2518 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2519 		break;
2520 
2521 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2522 		bnxt_async_event_process(bp,
2523 					 (struct hwrm_async_event_cmpl *)txcmp);
2524 		break;
2525 
2526 	default:
2527 		break;
2528 	}
2529 
2530 	return 0;
2531 }
2532 
2533 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2534 {
2535 	struct bnxt_napi *bnapi = dev_instance;
2536 	struct bnxt *bp = bnapi->bp;
2537 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2538 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2539 
2540 	cpr->event_ctr++;
2541 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2542 	napi_schedule(&bnapi->napi);
2543 	return IRQ_HANDLED;
2544 }
2545 
2546 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2547 {
2548 	u32 raw_cons = cpr->cp_raw_cons;
2549 	u16 cons = RING_CMP(raw_cons);
2550 	struct tx_cmp *txcmp;
2551 
2552 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2553 
2554 	return TX_CMP_VALID(txcmp, raw_cons);
2555 }
2556 
2557 static irqreturn_t bnxt_inta(int irq, void *dev_instance)
2558 {
2559 	struct bnxt_napi *bnapi = dev_instance;
2560 	struct bnxt *bp = bnapi->bp;
2561 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2562 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2563 	u32 int_status;
2564 
2565 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2566 
2567 	if (!bnxt_has_work(bp, cpr)) {
2568 		int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
2569 		/* return if erroneous interrupt */
2570 		if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
2571 			return IRQ_NONE;
2572 	}
2573 
2574 	/* disable ring IRQ */
2575 	BNXT_CP_DB_IRQ_DIS(cpr->cp_db.doorbell);
2576 
2577 	/* Return here if interrupt is shared and is disabled. */
2578 	if (unlikely(atomic_read(&bp->intr_sem) != 0))
2579 		return IRQ_HANDLED;
2580 
2581 	napi_schedule(&bnapi->napi);
2582 	return IRQ_HANDLED;
2583 }
2584 
2585 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2586 			    int budget)
2587 {
2588 	struct bnxt_napi *bnapi = cpr->bnapi;
2589 	u32 raw_cons = cpr->cp_raw_cons;
2590 	u32 cons;
2591 	int tx_pkts = 0;
2592 	int rx_pkts = 0;
2593 	u8 event = 0;
2594 	struct tx_cmp *txcmp;
2595 
2596 	cpr->has_more_work = 0;
2597 	cpr->had_work_done = 1;
2598 	while (1) {
2599 		int rc;
2600 
2601 		cons = RING_CMP(raw_cons);
2602 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2603 
2604 		if (!TX_CMP_VALID(txcmp, raw_cons))
2605 			break;
2606 
2607 		/* The valid test of the entry must be done first before
2608 		 * reading any further.
2609 		 */
2610 		dma_rmb();
2611 		if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
2612 			tx_pkts++;
2613 			/* return full budget so NAPI will complete. */
2614 			if (unlikely(tx_pkts >= bp->tx_wake_thresh)) {
2615 				rx_pkts = budget;
2616 				raw_cons = NEXT_RAW_CMP(raw_cons);
2617 				if (budget)
2618 					cpr->has_more_work = 1;
2619 				break;
2620 			}
2621 		} else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
2622 			if (likely(budget))
2623 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2624 			else
2625 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2626 							   &event);
2627 			if (likely(rc >= 0))
2628 				rx_pkts += rc;
2629 			/* Increment rx_pkts when rc is -ENOMEM to count towards
2630 			 * the NAPI budget.  Otherwise, we may potentially loop
2631 			 * here forever if we consistently cannot allocate
2632 			 * buffers.
2633 			 */
2634 			else if (rc == -ENOMEM && budget)
2635 				rx_pkts++;
2636 			else if (rc == -EBUSY)	/* partial completion */
2637 				break;
2638 		} else if (unlikely((TX_CMP_TYPE(txcmp) ==
2639 				     CMPL_BASE_TYPE_HWRM_DONE) ||
2640 				    (TX_CMP_TYPE(txcmp) ==
2641 				     CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
2642 				    (TX_CMP_TYPE(txcmp) ==
2643 				     CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
2644 			bnxt_hwrm_handler(bp, txcmp);
2645 		}
2646 		raw_cons = NEXT_RAW_CMP(raw_cons);
2647 
2648 		if (rx_pkts && rx_pkts == budget) {
2649 			cpr->has_more_work = 1;
2650 			break;
2651 		}
2652 	}
2653 
2654 	if (event & BNXT_REDIRECT_EVENT)
2655 		xdp_do_flush();
2656 
2657 	if (event & BNXT_TX_EVENT) {
2658 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
2659 		u16 prod = txr->tx_prod;
2660 
2661 		/* Sync BD data before updating doorbell */
2662 		wmb();
2663 
2664 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
2665 	}
2666 
2667 	cpr->cp_raw_cons = raw_cons;
2668 	bnapi->tx_pkts += tx_pkts;
2669 	bnapi->events |= event;
2670 	return rx_pkts;
2671 }
2672 
2673 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
2674 				  int budget)
2675 {
2676 	if (bnapi->tx_pkts && !bnapi->tx_fault)
2677 		bnapi->tx_int(bp, bnapi, budget);
2678 
2679 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
2680 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2681 
2682 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2683 	}
2684 	if (bnapi->events & BNXT_AGG_EVENT) {
2685 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2686 
2687 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2688 	}
2689 	bnapi->events = 0;
2690 }
2691 
2692 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2693 			  int budget)
2694 {
2695 	struct bnxt_napi *bnapi = cpr->bnapi;
2696 	int rx_pkts;
2697 
2698 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
2699 
2700 	/* ACK completion ring before freeing tx ring and producing new
2701 	 * buffers in rx/agg rings to prevent overflowing the completion
2702 	 * ring.
2703 	 */
2704 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
2705 
2706 	__bnxt_poll_work_done(bp, bnapi, budget);
2707 	return rx_pkts;
2708 }
2709 
2710 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
2711 {
2712 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2713 	struct bnxt *bp = bnapi->bp;
2714 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2715 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2716 	struct tx_cmp *txcmp;
2717 	struct rx_cmp_ext *rxcmp1;
2718 	u32 cp_cons, tmp_raw_cons;
2719 	u32 raw_cons = cpr->cp_raw_cons;
2720 	bool flush_xdp = false;
2721 	u32 rx_pkts = 0;
2722 	u8 event = 0;
2723 
2724 	while (1) {
2725 		int rc;
2726 
2727 		cp_cons = RING_CMP(raw_cons);
2728 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2729 
2730 		if (!TX_CMP_VALID(txcmp, raw_cons))
2731 			break;
2732 
2733 		/* The valid test of the entry must be done first before
2734 		 * reading any further.
2735 		 */
2736 		dma_rmb();
2737 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
2738 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
2739 			cp_cons = RING_CMP(tmp_raw_cons);
2740 			rxcmp1 = (struct rx_cmp_ext *)
2741 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2742 
2743 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2744 				break;
2745 
2746 			/* force an error to recycle the buffer */
2747 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2748 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2749 
2750 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2751 			if (likely(rc == -EIO) && budget)
2752 				rx_pkts++;
2753 			else if (rc == -EBUSY)	/* partial completion */
2754 				break;
2755 			if (event & BNXT_REDIRECT_EVENT)
2756 				flush_xdp = true;
2757 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
2758 				    CMPL_BASE_TYPE_HWRM_DONE)) {
2759 			bnxt_hwrm_handler(bp, txcmp);
2760 		} else {
2761 			netdev_err(bp->dev,
2762 				   "Invalid completion received on special ring\n");
2763 		}
2764 		raw_cons = NEXT_RAW_CMP(raw_cons);
2765 
2766 		if (rx_pkts == budget)
2767 			break;
2768 	}
2769 
2770 	cpr->cp_raw_cons = raw_cons;
2771 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
2772 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2773 
2774 	if (event & BNXT_AGG_EVENT)
2775 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2776 	if (flush_xdp)
2777 		xdp_do_flush();
2778 
2779 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
2780 		napi_complete_done(napi, rx_pkts);
2781 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2782 	}
2783 	return rx_pkts;
2784 }
2785 
2786 static int bnxt_poll(struct napi_struct *napi, int budget)
2787 {
2788 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2789 	struct bnxt *bp = bnapi->bp;
2790 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2791 	int work_done = 0;
2792 
2793 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
2794 		napi_complete(napi);
2795 		return 0;
2796 	}
2797 	while (1) {
2798 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
2799 
2800 		if (work_done >= budget) {
2801 			if (!budget)
2802 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2803 			break;
2804 		}
2805 
2806 		if (!bnxt_has_work(bp, cpr)) {
2807 			if (napi_complete_done(napi, work_done))
2808 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2809 			break;
2810 		}
2811 	}
2812 	if (bp->flags & BNXT_FLAG_DIM) {
2813 		struct dim_sample dim_sample = {};
2814 
2815 		dim_update_sample(cpr->event_ctr,
2816 				  cpr->rx_packets,
2817 				  cpr->rx_bytes,
2818 				  &dim_sample);
2819 		net_dim(&cpr->dim, dim_sample);
2820 	}
2821 	return work_done;
2822 }
2823 
2824 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
2825 {
2826 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2827 	int i, work_done = 0;
2828 
2829 	for (i = 0; i < 2; i++) {
2830 		struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i];
2831 
2832 		if (cpr2) {
2833 			work_done += __bnxt_poll_work(bp, cpr2,
2834 						      budget - work_done);
2835 			cpr->has_more_work |= cpr2->has_more_work;
2836 		}
2837 	}
2838 	return work_done;
2839 }
2840 
2841 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
2842 				 u64 dbr_type, int budget)
2843 {
2844 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2845 	int i;
2846 
2847 	for (i = 0; i < 2; i++) {
2848 		struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i];
2849 		struct bnxt_db_info *db;
2850 
2851 		if (cpr2 && cpr2->had_work_done) {
2852 			db = &cpr2->cp_db;
2853 			bnxt_writeq(bp, db->db_key64 | dbr_type |
2854 				    RING_CMP(cpr2->cp_raw_cons), db->doorbell);
2855 			cpr2->had_work_done = 0;
2856 		}
2857 	}
2858 	__bnxt_poll_work_done(bp, bnapi, budget);
2859 }
2860 
2861 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
2862 {
2863 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2864 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2865 	struct bnxt_cp_ring_info *cpr_rx;
2866 	u32 raw_cons = cpr->cp_raw_cons;
2867 	struct bnxt *bp = bnapi->bp;
2868 	struct nqe_cn *nqcmp;
2869 	int work_done = 0;
2870 	u32 cons;
2871 
2872 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
2873 		napi_complete(napi);
2874 		return 0;
2875 	}
2876 	if (cpr->has_more_work) {
2877 		cpr->has_more_work = 0;
2878 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
2879 	}
2880 	while (1) {
2881 		cons = RING_CMP(raw_cons);
2882 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2883 
2884 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
2885 			if (cpr->has_more_work)
2886 				break;
2887 
2888 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
2889 					     budget);
2890 			cpr->cp_raw_cons = raw_cons;
2891 			if (napi_complete_done(napi, work_done))
2892 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
2893 						  cpr->cp_raw_cons);
2894 			goto poll_done;
2895 		}
2896 
2897 		/* The valid test of the entry must be done first before
2898 		 * reading any further.
2899 		 */
2900 		dma_rmb();
2901 
2902 		if (nqcmp->type == cpu_to_le16(NQ_CN_TYPE_CQ_NOTIFICATION)) {
2903 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
2904 			struct bnxt_cp_ring_info *cpr2;
2905 
2906 			/* No more budget for RX work */
2907 			if (budget && work_done >= budget && idx == BNXT_RX_HDL)
2908 				break;
2909 
2910 			cpr2 = cpr->cp_ring_arr[idx];
2911 			work_done += __bnxt_poll_work(bp, cpr2,
2912 						      budget - work_done);
2913 			cpr->has_more_work |= cpr2->has_more_work;
2914 		} else {
2915 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
2916 		}
2917 		raw_cons = NEXT_RAW_CMP(raw_cons);
2918 	}
2919 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
2920 	if (raw_cons != cpr->cp_raw_cons) {
2921 		cpr->cp_raw_cons = raw_cons;
2922 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
2923 	}
2924 poll_done:
2925 	cpr_rx = cpr->cp_ring_arr[BNXT_RX_HDL];
2926 	if (cpr_rx && (bp->flags & BNXT_FLAG_DIM)) {
2927 		struct dim_sample dim_sample = {};
2928 
2929 		dim_update_sample(cpr->event_ctr,
2930 				  cpr_rx->rx_packets,
2931 				  cpr_rx->rx_bytes,
2932 				  &dim_sample);
2933 		net_dim(&cpr->dim, dim_sample);
2934 	}
2935 	return work_done;
2936 }
2937 
2938 static void bnxt_free_tx_skbs(struct bnxt *bp)
2939 {
2940 	int i, max_idx;
2941 	struct pci_dev *pdev = bp->pdev;
2942 
2943 	if (!bp->tx_ring)
2944 		return;
2945 
2946 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
2947 	for (i = 0; i < bp->tx_nr_rings; i++) {
2948 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2949 		int j;
2950 
2951 		if (!txr->tx_buf_ring)
2952 			continue;
2953 
2954 		for (j = 0; j < max_idx;) {
2955 			struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
2956 			struct sk_buff *skb;
2957 			int k, last;
2958 
2959 			if (i < bp->tx_nr_rings_xdp &&
2960 			    tx_buf->action == XDP_REDIRECT) {
2961 				dma_unmap_single(&pdev->dev,
2962 					dma_unmap_addr(tx_buf, mapping),
2963 					dma_unmap_len(tx_buf, len),
2964 					DMA_TO_DEVICE);
2965 				xdp_return_frame(tx_buf->xdpf);
2966 				tx_buf->action = 0;
2967 				tx_buf->xdpf = NULL;
2968 				j++;
2969 				continue;
2970 			}
2971 
2972 			skb = tx_buf->skb;
2973 			if (!skb) {
2974 				j++;
2975 				continue;
2976 			}
2977 
2978 			tx_buf->skb = NULL;
2979 
2980 			if (tx_buf->is_push) {
2981 				dev_kfree_skb(skb);
2982 				j += 2;
2983 				continue;
2984 			}
2985 
2986 			dma_unmap_single(&pdev->dev,
2987 					 dma_unmap_addr(tx_buf, mapping),
2988 					 skb_headlen(skb),
2989 					 DMA_TO_DEVICE);
2990 
2991 			last = tx_buf->nr_frags;
2992 			j += 2;
2993 			for (k = 0; k < last; k++, j++) {
2994 				int ring_idx = j & bp->tx_ring_mask;
2995 				skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2996 
2997 				tx_buf = &txr->tx_buf_ring[ring_idx];
2998 				dma_unmap_page(
2999 					&pdev->dev,
3000 					dma_unmap_addr(tx_buf, mapping),
3001 					skb_frag_size(frag), DMA_TO_DEVICE);
3002 			}
3003 			dev_kfree_skb(skb);
3004 		}
3005 		netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
3006 	}
3007 }
3008 
3009 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, int ring_nr)
3010 {
3011 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
3012 	struct pci_dev *pdev = bp->pdev;
3013 	struct bnxt_tpa_idx_map *map;
3014 	int i, max_idx, max_agg_idx;
3015 
3016 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3017 	max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3018 	if (!rxr->rx_tpa)
3019 		goto skip_rx_tpa_free;
3020 
3021 	for (i = 0; i < bp->max_tpa; i++) {
3022 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3023 		u8 *data = tpa_info->data;
3024 
3025 		if (!data)
3026 			continue;
3027 
3028 		dma_unmap_single_attrs(&pdev->dev, tpa_info->mapping,
3029 				       bp->rx_buf_use_size, bp->rx_dir,
3030 				       DMA_ATTR_WEAK_ORDERING);
3031 
3032 		tpa_info->data = NULL;
3033 
3034 		skb_free_frag(data);
3035 	}
3036 
3037 skip_rx_tpa_free:
3038 	if (!rxr->rx_buf_ring)
3039 		goto skip_rx_buf_free;
3040 
3041 	for (i = 0; i < max_idx; i++) {
3042 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3043 		dma_addr_t mapping = rx_buf->mapping;
3044 		void *data = rx_buf->data;
3045 
3046 		if (!data)
3047 			continue;
3048 
3049 		rx_buf->data = NULL;
3050 		if (BNXT_RX_PAGE_MODE(bp)) {
3051 			page_pool_recycle_direct(rxr->page_pool, data);
3052 		} else {
3053 			dma_unmap_single_attrs(&pdev->dev, mapping,
3054 					       bp->rx_buf_use_size, bp->rx_dir,
3055 					       DMA_ATTR_WEAK_ORDERING);
3056 			skb_free_frag(data);
3057 		}
3058 	}
3059 
3060 skip_rx_buf_free:
3061 	if (!rxr->rx_agg_ring)
3062 		goto skip_rx_agg_free;
3063 
3064 	for (i = 0; i < max_agg_idx; i++) {
3065 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3066 		struct page *page = rx_agg_buf->page;
3067 
3068 		if (!page)
3069 			continue;
3070 
3071 		rx_agg_buf->page = NULL;
3072 		__clear_bit(i, rxr->rx_agg_bmap);
3073 
3074 		page_pool_recycle_direct(rxr->page_pool, page);
3075 	}
3076 
3077 skip_rx_agg_free:
3078 	map = rxr->rx_tpa_idx_map;
3079 	if (map)
3080 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3081 }
3082 
3083 static void bnxt_free_rx_skbs(struct bnxt *bp)
3084 {
3085 	int i;
3086 
3087 	if (!bp->rx_ring)
3088 		return;
3089 
3090 	for (i = 0; i < bp->rx_nr_rings; i++)
3091 		bnxt_free_one_rx_ring_skbs(bp, i);
3092 }
3093 
3094 static void bnxt_free_skbs(struct bnxt *bp)
3095 {
3096 	bnxt_free_tx_skbs(bp);
3097 	bnxt_free_rx_skbs(bp);
3098 }
3099 
3100 static void bnxt_init_ctx_mem(struct bnxt_mem_init *mem_init, void *p, int len)
3101 {
3102 	u8 init_val = mem_init->init_val;
3103 	u16 offset = mem_init->offset;
3104 	u8 *p2 = p;
3105 	int i;
3106 
3107 	if (!init_val)
3108 		return;
3109 	if (offset == BNXT_MEM_INVALID_OFFSET) {
3110 		memset(p, init_val, len);
3111 		return;
3112 	}
3113 	for (i = 0; i < len; i += mem_init->size)
3114 		*(p2 + i + offset) = init_val;
3115 }
3116 
3117 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3118 {
3119 	struct pci_dev *pdev = bp->pdev;
3120 	int i;
3121 
3122 	if (!rmem->pg_arr)
3123 		goto skip_pages;
3124 
3125 	for (i = 0; i < rmem->nr_pages; i++) {
3126 		if (!rmem->pg_arr[i])
3127 			continue;
3128 
3129 		dma_free_coherent(&pdev->dev, rmem->page_size,
3130 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3131 
3132 		rmem->pg_arr[i] = NULL;
3133 	}
3134 skip_pages:
3135 	if (rmem->pg_tbl) {
3136 		size_t pg_tbl_size = rmem->nr_pages * 8;
3137 
3138 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3139 			pg_tbl_size = rmem->page_size;
3140 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3141 				  rmem->pg_tbl, rmem->pg_tbl_map);
3142 		rmem->pg_tbl = NULL;
3143 	}
3144 	if (rmem->vmem_size && *rmem->vmem) {
3145 		vfree(*rmem->vmem);
3146 		*rmem->vmem = NULL;
3147 	}
3148 }
3149 
3150 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3151 {
3152 	struct pci_dev *pdev = bp->pdev;
3153 	u64 valid_bit = 0;
3154 	int i;
3155 
3156 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3157 		valid_bit = PTU_PTE_VALID;
3158 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3159 		size_t pg_tbl_size = rmem->nr_pages * 8;
3160 
3161 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3162 			pg_tbl_size = rmem->page_size;
3163 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3164 						  &rmem->pg_tbl_map,
3165 						  GFP_KERNEL);
3166 		if (!rmem->pg_tbl)
3167 			return -ENOMEM;
3168 	}
3169 
3170 	for (i = 0; i < rmem->nr_pages; i++) {
3171 		u64 extra_bits = valid_bit;
3172 
3173 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3174 						     rmem->page_size,
3175 						     &rmem->dma_arr[i],
3176 						     GFP_KERNEL);
3177 		if (!rmem->pg_arr[i])
3178 			return -ENOMEM;
3179 
3180 		if (rmem->mem_init)
3181 			bnxt_init_ctx_mem(rmem->mem_init, rmem->pg_arr[i],
3182 					  rmem->page_size);
3183 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3184 			if (i == rmem->nr_pages - 2 &&
3185 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3186 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3187 			else if (i == rmem->nr_pages - 1 &&
3188 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3189 				extra_bits |= PTU_PTE_LAST;
3190 			rmem->pg_tbl[i] =
3191 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3192 		}
3193 	}
3194 
3195 	if (rmem->vmem_size) {
3196 		*rmem->vmem = vzalloc(rmem->vmem_size);
3197 		if (!(*rmem->vmem))
3198 			return -ENOMEM;
3199 	}
3200 	return 0;
3201 }
3202 
3203 static void bnxt_free_tpa_info(struct bnxt *bp)
3204 {
3205 	int i, j;
3206 
3207 	for (i = 0; i < bp->rx_nr_rings; i++) {
3208 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3209 
3210 		kfree(rxr->rx_tpa_idx_map);
3211 		rxr->rx_tpa_idx_map = NULL;
3212 		if (rxr->rx_tpa) {
3213 			for (j = 0; j < bp->max_tpa; j++) {
3214 				kfree(rxr->rx_tpa[j].agg_arr);
3215 				rxr->rx_tpa[j].agg_arr = NULL;
3216 			}
3217 		}
3218 		kfree(rxr->rx_tpa);
3219 		rxr->rx_tpa = NULL;
3220 	}
3221 }
3222 
3223 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3224 {
3225 	int i, j;
3226 
3227 	bp->max_tpa = MAX_TPA;
3228 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
3229 		if (!bp->max_tpa_v2)
3230 			return 0;
3231 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3232 	}
3233 
3234 	for (i = 0; i < bp->rx_nr_rings; i++) {
3235 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3236 		struct rx_agg_cmp *agg;
3237 
3238 		rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3239 				      GFP_KERNEL);
3240 		if (!rxr->rx_tpa)
3241 			return -ENOMEM;
3242 
3243 		if (!(bp->flags & BNXT_FLAG_CHIP_P5))
3244 			continue;
3245 		for (j = 0; j < bp->max_tpa; j++) {
3246 			agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3247 			if (!agg)
3248 				return -ENOMEM;
3249 			rxr->rx_tpa[j].agg_arr = agg;
3250 		}
3251 		rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3252 					      GFP_KERNEL);
3253 		if (!rxr->rx_tpa_idx_map)
3254 			return -ENOMEM;
3255 	}
3256 	return 0;
3257 }
3258 
3259 static void bnxt_free_rx_rings(struct bnxt *bp)
3260 {
3261 	int i;
3262 
3263 	if (!bp->rx_ring)
3264 		return;
3265 
3266 	bnxt_free_tpa_info(bp);
3267 	for (i = 0; i < bp->rx_nr_rings; i++) {
3268 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3269 		struct bnxt_ring_struct *ring;
3270 
3271 		if (rxr->xdp_prog)
3272 			bpf_prog_put(rxr->xdp_prog);
3273 
3274 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3275 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3276 
3277 		page_pool_destroy(rxr->page_pool);
3278 		rxr->page_pool = NULL;
3279 
3280 		kfree(rxr->rx_agg_bmap);
3281 		rxr->rx_agg_bmap = NULL;
3282 
3283 		ring = &rxr->rx_ring_struct;
3284 		bnxt_free_ring(bp, &ring->ring_mem);
3285 
3286 		ring = &rxr->rx_agg_ring_struct;
3287 		bnxt_free_ring(bp, &ring->ring_mem);
3288 	}
3289 }
3290 
3291 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3292 				   struct bnxt_rx_ring_info *rxr)
3293 {
3294 	struct page_pool_params pp = { 0 };
3295 
3296 	pp.pool_size = bp->rx_agg_ring_size;
3297 	if (BNXT_RX_PAGE_MODE(bp))
3298 		pp.pool_size += bp->rx_ring_size;
3299 	pp.nid = dev_to_node(&bp->pdev->dev);
3300 	pp.napi = &rxr->bnapi->napi;
3301 	pp.dev = &bp->pdev->dev;
3302 	pp.dma_dir = bp->rx_dir;
3303 	pp.max_len = PAGE_SIZE;
3304 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3305 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE)
3306 		pp.flags |= PP_FLAG_PAGE_FRAG;
3307 
3308 	rxr->page_pool = page_pool_create(&pp);
3309 	if (IS_ERR(rxr->page_pool)) {
3310 		int err = PTR_ERR(rxr->page_pool);
3311 
3312 		rxr->page_pool = NULL;
3313 		return err;
3314 	}
3315 	return 0;
3316 }
3317 
3318 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3319 {
3320 	int i, rc = 0, agg_rings = 0;
3321 
3322 	if (!bp->rx_ring)
3323 		return -ENOMEM;
3324 
3325 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3326 		agg_rings = 1;
3327 
3328 	for (i = 0; i < bp->rx_nr_rings; i++) {
3329 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3330 		struct bnxt_ring_struct *ring;
3331 
3332 		ring = &rxr->rx_ring_struct;
3333 
3334 		rc = bnxt_alloc_rx_page_pool(bp, rxr);
3335 		if (rc)
3336 			return rc;
3337 
3338 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3339 		if (rc < 0)
3340 			return rc;
3341 
3342 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3343 						MEM_TYPE_PAGE_POOL,
3344 						rxr->page_pool);
3345 		if (rc) {
3346 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3347 			return rc;
3348 		}
3349 
3350 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3351 		if (rc)
3352 			return rc;
3353 
3354 		ring->grp_idx = i;
3355 		if (agg_rings) {
3356 			u16 mem_size;
3357 
3358 			ring = &rxr->rx_agg_ring_struct;
3359 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3360 			if (rc)
3361 				return rc;
3362 
3363 			ring->grp_idx = i;
3364 			rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3365 			mem_size = rxr->rx_agg_bmap_size / 8;
3366 			rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3367 			if (!rxr->rx_agg_bmap)
3368 				return -ENOMEM;
3369 		}
3370 	}
3371 	if (bp->flags & BNXT_FLAG_TPA)
3372 		rc = bnxt_alloc_tpa_info(bp);
3373 	return rc;
3374 }
3375 
3376 static void bnxt_free_tx_rings(struct bnxt *bp)
3377 {
3378 	int i;
3379 	struct pci_dev *pdev = bp->pdev;
3380 
3381 	if (!bp->tx_ring)
3382 		return;
3383 
3384 	for (i = 0; i < bp->tx_nr_rings; i++) {
3385 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3386 		struct bnxt_ring_struct *ring;
3387 
3388 		if (txr->tx_push) {
3389 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3390 					  txr->tx_push, txr->tx_push_mapping);
3391 			txr->tx_push = NULL;
3392 		}
3393 
3394 		ring = &txr->tx_ring_struct;
3395 
3396 		bnxt_free_ring(bp, &ring->ring_mem);
3397 	}
3398 }
3399 
3400 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3401 {
3402 	int i, j, rc;
3403 	struct pci_dev *pdev = bp->pdev;
3404 
3405 	bp->tx_push_size = 0;
3406 	if (bp->tx_push_thresh) {
3407 		int push_size;
3408 
3409 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3410 					bp->tx_push_thresh);
3411 
3412 		if (push_size > 256) {
3413 			push_size = 0;
3414 			bp->tx_push_thresh = 0;
3415 		}
3416 
3417 		bp->tx_push_size = push_size;
3418 	}
3419 
3420 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3421 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3422 		struct bnxt_ring_struct *ring;
3423 		u8 qidx;
3424 
3425 		ring = &txr->tx_ring_struct;
3426 
3427 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3428 		if (rc)
3429 			return rc;
3430 
3431 		ring->grp_idx = txr->bnapi->index;
3432 		if (bp->tx_push_size) {
3433 			dma_addr_t mapping;
3434 
3435 			/* One pre-allocated DMA buffer to backup
3436 			 * TX push operation
3437 			 */
3438 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3439 						bp->tx_push_size,
3440 						&txr->tx_push_mapping,
3441 						GFP_KERNEL);
3442 
3443 			if (!txr->tx_push)
3444 				return -ENOMEM;
3445 
3446 			mapping = txr->tx_push_mapping +
3447 				sizeof(struct tx_push_bd);
3448 			txr->data_mapping = cpu_to_le64(mapping);
3449 		}
3450 		qidx = bp->tc_to_qidx[j];
3451 		ring->queue_id = bp->q_info[qidx].queue_id;
3452 		spin_lock_init(&txr->xdp_tx_lock);
3453 		if (i < bp->tx_nr_rings_xdp)
3454 			continue;
3455 		if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
3456 			j++;
3457 	}
3458 	return 0;
3459 }
3460 
3461 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
3462 {
3463 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3464 
3465 	kfree(cpr->cp_desc_ring);
3466 	cpr->cp_desc_ring = NULL;
3467 	ring->ring_mem.pg_arr = NULL;
3468 	kfree(cpr->cp_desc_mapping);
3469 	cpr->cp_desc_mapping = NULL;
3470 	ring->ring_mem.dma_arr = NULL;
3471 }
3472 
3473 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
3474 {
3475 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
3476 	if (!cpr->cp_desc_ring)
3477 		return -ENOMEM;
3478 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
3479 				       GFP_KERNEL);
3480 	if (!cpr->cp_desc_mapping)
3481 		return -ENOMEM;
3482 	return 0;
3483 }
3484 
3485 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
3486 {
3487 	int i;
3488 
3489 	if (!bp->bnapi)
3490 		return;
3491 	for (i = 0; i < bp->cp_nr_rings; i++) {
3492 		struct bnxt_napi *bnapi = bp->bnapi[i];
3493 
3494 		if (!bnapi)
3495 			continue;
3496 		bnxt_free_cp_arrays(&bnapi->cp_ring);
3497 	}
3498 }
3499 
3500 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
3501 {
3502 	int i, n = bp->cp_nr_pages;
3503 
3504 	for (i = 0; i < bp->cp_nr_rings; i++) {
3505 		struct bnxt_napi *bnapi = bp->bnapi[i];
3506 		int rc;
3507 
3508 		if (!bnapi)
3509 			continue;
3510 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
3511 		if (rc)
3512 			return rc;
3513 	}
3514 	return 0;
3515 }
3516 
3517 static void bnxt_free_cp_rings(struct bnxt *bp)
3518 {
3519 	int i;
3520 
3521 	if (!bp->bnapi)
3522 		return;
3523 
3524 	for (i = 0; i < bp->cp_nr_rings; i++) {
3525 		struct bnxt_napi *bnapi = bp->bnapi[i];
3526 		struct bnxt_cp_ring_info *cpr;
3527 		struct bnxt_ring_struct *ring;
3528 		int j;
3529 
3530 		if (!bnapi)
3531 			continue;
3532 
3533 		cpr = &bnapi->cp_ring;
3534 		ring = &cpr->cp_ring_struct;
3535 
3536 		bnxt_free_ring(bp, &ring->ring_mem);
3537 
3538 		for (j = 0; j < 2; j++) {
3539 			struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
3540 
3541 			if (cpr2) {
3542 				ring = &cpr2->cp_ring_struct;
3543 				bnxt_free_ring(bp, &ring->ring_mem);
3544 				bnxt_free_cp_arrays(cpr2);
3545 				kfree(cpr2);
3546 				cpr->cp_ring_arr[j] = NULL;
3547 			}
3548 		}
3549 	}
3550 }
3551 
3552 static struct bnxt_cp_ring_info *bnxt_alloc_cp_sub_ring(struct bnxt *bp)
3553 {
3554 	struct bnxt_ring_mem_info *rmem;
3555 	struct bnxt_ring_struct *ring;
3556 	struct bnxt_cp_ring_info *cpr;
3557 	int rc;
3558 
3559 	cpr = kzalloc(sizeof(*cpr), GFP_KERNEL);
3560 	if (!cpr)
3561 		return NULL;
3562 
3563 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
3564 	if (rc) {
3565 		bnxt_free_cp_arrays(cpr);
3566 		kfree(cpr);
3567 		return NULL;
3568 	}
3569 	ring = &cpr->cp_ring_struct;
3570 	rmem = &ring->ring_mem;
3571 	rmem->nr_pages = bp->cp_nr_pages;
3572 	rmem->page_size = HW_CMPD_RING_SIZE;
3573 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
3574 	rmem->dma_arr = cpr->cp_desc_mapping;
3575 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
3576 	rc = bnxt_alloc_ring(bp, rmem);
3577 	if (rc) {
3578 		bnxt_free_ring(bp, rmem);
3579 		bnxt_free_cp_arrays(cpr);
3580 		kfree(cpr);
3581 		cpr = NULL;
3582 	}
3583 	return cpr;
3584 }
3585 
3586 static int bnxt_alloc_cp_rings(struct bnxt *bp)
3587 {
3588 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
3589 	int i, rc, ulp_base_vec, ulp_msix;
3590 
3591 	ulp_msix = bnxt_get_ulp_msix_num(bp);
3592 	ulp_base_vec = bnxt_get_ulp_msix_base(bp);
3593 	for (i = 0; i < bp->cp_nr_rings; i++) {
3594 		struct bnxt_napi *bnapi = bp->bnapi[i];
3595 		struct bnxt_cp_ring_info *cpr;
3596 		struct bnxt_ring_struct *ring;
3597 
3598 		if (!bnapi)
3599 			continue;
3600 
3601 		cpr = &bnapi->cp_ring;
3602 		cpr->bnapi = bnapi;
3603 		ring = &cpr->cp_ring_struct;
3604 
3605 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3606 		if (rc)
3607 			return rc;
3608 
3609 		if (ulp_msix && i >= ulp_base_vec)
3610 			ring->map_idx = i + ulp_msix;
3611 		else
3612 			ring->map_idx = i;
3613 
3614 		if (!(bp->flags & BNXT_FLAG_CHIP_P5))
3615 			continue;
3616 
3617 		if (i < bp->rx_nr_rings) {
3618 			struct bnxt_cp_ring_info *cpr2 =
3619 				bnxt_alloc_cp_sub_ring(bp);
3620 
3621 			cpr->cp_ring_arr[BNXT_RX_HDL] = cpr2;
3622 			if (!cpr2)
3623 				return -ENOMEM;
3624 			cpr2->bnapi = bnapi;
3625 		}
3626 		if ((sh && i < bp->tx_nr_rings) ||
3627 		    (!sh && i >= bp->rx_nr_rings)) {
3628 			struct bnxt_cp_ring_info *cpr2 =
3629 				bnxt_alloc_cp_sub_ring(bp);
3630 
3631 			cpr->cp_ring_arr[BNXT_TX_HDL] = cpr2;
3632 			if (!cpr2)
3633 				return -ENOMEM;
3634 			cpr2->bnapi = bnapi;
3635 		}
3636 	}
3637 	return 0;
3638 }
3639 
3640 static void bnxt_init_ring_struct(struct bnxt *bp)
3641 {
3642 	int i;
3643 
3644 	for (i = 0; i < bp->cp_nr_rings; i++) {
3645 		struct bnxt_napi *bnapi = bp->bnapi[i];
3646 		struct bnxt_ring_mem_info *rmem;
3647 		struct bnxt_cp_ring_info *cpr;
3648 		struct bnxt_rx_ring_info *rxr;
3649 		struct bnxt_tx_ring_info *txr;
3650 		struct bnxt_ring_struct *ring;
3651 
3652 		if (!bnapi)
3653 			continue;
3654 
3655 		cpr = &bnapi->cp_ring;
3656 		ring = &cpr->cp_ring_struct;
3657 		rmem = &ring->ring_mem;
3658 		rmem->nr_pages = bp->cp_nr_pages;
3659 		rmem->page_size = HW_CMPD_RING_SIZE;
3660 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
3661 		rmem->dma_arr = cpr->cp_desc_mapping;
3662 		rmem->vmem_size = 0;
3663 
3664 		rxr = bnapi->rx_ring;
3665 		if (!rxr)
3666 			goto skip_rx;
3667 
3668 		ring = &rxr->rx_ring_struct;
3669 		rmem = &ring->ring_mem;
3670 		rmem->nr_pages = bp->rx_nr_pages;
3671 		rmem->page_size = HW_RXBD_RING_SIZE;
3672 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
3673 		rmem->dma_arr = rxr->rx_desc_mapping;
3674 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
3675 		rmem->vmem = (void **)&rxr->rx_buf_ring;
3676 
3677 		ring = &rxr->rx_agg_ring_struct;
3678 		rmem = &ring->ring_mem;
3679 		rmem->nr_pages = bp->rx_agg_nr_pages;
3680 		rmem->page_size = HW_RXBD_RING_SIZE;
3681 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
3682 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
3683 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
3684 		rmem->vmem = (void **)&rxr->rx_agg_ring;
3685 
3686 skip_rx:
3687 		txr = bnapi->tx_ring;
3688 		if (!txr)
3689 			continue;
3690 
3691 		ring = &txr->tx_ring_struct;
3692 		rmem = &ring->ring_mem;
3693 		rmem->nr_pages = bp->tx_nr_pages;
3694 		rmem->page_size = HW_RXBD_RING_SIZE;
3695 		rmem->pg_arr = (void **)txr->tx_desc_ring;
3696 		rmem->dma_arr = txr->tx_desc_mapping;
3697 		rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
3698 		rmem->vmem = (void **)&txr->tx_buf_ring;
3699 	}
3700 }
3701 
3702 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
3703 {
3704 	int i;
3705 	u32 prod;
3706 	struct rx_bd **rx_buf_ring;
3707 
3708 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
3709 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
3710 		int j;
3711 		struct rx_bd *rxbd;
3712 
3713 		rxbd = rx_buf_ring[i];
3714 		if (!rxbd)
3715 			continue;
3716 
3717 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
3718 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
3719 			rxbd->rx_bd_opaque = prod;
3720 		}
3721 	}
3722 }
3723 
3724 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
3725 {
3726 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
3727 	struct net_device *dev = bp->dev;
3728 	u32 prod;
3729 	int i;
3730 
3731 	prod = rxr->rx_prod;
3732 	for (i = 0; i < bp->rx_ring_size; i++) {
3733 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
3734 			netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
3735 				    ring_nr, i, bp->rx_ring_size);
3736 			break;
3737 		}
3738 		prod = NEXT_RX(prod);
3739 	}
3740 	rxr->rx_prod = prod;
3741 
3742 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
3743 		return 0;
3744 
3745 	prod = rxr->rx_agg_prod;
3746 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
3747 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
3748 			netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
3749 				    ring_nr, i, bp->rx_ring_size);
3750 			break;
3751 		}
3752 		prod = NEXT_RX_AGG(prod);
3753 	}
3754 	rxr->rx_agg_prod = prod;
3755 
3756 	if (rxr->rx_tpa) {
3757 		dma_addr_t mapping;
3758 		u8 *data;
3759 
3760 		for (i = 0; i < bp->max_tpa; i++) {
3761 			data = __bnxt_alloc_rx_frag(bp, &mapping, GFP_KERNEL);
3762 			if (!data)
3763 				return -ENOMEM;
3764 
3765 			rxr->rx_tpa[i].data = data;
3766 			rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
3767 			rxr->rx_tpa[i].mapping = mapping;
3768 		}
3769 	}
3770 	return 0;
3771 }
3772 
3773 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
3774 {
3775 	struct bnxt_rx_ring_info *rxr;
3776 	struct bnxt_ring_struct *ring;
3777 	u32 type;
3778 
3779 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
3780 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
3781 
3782 	if (NET_IP_ALIGN == 2)
3783 		type |= RX_BD_FLAGS_SOP;
3784 
3785 	rxr = &bp->rx_ring[ring_nr];
3786 	ring = &rxr->rx_ring_struct;
3787 	bnxt_init_rxbd_pages(ring, type);
3788 
3789 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
3790 		bpf_prog_add(bp->xdp_prog, 1);
3791 		rxr->xdp_prog = bp->xdp_prog;
3792 	}
3793 	ring->fw_ring_id = INVALID_HW_RING_ID;
3794 
3795 	ring = &rxr->rx_agg_ring_struct;
3796 	ring->fw_ring_id = INVALID_HW_RING_ID;
3797 
3798 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
3799 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
3800 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
3801 
3802 		bnxt_init_rxbd_pages(ring, type);
3803 	}
3804 
3805 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
3806 }
3807 
3808 static void bnxt_init_cp_rings(struct bnxt *bp)
3809 {
3810 	int i, j;
3811 
3812 	for (i = 0; i < bp->cp_nr_rings; i++) {
3813 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
3814 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3815 
3816 		ring->fw_ring_id = INVALID_HW_RING_ID;
3817 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
3818 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
3819 		for (j = 0; j < 2; j++) {
3820 			struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
3821 
3822 			if (!cpr2)
3823 				continue;
3824 
3825 			ring = &cpr2->cp_ring_struct;
3826 			ring->fw_ring_id = INVALID_HW_RING_ID;
3827 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
3828 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
3829 		}
3830 	}
3831 }
3832 
3833 static int bnxt_init_rx_rings(struct bnxt *bp)
3834 {
3835 	int i, rc = 0;
3836 
3837 	if (BNXT_RX_PAGE_MODE(bp)) {
3838 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
3839 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
3840 	} else {
3841 		bp->rx_offset = BNXT_RX_OFFSET;
3842 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
3843 	}
3844 
3845 	for (i = 0; i < bp->rx_nr_rings; i++) {
3846 		rc = bnxt_init_one_rx_ring(bp, i);
3847 		if (rc)
3848 			break;
3849 	}
3850 
3851 	return rc;
3852 }
3853 
3854 static int bnxt_init_tx_rings(struct bnxt *bp)
3855 {
3856 	u16 i;
3857 
3858 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
3859 				   BNXT_MIN_TX_DESC_CNT);
3860 
3861 	for (i = 0; i < bp->tx_nr_rings; i++) {
3862 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3863 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
3864 
3865 		ring->fw_ring_id = INVALID_HW_RING_ID;
3866 	}
3867 
3868 	return 0;
3869 }
3870 
3871 static void bnxt_free_ring_grps(struct bnxt *bp)
3872 {
3873 	kfree(bp->grp_info);
3874 	bp->grp_info = NULL;
3875 }
3876 
3877 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
3878 {
3879 	int i;
3880 
3881 	if (irq_re_init) {
3882 		bp->grp_info = kcalloc(bp->cp_nr_rings,
3883 				       sizeof(struct bnxt_ring_grp_info),
3884 				       GFP_KERNEL);
3885 		if (!bp->grp_info)
3886 			return -ENOMEM;
3887 	}
3888 	for (i = 0; i < bp->cp_nr_rings; i++) {
3889 		if (irq_re_init)
3890 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
3891 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
3892 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
3893 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
3894 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
3895 	}
3896 	return 0;
3897 }
3898 
3899 static void bnxt_free_vnics(struct bnxt *bp)
3900 {
3901 	kfree(bp->vnic_info);
3902 	bp->vnic_info = NULL;
3903 	bp->nr_vnics = 0;
3904 }
3905 
3906 static int bnxt_alloc_vnics(struct bnxt *bp)
3907 {
3908 	int num_vnics = 1;
3909 
3910 #ifdef CONFIG_RFS_ACCEL
3911 	if ((bp->flags & (BNXT_FLAG_RFS | BNXT_FLAG_CHIP_P5)) == BNXT_FLAG_RFS)
3912 		num_vnics += bp->rx_nr_rings;
3913 #endif
3914 
3915 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3916 		num_vnics++;
3917 
3918 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
3919 				GFP_KERNEL);
3920 	if (!bp->vnic_info)
3921 		return -ENOMEM;
3922 
3923 	bp->nr_vnics = num_vnics;
3924 	return 0;
3925 }
3926 
3927 static void bnxt_init_vnics(struct bnxt *bp)
3928 {
3929 	int i;
3930 
3931 	for (i = 0; i < bp->nr_vnics; i++) {
3932 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3933 		int j;
3934 
3935 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
3936 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
3937 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
3938 
3939 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
3940 
3941 		if (bp->vnic_info[i].rss_hash_key) {
3942 			if (i == 0)
3943 				get_random_bytes(vnic->rss_hash_key,
3944 					      HW_HASH_KEY_SIZE);
3945 			else
3946 				memcpy(vnic->rss_hash_key,
3947 				       bp->vnic_info[0].rss_hash_key,
3948 				       HW_HASH_KEY_SIZE);
3949 		}
3950 	}
3951 }
3952 
3953 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
3954 {
3955 	int pages;
3956 
3957 	pages = ring_size / desc_per_pg;
3958 
3959 	if (!pages)
3960 		return 1;
3961 
3962 	pages++;
3963 
3964 	while (pages & (pages - 1))
3965 		pages++;
3966 
3967 	return pages;
3968 }
3969 
3970 void bnxt_set_tpa_flags(struct bnxt *bp)
3971 {
3972 	bp->flags &= ~BNXT_FLAG_TPA;
3973 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
3974 		return;
3975 	if (bp->dev->features & NETIF_F_LRO)
3976 		bp->flags |= BNXT_FLAG_LRO;
3977 	else if (bp->dev->features & NETIF_F_GRO_HW)
3978 		bp->flags |= BNXT_FLAG_GRO;
3979 }
3980 
3981 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
3982  * be set on entry.
3983  */
3984 void bnxt_set_ring_params(struct bnxt *bp)
3985 {
3986 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
3987 	u32 agg_factor = 0, agg_ring_size = 0;
3988 
3989 	/* 8 for CRC and VLAN */
3990 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
3991 
3992 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
3993 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3994 
3995 	bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
3996 	ring_size = bp->rx_ring_size;
3997 	bp->rx_agg_ring_size = 0;
3998 	bp->rx_agg_nr_pages = 0;
3999 
4000 	if (bp->flags & BNXT_FLAG_TPA)
4001 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4002 
4003 	bp->flags &= ~BNXT_FLAG_JUMBO;
4004 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4005 		u32 jumbo_factor;
4006 
4007 		bp->flags |= BNXT_FLAG_JUMBO;
4008 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4009 		if (jumbo_factor > agg_factor)
4010 			agg_factor = jumbo_factor;
4011 	}
4012 	if (agg_factor) {
4013 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4014 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4015 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4016 				    bp->rx_ring_size, ring_size);
4017 			bp->rx_ring_size = ring_size;
4018 		}
4019 		agg_ring_size = ring_size * agg_factor;
4020 
4021 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4022 							RX_DESC_CNT);
4023 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4024 			u32 tmp = agg_ring_size;
4025 
4026 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4027 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4028 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4029 				    tmp, agg_ring_size);
4030 		}
4031 		bp->rx_agg_ring_size = agg_ring_size;
4032 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4033 
4034 		if (BNXT_RX_PAGE_MODE(bp)) {
4035 			rx_space = PAGE_SIZE;
4036 			rx_size = PAGE_SIZE -
4037 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4038 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4039 		} else {
4040 			rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
4041 			rx_space = rx_size + NET_SKB_PAD +
4042 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4043 		}
4044 	}
4045 
4046 	bp->rx_buf_use_size = rx_size;
4047 	bp->rx_buf_size = rx_space;
4048 
4049 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4050 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4051 
4052 	ring_size = bp->tx_ring_size;
4053 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4054 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4055 
4056 	max_rx_cmpl = bp->rx_ring_size;
4057 	/* MAX TPA needs to be added because TPA_START completions are
4058 	 * immediately recycled, so the TPA completions are not bound by
4059 	 * the RX ring size.
4060 	 */
4061 	if (bp->flags & BNXT_FLAG_TPA)
4062 		max_rx_cmpl += bp->max_tpa;
4063 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4064 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4065 	bp->cp_ring_size = ring_size;
4066 
4067 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4068 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4069 		bp->cp_nr_pages = MAX_CP_PAGES;
4070 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4071 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4072 			    ring_size, bp->cp_ring_size);
4073 	}
4074 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4075 	bp->cp_ring_mask = bp->cp_bit - 1;
4076 }
4077 
4078 /* Changing allocation mode of RX rings.
4079  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4080  */
4081 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4082 {
4083 	struct net_device *dev = bp->dev;
4084 
4085 	if (page_mode) {
4086 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
4087 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4088 
4089 		if (bp->xdp_prog->aux->xdp_has_frags)
4090 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4091 		else
4092 			dev->max_mtu =
4093 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4094 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4095 			bp->flags |= BNXT_FLAG_JUMBO;
4096 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4097 		} else {
4098 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4099 			bp->rx_skb_func = bnxt_rx_page_skb;
4100 		}
4101 		bp->rx_dir = DMA_BIDIRECTIONAL;
4102 		/* Disable LRO or GRO_HW */
4103 		netdev_update_features(dev);
4104 	} else {
4105 		dev->max_mtu = bp->max_mtu;
4106 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4107 		bp->rx_dir = DMA_FROM_DEVICE;
4108 		bp->rx_skb_func = bnxt_rx_skb;
4109 	}
4110 	return 0;
4111 }
4112 
4113 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4114 {
4115 	int i;
4116 	struct bnxt_vnic_info *vnic;
4117 	struct pci_dev *pdev = bp->pdev;
4118 
4119 	if (!bp->vnic_info)
4120 		return;
4121 
4122 	for (i = 0; i < bp->nr_vnics; i++) {
4123 		vnic = &bp->vnic_info[i];
4124 
4125 		kfree(vnic->fw_grp_ids);
4126 		vnic->fw_grp_ids = NULL;
4127 
4128 		kfree(vnic->uc_list);
4129 		vnic->uc_list = NULL;
4130 
4131 		if (vnic->mc_list) {
4132 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4133 					  vnic->mc_list, vnic->mc_list_mapping);
4134 			vnic->mc_list = NULL;
4135 		}
4136 
4137 		if (vnic->rss_table) {
4138 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4139 					  vnic->rss_table,
4140 					  vnic->rss_table_dma_addr);
4141 			vnic->rss_table = NULL;
4142 		}
4143 
4144 		vnic->rss_hash_key = NULL;
4145 		vnic->flags = 0;
4146 	}
4147 }
4148 
4149 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4150 {
4151 	int i, rc = 0, size;
4152 	struct bnxt_vnic_info *vnic;
4153 	struct pci_dev *pdev = bp->pdev;
4154 	int max_rings;
4155 
4156 	for (i = 0; i < bp->nr_vnics; i++) {
4157 		vnic = &bp->vnic_info[i];
4158 
4159 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4160 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4161 
4162 			if (mem_size > 0) {
4163 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4164 				if (!vnic->uc_list) {
4165 					rc = -ENOMEM;
4166 					goto out;
4167 				}
4168 			}
4169 		}
4170 
4171 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4172 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4173 			vnic->mc_list =
4174 				dma_alloc_coherent(&pdev->dev,
4175 						   vnic->mc_list_size,
4176 						   &vnic->mc_list_mapping,
4177 						   GFP_KERNEL);
4178 			if (!vnic->mc_list) {
4179 				rc = -ENOMEM;
4180 				goto out;
4181 			}
4182 		}
4183 
4184 		if (bp->flags & BNXT_FLAG_CHIP_P5)
4185 			goto vnic_skip_grps;
4186 
4187 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4188 			max_rings = bp->rx_nr_rings;
4189 		else
4190 			max_rings = 1;
4191 
4192 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4193 		if (!vnic->fw_grp_ids) {
4194 			rc = -ENOMEM;
4195 			goto out;
4196 		}
4197 vnic_skip_grps:
4198 		if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
4199 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4200 			continue;
4201 
4202 		/* Allocate rss table and hash key */
4203 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4204 		if (bp->flags & BNXT_FLAG_CHIP_P5)
4205 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4206 
4207 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4208 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4209 						     vnic->rss_table_size,
4210 						     &vnic->rss_table_dma_addr,
4211 						     GFP_KERNEL);
4212 		if (!vnic->rss_table) {
4213 			rc = -ENOMEM;
4214 			goto out;
4215 		}
4216 
4217 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4218 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4219 	}
4220 	return 0;
4221 
4222 out:
4223 	return rc;
4224 }
4225 
4226 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4227 {
4228 	struct bnxt_hwrm_wait_token *token;
4229 
4230 	dma_pool_destroy(bp->hwrm_dma_pool);
4231 	bp->hwrm_dma_pool = NULL;
4232 
4233 	rcu_read_lock();
4234 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4235 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4236 	rcu_read_unlock();
4237 }
4238 
4239 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4240 {
4241 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4242 					    BNXT_HWRM_DMA_SIZE,
4243 					    BNXT_HWRM_DMA_ALIGN, 0);
4244 	if (!bp->hwrm_dma_pool)
4245 		return -ENOMEM;
4246 
4247 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4248 
4249 	return 0;
4250 }
4251 
4252 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4253 {
4254 	kfree(stats->hw_masks);
4255 	stats->hw_masks = NULL;
4256 	kfree(stats->sw_stats);
4257 	stats->sw_stats = NULL;
4258 	if (stats->hw_stats) {
4259 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
4260 				  stats->hw_stats_map);
4261 		stats->hw_stats = NULL;
4262 	}
4263 }
4264 
4265 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
4266 				bool alloc_masks)
4267 {
4268 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
4269 					     &stats->hw_stats_map, GFP_KERNEL);
4270 	if (!stats->hw_stats)
4271 		return -ENOMEM;
4272 
4273 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
4274 	if (!stats->sw_stats)
4275 		goto stats_mem_err;
4276 
4277 	if (alloc_masks) {
4278 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
4279 		if (!stats->hw_masks)
4280 			goto stats_mem_err;
4281 	}
4282 	return 0;
4283 
4284 stats_mem_err:
4285 	bnxt_free_stats_mem(bp, stats);
4286 	return -ENOMEM;
4287 }
4288 
4289 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
4290 {
4291 	int i;
4292 
4293 	for (i = 0; i < count; i++)
4294 		mask_arr[i] = mask;
4295 }
4296 
4297 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
4298 {
4299 	int i;
4300 
4301 	for (i = 0; i < count; i++)
4302 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
4303 }
4304 
4305 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
4306 				    struct bnxt_stats_mem *stats)
4307 {
4308 	struct hwrm_func_qstats_ext_output *resp;
4309 	struct hwrm_func_qstats_ext_input *req;
4310 	__le64 *hw_masks;
4311 	int rc;
4312 
4313 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
4314 	    !(bp->flags & BNXT_FLAG_CHIP_P5))
4315 		return -EOPNOTSUPP;
4316 
4317 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
4318 	if (rc)
4319 		return rc;
4320 
4321 	req->fid = cpu_to_le16(0xffff);
4322 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4323 
4324 	resp = hwrm_req_hold(bp, req);
4325 	rc = hwrm_req_send(bp, req);
4326 	if (!rc) {
4327 		hw_masks = &resp->rx_ucast_pkts;
4328 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
4329 	}
4330 	hwrm_req_drop(bp, req);
4331 	return rc;
4332 }
4333 
4334 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
4335 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
4336 
4337 static void bnxt_init_stats(struct bnxt *bp)
4338 {
4339 	struct bnxt_napi *bnapi = bp->bnapi[0];
4340 	struct bnxt_cp_ring_info *cpr;
4341 	struct bnxt_stats_mem *stats;
4342 	__le64 *rx_stats, *tx_stats;
4343 	int rc, rx_count, tx_count;
4344 	u64 *rx_masks, *tx_masks;
4345 	u64 mask;
4346 	u8 flags;
4347 
4348 	cpr = &bnapi->cp_ring;
4349 	stats = &cpr->stats;
4350 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
4351 	if (rc) {
4352 		if (bp->flags & BNXT_FLAG_CHIP_P5)
4353 			mask = (1ULL << 48) - 1;
4354 		else
4355 			mask = -1ULL;
4356 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
4357 	}
4358 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
4359 		stats = &bp->port_stats;
4360 		rx_stats = stats->hw_stats;
4361 		rx_masks = stats->hw_masks;
4362 		rx_count = sizeof(struct rx_port_stats) / 8;
4363 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4364 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4365 		tx_count = sizeof(struct tx_port_stats) / 8;
4366 
4367 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
4368 		rc = bnxt_hwrm_port_qstats(bp, flags);
4369 		if (rc) {
4370 			mask = (1ULL << 40) - 1;
4371 
4372 			bnxt_fill_masks(rx_masks, mask, rx_count);
4373 			bnxt_fill_masks(tx_masks, mask, tx_count);
4374 		} else {
4375 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4376 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
4377 			bnxt_hwrm_port_qstats(bp, 0);
4378 		}
4379 	}
4380 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
4381 		stats = &bp->rx_port_stats_ext;
4382 		rx_stats = stats->hw_stats;
4383 		rx_masks = stats->hw_masks;
4384 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
4385 		stats = &bp->tx_port_stats_ext;
4386 		tx_stats = stats->hw_stats;
4387 		tx_masks = stats->hw_masks;
4388 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
4389 
4390 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4391 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
4392 		if (rc) {
4393 			mask = (1ULL << 40) - 1;
4394 
4395 			bnxt_fill_masks(rx_masks, mask, rx_count);
4396 			if (tx_stats)
4397 				bnxt_fill_masks(tx_masks, mask, tx_count);
4398 		} else {
4399 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4400 			if (tx_stats)
4401 				bnxt_copy_hw_masks(tx_masks, tx_stats,
4402 						   tx_count);
4403 			bnxt_hwrm_port_qstats_ext(bp, 0);
4404 		}
4405 	}
4406 }
4407 
4408 static void bnxt_free_port_stats(struct bnxt *bp)
4409 {
4410 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
4411 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
4412 
4413 	bnxt_free_stats_mem(bp, &bp->port_stats);
4414 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
4415 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
4416 }
4417 
4418 static void bnxt_free_ring_stats(struct bnxt *bp)
4419 {
4420 	int i;
4421 
4422 	if (!bp->bnapi)
4423 		return;
4424 
4425 	for (i = 0; i < bp->cp_nr_rings; i++) {
4426 		struct bnxt_napi *bnapi = bp->bnapi[i];
4427 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4428 
4429 		bnxt_free_stats_mem(bp, &cpr->stats);
4430 	}
4431 }
4432 
4433 static int bnxt_alloc_stats(struct bnxt *bp)
4434 {
4435 	u32 size, i;
4436 	int rc;
4437 
4438 	size = bp->hw_ring_stats_size;
4439 
4440 	for (i = 0; i < bp->cp_nr_rings; i++) {
4441 		struct bnxt_napi *bnapi = bp->bnapi[i];
4442 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4443 
4444 		cpr->stats.len = size;
4445 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
4446 		if (rc)
4447 			return rc;
4448 
4449 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4450 	}
4451 
4452 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
4453 		return 0;
4454 
4455 	if (bp->port_stats.hw_stats)
4456 		goto alloc_ext_stats;
4457 
4458 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
4459 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
4460 	if (rc)
4461 		return rc;
4462 
4463 	bp->flags |= BNXT_FLAG_PORT_STATS;
4464 
4465 alloc_ext_stats:
4466 	/* Display extended statistics only if FW supports it */
4467 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
4468 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
4469 			return 0;
4470 
4471 	if (bp->rx_port_stats_ext.hw_stats)
4472 		goto alloc_tx_ext_stats;
4473 
4474 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
4475 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
4476 	/* Extended stats are optional */
4477 	if (rc)
4478 		return 0;
4479 
4480 alloc_tx_ext_stats:
4481 	if (bp->tx_port_stats_ext.hw_stats)
4482 		return 0;
4483 
4484 	if (bp->hwrm_spec_code >= 0x10902 ||
4485 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
4486 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
4487 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
4488 		/* Extended stats are optional */
4489 		if (rc)
4490 			return 0;
4491 	}
4492 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
4493 	return 0;
4494 }
4495 
4496 static void bnxt_clear_ring_indices(struct bnxt *bp)
4497 {
4498 	int i;
4499 
4500 	if (!bp->bnapi)
4501 		return;
4502 
4503 	for (i = 0; i < bp->cp_nr_rings; i++) {
4504 		struct bnxt_napi *bnapi = bp->bnapi[i];
4505 		struct bnxt_cp_ring_info *cpr;
4506 		struct bnxt_rx_ring_info *rxr;
4507 		struct bnxt_tx_ring_info *txr;
4508 
4509 		if (!bnapi)
4510 			continue;
4511 
4512 		cpr = &bnapi->cp_ring;
4513 		cpr->cp_raw_cons = 0;
4514 
4515 		txr = bnapi->tx_ring;
4516 		if (txr) {
4517 			txr->tx_prod = 0;
4518 			txr->tx_cons = 0;
4519 		}
4520 
4521 		rxr = bnapi->rx_ring;
4522 		if (rxr) {
4523 			rxr->rx_prod = 0;
4524 			rxr->rx_agg_prod = 0;
4525 			rxr->rx_sw_agg_prod = 0;
4526 			rxr->rx_next_cons = 0;
4527 		}
4528 	}
4529 }
4530 
4531 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
4532 {
4533 #ifdef CONFIG_RFS_ACCEL
4534 	int i;
4535 
4536 	/* Under rtnl_lock and all our NAPIs have been disabled.  It's
4537 	 * safe to delete the hash table.
4538 	 */
4539 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
4540 		struct hlist_head *head;
4541 		struct hlist_node *tmp;
4542 		struct bnxt_ntuple_filter *fltr;
4543 
4544 		head = &bp->ntp_fltr_hash_tbl[i];
4545 		hlist_for_each_entry_safe(fltr, tmp, head, hash) {
4546 			hlist_del(&fltr->hash);
4547 			kfree(fltr);
4548 		}
4549 	}
4550 	if (irq_reinit) {
4551 		bitmap_free(bp->ntp_fltr_bmap);
4552 		bp->ntp_fltr_bmap = NULL;
4553 	}
4554 	bp->ntp_fltr_count = 0;
4555 #endif
4556 }
4557 
4558 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
4559 {
4560 #ifdef CONFIG_RFS_ACCEL
4561 	int i, rc = 0;
4562 
4563 	if (!(bp->flags & BNXT_FLAG_RFS))
4564 		return 0;
4565 
4566 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
4567 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
4568 
4569 	bp->ntp_fltr_count = 0;
4570 	bp->ntp_fltr_bmap = bitmap_zalloc(BNXT_NTP_FLTR_MAX_FLTR, GFP_KERNEL);
4571 
4572 	if (!bp->ntp_fltr_bmap)
4573 		rc = -ENOMEM;
4574 
4575 	return rc;
4576 #else
4577 	return 0;
4578 #endif
4579 }
4580 
4581 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
4582 {
4583 	bnxt_free_vnic_attributes(bp);
4584 	bnxt_free_tx_rings(bp);
4585 	bnxt_free_rx_rings(bp);
4586 	bnxt_free_cp_rings(bp);
4587 	bnxt_free_all_cp_arrays(bp);
4588 	bnxt_free_ntp_fltrs(bp, irq_re_init);
4589 	if (irq_re_init) {
4590 		bnxt_free_ring_stats(bp);
4591 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
4592 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
4593 			bnxt_free_port_stats(bp);
4594 		bnxt_free_ring_grps(bp);
4595 		bnxt_free_vnics(bp);
4596 		kfree(bp->tx_ring_map);
4597 		bp->tx_ring_map = NULL;
4598 		kfree(bp->tx_ring);
4599 		bp->tx_ring = NULL;
4600 		kfree(bp->rx_ring);
4601 		bp->rx_ring = NULL;
4602 		kfree(bp->bnapi);
4603 		bp->bnapi = NULL;
4604 	} else {
4605 		bnxt_clear_ring_indices(bp);
4606 	}
4607 }
4608 
4609 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
4610 {
4611 	int i, j, rc, size, arr_size;
4612 	void *bnapi;
4613 
4614 	if (irq_re_init) {
4615 		/* Allocate bnapi mem pointer array and mem block for
4616 		 * all queues
4617 		 */
4618 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
4619 				bp->cp_nr_rings);
4620 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
4621 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
4622 		if (!bnapi)
4623 			return -ENOMEM;
4624 
4625 		bp->bnapi = bnapi;
4626 		bnapi += arr_size;
4627 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
4628 			bp->bnapi[i] = bnapi;
4629 			bp->bnapi[i]->index = i;
4630 			bp->bnapi[i]->bp = bp;
4631 			if (bp->flags & BNXT_FLAG_CHIP_P5) {
4632 				struct bnxt_cp_ring_info *cpr =
4633 					&bp->bnapi[i]->cp_ring;
4634 
4635 				cpr->cp_ring_struct.ring_mem.flags =
4636 					BNXT_RMEM_RING_PTE_FLAG;
4637 			}
4638 		}
4639 
4640 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
4641 				      sizeof(struct bnxt_rx_ring_info),
4642 				      GFP_KERNEL);
4643 		if (!bp->rx_ring)
4644 			return -ENOMEM;
4645 
4646 		for (i = 0; i < bp->rx_nr_rings; i++) {
4647 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
4648 
4649 			if (bp->flags & BNXT_FLAG_CHIP_P5) {
4650 				rxr->rx_ring_struct.ring_mem.flags =
4651 					BNXT_RMEM_RING_PTE_FLAG;
4652 				rxr->rx_agg_ring_struct.ring_mem.flags =
4653 					BNXT_RMEM_RING_PTE_FLAG;
4654 			}
4655 			rxr->bnapi = bp->bnapi[i];
4656 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
4657 		}
4658 
4659 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
4660 				      sizeof(struct bnxt_tx_ring_info),
4661 				      GFP_KERNEL);
4662 		if (!bp->tx_ring)
4663 			return -ENOMEM;
4664 
4665 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
4666 					  GFP_KERNEL);
4667 
4668 		if (!bp->tx_ring_map)
4669 			return -ENOMEM;
4670 
4671 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
4672 			j = 0;
4673 		else
4674 			j = bp->rx_nr_rings;
4675 
4676 		for (i = 0; i < bp->tx_nr_rings; i++, j++) {
4677 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4678 
4679 			if (bp->flags & BNXT_FLAG_CHIP_P5)
4680 				txr->tx_ring_struct.ring_mem.flags =
4681 					BNXT_RMEM_RING_PTE_FLAG;
4682 			txr->bnapi = bp->bnapi[j];
4683 			bp->bnapi[j]->tx_ring = txr;
4684 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
4685 			if (i >= bp->tx_nr_rings_xdp) {
4686 				txr->txq_index = i - bp->tx_nr_rings_xdp;
4687 				bp->bnapi[j]->tx_int = bnxt_tx_int;
4688 			} else {
4689 				bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
4690 				bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
4691 			}
4692 		}
4693 
4694 		rc = bnxt_alloc_stats(bp);
4695 		if (rc)
4696 			goto alloc_mem_err;
4697 		bnxt_init_stats(bp);
4698 
4699 		rc = bnxt_alloc_ntp_fltrs(bp);
4700 		if (rc)
4701 			goto alloc_mem_err;
4702 
4703 		rc = bnxt_alloc_vnics(bp);
4704 		if (rc)
4705 			goto alloc_mem_err;
4706 	}
4707 
4708 	rc = bnxt_alloc_all_cp_arrays(bp);
4709 	if (rc)
4710 		goto alloc_mem_err;
4711 
4712 	bnxt_init_ring_struct(bp);
4713 
4714 	rc = bnxt_alloc_rx_rings(bp);
4715 	if (rc)
4716 		goto alloc_mem_err;
4717 
4718 	rc = bnxt_alloc_tx_rings(bp);
4719 	if (rc)
4720 		goto alloc_mem_err;
4721 
4722 	rc = bnxt_alloc_cp_rings(bp);
4723 	if (rc)
4724 		goto alloc_mem_err;
4725 
4726 	bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
4727 				  BNXT_VNIC_UCAST_FLAG;
4728 	rc = bnxt_alloc_vnic_attributes(bp);
4729 	if (rc)
4730 		goto alloc_mem_err;
4731 	return 0;
4732 
4733 alloc_mem_err:
4734 	bnxt_free_mem(bp, true);
4735 	return rc;
4736 }
4737 
4738 static void bnxt_disable_int(struct bnxt *bp)
4739 {
4740 	int i;
4741 
4742 	if (!bp->bnapi)
4743 		return;
4744 
4745 	for (i = 0; i < bp->cp_nr_rings; i++) {
4746 		struct bnxt_napi *bnapi = bp->bnapi[i];
4747 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4748 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4749 
4750 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
4751 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
4752 	}
4753 }
4754 
4755 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
4756 {
4757 	struct bnxt_napi *bnapi = bp->bnapi[n];
4758 	struct bnxt_cp_ring_info *cpr;
4759 
4760 	cpr = &bnapi->cp_ring;
4761 	return cpr->cp_ring_struct.map_idx;
4762 }
4763 
4764 static void bnxt_disable_int_sync(struct bnxt *bp)
4765 {
4766 	int i;
4767 
4768 	if (!bp->irq_tbl)
4769 		return;
4770 
4771 	atomic_inc(&bp->intr_sem);
4772 
4773 	bnxt_disable_int(bp);
4774 	for (i = 0; i < bp->cp_nr_rings; i++) {
4775 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
4776 
4777 		synchronize_irq(bp->irq_tbl[map_idx].vector);
4778 	}
4779 }
4780 
4781 static void bnxt_enable_int(struct bnxt *bp)
4782 {
4783 	int i;
4784 
4785 	atomic_set(&bp->intr_sem, 0);
4786 	for (i = 0; i < bp->cp_nr_rings; i++) {
4787 		struct bnxt_napi *bnapi = bp->bnapi[i];
4788 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4789 
4790 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
4791 	}
4792 }
4793 
4794 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
4795 			    bool async_only)
4796 {
4797 	DECLARE_BITMAP(async_events_bmap, 256);
4798 	u32 *events = (u32 *)async_events_bmap;
4799 	struct hwrm_func_drv_rgtr_output *resp;
4800 	struct hwrm_func_drv_rgtr_input *req;
4801 	u32 flags;
4802 	int rc, i;
4803 
4804 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
4805 	if (rc)
4806 		return rc;
4807 
4808 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
4809 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
4810 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
4811 
4812 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
4813 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
4814 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
4815 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
4816 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
4817 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
4818 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
4819 	req->flags = cpu_to_le32(flags);
4820 	req->ver_maj_8b = DRV_VER_MAJ;
4821 	req->ver_min_8b = DRV_VER_MIN;
4822 	req->ver_upd_8b = DRV_VER_UPD;
4823 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
4824 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
4825 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
4826 
4827 	if (BNXT_PF(bp)) {
4828 		u32 data[8];
4829 		int i;
4830 
4831 		memset(data, 0, sizeof(data));
4832 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
4833 			u16 cmd = bnxt_vf_req_snif[i];
4834 			unsigned int bit, idx;
4835 
4836 			idx = cmd / 32;
4837 			bit = cmd % 32;
4838 			data[idx] |= 1 << bit;
4839 		}
4840 
4841 		for (i = 0; i < 8; i++)
4842 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
4843 
4844 		req->enables |=
4845 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
4846 	}
4847 
4848 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
4849 		req->flags |= cpu_to_le32(
4850 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
4851 
4852 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
4853 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
4854 		u16 event_id = bnxt_async_events_arr[i];
4855 
4856 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
4857 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
4858 			continue;
4859 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
4860 		    !bp->ptp_cfg)
4861 			continue;
4862 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
4863 	}
4864 	if (bmap && bmap_size) {
4865 		for (i = 0; i < bmap_size; i++) {
4866 			if (test_bit(i, bmap))
4867 				__set_bit(i, async_events_bmap);
4868 		}
4869 	}
4870 	for (i = 0; i < 8; i++)
4871 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
4872 
4873 	if (async_only)
4874 		req->enables =
4875 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
4876 
4877 	resp = hwrm_req_hold(bp, req);
4878 	rc = hwrm_req_send(bp, req);
4879 	if (!rc) {
4880 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
4881 		if (resp->flags &
4882 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
4883 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
4884 	}
4885 	hwrm_req_drop(bp, req);
4886 	return rc;
4887 }
4888 
4889 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
4890 {
4891 	struct hwrm_func_drv_unrgtr_input *req;
4892 	int rc;
4893 
4894 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
4895 		return 0;
4896 
4897 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
4898 	if (rc)
4899 		return rc;
4900 	return hwrm_req_send(bp, req);
4901 }
4902 
4903 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
4904 {
4905 	struct hwrm_tunnel_dst_port_free_input *req;
4906 	int rc;
4907 
4908 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
4909 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
4910 		return 0;
4911 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
4912 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
4913 		return 0;
4914 
4915 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
4916 	if (rc)
4917 		return rc;
4918 
4919 	req->tunnel_type = tunnel_type;
4920 
4921 	switch (tunnel_type) {
4922 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
4923 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
4924 		bp->vxlan_port = 0;
4925 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
4926 		break;
4927 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
4928 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
4929 		bp->nge_port = 0;
4930 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
4931 		break;
4932 	default:
4933 		break;
4934 	}
4935 
4936 	rc = hwrm_req_send(bp, req);
4937 	if (rc)
4938 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
4939 			   rc);
4940 	return rc;
4941 }
4942 
4943 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
4944 					   u8 tunnel_type)
4945 {
4946 	struct hwrm_tunnel_dst_port_alloc_output *resp;
4947 	struct hwrm_tunnel_dst_port_alloc_input *req;
4948 	int rc;
4949 
4950 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
4951 	if (rc)
4952 		return rc;
4953 
4954 	req->tunnel_type = tunnel_type;
4955 	req->tunnel_dst_port_val = port;
4956 
4957 	resp = hwrm_req_hold(bp, req);
4958 	rc = hwrm_req_send(bp, req);
4959 	if (rc) {
4960 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
4961 			   rc);
4962 		goto err_out;
4963 	}
4964 
4965 	switch (tunnel_type) {
4966 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
4967 		bp->vxlan_port = port;
4968 		bp->vxlan_fw_dst_port_id =
4969 			le16_to_cpu(resp->tunnel_dst_port_id);
4970 		break;
4971 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
4972 		bp->nge_port = port;
4973 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
4974 		break;
4975 	default:
4976 		break;
4977 	}
4978 
4979 err_out:
4980 	hwrm_req_drop(bp, req);
4981 	return rc;
4982 }
4983 
4984 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
4985 {
4986 	struct hwrm_cfa_l2_set_rx_mask_input *req;
4987 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4988 	int rc;
4989 
4990 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
4991 	if (rc)
4992 		return rc;
4993 
4994 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
4995 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
4996 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
4997 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
4998 	}
4999 	req->mask = cpu_to_le32(vnic->rx_mask);
5000 	return hwrm_req_send_silent(bp, req);
5001 }
5002 
5003 #ifdef CONFIG_RFS_ACCEL
5004 static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
5005 					    struct bnxt_ntuple_filter *fltr)
5006 {
5007 	struct hwrm_cfa_ntuple_filter_free_input *req;
5008 	int rc;
5009 
5010 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
5011 	if (rc)
5012 		return rc;
5013 
5014 	req->ntuple_filter_id = fltr->filter_id;
5015 	return hwrm_req_send(bp, req);
5016 }
5017 
5018 #define BNXT_NTP_FLTR_FLAGS					\
5019 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
5020 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
5021 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR |	\
5022 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
5023 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
5024 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
5025 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
5026 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
5027 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
5028 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
5029 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
5030 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
5031 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
5032 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
5033 
5034 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
5035 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
5036 
5037 static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
5038 					     struct bnxt_ntuple_filter *fltr)
5039 {
5040 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
5041 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
5042 	struct flow_keys *keys = &fltr->fkeys;
5043 	struct bnxt_vnic_info *vnic;
5044 	u32 flags = 0;
5045 	int rc;
5046 
5047 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
5048 	if (rc)
5049 		return rc;
5050 
5051 	req->l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
5052 
5053 	if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
5054 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
5055 		req->dst_id = cpu_to_le16(fltr->rxq);
5056 	} else {
5057 		vnic = &bp->vnic_info[fltr->rxq + 1];
5058 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
5059 	}
5060 	req->flags = cpu_to_le32(flags);
5061 	req->enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
5062 
5063 	req->ethertype = htons(ETH_P_IP);
5064 	memcpy(req->src_macaddr, fltr->src_mac_addr, ETH_ALEN);
5065 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
5066 	req->ip_protocol = keys->basic.ip_proto;
5067 
5068 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
5069 		int i;
5070 
5071 		req->ethertype = htons(ETH_P_IPV6);
5072 		req->ip_addr_type =
5073 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
5074 		*(struct in6_addr *)&req->src_ipaddr[0] =
5075 			keys->addrs.v6addrs.src;
5076 		*(struct in6_addr *)&req->dst_ipaddr[0] =
5077 			keys->addrs.v6addrs.dst;
5078 		for (i = 0; i < 4; i++) {
5079 			req->src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
5080 			req->dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
5081 		}
5082 	} else {
5083 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
5084 		req->src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
5085 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
5086 		req->dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
5087 	}
5088 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
5089 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
5090 		req->tunnel_type =
5091 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
5092 	}
5093 
5094 	req->src_port = keys->ports.src;
5095 	req->src_port_mask = cpu_to_be16(0xffff);
5096 	req->dst_port = keys->ports.dst;
5097 	req->dst_port_mask = cpu_to_be16(0xffff);
5098 
5099 	resp = hwrm_req_hold(bp, req);
5100 	rc = hwrm_req_send(bp, req);
5101 	if (!rc)
5102 		fltr->filter_id = resp->ntuple_filter_id;
5103 	hwrm_req_drop(bp, req);
5104 	return rc;
5105 }
5106 #endif
5107 
5108 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
5109 				     const u8 *mac_addr)
5110 {
5111 	struct hwrm_cfa_l2_filter_alloc_output *resp;
5112 	struct hwrm_cfa_l2_filter_alloc_input *req;
5113 	int rc;
5114 
5115 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
5116 	if (rc)
5117 		return rc;
5118 
5119 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
5120 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
5121 		req->flags |=
5122 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
5123 	req->dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
5124 	req->enables =
5125 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
5126 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
5127 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
5128 	memcpy(req->l2_addr, mac_addr, ETH_ALEN);
5129 	req->l2_addr_mask[0] = 0xff;
5130 	req->l2_addr_mask[1] = 0xff;
5131 	req->l2_addr_mask[2] = 0xff;
5132 	req->l2_addr_mask[3] = 0xff;
5133 	req->l2_addr_mask[4] = 0xff;
5134 	req->l2_addr_mask[5] = 0xff;
5135 
5136 	resp = hwrm_req_hold(bp, req);
5137 	rc = hwrm_req_send(bp, req);
5138 	if (!rc)
5139 		bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
5140 							resp->l2_filter_id;
5141 	hwrm_req_drop(bp, req);
5142 	return rc;
5143 }
5144 
5145 static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
5146 {
5147 	struct hwrm_cfa_l2_filter_free_input *req;
5148 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
5149 	int rc;
5150 
5151 	/* Any associated ntuple filters will also be cleared by firmware. */
5152 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
5153 	if (rc)
5154 		return rc;
5155 	hwrm_req_hold(bp, req);
5156 	for (i = 0; i < num_of_vnics; i++) {
5157 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
5158 
5159 		for (j = 0; j < vnic->uc_filter_count; j++) {
5160 			req->l2_filter_id = vnic->fw_l2_filter_id[j];
5161 
5162 			rc = hwrm_req_send(bp, req);
5163 		}
5164 		vnic->uc_filter_count = 0;
5165 	}
5166 	hwrm_req_drop(bp, req);
5167 	return rc;
5168 }
5169 
5170 static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
5171 {
5172 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5173 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
5174 	struct hwrm_vnic_tpa_cfg_input *req;
5175 	int rc;
5176 
5177 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
5178 		return 0;
5179 
5180 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
5181 	if (rc)
5182 		return rc;
5183 
5184 	if (tpa_flags) {
5185 		u16 mss = bp->dev->mtu - 40;
5186 		u32 nsegs, n, segs = 0, flags;
5187 
5188 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
5189 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
5190 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
5191 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
5192 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
5193 		if (tpa_flags & BNXT_FLAG_GRO)
5194 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
5195 
5196 		req->flags = cpu_to_le32(flags);
5197 
5198 		req->enables =
5199 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
5200 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
5201 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
5202 
5203 		/* Number of segs are log2 units, and first packet is not
5204 		 * included as part of this units.
5205 		 */
5206 		if (mss <= BNXT_RX_PAGE_SIZE) {
5207 			n = BNXT_RX_PAGE_SIZE / mss;
5208 			nsegs = (MAX_SKB_FRAGS - 1) * n;
5209 		} else {
5210 			n = mss / BNXT_RX_PAGE_SIZE;
5211 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
5212 				n++;
5213 			nsegs = (MAX_SKB_FRAGS - n) / n;
5214 		}
5215 
5216 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5217 			segs = MAX_TPA_SEGS_P5;
5218 			max_aggs = bp->max_tpa;
5219 		} else {
5220 			segs = ilog2(nsegs);
5221 		}
5222 		req->max_agg_segs = cpu_to_le16(segs);
5223 		req->max_aggs = cpu_to_le16(max_aggs);
5224 
5225 		req->min_agg_len = cpu_to_le32(512);
5226 	}
5227 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5228 
5229 	return hwrm_req_send(bp, req);
5230 }
5231 
5232 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
5233 {
5234 	struct bnxt_ring_grp_info *grp_info;
5235 
5236 	grp_info = &bp->grp_info[ring->grp_idx];
5237 	return grp_info->cp_fw_ring_id;
5238 }
5239 
5240 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
5241 {
5242 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
5243 		struct bnxt_napi *bnapi = rxr->bnapi;
5244 		struct bnxt_cp_ring_info *cpr;
5245 
5246 		cpr = bnapi->cp_ring.cp_ring_arr[BNXT_RX_HDL];
5247 		return cpr->cp_ring_struct.fw_ring_id;
5248 	} else {
5249 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
5250 	}
5251 }
5252 
5253 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
5254 {
5255 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
5256 		struct bnxt_napi *bnapi = txr->bnapi;
5257 		struct bnxt_cp_ring_info *cpr;
5258 
5259 		cpr = bnapi->cp_ring.cp_ring_arr[BNXT_TX_HDL];
5260 		return cpr->cp_ring_struct.fw_ring_id;
5261 	} else {
5262 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
5263 	}
5264 }
5265 
5266 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
5267 {
5268 	int entries;
5269 
5270 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5271 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
5272 	else
5273 		entries = HW_HASH_INDEX_SIZE;
5274 
5275 	bp->rss_indir_tbl_entries = entries;
5276 	bp->rss_indir_tbl = kmalloc_array(entries, sizeof(*bp->rss_indir_tbl),
5277 					  GFP_KERNEL);
5278 	if (!bp->rss_indir_tbl)
5279 		return -ENOMEM;
5280 	return 0;
5281 }
5282 
5283 static void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp)
5284 {
5285 	u16 max_rings, max_entries, pad, i;
5286 
5287 	if (!bp->rx_nr_rings)
5288 		return;
5289 
5290 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5291 		max_rings = bp->rx_nr_rings - 1;
5292 	else
5293 		max_rings = bp->rx_nr_rings;
5294 
5295 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
5296 
5297 	for (i = 0; i < max_entries; i++)
5298 		bp->rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
5299 
5300 	pad = bp->rss_indir_tbl_entries - max_entries;
5301 	if (pad)
5302 		memset(&bp->rss_indir_tbl[i], 0, pad * sizeof(u16));
5303 }
5304 
5305 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
5306 {
5307 	u16 i, tbl_size, max_ring = 0;
5308 
5309 	if (!bp->rss_indir_tbl)
5310 		return 0;
5311 
5312 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
5313 	for (i = 0; i < tbl_size; i++)
5314 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
5315 	return max_ring;
5316 }
5317 
5318 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
5319 {
5320 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5321 		return DIV_ROUND_UP(rx_rings, BNXT_RSS_TABLE_ENTRIES_P5);
5322 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5323 		return 2;
5324 	return 1;
5325 }
5326 
5327 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
5328 {
5329 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
5330 	u16 i, j;
5331 
5332 	/* Fill the RSS indirection table with ring group ids */
5333 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
5334 		if (!no_rss)
5335 			j = bp->rss_indir_tbl[i];
5336 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
5337 	}
5338 }
5339 
5340 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
5341 				    struct bnxt_vnic_info *vnic)
5342 {
5343 	__le16 *ring_tbl = vnic->rss_table;
5344 	struct bnxt_rx_ring_info *rxr;
5345 	u16 tbl_size, i;
5346 
5347 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
5348 
5349 	for (i = 0; i < tbl_size; i++) {
5350 		u16 ring_id, j;
5351 
5352 		j = bp->rss_indir_tbl[i];
5353 		rxr = &bp->rx_ring[j];
5354 
5355 		ring_id = rxr->rx_ring_struct.fw_ring_id;
5356 		*ring_tbl++ = cpu_to_le16(ring_id);
5357 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5358 		*ring_tbl++ = cpu_to_le16(ring_id);
5359 	}
5360 }
5361 
5362 static void
5363 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
5364 			 struct bnxt_vnic_info *vnic)
5365 {
5366 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5367 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
5368 	else
5369 		bnxt_fill_hw_rss_tbl(bp, vnic);
5370 
5371 	if (bp->rss_hash_delta) {
5372 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
5373 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
5374 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
5375 		else
5376 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
5377 	} else {
5378 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
5379 	}
5380 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
5381 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
5382 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
5383 }
5384 
5385 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
5386 {
5387 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5388 	struct hwrm_vnic_rss_cfg_input *req;
5389 	int rc;
5390 
5391 	if ((bp->flags & BNXT_FLAG_CHIP_P5) ||
5392 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
5393 		return 0;
5394 
5395 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
5396 	if (rc)
5397 		return rc;
5398 
5399 	if (set_rss)
5400 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
5401 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
5402 	return hwrm_req_send(bp, req);
5403 }
5404 
5405 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp, u16 vnic_id, bool set_rss)
5406 {
5407 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5408 	struct hwrm_vnic_rss_cfg_input *req;
5409 	dma_addr_t ring_tbl_map;
5410 	u32 i, nr_ctxs;
5411 	int rc;
5412 
5413 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
5414 	if (rc)
5415 		return rc;
5416 
5417 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5418 	if (!set_rss)
5419 		return hwrm_req_send(bp, req);
5420 
5421 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
5422 	ring_tbl_map = vnic->rss_table_dma_addr;
5423 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
5424 
5425 	hwrm_req_hold(bp, req);
5426 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
5427 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
5428 		req->ring_table_pair_index = i;
5429 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
5430 		rc = hwrm_req_send(bp, req);
5431 		if (rc)
5432 			goto exit;
5433 	}
5434 
5435 exit:
5436 	hwrm_req_drop(bp, req);
5437 	return rc;
5438 }
5439 
5440 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
5441 {
5442 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5443 	struct hwrm_vnic_rss_qcfg_output *resp;
5444 	struct hwrm_vnic_rss_qcfg_input *req;
5445 
5446 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
5447 		return;
5448 
5449 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5450 	/* all contexts configured to same hash_type, zero always exists */
5451 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
5452 	resp = hwrm_req_hold(bp, req);
5453 	if (!hwrm_req_send(bp, req)) {
5454 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
5455 		bp->rss_hash_delta = 0;
5456 	}
5457 	hwrm_req_drop(bp, req);
5458 }
5459 
5460 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
5461 {
5462 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5463 	struct hwrm_vnic_plcmodes_cfg_input *req;
5464 	int rc;
5465 
5466 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
5467 	if (rc)
5468 		return rc;
5469 
5470 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
5471 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
5472 
5473 	if (BNXT_RX_PAGE_MODE(bp)) {
5474 		req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
5475 	} else {
5476 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
5477 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
5478 		req->enables |=
5479 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
5480 		req->jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
5481 		req->hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
5482 	}
5483 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5484 	return hwrm_req_send(bp, req);
5485 }
5486 
5487 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
5488 					u16 ctx_idx)
5489 {
5490 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
5491 
5492 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
5493 		return;
5494 
5495 	req->rss_cos_lb_ctx_id =
5496 		cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
5497 
5498 	hwrm_req_send(bp, req);
5499 	bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
5500 }
5501 
5502 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
5503 {
5504 	int i, j;
5505 
5506 	for (i = 0; i < bp->nr_vnics; i++) {
5507 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
5508 
5509 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
5510 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
5511 				bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
5512 		}
5513 	}
5514 	bp->rsscos_nr_ctxs = 0;
5515 }
5516 
5517 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
5518 {
5519 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
5520 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
5521 	int rc;
5522 
5523 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
5524 	if (rc)
5525 		return rc;
5526 
5527 	resp = hwrm_req_hold(bp, req);
5528 	rc = hwrm_req_send(bp, req);
5529 	if (!rc)
5530 		bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
5531 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
5532 	hwrm_req_drop(bp, req);
5533 
5534 	return rc;
5535 }
5536 
5537 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
5538 {
5539 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
5540 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
5541 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
5542 }
5543 
5544 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
5545 {
5546 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5547 	struct hwrm_vnic_cfg_input *req;
5548 	unsigned int ring = 0, grp_idx;
5549 	u16 def_vlan = 0;
5550 	int rc;
5551 
5552 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
5553 	if (rc)
5554 		return rc;
5555 
5556 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
5557 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
5558 
5559 		req->default_rx_ring_id =
5560 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
5561 		req->default_cmpl_ring_id =
5562 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
5563 		req->enables =
5564 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
5565 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
5566 		goto vnic_mru;
5567 	}
5568 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
5569 	/* Only RSS support for now TBD: COS & LB */
5570 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
5571 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
5572 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
5573 					   VNIC_CFG_REQ_ENABLES_MRU);
5574 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
5575 		req->rss_rule =
5576 			cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
5577 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
5578 					   VNIC_CFG_REQ_ENABLES_MRU);
5579 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
5580 	} else {
5581 		req->rss_rule = cpu_to_le16(0xffff);
5582 	}
5583 
5584 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
5585 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
5586 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
5587 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
5588 	} else {
5589 		req->cos_rule = cpu_to_le16(0xffff);
5590 	}
5591 
5592 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
5593 		ring = 0;
5594 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
5595 		ring = vnic_id - 1;
5596 	else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
5597 		ring = bp->rx_nr_rings - 1;
5598 
5599 	grp_idx = bp->rx_ring[ring].bnapi->index;
5600 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
5601 	req->lb_rule = cpu_to_le16(0xffff);
5602 vnic_mru:
5603 	req->mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + VLAN_HLEN);
5604 
5605 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5606 #ifdef CONFIG_BNXT_SRIOV
5607 	if (BNXT_VF(bp))
5608 		def_vlan = bp->vf.vlan;
5609 #endif
5610 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
5611 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
5612 	if (!vnic_id && bnxt_ulp_registered(bp->edev))
5613 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
5614 
5615 	return hwrm_req_send(bp, req);
5616 }
5617 
5618 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
5619 {
5620 	if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
5621 		struct hwrm_vnic_free_input *req;
5622 
5623 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
5624 			return;
5625 
5626 		req->vnic_id =
5627 			cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
5628 
5629 		hwrm_req_send(bp, req);
5630 		bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
5631 	}
5632 }
5633 
5634 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
5635 {
5636 	u16 i;
5637 
5638 	for (i = 0; i < bp->nr_vnics; i++)
5639 		bnxt_hwrm_vnic_free_one(bp, i);
5640 }
5641 
5642 static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
5643 				unsigned int start_rx_ring_idx,
5644 				unsigned int nr_rings)
5645 {
5646 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
5647 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5648 	struct hwrm_vnic_alloc_output *resp;
5649 	struct hwrm_vnic_alloc_input *req;
5650 	int rc;
5651 
5652 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
5653 	if (rc)
5654 		return rc;
5655 
5656 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5657 		goto vnic_no_ring_grps;
5658 
5659 	/* map ring groups to this vnic */
5660 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
5661 		grp_idx = bp->rx_ring[i].bnapi->index;
5662 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
5663 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
5664 				   j, nr_rings);
5665 			break;
5666 		}
5667 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
5668 	}
5669 
5670 vnic_no_ring_grps:
5671 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
5672 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
5673 	if (vnic_id == 0)
5674 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
5675 
5676 	resp = hwrm_req_hold(bp, req);
5677 	rc = hwrm_req_send(bp, req);
5678 	if (!rc)
5679 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
5680 	hwrm_req_drop(bp, req);
5681 	return rc;
5682 }
5683 
5684 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
5685 {
5686 	struct hwrm_vnic_qcaps_output *resp;
5687 	struct hwrm_vnic_qcaps_input *req;
5688 	int rc;
5689 
5690 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
5691 	bp->flags &= ~(BNXT_FLAG_NEW_RSS_CAP | BNXT_FLAG_ROCE_MIRROR_CAP);
5692 	if (bp->hwrm_spec_code < 0x10600)
5693 		return 0;
5694 
5695 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
5696 	if (rc)
5697 		return rc;
5698 
5699 	resp = hwrm_req_hold(bp, req);
5700 	rc = hwrm_req_send(bp, req);
5701 	if (!rc) {
5702 		u32 flags = le32_to_cpu(resp->flags);
5703 
5704 		if (!(bp->flags & BNXT_FLAG_CHIP_P5) &&
5705 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
5706 			bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
5707 		if (flags &
5708 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
5709 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
5710 
5711 		/* Older P5 fw before EXT_HW_STATS support did not set
5712 		 * VLAN_STRIP_CAP properly.
5713 		 */
5714 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
5715 		    (BNXT_CHIP_P5_THOR(bp) &&
5716 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
5717 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
5718 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
5719 			bp->fw_cap |= BNXT_FW_CAP_RSS_HASH_TYPE_DELTA;
5720 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
5721 		if (bp->max_tpa_v2) {
5722 			if (BNXT_CHIP_P5_THOR(bp))
5723 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
5724 			else
5725 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5_SR2;
5726 		}
5727 	}
5728 	hwrm_req_drop(bp, req);
5729 	return rc;
5730 }
5731 
5732 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
5733 {
5734 	struct hwrm_ring_grp_alloc_output *resp;
5735 	struct hwrm_ring_grp_alloc_input *req;
5736 	int rc;
5737 	u16 i;
5738 
5739 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5740 		return 0;
5741 
5742 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
5743 	if (rc)
5744 		return rc;
5745 
5746 	resp = hwrm_req_hold(bp, req);
5747 	for (i = 0; i < bp->rx_nr_rings; i++) {
5748 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
5749 
5750 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
5751 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
5752 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
5753 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
5754 
5755 		rc = hwrm_req_send(bp, req);
5756 
5757 		if (rc)
5758 			break;
5759 
5760 		bp->grp_info[grp_idx].fw_grp_id =
5761 			le32_to_cpu(resp->ring_group_id);
5762 	}
5763 	hwrm_req_drop(bp, req);
5764 	return rc;
5765 }
5766 
5767 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
5768 {
5769 	struct hwrm_ring_grp_free_input *req;
5770 	u16 i;
5771 
5772 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5))
5773 		return;
5774 
5775 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
5776 		return;
5777 
5778 	hwrm_req_hold(bp, req);
5779 	for (i = 0; i < bp->cp_nr_rings; i++) {
5780 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
5781 			continue;
5782 		req->ring_group_id =
5783 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
5784 
5785 		hwrm_req_send(bp, req);
5786 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
5787 	}
5788 	hwrm_req_drop(bp, req);
5789 }
5790 
5791 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
5792 				    struct bnxt_ring_struct *ring,
5793 				    u32 ring_type, u32 map_index)
5794 {
5795 	struct hwrm_ring_alloc_output *resp;
5796 	struct hwrm_ring_alloc_input *req;
5797 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
5798 	struct bnxt_ring_grp_info *grp_info;
5799 	int rc, err = 0;
5800 	u16 ring_id;
5801 
5802 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
5803 	if (rc)
5804 		goto exit;
5805 
5806 	req->enables = 0;
5807 	if (rmem->nr_pages > 1) {
5808 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
5809 		/* Page size is in log2 units */
5810 		req->page_size = BNXT_PAGE_SHIFT;
5811 		req->page_tbl_depth = 1;
5812 	} else {
5813 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
5814 	}
5815 	req->fbo = 0;
5816 	/* Association of ring index with doorbell index and MSIX number */
5817 	req->logical_id = cpu_to_le16(map_index);
5818 
5819 	switch (ring_type) {
5820 	case HWRM_RING_ALLOC_TX: {
5821 		struct bnxt_tx_ring_info *txr;
5822 
5823 		txr = container_of(ring, struct bnxt_tx_ring_info,
5824 				   tx_ring_struct);
5825 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
5826 		/* Association of transmit ring with completion ring */
5827 		grp_info = &bp->grp_info[ring->grp_idx];
5828 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
5829 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
5830 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5831 		req->queue_id = cpu_to_le16(ring->queue_id);
5832 		break;
5833 	}
5834 	case HWRM_RING_ALLOC_RX:
5835 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
5836 		req->length = cpu_to_le32(bp->rx_ring_mask + 1);
5837 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5838 			u16 flags = 0;
5839 
5840 			/* Association of rx ring with stats context */
5841 			grp_info = &bp->grp_info[ring->grp_idx];
5842 			req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
5843 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5844 			req->enables |= cpu_to_le32(
5845 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
5846 			if (NET_IP_ALIGN == 2)
5847 				flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
5848 			req->flags = cpu_to_le16(flags);
5849 		}
5850 		break;
5851 	case HWRM_RING_ALLOC_AGG:
5852 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5853 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
5854 			/* Association of agg ring with rx ring */
5855 			grp_info = &bp->grp_info[ring->grp_idx];
5856 			req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
5857 			req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
5858 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5859 			req->enables |= cpu_to_le32(
5860 				RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
5861 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
5862 		} else {
5863 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
5864 		}
5865 		req->length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
5866 		break;
5867 	case HWRM_RING_ALLOC_CMPL:
5868 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
5869 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
5870 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5871 			/* Association of cp ring with nq */
5872 			grp_info = &bp->grp_info[map_index];
5873 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
5874 			req->cq_handle = cpu_to_le64(ring->handle);
5875 			req->enables |= cpu_to_le32(
5876 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
5877 		} else if (bp->flags & BNXT_FLAG_USING_MSIX) {
5878 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
5879 		}
5880 		break;
5881 	case HWRM_RING_ALLOC_NQ:
5882 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
5883 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
5884 		if (bp->flags & BNXT_FLAG_USING_MSIX)
5885 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
5886 		break;
5887 	default:
5888 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
5889 			   ring_type);
5890 		return -1;
5891 	}
5892 
5893 	resp = hwrm_req_hold(bp, req);
5894 	rc = hwrm_req_send(bp, req);
5895 	err = le16_to_cpu(resp->error_code);
5896 	ring_id = le16_to_cpu(resp->ring_id);
5897 	hwrm_req_drop(bp, req);
5898 
5899 exit:
5900 	if (rc || err) {
5901 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
5902 			   ring_type, rc, err);
5903 		return -EIO;
5904 	}
5905 	ring->fw_ring_id = ring_id;
5906 	return rc;
5907 }
5908 
5909 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
5910 {
5911 	int rc;
5912 
5913 	if (BNXT_PF(bp)) {
5914 		struct hwrm_func_cfg_input *req;
5915 
5916 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
5917 		if (rc)
5918 			return rc;
5919 
5920 		req->fid = cpu_to_le16(0xffff);
5921 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
5922 		req->async_event_cr = cpu_to_le16(idx);
5923 		return hwrm_req_send(bp, req);
5924 	} else {
5925 		struct hwrm_func_vf_cfg_input *req;
5926 
5927 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
5928 		if (rc)
5929 			return rc;
5930 
5931 		req->enables =
5932 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
5933 		req->async_event_cr = cpu_to_le16(idx);
5934 		return hwrm_req_send(bp, req);
5935 	}
5936 }
5937 
5938 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
5939 			u32 map_idx, u32 xid)
5940 {
5941 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
5942 		if (BNXT_PF(bp))
5943 			db->doorbell = bp->bar1 + DB_PF_OFFSET_P5;
5944 		else
5945 			db->doorbell = bp->bar1 + DB_VF_OFFSET_P5;
5946 		switch (ring_type) {
5947 		case HWRM_RING_ALLOC_TX:
5948 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
5949 			break;
5950 		case HWRM_RING_ALLOC_RX:
5951 		case HWRM_RING_ALLOC_AGG:
5952 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
5953 			break;
5954 		case HWRM_RING_ALLOC_CMPL:
5955 			db->db_key64 = DBR_PATH_L2;
5956 			break;
5957 		case HWRM_RING_ALLOC_NQ:
5958 			db->db_key64 = DBR_PATH_L2;
5959 			break;
5960 		}
5961 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
5962 	} else {
5963 		db->doorbell = bp->bar1 + map_idx * 0x80;
5964 		switch (ring_type) {
5965 		case HWRM_RING_ALLOC_TX:
5966 			db->db_key32 = DB_KEY_TX;
5967 			break;
5968 		case HWRM_RING_ALLOC_RX:
5969 		case HWRM_RING_ALLOC_AGG:
5970 			db->db_key32 = DB_KEY_RX;
5971 			break;
5972 		case HWRM_RING_ALLOC_CMPL:
5973 			db->db_key32 = DB_KEY_CP;
5974 			break;
5975 		}
5976 	}
5977 }
5978 
5979 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
5980 {
5981 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
5982 	int i, rc = 0;
5983 	u32 type;
5984 
5985 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5986 		type = HWRM_RING_ALLOC_NQ;
5987 	else
5988 		type = HWRM_RING_ALLOC_CMPL;
5989 	for (i = 0; i < bp->cp_nr_rings; i++) {
5990 		struct bnxt_napi *bnapi = bp->bnapi[i];
5991 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5992 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5993 		u32 map_idx = ring->map_idx;
5994 		unsigned int vector;
5995 
5996 		vector = bp->irq_tbl[map_idx].vector;
5997 		disable_irq_nosync(vector);
5998 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5999 		if (rc) {
6000 			enable_irq(vector);
6001 			goto err_out;
6002 		}
6003 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
6004 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
6005 		enable_irq(vector);
6006 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
6007 
6008 		if (!i) {
6009 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
6010 			if (rc)
6011 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
6012 		}
6013 	}
6014 
6015 	type = HWRM_RING_ALLOC_TX;
6016 	for (i = 0; i < bp->tx_nr_rings; i++) {
6017 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
6018 		struct bnxt_ring_struct *ring;
6019 		u32 map_idx;
6020 
6021 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
6022 			struct bnxt_napi *bnapi = txr->bnapi;
6023 			struct bnxt_cp_ring_info *cpr, *cpr2;
6024 			u32 type2 = HWRM_RING_ALLOC_CMPL;
6025 
6026 			cpr = &bnapi->cp_ring;
6027 			cpr2 = cpr->cp_ring_arr[BNXT_TX_HDL];
6028 			ring = &cpr2->cp_ring_struct;
6029 			ring->handle = BNXT_TX_HDL;
6030 			map_idx = bnapi->index;
6031 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
6032 			if (rc)
6033 				goto err_out;
6034 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
6035 				    ring->fw_ring_id);
6036 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
6037 		}
6038 		ring = &txr->tx_ring_struct;
6039 		map_idx = i;
6040 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6041 		if (rc)
6042 			goto err_out;
6043 		bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
6044 	}
6045 
6046 	type = HWRM_RING_ALLOC_RX;
6047 	for (i = 0; i < bp->rx_nr_rings; i++) {
6048 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
6049 		struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
6050 		struct bnxt_napi *bnapi = rxr->bnapi;
6051 		u32 map_idx = bnapi->index;
6052 
6053 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6054 		if (rc)
6055 			goto err_out;
6056 		bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
6057 		/* If we have agg rings, post agg buffers first. */
6058 		if (!agg_rings)
6059 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
6060 		bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
6061 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
6062 			struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6063 			u32 type2 = HWRM_RING_ALLOC_CMPL;
6064 			struct bnxt_cp_ring_info *cpr2;
6065 
6066 			cpr2 = cpr->cp_ring_arr[BNXT_RX_HDL];
6067 			ring = &cpr2->cp_ring_struct;
6068 			ring->handle = BNXT_RX_HDL;
6069 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
6070 			if (rc)
6071 				goto err_out;
6072 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
6073 				    ring->fw_ring_id);
6074 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
6075 		}
6076 	}
6077 
6078 	if (agg_rings) {
6079 		type = HWRM_RING_ALLOC_AGG;
6080 		for (i = 0; i < bp->rx_nr_rings; i++) {
6081 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
6082 			struct bnxt_ring_struct *ring =
6083 						&rxr->rx_agg_ring_struct;
6084 			u32 grp_idx = ring->grp_idx;
6085 			u32 map_idx = grp_idx + bp->rx_nr_rings;
6086 
6087 			rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6088 			if (rc)
6089 				goto err_out;
6090 
6091 			bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
6092 				    ring->fw_ring_id);
6093 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
6094 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
6095 			bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
6096 		}
6097 	}
6098 err_out:
6099 	return rc;
6100 }
6101 
6102 static int hwrm_ring_free_send_msg(struct bnxt *bp,
6103 				   struct bnxt_ring_struct *ring,
6104 				   u32 ring_type, int cmpl_ring_id)
6105 {
6106 	struct hwrm_ring_free_output *resp;
6107 	struct hwrm_ring_free_input *req;
6108 	u16 error_code = 0;
6109 	int rc;
6110 
6111 	if (BNXT_NO_FW_ACCESS(bp))
6112 		return 0;
6113 
6114 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
6115 	if (rc)
6116 		goto exit;
6117 
6118 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
6119 	req->ring_type = ring_type;
6120 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
6121 
6122 	resp = hwrm_req_hold(bp, req);
6123 	rc = hwrm_req_send(bp, req);
6124 	error_code = le16_to_cpu(resp->error_code);
6125 	hwrm_req_drop(bp, req);
6126 exit:
6127 	if (rc || error_code) {
6128 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
6129 			   ring_type, rc, error_code);
6130 		return -EIO;
6131 	}
6132 	return 0;
6133 }
6134 
6135 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
6136 {
6137 	u32 type;
6138 	int i;
6139 
6140 	if (!bp->bnapi)
6141 		return;
6142 
6143 	for (i = 0; i < bp->tx_nr_rings; i++) {
6144 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
6145 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
6146 
6147 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
6148 			u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
6149 
6150 			hwrm_ring_free_send_msg(bp, ring,
6151 						RING_FREE_REQ_RING_TYPE_TX,
6152 						close_path ? cmpl_ring_id :
6153 						INVALID_HW_RING_ID);
6154 			ring->fw_ring_id = INVALID_HW_RING_ID;
6155 		}
6156 	}
6157 
6158 	for (i = 0; i < bp->rx_nr_rings; i++) {
6159 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
6160 		struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
6161 		u32 grp_idx = rxr->bnapi->index;
6162 
6163 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
6164 			u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6165 
6166 			hwrm_ring_free_send_msg(bp, ring,
6167 						RING_FREE_REQ_RING_TYPE_RX,
6168 						close_path ? cmpl_ring_id :
6169 						INVALID_HW_RING_ID);
6170 			ring->fw_ring_id = INVALID_HW_RING_ID;
6171 			bp->grp_info[grp_idx].rx_fw_ring_id =
6172 				INVALID_HW_RING_ID;
6173 		}
6174 	}
6175 
6176 	if (bp->flags & BNXT_FLAG_CHIP_P5)
6177 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
6178 	else
6179 		type = RING_FREE_REQ_RING_TYPE_RX;
6180 	for (i = 0; i < bp->rx_nr_rings; i++) {
6181 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
6182 		struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
6183 		u32 grp_idx = rxr->bnapi->index;
6184 
6185 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
6186 			u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6187 
6188 			hwrm_ring_free_send_msg(bp, ring, type,
6189 						close_path ? cmpl_ring_id :
6190 						INVALID_HW_RING_ID);
6191 			ring->fw_ring_id = INVALID_HW_RING_ID;
6192 			bp->grp_info[grp_idx].agg_fw_ring_id =
6193 				INVALID_HW_RING_ID;
6194 		}
6195 	}
6196 
6197 	/* The completion rings are about to be freed.  After that the
6198 	 * IRQ doorbell will not work anymore.  So we need to disable
6199 	 * IRQ here.
6200 	 */
6201 	bnxt_disable_int_sync(bp);
6202 
6203 	if (bp->flags & BNXT_FLAG_CHIP_P5)
6204 		type = RING_FREE_REQ_RING_TYPE_NQ;
6205 	else
6206 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
6207 	for (i = 0; i < bp->cp_nr_rings; i++) {
6208 		struct bnxt_napi *bnapi = bp->bnapi[i];
6209 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6210 		struct bnxt_ring_struct *ring;
6211 		int j;
6212 
6213 		for (j = 0; j < 2; j++) {
6214 			struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
6215 
6216 			if (cpr2) {
6217 				ring = &cpr2->cp_ring_struct;
6218 				if (ring->fw_ring_id == INVALID_HW_RING_ID)
6219 					continue;
6220 				hwrm_ring_free_send_msg(bp, ring,
6221 					RING_FREE_REQ_RING_TYPE_L2_CMPL,
6222 					INVALID_HW_RING_ID);
6223 				ring->fw_ring_id = INVALID_HW_RING_ID;
6224 			}
6225 		}
6226 		ring = &cpr->cp_ring_struct;
6227 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
6228 			hwrm_ring_free_send_msg(bp, ring, type,
6229 						INVALID_HW_RING_ID);
6230 			ring->fw_ring_id = INVALID_HW_RING_ID;
6231 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
6232 		}
6233 	}
6234 }
6235 
6236 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
6237 			   bool shared);
6238 
6239 static int bnxt_hwrm_get_rings(struct bnxt *bp)
6240 {
6241 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6242 	struct hwrm_func_qcfg_output *resp;
6243 	struct hwrm_func_qcfg_input *req;
6244 	int rc;
6245 
6246 	if (bp->hwrm_spec_code < 0x10601)
6247 		return 0;
6248 
6249 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
6250 	if (rc)
6251 		return rc;
6252 
6253 	req->fid = cpu_to_le16(0xffff);
6254 	resp = hwrm_req_hold(bp, req);
6255 	rc = hwrm_req_send(bp, req);
6256 	if (rc) {
6257 		hwrm_req_drop(bp, req);
6258 		return rc;
6259 	}
6260 
6261 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
6262 	if (BNXT_NEW_RM(bp)) {
6263 		u16 cp, stats;
6264 
6265 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
6266 		hw_resc->resv_hw_ring_grps =
6267 			le32_to_cpu(resp->alloc_hw_ring_grps);
6268 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
6269 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
6270 		stats = le16_to_cpu(resp->alloc_stat_ctx);
6271 		hw_resc->resv_irqs = cp;
6272 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
6273 			int rx = hw_resc->resv_rx_rings;
6274 			int tx = hw_resc->resv_tx_rings;
6275 
6276 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
6277 				rx >>= 1;
6278 			if (cp < (rx + tx)) {
6279 				bnxt_trim_rings(bp, &rx, &tx, cp, false);
6280 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
6281 					rx <<= 1;
6282 				hw_resc->resv_rx_rings = rx;
6283 				hw_resc->resv_tx_rings = tx;
6284 			}
6285 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
6286 			hw_resc->resv_hw_ring_grps = rx;
6287 		}
6288 		hw_resc->resv_cp_rings = cp;
6289 		hw_resc->resv_stat_ctxs = stats;
6290 	}
6291 	hwrm_req_drop(bp, req);
6292 	return 0;
6293 }
6294 
6295 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
6296 {
6297 	struct hwrm_func_qcfg_output *resp;
6298 	struct hwrm_func_qcfg_input *req;
6299 	int rc;
6300 
6301 	if (bp->hwrm_spec_code < 0x10601)
6302 		return 0;
6303 
6304 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
6305 	if (rc)
6306 		return rc;
6307 
6308 	req->fid = cpu_to_le16(fid);
6309 	resp = hwrm_req_hold(bp, req);
6310 	rc = hwrm_req_send(bp, req);
6311 	if (!rc)
6312 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
6313 
6314 	hwrm_req_drop(bp, req);
6315 	return rc;
6316 }
6317 
6318 static bool bnxt_rfs_supported(struct bnxt *bp);
6319 
6320 static struct hwrm_func_cfg_input *
6321 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6322 			     int ring_grps, int cp_rings, int stats, int vnics)
6323 {
6324 	struct hwrm_func_cfg_input *req;
6325 	u32 enables = 0;
6326 
6327 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
6328 		return NULL;
6329 
6330 	req->fid = cpu_to_le16(0xffff);
6331 	enables |= tx_rings ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
6332 	req->num_tx_rings = cpu_to_le16(tx_rings);
6333 	if (BNXT_NEW_RM(bp)) {
6334 		enables |= rx_rings ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
6335 		enables |= stats ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
6336 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
6337 			enables |= cp_rings ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
6338 			enables |= tx_rings + ring_grps ?
6339 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6340 			enables |= rx_rings ?
6341 				FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6342 		} else {
6343 			enables |= cp_rings ?
6344 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6345 			enables |= ring_grps ?
6346 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS |
6347 				   FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6348 		}
6349 		enables |= vnics ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
6350 
6351 		req->num_rx_rings = cpu_to_le16(rx_rings);
6352 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
6353 			req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
6354 			req->num_msix = cpu_to_le16(cp_rings);
6355 			req->num_rsscos_ctxs =
6356 				cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
6357 		} else {
6358 			req->num_cmpl_rings = cpu_to_le16(cp_rings);
6359 			req->num_hw_ring_grps = cpu_to_le16(ring_grps);
6360 			req->num_rsscos_ctxs = cpu_to_le16(1);
6361 			if (!(bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
6362 			    bnxt_rfs_supported(bp))
6363 				req->num_rsscos_ctxs =
6364 					cpu_to_le16(ring_grps + 1);
6365 		}
6366 		req->num_stat_ctxs = cpu_to_le16(stats);
6367 		req->num_vnics = cpu_to_le16(vnics);
6368 	}
6369 	req->enables = cpu_to_le32(enables);
6370 	return req;
6371 }
6372 
6373 static struct hwrm_func_vf_cfg_input *
6374 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6375 			     int ring_grps, int cp_rings, int stats, int vnics)
6376 {
6377 	struct hwrm_func_vf_cfg_input *req;
6378 	u32 enables = 0;
6379 
6380 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
6381 		return NULL;
6382 
6383 	enables |= tx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
6384 	enables |= rx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
6385 			      FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6386 	enables |= stats ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
6387 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
6388 		enables |= tx_rings + ring_grps ?
6389 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6390 	} else {
6391 		enables |= cp_rings ?
6392 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6393 		enables |= ring_grps ?
6394 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
6395 	}
6396 	enables |= vnics ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
6397 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
6398 
6399 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
6400 	req->num_tx_rings = cpu_to_le16(tx_rings);
6401 	req->num_rx_rings = cpu_to_le16(rx_rings);
6402 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
6403 		req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
6404 		req->num_rsscos_ctxs = cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
6405 	} else {
6406 		req->num_cmpl_rings = cpu_to_le16(cp_rings);
6407 		req->num_hw_ring_grps = cpu_to_le16(ring_grps);
6408 		req->num_rsscos_ctxs = cpu_to_le16(BNXT_VF_MAX_RSS_CTX);
6409 	}
6410 	req->num_stat_ctxs = cpu_to_le16(stats);
6411 	req->num_vnics = cpu_to_le16(vnics);
6412 
6413 	req->enables = cpu_to_le32(enables);
6414 	return req;
6415 }
6416 
6417 static int
6418 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6419 			   int ring_grps, int cp_rings, int stats, int vnics)
6420 {
6421 	struct hwrm_func_cfg_input *req;
6422 	int rc;
6423 
6424 	req = __bnxt_hwrm_reserve_pf_rings(bp, tx_rings, rx_rings, ring_grps,
6425 					   cp_rings, stats, vnics);
6426 	if (!req)
6427 		return -ENOMEM;
6428 
6429 	if (!req->enables) {
6430 		hwrm_req_drop(bp, req);
6431 		return 0;
6432 	}
6433 
6434 	rc = hwrm_req_send(bp, req);
6435 	if (rc)
6436 		return rc;
6437 
6438 	if (bp->hwrm_spec_code < 0x10601)
6439 		bp->hw_resc.resv_tx_rings = tx_rings;
6440 
6441 	return bnxt_hwrm_get_rings(bp);
6442 }
6443 
6444 static int
6445 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6446 			   int ring_grps, int cp_rings, int stats, int vnics)
6447 {
6448 	struct hwrm_func_vf_cfg_input *req;
6449 	int rc;
6450 
6451 	if (!BNXT_NEW_RM(bp)) {
6452 		bp->hw_resc.resv_tx_rings = tx_rings;
6453 		return 0;
6454 	}
6455 
6456 	req = __bnxt_hwrm_reserve_vf_rings(bp, tx_rings, rx_rings, ring_grps,
6457 					   cp_rings, stats, vnics);
6458 	if (!req)
6459 		return -ENOMEM;
6460 
6461 	rc = hwrm_req_send(bp, req);
6462 	if (rc)
6463 		return rc;
6464 
6465 	return bnxt_hwrm_get_rings(bp);
6466 }
6467 
6468 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, int tx, int rx, int grp,
6469 				   int cp, int stat, int vnic)
6470 {
6471 	if (BNXT_PF(bp))
6472 		return bnxt_hwrm_reserve_pf_rings(bp, tx, rx, grp, cp, stat,
6473 						  vnic);
6474 	else
6475 		return bnxt_hwrm_reserve_vf_rings(bp, tx, rx, grp, cp, stat,
6476 						  vnic);
6477 }
6478 
6479 int bnxt_nq_rings_in_use(struct bnxt *bp)
6480 {
6481 	int cp = bp->cp_nr_rings;
6482 	int ulp_msix, ulp_base;
6483 
6484 	ulp_msix = bnxt_get_ulp_msix_num(bp);
6485 	if (ulp_msix) {
6486 		ulp_base = bnxt_get_ulp_msix_base(bp);
6487 		cp += ulp_msix;
6488 		if ((ulp_base + ulp_msix) > cp)
6489 			cp = ulp_base + ulp_msix;
6490 	}
6491 	return cp;
6492 }
6493 
6494 static int bnxt_cp_rings_in_use(struct bnxt *bp)
6495 {
6496 	int cp;
6497 
6498 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6499 		return bnxt_nq_rings_in_use(bp);
6500 
6501 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
6502 	return cp;
6503 }
6504 
6505 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
6506 {
6507 	int ulp_stat = bnxt_get_ulp_stat_ctxs(bp);
6508 	int cp = bp->cp_nr_rings;
6509 
6510 	if (!ulp_stat)
6511 		return cp;
6512 
6513 	if (bnxt_nq_rings_in_use(bp) > cp + bnxt_get_ulp_msix_num(bp))
6514 		return bnxt_get_ulp_msix_base(bp) + ulp_stat;
6515 
6516 	return cp + ulp_stat;
6517 }
6518 
6519 /* Check if a default RSS map needs to be setup.  This function is only
6520  * used on older firmware that does not require reserving RX rings.
6521  */
6522 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
6523 {
6524 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6525 
6526 	/* The RSS map is valid for RX rings set to resv_rx_rings */
6527 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
6528 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
6529 		if (!netif_is_rxfh_configured(bp->dev))
6530 			bnxt_set_dflt_rss_indir_tbl(bp);
6531 	}
6532 }
6533 
6534 static bool bnxt_need_reserve_rings(struct bnxt *bp)
6535 {
6536 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6537 	int cp = bnxt_cp_rings_in_use(bp);
6538 	int nq = bnxt_nq_rings_in_use(bp);
6539 	int rx = bp->rx_nr_rings, stat;
6540 	int vnic = 1, grp = rx;
6541 
6542 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
6543 	    bp->hwrm_spec_code >= 0x10601)
6544 		return true;
6545 
6546 	/* Old firmware does not need RX ring reservations but we still
6547 	 * need to setup a default RSS map when needed.  With new firmware
6548 	 * we go through RX ring reservations first and then set up the
6549 	 * RSS map for the successfully reserved RX rings when needed.
6550 	 */
6551 	if (!BNXT_NEW_RM(bp)) {
6552 		bnxt_check_rss_tbl_no_rmgr(bp);
6553 		return false;
6554 	}
6555 	if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
6556 		vnic = rx + 1;
6557 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
6558 		rx <<= 1;
6559 	stat = bnxt_get_func_stat_ctxs(bp);
6560 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
6561 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
6562 	    (hw_resc->resv_hw_ring_grps != grp &&
6563 	     !(bp->flags & BNXT_FLAG_CHIP_P5)))
6564 		return true;
6565 	if ((bp->flags & BNXT_FLAG_CHIP_P5) && BNXT_PF(bp) &&
6566 	    hw_resc->resv_irqs != nq)
6567 		return true;
6568 	return false;
6569 }
6570 
6571 static int __bnxt_reserve_rings(struct bnxt *bp)
6572 {
6573 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6574 	int cp = bnxt_nq_rings_in_use(bp);
6575 	int tx = bp->tx_nr_rings;
6576 	int rx = bp->rx_nr_rings;
6577 	int grp, rx_rings, rc;
6578 	int vnic = 1, stat;
6579 	bool sh = false;
6580 
6581 	if (!bnxt_need_reserve_rings(bp))
6582 		return 0;
6583 
6584 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
6585 		sh = true;
6586 	if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
6587 		vnic = rx + 1;
6588 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
6589 		rx <<= 1;
6590 	grp = bp->rx_nr_rings;
6591 	stat = bnxt_get_func_stat_ctxs(bp);
6592 
6593 	rc = bnxt_hwrm_reserve_rings(bp, tx, rx, grp, cp, stat, vnic);
6594 	if (rc)
6595 		return rc;
6596 
6597 	tx = hw_resc->resv_tx_rings;
6598 	if (BNXT_NEW_RM(bp)) {
6599 		rx = hw_resc->resv_rx_rings;
6600 		cp = hw_resc->resv_irqs;
6601 		grp = hw_resc->resv_hw_ring_grps;
6602 		vnic = hw_resc->resv_vnics;
6603 		stat = hw_resc->resv_stat_ctxs;
6604 	}
6605 
6606 	rx_rings = rx;
6607 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
6608 		if (rx >= 2) {
6609 			rx_rings = rx >> 1;
6610 		} else {
6611 			if (netif_running(bp->dev))
6612 				return -ENOMEM;
6613 
6614 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
6615 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
6616 			bp->dev->hw_features &= ~NETIF_F_LRO;
6617 			bp->dev->features &= ~NETIF_F_LRO;
6618 			bnxt_set_ring_params(bp);
6619 		}
6620 	}
6621 	rx_rings = min_t(int, rx_rings, grp);
6622 	cp = min_t(int, cp, bp->cp_nr_rings);
6623 	if (stat > bnxt_get_ulp_stat_ctxs(bp))
6624 		stat -= bnxt_get_ulp_stat_ctxs(bp);
6625 	cp = min_t(int, cp, stat);
6626 	rc = bnxt_trim_rings(bp, &rx_rings, &tx, cp, sh);
6627 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
6628 		rx = rx_rings << 1;
6629 	cp = sh ? max_t(int, tx, rx_rings) : tx + rx_rings;
6630 	bp->tx_nr_rings = tx;
6631 
6632 	/* If we cannot reserve all the RX rings, reset the RSS map only
6633 	 * if absolutely necessary
6634 	 */
6635 	if (rx_rings != bp->rx_nr_rings) {
6636 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
6637 			    rx_rings, bp->rx_nr_rings);
6638 		if (netif_is_rxfh_configured(bp->dev) &&
6639 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
6640 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
6641 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
6642 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
6643 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
6644 		}
6645 	}
6646 	bp->rx_nr_rings = rx_rings;
6647 	bp->cp_nr_rings = cp;
6648 
6649 	if (!tx || !rx || !cp || !grp || !vnic || !stat)
6650 		return -ENOMEM;
6651 
6652 	if (!netif_is_rxfh_configured(bp->dev))
6653 		bnxt_set_dflt_rss_indir_tbl(bp);
6654 
6655 	return rc;
6656 }
6657 
6658 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6659 				    int ring_grps, int cp_rings, int stats,
6660 				    int vnics)
6661 {
6662 	struct hwrm_func_vf_cfg_input *req;
6663 	u32 flags;
6664 
6665 	if (!BNXT_NEW_RM(bp))
6666 		return 0;
6667 
6668 	req = __bnxt_hwrm_reserve_vf_rings(bp, tx_rings, rx_rings, ring_grps,
6669 					   cp_rings, stats, vnics);
6670 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
6671 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
6672 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
6673 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
6674 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
6675 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
6676 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6677 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
6678 
6679 	req->flags = cpu_to_le32(flags);
6680 	return hwrm_req_send_silent(bp, req);
6681 }
6682 
6683 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6684 				    int ring_grps, int cp_rings, int stats,
6685 				    int vnics)
6686 {
6687 	struct hwrm_func_cfg_input *req;
6688 	u32 flags;
6689 
6690 	req = __bnxt_hwrm_reserve_pf_rings(bp, tx_rings, rx_rings, ring_grps,
6691 					   cp_rings, stats, vnics);
6692 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
6693 	if (BNXT_NEW_RM(bp)) {
6694 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
6695 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
6696 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
6697 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
6698 		if (bp->flags & BNXT_FLAG_CHIP_P5)
6699 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
6700 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
6701 		else
6702 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
6703 	}
6704 
6705 	req->flags = cpu_to_le32(flags);
6706 	return hwrm_req_send_silent(bp, req);
6707 }
6708 
6709 static int bnxt_hwrm_check_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6710 				 int ring_grps, int cp_rings, int stats,
6711 				 int vnics)
6712 {
6713 	if (bp->hwrm_spec_code < 0x10801)
6714 		return 0;
6715 
6716 	if (BNXT_PF(bp))
6717 		return bnxt_hwrm_check_pf_rings(bp, tx_rings, rx_rings,
6718 						ring_grps, cp_rings, stats,
6719 						vnics);
6720 
6721 	return bnxt_hwrm_check_vf_rings(bp, tx_rings, rx_rings, ring_grps,
6722 					cp_rings, stats, vnics);
6723 }
6724 
6725 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
6726 {
6727 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6728 	struct hwrm_ring_aggint_qcaps_output *resp;
6729 	struct hwrm_ring_aggint_qcaps_input *req;
6730 	int rc;
6731 
6732 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
6733 	coal_cap->num_cmpl_dma_aggr_max = 63;
6734 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
6735 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
6736 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
6737 	coal_cap->int_lat_tmr_min_max = 65535;
6738 	coal_cap->int_lat_tmr_max_max = 65535;
6739 	coal_cap->num_cmpl_aggr_int_max = 65535;
6740 	coal_cap->timer_units = 80;
6741 
6742 	if (bp->hwrm_spec_code < 0x10902)
6743 		return;
6744 
6745 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
6746 		return;
6747 
6748 	resp = hwrm_req_hold(bp, req);
6749 	rc = hwrm_req_send_silent(bp, req);
6750 	if (!rc) {
6751 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
6752 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
6753 		coal_cap->num_cmpl_dma_aggr_max =
6754 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
6755 		coal_cap->num_cmpl_dma_aggr_during_int_max =
6756 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
6757 		coal_cap->cmpl_aggr_dma_tmr_max =
6758 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
6759 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
6760 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
6761 		coal_cap->int_lat_tmr_min_max =
6762 			le16_to_cpu(resp->int_lat_tmr_min_max);
6763 		coal_cap->int_lat_tmr_max_max =
6764 			le16_to_cpu(resp->int_lat_tmr_max_max);
6765 		coal_cap->num_cmpl_aggr_int_max =
6766 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
6767 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
6768 	}
6769 	hwrm_req_drop(bp, req);
6770 }
6771 
6772 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
6773 {
6774 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6775 
6776 	return usec * 1000 / coal_cap->timer_units;
6777 }
6778 
6779 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
6780 	struct bnxt_coal *hw_coal,
6781 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
6782 {
6783 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6784 	u16 val, tmr, max, flags = hw_coal->flags;
6785 	u32 cmpl_params = coal_cap->cmpl_params;
6786 
6787 	max = hw_coal->bufs_per_record * 128;
6788 	if (hw_coal->budget)
6789 		max = hw_coal->bufs_per_record * hw_coal->budget;
6790 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
6791 
6792 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
6793 	req->num_cmpl_aggr_int = cpu_to_le16(val);
6794 
6795 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
6796 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
6797 
6798 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
6799 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
6800 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
6801 
6802 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
6803 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
6804 	req->int_lat_tmr_max = cpu_to_le16(tmr);
6805 
6806 	/* min timer set to 1/2 of interrupt timer */
6807 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
6808 		val = tmr / 2;
6809 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
6810 		req->int_lat_tmr_min = cpu_to_le16(val);
6811 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
6812 	}
6813 
6814 	/* buf timer set to 1/4 of interrupt timer */
6815 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
6816 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
6817 
6818 	if (cmpl_params &
6819 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
6820 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
6821 		val = clamp_t(u16, tmr, 1,
6822 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
6823 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
6824 		req->enables |=
6825 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
6826 	}
6827 
6828 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
6829 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
6830 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
6831 	req->flags = cpu_to_le16(flags);
6832 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
6833 }
6834 
6835 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
6836 				   struct bnxt_coal *hw_coal)
6837 {
6838 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
6839 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6840 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6841 	u32 nq_params = coal_cap->nq_params;
6842 	u16 tmr;
6843 	int rc;
6844 
6845 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
6846 		return 0;
6847 
6848 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
6849 	if (rc)
6850 		return rc;
6851 
6852 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
6853 	req->flags =
6854 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
6855 
6856 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
6857 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
6858 	req->int_lat_tmr_min = cpu_to_le16(tmr);
6859 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
6860 	return hwrm_req_send(bp, req);
6861 }
6862 
6863 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
6864 {
6865 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
6866 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6867 	struct bnxt_coal coal;
6868 	int rc;
6869 
6870 	/* Tick values in micro seconds.
6871 	 * 1 coal_buf x bufs_per_record = 1 completion record.
6872 	 */
6873 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
6874 
6875 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
6876 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
6877 
6878 	if (!bnapi->rx_ring)
6879 		return -ENODEV;
6880 
6881 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
6882 	if (rc)
6883 		return rc;
6884 
6885 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
6886 
6887 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
6888 
6889 	return hwrm_req_send(bp, req_rx);
6890 }
6891 
6892 int bnxt_hwrm_set_coal(struct bnxt *bp)
6893 {
6894 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx,
6895 							   *req;
6896 	int i, rc;
6897 
6898 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
6899 	if (rc)
6900 		return rc;
6901 
6902 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
6903 	if (rc) {
6904 		hwrm_req_drop(bp, req_rx);
6905 		return rc;
6906 	}
6907 
6908 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
6909 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
6910 
6911 	hwrm_req_hold(bp, req_rx);
6912 	hwrm_req_hold(bp, req_tx);
6913 	for (i = 0; i < bp->cp_nr_rings; i++) {
6914 		struct bnxt_napi *bnapi = bp->bnapi[i];
6915 		struct bnxt_coal *hw_coal;
6916 		u16 ring_id;
6917 
6918 		req = req_rx;
6919 		if (!bnapi->rx_ring) {
6920 			ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
6921 			req = req_tx;
6922 		} else {
6923 			ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
6924 		}
6925 		req->ring_id = cpu_to_le16(ring_id);
6926 
6927 		rc = hwrm_req_send(bp, req);
6928 		if (rc)
6929 			break;
6930 
6931 		if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6932 			continue;
6933 
6934 		if (bnapi->rx_ring && bnapi->tx_ring) {
6935 			req = req_tx;
6936 			ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
6937 			req->ring_id = cpu_to_le16(ring_id);
6938 			rc = hwrm_req_send(bp, req);
6939 			if (rc)
6940 				break;
6941 		}
6942 		if (bnapi->rx_ring)
6943 			hw_coal = &bp->rx_coal;
6944 		else
6945 			hw_coal = &bp->tx_coal;
6946 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
6947 	}
6948 	hwrm_req_drop(bp, req_rx);
6949 	hwrm_req_drop(bp, req_tx);
6950 	return rc;
6951 }
6952 
6953 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
6954 {
6955 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
6956 	struct hwrm_stat_ctx_free_input *req;
6957 	int i;
6958 
6959 	if (!bp->bnapi)
6960 		return;
6961 
6962 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6963 		return;
6964 
6965 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
6966 		return;
6967 	if (BNXT_FW_MAJ(bp) <= 20) {
6968 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
6969 			hwrm_req_drop(bp, req);
6970 			return;
6971 		}
6972 		hwrm_req_hold(bp, req0);
6973 	}
6974 	hwrm_req_hold(bp, req);
6975 	for (i = 0; i < bp->cp_nr_rings; i++) {
6976 		struct bnxt_napi *bnapi = bp->bnapi[i];
6977 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6978 
6979 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
6980 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
6981 			if (req0) {
6982 				req0->stat_ctx_id = req->stat_ctx_id;
6983 				hwrm_req_send(bp, req0);
6984 			}
6985 			hwrm_req_send(bp, req);
6986 
6987 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
6988 		}
6989 	}
6990 	hwrm_req_drop(bp, req);
6991 	if (req0)
6992 		hwrm_req_drop(bp, req0);
6993 }
6994 
6995 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
6996 {
6997 	struct hwrm_stat_ctx_alloc_output *resp;
6998 	struct hwrm_stat_ctx_alloc_input *req;
6999 	int rc, i;
7000 
7001 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
7002 		return 0;
7003 
7004 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
7005 	if (rc)
7006 		return rc;
7007 
7008 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
7009 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
7010 
7011 	resp = hwrm_req_hold(bp, req);
7012 	for (i = 0; i < bp->cp_nr_rings; i++) {
7013 		struct bnxt_napi *bnapi = bp->bnapi[i];
7014 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7015 
7016 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
7017 
7018 		rc = hwrm_req_send(bp, req);
7019 		if (rc)
7020 			break;
7021 
7022 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
7023 
7024 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
7025 	}
7026 	hwrm_req_drop(bp, req);
7027 	return rc;
7028 }
7029 
7030 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
7031 {
7032 	struct hwrm_func_qcfg_output *resp;
7033 	struct hwrm_func_qcfg_input *req;
7034 	u32 min_db_offset = 0;
7035 	u16 flags;
7036 	int rc;
7037 
7038 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7039 	if (rc)
7040 		return rc;
7041 
7042 	req->fid = cpu_to_le16(0xffff);
7043 	resp = hwrm_req_hold(bp, req);
7044 	rc = hwrm_req_send(bp, req);
7045 	if (rc)
7046 		goto func_qcfg_exit;
7047 
7048 #ifdef CONFIG_BNXT_SRIOV
7049 	if (BNXT_VF(bp)) {
7050 		struct bnxt_vf_info *vf = &bp->vf;
7051 
7052 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
7053 	} else {
7054 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
7055 	}
7056 #endif
7057 	flags = le16_to_cpu(resp->flags);
7058 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
7059 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
7060 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
7061 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
7062 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
7063 	}
7064 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
7065 		bp->flags |= BNXT_FLAG_MULTI_HOST;
7066 
7067 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
7068 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
7069 
7070 	switch (resp->port_partition_type) {
7071 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
7072 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
7073 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
7074 		bp->port_partition_type = resp->port_partition_type;
7075 		break;
7076 	}
7077 	if (bp->hwrm_spec_code < 0x10707 ||
7078 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
7079 		bp->br_mode = BRIDGE_MODE_VEB;
7080 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
7081 		bp->br_mode = BRIDGE_MODE_VEPA;
7082 	else
7083 		bp->br_mode = BRIDGE_MODE_UNDEF;
7084 
7085 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
7086 	if (!bp->max_mtu)
7087 		bp->max_mtu = BNXT_MAX_MTU;
7088 
7089 	if (bp->db_size)
7090 		goto func_qcfg_exit;
7091 
7092 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
7093 		if (BNXT_PF(bp))
7094 			min_db_offset = DB_PF_OFFSET_P5;
7095 		else
7096 			min_db_offset = DB_VF_OFFSET_P5;
7097 	}
7098 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
7099 				 1024);
7100 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
7101 	    bp->db_size <= min_db_offset)
7102 		bp->db_size = pci_resource_len(bp->pdev, 2);
7103 
7104 func_qcfg_exit:
7105 	hwrm_req_drop(bp, req);
7106 	return rc;
7107 }
7108 
7109 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_info *ctx,
7110 			struct hwrm_func_backing_store_qcaps_output *resp)
7111 {
7112 	struct bnxt_mem_init *mem_init;
7113 	u16 init_mask;
7114 	u8 init_val;
7115 	u8 *offset;
7116 	int i;
7117 
7118 	init_val = resp->ctx_kind_initializer;
7119 	init_mask = le16_to_cpu(resp->ctx_init_mask);
7120 	offset = &resp->qp_init_offset;
7121 	mem_init = &ctx->mem_init[BNXT_CTX_MEM_INIT_QP];
7122 	for (i = 0; i < BNXT_CTX_MEM_INIT_MAX; i++, mem_init++, offset++) {
7123 		mem_init->init_val = init_val;
7124 		mem_init->offset = BNXT_MEM_INVALID_OFFSET;
7125 		if (!init_mask)
7126 			continue;
7127 		if (i == BNXT_CTX_MEM_INIT_STAT)
7128 			offset = &resp->stat_init_offset;
7129 		if (init_mask & (1 << i))
7130 			mem_init->offset = *offset * 4;
7131 		else
7132 			mem_init->init_val = 0;
7133 	}
7134 	ctx->mem_init[BNXT_CTX_MEM_INIT_QP].size = ctx->qp_entry_size;
7135 	ctx->mem_init[BNXT_CTX_MEM_INIT_SRQ].size = ctx->srq_entry_size;
7136 	ctx->mem_init[BNXT_CTX_MEM_INIT_CQ].size = ctx->cq_entry_size;
7137 	ctx->mem_init[BNXT_CTX_MEM_INIT_VNIC].size = ctx->vnic_entry_size;
7138 	ctx->mem_init[BNXT_CTX_MEM_INIT_STAT].size = ctx->stat_entry_size;
7139 	ctx->mem_init[BNXT_CTX_MEM_INIT_MRAV].size = ctx->mrav_entry_size;
7140 }
7141 
7142 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
7143 {
7144 	struct hwrm_func_backing_store_qcaps_output *resp;
7145 	struct hwrm_func_backing_store_qcaps_input *req;
7146 	int rc;
7147 
7148 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx)
7149 		return 0;
7150 
7151 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
7152 	if (rc)
7153 		return rc;
7154 
7155 	resp = hwrm_req_hold(bp, req);
7156 	rc = hwrm_req_send_silent(bp, req);
7157 	if (!rc) {
7158 		struct bnxt_ctx_pg_info *ctx_pg;
7159 		struct bnxt_ctx_mem_info *ctx;
7160 		int i, tqm_rings;
7161 
7162 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
7163 		if (!ctx) {
7164 			rc = -ENOMEM;
7165 			goto ctx_err;
7166 		}
7167 		ctx->qp_max_entries = le32_to_cpu(resp->qp_max_entries);
7168 		ctx->qp_min_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
7169 		ctx->qp_max_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
7170 		ctx->qp_entry_size = le16_to_cpu(resp->qp_entry_size);
7171 		ctx->srq_max_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
7172 		ctx->srq_max_entries = le32_to_cpu(resp->srq_max_entries);
7173 		ctx->srq_entry_size = le16_to_cpu(resp->srq_entry_size);
7174 		ctx->cq_max_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
7175 		ctx->cq_max_entries = le32_to_cpu(resp->cq_max_entries);
7176 		ctx->cq_entry_size = le16_to_cpu(resp->cq_entry_size);
7177 		ctx->vnic_max_vnic_entries =
7178 			le16_to_cpu(resp->vnic_max_vnic_entries);
7179 		ctx->vnic_max_ring_table_entries =
7180 			le16_to_cpu(resp->vnic_max_ring_table_entries);
7181 		ctx->vnic_entry_size = le16_to_cpu(resp->vnic_entry_size);
7182 		ctx->stat_max_entries = le32_to_cpu(resp->stat_max_entries);
7183 		ctx->stat_entry_size = le16_to_cpu(resp->stat_entry_size);
7184 		ctx->tqm_entry_size = le16_to_cpu(resp->tqm_entry_size);
7185 		ctx->tqm_min_entries_per_ring =
7186 			le32_to_cpu(resp->tqm_min_entries_per_ring);
7187 		ctx->tqm_max_entries_per_ring =
7188 			le32_to_cpu(resp->tqm_max_entries_per_ring);
7189 		ctx->tqm_entries_multiple = resp->tqm_entries_multiple;
7190 		if (!ctx->tqm_entries_multiple)
7191 			ctx->tqm_entries_multiple = 1;
7192 		ctx->mrav_max_entries = le32_to_cpu(resp->mrav_max_entries);
7193 		ctx->mrav_entry_size = le16_to_cpu(resp->mrav_entry_size);
7194 		ctx->mrav_num_entries_units =
7195 			le16_to_cpu(resp->mrav_num_entries_units);
7196 		ctx->tim_entry_size = le16_to_cpu(resp->tim_entry_size);
7197 		ctx->tim_max_entries = le32_to_cpu(resp->tim_max_entries);
7198 
7199 		bnxt_init_ctx_initializer(ctx, resp);
7200 
7201 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
7202 		if (!ctx->tqm_fp_rings_count)
7203 			ctx->tqm_fp_rings_count = bp->max_q;
7204 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
7205 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
7206 
7207 		tqm_rings = ctx->tqm_fp_rings_count + BNXT_MAX_TQM_SP_RINGS;
7208 		ctx_pg = kcalloc(tqm_rings, sizeof(*ctx_pg), GFP_KERNEL);
7209 		if (!ctx_pg) {
7210 			kfree(ctx);
7211 			rc = -ENOMEM;
7212 			goto ctx_err;
7213 		}
7214 		for (i = 0; i < tqm_rings; i++, ctx_pg++)
7215 			ctx->tqm_mem[i] = ctx_pg;
7216 		bp->ctx = ctx;
7217 	} else {
7218 		rc = 0;
7219 	}
7220 ctx_err:
7221 	hwrm_req_drop(bp, req);
7222 	return rc;
7223 }
7224 
7225 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
7226 				  __le64 *pg_dir)
7227 {
7228 	if (!rmem->nr_pages)
7229 		return;
7230 
7231 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
7232 	if (rmem->depth >= 1) {
7233 		if (rmem->depth == 2)
7234 			*pg_attr |= 2;
7235 		else
7236 			*pg_attr |= 1;
7237 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
7238 	} else {
7239 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
7240 	}
7241 }
7242 
7243 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
7244 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
7245 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
7246 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
7247 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
7248 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
7249 
7250 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
7251 {
7252 	struct hwrm_func_backing_store_cfg_input *req;
7253 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
7254 	struct bnxt_ctx_pg_info *ctx_pg;
7255 	void **__req = (void **)&req;
7256 	u32 req_len = sizeof(*req);
7257 	__le32 *num_entries;
7258 	__le64 *pg_dir;
7259 	u32 flags = 0;
7260 	u8 *pg_attr;
7261 	u32 ena;
7262 	int rc;
7263 	int i;
7264 
7265 	if (!ctx)
7266 		return 0;
7267 
7268 	if (req_len > bp->hwrm_max_ext_req_len)
7269 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
7270 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
7271 	if (rc)
7272 		return rc;
7273 
7274 	req->enables = cpu_to_le32(enables);
7275 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
7276 		ctx_pg = &ctx->qp_mem;
7277 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
7278 		req->qp_num_qp1_entries = cpu_to_le16(ctx->qp_min_qp1_entries);
7279 		req->qp_num_l2_entries = cpu_to_le16(ctx->qp_max_l2_entries);
7280 		req->qp_entry_size = cpu_to_le16(ctx->qp_entry_size);
7281 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7282 				      &req->qpc_pg_size_qpc_lvl,
7283 				      &req->qpc_page_dir);
7284 	}
7285 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
7286 		ctx_pg = &ctx->srq_mem;
7287 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
7288 		req->srq_num_l2_entries = cpu_to_le16(ctx->srq_max_l2_entries);
7289 		req->srq_entry_size = cpu_to_le16(ctx->srq_entry_size);
7290 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7291 				      &req->srq_pg_size_srq_lvl,
7292 				      &req->srq_page_dir);
7293 	}
7294 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
7295 		ctx_pg = &ctx->cq_mem;
7296 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
7297 		req->cq_num_l2_entries = cpu_to_le16(ctx->cq_max_l2_entries);
7298 		req->cq_entry_size = cpu_to_le16(ctx->cq_entry_size);
7299 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7300 				      &req->cq_pg_size_cq_lvl,
7301 				      &req->cq_page_dir);
7302 	}
7303 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
7304 		ctx_pg = &ctx->vnic_mem;
7305 		req->vnic_num_vnic_entries =
7306 			cpu_to_le16(ctx->vnic_max_vnic_entries);
7307 		req->vnic_num_ring_table_entries =
7308 			cpu_to_le16(ctx->vnic_max_ring_table_entries);
7309 		req->vnic_entry_size = cpu_to_le16(ctx->vnic_entry_size);
7310 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7311 				      &req->vnic_pg_size_vnic_lvl,
7312 				      &req->vnic_page_dir);
7313 	}
7314 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
7315 		ctx_pg = &ctx->stat_mem;
7316 		req->stat_num_entries = cpu_to_le32(ctx->stat_max_entries);
7317 		req->stat_entry_size = cpu_to_le16(ctx->stat_entry_size);
7318 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7319 				      &req->stat_pg_size_stat_lvl,
7320 				      &req->stat_page_dir);
7321 	}
7322 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
7323 		ctx_pg = &ctx->mrav_mem;
7324 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
7325 		if (ctx->mrav_num_entries_units)
7326 			flags |=
7327 			FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
7328 		req->mrav_entry_size = cpu_to_le16(ctx->mrav_entry_size);
7329 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7330 				      &req->mrav_pg_size_mrav_lvl,
7331 				      &req->mrav_page_dir);
7332 	}
7333 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
7334 		ctx_pg = &ctx->tim_mem;
7335 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
7336 		req->tim_entry_size = cpu_to_le16(ctx->tim_entry_size);
7337 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7338 				      &req->tim_pg_size_tim_lvl,
7339 				      &req->tim_page_dir);
7340 	}
7341 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
7342 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
7343 	     pg_dir = &req->tqm_sp_page_dir,
7344 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP;
7345 	     i < BNXT_MAX_TQM_RINGS;
7346 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
7347 		if (!(enables & ena))
7348 			continue;
7349 
7350 		req->tqm_entry_size = cpu_to_le16(ctx->tqm_entry_size);
7351 		ctx_pg = ctx->tqm_mem[i];
7352 		*num_entries = cpu_to_le32(ctx_pg->entries);
7353 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
7354 	}
7355 	req->flags = cpu_to_le32(flags);
7356 	return hwrm_req_send(bp, req);
7357 }
7358 
7359 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
7360 				  struct bnxt_ctx_pg_info *ctx_pg)
7361 {
7362 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7363 
7364 	rmem->page_size = BNXT_PAGE_SIZE;
7365 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
7366 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
7367 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
7368 	if (rmem->depth >= 1)
7369 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
7370 	return bnxt_alloc_ring(bp, rmem);
7371 }
7372 
7373 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
7374 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
7375 				  u8 depth, struct bnxt_mem_init *mem_init)
7376 {
7377 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7378 	int rc;
7379 
7380 	if (!mem_size)
7381 		return -EINVAL;
7382 
7383 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
7384 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
7385 		ctx_pg->nr_pages = 0;
7386 		return -EINVAL;
7387 	}
7388 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
7389 		int nr_tbls, i;
7390 
7391 		rmem->depth = 2;
7392 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
7393 					     GFP_KERNEL);
7394 		if (!ctx_pg->ctx_pg_tbl)
7395 			return -ENOMEM;
7396 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
7397 		rmem->nr_pages = nr_tbls;
7398 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
7399 		if (rc)
7400 			return rc;
7401 		for (i = 0; i < nr_tbls; i++) {
7402 			struct bnxt_ctx_pg_info *pg_tbl;
7403 
7404 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
7405 			if (!pg_tbl)
7406 				return -ENOMEM;
7407 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
7408 			rmem = &pg_tbl->ring_mem;
7409 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
7410 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
7411 			rmem->depth = 1;
7412 			rmem->nr_pages = MAX_CTX_PAGES;
7413 			rmem->mem_init = mem_init;
7414 			if (i == (nr_tbls - 1)) {
7415 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
7416 
7417 				if (rem)
7418 					rmem->nr_pages = rem;
7419 			}
7420 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
7421 			if (rc)
7422 				break;
7423 		}
7424 	} else {
7425 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
7426 		if (rmem->nr_pages > 1 || depth)
7427 			rmem->depth = 1;
7428 		rmem->mem_init = mem_init;
7429 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
7430 	}
7431 	return rc;
7432 }
7433 
7434 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
7435 				  struct bnxt_ctx_pg_info *ctx_pg)
7436 {
7437 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7438 
7439 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
7440 	    ctx_pg->ctx_pg_tbl) {
7441 		int i, nr_tbls = rmem->nr_pages;
7442 
7443 		for (i = 0; i < nr_tbls; i++) {
7444 			struct bnxt_ctx_pg_info *pg_tbl;
7445 			struct bnxt_ring_mem_info *rmem2;
7446 
7447 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
7448 			if (!pg_tbl)
7449 				continue;
7450 			rmem2 = &pg_tbl->ring_mem;
7451 			bnxt_free_ring(bp, rmem2);
7452 			ctx_pg->ctx_pg_arr[i] = NULL;
7453 			kfree(pg_tbl);
7454 			ctx_pg->ctx_pg_tbl[i] = NULL;
7455 		}
7456 		kfree(ctx_pg->ctx_pg_tbl);
7457 		ctx_pg->ctx_pg_tbl = NULL;
7458 	}
7459 	bnxt_free_ring(bp, rmem);
7460 	ctx_pg->nr_pages = 0;
7461 }
7462 
7463 void bnxt_free_ctx_mem(struct bnxt *bp)
7464 {
7465 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
7466 	int i;
7467 
7468 	if (!ctx)
7469 		return;
7470 
7471 	if (ctx->tqm_mem[0]) {
7472 		for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
7473 			bnxt_free_ctx_pg_tbls(bp, ctx->tqm_mem[i]);
7474 		kfree(ctx->tqm_mem[0]);
7475 		ctx->tqm_mem[0] = NULL;
7476 	}
7477 
7478 	bnxt_free_ctx_pg_tbls(bp, &ctx->tim_mem);
7479 	bnxt_free_ctx_pg_tbls(bp, &ctx->mrav_mem);
7480 	bnxt_free_ctx_pg_tbls(bp, &ctx->stat_mem);
7481 	bnxt_free_ctx_pg_tbls(bp, &ctx->vnic_mem);
7482 	bnxt_free_ctx_pg_tbls(bp, &ctx->cq_mem);
7483 	bnxt_free_ctx_pg_tbls(bp, &ctx->srq_mem);
7484 	bnxt_free_ctx_pg_tbls(bp, &ctx->qp_mem);
7485 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
7486 }
7487 
7488 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
7489 {
7490 	struct bnxt_ctx_pg_info *ctx_pg;
7491 	struct bnxt_ctx_mem_info *ctx;
7492 	struct bnxt_mem_init *init;
7493 	u32 mem_size, ena, entries;
7494 	u32 entries_sp, min;
7495 	u32 num_mr, num_ah;
7496 	u32 extra_srqs = 0;
7497 	u32 extra_qps = 0;
7498 	u8 pg_lvl = 1;
7499 	int i, rc;
7500 
7501 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
7502 	if (rc) {
7503 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
7504 			   rc);
7505 		return rc;
7506 	}
7507 	ctx = bp->ctx;
7508 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
7509 		return 0;
7510 
7511 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
7512 		pg_lvl = 2;
7513 		extra_qps = 65536;
7514 		extra_srqs = 8192;
7515 	}
7516 
7517 	ctx_pg = &ctx->qp_mem;
7518 	ctx_pg->entries = ctx->qp_min_qp1_entries + ctx->qp_max_l2_entries +
7519 			  extra_qps;
7520 	if (ctx->qp_entry_size) {
7521 		mem_size = ctx->qp_entry_size * ctx_pg->entries;
7522 		init = &ctx->mem_init[BNXT_CTX_MEM_INIT_QP];
7523 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, init);
7524 		if (rc)
7525 			return rc;
7526 	}
7527 
7528 	ctx_pg = &ctx->srq_mem;
7529 	ctx_pg->entries = ctx->srq_max_l2_entries + extra_srqs;
7530 	if (ctx->srq_entry_size) {
7531 		mem_size = ctx->srq_entry_size * ctx_pg->entries;
7532 		init = &ctx->mem_init[BNXT_CTX_MEM_INIT_SRQ];
7533 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, init);
7534 		if (rc)
7535 			return rc;
7536 	}
7537 
7538 	ctx_pg = &ctx->cq_mem;
7539 	ctx_pg->entries = ctx->cq_max_l2_entries + extra_qps * 2;
7540 	if (ctx->cq_entry_size) {
7541 		mem_size = ctx->cq_entry_size * ctx_pg->entries;
7542 		init = &ctx->mem_init[BNXT_CTX_MEM_INIT_CQ];
7543 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, init);
7544 		if (rc)
7545 			return rc;
7546 	}
7547 
7548 	ctx_pg = &ctx->vnic_mem;
7549 	ctx_pg->entries = ctx->vnic_max_vnic_entries +
7550 			  ctx->vnic_max_ring_table_entries;
7551 	if (ctx->vnic_entry_size) {
7552 		mem_size = ctx->vnic_entry_size * ctx_pg->entries;
7553 		init = &ctx->mem_init[BNXT_CTX_MEM_INIT_VNIC];
7554 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, init);
7555 		if (rc)
7556 			return rc;
7557 	}
7558 
7559 	ctx_pg = &ctx->stat_mem;
7560 	ctx_pg->entries = ctx->stat_max_entries;
7561 	if (ctx->stat_entry_size) {
7562 		mem_size = ctx->stat_entry_size * ctx_pg->entries;
7563 		init = &ctx->mem_init[BNXT_CTX_MEM_INIT_STAT];
7564 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, init);
7565 		if (rc)
7566 			return rc;
7567 	}
7568 
7569 	ena = 0;
7570 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
7571 		goto skip_rdma;
7572 
7573 	ctx_pg = &ctx->mrav_mem;
7574 	/* 128K extra is needed to accommodate static AH context
7575 	 * allocation by f/w.
7576 	 */
7577 	num_mr = 1024 * 256;
7578 	num_ah = 1024 * 128;
7579 	ctx_pg->entries = num_mr + num_ah;
7580 	if (ctx->mrav_entry_size) {
7581 		mem_size = ctx->mrav_entry_size * ctx_pg->entries;
7582 		init = &ctx->mem_init[BNXT_CTX_MEM_INIT_MRAV];
7583 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 2, init);
7584 		if (rc)
7585 			return rc;
7586 	}
7587 	ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
7588 	if (ctx->mrav_num_entries_units)
7589 		ctx_pg->entries =
7590 			((num_mr / ctx->mrav_num_entries_units) << 16) |
7591 			 (num_ah / ctx->mrav_num_entries_units);
7592 
7593 	ctx_pg = &ctx->tim_mem;
7594 	ctx_pg->entries = ctx->qp_mem.entries;
7595 	if (ctx->tim_entry_size) {
7596 		mem_size = ctx->tim_entry_size * ctx_pg->entries;
7597 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, NULL);
7598 		if (rc)
7599 			return rc;
7600 	}
7601 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
7602 
7603 skip_rdma:
7604 	min = ctx->tqm_min_entries_per_ring;
7605 	entries_sp = ctx->vnic_max_vnic_entries + ctx->qp_max_l2_entries +
7606 		     2 * (extra_qps + ctx->qp_min_qp1_entries) + min;
7607 	entries_sp = roundup(entries_sp, ctx->tqm_entries_multiple);
7608 	entries = ctx->qp_max_l2_entries + 2 * (extra_qps + ctx->qp_min_qp1_entries);
7609 	entries = roundup(entries, ctx->tqm_entries_multiple);
7610 	entries = clamp_t(u32, entries, min, ctx->tqm_max_entries_per_ring);
7611 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++) {
7612 		ctx_pg = ctx->tqm_mem[i];
7613 		ctx_pg->entries = i ? entries : entries_sp;
7614 		if (ctx->tqm_entry_size) {
7615 			mem_size = ctx->tqm_entry_size * ctx_pg->entries;
7616 			rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1,
7617 						    NULL);
7618 			if (rc)
7619 				return rc;
7620 		}
7621 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
7622 	}
7623 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
7624 	rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
7625 	if (rc) {
7626 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
7627 			   rc);
7628 		return rc;
7629 	}
7630 	ctx->flags |= BNXT_CTX_FLAG_INITED;
7631 	return 0;
7632 }
7633 
7634 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
7635 {
7636 	struct hwrm_func_resource_qcaps_output *resp;
7637 	struct hwrm_func_resource_qcaps_input *req;
7638 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7639 	int rc;
7640 
7641 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
7642 	if (rc)
7643 		return rc;
7644 
7645 	req->fid = cpu_to_le16(0xffff);
7646 	resp = hwrm_req_hold(bp, req);
7647 	rc = hwrm_req_send_silent(bp, req);
7648 	if (rc)
7649 		goto hwrm_func_resc_qcaps_exit;
7650 
7651 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
7652 	if (!all)
7653 		goto hwrm_func_resc_qcaps_exit;
7654 
7655 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
7656 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
7657 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
7658 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
7659 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
7660 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
7661 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
7662 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
7663 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
7664 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
7665 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
7666 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
7667 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
7668 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
7669 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
7670 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
7671 
7672 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
7673 		u16 max_msix = le16_to_cpu(resp->max_msix);
7674 
7675 		hw_resc->max_nqs = max_msix;
7676 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
7677 	}
7678 
7679 	if (BNXT_PF(bp)) {
7680 		struct bnxt_pf_info *pf = &bp->pf;
7681 
7682 		pf->vf_resv_strategy =
7683 			le16_to_cpu(resp->vf_reservation_strategy);
7684 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
7685 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
7686 	}
7687 hwrm_func_resc_qcaps_exit:
7688 	hwrm_req_drop(bp, req);
7689 	return rc;
7690 }
7691 
7692 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
7693 {
7694 	struct hwrm_port_mac_ptp_qcfg_output *resp;
7695 	struct hwrm_port_mac_ptp_qcfg_input *req;
7696 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
7697 	bool phc_cfg;
7698 	u8 flags;
7699 	int rc;
7700 
7701 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_THOR(bp)) {
7702 		rc = -ENODEV;
7703 		goto no_ptp;
7704 	}
7705 
7706 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
7707 	if (rc)
7708 		goto no_ptp;
7709 
7710 	req->port_id = cpu_to_le16(bp->pf.port_id);
7711 	resp = hwrm_req_hold(bp, req);
7712 	rc = hwrm_req_send(bp, req);
7713 	if (rc)
7714 		goto exit;
7715 
7716 	flags = resp->flags;
7717 	if (!(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
7718 		rc = -ENODEV;
7719 		goto exit;
7720 	}
7721 	if (!ptp) {
7722 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
7723 		if (!ptp) {
7724 			rc = -ENOMEM;
7725 			goto exit;
7726 		}
7727 		ptp->bp = bp;
7728 		bp->ptp_cfg = ptp;
7729 	}
7730 	if (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK) {
7731 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
7732 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
7733 	} else if (bp->flags & BNXT_FLAG_CHIP_P5) {
7734 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
7735 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
7736 	} else {
7737 		rc = -ENODEV;
7738 		goto exit;
7739 	}
7740 	phc_cfg = (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
7741 	rc = bnxt_ptp_init(bp, phc_cfg);
7742 	if (rc)
7743 		netdev_warn(bp->dev, "PTP initialization failed.\n");
7744 exit:
7745 	hwrm_req_drop(bp, req);
7746 	if (!rc)
7747 		return 0;
7748 
7749 no_ptp:
7750 	bnxt_ptp_clear(bp);
7751 	kfree(ptp);
7752 	bp->ptp_cfg = NULL;
7753 	return rc;
7754 }
7755 
7756 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
7757 {
7758 	struct hwrm_func_qcaps_output *resp;
7759 	struct hwrm_func_qcaps_input *req;
7760 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7761 	u32 flags, flags_ext, flags_ext2;
7762 	int rc;
7763 
7764 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
7765 	if (rc)
7766 		return rc;
7767 
7768 	req->fid = cpu_to_le16(0xffff);
7769 	resp = hwrm_req_hold(bp, req);
7770 	rc = hwrm_req_send(bp, req);
7771 	if (rc)
7772 		goto hwrm_func_qcaps_exit;
7773 
7774 	flags = le32_to_cpu(resp->flags);
7775 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
7776 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
7777 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
7778 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
7779 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
7780 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
7781 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
7782 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
7783 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
7784 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
7785 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
7786 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
7787 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
7788 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
7789 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
7790 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
7791 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
7792 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
7793 
7794 	flags_ext = le32_to_cpu(resp->flags_ext);
7795 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
7796 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
7797 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
7798 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
7799 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
7800 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
7801 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
7802 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
7803 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
7804 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
7805 
7806 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
7807 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
7808 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
7809 
7810 	bp->tx_push_thresh = 0;
7811 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
7812 	    BNXT_FW_MAJ(bp) > 217)
7813 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
7814 
7815 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
7816 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
7817 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
7818 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
7819 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
7820 	if (!hw_resc->max_hw_ring_grps)
7821 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
7822 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
7823 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
7824 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
7825 
7826 	if (BNXT_PF(bp)) {
7827 		struct bnxt_pf_info *pf = &bp->pf;
7828 
7829 		pf->fw_fid = le16_to_cpu(resp->fid);
7830 		pf->port_id = le16_to_cpu(resp->port_id);
7831 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
7832 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
7833 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
7834 		pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
7835 		pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
7836 		pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
7837 		pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
7838 		pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
7839 		pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
7840 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
7841 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
7842 			bp->flags |= BNXT_FLAG_WOL_CAP;
7843 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
7844 			bp->fw_cap |= BNXT_FW_CAP_PTP;
7845 		} else {
7846 			bnxt_ptp_clear(bp);
7847 			kfree(bp->ptp_cfg);
7848 			bp->ptp_cfg = NULL;
7849 		}
7850 	} else {
7851 #ifdef CONFIG_BNXT_SRIOV
7852 		struct bnxt_vf_info *vf = &bp->vf;
7853 
7854 		vf->fw_fid = le16_to_cpu(resp->fid);
7855 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
7856 #endif
7857 	}
7858 
7859 hwrm_func_qcaps_exit:
7860 	hwrm_req_drop(bp, req);
7861 	return rc;
7862 }
7863 
7864 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
7865 {
7866 	struct hwrm_dbg_qcaps_output *resp;
7867 	struct hwrm_dbg_qcaps_input *req;
7868 	int rc;
7869 
7870 	bp->fw_dbg_cap = 0;
7871 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
7872 		return;
7873 
7874 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
7875 	if (rc)
7876 		return;
7877 
7878 	req->fid = cpu_to_le16(0xffff);
7879 	resp = hwrm_req_hold(bp, req);
7880 	rc = hwrm_req_send(bp, req);
7881 	if (rc)
7882 		goto hwrm_dbg_qcaps_exit;
7883 
7884 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
7885 
7886 hwrm_dbg_qcaps_exit:
7887 	hwrm_req_drop(bp, req);
7888 }
7889 
7890 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
7891 
7892 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
7893 {
7894 	int rc;
7895 
7896 	rc = __bnxt_hwrm_func_qcaps(bp);
7897 	if (rc)
7898 		return rc;
7899 
7900 	bnxt_hwrm_dbg_qcaps(bp);
7901 
7902 	rc = bnxt_hwrm_queue_qportcfg(bp);
7903 	if (rc) {
7904 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
7905 		return rc;
7906 	}
7907 	if (bp->hwrm_spec_code >= 0x10803) {
7908 		rc = bnxt_alloc_ctx_mem(bp);
7909 		if (rc)
7910 			return rc;
7911 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
7912 		if (!rc)
7913 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
7914 	}
7915 	return 0;
7916 }
7917 
7918 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
7919 {
7920 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
7921 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
7922 	u32 flags;
7923 	int rc;
7924 
7925 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
7926 		return 0;
7927 
7928 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
7929 	if (rc)
7930 		return rc;
7931 
7932 	resp = hwrm_req_hold(bp, req);
7933 	rc = hwrm_req_send(bp, req);
7934 	if (rc)
7935 		goto hwrm_cfa_adv_qcaps_exit;
7936 
7937 	flags = le32_to_cpu(resp->flags);
7938 	if (flags &
7939 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
7940 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
7941 
7942 hwrm_cfa_adv_qcaps_exit:
7943 	hwrm_req_drop(bp, req);
7944 	return rc;
7945 }
7946 
7947 static int __bnxt_alloc_fw_health(struct bnxt *bp)
7948 {
7949 	if (bp->fw_health)
7950 		return 0;
7951 
7952 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
7953 	if (!bp->fw_health)
7954 		return -ENOMEM;
7955 
7956 	mutex_init(&bp->fw_health->lock);
7957 	return 0;
7958 }
7959 
7960 static int bnxt_alloc_fw_health(struct bnxt *bp)
7961 {
7962 	int rc;
7963 
7964 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
7965 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
7966 		return 0;
7967 
7968 	rc = __bnxt_alloc_fw_health(bp);
7969 	if (rc) {
7970 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
7971 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
7972 		return rc;
7973 	}
7974 
7975 	return 0;
7976 }
7977 
7978 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
7979 {
7980 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
7981 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
7982 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
7983 }
7984 
7985 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
7986 {
7987 	struct bnxt_fw_health *fw_health = bp->fw_health;
7988 	u32 reg_type;
7989 
7990 	if (!fw_health)
7991 		return;
7992 
7993 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
7994 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
7995 		fw_health->status_reliable = false;
7996 
7997 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
7998 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
7999 		fw_health->resets_reliable = false;
8000 }
8001 
8002 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
8003 {
8004 	void __iomem *hs;
8005 	u32 status_loc;
8006 	u32 reg_type;
8007 	u32 sig;
8008 
8009 	if (bp->fw_health)
8010 		bp->fw_health->status_reliable = false;
8011 
8012 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
8013 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
8014 
8015 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
8016 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
8017 		if (!bp->chip_num) {
8018 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
8019 			bp->chip_num = readl(bp->bar0 +
8020 					     BNXT_FW_HEALTH_WIN_BASE +
8021 					     BNXT_GRC_REG_CHIP_NUM);
8022 		}
8023 		if (!BNXT_CHIP_P5(bp))
8024 			return;
8025 
8026 		status_loc = BNXT_GRC_REG_STATUS_P5 |
8027 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
8028 	} else {
8029 		status_loc = readl(hs + offsetof(struct hcomm_status,
8030 						 fw_status_loc));
8031 	}
8032 
8033 	if (__bnxt_alloc_fw_health(bp)) {
8034 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
8035 		return;
8036 	}
8037 
8038 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
8039 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
8040 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
8041 		__bnxt_map_fw_health_reg(bp, status_loc);
8042 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
8043 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
8044 	}
8045 
8046 	bp->fw_health->status_reliable = true;
8047 }
8048 
8049 static int bnxt_map_fw_health_regs(struct bnxt *bp)
8050 {
8051 	struct bnxt_fw_health *fw_health = bp->fw_health;
8052 	u32 reg_base = 0xffffffff;
8053 	int i;
8054 
8055 	bp->fw_health->status_reliable = false;
8056 	bp->fw_health->resets_reliable = false;
8057 	/* Only pre-map the monitoring GRC registers using window 3 */
8058 	for (i = 0; i < 4; i++) {
8059 		u32 reg = fw_health->regs[i];
8060 
8061 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
8062 			continue;
8063 		if (reg_base == 0xffffffff)
8064 			reg_base = reg & BNXT_GRC_BASE_MASK;
8065 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
8066 			return -ERANGE;
8067 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
8068 	}
8069 	bp->fw_health->status_reliable = true;
8070 	bp->fw_health->resets_reliable = true;
8071 	if (reg_base == 0xffffffff)
8072 		return 0;
8073 
8074 	__bnxt_map_fw_health_reg(bp, reg_base);
8075 	return 0;
8076 }
8077 
8078 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
8079 {
8080 	if (!bp->fw_health)
8081 		return;
8082 
8083 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
8084 		bp->fw_health->status_reliable = true;
8085 		bp->fw_health->resets_reliable = true;
8086 	} else {
8087 		bnxt_try_map_fw_health_reg(bp);
8088 	}
8089 }
8090 
8091 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
8092 {
8093 	struct bnxt_fw_health *fw_health = bp->fw_health;
8094 	struct hwrm_error_recovery_qcfg_output *resp;
8095 	struct hwrm_error_recovery_qcfg_input *req;
8096 	int rc, i;
8097 
8098 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
8099 		return 0;
8100 
8101 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
8102 	if (rc)
8103 		return rc;
8104 
8105 	resp = hwrm_req_hold(bp, req);
8106 	rc = hwrm_req_send(bp, req);
8107 	if (rc)
8108 		goto err_recovery_out;
8109 	fw_health->flags = le32_to_cpu(resp->flags);
8110 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
8111 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
8112 		rc = -EINVAL;
8113 		goto err_recovery_out;
8114 	}
8115 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
8116 	fw_health->master_func_wait_dsecs =
8117 		le32_to_cpu(resp->master_func_wait_period);
8118 	fw_health->normal_func_wait_dsecs =
8119 		le32_to_cpu(resp->normal_func_wait_period);
8120 	fw_health->post_reset_wait_dsecs =
8121 		le32_to_cpu(resp->master_func_wait_period_after_reset);
8122 	fw_health->post_reset_max_wait_dsecs =
8123 		le32_to_cpu(resp->max_bailout_time_after_reset);
8124 	fw_health->regs[BNXT_FW_HEALTH_REG] =
8125 		le32_to_cpu(resp->fw_health_status_reg);
8126 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
8127 		le32_to_cpu(resp->fw_heartbeat_reg);
8128 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
8129 		le32_to_cpu(resp->fw_reset_cnt_reg);
8130 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
8131 		le32_to_cpu(resp->reset_inprogress_reg);
8132 	fw_health->fw_reset_inprog_reg_mask =
8133 		le32_to_cpu(resp->reset_inprogress_reg_mask);
8134 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
8135 	if (fw_health->fw_reset_seq_cnt >= 16) {
8136 		rc = -EINVAL;
8137 		goto err_recovery_out;
8138 	}
8139 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
8140 		fw_health->fw_reset_seq_regs[i] =
8141 			le32_to_cpu(resp->reset_reg[i]);
8142 		fw_health->fw_reset_seq_vals[i] =
8143 			le32_to_cpu(resp->reset_reg_val[i]);
8144 		fw_health->fw_reset_seq_delay_msec[i] =
8145 			resp->delay_after_reset[i];
8146 	}
8147 err_recovery_out:
8148 	hwrm_req_drop(bp, req);
8149 	if (!rc)
8150 		rc = bnxt_map_fw_health_regs(bp);
8151 	if (rc)
8152 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
8153 	return rc;
8154 }
8155 
8156 static int bnxt_hwrm_func_reset(struct bnxt *bp)
8157 {
8158 	struct hwrm_func_reset_input *req;
8159 	int rc;
8160 
8161 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
8162 	if (rc)
8163 		return rc;
8164 
8165 	req->enables = 0;
8166 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
8167 	return hwrm_req_send(bp, req);
8168 }
8169 
8170 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
8171 {
8172 	struct hwrm_nvm_get_dev_info_output nvm_info;
8173 
8174 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
8175 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
8176 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
8177 			 nvm_info.nvm_cfg_ver_upd);
8178 }
8179 
8180 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
8181 {
8182 	struct hwrm_queue_qportcfg_output *resp;
8183 	struct hwrm_queue_qportcfg_input *req;
8184 	u8 i, j, *qptr;
8185 	bool no_rdma;
8186 	int rc = 0;
8187 
8188 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
8189 	if (rc)
8190 		return rc;
8191 
8192 	resp = hwrm_req_hold(bp, req);
8193 	rc = hwrm_req_send(bp, req);
8194 	if (rc)
8195 		goto qportcfg_exit;
8196 
8197 	if (!resp->max_configurable_queues) {
8198 		rc = -EINVAL;
8199 		goto qportcfg_exit;
8200 	}
8201 	bp->max_tc = resp->max_configurable_queues;
8202 	bp->max_lltc = resp->max_configurable_lossless_queues;
8203 	if (bp->max_tc > BNXT_MAX_QUEUE)
8204 		bp->max_tc = BNXT_MAX_QUEUE;
8205 
8206 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
8207 	qptr = &resp->queue_id0;
8208 	for (i = 0, j = 0; i < bp->max_tc; i++) {
8209 		bp->q_info[j].queue_id = *qptr;
8210 		bp->q_ids[i] = *qptr++;
8211 		bp->q_info[j].queue_profile = *qptr++;
8212 		bp->tc_to_qidx[j] = j;
8213 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
8214 		    (no_rdma && BNXT_PF(bp)))
8215 			j++;
8216 	}
8217 	bp->max_q = bp->max_tc;
8218 	bp->max_tc = max_t(u8, j, 1);
8219 
8220 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
8221 		bp->max_tc = 1;
8222 
8223 	if (bp->max_lltc > bp->max_tc)
8224 		bp->max_lltc = bp->max_tc;
8225 
8226 qportcfg_exit:
8227 	hwrm_req_drop(bp, req);
8228 	return rc;
8229 }
8230 
8231 static int bnxt_hwrm_poll(struct bnxt *bp)
8232 {
8233 	struct hwrm_ver_get_input *req;
8234 	int rc;
8235 
8236 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
8237 	if (rc)
8238 		return rc;
8239 
8240 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
8241 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
8242 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
8243 
8244 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
8245 	rc = hwrm_req_send(bp, req);
8246 	return rc;
8247 }
8248 
8249 static int bnxt_hwrm_ver_get(struct bnxt *bp)
8250 {
8251 	struct hwrm_ver_get_output *resp;
8252 	struct hwrm_ver_get_input *req;
8253 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
8254 	u32 dev_caps_cfg, hwrm_ver;
8255 	int rc, len;
8256 
8257 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
8258 	if (rc)
8259 		return rc;
8260 
8261 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
8262 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
8263 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
8264 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
8265 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
8266 
8267 	resp = hwrm_req_hold(bp, req);
8268 	rc = hwrm_req_send(bp, req);
8269 	if (rc)
8270 		goto hwrm_ver_get_exit;
8271 
8272 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
8273 
8274 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
8275 			     resp->hwrm_intf_min_8b << 8 |
8276 			     resp->hwrm_intf_upd_8b;
8277 	if (resp->hwrm_intf_maj_8b < 1) {
8278 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
8279 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
8280 			    resp->hwrm_intf_upd_8b);
8281 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
8282 	}
8283 
8284 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
8285 			HWRM_VERSION_UPDATE;
8286 
8287 	if (bp->hwrm_spec_code > hwrm_ver)
8288 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
8289 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
8290 			 HWRM_VERSION_UPDATE);
8291 	else
8292 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
8293 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
8294 			 resp->hwrm_intf_upd_8b);
8295 
8296 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
8297 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
8298 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
8299 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
8300 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
8301 		len = FW_VER_STR_LEN;
8302 	} else {
8303 		fw_maj = resp->hwrm_fw_maj_8b;
8304 		fw_min = resp->hwrm_fw_min_8b;
8305 		fw_bld = resp->hwrm_fw_bld_8b;
8306 		fw_rsv = resp->hwrm_fw_rsvd_8b;
8307 		len = BC_HWRM_STR_LEN;
8308 	}
8309 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
8310 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
8311 		 fw_rsv);
8312 
8313 	if (strlen(resp->active_pkg_name)) {
8314 		int fw_ver_len = strlen(bp->fw_ver_str);
8315 
8316 		snprintf(bp->fw_ver_str + fw_ver_len,
8317 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
8318 			 resp->active_pkg_name);
8319 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
8320 	}
8321 
8322 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
8323 	if (!bp->hwrm_cmd_timeout)
8324 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
8325 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
8326 	if (!bp->hwrm_cmd_max_timeout)
8327 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
8328 	else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT)
8329 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n",
8330 			    bp->hwrm_cmd_max_timeout / 1000);
8331 
8332 	if (resp->hwrm_intf_maj_8b >= 1) {
8333 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
8334 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
8335 	}
8336 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
8337 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
8338 
8339 	bp->chip_num = le16_to_cpu(resp->chip_num);
8340 	bp->chip_rev = resp->chip_rev;
8341 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
8342 	    !resp->chip_metal)
8343 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
8344 
8345 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
8346 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
8347 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
8348 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
8349 
8350 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
8351 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
8352 
8353 	if (dev_caps_cfg &
8354 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
8355 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
8356 
8357 	if (dev_caps_cfg &
8358 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
8359 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
8360 
8361 	if (dev_caps_cfg &
8362 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
8363 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
8364 
8365 hwrm_ver_get_exit:
8366 	hwrm_req_drop(bp, req);
8367 	return rc;
8368 }
8369 
8370 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
8371 {
8372 	struct hwrm_fw_set_time_input *req;
8373 	struct tm tm;
8374 	time64_t now = ktime_get_real_seconds();
8375 	int rc;
8376 
8377 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
8378 	    bp->hwrm_spec_code < 0x10400)
8379 		return -EOPNOTSUPP;
8380 
8381 	time64_to_tm(now, 0, &tm);
8382 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
8383 	if (rc)
8384 		return rc;
8385 
8386 	req->year = cpu_to_le16(1900 + tm.tm_year);
8387 	req->month = 1 + tm.tm_mon;
8388 	req->day = tm.tm_mday;
8389 	req->hour = tm.tm_hour;
8390 	req->minute = tm.tm_min;
8391 	req->second = tm.tm_sec;
8392 	return hwrm_req_send(bp, req);
8393 }
8394 
8395 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
8396 {
8397 	u64 sw_tmp;
8398 
8399 	hw &= mask;
8400 	sw_tmp = (*sw & ~mask) | hw;
8401 	if (hw < (*sw & mask))
8402 		sw_tmp += mask + 1;
8403 	WRITE_ONCE(*sw, sw_tmp);
8404 }
8405 
8406 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
8407 				    int count, bool ignore_zero)
8408 {
8409 	int i;
8410 
8411 	for (i = 0; i < count; i++) {
8412 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
8413 
8414 		if (ignore_zero && !hw)
8415 			continue;
8416 
8417 		if (masks[i] == -1ULL)
8418 			sw_stats[i] = hw;
8419 		else
8420 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
8421 	}
8422 }
8423 
8424 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
8425 {
8426 	if (!stats->hw_stats)
8427 		return;
8428 
8429 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
8430 				stats->hw_masks, stats->len / 8, false);
8431 }
8432 
8433 static void bnxt_accumulate_all_stats(struct bnxt *bp)
8434 {
8435 	struct bnxt_stats_mem *ring0_stats;
8436 	bool ignore_zero = false;
8437 	int i;
8438 
8439 	/* Chip bug.  Counter intermittently becomes 0. */
8440 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8441 		ignore_zero = true;
8442 
8443 	for (i = 0; i < bp->cp_nr_rings; i++) {
8444 		struct bnxt_napi *bnapi = bp->bnapi[i];
8445 		struct bnxt_cp_ring_info *cpr;
8446 		struct bnxt_stats_mem *stats;
8447 
8448 		cpr = &bnapi->cp_ring;
8449 		stats = &cpr->stats;
8450 		if (!i)
8451 			ring0_stats = stats;
8452 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
8453 					ring0_stats->hw_masks,
8454 					ring0_stats->len / 8, ignore_zero);
8455 	}
8456 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
8457 		struct bnxt_stats_mem *stats = &bp->port_stats;
8458 		__le64 *hw_stats = stats->hw_stats;
8459 		u64 *sw_stats = stats->sw_stats;
8460 		u64 *masks = stats->hw_masks;
8461 		int cnt;
8462 
8463 		cnt = sizeof(struct rx_port_stats) / 8;
8464 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
8465 
8466 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
8467 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
8468 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
8469 		cnt = sizeof(struct tx_port_stats) / 8;
8470 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
8471 	}
8472 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
8473 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
8474 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
8475 	}
8476 }
8477 
8478 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
8479 {
8480 	struct hwrm_port_qstats_input *req;
8481 	struct bnxt_pf_info *pf = &bp->pf;
8482 	int rc;
8483 
8484 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
8485 		return 0;
8486 
8487 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
8488 		return -EOPNOTSUPP;
8489 
8490 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
8491 	if (rc)
8492 		return rc;
8493 
8494 	req->flags = flags;
8495 	req->port_id = cpu_to_le16(pf->port_id);
8496 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
8497 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
8498 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
8499 	return hwrm_req_send(bp, req);
8500 }
8501 
8502 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
8503 {
8504 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
8505 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
8506 	struct hwrm_port_qstats_ext_output *resp_qs;
8507 	struct hwrm_port_qstats_ext_input *req_qs;
8508 	struct bnxt_pf_info *pf = &bp->pf;
8509 	u32 tx_stat_size;
8510 	int rc;
8511 
8512 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
8513 		return 0;
8514 
8515 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
8516 		return -EOPNOTSUPP;
8517 
8518 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
8519 	if (rc)
8520 		return rc;
8521 
8522 	req_qs->flags = flags;
8523 	req_qs->port_id = cpu_to_le16(pf->port_id);
8524 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
8525 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
8526 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
8527 		       sizeof(struct tx_port_stats_ext) : 0;
8528 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
8529 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
8530 	resp_qs = hwrm_req_hold(bp, req_qs);
8531 	rc = hwrm_req_send(bp, req_qs);
8532 	if (!rc) {
8533 		bp->fw_rx_stats_ext_size =
8534 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
8535 		if (BNXT_FW_MAJ(bp) < 220 &&
8536 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
8537 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
8538 
8539 		bp->fw_tx_stats_ext_size = tx_stat_size ?
8540 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
8541 	} else {
8542 		bp->fw_rx_stats_ext_size = 0;
8543 		bp->fw_tx_stats_ext_size = 0;
8544 	}
8545 	hwrm_req_drop(bp, req_qs);
8546 
8547 	if (flags)
8548 		return rc;
8549 
8550 	if (bp->fw_tx_stats_ext_size <=
8551 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
8552 		bp->pri2cos_valid = 0;
8553 		return rc;
8554 	}
8555 
8556 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
8557 	if (rc)
8558 		return rc;
8559 
8560 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
8561 
8562 	resp_qc = hwrm_req_hold(bp, req_qc);
8563 	rc = hwrm_req_send(bp, req_qc);
8564 	if (!rc) {
8565 		u8 *pri2cos;
8566 		int i, j;
8567 
8568 		pri2cos = &resp_qc->pri0_cos_queue_id;
8569 		for (i = 0; i < 8; i++) {
8570 			u8 queue_id = pri2cos[i];
8571 			u8 queue_idx;
8572 
8573 			/* Per port queue IDs start from 0, 10, 20, etc */
8574 			queue_idx = queue_id % 10;
8575 			if (queue_idx > BNXT_MAX_QUEUE) {
8576 				bp->pri2cos_valid = false;
8577 				hwrm_req_drop(bp, req_qc);
8578 				return rc;
8579 			}
8580 			for (j = 0; j < bp->max_q; j++) {
8581 				if (bp->q_ids[j] == queue_id)
8582 					bp->pri2cos_idx[i] = queue_idx;
8583 			}
8584 		}
8585 		bp->pri2cos_valid = true;
8586 	}
8587 	hwrm_req_drop(bp, req_qc);
8588 
8589 	return rc;
8590 }
8591 
8592 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
8593 {
8594 	bnxt_hwrm_tunnel_dst_port_free(bp,
8595 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
8596 	bnxt_hwrm_tunnel_dst_port_free(bp,
8597 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
8598 }
8599 
8600 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
8601 {
8602 	int rc, i;
8603 	u32 tpa_flags = 0;
8604 
8605 	if (set_tpa)
8606 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
8607 	else if (BNXT_NO_FW_ACCESS(bp))
8608 		return 0;
8609 	for (i = 0; i < bp->nr_vnics; i++) {
8610 		rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
8611 		if (rc) {
8612 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
8613 				   i, rc);
8614 			return rc;
8615 		}
8616 	}
8617 	return 0;
8618 }
8619 
8620 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
8621 {
8622 	int i;
8623 
8624 	for (i = 0; i < bp->nr_vnics; i++)
8625 		bnxt_hwrm_vnic_set_rss(bp, i, false);
8626 }
8627 
8628 static void bnxt_clear_vnic(struct bnxt *bp)
8629 {
8630 	if (!bp->vnic_info)
8631 		return;
8632 
8633 	bnxt_hwrm_clear_vnic_filter(bp);
8634 	if (!(bp->flags & BNXT_FLAG_CHIP_P5)) {
8635 		/* clear all RSS setting before free vnic ctx */
8636 		bnxt_hwrm_clear_vnic_rss(bp);
8637 		bnxt_hwrm_vnic_ctx_free(bp);
8638 	}
8639 	/* before free the vnic, undo the vnic tpa settings */
8640 	if (bp->flags & BNXT_FLAG_TPA)
8641 		bnxt_set_tpa(bp, false);
8642 	bnxt_hwrm_vnic_free(bp);
8643 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8644 		bnxt_hwrm_vnic_ctx_free(bp);
8645 }
8646 
8647 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
8648 				    bool irq_re_init)
8649 {
8650 	bnxt_clear_vnic(bp);
8651 	bnxt_hwrm_ring_free(bp, close_path);
8652 	bnxt_hwrm_ring_grp_free(bp);
8653 	if (irq_re_init) {
8654 		bnxt_hwrm_stat_ctx_free(bp);
8655 		bnxt_hwrm_free_tunnel_ports(bp);
8656 	}
8657 }
8658 
8659 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
8660 {
8661 	struct hwrm_func_cfg_input *req;
8662 	u8 evb_mode;
8663 	int rc;
8664 
8665 	if (br_mode == BRIDGE_MODE_VEB)
8666 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
8667 	else if (br_mode == BRIDGE_MODE_VEPA)
8668 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
8669 	else
8670 		return -EINVAL;
8671 
8672 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
8673 	if (rc)
8674 		return rc;
8675 
8676 	req->fid = cpu_to_le16(0xffff);
8677 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
8678 	req->evb_mode = evb_mode;
8679 	return hwrm_req_send(bp, req);
8680 }
8681 
8682 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
8683 {
8684 	struct hwrm_func_cfg_input *req;
8685 	int rc;
8686 
8687 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
8688 		return 0;
8689 
8690 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
8691 	if (rc)
8692 		return rc;
8693 
8694 	req->fid = cpu_to_le16(0xffff);
8695 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
8696 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
8697 	if (size == 128)
8698 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
8699 
8700 	return hwrm_req_send(bp, req);
8701 }
8702 
8703 static int __bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
8704 {
8705 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
8706 	int rc;
8707 
8708 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
8709 		goto skip_rss_ctx;
8710 
8711 	/* allocate context for vnic */
8712 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
8713 	if (rc) {
8714 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
8715 			   vnic_id, rc);
8716 		goto vnic_setup_err;
8717 	}
8718 	bp->rsscos_nr_ctxs++;
8719 
8720 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8721 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
8722 		if (rc) {
8723 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
8724 				   vnic_id, rc);
8725 			goto vnic_setup_err;
8726 		}
8727 		bp->rsscos_nr_ctxs++;
8728 	}
8729 
8730 skip_rss_ctx:
8731 	/* configure default vnic, ring grp */
8732 	rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
8733 	if (rc) {
8734 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
8735 			   vnic_id, rc);
8736 		goto vnic_setup_err;
8737 	}
8738 
8739 	/* Enable RSS hashing on vnic */
8740 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
8741 	if (rc) {
8742 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
8743 			   vnic_id, rc);
8744 		goto vnic_setup_err;
8745 	}
8746 
8747 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8748 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
8749 		if (rc) {
8750 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
8751 				   vnic_id, rc);
8752 		}
8753 	}
8754 
8755 vnic_setup_err:
8756 	return rc;
8757 }
8758 
8759 static int __bnxt_setup_vnic_p5(struct bnxt *bp, u16 vnic_id)
8760 {
8761 	int rc, i, nr_ctxs;
8762 
8763 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
8764 	for (i = 0; i < nr_ctxs; i++) {
8765 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, i);
8766 		if (rc) {
8767 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
8768 				   vnic_id, i, rc);
8769 			break;
8770 		}
8771 		bp->rsscos_nr_ctxs++;
8772 	}
8773 	if (i < nr_ctxs)
8774 		return -ENOMEM;
8775 
8776 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic_id, true);
8777 	if (rc) {
8778 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
8779 			   vnic_id, rc);
8780 		return rc;
8781 	}
8782 	rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
8783 	if (rc) {
8784 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
8785 			   vnic_id, rc);
8786 		return rc;
8787 	}
8788 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8789 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
8790 		if (rc) {
8791 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
8792 				   vnic_id, rc);
8793 		}
8794 	}
8795 	return rc;
8796 }
8797 
8798 static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
8799 {
8800 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8801 		return __bnxt_setup_vnic_p5(bp, vnic_id);
8802 	else
8803 		return __bnxt_setup_vnic(bp, vnic_id);
8804 }
8805 
8806 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
8807 {
8808 #ifdef CONFIG_RFS_ACCEL
8809 	int i, rc = 0;
8810 
8811 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8812 		return 0;
8813 
8814 	for (i = 0; i < bp->rx_nr_rings; i++) {
8815 		struct bnxt_vnic_info *vnic;
8816 		u16 vnic_id = i + 1;
8817 		u16 ring_id = i;
8818 
8819 		if (vnic_id >= bp->nr_vnics)
8820 			break;
8821 
8822 		vnic = &bp->vnic_info[vnic_id];
8823 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
8824 		if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
8825 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
8826 		rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
8827 		if (rc) {
8828 			netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
8829 				   vnic_id, rc);
8830 			break;
8831 		}
8832 		rc = bnxt_setup_vnic(bp, vnic_id);
8833 		if (rc)
8834 			break;
8835 	}
8836 	return rc;
8837 #else
8838 	return 0;
8839 #endif
8840 }
8841 
8842 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
8843 static bool bnxt_promisc_ok(struct bnxt *bp)
8844 {
8845 #ifdef CONFIG_BNXT_SRIOV
8846 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
8847 		return false;
8848 #endif
8849 	return true;
8850 }
8851 
8852 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
8853 {
8854 	unsigned int rc = 0;
8855 
8856 	rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
8857 	if (rc) {
8858 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
8859 			   rc);
8860 		return rc;
8861 	}
8862 
8863 	rc = bnxt_hwrm_vnic_cfg(bp, 1);
8864 	if (rc) {
8865 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
8866 			   rc);
8867 		return rc;
8868 	}
8869 	return rc;
8870 }
8871 
8872 static int bnxt_cfg_rx_mode(struct bnxt *);
8873 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
8874 
8875 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
8876 {
8877 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
8878 	int rc = 0;
8879 	unsigned int rx_nr_rings = bp->rx_nr_rings;
8880 
8881 	if (irq_re_init) {
8882 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
8883 		if (rc) {
8884 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
8885 				   rc);
8886 			goto err_out;
8887 		}
8888 	}
8889 
8890 	rc = bnxt_hwrm_ring_alloc(bp);
8891 	if (rc) {
8892 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
8893 		goto err_out;
8894 	}
8895 
8896 	rc = bnxt_hwrm_ring_grp_alloc(bp);
8897 	if (rc) {
8898 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
8899 		goto err_out;
8900 	}
8901 
8902 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8903 		rx_nr_rings--;
8904 
8905 	/* default vnic 0 */
8906 	rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
8907 	if (rc) {
8908 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
8909 		goto err_out;
8910 	}
8911 
8912 	if (BNXT_VF(bp))
8913 		bnxt_hwrm_func_qcfg(bp);
8914 
8915 	rc = bnxt_setup_vnic(bp, 0);
8916 	if (rc)
8917 		goto err_out;
8918 	if (bp->fw_cap & BNXT_FW_CAP_RSS_HASH_TYPE_DELTA)
8919 		bnxt_hwrm_update_rss_hash_cfg(bp);
8920 
8921 	if (bp->flags & BNXT_FLAG_RFS) {
8922 		rc = bnxt_alloc_rfs_vnics(bp);
8923 		if (rc)
8924 			goto err_out;
8925 	}
8926 
8927 	if (bp->flags & BNXT_FLAG_TPA) {
8928 		rc = bnxt_set_tpa(bp, true);
8929 		if (rc)
8930 			goto err_out;
8931 	}
8932 
8933 	if (BNXT_VF(bp))
8934 		bnxt_update_vf_mac(bp);
8935 
8936 	/* Filter for default vnic 0 */
8937 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
8938 	if (rc) {
8939 		if (BNXT_VF(bp) && rc == -ENODEV)
8940 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
8941 		else
8942 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
8943 		goto err_out;
8944 	}
8945 	vnic->uc_filter_count = 1;
8946 
8947 	vnic->rx_mask = 0;
8948 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
8949 		goto skip_rx_mask;
8950 
8951 	if (bp->dev->flags & IFF_BROADCAST)
8952 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
8953 
8954 	if (bp->dev->flags & IFF_PROMISC)
8955 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
8956 
8957 	if (bp->dev->flags & IFF_ALLMULTI) {
8958 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
8959 		vnic->mc_list_count = 0;
8960 	} else if (bp->dev->flags & IFF_MULTICAST) {
8961 		u32 mask = 0;
8962 
8963 		bnxt_mc_list_updated(bp, &mask);
8964 		vnic->rx_mask |= mask;
8965 	}
8966 
8967 	rc = bnxt_cfg_rx_mode(bp);
8968 	if (rc)
8969 		goto err_out;
8970 
8971 skip_rx_mask:
8972 	rc = bnxt_hwrm_set_coal(bp);
8973 	if (rc)
8974 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
8975 				rc);
8976 
8977 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8978 		rc = bnxt_setup_nitroa0_vnic(bp);
8979 		if (rc)
8980 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
8981 				   rc);
8982 	}
8983 
8984 	if (BNXT_VF(bp)) {
8985 		bnxt_hwrm_func_qcfg(bp);
8986 		netdev_update_features(bp->dev);
8987 	}
8988 
8989 	return 0;
8990 
8991 err_out:
8992 	bnxt_hwrm_resource_free(bp, 0, true);
8993 
8994 	return rc;
8995 }
8996 
8997 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
8998 {
8999 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
9000 	return 0;
9001 }
9002 
9003 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
9004 {
9005 	bnxt_init_cp_rings(bp);
9006 	bnxt_init_rx_rings(bp);
9007 	bnxt_init_tx_rings(bp);
9008 	bnxt_init_ring_grps(bp, irq_re_init);
9009 	bnxt_init_vnics(bp);
9010 
9011 	return bnxt_init_chip(bp, irq_re_init);
9012 }
9013 
9014 static int bnxt_set_real_num_queues(struct bnxt *bp)
9015 {
9016 	int rc;
9017 	struct net_device *dev = bp->dev;
9018 
9019 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
9020 					  bp->tx_nr_rings_xdp);
9021 	if (rc)
9022 		return rc;
9023 
9024 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
9025 	if (rc)
9026 		return rc;
9027 
9028 #ifdef CONFIG_RFS_ACCEL
9029 	if (bp->flags & BNXT_FLAG_RFS)
9030 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
9031 #endif
9032 
9033 	return rc;
9034 }
9035 
9036 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
9037 			   bool shared)
9038 {
9039 	int _rx = *rx, _tx = *tx;
9040 
9041 	if (shared) {
9042 		*rx = min_t(int, _rx, max);
9043 		*tx = min_t(int, _tx, max);
9044 	} else {
9045 		if (max < 2)
9046 			return -ENOMEM;
9047 
9048 		while (_rx + _tx > max) {
9049 			if (_rx > _tx && _rx > 1)
9050 				_rx--;
9051 			else if (_tx > 1)
9052 				_tx--;
9053 		}
9054 		*rx = _rx;
9055 		*tx = _tx;
9056 	}
9057 	return 0;
9058 }
9059 
9060 static void bnxt_setup_msix(struct bnxt *bp)
9061 {
9062 	const int len = sizeof(bp->irq_tbl[0].name);
9063 	struct net_device *dev = bp->dev;
9064 	int tcs, i;
9065 
9066 	tcs = netdev_get_num_tc(dev);
9067 	if (tcs) {
9068 		int i, off, count;
9069 
9070 		for (i = 0; i < tcs; i++) {
9071 			count = bp->tx_nr_rings_per_tc;
9072 			off = i * count;
9073 			netdev_set_tc_queue(dev, i, count, off);
9074 		}
9075 	}
9076 
9077 	for (i = 0; i < bp->cp_nr_rings; i++) {
9078 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
9079 		char *attr;
9080 
9081 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
9082 			attr = "TxRx";
9083 		else if (i < bp->rx_nr_rings)
9084 			attr = "rx";
9085 		else
9086 			attr = "tx";
9087 
9088 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
9089 			 attr, i);
9090 		bp->irq_tbl[map_idx].handler = bnxt_msix;
9091 	}
9092 }
9093 
9094 static void bnxt_setup_inta(struct bnxt *bp)
9095 {
9096 	const int len = sizeof(bp->irq_tbl[0].name);
9097 
9098 	if (netdev_get_num_tc(bp->dev))
9099 		netdev_reset_tc(bp->dev);
9100 
9101 	snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
9102 		 0);
9103 	bp->irq_tbl[0].handler = bnxt_inta;
9104 }
9105 
9106 static int bnxt_init_int_mode(struct bnxt *bp);
9107 
9108 static int bnxt_setup_int_mode(struct bnxt *bp)
9109 {
9110 	int rc;
9111 
9112 	if (!bp->irq_tbl) {
9113 		rc = bnxt_init_int_mode(bp);
9114 		if (rc || !bp->irq_tbl)
9115 			return rc ?: -ENODEV;
9116 	}
9117 
9118 	if (bp->flags & BNXT_FLAG_USING_MSIX)
9119 		bnxt_setup_msix(bp);
9120 	else
9121 		bnxt_setup_inta(bp);
9122 
9123 	rc = bnxt_set_real_num_queues(bp);
9124 	return rc;
9125 }
9126 
9127 #ifdef CONFIG_RFS_ACCEL
9128 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
9129 {
9130 	return bp->hw_resc.max_rsscos_ctxs;
9131 }
9132 
9133 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
9134 {
9135 	return bp->hw_resc.max_vnics;
9136 }
9137 #endif
9138 
9139 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
9140 {
9141 	return bp->hw_resc.max_stat_ctxs;
9142 }
9143 
9144 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
9145 {
9146 	return bp->hw_resc.max_cp_rings;
9147 }
9148 
9149 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
9150 {
9151 	unsigned int cp = bp->hw_resc.max_cp_rings;
9152 
9153 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
9154 		cp -= bnxt_get_ulp_msix_num(bp);
9155 
9156 	return cp;
9157 }
9158 
9159 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
9160 {
9161 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9162 
9163 	if (bp->flags & BNXT_FLAG_CHIP_P5)
9164 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
9165 
9166 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
9167 }
9168 
9169 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
9170 {
9171 	bp->hw_resc.max_irqs = max_irqs;
9172 }
9173 
9174 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
9175 {
9176 	unsigned int cp;
9177 
9178 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
9179 	if (bp->flags & BNXT_FLAG_CHIP_P5)
9180 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
9181 	else
9182 		return cp - bp->cp_nr_rings;
9183 }
9184 
9185 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
9186 {
9187 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
9188 }
9189 
9190 int bnxt_get_avail_msix(struct bnxt *bp, int num)
9191 {
9192 	int max_cp = bnxt_get_max_func_cp_rings(bp);
9193 	int max_irq = bnxt_get_max_func_irqs(bp);
9194 	int total_req = bp->cp_nr_rings + num;
9195 	int max_idx, avail_msix;
9196 
9197 	max_idx = bp->total_irqs;
9198 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
9199 		max_idx = min_t(int, bp->total_irqs, max_cp);
9200 	avail_msix = max_idx - bp->cp_nr_rings;
9201 	if (!BNXT_NEW_RM(bp) || avail_msix >= num)
9202 		return avail_msix;
9203 
9204 	if (max_irq < total_req) {
9205 		num = max_irq - bp->cp_nr_rings;
9206 		if (num <= 0)
9207 			return 0;
9208 	}
9209 	return num;
9210 }
9211 
9212 static int bnxt_get_num_msix(struct bnxt *bp)
9213 {
9214 	if (!BNXT_NEW_RM(bp))
9215 		return bnxt_get_max_func_irqs(bp);
9216 
9217 	return bnxt_nq_rings_in_use(bp);
9218 }
9219 
9220 static int bnxt_init_msix(struct bnxt *bp)
9221 {
9222 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix;
9223 	struct msix_entry *msix_ent;
9224 
9225 	total_vecs = bnxt_get_num_msix(bp);
9226 	max = bnxt_get_max_func_irqs(bp);
9227 	if (total_vecs > max)
9228 		total_vecs = max;
9229 
9230 	if (!total_vecs)
9231 		return 0;
9232 
9233 	msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
9234 	if (!msix_ent)
9235 		return -ENOMEM;
9236 
9237 	for (i = 0; i < total_vecs; i++) {
9238 		msix_ent[i].entry = i;
9239 		msix_ent[i].vector = 0;
9240 	}
9241 
9242 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
9243 		min = 2;
9244 
9245 	total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
9246 	ulp_msix = bnxt_get_ulp_msix_num(bp);
9247 	if (total_vecs < 0 || total_vecs < ulp_msix) {
9248 		rc = -ENODEV;
9249 		goto msix_setup_exit;
9250 	}
9251 
9252 	bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
9253 	if (bp->irq_tbl) {
9254 		for (i = 0; i < total_vecs; i++)
9255 			bp->irq_tbl[i].vector = msix_ent[i].vector;
9256 
9257 		bp->total_irqs = total_vecs;
9258 		/* Trim rings based upon num of vectors allocated */
9259 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
9260 				     total_vecs - ulp_msix, min == 1);
9261 		if (rc)
9262 			goto msix_setup_exit;
9263 
9264 		bp->cp_nr_rings = (min == 1) ?
9265 				  max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
9266 				  bp->tx_nr_rings + bp->rx_nr_rings;
9267 
9268 	} else {
9269 		rc = -ENOMEM;
9270 		goto msix_setup_exit;
9271 	}
9272 	bp->flags |= BNXT_FLAG_USING_MSIX;
9273 	kfree(msix_ent);
9274 	return 0;
9275 
9276 msix_setup_exit:
9277 	netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
9278 	kfree(bp->irq_tbl);
9279 	bp->irq_tbl = NULL;
9280 	pci_disable_msix(bp->pdev);
9281 	kfree(msix_ent);
9282 	return rc;
9283 }
9284 
9285 static int bnxt_init_inta(struct bnxt *bp)
9286 {
9287 	bp->irq_tbl = kzalloc(sizeof(struct bnxt_irq), GFP_KERNEL);
9288 	if (!bp->irq_tbl)
9289 		return -ENOMEM;
9290 
9291 	bp->total_irqs = 1;
9292 	bp->rx_nr_rings = 1;
9293 	bp->tx_nr_rings = 1;
9294 	bp->cp_nr_rings = 1;
9295 	bp->flags |= BNXT_FLAG_SHARED_RINGS;
9296 	bp->irq_tbl[0].vector = bp->pdev->irq;
9297 	return 0;
9298 }
9299 
9300 static int bnxt_init_int_mode(struct bnxt *bp)
9301 {
9302 	int rc = -ENODEV;
9303 
9304 	if (bp->flags & BNXT_FLAG_MSIX_CAP)
9305 		rc = bnxt_init_msix(bp);
9306 
9307 	if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
9308 		/* fallback to INTA */
9309 		rc = bnxt_init_inta(bp);
9310 	}
9311 	return rc;
9312 }
9313 
9314 static void bnxt_clear_int_mode(struct bnxt *bp)
9315 {
9316 	if (bp->flags & BNXT_FLAG_USING_MSIX)
9317 		pci_disable_msix(bp->pdev);
9318 
9319 	kfree(bp->irq_tbl);
9320 	bp->irq_tbl = NULL;
9321 	bp->flags &= ~BNXT_FLAG_USING_MSIX;
9322 }
9323 
9324 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
9325 {
9326 	int tcs = netdev_get_num_tc(bp->dev);
9327 	bool irq_cleared = false;
9328 	int rc;
9329 
9330 	if (!bnxt_need_reserve_rings(bp))
9331 		return 0;
9332 
9333 	if (irq_re_init && BNXT_NEW_RM(bp) &&
9334 	    bnxt_get_num_msix(bp) != bp->total_irqs) {
9335 		bnxt_ulp_irq_stop(bp);
9336 		bnxt_clear_int_mode(bp);
9337 		irq_cleared = true;
9338 	}
9339 	rc = __bnxt_reserve_rings(bp);
9340 	if (irq_cleared) {
9341 		if (!rc)
9342 			rc = bnxt_init_int_mode(bp);
9343 		bnxt_ulp_irq_restart(bp, rc);
9344 	}
9345 	if (rc) {
9346 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
9347 		return rc;
9348 	}
9349 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
9350 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
9351 		netdev_err(bp->dev, "tx ring reservation failure\n");
9352 		netdev_reset_tc(bp->dev);
9353 		if (bp->tx_nr_rings_xdp)
9354 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
9355 		else
9356 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
9357 		return -ENOMEM;
9358 	}
9359 	return 0;
9360 }
9361 
9362 static void bnxt_free_irq(struct bnxt *bp)
9363 {
9364 	struct bnxt_irq *irq;
9365 	int i;
9366 
9367 #ifdef CONFIG_RFS_ACCEL
9368 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
9369 	bp->dev->rx_cpu_rmap = NULL;
9370 #endif
9371 	if (!bp->irq_tbl || !bp->bnapi)
9372 		return;
9373 
9374 	for (i = 0; i < bp->cp_nr_rings; i++) {
9375 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
9376 
9377 		irq = &bp->irq_tbl[map_idx];
9378 		if (irq->requested) {
9379 			if (irq->have_cpumask) {
9380 				irq_set_affinity_hint(irq->vector, NULL);
9381 				free_cpumask_var(irq->cpu_mask);
9382 				irq->have_cpumask = 0;
9383 			}
9384 			free_irq(irq->vector, bp->bnapi[i]);
9385 		}
9386 
9387 		irq->requested = 0;
9388 	}
9389 }
9390 
9391 static int bnxt_request_irq(struct bnxt *bp)
9392 {
9393 	int i, j, rc = 0;
9394 	unsigned long flags = 0;
9395 #ifdef CONFIG_RFS_ACCEL
9396 	struct cpu_rmap *rmap;
9397 #endif
9398 
9399 	rc = bnxt_setup_int_mode(bp);
9400 	if (rc) {
9401 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
9402 			   rc);
9403 		return rc;
9404 	}
9405 #ifdef CONFIG_RFS_ACCEL
9406 	rmap = bp->dev->rx_cpu_rmap;
9407 #endif
9408 	if (!(bp->flags & BNXT_FLAG_USING_MSIX))
9409 		flags = IRQF_SHARED;
9410 
9411 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
9412 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
9413 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
9414 
9415 #ifdef CONFIG_RFS_ACCEL
9416 		if (rmap && bp->bnapi[i]->rx_ring) {
9417 			rc = irq_cpu_rmap_add(rmap, irq->vector);
9418 			if (rc)
9419 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
9420 					    j);
9421 			j++;
9422 		}
9423 #endif
9424 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
9425 				 bp->bnapi[i]);
9426 		if (rc)
9427 			break;
9428 
9429 		irq->requested = 1;
9430 
9431 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
9432 			int numa_node = dev_to_node(&bp->pdev->dev);
9433 
9434 			irq->have_cpumask = 1;
9435 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
9436 					irq->cpu_mask);
9437 			rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
9438 			if (rc) {
9439 				netdev_warn(bp->dev,
9440 					    "Set affinity failed, IRQ = %d\n",
9441 					    irq->vector);
9442 				break;
9443 			}
9444 		}
9445 	}
9446 	return rc;
9447 }
9448 
9449 static void bnxt_del_napi(struct bnxt *bp)
9450 {
9451 	int i;
9452 
9453 	if (!bp->bnapi)
9454 		return;
9455 
9456 	for (i = 0; i < bp->cp_nr_rings; i++) {
9457 		struct bnxt_napi *bnapi = bp->bnapi[i];
9458 
9459 		__netif_napi_del(&bnapi->napi);
9460 	}
9461 	/* We called __netif_napi_del(), we need
9462 	 * to respect an RCU grace period before freeing napi structures.
9463 	 */
9464 	synchronize_net();
9465 }
9466 
9467 static void bnxt_init_napi(struct bnxt *bp)
9468 {
9469 	int i;
9470 	unsigned int cp_nr_rings = bp->cp_nr_rings;
9471 	struct bnxt_napi *bnapi;
9472 
9473 	if (bp->flags & BNXT_FLAG_USING_MSIX) {
9474 		int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
9475 
9476 		if (bp->flags & BNXT_FLAG_CHIP_P5)
9477 			poll_fn = bnxt_poll_p5;
9478 		else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
9479 			cp_nr_rings--;
9480 		for (i = 0; i < cp_nr_rings; i++) {
9481 			bnapi = bp->bnapi[i];
9482 			netif_napi_add(bp->dev, &bnapi->napi, poll_fn);
9483 		}
9484 		if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
9485 			bnapi = bp->bnapi[cp_nr_rings];
9486 			netif_napi_add(bp->dev, &bnapi->napi,
9487 				       bnxt_poll_nitroa0);
9488 		}
9489 	} else {
9490 		bnapi = bp->bnapi[0];
9491 		netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll);
9492 	}
9493 }
9494 
9495 static void bnxt_disable_napi(struct bnxt *bp)
9496 {
9497 	int i;
9498 
9499 	if (!bp->bnapi ||
9500 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
9501 		return;
9502 
9503 	for (i = 0; i < bp->cp_nr_rings; i++) {
9504 		struct bnxt_napi *bnapi = bp->bnapi[i];
9505 		struct bnxt_cp_ring_info *cpr;
9506 
9507 		cpr = &bnapi->cp_ring;
9508 		if (bnapi->tx_fault)
9509 			cpr->sw_stats.tx.tx_resets++;
9510 		if (bnapi->in_reset)
9511 			cpr->sw_stats.rx.rx_resets++;
9512 		napi_disable(&bnapi->napi);
9513 		if (bnapi->rx_ring)
9514 			cancel_work_sync(&cpr->dim.work);
9515 	}
9516 }
9517 
9518 static void bnxt_enable_napi(struct bnxt *bp)
9519 {
9520 	int i;
9521 
9522 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
9523 	for (i = 0; i < bp->cp_nr_rings; i++) {
9524 		struct bnxt_napi *bnapi = bp->bnapi[i];
9525 		struct bnxt_cp_ring_info *cpr;
9526 
9527 		bnapi->tx_fault = 0;
9528 
9529 		cpr = &bnapi->cp_ring;
9530 		bnapi->in_reset = false;
9531 
9532 		bnapi->tx_pkts = 0;
9533 
9534 		if (bnapi->rx_ring) {
9535 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
9536 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
9537 		}
9538 		napi_enable(&bnapi->napi);
9539 	}
9540 }
9541 
9542 void bnxt_tx_disable(struct bnxt *bp)
9543 {
9544 	int i;
9545 	struct bnxt_tx_ring_info *txr;
9546 
9547 	if (bp->tx_ring) {
9548 		for (i = 0; i < bp->tx_nr_rings; i++) {
9549 			txr = &bp->tx_ring[i];
9550 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
9551 		}
9552 	}
9553 	/* Make sure napi polls see @dev_state change */
9554 	synchronize_net();
9555 	/* Drop carrier first to prevent TX timeout */
9556 	netif_carrier_off(bp->dev);
9557 	/* Stop all TX queues */
9558 	netif_tx_disable(bp->dev);
9559 }
9560 
9561 void bnxt_tx_enable(struct bnxt *bp)
9562 {
9563 	int i;
9564 	struct bnxt_tx_ring_info *txr;
9565 
9566 	for (i = 0; i < bp->tx_nr_rings; i++) {
9567 		txr = &bp->tx_ring[i];
9568 		WRITE_ONCE(txr->dev_state, 0);
9569 	}
9570 	/* Make sure napi polls see @dev_state change */
9571 	synchronize_net();
9572 	netif_tx_wake_all_queues(bp->dev);
9573 	if (BNXT_LINK_IS_UP(bp))
9574 		netif_carrier_on(bp->dev);
9575 }
9576 
9577 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
9578 {
9579 	u8 active_fec = link_info->active_fec_sig_mode &
9580 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
9581 
9582 	switch (active_fec) {
9583 	default:
9584 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
9585 		return "None";
9586 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
9587 		return "Clause 74 BaseR";
9588 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
9589 		return "Clause 91 RS(528,514)";
9590 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
9591 		return "Clause 91 RS544_1XN";
9592 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
9593 		return "Clause 91 RS(544,514)";
9594 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
9595 		return "Clause 91 RS272_1XN";
9596 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
9597 		return "Clause 91 RS(272,257)";
9598 	}
9599 }
9600 
9601 void bnxt_report_link(struct bnxt *bp)
9602 {
9603 	if (BNXT_LINK_IS_UP(bp)) {
9604 		const char *signal = "";
9605 		const char *flow_ctrl;
9606 		const char *duplex;
9607 		u32 speed;
9608 		u16 fec;
9609 
9610 		netif_carrier_on(bp->dev);
9611 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
9612 		if (speed == SPEED_UNKNOWN) {
9613 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
9614 			return;
9615 		}
9616 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
9617 			duplex = "full";
9618 		else
9619 			duplex = "half";
9620 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
9621 			flow_ctrl = "ON - receive & transmit";
9622 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
9623 			flow_ctrl = "ON - transmit";
9624 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
9625 			flow_ctrl = "ON - receive";
9626 		else
9627 			flow_ctrl = "none";
9628 		if (bp->link_info.phy_qcfg_resp.option_flags &
9629 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
9630 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
9631 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
9632 			switch (sig_mode) {
9633 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
9634 				signal = "(NRZ) ";
9635 				break;
9636 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
9637 				signal = "(PAM4) ";
9638 				break;
9639 			default:
9640 				break;
9641 			}
9642 		}
9643 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
9644 			    speed, signal, duplex, flow_ctrl);
9645 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
9646 			netdev_info(bp->dev, "EEE is %s\n",
9647 				    bp->eee.eee_active ? "active" :
9648 							 "not active");
9649 		fec = bp->link_info.fec_cfg;
9650 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
9651 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
9652 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
9653 				    bnxt_report_fec(&bp->link_info));
9654 	} else {
9655 		netif_carrier_off(bp->dev);
9656 		netdev_err(bp->dev, "NIC Link is Down\n");
9657 	}
9658 }
9659 
9660 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
9661 {
9662 	if (!resp->supported_speeds_auto_mode &&
9663 	    !resp->supported_speeds_force_mode &&
9664 	    !resp->supported_pam4_speeds_auto_mode &&
9665 	    !resp->supported_pam4_speeds_force_mode)
9666 		return true;
9667 	return false;
9668 }
9669 
9670 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
9671 {
9672 	struct bnxt_link_info *link_info = &bp->link_info;
9673 	struct hwrm_port_phy_qcaps_output *resp;
9674 	struct hwrm_port_phy_qcaps_input *req;
9675 	int rc = 0;
9676 
9677 	if (bp->hwrm_spec_code < 0x10201)
9678 		return 0;
9679 
9680 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
9681 	if (rc)
9682 		return rc;
9683 
9684 	resp = hwrm_req_hold(bp, req);
9685 	rc = hwrm_req_send(bp, req);
9686 	if (rc)
9687 		goto hwrm_phy_qcaps_exit;
9688 
9689 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
9690 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
9691 		struct ethtool_eee *eee = &bp->eee;
9692 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
9693 
9694 		eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9695 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
9696 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
9697 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
9698 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
9699 	}
9700 
9701 	if (bp->hwrm_spec_code >= 0x10a01) {
9702 		if (bnxt_phy_qcaps_no_speed(resp)) {
9703 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
9704 			netdev_warn(bp->dev, "Ethernet link disabled\n");
9705 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
9706 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
9707 			netdev_info(bp->dev, "Ethernet link enabled\n");
9708 			/* Phy re-enabled, reprobe the speeds */
9709 			link_info->support_auto_speeds = 0;
9710 			link_info->support_pam4_auto_speeds = 0;
9711 		}
9712 	}
9713 	if (resp->supported_speeds_auto_mode)
9714 		link_info->support_auto_speeds =
9715 			le16_to_cpu(resp->supported_speeds_auto_mode);
9716 	if (resp->supported_pam4_speeds_auto_mode)
9717 		link_info->support_pam4_auto_speeds =
9718 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
9719 
9720 	bp->port_count = resp->port_cnt;
9721 
9722 hwrm_phy_qcaps_exit:
9723 	hwrm_req_drop(bp, req);
9724 	return rc;
9725 }
9726 
9727 static bool bnxt_support_dropped(u16 advertising, u16 supported)
9728 {
9729 	u16 diff = advertising ^ supported;
9730 
9731 	return ((supported | diff) != supported);
9732 }
9733 
9734 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
9735 {
9736 	/* Check if any advertised speeds are no longer supported. The caller
9737 	 * holds the link_lock mutex, so we can modify link_info settings.
9738 	 */
9739 	if (bnxt_support_dropped(link_info->advertising,
9740 				 link_info->support_auto_speeds)) {
9741 		link_info->advertising = link_info->support_auto_speeds;
9742 		return true;
9743 	}
9744 	if (bnxt_support_dropped(link_info->advertising_pam4,
9745 				 link_info->support_pam4_auto_speeds)) {
9746 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
9747 		return true;
9748 	}
9749 	return false;
9750 }
9751 
9752 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
9753 {
9754 	struct bnxt_link_info *link_info = &bp->link_info;
9755 	struct hwrm_port_phy_qcfg_output *resp;
9756 	struct hwrm_port_phy_qcfg_input *req;
9757 	u8 link_state = link_info->link_state;
9758 	bool support_changed;
9759 	int rc;
9760 
9761 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
9762 	if (rc)
9763 		return rc;
9764 
9765 	resp = hwrm_req_hold(bp, req);
9766 	rc = hwrm_req_send(bp, req);
9767 	if (rc) {
9768 		hwrm_req_drop(bp, req);
9769 		if (BNXT_VF(bp) && rc == -ENODEV) {
9770 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
9771 			rc = 0;
9772 		}
9773 		return rc;
9774 	}
9775 
9776 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
9777 	link_info->phy_link_status = resp->link;
9778 	link_info->duplex = resp->duplex_cfg;
9779 	if (bp->hwrm_spec_code >= 0x10800)
9780 		link_info->duplex = resp->duplex_state;
9781 	link_info->pause = resp->pause;
9782 	link_info->auto_mode = resp->auto_mode;
9783 	link_info->auto_pause_setting = resp->auto_pause;
9784 	link_info->lp_pause = resp->link_partner_adv_pause;
9785 	link_info->force_pause_setting = resp->force_pause;
9786 	link_info->duplex_setting = resp->duplex_cfg;
9787 	if (link_info->phy_link_status == BNXT_LINK_LINK)
9788 		link_info->link_speed = le16_to_cpu(resp->link_speed);
9789 	else
9790 		link_info->link_speed = 0;
9791 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
9792 	link_info->force_pam4_link_speed =
9793 		le16_to_cpu(resp->force_pam4_link_speed);
9794 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
9795 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
9796 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
9797 	link_info->auto_pam4_link_speeds =
9798 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
9799 	link_info->lp_auto_link_speeds =
9800 		le16_to_cpu(resp->link_partner_adv_speeds);
9801 	link_info->lp_auto_pam4_link_speeds =
9802 		resp->link_partner_pam4_adv_speeds;
9803 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
9804 	link_info->phy_ver[0] = resp->phy_maj;
9805 	link_info->phy_ver[1] = resp->phy_min;
9806 	link_info->phy_ver[2] = resp->phy_bld;
9807 	link_info->media_type = resp->media_type;
9808 	link_info->phy_type = resp->phy_type;
9809 	link_info->transceiver = resp->xcvr_pkg_type;
9810 	link_info->phy_addr = resp->eee_config_phy_addr &
9811 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
9812 	link_info->module_status = resp->module_status;
9813 
9814 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
9815 		struct ethtool_eee *eee = &bp->eee;
9816 		u16 fw_speeds;
9817 
9818 		eee->eee_active = 0;
9819 		if (resp->eee_config_phy_addr &
9820 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
9821 			eee->eee_active = 1;
9822 			fw_speeds = le16_to_cpu(
9823 				resp->link_partner_adv_eee_link_speed_mask);
9824 			eee->lp_advertised =
9825 				_bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9826 		}
9827 
9828 		/* Pull initial EEE config */
9829 		if (!chng_link_state) {
9830 			if (resp->eee_config_phy_addr &
9831 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
9832 				eee->eee_enabled = 1;
9833 
9834 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
9835 			eee->advertised =
9836 				_bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9837 
9838 			if (resp->eee_config_phy_addr &
9839 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
9840 				__le32 tmr;
9841 
9842 				eee->tx_lpi_enabled = 1;
9843 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
9844 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
9845 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
9846 			}
9847 		}
9848 	}
9849 
9850 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
9851 	if (bp->hwrm_spec_code >= 0x10504) {
9852 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
9853 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
9854 	}
9855 	/* TODO: need to add more logic to report VF link */
9856 	if (chng_link_state) {
9857 		if (link_info->phy_link_status == BNXT_LINK_LINK)
9858 			link_info->link_state = BNXT_LINK_STATE_UP;
9859 		else
9860 			link_info->link_state = BNXT_LINK_STATE_DOWN;
9861 		if (link_state != link_info->link_state)
9862 			bnxt_report_link(bp);
9863 	} else {
9864 		/* always link down if not require to update link state */
9865 		link_info->link_state = BNXT_LINK_STATE_DOWN;
9866 	}
9867 	hwrm_req_drop(bp, req);
9868 
9869 	if (!BNXT_PHY_CFG_ABLE(bp))
9870 		return 0;
9871 
9872 	support_changed = bnxt_support_speed_dropped(link_info);
9873 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
9874 		bnxt_hwrm_set_link_setting(bp, true, false);
9875 	return 0;
9876 }
9877 
9878 static void bnxt_get_port_module_status(struct bnxt *bp)
9879 {
9880 	struct bnxt_link_info *link_info = &bp->link_info;
9881 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
9882 	u8 module_status;
9883 
9884 	if (bnxt_update_link(bp, true))
9885 		return;
9886 
9887 	module_status = link_info->module_status;
9888 	switch (module_status) {
9889 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
9890 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
9891 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
9892 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
9893 			    bp->pf.port_id);
9894 		if (bp->hwrm_spec_code >= 0x10201) {
9895 			netdev_warn(bp->dev, "Module part number %s\n",
9896 				    resp->phy_vendor_partnumber);
9897 		}
9898 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
9899 			netdev_warn(bp->dev, "TX is disabled\n");
9900 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
9901 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
9902 	}
9903 }
9904 
9905 static void
9906 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
9907 {
9908 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
9909 		if (bp->hwrm_spec_code >= 0x10201)
9910 			req->auto_pause =
9911 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
9912 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
9913 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
9914 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
9915 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
9916 		req->enables |=
9917 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
9918 	} else {
9919 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
9920 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
9921 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
9922 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
9923 		req->enables |=
9924 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
9925 		if (bp->hwrm_spec_code >= 0x10201) {
9926 			req->auto_pause = req->force_pause;
9927 			req->enables |= cpu_to_le32(
9928 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
9929 		}
9930 	}
9931 }
9932 
9933 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
9934 {
9935 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
9936 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
9937 		if (bp->link_info.advertising) {
9938 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
9939 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
9940 		}
9941 		if (bp->link_info.advertising_pam4) {
9942 			req->enables |=
9943 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
9944 			req->auto_link_pam4_speed_mask =
9945 				cpu_to_le16(bp->link_info.advertising_pam4);
9946 		}
9947 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
9948 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
9949 	} else {
9950 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
9951 		if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
9952 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
9953 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
9954 		} else {
9955 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
9956 		}
9957 	}
9958 
9959 	/* tell chimp that the setting takes effect immediately */
9960 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
9961 }
9962 
9963 int bnxt_hwrm_set_pause(struct bnxt *bp)
9964 {
9965 	struct hwrm_port_phy_cfg_input *req;
9966 	int rc;
9967 
9968 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
9969 	if (rc)
9970 		return rc;
9971 
9972 	bnxt_hwrm_set_pause_common(bp, req);
9973 
9974 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
9975 	    bp->link_info.force_link_chng)
9976 		bnxt_hwrm_set_link_common(bp, req);
9977 
9978 	rc = hwrm_req_send(bp, req);
9979 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
9980 		/* since changing of pause setting doesn't trigger any link
9981 		 * change event, the driver needs to update the current pause
9982 		 * result upon successfully return of the phy_cfg command
9983 		 */
9984 		bp->link_info.pause =
9985 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
9986 		bp->link_info.auto_pause_setting = 0;
9987 		if (!bp->link_info.force_link_chng)
9988 			bnxt_report_link(bp);
9989 	}
9990 	bp->link_info.force_link_chng = false;
9991 	return rc;
9992 }
9993 
9994 static void bnxt_hwrm_set_eee(struct bnxt *bp,
9995 			      struct hwrm_port_phy_cfg_input *req)
9996 {
9997 	struct ethtool_eee *eee = &bp->eee;
9998 
9999 	if (eee->eee_enabled) {
10000 		u16 eee_speeds;
10001 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
10002 
10003 		if (eee->tx_lpi_enabled)
10004 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
10005 		else
10006 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
10007 
10008 		req->flags |= cpu_to_le32(flags);
10009 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
10010 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
10011 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
10012 	} else {
10013 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
10014 	}
10015 }
10016 
10017 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
10018 {
10019 	struct hwrm_port_phy_cfg_input *req;
10020 	int rc;
10021 
10022 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
10023 	if (rc)
10024 		return rc;
10025 
10026 	if (set_pause)
10027 		bnxt_hwrm_set_pause_common(bp, req);
10028 
10029 	bnxt_hwrm_set_link_common(bp, req);
10030 
10031 	if (set_eee)
10032 		bnxt_hwrm_set_eee(bp, req);
10033 	return hwrm_req_send(bp, req);
10034 }
10035 
10036 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
10037 {
10038 	struct hwrm_port_phy_cfg_input *req;
10039 	int rc;
10040 
10041 	if (!BNXT_SINGLE_PF(bp))
10042 		return 0;
10043 
10044 	if (pci_num_vf(bp->pdev) &&
10045 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
10046 		return 0;
10047 
10048 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
10049 	if (rc)
10050 		return rc;
10051 
10052 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
10053 	rc = hwrm_req_send(bp, req);
10054 	if (!rc) {
10055 		mutex_lock(&bp->link_lock);
10056 		/* Device is not obliged link down in certain scenarios, even
10057 		 * when forced. Setting the state unknown is consistent with
10058 		 * driver startup and will force link state to be reported
10059 		 * during subsequent open based on PORT_PHY_QCFG.
10060 		 */
10061 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
10062 		mutex_unlock(&bp->link_lock);
10063 	}
10064 	return rc;
10065 }
10066 
10067 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
10068 {
10069 #ifdef CONFIG_TEE_BNXT_FW
10070 	int rc = tee_bnxt_fw_load();
10071 
10072 	if (rc)
10073 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
10074 
10075 	return rc;
10076 #else
10077 	netdev_err(bp->dev, "OP-TEE not supported\n");
10078 	return -ENODEV;
10079 #endif
10080 }
10081 
10082 static int bnxt_try_recover_fw(struct bnxt *bp)
10083 {
10084 	if (bp->fw_health && bp->fw_health->status_reliable) {
10085 		int retry = 0, rc;
10086 		u32 sts;
10087 
10088 		do {
10089 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
10090 			rc = bnxt_hwrm_poll(bp);
10091 			if (!BNXT_FW_IS_BOOTING(sts) &&
10092 			    !BNXT_FW_IS_RECOVERING(sts))
10093 				break;
10094 			retry++;
10095 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
10096 
10097 		if (!BNXT_FW_IS_HEALTHY(sts)) {
10098 			netdev_err(bp->dev,
10099 				   "Firmware not responding, status: 0x%x\n",
10100 				   sts);
10101 			rc = -ENODEV;
10102 		}
10103 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
10104 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
10105 			return bnxt_fw_reset_via_optee(bp);
10106 		}
10107 		return rc;
10108 	}
10109 
10110 	return -ENODEV;
10111 }
10112 
10113 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
10114 {
10115 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
10116 
10117 	if (!BNXT_NEW_RM(bp))
10118 		return; /* no resource reservations required */
10119 
10120 	hw_resc->resv_cp_rings = 0;
10121 	hw_resc->resv_stat_ctxs = 0;
10122 	hw_resc->resv_irqs = 0;
10123 	hw_resc->resv_tx_rings = 0;
10124 	hw_resc->resv_rx_rings = 0;
10125 	hw_resc->resv_hw_ring_grps = 0;
10126 	hw_resc->resv_vnics = 0;
10127 	if (!fw_reset) {
10128 		bp->tx_nr_rings = 0;
10129 		bp->rx_nr_rings = 0;
10130 	}
10131 }
10132 
10133 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
10134 {
10135 	int rc;
10136 
10137 	if (!BNXT_NEW_RM(bp))
10138 		return 0; /* no resource reservations required */
10139 
10140 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
10141 	if (rc)
10142 		netdev_err(bp->dev, "resc_qcaps failed\n");
10143 
10144 	bnxt_clear_reservations(bp, fw_reset);
10145 
10146 	return rc;
10147 }
10148 
10149 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
10150 {
10151 	struct hwrm_func_drv_if_change_output *resp;
10152 	struct hwrm_func_drv_if_change_input *req;
10153 	bool fw_reset = !bp->irq_tbl;
10154 	bool resc_reinit = false;
10155 	int rc, retry = 0;
10156 	u32 flags = 0;
10157 
10158 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
10159 		return 0;
10160 
10161 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
10162 	if (rc)
10163 		return rc;
10164 
10165 	if (up)
10166 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
10167 	resp = hwrm_req_hold(bp, req);
10168 
10169 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10170 	while (retry < BNXT_FW_IF_RETRY) {
10171 		rc = hwrm_req_send(bp, req);
10172 		if (rc != -EAGAIN)
10173 			break;
10174 
10175 		msleep(50);
10176 		retry++;
10177 	}
10178 
10179 	if (rc == -EAGAIN) {
10180 		hwrm_req_drop(bp, req);
10181 		return rc;
10182 	} else if (!rc) {
10183 		flags = le32_to_cpu(resp->flags);
10184 	} else if (up) {
10185 		rc = bnxt_try_recover_fw(bp);
10186 		fw_reset = true;
10187 	}
10188 	hwrm_req_drop(bp, req);
10189 	if (rc)
10190 		return rc;
10191 
10192 	if (!up) {
10193 		bnxt_inv_fw_health_reg(bp);
10194 		return 0;
10195 	}
10196 
10197 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
10198 		resc_reinit = true;
10199 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
10200 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
10201 		fw_reset = true;
10202 	else
10203 		bnxt_remap_fw_health_regs(bp);
10204 
10205 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
10206 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
10207 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
10208 		return -ENODEV;
10209 	}
10210 	if (resc_reinit || fw_reset) {
10211 		if (fw_reset) {
10212 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
10213 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
10214 				bnxt_ulp_stop(bp);
10215 			bnxt_free_ctx_mem(bp);
10216 			kfree(bp->ctx);
10217 			bp->ctx = NULL;
10218 			bnxt_dcb_free(bp);
10219 			rc = bnxt_fw_init_one(bp);
10220 			if (rc) {
10221 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
10222 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
10223 				return rc;
10224 			}
10225 			bnxt_clear_int_mode(bp);
10226 			rc = bnxt_init_int_mode(bp);
10227 			if (rc) {
10228 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
10229 				netdev_err(bp->dev, "init int mode failed\n");
10230 				return rc;
10231 			}
10232 		}
10233 		rc = bnxt_cancel_reservations(bp, fw_reset);
10234 	}
10235 	return rc;
10236 }
10237 
10238 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
10239 {
10240 	struct hwrm_port_led_qcaps_output *resp;
10241 	struct hwrm_port_led_qcaps_input *req;
10242 	struct bnxt_pf_info *pf = &bp->pf;
10243 	int rc;
10244 
10245 	bp->num_leds = 0;
10246 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
10247 		return 0;
10248 
10249 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
10250 	if (rc)
10251 		return rc;
10252 
10253 	req->port_id = cpu_to_le16(pf->port_id);
10254 	resp = hwrm_req_hold(bp, req);
10255 	rc = hwrm_req_send(bp, req);
10256 	if (rc) {
10257 		hwrm_req_drop(bp, req);
10258 		return rc;
10259 	}
10260 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
10261 		int i;
10262 
10263 		bp->num_leds = resp->num_leds;
10264 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
10265 						 bp->num_leds);
10266 		for (i = 0; i < bp->num_leds; i++) {
10267 			struct bnxt_led_info *led = &bp->leds[i];
10268 			__le16 caps = led->led_state_caps;
10269 
10270 			if (!led->led_group_id ||
10271 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
10272 				bp->num_leds = 0;
10273 				break;
10274 			}
10275 		}
10276 	}
10277 	hwrm_req_drop(bp, req);
10278 	return 0;
10279 }
10280 
10281 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
10282 {
10283 	struct hwrm_wol_filter_alloc_output *resp;
10284 	struct hwrm_wol_filter_alloc_input *req;
10285 	int rc;
10286 
10287 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
10288 	if (rc)
10289 		return rc;
10290 
10291 	req->port_id = cpu_to_le16(bp->pf.port_id);
10292 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
10293 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
10294 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
10295 
10296 	resp = hwrm_req_hold(bp, req);
10297 	rc = hwrm_req_send(bp, req);
10298 	if (!rc)
10299 		bp->wol_filter_id = resp->wol_filter_id;
10300 	hwrm_req_drop(bp, req);
10301 	return rc;
10302 }
10303 
10304 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
10305 {
10306 	struct hwrm_wol_filter_free_input *req;
10307 	int rc;
10308 
10309 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
10310 	if (rc)
10311 		return rc;
10312 
10313 	req->port_id = cpu_to_le16(bp->pf.port_id);
10314 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
10315 	req->wol_filter_id = bp->wol_filter_id;
10316 
10317 	return hwrm_req_send(bp, req);
10318 }
10319 
10320 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
10321 {
10322 	struct hwrm_wol_filter_qcfg_output *resp;
10323 	struct hwrm_wol_filter_qcfg_input *req;
10324 	u16 next_handle = 0;
10325 	int rc;
10326 
10327 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
10328 	if (rc)
10329 		return rc;
10330 
10331 	req->port_id = cpu_to_le16(bp->pf.port_id);
10332 	req->handle = cpu_to_le16(handle);
10333 	resp = hwrm_req_hold(bp, req);
10334 	rc = hwrm_req_send(bp, req);
10335 	if (!rc) {
10336 		next_handle = le16_to_cpu(resp->next_handle);
10337 		if (next_handle != 0) {
10338 			if (resp->wol_type ==
10339 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
10340 				bp->wol = 1;
10341 				bp->wol_filter_id = resp->wol_filter_id;
10342 			}
10343 		}
10344 	}
10345 	hwrm_req_drop(bp, req);
10346 	return next_handle;
10347 }
10348 
10349 static void bnxt_get_wol_settings(struct bnxt *bp)
10350 {
10351 	u16 handle = 0;
10352 
10353 	bp->wol = 0;
10354 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
10355 		return;
10356 
10357 	do {
10358 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
10359 	} while (handle && handle != 0xffff);
10360 }
10361 
10362 static bool bnxt_eee_config_ok(struct bnxt *bp)
10363 {
10364 	struct ethtool_eee *eee = &bp->eee;
10365 	struct bnxt_link_info *link_info = &bp->link_info;
10366 
10367 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
10368 		return true;
10369 
10370 	if (eee->eee_enabled) {
10371 		u32 advertising =
10372 			_bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
10373 
10374 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
10375 			eee->eee_enabled = 0;
10376 			return false;
10377 		}
10378 		if (eee->advertised & ~advertising) {
10379 			eee->advertised = advertising & eee->supported;
10380 			return false;
10381 		}
10382 	}
10383 	return true;
10384 }
10385 
10386 static int bnxt_update_phy_setting(struct bnxt *bp)
10387 {
10388 	int rc;
10389 	bool update_link = false;
10390 	bool update_pause = false;
10391 	bool update_eee = false;
10392 	struct bnxt_link_info *link_info = &bp->link_info;
10393 
10394 	rc = bnxt_update_link(bp, true);
10395 	if (rc) {
10396 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
10397 			   rc);
10398 		return rc;
10399 	}
10400 	if (!BNXT_SINGLE_PF(bp))
10401 		return 0;
10402 
10403 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
10404 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
10405 	    link_info->req_flow_ctrl)
10406 		update_pause = true;
10407 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
10408 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
10409 		update_pause = true;
10410 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
10411 		if (BNXT_AUTO_MODE(link_info->auto_mode))
10412 			update_link = true;
10413 		if (bnxt_force_speed_updated(link_info))
10414 			update_link = true;
10415 		if (link_info->req_duplex != link_info->duplex_setting)
10416 			update_link = true;
10417 	} else {
10418 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
10419 			update_link = true;
10420 		if (bnxt_auto_speed_updated(link_info))
10421 			update_link = true;
10422 	}
10423 
10424 	/* The last close may have shutdown the link, so need to call
10425 	 * PHY_CFG to bring it back up.
10426 	 */
10427 	if (!BNXT_LINK_IS_UP(bp))
10428 		update_link = true;
10429 
10430 	if (!bnxt_eee_config_ok(bp))
10431 		update_eee = true;
10432 
10433 	if (update_link)
10434 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
10435 	else if (update_pause)
10436 		rc = bnxt_hwrm_set_pause(bp);
10437 	if (rc) {
10438 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
10439 			   rc);
10440 		return rc;
10441 	}
10442 
10443 	return rc;
10444 }
10445 
10446 /* Common routine to pre-map certain register block to different GRC window.
10447  * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
10448  * in PF and 3 windows in VF that can be customized to map in different
10449  * register blocks.
10450  */
10451 static void bnxt_preset_reg_win(struct bnxt *bp)
10452 {
10453 	if (BNXT_PF(bp)) {
10454 		/* CAG registers map to GRC window #4 */
10455 		writel(BNXT_CAG_REG_BASE,
10456 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
10457 	}
10458 }
10459 
10460 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
10461 
10462 static int bnxt_reinit_after_abort(struct bnxt *bp)
10463 {
10464 	int rc;
10465 
10466 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
10467 		return -EBUSY;
10468 
10469 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
10470 		return -ENODEV;
10471 
10472 	rc = bnxt_fw_init_one(bp);
10473 	if (!rc) {
10474 		bnxt_clear_int_mode(bp);
10475 		rc = bnxt_init_int_mode(bp);
10476 		if (!rc) {
10477 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
10478 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
10479 		}
10480 	}
10481 	return rc;
10482 }
10483 
10484 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
10485 {
10486 	int rc = 0;
10487 
10488 	bnxt_preset_reg_win(bp);
10489 	netif_carrier_off(bp->dev);
10490 	if (irq_re_init) {
10491 		/* Reserve rings now if none were reserved at driver probe. */
10492 		rc = bnxt_init_dflt_ring_mode(bp);
10493 		if (rc) {
10494 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
10495 			return rc;
10496 		}
10497 	}
10498 	rc = bnxt_reserve_rings(bp, irq_re_init);
10499 	if (rc)
10500 		return rc;
10501 	if ((bp->flags & BNXT_FLAG_RFS) &&
10502 	    !(bp->flags & BNXT_FLAG_USING_MSIX)) {
10503 		/* disable RFS if falling back to INTA */
10504 		bp->dev->hw_features &= ~NETIF_F_NTUPLE;
10505 		bp->flags &= ~BNXT_FLAG_RFS;
10506 	}
10507 
10508 	rc = bnxt_alloc_mem(bp, irq_re_init);
10509 	if (rc) {
10510 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
10511 		goto open_err_free_mem;
10512 	}
10513 
10514 	if (irq_re_init) {
10515 		bnxt_init_napi(bp);
10516 		rc = bnxt_request_irq(bp);
10517 		if (rc) {
10518 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
10519 			goto open_err_irq;
10520 		}
10521 	}
10522 
10523 	rc = bnxt_init_nic(bp, irq_re_init);
10524 	if (rc) {
10525 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
10526 		goto open_err_irq;
10527 	}
10528 
10529 	bnxt_enable_napi(bp);
10530 	bnxt_debug_dev_init(bp);
10531 
10532 	if (link_re_init) {
10533 		mutex_lock(&bp->link_lock);
10534 		rc = bnxt_update_phy_setting(bp);
10535 		mutex_unlock(&bp->link_lock);
10536 		if (rc) {
10537 			netdev_warn(bp->dev, "failed to update phy settings\n");
10538 			if (BNXT_SINGLE_PF(bp)) {
10539 				bp->link_info.phy_retry = true;
10540 				bp->link_info.phy_retry_expires =
10541 					jiffies + 5 * HZ;
10542 			}
10543 		}
10544 	}
10545 
10546 	if (irq_re_init)
10547 		udp_tunnel_nic_reset_ntf(bp->dev);
10548 
10549 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
10550 		if (!static_key_enabled(&bnxt_xdp_locking_key))
10551 			static_branch_enable(&bnxt_xdp_locking_key);
10552 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
10553 		static_branch_disable(&bnxt_xdp_locking_key);
10554 	}
10555 	set_bit(BNXT_STATE_OPEN, &bp->state);
10556 	bnxt_enable_int(bp);
10557 	/* Enable TX queues */
10558 	bnxt_tx_enable(bp);
10559 	mod_timer(&bp->timer, jiffies + bp->current_interval);
10560 	/* Poll link status and check for SFP+ module status */
10561 	mutex_lock(&bp->link_lock);
10562 	bnxt_get_port_module_status(bp);
10563 	mutex_unlock(&bp->link_lock);
10564 
10565 	/* VF-reps may need to be re-opened after the PF is re-opened */
10566 	if (BNXT_PF(bp))
10567 		bnxt_vf_reps_open(bp);
10568 	bnxt_ptp_init_rtc(bp, true);
10569 	bnxt_ptp_cfg_tstamp_filters(bp);
10570 	return 0;
10571 
10572 open_err_irq:
10573 	bnxt_del_napi(bp);
10574 
10575 open_err_free_mem:
10576 	bnxt_free_skbs(bp);
10577 	bnxt_free_irq(bp);
10578 	bnxt_free_mem(bp, true);
10579 	return rc;
10580 }
10581 
10582 /* rtnl_lock held */
10583 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
10584 {
10585 	int rc = 0;
10586 
10587 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
10588 		rc = -EIO;
10589 	if (!rc)
10590 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
10591 	if (rc) {
10592 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
10593 		dev_close(bp->dev);
10594 	}
10595 	return rc;
10596 }
10597 
10598 /* rtnl_lock held, open the NIC half way by allocating all resources, but
10599  * NAPI, IRQ, and TX are not enabled.  This is mainly used for offline
10600  * self tests.
10601  */
10602 int bnxt_half_open_nic(struct bnxt *bp)
10603 {
10604 	int rc = 0;
10605 
10606 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
10607 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
10608 		rc = -ENODEV;
10609 		goto half_open_err;
10610 	}
10611 
10612 	rc = bnxt_alloc_mem(bp, true);
10613 	if (rc) {
10614 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
10615 		goto half_open_err;
10616 	}
10617 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
10618 	rc = bnxt_init_nic(bp, true);
10619 	if (rc) {
10620 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
10621 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
10622 		goto half_open_err;
10623 	}
10624 	return 0;
10625 
10626 half_open_err:
10627 	bnxt_free_skbs(bp);
10628 	bnxt_free_mem(bp, true);
10629 	dev_close(bp->dev);
10630 	return rc;
10631 }
10632 
10633 /* rtnl_lock held, this call can only be made after a previous successful
10634  * call to bnxt_half_open_nic().
10635  */
10636 void bnxt_half_close_nic(struct bnxt *bp)
10637 {
10638 	bnxt_hwrm_resource_free(bp, false, true);
10639 	bnxt_free_skbs(bp);
10640 	bnxt_free_mem(bp, true);
10641 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
10642 }
10643 
10644 void bnxt_reenable_sriov(struct bnxt *bp)
10645 {
10646 	if (BNXT_PF(bp)) {
10647 		struct bnxt_pf_info *pf = &bp->pf;
10648 		int n = pf->active_vfs;
10649 
10650 		if (n)
10651 			bnxt_cfg_hw_sriov(bp, &n, true);
10652 	}
10653 }
10654 
10655 static int bnxt_open(struct net_device *dev)
10656 {
10657 	struct bnxt *bp = netdev_priv(dev);
10658 	int rc;
10659 
10660 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
10661 		rc = bnxt_reinit_after_abort(bp);
10662 		if (rc) {
10663 			if (rc == -EBUSY)
10664 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
10665 			else
10666 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
10667 			return -ENODEV;
10668 		}
10669 	}
10670 
10671 	rc = bnxt_hwrm_if_change(bp, true);
10672 	if (rc)
10673 		return rc;
10674 
10675 	rc = __bnxt_open_nic(bp, true, true);
10676 	if (rc) {
10677 		bnxt_hwrm_if_change(bp, false);
10678 	} else {
10679 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
10680 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
10681 				bnxt_ulp_start(bp, 0);
10682 				bnxt_reenable_sriov(bp);
10683 			}
10684 		}
10685 	}
10686 
10687 	return rc;
10688 }
10689 
10690 static bool bnxt_drv_busy(struct bnxt *bp)
10691 {
10692 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
10693 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
10694 }
10695 
10696 static void bnxt_get_ring_stats(struct bnxt *bp,
10697 				struct rtnl_link_stats64 *stats);
10698 
10699 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
10700 			     bool link_re_init)
10701 {
10702 	/* Close the VF-reps before closing PF */
10703 	if (BNXT_PF(bp))
10704 		bnxt_vf_reps_close(bp);
10705 
10706 	/* Change device state to avoid TX queue wake up's */
10707 	bnxt_tx_disable(bp);
10708 
10709 	clear_bit(BNXT_STATE_OPEN, &bp->state);
10710 	smp_mb__after_atomic();
10711 	while (bnxt_drv_busy(bp))
10712 		msleep(20);
10713 
10714 	/* Flush rings and disable interrupts */
10715 	bnxt_shutdown_nic(bp, irq_re_init);
10716 
10717 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
10718 
10719 	bnxt_debug_dev_exit(bp);
10720 	bnxt_disable_napi(bp);
10721 	del_timer_sync(&bp->timer);
10722 	bnxt_free_skbs(bp);
10723 
10724 	/* Save ring stats before shutdown */
10725 	if (bp->bnapi && irq_re_init) {
10726 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
10727 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
10728 	}
10729 	if (irq_re_init) {
10730 		bnxt_free_irq(bp);
10731 		bnxt_del_napi(bp);
10732 	}
10733 	bnxt_free_mem(bp, irq_re_init);
10734 }
10735 
10736 int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
10737 {
10738 	int rc = 0;
10739 
10740 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
10741 		/* If we get here, it means firmware reset is in progress
10742 		 * while we are trying to close.  We can safely proceed with
10743 		 * the close because we are holding rtnl_lock().  Some firmware
10744 		 * messages may fail as we proceed to close.  We set the
10745 		 * ABORT_ERR flag here so that the FW reset thread will later
10746 		 * abort when it gets the rtnl_lock() and sees the flag.
10747 		 */
10748 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
10749 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
10750 	}
10751 
10752 #ifdef CONFIG_BNXT_SRIOV
10753 	if (bp->sriov_cfg) {
10754 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
10755 						      !bp->sriov_cfg,
10756 						      BNXT_SRIOV_CFG_WAIT_TMO);
10757 		if (rc)
10758 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
10759 	}
10760 #endif
10761 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
10762 	return rc;
10763 }
10764 
10765 static int bnxt_close(struct net_device *dev)
10766 {
10767 	struct bnxt *bp = netdev_priv(dev);
10768 
10769 	bnxt_close_nic(bp, true, true);
10770 	bnxt_hwrm_shutdown_link(bp);
10771 	bnxt_hwrm_if_change(bp, false);
10772 	return 0;
10773 }
10774 
10775 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
10776 				   u16 *val)
10777 {
10778 	struct hwrm_port_phy_mdio_read_output *resp;
10779 	struct hwrm_port_phy_mdio_read_input *req;
10780 	int rc;
10781 
10782 	if (bp->hwrm_spec_code < 0x10a00)
10783 		return -EOPNOTSUPP;
10784 
10785 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
10786 	if (rc)
10787 		return rc;
10788 
10789 	req->port_id = cpu_to_le16(bp->pf.port_id);
10790 	req->phy_addr = phy_addr;
10791 	req->reg_addr = cpu_to_le16(reg & 0x1f);
10792 	if (mdio_phy_id_is_c45(phy_addr)) {
10793 		req->cl45_mdio = 1;
10794 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
10795 		req->dev_addr = mdio_phy_id_devad(phy_addr);
10796 		req->reg_addr = cpu_to_le16(reg);
10797 	}
10798 
10799 	resp = hwrm_req_hold(bp, req);
10800 	rc = hwrm_req_send(bp, req);
10801 	if (!rc)
10802 		*val = le16_to_cpu(resp->reg_data);
10803 	hwrm_req_drop(bp, req);
10804 	return rc;
10805 }
10806 
10807 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
10808 				    u16 val)
10809 {
10810 	struct hwrm_port_phy_mdio_write_input *req;
10811 	int rc;
10812 
10813 	if (bp->hwrm_spec_code < 0x10a00)
10814 		return -EOPNOTSUPP;
10815 
10816 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
10817 	if (rc)
10818 		return rc;
10819 
10820 	req->port_id = cpu_to_le16(bp->pf.port_id);
10821 	req->phy_addr = phy_addr;
10822 	req->reg_addr = cpu_to_le16(reg & 0x1f);
10823 	if (mdio_phy_id_is_c45(phy_addr)) {
10824 		req->cl45_mdio = 1;
10825 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
10826 		req->dev_addr = mdio_phy_id_devad(phy_addr);
10827 		req->reg_addr = cpu_to_le16(reg);
10828 	}
10829 	req->reg_data = cpu_to_le16(val);
10830 
10831 	return hwrm_req_send(bp, req);
10832 }
10833 
10834 /* rtnl_lock held */
10835 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
10836 {
10837 	struct mii_ioctl_data *mdio = if_mii(ifr);
10838 	struct bnxt *bp = netdev_priv(dev);
10839 	int rc;
10840 
10841 	switch (cmd) {
10842 	case SIOCGMIIPHY:
10843 		mdio->phy_id = bp->link_info.phy_addr;
10844 
10845 		fallthrough;
10846 	case SIOCGMIIREG: {
10847 		u16 mii_regval = 0;
10848 
10849 		if (!netif_running(dev))
10850 			return -EAGAIN;
10851 
10852 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
10853 					     &mii_regval);
10854 		mdio->val_out = mii_regval;
10855 		return rc;
10856 	}
10857 
10858 	case SIOCSMIIREG:
10859 		if (!netif_running(dev))
10860 			return -EAGAIN;
10861 
10862 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
10863 						mdio->val_in);
10864 
10865 	case SIOCSHWTSTAMP:
10866 		return bnxt_hwtstamp_set(dev, ifr);
10867 
10868 	case SIOCGHWTSTAMP:
10869 		return bnxt_hwtstamp_get(dev, ifr);
10870 
10871 	default:
10872 		/* do nothing */
10873 		break;
10874 	}
10875 	return -EOPNOTSUPP;
10876 }
10877 
10878 static void bnxt_get_ring_stats(struct bnxt *bp,
10879 				struct rtnl_link_stats64 *stats)
10880 {
10881 	int i;
10882 
10883 	for (i = 0; i < bp->cp_nr_rings; i++) {
10884 		struct bnxt_napi *bnapi = bp->bnapi[i];
10885 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
10886 		u64 *sw = cpr->stats.sw_stats;
10887 
10888 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
10889 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
10890 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
10891 
10892 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
10893 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
10894 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
10895 
10896 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
10897 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
10898 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
10899 
10900 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
10901 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
10902 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
10903 
10904 		stats->rx_missed_errors +=
10905 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
10906 
10907 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
10908 
10909 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
10910 
10911 		stats->rx_dropped +=
10912 			cpr->sw_stats.rx.rx_netpoll_discards +
10913 			cpr->sw_stats.rx.rx_oom_discards;
10914 	}
10915 }
10916 
10917 static void bnxt_add_prev_stats(struct bnxt *bp,
10918 				struct rtnl_link_stats64 *stats)
10919 {
10920 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
10921 
10922 	stats->rx_packets += prev_stats->rx_packets;
10923 	stats->tx_packets += prev_stats->tx_packets;
10924 	stats->rx_bytes += prev_stats->rx_bytes;
10925 	stats->tx_bytes += prev_stats->tx_bytes;
10926 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
10927 	stats->multicast += prev_stats->multicast;
10928 	stats->rx_dropped += prev_stats->rx_dropped;
10929 	stats->tx_dropped += prev_stats->tx_dropped;
10930 }
10931 
10932 static void
10933 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
10934 {
10935 	struct bnxt *bp = netdev_priv(dev);
10936 
10937 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
10938 	/* Make sure bnxt_close_nic() sees that we are reading stats before
10939 	 * we check the BNXT_STATE_OPEN flag.
10940 	 */
10941 	smp_mb__after_atomic();
10942 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
10943 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
10944 		*stats = bp->net_stats_prev;
10945 		return;
10946 	}
10947 
10948 	bnxt_get_ring_stats(bp, stats);
10949 	bnxt_add_prev_stats(bp, stats);
10950 
10951 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10952 		u64 *rx = bp->port_stats.sw_stats;
10953 		u64 *tx = bp->port_stats.sw_stats +
10954 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10955 
10956 		stats->rx_crc_errors =
10957 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
10958 		stats->rx_frame_errors =
10959 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
10960 		stats->rx_length_errors =
10961 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
10962 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
10963 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
10964 		stats->rx_errors =
10965 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
10966 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
10967 		stats->collisions =
10968 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
10969 		stats->tx_fifo_errors =
10970 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
10971 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
10972 	}
10973 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
10974 }
10975 
10976 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
10977 					struct bnxt_total_ring_err_stats *stats,
10978 					struct bnxt_cp_ring_info *cpr)
10979 {
10980 	struct bnxt_sw_stats *sw_stats = &cpr->sw_stats;
10981 	u64 *hw_stats = cpr->stats.sw_stats;
10982 
10983 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
10984 	stats->rx_total_resets += sw_stats->rx.rx_resets;
10985 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
10986 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
10987 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
10988 	stats->rx_total_ring_discards +=
10989 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
10990 	stats->tx_total_resets += sw_stats->tx.tx_resets;
10991 	stats->tx_total_ring_discards +=
10992 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
10993 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
10994 }
10995 
10996 void bnxt_get_ring_err_stats(struct bnxt *bp,
10997 			     struct bnxt_total_ring_err_stats *stats)
10998 {
10999 	int i;
11000 
11001 	for (i = 0; i < bp->cp_nr_rings; i++)
11002 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
11003 }
11004 
11005 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
11006 {
11007 	struct net_device *dev = bp->dev;
11008 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
11009 	struct netdev_hw_addr *ha;
11010 	u8 *haddr;
11011 	int mc_count = 0;
11012 	bool update = false;
11013 	int off = 0;
11014 
11015 	netdev_for_each_mc_addr(ha, dev) {
11016 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
11017 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
11018 			vnic->mc_list_count = 0;
11019 			return false;
11020 		}
11021 		haddr = ha->addr;
11022 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
11023 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
11024 			update = true;
11025 		}
11026 		off += ETH_ALEN;
11027 		mc_count++;
11028 	}
11029 	if (mc_count)
11030 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
11031 
11032 	if (mc_count != vnic->mc_list_count) {
11033 		vnic->mc_list_count = mc_count;
11034 		update = true;
11035 	}
11036 	return update;
11037 }
11038 
11039 static bool bnxt_uc_list_updated(struct bnxt *bp)
11040 {
11041 	struct net_device *dev = bp->dev;
11042 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
11043 	struct netdev_hw_addr *ha;
11044 	int off = 0;
11045 
11046 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
11047 		return true;
11048 
11049 	netdev_for_each_uc_addr(ha, dev) {
11050 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
11051 			return true;
11052 
11053 		off += ETH_ALEN;
11054 	}
11055 	return false;
11056 }
11057 
11058 static void bnxt_set_rx_mode(struct net_device *dev)
11059 {
11060 	struct bnxt *bp = netdev_priv(dev);
11061 	struct bnxt_vnic_info *vnic;
11062 	bool mc_update = false;
11063 	bool uc_update;
11064 	u32 mask;
11065 
11066 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
11067 		return;
11068 
11069 	vnic = &bp->vnic_info[0];
11070 	mask = vnic->rx_mask;
11071 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
11072 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
11073 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
11074 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
11075 
11076 	if (dev->flags & IFF_PROMISC)
11077 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11078 
11079 	uc_update = bnxt_uc_list_updated(bp);
11080 
11081 	if (dev->flags & IFF_BROADCAST)
11082 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
11083 	if (dev->flags & IFF_ALLMULTI) {
11084 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
11085 		vnic->mc_list_count = 0;
11086 	} else if (dev->flags & IFF_MULTICAST) {
11087 		mc_update = bnxt_mc_list_updated(bp, &mask);
11088 	}
11089 
11090 	if (mask != vnic->rx_mask || uc_update || mc_update) {
11091 		vnic->rx_mask = mask;
11092 
11093 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
11094 	}
11095 }
11096 
11097 static int bnxt_cfg_rx_mode(struct bnxt *bp)
11098 {
11099 	struct net_device *dev = bp->dev;
11100 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
11101 	struct hwrm_cfa_l2_filter_free_input *req;
11102 	struct netdev_hw_addr *ha;
11103 	int i, off = 0, rc;
11104 	bool uc_update;
11105 
11106 	netif_addr_lock_bh(dev);
11107 	uc_update = bnxt_uc_list_updated(bp);
11108 	netif_addr_unlock_bh(dev);
11109 
11110 	if (!uc_update)
11111 		goto skip_uc;
11112 
11113 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
11114 	if (rc)
11115 		return rc;
11116 	hwrm_req_hold(bp, req);
11117 	for (i = 1; i < vnic->uc_filter_count; i++) {
11118 		req->l2_filter_id = vnic->fw_l2_filter_id[i];
11119 
11120 		rc = hwrm_req_send(bp, req);
11121 	}
11122 	hwrm_req_drop(bp, req);
11123 
11124 	vnic->uc_filter_count = 1;
11125 
11126 	netif_addr_lock_bh(dev);
11127 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
11128 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11129 	} else {
11130 		netdev_for_each_uc_addr(ha, dev) {
11131 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
11132 			off += ETH_ALEN;
11133 			vnic->uc_filter_count++;
11134 		}
11135 	}
11136 	netif_addr_unlock_bh(dev);
11137 
11138 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
11139 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
11140 		if (rc) {
11141 			if (BNXT_VF(bp) && rc == -ENODEV) {
11142 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
11143 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
11144 				else
11145 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
11146 				rc = 0;
11147 			} else {
11148 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
11149 			}
11150 			vnic->uc_filter_count = i;
11151 			return rc;
11152 		}
11153 	}
11154 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
11155 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
11156 
11157 skip_uc:
11158 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
11159 	    !bnxt_promisc_ok(bp))
11160 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11161 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
11162 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
11163 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
11164 			    rc);
11165 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
11166 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
11167 		vnic->mc_list_count = 0;
11168 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
11169 	}
11170 	if (rc)
11171 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
11172 			   rc);
11173 
11174 	return rc;
11175 }
11176 
11177 static bool bnxt_can_reserve_rings(struct bnxt *bp)
11178 {
11179 #ifdef CONFIG_BNXT_SRIOV
11180 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
11181 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11182 
11183 		/* No minimum rings were provisioned by the PF.  Don't
11184 		 * reserve rings by default when device is down.
11185 		 */
11186 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
11187 			return true;
11188 
11189 		if (!netif_running(bp->dev))
11190 			return false;
11191 	}
11192 #endif
11193 	return true;
11194 }
11195 
11196 /* If the chip and firmware supports RFS */
11197 static bool bnxt_rfs_supported(struct bnxt *bp)
11198 {
11199 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
11200 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
11201 			return true;
11202 		return false;
11203 	}
11204 	/* 212 firmware is broken for aRFS */
11205 	if (BNXT_FW_MAJ(bp) == 212)
11206 		return false;
11207 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
11208 		return true;
11209 	if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
11210 		return true;
11211 	return false;
11212 }
11213 
11214 /* If runtime conditions support RFS */
11215 static bool bnxt_rfs_capable(struct bnxt *bp)
11216 {
11217 #ifdef CONFIG_RFS_ACCEL
11218 	int vnics, max_vnics, max_rss_ctxs;
11219 
11220 	if (bp->flags & BNXT_FLAG_CHIP_P5)
11221 		return bnxt_rfs_supported(bp);
11222 	if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
11223 		return false;
11224 
11225 	vnics = 1 + bp->rx_nr_rings;
11226 	max_vnics = bnxt_get_max_func_vnics(bp);
11227 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
11228 
11229 	/* RSS contexts not a limiting factor */
11230 	if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
11231 		max_rss_ctxs = max_vnics;
11232 	if (vnics > max_vnics || vnics > max_rss_ctxs) {
11233 		if (bp->rx_nr_rings > 1)
11234 			netdev_warn(bp->dev,
11235 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
11236 				    min(max_rss_ctxs - 1, max_vnics - 1));
11237 		return false;
11238 	}
11239 
11240 	if (!BNXT_NEW_RM(bp))
11241 		return true;
11242 
11243 	if (vnics == bp->hw_resc.resv_vnics)
11244 		return true;
11245 
11246 	bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, vnics);
11247 	if (vnics <= bp->hw_resc.resv_vnics)
11248 		return true;
11249 
11250 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
11251 	bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, 1);
11252 	return false;
11253 #else
11254 	return false;
11255 #endif
11256 }
11257 
11258 static netdev_features_t bnxt_fix_features(struct net_device *dev,
11259 					   netdev_features_t features)
11260 {
11261 	struct bnxt *bp = netdev_priv(dev);
11262 	netdev_features_t vlan_features;
11263 
11264 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
11265 		features &= ~NETIF_F_NTUPLE;
11266 
11267 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
11268 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
11269 
11270 	if (!(features & NETIF_F_GRO))
11271 		features &= ~NETIF_F_GRO_HW;
11272 
11273 	if (features & NETIF_F_GRO_HW)
11274 		features &= ~NETIF_F_LRO;
11275 
11276 	/* Both CTAG and STAG VLAN accelaration on the RX side have to be
11277 	 * turned on or off together.
11278 	 */
11279 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
11280 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
11281 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
11282 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
11283 		else if (vlan_features)
11284 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
11285 	}
11286 #ifdef CONFIG_BNXT_SRIOV
11287 	if (BNXT_VF(bp) && bp->vf.vlan)
11288 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
11289 #endif
11290 	return features;
11291 }
11292 
11293 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
11294 {
11295 	struct bnxt *bp = netdev_priv(dev);
11296 	u32 flags = bp->flags;
11297 	u32 changes;
11298 	int rc = 0;
11299 	bool re_init = false;
11300 	bool update_tpa = false;
11301 
11302 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
11303 	if (features & NETIF_F_GRO_HW)
11304 		flags |= BNXT_FLAG_GRO;
11305 	else if (features & NETIF_F_LRO)
11306 		flags |= BNXT_FLAG_LRO;
11307 
11308 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
11309 		flags &= ~BNXT_FLAG_TPA;
11310 
11311 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
11312 		flags |= BNXT_FLAG_STRIP_VLAN;
11313 
11314 	if (features & NETIF_F_NTUPLE)
11315 		flags |= BNXT_FLAG_RFS;
11316 
11317 	changes = flags ^ bp->flags;
11318 	if (changes & BNXT_FLAG_TPA) {
11319 		update_tpa = true;
11320 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
11321 		    (flags & BNXT_FLAG_TPA) == 0 ||
11322 		    (bp->flags & BNXT_FLAG_CHIP_P5))
11323 			re_init = true;
11324 	}
11325 
11326 	if (changes & ~BNXT_FLAG_TPA)
11327 		re_init = true;
11328 
11329 	if (flags != bp->flags) {
11330 		u32 old_flags = bp->flags;
11331 
11332 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
11333 			bp->flags = flags;
11334 			if (update_tpa)
11335 				bnxt_set_ring_params(bp);
11336 			return rc;
11337 		}
11338 
11339 		if (re_init) {
11340 			bnxt_close_nic(bp, false, false);
11341 			bp->flags = flags;
11342 			if (update_tpa)
11343 				bnxt_set_ring_params(bp);
11344 
11345 			return bnxt_open_nic(bp, false, false);
11346 		}
11347 		if (update_tpa) {
11348 			bp->flags = flags;
11349 			rc = bnxt_set_tpa(bp,
11350 					  (flags & BNXT_FLAG_TPA) ?
11351 					  true : false);
11352 			if (rc)
11353 				bp->flags = old_flags;
11354 		}
11355 	}
11356 	return rc;
11357 }
11358 
11359 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
11360 			      u8 **nextp)
11361 {
11362 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
11363 	struct hop_jumbo_hdr *jhdr;
11364 	int hdr_count = 0;
11365 	u8 *nexthdr;
11366 	int start;
11367 
11368 	/* Check that there are at most 2 IPv6 extension headers, no
11369 	 * fragment header, and each is <= 64 bytes.
11370 	 */
11371 	start = nw_off + sizeof(*ip6h);
11372 	nexthdr = &ip6h->nexthdr;
11373 	while (ipv6_ext_hdr(*nexthdr)) {
11374 		struct ipv6_opt_hdr *hp;
11375 		int hdrlen;
11376 
11377 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
11378 		    *nexthdr == NEXTHDR_FRAGMENT)
11379 			return false;
11380 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
11381 					  skb_headlen(skb), NULL);
11382 		if (!hp)
11383 			return false;
11384 		if (*nexthdr == NEXTHDR_AUTH)
11385 			hdrlen = ipv6_authlen(hp);
11386 		else
11387 			hdrlen = ipv6_optlen(hp);
11388 
11389 		if (hdrlen > 64)
11390 			return false;
11391 
11392 		/* The ext header may be a hop-by-hop header inserted for
11393 		 * big TCP purposes. This will be removed before sending
11394 		 * from NIC, so do not count it.
11395 		 */
11396 		if (*nexthdr == NEXTHDR_HOP) {
11397 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
11398 				goto increment_hdr;
11399 
11400 			jhdr = (struct hop_jumbo_hdr *)hp;
11401 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
11402 			    jhdr->nexthdr != IPPROTO_TCP)
11403 				goto increment_hdr;
11404 
11405 			goto next_hdr;
11406 		}
11407 increment_hdr:
11408 		hdr_count++;
11409 next_hdr:
11410 		nexthdr = &hp->nexthdr;
11411 		start += hdrlen;
11412 	}
11413 	if (nextp) {
11414 		/* Caller will check inner protocol */
11415 		if (skb->encapsulation) {
11416 			*nextp = nexthdr;
11417 			return true;
11418 		}
11419 		*nextp = NULL;
11420 	}
11421 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
11422 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
11423 }
11424 
11425 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
11426 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
11427 {
11428 	struct udphdr *uh = udp_hdr(skb);
11429 	__be16 udp_port = uh->dest;
11430 
11431 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port)
11432 		return false;
11433 	if (skb->inner_protocol_type == ENCAP_TYPE_ETHER) {
11434 		struct ethhdr *eh = inner_eth_hdr(skb);
11435 
11436 		switch (eh->h_proto) {
11437 		case htons(ETH_P_IP):
11438 			return true;
11439 		case htons(ETH_P_IPV6):
11440 			return bnxt_exthdr_check(bp, skb,
11441 						 skb_inner_network_offset(skb),
11442 						 NULL);
11443 		}
11444 	}
11445 	return false;
11446 }
11447 
11448 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
11449 {
11450 	switch (l4_proto) {
11451 	case IPPROTO_UDP:
11452 		return bnxt_udp_tunl_check(bp, skb);
11453 	case IPPROTO_IPIP:
11454 		return true;
11455 	case IPPROTO_GRE: {
11456 		switch (skb->inner_protocol) {
11457 		default:
11458 			return false;
11459 		case htons(ETH_P_IP):
11460 			return true;
11461 		case htons(ETH_P_IPV6):
11462 			fallthrough;
11463 		}
11464 	}
11465 	case IPPROTO_IPV6:
11466 		/* Check ext headers of inner ipv6 */
11467 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
11468 					 NULL);
11469 	}
11470 	return false;
11471 }
11472 
11473 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
11474 					     struct net_device *dev,
11475 					     netdev_features_t features)
11476 {
11477 	struct bnxt *bp = netdev_priv(dev);
11478 	u8 *l4_proto;
11479 
11480 	features = vlan_features_check(skb, features);
11481 	switch (vlan_get_protocol(skb)) {
11482 	case htons(ETH_P_IP):
11483 		if (!skb->encapsulation)
11484 			return features;
11485 		l4_proto = &ip_hdr(skb)->protocol;
11486 		if (bnxt_tunl_check(bp, skb, *l4_proto))
11487 			return features;
11488 		break;
11489 	case htons(ETH_P_IPV6):
11490 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
11491 				       &l4_proto))
11492 			break;
11493 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
11494 			return features;
11495 		break;
11496 	}
11497 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
11498 }
11499 
11500 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
11501 			 u32 *reg_buf)
11502 {
11503 	struct hwrm_dbg_read_direct_output *resp;
11504 	struct hwrm_dbg_read_direct_input *req;
11505 	__le32 *dbg_reg_buf;
11506 	dma_addr_t mapping;
11507 	int rc, i;
11508 
11509 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
11510 	if (rc)
11511 		return rc;
11512 
11513 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
11514 					 &mapping);
11515 	if (!dbg_reg_buf) {
11516 		rc = -ENOMEM;
11517 		goto dbg_rd_reg_exit;
11518 	}
11519 
11520 	req->host_dest_addr = cpu_to_le64(mapping);
11521 
11522 	resp = hwrm_req_hold(bp, req);
11523 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
11524 	req->read_len32 = cpu_to_le32(num_words);
11525 
11526 	rc = hwrm_req_send(bp, req);
11527 	if (rc || resp->error_code) {
11528 		rc = -EIO;
11529 		goto dbg_rd_reg_exit;
11530 	}
11531 	for (i = 0; i < num_words; i++)
11532 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
11533 
11534 dbg_rd_reg_exit:
11535 	hwrm_req_drop(bp, req);
11536 	return rc;
11537 }
11538 
11539 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
11540 				       u32 ring_id, u32 *prod, u32 *cons)
11541 {
11542 	struct hwrm_dbg_ring_info_get_output *resp;
11543 	struct hwrm_dbg_ring_info_get_input *req;
11544 	int rc;
11545 
11546 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
11547 	if (rc)
11548 		return rc;
11549 
11550 	req->ring_type = ring_type;
11551 	req->fw_ring_id = cpu_to_le32(ring_id);
11552 	resp = hwrm_req_hold(bp, req);
11553 	rc = hwrm_req_send(bp, req);
11554 	if (!rc) {
11555 		*prod = le32_to_cpu(resp->producer_index);
11556 		*cons = le32_to_cpu(resp->consumer_index);
11557 	}
11558 	hwrm_req_drop(bp, req);
11559 	return rc;
11560 }
11561 
11562 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
11563 {
11564 	struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
11565 	int i = bnapi->index;
11566 
11567 	if (!txr)
11568 		return;
11569 
11570 	netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
11571 		    i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
11572 		    txr->tx_cons);
11573 }
11574 
11575 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
11576 {
11577 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
11578 	int i = bnapi->index;
11579 
11580 	if (!rxr)
11581 		return;
11582 
11583 	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",
11584 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
11585 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
11586 		    rxr->rx_sw_agg_prod);
11587 }
11588 
11589 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
11590 {
11591 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
11592 	int i = bnapi->index;
11593 
11594 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
11595 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
11596 }
11597 
11598 static void bnxt_dbg_dump_states(struct bnxt *bp)
11599 {
11600 	int i;
11601 	struct bnxt_napi *bnapi;
11602 
11603 	for (i = 0; i < bp->cp_nr_rings; i++) {
11604 		bnapi = bp->bnapi[i];
11605 		if (netif_msg_drv(bp)) {
11606 			bnxt_dump_tx_sw_state(bnapi);
11607 			bnxt_dump_rx_sw_state(bnapi);
11608 			bnxt_dump_cp_sw_state(bnapi);
11609 		}
11610 	}
11611 }
11612 
11613 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
11614 {
11615 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
11616 	struct hwrm_ring_reset_input *req;
11617 	struct bnxt_napi *bnapi = rxr->bnapi;
11618 	struct bnxt_cp_ring_info *cpr;
11619 	u16 cp_ring_id;
11620 	int rc;
11621 
11622 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
11623 	if (rc)
11624 		return rc;
11625 
11626 	cpr = &bnapi->cp_ring;
11627 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
11628 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
11629 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
11630 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
11631 	return hwrm_req_send_silent(bp, req);
11632 }
11633 
11634 static void bnxt_reset_task(struct bnxt *bp, bool silent)
11635 {
11636 	if (!silent)
11637 		bnxt_dbg_dump_states(bp);
11638 	if (netif_running(bp->dev)) {
11639 		int rc;
11640 
11641 		if (silent) {
11642 			bnxt_close_nic(bp, false, false);
11643 			bnxt_open_nic(bp, false, false);
11644 		} else {
11645 			bnxt_ulp_stop(bp);
11646 			bnxt_close_nic(bp, true, false);
11647 			rc = bnxt_open_nic(bp, true, false);
11648 			bnxt_ulp_start(bp, rc);
11649 		}
11650 	}
11651 }
11652 
11653 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
11654 {
11655 	struct bnxt *bp = netdev_priv(dev);
11656 
11657 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
11658 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
11659 }
11660 
11661 static void bnxt_fw_health_check(struct bnxt *bp)
11662 {
11663 	struct bnxt_fw_health *fw_health = bp->fw_health;
11664 	struct pci_dev *pdev = bp->pdev;
11665 	u32 val;
11666 
11667 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11668 		return;
11669 
11670 	/* Make sure it is enabled before checking the tmr_counter. */
11671 	smp_rmb();
11672 	if (fw_health->tmr_counter) {
11673 		fw_health->tmr_counter--;
11674 		return;
11675 	}
11676 
11677 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
11678 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
11679 		fw_health->arrests++;
11680 		goto fw_reset;
11681 	}
11682 
11683 	fw_health->last_fw_heartbeat = val;
11684 
11685 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
11686 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
11687 		fw_health->discoveries++;
11688 		goto fw_reset;
11689 	}
11690 
11691 	fw_health->tmr_counter = fw_health->tmr_multiplier;
11692 	return;
11693 
11694 fw_reset:
11695 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
11696 }
11697 
11698 static void bnxt_timer(struct timer_list *t)
11699 {
11700 	struct bnxt *bp = from_timer(bp, t, timer);
11701 	struct net_device *dev = bp->dev;
11702 
11703 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
11704 		return;
11705 
11706 	if (atomic_read(&bp->intr_sem) != 0)
11707 		goto bnxt_restart_timer;
11708 
11709 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
11710 		bnxt_fw_health_check(bp);
11711 
11712 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
11713 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
11714 
11715 	if (bnxt_tc_flower_enabled(bp))
11716 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
11717 
11718 #ifdef CONFIG_RFS_ACCEL
11719 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
11720 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
11721 #endif /*CONFIG_RFS_ACCEL*/
11722 
11723 	if (bp->link_info.phy_retry) {
11724 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
11725 			bp->link_info.phy_retry = false;
11726 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
11727 		} else {
11728 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
11729 		}
11730 	}
11731 
11732 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
11733 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
11734 
11735 	if ((bp->flags & BNXT_FLAG_CHIP_P5) && !bp->chip_rev &&
11736 	    netif_carrier_ok(dev))
11737 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
11738 
11739 bnxt_restart_timer:
11740 	mod_timer(&bp->timer, jiffies + bp->current_interval);
11741 }
11742 
11743 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
11744 {
11745 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
11746 	 * set.  If the device is being closed, bnxt_close() may be holding
11747 	 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear.  So we
11748 	 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
11749 	 */
11750 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11751 	rtnl_lock();
11752 }
11753 
11754 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
11755 {
11756 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11757 	rtnl_unlock();
11758 }
11759 
11760 /* Only called from bnxt_sp_task() */
11761 static void bnxt_reset(struct bnxt *bp, bool silent)
11762 {
11763 	bnxt_rtnl_lock_sp(bp);
11764 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
11765 		bnxt_reset_task(bp, silent);
11766 	bnxt_rtnl_unlock_sp(bp);
11767 }
11768 
11769 /* Only called from bnxt_sp_task() */
11770 static void bnxt_rx_ring_reset(struct bnxt *bp)
11771 {
11772 	int i;
11773 
11774 	bnxt_rtnl_lock_sp(bp);
11775 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
11776 		bnxt_rtnl_unlock_sp(bp);
11777 		return;
11778 	}
11779 	/* Disable and flush TPA before resetting the RX ring */
11780 	if (bp->flags & BNXT_FLAG_TPA)
11781 		bnxt_set_tpa(bp, false);
11782 	for (i = 0; i < bp->rx_nr_rings; i++) {
11783 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
11784 		struct bnxt_cp_ring_info *cpr;
11785 		int rc;
11786 
11787 		if (!rxr->bnapi->in_reset)
11788 			continue;
11789 
11790 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
11791 		if (rc) {
11792 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
11793 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
11794 			else
11795 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
11796 					    rc);
11797 			bnxt_reset_task(bp, true);
11798 			break;
11799 		}
11800 		bnxt_free_one_rx_ring_skbs(bp, i);
11801 		rxr->rx_prod = 0;
11802 		rxr->rx_agg_prod = 0;
11803 		rxr->rx_sw_agg_prod = 0;
11804 		rxr->rx_next_cons = 0;
11805 		rxr->bnapi->in_reset = false;
11806 		bnxt_alloc_one_rx_ring(bp, i);
11807 		cpr = &rxr->bnapi->cp_ring;
11808 		cpr->sw_stats.rx.rx_resets++;
11809 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
11810 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
11811 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
11812 	}
11813 	if (bp->flags & BNXT_FLAG_TPA)
11814 		bnxt_set_tpa(bp, true);
11815 	bnxt_rtnl_unlock_sp(bp);
11816 }
11817 
11818 static void bnxt_fw_reset_close(struct bnxt *bp)
11819 {
11820 	bnxt_ulp_stop(bp);
11821 	/* When firmware is in fatal state, quiesce device and disable
11822 	 * bus master to prevent any potential bad DMAs before freeing
11823 	 * kernel memory.
11824 	 */
11825 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
11826 		u16 val = 0;
11827 
11828 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
11829 		if (val == 0xffff)
11830 			bp->fw_reset_min_dsecs = 0;
11831 		bnxt_tx_disable(bp);
11832 		bnxt_disable_napi(bp);
11833 		bnxt_disable_int_sync(bp);
11834 		bnxt_free_irq(bp);
11835 		bnxt_clear_int_mode(bp);
11836 		pci_disable_device(bp->pdev);
11837 	}
11838 	__bnxt_close_nic(bp, true, false);
11839 	bnxt_vf_reps_free(bp);
11840 	bnxt_clear_int_mode(bp);
11841 	bnxt_hwrm_func_drv_unrgtr(bp);
11842 	if (pci_is_enabled(bp->pdev))
11843 		pci_disable_device(bp->pdev);
11844 	bnxt_free_ctx_mem(bp);
11845 	kfree(bp->ctx);
11846 	bp->ctx = NULL;
11847 }
11848 
11849 static bool is_bnxt_fw_ok(struct bnxt *bp)
11850 {
11851 	struct bnxt_fw_health *fw_health = bp->fw_health;
11852 	bool no_heartbeat = false, has_reset = false;
11853 	u32 val;
11854 
11855 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
11856 	if (val == fw_health->last_fw_heartbeat)
11857 		no_heartbeat = true;
11858 
11859 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
11860 	if (val != fw_health->last_fw_reset_cnt)
11861 		has_reset = true;
11862 
11863 	if (!no_heartbeat && has_reset)
11864 		return true;
11865 
11866 	return false;
11867 }
11868 
11869 /* rtnl_lock is acquired before calling this function */
11870 static void bnxt_force_fw_reset(struct bnxt *bp)
11871 {
11872 	struct bnxt_fw_health *fw_health = bp->fw_health;
11873 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
11874 	u32 wait_dsecs;
11875 
11876 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
11877 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11878 		return;
11879 
11880 	if (ptp) {
11881 		spin_lock_bh(&ptp->ptp_lock);
11882 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11883 		spin_unlock_bh(&ptp->ptp_lock);
11884 	} else {
11885 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11886 	}
11887 	bnxt_fw_reset_close(bp);
11888 	wait_dsecs = fw_health->master_func_wait_dsecs;
11889 	if (fw_health->primary) {
11890 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
11891 			wait_dsecs = 0;
11892 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
11893 	} else {
11894 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
11895 		wait_dsecs = fw_health->normal_func_wait_dsecs;
11896 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11897 	}
11898 
11899 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
11900 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
11901 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
11902 }
11903 
11904 void bnxt_fw_exception(struct bnxt *bp)
11905 {
11906 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
11907 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
11908 	bnxt_rtnl_lock_sp(bp);
11909 	bnxt_force_fw_reset(bp);
11910 	bnxt_rtnl_unlock_sp(bp);
11911 }
11912 
11913 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
11914  * < 0 on error.
11915  */
11916 static int bnxt_get_registered_vfs(struct bnxt *bp)
11917 {
11918 #ifdef CONFIG_BNXT_SRIOV
11919 	int rc;
11920 
11921 	if (!BNXT_PF(bp))
11922 		return 0;
11923 
11924 	rc = bnxt_hwrm_func_qcfg(bp);
11925 	if (rc) {
11926 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
11927 		return rc;
11928 	}
11929 	if (bp->pf.registered_vfs)
11930 		return bp->pf.registered_vfs;
11931 	if (bp->sriov_cfg)
11932 		return 1;
11933 #endif
11934 	return 0;
11935 }
11936 
11937 void bnxt_fw_reset(struct bnxt *bp)
11938 {
11939 	bnxt_rtnl_lock_sp(bp);
11940 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
11941 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
11942 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
11943 		int n = 0, tmo;
11944 
11945 		if (ptp) {
11946 			spin_lock_bh(&ptp->ptp_lock);
11947 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11948 			spin_unlock_bh(&ptp->ptp_lock);
11949 		} else {
11950 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11951 		}
11952 		if (bp->pf.active_vfs &&
11953 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
11954 			n = bnxt_get_registered_vfs(bp);
11955 		if (n < 0) {
11956 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
11957 				   n);
11958 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11959 			dev_close(bp->dev);
11960 			goto fw_reset_exit;
11961 		} else if (n > 0) {
11962 			u16 vf_tmo_dsecs = n * 10;
11963 
11964 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
11965 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
11966 			bp->fw_reset_state =
11967 				BNXT_FW_RESET_STATE_POLL_VF;
11968 			bnxt_queue_fw_reset_work(bp, HZ / 10);
11969 			goto fw_reset_exit;
11970 		}
11971 		bnxt_fw_reset_close(bp);
11972 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
11973 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
11974 			tmo = HZ / 10;
11975 		} else {
11976 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11977 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
11978 		}
11979 		bnxt_queue_fw_reset_work(bp, tmo);
11980 	}
11981 fw_reset_exit:
11982 	bnxt_rtnl_unlock_sp(bp);
11983 }
11984 
11985 static void bnxt_chk_missed_irq(struct bnxt *bp)
11986 {
11987 	int i;
11988 
11989 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
11990 		return;
11991 
11992 	for (i = 0; i < bp->cp_nr_rings; i++) {
11993 		struct bnxt_napi *bnapi = bp->bnapi[i];
11994 		struct bnxt_cp_ring_info *cpr;
11995 		u32 fw_ring_id;
11996 		int j;
11997 
11998 		if (!bnapi)
11999 			continue;
12000 
12001 		cpr = &bnapi->cp_ring;
12002 		for (j = 0; j < 2; j++) {
12003 			struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
12004 			u32 val[2];
12005 
12006 			if (!cpr2 || cpr2->has_more_work ||
12007 			    !bnxt_has_work(bp, cpr2))
12008 				continue;
12009 
12010 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
12011 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
12012 				continue;
12013 			}
12014 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
12015 			bnxt_dbg_hwrm_ring_info_get(bp,
12016 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
12017 				fw_ring_id, &val[0], &val[1]);
12018 			cpr->sw_stats.cmn.missed_irqs++;
12019 		}
12020 	}
12021 }
12022 
12023 static void bnxt_cfg_ntp_filters(struct bnxt *);
12024 
12025 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
12026 {
12027 	struct bnxt_link_info *link_info = &bp->link_info;
12028 
12029 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
12030 		link_info->autoneg = BNXT_AUTONEG_SPEED;
12031 		if (bp->hwrm_spec_code >= 0x10201) {
12032 			if (link_info->auto_pause_setting &
12033 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
12034 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
12035 		} else {
12036 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
12037 		}
12038 		bnxt_set_auto_speed(link_info);
12039 	} else {
12040 		bnxt_set_force_speed(link_info);
12041 		link_info->req_duplex = link_info->duplex_setting;
12042 	}
12043 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
12044 		link_info->req_flow_ctrl =
12045 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
12046 	else
12047 		link_info->req_flow_ctrl = link_info->force_pause_setting;
12048 }
12049 
12050 static void bnxt_fw_echo_reply(struct bnxt *bp)
12051 {
12052 	struct bnxt_fw_health *fw_health = bp->fw_health;
12053 	struct hwrm_func_echo_response_input *req;
12054 	int rc;
12055 
12056 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
12057 	if (rc)
12058 		return;
12059 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
12060 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
12061 	hwrm_req_send(bp, req);
12062 }
12063 
12064 static void bnxt_sp_task(struct work_struct *work)
12065 {
12066 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
12067 
12068 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
12069 	smp_mb__after_atomic();
12070 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12071 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
12072 		return;
12073 	}
12074 
12075 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
12076 		bnxt_cfg_rx_mode(bp);
12077 
12078 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
12079 		bnxt_cfg_ntp_filters(bp);
12080 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
12081 		bnxt_hwrm_exec_fwd_req(bp);
12082 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
12083 		bnxt_hwrm_port_qstats(bp, 0);
12084 		bnxt_hwrm_port_qstats_ext(bp, 0);
12085 		bnxt_accumulate_all_stats(bp);
12086 	}
12087 
12088 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
12089 		int rc;
12090 
12091 		mutex_lock(&bp->link_lock);
12092 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
12093 				       &bp->sp_event))
12094 			bnxt_hwrm_phy_qcaps(bp);
12095 
12096 		rc = bnxt_update_link(bp, true);
12097 		if (rc)
12098 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
12099 				   rc);
12100 
12101 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
12102 				       &bp->sp_event))
12103 			bnxt_init_ethtool_link_settings(bp);
12104 		mutex_unlock(&bp->link_lock);
12105 	}
12106 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
12107 		int rc;
12108 
12109 		mutex_lock(&bp->link_lock);
12110 		rc = bnxt_update_phy_setting(bp);
12111 		mutex_unlock(&bp->link_lock);
12112 		if (rc) {
12113 			netdev_warn(bp->dev, "update phy settings retry failed\n");
12114 		} else {
12115 			bp->link_info.phy_retry = false;
12116 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
12117 		}
12118 	}
12119 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
12120 		mutex_lock(&bp->link_lock);
12121 		bnxt_get_port_module_status(bp);
12122 		mutex_unlock(&bp->link_lock);
12123 	}
12124 
12125 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
12126 		bnxt_tc_flow_stats_work(bp);
12127 
12128 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
12129 		bnxt_chk_missed_irq(bp);
12130 
12131 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
12132 		bnxt_fw_echo_reply(bp);
12133 
12134 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
12135 		bnxt_hwmon_notify_event(bp);
12136 
12137 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
12138 	 * must be the last functions to be called before exiting.
12139 	 */
12140 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
12141 		bnxt_reset(bp, false);
12142 
12143 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
12144 		bnxt_reset(bp, true);
12145 
12146 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
12147 		bnxt_rx_ring_reset(bp);
12148 
12149 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
12150 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
12151 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
12152 			bnxt_devlink_health_fw_report(bp);
12153 		else
12154 			bnxt_fw_reset(bp);
12155 	}
12156 
12157 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
12158 		if (!is_bnxt_fw_ok(bp))
12159 			bnxt_devlink_health_fw_report(bp);
12160 	}
12161 
12162 	smp_mb__before_atomic();
12163 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
12164 }
12165 
12166 /* Under rtnl_lock */
12167 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
12168 		     int tx_xdp)
12169 {
12170 	int max_rx, max_tx, tx_sets = 1;
12171 	int tx_rings_needed, stats;
12172 	int rx_rings = rx;
12173 	int cp, vnics, rc;
12174 
12175 	if (tcs)
12176 		tx_sets = tcs;
12177 
12178 	rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
12179 	if (rc)
12180 		return rc;
12181 
12182 	if (max_rx < rx)
12183 		return -ENOMEM;
12184 
12185 	tx_rings_needed = tx * tx_sets + tx_xdp;
12186 	if (max_tx < tx_rings_needed)
12187 		return -ENOMEM;
12188 
12189 	vnics = 1;
12190 	if ((bp->flags & (BNXT_FLAG_RFS | BNXT_FLAG_CHIP_P5)) == BNXT_FLAG_RFS)
12191 		vnics += rx_rings;
12192 
12193 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
12194 		rx_rings <<= 1;
12195 	cp = sh ? max_t(int, tx_rings_needed, rx) : tx_rings_needed + rx;
12196 	stats = cp;
12197 	if (BNXT_NEW_RM(bp)) {
12198 		cp += bnxt_get_ulp_msix_num(bp);
12199 		stats += bnxt_get_ulp_stat_ctxs(bp);
12200 	}
12201 	return bnxt_hwrm_check_rings(bp, tx_rings_needed, rx_rings, rx, cp,
12202 				     stats, vnics);
12203 }
12204 
12205 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
12206 {
12207 	if (bp->bar2) {
12208 		pci_iounmap(pdev, bp->bar2);
12209 		bp->bar2 = NULL;
12210 	}
12211 
12212 	if (bp->bar1) {
12213 		pci_iounmap(pdev, bp->bar1);
12214 		bp->bar1 = NULL;
12215 	}
12216 
12217 	if (bp->bar0) {
12218 		pci_iounmap(pdev, bp->bar0);
12219 		bp->bar0 = NULL;
12220 	}
12221 }
12222 
12223 static void bnxt_cleanup_pci(struct bnxt *bp)
12224 {
12225 	bnxt_unmap_bars(bp, bp->pdev);
12226 	pci_release_regions(bp->pdev);
12227 	if (pci_is_enabled(bp->pdev))
12228 		pci_disable_device(bp->pdev);
12229 }
12230 
12231 static void bnxt_init_dflt_coal(struct bnxt *bp)
12232 {
12233 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
12234 	struct bnxt_coal *coal;
12235 	u16 flags = 0;
12236 
12237 	if (coal_cap->cmpl_params &
12238 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
12239 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
12240 
12241 	/* Tick values in micro seconds.
12242 	 * 1 coal_buf x bufs_per_record = 1 completion record.
12243 	 */
12244 	coal = &bp->rx_coal;
12245 	coal->coal_ticks = 10;
12246 	coal->coal_bufs = 30;
12247 	coal->coal_ticks_irq = 1;
12248 	coal->coal_bufs_irq = 2;
12249 	coal->idle_thresh = 50;
12250 	coal->bufs_per_record = 2;
12251 	coal->budget = 64;		/* NAPI budget */
12252 	coal->flags = flags;
12253 
12254 	coal = &bp->tx_coal;
12255 	coal->coal_ticks = 28;
12256 	coal->coal_bufs = 30;
12257 	coal->coal_ticks_irq = 2;
12258 	coal->coal_bufs_irq = 2;
12259 	coal->bufs_per_record = 1;
12260 	coal->flags = flags;
12261 
12262 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
12263 }
12264 
12265 /* FW that pre-reserves 1 VNIC per function */
12266 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
12267 {
12268 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
12269 
12270 	if (!(bp->flags & BNXT_FLAG_CHIP_P5) &&
12271 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
12272 		return true;
12273 	if ((bp->flags & BNXT_FLAG_CHIP_P5) &&
12274 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
12275 		return true;
12276 	return false;
12277 }
12278 
12279 static int bnxt_fw_init_one_p1(struct bnxt *bp)
12280 {
12281 	int rc;
12282 
12283 	bp->fw_cap = 0;
12284 	rc = bnxt_hwrm_ver_get(bp);
12285 	bnxt_try_map_fw_health_reg(bp);
12286 	if (rc) {
12287 		rc = bnxt_try_recover_fw(bp);
12288 		if (rc)
12289 			return rc;
12290 		rc = bnxt_hwrm_ver_get(bp);
12291 		if (rc)
12292 			return rc;
12293 	}
12294 
12295 	bnxt_nvm_cfg_ver_get(bp);
12296 
12297 	rc = bnxt_hwrm_func_reset(bp);
12298 	if (rc)
12299 		return -ENODEV;
12300 
12301 	bnxt_hwrm_fw_set_time(bp);
12302 	return 0;
12303 }
12304 
12305 static int bnxt_fw_init_one_p2(struct bnxt *bp)
12306 {
12307 	int rc;
12308 
12309 	/* Get the MAX capabilities for this function */
12310 	rc = bnxt_hwrm_func_qcaps(bp);
12311 	if (rc) {
12312 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
12313 			   rc);
12314 		return -ENODEV;
12315 	}
12316 
12317 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
12318 	if (rc)
12319 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
12320 			    rc);
12321 
12322 	if (bnxt_alloc_fw_health(bp)) {
12323 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
12324 	} else {
12325 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
12326 		if (rc)
12327 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
12328 				    rc);
12329 	}
12330 
12331 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
12332 	if (rc)
12333 		return -ENODEV;
12334 
12335 	if (bnxt_fw_pre_resv_vnics(bp))
12336 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
12337 
12338 	bnxt_hwrm_func_qcfg(bp);
12339 	bnxt_hwrm_vnic_qcaps(bp);
12340 	bnxt_hwrm_port_led_qcaps(bp);
12341 	bnxt_ethtool_init(bp);
12342 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
12343 		__bnxt_hwrm_ptp_qcfg(bp);
12344 	bnxt_dcb_init(bp);
12345 	bnxt_hwmon_init(bp);
12346 	return 0;
12347 }
12348 
12349 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
12350 {
12351 	bp->flags &= ~BNXT_FLAG_UDP_RSS_CAP;
12352 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
12353 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
12354 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
12355 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
12356 	if (bp->fw_cap & BNXT_FW_CAP_RSS_HASH_TYPE_DELTA)
12357 		bp->rss_hash_delta = bp->rss_hash_cfg;
12358 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
12359 		bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
12360 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
12361 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
12362 	}
12363 }
12364 
12365 static void bnxt_set_dflt_rfs(struct bnxt *bp)
12366 {
12367 	struct net_device *dev = bp->dev;
12368 
12369 	dev->hw_features &= ~NETIF_F_NTUPLE;
12370 	dev->features &= ~NETIF_F_NTUPLE;
12371 	bp->flags &= ~BNXT_FLAG_RFS;
12372 	if (bnxt_rfs_supported(bp)) {
12373 		dev->hw_features |= NETIF_F_NTUPLE;
12374 		if (bnxt_rfs_capable(bp)) {
12375 			bp->flags |= BNXT_FLAG_RFS;
12376 			dev->features |= NETIF_F_NTUPLE;
12377 		}
12378 	}
12379 }
12380 
12381 static void bnxt_fw_init_one_p3(struct bnxt *bp)
12382 {
12383 	struct pci_dev *pdev = bp->pdev;
12384 
12385 	bnxt_set_dflt_rss_hash_type(bp);
12386 	bnxt_set_dflt_rfs(bp);
12387 
12388 	bnxt_get_wol_settings(bp);
12389 	if (bp->flags & BNXT_FLAG_WOL_CAP)
12390 		device_set_wakeup_enable(&pdev->dev, bp->wol);
12391 	else
12392 		device_set_wakeup_capable(&pdev->dev, false);
12393 
12394 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
12395 	bnxt_hwrm_coal_params_qcaps(bp);
12396 }
12397 
12398 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
12399 
12400 int bnxt_fw_init_one(struct bnxt *bp)
12401 {
12402 	int rc;
12403 
12404 	rc = bnxt_fw_init_one_p1(bp);
12405 	if (rc) {
12406 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
12407 		return rc;
12408 	}
12409 	rc = bnxt_fw_init_one_p2(bp);
12410 	if (rc) {
12411 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
12412 		return rc;
12413 	}
12414 	rc = bnxt_probe_phy(bp, false);
12415 	if (rc)
12416 		return rc;
12417 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
12418 	if (rc)
12419 		return rc;
12420 
12421 	bnxt_fw_init_one_p3(bp);
12422 	return 0;
12423 }
12424 
12425 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
12426 {
12427 	struct bnxt_fw_health *fw_health = bp->fw_health;
12428 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
12429 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
12430 	u32 reg_type, reg_off, delay_msecs;
12431 
12432 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
12433 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
12434 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
12435 	switch (reg_type) {
12436 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
12437 		pci_write_config_dword(bp->pdev, reg_off, val);
12438 		break;
12439 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
12440 		writel(reg_off & BNXT_GRC_BASE_MASK,
12441 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
12442 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
12443 		fallthrough;
12444 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
12445 		writel(val, bp->bar0 + reg_off);
12446 		break;
12447 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
12448 		writel(val, bp->bar1 + reg_off);
12449 		break;
12450 	}
12451 	if (delay_msecs) {
12452 		pci_read_config_dword(bp->pdev, 0, &val);
12453 		msleep(delay_msecs);
12454 	}
12455 }
12456 
12457 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
12458 {
12459 	struct hwrm_func_qcfg_output *resp;
12460 	struct hwrm_func_qcfg_input *req;
12461 	bool result = true; /* firmware will enforce if unknown */
12462 
12463 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
12464 		return result;
12465 
12466 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
12467 		return result;
12468 
12469 	req->fid = cpu_to_le16(0xffff);
12470 	resp = hwrm_req_hold(bp, req);
12471 	if (!hwrm_req_send(bp, req))
12472 		result = !!(le16_to_cpu(resp->flags) &
12473 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
12474 	hwrm_req_drop(bp, req);
12475 	return result;
12476 }
12477 
12478 static void bnxt_reset_all(struct bnxt *bp)
12479 {
12480 	struct bnxt_fw_health *fw_health = bp->fw_health;
12481 	int i, rc;
12482 
12483 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
12484 		bnxt_fw_reset_via_optee(bp);
12485 		bp->fw_reset_timestamp = jiffies;
12486 		return;
12487 	}
12488 
12489 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
12490 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
12491 			bnxt_fw_reset_writel(bp, i);
12492 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
12493 		struct hwrm_fw_reset_input *req;
12494 
12495 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
12496 		if (!rc) {
12497 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
12498 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
12499 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
12500 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
12501 			rc = hwrm_req_send(bp, req);
12502 		}
12503 		if (rc != -ENODEV)
12504 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
12505 	}
12506 	bp->fw_reset_timestamp = jiffies;
12507 }
12508 
12509 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
12510 {
12511 	return time_after(jiffies, bp->fw_reset_timestamp +
12512 			  (bp->fw_reset_max_dsecs * HZ / 10));
12513 }
12514 
12515 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
12516 {
12517 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
12518 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF) {
12519 		bnxt_ulp_start(bp, rc);
12520 		bnxt_dl_health_fw_status_update(bp, false);
12521 	}
12522 	bp->fw_reset_state = 0;
12523 	dev_close(bp->dev);
12524 }
12525 
12526 static void bnxt_fw_reset_task(struct work_struct *work)
12527 {
12528 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
12529 	int rc = 0;
12530 
12531 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12532 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
12533 		return;
12534 	}
12535 
12536 	switch (bp->fw_reset_state) {
12537 	case BNXT_FW_RESET_STATE_POLL_VF: {
12538 		int n = bnxt_get_registered_vfs(bp);
12539 		int tmo;
12540 
12541 		if (n < 0) {
12542 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
12543 				   n, jiffies_to_msecs(jiffies -
12544 				   bp->fw_reset_timestamp));
12545 			goto fw_reset_abort;
12546 		} else if (n > 0) {
12547 			if (bnxt_fw_reset_timeout(bp)) {
12548 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
12549 				bp->fw_reset_state = 0;
12550 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
12551 					   n);
12552 				return;
12553 			}
12554 			bnxt_queue_fw_reset_work(bp, HZ / 10);
12555 			return;
12556 		}
12557 		bp->fw_reset_timestamp = jiffies;
12558 		rtnl_lock();
12559 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12560 			bnxt_fw_reset_abort(bp, rc);
12561 			rtnl_unlock();
12562 			return;
12563 		}
12564 		bnxt_fw_reset_close(bp);
12565 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
12566 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
12567 			tmo = HZ / 10;
12568 		} else {
12569 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
12570 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
12571 		}
12572 		rtnl_unlock();
12573 		bnxt_queue_fw_reset_work(bp, tmo);
12574 		return;
12575 	}
12576 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
12577 		u32 val;
12578 
12579 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12580 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
12581 		    !bnxt_fw_reset_timeout(bp)) {
12582 			bnxt_queue_fw_reset_work(bp, HZ / 5);
12583 			return;
12584 		}
12585 
12586 		if (!bp->fw_health->primary) {
12587 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
12588 
12589 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
12590 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
12591 			return;
12592 		}
12593 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
12594 	}
12595 		fallthrough;
12596 	case BNXT_FW_RESET_STATE_RESET_FW:
12597 		bnxt_reset_all(bp);
12598 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
12599 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
12600 		return;
12601 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
12602 		bnxt_inv_fw_health_reg(bp);
12603 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
12604 		    !bp->fw_reset_min_dsecs) {
12605 			u16 val;
12606 
12607 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
12608 			if (val == 0xffff) {
12609 				if (bnxt_fw_reset_timeout(bp)) {
12610 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
12611 					rc = -ETIMEDOUT;
12612 					goto fw_reset_abort;
12613 				}
12614 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
12615 				return;
12616 			}
12617 		}
12618 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
12619 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
12620 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
12621 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
12622 			bnxt_dl_remote_reload(bp);
12623 		if (pci_enable_device(bp->pdev)) {
12624 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
12625 			rc = -ENODEV;
12626 			goto fw_reset_abort;
12627 		}
12628 		pci_set_master(bp->pdev);
12629 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
12630 		fallthrough;
12631 	case BNXT_FW_RESET_STATE_POLL_FW:
12632 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
12633 		rc = bnxt_hwrm_poll(bp);
12634 		if (rc) {
12635 			if (bnxt_fw_reset_timeout(bp)) {
12636 				netdev_err(bp->dev, "Firmware reset aborted\n");
12637 				goto fw_reset_abort_status;
12638 			}
12639 			bnxt_queue_fw_reset_work(bp, HZ / 5);
12640 			return;
12641 		}
12642 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
12643 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
12644 		fallthrough;
12645 	case BNXT_FW_RESET_STATE_OPENING:
12646 		while (!rtnl_trylock()) {
12647 			bnxt_queue_fw_reset_work(bp, HZ / 10);
12648 			return;
12649 		}
12650 		rc = bnxt_open(bp->dev);
12651 		if (rc) {
12652 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
12653 			bnxt_fw_reset_abort(bp, rc);
12654 			rtnl_unlock();
12655 			return;
12656 		}
12657 
12658 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
12659 		    bp->fw_health->enabled) {
12660 			bp->fw_health->last_fw_reset_cnt =
12661 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
12662 		}
12663 		bp->fw_reset_state = 0;
12664 		/* Make sure fw_reset_state is 0 before clearing the flag */
12665 		smp_mb__before_atomic();
12666 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
12667 		bnxt_ulp_start(bp, 0);
12668 		bnxt_reenable_sriov(bp);
12669 		bnxt_vf_reps_alloc(bp);
12670 		bnxt_vf_reps_open(bp);
12671 		bnxt_ptp_reapply_pps(bp);
12672 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
12673 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
12674 			bnxt_dl_health_fw_recovery_done(bp);
12675 			bnxt_dl_health_fw_status_update(bp, true);
12676 		}
12677 		rtnl_unlock();
12678 		break;
12679 	}
12680 	return;
12681 
12682 fw_reset_abort_status:
12683 	if (bp->fw_health->status_reliable ||
12684 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
12685 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12686 
12687 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
12688 	}
12689 fw_reset_abort:
12690 	rtnl_lock();
12691 	bnxt_fw_reset_abort(bp, rc);
12692 	rtnl_unlock();
12693 }
12694 
12695 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
12696 {
12697 	int rc;
12698 	struct bnxt *bp = netdev_priv(dev);
12699 
12700 	SET_NETDEV_DEV(dev, &pdev->dev);
12701 
12702 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
12703 	rc = pci_enable_device(pdev);
12704 	if (rc) {
12705 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
12706 		goto init_err;
12707 	}
12708 
12709 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
12710 		dev_err(&pdev->dev,
12711 			"Cannot find PCI device base address, aborting\n");
12712 		rc = -ENODEV;
12713 		goto init_err_disable;
12714 	}
12715 
12716 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
12717 	if (rc) {
12718 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
12719 		goto init_err_disable;
12720 	}
12721 
12722 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
12723 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
12724 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
12725 		rc = -EIO;
12726 		goto init_err_release;
12727 	}
12728 
12729 	pci_set_master(pdev);
12730 
12731 	bp->dev = dev;
12732 	bp->pdev = pdev;
12733 
12734 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
12735 	 * determines the BAR size.
12736 	 */
12737 	bp->bar0 = pci_ioremap_bar(pdev, 0);
12738 	if (!bp->bar0) {
12739 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
12740 		rc = -ENOMEM;
12741 		goto init_err_release;
12742 	}
12743 
12744 	bp->bar2 = pci_ioremap_bar(pdev, 4);
12745 	if (!bp->bar2) {
12746 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
12747 		rc = -ENOMEM;
12748 		goto init_err_release;
12749 	}
12750 
12751 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
12752 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
12753 
12754 	spin_lock_init(&bp->ntp_fltr_lock);
12755 #if BITS_PER_LONG == 32
12756 	spin_lock_init(&bp->db_lock);
12757 #endif
12758 
12759 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
12760 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
12761 
12762 	timer_setup(&bp->timer, bnxt_timer, 0);
12763 	bp->current_interval = BNXT_TIMER_INTERVAL;
12764 
12765 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
12766 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
12767 
12768 	clear_bit(BNXT_STATE_OPEN, &bp->state);
12769 	return 0;
12770 
12771 init_err_release:
12772 	bnxt_unmap_bars(bp, pdev);
12773 	pci_release_regions(pdev);
12774 
12775 init_err_disable:
12776 	pci_disable_device(pdev);
12777 
12778 init_err:
12779 	return rc;
12780 }
12781 
12782 /* rtnl_lock held */
12783 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
12784 {
12785 	struct sockaddr *addr = p;
12786 	struct bnxt *bp = netdev_priv(dev);
12787 	int rc = 0;
12788 
12789 	if (!is_valid_ether_addr(addr->sa_data))
12790 		return -EADDRNOTAVAIL;
12791 
12792 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
12793 		return 0;
12794 
12795 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
12796 	if (rc)
12797 		return rc;
12798 
12799 	eth_hw_addr_set(dev, addr->sa_data);
12800 	if (netif_running(dev)) {
12801 		bnxt_close_nic(bp, false, false);
12802 		rc = bnxt_open_nic(bp, false, false);
12803 	}
12804 
12805 	return rc;
12806 }
12807 
12808 /* rtnl_lock held */
12809 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
12810 {
12811 	struct bnxt *bp = netdev_priv(dev);
12812 
12813 	if (netif_running(dev))
12814 		bnxt_close_nic(bp, true, false);
12815 
12816 	dev->mtu = new_mtu;
12817 	bnxt_set_ring_params(bp);
12818 
12819 	if (netif_running(dev))
12820 		return bnxt_open_nic(bp, true, false);
12821 
12822 	return 0;
12823 }
12824 
12825 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
12826 {
12827 	struct bnxt *bp = netdev_priv(dev);
12828 	bool sh = false;
12829 	int rc;
12830 
12831 	if (tc > bp->max_tc) {
12832 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
12833 			   tc, bp->max_tc);
12834 		return -EINVAL;
12835 	}
12836 
12837 	if (netdev_get_num_tc(dev) == tc)
12838 		return 0;
12839 
12840 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
12841 		sh = true;
12842 
12843 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
12844 			      sh, tc, bp->tx_nr_rings_xdp);
12845 	if (rc)
12846 		return rc;
12847 
12848 	/* Needs to close the device and do hw resource re-allocations */
12849 	if (netif_running(bp->dev))
12850 		bnxt_close_nic(bp, true, false);
12851 
12852 	if (tc) {
12853 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
12854 		netdev_set_num_tc(dev, tc);
12855 	} else {
12856 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
12857 		netdev_reset_tc(dev);
12858 	}
12859 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
12860 	bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
12861 			       bp->tx_nr_rings + bp->rx_nr_rings;
12862 
12863 	if (netif_running(bp->dev))
12864 		return bnxt_open_nic(bp, true, false);
12865 
12866 	return 0;
12867 }
12868 
12869 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
12870 				  void *cb_priv)
12871 {
12872 	struct bnxt *bp = cb_priv;
12873 
12874 	if (!bnxt_tc_flower_enabled(bp) ||
12875 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
12876 		return -EOPNOTSUPP;
12877 
12878 	switch (type) {
12879 	case TC_SETUP_CLSFLOWER:
12880 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
12881 	default:
12882 		return -EOPNOTSUPP;
12883 	}
12884 }
12885 
12886 LIST_HEAD(bnxt_block_cb_list);
12887 
12888 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
12889 			 void *type_data)
12890 {
12891 	struct bnxt *bp = netdev_priv(dev);
12892 
12893 	switch (type) {
12894 	case TC_SETUP_BLOCK:
12895 		return flow_block_cb_setup_simple(type_data,
12896 						  &bnxt_block_cb_list,
12897 						  bnxt_setup_tc_block_cb,
12898 						  bp, bp, true);
12899 	case TC_SETUP_QDISC_MQPRIO: {
12900 		struct tc_mqprio_qopt *mqprio = type_data;
12901 
12902 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
12903 
12904 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
12905 	}
12906 	default:
12907 		return -EOPNOTSUPP;
12908 	}
12909 }
12910 
12911 #ifdef CONFIG_RFS_ACCEL
12912 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
12913 			    struct bnxt_ntuple_filter *f2)
12914 {
12915 	struct flow_keys *keys1 = &f1->fkeys;
12916 	struct flow_keys *keys2 = &f2->fkeys;
12917 
12918 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
12919 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
12920 		return false;
12921 
12922 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
12923 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
12924 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst)
12925 			return false;
12926 	} else {
12927 		if (memcmp(&keys1->addrs.v6addrs.src, &keys2->addrs.v6addrs.src,
12928 			   sizeof(keys1->addrs.v6addrs.src)) ||
12929 		    memcmp(&keys1->addrs.v6addrs.dst, &keys2->addrs.v6addrs.dst,
12930 			   sizeof(keys1->addrs.v6addrs.dst)))
12931 			return false;
12932 	}
12933 
12934 	if (keys1->ports.ports == keys2->ports.ports &&
12935 	    keys1->control.flags == keys2->control.flags &&
12936 	    ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
12937 	    ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
12938 		return true;
12939 
12940 	return false;
12941 }
12942 
12943 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
12944 			      u16 rxq_index, u32 flow_id)
12945 {
12946 	struct bnxt *bp = netdev_priv(dev);
12947 	struct bnxt_ntuple_filter *fltr, *new_fltr;
12948 	struct flow_keys *fkeys;
12949 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
12950 	int rc = 0, idx, bit_id, l2_idx = 0;
12951 	struct hlist_head *head;
12952 	u32 flags;
12953 
12954 	if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
12955 		struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
12956 		int off = 0, j;
12957 
12958 		netif_addr_lock_bh(dev);
12959 		for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
12960 			if (ether_addr_equal(eth->h_dest,
12961 					     vnic->uc_list + off)) {
12962 				l2_idx = j + 1;
12963 				break;
12964 			}
12965 		}
12966 		netif_addr_unlock_bh(dev);
12967 		if (!l2_idx)
12968 			return -EINVAL;
12969 	}
12970 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
12971 	if (!new_fltr)
12972 		return -ENOMEM;
12973 
12974 	fkeys = &new_fltr->fkeys;
12975 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
12976 		rc = -EPROTONOSUPPORT;
12977 		goto err_free;
12978 	}
12979 
12980 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
12981 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
12982 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
12983 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
12984 		rc = -EPROTONOSUPPORT;
12985 		goto err_free;
12986 	}
12987 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
12988 	    bp->hwrm_spec_code < 0x10601) {
12989 		rc = -EPROTONOSUPPORT;
12990 		goto err_free;
12991 	}
12992 	flags = fkeys->control.flags;
12993 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
12994 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
12995 		rc = -EPROTONOSUPPORT;
12996 		goto err_free;
12997 	}
12998 
12999 	memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
13000 	memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
13001 
13002 	idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
13003 	head = &bp->ntp_fltr_hash_tbl[idx];
13004 	rcu_read_lock();
13005 	hlist_for_each_entry_rcu(fltr, head, hash) {
13006 		if (bnxt_fltr_match(fltr, new_fltr)) {
13007 			rc = fltr->sw_id;
13008 			rcu_read_unlock();
13009 			goto err_free;
13010 		}
13011 	}
13012 	rcu_read_unlock();
13013 
13014 	spin_lock_bh(&bp->ntp_fltr_lock);
13015 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
13016 					 BNXT_NTP_FLTR_MAX_FLTR, 0);
13017 	if (bit_id < 0) {
13018 		spin_unlock_bh(&bp->ntp_fltr_lock);
13019 		rc = -ENOMEM;
13020 		goto err_free;
13021 	}
13022 
13023 	new_fltr->sw_id = (u16)bit_id;
13024 	new_fltr->flow_id = flow_id;
13025 	new_fltr->l2_fltr_idx = l2_idx;
13026 	new_fltr->rxq = rxq_index;
13027 	hlist_add_head_rcu(&new_fltr->hash, head);
13028 	bp->ntp_fltr_count++;
13029 	spin_unlock_bh(&bp->ntp_fltr_lock);
13030 
13031 	bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
13032 
13033 	return new_fltr->sw_id;
13034 
13035 err_free:
13036 	kfree(new_fltr);
13037 	return rc;
13038 }
13039 
13040 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
13041 {
13042 	int i;
13043 
13044 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
13045 		struct hlist_head *head;
13046 		struct hlist_node *tmp;
13047 		struct bnxt_ntuple_filter *fltr;
13048 		int rc;
13049 
13050 		head = &bp->ntp_fltr_hash_tbl[i];
13051 		hlist_for_each_entry_safe(fltr, tmp, head, hash) {
13052 			bool del = false;
13053 
13054 			if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
13055 				if (rps_may_expire_flow(bp->dev, fltr->rxq,
13056 							fltr->flow_id,
13057 							fltr->sw_id)) {
13058 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
13059 									 fltr);
13060 					del = true;
13061 				}
13062 			} else {
13063 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
13064 								       fltr);
13065 				if (rc)
13066 					del = true;
13067 				else
13068 					set_bit(BNXT_FLTR_VALID, &fltr->state);
13069 			}
13070 
13071 			if (del) {
13072 				spin_lock_bh(&bp->ntp_fltr_lock);
13073 				hlist_del_rcu(&fltr->hash);
13074 				bp->ntp_fltr_count--;
13075 				spin_unlock_bh(&bp->ntp_fltr_lock);
13076 				synchronize_rcu();
13077 				clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
13078 				kfree(fltr);
13079 			}
13080 		}
13081 	}
13082 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
13083 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
13084 }
13085 
13086 #else
13087 
13088 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
13089 {
13090 }
13091 
13092 #endif /* CONFIG_RFS_ACCEL */
13093 
13094 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
13095 				    unsigned int entry, struct udp_tunnel_info *ti)
13096 {
13097 	struct bnxt *bp = netdev_priv(netdev);
13098 	unsigned int cmd;
13099 
13100 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
13101 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
13102 	else
13103 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
13104 
13105 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
13106 }
13107 
13108 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
13109 				      unsigned int entry, struct udp_tunnel_info *ti)
13110 {
13111 	struct bnxt *bp = netdev_priv(netdev);
13112 	unsigned int cmd;
13113 
13114 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
13115 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
13116 	else
13117 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
13118 
13119 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
13120 }
13121 
13122 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
13123 	.set_port	= bnxt_udp_tunnel_set_port,
13124 	.unset_port	= bnxt_udp_tunnel_unset_port,
13125 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
13126 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
13127 	.tables		= {
13128 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
13129 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
13130 	},
13131 };
13132 
13133 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13134 			       struct net_device *dev, u32 filter_mask,
13135 			       int nlflags)
13136 {
13137 	struct bnxt *bp = netdev_priv(dev);
13138 
13139 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
13140 				       nlflags, filter_mask, NULL);
13141 }
13142 
13143 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
13144 			       u16 flags, struct netlink_ext_ack *extack)
13145 {
13146 	struct bnxt *bp = netdev_priv(dev);
13147 	struct nlattr *attr, *br_spec;
13148 	int rem, rc = 0;
13149 
13150 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
13151 		return -EOPNOTSUPP;
13152 
13153 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13154 	if (!br_spec)
13155 		return -EINVAL;
13156 
13157 	nla_for_each_nested(attr, br_spec, rem) {
13158 		u16 mode;
13159 
13160 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
13161 			continue;
13162 
13163 		mode = nla_get_u16(attr);
13164 		if (mode == bp->br_mode)
13165 			break;
13166 
13167 		rc = bnxt_hwrm_set_br_mode(bp, mode);
13168 		if (!rc)
13169 			bp->br_mode = mode;
13170 		break;
13171 	}
13172 	return rc;
13173 }
13174 
13175 int bnxt_get_port_parent_id(struct net_device *dev,
13176 			    struct netdev_phys_item_id *ppid)
13177 {
13178 	struct bnxt *bp = netdev_priv(dev);
13179 
13180 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
13181 		return -EOPNOTSUPP;
13182 
13183 	/* The PF and it's VF-reps only support the switchdev framework */
13184 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
13185 		return -EOPNOTSUPP;
13186 
13187 	ppid->id_len = sizeof(bp->dsn);
13188 	memcpy(ppid->id, bp->dsn, ppid->id_len);
13189 
13190 	return 0;
13191 }
13192 
13193 static const struct net_device_ops bnxt_netdev_ops = {
13194 	.ndo_open		= bnxt_open,
13195 	.ndo_start_xmit		= bnxt_start_xmit,
13196 	.ndo_stop		= bnxt_close,
13197 	.ndo_get_stats64	= bnxt_get_stats64,
13198 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
13199 	.ndo_eth_ioctl		= bnxt_ioctl,
13200 	.ndo_validate_addr	= eth_validate_addr,
13201 	.ndo_set_mac_address	= bnxt_change_mac_addr,
13202 	.ndo_change_mtu		= bnxt_change_mtu,
13203 	.ndo_fix_features	= bnxt_fix_features,
13204 	.ndo_set_features	= bnxt_set_features,
13205 	.ndo_features_check	= bnxt_features_check,
13206 	.ndo_tx_timeout		= bnxt_tx_timeout,
13207 #ifdef CONFIG_BNXT_SRIOV
13208 	.ndo_get_vf_config	= bnxt_get_vf_config,
13209 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
13210 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
13211 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
13212 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
13213 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
13214 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
13215 #endif
13216 	.ndo_setup_tc           = bnxt_setup_tc,
13217 #ifdef CONFIG_RFS_ACCEL
13218 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
13219 #endif
13220 	.ndo_bpf		= bnxt_xdp,
13221 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
13222 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
13223 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
13224 };
13225 
13226 static void bnxt_remove_one(struct pci_dev *pdev)
13227 {
13228 	struct net_device *dev = pci_get_drvdata(pdev);
13229 	struct bnxt *bp = netdev_priv(dev);
13230 
13231 	if (BNXT_PF(bp))
13232 		bnxt_sriov_disable(bp);
13233 
13234 	bnxt_rdma_aux_device_uninit(bp);
13235 
13236 	bnxt_ptp_clear(bp);
13237 	unregister_netdev(dev);
13238 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13239 	/* Flush any pending tasks */
13240 	cancel_work_sync(&bp->sp_task);
13241 	cancel_delayed_work_sync(&bp->fw_reset_task);
13242 	bp->sp_event = 0;
13243 
13244 	bnxt_dl_fw_reporters_destroy(bp);
13245 	bnxt_dl_unregister(bp);
13246 	bnxt_shutdown_tc(bp);
13247 
13248 	bnxt_clear_int_mode(bp);
13249 	bnxt_hwrm_func_drv_unrgtr(bp);
13250 	bnxt_free_hwrm_resources(bp);
13251 	bnxt_hwmon_uninit(bp);
13252 	bnxt_ethtool_free(bp);
13253 	bnxt_dcb_free(bp);
13254 	kfree(bp->ptp_cfg);
13255 	bp->ptp_cfg = NULL;
13256 	kfree(bp->fw_health);
13257 	bp->fw_health = NULL;
13258 	bnxt_cleanup_pci(bp);
13259 	bnxt_free_ctx_mem(bp);
13260 	kfree(bp->ctx);
13261 	bp->ctx = NULL;
13262 	kfree(bp->rss_indir_tbl);
13263 	bp->rss_indir_tbl = NULL;
13264 	bnxt_free_port_stats(bp);
13265 	free_netdev(dev);
13266 }
13267 
13268 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
13269 {
13270 	int rc = 0;
13271 	struct bnxt_link_info *link_info = &bp->link_info;
13272 
13273 	bp->phy_flags = 0;
13274 	rc = bnxt_hwrm_phy_qcaps(bp);
13275 	if (rc) {
13276 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
13277 			   rc);
13278 		return rc;
13279 	}
13280 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
13281 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
13282 	else
13283 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
13284 	if (!fw_dflt)
13285 		return 0;
13286 
13287 	mutex_lock(&bp->link_lock);
13288 	rc = bnxt_update_link(bp, false);
13289 	if (rc) {
13290 		mutex_unlock(&bp->link_lock);
13291 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
13292 			   rc);
13293 		return rc;
13294 	}
13295 
13296 	/* Older firmware does not have supported_auto_speeds, so assume
13297 	 * that all supported speeds can be autonegotiated.
13298 	 */
13299 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
13300 		link_info->support_auto_speeds = link_info->support_speeds;
13301 
13302 	bnxt_init_ethtool_link_settings(bp);
13303 	mutex_unlock(&bp->link_lock);
13304 	return 0;
13305 }
13306 
13307 static int bnxt_get_max_irq(struct pci_dev *pdev)
13308 {
13309 	u16 ctrl;
13310 
13311 	if (!pdev->msix_cap)
13312 		return 1;
13313 
13314 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
13315 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
13316 }
13317 
13318 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
13319 				int *max_cp)
13320 {
13321 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13322 	int max_ring_grps = 0, max_irq;
13323 
13324 	*max_tx = hw_resc->max_tx_rings;
13325 	*max_rx = hw_resc->max_rx_rings;
13326 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
13327 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
13328 			bnxt_get_ulp_msix_num(bp),
13329 			hw_resc->max_stat_ctxs - bnxt_get_ulp_stat_ctxs(bp));
13330 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
13331 		*max_cp = min_t(int, *max_cp, max_irq);
13332 	max_ring_grps = hw_resc->max_hw_ring_grps;
13333 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
13334 		*max_cp -= 1;
13335 		*max_rx -= 2;
13336 	}
13337 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
13338 		*max_rx >>= 1;
13339 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
13340 		bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
13341 		/* On P5 chips, max_cp output param should be available NQs */
13342 		*max_cp = max_irq;
13343 	}
13344 	*max_rx = min_t(int, *max_rx, max_ring_grps);
13345 }
13346 
13347 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
13348 {
13349 	int rx, tx, cp;
13350 
13351 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
13352 	*max_rx = rx;
13353 	*max_tx = tx;
13354 	if (!rx || !tx || !cp)
13355 		return -ENOMEM;
13356 
13357 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
13358 }
13359 
13360 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
13361 			       bool shared)
13362 {
13363 	int rc;
13364 
13365 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
13366 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
13367 		/* Not enough rings, try disabling agg rings. */
13368 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
13369 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
13370 		if (rc) {
13371 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
13372 			bp->flags |= BNXT_FLAG_AGG_RINGS;
13373 			return rc;
13374 		}
13375 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
13376 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13377 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13378 		bnxt_set_ring_params(bp);
13379 	}
13380 
13381 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
13382 		int max_cp, max_stat, max_irq;
13383 
13384 		/* Reserve minimum resources for RoCE */
13385 		max_cp = bnxt_get_max_func_cp_rings(bp);
13386 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
13387 		max_irq = bnxt_get_max_func_irqs(bp);
13388 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
13389 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
13390 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
13391 			return 0;
13392 
13393 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
13394 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
13395 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
13396 		max_cp = min_t(int, max_cp, max_irq);
13397 		max_cp = min_t(int, max_cp, max_stat);
13398 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
13399 		if (rc)
13400 			rc = 0;
13401 	}
13402 	return rc;
13403 }
13404 
13405 /* In initial default shared ring setting, each shared ring must have a
13406  * RX/TX ring pair.
13407  */
13408 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
13409 {
13410 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
13411 	bp->rx_nr_rings = bp->cp_nr_rings;
13412 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
13413 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
13414 }
13415 
13416 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
13417 {
13418 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
13419 
13420 	if (!bnxt_can_reserve_rings(bp))
13421 		return 0;
13422 
13423 	if (sh)
13424 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
13425 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
13426 	/* Reduce default rings on multi-port cards so that total default
13427 	 * rings do not exceed CPU count.
13428 	 */
13429 	if (bp->port_count > 1) {
13430 		int max_rings =
13431 			max_t(int, num_online_cpus() / bp->port_count, 1);
13432 
13433 		dflt_rings = min_t(int, dflt_rings, max_rings);
13434 	}
13435 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
13436 	if (rc)
13437 		return rc;
13438 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
13439 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
13440 	if (sh)
13441 		bnxt_trim_dflt_sh_rings(bp);
13442 	else
13443 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
13444 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
13445 
13446 	rc = __bnxt_reserve_rings(bp);
13447 	if (rc && rc != -ENODEV)
13448 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
13449 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
13450 	if (sh)
13451 		bnxt_trim_dflt_sh_rings(bp);
13452 
13453 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
13454 	if (bnxt_need_reserve_rings(bp)) {
13455 		rc = __bnxt_reserve_rings(bp);
13456 		if (rc && rc != -ENODEV)
13457 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
13458 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
13459 	}
13460 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
13461 		bp->rx_nr_rings++;
13462 		bp->cp_nr_rings++;
13463 	}
13464 	if (rc) {
13465 		bp->tx_nr_rings = 0;
13466 		bp->rx_nr_rings = 0;
13467 	}
13468 	return rc;
13469 }
13470 
13471 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
13472 {
13473 	int rc;
13474 
13475 	if (bp->tx_nr_rings)
13476 		return 0;
13477 
13478 	bnxt_ulp_irq_stop(bp);
13479 	bnxt_clear_int_mode(bp);
13480 	rc = bnxt_set_dflt_rings(bp, true);
13481 	if (rc) {
13482 		if (BNXT_VF(bp) && rc == -ENODEV)
13483 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
13484 		else
13485 			netdev_err(bp->dev, "Not enough rings available.\n");
13486 		goto init_dflt_ring_err;
13487 	}
13488 	rc = bnxt_init_int_mode(bp);
13489 	if (rc)
13490 		goto init_dflt_ring_err;
13491 
13492 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
13493 
13494 	bnxt_set_dflt_rfs(bp);
13495 
13496 init_dflt_ring_err:
13497 	bnxt_ulp_irq_restart(bp, rc);
13498 	return rc;
13499 }
13500 
13501 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
13502 {
13503 	int rc;
13504 
13505 	ASSERT_RTNL();
13506 	bnxt_hwrm_func_qcaps(bp);
13507 
13508 	if (netif_running(bp->dev))
13509 		__bnxt_close_nic(bp, true, false);
13510 
13511 	bnxt_ulp_irq_stop(bp);
13512 	bnxt_clear_int_mode(bp);
13513 	rc = bnxt_init_int_mode(bp);
13514 	bnxt_ulp_irq_restart(bp, rc);
13515 
13516 	if (netif_running(bp->dev)) {
13517 		if (rc)
13518 			dev_close(bp->dev);
13519 		else
13520 			rc = bnxt_open_nic(bp, true, false);
13521 	}
13522 
13523 	return rc;
13524 }
13525 
13526 static int bnxt_init_mac_addr(struct bnxt *bp)
13527 {
13528 	int rc = 0;
13529 
13530 	if (BNXT_PF(bp)) {
13531 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
13532 	} else {
13533 #ifdef CONFIG_BNXT_SRIOV
13534 		struct bnxt_vf_info *vf = &bp->vf;
13535 		bool strict_approval = true;
13536 
13537 		if (is_valid_ether_addr(vf->mac_addr)) {
13538 			/* overwrite netdev dev_addr with admin VF MAC */
13539 			eth_hw_addr_set(bp->dev, vf->mac_addr);
13540 			/* Older PF driver or firmware may not approve this
13541 			 * correctly.
13542 			 */
13543 			strict_approval = false;
13544 		} else {
13545 			eth_hw_addr_random(bp->dev);
13546 		}
13547 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
13548 #endif
13549 	}
13550 	return rc;
13551 }
13552 
13553 static void bnxt_vpd_read_info(struct bnxt *bp)
13554 {
13555 	struct pci_dev *pdev = bp->pdev;
13556 	unsigned int vpd_size, kw_len;
13557 	int pos, size;
13558 	u8 *vpd_data;
13559 
13560 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
13561 	if (IS_ERR(vpd_data)) {
13562 		pci_warn(pdev, "Unable to read VPD\n");
13563 		return;
13564 	}
13565 
13566 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
13567 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
13568 	if (pos < 0)
13569 		goto read_sn;
13570 
13571 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
13572 	memcpy(bp->board_partno, &vpd_data[pos], size);
13573 
13574 read_sn:
13575 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
13576 					   PCI_VPD_RO_KEYWORD_SERIALNO,
13577 					   &kw_len);
13578 	if (pos < 0)
13579 		goto exit;
13580 
13581 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
13582 	memcpy(bp->board_serialno, &vpd_data[pos], size);
13583 exit:
13584 	kfree(vpd_data);
13585 }
13586 
13587 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
13588 {
13589 	struct pci_dev *pdev = bp->pdev;
13590 	u64 qword;
13591 
13592 	qword = pci_get_dsn(pdev);
13593 	if (!qword) {
13594 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
13595 		return -EOPNOTSUPP;
13596 	}
13597 
13598 	put_unaligned_le64(qword, dsn);
13599 
13600 	bp->flags |= BNXT_FLAG_DSN_VALID;
13601 	return 0;
13602 }
13603 
13604 static int bnxt_map_db_bar(struct bnxt *bp)
13605 {
13606 	if (!bp->db_size)
13607 		return -ENODEV;
13608 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
13609 	if (!bp->bar1)
13610 		return -ENOMEM;
13611 	return 0;
13612 }
13613 
13614 void bnxt_print_device_info(struct bnxt *bp)
13615 {
13616 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
13617 		    board_info[bp->board_idx].name,
13618 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
13619 
13620 	pcie_print_link_status(bp->pdev);
13621 }
13622 
13623 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
13624 {
13625 	struct net_device *dev;
13626 	struct bnxt *bp;
13627 	int rc, max_irqs;
13628 
13629 	if (pci_is_bridge(pdev))
13630 		return -ENODEV;
13631 
13632 	/* Clear any pending DMA transactions from crash kernel
13633 	 * while loading driver in capture kernel.
13634 	 */
13635 	if (is_kdump_kernel()) {
13636 		pci_clear_master(pdev);
13637 		pcie_flr(pdev);
13638 	}
13639 
13640 	max_irqs = bnxt_get_max_irq(pdev);
13641 	dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
13642 	if (!dev)
13643 		return -ENOMEM;
13644 
13645 	bp = netdev_priv(dev);
13646 	bp->board_idx = ent->driver_data;
13647 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
13648 	bnxt_set_max_func_irqs(bp, max_irqs);
13649 
13650 	if (bnxt_vf_pciid(bp->board_idx))
13651 		bp->flags |= BNXT_FLAG_VF;
13652 
13653 	/* No devlink port registration in case of a VF */
13654 	if (BNXT_PF(bp))
13655 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
13656 
13657 	if (pdev->msix_cap)
13658 		bp->flags |= BNXT_FLAG_MSIX_CAP;
13659 
13660 	rc = bnxt_init_board(pdev, dev);
13661 	if (rc < 0)
13662 		goto init_err_free;
13663 
13664 	dev->netdev_ops = &bnxt_netdev_ops;
13665 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
13666 	dev->ethtool_ops = &bnxt_ethtool_ops;
13667 	pci_set_drvdata(pdev, dev);
13668 
13669 	rc = bnxt_alloc_hwrm_resources(bp);
13670 	if (rc)
13671 		goto init_err_pci_clean;
13672 
13673 	mutex_init(&bp->hwrm_cmd_lock);
13674 	mutex_init(&bp->link_lock);
13675 
13676 	rc = bnxt_fw_init_one_p1(bp);
13677 	if (rc)
13678 		goto init_err_pci_clean;
13679 
13680 	if (BNXT_PF(bp))
13681 		bnxt_vpd_read_info(bp);
13682 
13683 	if (BNXT_CHIP_P5(bp)) {
13684 		bp->flags |= BNXT_FLAG_CHIP_P5;
13685 		if (BNXT_CHIP_SR2(bp))
13686 			bp->flags |= BNXT_FLAG_CHIP_SR2;
13687 	}
13688 
13689 	rc = bnxt_alloc_rss_indir_tbl(bp);
13690 	if (rc)
13691 		goto init_err_pci_clean;
13692 
13693 	rc = bnxt_fw_init_one_p2(bp);
13694 	if (rc)
13695 		goto init_err_pci_clean;
13696 
13697 	rc = bnxt_map_db_bar(bp);
13698 	if (rc) {
13699 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
13700 			rc);
13701 		goto init_err_pci_clean;
13702 	}
13703 
13704 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
13705 			   NETIF_F_TSO | NETIF_F_TSO6 |
13706 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
13707 			   NETIF_F_GSO_IPXIP4 |
13708 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
13709 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
13710 			   NETIF_F_RXCSUM | NETIF_F_GRO;
13711 
13712 	if (BNXT_SUPPORTS_TPA(bp))
13713 		dev->hw_features |= NETIF_F_LRO;
13714 
13715 	dev->hw_enc_features =
13716 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
13717 			NETIF_F_TSO | NETIF_F_TSO6 |
13718 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
13719 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
13720 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
13721 	dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
13722 
13723 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
13724 				    NETIF_F_GSO_GRE_CSUM;
13725 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
13726 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
13727 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13728 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
13729 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
13730 	if (BNXT_SUPPORTS_TPA(bp))
13731 		dev->hw_features |= NETIF_F_GRO_HW;
13732 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
13733 	if (dev->features & NETIF_F_GRO_HW)
13734 		dev->features &= ~NETIF_F_LRO;
13735 	dev->priv_flags |= IFF_UNICAST_FLT;
13736 
13737 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
13738 
13739 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
13740 			    NETDEV_XDP_ACT_RX_SG;
13741 
13742 #ifdef CONFIG_BNXT_SRIOV
13743 	init_waitqueue_head(&bp->sriov_cfg_wait);
13744 #endif
13745 	if (BNXT_SUPPORTS_TPA(bp)) {
13746 		bp->gro_func = bnxt_gro_func_5730x;
13747 		if (BNXT_CHIP_P4(bp))
13748 			bp->gro_func = bnxt_gro_func_5731x;
13749 		else if (BNXT_CHIP_P5(bp))
13750 			bp->gro_func = bnxt_gro_func_5750x;
13751 	}
13752 	if (!BNXT_CHIP_P4_PLUS(bp))
13753 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
13754 
13755 	rc = bnxt_init_mac_addr(bp);
13756 	if (rc) {
13757 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
13758 		rc = -EADDRNOTAVAIL;
13759 		goto init_err_pci_clean;
13760 	}
13761 
13762 	if (BNXT_PF(bp)) {
13763 		/* Read the adapter's DSN to use as the eswitch switch_id */
13764 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
13765 	}
13766 
13767 	/* MTU range: 60 - FW defined max */
13768 	dev->min_mtu = ETH_ZLEN;
13769 	dev->max_mtu = bp->max_mtu;
13770 
13771 	rc = bnxt_probe_phy(bp, true);
13772 	if (rc)
13773 		goto init_err_pci_clean;
13774 
13775 	bnxt_set_rx_skb_mode(bp, false);
13776 	bnxt_set_tpa_flags(bp);
13777 	bnxt_set_ring_params(bp);
13778 	rc = bnxt_set_dflt_rings(bp, true);
13779 	if (rc) {
13780 		if (BNXT_VF(bp) && rc == -ENODEV) {
13781 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
13782 		} else {
13783 			netdev_err(bp->dev, "Not enough rings available.\n");
13784 			rc = -ENOMEM;
13785 		}
13786 		goto init_err_pci_clean;
13787 	}
13788 
13789 	bnxt_fw_init_one_p3(bp);
13790 
13791 	bnxt_init_dflt_coal(bp);
13792 
13793 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13794 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
13795 
13796 	rc = bnxt_init_int_mode(bp);
13797 	if (rc)
13798 		goto init_err_pci_clean;
13799 
13800 	/* No TC has been set yet and rings may have been trimmed due to
13801 	 * limited MSIX, so we re-initialize the TX rings per TC.
13802 	 */
13803 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
13804 
13805 	if (BNXT_PF(bp)) {
13806 		if (!bnxt_pf_wq) {
13807 			bnxt_pf_wq =
13808 				create_singlethread_workqueue("bnxt_pf_wq");
13809 			if (!bnxt_pf_wq) {
13810 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
13811 				rc = -ENOMEM;
13812 				goto init_err_pci_clean;
13813 			}
13814 		}
13815 		rc = bnxt_init_tc(bp);
13816 		if (rc)
13817 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
13818 				   rc);
13819 	}
13820 
13821 	bnxt_inv_fw_health_reg(bp);
13822 	rc = bnxt_dl_register(bp);
13823 	if (rc)
13824 		goto init_err_dl;
13825 
13826 	rc = register_netdev(dev);
13827 	if (rc)
13828 		goto init_err_cleanup;
13829 
13830 	bnxt_dl_fw_reporters_create(bp);
13831 
13832 	bnxt_rdma_aux_device_init(bp);
13833 
13834 	bnxt_print_device_info(bp);
13835 
13836 	pci_save_state(pdev);
13837 
13838 	return 0;
13839 init_err_cleanup:
13840 	bnxt_dl_unregister(bp);
13841 init_err_dl:
13842 	bnxt_shutdown_tc(bp);
13843 	bnxt_clear_int_mode(bp);
13844 
13845 init_err_pci_clean:
13846 	bnxt_hwrm_func_drv_unrgtr(bp);
13847 	bnxt_free_hwrm_resources(bp);
13848 	bnxt_hwmon_uninit(bp);
13849 	bnxt_ethtool_free(bp);
13850 	bnxt_ptp_clear(bp);
13851 	kfree(bp->ptp_cfg);
13852 	bp->ptp_cfg = NULL;
13853 	kfree(bp->fw_health);
13854 	bp->fw_health = NULL;
13855 	bnxt_cleanup_pci(bp);
13856 	bnxt_free_ctx_mem(bp);
13857 	kfree(bp->ctx);
13858 	bp->ctx = NULL;
13859 	kfree(bp->rss_indir_tbl);
13860 	bp->rss_indir_tbl = NULL;
13861 
13862 init_err_free:
13863 	free_netdev(dev);
13864 	return rc;
13865 }
13866 
13867 static void bnxt_shutdown(struct pci_dev *pdev)
13868 {
13869 	struct net_device *dev = pci_get_drvdata(pdev);
13870 	struct bnxt *bp;
13871 
13872 	if (!dev)
13873 		return;
13874 
13875 	rtnl_lock();
13876 	bp = netdev_priv(dev);
13877 	if (!bp)
13878 		goto shutdown_exit;
13879 
13880 	if (netif_running(dev))
13881 		dev_close(dev);
13882 
13883 	bnxt_clear_int_mode(bp);
13884 	pci_disable_device(pdev);
13885 
13886 	if (system_state == SYSTEM_POWER_OFF) {
13887 		pci_wake_from_d3(pdev, bp->wol);
13888 		pci_set_power_state(pdev, PCI_D3hot);
13889 	}
13890 
13891 shutdown_exit:
13892 	rtnl_unlock();
13893 }
13894 
13895 #ifdef CONFIG_PM_SLEEP
13896 static int bnxt_suspend(struct device *device)
13897 {
13898 	struct net_device *dev = dev_get_drvdata(device);
13899 	struct bnxt *bp = netdev_priv(dev);
13900 	int rc = 0;
13901 
13902 	rtnl_lock();
13903 	bnxt_ulp_stop(bp);
13904 	if (netif_running(dev)) {
13905 		netif_device_detach(dev);
13906 		rc = bnxt_close(dev);
13907 	}
13908 	bnxt_hwrm_func_drv_unrgtr(bp);
13909 	pci_disable_device(bp->pdev);
13910 	bnxt_free_ctx_mem(bp);
13911 	kfree(bp->ctx);
13912 	bp->ctx = NULL;
13913 	rtnl_unlock();
13914 	return rc;
13915 }
13916 
13917 static int bnxt_resume(struct device *device)
13918 {
13919 	struct net_device *dev = dev_get_drvdata(device);
13920 	struct bnxt *bp = netdev_priv(dev);
13921 	int rc = 0;
13922 
13923 	rtnl_lock();
13924 	rc = pci_enable_device(bp->pdev);
13925 	if (rc) {
13926 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
13927 			   rc);
13928 		goto resume_exit;
13929 	}
13930 	pci_set_master(bp->pdev);
13931 	if (bnxt_hwrm_ver_get(bp)) {
13932 		rc = -ENODEV;
13933 		goto resume_exit;
13934 	}
13935 	rc = bnxt_hwrm_func_reset(bp);
13936 	if (rc) {
13937 		rc = -EBUSY;
13938 		goto resume_exit;
13939 	}
13940 
13941 	rc = bnxt_hwrm_func_qcaps(bp);
13942 	if (rc)
13943 		goto resume_exit;
13944 
13945 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
13946 		rc = -ENODEV;
13947 		goto resume_exit;
13948 	}
13949 
13950 	bnxt_get_wol_settings(bp);
13951 	if (netif_running(dev)) {
13952 		rc = bnxt_open(dev);
13953 		if (!rc)
13954 			netif_device_attach(dev);
13955 	}
13956 
13957 resume_exit:
13958 	bnxt_ulp_start(bp, rc);
13959 	if (!rc)
13960 		bnxt_reenable_sriov(bp);
13961 	rtnl_unlock();
13962 	return rc;
13963 }
13964 
13965 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
13966 #define BNXT_PM_OPS (&bnxt_pm_ops)
13967 
13968 #else
13969 
13970 #define BNXT_PM_OPS NULL
13971 
13972 #endif /* CONFIG_PM_SLEEP */
13973 
13974 /**
13975  * bnxt_io_error_detected - called when PCI error is detected
13976  * @pdev: Pointer to PCI device
13977  * @state: The current pci connection state
13978  *
13979  * This function is called after a PCI bus error affecting
13980  * this device has been detected.
13981  */
13982 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
13983 					       pci_channel_state_t state)
13984 {
13985 	struct net_device *netdev = pci_get_drvdata(pdev);
13986 	struct bnxt *bp = netdev_priv(netdev);
13987 
13988 	netdev_info(netdev, "PCI I/O error detected\n");
13989 
13990 	rtnl_lock();
13991 	netif_device_detach(netdev);
13992 
13993 	bnxt_ulp_stop(bp);
13994 
13995 	if (state == pci_channel_io_perm_failure) {
13996 		rtnl_unlock();
13997 		return PCI_ERS_RESULT_DISCONNECT;
13998 	}
13999 
14000 	if (state == pci_channel_io_frozen)
14001 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
14002 
14003 	if (netif_running(netdev))
14004 		bnxt_close(netdev);
14005 
14006 	if (pci_is_enabled(pdev))
14007 		pci_disable_device(pdev);
14008 	bnxt_free_ctx_mem(bp);
14009 	kfree(bp->ctx);
14010 	bp->ctx = NULL;
14011 	rtnl_unlock();
14012 
14013 	/* Request a slot slot reset. */
14014 	return PCI_ERS_RESULT_NEED_RESET;
14015 }
14016 
14017 /**
14018  * bnxt_io_slot_reset - called after the pci bus has been reset.
14019  * @pdev: Pointer to PCI device
14020  *
14021  * Restart the card from scratch, as if from a cold-boot.
14022  * At this point, the card has exprienced a hard reset,
14023  * followed by fixups by BIOS, and has its config space
14024  * set up identically to what it was at cold boot.
14025  */
14026 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
14027 {
14028 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
14029 	struct net_device *netdev = pci_get_drvdata(pdev);
14030 	struct bnxt *bp = netdev_priv(netdev);
14031 	int retry = 0;
14032 	int err = 0;
14033 	int off;
14034 
14035 	netdev_info(bp->dev, "PCI Slot Reset\n");
14036 
14037 	rtnl_lock();
14038 
14039 	if (pci_enable_device(pdev)) {
14040 		dev_err(&pdev->dev,
14041 			"Cannot re-enable PCI device after reset.\n");
14042 	} else {
14043 		pci_set_master(pdev);
14044 		/* Upon fatal error, our device internal logic that latches to
14045 		 * BAR value is getting reset and will restore only upon
14046 		 * rewritting the BARs.
14047 		 *
14048 		 * As pci_restore_state() does not re-write the BARs if the
14049 		 * value is same as saved value earlier, driver needs to
14050 		 * write the BARs to 0 to force restore, in case of fatal error.
14051 		 */
14052 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
14053 				       &bp->state)) {
14054 			for (off = PCI_BASE_ADDRESS_0;
14055 			     off <= PCI_BASE_ADDRESS_5; off += 4)
14056 				pci_write_config_dword(bp->pdev, off, 0);
14057 		}
14058 		pci_restore_state(pdev);
14059 		pci_save_state(pdev);
14060 
14061 		bnxt_inv_fw_health_reg(bp);
14062 		bnxt_try_map_fw_health_reg(bp);
14063 
14064 		/* In some PCIe AER scenarios, firmware may take up to
14065 		 * 10 seconds to become ready in the worst case.
14066 		 */
14067 		do {
14068 			err = bnxt_try_recover_fw(bp);
14069 			if (!err)
14070 				break;
14071 			retry++;
14072 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
14073 
14074 		if (err) {
14075 			dev_err(&pdev->dev, "Firmware not ready\n");
14076 			goto reset_exit;
14077 		}
14078 
14079 		err = bnxt_hwrm_func_reset(bp);
14080 		if (!err)
14081 			result = PCI_ERS_RESULT_RECOVERED;
14082 
14083 		bnxt_ulp_irq_stop(bp);
14084 		bnxt_clear_int_mode(bp);
14085 		err = bnxt_init_int_mode(bp);
14086 		bnxt_ulp_irq_restart(bp, err);
14087 	}
14088 
14089 reset_exit:
14090 	bnxt_clear_reservations(bp, true);
14091 	rtnl_unlock();
14092 
14093 	return result;
14094 }
14095 
14096 /**
14097  * bnxt_io_resume - called when traffic can start flowing again.
14098  * @pdev: Pointer to PCI device
14099  *
14100  * This callback is called when the error recovery driver tells
14101  * us that its OK to resume normal operation.
14102  */
14103 static void bnxt_io_resume(struct pci_dev *pdev)
14104 {
14105 	struct net_device *netdev = pci_get_drvdata(pdev);
14106 	struct bnxt *bp = netdev_priv(netdev);
14107 	int err;
14108 
14109 	netdev_info(bp->dev, "PCI Slot Resume\n");
14110 	rtnl_lock();
14111 
14112 	err = bnxt_hwrm_func_qcaps(bp);
14113 	if (!err && netif_running(netdev))
14114 		err = bnxt_open(netdev);
14115 
14116 	bnxt_ulp_start(bp, err);
14117 	if (!err) {
14118 		bnxt_reenable_sriov(bp);
14119 		netif_device_attach(netdev);
14120 	}
14121 
14122 	rtnl_unlock();
14123 }
14124 
14125 static const struct pci_error_handlers bnxt_err_handler = {
14126 	.error_detected	= bnxt_io_error_detected,
14127 	.slot_reset	= bnxt_io_slot_reset,
14128 	.resume		= bnxt_io_resume
14129 };
14130 
14131 static struct pci_driver bnxt_pci_driver = {
14132 	.name		= DRV_MODULE_NAME,
14133 	.id_table	= bnxt_pci_tbl,
14134 	.probe		= bnxt_init_one,
14135 	.remove		= bnxt_remove_one,
14136 	.shutdown	= bnxt_shutdown,
14137 	.driver.pm	= BNXT_PM_OPS,
14138 	.err_handler	= &bnxt_err_handler,
14139 #if defined(CONFIG_BNXT_SRIOV)
14140 	.sriov_configure = bnxt_sriov_configure,
14141 #endif
14142 };
14143 
14144 static int __init bnxt_init(void)
14145 {
14146 	int err;
14147 
14148 	bnxt_debug_init();
14149 	err = pci_register_driver(&bnxt_pci_driver);
14150 	if (err) {
14151 		bnxt_debug_exit();
14152 		return err;
14153 	}
14154 
14155 	return 0;
14156 }
14157 
14158 static void __exit bnxt_exit(void)
14159 {
14160 	pci_unregister_driver(&bnxt_pci_driver);
14161 	if (bnxt_pf_wq)
14162 		destroy_workqueue(bnxt_pf_wq);
14163 	bnxt_debug_exit();
14164 }
14165 
14166 module_init(bnxt_init);
14167 module_exit(bnxt_exit);
14168