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