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