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