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