xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision 1cc3462159babb69c84c39cb1b4e262aef3ea325)
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_queues.h>
58 #include <net/netdev_rx_queue.h>
59 #include <linux/pci-tph.h>
60 
61 #include "bnxt_hsi.h"
62 #include "bnxt.h"
63 #include "bnxt_hwrm.h"
64 #include "bnxt_ulp.h"
65 #include "bnxt_sriov.h"
66 #include "bnxt_ethtool.h"
67 #include "bnxt_dcb.h"
68 #include "bnxt_xdp.h"
69 #include "bnxt_ptp.h"
70 #include "bnxt_vfr.h"
71 #include "bnxt_tc.h"
72 #include "bnxt_devlink.h"
73 #include "bnxt_debugfs.h"
74 #include "bnxt_coredump.h"
75 #include "bnxt_hwmon.h"
76 
77 #define BNXT_TX_TIMEOUT		(5 * HZ)
78 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
79 				 NETIF_MSG_TX_ERR)
80 
81 MODULE_IMPORT_NS("NETDEV_INTERNAL");
82 MODULE_LICENSE("GPL");
83 MODULE_DESCRIPTION("Broadcom NetXtreme network driver");
84 
85 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
86 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
87 
88 #define BNXT_TX_PUSH_THRESH 164
89 
90 /* indexed by enum board_idx */
91 static const struct {
92 	char *name;
93 } board_info[] = {
94 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
95 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
96 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
97 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
98 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
99 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
100 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
101 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
102 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
103 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
104 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
105 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
106 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
107 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
108 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
109 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
110 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
111 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
112 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
113 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
114 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
115 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
116 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
117 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
118 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
119 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
120 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
121 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
122 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
123 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
124 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
125 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
126 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
127 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
128 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
129 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
130 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
131 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
132 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
133 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
134 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
135 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
136 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
137 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
138 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
139 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
140 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
141 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
142 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
143 	[NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" },
144 };
145 
146 static const struct pci_device_id bnxt_pci_tbl[] = {
147 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
148 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
149 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
150 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
151 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
152 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
153 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
155 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
159 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
160 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
164 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
165 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
166 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
167 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
168 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
169 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
170 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
171 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
172 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
173 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
174 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
175 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
176 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
178 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
179 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
180 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
181 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
182 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
183 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
184 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
185 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
187 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
188 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
189 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
190 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
191 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
192 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
193 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
194 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
195 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
196 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
197 #ifdef CONFIG_BNXT_SRIOV
198 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
199 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
200 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
201 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
202 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
203 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
204 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
205 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
206 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
207 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
208 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
209 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
210 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
211 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
212 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
214 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
215 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
216 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
217 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
218 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
219 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
220 #endif
221 	{ 0 }
222 };
223 
224 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
225 
226 static const u16 bnxt_vf_req_snif[] = {
227 	HWRM_FUNC_CFG,
228 	HWRM_FUNC_VF_CFG,
229 	HWRM_PORT_PHY_QCFG,
230 	HWRM_CFA_L2_FILTER_ALLOC,
231 };
232 
233 static const u16 bnxt_async_events_arr[] = {
234 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
235 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
236 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
237 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
238 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
239 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
240 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
241 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
242 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
243 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
244 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
245 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
246 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
247 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
248 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
249 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
250 	ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER,
251 };
252 
253 const u16 bnxt_bstore_to_trace[] = {
254 	[BNXT_CTX_SRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT_TRACE,
255 	[BNXT_CTX_SRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT2_TRACE,
256 	[BNXT_CTX_CRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT_TRACE,
257 	[BNXT_CTX_CRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT2_TRACE,
258 	[BNXT_CTX_RIGP0]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP0_TRACE,
259 	[BNXT_CTX_L2HWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_L2_HWRM_TRACE,
260 	[BNXT_CTX_REHWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ROCE_HWRM_TRACE,
261 	[BNXT_CTX_CA0]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA0_TRACE,
262 	[BNXT_CTX_CA1]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA1_TRACE,
263 	[BNXT_CTX_CA2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA2_TRACE,
264 	[BNXT_CTX_RIGP1]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP1_TRACE,
265 };
266 
267 static struct workqueue_struct *bnxt_pf_wq;
268 
269 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
270 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
271 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
272 
273 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
274 	.ports = {
275 		.src = 0,
276 		.dst = 0,
277 	},
278 	.addrs = {
279 		.v6addrs = {
280 			.src = BNXT_IPV6_MASK_NONE,
281 			.dst = BNXT_IPV6_MASK_NONE,
282 		},
283 	},
284 };
285 
286 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
287 	.ports = {
288 		.src = cpu_to_be16(0xffff),
289 		.dst = cpu_to_be16(0xffff),
290 	},
291 	.addrs = {
292 		.v6addrs = {
293 			.src = BNXT_IPV6_MASK_ALL,
294 			.dst = BNXT_IPV6_MASK_ALL,
295 		},
296 	},
297 };
298 
299 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
300 	.ports = {
301 		.src = cpu_to_be16(0xffff),
302 		.dst = cpu_to_be16(0xffff),
303 	},
304 	.addrs = {
305 		.v4addrs = {
306 			.src = cpu_to_be32(0xffffffff),
307 			.dst = cpu_to_be32(0xffffffff),
308 		},
309 	},
310 };
311 
312 static bool bnxt_vf_pciid(enum board_idx idx)
313 {
314 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
315 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
316 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
317 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF);
318 }
319 
320 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
321 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
322 
323 #define BNXT_DB_CQ(db, idx)						\
324 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
325 
326 #define BNXT_DB_NQ_P5(db, idx)						\
327 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
328 		    (db)->doorbell)
329 
330 #define BNXT_DB_NQ_P7(db, idx)						\
331 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
332 		    DB_RING_IDX(db, idx), (db)->doorbell)
333 
334 #define BNXT_DB_CQ_ARM(db, idx)						\
335 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
336 
337 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
338 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
339 		    DB_RING_IDX(db, idx), (db)->doorbell)
340 
341 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
342 {
343 	if (bp->flags & BNXT_FLAG_CHIP_P7)
344 		BNXT_DB_NQ_P7(db, idx);
345 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
346 		BNXT_DB_NQ_P5(db, idx);
347 	else
348 		BNXT_DB_CQ(db, idx);
349 }
350 
351 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
352 {
353 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
354 		BNXT_DB_NQ_ARM_P5(db, idx);
355 	else
356 		BNXT_DB_CQ_ARM(db, idx);
357 }
358 
359 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
360 {
361 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
362 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
363 			    DB_RING_IDX(db, idx), db->doorbell);
364 	else
365 		BNXT_DB_CQ(db, idx);
366 }
367 
368 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
369 {
370 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
371 		return;
372 
373 	if (BNXT_PF(bp))
374 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
375 	else
376 		schedule_delayed_work(&bp->fw_reset_task, delay);
377 }
378 
379 static void __bnxt_queue_sp_work(struct bnxt *bp)
380 {
381 	if (BNXT_PF(bp))
382 		queue_work(bnxt_pf_wq, &bp->sp_task);
383 	else
384 		schedule_work(&bp->sp_task);
385 }
386 
387 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
388 {
389 	set_bit(event, &bp->sp_event);
390 	__bnxt_queue_sp_work(bp);
391 }
392 
393 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
394 {
395 	if (!rxr->bnapi->in_reset) {
396 		rxr->bnapi->in_reset = true;
397 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
398 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
399 		else
400 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
401 		__bnxt_queue_sp_work(bp);
402 	}
403 	rxr->rx_next_cons = 0xffff;
404 }
405 
406 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
407 			  u16 curr)
408 {
409 	struct bnxt_napi *bnapi = txr->bnapi;
410 
411 	if (bnapi->tx_fault)
412 		return;
413 
414 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
415 		   txr->txq_index, txr->tx_hw_cons,
416 		   txr->tx_cons, txr->tx_prod, curr);
417 	WARN_ON_ONCE(1);
418 	bnapi->tx_fault = 1;
419 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
420 }
421 
422 const u16 bnxt_lhint_arr[] = {
423 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
424 	TX_BD_FLAGS_LHINT_512_TO_1023,
425 	TX_BD_FLAGS_LHINT_1024_TO_2047,
426 	TX_BD_FLAGS_LHINT_1024_TO_2047,
427 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
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 };
443 
444 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
445 {
446 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
447 
448 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
449 		return 0;
450 
451 	return md_dst->u.port_info.port_id;
452 }
453 
454 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
455 			     u16 prod)
456 {
457 	/* Sync BD data before updating doorbell */
458 	wmb();
459 	bnxt_db_write(bp, &txr->tx_db, prod);
460 	txr->kick_pending = 0;
461 }
462 
463 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
464 {
465 	struct bnxt *bp = netdev_priv(dev);
466 	struct tx_bd *txbd, *txbd0;
467 	struct tx_bd_ext *txbd1;
468 	struct netdev_queue *txq;
469 	int i;
470 	dma_addr_t mapping;
471 	unsigned int length, pad = 0;
472 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
473 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
474 	struct pci_dev *pdev = bp->pdev;
475 	u16 prod, last_frag, txts_prod;
476 	struct bnxt_tx_ring_info *txr;
477 	struct bnxt_sw_tx_bd *tx_buf;
478 	__le32 lflags = 0;
479 
480 	i = skb_get_queue_mapping(skb);
481 	if (unlikely(i >= bp->tx_nr_rings)) {
482 		dev_kfree_skb_any(skb);
483 		dev_core_stats_tx_dropped_inc(dev);
484 		return NETDEV_TX_OK;
485 	}
486 
487 	txq = netdev_get_tx_queue(dev, i);
488 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
489 	prod = txr->tx_prod;
490 
491 	free_size = bnxt_tx_avail(bp, txr);
492 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
493 		/* We must have raced with NAPI cleanup */
494 		if (net_ratelimit() && txr->kick_pending)
495 			netif_warn(bp, tx_err, dev,
496 				   "bnxt: ring busy w/ flush pending!\n");
497 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
498 					bp->tx_wake_thresh))
499 			return NETDEV_TX_BUSY;
500 	}
501 
502 	if (unlikely(ipv6_hopopt_jumbo_remove(skb)))
503 		goto tx_free;
504 
505 	length = skb->len;
506 	len = skb_headlen(skb);
507 	last_frag = skb_shinfo(skb)->nr_frags;
508 
509 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
510 
511 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
512 	tx_buf->skb = skb;
513 	tx_buf->nr_frags = last_frag;
514 
515 	vlan_tag_flags = 0;
516 	cfa_action = bnxt_xmit_get_cfa_action(skb);
517 	if (skb_vlan_tag_present(skb)) {
518 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
519 				 skb_vlan_tag_get(skb);
520 		/* Currently supports 8021Q, 8021AD vlan offloads
521 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
522 		 */
523 		if (skb->vlan_proto == htons(ETH_P_8021Q))
524 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
525 	}
526 
527 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp &&
528 	    ptp->tx_tstamp_en) {
529 		if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) {
530 			lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
531 			tx_buf->is_ts_pkt = 1;
532 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
533 		} else if (!skb_is_gso(skb)) {
534 			u16 seq_id, hdr_off;
535 
536 			if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) &&
537 			    !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) {
538 				if (vlan_tag_flags)
539 					hdr_off += VLAN_HLEN;
540 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
541 				tx_buf->is_ts_pkt = 1;
542 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
543 
544 				ptp->txts_req[txts_prod].tx_seqid = seq_id;
545 				ptp->txts_req[txts_prod].tx_hdr_off = hdr_off;
546 				tx_buf->txts_prod = txts_prod;
547 			}
548 		}
549 	}
550 	if (unlikely(skb->no_fcs))
551 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
552 
553 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
554 	    !lflags) {
555 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
556 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
557 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
558 		void __iomem *db = txr->tx_db.doorbell;
559 		void *pdata = tx_push_buf->data;
560 		u64 *end;
561 		int j, push_len;
562 
563 		/* Set COAL_NOW to be ready quickly for the next push */
564 		tx_push->tx_bd_len_flags_type =
565 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
566 					TX_BD_TYPE_LONG_TX_BD |
567 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
568 					TX_BD_FLAGS_COAL_NOW |
569 					TX_BD_FLAGS_PACKET_END |
570 					(2 << TX_BD_FLAGS_BD_CNT_SHIFT));
571 
572 		if (skb->ip_summed == CHECKSUM_PARTIAL)
573 			tx_push1->tx_bd_hsize_lflags =
574 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
575 		else
576 			tx_push1->tx_bd_hsize_lflags = 0;
577 
578 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
579 		tx_push1->tx_bd_cfa_action =
580 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
581 
582 		end = pdata + length;
583 		end = PTR_ALIGN(end, 8) - 1;
584 		*end = 0;
585 
586 		skb_copy_from_linear_data(skb, pdata, len);
587 		pdata += len;
588 		for (j = 0; j < last_frag; j++) {
589 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
590 			void *fptr;
591 
592 			fptr = skb_frag_address_safe(frag);
593 			if (!fptr)
594 				goto normal_tx;
595 
596 			memcpy(pdata, fptr, skb_frag_size(frag));
597 			pdata += skb_frag_size(frag);
598 		}
599 
600 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
601 		txbd->tx_bd_haddr = txr->data_mapping;
602 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
603 		prod = NEXT_TX(prod);
604 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
605 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
606 		memcpy(txbd, tx_push1, sizeof(*txbd));
607 		prod = NEXT_TX(prod);
608 		tx_push->doorbell =
609 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
610 				    DB_RING_IDX(&txr->tx_db, prod));
611 		WRITE_ONCE(txr->tx_prod, prod);
612 
613 		tx_buf->is_push = 1;
614 		netdev_tx_sent_queue(txq, skb->len);
615 		wmb();	/* Sync is_push and byte queue before pushing data */
616 
617 		push_len = (length + sizeof(*tx_push) + 7) / 8;
618 		if (push_len > 16) {
619 			__iowrite64_copy(db, tx_push_buf, 16);
620 			__iowrite32_copy(db + 4, tx_push_buf + 1,
621 					 (push_len - 16) << 1);
622 		} else {
623 			__iowrite64_copy(db, tx_push_buf, push_len);
624 		}
625 
626 		goto tx_done;
627 	}
628 
629 normal_tx:
630 	if (length < BNXT_MIN_PKT_SIZE) {
631 		pad = BNXT_MIN_PKT_SIZE - length;
632 		if (skb_pad(skb, pad))
633 			/* SKB already freed. */
634 			goto tx_kick_pending;
635 		length = BNXT_MIN_PKT_SIZE;
636 	}
637 
638 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
639 
640 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
641 		goto tx_free;
642 
643 	dma_unmap_addr_set(tx_buf, mapping, mapping);
644 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
645 		((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
646 
647 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
648 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
649 
650 	prod = NEXT_TX(prod);
651 	txbd1 = (struct tx_bd_ext *)
652 		&txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
653 
654 	txbd1->tx_bd_hsize_lflags = lflags;
655 	if (skb_is_gso(skb)) {
656 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
657 		u32 hdr_len;
658 
659 		if (skb->encapsulation) {
660 			if (udp_gso)
661 				hdr_len = skb_inner_transport_offset(skb) +
662 					  sizeof(struct udphdr);
663 			else
664 				hdr_len = skb_inner_tcp_all_headers(skb);
665 		} else if (udp_gso) {
666 			hdr_len = skb_transport_offset(skb) +
667 				  sizeof(struct udphdr);
668 		} else {
669 			hdr_len = skb_tcp_all_headers(skb);
670 		}
671 
672 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
673 					TX_BD_FLAGS_T_IPID |
674 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
675 		length = skb_shinfo(skb)->gso_size;
676 		txbd1->tx_bd_mss = cpu_to_le32(length);
677 		length += hdr_len;
678 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
679 		txbd1->tx_bd_hsize_lflags |=
680 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
681 		txbd1->tx_bd_mss = 0;
682 	}
683 
684 	length >>= 9;
685 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
686 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
687 				     skb->len);
688 		i = 0;
689 		goto tx_dma_error;
690 	}
691 	flags |= bnxt_lhint_arr[length];
692 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
693 
694 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
695 	txbd1->tx_bd_cfa_action =
696 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
697 	txbd0 = txbd;
698 	for (i = 0; i < last_frag; i++) {
699 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
700 
701 		prod = NEXT_TX(prod);
702 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
703 
704 		len = skb_frag_size(frag);
705 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
706 					   DMA_TO_DEVICE);
707 
708 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
709 			goto tx_dma_error;
710 
711 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
712 		dma_unmap_addr_set(tx_buf, mapping, mapping);
713 
714 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
715 
716 		flags = len << TX_BD_LEN_SHIFT;
717 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
718 	}
719 
720 	flags &= ~TX_BD_LEN;
721 	txbd->tx_bd_len_flags_type =
722 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
723 			    TX_BD_FLAGS_PACKET_END);
724 
725 	netdev_tx_sent_queue(txq, skb->len);
726 
727 	skb_tx_timestamp(skb);
728 
729 	prod = NEXT_TX(prod);
730 	WRITE_ONCE(txr->tx_prod, prod);
731 
732 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
733 		bnxt_txr_db_kick(bp, txr, prod);
734 	} else {
735 		if (free_size >= bp->tx_wake_thresh)
736 			txbd0->tx_bd_len_flags_type |=
737 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
738 		txr->kick_pending = 1;
739 	}
740 
741 tx_done:
742 
743 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
744 		if (netdev_xmit_more() && !tx_buf->is_push) {
745 			txbd0->tx_bd_len_flags_type &=
746 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
747 			bnxt_txr_db_kick(bp, txr, prod);
748 		}
749 
750 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
751 				   bp->tx_wake_thresh);
752 	}
753 	return NETDEV_TX_OK;
754 
755 tx_dma_error:
756 	last_frag = i;
757 
758 	/* start back at beginning and unmap skb */
759 	prod = txr->tx_prod;
760 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
761 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
762 			 skb_headlen(skb), DMA_TO_DEVICE);
763 	prod = NEXT_TX(prod);
764 
765 	/* unmap remaining mapped pages */
766 	for (i = 0; i < last_frag; i++) {
767 		prod = NEXT_TX(prod);
768 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
769 		dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
770 			       skb_frag_size(&skb_shinfo(skb)->frags[i]),
771 			       DMA_TO_DEVICE);
772 	}
773 
774 tx_free:
775 	dev_kfree_skb_any(skb);
776 tx_kick_pending:
777 	if (BNXT_TX_PTP_IS_SET(lflags)) {
778 		txr->tx_buf_ring[txr->tx_prod].is_ts_pkt = 0;
779 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
780 		if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
781 			/* set SKB to err so PTP worker will clean up */
782 			ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO);
783 	}
784 	if (txr->kick_pending)
785 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
786 	txr->tx_buf_ring[txr->tx_prod].skb = NULL;
787 	dev_core_stats_tx_dropped_inc(dev);
788 	return NETDEV_TX_OK;
789 }
790 
791 /* Returns true if some remaining TX packets not processed. */
792 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
793 			  int budget)
794 {
795 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
796 	struct pci_dev *pdev = bp->pdev;
797 	u16 hw_cons = txr->tx_hw_cons;
798 	unsigned int tx_bytes = 0;
799 	u16 cons = txr->tx_cons;
800 	int tx_pkts = 0;
801 	bool rc = false;
802 
803 	while (RING_TX(bp, cons) != hw_cons) {
804 		struct bnxt_sw_tx_bd *tx_buf;
805 		struct sk_buff *skb;
806 		bool is_ts_pkt;
807 		int j, last;
808 
809 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
810 		skb = tx_buf->skb;
811 
812 		if (unlikely(!skb)) {
813 			bnxt_sched_reset_txr(bp, txr, cons);
814 			return rc;
815 		}
816 
817 		is_ts_pkt = tx_buf->is_ts_pkt;
818 		if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) {
819 			rc = true;
820 			break;
821 		}
822 
823 		cons = NEXT_TX(cons);
824 		tx_pkts++;
825 		tx_bytes += skb->len;
826 		tx_buf->skb = NULL;
827 		tx_buf->is_ts_pkt = 0;
828 
829 		if (tx_buf->is_push) {
830 			tx_buf->is_push = 0;
831 			goto next_tx_int;
832 		}
833 
834 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
835 				 skb_headlen(skb), DMA_TO_DEVICE);
836 		last = tx_buf->nr_frags;
837 
838 		for (j = 0; j < last; j++) {
839 			cons = NEXT_TX(cons);
840 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
841 			dma_unmap_page(
842 				&pdev->dev,
843 				dma_unmap_addr(tx_buf, mapping),
844 				skb_frag_size(&skb_shinfo(skb)->frags[j]),
845 				DMA_TO_DEVICE);
846 		}
847 		if (unlikely(is_ts_pkt)) {
848 			if (BNXT_CHIP_P5(bp)) {
849 				/* PTP worker takes ownership of the skb */
850 				bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod);
851 				skb = NULL;
852 			}
853 		}
854 
855 next_tx_int:
856 		cons = NEXT_TX(cons);
857 
858 		dev_consume_skb_any(skb);
859 	}
860 
861 	WRITE_ONCE(txr->tx_cons, cons);
862 
863 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
864 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
865 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
866 
867 	return rc;
868 }
869 
870 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
871 {
872 	struct bnxt_tx_ring_info *txr;
873 	bool more = false;
874 	int i;
875 
876 	bnxt_for_each_napi_tx(i, bnapi, txr) {
877 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
878 			more |= __bnxt_tx_int(bp, txr, budget);
879 	}
880 	if (!more)
881 		bnapi->events &= ~BNXT_TX_CMP_EVENT;
882 }
883 
884 static bool bnxt_separate_head_pool(void)
885 {
886 	return PAGE_SIZE > BNXT_RX_PAGE_SIZE;
887 }
888 
889 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
890 					 struct bnxt_rx_ring_info *rxr,
891 					 unsigned int *offset,
892 					 gfp_t gfp)
893 {
894 	struct page *page;
895 
896 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
897 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
898 						BNXT_RX_PAGE_SIZE);
899 	} else {
900 		page = page_pool_dev_alloc_pages(rxr->page_pool);
901 		*offset = 0;
902 	}
903 	if (!page)
904 		return NULL;
905 
906 	*mapping = page_pool_get_dma_addr(page) + *offset;
907 	return page;
908 }
909 
910 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
911 				       struct bnxt_rx_ring_info *rxr,
912 				       gfp_t gfp)
913 {
914 	unsigned int offset;
915 	struct page *page;
916 
917 	page = page_pool_alloc_frag(rxr->head_pool, &offset,
918 				    bp->rx_buf_size, gfp);
919 	if (!page)
920 		return NULL;
921 
922 	*mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset;
923 	return page_address(page) + offset;
924 }
925 
926 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
927 		       u16 prod, gfp_t gfp)
928 {
929 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
930 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
931 	dma_addr_t mapping;
932 
933 	if (BNXT_RX_PAGE_MODE(bp)) {
934 		unsigned int offset;
935 		struct page *page =
936 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
937 
938 		if (!page)
939 			return -ENOMEM;
940 
941 		mapping += bp->rx_dma_offset;
942 		rx_buf->data = page;
943 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
944 	} else {
945 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp);
946 
947 		if (!data)
948 			return -ENOMEM;
949 
950 		rx_buf->data = data;
951 		rx_buf->data_ptr = data + bp->rx_offset;
952 	}
953 	rx_buf->mapping = mapping;
954 
955 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
956 	return 0;
957 }
958 
959 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
960 {
961 	u16 prod = rxr->rx_prod;
962 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
963 	struct bnxt *bp = rxr->bnapi->bp;
964 	struct rx_bd *cons_bd, *prod_bd;
965 
966 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
967 	cons_rx_buf = &rxr->rx_buf_ring[cons];
968 
969 	prod_rx_buf->data = data;
970 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
971 
972 	prod_rx_buf->mapping = cons_rx_buf->mapping;
973 
974 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
975 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
976 
977 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
978 }
979 
980 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
981 {
982 	u16 next, max = rxr->rx_agg_bmap_size;
983 
984 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
985 	if (next >= max)
986 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
987 	return next;
988 }
989 
990 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
991 				     struct bnxt_rx_ring_info *rxr,
992 				     u16 prod, gfp_t gfp)
993 {
994 	struct rx_bd *rxbd =
995 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
996 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
997 	struct page *page;
998 	dma_addr_t mapping;
999 	u16 sw_prod = rxr->rx_sw_agg_prod;
1000 	unsigned int offset = 0;
1001 
1002 	page = __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
1003 
1004 	if (!page)
1005 		return -ENOMEM;
1006 
1007 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1008 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1009 
1010 	__set_bit(sw_prod, rxr->rx_agg_bmap);
1011 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
1012 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1013 
1014 	rx_agg_buf->page = page;
1015 	rx_agg_buf->offset = offset;
1016 	rx_agg_buf->mapping = mapping;
1017 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1018 	rxbd->rx_bd_opaque = sw_prod;
1019 	return 0;
1020 }
1021 
1022 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1023 				       struct bnxt_cp_ring_info *cpr,
1024 				       u16 cp_cons, u16 curr)
1025 {
1026 	struct rx_agg_cmp *agg;
1027 
1028 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1029 	agg = (struct rx_agg_cmp *)
1030 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1031 	return agg;
1032 }
1033 
1034 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1035 					      struct bnxt_rx_ring_info *rxr,
1036 					      u16 agg_id, u16 curr)
1037 {
1038 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1039 
1040 	return &tpa_info->agg_arr[curr];
1041 }
1042 
1043 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1044 				   u16 start, u32 agg_bufs, bool tpa)
1045 {
1046 	struct bnxt_napi *bnapi = cpr->bnapi;
1047 	struct bnxt *bp = bnapi->bp;
1048 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1049 	u16 prod = rxr->rx_agg_prod;
1050 	u16 sw_prod = rxr->rx_sw_agg_prod;
1051 	bool p5_tpa = false;
1052 	u32 i;
1053 
1054 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1055 		p5_tpa = true;
1056 
1057 	for (i = 0; i < agg_bufs; i++) {
1058 		u16 cons;
1059 		struct rx_agg_cmp *agg;
1060 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1061 		struct rx_bd *prod_bd;
1062 		struct page *page;
1063 
1064 		if (p5_tpa)
1065 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1066 		else
1067 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1068 		cons = agg->rx_agg_cmp_opaque;
1069 		__clear_bit(cons, rxr->rx_agg_bmap);
1070 
1071 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1072 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1073 
1074 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1075 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1076 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1077 
1078 		/* It is possible for sw_prod to be equal to cons, so
1079 		 * set cons_rx_buf->page to NULL first.
1080 		 */
1081 		page = cons_rx_buf->page;
1082 		cons_rx_buf->page = NULL;
1083 		prod_rx_buf->page = page;
1084 		prod_rx_buf->offset = cons_rx_buf->offset;
1085 
1086 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1087 
1088 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1089 
1090 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1091 		prod_bd->rx_bd_opaque = sw_prod;
1092 
1093 		prod = NEXT_RX_AGG(prod);
1094 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1095 	}
1096 	rxr->rx_agg_prod = prod;
1097 	rxr->rx_sw_agg_prod = sw_prod;
1098 }
1099 
1100 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1101 					      struct bnxt_rx_ring_info *rxr,
1102 					      u16 cons, void *data, u8 *data_ptr,
1103 					      dma_addr_t dma_addr,
1104 					      unsigned int offset_and_len)
1105 {
1106 	unsigned int len = offset_and_len & 0xffff;
1107 	struct page *page = data;
1108 	u16 prod = rxr->rx_prod;
1109 	struct sk_buff *skb;
1110 	int err;
1111 
1112 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1113 	if (unlikely(err)) {
1114 		bnxt_reuse_rx_data(rxr, cons, data);
1115 		return NULL;
1116 	}
1117 	dma_addr -= bp->rx_dma_offset;
1118 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1119 				bp->rx_dir);
1120 	skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE);
1121 	if (!skb) {
1122 		page_pool_recycle_direct(rxr->page_pool, page);
1123 		return NULL;
1124 	}
1125 	skb_mark_for_recycle(skb);
1126 	skb_reserve(skb, bp->rx_offset);
1127 	__skb_put(skb, len);
1128 
1129 	return skb;
1130 }
1131 
1132 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1133 					struct bnxt_rx_ring_info *rxr,
1134 					u16 cons, void *data, u8 *data_ptr,
1135 					dma_addr_t dma_addr,
1136 					unsigned int offset_and_len)
1137 {
1138 	unsigned int payload = offset_and_len >> 16;
1139 	unsigned int len = offset_and_len & 0xffff;
1140 	skb_frag_t *frag;
1141 	struct page *page = data;
1142 	u16 prod = rxr->rx_prod;
1143 	struct sk_buff *skb;
1144 	int off, err;
1145 
1146 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1147 	if (unlikely(err)) {
1148 		bnxt_reuse_rx_data(rxr, cons, data);
1149 		return NULL;
1150 	}
1151 	dma_addr -= bp->rx_dma_offset;
1152 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1153 				bp->rx_dir);
1154 
1155 	if (unlikely(!payload))
1156 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1157 
1158 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1159 	if (!skb) {
1160 		page_pool_recycle_direct(rxr->page_pool, page);
1161 		return NULL;
1162 	}
1163 
1164 	skb_mark_for_recycle(skb);
1165 	off = (void *)data_ptr - page_address(page);
1166 	skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE);
1167 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1168 	       payload + NET_IP_ALIGN);
1169 
1170 	frag = &skb_shinfo(skb)->frags[0];
1171 	skb_frag_size_sub(frag, payload);
1172 	skb_frag_off_add(frag, payload);
1173 	skb->data_len -= payload;
1174 	skb->tail += payload;
1175 
1176 	return skb;
1177 }
1178 
1179 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1180 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1181 				   void *data, u8 *data_ptr,
1182 				   dma_addr_t dma_addr,
1183 				   unsigned int offset_and_len)
1184 {
1185 	u16 prod = rxr->rx_prod;
1186 	struct sk_buff *skb;
1187 	int err;
1188 
1189 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1190 	if (unlikely(err)) {
1191 		bnxt_reuse_rx_data(rxr, cons, data);
1192 		return NULL;
1193 	}
1194 
1195 	skb = napi_build_skb(data, bp->rx_buf_size);
1196 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1197 				bp->rx_dir);
1198 	if (!skb) {
1199 		page_pool_free_va(rxr->head_pool, data, true);
1200 		return NULL;
1201 	}
1202 
1203 	skb_mark_for_recycle(skb);
1204 	skb_reserve(skb, bp->rx_offset);
1205 	skb_put(skb, offset_and_len & 0xffff);
1206 	return skb;
1207 }
1208 
1209 static u32 __bnxt_rx_agg_pages(struct bnxt *bp,
1210 			       struct bnxt_cp_ring_info *cpr,
1211 			       struct skb_shared_info *shinfo,
1212 			       u16 idx, u32 agg_bufs, bool tpa,
1213 			       struct xdp_buff *xdp)
1214 {
1215 	struct bnxt_napi *bnapi = cpr->bnapi;
1216 	struct pci_dev *pdev = bp->pdev;
1217 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1218 	u16 prod = rxr->rx_agg_prod;
1219 	u32 i, total_frag_len = 0;
1220 	bool p5_tpa = false;
1221 
1222 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1223 		p5_tpa = true;
1224 
1225 	for (i = 0; i < agg_bufs; i++) {
1226 		skb_frag_t *frag = &shinfo->frags[i];
1227 		u16 cons, frag_len;
1228 		struct rx_agg_cmp *agg;
1229 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1230 		struct page *page;
1231 		dma_addr_t mapping;
1232 
1233 		if (p5_tpa)
1234 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1235 		else
1236 			agg = bnxt_get_agg(bp, cpr, idx, i);
1237 		cons = agg->rx_agg_cmp_opaque;
1238 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1239 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1240 
1241 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1242 		skb_frag_fill_page_desc(frag, cons_rx_buf->page,
1243 					cons_rx_buf->offset, frag_len);
1244 		shinfo->nr_frags = i + 1;
1245 		__clear_bit(cons, rxr->rx_agg_bmap);
1246 
1247 		/* It is possible for bnxt_alloc_rx_page() to allocate
1248 		 * a sw_prod index that equals the cons index, so we
1249 		 * need to clear the cons entry now.
1250 		 */
1251 		mapping = cons_rx_buf->mapping;
1252 		page = cons_rx_buf->page;
1253 		cons_rx_buf->page = NULL;
1254 
1255 		if (xdp && page_is_pfmemalloc(page))
1256 			xdp_buff_set_frag_pfmemalloc(xdp);
1257 
1258 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1259 			--shinfo->nr_frags;
1260 			cons_rx_buf->page = page;
1261 
1262 			/* Update prod since possibly some pages have been
1263 			 * allocated already.
1264 			 */
1265 			rxr->rx_agg_prod = prod;
1266 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1267 			return 0;
1268 		}
1269 
1270 		dma_sync_single_for_cpu(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1271 					bp->rx_dir);
1272 
1273 		total_frag_len += frag_len;
1274 		prod = NEXT_RX_AGG(prod);
1275 	}
1276 	rxr->rx_agg_prod = prod;
1277 	return total_frag_len;
1278 }
1279 
1280 static struct sk_buff *bnxt_rx_agg_pages_skb(struct bnxt *bp,
1281 					     struct bnxt_cp_ring_info *cpr,
1282 					     struct sk_buff *skb, u16 idx,
1283 					     u32 agg_bufs, bool tpa)
1284 {
1285 	struct skb_shared_info *shinfo = skb_shinfo(skb);
1286 	u32 total_frag_len = 0;
1287 
1288 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo, idx,
1289 					     agg_bufs, tpa, NULL);
1290 	if (!total_frag_len) {
1291 		skb_mark_for_recycle(skb);
1292 		dev_kfree_skb(skb);
1293 		return NULL;
1294 	}
1295 
1296 	skb->data_len += total_frag_len;
1297 	skb->len += total_frag_len;
1298 	skb->truesize += BNXT_RX_PAGE_SIZE * agg_bufs;
1299 	return skb;
1300 }
1301 
1302 static u32 bnxt_rx_agg_pages_xdp(struct bnxt *bp,
1303 				 struct bnxt_cp_ring_info *cpr,
1304 				 struct xdp_buff *xdp, u16 idx,
1305 				 u32 agg_bufs, bool tpa)
1306 {
1307 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1308 	u32 total_frag_len = 0;
1309 
1310 	if (!xdp_buff_has_frags(xdp))
1311 		shinfo->nr_frags = 0;
1312 
1313 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo,
1314 					     idx, agg_bufs, tpa, xdp);
1315 	if (total_frag_len) {
1316 		xdp_buff_set_frags_flag(xdp);
1317 		shinfo->nr_frags = agg_bufs;
1318 		shinfo->xdp_frags_size = total_frag_len;
1319 	}
1320 	return total_frag_len;
1321 }
1322 
1323 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1324 			       u8 agg_bufs, u32 *raw_cons)
1325 {
1326 	u16 last;
1327 	struct rx_agg_cmp *agg;
1328 
1329 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1330 	last = RING_CMP(*raw_cons);
1331 	agg = (struct rx_agg_cmp *)
1332 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1333 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1334 }
1335 
1336 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1337 				      unsigned int len,
1338 				      dma_addr_t mapping)
1339 {
1340 	struct bnxt *bp = bnapi->bp;
1341 	struct pci_dev *pdev = bp->pdev;
1342 	struct sk_buff *skb;
1343 
1344 	skb = napi_alloc_skb(&bnapi->napi, len);
1345 	if (!skb)
1346 		return NULL;
1347 
1348 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copybreak,
1349 				bp->rx_dir);
1350 
1351 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1352 	       len + NET_IP_ALIGN);
1353 
1354 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copybreak,
1355 				   bp->rx_dir);
1356 
1357 	skb_put(skb, len);
1358 
1359 	return skb;
1360 }
1361 
1362 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1363 				     unsigned int len,
1364 				     dma_addr_t mapping)
1365 {
1366 	return bnxt_copy_data(bnapi, data, len, mapping);
1367 }
1368 
1369 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1370 				     struct xdp_buff *xdp,
1371 				     unsigned int len,
1372 				     dma_addr_t mapping)
1373 {
1374 	unsigned int metasize = 0;
1375 	u8 *data = xdp->data;
1376 	struct sk_buff *skb;
1377 
1378 	len = xdp->data_end - xdp->data_meta;
1379 	metasize = xdp->data - xdp->data_meta;
1380 	data = xdp->data_meta;
1381 
1382 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1383 	if (!skb)
1384 		return skb;
1385 
1386 	if (metasize) {
1387 		skb_metadata_set(skb, metasize);
1388 		__skb_pull(skb, metasize);
1389 	}
1390 
1391 	return skb;
1392 }
1393 
1394 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1395 			   u32 *raw_cons, void *cmp)
1396 {
1397 	struct rx_cmp *rxcmp = cmp;
1398 	u32 tmp_raw_cons = *raw_cons;
1399 	u8 cmp_type, agg_bufs = 0;
1400 
1401 	cmp_type = RX_CMP_TYPE(rxcmp);
1402 
1403 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1404 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1405 			    RX_CMP_AGG_BUFS) >>
1406 			   RX_CMP_AGG_BUFS_SHIFT;
1407 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1408 		struct rx_tpa_end_cmp *tpa_end = cmp;
1409 
1410 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1411 			return 0;
1412 
1413 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1414 	}
1415 
1416 	if (agg_bufs) {
1417 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1418 			return -EBUSY;
1419 	}
1420 	*raw_cons = tmp_raw_cons;
1421 	return 0;
1422 }
1423 
1424 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1425 {
1426 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1427 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1428 
1429 	if (test_bit(idx, map->agg_idx_bmap))
1430 		idx = find_first_zero_bit(map->agg_idx_bmap,
1431 					  BNXT_AGG_IDX_BMAP_SIZE);
1432 	__set_bit(idx, map->agg_idx_bmap);
1433 	map->agg_id_tbl[agg_id] = idx;
1434 	return idx;
1435 }
1436 
1437 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1438 {
1439 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1440 
1441 	__clear_bit(idx, map->agg_idx_bmap);
1442 }
1443 
1444 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1445 {
1446 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1447 
1448 	return map->agg_id_tbl[agg_id];
1449 }
1450 
1451 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1452 			      struct rx_tpa_start_cmp *tpa_start,
1453 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1454 {
1455 	tpa_info->cfa_code_valid = 1;
1456 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1457 	tpa_info->vlan_valid = 0;
1458 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1459 		tpa_info->vlan_valid = 1;
1460 		tpa_info->metadata =
1461 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1462 	}
1463 }
1464 
1465 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1466 				 struct rx_tpa_start_cmp *tpa_start,
1467 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1468 {
1469 	tpa_info->vlan_valid = 0;
1470 	if (TPA_START_VLAN_VALID(tpa_start)) {
1471 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1472 		u32 vlan_proto = ETH_P_8021Q;
1473 
1474 		tpa_info->vlan_valid = 1;
1475 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1476 			vlan_proto = ETH_P_8021AD;
1477 		tpa_info->metadata = vlan_proto << 16 |
1478 				     TPA_START_METADATA0_TCI(tpa_start1);
1479 	}
1480 }
1481 
1482 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1483 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1484 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1485 {
1486 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1487 	struct bnxt_tpa_info *tpa_info;
1488 	u16 cons, prod, agg_id;
1489 	struct rx_bd *prod_bd;
1490 	dma_addr_t mapping;
1491 
1492 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1493 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1494 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1495 	} else {
1496 		agg_id = TPA_START_AGG_ID(tpa_start);
1497 	}
1498 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1499 	prod = rxr->rx_prod;
1500 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1501 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1502 	tpa_info = &rxr->rx_tpa[agg_id];
1503 
1504 	if (unlikely(cons != rxr->rx_next_cons ||
1505 		     TPA_START_ERROR(tpa_start))) {
1506 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1507 			    cons, rxr->rx_next_cons,
1508 			    TPA_START_ERROR_CODE(tpa_start1));
1509 		bnxt_sched_reset_rxr(bp, rxr);
1510 		return;
1511 	}
1512 	prod_rx_buf->data = tpa_info->data;
1513 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1514 
1515 	mapping = tpa_info->mapping;
1516 	prod_rx_buf->mapping = mapping;
1517 
1518 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1519 
1520 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1521 
1522 	tpa_info->data = cons_rx_buf->data;
1523 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1524 	cons_rx_buf->data = NULL;
1525 	tpa_info->mapping = cons_rx_buf->mapping;
1526 
1527 	tpa_info->len =
1528 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1529 				RX_TPA_START_CMP_LEN_SHIFT;
1530 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1531 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1532 		tpa_info->gso_type = SKB_GSO_TCPV4;
1533 		if (TPA_START_IS_IPV6(tpa_start1))
1534 			tpa_info->gso_type = SKB_GSO_TCPV6;
1535 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1536 		else if (!BNXT_CHIP_P4_PLUS(bp) &&
1537 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1538 			tpa_info->gso_type = SKB_GSO_TCPV6;
1539 		tpa_info->rss_hash =
1540 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1541 	} else {
1542 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1543 		tpa_info->gso_type = 0;
1544 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1545 	}
1546 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1547 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1548 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1549 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1550 	else
1551 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1552 	tpa_info->agg_count = 0;
1553 
1554 	rxr->rx_prod = NEXT_RX(prod);
1555 	cons = RING_RX(bp, NEXT_RX(cons));
1556 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1557 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1558 
1559 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1560 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1561 	cons_rx_buf->data = NULL;
1562 }
1563 
1564 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1565 {
1566 	if (agg_bufs)
1567 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1568 }
1569 
1570 #ifdef CONFIG_INET
1571 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1572 {
1573 	struct udphdr *uh = NULL;
1574 
1575 	if (ip_proto == htons(ETH_P_IP)) {
1576 		struct iphdr *iph = (struct iphdr *)skb->data;
1577 
1578 		if (iph->protocol == IPPROTO_UDP)
1579 			uh = (struct udphdr *)(iph + 1);
1580 	} else {
1581 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1582 
1583 		if (iph->nexthdr == IPPROTO_UDP)
1584 			uh = (struct udphdr *)(iph + 1);
1585 	}
1586 	if (uh) {
1587 		if (uh->check)
1588 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1589 		else
1590 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1591 	}
1592 }
1593 #endif
1594 
1595 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1596 					   int payload_off, int tcp_ts,
1597 					   struct sk_buff *skb)
1598 {
1599 #ifdef CONFIG_INET
1600 	struct tcphdr *th;
1601 	int len, nw_off;
1602 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1603 	u32 hdr_info = tpa_info->hdr_info;
1604 	bool loopback = false;
1605 
1606 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1607 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1608 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1609 
1610 	/* If the packet is an internal loopback packet, the offsets will
1611 	 * have an extra 4 bytes.
1612 	 */
1613 	if (inner_mac_off == 4) {
1614 		loopback = true;
1615 	} else if (inner_mac_off > 4) {
1616 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1617 					    ETH_HLEN - 2));
1618 
1619 		/* We only support inner iPv4/ipv6.  If we don't see the
1620 		 * correct protocol ID, it must be a loopback packet where
1621 		 * the offsets are off by 4.
1622 		 */
1623 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1624 			loopback = true;
1625 	}
1626 	if (loopback) {
1627 		/* internal loopback packet, subtract all offsets by 4 */
1628 		inner_ip_off -= 4;
1629 		inner_mac_off -= 4;
1630 		outer_ip_off -= 4;
1631 	}
1632 
1633 	nw_off = inner_ip_off - ETH_HLEN;
1634 	skb_set_network_header(skb, nw_off);
1635 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1636 		struct ipv6hdr *iph = ipv6_hdr(skb);
1637 
1638 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1639 		len = skb->len - skb_transport_offset(skb);
1640 		th = tcp_hdr(skb);
1641 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1642 	} else {
1643 		struct iphdr *iph = ip_hdr(skb);
1644 
1645 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1646 		len = skb->len - skb_transport_offset(skb);
1647 		th = tcp_hdr(skb);
1648 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1649 	}
1650 
1651 	if (inner_mac_off) { /* tunnel */
1652 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1653 					    ETH_HLEN - 2));
1654 
1655 		bnxt_gro_tunnel(skb, proto);
1656 	}
1657 #endif
1658 	return skb;
1659 }
1660 
1661 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1662 					   int payload_off, int tcp_ts,
1663 					   struct sk_buff *skb)
1664 {
1665 #ifdef CONFIG_INET
1666 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1667 	u32 hdr_info = tpa_info->hdr_info;
1668 	int iphdr_len, nw_off;
1669 
1670 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1671 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1672 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1673 
1674 	nw_off = inner_ip_off - ETH_HLEN;
1675 	skb_set_network_header(skb, nw_off);
1676 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1677 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1678 	skb_set_transport_header(skb, nw_off + iphdr_len);
1679 
1680 	if (inner_mac_off) { /* tunnel */
1681 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1682 					    ETH_HLEN - 2));
1683 
1684 		bnxt_gro_tunnel(skb, proto);
1685 	}
1686 #endif
1687 	return skb;
1688 }
1689 
1690 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1691 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1692 
1693 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1694 					   int payload_off, int tcp_ts,
1695 					   struct sk_buff *skb)
1696 {
1697 #ifdef CONFIG_INET
1698 	struct tcphdr *th;
1699 	int len, nw_off, tcp_opt_len = 0;
1700 
1701 	if (tcp_ts)
1702 		tcp_opt_len = 12;
1703 
1704 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1705 		struct iphdr *iph;
1706 
1707 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1708 			 ETH_HLEN;
1709 		skb_set_network_header(skb, nw_off);
1710 		iph = ip_hdr(skb);
1711 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1712 		len = skb->len - skb_transport_offset(skb);
1713 		th = tcp_hdr(skb);
1714 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1715 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1716 		struct ipv6hdr *iph;
1717 
1718 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1719 			 ETH_HLEN;
1720 		skb_set_network_header(skb, nw_off);
1721 		iph = ipv6_hdr(skb);
1722 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1723 		len = skb->len - skb_transport_offset(skb);
1724 		th = tcp_hdr(skb);
1725 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1726 	} else {
1727 		dev_kfree_skb_any(skb);
1728 		return NULL;
1729 	}
1730 
1731 	if (nw_off) /* tunnel */
1732 		bnxt_gro_tunnel(skb, skb->protocol);
1733 #endif
1734 	return skb;
1735 }
1736 
1737 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1738 					   struct bnxt_tpa_info *tpa_info,
1739 					   struct rx_tpa_end_cmp *tpa_end,
1740 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1741 					   struct sk_buff *skb)
1742 {
1743 #ifdef CONFIG_INET
1744 	int payload_off;
1745 	u16 segs;
1746 
1747 	segs = TPA_END_TPA_SEGS(tpa_end);
1748 	if (segs == 1)
1749 		return skb;
1750 
1751 	NAPI_GRO_CB(skb)->count = segs;
1752 	skb_shinfo(skb)->gso_size =
1753 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1754 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1755 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1756 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1757 	else
1758 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1759 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1760 	if (likely(skb))
1761 		tcp_gro_complete(skb);
1762 #endif
1763 	return skb;
1764 }
1765 
1766 /* Given the cfa_code of a received packet determine which
1767  * netdev (vf-rep or PF) the packet is destined to.
1768  */
1769 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1770 {
1771 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1772 
1773 	/* if vf-rep dev is NULL, the must belongs to the PF */
1774 	return dev ? dev : bp->dev;
1775 }
1776 
1777 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1778 					   struct bnxt_cp_ring_info *cpr,
1779 					   u32 *raw_cons,
1780 					   struct rx_tpa_end_cmp *tpa_end,
1781 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1782 					   u8 *event)
1783 {
1784 	struct bnxt_napi *bnapi = cpr->bnapi;
1785 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1786 	struct net_device *dev = bp->dev;
1787 	u8 *data_ptr, agg_bufs;
1788 	unsigned int len;
1789 	struct bnxt_tpa_info *tpa_info;
1790 	dma_addr_t mapping;
1791 	struct sk_buff *skb;
1792 	u16 idx = 0, agg_id;
1793 	void *data;
1794 	bool gro;
1795 
1796 	if (unlikely(bnapi->in_reset)) {
1797 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1798 
1799 		if (rc < 0)
1800 			return ERR_PTR(-EBUSY);
1801 		return NULL;
1802 	}
1803 
1804 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1805 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1806 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1807 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1808 		tpa_info = &rxr->rx_tpa[agg_id];
1809 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1810 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1811 				    agg_bufs, tpa_info->agg_count);
1812 			agg_bufs = tpa_info->agg_count;
1813 		}
1814 		tpa_info->agg_count = 0;
1815 		*event |= BNXT_AGG_EVENT;
1816 		bnxt_free_agg_idx(rxr, agg_id);
1817 		idx = agg_id;
1818 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1819 	} else {
1820 		agg_id = TPA_END_AGG_ID(tpa_end);
1821 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1822 		tpa_info = &rxr->rx_tpa[agg_id];
1823 		idx = RING_CMP(*raw_cons);
1824 		if (agg_bufs) {
1825 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1826 				return ERR_PTR(-EBUSY);
1827 
1828 			*event |= BNXT_AGG_EVENT;
1829 			idx = NEXT_CMP(idx);
1830 		}
1831 		gro = !!TPA_END_GRO(tpa_end);
1832 	}
1833 	data = tpa_info->data;
1834 	data_ptr = tpa_info->data_ptr;
1835 	prefetch(data_ptr);
1836 	len = tpa_info->len;
1837 	mapping = tpa_info->mapping;
1838 
1839 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1840 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1841 		if (agg_bufs > MAX_SKB_FRAGS)
1842 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1843 				    agg_bufs, (int)MAX_SKB_FRAGS);
1844 		return NULL;
1845 	}
1846 
1847 	if (len <= bp->rx_copybreak) {
1848 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1849 		if (!skb) {
1850 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1851 			cpr->sw_stats->rx.rx_oom_discards += 1;
1852 			return NULL;
1853 		}
1854 	} else {
1855 		u8 *new_data;
1856 		dma_addr_t new_mapping;
1857 
1858 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr,
1859 						GFP_ATOMIC);
1860 		if (!new_data) {
1861 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1862 			cpr->sw_stats->rx.rx_oom_discards += 1;
1863 			return NULL;
1864 		}
1865 
1866 		tpa_info->data = new_data;
1867 		tpa_info->data_ptr = new_data + bp->rx_offset;
1868 		tpa_info->mapping = new_mapping;
1869 
1870 		skb = napi_build_skb(data, bp->rx_buf_size);
1871 		dma_sync_single_for_cpu(&bp->pdev->dev, mapping,
1872 					bp->rx_buf_use_size, bp->rx_dir);
1873 
1874 		if (!skb) {
1875 			page_pool_free_va(rxr->head_pool, data, true);
1876 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1877 			cpr->sw_stats->rx.rx_oom_discards += 1;
1878 			return NULL;
1879 		}
1880 		skb_mark_for_recycle(skb);
1881 		skb_reserve(skb, bp->rx_offset);
1882 		skb_put(skb, len);
1883 	}
1884 
1885 	if (agg_bufs) {
1886 		skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, idx, agg_bufs, true);
1887 		if (!skb) {
1888 			/* Page reuse already handled by bnxt_rx_pages(). */
1889 			cpr->sw_stats->rx.rx_oom_discards += 1;
1890 			return NULL;
1891 		}
1892 	}
1893 
1894 	if (tpa_info->cfa_code_valid)
1895 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1896 	skb->protocol = eth_type_trans(skb, dev);
1897 
1898 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1899 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1900 
1901 	if (tpa_info->vlan_valid &&
1902 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1903 		__be16 vlan_proto = htons(tpa_info->metadata >>
1904 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1905 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1906 
1907 		if (eth_type_vlan(vlan_proto)) {
1908 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1909 		} else {
1910 			dev_kfree_skb(skb);
1911 			return NULL;
1912 		}
1913 	}
1914 
1915 	skb_checksum_none_assert(skb);
1916 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1917 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1918 		skb->csum_level =
1919 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1920 	}
1921 
1922 	if (gro)
1923 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1924 
1925 	return skb;
1926 }
1927 
1928 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1929 			 struct rx_agg_cmp *rx_agg)
1930 {
1931 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1932 	struct bnxt_tpa_info *tpa_info;
1933 
1934 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1935 	tpa_info = &rxr->rx_tpa[agg_id];
1936 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1937 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1938 }
1939 
1940 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1941 			     struct sk_buff *skb)
1942 {
1943 	skb_mark_for_recycle(skb);
1944 
1945 	if (skb->dev != bp->dev) {
1946 		/* this packet belongs to a vf-rep */
1947 		bnxt_vf_rep_rx(bp, skb);
1948 		return;
1949 	}
1950 	skb_record_rx_queue(skb, bnapi->index);
1951 	napi_gro_receive(&bnapi->napi, skb);
1952 }
1953 
1954 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
1955 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
1956 {
1957 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
1958 
1959 	if (BNXT_PTP_RX_TS_VALID(flags))
1960 		goto ts_valid;
1961 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
1962 		return false;
1963 
1964 ts_valid:
1965 	*cmpl_ts = ts;
1966 	return true;
1967 }
1968 
1969 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
1970 				    struct rx_cmp *rxcmp,
1971 				    struct rx_cmp_ext *rxcmp1)
1972 {
1973 	__be16 vlan_proto;
1974 	u16 vtag;
1975 
1976 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1977 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
1978 		u32 meta_data;
1979 
1980 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
1981 			return skb;
1982 
1983 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1984 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1985 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
1986 		if (eth_type_vlan(vlan_proto))
1987 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1988 		else
1989 			goto vlan_err;
1990 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
1991 		if (RX_CMP_VLAN_VALID(rxcmp)) {
1992 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
1993 
1994 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
1995 				vlan_proto = htons(ETH_P_8021Q);
1996 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
1997 				vlan_proto = htons(ETH_P_8021AD);
1998 			else
1999 				goto vlan_err;
2000 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
2001 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2002 		}
2003 	}
2004 	return skb;
2005 vlan_err:
2006 	dev_kfree_skb(skb);
2007 	return NULL;
2008 }
2009 
2010 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp,
2011 					   struct rx_cmp *rxcmp)
2012 {
2013 	u8 ext_op;
2014 
2015 	ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp);
2016 	switch (ext_op) {
2017 	case EXT_OP_INNER_4:
2018 	case EXT_OP_OUTER_4:
2019 	case EXT_OP_INNFL_3:
2020 	case EXT_OP_OUTFL_3:
2021 		return PKT_HASH_TYPE_L4;
2022 	default:
2023 		return PKT_HASH_TYPE_L3;
2024 	}
2025 }
2026 
2027 /* returns the following:
2028  * 1       - 1 packet successfully received
2029  * 0       - successful TPA_START, packet not completed yet
2030  * -EBUSY  - completion ring does not have all the agg buffers yet
2031  * -ENOMEM - packet aborted due to out of memory
2032  * -EIO    - packet aborted due to hw error indicated in BD
2033  */
2034 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2035 		       u32 *raw_cons, u8 *event)
2036 {
2037 	struct bnxt_napi *bnapi = cpr->bnapi;
2038 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2039 	struct net_device *dev = bp->dev;
2040 	struct rx_cmp *rxcmp;
2041 	struct rx_cmp_ext *rxcmp1;
2042 	u32 tmp_raw_cons = *raw_cons;
2043 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2044 	struct bnxt_sw_rx_bd *rx_buf;
2045 	unsigned int len;
2046 	u8 *data_ptr, agg_bufs, cmp_type;
2047 	bool xdp_active = false;
2048 	dma_addr_t dma_addr;
2049 	struct sk_buff *skb;
2050 	struct xdp_buff xdp;
2051 	u32 flags, misc;
2052 	u32 cmpl_ts;
2053 	void *data;
2054 	int rc = 0;
2055 
2056 	rxcmp = (struct rx_cmp *)
2057 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2058 
2059 	cmp_type = RX_CMP_TYPE(rxcmp);
2060 
2061 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2062 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2063 		goto next_rx_no_prod_no_len;
2064 	}
2065 
2066 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2067 	cp_cons = RING_CMP(tmp_raw_cons);
2068 	rxcmp1 = (struct rx_cmp_ext *)
2069 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2070 
2071 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2072 		return -EBUSY;
2073 
2074 	/* The valid test of the entry must be done first before
2075 	 * reading any further.
2076 	 */
2077 	dma_rmb();
2078 	prod = rxr->rx_prod;
2079 
2080 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2081 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2082 		bnxt_tpa_start(bp, rxr, cmp_type,
2083 			       (struct rx_tpa_start_cmp *)rxcmp,
2084 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2085 
2086 		*event |= BNXT_RX_EVENT;
2087 		goto next_rx_no_prod_no_len;
2088 
2089 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2090 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2091 				   (struct rx_tpa_end_cmp *)rxcmp,
2092 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2093 
2094 		if (IS_ERR(skb))
2095 			return -EBUSY;
2096 
2097 		rc = -ENOMEM;
2098 		if (likely(skb)) {
2099 			bnxt_deliver_skb(bp, bnapi, skb);
2100 			rc = 1;
2101 		}
2102 		*event |= BNXT_RX_EVENT;
2103 		goto next_rx_no_prod_no_len;
2104 	}
2105 
2106 	cons = rxcmp->rx_cmp_opaque;
2107 	if (unlikely(cons != rxr->rx_next_cons)) {
2108 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2109 
2110 		/* 0xffff is forced error, don't print it */
2111 		if (rxr->rx_next_cons != 0xffff)
2112 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2113 				    cons, rxr->rx_next_cons);
2114 		bnxt_sched_reset_rxr(bp, rxr);
2115 		if (rc1)
2116 			return rc1;
2117 		goto next_rx_no_prod_no_len;
2118 	}
2119 	rx_buf = &rxr->rx_buf_ring[cons];
2120 	data = rx_buf->data;
2121 	data_ptr = rx_buf->data_ptr;
2122 	prefetch(data_ptr);
2123 
2124 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2125 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2126 
2127 	if (agg_bufs) {
2128 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2129 			return -EBUSY;
2130 
2131 		cp_cons = NEXT_CMP(cp_cons);
2132 		*event |= BNXT_AGG_EVENT;
2133 	}
2134 	*event |= BNXT_RX_EVENT;
2135 
2136 	rx_buf->data = NULL;
2137 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2138 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2139 
2140 		bnxt_reuse_rx_data(rxr, cons, data);
2141 		if (agg_bufs)
2142 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2143 					       false);
2144 
2145 		rc = -EIO;
2146 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2147 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2148 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2149 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2150 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2151 						 rx_err);
2152 				bnxt_sched_reset_rxr(bp, rxr);
2153 			}
2154 		}
2155 		goto next_rx_no_len;
2156 	}
2157 
2158 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2159 	len = flags >> RX_CMP_LEN_SHIFT;
2160 	dma_addr = rx_buf->mapping;
2161 
2162 	if (bnxt_xdp_attached(bp, rxr)) {
2163 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2164 		if (agg_bufs) {
2165 			u32 frag_len = bnxt_rx_agg_pages_xdp(bp, cpr, &xdp,
2166 							     cp_cons, agg_bufs,
2167 							     false);
2168 			if (!frag_len)
2169 				goto oom_next_rx;
2170 		}
2171 		xdp_active = true;
2172 	}
2173 
2174 	if (xdp_active) {
2175 		if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) {
2176 			rc = 1;
2177 			goto next_rx;
2178 		}
2179 	}
2180 
2181 	if (len <= bp->rx_copybreak) {
2182 		if (!xdp_active)
2183 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2184 		else
2185 			skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr);
2186 		bnxt_reuse_rx_data(rxr, cons, data);
2187 		if (!skb) {
2188 			if (agg_bufs) {
2189 				if (!xdp_active)
2190 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2191 							       agg_bufs, false);
2192 				else
2193 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2194 			}
2195 			goto oom_next_rx;
2196 		}
2197 	} else {
2198 		u32 payload;
2199 
2200 		if (rx_buf->data_ptr == data_ptr)
2201 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2202 		else
2203 			payload = 0;
2204 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2205 				      payload | len);
2206 		if (!skb)
2207 			goto oom_next_rx;
2208 	}
2209 
2210 	if (agg_bufs) {
2211 		if (!xdp_active) {
2212 			skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, cp_cons, agg_bufs, false);
2213 			if (!skb)
2214 				goto oom_next_rx;
2215 		} else {
2216 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, rxr->page_pool, &xdp, rxcmp1);
2217 			if (!skb) {
2218 				/* we should be able to free the old skb here */
2219 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2220 				goto oom_next_rx;
2221 			}
2222 		}
2223 	}
2224 
2225 	if (RX_CMP_HASH_VALID(rxcmp)) {
2226 		enum pkt_hash_types type;
2227 
2228 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2229 			type = bnxt_rss_ext_op(bp, rxcmp);
2230 		} else {
2231 			u32 itypes = RX_CMP_ITYPES(rxcmp);
2232 
2233 			if (itypes == RX_CMP_FLAGS_ITYPE_TCP ||
2234 			    itypes == RX_CMP_FLAGS_ITYPE_UDP)
2235 				type = PKT_HASH_TYPE_L4;
2236 			else
2237 				type = PKT_HASH_TYPE_L3;
2238 		}
2239 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2240 	}
2241 
2242 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2243 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2244 	skb->protocol = eth_type_trans(skb, dev);
2245 
2246 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2247 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2248 		if (!skb)
2249 			goto next_rx;
2250 	}
2251 
2252 	skb_checksum_none_assert(skb);
2253 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2254 		if (dev->features & NETIF_F_RXCSUM) {
2255 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2256 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2257 		}
2258 	} else {
2259 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2260 			if (dev->features & NETIF_F_RXCSUM)
2261 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2262 		}
2263 	}
2264 
2265 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2266 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2267 			u64 ns, ts;
2268 
2269 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2270 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2271 
2272 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2273 				memset(skb_hwtstamps(skb), 0,
2274 				       sizeof(*skb_hwtstamps(skb)));
2275 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2276 			}
2277 		}
2278 	}
2279 	bnxt_deliver_skb(bp, bnapi, skb);
2280 	rc = 1;
2281 
2282 next_rx:
2283 	cpr->rx_packets += 1;
2284 	cpr->rx_bytes += len;
2285 
2286 next_rx_no_len:
2287 	rxr->rx_prod = NEXT_RX(prod);
2288 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2289 
2290 next_rx_no_prod_no_len:
2291 	*raw_cons = tmp_raw_cons;
2292 
2293 	return rc;
2294 
2295 oom_next_rx:
2296 	cpr->sw_stats->rx.rx_oom_discards += 1;
2297 	rc = -ENOMEM;
2298 	goto next_rx;
2299 }
2300 
2301 /* In netpoll mode, if we are using a combined completion ring, we need to
2302  * discard the rx packets and recycle the buffers.
2303  */
2304 static int bnxt_force_rx_discard(struct bnxt *bp,
2305 				 struct bnxt_cp_ring_info *cpr,
2306 				 u32 *raw_cons, u8 *event)
2307 {
2308 	u32 tmp_raw_cons = *raw_cons;
2309 	struct rx_cmp_ext *rxcmp1;
2310 	struct rx_cmp *rxcmp;
2311 	u16 cp_cons;
2312 	u8 cmp_type;
2313 	int rc;
2314 
2315 	cp_cons = RING_CMP(tmp_raw_cons);
2316 	rxcmp = (struct rx_cmp *)
2317 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2318 
2319 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2320 	cp_cons = RING_CMP(tmp_raw_cons);
2321 	rxcmp1 = (struct rx_cmp_ext *)
2322 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2323 
2324 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2325 		return -EBUSY;
2326 
2327 	/* The valid test of the entry must be done first before
2328 	 * reading any further.
2329 	 */
2330 	dma_rmb();
2331 	cmp_type = RX_CMP_TYPE(rxcmp);
2332 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2333 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2334 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2335 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2336 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2337 		struct rx_tpa_end_cmp_ext *tpa_end1;
2338 
2339 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2340 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2341 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2342 	}
2343 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2344 	if (rc && rc != -EBUSY)
2345 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2346 	return rc;
2347 }
2348 
2349 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2350 {
2351 	struct bnxt_fw_health *fw_health = bp->fw_health;
2352 	u32 reg = fw_health->regs[reg_idx];
2353 	u32 reg_type, reg_off, val = 0;
2354 
2355 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2356 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2357 	switch (reg_type) {
2358 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2359 		pci_read_config_dword(bp->pdev, reg_off, &val);
2360 		break;
2361 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2362 		reg_off = fw_health->mapped_regs[reg_idx];
2363 		fallthrough;
2364 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2365 		val = readl(bp->bar0 + reg_off);
2366 		break;
2367 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2368 		val = readl(bp->bar1 + reg_off);
2369 		break;
2370 	}
2371 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2372 		val &= fw_health->fw_reset_inprog_reg_mask;
2373 	return val;
2374 }
2375 
2376 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2377 {
2378 	int i;
2379 
2380 	for (i = 0; i < bp->rx_nr_rings; i++) {
2381 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2382 		struct bnxt_ring_grp_info *grp_info;
2383 
2384 		grp_info = &bp->grp_info[grp_idx];
2385 		if (grp_info->agg_fw_ring_id == ring_id)
2386 			return grp_idx;
2387 	}
2388 	return INVALID_HW_RING_ID;
2389 }
2390 
2391 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2392 {
2393 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2394 
2395 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2396 		return link_info->force_link_speed2;
2397 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2398 		return link_info->force_pam4_link_speed;
2399 	return link_info->force_link_speed;
2400 }
2401 
2402 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2403 {
2404 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2405 
2406 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2407 		link_info->req_link_speed = link_info->force_link_speed2;
2408 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2409 		switch (link_info->req_link_speed) {
2410 		case BNXT_LINK_SPEED_50GB_PAM4:
2411 		case BNXT_LINK_SPEED_100GB_PAM4:
2412 		case BNXT_LINK_SPEED_200GB_PAM4:
2413 		case BNXT_LINK_SPEED_400GB_PAM4:
2414 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2415 			break;
2416 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2417 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2418 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2419 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2420 			break;
2421 		default:
2422 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2423 		}
2424 		return;
2425 	}
2426 	link_info->req_link_speed = link_info->force_link_speed;
2427 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2428 	if (link_info->force_pam4_link_speed) {
2429 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2430 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2431 	}
2432 }
2433 
2434 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2435 {
2436 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2437 
2438 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2439 		link_info->advertising = link_info->auto_link_speeds2;
2440 		return;
2441 	}
2442 	link_info->advertising = link_info->auto_link_speeds;
2443 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2444 }
2445 
2446 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2447 {
2448 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2449 
2450 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2451 		if (link_info->req_link_speed != link_info->force_link_speed2)
2452 			return true;
2453 		return false;
2454 	}
2455 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2456 	    link_info->req_link_speed != link_info->force_link_speed)
2457 		return true;
2458 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2459 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2460 		return true;
2461 	return false;
2462 }
2463 
2464 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2465 {
2466 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2467 
2468 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2469 		if (link_info->advertising != link_info->auto_link_speeds2)
2470 			return true;
2471 		return false;
2472 	}
2473 	if (link_info->advertising != link_info->auto_link_speeds ||
2474 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2475 		return true;
2476 	return false;
2477 }
2478 
2479 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type)
2480 {
2481 	u32 flags = bp->ctx->ctx_arr[type].flags;
2482 
2483 	return (flags & BNXT_CTX_MEM_TYPE_VALID) &&
2484 		((flags & BNXT_CTX_MEM_FW_TRACE) ||
2485 		 (flags & BNXT_CTX_MEM_FW_BIN_TRACE));
2486 }
2487 
2488 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm)
2489 {
2490 	u32 mem_size, pages, rem_bytes, magic_byte_offset;
2491 	u16 trace_type = bnxt_bstore_to_trace[ctxm->type];
2492 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
2493 	struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl;
2494 	struct bnxt_bs_trace_info *bs_trace;
2495 	int last_pg;
2496 
2497 	if (ctxm->instance_bmap && ctxm->instance_bmap > 1)
2498 		return;
2499 
2500 	mem_size = ctxm->max_entries * ctxm->entry_size;
2501 	rem_bytes = mem_size % BNXT_PAGE_SIZE;
2502 	pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
2503 
2504 	last_pg = (pages - 1) & (MAX_CTX_PAGES - 1);
2505 	magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1;
2506 
2507 	rmem = &ctx_pg[0].ring_mem;
2508 	bs_trace = &bp->bs_trace[trace_type];
2509 	bs_trace->ctx_type = ctxm->type;
2510 	bs_trace->trace_type = trace_type;
2511 	if (pages > MAX_CTX_PAGES) {
2512 		int last_pg_dir = rmem->nr_pages - 1;
2513 
2514 		rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem;
2515 		bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg];
2516 	} else {
2517 		bs_trace->magic_byte = rmem->pg_arr[last_pg];
2518 	}
2519 	bs_trace->magic_byte += magic_byte_offset;
2520 	*bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE;
2521 }
2522 
2523 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1)				\
2524 	(((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\
2525 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT)
2526 
2527 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2)				\
2528 	(((data2) &							\
2529 	  ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\
2530 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT)
2531 
2532 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2533 	((data2) &							\
2534 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2535 
2536 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2537 	(((data2) &							\
2538 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2539 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2540 
2541 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2542 	((data1) &							\
2543 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2544 
2545 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2546 	(((data1) &							\
2547 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2548 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2549 
2550 /* Return true if the workqueue has to be scheduled */
2551 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2552 {
2553 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2554 
2555 	switch (err_type) {
2556 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2557 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2558 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2559 		break;
2560 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2561 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2562 		break;
2563 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2564 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2565 		break;
2566 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2567 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2568 		char *threshold_type;
2569 		bool notify = false;
2570 		char *dir_str;
2571 
2572 		switch (type) {
2573 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2574 			threshold_type = "warning";
2575 			break;
2576 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2577 			threshold_type = "critical";
2578 			break;
2579 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2580 			threshold_type = "fatal";
2581 			break;
2582 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2583 			threshold_type = "shutdown";
2584 			break;
2585 		default:
2586 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2587 			return false;
2588 		}
2589 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2590 			dir_str = "above";
2591 			notify = true;
2592 		} else {
2593 			dir_str = "below";
2594 		}
2595 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2596 			    dir_str, threshold_type);
2597 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2598 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2599 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2600 		if (notify) {
2601 			bp->thermal_threshold_type = type;
2602 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2603 			return true;
2604 		}
2605 		return false;
2606 	}
2607 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2608 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2609 		break;
2610 	default:
2611 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2612 			   err_type);
2613 		break;
2614 	}
2615 	return false;
2616 }
2617 
2618 #define BNXT_GET_EVENT_PORT(data)	\
2619 	((data) &			\
2620 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2621 
2622 #define BNXT_EVENT_RING_TYPE(data2)	\
2623 	((data2) &			\
2624 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2625 
2626 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2627 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2628 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2629 
2630 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2631 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2632 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2633 
2634 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2635 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2636 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2637 
2638 #define BNXT_PHC_BITS	48
2639 
2640 static int bnxt_async_event_process(struct bnxt *bp,
2641 				    struct hwrm_async_event_cmpl *cmpl)
2642 {
2643 	u16 event_id = le16_to_cpu(cmpl->event_id);
2644 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2645 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2646 
2647 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2648 		   event_id, data1, data2);
2649 
2650 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2651 	switch (event_id) {
2652 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2653 		struct bnxt_link_info *link_info = &bp->link_info;
2654 
2655 		if (BNXT_VF(bp))
2656 			goto async_event_process_exit;
2657 
2658 		/* print unsupported speed warning in forced speed mode only */
2659 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2660 		    (data1 & 0x20000)) {
2661 			u16 fw_speed = bnxt_get_force_speed(link_info);
2662 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2663 
2664 			if (speed != SPEED_UNKNOWN)
2665 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2666 					    speed);
2667 		}
2668 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2669 	}
2670 		fallthrough;
2671 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2672 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2673 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2674 		fallthrough;
2675 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2676 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2677 		break;
2678 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2679 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2680 		break;
2681 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2682 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2683 
2684 		if (BNXT_VF(bp))
2685 			break;
2686 
2687 		if (bp->pf.port_id != port_id)
2688 			break;
2689 
2690 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2691 		break;
2692 	}
2693 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2694 		if (BNXT_PF(bp))
2695 			goto async_event_process_exit;
2696 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2697 		break;
2698 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2699 		char *type_str = "Solicited";
2700 
2701 		if (!bp->fw_health)
2702 			goto async_event_process_exit;
2703 
2704 		bp->fw_reset_timestamp = jiffies;
2705 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2706 		if (!bp->fw_reset_min_dsecs)
2707 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2708 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2709 		if (!bp->fw_reset_max_dsecs)
2710 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2711 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2712 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2713 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2714 			type_str = "Fatal";
2715 			bp->fw_health->fatalities++;
2716 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2717 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2718 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2719 			type_str = "Non-fatal";
2720 			bp->fw_health->survivals++;
2721 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2722 		}
2723 		netif_warn(bp, hw, bp->dev,
2724 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2725 			   type_str, data1, data2,
2726 			   bp->fw_reset_min_dsecs * 100,
2727 			   bp->fw_reset_max_dsecs * 100);
2728 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2729 		break;
2730 	}
2731 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2732 		struct bnxt_fw_health *fw_health = bp->fw_health;
2733 		char *status_desc = "healthy";
2734 		u32 status;
2735 
2736 		if (!fw_health)
2737 			goto async_event_process_exit;
2738 
2739 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2740 			fw_health->enabled = false;
2741 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2742 			break;
2743 		}
2744 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2745 		fw_health->tmr_multiplier =
2746 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2747 				     bp->current_interval * 10);
2748 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2749 		if (!fw_health->enabled)
2750 			fw_health->last_fw_heartbeat =
2751 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2752 		fw_health->last_fw_reset_cnt =
2753 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2754 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2755 		if (status != BNXT_FW_STATUS_HEALTHY)
2756 			status_desc = "unhealthy";
2757 		netif_info(bp, drv, bp->dev,
2758 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2759 			   fw_health->primary ? "primary" : "backup", status,
2760 			   status_desc, fw_health->last_fw_reset_cnt);
2761 		if (!fw_health->enabled) {
2762 			/* Make sure tmr_counter is set and visible to
2763 			 * bnxt_health_check() before setting enabled to true.
2764 			 */
2765 			smp_wmb();
2766 			fw_health->enabled = true;
2767 		}
2768 		goto async_event_process_exit;
2769 	}
2770 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2771 		netif_notice(bp, hw, bp->dev,
2772 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2773 			     data1, data2);
2774 		goto async_event_process_exit;
2775 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2776 		struct bnxt_rx_ring_info *rxr;
2777 		u16 grp_idx;
2778 
2779 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2780 			goto async_event_process_exit;
2781 
2782 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2783 			    BNXT_EVENT_RING_TYPE(data2), data1);
2784 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2785 			goto async_event_process_exit;
2786 
2787 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2788 		if (grp_idx == INVALID_HW_RING_ID) {
2789 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2790 				    data1);
2791 			goto async_event_process_exit;
2792 		}
2793 		rxr = bp->bnapi[grp_idx]->rx_ring;
2794 		bnxt_sched_reset_rxr(bp, rxr);
2795 		goto async_event_process_exit;
2796 	}
2797 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2798 		struct bnxt_fw_health *fw_health = bp->fw_health;
2799 
2800 		netif_notice(bp, hw, bp->dev,
2801 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2802 			     data1, data2);
2803 		if (fw_health) {
2804 			fw_health->echo_req_data1 = data1;
2805 			fw_health->echo_req_data2 = data2;
2806 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2807 			break;
2808 		}
2809 		goto async_event_process_exit;
2810 	}
2811 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2812 		bnxt_ptp_pps_event(bp, data1, data2);
2813 		goto async_event_process_exit;
2814 	}
2815 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2816 		if (bnxt_event_error_report(bp, data1, data2))
2817 			break;
2818 		goto async_event_process_exit;
2819 	}
2820 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2821 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2822 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2823 			if (BNXT_PTP_USE_RTC(bp)) {
2824 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2825 				unsigned long flags;
2826 				u64 ns;
2827 
2828 				if (!ptp)
2829 					goto async_event_process_exit;
2830 
2831 				bnxt_ptp_update_current_time(bp);
2832 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2833 				       BNXT_PHC_BITS) | ptp->current_time);
2834 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2835 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2836 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2837 			}
2838 			break;
2839 		}
2840 		goto async_event_process_exit;
2841 	}
2842 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2843 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2844 
2845 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2846 		goto async_event_process_exit;
2847 	}
2848 	case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: {
2849 		u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1);
2850 		u32 offset =  BNXT_EVENT_BUF_PRODUCER_OFFSET(data2);
2851 
2852 		bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset);
2853 		goto async_event_process_exit;
2854 	}
2855 	default:
2856 		goto async_event_process_exit;
2857 	}
2858 	__bnxt_queue_sp_work(bp);
2859 async_event_process_exit:
2860 	bnxt_ulp_async_events(bp, cmpl);
2861 	return 0;
2862 }
2863 
2864 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2865 {
2866 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2867 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2868 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2869 				(struct hwrm_fwd_req_cmpl *)txcmp;
2870 
2871 	switch (cmpl_type) {
2872 	case CMPL_BASE_TYPE_HWRM_DONE:
2873 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2874 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2875 		break;
2876 
2877 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2878 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2879 
2880 		if ((vf_id < bp->pf.first_vf_id) ||
2881 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2882 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2883 				   vf_id);
2884 			return -EINVAL;
2885 		}
2886 
2887 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2888 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2889 		break;
2890 
2891 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2892 		bnxt_async_event_process(bp,
2893 					 (struct hwrm_async_event_cmpl *)txcmp);
2894 		break;
2895 
2896 	default:
2897 		break;
2898 	}
2899 
2900 	return 0;
2901 }
2902 
2903 static bool bnxt_vnic_is_active(struct bnxt *bp)
2904 {
2905 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
2906 
2907 	return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0;
2908 }
2909 
2910 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2911 {
2912 	struct bnxt_napi *bnapi = dev_instance;
2913 	struct bnxt *bp = bnapi->bp;
2914 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2915 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2916 
2917 	cpr->event_ctr++;
2918 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2919 	napi_schedule(&bnapi->napi);
2920 	return IRQ_HANDLED;
2921 }
2922 
2923 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2924 {
2925 	u32 raw_cons = cpr->cp_raw_cons;
2926 	u16 cons = RING_CMP(raw_cons);
2927 	struct tx_cmp *txcmp;
2928 
2929 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2930 
2931 	return TX_CMP_VALID(txcmp, raw_cons);
2932 }
2933 
2934 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2935 			    int budget)
2936 {
2937 	struct bnxt_napi *bnapi = cpr->bnapi;
2938 	u32 raw_cons = cpr->cp_raw_cons;
2939 	u32 cons;
2940 	int rx_pkts = 0;
2941 	u8 event = 0;
2942 	struct tx_cmp *txcmp;
2943 
2944 	cpr->has_more_work = 0;
2945 	cpr->had_work_done = 1;
2946 	while (1) {
2947 		u8 cmp_type;
2948 		int rc;
2949 
2950 		cons = RING_CMP(raw_cons);
2951 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2952 
2953 		if (!TX_CMP_VALID(txcmp, raw_cons))
2954 			break;
2955 
2956 		/* The valid test of the entry must be done first before
2957 		 * reading any further.
2958 		 */
2959 		dma_rmb();
2960 		cmp_type = TX_CMP_TYPE(txcmp);
2961 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
2962 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
2963 			u32 opaque = txcmp->tx_cmp_opaque;
2964 			struct bnxt_tx_ring_info *txr;
2965 			u16 tx_freed;
2966 
2967 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
2968 			event |= BNXT_TX_CMP_EVENT;
2969 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
2970 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
2971 			else
2972 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
2973 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
2974 				   bp->tx_ring_mask;
2975 			/* return full budget so NAPI will complete. */
2976 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
2977 				rx_pkts = budget;
2978 				raw_cons = NEXT_RAW_CMP(raw_cons);
2979 				if (budget)
2980 					cpr->has_more_work = 1;
2981 				break;
2982 			}
2983 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
2984 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
2985 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
2986 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2987 			if (likely(budget))
2988 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2989 			else
2990 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2991 							   &event);
2992 			if (likely(rc >= 0))
2993 				rx_pkts += rc;
2994 			/* Increment rx_pkts when rc is -ENOMEM to count towards
2995 			 * the NAPI budget.  Otherwise, we may potentially loop
2996 			 * here forever if we consistently cannot allocate
2997 			 * buffers.
2998 			 */
2999 			else if (rc == -ENOMEM && budget)
3000 				rx_pkts++;
3001 			else if (rc == -EBUSY)	/* partial completion */
3002 				break;
3003 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
3004 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
3005 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
3006 			bnxt_hwrm_handler(bp, txcmp);
3007 		}
3008 		raw_cons = NEXT_RAW_CMP(raw_cons);
3009 
3010 		if (rx_pkts && rx_pkts == budget) {
3011 			cpr->has_more_work = 1;
3012 			break;
3013 		}
3014 	}
3015 
3016 	if (event & BNXT_REDIRECT_EVENT) {
3017 		xdp_do_flush();
3018 		event &= ~BNXT_REDIRECT_EVENT;
3019 	}
3020 
3021 	if (event & BNXT_TX_EVENT) {
3022 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3023 		u16 prod = txr->tx_prod;
3024 
3025 		/* Sync BD data before updating doorbell */
3026 		wmb();
3027 
3028 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3029 		event &= ~BNXT_TX_EVENT;
3030 	}
3031 
3032 	cpr->cp_raw_cons = raw_cons;
3033 	bnapi->events |= event;
3034 	return rx_pkts;
3035 }
3036 
3037 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3038 				  int budget)
3039 {
3040 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3041 		bnapi->tx_int(bp, bnapi, budget);
3042 
3043 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3044 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3045 
3046 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3047 		bnapi->events &= ~BNXT_RX_EVENT;
3048 	}
3049 	if (bnapi->events & BNXT_AGG_EVENT) {
3050 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3051 
3052 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3053 		bnapi->events &= ~BNXT_AGG_EVENT;
3054 	}
3055 }
3056 
3057 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3058 			  int budget)
3059 {
3060 	struct bnxt_napi *bnapi = cpr->bnapi;
3061 	int rx_pkts;
3062 
3063 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3064 
3065 	/* ACK completion ring before freeing tx ring and producing new
3066 	 * buffers in rx/agg rings to prevent overflowing the completion
3067 	 * ring.
3068 	 */
3069 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3070 
3071 	__bnxt_poll_work_done(bp, bnapi, budget);
3072 	return rx_pkts;
3073 }
3074 
3075 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3076 {
3077 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3078 	struct bnxt *bp = bnapi->bp;
3079 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3080 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3081 	struct tx_cmp *txcmp;
3082 	struct rx_cmp_ext *rxcmp1;
3083 	u32 cp_cons, tmp_raw_cons;
3084 	u32 raw_cons = cpr->cp_raw_cons;
3085 	bool flush_xdp = false;
3086 	u32 rx_pkts = 0;
3087 	u8 event = 0;
3088 
3089 	while (1) {
3090 		int rc;
3091 
3092 		cp_cons = RING_CMP(raw_cons);
3093 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3094 
3095 		if (!TX_CMP_VALID(txcmp, raw_cons))
3096 			break;
3097 
3098 		/* The valid test of the entry must be done first before
3099 		 * reading any further.
3100 		 */
3101 		dma_rmb();
3102 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3103 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3104 			cp_cons = RING_CMP(tmp_raw_cons);
3105 			rxcmp1 = (struct rx_cmp_ext *)
3106 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3107 
3108 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3109 				break;
3110 
3111 			/* force an error to recycle the buffer */
3112 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3113 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3114 
3115 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3116 			if (likely(rc == -EIO) && budget)
3117 				rx_pkts++;
3118 			else if (rc == -EBUSY)	/* partial completion */
3119 				break;
3120 			if (event & BNXT_REDIRECT_EVENT)
3121 				flush_xdp = true;
3122 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3123 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3124 			bnxt_hwrm_handler(bp, txcmp);
3125 		} else {
3126 			netdev_err(bp->dev,
3127 				   "Invalid completion received on special ring\n");
3128 		}
3129 		raw_cons = NEXT_RAW_CMP(raw_cons);
3130 
3131 		if (rx_pkts == budget)
3132 			break;
3133 	}
3134 
3135 	cpr->cp_raw_cons = raw_cons;
3136 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3137 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3138 
3139 	if (event & BNXT_AGG_EVENT)
3140 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3141 	if (flush_xdp)
3142 		xdp_do_flush();
3143 
3144 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3145 		napi_complete_done(napi, rx_pkts);
3146 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3147 	}
3148 	return rx_pkts;
3149 }
3150 
3151 static int bnxt_poll(struct napi_struct *napi, int budget)
3152 {
3153 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3154 	struct bnxt *bp = bnapi->bp;
3155 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3156 	int work_done = 0;
3157 
3158 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3159 		napi_complete(napi);
3160 		return 0;
3161 	}
3162 	while (1) {
3163 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3164 
3165 		if (work_done >= budget) {
3166 			if (!budget)
3167 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3168 			break;
3169 		}
3170 
3171 		if (!bnxt_has_work(bp, cpr)) {
3172 			if (napi_complete_done(napi, work_done))
3173 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3174 			break;
3175 		}
3176 	}
3177 	if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3178 		struct dim_sample dim_sample = {};
3179 
3180 		dim_update_sample(cpr->event_ctr,
3181 				  cpr->rx_packets,
3182 				  cpr->rx_bytes,
3183 				  &dim_sample);
3184 		net_dim(&cpr->dim, &dim_sample);
3185 	}
3186 	return work_done;
3187 }
3188 
3189 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3190 {
3191 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3192 	int i, work_done = 0;
3193 
3194 	for (i = 0; i < cpr->cp_ring_count; i++) {
3195 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3196 
3197 		if (cpr2->had_nqe_notify) {
3198 			work_done += __bnxt_poll_work(bp, cpr2,
3199 						      budget - work_done);
3200 			cpr->has_more_work |= cpr2->has_more_work;
3201 		}
3202 	}
3203 	return work_done;
3204 }
3205 
3206 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3207 				 u64 dbr_type, int budget)
3208 {
3209 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3210 	int i;
3211 
3212 	for (i = 0; i < cpr->cp_ring_count; i++) {
3213 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3214 		struct bnxt_db_info *db;
3215 
3216 		if (cpr2->had_work_done) {
3217 			u32 tgl = 0;
3218 
3219 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3220 				cpr2->had_nqe_notify = 0;
3221 				tgl = cpr2->toggle;
3222 			}
3223 			db = &cpr2->cp_db;
3224 			bnxt_writeq(bp,
3225 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3226 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3227 				    db->doorbell);
3228 			cpr2->had_work_done = 0;
3229 		}
3230 	}
3231 	__bnxt_poll_work_done(bp, bnapi, budget);
3232 }
3233 
3234 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3235 {
3236 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3237 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3238 	struct bnxt_cp_ring_info *cpr_rx;
3239 	u32 raw_cons = cpr->cp_raw_cons;
3240 	struct bnxt *bp = bnapi->bp;
3241 	struct nqe_cn *nqcmp;
3242 	int work_done = 0;
3243 	u32 cons;
3244 
3245 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3246 		napi_complete(napi);
3247 		return 0;
3248 	}
3249 	if (cpr->has_more_work) {
3250 		cpr->has_more_work = 0;
3251 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3252 	}
3253 	while (1) {
3254 		u16 type;
3255 
3256 		cons = RING_CMP(raw_cons);
3257 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3258 
3259 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3260 			if (cpr->has_more_work)
3261 				break;
3262 
3263 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3264 					     budget);
3265 			cpr->cp_raw_cons = raw_cons;
3266 			if (napi_complete_done(napi, work_done))
3267 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3268 						  cpr->cp_raw_cons);
3269 			goto poll_done;
3270 		}
3271 
3272 		/* The valid test of the entry must be done first before
3273 		 * reading any further.
3274 		 */
3275 		dma_rmb();
3276 
3277 		type = le16_to_cpu(nqcmp->type);
3278 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3279 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3280 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3281 			struct bnxt_cp_ring_info *cpr2;
3282 
3283 			/* No more budget for RX work */
3284 			if (budget && work_done >= budget &&
3285 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3286 				break;
3287 
3288 			idx = BNXT_NQ_HDL_IDX(idx);
3289 			cpr2 = &cpr->cp_ring_arr[idx];
3290 			cpr2->had_nqe_notify = 1;
3291 			cpr2->toggle = NQE_CN_TOGGLE(type);
3292 			work_done += __bnxt_poll_work(bp, cpr2,
3293 						      budget - work_done);
3294 			cpr->has_more_work |= cpr2->has_more_work;
3295 		} else {
3296 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3297 		}
3298 		raw_cons = NEXT_RAW_CMP(raw_cons);
3299 	}
3300 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3301 	if (raw_cons != cpr->cp_raw_cons) {
3302 		cpr->cp_raw_cons = raw_cons;
3303 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3304 	}
3305 poll_done:
3306 	cpr_rx = &cpr->cp_ring_arr[0];
3307 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3308 	    (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3309 		struct dim_sample dim_sample = {};
3310 
3311 		dim_update_sample(cpr->event_ctr,
3312 				  cpr_rx->rx_packets,
3313 				  cpr_rx->rx_bytes,
3314 				  &dim_sample);
3315 		net_dim(&cpr->dim, &dim_sample);
3316 	}
3317 	return work_done;
3318 }
3319 
3320 static void bnxt_free_one_tx_ring_skbs(struct bnxt *bp,
3321 				       struct bnxt_tx_ring_info *txr, int idx)
3322 {
3323 	int i, max_idx;
3324 	struct pci_dev *pdev = bp->pdev;
3325 
3326 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3327 
3328 	for (i = 0; i < max_idx;) {
3329 		struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[i];
3330 		struct sk_buff *skb;
3331 		int j, last;
3332 
3333 		if (idx  < bp->tx_nr_rings_xdp &&
3334 		    tx_buf->action == XDP_REDIRECT) {
3335 			dma_unmap_single(&pdev->dev,
3336 					 dma_unmap_addr(tx_buf, mapping),
3337 					 dma_unmap_len(tx_buf, len),
3338 					 DMA_TO_DEVICE);
3339 			xdp_return_frame(tx_buf->xdpf);
3340 			tx_buf->action = 0;
3341 			tx_buf->xdpf = NULL;
3342 			i++;
3343 			continue;
3344 		}
3345 
3346 		skb = tx_buf->skb;
3347 		if (!skb) {
3348 			i++;
3349 			continue;
3350 		}
3351 
3352 		tx_buf->skb = NULL;
3353 
3354 		if (tx_buf->is_push) {
3355 			dev_kfree_skb(skb);
3356 			i += 2;
3357 			continue;
3358 		}
3359 
3360 		dma_unmap_single(&pdev->dev,
3361 				 dma_unmap_addr(tx_buf, mapping),
3362 				 skb_headlen(skb),
3363 				 DMA_TO_DEVICE);
3364 
3365 		last = tx_buf->nr_frags;
3366 		i += 2;
3367 		for (j = 0; j < last; j++, i++) {
3368 			int ring_idx = i & bp->tx_ring_mask;
3369 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
3370 
3371 			tx_buf = &txr->tx_buf_ring[ring_idx];
3372 			dma_unmap_page(&pdev->dev,
3373 				       dma_unmap_addr(tx_buf, mapping),
3374 				       skb_frag_size(frag), DMA_TO_DEVICE);
3375 		}
3376 		dev_kfree_skb(skb);
3377 	}
3378 	netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, idx));
3379 }
3380 
3381 static void bnxt_free_tx_skbs(struct bnxt *bp)
3382 {
3383 	int i;
3384 
3385 	if (!bp->tx_ring)
3386 		return;
3387 
3388 	for (i = 0; i < bp->tx_nr_rings; i++) {
3389 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3390 
3391 		if (!txr->tx_buf_ring)
3392 			continue;
3393 
3394 		bnxt_free_one_tx_ring_skbs(bp, txr, i);
3395 	}
3396 }
3397 
3398 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3399 {
3400 	int i, max_idx;
3401 
3402 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3403 
3404 	for (i = 0; i < max_idx; i++) {
3405 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3406 		void *data = rx_buf->data;
3407 
3408 		if (!data)
3409 			continue;
3410 
3411 		rx_buf->data = NULL;
3412 		if (BNXT_RX_PAGE_MODE(bp))
3413 			page_pool_recycle_direct(rxr->page_pool, data);
3414 		else
3415 			page_pool_free_va(rxr->head_pool, data, true);
3416 	}
3417 }
3418 
3419 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3420 {
3421 	int i, max_idx;
3422 
3423 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3424 
3425 	for (i = 0; i < max_idx; i++) {
3426 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3427 		struct page *page = rx_agg_buf->page;
3428 
3429 		if (!page)
3430 			continue;
3431 
3432 		rx_agg_buf->page = NULL;
3433 		__clear_bit(i, rxr->rx_agg_bmap);
3434 
3435 		page_pool_recycle_direct(rxr->page_pool, page);
3436 	}
3437 }
3438 
3439 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3440 					struct bnxt_rx_ring_info *rxr)
3441 {
3442 	int i;
3443 
3444 	for (i = 0; i < bp->max_tpa; i++) {
3445 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3446 		u8 *data = tpa_info->data;
3447 
3448 		if (!data)
3449 			continue;
3450 
3451 		tpa_info->data = NULL;
3452 		page_pool_free_va(rxr->head_pool, data, false);
3453 	}
3454 }
3455 
3456 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3457 				       struct bnxt_rx_ring_info *rxr)
3458 {
3459 	struct bnxt_tpa_idx_map *map;
3460 
3461 	if (!rxr->rx_tpa)
3462 		goto skip_rx_tpa_free;
3463 
3464 	bnxt_free_one_tpa_info_data(bp, rxr);
3465 
3466 skip_rx_tpa_free:
3467 	if (!rxr->rx_buf_ring)
3468 		goto skip_rx_buf_free;
3469 
3470 	bnxt_free_one_rx_ring(bp, rxr);
3471 
3472 skip_rx_buf_free:
3473 	if (!rxr->rx_agg_ring)
3474 		goto skip_rx_agg_free;
3475 
3476 	bnxt_free_one_rx_agg_ring(bp, rxr);
3477 
3478 skip_rx_agg_free:
3479 	map = rxr->rx_tpa_idx_map;
3480 	if (map)
3481 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3482 }
3483 
3484 static void bnxt_free_rx_skbs(struct bnxt *bp)
3485 {
3486 	int i;
3487 
3488 	if (!bp->rx_ring)
3489 		return;
3490 
3491 	for (i = 0; i < bp->rx_nr_rings; i++)
3492 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3493 }
3494 
3495 static void bnxt_free_skbs(struct bnxt *bp)
3496 {
3497 	bnxt_free_tx_skbs(bp);
3498 	bnxt_free_rx_skbs(bp);
3499 }
3500 
3501 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3502 {
3503 	u8 init_val = ctxm->init_value;
3504 	u16 offset = ctxm->init_offset;
3505 	u8 *p2 = p;
3506 	int i;
3507 
3508 	if (!init_val)
3509 		return;
3510 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3511 		memset(p, init_val, len);
3512 		return;
3513 	}
3514 	for (i = 0; i < len; i += ctxm->entry_size)
3515 		*(p2 + i + offset) = init_val;
3516 }
3517 
3518 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3519 			       void *buf, size_t offset, size_t head,
3520 			       size_t tail)
3521 {
3522 	int i, head_page, start_idx, source_offset;
3523 	size_t len, rem_len, total_len, max_bytes;
3524 
3525 	head_page = head / rmem->page_size;
3526 	source_offset = head % rmem->page_size;
3527 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3528 	if (!total_len)
3529 		total_len = MAX_CTX_BYTES;
3530 	start_idx = head_page % MAX_CTX_PAGES;
3531 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3532 		    source_offset;
3533 	total_len = min(total_len, max_bytes);
3534 	rem_len = total_len;
3535 
3536 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3537 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3538 		if (buf)
3539 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3540 			       len);
3541 		offset += len;
3542 		rem_len -= len;
3543 	}
3544 	return total_len;
3545 }
3546 
3547 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3548 {
3549 	struct pci_dev *pdev = bp->pdev;
3550 	int i;
3551 
3552 	if (!rmem->pg_arr)
3553 		goto skip_pages;
3554 
3555 	for (i = 0; i < rmem->nr_pages; i++) {
3556 		if (!rmem->pg_arr[i])
3557 			continue;
3558 
3559 		dma_free_coherent(&pdev->dev, rmem->page_size,
3560 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3561 
3562 		rmem->pg_arr[i] = NULL;
3563 	}
3564 skip_pages:
3565 	if (rmem->pg_tbl) {
3566 		size_t pg_tbl_size = rmem->nr_pages * 8;
3567 
3568 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3569 			pg_tbl_size = rmem->page_size;
3570 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3571 				  rmem->pg_tbl, rmem->pg_tbl_map);
3572 		rmem->pg_tbl = NULL;
3573 	}
3574 	if (rmem->vmem_size && *rmem->vmem) {
3575 		vfree(*rmem->vmem);
3576 		*rmem->vmem = NULL;
3577 	}
3578 }
3579 
3580 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3581 {
3582 	struct pci_dev *pdev = bp->pdev;
3583 	u64 valid_bit = 0;
3584 	int i;
3585 
3586 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3587 		valid_bit = PTU_PTE_VALID;
3588 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3589 		size_t pg_tbl_size = rmem->nr_pages * 8;
3590 
3591 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3592 			pg_tbl_size = rmem->page_size;
3593 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3594 						  &rmem->pg_tbl_map,
3595 						  GFP_KERNEL);
3596 		if (!rmem->pg_tbl)
3597 			return -ENOMEM;
3598 	}
3599 
3600 	for (i = 0; i < rmem->nr_pages; i++) {
3601 		u64 extra_bits = valid_bit;
3602 
3603 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3604 						     rmem->page_size,
3605 						     &rmem->dma_arr[i],
3606 						     GFP_KERNEL);
3607 		if (!rmem->pg_arr[i])
3608 			return -ENOMEM;
3609 
3610 		if (rmem->ctx_mem)
3611 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3612 					  rmem->page_size);
3613 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3614 			if (i == rmem->nr_pages - 2 &&
3615 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3616 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3617 			else if (i == rmem->nr_pages - 1 &&
3618 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3619 				extra_bits |= PTU_PTE_LAST;
3620 			rmem->pg_tbl[i] =
3621 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3622 		}
3623 	}
3624 
3625 	if (rmem->vmem_size) {
3626 		*rmem->vmem = vzalloc(rmem->vmem_size);
3627 		if (!(*rmem->vmem))
3628 			return -ENOMEM;
3629 	}
3630 	return 0;
3631 }
3632 
3633 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3634 				   struct bnxt_rx_ring_info *rxr)
3635 {
3636 	int i;
3637 
3638 	kfree(rxr->rx_tpa_idx_map);
3639 	rxr->rx_tpa_idx_map = NULL;
3640 	if (rxr->rx_tpa) {
3641 		for (i = 0; i < bp->max_tpa; i++) {
3642 			kfree(rxr->rx_tpa[i].agg_arr);
3643 			rxr->rx_tpa[i].agg_arr = NULL;
3644 		}
3645 	}
3646 	kfree(rxr->rx_tpa);
3647 	rxr->rx_tpa = NULL;
3648 }
3649 
3650 static void bnxt_free_tpa_info(struct bnxt *bp)
3651 {
3652 	int i;
3653 
3654 	for (i = 0; i < bp->rx_nr_rings; i++) {
3655 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3656 
3657 		bnxt_free_one_tpa_info(bp, rxr);
3658 	}
3659 }
3660 
3661 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3662 				   struct bnxt_rx_ring_info *rxr)
3663 {
3664 	struct rx_agg_cmp *agg;
3665 	int i;
3666 
3667 	rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3668 			      GFP_KERNEL);
3669 	if (!rxr->rx_tpa)
3670 		return -ENOMEM;
3671 
3672 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3673 		return 0;
3674 	for (i = 0; i < bp->max_tpa; i++) {
3675 		agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3676 		if (!agg)
3677 			return -ENOMEM;
3678 		rxr->rx_tpa[i].agg_arr = agg;
3679 	}
3680 	rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3681 				      GFP_KERNEL);
3682 	if (!rxr->rx_tpa_idx_map)
3683 		return -ENOMEM;
3684 
3685 	return 0;
3686 }
3687 
3688 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3689 {
3690 	int i, rc;
3691 
3692 	bp->max_tpa = MAX_TPA;
3693 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3694 		if (!bp->max_tpa_v2)
3695 			return 0;
3696 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3697 	}
3698 
3699 	for (i = 0; i < bp->rx_nr_rings; i++) {
3700 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3701 
3702 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3703 		if (rc)
3704 			return rc;
3705 	}
3706 	return 0;
3707 }
3708 
3709 static void bnxt_free_rx_rings(struct bnxt *bp)
3710 {
3711 	int i;
3712 
3713 	if (!bp->rx_ring)
3714 		return;
3715 
3716 	bnxt_free_tpa_info(bp);
3717 	for (i = 0; i < bp->rx_nr_rings; i++) {
3718 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3719 		struct bnxt_ring_struct *ring;
3720 
3721 		if (rxr->xdp_prog)
3722 			bpf_prog_put(rxr->xdp_prog);
3723 
3724 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3725 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3726 
3727 		page_pool_destroy(rxr->page_pool);
3728 		if (bnxt_separate_head_pool())
3729 			page_pool_destroy(rxr->head_pool);
3730 		rxr->page_pool = rxr->head_pool = NULL;
3731 
3732 		kfree(rxr->rx_agg_bmap);
3733 		rxr->rx_agg_bmap = NULL;
3734 
3735 		ring = &rxr->rx_ring_struct;
3736 		bnxt_free_ring(bp, &ring->ring_mem);
3737 
3738 		ring = &rxr->rx_agg_ring_struct;
3739 		bnxt_free_ring(bp, &ring->ring_mem);
3740 	}
3741 }
3742 
3743 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3744 				   struct bnxt_rx_ring_info *rxr,
3745 				   int numa_node)
3746 {
3747 	struct page_pool_params pp = { 0 };
3748 	struct page_pool *pool;
3749 
3750 	pp.pool_size = bp->rx_agg_ring_size;
3751 	if (BNXT_RX_PAGE_MODE(bp))
3752 		pp.pool_size += bp->rx_ring_size;
3753 	pp.nid = numa_node;
3754 	pp.napi = &rxr->bnapi->napi;
3755 	pp.netdev = bp->dev;
3756 	pp.dev = &bp->pdev->dev;
3757 	pp.dma_dir = bp->rx_dir;
3758 	pp.max_len = PAGE_SIZE;
3759 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3760 
3761 	pool = page_pool_create(&pp);
3762 	if (IS_ERR(pool))
3763 		return PTR_ERR(pool);
3764 	rxr->page_pool = pool;
3765 
3766 	if (bnxt_separate_head_pool()) {
3767 		pp.pool_size = max(bp->rx_ring_size, 1024);
3768 		pool = page_pool_create(&pp);
3769 		if (IS_ERR(pool))
3770 			goto err_destroy_pp;
3771 	}
3772 	rxr->head_pool = pool;
3773 
3774 	return 0;
3775 
3776 err_destroy_pp:
3777 	page_pool_destroy(rxr->page_pool);
3778 	rxr->page_pool = NULL;
3779 	return PTR_ERR(pool);
3780 }
3781 
3782 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3783 {
3784 	u16 mem_size;
3785 
3786 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3787 	mem_size = rxr->rx_agg_bmap_size / 8;
3788 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3789 	if (!rxr->rx_agg_bmap)
3790 		return -ENOMEM;
3791 
3792 	return 0;
3793 }
3794 
3795 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3796 {
3797 	int numa_node = dev_to_node(&bp->pdev->dev);
3798 	int i, rc = 0, agg_rings = 0, cpu;
3799 
3800 	if (!bp->rx_ring)
3801 		return -ENOMEM;
3802 
3803 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3804 		agg_rings = 1;
3805 
3806 	for (i = 0; i < bp->rx_nr_rings; i++) {
3807 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3808 		struct bnxt_ring_struct *ring;
3809 		int cpu_node;
3810 
3811 		ring = &rxr->rx_ring_struct;
3812 
3813 		cpu = cpumask_local_spread(i, numa_node);
3814 		cpu_node = cpu_to_node(cpu);
3815 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3816 			   i, cpu_node);
3817 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3818 		if (rc)
3819 			return rc;
3820 
3821 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3822 		if (rc < 0)
3823 			return rc;
3824 
3825 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3826 						MEM_TYPE_PAGE_POOL,
3827 						rxr->page_pool);
3828 		if (rc) {
3829 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3830 			return rc;
3831 		}
3832 
3833 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3834 		if (rc)
3835 			return rc;
3836 
3837 		ring->grp_idx = i;
3838 		if (agg_rings) {
3839 			ring = &rxr->rx_agg_ring_struct;
3840 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3841 			if (rc)
3842 				return rc;
3843 
3844 			ring->grp_idx = i;
3845 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
3846 			if (rc)
3847 				return rc;
3848 		}
3849 	}
3850 	if (bp->flags & BNXT_FLAG_TPA)
3851 		rc = bnxt_alloc_tpa_info(bp);
3852 	return rc;
3853 }
3854 
3855 static void bnxt_free_tx_rings(struct bnxt *bp)
3856 {
3857 	int i;
3858 	struct pci_dev *pdev = bp->pdev;
3859 
3860 	if (!bp->tx_ring)
3861 		return;
3862 
3863 	for (i = 0; i < bp->tx_nr_rings; i++) {
3864 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3865 		struct bnxt_ring_struct *ring;
3866 
3867 		if (txr->tx_push) {
3868 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3869 					  txr->tx_push, txr->tx_push_mapping);
3870 			txr->tx_push = NULL;
3871 		}
3872 
3873 		ring = &txr->tx_ring_struct;
3874 
3875 		bnxt_free_ring(bp, &ring->ring_mem);
3876 	}
3877 }
3878 
3879 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3880 	((tc) * (bp)->tx_nr_rings_per_tc)
3881 
3882 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3883 	((tx) % (bp)->tx_nr_rings_per_tc)
3884 
3885 #define BNXT_RING_TO_TC(bp, tx)		\
3886 	((tx) / (bp)->tx_nr_rings_per_tc)
3887 
3888 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3889 {
3890 	int i, j, rc;
3891 	struct pci_dev *pdev = bp->pdev;
3892 
3893 	bp->tx_push_size = 0;
3894 	if (bp->tx_push_thresh) {
3895 		int push_size;
3896 
3897 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3898 					bp->tx_push_thresh);
3899 
3900 		if (push_size > 256) {
3901 			push_size = 0;
3902 			bp->tx_push_thresh = 0;
3903 		}
3904 
3905 		bp->tx_push_size = push_size;
3906 	}
3907 
3908 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3909 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3910 		struct bnxt_ring_struct *ring;
3911 		u8 qidx;
3912 
3913 		ring = &txr->tx_ring_struct;
3914 
3915 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3916 		if (rc)
3917 			return rc;
3918 
3919 		ring->grp_idx = txr->bnapi->index;
3920 		if (bp->tx_push_size) {
3921 			dma_addr_t mapping;
3922 
3923 			/* One pre-allocated DMA buffer to backup
3924 			 * TX push operation
3925 			 */
3926 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3927 						bp->tx_push_size,
3928 						&txr->tx_push_mapping,
3929 						GFP_KERNEL);
3930 
3931 			if (!txr->tx_push)
3932 				return -ENOMEM;
3933 
3934 			mapping = txr->tx_push_mapping +
3935 				sizeof(struct tx_push_bd);
3936 			txr->data_mapping = cpu_to_le64(mapping);
3937 		}
3938 		qidx = bp->tc_to_qidx[j];
3939 		ring->queue_id = bp->q_info[qidx].queue_id;
3940 		spin_lock_init(&txr->xdp_tx_lock);
3941 		if (i < bp->tx_nr_rings_xdp)
3942 			continue;
3943 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
3944 			j++;
3945 	}
3946 	return 0;
3947 }
3948 
3949 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
3950 {
3951 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3952 
3953 	kfree(cpr->cp_desc_ring);
3954 	cpr->cp_desc_ring = NULL;
3955 	ring->ring_mem.pg_arr = NULL;
3956 	kfree(cpr->cp_desc_mapping);
3957 	cpr->cp_desc_mapping = NULL;
3958 	ring->ring_mem.dma_arr = NULL;
3959 }
3960 
3961 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
3962 {
3963 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
3964 	if (!cpr->cp_desc_ring)
3965 		return -ENOMEM;
3966 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
3967 				       GFP_KERNEL);
3968 	if (!cpr->cp_desc_mapping)
3969 		return -ENOMEM;
3970 	return 0;
3971 }
3972 
3973 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
3974 {
3975 	int i;
3976 
3977 	if (!bp->bnapi)
3978 		return;
3979 	for (i = 0; i < bp->cp_nr_rings; i++) {
3980 		struct bnxt_napi *bnapi = bp->bnapi[i];
3981 
3982 		if (!bnapi)
3983 			continue;
3984 		bnxt_free_cp_arrays(&bnapi->cp_ring);
3985 	}
3986 }
3987 
3988 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
3989 {
3990 	int i, n = bp->cp_nr_pages;
3991 
3992 	for (i = 0; i < bp->cp_nr_rings; i++) {
3993 		struct bnxt_napi *bnapi = bp->bnapi[i];
3994 		int rc;
3995 
3996 		if (!bnapi)
3997 			continue;
3998 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
3999 		if (rc)
4000 			return rc;
4001 	}
4002 	return 0;
4003 }
4004 
4005 static void bnxt_free_cp_rings(struct bnxt *bp)
4006 {
4007 	int i;
4008 
4009 	if (!bp->bnapi)
4010 		return;
4011 
4012 	for (i = 0; i < bp->cp_nr_rings; i++) {
4013 		struct bnxt_napi *bnapi = bp->bnapi[i];
4014 		struct bnxt_cp_ring_info *cpr;
4015 		struct bnxt_ring_struct *ring;
4016 		int j;
4017 
4018 		if (!bnapi)
4019 			continue;
4020 
4021 		cpr = &bnapi->cp_ring;
4022 		ring = &cpr->cp_ring_struct;
4023 
4024 		bnxt_free_ring(bp, &ring->ring_mem);
4025 
4026 		if (!cpr->cp_ring_arr)
4027 			continue;
4028 
4029 		for (j = 0; j < cpr->cp_ring_count; j++) {
4030 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4031 
4032 			ring = &cpr2->cp_ring_struct;
4033 			bnxt_free_ring(bp, &ring->ring_mem);
4034 			bnxt_free_cp_arrays(cpr2);
4035 		}
4036 		kfree(cpr->cp_ring_arr);
4037 		cpr->cp_ring_arr = NULL;
4038 		cpr->cp_ring_count = 0;
4039 	}
4040 }
4041 
4042 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4043 				  struct bnxt_cp_ring_info *cpr)
4044 {
4045 	struct bnxt_ring_mem_info *rmem;
4046 	struct bnxt_ring_struct *ring;
4047 	int rc;
4048 
4049 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4050 	if (rc) {
4051 		bnxt_free_cp_arrays(cpr);
4052 		return -ENOMEM;
4053 	}
4054 	ring = &cpr->cp_ring_struct;
4055 	rmem = &ring->ring_mem;
4056 	rmem->nr_pages = bp->cp_nr_pages;
4057 	rmem->page_size = HW_CMPD_RING_SIZE;
4058 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4059 	rmem->dma_arr = cpr->cp_desc_mapping;
4060 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4061 	rc = bnxt_alloc_ring(bp, rmem);
4062 	if (rc) {
4063 		bnxt_free_ring(bp, rmem);
4064 		bnxt_free_cp_arrays(cpr);
4065 	}
4066 	return rc;
4067 }
4068 
4069 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4070 {
4071 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4072 	int i, j, rc, ulp_msix;
4073 	int tcs = bp->num_tc;
4074 
4075 	if (!tcs)
4076 		tcs = 1;
4077 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4078 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4079 		struct bnxt_napi *bnapi = bp->bnapi[i];
4080 		struct bnxt_cp_ring_info *cpr, *cpr2;
4081 		struct bnxt_ring_struct *ring;
4082 		int cp_count = 0, k;
4083 		int rx = 0, tx = 0;
4084 
4085 		if (!bnapi)
4086 			continue;
4087 
4088 		cpr = &bnapi->cp_ring;
4089 		cpr->bnapi = bnapi;
4090 		ring = &cpr->cp_ring_struct;
4091 
4092 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4093 		if (rc)
4094 			return rc;
4095 
4096 		ring->map_idx = ulp_msix + i;
4097 
4098 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4099 			continue;
4100 
4101 		if (i < bp->rx_nr_rings) {
4102 			cp_count++;
4103 			rx = 1;
4104 		}
4105 		if (i < bp->tx_nr_rings_xdp) {
4106 			cp_count++;
4107 			tx = 1;
4108 		} else if ((sh && i < bp->tx_nr_rings) ||
4109 			 (!sh && i >= bp->rx_nr_rings)) {
4110 			cp_count += tcs;
4111 			tx = 1;
4112 		}
4113 
4114 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
4115 					   GFP_KERNEL);
4116 		if (!cpr->cp_ring_arr)
4117 			return -ENOMEM;
4118 		cpr->cp_ring_count = cp_count;
4119 
4120 		for (k = 0; k < cp_count; k++) {
4121 			cpr2 = &cpr->cp_ring_arr[k];
4122 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4123 			if (rc)
4124 				return rc;
4125 			cpr2->bnapi = bnapi;
4126 			cpr2->sw_stats = cpr->sw_stats;
4127 			cpr2->cp_idx = k;
4128 			if (!k && rx) {
4129 				bp->rx_ring[i].rx_cpr = cpr2;
4130 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4131 			} else {
4132 				int n, tc = k - rx;
4133 
4134 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4135 				bp->tx_ring[n].tx_cpr = cpr2;
4136 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4137 			}
4138 		}
4139 		if (tx)
4140 			j++;
4141 	}
4142 	return 0;
4143 }
4144 
4145 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4146 				     struct bnxt_rx_ring_info *rxr)
4147 {
4148 	struct bnxt_ring_mem_info *rmem;
4149 	struct bnxt_ring_struct *ring;
4150 
4151 	ring = &rxr->rx_ring_struct;
4152 	rmem = &ring->ring_mem;
4153 	rmem->nr_pages = bp->rx_nr_pages;
4154 	rmem->page_size = HW_RXBD_RING_SIZE;
4155 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4156 	rmem->dma_arr = rxr->rx_desc_mapping;
4157 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4158 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4159 
4160 	ring = &rxr->rx_agg_ring_struct;
4161 	rmem = &ring->ring_mem;
4162 	rmem->nr_pages = bp->rx_agg_nr_pages;
4163 	rmem->page_size = HW_RXBD_RING_SIZE;
4164 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4165 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4166 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4167 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4168 }
4169 
4170 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4171 				      struct bnxt_rx_ring_info *rxr)
4172 {
4173 	struct bnxt_ring_mem_info *rmem;
4174 	struct bnxt_ring_struct *ring;
4175 	int i;
4176 
4177 	rxr->page_pool->p.napi = NULL;
4178 	rxr->page_pool = NULL;
4179 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4180 
4181 	ring = &rxr->rx_ring_struct;
4182 	rmem = &ring->ring_mem;
4183 	rmem->pg_tbl = NULL;
4184 	rmem->pg_tbl_map = 0;
4185 	for (i = 0; i < rmem->nr_pages; i++) {
4186 		rmem->pg_arr[i] = NULL;
4187 		rmem->dma_arr[i] = 0;
4188 	}
4189 	*rmem->vmem = NULL;
4190 
4191 	ring = &rxr->rx_agg_ring_struct;
4192 	rmem = &ring->ring_mem;
4193 	rmem->pg_tbl = NULL;
4194 	rmem->pg_tbl_map = 0;
4195 	for (i = 0; i < rmem->nr_pages; i++) {
4196 		rmem->pg_arr[i] = NULL;
4197 		rmem->dma_arr[i] = 0;
4198 	}
4199 	*rmem->vmem = NULL;
4200 }
4201 
4202 static void bnxt_init_ring_struct(struct bnxt *bp)
4203 {
4204 	int i, j;
4205 
4206 	for (i = 0; i < bp->cp_nr_rings; i++) {
4207 		struct bnxt_napi *bnapi = bp->bnapi[i];
4208 		struct bnxt_ring_mem_info *rmem;
4209 		struct bnxt_cp_ring_info *cpr;
4210 		struct bnxt_rx_ring_info *rxr;
4211 		struct bnxt_tx_ring_info *txr;
4212 		struct bnxt_ring_struct *ring;
4213 
4214 		if (!bnapi)
4215 			continue;
4216 
4217 		cpr = &bnapi->cp_ring;
4218 		ring = &cpr->cp_ring_struct;
4219 		rmem = &ring->ring_mem;
4220 		rmem->nr_pages = bp->cp_nr_pages;
4221 		rmem->page_size = HW_CMPD_RING_SIZE;
4222 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4223 		rmem->dma_arr = cpr->cp_desc_mapping;
4224 		rmem->vmem_size = 0;
4225 
4226 		rxr = bnapi->rx_ring;
4227 		if (!rxr)
4228 			goto skip_rx;
4229 
4230 		ring = &rxr->rx_ring_struct;
4231 		rmem = &ring->ring_mem;
4232 		rmem->nr_pages = bp->rx_nr_pages;
4233 		rmem->page_size = HW_RXBD_RING_SIZE;
4234 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4235 		rmem->dma_arr = rxr->rx_desc_mapping;
4236 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4237 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4238 
4239 		ring = &rxr->rx_agg_ring_struct;
4240 		rmem = &ring->ring_mem;
4241 		rmem->nr_pages = bp->rx_agg_nr_pages;
4242 		rmem->page_size = HW_RXBD_RING_SIZE;
4243 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4244 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4245 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4246 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4247 
4248 skip_rx:
4249 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4250 			ring = &txr->tx_ring_struct;
4251 			rmem = &ring->ring_mem;
4252 			rmem->nr_pages = bp->tx_nr_pages;
4253 			rmem->page_size = HW_TXBD_RING_SIZE;
4254 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4255 			rmem->dma_arr = txr->tx_desc_mapping;
4256 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4257 			rmem->vmem = (void **)&txr->tx_buf_ring;
4258 		}
4259 	}
4260 }
4261 
4262 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4263 {
4264 	int i;
4265 	u32 prod;
4266 	struct rx_bd **rx_buf_ring;
4267 
4268 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4269 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4270 		int j;
4271 		struct rx_bd *rxbd;
4272 
4273 		rxbd = rx_buf_ring[i];
4274 		if (!rxbd)
4275 			continue;
4276 
4277 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4278 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4279 			rxbd->rx_bd_opaque = prod;
4280 		}
4281 	}
4282 }
4283 
4284 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4285 				       struct bnxt_rx_ring_info *rxr,
4286 				       int ring_nr)
4287 {
4288 	u32 prod;
4289 	int i;
4290 
4291 	prod = rxr->rx_prod;
4292 	for (i = 0; i < bp->rx_ring_size; i++) {
4293 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4294 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4295 				    ring_nr, i, bp->rx_ring_size);
4296 			break;
4297 		}
4298 		prod = NEXT_RX(prod);
4299 	}
4300 	rxr->rx_prod = prod;
4301 }
4302 
4303 static void bnxt_alloc_one_rx_ring_page(struct bnxt *bp,
4304 					struct bnxt_rx_ring_info *rxr,
4305 					int ring_nr)
4306 {
4307 	u32 prod;
4308 	int i;
4309 
4310 	prod = rxr->rx_agg_prod;
4311 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4312 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
4313 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4314 				    ring_nr, i, bp->rx_ring_size);
4315 			break;
4316 		}
4317 		prod = NEXT_RX_AGG(prod);
4318 	}
4319 	rxr->rx_agg_prod = prod;
4320 }
4321 
4322 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4323 					struct bnxt_rx_ring_info *rxr)
4324 {
4325 	dma_addr_t mapping;
4326 	u8 *data;
4327 	int i;
4328 
4329 	for (i = 0; i < bp->max_tpa; i++) {
4330 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4331 					    GFP_KERNEL);
4332 		if (!data)
4333 			return -ENOMEM;
4334 
4335 		rxr->rx_tpa[i].data = data;
4336 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4337 		rxr->rx_tpa[i].mapping = mapping;
4338 	}
4339 
4340 	return 0;
4341 }
4342 
4343 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4344 {
4345 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4346 	int rc;
4347 
4348 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4349 
4350 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4351 		return 0;
4352 
4353 	bnxt_alloc_one_rx_ring_page(bp, rxr, ring_nr);
4354 
4355 	if (rxr->rx_tpa) {
4356 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4357 		if (rc)
4358 			return rc;
4359 	}
4360 	return 0;
4361 }
4362 
4363 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4364 				       struct bnxt_rx_ring_info *rxr)
4365 {
4366 	struct bnxt_ring_struct *ring;
4367 	u32 type;
4368 
4369 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4370 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4371 
4372 	if (NET_IP_ALIGN == 2)
4373 		type |= RX_BD_FLAGS_SOP;
4374 
4375 	ring = &rxr->rx_ring_struct;
4376 	bnxt_init_rxbd_pages(ring, type);
4377 	ring->fw_ring_id = INVALID_HW_RING_ID;
4378 }
4379 
4380 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4381 					   struct bnxt_rx_ring_info *rxr)
4382 {
4383 	struct bnxt_ring_struct *ring;
4384 	u32 type;
4385 
4386 	ring = &rxr->rx_agg_ring_struct;
4387 	ring->fw_ring_id = INVALID_HW_RING_ID;
4388 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4389 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4390 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4391 
4392 		bnxt_init_rxbd_pages(ring, type);
4393 	}
4394 }
4395 
4396 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4397 {
4398 	struct bnxt_rx_ring_info *rxr;
4399 
4400 	rxr = &bp->rx_ring[ring_nr];
4401 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4402 
4403 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4404 			     &rxr->bnapi->napi);
4405 
4406 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4407 		bpf_prog_add(bp->xdp_prog, 1);
4408 		rxr->xdp_prog = bp->xdp_prog;
4409 	}
4410 
4411 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4412 
4413 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4414 }
4415 
4416 static void bnxt_init_cp_rings(struct bnxt *bp)
4417 {
4418 	int i, j;
4419 
4420 	for (i = 0; i < bp->cp_nr_rings; i++) {
4421 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4422 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4423 
4424 		ring->fw_ring_id = INVALID_HW_RING_ID;
4425 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4426 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4427 		if (!cpr->cp_ring_arr)
4428 			continue;
4429 		for (j = 0; j < cpr->cp_ring_count; j++) {
4430 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4431 
4432 			ring = &cpr2->cp_ring_struct;
4433 			ring->fw_ring_id = INVALID_HW_RING_ID;
4434 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4435 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4436 		}
4437 	}
4438 }
4439 
4440 static int bnxt_init_rx_rings(struct bnxt *bp)
4441 {
4442 	int i, rc = 0;
4443 
4444 	if (BNXT_RX_PAGE_MODE(bp)) {
4445 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4446 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4447 	} else {
4448 		bp->rx_offset = BNXT_RX_OFFSET;
4449 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4450 	}
4451 
4452 	for (i = 0; i < bp->rx_nr_rings; i++) {
4453 		rc = bnxt_init_one_rx_ring(bp, i);
4454 		if (rc)
4455 			break;
4456 	}
4457 
4458 	return rc;
4459 }
4460 
4461 static int bnxt_init_tx_rings(struct bnxt *bp)
4462 {
4463 	u16 i;
4464 
4465 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4466 				   BNXT_MIN_TX_DESC_CNT);
4467 
4468 	for (i = 0; i < bp->tx_nr_rings; i++) {
4469 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4470 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4471 
4472 		ring->fw_ring_id = INVALID_HW_RING_ID;
4473 
4474 		if (i >= bp->tx_nr_rings_xdp)
4475 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4476 					     NETDEV_QUEUE_TYPE_TX,
4477 					     &txr->bnapi->napi);
4478 	}
4479 
4480 	return 0;
4481 }
4482 
4483 static void bnxt_free_ring_grps(struct bnxt *bp)
4484 {
4485 	kfree(bp->grp_info);
4486 	bp->grp_info = NULL;
4487 }
4488 
4489 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4490 {
4491 	int i;
4492 
4493 	if (irq_re_init) {
4494 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4495 				       sizeof(struct bnxt_ring_grp_info),
4496 				       GFP_KERNEL);
4497 		if (!bp->grp_info)
4498 			return -ENOMEM;
4499 	}
4500 	for (i = 0; i < bp->cp_nr_rings; i++) {
4501 		if (irq_re_init)
4502 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4503 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4504 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4505 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4506 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4507 	}
4508 	return 0;
4509 }
4510 
4511 static void bnxt_free_vnics(struct bnxt *bp)
4512 {
4513 	kfree(bp->vnic_info);
4514 	bp->vnic_info = NULL;
4515 	bp->nr_vnics = 0;
4516 }
4517 
4518 static int bnxt_alloc_vnics(struct bnxt *bp)
4519 {
4520 	int num_vnics = 1;
4521 
4522 #ifdef CONFIG_RFS_ACCEL
4523 	if (bp->flags & BNXT_FLAG_RFS) {
4524 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4525 			num_vnics++;
4526 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4527 			num_vnics += bp->rx_nr_rings;
4528 	}
4529 #endif
4530 
4531 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4532 		num_vnics++;
4533 
4534 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4535 				GFP_KERNEL);
4536 	if (!bp->vnic_info)
4537 		return -ENOMEM;
4538 
4539 	bp->nr_vnics = num_vnics;
4540 	return 0;
4541 }
4542 
4543 static void bnxt_init_vnics(struct bnxt *bp)
4544 {
4545 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4546 	int i;
4547 
4548 	for (i = 0; i < bp->nr_vnics; i++) {
4549 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4550 		int j;
4551 
4552 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4553 		vnic->vnic_id = i;
4554 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4555 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4556 
4557 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4558 
4559 		if (bp->vnic_info[i].rss_hash_key) {
4560 			if (i == BNXT_VNIC_DEFAULT) {
4561 				u8 *key = (void *)vnic->rss_hash_key;
4562 				int k;
4563 
4564 				if (!bp->rss_hash_key_valid &&
4565 				    !bp->rss_hash_key_updated) {
4566 					get_random_bytes(bp->rss_hash_key,
4567 							 HW_HASH_KEY_SIZE);
4568 					bp->rss_hash_key_updated = true;
4569 				}
4570 
4571 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4572 				       HW_HASH_KEY_SIZE);
4573 
4574 				if (!bp->rss_hash_key_updated)
4575 					continue;
4576 
4577 				bp->rss_hash_key_updated = false;
4578 				bp->rss_hash_key_valid = true;
4579 
4580 				bp->toeplitz_prefix = 0;
4581 				for (k = 0; k < 8; k++) {
4582 					bp->toeplitz_prefix <<= 8;
4583 					bp->toeplitz_prefix |= key[k];
4584 				}
4585 			} else {
4586 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4587 				       HW_HASH_KEY_SIZE);
4588 			}
4589 		}
4590 	}
4591 }
4592 
4593 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4594 {
4595 	int pages;
4596 
4597 	pages = ring_size / desc_per_pg;
4598 
4599 	if (!pages)
4600 		return 1;
4601 
4602 	pages++;
4603 
4604 	while (pages & (pages - 1))
4605 		pages++;
4606 
4607 	return pages;
4608 }
4609 
4610 void bnxt_set_tpa_flags(struct bnxt *bp)
4611 {
4612 	bp->flags &= ~BNXT_FLAG_TPA;
4613 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4614 		return;
4615 	if (bp->dev->features & NETIF_F_LRO)
4616 		bp->flags |= BNXT_FLAG_LRO;
4617 	else if (bp->dev->features & NETIF_F_GRO_HW)
4618 		bp->flags |= BNXT_FLAG_GRO;
4619 }
4620 
4621 static void bnxt_init_ring_params(struct bnxt *bp)
4622 {
4623 	unsigned int rx_size;
4624 
4625 	bp->rx_copybreak = BNXT_DEFAULT_RX_COPYBREAK;
4626 	/* Try to fit 4 chunks into a 4k page */
4627 	rx_size = SZ_1K -
4628 		NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4629 	bp->dev->cfg->hds_thresh = max(BNXT_DEFAULT_RX_COPYBREAK, rx_size);
4630 }
4631 
4632 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4633  * be set on entry.
4634  */
4635 void bnxt_set_ring_params(struct bnxt *bp)
4636 {
4637 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4638 	u32 agg_factor = 0, agg_ring_size = 0;
4639 
4640 	/* 8 for CRC and VLAN */
4641 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4642 
4643 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4644 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4645 
4646 	ring_size = bp->rx_ring_size;
4647 	bp->rx_agg_ring_size = 0;
4648 	bp->rx_agg_nr_pages = 0;
4649 
4650 	if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS)
4651 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4652 
4653 	bp->flags &= ~BNXT_FLAG_JUMBO;
4654 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4655 		u32 jumbo_factor;
4656 
4657 		bp->flags |= BNXT_FLAG_JUMBO;
4658 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4659 		if (jumbo_factor > agg_factor)
4660 			agg_factor = jumbo_factor;
4661 	}
4662 	if (agg_factor) {
4663 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4664 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4665 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4666 				    bp->rx_ring_size, ring_size);
4667 			bp->rx_ring_size = ring_size;
4668 		}
4669 		agg_ring_size = ring_size * agg_factor;
4670 
4671 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4672 							RX_DESC_CNT);
4673 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4674 			u32 tmp = agg_ring_size;
4675 
4676 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4677 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4678 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4679 				    tmp, agg_ring_size);
4680 		}
4681 		bp->rx_agg_ring_size = agg_ring_size;
4682 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4683 
4684 		if (BNXT_RX_PAGE_MODE(bp)) {
4685 			rx_space = PAGE_SIZE;
4686 			rx_size = PAGE_SIZE -
4687 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4688 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4689 		} else {
4690 			rx_size = max3(BNXT_DEFAULT_RX_COPYBREAK,
4691 				       bp->rx_copybreak,
4692 				       bp->dev->cfg_pending->hds_thresh);
4693 			rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN);
4694 			rx_space = rx_size + NET_SKB_PAD +
4695 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4696 		}
4697 	}
4698 
4699 	bp->rx_buf_use_size = rx_size;
4700 	bp->rx_buf_size = rx_space;
4701 
4702 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4703 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4704 
4705 	ring_size = bp->tx_ring_size;
4706 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4707 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4708 
4709 	max_rx_cmpl = bp->rx_ring_size;
4710 	/* MAX TPA needs to be added because TPA_START completions are
4711 	 * immediately recycled, so the TPA completions are not bound by
4712 	 * the RX ring size.
4713 	 */
4714 	if (bp->flags & BNXT_FLAG_TPA)
4715 		max_rx_cmpl += bp->max_tpa;
4716 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4717 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4718 	bp->cp_ring_size = ring_size;
4719 
4720 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4721 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4722 		bp->cp_nr_pages = MAX_CP_PAGES;
4723 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4724 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4725 			    ring_size, bp->cp_ring_size);
4726 	}
4727 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4728 	bp->cp_ring_mask = bp->cp_bit - 1;
4729 }
4730 
4731 /* Changing allocation mode of RX rings.
4732  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4733  */
4734 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4735 {
4736 	struct net_device *dev = bp->dev;
4737 
4738 	if (page_mode) {
4739 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
4740 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4741 
4742 		if (bp->xdp_prog->aux->xdp_has_frags)
4743 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4744 		else
4745 			dev->max_mtu =
4746 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4747 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4748 			bp->flags |= BNXT_FLAG_JUMBO;
4749 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4750 		} else {
4751 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4752 			bp->rx_skb_func = bnxt_rx_page_skb;
4753 		}
4754 		bp->rx_dir = DMA_BIDIRECTIONAL;
4755 	} else {
4756 		dev->max_mtu = bp->max_mtu;
4757 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4758 		bp->rx_dir = DMA_FROM_DEVICE;
4759 		bp->rx_skb_func = bnxt_rx_skb;
4760 	}
4761 }
4762 
4763 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4764 {
4765 	__bnxt_set_rx_skb_mode(bp, page_mode);
4766 
4767 	if (!page_mode) {
4768 		int rx, tx;
4769 
4770 		bnxt_get_max_rings(bp, &rx, &tx, true);
4771 		if (rx > 1) {
4772 			bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS;
4773 			bp->dev->hw_features |= NETIF_F_LRO;
4774 		}
4775 	}
4776 
4777 	/* Update LRO and GRO_HW availability */
4778 	netdev_update_features(bp->dev);
4779 }
4780 
4781 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4782 {
4783 	int i;
4784 	struct bnxt_vnic_info *vnic;
4785 	struct pci_dev *pdev = bp->pdev;
4786 
4787 	if (!bp->vnic_info)
4788 		return;
4789 
4790 	for (i = 0; i < bp->nr_vnics; i++) {
4791 		vnic = &bp->vnic_info[i];
4792 
4793 		kfree(vnic->fw_grp_ids);
4794 		vnic->fw_grp_ids = NULL;
4795 
4796 		kfree(vnic->uc_list);
4797 		vnic->uc_list = NULL;
4798 
4799 		if (vnic->mc_list) {
4800 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4801 					  vnic->mc_list, vnic->mc_list_mapping);
4802 			vnic->mc_list = NULL;
4803 		}
4804 
4805 		if (vnic->rss_table) {
4806 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4807 					  vnic->rss_table,
4808 					  vnic->rss_table_dma_addr);
4809 			vnic->rss_table = NULL;
4810 		}
4811 
4812 		vnic->rss_hash_key = NULL;
4813 		vnic->flags = 0;
4814 	}
4815 }
4816 
4817 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4818 {
4819 	int i, rc = 0, size;
4820 	struct bnxt_vnic_info *vnic;
4821 	struct pci_dev *pdev = bp->pdev;
4822 	int max_rings;
4823 
4824 	for (i = 0; i < bp->nr_vnics; i++) {
4825 		vnic = &bp->vnic_info[i];
4826 
4827 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4828 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4829 
4830 			if (mem_size > 0) {
4831 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4832 				if (!vnic->uc_list) {
4833 					rc = -ENOMEM;
4834 					goto out;
4835 				}
4836 			}
4837 		}
4838 
4839 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4840 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4841 			vnic->mc_list =
4842 				dma_alloc_coherent(&pdev->dev,
4843 						   vnic->mc_list_size,
4844 						   &vnic->mc_list_mapping,
4845 						   GFP_KERNEL);
4846 			if (!vnic->mc_list) {
4847 				rc = -ENOMEM;
4848 				goto out;
4849 			}
4850 		}
4851 
4852 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4853 			goto vnic_skip_grps;
4854 
4855 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4856 			max_rings = bp->rx_nr_rings;
4857 		else
4858 			max_rings = 1;
4859 
4860 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4861 		if (!vnic->fw_grp_ids) {
4862 			rc = -ENOMEM;
4863 			goto out;
4864 		}
4865 vnic_skip_grps:
4866 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4867 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4868 			continue;
4869 
4870 		/* Allocate rss table and hash key */
4871 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4872 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4873 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4874 
4875 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4876 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4877 						     vnic->rss_table_size,
4878 						     &vnic->rss_table_dma_addr,
4879 						     GFP_KERNEL);
4880 		if (!vnic->rss_table) {
4881 			rc = -ENOMEM;
4882 			goto out;
4883 		}
4884 
4885 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4886 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4887 	}
4888 	return 0;
4889 
4890 out:
4891 	return rc;
4892 }
4893 
4894 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4895 {
4896 	struct bnxt_hwrm_wait_token *token;
4897 
4898 	dma_pool_destroy(bp->hwrm_dma_pool);
4899 	bp->hwrm_dma_pool = NULL;
4900 
4901 	rcu_read_lock();
4902 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4903 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4904 	rcu_read_unlock();
4905 }
4906 
4907 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4908 {
4909 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4910 					    BNXT_HWRM_DMA_SIZE,
4911 					    BNXT_HWRM_DMA_ALIGN, 0);
4912 	if (!bp->hwrm_dma_pool)
4913 		return -ENOMEM;
4914 
4915 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4916 
4917 	return 0;
4918 }
4919 
4920 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4921 {
4922 	kfree(stats->hw_masks);
4923 	stats->hw_masks = NULL;
4924 	kfree(stats->sw_stats);
4925 	stats->sw_stats = NULL;
4926 	if (stats->hw_stats) {
4927 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
4928 				  stats->hw_stats_map);
4929 		stats->hw_stats = NULL;
4930 	}
4931 }
4932 
4933 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
4934 				bool alloc_masks)
4935 {
4936 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
4937 					     &stats->hw_stats_map, GFP_KERNEL);
4938 	if (!stats->hw_stats)
4939 		return -ENOMEM;
4940 
4941 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
4942 	if (!stats->sw_stats)
4943 		goto stats_mem_err;
4944 
4945 	if (alloc_masks) {
4946 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
4947 		if (!stats->hw_masks)
4948 			goto stats_mem_err;
4949 	}
4950 	return 0;
4951 
4952 stats_mem_err:
4953 	bnxt_free_stats_mem(bp, stats);
4954 	return -ENOMEM;
4955 }
4956 
4957 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
4958 {
4959 	int i;
4960 
4961 	for (i = 0; i < count; i++)
4962 		mask_arr[i] = mask;
4963 }
4964 
4965 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
4966 {
4967 	int i;
4968 
4969 	for (i = 0; i < count; i++)
4970 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
4971 }
4972 
4973 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
4974 				    struct bnxt_stats_mem *stats)
4975 {
4976 	struct hwrm_func_qstats_ext_output *resp;
4977 	struct hwrm_func_qstats_ext_input *req;
4978 	__le64 *hw_masks;
4979 	int rc;
4980 
4981 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
4982 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4983 		return -EOPNOTSUPP;
4984 
4985 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
4986 	if (rc)
4987 		return rc;
4988 
4989 	req->fid = cpu_to_le16(0xffff);
4990 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4991 
4992 	resp = hwrm_req_hold(bp, req);
4993 	rc = hwrm_req_send(bp, req);
4994 	if (!rc) {
4995 		hw_masks = &resp->rx_ucast_pkts;
4996 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
4997 	}
4998 	hwrm_req_drop(bp, req);
4999 	return rc;
5000 }
5001 
5002 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
5003 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
5004 
5005 static void bnxt_init_stats(struct bnxt *bp)
5006 {
5007 	struct bnxt_napi *bnapi = bp->bnapi[0];
5008 	struct bnxt_cp_ring_info *cpr;
5009 	struct bnxt_stats_mem *stats;
5010 	__le64 *rx_stats, *tx_stats;
5011 	int rc, rx_count, tx_count;
5012 	u64 *rx_masks, *tx_masks;
5013 	u64 mask;
5014 	u8 flags;
5015 
5016 	cpr = &bnapi->cp_ring;
5017 	stats = &cpr->stats;
5018 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
5019 	if (rc) {
5020 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5021 			mask = (1ULL << 48) - 1;
5022 		else
5023 			mask = -1ULL;
5024 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
5025 	}
5026 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
5027 		stats = &bp->port_stats;
5028 		rx_stats = stats->hw_stats;
5029 		rx_masks = stats->hw_masks;
5030 		rx_count = sizeof(struct rx_port_stats) / 8;
5031 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5032 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5033 		tx_count = sizeof(struct tx_port_stats) / 8;
5034 
5035 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
5036 		rc = bnxt_hwrm_port_qstats(bp, flags);
5037 		if (rc) {
5038 			mask = (1ULL << 40) - 1;
5039 
5040 			bnxt_fill_masks(rx_masks, mask, rx_count);
5041 			bnxt_fill_masks(tx_masks, mask, tx_count);
5042 		} else {
5043 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5044 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5045 			bnxt_hwrm_port_qstats(bp, 0);
5046 		}
5047 	}
5048 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5049 		stats = &bp->rx_port_stats_ext;
5050 		rx_stats = stats->hw_stats;
5051 		rx_masks = stats->hw_masks;
5052 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5053 		stats = &bp->tx_port_stats_ext;
5054 		tx_stats = stats->hw_stats;
5055 		tx_masks = stats->hw_masks;
5056 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5057 
5058 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5059 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5060 		if (rc) {
5061 			mask = (1ULL << 40) - 1;
5062 
5063 			bnxt_fill_masks(rx_masks, mask, rx_count);
5064 			if (tx_stats)
5065 				bnxt_fill_masks(tx_masks, mask, tx_count);
5066 		} else {
5067 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5068 			if (tx_stats)
5069 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5070 						   tx_count);
5071 			bnxt_hwrm_port_qstats_ext(bp, 0);
5072 		}
5073 	}
5074 }
5075 
5076 static void bnxt_free_port_stats(struct bnxt *bp)
5077 {
5078 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5079 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5080 
5081 	bnxt_free_stats_mem(bp, &bp->port_stats);
5082 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5083 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5084 }
5085 
5086 static void bnxt_free_ring_stats(struct bnxt *bp)
5087 {
5088 	int i;
5089 
5090 	if (!bp->bnapi)
5091 		return;
5092 
5093 	for (i = 0; i < bp->cp_nr_rings; i++) {
5094 		struct bnxt_napi *bnapi = bp->bnapi[i];
5095 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5096 
5097 		bnxt_free_stats_mem(bp, &cpr->stats);
5098 
5099 		kfree(cpr->sw_stats);
5100 		cpr->sw_stats = NULL;
5101 	}
5102 }
5103 
5104 static int bnxt_alloc_stats(struct bnxt *bp)
5105 {
5106 	u32 size, i;
5107 	int rc;
5108 
5109 	size = bp->hw_ring_stats_size;
5110 
5111 	for (i = 0; i < bp->cp_nr_rings; i++) {
5112 		struct bnxt_napi *bnapi = bp->bnapi[i];
5113 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5114 
5115 		cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL);
5116 		if (!cpr->sw_stats)
5117 			return -ENOMEM;
5118 
5119 		cpr->stats.len = size;
5120 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5121 		if (rc)
5122 			return rc;
5123 
5124 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5125 	}
5126 
5127 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5128 		return 0;
5129 
5130 	if (bp->port_stats.hw_stats)
5131 		goto alloc_ext_stats;
5132 
5133 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5134 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5135 	if (rc)
5136 		return rc;
5137 
5138 	bp->flags |= BNXT_FLAG_PORT_STATS;
5139 
5140 alloc_ext_stats:
5141 	/* Display extended statistics only if FW supports it */
5142 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5143 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5144 			return 0;
5145 
5146 	if (bp->rx_port_stats_ext.hw_stats)
5147 		goto alloc_tx_ext_stats;
5148 
5149 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5150 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5151 	/* Extended stats are optional */
5152 	if (rc)
5153 		return 0;
5154 
5155 alloc_tx_ext_stats:
5156 	if (bp->tx_port_stats_ext.hw_stats)
5157 		return 0;
5158 
5159 	if (bp->hwrm_spec_code >= 0x10902 ||
5160 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5161 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5162 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5163 		/* Extended stats are optional */
5164 		if (rc)
5165 			return 0;
5166 	}
5167 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5168 	return 0;
5169 }
5170 
5171 static void bnxt_clear_ring_indices(struct bnxt *bp)
5172 {
5173 	int i, j;
5174 
5175 	if (!bp->bnapi)
5176 		return;
5177 
5178 	for (i = 0; i < bp->cp_nr_rings; i++) {
5179 		struct bnxt_napi *bnapi = bp->bnapi[i];
5180 		struct bnxt_cp_ring_info *cpr;
5181 		struct bnxt_rx_ring_info *rxr;
5182 		struct bnxt_tx_ring_info *txr;
5183 
5184 		if (!bnapi)
5185 			continue;
5186 
5187 		cpr = &bnapi->cp_ring;
5188 		cpr->cp_raw_cons = 0;
5189 
5190 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5191 			txr->tx_prod = 0;
5192 			txr->tx_cons = 0;
5193 			txr->tx_hw_cons = 0;
5194 		}
5195 
5196 		rxr = bnapi->rx_ring;
5197 		if (rxr) {
5198 			rxr->rx_prod = 0;
5199 			rxr->rx_agg_prod = 0;
5200 			rxr->rx_sw_agg_prod = 0;
5201 			rxr->rx_next_cons = 0;
5202 		}
5203 		bnapi->events = 0;
5204 	}
5205 }
5206 
5207 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5208 {
5209 	u8 type = fltr->type, flags = fltr->flags;
5210 
5211 	INIT_LIST_HEAD(&fltr->list);
5212 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5213 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5214 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5215 }
5216 
5217 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5218 {
5219 	if (!list_empty(&fltr->list))
5220 		list_del_init(&fltr->list);
5221 }
5222 
5223 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5224 {
5225 	struct bnxt_filter_base *usr_fltr, *tmp;
5226 
5227 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5228 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5229 			continue;
5230 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5231 	}
5232 }
5233 
5234 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5235 {
5236 	hlist_del(&fltr->hash);
5237 	bnxt_del_one_usr_fltr(bp, fltr);
5238 	if (fltr->flags) {
5239 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5240 		bp->ntp_fltr_count--;
5241 	}
5242 	kfree(fltr);
5243 }
5244 
5245 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5246 {
5247 	int i;
5248 
5249 	netdev_assert_locked(bp->dev);
5250 
5251 	/* Under netdev instance lock and all our NAPIs have been disabled.
5252 	 * It's safe to delete the hash table.
5253 	 */
5254 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5255 		struct hlist_head *head;
5256 		struct hlist_node *tmp;
5257 		struct bnxt_ntuple_filter *fltr;
5258 
5259 		head = &bp->ntp_fltr_hash_tbl[i];
5260 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5261 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5262 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5263 				     !list_empty(&fltr->base.list)))
5264 				continue;
5265 			bnxt_del_fltr(bp, &fltr->base);
5266 		}
5267 	}
5268 	if (!all)
5269 		return;
5270 
5271 	bitmap_free(bp->ntp_fltr_bmap);
5272 	bp->ntp_fltr_bmap = NULL;
5273 	bp->ntp_fltr_count = 0;
5274 }
5275 
5276 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5277 {
5278 	int i, rc = 0;
5279 
5280 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5281 		return 0;
5282 
5283 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5284 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5285 
5286 	bp->ntp_fltr_count = 0;
5287 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5288 
5289 	if (!bp->ntp_fltr_bmap)
5290 		rc = -ENOMEM;
5291 
5292 	return rc;
5293 }
5294 
5295 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5296 {
5297 	int i;
5298 
5299 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5300 		struct hlist_head *head;
5301 		struct hlist_node *tmp;
5302 		struct bnxt_l2_filter *fltr;
5303 
5304 		head = &bp->l2_fltr_hash_tbl[i];
5305 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5306 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5307 				     !list_empty(&fltr->base.list)))
5308 				continue;
5309 			bnxt_del_fltr(bp, &fltr->base);
5310 		}
5311 	}
5312 }
5313 
5314 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5315 {
5316 	int i;
5317 
5318 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5319 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5320 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5321 }
5322 
5323 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5324 {
5325 	bnxt_free_vnic_attributes(bp);
5326 	bnxt_free_tx_rings(bp);
5327 	bnxt_free_rx_rings(bp);
5328 	bnxt_free_cp_rings(bp);
5329 	bnxt_free_all_cp_arrays(bp);
5330 	bnxt_free_ntp_fltrs(bp, false);
5331 	bnxt_free_l2_filters(bp, false);
5332 	if (irq_re_init) {
5333 		bnxt_free_ring_stats(bp);
5334 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5335 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5336 			bnxt_free_port_stats(bp);
5337 		bnxt_free_ring_grps(bp);
5338 		bnxt_free_vnics(bp);
5339 		kfree(bp->tx_ring_map);
5340 		bp->tx_ring_map = NULL;
5341 		kfree(bp->tx_ring);
5342 		bp->tx_ring = NULL;
5343 		kfree(bp->rx_ring);
5344 		bp->rx_ring = NULL;
5345 		kfree(bp->bnapi);
5346 		bp->bnapi = NULL;
5347 	} else {
5348 		bnxt_clear_ring_indices(bp);
5349 	}
5350 }
5351 
5352 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5353 {
5354 	int i, j, rc, size, arr_size;
5355 	void *bnapi;
5356 
5357 	if (irq_re_init) {
5358 		/* Allocate bnapi mem pointer array and mem block for
5359 		 * all queues
5360 		 */
5361 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5362 				bp->cp_nr_rings);
5363 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5364 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5365 		if (!bnapi)
5366 			return -ENOMEM;
5367 
5368 		bp->bnapi = bnapi;
5369 		bnapi += arr_size;
5370 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5371 			bp->bnapi[i] = bnapi;
5372 			bp->bnapi[i]->index = i;
5373 			bp->bnapi[i]->bp = bp;
5374 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5375 				struct bnxt_cp_ring_info *cpr =
5376 					&bp->bnapi[i]->cp_ring;
5377 
5378 				cpr->cp_ring_struct.ring_mem.flags =
5379 					BNXT_RMEM_RING_PTE_FLAG;
5380 			}
5381 		}
5382 
5383 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5384 				      sizeof(struct bnxt_rx_ring_info),
5385 				      GFP_KERNEL);
5386 		if (!bp->rx_ring)
5387 			return -ENOMEM;
5388 
5389 		for (i = 0; i < bp->rx_nr_rings; i++) {
5390 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5391 
5392 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5393 				rxr->rx_ring_struct.ring_mem.flags =
5394 					BNXT_RMEM_RING_PTE_FLAG;
5395 				rxr->rx_agg_ring_struct.ring_mem.flags =
5396 					BNXT_RMEM_RING_PTE_FLAG;
5397 			} else {
5398 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5399 			}
5400 			rxr->bnapi = bp->bnapi[i];
5401 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5402 		}
5403 
5404 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5405 				      sizeof(struct bnxt_tx_ring_info),
5406 				      GFP_KERNEL);
5407 		if (!bp->tx_ring)
5408 			return -ENOMEM;
5409 
5410 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5411 					  GFP_KERNEL);
5412 
5413 		if (!bp->tx_ring_map)
5414 			return -ENOMEM;
5415 
5416 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5417 			j = 0;
5418 		else
5419 			j = bp->rx_nr_rings;
5420 
5421 		for (i = 0; i < bp->tx_nr_rings; i++) {
5422 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5423 			struct bnxt_napi *bnapi2;
5424 
5425 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5426 				txr->tx_ring_struct.ring_mem.flags =
5427 					BNXT_RMEM_RING_PTE_FLAG;
5428 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5429 			if (i >= bp->tx_nr_rings_xdp) {
5430 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5431 
5432 				bnapi2 = bp->bnapi[k];
5433 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5434 				txr->tx_napi_idx =
5435 					BNXT_RING_TO_TC(bp, txr->txq_index);
5436 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5437 				bnapi2->tx_int = bnxt_tx_int;
5438 			} else {
5439 				bnapi2 = bp->bnapi[j];
5440 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5441 				bnapi2->tx_ring[0] = txr;
5442 				bnapi2->tx_int = bnxt_tx_int_xdp;
5443 				j++;
5444 			}
5445 			txr->bnapi = bnapi2;
5446 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5447 				txr->tx_cpr = &bnapi2->cp_ring;
5448 		}
5449 
5450 		rc = bnxt_alloc_stats(bp);
5451 		if (rc)
5452 			goto alloc_mem_err;
5453 		bnxt_init_stats(bp);
5454 
5455 		rc = bnxt_alloc_ntp_fltrs(bp);
5456 		if (rc)
5457 			goto alloc_mem_err;
5458 
5459 		rc = bnxt_alloc_vnics(bp);
5460 		if (rc)
5461 			goto alloc_mem_err;
5462 	}
5463 
5464 	rc = bnxt_alloc_all_cp_arrays(bp);
5465 	if (rc)
5466 		goto alloc_mem_err;
5467 
5468 	bnxt_init_ring_struct(bp);
5469 
5470 	rc = bnxt_alloc_rx_rings(bp);
5471 	if (rc)
5472 		goto alloc_mem_err;
5473 
5474 	rc = bnxt_alloc_tx_rings(bp);
5475 	if (rc)
5476 		goto alloc_mem_err;
5477 
5478 	rc = bnxt_alloc_cp_rings(bp);
5479 	if (rc)
5480 		goto alloc_mem_err;
5481 
5482 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5483 						  BNXT_VNIC_MCAST_FLAG |
5484 						  BNXT_VNIC_UCAST_FLAG;
5485 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5486 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5487 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5488 
5489 	rc = bnxt_alloc_vnic_attributes(bp);
5490 	if (rc)
5491 		goto alloc_mem_err;
5492 	return 0;
5493 
5494 alloc_mem_err:
5495 	bnxt_free_mem(bp, true);
5496 	return rc;
5497 }
5498 
5499 static void bnxt_disable_int(struct bnxt *bp)
5500 {
5501 	int i;
5502 
5503 	if (!bp->bnapi)
5504 		return;
5505 
5506 	for (i = 0; i < bp->cp_nr_rings; i++) {
5507 		struct bnxt_napi *bnapi = bp->bnapi[i];
5508 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5509 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5510 
5511 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5512 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5513 	}
5514 }
5515 
5516 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5517 {
5518 	struct bnxt_napi *bnapi = bp->bnapi[n];
5519 	struct bnxt_cp_ring_info *cpr;
5520 
5521 	cpr = &bnapi->cp_ring;
5522 	return cpr->cp_ring_struct.map_idx;
5523 }
5524 
5525 static void bnxt_disable_int_sync(struct bnxt *bp)
5526 {
5527 	int i;
5528 
5529 	if (!bp->irq_tbl)
5530 		return;
5531 
5532 	atomic_inc(&bp->intr_sem);
5533 
5534 	bnxt_disable_int(bp);
5535 	for (i = 0; i < bp->cp_nr_rings; i++) {
5536 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5537 
5538 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5539 	}
5540 }
5541 
5542 static void bnxt_enable_int(struct bnxt *bp)
5543 {
5544 	int i;
5545 
5546 	atomic_set(&bp->intr_sem, 0);
5547 	for (i = 0; i < bp->cp_nr_rings; i++) {
5548 		struct bnxt_napi *bnapi = bp->bnapi[i];
5549 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5550 
5551 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5552 	}
5553 }
5554 
5555 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5556 			    bool async_only)
5557 {
5558 	DECLARE_BITMAP(async_events_bmap, 256);
5559 	u32 *events = (u32 *)async_events_bmap;
5560 	struct hwrm_func_drv_rgtr_output *resp;
5561 	struct hwrm_func_drv_rgtr_input *req;
5562 	u32 flags;
5563 	int rc, i;
5564 
5565 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5566 	if (rc)
5567 		return rc;
5568 
5569 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5570 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5571 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5572 
5573 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5574 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5575 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5576 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5577 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5578 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5579 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5580 	if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2)
5581 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT;
5582 	req->flags = cpu_to_le32(flags);
5583 	req->ver_maj_8b = DRV_VER_MAJ;
5584 	req->ver_min_8b = DRV_VER_MIN;
5585 	req->ver_upd_8b = DRV_VER_UPD;
5586 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5587 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5588 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5589 
5590 	if (BNXT_PF(bp)) {
5591 		u32 data[8];
5592 		int i;
5593 
5594 		memset(data, 0, sizeof(data));
5595 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5596 			u16 cmd = bnxt_vf_req_snif[i];
5597 			unsigned int bit, idx;
5598 
5599 			idx = cmd / 32;
5600 			bit = cmd % 32;
5601 			data[idx] |= 1 << bit;
5602 		}
5603 
5604 		for (i = 0; i < 8; i++)
5605 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5606 
5607 		req->enables |=
5608 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5609 	}
5610 
5611 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5612 		req->flags |= cpu_to_le32(
5613 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5614 
5615 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5616 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5617 		u16 event_id = bnxt_async_events_arr[i];
5618 
5619 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5620 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5621 			continue;
5622 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5623 		    !bp->ptp_cfg)
5624 			continue;
5625 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5626 	}
5627 	if (bmap && bmap_size) {
5628 		for (i = 0; i < bmap_size; i++) {
5629 			if (test_bit(i, bmap))
5630 				__set_bit(i, async_events_bmap);
5631 		}
5632 	}
5633 	for (i = 0; i < 8; i++)
5634 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5635 
5636 	if (async_only)
5637 		req->enables =
5638 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5639 
5640 	resp = hwrm_req_hold(bp, req);
5641 	rc = hwrm_req_send(bp, req);
5642 	if (!rc) {
5643 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5644 		if (resp->flags &
5645 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5646 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5647 	}
5648 	hwrm_req_drop(bp, req);
5649 	return rc;
5650 }
5651 
5652 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5653 {
5654 	struct hwrm_func_drv_unrgtr_input *req;
5655 	int rc;
5656 
5657 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5658 		return 0;
5659 
5660 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5661 	if (rc)
5662 		return rc;
5663 	return hwrm_req_send(bp, req);
5664 }
5665 
5666 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5667 
5668 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5669 {
5670 	struct hwrm_tunnel_dst_port_free_input *req;
5671 	int rc;
5672 
5673 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5674 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5675 		return 0;
5676 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5677 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5678 		return 0;
5679 
5680 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5681 	if (rc)
5682 		return rc;
5683 
5684 	req->tunnel_type = tunnel_type;
5685 
5686 	switch (tunnel_type) {
5687 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5688 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5689 		bp->vxlan_port = 0;
5690 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5691 		break;
5692 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5693 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5694 		bp->nge_port = 0;
5695 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5696 		break;
5697 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5698 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5699 		bp->vxlan_gpe_port = 0;
5700 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5701 		break;
5702 	default:
5703 		break;
5704 	}
5705 
5706 	rc = hwrm_req_send(bp, req);
5707 	if (rc)
5708 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5709 			   rc);
5710 	if (bp->flags & BNXT_FLAG_TPA)
5711 		bnxt_set_tpa(bp, true);
5712 	return rc;
5713 }
5714 
5715 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5716 					   u8 tunnel_type)
5717 {
5718 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5719 	struct hwrm_tunnel_dst_port_alloc_input *req;
5720 	int rc;
5721 
5722 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5723 	if (rc)
5724 		return rc;
5725 
5726 	req->tunnel_type = tunnel_type;
5727 	req->tunnel_dst_port_val = port;
5728 
5729 	resp = hwrm_req_hold(bp, req);
5730 	rc = hwrm_req_send(bp, req);
5731 	if (rc) {
5732 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5733 			   rc);
5734 		goto err_out;
5735 	}
5736 
5737 	switch (tunnel_type) {
5738 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5739 		bp->vxlan_port = port;
5740 		bp->vxlan_fw_dst_port_id =
5741 			le16_to_cpu(resp->tunnel_dst_port_id);
5742 		break;
5743 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5744 		bp->nge_port = port;
5745 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5746 		break;
5747 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5748 		bp->vxlan_gpe_port = port;
5749 		bp->vxlan_gpe_fw_dst_port_id =
5750 			le16_to_cpu(resp->tunnel_dst_port_id);
5751 		break;
5752 	default:
5753 		break;
5754 	}
5755 	if (bp->flags & BNXT_FLAG_TPA)
5756 		bnxt_set_tpa(bp, true);
5757 
5758 err_out:
5759 	hwrm_req_drop(bp, req);
5760 	return rc;
5761 }
5762 
5763 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5764 {
5765 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5766 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5767 	int rc;
5768 
5769 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5770 	if (rc)
5771 		return rc;
5772 
5773 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5774 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5775 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5776 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5777 	}
5778 	req->mask = cpu_to_le32(vnic->rx_mask);
5779 	return hwrm_req_send_silent(bp, req);
5780 }
5781 
5782 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5783 {
5784 	if (!atomic_dec_and_test(&fltr->refcnt))
5785 		return;
5786 	spin_lock_bh(&bp->ntp_fltr_lock);
5787 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5788 		spin_unlock_bh(&bp->ntp_fltr_lock);
5789 		return;
5790 	}
5791 	hlist_del_rcu(&fltr->base.hash);
5792 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5793 	if (fltr->base.flags) {
5794 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5795 		bp->ntp_fltr_count--;
5796 	}
5797 	spin_unlock_bh(&bp->ntp_fltr_lock);
5798 	kfree_rcu(fltr, base.rcu);
5799 }
5800 
5801 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5802 						      struct bnxt_l2_key *key,
5803 						      u32 idx)
5804 {
5805 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5806 	struct bnxt_l2_filter *fltr;
5807 
5808 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5809 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5810 
5811 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5812 		    l2_key->vlan == key->vlan)
5813 			return fltr;
5814 	}
5815 	return NULL;
5816 }
5817 
5818 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5819 						    struct bnxt_l2_key *key,
5820 						    u32 idx)
5821 {
5822 	struct bnxt_l2_filter *fltr = NULL;
5823 
5824 	rcu_read_lock();
5825 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5826 	if (fltr)
5827 		atomic_inc(&fltr->refcnt);
5828 	rcu_read_unlock();
5829 	return fltr;
5830 }
5831 
5832 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5833 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5834 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5835 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5836 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5837 
5838 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5839 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5840 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5841 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5842 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5843 
5844 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5845 {
5846 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5847 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5848 			return sizeof(fkeys->addrs.v4addrs) +
5849 			       sizeof(fkeys->ports);
5850 
5851 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5852 			return sizeof(fkeys->addrs.v4addrs);
5853 	}
5854 
5855 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5856 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5857 			return sizeof(fkeys->addrs.v6addrs) +
5858 			       sizeof(fkeys->ports);
5859 
5860 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5861 			return sizeof(fkeys->addrs.v6addrs);
5862 	}
5863 
5864 	return 0;
5865 }
5866 
5867 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5868 			 const unsigned char *key)
5869 {
5870 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5871 	struct bnxt_ipv4_tuple tuple4;
5872 	struct bnxt_ipv6_tuple tuple6;
5873 	int i, j, len = 0;
5874 	u8 *four_tuple;
5875 
5876 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5877 	if (!len)
5878 		return 0;
5879 
5880 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5881 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5882 		tuple4.ports = fkeys->ports;
5883 		four_tuple = (unsigned char *)&tuple4;
5884 	} else {
5885 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5886 		tuple6.ports = fkeys->ports;
5887 		four_tuple = (unsigned char *)&tuple6;
5888 	}
5889 
5890 	for (i = 0, j = 8; i < len; i++, j++) {
5891 		u8 byte = four_tuple[i];
5892 		int bit;
5893 
5894 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5895 			if (byte & 0x80)
5896 				hash ^= prefix;
5897 		}
5898 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5899 	}
5900 
5901 	/* The valid part of the hash is in the upper 32 bits. */
5902 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5903 }
5904 
5905 #ifdef CONFIG_RFS_ACCEL
5906 static struct bnxt_l2_filter *
5907 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5908 {
5909 	struct bnxt_l2_filter *fltr;
5910 	u32 idx;
5911 
5912 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5913 	      BNXT_L2_FLTR_HASH_MASK;
5914 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5915 	return fltr;
5916 }
5917 #endif
5918 
5919 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
5920 			       struct bnxt_l2_key *key, u32 idx)
5921 {
5922 	struct hlist_head *head;
5923 
5924 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
5925 	fltr->l2_key.vlan = key->vlan;
5926 	fltr->base.type = BNXT_FLTR_TYPE_L2;
5927 	if (fltr->base.flags) {
5928 		int bit_id;
5929 
5930 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
5931 						 bp->max_fltr, 0);
5932 		if (bit_id < 0)
5933 			return -ENOMEM;
5934 		fltr->base.sw_id = (u16)bit_id;
5935 		bp->ntp_fltr_count++;
5936 	}
5937 	head = &bp->l2_fltr_hash_tbl[idx];
5938 	hlist_add_head_rcu(&fltr->base.hash, head);
5939 	bnxt_insert_usr_fltr(bp, &fltr->base);
5940 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
5941 	atomic_set(&fltr->refcnt, 1);
5942 	return 0;
5943 }
5944 
5945 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
5946 						   struct bnxt_l2_key *key,
5947 						   gfp_t gfp)
5948 {
5949 	struct bnxt_l2_filter *fltr;
5950 	u32 idx;
5951 	int rc;
5952 
5953 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5954 	      BNXT_L2_FLTR_HASH_MASK;
5955 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5956 	if (fltr)
5957 		return fltr;
5958 
5959 	fltr = kzalloc(sizeof(*fltr), gfp);
5960 	if (!fltr)
5961 		return ERR_PTR(-ENOMEM);
5962 	spin_lock_bh(&bp->ntp_fltr_lock);
5963 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5964 	spin_unlock_bh(&bp->ntp_fltr_lock);
5965 	if (rc) {
5966 		bnxt_del_l2_filter(bp, fltr);
5967 		fltr = ERR_PTR(rc);
5968 	}
5969 	return fltr;
5970 }
5971 
5972 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
5973 						struct bnxt_l2_key *key,
5974 						u16 flags)
5975 {
5976 	struct bnxt_l2_filter *fltr;
5977 	u32 idx;
5978 	int rc;
5979 
5980 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5981 	      BNXT_L2_FLTR_HASH_MASK;
5982 	spin_lock_bh(&bp->ntp_fltr_lock);
5983 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5984 	if (fltr) {
5985 		fltr = ERR_PTR(-EEXIST);
5986 		goto l2_filter_exit;
5987 	}
5988 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
5989 	if (!fltr) {
5990 		fltr = ERR_PTR(-ENOMEM);
5991 		goto l2_filter_exit;
5992 	}
5993 	fltr->base.flags = flags;
5994 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5995 	if (rc) {
5996 		spin_unlock_bh(&bp->ntp_fltr_lock);
5997 		bnxt_del_l2_filter(bp, fltr);
5998 		return ERR_PTR(rc);
5999 	}
6000 
6001 l2_filter_exit:
6002 	spin_unlock_bh(&bp->ntp_fltr_lock);
6003 	return fltr;
6004 }
6005 
6006 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
6007 {
6008 #ifdef CONFIG_BNXT_SRIOV
6009 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
6010 
6011 	return vf->fw_fid;
6012 #else
6013 	return INVALID_HW_RING_ID;
6014 #endif
6015 }
6016 
6017 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6018 {
6019 	struct hwrm_cfa_l2_filter_free_input *req;
6020 	u16 target_id = 0xffff;
6021 	int rc;
6022 
6023 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6024 		struct bnxt_pf_info *pf = &bp->pf;
6025 
6026 		if (fltr->base.vf_idx >= pf->active_vfs)
6027 			return -EINVAL;
6028 
6029 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6030 		if (target_id == INVALID_HW_RING_ID)
6031 			return -EINVAL;
6032 	}
6033 
6034 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
6035 	if (rc)
6036 		return rc;
6037 
6038 	req->target_id = cpu_to_le16(target_id);
6039 	req->l2_filter_id = fltr->base.filter_id;
6040 	return hwrm_req_send(bp, req);
6041 }
6042 
6043 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6044 {
6045 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6046 	struct hwrm_cfa_l2_filter_alloc_input *req;
6047 	u16 target_id = 0xffff;
6048 	int rc;
6049 
6050 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6051 		struct bnxt_pf_info *pf = &bp->pf;
6052 
6053 		if (fltr->base.vf_idx >= pf->active_vfs)
6054 			return -EINVAL;
6055 
6056 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6057 	}
6058 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6059 	if (rc)
6060 		return rc;
6061 
6062 	req->target_id = cpu_to_le16(target_id);
6063 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6064 
6065 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6066 		req->flags |=
6067 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6068 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6069 	req->enables =
6070 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6071 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6072 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6073 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6074 	eth_broadcast_addr(req->l2_addr_mask);
6075 
6076 	if (fltr->l2_key.vlan) {
6077 		req->enables |=
6078 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6079 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6080 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6081 		req->num_vlans = 1;
6082 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6083 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6084 	}
6085 
6086 	resp = hwrm_req_hold(bp, req);
6087 	rc = hwrm_req_send(bp, req);
6088 	if (!rc) {
6089 		fltr->base.filter_id = resp->l2_filter_id;
6090 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6091 	}
6092 	hwrm_req_drop(bp, req);
6093 	return rc;
6094 }
6095 
6096 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6097 				     struct bnxt_ntuple_filter *fltr)
6098 {
6099 	struct hwrm_cfa_ntuple_filter_free_input *req;
6100 	int rc;
6101 
6102 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6103 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6104 	if (rc)
6105 		return rc;
6106 
6107 	req->ntuple_filter_id = fltr->base.filter_id;
6108 	return hwrm_req_send(bp, req);
6109 }
6110 
6111 #define BNXT_NTP_FLTR_FLAGS					\
6112 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6113 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6114 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6115 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6116 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6117 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6118 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6119 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6120 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6121 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6122 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6123 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6124 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6125 
6126 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6127 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6128 
6129 void bnxt_fill_ipv6_mask(__be32 mask[4])
6130 {
6131 	int i;
6132 
6133 	for (i = 0; i < 4; i++)
6134 		mask[i] = cpu_to_be32(~0);
6135 }
6136 
6137 static void
6138 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6139 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6140 			  struct bnxt_ntuple_filter *fltr)
6141 {
6142 	u16 rxq = fltr->base.rxq;
6143 
6144 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6145 		struct ethtool_rxfh_context *ctx;
6146 		struct bnxt_rss_ctx *rss_ctx;
6147 		struct bnxt_vnic_info *vnic;
6148 
6149 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6150 			      fltr->base.fw_vnic_id);
6151 		if (ctx) {
6152 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6153 			vnic = &rss_ctx->vnic;
6154 
6155 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6156 		}
6157 		return;
6158 	}
6159 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6160 		struct bnxt_vnic_info *vnic;
6161 		u32 enables;
6162 
6163 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6164 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6165 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6166 		req->enables |= cpu_to_le32(enables);
6167 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6168 	} else {
6169 		u32 flags;
6170 
6171 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6172 		req->flags |= cpu_to_le32(flags);
6173 		req->dst_id = cpu_to_le16(rxq);
6174 	}
6175 }
6176 
6177 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6178 				      struct bnxt_ntuple_filter *fltr)
6179 {
6180 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6181 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6182 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6183 	struct flow_keys *keys = &fltr->fkeys;
6184 	struct bnxt_l2_filter *l2_fltr;
6185 	struct bnxt_vnic_info *vnic;
6186 	int rc;
6187 
6188 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6189 	if (rc)
6190 		return rc;
6191 
6192 	l2_fltr = fltr->l2_fltr;
6193 	req->l2_filter_id = l2_fltr->base.filter_id;
6194 
6195 	if (fltr->base.flags & BNXT_ACT_DROP) {
6196 		req->flags =
6197 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6198 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6199 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6200 	} else {
6201 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6202 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6203 	}
6204 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6205 
6206 	req->ethertype = htons(ETH_P_IP);
6207 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6208 	req->ip_protocol = keys->basic.ip_proto;
6209 
6210 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6211 		req->ethertype = htons(ETH_P_IPV6);
6212 		req->ip_addr_type =
6213 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6214 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6215 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6216 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6217 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6218 	} else {
6219 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6220 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6221 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6222 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6223 	}
6224 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6225 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6226 		req->tunnel_type =
6227 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6228 	}
6229 
6230 	req->src_port = keys->ports.src;
6231 	req->src_port_mask = masks->ports.src;
6232 	req->dst_port = keys->ports.dst;
6233 	req->dst_port_mask = masks->ports.dst;
6234 
6235 	resp = hwrm_req_hold(bp, req);
6236 	rc = hwrm_req_send(bp, req);
6237 	if (!rc)
6238 		fltr->base.filter_id = resp->ntuple_filter_id;
6239 	hwrm_req_drop(bp, req);
6240 	return rc;
6241 }
6242 
6243 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6244 				     const u8 *mac_addr)
6245 {
6246 	struct bnxt_l2_filter *fltr;
6247 	struct bnxt_l2_key key;
6248 	int rc;
6249 
6250 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6251 	key.vlan = 0;
6252 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6253 	if (IS_ERR(fltr))
6254 		return PTR_ERR(fltr);
6255 
6256 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6257 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6258 	if (rc)
6259 		bnxt_del_l2_filter(bp, fltr);
6260 	else
6261 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6262 	return rc;
6263 }
6264 
6265 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6266 {
6267 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6268 
6269 	/* Any associated ntuple filters will also be cleared by firmware. */
6270 	for (i = 0; i < num_of_vnics; i++) {
6271 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6272 
6273 		for (j = 0; j < vnic->uc_filter_count; j++) {
6274 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6275 
6276 			bnxt_hwrm_l2_filter_free(bp, fltr);
6277 			bnxt_del_l2_filter(bp, fltr);
6278 		}
6279 		vnic->uc_filter_count = 0;
6280 	}
6281 }
6282 
6283 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6284 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6285 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6286 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6287 
6288 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6289 					   struct hwrm_vnic_tpa_cfg_input *req)
6290 {
6291 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6292 
6293 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6294 		return;
6295 
6296 	if (bp->vxlan_port)
6297 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6298 	if (bp->vxlan_gpe_port)
6299 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6300 	if (bp->nge_port)
6301 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6302 
6303 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6304 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6305 }
6306 
6307 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6308 			   u32 tpa_flags)
6309 {
6310 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6311 	struct hwrm_vnic_tpa_cfg_input *req;
6312 	int rc;
6313 
6314 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6315 		return 0;
6316 
6317 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6318 	if (rc)
6319 		return rc;
6320 
6321 	if (tpa_flags) {
6322 		u16 mss = bp->dev->mtu - 40;
6323 		u32 nsegs, n, segs = 0, flags;
6324 
6325 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6326 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6327 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6328 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6329 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6330 		if (tpa_flags & BNXT_FLAG_GRO)
6331 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6332 
6333 		req->flags = cpu_to_le32(flags);
6334 
6335 		req->enables =
6336 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6337 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6338 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6339 
6340 		/* Number of segs are log2 units, and first packet is not
6341 		 * included as part of this units.
6342 		 */
6343 		if (mss <= BNXT_RX_PAGE_SIZE) {
6344 			n = BNXT_RX_PAGE_SIZE / mss;
6345 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6346 		} else {
6347 			n = mss / BNXT_RX_PAGE_SIZE;
6348 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6349 				n++;
6350 			nsegs = (MAX_SKB_FRAGS - n) / n;
6351 		}
6352 
6353 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6354 			segs = MAX_TPA_SEGS_P5;
6355 			max_aggs = bp->max_tpa;
6356 		} else {
6357 			segs = ilog2(nsegs);
6358 		}
6359 		req->max_agg_segs = cpu_to_le16(segs);
6360 		req->max_aggs = cpu_to_le16(max_aggs);
6361 
6362 		req->min_agg_len = cpu_to_le32(512);
6363 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6364 	}
6365 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6366 
6367 	return hwrm_req_send(bp, req);
6368 }
6369 
6370 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6371 {
6372 	struct bnxt_ring_grp_info *grp_info;
6373 
6374 	grp_info = &bp->grp_info[ring->grp_idx];
6375 	return grp_info->cp_fw_ring_id;
6376 }
6377 
6378 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6379 {
6380 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6381 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6382 	else
6383 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6384 }
6385 
6386 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6387 {
6388 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6389 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6390 	else
6391 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6392 }
6393 
6394 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6395 {
6396 	int entries;
6397 
6398 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6399 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6400 	else
6401 		entries = HW_HASH_INDEX_SIZE;
6402 
6403 	bp->rss_indir_tbl_entries = entries;
6404 	bp->rss_indir_tbl =
6405 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6406 	if (!bp->rss_indir_tbl)
6407 		return -ENOMEM;
6408 
6409 	return 0;
6410 }
6411 
6412 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6413 				 struct ethtool_rxfh_context *rss_ctx)
6414 {
6415 	u16 max_rings, max_entries, pad, i;
6416 	u32 *rss_indir_tbl;
6417 
6418 	if (!bp->rx_nr_rings)
6419 		return;
6420 
6421 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6422 		max_rings = bp->rx_nr_rings - 1;
6423 	else
6424 		max_rings = bp->rx_nr_rings;
6425 
6426 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6427 	if (rss_ctx)
6428 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6429 	else
6430 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6431 
6432 	for (i = 0; i < max_entries; i++)
6433 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6434 
6435 	pad = bp->rss_indir_tbl_entries - max_entries;
6436 	if (pad)
6437 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6438 }
6439 
6440 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6441 {
6442 	u32 i, tbl_size, max_ring = 0;
6443 
6444 	if (!bp->rss_indir_tbl)
6445 		return 0;
6446 
6447 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6448 	for (i = 0; i < tbl_size; i++)
6449 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6450 	return max_ring;
6451 }
6452 
6453 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6454 {
6455 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6456 		if (!rx_rings)
6457 			return 0;
6458 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6459 					       BNXT_RSS_TABLE_ENTRIES_P5);
6460 	}
6461 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6462 		return 2;
6463 	return 1;
6464 }
6465 
6466 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6467 {
6468 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6469 	u16 i, j;
6470 
6471 	/* Fill the RSS indirection table with ring group ids */
6472 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6473 		if (!no_rss)
6474 			j = bp->rss_indir_tbl[i];
6475 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6476 	}
6477 }
6478 
6479 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6480 				    struct bnxt_vnic_info *vnic)
6481 {
6482 	__le16 *ring_tbl = vnic->rss_table;
6483 	struct bnxt_rx_ring_info *rxr;
6484 	u16 tbl_size, i;
6485 
6486 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6487 
6488 	for (i = 0; i < tbl_size; i++) {
6489 		u16 ring_id, j;
6490 
6491 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6492 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6493 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6494 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6495 		else
6496 			j = bp->rss_indir_tbl[i];
6497 		rxr = &bp->rx_ring[j];
6498 
6499 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6500 		*ring_tbl++ = cpu_to_le16(ring_id);
6501 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6502 		*ring_tbl++ = cpu_to_le16(ring_id);
6503 	}
6504 }
6505 
6506 static void
6507 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6508 			 struct bnxt_vnic_info *vnic)
6509 {
6510 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6511 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6512 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6513 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6514 	} else {
6515 		bnxt_fill_hw_rss_tbl(bp, vnic);
6516 	}
6517 
6518 	if (bp->rss_hash_delta) {
6519 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6520 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6521 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6522 		else
6523 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6524 	} else {
6525 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6526 	}
6527 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6528 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6529 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6530 }
6531 
6532 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6533 				  bool set_rss)
6534 {
6535 	struct hwrm_vnic_rss_cfg_input *req;
6536 	int rc;
6537 
6538 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6539 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6540 		return 0;
6541 
6542 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6543 	if (rc)
6544 		return rc;
6545 
6546 	if (set_rss)
6547 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6548 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6549 	return hwrm_req_send(bp, req);
6550 }
6551 
6552 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6553 				     struct bnxt_vnic_info *vnic, bool set_rss)
6554 {
6555 	struct hwrm_vnic_rss_cfg_input *req;
6556 	dma_addr_t ring_tbl_map;
6557 	u32 i, nr_ctxs;
6558 	int rc;
6559 
6560 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6561 	if (rc)
6562 		return rc;
6563 
6564 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6565 	if (!set_rss)
6566 		return hwrm_req_send(bp, req);
6567 
6568 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6569 	ring_tbl_map = vnic->rss_table_dma_addr;
6570 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6571 
6572 	hwrm_req_hold(bp, req);
6573 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6574 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6575 		req->ring_table_pair_index = i;
6576 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6577 		rc = hwrm_req_send(bp, req);
6578 		if (rc)
6579 			goto exit;
6580 	}
6581 
6582 exit:
6583 	hwrm_req_drop(bp, req);
6584 	return rc;
6585 }
6586 
6587 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6588 {
6589 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6590 	struct hwrm_vnic_rss_qcfg_output *resp;
6591 	struct hwrm_vnic_rss_qcfg_input *req;
6592 
6593 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6594 		return;
6595 
6596 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6597 	/* all contexts configured to same hash_type, zero always exists */
6598 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6599 	resp = hwrm_req_hold(bp, req);
6600 	if (!hwrm_req_send(bp, req)) {
6601 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6602 		bp->rss_hash_delta = 0;
6603 	}
6604 	hwrm_req_drop(bp, req);
6605 }
6606 
6607 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6608 {
6609 	u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh;
6610 	struct hwrm_vnic_plcmodes_cfg_input *req;
6611 	int rc;
6612 
6613 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6614 	if (rc)
6615 		return rc;
6616 
6617 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6618 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6619 	req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6620 
6621 	if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
6622 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6623 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6624 		req->enables |=
6625 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6626 		req->hds_threshold = cpu_to_le16(hds_thresh);
6627 	}
6628 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6629 	return hwrm_req_send(bp, req);
6630 }
6631 
6632 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6633 					struct bnxt_vnic_info *vnic,
6634 					u16 ctx_idx)
6635 {
6636 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6637 
6638 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6639 		return;
6640 
6641 	req->rss_cos_lb_ctx_id =
6642 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6643 
6644 	hwrm_req_send(bp, req);
6645 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6646 }
6647 
6648 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6649 {
6650 	int i, j;
6651 
6652 	for (i = 0; i < bp->nr_vnics; i++) {
6653 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6654 
6655 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6656 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6657 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6658 		}
6659 	}
6660 	bp->rsscos_nr_ctxs = 0;
6661 }
6662 
6663 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6664 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6665 {
6666 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6667 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6668 	int rc;
6669 
6670 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6671 	if (rc)
6672 		return rc;
6673 
6674 	resp = hwrm_req_hold(bp, req);
6675 	rc = hwrm_req_send(bp, req);
6676 	if (!rc)
6677 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6678 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6679 	hwrm_req_drop(bp, req);
6680 
6681 	return rc;
6682 }
6683 
6684 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6685 {
6686 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6687 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6688 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6689 }
6690 
6691 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6692 {
6693 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6694 	struct hwrm_vnic_cfg_input *req;
6695 	unsigned int ring = 0, grp_idx;
6696 	u16 def_vlan = 0;
6697 	int rc;
6698 
6699 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6700 	if (rc)
6701 		return rc;
6702 
6703 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6704 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6705 
6706 		req->default_rx_ring_id =
6707 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6708 		req->default_cmpl_ring_id =
6709 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6710 		req->enables =
6711 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6712 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6713 		goto vnic_mru;
6714 	}
6715 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6716 	/* Only RSS support for now TBD: COS & LB */
6717 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6718 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6719 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6720 					   VNIC_CFG_REQ_ENABLES_MRU);
6721 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6722 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6723 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6724 					   VNIC_CFG_REQ_ENABLES_MRU);
6725 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6726 	} else {
6727 		req->rss_rule = cpu_to_le16(0xffff);
6728 	}
6729 
6730 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6731 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6732 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6733 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6734 	} else {
6735 		req->cos_rule = cpu_to_le16(0xffff);
6736 	}
6737 
6738 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6739 		ring = 0;
6740 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6741 		ring = vnic->vnic_id - 1;
6742 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6743 		ring = bp->rx_nr_rings - 1;
6744 
6745 	grp_idx = bp->rx_ring[ring].bnapi->index;
6746 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6747 	req->lb_rule = cpu_to_le16(0xffff);
6748 vnic_mru:
6749 	vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
6750 	req->mru = cpu_to_le16(vnic->mru);
6751 
6752 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6753 #ifdef CONFIG_BNXT_SRIOV
6754 	if (BNXT_VF(bp))
6755 		def_vlan = bp->vf.vlan;
6756 #endif
6757 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6758 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6759 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6760 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6761 
6762 	return hwrm_req_send(bp, req);
6763 }
6764 
6765 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6766 				    struct bnxt_vnic_info *vnic)
6767 {
6768 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6769 		struct hwrm_vnic_free_input *req;
6770 
6771 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6772 			return;
6773 
6774 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6775 
6776 		hwrm_req_send(bp, req);
6777 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6778 	}
6779 }
6780 
6781 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6782 {
6783 	u16 i;
6784 
6785 	for (i = 0; i < bp->nr_vnics; i++)
6786 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6787 }
6788 
6789 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6790 			 unsigned int start_rx_ring_idx,
6791 			 unsigned int nr_rings)
6792 {
6793 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6794 	struct hwrm_vnic_alloc_output *resp;
6795 	struct hwrm_vnic_alloc_input *req;
6796 	int rc;
6797 
6798 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6799 	if (rc)
6800 		return rc;
6801 
6802 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6803 		goto vnic_no_ring_grps;
6804 
6805 	/* map ring groups to this vnic */
6806 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6807 		grp_idx = bp->rx_ring[i].bnapi->index;
6808 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6809 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6810 				   j, nr_rings);
6811 			break;
6812 		}
6813 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6814 	}
6815 
6816 vnic_no_ring_grps:
6817 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6818 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6819 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6820 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6821 
6822 	resp = hwrm_req_hold(bp, req);
6823 	rc = hwrm_req_send(bp, req);
6824 	if (!rc)
6825 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6826 	hwrm_req_drop(bp, req);
6827 	return rc;
6828 }
6829 
6830 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6831 {
6832 	struct hwrm_vnic_qcaps_output *resp;
6833 	struct hwrm_vnic_qcaps_input *req;
6834 	int rc;
6835 
6836 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6837 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6838 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6839 	if (bp->hwrm_spec_code < 0x10600)
6840 		return 0;
6841 
6842 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6843 	if (rc)
6844 		return rc;
6845 
6846 	resp = hwrm_req_hold(bp, req);
6847 	rc = hwrm_req_send(bp, req);
6848 	if (!rc) {
6849 		u32 flags = le32_to_cpu(resp->flags);
6850 
6851 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6852 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6853 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6854 		if (flags &
6855 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6856 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6857 
6858 		/* Older P5 fw before EXT_HW_STATS support did not set
6859 		 * VLAN_STRIP_CAP properly.
6860 		 */
6861 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6862 		    (BNXT_CHIP_P5(bp) &&
6863 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6864 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6865 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6866 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6867 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6868 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6869 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6870 		if (bp->max_tpa_v2) {
6871 			if (BNXT_CHIP_P5(bp))
6872 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6873 			else
6874 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6875 		}
6876 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6877 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6878 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6879 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6880 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6881 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6882 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6883 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6884 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6885 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6886 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
6887 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
6888 	}
6889 	hwrm_req_drop(bp, req);
6890 	return rc;
6891 }
6892 
6893 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6894 {
6895 	struct hwrm_ring_grp_alloc_output *resp;
6896 	struct hwrm_ring_grp_alloc_input *req;
6897 	int rc;
6898 	u16 i;
6899 
6900 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6901 		return 0;
6902 
6903 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6904 	if (rc)
6905 		return rc;
6906 
6907 	resp = hwrm_req_hold(bp, req);
6908 	for (i = 0; i < bp->rx_nr_rings; i++) {
6909 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
6910 
6911 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
6912 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
6913 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
6914 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
6915 
6916 		rc = hwrm_req_send(bp, req);
6917 
6918 		if (rc)
6919 			break;
6920 
6921 		bp->grp_info[grp_idx].fw_grp_id =
6922 			le32_to_cpu(resp->ring_group_id);
6923 	}
6924 	hwrm_req_drop(bp, req);
6925 	return rc;
6926 }
6927 
6928 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
6929 {
6930 	struct hwrm_ring_grp_free_input *req;
6931 	u16 i;
6932 
6933 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
6934 		return;
6935 
6936 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
6937 		return;
6938 
6939 	hwrm_req_hold(bp, req);
6940 	for (i = 0; i < bp->cp_nr_rings; i++) {
6941 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
6942 			continue;
6943 		req->ring_group_id =
6944 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
6945 
6946 		hwrm_req_send(bp, req);
6947 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
6948 	}
6949 	hwrm_req_drop(bp, req);
6950 }
6951 
6952 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type,
6953 				       struct hwrm_ring_alloc_input *req,
6954 				       struct bnxt_ring_struct *ring)
6955 {
6956 	struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx];
6957 	u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID |
6958 		      RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID;
6959 
6960 	if (ring_type == HWRM_RING_ALLOC_AGG) {
6961 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
6962 		req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
6963 		req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
6964 		enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID;
6965 	} else {
6966 		req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
6967 		if (NET_IP_ALIGN == 2)
6968 			req->flags =
6969 				cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD);
6970 	}
6971 	req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6972 	req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
6973 	req->enables |= cpu_to_le32(enables);
6974 }
6975 
6976 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
6977 				    struct bnxt_ring_struct *ring,
6978 				    u32 ring_type, u32 map_index)
6979 {
6980 	struct hwrm_ring_alloc_output *resp;
6981 	struct hwrm_ring_alloc_input *req;
6982 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
6983 	struct bnxt_ring_grp_info *grp_info;
6984 	int rc, err = 0;
6985 	u16 ring_id;
6986 
6987 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
6988 	if (rc)
6989 		goto exit;
6990 
6991 	req->enables = 0;
6992 	if (rmem->nr_pages > 1) {
6993 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
6994 		/* Page size is in log2 units */
6995 		req->page_size = BNXT_PAGE_SHIFT;
6996 		req->page_tbl_depth = 1;
6997 	} else {
6998 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
6999 	}
7000 	req->fbo = 0;
7001 	/* Association of ring index with doorbell index and MSIX number */
7002 	req->logical_id = cpu_to_le16(map_index);
7003 
7004 	switch (ring_type) {
7005 	case HWRM_RING_ALLOC_TX: {
7006 		struct bnxt_tx_ring_info *txr;
7007 		u16 flags = 0;
7008 
7009 		txr = container_of(ring, struct bnxt_tx_ring_info,
7010 				   tx_ring_struct);
7011 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
7012 		/* Association of transmit ring with completion ring */
7013 		grp_info = &bp->grp_info[ring->grp_idx];
7014 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
7015 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
7016 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7017 		req->queue_id = cpu_to_le16(ring->queue_id);
7018 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
7019 			req->cmpl_coal_cnt =
7020 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
7021 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
7022 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
7023 		req->flags = cpu_to_le16(flags);
7024 		break;
7025 	}
7026 	case HWRM_RING_ALLOC_RX:
7027 	case HWRM_RING_ALLOC_AGG:
7028 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
7029 		req->length = (ring_type == HWRM_RING_ALLOC_RX) ?
7030 			      cpu_to_le32(bp->rx_ring_mask + 1) :
7031 			      cpu_to_le32(bp->rx_agg_ring_mask + 1);
7032 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7033 			bnxt_set_rx_ring_params_p5(bp, ring_type, req, ring);
7034 		break;
7035 	case HWRM_RING_ALLOC_CMPL:
7036 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7037 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7038 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7039 			/* Association of cp ring with nq */
7040 			grp_info = &bp->grp_info[map_index];
7041 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7042 			req->cq_handle = cpu_to_le64(ring->handle);
7043 			req->enables |= cpu_to_le32(
7044 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7045 		} else {
7046 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7047 		}
7048 		break;
7049 	case HWRM_RING_ALLOC_NQ:
7050 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7051 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7052 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7053 		break;
7054 	default:
7055 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7056 			   ring_type);
7057 		return -1;
7058 	}
7059 
7060 	resp = hwrm_req_hold(bp, req);
7061 	rc = hwrm_req_send(bp, req);
7062 	err = le16_to_cpu(resp->error_code);
7063 	ring_id = le16_to_cpu(resp->ring_id);
7064 	hwrm_req_drop(bp, req);
7065 
7066 exit:
7067 	if (rc || err) {
7068 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7069 			   ring_type, rc, err);
7070 		return -EIO;
7071 	}
7072 	ring->fw_ring_id = ring_id;
7073 	return rc;
7074 }
7075 
7076 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7077 {
7078 	int rc;
7079 
7080 	if (BNXT_PF(bp)) {
7081 		struct hwrm_func_cfg_input *req;
7082 
7083 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7084 		if (rc)
7085 			return rc;
7086 
7087 		req->fid = cpu_to_le16(0xffff);
7088 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7089 		req->async_event_cr = cpu_to_le16(idx);
7090 		return hwrm_req_send(bp, req);
7091 	} else {
7092 		struct hwrm_func_vf_cfg_input *req;
7093 
7094 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7095 		if (rc)
7096 			return rc;
7097 
7098 		req->enables =
7099 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7100 		req->async_event_cr = cpu_to_le16(idx);
7101 		return hwrm_req_send(bp, req);
7102 	}
7103 }
7104 
7105 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7106 			     u32 ring_type)
7107 {
7108 	switch (ring_type) {
7109 	case HWRM_RING_ALLOC_TX:
7110 		db->db_ring_mask = bp->tx_ring_mask;
7111 		break;
7112 	case HWRM_RING_ALLOC_RX:
7113 		db->db_ring_mask = bp->rx_ring_mask;
7114 		break;
7115 	case HWRM_RING_ALLOC_AGG:
7116 		db->db_ring_mask = bp->rx_agg_ring_mask;
7117 		break;
7118 	case HWRM_RING_ALLOC_CMPL:
7119 	case HWRM_RING_ALLOC_NQ:
7120 		db->db_ring_mask = bp->cp_ring_mask;
7121 		break;
7122 	}
7123 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7124 		db->db_epoch_mask = db->db_ring_mask + 1;
7125 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7126 	}
7127 }
7128 
7129 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7130 			u32 map_idx, u32 xid)
7131 {
7132 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7133 		switch (ring_type) {
7134 		case HWRM_RING_ALLOC_TX:
7135 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7136 			break;
7137 		case HWRM_RING_ALLOC_RX:
7138 		case HWRM_RING_ALLOC_AGG:
7139 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7140 			break;
7141 		case HWRM_RING_ALLOC_CMPL:
7142 			db->db_key64 = DBR_PATH_L2;
7143 			break;
7144 		case HWRM_RING_ALLOC_NQ:
7145 			db->db_key64 = DBR_PATH_L2;
7146 			break;
7147 		}
7148 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7149 
7150 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7151 			db->db_key64 |= DBR_VALID;
7152 
7153 		db->doorbell = bp->bar1 + bp->db_offset;
7154 	} else {
7155 		db->doorbell = bp->bar1 + map_idx * 0x80;
7156 		switch (ring_type) {
7157 		case HWRM_RING_ALLOC_TX:
7158 			db->db_key32 = DB_KEY_TX;
7159 			break;
7160 		case HWRM_RING_ALLOC_RX:
7161 		case HWRM_RING_ALLOC_AGG:
7162 			db->db_key32 = DB_KEY_RX;
7163 			break;
7164 		case HWRM_RING_ALLOC_CMPL:
7165 			db->db_key32 = DB_KEY_CP;
7166 			break;
7167 		}
7168 	}
7169 	bnxt_set_db_mask(bp, db, ring_type);
7170 }
7171 
7172 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7173 				   struct bnxt_rx_ring_info *rxr)
7174 {
7175 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7176 	struct bnxt_napi *bnapi = rxr->bnapi;
7177 	u32 type = HWRM_RING_ALLOC_RX;
7178 	u32 map_idx = bnapi->index;
7179 	int rc;
7180 
7181 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7182 	if (rc)
7183 		return rc;
7184 
7185 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7186 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7187 
7188 	return 0;
7189 }
7190 
7191 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7192 				       struct bnxt_rx_ring_info *rxr)
7193 {
7194 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7195 	u32 type = HWRM_RING_ALLOC_AGG;
7196 	u32 grp_idx = ring->grp_idx;
7197 	u32 map_idx;
7198 	int rc;
7199 
7200 	map_idx = grp_idx + bp->rx_nr_rings;
7201 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7202 	if (rc)
7203 		return rc;
7204 
7205 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7206 		    ring->fw_ring_id);
7207 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7208 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7209 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7210 
7211 	return 0;
7212 }
7213 
7214 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp,
7215 				      struct bnxt_cp_ring_info *cpr)
7216 {
7217 	const u32 type = HWRM_RING_ALLOC_CMPL;
7218 	struct bnxt_napi *bnapi = cpr->bnapi;
7219 	struct bnxt_ring_struct *ring;
7220 	u32 map_idx = bnapi->index;
7221 	int rc;
7222 
7223 	ring = &cpr->cp_ring_struct;
7224 	ring->handle = BNXT_SET_NQ_HDL(cpr);
7225 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7226 	if (rc)
7227 		return rc;
7228 	bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7229 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7230 	return 0;
7231 }
7232 
7233 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp,
7234 				   struct bnxt_tx_ring_info *txr, u32 tx_idx)
7235 {
7236 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7237 	const u32 type = HWRM_RING_ALLOC_TX;
7238 	int rc;
7239 
7240 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, tx_idx);
7241 	if (rc)
7242 		return rc;
7243 	bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id);
7244 	return 0;
7245 }
7246 
7247 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7248 {
7249 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7250 	int i, rc = 0;
7251 	u32 type;
7252 
7253 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7254 		type = HWRM_RING_ALLOC_NQ;
7255 	else
7256 		type = HWRM_RING_ALLOC_CMPL;
7257 	for (i = 0; i < bp->cp_nr_rings; i++) {
7258 		struct bnxt_napi *bnapi = bp->bnapi[i];
7259 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7260 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7261 		u32 map_idx = ring->map_idx;
7262 		unsigned int vector;
7263 
7264 		vector = bp->irq_tbl[map_idx].vector;
7265 		disable_irq_nosync(vector);
7266 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7267 		if (rc) {
7268 			enable_irq(vector);
7269 			goto err_out;
7270 		}
7271 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7272 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7273 		enable_irq(vector);
7274 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7275 
7276 		if (!i) {
7277 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7278 			if (rc)
7279 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7280 		}
7281 	}
7282 
7283 	for (i = 0; i < bp->tx_nr_rings; i++) {
7284 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7285 
7286 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7287 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
7288 			if (rc)
7289 				goto err_out;
7290 		}
7291 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i);
7292 		if (rc)
7293 			goto err_out;
7294 	}
7295 
7296 	for (i = 0; i < bp->rx_nr_rings; i++) {
7297 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7298 
7299 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7300 		if (rc)
7301 			goto err_out;
7302 		/* If we have agg rings, post agg buffers first. */
7303 		if (!agg_rings)
7304 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7305 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7306 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
7307 			if (rc)
7308 				goto err_out;
7309 		}
7310 	}
7311 
7312 	if (agg_rings) {
7313 		for (i = 0; i < bp->rx_nr_rings; i++) {
7314 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7315 			if (rc)
7316 				goto err_out;
7317 		}
7318 	}
7319 err_out:
7320 	return rc;
7321 }
7322 
7323 static void bnxt_cancel_dim(struct bnxt *bp)
7324 {
7325 	int i;
7326 
7327 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7328 	 * if NAPI is enabled.
7329 	 */
7330 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7331 		return;
7332 
7333 	/* Make sure NAPI sees that the VNIC is disabled */
7334 	synchronize_net();
7335 	for (i = 0; i < bp->rx_nr_rings; i++) {
7336 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7337 		struct bnxt_napi *bnapi = rxr->bnapi;
7338 
7339 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7340 	}
7341 }
7342 
7343 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7344 				   struct bnxt_ring_struct *ring,
7345 				   u32 ring_type, int cmpl_ring_id)
7346 {
7347 	struct hwrm_ring_free_output *resp;
7348 	struct hwrm_ring_free_input *req;
7349 	u16 error_code = 0;
7350 	int rc;
7351 
7352 	if (BNXT_NO_FW_ACCESS(bp))
7353 		return 0;
7354 
7355 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7356 	if (rc)
7357 		goto exit;
7358 
7359 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7360 	req->ring_type = ring_type;
7361 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7362 
7363 	resp = hwrm_req_hold(bp, req);
7364 	rc = hwrm_req_send(bp, req);
7365 	error_code = le16_to_cpu(resp->error_code);
7366 	hwrm_req_drop(bp, req);
7367 exit:
7368 	if (rc || error_code) {
7369 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7370 			   ring_type, rc, error_code);
7371 		return -EIO;
7372 	}
7373 	return 0;
7374 }
7375 
7376 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp,
7377 				   struct bnxt_tx_ring_info *txr,
7378 				   bool close_path)
7379 {
7380 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7381 	u32 cmpl_ring_id;
7382 
7383 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7384 		return;
7385 
7386 	cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) :
7387 		       INVALID_HW_RING_ID;
7388 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX,
7389 				cmpl_ring_id);
7390 	ring->fw_ring_id = INVALID_HW_RING_ID;
7391 }
7392 
7393 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7394 				   struct bnxt_rx_ring_info *rxr,
7395 				   bool close_path)
7396 {
7397 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7398 	u32 grp_idx = rxr->bnapi->index;
7399 	u32 cmpl_ring_id;
7400 
7401 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7402 		return;
7403 
7404 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7405 	hwrm_ring_free_send_msg(bp, ring,
7406 				RING_FREE_REQ_RING_TYPE_RX,
7407 				close_path ? cmpl_ring_id :
7408 				INVALID_HW_RING_ID);
7409 	ring->fw_ring_id = INVALID_HW_RING_ID;
7410 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7411 }
7412 
7413 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7414 				       struct bnxt_rx_ring_info *rxr,
7415 				       bool close_path)
7416 {
7417 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7418 	u32 grp_idx = rxr->bnapi->index;
7419 	u32 type, cmpl_ring_id;
7420 
7421 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7422 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7423 	else
7424 		type = RING_FREE_REQ_RING_TYPE_RX;
7425 
7426 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7427 		return;
7428 
7429 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7430 	hwrm_ring_free_send_msg(bp, ring, type,
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].agg_fw_ring_id = INVALID_HW_RING_ID;
7435 }
7436 
7437 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp,
7438 				   struct bnxt_cp_ring_info *cpr)
7439 {
7440 	struct bnxt_ring_struct *ring;
7441 
7442 	ring = &cpr->cp_ring_struct;
7443 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7444 		return;
7445 
7446 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL,
7447 				INVALID_HW_RING_ID);
7448 	ring->fw_ring_id = INVALID_HW_RING_ID;
7449 }
7450 
7451 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
7452 {
7453 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7454 	int i, size = ring->ring_mem.page_size;
7455 
7456 	cpr->cp_raw_cons = 0;
7457 	cpr->toggle = 0;
7458 
7459 	for (i = 0; i < bp->cp_nr_pages; i++)
7460 		if (cpr->cp_desc_ring[i])
7461 			memset(cpr->cp_desc_ring[i], 0, size);
7462 }
7463 
7464 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7465 {
7466 	u32 type;
7467 	int i;
7468 
7469 	if (!bp->bnapi)
7470 		return;
7471 
7472 	for (i = 0; i < bp->tx_nr_rings; i++)
7473 		bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path);
7474 
7475 	bnxt_cancel_dim(bp);
7476 	for (i = 0; i < bp->rx_nr_rings; i++) {
7477 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7478 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7479 	}
7480 
7481 	/* The completion rings are about to be freed.  After that the
7482 	 * IRQ doorbell will not work anymore.  So we need to disable
7483 	 * IRQ here.
7484 	 */
7485 	bnxt_disable_int_sync(bp);
7486 
7487 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7488 		type = RING_FREE_REQ_RING_TYPE_NQ;
7489 	else
7490 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7491 	for (i = 0; i < bp->cp_nr_rings; i++) {
7492 		struct bnxt_napi *bnapi = bp->bnapi[i];
7493 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7494 		struct bnxt_ring_struct *ring;
7495 		int j;
7496 
7497 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++)
7498 			bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]);
7499 
7500 		ring = &cpr->cp_ring_struct;
7501 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7502 			hwrm_ring_free_send_msg(bp, ring, type,
7503 						INVALID_HW_RING_ID);
7504 			ring->fw_ring_id = INVALID_HW_RING_ID;
7505 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7506 		}
7507 	}
7508 }
7509 
7510 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7511 			     bool shared);
7512 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7513 			   bool shared);
7514 
7515 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7516 {
7517 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7518 	struct hwrm_func_qcfg_output *resp;
7519 	struct hwrm_func_qcfg_input *req;
7520 	int rc;
7521 
7522 	if (bp->hwrm_spec_code < 0x10601)
7523 		return 0;
7524 
7525 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7526 	if (rc)
7527 		return rc;
7528 
7529 	req->fid = cpu_to_le16(0xffff);
7530 	resp = hwrm_req_hold(bp, req);
7531 	rc = hwrm_req_send(bp, req);
7532 	if (rc) {
7533 		hwrm_req_drop(bp, req);
7534 		return rc;
7535 	}
7536 
7537 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7538 	if (BNXT_NEW_RM(bp)) {
7539 		u16 cp, stats;
7540 
7541 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7542 		hw_resc->resv_hw_ring_grps =
7543 			le32_to_cpu(resp->alloc_hw_ring_grps);
7544 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7545 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7546 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7547 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7548 		hw_resc->resv_irqs = cp;
7549 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7550 			int rx = hw_resc->resv_rx_rings;
7551 			int tx = hw_resc->resv_tx_rings;
7552 
7553 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7554 				rx >>= 1;
7555 			if (cp < (rx + tx)) {
7556 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7557 				if (rc)
7558 					goto get_rings_exit;
7559 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7560 					rx <<= 1;
7561 				hw_resc->resv_rx_rings = rx;
7562 				hw_resc->resv_tx_rings = tx;
7563 			}
7564 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7565 			hw_resc->resv_hw_ring_grps = rx;
7566 		}
7567 		hw_resc->resv_cp_rings = cp;
7568 		hw_resc->resv_stat_ctxs = stats;
7569 	}
7570 get_rings_exit:
7571 	hwrm_req_drop(bp, req);
7572 	return rc;
7573 }
7574 
7575 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7576 {
7577 	struct hwrm_func_qcfg_output *resp;
7578 	struct hwrm_func_qcfg_input *req;
7579 	int rc;
7580 
7581 	if (bp->hwrm_spec_code < 0x10601)
7582 		return 0;
7583 
7584 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7585 	if (rc)
7586 		return rc;
7587 
7588 	req->fid = cpu_to_le16(fid);
7589 	resp = hwrm_req_hold(bp, req);
7590 	rc = hwrm_req_send(bp, req);
7591 	if (!rc)
7592 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7593 
7594 	hwrm_req_drop(bp, req);
7595 	return rc;
7596 }
7597 
7598 static bool bnxt_rfs_supported(struct bnxt *bp);
7599 
7600 static struct hwrm_func_cfg_input *
7601 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7602 {
7603 	struct hwrm_func_cfg_input *req;
7604 	u32 enables = 0;
7605 
7606 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7607 		return NULL;
7608 
7609 	req->fid = cpu_to_le16(0xffff);
7610 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7611 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7612 	if (BNXT_NEW_RM(bp)) {
7613 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7614 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7615 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7616 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7617 			enables |= hwr->cp_p5 ?
7618 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7619 		} else {
7620 			enables |= hwr->cp ?
7621 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7622 			enables |= hwr->grp ?
7623 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7624 		}
7625 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7626 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7627 					  0;
7628 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7629 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7630 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7631 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7632 			req->num_msix = cpu_to_le16(hwr->cp);
7633 		} else {
7634 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7635 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7636 		}
7637 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7638 		req->num_vnics = cpu_to_le16(hwr->vnic);
7639 	}
7640 	req->enables = cpu_to_le32(enables);
7641 	return req;
7642 }
7643 
7644 static struct hwrm_func_vf_cfg_input *
7645 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7646 {
7647 	struct hwrm_func_vf_cfg_input *req;
7648 	u32 enables = 0;
7649 
7650 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7651 		return NULL;
7652 
7653 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7654 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7655 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7656 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7657 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7658 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7659 		enables |= hwr->cp_p5 ?
7660 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7661 	} else {
7662 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7663 		enables |= hwr->grp ?
7664 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7665 	}
7666 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7667 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7668 
7669 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7670 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7671 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7672 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7673 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7674 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7675 	} else {
7676 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7677 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7678 	}
7679 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7680 	req->num_vnics = cpu_to_le16(hwr->vnic);
7681 
7682 	req->enables = cpu_to_le32(enables);
7683 	return req;
7684 }
7685 
7686 static int
7687 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7688 {
7689 	struct hwrm_func_cfg_input *req;
7690 	int rc;
7691 
7692 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7693 	if (!req)
7694 		return -ENOMEM;
7695 
7696 	if (!req->enables) {
7697 		hwrm_req_drop(bp, req);
7698 		return 0;
7699 	}
7700 
7701 	rc = hwrm_req_send(bp, req);
7702 	if (rc)
7703 		return rc;
7704 
7705 	if (bp->hwrm_spec_code < 0x10601)
7706 		bp->hw_resc.resv_tx_rings = hwr->tx;
7707 
7708 	return bnxt_hwrm_get_rings(bp);
7709 }
7710 
7711 static int
7712 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7713 {
7714 	struct hwrm_func_vf_cfg_input *req;
7715 	int rc;
7716 
7717 	if (!BNXT_NEW_RM(bp)) {
7718 		bp->hw_resc.resv_tx_rings = hwr->tx;
7719 		return 0;
7720 	}
7721 
7722 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7723 	if (!req)
7724 		return -ENOMEM;
7725 
7726 	rc = hwrm_req_send(bp, req);
7727 	if (rc)
7728 		return rc;
7729 
7730 	return bnxt_hwrm_get_rings(bp);
7731 }
7732 
7733 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7734 {
7735 	if (BNXT_PF(bp))
7736 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7737 	else
7738 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7739 }
7740 
7741 int bnxt_nq_rings_in_use(struct bnxt *bp)
7742 {
7743 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7744 }
7745 
7746 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7747 {
7748 	int cp;
7749 
7750 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7751 		return bnxt_nq_rings_in_use(bp);
7752 
7753 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7754 	return cp;
7755 }
7756 
7757 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7758 {
7759 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7760 }
7761 
7762 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7763 {
7764 	if (!hwr->grp)
7765 		return 0;
7766 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7767 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7768 
7769 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7770 			rss_ctx *= hwr->vnic;
7771 		return rss_ctx;
7772 	}
7773 	if (BNXT_VF(bp))
7774 		return BNXT_VF_MAX_RSS_CTX;
7775 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7776 		return hwr->grp + 1;
7777 	return 1;
7778 }
7779 
7780 /* Check if a default RSS map needs to be setup.  This function is only
7781  * used on older firmware that does not require reserving RX rings.
7782  */
7783 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7784 {
7785 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7786 
7787 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7788 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7789 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7790 		if (!netif_is_rxfh_configured(bp->dev))
7791 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7792 	}
7793 }
7794 
7795 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7796 {
7797 	if (bp->flags & BNXT_FLAG_RFS) {
7798 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7799 			return 2 + bp->num_rss_ctx;
7800 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7801 			return rx_rings + 1;
7802 	}
7803 	return 1;
7804 }
7805 
7806 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7807 {
7808 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7809 	int cp = bnxt_cp_rings_in_use(bp);
7810 	int nq = bnxt_nq_rings_in_use(bp);
7811 	int rx = bp->rx_nr_rings, stat;
7812 	int vnic, grp = rx;
7813 
7814 	/* Old firmware does not need RX ring reservations but we still
7815 	 * need to setup a default RSS map when needed.  With new firmware
7816 	 * we go through RX ring reservations first and then set up the
7817 	 * RSS map for the successfully reserved RX rings when needed.
7818 	 */
7819 	if (!BNXT_NEW_RM(bp))
7820 		bnxt_check_rss_tbl_no_rmgr(bp);
7821 
7822 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7823 	    bp->hwrm_spec_code >= 0x10601)
7824 		return true;
7825 
7826 	if (!BNXT_NEW_RM(bp))
7827 		return false;
7828 
7829 	vnic = bnxt_get_total_vnics(bp, rx);
7830 
7831 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7832 		rx <<= 1;
7833 	stat = bnxt_get_func_stat_ctxs(bp);
7834 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7835 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7836 	    (hw_resc->resv_hw_ring_grps != grp &&
7837 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7838 		return true;
7839 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7840 	    hw_resc->resv_irqs != nq)
7841 		return true;
7842 	return false;
7843 }
7844 
7845 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7846 {
7847 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7848 
7849 	hwr->tx = hw_resc->resv_tx_rings;
7850 	if (BNXT_NEW_RM(bp)) {
7851 		hwr->rx = hw_resc->resv_rx_rings;
7852 		hwr->cp = hw_resc->resv_irqs;
7853 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7854 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7855 		hwr->grp = hw_resc->resv_hw_ring_grps;
7856 		hwr->vnic = hw_resc->resv_vnics;
7857 		hwr->stat = hw_resc->resv_stat_ctxs;
7858 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7859 	}
7860 }
7861 
7862 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7863 {
7864 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7865 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7866 }
7867 
7868 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
7869 
7870 static int __bnxt_reserve_rings(struct bnxt *bp)
7871 {
7872 	struct bnxt_hw_rings hwr = {0};
7873 	int rx_rings, old_rx_rings, rc;
7874 	int cp = bp->cp_nr_rings;
7875 	int ulp_msix = 0;
7876 	bool sh = false;
7877 	int tx_cp;
7878 
7879 	if (!bnxt_need_reserve_rings(bp))
7880 		return 0;
7881 
7882 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
7883 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
7884 		if (!ulp_msix)
7885 			bnxt_set_ulp_stat_ctxs(bp, 0);
7886 
7887 		if (ulp_msix > bp->ulp_num_msix_want)
7888 			ulp_msix = bp->ulp_num_msix_want;
7889 		hwr.cp = cp + ulp_msix;
7890 	} else {
7891 		hwr.cp = bnxt_nq_rings_in_use(bp);
7892 	}
7893 
7894 	hwr.tx = bp->tx_nr_rings;
7895 	hwr.rx = bp->rx_nr_rings;
7896 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7897 		sh = true;
7898 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7899 		hwr.cp_p5 = hwr.rx + hwr.tx;
7900 
7901 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7902 
7903 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7904 		hwr.rx <<= 1;
7905 	hwr.grp = bp->rx_nr_rings;
7906 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7907 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
7908 	old_rx_rings = bp->hw_resc.resv_rx_rings;
7909 
7910 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
7911 	if (rc)
7912 		return rc;
7913 
7914 	bnxt_copy_reserved_rings(bp, &hwr);
7915 
7916 	rx_rings = hwr.rx;
7917 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7918 		if (hwr.rx >= 2) {
7919 			rx_rings = hwr.rx >> 1;
7920 		} else {
7921 			if (netif_running(bp->dev))
7922 				return -ENOMEM;
7923 
7924 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7925 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7926 			bp->dev->hw_features &= ~NETIF_F_LRO;
7927 			bp->dev->features &= ~NETIF_F_LRO;
7928 			bnxt_set_ring_params(bp);
7929 		}
7930 	}
7931 	rx_rings = min_t(int, rx_rings, hwr.grp);
7932 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
7933 	if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
7934 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
7935 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
7936 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
7937 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7938 		hwr.rx = rx_rings << 1;
7939 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
7940 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
7941 	bp->tx_nr_rings = hwr.tx;
7942 
7943 	/* If we cannot reserve all the RX rings, reset the RSS map only
7944 	 * if absolutely necessary
7945 	 */
7946 	if (rx_rings != bp->rx_nr_rings) {
7947 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
7948 			    rx_rings, bp->rx_nr_rings);
7949 		if (netif_is_rxfh_configured(bp->dev) &&
7950 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
7951 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
7952 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
7953 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
7954 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
7955 		}
7956 	}
7957 	bp->rx_nr_rings = rx_rings;
7958 	bp->cp_nr_rings = hwr.cp;
7959 
7960 	if (!bnxt_rings_ok(bp, &hwr))
7961 		return -ENOMEM;
7962 
7963 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
7964 	    !netif_is_rxfh_configured(bp->dev))
7965 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7966 
7967 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
7968 		int resv_msix, resv_ctx, ulp_ctxs;
7969 		struct bnxt_hw_resc *hw_resc;
7970 
7971 		hw_resc = &bp->hw_resc;
7972 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
7973 		ulp_msix = min_t(int, resv_msix, ulp_msix);
7974 		bnxt_set_ulp_msix_num(bp, ulp_msix);
7975 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
7976 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
7977 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
7978 	}
7979 
7980 	return rc;
7981 }
7982 
7983 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7984 {
7985 	struct hwrm_func_vf_cfg_input *req;
7986 	u32 flags;
7987 
7988 	if (!BNXT_NEW_RM(bp))
7989 		return 0;
7990 
7991 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7992 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
7993 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7994 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7995 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7996 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
7997 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
7998 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7999 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8000 
8001 	req->flags = cpu_to_le32(flags);
8002 	return hwrm_req_send_silent(bp, req);
8003 }
8004 
8005 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8006 {
8007 	struct hwrm_func_cfg_input *req;
8008 	u32 flags;
8009 
8010 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
8011 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
8012 	if (BNXT_NEW_RM(bp)) {
8013 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8014 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8015 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8016 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
8017 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8018 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
8019 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
8020 		else
8021 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8022 	}
8023 
8024 	req->flags = cpu_to_le32(flags);
8025 	return hwrm_req_send_silent(bp, req);
8026 }
8027 
8028 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8029 {
8030 	if (bp->hwrm_spec_code < 0x10801)
8031 		return 0;
8032 
8033 	if (BNXT_PF(bp))
8034 		return bnxt_hwrm_check_pf_rings(bp, hwr);
8035 
8036 	return bnxt_hwrm_check_vf_rings(bp, hwr);
8037 }
8038 
8039 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
8040 {
8041 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8042 	struct hwrm_ring_aggint_qcaps_output *resp;
8043 	struct hwrm_ring_aggint_qcaps_input *req;
8044 	int rc;
8045 
8046 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
8047 	coal_cap->num_cmpl_dma_aggr_max = 63;
8048 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
8049 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
8050 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
8051 	coal_cap->int_lat_tmr_min_max = 65535;
8052 	coal_cap->int_lat_tmr_max_max = 65535;
8053 	coal_cap->num_cmpl_aggr_int_max = 65535;
8054 	coal_cap->timer_units = 80;
8055 
8056 	if (bp->hwrm_spec_code < 0x10902)
8057 		return;
8058 
8059 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
8060 		return;
8061 
8062 	resp = hwrm_req_hold(bp, req);
8063 	rc = hwrm_req_send_silent(bp, req);
8064 	if (!rc) {
8065 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8066 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8067 		coal_cap->num_cmpl_dma_aggr_max =
8068 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8069 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8070 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8071 		coal_cap->cmpl_aggr_dma_tmr_max =
8072 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8073 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8074 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8075 		coal_cap->int_lat_tmr_min_max =
8076 			le16_to_cpu(resp->int_lat_tmr_min_max);
8077 		coal_cap->int_lat_tmr_max_max =
8078 			le16_to_cpu(resp->int_lat_tmr_max_max);
8079 		coal_cap->num_cmpl_aggr_int_max =
8080 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8081 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8082 	}
8083 	hwrm_req_drop(bp, req);
8084 }
8085 
8086 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8087 {
8088 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8089 
8090 	return usec * 1000 / coal_cap->timer_units;
8091 }
8092 
8093 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8094 	struct bnxt_coal *hw_coal,
8095 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8096 {
8097 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8098 	u16 val, tmr, max, flags = hw_coal->flags;
8099 	u32 cmpl_params = coal_cap->cmpl_params;
8100 
8101 	max = hw_coal->bufs_per_record * 128;
8102 	if (hw_coal->budget)
8103 		max = hw_coal->bufs_per_record * hw_coal->budget;
8104 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8105 
8106 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8107 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8108 
8109 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8110 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8111 
8112 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8113 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8114 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8115 
8116 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8117 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8118 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8119 
8120 	/* min timer set to 1/2 of interrupt timer */
8121 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8122 		val = tmr / 2;
8123 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8124 		req->int_lat_tmr_min = cpu_to_le16(val);
8125 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8126 	}
8127 
8128 	/* buf timer set to 1/4 of interrupt timer */
8129 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8130 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8131 
8132 	if (cmpl_params &
8133 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8134 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8135 		val = clamp_t(u16, tmr, 1,
8136 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8137 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8138 		req->enables |=
8139 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8140 	}
8141 
8142 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8143 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8144 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8145 	req->flags = cpu_to_le16(flags);
8146 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8147 }
8148 
8149 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8150 				   struct bnxt_coal *hw_coal)
8151 {
8152 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8153 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8154 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8155 	u32 nq_params = coal_cap->nq_params;
8156 	u16 tmr;
8157 	int rc;
8158 
8159 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8160 		return 0;
8161 
8162 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8163 	if (rc)
8164 		return rc;
8165 
8166 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8167 	req->flags =
8168 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8169 
8170 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8171 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8172 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8173 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8174 	return hwrm_req_send(bp, req);
8175 }
8176 
8177 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8178 {
8179 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8180 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8181 	struct bnxt_coal coal;
8182 	int rc;
8183 
8184 	/* Tick values in micro seconds.
8185 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8186 	 */
8187 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8188 
8189 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8190 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8191 
8192 	if (!bnapi->rx_ring)
8193 		return -ENODEV;
8194 
8195 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8196 	if (rc)
8197 		return rc;
8198 
8199 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8200 
8201 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8202 
8203 	return hwrm_req_send(bp, req_rx);
8204 }
8205 
8206 static int
8207 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8208 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8209 {
8210 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8211 
8212 	req->ring_id = cpu_to_le16(ring_id);
8213 	return hwrm_req_send(bp, req);
8214 }
8215 
8216 static int
8217 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8218 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8219 {
8220 	struct bnxt_tx_ring_info *txr;
8221 	int i, rc;
8222 
8223 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8224 		u16 ring_id;
8225 
8226 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8227 		req->ring_id = cpu_to_le16(ring_id);
8228 		rc = hwrm_req_send(bp, req);
8229 		if (rc)
8230 			return rc;
8231 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8232 			return 0;
8233 	}
8234 	return 0;
8235 }
8236 
8237 int bnxt_hwrm_set_coal(struct bnxt *bp)
8238 {
8239 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8240 	int i, rc;
8241 
8242 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8243 	if (rc)
8244 		return rc;
8245 
8246 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8247 	if (rc) {
8248 		hwrm_req_drop(bp, req_rx);
8249 		return rc;
8250 	}
8251 
8252 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8253 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8254 
8255 	hwrm_req_hold(bp, req_rx);
8256 	hwrm_req_hold(bp, req_tx);
8257 	for (i = 0; i < bp->cp_nr_rings; i++) {
8258 		struct bnxt_napi *bnapi = bp->bnapi[i];
8259 		struct bnxt_coal *hw_coal;
8260 
8261 		if (!bnapi->rx_ring)
8262 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8263 		else
8264 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8265 		if (rc)
8266 			break;
8267 
8268 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8269 			continue;
8270 
8271 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8272 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8273 			if (rc)
8274 				break;
8275 		}
8276 		if (bnapi->rx_ring)
8277 			hw_coal = &bp->rx_coal;
8278 		else
8279 			hw_coal = &bp->tx_coal;
8280 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8281 	}
8282 	hwrm_req_drop(bp, req_rx);
8283 	hwrm_req_drop(bp, req_tx);
8284 	return rc;
8285 }
8286 
8287 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8288 {
8289 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8290 	struct hwrm_stat_ctx_free_input *req;
8291 	int i;
8292 
8293 	if (!bp->bnapi)
8294 		return;
8295 
8296 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8297 		return;
8298 
8299 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8300 		return;
8301 	if (BNXT_FW_MAJ(bp) <= 20) {
8302 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8303 			hwrm_req_drop(bp, req);
8304 			return;
8305 		}
8306 		hwrm_req_hold(bp, req0);
8307 	}
8308 	hwrm_req_hold(bp, req);
8309 	for (i = 0; i < bp->cp_nr_rings; i++) {
8310 		struct bnxt_napi *bnapi = bp->bnapi[i];
8311 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8312 
8313 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8314 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8315 			if (req0) {
8316 				req0->stat_ctx_id = req->stat_ctx_id;
8317 				hwrm_req_send(bp, req0);
8318 			}
8319 			hwrm_req_send(bp, req);
8320 
8321 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8322 		}
8323 	}
8324 	hwrm_req_drop(bp, req);
8325 	if (req0)
8326 		hwrm_req_drop(bp, req0);
8327 }
8328 
8329 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8330 {
8331 	struct hwrm_stat_ctx_alloc_output *resp;
8332 	struct hwrm_stat_ctx_alloc_input *req;
8333 	int rc, i;
8334 
8335 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8336 		return 0;
8337 
8338 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8339 	if (rc)
8340 		return rc;
8341 
8342 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8343 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8344 
8345 	resp = hwrm_req_hold(bp, req);
8346 	for (i = 0; i < bp->cp_nr_rings; i++) {
8347 		struct bnxt_napi *bnapi = bp->bnapi[i];
8348 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8349 
8350 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8351 
8352 		rc = hwrm_req_send(bp, req);
8353 		if (rc)
8354 			break;
8355 
8356 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8357 
8358 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8359 	}
8360 	hwrm_req_drop(bp, req);
8361 	return rc;
8362 }
8363 
8364 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8365 {
8366 	struct hwrm_func_qcfg_output *resp;
8367 	struct hwrm_func_qcfg_input *req;
8368 	u16 flags;
8369 	int rc;
8370 
8371 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8372 	if (rc)
8373 		return rc;
8374 
8375 	req->fid = cpu_to_le16(0xffff);
8376 	resp = hwrm_req_hold(bp, req);
8377 	rc = hwrm_req_send(bp, req);
8378 	if (rc)
8379 		goto func_qcfg_exit;
8380 
8381 	flags = le16_to_cpu(resp->flags);
8382 #ifdef CONFIG_BNXT_SRIOV
8383 	if (BNXT_VF(bp)) {
8384 		struct bnxt_vf_info *vf = &bp->vf;
8385 
8386 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8387 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8388 			vf->flags |= BNXT_VF_TRUST;
8389 		else
8390 			vf->flags &= ~BNXT_VF_TRUST;
8391 	} else {
8392 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8393 	}
8394 #endif
8395 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8396 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8397 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8398 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8399 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8400 	}
8401 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8402 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8403 
8404 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8405 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8406 
8407 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8408 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8409 
8410 	switch (resp->port_partition_type) {
8411 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8412 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2:
8413 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8414 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8415 		bp->port_partition_type = resp->port_partition_type;
8416 		break;
8417 	}
8418 	if (bp->hwrm_spec_code < 0x10707 ||
8419 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8420 		bp->br_mode = BRIDGE_MODE_VEB;
8421 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8422 		bp->br_mode = BRIDGE_MODE_VEPA;
8423 	else
8424 		bp->br_mode = BRIDGE_MODE_UNDEF;
8425 
8426 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8427 	if (!bp->max_mtu)
8428 		bp->max_mtu = BNXT_MAX_MTU;
8429 
8430 	if (bp->db_size)
8431 		goto func_qcfg_exit;
8432 
8433 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8434 	if (BNXT_CHIP_P5(bp)) {
8435 		if (BNXT_PF(bp))
8436 			bp->db_offset = DB_PF_OFFSET_P5;
8437 		else
8438 			bp->db_offset = DB_VF_OFFSET_P5;
8439 	}
8440 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8441 				 1024);
8442 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8443 	    bp->db_size <= bp->db_offset)
8444 		bp->db_size = pci_resource_len(bp->pdev, 2);
8445 
8446 func_qcfg_exit:
8447 	hwrm_req_drop(bp, req);
8448 	return rc;
8449 }
8450 
8451 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8452 				      u8 init_val, u8 init_offset,
8453 				      bool init_mask_set)
8454 {
8455 	ctxm->init_value = init_val;
8456 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8457 	if (init_mask_set)
8458 		ctxm->init_offset = init_offset * 4;
8459 	else
8460 		ctxm->init_value = 0;
8461 }
8462 
8463 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8464 {
8465 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8466 	u16 type;
8467 
8468 	for (type = 0; type < ctx_max; type++) {
8469 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8470 		int n = 1;
8471 
8472 		if (!ctxm->max_entries || ctxm->pg_info)
8473 			continue;
8474 
8475 		if (ctxm->instance_bmap)
8476 			n = hweight32(ctxm->instance_bmap);
8477 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8478 		if (!ctxm->pg_info)
8479 			return -ENOMEM;
8480 	}
8481 	return 0;
8482 }
8483 
8484 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8485 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8486 
8487 #define BNXT_CTX_INIT_VALID(flags)	\
8488 	(!!((flags) &			\
8489 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8490 
8491 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8492 {
8493 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8494 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8495 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8496 	u16 type;
8497 	int rc;
8498 
8499 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8500 	if (rc)
8501 		return rc;
8502 
8503 	if (!ctx) {
8504 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8505 		if (!ctx)
8506 			return -ENOMEM;
8507 		bp->ctx = ctx;
8508 	}
8509 
8510 	resp = hwrm_req_hold(bp, req);
8511 
8512 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8513 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8514 		u8 init_val, init_off, i;
8515 		u32 max_entries;
8516 		u16 entry_size;
8517 		__le32 *p;
8518 		u32 flags;
8519 
8520 		req->type = cpu_to_le16(type);
8521 		rc = hwrm_req_send(bp, req);
8522 		if (rc)
8523 			goto ctx_done;
8524 		flags = le32_to_cpu(resp->flags);
8525 		type = le16_to_cpu(resp->next_valid_type);
8526 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8527 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8528 			continue;
8529 		}
8530 		entry_size = le16_to_cpu(resp->entry_size);
8531 		max_entries = le32_to_cpu(resp->max_num_entries);
8532 		if (ctxm->mem_valid) {
8533 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8534 			    ctxm->entry_size != entry_size ||
8535 			    ctxm->max_entries != max_entries)
8536 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8537 			else
8538 				continue;
8539 		}
8540 		ctxm->type = le16_to_cpu(resp->type);
8541 		ctxm->entry_size = entry_size;
8542 		ctxm->flags = flags;
8543 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8544 		ctxm->entry_multiple = resp->entry_multiple;
8545 		ctxm->max_entries = max_entries;
8546 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8547 		init_val = resp->ctx_init_value;
8548 		init_off = resp->ctx_init_offset;
8549 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8550 					  BNXT_CTX_INIT_VALID(flags));
8551 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8552 					      BNXT_MAX_SPLIT_ENTRY);
8553 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8554 		     i++, p++)
8555 			ctxm->split[i] = le32_to_cpu(*p);
8556 	}
8557 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8558 
8559 ctx_done:
8560 	hwrm_req_drop(bp, req);
8561 	return rc;
8562 }
8563 
8564 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8565 {
8566 	struct hwrm_func_backing_store_qcaps_output *resp;
8567 	struct hwrm_func_backing_store_qcaps_input *req;
8568 	int rc;
8569 
8570 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8571 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8572 		return 0;
8573 
8574 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8575 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8576 
8577 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8578 	if (rc)
8579 		return rc;
8580 
8581 	resp = hwrm_req_hold(bp, req);
8582 	rc = hwrm_req_send_silent(bp, req);
8583 	if (!rc) {
8584 		struct bnxt_ctx_mem_type *ctxm;
8585 		struct bnxt_ctx_mem_info *ctx;
8586 		u8 init_val, init_idx = 0;
8587 		u16 init_mask;
8588 
8589 		ctx = bp->ctx;
8590 		if (!ctx) {
8591 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8592 			if (!ctx) {
8593 				rc = -ENOMEM;
8594 				goto ctx_err;
8595 			}
8596 			bp->ctx = ctx;
8597 		}
8598 		init_val = resp->ctx_kind_initializer;
8599 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8600 
8601 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8602 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8603 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8604 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8605 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8606 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8607 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8608 					  (init_mask & (1 << init_idx++)) != 0);
8609 
8610 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8611 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8612 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8613 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8614 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8615 					  (init_mask & (1 << init_idx++)) != 0);
8616 
8617 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8618 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8619 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8620 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8621 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8622 					  (init_mask & (1 << init_idx++)) != 0);
8623 
8624 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8625 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8626 		ctxm->max_entries = ctxm->vnic_entries +
8627 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8628 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8629 		bnxt_init_ctx_initializer(ctxm, init_val,
8630 					  resp->vnic_init_offset,
8631 					  (init_mask & (1 << init_idx++)) != 0);
8632 
8633 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8634 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8635 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8636 		bnxt_init_ctx_initializer(ctxm, init_val,
8637 					  resp->stat_init_offset,
8638 					  (init_mask & (1 << init_idx++)) != 0);
8639 
8640 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8641 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8642 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8643 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8644 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8645 		if (!ctxm->entry_multiple)
8646 			ctxm->entry_multiple = 1;
8647 
8648 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8649 
8650 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8651 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8652 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8653 		ctxm->mrav_num_entries_units =
8654 			le16_to_cpu(resp->mrav_num_entries_units);
8655 		bnxt_init_ctx_initializer(ctxm, init_val,
8656 					  resp->mrav_init_offset,
8657 					  (init_mask & (1 << init_idx++)) != 0);
8658 
8659 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8660 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8661 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8662 
8663 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8664 		if (!ctx->tqm_fp_rings_count)
8665 			ctx->tqm_fp_rings_count = bp->max_q;
8666 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8667 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8668 
8669 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8670 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8671 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8672 
8673 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8674 	} else {
8675 		rc = 0;
8676 	}
8677 ctx_err:
8678 	hwrm_req_drop(bp, req);
8679 	return rc;
8680 }
8681 
8682 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8683 				  __le64 *pg_dir)
8684 {
8685 	if (!rmem->nr_pages)
8686 		return;
8687 
8688 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8689 	if (rmem->depth >= 1) {
8690 		if (rmem->depth == 2)
8691 			*pg_attr |= 2;
8692 		else
8693 			*pg_attr |= 1;
8694 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8695 	} else {
8696 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8697 	}
8698 }
8699 
8700 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8701 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8702 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8703 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8704 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8705 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8706 
8707 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8708 {
8709 	struct hwrm_func_backing_store_cfg_input *req;
8710 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8711 	struct bnxt_ctx_pg_info *ctx_pg;
8712 	struct bnxt_ctx_mem_type *ctxm;
8713 	void **__req = (void **)&req;
8714 	u32 req_len = sizeof(*req);
8715 	__le32 *num_entries;
8716 	__le64 *pg_dir;
8717 	u32 flags = 0;
8718 	u8 *pg_attr;
8719 	u32 ena;
8720 	int rc;
8721 	int i;
8722 
8723 	if (!ctx)
8724 		return 0;
8725 
8726 	if (req_len > bp->hwrm_max_ext_req_len)
8727 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8728 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8729 	if (rc)
8730 		return rc;
8731 
8732 	req->enables = cpu_to_le32(enables);
8733 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8734 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8735 		ctx_pg = ctxm->pg_info;
8736 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8737 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8738 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8739 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8740 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8741 				      &req->qpc_pg_size_qpc_lvl,
8742 				      &req->qpc_page_dir);
8743 
8744 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8745 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8746 	}
8747 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8748 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8749 		ctx_pg = ctxm->pg_info;
8750 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8751 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8752 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8753 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8754 				      &req->srq_pg_size_srq_lvl,
8755 				      &req->srq_page_dir);
8756 	}
8757 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8758 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8759 		ctx_pg = ctxm->pg_info;
8760 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8761 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8762 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8763 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8764 				      &req->cq_pg_size_cq_lvl,
8765 				      &req->cq_page_dir);
8766 	}
8767 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8768 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8769 		ctx_pg = ctxm->pg_info;
8770 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8771 		req->vnic_num_ring_table_entries =
8772 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8773 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8774 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8775 				      &req->vnic_pg_size_vnic_lvl,
8776 				      &req->vnic_page_dir);
8777 	}
8778 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8779 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8780 		ctx_pg = ctxm->pg_info;
8781 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8782 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8783 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8784 				      &req->stat_pg_size_stat_lvl,
8785 				      &req->stat_page_dir);
8786 	}
8787 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8788 		u32 units;
8789 
8790 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8791 		ctx_pg = ctxm->pg_info;
8792 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8793 		units = ctxm->mrav_num_entries_units;
8794 		if (units) {
8795 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8796 			u32 entries;
8797 
8798 			num_mr = ctx_pg->entries - num_ah;
8799 			entries = ((num_mr / units) << 16) | (num_ah / units);
8800 			req->mrav_num_entries = cpu_to_le32(entries);
8801 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8802 		}
8803 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8804 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8805 				      &req->mrav_pg_size_mrav_lvl,
8806 				      &req->mrav_page_dir);
8807 	}
8808 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8809 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8810 		ctx_pg = ctxm->pg_info;
8811 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8812 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8813 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8814 				      &req->tim_pg_size_tim_lvl,
8815 				      &req->tim_page_dir);
8816 	}
8817 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8818 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8819 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8820 	     pg_dir = &req->tqm_sp_page_dir,
8821 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8822 	     ctx_pg = ctxm->pg_info;
8823 	     i < BNXT_MAX_TQM_RINGS;
8824 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8825 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8826 		if (!(enables & ena))
8827 			continue;
8828 
8829 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8830 		*num_entries = cpu_to_le32(ctx_pg->entries);
8831 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8832 	}
8833 	req->flags = cpu_to_le32(flags);
8834 	return hwrm_req_send(bp, req);
8835 }
8836 
8837 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8838 				  struct bnxt_ctx_pg_info *ctx_pg)
8839 {
8840 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8841 
8842 	rmem->page_size = BNXT_PAGE_SIZE;
8843 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8844 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8845 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8846 	if (rmem->depth >= 1)
8847 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8848 	return bnxt_alloc_ring(bp, rmem);
8849 }
8850 
8851 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8852 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8853 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8854 {
8855 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8856 	int rc;
8857 
8858 	if (!mem_size)
8859 		return -EINVAL;
8860 
8861 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8862 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8863 		ctx_pg->nr_pages = 0;
8864 		return -EINVAL;
8865 	}
8866 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8867 		int nr_tbls, i;
8868 
8869 		rmem->depth = 2;
8870 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8871 					     GFP_KERNEL);
8872 		if (!ctx_pg->ctx_pg_tbl)
8873 			return -ENOMEM;
8874 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8875 		rmem->nr_pages = nr_tbls;
8876 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8877 		if (rc)
8878 			return rc;
8879 		for (i = 0; i < nr_tbls; i++) {
8880 			struct bnxt_ctx_pg_info *pg_tbl;
8881 
8882 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8883 			if (!pg_tbl)
8884 				return -ENOMEM;
8885 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8886 			rmem = &pg_tbl->ring_mem;
8887 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8888 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8889 			rmem->depth = 1;
8890 			rmem->nr_pages = MAX_CTX_PAGES;
8891 			rmem->ctx_mem = ctxm;
8892 			if (i == (nr_tbls - 1)) {
8893 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8894 
8895 				if (rem)
8896 					rmem->nr_pages = rem;
8897 			}
8898 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8899 			if (rc)
8900 				break;
8901 		}
8902 	} else {
8903 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8904 		if (rmem->nr_pages > 1 || depth)
8905 			rmem->depth = 1;
8906 		rmem->ctx_mem = ctxm;
8907 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8908 	}
8909 	return rc;
8910 }
8911 
8912 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
8913 				    struct bnxt_ctx_pg_info *ctx_pg,
8914 				    void *buf, size_t offset, size_t head,
8915 				    size_t tail)
8916 {
8917 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8918 	size_t nr_pages = ctx_pg->nr_pages;
8919 	int page_size = rmem->page_size;
8920 	size_t len = 0, total_len = 0;
8921 	u16 depth = rmem->depth;
8922 
8923 	tail %= nr_pages * page_size;
8924 	do {
8925 		if (depth > 1) {
8926 			int i = head / (page_size * MAX_CTX_PAGES);
8927 			struct bnxt_ctx_pg_info *pg_tbl;
8928 
8929 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8930 			rmem = &pg_tbl->ring_mem;
8931 		}
8932 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
8933 		head += len;
8934 		offset += len;
8935 		total_len += len;
8936 		if (head >= nr_pages * page_size)
8937 			head = 0;
8938 	} while (head != tail);
8939 	return total_len;
8940 }
8941 
8942 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
8943 				  struct bnxt_ctx_pg_info *ctx_pg)
8944 {
8945 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8946 
8947 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
8948 	    ctx_pg->ctx_pg_tbl) {
8949 		int i, nr_tbls = rmem->nr_pages;
8950 
8951 		for (i = 0; i < nr_tbls; i++) {
8952 			struct bnxt_ctx_pg_info *pg_tbl;
8953 			struct bnxt_ring_mem_info *rmem2;
8954 
8955 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8956 			if (!pg_tbl)
8957 				continue;
8958 			rmem2 = &pg_tbl->ring_mem;
8959 			bnxt_free_ring(bp, rmem2);
8960 			ctx_pg->ctx_pg_arr[i] = NULL;
8961 			kfree(pg_tbl);
8962 			ctx_pg->ctx_pg_tbl[i] = NULL;
8963 		}
8964 		kfree(ctx_pg->ctx_pg_tbl);
8965 		ctx_pg->ctx_pg_tbl = NULL;
8966 	}
8967 	bnxt_free_ring(bp, rmem);
8968 	ctx_pg->nr_pages = 0;
8969 }
8970 
8971 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
8972 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
8973 				   u8 pg_lvl)
8974 {
8975 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8976 	int i, rc = 0, n = 1;
8977 	u32 mem_size;
8978 
8979 	if (!ctxm->entry_size || !ctx_pg)
8980 		return -EINVAL;
8981 	if (ctxm->instance_bmap)
8982 		n = hweight32(ctxm->instance_bmap);
8983 	if (ctxm->entry_multiple)
8984 		entries = roundup(entries, ctxm->entry_multiple);
8985 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
8986 	mem_size = entries * ctxm->entry_size;
8987 	for (i = 0; i < n && !rc; i++) {
8988 		ctx_pg[i].entries = entries;
8989 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
8990 					    ctxm->init_value ? ctxm : NULL);
8991 	}
8992 	if (!rc)
8993 		ctxm->mem_valid = 1;
8994 	return rc;
8995 }
8996 
8997 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
8998 					       struct bnxt_ctx_mem_type *ctxm,
8999 					       bool last)
9000 {
9001 	struct hwrm_func_backing_store_cfg_v2_input *req;
9002 	u32 instance_bmap = ctxm->instance_bmap;
9003 	int i, j, rc = 0, n = 1;
9004 	__le32 *p;
9005 
9006 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
9007 		return 0;
9008 
9009 	if (instance_bmap)
9010 		n = hweight32(ctxm->instance_bmap);
9011 	else
9012 		instance_bmap = 1;
9013 
9014 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
9015 	if (rc)
9016 		return rc;
9017 	hwrm_req_hold(bp, req);
9018 	req->type = cpu_to_le16(ctxm->type);
9019 	req->entry_size = cpu_to_le16(ctxm->entry_size);
9020 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
9021 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
9022 		struct bnxt_bs_trace_info *bs_trace;
9023 		u32 enables;
9024 
9025 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
9026 		req->enables = cpu_to_le32(enables);
9027 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
9028 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
9029 	}
9030 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
9031 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
9032 		p[i] = cpu_to_le32(ctxm->split[i]);
9033 	for (i = 0, j = 0; j < n && !rc; i++) {
9034 		struct bnxt_ctx_pg_info *ctx_pg;
9035 
9036 		if (!(instance_bmap & (1 << i)))
9037 			continue;
9038 		req->instance = cpu_to_le16(i);
9039 		ctx_pg = &ctxm->pg_info[j++];
9040 		if (!ctx_pg->entries)
9041 			continue;
9042 		req->num_entries = cpu_to_le32(ctx_pg->entries);
9043 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9044 				      &req->page_size_pbl_level,
9045 				      &req->page_dir);
9046 		if (last && j == n)
9047 			req->flags =
9048 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
9049 		rc = hwrm_req_send(bp, req);
9050 	}
9051 	hwrm_req_drop(bp, req);
9052 	return rc;
9053 }
9054 
9055 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
9056 {
9057 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9058 	struct bnxt_ctx_mem_type *ctxm;
9059 	u16 last_type = BNXT_CTX_INV;
9060 	int rc = 0;
9061 	u16 type;
9062 
9063 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_RIGP1; type++) {
9064 		ctxm = &ctx->ctx_arr[type];
9065 		if (!bnxt_bs_trace_avail(bp, type))
9066 			continue;
9067 		if (!ctxm->mem_valid) {
9068 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9069 						     ctxm->max_entries, 1);
9070 			if (rc) {
9071 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9072 					    type);
9073 				continue;
9074 			}
9075 			bnxt_bs_trace_init(bp, ctxm);
9076 		}
9077 		last_type = type;
9078 	}
9079 
9080 	if (last_type == BNXT_CTX_INV) {
9081 		if (!ena)
9082 			return 0;
9083 		else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
9084 			last_type = BNXT_CTX_MAX - 1;
9085 		else
9086 			last_type = BNXT_CTX_L2_MAX - 1;
9087 	}
9088 	ctx->ctx_arr[last_type].last = 1;
9089 
9090 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9091 		ctxm = &ctx->ctx_arr[type];
9092 
9093 		if (!ctxm->mem_valid)
9094 			continue;
9095 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9096 		if (rc)
9097 			return rc;
9098 	}
9099 	return 0;
9100 }
9101 
9102 /**
9103  * __bnxt_copy_ctx_mem - copy host context memory
9104  * @bp: The driver context
9105  * @ctxm: The pointer to the context memory type
9106  * @buf: The destination buffer or NULL to just obtain the length
9107  * @offset: The buffer offset to copy the data to
9108  * @head: The head offset of context memory to copy from
9109  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9110  *
9111  * This function is called for debugging purposes to dump the host context
9112  * used by the chip.
9113  *
9114  * Return: Length of memory copied
9115  */
9116 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9117 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9118 				  size_t offset, size_t head, size_t tail)
9119 {
9120 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9121 	size_t len = 0, total_len = 0;
9122 	int i, n = 1;
9123 
9124 	if (!ctx_pg)
9125 		return 0;
9126 
9127 	if (ctxm->instance_bmap)
9128 		n = hweight32(ctxm->instance_bmap);
9129 	for (i = 0; i < n; i++) {
9130 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9131 					    tail);
9132 		offset += len;
9133 		total_len += len;
9134 	}
9135 	return total_len;
9136 }
9137 
9138 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9139 			 void *buf, size_t offset)
9140 {
9141 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9142 
9143 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9144 }
9145 
9146 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9147 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9148 {
9149 	struct bnxt_ctx_pg_info *ctx_pg;
9150 	int i, n = 1;
9151 
9152 	ctxm->last = 0;
9153 
9154 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9155 		return;
9156 
9157 	ctx_pg = ctxm->pg_info;
9158 	if (ctx_pg) {
9159 		if (ctxm->instance_bmap)
9160 			n = hweight32(ctxm->instance_bmap);
9161 		for (i = 0; i < n; i++)
9162 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9163 
9164 		kfree(ctx_pg);
9165 		ctxm->pg_info = NULL;
9166 		ctxm->mem_valid = 0;
9167 	}
9168 	memset(ctxm, 0, sizeof(*ctxm));
9169 }
9170 
9171 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9172 {
9173 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9174 	u16 type;
9175 
9176 	if (!ctx)
9177 		return;
9178 
9179 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9180 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9181 
9182 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9183 	if (force) {
9184 		kfree(ctx);
9185 		bp->ctx = NULL;
9186 	}
9187 }
9188 
9189 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9190 {
9191 	struct bnxt_ctx_mem_type *ctxm;
9192 	struct bnxt_ctx_mem_info *ctx;
9193 	u32 l2_qps, qp1_qps, max_qps;
9194 	u32 ena, entries_sp, entries;
9195 	u32 srqs, max_srqs, min;
9196 	u32 num_mr, num_ah;
9197 	u32 extra_srqs = 0;
9198 	u32 extra_qps = 0;
9199 	u32 fast_qpmd_qps;
9200 	u8 pg_lvl = 1;
9201 	int i, rc;
9202 
9203 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9204 	if (rc) {
9205 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9206 			   rc);
9207 		return rc;
9208 	}
9209 	ctx = bp->ctx;
9210 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9211 		return 0;
9212 
9213 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9214 	l2_qps = ctxm->qp_l2_entries;
9215 	qp1_qps = ctxm->qp_qp1_entries;
9216 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9217 	max_qps = ctxm->max_entries;
9218 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9219 	srqs = ctxm->srq_l2_entries;
9220 	max_srqs = ctxm->max_entries;
9221 	ena = 0;
9222 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9223 		pg_lvl = 2;
9224 		if (BNXT_SW_RES_LMT(bp)) {
9225 			extra_qps = max_qps - l2_qps - qp1_qps;
9226 			extra_srqs = max_srqs - srqs;
9227 		} else {
9228 			extra_qps = min_t(u32, 65536,
9229 					  max_qps - l2_qps - qp1_qps);
9230 			/* allocate extra qps if fw supports RoCE fast qp
9231 			 * destroy feature
9232 			 */
9233 			extra_qps += fast_qpmd_qps;
9234 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9235 		}
9236 		if (fast_qpmd_qps)
9237 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9238 	}
9239 
9240 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9241 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9242 				     pg_lvl);
9243 	if (rc)
9244 		return rc;
9245 
9246 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9247 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9248 	if (rc)
9249 		return rc;
9250 
9251 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9252 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9253 				     extra_qps * 2, pg_lvl);
9254 	if (rc)
9255 		return rc;
9256 
9257 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9258 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9259 	if (rc)
9260 		return rc;
9261 
9262 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9263 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9264 	if (rc)
9265 		return rc;
9266 
9267 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9268 		goto skip_rdma;
9269 
9270 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9271 	if (BNXT_SW_RES_LMT(bp) &&
9272 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9273 		num_ah = ctxm->mrav_av_entries;
9274 		num_mr = ctxm->max_entries - num_ah;
9275 	} else {
9276 		/* 128K extra is needed to accommodate static AH context
9277 		 * allocation by f/w.
9278 		 */
9279 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9280 		num_ah = min_t(u32, num_mr, 1024 * 128);
9281 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9282 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9283 			ctxm->mrav_av_entries = num_ah;
9284 	}
9285 
9286 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9287 	if (rc)
9288 		return rc;
9289 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9290 
9291 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9292 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9293 	if (rc)
9294 		return rc;
9295 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9296 
9297 skip_rdma:
9298 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9299 	min = ctxm->min_entries;
9300 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9301 		     2 * (extra_qps + qp1_qps) + min;
9302 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9303 	if (rc)
9304 		return rc;
9305 
9306 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9307 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9308 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9309 	if (rc)
9310 		return rc;
9311 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9312 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9313 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9314 
9315 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9316 		rc = bnxt_backing_store_cfg_v2(bp, ena);
9317 	else
9318 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9319 	if (rc) {
9320 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9321 			   rc);
9322 		return rc;
9323 	}
9324 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9325 	return 0;
9326 }
9327 
9328 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9329 {
9330 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9331 	u16 page_attr;
9332 	int rc;
9333 
9334 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9335 		return 0;
9336 
9337 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9338 	if (rc)
9339 		return rc;
9340 
9341 	if (BNXT_PAGE_SIZE == 0x2000)
9342 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9343 	else if (BNXT_PAGE_SIZE == 0x10000)
9344 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9345 	else
9346 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9347 	req->pg_size_lvl = cpu_to_le16(page_attr |
9348 				       bp->fw_crash_mem->ring_mem.depth);
9349 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9350 	req->size = cpu_to_le32(bp->fw_crash_len);
9351 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9352 	return hwrm_req_send(bp, req);
9353 }
9354 
9355 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9356 {
9357 	if (bp->fw_crash_mem) {
9358 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9359 		kfree(bp->fw_crash_mem);
9360 		bp->fw_crash_mem = NULL;
9361 	}
9362 }
9363 
9364 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9365 {
9366 	u32 mem_size = 0;
9367 	int rc;
9368 
9369 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9370 		return 0;
9371 
9372 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9373 	if (rc)
9374 		return rc;
9375 
9376 	mem_size = round_up(mem_size, 4);
9377 
9378 	/* keep and use the existing pages */
9379 	if (bp->fw_crash_mem &&
9380 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9381 		goto alloc_done;
9382 
9383 	if (bp->fw_crash_mem)
9384 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9385 	else
9386 		bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem),
9387 					   GFP_KERNEL);
9388 	if (!bp->fw_crash_mem)
9389 		return -ENOMEM;
9390 
9391 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9392 	if (rc) {
9393 		bnxt_free_crash_dump_mem(bp);
9394 		return rc;
9395 	}
9396 
9397 alloc_done:
9398 	bp->fw_crash_len = mem_size;
9399 	return 0;
9400 }
9401 
9402 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9403 {
9404 	struct hwrm_func_resource_qcaps_output *resp;
9405 	struct hwrm_func_resource_qcaps_input *req;
9406 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9407 	int rc;
9408 
9409 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9410 	if (rc)
9411 		return rc;
9412 
9413 	req->fid = cpu_to_le16(0xffff);
9414 	resp = hwrm_req_hold(bp, req);
9415 	rc = hwrm_req_send_silent(bp, req);
9416 	if (rc)
9417 		goto hwrm_func_resc_qcaps_exit;
9418 
9419 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9420 	if (!all)
9421 		goto hwrm_func_resc_qcaps_exit;
9422 
9423 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9424 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9425 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9426 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9427 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9428 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9429 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9430 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9431 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9432 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9433 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9434 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9435 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9436 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9437 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9438 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9439 
9440 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9441 		u16 max_msix = le16_to_cpu(resp->max_msix);
9442 
9443 		hw_resc->max_nqs = max_msix;
9444 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9445 	}
9446 
9447 	if (BNXT_PF(bp)) {
9448 		struct bnxt_pf_info *pf = &bp->pf;
9449 
9450 		pf->vf_resv_strategy =
9451 			le16_to_cpu(resp->vf_reservation_strategy);
9452 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9453 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9454 	}
9455 hwrm_func_resc_qcaps_exit:
9456 	hwrm_req_drop(bp, req);
9457 	return rc;
9458 }
9459 
9460 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9461 {
9462 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9463 	struct hwrm_port_mac_ptp_qcfg_input *req;
9464 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9465 	u8 flags;
9466 	int rc;
9467 
9468 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9469 		rc = -ENODEV;
9470 		goto no_ptp;
9471 	}
9472 
9473 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9474 	if (rc)
9475 		goto no_ptp;
9476 
9477 	req->port_id = cpu_to_le16(bp->pf.port_id);
9478 	resp = hwrm_req_hold(bp, req);
9479 	rc = hwrm_req_send(bp, req);
9480 	if (rc)
9481 		goto exit;
9482 
9483 	flags = resp->flags;
9484 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9485 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9486 		rc = -ENODEV;
9487 		goto exit;
9488 	}
9489 	if (!ptp) {
9490 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
9491 		if (!ptp) {
9492 			rc = -ENOMEM;
9493 			goto exit;
9494 		}
9495 		ptp->bp = bp;
9496 		bp->ptp_cfg = ptp;
9497 	}
9498 
9499 	if (flags &
9500 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9501 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9502 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9503 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9504 	} else if (BNXT_CHIP_P5(bp)) {
9505 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9506 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9507 	} else {
9508 		rc = -ENODEV;
9509 		goto exit;
9510 	}
9511 	ptp->rtc_configured =
9512 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9513 	rc = bnxt_ptp_init(bp);
9514 	if (rc)
9515 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9516 exit:
9517 	hwrm_req_drop(bp, req);
9518 	if (!rc)
9519 		return 0;
9520 
9521 no_ptp:
9522 	bnxt_ptp_clear(bp);
9523 	kfree(ptp);
9524 	bp->ptp_cfg = NULL;
9525 	return rc;
9526 }
9527 
9528 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9529 {
9530 	struct hwrm_func_qcaps_output *resp;
9531 	struct hwrm_func_qcaps_input *req;
9532 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9533 	u32 flags, flags_ext, flags_ext2;
9534 	int rc;
9535 
9536 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9537 	if (rc)
9538 		return rc;
9539 
9540 	req->fid = cpu_to_le16(0xffff);
9541 	resp = hwrm_req_hold(bp, req);
9542 	rc = hwrm_req_send(bp, req);
9543 	if (rc)
9544 		goto hwrm_func_qcaps_exit;
9545 
9546 	flags = le32_to_cpu(resp->flags);
9547 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9548 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9549 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9550 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9551 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9552 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9553 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9554 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9555 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9556 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9557 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9558 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9559 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9560 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9561 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9562 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9563 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9564 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9565 
9566 	flags_ext = le32_to_cpu(resp->flags_ext);
9567 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9568 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9569 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9570 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9571 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9572 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9573 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9574 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9575 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9576 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9577 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED)
9578 		bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2;
9579 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9580 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9581 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9582 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9583 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9584 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9585 
9586 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9587 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9588 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9589 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9590 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9591 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9592 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9593 	if (flags_ext2 &
9594 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9595 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9596 	if (BNXT_PF(bp) &&
9597 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9598 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9599 
9600 	bp->tx_push_thresh = 0;
9601 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9602 	    BNXT_FW_MAJ(bp) > 217)
9603 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9604 
9605 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9606 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9607 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9608 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9609 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9610 	if (!hw_resc->max_hw_ring_grps)
9611 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9612 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9613 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9614 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9615 
9616 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9617 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9618 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9619 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9620 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9621 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9622 
9623 	if (BNXT_PF(bp)) {
9624 		struct bnxt_pf_info *pf = &bp->pf;
9625 
9626 		pf->fw_fid = le16_to_cpu(resp->fid);
9627 		pf->port_id = le16_to_cpu(resp->port_id);
9628 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9629 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9630 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9631 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9632 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9633 			bp->flags |= BNXT_FLAG_WOL_CAP;
9634 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9635 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9636 		} else {
9637 			bnxt_ptp_clear(bp);
9638 			kfree(bp->ptp_cfg);
9639 			bp->ptp_cfg = NULL;
9640 		}
9641 	} else {
9642 #ifdef CONFIG_BNXT_SRIOV
9643 		struct bnxt_vf_info *vf = &bp->vf;
9644 
9645 		vf->fw_fid = le16_to_cpu(resp->fid);
9646 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9647 #endif
9648 	}
9649 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9650 
9651 hwrm_func_qcaps_exit:
9652 	hwrm_req_drop(bp, req);
9653 	return rc;
9654 }
9655 
9656 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9657 {
9658 	struct hwrm_dbg_qcaps_output *resp;
9659 	struct hwrm_dbg_qcaps_input *req;
9660 	int rc;
9661 
9662 	bp->fw_dbg_cap = 0;
9663 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9664 		return;
9665 
9666 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9667 	if (rc)
9668 		return;
9669 
9670 	req->fid = cpu_to_le16(0xffff);
9671 	resp = hwrm_req_hold(bp, req);
9672 	rc = hwrm_req_send(bp, req);
9673 	if (rc)
9674 		goto hwrm_dbg_qcaps_exit;
9675 
9676 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9677 
9678 hwrm_dbg_qcaps_exit:
9679 	hwrm_req_drop(bp, req);
9680 }
9681 
9682 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9683 
9684 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9685 {
9686 	int rc;
9687 
9688 	rc = __bnxt_hwrm_func_qcaps(bp);
9689 	if (rc)
9690 		return rc;
9691 
9692 	bnxt_hwrm_dbg_qcaps(bp);
9693 
9694 	rc = bnxt_hwrm_queue_qportcfg(bp);
9695 	if (rc) {
9696 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9697 		return rc;
9698 	}
9699 	if (bp->hwrm_spec_code >= 0x10803) {
9700 		rc = bnxt_alloc_ctx_mem(bp);
9701 		if (rc)
9702 			return rc;
9703 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9704 		if (!rc)
9705 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9706 	}
9707 	return 0;
9708 }
9709 
9710 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9711 {
9712 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9713 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9714 	u32 flags;
9715 	int rc;
9716 
9717 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9718 		return 0;
9719 
9720 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9721 	if (rc)
9722 		return rc;
9723 
9724 	resp = hwrm_req_hold(bp, req);
9725 	rc = hwrm_req_send(bp, req);
9726 	if (rc)
9727 		goto hwrm_cfa_adv_qcaps_exit;
9728 
9729 	flags = le32_to_cpu(resp->flags);
9730 	if (flags &
9731 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9732 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9733 
9734 	if (flags &
9735 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9736 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9737 
9738 	if (flags &
9739 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9740 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9741 
9742 hwrm_cfa_adv_qcaps_exit:
9743 	hwrm_req_drop(bp, req);
9744 	return rc;
9745 }
9746 
9747 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9748 {
9749 	if (bp->fw_health)
9750 		return 0;
9751 
9752 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9753 	if (!bp->fw_health)
9754 		return -ENOMEM;
9755 
9756 	mutex_init(&bp->fw_health->lock);
9757 	return 0;
9758 }
9759 
9760 static int bnxt_alloc_fw_health(struct bnxt *bp)
9761 {
9762 	int rc;
9763 
9764 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9765 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9766 		return 0;
9767 
9768 	rc = __bnxt_alloc_fw_health(bp);
9769 	if (rc) {
9770 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9771 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9772 		return rc;
9773 	}
9774 
9775 	return 0;
9776 }
9777 
9778 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9779 {
9780 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9781 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9782 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9783 }
9784 
9785 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9786 {
9787 	struct bnxt_fw_health *fw_health = bp->fw_health;
9788 	u32 reg_type;
9789 
9790 	if (!fw_health)
9791 		return;
9792 
9793 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9794 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9795 		fw_health->status_reliable = false;
9796 
9797 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9798 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9799 		fw_health->resets_reliable = false;
9800 }
9801 
9802 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9803 {
9804 	void __iomem *hs;
9805 	u32 status_loc;
9806 	u32 reg_type;
9807 	u32 sig;
9808 
9809 	if (bp->fw_health)
9810 		bp->fw_health->status_reliable = false;
9811 
9812 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9813 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9814 
9815 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9816 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9817 		if (!bp->chip_num) {
9818 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9819 			bp->chip_num = readl(bp->bar0 +
9820 					     BNXT_FW_HEALTH_WIN_BASE +
9821 					     BNXT_GRC_REG_CHIP_NUM);
9822 		}
9823 		if (!BNXT_CHIP_P5_PLUS(bp))
9824 			return;
9825 
9826 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9827 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9828 	} else {
9829 		status_loc = readl(hs + offsetof(struct hcomm_status,
9830 						 fw_status_loc));
9831 	}
9832 
9833 	if (__bnxt_alloc_fw_health(bp)) {
9834 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9835 		return;
9836 	}
9837 
9838 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9839 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9840 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9841 		__bnxt_map_fw_health_reg(bp, status_loc);
9842 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9843 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9844 	}
9845 
9846 	bp->fw_health->status_reliable = true;
9847 }
9848 
9849 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9850 {
9851 	struct bnxt_fw_health *fw_health = bp->fw_health;
9852 	u32 reg_base = 0xffffffff;
9853 	int i;
9854 
9855 	bp->fw_health->status_reliable = false;
9856 	bp->fw_health->resets_reliable = false;
9857 	/* Only pre-map the monitoring GRC registers using window 3 */
9858 	for (i = 0; i < 4; i++) {
9859 		u32 reg = fw_health->regs[i];
9860 
9861 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9862 			continue;
9863 		if (reg_base == 0xffffffff)
9864 			reg_base = reg & BNXT_GRC_BASE_MASK;
9865 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9866 			return -ERANGE;
9867 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9868 	}
9869 	bp->fw_health->status_reliable = true;
9870 	bp->fw_health->resets_reliable = true;
9871 	if (reg_base == 0xffffffff)
9872 		return 0;
9873 
9874 	__bnxt_map_fw_health_reg(bp, reg_base);
9875 	return 0;
9876 }
9877 
9878 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9879 {
9880 	if (!bp->fw_health)
9881 		return;
9882 
9883 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9884 		bp->fw_health->status_reliable = true;
9885 		bp->fw_health->resets_reliable = true;
9886 	} else {
9887 		bnxt_try_map_fw_health_reg(bp);
9888 	}
9889 }
9890 
9891 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9892 {
9893 	struct bnxt_fw_health *fw_health = bp->fw_health;
9894 	struct hwrm_error_recovery_qcfg_output *resp;
9895 	struct hwrm_error_recovery_qcfg_input *req;
9896 	int rc, i;
9897 
9898 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9899 		return 0;
9900 
9901 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9902 	if (rc)
9903 		return rc;
9904 
9905 	resp = hwrm_req_hold(bp, req);
9906 	rc = hwrm_req_send(bp, req);
9907 	if (rc)
9908 		goto err_recovery_out;
9909 	fw_health->flags = le32_to_cpu(resp->flags);
9910 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
9911 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
9912 		rc = -EINVAL;
9913 		goto err_recovery_out;
9914 	}
9915 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
9916 	fw_health->master_func_wait_dsecs =
9917 		le32_to_cpu(resp->master_func_wait_period);
9918 	fw_health->normal_func_wait_dsecs =
9919 		le32_to_cpu(resp->normal_func_wait_period);
9920 	fw_health->post_reset_wait_dsecs =
9921 		le32_to_cpu(resp->master_func_wait_period_after_reset);
9922 	fw_health->post_reset_max_wait_dsecs =
9923 		le32_to_cpu(resp->max_bailout_time_after_reset);
9924 	fw_health->regs[BNXT_FW_HEALTH_REG] =
9925 		le32_to_cpu(resp->fw_health_status_reg);
9926 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
9927 		le32_to_cpu(resp->fw_heartbeat_reg);
9928 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
9929 		le32_to_cpu(resp->fw_reset_cnt_reg);
9930 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
9931 		le32_to_cpu(resp->reset_inprogress_reg);
9932 	fw_health->fw_reset_inprog_reg_mask =
9933 		le32_to_cpu(resp->reset_inprogress_reg_mask);
9934 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
9935 	if (fw_health->fw_reset_seq_cnt >= 16) {
9936 		rc = -EINVAL;
9937 		goto err_recovery_out;
9938 	}
9939 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
9940 		fw_health->fw_reset_seq_regs[i] =
9941 			le32_to_cpu(resp->reset_reg[i]);
9942 		fw_health->fw_reset_seq_vals[i] =
9943 			le32_to_cpu(resp->reset_reg_val[i]);
9944 		fw_health->fw_reset_seq_delay_msec[i] =
9945 			resp->delay_after_reset[i];
9946 	}
9947 err_recovery_out:
9948 	hwrm_req_drop(bp, req);
9949 	if (!rc)
9950 		rc = bnxt_map_fw_health_regs(bp);
9951 	if (rc)
9952 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9953 	return rc;
9954 }
9955 
9956 static int bnxt_hwrm_func_reset(struct bnxt *bp)
9957 {
9958 	struct hwrm_func_reset_input *req;
9959 	int rc;
9960 
9961 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
9962 	if (rc)
9963 		return rc;
9964 
9965 	req->enables = 0;
9966 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
9967 	return hwrm_req_send(bp, req);
9968 }
9969 
9970 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
9971 {
9972 	struct hwrm_nvm_get_dev_info_output nvm_info;
9973 
9974 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
9975 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
9976 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
9977 			 nvm_info.nvm_cfg_ver_upd);
9978 }
9979 
9980 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
9981 {
9982 	struct hwrm_queue_qportcfg_output *resp;
9983 	struct hwrm_queue_qportcfg_input *req;
9984 	u8 i, j, *qptr;
9985 	bool no_rdma;
9986 	int rc = 0;
9987 
9988 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
9989 	if (rc)
9990 		return rc;
9991 
9992 	resp = hwrm_req_hold(bp, req);
9993 	rc = hwrm_req_send(bp, req);
9994 	if (rc)
9995 		goto qportcfg_exit;
9996 
9997 	if (!resp->max_configurable_queues) {
9998 		rc = -EINVAL;
9999 		goto qportcfg_exit;
10000 	}
10001 	bp->max_tc = resp->max_configurable_queues;
10002 	bp->max_lltc = resp->max_configurable_lossless_queues;
10003 	if (bp->max_tc > BNXT_MAX_QUEUE)
10004 		bp->max_tc = BNXT_MAX_QUEUE;
10005 
10006 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
10007 	qptr = &resp->queue_id0;
10008 	for (i = 0, j = 0; i < bp->max_tc; i++) {
10009 		bp->q_info[j].queue_id = *qptr;
10010 		bp->q_ids[i] = *qptr++;
10011 		bp->q_info[j].queue_profile = *qptr++;
10012 		bp->tc_to_qidx[j] = j;
10013 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
10014 		    (no_rdma && BNXT_PF(bp)))
10015 			j++;
10016 	}
10017 	bp->max_q = bp->max_tc;
10018 	bp->max_tc = max_t(u8, j, 1);
10019 
10020 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
10021 		bp->max_tc = 1;
10022 
10023 	if (bp->max_lltc > bp->max_tc)
10024 		bp->max_lltc = bp->max_tc;
10025 
10026 qportcfg_exit:
10027 	hwrm_req_drop(bp, req);
10028 	return rc;
10029 }
10030 
10031 static int bnxt_hwrm_poll(struct bnxt *bp)
10032 {
10033 	struct hwrm_ver_get_input *req;
10034 	int rc;
10035 
10036 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10037 	if (rc)
10038 		return rc;
10039 
10040 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10041 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10042 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10043 
10044 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
10045 	rc = hwrm_req_send(bp, req);
10046 	return rc;
10047 }
10048 
10049 static int bnxt_hwrm_ver_get(struct bnxt *bp)
10050 {
10051 	struct hwrm_ver_get_output *resp;
10052 	struct hwrm_ver_get_input *req;
10053 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
10054 	u32 dev_caps_cfg, hwrm_ver;
10055 	int rc, len;
10056 
10057 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10058 	if (rc)
10059 		return rc;
10060 
10061 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10062 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
10063 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10064 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10065 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10066 
10067 	resp = hwrm_req_hold(bp, req);
10068 	rc = hwrm_req_send(bp, req);
10069 	if (rc)
10070 		goto hwrm_ver_get_exit;
10071 
10072 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
10073 
10074 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
10075 			     resp->hwrm_intf_min_8b << 8 |
10076 			     resp->hwrm_intf_upd_8b;
10077 	if (resp->hwrm_intf_maj_8b < 1) {
10078 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
10079 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10080 			    resp->hwrm_intf_upd_8b);
10081 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10082 	}
10083 
10084 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10085 			HWRM_VERSION_UPDATE;
10086 
10087 	if (bp->hwrm_spec_code > hwrm_ver)
10088 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10089 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10090 			 HWRM_VERSION_UPDATE);
10091 	else
10092 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10093 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10094 			 resp->hwrm_intf_upd_8b);
10095 
10096 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10097 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10098 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10099 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10100 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10101 		len = FW_VER_STR_LEN;
10102 	} else {
10103 		fw_maj = resp->hwrm_fw_maj_8b;
10104 		fw_min = resp->hwrm_fw_min_8b;
10105 		fw_bld = resp->hwrm_fw_bld_8b;
10106 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10107 		len = BC_HWRM_STR_LEN;
10108 	}
10109 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10110 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10111 		 fw_rsv);
10112 
10113 	if (strlen(resp->active_pkg_name)) {
10114 		int fw_ver_len = strlen(bp->fw_ver_str);
10115 
10116 		snprintf(bp->fw_ver_str + fw_ver_len,
10117 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10118 			 resp->active_pkg_name);
10119 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10120 	}
10121 
10122 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10123 	if (!bp->hwrm_cmd_timeout)
10124 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10125 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10126 	if (!bp->hwrm_cmd_max_timeout)
10127 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10128 	else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT)
10129 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n",
10130 			    bp->hwrm_cmd_max_timeout / 1000);
10131 
10132 	if (resp->hwrm_intf_maj_8b >= 1) {
10133 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10134 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10135 	}
10136 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10137 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10138 
10139 	bp->chip_num = le16_to_cpu(resp->chip_num);
10140 	bp->chip_rev = resp->chip_rev;
10141 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10142 	    !resp->chip_metal)
10143 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10144 
10145 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10146 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10147 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10148 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10149 
10150 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10151 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10152 
10153 	if (dev_caps_cfg &
10154 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10155 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10156 
10157 	if (dev_caps_cfg &
10158 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10159 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10160 
10161 	if (dev_caps_cfg &
10162 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10163 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10164 
10165 hwrm_ver_get_exit:
10166 	hwrm_req_drop(bp, req);
10167 	return rc;
10168 }
10169 
10170 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10171 {
10172 	struct hwrm_fw_set_time_input *req;
10173 	struct tm tm;
10174 	time64_t now = ktime_get_real_seconds();
10175 	int rc;
10176 
10177 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10178 	    bp->hwrm_spec_code < 0x10400)
10179 		return -EOPNOTSUPP;
10180 
10181 	time64_to_tm(now, 0, &tm);
10182 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10183 	if (rc)
10184 		return rc;
10185 
10186 	req->year = cpu_to_le16(1900 + tm.tm_year);
10187 	req->month = 1 + tm.tm_mon;
10188 	req->day = tm.tm_mday;
10189 	req->hour = tm.tm_hour;
10190 	req->minute = tm.tm_min;
10191 	req->second = tm.tm_sec;
10192 	return hwrm_req_send(bp, req);
10193 }
10194 
10195 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10196 {
10197 	u64 sw_tmp;
10198 
10199 	hw &= mask;
10200 	sw_tmp = (*sw & ~mask) | hw;
10201 	if (hw < (*sw & mask))
10202 		sw_tmp += mask + 1;
10203 	WRITE_ONCE(*sw, sw_tmp);
10204 }
10205 
10206 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10207 				    int count, bool ignore_zero)
10208 {
10209 	int i;
10210 
10211 	for (i = 0; i < count; i++) {
10212 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10213 
10214 		if (ignore_zero && !hw)
10215 			continue;
10216 
10217 		if (masks[i] == -1ULL)
10218 			sw_stats[i] = hw;
10219 		else
10220 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10221 	}
10222 }
10223 
10224 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10225 {
10226 	if (!stats->hw_stats)
10227 		return;
10228 
10229 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10230 				stats->hw_masks, stats->len / 8, false);
10231 }
10232 
10233 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10234 {
10235 	struct bnxt_stats_mem *ring0_stats;
10236 	bool ignore_zero = false;
10237 	int i;
10238 
10239 	/* Chip bug.  Counter intermittently becomes 0. */
10240 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10241 		ignore_zero = true;
10242 
10243 	for (i = 0; i < bp->cp_nr_rings; i++) {
10244 		struct bnxt_napi *bnapi = bp->bnapi[i];
10245 		struct bnxt_cp_ring_info *cpr;
10246 		struct bnxt_stats_mem *stats;
10247 
10248 		cpr = &bnapi->cp_ring;
10249 		stats = &cpr->stats;
10250 		if (!i)
10251 			ring0_stats = stats;
10252 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10253 					ring0_stats->hw_masks,
10254 					ring0_stats->len / 8, ignore_zero);
10255 	}
10256 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10257 		struct bnxt_stats_mem *stats = &bp->port_stats;
10258 		__le64 *hw_stats = stats->hw_stats;
10259 		u64 *sw_stats = stats->sw_stats;
10260 		u64 *masks = stats->hw_masks;
10261 		int cnt;
10262 
10263 		cnt = sizeof(struct rx_port_stats) / 8;
10264 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10265 
10266 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10267 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10268 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10269 		cnt = sizeof(struct tx_port_stats) / 8;
10270 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10271 	}
10272 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10273 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10274 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10275 	}
10276 }
10277 
10278 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10279 {
10280 	struct hwrm_port_qstats_input *req;
10281 	struct bnxt_pf_info *pf = &bp->pf;
10282 	int rc;
10283 
10284 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10285 		return 0;
10286 
10287 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10288 		return -EOPNOTSUPP;
10289 
10290 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10291 	if (rc)
10292 		return rc;
10293 
10294 	req->flags = flags;
10295 	req->port_id = cpu_to_le16(pf->port_id);
10296 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10297 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10298 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10299 	return hwrm_req_send(bp, req);
10300 }
10301 
10302 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10303 {
10304 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10305 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10306 	struct hwrm_port_qstats_ext_output *resp_qs;
10307 	struct hwrm_port_qstats_ext_input *req_qs;
10308 	struct bnxt_pf_info *pf = &bp->pf;
10309 	u32 tx_stat_size;
10310 	int rc;
10311 
10312 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10313 		return 0;
10314 
10315 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10316 		return -EOPNOTSUPP;
10317 
10318 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10319 	if (rc)
10320 		return rc;
10321 
10322 	req_qs->flags = flags;
10323 	req_qs->port_id = cpu_to_le16(pf->port_id);
10324 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10325 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10326 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10327 		       sizeof(struct tx_port_stats_ext) : 0;
10328 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10329 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10330 	resp_qs = hwrm_req_hold(bp, req_qs);
10331 	rc = hwrm_req_send(bp, req_qs);
10332 	if (!rc) {
10333 		bp->fw_rx_stats_ext_size =
10334 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10335 		if (BNXT_FW_MAJ(bp) < 220 &&
10336 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10337 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10338 
10339 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10340 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10341 	} else {
10342 		bp->fw_rx_stats_ext_size = 0;
10343 		bp->fw_tx_stats_ext_size = 0;
10344 	}
10345 	hwrm_req_drop(bp, req_qs);
10346 
10347 	if (flags)
10348 		return rc;
10349 
10350 	if (bp->fw_tx_stats_ext_size <=
10351 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10352 		bp->pri2cos_valid = 0;
10353 		return rc;
10354 	}
10355 
10356 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10357 	if (rc)
10358 		return rc;
10359 
10360 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10361 
10362 	resp_qc = hwrm_req_hold(bp, req_qc);
10363 	rc = hwrm_req_send(bp, req_qc);
10364 	if (!rc) {
10365 		u8 *pri2cos;
10366 		int i, j;
10367 
10368 		pri2cos = &resp_qc->pri0_cos_queue_id;
10369 		for (i = 0; i < 8; i++) {
10370 			u8 queue_id = pri2cos[i];
10371 			u8 queue_idx;
10372 
10373 			/* Per port queue IDs start from 0, 10, 20, etc */
10374 			queue_idx = queue_id % 10;
10375 			if (queue_idx > BNXT_MAX_QUEUE) {
10376 				bp->pri2cos_valid = false;
10377 				hwrm_req_drop(bp, req_qc);
10378 				return rc;
10379 			}
10380 			for (j = 0; j < bp->max_q; j++) {
10381 				if (bp->q_ids[j] == queue_id)
10382 					bp->pri2cos_idx[i] = queue_idx;
10383 			}
10384 		}
10385 		bp->pri2cos_valid = true;
10386 	}
10387 	hwrm_req_drop(bp, req_qc);
10388 
10389 	return rc;
10390 }
10391 
10392 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10393 {
10394 	bnxt_hwrm_tunnel_dst_port_free(bp,
10395 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10396 	bnxt_hwrm_tunnel_dst_port_free(bp,
10397 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10398 }
10399 
10400 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10401 {
10402 	int rc, i;
10403 	u32 tpa_flags = 0;
10404 
10405 	if (set_tpa)
10406 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10407 	else if (BNXT_NO_FW_ACCESS(bp))
10408 		return 0;
10409 	for (i = 0; i < bp->nr_vnics; i++) {
10410 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10411 		if (rc) {
10412 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10413 				   i, rc);
10414 			return rc;
10415 		}
10416 	}
10417 	return 0;
10418 }
10419 
10420 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10421 {
10422 	int i;
10423 
10424 	for (i = 0; i < bp->nr_vnics; i++)
10425 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10426 }
10427 
10428 static void bnxt_clear_vnic(struct bnxt *bp)
10429 {
10430 	if (!bp->vnic_info)
10431 		return;
10432 
10433 	bnxt_hwrm_clear_vnic_filter(bp);
10434 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10435 		/* clear all RSS setting before free vnic ctx */
10436 		bnxt_hwrm_clear_vnic_rss(bp);
10437 		bnxt_hwrm_vnic_ctx_free(bp);
10438 	}
10439 	/* before free the vnic, undo the vnic tpa settings */
10440 	if (bp->flags & BNXT_FLAG_TPA)
10441 		bnxt_set_tpa(bp, false);
10442 	bnxt_hwrm_vnic_free(bp);
10443 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10444 		bnxt_hwrm_vnic_ctx_free(bp);
10445 }
10446 
10447 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10448 				    bool irq_re_init)
10449 {
10450 	bnxt_clear_vnic(bp);
10451 	bnxt_hwrm_ring_free(bp, close_path);
10452 	bnxt_hwrm_ring_grp_free(bp);
10453 	if (irq_re_init) {
10454 		bnxt_hwrm_stat_ctx_free(bp);
10455 		bnxt_hwrm_free_tunnel_ports(bp);
10456 	}
10457 }
10458 
10459 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10460 {
10461 	struct hwrm_func_cfg_input *req;
10462 	u8 evb_mode;
10463 	int rc;
10464 
10465 	if (br_mode == BRIDGE_MODE_VEB)
10466 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10467 	else if (br_mode == BRIDGE_MODE_VEPA)
10468 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10469 	else
10470 		return -EINVAL;
10471 
10472 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10473 	if (rc)
10474 		return rc;
10475 
10476 	req->fid = cpu_to_le16(0xffff);
10477 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10478 	req->evb_mode = evb_mode;
10479 	return hwrm_req_send(bp, req);
10480 }
10481 
10482 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10483 {
10484 	struct hwrm_func_cfg_input *req;
10485 	int rc;
10486 
10487 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10488 		return 0;
10489 
10490 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10491 	if (rc)
10492 		return rc;
10493 
10494 	req->fid = cpu_to_le16(0xffff);
10495 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10496 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10497 	if (size == 128)
10498 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10499 
10500 	return hwrm_req_send(bp, req);
10501 }
10502 
10503 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10504 {
10505 	int rc;
10506 
10507 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10508 		goto skip_rss_ctx;
10509 
10510 	/* allocate context for vnic */
10511 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10512 	if (rc) {
10513 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10514 			   vnic->vnic_id, rc);
10515 		goto vnic_setup_err;
10516 	}
10517 	bp->rsscos_nr_ctxs++;
10518 
10519 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10520 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10521 		if (rc) {
10522 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10523 				   vnic->vnic_id, rc);
10524 			goto vnic_setup_err;
10525 		}
10526 		bp->rsscos_nr_ctxs++;
10527 	}
10528 
10529 skip_rss_ctx:
10530 	/* configure default vnic, ring grp */
10531 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10532 	if (rc) {
10533 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10534 			   vnic->vnic_id, rc);
10535 		goto vnic_setup_err;
10536 	}
10537 
10538 	/* Enable RSS hashing on vnic */
10539 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10540 	if (rc) {
10541 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10542 			   vnic->vnic_id, rc);
10543 		goto vnic_setup_err;
10544 	}
10545 
10546 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10547 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10548 		if (rc) {
10549 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10550 				   vnic->vnic_id, rc);
10551 		}
10552 	}
10553 
10554 vnic_setup_err:
10555 	return rc;
10556 }
10557 
10558 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10559 			  u8 valid)
10560 {
10561 	struct hwrm_vnic_update_input *req;
10562 	int rc;
10563 
10564 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10565 	if (rc)
10566 		return rc;
10567 
10568 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10569 
10570 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10571 		req->mru = cpu_to_le16(vnic->mru);
10572 
10573 	req->enables = cpu_to_le32(valid);
10574 
10575 	return hwrm_req_send(bp, req);
10576 }
10577 
10578 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10579 {
10580 	int rc;
10581 
10582 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10583 	if (rc) {
10584 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10585 			   vnic->vnic_id, rc);
10586 		return rc;
10587 	}
10588 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10589 	if (rc)
10590 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10591 			   vnic->vnic_id, rc);
10592 	return rc;
10593 }
10594 
10595 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10596 {
10597 	int rc, i, nr_ctxs;
10598 
10599 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10600 	for (i = 0; i < nr_ctxs; i++) {
10601 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10602 		if (rc) {
10603 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10604 				   vnic->vnic_id, i, rc);
10605 			break;
10606 		}
10607 		bp->rsscos_nr_ctxs++;
10608 	}
10609 	if (i < nr_ctxs)
10610 		return -ENOMEM;
10611 
10612 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10613 	if (rc)
10614 		return rc;
10615 
10616 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10617 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10618 		if (rc) {
10619 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10620 				   vnic->vnic_id, rc);
10621 		}
10622 	}
10623 	return rc;
10624 }
10625 
10626 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10627 {
10628 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10629 		return __bnxt_setup_vnic_p5(bp, vnic);
10630 	else
10631 		return __bnxt_setup_vnic(bp, vnic);
10632 }
10633 
10634 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10635 				     struct bnxt_vnic_info *vnic,
10636 				     u16 start_rx_ring_idx, int rx_rings)
10637 {
10638 	int rc;
10639 
10640 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10641 	if (rc) {
10642 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10643 			   vnic->vnic_id, rc);
10644 		return rc;
10645 	}
10646 	return bnxt_setup_vnic(bp, vnic);
10647 }
10648 
10649 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10650 {
10651 	struct bnxt_vnic_info *vnic;
10652 	int i, rc = 0;
10653 
10654 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10655 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10656 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10657 	}
10658 
10659 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10660 		return 0;
10661 
10662 	for (i = 0; i < bp->rx_nr_rings; i++) {
10663 		u16 vnic_id = i + 1;
10664 		u16 ring_id = i;
10665 
10666 		if (vnic_id >= bp->nr_vnics)
10667 			break;
10668 
10669 		vnic = &bp->vnic_info[vnic_id];
10670 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10671 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10672 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10673 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10674 			break;
10675 	}
10676 	return rc;
10677 }
10678 
10679 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10680 			  bool all)
10681 {
10682 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10683 	struct bnxt_filter_base *usr_fltr, *tmp;
10684 	struct bnxt_ntuple_filter *ntp_fltr;
10685 	int i;
10686 
10687 	if (netif_running(bp->dev)) {
10688 		bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10689 		for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10690 			if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10691 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10692 		}
10693 	}
10694 	if (!all)
10695 		return;
10696 
10697 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10698 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10699 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10700 			ntp_fltr = container_of(usr_fltr,
10701 						struct bnxt_ntuple_filter,
10702 						base);
10703 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10704 			bnxt_del_ntp_filter(bp, ntp_fltr);
10705 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10706 		}
10707 	}
10708 
10709 	if (vnic->rss_table)
10710 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10711 				  vnic->rss_table,
10712 				  vnic->rss_table_dma_addr);
10713 	bp->num_rss_ctx--;
10714 }
10715 
10716 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10717 {
10718 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10719 	struct ethtool_rxfh_context *ctx;
10720 	unsigned long context;
10721 
10722 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10723 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10724 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10725 
10726 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10727 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10728 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10729 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10730 				   rss_ctx->index);
10731 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10732 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
10733 		}
10734 	}
10735 }
10736 
10737 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
10738 {
10739 	struct ethtool_rxfh_context *ctx;
10740 	unsigned long context;
10741 
10742 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10743 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10744 
10745 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
10746 	}
10747 }
10748 
10749 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
10750 static bool bnxt_promisc_ok(struct bnxt *bp)
10751 {
10752 #ifdef CONFIG_BNXT_SRIOV
10753 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10754 		return false;
10755 #endif
10756 	return true;
10757 }
10758 
10759 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10760 {
10761 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10762 	unsigned int rc = 0;
10763 
10764 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10765 	if (rc) {
10766 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10767 			   rc);
10768 		return rc;
10769 	}
10770 
10771 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10772 	if (rc) {
10773 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10774 			   rc);
10775 		return rc;
10776 	}
10777 	return rc;
10778 }
10779 
10780 static int bnxt_cfg_rx_mode(struct bnxt *);
10781 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10782 
10783 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10784 {
10785 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10786 	int rc = 0;
10787 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10788 
10789 	if (irq_re_init) {
10790 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10791 		if (rc) {
10792 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10793 				   rc);
10794 			goto err_out;
10795 		}
10796 	}
10797 
10798 	rc = bnxt_hwrm_ring_alloc(bp);
10799 	if (rc) {
10800 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10801 		goto err_out;
10802 	}
10803 
10804 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10805 	if (rc) {
10806 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10807 		goto err_out;
10808 	}
10809 
10810 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10811 		rx_nr_rings--;
10812 
10813 	/* default vnic 0 */
10814 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10815 	if (rc) {
10816 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10817 		goto err_out;
10818 	}
10819 
10820 	if (BNXT_VF(bp))
10821 		bnxt_hwrm_func_qcfg(bp);
10822 
10823 	rc = bnxt_setup_vnic(bp, vnic);
10824 	if (rc)
10825 		goto err_out;
10826 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10827 		bnxt_hwrm_update_rss_hash_cfg(bp);
10828 
10829 	if (bp->flags & BNXT_FLAG_RFS) {
10830 		rc = bnxt_alloc_rfs_vnics(bp);
10831 		if (rc)
10832 			goto err_out;
10833 	}
10834 
10835 	if (bp->flags & BNXT_FLAG_TPA) {
10836 		rc = bnxt_set_tpa(bp, true);
10837 		if (rc)
10838 			goto err_out;
10839 	}
10840 
10841 	if (BNXT_VF(bp))
10842 		bnxt_update_vf_mac(bp);
10843 
10844 	/* Filter for default vnic 0 */
10845 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
10846 	if (rc) {
10847 		if (BNXT_VF(bp) && rc == -ENODEV)
10848 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
10849 		else
10850 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
10851 		goto err_out;
10852 	}
10853 	vnic->uc_filter_count = 1;
10854 
10855 	vnic->rx_mask = 0;
10856 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
10857 		goto skip_rx_mask;
10858 
10859 	if (bp->dev->flags & IFF_BROADCAST)
10860 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10861 
10862 	if (bp->dev->flags & IFF_PROMISC)
10863 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10864 
10865 	if (bp->dev->flags & IFF_ALLMULTI) {
10866 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10867 		vnic->mc_list_count = 0;
10868 	} else if (bp->dev->flags & IFF_MULTICAST) {
10869 		u32 mask = 0;
10870 
10871 		bnxt_mc_list_updated(bp, &mask);
10872 		vnic->rx_mask |= mask;
10873 	}
10874 
10875 	rc = bnxt_cfg_rx_mode(bp);
10876 	if (rc)
10877 		goto err_out;
10878 
10879 skip_rx_mask:
10880 	rc = bnxt_hwrm_set_coal(bp);
10881 	if (rc)
10882 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
10883 				rc);
10884 
10885 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10886 		rc = bnxt_setup_nitroa0_vnic(bp);
10887 		if (rc)
10888 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
10889 				   rc);
10890 	}
10891 
10892 	if (BNXT_VF(bp)) {
10893 		bnxt_hwrm_func_qcfg(bp);
10894 		netdev_update_features(bp->dev);
10895 	}
10896 
10897 	return 0;
10898 
10899 err_out:
10900 	bnxt_hwrm_resource_free(bp, 0, true);
10901 
10902 	return rc;
10903 }
10904 
10905 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
10906 {
10907 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
10908 	return 0;
10909 }
10910 
10911 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
10912 {
10913 	bnxt_init_cp_rings(bp);
10914 	bnxt_init_rx_rings(bp);
10915 	bnxt_init_tx_rings(bp);
10916 	bnxt_init_ring_grps(bp, irq_re_init);
10917 	bnxt_init_vnics(bp);
10918 
10919 	return bnxt_init_chip(bp, irq_re_init);
10920 }
10921 
10922 static int bnxt_set_real_num_queues(struct bnxt *bp)
10923 {
10924 	int rc;
10925 	struct net_device *dev = bp->dev;
10926 
10927 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
10928 					  bp->tx_nr_rings_xdp);
10929 	if (rc)
10930 		return rc;
10931 
10932 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
10933 	if (rc)
10934 		return rc;
10935 
10936 #ifdef CONFIG_RFS_ACCEL
10937 	if (bp->flags & BNXT_FLAG_RFS)
10938 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
10939 #endif
10940 
10941 	return rc;
10942 }
10943 
10944 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10945 			     bool shared)
10946 {
10947 	int _rx = *rx, _tx = *tx;
10948 
10949 	if (shared) {
10950 		*rx = min_t(int, _rx, max);
10951 		*tx = min_t(int, _tx, max);
10952 	} else {
10953 		if (max < 2)
10954 			return -ENOMEM;
10955 
10956 		while (_rx + _tx > max) {
10957 			if (_rx > _tx && _rx > 1)
10958 				_rx--;
10959 			else if (_tx > 1)
10960 				_tx--;
10961 		}
10962 		*rx = _rx;
10963 		*tx = _tx;
10964 	}
10965 	return 0;
10966 }
10967 
10968 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
10969 {
10970 	return (tx - tx_xdp) / tx_sets + tx_xdp;
10971 }
10972 
10973 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
10974 {
10975 	int tcs = bp->num_tc;
10976 
10977 	if (!tcs)
10978 		tcs = 1;
10979 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
10980 }
10981 
10982 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
10983 {
10984 	int tcs = bp->num_tc;
10985 
10986 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
10987 	       bp->tx_nr_rings_xdp;
10988 }
10989 
10990 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10991 			   bool sh)
10992 {
10993 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
10994 
10995 	if (tx_cp != *tx) {
10996 		int tx_saved = tx_cp, rc;
10997 
10998 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
10999 		if (rc)
11000 			return rc;
11001 		if (tx_cp != tx_saved)
11002 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
11003 		return 0;
11004 	}
11005 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
11006 }
11007 
11008 static void bnxt_setup_msix(struct bnxt *bp)
11009 {
11010 	const int len = sizeof(bp->irq_tbl[0].name);
11011 	struct net_device *dev = bp->dev;
11012 	int tcs, i;
11013 
11014 	tcs = bp->num_tc;
11015 	if (tcs) {
11016 		int i, off, count;
11017 
11018 		for (i = 0; i < tcs; i++) {
11019 			count = bp->tx_nr_rings_per_tc;
11020 			off = BNXT_TC_TO_RING_BASE(bp, i);
11021 			netdev_set_tc_queue(dev, i, count, off);
11022 		}
11023 	}
11024 
11025 	for (i = 0; i < bp->cp_nr_rings; i++) {
11026 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11027 		char *attr;
11028 
11029 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11030 			attr = "TxRx";
11031 		else if (i < bp->rx_nr_rings)
11032 			attr = "rx";
11033 		else
11034 			attr = "tx";
11035 
11036 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
11037 			 attr, i);
11038 		bp->irq_tbl[map_idx].handler = bnxt_msix;
11039 	}
11040 }
11041 
11042 static int bnxt_init_int_mode(struct bnxt *bp);
11043 
11044 static int bnxt_change_msix(struct bnxt *bp, int total)
11045 {
11046 	struct msi_map map;
11047 	int i;
11048 
11049 	/* add MSIX to the end if needed */
11050 	for (i = bp->total_irqs; i < total; i++) {
11051 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
11052 		if (map.index < 0)
11053 			return bp->total_irqs;
11054 		bp->irq_tbl[i].vector = map.virq;
11055 		bp->total_irqs++;
11056 	}
11057 
11058 	/* trim MSIX from the end if needed */
11059 	for (i = bp->total_irqs; i > total; i--) {
11060 		map.index = i - 1;
11061 		map.virq = bp->irq_tbl[i - 1].vector;
11062 		pci_msix_free_irq(bp->pdev, map);
11063 		bp->total_irqs--;
11064 	}
11065 	return bp->total_irqs;
11066 }
11067 
11068 static int bnxt_setup_int_mode(struct bnxt *bp)
11069 {
11070 	int rc;
11071 
11072 	if (!bp->irq_tbl) {
11073 		rc = bnxt_init_int_mode(bp);
11074 		if (rc || !bp->irq_tbl)
11075 			return rc ?: -ENODEV;
11076 	}
11077 
11078 	bnxt_setup_msix(bp);
11079 
11080 	rc = bnxt_set_real_num_queues(bp);
11081 	return rc;
11082 }
11083 
11084 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
11085 {
11086 	return bp->hw_resc.max_rsscos_ctxs;
11087 }
11088 
11089 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
11090 {
11091 	return bp->hw_resc.max_vnics;
11092 }
11093 
11094 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11095 {
11096 	return bp->hw_resc.max_stat_ctxs;
11097 }
11098 
11099 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11100 {
11101 	return bp->hw_resc.max_cp_rings;
11102 }
11103 
11104 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11105 {
11106 	unsigned int cp = bp->hw_resc.max_cp_rings;
11107 
11108 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11109 		cp -= bnxt_get_ulp_msix_num(bp);
11110 
11111 	return cp;
11112 }
11113 
11114 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11115 {
11116 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11117 
11118 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11119 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11120 
11121 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11122 }
11123 
11124 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11125 {
11126 	bp->hw_resc.max_irqs = max_irqs;
11127 }
11128 
11129 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11130 {
11131 	unsigned int cp;
11132 
11133 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11134 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11135 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11136 	else
11137 		return cp - bp->cp_nr_rings;
11138 }
11139 
11140 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11141 {
11142 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11143 }
11144 
11145 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11146 {
11147 	int max_irq = bnxt_get_max_func_irqs(bp);
11148 	int total_req = bp->cp_nr_rings + num;
11149 
11150 	if (max_irq < total_req) {
11151 		num = max_irq - bp->cp_nr_rings;
11152 		if (num <= 0)
11153 			return 0;
11154 	}
11155 	return num;
11156 }
11157 
11158 static int bnxt_get_num_msix(struct bnxt *bp)
11159 {
11160 	if (!BNXT_NEW_RM(bp))
11161 		return bnxt_get_max_func_irqs(bp);
11162 
11163 	return bnxt_nq_rings_in_use(bp);
11164 }
11165 
11166 static int bnxt_init_int_mode(struct bnxt *bp)
11167 {
11168 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
11169 
11170 	total_vecs = bnxt_get_num_msix(bp);
11171 	max = bnxt_get_max_func_irqs(bp);
11172 	if (total_vecs > max)
11173 		total_vecs = max;
11174 
11175 	if (!total_vecs)
11176 		return 0;
11177 
11178 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11179 		min = 2;
11180 
11181 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11182 					   PCI_IRQ_MSIX);
11183 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11184 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11185 		rc = -ENODEV;
11186 		goto msix_setup_exit;
11187 	}
11188 
11189 	tbl_size = total_vecs;
11190 	if (pci_msix_can_alloc_dyn(bp->pdev))
11191 		tbl_size = max;
11192 	bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL);
11193 	if (bp->irq_tbl) {
11194 		for (i = 0; i < total_vecs; i++)
11195 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11196 
11197 		bp->total_irqs = total_vecs;
11198 		/* Trim rings based upon num of vectors allocated */
11199 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11200 				     total_vecs - ulp_msix, min == 1);
11201 		if (rc)
11202 			goto msix_setup_exit;
11203 
11204 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11205 		bp->cp_nr_rings = (min == 1) ?
11206 				  max_t(int, tx_cp, bp->rx_nr_rings) :
11207 				  tx_cp + bp->rx_nr_rings;
11208 
11209 	} else {
11210 		rc = -ENOMEM;
11211 		goto msix_setup_exit;
11212 	}
11213 	return 0;
11214 
11215 msix_setup_exit:
11216 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11217 	kfree(bp->irq_tbl);
11218 	bp->irq_tbl = NULL;
11219 	pci_free_irq_vectors(bp->pdev);
11220 	return rc;
11221 }
11222 
11223 static void bnxt_clear_int_mode(struct bnxt *bp)
11224 {
11225 	pci_free_irq_vectors(bp->pdev);
11226 
11227 	kfree(bp->irq_tbl);
11228 	bp->irq_tbl = NULL;
11229 }
11230 
11231 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11232 {
11233 	bool irq_cleared = false;
11234 	bool irq_change = false;
11235 	int tcs = bp->num_tc;
11236 	int irqs_required;
11237 	int rc;
11238 
11239 	if (!bnxt_need_reserve_rings(bp))
11240 		return 0;
11241 
11242 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
11243 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11244 
11245 		if (ulp_msix > bp->ulp_num_msix_want)
11246 			ulp_msix = bp->ulp_num_msix_want;
11247 		irqs_required = ulp_msix + bp->cp_nr_rings;
11248 	} else {
11249 		irqs_required = bnxt_get_num_msix(bp);
11250 	}
11251 
11252 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11253 		irq_change = true;
11254 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11255 			bnxt_ulp_irq_stop(bp);
11256 			bnxt_clear_int_mode(bp);
11257 			irq_cleared = true;
11258 		}
11259 	}
11260 	rc = __bnxt_reserve_rings(bp);
11261 	if (irq_cleared) {
11262 		if (!rc)
11263 			rc = bnxt_init_int_mode(bp);
11264 		bnxt_ulp_irq_restart(bp, rc);
11265 	} else if (irq_change && !rc) {
11266 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11267 			rc = -ENOSPC;
11268 	}
11269 	if (rc) {
11270 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11271 		return rc;
11272 	}
11273 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11274 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11275 		netdev_err(bp->dev, "tx ring reservation failure\n");
11276 		netdev_reset_tc(bp->dev);
11277 		bp->num_tc = 0;
11278 		if (bp->tx_nr_rings_xdp)
11279 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11280 		else
11281 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11282 		return -ENOMEM;
11283 	}
11284 	return 0;
11285 }
11286 
11287 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx)
11288 {
11289 	struct bnxt_tx_ring_info *txr;
11290 	struct netdev_queue *txq;
11291 	struct bnxt_napi *bnapi;
11292 	int i;
11293 
11294 	bnapi = bp->bnapi[idx];
11295 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11296 		WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11297 		synchronize_net();
11298 
11299 		if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) {
11300 			txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11301 			if (txq) {
11302 				__netif_tx_lock_bh(txq);
11303 				netif_tx_stop_queue(txq);
11304 				__netif_tx_unlock_bh(txq);
11305 			}
11306 		}
11307 
11308 		if (!bp->tph_mode)
11309 			continue;
11310 
11311 		bnxt_hwrm_tx_ring_free(bp, txr, true);
11312 		bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr);
11313 		bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index);
11314 		bnxt_clear_one_cp_ring(bp, txr->tx_cpr);
11315 	}
11316 }
11317 
11318 static int bnxt_tx_queue_start(struct bnxt *bp, int idx)
11319 {
11320 	struct bnxt_tx_ring_info *txr;
11321 	struct netdev_queue *txq;
11322 	struct bnxt_napi *bnapi;
11323 	int rc, i;
11324 
11325 	bnapi = bp->bnapi[idx];
11326 	/* All rings have been reserved and previously allocated.
11327 	 * Reallocating with the same parameters should never fail.
11328 	 */
11329 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11330 		if (!bp->tph_mode)
11331 			goto start_tx;
11332 
11333 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
11334 		if (rc)
11335 			return rc;
11336 
11337 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false);
11338 		if (rc)
11339 			return rc;
11340 
11341 		txr->tx_prod = 0;
11342 		txr->tx_cons = 0;
11343 		txr->tx_hw_cons = 0;
11344 start_tx:
11345 		WRITE_ONCE(txr->dev_state, 0);
11346 		synchronize_net();
11347 
11348 		if (bnapi->flags & BNXT_NAPI_FLAG_XDP)
11349 			continue;
11350 
11351 		txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11352 		if (txq)
11353 			netif_tx_start_queue(txq);
11354 	}
11355 
11356 	return 0;
11357 }
11358 
11359 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify,
11360 				     const cpumask_t *mask)
11361 {
11362 	struct bnxt_irq *irq;
11363 	u16 tag;
11364 	int err;
11365 
11366 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11367 
11368 	if (!irq->bp->tph_mode)
11369 		return;
11370 
11371 	cpumask_copy(irq->cpu_mask, mask);
11372 
11373 	if (irq->ring_nr >= irq->bp->rx_nr_rings)
11374 		return;
11375 
11376 	if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11377 				cpumask_first(irq->cpu_mask), &tag))
11378 		return;
11379 
11380 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag))
11381 		return;
11382 
11383 	rtnl_lock();
11384 	if (netif_running(irq->bp->dev)) {
11385 		err = netdev_rx_queue_restart(irq->bp->dev, irq->ring_nr);
11386 		if (err)
11387 			netdev_err(irq->bp->dev,
11388 				   "RX queue restart failed: err=%d\n", err);
11389 	}
11390 	rtnl_unlock();
11391 }
11392 
11393 static void bnxt_irq_affinity_release(struct kref *ref)
11394 {
11395 	struct irq_affinity_notify *notify =
11396 		container_of(ref, struct irq_affinity_notify, kref);
11397 	struct bnxt_irq *irq;
11398 
11399 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11400 
11401 	if (!irq->bp->tph_mode)
11402 		return;
11403 
11404 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) {
11405 		netdev_err(irq->bp->dev,
11406 			   "Setting ST=0 for MSIX entry %d failed\n",
11407 			   irq->msix_nr);
11408 		return;
11409 	}
11410 }
11411 
11412 static void bnxt_release_irq_notifier(struct bnxt_irq *irq)
11413 {
11414 	irq_set_affinity_notifier(irq->vector, NULL);
11415 }
11416 
11417 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq)
11418 {
11419 	struct irq_affinity_notify *notify;
11420 
11421 	irq->bp = bp;
11422 
11423 	/* Nothing to do if TPH is not enabled */
11424 	if (!bp->tph_mode)
11425 		return;
11426 
11427 	/* Register IRQ affinity notifier */
11428 	notify = &irq->affinity_notify;
11429 	notify->irq = irq->vector;
11430 	notify->notify = bnxt_irq_affinity_notify;
11431 	notify->release = bnxt_irq_affinity_release;
11432 
11433 	irq_set_affinity_notifier(irq->vector, notify);
11434 }
11435 
11436 static void bnxt_free_irq(struct bnxt *bp)
11437 {
11438 	struct bnxt_irq *irq;
11439 	int i;
11440 
11441 #ifdef CONFIG_RFS_ACCEL
11442 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11443 	bp->dev->rx_cpu_rmap = NULL;
11444 #endif
11445 	if (!bp->irq_tbl || !bp->bnapi)
11446 		return;
11447 
11448 	for (i = 0; i < bp->cp_nr_rings; i++) {
11449 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11450 
11451 		irq = &bp->irq_tbl[map_idx];
11452 		if (irq->requested) {
11453 			if (irq->have_cpumask) {
11454 				irq_update_affinity_hint(irq->vector, NULL);
11455 				free_cpumask_var(irq->cpu_mask);
11456 				irq->have_cpumask = 0;
11457 			}
11458 
11459 			bnxt_release_irq_notifier(irq);
11460 
11461 			free_irq(irq->vector, bp->bnapi[i]);
11462 		}
11463 
11464 		irq->requested = 0;
11465 	}
11466 
11467 	/* Disable TPH support */
11468 	pcie_disable_tph(bp->pdev);
11469 	bp->tph_mode = 0;
11470 }
11471 
11472 static int bnxt_request_irq(struct bnxt *bp)
11473 {
11474 	int i, j, rc = 0;
11475 	unsigned long flags = 0;
11476 #ifdef CONFIG_RFS_ACCEL
11477 	struct cpu_rmap *rmap;
11478 #endif
11479 
11480 	rc = bnxt_setup_int_mode(bp);
11481 	if (rc) {
11482 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11483 			   rc);
11484 		return rc;
11485 	}
11486 #ifdef CONFIG_RFS_ACCEL
11487 	rmap = bp->dev->rx_cpu_rmap;
11488 #endif
11489 
11490 	/* Enable TPH support as part of IRQ request */
11491 	rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE);
11492 	if (!rc)
11493 		bp->tph_mode = PCI_TPH_ST_IV_MODE;
11494 
11495 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11496 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11497 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11498 
11499 #ifdef CONFIG_RFS_ACCEL
11500 		if (rmap && bp->bnapi[i]->rx_ring) {
11501 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11502 			if (rc)
11503 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11504 					    j);
11505 			j++;
11506 		}
11507 #endif
11508 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11509 				 bp->bnapi[i]);
11510 		if (rc)
11511 			break;
11512 
11513 		netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector);
11514 		irq->requested = 1;
11515 
11516 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
11517 			int numa_node = dev_to_node(&bp->pdev->dev);
11518 			u16 tag;
11519 
11520 			irq->have_cpumask = 1;
11521 			irq->msix_nr = map_idx;
11522 			irq->ring_nr = i;
11523 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
11524 					irq->cpu_mask);
11525 			rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
11526 			if (rc) {
11527 				netdev_warn(bp->dev,
11528 					    "Update affinity hint failed, IRQ = %d\n",
11529 					    irq->vector);
11530 				break;
11531 			}
11532 
11533 			bnxt_register_irq_notifier(bp, irq);
11534 
11535 			/* Init ST table entry */
11536 			if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11537 						cpumask_first(irq->cpu_mask),
11538 						&tag))
11539 				continue;
11540 
11541 			pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag);
11542 		}
11543 	}
11544 	return rc;
11545 }
11546 
11547 static void bnxt_del_napi(struct bnxt *bp)
11548 {
11549 	int i;
11550 
11551 	if (!bp->bnapi)
11552 		return;
11553 
11554 	for (i = 0; i < bp->rx_nr_rings; i++)
11555 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
11556 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
11557 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
11558 
11559 	for (i = 0; i < bp->cp_nr_rings; i++) {
11560 		struct bnxt_napi *bnapi = bp->bnapi[i];
11561 
11562 		__netif_napi_del_locked(&bnapi->napi);
11563 	}
11564 	/* We called __netif_napi_del_locked(), we need
11565 	 * to respect an RCU grace period before freeing napi structures.
11566 	 */
11567 	synchronize_net();
11568 }
11569 
11570 static void bnxt_init_napi(struct bnxt *bp)
11571 {
11572 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
11573 	unsigned int cp_nr_rings = bp->cp_nr_rings;
11574 	struct bnxt_napi *bnapi;
11575 	int i;
11576 
11577 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11578 		poll_fn = bnxt_poll_p5;
11579 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11580 		cp_nr_rings--;
11581 	for (i = 0; i < cp_nr_rings; i++) {
11582 		bnapi = bp->bnapi[i];
11583 		netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn,
11584 					     bnapi->index);
11585 	}
11586 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11587 		bnapi = bp->bnapi[cp_nr_rings];
11588 		netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
11589 	}
11590 }
11591 
11592 static void bnxt_disable_napi(struct bnxt *bp)
11593 {
11594 	int i;
11595 
11596 	if (!bp->bnapi ||
11597 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11598 		return;
11599 
11600 	for (i = 0; i < bp->cp_nr_rings; i++) {
11601 		struct bnxt_napi *bnapi = bp->bnapi[i];
11602 		struct bnxt_cp_ring_info *cpr;
11603 
11604 		cpr = &bnapi->cp_ring;
11605 		if (bnapi->tx_fault)
11606 			cpr->sw_stats->tx.tx_resets++;
11607 		if (bnapi->in_reset)
11608 			cpr->sw_stats->rx.rx_resets++;
11609 		napi_disable_locked(&bnapi->napi);
11610 	}
11611 }
11612 
11613 static void bnxt_enable_napi(struct bnxt *bp)
11614 {
11615 	int i;
11616 
11617 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11618 	for (i = 0; i < bp->cp_nr_rings; i++) {
11619 		struct bnxt_napi *bnapi = bp->bnapi[i];
11620 		struct bnxt_cp_ring_info *cpr;
11621 
11622 		bnapi->tx_fault = 0;
11623 
11624 		cpr = &bnapi->cp_ring;
11625 		bnapi->in_reset = false;
11626 
11627 		if (bnapi->rx_ring) {
11628 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
11629 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
11630 		}
11631 		napi_enable_locked(&bnapi->napi);
11632 	}
11633 }
11634 
11635 void bnxt_tx_disable(struct bnxt *bp)
11636 {
11637 	int i;
11638 	struct bnxt_tx_ring_info *txr;
11639 
11640 	if (bp->tx_ring) {
11641 		for (i = 0; i < bp->tx_nr_rings; i++) {
11642 			txr = &bp->tx_ring[i];
11643 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11644 		}
11645 	}
11646 	/* Make sure napi polls see @dev_state change */
11647 	synchronize_net();
11648 	/* Drop carrier first to prevent TX timeout */
11649 	netif_carrier_off(bp->dev);
11650 	/* Stop all TX queues */
11651 	netif_tx_disable(bp->dev);
11652 }
11653 
11654 void bnxt_tx_enable(struct bnxt *bp)
11655 {
11656 	int i;
11657 	struct bnxt_tx_ring_info *txr;
11658 
11659 	for (i = 0; i < bp->tx_nr_rings; i++) {
11660 		txr = &bp->tx_ring[i];
11661 		WRITE_ONCE(txr->dev_state, 0);
11662 	}
11663 	/* Make sure napi polls see @dev_state change */
11664 	synchronize_net();
11665 	netif_tx_wake_all_queues(bp->dev);
11666 	if (BNXT_LINK_IS_UP(bp))
11667 		netif_carrier_on(bp->dev);
11668 }
11669 
11670 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
11671 {
11672 	u8 active_fec = link_info->active_fec_sig_mode &
11673 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
11674 
11675 	switch (active_fec) {
11676 	default:
11677 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
11678 		return "None";
11679 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
11680 		return "Clause 74 BaseR";
11681 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
11682 		return "Clause 91 RS(528,514)";
11683 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
11684 		return "Clause 91 RS544_1XN";
11685 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
11686 		return "Clause 91 RS(544,514)";
11687 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
11688 		return "Clause 91 RS272_1XN";
11689 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
11690 		return "Clause 91 RS(272,257)";
11691 	}
11692 }
11693 
11694 void bnxt_report_link(struct bnxt *bp)
11695 {
11696 	if (BNXT_LINK_IS_UP(bp)) {
11697 		const char *signal = "";
11698 		const char *flow_ctrl;
11699 		const char *duplex;
11700 		u32 speed;
11701 		u16 fec;
11702 
11703 		netif_carrier_on(bp->dev);
11704 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
11705 		if (speed == SPEED_UNKNOWN) {
11706 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
11707 			return;
11708 		}
11709 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
11710 			duplex = "full";
11711 		else
11712 			duplex = "half";
11713 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
11714 			flow_ctrl = "ON - receive & transmit";
11715 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
11716 			flow_ctrl = "ON - transmit";
11717 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
11718 			flow_ctrl = "ON - receive";
11719 		else
11720 			flow_ctrl = "none";
11721 		if (bp->link_info.phy_qcfg_resp.option_flags &
11722 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
11723 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
11724 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
11725 			switch (sig_mode) {
11726 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
11727 				signal = "(NRZ) ";
11728 				break;
11729 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
11730 				signal = "(PAM4 56Gbps) ";
11731 				break;
11732 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
11733 				signal = "(PAM4 112Gbps) ";
11734 				break;
11735 			default:
11736 				break;
11737 			}
11738 		}
11739 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
11740 			    speed, signal, duplex, flow_ctrl);
11741 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
11742 			netdev_info(bp->dev, "EEE is %s\n",
11743 				    bp->eee.eee_active ? "active" :
11744 							 "not active");
11745 		fec = bp->link_info.fec_cfg;
11746 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
11747 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
11748 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
11749 				    bnxt_report_fec(&bp->link_info));
11750 	} else {
11751 		netif_carrier_off(bp->dev);
11752 		netdev_err(bp->dev, "NIC Link is Down\n");
11753 	}
11754 }
11755 
11756 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
11757 {
11758 	if (!resp->supported_speeds_auto_mode &&
11759 	    !resp->supported_speeds_force_mode &&
11760 	    !resp->supported_pam4_speeds_auto_mode &&
11761 	    !resp->supported_pam4_speeds_force_mode &&
11762 	    !resp->supported_speeds2_auto_mode &&
11763 	    !resp->supported_speeds2_force_mode)
11764 		return true;
11765 	return false;
11766 }
11767 
11768 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
11769 {
11770 	struct bnxt_link_info *link_info = &bp->link_info;
11771 	struct hwrm_port_phy_qcaps_output *resp;
11772 	struct hwrm_port_phy_qcaps_input *req;
11773 	int rc = 0;
11774 
11775 	if (bp->hwrm_spec_code < 0x10201)
11776 		return 0;
11777 
11778 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
11779 	if (rc)
11780 		return rc;
11781 
11782 	resp = hwrm_req_hold(bp, req);
11783 	rc = hwrm_req_send(bp, req);
11784 	if (rc)
11785 		goto hwrm_phy_qcaps_exit;
11786 
11787 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
11788 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
11789 		struct ethtool_keee *eee = &bp->eee;
11790 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
11791 
11792 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
11793 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
11794 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11795 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11796 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11797 	}
11798 
11799 	if (bp->hwrm_spec_code >= 0x10a01) {
11800 		if (bnxt_phy_qcaps_no_speed(resp)) {
11801 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11802 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11803 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11804 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11805 			netdev_info(bp->dev, "Ethernet link enabled\n");
11806 			/* Phy re-enabled, reprobe the speeds */
11807 			link_info->support_auto_speeds = 0;
11808 			link_info->support_pam4_auto_speeds = 0;
11809 			link_info->support_auto_speeds2 = 0;
11810 		}
11811 	}
11812 	if (resp->supported_speeds_auto_mode)
11813 		link_info->support_auto_speeds =
11814 			le16_to_cpu(resp->supported_speeds_auto_mode);
11815 	if (resp->supported_pam4_speeds_auto_mode)
11816 		link_info->support_pam4_auto_speeds =
11817 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11818 	if (resp->supported_speeds2_auto_mode)
11819 		link_info->support_auto_speeds2 =
11820 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11821 
11822 	bp->port_count = resp->port_cnt;
11823 
11824 hwrm_phy_qcaps_exit:
11825 	hwrm_req_drop(bp, req);
11826 	return rc;
11827 }
11828 
11829 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
11830 {
11831 	struct hwrm_port_mac_qcaps_output *resp;
11832 	struct hwrm_port_mac_qcaps_input *req;
11833 	int rc;
11834 
11835 	if (bp->hwrm_spec_code < 0x10a03)
11836 		return;
11837 
11838 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
11839 	if (rc)
11840 		return;
11841 
11842 	resp = hwrm_req_hold(bp, req);
11843 	rc = hwrm_req_send_silent(bp, req);
11844 	if (!rc)
11845 		bp->mac_flags = resp->flags;
11846 	hwrm_req_drop(bp, req);
11847 }
11848 
11849 static bool bnxt_support_dropped(u16 advertising, u16 supported)
11850 {
11851 	u16 diff = advertising ^ supported;
11852 
11853 	return ((supported | diff) != supported);
11854 }
11855 
11856 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
11857 {
11858 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
11859 
11860 	/* Check if any advertised speeds are no longer supported. The caller
11861 	 * holds the link_lock mutex, so we can modify link_info settings.
11862 	 */
11863 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11864 		if (bnxt_support_dropped(link_info->advertising,
11865 					 link_info->support_auto_speeds2)) {
11866 			link_info->advertising = link_info->support_auto_speeds2;
11867 			return true;
11868 		}
11869 		return false;
11870 	}
11871 	if (bnxt_support_dropped(link_info->advertising,
11872 				 link_info->support_auto_speeds)) {
11873 		link_info->advertising = link_info->support_auto_speeds;
11874 		return true;
11875 	}
11876 	if (bnxt_support_dropped(link_info->advertising_pam4,
11877 				 link_info->support_pam4_auto_speeds)) {
11878 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
11879 		return true;
11880 	}
11881 	return false;
11882 }
11883 
11884 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
11885 {
11886 	struct bnxt_link_info *link_info = &bp->link_info;
11887 	struct hwrm_port_phy_qcfg_output *resp;
11888 	struct hwrm_port_phy_qcfg_input *req;
11889 	u8 link_state = link_info->link_state;
11890 	bool support_changed;
11891 	int rc;
11892 
11893 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
11894 	if (rc)
11895 		return rc;
11896 
11897 	resp = hwrm_req_hold(bp, req);
11898 	rc = hwrm_req_send(bp, req);
11899 	if (rc) {
11900 		hwrm_req_drop(bp, req);
11901 		if (BNXT_VF(bp) && rc == -ENODEV) {
11902 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
11903 			rc = 0;
11904 		}
11905 		return rc;
11906 	}
11907 
11908 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
11909 	link_info->phy_link_status = resp->link;
11910 	link_info->duplex = resp->duplex_cfg;
11911 	if (bp->hwrm_spec_code >= 0x10800)
11912 		link_info->duplex = resp->duplex_state;
11913 	link_info->pause = resp->pause;
11914 	link_info->auto_mode = resp->auto_mode;
11915 	link_info->auto_pause_setting = resp->auto_pause;
11916 	link_info->lp_pause = resp->link_partner_adv_pause;
11917 	link_info->force_pause_setting = resp->force_pause;
11918 	link_info->duplex_setting = resp->duplex_cfg;
11919 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
11920 		link_info->link_speed = le16_to_cpu(resp->link_speed);
11921 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
11922 			link_info->active_lanes = resp->active_lanes;
11923 	} else {
11924 		link_info->link_speed = 0;
11925 		link_info->active_lanes = 0;
11926 	}
11927 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
11928 	link_info->force_pam4_link_speed =
11929 		le16_to_cpu(resp->force_pam4_link_speed);
11930 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
11931 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
11932 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
11933 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
11934 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
11935 	link_info->auto_pam4_link_speeds =
11936 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
11937 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
11938 	link_info->lp_auto_link_speeds =
11939 		le16_to_cpu(resp->link_partner_adv_speeds);
11940 	link_info->lp_auto_pam4_link_speeds =
11941 		resp->link_partner_pam4_adv_speeds;
11942 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
11943 	link_info->phy_ver[0] = resp->phy_maj;
11944 	link_info->phy_ver[1] = resp->phy_min;
11945 	link_info->phy_ver[2] = resp->phy_bld;
11946 	link_info->media_type = resp->media_type;
11947 	link_info->phy_type = resp->phy_type;
11948 	link_info->transceiver = resp->xcvr_pkg_type;
11949 	link_info->phy_addr = resp->eee_config_phy_addr &
11950 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
11951 	link_info->module_status = resp->module_status;
11952 
11953 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
11954 		struct ethtool_keee *eee = &bp->eee;
11955 		u16 fw_speeds;
11956 
11957 		eee->eee_active = 0;
11958 		if (resp->eee_config_phy_addr &
11959 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
11960 			eee->eee_active = 1;
11961 			fw_speeds = le16_to_cpu(
11962 				resp->link_partner_adv_eee_link_speed_mask);
11963 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
11964 		}
11965 
11966 		/* Pull initial EEE config */
11967 		if (!chng_link_state) {
11968 			if (resp->eee_config_phy_addr &
11969 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
11970 				eee->eee_enabled = 1;
11971 
11972 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
11973 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
11974 
11975 			if (resp->eee_config_phy_addr &
11976 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
11977 				__le32 tmr;
11978 
11979 				eee->tx_lpi_enabled = 1;
11980 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
11981 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
11982 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
11983 			}
11984 		}
11985 	}
11986 
11987 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
11988 	if (bp->hwrm_spec_code >= 0x10504) {
11989 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
11990 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
11991 	}
11992 	/* TODO: need to add more logic to report VF link */
11993 	if (chng_link_state) {
11994 		if (link_info->phy_link_status == BNXT_LINK_LINK)
11995 			link_info->link_state = BNXT_LINK_STATE_UP;
11996 		else
11997 			link_info->link_state = BNXT_LINK_STATE_DOWN;
11998 		if (link_state != link_info->link_state)
11999 			bnxt_report_link(bp);
12000 	} else {
12001 		/* always link down if not require to update link state */
12002 		link_info->link_state = BNXT_LINK_STATE_DOWN;
12003 	}
12004 	hwrm_req_drop(bp, req);
12005 
12006 	if (!BNXT_PHY_CFG_ABLE(bp))
12007 		return 0;
12008 
12009 	support_changed = bnxt_support_speed_dropped(link_info);
12010 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
12011 		bnxt_hwrm_set_link_setting(bp, true, false);
12012 	return 0;
12013 }
12014 
12015 static void bnxt_get_port_module_status(struct bnxt *bp)
12016 {
12017 	struct bnxt_link_info *link_info = &bp->link_info;
12018 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
12019 	u8 module_status;
12020 
12021 	if (bnxt_update_link(bp, true))
12022 		return;
12023 
12024 	module_status = link_info->module_status;
12025 	switch (module_status) {
12026 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
12027 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
12028 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
12029 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
12030 			    bp->pf.port_id);
12031 		if (bp->hwrm_spec_code >= 0x10201) {
12032 			netdev_warn(bp->dev, "Module part number %s\n",
12033 				    resp->phy_vendor_partnumber);
12034 		}
12035 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
12036 			netdev_warn(bp->dev, "TX is disabled\n");
12037 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
12038 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
12039 	}
12040 }
12041 
12042 static void
12043 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12044 {
12045 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
12046 		if (bp->hwrm_spec_code >= 0x10201)
12047 			req->auto_pause =
12048 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
12049 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12050 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
12051 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12052 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
12053 		req->enables |=
12054 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12055 	} else {
12056 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12057 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
12058 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12059 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
12060 		req->enables |=
12061 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
12062 		if (bp->hwrm_spec_code >= 0x10201) {
12063 			req->auto_pause = req->force_pause;
12064 			req->enables |= cpu_to_le32(
12065 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12066 		}
12067 	}
12068 }
12069 
12070 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12071 {
12072 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
12073 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
12074 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12075 			req->enables |=
12076 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
12077 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
12078 		} else if (bp->link_info.advertising) {
12079 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
12080 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
12081 		}
12082 		if (bp->link_info.advertising_pam4) {
12083 			req->enables |=
12084 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
12085 			req->auto_link_pam4_speed_mask =
12086 				cpu_to_le16(bp->link_info.advertising_pam4);
12087 		}
12088 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
12089 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
12090 	} else {
12091 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
12092 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12093 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
12094 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
12095 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
12096 				   (u32)bp->link_info.req_link_speed);
12097 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
12098 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12099 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
12100 		} else {
12101 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12102 		}
12103 	}
12104 
12105 	/* tell chimp that the setting takes effect immediately */
12106 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
12107 }
12108 
12109 int bnxt_hwrm_set_pause(struct bnxt *bp)
12110 {
12111 	struct hwrm_port_phy_cfg_input *req;
12112 	int rc;
12113 
12114 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12115 	if (rc)
12116 		return rc;
12117 
12118 	bnxt_hwrm_set_pause_common(bp, req);
12119 
12120 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
12121 	    bp->link_info.force_link_chng)
12122 		bnxt_hwrm_set_link_common(bp, req);
12123 
12124 	rc = hwrm_req_send(bp, req);
12125 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
12126 		/* since changing of pause setting doesn't trigger any link
12127 		 * change event, the driver needs to update the current pause
12128 		 * result upon successfully return of the phy_cfg command
12129 		 */
12130 		bp->link_info.pause =
12131 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
12132 		bp->link_info.auto_pause_setting = 0;
12133 		if (!bp->link_info.force_link_chng)
12134 			bnxt_report_link(bp);
12135 	}
12136 	bp->link_info.force_link_chng = false;
12137 	return rc;
12138 }
12139 
12140 static void bnxt_hwrm_set_eee(struct bnxt *bp,
12141 			      struct hwrm_port_phy_cfg_input *req)
12142 {
12143 	struct ethtool_keee *eee = &bp->eee;
12144 
12145 	if (eee->eee_enabled) {
12146 		u16 eee_speeds;
12147 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
12148 
12149 		if (eee->tx_lpi_enabled)
12150 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
12151 		else
12152 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
12153 
12154 		req->flags |= cpu_to_le32(flags);
12155 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
12156 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
12157 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
12158 	} else {
12159 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
12160 	}
12161 }
12162 
12163 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
12164 {
12165 	struct hwrm_port_phy_cfg_input *req;
12166 	int rc;
12167 
12168 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12169 	if (rc)
12170 		return rc;
12171 
12172 	if (set_pause)
12173 		bnxt_hwrm_set_pause_common(bp, req);
12174 
12175 	bnxt_hwrm_set_link_common(bp, req);
12176 
12177 	if (set_eee)
12178 		bnxt_hwrm_set_eee(bp, req);
12179 	return hwrm_req_send(bp, req);
12180 }
12181 
12182 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
12183 {
12184 	struct hwrm_port_phy_cfg_input *req;
12185 	int rc;
12186 
12187 	if (!BNXT_SINGLE_PF(bp))
12188 		return 0;
12189 
12190 	if (pci_num_vf(bp->pdev) &&
12191 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
12192 		return 0;
12193 
12194 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12195 	if (rc)
12196 		return rc;
12197 
12198 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
12199 	rc = hwrm_req_send(bp, req);
12200 	if (!rc) {
12201 		mutex_lock(&bp->link_lock);
12202 		/* Device is not obliged link down in certain scenarios, even
12203 		 * when forced. Setting the state unknown is consistent with
12204 		 * driver startup and will force link state to be reported
12205 		 * during subsequent open based on PORT_PHY_QCFG.
12206 		 */
12207 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
12208 		mutex_unlock(&bp->link_lock);
12209 	}
12210 	return rc;
12211 }
12212 
12213 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
12214 {
12215 #ifdef CONFIG_TEE_BNXT_FW
12216 	int rc = tee_bnxt_fw_load();
12217 
12218 	if (rc)
12219 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
12220 
12221 	return rc;
12222 #else
12223 	netdev_err(bp->dev, "OP-TEE not supported\n");
12224 	return -ENODEV;
12225 #endif
12226 }
12227 
12228 static int bnxt_try_recover_fw(struct bnxt *bp)
12229 {
12230 	if (bp->fw_health && bp->fw_health->status_reliable) {
12231 		int retry = 0, rc;
12232 		u32 sts;
12233 
12234 		do {
12235 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12236 			rc = bnxt_hwrm_poll(bp);
12237 			if (!BNXT_FW_IS_BOOTING(sts) &&
12238 			    !BNXT_FW_IS_RECOVERING(sts))
12239 				break;
12240 			retry++;
12241 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
12242 
12243 		if (!BNXT_FW_IS_HEALTHY(sts)) {
12244 			netdev_err(bp->dev,
12245 				   "Firmware not responding, status: 0x%x\n",
12246 				   sts);
12247 			rc = -ENODEV;
12248 		}
12249 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
12250 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
12251 			return bnxt_fw_reset_via_optee(bp);
12252 		}
12253 		return rc;
12254 	}
12255 
12256 	return -ENODEV;
12257 }
12258 
12259 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
12260 {
12261 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12262 
12263 	if (!BNXT_NEW_RM(bp))
12264 		return; /* no resource reservations required */
12265 
12266 	hw_resc->resv_cp_rings = 0;
12267 	hw_resc->resv_stat_ctxs = 0;
12268 	hw_resc->resv_irqs = 0;
12269 	hw_resc->resv_tx_rings = 0;
12270 	hw_resc->resv_rx_rings = 0;
12271 	hw_resc->resv_hw_ring_grps = 0;
12272 	hw_resc->resv_vnics = 0;
12273 	hw_resc->resv_rsscos_ctxs = 0;
12274 	if (!fw_reset) {
12275 		bp->tx_nr_rings = 0;
12276 		bp->rx_nr_rings = 0;
12277 	}
12278 }
12279 
12280 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12281 {
12282 	int rc;
12283 
12284 	if (!BNXT_NEW_RM(bp))
12285 		return 0; /* no resource reservations required */
12286 
12287 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12288 	if (rc)
12289 		netdev_err(bp->dev, "resc_qcaps failed\n");
12290 
12291 	bnxt_clear_reservations(bp, fw_reset);
12292 
12293 	return rc;
12294 }
12295 
12296 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12297 {
12298 	struct hwrm_func_drv_if_change_output *resp;
12299 	struct hwrm_func_drv_if_change_input *req;
12300 	bool fw_reset = !bp->irq_tbl;
12301 	bool resc_reinit = false;
12302 	int rc, retry = 0;
12303 	u32 flags = 0;
12304 
12305 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12306 		return 0;
12307 
12308 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12309 	if (rc)
12310 		return rc;
12311 
12312 	if (up)
12313 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12314 	resp = hwrm_req_hold(bp, req);
12315 
12316 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12317 	while (retry < BNXT_FW_IF_RETRY) {
12318 		rc = hwrm_req_send(bp, req);
12319 		if (rc != -EAGAIN)
12320 			break;
12321 
12322 		msleep(50);
12323 		retry++;
12324 	}
12325 
12326 	if (rc == -EAGAIN) {
12327 		hwrm_req_drop(bp, req);
12328 		return rc;
12329 	} else if (!rc) {
12330 		flags = le32_to_cpu(resp->flags);
12331 	} else if (up) {
12332 		rc = bnxt_try_recover_fw(bp);
12333 		fw_reset = true;
12334 	}
12335 	hwrm_req_drop(bp, req);
12336 	if (rc)
12337 		return rc;
12338 
12339 	if (!up) {
12340 		bnxt_inv_fw_health_reg(bp);
12341 		return 0;
12342 	}
12343 
12344 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12345 		resc_reinit = true;
12346 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12347 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12348 		fw_reset = true;
12349 	else
12350 		bnxt_remap_fw_health_regs(bp);
12351 
12352 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12353 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12354 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12355 		return -ENODEV;
12356 	}
12357 	if (resc_reinit || fw_reset) {
12358 		if (fw_reset) {
12359 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12360 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12361 				bnxt_ulp_irq_stop(bp);
12362 			bnxt_free_ctx_mem(bp, false);
12363 			bnxt_dcb_free(bp);
12364 			rc = bnxt_fw_init_one(bp);
12365 			if (rc) {
12366 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12367 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12368 				return rc;
12369 			}
12370 			bnxt_clear_int_mode(bp);
12371 			rc = bnxt_init_int_mode(bp);
12372 			if (rc) {
12373 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12374 				netdev_err(bp->dev, "init int mode failed\n");
12375 				return rc;
12376 			}
12377 		}
12378 		rc = bnxt_cancel_reservations(bp, fw_reset);
12379 	}
12380 	return rc;
12381 }
12382 
12383 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12384 {
12385 	struct hwrm_port_led_qcaps_output *resp;
12386 	struct hwrm_port_led_qcaps_input *req;
12387 	struct bnxt_pf_info *pf = &bp->pf;
12388 	int rc;
12389 
12390 	bp->num_leds = 0;
12391 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12392 		return 0;
12393 
12394 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12395 	if (rc)
12396 		return rc;
12397 
12398 	req->port_id = cpu_to_le16(pf->port_id);
12399 	resp = hwrm_req_hold(bp, req);
12400 	rc = hwrm_req_send(bp, req);
12401 	if (rc) {
12402 		hwrm_req_drop(bp, req);
12403 		return rc;
12404 	}
12405 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12406 		int i;
12407 
12408 		bp->num_leds = resp->num_leds;
12409 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12410 						 bp->num_leds);
12411 		for (i = 0; i < bp->num_leds; i++) {
12412 			struct bnxt_led_info *led = &bp->leds[i];
12413 			__le16 caps = led->led_state_caps;
12414 
12415 			if (!led->led_group_id ||
12416 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12417 				bp->num_leds = 0;
12418 				break;
12419 			}
12420 		}
12421 	}
12422 	hwrm_req_drop(bp, req);
12423 	return 0;
12424 }
12425 
12426 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12427 {
12428 	struct hwrm_wol_filter_alloc_output *resp;
12429 	struct hwrm_wol_filter_alloc_input *req;
12430 	int rc;
12431 
12432 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12433 	if (rc)
12434 		return rc;
12435 
12436 	req->port_id = cpu_to_le16(bp->pf.port_id);
12437 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12438 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12439 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12440 
12441 	resp = hwrm_req_hold(bp, req);
12442 	rc = hwrm_req_send(bp, req);
12443 	if (!rc)
12444 		bp->wol_filter_id = resp->wol_filter_id;
12445 	hwrm_req_drop(bp, req);
12446 	return rc;
12447 }
12448 
12449 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12450 {
12451 	struct hwrm_wol_filter_free_input *req;
12452 	int rc;
12453 
12454 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12455 	if (rc)
12456 		return rc;
12457 
12458 	req->port_id = cpu_to_le16(bp->pf.port_id);
12459 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12460 	req->wol_filter_id = bp->wol_filter_id;
12461 
12462 	return hwrm_req_send(bp, req);
12463 }
12464 
12465 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12466 {
12467 	struct hwrm_wol_filter_qcfg_output *resp;
12468 	struct hwrm_wol_filter_qcfg_input *req;
12469 	u16 next_handle = 0;
12470 	int rc;
12471 
12472 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12473 	if (rc)
12474 		return rc;
12475 
12476 	req->port_id = cpu_to_le16(bp->pf.port_id);
12477 	req->handle = cpu_to_le16(handle);
12478 	resp = hwrm_req_hold(bp, req);
12479 	rc = hwrm_req_send(bp, req);
12480 	if (!rc) {
12481 		next_handle = le16_to_cpu(resp->next_handle);
12482 		if (next_handle != 0) {
12483 			if (resp->wol_type ==
12484 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12485 				bp->wol = 1;
12486 				bp->wol_filter_id = resp->wol_filter_id;
12487 			}
12488 		}
12489 	}
12490 	hwrm_req_drop(bp, req);
12491 	return next_handle;
12492 }
12493 
12494 static void bnxt_get_wol_settings(struct bnxt *bp)
12495 {
12496 	u16 handle = 0;
12497 
12498 	bp->wol = 0;
12499 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
12500 		return;
12501 
12502 	do {
12503 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
12504 	} while (handle && handle != 0xffff);
12505 }
12506 
12507 static bool bnxt_eee_config_ok(struct bnxt *bp)
12508 {
12509 	struct ethtool_keee *eee = &bp->eee;
12510 	struct bnxt_link_info *link_info = &bp->link_info;
12511 
12512 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
12513 		return true;
12514 
12515 	if (eee->eee_enabled) {
12516 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
12517 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
12518 
12519 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
12520 
12521 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12522 			eee->eee_enabled = 0;
12523 			return false;
12524 		}
12525 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
12526 			linkmode_and(eee->advertised, advertising,
12527 				     eee->supported);
12528 			return false;
12529 		}
12530 	}
12531 	return true;
12532 }
12533 
12534 static int bnxt_update_phy_setting(struct bnxt *bp)
12535 {
12536 	int rc;
12537 	bool update_link = false;
12538 	bool update_pause = false;
12539 	bool update_eee = false;
12540 	struct bnxt_link_info *link_info = &bp->link_info;
12541 
12542 	rc = bnxt_update_link(bp, true);
12543 	if (rc) {
12544 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
12545 			   rc);
12546 		return rc;
12547 	}
12548 	if (!BNXT_SINGLE_PF(bp))
12549 		return 0;
12550 
12551 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12552 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
12553 	    link_info->req_flow_ctrl)
12554 		update_pause = true;
12555 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12556 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
12557 		update_pause = true;
12558 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12559 		if (BNXT_AUTO_MODE(link_info->auto_mode))
12560 			update_link = true;
12561 		if (bnxt_force_speed_updated(link_info))
12562 			update_link = true;
12563 		if (link_info->req_duplex != link_info->duplex_setting)
12564 			update_link = true;
12565 	} else {
12566 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
12567 			update_link = true;
12568 		if (bnxt_auto_speed_updated(link_info))
12569 			update_link = true;
12570 	}
12571 
12572 	/* The last close may have shutdown the link, so need to call
12573 	 * PHY_CFG to bring it back up.
12574 	 */
12575 	if (!BNXT_LINK_IS_UP(bp))
12576 		update_link = true;
12577 
12578 	if (!bnxt_eee_config_ok(bp))
12579 		update_eee = true;
12580 
12581 	if (update_link)
12582 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
12583 	else if (update_pause)
12584 		rc = bnxt_hwrm_set_pause(bp);
12585 	if (rc) {
12586 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
12587 			   rc);
12588 		return rc;
12589 	}
12590 
12591 	return rc;
12592 }
12593 
12594 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
12595 
12596 static int bnxt_reinit_after_abort(struct bnxt *bp)
12597 {
12598 	int rc;
12599 
12600 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12601 		return -EBUSY;
12602 
12603 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
12604 		return -ENODEV;
12605 
12606 	rc = bnxt_fw_init_one(bp);
12607 	if (!rc) {
12608 		bnxt_clear_int_mode(bp);
12609 		rc = bnxt_init_int_mode(bp);
12610 		if (!rc) {
12611 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12612 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12613 		}
12614 	}
12615 	return rc;
12616 }
12617 
12618 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
12619 {
12620 	struct bnxt_ntuple_filter *ntp_fltr;
12621 	struct bnxt_l2_filter *l2_fltr;
12622 
12623 	if (list_empty(&fltr->list))
12624 		return;
12625 
12626 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
12627 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
12628 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
12629 		atomic_inc(&l2_fltr->refcnt);
12630 		ntp_fltr->l2_fltr = l2_fltr;
12631 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
12632 			bnxt_del_ntp_filter(bp, ntp_fltr);
12633 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
12634 				   fltr->sw_id);
12635 		}
12636 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
12637 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
12638 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
12639 			bnxt_del_l2_filter(bp, l2_fltr);
12640 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
12641 				   fltr->sw_id);
12642 		}
12643 	}
12644 }
12645 
12646 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
12647 {
12648 	struct bnxt_filter_base *usr_fltr, *tmp;
12649 
12650 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
12651 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
12652 }
12653 
12654 static int bnxt_set_xps_mapping(struct bnxt *bp)
12655 {
12656 	int numa_node = dev_to_node(&bp->pdev->dev);
12657 	unsigned int q_idx, map_idx, cpu, i;
12658 	const struct cpumask *cpu_mask_ptr;
12659 	int nr_cpus = num_online_cpus();
12660 	cpumask_t *q_map;
12661 	int rc = 0;
12662 
12663 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
12664 	if (!q_map)
12665 		return -ENOMEM;
12666 
12667 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
12668 	 * Each TC has the same number of TX queues. The nth TX queue for each
12669 	 * TC will have the same CPU mask.
12670 	 */
12671 	for (i = 0; i < nr_cpus; i++) {
12672 		map_idx = i % bp->tx_nr_rings_per_tc;
12673 		cpu = cpumask_local_spread(i, numa_node);
12674 		cpu_mask_ptr = get_cpu_mask(cpu);
12675 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
12676 	}
12677 
12678 	/* Register CPU mask for each TX queue except the ones marked for XDP */
12679 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
12680 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
12681 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
12682 		if (rc) {
12683 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
12684 				    q_idx);
12685 			break;
12686 		}
12687 	}
12688 
12689 	kfree(q_map);
12690 
12691 	return rc;
12692 }
12693 
12694 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12695 {
12696 	int rc = 0;
12697 
12698 	netif_carrier_off(bp->dev);
12699 	if (irq_re_init) {
12700 		/* Reserve rings now if none were reserved at driver probe. */
12701 		rc = bnxt_init_dflt_ring_mode(bp);
12702 		if (rc) {
12703 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
12704 			return rc;
12705 		}
12706 	}
12707 	rc = bnxt_reserve_rings(bp, irq_re_init);
12708 	if (rc)
12709 		return rc;
12710 
12711 	rc = bnxt_alloc_mem(bp, irq_re_init);
12712 	if (rc) {
12713 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12714 		goto open_err_free_mem;
12715 	}
12716 
12717 	if (irq_re_init) {
12718 		bnxt_init_napi(bp);
12719 		rc = bnxt_request_irq(bp);
12720 		if (rc) {
12721 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
12722 			goto open_err_irq;
12723 		}
12724 	}
12725 
12726 	rc = bnxt_init_nic(bp, irq_re_init);
12727 	if (rc) {
12728 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12729 		goto open_err_irq;
12730 	}
12731 
12732 	bnxt_enable_napi(bp);
12733 	bnxt_debug_dev_init(bp);
12734 
12735 	if (link_re_init) {
12736 		mutex_lock(&bp->link_lock);
12737 		rc = bnxt_update_phy_setting(bp);
12738 		mutex_unlock(&bp->link_lock);
12739 		if (rc) {
12740 			netdev_warn(bp->dev, "failed to update phy settings\n");
12741 			if (BNXT_SINGLE_PF(bp)) {
12742 				bp->link_info.phy_retry = true;
12743 				bp->link_info.phy_retry_expires =
12744 					jiffies + 5 * HZ;
12745 			}
12746 		}
12747 	}
12748 
12749 	if (irq_re_init) {
12750 		udp_tunnel_nic_reset_ntf(bp->dev);
12751 		rc = bnxt_set_xps_mapping(bp);
12752 		if (rc)
12753 			netdev_warn(bp->dev, "failed to set xps mapping\n");
12754 	}
12755 
12756 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
12757 		if (!static_key_enabled(&bnxt_xdp_locking_key))
12758 			static_branch_enable(&bnxt_xdp_locking_key);
12759 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
12760 		static_branch_disable(&bnxt_xdp_locking_key);
12761 	}
12762 	set_bit(BNXT_STATE_OPEN, &bp->state);
12763 	bnxt_enable_int(bp);
12764 	/* Enable TX queues */
12765 	bnxt_tx_enable(bp);
12766 	mod_timer(&bp->timer, jiffies + bp->current_interval);
12767 	/* Poll link status and check for SFP+ module status */
12768 	mutex_lock(&bp->link_lock);
12769 	bnxt_get_port_module_status(bp);
12770 	mutex_unlock(&bp->link_lock);
12771 
12772 	/* VF-reps may need to be re-opened after the PF is re-opened */
12773 	if (BNXT_PF(bp))
12774 		bnxt_vf_reps_open(bp);
12775 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
12776 		WRITE_ONCE(bp->ptp_cfg->tx_avail, BNXT_MAX_TX_TS);
12777 	bnxt_ptp_init_rtc(bp, true);
12778 	bnxt_ptp_cfg_tstamp_filters(bp);
12779 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12780 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
12781 	bnxt_cfg_usr_fltrs(bp);
12782 	return 0;
12783 
12784 open_err_irq:
12785 	bnxt_del_napi(bp);
12786 
12787 open_err_free_mem:
12788 	bnxt_free_skbs(bp);
12789 	bnxt_free_irq(bp);
12790 	bnxt_free_mem(bp, true);
12791 	return rc;
12792 }
12793 
12794 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12795 {
12796 	int rc = 0;
12797 
12798 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12799 		rc = -EIO;
12800 	if (!rc)
12801 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12802 	if (rc) {
12803 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12804 		dev_close(bp->dev);
12805 	}
12806 	return rc;
12807 }
12808 
12809 /* netdev instance lock held, open the NIC half way by allocating all
12810  * resources, but NAPI, IRQ, and TX are not enabled.  This is mainly used
12811  * for offline self tests.
12812  */
12813 int bnxt_half_open_nic(struct bnxt *bp)
12814 {
12815 	int rc = 0;
12816 
12817 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12818 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
12819 		rc = -ENODEV;
12820 		goto half_open_err;
12821 	}
12822 
12823 	rc = bnxt_alloc_mem(bp, true);
12824 	if (rc) {
12825 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12826 		goto half_open_err;
12827 	}
12828 	bnxt_init_napi(bp);
12829 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12830 	rc = bnxt_init_nic(bp, true);
12831 	if (rc) {
12832 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12833 		bnxt_del_napi(bp);
12834 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12835 		goto half_open_err;
12836 	}
12837 	return 0;
12838 
12839 half_open_err:
12840 	bnxt_free_skbs(bp);
12841 	bnxt_free_mem(bp, true);
12842 	dev_close(bp->dev);
12843 	return rc;
12844 }
12845 
12846 /* netdev instance lock held, this call can only be made after a previous
12847  * successful call to bnxt_half_open_nic().
12848  */
12849 void bnxt_half_close_nic(struct bnxt *bp)
12850 {
12851 	bnxt_hwrm_resource_free(bp, false, true);
12852 	bnxt_del_napi(bp);
12853 	bnxt_free_skbs(bp);
12854 	bnxt_free_mem(bp, true);
12855 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12856 }
12857 
12858 void bnxt_reenable_sriov(struct bnxt *bp)
12859 {
12860 	if (BNXT_PF(bp)) {
12861 		struct bnxt_pf_info *pf = &bp->pf;
12862 		int n = pf->active_vfs;
12863 
12864 		if (n)
12865 			bnxt_cfg_hw_sriov(bp, &n, true);
12866 	}
12867 }
12868 
12869 static int bnxt_open(struct net_device *dev)
12870 {
12871 	struct bnxt *bp = netdev_priv(dev);
12872 	int rc;
12873 
12874 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12875 		rc = bnxt_reinit_after_abort(bp);
12876 		if (rc) {
12877 			if (rc == -EBUSY)
12878 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
12879 			else
12880 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
12881 			return -ENODEV;
12882 		}
12883 	}
12884 
12885 	rc = bnxt_hwrm_if_change(bp, true);
12886 	if (rc)
12887 		return rc;
12888 
12889 	rc = __bnxt_open_nic(bp, true, true);
12890 	if (rc) {
12891 		bnxt_hwrm_if_change(bp, false);
12892 	} else {
12893 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
12894 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12895 				bnxt_queue_sp_work(bp,
12896 						   BNXT_RESTART_ULP_SP_EVENT);
12897 		}
12898 	}
12899 
12900 	return rc;
12901 }
12902 
12903 static bool bnxt_drv_busy(struct bnxt *bp)
12904 {
12905 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
12906 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
12907 }
12908 
12909 static void bnxt_get_ring_stats(struct bnxt *bp,
12910 				struct rtnl_link_stats64 *stats);
12911 
12912 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
12913 			     bool link_re_init)
12914 {
12915 	/* Close the VF-reps before closing PF */
12916 	if (BNXT_PF(bp))
12917 		bnxt_vf_reps_close(bp);
12918 
12919 	/* Change device state to avoid TX queue wake up's */
12920 	bnxt_tx_disable(bp);
12921 
12922 	clear_bit(BNXT_STATE_OPEN, &bp->state);
12923 	smp_mb__after_atomic();
12924 	while (bnxt_drv_busy(bp))
12925 		msleep(20);
12926 
12927 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12928 		bnxt_clear_rss_ctxs(bp);
12929 	/* Flush rings and disable interrupts */
12930 	bnxt_shutdown_nic(bp, irq_re_init);
12931 
12932 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
12933 
12934 	bnxt_debug_dev_exit(bp);
12935 	bnxt_disable_napi(bp);
12936 	del_timer_sync(&bp->timer);
12937 	bnxt_free_skbs(bp);
12938 
12939 	/* Save ring stats before shutdown */
12940 	if (bp->bnapi && irq_re_init) {
12941 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
12942 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
12943 	}
12944 	if (irq_re_init) {
12945 		bnxt_free_irq(bp);
12946 		bnxt_del_napi(bp);
12947 	}
12948 	bnxt_free_mem(bp, irq_re_init);
12949 }
12950 
12951 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12952 {
12953 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12954 		/* If we get here, it means firmware reset is in progress
12955 		 * while we are trying to close.  We can safely proceed with
12956 		 * the close because we are holding netdev instance lock.
12957 		 * Some firmware messages may fail as we proceed to close.
12958 		 * We set the ABORT_ERR flag here so that the FW reset thread
12959 		 * will later abort when it gets the netdev instance lock
12960 		 * and sees the flag.
12961 		 */
12962 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
12963 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12964 	}
12965 
12966 #ifdef CONFIG_BNXT_SRIOV
12967 	if (bp->sriov_cfg) {
12968 		int rc;
12969 
12970 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
12971 						      !bp->sriov_cfg,
12972 						      BNXT_SRIOV_CFG_WAIT_TMO);
12973 		if (!rc)
12974 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
12975 		else if (rc < 0)
12976 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
12977 	}
12978 #endif
12979 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
12980 }
12981 
12982 static int bnxt_close(struct net_device *dev)
12983 {
12984 	struct bnxt *bp = netdev_priv(dev);
12985 
12986 	bnxt_close_nic(bp, true, true);
12987 	bnxt_hwrm_shutdown_link(bp);
12988 	bnxt_hwrm_if_change(bp, false);
12989 	return 0;
12990 }
12991 
12992 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
12993 				   u16 *val)
12994 {
12995 	struct hwrm_port_phy_mdio_read_output *resp;
12996 	struct hwrm_port_phy_mdio_read_input *req;
12997 	int rc;
12998 
12999 	if (bp->hwrm_spec_code < 0x10a00)
13000 		return -EOPNOTSUPP;
13001 
13002 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
13003 	if (rc)
13004 		return rc;
13005 
13006 	req->port_id = cpu_to_le16(bp->pf.port_id);
13007 	req->phy_addr = phy_addr;
13008 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13009 	if (mdio_phy_id_is_c45(phy_addr)) {
13010 		req->cl45_mdio = 1;
13011 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13012 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13013 		req->reg_addr = cpu_to_le16(reg);
13014 	}
13015 
13016 	resp = hwrm_req_hold(bp, req);
13017 	rc = hwrm_req_send(bp, req);
13018 	if (!rc)
13019 		*val = le16_to_cpu(resp->reg_data);
13020 	hwrm_req_drop(bp, req);
13021 	return rc;
13022 }
13023 
13024 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
13025 				    u16 val)
13026 {
13027 	struct hwrm_port_phy_mdio_write_input *req;
13028 	int rc;
13029 
13030 	if (bp->hwrm_spec_code < 0x10a00)
13031 		return -EOPNOTSUPP;
13032 
13033 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
13034 	if (rc)
13035 		return rc;
13036 
13037 	req->port_id = cpu_to_le16(bp->pf.port_id);
13038 	req->phy_addr = phy_addr;
13039 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13040 	if (mdio_phy_id_is_c45(phy_addr)) {
13041 		req->cl45_mdio = 1;
13042 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13043 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13044 		req->reg_addr = cpu_to_le16(reg);
13045 	}
13046 	req->reg_data = cpu_to_le16(val);
13047 
13048 	return hwrm_req_send(bp, req);
13049 }
13050 
13051 /* netdev instance lock held */
13052 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13053 {
13054 	struct mii_ioctl_data *mdio = if_mii(ifr);
13055 	struct bnxt *bp = netdev_priv(dev);
13056 	int rc;
13057 
13058 	switch (cmd) {
13059 	case SIOCGMIIPHY:
13060 		mdio->phy_id = bp->link_info.phy_addr;
13061 
13062 		fallthrough;
13063 	case SIOCGMIIREG: {
13064 		u16 mii_regval = 0;
13065 
13066 		if (!netif_running(dev))
13067 			return -EAGAIN;
13068 
13069 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
13070 					     &mii_regval);
13071 		mdio->val_out = mii_regval;
13072 		return rc;
13073 	}
13074 
13075 	case SIOCSMIIREG:
13076 		if (!netif_running(dev))
13077 			return -EAGAIN;
13078 
13079 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
13080 						mdio->val_in);
13081 
13082 	case SIOCSHWTSTAMP:
13083 		return bnxt_hwtstamp_set(dev, ifr);
13084 
13085 	case SIOCGHWTSTAMP:
13086 		return bnxt_hwtstamp_get(dev, ifr);
13087 
13088 	default:
13089 		/* do nothing */
13090 		break;
13091 	}
13092 	return -EOPNOTSUPP;
13093 }
13094 
13095 static void bnxt_get_ring_stats(struct bnxt *bp,
13096 				struct rtnl_link_stats64 *stats)
13097 {
13098 	int i;
13099 
13100 	for (i = 0; i < bp->cp_nr_rings; i++) {
13101 		struct bnxt_napi *bnapi = bp->bnapi[i];
13102 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13103 		u64 *sw = cpr->stats.sw_stats;
13104 
13105 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
13106 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13107 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
13108 
13109 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
13110 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
13111 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
13112 
13113 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
13114 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
13115 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
13116 
13117 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
13118 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
13119 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
13120 
13121 		stats->rx_missed_errors +=
13122 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
13123 
13124 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13125 
13126 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
13127 
13128 		stats->rx_dropped +=
13129 			cpr->sw_stats->rx.rx_netpoll_discards +
13130 			cpr->sw_stats->rx.rx_oom_discards;
13131 	}
13132 }
13133 
13134 static void bnxt_add_prev_stats(struct bnxt *bp,
13135 				struct rtnl_link_stats64 *stats)
13136 {
13137 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
13138 
13139 	stats->rx_packets += prev_stats->rx_packets;
13140 	stats->tx_packets += prev_stats->tx_packets;
13141 	stats->rx_bytes += prev_stats->rx_bytes;
13142 	stats->tx_bytes += prev_stats->tx_bytes;
13143 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
13144 	stats->multicast += prev_stats->multicast;
13145 	stats->rx_dropped += prev_stats->rx_dropped;
13146 	stats->tx_dropped += prev_stats->tx_dropped;
13147 }
13148 
13149 static void
13150 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
13151 {
13152 	struct bnxt *bp = netdev_priv(dev);
13153 
13154 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
13155 	/* Make sure bnxt_close_nic() sees that we are reading stats before
13156 	 * we check the BNXT_STATE_OPEN flag.
13157 	 */
13158 	smp_mb__after_atomic();
13159 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13160 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13161 		*stats = bp->net_stats_prev;
13162 		return;
13163 	}
13164 
13165 	bnxt_get_ring_stats(bp, stats);
13166 	bnxt_add_prev_stats(bp, stats);
13167 
13168 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
13169 		u64 *rx = bp->port_stats.sw_stats;
13170 		u64 *tx = bp->port_stats.sw_stats +
13171 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
13172 
13173 		stats->rx_crc_errors =
13174 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
13175 		stats->rx_frame_errors =
13176 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
13177 		stats->rx_length_errors =
13178 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
13179 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
13180 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
13181 		stats->rx_errors =
13182 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
13183 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
13184 		stats->collisions =
13185 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
13186 		stats->tx_fifo_errors =
13187 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
13188 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
13189 	}
13190 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13191 }
13192 
13193 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
13194 					struct bnxt_total_ring_err_stats *stats,
13195 					struct bnxt_cp_ring_info *cpr)
13196 {
13197 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
13198 	u64 *hw_stats = cpr->stats.sw_stats;
13199 
13200 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
13201 	stats->rx_total_resets += sw_stats->rx.rx_resets;
13202 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
13203 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
13204 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
13205 	stats->rx_total_ring_discards +=
13206 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
13207 	stats->tx_total_resets += sw_stats->tx.tx_resets;
13208 	stats->tx_total_ring_discards +=
13209 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
13210 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
13211 }
13212 
13213 void bnxt_get_ring_err_stats(struct bnxt *bp,
13214 			     struct bnxt_total_ring_err_stats *stats)
13215 {
13216 	int i;
13217 
13218 	for (i = 0; i < bp->cp_nr_rings; i++)
13219 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
13220 }
13221 
13222 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
13223 {
13224 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13225 	struct net_device *dev = bp->dev;
13226 	struct netdev_hw_addr *ha;
13227 	u8 *haddr;
13228 	int mc_count = 0;
13229 	bool update = false;
13230 	int off = 0;
13231 
13232 	netdev_for_each_mc_addr(ha, dev) {
13233 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
13234 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13235 			vnic->mc_list_count = 0;
13236 			return false;
13237 		}
13238 		haddr = ha->addr;
13239 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
13240 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
13241 			update = true;
13242 		}
13243 		off += ETH_ALEN;
13244 		mc_count++;
13245 	}
13246 	if (mc_count)
13247 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13248 
13249 	if (mc_count != vnic->mc_list_count) {
13250 		vnic->mc_list_count = mc_count;
13251 		update = true;
13252 	}
13253 	return update;
13254 }
13255 
13256 static bool bnxt_uc_list_updated(struct bnxt *bp)
13257 {
13258 	struct net_device *dev = bp->dev;
13259 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13260 	struct netdev_hw_addr *ha;
13261 	int off = 0;
13262 
13263 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
13264 		return true;
13265 
13266 	netdev_for_each_uc_addr(ha, dev) {
13267 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
13268 			return true;
13269 
13270 		off += ETH_ALEN;
13271 	}
13272 	return false;
13273 }
13274 
13275 static void bnxt_set_rx_mode(struct net_device *dev)
13276 {
13277 	struct bnxt *bp = netdev_priv(dev);
13278 	struct bnxt_vnic_info *vnic;
13279 	bool mc_update = false;
13280 	bool uc_update;
13281 	u32 mask;
13282 
13283 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13284 		return;
13285 
13286 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13287 	mask = vnic->rx_mask;
13288 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13289 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13290 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13291 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13292 
13293 	if (dev->flags & IFF_PROMISC)
13294 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13295 
13296 	uc_update = bnxt_uc_list_updated(bp);
13297 
13298 	if (dev->flags & IFF_BROADCAST)
13299 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13300 	if (dev->flags & IFF_ALLMULTI) {
13301 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13302 		vnic->mc_list_count = 0;
13303 	} else if (dev->flags & IFF_MULTICAST) {
13304 		mc_update = bnxt_mc_list_updated(bp, &mask);
13305 	}
13306 
13307 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13308 		vnic->rx_mask = mask;
13309 
13310 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13311 	}
13312 }
13313 
13314 static int bnxt_cfg_rx_mode(struct bnxt *bp)
13315 {
13316 	struct net_device *dev = bp->dev;
13317 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13318 	struct netdev_hw_addr *ha;
13319 	int i, off = 0, rc;
13320 	bool uc_update;
13321 
13322 	netif_addr_lock_bh(dev);
13323 	uc_update = bnxt_uc_list_updated(bp);
13324 	netif_addr_unlock_bh(dev);
13325 
13326 	if (!uc_update)
13327 		goto skip_uc;
13328 
13329 	for (i = 1; i < vnic->uc_filter_count; i++) {
13330 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13331 
13332 		bnxt_hwrm_l2_filter_free(bp, fltr);
13333 		bnxt_del_l2_filter(bp, fltr);
13334 	}
13335 
13336 	vnic->uc_filter_count = 1;
13337 
13338 	netif_addr_lock_bh(dev);
13339 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
13340 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13341 	} else {
13342 		netdev_for_each_uc_addr(ha, dev) {
13343 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13344 			off += ETH_ALEN;
13345 			vnic->uc_filter_count++;
13346 		}
13347 	}
13348 	netif_addr_unlock_bh(dev);
13349 
13350 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13351 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13352 		if (rc) {
13353 			if (BNXT_VF(bp) && rc == -ENODEV) {
13354 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13355 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13356 				else
13357 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
13358 				rc = 0;
13359 			} else {
13360 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13361 			}
13362 			vnic->uc_filter_count = i;
13363 			return rc;
13364 		}
13365 	}
13366 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13367 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
13368 
13369 skip_uc:
13370 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13371 	    !bnxt_promisc_ok(bp))
13372 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13373 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13374 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13375 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13376 			    rc);
13377 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13378 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13379 		vnic->mc_list_count = 0;
13380 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13381 	}
13382 	if (rc)
13383 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13384 			   rc);
13385 
13386 	return rc;
13387 }
13388 
13389 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13390 {
13391 #ifdef CONFIG_BNXT_SRIOV
13392 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13393 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13394 
13395 		/* No minimum rings were provisioned by the PF.  Don't
13396 		 * reserve rings by default when device is down.
13397 		 */
13398 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13399 			return true;
13400 
13401 		if (!netif_running(bp->dev))
13402 			return false;
13403 	}
13404 #endif
13405 	return true;
13406 }
13407 
13408 /* If the chip and firmware supports RFS */
13409 static bool bnxt_rfs_supported(struct bnxt *bp)
13410 {
13411 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13412 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13413 			return true;
13414 		return false;
13415 	}
13416 	/* 212 firmware is broken for aRFS */
13417 	if (BNXT_FW_MAJ(bp) == 212)
13418 		return false;
13419 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13420 		return true;
13421 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13422 		return true;
13423 	return false;
13424 }
13425 
13426 /* If runtime conditions support RFS */
13427 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13428 {
13429 	struct bnxt_hw_rings hwr = {0};
13430 	int max_vnics, max_rss_ctxs;
13431 
13432 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13433 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13434 		return bnxt_rfs_supported(bp);
13435 
13436 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13437 		return false;
13438 
13439 	hwr.grp = bp->rx_nr_rings;
13440 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13441 	if (new_rss_ctx)
13442 		hwr.vnic++;
13443 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13444 	max_vnics = bnxt_get_max_func_vnics(bp);
13445 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13446 
13447 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13448 		if (bp->rx_nr_rings > 1)
13449 			netdev_warn(bp->dev,
13450 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13451 				    min(max_rss_ctxs - 1, max_vnics - 1));
13452 		return false;
13453 	}
13454 
13455 	if (!BNXT_NEW_RM(bp))
13456 		return true;
13457 
13458 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13459 	 * issue that will mess up the default VNIC if we reduce the
13460 	 * reservations.
13461 	 */
13462 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13463 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13464 		return true;
13465 
13466 	bnxt_hwrm_reserve_rings(bp, &hwr);
13467 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13468 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13469 		return true;
13470 
13471 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13472 	hwr.vnic = 1;
13473 	hwr.rss_ctx = 0;
13474 	bnxt_hwrm_reserve_rings(bp, &hwr);
13475 	return false;
13476 }
13477 
13478 static netdev_features_t bnxt_fix_features(struct net_device *dev,
13479 					   netdev_features_t features)
13480 {
13481 	struct bnxt *bp = netdev_priv(dev);
13482 	netdev_features_t vlan_features;
13483 
13484 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
13485 		features &= ~NETIF_F_NTUPLE;
13486 
13487 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
13488 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13489 
13490 	if (!(features & NETIF_F_GRO))
13491 		features &= ~NETIF_F_GRO_HW;
13492 
13493 	if (features & NETIF_F_GRO_HW)
13494 		features &= ~NETIF_F_LRO;
13495 
13496 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
13497 	 * turned on or off together.
13498 	 */
13499 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
13500 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
13501 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13502 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13503 		else if (vlan_features)
13504 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13505 	}
13506 #ifdef CONFIG_BNXT_SRIOV
13507 	if (BNXT_VF(bp) && bp->vf.vlan)
13508 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13509 #endif
13510 	return features;
13511 }
13512 
13513 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
13514 				bool link_re_init, u32 flags, bool update_tpa)
13515 {
13516 	bnxt_close_nic(bp, irq_re_init, link_re_init);
13517 	bp->flags = flags;
13518 	if (update_tpa)
13519 		bnxt_set_ring_params(bp);
13520 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
13521 }
13522 
13523 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
13524 {
13525 	bool update_tpa = false, update_ntuple = false;
13526 	struct bnxt *bp = netdev_priv(dev);
13527 	u32 flags = bp->flags;
13528 	u32 changes;
13529 	int rc = 0;
13530 	bool re_init = false;
13531 
13532 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
13533 	if (features & NETIF_F_GRO_HW)
13534 		flags |= BNXT_FLAG_GRO;
13535 	else if (features & NETIF_F_LRO)
13536 		flags |= BNXT_FLAG_LRO;
13537 
13538 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
13539 		flags &= ~BNXT_FLAG_TPA;
13540 
13541 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13542 		flags |= BNXT_FLAG_STRIP_VLAN;
13543 
13544 	if (features & NETIF_F_NTUPLE)
13545 		flags |= BNXT_FLAG_RFS;
13546 	else
13547 		bnxt_clear_usr_fltrs(bp, true);
13548 
13549 	changes = flags ^ bp->flags;
13550 	if (changes & BNXT_FLAG_TPA) {
13551 		update_tpa = true;
13552 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
13553 		    (flags & BNXT_FLAG_TPA) == 0 ||
13554 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13555 			re_init = true;
13556 	}
13557 
13558 	if (changes & ~BNXT_FLAG_TPA)
13559 		re_init = true;
13560 
13561 	if (changes & BNXT_FLAG_RFS)
13562 		update_ntuple = true;
13563 
13564 	if (flags != bp->flags) {
13565 		u32 old_flags = bp->flags;
13566 
13567 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13568 			bp->flags = flags;
13569 			if (update_tpa)
13570 				bnxt_set_ring_params(bp);
13571 			return rc;
13572 		}
13573 
13574 		if (update_ntuple)
13575 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
13576 
13577 		if (re_init)
13578 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
13579 
13580 		if (update_tpa) {
13581 			bp->flags = flags;
13582 			rc = bnxt_set_tpa(bp,
13583 					  (flags & BNXT_FLAG_TPA) ?
13584 					  true : false);
13585 			if (rc)
13586 				bp->flags = old_flags;
13587 		}
13588 	}
13589 	return rc;
13590 }
13591 
13592 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
13593 			      u8 **nextp)
13594 {
13595 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
13596 	struct hop_jumbo_hdr *jhdr;
13597 	int hdr_count = 0;
13598 	u8 *nexthdr;
13599 	int start;
13600 
13601 	/* Check that there are at most 2 IPv6 extension headers, no
13602 	 * fragment header, and each is <= 64 bytes.
13603 	 */
13604 	start = nw_off + sizeof(*ip6h);
13605 	nexthdr = &ip6h->nexthdr;
13606 	while (ipv6_ext_hdr(*nexthdr)) {
13607 		struct ipv6_opt_hdr *hp;
13608 		int hdrlen;
13609 
13610 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
13611 		    *nexthdr == NEXTHDR_FRAGMENT)
13612 			return false;
13613 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
13614 					  skb_headlen(skb), NULL);
13615 		if (!hp)
13616 			return false;
13617 		if (*nexthdr == NEXTHDR_AUTH)
13618 			hdrlen = ipv6_authlen(hp);
13619 		else
13620 			hdrlen = ipv6_optlen(hp);
13621 
13622 		if (hdrlen > 64)
13623 			return false;
13624 
13625 		/* The ext header may be a hop-by-hop header inserted for
13626 		 * big TCP purposes. This will be removed before sending
13627 		 * from NIC, so do not count it.
13628 		 */
13629 		if (*nexthdr == NEXTHDR_HOP) {
13630 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
13631 				goto increment_hdr;
13632 
13633 			jhdr = (struct hop_jumbo_hdr *)hp;
13634 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
13635 			    jhdr->nexthdr != IPPROTO_TCP)
13636 				goto increment_hdr;
13637 
13638 			goto next_hdr;
13639 		}
13640 increment_hdr:
13641 		hdr_count++;
13642 next_hdr:
13643 		nexthdr = &hp->nexthdr;
13644 		start += hdrlen;
13645 	}
13646 	if (nextp) {
13647 		/* Caller will check inner protocol */
13648 		if (skb->encapsulation) {
13649 			*nextp = nexthdr;
13650 			return true;
13651 		}
13652 		*nextp = NULL;
13653 	}
13654 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
13655 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
13656 }
13657 
13658 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
13659 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
13660 {
13661 	struct udphdr *uh = udp_hdr(skb);
13662 	__be16 udp_port = uh->dest;
13663 
13664 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
13665 	    udp_port != bp->vxlan_gpe_port)
13666 		return false;
13667 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
13668 		struct ethhdr *eh = inner_eth_hdr(skb);
13669 
13670 		switch (eh->h_proto) {
13671 		case htons(ETH_P_IP):
13672 			return true;
13673 		case htons(ETH_P_IPV6):
13674 			return bnxt_exthdr_check(bp, skb,
13675 						 skb_inner_network_offset(skb),
13676 						 NULL);
13677 		}
13678 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
13679 		return true;
13680 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
13681 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13682 					 NULL);
13683 	}
13684 	return false;
13685 }
13686 
13687 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
13688 {
13689 	switch (l4_proto) {
13690 	case IPPROTO_UDP:
13691 		return bnxt_udp_tunl_check(bp, skb);
13692 	case IPPROTO_IPIP:
13693 		return true;
13694 	case IPPROTO_GRE: {
13695 		switch (skb->inner_protocol) {
13696 		default:
13697 			return false;
13698 		case htons(ETH_P_IP):
13699 			return true;
13700 		case htons(ETH_P_IPV6):
13701 			fallthrough;
13702 		}
13703 	}
13704 	case IPPROTO_IPV6:
13705 		/* Check ext headers of inner ipv6 */
13706 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13707 					 NULL);
13708 	}
13709 	return false;
13710 }
13711 
13712 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
13713 					     struct net_device *dev,
13714 					     netdev_features_t features)
13715 {
13716 	struct bnxt *bp = netdev_priv(dev);
13717 	u8 *l4_proto;
13718 
13719 	features = vlan_features_check(skb, features);
13720 	switch (vlan_get_protocol(skb)) {
13721 	case htons(ETH_P_IP):
13722 		if (!skb->encapsulation)
13723 			return features;
13724 		l4_proto = &ip_hdr(skb)->protocol;
13725 		if (bnxt_tunl_check(bp, skb, *l4_proto))
13726 			return features;
13727 		break;
13728 	case htons(ETH_P_IPV6):
13729 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
13730 				       &l4_proto))
13731 			break;
13732 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
13733 			return features;
13734 		break;
13735 	}
13736 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13737 }
13738 
13739 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
13740 			 u32 *reg_buf)
13741 {
13742 	struct hwrm_dbg_read_direct_output *resp;
13743 	struct hwrm_dbg_read_direct_input *req;
13744 	__le32 *dbg_reg_buf;
13745 	dma_addr_t mapping;
13746 	int rc, i;
13747 
13748 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
13749 	if (rc)
13750 		return rc;
13751 
13752 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
13753 					 &mapping);
13754 	if (!dbg_reg_buf) {
13755 		rc = -ENOMEM;
13756 		goto dbg_rd_reg_exit;
13757 	}
13758 
13759 	req->host_dest_addr = cpu_to_le64(mapping);
13760 
13761 	resp = hwrm_req_hold(bp, req);
13762 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
13763 	req->read_len32 = cpu_to_le32(num_words);
13764 
13765 	rc = hwrm_req_send(bp, req);
13766 	if (rc || resp->error_code) {
13767 		rc = -EIO;
13768 		goto dbg_rd_reg_exit;
13769 	}
13770 	for (i = 0; i < num_words; i++)
13771 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
13772 
13773 dbg_rd_reg_exit:
13774 	hwrm_req_drop(bp, req);
13775 	return rc;
13776 }
13777 
13778 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
13779 				       u32 ring_id, u32 *prod, u32 *cons)
13780 {
13781 	struct hwrm_dbg_ring_info_get_output *resp;
13782 	struct hwrm_dbg_ring_info_get_input *req;
13783 	int rc;
13784 
13785 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
13786 	if (rc)
13787 		return rc;
13788 
13789 	req->ring_type = ring_type;
13790 	req->fw_ring_id = cpu_to_le32(ring_id);
13791 	resp = hwrm_req_hold(bp, req);
13792 	rc = hwrm_req_send(bp, req);
13793 	if (!rc) {
13794 		*prod = le32_to_cpu(resp->producer_index);
13795 		*cons = le32_to_cpu(resp->consumer_index);
13796 	}
13797 	hwrm_req_drop(bp, req);
13798 	return rc;
13799 }
13800 
13801 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13802 {
13803 	struct bnxt_tx_ring_info *txr;
13804 	int i = bnapi->index, j;
13805 
13806 	bnxt_for_each_napi_tx(j, bnapi, txr)
13807 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13808 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13809 			    txr->tx_cons);
13810 }
13811 
13812 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
13813 {
13814 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
13815 	int i = bnapi->index;
13816 
13817 	if (!rxr)
13818 		return;
13819 
13820 	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",
13821 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
13822 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
13823 		    rxr->rx_sw_agg_prod);
13824 }
13825 
13826 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
13827 {
13828 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13829 	int i = bnapi->index;
13830 
13831 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
13832 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
13833 }
13834 
13835 static void bnxt_dbg_dump_states(struct bnxt *bp)
13836 {
13837 	int i;
13838 	struct bnxt_napi *bnapi;
13839 
13840 	for (i = 0; i < bp->cp_nr_rings; i++) {
13841 		bnapi = bp->bnapi[i];
13842 		if (netif_msg_drv(bp)) {
13843 			bnxt_dump_tx_sw_state(bnapi);
13844 			bnxt_dump_rx_sw_state(bnapi);
13845 			bnxt_dump_cp_sw_state(bnapi);
13846 		}
13847 	}
13848 }
13849 
13850 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
13851 {
13852 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
13853 	struct hwrm_ring_reset_input *req;
13854 	struct bnxt_napi *bnapi = rxr->bnapi;
13855 	struct bnxt_cp_ring_info *cpr;
13856 	u16 cp_ring_id;
13857 	int rc;
13858 
13859 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
13860 	if (rc)
13861 		return rc;
13862 
13863 	cpr = &bnapi->cp_ring;
13864 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
13865 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
13866 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
13867 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
13868 	return hwrm_req_send_silent(bp, req);
13869 }
13870 
13871 static void bnxt_reset_task(struct bnxt *bp, bool silent)
13872 {
13873 	if (!silent)
13874 		bnxt_dbg_dump_states(bp);
13875 	if (netif_running(bp->dev)) {
13876 		bnxt_close_nic(bp, !silent, false);
13877 		bnxt_open_nic(bp, !silent, false);
13878 	}
13879 }
13880 
13881 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
13882 {
13883 	struct bnxt *bp = netdev_priv(dev);
13884 
13885 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
13886 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
13887 }
13888 
13889 static void bnxt_fw_health_check(struct bnxt *bp)
13890 {
13891 	struct bnxt_fw_health *fw_health = bp->fw_health;
13892 	struct pci_dev *pdev = bp->pdev;
13893 	u32 val;
13894 
13895 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13896 		return;
13897 
13898 	/* Make sure it is enabled before checking the tmr_counter. */
13899 	smp_rmb();
13900 	if (fw_health->tmr_counter) {
13901 		fw_health->tmr_counter--;
13902 		return;
13903 	}
13904 
13905 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13906 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
13907 		fw_health->arrests++;
13908 		goto fw_reset;
13909 	}
13910 
13911 	fw_health->last_fw_heartbeat = val;
13912 
13913 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13914 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
13915 		fw_health->discoveries++;
13916 		goto fw_reset;
13917 	}
13918 
13919 	fw_health->tmr_counter = fw_health->tmr_multiplier;
13920 	return;
13921 
13922 fw_reset:
13923 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
13924 }
13925 
13926 static void bnxt_timer(struct timer_list *t)
13927 {
13928 	struct bnxt *bp = from_timer(bp, t, timer);
13929 	struct net_device *dev = bp->dev;
13930 
13931 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
13932 		return;
13933 
13934 	if (atomic_read(&bp->intr_sem) != 0)
13935 		goto bnxt_restart_timer;
13936 
13937 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
13938 		bnxt_fw_health_check(bp);
13939 
13940 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
13941 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
13942 
13943 	if (bnxt_tc_flower_enabled(bp))
13944 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
13945 
13946 #ifdef CONFIG_RFS_ACCEL
13947 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
13948 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
13949 #endif /*CONFIG_RFS_ACCEL*/
13950 
13951 	if (bp->link_info.phy_retry) {
13952 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
13953 			bp->link_info.phy_retry = false;
13954 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
13955 		} else {
13956 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
13957 		}
13958 	}
13959 
13960 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13961 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13962 
13963 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
13964 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
13965 
13966 bnxt_restart_timer:
13967 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13968 }
13969 
13970 static void bnxt_lock_sp(struct bnxt *bp)
13971 {
13972 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
13973 	 * set.  If the device is being closed, bnxt_close() may be holding
13974 	 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear.
13975 	 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev
13976 	 * instance lock.
13977 	 */
13978 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13979 	netdev_lock(bp->dev);
13980 }
13981 
13982 static void bnxt_unlock_sp(struct bnxt *bp)
13983 {
13984 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13985 	netdev_unlock(bp->dev);
13986 }
13987 
13988 /* Only called from bnxt_sp_task() */
13989 static void bnxt_reset(struct bnxt *bp, bool silent)
13990 {
13991 	bnxt_lock_sp(bp);
13992 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
13993 		bnxt_reset_task(bp, silent);
13994 	bnxt_unlock_sp(bp);
13995 }
13996 
13997 /* Only called from bnxt_sp_task() */
13998 static void bnxt_rx_ring_reset(struct bnxt *bp)
13999 {
14000 	int i;
14001 
14002 	bnxt_lock_sp(bp);
14003 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14004 		bnxt_unlock_sp(bp);
14005 		return;
14006 	}
14007 	/* Disable and flush TPA before resetting the RX ring */
14008 	if (bp->flags & BNXT_FLAG_TPA)
14009 		bnxt_set_tpa(bp, false);
14010 	for (i = 0; i < bp->rx_nr_rings; i++) {
14011 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
14012 		struct bnxt_cp_ring_info *cpr;
14013 		int rc;
14014 
14015 		if (!rxr->bnapi->in_reset)
14016 			continue;
14017 
14018 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
14019 		if (rc) {
14020 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
14021 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
14022 			else
14023 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
14024 					    rc);
14025 			bnxt_reset_task(bp, true);
14026 			break;
14027 		}
14028 		bnxt_free_one_rx_ring_skbs(bp, rxr);
14029 		rxr->rx_prod = 0;
14030 		rxr->rx_agg_prod = 0;
14031 		rxr->rx_sw_agg_prod = 0;
14032 		rxr->rx_next_cons = 0;
14033 		rxr->bnapi->in_reset = false;
14034 		bnxt_alloc_one_rx_ring(bp, i);
14035 		cpr = &rxr->bnapi->cp_ring;
14036 		cpr->sw_stats->rx.rx_resets++;
14037 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
14038 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
14039 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
14040 	}
14041 	if (bp->flags & BNXT_FLAG_TPA)
14042 		bnxt_set_tpa(bp, true);
14043 	bnxt_unlock_sp(bp);
14044 }
14045 
14046 static void bnxt_fw_fatal_close(struct bnxt *bp)
14047 {
14048 	bnxt_tx_disable(bp);
14049 	bnxt_disable_napi(bp);
14050 	bnxt_disable_int_sync(bp);
14051 	bnxt_free_irq(bp);
14052 	bnxt_clear_int_mode(bp);
14053 	pci_disable_device(bp->pdev);
14054 }
14055 
14056 static void bnxt_fw_reset_close(struct bnxt *bp)
14057 {
14058 	/* When firmware is in fatal state, quiesce device and disable
14059 	 * bus master to prevent any potential bad DMAs before freeing
14060 	 * kernel memory.
14061 	 */
14062 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
14063 		u16 val = 0;
14064 
14065 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14066 		if (val == 0xffff)
14067 			bp->fw_reset_min_dsecs = 0;
14068 		bnxt_fw_fatal_close(bp);
14069 	}
14070 	__bnxt_close_nic(bp, true, false);
14071 	bnxt_vf_reps_free(bp);
14072 	bnxt_clear_int_mode(bp);
14073 	bnxt_hwrm_func_drv_unrgtr(bp);
14074 	if (pci_is_enabled(bp->pdev))
14075 		pci_disable_device(bp->pdev);
14076 	bnxt_free_ctx_mem(bp, false);
14077 }
14078 
14079 static bool is_bnxt_fw_ok(struct bnxt *bp)
14080 {
14081 	struct bnxt_fw_health *fw_health = bp->fw_health;
14082 	bool no_heartbeat = false, has_reset = false;
14083 	u32 val;
14084 
14085 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14086 	if (val == fw_health->last_fw_heartbeat)
14087 		no_heartbeat = true;
14088 
14089 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14090 	if (val != fw_health->last_fw_reset_cnt)
14091 		has_reset = true;
14092 
14093 	if (!no_heartbeat && has_reset)
14094 		return true;
14095 
14096 	return false;
14097 }
14098 
14099 /* netdev instance lock is acquired before calling this function */
14100 static void bnxt_force_fw_reset(struct bnxt *bp)
14101 {
14102 	struct bnxt_fw_health *fw_health = bp->fw_health;
14103 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14104 	u32 wait_dsecs;
14105 
14106 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
14107 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14108 		return;
14109 
14110 	/* we have to serialize with bnxt_refclk_read()*/
14111 	if (ptp) {
14112 		unsigned long flags;
14113 
14114 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
14115 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14116 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14117 	} else {
14118 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14119 	}
14120 	bnxt_fw_reset_close(bp);
14121 	wait_dsecs = fw_health->master_func_wait_dsecs;
14122 	if (fw_health->primary) {
14123 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
14124 			wait_dsecs = 0;
14125 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14126 	} else {
14127 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
14128 		wait_dsecs = fw_health->normal_func_wait_dsecs;
14129 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14130 	}
14131 
14132 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
14133 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
14134 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14135 }
14136 
14137 void bnxt_fw_exception(struct bnxt *bp)
14138 {
14139 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
14140 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14141 	bnxt_ulp_stop(bp);
14142 	bnxt_lock_sp(bp);
14143 	bnxt_force_fw_reset(bp);
14144 	bnxt_unlock_sp(bp);
14145 }
14146 
14147 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
14148  * < 0 on error.
14149  */
14150 static int bnxt_get_registered_vfs(struct bnxt *bp)
14151 {
14152 #ifdef CONFIG_BNXT_SRIOV
14153 	int rc;
14154 
14155 	if (!BNXT_PF(bp))
14156 		return 0;
14157 
14158 	rc = bnxt_hwrm_func_qcfg(bp);
14159 	if (rc) {
14160 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
14161 		return rc;
14162 	}
14163 	if (bp->pf.registered_vfs)
14164 		return bp->pf.registered_vfs;
14165 	if (bp->sriov_cfg)
14166 		return 1;
14167 #endif
14168 	return 0;
14169 }
14170 
14171 void bnxt_fw_reset(struct bnxt *bp)
14172 {
14173 	bnxt_ulp_stop(bp);
14174 	bnxt_lock_sp(bp);
14175 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
14176 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14177 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14178 		int n = 0, tmo;
14179 
14180 		/* we have to serialize with bnxt_refclk_read()*/
14181 		if (ptp) {
14182 			unsigned long flags;
14183 
14184 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
14185 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14186 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14187 		} else {
14188 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14189 		}
14190 		if (bp->pf.active_vfs &&
14191 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
14192 			n = bnxt_get_registered_vfs(bp);
14193 		if (n < 0) {
14194 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
14195 				   n);
14196 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14197 			dev_close(bp->dev);
14198 			goto fw_reset_exit;
14199 		} else if (n > 0) {
14200 			u16 vf_tmo_dsecs = n * 10;
14201 
14202 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
14203 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
14204 			bp->fw_reset_state =
14205 				BNXT_FW_RESET_STATE_POLL_VF;
14206 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14207 			goto fw_reset_exit;
14208 		}
14209 		bnxt_fw_reset_close(bp);
14210 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14211 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14212 			tmo = HZ / 10;
14213 		} else {
14214 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14215 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14216 		}
14217 		bnxt_queue_fw_reset_work(bp, tmo);
14218 	}
14219 fw_reset_exit:
14220 	bnxt_unlock_sp(bp);
14221 }
14222 
14223 static void bnxt_chk_missed_irq(struct bnxt *bp)
14224 {
14225 	int i;
14226 
14227 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14228 		return;
14229 
14230 	for (i = 0; i < bp->cp_nr_rings; i++) {
14231 		struct bnxt_napi *bnapi = bp->bnapi[i];
14232 		struct bnxt_cp_ring_info *cpr;
14233 		u32 fw_ring_id;
14234 		int j;
14235 
14236 		if (!bnapi)
14237 			continue;
14238 
14239 		cpr = &bnapi->cp_ring;
14240 		for (j = 0; j < cpr->cp_ring_count; j++) {
14241 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
14242 			u32 val[2];
14243 
14244 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
14245 				continue;
14246 
14247 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
14248 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
14249 				continue;
14250 			}
14251 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
14252 			bnxt_dbg_hwrm_ring_info_get(bp,
14253 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
14254 				fw_ring_id, &val[0], &val[1]);
14255 			cpr->sw_stats->cmn.missed_irqs++;
14256 		}
14257 	}
14258 }
14259 
14260 static void bnxt_cfg_ntp_filters(struct bnxt *);
14261 
14262 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
14263 {
14264 	struct bnxt_link_info *link_info = &bp->link_info;
14265 
14266 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
14267 		link_info->autoneg = BNXT_AUTONEG_SPEED;
14268 		if (bp->hwrm_spec_code >= 0x10201) {
14269 			if (link_info->auto_pause_setting &
14270 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
14271 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14272 		} else {
14273 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14274 		}
14275 		bnxt_set_auto_speed(link_info);
14276 	} else {
14277 		bnxt_set_force_speed(link_info);
14278 		link_info->req_duplex = link_info->duplex_setting;
14279 	}
14280 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14281 		link_info->req_flow_ctrl =
14282 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14283 	else
14284 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14285 }
14286 
14287 static void bnxt_fw_echo_reply(struct bnxt *bp)
14288 {
14289 	struct bnxt_fw_health *fw_health = bp->fw_health;
14290 	struct hwrm_func_echo_response_input *req;
14291 	int rc;
14292 
14293 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14294 	if (rc)
14295 		return;
14296 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14297 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14298 	hwrm_req_send(bp, req);
14299 }
14300 
14301 static void bnxt_ulp_restart(struct bnxt *bp)
14302 {
14303 	bnxt_ulp_stop(bp);
14304 	bnxt_ulp_start(bp, 0);
14305 }
14306 
14307 static void bnxt_sp_task(struct work_struct *work)
14308 {
14309 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14310 
14311 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14312 	smp_mb__after_atomic();
14313 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14314 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14315 		return;
14316 	}
14317 
14318 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14319 		bnxt_ulp_restart(bp);
14320 		bnxt_reenable_sriov(bp);
14321 	}
14322 
14323 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
14324 		bnxt_cfg_rx_mode(bp);
14325 
14326 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14327 		bnxt_cfg_ntp_filters(bp);
14328 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14329 		bnxt_hwrm_exec_fwd_req(bp);
14330 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14331 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14332 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14333 		bnxt_hwrm_port_qstats(bp, 0);
14334 		bnxt_hwrm_port_qstats_ext(bp, 0);
14335 		bnxt_accumulate_all_stats(bp);
14336 	}
14337 
14338 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14339 		int rc;
14340 
14341 		mutex_lock(&bp->link_lock);
14342 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14343 				       &bp->sp_event))
14344 			bnxt_hwrm_phy_qcaps(bp);
14345 
14346 		rc = bnxt_update_link(bp, true);
14347 		if (rc)
14348 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14349 				   rc);
14350 
14351 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14352 				       &bp->sp_event))
14353 			bnxt_init_ethtool_link_settings(bp);
14354 		mutex_unlock(&bp->link_lock);
14355 	}
14356 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14357 		int rc;
14358 
14359 		mutex_lock(&bp->link_lock);
14360 		rc = bnxt_update_phy_setting(bp);
14361 		mutex_unlock(&bp->link_lock);
14362 		if (rc) {
14363 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14364 		} else {
14365 			bp->link_info.phy_retry = false;
14366 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14367 		}
14368 	}
14369 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14370 		mutex_lock(&bp->link_lock);
14371 		bnxt_get_port_module_status(bp);
14372 		mutex_unlock(&bp->link_lock);
14373 	}
14374 
14375 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14376 		bnxt_tc_flow_stats_work(bp);
14377 
14378 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14379 		bnxt_chk_missed_irq(bp);
14380 
14381 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14382 		bnxt_fw_echo_reply(bp);
14383 
14384 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14385 		bnxt_hwmon_notify_event(bp);
14386 
14387 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14388 	 * must be the last functions to be called before exiting.
14389 	 */
14390 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14391 		bnxt_reset(bp, false);
14392 
14393 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14394 		bnxt_reset(bp, true);
14395 
14396 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14397 		bnxt_rx_ring_reset(bp);
14398 
14399 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14400 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14401 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14402 			bnxt_devlink_health_fw_report(bp);
14403 		else
14404 			bnxt_fw_reset(bp);
14405 	}
14406 
14407 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14408 		if (!is_bnxt_fw_ok(bp))
14409 			bnxt_devlink_health_fw_report(bp);
14410 	}
14411 
14412 	smp_mb__before_atomic();
14413 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14414 }
14415 
14416 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14417 				int *max_cp);
14418 
14419 /* Under netdev instance lock */
14420 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14421 		     int tx_xdp)
14422 {
14423 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14424 	struct bnxt_hw_rings hwr = {0};
14425 	int rx_rings = rx;
14426 	int rc;
14427 
14428 	if (tcs)
14429 		tx_sets = tcs;
14430 
14431 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
14432 
14433 	if (max_rx < rx_rings)
14434 		return -ENOMEM;
14435 
14436 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14437 		rx_rings <<= 1;
14438 
14439 	hwr.rx = rx_rings;
14440 	hwr.tx = tx * tx_sets + tx_xdp;
14441 	if (max_tx < hwr.tx)
14442 		return -ENOMEM;
14443 
14444 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
14445 
14446 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
14447 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
14448 	if (max_cp < hwr.cp)
14449 		return -ENOMEM;
14450 	hwr.stat = hwr.cp;
14451 	if (BNXT_NEW_RM(bp)) {
14452 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
14453 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
14454 		hwr.grp = rx;
14455 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14456 	}
14457 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
14458 		hwr.cp_p5 = hwr.tx + rx;
14459 	rc = bnxt_hwrm_check_rings(bp, &hwr);
14460 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
14461 		if (!bnxt_ulp_registered(bp->edev)) {
14462 			hwr.cp += bnxt_get_ulp_msix_num(bp);
14463 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
14464 		}
14465 		if (hwr.cp > bp->total_irqs) {
14466 			int total_msix = bnxt_change_msix(bp, hwr.cp);
14467 
14468 			if (total_msix < hwr.cp) {
14469 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
14470 					    hwr.cp, total_msix);
14471 				rc = -ENOSPC;
14472 			}
14473 		}
14474 	}
14475 	return rc;
14476 }
14477 
14478 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
14479 {
14480 	if (bp->bar2) {
14481 		pci_iounmap(pdev, bp->bar2);
14482 		bp->bar2 = NULL;
14483 	}
14484 
14485 	if (bp->bar1) {
14486 		pci_iounmap(pdev, bp->bar1);
14487 		bp->bar1 = NULL;
14488 	}
14489 
14490 	if (bp->bar0) {
14491 		pci_iounmap(pdev, bp->bar0);
14492 		bp->bar0 = NULL;
14493 	}
14494 }
14495 
14496 static void bnxt_cleanup_pci(struct bnxt *bp)
14497 {
14498 	bnxt_unmap_bars(bp, bp->pdev);
14499 	pci_release_regions(bp->pdev);
14500 	if (pci_is_enabled(bp->pdev))
14501 		pci_disable_device(bp->pdev);
14502 }
14503 
14504 static void bnxt_init_dflt_coal(struct bnxt *bp)
14505 {
14506 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
14507 	struct bnxt_coal *coal;
14508 	u16 flags = 0;
14509 
14510 	if (coal_cap->cmpl_params &
14511 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
14512 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
14513 
14514 	/* Tick values in micro seconds.
14515 	 * 1 coal_buf x bufs_per_record = 1 completion record.
14516 	 */
14517 	coal = &bp->rx_coal;
14518 	coal->coal_ticks = 10;
14519 	coal->coal_bufs = 30;
14520 	coal->coal_ticks_irq = 1;
14521 	coal->coal_bufs_irq = 2;
14522 	coal->idle_thresh = 50;
14523 	coal->bufs_per_record = 2;
14524 	coal->budget = 64;		/* NAPI budget */
14525 	coal->flags = flags;
14526 
14527 	coal = &bp->tx_coal;
14528 	coal->coal_ticks = 28;
14529 	coal->coal_bufs = 30;
14530 	coal->coal_ticks_irq = 2;
14531 	coal->coal_bufs_irq = 2;
14532 	coal->bufs_per_record = 1;
14533 	coal->flags = flags;
14534 
14535 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
14536 }
14537 
14538 /* FW that pre-reserves 1 VNIC per function */
14539 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
14540 {
14541 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
14542 
14543 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14544 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
14545 		return true;
14546 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14547 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
14548 		return true;
14549 	return false;
14550 }
14551 
14552 static int bnxt_fw_init_one_p1(struct bnxt *bp)
14553 {
14554 	int rc;
14555 
14556 	bp->fw_cap = 0;
14557 	rc = bnxt_hwrm_ver_get(bp);
14558 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
14559 	 * so wait before continuing with recovery.
14560 	 */
14561 	if (rc)
14562 		msleep(100);
14563 	bnxt_try_map_fw_health_reg(bp);
14564 	if (rc) {
14565 		rc = bnxt_try_recover_fw(bp);
14566 		if (rc)
14567 			return rc;
14568 		rc = bnxt_hwrm_ver_get(bp);
14569 		if (rc)
14570 			return rc;
14571 	}
14572 
14573 	bnxt_nvm_cfg_ver_get(bp);
14574 
14575 	rc = bnxt_hwrm_func_reset(bp);
14576 	if (rc)
14577 		return -ENODEV;
14578 
14579 	bnxt_hwrm_fw_set_time(bp);
14580 	return 0;
14581 }
14582 
14583 static int bnxt_fw_init_one_p2(struct bnxt *bp)
14584 {
14585 	int rc;
14586 
14587 	/* Get the MAX capabilities for this function */
14588 	rc = bnxt_hwrm_func_qcaps(bp);
14589 	if (rc) {
14590 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
14591 			   rc);
14592 		return -ENODEV;
14593 	}
14594 
14595 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
14596 	if (rc)
14597 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
14598 			    rc);
14599 
14600 	if (bnxt_alloc_fw_health(bp)) {
14601 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
14602 	} else {
14603 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
14604 		if (rc)
14605 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
14606 				    rc);
14607 	}
14608 
14609 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
14610 	if (rc)
14611 		return -ENODEV;
14612 
14613 	rc = bnxt_alloc_crash_dump_mem(bp);
14614 	if (rc)
14615 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
14616 			    rc);
14617 	if (!rc) {
14618 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
14619 		if (rc) {
14620 			bnxt_free_crash_dump_mem(bp);
14621 			netdev_warn(bp->dev,
14622 				    "hwrm crash dump mem failure rc: %d\n", rc);
14623 		}
14624 	}
14625 
14626 	if (bnxt_fw_pre_resv_vnics(bp))
14627 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
14628 
14629 	bnxt_hwrm_func_qcfg(bp);
14630 	bnxt_hwrm_vnic_qcaps(bp);
14631 	bnxt_hwrm_port_led_qcaps(bp);
14632 	bnxt_ethtool_init(bp);
14633 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
14634 		__bnxt_hwrm_ptp_qcfg(bp);
14635 	bnxt_dcb_init(bp);
14636 	bnxt_hwmon_init(bp);
14637 	return 0;
14638 }
14639 
14640 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
14641 {
14642 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
14643 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
14644 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
14645 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
14646 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
14647 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
14648 		bp->rss_hash_delta = bp->rss_hash_cfg;
14649 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
14650 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
14651 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
14652 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
14653 	}
14654 }
14655 
14656 static void bnxt_set_dflt_rfs(struct bnxt *bp)
14657 {
14658 	struct net_device *dev = bp->dev;
14659 
14660 	dev->hw_features &= ~NETIF_F_NTUPLE;
14661 	dev->features &= ~NETIF_F_NTUPLE;
14662 	bp->flags &= ~BNXT_FLAG_RFS;
14663 	if (bnxt_rfs_supported(bp)) {
14664 		dev->hw_features |= NETIF_F_NTUPLE;
14665 		if (bnxt_rfs_capable(bp, false)) {
14666 			bp->flags |= BNXT_FLAG_RFS;
14667 			dev->features |= NETIF_F_NTUPLE;
14668 		}
14669 	}
14670 }
14671 
14672 static void bnxt_fw_init_one_p3(struct bnxt *bp)
14673 {
14674 	struct pci_dev *pdev = bp->pdev;
14675 
14676 	bnxt_set_dflt_rss_hash_type(bp);
14677 	bnxt_set_dflt_rfs(bp);
14678 
14679 	bnxt_get_wol_settings(bp);
14680 	if (bp->flags & BNXT_FLAG_WOL_CAP)
14681 		device_set_wakeup_enable(&pdev->dev, bp->wol);
14682 	else
14683 		device_set_wakeup_capable(&pdev->dev, false);
14684 
14685 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
14686 	bnxt_hwrm_coal_params_qcaps(bp);
14687 }
14688 
14689 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
14690 
14691 int bnxt_fw_init_one(struct bnxt *bp)
14692 {
14693 	int rc;
14694 
14695 	rc = bnxt_fw_init_one_p1(bp);
14696 	if (rc) {
14697 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
14698 		return rc;
14699 	}
14700 	rc = bnxt_fw_init_one_p2(bp);
14701 	if (rc) {
14702 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
14703 		return rc;
14704 	}
14705 	rc = bnxt_probe_phy(bp, false);
14706 	if (rc)
14707 		return rc;
14708 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
14709 	if (rc)
14710 		return rc;
14711 
14712 	bnxt_fw_init_one_p3(bp);
14713 	return 0;
14714 }
14715 
14716 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
14717 {
14718 	struct bnxt_fw_health *fw_health = bp->fw_health;
14719 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
14720 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
14721 	u32 reg_type, reg_off, delay_msecs;
14722 
14723 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
14724 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
14725 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
14726 	switch (reg_type) {
14727 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
14728 		pci_write_config_dword(bp->pdev, reg_off, val);
14729 		break;
14730 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
14731 		writel(reg_off & BNXT_GRC_BASE_MASK,
14732 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
14733 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
14734 		fallthrough;
14735 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
14736 		writel(val, bp->bar0 + reg_off);
14737 		break;
14738 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
14739 		writel(val, bp->bar1 + reg_off);
14740 		break;
14741 	}
14742 	if (delay_msecs) {
14743 		pci_read_config_dword(bp->pdev, 0, &val);
14744 		msleep(delay_msecs);
14745 	}
14746 }
14747 
14748 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
14749 {
14750 	struct hwrm_func_qcfg_output *resp;
14751 	struct hwrm_func_qcfg_input *req;
14752 	bool result = true; /* firmware will enforce if unknown */
14753 
14754 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
14755 		return result;
14756 
14757 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
14758 		return result;
14759 
14760 	req->fid = cpu_to_le16(0xffff);
14761 	resp = hwrm_req_hold(bp, req);
14762 	if (!hwrm_req_send(bp, req))
14763 		result = !!(le16_to_cpu(resp->flags) &
14764 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
14765 	hwrm_req_drop(bp, req);
14766 	return result;
14767 }
14768 
14769 static void bnxt_reset_all(struct bnxt *bp)
14770 {
14771 	struct bnxt_fw_health *fw_health = bp->fw_health;
14772 	int i, rc;
14773 
14774 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14775 		bnxt_fw_reset_via_optee(bp);
14776 		bp->fw_reset_timestamp = jiffies;
14777 		return;
14778 	}
14779 
14780 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
14781 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
14782 			bnxt_fw_reset_writel(bp, i);
14783 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
14784 		struct hwrm_fw_reset_input *req;
14785 
14786 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
14787 		if (!rc) {
14788 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
14789 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
14790 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
14791 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
14792 			rc = hwrm_req_send(bp, req);
14793 		}
14794 		if (rc != -ENODEV)
14795 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
14796 	}
14797 	bp->fw_reset_timestamp = jiffies;
14798 }
14799 
14800 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
14801 {
14802 	return time_after(jiffies, bp->fw_reset_timestamp +
14803 			  (bp->fw_reset_max_dsecs * HZ / 10));
14804 }
14805 
14806 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
14807 {
14808 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14809 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
14810 		bnxt_dl_health_fw_status_update(bp, false);
14811 	bp->fw_reset_state = 0;
14812 	dev_close(bp->dev);
14813 }
14814 
14815 static void bnxt_fw_reset_task(struct work_struct *work)
14816 {
14817 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
14818 	int rc = 0;
14819 
14820 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14821 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
14822 		return;
14823 	}
14824 
14825 	switch (bp->fw_reset_state) {
14826 	case BNXT_FW_RESET_STATE_POLL_VF: {
14827 		int n = bnxt_get_registered_vfs(bp);
14828 		int tmo;
14829 
14830 		if (n < 0) {
14831 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
14832 				   n, jiffies_to_msecs(jiffies -
14833 				   bp->fw_reset_timestamp));
14834 			goto fw_reset_abort;
14835 		} else if (n > 0) {
14836 			if (bnxt_fw_reset_timeout(bp)) {
14837 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14838 				bp->fw_reset_state = 0;
14839 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
14840 					   n);
14841 				goto ulp_start;
14842 			}
14843 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14844 			return;
14845 		}
14846 		bp->fw_reset_timestamp = jiffies;
14847 		netdev_lock(bp->dev);
14848 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
14849 			bnxt_fw_reset_abort(bp, rc);
14850 			netdev_unlock(bp->dev);
14851 			goto ulp_start;
14852 		}
14853 		bnxt_fw_reset_close(bp);
14854 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14855 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14856 			tmo = HZ / 10;
14857 		} else {
14858 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14859 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14860 		}
14861 		netdev_unlock(bp->dev);
14862 		bnxt_queue_fw_reset_work(bp, tmo);
14863 		return;
14864 	}
14865 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
14866 		u32 val;
14867 
14868 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14869 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
14870 		    !bnxt_fw_reset_timeout(bp)) {
14871 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14872 			return;
14873 		}
14874 
14875 		if (!bp->fw_health->primary) {
14876 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
14877 
14878 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14879 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14880 			return;
14881 		}
14882 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14883 	}
14884 		fallthrough;
14885 	case BNXT_FW_RESET_STATE_RESET_FW:
14886 		bnxt_reset_all(bp);
14887 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14888 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
14889 		return;
14890 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
14891 		bnxt_inv_fw_health_reg(bp);
14892 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
14893 		    !bp->fw_reset_min_dsecs) {
14894 			u16 val;
14895 
14896 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14897 			if (val == 0xffff) {
14898 				if (bnxt_fw_reset_timeout(bp)) {
14899 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
14900 					rc = -ETIMEDOUT;
14901 					goto fw_reset_abort;
14902 				}
14903 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
14904 				return;
14905 			}
14906 		}
14907 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14908 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
14909 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
14910 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
14911 			bnxt_dl_remote_reload(bp);
14912 		if (pci_enable_device(bp->pdev)) {
14913 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
14914 			rc = -ENODEV;
14915 			goto fw_reset_abort;
14916 		}
14917 		pci_set_master(bp->pdev);
14918 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
14919 		fallthrough;
14920 	case BNXT_FW_RESET_STATE_POLL_FW:
14921 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
14922 		rc = bnxt_hwrm_poll(bp);
14923 		if (rc) {
14924 			if (bnxt_fw_reset_timeout(bp)) {
14925 				netdev_err(bp->dev, "Firmware reset aborted\n");
14926 				goto fw_reset_abort_status;
14927 			}
14928 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14929 			return;
14930 		}
14931 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
14932 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
14933 		fallthrough;
14934 	case BNXT_FW_RESET_STATE_OPENING:
14935 		while (!netdev_trylock(bp->dev)) {
14936 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14937 			return;
14938 		}
14939 		rc = bnxt_open(bp->dev);
14940 		if (rc) {
14941 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
14942 			bnxt_fw_reset_abort(bp, rc);
14943 			netdev_unlock(bp->dev);
14944 			goto ulp_start;
14945 		}
14946 
14947 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
14948 		    bp->fw_health->enabled) {
14949 			bp->fw_health->last_fw_reset_cnt =
14950 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14951 		}
14952 		bp->fw_reset_state = 0;
14953 		/* Make sure fw_reset_state is 0 before clearing the flag */
14954 		smp_mb__before_atomic();
14955 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14956 		bnxt_ptp_reapply_pps(bp);
14957 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
14958 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
14959 			bnxt_dl_health_fw_recovery_done(bp);
14960 			bnxt_dl_health_fw_status_update(bp, true);
14961 		}
14962 		netdev_unlock(bp->dev);
14963 		bnxt_ulp_start(bp, 0);
14964 		bnxt_reenable_sriov(bp);
14965 		netdev_lock(bp->dev);
14966 		bnxt_vf_reps_alloc(bp);
14967 		bnxt_vf_reps_open(bp);
14968 		netdev_unlock(bp->dev);
14969 		break;
14970 	}
14971 	return;
14972 
14973 fw_reset_abort_status:
14974 	if (bp->fw_health->status_reliable ||
14975 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
14976 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14977 
14978 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
14979 	}
14980 fw_reset_abort:
14981 	netdev_lock(bp->dev);
14982 	bnxt_fw_reset_abort(bp, rc);
14983 	netdev_unlock(bp->dev);
14984 ulp_start:
14985 	bnxt_ulp_start(bp, rc);
14986 }
14987 
14988 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
14989 {
14990 	int rc;
14991 	struct bnxt *bp = netdev_priv(dev);
14992 
14993 	SET_NETDEV_DEV(dev, &pdev->dev);
14994 
14995 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
14996 	rc = pci_enable_device(pdev);
14997 	if (rc) {
14998 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
14999 		goto init_err;
15000 	}
15001 
15002 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
15003 		dev_err(&pdev->dev,
15004 			"Cannot find PCI device base address, aborting\n");
15005 		rc = -ENODEV;
15006 		goto init_err_disable;
15007 	}
15008 
15009 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
15010 	if (rc) {
15011 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
15012 		goto init_err_disable;
15013 	}
15014 
15015 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
15016 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
15017 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
15018 		rc = -EIO;
15019 		goto init_err_release;
15020 	}
15021 
15022 	pci_set_master(pdev);
15023 
15024 	bp->dev = dev;
15025 	bp->pdev = pdev;
15026 
15027 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
15028 	 * determines the BAR size.
15029 	 */
15030 	bp->bar0 = pci_ioremap_bar(pdev, 0);
15031 	if (!bp->bar0) {
15032 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
15033 		rc = -ENOMEM;
15034 		goto init_err_release;
15035 	}
15036 
15037 	bp->bar2 = pci_ioremap_bar(pdev, 4);
15038 	if (!bp->bar2) {
15039 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
15040 		rc = -ENOMEM;
15041 		goto init_err_release;
15042 	}
15043 
15044 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
15045 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
15046 
15047 	spin_lock_init(&bp->ntp_fltr_lock);
15048 #if BITS_PER_LONG == 32
15049 	spin_lock_init(&bp->db_lock);
15050 #endif
15051 
15052 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
15053 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
15054 
15055 	timer_setup(&bp->timer, bnxt_timer, 0);
15056 	bp->current_interval = BNXT_TIMER_INTERVAL;
15057 
15058 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
15059 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
15060 
15061 	clear_bit(BNXT_STATE_OPEN, &bp->state);
15062 	return 0;
15063 
15064 init_err_release:
15065 	bnxt_unmap_bars(bp, pdev);
15066 	pci_release_regions(pdev);
15067 
15068 init_err_disable:
15069 	pci_disable_device(pdev);
15070 
15071 init_err:
15072 	return rc;
15073 }
15074 
15075 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
15076 {
15077 	struct sockaddr *addr = p;
15078 	struct bnxt *bp = netdev_priv(dev);
15079 	int rc = 0;
15080 
15081 	netdev_assert_locked(dev);
15082 
15083 	if (!is_valid_ether_addr(addr->sa_data))
15084 		return -EADDRNOTAVAIL;
15085 
15086 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
15087 		return 0;
15088 
15089 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
15090 	if (rc)
15091 		return rc;
15092 
15093 	eth_hw_addr_set(dev, addr->sa_data);
15094 	bnxt_clear_usr_fltrs(bp, true);
15095 	if (netif_running(dev)) {
15096 		bnxt_close_nic(bp, false, false);
15097 		rc = bnxt_open_nic(bp, false, false);
15098 	}
15099 
15100 	return rc;
15101 }
15102 
15103 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
15104 {
15105 	struct bnxt *bp = netdev_priv(dev);
15106 
15107 	netdev_assert_locked(dev);
15108 
15109 	if (netif_running(dev))
15110 		bnxt_close_nic(bp, true, false);
15111 
15112 	WRITE_ONCE(dev->mtu, new_mtu);
15113 
15114 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
15115 	 * program is attached.  We need to set the AGG rings settings and
15116 	 * rx_skb_func accordingly.
15117 	 */
15118 	if (READ_ONCE(bp->xdp_prog))
15119 		bnxt_set_rx_skb_mode(bp, true);
15120 
15121 	bnxt_set_ring_params(bp);
15122 
15123 	if (netif_running(dev))
15124 		return bnxt_open_nic(bp, true, false);
15125 
15126 	return 0;
15127 }
15128 
15129 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
15130 {
15131 	struct bnxt *bp = netdev_priv(dev);
15132 	bool sh = false;
15133 	int rc, tx_cp;
15134 
15135 	if (tc > bp->max_tc) {
15136 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
15137 			   tc, bp->max_tc);
15138 		return -EINVAL;
15139 	}
15140 
15141 	if (bp->num_tc == tc)
15142 		return 0;
15143 
15144 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
15145 		sh = true;
15146 
15147 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
15148 			      sh, tc, bp->tx_nr_rings_xdp);
15149 	if (rc)
15150 		return rc;
15151 
15152 	/* Needs to close the device and do hw resource re-allocations */
15153 	if (netif_running(bp->dev))
15154 		bnxt_close_nic(bp, true, false);
15155 
15156 	if (tc) {
15157 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
15158 		netdev_set_num_tc(dev, tc);
15159 		bp->num_tc = tc;
15160 	} else {
15161 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15162 		netdev_reset_tc(dev);
15163 		bp->num_tc = 0;
15164 	}
15165 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
15166 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
15167 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
15168 			       tx_cp + bp->rx_nr_rings;
15169 
15170 	if (netif_running(bp->dev))
15171 		return bnxt_open_nic(bp, true, false);
15172 
15173 	return 0;
15174 }
15175 
15176 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
15177 				  void *cb_priv)
15178 {
15179 	struct bnxt *bp = cb_priv;
15180 
15181 	if (!bnxt_tc_flower_enabled(bp) ||
15182 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
15183 		return -EOPNOTSUPP;
15184 
15185 	switch (type) {
15186 	case TC_SETUP_CLSFLOWER:
15187 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
15188 	default:
15189 		return -EOPNOTSUPP;
15190 	}
15191 }
15192 
15193 LIST_HEAD(bnxt_block_cb_list);
15194 
15195 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
15196 			 void *type_data)
15197 {
15198 	struct bnxt *bp = netdev_priv(dev);
15199 
15200 	switch (type) {
15201 	case TC_SETUP_BLOCK:
15202 		return flow_block_cb_setup_simple(type_data,
15203 						  &bnxt_block_cb_list,
15204 						  bnxt_setup_tc_block_cb,
15205 						  bp, bp, true);
15206 	case TC_SETUP_QDISC_MQPRIO: {
15207 		struct tc_mqprio_qopt *mqprio = type_data;
15208 
15209 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
15210 
15211 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
15212 	}
15213 	default:
15214 		return -EOPNOTSUPP;
15215 	}
15216 }
15217 
15218 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
15219 			    const struct sk_buff *skb)
15220 {
15221 	struct bnxt_vnic_info *vnic;
15222 
15223 	if (skb)
15224 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
15225 
15226 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
15227 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
15228 }
15229 
15230 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
15231 			   u32 idx)
15232 {
15233 	struct hlist_head *head;
15234 	int bit_id;
15235 
15236 	spin_lock_bh(&bp->ntp_fltr_lock);
15237 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
15238 	if (bit_id < 0) {
15239 		spin_unlock_bh(&bp->ntp_fltr_lock);
15240 		return -ENOMEM;
15241 	}
15242 
15243 	fltr->base.sw_id = (u16)bit_id;
15244 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
15245 	fltr->base.flags |= BNXT_ACT_RING_DST;
15246 	head = &bp->ntp_fltr_hash_tbl[idx];
15247 	hlist_add_head_rcu(&fltr->base.hash, head);
15248 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
15249 	bnxt_insert_usr_fltr(bp, &fltr->base);
15250 	bp->ntp_fltr_count++;
15251 	spin_unlock_bh(&bp->ntp_fltr_lock);
15252 	return 0;
15253 }
15254 
15255 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
15256 			    struct bnxt_ntuple_filter *f2)
15257 {
15258 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
15259 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
15260 	struct flow_keys *keys1 = &f1->fkeys;
15261 	struct flow_keys *keys2 = &f2->fkeys;
15262 
15263 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
15264 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
15265 		return false;
15266 
15267 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
15268 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
15269 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
15270 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
15271 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
15272 			return false;
15273 	} else {
15274 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
15275 				     &keys2->addrs.v6addrs.src) ||
15276 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
15277 				     &masks2->addrs.v6addrs.src) ||
15278 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15279 				     &keys2->addrs.v6addrs.dst) ||
15280 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
15281 				     &masks2->addrs.v6addrs.dst))
15282 			return false;
15283 	}
15284 
15285 	return keys1->ports.src == keys2->ports.src &&
15286 	       masks1->ports.src == masks2->ports.src &&
15287 	       keys1->ports.dst == keys2->ports.dst &&
15288 	       masks1->ports.dst == masks2->ports.dst &&
15289 	       keys1->control.flags == keys2->control.flags &&
15290 	       f1->l2_fltr == f2->l2_fltr;
15291 }
15292 
15293 struct bnxt_ntuple_filter *
15294 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15295 				struct bnxt_ntuple_filter *fltr, u32 idx)
15296 {
15297 	struct bnxt_ntuple_filter *f;
15298 	struct hlist_head *head;
15299 
15300 	head = &bp->ntp_fltr_hash_tbl[idx];
15301 	hlist_for_each_entry_rcu(f, head, base.hash) {
15302 		if (bnxt_fltr_match(f, fltr))
15303 			return f;
15304 	}
15305 	return NULL;
15306 }
15307 
15308 #ifdef CONFIG_RFS_ACCEL
15309 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15310 			      u16 rxq_index, u32 flow_id)
15311 {
15312 	struct bnxt *bp = netdev_priv(dev);
15313 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15314 	struct flow_keys *fkeys;
15315 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15316 	struct bnxt_l2_filter *l2_fltr;
15317 	int rc = 0, idx;
15318 	u32 flags;
15319 
15320 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15321 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15322 		atomic_inc(&l2_fltr->refcnt);
15323 	} else {
15324 		struct bnxt_l2_key key;
15325 
15326 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15327 		key.vlan = 0;
15328 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15329 		if (!l2_fltr)
15330 			return -EINVAL;
15331 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15332 			bnxt_del_l2_filter(bp, l2_fltr);
15333 			return -EINVAL;
15334 		}
15335 	}
15336 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
15337 	if (!new_fltr) {
15338 		bnxt_del_l2_filter(bp, l2_fltr);
15339 		return -ENOMEM;
15340 	}
15341 
15342 	fkeys = &new_fltr->fkeys;
15343 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15344 		rc = -EPROTONOSUPPORT;
15345 		goto err_free;
15346 	}
15347 
15348 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15349 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15350 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15351 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15352 		rc = -EPROTONOSUPPORT;
15353 		goto err_free;
15354 	}
15355 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15356 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15357 		if (bp->hwrm_spec_code < 0x10601) {
15358 			rc = -EPROTONOSUPPORT;
15359 			goto err_free;
15360 		}
15361 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15362 	}
15363 	flags = fkeys->control.flags;
15364 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15365 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15366 		rc = -EPROTONOSUPPORT;
15367 		goto err_free;
15368 	}
15369 	new_fltr->l2_fltr = l2_fltr;
15370 
15371 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15372 	rcu_read_lock();
15373 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15374 	if (fltr) {
15375 		rc = fltr->base.sw_id;
15376 		rcu_read_unlock();
15377 		goto err_free;
15378 	}
15379 	rcu_read_unlock();
15380 
15381 	new_fltr->flow_id = flow_id;
15382 	new_fltr->base.rxq = rxq_index;
15383 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15384 	if (!rc) {
15385 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15386 		return new_fltr->base.sw_id;
15387 	}
15388 
15389 err_free:
15390 	bnxt_del_l2_filter(bp, l2_fltr);
15391 	kfree(new_fltr);
15392 	return rc;
15393 }
15394 #endif
15395 
15396 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
15397 {
15398 	spin_lock_bh(&bp->ntp_fltr_lock);
15399 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
15400 		spin_unlock_bh(&bp->ntp_fltr_lock);
15401 		return;
15402 	}
15403 	hlist_del_rcu(&fltr->base.hash);
15404 	bnxt_del_one_usr_fltr(bp, &fltr->base);
15405 	bp->ntp_fltr_count--;
15406 	spin_unlock_bh(&bp->ntp_fltr_lock);
15407 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
15408 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
15409 	kfree_rcu(fltr, base.rcu);
15410 }
15411 
15412 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
15413 {
15414 #ifdef CONFIG_RFS_ACCEL
15415 	int i;
15416 
15417 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
15418 		struct hlist_head *head;
15419 		struct hlist_node *tmp;
15420 		struct bnxt_ntuple_filter *fltr;
15421 		int rc;
15422 
15423 		head = &bp->ntp_fltr_hash_tbl[i];
15424 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
15425 			bool del = false;
15426 
15427 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
15428 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
15429 					continue;
15430 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
15431 							fltr->flow_id,
15432 							fltr->base.sw_id)) {
15433 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
15434 									 fltr);
15435 					del = true;
15436 				}
15437 			} else {
15438 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
15439 								       fltr);
15440 				if (rc)
15441 					del = true;
15442 				else
15443 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
15444 			}
15445 
15446 			if (del)
15447 				bnxt_del_ntp_filter(bp, fltr);
15448 		}
15449 	}
15450 #endif
15451 }
15452 
15453 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
15454 				    unsigned int entry, struct udp_tunnel_info *ti)
15455 {
15456 	struct bnxt *bp = netdev_priv(netdev);
15457 	unsigned int cmd;
15458 
15459 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15460 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
15461 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15462 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
15463 	else
15464 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
15465 
15466 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
15467 }
15468 
15469 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
15470 				      unsigned int entry, struct udp_tunnel_info *ti)
15471 {
15472 	struct bnxt *bp = netdev_priv(netdev);
15473 	unsigned int cmd;
15474 
15475 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15476 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
15477 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15478 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
15479 	else
15480 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
15481 
15482 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
15483 }
15484 
15485 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
15486 	.set_port	= bnxt_udp_tunnel_set_port,
15487 	.unset_port	= bnxt_udp_tunnel_unset_port,
15488 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
15489 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15490 	.tables		= {
15491 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15492 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15493 	},
15494 }, bnxt_udp_tunnels_p7 = {
15495 	.set_port	= bnxt_udp_tunnel_set_port,
15496 	.unset_port	= bnxt_udp_tunnel_unset_port,
15497 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
15498 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15499 	.tables		= {
15500 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15501 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15502 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
15503 	},
15504 };
15505 
15506 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
15507 			       struct net_device *dev, u32 filter_mask,
15508 			       int nlflags)
15509 {
15510 	struct bnxt *bp = netdev_priv(dev);
15511 
15512 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
15513 				       nlflags, filter_mask, NULL);
15514 }
15515 
15516 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
15517 			       u16 flags, struct netlink_ext_ack *extack)
15518 {
15519 	struct bnxt *bp = netdev_priv(dev);
15520 	struct nlattr *attr, *br_spec;
15521 	int rem, rc = 0;
15522 
15523 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
15524 		return -EOPNOTSUPP;
15525 
15526 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
15527 	if (!br_spec)
15528 		return -EINVAL;
15529 
15530 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
15531 		u16 mode;
15532 
15533 		mode = nla_get_u16(attr);
15534 		if (mode == bp->br_mode)
15535 			break;
15536 
15537 		rc = bnxt_hwrm_set_br_mode(bp, mode);
15538 		if (!rc)
15539 			bp->br_mode = mode;
15540 		break;
15541 	}
15542 	return rc;
15543 }
15544 
15545 int bnxt_get_port_parent_id(struct net_device *dev,
15546 			    struct netdev_phys_item_id *ppid)
15547 {
15548 	struct bnxt *bp = netdev_priv(dev);
15549 
15550 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
15551 		return -EOPNOTSUPP;
15552 
15553 	/* The PF and it's VF-reps only support the switchdev framework */
15554 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
15555 		return -EOPNOTSUPP;
15556 
15557 	ppid->id_len = sizeof(bp->dsn);
15558 	memcpy(ppid->id, bp->dsn, ppid->id_len);
15559 
15560 	return 0;
15561 }
15562 
15563 static const struct net_device_ops bnxt_netdev_ops = {
15564 	.ndo_open		= bnxt_open,
15565 	.ndo_start_xmit		= bnxt_start_xmit,
15566 	.ndo_stop		= bnxt_close,
15567 	.ndo_get_stats64	= bnxt_get_stats64,
15568 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
15569 	.ndo_eth_ioctl		= bnxt_ioctl,
15570 	.ndo_validate_addr	= eth_validate_addr,
15571 	.ndo_set_mac_address	= bnxt_change_mac_addr,
15572 	.ndo_change_mtu		= bnxt_change_mtu,
15573 	.ndo_fix_features	= bnxt_fix_features,
15574 	.ndo_set_features	= bnxt_set_features,
15575 	.ndo_features_check	= bnxt_features_check,
15576 	.ndo_tx_timeout		= bnxt_tx_timeout,
15577 #ifdef CONFIG_BNXT_SRIOV
15578 	.ndo_get_vf_config	= bnxt_get_vf_config,
15579 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
15580 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
15581 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
15582 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
15583 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
15584 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
15585 #endif
15586 	.ndo_setup_tc           = bnxt_setup_tc,
15587 #ifdef CONFIG_RFS_ACCEL
15588 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
15589 #endif
15590 	.ndo_bpf		= bnxt_xdp,
15591 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
15592 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
15593 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
15594 };
15595 
15596 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
15597 				    struct netdev_queue_stats_rx *stats)
15598 {
15599 	struct bnxt *bp = netdev_priv(dev);
15600 	struct bnxt_cp_ring_info *cpr;
15601 	u64 *sw;
15602 
15603 	cpr = &bp->bnapi[i]->cp_ring;
15604 	sw = cpr->stats.sw_stats;
15605 
15606 	stats->packets = 0;
15607 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
15608 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
15609 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
15610 
15611 	stats->bytes = 0;
15612 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
15613 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
15614 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
15615 
15616 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
15617 }
15618 
15619 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
15620 				    struct netdev_queue_stats_tx *stats)
15621 {
15622 	struct bnxt *bp = netdev_priv(dev);
15623 	struct bnxt_napi *bnapi;
15624 	u64 *sw;
15625 
15626 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
15627 	sw = bnapi->cp_ring.stats.sw_stats;
15628 
15629 	stats->packets = 0;
15630 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
15631 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
15632 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
15633 
15634 	stats->bytes = 0;
15635 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
15636 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
15637 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
15638 }
15639 
15640 static void bnxt_get_base_stats(struct net_device *dev,
15641 				struct netdev_queue_stats_rx *rx,
15642 				struct netdev_queue_stats_tx *tx)
15643 {
15644 	struct bnxt *bp = netdev_priv(dev);
15645 
15646 	rx->packets = bp->net_stats_prev.rx_packets;
15647 	rx->bytes = bp->net_stats_prev.rx_bytes;
15648 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
15649 
15650 	tx->packets = bp->net_stats_prev.tx_packets;
15651 	tx->bytes = bp->net_stats_prev.tx_bytes;
15652 }
15653 
15654 static const struct netdev_stat_ops bnxt_stat_ops = {
15655 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
15656 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
15657 	.get_base_stats		= bnxt_get_base_stats,
15658 };
15659 
15660 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx)
15661 {
15662 	struct bnxt_rx_ring_info *rxr, *clone;
15663 	struct bnxt *bp = netdev_priv(dev);
15664 	struct bnxt_ring_struct *ring;
15665 	int rc;
15666 
15667 	rxr = &bp->rx_ring[idx];
15668 	clone = qmem;
15669 	memcpy(clone, rxr, sizeof(*rxr));
15670 	bnxt_init_rx_ring_struct(bp, clone);
15671 	bnxt_reset_rx_ring_struct(bp, clone);
15672 
15673 	clone->rx_prod = 0;
15674 	clone->rx_agg_prod = 0;
15675 	clone->rx_sw_agg_prod = 0;
15676 	clone->rx_next_cons = 0;
15677 
15678 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
15679 	if (rc)
15680 		return rc;
15681 
15682 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
15683 	if (rc < 0)
15684 		goto err_page_pool_destroy;
15685 
15686 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
15687 					MEM_TYPE_PAGE_POOL,
15688 					clone->page_pool);
15689 	if (rc)
15690 		goto err_rxq_info_unreg;
15691 
15692 	ring = &clone->rx_ring_struct;
15693 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15694 	if (rc)
15695 		goto err_free_rx_ring;
15696 
15697 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
15698 		ring = &clone->rx_agg_ring_struct;
15699 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15700 		if (rc)
15701 			goto err_free_rx_agg_ring;
15702 
15703 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
15704 		if (rc)
15705 			goto err_free_rx_agg_ring;
15706 	}
15707 
15708 	if (bp->flags & BNXT_FLAG_TPA) {
15709 		rc = bnxt_alloc_one_tpa_info(bp, clone);
15710 		if (rc)
15711 			goto err_free_tpa_info;
15712 	}
15713 
15714 	bnxt_init_one_rx_ring_rxbd(bp, clone);
15715 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
15716 
15717 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
15718 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15719 		bnxt_alloc_one_rx_ring_page(bp, clone, idx);
15720 	if (bp->flags & BNXT_FLAG_TPA)
15721 		bnxt_alloc_one_tpa_info_data(bp, clone);
15722 
15723 	return 0;
15724 
15725 err_free_tpa_info:
15726 	bnxt_free_one_tpa_info(bp, clone);
15727 err_free_rx_agg_ring:
15728 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
15729 err_free_rx_ring:
15730 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
15731 err_rxq_info_unreg:
15732 	xdp_rxq_info_unreg(&clone->xdp_rxq);
15733 err_page_pool_destroy:
15734 	page_pool_destroy(clone->page_pool);
15735 	if (bnxt_separate_head_pool())
15736 		page_pool_destroy(clone->head_pool);
15737 	clone->page_pool = NULL;
15738 	clone->head_pool = NULL;
15739 	return rc;
15740 }
15741 
15742 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
15743 {
15744 	struct bnxt_rx_ring_info *rxr = qmem;
15745 	struct bnxt *bp = netdev_priv(dev);
15746 	struct bnxt_ring_struct *ring;
15747 
15748 	bnxt_free_one_rx_ring_skbs(bp, rxr);
15749 
15750 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
15751 
15752 	page_pool_destroy(rxr->page_pool);
15753 	if (bnxt_separate_head_pool())
15754 		page_pool_destroy(rxr->head_pool);
15755 	rxr->page_pool = NULL;
15756 	rxr->head_pool = NULL;
15757 
15758 	ring = &rxr->rx_ring_struct;
15759 	bnxt_free_ring(bp, &ring->ring_mem);
15760 
15761 	ring = &rxr->rx_agg_ring_struct;
15762 	bnxt_free_ring(bp, &ring->ring_mem);
15763 
15764 	kfree(rxr->rx_agg_bmap);
15765 	rxr->rx_agg_bmap = NULL;
15766 }
15767 
15768 static void bnxt_copy_rx_ring(struct bnxt *bp,
15769 			      struct bnxt_rx_ring_info *dst,
15770 			      struct bnxt_rx_ring_info *src)
15771 {
15772 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
15773 	struct bnxt_ring_struct *dst_ring, *src_ring;
15774 	int i;
15775 
15776 	dst_ring = &dst->rx_ring_struct;
15777 	dst_rmem = &dst_ring->ring_mem;
15778 	src_ring = &src->rx_ring_struct;
15779 	src_rmem = &src_ring->ring_mem;
15780 
15781 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15782 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15783 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15784 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15785 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15786 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15787 
15788 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15789 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15790 	*dst_rmem->vmem = *src_rmem->vmem;
15791 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15792 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15793 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15794 	}
15795 
15796 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
15797 		return;
15798 
15799 	dst_ring = &dst->rx_agg_ring_struct;
15800 	dst_rmem = &dst_ring->ring_mem;
15801 	src_ring = &src->rx_agg_ring_struct;
15802 	src_rmem = &src_ring->ring_mem;
15803 
15804 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15805 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15806 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15807 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15808 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15809 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15810 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
15811 
15812 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15813 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15814 	*dst_rmem->vmem = *src_rmem->vmem;
15815 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15816 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15817 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15818 	}
15819 
15820 	dst->rx_agg_bmap = src->rx_agg_bmap;
15821 }
15822 
15823 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx)
15824 {
15825 	struct bnxt *bp = netdev_priv(dev);
15826 	struct bnxt_rx_ring_info *rxr, *clone;
15827 	struct bnxt_cp_ring_info *cpr;
15828 	struct bnxt_vnic_info *vnic;
15829 	struct bnxt_napi *bnapi;
15830 	int i, rc;
15831 
15832 	rxr = &bp->rx_ring[idx];
15833 	clone = qmem;
15834 
15835 	rxr->rx_prod = clone->rx_prod;
15836 	rxr->rx_agg_prod = clone->rx_agg_prod;
15837 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
15838 	rxr->rx_next_cons = clone->rx_next_cons;
15839 	rxr->rx_tpa = clone->rx_tpa;
15840 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
15841 	rxr->page_pool = clone->page_pool;
15842 	rxr->head_pool = clone->head_pool;
15843 	rxr->xdp_rxq = clone->xdp_rxq;
15844 
15845 	bnxt_copy_rx_ring(bp, rxr, clone);
15846 
15847 	bnapi = rxr->bnapi;
15848 	cpr = &bnapi->cp_ring;
15849 
15850 	/* All rings have been reserved and previously allocated.
15851 	 * Reallocating with the same parameters should never fail.
15852 	 */
15853 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
15854 	if (rc)
15855 		goto err_reset;
15856 
15857 	if (bp->tph_mode) {
15858 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
15859 		if (rc)
15860 			goto err_reset;
15861 	}
15862 
15863 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
15864 	if (rc)
15865 		goto err_reset;
15866 
15867 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
15868 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15869 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
15870 
15871 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
15872 		rc = bnxt_tx_queue_start(bp, idx);
15873 		if (rc)
15874 			goto err_reset;
15875 	}
15876 
15877 	napi_enable(&bnapi->napi);
15878 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
15879 
15880 	for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) {
15881 		vnic = &bp->vnic_info[i];
15882 
15883 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
15884 		if (rc) {
15885 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
15886 				   vnic->vnic_id, rc);
15887 			return rc;
15888 		}
15889 		vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
15890 		bnxt_hwrm_vnic_update(bp, vnic,
15891 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15892 	}
15893 
15894 	return 0;
15895 
15896 err_reset:
15897 	netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n",
15898 		   rc);
15899 	napi_enable(&bnapi->napi);
15900 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
15901 	bnxt_reset_task(bp, true);
15902 	return rc;
15903 }
15904 
15905 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
15906 {
15907 	struct bnxt *bp = netdev_priv(dev);
15908 	struct bnxt_rx_ring_info *rxr;
15909 	struct bnxt_cp_ring_info *cpr;
15910 	struct bnxt_vnic_info *vnic;
15911 	struct bnxt_napi *bnapi;
15912 	int i;
15913 
15914 	for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) {
15915 		vnic = &bp->vnic_info[i];
15916 		vnic->mru = 0;
15917 		bnxt_hwrm_vnic_update(bp, vnic,
15918 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15919 	}
15920 	/* Make sure NAPI sees that the VNIC is disabled */
15921 	synchronize_net();
15922 	rxr = &bp->rx_ring[idx];
15923 	bnapi = rxr->bnapi;
15924 	cpr = &bnapi->cp_ring;
15925 	cancel_work_sync(&cpr->dim.work);
15926 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15927 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
15928 	page_pool_disable_direct_recycling(rxr->page_pool);
15929 	if (bnxt_separate_head_pool())
15930 		page_pool_disable_direct_recycling(rxr->head_pool);
15931 
15932 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
15933 		bnxt_tx_queue_stop(bp, idx);
15934 
15935 	/* Disable NAPI now after freeing the rings because HWRM_RING_FREE
15936 	 * completion is handled in NAPI to guarantee no more DMA on that ring
15937 	 * after seeing the completion.
15938 	 */
15939 	napi_disable(&bnapi->napi);
15940 
15941 	if (bp->tph_mode) {
15942 		bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr);
15943 		bnxt_clear_one_cp_ring(bp, rxr->rx_cpr);
15944 	}
15945 	bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
15946 
15947 	memcpy(qmem, rxr, sizeof(*rxr));
15948 	bnxt_init_rx_ring_struct(bp, qmem);
15949 
15950 	return 0;
15951 }
15952 
15953 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
15954 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
15955 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
15956 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
15957 	.ndo_queue_start	= bnxt_queue_start,
15958 	.ndo_queue_stop		= bnxt_queue_stop,
15959 };
15960 
15961 static void bnxt_remove_one(struct pci_dev *pdev)
15962 {
15963 	struct net_device *dev = pci_get_drvdata(pdev);
15964 	struct bnxt *bp = netdev_priv(dev);
15965 
15966 	if (BNXT_PF(bp))
15967 		bnxt_sriov_disable(bp);
15968 
15969 	bnxt_rdma_aux_device_del(bp);
15970 
15971 	bnxt_ptp_clear(bp);
15972 	unregister_netdev(dev);
15973 
15974 	bnxt_rdma_aux_device_uninit(bp);
15975 
15976 	bnxt_free_l2_filters(bp, true);
15977 	bnxt_free_ntp_fltrs(bp, true);
15978 	WARN_ON(bp->num_rss_ctx);
15979 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15980 	/* Flush any pending tasks */
15981 	cancel_work_sync(&bp->sp_task);
15982 	cancel_delayed_work_sync(&bp->fw_reset_task);
15983 	bp->sp_event = 0;
15984 
15985 	bnxt_dl_fw_reporters_destroy(bp);
15986 	bnxt_dl_unregister(bp);
15987 	bnxt_shutdown_tc(bp);
15988 
15989 	bnxt_clear_int_mode(bp);
15990 	bnxt_hwrm_func_drv_unrgtr(bp);
15991 	bnxt_free_hwrm_resources(bp);
15992 	bnxt_hwmon_uninit(bp);
15993 	bnxt_ethtool_free(bp);
15994 	bnxt_dcb_free(bp);
15995 	kfree(bp->ptp_cfg);
15996 	bp->ptp_cfg = NULL;
15997 	kfree(bp->fw_health);
15998 	bp->fw_health = NULL;
15999 	bnxt_cleanup_pci(bp);
16000 	bnxt_free_ctx_mem(bp, true);
16001 	bnxt_free_crash_dump_mem(bp);
16002 	kfree(bp->rss_indir_tbl);
16003 	bp->rss_indir_tbl = NULL;
16004 	bnxt_free_port_stats(bp);
16005 	free_netdev(dev);
16006 }
16007 
16008 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
16009 {
16010 	int rc = 0;
16011 	struct bnxt_link_info *link_info = &bp->link_info;
16012 
16013 	bp->phy_flags = 0;
16014 	rc = bnxt_hwrm_phy_qcaps(bp);
16015 	if (rc) {
16016 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
16017 			   rc);
16018 		return rc;
16019 	}
16020 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
16021 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
16022 	else
16023 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
16024 
16025 	bp->mac_flags = 0;
16026 	bnxt_hwrm_mac_qcaps(bp);
16027 
16028 	if (!fw_dflt)
16029 		return 0;
16030 
16031 	mutex_lock(&bp->link_lock);
16032 	rc = bnxt_update_link(bp, false);
16033 	if (rc) {
16034 		mutex_unlock(&bp->link_lock);
16035 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
16036 			   rc);
16037 		return rc;
16038 	}
16039 
16040 	/* Older firmware does not have supported_auto_speeds, so assume
16041 	 * that all supported speeds can be autonegotiated.
16042 	 */
16043 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
16044 		link_info->support_auto_speeds = link_info->support_speeds;
16045 
16046 	bnxt_init_ethtool_link_settings(bp);
16047 	mutex_unlock(&bp->link_lock);
16048 	return 0;
16049 }
16050 
16051 static int bnxt_get_max_irq(struct pci_dev *pdev)
16052 {
16053 	u16 ctrl;
16054 
16055 	if (!pdev->msix_cap)
16056 		return 1;
16057 
16058 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
16059 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
16060 }
16061 
16062 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16063 				int *max_cp)
16064 {
16065 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
16066 	int max_ring_grps = 0, max_irq;
16067 
16068 	*max_tx = hw_resc->max_tx_rings;
16069 	*max_rx = hw_resc->max_rx_rings;
16070 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
16071 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
16072 			bnxt_get_ulp_msix_num_in_use(bp),
16073 			hw_resc->max_stat_ctxs -
16074 			bnxt_get_ulp_stat_ctxs_in_use(bp));
16075 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
16076 		*max_cp = min_t(int, *max_cp, max_irq);
16077 	max_ring_grps = hw_resc->max_hw_ring_grps;
16078 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
16079 		*max_cp -= 1;
16080 		*max_rx -= 2;
16081 	}
16082 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16083 		*max_rx >>= 1;
16084 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
16085 		int rc;
16086 
16087 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
16088 		if (rc) {
16089 			*max_rx = 0;
16090 			*max_tx = 0;
16091 		}
16092 		/* On P5 chips, max_cp output param should be available NQs */
16093 		*max_cp = max_irq;
16094 	}
16095 	*max_rx = min_t(int, *max_rx, max_ring_grps);
16096 }
16097 
16098 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
16099 {
16100 	int rx, tx, cp;
16101 
16102 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
16103 	*max_rx = rx;
16104 	*max_tx = tx;
16105 	if (!rx || !tx || !cp)
16106 		return -ENOMEM;
16107 
16108 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
16109 }
16110 
16111 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16112 			       bool shared)
16113 {
16114 	int rc;
16115 
16116 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16117 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
16118 		/* Not enough rings, try disabling agg rings. */
16119 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
16120 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16121 		if (rc) {
16122 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
16123 			bp->flags |= BNXT_FLAG_AGG_RINGS;
16124 			return rc;
16125 		}
16126 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
16127 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16128 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16129 		bnxt_set_ring_params(bp);
16130 	}
16131 
16132 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
16133 		int max_cp, max_stat, max_irq;
16134 
16135 		/* Reserve minimum resources for RoCE */
16136 		max_cp = bnxt_get_max_func_cp_rings(bp);
16137 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
16138 		max_irq = bnxt_get_max_func_irqs(bp);
16139 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
16140 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
16141 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
16142 			return 0;
16143 
16144 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
16145 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
16146 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
16147 		max_cp = min_t(int, max_cp, max_irq);
16148 		max_cp = min_t(int, max_cp, max_stat);
16149 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
16150 		if (rc)
16151 			rc = 0;
16152 	}
16153 	return rc;
16154 }
16155 
16156 /* In initial default shared ring setting, each shared ring must have a
16157  * RX/TX ring pair.
16158  */
16159 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
16160 {
16161 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
16162 	bp->rx_nr_rings = bp->cp_nr_rings;
16163 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
16164 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
16165 }
16166 
16167 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
16168 {
16169 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
16170 	int avail_msix;
16171 
16172 	if (!bnxt_can_reserve_rings(bp))
16173 		return 0;
16174 
16175 	if (sh)
16176 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
16177 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
16178 	/* Reduce default rings on multi-port cards so that total default
16179 	 * rings do not exceed CPU count.
16180 	 */
16181 	if (bp->port_count > 1) {
16182 		int max_rings =
16183 			max_t(int, num_online_cpus() / bp->port_count, 1);
16184 
16185 		dflt_rings = min_t(int, dflt_rings, max_rings);
16186 	}
16187 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
16188 	if (rc)
16189 		return rc;
16190 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
16191 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
16192 	if (sh)
16193 		bnxt_trim_dflt_sh_rings(bp);
16194 	else
16195 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
16196 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
16197 
16198 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
16199 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
16200 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
16201 
16202 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
16203 		bnxt_set_dflt_ulp_stat_ctxs(bp);
16204 	}
16205 
16206 	rc = __bnxt_reserve_rings(bp);
16207 	if (rc && rc != -ENODEV)
16208 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
16209 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16210 	if (sh)
16211 		bnxt_trim_dflt_sh_rings(bp);
16212 
16213 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
16214 	if (bnxt_need_reserve_rings(bp)) {
16215 		rc = __bnxt_reserve_rings(bp);
16216 		if (rc && rc != -ENODEV)
16217 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
16218 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16219 	}
16220 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
16221 		bp->rx_nr_rings++;
16222 		bp->cp_nr_rings++;
16223 	}
16224 	if (rc) {
16225 		bp->tx_nr_rings = 0;
16226 		bp->rx_nr_rings = 0;
16227 	}
16228 	return rc;
16229 }
16230 
16231 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
16232 {
16233 	int rc;
16234 
16235 	if (bp->tx_nr_rings)
16236 		return 0;
16237 
16238 	bnxt_ulp_irq_stop(bp);
16239 	bnxt_clear_int_mode(bp);
16240 	rc = bnxt_set_dflt_rings(bp, true);
16241 	if (rc) {
16242 		if (BNXT_VF(bp) && rc == -ENODEV)
16243 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16244 		else
16245 			netdev_err(bp->dev, "Not enough rings available.\n");
16246 		goto init_dflt_ring_err;
16247 	}
16248 	rc = bnxt_init_int_mode(bp);
16249 	if (rc)
16250 		goto init_dflt_ring_err;
16251 
16252 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16253 
16254 	bnxt_set_dflt_rfs(bp);
16255 
16256 init_dflt_ring_err:
16257 	bnxt_ulp_irq_restart(bp, rc);
16258 	return rc;
16259 }
16260 
16261 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
16262 {
16263 	int rc;
16264 
16265 	netdev_ops_assert_locked(bp->dev);
16266 	bnxt_hwrm_func_qcaps(bp);
16267 
16268 	if (netif_running(bp->dev))
16269 		__bnxt_close_nic(bp, true, false);
16270 
16271 	bnxt_ulp_irq_stop(bp);
16272 	bnxt_clear_int_mode(bp);
16273 	rc = bnxt_init_int_mode(bp);
16274 	bnxt_ulp_irq_restart(bp, rc);
16275 
16276 	if (netif_running(bp->dev)) {
16277 		if (rc)
16278 			dev_close(bp->dev);
16279 		else
16280 			rc = bnxt_open_nic(bp, true, false);
16281 	}
16282 
16283 	return rc;
16284 }
16285 
16286 static int bnxt_init_mac_addr(struct bnxt *bp)
16287 {
16288 	int rc = 0;
16289 
16290 	if (BNXT_PF(bp)) {
16291 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
16292 	} else {
16293 #ifdef CONFIG_BNXT_SRIOV
16294 		struct bnxt_vf_info *vf = &bp->vf;
16295 		bool strict_approval = true;
16296 
16297 		if (is_valid_ether_addr(vf->mac_addr)) {
16298 			/* overwrite netdev dev_addr with admin VF MAC */
16299 			eth_hw_addr_set(bp->dev, vf->mac_addr);
16300 			/* Older PF driver or firmware may not approve this
16301 			 * correctly.
16302 			 */
16303 			strict_approval = false;
16304 		} else {
16305 			eth_hw_addr_random(bp->dev);
16306 		}
16307 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
16308 #endif
16309 	}
16310 	return rc;
16311 }
16312 
16313 static void bnxt_vpd_read_info(struct bnxt *bp)
16314 {
16315 	struct pci_dev *pdev = bp->pdev;
16316 	unsigned int vpd_size, kw_len;
16317 	int pos, size;
16318 	u8 *vpd_data;
16319 
16320 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
16321 	if (IS_ERR(vpd_data)) {
16322 		pci_warn(pdev, "Unable to read VPD\n");
16323 		return;
16324 	}
16325 
16326 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16327 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
16328 	if (pos < 0)
16329 		goto read_sn;
16330 
16331 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16332 	memcpy(bp->board_partno, &vpd_data[pos], size);
16333 
16334 read_sn:
16335 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16336 					   PCI_VPD_RO_KEYWORD_SERIALNO,
16337 					   &kw_len);
16338 	if (pos < 0)
16339 		goto exit;
16340 
16341 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16342 	memcpy(bp->board_serialno, &vpd_data[pos], size);
16343 exit:
16344 	kfree(vpd_data);
16345 }
16346 
16347 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
16348 {
16349 	struct pci_dev *pdev = bp->pdev;
16350 	u64 qword;
16351 
16352 	qword = pci_get_dsn(pdev);
16353 	if (!qword) {
16354 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
16355 		return -EOPNOTSUPP;
16356 	}
16357 
16358 	put_unaligned_le64(qword, dsn);
16359 
16360 	bp->flags |= BNXT_FLAG_DSN_VALID;
16361 	return 0;
16362 }
16363 
16364 static int bnxt_map_db_bar(struct bnxt *bp)
16365 {
16366 	if (!bp->db_size)
16367 		return -ENODEV;
16368 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
16369 	if (!bp->bar1)
16370 		return -ENOMEM;
16371 	return 0;
16372 }
16373 
16374 void bnxt_print_device_info(struct bnxt *bp)
16375 {
16376 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
16377 		    board_info[bp->board_idx].name,
16378 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
16379 
16380 	pcie_print_link_status(bp->pdev);
16381 }
16382 
16383 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
16384 {
16385 	struct bnxt_hw_resc *hw_resc;
16386 	struct net_device *dev;
16387 	struct bnxt *bp;
16388 	int rc, max_irqs;
16389 
16390 	if (pci_is_bridge(pdev))
16391 		return -ENODEV;
16392 
16393 	if (!pdev->msix_cap) {
16394 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
16395 		return -ENODEV;
16396 	}
16397 
16398 	/* Clear any pending DMA transactions from crash kernel
16399 	 * while loading driver in capture kernel.
16400 	 */
16401 	if (is_kdump_kernel()) {
16402 		pci_clear_master(pdev);
16403 		pcie_flr(pdev);
16404 	}
16405 
16406 	max_irqs = bnxt_get_max_irq(pdev);
16407 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
16408 				 max_irqs);
16409 	if (!dev)
16410 		return -ENOMEM;
16411 
16412 	bp = netdev_priv(dev);
16413 	bp->board_idx = ent->driver_data;
16414 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
16415 	bnxt_set_max_func_irqs(bp, max_irqs);
16416 
16417 	if (bnxt_vf_pciid(bp->board_idx))
16418 		bp->flags |= BNXT_FLAG_VF;
16419 
16420 	/* No devlink port registration in case of a VF */
16421 	if (BNXT_PF(bp))
16422 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
16423 
16424 	rc = bnxt_init_board(pdev, dev);
16425 	if (rc < 0)
16426 		goto init_err_free;
16427 
16428 	dev->netdev_ops = &bnxt_netdev_ops;
16429 	dev->stat_ops = &bnxt_stat_ops;
16430 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
16431 	dev->ethtool_ops = &bnxt_ethtool_ops;
16432 	pci_set_drvdata(pdev, dev);
16433 
16434 	rc = bnxt_alloc_hwrm_resources(bp);
16435 	if (rc)
16436 		goto init_err_pci_clean;
16437 
16438 	mutex_init(&bp->hwrm_cmd_lock);
16439 	mutex_init(&bp->link_lock);
16440 
16441 	rc = bnxt_fw_init_one_p1(bp);
16442 	if (rc)
16443 		goto init_err_pci_clean;
16444 
16445 	if (BNXT_PF(bp))
16446 		bnxt_vpd_read_info(bp);
16447 
16448 	if (BNXT_CHIP_P5_PLUS(bp)) {
16449 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
16450 		if (BNXT_CHIP_P7(bp))
16451 			bp->flags |= BNXT_FLAG_CHIP_P7;
16452 	}
16453 
16454 	rc = bnxt_alloc_rss_indir_tbl(bp);
16455 	if (rc)
16456 		goto init_err_pci_clean;
16457 
16458 	rc = bnxt_fw_init_one_p2(bp);
16459 	if (rc)
16460 		goto init_err_pci_clean;
16461 
16462 	rc = bnxt_map_db_bar(bp);
16463 	if (rc) {
16464 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
16465 			rc);
16466 		goto init_err_pci_clean;
16467 	}
16468 
16469 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16470 			   NETIF_F_TSO | NETIF_F_TSO6 |
16471 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16472 			   NETIF_F_GSO_IPXIP4 |
16473 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16474 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
16475 			   NETIF_F_RXCSUM | NETIF_F_GRO;
16476 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16477 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
16478 
16479 	if (BNXT_SUPPORTS_TPA(bp))
16480 		dev->hw_features |= NETIF_F_LRO;
16481 
16482 	dev->hw_enc_features =
16483 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16484 			NETIF_F_TSO | NETIF_F_TSO6 |
16485 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16486 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16487 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
16488 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16489 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
16490 	if (bp->flags & BNXT_FLAG_CHIP_P7)
16491 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
16492 	else
16493 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
16494 
16495 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
16496 				    NETIF_F_GSO_GRE_CSUM;
16497 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
16498 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
16499 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
16500 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
16501 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
16502 	if (BNXT_SUPPORTS_TPA(bp))
16503 		dev->hw_features |= NETIF_F_GRO_HW;
16504 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
16505 	if (dev->features & NETIF_F_GRO_HW)
16506 		dev->features &= ~NETIF_F_LRO;
16507 	dev->priv_flags |= IFF_UNICAST_FLT;
16508 
16509 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
16510 	if (bp->tso_max_segs)
16511 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
16512 
16513 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
16514 			    NETDEV_XDP_ACT_RX_SG;
16515 
16516 #ifdef CONFIG_BNXT_SRIOV
16517 	init_waitqueue_head(&bp->sriov_cfg_wait);
16518 #endif
16519 	if (BNXT_SUPPORTS_TPA(bp)) {
16520 		bp->gro_func = bnxt_gro_func_5730x;
16521 		if (BNXT_CHIP_P4(bp))
16522 			bp->gro_func = bnxt_gro_func_5731x;
16523 		else if (BNXT_CHIP_P5_PLUS(bp))
16524 			bp->gro_func = bnxt_gro_func_5750x;
16525 	}
16526 	if (!BNXT_CHIP_P4_PLUS(bp))
16527 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
16528 
16529 	rc = bnxt_init_mac_addr(bp);
16530 	if (rc) {
16531 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
16532 		rc = -EADDRNOTAVAIL;
16533 		goto init_err_pci_clean;
16534 	}
16535 
16536 	if (BNXT_PF(bp)) {
16537 		/* Read the adapter's DSN to use as the eswitch switch_id */
16538 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
16539 	}
16540 
16541 	/* MTU range: 60 - FW defined max */
16542 	dev->min_mtu = ETH_ZLEN;
16543 	dev->max_mtu = bp->max_mtu;
16544 
16545 	rc = bnxt_probe_phy(bp, true);
16546 	if (rc)
16547 		goto init_err_pci_clean;
16548 
16549 	hw_resc = &bp->hw_resc;
16550 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
16551 		       BNXT_L2_FLTR_MAX_FLTR;
16552 	/* Older firmware may not report these filters properly */
16553 	if (bp->max_fltr < BNXT_MAX_FLTR)
16554 		bp->max_fltr = BNXT_MAX_FLTR;
16555 	bnxt_init_l2_fltr_tbl(bp);
16556 	__bnxt_set_rx_skb_mode(bp, false);
16557 	bnxt_set_tpa_flags(bp);
16558 	bnxt_init_ring_params(bp);
16559 	bnxt_set_ring_params(bp);
16560 	bnxt_rdma_aux_device_init(bp);
16561 	rc = bnxt_set_dflt_rings(bp, true);
16562 	if (rc) {
16563 		if (BNXT_VF(bp) && rc == -ENODEV) {
16564 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16565 		} else {
16566 			netdev_err(bp->dev, "Not enough rings available.\n");
16567 			rc = -ENOMEM;
16568 		}
16569 		goto init_err_pci_clean;
16570 	}
16571 
16572 	bnxt_fw_init_one_p3(bp);
16573 
16574 	bnxt_init_dflt_coal(bp);
16575 
16576 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
16577 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
16578 
16579 	rc = bnxt_init_int_mode(bp);
16580 	if (rc)
16581 		goto init_err_pci_clean;
16582 
16583 	/* No TC has been set yet and rings may have been trimmed due to
16584 	 * limited MSIX, so we re-initialize the TX rings per TC.
16585 	 */
16586 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16587 
16588 	if (BNXT_PF(bp)) {
16589 		if (!bnxt_pf_wq) {
16590 			bnxt_pf_wq =
16591 				create_singlethread_workqueue("bnxt_pf_wq");
16592 			if (!bnxt_pf_wq) {
16593 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
16594 				rc = -ENOMEM;
16595 				goto init_err_pci_clean;
16596 			}
16597 		}
16598 		rc = bnxt_init_tc(bp);
16599 		if (rc)
16600 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
16601 				   rc);
16602 	}
16603 
16604 	bnxt_inv_fw_health_reg(bp);
16605 	rc = bnxt_dl_register(bp);
16606 	if (rc)
16607 		goto init_err_dl;
16608 
16609 	INIT_LIST_HEAD(&bp->usr_fltr_list);
16610 
16611 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
16612 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
16613 	if (BNXT_SUPPORTS_QUEUE_API(bp))
16614 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
16615 
16616 	rc = register_netdev(dev);
16617 	if (rc)
16618 		goto init_err_cleanup;
16619 
16620 	bnxt_dl_fw_reporters_create(bp);
16621 
16622 	bnxt_rdma_aux_device_add(bp);
16623 
16624 	bnxt_print_device_info(bp);
16625 
16626 	pci_save_state(pdev);
16627 
16628 	return 0;
16629 init_err_cleanup:
16630 	bnxt_rdma_aux_device_uninit(bp);
16631 	bnxt_dl_unregister(bp);
16632 init_err_dl:
16633 	bnxt_shutdown_tc(bp);
16634 	bnxt_clear_int_mode(bp);
16635 
16636 init_err_pci_clean:
16637 	bnxt_hwrm_func_drv_unrgtr(bp);
16638 	bnxt_free_hwrm_resources(bp);
16639 	bnxt_hwmon_uninit(bp);
16640 	bnxt_ethtool_free(bp);
16641 	bnxt_ptp_clear(bp);
16642 	kfree(bp->ptp_cfg);
16643 	bp->ptp_cfg = NULL;
16644 	kfree(bp->fw_health);
16645 	bp->fw_health = NULL;
16646 	bnxt_cleanup_pci(bp);
16647 	bnxt_free_ctx_mem(bp, true);
16648 	bnxt_free_crash_dump_mem(bp);
16649 	kfree(bp->rss_indir_tbl);
16650 	bp->rss_indir_tbl = NULL;
16651 
16652 init_err_free:
16653 	free_netdev(dev);
16654 	return rc;
16655 }
16656 
16657 static void bnxt_shutdown(struct pci_dev *pdev)
16658 {
16659 	struct net_device *dev = pci_get_drvdata(pdev);
16660 	struct bnxt *bp;
16661 
16662 	if (!dev)
16663 		return;
16664 
16665 	netdev_lock(dev);
16666 	bp = netdev_priv(dev);
16667 	if (!bp)
16668 		goto shutdown_exit;
16669 
16670 	if (netif_running(dev))
16671 		dev_close(dev);
16672 
16673 	bnxt_ptp_clear(bp);
16674 	bnxt_clear_int_mode(bp);
16675 	pci_disable_device(pdev);
16676 
16677 	if (system_state == SYSTEM_POWER_OFF) {
16678 		pci_wake_from_d3(pdev, bp->wol);
16679 		pci_set_power_state(pdev, PCI_D3hot);
16680 	}
16681 
16682 shutdown_exit:
16683 	netdev_unlock(dev);
16684 }
16685 
16686 #ifdef CONFIG_PM_SLEEP
16687 static int bnxt_suspend(struct device *device)
16688 {
16689 	struct net_device *dev = dev_get_drvdata(device);
16690 	struct bnxt *bp = netdev_priv(dev);
16691 	int rc = 0;
16692 
16693 	bnxt_ulp_stop(bp);
16694 
16695 	netdev_lock(dev);
16696 	if (netif_running(dev)) {
16697 		netif_device_detach(dev);
16698 		rc = bnxt_close(dev);
16699 	}
16700 	bnxt_hwrm_func_drv_unrgtr(bp);
16701 	bnxt_ptp_clear(bp);
16702 	pci_disable_device(bp->pdev);
16703 	bnxt_free_ctx_mem(bp, false);
16704 	netdev_unlock(dev);
16705 	return rc;
16706 }
16707 
16708 static int bnxt_resume(struct device *device)
16709 {
16710 	struct net_device *dev = dev_get_drvdata(device);
16711 	struct bnxt *bp = netdev_priv(dev);
16712 	int rc = 0;
16713 
16714 	netdev_lock(dev);
16715 	rc = pci_enable_device(bp->pdev);
16716 	if (rc) {
16717 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
16718 			   rc);
16719 		goto resume_exit;
16720 	}
16721 	pci_set_master(bp->pdev);
16722 	if (bnxt_hwrm_ver_get(bp)) {
16723 		rc = -ENODEV;
16724 		goto resume_exit;
16725 	}
16726 	rc = bnxt_hwrm_func_reset(bp);
16727 	if (rc) {
16728 		rc = -EBUSY;
16729 		goto resume_exit;
16730 	}
16731 
16732 	rc = bnxt_hwrm_func_qcaps(bp);
16733 	if (rc)
16734 		goto resume_exit;
16735 
16736 	bnxt_clear_reservations(bp, true);
16737 
16738 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
16739 		rc = -ENODEV;
16740 		goto resume_exit;
16741 	}
16742 	if (bp->fw_crash_mem)
16743 		bnxt_hwrm_crash_dump_mem_cfg(bp);
16744 
16745 	if (bnxt_ptp_init(bp)) {
16746 		kfree(bp->ptp_cfg);
16747 		bp->ptp_cfg = NULL;
16748 	}
16749 	bnxt_get_wol_settings(bp);
16750 	if (netif_running(dev)) {
16751 		rc = bnxt_open(dev);
16752 		if (!rc)
16753 			netif_device_attach(dev);
16754 	}
16755 
16756 resume_exit:
16757 	netdev_unlock(bp->dev);
16758 	bnxt_ulp_start(bp, rc);
16759 	if (!rc)
16760 		bnxt_reenable_sriov(bp);
16761 	return rc;
16762 }
16763 
16764 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
16765 #define BNXT_PM_OPS (&bnxt_pm_ops)
16766 
16767 #else
16768 
16769 #define BNXT_PM_OPS NULL
16770 
16771 #endif /* CONFIG_PM_SLEEP */
16772 
16773 /**
16774  * bnxt_io_error_detected - called when PCI error is detected
16775  * @pdev: Pointer to PCI device
16776  * @state: The current pci connection state
16777  *
16778  * This function is called after a PCI bus error affecting
16779  * this device has been detected.
16780  */
16781 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
16782 					       pci_channel_state_t state)
16783 {
16784 	struct net_device *netdev = pci_get_drvdata(pdev);
16785 	struct bnxt *bp = netdev_priv(netdev);
16786 	bool abort = false;
16787 
16788 	netdev_info(netdev, "PCI I/O error detected\n");
16789 
16790 	bnxt_ulp_stop(bp);
16791 
16792 	netdev_lock(netdev);
16793 	netif_device_detach(netdev);
16794 
16795 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
16796 		netdev_err(bp->dev, "Firmware reset already in progress\n");
16797 		abort = true;
16798 	}
16799 
16800 	if (abort || state == pci_channel_io_perm_failure) {
16801 		netdev_unlock(netdev);
16802 		return PCI_ERS_RESULT_DISCONNECT;
16803 	}
16804 
16805 	/* Link is not reliable anymore if state is pci_channel_io_frozen
16806 	 * so we disable bus master to prevent any potential bad DMAs before
16807 	 * freeing kernel memory.
16808 	 */
16809 	if (state == pci_channel_io_frozen) {
16810 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
16811 		bnxt_fw_fatal_close(bp);
16812 	}
16813 
16814 	if (netif_running(netdev))
16815 		__bnxt_close_nic(bp, true, true);
16816 
16817 	if (pci_is_enabled(pdev))
16818 		pci_disable_device(pdev);
16819 	bnxt_free_ctx_mem(bp, false);
16820 	netdev_unlock(netdev);
16821 
16822 	/* Request a slot slot reset. */
16823 	return PCI_ERS_RESULT_NEED_RESET;
16824 }
16825 
16826 /**
16827  * bnxt_io_slot_reset - called after the pci bus has been reset.
16828  * @pdev: Pointer to PCI device
16829  *
16830  * Restart the card from scratch, as if from a cold-boot.
16831  * At this point, the card has experienced a hard reset,
16832  * followed by fixups by BIOS, and has its config space
16833  * set up identically to what it was at cold boot.
16834  */
16835 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
16836 {
16837 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
16838 	struct net_device *netdev = pci_get_drvdata(pdev);
16839 	struct bnxt *bp = netdev_priv(netdev);
16840 	int retry = 0;
16841 	int err = 0;
16842 	int off;
16843 
16844 	netdev_info(bp->dev, "PCI Slot Reset\n");
16845 
16846 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16847 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
16848 		msleep(900);
16849 
16850 	netdev_lock(netdev);
16851 
16852 	if (pci_enable_device(pdev)) {
16853 		dev_err(&pdev->dev,
16854 			"Cannot re-enable PCI device after reset.\n");
16855 	} else {
16856 		pci_set_master(pdev);
16857 		/* Upon fatal error, our device internal logic that latches to
16858 		 * BAR value is getting reset and will restore only upon
16859 		 * rewriting the BARs.
16860 		 *
16861 		 * As pci_restore_state() does not re-write the BARs if the
16862 		 * value is same as saved value earlier, driver needs to
16863 		 * write the BARs to 0 to force restore, in case of fatal error.
16864 		 */
16865 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
16866 				       &bp->state)) {
16867 			for (off = PCI_BASE_ADDRESS_0;
16868 			     off <= PCI_BASE_ADDRESS_5; off += 4)
16869 				pci_write_config_dword(bp->pdev, off, 0);
16870 		}
16871 		pci_restore_state(pdev);
16872 		pci_save_state(pdev);
16873 
16874 		bnxt_inv_fw_health_reg(bp);
16875 		bnxt_try_map_fw_health_reg(bp);
16876 
16877 		/* In some PCIe AER scenarios, firmware may take up to
16878 		 * 10 seconds to become ready in the worst case.
16879 		 */
16880 		do {
16881 			err = bnxt_try_recover_fw(bp);
16882 			if (!err)
16883 				break;
16884 			retry++;
16885 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
16886 
16887 		if (err) {
16888 			dev_err(&pdev->dev, "Firmware not ready\n");
16889 			goto reset_exit;
16890 		}
16891 
16892 		err = bnxt_hwrm_func_reset(bp);
16893 		if (!err)
16894 			result = PCI_ERS_RESULT_RECOVERED;
16895 
16896 		bnxt_ulp_irq_stop(bp);
16897 		bnxt_clear_int_mode(bp);
16898 		err = bnxt_init_int_mode(bp);
16899 		bnxt_ulp_irq_restart(bp, err);
16900 	}
16901 
16902 reset_exit:
16903 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16904 	bnxt_clear_reservations(bp, true);
16905 	netdev_unlock(netdev);
16906 
16907 	return result;
16908 }
16909 
16910 /**
16911  * bnxt_io_resume - called when traffic can start flowing again.
16912  * @pdev: Pointer to PCI device
16913  *
16914  * This callback is called when the error recovery driver tells
16915  * us that its OK to resume normal operation.
16916  */
16917 static void bnxt_io_resume(struct pci_dev *pdev)
16918 {
16919 	struct net_device *netdev = pci_get_drvdata(pdev);
16920 	struct bnxt *bp = netdev_priv(netdev);
16921 	int err;
16922 
16923 	netdev_info(bp->dev, "PCI Slot Resume\n");
16924 	netdev_lock(netdev);
16925 
16926 	err = bnxt_hwrm_func_qcaps(bp);
16927 	if (!err) {
16928 		if (netif_running(netdev))
16929 			err = bnxt_open(netdev);
16930 		else
16931 			err = bnxt_reserve_rings(bp, true);
16932 	}
16933 
16934 	if (!err)
16935 		netif_device_attach(netdev);
16936 
16937 	netdev_unlock(netdev);
16938 	bnxt_ulp_start(bp, err);
16939 	if (!err)
16940 		bnxt_reenable_sriov(bp);
16941 }
16942 
16943 static const struct pci_error_handlers bnxt_err_handler = {
16944 	.error_detected	= bnxt_io_error_detected,
16945 	.slot_reset	= bnxt_io_slot_reset,
16946 	.resume		= bnxt_io_resume
16947 };
16948 
16949 static struct pci_driver bnxt_pci_driver = {
16950 	.name		= DRV_MODULE_NAME,
16951 	.id_table	= bnxt_pci_tbl,
16952 	.probe		= bnxt_init_one,
16953 	.remove		= bnxt_remove_one,
16954 	.shutdown	= bnxt_shutdown,
16955 	.driver.pm	= BNXT_PM_OPS,
16956 	.err_handler	= &bnxt_err_handler,
16957 #if defined(CONFIG_BNXT_SRIOV)
16958 	.sriov_configure = bnxt_sriov_configure,
16959 #endif
16960 };
16961 
16962 static int __init bnxt_init(void)
16963 {
16964 	int err;
16965 
16966 	bnxt_debug_init();
16967 	err = pci_register_driver(&bnxt_pci_driver);
16968 	if (err) {
16969 		bnxt_debug_exit();
16970 		return err;
16971 	}
16972 
16973 	return 0;
16974 }
16975 
16976 static void __exit bnxt_exit(void)
16977 {
16978 	pci_unregister_driver(&bnxt_pci_driver);
16979 	if (bnxt_pf_wq)
16980 		destroy_workqueue(bnxt_pf_wq);
16981 	bnxt_debug_exit();
16982 }
16983 
16984 module_init(bnxt_init);
16985 module_exit(bnxt_exit);
16986