xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision 4eb9da050f005fbbb7d301e8e99cfdb6e4771a0d)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  * Copyright (c) 2016-2019 Broadcom Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  */
10 
11 #include <linux/module.h>
12 
13 #include <linux/stringify.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/pci.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/bitops.h>
27 #include <linux/io.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/byteorder.h>
31 #include <asm/page.h>
32 #include <linux/time.h>
33 #include <linux/mii.h>
34 #include <linux/mdio.h>
35 #include <linux/if.h>
36 #include <linux/if_vlan.h>
37 #include <linux/if_bridge.h>
38 #include <linux/rtc.h>
39 #include <linux/bpf.h>
40 #include <net/gro.h>
41 #include <net/ip.h>
42 #include <net/tcp.h>
43 #include <net/udp.h>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
46 #include <net/udp_tunnel.h>
47 #include <linux/workqueue.h>
48 #include <linux/prefetch.h>
49 #include <linux/cache.h>
50 #include <linux/log2.h>
51 #include <linux/bitmap.h>
52 #include <linux/cpu_rmap.h>
53 #include <linux/cpumask.h>
54 #include <net/pkt_cls.h>
55 #include <net/page_pool/helpers.h>
56 #include <linux/align.h>
57 #include <net/netdev_lock.h>
58 #include <net/netdev_queues.h>
59 #include <net/netdev_rx_queue.h>
60 #include <linux/pci-tph.h>
61 
62 #include "bnxt_hsi.h"
63 #include "bnxt.h"
64 #include "bnxt_hwrm.h"
65 #include "bnxt_ulp.h"
66 #include "bnxt_sriov.h"
67 #include "bnxt_ethtool.h"
68 #include "bnxt_dcb.h"
69 #include "bnxt_xdp.h"
70 #include "bnxt_ptp.h"
71 #include "bnxt_vfr.h"
72 #include "bnxt_tc.h"
73 #include "bnxt_devlink.h"
74 #include "bnxt_debugfs.h"
75 #include "bnxt_coredump.h"
76 #include "bnxt_hwmon.h"
77 
78 #define BNXT_TX_TIMEOUT		(5 * HZ)
79 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
80 				 NETIF_MSG_TX_ERR)
81 
82 MODULE_IMPORT_NS("NETDEV_INTERNAL");
83 MODULE_LICENSE("GPL");
84 MODULE_DESCRIPTION("Broadcom NetXtreme network driver");
85 
86 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
87 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
88 
89 #define BNXT_TX_PUSH_THRESH 164
90 
91 /* indexed by enum board_idx */
92 static const struct {
93 	char *name;
94 } board_info[] = {
95 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
96 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
97 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
98 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
99 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
100 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
101 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
102 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
103 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
104 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
105 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
106 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
108 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
109 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
110 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
111 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
112 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
113 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
114 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
115 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
116 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
117 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
118 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
119 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
120 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
121 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
122 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
123 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
124 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
125 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
126 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
127 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
128 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
129 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
130 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
131 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
132 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
133 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
134 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
135 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
136 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
137 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
138 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
139 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
140 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
141 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
142 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
143 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
144 	[NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" },
145 };
146 
147 static const struct pci_device_id bnxt_pci_tbl[] = {
148 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
149 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
150 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
151 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
152 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
153 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
155 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
156 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
159 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
160 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
164 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
165 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
166 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
167 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
168 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
169 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
170 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
171 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
172 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
173 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
174 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
175 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
176 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
178 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
179 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
180 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
181 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
182 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
183 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
184 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
185 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
187 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
188 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
189 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
190 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
191 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
192 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
193 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
194 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
195 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
196 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
197 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
198 #ifdef CONFIG_BNXT_SRIOV
199 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
200 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
201 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
202 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
203 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
204 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
205 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
206 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
207 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
208 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
209 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
210 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
211 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
212 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
214 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
215 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
216 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
217 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
218 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
219 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
220 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
221 #endif
222 	{ 0 }
223 };
224 
225 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
226 
227 static const u16 bnxt_vf_req_snif[] = {
228 	HWRM_FUNC_CFG,
229 	HWRM_FUNC_VF_CFG,
230 	HWRM_PORT_PHY_QCFG,
231 	HWRM_CFA_L2_FILTER_ALLOC,
232 };
233 
234 static const u16 bnxt_async_events_arr[] = {
235 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
236 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
237 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
238 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
239 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
240 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
241 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
242 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
243 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
244 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
245 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
246 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
247 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
248 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
249 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
250 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
251 	ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER,
252 };
253 
254 const u16 bnxt_bstore_to_trace[] = {
255 	[BNXT_CTX_SRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT_TRACE,
256 	[BNXT_CTX_SRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT2_TRACE,
257 	[BNXT_CTX_CRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT_TRACE,
258 	[BNXT_CTX_CRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT2_TRACE,
259 	[BNXT_CTX_RIGP0]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP0_TRACE,
260 	[BNXT_CTX_L2HWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_L2_HWRM_TRACE,
261 	[BNXT_CTX_REHWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ROCE_HWRM_TRACE,
262 	[BNXT_CTX_CA0]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA0_TRACE,
263 	[BNXT_CTX_CA1]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA1_TRACE,
264 	[BNXT_CTX_CA2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA2_TRACE,
265 	[BNXT_CTX_RIGP1]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP1_TRACE,
266 };
267 
268 static struct workqueue_struct *bnxt_pf_wq;
269 
270 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
271 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
272 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
273 
274 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
275 	.ports = {
276 		.src = 0,
277 		.dst = 0,
278 	},
279 	.addrs = {
280 		.v6addrs = {
281 			.src = BNXT_IPV6_MASK_NONE,
282 			.dst = BNXT_IPV6_MASK_NONE,
283 		},
284 	},
285 };
286 
287 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
288 	.ports = {
289 		.src = cpu_to_be16(0xffff),
290 		.dst = cpu_to_be16(0xffff),
291 	},
292 	.addrs = {
293 		.v6addrs = {
294 			.src = BNXT_IPV6_MASK_ALL,
295 			.dst = BNXT_IPV6_MASK_ALL,
296 		},
297 	},
298 };
299 
300 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
301 	.ports = {
302 		.src = cpu_to_be16(0xffff),
303 		.dst = cpu_to_be16(0xffff),
304 	},
305 	.addrs = {
306 		.v4addrs = {
307 			.src = cpu_to_be32(0xffffffff),
308 			.dst = cpu_to_be32(0xffffffff),
309 		},
310 	},
311 };
312 
313 static bool bnxt_vf_pciid(enum board_idx idx)
314 {
315 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
316 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
317 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
318 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF);
319 }
320 
321 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
322 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
323 
324 #define BNXT_DB_CQ(db, idx)						\
325 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
326 
327 #define BNXT_DB_NQ_P5(db, idx)						\
328 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
329 		    (db)->doorbell)
330 
331 #define BNXT_DB_NQ_P7(db, idx)						\
332 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
333 		    DB_RING_IDX(db, idx), (db)->doorbell)
334 
335 #define BNXT_DB_CQ_ARM(db, idx)						\
336 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
337 
338 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
339 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
340 		    DB_RING_IDX(db, idx), (db)->doorbell)
341 
342 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
343 {
344 	if (bp->flags & BNXT_FLAG_CHIP_P7)
345 		BNXT_DB_NQ_P7(db, idx);
346 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
347 		BNXT_DB_NQ_P5(db, idx);
348 	else
349 		BNXT_DB_CQ(db, idx);
350 }
351 
352 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
353 {
354 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
355 		BNXT_DB_NQ_ARM_P5(db, idx);
356 	else
357 		BNXT_DB_CQ_ARM(db, idx);
358 }
359 
360 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
361 {
362 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
363 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
364 			    DB_RING_IDX(db, idx), db->doorbell);
365 	else
366 		BNXT_DB_CQ(db, idx);
367 }
368 
369 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
370 {
371 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
372 		return;
373 
374 	if (BNXT_PF(bp))
375 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
376 	else
377 		schedule_delayed_work(&bp->fw_reset_task, delay);
378 }
379 
380 static void __bnxt_queue_sp_work(struct bnxt *bp)
381 {
382 	if (BNXT_PF(bp))
383 		queue_work(bnxt_pf_wq, &bp->sp_task);
384 	else
385 		schedule_work(&bp->sp_task);
386 }
387 
388 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
389 {
390 	set_bit(event, &bp->sp_event);
391 	__bnxt_queue_sp_work(bp);
392 }
393 
394 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
395 {
396 	if (!rxr->bnapi->in_reset) {
397 		rxr->bnapi->in_reset = true;
398 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
399 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
400 		else
401 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
402 		__bnxt_queue_sp_work(bp);
403 	}
404 	rxr->rx_next_cons = 0xffff;
405 }
406 
407 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
408 			  u16 curr)
409 {
410 	struct bnxt_napi *bnapi = txr->bnapi;
411 
412 	if (bnapi->tx_fault)
413 		return;
414 
415 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
416 		   txr->txq_index, txr->tx_hw_cons,
417 		   txr->tx_cons, txr->tx_prod, curr);
418 	WARN_ON_ONCE(1);
419 	bnapi->tx_fault = 1;
420 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
421 }
422 
423 const u16 bnxt_lhint_arr[] = {
424 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
425 	TX_BD_FLAGS_LHINT_512_TO_1023,
426 	TX_BD_FLAGS_LHINT_1024_TO_2047,
427 	TX_BD_FLAGS_LHINT_1024_TO_2047,
428 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
429 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
430 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
431 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
432 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
433 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
434 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
435 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
436 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
437 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
438 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
439 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
440 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
441 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
442 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
443 };
444 
445 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
446 {
447 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
448 
449 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
450 		return 0;
451 
452 	return md_dst->u.port_info.port_id;
453 }
454 
455 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
456 			     u16 prod)
457 {
458 	/* Sync BD data before updating doorbell */
459 	wmb();
460 	bnxt_db_write(bp, &txr->tx_db, prod);
461 	txr->kick_pending = 0;
462 }
463 
464 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
465 {
466 	struct bnxt *bp = netdev_priv(dev);
467 	struct tx_bd *txbd, *txbd0;
468 	struct tx_bd_ext *txbd1;
469 	struct netdev_queue *txq;
470 	int i;
471 	dma_addr_t mapping;
472 	unsigned int length, pad = 0;
473 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
474 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
475 	struct pci_dev *pdev = bp->pdev;
476 	u16 prod, last_frag, txts_prod;
477 	struct bnxt_tx_ring_info *txr;
478 	struct bnxt_sw_tx_bd *tx_buf;
479 	__le32 lflags = 0;
480 
481 	i = skb_get_queue_mapping(skb);
482 	if (unlikely(i >= bp->tx_nr_rings)) {
483 		dev_kfree_skb_any(skb);
484 		dev_core_stats_tx_dropped_inc(dev);
485 		return NETDEV_TX_OK;
486 	}
487 
488 	txq = netdev_get_tx_queue(dev, i);
489 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
490 	prod = txr->tx_prod;
491 
492 #if (MAX_SKB_FRAGS > TX_MAX_FRAGS)
493 	if (skb_shinfo(skb)->nr_frags > TX_MAX_FRAGS) {
494 		netdev_warn_once(dev, "SKB has too many (%d) fragments, max supported is %d.  SKB will be linearized.\n",
495 				 skb_shinfo(skb)->nr_frags, TX_MAX_FRAGS);
496 		if (skb_linearize(skb)) {
497 			dev_kfree_skb_any(skb);
498 			dev_core_stats_tx_dropped_inc(dev);
499 			return NETDEV_TX_OK;
500 		}
501 	}
502 #endif
503 	free_size = bnxt_tx_avail(bp, txr);
504 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
505 		/* We must have raced with NAPI cleanup */
506 		if (net_ratelimit() && txr->kick_pending)
507 			netif_warn(bp, tx_err, dev,
508 				   "bnxt: ring busy w/ flush pending!\n");
509 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
510 					bp->tx_wake_thresh))
511 			return NETDEV_TX_BUSY;
512 	}
513 
514 	if (unlikely(ipv6_hopopt_jumbo_remove(skb)))
515 		goto tx_free;
516 
517 	length = skb->len;
518 	len = skb_headlen(skb);
519 	last_frag = skb_shinfo(skb)->nr_frags;
520 
521 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
522 
523 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
524 	tx_buf->skb = skb;
525 	tx_buf->nr_frags = last_frag;
526 
527 	vlan_tag_flags = 0;
528 	cfa_action = bnxt_xmit_get_cfa_action(skb);
529 	if (skb_vlan_tag_present(skb)) {
530 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
531 				 skb_vlan_tag_get(skb);
532 		/* Currently supports 8021Q, 8021AD vlan offloads
533 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
534 		 */
535 		if (skb->vlan_proto == htons(ETH_P_8021Q))
536 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
537 	}
538 
539 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp &&
540 	    ptp->tx_tstamp_en) {
541 		if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) {
542 			lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
543 			tx_buf->is_ts_pkt = 1;
544 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
545 		} else if (!skb_is_gso(skb)) {
546 			u16 seq_id, hdr_off;
547 
548 			if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) &&
549 			    !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) {
550 				if (vlan_tag_flags)
551 					hdr_off += VLAN_HLEN;
552 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
553 				tx_buf->is_ts_pkt = 1;
554 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
555 
556 				ptp->txts_req[txts_prod].tx_seqid = seq_id;
557 				ptp->txts_req[txts_prod].tx_hdr_off = hdr_off;
558 				tx_buf->txts_prod = txts_prod;
559 			}
560 		}
561 	}
562 	if (unlikely(skb->no_fcs))
563 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
564 
565 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
566 	    !lflags) {
567 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
568 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
569 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
570 		void __iomem *db = txr->tx_db.doorbell;
571 		void *pdata = tx_push_buf->data;
572 		u64 *end;
573 		int j, push_len;
574 
575 		/* Set COAL_NOW to be ready quickly for the next push */
576 		tx_push->tx_bd_len_flags_type =
577 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
578 					TX_BD_TYPE_LONG_TX_BD |
579 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
580 					TX_BD_FLAGS_COAL_NOW |
581 					TX_BD_FLAGS_PACKET_END |
582 					TX_BD_CNT(2));
583 
584 		if (skb->ip_summed == CHECKSUM_PARTIAL)
585 			tx_push1->tx_bd_hsize_lflags =
586 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
587 		else
588 			tx_push1->tx_bd_hsize_lflags = 0;
589 
590 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
591 		tx_push1->tx_bd_cfa_action =
592 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
593 
594 		end = pdata + length;
595 		end = PTR_ALIGN(end, 8) - 1;
596 		*end = 0;
597 
598 		skb_copy_from_linear_data(skb, pdata, len);
599 		pdata += len;
600 		for (j = 0; j < last_frag; j++) {
601 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
602 			void *fptr;
603 
604 			fptr = skb_frag_address_safe(frag);
605 			if (!fptr)
606 				goto normal_tx;
607 
608 			memcpy(pdata, fptr, skb_frag_size(frag));
609 			pdata += skb_frag_size(frag);
610 		}
611 
612 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
613 		txbd->tx_bd_haddr = txr->data_mapping;
614 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
615 		prod = NEXT_TX(prod);
616 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
617 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
618 		memcpy(txbd, tx_push1, sizeof(*txbd));
619 		prod = NEXT_TX(prod);
620 		tx_push->doorbell =
621 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
622 				    DB_RING_IDX(&txr->tx_db, prod));
623 		WRITE_ONCE(txr->tx_prod, prod);
624 
625 		tx_buf->is_push = 1;
626 		netdev_tx_sent_queue(txq, skb->len);
627 		wmb();	/* Sync is_push and byte queue before pushing data */
628 
629 		push_len = (length + sizeof(*tx_push) + 7) / 8;
630 		if (push_len > 16) {
631 			__iowrite64_copy(db, tx_push_buf, 16);
632 			__iowrite32_copy(db + 4, tx_push_buf + 1,
633 					 (push_len - 16) << 1);
634 		} else {
635 			__iowrite64_copy(db, tx_push_buf, push_len);
636 		}
637 
638 		goto tx_done;
639 	}
640 
641 normal_tx:
642 	if (length < BNXT_MIN_PKT_SIZE) {
643 		pad = BNXT_MIN_PKT_SIZE - length;
644 		if (skb_pad(skb, pad))
645 			/* SKB already freed. */
646 			goto tx_kick_pending;
647 		length = BNXT_MIN_PKT_SIZE;
648 	}
649 
650 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
651 
652 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
653 		goto tx_free;
654 
655 	dma_unmap_addr_set(tx_buf, mapping, mapping);
656 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
657 		TX_BD_CNT(last_frag + 2);
658 
659 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
660 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
661 
662 	prod = NEXT_TX(prod);
663 	txbd1 = (struct tx_bd_ext *)
664 		&txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
665 
666 	txbd1->tx_bd_hsize_lflags = lflags;
667 	if (skb_is_gso(skb)) {
668 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
669 		u32 hdr_len;
670 
671 		if (skb->encapsulation) {
672 			if (udp_gso)
673 				hdr_len = skb_inner_transport_offset(skb) +
674 					  sizeof(struct udphdr);
675 			else
676 				hdr_len = skb_inner_tcp_all_headers(skb);
677 		} else if (udp_gso) {
678 			hdr_len = skb_transport_offset(skb) +
679 				  sizeof(struct udphdr);
680 		} else {
681 			hdr_len = skb_tcp_all_headers(skb);
682 		}
683 
684 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
685 					TX_BD_FLAGS_T_IPID |
686 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
687 		length = skb_shinfo(skb)->gso_size;
688 		txbd1->tx_bd_mss = cpu_to_le32(length);
689 		length += hdr_len;
690 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
691 		txbd1->tx_bd_hsize_lflags |=
692 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
693 		txbd1->tx_bd_mss = 0;
694 	}
695 
696 	length >>= 9;
697 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
698 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
699 				     skb->len);
700 		i = 0;
701 		goto tx_dma_error;
702 	}
703 	flags |= bnxt_lhint_arr[length];
704 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
705 
706 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
707 	txbd1->tx_bd_cfa_action =
708 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
709 	txbd0 = txbd;
710 	for (i = 0; i < last_frag; i++) {
711 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
712 
713 		prod = NEXT_TX(prod);
714 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
715 
716 		len = skb_frag_size(frag);
717 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
718 					   DMA_TO_DEVICE);
719 
720 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
721 			goto tx_dma_error;
722 
723 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
724 		dma_unmap_addr_set(tx_buf, mapping, mapping);
725 
726 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
727 
728 		flags = len << TX_BD_LEN_SHIFT;
729 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
730 	}
731 
732 	flags &= ~TX_BD_LEN;
733 	txbd->tx_bd_len_flags_type =
734 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
735 			    TX_BD_FLAGS_PACKET_END);
736 
737 	netdev_tx_sent_queue(txq, skb->len);
738 
739 	skb_tx_timestamp(skb);
740 
741 	prod = NEXT_TX(prod);
742 	WRITE_ONCE(txr->tx_prod, prod);
743 
744 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
745 		bnxt_txr_db_kick(bp, txr, prod);
746 	} else {
747 		if (free_size >= bp->tx_wake_thresh)
748 			txbd0->tx_bd_len_flags_type |=
749 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
750 		txr->kick_pending = 1;
751 	}
752 
753 tx_done:
754 
755 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
756 		if (netdev_xmit_more() && !tx_buf->is_push) {
757 			txbd0->tx_bd_len_flags_type &=
758 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
759 			bnxt_txr_db_kick(bp, txr, prod);
760 		}
761 
762 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
763 				   bp->tx_wake_thresh);
764 	}
765 	return NETDEV_TX_OK;
766 
767 tx_dma_error:
768 	last_frag = i;
769 
770 	/* start back at beginning and unmap skb */
771 	prod = txr->tx_prod;
772 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
773 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
774 			 skb_headlen(skb), DMA_TO_DEVICE);
775 	prod = NEXT_TX(prod);
776 
777 	/* unmap remaining mapped pages */
778 	for (i = 0; i < last_frag; i++) {
779 		prod = NEXT_TX(prod);
780 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
781 		dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
782 			       skb_frag_size(&skb_shinfo(skb)->frags[i]),
783 			       DMA_TO_DEVICE);
784 	}
785 
786 tx_free:
787 	dev_kfree_skb_any(skb);
788 tx_kick_pending:
789 	if (BNXT_TX_PTP_IS_SET(lflags)) {
790 		txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].is_ts_pkt = 0;
791 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
792 		if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
793 			/* set SKB to err so PTP worker will clean up */
794 			ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO);
795 	}
796 	if (txr->kick_pending)
797 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
798 	txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].skb = NULL;
799 	dev_core_stats_tx_dropped_inc(dev);
800 	return NETDEV_TX_OK;
801 }
802 
803 /* Returns true if some remaining TX packets not processed. */
804 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
805 			  int budget)
806 {
807 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
808 	struct pci_dev *pdev = bp->pdev;
809 	u16 hw_cons = txr->tx_hw_cons;
810 	unsigned int tx_bytes = 0;
811 	u16 cons = txr->tx_cons;
812 	int tx_pkts = 0;
813 	bool rc = false;
814 
815 	while (RING_TX(bp, cons) != hw_cons) {
816 		struct bnxt_sw_tx_bd *tx_buf;
817 		struct sk_buff *skb;
818 		bool is_ts_pkt;
819 		int j, last;
820 
821 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
822 		skb = tx_buf->skb;
823 
824 		if (unlikely(!skb)) {
825 			bnxt_sched_reset_txr(bp, txr, cons);
826 			return rc;
827 		}
828 
829 		is_ts_pkt = tx_buf->is_ts_pkt;
830 		if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) {
831 			rc = true;
832 			break;
833 		}
834 
835 		cons = NEXT_TX(cons);
836 		tx_pkts++;
837 		tx_bytes += skb->len;
838 		tx_buf->skb = NULL;
839 		tx_buf->is_ts_pkt = 0;
840 
841 		if (tx_buf->is_push) {
842 			tx_buf->is_push = 0;
843 			goto next_tx_int;
844 		}
845 
846 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
847 				 skb_headlen(skb), DMA_TO_DEVICE);
848 		last = tx_buf->nr_frags;
849 
850 		for (j = 0; j < last; j++) {
851 			cons = NEXT_TX(cons);
852 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
853 			dma_unmap_page(
854 				&pdev->dev,
855 				dma_unmap_addr(tx_buf, mapping),
856 				skb_frag_size(&skb_shinfo(skb)->frags[j]),
857 				DMA_TO_DEVICE);
858 		}
859 		if (unlikely(is_ts_pkt)) {
860 			if (BNXT_CHIP_P5(bp)) {
861 				/* PTP worker takes ownership of the skb */
862 				bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod);
863 				skb = NULL;
864 			}
865 		}
866 
867 next_tx_int:
868 		cons = NEXT_TX(cons);
869 
870 		dev_consume_skb_any(skb);
871 	}
872 
873 	WRITE_ONCE(txr->tx_cons, cons);
874 
875 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
876 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
877 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
878 
879 	return rc;
880 }
881 
882 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
883 {
884 	struct bnxt_tx_ring_info *txr;
885 	bool more = false;
886 	int i;
887 
888 	bnxt_for_each_napi_tx(i, bnapi, txr) {
889 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
890 			more |= __bnxt_tx_int(bp, txr, budget);
891 	}
892 	if (!more)
893 		bnapi->events &= ~BNXT_TX_CMP_EVENT;
894 }
895 
896 static bool bnxt_separate_head_pool(void)
897 {
898 	return PAGE_SIZE > BNXT_RX_PAGE_SIZE;
899 }
900 
901 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
902 					 struct bnxt_rx_ring_info *rxr,
903 					 unsigned int *offset,
904 					 gfp_t gfp)
905 {
906 	struct page *page;
907 
908 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
909 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
910 						BNXT_RX_PAGE_SIZE);
911 	} else {
912 		page = page_pool_dev_alloc_pages(rxr->page_pool);
913 		*offset = 0;
914 	}
915 	if (!page)
916 		return NULL;
917 
918 	*mapping = page_pool_get_dma_addr(page) + *offset;
919 	return page;
920 }
921 
922 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
923 				       struct bnxt_rx_ring_info *rxr,
924 				       gfp_t gfp)
925 {
926 	unsigned int offset;
927 	struct page *page;
928 
929 	page = page_pool_alloc_frag(rxr->head_pool, &offset,
930 				    bp->rx_buf_size, gfp);
931 	if (!page)
932 		return NULL;
933 
934 	*mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset;
935 	return page_address(page) + offset;
936 }
937 
938 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
939 		       u16 prod, gfp_t gfp)
940 {
941 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
942 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
943 	dma_addr_t mapping;
944 
945 	if (BNXT_RX_PAGE_MODE(bp)) {
946 		unsigned int offset;
947 		struct page *page =
948 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
949 
950 		if (!page)
951 			return -ENOMEM;
952 
953 		mapping += bp->rx_dma_offset;
954 		rx_buf->data = page;
955 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
956 	} else {
957 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp);
958 
959 		if (!data)
960 			return -ENOMEM;
961 
962 		rx_buf->data = data;
963 		rx_buf->data_ptr = data + bp->rx_offset;
964 	}
965 	rx_buf->mapping = mapping;
966 
967 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
968 	return 0;
969 }
970 
971 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
972 {
973 	u16 prod = rxr->rx_prod;
974 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
975 	struct bnxt *bp = rxr->bnapi->bp;
976 	struct rx_bd *cons_bd, *prod_bd;
977 
978 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
979 	cons_rx_buf = &rxr->rx_buf_ring[cons];
980 
981 	prod_rx_buf->data = data;
982 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
983 
984 	prod_rx_buf->mapping = cons_rx_buf->mapping;
985 
986 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
987 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
988 
989 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
990 }
991 
992 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
993 {
994 	u16 next, max = rxr->rx_agg_bmap_size;
995 
996 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
997 	if (next >= max)
998 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
999 	return next;
1000 }
1001 
1002 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
1003 				     struct bnxt_rx_ring_info *rxr,
1004 				     u16 prod, gfp_t gfp)
1005 {
1006 	struct rx_bd *rxbd =
1007 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1008 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
1009 	struct page *page;
1010 	dma_addr_t mapping;
1011 	u16 sw_prod = rxr->rx_sw_agg_prod;
1012 	unsigned int offset = 0;
1013 
1014 	page = __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
1015 
1016 	if (!page)
1017 		return -ENOMEM;
1018 
1019 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1020 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1021 
1022 	__set_bit(sw_prod, rxr->rx_agg_bmap);
1023 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
1024 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1025 
1026 	rx_agg_buf->page = page;
1027 	rx_agg_buf->offset = offset;
1028 	rx_agg_buf->mapping = mapping;
1029 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1030 	rxbd->rx_bd_opaque = sw_prod;
1031 	return 0;
1032 }
1033 
1034 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1035 				       struct bnxt_cp_ring_info *cpr,
1036 				       u16 cp_cons, u16 curr)
1037 {
1038 	struct rx_agg_cmp *agg;
1039 
1040 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1041 	agg = (struct rx_agg_cmp *)
1042 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1043 	return agg;
1044 }
1045 
1046 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1047 					      struct bnxt_rx_ring_info *rxr,
1048 					      u16 agg_id, u16 curr)
1049 {
1050 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1051 
1052 	return &tpa_info->agg_arr[curr];
1053 }
1054 
1055 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1056 				   u16 start, u32 agg_bufs, bool tpa)
1057 {
1058 	struct bnxt_napi *bnapi = cpr->bnapi;
1059 	struct bnxt *bp = bnapi->bp;
1060 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1061 	u16 prod = rxr->rx_agg_prod;
1062 	u16 sw_prod = rxr->rx_sw_agg_prod;
1063 	bool p5_tpa = false;
1064 	u32 i;
1065 
1066 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1067 		p5_tpa = true;
1068 
1069 	for (i = 0; i < agg_bufs; i++) {
1070 		u16 cons;
1071 		struct rx_agg_cmp *agg;
1072 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1073 		struct rx_bd *prod_bd;
1074 		struct page *page;
1075 
1076 		if (p5_tpa)
1077 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1078 		else
1079 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1080 		cons = agg->rx_agg_cmp_opaque;
1081 		__clear_bit(cons, rxr->rx_agg_bmap);
1082 
1083 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1084 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1085 
1086 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1087 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1088 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1089 
1090 		/* It is possible for sw_prod to be equal to cons, so
1091 		 * set cons_rx_buf->page to NULL first.
1092 		 */
1093 		page = cons_rx_buf->page;
1094 		cons_rx_buf->page = NULL;
1095 		prod_rx_buf->page = page;
1096 		prod_rx_buf->offset = cons_rx_buf->offset;
1097 
1098 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1099 
1100 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1101 
1102 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1103 		prod_bd->rx_bd_opaque = sw_prod;
1104 
1105 		prod = NEXT_RX_AGG(prod);
1106 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1107 	}
1108 	rxr->rx_agg_prod = prod;
1109 	rxr->rx_sw_agg_prod = sw_prod;
1110 }
1111 
1112 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1113 					      struct bnxt_rx_ring_info *rxr,
1114 					      u16 cons, void *data, u8 *data_ptr,
1115 					      dma_addr_t dma_addr,
1116 					      unsigned int offset_and_len)
1117 {
1118 	unsigned int len = offset_and_len & 0xffff;
1119 	struct page *page = data;
1120 	u16 prod = rxr->rx_prod;
1121 	struct sk_buff *skb;
1122 	int err;
1123 
1124 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1125 	if (unlikely(err)) {
1126 		bnxt_reuse_rx_data(rxr, cons, data);
1127 		return NULL;
1128 	}
1129 	dma_addr -= bp->rx_dma_offset;
1130 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1131 				bp->rx_dir);
1132 	skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE);
1133 	if (!skb) {
1134 		page_pool_recycle_direct(rxr->page_pool, page);
1135 		return NULL;
1136 	}
1137 	skb_mark_for_recycle(skb);
1138 	skb_reserve(skb, bp->rx_offset);
1139 	__skb_put(skb, len);
1140 
1141 	return skb;
1142 }
1143 
1144 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1145 					struct bnxt_rx_ring_info *rxr,
1146 					u16 cons, void *data, u8 *data_ptr,
1147 					dma_addr_t dma_addr,
1148 					unsigned int offset_and_len)
1149 {
1150 	unsigned int payload = offset_and_len >> 16;
1151 	unsigned int len = offset_and_len & 0xffff;
1152 	skb_frag_t *frag;
1153 	struct page *page = data;
1154 	u16 prod = rxr->rx_prod;
1155 	struct sk_buff *skb;
1156 	int off, err;
1157 
1158 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1159 	if (unlikely(err)) {
1160 		bnxt_reuse_rx_data(rxr, cons, data);
1161 		return NULL;
1162 	}
1163 	dma_addr -= bp->rx_dma_offset;
1164 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1165 				bp->rx_dir);
1166 
1167 	if (unlikely(!payload))
1168 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1169 
1170 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1171 	if (!skb) {
1172 		page_pool_recycle_direct(rxr->page_pool, page);
1173 		return NULL;
1174 	}
1175 
1176 	skb_mark_for_recycle(skb);
1177 	off = (void *)data_ptr - page_address(page);
1178 	skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE);
1179 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1180 	       payload + NET_IP_ALIGN);
1181 
1182 	frag = &skb_shinfo(skb)->frags[0];
1183 	skb_frag_size_sub(frag, payload);
1184 	skb_frag_off_add(frag, payload);
1185 	skb->data_len -= payload;
1186 	skb->tail += payload;
1187 
1188 	return skb;
1189 }
1190 
1191 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1192 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1193 				   void *data, u8 *data_ptr,
1194 				   dma_addr_t dma_addr,
1195 				   unsigned int offset_and_len)
1196 {
1197 	u16 prod = rxr->rx_prod;
1198 	struct sk_buff *skb;
1199 	int err;
1200 
1201 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1202 	if (unlikely(err)) {
1203 		bnxt_reuse_rx_data(rxr, cons, data);
1204 		return NULL;
1205 	}
1206 
1207 	skb = napi_build_skb(data, bp->rx_buf_size);
1208 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1209 				bp->rx_dir);
1210 	if (!skb) {
1211 		page_pool_free_va(rxr->head_pool, data, true);
1212 		return NULL;
1213 	}
1214 
1215 	skb_mark_for_recycle(skb);
1216 	skb_reserve(skb, bp->rx_offset);
1217 	skb_put(skb, offset_and_len & 0xffff);
1218 	return skb;
1219 }
1220 
1221 static u32 __bnxt_rx_agg_pages(struct bnxt *bp,
1222 			       struct bnxt_cp_ring_info *cpr,
1223 			       struct skb_shared_info *shinfo,
1224 			       u16 idx, u32 agg_bufs, bool tpa,
1225 			       struct xdp_buff *xdp)
1226 {
1227 	struct bnxt_napi *bnapi = cpr->bnapi;
1228 	struct pci_dev *pdev = bp->pdev;
1229 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1230 	u16 prod = rxr->rx_agg_prod;
1231 	u32 i, total_frag_len = 0;
1232 	bool p5_tpa = false;
1233 
1234 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1235 		p5_tpa = true;
1236 
1237 	for (i = 0; i < agg_bufs; i++) {
1238 		skb_frag_t *frag = &shinfo->frags[i];
1239 		u16 cons, frag_len;
1240 		struct rx_agg_cmp *agg;
1241 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1242 		struct page *page;
1243 		dma_addr_t mapping;
1244 
1245 		if (p5_tpa)
1246 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1247 		else
1248 			agg = bnxt_get_agg(bp, cpr, idx, i);
1249 		cons = agg->rx_agg_cmp_opaque;
1250 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1251 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1252 
1253 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1254 		skb_frag_fill_page_desc(frag, cons_rx_buf->page,
1255 					cons_rx_buf->offset, frag_len);
1256 		shinfo->nr_frags = i + 1;
1257 		__clear_bit(cons, rxr->rx_agg_bmap);
1258 
1259 		/* It is possible for bnxt_alloc_rx_page() to allocate
1260 		 * a sw_prod index that equals the cons index, so we
1261 		 * need to clear the cons entry now.
1262 		 */
1263 		mapping = cons_rx_buf->mapping;
1264 		page = cons_rx_buf->page;
1265 		cons_rx_buf->page = NULL;
1266 
1267 		if (xdp && page_is_pfmemalloc(page))
1268 			xdp_buff_set_frag_pfmemalloc(xdp);
1269 
1270 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1271 			--shinfo->nr_frags;
1272 			cons_rx_buf->page = page;
1273 
1274 			/* Update prod since possibly some pages have been
1275 			 * allocated already.
1276 			 */
1277 			rxr->rx_agg_prod = prod;
1278 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1279 			return 0;
1280 		}
1281 
1282 		dma_sync_single_for_cpu(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1283 					bp->rx_dir);
1284 
1285 		total_frag_len += frag_len;
1286 		prod = NEXT_RX_AGG(prod);
1287 	}
1288 	rxr->rx_agg_prod = prod;
1289 	return total_frag_len;
1290 }
1291 
1292 static struct sk_buff *bnxt_rx_agg_pages_skb(struct bnxt *bp,
1293 					     struct bnxt_cp_ring_info *cpr,
1294 					     struct sk_buff *skb, u16 idx,
1295 					     u32 agg_bufs, bool tpa)
1296 {
1297 	struct skb_shared_info *shinfo = skb_shinfo(skb);
1298 	u32 total_frag_len = 0;
1299 
1300 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo, idx,
1301 					     agg_bufs, tpa, NULL);
1302 	if (!total_frag_len) {
1303 		skb_mark_for_recycle(skb);
1304 		dev_kfree_skb(skb);
1305 		return NULL;
1306 	}
1307 
1308 	skb->data_len += total_frag_len;
1309 	skb->len += total_frag_len;
1310 	skb->truesize += BNXT_RX_PAGE_SIZE * agg_bufs;
1311 	return skb;
1312 }
1313 
1314 static u32 bnxt_rx_agg_pages_xdp(struct bnxt *bp,
1315 				 struct bnxt_cp_ring_info *cpr,
1316 				 struct xdp_buff *xdp, u16 idx,
1317 				 u32 agg_bufs, bool tpa)
1318 {
1319 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1320 	u32 total_frag_len = 0;
1321 
1322 	if (!xdp_buff_has_frags(xdp))
1323 		shinfo->nr_frags = 0;
1324 
1325 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo,
1326 					     idx, agg_bufs, tpa, xdp);
1327 	if (total_frag_len) {
1328 		xdp_buff_set_frags_flag(xdp);
1329 		shinfo->nr_frags = agg_bufs;
1330 		shinfo->xdp_frags_size = total_frag_len;
1331 	}
1332 	return total_frag_len;
1333 }
1334 
1335 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1336 			       u8 agg_bufs, u32 *raw_cons)
1337 {
1338 	u16 last;
1339 	struct rx_agg_cmp *agg;
1340 
1341 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1342 	last = RING_CMP(*raw_cons);
1343 	agg = (struct rx_agg_cmp *)
1344 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1345 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1346 }
1347 
1348 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1349 				      unsigned int len,
1350 				      dma_addr_t mapping)
1351 {
1352 	struct bnxt *bp = bnapi->bp;
1353 	struct pci_dev *pdev = bp->pdev;
1354 	struct sk_buff *skb;
1355 
1356 	skb = napi_alloc_skb(&bnapi->napi, len);
1357 	if (!skb)
1358 		return NULL;
1359 
1360 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copybreak,
1361 				bp->rx_dir);
1362 
1363 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1364 	       len + NET_IP_ALIGN);
1365 
1366 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copybreak,
1367 				   bp->rx_dir);
1368 
1369 	skb_put(skb, len);
1370 
1371 	return skb;
1372 }
1373 
1374 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1375 				     unsigned int len,
1376 				     dma_addr_t mapping)
1377 {
1378 	return bnxt_copy_data(bnapi, data, len, mapping);
1379 }
1380 
1381 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1382 				     struct xdp_buff *xdp,
1383 				     unsigned int len,
1384 				     dma_addr_t mapping)
1385 {
1386 	unsigned int metasize = 0;
1387 	u8 *data = xdp->data;
1388 	struct sk_buff *skb;
1389 
1390 	len = xdp->data_end - xdp->data_meta;
1391 	metasize = xdp->data - xdp->data_meta;
1392 	data = xdp->data_meta;
1393 
1394 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1395 	if (!skb)
1396 		return skb;
1397 
1398 	if (metasize) {
1399 		skb_metadata_set(skb, metasize);
1400 		__skb_pull(skb, metasize);
1401 	}
1402 
1403 	return skb;
1404 }
1405 
1406 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1407 			   u32 *raw_cons, void *cmp)
1408 {
1409 	struct rx_cmp *rxcmp = cmp;
1410 	u32 tmp_raw_cons = *raw_cons;
1411 	u8 cmp_type, agg_bufs = 0;
1412 
1413 	cmp_type = RX_CMP_TYPE(rxcmp);
1414 
1415 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1416 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1417 			    RX_CMP_AGG_BUFS) >>
1418 			   RX_CMP_AGG_BUFS_SHIFT;
1419 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1420 		struct rx_tpa_end_cmp *tpa_end = cmp;
1421 
1422 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1423 			return 0;
1424 
1425 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1426 	}
1427 
1428 	if (agg_bufs) {
1429 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1430 			return -EBUSY;
1431 	}
1432 	*raw_cons = tmp_raw_cons;
1433 	return 0;
1434 }
1435 
1436 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1437 {
1438 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1439 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1440 
1441 	if (test_bit(idx, map->agg_idx_bmap))
1442 		idx = find_first_zero_bit(map->agg_idx_bmap,
1443 					  BNXT_AGG_IDX_BMAP_SIZE);
1444 	__set_bit(idx, map->agg_idx_bmap);
1445 	map->agg_id_tbl[agg_id] = idx;
1446 	return idx;
1447 }
1448 
1449 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1450 {
1451 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1452 
1453 	__clear_bit(idx, map->agg_idx_bmap);
1454 }
1455 
1456 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1457 {
1458 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1459 
1460 	return map->agg_id_tbl[agg_id];
1461 }
1462 
1463 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1464 			      struct rx_tpa_start_cmp *tpa_start,
1465 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1466 {
1467 	tpa_info->cfa_code_valid = 1;
1468 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1469 	tpa_info->vlan_valid = 0;
1470 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1471 		tpa_info->vlan_valid = 1;
1472 		tpa_info->metadata =
1473 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1474 	}
1475 }
1476 
1477 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1478 				 struct rx_tpa_start_cmp *tpa_start,
1479 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1480 {
1481 	tpa_info->vlan_valid = 0;
1482 	if (TPA_START_VLAN_VALID(tpa_start)) {
1483 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1484 		u32 vlan_proto = ETH_P_8021Q;
1485 
1486 		tpa_info->vlan_valid = 1;
1487 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1488 			vlan_proto = ETH_P_8021AD;
1489 		tpa_info->metadata = vlan_proto << 16 |
1490 				     TPA_START_METADATA0_TCI(tpa_start1);
1491 	}
1492 }
1493 
1494 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1495 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1496 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1497 {
1498 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1499 	struct bnxt_tpa_info *tpa_info;
1500 	u16 cons, prod, agg_id;
1501 	struct rx_bd *prod_bd;
1502 	dma_addr_t mapping;
1503 
1504 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1505 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1506 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1507 	} else {
1508 		agg_id = TPA_START_AGG_ID(tpa_start);
1509 	}
1510 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1511 	prod = rxr->rx_prod;
1512 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1513 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1514 	tpa_info = &rxr->rx_tpa[agg_id];
1515 
1516 	if (unlikely(cons != rxr->rx_next_cons ||
1517 		     TPA_START_ERROR(tpa_start))) {
1518 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1519 			    cons, rxr->rx_next_cons,
1520 			    TPA_START_ERROR_CODE(tpa_start1));
1521 		bnxt_sched_reset_rxr(bp, rxr);
1522 		return;
1523 	}
1524 	prod_rx_buf->data = tpa_info->data;
1525 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1526 
1527 	mapping = tpa_info->mapping;
1528 	prod_rx_buf->mapping = mapping;
1529 
1530 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1531 
1532 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1533 
1534 	tpa_info->data = cons_rx_buf->data;
1535 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1536 	cons_rx_buf->data = NULL;
1537 	tpa_info->mapping = cons_rx_buf->mapping;
1538 
1539 	tpa_info->len =
1540 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1541 				RX_TPA_START_CMP_LEN_SHIFT;
1542 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1543 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1544 		tpa_info->gso_type = SKB_GSO_TCPV4;
1545 		if (TPA_START_IS_IPV6(tpa_start1))
1546 			tpa_info->gso_type = SKB_GSO_TCPV6;
1547 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1548 		else if (!BNXT_CHIP_P4_PLUS(bp) &&
1549 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1550 			tpa_info->gso_type = SKB_GSO_TCPV6;
1551 		tpa_info->rss_hash =
1552 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1553 	} else {
1554 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1555 		tpa_info->gso_type = 0;
1556 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1557 	}
1558 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1559 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1560 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1561 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1562 	else
1563 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1564 	tpa_info->agg_count = 0;
1565 
1566 	rxr->rx_prod = NEXT_RX(prod);
1567 	cons = RING_RX(bp, NEXT_RX(cons));
1568 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1569 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1570 
1571 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1572 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1573 	cons_rx_buf->data = NULL;
1574 }
1575 
1576 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1577 {
1578 	if (agg_bufs)
1579 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1580 }
1581 
1582 #ifdef CONFIG_INET
1583 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1584 {
1585 	struct udphdr *uh = NULL;
1586 
1587 	if (ip_proto == htons(ETH_P_IP)) {
1588 		struct iphdr *iph = (struct iphdr *)skb->data;
1589 
1590 		if (iph->protocol == IPPROTO_UDP)
1591 			uh = (struct udphdr *)(iph + 1);
1592 	} else {
1593 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1594 
1595 		if (iph->nexthdr == IPPROTO_UDP)
1596 			uh = (struct udphdr *)(iph + 1);
1597 	}
1598 	if (uh) {
1599 		if (uh->check)
1600 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1601 		else
1602 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1603 	}
1604 }
1605 #endif
1606 
1607 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1608 					   int payload_off, int tcp_ts,
1609 					   struct sk_buff *skb)
1610 {
1611 #ifdef CONFIG_INET
1612 	struct tcphdr *th;
1613 	int len, nw_off;
1614 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1615 	u32 hdr_info = tpa_info->hdr_info;
1616 	bool loopback = false;
1617 
1618 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1619 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1620 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1621 
1622 	/* If the packet is an internal loopback packet, the offsets will
1623 	 * have an extra 4 bytes.
1624 	 */
1625 	if (inner_mac_off == 4) {
1626 		loopback = true;
1627 	} else if (inner_mac_off > 4) {
1628 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1629 					    ETH_HLEN - 2));
1630 
1631 		/* We only support inner iPv4/ipv6.  If we don't see the
1632 		 * correct protocol ID, it must be a loopback packet where
1633 		 * the offsets are off by 4.
1634 		 */
1635 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1636 			loopback = true;
1637 	}
1638 	if (loopback) {
1639 		/* internal loopback packet, subtract all offsets by 4 */
1640 		inner_ip_off -= 4;
1641 		inner_mac_off -= 4;
1642 		outer_ip_off -= 4;
1643 	}
1644 
1645 	nw_off = inner_ip_off - ETH_HLEN;
1646 	skb_set_network_header(skb, nw_off);
1647 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1648 		struct ipv6hdr *iph = ipv6_hdr(skb);
1649 
1650 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1651 		len = skb->len - skb_transport_offset(skb);
1652 		th = tcp_hdr(skb);
1653 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1654 	} else {
1655 		struct iphdr *iph = ip_hdr(skb);
1656 
1657 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1658 		len = skb->len - skb_transport_offset(skb);
1659 		th = tcp_hdr(skb);
1660 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1661 	}
1662 
1663 	if (inner_mac_off) { /* tunnel */
1664 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1665 					    ETH_HLEN - 2));
1666 
1667 		bnxt_gro_tunnel(skb, proto);
1668 	}
1669 #endif
1670 	return skb;
1671 }
1672 
1673 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1674 					   int payload_off, int tcp_ts,
1675 					   struct sk_buff *skb)
1676 {
1677 #ifdef CONFIG_INET
1678 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1679 	u32 hdr_info = tpa_info->hdr_info;
1680 	int iphdr_len, nw_off;
1681 
1682 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1683 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1684 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1685 
1686 	nw_off = inner_ip_off - ETH_HLEN;
1687 	skb_set_network_header(skb, nw_off);
1688 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1689 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1690 	skb_set_transport_header(skb, nw_off + iphdr_len);
1691 
1692 	if (inner_mac_off) { /* tunnel */
1693 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1694 					    ETH_HLEN - 2));
1695 
1696 		bnxt_gro_tunnel(skb, proto);
1697 	}
1698 #endif
1699 	return skb;
1700 }
1701 
1702 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1703 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1704 
1705 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1706 					   int payload_off, int tcp_ts,
1707 					   struct sk_buff *skb)
1708 {
1709 #ifdef CONFIG_INET
1710 	struct tcphdr *th;
1711 	int len, nw_off, tcp_opt_len = 0;
1712 
1713 	if (tcp_ts)
1714 		tcp_opt_len = 12;
1715 
1716 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1717 		struct iphdr *iph;
1718 
1719 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1720 			 ETH_HLEN;
1721 		skb_set_network_header(skb, nw_off);
1722 		iph = ip_hdr(skb);
1723 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1724 		len = skb->len - skb_transport_offset(skb);
1725 		th = tcp_hdr(skb);
1726 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1727 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1728 		struct ipv6hdr *iph;
1729 
1730 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1731 			 ETH_HLEN;
1732 		skb_set_network_header(skb, nw_off);
1733 		iph = ipv6_hdr(skb);
1734 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1735 		len = skb->len - skb_transport_offset(skb);
1736 		th = tcp_hdr(skb);
1737 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1738 	} else {
1739 		dev_kfree_skb_any(skb);
1740 		return NULL;
1741 	}
1742 
1743 	if (nw_off) /* tunnel */
1744 		bnxt_gro_tunnel(skb, skb->protocol);
1745 #endif
1746 	return skb;
1747 }
1748 
1749 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1750 					   struct bnxt_tpa_info *tpa_info,
1751 					   struct rx_tpa_end_cmp *tpa_end,
1752 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1753 					   struct sk_buff *skb)
1754 {
1755 #ifdef CONFIG_INET
1756 	int payload_off;
1757 	u16 segs;
1758 
1759 	segs = TPA_END_TPA_SEGS(tpa_end);
1760 	if (segs == 1)
1761 		return skb;
1762 
1763 	NAPI_GRO_CB(skb)->count = segs;
1764 	skb_shinfo(skb)->gso_size =
1765 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1766 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1767 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1768 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1769 	else
1770 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1771 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1772 	if (likely(skb))
1773 		tcp_gro_complete(skb);
1774 #endif
1775 	return skb;
1776 }
1777 
1778 /* Given the cfa_code of a received packet determine which
1779  * netdev (vf-rep or PF) the packet is destined to.
1780  */
1781 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1782 {
1783 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1784 
1785 	/* if vf-rep dev is NULL, the must belongs to the PF */
1786 	return dev ? dev : bp->dev;
1787 }
1788 
1789 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1790 					   struct bnxt_cp_ring_info *cpr,
1791 					   u32 *raw_cons,
1792 					   struct rx_tpa_end_cmp *tpa_end,
1793 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1794 					   u8 *event)
1795 {
1796 	struct bnxt_napi *bnapi = cpr->bnapi;
1797 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1798 	struct net_device *dev = bp->dev;
1799 	u8 *data_ptr, agg_bufs;
1800 	unsigned int len;
1801 	struct bnxt_tpa_info *tpa_info;
1802 	dma_addr_t mapping;
1803 	struct sk_buff *skb;
1804 	u16 idx = 0, agg_id;
1805 	void *data;
1806 	bool gro;
1807 
1808 	if (unlikely(bnapi->in_reset)) {
1809 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1810 
1811 		if (rc < 0)
1812 			return ERR_PTR(-EBUSY);
1813 		return NULL;
1814 	}
1815 
1816 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1817 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1818 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1819 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1820 		tpa_info = &rxr->rx_tpa[agg_id];
1821 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1822 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1823 				    agg_bufs, tpa_info->agg_count);
1824 			agg_bufs = tpa_info->agg_count;
1825 		}
1826 		tpa_info->agg_count = 0;
1827 		*event |= BNXT_AGG_EVENT;
1828 		bnxt_free_agg_idx(rxr, agg_id);
1829 		idx = agg_id;
1830 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1831 	} else {
1832 		agg_id = TPA_END_AGG_ID(tpa_end);
1833 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1834 		tpa_info = &rxr->rx_tpa[agg_id];
1835 		idx = RING_CMP(*raw_cons);
1836 		if (agg_bufs) {
1837 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1838 				return ERR_PTR(-EBUSY);
1839 
1840 			*event |= BNXT_AGG_EVENT;
1841 			idx = NEXT_CMP(idx);
1842 		}
1843 		gro = !!TPA_END_GRO(tpa_end);
1844 	}
1845 	data = tpa_info->data;
1846 	data_ptr = tpa_info->data_ptr;
1847 	prefetch(data_ptr);
1848 	len = tpa_info->len;
1849 	mapping = tpa_info->mapping;
1850 
1851 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1852 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1853 		if (agg_bufs > MAX_SKB_FRAGS)
1854 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1855 				    agg_bufs, (int)MAX_SKB_FRAGS);
1856 		return NULL;
1857 	}
1858 
1859 	if (len <= bp->rx_copybreak) {
1860 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1861 		if (!skb) {
1862 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1863 			cpr->sw_stats->rx.rx_oom_discards += 1;
1864 			return NULL;
1865 		}
1866 	} else {
1867 		u8 *new_data;
1868 		dma_addr_t new_mapping;
1869 
1870 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr,
1871 						GFP_ATOMIC);
1872 		if (!new_data) {
1873 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1874 			cpr->sw_stats->rx.rx_oom_discards += 1;
1875 			return NULL;
1876 		}
1877 
1878 		tpa_info->data = new_data;
1879 		tpa_info->data_ptr = new_data + bp->rx_offset;
1880 		tpa_info->mapping = new_mapping;
1881 
1882 		skb = napi_build_skb(data, bp->rx_buf_size);
1883 		dma_sync_single_for_cpu(&bp->pdev->dev, mapping,
1884 					bp->rx_buf_use_size, bp->rx_dir);
1885 
1886 		if (!skb) {
1887 			page_pool_free_va(rxr->head_pool, data, true);
1888 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1889 			cpr->sw_stats->rx.rx_oom_discards += 1;
1890 			return NULL;
1891 		}
1892 		skb_mark_for_recycle(skb);
1893 		skb_reserve(skb, bp->rx_offset);
1894 		skb_put(skb, len);
1895 	}
1896 
1897 	if (agg_bufs) {
1898 		skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, idx, agg_bufs, true);
1899 		if (!skb) {
1900 			/* Page reuse already handled by bnxt_rx_pages(). */
1901 			cpr->sw_stats->rx.rx_oom_discards += 1;
1902 			return NULL;
1903 		}
1904 	}
1905 
1906 	if (tpa_info->cfa_code_valid)
1907 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1908 	skb->protocol = eth_type_trans(skb, dev);
1909 
1910 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1911 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1912 
1913 	if (tpa_info->vlan_valid &&
1914 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1915 		__be16 vlan_proto = htons(tpa_info->metadata >>
1916 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1917 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1918 
1919 		if (eth_type_vlan(vlan_proto)) {
1920 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1921 		} else {
1922 			dev_kfree_skb(skb);
1923 			return NULL;
1924 		}
1925 	}
1926 
1927 	skb_checksum_none_assert(skb);
1928 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1929 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1930 		skb->csum_level =
1931 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1932 	}
1933 
1934 	if (gro)
1935 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1936 
1937 	return skb;
1938 }
1939 
1940 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1941 			 struct rx_agg_cmp *rx_agg)
1942 {
1943 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1944 	struct bnxt_tpa_info *tpa_info;
1945 
1946 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1947 	tpa_info = &rxr->rx_tpa[agg_id];
1948 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1949 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1950 }
1951 
1952 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1953 			     struct sk_buff *skb)
1954 {
1955 	skb_mark_for_recycle(skb);
1956 
1957 	if (skb->dev != bp->dev) {
1958 		/* this packet belongs to a vf-rep */
1959 		bnxt_vf_rep_rx(bp, skb);
1960 		return;
1961 	}
1962 	skb_record_rx_queue(skb, bnapi->index);
1963 	napi_gro_receive(&bnapi->napi, skb);
1964 }
1965 
1966 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
1967 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
1968 {
1969 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
1970 
1971 	if (BNXT_PTP_RX_TS_VALID(flags))
1972 		goto ts_valid;
1973 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
1974 		return false;
1975 
1976 ts_valid:
1977 	*cmpl_ts = ts;
1978 	return true;
1979 }
1980 
1981 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
1982 				    struct rx_cmp *rxcmp,
1983 				    struct rx_cmp_ext *rxcmp1)
1984 {
1985 	__be16 vlan_proto;
1986 	u16 vtag;
1987 
1988 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1989 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
1990 		u32 meta_data;
1991 
1992 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
1993 			return skb;
1994 
1995 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1996 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1997 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
1998 		if (eth_type_vlan(vlan_proto))
1999 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2000 		else
2001 			goto vlan_err;
2002 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2003 		if (RX_CMP_VLAN_VALID(rxcmp)) {
2004 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
2005 
2006 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
2007 				vlan_proto = htons(ETH_P_8021Q);
2008 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
2009 				vlan_proto = htons(ETH_P_8021AD);
2010 			else
2011 				goto vlan_err;
2012 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
2013 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2014 		}
2015 	}
2016 	return skb;
2017 vlan_err:
2018 	dev_kfree_skb(skb);
2019 	return NULL;
2020 }
2021 
2022 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp,
2023 					   struct rx_cmp *rxcmp)
2024 {
2025 	u8 ext_op;
2026 
2027 	ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp);
2028 	switch (ext_op) {
2029 	case EXT_OP_INNER_4:
2030 	case EXT_OP_OUTER_4:
2031 	case EXT_OP_INNFL_3:
2032 	case EXT_OP_OUTFL_3:
2033 		return PKT_HASH_TYPE_L4;
2034 	default:
2035 		return PKT_HASH_TYPE_L3;
2036 	}
2037 }
2038 
2039 /* returns the following:
2040  * 1       - 1 packet successfully received
2041  * 0       - successful TPA_START, packet not completed yet
2042  * -EBUSY  - completion ring does not have all the agg buffers yet
2043  * -ENOMEM - packet aborted due to out of memory
2044  * -EIO    - packet aborted due to hw error indicated in BD
2045  */
2046 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2047 		       u32 *raw_cons, u8 *event)
2048 {
2049 	struct bnxt_napi *bnapi = cpr->bnapi;
2050 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2051 	struct net_device *dev = bp->dev;
2052 	struct rx_cmp *rxcmp;
2053 	struct rx_cmp_ext *rxcmp1;
2054 	u32 tmp_raw_cons = *raw_cons;
2055 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2056 	struct skb_shared_info *sinfo;
2057 	struct bnxt_sw_rx_bd *rx_buf;
2058 	unsigned int len;
2059 	u8 *data_ptr, agg_bufs, cmp_type;
2060 	bool xdp_active = false;
2061 	dma_addr_t dma_addr;
2062 	struct sk_buff *skb;
2063 	struct xdp_buff xdp;
2064 	u32 flags, misc;
2065 	u32 cmpl_ts;
2066 	void *data;
2067 	int rc = 0;
2068 
2069 	rxcmp = (struct rx_cmp *)
2070 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2071 
2072 	cmp_type = RX_CMP_TYPE(rxcmp);
2073 
2074 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2075 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2076 		goto next_rx_no_prod_no_len;
2077 	}
2078 
2079 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2080 	cp_cons = RING_CMP(tmp_raw_cons);
2081 	rxcmp1 = (struct rx_cmp_ext *)
2082 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2083 
2084 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2085 		return -EBUSY;
2086 
2087 	/* The valid test of the entry must be done first before
2088 	 * reading any further.
2089 	 */
2090 	dma_rmb();
2091 	prod = rxr->rx_prod;
2092 
2093 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2094 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2095 		bnxt_tpa_start(bp, rxr, cmp_type,
2096 			       (struct rx_tpa_start_cmp *)rxcmp,
2097 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2098 
2099 		*event |= BNXT_RX_EVENT;
2100 		goto next_rx_no_prod_no_len;
2101 
2102 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2103 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2104 				   (struct rx_tpa_end_cmp *)rxcmp,
2105 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2106 
2107 		if (IS_ERR(skb))
2108 			return -EBUSY;
2109 
2110 		rc = -ENOMEM;
2111 		if (likely(skb)) {
2112 			bnxt_deliver_skb(bp, bnapi, skb);
2113 			rc = 1;
2114 		}
2115 		*event |= BNXT_RX_EVENT;
2116 		goto next_rx_no_prod_no_len;
2117 	}
2118 
2119 	cons = rxcmp->rx_cmp_opaque;
2120 	if (unlikely(cons != rxr->rx_next_cons)) {
2121 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2122 
2123 		/* 0xffff is forced error, don't print it */
2124 		if (rxr->rx_next_cons != 0xffff)
2125 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2126 				    cons, rxr->rx_next_cons);
2127 		bnxt_sched_reset_rxr(bp, rxr);
2128 		if (rc1)
2129 			return rc1;
2130 		goto next_rx_no_prod_no_len;
2131 	}
2132 	rx_buf = &rxr->rx_buf_ring[cons];
2133 	data = rx_buf->data;
2134 	data_ptr = rx_buf->data_ptr;
2135 	prefetch(data_ptr);
2136 
2137 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2138 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2139 
2140 	if (agg_bufs) {
2141 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2142 			return -EBUSY;
2143 
2144 		cp_cons = NEXT_CMP(cp_cons);
2145 		*event |= BNXT_AGG_EVENT;
2146 	}
2147 	*event |= BNXT_RX_EVENT;
2148 
2149 	rx_buf->data = NULL;
2150 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2151 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2152 
2153 		bnxt_reuse_rx_data(rxr, cons, data);
2154 		if (agg_bufs)
2155 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2156 					       false);
2157 
2158 		rc = -EIO;
2159 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2160 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2161 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2162 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2163 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2164 						 rx_err);
2165 				bnxt_sched_reset_rxr(bp, rxr);
2166 			}
2167 		}
2168 		goto next_rx_no_len;
2169 	}
2170 
2171 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2172 	len = flags >> RX_CMP_LEN_SHIFT;
2173 	dma_addr = rx_buf->mapping;
2174 
2175 	if (bnxt_xdp_attached(bp, rxr)) {
2176 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2177 		if (agg_bufs) {
2178 			u32 frag_len = bnxt_rx_agg_pages_xdp(bp, cpr, &xdp,
2179 							     cp_cons, agg_bufs,
2180 							     false);
2181 			if (!frag_len)
2182 				goto oom_next_rx;
2183 
2184 		}
2185 		xdp_active = true;
2186 	}
2187 
2188 	if (xdp_active) {
2189 		if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) {
2190 			rc = 1;
2191 			goto next_rx;
2192 		}
2193 		if (xdp_buff_has_frags(&xdp)) {
2194 			sinfo = xdp_get_shared_info_from_buff(&xdp);
2195 			agg_bufs = sinfo->nr_frags;
2196 		} else {
2197 			agg_bufs = 0;
2198 		}
2199 	}
2200 
2201 	if (len <= bp->rx_copybreak) {
2202 		if (!xdp_active)
2203 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2204 		else
2205 			skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr);
2206 		bnxt_reuse_rx_data(rxr, cons, data);
2207 		if (!skb) {
2208 			if (agg_bufs) {
2209 				if (!xdp_active)
2210 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2211 							       agg_bufs, false);
2212 				else
2213 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2214 			}
2215 			goto oom_next_rx;
2216 		}
2217 	} else {
2218 		u32 payload;
2219 
2220 		if (rx_buf->data_ptr == data_ptr)
2221 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2222 		else
2223 			payload = 0;
2224 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2225 				      payload | len);
2226 		if (!skb)
2227 			goto oom_next_rx;
2228 	}
2229 
2230 	if (agg_bufs) {
2231 		if (!xdp_active) {
2232 			skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, cp_cons, agg_bufs, false);
2233 			if (!skb)
2234 				goto oom_next_rx;
2235 		} else {
2236 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs,
2237 						 rxr->page_pool, &xdp);
2238 			if (!skb) {
2239 				/* we should be able to free the old skb here */
2240 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2241 				goto oom_next_rx;
2242 			}
2243 		}
2244 	}
2245 
2246 	if (RX_CMP_HASH_VALID(rxcmp)) {
2247 		enum pkt_hash_types type;
2248 
2249 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2250 			type = bnxt_rss_ext_op(bp, rxcmp);
2251 		} else {
2252 			u32 itypes = RX_CMP_ITYPES(rxcmp);
2253 
2254 			if (itypes == RX_CMP_FLAGS_ITYPE_TCP ||
2255 			    itypes == RX_CMP_FLAGS_ITYPE_UDP)
2256 				type = PKT_HASH_TYPE_L4;
2257 			else
2258 				type = PKT_HASH_TYPE_L3;
2259 		}
2260 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2261 	}
2262 
2263 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2264 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2265 	skb->protocol = eth_type_trans(skb, dev);
2266 
2267 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2268 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2269 		if (!skb)
2270 			goto next_rx;
2271 	}
2272 
2273 	skb_checksum_none_assert(skb);
2274 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2275 		if (dev->features & NETIF_F_RXCSUM) {
2276 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2277 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2278 		}
2279 	} else {
2280 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2281 			if (dev->features & NETIF_F_RXCSUM)
2282 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2283 		}
2284 	}
2285 
2286 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2287 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2288 			u64 ns, ts;
2289 
2290 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2291 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2292 
2293 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2294 				memset(skb_hwtstamps(skb), 0,
2295 				       sizeof(*skb_hwtstamps(skb)));
2296 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2297 			}
2298 		}
2299 	}
2300 	bnxt_deliver_skb(bp, bnapi, skb);
2301 	rc = 1;
2302 
2303 next_rx:
2304 	cpr->rx_packets += 1;
2305 	cpr->rx_bytes += len;
2306 
2307 next_rx_no_len:
2308 	rxr->rx_prod = NEXT_RX(prod);
2309 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2310 
2311 next_rx_no_prod_no_len:
2312 	*raw_cons = tmp_raw_cons;
2313 
2314 	return rc;
2315 
2316 oom_next_rx:
2317 	cpr->sw_stats->rx.rx_oom_discards += 1;
2318 	rc = -ENOMEM;
2319 	goto next_rx;
2320 }
2321 
2322 /* In netpoll mode, if we are using a combined completion ring, we need to
2323  * discard the rx packets and recycle the buffers.
2324  */
2325 static int bnxt_force_rx_discard(struct bnxt *bp,
2326 				 struct bnxt_cp_ring_info *cpr,
2327 				 u32 *raw_cons, u8 *event)
2328 {
2329 	u32 tmp_raw_cons = *raw_cons;
2330 	struct rx_cmp_ext *rxcmp1;
2331 	struct rx_cmp *rxcmp;
2332 	u16 cp_cons;
2333 	u8 cmp_type;
2334 	int rc;
2335 
2336 	cp_cons = RING_CMP(tmp_raw_cons);
2337 	rxcmp = (struct rx_cmp *)
2338 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2339 
2340 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2341 	cp_cons = RING_CMP(tmp_raw_cons);
2342 	rxcmp1 = (struct rx_cmp_ext *)
2343 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2344 
2345 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2346 		return -EBUSY;
2347 
2348 	/* The valid test of the entry must be done first before
2349 	 * reading any further.
2350 	 */
2351 	dma_rmb();
2352 	cmp_type = RX_CMP_TYPE(rxcmp);
2353 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2354 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2355 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2356 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2357 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2358 		struct rx_tpa_end_cmp_ext *tpa_end1;
2359 
2360 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2361 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2362 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2363 	}
2364 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2365 	if (rc && rc != -EBUSY)
2366 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2367 	return rc;
2368 }
2369 
2370 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2371 {
2372 	struct bnxt_fw_health *fw_health = bp->fw_health;
2373 	u32 reg = fw_health->regs[reg_idx];
2374 	u32 reg_type, reg_off, val = 0;
2375 
2376 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2377 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2378 	switch (reg_type) {
2379 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2380 		pci_read_config_dword(bp->pdev, reg_off, &val);
2381 		break;
2382 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2383 		reg_off = fw_health->mapped_regs[reg_idx];
2384 		fallthrough;
2385 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2386 		val = readl(bp->bar0 + reg_off);
2387 		break;
2388 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2389 		val = readl(bp->bar1 + reg_off);
2390 		break;
2391 	}
2392 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2393 		val &= fw_health->fw_reset_inprog_reg_mask;
2394 	return val;
2395 }
2396 
2397 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2398 {
2399 	int i;
2400 
2401 	for (i = 0; i < bp->rx_nr_rings; i++) {
2402 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2403 		struct bnxt_ring_grp_info *grp_info;
2404 
2405 		grp_info = &bp->grp_info[grp_idx];
2406 		if (grp_info->agg_fw_ring_id == ring_id)
2407 			return grp_idx;
2408 	}
2409 	return INVALID_HW_RING_ID;
2410 }
2411 
2412 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2413 {
2414 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2415 
2416 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2417 		return link_info->force_link_speed2;
2418 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2419 		return link_info->force_pam4_link_speed;
2420 	return link_info->force_link_speed;
2421 }
2422 
2423 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2424 {
2425 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2426 
2427 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2428 		link_info->req_link_speed = link_info->force_link_speed2;
2429 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2430 		switch (link_info->req_link_speed) {
2431 		case BNXT_LINK_SPEED_50GB_PAM4:
2432 		case BNXT_LINK_SPEED_100GB_PAM4:
2433 		case BNXT_LINK_SPEED_200GB_PAM4:
2434 		case BNXT_LINK_SPEED_400GB_PAM4:
2435 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2436 			break;
2437 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2438 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2439 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2440 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2441 			break;
2442 		default:
2443 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2444 		}
2445 		return;
2446 	}
2447 	link_info->req_link_speed = link_info->force_link_speed;
2448 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2449 	if (link_info->force_pam4_link_speed) {
2450 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2451 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2452 	}
2453 }
2454 
2455 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2456 {
2457 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2458 
2459 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2460 		link_info->advertising = link_info->auto_link_speeds2;
2461 		return;
2462 	}
2463 	link_info->advertising = link_info->auto_link_speeds;
2464 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2465 }
2466 
2467 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2468 {
2469 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2470 
2471 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2472 		if (link_info->req_link_speed != link_info->force_link_speed2)
2473 			return true;
2474 		return false;
2475 	}
2476 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2477 	    link_info->req_link_speed != link_info->force_link_speed)
2478 		return true;
2479 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2480 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2481 		return true;
2482 	return false;
2483 }
2484 
2485 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2486 {
2487 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2488 
2489 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2490 		if (link_info->advertising != link_info->auto_link_speeds2)
2491 			return true;
2492 		return false;
2493 	}
2494 	if (link_info->advertising != link_info->auto_link_speeds ||
2495 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2496 		return true;
2497 	return false;
2498 }
2499 
2500 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type)
2501 {
2502 	u32 flags = bp->ctx->ctx_arr[type].flags;
2503 
2504 	return (flags & BNXT_CTX_MEM_TYPE_VALID) &&
2505 		((flags & BNXT_CTX_MEM_FW_TRACE) ||
2506 		 (flags & BNXT_CTX_MEM_FW_BIN_TRACE));
2507 }
2508 
2509 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm)
2510 {
2511 	u32 mem_size, pages, rem_bytes, magic_byte_offset;
2512 	u16 trace_type = bnxt_bstore_to_trace[ctxm->type];
2513 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
2514 	struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl;
2515 	struct bnxt_bs_trace_info *bs_trace;
2516 	int last_pg;
2517 
2518 	if (ctxm->instance_bmap && ctxm->instance_bmap > 1)
2519 		return;
2520 
2521 	mem_size = ctxm->max_entries * ctxm->entry_size;
2522 	rem_bytes = mem_size % BNXT_PAGE_SIZE;
2523 	pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
2524 
2525 	last_pg = (pages - 1) & (MAX_CTX_PAGES - 1);
2526 	magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1;
2527 
2528 	rmem = &ctx_pg[0].ring_mem;
2529 	bs_trace = &bp->bs_trace[trace_type];
2530 	bs_trace->ctx_type = ctxm->type;
2531 	bs_trace->trace_type = trace_type;
2532 	if (pages > MAX_CTX_PAGES) {
2533 		int last_pg_dir = rmem->nr_pages - 1;
2534 
2535 		rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem;
2536 		bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg];
2537 	} else {
2538 		bs_trace->magic_byte = rmem->pg_arr[last_pg];
2539 	}
2540 	bs_trace->magic_byte += magic_byte_offset;
2541 	*bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE;
2542 }
2543 
2544 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1)				\
2545 	(((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\
2546 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT)
2547 
2548 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2)				\
2549 	(((data2) &							\
2550 	  ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\
2551 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT)
2552 
2553 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2554 	((data2) &							\
2555 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2556 
2557 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2558 	(((data2) &							\
2559 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2560 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2561 
2562 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2563 	((data1) &							\
2564 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2565 
2566 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2567 	(((data1) &							\
2568 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2569 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2570 
2571 /* Return true if the workqueue has to be scheduled */
2572 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2573 {
2574 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2575 
2576 	switch (err_type) {
2577 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2578 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2579 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2580 		break;
2581 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2582 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2583 		break;
2584 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2585 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2586 		break;
2587 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2588 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2589 		char *threshold_type;
2590 		bool notify = false;
2591 		char *dir_str;
2592 
2593 		switch (type) {
2594 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2595 			threshold_type = "warning";
2596 			break;
2597 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2598 			threshold_type = "critical";
2599 			break;
2600 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2601 			threshold_type = "fatal";
2602 			break;
2603 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2604 			threshold_type = "shutdown";
2605 			break;
2606 		default:
2607 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2608 			return false;
2609 		}
2610 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2611 			dir_str = "above";
2612 			notify = true;
2613 		} else {
2614 			dir_str = "below";
2615 		}
2616 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2617 			    dir_str, threshold_type);
2618 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2619 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2620 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2621 		if (notify) {
2622 			bp->thermal_threshold_type = type;
2623 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2624 			return true;
2625 		}
2626 		return false;
2627 	}
2628 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2629 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2630 		break;
2631 	default:
2632 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2633 			   err_type);
2634 		break;
2635 	}
2636 	return false;
2637 }
2638 
2639 #define BNXT_GET_EVENT_PORT(data)	\
2640 	((data) &			\
2641 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2642 
2643 #define BNXT_EVENT_RING_TYPE(data2)	\
2644 	((data2) &			\
2645 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2646 
2647 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2648 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2649 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2650 
2651 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2652 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2653 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2654 
2655 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2656 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2657 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2658 
2659 #define BNXT_PHC_BITS	48
2660 
2661 static int bnxt_async_event_process(struct bnxt *bp,
2662 				    struct hwrm_async_event_cmpl *cmpl)
2663 {
2664 	u16 event_id = le16_to_cpu(cmpl->event_id);
2665 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2666 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2667 
2668 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2669 		   event_id, data1, data2);
2670 
2671 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2672 	switch (event_id) {
2673 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2674 		struct bnxt_link_info *link_info = &bp->link_info;
2675 
2676 		if (BNXT_VF(bp))
2677 			goto async_event_process_exit;
2678 
2679 		/* print unsupported speed warning in forced speed mode only */
2680 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2681 		    (data1 & 0x20000)) {
2682 			u16 fw_speed = bnxt_get_force_speed(link_info);
2683 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2684 
2685 			if (speed != SPEED_UNKNOWN)
2686 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2687 					    speed);
2688 		}
2689 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2690 	}
2691 		fallthrough;
2692 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2693 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2694 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2695 		fallthrough;
2696 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2697 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2698 		break;
2699 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2700 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2701 		break;
2702 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2703 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2704 
2705 		if (BNXT_VF(bp))
2706 			break;
2707 
2708 		if (bp->pf.port_id != port_id)
2709 			break;
2710 
2711 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2712 		break;
2713 	}
2714 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2715 		if (BNXT_PF(bp))
2716 			goto async_event_process_exit;
2717 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2718 		break;
2719 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2720 		char *type_str = "Solicited";
2721 
2722 		if (!bp->fw_health)
2723 			goto async_event_process_exit;
2724 
2725 		bp->fw_reset_timestamp = jiffies;
2726 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2727 		if (!bp->fw_reset_min_dsecs)
2728 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2729 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2730 		if (!bp->fw_reset_max_dsecs)
2731 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2732 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2733 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2734 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2735 			type_str = "Fatal";
2736 			bp->fw_health->fatalities++;
2737 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2738 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2739 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2740 			type_str = "Non-fatal";
2741 			bp->fw_health->survivals++;
2742 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2743 		}
2744 		netif_warn(bp, hw, bp->dev,
2745 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2746 			   type_str, data1, data2,
2747 			   bp->fw_reset_min_dsecs * 100,
2748 			   bp->fw_reset_max_dsecs * 100);
2749 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2750 		break;
2751 	}
2752 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2753 		struct bnxt_fw_health *fw_health = bp->fw_health;
2754 		char *status_desc = "healthy";
2755 		u32 status;
2756 
2757 		if (!fw_health)
2758 			goto async_event_process_exit;
2759 
2760 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2761 			fw_health->enabled = false;
2762 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2763 			break;
2764 		}
2765 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2766 		fw_health->tmr_multiplier =
2767 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2768 				     bp->current_interval * 10);
2769 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2770 		if (!fw_health->enabled)
2771 			fw_health->last_fw_heartbeat =
2772 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2773 		fw_health->last_fw_reset_cnt =
2774 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2775 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2776 		if (status != BNXT_FW_STATUS_HEALTHY)
2777 			status_desc = "unhealthy";
2778 		netif_info(bp, drv, bp->dev,
2779 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2780 			   fw_health->primary ? "primary" : "backup", status,
2781 			   status_desc, fw_health->last_fw_reset_cnt);
2782 		if (!fw_health->enabled) {
2783 			/* Make sure tmr_counter is set and visible to
2784 			 * bnxt_health_check() before setting enabled to true.
2785 			 */
2786 			smp_wmb();
2787 			fw_health->enabled = true;
2788 		}
2789 		goto async_event_process_exit;
2790 	}
2791 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2792 		netif_notice(bp, hw, bp->dev,
2793 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2794 			     data1, data2);
2795 		goto async_event_process_exit;
2796 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2797 		struct bnxt_rx_ring_info *rxr;
2798 		u16 grp_idx;
2799 
2800 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2801 			goto async_event_process_exit;
2802 
2803 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2804 			    BNXT_EVENT_RING_TYPE(data2), data1);
2805 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2806 			goto async_event_process_exit;
2807 
2808 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2809 		if (grp_idx == INVALID_HW_RING_ID) {
2810 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2811 				    data1);
2812 			goto async_event_process_exit;
2813 		}
2814 		rxr = bp->bnapi[grp_idx]->rx_ring;
2815 		bnxt_sched_reset_rxr(bp, rxr);
2816 		goto async_event_process_exit;
2817 	}
2818 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2819 		struct bnxt_fw_health *fw_health = bp->fw_health;
2820 
2821 		netif_notice(bp, hw, bp->dev,
2822 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2823 			     data1, data2);
2824 		if (fw_health) {
2825 			fw_health->echo_req_data1 = data1;
2826 			fw_health->echo_req_data2 = data2;
2827 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2828 			break;
2829 		}
2830 		goto async_event_process_exit;
2831 	}
2832 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2833 		bnxt_ptp_pps_event(bp, data1, data2);
2834 		goto async_event_process_exit;
2835 	}
2836 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2837 		if (bnxt_event_error_report(bp, data1, data2))
2838 			break;
2839 		goto async_event_process_exit;
2840 	}
2841 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2842 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2843 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2844 			if (BNXT_PTP_USE_RTC(bp)) {
2845 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2846 				unsigned long flags;
2847 				u64 ns;
2848 
2849 				if (!ptp)
2850 					goto async_event_process_exit;
2851 
2852 				bnxt_ptp_update_current_time(bp);
2853 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2854 				       BNXT_PHC_BITS) | ptp->current_time);
2855 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2856 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2857 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2858 			}
2859 			break;
2860 		}
2861 		goto async_event_process_exit;
2862 	}
2863 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2864 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2865 
2866 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2867 		goto async_event_process_exit;
2868 	}
2869 	case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: {
2870 		u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1);
2871 		u32 offset =  BNXT_EVENT_BUF_PRODUCER_OFFSET(data2);
2872 
2873 		bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset);
2874 		goto async_event_process_exit;
2875 	}
2876 	default:
2877 		goto async_event_process_exit;
2878 	}
2879 	__bnxt_queue_sp_work(bp);
2880 async_event_process_exit:
2881 	bnxt_ulp_async_events(bp, cmpl);
2882 	return 0;
2883 }
2884 
2885 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2886 {
2887 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2888 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2889 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2890 				(struct hwrm_fwd_req_cmpl *)txcmp;
2891 
2892 	switch (cmpl_type) {
2893 	case CMPL_BASE_TYPE_HWRM_DONE:
2894 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2895 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2896 		break;
2897 
2898 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2899 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2900 
2901 		if ((vf_id < bp->pf.first_vf_id) ||
2902 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2903 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2904 				   vf_id);
2905 			return -EINVAL;
2906 		}
2907 
2908 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2909 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2910 		break;
2911 
2912 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2913 		bnxt_async_event_process(bp,
2914 					 (struct hwrm_async_event_cmpl *)txcmp);
2915 		break;
2916 
2917 	default:
2918 		break;
2919 	}
2920 
2921 	return 0;
2922 }
2923 
2924 static bool bnxt_vnic_is_active(struct bnxt *bp)
2925 {
2926 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
2927 
2928 	return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0;
2929 }
2930 
2931 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2932 {
2933 	struct bnxt_napi *bnapi = dev_instance;
2934 	struct bnxt *bp = bnapi->bp;
2935 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2936 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2937 
2938 	cpr->event_ctr++;
2939 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2940 	napi_schedule(&bnapi->napi);
2941 	return IRQ_HANDLED;
2942 }
2943 
2944 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2945 {
2946 	u32 raw_cons = cpr->cp_raw_cons;
2947 	u16 cons = RING_CMP(raw_cons);
2948 	struct tx_cmp *txcmp;
2949 
2950 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2951 
2952 	return TX_CMP_VALID(txcmp, raw_cons);
2953 }
2954 
2955 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2956 			    int budget)
2957 {
2958 	struct bnxt_napi *bnapi = cpr->bnapi;
2959 	u32 raw_cons = cpr->cp_raw_cons;
2960 	u32 cons;
2961 	int rx_pkts = 0;
2962 	u8 event = 0;
2963 	struct tx_cmp *txcmp;
2964 
2965 	cpr->has_more_work = 0;
2966 	cpr->had_work_done = 1;
2967 	while (1) {
2968 		u8 cmp_type;
2969 		int rc;
2970 
2971 		cons = RING_CMP(raw_cons);
2972 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2973 
2974 		if (!TX_CMP_VALID(txcmp, raw_cons))
2975 			break;
2976 
2977 		/* The valid test of the entry must be done first before
2978 		 * reading any further.
2979 		 */
2980 		dma_rmb();
2981 		cmp_type = TX_CMP_TYPE(txcmp);
2982 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
2983 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
2984 			u32 opaque = txcmp->tx_cmp_opaque;
2985 			struct bnxt_tx_ring_info *txr;
2986 			u16 tx_freed;
2987 
2988 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
2989 			event |= BNXT_TX_CMP_EVENT;
2990 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
2991 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
2992 			else
2993 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
2994 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
2995 				   bp->tx_ring_mask;
2996 			/* return full budget so NAPI will complete. */
2997 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
2998 				rx_pkts = budget;
2999 				raw_cons = NEXT_RAW_CMP(raw_cons);
3000 				if (budget)
3001 					cpr->has_more_work = 1;
3002 				break;
3003 			}
3004 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
3005 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
3006 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
3007 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
3008 			if (likely(budget))
3009 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3010 			else
3011 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
3012 							   &event);
3013 			if (likely(rc >= 0))
3014 				rx_pkts += rc;
3015 			/* Increment rx_pkts when rc is -ENOMEM to count towards
3016 			 * the NAPI budget.  Otherwise, we may potentially loop
3017 			 * here forever if we consistently cannot allocate
3018 			 * buffers.
3019 			 */
3020 			else if (rc == -ENOMEM && budget)
3021 				rx_pkts++;
3022 			else if (rc == -EBUSY)	/* partial completion */
3023 				break;
3024 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
3025 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
3026 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
3027 			bnxt_hwrm_handler(bp, txcmp);
3028 		}
3029 		raw_cons = NEXT_RAW_CMP(raw_cons);
3030 
3031 		if (rx_pkts && rx_pkts == budget) {
3032 			cpr->has_more_work = 1;
3033 			break;
3034 		}
3035 	}
3036 
3037 	if (event & BNXT_REDIRECT_EVENT) {
3038 		xdp_do_flush();
3039 		event &= ~BNXT_REDIRECT_EVENT;
3040 	}
3041 
3042 	if (event & BNXT_TX_EVENT) {
3043 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3044 		u16 prod = txr->tx_prod;
3045 
3046 		/* Sync BD data before updating doorbell */
3047 		wmb();
3048 
3049 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3050 		event &= ~BNXT_TX_EVENT;
3051 	}
3052 
3053 	cpr->cp_raw_cons = raw_cons;
3054 	bnapi->events |= event;
3055 	return rx_pkts;
3056 }
3057 
3058 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3059 				  int budget)
3060 {
3061 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3062 		bnapi->tx_int(bp, bnapi, budget);
3063 
3064 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3065 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3066 
3067 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3068 		bnapi->events &= ~BNXT_RX_EVENT;
3069 	}
3070 	if (bnapi->events & BNXT_AGG_EVENT) {
3071 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3072 
3073 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3074 		bnapi->events &= ~BNXT_AGG_EVENT;
3075 	}
3076 }
3077 
3078 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3079 			  int budget)
3080 {
3081 	struct bnxt_napi *bnapi = cpr->bnapi;
3082 	int rx_pkts;
3083 
3084 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3085 
3086 	/* ACK completion ring before freeing tx ring and producing new
3087 	 * buffers in rx/agg rings to prevent overflowing the completion
3088 	 * ring.
3089 	 */
3090 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3091 
3092 	__bnxt_poll_work_done(bp, bnapi, budget);
3093 	return rx_pkts;
3094 }
3095 
3096 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3097 {
3098 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3099 	struct bnxt *bp = bnapi->bp;
3100 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3101 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3102 	struct tx_cmp *txcmp;
3103 	struct rx_cmp_ext *rxcmp1;
3104 	u32 cp_cons, tmp_raw_cons;
3105 	u32 raw_cons = cpr->cp_raw_cons;
3106 	bool flush_xdp = false;
3107 	u32 rx_pkts = 0;
3108 	u8 event = 0;
3109 
3110 	while (1) {
3111 		int rc;
3112 
3113 		cp_cons = RING_CMP(raw_cons);
3114 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3115 
3116 		if (!TX_CMP_VALID(txcmp, raw_cons))
3117 			break;
3118 
3119 		/* The valid test of the entry must be done first before
3120 		 * reading any further.
3121 		 */
3122 		dma_rmb();
3123 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3124 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3125 			cp_cons = RING_CMP(tmp_raw_cons);
3126 			rxcmp1 = (struct rx_cmp_ext *)
3127 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3128 
3129 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3130 				break;
3131 
3132 			/* force an error to recycle the buffer */
3133 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3134 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3135 
3136 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3137 			if (likely(rc == -EIO) && budget)
3138 				rx_pkts++;
3139 			else if (rc == -EBUSY)	/* partial completion */
3140 				break;
3141 			if (event & BNXT_REDIRECT_EVENT)
3142 				flush_xdp = true;
3143 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3144 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3145 			bnxt_hwrm_handler(bp, txcmp);
3146 		} else {
3147 			netdev_err(bp->dev,
3148 				   "Invalid completion received on special ring\n");
3149 		}
3150 		raw_cons = NEXT_RAW_CMP(raw_cons);
3151 
3152 		if (rx_pkts == budget)
3153 			break;
3154 	}
3155 
3156 	cpr->cp_raw_cons = raw_cons;
3157 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3158 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3159 
3160 	if (event & BNXT_AGG_EVENT)
3161 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3162 	if (flush_xdp)
3163 		xdp_do_flush();
3164 
3165 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3166 		napi_complete_done(napi, rx_pkts);
3167 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3168 	}
3169 	return rx_pkts;
3170 }
3171 
3172 static int bnxt_poll(struct napi_struct *napi, int budget)
3173 {
3174 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3175 	struct bnxt *bp = bnapi->bp;
3176 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3177 	int work_done = 0;
3178 
3179 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3180 		napi_complete(napi);
3181 		return 0;
3182 	}
3183 	while (1) {
3184 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3185 
3186 		if (work_done >= budget) {
3187 			if (!budget)
3188 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3189 			break;
3190 		}
3191 
3192 		if (!bnxt_has_work(bp, cpr)) {
3193 			if (napi_complete_done(napi, work_done))
3194 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3195 			break;
3196 		}
3197 	}
3198 	if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3199 		struct dim_sample dim_sample = {};
3200 
3201 		dim_update_sample(cpr->event_ctr,
3202 				  cpr->rx_packets,
3203 				  cpr->rx_bytes,
3204 				  &dim_sample);
3205 		net_dim(&cpr->dim, &dim_sample);
3206 	}
3207 	return work_done;
3208 }
3209 
3210 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3211 {
3212 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3213 	int i, work_done = 0;
3214 
3215 	for (i = 0; i < cpr->cp_ring_count; i++) {
3216 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3217 
3218 		if (cpr2->had_nqe_notify) {
3219 			work_done += __bnxt_poll_work(bp, cpr2,
3220 						      budget - work_done);
3221 			cpr->has_more_work |= cpr2->has_more_work;
3222 		}
3223 	}
3224 	return work_done;
3225 }
3226 
3227 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3228 				 u64 dbr_type, int budget)
3229 {
3230 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3231 	int i;
3232 
3233 	for (i = 0; i < cpr->cp_ring_count; i++) {
3234 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3235 		struct bnxt_db_info *db;
3236 
3237 		if (cpr2->had_work_done) {
3238 			u32 tgl = 0;
3239 
3240 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3241 				cpr2->had_nqe_notify = 0;
3242 				tgl = cpr2->toggle;
3243 			}
3244 			db = &cpr2->cp_db;
3245 			bnxt_writeq(bp,
3246 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3247 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3248 				    db->doorbell);
3249 			cpr2->had_work_done = 0;
3250 		}
3251 	}
3252 	__bnxt_poll_work_done(bp, bnapi, budget);
3253 }
3254 
3255 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3256 {
3257 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3258 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3259 	struct bnxt_cp_ring_info *cpr_rx;
3260 	u32 raw_cons = cpr->cp_raw_cons;
3261 	struct bnxt *bp = bnapi->bp;
3262 	struct nqe_cn *nqcmp;
3263 	int work_done = 0;
3264 	u32 cons;
3265 
3266 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3267 		napi_complete(napi);
3268 		return 0;
3269 	}
3270 	if (cpr->has_more_work) {
3271 		cpr->has_more_work = 0;
3272 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3273 	}
3274 	while (1) {
3275 		u16 type;
3276 
3277 		cons = RING_CMP(raw_cons);
3278 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3279 
3280 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3281 			if (cpr->has_more_work)
3282 				break;
3283 
3284 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3285 					     budget);
3286 			cpr->cp_raw_cons = raw_cons;
3287 			if (napi_complete_done(napi, work_done))
3288 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3289 						  cpr->cp_raw_cons);
3290 			goto poll_done;
3291 		}
3292 
3293 		/* The valid test of the entry must be done first before
3294 		 * reading any further.
3295 		 */
3296 		dma_rmb();
3297 
3298 		type = le16_to_cpu(nqcmp->type);
3299 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3300 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3301 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3302 			struct bnxt_cp_ring_info *cpr2;
3303 
3304 			/* No more budget for RX work */
3305 			if (budget && work_done >= budget &&
3306 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3307 				break;
3308 
3309 			idx = BNXT_NQ_HDL_IDX(idx);
3310 			cpr2 = &cpr->cp_ring_arr[idx];
3311 			cpr2->had_nqe_notify = 1;
3312 			cpr2->toggle = NQE_CN_TOGGLE(type);
3313 			work_done += __bnxt_poll_work(bp, cpr2,
3314 						      budget - work_done);
3315 			cpr->has_more_work |= cpr2->has_more_work;
3316 		} else {
3317 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3318 		}
3319 		raw_cons = NEXT_RAW_CMP(raw_cons);
3320 	}
3321 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3322 	if (raw_cons != cpr->cp_raw_cons) {
3323 		cpr->cp_raw_cons = raw_cons;
3324 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3325 	}
3326 poll_done:
3327 	cpr_rx = &cpr->cp_ring_arr[0];
3328 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3329 	    (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3330 		struct dim_sample dim_sample = {};
3331 
3332 		dim_update_sample(cpr->event_ctr,
3333 				  cpr_rx->rx_packets,
3334 				  cpr_rx->rx_bytes,
3335 				  &dim_sample);
3336 		net_dim(&cpr->dim, &dim_sample);
3337 	}
3338 	return work_done;
3339 }
3340 
3341 static void bnxt_free_one_tx_ring_skbs(struct bnxt *bp,
3342 				       struct bnxt_tx_ring_info *txr, int idx)
3343 {
3344 	int i, max_idx;
3345 	struct pci_dev *pdev = bp->pdev;
3346 
3347 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3348 
3349 	for (i = 0; i < max_idx;) {
3350 		struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[i];
3351 		struct sk_buff *skb;
3352 		int j, last;
3353 
3354 		if (idx  < bp->tx_nr_rings_xdp &&
3355 		    tx_buf->action == XDP_REDIRECT) {
3356 			dma_unmap_single(&pdev->dev,
3357 					 dma_unmap_addr(tx_buf, mapping),
3358 					 dma_unmap_len(tx_buf, len),
3359 					 DMA_TO_DEVICE);
3360 			xdp_return_frame(tx_buf->xdpf);
3361 			tx_buf->action = 0;
3362 			tx_buf->xdpf = NULL;
3363 			i++;
3364 			continue;
3365 		}
3366 
3367 		skb = tx_buf->skb;
3368 		if (!skb) {
3369 			i++;
3370 			continue;
3371 		}
3372 
3373 		tx_buf->skb = NULL;
3374 
3375 		if (tx_buf->is_push) {
3376 			dev_kfree_skb(skb);
3377 			i += 2;
3378 			continue;
3379 		}
3380 
3381 		dma_unmap_single(&pdev->dev,
3382 				 dma_unmap_addr(tx_buf, mapping),
3383 				 skb_headlen(skb),
3384 				 DMA_TO_DEVICE);
3385 
3386 		last = tx_buf->nr_frags;
3387 		i += 2;
3388 		for (j = 0; j < last; j++, i++) {
3389 			int ring_idx = i & bp->tx_ring_mask;
3390 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
3391 
3392 			tx_buf = &txr->tx_buf_ring[ring_idx];
3393 			dma_unmap_page(&pdev->dev,
3394 				       dma_unmap_addr(tx_buf, mapping),
3395 				       skb_frag_size(frag), DMA_TO_DEVICE);
3396 		}
3397 		dev_kfree_skb(skb);
3398 	}
3399 	netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, idx));
3400 }
3401 
3402 static void bnxt_free_tx_skbs(struct bnxt *bp)
3403 {
3404 	int i;
3405 
3406 	if (!bp->tx_ring)
3407 		return;
3408 
3409 	for (i = 0; i < bp->tx_nr_rings; i++) {
3410 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3411 
3412 		if (!txr->tx_buf_ring)
3413 			continue;
3414 
3415 		bnxt_free_one_tx_ring_skbs(bp, txr, i);
3416 	}
3417 
3418 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
3419 		bnxt_ptp_free_txts_skbs(bp->ptp_cfg);
3420 }
3421 
3422 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3423 {
3424 	int i, max_idx;
3425 
3426 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3427 
3428 	for (i = 0; i < max_idx; i++) {
3429 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3430 		void *data = rx_buf->data;
3431 
3432 		if (!data)
3433 			continue;
3434 
3435 		rx_buf->data = NULL;
3436 		if (BNXT_RX_PAGE_MODE(bp))
3437 			page_pool_recycle_direct(rxr->page_pool, data);
3438 		else
3439 			page_pool_free_va(rxr->head_pool, data, true);
3440 	}
3441 }
3442 
3443 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3444 {
3445 	int i, max_idx;
3446 
3447 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3448 
3449 	for (i = 0; i < max_idx; i++) {
3450 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3451 		struct page *page = rx_agg_buf->page;
3452 
3453 		if (!page)
3454 			continue;
3455 
3456 		rx_agg_buf->page = NULL;
3457 		__clear_bit(i, rxr->rx_agg_bmap);
3458 
3459 		page_pool_recycle_direct(rxr->page_pool, page);
3460 	}
3461 }
3462 
3463 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3464 					struct bnxt_rx_ring_info *rxr)
3465 {
3466 	int i;
3467 
3468 	for (i = 0; i < bp->max_tpa; i++) {
3469 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3470 		u8 *data = tpa_info->data;
3471 
3472 		if (!data)
3473 			continue;
3474 
3475 		tpa_info->data = NULL;
3476 		page_pool_free_va(rxr->head_pool, data, false);
3477 	}
3478 }
3479 
3480 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3481 				       struct bnxt_rx_ring_info *rxr)
3482 {
3483 	struct bnxt_tpa_idx_map *map;
3484 
3485 	if (!rxr->rx_tpa)
3486 		goto skip_rx_tpa_free;
3487 
3488 	bnxt_free_one_tpa_info_data(bp, rxr);
3489 
3490 skip_rx_tpa_free:
3491 	if (!rxr->rx_buf_ring)
3492 		goto skip_rx_buf_free;
3493 
3494 	bnxt_free_one_rx_ring(bp, rxr);
3495 
3496 skip_rx_buf_free:
3497 	if (!rxr->rx_agg_ring)
3498 		goto skip_rx_agg_free;
3499 
3500 	bnxt_free_one_rx_agg_ring(bp, rxr);
3501 
3502 skip_rx_agg_free:
3503 	map = rxr->rx_tpa_idx_map;
3504 	if (map)
3505 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3506 }
3507 
3508 static void bnxt_free_rx_skbs(struct bnxt *bp)
3509 {
3510 	int i;
3511 
3512 	if (!bp->rx_ring)
3513 		return;
3514 
3515 	for (i = 0; i < bp->rx_nr_rings; i++)
3516 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3517 }
3518 
3519 static void bnxt_free_skbs(struct bnxt *bp)
3520 {
3521 	bnxt_free_tx_skbs(bp);
3522 	bnxt_free_rx_skbs(bp);
3523 }
3524 
3525 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3526 {
3527 	u8 init_val = ctxm->init_value;
3528 	u16 offset = ctxm->init_offset;
3529 	u8 *p2 = p;
3530 	int i;
3531 
3532 	if (!init_val)
3533 		return;
3534 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3535 		memset(p, init_val, len);
3536 		return;
3537 	}
3538 	for (i = 0; i < len; i += ctxm->entry_size)
3539 		*(p2 + i + offset) = init_val;
3540 }
3541 
3542 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3543 			       void *buf, size_t offset, size_t head,
3544 			       size_t tail)
3545 {
3546 	int i, head_page, start_idx, source_offset;
3547 	size_t len, rem_len, total_len, max_bytes;
3548 
3549 	head_page = head / rmem->page_size;
3550 	source_offset = head % rmem->page_size;
3551 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3552 	if (!total_len)
3553 		total_len = MAX_CTX_BYTES;
3554 	start_idx = head_page % MAX_CTX_PAGES;
3555 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3556 		    source_offset;
3557 	total_len = min(total_len, max_bytes);
3558 	rem_len = total_len;
3559 
3560 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3561 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3562 		if (buf)
3563 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3564 			       len);
3565 		offset += len;
3566 		rem_len -= len;
3567 	}
3568 	return total_len;
3569 }
3570 
3571 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3572 {
3573 	struct pci_dev *pdev = bp->pdev;
3574 	int i;
3575 
3576 	if (!rmem->pg_arr)
3577 		goto skip_pages;
3578 
3579 	for (i = 0; i < rmem->nr_pages; i++) {
3580 		if (!rmem->pg_arr[i])
3581 			continue;
3582 
3583 		dma_free_coherent(&pdev->dev, rmem->page_size,
3584 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3585 
3586 		rmem->pg_arr[i] = NULL;
3587 	}
3588 skip_pages:
3589 	if (rmem->pg_tbl) {
3590 		size_t pg_tbl_size = rmem->nr_pages * 8;
3591 
3592 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3593 			pg_tbl_size = rmem->page_size;
3594 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3595 				  rmem->pg_tbl, rmem->pg_tbl_map);
3596 		rmem->pg_tbl = NULL;
3597 	}
3598 	if (rmem->vmem_size && *rmem->vmem) {
3599 		vfree(*rmem->vmem);
3600 		*rmem->vmem = NULL;
3601 	}
3602 }
3603 
3604 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3605 {
3606 	struct pci_dev *pdev = bp->pdev;
3607 	u64 valid_bit = 0;
3608 	int i;
3609 
3610 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3611 		valid_bit = PTU_PTE_VALID;
3612 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3613 		size_t pg_tbl_size = rmem->nr_pages * 8;
3614 
3615 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3616 			pg_tbl_size = rmem->page_size;
3617 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3618 						  &rmem->pg_tbl_map,
3619 						  GFP_KERNEL);
3620 		if (!rmem->pg_tbl)
3621 			return -ENOMEM;
3622 	}
3623 
3624 	for (i = 0; i < rmem->nr_pages; i++) {
3625 		u64 extra_bits = valid_bit;
3626 
3627 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3628 						     rmem->page_size,
3629 						     &rmem->dma_arr[i],
3630 						     GFP_KERNEL);
3631 		if (!rmem->pg_arr[i])
3632 			return -ENOMEM;
3633 
3634 		if (rmem->ctx_mem)
3635 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3636 					  rmem->page_size);
3637 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3638 			if (i == rmem->nr_pages - 2 &&
3639 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3640 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3641 			else if (i == rmem->nr_pages - 1 &&
3642 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3643 				extra_bits |= PTU_PTE_LAST;
3644 			rmem->pg_tbl[i] =
3645 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3646 		}
3647 	}
3648 
3649 	if (rmem->vmem_size) {
3650 		*rmem->vmem = vzalloc(rmem->vmem_size);
3651 		if (!(*rmem->vmem))
3652 			return -ENOMEM;
3653 	}
3654 	return 0;
3655 }
3656 
3657 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3658 				   struct bnxt_rx_ring_info *rxr)
3659 {
3660 	int i;
3661 
3662 	kfree(rxr->rx_tpa_idx_map);
3663 	rxr->rx_tpa_idx_map = NULL;
3664 	if (rxr->rx_tpa) {
3665 		for (i = 0; i < bp->max_tpa; i++) {
3666 			kfree(rxr->rx_tpa[i].agg_arr);
3667 			rxr->rx_tpa[i].agg_arr = NULL;
3668 		}
3669 	}
3670 	kfree(rxr->rx_tpa);
3671 	rxr->rx_tpa = NULL;
3672 }
3673 
3674 static void bnxt_free_tpa_info(struct bnxt *bp)
3675 {
3676 	int i;
3677 
3678 	for (i = 0; i < bp->rx_nr_rings; i++) {
3679 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3680 
3681 		bnxt_free_one_tpa_info(bp, rxr);
3682 	}
3683 }
3684 
3685 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3686 				   struct bnxt_rx_ring_info *rxr)
3687 {
3688 	struct rx_agg_cmp *agg;
3689 	int i;
3690 
3691 	rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3692 			      GFP_KERNEL);
3693 	if (!rxr->rx_tpa)
3694 		return -ENOMEM;
3695 
3696 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3697 		return 0;
3698 	for (i = 0; i < bp->max_tpa; i++) {
3699 		agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3700 		if (!agg)
3701 			return -ENOMEM;
3702 		rxr->rx_tpa[i].agg_arr = agg;
3703 	}
3704 	rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3705 				      GFP_KERNEL);
3706 	if (!rxr->rx_tpa_idx_map)
3707 		return -ENOMEM;
3708 
3709 	return 0;
3710 }
3711 
3712 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3713 {
3714 	int i, rc;
3715 
3716 	bp->max_tpa = MAX_TPA;
3717 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3718 		if (!bp->max_tpa_v2)
3719 			return 0;
3720 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3721 	}
3722 
3723 	for (i = 0; i < bp->rx_nr_rings; i++) {
3724 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3725 
3726 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3727 		if (rc)
3728 			return rc;
3729 	}
3730 	return 0;
3731 }
3732 
3733 static void bnxt_free_rx_rings(struct bnxt *bp)
3734 {
3735 	int i;
3736 
3737 	if (!bp->rx_ring)
3738 		return;
3739 
3740 	bnxt_free_tpa_info(bp);
3741 	for (i = 0; i < bp->rx_nr_rings; i++) {
3742 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3743 		struct bnxt_ring_struct *ring;
3744 
3745 		if (rxr->xdp_prog)
3746 			bpf_prog_put(rxr->xdp_prog);
3747 
3748 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3749 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3750 
3751 		page_pool_destroy(rxr->page_pool);
3752 		if (bnxt_separate_head_pool())
3753 			page_pool_destroy(rxr->head_pool);
3754 		rxr->page_pool = rxr->head_pool = NULL;
3755 
3756 		kfree(rxr->rx_agg_bmap);
3757 		rxr->rx_agg_bmap = NULL;
3758 
3759 		ring = &rxr->rx_ring_struct;
3760 		bnxt_free_ring(bp, &ring->ring_mem);
3761 
3762 		ring = &rxr->rx_agg_ring_struct;
3763 		bnxt_free_ring(bp, &ring->ring_mem);
3764 	}
3765 }
3766 
3767 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3768 				   struct bnxt_rx_ring_info *rxr,
3769 				   int numa_node)
3770 {
3771 	struct page_pool_params pp = { 0 };
3772 	struct page_pool *pool;
3773 
3774 	pp.pool_size = bp->rx_agg_ring_size;
3775 	if (BNXT_RX_PAGE_MODE(bp))
3776 		pp.pool_size += bp->rx_ring_size;
3777 	pp.nid = numa_node;
3778 	pp.napi = &rxr->bnapi->napi;
3779 	pp.netdev = bp->dev;
3780 	pp.dev = &bp->pdev->dev;
3781 	pp.dma_dir = bp->rx_dir;
3782 	pp.max_len = PAGE_SIZE;
3783 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3784 
3785 	pool = page_pool_create(&pp);
3786 	if (IS_ERR(pool))
3787 		return PTR_ERR(pool);
3788 	rxr->page_pool = pool;
3789 
3790 	if (bnxt_separate_head_pool()) {
3791 		pp.pool_size = max(bp->rx_ring_size, 1024);
3792 		pool = page_pool_create(&pp);
3793 		if (IS_ERR(pool))
3794 			goto err_destroy_pp;
3795 	}
3796 	rxr->head_pool = pool;
3797 
3798 	return 0;
3799 
3800 err_destroy_pp:
3801 	page_pool_destroy(rxr->page_pool);
3802 	rxr->page_pool = NULL;
3803 	return PTR_ERR(pool);
3804 }
3805 
3806 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3807 {
3808 	u16 mem_size;
3809 
3810 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3811 	mem_size = rxr->rx_agg_bmap_size / 8;
3812 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3813 	if (!rxr->rx_agg_bmap)
3814 		return -ENOMEM;
3815 
3816 	return 0;
3817 }
3818 
3819 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3820 {
3821 	int numa_node = dev_to_node(&bp->pdev->dev);
3822 	int i, rc = 0, agg_rings = 0, cpu;
3823 
3824 	if (!bp->rx_ring)
3825 		return -ENOMEM;
3826 
3827 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3828 		agg_rings = 1;
3829 
3830 	for (i = 0; i < bp->rx_nr_rings; i++) {
3831 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3832 		struct bnxt_ring_struct *ring;
3833 		int cpu_node;
3834 
3835 		ring = &rxr->rx_ring_struct;
3836 
3837 		cpu = cpumask_local_spread(i, numa_node);
3838 		cpu_node = cpu_to_node(cpu);
3839 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3840 			   i, cpu_node);
3841 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3842 		if (rc)
3843 			return rc;
3844 
3845 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3846 		if (rc < 0)
3847 			return rc;
3848 
3849 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3850 						MEM_TYPE_PAGE_POOL,
3851 						rxr->page_pool);
3852 		if (rc) {
3853 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3854 			return rc;
3855 		}
3856 
3857 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3858 		if (rc)
3859 			return rc;
3860 
3861 		ring->grp_idx = i;
3862 		if (agg_rings) {
3863 			ring = &rxr->rx_agg_ring_struct;
3864 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3865 			if (rc)
3866 				return rc;
3867 
3868 			ring->grp_idx = i;
3869 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
3870 			if (rc)
3871 				return rc;
3872 		}
3873 	}
3874 	if (bp->flags & BNXT_FLAG_TPA)
3875 		rc = bnxt_alloc_tpa_info(bp);
3876 	return rc;
3877 }
3878 
3879 static void bnxt_free_tx_rings(struct bnxt *bp)
3880 {
3881 	int i;
3882 	struct pci_dev *pdev = bp->pdev;
3883 
3884 	if (!bp->tx_ring)
3885 		return;
3886 
3887 	for (i = 0; i < bp->tx_nr_rings; i++) {
3888 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3889 		struct bnxt_ring_struct *ring;
3890 
3891 		if (txr->tx_push) {
3892 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3893 					  txr->tx_push, txr->tx_push_mapping);
3894 			txr->tx_push = NULL;
3895 		}
3896 
3897 		ring = &txr->tx_ring_struct;
3898 
3899 		bnxt_free_ring(bp, &ring->ring_mem);
3900 	}
3901 }
3902 
3903 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3904 	((tc) * (bp)->tx_nr_rings_per_tc)
3905 
3906 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3907 	((tx) % (bp)->tx_nr_rings_per_tc)
3908 
3909 #define BNXT_RING_TO_TC(bp, tx)		\
3910 	((tx) / (bp)->tx_nr_rings_per_tc)
3911 
3912 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3913 {
3914 	int i, j, rc;
3915 	struct pci_dev *pdev = bp->pdev;
3916 
3917 	bp->tx_push_size = 0;
3918 	if (bp->tx_push_thresh) {
3919 		int push_size;
3920 
3921 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3922 					bp->tx_push_thresh);
3923 
3924 		if (push_size > 256) {
3925 			push_size = 0;
3926 			bp->tx_push_thresh = 0;
3927 		}
3928 
3929 		bp->tx_push_size = push_size;
3930 	}
3931 
3932 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3933 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3934 		struct bnxt_ring_struct *ring;
3935 		u8 qidx;
3936 
3937 		ring = &txr->tx_ring_struct;
3938 
3939 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3940 		if (rc)
3941 			return rc;
3942 
3943 		ring->grp_idx = txr->bnapi->index;
3944 		if (bp->tx_push_size) {
3945 			dma_addr_t mapping;
3946 
3947 			/* One pre-allocated DMA buffer to backup
3948 			 * TX push operation
3949 			 */
3950 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3951 						bp->tx_push_size,
3952 						&txr->tx_push_mapping,
3953 						GFP_KERNEL);
3954 
3955 			if (!txr->tx_push)
3956 				return -ENOMEM;
3957 
3958 			mapping = txr->tx_push_mapping +
3959 				sizeof(struct tx_push_bd);
3960 			txr->data_mapping = cpu_to_le64(mapping);
3961 		}
3962 		qidx = bp->tc_to_qidx[j];
3963 		ring->queue_id = bp->q_info[qidx].queue_id;
3964 		spin_lock_init(&txr->xdp_tx_lock);
3965 		if (i < bp->tx_nr_rings_xdp)
3966 			continue;
3967 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
3968 			j++;
3969 	}
3970 	return 0;
3971 }
3972 
3973 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
3974 {
3975 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3976 
3977 	kfree(cpr->cp_desc_ring);
3978 	cpr->cp_desc_ring = NULL;
3979 	ring->ring_mem.pg_arr = NULL;
3980 	kfree(cpr->cp_desc_mapping);
3981 	cpr->cp_desc_mapping = NULL;
3982 	ring->ring_mem.dma_arr = NULL;
3983 }
3984 
3985 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
3986 {
3987 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
3988 	if (!cpr->cp_desc_ring)
3989 		return -ENOMEM;
3990 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
3991 				       GFP_KERNEL);
3992 	if (!cpr->cp_desc_mapping)
3993 		return -ENOMEM;
3994 	return 0;
3995 }
3996 
3997 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
3998 {
3999 	int i;
4000 
4001 	if (!bp->bnapi)
4002 		return;
4003 	for (i = 0; i < bp->cp_nr_rings; i++) {
4004 		struct bnxt_napi *bnapi = bp->bnapi[i];
4005 
4006 		if (!bnapi)
4007 			continue;
4008 		bnxt_free_cp_arrays(&bnapi->cp_ring);
4009 	}
4010 }
4011 
4012 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
4013 {
4014 	int i, n = bp->cp_nr_pages;
4015 
4016 	for (i = 0; i < bp->cp_nr_rings; i++) {
4017 		struct bnxt_napi *bnapi = bp->bnapi[i];
4018 		int rc;
4019 
4020 		if (!bnapi)
4021 			continue;
4022 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
4023 		if (rc)
4024 			return rc;
4025 	}
4026 	return 0;
4027 }
4028 
4029 static void bnxt_free_cp_rings(struct bnxt *bp)
4030 {
4031 	int i;
4032 
4033 	if (!bp->bnapi)
4034 		return;
4035 
4036 	for (i = 0; i < bp->cp_nr_rings; i++) {
4037 		struct bnxt_napi *bnapi = bp->bnapi[i];
4038 		struct bnxt_cp_ring_info *cpr;
4039 		struct bnxt_ring_struct *ring;
4040 		int j;
4041 
4042 		if (!bnapi)
4043 			continue;
4044 
4045 		cpr = &bnapi->cp_ring;
4046 		ring = &cpr->cp_ring_struct;
4047 
4048 		bnxt_free_ring(bp, &ring->ring_mem);
4049 
4050 		if (!cpr->cp_ring_arr)
4051 			continue;
4052 
4053 		for (j = 0; j < cpr->cp_ring_count; j++) {
4054 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4055 
4056 			ring = &cpr2->cp_ring_struct;
4057 			bnxt_free_ring(bp, &ring->ring_mem);
4058 			bnxt_free_cp_arrays(cpr2);
4059 		}
4060 		kfree(cpr->cp_ring_arr);
4061 		cpr->cp_ring_arr = NULL;
4062 		cpr->cp_ring_count = 0;
4063 	}
4064 }
4065 
4066 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4067 				  struct bnxt_cp_ring_info *cpr)
4068 {
4069 	struct bnxt_ring_mem_info *rmem;
4070 	struct bnxt_ring_struct *ring;
4071 	int rc;
4072 
4073 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4074 	if (rc) {
4075 		bnxt_free_cp_arrays(cpr);
4076 		return -ENOMEM;
4077 	}
4078 	ring = &cpr->cp_ring_struct;
4079 	rmem = &ring->ring_mem;
4080 	rmem->nr_pages = bp->cp_nr_pages;
4081 	rmem->page_size = HW_CMPD_RING_SIZE;
4082 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4083 	rmem->dma_arr = cpr->cp_desc_mapping;
4084 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4085 	rc = bnxt_alloc_ring(bp, rmem);
4086 	if (rc) {
4087 		bnxt_free_ring(bp, rmem);
4088 		bnxt_free_cp_arrays(cpr);
4089 	}
4090 	return rc;
4091 }
4092 
4093 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4094 {
4095 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4096 	int i, j, rc, ulp_msix;
4097 	int tcs = bp->num_tc;
4098 
4099 	if (!tcs)
4100 		tcs = 1;
4101 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4102 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4103 		struct bnxt_napi *bnapi = bp->bnapi[i];
4104 		struct bnxt_cp_ring_info *cpr, *cpr2;
4105 		struct bnxt_ring_struct *ring;
4106 		int cp_count = 0, k;
4107 		int rx = 0, tx = 0;
4108 
4109 		if (!bnapi)
4110 			continue;
4111 
4112 		cpr = &bnapi->cp_ring;
4113 		cpr->bnapi = bnapi;
4114 		ring = &cpr->cp_ring_struct;
4115 
4116 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4117 		if (rc)
4118 			return rc;
4119 
4120 		ring->map_idx = ulp_msix + i;
4121 
4122 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4123 			continue;
4124 
4125 		if (i < bp->rx_nr_rings) {
4126 			cp_count++;
4127 			rx = 1;
4128 		}
4129 		if (i < bp->tx_nr_rings_xdp) {
4130 			cp_count++;
4131 			tx = 1;
4132 		} else if ((sh && i < bp->tx_nr_rings) ||
4133 			 (!sh && i >= bp->rx_nr_rings)) {
4134 			cp_count += tcs;
4135 			tx = 1;
4136 		}
4137 
4138 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
4139 					   GFP_KERNEL);
4140 		if (!cpr->cp_ring_arr)
4141 			return -ENOMEM;
4142 		cpr->cp_ring_count = cp_count;
4143 
4144 		for (k = 0; k < cp_count; k++) {
4145 			cpr2 = &cpr->cp_ring_arr[k];
4146 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4147 			if (rc)
4148 				return rc;
4149 			cpr2->bnapi = bnapi;
4150 			cpr2->sw_stats = cpr->sw_stats;
4151 			cpr2->cp_idx = k;
4152 			if (!k && rx) {
4153 				bp->rx_ring[i].rx_cpr = cpr2;
4154 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4155 			} else {
4156 				int n, tc = k - rx;
4157 
4158 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4159 				bp->tx_ring[n].tx_cpr = cpr2;
4160 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4161 			}
4162 		}
4163 		if (tx)
4164 			j++;
4165 	}
4166 	return 0;
4167 }
4168 
4169 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4170 				     struct bnxt_rx_ring_info *rxr)
4171 {
4172 	struct bnxt_ring_mem_info *rmem;
4173 	struct bnxt_ring_struct *ring;
4174 
4175 	ring = &rxr->rx_ring_struct;
4176 	rmem = &ring->ring_mem;
4177 	rmem->nr_pages = bp->rx_nr_pages;
4178 	rmem->page_size = HW_RXBD_RING_SIZE;
4179 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4180 	rmem->dma_arr = rxr->rx_desc_mapping;
4181 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4182 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4183 
4184 	ring = &rxr->rx_agg_ring_struct;
4185 	rmem = &ring->ring_mem;
4186 	rmem->nr_pages = bp->rx_agg_nr_pages;
4187 	rmem->page_size = HW_RXBD_RING_SIZE;
4188 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4189 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4190 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4191 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4192 }
4193 
4194 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4195 				      struct bnxt_rx_ring_info *rxr)
4196 {
4197 	struct bnxt_ring_mem_info *rmem;
4198 	struct bnxt_ring_struct *ring;
4199 	int i;
4200 
4201 	rxr->page_pool->p.napi = NULL;
4202 	rxr->page_pool = NULL;
4203 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4204 
4205 	ring = &rxr->rx_ring_struct;
4206 	rmem = &ring->ring_mem;
4207 	rmem->pg_tbl = NULL;
4208 	rmem->pg_tbl_map = 0;
4209 	for (i = 0; i < rmem->nr_pages; i++) {
4210 		rmem->pg_arr[i] = NULL;
4211 		rmem->dma_arr[i] = 0;
4212 	}
4213 	*rmem->vmem = NULL;
4214 
4215 	ring = &rxr->rx_agg_ring_struct;
4216 	rmem = &ring->ring_mem;
4217 	rmem->pg_tbl = NULL;
4218 	rmem->pg_tbl_map = 0;
4219 	for (i = 0; i < rmem->nr_pages; i++) {
4220 		rmem->pg_arr[i] = NULL;
4221 		rmem->dma_arr[i] = 0;
4222 	}
4223 	*rmem->vmem = NULL;
4224 }
4225 
4226 static void bnxt_init_ring_struct(struct bnxt *bp)
4227 {
4228 	int i, j;
4229 
4230 	for (i = 0; i < bp->cp_nr_rings; i++) {
4231 		struct bnxt_napi *bnapi = bp->bnapi[i];
4232 		struct bnxt_ring_mem_info *rmem;
4233 		struct bnxt_cp_ring_info *cpr;
4234 		struct bnxt_rx_ring_info *rxr;
4235 		struct bnxt_tx_ring_info *txr;
4236 		struct bnxt_ring_struct *ring;
4237 
4238 		if (!bnapi)
4239 			continue;
4240 
4241 		cpr = &bnapi->cp_ring;
4242 		ring = &cpr->cp_ring_struct;
4243 		rmem = &ring->ring_mem;
4244 		rmem->nr_pages = bp->cp_nr_pages;
4245 		rmem->page_size = HW_CMPD_RING_SIZE;
4246 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4247 		rmem->dma_arr = cpr->cp_desc_mapping;
4248 		rmem->vmem_size = 0;
4249 
4250 		rxr = bnapi->rx_ring;
4251 		if (!rxr)
4252 			goto skip_rx;
4253 
4254 		ring = &rxr->rx_ring_struct;
4255 		rmem = &ring->ring_mem;
4256 		rmem->nr_pages = bp->rx_nr_pages;
4257 		rmem->page_size = HW_RXBD_RING_SIZE;
4258 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4259 		rmem->dma_arr = rxr->rx_desc_mapping;
4260 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4261 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4262 
4263 		ring = &rxr->rx_agg_ring_struct;
4264 		rmem = &ring->ring_mem;
4265 		rmem->nr_pages = bp->rx_agg_nr_pages;
4266 		rmem->page_size = HW_RXBD_RING_SIZE;
4267 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4268 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4269 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4270 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4271 
4272 skip_rx:
4273 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4274 			ring = &txr->tx_ring_struct;
4275 			rmem = &ring->ring_mem;
4276 			rmem->nr_pages = bp->tx_nr_pages;
4277 			rmem->page_size = HW_TXBD_RING_SIZE;
4278 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4279 			rmem->dma_arr = txr->tx_desc_mapping;
4280 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4281 			rmem->vmem = (void **)&txr->tx_buf_ring;
4282 		}
4283 	}
4284 }
4285 
4286 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4287 {
4288 	int i;
4289 	u32 prod;
4290 	struct rx_bd **rx_buf_ring;
4291 
4292 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4293 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4294 		int j;
4295 		struct rx_bd *rxbd;
4296 
4297 		rxbd = rx_buf_ring[i];
4298 		if (!rxbd)
4299 			continue;
4300 
4301 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4302 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4303 			rxbd->rx_bd_opaque = prod;
4304 		}
4305 	}
4306 }
4307 
4308 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4309 				       struct bnxt_rx_ring_info *rxr,
4310 				       int ring_nr)
4311 {
4312 	u32 prod;
4313 	int i;
4314 
4315 	prod = rxr->rx_prod;
4316 	for (i = 0; i < bp->rx_ring_size; i++) {
4317 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4318 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4319 				    ring_nr, i, bp->rx_ring_size);
4320 			break;
4321 		}
4322 		prod = NEXT_RX(prod);
4323 	}
4324 	rxr->rx_prod = prod;
4325 }
4326 
4327 static void bnxt_alloc_one_rx_ring_page(struct bnxt *bp,
4328 					struct bnxt_rx_ring_info *rxr,
4329 					int ring_nr)
4330 {
4331 	u32 prod;
4332 	int i;
4333 
4334 	prod = rxr->rx_agg_prod;
4335 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4336 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
4337 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4338 				    ring_nr, i, bp->rx_ring_size);
4339 			break;
4340 		}
4341 		prod = NEXT_RX_AGG(prod);
4342 	}
4343 	rxr->rx_agg_prod = prod;
4344 }
4345 
4346 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4347 					struct bnxt_rx_ring_info *rxr)
4348 {
4349 	dma_addr_t mapping;
4350 	u8 *data;
4351 	int i;
4352 
4353 	for (i = 0; i < bp->max_tpa; i++) {
4354 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4355 					    GFP_KERNEL);
4356 		if (!data)
4357 			return -ENOMEM;
4358 
4359 		rxr->rx_tpa[i].data = data;
4360 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4361 		rxr->rx_tpa[i].mapping = mapping;
4362 	}
4363 
4364 	return 0;
4365 }
4366 
4367 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4368 {
4369 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4370 	int rc;
4371 
4372 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4373 
4374 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4375 		return 0;
4376 
4377 	bnxt_alloc_one_rx_ring_page(bp, rxr, ring_nr);
4378 
4379 	if (rxr->rx_tpa) {
4380 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4381 		if (rc)
4382 			return rc;
4383 	}
4384 	return 0;
4385 }
4386 
4387 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4388 				       struct bnxt_rx_ring_info *rxr)
4389 {
4390 	struct bnxt_ring_struct *ring;
4391 	u32 type;
4392 
4393 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4394 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4395 
4396 	if (NET_IP_ALIGN == 2)
4397 		type |= RX_BD_FLAGS_SOP;
4398 
4399 	ring = &rxr->rx_ring_struct;
4400 	bnxt_init_rxbd_pages(ring, type);
4401 	ring->fw_ring_id = INVALID_HW_RING_ID;
4402 }
4403 
4404 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4405 					   struct bnxt_rx_ring_info *rxr)
4406 {
4407 	struct bnxt_ring_struct *ring;
4408 	u32 type;
4409 
4410 	ring = &rxr->rx_agg_ring_struct;
4411 	ring->fw_ring_id = INVALID_HW_RING_ID;
4412 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4413 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4414 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4415 
4416 		bnxt_init_rxbd_pages(ring, type);
4417 	}
4418 }
4419 
4420 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4421 {
4422 	struct bnxt_rx_ring_info *rxr;
4423 
4424 	rxr = &bp->rx_ring[ring_nr];
4425 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4426 
4427 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4428 			     &rxr->bnapi->napi);
4429 
4430 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4431 		bpf_prog_add(bp->xdp_prog, 1);
4432 		rxr->xdp_prog = bp->xdp_prog;
4433 	}
4434 
4435 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4436 
4437 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4438 }
4439 
4440 static void bnxt_init_cp_rings(struct bnxt *bp)
4441 {
4442 	int i, j;
4443 
4444 	for (i = 0; i < bp->cp_nr_rings; i++) {
4445 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4446 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4447 
4448 		ring->fw_ring_id = INVALID_HW_RING_ID;
4449 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4450 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4451 		if (!cpr->cp_ring_arr)
4452 			continue;
4453 		for (j = 0; j < cpr->cp_ring_count; j++) {
4454 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4455 
4456 			ring = &cpr2->cp_ring_struct;
4457 			ring->fw_ring_id = INVALID_HW_RING_ID;
4458 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4459 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4460 		}
4461 	}
4462 }
4463 
4464 static int bnxt_init_rx_rings(struct bnxt *bp)
4465 {
4466 	int i, rc = 0;
4467 
4468 	if (BNXT_RX_PAGE_MODE(bp)) {
4469 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4470 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4471 	} else {
4472 		bp->rx_offset = BNXT_RX_OFFSET;
4473 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4474 	}
4475 
4476 	for (i = 0; i < bp->rx_nr_rings; i++) {
4477 		rc = bnxt_init_one_rx_ring(bp, i);
4478 		if (rc)
4479 			break;
4480 	}
4481 
4482 	return rc;
4483 }
4484 
4485 static int bnxt_init_tx_rings(struct bnxt *bp)
4486 {
4487 	u16 i;
4488 
4489 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4490 				   BNXT_MIN_TX_DESC_CNT);
4491 
4492 	for (i = 0; i < bp->tx_nr_rings; i++) {
4493 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4494 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4495 
4496 		ring->fw_ring_id = INVALID_HW_RING_ID;
4497 
4498 		if (i >= bp->tx_nr_rings_xdp)
4499 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4500 					     NETDEV_QUEUE_TYPE_TX,
4501 					     &txr->bnapi->napi);
4502 	}
4503 
4504 	return 0;
4505 }
4506 
4507 static void bnxt_free_ring_grps(struct bnxt *bp)
4508 {
4509 	kfree(bp->grp_info);
4510 	bp->grp_info = NULL;
4511 }
4512 
4513 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4514 {
4515 	int i;
4516 
4517 	if (irq_re_init) {
4518 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4519 				       sizeof(struct bnxt_ring_grp_info),
4520 				       GFP_KERNEL);
4521 		if (!bp->grp_info)
4522 			return -ENOMEM;
4523 	}
4524 	for (i = 0; i < bp->cp_nr_rings; i++) {
4525 		if (irq_re_init)
4526 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4527 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4528 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4529 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4530 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4531 	}
4532 	return 0;
4533 }
4534 
4535 static void bnxt_free_vnics(struct bnxt *bp)
4536 {
4537 	kfree(bp->vnic_info);
4538 	bp->vnic_info = NULL;
4539 	bp->nr_vnics = 0;
4540 }
4541 
4542 static int bnxt_alloc_vnics(struct bnxt *bp)
4543 {
4544 	int num_vnics = 1;
4545 
4546 #ifdef CONFIG_RFS_ACCEL
4547 	if (bp->flags & BNXT_FLAG_RFS) {
4548 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4549 			num_vnics++;
4550 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4551 			num_vnics += bp->rx_nr_rings;
4552 	}
4553 #endif
4554 
4555 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4556 		num_vnics++;
4557 
4558 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4559 				GFP_KERNEL);
4560 	if (!bp->vnic_info)
4561 		return -ENOMEM;
4562 
4563 	bp->nr_vnics = num_vnics;
4564 	return 0;
4565 }
4566 
4567 static void bnxt_init_vnics(struct bnxt *bp)
4568 {
4569 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4570 	int i;
4571 
4572 	for (i = 0; i < bp->nr_vnics; i++) {
4573 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4574 		int j;
4575 
4576 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4577 		vnic->vnic_id = i;
4578 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4579 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4580 
4581 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4582 
4583 		if (bp->vnic_info[i].rss_hash_key) {
4584 			if (i == BNXT_VNIC_DEFAULT) {
4585 				u8 *key = (void *)vnic->rss_hash_key;
4586 				int k;
4587 
4588 				if (!bp->rss_hash_key_valid &&
4589 				    !bp->rss_hash_key_updated) {
4590 					get_random_bytes(bp->rss_hash_key,
4591 							 HW_HASH_KEY_SIZE);
4592 					bp->rss_hash_key_updated = true;
4593 				}
4594 
4595 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4596 				       HW_HASH_KEY_SIZE);
4597 
4598 				if (!bp->rss_hash_key_updated)
4599 					continue;
4600 
4601 				bp->rss_hash_key_updated = false;
4602 				bp->rss_hash_key_valid = true;
4603 
4604 				bp->toeplitz_prefix = 0;
4605 				for (k = 0; k < 8; k++) {
4606 					bp->toeplitz_prefix <<= 8;
4607 					bp->toeplitz_prefix |= key[k];
4608 				}
4609 			} else {
4610 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4611 				       HW_HASH_KEY_SIZE);
4612 			}
4613 		}
4614 	}
4615 }
4616 
4617 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4618 {
4619 	int pages;
4620 
4621 	pages = ring_size / desc_per_pg;
4622 
4623 	if (!pages)
4624 		return 1;
4625 
4626 	pages++;
4627 
4628 	while (pages & (pages - 1))
4629 		pages++;
4630 
4631 	return pages;
4632 }
4633 
4634 void bnxt_set_tpa_flags(struct bnxt *bp)
4635 {
4636 	bp->flags &= ~BNXT_FLAG_TPA;
4637 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4638 		return;
4639 	if (bp->dev->features & NETIF_F_LRO)
4640 		bp->flags |= BNXT_FLAG_LRO;
4641 	else if (bp->dev->features & NETIF_F_GRO_HW)
4642 		bp->flags |= BNXT_FLAG_GRO;
4643 }
4644 
4645 static void bnxt_init_ring_params(struct bnxt *bp)
4646 {
4647 	unsigned int rx_size;
4648 
4649 	bp->rx_copybreak = BNXT_DEFAULT_RX_COPYBREAK;
4650 	/* Try to fit 4 chunks into a 4k page */
4651 	rx_size = SZ_1K -
4652 		NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4653 	bp->dev->cfg->hds_thresh = max(BNXT_DEFAULT_RX_COPYBREAK, rx_size);
4654 }
4655 
4656 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4657  * be set on entry.
4658  */
4659 void bnxt_set_ring_params(struct bnxt *bp)
4660 {
4661 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4662 	u32 agg_factor = 0, agg_ring_size = 0;
4663 
4664 	/* 8 for CRC and VLAN */
4665 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4666 
4667 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4668 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4669 
4670 	ring_size = bp->rx_ring_size;
4671 	bp->rx_agg_ring_size = 0;
4672 	bp->rx_agg_nr_pages = 0;
4673 
4674 	if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS)
4675 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4676 
4677 	bp->flags &= ~BNXT_FLAG_JUMBO;
4678 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4679 		u32 jumbo_factor;
4680 
4681 		bp->flags |= BNXT_FLAG_JUMBO;
4682 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4683 		if (jumbo_factor > agg_factor)
4684 			agg_factor = jumbo_factor;
4685 	}
4686 	if (agg_factor) {
4687 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4688 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4689 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4690 				    bp->rx_ring_size, ring_size);
4691 			bp->rx_ring_size = ring_size;
4692 		}
4693 		agg_ring_size = ring_size * agg_factor;
4694 
4695 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4696 							RX_DESC_CNT);
4697 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4698 			u32 tmp = agg_ring_size;
4699 
4700 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4701 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4702 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4703 				    tmp, agg_ring_size);
4704 		}
4705 		bp->rx_agg_ring_size = agg_ring_size;
4706 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4707 
4708 		if (BNXT_RX_PAGE_MODE(bp)) {
4709 			rx_space = PAGE_SIZE;
4710 			rx_size = PAGE_SIZE -
4711 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4712 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4713 		} else {
4714 			rx_size = max3(BNXT_DEFAULT_RX_COPYBREAK,
4715 				       bp->rx_copybreak,
4716 				       bp->dev->cfg_pending->hds_thresh);
4717 			rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN);
4718 			rx_space = rx_size + NET_SKB_PAD +
4719 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4720 		}
4721 	}
4722 
4723 	bp->rx_buf_use_size = rx_size;
4724 	bp->rx_buf_size = rx_space;
4725 
4726 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4727 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4728 
4729 	ring_size = bp->tx_ring_size;
4730 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4731 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4732 
4733 	max_rx_cmpl = bp->rx_ring_size;
4734 	/* MAX TPA needs to be added because TPA_START completions are
4735 	 * immediately recycled, so the TPA completions are not bound by
4736 	 * the RX ring size.
4737 	 */
4738 	if (bp->flags & BNXT_FLAG_TPA)
4739 		max_rx_cmpl += bp->max_tpa;
4740 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4741 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4742 	bp->cp_ring_size = ring_size;
4743 
4744 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4745 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4746 		bp->cp_nr_pages = MAX_CP_PAGES;
4747 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4748 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4749 			    ring_size, bp->cp_ring_size);
4750 	}
4751 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4752 	bp->cp_ring_mask = bp->cp_bit - 1;
4753 }
4754 
4755 /* Changing allocation mode of RX rings.
4756  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4757  */
4758 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4759 {
4760 	struct net_device *dev = bp->dev;
4761 
4762 	if (page_mode) {
4763 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
4764 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4765 
4766 		if (bp->xdp_prog->aux->xdp_has_frags)
4767 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4768 		else
4769 			dev->max_mtu =
4770 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4771 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4772 			bp->flags |= BNXT_FLAG_JUMBO;
4773 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4774 		} else {
4775 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4776 			bp->rx_skb_func = bnxt_rx_page_skb;
4777 		}
4778 		bp->rx_dir = DMA_BIDIRECTIONAL;
4779 	} else {
4780 		dev->max_mtu = bp->max_mtu;
4781 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4782 		bp->rx_dir = DMA_FROM_DEVICE;
4783 		bp->rx_skb_func = bnxt_rx_skb;
4784 	}
4785 }
4786 
4787 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4788 {
4789 	__bnxt_set_rx_skb_mode(bp, page_mode);
4790 
4791 	if (!page_mode) {
4792 		int rx, tx;
4793 
4794 		bnxt_get_max_rings(bp, &rx, &tx, true);
4795 		if (rx > 1) {
4796 			bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS;
4797 			bp->dev->hw_features |= NETIF_F_LRO;
4798 		}
4799 	}
4800 
4801 	/* Update LRO and GRO_HW availability */
4802 	netdev_update_features(bp->dev);
4803 }
4804 
4805 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4806 {
4807 	int i;
4808 	struct bnxt_vnic_info *vnic;
4809 	struct pci_dev *pdev = bp->pdev;
4810 
4811 	if (!bp->vnic_info)
4812 		return;
4813 
4814 	for (i = 0; i < bp->nr_vnics; i++) {
4815 		vnic = &bp->vnic_info[i];
4816 
4817 		kfree(vnic->fw_grp_ids);
4818 		vnic->fw_grp_ids = NULL;
4819 
4820 		kfree(vnic->uc_list);
4821 		vnic->uc_list = NULL;
4822 
4823 		if (vnic->mc_list) {
4824 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4825 					  vnic->mc_list, vnic->mc_list_mapping);
4826 			vnic->mc_list = NULL;
4827 		}
4828 
4829 		if (vnic->rss_table) {
4830 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4831 					  vnic->rss_table,
4832 					  vnic->rss_table_dma_addr);
4833 			vnic->rss_table = NULL;
4834 		}
4835 
4836 		vnic->rss_hash_key = NULL;
4837 		vnic->flags = 0;
4838 	}
4839 }
4840 
4841 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4842 {
4843 	int i, rc = 0, size;
4844 	struct bnxt_vnic_info *vnic;
4845 	struct pci_dev *pdev = bp->pdev;
4846 	int max_rings;
4847 
4848 	for (i = 0; i < bp->nr_vnics; i++) {
4849 		vnic = &bp->vnic_info[i];
4850 
4851 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4852 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4853 
4854 			if (mem_size > 0) {
4855 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4856 				if (!vnic->uc_list) {
4857 					rc = -ENOMEM;
4858 					goto out;
4859 				}
4860 			}
4861 		}
4862 
4863 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4864 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4865 			vnic->mc_list =
4866 				dma_alloc_coherent(&pdev->dev,
4867 						   vnic->mc_list_size,
4868 						   &vnic->mc_list_mapping,
4869 						   GFP_KERNEL);
4870 			if (!vnic->mc_list) {
4871 				rc = -ENOMEM;
4872 				goto out;
4873 			}
4874 		}
4875 
4876 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4877 			goto vnic_skip_grps;
4878 
4879 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4880 			max_rings = bp->rx_nr_rings;
4881 		else
4882 			max_rings = 1;
4883 
4884 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4885 		if (!vnic->fw_grp_ids) {
4886 			rc = -ENOMEM;
4887 			goto out;
4888 		}
4889 vnic_skip_grps:
4890 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4891 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4892 			continue;
4893 
4894 		/* Allocate rss table and hash key */
4895 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4896 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4897 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4898 
4899 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4900 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4901 						     vnic->rss_table_size,
4902 						     &vnic->rss_table_dma_addr,
4903 						     GFP_KERNEL);
4904 		if (!vnic->rss_table) {
4905 			rc = -ENOMEM;
4906 			goto out;
4907 		}
4908 
4909 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4910 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4911 	}
4912 	return 0;
4913 
4914 out:
4915 	return rc;
4916 }
4917 
4918 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4919 {
4920 	struct bnxt_hwrm_wait_token *token;
4921 
4922 	dma_pool_destroy(bp->hwrm_dma_pool);
4923 	bp->hwrm_dma_pool = NULL;
4924 
4925 	rcu_read_lock();
4926 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4927 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4928 	rcu_read_unlock();
4929 }
4930 
4931 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4932 {
4933 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4934 					    BNXT_HWRM_DMA_SIZE,
4935 					    BNXT_HWRM_DMA_ALIGN, 0);
4936 	if (!bp->hwrm_dma_pool)
4937 		return -ENOMEM;
4938 
4939 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4940 
4941 	return 0;
4942 }
4943 
4944 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4945 {
4946 	kfree(stats->hw_masks);
4947 	stats->hw_masks = NULL;
4948 	kfree(stats->sw_stats);
4949 	stats->sw_stats = NULL;
4950 	if (stats->hw_stats) {
4951 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
4952 				  stats->hw_stats_map);
4953 		stats->hw_stats = NULL;
4954 	}
4955 }
4956 
4957 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
4958 				bool alloc_masks)
4959 {
4960 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
4961 					     &stats->hw_stats_map, GFP_KERNEL);
4962 	if (!stats->hw_stats)
4963 		return -ENOMEM;
4964 
4965 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
4966 	if (!stats->sw_stats)
4967 		goto stats_mem_err;
4968 
4969 	if (alloc_masks) {
4970 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
4971 		if (!stats->hw_masks)
4972 			goto stats_mem_err;
4973 	}
4974 	return 0;
4975 
4976 stats_mem_err:
4977 	bnxt_free_stats_mem(bp, stats);
4978 	return -ENOMEM;
4979 }
4980 
4981 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
4982 {
4983 	int i;
4984 
4985 	for (i = 0; i < count; i++)
4986 		mask_arr[i] = mask;
4987 }
4988 
4989 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
4990 {
4991 	int i;
4992 
4993 	for (i = 0; i < count; i++)
4994 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
4995 }
4996 
4997 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
4998 				    struct bnxt_stats_mem *stats)
4999 {
5000 	struct hwrm_func_qstats_ext_output *resp;
5001 	struct hwrm_func_qstats_ext_input *req;
5002 	__le64 *hw_masks;
5003 	int rc;
5004 
5005 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
5006 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5007 		return -EOPNOTSUPP;
5008 
5009 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
5010 	if (rc)
5011 		return rc;
5012 
5013 	req->fid = cpu_to_le16(0xffff);
5014 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5015 
5016 	resp = hwrm_req_hold(bp, req);
5017 	rc = hwrm_req_send(bp, req);
5018 	if (!rc) {
5019 		hw_masks = &resp->rx_ucast_pkts;
5020 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
5021 	}
5022 	hwrm_req_drop(bp, req);
5023 	return rc;
5024 }
5025 
5026 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
5027 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
5028 
5029 static void bnxt_init_stats(struct bnxt *bp)
5030 {
5031 	struct bnxt_napi *bnapi = bp->bnapi[0];
5032 	struct bnxt_cp_ring_info *cpr;
5033 	struct bnxt_stats_mem *stats;
5034 	__le64 *rx_stats, *tx_stats;
5035 	int rc, rx_count, tx_count;
5036 	u64 *rx_masks, *tx_masks;
5037 	u64 mask;
5038 	u8 flags;
5039 
5040 	cpr = &bnapi->cp_ring;
5041 	stats = &cpr->stats;
5042 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
5043 	if (rc) {
5044 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5045 			mask = (1ULL << 48) - 1;
5046 		else
5047 			mask = -1ULL;
5048 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
5049 	}
5050 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
5051 		stats = &bp->port_stats;
5052 		rx_stats = stats->hw_stats;
5053 		rx_masks = stats->hw_masks;
5054 		rx_count = sizeof(struct rx_port_stats) / 8;
5055 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5056 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5057 		tx_count = sizeof(struct tx_port_stats) / 8;
5058 
5059 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
5060 		rc = bnxt_hwrm_port_qstats(bp, flags);
5061 		if (rc) {
5062 			mask = (1ULL << 40) - 1;
5063 
5064 			bnxt_fill_masks(rx_masks, mask, rx_count);
5065 			bnxt_fill_masks(tx_masks, mask, tx_count);
5066 		} else {
5067 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5068 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5069 			bnxt_hwrm_port_qstats(bp, 0);
5070 		}
5071 	}
5072 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5073 		stats = &bp->rx_port_stats_ext;
5074 		rx_stats = stats->hw_stats;
5075 		rx_masks = stats->hw_masks;
5076 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5077 		stats = &bp->tx_port_stats_ext;
5078 		tx_stats = stats->hw_stats;
5079 		tx_masks = stats->hw_masks;
5080 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5081 
5082 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5083 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5084 		if (rc) {
5085 			mask = (1ULL << 40) - 1;
5086 
5087 			bnxt_fill_masks(rx_masks, mask, rx_count);
5088 			if (tx_stats)
5089 				bnxt_fill_masks(tx_masks, mask, tx_count);
5090 		} else {
5091 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5092 			if (tx_stats)
5093 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5094 						   tx_count);
5095 			bnxt_hwrm_port_qstats_ext(bp, 0);
5096 		}
5097 	}
5098 }
5099 
5100 static void bnxt_free_port_stats(struct bnxt *bp)
5101 {
5102 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5103 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5104 
5105 	bnxt_free_stats_mem(bp, &bp->port_stats);
5106 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5107 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5108 }
5109 
5110 static void bnxt_free_ring_stats(struct bnxt *bp)
5111 {
5112 	int i;
5113 
5114 	if (!bp->bnapi)
5115 		return;
5116 
5117 	for (i = 0; i < bp->cp_nr_rings; i++) {
5118 		struct bnxt_napi *bnapi = bp->bnapi[i];
5119 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5120 
5121 		bnxt_free_stats_mem(bp, &cpr->stats);
5122 
5123 		kfree(cpr->sw_stats);
5124 		cpr->sw_stats = NULL;
5125 	}
5126 }
5127 
5128 static int bnxt_alloc_stats(struct bnxt *bp)
5129 {
5130 	u32 size, i;
5131 	int rc;
5132 
5133 	size = bp->hw_ring_stats_size;
5134 
5135 	for (i = 0; i < bp->cp_nr_rings; i++) {
5136 		struct bnxt_napi *bnapi = bp->bnapi[i];
5137 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5138 
5139 		cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL);
5140 		if (!cpr->sw_stats)
5141 			return -ENOMEM;
5142 
5143 		cpr->stats.len = size;
5144 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5145 		if (rc)
5146 			return rc;
5147 
5148 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5149 	}
5150 
5151 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5152 		return 0;
5153 
5154 	if (bp->port_stats.hw_stats)
5155 		goto alloc_ext_stats;
5156 
5157 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5158 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5159 	if (rc)
5160 		return rc;
5161 
5162 	bp->flags |= BNXT_FLAG_PORT_STATS;
5163 
5164 alloc_ext_stats:
5165 	/* Display extended statistics only if FW supports it */
5166 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5167 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5168 			return 0;
5169 
5170 	if (bp->rx_port_stats_ext.hw_stats)
5171 		goto alloc_tx_ext_stats;
5172 
5173 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5174 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5175 	/* Extended stats are optional */
5176 	if (rc)
5177 		return 0;
5178 
5179 alloc_tx_ext_stats:
5180 	if (bp->tx_port_stats_ext.hw_stats)
5181 		return 0;
5182 
5183 	if (bp->hwrm_spec_code >= 0x10902 ||
5184 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5185 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5186 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5187 		/* Extended stats are optional */
5188 		if (rc)
5189 			return 0;
5190 	}
5191 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5192 	return 0;
5193 }
5194 
5195 static void bnxt_clear_ring_indices(struct bnxt *bp)
5196 {
5197 	int i, j;
5198 
5199 	if (!bp->bnapi)
5200 		return;
5201 
5202 	for (i = 0; i < bp->cp_nr_rings; i++) {
5203 		struct bnxt_napi *bnapi = bp->bnapi[i];
5204 		struct bnxt_cp_ring_info *cpr;
5205 		struct bnxt_rx_ring_info *rxr;
5206 		struct bnxt_tx_ring_info *txr;
5207 
5208 		if (!bnapi)
5209 			continue;
5210 
5211 		cpr = &bnapi->cp_ring;
5212 		cpr->cp_raw_cons = 0;
5213 
5214 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5215 			txr->tx_prod = 0;
5216 			txr->tx_cons = 0;
5217 			txr->tx_hw_cons = 0;
5218 		}
5219 
5220 		rxr = bnapi->rx_ring;
5221 		if (rxr) {
5222 			rxr->rx_prod = 0;
5223 			rxr->rx_agg_prod = 0;
5224 			rxr->rx_sw_agg_prod = 0;
5225 			rxr->rx_next_cons = 0;
5226 		}
5227 		bnapi->events = 0;
5228 	}
5229 }
5230 
5231 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5232 {
5233 	u8 type = fltr->type, flags = fltr->flags;
5234 
5235 	INIT_LIST_HEAD(&fltr->list);
5236 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5237 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5238 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5239 }
5240 
5241 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5242 {
5243 	if (!list_empty(&fltr->list))
5244 		list_del_init(&fltr->list);
5245 }
5246 
5247 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5248 {
5249 	struct bnxt_filter_base *usr_fltr, *tmp;
5250 
5251 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5252 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5253 			continue;
5254 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5255 	}
5256 }
5257 
5258 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5259 {
5260 	hlist_del(&fltr->hash);
5261 	bnxt_del_one_usr_fltr(bp, fltr);
5262 	if (fltr->flags) {
5263 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5264 		bp->ntp_fltr_count--;
5265 	}
5266 	kfree(fltr);
5267 }
5268 
5269 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5270 {
5271 	int i;
5272 
5273 	netdev_assert_locked(bp->dev);
5274 
5275 	/* Under netdev instance lock and all our NAPIs have been disabled.
5276 	 * It's safe to delete the hash table.
5277 	 */
5278 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5279 		struct hlist_head *head;
5280 		struct hlist_node *tmp;
5281 		struct bnxt_ntuple_filter *fltr;
5282 
5283 		head = &bp->ntp_fltr_hash_tbl[i];
5284 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5285 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5286 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5287 				     !list_empty(&fltr->base.list)))
5288 				continue;
5289 			bnxt_del_fltr(bp, &fltr->base);
5290 		}
5291 	}
5292 	if (!all)
5293 		return;
5294 
5295 	bitmap_free(bp->ntp_fltr_bmap);
5296 	bp->ntp_fltr_bmap = NULL;
5297 	bp->ntp_fltr_count = 0;
5298 }
5299 
5300 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5301 {
5302 	int i, rc = 0;
5303 
5304 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5305 		return 0;
5306 
5307 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5308 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5309 
5310 	bp->ntp_fltr_count = 0;
5311 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5312 
5313 	if (!bp->ntp_fltr_bmap)
5314 		rc = -ENOMEM;
5315 
5316 	return rc;
5317 }
5318 
5319 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5320 {
5321 	int i;
5322 
5323 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5324 		struct hlist_head *head;
5325 		struct hlist_node *tmp;
5326 		struct bnxt_l2_filter *fltr;
5327 
5328 		head = &bp->l2_fltr_hash_tbl[i];
5329 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5330 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5331 				     !list_empty(&fltr->base.list)))
5332 				continue;
5333 			bnxt_del_fltr(bp, &fltr->base);
5334 		}
5335 	}
5336 }
5337 
5338 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5339 {
5340 	int i;
5341 
5342 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5343 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5344 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5345 }
5346 
5347 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5348 {
5349 	bnxt_free_vnic_attributes(bp);
5350 	bnxt_free_tx_rings(bp);
5351 	bnxt_free_rx_rings(bp);
5352 	bnxt_free_cp_rings(bp);
5353 	bnxt_free_all_cp_arrays(bp);
5354 	bnxt_free_ntp_fltrs(bp, false);
5355 	bnxt_free_l2_filters(bp, false);
5356 	if (irq_re_init) {
5357 		bnxt_free_ring_stats(bp);
5358 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5359 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5360 			bnxt_free_port_stats(bp);
5361 		bnxt_free_ring_grps(bp);
5362 		bnxt_free_vnics(bp);
5363 		kfree(bp->tx_ring_map);
5364 		bp->tx_ring_map = NULL;
5365 		kfree(bp->tx_ring);
5366 		bp->tx_ring = NULL;
5367 		kfree(bp->rx_ring);
5368 		bp->rx_ring = NULL;
5369 		kfree(bp->bnapi);
5370 		bp->bnapi = NULL;
5371 	} else {
5372 		bnxt_clear_ring_indices(bp);
5373 	}
5374 }
5375 
5376 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5377 {
5378 	int i, j, rc, size, arr_size;
5379 	void *bnapi;
5380 
5381 	if (irq_re_init) {
5382 		/* Allocate bnapi mem pointer array and mem block for
5383 		 * all queues
5384 		 */
5385 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5386 				bp->cp_nr_rings);
5387 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5388 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5389 		if (!bnapi)
5390 			return -ENOMEM;
5391 
5392 		bp->bnapi = bnapi;
5393 		bnapi += arr_size;
5394 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5395 			bp->bnapi[i] = bnapi;
5396 			bp->bnapi[i]->index = i;
5397 			bp->bnapi[i]->bp = bp;
5398 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5399 				struct bnxt_cp_ring_info *cpr =
5400 					&bp->bnapi[i]->cp_ring;
5401 
5402 				cpr->cp_ring_struct.ring_mem.flags =
5403 					BNXT_RMEM_RING_PTE_FLAG;
5404 			}
5405 		}
5406 
5407 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5408 				      sizeof(struct bnxt_rx_ring_info),
5409 				      GFP_KERNEL);
5410 		if (!bp->rx_ring)
5411 			return -ENOMEM;
5412 
5413 		for (i = 0; i < bp->rx_nr_rings; i++) {
5414 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5415 
5416 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5417 				rxr->rx_ring_struct.ring_mem.flags =
5418 					BNXT_RMEM_RING_PTE_FLAG;
5419 				rxr->rx_agg_ring_struct.ring_mem.flags =
5420 					BNXT_RMEM_RING_PTE_FLAG;
5421 			} else {
5422 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5423 			}
5424 			rxr->bnapi = bp->bnapi[i];
5425 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5426 		}
5427 
5428 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5429 				      sizeof(struct bnxt_tx_ring_info),
5430 				      GFP_KERNEL);
5431 		if (!bp->tx_ring)
5432 			return -ENOMEM;
5433 
5434 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5435 					  GFP_KERNEL);
5436 
5437 		if (!bp->tx_ring_map)
5438 			return -ENOMEM;
5439 
5440 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5441 			j = 0;
5442 		else
5443 			j = bp->rx_nr_rings;
5444 
5445 		for (i = 0; i < bp->tx_nr_rings; i++) {
5446 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5447 			struct bnxt_napi *bnapi2;
5448 
5449 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5450 				txr->tx_ring_struct.ring_mem.flags =
5451 					BNXT_RMEM_RING_PTE_FLAG;
5452 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5453 			if (i >= bp->tx_nr_rings_xdp) {
5454 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5455 
5456 				bnapi2 = bp->bnapi[k];
5457 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5458 				txr->tx_napi_idx =
5459 					BNXT_RING_TO_TC(bp, txr->txq_index);
5460 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5461 				bnapi2->tx_int = bnxt_tx_int;
5462 			} else {
5463 				bnapi2 = bp->bnapi[j];
5464 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5465 				bnapi2->tx_ring[0] = txr;
5466 				bnapi2->tx_int = bnxt_tx_int_xdp;
5467 				j++;
5468 			}
5469 			txr->bnapi = bnapi2;
5470 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5471 				txr->tx_cpr = &bnapi2->cp_ring;
5472 		}
5473 
5474 		rc = bnxt_alloc_stats(bp);
5475 		if (rc)
5476 			goto alloc_mem_err;
5477 		bnxt_init_stats(bp);
5478 
5479 		rc = bnxt_alloc_ntp_fltrs(bp);
5480 		if (rc)
5481 			goto alloc_mem_err;
5482 
5483 		rc = bnxt_alloc_vnics(bp);
5484 		if (rc)
5485 			goto alloc_mem_err;
5486 	}
5487 
5488 	rc = bnxt_alloc_all_cp_arrays(bp);
5489 	if (rc)
5490 		goto alloc_mem_err;
5491 
5492 	bnxt_init_ring_struct(bp);
5493 
5494 	rc = bnxt_alloc_rx_rings(bp);
5495 	if (rc)
5496 		goto alloc_mem_err;
5497 
5498 	rc = bnxt_alloc_tx_rings(bp);
5499 	if (rc)
5500 		goto alloc_mem_err;
5501 
5502 	rc = bnxt_alloc_cp_rings(bp);
5503 	if (rc)
5504 		goto alloc_mem_err;
5505 
5506 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5507 						  BNXT_VNIC_MCAST_FLAG |
5508 						  BNXT_VNIC_UCAST_FLAG;
5509 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5510 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5511 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5512 
5513 	rc = bnxt_alloc_vnic_attributes(bp);
5514 	if (rc)
5515 		goto alloc_mem_err;
5516 	return 0;
5517 
5518 alloc_mem_err:
5519 	bnxt_free_mem(bp, true);
5520 	return rc;
5521 }
5522 
5523 static void bnxt_disable_int(struct bnxt *bp)
5524 {
5525 	int i;
5526 
5527 	if (!bp->bnapi)
5528 		return;
5529 
5530 	for (i = 0; i < bp->cp_nr_rings; i++) {
5531 		struct bnxt_napi *bnapi = bp->bnapi[i];
5532 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5533 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5534 
5535 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5536 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5537 	}
5538 }
5539 
5540 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5541 {
5542 	struct bnxt_napi *bnapi = bp->bnapi[n];
5543 	struct bnxt_cp_ring_info *cpr;
5544 
5545 	cpr = &bnapi->cp_ring;
5546 	return cpr->cp_ring_struct.map_idx;
5547 }
5548 
5549 static void bnxt_disable_int_sync(struct bnxt *bp)
5550 {
5551 	int i;
5552 
5553 	if (!bp->irq_tbl)
5554 		return;
5555 
5556 	atomic_inc(&bp->intr_sem);
5557 
5558 	bnxt_disable_int(bp);
5559 	for (i = 0; i < bp->cp_nr_rings; i++) {
5560 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5561 
5562 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5563 	}
5564 }
5565 
5566 static void bnxt_enable_int(struct bnxt *bp)
5567 {
5568 	int i;
5569 
5570 	atomic_set(&bp->intr_sem, 0);
5571 	for (i = 0; i < bp->cp_nr_rings; i++) {
5572 		struct bnxt_napi *bnapi = bp->bnapi[i];
5573 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5574 
5575 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5576 	}
5577 }
5578 
5579 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5580 			    bool async_only)
5581 {
5582 	DECLARE_BITMAP(async_events_bmap, 256);
5583 	u32 *events = (u32 *)async_events_bmap;
5584 	struct hwrm_func_drv_rgtr_output *resp;
5585 	struct hwrm_func_drv_rgtr_input *req;
5586 	u32 flags;
5587 	int rc, i;
5588 
5589 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5590 	if (rc)
5591 		return rc;
5592 
5593 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5594 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5595 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5596 
5597 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5598 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5599 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5600 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5601 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5602 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5603 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5604 	if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2)
5605 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT;
5606 	req->flags = cpu_to_le32(flags);
5607 	req->ver_maj_8b = DRV_VER_MAJ;
5608 	req->ver_min_8b = DRV_VER_MIN;
5609 	req->ver_upd_8b = DRV_VER_UPD;
5610 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5611 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5612 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5613 
5614 	if (BNXT_PF(bp)) {
5615 		u32 data[8];
5616 		int i;
5617 
5618 		memset(data, 0, sizeof(data));
5619 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5620 			u16 cmd = bnxt_vf_req_snif[i];
5621 			unsigned int bit, idx;
5622 
5623 			idx = cmd / 32;
5624 			bit = cmd % 32;
5625 			data[idx] |= 1 << bit;
5626 		}
5627 
5628 		for (i = 0; i < 8; i++)
5629 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5630 
5631 		req->enables |=
5632 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5633 	}
5634 
5635 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5636 		req->flags |= cpu_to_le32(
5637 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5638 
5639 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5640 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5641 		u16 event_id = bnxt_async_events_arr[i];
5642 
5643 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5644 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5645 			continue;
5646 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5647 		    !bp->ptp_cfg)
5648 			continue;
5649 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5650 	}
5651 	if (bmap && bmap_size) {
5652 		for (i = 0; i < bmap_size; i++) {
5653 			if (test_bit(i, bmap))
5654 				__set_bit(i, async_events_bmap);
5655 		}
5656 	}
5657 	for (i = 0; i < 8; i++)
5658 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5659 
5660 	if (async_only)
5661 		req->enables =
5662 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5663 
5664 	resp = hwrm_req_hold(bp, req);
5665 	rc = hwrm_req_send(bp, req);
5666 	if (!rc) {
5667 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5668 		if (resp->flags &
5669 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5670 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5671 	}
5672 	hwrm_req_drop(bp, req);
5673 	return rc;
5674 }
5675 
5676 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5677 {
5678 	struct hwrm_func_drv_unrgtr_input *req;
5679 	int rc;
5680 
5681 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5682 		return 0;
5683 
5684 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5685 	if (rc)
5686 		return rc;
5687 	return hwrm_req_send(bp, req);
5688 }
5689 
5690 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5691 
5692 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5693 {
5694 	struct hwrm_tunnel_dst_port_free_input *req;
5695 	int rc;
5696 
5697 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5698 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5699 		return 0;
5700 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5701 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5702 		return 0;
5703 
5704 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5705 	if (rc)
5706 		return rc;
5707 
5708 	req->tunnel_type = tunnel_type;
5709 
5710 	switch (tunnel_type) {
5711 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5712 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5713 		bp->vxlan_port = 0;
5714 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5715 		break;
5716 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5717 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5718 		bp->nge_port = 0;
5719 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5720 		break;
5721 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5722 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5723 		bp->vxlan_gpe_port = 0;
5724 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5725 		break;
5726 	default:
5727 		break;
5728 	}
5729 
5730 	rc = hwrm_req_send(bp, req);
5731 	if (rc)
5732 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5733 			   rc);
5734 	if (bp->flags & BNXT_FLAG_TPA)
5735 		bnxt_set_tpa(bp, true);
5736 	return rc;
5737 }
5738 
5739 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5740 					   u8 tunnel_type)
5741 {
5742 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5743 	struct hwrm_tunnel_dst_port_alloc_input *req;
5744 	int rc;
5745 
5746 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5747 	if (rc)
5748 		return rc;
5749 
5750 	req->tunnel_type = tunnel_type;
5751 	req->tunnel_dst_port_val = port;
5752 
5753 	resp = hwrm_req_hold(bp, req);
5754 	rc = hwrm_req_send(bp, req);
5755 	if (rc) {
5756 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5757 			   rc);
5758 		goto err_out;
5759 	}
5760 
5761 	switch (tunnel_type) {
5762 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5763 		bp->vxlan_port = port;
5764 		bp->vxlan_fw_dst_port_id =
5765 			le16_to_cpu(resp->tunnel_dst_port_id);
5766 		break;
5767 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5768 		bp->nge_port = port;
5769 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5770 		break;
5771 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5772 		bp->vxlan_gpe_port = port;
5773 		bp->vxlan_gpe_fw_dst_port_id =
5774 			le16_to_cpu(resp->tunnel_dst_port_id);
5775 		break;
5776 	default:
5777 		break;
5778 	}
5779 	if (bp->flags & BNXT_FLAG_TPA)
5780 		bnxt_set_tpa(bp, true);
5781 
5782 err_out:
5783 	hwrm_req_drop(bp, req);
5784 	return rc;
5785 }
5786 
5787 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5788 {
5789 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5790 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5791 	int rc;
5792 
5793 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5794 	if (rc)
5795 		return rc;
5796 
5797 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5798 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5799 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5800 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5801 	}
5802 	req->mask = cpu_to_le32(vnic->rx_mask);
5803 	return hwrm_req_send_silent(bp, req);
5804 }
5805 
5806 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5807 {
5808 	if (!atomic_dec_and_test(&fltr->refcnt))
5809 		return;
5810 	spin_lock_bh(&bp->ntp_fltr_lock);
5811 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5812 		spin_unlock_bh(&bp->ntp_fltr_lock);
5813 		return;
5814 	}
5815 	hlist_del_rcu(&fltr->base.hash);
5816 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5817 	if (fltr->base.flags) {
5818 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5819 		bp->ntp_fltr_count--;
5820 	}
5821 	spin_unlock_bh(&bp->ntp_fltr_lock);
5822 	kfree_rcu(fltr, base.rcu);
5823 }
5824 
5825 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5826 						      struct bnxt_l2_key *key,
5827 						      u32 idx)
5828 {
5829 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5830 	struct bnxt_l2_filter *fltr;
5831 
5832 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5833 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5834 
5835 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5836 		    l2_key->vlan == key->vlan)
5837 			return fltr;
5838 	}
5839 	return NULL;
5840 }
5841 
5842 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5843 						    struct bnxt_l2_key *key,
5844 						    u32 idx)
5845 {
5846 	struct bnxt_l2_filter *fltr = NULL;
5847 
5848 	rcu_read_lock();
5849 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5850 	if (fltr)
5851 		atomic_inc(&fltr->refcnt);
5852 	rcu_read_unlock();
5853 	return fltr;
5854 }
5855 
5856 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5857 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5858 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5859 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5860 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5861 
5862 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5863 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5864 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5865 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5866 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5867 
5868 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5869 {
5870 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5871 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5872 			return sizeof(fkeys->addrs.v4addrs) +
5873 			       sizeof(fkeys->ports);
5874 
5875 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5876 			return sizeof(fkeys->addrs.v4addrs);
5877 	}
5878 
5879 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5880 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5881 			return sizeof(fkeys->addrs.v6addrs) +
5882 			       sizeof(fkeys->ports);
5883 
5884 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5885 			return sizeof(fkeys->addrs.v6addrs);
5886 	}
5887 
5888 	return 0;
5889 }
5890 
5891 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5892 			 const unsigned char *key)
5893 {
5894 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5895 	struct bnxt_ipv4_tuple tuple4;
5896 	struct bnxt_ipv6_tuple tuple6;
5897 	int i, j, len = 0;
5898 	u8 *four_tuple;
5899 
5900 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5901 	if (!len)
5902 		return 0;
5903 
5904 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5905 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5906 		tuple4.ports = fkeys->ports;
5907 		four_tuple = (unsigned char *)&tuple4;
5908 	} else {
5909 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5910 		tuple6.ports = fkeys->ports;
5911 		four_tuple = (unsigned char *)&tuple6;
5912 	}
5913 
5914 	for (i = 0, j = 8; i < len; i++, j++) {
5915 		u8 byte = four_tuple[i];
5916 		int bit;
5917 
5918 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5919 			if (byte & 0x80)
5920 				hash ^= prefix;
5921 		}
5922 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5923 	}
5924 
5925 	/* The valid part of the hash is in the upper 32 bits. */
5926 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5927 }
5928 
5929 #ifdef CONFIG_RFS_ACCEL
5930 static struct bnxt_l2_filter *
5931 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5932 {
5933 	struct bnxt_l2_filter *fltr;
5934 	u32 idx;
5935 
5936 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5937 	      BNXT_L2_FLTR_HASH_MASK;
5938 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5939 	return fltr;
5940 }
5941 #endif
5942 
5943 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
5944 			       struct bnxt_l2_key *key, u32 idx)
5945 {
5946 	struct hlist_head *head;
5947 
5948 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
5949 	fltr->l2_key.vlan = key->vlan;
5950 	fltr->base.type = BNXT_FLTR_TYPE_L2;
5951 	if (fltr->base.flags) {
5952 		int bit_id;
5953 
5954 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
5955 						 bp->max_fltr, 0);
5956 		if (bit_id < 0)
5957 			return -ENOMEM;
5958 		fltr->base.sw_id = (u16)bit_id;
5959 		bp->ntp_fltr_count++;
5960 	}
5961 	head = &bp->l2_fltr_hash_tbl[idx];
5962 	hlist_add_head_rcu(&fltr->base.hash, head);
5963 	bnxt_insert_usr_fltr(bp, &fltr->base);
5964 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
5965 	atomic_set(&fltr->refcnt, 1);
5966 	return 0;
5967 }
5968 
5969 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
5970 						   struct bnxt_l2_key *key,
5971 						   gfp_t gfp)
5972 {
5973 	struct bnxt_l2_filter *fltr;
5974 	u32 idx;
5975 	int rc;
5976 
5977 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5978 	      BNXT_L2_FLTR_HASH_MASK;
5979 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5980 	if (fltr)
5981 		return fltr;
5982 
5983 	fltr = kzalloc(sizeof(*fltr), gfp);
5984 	if (!fltr)
5985 		return ERR_PTR(-ENOMEM);
5986 	spin_lock_bh(&bp->ntp_fltr_lock);
5987 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5988 	spin_unlock_bh(&bp->ntp_fltr_lock);
5989 	if (rc) {
5990 		bnxt_del_l2_filter(bp, fltr);
5991 		fltr = ERR_PTR(rc);
5992 	}
5993 	return fltr;
5994 }
5995 
5996 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
5997 						struct bnxt_l2_key *key,
5998 						u16 flags)
5999 {
6000 	struct bnxt_l2_filter *fltr;
6001 	u32 idx;
6002 	int rc;
6003 
6004 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6005 	      BNXT_L2_FLTR_HASH_MASK;
6006 	spin_lock_bh(&bp->ntp_fltr_lock);
6007 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6008 	if (fltr) {
6009 		fltr = ERR_PTR(-EEXIST);
6010 		goto l2_filter_exit;
6011 	}
6012 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
6013 	if (!fltr) {
6014 		fltr = ERR_PTR(-ENOMEM);
6015 		goto l2_filter_exit;
6016 	}
6017 	fltr->base.flags = flags;
6018 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6019 	if (rc) {
6020 		spin_unlock_bh(&bp->ntp_fltr_lock);
6021 		bnxt_del_l2_filter(bp, fltr);
6022 		return ERR_PTR(rc);
6023 	}
6024 
6025 l2_filter_exit:
6026 	spin_unlock_bh(&bp->ntp_fltr_lock);
6027 	return fltr;
6028 }
6029 
6030 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
6031 {
6032 #ifdef CONFIG_BNXT_SRIOV
6033 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
6034 
6035 	return vf->fw_fid;
6036 #else
6037 	return INVALID_HW_RING_ID;
6038 #endif
6039 }
6040 
6041 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6042 {
6043 	struct hwrm_cfa_l2_filter_free_input *req;
6044 	u16 target_id = 0xffff;
6045 	int rc;
6046 
6047 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6048 		struct bnxt_pf_info *pf = &bp->pf;
6049 
6050 		if (fltr->base.vf_idx >= pf->active_vfs)
6051 			return -EINVAL;
6052 
6053 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6054 		if (target_id == INVALID_HW_RING_ID)
6055 			return -EINVAL;
6056 	}
6057 
6058 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
6059 	if (rc)
6060 		return rc;
6061 
6062 	req->target_id = cpu_to_le16(target_id);
6063 	req->l2_filter_id = fltr->base.filter_id;
6064 	return hwrm_req_send(bp, req);
6065 }
6066 
6067 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6068 {
6069 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6070 	struct hwrm_cfa_l2_filter_alloc_input *req;
6071 	u16 target_id = 0xffff;
6072 	int rc;
6073 
6074 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6075 		struct bnxt_pf_info *pf = &bp->pf;
6076 
6077 		if (fltr->base.vf_idx >= pf->active_vfs)
6078 			return -EINVAL;
6079 
6080 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6081 	}
6082 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6083 	if (rc)
6084 		return rc;
6085 
6086 	req->target_id = cpu_to_le16(target_id);
6087 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6088 
6089 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6090 		req->flags |=
6091 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6092 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6093 	req->enables =
6094 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6095 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6096 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6097 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6098 	eth_broadcast_addr(req->l2_addr_mask);
6099 
6100 	if (fltr->l2_key.vlan) {
6101 		req->enables |=
6102 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6103 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6104 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6105 		req->num_vlans = 1;
6106 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6107 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6108 	}
6109 
6110 	resp = hwrm_req_hold(bp, req);
6111 	rc = hwrm_req_send(bp, req);
6112 	if (!rc) {
6113 		fltr->base.filter_id = resp->l2_filter_id;
6114 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6115 	}
6116 	hwrm_req_drop(bp, req);
6117 	return rc;
6118 }
6119 
6120 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6121 				     struct bnxt_ntuple_filter *fltr)
6122 {
6123 	struct hwrm_cfa_ntuple_filter_free_input *req;
6124 	int rc;
6125 
6126 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6127 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6128 	if (rc)
6129 		return rc;
6130 
6131 	req->ntuple_filter_id = fltr->base.filter_id;
6132 	return hwrm_req_send(bp, req);
6133 }
6134 
6135 #define BNXT_NTP_FLTR_FLAGS					\
6136 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6137 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6138 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6139 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6140 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6141 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6142 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6143 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6144 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6145 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6146 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6147 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6148 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6149 
6150 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6151 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6152 
6153 void bnxt_fill_ipv6_mask(__be32 mask[4])
6154 {
6155 	int i;
6156 
6157 	for (i = 0; i < 4; i++)
6158 		mask[i] = cpu_to_be32(~0);
6159 }
6160 
6161 static void
6162 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6163 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6164 			  struct bnxt_ntuple_filter *fltr)
6165 {
6166 	u16 rxq = fltr->base.rxq;
6167 
6168 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6169 		struct ethtool_rxfh_context *ctx;
6170 		struct bnxt_rss_ctx *rss_ctx;
6171 		struct bnxt_vnic_info *vnic;
6172 
6173 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6174 			      fltr->base.fw_vnic_id);
6175 		if (ctx) {
6176 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6177 			vnic = &rss_ctx->vnic;
6178 
6179 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6180 		}
6181 		return;
6182 	}
6183 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6184 		struct bnxt_vnic_info *vnic;
6185 		u32 enables;
6186 
6187 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6188 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6189 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6190 		req->enables |= cpu_to_le32(enables);
6191 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6192 	} else {
6193 		u32 flags;
6194 
6195 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6196 		req->flags |= cpu_to_le32(flags);
6197 		req->dst_id = cpu_to_le16(rxq);
6198 	}
6199 }
6200 
6201 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6202 				      struct bnxt_ntuple_filter *fltr)
6203 {
6204 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6205 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6206 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6207 	struct flow_keys *keys = &fltr->fkeys;
6208 	struct bnxt_l2_filter *l2_fltr;
6209 	struct bnxt_vnic_info *vnic;
6210 	int rc;
6211 
6212 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6213 	if (rc)
6214 		return rc;
6215 
6216 	l2_fltr = fltr->l2_fltr;
6217 	req->l2_filter_id = l2_fltr->base.filter_id;
6218 
6219 	if (fltr->base.flags & BNXT_ACT_DROP) {
6220 		req->flags =
6221 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6222 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6223 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6224 	} else {
6225 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6226 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6227 	}
6228 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6229 
6230 	req->ethertype = htons(ETH_P_IP);
6231 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6232 	req->ip_protocol = keys->basic.ip_proto;
6233 
6234 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6235 		req->ethertype = htons(ETH_P_IPV6);
6236 		req->ip_addr_type =
6237 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6238 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6239 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6240 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6241 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6242 	} else {
6243 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6244 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6245 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6246 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6247 	}
6248 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6249 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6250 		req->tunnel_type =
6251 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6252 	}
6253 
6254 	req->src_port = keys->ports.src;
6255 	req->src_port_mask = masks->ports.src;
6256 	req->dst_port = keys->ports.dst;
6257 	req->dst_port_mask = masks->ports.dst;
6258 
6259 	resp = hwrm_req_hold(bp, req);
6260 	rc = hwrm_req_send(bp, req);
6261 	if (!rc)
6262 		fltr->base.filter_id = resp->ntuple_filter_id;
6263 	hwrm_req_drop(bp, req);
6264 	return rc;
6265 }
6266 
6267 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6268 				     const u8 *mac_addr)
6269 {
6270 	struct bnxt_l2_filter *fltr;
6271 	struct bnxt_l2_key key;
6272 	int rc;
6273 
6274 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6275 	key.vlan = 0;
6276 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6277 	if (IS_ERR(fltr))
6278 		return PTR_ERR(fltr);
6279 
6280 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6281 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6282 	if (rc)
6283 		bnxt_del_l2_filter(bp, fltr);
6284 	else
6285 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6286 	return rc;
6287 }
6288 
6289 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6290 {
6291 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6292 
6293 	/* Any associated ntuple filters will also be cleared by firmware. */
6294 	for (i = 0; i < num_of_vnics; i++) {
6295 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6296 
6297 		for (j = 0; j < vnic->uc_filter_count; j++) {
6298 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6299 
6300 			bnxt_hwrm_l2_filter_free(bp, fltr);
6301 			bnxt_del_l2_filter(bp, fltr);
6302 		}
6303 		vnic->uc_filter_count = 0;
6304 	}
6305 }
6306 
6307 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6308 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6309 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6310 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6311 
6312 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6313 					   struct hwrm_vnic_tpa_cfg_input *req)
6314 {
6315 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6316 
6317 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6318 		return;
6319 
6320 	if (bp->vxlan_port)
6321 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6322 	if (bp->vxlan_gpe_port)
6323 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6324 	if (bp->nge_port)
6325 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6326 
6327 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6328 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6329 }
6330 
6331 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6332 			   u32 tpa_flags)
6333 {
6334 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6335 	struct hwrm_vnic_tpa_cfg_input *req;
6336 	int rc;
6337 
6338 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6339 		return 0;
6340 
6341 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6342 	if (rc)
6343 		return rc;
6344 
6345 	if (tpa_flags) {
6346 		u16 mss = bp->dev->mtu - 40;
6347 		u32 nsegs, n, segs = 0, flags;
6348 
6349 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6350 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6351 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6352 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6353 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6354 		if (tpa_flags & BNXT_FLAG_GRO)
6355 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6356 
6357 		req->flags = cpu_to_le32(flags);
6358 
6359 		req->enables =
6360 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6361 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6362 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6363 
6364 		/* Number of segs are log2 units, and first packet is not
6365 		 * included as part of this units.
6366 		 */
6367 		if (mss <= BNXT_RX_PAGE_SIZE) {
6368 			n = BNXT_RX_PAGE_SIZE / mss;
6369 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6370 		} else {
6371 			n = mss / BNXT_RX_PAGE_SIZE;
6372 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6373 				n++;
6374 			nsegs = (MAX_SKB_FRAGS - n) / n;
6375 		}
6376 
6377 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6378 			segs = MAX_TPA_SEGS_P5;
6379 			max_aggs = bp->max_tpa;
6380 		} else {
6381 			segs = ilog2(nsegs);
6382 		}
6383 		req->max_agg_segs = cpu_to_le16(segs);
6384 		req->max_aggs = cpu_to_le16(max_aggs);
6385 
6386 		req->min_agg_len = cpu_to_le32(512);
6387 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6388 	}
6389 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6390 
6391 	return hwrm_req_send(bp, req);
6392 }
6393 
6394 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6395 {
6396 	struct bnxt_ring_grp_info *grp_info;
6397 
6398 	grp_info = &bp->grp_info[ring->grp_idx];
6399 	return grp_info->cp_fw_ring_id;
6400 }
6401 
6402 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6403 {
6404 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6405 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6406 	else
6407 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6408 }
6409 
6410 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6411 {
6412 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6413 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6414 	else
6415 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6416 }
6417 
6418 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6419 {
6420 	int entries;
6421 
6422 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6423 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6424 	else
6425 		entries = HW_HASH_INDEX_SIZE;
6426 
6427 	bp->rss_indir_tbl_entries = entries;
6428 	bp->rss_indir_tbl =
6429 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6430 	if (!bp->rss_indir_tbl)
6431 		return -ENOMEM;
6432 
6433 	return 0;
6434 }
6435 
6436 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6437 				 struct ethtool_rxfh_context *rss_ctx)
6438 {
6439 	u16 max_rings, max_entries, pad, i;
6440 	u32 *rss_indir_tbl;
6441 
6442 	if (!bp->rx_nr_rings)
6443 		return;
6444 
6445 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6446 		max_rings = bp->rx_nr_rings - 1;
6447 	else
6448 		max_rings = bp->rx_nr_rings;
6449 
6450 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6451 	if (rss_ctx)
6452 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6453 	else
6454 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6455 
6456 	for (i = 0; i < max_entries; i++)
6457 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6458 
6459 	pad = bp->rss_indir_tbl_entries - max_entries;
6460 	if (pad)
6461 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6462 }
6463 
6464 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6465 {
6466 	u32 i, tbl_size, max_ring = 0;
6467 
6468 	if (!bp->rss_indir_tbl)
6469 		return 0;
6470 
6471 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6472 	for (i = 0; i < tbl_size; i++)
6473 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6474 	return max_ring;
6475 }
6476 
6477 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6478 {
6479 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6480 		if (!rx_rings)
6481 			return 0;
6482 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6483 					       BNXT_RSS_TABLE_ENTRIES_P5);
6484 	}
6485 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6486 		return 2;
6487 	return 1;
6488 }
6489 
6490 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6491 {
6492 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6493 	u16 i, j;
6494 
6495 	/* Fill the RSS indirection table with ring group ids */
6496 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6497 		if (!no_rss)
6498 			j = bp->rss_indir_tbl[i];
6499 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6500 	}
6501 }
6502 
6503 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6504 				    struct bnxt_vnic_info *vnic)
6505 {
6506 	__le16 *ring_tbl = vnic->rss_table;
6507 	struct bnxt_rx_ring_info *rxr;
6508 	u16 tbl_size, i;
6509 
6510 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6511 
6512 	for (i = 0; i < tbl_size; i++) {
6513 		u16 ring_id, j;
6514 
6515 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6516 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6517 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6518 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6519 		else
6520 			j = bp->rss_indir_tbl[i];
6521 		rxr = &bp->rx_ring[j];
6522 
6523 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6524 		*ring_tbl++ = cpu_to_le16(ring_id);
6525 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6526 		*ring_tbl++ = cpu_to_le16(ring_id);
6527 	}
6528 }
6529 
6530 static void
6531 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6532 			 struct bnxt_vnic_info *vnic)
6533 {
6534 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6535 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6536 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6537 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6538 	} else {
6539 		bnxt_fill_hw_rss_tbl(bp, vnic);
6540 	}
6541 
6542 	if (bp->rss_hash_delta) {
6543 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6544 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6545 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6546 		else
6547 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6548 	} else {
6549 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6550 	}
6551 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6552 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6553 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6554 }
6555 
6556 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6557 				  bool set_rss)
6558 {
6559 	struct hwrm_vnic_rss_cfg_input *req;
6560 	int rc;
6561 
6562 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6563 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6564 		return 0;
6565 
6566 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6567 	if (rc)
6568 		return rc;
6569 
6570 	if (set_rss)
6571 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6572 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6573 	return hwrm_req_send(bp, req);
6574 }
6575 
6576 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6577 				     struct bnxt_vnic_info *vnic, bool set_rss)
6578 {
6579 	struct hwrm_vnic_rss_cfg_input *req;
6580 	dma_addr_t ring_tbl_map;
6581 	u32 i, nr_ctxs;
6582 	int rc;
6583 
6584 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6585 	if (rc)
6586 		return rc;
6587 
6588 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6589 	if (!set_rss)
6590 		return hwrm_req_send(bp, req);
6591 
6592 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6593 	ring_tbl_map = vnic->rss_table_dma_addr;
6594 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6595 
6596 	hwrm_req_hold(bp, req);
6597 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6598 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6599 		req->ring_table_pair_index = i;
6600 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6601 		rc = hwrm_req_send(bp, req);
6602 		if (rc)
6603 			goto exit;
6604 	}
6605 
6606 exit:
6607 	hwrm_req_drop(bp, req);
6608 	return rc;
6609 }
6610 
6611 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6612 {
6613 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6614 	struct hwrm_vnic_rss_qcfg_output *resp;
6615 	struct hwrm_vnic_rss_qcfg_input *req;
6616 
6617 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6618 		return;
6619 
6620 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6621 	/* all contexts configured to same hash_type, zero always exists */
6622 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6623 	resp = hwrm_req_hold(bp, req);
6624 	if (!hwrm_req_send(bp, req)) {
6625 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6626 		bp->rss_hash_delta = 0;
6627 	}
6628 	hwrm_req_drop(bp, req);
6629 }
6630 
6631 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6632 {
6633 	u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh;
6634 	struct hwrm_vnic_plcmodes_cfg_input *req;
6635 	int rc;
6636 
6637 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6638 	if (rc)
6639 		return rc;
6640 
6641 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6642 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6643 	req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6644 
6645 	if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
6646 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6647 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6648 		req->enables |=
6649 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6650 		req->hds_threshold = cpu_to_le16(hds_thresh);
6651 	}
6652 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6653 	return hwrm_req_send(bp, req);
6654 }
6655 
6656 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6657 					struct bnxt_vnic_info *vnic,
6658 					u16 ctx_idx)
6659 {
6660 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6661 
6662 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6663 		return;
6664 
6665 	req->rss_cos_lb_ctx_id =
6666 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6667 
6668 	hwrm_req_send(bp, req);
6669 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6670 }
6671 
6672 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6673 {
6674 	int i, j;
6675 
6676 	for (i = 0; i < bp->nr_vnics; i++) {
6677 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6678 
6679 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6680 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6681 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6682 		}
6683 	}
6684 	bp->rsscos_nr_ctxs = 0;
6685 }
6686 
6687 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6688 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6689 {
6690 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6691 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6692 	int rc;
6693 
6694 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6695 	if (rc)
6696 		return rc;
6697 
6698 	resp = hwrm_req_hold(bp, req);
6699 	rc = hwrm_req_send(bp, req);
6700 	if (!rc)
6701 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6702 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6703 	hwrm_req_drop(bp, req);
6704 
6705 	return rc;
6706 }
6707 
6708 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6709 {
6710 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6711 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6712 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6713 }
6714 
6715 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6716 {
6717 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6718 	struct hwrm_vnic_cfg_input *req;
6719 	unsigned int ring = 0, grp_idx;
6720 	u16 def_vlan = 0;
6721 	int rc;
6722 
6723 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6724 	if (rc)
6725 		return rc;
6726 
6727 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6728 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6729 
6730 		req->default_rx_ring_id =
6731 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6732 		req->default_cmpl_ring_id =
6733 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6734 		req->enables =
6735 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6736 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6737 		goto vnic_mru;
6738 	}
6739 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6740 	/* Only RSS support for now TBD: COS & LB */
6741 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6742 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6743 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6744 					   VNIC_CFG_REQ_ENABLES_MRU);
6745 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6746 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6747 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6748 					   VNIC_CFG_REQ_ENABLES_MRU);
6749 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6750 	} else {
6751 		req->rss_rule = cpu_to_le16(0xffff);
6752 	}
6753 
6754 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6755 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6756 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6757 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6758 	} else {
6759 		req->cos_rule = cpu_to_le16(0xffff);
6760 	}
6761 
6762 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6763 		ring = 0;
6764 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6765 		ring = vnic->vnic_id - 1;
6766 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6767 		ring = bp->rx_nr_rings - 1;
6768 
6769 	grp_idx = bp->rx_ring[ring].bnapi->index;
6770 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6771 	req->lb_rule = cpu_to_le16(0xffff);
6772 vnic_mru:
6773 	vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
6774 	req->mru = cpu_to_le16(vnic->mru);
6775 
6776 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6777 #ifdef CONFIG_BNXT_SRIOV
6778 	if (BNXT_VF(bp))
6779 		def_vlan = bp->vf.vlan;
6780 #endif
6781 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6782 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6783 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6784 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6785 
6786 	return hwrm_req_send(bp, req);
6787 }
6788 
6789 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6790 				    struct bnxt_vnic_info *vnic)
6791 {
6792 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6793 		struct hwrm_vnic_free_input *req;
6794 
6795 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6796 			return;
6797 
6798 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6799 
6800 		hwrm_req_send(bp, req);
6801 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6802 	}
6803 }
6804 
6805 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6806 {
6807 	u16 i;
6808 
6809 	for (i = 0; i < bp->nr_vnics; i++)
6810 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6811 }
6812 
6813 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6814 			 unsigned int start_rx_ring_idx,
6815 			 unsigned int nr_rings)
6816 {
6817 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6818 	struct hwrm_vnic_alloc_output *resp;
6819 	struct hwrm_vnic_alloc_input *req;
6820 	int rc;
6821 
6822 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6823 	if (rc)
6824 		return rc;
6825 
6826 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6827 		goto vnic_no_ring_grps;
6828 
6829 	/* map ring groups to this vnic */
6830 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6831 		grp_idx = bp->rx_ring[i].bnapi->index;
6832 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6833 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6834 				   j, nr_rings);
6835 			break;
6836 		}
6837 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6838 	}
6839 
6840 vnic_no_ring_grps:
6841 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6842 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6843 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6844 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6845 
6846 	resp = hwrm_req_hold(bp, req);
6847 	rc = hwrm_req_send(bp, req);
6848 	if (!rc)
6849 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6850 	hwrm_req_drop(bp, req);
6851 	return rc;
6852 }
6853 
6854 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6855 {
6856 	struct hwrm_vnic_qcaps_output *resp;
6857 	struct hwrm_vnic_qcaps_input *req;
6858 	int rc;
6859 
6860 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6861 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6862 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6863 	if (bp->hwrm_spec_code < 0x10600)
6864 		return 0;
6865 
6866 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6867 	if (rc)
6868 		return rc;
6869 
6870 	resp = hwrm_req_hold(bp, req);
6871 	rc = hwrm_req_send(bp, req);
6872 	if (!rc) {
6873 		u32 flags = le32_to_cpu(resp->flags);
6874 
6875 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6876 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6877 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6878 		if (flags &
6879 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6880 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6881 
6882 		/* Older P5 fw before EXT_HW_STATS support did not set
6883 		 * VLAN_STRIP_CAP properly.
6884 		 */
6885 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6886 		    (BNXT_CHIP_P5(bp) &&
6887 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6888 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6889 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6890 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6891 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6892 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6893 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6894 		if (bp->max_tpa_v2) {
6895 			if (BNXT_CHIP_P5(bp))
6896 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6897 			else
6898 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6899 		}
6900 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6901 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6902 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6903 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6904 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6905 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6906 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6907 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6908 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6909 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6910 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
6911 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
6912 	}
6913 	hwrm_req_drop(bp, req);
6914 	return rc;
6915 }
6916 
6917 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6918 {
6919 	struct hwrm_ring_grp_alloc_output *resp;
6920 	struct hwrm_ring_grp_alloc_input *req;
6921 	int rc;
6922 	u16 i;
6923 
6924 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6925 		return 0;
6926 
6927 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6928 	if (rc)
6929 		return rc;
6930 
6931 	resp = hwrm_req_hold(bp, req);
6932 	for (i = 0; i < bp->rx_nr_rings; i++) {
6933 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
6934 
6935 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
6936 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
6937 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
6938 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
6939 
6940 		rc = hwrm_req_send(bp, req);
6941 
6942 		if (rc)
6943 			break;
6944 
6945 		bp->grp_info[grp_idx].fw_grp_id =
6946 			le32_to_cpu(resp->ring_group_id);
6947 	}
6948 	hwrm_req_drop(bp, req);
6949 	return rc;
6950 }
6951 
6952 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
6953 {
6954 	struct hwrm_ring_grp_free_input *req;
6955 	u16 i;
6956 
6957 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
6958 		return;
6959 
6960 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
6961 		return;
6962 
6963 	hwrm_req_hold(bp, req);
6964 	for (i = 0; i < bp->cp_nr_rings; i++) {
6965 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
6966 			continue;
6967 		req->ring_group_id =
6968 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
6969 
6970 		hwrm_req_send(bp, req);
6971 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
6972 	}
6973 	hwrm_req_drop(bp, req);
6974 }
6975 
6976 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type,
6977 				       struct hwrm_ring_alloc_input *req,
6978 				       struct bnxt_ring_struct *ring)
6979 {
6980 	struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx];
6981 	u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID |
6982 		      RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID;
6983 
6984 	if (ring_type == HWRM_RING_ALLOC_AGG) {
6985 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
6986 		req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
6987 		req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
6988 		enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID;
6989 	} else {
6990 		req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
6991 		if (NET_IP_ALIGN == 2)
6992 			req->flags =
6993 				cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD);
6994 	}
6995 	req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6996 	req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
6997 	req->enables |= cpu_to_le32(enables);
6998 }
6999 
7000 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
7001 				    struct bnxt_ring_struct *ring,
7002 				    u32 ring_type, u32 map_index)
7003 {
7004 	struct hwrm_ring_alloc_output *resp;
7005 	struct hwrm_ring_alloc_input *req;
7006 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
7007 	struct bnxt_ring_grp_info *grp_info;
7008 	int rc, err = 0;
7009 	u16 ring_id;
7010 
7011 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
7012 	if (rc)
7013 		goto exit;
7014 
7015 	req->enables = 0;
7016 	if (rmem->nr_pages > 1) {
7017 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
7018 		/* Page size is in log2 units */
7019 		req->page_size = BNXT_PAGE_SHIFT;
7020 		req->page_tbl_depth = 1;
7021 	} else {
7022 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
7023 	}
7024 	req->fbo = 0;
7025 	/* Association of ring index with doorbell index and MSIX number */
7026 	req->logical_id = cpu_to_le16(map_index);
7027 
7028 	switch (ring_type) {
7029 	case HWRM_RING_ALLOC_TX: {
7030 		struct bnxt_tx_ring_info *txr;
7031 		u16 flags = 0;
7032 
7033 		txr = container_of(ring, struct bnxt_tx_ring_info,
7034 				   tx_ring_struct);
7035 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
7036 		/* Association of transmit ring with completion ring */
7037 		grp_info = &bp->grp_info[ring->grp_idx];
7038 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
7039 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
7040 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7041 		req->queue_id = cpu_to_le16(ring->queue_id);
7042 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
7043 			req->cmpl_coal_cnt =
7044 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
7045 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
7046 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
7047 		req->flags = cpu_to_le16(flags);
7048 		break;
7049 	}
7050 	case HWRM_RING_ALLOC_RX:
7051 	case HWRM_RING_ALLOC_AGG:
7052 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
7053 		req->length = (ring_type == HWRM_RING_ALLOC_RX) ?
7054 			      cpu_to_le32(bp->rx_ring_mask + 1) :
7055 			      cpu_to_le32(bp->rx_agg_ring_mask + 1);
7056 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7057 			bnxt_set_rx_ring_params_p5(bp, ring_type, req, ring);
7058 		break;
7059 	case HWRM_RING_ALLOC_CMPL:
7060 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7061 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7062 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7063 			/* Association of cp ring with nq */
7064 			grp_info = &bp->grp_info[map_index];
7065 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7066 			req->cq_handle = cpu_to_le64(ring->handle);
7067 			req->enables |= cpu_to_le32(
7068 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7069 		} else {
7070 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7071 		}
7072 		break;
7073 	case HWRM_RING_ALLOC_NQ:
7074 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7075 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7076 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7077 		break;
7078 	default:
7079 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7080 			   ring_type);
7081 		return -1;
7082 	}
7083 
7084 	resp = hwrm_req_hold(bp, req);
7085 	rc = hwrm_req_send(bp, req);
7086 	err = le16_to_cpu(resp->error_code);
7087 	ring_id = le16_to_cpu(resp->ring_id);
7088 	hwrm_req_drop(bp, req);
7089 
7090 exit:
7091 	if (rc || err) {
7092 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7093 			   ring_type, rc, err);
7094 		return -EIO;
7095 	}
7096 	ring->fw_ring_id = ring_id;
7097 	return rc;
7098 }
7099 
7100 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7101 {
7102 	int rc;
7103 
7104 	if (BNXT_PF(bp)) {
7105 		struct hwrm_func_cfg_input *req;
7106 
7107 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7108 		if (rc)
7109 			return rc;
7110 
7111 		req->fid = cpu_to_le16(0xffff);
7112 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7113 		req->async_event_cr = cpu_to_le16(idx);
7114 		return hwrm_req_send(bp, req);
7115 	} else {
7116 		struct hwrm_func_vf_cfg_input *req;
7117 
7118 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7119 		if (rc)
7120 			return rc;
7121 
7122 		req->enables =
7123 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7124 		req->async_event_cr = cpu_to_le16(idx);
7125 		return hwrm_req_send(bp, req);
7126 	}
7127 }
7128 
7129 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7130 			     u32 ring_type)
7131 {
7132 	switch (ring_type) {
7133 	case HWRM_RING_ALLOC_TX:
7134 		db->db_ring_mask = bp->tx_ring_mask;
7135 		break;
7136 	case HWRM_RING_ALLOC_RX:
7137 		db->db_ring_mask = bp->rx_ring_mask;
7138 		break;
7139 	case HWRM_RING_ALLOC_AGG:
7140 		db->db_ring_mask = bp->rx_agg_ring_mask;
7141 		break;
7142 	case HWRM_RING_ALLOC_CMPL:
7143 	case HWRM_RING_ALLOC_NQ:
7144 		db->db_ring_mask = bp->cp_ring_mask;
7145 		break;
7146 	}
7147 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7148 		db->db_epoch_mask = db->db_ring_mask + 1;
7149 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7150 	}
7151 }
7152 
7153 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7154 			u32 map_idx, u32 xid)
7155 {
7156 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7157 		switch (ring_type) {
7158 		case HWRM_RING_ALLOC_TX:
7159 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7160 			break;
7161 		case HWRM_RING_ALLOC_RX:
7162 		case HWRM_RING_ALLOC_AGG:
7163 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7164 			break;
7165 		case HWRM_RING_ALLOC_CMPL:
7166 			db->db_key64 = DBR_PATH_L2;
7167 			break;
7168 		case HWRM_RING_ALLOC_NQ:
7169 			db->db_key64 = DBR_PATH_L2;
7170 			break;
7171 		}
7172 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7173 
7174 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7175 			db->db_key64 |= DBR_VALID;
7176 
7177 		db->doorbell = bp->bar1 + bp->db_offset;
7178 	} else {
7179 		db->doorbell = bp->bar1 + map_idx * 0x80;
7180 		switch (ring_type) {
7181 		case HWRM_RING_ALLOC_TX:
7182 			db->db_key32 = DB_KEY_TX;
7183 			break;
7184 		case HWRM_RING_ALLOC_RX:
7185 		case HWRM_RING_ALLOC_AGG:
7186 			db->db_key32 = DB_KEY_RX;
7187 			break;
7188 		case HWRM_RING_ALLOC_CMPL:
7189 			db->db_key32 = DB_KEY_CP;
7190 			break;
7191 		}
7192 	}
7193 	bnxt_set_db_mask(bp, db, ring_type);
7194 }
7195 
7196 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7197 				   struct bnxt_rx_ring_info *rxr)
7198 {
7199 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7200 	struct bnxt_napi *bnapi = rxr->bnapi;
7201 	u32 type = HWRM_RING_ALLOC_RX;
7202 	u32 map_idx = bnapi->index;
7203 	int rc;
7204 
7205 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7206 	if (rc)
7207 		return rc;
7208 
7209 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7210 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7211 
7212 	return 0;
7213 }
7214 
7215 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7216 				       struct bnxt_rx_ring_info *rxr)
7217 {
7218 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7219 	u32 type = HWRM_RING_ALLOC_AGG;
7220 	u32 grp_idx = ring->grp_idx;
7221 	u32 map_idx;
7222 	int rc;
7223 
7224 	map_idx = grp_idx + bp->rx_nr_rings;
7225 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7226 	if (rc)
7227 		return rc;
7228 
7229 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7230 		    ring->fw_ring_id);
7231 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7232 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7233 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7234 
7235 	return 0;
7236 }
7237 
7238 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp,
7239 				      struct bnxt_cp_ring_info *cpr)
7240 {
7241 	const u32 type = HWRM_RING_ALLOC_CMPL;
7242 	struct bnxt_napi *bnapi = cpr->bnapi;
7243 	struct bnxt_ring_struct *ring;
7244 	u32 map_idx = bnapi->index;
7245 	int rc;
7246 
7247 	ring = &cpr->cp_ring_struct;
7248 	ring->handle = BNXT_SET_NQ_HDL(cpr);
7249 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7250 	if (rc)
7251 		return rc;
7252 	bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7253 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7254 	return 0;
7255 }
7256 
7257 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp,
7258 				   struct bnxt_tx_ring_info *txr, u32 tx_idx)
7259 {
7260 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7261 	const u32 type = HWRM_RING_ALLOC_TX;
7262 	int rc;
7263 
7264 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, tx_idx);
7265 	if (rc)
7266 		return rc;
7267 	bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id);
7268 	return 0;
7269 }
7270 
7271 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7272 {
7273 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7274 	int i, rc = 0;
7275 	u32 type;
7276 
7277 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7278 		type = HWRM_RING_ALLOC_NQ;
7279 	else
7280 		type = HWRM_RING_ALLOC_CMPL;
7281 	for (i = 0; i < bp->cp_nr_rings; i++) {
7282 		struct bnxt_napi *bnapi = bp->bnapi[i];
7283 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7284 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7285 		u32 map_idx = ring->map_idx;
7286 		unsigned int vector;
7287 
7288 		vector = bp->irq_tbl[map_idx].vector;
7289 		disable_irq_nosync(vector);
7290 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7291 		if (rc) {
7292 			enable_irq(vector);
7293 			goto err_out;
7294 		}
7295 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7296 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7297 		enable_irq(vector);
7298 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7299 
7300 		if (!i) {
7301 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7302 			if (rc)
7303 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7304 		}
7305 	}
7306 
7307 	for (i = 0; i < bp->tx_nr_rings; i++) {
7308 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7309 
7310 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7311 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
7312 			if (rc)
7313 				goto err_out;
7314 		}
7315 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i);
7316 		if (rc)
7317 			goto err_out;
7318 	}
7319 
7320 	for (i = 0; i < bp->rx_nr_rings; i++) {
7321 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7322 
7323 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7324 		if (rc)
7325 			goto err_out;
7326 		/* If we have agg rings, post agg buffers first. */
7327 		if (!agg_rings)
7328 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7329 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7330 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
7331 			if (rc)
7332 				goto err_out;
7333 		}
7334 	}
7335 
7336 	if (agg_rings) {
7337 		for (i = 0; i < bp->rx_nr_rings; i++) {
7338 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7339 			if (rc)
7340 				goto err_out;
7341 		}
7342 	}
7343 err_out:
7344 	return rc;
7345 }
7346 
7347 static void bnxt_cancel_dim(struct bnxt *bp)
7348 {
7349 	int i;
7350 
7351 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7352 	 * if NAPI is enabled.
7353 	 */
7354 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7355 		return;
7356 
7357 	/* Make sure NAPI sees that the VNIC is disabled */
7358 	synchronize_net();
7359 	for (i = 0; i < bp->rx_nr_rings; i++) {
7360 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7361 		struct bnxt_napi *bnapi = rxr->bnapi;
7362 
7363 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7364 	}
7365 }
7366 
7367 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7368 				   struct bnxt_ring_struct *ring,
7369 				   u32 ring_type, int cmpl_ring_id)
7370 {
7371 	struct hwrm_ring_free_output *resp;
7372 	struct hwrm_ring_free_input *req;
7373 	u16 error_code = 0;
7374 	int rc;
7375 
7376 	if (BNXT_NO_FW_ACCESS(bp))
7377 		return 0;
7378 
7379 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7380 	if (rc)
7381 		goto exit;
7382 
7383 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7384 	req->ring_type = ring_type;
7385 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7386 
7387 	resp = hwrm_req_hold(bp, req);
7388 	rc = hwrm_req_send(bp, req);
7389 	error_code = le16_to_cpu(resp->error_code);
7390 	hwrm_req_drop(bp, req);
7391 exit:
7392 	if (rc || error_code) {
7393 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7394 			   ring_type, rc, error_code);
7395 		return -EIO;
7396 	}
7397 	return 0;
7398 }
7399 
7400 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp,
7401 				   struct bnxt_tx_ring_info *txr,
7402 				   bool close_path)
7403 {
7404 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7405 	u32 cmpl_ring_id;
7406 
7407 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7408 		return;
7409 
7410 	cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) :
7411 		       INVALID_HW_RING_ID;
7412 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX,
7413 				cmpl_ring_id);
7414 	ring->fw_ring_id = INVALID_HW_RING_ID;
7415 }
7416 
7417 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7418 				   struct bnxt_rx_ring_info *rxr,
7419 				   bool close_path)
7420 {
7421 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7422 	u32 grp_idx = rxr->bnapi->index;
7423 	u32 cmpl_ring_id;
7424 
7425 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7426 		return;
7427 
7428 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7429 	hwrm_ring_free_send_msg(bp, ring,
7430 				RING_FREE_REQ_RING_TYPE_RX,
7431 				close_path ? cmpl_ring_id :
7432 				INVALID_HW_RING_ID);
7433 	ring->fw_ring_id = INVALID_HW_RING_ID;
7434 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7435 }
7436 
7437 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7438 				       struct bnxt_rx_ring_info *rxr,
7439 				       bool close_path)
7440 {
7441 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7442 	u32 grp_idx = rxr->bnapi->index;
7443 	u32 type, cmpl_ring_id;
7444 
7445 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7446 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7447 	else
7448 		type = RING_FREE_REQ_RING_TYPE_RX;
7449 
7450 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7451 		return;
7452 
7453 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7454 	hwrm_ring_free_send_msg(bp, ring, type,
7455 				close_path ? cmpl_ring_id :
7456 				INVALID_HW_RING_ID);
7457 	ring->fw_ring_id = INVALID_HW_RING_ID;
7458 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7459 }
7460 
7461 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp,
7462 				   struct bnxt_cp_ring_info *cpr)
7463 {
7464 	struct bnxt_ring_struct *ring;
7465 
7466 	ring = &cpr->cp_ring_struct;
7467 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7468 		return;
7469 
7470 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL,
7471 				INVALID_HW_RING_ID);
7472 	ring->fw_ring_id = INVALID_HW_RING_ID;
7473 }
7474 
7475 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
7476 {
7477 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7478 	int i, size = ring->ring_mem.page_size;
7479 
7480 	cpr->cp_raw_cons = 0;
7481 	cpr->toggle = 0;
7482 
7483 	for (i = 0; i < bp->cp_nr_pages; i++)
7484 		if (cpr->cp_desc_ring[i])
7485 			memset(cpr->cp_desc_ring[i], 0, size);
7486 }
7487 
7488 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7489 {
7490 	u32 type;
7491 	int i;
7492 
7493 	if (!bp->bnapi)
7494 		return;
7495 
7496 	for (i = 0; i < bp->tx_nr_rings; i++)
7497 		bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path);
7498 
7499 	bnxt_cancel_dim(bp);
7500 	for (i = 0; i < bp->rx_nr_rings; i++) {
7501 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7502 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7503 	}
7504 
7505 	/* The completion rings are about to be freed.  After that the
7506 	 * IRQ doorbell will not work anymore.  So we need to disable
7507 	 * IRQ here.
7508 	 */
7509 	bnxt_disable_int_sync(bp);
7510 
7511 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7512 		type = RING_FREE_REQ_RING_TYPE_NQ;
7513 	else
7514 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7515 	for (i = 0; i < bp->cp_nr_rings; i++) {
7516 		struct bnxt_napi *bnapi = bp->bnapi[i];
7517 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7518 		struct bnxt_ring_struct *ring;
7519 		int j;
7520 
7521 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++)
7522 			bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]);
7523 
7524 		ring = &cpr->cp_ring_struct;
7525 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7526 			hwrm_ring_free_send_msg(bp, ring, type,
7527 						INVALID_HW_RING_ID);
7528 			ring->fw_ring_id = INVALID_HW_RING_ID;
7529 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7530 		}
7531 	}
7532 }
7533 
7534 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7535 			     bool shared);
7536 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7537 			   bool shared);
7538 
7539 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7540 {
7541 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7542 	struct hwrm_func_qcfg_output *resp;
7543 	struct hwrm_func_qcfg_input *req;
7544 	int rc;
7545 
7546 	if (bp->hwrm_spec_code < 0x10601)
7547 		return 0;
7548 
7549 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7550 	if (rc)
7551 		return rc;
7552 
7553 	req->fid = cpu_to_le16(0xffff);
7554 	resp = hwrm_req_hold(bp, req);
7555 	rc = hwrm_req_send(bp, req);
7556 	if (rc) {
7557 		hwrm_req_drop(bp, req);
7558 		return rc;
7559 	}
7560 
7561 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7562 	if (BNXT_NEW_RM(bp)) {
7563 		u16 cp, stats;
7564 
7565 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7566 		hw_resc->resv_hw_ring_grps =
7567 			le32_to_cpu(resp->alloc_hw_ring_grps);
7568 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7569 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7570 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7571 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7572 		hw_resc->resv_irqs = cp;
7573 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7574 			int rx = hw_resc->resv_rx_rings;
7575 			int tx = hw_resc->resv_tx_rings;
7576 
7577 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7578 				rx >>= 1;
7579 			if (cp < (rx + tx)) {
7580 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7581 				if (rc)
7582 					goto get_rings_exit;
7583 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7584 					rx <<= 1;
7585 				hw_resc->resv_rx_rings = rx;
7586 				hw_resc->resv_tx_rings = tx;
7587 			}
7588 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7589 			hw_resc->resv_hw_ring_grps = rx;
7590 		}
7591 		hw_resc->resv_cp_rings = cp;
7592 		hw_resc->resv_stat_ctxs = stats;
7593 	}
7594 get_rings_exit:
7595 	hwrm_req_drop(bp, req);
7596 	return rc;
7597 }
7598 
7599 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7600 {
7601 	struct hwrm_func_qcfg_output *resp;
7602 	struct hwrm_func_qcfg_input *req;
7603 	int rc;
7604 
7605 	if (bp->hwrm_spec_code < 0x10601)
7606 		return 0;
7607 
7608 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7609 	if (rc)
7610 		return rc;
7611 
7612 	req->fid = cpu_to_le16(fid);
7613 	resp = hwrm_req_hold(bp, req);
7614 	rc = hwrm_req_send(bp, req);
7615 	if (!rc)
7616 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7617 
7618 	hwrm_req_drop(bp, req);
7619 	return rc;
7620 }
7621 
7622 static bool bnxt_rfs_supported(struct bnxt *bp);
7623 
7624 static struct hwrm_func_cfg_input *
7625 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7626 {
7627 	struct hwrm_func_cfg_input *req;
7628 	u32 enables = 0;
7629 
7630 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7631 		return NULL;
7632 
7633 	req->fid = cpu_to_le16(0xffff);
7634 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7635 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7636 	if (BNXT_NEW_RM(bp)) {
7637 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7638 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7639 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7640 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7641 			enables |= hwr->cp_p5 ?
7642 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7643 		} else {
7644 			enables |= hwr->cp ?
7645 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7646 			enables |= hwr->grp ?
7647 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7648 		}
7649 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7650 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7651 					  0;
7652 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7653 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7654 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7655 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7656 			req->num_msix = cpu_to_le16(hwr->cp);
7657 		} else {
7658 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7659 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7660 		}
7661 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7662 		req->num_vnics = cpu_to_le16(hwr->vnic);
7663 	}
7664 	req->enables = cpu_to_le32(enables);
7665 	return req;
7666 }
7667 
7668 static struct hwrm_func_vf_cfg_input *
7669 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7670 {
7671 	struct hwrm_func_vf_cfg_input *req;
7672 	u32 enables = 0;
7673 
7674 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7675 		return NULL;
7676 
7677 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7678 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7679 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7680 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7681 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7682 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7683 		enables |= hwr->cp_p5 ?
7684 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7685 	} else {
7686 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7687 		enables |= hwr->grp ?
7688 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7689 	}
7690 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7691 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7692 
7693 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7694 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7695 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7696 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7697 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7698 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7699 	} else {
7700 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7701 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7702 	}
7703 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7704 	req->num_vnics = cpu_to_le16(hwr->vnic);
7705 
7706 	req->enables = cpu_to_le32(enables);
7707 	return req;
7708 }
7709 
7710 static int
7711 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7712 {
7713 	struct hwrm_func_cfg_input *req;
7714 	int rc;
7715 
7716 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7717 	if (!req)
7718 		return -ENOMEM;
7719 
7720 	if (!req->enables) {
7721 		hwrm_req_drop(bp, req);
7722 		return 0;
7723 	}
7724 
7725 	rc = hwrm_req_send(bp, req);
7726 	if (rc)
7727 		return rc;
7728 
7729 	if (bp->hwrm_spec_code < 0x10601)
7730 		bp->hw_resc.resv_tx_rings = hwr->tx;
7731 
7732 	return bnxt_hwrm_get_rings(bp);
7733 }
7734 
7735 static int
7736 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7737 {
7738 	struct hwrm_func_vf_cfg_input *req;
7739 	int rc;
7740 
7741 	if (!BNXT_NEW_RM(bp)) {
7742 		bp->hw_resc.resv_tx_rings = hwr->tx;
7743 		return 0;
7744 	}
7745 
7746 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7747 	if (!req)
7748 		return -ENOMEM;
7749 
7750 	rc = hwrm_req_send(bp, req);
7751 	if (rc)
7752 		return rc;
7753 
7754 	return bnxt_hwrm_get_rings(bp);
7755 }
7756 
7757 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7758 {
7759 	if (BNXT_PF(bp))
7760 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7761 	else
7762 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7763 }
7764 
7765 int bnxt_nq_rings_in_use(struct bnxt *bp)
7766 {
7767 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7768 }
7769 
7770 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7771 {
7772 	int cp;
7773 
7774 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7775 		return bnxt_nq_rings_in_use(bp);
7776 
7777 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7778 	return cp;
7779 }
7780 
7781 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7782 {
7783 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7784 }
7785 
7786 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7787 {
7788 	if (!hwr->grp)
7789 		return 0;
7790 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7791 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7792 
7793 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7794 			rss_ctx *= hwr->vnic;
7795 		return rss_ctx;
7796 	}
7797 	if (BNXT_VF(bp))
7798 		return BNXT_VF_MAX_RSS_CTX;
7799 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7800 		return hwr->grp + 1;
7801 	return 1;
7802 }
7803 
7804 /* Check if a default RSS map needs to be setup.  This function is only
7805  * used on older firmware that does not require reserving RX rings.
7806  */
7807 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7808 {
7809 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7810 
7811 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7812 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7813 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7814 		if (!netif_is_rxfh_configured(bp->dev))
7815 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7816 	}
7817 }
7818 
7819 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7820 {
7821 	if (bp->flags & BNXT_FLAG_RFS) {
7822 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7823 			return 2 + bp->num_rss_ctx;
7824 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7825 			return rx_rings + 1;
7826 	}
7827 	return 1;
7828 }
7829 
7830 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7831 {
7832 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7833 	int cp = bnxt_cp_rings_in_use(bp);
7834 	int nq = bnxt_nq_rings_in_use(bp);
7835 	int rx = bp->rx_nr_rings, stat;
7836 	int vnic, grp = rx;
7837 
7838 	/* Old firmware does not need RX ring reservations but we still
7839 	 * need to setup a default RSS map when needed.  With new firmware
7840 	 * we go through RX ring reservations first and then set up the
7841 	 * RSS map for the successfully reserved RX rings when needed.
7842 	 */
7843 	if (!BNXT_NEW_RM(bp))
7844 		bnxt_check_rss_tbl_no_rmgr(bp);
7845 
7846 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7847 	    bp->hwrm_spec_code >= 0x10601)
7848 		return true;
7849 
7850 	if (!BNXT_NEW_RM(bp))
7851 		return false;
7852 
7853 	vnic = bnxt_get_total_vnics(bp, rx);
7854 
7855 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7856 		rx <<= 1;
7857 	stat = bnxt_get_func_stat_ctxs(bp);
7858 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7859 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7860 	    (hw_resc->resv_hw_ring_grps != grp &&
7861 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7862 		return true;
7863 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7864 	    hw_resc->resv_irqs != nq)
7865 		return true;
7866 	return false;
7867 }
7868 
7869 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7870 {
7871 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7872 
7873 	hwr->tx = hw_resc->resv_tx_rings;
7874 	if (BNXT_NEW_RM(bp)) {
7875 		hwr->rx = hw_resc->resv_rx_rings;
7876 		hwr->cp = hw_resc->resv_irqs;
7877 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7878 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7879 		hwr->grp = hw_resc->resv_hw_ring_grps;
7880 		hwr->vnic = hw_resc->resv_vnics;
7881 		hwr->stat = hw_resc->resv_stat_ctxs;
7882 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7883 	}
7884 }
7885 
7886 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7887 {
7888 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7889 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7890 }
7891 
7892 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
7893 
7894 static int __bnxt_reserve_rings(struct bnxt *bp)
7895 {
7896 	struct bnxt_hw_rings hwr = {0};
7897 	int rx_rings, old_rx_rings, rc;
7898 	int cp = bp->cp_nr_rings;
7899 	int ulp_msix = 0;
7900 	bool sh = false;
7901 	int tx_cp;
7902 
7903 	if (!bnxt_need_reserve_rings(bp))
7904 		return 0;
7905 
7906 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
7907 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
7908 		if (!ulp_msix)
7909 			bnxt_set_ulp_stat_ctxs(bp, 0);
7910 
7911 		if (ulp_msix > bp->ulp_num_msix_want)
7912 			ulp_msix = bp->ulp_num_msix_want;
7913 		hwr.cp = cp + ulp_msix;
7914 	} else {
7915 		hwr.cp = bnxt_nq_rings_in_use(bp);
7916 	}
7917 
7918 	hwr.tx = bp->tx_nr_rings;
7919 	hwr.rx = bp->rx_nr_rings;
7920 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7921 		sh = true;
7922 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7923 		hwr.cp_p5 = hwr.rx + hwr.tx;
7924 
7925 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7926 
7927 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7928 		hwr.rx <<= 1;
7929 	hwr.grp = bp->rx_nr_rings;
7930 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7931 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
7932 	old_rx_rings = bp->hw_resc.resv_rx_rings;
7933 
7934 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
7935 	if (rc)
7936 		return rc;
7937 
7938 	bnxt_copy_reserved_rings(bp, &hwr);
7939 
7940 	rx_rings = hwr.rx;
7941 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7942 		if (hwr.rx >= 2) {
7943 			rx_rings = hwr.rx >> 1;
7944 		} else {
7945 			if (netif_running(bp->dev))
7946 				return -ENOMEM;
7947 
7948 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7949 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7950 			bp->dev->hw_features &= ~NETIF_F_LRO;
7951 			bp->dev->features &= ~NETIF_F_LRO;
7952 			bnxt_set_ring_params(bp);
7953 		}
7954 	}
7955 	rx_rings = min_t(int, rx_rings, hwr.grp);
7956 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
7957 	if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
7958 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
7959 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
7960 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
7961 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7962 		hwr.rx = rx_rings << 1;
7963 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
7964 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
7965 	bp->tx_nr_rings = hwr.tx;
7966 
7967 	/* If we cannot reserve all the RX rings, reset the RSS map only
7968 	 * if absolutely necessary
7969 	 */
7970 	if (rx_rings != bp->rx_nr_rings) {
7971 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
7972 			    rx_rings, bp->rx_nr_rings);
7973 		if (netif_is_rxfh_configured(bp->dev) &&
7974 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
7975 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
7976 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
7977 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
7978 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
7979 		}
7980 	}
7981 	bp->rx_nr_rings = rx_rings;
7982 	bp->cp_nr_rings = hwr.cp;
7983 
7984 	if (!bnxt_rings_ok(bp, &hwr))
7985 		return -ENOMEM;
7986 
7987 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
7988 	    !netif_is_rxfh_configured(bp->dev))
7989 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7990 
7991 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
7992 		int resv_msix, resv_ctx, ulp_ctxs;
7993 		struct bnxt_hw_resc *hw_resc;
7994 
7995 		hw_resc = &bp->hw_resc;
7996 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
7997 		ulp_msix = min_t(int, resv_msix, ulp_msix);
7998 		bnxt_set_ulp_msix_num(bp, ulp_msix);
7999 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
8000 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
8001 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
8002 	}
8003 
8004 	return rc;
8005 }
8006 
8007 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8008 {
8009 	struct hwrm_func_vf_cfg_input *req;
8010 	u32 flags;
8011 
8012 	if (!BNXT_NEW_RM(bp))
8013 		return 0;
8014 
8015 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
8016 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
8017 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8018 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8019 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8020 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
8021 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
8022 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8023 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8024 
8025 	req->flags = cpu_to_le32(flags);
8026 	return hwrm_req_send_silent(bp, req);
8027 }
8028 
8029 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8030 {
8031 	struct hwrm_func_cfg_input *req;
8032 	u32 flags;
8033 
8034 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
8035 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
8036 	if (BNXT_NEW_RM(bp)) {
8037 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8038 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8039 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8040 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
8041 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8042 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
8043 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
8044 		else
8045 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8046 	}
8047 
8048 	req->flags = cpu_to_le32(flags);
8049 	return hwrm_req_send_silent(bp, req);
8050 }
8051 
8052 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8053 {
8054 	if (bp->hwrm_spec_code < 0x10801)
8055 		return 0;
8056 
8057 	if (BNXT_PF(bp))
8058 		return bnxt_hwrm_check_pf_rings(bp, hwr);
8059 
8060 	return bnxt_hwrm_check_vf_rings(bp, hwr);
8061 }
8062 
8063 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
8064 {
8065 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8066 	struct hwrm_ring_aggint_qcaps_output *resp;
8067 	struct hwrm_ring_aggint_qcaps_input *req;
8068 	int rc;
8069 
8070 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
8071 	coal_cap->num_cmpl_dma_aggr_max = 63;
8072 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
8073 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
8074 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
8075 	coal_cap->int_lat_tmr_min_max = 65535;
8076 	coal_cap->int_lat_tmr_max_max = 65535;
8077 	coal_cap->num_cmpl_aggr_int_max = 65535;
8078 	coal_cap->timer_units = 80;
8079 
8080 	if (bp->hwrm_spec_code < 0x10902)
8081 		return;
8082 
8083 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
8084 		return;
8085 
8086 	resp = hwrm_req_hold(bp, req);
8087 	rc = hwrm_req_send_silent(bp, req);
8088 	if (!rc) {
8089 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8090 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8091 		coal_cap->num_cmpl_dma_aggr_max =
8092 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8093 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8094 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8095 		coal_cap->cmpl_aggr_dma_tmr_max =
8096 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8097 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8098 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8099 		coal_cap->int_lat_tmr_min_max =
8100 			le16_to_cpu(resp->int_lat_tmr_min_max);
8101 		coal_cap->int_lat_tmr_max_max =
8102 			le16_to_cpu(resp->int_lat_tmr_max_max);
8103 		coal_cap->num_cmpl_aggr_int_max =
8104 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8105 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8106 	}
8107 	hwrm_req_drop(bp, req);
8108 }
8109 
8110 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8111 {
8112 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8113 
8114 	return usec * 1000 / coal_cap->timer_units;
8115 }
8116 
8117 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8118 	struct bnxt_coal *hw_coal,
8119 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8120 {
8121 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8122 	u16 val, tmr, max, flags = hw_coal->flags;
8123 	u32 cmpl_params = coal_cap->cmpl_params;
8124 
8125 	max = hw_coal->bufs_per_record * 128;
8126 	if (hw_coal->budget)
8127 		max = hw_coal->bufs_per_record * hw_coal->budget;
8128 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8129 
8130 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8131 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8132 
8133 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8134 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8135 
8136 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8137 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8138 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8139 
8140 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8141 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8142 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8143 
8144 	/* min timer set to 1/2 of interrupt timer */
8145 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8146 		val = tmr / 2;
8147 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8148 		req->int_lat_tmr_min = cpu_to_le16(val);
8149 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8150 	}
8151 
8152 	/* buf timer set to 1/4 of interrupt timer */
8153 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8154 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8155 
8156 	if (cmpl_params &
8157 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8158 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8159 		val = clamp_t(u16, tmr, 1,
8160 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8161 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8162 		req->enables |=
8163 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8164 	}
8165 
8166 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8167 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8168 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8169 	req->flags = cpu_to_le16(flags);
8170 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8171 }
8172 
8173 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8174 				   struct bnxt_coal *hw_coal)
8175 {
8176 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8177 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8178 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8179 	u32 nq_params = coal_cap->nq_params;
8180 	u16 tmr;
8181 	int rc;
8182 
8183 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8184 		return 0;
8185 
8186 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8187 	if (rc)
8188 		return rc;
8189 
8190 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8191 	req->flags =
8192 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8193 
8194 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8195 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8196 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8197 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8198 	return hwrm_req_send(bp, req);
8199 }
8200 
8201 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8202 {
8203 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8204 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8205 	struct bnxt_coal coal;
8206 	int rc;
8207 
8208 	/* Tick values in micro seconds.
8209 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8210 	 */
8211 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8212 
8213 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8214 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8215 
8216 	if (!bnapi->rx_ring)
8217 		return -ENODEV;
8218 
8219 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8220 	if (rc)
8221 		return rc;
8222 
8223 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8224 
8225 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8226 
8227 	return hwrm_req_send(bp, req_rx);
8228 }
8229 
8230 static int
8231 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8232 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8233 {
8234 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8235 
8236 	req->ring_id = cpu_to_le16(ring_id);
8237 	return hwrm_req_send(bp, req);
8238 }
8239 
8240 static int
8241 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8242 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8243 {
8244 	struct bnxt_tx_ring_info *txr;
8245 	int i, rc;
8246 
8247 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8248 		u16 ring_id;
8249 
8250 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8251 		req->ring_id = cpu_to_le16(ring_id);
8252 		rc = hwrm_req_send(bp, req);
8253 		if (rc)
8254 			return rc;
8255 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8256 			return 0;
8257 	}
8258 	return 0;
8259 }
8260 
8261 int bnxt_hwrm_set_coal(struct bnxt *bp)
8262 {
8263 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8264 	int i, rc;
8265 
8266 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8267 	if (rc)
8268 		return rc;
8269 
8270 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8271 	if (rc) {
8272 		hwrm_req_drop(bp, req_rx);
8273 		return rc;
8274 	}
8275 
8276 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8277 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8278 
8279 	hwrm_req_hold(bp, req_rx);
8280 	hwrm_req_hold(bp, req_tx);
8281 	for (i = 0; i < bp->cp_nr_rings; i++) {
8282 		struct bnxt_napi *bnapi = bp->bnapi[i];
8283 		struct bnxt_coal *hw_coal;
8284 
8285 		if (!bnapi->rx_ring)
8286 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8287 		else
8288 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8289 		if (rc)
8290 			break;
8291 
8292 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8293 			continue;
8294 
8295 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8296 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8297 			if (rc)
8298 				break;
8299 		}
8300 		if (bnapi->rx_ring)
8301 			hw_coal = &bp->rx_coal;
8302 		else
8303 			hw_coal = &bp->tx_coal;
8304 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8305 	}
8306 	hwrm_req_drop(bp, req_rx);
8307 	hwrm_req_drop(bp, req_tx);
8308 	return rc;
8309 }
8310 
8311 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8312 {
8313 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8314 	struct hwrm_stat_ctx_free_input *req;
8315 	int i;
8316 
8317 	if (!bp->bnapi)
8318 		return;
8319 
8320 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8321 		return;
8322 
8323 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8324 		return;
8325 	if (BNXT_FW_MAJ(bp) <= 20) {
8326 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8327 			hwrm_req_drop(bp, req);
8328 			return;
8329 		}
8330 		hwrm_req_hold(bp, req0);
8331 	}
8332 	hwrm_req_hold(bp, req);
8333 	for (i = 0; i < bp->cp_nr_rings; i++) {
8334 		struct bnxt_napi *bnapi = bp->bnapi[i];
8335 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8336 
8337 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8338 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8339 			if (req0) {
8340 				req0->stat_ctx_id = req->stat_ctx_id;
8341 				hwrm_req_send(bp, req0);
8342 			}
8343 			hwrm_req_send(bp, req);
8344 
8345 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8346 		}
8347 	}
8348 	hwrm_req_drop(bp, req);
8349 	if (req0)
8350 		hwrm_req_drop(bp, req0);
8351 }
8352 
8353 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8354 {
8355 	struct hwrm_stat_ctx_alloc_output *resp;
8356 	struct hwrm_stat_ctx_alloc_input *req;
8357 	int rc, i;
8358 
8359 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8360 		return 0;
8361 
8362 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8363 	if (rc)
8364 		return rc;
8365 
8366 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8367 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8368 
8369 	resp = hwrm_req_hold(bp, req);
8370 	for (i = 0; i < bp->cp_nr_rings; i++) {
8371 		struct bnxt_napi *bnapi = bp->bnapi[i];
8372 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8373 
8374 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8375 
8376 		rc = hwrm_req_send(bp, req);
8377 		if (rc)
8378 			break;
8379 
8380 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8381 
8382 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8383 	}
8384 	hwrm_req_drop(bp, req);
8385 	return rc;
8386 }
8387 
8388 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8389 {
8390 	struct hwrm_func_qcfg_output *resp;
8391 	struct hwrm_func_qcfg_input *req;
8392 	u16 flags;
8393 	int rc;
8394 
8395 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8396 	if (rc)
8397 		return rc;
8398 
8399 	req->fid = cpu_to_le16(0xffff);
8400 	resp = hwrm_req_hold(bp, req);
8401 	rc = hwrm_req_send(bp, req);
8402 	if (rc)
8403 		goto func_qcfg_exit;
8404 
8405 	flags = le16_to_cpu(resp->flags);
8406 #ifdef CONFIG_BNXT_SRIOV
8407 	if (BNXT_VF(bp)) {
8408 		struct bnxt_vf_info *vf = &bp->vf;
8409 
8410 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8411 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8412 			vf->flags |= BNXT_VF_TRUST;
8413 		else
8414 			vf->flags &= ~BNXT_VF_TRUST;
8415 	} else {
8416 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8417 	}
8418 #endif
8419 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8420 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8421 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8422 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8423 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8424 	}
8425 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8426 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8427 
8428 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8429 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8430 
8431 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8432 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8433 
8434 	switch (resp->port_partition_type) {
8435 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8436 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2:
8437 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8438 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8439 		bp->port_partition_type = resp->port_partition_type;
8440 		break;
8441 	}
8442 	if (bp->hwrm_spec_code < 0x10707 ||
8443 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8444 		bp->br_mode = BRIDGE_MODE_VEB;
8445 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8446 		bp->br_mode = BRIDGE_MODE_VEPA;
8447 	else
8448 		bp->br_mode = BRIDGE_MODE_UNDEF;
8449 
8450 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8451 	if (!bp->max_mtu)
8452 		bp->max_mtu = BNXT_MAX_MTU;
8453 
8454 	if (bp->db_size)
8455 		goto func_qcfg_exit;
8456 
8457 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8458 	if (BNXT_CHIP_P5(bp)) {
8459 		if (BNXT_PF(bp))
8460 			bp->db_offset = DB_PF_OFFSET_P5;
8461 		else
8462 			bp->db_offset = DB_VF_OFFSET_P5;
8463 	}
8464 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8465 				 1024);
8466 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8467 	    bp->db_size <= bp->db_offset)
8468 		bp->db_size = pci_resource_len(bp->pdev, 2);
8469 
8470 func_qcfg_exit:
8471 	hwrm_req_drop(bp, req);
8472 	return rc;
8473 }
8474 
8475 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8476 				      u8 init_val, u8 init_offset,
8477 				      bool init_mask_set)
8478 {
8479 	ctxm->init_value = init_val;
8480 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8481 	if (init_mask_set)
8482 		ctxm->init_offset = init_offset * 4;
8483 	else
8484 		ctxm->init_value = 0;
8485 }
8486 
8487 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8488 {
8489 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8490 	u16 type;
8491 
8492 	for (type = 0; type < ctx_max; type++) {
8493 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8494 		int n = 1;
8495 
8496 		if (!ctxm->max_entries || ctxm->pg_info)
8497 			continue;
8498 
8499 		if (ctxm->instance_bmap)
8500 			n = hweight32(ctxm->instance_bmap);
8501 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8502 		if (!ctxm->pg_info)
8503 			return -ENOMEM;
8504 	}
8505 	return 0;
8506 }
8507 
8508 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8509 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8510 
8511 #define BNXT_CTX_INIT_VALID(flags)	\
8512 	(!!((flags) &			\
8513 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8514 
8515 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8516 {
8517 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8518 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8519 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8520 	u16 type;
8521 	int rc;
8522 
8523 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8524 	if (rc)
8525 		return rc;
8526 
8527 	if (!ctx) {
8528 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8529 		if (!ctx)
8530 			return -ENOMEM;
8531 		bp->ctx = ctx;
8532 	}
8533 
8534 	resp = hwrm_req_hold(bp, req);
8535 
8536 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8537 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8538 		u8 init_val, init_off, i;
8539 		u32 max_entries;
8540 		u16 entry_size;
8541 		__le32 *p;
8542 		u32 flags;
8543 
8544 		req->type = cpu_to_le16(type);
8545 		rc = hwrm_req_send(bp, req);
8546 		if (rc)
8547 			goto ctx_done;
8548 		flags = le32_to_cpu(resp->flags);
8549 		type = le16_to_cpu(resp->next_valid_type);
8550 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8551 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8552 			continue;
8553 		}
8554 		entry_size = le16_to_cpu(resp->entry_size);
8555 		max_entries = le32_to_cpu(resp->max_num_entries);
8556 		if (ctxm->mem_valid) {
8557 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8558 			    ctxm->entry_size != entry_size ||
8559 			    ctxm->max_entries != max_entries)
8560 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8561 			else
8562 				continue;
8563 		}
8564 		ctxm->type = le16_to_cpu(resp->type);
8565 		ctxm->entry_size = entry_size;
8566 		ctxm->flags = flags;
8567 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8568 		ctxm->entry_multiple = resp->entry_multiple;
8569 		ctxm->max_entries = max_entries;
8570 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8571 		init_val = resp->ctx_init_value;
8572 		init_off = resp->ctx_init_offset;
8573 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8574 					  BNXT_CTX_INIT_VALID(flags));
8575 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8576 					      BNXT_MAX_SPLIT_ENTRY);
8577 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8578 		     i++, p++)
8579 			ctxm->split[i] = le32_to_cpu(*p);
8580 	}
8581 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8582 
8583 ctx_done:
8584 	hwrm_req_drop(bp, req);
8585 	return rc;
8586 }
8587 
8588 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8589 {
8590 	struct hwrm_func_backing_store_qcaps_output *resp;
8591 	struct hwrm_func_backing_store_qcaps_input *req;
8592 	int rc;
8593 
8594 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8595 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8596 		return 0;
8597 
8598 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8599 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8600 
8601 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8602 	if (rc)
8603 		return rc;
8604 
8605 	resp = hwrm_req_hold(bp, req);
8606 	rc = hwrm_req_send_silent(bp, req);
8607 	if (!rc) {
8608 		struct bnxt_ctx_mem_type *ctxm;
8609 		struct bnxt_ctx_mem_info *ctx;
8610 		u8 init_val, init_idx = 0;
8611 		u16 init_mask;
8612 
8613 		ctx = bp->ctx;
8614 		if (!ctx) {
8615 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8616 			if (!ctx) {
8617 				rc = -ENOMEM;
8618 				goto ctx_err;
8619 			}
8620 			bp->ctx = ctx;
8621 		}
8622 		init_val = resp->ctx_kind_initializer;
8623 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8624 
8625 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8626 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8627 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8628 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8629 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8630 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8631 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8632 					  (init_mask & (1 << init_idx++)) != 0);
8633 
8634 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8635 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8636 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8637 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8638 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8639 					  (init_mask & (1 << init_idx++)) != 0);
8640 
8641 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8642 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8643 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8644 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8645 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8646 					  (init_mask & (1 << init_idx++)) != 0);
8647 
8648 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8649 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8650 		ctxm->max_entries = ctxm->vnic_entries +
8651 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8652 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8653 		bnxt_init_ctx_initializer(ctxm, init_val,
8654 					  resp->vnic_init_offset,
8655 					  (init_mask & (1 << init_idx++)) != 0);
8656 
8657 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8658 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8659 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8660 		bnxt_init_ctx_initializer(ctxm, init_val,
8661 					  resp->stat_init_offset,
8662 					  (init_mask & (1 << init_idx++)) != 0);
8663 
8664 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8665 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8666 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8667 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8668 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8669 		if (!ctxm->entry_multiple)
8670 			ctxm->entry_multiple = 1;
8671 
8672 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8673 
8674 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8675 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8676 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8677 		ctxm->mrav_num_entries_units =
8678 			le16_to_cpu(resp->mrav_num_entries_units);
8679 		bnxt_init_ctx_initializer(ctxm, init_val,
8680 					  resp->mrav_init_offset,
8681 					  (init_mask & (1 << init_idx++)) != 0);
8682 
8683 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8684 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8685 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8686 
8687 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8688 		if (!ctx->tqm_fp_rings_count)
8689 			ctx->tqm_fp_rings_count = bp->max_q;
8690 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8691 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8692 
8693 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8694 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8695 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8696 
8697 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8698 	} else {
8699 		rc = 0;
8700 	}
8701 ctx_err:
8702 	hwrm_req_drop(bp, req);
8703 	return rc;
8704 }
8705 
8706 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8707 				  __le64 *pg_dir)
8708 {
8709 	if (!rmem->nr_pages)
8710 		return;
8711 
8712 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8713 	if (rmem->depth >= 1) {
8714 		if (rmem->depth == 2)
8715 			*pg_attr |= 2;
8716 		else
8717 			*pg_attr |= 1;
8718 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8719 	} else {
8720 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8721 	}
8722 }
8723 
8724 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8725 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8726 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8727 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8728 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8729 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8730 
8731 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8732 {
8733 	struct hwrm_func_backing_store_cfg_input *req;
8734 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8735 	struct bnxt_ctx_pg_info *ctx_pg;
8736 	struct bnxt_ctx_mem_type *ctxm;
8737 	void **__req = (void **)&req;
8738 	u32 req_len = sizeof(*req);
8739 	__le32 *num_entries;
8740 	__le64 *pg_dir;
8741 	u32 flags = 0;
8742 	u8 *pg_attr;
8743 	u32 ena;
8744 	int rc;
8745 	int i;
8746 
8747 	if (!ctx)
8748 		return 0;
8749 
8750 	if (req_len > bp->hwrm_max_ext_req_len)
8751 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8752 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8753 	if (rc)
8754 		return rc;
8755 
8756 	req->enables = cpu_to_le32(enables);
8757 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8758 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8759 		ctx_pg = ctxm->pg_info;
8760 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8761 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8762 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8763 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8764 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8765 				      &req->qpc_pg_size_qpc_lvl,
8766 				      &req->qpc_page_dir);
8767 
8768 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8769 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8770 	}
8771 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8772 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8773 		ctx_pg = ctxm->pg_info;
8774 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8775 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8776 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8777 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8778 				      &req->srq_pg_size_srq_lvl,
8779 				      &req->srq_page_dir);
8780 	}
8781 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8782 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8783 		ctx_pg = ctxm->pg_info;
8784 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8785 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8786 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8787 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8788 				      &req->cq_pg_size_cq_lvl,
8789 				      &req->cq_page_dir);
8790 	}
8791 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8792 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8793 		ctx_pg = ctxm->pg_info;
8794 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8795 		req->vnic_num_ring_table_entries =
8796 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8797 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8798 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8799 				      &req->vnic_pg_size_vnic_lvl,
8800 				      &req->vnic_page_dir);
8801 	}
8802 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8803 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8804 		ctx_pg = ctxm->pg_info;
8805 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8806 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8807 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8808 				      &req->stat_pg_size_stat_lvl,
8809 				      &req->stat_page_dir);
8810 	}
8811 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8812 		u32 units;
8813 
8814 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8815 		ctx_pg = ctxm->pg_info;
8816 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8817 		units = ctxm->mrav_num_entries_units;
8818 		if (units) {
8819 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8820 			u32 entries;
8821 
8822 			num_mr = ctx_pg->entries - num_ah;
8823 			entries = ((num_mr / units) << 16) | (num_ah / units);
8824 			req->mrav_num_entries = cpu_to_le32(entries);
8825 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8826 		}
8827 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8828 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8829 				      &req->mrav_pg_size_mrav_lvl,
8830 				      &req->mrav_page_dir);
8831 	}
8832 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8833 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8834 		ctx_pg = ctxm->pg_info;
8835 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8836 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8837 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8838 				      &req->tim_pg_size_tim_lvl,
8839 				      &req->tim_page_dir);
8840 	}
8841 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8842 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8843 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8844 	     pg_dir = &req->tqm_sp_page_dir,
8845 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8846 	     ctx_pg = ctxm->pg_info;
8847 	     i < BNXT_MAX_TQM_RINGS;
8848 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8849 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8850 		if (!(enables & ena))
8851 			continue;
8852 
8853 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8854 		*num_entries = cpu_to_le32(ctx_pg->entries);
8855 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8856 	}
8857 	req->flags = cpu_to_le32(flags);
8858 	return hwrm_req_send(bp, req);
8859 }
8860 
8861 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8862 				  struct bnxt_ctx_pg_info *ctx_pg)
8863 {
8864 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8865 
8866 	rmem->page_size = BNXT_PAGE_SIZE;
8867 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8868 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8869 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8870 	if (rmem->depth >= 1)
8871 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8872 	return bnxt_alloc_ring(bp, rmem);
8873 }
8874 
8875 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8876 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8877 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8878 {
8879 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8880 	int rc;
8881 
8882 	if (!mem_size)
8883 		return -EINVAL;
8884 
8885 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8886 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8887 		ctx_pg->nr_pages = 0;
8888 		return -EINVAL;
8889 	}
8890 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8891 		int nr_tbls, i;
8892 
8893 		rmem->depth = 2;
8894 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8895 					     GFP_KERNEL);
8896 		if (!ctx_pg->ctx_pg_tbl)
8897 			return -ENOMEM;
8898 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8899 		rmem->nr_pages = nr_tbls;
8900 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8901 		if (rc)
8902 			return rc;
8903 		for (i = 0; i < nr_tbls; i++) {
8904 			struct bnxt_ctx_pg_info *pg_tbl;
8905 
8906 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8907 			if (!pg_tbl)
8908 				return -ENOMEM;
8909 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8910 			rmem = &pg_tbl->ring_mem;
8911 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8912 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8913 			rmem->depth = 1;
8914 			rmem->nr_pages = MAX_CTX_PAGES;
8915 			rmem->ctx_mem = ctxm;
8916 			if (i == (nr_tbls - 1)) {
8917 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8918 
8919 				if (rem)
8920 					rmem->nr_pages = rem;
8921 			}
8922 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8923 			if (rc)
8924 				break;
8925 		}
8926 	} else {
8927 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8928 		if (rmem->nr_pages > 1 || depth)
8929 			rmem->depth = 1;
8930 		rmem->ctx_mem = ctxm;
8931 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8932 	}
8933 	return rc;
8934 }
8935 
8936 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
8937 				    struct bnxt_ctx_pg_info *ctx_pg,
8938 				    void *buf, size_t offset, size_t head,
8939 				    size_t tail)
8940 {
8941 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8942 	size_t nr_pages = ctx_pg->nr_pages;
8943 	int page_size = rmem->page_size;
8944 	size_t len = 0, total_len = 0;
8945 	u16 depth = rmem->depth;
8946 
8947 	tail %= nr_pages * page_size;
8948 	do {
8949 		if (depth > 1) {
8950 			int i = head / (page_size * MAX_CTX_PAGES);
8951 			struct bnxt_ctx_pg_info *pg_tbl;
8952 
8953 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8954 			rmem = &pg_tbl->ring_mem;
8955 		}
8956 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
8957 		head += len;
8958 		offset += len;
8959 		total_len += len;
8960 		if (head >= nr_pages * page_size)
8961 			head = 0;
8962 	} while (head != tail);
8963 	return total_len;
8964 }
8965 
8966 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
8967 				  struct bnxt_ctx_pg_info *ctx_pg)
8968 {
8969 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8970 
8971 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
8972 	    ctx_pg->ctx_pg_tbl) {
8973 		int i, nr_tbls = rmem->nr_pages;
8974 
8975 		for (i = 0; i < nr_tbls; i++) {
8976 			struct bnxt_ctx_pg_info *pg_tbl;
8977 			struct bnxt_ring_mem_info *rmem2;
8978 
8979 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8980 			if (!pg_tbl)
8981 				continue;
8982 			rmem2 = &pg_tbl->ring_mem;
8983 			bnxt_free_ring(bp, rmem2);
8984 			ctx_pg->ctx_pg_arr[i] = NULL;
8985 			kfree(pg_tbl);
8986 			ctx_pg->ctx_pg_tbl[i] = NULL;
8987 		}
8988 		kfree(ctx_pg->ctx_pg_tbl);
8989 		ctx_pg->ctx_pg_tbl = NULL;
8990 	}
8991 	bnxt_free_ring(bp, rmem);
8992 	ctx_pg->nr_pages = 0;
8993 }
8994 
8995 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
8996 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
8997 				   u8 pg_lvl)
8998 {
8999 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9000 	int i, rc = 0, n = 1;
9001 	u32 mem_size;
9002 
9003 	if (!ctxm->entry_size || !ctx_pg)
9004 		return -EINVAL;
9005 	if (ctxm->instance_bmap)
9006 		n = hweight32(ctxm->instance_bmap);
9007 	if (ctxm->entry_multiple)
9008 		entries = roundup(entries, ctxm->entry_multiple);
9009 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
9010 	mem_size = entries * ctxm->entry_size;
9011 	for (i = 0; i < n && !rc; i++) {
9012 		ctx_pg[i].entries = entries;
9013 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
9014 					    ctxm->init_value ? ctxm : NULL);
9015 	}
9016 	if (!rc)
9017 		ctxm->mem_valid = 1;
9018 	return rc;
9019 }
9020 
9021 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
9022 					       struct bnxt_ctx_mem_type *ctxm,
9023 					       bool last)
9024 {
9025 	struct hwrm_func_backing_store_cfg_v2_input *req;
9026 	u32 instance_bmap = ctxm->instance_bmap;
9027 	int i, j, rc = 0, n = 1;
9028 	__le32 *p;
9029 
9030 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
9031 		return 0;
9032 
9033 	if (instance_bmap)
9034 		n = hweight32(ctxm->instance_bmap);
9035 	else
9036 		instance_bmap = 1;
9037 
9038 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
9039 	if (rc)
9040 		return rc;
9041 	hwrm_req_hold(bp, req);
9042 	req->type = cpu_to_le16(ctxm->type);
9043 	req->entry_size = cpu_to_le16(ctxm->entry_size);
9044 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
9045 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
9046 		struct bnxt_bs_trace_info *bs_trace;
9047 		u32 enables;
9048 
9049 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
9050 		req->enables = cpu_to_le32(enables);
9051 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
9052 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
9053 	}
9054 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
9055 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
9056 		p[i] = cpu_to_le32(ctxm->split[i]);
9057 	for (i = 0, j = 0; j < n && !rc; i++) {
9058 		struct bnxt_ctx_pg_info *ctx_pg;
9059 
9060 		if (!(instance_bmap & (1 << i)))
9061 			continue;
9062 		req->instance = cpu_to_le16(i);
9063 		ctx_pg = &ctxm->pg_info[j++];
9064 		if (!ctx_pg->entries)
9065 			continue;
9066 		req->num_entries = cpu_to_le32(ctx_pg->entries);
9067 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9068 				      &req->page_size_pbl_level,
9069 				      &req->page_dir);
9070 		if (last && j == n)
9071 			req->flags =
9072 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
9073 		rc = hwrm_req_send(bp, req);
9074 	}
9075 	hwrm_req_drop(bp, req);
9076 	return rc;
9077 }
9078 
9079 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
9080 {
9081 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9082 	struct bnxt_ctx_mem_type *ctxm;
9083 	u16 last_type = BNXT_CTX_INV;
9084 	int rc = 0;
9085 	u16 type;
9086 
9087 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_RIGP1; type++) {
9088 		ctxm = &ctx->ctx_arr[type];
9089 		if (!bnxt_bs_trace_avail(bp, type))
9090 			continue;
9091 		if (!ctxm->mem_valid) {
9092 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9093 						     ctxm->max_entries, 1);
9094 			if (rc) {
9095 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9096 					    type);
9097 				continue;
9098 			}
9099 			bnxt_bs_trace_init(bp, ctxm);
9100 		}
9101 		last_type = type;
9102 	}
9103 
9104 	if (last_type == BNXT_CTX_INV) {
9105 		if (!ena)
9106 			return 0;
9107 		else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
9108 			last_type = BNXT_CTX_MAX - 1;
9109 		else
9110 			last_type = BNXT_CTX_L2_MAX - 1;
9111 	}
9112 	ctx->ctx_arr[last_type].last = 1;
9113 
9114 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9115 		ctxm = &ctx->ctx_arr[type];
9116 
9117 		if (!ctxm->mem_valid)
9118 			continue;
9119 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9120 		if (rc)
9121 			return rc;
9122 	}
9123 	return 0;
9124 }
9125 
9126 /**
9127  * __bnxt_copy_ctx_mem - copy host context memory
9128  * @bp: The driver context
9129  * @ctxm: The pointer to the context memory type
9130  * @buf: The destination buffer or NULL to just obtain the length
9131  * @offset: The buffer offset to copy the data to
9132  * @head: The head offset of context memory to copy from
9133  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9134  *
9135  * This function is called for debugging purposes to dump the host context
9136  * used by the chip.
9137  *
9138  * Return: Length of memory copied
9139  */
9140 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9141 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9142 				  size_t offset, size_t head, size_t tail)
9143 {
9144 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9145 	size_t len = 0, total_len = 0;
9146 	int i, n = 1;
9147 
9148 	if (!ctx_pg)
9149 		return 0;
9150 
9151 	if (ctxm->instance_bmap)
9152 		n = hweight32(ctxm->instance_bmap);
9153 	for (i = 0; i < n; i++) {
9154 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9155 					    tail);
9156 		offset += len;
9157 		total_len += len;
9158 	}
9159 	return total_len;
9160 }
9161 
9162 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9163 			 void *buf, size_t offset)
9164 {
9165 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9166 
9167 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9168 }
9169 
9170 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9171 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9172 {
9173 	struct bnxt_ctx_pg_info *ctx_pg;
9174 	int i, n = 1;
9175 
9176 	ctxm->last = 0;
9177 
9178 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9179 		return;
9180 
9181 	ctx_pg = ctxm->pg_info;
9182 	if (ctx_pg) {
9183 		if (ctxm->instance_bmap)
9184 			n = hweight32(ctxm->instance_bmap);
9185 		for (i = 0; i < n; i++)
9186 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9187 
9188 		kfree(ctx_pg);
9189 		ctxm->pg_info = NULL;
9190 		ctxm->mem_valid = 0;
9191 	}
9192 	memset(ctxm, 0, sizeof(*ctxm));
9193 }
9194 
9195 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9196 {
9197 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9198 	u16 type;
9199 
9200 	if (!ctx)
9201 		return;
9202 
9203 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9204 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9205 
9206 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9207 	if (force) {
9208 		kfree(ctx);
9209 		bp->ctx = NULL;
9210 	}
9211 }
9212 
9213 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9214 {
9215 	struct bnxt_ctx_mem_type *ctxm;
9216 	struct bnxt_ctx_mem_info *ctx;
9217 	u32 l2_qps, qp1_qps, max_qps;
9218 	u32 ena, entries_sp, entries;
9219 	u32 srqs, max_srqs, min;
9220 	u32 num_mr, num_ah;
9221 	u32 extra_srqs = 0;
9222 	u32 extra_qps = 0;
9223 	u32 fast_qpmd_qps;
9224 	u8 pg_lvl = 1;
9225 	int i, rc;
9226 
9227 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9228 	if (rc) {
9229 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9230 			   rc);
9231 		return rc;
9232 	}
9233 	ctx = bp->ctx;
9234 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9235 		return 0;
9236 
9237 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9238 	l2_qps = ctxm->qp_l2_entries;
9239 	qp1_qps = ctxm->qp_qp1_entries;
9240 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9241 	max_qps = ctxm->max_entries;
9242 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9243 	srqs = ctxm->srq_l2_entries;
9244 	max_srqs = ctxm->max_entries;
9245 	ena = 0;
9246 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9247 		pg_lvl = 2;
9248 		if (BNXT_SW_RES_LMT(bp)) {
9249 			extra_qps = max_qps - l2_qps - qp1_qps;
9250 			extra_srqs = max_srqs - srqs;
9251 		} else {
9252 			extra_qps = min_t(u32, 65536,
9253 					  max_qps - l2_qps - qp1_qps);
9254 			/* allocate extra qps if fw supports RoCE fast qp
9255 			 * destroy feature
9256 			 */
9257 			extra_qps += fast_qpmd_qps;
9258 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9259 		}
9260 		if (fast_qpmd_qps)
9261 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9262 	}
9263 
9264 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9265 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9266 				     pg_lvl);
9267 	if (rc)
9268 		return rc;
9269 
9270 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9271 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9272 	if (rc)
9273 		return rc;
9274 
9275 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9276 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9277 				     extra_qps * 2, pg_lvl);
9278 	if (rc)
9279 		return rc;
9280 
9281 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9282 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9283 	if (rc)
9284 		return rc;
9285 
9286 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9287 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9288 	if (rc)
9289 		return rc;
9290 
9291 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9292 		goto skip_rdma;
9293 
9294 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9295 	if (BNXT_SW_RES_LMT(bp) &&
9296 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9297 		num_ah = ctxm->mrav_av_entries;
9298 		num_mr = ctxm->max_entries - num_ah;
9299 	} else {
9300 		/* 128K extra is needed to accommodate static AH context
9301 		 * allocation by f/w.
9302 		 */
9303 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9304 		num_ah = min_t(u32, num_mr, 1024 * 128);
9305 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9306 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9307 			ctxm->mrav_av_entries = num_ah;
9308 	}
9309 
9310 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9311 	if (rc)
9312 		return rc;
9313 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9314 
9315 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9316 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9317 	if (rc)
9318 		return rc;
9319 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9320 
9321 skip_rdma:
9322 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9323 	min = ctxm->min_entries;
9324 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9325 		     2 * (extra_qps + qp1_qps) + min;
9326 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9327 	if (rc)
9328 		return rc;
9329 
9330 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9331 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9332 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9333 	if (rc)
9334 		return rc;
9335 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9336 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9337 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9338 
9339 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9340 		rc = bnxt_backing_store_cfg_v2(bp, ena);
9341 	else
9342 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9343 	if (rc) {
9344 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9345 			   rc);
9346 		return rc;
9347 	}
9348 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9349 	return 0;
9350 }
9351 
9352 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9353 {
9354 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9355 	u16 page_attr;
9356 	int rc;
9357 
9358 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9359 		return 0;
9360 
9361 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9362 	if (rc)
9363 		return rc;
9364 
9365 	if (BNXT_PAGE_SIZE == 0x2000)
9366 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9367 	else if (BNXT_PAGE_SIZE == 0x10000)
9368 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9369 	else
9370 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9371 	req->pg_size_lvl = cpu_to_le16(page_attr |
9372 				       bp->fw_crash_mem->ring_mem.depth);
9373 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9374 	req->size = cpu_to_le32(bp->fw_crash_len);
9375 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9376 	return hwrm_req_send(bp, req);
9377 }
9378 
9379 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9380 {
9381 	if (bp->fw_crash_mem) {
9382 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9383 		kfree(bp->fw_crash_mem);
9384 		bp->fw_crash_mem = NULL;
9385 	}
9386 }
9387 
9388 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9389 {
9390 	u32 mem_size = 0;
9391 	int rc;
9392 
9393 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9394 		return 0;
9395 
9396 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9397 	if (rc)
9398 		return rc;
9399 
9400 	mem_size = round_up(mem_size, 4);
9401 
9402 	/* keep and use the existing pages */
9403 	if (bp->fw_crash_mem &&
9404 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9405 		goto alloc_done;
9406 
9407 	if (bp->fw_crash_mem)
9408 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9409 	else
9410 		bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem),
9411 					   GFP_KERNEL);
9412 	if (!bp->fw_crash_mem)
9413 		return -ENOMEM;
9414 
9415 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9416 	if (rc) {
9417 		bnxt_free_crash_dump_mem(bp);
9418 		return rc;
9419 	}
9420 
9421 alloc_done:
9422 	bp->fw_crash_len = mem_size;
9423 	return 0;
9424 }
9425 
9426 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9427 {
9428 	struct hwrm_func_resource_qcaps_output *resp;
9429 	struct hwrm_func_resource_qcaps_input *req;
9430 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9431 	int rc;
9432 
9433 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9434 	if (rc)
9435 		return rc;
9436 
9437 	req->fid = cpu_to_le16(0xffff);
9438 	resp = hwrm_req_hold(bp, req);
9439 	rc = hwrm_req_send_silent(bp, req);
9440 	if (rc)
9441 		goto hwrm_func_resc_qcaps_exit;
9442 
9443 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9444 	if (!all)
9445 		goto hwrm_func_resc_qcaps_exit;
9446 
9447 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9448 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9449 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9450 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9451 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9452 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9453 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9454 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9455 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9456 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9457 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9458 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9459 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9460 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9461 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9462 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9463 
9464 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9465 		u16 max_msix = le16_to_cpu(resp->max_msix);
9466 
9467 		hw_resc->max_nqs = max_msix;
9468 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9469 	}
9470 
9471 	if (BNXT_PF(bp)) {
9472 		struct bnxt_pf_info *pf = &bp->pf;
9473 
9474 		pf->vf_resv_strategy =
9475 			le16_to_cpu(resp->vf_reservation_strategy);
9476 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9477 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9478 	}
9479 hwrm_func_resc_qcaps_exit:
9480 	hwrm_req_drop(bp, req);
9481 	return rc;
9482 }
9483 
9484 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9485 {
9486 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9487 	struct hwrm_port_mac_ptp_qcfg_input *req;
9488 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9489 	u8 flags;
9490 	int rc;
9491 
9492 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9493 		rc = -ENODEV;
9494 		goto no_ptp;
9495 	}
9496 
9497 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9498 	if (rc)
9499 		goto no_ptp;
9500 
9501 	req->port_id = cpu_to_le16(bp->pf.port_id);
9502 	resp = hwrm_req_hold(bp, req);
9503 	rc = hwrm_req_send(bp, req);
9504 	if (rc)
9505 		goto exit;
9506 
9507 	flags = resp->flags;
9508 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9509 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9510 		rc = -ENODEV;
9511 		goto exit;
9512 	}
9513 	if (!ptp) {
9514 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
9515 		if (!ptp) {
9516 			rc = -ENOMEM;
9517 			goto exit;
9518 		}
9519 		ptp->bp = bp;
9520 		bp->ptp_cfg = ptp;
9521 	}
9522 
9523 	if (flags &
9524 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9525 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9526 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9527 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9528 	} else if (BNXT_CHIP_P5(bp)) {
9529 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9530 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9531 	} else {
9532 		rc = -ENODEV;
9533 		goto exit;
9534 	}
9535 	ptp->rtc_configured =
9536 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9537 	rc = bnxt_ptp_init(bp);
9538 	if (rc)
9539 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9540 exit:
9541 	hwrm_req_drop(bp, req);
9542 	if (!rc)
9543 		return 0;
9544 
9545 no_ptp:
9546 	bnxt_ptp_clear(bp);
9547 	kfree(ptp);
9548 	bp->ptp_cfg = NULL;
9549 	return rc;
9550 }
9551 
9552 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9553 {
9554 	struct hwrm_func_qcaps_output *resp;
9555 	struct hwrm_func_qcaps_input *req;
9556 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9557 	u32 flags, flags_ext, flags_ext2;
9558 	int rc;
9559 
9560 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9561 	if (rc)
9562 		return rc;
9563 
9564 	req->fid = cpu_to_le16(0xffff);
9565 	resp = hwrm_req_hold(bp, req);
9566 	rc = hwrm_req_send(bp, req);
9567 	if (rc)
9568 		goto hwrm_func_qcaps_exit;
9569 
9570 	flags = le32_to_cpu(resp->flags);
9571 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9572 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9573 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9574 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9575 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9576 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9577 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9578 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9579 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9580 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9581 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9582 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9583 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9584 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9585 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9586 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9587 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9588 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9589 
9590 	flags_ext = le32_to_cpu(resp->flags_ext);
9591 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9592 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9593 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9594 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9595 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9596 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9597 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9598 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9599 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9600 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9601 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED)
9602 		bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2;
9603 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9604 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9605 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9606 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9607 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9608 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9609 
9610 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9611 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9612 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9613 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9614 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9615 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9616 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9617 	if (flags_ext2 &
9618 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9619 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9620 	if (BNXT_PF(bp) &&
9621 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9622 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9623 
9624 	bp->tx_push_thresh = 0;
9625 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9626 	    BNXT_FW_MAJ(bp) > 217)
9627 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9628 
9629 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9630 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9631 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9632 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9633 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9634 	if (!hw_resc->max_hw_ring_grps)
9635 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9636 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9637 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9638 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9639 
9640 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9641 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9642 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9643 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9644 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9645 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9646 
9647 	if (BNXT_PF(bp)) {
9648 		struct bnxt_pf_info *pf = &bp->pf;
9649 
9650 		pf->fw_fid = le16_to_cpu(resp->fid);
9651 		pf->port_id = le16_to_cpu(resp->port_id);
9652 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9653 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9654 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9655 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9656 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9657 			bp->flags |= BNXT_FLAG_WOL_CAP;
9658 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9659 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9660 		} else {
9661 			bnxt_ptp_clear(bp);
9662 			kfree(bp->ptp_cfg);
9663 			bp->ptp_cfg = NULL;
9664 		}
9665 	} else {
9666 #ifdef CONFIG_BNXT_SRIOV
9667 		struct bnxt_vf_info *vf = &bp->vf;
9668 
9669 		vf->fw_fid = le16_to_cpu(resp->fid);
9670 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9671 #endif
9672 	}
9673 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9674 
9675 hwrm_func_qcaps_exit:
9676 	hwrm_req_drop(bp, req);
9677 	return rc;
9678 }
9679 
9680 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9681 {
9682 	struct hwrm_dbg_qcaps_output *resp;
9683 	struct hwrm_dbg_qcaps_input *req;
9684 	int rc;
9685 
9686 	bp->fw_dbg_cap = 0;
9687 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9688 		return;
9689 
9690 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9691 	if (rc)
9692 		return;
9693 
9694 	req->fid = cpu_to_le16(0xffff);
9695 	resp = hwrm_req_hold(bp, req);
9696 	rc = hwrm_req_send(bp, req);
9697 	if (rc)
9698 		goto hwrm_dbg_qcaps_exit;
9699 
9700 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9701 
9702 hwrm_dbg_qcaps_exit:
9703 	hwrm_req_drop(bp, req);
9704 }
9705 
9706 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9707 
9708 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9709 {
9710 	int rc;
9711 
9712 	rc = __bnxt_hwrm_func_qcaps(bp);
9713 	if (rc)
9714 		return rc;
9715 
9716 	bnxt_hwrm_dbg_qcaps(bp);
9717 
9718 	rc = bnxt_hwrm_queue_qportcfg(bp);
9719 	if (rc) {
9720 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9721 		return rc;
9722 	}
9723 	if (bp->hwrm_spec_code >= 0x10803) {
9724 		rc = bnxt_alloc_ctx_mem(bp);
9725 		if (rc)
9726 			return rc;
9727 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9728 		if (!rc)
9729 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9730 	}
9731 	return 0;
9732 }
9733 
9734 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9735 {
9736 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9737 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9738 	u32 flags;
9739 	int rc;
9740 
9741 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9742 		return 0;
9743 
9744 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9745 	if (rc)
9746 		return rc;
9747 
9748 	resp = hwrm_req_hold(bp, req);
9749 	rc = hwrm_req_send(bp, req);
9750 	if (rc)
9751 		goto hwrm_cfa_adv_qcaps_exit;
9752 
9753 	flags = le32_to_cpu(resp->flags);
9754 	if (flags &
9755 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9756 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9757 
9758 	if (flags &
9759 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9760 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9761 
9762 	if (flags &
9763 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9764 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9765 
9766 hwrm_cfa_adv_qcaps_exit:
9767 	hwrm_req_drop(bp, req);
9768 	return rc;
9769 }
9770 
9771 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9772 {
9773 	if (bp->fw_health)
9774 		return 0;
9775 
9776 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9777 	if (!bp->fw_health)
9778 		return -ENOMEM;
9779 
9780 	mutex_init(&bp->fw_health->lock);
9781 	return 0;
9782 }
9783 
9784 static int bnxt_alloc_fw_health(struct bnxt *bp)
9785 {
9786 	int rc;
9787 
9788 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9789 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9790 		return 0;
9791 
9792 	rc = __bnxt_alloc_fw_health(bp);
9793 	if (rc) {
9794 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9795 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9796 		return rc;
9797 	}
9798 
9799 	return 0;
9800 }
9801 
9802 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9803 {
9804 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9805 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9806 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9807 }
9808 
9809 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9810 {
9811 	struct bnxt_fw_health *fw_health = bp->fw_health;
9812 	u32 reg_type;
9813 
9814 	if (!fw_health)
9815 		return;
9816 
9817 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9818 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9819 		fw_health->status_reliable = false;
9820 
9821 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9822 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9823 		fw_health->resets_reliable = false;
9824 }
9825 
9826 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9827 {
9828 	void __iomem *hs;
9829 	u32 status_loc;
9830 	u32 reg_type;
9831 	u32 sig;
9832 
9833 	if (bp->fw_health)
9834 		bp->fw_health->status_reliable = false;
9835 
9836 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9837 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9838 
9839 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9840 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9841 		if (!bp->chip_num) {
9842 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9843 			bp->chip_num = readl(bp->bar0 +
9844 					     BNXT_FW_HEALTH_WIN_BASE +
9845 					     BNXT_GRC_REG_CHIP_NUM);
9846 		}
9847 		if (!BNXT_CHIP_P5_PLUS(bp))
9848 			return;
9849 
9850 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9851 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9852 	} else {
9853 		status_loc = readl(hs + offsetof(struct hcomm_status,
9854 						 fw_status_loc));
9855 	}
9856 
9857 	if (__bnxt_alloc_fw_health(bp)) {
9858 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9859 		return;
9860 	}
9861 
9862 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9863 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9864 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9865 		__bnxt_map_fw_health_reg(bp, status_loc);
9866 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9867 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9868 	}
9869 
9870 	bp->fw_health->status_reliable = true;
9871 }
9872 
9873 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9874 {
9875 	struct bnxt_fw_health *fw_health = bp->fw_health;
9876 	u32 reg_base = 0xffffffff;
9877 	int i;
9878 
9879 	bp->fw_health->status_reliable = false;
9880 	bp->fw_health->resets_reliable = false;
9881 	/* Only pre-map the monitoring GRC registers using window 3 */
9882 	for (i = 0; i < 4; i++) {
9883 		u32 reg = fw_health->regs[i];
9884 
9885 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9886 			continue;
9887 		if (reg_base == 0xffffffff)
9888 			reg_base = reg & BNXT_GRC_BASE_MASK;
9889 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9890 			return -ERANGE;
9891 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9892 	}
9893 	bp->fw_health->status_reliable = true;
9894 	bp->fw_health->resets_reliable = true;
9895 	if (reg_base == 0xffffffff)
9896 		return 0;
9897 
9898 	__bnxt_map_fw_health_reg(bp, reg_base);
9899 	return 0;
9900 }
9901 
9902 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9903 {
9904 	if (!bp->fw_health)
9905 		return;
9906 
9907 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9908 		bp->fw_health->status_reliable = true;
9909 		bp->fw_health->resets_reliable = true;
9910 	} else {
9911 		bnxt_try_map_fw_health_reg(bp);
9912 	}
9913 }
9914 
9915 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9916 {
9917 	struct bnxt_fw_health *fw_health = bp->fw_health;
9918 	struct hwrm_error_recovery_qcfg_output *resp;
9919 	struct hwrm_error_recovery_qcfg_input *req;
9920 	int rc, i;
9921 
9922 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9923 		return 0;
9924 
9925 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9926 	if (rc)
9927 		return rc;
9928 
9929 	resp = hwrm_req_hold(bp, req);
9930 	rc = hwrm_req_send(bp, req);
9931 	if (rc)
9932 		goto err_recovery_out;
9933 	fw_health->flags = le32_to_cpu(resp->flags);
9934 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
9935 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
9936 		rc = -EINVAL;
9937 		goto err_recovery_out;
9938 	}
9939 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
9940 	fw_health->master_func_wait_dsecs =
9941 		le32_to_cpu(resp->master_func_wait_period);
9942 	fw_health->normal_func_wait_dsecs =
9943 		le32_to_cpu(resp->normal_func_wait_period);
9944 	fw_health->post_reset_wait_dsecs =
9945 		le32_to_cpu(resp->master_func_wait_period_after_reset);
9946 	fw_health->post_reset_max_wait_dsecs =
9947 		le32_to_cpu(resp->max_bailout_time_after_reset);
9948 	fw_health->regs[BNXT_FW_HEALTH_REG] =
9949 		le32_to_cpu(resp->fw_health_status_reg);
9950 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
9951 		le32_to_cpu(resp->fw_heartbeat_reg);
9952 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
9953 		le32_to_cpu(resp->fw_reset_cnt_reg);
9954 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
9955 		le32_to_cpu(resp->reset_inprogress_reg);
9956 	fw_health->fw_reset_inprog_reg_mask =
9957 		le32_to_cpu(resp->reset_inprogress_reg_mask);
9958 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
9959 	if (fw_health->fw_reset_seq_cnt >= 16) {
9960 		rc = -EINVAL;
9961 		goto err_recovery_out;
9962 	}
9963 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
9964 		fw_health->fw_reset_seq_regs[i] =
9965 			le32_to_cpu(resp->reset_reg[i]);
9966 		fw_health->fw_reset_seq_vals[i] =
9967 			le32_to_cpu(resp->reset_reg_val[i]);
9968 		fw_health->fw_reset_seq_delay_msec[i] =
9969 			resp->delay_after_reset[i];
9970 	}
9971 err_recovery_out:
9972 	hwrm_req_drop(bp, req);
9973 	if (!rc)
9974 		rc = bnxt_map_fw_health_regs(bp);
9975 	if (rc)
9976 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9977 	return rc;
9978 }
9979 
9980 static int bnxt_hwrm_func_reset(struct bnxt *bp)
9981 {
9982 	struct hwrm_func_reset_input *req;
9983 	int rc;
9984 
9985 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
9986 	if (rc)
9987 		return rc;
9988 
9989 	req->enables = 0;
9990 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
9991 	return hwrm_req_send(bp, req);
9992 }
9993 
9994 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
9995 {
9996 	struct hwrm_nvm_get_dev_info_output nvm_info;
9997 
9998 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
9999 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
10000 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
10001 			 nvm_info.nvm_cfg_ver_upd);
10002 }
10003 
10004 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
10005 {
10006 	struct hwrm_queue_qportcfg_output *resp;
10007 	struct hwrm_queue_qportcfg_input *req;
10008 	u8 i, j, *qptr;
10009 	bool no_rdma;
10010 	int rc = 0;
10011 
10012 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
10013 	if (rc)
10014 		return rc;
10015 
10016 	resp = hwrm_req_hold(bp, req);
10017 	rc = hwrm_req_send(bp, req);
10018 	if (rc)
10019 		goto qportcfg_exit;
10020 
10021 	if (!resp->max_configurable_queues) {
10022 		rc = -EINVAL;
10023 		goto qportcfg_exit;
10024 	}
10025 	bp->max_tc = resp->max_configurable_queues;
10026 	bp->max_lltc = resp->max_configurable_lossless_queues;
10027 	if (bp->max_tc > BNXT_MAX_QUEUE)
10028 		bp->max_tc = BNXT_MAX_QUEUE;
10029 
10030 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
10031 	qptr = &resp->queue_id0;
10032 	for (i = 0, j = 0; i < bp->max_tc; i++) {
10033 		bp->q_info[j].queue_id = *qptr;
10034 		bp->q_ids[i] = *qptr++;
10035 		bp->q_info[j].queue_profile = *qptr++;
10036 		bp->tc_to_qidx[j] = j;
10037 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
10038 		    (no_rdma && BNXT_PF(bp)))
10039 			j++;
10040 	}
10041 	bp->max_q = bp->max_tc;
10042 	bp->max_tc = max_t(u8, j, 1);
10043 
10044 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
10045 		bp->max_tc = 1;
10046 
10047 	if (bp->max_lltc > bp->max_tc)
10048 		bp->max_lltc = bp->max_tc;
10049 
10050 qportcfg_exit:
10051 	hwrm_req_drop(bp, req);
10052 	return rc;
10053 }
10054 
10055 static int bnxt_hwrm_poll(struct bnxt *bp)
10056 {
10057 	struct hwrm_ver_get_input *req;
10058 	int rc;
10059 
10060 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10061 	if (rc)
10062 		return rc;
10063 
10064 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10065 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10066 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10067 
10068 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
10069 	rc = hwrm_req_send(bp, req);
10070 	return rc;
10071 }
10072 
10073 static int bnxt_hwrm_ver_get(struct bnxt *bp)
10074 {
10075 	struct hwrm_ver_get_output *resp;
10076 	struct hwrm_ver_get_input *req;
10077 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
10078 	u32 dev_caps_cfg, hwrm_ver;
10079 	int rc, len;
10080 
10081 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10082 	if (rc)
10083 		return rc;
10084 
10085 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10086 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
10087 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10088 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10089 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10090 
10091 	resp = hwrm_req_hold(bp, req);
10092 	rc = hwrm_req_send(bp, req);
10093 	if (rc)
10094 		goto hwrm_ver_get_exit;
10095 
10096 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
10097 
10098 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
10099 			     resp->hwrm_intf_min_8b << 8 |
10100 			     resp->hwrm_intf_upd_8b;
10101 	if (resp->hwrm_intf_maj_8b < 1) {
10102 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
10103 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10104 			    resp->hwrm_intf_upd_8b);
10105 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10106 	}
10107 
10108 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10109 			HWRM_VERSION_UPDATE;
10110 
10111 	if (bp->hwrm_spec_code > hwrm_ver)
10112 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10113 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10114 			 HWRM_VERSION_UPDATE);
10115 	else
10116 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10117 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10118 			 resp->hwrm_intf_upd_8b);
10119 
10120 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10121 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10122 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10123 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10124 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10125 		len = FW_VER_STR_LEN;
10126 	} else {
10127 		fw_maj = resp->hwrm_fw_maj_8b;
10128 		fw_min = resp->hwrm_fw_min_8b;
10129 		fw_bld = resp->hwrm_fw_bld_8b;
10130 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10131 		len = BC_HWRM_STR_LEN;
10132 	}
10133 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10134 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10135 		 fw_rsv);
10136 
10137 	if (strlen(resp->active_pkg_name)) {
10138 		int fw_ver_len = strlen(bp->fw_ver_str);
10139 
10140 		snprintf(bp->fw_ver_str + fw_ver_len,
10141 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10142 			 resp->active_pkg_name);
10143 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10144 	}
10145 
10146 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10147 	if (!bp->hwrm_cmd_timeout)
10148 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10149 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10150 	if (!bp->hwrm_cmd_max_timeout)
10151 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10152 	else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT)
10153 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n",
10154 			    bp->hwrm_cmd_max_timeout / 1000);
10155 
10156 	if (resp->hwrm_intf_maj_8b >= 1) {
10157 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10158 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10159 	}
10160 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10161 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10162 
10163 	bp->chip_num = le16_to_cpu(resp->chip_num);
10164 	bp->chip_rev = resp->chip_rev;
10165 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10166 	    !resp->chip_metal)
10167 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10168 
10169 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10170 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10171 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10172 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10173 
10174 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10175 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10176 
10177 	if (dev_caps_cfg &
10178 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10179 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10180 
10181 	if (dev_caps_cfg &
10182 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10183 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10184 
10185 	if (dev_caps_cfg &
10186 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10187 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10188 
10189 hwrm_ver_get_exit:
10190 	hwrm_req_drop(bp, req);
10191 	return rc;
10192 }
10193 
10194 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10195 {
10196 	struct hwrm_fw_set_time_input *req;
10197 	struct tm tm;
10198 	time64_t now = ktime_get_real_seconds();
10199 	int rc;
10200 
10201 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10202 	    bp->hwrm_spec_code < 0x10400)
10203 		return -EOPNOTSUPP;
10204 
10205 	time64_to_tm(now, 0, &tm);
10206 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10207 	if (rc)
10208 		return rc;
10209 
10210 	req->year = cpu_to_le16(1900 + tm.tm_year);
10211 	req->month = 1 + tm.tm_mon;
10212 	req->day = tm.tm_mday;
10213 	req->hour = tm.tm_hour;
10214 	req->minute = tm.tm_min;
10215 	req->second = tm.tm_sec;
10216 	return hwrm_req_send(bp, req);
10217 }
10218 
10219 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10220 {
10221 	u64 sw_tmp;
10222 
10223 	hw &= mask;
10224 	sw_tmp = (*sw & ~mask) | hw;
10225 	if (hw < (*sw & mask))
10226 		sw_tmp += mask + 1;
10227 	WRITE_ONCE(*sw, sw_tmp);
10228 }
10229 
10230 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10231 				    int count, bool ignore_zero)
10232 {
10233 	int i;
10234 
10235 	for (i = 0; i < count; i++) {
10236 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10237 
10238 		if (ignore_zero && !hw)
10239 			continue;
10240 
10241 		if (masks[i] == -1ULL)
10242 			sw_stats[i] = hw;
10243 		else
10244 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10245 	}
10246 }
10247 
10248 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10249 {
10250 	if (!stats->hw_stats)
10251 		return;
10252 
10253 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10254 				stats->hw_masks, stats->len / 8, false);
10255 }
10256 
10257 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10258 {
10259 	struct bnxt_stats_mem *ring0_stats;
10260 	bool ignore_zero = false;
10261 	int i;
10262 
10263 	/* Chip bug.  Counter intermittently becomes 0. */
10264 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10265 		ignore_zero = true;
10266 
10267 	for (i = 0; i < bp->cp_nr_rings; i++) {
10268 		struct bnxt_napi *bnapi = bp->bnapi[i];
10269 		struct bnxt_cp_ring_info *cpr;
10270 		struct bnxt_stats_mem *stats;
10271 
10272 		cpr = &bnapi->cp_ring;
10273 		stats = &cpr->stats;
10274 		if (!i)
10275 			ring0_stats = stats;
10276 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10277 					ring0_stats->hw_masks,
10278 					ring0_stats->len / 8, ignore_zero);
10279 	}
10280 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10281 		struct bnxt_stats_mem *stats = &bp->port_stats;
10282 		__le64 *hw_stats = stats->hw_stats;
10283 		u64 *sw_stats = stats->sw_stats;
10284 		u64 *masks = stats->hw_masks;
10285 		int cnt;
10286 
10287 		cnt = sizeof(struct rx_port_stats) / 8;
10288 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10289 
10290 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10291 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10292 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10293 		cnt = sizeof(struct tx_port_stats) / 8;
10294 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10295 	}
10296 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10297 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10298 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10299 	}
10300 }
10301 
10302 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10303 {
10304 	struct hwrm_port_qstats_input *req;
10305 	struct bnxt_pf_info *pf = &bp->pf;
10306 	int rc;
10307 
10308 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10309 		return 0;
10310 
10311 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10312 		return -EOPNOTSUPP;
10313 
10314 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10315 	if (rc)
10316 		return rc;
10317 
10318 	req->flags = flags;
10319 	req->port_id = cpu_to_le16(pf->port_id);
10320 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10321 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10322 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10323 	return hwrm_req_send(bp, req);
10324 }
10325 
10326 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10327 {
10328 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10329 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10330 	struct hwrm_port_qstats_ext_output *resp_qs;
10331 	struct hwrm_port_qstats_ext_input *req_qs;
10332 	struct bnxt_pf_info *pf = &bp->pf;
10333 	u32 tx_stat_size;
10334 	int rc;
10335 
10336 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10337 		return 0;
10338 
10339 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10340 		return -EOPNOTSUPP;
10341 
10342 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10343 	if (rc)
10344 		return rc;
10345 
10346 	req_qs->flags = flags;
10347 	req_qs->port_id = cpu_to_le16(pf->port_id);
10348 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10349 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10350 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10351 		       sizeof(struct tx_port_stats_ext) : 0;
10352 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10353 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10354 	resp_qs = hwrm_req_hold(bp, req_qs);
10355 	rc = hwrm_req_send(bp, req_qs);
10356 	if (!rc) {
10357 		bp->fw_rx_stats_ext_size =
10358 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10359 		if (BNXT_FW_MAJ(bp) < 220 &&
10360 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10361 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10362 
10363 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10364 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10365 	} else {
10366 		bp->fw_rx_stats_ext_size = 0;
10367 		bp->fw_tx_stats_ext_size = 0;
10368 	}
10369 	hwrm_req_drop(bp, req_qs);
10370 
10371 	if (flags)
10372 		return rc;
10373 
10374 	if (bp->fw_tx_stats_ext_size <=
10375 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10376 		bp->pri2cos_valid = 0;
10377 		return rc;
10378 	}
10379 
10380 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10381 	if (rc)
10382 		return rc;
10383 
10384 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10385 
10386 	resp_qc = hwrm_req_hold(bp, req_qc);
10387 	rc = hwrm_req_send(bp, req_qc);
10388 	if (!rc) {
10389 		u8 *pri2cos;
10390 		int i, j;
10391 
10392 		pri2cos = &resp_qc->pri0_cos_queue_id;
10393 		for (i = 0; i < 8; i++) {
10394 			u8 queue_id = pri2cos[i];
10395 			u8 queue_idx;
10396 
10397 			/* Per port queue IDs start from 0, 10, 20, etc */
10398 			queue_idx = queue_id % 10;
10399 			if (queue_idx > BNXT_MAX_QUEUE) {
10400 				bp->pri2cos_valid = false;
10401 				hwrm_req_drop(bp, req_qc);
10402 				return rc;
10403 			}
10404 			for (j = 0; j < bp->max_q; j++) {
10405 				if (bp->q_ids[j] == queue_id)
10406 					bp->pri2cos_idx[i] = queue_idx;
10407 			}
10408 		}
10409 		bp->pri2cos_valid = true;
10410 	}
10411 	hwrm_req_drop(bp, req_qc);
10412 
10413 	return rc;
10414 }
10415 
10416 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10417 {
10418 	bnxt_hwrm_tunnel_dst_port_free(bp,
10419 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10420 	bnxt_hwrm_tunnel_dst_port_free(bp,
10421 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10422 }
10423 
10424 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10425 {
10426 	int rc, i;
10427 	u32 tpa_flags = 0;
10428 
10429 	if (set_tpa)
10430 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10431 	else if (BNXT_NO_FW_ACCESS(bp))
10432 		return 0;
10433 	for (i = 0; i < bp->nr_vnics; i++) {
10434 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10435 		if (rc) {
10436 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10437 				   i, rc);
10438 			return rc;
10439 		}
10440 	}
10441 	return 0;
10442 }
10443 
10444 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10445 {
10446 	int i;
10447 
10448 	for (i = 0; i < bp->nr_vnics; i++)
10449 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10450 }
10451 
10452 static void bnxt_clear_vnic(struct bnxt *bp)
10453 {
10454 	if (!bp->vnic_info)
10455 		return;
10456 
10457 	bnxt_hwrm_clear_vnic_filter(bp);
10458 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10459 		/* clear all RSS setting before free vnic ctx */
10460 		bnxt_hwrm_clear_vnic_rss(bp);
10461 		bnxt_hwrm_vnic_ctx_free(bp);
10462 	}
10463 	/* before free the vnic, undo the vnic tpa settings */
10464 	if (bp->flags & BNXT_FLAG_TPA)
10465 		bnxt_set_tpa(bp, false);
10466 	bnxt_hwrm_vnic_free(bp);
10467 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10468 		bnxt_hwrm_vnic_ctx_free(bp);
10469 }
10470 
10471 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10472 				    bool irq_re_init)
10473 {
10474 	bnxt_clear_vnic(bp);
10475 	bnxt_hwrm_ring_free(bp, close_path);
10476 	bnxt_hwrm_ring_grp_free(bp);
10477 	if (irq_re_init) {
10478 		bnxt_hwrm_stat_ctx_free(bp);
10479 		bnxt_hwrm_free_tunnel_ports(bp);
10480 	}
10481 }
10482 
10483 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10484 {
10485 	struct hwrm_func_cfg_input *req;
10486 	u8 evb_mode;
10487 	int rc;
10488 
10489 	if (br_mode == BRIDGE_MODE_VEB)
10490 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10491 	else if (br_mode == BRIDGE_MODE_VEPA)
10492 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10493 	else
10494 		return -EINVAL;
10495 
10496 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10497 	if (rc)
10498 		return rc;
10499 
10500 	req->fid = cpu_to_le16(0xffff);
10501 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10502 	req->evb_mode = evb_mode;
10503 	return hwrm_req_send(bp, req);
10504 }
10505 
10506 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10507 {
10508 	struct hwrm_func_cfg_input *req;
10509 	int rc;
10510 
10511 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10512 		return 0;
10513 
10514 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10515 	if (rc)
10516 		return rc;
10517 
10518 	req->fid = cpu_to_le16(0xffff);
10519 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10520 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10521 	if (size == 128)
10522 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10523 
10524 	return hwrm_req_send(bp, req);
10525 }
10526 
10527 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10528 {
10529 	int rc;
10530 
10531 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10532 		goto skip_rss_ctx;
10533 
10534 	/* allocate context for vnic */
10535 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10536 	if (rc) {
10537 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10538 			   vnic->vnic_id, rc);
10539 		goto vnic_setup_err;
10540 	}
10541 	bp->rsscos_nr_ctxs++;
10542 
10543 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10544 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10545 		if (rc) {
10546 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10547 				   vnic->vnic_id, rc);
10548 			goto vnic_setup_err;
10549 		}
10550 		bp->rsscos_nr_ctxs++;
10551 	}
10552 
10553 skip_rss_ctx:
10554 	/* configure default vnic, ring grp */
10555 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10556 	if (rc) {
10557 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10558 			   vnic->vnic_id, rc);
10559 		goto vnic_setup_err;
10560 	}
10561 
10562 	/* Enable RSS hashing on vnic */
10563 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10564 	if (rc) {
10565 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10566 			   vnic->vnic_id, rc);
10567 		goto vnic_setup_err;
10568 	}
10569 
10570 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10571 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10572 		if (rc) {
10573 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10574 				   vnic->vnic_id, rc);
10575 		}
10576 	}
10577 
10578 vnic_setup_err:
10579 	return rc;
10580 }
10581 
10582 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10583 			  u8 valid)
10584 {
10585 	struct hwrm_vnic_update_input *req;
10586 	int rc;
10587 
10588 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10589 	if (rc)
10590 		return rc;
10591 
10592 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10593 
10594 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10595 		req->mru = cpu_to_le16(vnic->mru);
10596 
10597 	req->enables = cpu_to_le32(valid);
10598 
10599 	return hwrm_req_send(bp, req);
10600 }
10601 
10602 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10603 {
10604 	int rc;
10605 
10606 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10607 	if (rc) {
10608 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10609 			   vnic->vnic_id, rc);
10610 		return rc;
10611 	}
10612 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10613 	if (rc)
10614 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10615 			   vnic->vnic_id, rc);
10616 	return rc;
10617 }
10618 
10619 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10620 {
10621 	int rc, i, nr_ctxs;
10622 
10623 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10624 	for (i = 0; i < nr_ctxs; i++) {
10625 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10626 		if (rc) {
10627 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10628 				   vnic->vnic_id, i, rc);
10629 			break;
10630 		}
10631 		bp->rsscos_nr_ctxs++;
10632 	}
10633 	if (i < nr_ctxs)
10634 		return -ENOMEM;
10635 
10636 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10637 	if (rc)
10638 		return rc;
10639 
10640 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10641 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10642 		if (rc) {
10643 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10644 				   vnic->vnic_id, rc);
10645 		}
10646 	}
10647 	return rc;
10648 }
10649 
10650 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10651 {
10652 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10653 		return __bnxt_setup_vnic_p5(bp, vnic);
10654 	else
10655 		return __bnxt_setup_vnic(bp, vnic);
10656 }
10657 
10658 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10659 				     struct bnxt_vnic_info *vnic,
10660 				     u16 start_rx_ring_idx, int rx_rings)
10661 {
10662 	int rc;
10663 
10664 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10665 	if (rc) {
10666 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10667 			   vnic->vnic_id, rc);
10668 		return rc;
10669 	}
10670 	return bnxt_setup_vnic(bp, vnic);
10671 }
10672 
10673 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10674 {
10675 	struct bnxt_vnic_info *vnic;
10676 	int i, rc = 0;
10677 
10678 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10679 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10680 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10681 	}
10682 
10683 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10684 		return 0;
10685 
10686 	for (i = 0; i < bp->rx_nr_rings; i++) {
10687 		u16 vnic_id = i + 1;
10688 		u16 ring_id = i;
10689 
10690 		if (vnic_id >= bp->nr_vnics)
10691 			break;
10692 
10693 		vnic = &bp->vnic_info[vnic_id];
10694 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10695 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10696 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10697 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10698 			break;
10699 	}
10700 	return rc;
10701 }
10702 
10703 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10704 			  bool all)
10705 {
10706 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10707 	struct bnxt_filter_base *usr_fltr, *tmp;
10708 	struct bnxt_ntuple_filter *ntp_fltr;
10709 	int i;
10710 
10711 	if (netif_running(bp->dev)) {
10712 		bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10713 		for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10714 			if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10715 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10716 		}
10717 	}
10718 	if (!all)
10719 		return;
10720 
10721 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10722 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10723 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10724 			ntp_fltr = container_of(usr_fltr,
10725 						struct bnxt_ntuple_filter,
10726 						base);
10727 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10728 			bnxt_del_ntp_filter(bp, ntp_fltr);
10729 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10730 		}
10731 	}
10732 
10733 	if (vnic->rss_table)
10734 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10735 				  vnic->rss_table,
10736 				  vnic->rss_table_dma_addr);
10737 	bp->num_rss_ctx--;
10738 }
10739 
10740 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10741 {
10742 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10743 	struct ethtool_rxfh_context *ctx;
10744 	unsigned long context;
10745 
10746 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10747 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10748 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10749 
10750 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10751 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10752 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10753 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10754 				   rss_ctx->index);
10755 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10756 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
10757 		}
10758 	}
10759 }
10760 
10761 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
10762 {
10763 	struct ethtool_rxfh_context *ctx;
10764 	unsigned long context;
10765 
10766 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10767 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10768 
10769 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
10770 	}
10771 }
10772 
10773 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
10774 static bool bnxt_promisc_ok(struct bnxt *bp)
10775 {
10776 #ifdef CONFIG_BNXT_SRIOV
10777 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10778 		return false;
10779 #endif
10780 	return true;
10781 }
10782 
10783 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10784 {
10785 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10786 	unsigned int rc = 0;
10787 
10788 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10789 	if (rc) {
10790 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10791 			   rc);
10792 		return rc;
10793 	}
10794 
10795 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10796 	if (rc) {
10797 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10798 			   rc);
10799 		return rc;
10800 	}
10801 	return rc;
10802 }
10803 
10804 static int bnxt_cfg_rx_mode(struct bnxt *);
10805 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10806 
10807 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10808 {
10809 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10810 	int rc = 0;
10811 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10812 
10813 	if (irq_re_init) {
10814 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10815 		if (rc) {
10816 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10817 				   rc);
10818 			goto err_out;
10819 		}
10820 	}
10821 
10822 	rc = bnxt_hwrm_ring_alloc(bp);
10823 	if (rc) {
10824 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10825 		goto err_out;
10826 	}
10827 
10828 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10829 	if (rc) {
10830 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10831 		goto err_out;
10832 	}
10833 
10834 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10835 		rx_nr_rings--;
10836 
10837 	/* default vnic 0 */
10838 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10839 	if (rc) {
10840 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10841 		goto err_out;
10842 	}
10843 
10844 	if (BNXT_VF(bp))
10845 		bnxt_hwrm_func_qcfg(bp);
10846 
10847 	rc = bnxt_setup_vnic(bp, vnic);
10848 	if (rc)
10849 		goto err_out;
10850 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10851 		bnxt_hwrm_update_rss_hash_cfg(bp);
10852 
10853 	if (bp->flags & BNXT_FLAG_RFS) {
10854 		rc = bnxt_alloc_rfs_vnics(bp);
10855 		if (rc)
10856 			goto err_out;
10857 	}
10858 
10859 	if (bp->flags & BNXT_FLAG_TPA) {
10860 		rc = bnxt_set_tpa(bp, true);
10861 		if (rc)
10862 			goto err_out;
10863 	}
10864 
10865 	if (BNXT_VF(bp))
10866 		bnxt_update_vf_mac(bp);
10867 
10868 	/* Filter for default vnic 0 */
10869 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
10870 	if (rc) {
10871 		if (BNXT_VF(bp) && rc == -ENODEV)
10872 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
10873 		else
10874 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
10875 		goto err_out;
10876 	}
10877 	vnic->uc_filter_count = 1;
10878 
10879 	vnic->rx_mask = 0;
10880 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
10881 		goto skip_rx_mask;
10882 
10883 	if (bp->dev->flags & IFF_BROADCAST)
10884 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10885 
10886 	if (bp->dev->flags & IFF_PROMISC)
10887 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10888 
10889 	if (bp->dev->flags & IFF_ALLMULTI) {
10890 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10891 		vnic->mc_list_count = 0;
10892 	} else if (bp->dev->flags & IFF_MULTICAST) {
10893 		u32 mask = 0;
10894 
10895 		bnxt_mc_list_updated(bp, &mask);
10896 		vnic->rx_mask |= mask;
10897 	}
10898 
10899 	rc = bnxt_cfg_rx_mode(bp);
10900 	if (rc)
10901 		goto err_out;
10902 
10903 skip_rx_mask:
10904 	rc = bnxt_hwrm_set_coal(bp);
10905 	if (rc)
10906 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
10907 				rc);
10908 
10909 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10910 		rc = bnxt_setup_nitroa0_vnic(bp);
10911 		if (rc)
10912 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
10913 				   rc);
10914 	}
10915 
10916 	if (BNXT_VF(bp)) {
10917 		bnxt_hwrm_func_qcfg(bp);
10918 		netdev_update_features(bp->dev);
10919 	}
10920 
10921 	return 0;
10922 
10923 err_out:
10924 	bnxt_hwrm_resource_free(bp, 0, true);
10925 
10926 	return rc;
10927 }
10928 
10929 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
10930 {
10931 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
10932 	return 0;
10933 }
10934 
10935 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
10936 {
10937 	bnxt_init_cp_rings(bp);
10938 	bnxt_init_rx_rings(bp);
10939 	bnxt_init_tx_rings(bp);
10940 	bnxt_init_ring_grps(bp, irq_re_init);
10941 	bnxt_init_vnics(bp);
10942 
10943 	return bnxt_init_chip(bp, irq_re_init);
10944 }
10945 
10946 static int bnxt_set_real_num_queues(struct bnxt *bp)
10947 {
10948 	int rc;
10949 	struct net_device *dev = bp->dev;
10950 
10951 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
10952 					  bp->tx_nr_rings_xdp);
10953 	if (rc)
10954 		return rc;
10955 
10956 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
10957 	if (rc)
10958 		return rc;
10959 
10960 #ifdef CONFIG_RFS_ACCEL
10961 	if (bp->flags & BNXT_FLAG_RFS)
10962 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
10963 #endif
10964 
10965 	return rc;
10966 }
10967 
10968 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10969 			     bool shared)
10970 {
10971 	int _rx = *rx, _tx = *tx;
10972 
10973 	if (shared) {
10974 		*rx = min_t(int, _rx, max);
10975 		*tx = min_t(int, _tx, max);
10976 	} else {
10977 		if (max < 2)
10978 			return -ENOMEM;
10979 
10980 		while (_rx + _tx > max) {
10981 			if (_rx > _tx && _rx > 1)
10982 				_rx--;
10983 			else if (_tx > 1)
10984 				_tx--;
10985 		}
10986 		*rx = _rx;
10987 		*tx = _tx;
10988 	}
10989 	return 0;
10990 }
10991 
10992 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
10993 {
10994 	return (tx - tx_xdp) / tx_sets + tx_xdp;
10995 }
10996 
10997 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
10998 {
10999 	int tcs = bp->num_tc;
11000 
11001 	if (!tcs)
11002 		tcs = 1;
11003 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
11004 }
11005 
11006 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
11007 {
11008 	int tcs = bp->num_tc;
11009 
11010 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
11011 	       bp->tx_nr_rings_xdp;
11012 }
11013 
11014 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11015 			   bool sh)
11016 {
11017 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
11018 
11019 	if (tx_cp != *tx) {
11020 		int tx_saved = tx_cp, rc;
11021 
11022 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
11023 		if (rc)
11024 			return rc;
11025 		if (tx_cp != tx_saved)
11026 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
11027 		return 0;
11028 	}
11029 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
11030 }
11031 
11032 static void bnxt_setup_msix(struct bnxt *bp)
11033 {
11034 	const int len = sizeof(bp->irq_tbl[0].name);
11035 	struct net_device *dev = bp->dev;
11036 	int tcs, i;
11037 
11038 	tcs = bp->num_tc;
11039 	if (tcs) {
11040 		int i, off, count;
11041 
11042 		for (i = 0; i < tcs; i++) {
11043 			count = bp->tx_nr_rings_per_tc;
11044 			off = BNXT_TC_TO_RING_BASE(bp, i);
11045 			netdev_set_tc_queue(dev, i, count, off);
11046 		}
11047 	}
11048 
11049 	for (i = 0; i < bp->cp_nr_rings; i++) {
11050 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11051 		char *attr;
11052 
11053 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11054 			attr = "TxRx";
11055 		else if (i < bp->rx_nr_rings)
11056 			attr = "rx";
11057 		else
11058 			attr = "tx";
11059 
11060 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
11061 			 attr, i);
11062 		bp->irq_tbl[map_idx].handler = bnxt_msix;
11063 	}
11064 }
11065 
11066 static int bnxt_init_int_mode(struct bnxt *bp);
11067 
11068 static int bnxt_change_msix(struct bnxt *bp, int total)
11069 {
11070 	struct msi_map map;
11071 	int i;
11072 
11073 	/* add MSIX to the end if needed */
11074 	for (i = bp->total_irqs; i < total; i++) {
11075 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
11076 		if (map.index < 0)
11077 			return bp->total_irqs;
11078 		bp->irq_tbl[i].vector = map.virq;
11079 		bp->total_irqs++;
11080 	}
11081 
11082 	/* trim MSIX from the end if needed */
11083 	for (i = bp->total_irqs; i > total; i--) {
11084 		map.index = i - 1;
11085 		map.virq = bp->irq_tbl[i - 1].vector;
11086 		pci_msix_free_irq(bp->pdev, map);
11087 		bp->total_irqs--;
11088 	}
11089 	return bp->total_irqs;
11090 }
11091 
11092 static int bnxt_setup_int_mode(struct bnxt *bp)
11093 {
11094 	int rc;
11095 
11096 	if (!bp->irq_tbl) {
11097 		rc = bnxt_init_int_mode(bp);
11098 		if (rc || !bp->irq_tbl)
11099 			return rc ?: -ENODEV;
11100 	}
11101 
11102 	bnxt_setup_msix(bp);
11103 
11104 	rc = bnxt_set_real_num_queues(bp);
11105 	return rc;
11106 }
11107 
11108 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
11109 {
11110 	return bp->hw_resc.max_rsscos_ctxs;
11111 }
11112 
11113 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
11114 {
11115 	return bp->hw_resc.max_vnics;
11116 }
11117 
11118 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11119 {
11120 	return bp->hw_resc.max_stat_ctxs;
11121 }
11122 
11123 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11124 {
11125 	return bp->hw_resc.max_cp_rings;
11126 }
11127 
11128 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11129 {
11130 	unsigned int cp = bp->hw_resc.max_cp_rings;
11131 
11132 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11133 		cp -= bnxt_get_ulp_msix_num(bp);
11134 
11135 	return cp;
11136 }
11137 
11138 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11139 {
11140 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11141 
11142 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11143 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11144 
11145 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11146 }
11147 
11148 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11149 {
11150 	bp->hw_resc.max_irqs = max_irqs;
11151 }
11152 
11153 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11154 {
11155 	unsigned int cp;
11156 
11157 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11158 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11159 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11160 	else
11161 		return cp - bp->cp_nr_rings;
11162 }
11163 
11164 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11165 {
11166 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11167 }
11168 
11169 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11170 {
11171 	int max_irq = bnxt_get_max_func_irqs(bp);
11172 	int total_req = bp->cp_nr_rings + num;
11173 
11174 	if (max_irq < total_req) {
11175 		num = max_irq - bp->cp_nr_rings;
11176 		if (num <= 0)
11177 			return 0;
11178 	}
11179 	return num;
11180 }
11181 
11182 static int bnxt_get_num_msix(struct bnxt *bp)
11183 {
11184 	if (!BNXT_NEW_RM(bp))
11185 		return bnxt_get_max_func_irqs(bp);
11186 
11187 	return bnxt_nq_rings_in_use(bp);
11188 }
11189 
11190 static int bnxt_init_int_mode(struct bnxt *bp)
11191 {
11192 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
11193 
11194 	total_vecs = bnxt_get_num_msix(bp);
11195 	max = bnxt_get_max_func_irqs(bp);
11196 	if (total_vecs > max)
11197 		total_vecs = max;
11198 
11199 	if (!total_vecs)
11200 		return 0;
11201 
11202 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11203 		min = 2;
11204 
11205 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11206 					   PCI_IRQ_MSIX);
11207 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11208 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11209 		rc = -ENODEV;
11210 		goto msix_setup_exit;
11211 	}
11212 
11213 	tbl_size = total_vecs;
11214 	if (pci_msix_can_alloc_dyn(bp->pdev))
11215 		tbl_size = max;
11216 	bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL);
11217 	if (bp->irq_tbl) {
11218 		for (i = 0; i < total_vecs; i++)
11219 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11220 
11221 		bp->total_irqs = total_vecs;
11222 		/* Trim rings based upon num of vectors allocated */
11223 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11224 				     total_vecs - ulp_msix, min == 1);
11225 		if (rc)
11226 			goto msix_setup_exit;
11227 
11228 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11229 		bp->cp_nr_rings = (min == 1) ?
11230 				  max_t(int, tx_cp, bp->rx_nr_rings) :
11231 				  tx_cp + bp->rx_nr_rings;
11232 
11233 	} else {
11234 		rc = -ENOMEM;
11235 		goto msix_setup_exit;
11236 	}
11237 	return 0;
11238 
11239 msix_setup_exit:
11240 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11241 	kfree(bp->irq_tbl);
11242 	bp->irq_tbl = NULL;
11243 	pci_free_irq_vectors(bp->pdev);
11244 	return rc;
11245 }
11246 
11247 static void bnxt_clear_int_mode(struct bnxt *bp)
11248 {
11249 	pci_free_irq_vectors(bp->pdev);
11250 
11251 	kfree(bp->irq_tbl);
11252 	bp->irq_tbl = NULL;
11253 }
11254 
11255 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11256 {
11257 	bool irq_cleared = false;
11258 	bool irq_change = false;
11259 	int tcs = bp->num_tc;
11260 	int irqs_required;
11261 	int rc;
11262 
11263 	if (!bnxt_need_reserve_rings(bp))
11264 		return 0;
11265 
11266 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
11267 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11268 
11269 		if (ulp_msix > bp->ulp_num_msix_want)
11270 			ulp_msix = bp->ulp_num_msix_want;
11271 		irqs_required = ulp_msix + bp->cp_nr_rings;
11272 	} else {
11273 		irqs_required = bnxt_get_num_msix(bp);
11274 	}
11275 
11276 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11277 		irq_change = true;
11278 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11279 			bnxt_ulp_irq_stop(bp);
11280 			bnxt_clear_int_mode(bp);
11281 			irq_cleared = true;
11282 		}
11283 	}
11284 	rc = __bnxt_reserve_rings(bp);
11285 	if (irq_cleared) {
11286 		if (!rc)
11287 			rc = bnxt_init_int_mode(bp);
11288 		bnxt_ulp_irq_restart(bp, rc);
11289 	} else if (irq_change && !rc) {
11290 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11291 			rc = -ENOSPC;
11292 	}
11293 	if (rc) {
11294 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11295 		return rc;
11296 	}
11297 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11298 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11299 		netdev_err(bp->dev, "tx ring reservation failure\n");
11300 		netdev_reset_tc(bp->dev);
11301 		bp->num_tc = 0;
11302 		if (bp->tx_nr_rings_xdp)
11303 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11304 		else
11305 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11306 		return -ENOMEM;
11307 	}
11308 	return 0;
11309 }
11310 
11311 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx)
11312 {
11313 	struct bnxt_tx_ring_info *txr;
11314 	struct netdev_queue *txq;
11315 	struct bnxt_napi *bnapi;
11316 	int i;
11317 
11318 	bnapi = bp->bnapi[idx];
11319 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11320 		WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11321 		synchronize_net();
11322 
11323 		if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) {
11324 			txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11325 			if (txq) {
11326 				__netif_tx_lock_bh(txq);
11327 				netif_tx_stop_queue(txq);
11328 				__netif_tx_unlock_bh(txq);
11329 			}
11330 		}
11331 
11332 		if (!bp->tph_mode)
11333 			continue;
11334 
11335 		bnxt_hwrm_tx_ring_free(bp, txr, true);
11336 		bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr);
11337 		bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index);
11338 		bnxt_clear_one_cp_ring(bp, txr->tx_cpr);
11339 	}
11340 }
11341 
11342 static int bnxt_tx_queue_start(struct bnxt *bp, int idx)
11343 {
11344 	struct bnxt_tx_ring_info *txr;
11345 	struct netdev_queue *txq;
11346 	struct bnxt_napi *bnapi;
11347 	int rc, i;
11348 
11349 	bnapi = bp->bnapi[idx];
11350 	/* All rings have been reserved and previously allocated.
11351 	 * Reallocating with the same parameters should never fail.
11352 	 */
11353 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11354 		if (!bp->tph_mode)
11355 			goto start_tx;
11356 
11357 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
11358 		if (rc)
11359 			return rc;
11360 
11361 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false);
11362 		if (rc)
11363 			return rc;
11364 
11365 		txr->tx_prod = 0;
11366 		txr->tx_cons = 0;
11367 		txr->tx_hw_cons = 0;
11368 start_tx:
11369 		WRITE_ONCE(txr->dev_state, 0);
11370 		synchronize_net();
11371 
11372 		if (bnapi->flags & BNXT_NAPI_FLAG_XDP)
11373 			continue;
11374 
11375 		txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11376 		if (txq)
11377 			netif_tx_start_queue(txq);
11378 	}
11379 
11380 	return 0;
11381 }
11382 
11383 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify,
11384 				     const cpumask_t *mask)
11385 {
11386 	struct bnxt_irq *irq;
11387 	u16 tag;
11388 	int err;
11389 
11390 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11391 
11392 	if (!irq->bp->tph_mode)
11393 		return;
11394 
11395 	cpumask_copy(irq->cpu_mask, mask);
11396 
11397 	if (irq->ring_nr >= irq->bp->rx_nr_rings)
11398 		return;
11399 
11400 	if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11401 				cpumask_first(irq->cpu_mask), &tag))
11402 		return;
11403 
11404 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag))
11405 		return;
11406 
11407 	netdev_lock(irq->bp->dev);
11408 	if (netif_running(irq->bp->dev)) {
11409 		err = netdev_rx_queue_restart(irq->bp->dev, irq->ring_nr);
11410 		if (err)
11411 			netdev_err(irq->bp->dev,
11412 				   "RX queue restart failed: err=%d\n", err);
11413 	}
11414 	netdev_unlock(irq->bp->dev);
11415 }
11416 
11417 static void bnxt_irq_affinity_release(struct kref *ref)
11418 {
11419 	struct irq_affinity_notify *notify =
11420 		container_of(ref, struct irq_affinity_notify, kref);
11421 	struct bnxt_irq *irq;
11422 
11423 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11424 
11425 	if (!irq->bp->tph_mode)
11426 		return;
11427 
11428 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) {
11429 		netdev_err(irq->bp->dev,
11430 			   "Setting ST=0 for MSIX entry %d failed\n",
11431 			   irq->msix_nr);
11432 		return;
11433 	}
11434 }
11435 
11436 static void bnxt_release_irq_notifier(struct bnxt_irq *irq)
11437 {
11438 	irq_set_affinity_notifier(irq->vector, NULL);
11439 }
11440 
11441 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq)
11442 {
11443 	struct irq_affinity_notify *notify;
11444 
11445 	irq->bp = bp;
11446 
11447 	/* Nothing to do if TPH is not enabled */
11448 	if (!bp->tph_mode)
11449 		return;
11450 
11451 	/* Register IRQ affinity notifier */
11452 	notify = &irq->affinity_notify;
11453 	notify->irq = irq->vector;
11454 	notify->notify = bnxt_irq_affinity_notify;
11455 	notify->release = bnxt_irq_affinity_release;
11456 
11457 	irq_set_affinity_notifier(irq->vector, notify);
11458 }
11459 
11460 static void bnxt_free_irq(struct bnxt *bp)
11461 {
11462 	struct bnxt_irq *irq;
11463 	int i;
11464 
11465 #ifdef CONFIG_RFS_ACCEL
11466 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11467 	bp->dev->rx_cpu_rmap = NULL;
11468 #endif
11469 	if (!bp->irq_tbl || !bp->bnapi)
11470 		return;
11471 
11472 	for (i = 0; i < bp->cp_nr_rings; i++) {
11473 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11474 
11475 		irq = &bp->irq_tbl[map_idx];
11476 		if (irq->requested) {
11477 			if (irq->have_cpumask) {
11478 				irq_update_affinity_hint(irq->vector, NULL);
11479 				free_cpumask_var(irq->cpu_mask);
11480 				irq->have_cpumask = 0;
11481 			}
11482 
11483 			bnxt_release_irq_notifier(irq);
11484 
11485 			free_irq(irq->vector, bp->bnapi[i]);
11486 		}
11487 
11488 		irq->requested = 0;
11489 	}
11490 
11491 	/* Disable TPH support */
11492 	pcie_disable_tph(bp->pdev);
11493 	bp->tph_mode = 0;
11494 }
11495 
11496 static int bnxt_request_irq(struct bnxt *bp)
11497 {
11498 	int i, j, rc = 0;
11499 	unsigned long flags = 0;
11500 #ifdef CONFIG_RFS_ACCEL
11501 	struct cpu_rmap *rmap;
11502 #endif
11503 
11504 	rc = bnxt_setup_int_mode(bp);
11505 	if (rc) {
11506 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11507 			   rc);
11508 		return rc;
11509 	}
11510 #ifdef CONFIG_RFS_ACCEL
11511 	rmap = bp->dev->rx_cpu_rmap;
11512 #endif
11513 
11514 	/* Enable TPH support as part of IRQ request */
11515 	rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE);
11516 	if (!rc)
11517 		bp->tph_mode = PCI_TPH_ST_IV_MODE;
11518 
11519 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11520 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11521 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11522 
11523 #ifdef CONFIG_RFS_ACCEL
11524 		if (rmap && bp->bnapi[i]->rx_ring) {
11525 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11526 			if (rc)
11527 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11528 					    j);
11529 			j++;
11530 		}
11531 #endif
11532 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11533 				 bp->bnapi[i]);
11534 		if (rc)
11535 			break;
11536 
11537 		netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector);
11538 		irq->requested = 1;
11539 
11540 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
11541 			int numa_node = dev_to_node(&bp->pdev->dev);
11542 			u16 tag;
11543 
11544 			irq->have_cpumask = 1;
11545 			irq->msix_nr = map_idx;
11546 			irq->ring_nr = i;
11547 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
11548 					irq->cpu_mask);
11549 			rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
11550 			if (rc) {
11551 				netdev_warn(bp->dev,
11552 					    "Update affinity hint failed, IRQ = %d\n",
11553 					    irq->vector);
11554 				break;
11555 			}
11556 
11557 			bnxt_register_irq_notifier(bp, irq);
11558 
11559 			/* Init ST table entry */
11560 			if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11561 						cpumask_first(irq->cpu_mask),
11562 						&tag))
11563 				continue;
11564 
11565 			pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag);
11566 		}
11567 	}
11568 	return rc;
11569 }
11570 
11571 static void bnxt_del_napi(struct bnxt *bp)
11572 {
11573 	int i;
11574 
11575 	if (!bp->bnapi)
11576 		return;
11577 
11578 	for (i = 0; i < bp->rx_nr_rings; i++)
11579 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
11580 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
11581 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
11582 
11583 	for (i = 0; i < bp->cp_nr_rings; i++) {
11584 		struct bnxt_napi *bnapi = bp->bnapi[i];
11585 
11586 		__netif_napi_del_locked(&bnapi->napi);
11587 	}
11588 	/* We called __netif_napi_del_locked(), we need
11589 	 * to respect an RCU grace period before freeing napi structures.
11590 	 */
11591 	synchronize_net();
11592 }
11593 
11594 static void bnxt_init_napi(struct bnxt *bp)
11595 {
11596 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
11597 	unsigned int cp_nr_rings = bp->cp_nr_rings;
11598 	struct bnxt_napi *bnapi;
11599 	int i;
11600 
11601 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11602 		poll_fn = bnxt_poll_p5;
11603 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11604 		cp_nr_rings--;
11605 	for (i = 0; i < cp_nr_rings; i++) {
11606 		bnapi = bp->bnapi[i];
11607 		netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn,
11608 					     bnapi->index);
11609 	}
11610 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11611 		bnapi = bp->bnapi[cp_nr_rings];
11612 		netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
11613 	}
11614 }
11615 
11616 static void bnxt_disable_napi(struct bnxt *bp)
11617 {
11618 	int i;
11619 
11620 	if (!bp->bnapi ||
11621 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11622 		return;
11623 
11624 	for (i = 0; i < bp->cp_nr_rings; i++) {
11625 		struct bnxt_napi *bnapi = bp->bnapi[i];
11626 		struct bnxt_cp_ring_info *cpr;
11627 
11628 		cpr = &bnapi->cp_ring;
11629 		if (bnapi->tx_fault)
11630 			cpr->sw_stats->tx.tx_resets++;
11631 		if (bnapi->in_reset)
11632 			cpr->sw_stats->rx.rx_resets++;
11633 		napi_disable_locked(&bnapi->napi);
11634 	}
11635 }
11636 
11637 static void bnxt_enable_napi(struct bnxt *bp)
11638 {
11639 	int i;
11640 
11641 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11642 	for (i = 0; i < bp->cp_nr_rings; i++) {
11643 		struct bnxt_napi *bnapi = bp->bnapi[i];
11644 		struct bnxt_cp_ring_info *cpr;
11645 
11646 		bnapi->tx_fault = 0;
11647 
11648 		cpr = &bnapi->cp_ring;
11649 		bnapi->in_reset = false;
11650 
11651 		if (bnapi->rx_ring) {
11652 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
11653 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
11654 		}
11655 		napi_enable_locked(&bnapi->napi);
11656 	}
11657 }
11658 
11659 void bnxt_tx_disable(struct bnxt *bp)
11660 {
11661 	int i;
11662 	struct bnxt_tx_ring_info *txr;
11663 
11664 	if (bp->tx_ring) {
11665 		for (i = 0; i < bp->tx_nr_rings; i++) {
11666 			txr = &bp->tx_ring[i];
11667 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11668 		}
11669 	}
11670 	/* Make sure napi polls see @dev_state change */
11671 	synchronize_net();
11672 	/* Drop carrier first to prevent TX timeout */
11673 	netif_carrier_off(bp->dev);
11674 	/* Stop all TX queues */
11675 	netif_tx_disable(bp->dev);
11676 }
11677 
11678 void bnxt_tx_enable(struct bnxt *bp)
11679 {
11680 	int i;
11681 	struct bnxt_tx_ring_info *txr;
11682 
11683 	for (i = 0; i < bp->tx_nr_rings; i++) {
11684 		txr = &bp->tx_ring[i];
11685 		WRITE_ONCE(txr->dev_state, 0);
11686 	}
11687 	/* Make sure napi polls see @dev_state change */
11688 	synchronize_net();
11689 	netif_tx_wake_all_queues(bp->dev);
11690 	if (BNXT_LINK_IS_UP(bp))
11691 		netif_carrier_on(bp->dev);
11692 }
11693 
11694 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
11695 {
11696 	u8 active_fec = link_info->active_fec_sig_mode &
11697 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
11698 
11699 	switch (active_fec) {
11700 	default:
11701 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
11702 		return "None";
11703 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
11704 		return "Clause 74 BaseR";
11705 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
11706 		return "Clause 91 RS(528,514)";
11707 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
11708 		return "Clause 91 RS544_1XN";
11709 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
11710 		return "Clause 91 RS(544,514)";
11711 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
11712 		return "Clause 91 RS272_1XN";
11713 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
11714 		return "Clause 91 RS(272,257)";
11715 	}
11716 }
11717 
11718 void bnxt_report_link(struct bnxt *bp)
11719 {
11720 	if (BNXT_LINK_IS_UP(bp)) {
11721 		const char *signal = "";
11722 		const char *flow_ctrl;
11723 		const char *duplex;
11724 		u32 speed;
11725 		u16 fec;
11726 
11727 		netif_carrier_on(bp->dev);
11728 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
11729 		if (speed == SPEED_UNKNOWN) {
11730 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
11731 			return;
11732 		}
11733 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
11734 			duplex = "full";
11735 		else
11736 			duplex = "half";
11737 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
11738 			flow_ctrl = "ON - receive & transmit";
11739 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
11740 			flow_ctrl = "ON - transmit";
11741 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
11742 			flow_ctrl = "ON - receive";
11743 		else
11744 			flow_ctrl = "none";
11745 		if (bp->link_info.phy_qcfg_resp.option_flags &
11746 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
11747 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
11748 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
11749 			switch (sig_mode) {
11750 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
11751 				signal = "(NRZ) ";
11752 				break;
11753 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
11754 				signal = "(PAM4 56Gbps) ";
11755 				break;
11756 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
11757 				signal = "(PAM4 112Gbps) ";
11758 				break;
11759 			default:
11760 				break;
11761 			}
11762 		}
11763 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
11764 			    speed, signal, duplex, flow_ctrl);
11765 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
11766 			netdev_info(bp->dev, "EEE is %s\n",
11767 				    bp->eee.eee_active ? "active" :
11768 							 "not active");
11769 		fec = bp->link_info.fec_cfg;
11770 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
11771 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
11772 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
11773 				    bnxt_report_fec(&bp->link_info));
11774 	} else {
11775 		netif_carrier_off(bp->dev);
11776 		netdev_err(bp->dev, "NIC Link is Down\n");
11777 	}
11778 }
11779 
11780 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
11781 {
11782 	if (!resp->supported_speeds_auto_mode &&
11783 	    !resp->supported_speeds_force_mode &&
11784 	    !resp->supported_pam4_speeds_auto_mode &&
11785 	    !resp->supported_pam4_speeds_force_mode &&
11786 	    !resp->supported_speeds2_auto_mode &&
11787 	    !resp->supported_speeds2_force_mode)
11788 		return true;
11789 	return false;
11790 }
11791 
11792 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
11793 {
11794 	struct bnxt_link_info *link_info = &bp->link_info;
11795 	struct hwrm_port_phy_qcaps_output *resp;
11796 	struct hwrm_port_phy_qcaps_input *req;
11797 	int rc = 0;
11798 
11799 	if (bp->hwrm_spec_code < 0x10201)
11800 		return 0;
11801 
11802 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
11803 	if (rc)
11804 		return rc;
11805 
11806 	resp = hwrm_req_hold(bp, req);
11807 	rc = hwrm_req_send(bp, req);
11808 	if (rc)
11809 		goto hwrm_phy_qcaps_exit;
11810 
11811 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
11812 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
11813 		struct ethtool_keee *eee = &bp->eee;
11814 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
11815 
11816 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
11817 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
11818 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11819 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11820 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11821 	}
11822 
11823 	if (bp->hwrm_spec_code >= 0x10a01) {
11824 		if (bnxt_phy_qcaps_no_speed(resp)) {
11825 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11826 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11827 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11828 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11829 			netdev_info(bp->dev, "Ethernet link enabled\n");
11830 			/* Phy re-enabled, reprobe the speeds */
11831 			link_info->support_auto_speeds = 0;
11832 			link_info->support_pam4_auto_speeds = 0;
11833 			link_info->support_auto_speeds2 = 0;
11834 		}
11835 	}
11836 	if (resp->supported_speeds_auto_mode)
11837 		link_info->support_auto_speeds =
11838 			le16_to_cpu(resp->supported_speeds_auto_mode);
11839 	if (resp->supported_pam4_speeds_auto_mode)
11840 		link_info->support_pam4_auto_speeds =
11841 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11842 	if (resp->supported_speeds2_auto_mode)
11843 		link_info->support_auto_speeds2 =
11844 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11845 
11846 	bp->port_count = resp->port_cnt;
11847 
11848 hwrm_phy_qcaps_exit:
11849 	hwrm_req_drop(bp, req);
11850 	return rc;
11851 }
11852 
11853 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
11854 {
11855 	struct hwrm_port_mac_qcaps_output *resp;
11856 	struct hwrm_port_mac_qcaps_input *req;
11857 	int rc;
11858 
11859 	if (bp->hwrm_spec_code < 0x10a03)
11860 		return;
11861 
11862 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
11863 	if (rc)
11864 		return;
11865 
11866 	resp = hwrm_req_hold(bp, req);
11867 	rc = hwrm_req_send_silent(bp, req);
11868 	if (!rc)
11869 		bp->mac_flags = resp->flags;
11870 	hwrm_req_drop(bp, req);
11871 }
11872 
11873 static bool bnxt_support_dropped(u16 advertising, u16 supported)
11874 {
11875 	u16 diff = advertising ^ supported;
11876 
11877 	return ((supported | diff) != supported);
11878 }
11879 
11880 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
11881 {
11882 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
11883 
11884 	/* Check if any advertised speeds are no longer supported. The caller
11885 	 * holds the link_lock mutex, so we can modify link_info settings.
11886 	 */
11887 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11888 		if (bnxt_support_dropped(link_info->advertising,
11889 					 link_info->support_auto_speeds2)) {
11890 			link_info->advertising = link_info->support_auto_speeds2;
11891 			return true;
11892 		}
11893 		return false;
11894 	}
11895 	if (bnxt_support_dropped(link_info->advertising,
11896 				 link_info->support_auto_speeds)) {
11897 		link_info->advertising = link_info->support_auto_speeds;
11898 		return true;
11899 	}
11900 	if (bnxt_support_dropped(link_info->advertising_pam4,
11901 				 link_info->support_pam4_auto_speeds)) {
11902 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
11903 		return true;
11904 	}
11905 	return false;
11906 }
11907 
11908 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
11909 {
11910 	struct bnxt_link_info *link_info = &bp->link_info;
11911 	struct hwrm_port_phy_qcfg_output *resp;
11912 	struct hwrm_port_phy_qcfg_input *req;
11913 	u8 link_state = link_info->link_state;
11914 	bool support_changed;
11915 	int rc;
11916 
11917 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
11918 	if (rc)
11919 		return rc;
11920 
11921 	resp = hwrm_req_hold(bp, req);
11922 	rc = hwrm_req_send(bp, req);
11923 	if (rc) {
11924 		hwrm_req_drop(bp, req);
11925 		if (BNXT_VF(bp) && rc == -ENODEV) {
11926 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
11927 			rc = 0;
11928 		}
11929 		return rc;
11930 	}
11931 
11932 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
11933 	link_info->phy_link_status = resp->link;
11934 	link_info->duplex = resp->duplex_cfg;
11935 	if (bp->hwrm_spec_code >= 0x10800)
11936 		link_info->duplex = resp->duplex_state;
11937 	link_info->pause = resp->pause;
11938 	link_info->auto_mode = resp->auto_mode;
11939 	link_info->auto_pause_setting = resp->auto_pause;
11940 	link_info->lp_pause = resp->link_partner_adv_pause;
11941 	link_info->force_pause_setting = resp->force_pause;
11942 	link_info->duplex_setting = resp->duplex_cfg;
11943 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
11944 		link_info->link_speed = le16_to_cpu(resp->link_speed);
11945 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
11946 			link_info->active_lanes = resp->active_lanes;
11947 	} else {
11948 		link_info->link_speed = 0;
11949 		link_info->active_lanes = 0;
11950 	}
11951 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
11952 	link_info->force_pam4_link_speed =
11953 		le16_to_cpu(resp->force_pam4_link_speed);
11954 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
11955 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
11956 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
11957 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
11958 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
11959 	link_info->auto_pam4_link_speeds =
11960 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
11961 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
11962 	link_info->lp_auto_link_speeds =
11963 		le16_to_cpu(resp->link_partner_adv_speeds);
11964 	link_info->lp_auto_pam4_link_speeds =
11965 		resp->link_partner_pam4_adv_speeds;
11966 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
11967 	link_info->phy_ver[0] = resp->phy_maj;
11968 	link_info->phy_ver[1] = resp->phy_min;
11969 	link_info->phy_ver[2] = resp->phy_bld;
11970 	link_info->media_type = resp->media_type;
11971 	link_info->phy_type = resp->phy_type;
11972 	link_info->transceiver = resp->xcvr_pkg_type;
11973 	link_info->phy_addr = resp->eee_config_phy_addr &
11974 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
11975 	link_info->module_status = resp->module_status;
11976 
11977 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
11978 		struct ethtool_keee *eee = &bp->eee;
11979 		u16 fw_speeds;
11980 
11981 		eee->eee_active = 0;
11982 		if (resp->eee_config_phy_addr &
11983 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
11984 			eee->eee_active = 1;
11985 			fw_speeds = le16_to_cpu(
11986 				resp->link_partner_adv_eee_link_speed_mask);
11987 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
11988 		}
11989 
11990 		/* Pull initial EEE config */
11991 		if (!chng_link_state) {
11992 			if (resp->eee_config_phy_addr &
11993 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
11994 				eee->eee_enabled = 1;
11995 
11996 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
11997 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
11998 
11999 			if (resp->eee_config_phy_addr &
12000 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
12001 				__le32 tmr;
12002 
12003 				eee->tx_lpi_enabled = 1;
12004 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
12005 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
12006 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
12007 			}
12008 		}
12009 	}
12010 
12011 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
12012 	if (bp->hwrm_spec_code >= 0x10504) {
12013 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
12014 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
12015 	}
12016 	/* TODO: need to add more logic to report VF link */
12017 	if (chng_link_state) {
12018 		if (link_info->phy_link_status == BNXT_LINK_LINK)
12019 			link_info->link_state = BNXT_LINK_STATE_UP;
12020 		else
12021 			link_info->link_state = BNXT_LINK_STATE_DOWN;
12022 		if (link_state != link_info->link_state)
12023 			bnxt_report_link(bp);
12024 	} else {
12025 		/* always link down if not require to update link state */
12026 		link_info->link_state = BNXT_LINK_STATE_DOWN;
12027 	}
12028 	hwrm_req_drop(bp, req);
12029 
12030 	if (!BNXT_PHY_CFG_ABLE(bp))
12031 		return 0;
12032 
12033 	support_changed = bnxt_support_speed_dropped(link_info);
12034 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
12035 		bnxt_hwrm_set_link_setting(bp, true, false);
12036 	return 0;
12037 }
12038 
12039 static void bnxt_get_port_module_status(struct bnxt *bp)
12040 {
12041 	struct bnxt_link_info *link_info = &bp->link_info;
12042 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
12043 	u8 module_status;
12044 
12045 	if (bnxt_update_link(bp, true))
12046 		return;
12047 
12048 	module_status = link_info->module_status;
12049 	switch (module_status) {
12050 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
12051 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
12052 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
12053 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
12054 			    bp->pf.port_id);
12055 		if (bp->hwrm_spec_code >= 0x10201) {
12056 			netdev_warn(bp->dev, "Module part number %s\n",
12057 				    resp->phy_vendor_partnumber);
12058 		}
12059 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
12060 			netdev_warn(bp->dev, "TX is disabled\n");
12061 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
12062 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
12063 	}
12064 }
12065 
12066 static void
12067 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12068 {
12069 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
12070 		if (bp->hwrm_spec_code >= 0x10201)
12071 			req->auto_pause =
12072 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
12073 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12074 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
12075 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12076 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
12077 		req->enables |=
12078 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12079 	} else {
12080 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12081 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
12082 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12083 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
12084 		req->enables |=
12085 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
12086 		if (bp->hwrm_spec_code >= 0x10201) {
12087 			req->auto_pause = req->force_pause;
12088 			req->enables |= cpu_to_le32(
12089 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12090 		}
12091 	}
12092 }
12093 
12094 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12095 {
12096 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
12097 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
12098 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12099 			req->enables |=
12100 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
12101 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
12102 		} else if (bp->link_info.advertising) {
12103 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
12104 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
12105 		}
12106 		if (bp->link_info.advertising_pam4) {
12107 			req->enables |=
12108 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
12109 			req->auto_link_pam4_speed_mask =
12110 				cpu_to_le16(bp->link_info.advertising_pam4);
12111 		}
12112 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
12113 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
12114 	} else {
12115 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
12116 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12117 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
12118 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
12119 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
12120 				   (u32)bp->link_info.req_link_speed);
12121 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
12122 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12123 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
12124 		} else {
12125 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12126 		}
12127 	}
12128 
12129 	/* tell chimp that the setting takes effect immediately */
12130 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
12131 }
12132 
12133 int bnxt_hwrm_set_pause(struct bnxt *bp)
12134 {
12135 	struct hwrm_port_phy_cfg_input *req;
12136 	int rc;
12137 
12138 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12139 	if (rc)
12140 		return rc;
12141 
12142 	bnxt_hwrm_set_pause_common(bp, req);
12143 
12144 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
12145 	    bp->link_info.force_link_chng)
12146 		bnxt_hwrm_set_link_common(bp, req);
12147 
12148 	rc = hwrm_req_send(bp, req);
12149 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
12150 		/* since changing of pause setting doesn't trigger any link
12151 		 * change event, the driver needs to update the current pause
12152 		 * result upon successfully return of the phy_cfg command
12153 		 */
12154 		bp->link_info.pause =
12155 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
12156 		bp->link_info.auto_pause_setting = 0;
12157 		if (!bp->link_info.force_link_chng)
12158 			bnxt_report_link(bp);
12159 	}
12160 	bp->link_info.force_link_chng = false;
12161 	return rc;
12162 }
12163 
12164 static void bnxt_hwrm_set_eee(struct bnxt *bp,
12165 			      struct hwrm_port_phy_cfg_input *req)
12166 {
12167 	struct ethtool_keee *eee = &bp->eee;
12168 
12169 	if (eee->eee_enabled) {
12170 		u16 eee_speeds;
12171 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
12172 
12173 		if (eee->tx_lpi_enabled)
12174 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
12175 		else
12176 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
12177 
12178 		req->flags |= cpu_to_le32(flags);
12179 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
12180 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
12181 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
12182 	} else {
12183 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
12184 	}
12185 }
12186 
12187 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
12188 {
12189 	struct hwrm_port_phy_cfg_input *req;
12190 	int rc;
12191 
12192 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12193 	if (rc)
12194 		return rc;
12195 
12196 	if (set_pause)
12197 		bnxt_hwrm_set_pause_common(bp, req);
12198 
12199 	bnxt_hwrm_set_link_common(bp, req);
12200 
12201 	if (set_eee)
12202 		bnxt_hwrm_set_eee(bp, req);
12203 	return hwrm_req_send(bp, req);
12204 }
12205 
12206 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
12207 {
12208 	struct hwrm_port_phy_cfg_input *req;
12209 	int rc;
12210 
12211 	if (!BNXT_SINGLE_PF(bp))
12212 		return 0;
12213 
12214 	if (pci_num_vf(bp->pdev) &&
12215 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
12216 		return 0;
12217 
12218 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12219 	if (rc)
12220 		return rc;
12221 
12222 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
12223 	rc = hwrm_req_send(bp, req);
12224 	if (!rc) {
12225 		mutex_lock(&bp->link_lock);
12226 		/* Device is not obliged link down in certain scenarios, even
12227 		 * when forced. Setting the state unknown is consistent with
12228 		 * driver startup and will force link state to be reported
12229 		 * during subsequent open based on PORT_PHY_QCFG.
12230 		 */
12231 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
12232 		mutex_unlock(&bp->link_lock);
12233 	}
12234 	return rc;
12235 }
12236 
12237 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
12238 {
12239 #ifdef CONFIG_TEE_BNXT_FW
12240 	int rc = tee_bnxt_fw_load();
12241 
12242 	if (rc)
12243 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
12244 
12245 	return rc;
12246 #else
12247 	netdev_err(bp->dev, "OP-TEE not supported\n");
12248 	return -ENODEV;
12249 #endif
12250 }
12251 
12252 static int bnxt_try_recover_fw(struct bnxt *bp)
12253 {
12254 	if (bp->fw_health && bp->fw_health->status_reliable) {
12255 		int retry = 0, rc;
12256 		u32 sts;
12257 
12258 		do {
12259 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12260 			rc = bnxt_hwrm_poll(bp);
12261 			if (!BNXT_FW_IS_BOOTING(sts) &&
12262 			    !BNXT_FW_IS_RECOVERING(sts))
12263 				break;
12264 			retry++;
12265 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
12266 
12267 		if (!BNXT_FW_IS_HEALTHY(sts)) {
12268 			netdev_err(bp->dev,
12269 				   "Firmware not responding, status: 0x%x\n",
12270 				   sts);
12271 			rc = -ENODEV;
12272 		}
12273 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
12274 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
12275 			return bnxt_fw_reset_via_optee(bp);
12276 		}
12277 		return rc;
12278 	}
12279 
12280 	return -ENODEV;
12281 }
12282 
12283 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
12284 {
12285 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12286 
12287 	if (!BNXT_NEW_RM(bp))
12288 		return; /* no resource reservations required */
12289 
12290 	hw_resc->resv_cp_rings = 0;
12291 	hw_resc->resv_stat_ctxs = 0;
12292 	hw_resc->resv_irqs = 0;
12293 	hw_resc->resv_tx_rings = 0;
12294 	hw_resc->resv_rx_rings = 0;
12295 	hw_resc->resv_hw_ring_grps = 0;
12296 	hw_resc->resv_vnics = 0;
12297 	hw_resc->resv_rsscos_ctxs = 0;
12298 	if (!fw_reset) {
12299 		bp->tx_nr_rings = 0;
12300 		bp->rx_nr_rings = 0;
12301 	}
12302 }
12303 
12304 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12305 {
12306 	int rc;
12307 
12308 	if (!BNXT_NEW_RM(bp))
12309 		return 0; /* no resource reservations required */
12310 
12311 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12312 	if (rc)
12313 		netdev_err(bp->dev, "resc_qcaps failed\n");
12314 
12315 	bnxt_clear_reservations(bp, fw_reset);
12316 
12317 	return rc;
12318 }
12319 
12320 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12321 {
12322 	struct hwrm_func_drv_if_change_output *resp;
12323 	struct hwrm_func_drv_if_change_input *req;
12324 	bool fw_reset = !bp->irq_tbl;
12325 	bool resc_reinit = false;
12326 	bool caps_change = false;
12327 	int rc, retry = 0;
12328 	u32 flags = 0;
12329 
12330 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12331 		return 0;
12332 
12333 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12334 	if (rc)
12335 		return rc;
12336 
12337 	if (up)
12338 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12339 	resp = hwrm_req_hold(bp, req);
12340 
12341 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12342 	while (retry < BNXT_FW_IF_RETRY) {
12343 		rc = hwrm_req_send(bp, req);
12344 		if (rc != -EAGAIN)
12345 			break;
12346 
12347 		msleep(50);
12348 		retry++;
12349 	}
12350 
12351 	if (rc == -EAGAIN) {
12352 		hwrm_req_drop(bp, req);
12353 		return rc;
12354 	} else if (!rc) {
12355 		flags = le32_to_cpu(resp->flags);
12356 	} else if (up) {
12357 		rc = bnxt_try_recover_fw(bp);
12358 		fw_reset = true;
12359 	}
12360 	hwrm_req_drop(bp, req);
12361 	if (rc)
12362 		return rc;
12363 
12364 	if (!up) {
12365 		bnxt_inv_fw_health_reg(bp);
12366 		return 0;
12367 	}
12368 
12369 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12370 		resc_reinit = true;
12371 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12372 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12373 		fw_reset = true;
12374 	else
12375 		bnxt_remap_fw_health_regs(bp);
12376 
12377 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12378 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12379 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12380 		return -ENODEV;
12381 	}
12382 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE)
12383 		caps_change = true;
12384 
12385 	if (resc_reinit || fw_reset || caps_change) {
12386 		if (fw_reset || caps_change) {
12387 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12388 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12389 				bnxt_ulp_irq_stop(bp);
12390 			bnxt_free_ctx_mem(bp, false);
12391 			bnxt_dcb_free(bp);
12392 			rc = bnxt_fw_init_one(bp);
12393 			if (rc) {
12394 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12395 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12396 				return rc;
12397 			}
12398 			bnxt_clear_int_mode(bp);
12399 			rc = bnxt_init_int_mode(bp);
12400 			if (rc) {
12401 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12402 				netdev_err(bp->dev, "init int mode failed\n");
12403 				return rc;
12404 			}
12405 		}
12406 		rc = bnxt_cancel_reservations(bp, fw_reset);
12407 	}
12408 	return rc;
12409 }
12410 
12411 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12412 {
12413 	struct hwrm_port_led_qcaps_output *resp;
12414 	struct hwrm_port_led_qcaps_input *req;
12415 	struct bnxt_pf_info *pf = &bp->pf;
12416 	int rc;
12417 
12418 	bp->num_leds = 0;
12419 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12420 		return 0;
12421 
12422 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12423 	if (rc)
12424 		return rc;
12425 
12426 	req->port_id = cpu_to_le16(pf->port_id);
12427 	resp = hwrm_req_hold(bp, req);
12428 	rc = hwrm_req_send(bp, req);
12429 	if (rc) {
12430 		hwrm_req_drop(bp, req);
12431 		return rc;
12432 	}
12433 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12434 		int i;
12435 
12436 		bp->num_leds = resp->num_leds;
12437 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12438 						 bp->num_leds);
12439 		for (i = 0; i < bp->num_leds; i++) {
12440 			struct bnxt_led_info *led = &bp->leds[i];
12441 			__le16 caps = led->led_state_caps;
12442 
12443 			if (!led->led_group_id ||
12444 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12445 				bp->num_leds = 0;
12446 				break;
12447 			}
12448 		}
12449 	}
12450 	hwrm_req_drop(bp, req);
12451 	return 0;
12452 }
12453 
12454 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12455 {
12456 	struct hwrm_wol_filter_alloc_output *resp;
12457 	struct hwrm_wol_filter_alloc_input *req;
12458 	int rc;
12459 
12460 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12461 	if (rc)
12462 		return rc;
12463 
12464 	req->port_id = cpu_to_le16(bp->pf.port_id);
12465 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12466 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12467 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12468 
12469 	resp = hwrm_req_hold(bp, req);
12470 	rc = hwrm_req_send(bp, req);
12471 	if (!rc)
12472 		bp->wol_filter_id = resp->wol_filter_id;
12473 	hwrm_req_drop(bp, req);
12474 	return rc;
12475 }
12476 
12477 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12478 {
12479 	struct hwrm_wol_filter_free_input *req;
12480 	int rc;
12481 
12482 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12483 	if (rc)
12484 		return rc;
12485 
12486 	req->port_id = cpu_to_le16(bp->pf.port_id);
12487 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12488 	req->wol_filter_id = bp->wol_filter_id;
12489 
12490 	return hwrm_req_send(bp, req);
12491 }
12492 
12493 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12494 {
12495 	struct hwrm_wol_filter_qcfg_output *resp;
12496 	struct hwrm_wol_filter_qcfg_input *req;
12497 	u16 next_handle = 0;
12498 	int rc;
12499 
12500 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12501 	if (rc)
12502 		return rc;
12503 
12504 	req->port_id = cpu_to_le16(bp->pf.port_id);
12505 	req->handle = cpu_to_le16(handle);
12506 	resp = hwrm_req_hold(bp, req);
12507 	rc = hwrm_req_send(bp, req);
12508 	if (!rc) {
12509 		next_handle = le16_to_cpu(resp->next_handle);
12510 		if (next_handle != 0) {
12511 			if (resp->wol_type ==
12512 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12513 				bp->wol = 1;
12514 				bp->wol_filter_id = resp->wol_filter_id;
12515 			}
12516 		}
12517 	}
12518 	hwrm_req_drop(bp, req);
12519 	return next_handle;
12520 }
12521 
12522 static void bnxt_get_wol_settings(struct bnxt *bp)
12523 {
12524 	u16 handle = 0;
12525 
12526 	bp->wol = 0;
12527 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
12528 		return;
12529 
12530 	do {
12531 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
12532 	} while (handle && handle != 0xffff);
12533 }
12534 
12535 static bool bnxt_eee_config_ok(struct bnxt *bp)
12536 {
12537 	struct ethtool_keee *eee = &bp->eee;
12538 	struct bnxt_link_info *link_info = &bp->link_info;
12539 
12540 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
12541 		return true;
12542 
12543 	if (eee->eee_enabled) {
12544 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
12545 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
12546 
12547 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
12548 
12549 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12550 			eee->eee_enabled = 0;
12551 			return false;
12552 		}
12553 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
12554 			linkmode_and(eee->advertised, advertising,
12555 				     eee->supported);
12556 			return false;
12557 		}
12558 	}
12559 	return true;
12560 }
12561 
12562 static int bnxt_update_phy_setting(struct bnxt *bp)
12563 {
12564 	int rc;
12565 	bool update_link = false;
12566 	bool update_pause = false;
12567 	bool update_eee = false;
12568 	struct bnxt_link_info *link_info = &bp->link_info;
12569 
12570 	rc = bnxt_update_link(bp, true);
12571 	if (rc) {
12572 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
12573 			   rc);
12574 		return rc;
12575 	}
12576 	if (!BNXT_SINGLE_PF(bp))
12577 		return 0;
12578 
12579 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12580 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
12581 	    link_info->req_flow_ctrl)
12582 		update_pause = true;
12583 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12584 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
12585 		update_pause = true;
12586 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12587 		if (BNXT_AUTO_MODE(link_info->auto_mode))
12588 			update_link = true;
12589 		if (bnxt_force_speed_updated(link_info))
12590 			update_link = true;
12591 		if (link_info->req_duplex != link_info->duplex_setting)
12592 			update_link = true;
12593 	} else {
12594 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
12595 			update_link = true;
12596 		if (bnxt_auto_speed_updated(link_info))
12597 			update_link = true;
12598 	}
12599 
12600 	/* The last close may have shutdown the link, so need to call
12601 	 * PHY_CFG to bring it back up.
12602 	 */
12603 	if (!BNXT_LINK_IS_UP(bp))
12604 		update_link = true;
12605 
12606 	if (!bnxt_eee_config_ok(bp))
12607 		update_eee = true;
12608 
12609 	if (update_link)
12610 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
12611 	else if (update_pause)
12612 		rc = bnxt_hwrm_set_pause(bp);
12613 	if (rc) {
12614 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
12615 			   rc);
12616 		return rc;
12617 	}
12618 
12619 	return rc;
12620 }
12621 
12622 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
12623 
12624 static int bnxt_reinit_after_abort(struct bnxt *bp)
12625 {
12626 	int rc;
12627 
12628 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12629 		return -EBUSY;
12630 
12631 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
12632 		return -ENODEV;
12633 
12634 	rc = bnxt_fw_init_one(bp);
12635 	if (!rc) {
12636 		bnxt_clear_int_mode(bp);
12637 		rc = bnxt_init_int_mode(bp);
12638 		if (!rc) {
12639 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12640 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12641 		}
12642 	}
12643 	return rc;
12644 }
12645 
12646 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
12647 {
12648 	struct bnxt_ntuple_filter *ntp_fltr;
12649 	struct bnxt_l2_filter *l2_fltr;
12650 
12651 	if (list_empty(&fltr->list))
12652 		return;
12653 
12654 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
12655 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
12656 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
12657 		atomic_inc(&l2_fltr->refcnt);
12658 		ntp_fltr->l2_fltr = l2_fltr;
12659 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
12660 			bnxt_del_ntp_filter(bp, ntp_fltr);
12661 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
12662 				   fltr->sw_id);
12663 		}
12664 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
12665 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
12666 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
12667 			bnxt_del_l2_filter(bp, l2_fltr);
12668 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
12669 				   fltr->sw_id);
12670 		}
12671 	}
12672 }
12673 
12674 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
12675 {
12676 	struct bnxt_filter_base *usr_fltr, *tmp;
12677 
12678 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
12679 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
12680 }
12681 
12682 static int bnxt_set_xps_mapping(struct bnxt *bp)
12683 {
12684 	int numa_node = dev_to_node(&bp->pdev->dev);
12685 	unsigned int q_idx, map_idx, cpu, i;
12686 	const struct cpumask *cpu_mask_ptr;
12687 	int nr_cpus = num_online_cpus();
12688 	cpumask_t *q_map;
12689 	int rc = 0;
12690 
12691 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
12692 	if (!q_map)
12693 		return -ENOMEM;
12694 
12695 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
12696 	 * Each TC has the same number of TX queues. The nth TX queue for each
12697 	 * TC will have the same CPU mask.
12698 	 */
12699 	for (i = 0; i < nr_cpus; i++) {
12700 		map_idx = i % bp->tx_nr_rings_per_tc;
12701 		cpu = cpumask_local_spread(i, numa_node);
12702 		cpu_mask_ptr = get_cpu_mask(cpu);
12703 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
12704 	}
12705 
12706 	/* Register CPU mask for each TX queue except the ones marked for XDP */
12707 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
12708 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
12709 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
12710 		if (rc) {
12711 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
12712 				    q_idx);
12713 			break;
12714 		}
12715 	}
12716 
12717 	kfree(q_map);
12718 
12719 	return rc;
12720 }
12721 
12722 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12723 {
12724 	int rc = 0;
12725 
12726 	netif_carrier_off(bp->dev);
12727 	if (irq_re_init) {
12728 		/* Reserve rings now if none were reserved at driver probe. */
12729 		rc = bnxt_init_dflt_ring_mode(bp);
12730 		if (rc) {
12731 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
12732 			return rc;
12733 		}
12734 	}
12735 	rc = bnxt_reserve_rings(bp, irq_re_init);
12736 	if (rc)
12737 		return rc;
12738 
12739 	rc = bnxt_alloc_mem(bp, irq_re_init);
12740 	if (rc) {
12741 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12742 		goto open_err_free_mem;
12743 	}
12744 
12745 	if (irq_re_init) {
12746 		bnxt_init_napi(bp);
12747 		rc = bnxt_request_irq(bp);
12748 		if (rc) {
12749 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
12750 			goto open_err_irq;
12751 		}
12752 	}
12753 
12754 	rc = bnxt_init_nic(bp, irq_re_init);
12755 	if (rc) {
12756 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12757 		goto open_err_irq;
12758 	}
12759 
12760 	bnxt_enable_napi(bp);
12761 	bnxt_debug_dev_init(bp);
12762 
12763 	if (link_re_init) {
12764 		mutex_lock(&bp->link_lock);
12765 		rc = bnxt_update_phy_setting(bp);
12766 		mutex_unlock(&bp->link_lock);
12767 		if (rc) {
12768 			netdev_warn(bp->dev, "failed to update phy settings\n");
12769 			if (BNXT_SINGLE_PF(bp)) {
12770 				bp->link_info.phy_retry = true;
12771 				bp->link_info.phy_retry_expires =
12772 					jiffies + 5 * HZ;
12773 			}
12774 		}
12775 	}
12776 
12777 	if (irq_re_init) {
12778 		udp_tunnel_nic_reset_ntf(bp->dev);
12779 		rc = bnxt_set_xps_mapping(bp);
12780 		if (rc)
12781 			netdev_warn(bp->dev, "failed to set xps mapping\n");
12782 	}
12783 
12784 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
12785 		if (!static_key_enabled(&bnxt_xdp_locking_key))
12786 			static_branch_enable(&bnxt_xdp_locking_key);
12787 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
12788 		static_branch_disable(&bnxt_xdp_locking_key);
12789 	}
12790 	set_bit(BNXT_STATE_OPEN, &bp->state);
12791 	bnxt_enable_int(bp);
12792 	/* Enable TX queues */
12793 	bnxt_tx_enable(bp);
12794 	mod_timer(&bp->timer, jiffies + bp->current_interval);
12795 	/* Poll link status and check for SFP+ module status */
12796 	mutex_lock(&bp->link_lock);
12797 	bnxt_get_port_module_status(bp);
12798 	mutex_unlock(&bp->link_lock);
12799 
12800 	/* VF-reps may need to be re-opened after the PF is re-opened */
12801 	if (BNXT_PF(bp))
12802 		bnxt_vf_reps_open(bp);
12803 	bnxt_ptp_init_rtc(bp, true);
12804 	bnxt_ptp_cfg_tstamp_filters(bp);
12805 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12806 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
12807 	bnxt_cfg_usr_fltrs(bp);
12808 	return 0;
12809 
12810 open_err_irq:
12811 	bnxt_del_napi(bp);
12812 
12813 open_err_free_mem:
12814 	bnxt_free_skbs(bp);
12815 	bnxt_free_irq(bp);
12816 	bnxt_free_mem(bp, true);
12817 	return rc;
12818 }
12819 
12820 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12821 {
12822 	int rc = 0;
12823 
12824 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12825 		rc = -EIO;
12826 	if (!rc)
12827 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12828 	if (rc) {
12829 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12830 		netif_close(bp->dev);
12831 	}
12832 	return rc;
12833 }
12834 
12835 /* netdev instance lock held, open the NIC half way by allocating all
12836  * resources, but NAPI, IRQ, and TX are not enabled.  This is mainly used
12837  * for offline self tests.
12838  */
12839 int bnxt_half_open_nic(struct bnxt *bp)
12840 {
12841 	int rc = 0;
12842 
12843 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12844 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
12845 		rc = -ENODEV;
12846 		goto half_open_err;
12847 	}
12848 
12849 	rc = bnxt_alloc_mem(bp, true);
12850 	if (rc) {
12851 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12852 		goto half_open_err;
12853 	}
12854 	bnxt_init_napi(bp);
12855 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12856 	rc = bnxt_init_nic(bp, true);
12857 	if (rc) {
12858 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12859 		bnxt_del_napi(bp);
12860 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12861 		goto half_open_err;
12862 	}
12863 	return 0;
12864 
12865 half_open_err:
12866 	bnxt_free_skbs(bp);
12867 	bnxt_free_mem(bp, true);
12868 	netif_close(bp->dev);
12869 	return rc;
12870 }
12871 
12872 /* netdev instance lock held, this call can only be made after a previous
12873  * successful call to bnxt_half_open_nic().
12874  */
12875 void bnxt_half_close_nic(struct bnxt *bp)
12876 {
12877 	bnxt_hwrm_resource_free(bp, false, true);
12878 	bnxt_del_napi(bp);
12879 	bnxt_free_skbs(bp);
12880 	bnxt_free_mem(bp, true);
12881 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12882 }
12883 
12884 void bnxt_reenable_sriov(struct bnxt *bp)
12885 {
12886 	if (BNXT_PF(bp)) {
12887 		struct bnxt_pf_info *pf = &bp->pf;
12888 		int n = pf->active_vfs;
12889 
12890 		if (n)
12891 			bnxt_cfg_hw_sriov(bp, &n, true);
12892 	}
12893 }
12894 
12895 static int bnxt_open(struct net_device *dev)
12896 {
12897 	struct bnxt *bp = netdev_priv(dev);
12898 	int rc;
12899 
12900 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12901 		rc = bnxt_reinit_after_abort(bp);
12902 		if (rc) {
12903 			if (rc == -EBUSY)
12904 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
12905 			else
12906 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
12907 			return -ENODEV;
12908 		}
12909 	}
12910 
12911 	rc = bnxt_hwrm_if_change(bp, true);
12912 	if (rc)
12913 		return rc;
12914 
12915 	rc = __bnxt_open_nic(bp, true, true);
12916 	if (rc) {
12917 		bnxt_hwrm_if_change(bp, false);
12918 	} else {
12919 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
12920 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12921 				bnxt_queue_sp_work(bp,
12922 						   BNXT_RESTART_ULP_SP_EVENT);
12923 		}
12924 	}
12925 
12926 	return rc;
12927 }
12928 
12929 static bool bnxt_drv_busy(struct bnxt *bp)
12930 {
12931 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
12932 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
12933 }
12934 
12935 static void bnxt_get_ring_stats(struct bnxt *bp,
12936 				struct rtnl_link_stats64 *stats);
12937 
12938 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
12939 			     bool link_re_init)
12940 {
12941 	/* Close the VF-reps before closing PF */
12942 	if (BNXT_PF(bp))
12943 		bnxt_vf_reps_close(bp);
12944 
12945 	/* Change device state to avoid TX queue wake up's */
12946 	bnxt_tx_disable(bp);
12947 
12948 	clear_bit(BNXT_STATE_OPEN, &bp->state);
12949 	smp_mb__after_atomic();
12950 	while (bnxt_drv_busy(bp))
12951 		msleep(20);
12952 
12953 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12954 		bnxt_clear_rss_ctxs(bp);
12955 	/* Flush rings and disable interrupts */
12956 	bnxt_shutdown_nic(bp, irq_re_init);
12957 
12958 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
12959 
12960 	bnxt_debug_dev_exit(bp);
12961 	bnxt_disable_napi(bp);
12962 	timer_delete_sync(&bp->timer);
12963 	bnxt_free_skbs(bp);
12964 
12965 	/* Save ring stats before shutdown */
12966 	if (bp->bnapi && irq_re_init) {
12967 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
12968 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
12969 	}
12970 	if (irq_re_init) {
12971 		bnxt_free_irq(bp);
12972 		bnxt_del_napi(bp);
12973 	}
12974 	bnxt_free_mem(bp, irq_re_init);
12975 }
12976 
12977 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12978 {
12979 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12980 		/* If we get here, it means firmware reset is in progress
12981 		 * while we are trying to close.  We can safely proceed with
12982 		 * the close because we are holding netdev instance lock.
12983 		 * Some firmware messages may fail as we proceed to close.
12984 		 * We set the ABORT_ERR flag here so that the FW reset thread
12985 		 * will later abort when it gets the netdev instance lock
12986 		 * and sees the flag.
12987 		 */
12988 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
12989 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12990 	}
12991 
12992 #ifdef CONFIG_BNXT_SRIOV
12993 	if (bp->sriov_cfg) {
12994 		int rc;
12995 
12996 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
12997 						      !bp->sriov_cfg,
12998 						      BNXT_SRIOV_CFG_WAIT_TMO);
12999 		if (!rc)
13000 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
13001 		else if (rc < 0)
13002 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
13003 	}
13004 #endif
13005 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
13006 }
13007 
13008 static int bnxt_close(struct net_device *dev)
13009 {
13010 	struct bnxt *bp = netdev_priv(dev);
13011 
13012 	bnxt_close_nic(bp, true, true);
13013 	bnxt_hwrm_shutdown_link(bp);
13014 	bnxt_hwrm_if_change(bp, false);
13015 	return 0;
13016 }
13017 
13018 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
13019 				   u16 *val)
13020 {
13021 	struct hwrm_port_phy_mdio_read_output *resp;
13022 	struct hwrm_port_phy_mdio_read_input *req;
13023 	int rc;
13024 
13025 	if (bp->hwrm_spec_code < 0x10a00)
13026 		return -EOPNOTSUPP;
13027 
13028 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
13029 	if (rc)
13030 		return rc;
13031 
13032 	req->port_id = cpu_to_le16(bp->pf.port_id);
13033 	req->phy_addr = phy_addr;
13034 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13035 	if (mdio_phy_id_is_c45(phy_addr)) {
13036 		req->cl45_mdio = 1;
13037 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13038 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13039 		req->reg_addr = cpu_to_le16(reg);
13040 	}
13041 
13042 	resp = hwrm_req_hold(bp, req);
13043 	rc = hwrm_req_send(bp, req);
13044 	if (!rc)
13045 		*val = le16_to_cpu(resp->reg_data);
13046 	hwrm_req_drop(bp, req);
13047 	return rc;
13048 }
13049 
13050 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
13051 				    u16 val)
13052 {
13053 	struct hwrm_port_phy_mdio_write_input *req;
13054 	int rc;
13055 
13056 	if (bp->hwrm_spec_code < 0x10a00)
13057 		return -EOPNOTSUPP;
13058 
13059 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
13060 	if (rc)
13061 		return rc;
13062 
13063 	req->port_id = cpu_to_le16(bp->pf.port_id);
13064 	req->phy_addr = phy_addr;
13065 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13066 	if (mdio_phy_id_is_c45(phy_addr)) {
13067 		req->cl45_mdio = 1;
13068 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13069 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13070 		req->reg_addr = cpu_to_le16(reg);
13071 	}
13072 	req->reg_data = cpu_to_le16(val);
13073 
13074 	return hwrm_req_send(bp, req);
13075 }
13076 
13077 /* netdev instance lock held */
13078 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13079 {
13080 	struct mii_ioctl_data *mdio = if_mii(ifr);
13081 	struct bnxt *bp = netdev_priv(dev);
13082 	int rc;
13083 
13084 	switch (cmd) {
13085 	case SIOCGMIIPHY:
13086 		mdio->phy_id = bp->link_info.phy_addr;
13087 
13088 		fallthrough;
13089 	case SIOCGMIIREG: {
13090 		u16 mii_regval = 0;
13091 
13092 		if (!netif_running(dev))
13093 			return -EAGAIN;
13094 
13095 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
13096 					     &mii_regval);
13097 		mdio->val_out = mii_regval;
13098 		return rc;
13099 	}
13100 
13101 	case SIOCSMIIREG:
13102 		if (!netif_running(dev))
13103 			return -EAGAIN;
13104 
13105 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
13106 						mdio->val_in);
13107 
13108 	case SIOCSHWTSTAMP:
13109 		return bnxt_hwtstamp_set(dev, ifr);
13110 
13111 	case SIOCGHWTSTAMP:
13112 		return bnxt_hwtstamp_get(dev, ifr);
13113 
13114 	default:
13115 		/* do nothing */
13116 		break;
13117 	}
13118 	return -EOPNOTSUPP;
13119 }
13120 
13121 static void bnxt_get_ring_stats(struct bnxt *bp,
13122 				struct rtnl_link_stats64 *stats)
13123 {
13124 	int i;
13125 
13126 	for (i = 0; i < bp->cp_nr_rings; i++) {
13127 		struct bnxt_napi *bnapi = bp->bnapi[i];
13128 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13129 		u64 *sw = cpr->stats.sw_stats;
13130 
13131 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
13132 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13133 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
13134 
13135 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
13136 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
13137 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
13138 
13139 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
13140 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
13141 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
13142 
13143 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
13144 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
13145 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
13146 
13147 		stats->rx_missed_errors +=
13148 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
13149 
13150 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13151 
13152 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
13153 
13154 		stats->rx_dropped +=
13155 			cpr->sw_stats->rx.rx_netpoll_discards +
13156 			cpr->sw_stats->rx.rx_oom_discards;
13157 	}
13158 }
13159 
13160 static void bnxt_add_prev_stats(struct bnxt *bp,
13161 				struct rtnl_link_stats64 *stats)
13162 {
13163 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
13164 
13165 	stats->rx_packets += prev_stats->rx_packets;
13166 	stats->tx_packets += prev_stats->tx_packets;
13167 	stats->rx_bytes += prev_stats->rx_bytes;
13168 	stats->tx_bytes += prev_stats->tx_bytes;
13169 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
13170 	stats->multicast += prev_stats->multicast;
13171 	stats->rx_dropped += prev_stats->rx_dropped;
13172 	stats->tx_dropped += prev_stats->tx_dropped;
13173 }
13174 
13175 static void
13176 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
13177 {
13178 	struct bnxt *bp = netdev_priv(dev);
13179 
13180 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
13181 	/* Make sure bnxt_close_nic() sees that we are reading stats before
13182 	 * we check the BNXT_STATE_OPEN flag.
13183 	 */
13184 	smp_mb__after_atomic();
13185 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13186 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13187 		*stats = bp->net_stats_prev;
13188 		return;
13189 	}
13190 
13191 	bnxt_get_ring_stats(bp, stats);
13192 	bnxt_add_prev_stats(bp, stats);
13193 
13194 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
13195 		u64 *rx = bp->port_stats.sw_stats;
13196 		u64 *tx = bp->port_stats.sw_stats +
13197 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
13198 
13199 		stats->rx_crc_errors =
13200 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
13201 		stats->rx_frame_errors =
13202 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
13203 		stats->rx_length_errors =
13204 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
13205 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
13206 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
13207 		stats->rx_errors =
13208 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
13209 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
13210 		stats->collisions =
13211 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
13212 		stats->tx_fifo_errors =
13213 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
13214 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
13215 	}
13216 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13217 }
13218 
13219 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
13220 					struct bnxt_total_ring_err_stats *stats,
13221 					struct bnxt_cp_ring_info *cpr)
13222 {
13223 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
13224 	u64 *hw_stats = cpr->stats.sw_stats;
13225 
13226 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
13227 	stats->rx_total_resets += sw_stats->rx.rx_resets;
13228 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
13229 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
13230 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
13231 	stats->rx_total_ring_discards +=
13232 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
13233 	stats->tx_total_resets += sw_stats->tx.tx_resets;
13234 	stats->tx_total_ring_discards +=
13235 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
13236 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
13237 }
13238 
13239 void bnxt_get_ring_err_stats(struct bnxt *bp,
13240 			     struct bnxt_total_ring_err_stats *stats)
13241 {
13242 	int i;
13243 
13244 	for (i = 0; i < bp->cp_nr_rings; i++)
13245 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
13246 }
13247 
13248 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
13249 {
13250 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13251 	struct net_device *dev = bp->dev;
13252 	struct netdev_hw_addr *ha;
13253 	u8 *haddr;
13254 	int mc_count = 0;
13255 	bool update = false;
13256 	int off = 0;
13257 
13258 	netdev_for_each_mc_addr(ha, dev) {
13259 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
13260 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13261 			vnic->mc_list_count = 0;
13262 			return false;
13263 		}
13264 		haddr = ha->addr;
13265 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
13266 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
13267 			update = true;
13268 		}
13269 		off += ETH_ALEN;
13270 		mc_count++;
13271 	}
13272 	if (mc_count)
13273 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13274 
13275 	if (mc_count != vnic->mc_list_count) {
13276 		vnic->mc_list_count = mc_count;
13277 		update = true;
13278 	}
13279 	return update;
13280 }
13281 
13282 static bool bnxt_uc_list_updated(struct bnxt *bp)
13283 {
13284 	struct net_device *dev = bp->dev;
13285 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13286 	struct netdev_hw_addr *ha;
13287 	int off = 0;
13288 
13289 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
13290 		return true;
13291 
13292 	netdev_for_each_uc_addr(ha, dev) {
13293 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
13294 			return true;
13295 
13296 		off += ETH_ALEN;
13297 	}
13298 	return false;
13299 }
13300 
13301 static void bnxt_set_rx_mode(struct net_device *dev)
13302 {
13303 	struct bnxt *bp = netdev_priv(dev);
13304 	struct bnxt_vnic_info *vnic;
13305 	bool mc_update = false;
13306 	bool uc_update;
13307 	u32 mask;
13308 
13309 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13310 		return;
13311 
13312 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13313 	mask = vnic->rx_mask;
13314 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13315 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13316 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13317 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13318 
13319 	if (dev->flags & IFF_PROMISC)
13320 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13321 
13322 	uc_update = bnxt_uc_list_updated(bp);
13323 
13324 	if (dev->flags & IFF_BROADCAST)
13325 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13326 	if (dev->flags & IFF_ALLMULTI) {
13327 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13328 		vnic->mc_list_count = 0;
13329 	} else if (dev->flags & IFF_MULTICAST) {
13330 		mc_update = bnxt_mc_list_updated(bp, &mask);
13331 	}
13332 
13333 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13334 		vnic->rx_mask = mask;
13335 
13336 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13337 	}
13338 }
13339 
13340 static int bnxt_cfg_rx_mode(struct bnxt *bp)
13341 {
13342 	struct net_device *dev = bp->dev;
13343 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13344 	struct netdev_hw_addr *ha;
13345 	int i, off = 0, rc;
13346 	bool uc_update;
13347 
13348 	netif_addr_lock_bh(dev);
13349 	uc_update = bnxt_uc_list_updated(bp);
13350 	netif_addr_unlock_bh(dev);
13351 
13352 	if (!uc_update)
13353 		goto skip_uc;
13354 
13355 	for (i = 1; i < vnic->uc_filter_count; i++) {
13356 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13357 
13358 		bnxt_hwrm_l2_filter_free(bp, fltr);
13359 		bnxt_del_l2_filter(bp, fltr);
13360 	}
13361 
13362 	vnic->uc_filter_count = 1;
13363 
13364 	netif_addr_lock_bh(dev);
13365 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
13366 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13367 	} else {
13368 		netdev_for_each_uc_addr(ha, dev) {
13369 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13370 			off += ETH_ALEN;
13371 			vnic->uc_filter_count++;
13372 		}
13373 	}
13374 	netif_addr_unlock_bh(dev);
13375 
13376 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13377 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13378 		if (rc) {
13379 			if (BNXT_VF(bp) && rc == -ENODEV) {
13380 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13381 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13382 				else
13383 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
13384 				rc = 0;
13385 			} else {
13386 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13387 			}
13388 			vnic->uc_filter_count = i;
13389 			return rc;
13390 		}
13391 	}
13392 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13393 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
13394 
13395 skip_uc:
13396 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13397 	    !bnxt_promisc_ok(bp))
13398 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13399 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13400 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13401 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13402 			    rc);
13403 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13404 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13405 		vnic->mc_list_count = 0;
13406 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13407 	}
13408 	if (rc)
13409 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13410 			   rc);
13411 
13412 	return rc;
13413 }
13414 
13415 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13416 {
13417 #ifdef CONFIG_BNXT_SRIOV
13418 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13419 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13420 
13421 		/* No minimum rings were provisioned by the PF.  Don't
13422 		 * reserve rings by default when device is down.
13423 		 */
13424 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13425 			return true;
13426 
13427 		if (!netif_running(bp->dev))
13428 			return false;
13429 	}
13430 #endif
13431 	return true;
13432 }
13433 
13434 /* If the chip and firmware supports RFS */
13435 static bool bnxt_rfs_supported(struct bnxt *bp)
13436 {
13437 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13438 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13439 			return true;
13440 		return false;
13441 	}
13442 	/* 212 firmware is broken for aRFS */
13443 	if (BNXT_FW_MAJ(bp) == 212)
13444 		return false;
13445 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13446 		return true;
13447 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13448 		return true;
13449 	return false;
13450 }
13451 
13452 /* If runtime conditions support RFS */
13453 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13454 {
13455 	struct bnxt_hw_rings hwr = {0};
13456 	int max_vnics, max_rss_ctxs;
13457 
13458 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13459 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13460 		return bnxt_rfs_supported(bp);
13461 
13462 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13463 		return false;
13464 
13465 	hwr.grp = bp->rx_nr_rings;
13466 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13467 	if (new_rss_ctx)
13468 		hwr.vnic++;
13469 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13470 	max_vnics = bnxt_get_max_func_vnics(bp);
13471 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13472 
13473 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13474 		if (bp->rx_nr_rings > 1)
13475 			netdev_warn(bp->dev,
13476 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13477 				    min(max_rss_ctxs - 1, max_vnics - 1));
13478 		return false;
13479 	}
13480 
13481 	if (!BNXT_NEW_RM(bp))
13482 		return true;
13483 
13484 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13485 	 * issue that will mess up the default VNIC if we reduce the
13486 	 * reservations.
13487 	 */
13488 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13489 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13490 		return true;
13491 
13492 	bnxt_hwrm_reserve_rings(bp, &hwr);
13493 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13494 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13495 		return true;
13496 
13497 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13498 	hwr.vnic = 1;
13499 	hwr.rss_ctx = 0;
13500 	bnxt_hwrm_reserve_rings(bp, &hwr);
13501 	return false;
13502 }
13503 
13504 static netdev_features_t bnxt_fix_features(struct net_device *dev,
13505 					   netdev_features_t features)
13506 {
13507 	struct bnxt *bp = netdev_priv(dev);
13508 	netdev_features_t vlan_features;
13509 
13510 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
13511 		features &= ~NETIF_F_NTUPLE;
13512 
13513 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
13514 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13515 
13516 	if (!(features & NETIF_F_GRO))
13517 		features &= ~NETIF_F_GRO_HW;
13518 
13519 	if (features & NETIF_F_GRO_HW)
13520 		features &= ~NETIF_F_LRO;
13521 
13522 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
13523 	 * turned on or off together.
13524 	 */
13525 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
13526 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
13527 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13528 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13529 		else if (vlan_features)
13530 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13531 	}
13532 #ifdef CONFIG_BNXT_SRIOV
13533 	if (BNXT_VF(bp) && bp->vf.vlan)
13534 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13535 #endif
13536 	return features;
13537 }
13538 
13539 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
13540 				bool link_re_init, u32 flags, bool update_tpa)
13541 {
13542 	bnxt_close_nic(bp, irq_re_init, link_re_init);
13543 	bp->flags = flags;
13544 	if (update_tpa)
13545 		bnxt_set_ring_params(bp);
13546 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
13547 }
13548 
13549 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
13550 {
13551 	bool update_tpa = false, update_ntuple = false;
13552 	struct bnxt *bp = netdev_priv(dev);
13553 	u32 flags = bp->flags;
13554 	u32 changes;
13555 	int rc = 0;
13556 	bool re_init = false;
13557 
13558 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
13559 	if (features & NETIF_F_GRO_HW)
13560 		flags |= BNXT_FLAG_GRO;
13561 	else if (features & NETIF_F_LRO)
13562 		flags |= BNXT_FLAG_LRO;
13563 
13564 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
13565 		flags &= ~BNXT_FLAG_TPA;
13566 
13567 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13568 		flags |= BNXT_FLAG_STRIP_VLAN;
13569 
13570 	if (features & NETIF_F_NTUPLE)
13571 		flags |= BNXT_FLAG_RFS;
13572 	else
13573 		bnxt_clear_usr_fltrs(bp, true);
13574 
13575 	changes = flags ^ bp->flags;
13576 	if (changes & BNXT_FLAG_TPA) {
13577 		update_tpa = true;
13578 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
13579 		    (flags & BNXT_FLAG_TPA) == 0 ||
13580 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13581 			re_init = true;
13582 	}
13583 
13584 	if (changes & ~BNXT_FLAG_TPA)
13585 		re_init = true;
13586 
13587 	if (changes & BNXT_FLAG_RFS)
13588 		update_ntuple = true;
13589 
13590 	if (flags != bp->flags) {
13591 		u32 old_flags = bp->flags;
13592 
13593 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13594 			bp->flags = flags;
13595 			if (update_tpa)
13596 				bnxt_set_ring_params(bp);
13597 			return rc;
13598 		}
13599 
13600 		if (update_ntuple)
13601 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
13602 
13603 		if (re_init)
13604 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
13605 
13606 		if (update_tpa) {
13607 			bp->flags = flags;
13608 			rc = bnxt_set_tpa(bp,
13609 					  (flags & BNXT_FLAG_TPA) ?
13610 					  true : false);
13611 			if (rc)
13612 				bp->flags = old_flags;
13613 		}
13614 	}
13615 	return rc;
13616 }
13617 
13618 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
13619 			      u8 **nextp)
13620 {
13621 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
13622 	struct hop_jumbo_hdr *jhdr;
13623 	int hdr_count = 0;
13624 	u8 *nexthdr;
13625 	int start;
13626 
13627 	/* Check that there are at most 2 IPv6 extension headers, no
13628 	 * fragment header, and each is <= 64 bytes.
13629 	 */
13630 	start = nw_off + sizeof(*ip6h);
13631 	nexthdr = &ip6h->nexthdr;
13632 	while (ipv6_ext_hdr(*nexthdr)) {
13633 		struct ipv6_opt_hdr *hp;
13634 		int hdrlen;
13635 
13636 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
13637 		    *nexthdr == NEXTHDR_FRAGMENT)
13638 			return false;
13639 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
13640 					  skb_headlen(skb), NULL);
13641 		if (!hp)
13642 			return false;
13643 		if (*nexthdr == NEXTHDR_AUTH)
13644 			hdrlen = ipv6_authlen(hp);
13645 		else
13646 			hdrlen = ipv6_optlen(hp);
13647 
13648 		if (hdrlen > 64)
13649 			return false;
13650 
13651 		/* The ext header may be a hop-by-hop header inserted for
13652 		 * big TCP purposes. This will be removed before sending
13653 		 * from NIC, so do not count it.
13654 		 */
13655 		if (*nexthdr == NEXTHDR_HOP) {
13656 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
13657 				goto increment_hdr;
13658 
13659 			jhdr = (struct hop_jumbo_hdr *)hp;
13660 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
13661 			    jhdr->nexthdr != IPPROTO_TCP)
13662 				goto increment_hdr;
13663 
13664 			goto next_hdr;
13665 		}
13666 increment_hdr:
13667 		hdr_count++;
13668 next_hdr:
13669 		nexthdr = &hp->nexthdr;
13670 		start += hdrlen;
13671 	}
13672 	if (nextp) {
13673 		/* Caller will check inner protocol */
13674 		if (skb->encapsulation) {
13675 			*nextp = nexthdr;
13676 			return true;
13677 		}
13678 		*nextp = NULL;
13679 	}
13680 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
13681 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
13682 }
13683 
13684 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
13685 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
13686 {
13687 	struct udphdr *uh = udp_hdr(skb);
13688 	__be16 udp_port = uh->dest;
13689 
13690 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
13691 	    udp_port != bp->vxlan_gpe_port)
13692 		return false;
13693 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
13694 		struct ethhdr *eh = inner_eth_hdr(skb);
13695 
13696 		switch (eh->h_proto) {
13697 		case htons(ETH_P_IP):
13698 			return true;
13699 		case htons(ETH_P_IPV6):
13700 			return bnxt_exthdr_check(bp, skb,
13701 						 skb_inner_network_offset(skb),
13702 						 NULL);
13703 		}
13704 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
13705 		return true;
13706 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
13707 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13708 					 NULL);
13709 	}
13710 	return false;
13711 }
13712 
13713 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
13714 {
13715 	switch (l4_proto) {
13716 	case IPPROTO_UDP:
13717 		return bnxt_udp_tunl_check(bp, skb);
13718 	case IPPROTO_IPIP:
13719 		return true;
13720 	case IPPROTO_GRE: {
13721 		switch (skb->inner_protocol) {
13722 		default:
13723 			return false;
13724 		case htons(ETH_P_IP):
13725 			return true;
13726 		case htons(ETH_P_IPV6):
13727 			fallthrough;
13728 		}
13729 	}
13730 	case IPPROTO_IPV6:
13731 		/* Check ext headers of inner ipv6 */
13732 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13733 					 NULL);
13734 	}
13735 	return false;
13736 }
13737 
13738 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
13739 					     struct net_device *dev,
13740 					     netdev_features_t features)
13741 {
13742 	struct bnxt *bp = netdev_priv(dev);
13743 	u8 *l4_proto;
13744 
13745 	features = vlan_features_check(skb, features);
13746 	switch (vlan_get_protocol(skb)) {
13747 	case htons(ETH_P_IP):
13748 		if (!skb->encapsulation)
13749 			return features;
13750 		l4_proto = &ip_hdr(skb)->protocol;
13751 		if (bnxt_tunl_check(bp, skb, *l4_proto))
13752 			return features;
13753 		break;
13754 	case htons(ETH_P_IPV6):
13755 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
13756 				       &l4_proto))
13757 			break;
13758 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
13759 			return features;
13760 		break;
13761 	}
13762 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13763 }
13764 
13765 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
13766 			 u32 *reg_buf)
13767 {
13768 	struct hwrm_dbg_read_direct_output *resp;
13769 	struct hwrm_dbg_read_direct_input *req;
13770 	__le32 *dbg_reg_buf;
13771 	dma_addr_t mapping;
13772 	int rc, i;
13773 
13774 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
13775 	if (rc)
13776 		return rc;
13777 
13778 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
13779 					 &mapping);
13780 	if (!dbg_reg_buf) {
13781 		rc = -ENOMEM;
13782 		goto dbg_rd_reg_exit;
13783 	}
13784 
13785 	req->host_dest_addr = cpu_to_le64(mapping);
13786 
13787 	resp = hwrm_req_hold(bp, req);
13788 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
13789 	req->read_len32 = cpu_to_le32(num_words);
13790 
13791 	rc = hwrm_req_send(bp, req);
13792 	if (rc || resp->error_code) {
13793 		rc = -EIO;
13794 		goto dbg_rd_reg_exit;
13795 	}
13796 	for (i = 0; i < num_words; i++)
13797 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
13798 
13799 dbg_rd_reg_exit:
13800 	hwrm_req_drop(bp, req);
13801 	return rc;
13802 }
13803 
13804 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
13805 				       u32 ring_id, u32 *prod, u32 *cons)
13806 {
13807 	struct hwrm_dbg_ring_info_get_output *resp;
13808 	struct hwrm_dbg_ring_info_get_input *req;
13809 	int rc;
13810 
13811 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
13812 	if (rc)
13813 		return rc;
13814 
13815 	req->ring_type = ring_type;
13816 	req->fw_ring_id = cpu_to_le32(ring_id);
13817 	resp = hwrm_req_hold(bp, req);
13818 	rc = hwrm_req_send(bp, req);
13819 	if (!rc) {
13820 		*prod = le32_to_cpu(resp->producer_index);
13821 		*cons = le32_to_cpu(resp->consumer_index);
13822 	}
13823 	hwrm_req_drop(bp, req);
13824 	return rc;
13825 }
13826 
13827 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13828 {
13829 	struct bnxt_tx_ring_info *txr;
13830 	int i = bnapi->index, j;
13831 
13832 	bnxt_for_each_napi_tx(j, bnapi, txr)
13833 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13834 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13835 			    txr->tx_cons);
13836 }
13837 
13838 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
13839 {
13840 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
13841 	int i = bnapi->index;
13842 
13843 	if (!rxr)
13844 		return;
13845 
13846 	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",
13847 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
13848 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
13849 		    rxr->rx_sw_agg_prod);
13850 }
13851 
13852 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
13853 {
13854 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13855 	int i = bnapi->index;
13856 
13857 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
13858 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
13859 }
13860 
13861 static void bnxt_dbg_dump_states(struct bnxt *bp)
13862 {
13863 	int i;
13864 	struct bnxt_napi *bnapi;
13865 
13866 	for (i = 0; i < bp->cp_nr_rings; i++) {
13867 		bnapi = bp->bnapi[i];
13868 		if (netif_msg_drv(bp)) {
13869 			bnxt_dump_tx_sw_state(bnapi);
13870 			bnxt_dump_rx_sw_state(bnapi);
13871 			bnxt_dump_cp_sw_state(bnapi);
13872 		}
13873 	}
13874 }
13875 
13876 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
13877 {
13878 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
13879 	struct hwrm_ring_reset_input *req;
13880 	struct bnxt_napi *bnapi = rxr->bnapi;
13881 	struct bnxt_cp_ring_info *cpr;
13882 	u16 cp_ring_id;
13883 	int rc;
13884 
13885 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
13886 	if (rc)
13887 		return rc;
13888 
13889 	cpr = &bnapi->cp_ring;
13890 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
13891 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
13892 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
13893 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
13894 	return hwrm_req_send_silent(bp, req);
13895 }
13896 
13897 static void bnxt_reset_task(struct bnxt *bp, bool silent)
13898 {
13899 	if (!silent)
13900 		bnxt_dbg_dump_states(bp);
13901 	if (netif_running(bp->dev)) {
13902 		bnxt_close_nic(bp, !silent, false);
13903 		bnxt_open_nic(bp, !silent, false);
13904 	}
13905 }
13906 
13907 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
13908 {
13909 	struct bnxt *bp = netdev_priv(dev);
13910 
13911 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
13912 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
13913 }
13914 
13915 static void bnxt_fw_health_check(struct bnxt *bp)
13916 {
13917 	struct bnxt_fw_health *fw_health = bp->fw_health;
13918 	struct pci_dev *pdev = bp->pdev;
13919 	u32 val;
13920 
13921 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13922 		return;
13923 
13924 	/* Make sure it is enabled before checking the tmr_counter. */
13925 	smp_rmb();
13926 	if (fw_health->tmr_counter) {
13927 		fw_health->tmr_counter--;
13928 		return;
13929 	}
13930 
13931 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13932 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
13933 		fw_health->arrests++;
13934 		goto fw_reset;
13935 	}
13936 
13937 	fw_health->last_fw_heartbeat = val;
13938 
13939 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13940 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
13941 		fw_health->discoveries++;
13942 		goto fw_reset;
13943 	}
13944 
13945 	fw_health->tmr_counter = fw_health->tmr_multiplier;
13946 	return;
13947 
13948 fw_reset:
13949 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
13950 }
13951 
13952 static void bnxt_timer(struct timer_list *t)
13953 {
13954 	struct bnxt *bp = from_timer(bp, t, timer);
13955 	struct net_device *dev = bp->dev;
13956 
13957 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
13958 		return;
13959 
13960 	if (atomic_read(&bp->intr_sem) != 0)
13961 		goto bnxt_restart_timer;
13962 
13963 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
13964 		bnxt_fw_health_check(bp);
13965 
13966 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
13967 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
13968 
13969 	if (bnxt_tc_flower_enabled(bp))
13970 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
13971 
13972 #ifdef CONFIG_RFS_ACCEL
13973 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
13974 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
13975 #endif /*CONFIG_RFS_ACCEL*/
13976 
13977 	if (bp->link_info.phy_retry) {
13978 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
13979 			bp->link_info.phy_retry = false;
13980 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
13981 		} else {
13982 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
13983 		}
13984 	}
13985 
13986 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13987 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13988 
13989 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
13990 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
13991 
13992 bnxt_restart_timer:
13993 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13994 }
13995 
13996 static void bnxt_lock_sp(struct bnxt *bp)
13997 {
13998 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
13999 	 * set.  If the device is being closed, bnxt_close() may be holding
14000 	 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear.
14001 	 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev
14002 	 * instance lock.
14003 	 */
14004 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14005 	netdev_lock(bp->dev);
14006 }
14007 
14008 static void bnxt_unlock_sp(struct bnxt *bp)
14009 {
14010 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14011 	netdev_unlock(bp->dev);
14012 }
14013 
14014 /* Only called from bnxt_sp_task() */
14015 static void bnxt_reset(struct bnxt *bp, bool silent)
14016 {
14017 	bnxt_lock_sp(bp);
14018 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
14019 		bnxt_reset_task(bp, silent);
14020 	bnxt_unlock_sp(bp);
14021 }
14022 
14023 /* Only called from bnxt_sp_task() */
14024 static void bnxt_rx_ring_reset(struct bnxt *bp)
14025 {
14026 	int i;
14027 
14028 	bnxt_lock_sp(bp);
14029 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14030 		bnxt_unlock_sp(bp);
14031 		return;
14032 	}
14033 	/* Disable and flush TPA before resetting the RX ring */
14034 	if (bp->flags & BNXT_FLAG_TPA)
14035 		bnxt_set_tpa(bp, false);
14036 	for (i = 0; i < bp->rx_nr_rings; i++) {
14037 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
14038 		struct bnxt_cp_ring_info *cpr;
14039 		int rc;
14040 
14041 		if (!rxr->bnapi->in_reset)
14042 			continue;
14043 
14044 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
14045 		if (rc) {
14046 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
14047 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
14048 			else
14049 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
14050 					    rc);
14051 			bnxt_reset_task(bp, true);
14052 			break;
14053 		}
14054 		bnxt_free_one_rx_ring_skbs(bp, rxr);
14055 		rxr->rx_prod = 0;
14056 		rxr->rx_agg_prod = 0;
14057 		rxr->rx_sw_agg_prod = 0;
14058 		rxr->rx_next_cons = 0;
14059 		rxr->bnapi->in_reset = false;
14060 		bnxt_alloc_one_rx_ring(bp, i);
14061 		cpr = &rxr->bnapi->cp_ring;
14062 		cpr->sw_stats->rx.rx_resets++;
14063 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
14064 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
14065 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
14066 	}
14067 	if (bp->flags & BNXT_FLAG_TPA)
14068 		bnxt_set_tpa(bp, true);
14069 	bnxt_unlock_sp(bp);
14070 }
14071 
14072 static void bnxt_fw_fatal_close(struct bnxt *bp)
14073 {
14074 	bnxt_tx_disable(bp);
14075 	bnxt_disable_napi(bp);
14076 	bnxt_disable_int_sync(bp);
14077 	bnxt_free_irq(bp);
14078 	bnxt_clear_int_mode(bp);
14079 	pci_disable_device(bp->pdev);
14080 }
14081 
14082 static void bnxt_fw_reset_close(struct bnxt *bp)
14083 {
14084 	/* When firmware is in fatal state, quiesce device and disable
14085 	 * bus master to prevent any potential bad DMAs before freeing
14086 	 * kernel memory.
14087 	 */
14088 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
14089 		u16 val = 0;
14090 
14091 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14092 		if (val == 0xffff)
14093 			bp->fw_reset_min_dsecs = 0;
14094 		bnxt_fw_fatal_close(bp);
14095 	}
14096 	__bnxt_close_nic(bp, true, false);
14097 	bnxt_vf_reps_free(bp);
14098 	bnxt_clear_int_mode(bp);
14099 	bnxt_hwrm_func_drv_unrgtr(bp);
14100 	if (pci_is_enabled(bp->pdev))
14101 		pci_disable_device(bp->pdev);
14102 	bnxt_free_ctx_mem(bp, false);
14103 }
14104 
14105 static bool is_bnxt_fw_ok(struct bnxt *bp)
14106 {
14107 	struct bnxt_fw_health *fw_health = bp->fw_health;
14108 	bool no_heartbeat = false, has_reset = false;
14109 	u32 val;
14110 
14111 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14112 	if (val == fw_health->last_fw_heartbeat)
14113 		no_heartbeat = true;
14114 
14115 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14116 	if (val != fw_health->last_fw_reset_cnt)
14117 		has_reset = true;
14118 
14119 	if (!no_heartbeat && has_reset)
14120 		return true;
14121 
14122 	return false;
14123 }
14124 
14125 /* netdev instance lock is acquired before calling this function */
14126 static void bnxt_force_fw_reset(struct bnxt *bp)
14127 {
14128 	struct bnxt_fw_health *fw_health = bp->fw_health;
14129 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14130 	u32 wait_dsecs;
14131 
14132 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
14133 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14134 		return;
14135 
14136 	/* we have to serialize with bnxt_refclk_read()*/
14137 	if (ptp) {
14138 		unsigned long flags;
14139 
14140 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
14141 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14142 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14143 	} else {
14144 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14145 	}
14146 	bnxt_fw_reset_close(bp);
14147 	wait_dsecs = fw_health->master_func_wait_dsecs;
14148 	if (fw_health->primary) {
14149 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
14150 			wait_dsecs = 0;
14151 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14152 	} else {
14153 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
14154 		wait_dsecs = fw_health->normal_func_wait_dsecs;
14155 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14156 	}
14157 
14158 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
14159 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
14160 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14161 }
14162 
14163 void bnxt_fw_exception(struct bnxt *bp)
14164 {
14165 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
14166 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14167 	bnxt_ulp_stop(bp);
14168 	bnxt_lock_sp(bp);
14169 	bnxt_force_fw_reset(bp);
14170 	bnxt_unlock_sp(bp);
14171 }
14172 
14173 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
14174  * < 0 on error.
14175  */
14176 static int bnxt_get_registered_vfs(struct bnxt *bp)
14177 {
14178 #ifdef CONFIG_BNXT_SRIOV
14179 	int rc;
14180 
14181 	if (!BNXT_PF(bp))
14182 		return 0;
14183 
14184 	rc = bnxt_hwrm_func_qcfg(bp);
14185 	if (rc) {
14186 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
14187 		return rc;
14188 	}
14189 	if (bp->pf.registered_vfs)
14190 		return bp->pf.registered_vfs;
14191 	if (bp->sriov_cfg)
14192 		return 1;
14193 #endif
14194 	return 0;
14195 }
14196 
14197 void bnxt_fw_reset(struct bnxt *bp)
14198 {
14199 	bnxt_ulp_stop(bp);
14200 	bnxt_lock_sp(bp);
14201 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
14202 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14203 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14204 		int n = 0, tmo;
14205 
14206 		/* we have to serialize with bnxt_refclk_read()*/
14207 		if (ptp) {
14208 			unsigned long flags;
14209 
14210 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
14211 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14212 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14213 		} else {
14214 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14215 		}
14216 		if (bp->pf.active_vfs &&
14217 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
14218 			n = bnxt_get_registered_vfs(bp);
14219 		if (n < 0) {
14220 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
14221 				   n);
14222 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14223 			netif_close(bp->dev);
14224 			goto fw_reset_exit;
14225 		} else if (n > 0) {
14226 			u16 vf_tmo_dsecs = n * 10;
14227 
14228 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
14229 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
14230 			bp->fw_reset_state =
14231 				BNXT_FW_RESET_STATE_POLL_VF;
14232 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14233 			goto fw_reset_exit;
14234 		}
14235 		bnxt_fw_reset_close(bp);
14236 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14237 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14238 			tmo = HZ / 10;
14239 		} else {
14240 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14241 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14242 		}
14243 		bnxt_queue_fw_reset_work(bp, tmo);
14244 	}
14245 fw_reset_exit:
14246 	bnxt_unlock_sp(bp);
14247 }
14248 
14249 static void bnxt_chk_missed_irq(struct bnxt *bp)
14250 {
14251 	int i;
14252 
14253 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14254 		return;
14255 
14256 	for (i = 0; i < bp->cp_nr_rings; i++) {
14257 		struct bnxt_napi *bnapi = bp->bnapi[i];
14258 		struct bnxt_cp_ring_info *cpr;
14259 		u32 fw_ring_id;
14260 		int j;
14261 
14262 		if (!bnapi)
14263 			continue;
14264 
14265 		cpr = &bnapi->cp_ring;
14266 		for (j = 0; j < cpr->cp_ring_count; j++) {
14267 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
14268 			u32 val[2];
14269 
14270 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
14271 				continue;
14272 
14273 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
14274 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
14275 				continue;
14276 			}
14277 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
14278 			bnxt_dbg_hwrm_ring_info_get(bp,
14279 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
14280 				fw_ring_id, &val[0], &val[1]);
14281 			cpr->sw_stats->cmn.missed_irqs++;
14282 		}
14283 	}
14284 }
14285 
14286 static void bnxt_cfg_ntp_filters(struct bnxt *);
14287 
14288 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
14289 {
14290 	struct bnxt_link_info *link_info = &bp->link_info;
14291 
14292 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
14293 		link_info->autoneg = BNXT_AUTONEG_SPEED;
14294 		if (bp->hwrm_spec_code >= 0x10201) {
14295 			if (link_info->auto_pause_setting &
14296 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
14297 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14298 		} else {
14299 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14300 		}
14301 		bnxt_set_auto_speed(link_info);
14302 	} else {
14303 		bnxt_set_force_speed(link_info);
14304 		link_info->req_duplex = link_info->duplex_setting;
14305 	}
14306 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14307 		link_info->req_flow_ctrl =
14308 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14309 	else
14310 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14311 }
14312 
14313 static void bnxt_fw_echo_reply(struct bnxt *bp)
14314 {
14315 	struct bnxt_fw_health *fw_health = bp->fw_health;
14316 	struct hwrm_func_echo_response_input *req;
14317 	int rc;
14318 
14319 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14320 	if (rc)
14321 		return;
14322 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14323 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14324 	hwrm_req_send(bp, req);
14325 }
14326 
14327 static void bnxt_ulp_restart(struct bnxt *bp)
14328 {
14329 	bnxt_ulp_stop(bp);
14330 	bnxt_ulp_start(bp, 0);
14331 }
14332 
14333 static void bnxt_sp_task(struct work_struct *work)
14334 {
14335 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14336 
14337 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14338 	smp_mb__after_atomic();
14339 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14340 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14341 		return;
14342 	}
14343 
14344 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14345 		bnxt_ulp_restart(bp);
14346 		bnxt_reenable_sriov(bp);
14347 	}
14348 
14349 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
14350 		bnxt_cfg_rx_mode(bp);
14351 
14352 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14353 		bnxt_cfg_ntp_filters(bp);
14354 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14355 		bnxt_hwrm_exec_fwd_req(bp);
14356 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14357 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14358 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14359 		bnxt_hwrm_port_qstats(bp, 0);
14360 		bnxt_hwrm_port_qstats_ext(bp, 0);
14361 		bnxt_accumulate_all_stats(bp);
14362 	}
14363 
14364 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14365 		int rc;
14366 
14367 		mutex_lock(&bp->link_lock);
14368 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14369 				       &bp->sp_event))
14370 			bnxt_hwrm_phy_qcaps(bp);
14371 
14372 		rc = bnxt_update_link(bp, true);
14373 		if (rc)
14374 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14375 				   rc);
14376 
14377 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14378 				       &bp->sp_event))
14379 			bnxt_init_ethtool_link_settings(bp);
14380 		mutex_unlock(&bp->link_lock);
14381 	}
14382 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14383 		int rc;
14384 
14385 		mutex_lock(&bp->link_lock);
14386 		rc = bnxt_update_phy_setting(bp);
14387 		mutex_unlock(&bp->link_lock);
14388 		if (rc) {
14389 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14390 		} else {
14391 			bp->link_info.phy_retry = false;
14392 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14393 		}
14394 	}
14395 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14396 		mutex_lock(&bp->link_lock);
14397 		bnxt_get_port_module_status(bp);
14398 		mutex_unlock(&bp->link_lock);
14399 	}
14400 
14401 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14402 		bnxt_tc_flow_stats_work(bp);
14403 
14404 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14405 		bnxt_chk_missed_irq(bp);
14406 
14407 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14408 		bnxt_fw_echo_reply(bp);
14409 
14410 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14411 		bnxt_hwmon_notify_event(bp);
14412 
14413 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14414 	 * must be the last functions to be called before exiting.
14415 	 */
14416 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14417 		bnxt_reset(bp, false);
14418 
14419 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14420 		bnxt_reset(bp, true);
14421 
14422 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14423 		bnxt_rx_ring_reset(bp);
14424 
14425 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14426 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14427 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14428 			bnxt_devlink_health_fw_report(bp);
14429 		else
14430 			bnxt_fw_reset(bp);
14431 	}
14432 
14433 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14434 		if (!is_bnxt_fw_ok(bp))
14435 			bnxt_devlink_health_fw_report(bp);
14436 	}
14437 
14438 	smp_mb__before_atomic();
14439 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14440 }
14441 
14442 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14443 				int *max_cp);
14444 
14445 /* Under netdev instance lock */
14446 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14447 		     int tx_xdp)
14448 {
14449 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14450 	struct bnxt_hw_rings hwr = {0};
14451 	int rx_rings = rx;
14452 	int rc;
14453 
14454 	if (tcs)
14455 		tx_sets = tcs;
14456 
14457 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
14458 
14459 	if (max_rx < rx_rings)
14460 		return -ENOMEM;
14461 
14462 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14463 		rx_rings <<= 1;
14464 
14465 	hwr.rx = rx_rings;
14466 	hwr.tx = tx * tx_sets + tx_xdp;
14467 	if (max_tx < hwr.tx)
14468 		return -ENOMEM;
14469 
14470 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
14471 
14472 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
14473 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
14474 	if (max_cp < hwr.cp)
14475 		return -ENOMEM;
14476 	hwr.stat = hwr.cp;
14477 	if (BNXT_NEW_RM(bp)) {
14478 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
14479 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
14480 		hwr.grp = rx;
14481 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14482 	}
14483 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
14484 		hwr.cp_p5 = hwr.tx + rx;
14485 	rc = bnxt_hwrm_check_rings(bp, &hwr);
14486 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
14487 		if (!bnxt_ulp_registered(bp->edev)) {
14488 			hwr.cp += bnxt_get_ulp_msix_num(bp);
14489 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
14490 		}
14491 		if (hwr.cp > bp->total_irqs) {
14492 			int total_msix = bnxt_change_msix(bp, hwr.cp);
14493 
14494 			if (total_msix < hwr.cp) {
14495 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
14496 					    hwr.cp, total_msix);
14497 				rc = -ENOSPC;
14498 			}
14499 		}
14500 	}
14501 	return rc;
14502 }
14503 
14504 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
14505 {
14506 	if (bp->bar2) {
14507 		pci_iounmap(pdev, bp->bar2);
14508 		bp->bar2 = NULL;
14509 	}
14510 
14511 	if (bp->bar1) {
14512 		pci_iounmap(pdev, bp->bar1);
14513 		bp->bar1 = NULL;
14514 	}
14515 
14516 	if (bp->bar0) {
14517 		pci_iounmap(pdev, bp->bar0);
14518 		bp->bar0 = NULL;
14519 	}
14520 }
14521 
14522 static void bnxt_cleanup_pci(struct bnxt *bp)
14523 {
14524 	bnxt_unmap_bars(bp, bp->pdev);
14525 	pci_release_regions(bp->pdev);
14526 	if (pci_is_enabled(bp->pdev))
14527 		pci_disable_device(bp->pdev);
14528 }
14529 
14530 static void bnxt_init_dflt_coal(struct bnxt *bp)
14531 {
14532 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
14533 	struct bnxt_coal *coal;
14534 	u16 flags = 0;
14535 
14536 	if (coal_cap->cmpl_params &
14537 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
14538 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
14539 
14540 	/* Tick values in micro seconds.
14541 	 * 1 coal_buf x bufs_per_record = 1 completion record.
14542 	 */
14543 	coal = &bp->rx_coal;
14544 	coal->coal_ticks = 10;
14545 	coal->coal_bufs = 30;
14546 	coal->coal_ticks_irq = 1;
14547 	coal->coal_bufs_irq = 2;
14548 	coal->idle_thresh = 50;
14549 	coal->bufs_per_record = 2;
14550 	coal->budget = 64;		/* NAPI budget */
14551 	coal->flags = flags;
14552 
14553 	coal = &bp->tx_coal;
14554 	coal->coal_ticks = 28;
14555 	coal->coal_bufs = 30;
14556 	coal->coal_ticks_irq = 2;
14557 	coal->coal_bufs_irq = 2;
14558 	coal->bufs_per_record = 1;
14559 	coal->flags = flags;
14560 
14561 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
14562 }
14563 
14564 /* FW that pre-reserves 1 VNIC per function */
14565 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
14566 {
14567 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
14568 
14569 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14570 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
14571 		return true;
14572 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14573 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
14574 		return true;
14575 	return false;
14576 }
14577 
14578 static int bnxt_fw_init_one_p1(struct bnxt *bp)
14579 {
14580 	int rc;
14581 
14582 	bp->fw_cap = 0;
14583 	rc = bnxt_hwrm_ver_get(bp);
14584 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
14585 	 * so wait before continuing with recovery.
14586 	 */
14587 	if (rc)
14588 		msleep(100);
14589 	bnxt_try_map_fw_health_reg(bp);
14590 	if (rc) {
14591 		rc = bnxt_try_recover_fw(bp);
14592 		if (rc)
14593 			return rc;
14594 		rc = bnxt_hwrm_ver_get(bp);
14595 		if (rc)
14596 			return rc;
14597 	}
14598 
14599 	bnxt_nvm_cfg_ver_get(bp);
14600 
14601 	rc = bnxt_hwrm_func_reset(bp);
14602 	if (rc)
14603 		return -ENODEV;
14604 
14605 	bnxt_hwrm_fw_set_time(bp);
14606 	return 0;
14607 }
14608 
14609 static int bnxt_fw_init_one_p2(struct bnxt *bp)
14610 {
14611 	int rc;
14612 
14613 	/* Get the MAX capabilities for this function */
14614 	rc = bnxt_hwrm_func_qcaps(bp);
14615 	if (rc) {
14616 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
14617 			   rc);
14618 		return -ENODEV;
14619 	}
14620 
14621 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
14622 	if (rc)
14623 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
14624 			    rc);
14625 
14626 	if (bnxt_alloc_fw_health(bp)) {
14627 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
14628 	} else {
14629 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
14630 		if (rc)
14631 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
14632 				    rc);
14633 	}
14634 
14635 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
14636 	if (rc)
14637 		return -ENODEV;
14638 
14639 	rc = bnxt_alloc_crash_dump_mem(bp);
14640 	if (rc)
14641 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
14642 			    rc);
14643 	if (!rc) {
14644 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
14645 		if (rc) {
14646 			bnxt_free_crash_dump_mem(bp);
14647 			netdev_warn(bp->dev,
14648 				    "hwrm crash dump mem failure rc: %d\n", rc);
14649 		}
14650 	}
14651 
14652 	if (bnxt_fw_pre_resv_vnics(bp))
14653 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
14654 
14655 	bnxt_hwrm_func_qcfg(bp);
14656 	bnxt_hwrm_vnic_qcaps(bp);
14657 	bnxt_hwrm_port_led_qcaps(bp);
14658 	bnxt_ethtool_init(bp);
14659 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
14660 		__bnxt_hwrm_ptp_qcfg(bp);
14661 	bnxt_dcb_init(bp);
14662 	bnxt_hwmon_init(bp);
14663 	return 0;
14664 }
14665 
14666 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
14667 {
14668 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
14669 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
14670 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
14671 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
14672 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
14673 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
14674 		bp->rss_hash_delta = bp->rss_hash_cfg;
14675 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
14676 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
14677 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
14678 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
14679 	}
14680 }
14681 
14682 static void bnxt_set_dflt_rfs(struct bnxt *bp)
14683 {
14684 	struct net_device *dev = bp->dev;
14685 
14686 	dev->hw_features &= ~NETIF_F_NTUPLE;
14687 	dev->features &= ~NETIF_F_NTUPLE;
14688 	bp->flags &= ~BNXT_FLAG_RFS;
14689 	if (bnxt_rfs_supported(bp)) {
14690 		dev->hw_features |= NETIF_F_NTUPLE;
14691 		if (bnxt_rfs_capable(bp, false)) {
14692 			bp->flags |= BNXT_FLAG_RFS;
14693 			dev->features |= NETIF_F_NTUPLE;
14694 		}
14695 	}
14696 }
14697 
14698 static void bnxt_fw_init_one_p3(struct bnxt *bp)
14699 {
14700 	struct pci_dev *pdev = bp->pdev;
14701 
14702 	bnxt_set_dflt_rss_hash_type(bp);
14703 	bnxt_set_dflt_rfs(bp);
14704 
14705 	bnxt_get_wol_settings(bp);
14706 	if (bp->flags & BNXT_FLAG_WOL_CAP)
14707 		device_set_wakeup_enable(&pdev->dev, bp->wol);
14708 	else
14709 		device_set_wakeup_capable(&pdev->dev, false);
14710 
14711 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
14712 	bnxt_hwrm_coal_params_qcaps(bp);
14713 }
14714 
14715 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
14716 
14717 int bnxt_fw_init_one(struct bnxt *bp)
14718 {
14719 	int rc;
14720 
14721 	rc = bnxt_fw_init_one_p1(bp);
14722 	if (rc) {
14723 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
14724 		return rc;
14725 	}
14726 	rc = bnxt_fw_init_one_p2(bp);
14727 	if (rc) {
14728 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
14729 		return rc;
14730 	}
14731 	rc = bnxt_probe_phy(bp, false);
14732 	if (rc)
14733 		return rc;
14734 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
14735 	if (rc)
14736 		return rc;
14737 
14738 	bnxt_fw_init_one_p3(bp);
14739 	return 0;
14740 }
14741 
14742 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
14743 {
14744 	struct bnxt_fw_health *fw_health = bp->fw_health;
14745 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
14746 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
14747 	u32 reg_type, reg_off, delay_msecs;
14748 
14749 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
14750 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
14751 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
14752 	switch (reg_type) {
14753 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
14754 		pci_write_config_dword(bp->pdev, reg_off, val);
14755 		break;
14756 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
14757 		writel(reg_off & BNXT_GRC_BASE_MASK,
14758 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
14759 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
14760 		fallthrough;
14761 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
14762 		writel(val, bp->bar0 + reg_off);
14763 		break;
14764 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
14765 		writel(val, bp->bar1 + reg_off);
14766 		break;
14767 	}
14768 	if (delay_msecs) {
14769 		pci_read_config_dword(bp->pdev, 0, &val);
14770 		msleep(delay_msecs);
14771 	}
14772 }
14773 
14774 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
14775 {
14776 	struct hwrm_func_qcfg_output *resp;
14777 	struct hwrm_func_qcfg_input *req;
14778 	bool result = true; /* firmware will enforce if unknown */
14779 
14780 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
14781 		return result;
14782 
14783 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
14784 		return result;
14785 
14786 	req->fid = cpu_to_le16(0xffff);
14787 	resp = hwrm_req_hold(bp, req);
14788 	if (!hwrm_req_send(bp, req))
14789 		result = !!(le16_to_cpu(resp->flags) &
14790 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
14791 	hwrm_req_drop(bp, req);
14792 	return result;
14793 }
14794 
14795 static void bnxt_reset_all(struct bnxt *bp)
14796 {
14797 	struct bnxt_fw_health *fw_health = bp->fw_health;
14798 	int i, rc;
14799 
14800 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14801 		bnxt_fw_reset_via_optee(bp);
14802 		bp->fw_reset_timestamp = jiffies;
14803 		return;
14804 	}
14805 
14806 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
14807 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
14808 			bnxt_fw_reset_writel(bp, i);
14809 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
14810 		struct hwrm_fw_reset_input *req;
14811 
14812 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
14813 		if (!rc) {
14814 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
14815 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
14816 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
14817 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
14818 			rc = hwrm_req_send(bp, req);
14819 		}
14820 		if (rc != -ENODEV)
14821 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
14822 	}
14823 	bp->fw_reset_timestamp = jiffies;
14824 }
14825 
14826 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
14827 {
14828 	return time_after(jiffies, bp->fw_reset_timestamp +
14829 			  (bp->fw_reset_max_dsecs * HZ / 10));
14830 }
14831 
14832 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
14833 {
14834 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14835 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
14836 		bnxt_dl_health_fw_status_update(bp, false);
14837 	bp->fw_reset_state = 0;
14838 	netif_close(bp->dev);
14839 }
14840 
14841 static void bnxt_fw_reset_task(struct work_struct *work)
14842 {
14843 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
14844 	int rc = 0;
14845 
14846 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14847 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
14848 		return;
14849 	}
14850 
14851 	switch (bp->fw_reset_state) {
14852 	case BNXT_FW_RESET_STATE_POLL_VF: {
14853 		int n = bnxt_get_registered_vfs(bp);
14854 		int tmo;
14855 
14856 		if (n < 0) {
14857 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
14858 				   n, jiffies_to_msecs(jiffies -
14859 				   bp->fw_reset_timestamp));
14860 			goto fw_reset_abort;
14861 		} else if (n > 0) {
14862 			if (bnxt_fw_reset_timeout(bp)) {
14863 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14864 				bp->fw_reset_state = 0;
14865 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
14866 					   n);
14867 				goto ulp_start;
14868 			}
14869 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14870 			return;
14871 		}
14872 		bp->fw_reset_timestamp = jiffies;
14873 		netdev_lock(bp->dev);
14874 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
14875 			bnxt_fw_reset_abort(bp, rc);
14876 			netdev_unlock(bp->dev);
14877 			goto ulp_start;
14878 		}
14879 		bnxt_fw_reset_close(bp);
14880 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14881 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14882 			tmo = HZ / 10;
14883 		} else {
14884 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14885 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14886 		}
14887 		netdev_unlock(bp->dev);
14888 		bnxt_queue_fw_reset_work(bp, tmo);
14889 		return;
14890 	}
14891 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
14892 		u32 val;
14893 
14894 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14895 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
14896 		    !bnxt_fw_reset_timeout(bp)) {
14897 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14898 			return;
14899 		}
14900 
14901 		if (!bp->fw_health->primary) {
14902 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
14903 
14904 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14905 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14906 			return;
14907 		}
14908 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14909 	}
14910 		fallthrough;
14911 	case BNXT_FW_RESET_STATE_RESET_FW:
14912 		bnxt_reset_all(bp);
14913 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14914 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
14915 		return;
14916 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
14917 		bnxt_inv_fw_health_reg(bp);
14918 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
14919 		    !bp->fw_reset_min_dsecs) {
14920 			u16 val;
14921 
14922 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14923 			if (val == 0xffff) {
14924 				if (bnxt_fw_reset_timeout(bp)) {
14925 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
14926 					rc = -ETIMEDOUT;
14927 					goto fw_reset_abort;
14928 				}
14929 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
14930 				return;
14931 			}
14932 		}
14933 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14934 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
14935 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
14936 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
14937 			bnxt_dl_remote_reload(bp);
14938 		if (pci_enable_device(bp->pdev)) {
14939 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
14940 			rc = -ENODEV;
14941 			goto fw_reset_abort;
14942 		}
14943 		pci_set_master(bp->pdev);
14944 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
14945 		fallthrough;
14946 	case BNXT_FW_RESET_STATE_POLL_FW:
14947 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
14948 		rc = bnxt_hwrm_poll(bp);
14949 		if (rc) {
14950 			if (bnxt_fw_reset_timeout(bp)) {
14951 				netdev_err(bp->dev, "Firmware reset aborted\n");
14952 				goto fw_reset_abort_status;
14953 			}
14954 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14955 			return;
14956 		}
14957 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
14958 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
14959 		fallthrough;
14960 	case BNXT_FW_RESET_STATE_OPENING:
14961 		while (!netdev_trylock(bp->dev)) {
14962 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14963 			return;
14964 		}
14965 		rc = bnxt_open(bp->dev);
14966 		if (rc) {
14967 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
14968 			bnxt_fw_reset_abort(bp, rc);
14969 			netdev_unlock(bp->dev);
14970 			goto ulp_start;
14971 		}
14972 
14973 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
14974 		    bp->fw_health->enabled) {
14975 			bp->fw_health->last_fw_reset_cnt =
14976 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14977 		}
14978 		bp->fw_reset_state = 0;
14979 		/* Make sure fw_reset_state is 0 before clearing the flag */
14980 		smp_mb__before_atomic();
14981 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14982 		bnxt_ptp_reapply_pps(bp);
14983 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
14984 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
14985 			bnxt_dl_health_fw_recovery_done(bp);
14986 			bnxt_dl_health_fw_status_update(bp, true);
14987 		}
14988 		netdev_unlock(bp->dev);
14989 		bnxt_ulp_start(bp, 0);
14990 		bnxt_reenable_sriov(bp);
14991 		netdev_lock(bp->dev);
14992 		bnxt_vf_reps_alloc(bp);
14993 		bnxt_vf_reps_open(bp);
14994 		netdev_unlock(bp->dev);
14995 		break;
14996 	}
14997 	return;
14998 
14999 fw_reset_abort_status:
15000 	if (bp->fw_health->status_reliable ||
15001 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
15002 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15003 
15004 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
15005 	}
15006 fw_reset_abort:
15007 	netdev_lock(bp->dev);
15008 	bnxt_fw_reset_abort(bp, rc);
15009 	netdev_unlock(bp->dev);
15010 ulp_start:
15011 	bnxt_ulp_start(bp, rc);
15012 }
15013 
15014 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
15015 {
15016 	int rc;
15017 	struct bnxt *bp = netdev_priv(dev);
15018 
15019 	SET_NETDEV_DEV(dev, &pdev->dev);
15020 
15021 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
15022 	rc = pci_enable_device(pdev);
15023 	if (rc) {
15024 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
15025 		goto init_err;
15026 	}
15027 
15028 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
15029 		dev_err(&pdev->dev,
15030 			"Cannot find PCI device base address, aborting\n");
15031 		rc = -ENODEV;
15032 		goto init_err_disable;
15033 	}
15034 
15035 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
15036 	if (rc) {
15037 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
15038 		goto init_err_disable;
15039 	}
15040 
15041 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
15042 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
15043 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
15044 		rc = -EIO;
15045 		goto init_err_release;
15046 	}
15047 
15048 	pci_set_master(pdev);
15049 
15050 	bp->dev = dev;
15051 	bp->pdev = pdev;
15052 
15053 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
15054 	 * determines the BAR size.
15055 	 */
15056 	bp->bar0 = pci_ioremap_bar(pdev, 0);
15057 	if (!bp->bar0) {
15058 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
15059 		rc = -ENOMEM;
15060 		goto init_err_release;
15061 	}
15062 
15063 	bp->bar2 = pci_ioremap_bar(pdev, 4);
15064 	if (!bp->bar2) {
15065 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
15066 		rc = -ENOMEM;
15067 		goto init_err_release;
15068 	}
15069 
15070 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
15071 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
15072 
15073 	spin_lock_init(&bp->ntp_fltr_lock);
15074 #if BITS_PER_LONG == 32
15075 	spin_lock_init(&bp->db_lock);
15076 #endif
15077 
15078 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
15079 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
15080 
15081 	timer_setup(&bp->timer, bnxt_timer, 0);
15082 	bp->current_interval = BNXT_TIMER_INTERVAL;
15083 
15084 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
15085 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
15086 
15087 	clear_bit(BNXT_STATE_OPEN, &bp->state);
15088 	return 0;
15089 
15090 init_err_release:
15091 	bnxt_unmap_bars(bp, pdev);
15092 	pci_release_regions(pdev);
15093 
15094 init_err_disable:
15095 	pci_disable_device(pdev);
15096 
15097 init_err:
15098 	return rc;
15099 }
15100 
15101 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
15102 {
15103 	struct sockaddr *addr = p;
15104 	struct bnxt *bp = netdev_priv(dev);
15105 	int rc = 0;
15106 
15107 	netdev_assert_locked(dev);
15108 
15109 	if (!is_valid_ether_addr(addr->sa_data))
15110 		return -EADDRNOTAVAIL;
15111 
15112 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
15113 		return 0;
15114 
15115 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
15116 	if (rc)
15117 		return rc;
15118 
15119 	eth_hw_addr_set(dev, addr->sa_data);
15120 	bnxt_clear_usr_fltrs(bp, true);
15121 	if (netif_running(dev)) {
15122 		bnxt_close_nic(bp, false, false);
15123 		rc = bnxt_open_nic(bp, false, false);
15124 	}
15125 
15126 	return rc;
15127 }
15128 
15129 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
15130 {
15131 	struct bnxt *bp = netdev_priv(dev);
15132 
15133 	netdev_assert_locked(dev);
15134 
15135 	if (netif_running(dev))
15136 		bnxt_close_nic(bp, true, false);
15137 
15138 	WRITE_ONCE(dev->mtu, new_mtu);
15139 
15140 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
15141 	 * program is attached.  We need to set the AGG rings settings and
15142 	 * rx_skb_func accordingly.
15143 	 */
15144 	if (READ_ONCE(bp->xdp_prog))
15145 		bnxt_set_rx_skb_mode(bp, true);
15146 
15147 	bnxt_set_ring_params(bp);
15148 
15149 	if (netif_running(dev))
15150 		return bnxt_open_nic(bp, true, false);
15151 
15152 	return 0;
15153 }
15154 
15155 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
15156 {
15157 	struct bnxt *bp = netdev_priv(dev);
15158 	bool sh = false;
15159 	int rc, tx_cp;
15160 
15161 	if (tc > bp->max_tc) {
15162 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
15163 			   tc, bp->max_tc);
15164 		return -EINVAL;
15165 	}
15166 
15167 	if (bp->num_tc == tc)
15168 		return 0;
15169 
15170 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
15171 		sh = true;
15172 
15173 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
15174 			      sh, tc, bp->tx_nr_rings_xdp);
15175 	if (rc)
15176 		return rc;
15177 
15178 	/* Needs to close the device and do hw resource re-allocations */
15179 	if (netif_running(bp->dev))
15180 		bnxt_close_nic(bp, true, false);
15181 
15182 	if (tc) {
15183 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
15184 		netdev_set_num_tc(dev, tc);
15185 		bp->num_tc = tc;
15186 	} else {
15187 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15188 		netdev_reset_tc(dev);
15189 		bp->num_tc = 0;
15190 	}
15191 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
15192 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
15193 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
15194 			       tx_cp + bp->rx_nr_rings;
15195 
15196 	if (netif_running(bp->dev))
15197 		return bnxt_open_nic(bp, true, false);
15198 
15199 	return 0;
15200 }
15201 
15202 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
15203 				  void *cb_priv)
15204 {
15205 	struct bnxt *bp = cb_priv;
15206 
15207 	if (!bnxt_tc_flower_enabled(bp) ||
15208 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
15209 		return -EOPNOTSUPP;
15210 
15211 	switch (type) {
15212 	case TC_SETUP_CLSFLOWER:
15213 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
15214 	default:
15215 		return -EOPNOTSUPP;
15216 	}
15217 }
15218 
15219 LIST_HEAD(bnxt_block_cb_list);
15220 
15221 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
15222 			 void *type_data)
15223 {
15224 	struct bnxt *bp = netdev_priv(dev);
15225 
15226 	switch (type) {
15227 	case TC_SETUP_BLOCK:
15228 		return flow_block_cb_setup_simple(type_data,
15229 						  &bnxt_block_cb_list,
15230 						  bnxt_setup_tc_block_cb,
15231 						  bp, bp, true);
15232 	case TC_SETUP_QDISC_MQPRIO: {
15233 		struct tc_mqprio_qopt *mqprio = type_data;
15234 
15235 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
15236 
15237 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
15238 	}
15239 	default:
15240 		return -EOPNOTSUPP;
15241 	}
15242 }
15243 
15244 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
15245 			    const struct sk_buff *skb)
15246 {
15247 	struct bnxt_vnic_info *vnic;
15248 
15249 	if (skb)
15250 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
15251 
15252 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
15253 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
15254 }
15255 
15256 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
15257 			   u32 idx)
15258 {
15259 	struct hlist_head *head;
15260 	int bit_id;
15261 
15262 	spin_lock_bh(&bp->ntp_fltr_lock);
15263 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
15264 	if (bit_id < 0) {
15265 		spin_unlock_bh(&bp->ntp_fltr_lock);
15266 		return -ENOMEM;
15267 	}
15268 
15269 	fltr->base.sw_id = (u16)bit_id;
15270 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
15271 	fltr->base.flags |= BNXT_ACT_RING_DST;
15272 	head = &bp->ntp_fltr_hash_tbl[idx];
15273 	hlist_add_head_rcu(&fltr->base.hash, head);
15274 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
15275 	bnxt_insert_usr_fltr(bp, &fltr->base);
15276 	bp->ntp_fltr_count++;
15277 	spin_unlock_bh(&bp->ntp_fltr_lock);
15278 	return 0;
15279 }
15280 
15281 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
15282 			    struct bnxt_ntuple_filter *f2)
15283 {
15284 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
15285 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
15286 	struct flow_keys *keys1 = &f1->fkeys;
15287 	struct flow_keys *keys2 = &f2->fkeys;
15288 
15289 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
15290 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
15291 		return false;
15292 
15293 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
15294 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
15295 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
15296 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
15297 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
15298 			return false;
15299 	} else {
15300 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
15301 				     &keys2->addrs.v6addrs.src) ||
15302 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
15303 				     &masks2->addrs.v6addrs.src) ||
15304 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15305 				     &keys2->addrs.v6addrs.dst) ||
15306 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
15307 				     &masks2->addrs.v6addrs.dst))
15308 			return false;
15309 	}
15310 
15311 	return keys1->ports.src == keys2->ports.src &&
15312 	       masks1->ports.src == masks2->ports.src &&
15313 	       keys1->ports.dst == keys2->ports.dst &&
15314 	       masks1->ports.dst == masks2->ports.dst &&
15315 	       keys1->control.flags == keys2->control.flags &&
15316 	       f1->l2_fltr == f2->l2_fltr;
15317 }
15318 
15319 struct bnxt_ntuple_filter *
15320 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15321 				struct bnxt_ntuple_filter *fltr, u32 idx)
15322 {
15323 	struct bnxt_ntuple_filter *f;
15324 	struct hlist_head *head;
15325 
15326 	head = &bp->ntp_fltr_hash_tbl[idx];
15327 	hlist_for_each_entry_rcu(f, head, base.hash) {
15328 		if (bnxt_fltr_match(f, fltr))
15329 			return f;
15330 	}
15331 	return NULL;
15332 }
15333 
15334 #ifdef CONFIG_RFS_ACCEL
15335 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15336 			      u16 rxq_index, u32 flow_id)
15337 {
15338 	struct bnxt *bp = netdev_priv(dev);
15339 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15340 	struct flow_keys *fkeys;
15341 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15342 	struct bnxt_l2_filter *l2_fltr;
15343 	int rc = 0, idx;
15344 	u32 flags;
15345 
15346 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15347 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15348 		atomic_inc(&l2_fltr->refcnt);
15349 	} else {
15350 		struct bnxt_l2_key key;
15351 
15352 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15353 		key.vlan = 0;
15354 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15355 		if (!l2_fltr)
15356 			return -EINVAL;
15357 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15358 			bnxt_del_l2_filter(bp, l2_fltr);
15359 			return -EINVAL;
15360 		}
15361 	}
15362 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
15363 	if (!new_fltr) {
15364 		bnxt_del_l2_filter(bp, l2_fltr);
15365 		return -ENOMEM;
15366 	}
15367 
15368 	fkeys = &new_fltr->fkeys;
15369 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15370 		rc = -EPROTONOSUPPORT;
15371 		goto err_free;
15372 	}
15373 
15374 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15375 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15376 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15377 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15378 		rc = -EPROTONOSUPPORT;
15379 		goto err_free;
15380 	}
15381 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15382 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15383 		if (bp->hwrm_spec_code < 0x10601) {
15384 			rc = -EPROTONOSUPPORT;
15385 			goto err_free;
15386 		}
15387 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15388 	}
15389 	flags = fkeys->control.flags;
15390 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15391 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15392 		rc = -EPROTONOSUPPORT;
15393 		goto err_free;
15394 	}
15395 	new_fltr->l2_fltr = l2_fltr;
15396 
15397 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15398 	rcu_read_lock();
15399 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15400 	if (fltr) {
15401 		rc = fltr->base.sw_id;
15402 		rcu_read_unlock();
15403 		goto err_free;
15404 	}
15405 	rcu_read_unlock();
15406 
15407 	new_fltr->flow_id = flow_id;
15408 	new_fltr->base.rxq = rxq_index;
15409 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15410 	if (!rc) {
15411 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15412 		return new_fltr->base.sw_id;
15413 	}
15414 
15415 err_free:
15416 	bnxt_del_l2_filter(bp, l2_fltr);
15417 	kfree(new_fltr);
15418 	return rc;
15419 }
15420 #endif
15421 
15422 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
15423 {
15424 	spin_lock_bh(&bp->ntp_fltr_lock);
15425 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
15426 		spin_unlock_bh(&bp->ntp_fltr_lock);
15427 		return;
15428 	}
15429 	hlist_del_rcu(&fltr->base.hash);
15430 	bnxt_del_one_usr_fltr(bp, &fltr->base);
15431 	bp->ntp_fltr_count--;
15432 	spin_unlock_bh(&bp->ntp_fltr_lock);
15433 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
15434 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
15435 	kfree_rcu(fltr, base.rcu);
15436 }
15437 
15438 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
15439 {
15440 #ifdef CONFIG_RFS_ACCEL
15441 	int i;
15442 
15443 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
15444 		struct hlist_head *head;
15445 		struct hlist_node *tmp;
15446 		struct bnxt_ntuple_filter *fltr;
15447 		int rc;
15448 
15449 		head = &bp->ntp_fltr_hash_tbl[i];
15450 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
15451 			bool del = false;
15452 
15453 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
15454 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
15455 					continue;
15456 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
15457 							fltr->flow_id,
15458 							fltr->base.sw_id)) {
15459 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
15460 									 fltr);
15461 					del = true;
15462 				}
15463 			} else {
15464 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
15465 								       fltr);
15466 				if (rc)
15467 					del = true;
15468 				else
15469 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
15470 			}
15471 
15472 			if (del)
15473 				bnxt_del_ntp_filter(bp, fltr);
15474 		}
15475 	}
15476 #endif
15477 }
15478 
15479 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
15480 				    unsigned int entry, struct udp_tunnel_info *ti)
15481 {
15482 	struct bnxt *bp = netdev_priv(netdev);
15483 	unsigned int cmd;
15484 
15485 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15486 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
15487 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15488 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
15489 	else
15490 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
15491 
15492 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
15493 }
15494 
15495 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
15496 				      unsigned int entry, struct udp_tunnel_info *ti)
15497 {
15498 	struct bnxt *bp = netdev_priv(netdev);
15499 	unsigned int cmd;
15500 
15501 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15502 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
15503 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15504 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
15505 	else
15506 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
15507 
15508 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
15509 }
15510 
15511 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
15512 	.set_port	= bnxt_udp_tunnel_set_port,
15513 	.unset_port	= bnxt_udp_tunnel_unset_port,
15514 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
15515 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15516 	.tables		= {
15517 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15518 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15519 	},
15520 }, bnxt_udp_tunnels_p7 = {
15521 	.set_port	= bnxt_udp_tunnel_set_port,
15522 	.unset_port	= bnxt_udp_tunnel_unset_port,
15523 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
15524 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15525 	.tables		= {
15526 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15527 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15528 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
15529 	},
15530 };
15531 
15532 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
15533 			       struct net_device *dev, u32 filter_mask,
15534 			       int nlflags)
15535 {
15536 	struct bnxt *bp = netdev_priv(dev);
15537 
15538 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
15539 				       nlflags, filter_mask, NULL);
15540 }
15541 
15542 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
15543 			       u16 flags, struct netlink_ext_ack *extack)
15544 {
15545 	struct bnxt *bp = netdev_priv(dev);
15546 	struct nlattr *attr, *br_spec;
15547 	int rem, rc = 0;
15548 
15549 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
15550 		return -EOPNOTSUPP;
15551 
15552 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
15553 	if (!br_spec)
15554 		return -EINVAL;
15555 
15556 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
15557 		u16 mode;
15558 
15559 		mode = nla_get_u16(attr);
15560 		if (mode == bp->br_mode)
15561 			break;
15562 
15563 		rc = bnxt_hwrm_set_br_mode(bp, mode);
15564 		if (!rc)
15565 			bp->br_mode = mode;
15566 		break;
15567 	}
15568 	return rc;
15569 }
15570 
15571 int bnxt_get_port_parent_id(struct net_device *dev,
15572 			    struct netdev_phys_item_id *ppid)
15573 {
15574 	struct bnxt *bp = netdev_priv(dev);
15575 
15576 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
15577 		return -EOPNOTSUPP;
15578 
15579 	/* The PF and it's VF-reps only support the switchdev framework */
15580 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
15581 		return -EOPNOTSUPP;
15582 
15583 	ppid->id_len = sizeof(bp->dsn);
15584 	memcpy(ppid->id, bp->dsn, ppid->id_len);
15585 
15586 	return 0;
15587 }
15588 
15589 static const struct net_device_ops bnxt_netdev_ops = {
15590 	.ndo_open		= bnxt_open,
15591 	.ndo_start_xmit		= bnxt_start_xmit,
15592 	.ndo_stop		= bnxt_close,
15593 	.ndo_get_stats64	= bnxt_get_stats64,
15594 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
15595 	.ndo_eth_ioctl		= bnxt_ioctl,
15596 	.ndo_validate_addr	= eth_validate_addr,
15597 	.ndo_set_mac_address	= bnxt_change_mac_addr,
15598 	.ndo_change_mtu		= bnxt_change_mtu,
15599 	.ndo_fix_features	= bnxt_fix_features,
15600 	.ndo_set_features	= bnxt_set_features,
15601 	.ndo_features_check	= bnxt_features_check,
15602 	.ndo_tx_timeout		= bnxt_tx_timeout,
15603 #ifdef CONFIG_BNXT_SRIOV
15604 	.ndo_get_vf_config	= bnxt_get_vf_config,
15605 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
15606 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
15607 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
15608 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
15609 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
15610 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
15611 #endif
15612 	.ndo_setup_tc           = bnxt_setup_tc,
15613 #ifdef CONFIG_RFS_ACCEL
15614 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
15615 #endif
15616 	.ndo_bpf		= bnxt_xdp,
15617 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
15618 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
15619 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
15620 };
15621 
15622 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
15623 				    struct netdev_queue_stats_rx *stats)
15624 {
15625 	struct bnxt *bp = netdev_priv(dev);
15626 	struct bnxt_cp_ring_info *cpr;
15627 	u64 *sw;
15628 
15629 	if (!bp->bnapi)
15630 		return;
15631 
15632 	cpr = &bp->bnapi[i]->cp_ring;
15633 	sw = cpr->stats.sw_stats;
15634 
15635 	stats->packets = 0;
15636 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
15637 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
15638 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
15639 
15640 	stats->bytes = 0;
15641 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
15642 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
15643 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
15644 
15645 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
15646 }
15647 
15648 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
15649 				    struct netdev_queue_stats_tx *stats)
15650 {
15651 	struct bnxt *bp = netdev_priv(dev);
15652 	struct bnxt_napi *bnapi;
15653 	u64 *sw;
15654 
15655 	if (!bp->tx_ring)
15656 		return;
15657 
15658 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
15659 	sw = bnapi->cp_ring.stats.sw_stats;
15660 
15661 	stats->packets = 0;
15662 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
15663 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
15664 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
15665 
15666 	stats->bytes = 0;
15667 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
15668 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
15669 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
15670 }
15671 
15672 static void bnxt_get_base_stats(struct net_device *dev,
15673 				struct netdev_queue_stats_rx *rx,
15674 				struct netdev_queue_stats_tx *tx)
15675 {
15676 	struct bnxt *bp = netdev_priv(dev);
15677 
15678 	rx->packets = bp->net_stats_prev.rx_packets;
15679 	rx->bytes = bp->net_stats_prev.rx_bytes;
15680 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
15681 
15682 	tx->packets = bp->net_stats_prev.tx_packets;
15683 	tx->bytes = bp->net_stats_prev.tx_bytes;
15684 }
15685 
15686 static const struct netdev_stat_ops bnxt_stat_ops = {
15687 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
15688 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
15689 	.get_base_stats		= bnxt_get_base_stats,
15690 };
15691 
15692 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx)
15693 {
15694 	struct bnxt_rx_ring_info *rxr, *clone;
15695 	struct bnxt *bp = netdev_priv(dev);
15696 	struct bnxt_ring_struct *ring;
15697 	int rc;
15698 
15699 	if (!bp->rx_ring)
15700 		return -ENETDOWN;
15701 
15702 	rxr = &bp->rx_ring[idx];
15703 	clone = qmem;
15704 	memcpy(clone, rxr, sizeof(*rxr));
15705 	bnxt_init_rx_ring_struct(bp, clone);
15706 	bnxt_reset_rx_ring_struct(bp, clone);
15707 
15708 	clone->rx_prod = 0;
15709 	clone->rx_agg_prod = 0;
15710 	clone->rx_sw_agg_prod = 0;
15711 	clone->rx_next_cons = 0;
15712 
15713 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
15714 	if (rc)
15715 		return rc;
15716 
15717 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
15718 	if (rc < 0)
15719 		goto err_page_pool_destroy;
15720 
15721 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
15722 					MEM_TYPE_PAGE_POOL,
15723 					clone->page_pool);
15724 	if (rc)
15725 		goto err_rxq_info_unreg;
15726 
15727 	ring = &clone->rx_ring_struct;
15728 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15729 	if (rc)
15730 		goto err_free_rx_ring;
15731 
15732 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
15733 		ring = &clone->rx_agg_ring_struct;
15734 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15735 		if (rc)
15736 			goto err_free_rx_agg_ring;
15737 
15738 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
15739 		if (rc)
15740 			goto err_free_rx_agg_ring;
15741 	}
15742 
15743 	if (bp->flags & BNXT_FLAG_TPA) {
15744 		rc = bnxt_alloc_one_tpa_info(bp, clone);
15745 		if (rc)
15746 			goto err_free_tpa_info;
15747 	}
15748 
15749 	bnxt_init_one_rx_ring_rxbd(bp, clone);
15750 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
15751 
15752 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
15753 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15754 		bnxt_alloc_one_rx_ring_page(bp, clone, idx);
15755 	if (bp->flags & BNXT_FLAG_TPA)
15756 		bnxt_alloc_one_tpa_info_data(bp, clone);
15757 
15758 	return 0;
15759 
15760 err_free_tpa_info:
15761 	bnxt_free_one_tpa_info(bp, clone);
15762 err_free_rx_agg_ring:
15763 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
15764 err_free_rx_ring:
15765 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
15766 err_rxq_info_unreg:
15767 	xdp_rxq_info_unreg(&clone->xdp_rxq);
15768 err_page_pool_destroy:
15769 	page_pool_destroy(clone->page_pool);
15770 	if (bnxt_separate_head_pool())
15771 		page_pool_destroy(clone->head_pool);
15772 	clone->page_pool = NULL;
15773 	clone->head_pool = NULL;
15774 	return rc;
15775 }
15776 
15777 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
15778 {
15779 	struct bnxt_rx_ring_info *rxr = qmem;
15780 	struct bnxt *bp = netdev_priv(dev);
15781 	struct bnxt_ring_struct *ring;
15782 
15783 	bnxt_free_one_rx_ring_skbs(bp, rxr);
15784 	bnxt_free_one_tpa_info(bp, rxr);
15785 
15786 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
15787 
15788 	page_pool_destroy(rxr->page_pool);
15789 	if (bnxt_separate_head_pool())
15790 		page_pool_destroy(rxr->head_pool);
15791 	rxr->page_pool = NULL;
15792 	rxr->head_pool = NULL;
15793 
15794 	ring = &rxr->rx_ring_struct;
15795 	bnxt_free_ring(bp, &ring->ring_mem);
15796 
15797 	ring = &rxr->rx_agg_ring_struct;
15798 	bnxt_free_ring(bp, &ring->ring_mem);
15799 
15800 	kfree(rxr->rx_agg_bmap);
15801 	rxr->rx_agg_bmap = NULL;
15802 }
15803 
15804 static void bnxt_copy_rx_ring(struct bnxt *bp,
15805 			      struct bnxt_rx_ring_info *dst,
15806 			      struct bnxt_rx_ring_info *src)
15807 {
15808 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
15809 	struct bnxt_ring_struct *dst_ring, *src_ring;
15810 	int i;
15811 
15812 	dst_ring = &dst->rx_ring_struct;
15813 	dst_rmem = &dst_ring->ring_mem;
15814 	src_ring = &src->rx_ring_struct;
15815 	src_rmem = &src_ring->ring_mem;
15816 
15817 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15818 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15819 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15820 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15821 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15822 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15823 
15824 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15825 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15826 	*dst_rmem->vmem = *src_rmem->vmem;
15827 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15828 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15829 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15830 	}
15831 
15832 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
15833 		return;
15834 
15835 	dst_ring = &dst->rx_agg_ring_struct;
15836 	dst_rmem = &dst_ring->ring_mem;
15837 	src_ring = &src->rx_agg_ring_struct;
15838 	src_rmem = &src_ring->ring_mem;
15839 
15840 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15841 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15842 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15843 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15844 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15845 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15846 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
15847 
15848 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15849 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15850 	*dst_rmem->vmem = *src_rmem->vmem;
15851 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15852 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15853 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15854 	}
15855 
15856 	dst->rx_agg_bmap = src->rx_agg_bmap;
15857 }
15858 
15859 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx)
15860 {
15861 	struct bnxt *bp = netdev_priv(dev);
15862 	struct bnxt_rx_ring_info *rxr, *clone;
15863 	struct bnxt_cp_ring_info *cpr;
15864 	struct bnxt_vnic_info *vnic;
15865 	struct bnxt_napi *bnapi;
15866 	int i, rc;
15867 
15868 	rxr = &bp->rx_ring[idx];
15869 	clone = qmem;
15870 
15871 	rxr->rx_prod = clone->rx_prod;
15872 	rxr->rx_agg_prod = clone->rx_agg_prod;
15873 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
15874 	rxr->rx_next_cons = clone->rx_next_cons;
15875 	rxr->rx_tpa = clone->rx_tpa;
15876 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
15877 	rxr->page_pool = clone->page_pool;
15878 	rxr->head_pool = clone->head_pool;
15879 	rxr->xdp_rxq = clone->xdp_rxq;
15880 
15881 	bnxt_copy_rx_ring(bp, rxr, clone);
15882 
15883 	bnapi = rxr->bnapi;
15884 	cpr = &bnapi->cp_ring;
15885 
15886 	/* All rings have been reserved and previously allocated.
15887 	 * Reallocating with the same parameters should never fail.
15888 	 */
15889 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
15890 	if (rc)
15891 		goto err_reset;
15892 
15893 	if (bp->tph_mode) {
15894 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
15895 		if (rc)
15896 			goto err_reset;
15897 	}
15898 
15899 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
15900 	if (rc)
15901 		goto err_reset;
15902 
15903 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
15904 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15905 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
15906 
15907 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
15908 		rc = bnxt_tx_queue_start(bp, idx);
15909 		if (rc)
15910 			goto err_reset;
15911 	}
15912 
15913 	napi_enable_locked(&bnapi->napi);
15914 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
15915 
15916 	for (i = 0; i < bp->nr_vnics; i++) {
15917 		vnic = &bp->vnic_info[i];
15918 
15919 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
15920 		if (rc) {
15921 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
15922 				   vnic->vnic_id, rc);
15923 			return rc;
15924 		}
15925 		vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
15926 		bnxt_hwrm_vnic_update(bp, vnic,
15927 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15928 	}
15929 
15930 	return 0;
15931 
15932 err_reset:
15933 	netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n",
15934 		   rc);
15935 	napi_enable_locked(&bnapi->napi);
15936 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
15937 	bnxt_reset_task(bp, true);
15938 	return rc;
15939 }
15940 
15941 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
15942 {
15943 	struct bnxt *bp = netdev_priv(dev);
15944 	struct bnxt_rx_ring_info *rxr;
15945 	struct bnxt_cp_ring_info *cpr;
15946 	struct bnxt_vnic_info *vnic;
15947 	struct bnxt_napi *bnapi;
15948 	int i;
15949 
15950 	for (i = 0; i < bp->nr_vnics; i++) {
15951 		vnic = &bp->vnic_info[i];
15952 		vnic->mru = 0;
15953 		bnxt_hwrm_vnic_update(bp, vnic,
15954 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15955 	}
15956 	/* Make sure NAPI sees that the VNIC is disabled */
15957 	synchronize_net();
15958 	rxr = &bp->rx_ring[idx];
15959 	bnapi = rxr->bnapi;
15960 	cpr = &bnapi->cp_ring;
15961 	cancel_work_sync(&cpr->dim.work);
15962 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15963 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
15964 	page_pool_disable_direct_recycling(rxr->page_pool);
15965 	if (bnxt_separate_head_pool())
15966 		page_pool_disable_direct_recycling(rxr->head_pool);
15967 
15968 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
15969 		bnxt_tx_queue_stop(bp, idx);
15970 
15971 	/* Disable NAPI now after freeing the rings because HWRM_RING_FREE
15972 	 * completion is handled in NAPI to guarantee no more DMA on that ring
15973 	 * after seeing the completion.
15974 	 */
15975 	napi_disable_locked(&bnapi->napi);
15976 
15977 	if (bp->tph_mode) {
15978 		bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr);
15979 		bnxt_clear_one_cp_ring(bp, rxr->rx_cpr);
15980 	}
15981 	bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
15982 
15983 	memcpy(qmem, rxr, sizeof(*rxr));
15984 	bnxt_init_rx_ring_struct(bp, qmem);
15985 
15986 	return 0;
15987 }
15988 
15989 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
15990 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
15991 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
15992 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
15993 	.ndo_queue_start	= bnxt_queue_start,
15994 	.ndo_queue_stop		= bnxt_queue_stop,
15995 };
15996 
15997 static void bnxt_remove_one(struct pci_dev *pdev)
15998 {
15999 	struct net_device *dev = pci_get_drvdata(pdev);
16000 	struct bnxt *bp = netdev_priv(dev);
16001 
16002 	if (BNXT_PF(bp))
16003 		bnxt_sriov_disable(bp);
16004 
16005 	bnxt_rdma_aux_device_del(bp);
16006 
16007 	bnxt_ptp_clear(bp);
16008 	unregister_netdev(dev);
16009 
16010 	bnxt_rdma_aux_device_uninit(bp);
16011 
16012 	bnxt_free_l2_filters(bp, true);
16013 	bnxt_free_ntp_fltrs(bp, true);
16014 	WARN_ON(bp->num_rss_ctx);
16015 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16016 	/* Flush any pending tasks */
16017 	cancel_work_sync(&bp->sp_task);
16018 	cancel_delayed_work_sync(&bp->fw_reset_task);
16019 	bp->sp_event = 0;
16020 
16021 	bnxt_dl_fw_reporters_destroy(bp);
16022 	bnxt_dl_unregister(bp);
16023 	bnxt_shutdown_tc(bp);
16024 
16025 	bnxt_clear_int_mode(bp);
16026 	bnxt_hwrm_func_drv_unrgtr(bp);
16027 	bnxt_free_hwrm_resources(bp);
16028 	bnxt_hwmon_uninit(bp);
16029 	bnxt_ethtool_free(bp);
16030 	bnxt_dcb_free(bp);
16031 	kfree(bp->ptp_cfg);
16032 	bp->ptp_cfg = NULL;
16033 	kfree(bp->fw_health);
16034 	bp->fw_health = NULL;
16035 	bnxt_cleanup_pci(bp);
16036 	bnxt_free_ctx_mem(bp, true);
16037 	bnxt_free_crash_dump_mem(bp);
16038 	kfree(bp->rss_indir_tbl);
16039 	bp->rss_indir_tbl = NULL;
16040 	bnxt_free_port_stats(bp);
16041 	free_netdev(dev);
16042 }
16043 
16044 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
16045 {
16046 	int rc = 0;
16047 	struct bnxt_link_info *link_info = &bp->link_info;
16048 
16049 	bp->phy_flags = 0;
16050 	rc = bnxt_hwrm_phy_qcaps(bp);
16051 	if (rc) {
16052 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
16053 			   rc);
16054 		return rc;
16055 	}
16056 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
16057 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
16058 	else
16059 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
16060 
16061 	bp->mac_flags = 0;
16062 	bnxt_hwrm_mac_qcaps(bp);
16063 
16064 	if (!fw_dflt)
16065 		return 0;
16066 
16067 	mutex_lock(&bp->link_lock);
16068 	rc = bnxt_update_link(bp, false);
16069 	if (rc) {
16070 		mutex_unlock(&bp->link_lock);
16071 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
16072 			   rc);
16073 		return rc;
16074 	}
16075 
16076 	/* Older firmware does not have supported_auto_speeds, so assume
16077 	 * that all supported speeds can be autonegotiated.
16078 	 */
16079 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
16080 		link_info->support_auto_speeds = link_info->support_speeds;
16081 
16082 	bnxt_init_ethtool_link_settings(bp);
16083 	mutex_unlock(&bp->link_lock);
16084 	return 0;
16085 }
16086 
16087 static int bnxt_get_max_irq(struct pci_dev *pdev)
16088 {
16089 	u16 ctrl;
16090 
16091 	if (!pdev->msix_cap)
16092 		return 1;
16093 
16094 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
16095 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
16096 }
16097 
16098 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16099 				int *max_cp)
16100 {
16101 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
16102 	int max_ring_grps = 0, max_irq;
16103 
16104 	*max_tx = hw_resc->max_tx_rings;
16105 	*max_rx = hw_resc->max_rx_rings;
16106 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
16107 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
16108 			bnxt_get_ulp_msix_num_in_use(bp),
16109 			hw_resc->max_stat_ctxs -
16110 			bnxt_get_ulp_stat_ctxs_in_use(bp));
16111 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
16112 		*max_cp = min_t(int, *max_cp, max_irq);
16113 	max_ring_grps = hw_resc->max_hw_ring_grps;
16114 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
16115 		*max_cp -= 1;
16116 		*max_rx -= 2;
16117 	}
16118 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16119 		*max_rx >>= 1;
16120 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
16121 		int rc;
16122 
16123 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
16124 		if (rc) {
16125 			*max_rx = 0;
16126 			*max_tx = 0;
16127 		}
16128 		/* On P5 chips, max_cp output param should be available NQs */
16129 		*max_cp = max_irq;
16130 	}
16131 	*max_rx = min_t(int, *max_rx, max_ring_grps);
16132 }
16133 
16134 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
16135 {
16136 	int rx, tx, cp;
16137 
16138 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
16139 	*max_rx = rx;
16140 	*max_tx = tx;
16141 	if (!rx || !tx || !cp)
16142 		return -ENOMEM;
16143 
16144 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
16145 }
16146 
16147 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16148 			       bool shared)
16149 {
16150 	int rc;
16151 
16152 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16153 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
16154 		/* Not enough rings, try disabling agg rings. */
16155 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
16156 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16157 		if (rc) {
16158 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
16159 			bp->flags |= BNXT_FLAG_AGG_RINGS;
16160 			return rc;
16161 		}
16162 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
16163 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16164 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16165 		bnxt_set_ring_params(bp);
16166 	}
16167 
16168 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
16169 		int max_cp, max_stat, max_irq;
16170 
16171 		/* Reserve minimum resources for RoCE */
16172 		max_cp = bnxt_get_max_func_cp_rings(bp);
16173 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
16174 		max_irq = bnxt_get_max_func_irqs(bp);
16175 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
16176 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
16177 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
16178 			return 0;
16179 
16180 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
16181 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
16182 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
16183 		max_cp = min_t(int, max_cp, max_irq);
16184 		max_cp = min_t(int, max_cp, max_stat);
16185 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
16186 		if (rc)
16187 			rc = 0;
16188 	}
16189 	return rc;
16190 }
16191 
16192 /* In initial default shared ring setting, each shared ring must have a
16193  * RX/TX ring pair.
16194  */
16195 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
16196 {
16197 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
16198 	bp->rx_nr_rings = bp->cp_nr_rings;
16199 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
16200 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
16201 }
16202 
16203 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
16204 {
16205 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
16206 	int avail_msix;
16207 
16208 	if (!bnxt_can_reserve_rings(bp))
16209 		return 0;
16210 
16211 	if (sh)
16212 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
16213 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
16214 	/* Reduce default rings on multi-port cards so that total default
16215 	 * rings do not exceed CPU count.
16216 	 */
16217 	if (bp->port_count > 1) {
16218 		int max_rings =
16219 			max_t(int, num_online_cpus() / bp->port_count, 1);
16220 
16221 		dflt_rings = min_t(int, dflt_rings, max_rings);
16222 	}
16223 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
16224 	if (rc)
16225 		return rc;
16226 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
16227 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
16228 	if (sh)
16229 		bnxt_trim_dflt_sh_rings(bp);
16230 	else
16231 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
16232 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
16233 
16234 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
16235 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
16236 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
16237 
16238 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
16239 		bnxt_set_dflt_ulp_stat_ctxs(bp);
16240 	}
16241 
16242 	rc = __bnxt_reserve_rings(bp);
16243 	if (rc && rc != -ENODEV)
16244 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
16245 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16246 	if (sh)
16247 		bnxt_trim_dflt_sh_rings(bp);
16248 
16249 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
16250 	if (bnxt_need_reserve_rings(bp)) {
16251 		rc = __bnxt_reserve_rings(bp);
16252 		if (rc && rc != -ENODEV)
16253 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
16254 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16255 	}
16256 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
16257 		bp->rx_nr_rings++;
16258 		bp->cp_nr_rings++;
16259 	}
16260 	if (rc) {
16261 		bp->tx_nr_rings = 0;
16262 		bp->rx_nr_rings = 0;
16263 	}
16264 	return rc;
16265 }
16266 
16267 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
16268 {
16269 	int rc;
16270 
16271 	if (bp->tx_nr_rings)
16272 		return 0;
16273 
16274 	bnxt_ulp_irq_stop(bp);
16275 	bnxt_clear_int_mode(bp);
16276 	rc = bnxt_set_dflt_rings(bp, true);
16277 	if (rc) {
16278 		if (BNXT_VF(bp) && rc == -ENODEV)
16279 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16280 		else
16281 			netdev_err(bp->dev, "Not enough rings available.\n");
16282 		goto init_dflt_ring_err;
16283 	}
16284 	rc = bnxt_init_int_mode(bp);
16285 	if (rc)
16286 		goto init_dflt_ring_err;
16287 
16288 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16289 
16290 	bnxt_set_dflt_rfs(bp);
16291 
16292 init_dflt_ring_err:
16293 	bnxt_ulp_irq_restart(bp, rc);
16294 	return rc;
16295 }
16296 
16297 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
16298 {
16299 	int rc;
16300 
16301 	netdev_ops_assert_locked(bp->dev);
16302 	bnxt_hwrm_func_qcaps(bp);
16303 
16304 	if (netif_running(bp->dev))
16305 		__bnxt_close_nic(bp, true, false);
16306 
16307 	bnxt_ulp_irq_stop(bp);
16308 	bnxt_clear_int_mode(bp);
16309 	rc = bnxt_init_int_mode(bp);
16310 	bnxt_ulp_irq_restart(bp, rc);
16311 
16312 	if (netif_running(bp->dev)) {
16313 		if (rc)
16314 			netif_close(bp->dev);
16315 		else
16316 			rc = bnxt_open_nic(bp, true, false);
16317 	}
16318 
16319 	return rc;
16320 }
16321 
16322 static int bnxt_init_mac_addr(struct bnxt *bp)
16323 {
16324 	int rc = 0;
16325 
16326 	if (BNXT_PF(bp)) {
16327 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
16328 	} else {
16329 #ifdef CONFIG_BNXT_SRIOV
16330 		struct bnxt_vf_info *vf = &bp->vf;
16331 		bool strict_approval = true;
16332 
16333 		if (is_valid_ether_addr(vf->mac_addr)) {
16334 			/* overwrite netdev dev_addr with admin VF MAC */
16335 			eth_hw_addr_set(bp->dev, vf->mac_addr);
16336 			/* Older PF driver or firmware may not approve this
16337 			 * correctly.
16338 			 */
16339 			strict_approval = false;
16340 		} else {
16341 			eth_hw_addr_random(bp->dev);
16342 		}
16343 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
16344 #endif
16345 	}
16346 	return rc;
16347 }
16348 
16349 static void bnxt_vpd_read_info(struct bnxt *bp)
16350 {
16351 	struct pci_dev *pdev = bp->pdev;
16352 	unsigned int vpd_size, kw_len;
16353 	int pos, size;
16354 	u8 *vpd_data;
16355 
16356 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
16357 	if (IS_ERR(vpd_data)) {
16358 		pci_warn(pdev, "Unable to read VPD\n");
16359 		return;
16360 	}
16361 
16362 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16363 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
16364 	if (pos < 0)
16365 		goto read_sn;
16366 
16367 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16368 	memcpy(bp->board_partno, &vpd_data[pos], size);
16369 
16370 read_sn:
16371 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16372 					   PCI_VPD_RO_KEYWORD_SERIALNO,
16373 					   &kw_len);
16374 	if (pos < 0)
16375 		goto exit;
16376 
16377 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16378 	memcpy(bp->board_serialno, &vpd_data[pos], size);
16379 exit:
16380 	kfree(vpd_data);
16381 }
16382 
16383 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
16384 {
16385 	struct pci_dev *pdev = bp->pdev;
16386 	u64 qword;
16387 
16388 	qword = pci_get_dsn(pdev);
16389 	if (!qword) {
16390 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
16391 		return -EOPNOTSUPP;
16392 	}
16393 
16394 	put_unaligned_le64(qword, dsn);
16395 
16396 	bp->flags |= BNXT_FLAG_DSN_VALID;
16397 	return 0;
16398 }
16399 
16400 static int bnxt_map_db_bar(struct bnxt *bp)
16401 {
16402 	if (!bp->db_size)
16403 		return -ENODEV;
16404 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
16405 	if (!bp->bar1)
16406 		return -ENOMEM;
16407 	return 0;
16408 }
16409 
16410 void bnxt_print_device_info(struct bnxt *bp)
16411 {
16412 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
16413 		    board_info[bp->board_idx].name,
16414 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
16415 
16416 	pcie_print_link_status(bp->pdev);
16417 }
16418 
16419 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
16420 {
16421 	struct bnxt_hw_resc *hw_resc;
16422 	struct net_device *dev;
16423 	struct bnxt *bp;
16424 	int rc, max_irqs;
16425 
16426 	if (pci_is_bridge(pdev))
16427 		return -ENODEV;
16428 
16429 	if (!pdev->msix_cap) {
16430 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
16431 		return -ENODEV;
16432 	}
16433 
16434 	/* Clear any pending DMA transactions from crash kernel
16435 	 * while loading driver in capture kernel.
16436 	 */
16437 	if (is_kdump_kernel()) {
16438 		pci_clear_master(pdev);
16439 		pcie_flr(pdev);
16440 	}
16441 
16442 	max_irqs = bnxt_get_max_irq(pdev);
16443 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
16444 				 max_irqs);
16445 	if (!dev)
16446 		return -ENOMEM;
16447 
16448 	bp = netdev_priv(dev);
16449 	bp->board_idx = ent->driver_data;
16450 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
16451 	bnxt_set_max_func_irqs(bp, max_irqs);
16452 
16453 	if (bnxt_vf_pciid(bp->board_idx))
16454 		bp->flags |= BNXT_FLAG_VF;
16455 
16456 	/* No devlink port registration in case of a VF */
16457 	if (BNXT_PF(bp))
16458 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
16459 
16460 	rc = bnxt_init_board(pdev, dev);
16461 	if (rc < 0)
16462 		goto init_err_free;
16463 
16464 	dev->netdev_ops = &bnxt_netdev_ops;
16465 	dev->stat_ops = &bnxt_stat_ops;
16466 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
16467 	dev->ethtool_ops = &bnxt_ethtool_ops;
16468 	pci_set_drvdata(pdev, dev);
16469 
16470 	rc = bnxt_alloc_hwrm_resources(bp);
16471 	if (rc)
16472 		goto init_err_pci_clean;
16473 
16474 	mutex_init(&bp->hwrm_cmd_lock);
16475 	mutex_init(&bp->link_lock);
16476 
16477 	rc = bnxt_fw_init_one_p1(bp);
16478 	if (rc)
16479 		goto init_err_pci_clean;
16480 
16481 	if (BNXT_PF(bp))
16482 		bnxt_vpd_read_info(bp);
16483 
16484 	if (BNXT_CHIP_P5_PLUS(bp)) {
16485 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
16486 		if (BNXT_CHIP_P7(bp))
16487 			bp->flags |= BNXT_FLAG_CHIP_P7;
16488 	}
16489 
16490 	rc = bnxt_alloc_rss_indir_tbl(bp);
16491 	if (rc)
16492 		goto init_err_pci_clean;
16493 
16494 	rc = bnxt_fw_init_one_p2(bp);
16495 	if (rc)
16496 		goto init_err_pci_clean;
16497 
16498 	rc = bnxt_map_db_bar(bp);
16499 	if (rc) {
16500 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
16501 			rc);
16502 		goto init_err_pci_clean;
16503 	}
16504 
16505 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16506 			   NETIF_F_TSO | NETIF_F_TSO6 |
16507 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16508 			   NETIF_F_GSO_IPXIP4 |
16509 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16510 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
16511 			   NETIF_F_RXCSUM | NETIF_F_GRO;
16512 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16513 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
16514 
16515 	if (BNXT_SUPPORTS_TPA(bp))
16516 		dev->hw_features |= NETIF_F_LRO;
16517 
16518 	dev->hw_enc_features =
16519 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16520 			NETIF_F_TSO | NETIF_F_TSO6 |
16521 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16522 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16523 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
16524 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16525 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
16526 	if (bp->flags & BNXT_FLAG_CHIP_P7)
16527 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
16528 	else
16529 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
16530 
16531 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
16532 				    NETIF_F_GSO_GRE_CSUM;
16533 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
16534 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
16535 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
16536 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
16537 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
16538 	if (BNXT_SUPPORTS_TPA(bp))
16539 		dev->hw_features |= NETIF_F_GRO_HW;
16540 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
16541 	if (dev->features & NETIF_F_GRO_HW)
16542 		dev->features &= ~NETIF_F_LRO;
16543 	dev->priv_flags |= IFF_UNICAST_FLT;
16544 
16545 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
16546 	if (bp->tso_max_segs)
16547 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
16548 
16549 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
16550 			    NETDEV_XDP_ACT_RX_SG;
16551 
16552 #ifdef CONFIG_BNXT_SRIOV
16553 	init_waitqueue_head(&bp->sriov_cfg_wait);
16554 #endif
16555 	if (BNXT_SUPPORTS_TPA(bp)) {
16556 		bp->gro_func = bnxt_gro_func_5730x;
16557 		if (BNXT_CHIP_P4(bp))
16558 			bp->gro_func = bnxt_gro_func_5731x;
16559 		else if (BNXT_CHIP_P5_PLUS(bp))
16560 			bp->gro_func = bnxt_gro_func_5750x;
16561 	}
16562 	if (!BNXT_CHIP_P4_PLUS(bp))
16563 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
16564 
16565 	rc = bnxt_init_mac_addr(bp);
16566 	if (rc) {
16567 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
16568 		rc = -EADDRNOTAVAIL;
16569 		goto init_err_pci_clean;
16570 	}
16571 
16572 	if (BNXT_PF(bp)) {
16573 		/* Read the adapter's DSN to use as the eswitch switch_id */
16574 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
16575 	}
16576 
16577 	/* MTU range: 60 - FW defined max */
16578 	dev->min_mtu = ETH_ZLEN;
16579 	dev->max_mtu = bp->max_mtu;
16580 
16581 	rc = bnxt_probe_phy(bp, true);
16582 	if (rc)
16583 		goto init_err_pci_clean;
16584 
16585 	hw_resc = &bp->hw_resc;
16586 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
16587 		       BNXT_L2_FLTR_MAX_FLTR;
16588 	/* Older firmware may not report these filters properly */
16589 	if (bp->max_fltr < BNXT_MAX_FLTR)
16590 		bp->max_fltr = BNXT_MAX_FLTR;
16591 	bnxt_init_l2_fltr_tbl(bp);
16592 	__bnxt_set_rx_skb_mode(bp, false);
16593 	bnxt_set_tpa_flags(bp);
16594 	bnxt_init_ring_params(bp);
16595 	bnxt_set_ring_params(bp);
16596 	bnxt_rdma_aux_device_init(bp);
16597 	rc = bnxt_set_dflt_rings(bp, true);
16598 	if (rc) {
16599 		if (BNXT_VF(bp) && rc == -ENODEV) {
16600 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16601 		} else {
16602 			netdev_err(bp->dev, "Not enough rings available.\n");
16603 			rc = -ENOMEM;
16604 		}
16605 		goto init_err_pci_clean;
16606 	}
16607 
16608 	bnxt_fw_init_one_p3(bp);
16609 
16610 	bnxt_init_dflt_coal(bp);
16611 
16612 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
16613 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
16614 
16615 	rc = bnxt_init_int_mode(bp);
16616 	if (rc)
16617 		goto init_err_pci_clean;
16618 
16619 	/* No TC has been set yet and rings may have been trimmed due to
16620 	 * limited MSIX, so we re-initialize the TX rings per TC.
16621 	 */
16622 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16623 
16624 	if (BNXT_PF(bp)) {
16625 		if (!bnxt_pf_wq) {
16626 			bnxt_pf_wq =
16627 				create_singlethread_workqueue("bnxt_pf_wq");
16628 			if (!bnxt_pf_wq) {
16629 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
16630 				rc = -ENOMEM;
16631 				goto init_err_pci_clean;
16632 			}
16633 		}
16634 		rc = bnxt_init_tc(bp);
16635 		if (rc)
16636 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
16637 				   rc);
16638 	}
16639 
16640 	bnxt_inv_fw_health_reg(bp);
16641 	rc = bnxt_dl_register(bp);
16642 	if (rc)
16643 		goto init_err_dl;
16644 
16645 	INIT_LIST_HEAD(&bp->usr_fltr_list);
16646 
16647 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
16648 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
16649 	if (BNXT_SUPPORTS_QUEUE_API(bp))
16650 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
16651 	dev->request_ops_lock = true;
16652 
16653 	rc = register_netdev(dev);
16654 	if (rc)
16655 		goto init_err_cleanup;
16656 
16657 	bnxt_dl_fw_reporters_create(bp);
16658 
16659 	bnxt_rdma_aux_device_add(bp);
16660 
16661 	bnxt_print_device_info(bp);
16662 
16663 	pci_save_state(pdev);
16664 
16665 	return 0;
16666 init_err_cleanup:
16667 	bnxt_rdma_aux_device_uninit(bp);
16668 	bnxt_dl_unregister(bp);
16669 init_err_dl:
16670 	bnxt_shutdown_tc(bp);
16671 	bnxt_clear_int_mode(bp);
16672 
16673 init_err_pci_clean:
16674 	bnxt_hwrm_func_drv_unrgtr(bp);
16675 	bnxt_free_hwrm_resources(bp);
16676 	bnxt_hwmon_uninit(bp);
16677 	bnxt_ethtool_free(bp);
16678 	bnxt_ptp_clear(bp);
16679 	kfree(bp->ptp_cfg);
16680 	bp->ptp_cfg = NULL;
16681 	kfree(bp->fw_health);
16682 	bp->fw_health = NULL;
16683 	bnxt_cleanup_pci(bp);
16684 	bnxt_free_ctx_mem(bp, true);
16685 	bnxt_free_crash_dump_mem(bp);
16686 	kfree(bp->rss_indir_tbl);
16687 	bp->rss_indir_tbl = NULL;
16688 
16689 init_err_free:
16690 	free_netdev(dev);
16691 	return rc;
16692 }
16693 
16694 static void bnxt_shutdown(struct pci_dev *pdev)
16695 {
16696 	struct net_device *dev = pci_get_drvdata(pdev);
16697 	struct bnxt *bp;
16698 
16699 	if (!dev)
16700 		return;
16701 
16702 	rtnl_lock();
16703 	netdev_lock(dev);
16704 	bp = netdev_priv(dev);
16705 	if (!bp)
16706 		goto shutdown_exit;
16707 
16708 	if (netif_running(dev))
16709 		netif_close(dev);
16710 
16711 	bnxt_ptp_clear(bp);
16712 	bnxt_clear_int_mode(bp);
16713 	pci_disable_device(pdev);
16714 
16715 	if (system_state == SYSTEM_POWER_OFF) {
16716 		pci_wake_from_d3(pdev, bp->wol);
16717 		pci_set_power_state(pdev, PCI_D3hot);
16718 	}
16719 
16720 shutdown_exit:
16721 	netdev_unlock(dev);
16722 	rtnl_unlock();
16723 }
16724 
16725 #ifdef CONFIG_PM_SLEEP
16726 static int bnxt_suspend(struct device *device)
16727 {
16728 	struct net_device *dev = dev_get_drvdata(device);
16729 	struct bnxt *bp = netdev_priv(dev);
16730 	int rc = 0;
16731 
16732 	bnxt_ulp_stop(bp);
16733 
16734 	netdev_lock(dev);
16735 	if (netif_running(dev)) {
16736 		netif_device_detach(dev);
16737 		rc = bnxt_close(dev);
16738 	}
16739 	bnxt_hwrm_func_drv_unrgtr(bp);
16740 	bnxt_ptp_clear(bp);
16741 	pci_disable_device(bp->pdev);
16742 	bnxt_free_ctx_mem(bp, false);
16743 	netdev_unlock(dev);
16744 	return rc;
16745 }
16746 
16747 static int bnxt_resume(struct device *device)
16748 {
16749 	struct net_device *dev = dev_get_drvdata(device);
16750 	struct bnxt *bp = netdev_priv(dev);
16751 	int rc = 0;
16752 
16753 	netdev_lock(dev);
16754 	rc = pci_enable_device(bp->pdev);
16755 	if (rc) {
16756 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
16757 			   rc);
16758 		goto resume_exit;
16759 	}
16760 	pci_set_master(bp->pdev);
16761 	if (bnxt_hwrm_ver_get(bp)) {
16762 		rc = -ENODEV;
16763 		goto resume_exit;
16764 	}
16765 	rc = bnxt_hwrm_func_reset(bp);
16766 	if (rc) {
16767 		rc = -EBUSY;
16768 		goto resume_exit;
16769 	}
16770 
16771 	rc = bnxt_hwrm_func_qcaps(bp);
16772 	if (rc)
16773 		goto resume_exit;
16774 
16775 	bnxt_clear_reservations(bp, true);
16776 
16777 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
16778 		rc = -ENODEV;
16779 		goto resume_exit;
16780 	}
16781 	if (bp->fw_crash_mem)
16782 		bnxt_hwrm_crash_dump_mem_cfg(bp);
16783 
16784 	if (bnxt_ptp_init(bp)) {
16785 		kfree(bp->ptp_cfg);
16786 		bp->ptp_cfg = NULL;
16787 	}
16788 	bnxt_get_wol_settings(bp);
16789 	if (netif_running(dev)) {
16790 		rc = bnxt_open(dev);
16791 		if (!rc)
16792 			netif_device_attach(dev);
16793 	}
16794 
16795 resume_exit:
16796 	netdev_unlock(bp->dev);
16797 	bnxt_ulp_start(bp, rc);
16798 	if (!rc)
16799 		bnxt_reenable_sriov(bp);
16800 	return rc;
16801 }
16802 
16803 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
16804 #define BNXT_PM_OPS (&bnxt_pm_ops)
16805 
16806 #else
16807 
16808 #define BNXT_PM_OPS NULL
16809 
16810 #endif /* CONFIG_PM_SLEEP */
16811 
16812 /**
16813  * bnxt_io_error_detected - called when PCI error is detected
16814  * @pdev: Pointer to PCI device
16815  * @state: The current pci connection state
16816  *
16817  * This function is called after a PCI bus error affecting
16818  * this device has been detected.
16819  */
16820 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
16821 					       pci_channel_state_t state)
16822 {
16823 	struct net_device *netdev = pci_get_drvdata(pdev);
16824 	struct bnxt *bp = netdev_priv(netdev);
16825 	bool abort = false;
16826 
16827 	netdev_info(netdev, "PCI I/O error detected\n");
16828 
16829 	bnxt_ulp_stop(bp);
16830 
16831 	netdev_lock(netdev);
16832 	netif_device_detach(netdev);
16833 
16834 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
16835 		netdev_err(bp->dev, "Firmware reset already in progress\n");
16836 		abort = true;
16837 	}
16838 
16839 	if (abort || state == pci_channel_io_perm_failure) {
16840 		netdev_unlock(netdev);
16841 		return PCI_ERS_RESULT_DISCONNECT;
16842 	}
16843 
16844 	/* Link is not reliable anymore if state is pci_channel_io_frozen
16845 	 * so we disable bus master to prevent any potential bad DMAs before
16846 	 * freeing kernel memory.
16847 	 */
16848 	if (state == pci_channel_io_frozen) {
16849 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
16850 		bnxt_fw_fatal_close(bp);
16851 	}
16852 
16853 	if (netif_running(netdev))
16854 		__bnxt_close_nic(bp, true, true);
16855 
16856 	if (pci_is_enabled(pdev))
16857 		pci_disable_device(pdev);
16858 	bnxt_free_ctx_mem(bp, false);
16859 	netdev_unlock(netdev);
16860 
16861 	/* Request a slot slot reset. */
16862 	return PCI_ERS_RESULT_NEED_RESET;
16863 }
16864 
16865 /**
16866  * bnxt_io_slot_reset - called after the pci bus has been reset.
16867  * @pdev: Pointer to PCI device
16868  *
16869  * Restart the card from scratch, as if from a cold-boot.
16870  * At this point, the card has experienced a hard reset,
16871  * followed by fixups by BIOS, and has its config space
16872  * set up identically to what it was at cold boot.
16873  */
16874 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
16875 {
16876 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
16877 	struct net_device *netdev = pci_get_drvdata(pdev);
16878 	struct bnxt *bp = netdev_priv(netdev);
16879 	int retry = 0;
16880 	int err = 0;
16881 	int off;
16882 
16883 	netdev_info(bp->dev, "PCI Slot Reset\n");
16884 
16885 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16886 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
16887 		msleep(900);
16888 
16889 	netdev_lock(netdev);
16890 
16891 	if (pci_enable_device(pdev)) {
16892 		dev_err(&pdev->dev,
16893 			"Cannot re-enable PCI device after reset.\n");
16894 	} else {
16895 		pci_set_master(pdev);
16896 		/* Upon fatal error, our device internal logic that latches to
16897 		 * BAR value is getting reset and will restore only upon
16898 		 * rewriting the BARs.
16899 		 *
16900 		 * As pci_restore_state() does not re-write the BARs if the
16901 		 * value is same as saved value earlier, driver needs to
16902 		 * write the BARs to 0 to force restore, in case of fatal error.
16903 		 */
16904 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
16905 				       &bp->state)) {
16906 			for (off = PCI_BASE_ADDRESS_0;
16907 			     off <= PCI_BASE_ADDRESS_5; off += 4)
16908 				pci_write_config_dword(bp->pdev, off, 0);
16909 		}
16910 		pci_restore_state(pdev);
16911 		pci_save_state(pdev);
16912 
16913 		bnxt_inv_fw_health_reg(bp);
16914 		bnxt_try_map_fw_health_reg(bp);
16915 
16916 		/* In some PCIe AER scenarios, firmware may take up to
16917 		 * 10 seconds to become ready in the worst case.
16918 		 */
16919 		do {
16920 			err = bnxt_try_recover_fw(bp);
16921 			if (!err)
16922 				break;
16923 			retry++;
16924 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
16925 
16926 		if (err) {
16927 			dev_err(&pdev->dev, "Firmware not ready\n");
16928 			goto reset_exit;
16929 		}
16930 
16931 		err = bnxt_hwrm_func_reset(bp);
16932 		if (!err)
16933 			result = PCI_ERS_RESULT_RECOVERED;
16934 
16935 		bnxt_ulp_irq_stop(bp);
16936 		bnxt_clear_int_mode(bp);
16937 		err = bnxt_init_int_mode(bp);
16938 		bnxt_ulp_irq_restart(bp, err);
16939 	}
16940 
16941 reset_exit:
16942 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16943 	bnxt_clear_reservations(bp, true);
16944 	netdev_unlock(netdev);
16945 
16946 	return result;
16947 }
16948 
16949 /**
16950  * bnxt_io_resume - called when traffic can start flowing again.
16951  * @pdev: Pointer to PCI device
16952  *
16953  * This callback is called when the error recovery driver tells
16954  * us that its OK to resume normal operation.
16955  */
16956 static void bnxt_io_resume(struct pci_dev *pdev)
16957 {
16958 	struct net_device *netdev = pci_get_drvdata(pdev);
16959 	struct bnxt *bp = netdev_priv(netdev);
16960 	int err;
16961 
16962 	netdev_info(bp->dev, "PCI Slot Resume\n");
16963 	netdev_lock(netdev);
16964 
16965 	err = bnxt_hwrm_func_qcaps(bp);
16966 	if (!err) {
16967 		if (netif_running(netdev))
16968 			err = bnxt_open(netdev);
16969 		else
16970 			err = bnxt_reserve_rings(bp, true);
16971 	}
16972 
16973 	if (!err)
16974 		netif_device_attach(netdev);
16975 
16976 	netdev_unlock(netdev);
16977 	bnxt_ulp_start(bp, err);
16978 	if (!err)
16979 		bnxt_reenable_sriov(bp);
16980 }
16981 
16982 static const struct pci_error_handlers bnxt_err_handler = {
16983 	.error_detected	= bnxt_io_error_detected,
16984 	.slot_reset	= bnxt_io_slot_reset,
16985 	.resume		= bnxt_io_resume
16986 };
16987 
16988 static struct pci_driver bnxt_pci_driver = {
16989 	.name		= DRV_MODULE_NAME,
16990 	.id_table	= bnxt_pci_tbl,
16991 	.probe		= bnxt_init_one,
16992 	.remove		= bnxt_remove_one,
16993 	.shutdown	= bnxt_shutdown,
16994 	.driver.pm	= BNXT_PM_OPS,
16995 	.err_handler	= &bnxt_err_handler,
16996 #if defined(CONFIG_BNXT_SRIOV)
16997 	.sriov_configure = bnxt_sriov_configure,
16998 #endif
16999 };
17000 
17001 static int __init bnxt_init(void)
17002 {
17003 	int err;
17004 
17005 	bnxt_debug_init();
17006 	err = pci_register_driver(&bnxt_pci_driver);
17007 	if (err) {
17008 		bnxt_debug_exit();
17009 		return err;
17010 	}
17011 
17012 	return 0;
17013 }
17014 
17015 static void __exit bnxt_exit(void)
17016 {
17017 	pci_unregister_driver(&bnxt_pci_driver);
17018 	if (bnxt_pf_wq)
17019 		destroy_workqueue(bnxt_pf_wq);
17020 	bnxt_debug_exit();
17021 }
17022 
17023 module_init(bnxt_init);
17024 module_exit(bnxt_exit);
17025