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