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