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