xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision a6fa4d6e38d6d57986957de990d1feab663ea3c7)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  * Copyright (c) 2016-2019 Broadcom Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  */
10 
11 #include <linux/module.h>
12 
13 #include <linux/stringify.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/pci.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/bitops.h>
27 #include <linux/io.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/byteorder.h>
31 #include <asm/page.h>
32 #include <linux/time.h>
33 #include <linux/mii.h>
34 #include <linux/mdio.h>
35 #include <linux/if.h>
36 #include <linux/if_vlan.h>
37 #include <linux/if_bridge.h>
38 #include <linux/rtc.h>
39 #include <linux/bpf.h>
40 #include <net/gro.h>
41 #include <net/ip.h>
42 #include <net/tcp.h>
43 #include <net/udp.h>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
46 #include <net/udp_tunnel.h>
47 #include <linux/workqueue.h>
48 #include <linux/prefetch.h>
49 #include <linux/cache.h>
50 #include <linux/log2.h>
51 #include <linux/bitmap.h>
52 #include <linux/cpu_rmap.h>
53 #include <linux/cpumask.h>
54 #include <net/pkt_cls.h>
55 #include <net/page_pool/helpers.h>
56 #include <linux/align.h>
57 #include <net/netdev_lock.h>
58 #include <net/netdev_queues.h>
59 #include <net/netdev_rx_queue.h>
60 #include <linux/pci-tph.h>
61 #include <linux/bnxt/hsi.h>
62 #include <linux/bnxt/ulp.h>
63 
64 #include "bnxt.h"
65 #include "bnxt_hwrm.h"
66 #include "bnxt_sriov.h"
67 #include "bnxt_ethtool.h"
68 #include "bnxt_dcb.h"
69 #include "bnxt_xdp.h"
70 #include "bnxt_ptp.h"
71 #include "bnxt_vfr.h"
72 #include "bnxt_tc.h"
73 #include "bnxt_devlink.h"
74 #include "bnxt_debugfs.h"
75 #include "bnxt_coredump.h"
76 #include "bnxt_hwmon.h"
77 #include "bnxt_gso.h"
78 #include <net/tso.h>
79 
80 #define BNXT_TX_TIMEOUT		(5 * HZ)
81 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
82 				 NETIF_MSG_TX_ERR)
83 
84 MODULE_IMPORT_NS("NETDEV_INTERNAL");
85 MODULE_LICENSE("GPL");
86 MODULE_DESCRIPTION("Broadcom NetXtreme network driver");
87 
88 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
89 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
90 
91 #define BNXT_TX_PUSH_THRESH 164
92 
93 /* indexed by enum board_idx */
94 static const struct {
95 	char *name;
96 } board_info[] = {
97 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
98 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
99 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
100 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
101 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
102 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
103 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
104 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
105 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
106 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
108 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
109 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
110 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
111 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
112 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
113 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
114 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
115 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
116 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
117 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
118 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
119 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
120 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
121 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
122 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
123 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
124 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
125 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
126 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
127 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
128 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
129 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
130 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
131 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
132 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
133 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
134 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
135 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
136 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
137 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
138 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
139 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
140 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
141 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
142 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
143 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
144 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
145 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
146 	[NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" },
147 	[NETXTREME_E_P7_VF_HV] = { "Broadcom BCM5760X Virtual Function for Hyper-V" },
148 };
149 
150 static const struct pci_device_id bnxt_pci_tbl[] = {
151 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
152 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
153 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
155 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
159 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
160 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
164 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
165 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
166 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
167 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
168 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
169 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
170 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
171 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
172 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
173 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
174 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
175 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
176 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
178 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
179 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
180 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
181 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
182 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
183 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
184 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
185 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
187 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
188 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
189 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
190 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
191 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
192 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
193 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
194 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
195 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
196 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
197 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
198 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
199 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
200 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
201 #ifdef CONFIG_BNXT_SRIOV
202 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
203 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
204 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
205 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
206 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
207 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
208 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
209 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
210 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
211 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
212 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
214 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
215 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
216 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
217 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
218 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
219 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
220 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
221 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
222 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
223 	{ PCI_VDEVICE(BROADCOM, 0x181b), .driver_data = NETXTREME_E_P7_VF_HV },
224 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
225 #endif
226 	{ 0 }
227 };
228 
229 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
230 
231 static const u16 bnxt_vf_req_snif[] = {
232 	HWRM_FUNC_CFG,
233 	HWRM_FUNC_VF_CFG,
234 	HWRM_PORT_PHY_QCFG,
235 	HWRM_CFA_L2_FILTER_ALLOC,
236 };
237 
238 static const u16 bnxt_async_events_arr[] = {
239 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
240 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
241 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
242 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
243 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
244 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
245 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
246 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
247 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
248 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
249 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
250 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
251 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
252 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
253 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
254 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
255 	ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER,
256 };
257 
258 const u16 bnxt_bstore_to_trace[] = {
259 	[BNXT_CTX_SRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT_TRACE,
260 	[BNXT_CTX_SRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT2_TRACE,
261 	[BNXT_CTX_CRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT_TRACE,
262 	[BNXT_CTX_CRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT2_TRACE,
263 	[BNXT_CTX_RIGP0]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP0_TRACE,
264 	[BNXT_CTX_L2HWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_L2_HWRM_TRACE,
265 	[BNXT_CTX_REHWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ROCE_HWRM_TRACE,
266 	[BNXT_CTX_CA0]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA0_TRACE,
267 	[BNXT_CTX_CA1]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA1_TRACE,
268 	[BNXT_CTX_CA2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA2_TRACE,
269 	[BNXT_CTX_RIGP1]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP1_TRACE,
270 	[BNXT_CTX_KONG]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_AFM_KONG_HWRM_TRACE,
271 	[BNXT_CTX_QPC]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ERR_QPC_TRACE,
272 };
273 
274 static struct workqueue_struct *bnxt_pf_wq;
275 
276 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
277 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
278 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
279 
280 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
281 	.ports = {
282 		.src = 0,
283 		.dst = 0,
284 	},
285 	.addrs = {
286 		.v6addrs = {
287 			.src = BNXT_IPV6_MASK_NONE,
288 			.dst = BNXT_IPV6_MASK_NONE,
289 		},
290 	},
291 };
292 
293 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
294 	.ports = {
295 		.src = cpu_to_be16(0xffff),
296 		.dst = cpu_to_be16(0xffff),
297 	},
298 	.addrs = {
299 		.v6addrs = {
300 			.src = BNXT_IPV6_MASK_ALL,
301 			.dst = BNXT_IPV6_MASK_ALL,
302 		},
303 	},
304 };
305 
306 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
307 	.ports = {
308 		.src = cpu_to_be16(0xffff),
309 		.dst = cpu_to_be16(0xffff),
310 	},
311 	.addrs = {
312 		.v4addrs = {
313 			.src = cpu_to_be32(0xffffffff),
314 			.dst = cpu_to_be32(0xffffffff),
315 		},
316 	},
317 };
318 
319 static bool bnxt_vf_pciid(enum board_idx idx)
320 {
321 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
322 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
323 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
324 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF ||
325 		idx == NETXTREME_E_P7_VF_HV);
326 }
327 
328 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
329 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
330 
331 #define BNXT_DB_CQ(db, idx)						\
332 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
333 
334 #define BNXT_DB_NQ_P5(db, idx)						\
335 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
336 		    (db)->doorbell)
337 
338 #define BNXT_DB_NQ_P7(db, idx)						\
339 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
340 		    DB_RING_IDX(db, idx), (db)->doorbell)
341 
342 #define BNXT_DB_CQ_ARM(db, idx)						\
343 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
344 
345 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
346 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
347 		    DB_RING_IDX(db, idx), (db)->doorbell)
348 
349 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
350 {
351 	if (bp->flags & BNXT_FLAG_CHIP_P7)
352 		BNXT_DB_NQ_P7(db, idx);
353 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
354 		BNXT_DB_NQ_P5(db, idx);
355 	else
356 		BNXT_DB_CQ(db, idx);
357 }
358 
359 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
360 {
361 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
362 		BNXT_DB_NQ_ARM_P5(db, idx);
363 	else
364 		BNXT_DB_CQ_ARM(db, idx);
365 }
366 
367 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
368 {
369 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
370 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
371 			    DB_RING_IDX(db, idx), db->doorbell);
372 	else
373 		BNXT_DB_CQ(db, idx);
374 }
375 
376 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
377 {
378 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
379 		return;
380 
381 	if (BNXT_PF(bp))
382 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
383 	else
384 		schedule_delayed_work(&bp->fw_reset_task, delay);
385 }
386 
387 static void __bnxt_queue_sp_work(struct bnxt *bp)
388 {
389 	if (BNXT_PF(bp))
390 		queue_work(bnxt_pf_wq, &bp->sp_task);
391 	else
392 		schedule_work(&bp->sp_task);
393 }
394 
395 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
396 {
397 	set_bit(event, &bp->sp_event);
398 	__bnxt_queue_sp_work(bp);
399 }
400 
401 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
402 {
403 	if (!rxr->bnapi->in_reset) {
404 		rxr->bnapi->in_reset = true;
405 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
406 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
407 		else
408 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
409 		__bnxt_queue_sp_work(bp);
410 	}
411 	rxr->rx_next_cons = 0xffff;
412 }
413 
414 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
415 			  u16 curr)
416 {
417 	struct bnxt_napi *bnapi = txr->bnapi;
418 
419 	if (bnapi->tx_fault)
420 		return;
421 
422 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
423 		   txr->txq_index, txr->tx_hw_cons,
424 		   txr->tx_cons, txr->tx_prod, curr);
425 	WARN_ON_ONCE(1);
426 	bnapi->tx_fault = 1;
427 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
428 }
429 
430 const u16 bnxt_lhint_arr[] = {
431 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
432 	TX_BD_FLAGS_LHINT_512_TO_1023,
433 	TX_BD_FLAGS_LHINT_1024_TO_2047,
434 	TX_BD_FLAGS_LHINT_1024_TO_2047,
435 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
436 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
437 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
438 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
439 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
440 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
441 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
442 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
443 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
444 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
445 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
446 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
447 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
448 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
449 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
450 };
451 
452 u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
453 {
454 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
455 
456 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
457 		return 0;
458 
459 	return md_dst->u.port_info.port_id;
460 }
461 
462 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
463 			     u16 prod)
464 {
465 	/* Sync BD data before updating doorbell */
466 	wmb();
467 	bnxt_db_write(bp, &txr->tx_db, prod);
468 	txr->kick_pending = 0;
469 }
470 
471 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
472 {
473 	struct bnxt *bp = netdev_priv(dev);
474 	struct tx_bd *txbd, *txbd0;
475 	struct tx_bd_ext *txbd1;
476 	struct netdev_queue *txq;
477 	int i;
478 	dma_addr_t mapping;
479 	unsigned int length, pad = 0;
480 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
481 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
482 	struct pci_dev *pdev = bp->pdev;
483 	u16 prod, last_frag, txts_prod;
484 	struct bnxt_tx_ring_info *txr;
485 	struct bnxt_sw_tx_bd *tx_buf;
486 	__le32 lflags = 0;
487 	skb_frag_t *frag;
488 
489 	i = skb_get_queue_mapping(skb);
490 	if (unlikely(i >= bp->tx_nr_rings)) {
491 		dev_kfree_skb_any(skb);
492 		dev_core_stats_tx_dropped_inc(dev);
493 		return NETDEV_TX_OK;
494 	}
495 
496 	txq = netdev_get_tx_queue(dev, i);
497 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
498 	prod = txr->tx_prod;
499 
500 #if (MAX_SKB_FRAGS > TX_MAX_FRAGS)
501 	if (skb_shinfo(skb)->nr_frags > TX_MAX_FRAGS) {
502 		netdev_warn_once(dev, "SKB has too many (%d) fragments, max supported is %d.  SKB will be linearized.\n",
503 				 skb_shinfo(skb)->nr_frags, TX_MAX_FRAGS);
504 		if (skb_linearize(skb)) {
505 			dev_kfree_skb_any(skb);
506 			dev_core_stats_tx_dropped_inc(dev);
507 			return NETDEV_TX_OK;
508 		}
509 	}
510 #endif
511 	if (skb_is_gso(skb) &&
512 	    (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) &&
513 	    !(bp->flags & BNXT_FLAG_UDP_GSO_CAP))
514 		return bnxt_sw_udp_gso_xmit(bp, txr, txq, skb);
515 
516 	free_size = bnxt_tx_avail(bp, txr);
517 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
518 		/* We must have raced with NAPI cleanup */
519 		if (net_ratelimit() && txr->kick_pending)
520 			netif_warn(bp, tx_err, dev,
521 				   "bnxt: ring busy w/ flush pending!\n");
522 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
523 					bp->tx_wake_thresh))
524 			return NETDEV_TX_BUSY;
525 	}
526 
527 	length = skb->len;
528 	len = skb_headlen(skb);
529 	last_frag = skb_shinfo(skb)->nr_frags;
530 
531 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
532 
533 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
534 	tx_buf->skb = skb;
535 	tx_buf->nr_frags = last_frag;
536 
537 	vlan_tag_flags = 0;
538 	cfa_action = bnxt_xmit_get_cfa_action(skb);
539 	if (skb_vlan_tag_present(skb)) {
540 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
541 				 skb_vlan_tag_get(skb);
542 		/* Currently supports 8021Q, 8021AD vlan offloads
543 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
544 		 */
545 		if (skb->vlan_proto == htons(ETH_P_8021Q))
546 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
547 	}
548 
549 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp &&
550 	    ptp->tx_tstamp_en) {
551 		if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) {
552 			lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
553 			tx_buf->is_ts_pkt = 1;
554 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
555 		} else if (!skb_is_gso(skb)) {
556 			u16 seq_id, hdr_off;
557 
558 			if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) &&
559 			    !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) {
560 				if (vlan_tag_flags)
561 					hdr_off += VLAN_HLEN;
562 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
563 				tx_buf->is_ts_pkt = 1;
564 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
565 
566 				ptp->txts_req[txts_prod].tx_seqid = seq_id;
567 				ptp->txts_req[txts_prod].tx_hdr_off = hdr_off;
568 				tx_buf->txts_prod = txts_prod;
569 			}
570 		}
571 	}
572 	if (unlikely(skb->no_fcs))
573 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
574 
575 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
576 	    skb_frags_readable(skb) && !lflags) {
577 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
578 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
579 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
580 		void __iomem *db = txr->tx_db.doorbell;
581 		void *pdata = tx_push_buf->data;
582 		u64 *end;
583 		int j, push_len;
584 
585 		/* Set COAL_NOW to be ready quickly for the next push */
586 		tx_push->tx_bd_len_flags_type =
587 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
588 					TX_BD_TYPE_LONG_TX_BD |
589 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
590 					TX_BD_FLAGS_COAL_NOW |
591 					TX_BD_FLAGS_PACKET_END |
592 					TX_BD_CNT(2));
593 
594 		if (skb->ip_summed == CHECKSUM_PARTIAL)
595 			tx_push1->tx_bd_hsize_lflags =
596 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
597 		else
598 			tx_push1->tx_bd_hsize_lflags = 0;
599 
600 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
601 		tx_push1->tx_bd_cfa_action =
602 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
603 
604 		end = pdata + length;
605 		end = PTR_ALIGN(end, 8) - 1;
606 		*end = 0;
607 
608 		skb_copy_from_linear_data(skb, pdata, len);
609 		pdata += len;
610 		for (j = 0; j < last_frag; j++) {
611 			void *fptr;
612 
613 			frag = &skb_shinfo(skb)->frags[j];
614 			fptr = skb_frag_address_safe(frag);
615 			if (!fptr)
616 				goto normal_tx;
617 
618 			memcpy(pdata, fptr, skb_frag_size(frag));
619 			pdata += skb_frag_size(frag);
620 		}
621 
622 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
623 		txbd->tx_bd_haddr = txr->data_mapping;
624 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
625 		prod = NEXT_TX(prod);
626 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
627 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
628 		memcpy(txbd, tx_push1, sizeof(*txbd));
629 		prod = NEXT_TX(prod);
630 		tx_push->doorbell =
631 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
632 				    DB_RING_IDX(&txr->tx_db, prod));
633 		WRITE_ONCE(txr->tx_prod, prod);
634 
635 		tx_buf->is_push = 1;
636 		netdev_tx_sent_queue(txq, skb->len);
637 		wmb();	/* Sync is_push and byte queue before pushing data */
638 
639 		push_len = (length + sizeof(*tx_push) + 7) / 8;
640 		if (push_len > 16) {
641 			__iowrite64_copy(db, tx_push_buf, 16);
642 			__iowrite32_copy(db + 4, tx_push_buf + 1,
643 					 (push_len - 16) << 1);
644 		} else {
645 			__iowrite64_copy(db, tx_push_buf, push_len);
646 		}
647 
648 		goto tx_done;
649 	}
650 
651 normal_tx:
652 	if (length < BNXT_MIN_PKT_SIZE) {
653 		pad = BNXT_MIN_PKT_SIZE - length;
654 		if (skb_pad(skb, pad))
655 			/* SKB already freed. */
656 			goto tx_kick_pending;
657 		length = BNXT_MIN_PKT_SIZE;
658 	}
659 
660 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
661 
662 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
663 		goto tx_free;
664 
665 	dma_unmap_addr_set(tx_buf, mapping, mapping);
666 	dma_unmap_len_set(tx_buf, len, len);
667 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
668 		TX_BD_CNT(last_frag + 2);
669 
670 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
671 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
672 
673 	prod = NEXT_TX(prod);
674 	txbd1 = bnxt_init_ext_bd(bp, txr, prod, lflags, vlan_tag_flags,
675 				 cfa_action);
676 
677 	if (skb_is_gso(skb)) {
678 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
679 		u32 hdr_len;
680 
681 		if (skb->encapsulation) {
682 			if (udp_gso)
683 				hdr_len = skb_inner_transport_offset(skb) +
684 					  sizeof(struct udphdr);
685 			else
686 				hdr_len = skb_inner_tcp_all_headers(skb);
687 		} else if (udp_gso) {
688 			hdr_len = skb_transport_offset(skb) +
689 				  sizeof(struct udphdr);
690 		} else {
691 			hdr_len = skb_tcp_all_headers(skb);
692 		}
693 
694 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
695 					TX_BD_FLAGS_T_IPID |
696 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
697 		length = skb_shinfo(skb)->gso_size;
698 		txbd1->tx_bd_mss = cpu_to_le32(length);
699 		length += hdr_len;
700 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
701 		txbd1->tx_bd_hsize_lflags |=
702 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
703 	}
704 
705 	length >>= 9;
706 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
707 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
708 				     skb->len);
709 		i = 0;
710 		goto tx_dma_error;
711 	}
712 	flags |= bnxt_lhint_arr[length];
713 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
714 
715 	txbd0 = txbd;
716 	for (i = 0; i < last_frag; i++) {
717 		frag = &skb_shinfo(skb)->frags[i];
718 		prod = NEXT_TX(prod);
719 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
720 
721 		len = skb_frag_size(frag);
722 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
723 					   DMA_TO_DEVICE);
724 
725 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
726 			goto tx_dma_error;
727 
728 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
729 		netmem_dma_unmap_addr_set(skb_frag_netmem(frag), tx_buf,
730 					  mapping, mapping);
731 		dma_unmap_len_set(tx_buf, len, len);
732 
733 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
734 
735 		flags = len << TX_BD_LEN_SHIFT;
736 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
737 	}
738 
739 	flags &= ~TX_BD_LEN;
740 	txbd->tx_bd_len_flags_type =
741 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
742 			    TX_BD_FLAGS_PACKET_END);
743 
744 	netdev_tx_sent_queue(txq, skb->len);
745 
746 	skb_tx_timestamp(skb);
747 
748 	prod = NEXT_TX(prod);
749 	WRITE_ONCE(txr->tx_prod, prod);
750 
751 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
752 		bnxt_txr_db_kick(bp, txr, prod);
753 	} else {
754 		if (free_size >= bp->tx_wake_thresh)
755 			txbd0->tx_bd_len_flags_type |=
756 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
757 		txr->kick_pending = 1;
758 	}
759 
760 tx_done:
761 
762 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
763 		if (netdev_xmit_more() && !tx_buf->is_push) {
764 			txbd0->tx_bd_len_flags_type &=
765 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
766 			bnxt_txr_db_kick(bp, txr, prod);
767 		}
768 
769 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
770 				   bp->tx_wake_thresh);
771 	}
772 	return NETDEV_TX_OK;
773 
774 tx_dma_error:
775 	last_frag = i;
776 
777 	/* start back at beginning and unmap skb */
778 	prod = txr->tx_prod;
779 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
780 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
781 			 skb_headlen(skb), DMA_TO_DEVICE);
782 	prod = NEXT_TX(prod);
783 
784 	/* unmap remaining mapped pages */
785 	for (i = 0; i < last_frag; i++) {
786 		prod = NEXT_TX(prod);
787 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
788 		frag = &skb_shinfo(skb)->frags[i];
789 		netmem_dma_unmap_page_attrs(&pdev->dev,
790 					    dma_unmap_addr(tx_buf, mapping),
791 					    skb_frag_size(frag),
792 					    DMA_TO_DEVICE, 0);
793 	}
794 
795 tx_free:
796 	dev_kfree_skb_any(skb);
797 tx_kick_pending:
798 	if (BNXT_TX_PTP_IS_SET(lflags)) {
799 		txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].is_ts_pkt = 0;
800 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
801 		if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
802 			/* set SKB to err so PTP worker will clean up */
803 			ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO);
804 	}
805 	if (txr->kick_pending)
806 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
807 	txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].skb = NULL;
808 	dev_core_stats_tx_dropped_inc(dev);
809 	return NETDEV_TX_OK;
810 }
811 
812 /* Returns true if some remaining TX packets not processed. */
813 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
814 			  int budget)
815 {
816 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
817 	struct pci_dev *pdev = bp->pdev;
818 	u16 hw_cons = txr->tx_hw_cons;
819 	unsigned int tx_bytes = 0;
820 	u16 cons = txr->tx_cons;
821 	unsigned int dma_len;
822 	dma_addr_t dma_addr;
823 	int tx_pkts = 0;
824 	bool rc = false;
825 
826 	while (RING_TX(bp, cons) != hw_cons) {
827 		struct bnxt_sw_tx_bd *tx_buf, *head_buf;
828 		struct sk_buff *skb;
829 		bool is_ts_pkt;
830 		int j, last;
831 
832 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
833 		head_buf = tx_buf;
834 		skb = tx_buf->skb;
835 
836 		if (unlikely(!skb)) {
837 			bnxt_sched_reset_txr(bp, txr, cons);
838 			return rc;
839 		}
840 
841 		is_ts_pkt = tx_buf->is_ts_pkt;
842 		if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) {
843 			rc = true;
844 			break;
845 		}
846 
847 		cons = NEXT_TX(cons);
848 		tx_pkts++;
849 		tx_bytes += skb->len;
850 		tx_buf->skb = NULL;
851 		tx_buf->is_ts_pkt = 0;
852 
853 		if (tx_buf->is_push) {
854 			tx_buf->is_push = 0;
855 			goto next_tx_int;
856 		}
857 
858 		if (dma_unmap_len(tx_buf, len)) {
859 			dma_addr = dma_unmap_addr(tx_buf, mapping);
860 			dma_len = dma_unmap_len(tx_buf, len);
861 
862 			dma_unmap_single(&pdev->dev, dma_addr, dma_len,
863 					 DMA_TO_DEVICE);
864 		}
865 
866 		last = tx_buf->nr_frags;
867 
868 		for (j = 0; j < last; j++) {
869 			cons = NEXT_TX(cons);
870 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
871 			if (dma_unmap_len(tx_buf, len)) {
872 				dma_addr = dma_unmap_addr(tx_buf, mapping);
873 				dma_len = dma_unmap_len(tx_buf, len);
874 
875 				netmem_dma_unmap_page_attrs(&pdev->dev,
876 							    dma_addr, dma_len,
877 							    DMA_TO_DEVICE, 0);
878 			}
879 		}
880 
881 		if (unlikely(head_buf->is_sw_gso)) {
882 			u16 inline_cons = txr->tx_inline_cons + 1;
883 
884 			WRITE_ONCE(txr->tx_inline_cons, inline_cons);
885 			if (head_buf->is_sw_gso == BNXT_SW_GSO_LAST) {
886 				tso_dma_map_complete(&pdev->dev,
887 						     &head_buf->sw_gso_cstate);
888 			} else {
889 				tx_pkts--;
890 				tx_bytes -= skb->len;
891 				skb = NULL;
892 			}
893 			head_buf->is_sw_gso = 0;
894 		}
895 
896 		if (unlikely(is_ts_pkt)) {
897 			if (BNXT_CHIP_P5(bp)) {
898 				/* PTP worker takes ownership of the skb */
899 				bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod);
900 				skb = NULL;
901 			}
902 		}
903 
904 next_tx_int:
905 		cons = NEXT_TX(cons);
906 
907 		napi_consume_skb(skb, budget);
908 	}
909 
910 	WRITE_ONCE(txr->tx_cons, cons);
911 
912 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
913 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
914 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
915 
916 	return rc;
917 }
918 
919 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
920 {
921 	struct bnxt_tx_ring_info *txr;
922 	bool more = false;
923 	int i;
924 
925 	bnxt_for_each_napi_tx(i, bnapi, txr) {
926 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
927 			more |= __bnxt_tx_int(bp, txr, budget);
928 	}
929 	if (!more)
930 		bnapi->events &= ~BNXT_TX_CMP_EVENT;
931 }
932 
933 static bool bnxt_separate_head_pool(struct bnxt_rx_ring_info *rxr)
934 {
935 	return rxr->need_head_pool || rxr->rx_page_size < PAGE_SIZE;
936 }
937 
938 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
939 					 struct bnxt_rx_ring_info *rxr,
940 					 unsigned int *offset,
941 					 gfp_t gfp)
942 {
943 	struct page *page;
944 
945 	if (rxr->rx_page_size < PAGE_SIZE) {
946 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
947 						rxr->rx_page_size);
948 	} else {
949 		page = page_pool_dev_alloc_pages(rxr->page_pool);
950 		*offset = 0;
951 	}
952 	if (!page)
953 		return NULL;
954 
955 	*mapping = page_pool_get_dma_addr(page) + *offset;
956 	return page;
957 }
958 
959 static netmem_ref __bnxt_alloc_rx_netmem(struct bnxt *bp, dma_addr_t *mapping,
960 					 struct bnxt_rx_ring_info *rxr,
961 					 unsigned int *offset,
962 					 gfp_t gfp)
963 {
964 	netmem_ref netmem;
965 
966 	if (rxr->rx_page_size < PAGE_SIZE) {
967 		netmem = page_pool_alloc_frag_netmem(rxr->page_pool, offset,
968 						     rxr->rx_page_size, gfp);
969 	} else {
970 		netmem = page_pool_alloc_netmems(rxr->page_pool, gfp);
971 		*offset = 0;
972 	}
973 	if (!netmem)
974 		return 0;
975 
976 	*mapping = page_pool_get_dma_addr_netmem(netmem) + *offset;
977 	return netmem;
978 }
979 
980 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
981 				       struct bnxt_rx_ring_info *rxr,
982 				       gfp_t gfp)
983 {
984 	unsigned int offset;
985 	struct page *page;
986 
987 	page = page_pool_alloc_frag(rxr->head_pool, &offset,
988 				    bp->rx_buf_size, gfp);
989 	if (!page)
990 		return NULL;
991 
992 	*mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset;
993 	return page_address(page) + offset;
994 }
995 
996 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
997 		       u16 prod, gfp_t gfp)
998 {
999 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1000 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1001 	dma_addr_t mapping;
1002 
1003 	if (BNXT_RX_PAGE_MODE(bp)) {
1004 		unsigned int offset;
1005 		struct page *page =
1006 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
1007 
1008 		if (!page)
1009 			return -ENOMEM;
1010 
1011 		mapping += bp->rx_dma_offset;
1012 		rx_buf->data = page;
1013 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
1014 		rx_buf->offset = offset;
1015 	} else {
1016 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp);
1017 
1018 		if (!data)
1019 			return -ENOMEM;
1020 
1021 		rx_buf->data = data;
1022 		rx_buf->data_ptr = data + bp->rx_offset;
1023 		rx_buf->offset = 0;
1024 	}
1025 	rx_buf->mapping = mapping;
1026 
1027 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1028 	return 0;
1029 }
1030 
1031 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
1032 {
1033 	u16 prod = rxr->rx_prod;
1034 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1035 	struct bnxt *bp = rxr->bnapi->bp;
1036 	struct rx_bd *cons_bd, *prod_bd;
1037 
1038 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1039 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1040 
1041 	prod_rx_buf->data = data;
1042 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
1043 
1044 	prod_rx_buf->mapping = cons_rx_buf->mapping;
1045 	prod_rx_buf->offset = cons_rx_buf->offset;
1046 
1047 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1048 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
1049 
1050 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
1051 }
1052 
1053 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1054 {
1055 	u16 next, max = rxr->rx_agg_bmap_size;
1056 
1057 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
1058 	if (next >= max)
1059 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
1060 	return next;
1061 }
1062 
1063 static int bnxt_alloc_rx_netmem(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1064 				u16 prod, gfp_t gfp)
1065 {
1066 	struct rx_bd *rxbd =
1067 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1068 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
1069 	u16 sw_prod = rxr->rx_sw_agg_prod;
1070 	unsigned int offset = 0;
1071 	dma_addr_t mapping;
1072 	netmem_ref netmem;
1073 
1074 	netmem = __bnxt_alloc_rx_netmem(bp, &mapping, rxr, &offset, gfp);
1075 	if (!netmem)
1076 		return -ENOMEM;
1077 
1078 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1079 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1080 
1081 	__set_bit(sw_prod, rxr->rx_agg_bmap);
1082 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
1083 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1084 
1085 	rx_agg_buf->netmem = netmem;
1086 	rx_agg_buf->offset = offset;
1087 	rx_agg_buf->mapping = mapping;
1088 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1089 	rxbd->rx_bd_opaque = sw_prod;
1090 	return 0;
1091 }
1092 
1093 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1094 				       struct bnxt_cp_ring_info *cpr,
1095 				       u16 cp_cons, u16 curr)
1096 {
1097 	struct rx_agg_cmp *agg;
1098 
1099 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1100 	agg = (struct rx_agg_cmp *)
1101 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1102 	return agg;
1103 }
1104 
1105 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1106 					      struct bnxt_rx_ring_info *rxr,
1107 					      u16 agg_id, u16 curr)
1108 {
1109 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1110 
1111 	return &tpa_info->agg_arr[curr];
1112 }
1113 
1114 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1115 				   u16 start, u32 agg_bufs, bool tpa)
1116 {
1117 	struct bnxt_napi *bnapi = cpr->bnapi;
1118 	struct bnxt *bp = bnapi->bp;
1119 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1120 	u16 prod = rxr->rx_agg_prod;
1121 	u16 sw_prod = rxr->rx_sw_agg_prod;
1122 	bool p5_tpa = false;
1123 	u32 i;
1124 
1125 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1126 		p5_tpa = true;
1127 
1128 	for (i = 0; i < agg_bufs; i++) {
1129 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1130 		struct rx_agg_cmp *agg;
1131 		struct rx_bd *prod_bd;
1132 		netmem_ref netmem;
1133 		u16 cons;
1134 
1135 		if (p5_tpa)
1136 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1137 		else
1138 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1139 		cons = agg->rx_agg_cmp_opaque;
1140 		__clear_bit(cons, rxr->rx_agg_bmap);
1141 
1142 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1143 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1144 
1145 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1146 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1147 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1148 
1149 		/* It is possible for sw_prod to be equal to cons, so
1150 		 * set cons_rx_buf->netmem to 0 first.
1151 		 */
1152 		netmem = cons_rx_buf->netmem;
1153 		cons_rx_buf->netmem = 0;
1154 		prod_rx_buf->netmem = netmem;
1155 		prod_rx_buf->offset = cons_rx_buf->offset;
1156 
1157 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1158 
1159 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1160 
1161 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1162 		prod_bd->rx_bd_opaque = sw_prod;
1163 
1164 		prod = NEXT_RX_AGG(prod);
1165 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1166 	}
1167 	rxr->rx_agg_prod = prod;
1168 	rxr->rx_sw_agg_prod = sw_prod;
1169 }
1170 
1171 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1172 					      struct bnxt_rx_ring_info *rxr,
1173 					      u16 cons, void *data, u8 *data_ptr,
1174 					      dma_addr_t dma_addr,
1175 					      unsigned int offset_and_len)
1176 {
1177 	unsigned int len = offset_and_len & 0xffff;
1178 	struct page *page = data;
1179 	u16 prod = rxr->rx_prod;
1180 	struct sk_buff *skb;
1181 	void *frag_start;
1182 	int err;
1183 
1184 	frag_start = page_address(page) + rxr->rx_buf_ring[cons].offset;
1185 
1186 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1187 	if (unlikely(err)) {
1188 		bnxt_reuse_rx_data(rxr, cons, data);
1189 		return NULL;
1190 	}
1191 	dma_addr -= bp->rx_dma_offset;
1192 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, rxr->rx_page_size,
1193 				bp->rx_dir);
1194 	skb = napi_build_skb(frag_start, rxr->rx_page_size);
1195 	if (!skb) {
1196 		page_pool_recycle_direct(rxr->page_pool, page);
1197 		return NULL;
1198 	}
1199 	skb_mark_for_recycle(skb);
1200 	skb_reserve(skb, data_ptr - (u8 *)frag_start);
1201 	__skb_put(skb, len);
1202 
1203 	return skb;
1204 }
1205 
1206 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1207 					struct bnxt_rx_ring_info *rxr,
1208 					u16 cons, void *data, u8 *data_ptr,
1209 					dma_addr_t dma_addr,
1210 					unsigned int offset_and_len)
1211 {
1212 	unsigned int payload = offset_and_len >> 16;
1213 	unsigned int len = offset_and_len & 0xffff;
1214 	skb_frag_t *frag;
1215 	struct page *page = data;
1216 	u16 prod = rxr->rx_prod;
1217 	struct sk_buff *skb;
1218 	int off, err;
1219 
1220 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1221 	if (unlikely(err)) {
1222 		bnxt_reuse_rx_data(rxr, cons, data);
1223 		return NULL;
1224 	}
1225 	dma_addr -= bp->rx_dma_offset;
1226 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, rxr->rx_page_size,
1227 				bp->rx_dir);
1228 
1229 	if (unlikely(!payload))
1230 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1231 
1232 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1233 	if (!skb) {
1234 		page_pool_recycle_direct(rxr->page_pool, page);
1235 		return NULL;
1236 	}
1237 
1238 	skb_mark_for_recycle(skb);
1239 	off = (void *)data_ptr - page_address(page);
1240 	skb_add_rx_frag(skb, 0, page, off, len, rxr->rx_page_size);
1241 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1242 	       payload + NET_IP_ALIGN);
1243 
1244 	frag = &skb_shinfo(skb)->frags[0];
1245 	skb_frag_size_sub(frag, payload);
1246 	skb_frag_off_add(frag, payload);
1247 	skb->data_len -= payload;
1248 	skb->tail += payload;
1249 
1250 	return skb;
1251 }
1252 
1253 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1254 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1255 				   void *data, u8 *data_ptr,
1256 				   dma_addr_t dma_addr,
1257 				   unsigned int offset_and_len)
1258 {
1259 	u16 prod = rxr->rx_prod;
1260 	struct sk_buff *skb;
1261 	int err;
1262 
1263 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1264 	if (unlikely(err)) {
1265 		bnxt_reuse_rx_data(rxr, cons, data);
1266 		return NULL;
1267 	}
1268 
1269 	skb = napi_build_skb(data, bp->rx_buf_size);
1270 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1271 				bp->rx_dir);
1272 	if (!skb) {
1273 		page_pool_free_va(rxr->head_pool, data, true);
1274 		return NULL;
1275 	}
1276 
1277 	skb_mark_for_recycle(skb);
1278 	skb_reserve(skb, bp->rx_offset);
1279 	skb_put(skb, offset_and_len & 0xffff);
1280 	return skb;
1281 }
1282 
1283 static u32 __bnxt_rx_agg_netmems(struct bnxt *bp,
1284 				 struct bnxt_cp_ring_info *cpr,
1285 				 u16 idx, u32 agg_bufs, bool tpa,
1286 				 struct sk_buff *skb,
1287 				 struct xdp_buff *xdp)
1288 {
1289 	struct bnxt_napi *bnapi = cpr->bnapi;
1290 	struct skb_shared_info *shinfo;
1291 	struct bnxt_rx_ring_info *rxr;
1292 	u32 i, total_frag_len = 0;
1293 	bool p5_tpa = false;
1294 	u16 prod;
1295 
1296 	rxr = bnapi->rx_ring;
1297 	prod = rxr->rx_agg_prod;
1298 
1299 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1300 		p5_tpa = true;
1301 
1302 	if (skb)
1303 		shinfo = skb_shinfo(skb);
1304 	else
1305 		shinfo = xdp_get_shared_info_from_buff(xdp);
1306 
1307 	for (i = 0; i < agg_bufs; i++) {
1308 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1309 		struct rx_agg_cmp *agg;
1310 		u16 cons, frag_len;
1311 		netmem_ref netmem;
1312 
1313 		if (p5_tpa)
1314 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1315 		else
1316 			agg = bnxt_get_agg(bp, cpr, idx, i);
1317 		cons = agg->rx_agg_cmp_opaque;
1318 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1319 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1320 
1321 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1322 		if (skb) {
1323 			skb_add_rx_frag_netmem(skb, i, cons_rx_buf->netmem,
1324 					       cons_rx_buf->offset,
1325 					       frag_len, rxr->rx_page_size);
1326 		} else {
1327 			skb_frag_t *frag = &shinfo->frags[i];
1328 
1329 			skb_frag_fill_netmem_desc(frag, cons_rx_buf->netmem,
1330 						  cons_rx_buf->offset,
1331 						  frag_len);
1332 			shinfo->nr_frags = i + 1;
1333 		}
1334 		__clear_bit(cons, rxr->rx_agg_bmap);
1335 
1336 		/* It is possible for bnxt_alloc_rx_netmem() to allocate
1337 		 * a sw_prod index that equals the cons index, so we
1338 		 * need to clear the cons entry now.
1339 		 */
1340 		netmem = cons_rx_buf->netmem;
1341 		cons_rx_buf->netmem = 0;
1342 
1343 		if (xdp && netmem_is_pfmemalloc(netmem))
1344 			xdp_buff_set_frag_pfmemalloc(xdp);
1345 
1346 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_ATOMIC) != 0) {
1347 			if (skb) {
1348 				skb->len -= frag_len;
1349 				skb->data_len -= frag_len;
1350 				skb->truesize -= rxr->rx_page_size;
1351 			}
1352 
1353 			--shinfo->nr_frags;
1354 			cons_rx_buf->netmem = netmem;
1355 
1356 			/* Update prod since possibly some netmems have been
1357 			 * allocated already.
1358 			 */
1359 			rxr->rx_agg_prod = prod;
1360 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1361 			return 0;
1362 		}
1363 
1364 		page_pool_dma_sync_netmem_for_cpu(rxr->page_pool, netmem, 0,
1365 						  rxr->rx_page_size);
1366 
1367 		total_frag_len += frag_len;
1368 		prod = NEXT_RX_AGG(prod);
1369 	}
1370 	rxr->rx_agg_prod = prod;
1371 	return total_frag_len;
1372 }
1373 
1374 static struct sk_buff *bnxt_rx_agg_netmems_skb(struct bnxt *bp,
1375 					       struct bnxt_cp_ring_info *cpr,
1376 					       struct sk_buff *skb, u16 idx,
1377 					       u32 agg_bufs, bool tpa)
1378 {
1379 	u32 total_frag_len = 0;
1380 
1381 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1382 					       skb, NULL);
1383 	if (!total_frag_len) {
1384 		skb_mark_for_recycle(skb);
1385 		dev_kfree_skb(skb);
1386 		return NULL;
1387 	}
1388 
1389 	return skb;
1390 }
1391 
1392 static u32 bnxt_rx_agg_netmems_xdp(struct bnxt *bp,
1393 				   struct bnxt_cp_ring_info *cpr,
1394 				   struct xdp_buff *xdp, u16 idx,
1395 				   u32 agg_bufs, bool tpa)
1396 {
1397 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1398 	u32 total_frag_len = 0;
1399 
1400 	if (!xdp_buff_has_frags(xdp))
1401 		shinfo->nr_frags = 0;
1402 
1403 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1404 					       NULL, xdp);
1405 	if (total_frag_len) {
1406 		xdp_buff_set_frags_flag(xdp);
1407 		shinfo->nr_frags = agg_bufs;
1408 		shinfo->xdp_frags_size = total_frag_len;
1409 	}
1410 	return total_frag_len;
1411 }
1412 
1413 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1414 			       u8 agg_bufs, u32 *raw_cons)
1415 {
1416 	u16 last;
1417 	struct rx_agg_cmp *agg;
1418 
1419 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1420 	last = RING_CMP(*raw_cons);
1421 	agg = (struct rx_agg_cmp *)
1422 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1423 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1424 }
1425 
1426 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1427 				      unsigned int len,
1428 				      dma_addr_t mapping)
1429 {
1430 	struct bnxt *bp = bnapi->bp;
1431 	struct pci_dev *pdev = bp->pdev;
1432 	struct sk_buff *skb;
1433 
1434 	skb = napi_alloc_skb(&bnapi->napi, len);
1435 	if (!skb)
1436 		return NULL;
1437 
1438 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copybreak,
1439 				bp->rx_dir);
1440 
1441 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1442 	       len + NET_IP_ALIGN);
1443 
1444 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copybreak,
1445 				   bp->rx_dir);
1446 
1447 	skb_put(skb, len);
1448 
1449 	return skb;
1450 }
1451 
1452 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1453 				     unsigned int len,
1454 				     dma_addr_t mapping)
1455 {
1456 	return bnxt_copy_data(bnapi, data, len, mapping);
1457 }
1458 
1459 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1460 				     struct xdp_buff *xdp,
1461 				     unsigned int len,
1462 				     dma_addr_t mapping)
1463 {
1464 	unsigned int metasize = 0;
1465 	u8 *data = xdp->data;
1466 	struct sk_buff *skb;
1467 
1468 	len = xdp->data_end - xdp->data_meta;
1469 	metasize = xdp->data - xdp->data_meta;
1470 	data = xdp->data_meta;
1471 
1472 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1473 	if (!skb)
1474 		return skb;
1475 
1476 	if (metasize) {
1477 		skb_metadata_set(skb, metasize);
1478 		__skb_pull(skb, metasize);
1479 	}
1480 
1481 	return skb;
1482 }
1483 
1484 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1485 			   u32 *raw_cons, void *cmp)
1486 {
1487 	struct rx_cmp *rxcmp = cmp;
1488 	u32 tmp_raw_cons = *raw_cons;
1489 	u8 cmp_type, agg_bufs = 0;
1490 
1491 	cmp_type = RX_CMP_TYPE(rxcmp);
1492 
1493 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1494 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1495 			    RX_CMP_AGG_BUFS) >>
1496 			   RX_CMP_AGG_BUFS_SHIFT;
1497 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1498 		struct rx_tpa_end_cmp *tpa_end = cmp;
1499 
1500 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1501 			return 0;
1502 
1503 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1504 	}
1505 
1506 	if (agg_bufs) {
1507 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1508 			return -EBUSY;
1509 	}
1510 	*raw_cons = tmp_raw_cons;
1511 	return 0;
1512 }
1513 
1514 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1515 {
1516 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1517 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1518 
1519 	if (test_bit(idx, map->agg_idx_bmap)) {
1520 		idx = find_first_zero_bit(map->agg_idx_bmap, MAX_TPA_P5);
1521 		if (idx >= MAX_TPA_P5)
1522 			return INVALID_HW_RING_ID;
1523 	}
1524 	__set_bit(idx, map->agg_idx_bmap);
1525 	map->agg_id_tbl[agg_id] = idx;
1526 	return idx;
1527 }
1528 
1529 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1530 {
1531 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1532 
1533 	__clear_bit(idx, map->agg_idx_bmap);
1534 }
1535 
1536 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1537 {
1538 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1539 
1540 	return map->agg_id_tbl[agg_id];
1541 }
1542 
1543 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1544 			      struct rx_tpa_start_cmp *tpa_start,
1545 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1546 {
1547 	tpa_info->cfa_code_valid = 1;
1548 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1549 	tpa_info->vlan_valid = 0;
1550 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1551 		tpa_info->vlan_valid = 1;
1552 		tpa_info->metadata =
1553 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1554 	}
1555 }
1556 
1557 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1558 				 struct rx_tpa_start_cmp *tpa_start,
1559 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1560 {
1561 	tpa_info->vlan_valid = 0;
1562 	if (TPA_START_VLAN_VALID(tpa_start)) {
1563 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1564 		u32 vlan_proto = ETH_P_8021Q;
1565 
1566 		tpa_info->vlan_valid = 1;
1567 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1568 			vlan_proto = ETH_P_8021AD;
1569 		tpa_info->metadata = vlan_proto << 16 |
1570 				     TPA_START_METADATA0_TCI(tpa_start1);
1571 	}
1572 }
1573 
1574 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1575 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1576 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1577 {
1578 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1579 	struct bnxt_tpa_info *tpa_info;
1580 	u16 cons, prod, agg_id;
1581 	struct rx_bd *prod_bd;
1582 	dma_addr_t mapping;
1583 
1584 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1585 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1586 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1587 		if (unlikely(agg_id == INVALID_HW_RING_ID)) {
1588 			netdev_warn(bp->dev, "Unable to allocate agg ID for ring %d, agg 0x%x\n",
1589 				    rxr->bnapi->index,
1590 				    TPA_START_AGG_ID_P5(tpa_start));
1591 			bnxt_sched_reset_rxr(bp, rxr);
1592 			return;
1593 		}
1594 	} else {
1595 		agg_id = TPA_START_AGG_ID(tpa_start);
1596 	}
1597 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1598 	prod = rxr->rx_prod;
1599 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1600 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1601 	tpa_info = &rxr->rx_tpa[agg_id];
1602 
1603 	if (unlikely(cons != rxr->rx_next_cons ||
1604 		     TPA_START_ERROR(tpa_start))) {
1605 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1606 			    cons, rxr->rx_next_cons,
1607 			    TPA_START_ERROR_CODE(tpa_start1));
1608 		bnxt_sched_reset_rxr(bp, rxr);
1609 		return;
1610 	}
1611 	prod_rx_buf->data = tpa_info->data;
1612 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1613 
1614 	mapping = tpa_info->mapping;
1615 	prod_rx_buf->mapping = mapping;
1616 
1617 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1618 
1619 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1620 
1621 	tpa_info->data = cons_rx_buf->data;
1622 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1623 	cons_rx_buf->data = NULL;
1624 	tpa_info->mapping = cons_rx_buf->mapping;
1625 
1626 	tpa_info->len =
1627 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1628 				RX_TPA_START_CMP_LEN_SHIFT;
1629 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1630 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1631 		tpa_info->gso_type = SKB_GSO_TCPV4;
1632 		if (TPA_START_IS_IPV6(tpa_start1))
1633 			tpa_info->gso_type = SKB_GSO_TCPV6;
1634 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1635 		else if (!BNXT_CHIP_P4_PLUS(bp) &&
1636 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1637 			tpa_info->gso_type = SKB_GSO_TCPV6;
1638 		tpa_info->rss_hash =
1639 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1640 	} else {
1641 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1642 		tpa_info->gso_type = 0;
1643 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1644 	}
1645 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1646 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1647 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1648 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1649 	else
1650 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1651 	tpa_info->agg_count = 0;
1652 
1653 	rxr->rx_prod = NEXT_RX(prod);
1654 	cons = RING_RX(bp, NEXT_RX(cons));
1655 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1656 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1657 
1658 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1659 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1660 	cons_rx_buf->data = NULL;
1661 }
1662 
1663 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1664 {
1665 	if (agg_bufs)
1666 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1667 }
1668 
1669 #ifdef CONFIG_INET
1670 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1671 {
1672 	struct udphdr *uh = NULL;
1673 
1674 	if (ip_proto == htons(ETH_P_IP)) {
1675 		struct iphdr *iph = (struct iphdr *)skb->data;
1676 
1677 		if (iph->protocol == IPPROTO_UDP)
1678 			uh = (struct udphdr *)(iph + 1);
1679 	} else {
1680 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1681 
1682 		if (iph->nexthdr == IPPROTO_UDP)
1683 			uh = (struct udphdr *)(iph + 1);
1684 	}
1685 	if (uh) {
1686 		if (uh->check)
1687 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1688 		else
1689 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1690 	}
1691 }
1692 #endif
1693 
1694 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1695 					   int payload_off, int tcp_ts,
1696 					   struct sk_buff *skb)
1697 {
1698 #ifdef CONFIG_INET
1699 	struct tcphdr *th;
1700 	int len, nw_off;
1701 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1702 	u32 hdr_info = tpa_info->hdr_info;
1703 	bool loopback = false;
1704 
1705 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1706 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1707 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1708 
1709 	/* If the packet is an internal loopback packet, the offsets will
1710 	 * have an extra 4 bytes.
1711 	 */
1712 	if (inner_mac_off == 4) {
1713 		loopback = true;
1714 	} else if (inner_mac_off > 4) {
1715 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1716 					    ETH_HLEN - 2));
1717 
1718 		/* We only support inner iPv4/ipv6.  If we don't see the
1719 		 * correct protocol ID, it must be a loopback packet where
1720 		 * the offsets are off by 4.
1721 		 */
1722 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1723 			loopback = true;
1724 	}
1725 	if (loopback) {
1726 		/* internal loopback packet, subtract all offsets by 4 */
1727 		inner_ip_off -= 4;
1728 		inner_mac_off -= 4;
1729 		outer_ip_off -= 4;
1730 	}
1731 
1732 	nw_off = inner_ip_off - ETH_HLEN;
1733 	skb_set_network_header(skb, nw_off);
1734 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1735 		struct ipv6hdr *iph = ipv6_hdr(skb);
1736 
1737 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1738 		len = skb->len - skb_transport_offset(skb);
1739 		th = tcp_hdr(skb);
1740 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1741 	} else {
1742 		struct iphdr *iph = ip_hdr(skb);
1743 
1744 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1745 		len = skb->len - skb_transport_offset(skb);
1746 		th = tcp_hdr(skb);
1747 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1748 	}
1749 
1750 	if (inner_mac_off) { /* tunnel */
1751 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1752 					    ETH_HLEN - 2));
1753 
1754 		bnxt_gro_tunnel(skb, proto);
1755 	}
1756 #endif
1757 	return skb;
1758 }
1759 
1760 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1761 					   int payload_off, int tcp_ts,
1762 					   struct sk_buff *skb)
1763 {
1764 #ifdef CONFIG_INET
1765 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1766 	u32 hdr_info = tpa_info->hdr_info;
1767 	int iphdr_len, nw_off;
1768 
1769 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1770 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1771 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1772 
1773 	nw_off = inner_ip_off - ETH_HLEN;
1774 	skb_set_network_header(skb, nw_off);
1775 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1776 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1777 	skb_set_transport_header(skb, nw_off + iphdr_len);
1778 
1779 	if (inner_mac_off) { /* tunnel */
1780 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1781 					    ETH_HLEN - 2));
1782 
1783 		bnxt_gro_tunnel(skb, proto);
1784 	}
1785 #endif
1786 	return skb;
1787 }
1788 
1789 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1790 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1791 
1792 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1793 					   int payload_off, int tcp_ts,
1794 					   struct sk_buff *skb)
1795 {
1796 #ifdef CONFIG_INET
1797 	struct tcphdr *th;
1798 	int len, nw_off, tcp_opt_len = 0;
1799 
1800 	if (tcp_ts)
1801 		tcp_opt_len = 12;
1802 
1803 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1804 		struct iphdr *iph;
1805 
1806 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1807 			 ETH_HLEN;
1808 		skb_set_network_header(skb, nw_off);
1809 		iph = ip_hdr(skb);
1810 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1811 		len = skb->len - skb_transport_offset(skb);
1812 		th = tcp_hdr(skb);
1813 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1814 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1815 		struct ipv6hdr *iph;
1816 
1817 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1818 			 ETH_HLEN;
1819 		skb_set_network_header(skb, nw_off);
1820 		iph = ipv6_hdr(skb);
1821 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1822 		len = skb->len - skb_transport_offset(skb);
1823 		th = tcp_hdr(skb);
1824 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1825 	} else {
1826 		dev_kfree_skb_any(skb);
1827 		return NULL;
1828 	}
1829 
1830 	if (nw_off) /* tunnel */
1831 		bnxt_gro_tunnel(skb, skb->protocol);
1832 #endif
1833 	return skb;
1834 }
1835 
1836 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1837 					   struct bnxt_tpa_info *tpa_info,
1838 					   struct rx_tpa_end_cmp *tpa_end,
1839 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1840 					   struct sk_buff *skb,
1841 					   struct bnxt_rx_sw_stats *rx_stats)
1842 {
1843 #ifdef CONFIG_INET
1844 	int payload_off;
1845 	u16 segs;
1846 
1847 	segs = TPA_END_TPA_SEGS(tpa_end);
1848 	if (segs == 1)
1849 		return skb;
1850 
1851 	rx_stats->rx_hw_gro_packets++;
1852 	rx_stats->rx_hw_gro_wire_packets += segs;
1853 
1854 	NAPI_GRO_CB(skb)->count = segs;
1855 	skb_shinfo(skb)->gso_size =
1856 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1857 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1858 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1859 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1860 	else
1861 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1862 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1863 	if (likely(skb))
1864 		tcp_gro_complete(skb);
1865 #endif
1866 	return skb;
1867 }
1868 
1869 /* Given the cfa_code of a received packet determine which
1870  * netdev (vf-rep or PF) the packet is destined to.
1871  */
1872 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1873 {
1874 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1875 
1876 	/* if vf-rep dev is NULL, it must belong to the PF */
1877 	return dev ? dev : bp->dev;
1878 }
1879 
1880 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1881 					   struct bnxt_cp_ring_info *cpr,
1882 					   u32 *raw_cons,
1883 					   struct rx_tpa_end_cmp *tpa_end,
1884 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1885 					   u8 *event)
1886 {
1887 	struct bnxt_napi *bnapi = cpr->bnapi;
1888 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1889 	struct net_device *dev = bp->dev;
1890 	u8 *data_ptr, agg_bufs;
1891 	unsigned int len;
1892 	struct bnxt_tpa_info *tpa_info;
1893 	dma_addr_t mapping;
1894 	struct sk_buff *skb;
1895 	u16 idx = 0, agg_id;
1896 	void *data;
1897 	bool gro;
1898 
1899 	if (unlikely(bnapi->in_reset)) {
1900 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1901 
1902 		if (rc < 0)
1903 			return ERR_PTR(-EBUSY);
1904 		return NULL;
1905 	}
1906 
1907 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1908 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1909 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1910 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1911 		tpa_info = &rxr->rx_tpa[agg_id];
1912 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1913 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1914 				    agg_bufs, tpa_info->agg_count);
1915 			agg_bufs = tpa_info->agg_count;
1916 		}
1917 		tpa_info->agg_count = 0;
1918 		*event |= BNXT_AGG_EVENT;
1919 		bnxt_free_agg_idx(rxr, agg_id);
1920 		idx = agg_id;
1921 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1922 	} else {
1923 		agg_id = TPA_END_AGG_ID(tpa_end);
1924 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1925 		tpa_info = &rxr->rx_tpa[agg_id];
1926 		idx = RING_CMP(*raw_cons);
1927 		if (agg_bufs) {
1928 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1929 				return ERR_PTR(-EBUSY);
1930 
1931 			*event |= BNXT_AGG_EVENT;
1932 			idx = NEXT_CMP(idx);
1933 		}
1934 		gro = !!TPA_END_GRO(tpa_end);
1935 	}
1936 	data = tpa_info->data;
1937 	data_ptr = tpa_info->data_ptr;
1938 	prefetch(data_ptr);
1939 	len = tpa_info->len;
1940 	mapping = tpa_info->mapping;
1941 
1942 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1943 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1944 		if (agg_bufs > MAX_SKB_FRAGS)
1945 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1946 				    agg_bufs, (int)MAX_SKB_FRAGS);
1947 		return NULL;
1948 	}
1949 
1950 	if (len <= bp->rx_copybreak) {
1951 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1952 		if (!skb) {
1953 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1954 			cpr->sw_stats->rx.rx_oom_discards += 1;
1955 			return NULL;
1956 		}
1957 	} else {
1958 		u8 *new_data;
1959 		dma_addr_t new_mapping;
1960 
1961 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr,
1962 						GFP_ATOMIC);
1963 		if (!new_data) {
1964 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1965 			cpr->sw_stats->rx.rx_oom_discards += 1;
1966 			return NULL;
1967 		}
1968 
1969 		tpa_info->data = new_data;
1970 		tpa_info->data_ptr = new_data + bp->rx_offset;
1971 		tpa_info->mapping = new_mapping;
1972 
1973 		skb = napi_build_skb(data, bp->rx_buf_size);
1974 		dma_sync_single_for_cpu(&bp->pdev->dev, mapping,
1975 					bp->rx_buf_use_size, bp->rx_dir);
1976 
1977 		if (!skb) {
1978 			page_pool_free_va(rxr->head_pool, data, true);
1979 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1980 			cpr->sw_stats->rx.rx_oom_discards += 1;
1981 			return NULL;
1982 		}
1983 		skb_mark_for_recycle(skb);
1984 		skb_reserve(skb, bp->rx_offset);
1985 		skb_put(skb, len);
1986 	}
1987 
1988 	if (agg_bufs) {
1989 		skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, idx, agg_bufs,
1990 					      true);
1991 		if (!skb) {
1992 			/* Page reuse already handled by bnxt_rx_pages(). */
1993 			cpr->sw_stats->rx.rx_oom_discards += 1;
1994 			return NULL;
1995 		}
1996 	}
1997 
1998 	if (tpa_info->cfa_code_valid)
1999 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
2000 	skb->protocol = eth_type_trans(skb, dev);
2001 
2002 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
2003 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
2004 
2005 	if (tpa_info->vlan_valid &&
2006 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
2007 		__be16 vlan_proto = htons(tpa_info->metadata >>
2008 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
2009 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
2010 
2011 		if (eth_type_vlan(vlan_proto)) {
2012 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2013 		} else {
2014 			dev_kfree_skb(skb);
2015 			return NULL;
2016 		}
2017 	}
2018 
2019 	skb_checksum_none_assert(skb);
2020 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
2021 		skb->ip_summed = CHECKSUM_UNNECESSARY;
2022 		skb->csum_level =
2023 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
2024 	}
2025 
2026 	if (gro)
2027 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb,
2028 				   &cpr->sw_stats->rx);
2029 
2030 	return skb;
2031 }
2032 
2033 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
2034 			 struct rx_agg_cmp *rx_agg)
2035 {
2036 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
2037 	struct bnxt_tpa_info *tpa_info;
2038 
2039 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
2040 	tpa_info = &rxr->rx_tpa[agg_id];
2041 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
2042 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
2043 }
2044 
2045 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
2046 			     struct sk_buff *skb)
2047 {
2048 	skb_mark_for_recycle(skb);
2049 
2050 	if (skb->dev != bp->dev) {
2051 		/* this packet belongs to a vf-rep */
2052 		bnxt_vf_rep_rx(bp, skb);
2053 		return;
2054 	}
2055 	skb_record_rx_queue(skb, bnapi->index);
2056 	napi_gro_receive(&bnapi->napi, skb);
2057 }
2058 
2059 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
2060 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
2061 {
2062 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
2063 
2064 	if (BNXT_PTP_RX_TS_VALID(flags))
2065 		goto ts_valid;
2066 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
2067 		return false;
2068 
2069 ts_valid:
2070 	*cmpl_ts = ts;
2071 	return true;
2072 }
2073 
2074 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
2075 				    struct rx_cmp *rxcmp,
2076 				    struct rx_cmp_ext *rxcmp1)
2077 {
2078 	__be16 vlan_proto;
2079 	u16 vtag;
2080 
2081 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
2082 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
2083 		u32 meta_data;
2084 
2085 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
2086 			return skb;
2087 
2088 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
2089 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
2090 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
2091 		if (eth_type_vlan(vlan_proto))
2092 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2093 		else
2094 			goto vlan_err;
2095 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2096 		if (RX_CMP_VLAN_VALID(rxcmp)) {
2097 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
2098 
2099 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
2100 				vlan_proto = htons(ETH_P_8021Q);
2101 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
2102 				vlan_proto = htons(ETH_P_8021AD);
2103 			else
2104 				goto vlan_err;
2105 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
2106 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2107 		}
2108 	}
2109 	return skb;
2110 vlan_err:
2111 	skb_mark_for_recycle(skb);
2112 	dev_kfree_skb(skb);
2113 	return NULL;
2114 }
2115 
2116 /* returns the following:
2117  * 1       - 1 packet successfully received
2118  * 0       - successful TPA_START, packet not completed yet
2119  * -EBUSY  - completion ring does not have all the agg buffers yet
2120  * -ENOMEM - packet aborted due to out of memory
2121  * -EIO    - packet aborted due to hw error indicated in BD
2122  */
2123 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2124 		       u32 *raw_cons, u8 *event)
2125 {
2126 	struct bnxt_napi *bnapi = cpr->bnapi;
2127 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2128 	struct net_device *dev = bp->dev;
2129 	struct rx_cmp *rxcmp;
2130 	struct rx_cmp_ext *rxcmp1;
2131 	u32 tmp_raw_cons = *raw_cons;
2132 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2133 	struct skb_shared_info *sinfo;
2134 	struct bnxt_xdp_buff bnxt_xdp;
2135 	struct bnxt_sw_rx_bd *rx_buf;
2136 	unsigned int len;
2137 	u8 *data_ptr, agg_bufs, cmp_type;
2138 	bool xdp_active = false;
2139 	dma_addr_t dma_addr;
2140 	struct sk_buff *skb;
2141 	u32 flags, misc;
2142 	u32 cmpl_ts;
2143 	void *data;
2144 	int rc = 0;
2145 
2146 	rxcmp = (struct rx_cmp *)
2147 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2148 
2149 	cmp_type = RX_CMP_TYPE(rxcmp);
2150 
2151 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2152 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2153 		goto next_rx_no_prod_no_len;
2154 	}
2155 
2156 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2157 	cp_cons = RING_CMP(tmp_raw_cons);
2158 	rxcmp1 = (struct rx_cmp_ext *)
2159 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2160 
2161 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2162 		return -EBUSY;
2163 
2164 	/* The valid test of the entry must be done first before
2165 	 * reading any further.
2166 	 */
2167 	dma_rmb();
2168 	prod = rxr->rx_prod;
2169 
2170 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2171 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2172 		bnxt_tpa_start(bp, rxr, cmp_type,
2173 			       (struct rx_tpa_start_cmp *)rxcmp,
2174 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2175 
2176 		*event |= BNXT_RX_EVENT;
2177 		goto next_rx_no_prod_no_len;
2178 
2179 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2180 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2181 				   (struct rx_tpa_end_cmp *)rxcmp,
2182 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2183 
2184 		if (IS_ERR(skb))
2185 			return -EBUSY;
2186 
2187 		rc = -ENOMEM;
2188 		if (likely(skb)) {
2189 			bnxt_deliver_skb(bp, bnapi, skb);
2190 			rc = 1;
2191 		}
2192 		*event |= BNXT_RX_EVENT;
2193 		goto next_rx_no_prod_no_len;
2194 	}
2195 
2196 	cons = rxcmp->rx_cmp_opaque;
2197 	if (unlikely(cons != rxr->rx_next_cons)) {
2198 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2199 
2200 		/* 0xffff is forced error, don't print it */
2201 		if (rxr->rx_next_cons != 0xffff)
2202 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2203 				    cons, rxr->rx_next_cons);
2204 		bnxt_sched_reset_rxr(bp, rxr);
2205 		if (rc1)
2206 			return rc1;
2207 		goto next_rx_no_prod_no_len;
2208 	}
2209 	rx_buf = &rxr->rx_buf_ring[cons];
2210 	data = rx_buf->data;
2211 	data_ptr = rx_buf->data_ptr;
2212 	prefetch(data_ptr);
2213 
2214 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2215 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2216 
2217 	if (agg_bufs) {
2218 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2219 			return -EBUSY;
2220 
2221 		cp_cons = NEXT_CMP(cp_cons);
2222 		*event |= BNXT_AGG_EVENT;
2223 	}
2224 	*event |= BNXT_RX_EVENT;
2225 
2226 	rx_buf->data = NULL;
2227 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2228 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2229 
2230 		bnxt_reuse_rx_data(rxr, cons, data);
2231 		if (agg_bufs)
2232 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2233 					       false);
2234 
2235 		rc = -EIO;
2236 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2237 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2238 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2239 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2240 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2241 						 rx_err);
2242 				bnxt_sched_reset_rxr(bp, rxr);
2243 			}
2244 		}
2245 		goto next_rx_no_len;
2246 	}
2247 
2248 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2249 	len = flags >> RX_CMP_LEN_SHIFT;
2250 	dma_addr = rx_buf->mapping;
2251 
2252 	if (bnxt_xdp_attached(bp, rxr)) {
2253 		bnxt_xdp.rxcmp = rxcmp;
2254 		bnxt_xdp.rxcmp1 = rxcmp1;
2255 		bnxt_xdp.cmp_type = cmp_type;
2256 
2257 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &bnxt_xdp.xdp);
2258 		if (agg_bufs) {
2259 			u32 frag_len = bnxt_rx_agg_netmems_xdp(bp, cpr,
2260 							       &bnxt_xdp.xdp,
2261 							       cp_cons,
2262 							       agg_bufs,
2263 							       false);
2264 			if (!frag_len)
2265 				goto oom_next_rx;
2266 
2267 		}
2268 		xdp_active = true;
2269 	}
2270 
2271 	if (xdp_active) {
2272 		if (bnxt_rx_xdp(bp, rxr, cons, &bnxt_xdp.xdp, data, &data_ptr,
2273 				&len, event)) {
2274 			rc = 1;
2275 			goto next_rx;
2276 		}
2277 		if (xdp_buff_has_frags(&bnxt_xdp.xdp)) {
2278 			sinfo = xdp_get_shared_info_from_buff(&bnxt_xdp.xdp);
2279 			agg_bufs = sinfo->nr_frags;
2280 		} else {
2281 			agg_bufs = 0;
2282 		}
2283 	}
2284 
2285 	if (len <= bp->rx_copybreak) {
2286 		if (!xdp_active)
2287 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2288 		else
2289 			skb = bnxt_copy_xdp(bnapi, &bnxt_xdp.xdp, len,
2290 					    dma_addr);
2291 		bnxt_reuse_rx_data(rxr, cons, data);
2292 		if (!skb) {
2293 			if (agg_bufs) {
2294 				if (!xdp_active)
2295 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2296 							       agg_bufs, false);
2297 				else
2298 					bnxt_xdp_buff_frags_free(rxr,
2299 								 &bnxt_xdp.xdp);
2300 			}
2301 			goto oom_next_rx;
2302 		}
2303 	} else {
2304 		u32 payload;
2305 
2306 		if (rx_buf->data_ptr == data_ptr)
2307 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2308 		else
2309 			payload = 0;
2310 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2311 				      payload | len);
2312 		if (!skb)
2313 			goto oom_next_rx;
2314 	}
2315 
2316 	if (agg_bufs) {
2317 		if (!xdp_active) {
2318 			skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, cp_cons,
2319 						      agg_bufs, false);
2320 			if (!skb)
2321 				goto oom_next_rx;
2322 		} else {
2323 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs,
2324 						 rxr, &bnxt_xdp.xdp);
2325 			if (!skb) {
2326 				/* we should be able to free the old skb here */
2327 				bnxt_xdp_buff_frags_free(rxr, &bnxt_xdp.xdp);
2328 				goto oom_next_rx;
2329 			}
2330 		}
2331 	}
2332 
2333 	if (RX_CMP_HASH_VALID(rxcmp)) {
2334 		enum pkt_hash_types type;
2335 
2336 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2337 			type = bnxt_rss_ext_op(bp, rxcmp);
2338 		} else {
2339 			u32 itypes = RX_CMP_ITYPES(rxcmp);
2340 
2341 			if (itypes == RX_CMP_FLAGS_ITYPE_TCP ||
2342 			    itypes == RX_CMP_FLAGS_ITYPE_UDP)
2343 				type = PKT_HASH_TYPE_L4;
2344 			else
2345 				type = PKT_HASH_TYPE_L3;
2346 		}
2347 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2348 	}
2349 
2350 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2351 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2352 	skb->protocol = eth_type_trans(skb, dev);
2353 
2354 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2355 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2356 		if (!skb)
2357 			goto next_rx;
2358 	}
2359 
2360 	skb_checksum_none_assert(skb);
2361 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2362 		if (dev->features & NETIF_F_RXCSUM) {
2363 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2364 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2365 		}
2366 	} else {
2367 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2368 			if (dev->features & NETIF_F_RXCSUM)
2369 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2370 		}
2371 	}
2372 
2373 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2374 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2375 			u64 ns, ts;
2376 
2377 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2378 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2379 
2380 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2381 				memset(skb_hwtstamps(skb), 0,
2382 				       sizeof(*skb_hwtstamps(skb)));
2383 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2384 			}
2385 		}
2386 	}
2387 	bnxt_deliver_skb(bp, bnapi, skb);
2388 	rc = 1;
2389 
2390 next_rx:
2391 	cpr->rx_packets += 1;
2392 	cpr->rx_bytes += len;
2393 
2394 next_rx_no_len:
2395 	rxr->rx_prod = NEXT_RX(prod);
2396 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2397 
2398 next_rx_no_prod_no_len:
2399 	*raw_cons = tmp_raw_cons;
2400 
2401 	return rc;
2402 
2403 oom_next_rx:
2404 	cpr->sw_stats->rx.rx_oom_discards += 1;
2405 	rc = -ENOMEM;
2406 	goto next_rx;
2407 }
2408 
2409 /* In netpoll mode, if we are using a combined completion ring, we need to
2410  * discard the rx packets and recycle the buffers.
2411  */
2412 static int bnxt_force_rx_discard(struct bnxt *bp,
2413 				 struct bnxt_cp_ring_info *cpr,
2414 				 u32 *raw_cons, u8 *event)
2415 {
2416 	u32 tmp_raw_cons = *raw_cons;
2417 	struct rx_cmp_ext *rxcmp1;
2418 	struct rx_cmp *rxcmp;
2419 	u16 cp_cons;
2420 	u8 cmp_type;
2421 	int rc;
2422 
2423 	cp_cons = RING_CMP(tmp_raw_cons);
2424 	rxcmp = (struct rx_cmp *)
2425 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2426 
2427 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2428 	cp_cons = RING_CMP(tmp_raw_cons);
2429 	rxcmp1 = (struct rx_cmp_ext *)
2430 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2431 
2432 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2433 		return -EBUSY;
2434 
2435 	/* The valid test of the entry must be done first before
2436 	 * reading any further.
2437 	 */
2438 	dma_rmb();
2439 	cmp_type = RX_CMP_TYPE(rxcmp);
2440 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2441 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2442 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2443 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2444 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2445 		struct rx_tpa_end_cmp_ext *tpa_end1;
2446 
2447 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2448 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2449 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2450 	}
2451 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2452 	if (rc && rc != -EBUSY)
2453 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2454 	return rc;
2455 }
2456 
2457 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2458 {
2459 	struct bnxt_fw_health *fw_health = bp->fw_health;
2460 	u32 reg = fw_health->regs[reg_idx];
2461 	u32 reg_type, reg_off, val = 0;
2462 
2463 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2464 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2465 	switch (reg_type) {
2466 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2467 		pci_read_config_dword(bp->pdev, reg_off, &val);
2468 		break;
2469 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2470 		reg_off = fw_health->mapped_regs[reg_idx];
2471 		fallthrough;
2472 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2473 		val = readl(bp->bar0 + reg_off);
2474 		break;
2475 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2476 		val = readl(bp->bar1 + reg_off);
2477 		break;
2478 	}
2479 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2480 		val &= fw_health->fw_reset_inprog_reg_mask;
2481 	return val;
2482 }
2483 
2484 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2485 {
2486 	int i;
2487 
2488 	for (i = 0; i < bp->rx_nr_rings; i++) {
2489 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2490 		struct bnxt_ring_grp_info *grp_info;
2491 
2492 		grp_info = &bp->grp_info[grp_idx];
2493 		if (grp_info->agg_fw_ring_id == ring_id)
2494 			return grp_idx;
2495 	}
2496 	return INVALID_HW_RING_ID;
2497 }
2498 
2499 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2500 {
2501 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2502 
2503 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2504 		return link_info->force_link_speed2;
2505 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2506 		return link_info->force_pam4_link_speed;
2507 	return link_info->force_link_speed;
2508 }
2509 
2510 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2511 {
2512 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2513 
2514 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2515 		link_info->req_link_speed = link_info->force_link_speed2;
2516 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2517 		switch (link_info->req_link_speed) {
2518 		case BNXT_LINK_SPEED_50GB_PAM4:
2519 		case BNXT_LINK_SPEED_100GB_PAM4:
2520 		case BNXT_LINK_SPEED_200GB_PAM4:
2521 		case BNXT_LINK_SPEED_400GB_PAM4:
2522 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2523 			break;
2524 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2525 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2526 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2527 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2528 			break;
2529 		default:
2530 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2531 		}
2532 		return;
2533 	}
2534 	link_info->req_link_speed = link_info->force_link_speed;
2535 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2536 	if (link_info->force_pam4_link_speed) {
2537 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2538 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2539 	}
2540 }
2541 
2542 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2543 {
2544 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2545 
2546 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2547 		link_info->advertising = link_info->auto_link_speeds2;
2548 		return;
2549 	}
2550 	link_info->advertising = link_info->auto_link_speeds;
2551 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2552 }
2553 
2554 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2555 {
2556 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2557 
2558 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2559 		if (link_info->req_link_speed != link_info->force_link_speed2)
2560 			return true;
2561 		return false;
2562 	}
2563 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2564 	    link_info->req_link_speed != link_info->force_link_speed)
2565 		return true;
2566 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2567 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2568 		return true;
2569 	return false;
2570 }
2571 
2572 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2573 {
2574 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2575 
2576 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2577 		if (link_info->advertising != link_info->auto_link_speeds2)
2578 			return true;
2579 		return false;
2580 	}
2581 	if (link_info->advertising != link_info->auto_link_speeds ||
2582 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2583 		return true;
2584 	return false;
2585 }
2586 
2587 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type)
2588 {
2589 	u32 flags = bp->ctx->ctx_arr[type].flags;
2590 
2591 	return (flags & BNXT_CTX_MEM_TYPE_VALID) &&
2592 		((flags & BNXT_CTX_MEM_FW_TRACE) ||
2593 		 (flags & BNXT_CTX_MEM_FW_BIN_TRACE));
2594 }
2595 
2596 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm)
2597 {
2598 	u32 mem_size, pages, rem_bytes, magic_byte_offset;
2599 	u16 trace_type = bnxt_bstore_to_trace[ctxm->type];
2600 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
2601 	struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl;
2602 	struct bnxt_bs_trace_info *bs_trace;
2603 	int last_pg;
2604 
2605 	if (ctxm->instance_bmap && ctxm->instance_bmap > 1)
2606 		return;
2607 
2608 	mem_size = ctxm->max_entries * ctxm->entry_size;
2609 	rem_bytes = mem_size % BNXT_PAGE_SIZE;
2610 	pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
2611 
2612 	last_pg = (pages - 1) & (MAX_CTX_PAGES - 1);
2613 	magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1;
2614 
2615 	rmem = &ctx_pg[0].ring_mem;
2616 	bs_trace = &bp->bs_trace[trace_type];
2617 	bs_trace->ctx_type = ctxm->type;
2618 	bs_trace->trace_type = trace_type;
2619 	if (pages > MAX_CTX_PAGES) {
2620 		int last_pg_dir = rmem->nr_pages - 1;
2621 
2622 		rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem;
2623 		bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg];
2624 	} else {
2625 		bs_trace->magic_byte = rmem->pg_arr[last_pg];
2626 	}
2627 	bs_trace->magic_byte += magic_byte_offset;
2628 	*bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE;
2629 }
2630 
2631 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1)				\
2632 	(((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\
2633 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT)
2634 
2635 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2)				\
2636 	(((data2) &							\
2637 	  ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\
2638 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT)
2639 
2640 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2641 	((data2) &							\
2642 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2643 
2644 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2645 	(((data2) &							\
2646 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2647 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2648 
2649 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2650 	((data1) &							\
2651 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2652 
2653 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2654 	(((data1) &							\
2655 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2656 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2657 
2658 /* Return true if the workqueue has to be scheduled */
2659 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2660 {
2661 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2662 
2663 	switch (err_type) {
2664 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2665 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2666 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2667 		break;
2668 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2669 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2670 		break;
2671 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2672 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2673 		break;
2674 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2675 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2676 		char *threshold_type;
2677 		bool notify = false;
2678 		char *dir_str;
2679 
2680 		switch (type) {
2681 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2682 			threshold_type = "warning";
2683 			break;
2684 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2685 			threshold_type = "critical";
2686 			break;
2687 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2688 			threshold_type = "fatal";
2689 			break;
2690 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2691 			threshold_type = "shutdown";
2692 			break;
2693 		default:
2694 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2695 			return false;
2696 		}
2697 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2698 			dir_str = "above";
2699 			notify = true;
2700 		} else {
2701 			dir_str = "below";
2702 		}
2703 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2704 			    dir_str, threshold_type);
2705 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2706 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2707 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2708 		if (notify) {
2709 			bp->thermal_threshold_type = type;
2710 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2711 			return true;
2712 		}
2713 		return false;
2714 	}
2715 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2716 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2717 		break;
2718 	default:
2719 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2720 			   err_type);
2721 		break;
2722 	}
2723 	return false;
2724 }
2725 
2726 #define BNXT_GET_EVENT_PORT(data)	\
2727 	((data) &			\
2728 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2729 
2730 #define BNXT_EVENT_RING_TYPE(data2)	\
2731 	((data2) &			\
2732 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2733 
2734 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2735 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2736 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2737 
2738 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2739 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2740 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2741 
2742 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2743 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2744 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2745 
2746 #define BNXT_PHC_BITS	48
2747 
2748 static int bnxt_async_event_process(struct bnxt *bp,
2749 				    struct hwrm_async_event_cmpl *cmpl)
2750 {
2751 	u16 event_id = le16_to_cpu(cmpl->event_id);
2752 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2753 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2754 
2755 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2756 		   event_id, data1, data2);
2757 
2758 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2759 	switch (event_id) {
2760 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2761 		struct bnxt_link_info *link_info = &bp->link_info;
2762 
2763 		if (BNXT_VF(bp))
2764 			goto async_event_process_exit;
2765 
2766 		/* print unsupported speed warning in forced speed mode only */
2767 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2768 		    (data1 & 0x20000)) {
2769 			u16 fw_speed = bnxt_get_force_speed(link_info);
2770 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2771 
2772 			if (speed != SPEED_UNKNOWN)
2773 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2774 					    speed);
2775 		}
2776 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2777 	}
2778 		fallthrough;
2779 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2780 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2781 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2782 		fallthrough;
2783 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2784 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2785 		break;
2786 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2787 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2788 		break;
2789 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2790 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2791 
2792 		if (BNXT_VF(bp))
2793 			break;
2794 
2795 		if (bp->pf.port_id != port_id)
2796 			break;
2797 
2798 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2799 		break;
2800 	}
2801 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2802 		if (BNXT_PF(bp))
2803 			goto async_event_process_exit;
2804 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2805 		break;
2806 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2807 		char *type_str = "Solicited";
2808 
2809 		if (!bp->fw_health)
2810 			goto async_event_process_exit;
2811 
2812 		bp->fw_reset_timestamp = jiffies;
2813 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2814 		if (!bp->fw_reset_min_dsecs)
2815 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2816 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2817 		if (!bp->fw_reset_max_dsecs)
2818 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2819 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2820 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2821 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2822 			type_str = "Fatal";
2823 			bp->fw_health->fatalities++;
2824 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2825 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2826 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2827 			type_str = "Non-fatal";
2828 			bp->fw_health->survivals++;
2829 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2830 		}
2831 		netif_warn(bp, hw, bp->dev,
2832 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2833 			   type_str, data1, data2,
2834 			   bp->fw_reset_min_dsecs * 100,
2835 			   bp->fw_reset_max_dsecs * 100);
2836 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2837 		break;
2838 	}
2839 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2840 		struct bnxt_fw_health *fw_health = bp->fw_health;
2841 		char *status_desc = "healthy";
2842 		u32 status;
2843 
2844 		if (!fw_health)
2845 			goto async_event_process_exit;
2846 
2847 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2848 			fw_health->enabled = false;
2849 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2850 			break;
2851 		}
2852 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2853 		fw_health->tmr_multiplier =
2854 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2855 				     bp->current_interval * 10);
2856 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2857 		if (!fw_health->enabled)
2858 			fw_health->last_fw_heartbeat =
2859 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2860 		fw_health->last_fw_reset_cnt =
2861 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2862 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2863 		if (status != BNXT_FW_STATUS_HEALTHY)
2864 			status_desc = "unhealthy";
2865 		netif_info(bp, drv, bp->dev,
2866 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2867 			   fw_health->primary ? "primary" : "backup", status,
2868 			   status_desc, fw_health->last_fw_reset_cnt);
2869 		if (!fw_health->enabled) {
2870 			/* Make sure tmr_counter is set and visible to
2871 			 * bnxt_health_check() before setting enabled to true.
2872 			 */
2873 			smp_wmb();
2874 			fw_health->enabled = true;
2875 		}
2876 		goto async_event_process_exit;
2877 	}
2878 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2879 		netif_notice(bp, hw, bp->dev,
2880 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2881 			     data1, data2);
2882 		goto async_event_process_exit;
2883 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2884 		struct bnxt_rx_ring_info *rxr;
2885 		u16 grp_idx;
2886 
2887 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2888 			goto async_event_process_exit;
2889 
2890 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2891 			    BNXT_EVENT_RING_TYPE(data2), data1);
2892 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2893 			goto async_event_process_exit;
2894 
2895 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2896 		if (grp_idx == INVALID_HW_RING_ID) {
2897 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2898 				    data1);
2899 			goto async_event_process_exit;
2900 		}
2901 		rxr = bp->bnapi[grp_idx]->rx_ring;
2902 		bnxt_sched_reset_rxr(bp, rxr);
2903 		goto async_event_process_exit;
2904 	}
2905 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2906 		struct bnxt_fw_health *fw_health = bp->fw_health;
2907 
2908 		netif_notice(bp, hw, bp->dev,
2909 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2910 			     data1, data2);
2911 		if (fw_health) {
2912 			fw_health->echo_req_data1 = data1;
2913 			fw_health->echo_req_data2 = data2;
2914 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2915 			break;
2916 		}
2917 		goto async_event_process_exit;
2918 	}
2919 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2920 		bnxt_ptp_pps_event(bp, data1, data2);
2921 		goto async_event_process_exit;
2922 	}
2923 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2924 		if (bnxt_event_error_report(bp, data1, data2))
2925 			break;
2926 		goto async_event_process_exit;
2927 	}
2928 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2929 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2930 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2931 			if (BNXT_PTP_USE_RTC(bp)) {
2932 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2933 				unsigned long flags;
2934 				u64 ns;
2935 
2936 				if (!ptp)
2937 					goto async_event_process_exit;
2938 
2939 				bnxt_ptp_update_current_time(bp);
2940 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2941 				       BNXT_PHC_BITS) | ptp->current_time);
2942 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2943 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2944 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2945 			}
2946 			break;
2947 		}
2948 		goto async_event_process_exit;
2949 	}
2950 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2951 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2952 
2953 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2954 		goto async_event_process_exit;
2955 	}
2956 	case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: {
2957 		u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1);
2958 		u32 offset =  BNXT_EVENT_BUF_PRODUCER_OFFSET(data2);
2959 
2960 		if (type >= ARRAY_SIZE(bp->bs_trace))
2961 			goto async_event_process_exit;
2962 		bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset);
2963 		goto async_event_process_exit;
2964 	}
2965 	default:
2966 		goto async_event_process_exit;
2967 	}
2968 	__bnxt_queue_sp_work(bp);
2969 async_event_process_exit:
2970 	bnxt_ulp_async_events(bp, cmpl);
2971 	return 0;
2972 }
2973 
2974 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2975 {
2976 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2977 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2978 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2979 				(struct hwrm_fwd_req_cmpl *)txcmp;
2980 
2981 	switch (cmpl_type) {
2982 	case CMPL_BASE_TYPE_HWRM_DONE:
2983 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2984 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2985 		break;
2986 
2987 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2988 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2989 
2990 		if ((vf_id < bp->pf.first_vf_id) ||
2991 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2992 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2993 				   vf_id);
2994 			return -EINVAL;
2995 		}
2996 
2997 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2998 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2999 		break;
3000 
3001 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
3002 		bnxt_async_event_process(bp,
3003 					 (struct hwrm_async_event_cmpl *)txcmp);
3004 		break;
3005 
3006 	default:
3007 		break;
3008 	}
3009 
3010 	return 0;
3011 }
3012 
3013 static bool bnxt_vnic_is_active(struct bnxt *bp)
3014 {
3015 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
3016 
3017 	return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0;
3018 }
3019 
3020 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
3021 {
3022 	struct bnxt_napi *bnapi = dev_instance;
3023 	struct bnxt *bp = bnapi->bp;
3024 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3025 	u32 cons = RING_CMP(cpr->cp_raw_cons);
3026 
3027 	cpr->event_ctr++;
3028 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
3029 	napi_schedule(&bnapi->napi);
3030 	return IRQ_HANDLED;
3031 }
3032 
3033 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
3034 {
3035 	u32 raw_cons = cpr->cp_raw_cons;
3036 	u16 cons = RING_CMP(raw_cons);
3037 	struct tx_cmp *txcmp;
3038 
3039 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3040 
3041 	return TX_CMP_VALID(txcmp, raw_cons);
3042 }
3043 
3044 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3045 			    int budget)
3046 {
3047 	struct bnxt_napi *bnapi = cpr->bnapi;
3048 	u32 raw_cons = cpr->cp_raw_cons;
3049 	bool flush_xdp = false;
3050 	u32 cons;
3051 	int rx_pkts = 0;
3052 	u8 event = 0;
3053 	struct tx_cmp *txcmp;
3054 
3055 	cpr->has_more_work = 0;
3056 	cpr->had_work_done = 1;
3057 	while (1) {
3058 		u8 cmp_type;
3059 		int rc;
3060 
3061 		cons = RING_CMP(raw_cons);
3062 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3063 
3064 		if (!TX_CMP_VALID(txcmp, raw_cons))
3065 			break;
3066 
3067 		/* The valid test of the entry must be done first before
3068 		 * reading any further.
3069 		 */
3070 		dma_rmb();
3071 		cmp_type = TX_CMP_TYPE(txcmp);
3072 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
3073 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
3074 			u32 opaque = txcmp->tx_cmp_opaque;
3075 			struct bnxt_tx_ring_info *txr;
3076 			u16 tx_freed;
3077 
3078 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
3079 			event |= BNXT_TX_CMP_EVENT;
3080 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
3081 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
3082 			else
3083 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
3084 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
3085 				   bp->tx_ring_mask;
3086 			/* return full budget so NAPI will complete. */
3087 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
3088 				rx_pkts = budget;
3089 				raw_cons = NEXT_RAW_CMP(raw_cons);
3090 				if (budget)
3091 					cpr->has_more_work = 1;
3092 				break;
3093 			}
3094 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
3095 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
3096 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
3097 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
3098 			if (likely(budget))
3099 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3100 			else
3101 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
3102 							   &event);
3103 			if (event & BNXT_REDIRECT_EVENT)
3104 				flush_xdp = true;
3105 			if (likely(rc >= 0))
3106 				rx_pkts += rc;
3107 			/* Increment rx_pkts when rc is -ENOMEM to count towards
3108 			 * the NAPI budget.  Otherwise, we may potentially loop
3109 			 * here forever if we consistently cannot allocate
3110 			 * buffers.
3111 			 */
3112 			else if (rc == -ENOMEM && budget)
3113 				rx_pkts++;
3114 			else if (rc == -EBUSY)	/* partial completion */
3115 				break;
3116 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
3117 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
3118 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
3119 			bnxt_hwrm_handler(bp, txcmp);
3120 		}
3121 		raw_cons = NEXT_RAW_CMP(raw_cons);
3122 
3123 		if (rx_pkts && rx_pkts == budget) {
3124 			cpr->has_more_work = 1;
3125 			break;
3126 		}
3127 	}
3128 
3129 	if (flush_xdp) {
3130 		xdp_do_flush();
3131 		event &= ~BNXT_REDIRECT_EVENT;
3132 	}
3133 
3134 	if (event & BNXT_TX_EVENT) {
3135 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3136 		u16 prod = txr->tx_prod;
3137 
3138 		/* Sync BD data before updating doorbell */
3139 		wmb();
3140 
3141 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3142 		event &= ~BNXT_TX_EVENT;
3143 	}
3144 
3145 	cpr->cp_raw_cons = raw_cons;
3146 	bnapi->events |= event;
3147 	return rx_pkts;
3148 }
3149 
3150 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3151 				  int budget)
3152 {
3153 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3154 		bnapi->tx_int(bp, bnapi, budget);
3155 
3156 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3157 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3158 
3159 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3160 		bnapi->events &= ~BNXT_RX_EVENT;
3161 	}
3162 	if (bnapi->events & BNXT_AGG_EVENT) {
3163 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3164 
3165 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3166 		bnapi->events &= ~BNXT_AGG_EVENT;
3167 	}
3168 }
3169 
3170 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3171 			  int budget)
3172 {
3173 	struct bnxt_napi *bnapi = cpr->bnapi;
3174 	int rx_pkts;
3175 
3176 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3177 
3178 	/* ACK completion ring before freeing tx ring and producing new
3179 	 * buffers in rx/agg rings to prevent overflowing the completion
3180 	 * ring.
3181 	 */
3182 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3183 
3184 	__bnxt_poll_work_done(bp, bnapi, budget);
3185 	return rx_pkts;
3186 }
3187 
3188 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3189 {
3190 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3191 	struct bnxt *bp = bnapi->bp;
3192 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3193 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3194 	struct tx_cmp *txcmp;
3195 	struct rx_cmp_ext *rxcmp1;
3196 	u32 cp_cons, tmp_raw_cons;
3197 	u32 raw_cons = cpr->cp_raw_cons;
3198 	bool flush_xdp = false;
3199 	u32 rx_pkts = 0;
3200 	u8 event = 0;
3201 
3202 	while (1) {
3203 		int rc;
3204 
3205 		cp_cons = RING_CMP(raw_cons);
3206 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3207 
3208 		if (!TX_CMP_VALID(txcmp, raw_cons))
3209 			break;
3210 
3211 		/* The valid test of the entry must be done first before
3212 		 * reading any further.
3213 		 */
3214 		dma_rmb();
3215 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3216 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3217 			cp_cons = RING_CMP(tmp_raw_cons);
3218 			rxcmp1 = (struct rx_cmp_ext *)
3219 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3220 
3221 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3222 				break;
3223 
3224 			/* force an error to recycle the buffer */
3225 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3226 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3227 
3228 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3229 			if (likely(rc == -EIO) && budget)
3230 				rx_pkts++;
3231 			else if (rc == -EBUSY)	/* partial completion */
3232 				break;
3233 			if (event & BNXT_REDIRECT_EVENT)
3234 				flush_xdp = true;
3235 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3236 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3237 			bnxt_hwrm_handler(bp, txcmp);
3238 		} else {
3239 			netdev_err(bp->dev,
3240 				   "Invalid completion received on special ring\n");
3241 		}
3242 		raw_cons = NEXT_RAW_CMP(raw_cons);
3243 
3244 		if (rx_pkts == budget)
3245 			break;
3246 	}
3247 
3248 	cpr->cp_raw_cons = raw_cons;
3249 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3250 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3251 
3252 	if (event & BNXT_AGG_EVENT)
3253 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3254 	if (flush_xdp)
3255 		xdp_do_flush();
3256 
3257 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3258 		napi_complete_done(napi, rx_pkts);
3259 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3260 	}
3261 	return rx_pkts;
3262 }
3263 
3264 static int bnxt_poll(struct napi_struct *napi, int budget)
3265 {
3266 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3267 	struct bnxt *bp = bnapi->bp;
3268 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3269 	int work_done = 0;
3270 
3271 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3272 		napi_complete(napi);
3273 		return 0;
3274 	}
3275 	while (1) {
3276 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3277 
3278 		if (work_done >= budget) {
3279 			if (!budget)
3280 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3281 			break;
3282 		}
3283 
3284 		if (!bnxt_has_work(bp, cpr)) {
3285 			if (napi_complete_done(napi, work_done))
3286 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3287 			break;
3288 		}
3289 	}
3290 	if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3291 		struct dim_sample dim_sample = {};
3292 
3293 		dim_update_sample(cpr->event_ctr,
3294 				  cpr->rx_packets,
3295 				  cpr->rx_bytes,
3296 				  &dim_sample);
3297 		net_dim(&cpr->dim, &dim_sample);
3298 	}
3299 	return work_done;
3300 }
3301 
3302 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3303 {
3304 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3305 	int i, work_done = 0;
3306 
3307 	for (i = 0; i < cpr->cp_ring_count; i++) {
3308 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3309 
3310 		if (cpr2->had_nqe_notify) {
3311 			work_done += __bnxt_poll_work(bp, cpr2,
3312 						      budget - work_done);
3313 			cpr->has_more_work |= cpr2->has_more_work;
3314 		}
3315 	}
3316 	return work_done;
3317 }
3318 
3319 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3320 				 u64 dbr_type, int budget)
3321 {
3322 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3323 	int i;
3324 
3325 	for (i = 0; i < cpr->cp_ring_count; i++) {
3326 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3327 		struct bnxt_db_info *db;
3328 
3329 		if (cpr2->had_work_done) {
3330 			u32 tgl = 0;
3331 
3332 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3333 				cpr2->had_nqe_notify = 0;
3334 				tgl = cpr2->toggle;
3335 			}
3336 			db = &cpr2->cp_db;
3337 			bnxt_writeq(bp,
3338 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3339 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3340 				    db->doorbell);
3341 			cpr2->had_work_done = 0;
3342 		}
3343 	}
3344 	__bnxt_poll_work_done(bp, bnapi, budget);
3345 }
3346 
3347 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3348 {
3349 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3350 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3351 	struct bnxt_cp_ring_info *cpr_rx;
3352 	u32 raw_cons = cpr->cp_raw_cons;
3353 	struct bnxt *bp = bnapi->bp;
3354 	struct nqe_cn *nqcmp;
3355 	int work_done = 0;
3356 	u32 cons;
3357 
3358 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3359 		napi_complete(napi);
3360 		return 0;
3361 	}
3362 	if (cpr->has_more_work) {
3363 		cpr->has_more_work = 0;
3364 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3365 	}
3366 	while (1) {
3367 		u16 type;
3368 
3369 		cons = RING_CMP(raw_cons);
3370 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3371 
3372 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3373 			if (cpr->has_more_work)
3374 				break;
3375 
3376 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3377 					     budget);
3378 			cpr->cp_raw_cons = raw_cons;
3379 			if (napi_complete_done(napi, work_done))
3380 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3381 						  cpr->cp_raw_cons);
3382 			goto poll_done;
3383 		}
3384 
3385 		/* The valid test of the entry must be done first before
3386 		 * reading any further.
3387 		 */
3388 		dma_rmb();
3389 
3390 		type = le16_to_cpu(nqcmp->type);
3391 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3392 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3393 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3394 			struct bnxt_cp_ring_info *cpr2;
3395 
3396 			/* No more budget for RX work */
3397 			if (budget && work_done >= budget &&
3398 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3399 				break;
3400 
3401 			idx = BNXT_NQ_HDL_IDX(idx);
3402 			cpr2 = &cpr->cp_ring_arr[idx];
3403 			cpr2->had_nqe_notify = 1;
3404 			cpr2->toggle = NQE_CN_TOGGLE(type);
3405 			work_done += __bnxt_poll_work(bp, cpr2,
3406 						      budget - work_done);
3407 			cpr->has_more_work |= cpr2->has_more_work;
3408 		} else {
3409 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3410 		}
3411 		raw_cons = NEXT_RAW_CMP(raw_cons);
3412 	}
3413 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3414 	if (raw_cons != cpr->cp_raw_cons) {
3415 		cpr->cp_raw_cons = raw_cons;
3416 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3417 	}
3418 poll_done:
3419 	cpr_rx = &cpr->cp_ring_arr[0];
3420 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3421 	    (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3422 		struct dim_sample dim_sample = {};
3423 
3424 		dim_update_sample(cpr->event_ctr,
3425 				  cpr_rx->rx_packets,
3426 				  cpr_rx->rx_bytes,
3427 				  &dim_sample);
3428 		net_dim(&cpr->dim, &dim_sample);
3429 	}
3430 	return work_done;
3431 }
3432 
3433 static void bnxt_free_one_tx_ring_skbs(struct bnxt *bp,
3434 				       struct bnxt_tx_ring_info *txr, int idx)
3435 {
3436 	int i, max_idx;
3437 	struct pci_dev *pdev = bp->pdev;
3438 	unsigned int dma_len;
3439 	dma_addr_t dma_addr;
3440 
3441 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3442 
3443 	for (i = 0; i < max_idx;) {
3444 		struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[i];
3445 		struct bnxt_sw_tx_bd *head_buf = tx_buf;
3446 		struct sk_buff *skb;
3447 		int j, last;
3448 
3449 		if (idx  < bp->tx_nr_rings_xdp &&
3450 		    tx_buf->action == XDP_REDIRECT) {
3451 			dma_addr = dma_unmap_addr(tx_buf, mapping);
3452 			dma_len = dma_unmap_len(tx_buf, len);
3453 
3454 			dma_unmap_single(&pdev->dev, dma_addr, dma_len,
3455 					 DMA_TO_DEVICE);
3456 			xdp_return_frame(tx_buf->xdpf);
3457 			tx_buf->action = 0;
3458 			tx_buf->xdpf = NULL;
3459 			i++;
3460 			continue;
3461 		}
3462 
3463 		skb = tx_buf->skb;
3464 		if (!skb) {
3465 			i++;
3466 			continue;
3467 		}
3468 
3469 		tx_buf->skb = NULL;
3470 
3471 		if (tx_buf->is_push) {
3472 			dev_kfree_skb(skb);
3473 			i += 2;
3474 			continue;
3475 		}
3476 
3477 		if (dma_unmap_len(tx_buf, len)) {
3478 			dma_addr = dma_unmap_addr(tx_buf, mapping);
3479 			dma_len = dma_unmap_len(tx_buf, len);
3480 
3481 			dma_unmap_single(&pdev->dev, dma_addr, dma_len,
3482 					 DMA_TO_DEVICE);
3483 		}
3484 
3485 		last = tx_buf->nr_frags;
3486 		i += 2;
3487 		for (j = 0; j < last; j++, i++) {
3488 			int ring_idx = i & bp->tx_ring_mask;
3489 
3490 			tx_buf = &txr->tx_buf_ring[ring_idx];
3491 			if (dma_unmap_len(tx_buf, len)) {
3492 				dma_addr = dma_unmap_addr(tx_buf, mapping);
3493 				dma_len = dma_unmap_len(tx_buf, len);
3494 
3495 				netmem_dma_unmap_page_attrs(&pdev->dev,
3496 							    dma_addr, dma_len,
3497 							    DMA_TO_DEVICE, 0);
3498 			}
3499 		}
3500 		if (head_buf->is_sw_gso) {
3501 			u16 inline_cons = txr->tx_inline_cons + 1;
3502 
3503 			WRITE_ONCE(txr->tx_inline_cons, inline_cons);
3504 			if (head_buf->is_sw_gso == BNXT_SW_GSO_LAST) {
3505 				tso_dma_map_complete(&pdev->dev,
3506 						     &head_buf->sw_gso_cstate);
3507 			} else {
3508 				skb = NULL;
3509 			}
3510 			head_buf->is_sw_gso = 0;
3511 		}
3512 		if (skb)
3513 			dev_kfree_skb(skb);
3514 	}
3515 	netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, idx));
3516 }
3517 
3518 static void bnxt_free_tx_skbs(struct bnxt *bp)
3519 {
3520 	int i;
3521 
3522 	if (!bp->tx_ring)
3523 		return;
3524 
3525 	for (i = 0; i < bp->tx_nr_rings; i++) {
3526 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3527 
3528 		if (!txr->tx_buf_ring)
3529 			continue;
3530 
3531 		bnxt_free_one_tx_ring_skbs(bp, txr, i);
3532 	}
3533 
3534 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
3535 		bnxt_ptp_free_txts_skbs(bp->ptp_cfg);
3536 }
3537 
3538 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3539 {
3540 	int i, max_idx;
3541 
3542 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3543 
3544 	for (i = 0; i < max_idx; i++) {
3545 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3546 		void *data = rx_buf->data;
3547 
3548 		if (!data)
3549 			continue;
3550 
3551 		rx_buf->data = NULL;
3552 		if (BNXT_RX_PAGE_MODE(bp))
3553 			page_pool_recycle_direct(rxr->page_pool, data);
3554 		else
3555 			page_pool_free_va(rxr->head_pool, data, true);
3556 	}
3557 }
3558 
3559 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3560 {
3561 	int i, max_idx;
3562 
3563 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3564 
3565 	for (i = 0; i < max_idx; i++) {
3566 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3567 		netmem_ref netmem = rx_agg_buf->netmem;
3568 
3569 		if (!netmem)
3570 			continue;
3571 
3572 		rx_agg_buf->netmem = 0;
3573 		__clear_bit(i, rxr->rx_agg_bmap);
3574 
3575 		page_pool_recycle_direct_netmem(rxr->page_pool, netmem);
3576 	}
3577 }
3578 
3579 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3580 					struct bnxt_rx_ring_info *rxr)
3581 {
3582 	int i;
3583 
3584 	for (i = 0; i < bp->max_tpa; i++) {
3585 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3586 		u8 *data = tpa_info->data;
3587 
3588 		if (!data)
3589 			continue;
3590 
3591 		tpa_info->data = NULL;
3592 		page_pool_free_va(rxr->head_pool, data, false);
3593 	}
3594 }
3595 
3596 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3597 				       struct bnxt_rx_ring_info *rxr)
3598 {
3599 	struct bnxt_tpa_idx_map *map;
3600 
3601 	if (!rxr->rx_tpa)
3602 		goto skip_rx_tpa_free;
3603 
3604 	bnxt_free_one_tpa_info_data(bp, rxr);
3605 
3606 skip_rx_tpa_free:
3607 	if (!rxr->rx_buf_ring)
3608 		goto skip_rx_buf_free;
3609 
3610 	bnxt_free_one_rx_ring(bp, rxr);
3611 
3612 skip_rx_buf_free:
3613 	if (!rxr->rx_agg_ring)
3614 		goto skip_rx_agg_free;
3615 
3616 	bnxt_free_one_rx_agg_ring(bp, rxr);
3617 
3618 skip_rx_agg_free:
3619 	map = rxr->rx_tpa_idx_map;
3620 	if (map)
3621 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3622 }
3623 
3624 static void bnxt_free_rx_skbs(struct bnxt *bp)
3625 {
3626 	int i;
3627 
3628 	if (!bp->rx_ring)
3629 		return;
3630 
3631 	for (i = 0; i < bp->rx_nr_rings; i++)
3632 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3633 }
3634 
3635 static void bnxt_free_skbs(struct bnxt *bp)
3636 {
3637 	bnxt_free_tx_skbs(bp);
3638 	bnxt_free_rx_skbs(bp);
3639 }
3640 
3641 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3642 {
3643 	u8 init_val = ctxm->init_value;
3644 	u16 offset = ctxm->init_offset;
3645 	u8 *p2 = p;
3646 	int i;
3647 
3648 	if (!init_val)
3649 		return;
3650 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3651 		memset(p, init_val, len);
3652 		return;
3653 	}
3654 	for (i = 0; i < len; i += ctxm->entry_size)
3655 		*(p2 + i + offset) = init_val;
3656 }
3657 
3658 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3659 			       void *buf, size_t offset, size_t head,
3660 			       size_t tail)
3661 {
3662 	int i, head_page, start_idx, source_offset;
3663 	size_t len, rem_len, total_len, max_bytes;
3664 
3665 	head_page = head / rmem->page_size;
3666 	source_offset = head % rmem->page_size;
3667 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3668 	if (!total_len)
3669 		total_len = MAX_CTX_BYTES;
3670 	start_idx = head_page % MAX_CTX_PAGES;
3671 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3672 		    source_offset;
3673 	total_len = min(total_len, max_bytes);
3674 	rem_len = total_len;
3675 
3676 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3677 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3678 		if (buf)
3679 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3680 			       len);
3681 		offset += len;
3682 		rem_len -= len;
3683 	}
3684 	return total_len;
3685 }
3686 
3687 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3688 {
3689 	struct pci_dev *pdev = bp->pdev;
3690 	int i;
3691 
3692 	if (!rmem->pg_arr)
3693 		goto skip_pages;
3694 
3695 	for (i = 0; i < rmem->nr_pages; i++) {
3696 		if (!rmem->pg_arr[i])
3697 			continue;
3698 
3699 		dma_free_coherent(&pdev->dev, rmem->page_size,
3700 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3701 
3702 		rmem->pg_arr[i] = NULL;
3703 	}
3704 skip_pages:
3705 	if (rmem->pg_tbl) {
3706 		size_t pg_tbl_size = rmem->nr_pages * 8;
3707 
3708 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3709 			pg_tbl_size = rmem->page_size;
3710 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3711 				  rmem->pg_tbl, rmem->pg_tbl_map);
3712 		rmem->pg_tbl = NULL;
3713 	}
3714 	if (rmem->vmem_size && *rmem->vmem) {
3715 		vfree(*rmem->vmem);
3716 		*rmem->vmem = NULL;
3717 	}
3718 }
3719 
3720 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3721 {
3722 	struct pci_dev *pdev = bp->pdev;
3723 	u64 valid_bit = 0;
3724 	int i;
3725 
3726 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3727 		valid_bit = PTU_PTE_VALID;
3728 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3729 		size_t pg_tbl_size = rmem->nr_pages * 8;
3730 
3731 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3732 			pg_tbl_size = rmem->page_size;
3733 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3734 						  &rmem->pg_tbl_map,
3735 						  GFP_KERNEL);
3736 		if (!rmem->pg_tbl)
3737 			return -ENOMEM;
3738 	}
3739 
3740 	for (i = 0; i < rmem->nr_pages; i++) {
3741 		u64 extra_bits = valid_bit;
3742 
3743 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3744 						     rmem->page_size,
3745 						     &rmem->dma_arr[i],
3746 						     GFP_KERNEL);
3747 		if (!rmem->pg_arr[i])
3748 			return -ENOMEM;
3749 
3750 		if (rmem->ctx_mem)
3751 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3752 					  rmem->page_size);
3753 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3754 			if (i == rmem->nr_pages - 2 &&
3755 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3756 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3757 			else if (i == rmem->nr_pages - 1 &&
3758 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3759 				extra_bits |= PTU_PTE_LAST;
3760 			rmem->pg_tbl[i] =
3761 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3762 		}
3763 	}
3764 
3765 	if (rmem->vmem_size) {
3766 		*rmem->vmem = vzalloc(rmem->vmem_size);
3767 		if (!(*rmem->vmem))
3768 			return -ENOMEM;
3769 	}
3770 	return 0;
3771 }
3772 
3773 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3774 				   struct bnxt_rx_ring_info *rxr)
3775 {
3776 	int i;
3777 
3778 	kfree(rxr->rx_tpa_idx_map);
3779 	rxr->rx_tpa_idx_map = NULL;
3780 	if (rxr->rx_tpa) {
3781 		for (i = 0; i < bp->max_tpa; i++) {
3782 			kfree(rxr->rx_tpa[i].agg_arr);
3783 			rxr->rx_tpa[i].agg_arr = NULL;
3784 		}
3785 	}
3786 	kfree(rxr->rx_tpa);
3787 	rxr->rx_tpa = NULL;
3788 }
3789 
3790 static void bnxt_free_tpa_info(struct bnxt *bp)
3791 {
3792 	int i;
3793 
3794 	for (i = 0; i < bp->rx_nr_rings; i++) {
3795 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3796 
3797 		bnxt_free_one_tpa_info(bp, rxr);
3798 	}
3799 }
3800 
3801 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3802 				   struct bnxt_rx_ring_info *rxr)
3803 {
3804 	struct rx_agg_cmp *agg;
3805 	int i;
3806 
3807 	rxr->rx_tpa = kzalloc_objs(struct bnxt_tpa_info, bp->max_tpa);
3808 	if (!rxr->rx_tpa)
3809 		return -ENOMEM;
3810 
3811 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3812 		return 0;
3813 	for (i = 0; i < bp->max_tpa; i++) {
3814 		agg = kzalloc_objs(*agg, MAX_SKB_FRAGS);
3815 		if (!agg)
3816 			return -ENOMEM;
3817 		rxr->rx_tpa[i].agg_arr = agg;
3818 	}
3819 	rxr->rx_tpa_idx_map = kzalloc_obj(*rxr->rx_tpa_idx_map);
3820 	if (!rxr->rx_tpa_idx_map)
3821 		return -ENOMEM;
3822 
3823 	return 0;
3824 }
3825 
3826 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3827 {
3828 	int i, rc;
3829 
3830 	bp->max_tpa = MAX_TPA;
3831 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3832 		if (!bp->max_tpa_v2)
3833 			return 0;
3834 		bp->max_tpa = min_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3835 		/* Older P5 FW sets max_tpa_v2 low by mistake except NPAR */
3836 		if (bp->max_tpa <= 32 && BNXT_CHIP_P5(bp) && !BNXT_NPAR(bp))
3837 			bp->max_tpa = MAX_TPA_P5;
3838 	}
3839 
3840 	for (i = 0; i < bp->rx_nr_rings; i++) {
3841 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3842 
3843 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3844 		if (rc)
3845 			return rc;
3846 	}
3847 	return 0;
3848 }
3849 
3850 static void bnxt_free_rx_rings(struct bnxt *bp)
3851 {
3852 	int i;
3853 
3854 	if (!bp->rx_ring)
3855 		return;
3856 
3857 	bnxt_free_tpa_info(bp);
3858 	for (i = 0; i < bp->rx_nr_rings; i++) {
3859 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3860 		struct bnxt_ring_struct *ring;
3861 
3862 		if (rxr->xdp_prog)
3863 			bpf_prog_put(rxr->xdp_prog);
3864 
3865 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3866 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3867 
3868 		page_pool_destroy(rxr->page_pool);
3869 		page_pool_destroy(rxr->head_pool);
3870 		rxr->page_pool = rxr->head_pool = NULL;
3871 
3872 		kfree(rxr->rx_agg_bmap);
3873 		rxr->rx_agg_bmap = NULL;
3874 
3875 		ring = &rxr->rx_ring_struct;
3876 		bnxt_free_ring(bp, &ring->ring_mem);
3877 
3878 		ring = &rxr->rx_agg_ring_struct;
3879 		bnxt_free_ring(bp, &ring->ring_mem);
3880 	}
3881 }
3882 
3883 static int bnxt_rx_agg_ring_fill_level(struct bnxt *bp,
3884 				       struct bnxt_rx_ring_info *rxr)
3885 {
3886 	/* User may have chosen larger than default rx_page_size,
3887 	 * we keep the ring sizes uniform and also want uniform amount
3888 	 * of bytes consumed per ring, so cap how much of the rings we fill.
3889 	 */
3890 	int fill_level = bp->rx_agg_ring_size;
3891 
3892 	if (rxr->rx_page_size > BNXT_RX_PAGE_SIZE)
3893 		fill_level /= rxr->rx_page_size / BNXT_RX_PAGE_SIZE;
3894 
3895 	return fill_level;
3896 }
3897 
3898 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3899 				   struct bnxt_rx_ring_info *rxr,
3900 				   int numa_node)
3901 {
3902 	unsigned int agg_size_fac = rxr->rx_page_size / BNXT_RX_PAGE_SIZE;
3903 	const unsigned int rx_size_fac = PAGE_SIZE / SZ_4K;
3904 	struct page_pool_params pp = { 0 };
3905 	struct page_pool *pool;
3906 
3907 	pp.pool_size = bnxt_rx_agg_ring_fill_level(bp, rxr) / agg_size_fac;
3908 	if (BNXT_RX_PAGE_MODE(bp))
3909 		pp.pool_size += bp->rx_ring_size / rx_size_fac;
3910 
3911 	pp.order = get_order(rxr->rx_page_size);
3912 	pp.nid = numa_node;
3913 	pp.netdev = bp->dev;
3914 	pp.dev = &bp->pdev->dev;
3915 	pp.dma_dir = bp->rx_dir;
3916 	pp.max_len = PAGE_SIZE << pp.order;
3917 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV |
3918 		   PP_FLAG_ALLOW_UNREADABLE_NETMEM;
3919 	pp.queue_idx = rxr->bnapi->index;
3920 
3921 	pool = page_pool_create(&pp);
3922 	if (IS_ERR(pool))
3923 		return PTR_ERR(pool);
3924 	rxr->page_pool = pool;
3925 
3926 	rxr->need_head_pool = page_pool_is_unreadable(pool);
3927 	rxr->need_head_pool |= !!pp.order;
3928 	if (bnxt_separate_head_pool(rxr)) {
3929 		pp.order = 0;
3930 		pp.max_len = PAGE_SIZE;
3931 		pp.pool_size = min(bp->rx_ring_size / rx_size_fac, 1024);
3932 		pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3933 		pool = page_pool_create(&pp);
3934 		if (IS_ERR(pool))
3935 			goto err_destroy_pp;
3936 	} else {
3937 		page_pool_get(pool);
3938 	}
3939 	rxr->head_pool = pool;
3940 
3941 	return 0;
3942 
3943 err_destroy_pp:
3944 	page_pool_destroy(rxr->page_pool);
3945 	rxr->page_pool = NULL;
3946 	return PTR_ERR(pool);
3947 }
3948 
3949 static void bnxt_enable_rx_page_pool(struct bnxt_rx_ring_info *rxr)
3950 {
3951 	page_pool_enable_direct_recycling(rxr->head_pool, &rxr->bnapi->napi);
3952 	page_pool_enable_direct_recycling(rxr->page_pool, &rxr->bnapi->napi);
3953 }
3954 
3955 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3956 {
3957 	u16 mem_size;
3958 
3959 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3960 	mem_size = rxr->rx_agg_bmap_size / 8;
3961 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3962 	if (!rxr->rx_agg_bmap)
3963 		return -ENOMEM;
3964 
3965 	return 0;
3966 }
3967 
3968 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3969 {
3970 	int numa_node = dev_to_node(&bp->pdev->dev);
3971 	int i, rc = 0, agg_rings = 0, cpu;
3972 
3973 	if (!bp->rx_ring)
3974 		return -ENOMEM;
3975 
3976 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3977 		agg_rings = 1;
3978 
3979 	for (i = 0; i < bp->rx_nr_rings; i++) {
3980 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3981 		struct bnxt_ring_struct *ring;
3982 		int cpu_node;
3983 
3984 		ring = &rxr->rx_ring_struct;
3985 
3986 		cpu = cpumask_local_spread(i, numa_node);
3987 		cpu_node = cpu_to_node(cpu);
3988 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3989 			   i, cpu_node);
3990 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3991 		if (rc)
3992 			return rc;
3993 		bnxt_enable_rx_page_pool(rxr);
3994 
3995 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3996 		if (rc < 0)
3997 			return rc;
3998 
3999 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
4000 						MEM_TYPE_PAGE_POOL,
4001 						rxr->page_pool);
4002 		if (rc) {
4003 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
4004 			return rc;
4005 		}
4006 
4007 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4008 		if (rc)
4009 			return rc;
4010 
4011 		ring->grp_idx = i;
4012 		if (agg_rings) {
4013 			ring = &rxr->rx_agg_ring_struct;
4014 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4015 			if (rc)
4016 				return rc;
4017 
4018 			ring->grp_idx = i;
4019 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
4020 			if (rc)
4021 				return rc;
4022 		}
4023 	}
4024 	if (bp->flags & BNXT_FLAG_TPA)
4025 		rc = bnxt_alloc_tpa_info(bp);
4026 	return rc;
4027 }
4028 
4029 static void bnxt_free_tx_inline_buf(struct bnxt_tx_ring_info *txr,
4030 				    struct pci_dev *pdev)
4031 {
4032 	if (!txr->tx_inline_buf)
4033 		return;
4034 
4035 	dma_unmap_single(&pdev->dev, txr->tx_inline_dma,
4036 			 txr->tx_inline_size, DMA_TO_DEVICE);
4037 	kfree(txr->tx_inline_buf);
4038 	txr->tx_inline_buf = NULL;
4039 	txr->tx_inline_size = 0;
4040 }
4041 
4042 static int bnxt_alloc_tx_inline_buf(struct bnxt_tx_ring_info *txr,
4043 				    struct pci_dev *pdev,
4044 				    unsigned int size)
4045 {
4046 	txr->tx_inline_buf = kmalloc(size, GFP_KERNEL);
4047 	if (!txr->tx_inline_buf)
4048 		return -ENOMEM;
4049 
4050 	txr->tx_inline_dma = dma_map_single(&pdev->dev, txr->tx_inline_buf,
4051 					    size, DMA_TO_DEVICE);
4052 	if (dma_mapping_error(&pdev->dev, txr->tx_inline_dma)) {
4053 		kfree(txr->tx_inline_buf);
4054 		txr->tx_inline_buf = NULL;
4055 		return -ENOMEM;
4056 	}
4057 	txr->tx_inline_size = size;
4058 
4059 	return 0;
4060 }
4061 
4062 static void bnxt_free_tx_rings(struct bnxt *bp)
4063 {
4064 	int i;
4065 	struct pci_dev *pdev = bp->pdev;
4066 
4067 	if (!bp->tx_ring)
4068 		return;
4069 
4070 	for (i = 0; i < bp->tx_nr_rings; i++) {
4071 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4072 		struct bnxt_ring_struct *ring;
4073 
4074 		if (txr->tx_push) {
4075 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
4076 					  txr->tx_push, txr->tx_push_mapping);
4077 			txr->tx_push = NULL;
4078 		}
4079 
4080 		bnxt_free_tx_inline_buf(txr, pdev);
4081 
4082 		ring = &txr->tx_ring_struct;
4083 
4084 		bnxt_free_ring(bp, &ring->ring_mem);
4085 	}
4086 }
4087 
4088 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
4089 	((tc) * (bp)->tx_nr_rings_per_tc)
4090 
4091 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
4092 	((tx) % (bp)->tx_nr_rings_per_tc)
4093 
4094 #define BNXT_RING_TO_TC(bp, tx)		\
4095 	((tx) / (bp)->tx_nr_rings_per_tc)
4096 
4097 static int bnxt_alloc_tx_rings(struct bnxt *bp)
4098 {
4099 	int i, j, rc;
4100 	struct pci_dev *pdev = bp->pdev;
4101 
4102 	bp->tx_push_size = 0;
4103 	if (bp->tx_push_thresh) {
4104 		int push_size;
4105 
4106 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
4107 					bp->tx_push_thresh);
4108 
4109 		if (push_size > 256) {
4110 			push_size = 0;
4111 			bp->tx_push_thresh = 0;
4112 		}
4113 
4114 		bp->tx_push_size = push_size;
4115 	}
4116 
4117 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
4118 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4119 		struct bnxt_ring_struct *ring;
4120 		u8 qidx;
4121 
4122 		ring = &txr->tx_ring_struct;
4123 
4124 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4125 		if (rc)
4126 			return rc;
4127 
4128 		ring->grp_idx = txr->bnapi->index;
4129 		if (bp->tx_push_size) {
4130 			dma_addr_t mapping;
4131 
4132 			/* One pre-allocated DMA buffer to backup
4133 			 * TX push operation
4134 			 */
4135 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
4136 						bp->tx_push_size,
4137 						&txr->tx_push_mapping,
4138 						GFP_KERNEL);
4139 
4140 			if (!txr->tx_push)
4141 				return -ENOMEM;
4142 
4143 			mapping = txr->tx_push_mapping +
4144 				sizeof(struct tx_push_bd);
4145 			txr->data_mapping = cpu_to_le64(mapping);
4146 		}
4147 		if (!(bp->flags & BNXT_FLAG_UDP_GSO_CAP)) {
4148 			rc = bnxt_alloc_tx_inline_buf(txr, pdev,
4149 						      BNXT_SW_USO_MAX_SEGS *
4150 						      TSO_HEADER_SIZE);
4151 			if (rc)
4152 				return rc;
4153 		}
4154 		qidx = bp->tc_to_qidx[j];
4155 		ring->queue_id = bp->q_info[qidx].queue_id;
4156 		spin_lock_init(&txr->xdp_tx_lock);
4157 		if (i < bp->tx_nr_rings_xdp)
4158 			continue;
4159 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
4160 			j++;
4161 	}
4162 	return 0;
4163 }
4164 
4165 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
4166 {
4167 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4168 
4169 	kfree(cpr->cp_desc_ring);
4170 	cpr->cp_desc_ring = NULL;
4171 	ring->ring_mem.pg_arr = NULL;
4172 	kfree(cpr->cp_desc_mapping);
4173 	cpr->cp_desc_mapping = NULL;
4174 	ring->ring_mem.dma_arr = NULL;
4175 }
4176 
4177 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
4178 {
4179 	cpr->cp_desc_ring = kzalloc_objs(*cpr->cp_desc_ring, n);
4180 	if (!cpr->cp_desc_ring)
4181 		return -ENOMEM;
4182 	cpr->cp_desc_mapping = kzalloc_objs(*cpr->cp_desc_mapping, n);
4183 	if (!cpr->cp_desc_mapping)
4184 		return -ENOMEM;
4185 	return 0;
4186 }
4187 
4188 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
4189 {
4190 	int i;
4191 
4192 	if (!bp->bnapi)
4193 		return;
4194 	for (i = 0; i < bp->cp_nr_rings; i++) {
4195 		struct bnxt_napi *bnapi = bp->bnapi[i];
4196 
4197 		if (!bnapi)
4198 			continue;
4199 		bnxt_free_cp_arrays(&bnapi->cp_ring);
4200 	}
4201 }
4202 
4203 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
4204 {
4205 	int i, n = bp->cp_nr_pages;
4206 
4207 	for (i = 0; i < bp->cp_nr_rings; i++) {
4208 		struct bnxt_napi *bnapi = bp->bnapi[i];
4209 		int rc;
4210 
4211 		if (!bnapi)
4212 			continue;
4213 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
4214 		if (rc)
4215 			return rc;
4216 	}
4217 	return 0;
4218 }
4219 
4220 static void bnxt_free_cp_rings(struct bnxt *bp)
4221 {
4222 	int i;
4223 
4224 	if (!bp->bnapi)
4225 		return;
4226 
4227 	for (i = 0; i < bp->cp_nr_rings; i++) {
4228 		struct bnxt_napi *bnapi = bp->bnapi[i];
4229 		struct bnxt_cp_ring_info *cpr;
4230 		struct bnxt_ring_struct *ring;
4231 		int j;
4232 
4233 		if (!bnapi)
4234 			continue;
4235 
4236 		cpr = &bnapi->cp_ring;
4237 		ring = &cpr->cp_ring_struct;
4238 
4239 		bnxt_free_ring(bp, &ring->ring_mem);
4240 
4241 		if (!cpr->cp_ring_arr)
4242 			continue;
4243 
4244 		for (j = 0; j < cpr->cp_ring_count; j++) {
4245 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4246 
4247 			ring = &cpr2->cp_ring_struct;
4248 			bnxt_free_ring(bp, &ring->ring_mem);
4249 			bnxt_free_cp_arrays(cpr2);
4250 		}
4251 		kfree(cpr->cp_ring_arr);
4252 		cpr->cp_ring_arr = NULL;
4253 		cpr->cp_ring_count = 0;
4254 	}
4255 }
4256 
4257 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4258 				  struct bnxt_cp_ring_info *cpr)
4259 {
4260 	struct bnxt_ring_mem_info *rmem;
4261 	struct bnxt_ring_struct *ring;
4262 	int rc;
4263 
4264 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4265 	if (rc) {
4266 		bnxt_free_cp_arrays(cpr);
4267 		return -ENOMEM;
4268 	}
4269 	ring = &cpr->cp_ring_struct;
4270 	rmem = &ring->ring_mem;
4271 	rmem->nr_pages = bp->cp_nr_pages;
4272 	rmem->page_size = HW_CMPD_RING_SIZE;
4273 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4274 	rmem->dma_arr = cpr->cp_desc_mapping;
4275 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4276 	rc = bnxt_alloc_ring(bp, rmem);
4277 	if (rc) {
4278 		bnxt_free_ring(bp, rmem);
4279 		bnxt_free_cp_arrays(cpr);
4280 	}
4281 	return rc;
4282 }
4283 
4284 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4285 {
4286 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4287 	int i, j, rc, ulp_msix;
4288 	int tcs = bp->num_tc;
4289 
4290 	if (!tcs)
4291 		tcs = 1;
4292 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4293 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4294 		struct bnxt_napi *bnapi = bp->bnapi[i];
4295 		struct bnxt_cp_ring_info *cpr, *cpr2;
4296 		struct bnxt_ring_struct *ring;
4297 		int cp_count = 0, k;
4298 		int rx = 0, tx = 0;
4299 
4300 		if (!bnapi)
4301 			continue;
4302 
4303 		cpr = &bnapi->cp_ring;
4304 		cpr->bnapi = bnapi;
4305 		ring = &cpr->cp_ring_struct;
4306 
4307 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4308 		if (rc)
4309 			return rc;
4310 
4311 		ring->map_idx = ulp_msix + i;
4312 
4313 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4314 			continue;
4315 
4316 		if (i < bp->rx_nr_rings) {
4317 			cp_count++;
4318 			rx = 1;
4319 		}
4320 		if (i < bp->tx_nr_rings_xdp) {
4321 			cp_count++;
4322 			tx = 1;
4323 		} else if ((sh && i < bp->tx_nr_rings) ||
4324 			 (!sh && i >= bp->rx_nr_rings)) {
4325 			cp_count += tcs;
4326 			tx = 1;
4327 		}
4328 
4329 		cpr->cp_ring_arr = kzalloc_objs(*cpr, cp_count);
4330 		if (!cpr->cp_ring_arr)
4331 			return -ENOMEM;
4332 		cpr->cp_ring_count = cp_count;
4333 
4334 		for (k = 0; k < cp_count; k++) {
4335 			cpr2 = &cpr->cp_ring_arr[k];
4336 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4337 			if (rc)
4338 				return rc;
4339 			cpr2->bnapi = bnapi;
4340 			cpr2->sw_stats = cpr->sw_stats;
4341 			cpr2->cp_idx = k;
4342 			if (!k && rx) {
4343 				bp->rx_ring[i].rx_cpr = cpr2;
4344 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4345 			} else {
4346 				int n, tc = k - rx;
4347 
4348 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4349 				bp->tx_ring[n].tx_cpr = cpr2;
4350 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4351 			}
4352 		}
4353 		if (tx)
4354 			j++;
4355 	}
4356 	return 0;
4357 }
4358 
4359 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4360 				     struct bnxt_rx_ring_info *rxr)
4361 {
4362 	struct bnxt_ring_mem_info *rmem;
4363 	struct bnxt_ring_struct *ring;
4364 
4365 	ring = &rxr->rx_ring_struct;
4366 	rmem = &ring->ring_mem;
4367 	rmem->nr_pages = bp->rx_nr_pages;
4368 	rmem->page_size = HW_RXBD_RING_SIZE;
4369 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4370 	rmem->dma_arr = rxr->rx_desc_mapping;
4371 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4372 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4373 
4374 	ring = &rxr->rx_agg_ring_struct;
4375 	rmem = &ring->ring_mem;
4376 	rmem->nr_pages = bp->rx_agg_nr_pages;
4377 	rmem->page_size = HW_RXBD_RING_SIZE;
4378 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4379 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4380 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4381 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4382 }
4383 
4384 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4385 				      struct bnxt_rx_ring_info *rxr)
4386 {
4387 	struct bnxt_ring_mem_info *rmem;
4388 	struct bnxt_ring_struct *ring;
4389 	int i;
4390 
4391 	rxr->page_pool->p.napi = NULL;
4392 	rxr->page_pool = NULL;
4393 	rxr->head_pool->p.napi = NULL;
4394 	rxr->head_pool = NULL;
4395 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4396 
4397 	ring = &rxr->rx_ring_struct;
4398 	rmem = &ring->ring_mem;
4399 	rmem->pg_tbl = NULL;
4400 	rmem->pg_tbl_map = 0;
4401 	for (i = 0; i < rmem->nr_pages; i++) {
4402 		rmem->pg_arr[i] = NULL;
4403 		rmem->dma_arr[i] = 0;
4404 	}
4405 	*rmem->vmem = NULL;
4406 
4407 	ring = &rxr->rx_agg_ring_struct;
4408 	rmem = &ring->ring_mem;
4409 	rmem->pg_tbl = NULL;
4410 	rmem->pg_tbl_map = 0;
4411 	for (i = 0; i < rmem->nr_pages; i++) {
4412 		rmem->pg_arr[i] = NULL;
4413 		rmem->dma_arr[i] = 0;
4414 	}
4415 	*rmem->vmem = NULL;
4416 }
4417 
4418 static void bnxt_init_ring_struct(struct bnxt *bp)
4419 {
4420 	int i, j;
4421 
4422 	for (i = 0; i < bp->cp_nr_rings; i++) {
4423 		struct bnxt_napi *bnapi = bp->bnapi[i];
4424 		struct netdev_queue_config qcfg;
4425 		struct bnxt_ring_mem_info *rmem;
4426 		struct bnxt_cp_ring_info *cpr;
4427 		struct bnxt_rx_ring_info *rxr;
4428 		struct bnxt_tx_ring_info *txr;
4429 		struct bnxt_ring_struct *ring;
4430 
4431 		if (!bnapi)
4432 			continue;
4433 
4434 		cpr = &bnapi->cp_ring;
4435 		ring = &cpr->cp_ring_struct;
4436 		rmem = &ring->ring_mem;
4437 		rmem->nr_pages = bp->cp_nr_pages;
4438 		rmem->page_size = HW_CMPD_RING_SIZE;
4439 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4440 		rmem->dma_arr = cpr->cp_desc_mapping;
4441 		rmem->vmem_size = 0;
4442 
4443 		rxr = bnapi->rx_ring;
4444 		if (!rxr)
4445 			goto skip_rx;
4446 
4447 		netdev_queue_config(bp->dev, i, &qcfg);
4448 		rxr->rx_page_size = qcfg.rx_page_size;
4449 
4450 		ring = &rxr->rx_ring_struct;
4451 		rmem = &ring->ring_mem;
4452 		rmem->nr_pages = bp->rx_nr_pages;
4453 		rmem->page_size = HW_RXBD_RING_SIZE;
4454 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4455 		rmem->dma_arr = rxr->rx_desc_mapping;
4456 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4457 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4458 
4459 		ring = &rxr->rx_agg_ring_struct;
4460 		rmem = &ring->ring_mem;
4461 		rmem->nr_pages = bp->rx_agg_nr_pages;
4462 		rmem->page_size = HW_RXBD_RING_SIZE;
4463 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4464 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4465 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4466 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4467 
4468 skip_rx:
4469 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4470 			ring = &txr->tx_ring_struct;
4471 			rmem = &ring->ring_mem;
4472 			rmem->nr_pages = bp->tx_nr_pages;
4473 			rmem->page_size = HW_TXBD_RING_SIZE;
4474 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4475 			rmem->dma_arr = txr->tx_desc_mapping;
4476 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4477 			rmem->vmem = (void **)&txr->tx_buf_ring;
4478 		}
4479 	}
4480 }
4481 
4482 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4483 {
4484 	int i;
4485 	u32 prod;
4486 	struct rx_bd **rx_buf_ring;
4487 
4488 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4489 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4490 		int j;
4491 		struct rx_bd *rxbd;
4492 
4493 		rxbd = rx_buf_ring[i];
4494 		if (!rxbd)
4495 			continue;
4496 
4497 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4498 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4499 			rxbd->rx_bd_opaque = prod;
4500 		}
4501 	}
4502 }
4503 
4504 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4505 				       struct bnxt_rx_ring_info *rxr,
4506 				       int ring_nr)
4507 {
4508 	u32 prod;
4509 	int i;
4510 
4511 	prod = rxr->rx_prod;
4512 	for (i = 0; i < bp->rx_ring_size; i++) {
4513 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4514 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4515 				    ring_nr, i, bp->rx_ring_size);
4516 			break;
4517 		}
4518 		prod = NEXT_RX(prod);
4519 	}
4520 	rxr->rx_prod = prod;
4521 }
4522 
4523 static void bnxt_alloc_one_rx_ring_netmem(struct bnxt *bp,
4524 					  struct bnxt_rx_ring_info *rxr,
4525 					  int ring_nr)
4526 {
4527 	int fill_level, i;
4528 	u32 prod;
4529 
4530 	fill_level = bnxt_rx_agg_ring_fill_level(bp, rxr);
4531 
4532 	prod = rxr->rx_agg_prod;
4533 	for (i = 0; i < fill_level; i++) {
4534 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_KERNEL)) {
4535 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4536 				    ring_nr, i, bp->rx_agg_ring_size);
4537 			break;
4538 		}
4539 		prod = NEXT_RX_AGG(prod);
4540 	}
4541 	rxr->rx_agg_prod = prod;
4542 }
4543 
4544 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4545 					struct bnxt_rx_ring_info *rxr)
4546 {
4547 	dma_addr_t mapping;
4548 	u8 *data;
4549 	int i;
4550 
4551 	for (i = 0; i < bp->max_tpa; i++) {
4552 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4553 					    GFP_KERNEL);
4554 		if (!data)
4555 			return -ENOMEM;
4556 
4557 		rxr->rx_tpa[i].data = data;
4558 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4559 		rxr->rx_tpa[i].mapping = mapping;
4560 	}
4561 
4562 	return 0;
4563 }
4564 
4565 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4566 {
4567 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4568 	int rc;
4569 
4570 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4571 
4572 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4573 		return 0;
4574 
4575 	bnxt_alloc_one_rx_ring_netmem(bp, rxr, ring_nr);
4576 
4577 	if (rxr->rx_tpa) {
4578 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4579 		if (rc)
4580 			return rc;
4581 	}
4582 	return 0;
4583 }
4584 
4585 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4586 				       struct bnxt_rx_ring_info *rxr)
4587 {
4588 	struct bnxt_ring_struct *ring;
4589 	u32 type;
4590 
4591 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4592 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4593 
4594 	if (NET_IP_ALIGN == 2)
4595 		type |= RX_BD_FLAGS_SOP;
4596 
4597 	ring = &rxr->rx_ring_struct;
4598 	bnxt_init_rxbd_pages(ring, type);
4599 	ring->fw_ring_id = INVALID_HW_RING_ID;
4600 }
4601 
4602 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4603 					   struct bnxt_rx_ring_info *rxr)
4604 {
4605 	struct bnxt_ring_struct *ring;
4606 	u32 type;
4607 
4608 	ring = &rxr->rx_agg_ring_struct;
4609 	ring->fw_ring_id = INVALID_HW_RING_ID;
4610 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4611 		type = ((u32)rxr->rx_page_size << RX_BD_LEN_SHIFT) |
4612 			RX_BD_TYPE_RX_AGG_BD;
4613 
4614 		/* Disable EOP if TPA is enabled to prevent overlapping zero
4615 		 * padding with the next segment's data.  On P7_PLUS, EOP will
4616 		 * automatically disable Relaxed Ordering (RO) to prevent
4617 		 * potential data corruption (and may degrade performance).  On
4618 		 * older chips, RO will not be automatically disabled and may
4619 		 * cause corruption.
4620 		 */
4621 		if (!(bp->flags & BNXT_FLAG_TPA))
4622 			type |= RX_BD_FLAGS_AGG_EOP;
4623 
4624 		bnxt_init_rxbd_pages(ring, type);
4625 	}
4626 }
4627 
4628 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4629 {
4630 	struct bnxt_rx_ring_info *rxr;
4631 
4632 	rxr = &bp->rx_ring[ring_nr];
4633 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4634 
4635 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4636 			     &rxr->bnapi->napi);
4637 
4638 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4639 		bpf_prog_add(bp->xdp_prog, 1);
4640 		rxr->xdp_prog = bp->xdp_prog;
4641 	}
4642 
4643 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4644 
4645 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4646 }
4647 
4648 static void bnxt_init_cp_rings(struct bnxt *bp)
4649 {
4650 	int i, j;
4651 
4652 	for (i = 0; i < bp->cp_nr_rings; i++) {
4653 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4654 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4655 
4656 		ring->fw_ring_id = INVALID_HW_RING_ID;
4657 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4658 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4659 		if (!cpr->cp_ring_arr)
4660 			continue;
4661 		for (j = 0; j < cpr->cp_ring_count; j++) {
4662 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4663 
4664 			ring = &cpr2->cp_ring_struct;
4665 			ring->fw_ring_id = INVALID_HW_RING_ID;
4666 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4667 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4668 		}
4669 	}
4670 }
4671 
4672 static int bnxt_init_rx_rings(struct bnxt *bp)
4673 {
4674 	int i, rc = 0;
4675 
4676 	if (BNXT_RX_PAGE_MODE(bp)) {
4677 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4678 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4679 	} else {
4680 		bp->rx_offset = BNXT_RX_OFFSET;
4681 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4682 	}
4683 
4684 	for (i = 0; i < bp->rx_nr_rings; i++) {
4685 		rc = bnxt_init_one_rx_ring(bp, i);
4686 		if (rc)
4687 			break;
4688 	}
4689 
4690 	return rc;
4691 }
4692 
4693 static int bnxt_init_tx_rings(struct bnxt *bp)
4694 {
4695 	netdev_features_t features;
4696 	u16 i;
4697 
4698 	features = bp->dev->features;
4699 
4700 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4701 				   bnxt_min_tx_desc_cnt(bp, features));
4702 
4703 	for (i = 0; i < bp->tx_nr_rings; i++) {
4704 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4705 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4706 
4707 		ring->fw_ring_id = INVALID_HW_RING_ID;
4708 
4709 		if (i >= bp->tx_nr_rings_xdp)
4710 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4711 					     NETDEV_QUEUE_TYPE_TX,
4712 					     &txr->bnapi->napi);
4713 	}
4714 
4715 	return 0;
4716 }
4717 
4718 static void bnxt_free_ring_grps(struct bnxt *bp)
4719 {
4720 	kfree(bp->grp_info);
4721 	bp->grp_info = NULL;
4722 }
4723 
4724 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4725 {
4726 	int i;
4727 
4728 	if (irq_re_init) {
4729 		bp->grp_info = kzalloc_objs(struct bnxt_ring_grp_info,
4730 					    bp->cp_nr_rings);
4731 		if (!bp->grp_info)
4732 			return -ENOMEM;
4733 	}
4734 	for (i = 0; i < bp->cp_nr_rings; i++) {
4735 		if (irq_re_init)
4736 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4737 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4738 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4739 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4740 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4741 	}
4742 	return 0;
4743 }
4744 
4745 static void bnxt_free_vnics(struct bnxt *bp)
4746 {
4747 	kfree(bp->vnic_info);
4748 	bp->vnic_info = NULL;
4749 	bp->nr_vnics = 0;
4750 }
4751 
4752 static int bnxt_alloc_vnics(struct bnxt *bp)
4753 {
4754 	int num_vnics = 1;
4755 
4756 #ifdef CONFIG_RFS_ACCEL
4757 	if (bp->flags & BNXT_FLAG_RFS) {
4758 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4759 			num_vnics++;
4760 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4761 			num_vnics += bp->rx_nr_rings;
4762 	}
4763 #endif
4764 
4765 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4766 		num_vnics++;
4767 
4768 	bp->vnic_info = kzalloc_objs(struct bnxt_vnic_info, num_vnics);
4769 	if (!bp->vnic_info)
4770 		return -ENOMEM;
4771 
4772 	bp->nr_vnics = num_vnics;
4773 	return 0;
4774 }
4775 
4776 static void bnxt_init_vnics(struct bnxt *bp)
4777 {
4778 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4779 	int i;
4780 
4781 	for (i = 0; i < bp->nr_vnics; i++) {
4782 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4783 		int j;
4784 
4785 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4786 		vnic->vnic_id = i;
4787 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4788 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4789 
4790 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4791 
4792 		if (bp->vnic_info[i].rss_hash_key) {
4793 			if (i == BNXT_VNIC_DEFAULT) {
4794 				u8 *key = (void *)vnic->rss_hash_key;
4795 				int k;
4796 
4797 				if (!bp->rss_hash_key_valid &&
4798 				    !bp->rss_hash_key_updated) {
4799 					get_random_bytes(bp->rss_hash_key,
4800 							 HW_HASH_KEY_SIZE);
4801 					bp->rss_hash_key_updated = true;
4802 				}
4803 
4804 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4805 				       HW_HASH_KEY_SIZE);
4806 
4807 				if (!bp->rss_hash_key_updated)
4808 					continue;
4809 
4810 				bp->rss_hash_key_updated = false;
4811 				bp->rss_hash_key_valid = true;
4812 
4813 				bp->toeplitz_prefix = 0;
4814 				for (k = 0; k < 8; k++) {
4815 					bp->toeplitz_prefix <<= 8;
4816 					bp->toeplitz_prefix |= key[k];
4817 				}
4818 			} else {
4819 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4820 				       HW_HASH_KEY_SIZE);
4821 			}
4822 		}
4823 	}
4824 }
4825 
4826 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4827 {
4828 	int pages;
4829 
4830 	pages = ring_size / desc_per_pg;
4831 
4832 	if (!pages)
4833 		return 1;
4834 
4835 	pages++;
4836 
4837 	while (pages & (pages - 1))
4838 		pages++;
4839 
4840 	return pages;
4841 }
4842 
4843 void bnxt_set_tpa_flags(struct bnxt *bp)
4844 {
4845 	bp->flags &= ~BNXT_FLAG_TPA;
4846 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4847 		return;
4848 	if (bp->dev->features & NETIF_F_LRO)
4849 		bp->flags |= BNXT_FLAG_LRO;
4850 	else if (bp->dev->features & NETIF_F_GRO_HW)
4851 		bp->flags |= BNXT_FLAG_GRO;
4852 }
4853 
4854 static void bnxt_init_ring_params(struct bnxt *bp)
4855 {
4856 	unsigned int rx_size;
4857 
4858 	bp->rx_copybreak = 0; /* rx-copybreak disabled by default */
4859 	/* Try to fit 4 chunks into a 4k page */
4860 	rx_size = SZ_1K -
4861 		NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4862 	bp->dev->cfg->hds_thresh = max(BNXT_MIN_RX_HDR_BUF, rx_size);
4863 }
4864 
4865 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4866  * be set on entry.
4867  */
4868 void bnxt_set_ring_params(struct bnxt *bp)
4869 {
4870 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4871 	u32 agg_factor = 0, agg_ring_size = 0;
4872 
4873 	/* 8 for CRC and VLAN */
4874 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4875 
4876 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4877 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4878 
4879 	ring_size = bp->rx_ring_size;
4880 	bp->rx_agg_ring_size = 0;
4881 	bp->rx_agg_nr_pages = 0;
4882 
4883 	if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS)
4884 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4885 
4886 	bp->flags &= ~BNXT_FLAG_JUMBO;
4887 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4888 		u32 jumbo_factor;
4889 
4890 		bp->flags |= BNXT_FLAG_JUMBO;
4891 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4892 		if (jumbo_factor > agg_factor)
4893 			agg_factor = jumbo_factor;
4894 	}
4895 	if (agg_factor) {
4896 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4897 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4898 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4899 				    bp->rx_ring_size, ring_size);
4900 			bp->rx_ring_size = ring_size;
4901 		}
4902 		agg_ring_size = ring_size * agg_factor;
4903 
4904 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4905 							RX_DESC_CNT);
4906 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4907 			u32 tmp = agg_ring_size;
4908 
4909 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4910 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4911 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4912 				    tmp, agg_ring_size);
4913 		}
4914 		bp->rx_agg_ring_size = agg_ring_size;
4915 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4916 
4917 		if (BNXT_RX_PAGE_MODE(bp)) {
4918 			rx_space = PAGE_SIZE;
4919 			rx_size = PAGE_SIZE -
4920 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4921 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4922 		} else {
4923 			rx_size = max3(BNXT_MIN_RX_HDR_BUF,
4924 				       bp->rx_copybreak,
4925 				       bp->dev->cfg_pending->hds_thresh);
4926 			rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN);
4927 			rx_space = rx_size + NET_SKB_PAD +
4928 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4929 		}
4930 	}
4931 
4932 	bp->rx_buf_use_size = rx_size;
4933 	bp->rx_buf_size = rx_space;
4934 
4935 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4936 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4937 
4938 	ring_size = bp->tx_ring_size;
4939 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4940 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4941 
4942 	max_rx_cmpl = bp->rx_ring_size;
4943 	/* MAX TPA needs to be added because TPA_START completions are
4944 	 * immediately recycled, so the TPA completions are not bound by
4945 	 * the RX ring size.
4946 	 */
4947 	if (bp->flags & BNXT_FLAG_TPA)
4948 		max_rx_cmpl += bp->max_tpa;
4949 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4950 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4951 	bp->cp_ring_size = ring_size;
4952 
4953 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4954 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4955 		bp->cp_nr_pages = MAX_CP_PAGES;
4956 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4957 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4958 			    ring_size, bp->cp_ring_size);
4959 	}
4960 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4961 	bp->cp_ring_mask = bp->cp_bit - 1;
4962 }
4963 
4964 /* Changing allocation mode of RX rings.
4965  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4966  */
4967 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4968 {
4969 	struct net_device *dev = bp->dev;
4970 
4971 	if (page_mode) {
4972 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
4973 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4974 
4975 		if (bp->xdp_prog->aux->xdp_has_frags)
4976 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4977 		else
4978 			dev->max_mtu =
4979 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4980 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4981 			bp->flags |= BNXT_FLAG_JUMBO;
4982 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4983 		} else {
4984 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4985 			bp->rx_skb_func = bnxt_rx_page_skb;
4986 		}
4987 		bp->rx_dir = DMA_BIDIRECTIONAL;
4988 	} else {
4989 		dev->max_mtu = bp->max_mtu;
4990 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4991 		bp->rx_dir = DMA_FROM_DEVICE;
4992 		bp->rx_skb_func = bnxt_rx_skb;
4993 	}
4994 }
4995 
4996 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4997 {
4998 	__bnxt_set_rx_skb_mode(bp, page_mode);
4999 
5000 	if (!page_mode) {
5001 		int rx, tx;
5002 
5003 		bnxt_get_max_rings(bp, &rx, &tx, true);
5004 		if (rx > 1) {
5005 			bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS;
5006 			bp->dev->hw_features |= NETIF_F_LRO;
5007 		}
5008 	}
5009 
5010 	/* Update LRO and GRO_HW availability */
5011 	netdev_update_features(bp->dev);
5012 }
5013 
5014 static void bnxt_free_vnic_attributes(struct bnxt *bp)
5015 {
5016 	int i;
5017 	struct bnxt_vnic_info *vnic;
5018 	struct pci_dev *pdev = bp->pdev;
5019 
5020 	if (!bp->vnic_info)
5021 		return;
5022 
5023 	for (i = 0; i < bp->nr_vnics; i++) {
5024 		vnic = &bp->vnic_info[i];
5025 
5026 		kfree(vnic->fw_grp_ids);
5027 		vnic->fw_grp_ids = NULL;
5028 
5029 		kfree(vnic->uc_list);
5030 		vnic->uc_list = NULL;
5031 
5032 		if (vnic->mc_list) {
5033 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
5034 					  vnic->mc_list, vnic->mc_list_mapping);
5035 			vnic->mc_list = NULL;
5036 		}
5037 
5038 		if (vnic->rss_table) {
5039 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
5040 					  vnic->rss_table,
5041 					  vnic->rss_table_dma_addr);
5042 			vnic->rss_table = NULL;
5043 		}
5044 
5045 		vnic->rss_hash_key = NULL;
5046 		vnic->flags = 0;
5047 	}
5048 }
5049 
5050 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
5051 {
5052 	int i, rc = 0, size;
5053 	struct bnxt_vnic_info *vnic;
5054 	struct pci_dev *pdev = bp->pdev;
5055 	int max_rings;
5056 
5057 	for (i = 0; i < bp->nr_vnics; i++) {
5058 		vnic = &bp->vnic_info[i];
5059 
5060 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
5061 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
5062 
5063 			if (mem_size > 0) {
5064 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
5065 				if (!vnic->uc_list) {
5066 					rc = -ENOMEM;
5067 					goto out;
5068 				}
5069 			}
5070 		}
5071 
5072 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
5073 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
5074 			vnic->mc_list =
5075 				dma_alloc_coherent(&pdev->dev,
5076 						   vnic->mc_list_size,
5077 						   &vnic->mc_list_mapping,
5078 						   GFP_KERNEL);
5079 			if (!vnic->mc_list) {
5080 				rc = -ENOMEM;
5081 				goto out;
5082 			}
5083 		}
5084 
5085 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5086 			goto vnic_skip_grps;
5087 
5088 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
5089 			max_rings = bp->rx_nr_rings;
5090 		else
5091 			max_rings = 1;
5092 
5093 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
5094 		if (!vnic->fw_grp_ids) {
5095 			rc = -ENOMEM;
5096 			goto out;
5097 		}
5098 vnic_skip_grps:
5099 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
5100 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
5101 			continue;
5102 
5103 		/* Allocate rss table and hash key */
5104 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
5105 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5106 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
5107 
5108 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
5109 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
5110 						     vnic->rss_table_size,
5111 						     &vnic->rss_table_dma_addr,
5112 						     GFP_KERNEL);
5113 		if (!vnic->rss_table) {
5114 			rc = -ENOMEM;
5115 			goto out;
5116 		}
5117 
5118 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
5119 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
5120 	}
5121 	return 0;
5122 
5123 out:
5124 	return rc;
5125 }
5126 
5127 static void bnxt_free_hwrm_resources(struct bnxt *bp)
5128 {
5129 	struct bnxt_hwrm_wait_token *token;
5130 
5131 	dma_pool_destroy(bp->hwrm_dma_pool);
5132 	bp->hwrm_dma_pool = NULL;
5133 
5134 	rcu_read_lock();
5135 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
5136 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
5137 	rcu_read_unlock();
5138 }
5139 
5140 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
5141 {
5142 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
5143 					    BNXT_HWRM_DMA_SIZE,
5144 					    BNXT_HWRM_DMA_ALIGN, 0);
5145 	if (!bp->hwrm_dma_pool)
5146 		return -ENOMEM;
5147 
5148 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
5149 
5150 	return 0;
5151 }
5152 
5153 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
5154 {
5155 	kfree(stats->hw_masks);
5156 	stats->hw_masks = NULL;
5157 	kfree(stats->sw_stats);
5158 	stats->sw_stats = NULL;
5159 	if (stats->hw_stats) {
5160 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
5161 				  stats->hw_stats_map);
5162 		stats->hw_stats = NULL;
5163 	}
5164 }
5165 
5166 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
5167 				bool alloc_masks)
5168 {
5169 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
5170 					     &stats->hw_stats_map, GFP_KERNEL);
5171 	if (!stats->hw_stats)
5172 		return -ENOMEM;
5173 
5174 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
5175 	if (!stats->sw_stats)
5176 		goto stats_mem_err;
5177 
5178 	if (alloc_masks) {
5179 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
5180 		if (!stats->hw_masks)
5181 			goto stats_mem_err;
5182 	}
5183 	return 0;
5184 
5185 stats_mem_err:
5186 	bnxt_free_stats_mem(bp, stats);
5187 	return -ENOMEM;
5188 }
5189 
5190 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
5191 {
5192 	int i;
5193 
5194 	for (i = 0; i < count; i++)
5195 		mask_arr[i] = mask;
5196 }
5197 
5198 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
5199 {
5200 	int i;
5201 
5202 	for (i = 0; i < count; i++)
5203 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
5204 }
5205 
5206 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
5207 				    struct bnxt_stats_mem *stats)
5208 {
5209 	struct hwrm_func_qstats_ext_output *resp;
5210 	struct hwrm_func_qstats_ext_input *req;
5211 	__le64 *hw_masks;
5212 	int rc;
5213 
5214 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
5215 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5216 		return -EOPNOTSUPP;
5217 
5218 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
5219 	if (rc)
5220 		return rc;
5221 
5222 	req->fid = cpu_to_le16(0xffff);
5223 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5224 
5225 	resp = hwrm_req_hold(bp, req);
5226 	rc = hwrm_req_send(bp, req);
5227 	if (!rc) {
5228 		hw_masks = &resp->rx_ucast_pkts;
5229 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
5230 	}
5231 	hwrm_req_drop(bp, req);
5232 	return rc;
5233 }
5234 
5235 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
5236 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
5237 
5238 static void bnxt_init_stats(struct bnxt *bp)
5239 {
5240 	struct bnxt_napi *bnapi = bp->bnapi[0];
5241 	struct bnxt_cp_ring_info *cpr;
5242 	struct bnxt_stats_mem *stats;
5243 	__le64 *rx_stats, *tx_stats;
5244 	int rc, rx_count, tx_count;
5245 	u64 *rx_masks, *tx_masks;
5246 	u64 mask;
5247 	u8 flags;
5248 
5249 	cpr = &bnapi->cp_ring;
5250 	stats = &cpr->stats;
5251 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
5252 	if (rc) {
5253 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5254 			mask = (1ULL << 48) - 1;
5255 		else
5256 			mask = -1ULL;
5257 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
5258 	}
5259 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
5260 		stats = &bp->port_stats;
5261 		rx_stats = stats->hw_stats;
5262 		rx_masks = stats->hw_masks;
5263 		rx_count = sizeof(struct rx_port_stats) / 8;
5264 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5265 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5266 		tx_count = sizeof(struct tx_port_stats) / 8;
5267 
5268 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
5269 		rc = bnxt_hwrm_port_qstats(bp, flags);
5270 		if (rc) {
5271 			mask = (1ULL << 40) - 1;
5272 
5273 			bnxt_fill_masks(rx_masks, mask, rx_count);
5274 			bnxt_fill_masks(tx_masks, mask, tx_count);
5275 		} else {
5276 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5277 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5278 			bnxt_hwrm_port_qstats(bp, 0);
5279 		}
5280 	}
5281 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5282 		stats = &bp->rx_port_stats_ext;
5283 		rx_stats = stats->hw_stats;
5284 		rx_masks = stats->hw_masks;
5285 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5286 		stats = &bp->tx_port_stats_ext;
5287 		tx_stats = stats->hw_stats;
5288 		tx_masks = stats->hw_masks;
5289 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5290 
5291 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5292 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5293 		if (rc) {
5294 			mask = (1ULL << 40) - 1;
5295 
5296 			bnxt_fill_masks(rx_masks, mask, rx_count);
5297 			if (tx_stats)
5298 				bnxt_fill_masks(tx_masks, mask, tx_count);
5299 		} else {
5300 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5301 			if (tx_stats)
5302 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5303 						   tx_count);
5304 			bnxt_hwrm_port_qstats_ext(bp, 0);
5305 		}
5306 	}
5307 }
5308 
5309 static void bnxt_free_port_stats(struct bnxt *bp)
5310 {
5311 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5312 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5313 
5314 	bnxt_free_stats_mem(bp, &bp->port_stats);
5315 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5316 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5317 }
5318 
5319 static void bnxt_free_ring_stats(struct bnxt *bp)
5320 {
5321 	int i;
5322 
5323 	if (!bp->bnapi)
5324 		return;
5325 
5326 	for (i = 0; i < bp->cp_nr_rings; i++) {
5327 		struct bnxt_napi *bnapi = bp->bnapi[i];
5328 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5329 
5330 		bnxt_free_stats_mem(bp, &cpr->stats);
5331 
5332 		kfree(cpr->sw_stats);
5333 		cpr->sw_stats = NULL;
5334 	}
5335 }
5336 
5337 static int bnxt_alloc_stats(struct bnxt *bp)
5338 {
5339 	u32 size, i;
5340 	int rc;
5341 
5342 	size = bp->hw_ring_stats_size;
5343 
5344 	for (i = 0; i < bp->cp_nr_rings; i++) {
5345 		struct bnxt_napi *bnapi = bp->bnapi[i];
5346 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5347 
5348 		cpr->sw_stats = kzalloc_obj(*cpr->sw_stats);
5349 		if (!cpr->sw_stats)
5350 			return -ENOMEM;
5351 
5352 		cpr->stats.len = size;
5353 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5354 		if (rc)
5355 			return rc;
5356 
5357 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5358 	}
5359 
5360 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5361 		return 0;
5362 
5363 	if (bp->port_stats.hw_stats)
5364 		goto alloc_ext_stats;
5365 
5366 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5367 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5368 	if (rc)
5369 		return rc;
5370 
5371 	bp->flags |= BNXT_FLAG_PORT_STATS;
5372 
5373 alloc_ext_stats:
5374 	/* Display extended statistics only if FW supports it */
5375 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5376 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5377 			return 0;
5378 
5379 	if (bp->rx_port_stats_ext.hw_stats)
5380 		goto alloc_tx_ext_stats;
5381 
5382 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5383 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5384 	/* Extended stats are optional */
5385 	if (rc)
5386 		return 0;
5387 
5388 alloc_tx_ext_stats:
5389 	if (bp->tx_port_stats_ext.hw_stats)
5390 		return 0;
5391 
5392 	if (bp->hwrm_spec_code >= 0x10902 ||
5393 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5394 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5395 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5396 		/* Extended stats are optional */
5397 		if (rc)
5398 			return 0;
5399 	}
5400 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5401 	return 0;
5402 }
5403 
5404 static void bnxt_clear_ring_indices(struct bnxt *bp)
5405 {
5406 	int i, j;
5407 
5408 	if (!bp->bnapi)
5409 		return;
5410 
5411 	for (i = 0; i < bp->cp_nr_rings; i++) {
5412 		struct bnxt_napi *bnapi = bp->bnapi[i];
5413 		struct bnxt_cp_ring_info *cpr;
5414 		struct bnxt_rx_ring_info *rxr;
5415 		struct bnxt_tx_ring_info *txr;
5416 
5417 		if (!bnapi)
5418 			continue;
5419 
5420 		cpr = &bnapi->cp_ring;
5421 		cpr->cp_raw_cons = 0;
5422 
5423 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5424 			txr->tx_prod = 0;
5425 			txr->tx_cons = 0;
5426 			txr->tx_hw_cons = 0;
5427 		}
5428 
5429 		rxr = bnapi->rx_ring;
5430 		if (rxr) {
5431 			rxr->rx_prod = 0;
5432 			rxr->rx_agg_prod = 0;
5433 			rxr->rx_sw_agg_prod = 0;
5434 			rxr->rx_next_cons = 0;
5435 		}
5436 		bnapi->events = 0;
5437 	}
5438 }
5439 
5440 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5441 {
5442 	u8 type = fltr->type, flags = fltr->flags;
5443 
5444 	INIT_LIST_HEAD(&fltr->list);
5445 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5446 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5447 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5448 }
5449 
5450 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5451 {
5452 	if (!list_empty(&fltr->list))
5453 		list_del_init(&fltr->list);
5454 }
5455 
5456 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5457 {
5458 	struct bnxt_filter_base *usr_fltr, *tmp;
5459 
5460 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5461 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5462 			continue;
5463 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5464 	}
5465 }
5466 
5467 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5468 {
5469 	hlist_del(&fltr->hash);
5470 	bnxt_del_one_usr_fltr(bp, fltr);
5471 	if (fltr->flags) {
5472 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5473 		bp->ntp_fltr_count--;
5474 	}
5475 	kfree(fltr);
5476 }
5477 
5478 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5479 {
5480 	int i;
5481 
5482 	netdev_assert_locked_or_invisible(bp->dev);
5483 
5484 	/* Under netdev instance lock and all our NAPIs have been disabled.
5485 	 * It's safe to delete the hash table.
5486 	 */
5487 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5488 		struct hlist_head *head;
5489 		struct hlist_node *tmp;
5490 		struct bnxt_ntuple_filter *fltr;
5491 
5492 		head = &bp->ntp_fltr_hash_tbl[i];
5493 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5494 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5495 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5496 				     !list_empty(&fltr->base.list)))
5497 				continue;
5498 			bnxt_del_fltr(bp, &fltr->base);
5499 		}
5500 	}
5501 	if (!all)
5502 		return;
5503 
5504 	bitmap_free(bp->ntp_fltr_bmap);
5505 	bp->ntp_fltr_bmap = NULL;
5506 	bp->ntp_fltr_count = 0;
5507 }
5508 
5509 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5510 {
5511 	int i, rc = 0;
5512 
5513 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5514 		return 0;
5515 
5516 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5517 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5518 
5519 	bp->ntp_fltr_count = 0;
5520 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5521 
5522 	if (!bp->ntp_fltr_bmap)
5523 		rc = -ENOMEM;
5524 
5525 	return rc;
5526 }
5527 
5528 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5529 {
5530 	int i;
5531 
5532 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5533 		struct hlist_head *head;
5534 		struct hlist_node *tmp;
5535 		struct bnxt_l2_filter *fltr;
5536 
5537 		head = &bp->l2_fltr_hash_tbl[i];
5538 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5539 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5540 				     !list_empty(&fltr->base.list)))
5541 				continue;
5542 			bnxt_del_fltr(bp, &fltr->base);
5543 		}
5544 	}
5545 }
5546 
5547 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5548 {
5549 	int i;
5550 
5551 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5552 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5553 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5554 }
5555 
5556 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5557 {
5558 	bnxt_free_vnic_attributes(bp);
5559 	bnxt_free_tx_rings(bp);
5560 	bnxt_free_rx_rings(bp);
5561 	bnxt_free_cp_rings(bp);
5562 	bnxt_free_all_cp_arrays(bp);
5563 	bnxt_free_ntp_fltrs(bp, false);
5564 	bnxt_free_l2_filters(bp, false);
5565 	if (irq_re_init) {
5566 		bnxt_free_ring_stats(bp);
5567 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5568 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5569 			bnxt_free_port_stats(bp);
5570 		bnxt_free_ring_grps(bp);
5571 		bnxt_free_vnics(bp);
5572 		kfree(bp->tx_ring_map);
5573 		bp->tx_ring_map = NULL;
5574 		kfree(bp->tx_ring);
5575 		bp->tx_ring = NULL;
5576 		kfree(bp->rx_ring);
5577 		bp->rx_ring = NULL;
5578 		kfree(bp->bnapi);
5579 		bp->bnapi = NULL;
5580 	} else {
5581 		bnxt_clear_ring_indices(bp);
5582 	}
5583 }
5584 
5585 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5586 {
5587 	int i, j, rc, size, arr_size;
5588 	void *bnapi;
5589 
5590 	if (irq_re_init) {
5591 		/* Allocate bnapi mem pointer array and mem block for
5592 		 * all queues
5593 		 */
5594 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5595 				bp->cp_nr_rings);
5596 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5597 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5598 		if (!bnapi)
5599 			return -ENOMEM;
5600 
5601 		bp->bnapi = bnapi;
5602 		bnapi += arr_size;
5603 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5604 			bp->bnapi[i] = bnapi;
5605 			bp->bnapi[i]->index = i;
5606 			bp->bnapi[i]->bp = bp;
5607 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5608 				struct bnxt_cp_ring_info *cpr =
5609 					&bp->bnapi[i]->cp_ring;
5610 
5611 				cpr->cp_ring_struct.ring_mem.flags =
5612 					BNXT_RMEM_RING_PTE_FLAG;
5613 			}
5614 		}
5615 
5616 		bp->rx_ring = kzalloc_objs(struct bnxt_rx_ring_info,
5617 					   bp->rx_nr_rings);
5618 		if (!bp->rx_ring)
5619 			return -ENOMEM;
5620 
5621 		for (i = 0; i < bp->rx_nr_rings; i++) {
5622 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5623 
5624 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5625 				rxr->rx_ring_struct.ring_mem.flags =
5626 					BNXT_RMEM_RING_PTE_FLAG;
5627 				rxr->rx_agg_ring_struct.ring_mem.flags =
5628 					BNXT_RMEM_RING_PTE_FLAG;
5629 			} else {
5630 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5631 			}
5632 			rxr->bnapi = bp->bnapi[i];
5633 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5634 		}
5635 
5636 		bp->tx_ring = kzalloc_objs(struct bnxt_tx_ring_info,
5637 					   bp->tx_nr_rings);
5638 		if (!bp->tx_ring)
5639 			return -ENOMEM;
5640 
5641 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5642 					  GFP_KERNEL);
5643 
5644 		if (!bp->tx_ring_map)
5645 			return -ENOMEM;
5646 
5647 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5648 			j = 0;
5649 		else
5650 			j = bp->rx_nr_rings;
5651 
5652 		for (i = 0; i < bp->tx_nr_rings; i++) {
5653 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5654 			struct bnxt_napi *bnapi2;
5655 
5656 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5657 				txr->tx_ring_struct.ring_mem.flags =
5658 					BNXT_RMEM_RING_PTE_FLAG;
5659 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5660 			if (i >= bp->tx_nr_rings_xdp) {
5661 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5662 
5663 				bnapi2 = bp->bnapi[k];
5664 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5665 				txr->tx_napi_idx =
5666 					BNXT_RING_TO_TC(bp, txr->txq_index);
5667 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5668 				bnapi2->tx_int = bnxt_tx_int;
5669 			} else {
5670 				bnapi2 = bp->bnapi[j];
5671 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5672 				bnapi2->tx_ring[0] = txr;
5673 				bnapi2->tx_int = bnxt_tx_int_xdp;
5674 				j++;
5675 			}
5676 			txr->bnapi = bnapi2;
5677 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5678 				txr->tx_cpr = &bnapi2->cp_ring;
5679 		}
5680 
5681 		rc = bnxt_alloc_stats(bp);
5682 		if (rc)
5683 			goto alloc_mem_err;
5684 		bnxt_init_stats(bp);
5685 
5686 		rc = bnxt_alloc_ntp_fltrs(bp);
5687 		if (rc)
5688 			goto alloc_mem_err;
5689 
5690 		rc = bnxt_alloc_vnics(bp);
5691 		if (rc)
5692 			goto alloc_mem_err;
5693 	}
5694 
5695 	rc = bnxt_alloc_all_cp_arrays(bp);
5696 	if (rc)
5697 		goto alloc_mem_err;
5698 
5699 	bnxt_init_ring_struct(bp);
5700 
5701 	rc = bnxt_alloc_rx_rings(bp);
5702 	if (rc)
5703 		goto alloc_mem_err;
5704 
5705 	rc = bnxt_alloc_tx_rings(bp);
5706 	if (rc)
5707 		goto alloc_mem_err;
5708 
5709 	rc = bnxt_alloc_cp_rings(bp);
5710 	if (rc)
5711 		goto alloc_mem_err;
5712 
5713 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5714 						  BNXT_VNIC_MCAST_FLAG |
5715 						  BNXT_VNIC_UCAST_FLAG;
5716 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5717 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5718 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5719 
5720 	rc = bnxt_alloc_vnic_attributes(bp);
5721 	if (rc)
5722 		goto alloc_mem_err;
5723 	return 0;
5724 
5725 alloc_mem_err:
5726 	bnxt_free_mem(bp, true);
5727 	return rc;
5728 }
5729 
5730 static void bnxt_disable_int(struct bnxt *bp)
5731 {
5732 	int i;
5733 
5734 	if (!bp->bnapi)
5735 		return;
5736 
5737 	for (i = 0; i < bp->cp_nr_rings; i++) {
5738 		struct bnxt_napi *bnapi = bp->bnapi[i];
5739 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5740 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5741 
5742 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5743 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5744 	}
5745 }
5746 
5747 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5748 {
5749 	struct bnxt_napi *bnapi = bp->bnapi[n];
5750 	struct bnxt_cp_ring_info *cpr;
5751 
5752 	cpr = &bnapi->cp_ring;
5753 	return cpr->cp_ring_struct.map_idx;
5754 }
5755 
5756 static void bnxt_disable_int_sync(struct bnxt *bp)
5757 {
5758 	int i;
5759 
5760 	if (!bp->irq_tbl || !bp->bnapi)
5761 		return;
5762 
5763 	atomic_inc(&bp->intr_sem);
5764 
5765 	bnxt_disable_int(bp);
5766 	for (i = 0; i < bp->cp_nr_rings; i++) {
5767 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5768 
5769 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5770 	}
5771 }
5772 
5773 static void bnxt_enable_int(struct bnxt *bp)
5774 {
5775 	int i;
5776 
5777 	atomic_set(&bp->intr_sem, 0);
5778 	for (i = 0; i < bp->cp_nr_rings; i++) {
5779 		struct bnxt_napi *bnapi = bp->bnapi[i];
5780 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5781 
5782 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5783 	}
5784 }
5785 
5786 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5787 			    bool async_only)
5788 {
5789 	DECLARE_BITMAP(async_events_bmap, 256);
5790 	u32 *events = (u32 *)async_events_bmap;
5791 	struct hwrm_func_drv_rgtr_output *resp;
5792 	struct hwrm_func_drv_rgtr_input *req;
5793 	u32 flags;
5794 	int rc, i;
5795 
5796 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5797 	if (rc)
5798 		return rc;
5799 
5800 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5801 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5802 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5803 
5804 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5805 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5806 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5807 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5808 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5809 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5810 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5811 	if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2)
5812 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT;
5813 	req->flags = cpu_to_le32(flags);
5814 	req->ver_maj_8b = DRV_VER_MAJ;
5815 	req->ver_min_8b = DRV_VER_MIN;
5816 	req->ver_upd_8b = DRV_VER_UPD;
5817 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5818 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5819 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5820 
5821 	if (BNXT_PF(bp)) {
5822 		u32 data[8];
5823 		int i;
5824 
5825 		memset(data, 0, sizeof(data));
5826 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5827 			u16 cmd = bnxt_vf_req_snif[i];
5828 			unsigned int bit, idx;
5829 
5830 			if ((bp->fw_cap & BNXT_FW_CAP_LINK_ADMIN) &&
5831 			    cmd == HWRM_PORT_PHY_QCFG)
5832 				continue;
5833 
5834 			idx = cmd / 32;
5835 			bit = cmd % 32;
5836 			data[idx] |= 1 << bit;
5837 		}
5838 
5839 		for (i = 0; i < 8; i++)
5840 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5841 
5842 		req->enables |=
5843 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5844 	}
5845 
5846 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5847 		req->flags |= cpu_to_le32(
5848 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5849 
5850 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5851 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5852 		u16 event_id = bnxt_async_events_arr[i];
5853 
5854 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5855 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5856 			continue;
5857 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5858 		    !bp->ptp_cfg)
5859 			continue;
5860 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5861 	}
5862 	if (bmap && bmap_size) {
5863 		for (i = 0; i < bmap_size; i++) {
5864 			if (test_bit(i, bmap))
5865 				__set_bit(i, async_events_bmap);
5866 		}
5867 	}
5868 	for (i = 0; i < 8; i++)
5869 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5870 
5871 	if (async_only)
5872 		req->enables =
5873 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5874 
5875 	resp = hwrm_req_hold(bp, req);
5876 	rc = hwrm_req_send(bp, req);
5877 	if (!rc) {
5878 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5879 		if (resp->flags &
5880 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5881 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5882 	}
5883 	hwrm_req_drop(bp, req);
5884 	return rc;
5885 }
5886 
5887 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5888 {
5889 	struct hwrm_func_drv_unrgtr_input *req;
5890 	int rc;
5891 
5892 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5893 		return 0;
5894 
5895 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5896 	if (rc)
5897 		return rc;
5898 	return hwrm_req_send(bp, req);
5899 }
5900 
5901 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5902 
5903 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5904 {
5905 	struct hwrm_tunnel_dst_port_free_input *req;
5906 	int rc;
5907 
5908 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5909 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5910 		return 0;
5911 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5912 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5913 		return 0;
5914 
5915 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5916 	if (rc)
5917 		return rc;
5918 
5919 	req->tunnel_type = tunnel_type;
5920 
5921 	switch (tunnel_type) {
5922 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5923 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5924 		bp->vxlan_port = 0;
5925 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5926 		break;
5927 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5928 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5929 		bp->nge_port = 0;
5930 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5931 		break;
5932 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5933 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5934 		bp->vxlan_gpe_port = 0;
5935 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5936 		break;
5937 	default:
5938 		break;
5939 	}
5940 
5941 	rc = hwrm_req_send(bp, req);
5942 	if (rc)
5943 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5944 			   rc);
5945 	if (bp->flags & BNXT_FLAG_TPA)
5946 		bnxt_set_tpa(bp, true);
5947 	return rc;
5948 }
5949 
5950 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5951 					   u8 tunnel_type)
5952 {
5953 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5954 	struct hwrm_tunnel_dst_port_alloc_input *req;
5955 	int rc;
5956 
5957 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5958 	if (rc)
5959 		return rc;
5960 
5961 	req->tunnel_type = tunnel_type;
5962 	req->tunnel_dst_port_val = port;
5963 
5964 	resp = hwrm_req_hold(bp, req);
5965 	rc = hwrm_req_send(bp, req);
5966 	if (rc) {
5967 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5968 			   rc);
5969 		goto err_out;
5970 	}
5971 
5972 	switch (tunnel_type) {
5973 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5974 		bp->vxlan_port = port;
5975 		bp->vxlan_fw_dst_port_id =
5976 			le16_to_cpu(resp->tunnel_dst_port_id);
5977 		break;
5978 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5979 		bp->nge_port = port;
5980 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5981 		break;
5982 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5983 		bp->vxlan_gpe_port = port;
5984 		bp->vxlan_gpe_fw_dst_port_id =
5985 			le16_to_cpu(resp->tunnel_dst_port_id);
5986 		break;
5987 	default:
5988 		break;
5989 	}
5990 	if (bp->flags & BNXT_FLAG_TPA)
5991 		bnxt_set_tpa(bp, true);
5992 
5993 err_out:
5994 	hwrm_req_drop(bp, req);
5995 	return rc;
5996 }
5997 
5998 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5999 {
6000 	struct hwrm_cfa_l2_set_rx_mask_input *req;
6001 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
6002 	int rc;
6003 
6004 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
6005 	if (rc)
6006 		return rc;
6007 
6008 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6009 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
6010 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
6011 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
6012 	}
6013 	req->mask = cpu_to_le32(vnic->rx_mask);
6014 	return hwrm_req_send_silent(bp, req);
6015 }
6016 
6017 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6018 {
6019 	if (!atomic_dec_and_test(&fltr->refcnt))
6020 		return;
6021 	spin_lock_bh(&bp->ntp_fltr_lock);
6022 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
6023 		spin_unlock_bh(&bp->ntp_fltr_lock);
6024 		return;
6025 	}
6026 	hlist_del_rcu(&fltr->base.hash);
6027 	bnxt_del_one_usr_fltr(bp, &fltr->base);
6028 	if (fltr->base.flags) {
6029 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
6030 		bp->ntp_fltr_count--;
6031 	}
6032 	spin_unlock_bh(&bp->ntp_fltr_lock);
6033 	kfree_rcu(fltr, base.rcu);
6034 }
6035 
6036 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
6037 						      struct bnxt_l2_key *key,
6038 						      u32 idx)
6039 {
6040 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
6041 	struct bnxt_l2_filter *fltr;
6042 
6043 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
6044 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
6045 
6046 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
6047 		    l2_key->vlan == key->vlan)
6048 			return fltr;
6049 	}
6050 	return NULL;
6051 }
6052 
6053 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
6054 						    struct bnxt_l2_key *key,
6055 						    u32 idx)
6056 {
6057 	struct bnxt_l2_filter *fltr = NULL;
6058 
6059 	rcu_read_lock();
6060 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6061 	if (fltr)
6062 		atomic_inc(&fltr->refcnt);
6063 	rcu_read_unlock();
6064 	return fltr;
6065 }
6066 
6067 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
6068 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
6069 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
6070 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
6071 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
6072 
6073 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
6074 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
6075 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
6076 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
6077 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
6078 
6079 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
6080 {
6081 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
6082 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
6083 			return sizeof(fkeys->addrs.v4addrs) +
6084 			       sizeof(fkeys->ports);
6085 
6086 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
6087 			return sizeof(fkeys->addrs.v4addrs);
6088 	}
6089 
6090 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
6091 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
6092 			return sizeof(fkeys->addrs.v6addrs) +
6093 			       sizeof(fkeys->ports);
6094 
6095 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
6096 			return sizeof(fkeys->addrs.v6addrs);
6097 	}
6098 
6099 	return 0;
6100 }
6101 
6102 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
6103 			 const unsigned char *key)
6104 {
6105 	u64 prefix = bp->toeplitz_prefix, hash = 0;
6106 	struct bnxt_ipv4_tuple tuple4;
6107 	struct bnxt_ipv6_tuple tuple6;
6108 	int i, j, len = 0;
6109 	u8 *four_tuple;
6110 
6111 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
6112 	if (!len)
6113 		return 0;
6114 
6115 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
6116 		tuple4.v4addrs = fkeys->addrs.v4addrs;
6117 		tuple4.ports = fkeys->ports;
6118 		four_tuple = (unsigned char *)&tuple4;
6119 	} else {
6120 		tuple6.v6addrs = fkeys->addrs.v6addrs;
6121 		tuple6.ports = fkeys->ports;
6122 		four_tuple = (unsigned char *)&tuple6;
6123 	}
6124 
6125 	for (i = 0, j = 8; i < len; i++, j++) {
6126 		u8 byte = four_tuple[i];
6127 		int bit;
6128 
6129 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
6130 			if (byte & 0x80)
6131 				hash ^= prefix;
6132 		}
6133 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
6134 	}
6135 
6136 	/* The valid part of the hash is in the upper 32 bits. */
6137 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
6138 }
6139 
6140 #ifdef CONFIG_RFS_ACCEL
6141 static struct bnxt_l2_filter *
6142 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
6143 {
6144 	struct bnxt_l2_filter *fltr;
6145 	u32 idx;
6146 
6147 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6148 	      BNXT_L2_FLTR_HASH_MASK;
6149 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6150 	return fltr;
6151 }
6152 #endif
6153 
6154 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
6155 			       struct bnxt_l2_key *key, u32 idx)
6156 {
6157 	struct hlist_head *head;
6158 
6159 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
6160 	fltr->l2_key.vlan = key->vlan;
6161 	fltr->base.type = BNXT_FLTR_TYPE_L2;
6162 	if (fltr->base.flags) {
6163 		int bit_id;
6164 
6165 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6166 						 bp->max_fltr, 0);
6167 		if (bit_id < 0)
6168 			return -ENOMEM;
6169 		fltr->base.sw_id = (u16)bit_id;
6170 		bp->ntp_fltr_count++;
6171 	}
6172 	head = &bp->l2_fltr_hash_tbl[idx];
6173 	hlist_add_head_rcu(&fltr->base.hash, head);
6174 	bnxt_insert_usr_fltr(bp, &fltr->base);
6175 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
6176 	atomic_set(&fltr->refcnt, 1);
6177 	return 0;
6178 }
6179 
6180 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
6181 						   struct bnxt_l2_key *key,
6182 						   gfp_t gfp)
6183 {
6184 	struct bnxt_l2_filter *fltr;
6185 	u32 idx;
6186 	int rc;
6187 
6188 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6189 	      BNXT_L2_FLTR_HASH_MASK;
6190 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6191 	if (fltr)
6192 		return fltr;
6193 
6194 	fltr = kzalloc_obj(*fltr, gfp);
6195 	if (!fltr)
6196 		return ERR_PTR(-ENOMEM);
6197 	spin_lock_bh(&bp->ntp_fltr_lock);
6198 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6199 	spin_unlock_bh(&bp->ntp_fltr_lock);
6200 	if (rc) {
6201 		bnxt_del_l2_filter(bp, fltr);
6202 		fltr = ERR_PTR(rc);
6203 	}
6204 	return fltr;
6205 }
6206 
6207 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
6208 						struct bnxt_l2_key *key,
6209 						u16 flags)
6210 {
6211 	struct bnxt_l2_filter *fltr;
6212 	u32 idx;
6213 	int rc;
6214 
6215 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6216 	      BNXT_L2_FLTR_HASH_MASK;
6217 	spin_lock_bh(&bp->ntp_fltr_lock);
6218 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6219 	if (fltr) {
6220 		fltr = ERR_PTR(-EEXIST);
6221 		goto l2_filter_exit;
6222 	}
6223 	fltr = kzalloc_obj(*fltr, GFP_ATOMIC);
6224 	if (!fltr) {
6225 		fltr = ERR_PTR(-ENOMEM);
6226 		goto l2_filter_exit;
6227 	}
6228 	fltr->base.flags = flags;
6229 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6230 	if (rc) {
6231 		spin_unlock_bh(&bp->ntp_fltr_lock);
6232 		bnxt_del_l2_filter(bp, fltr);
6233 		return ERR_PTR(rc);
6234 	}
6235 
6236 l2_filter_exit:
6237 	spin_unlock_bh(&bp->ntp_fltr_lock);
6238 	return fltr;
6239 }
6240 
6241 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
6242 {
6243 #ifdef CONFIG_BNXT_SRIOV
6244 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
6245 
6246 	return vf->fw_fid;
6247 #else
6248 	return INVALID_HW_RING_ID;
6249 #endif
6250 }
6251 
6252 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6253 {
6254 	struct hwrm_cfa_l2_filter_free_input *req;
6255 	u16 target_id = 0xffff;
6256 	int rc;
6257 
6258 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6259 		struct bnxt_pf_info *pf = &bp->pf;
6260 
6261 		if (fltr->base.vf_idx >= pf->active_vfs)
6262 			return -EINVAL;
6263 
6264 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6265 		if (target_id == INVALID_HW_RING_ID)
6266 			return -EINVAL;
6267 	}
6268 
6269 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
6270 	if (rc)
6271 		return rc;
6272 
6273 	req->target_id = cpu_to_le16(target_id);
6274 	req->l2_filter_id = fltr->base.filter_id;
6275 	return hwrm_req_send(bp, req);
6276 }
6277 
6278 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6279 {
6280 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6281 	struct hwrm_cfa_l2_filter_alloc_input *req;
6282 	u16 target_id = 0xffff;
6283 	int rc;
6284 
6285 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6286 		struct bnxt_pf_info *pf = &bp->pf;
6287 
6288 		if (fltr->base.vf_idx >= pf->active_vfs)
6289 			return -EINVAL;
6290 
6291 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6292 	}
6293 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6294 	if (rc)
6295 		return rc;
6296 
6297 	req->target_id = cpu_to_le16(target_id);
6298 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6299 
6300 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6301 		req->flags |=
6302 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6303 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6304 	req->enables =
6305 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6306 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6307 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6308 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6309 	eth_broadcast_addr(req->l2_addr_mask);
6310 
6311 	if (fltr->l2_key.vlan) {
6312 		req->enables |=
6313 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6314 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6315 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6316 		req->num_vlans = 1;
6317 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6318 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6319 	}
6320 
6321 	resp = hwrm_req_hold(bp, req);
6322 	rc = hwrm_req_send(bp, req);
6323 	if (!rc) {
6324 		fltr->base.filter_id = resp->l2_filter_id;
6325 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6326 	}
6327 	hwrm_req_drop(bp, req);
6328 	return rc;
6329 }
6330 
6331 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6332 				     struct bnxt_ntuple_filter *fltr)
6333 {
6334 	struct hwrm_cfa_ntuple_filter_free_input *req;
6335 	int rc;
6336 
6337 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6338 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
6339 		return 0;
6340 
6341 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6342 	if (rc)
6343 		return rc;
6344 
6345 	req->ntuple_filter_id = fltr->base.filter_id;
6346 	return hwrm_req_send(bp, req);
6347 }
6348 
6349 #define BNXT_NTP_FLTR_FLAGS					\
6350 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6351 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6352 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6353 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6354 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6355 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6356 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6357 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6358 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6359 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6360 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6361 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6362 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6363 
6364 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6365 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6366 
6367 void bnxt_fill_ipv6_mask(__be32 mask[4])
6368 {
6369 	int i;
6370 
6371 	for (i = 0; i < 4; i++)
6372 		mask[i] = cpu_to_be32(~0);
6373 }
6374 
6375 static void
6376 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6377 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6378 			  struct bnxt_ntuple_filter *fltr)
6379 {
6380 	u16 rxq = fltr->base.rxq;
6381 
6382 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6383 		struct ethtool_rxfh_context *ctx;
6384 		struct bnxt_rss_ctx *rss_ctx;
6385 		struct bnxt_vnic_info *vnic;
6386 
6387 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6388 			      fltr->base.fw_vnic_id);
6389 		if (ctx) {
6390 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6391 			vnic = &rss_ctx->vnic;
6392 
6393 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6394 		}
6395 		return;
6396 	}
6397 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6398 		struct bnxt_vnic_info *vnic;
6399 		u32 enables;
6400 
6401 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6402 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6403 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6404 		req->enables |= cpu_to_le32(enables);
6405 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6406 	} else {
6407 		u32 flags;
6408 
6409 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6410 		req->flags |= cpu_to_le32(flags);
6411 		req->dst_id = cpu_to_le16(rxq);
6412 	}
6413 }
6414 
6415 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6416 				      struct bnxt_ntuple_filter *fltr)
6417 {
6418 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6419 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6420 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6421 	struct flow_keys *keys = &fltr->fkeys;
6422 	struct bnxt_l2_filter *l2_fltr;
6423 	struct bnxt_vnic_info *vnic;
6424 	int rc;
6425 
6426 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6427 	if (rc)
6428 		return rc;
6429 
6430 	l2_fltr = fltr->l2_fltr;
6431 	req->l2_filter_id = l2_fltr->base.filter_id;
6432 
6433 	if (fltr->base.flags & BNXT_ACT_DROP) {
6434 		req->flags =
6435 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6436 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6437 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6438 	} else {
6439 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6440 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6441 	}
6442 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6443 
6444 	req->ethertype = htons(ETH_P_IP);
6445 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6446 	req->ip_protocol = keys->basic.ip_proto;
6447 
6448 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6449 		req->ethertype = htons(ETH_P_IPV6);
6450 		req->ip_addr_type =
6451 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6452 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6453 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6454 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6455 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6456 	} else {
6457 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6458 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6459 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6460 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6461 	}
6462 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6463 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6464 		req->tunnel_type =
6465 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6466 	}
6467 
6468 	req->src_port = keys->ports.src;
6469 	req->src_port_mask = masks->ports.src;
6470 	req->dst_port = keys->ports.dst;
6471 	req->dst_port_mask = masks->ports.dst;
6472 
6473 	resp = hwrm_req_hold(bp, req);
6474 	rc = hwrm_req_send(bp, req);
6475 	if (!rc)
6476 		fltr->base.filter_id = resp->ntuple_filter_id;
6477 	hwrm_req_drop(bp, req);
6478 	return rc;
6479 }
6480 
6481 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6482 				     const u8 *mac_addr)
6483 {
6484 	struct bnxt_l2_filter *fltr;
6485 	struct bnxt_l2_key key;
6486 	int rc;
6487 
6488 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6489 	key.vlan = 0;
6490 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6491 	if (IS_ERR(fltr))
6492 		return PTR_ERR(fltr);
6493 
6494 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6495 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6496 	if (rc)
6497 		bnxt_del_l2_filter(bp, fltr);
6498 	else
6499 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6500 	return rc;
6501 }
6502 
6503 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6504 {
6505 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6506 
6507 	/* Any associated ntuple filters will also be cleared by firmware. */
6508 	for (i = 0; i < num_of_vnics; i++) {
6509 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6510 
6511 		for (j = 0; j < vnic->uc_filter_count; j++) {
6512 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6513 
6514 			bnxt_hwrm_l2_filter_free(bp, fltr);
6515 			bnxt_del_l2_filter(bp, fltr);
6516 		}
6517 		vnic->uc_filter_count = 0;
6518 	}
6519 }
6520 
6521 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6522 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6523 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6524 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6525 
6526 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6527 					   struct hwrm_vnic_tpa_cfg_input *req)
6528 {
6529 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6530 
6531 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6532 		return;
6533 
6534 	if (bp->vxlan_port)
6535 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6536 	if (bp->vxlan_gpe_port)
6537 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6538 	if (bp->nge_port)
6539 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6540 
6541 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6542 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6543 }
6544 
6545 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6546 			   u32 tpa_flags)
6547 {
6548 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6549 	struct hwrm_vnic_tpa_cfg_input *req;
6550 	int rc;
6551 
6552 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6553 		return 0;
6554 
6555 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6556 	if (rc)
6557 		return rc;
6558 
6559 	if (tpa_flags) {
6560 		u16 mss = bp->dev->mtu - 40;
6561 		u32 nsegs, n, segs = 0, flags;
6562 
6563 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6564 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6565 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6566 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6567 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6568 		if (tpa_flags & BNXT_FLAG_GRO)
6569 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6570 
6571 		req->flags = cpu_to_le32(flags);
6572 
6573 		req->enables =
6574 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6575 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6576 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6577 
6578 		/* Number of segs are log2 units, and first packet is not
6579 		 * included as part of this units.
6580 		 */
6581 		if (mss <= BNXT_RX_PAGE_SIZE) {
6582 			n = BNXT_RX_PAGE_SIZE / mss;
6583 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6584 		} else {
6585 			n = mss / BNXT_RX_PAGE_SIZE;
6586 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6587 				n++;
6588 			nsegs = (MAX_SKB_FRAGS - n) / n;
6589 		}
6590 
6591 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6592 			segs = MAX_TPA_SEGS_P5;
6593 			max_aggs = bp->max_tpa;
6594 		} else {
6595 			segs = ilog2(nsegs);
6596 		}
6597 		req->max_agg_segs = cpu_to_le16(segs);
6598 		req->max_aggs = cpu_to_le16(max_aggs);
6599 
6600 		req->min_agg_len = cpu_to_le32(512);
6601 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6602 	}
6603 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6604 
6605 	return hwrm_req_send(bp, req);
6606 }
6607 
6608 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6609 {
6610 	struct bnxt_ring_grp_info *grp_info;
6611 
6612 	grp_info = &bp->grp_info[ring->grp_idx];
6613 	return grp_info->cp_fw_ring_id;
6614 }
6615 
6616 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6617 {
6618 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6619 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6620 	else
6621 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6622 }
6623 
6624 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6625 {
6626 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6627 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6628 	else
6629 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6630 }
6631 
6632 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6633 {
6634 	int entries;
6635 
6636 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6637 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6638 	else
6639 		entries = HW_HASH_INDEX_SIZE;
6640 
6641 	bp->rss_indir_tbl_entries = entries;
6642 	bp->rss_indir_tbl =
6643 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6644 	if (!bp->rss_indir_tbl)
6645 		return -ENOMEM;
6646 
6647 	return 0;
6648 }
6649 
6650 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6651 				 struct ethtool_rxfh_context *rss_ctx)
6652 {
6653 	u16 max_rings, max_entries, pad, i;
6654 	u32 *rss_indir_tbl;
6655 
6656 	if (!bp->rx_nr_rings)
6657 		return;
6658 
6659 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6660 		max_rings = bp->rx_nr_rings - 1;
6661 	else
6662 		max_rings = bp->rx_nr_rings;
6663 
6664 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6665 	if (rss_ctx)
6666 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6667 	else
6668 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6669 
6670 	for (i = 0; i < max_entries; i++)
6671 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6672 
6673 	pad = bp->rss_indir_tbl_entries - max_entries;
6674 	if (pad)
6675 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6676 }
6677 
6678 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6679 {
6680 	u32 i, tbl_size, max_ring = 0;
6681 
6682 	if (!bp->rss_indir_tbl)
6683 		return 0;
6684 
6685 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6686 	for (i = 0; i < tbl_size; i++)
6687 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6688 	return max_ring;
6689 }
6690 
6691 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6692 {
6693 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6694 		if (!rx_rings)
6695 			return 0;
6696 		if (bp->rss_cap & BNXT_RSS_CAP_LARGE_RSS_CTX)
6697 			return BNXT_RSS_TABLE_MAX_TBL_P5;
6698 
6699 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6700 					       BNXT_RSS_TABLE_ENTRIES_P5);
6701 	}
6702 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6703 		return 2;
6704 	return 1;
6705 }
6706 
6707 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6708 {
6709 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6710 	u16 i, j, min_j = bp->rx_nr_rings - 1;
6711 
6712 	if (!vnic->rss_table)
6713 		goto skip_rss_tbl;
6714 
6715 	/* Fill the RSS indirection table with ring group ids */
6716 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6717 		if (!no_rss)
6718 			j = bp->rss_indir_tbl[i];
6719 		min_j = min(j, min_j);
6720 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6721 	}
6722 
6723 skip_rss_tbl:
6724 	if (vnic->rss_table && !no_rss)
6725 		vnic->default_rx_ring = min_j;
6726 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6727 		vnic->default_rx_ring = vnic->vnic_id - 1;
6728 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6729 		vnic->default_rx_ring = bp->rx_nr_rings - 1;
6730 	else
6731 		vnic->default_rx_ring = 0;
6732 }
6733 
6734 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6735 				    struct bnxt_vnic_info *vnic)
6736 {
6737 	u16 tbl_size, i, min_j = bp->rx_nr_rings - 1;
6738 	__le16 *ring_tbl = vnic->rss_table;
6739 	struct bnxt_rx_ring_info *rxr;
6740 
6741 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6742 
6743 	for (i = 0; i < tbl_size; i++) {
6744 		u16 ring_id, j;
6745 
6746 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6747 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6748 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6749 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6750 		else
6751 			j = bp->rss_indir_tbl[i];
6752 		min_j = min(j, min_j);
6753 		rxr = &bp->rx_ring[j];
6754 
6755 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6756 		*ring_tbl++ = cpu_to_le16(ring_id);
6757 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6758 		*ring_tbl++ = cpu_to_le16(ring_id);
6759 	}
6760 	vnic->default_rx_ring = min_j;
6761 }
6762 
6763 static void
6764 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6765 			 struct bnxt_vnic_info *vnic)
6766 {
6767 	if (bp->flags & BNXT_FLAG_CHIP_P7)
6768 		req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6769 
6770 	if (bp->rss_hash_delta) {
6771 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6772 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6773 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6774 		else
6775 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6776 	} else {
6777 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6778 	}
6779 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6780 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6781 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6782 }
6783 
6784 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6785 				  bool set_rss)
6786 {
6787 	struct hwrm_vnic_rss_cfg_input *req;
6788 	int rc;
6789 
6790 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6791 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6792 		return 0;
6793 
6794 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6795 	if (rc)
6796 		return rc;
6797 
6798 	if (set_rss)
6799 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6800 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6801 	return hwrm_req_send(bp, req);
6802 }
6803 
6804 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6805 				     struct bnxt_vnic_info *vnic, bool set_rss)
6806 {
6807 	struct hwrm_vnic_rss_cfg_input *req;
6808 	dma_addr_t ring_tbl_map;
6809 	u32 i, nr_ctxs;
6810 	int rc;
6811 
6812 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6813 	if (rc)
6814 		return rc;
6815 
6816 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6817 	if (!set_rss)
6818 		return hwrm_req_send(bp, req);
6819 
6820 	bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6821 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6822 	ring_tbl_map = vnic->rss_table_dma_addr;
6823 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6824 
6825 	hwrm_req_hold(bp, req);
6826 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6827 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6828 		req->ring_table_pair_index = i;
6829 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6830 		rc = hwrm_req_send(bp, req);
6831 		if (rc)
6832 			goto exit;
6833 	}
6834 
6835 exit:
6836 	hwrm_req_drop(bp, req);
6837 	return rc;
6838 }
6839 
6840 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6841 {
6842 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6843 	struct hwrm_vnic_rss_qcfg_output *resp;
6844 	struct hwrm_vnic_rss_qcfg_input *req;
6845 
6846 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6847 		return;
6848 
6849 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6850 	/* all contexts configured to same hash_type, zero always exists */
6851 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6852 	resp = hwrm_req_hold(bp, req);
6853 	if (!hwrm_req_send(bp, req)) {
6854 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6855 		bp->rss_hash_delta = 0;
6856 	}
6857 	hwrm_req_drop(bp, req);
6858 }
6859 
6860 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6861 {
6862 	u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh;
6863 	struct hwrm_vnic_plcmodes_cfg_input *req;
6864 	int rc;
6865 
6866 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6867 	if (rc)
6868 		return rc;
6869 
6870 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6871 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6872 	req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6873 
6874 	if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
6875 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6876 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6877 		req->enables |=
6878 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6879 		req->hds_threshold = cpu_to_le16(hds_thresh);
6880 	}
6881 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6882 	return hwrm_req_send(bp, req);
6883 }
6884 
6885 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6886 					struct bnxt_vnic_info *vnic,
6887 					u16 ctx_idx)
6888 {
6889 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6890 
6891 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6892 		return;
6893 
6894 	req->rss_cos_lb_ctx_id =
6895 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6896 
6897 	hwrm_req_send(bp, req);
6898 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6899 }
6900 
6901 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6902 {
6903 	int i, j;
6904 
6905 	for (i = 0; i < bp->nr_vnics; i++) {
6906 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6907 
6908 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6909 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6910 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6911 		}
6912 	}
6913 	bp->rsscos_nr_ctxs = 0;
6914 }
6915 
6916 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6917 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6918 {
6919 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6920 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6921 	int rc;
6922 
6923 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6924 	if (rc)
6925 		return rc;
6926 
6927 	resp = hwrm_req_hold(bp, req);
6928 	rc = hwrm_req_send(bp, req);
6929 	if (!rc)
6930 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6931 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6932 	hwrm_req_drop(bp, req);
6933 
6934 	return rc;
6935 }
6936 
6937 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6938 {
6939 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6940 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6941 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6942 }
6943 
6944 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6945 {
6946 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6947 	struct hwrm_vnic_cfg_input *req;
6948 	unsigned int ring = 0, grp_idx;
6949 	u16 def_vlan = 0;
6950 	int rc;
6951 
6952 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6953 	if (rc)
6954 		return rc;
6955 
6956 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6957 		struct bnxt_rx_ring_info *rxr;
6958 
6959 		rxr = &bp->rx_ring[vnic->default_rx_ring];
6960 		req->default_rx_ring_id =
6961 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6962 		req->default_cmpl_ring_id =
6963 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6964 		req->enables =
6965 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6966 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6967 		goto vnic_mru;
6968 	}
6969 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6970 	/* Only RSS support for now TBD: COS & LB */
6971 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6972 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6973 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6974 					   VNIC_CFG_REQ_ENABLES_MRU);
6975 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6976 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6977 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6978 					   VNIC_CFG_REQ_ENABLES_MRU);
6979 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6980 	} else {
6981 		req->rss_rule = cpu_to_le16(0xffff);
6982 	}
6983 
6984 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6985 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6986 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6987 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6988 	} else {
6989 		req->cos_rule = cpu_to_le16(0xffff);
6990 	}
6991 
6992 	ring = vnic->default_rx_ring;
6993 	grp_idx = bp->rx_ring[ring].bnapi->index;
6994 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6995 	req->lb_rule = cpu_to_le16(0xffff);
6996 vnic_mru:
6997 	vnic->mru = bp->dev->mtu + VLAN_ETH_HLEN;
6998 	req->mru = cpu_to_le16(vnic->mru);
6999 
7000 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
7001 #ifdef CONFIG_BNXT_SRIOV
7002 	if (BNXT_VF(bp))
7003 		def_vlan = bp->vf.vlan;
7004 #endif
7005 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
7006 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
7007 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT &&
7008 	    bnxt_ulp_registered(bp->edev[BNXT_AUXDEV_RDMA]))
7009 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
7010 
7011 	return hwrm_req_send(bp, req);
7012 }
7013 
7014 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
7015 				    struct bnxt_vnic_info *vnic)
7016 {
7017 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
7018 		struct hwrm_vnic_free_input *req;
7019 
7020 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
7021 			return;
7022 
7023 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
7024 
7025 		hwrm_req_send(bp, req);
7026 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
7027 	}
7028 }
7029 
7030 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
7031 {
7032 	u16 i;
7033 
7034 	for (i = 0; i < bp->nr_vnics; i++)
7035 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
7036 }
7037 
7038 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
7039 			 unsigned int start_rx_ring_idx,
7040 			 unsigned int nr_rings)
7041 {
7042 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
7043 	struct hwrm_vnic_alloc_output *resp;
7044 	struct hwrm_vnic_alloc_input *req;
7045 	int rc;
7046 
7047 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
7048 	if (rc)
7049 		return rc;
7050 
7051 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7052 		goto vnic_no_ring_grps;
7053 
7054 	/* map ring groups to this vnic */
7055 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
7056 		grp_idx = bp->rx_ring[i].bnapi->index;
7057 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
7058 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
7059 				   j, nr_rings);
7060 			break;
7061 		}
7062 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
7063 	}
7064 
7065 vnic_no_ring_grps:
7066 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
7067 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
7068 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
7069 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
7070 
7071 	resp = hwrm_req_hold(bp, req);
7072 	rc = hwrm_req_send(bp, req);
7073 	if (!rc)
7074 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
7075 	hwrm_req_drop(bp, req);
7076 	return rc;
7077 }
7078 
7079 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
7080 {
7081 	struct hwrm_vnic_qcaps_output *resp;
7082 	struct hwrm_vnic_qcaps_input *req;
7083 	int rc;
7084 
7085 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
7086 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
7087 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
7088 	if (bp->hwrm_spec_code < 0x10600)
7089 		return 0;
7090 
7091 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
7092 	if (rc)
7093 		return rc;
7094 
7095 	resp = hwrm_req_hold(bp, req);
7096 	rc = hwrm_req_send(bp, req);
7097 	if (!rc) {
7098 		u32 flags = le32_to_cpu(resp->flags);
7099 
7100 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
7101 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
7102 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
7103 		if (flags &
7104 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
7105 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
7106 
7107 		/* Older P5 fw before EXT_HW_STATS support did not set
7108 		 * VLAN_STRIP_CAP properly.
7109 		 */
7110 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
7111 		    (BNXT_CHIP_P5(bp) &&
7112 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
7113 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
7114 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
7115 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
7116 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
7117 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
7118 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
7119 		if (bp->max_tpa_v2) {
7120 			if (BNXT_CHIP_P5(bp))
7121 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
7122 			else
7123 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
7124 		}
7125 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
7126 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
7127 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
7128 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
7129 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
7130 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
7131 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
7132 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
7133 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
7134 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
7135 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPV6_FLOW_LABEL_CAP)
7136 			bp->rss_cap |= BNXT_RSS_CAP_IPV6_FLOW_LABEL_RSS_CAP;
7137 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
7138 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
7139 	}
7140 	hwrm_req_drop(bp, req);
7141 	return rc;
7142 }
7143 
7144 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
7145 {
7146 	struct hwrm_ring_grp_alloc_output *resp;
7147 	struct hwrm_ring_grp_alloc_input *req;
7148 	int rc;
7149 	u16 i;
7150 
7151 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7152 		return 0;
7153 
7154 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
7155 	if (rc)
7156 		return rc;
7157 
7158 	resp = hwrm_req_hold(bp, req);
7159 	for (i = 0; i < bp->rx_nr_rings; i++) {
7160 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
7161 
7162 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
7163 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
7164 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
7165 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
7166 
7167 		rc = hwrm_req_send(bp, req);
7168 
7169 		if (rc)
7170 			break;
7171 
7172 		bp->grp_info[grp_idx].fw_grp_id =
7173 			le32_to_cpu(resp->ring_group_id);
7174 	}
7175 	hwrm_req_drop(bp, req);
7176 	return rc;
7177 }
7178 
7179 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
7180 {
7181 	struct hwrm_ring_grp_free_input *req;
7182 	u16 i;
7183 
7184 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7185 		return;
7186 
7187 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
7188 		return;
7189 
7190 	hwrm_req_hold(bp, req);
7191 	for (i = 0; i < bp->cp_nr_rings; i++) {
7192 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
7193 			continue;
7194 		req->ring_group_id =
7195 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
7196 
7197 		hwrm_req_send(bp, req);
7198 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
7199 	}
7200 	hwrm_req_drop(bp, req);
7201 }
7202 
7203 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type,
7204 				       struct hwrm_ring_alloc_input *req,
7205 				       struct bnxt_rx_ring_info *rxr,
7206 				       struct bnxt_ring_struct *ring)
7207 {
7208 	struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx];
7209 	u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID |
7210 		      RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID;
7211 
7212 	if (ring_type == HWRM_RING_ALLOC_AGG) {
7213 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
7214 		req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
7215 		req->rx_buf_size = cpu_to_le16(rxr->rx_page_size);
7216 		enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID;
7217 	} else {
7218 		req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
7219 		if (NET_IP_ALIGN == 2)
7220 			req->flags =
7221 				cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD);
7222 	}
7223 	req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7224 	req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7225 	req->enables |= cpu_to_le32(enables);
7226 }
7227 
7228 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
7229 				    struct bnxt_rx_ring_info *rxr,
7230 				    struct bnxt_ring_struct *ring,
7231 				    u32 ring_type, u32 map_index)
7232 {
7233 	struct hwrm_ring_alloc_output *resp;
7234 	struct hwrm_ring_alloc_input *req;
7235 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
7236 	struct bnxt_ring_grp_info *grp_info;
7237 	int rc, err = 0;
7238 	u16 ring_id;
7239 
7240 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
7241 	if (rc)
7242 		goto exit;
7243 
7244 	req->enables = 0;
7245 	if (rmem->nr_pages > 1) {
7246 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
7247 		/* Page size is in log2 units */
7248 		req->page_size = BNXT_PAGE_SHIFT;
7249 		req->page_tbl_depth = 1;
7250 	} else {
7251 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
7252 	}
7253 	req->fbo = 0;
7254 	/* Association of ring index with doorbell index and MSIX number */
7255 	req->logical_id = cpu_to_le16(map_index);
7256 
7257 	switch (ring_type) {
7258 	case HWRM_RING_ALLOC_TX: {
7259 		struct bnxt_tx_ring_info *txr;
7260 		u16 flags = 0;
7261 
7262 		txr = container_of(ring, struct bnxt_tx_ring_info,
7263 				   tx_ring_struct);
7264 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
7265 		/* Association of transmit ring with completion ring */
7266 		grp_info = &bp->grp_info[ring->grp_idx];
7267 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
7268 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
7269 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7270 		req->queue_id = cpu_to_le16(ring->queue_id);
7271 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
7272 			req->cmpl_coal_cnt =
7273 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
7274 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
7275 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
7276 		req->flags = cpu_to_le16(flags);
7277 		break;
7278 	}
7279 	case HWRM_RING_ALLOC_RX:
7280 	case HWRM_RING_ALLOC_AGG:
7281 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
7282 		req->length = (ring_type == HWRM_RING_ALLOC_RX) ?
7283 			      cpu_to_le32(bp->rx_ring_mask + 1) :
7284 			      cpu_to_le32(bp->rx_agg_ring_mask + 1);
7285 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7286 			bnxt_set_rx_ring_params_p5(bp, ring_type, req,
7287 						   rxr, ring);
7288 		break;
7289 	case HWRM_RING_ALLOC_CMPL:
7290 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7291 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7292 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7293 			/* Association of cp ring with nq */
7294 			grp_info = &bp->grp_info[map_index];
7295 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7296 			req->cq_handle = cpu_to_le64(ring->handle);
7297 			req->enables |= cpu_to_le32(
7298 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7299 		} else {
7300 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7301 		}
7302 		break;
7303 	case HWRM_RING_ALLOC_NQ:
7304 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7305 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7306 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7307 		break;
7308 	default:
7309 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7310 			   ring_type);
7311 		return -EINVAL;
7312 	}
7313 
7314 	resp = hwrm_req_hold(bp, req);
7315 	rc = hwrm_req_send(bp, req);
7316 	err = le16_to_cpu(resp->error_code);
7317 	ring_id = le16_to_cpu(resp->ring_id);
7318 	hwrm_req_drop(bp, req);
7319 
7320 exit:
7321 	if (rc || err) {
7322 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7323 			   ring_type, rc, err);
7324 		return -EIO;
7325 	}
7326 	ring->fw_ring_id = ring_id;
7327 	return rc;
7328 }
7329 
7330 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7331 {
7332 	int rc;
7333 
7334 	if (BNXT_PF(bp)) {
7335 		struct hwrm_func_cfg_input *req;
7336 
7337 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7338 		if (rc)
7339 			return rc;
7340 
7341 		req->fid = cpu_to_le16(0xffff);
7342 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7343 		req->async_event_cr = cpu_to_le16(idx);
7344 		return hwrm_req_send(bp, req);
7345 	} else {
7346 		struct hwrm_func_vf_cfg_input *req;
7347 
7348 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7349 		if (rc)
7350 			return rc;
7351 
7352 		req->enables =
7353 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7354 		req->async_event_cr = cpu_to_le16(idx);
7355 		return hwrm_req_send(bp, req);
7356 	}
7357 }
7358 
7359 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7360 			     u32 ring_type)
7361 {
7362 	switch (ring_type) {
7363 	case HWRM_RING_ALLOC_TX:
7364 		db->db_ring_mask = bp->tx_ring_mask;
7365 		break;
7366 	case HWRM_RING_ALLOC_RX:
7367 		db->db_ring_mask = bp->rx_ring_mask;
7368 		break;
7369 	case HWRM_RING_ALLOC_AGG:
7370 		db->db_ring_mask = bp->rx_agg_ring_mask;
7371 		break;
7372 	case HWRM_RING_ALLOC_CMPL:
7373 	case HWRM_RING_ALLOC_NQ:
7374 		db->db_ring_mask = bp->cp_ring_mask;
7375 		break;
7376 	}
7377 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7378 		db->db_epoch_mask = db->db_ring_mask + 1;
7379 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7380 	}
7381 }
7382 
7383 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7384 			u32 map_idx, u32 xid)
7385 {
7386 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7387 		switch (ring_type) {
7388 		case HWRM_RING_ALLOC_TX:
7389 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7390 			break;
7391 		case HWRM_RING_ALLOC_RX:
7392 		case HWRM_RING_ALLOC_AGG:
7393 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7394 			break;
7395 		case HWRM_RING_ALLOC_CMPL:
7396 			db->db_key64 = DBR_PATH_L2;
7397 			break;
7398 		case HWRM_RING_ALLOC_NQ:
7399 			db->db_key64 = DBR_PATH_L2;
7400 			break;
7401 		}
7402 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7403 
7404 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7405 			db->db_key64 |= DBR_VALID;
7406 
7407 		db->doorbell = bp->bar1 + bp->db_offset;
7408 	} else {
7409 		db->doorbell = bp->bar1 + map_idx * 0x80;
7410 		switch (ring_type) {
7411 		case HWRM_RING_ALLOC_TX:
7412 			db->db_key32 = DB_KEY_TX;
7413 			break;
7414 		case HWRM_RING_ALLOC_RX:
7415 		case HWRM_RING_ALLOC_AGG:
7416 			db->db_key32 = DB_KEY_RX;
7417 			break;
7418 		case HWRM_RING_ALLOC_CMPL:
7419 			db->db_key32 = DB_KEY_CP;
7420 			break;
7421 		}
7422 	}
7423 	bnxt_set_db_mask(bp, db, ring_type);
7424 }
7425 
7426 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7427 				   struct bnxt_rx_ring_info *rxr)
7428 {
7429 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7430 	struct bnxt_napi *bnapi = rxr->bnapi;
7431 	u32 type = HWRM_RING_ALLOC_RX;
7432 	u32 map_idx = bnapi->index;
7433 	int rc;
7434 
7435 	rc = hwrm_ring_alloc_send_msg(bp, rxr, ring, type, map_idx);
7436 	if (rc)
7437 		return rc;
7438 
7439 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7440 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7441 
7442 	return 0;
7443 }
7444 
7445 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7446 				       struct bnxt_rx_ring_info *rxr)
7447 {
7448 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7449 	u32 type = HWRM_RING_ALLOC_AGG;
7450 	u32 grp_idx = ring->grp_idx;
7451 	u32 map_idx;
7452 	int rc;
7453 
7454 	map_idx = grp_idx + bp->rx_nr_rings;
7455 	rc = hwrm_ring_alloc_send_msg(bp, rxr, ring, type, map_idx);
7456 	if (rc)
7457 		return rc;
7458 
7459 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7460 		    ring->fw_ring_id);
7461 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7462 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7463 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7464 
7465 	return 0;
7466 }
7467 
7468 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp,
7469 				      struct bnxt_cp_ring_info *cpr)
7470 {
7471 	const u32 type = HWRM_RING_ALLOC_CMPL;
7472 	struct bnxt_napi *bnapi = cpr->bnapi;
7473 	struct bnxt_ring_struct *ring;
7474 	u32 map_idx = bnapi->index;
7475 	int rc;
7476 
7477 	ring = &cpr->cp_ring_struct;
7478 	ring->handle = BNXT_SET_NQ_HDL(cpr);
7479 	rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, map_idx);
7480 	if (rc)
7481 		return rc;
7482 	bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7483 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7484 	return 0;
7485 }
7486 
7487 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp,
7488 				   struct bnxt_tx_ring_info *txr, u32 tx_idx)
7489 {
7490 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7491 	const u32 type = HWRM_RING_ALLOC_TX;
7492 	int rc;
7493 
7494 	rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, tx_idx);
7495 	if (rc)
7496 		return rc;
7497 	bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id);
7498 	return 0;
7499 }
7500 
7501 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7502 {
7503 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7504 	int i, rc = 0;
7505 	u32 type;
7506 
7507 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7508 		type = HWRM_RING_ALLOC_NQ;
7509 	else
7510 		type = HWRM_RING_ALLOC_CMPL;
7511 	for (i = 0; i < bp->cp_nr_rings; i++) {
7512 		struct bnxt_napi *bnapi = bp->bnapi[i];
7513 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7514 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7515 		u32 map_idx = ring->map_idx;
7516 		unsigned int vector;
7517 
7518 		vector = bp->irq_tbl[map_idx].vector;
7519 		disable_irq_nosync(vector);
7520 		rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, map_idx);
7521 		if (rc) {
7522 			enable_irq(vector);
7523 			goto err_out;
7524 		}
7525 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7526 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7527 		enable_irq(vector);
7528 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7529 
7530 		if (!i) {
7531 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7532 			if (rc)
7533 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7534 		}
7535 	}
7536 
7537 	for (i = 0; i < bp->tx_nr_rings; i++) {
7538 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7539 
7540 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7541 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
7542 			if (rc)
7543 				goto err_out;
7544 		}
7545 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i);
7546 		if (rc)
7547 			goto err_out;
7548 	}
7549 
7550 	for (i = 0; i < bp->rx_nr_rings; i++) {
7551 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7552 
7553 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7554 		if (rc)
7555 			goto err_out;
7556 		/* If we have agg rings, post agg buffers first. */
7557 		if (!agg_rings)
7558 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7559 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7560 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
7561 			if (rc)
7562 				goto err_out;
7563 		}
7564 	}
7565 
7566 	if (agg_rings) {
7567 		for (i = 0; i < bp->rx_nr_rings; i++) {
7568 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7569 			if (rc)
7570 				goto err_out;
7571 		}
7572 	}
7573 err_out:
7574 	return rc;
7575 }
7576 
7577 static void bnxt_cancel_dim(struct bnxt *bp)
7578 {
7579 	int i;
7580 
7581 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7582 	 * if NAPI is enabled.
7583 	 */
7584 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7585 		return;
7586 
7587 	/* Make sure NAPI sees that the VNIC is disabled */
7588 	synchronize_net();
7589 	for (i = 0; i < bp->rx_nr_rings; i++) {
7590 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7591 		struct bnxt_napi *bnapi = rxr->bnapi;
7592 
7593 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7594 	}
7595 }
7596 
7597 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7598 				   struct bnxt_ring_struct *ring,
7599 				   u32 ring_type, int cmpl_ring_id)
7600 {
7601 	struct hwrm_ring_free_output *resp;
7602 	struct hwrm_ring_free_input *req;
7603 	u16 error_code = 0;
7604 	int rc;
7605 
7606 	if (BNXT_NO_FW_ACCESS(bp))
7607 		return 0;
7608 
7609 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7610 	if (rc)
7611 		goto exit;
7612 
7613 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7614 	req->ring_type = ring_type;
7615 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7616 
7617 	resp = hwrm_req_hold(bp, req);
7618 	rc = hwrm_req_send(bp, req);
7619 	error_code = le16_to_cpu(resp->error_code);
7620 	hwrm_req_drop(bp, req);
7621 exit:
7622 	if (rc || error_code) {
7623 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7624 			   ring_type, rc, error_code);
7625 		return -EIO;
7626 	}
7627 	return 0;
7628 }
7629 
7630 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp,
7631 				   struct bnxt_tx_ring_info *txr,
7632 				   bool close_path)
7633 {
7634 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7635 	u32 cmpl_ring_id;
7636 
7637 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7638 		return;
7639 
7640 	cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) :
7641 		       INVALID_HW_RING_ID;
7642 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX,
7643 				cmpl_ring_id);
7644 	ring->fw_ring_id = INVALID_HW_RING_ID;
7645 }
7646 
7647 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7648 				   struct bnxt_rx_ring_info *rxr,
7649 				   bool close_path)
7650 {
7651 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7652 	u32 grp_idx = rxr->bnapi->index;
7653 	u32 cmpl_ring_id;
7654 
7655 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7656 		return;
7657 
7658 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7659 	hwrm_ring_free_send_msg(bp, ring,
7660 				RING_FREE_REQ_RING_TYPE_RX,
7661 				close_path ? cmpl_ring_id :
7662 				INVALID_HW_RING_ID);
7663 	ring->fw_ring_id = INVALID_HW_RING_ID;
7664 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7665 }
7666 
7667 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7668 				       struct bnxt_rx_ring_info *rxr,
7669 				       bool close_path)
7670 {
7671 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7672 	u32 grp_idx = rxr->bnapi->index;
7673 	u32 type, cmpl_ring_id;
7674 
7675 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7676 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7677 	else
7678 		type = RING_FREE_REQ_RING_TYPE_RX;
7679 
7680 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7681 		return;
7682 
7683 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7684 	hwrm_ring_free_send_msg(bp, ring, type,
7685 				close_path ? cmpl_ring_id :
7686 				INVALID_HW_RING_ID);
7687 	ring->fw_ring_id = INVALID_HW_RING_ID;
7688 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7689 }
7690 
7691 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp,
7692 				   struct bnxt_cp_ring_info *cpr)
7693 {
7694 	struct bnxt_ring_struct *ring;
7695 
7696 	ring = &cpr->cp_ring_struct;
7697 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7698 		return;
7699 
7700 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL,
7701 				INVALID_HW_RING_ID);
7702 	ring->fw_ring_id = INVALID_HW_RING_ID;
7703 }
7704 
7705 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
7706 {
7707 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7708 	int i, size = ring->ring_mem.page_size;
7709 
7710 	cpr->cp_raw_cons = 0;
7711 	cpr->toggle = 0;
7712 
7713 	for (i = 0; i < bp->cp_nr_pages; i++)
7714 		if (cpr->cp_desc_ring[i])
7715 			memset(cpr->cp_desc_ring[i], 0, size);
7716 }
7717 
7718 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7719 {
7720 	u32 type;
7721 	int i;
7722 
7723 	if (!bp->bnapi)
7724 		return;
7725 
7726 	for (i = 0; i < bp->tx_nr_rings; i++)
7727 		bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path);
7728 
7729 	bnxt_cancel_dim(bp);
7730 	for (i = 0; i < bp->rx_nr_rings; i++) {
7731 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7732 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7733 	}
7734 
7735 	/* The completion rings are about to be freed.  After that the
7736 	 * IRQ doorbell will not work anymore.  So we need to disable
7737 	 * IRQ here.
7738 	 */
7739 	bnxt_disable_int_sync(bp);
7740 
7741 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7742 		type = RING_FREE_REQ_RING_TYPE_NQ;
7743 	else
7744 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7745 	for (i = 0; i < bp->cp_nr_rings; i++) {
7746 		struct bnxt_napi *bnapi = bp->bnapi[i];
7747 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7748 		struct bnxt_ring_struct *ring;
7749 		int j;
7750 
7751 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++)
7752 			bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]);
7753 
7754 		ring = &cpr->cp_ring_struct;
7755 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7756 			hwrm_ring_free_send_msg(bp, ring, type,
7757 						INVALID_HW_RING_ID);
7758 			ring->fw_ring_id = INVALID_HW_RING_ID;
7759 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7760 		}
7761 	}
7762 }
7763 
7764 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7765 			     bool shared);
7766 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7767 			   bool shared);
7768 
7769 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7770 {
7771 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7772 	struct hwrm_func_qcfg_output *resp;
7773 	struct hwrm_func_qcfg_input *req;
7774 	int rc;
7775 
7776 	if (bp->hwrm_spec_code < 0x10601)
7777 		return 0;
7778 
7779 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7780 	if (rc)
7781 		return rc;
7782 
7783 	req->fid = cpu_to_le16(0xffff);
7784 	resp = hwrm_req_hold(bp, req);
7785 	rc = hwrm_req_send(bp, req);
7786 	if (rc) {
7787 		hwrm_req_drop(bp, req);
7788 		return rc;
7789 	}
7790 
7791 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7792 	if (BNXT_NEW_RM(bp)) {
7793 		u16 cp, stats;
7794 
7795 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7796 		hw_resc->resv_hw_ring_grps =
7797 			le32_to_cpu(resp->alloc_hw_ring_grps);
7798 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7799 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7800 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7801 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7802 		hw_resc->resv_irqs = cp;
7803 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7804 			int rx = hw_resc->resv_rx_rings;
7805 			int tx = hw_resc->resv_tx_rings;
7806 
7807 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7808 				rx >>= 1;
7809 			if (cp < (rx + tx)) {
7810 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7811 				if (rc)
7812 					goto get_rings_exit;
7813 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7814 					rx <<= 1;
7815 				hw_resc->resv_rx_rings = rx;
7816 				hw_resc->resv_tx_rings = tx;
7817 			}
7818 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7819 			hw_resc->resv_hw_ring_grps = rx;
7820 		}
7821 		hw_resc->resv_cp_rings = cp;
7822 		hw_resc->resv_stat_ctxs = stats;
7823 	}
7824 get_rings_exit:
7825 	hwrm_req_drop(bp, req);
7826 	return rc;
7827 }
7828 
7829 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7830 {
7831 	struct hwrm_func_qcfg_output *resp;
7832 	struct hwrm_func_qcfg_input *req;
7833 	int rc;
7834 
7835 	if (bp->hwrm_spec_code < 0x10601)
7836 		return 0;
7837 
7838 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7839 	if (rc)
7840 		return rc;
7841 
7842 	req->fid = cpu_to_le16(fid);
7843 	resp = hwrm_req_hold(bp, req);
7844 	rc = hwrm_req_send(bp, req);
7845 	if (!rc)
7846 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7847 
7848 	hwrm_req_drop(bp, req);
7849 	return rc;
7850 }
7851 
7852 static bool bnxt_rfs_supported(struct bnxt *bp);
7853 
7854 static struct hwrm_func_cfg_input *
7855 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7856 {
7857 	struct hwrm_func_cfg_input *req;
7858 	u32 enables = 0;
7859 
7860 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7861 		return NULL;
7862 
7863 	req->fid = cpu_to_le16(0xffff);
7864 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7865 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7866 	if (BNXT_NEW_RM(bp)) {
7867 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7868 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7869 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7870 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7871 			enables |= hwr->cp_p5 ?
7872 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7873 		} else {
7874 			enables |= hwr->cp ?
7875 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7876 			enables |= hwr->grp ?
7877 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7878 		}
7879 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7880 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7881 					  0;
7882 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7883 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7884 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7885 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7886 			req->num_msix = cpu_to_le16(hwr->cp);
7887 		} else {
7888 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7889 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7890 		}
7891 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7892 		req->num_vnics = cpu_to_le16(hwr->vnic);
7893 	}
7894 	req->enables = cpu_to_le32(enables);
7895 	return req;
7896 }
7897 
7898 static struct hwrm_func_vf_cfg_input *
7899 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7900 {
7901 	struct hwrm_func_vf_cfg_input *req;
7902 	u32 enables = 0;
7903 
7904 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7905 		return NULL;
7906 
7907 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7908 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7909 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7910 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7911 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7912 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7913 		enables |= hwr->cp_p5 ?
7914 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7915 	} else {
7916 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7917 		enables |= hwr->grp ?
7918 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7919 	}
7920 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7921 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7922 
7923 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7924 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7925 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7926 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7927 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7928 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7929 	} else {
7930 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7931 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7932 	}
7933 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7934 	req->num_vnics = cpu_to_le16(hwr->vnic);
7935 
7936 	req->enables = cpu_to_le32(enables);
7937 	return req;
7938 }
7939 
7940 static int
7941 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7942 {
7943 	struct hwrm_func_cfg_input *req;
7944 	int rc;
7945 
7946 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7947 	if (!req)
7948 		return -ENOMEM;
7949 
7950 	if (!req->enables) {
7951 		hwrm_req_drop(bp, req);
7952 		return 0;
7953 	}
7954 
7955 	rc = hwrm_req_send(bp, req);
7956 	if (rc)
7957 		return rc;
7958 
7959 	if (bp->hwrm_spec_code < 0x10601)
7960 		bp->hw_resc.resv_tx_rings = hwr->tx;
7961 
7962 	return bnxt_hwrm_get_rings(bp);
7963 }
7964 
7965 static int
7966 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7967 {
7968 	struct hwrm_func_vf_cfg_input *req;
7969 	int rc;
7970 
7971 	if (!BNXT_NEW_RM(bp)) {
7972 		bp->hw_resc.resv_tx_rings = hwr->tx;
7973 		return 0;
7974 	}
7975 
7976 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7977 	if (!req)
7978 		return -ENOMEM;
7979 
7980 	rc = hwrm_req_send(bp, req);
7981 	if (rc)
7982 		return rc;
7983 
7984 	return bnxt_hwrm_get_rings(bp);
7985 }
7986 
7987 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7988 {
7989 	if (BNXT_PF(bp))
7990 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7991 	else
7992 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7993 }
7994 
7995 int bnxt_nq_rings_in_use(struct bnxt *bp)
7996 {
7997 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7998 }
7999 
8000 static int bnxt_cp_rings_in_use(struct bnxt *bp)
8001 {
8002 	int cp;
8003 
8004 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8005 		return bnxt_nq_rings_in_use(bp);
8006 
8007 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
8008 	return cp;
8009 }
8010 
8011 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
8012 {
8013 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
8014 }
8015 
8016 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8017 {
8018 	if (!hwr->grp)
8019 		return 0;
8020 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8021 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
8022 
8023 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
8024 			rss_ctx *= hwr->vnic;
8025 		return rss_ctx;
8026 	}
8027 	if (BNXT_VF(bp))
8028 		return BNXT_VF_MAX_RSS_CTX;
8029 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
8030 		return hwr->grp + 1;
8031 	return 1;
8032 }
8033 
8034 /* Check if a default RSS map needs to be setup.  This function is only
8035  * used on older firmware that does not require reserving RX rings.
8036  */
8037 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
8038 {
8039 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8040 
8041 	/* The RSS map is valid for RX rings set to resv_rx_rings */
8042 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
8043 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
8044 		if (!netif_is_rxfh_configured(bp->dev))
8045 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8046 	}
8047 }
8048 
8049 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
8050 {
8051 	if (bp->flags & BNXT_FLAG_RFS) {
8052 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
8053 			return 2 + bp->num_rss_ctx;
8054 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8055 			return rx_rings + 1;
8056 	}
8057 	return 1;
8058 }
8059 
8060 static void bnxt_get_total_resources(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8061 {
8062 	hwr->cp = bnxt_nq_rings_in_use(bp);
8063 	hwr->cp_p5 = 0;
8064 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8065 		hwr->cp_p5 = bnxt_cp_rings_in_use(bp);
8066 	hwr->tx = bp->tx_nr_rings;
8067 	hwr->rx = bp->rx_nr_rings;
8068 	hwr->grp = hwr->rx;
8069 	hwr->vnic = bnxt_get_total_vnics(bp, hwr->rx);
8070 	hwr->rss_ctx = bnxt_get_total_rss_ctxs(bp, hwr);
8071 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8072 		hwr->rx <<= 1;
8073 	hwr->stat = bnxt_get_func_stat_ctxs(bp);
8074 }
8075 
8076 static bool bnxt_need_reserve_rings(struct bnxt *bp)
8077 {
8078 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8079 	struct bnxt_hw_rings hwr;
8080 
8081 	bnxt_get_total_resources(bp, &hwr);
8082 
8083 	/* Old firmware does not need RX ring reservations but we still
8084 	 * need to setup a default RSS map when needed.  With new firmware
8085 	 * we go through RX ring reservations first and then set up the
8086 	 * RSS map for the successfully reserved RX rings when needed.
8087 	 */
8088 	if (!BNXT_NEW_RM(bp))
8089 		bnxt_check_rss_tbl_no_rmgr(bp);
8090 
8091 	if (hw_resc->resv_tx_rings != hwr.tx && bp->hwrm_spec_code >= 0x10601)
8092 		return true;
8093 
8094 	if (!BNXT_NEW_RM(bp))
8095 		return false;
8096 
8097 	if (hw_resc->resv_rx_rings != hwr.rx ||
8098 	    hw_resc->resv_vnics != hwr.vnic ||
8099 	    hw_resc->resv_stat_ctxs != hwr.stat ||
8100 	    hw_resc->resv_rsscos_ctxs != hwr.rss_ctx ||
8101 	    (hw_resc->resv_hw_ring_grps != hwr.grp &&
8102 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
8103 		return true;
8104 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8105 		if (hw_resc->resv_cp_rings != hwr.cp_p5)
8106 			return true;
8107 	} else if (hw_resc->resv_cp_rings != hwr.cp) {
8108 		return true;
8109 	}
8110 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
8111 	    hw_resc->resv_irqs != hwr.cp)
8112 		return true;
8113 	return false;
8114 }
8115 
8116 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8117 {
8118 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8119 
8120 	hwr->tx = hw_resc->resv_tx_rings;
8121 	if (BNXT_NEW_RM(bp)) {
8122 		hwr->rx = hw_resc->resv_rx_rings;
8123 		hwr->cp = hw_resc->resv_irqs;
8124 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8125 			hwr->cp_p5 = hw_resc->resv_cp_rings;
8126 		hwr->grp = hw_resc->resv_hw_ring_grps;
8127 		hwr->vnic = hw_resc->resv_vnics;
8128 		hwr->stat = hw_resc->resv_stat_ctxs;
8129 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
8130 	}
8131 }
8132 
8133 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8134 {
8135 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
8136 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
8137 }
8138 
8139 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
8140 
8141 static int __bnxt_reserve_rings(struct bnxt *bp)
8142 {
8143 	struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA];
8144 	struct bnxt_hw_rings hwr = {0};
8145 	int rx_rings, old_rx_rings, rc;
8146 	int cp = bp->cp_nr_rings;
8147 	int ulp_msix = 0;
8148 	bool sh = false;
8149 	int tx_cp;
8150 
8151 	if (!bnxt_need_reserve_rings(bp))
8152 		return 0;
8153 
8154 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(edev)) {
8155 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
8156 		if (!ulp_msix)
8157 			bnxt_set_ulp_stat_ctxs(bp, 0);
8158 		else
8159 			bnxt_set_dflt_ulp_stat_ctxs(bp);
8160 
8161 		if (ulp_msix > bp->ulp_num_msix_want)
8162 			ulp_msix = bp->ulp_num_msix_want;
8163 		hwr.cp = cp + ulp_msix;
8164 	} else {
8165 		hwr.cp = bnxt_nq_rings_in_use(bp);
8166 	}
8167 
8168 	hwr.tx = bp->tx_nr_rings;
8169 	hwr.rx = bp->rx_nr_rings;
8170 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
8171 		sh = true;
8172 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8173 		hwr.cp_p5 = hwr.rx + hwr.tx;
8174 
8175 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
8176 
8177 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8178 		hwr.rx <<= 1;
8179 	hwr.grp = bp->rx_nr_rings;
8180 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
8181 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
8182 	old_rx_rings = bp->hw_resc.resv_rx_rings;
8183 
8184 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
8185 	if (rc)
8186 		return rc;
8187 
8188 	bnxt_copy_reserved_rings(bp, &hwr);
8189 
8190 	rx_rings = hwr.rx;
8191 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8192 		if (hwr.rx >= 2) {
8193 			rx_rings = hwr.rx >> 1;
8194 		} else {
8195 			if (netif_running(bp->dev))
8196 				return -ENOMEM;
8197 
8198 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
8199 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
8200 			bp->dev->hw_features &= ~NETIF_F_LRO;
8201 			bp->dev->features &= ~NETIF_F_LRO;
8202 			bnxt_set_ring_params(bp);
8203 		}
8204 	}
8205 	rx_rings = min_t(int, rx_rings, hwr.grp);
8206 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
8207 	if (bnxt_ulp_registered(edev) && hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
8208 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
8209 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
8210 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
8211 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8212 		hwr.rx = rx_rings << 1;
8213 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
8214 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
8215 	if (hwr.tx != bp->tx_nr_rings) {
8216 		netdev_warn(bp->dev,
8217 			    "Able to reserve only %d out of %d requested TX rings\n",
8218 			    hwr.tx, bp->tx_nr_rings);
8219 	}
8220 	bp->tx_nr_rings = hwr.tx;
8221 
8222 	/* If we cannot reserve all the RX rings, reset the RSS map only
8223 	 * if absolutely necessary
8224 	 */
8225 	if (rx_rings != bp->rx_nr_rings) {
8226 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
8227 			    rx_rings, bp->rx_nr_rings);
8228 		if (netif_is_rxfh_configured(bp->dev) &&
8229 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
8230 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
8231 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
8232 			ethtool_rxfh_indir_lost(bp->dev);
8233 		}
8234 	}
8235 	bp->rx_nr_rings = rx_rings;
8236 	bp->cp_nr_rings = hwr.cp;
8237 
8238 	/* Fall back if we cannot reserve enough HW RSS contexts */
8239 	if ((bp->rss_cap & BNXT_RSS_CAP_LARGE_RSS_CTX) &&
8240 	    hwr.rss_ctx < bnxt_get_total_rss_ctxs(bp, &hwr))
8241 		bp->rss_cap &= ~BNXT_RSS_CAP_LARGE_RSS_CTX;
8242 
8243 	if (!bnxt_rings_ok(bp, &hwr))
8244 		return -ENOMEM;
8245 
8246 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
8247 	    !netif_is_rxfh_configured(bp->dev))
8248 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8249 
8250 	if (!bnxt_ulp_registered(edev) && BNXT_NEW_RM(bp)) {
8251 		int resv_msix, resv_ctx, ulp_ctxs;
8252 		struct bnxt_hw_resc *hw_resc;
8253 
8254 		hw_resc = &bp->hw_resc;
8255 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
8256 		ulp_msix = min_t(int, resv_msix, ulp_msix);
8257 		bnxt_set_ulp_msix_num(bp, ulp_msix);
8258 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
8259 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
8260 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
8261 	}
8262 
8263 	return rc;
8264 }
8265 
8266 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8267 {
8268 	struct hwrm_func_vf_cfg_input *req;
8269 	u32 flags;
8270 
8271 	if (!BNXT_NEW_RM(bp))
8272 		return 0;
8273 
8274 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
8275 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
8276 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8277 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8278 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8279 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
8280 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
8281 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8282 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8283 
8284 	req->flags = cpu_to_le32(flags);
8285 	return hwrm_req_send_silent(bp, req);
8286 }
8287 
8288 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8289 {
8290 	struct hwrm_func_cfg_input *req;
8291 	u32 flags;
8292 
8293 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
8294 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
8295 	if (BNXT_NEW_RM(bp)) {
8296 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8297 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8298 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8299 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
8300 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8301 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
8302 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
8303 		else
8304 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8305 	}
8306 
8307 	req->flags = cpu_to_le32(flags);
8308 	return hwrm_req_send_silent(bp, req);
8309 }
8310 
8311 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8312 {
8313 	if (bp->hwrm_spec_code < 0x10801)
8314 		return 0;
8315 
8316 	if (BNXT_PF(bp))
8317 		return bnxt_hwrm_check_pf_rings(bp, hwr);
8318 
8319 	return bnxt_hwrm_check_vf_rings(bp, hwr);
8320 }
8321 
8322 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
8323 {
8324 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8325 	struct hwrm_ring_aggint_qcaps_output *resp;
8326 	struct hwrm_ring_aggint_qcaps_input *req;
8327 	int rc;
8328 
8329 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
8330 	coal_cap->num_cmpl_dma_aggr_max = 63;
8331 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
8332 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
8333 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
8334 	coal_cap->int_lat_tmr_min_max = 65535;
8335 	coal_cap->int_lat_tmr_max_max = 65535;
8336 	coal_cap->num_cmpl_aggr_int_max = 65535;
8337 	coal_cap->timer_units = 80;
8338 
8339 	if (bp->hwrm_spec_code < 0x10902)
8340 		return;
8341 
8342 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
8343 		return;
8344 
8345 	resp = hwrm_req_hold(bp, req);
8346 	rc = hwrm_req_send_silent(bp, req);
8347 	if (!rc) {
8348 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8349 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8350 		coal_cap->num_cmpl_dma_aggr_max =
8351 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8352 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8353 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8354 		coal_cap->cmpl_aggr_dma_tmr_max =
8355 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8356 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8357 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8358 		coal_cap->int_lat_tmr_min_max =
8359 			le16_to_cpu(resp->int_lat_tmr_min_max);
8360 		coal_cap->int_lat_tmr_max_max =
8361 			le16_to_cpu(resp->int_lat_tmr_max_max);
8362 		coal_cap->num_cmpl_aggr_int_max =
8363 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8364 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8365 	}
8366 	hwrm_req_drop(bp, req);
8367 }
8368 
8369 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8370 {
8371 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8372 
8373 	return usec * 1000 / coal_cap->timer_units;
8374 }
8375 
8376 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8377 	struct bnxt_coal *hw_coal,
8378 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8379 {
8380 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8381 	u16 val, tmr, max, flags = hw_coal->flags;
8382 	u32 cmpl_params = coal_cap->cmpl_params;
8383 
8384 	max = hw_coal->bufs_per_record * 128;
8385 	if (hw_coal->budget)
8386 		max = hw_coal->bufs_per_record * hw_coal->budget;
8387 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8388 
8389 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8390 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8391 
8392 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8393 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8394 
8395 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8396 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8397 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8398 
8399 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8400 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8401 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8402 
8403 	/* min timer set to 1/2 of interrupt timer */
8404 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8405 		val = tmr / 2;
8406 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8407 		req->int_lat_tmr_min = cpu_to_le16(val);
8408 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8409 	}
8410 
8411 	/* buf timer set to 1/4 of interrupt timer */
8412 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8413 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8414 
8415 	if (cmpl_params &
8416 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8417 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8418 		val = clamp_t(u16, tmr, 1,
8419 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8420 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8421 		req->enables |=
8422 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8423 	}
8424 
8425 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8426 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8427 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8428 	req->flags = cpu_to_le16(flags);
8429 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8430 }
8431 
8432 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8433 				   struct bnxt_coal *hw_coal)
8434 {
8435 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8436 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8437 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8438 	u32 nq_params = coal_cap->nq_params;
8439 	u16 tmr;
8440 	int rc;
8441 
8442 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8443 		return 0;
8444 
8445 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8446 	if (rc)
8447 		return rc;
8448 
8449 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8450 	req->flags =
8451 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8452 
8453 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8454 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8455 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8456 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8457 	return hwrm_req_send(bp, req);
8458 }
8459 
8460 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8461 {
8462 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8463 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8464 	struct bnxt_coal coal;
8465 	int rc;
8466 
8467 	/* Tick values in micro seconds.
8468 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8469 	 */
8470 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8471 
8472 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8473 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8474 
8475 	if (!bnapi->rx_ring)
8476 		return -ENODEV;
8477 
8478 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8479 	if (rc)
8480 		return rc;
8481 
8482 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8483 
8484 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8485 
8486 	return hwrm_req_send(bp, req_rx);
8487 }
8488 
8489 static int
8490 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8491 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8492 {
8493 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8494 
8495 	req->ring_id = cpu_to_le16(ring_id);
8496 	return hwrm_req_send(bp, req);
8497 }
8498 
8499 static int
8500 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8501 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8502 {
8503 	struct bnxt_tx_ring_info *txr;
8504 	int i, rc;
8505 
8506 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8507 		u16 ring_id;
8508 
8509 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8510 		req->ring_id = cpu_to_le16(ring_id);
8511 		rc = hwrm_req_send(bp, req);
8512 		if (rc)
8513 			return rc;
8514 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8515 			return 0;
8516 	}
8517 	return 0;
8518 }
8519 
8520 int bnxt_hwrm_set_coal(struct bnxt *bp)
8521 {
8522 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8523 	int i, rc;
8524 
8525 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8526 	if (rc)
8527 		return rc;
8528 
8529 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8530 	if (rc) {
8531 		hwrm_req_drop(bp, req_rx);
8532 		return rc;
8533 	}
8534 
8535 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8536 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8537 
8538 	hwrm_req_hold(bp, req_rx);
8539 	hwrm_req_hold(bp, req_tx);
8540 	for (i = 0; i < bp->cp_nr_rings; i++) {
8541 		struct bnxt_napi *bnapi = bp->bnapi[i];
8542 		struct bnxt_coal *hw_coal;
8543 
8544 		if (!bnapi->rx_ring)
8545 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8546 		else
8547 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8548 		if (rc)
8549 			break;
8550 
8551 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8552 			continue;
8553 
8554 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8555 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8556 			if (rc)
8557 				break;
8558 		}
8559 		if (bnapi->rx_ring)
8560 			hw_coal = &bp->rx_coal;
8561 		else
8562 			hw_coal = &bp->tx_coal;
8563 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8564 	}
8565 	hwrm_req_drop(bp, req_rx);
8566 	hwrm_req_drop(bp, req_tx);
8567 	return rc;
8568 }
8569 
8570 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8571 {
8572 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8573 	struct hwrm_stat_ctx_free_input *req;
8574 	int i;
8575 
8576 	if (!bp->bnapi)
8577 		return;
8578 
8579 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8580 		return;
8581 
8582 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8583 		return;
8584 	if (BNXT_FW_MAJ(bp) <= 20) {
8585 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8586 			hwrm_req_drop(bp, req);
8587 			return;
8588 		}
8589 		hwrm_req_hold(bp, req0);
8590 	}
8591 	hwrm_req_hold(bp, req);
8592 	for (i = 0; i < bp->cp_nr_rings; i++) {
8593 		struct bnxt_napi *bnapi = bp->bnapi[i];
8594 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8595 
8596 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8597 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8598 			if (req0) {
8599 				req0->stat_ctx_id = req->stat_ctx_id;
8600 				hwrm_req_send(bp, req0);
8601 			}
8602 			hwrm_req_send(bp, req);
8603 
8604 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8605 		}
8606 	}
8607 	hwrm_req_drop(bp, req);
8608 	if (req0)
8609 		hwrm_req_drop(bp, req0);
8610 }
8611 
8612 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8613 {
8614 	struct hwrm_stat_ctx_alloc_output *resp;
8615 	struct hwrm_stat_ctx_alloc_input *req;
8616 	int rc, i;
8617 
8618 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8619 		return 0;
8620 
8621 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8622 	if (rc)
8623 		return rc;
8624 
8625 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8626 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8627 
8628 	resp = hwrm_req_hold(bp, req);
8629 	for (i = 0; i < bp->cp_nr_rings; i++) {
8630 		struct bnxt_napi *bnapi = bp->bnapi[i];
8631 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8632 
8633 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8634 
8635 		rc = hwrm_req_send(bp, req);
8636 		if (rc)
8637 			break;
8638 
8639 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8640 
8641 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8642 	}
8643 	hwrm_req_drop(bp, req);
8644 	return rc;
8645 }
8646 
8647 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8648 {
8649 	struct hwrm_func_qcfg_output *resp;
8650 	struct hwrm_func_qcfg_input *req;
8651 	u16 flags;
8652 	int rc;
8653 
8654 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8655 	if (rc)
8656 		return rc;
8657 
8658 	req->fid = cpu_to_le16(0xffff);
8659 	resp = hwrm_req_hold(bp, req);
8660 	rc = hwrm_req_send(bp, req);
8661 	if (rc)
8662 		goto func_qcfg_exit;
8663 
8664 	flags = le16_to_cpu(resp->flags);
8665 #ifdef CONFIG_BNXT_SRIOV
8666 	if (BNXT_VF(bp)) {
8667 		struct bnxt_vf_info *vf = &bp->vf;
8668 
8669 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8670 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8671 			vf->flags |= BNXT_VF_TRUST;
8672 		else
8673 			vf->flags &= ~BNXT_VF_TRUST;
8674 	} else {
8675 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8676 	}
8677 #endif
8678 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8679 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8680 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8681 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8682 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8683 	}
8684 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8685 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8686 
8687 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8688 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8689 
8690 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8691 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8692 	if (resp->roce_bidi_opt_mode &
8693 	    FUNC_QCFG_RESP_ROCE_BIDI_OPT_MODE_DEDICATED)
8694 		bp->cos0_cos1_shared = 1;
8695 	else
8696 		bp->cos0_cos1_shared = 0;
8697 
8698 	switch (resp->port_partition_type) {
8699 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8700 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2:
8701 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8702 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8703 		bp->port_partition_type = resp->port_partition_type;
8704 		break;
8705 	}
8706 	if (bp->hwrm_spec_code < 0x10707 ||
8707 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8708 		bp->br_mode = BRIDGE_MODE_VEB;
8709 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8710 		bp->br_mode = BRIDGE_MODE_VEPA;
8711 	else
8712 		bp->br_mode = BRIDGE_MODE_UNDEF;
8713 
8714 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8715 	if (!bp->max_mtu)
8716 		bp->max_mtu = BNXT_MAX_MTU;
8717 
8718 	if (bp->db_size)
8719 		goto func_qcfg_exit;
8720 
8721 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8722 	if (BNXT_CHIP_P5(bp)) {
8723 		if (BNXT_PF(bp))
8724 			bp->db_offset = DB_PF_OFFSET_P5;
8725 		else
8726 			bp->db_offset = DB_VF_OFFSET_P5;
8727 	}
8728 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8729 				 1024);
8730 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8731 	    bp->db_size <= bp->db_offset)
8732 		bp->db_size = pci_resource_len(bp->pdev, 2);
8733 
8734 func_qcfg_exit:
8735 	hwrm_req_drop(bp, req);
8736 	return rc;
8737 }
8738 
8739 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8740 				      u8 init_val, u8 init_offset,
8741 				      bool init_mask_set)
8742 {
8743 	ctxm->init_value = init_val;
8744 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8745 	if (init_mask_set)
8746 		ctxm->init_offset = init_offset * 4;
8747 	else
8748 		ctxm->init_value = 0;
8749 }
8750 
8751 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8752 {
8753 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8754 	u16 type;
8755 
8756 	for (type = 0; type < ctx_max; type++) {
8757 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8758 		int n = 1;
8759 
8760 		if (!ctxm->max_entries || ctxm->pg_info)
8761 			continue;
8762 
8763 		if (ctxm->instance_bmap)
8764 			n = hweight32(ctxm->instance_bmap);
8765 		ctxm->pg_info = kzalloc_objs(*ctxm->pg_info, n);
8766 		if (!ctxm->pg_info)
8767 			return -ENOMEM;
8768 	}
8769 	return 0;
8770 }
8771 
8772 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8773 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8774 
8775 #define BNXT_CTX_INIT_VALID(flags)	\
8776 	(!!((flags) &			\
8777 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8778 
8779 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8780 {
8781 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8782 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8783 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8784 	u16 type, next_type = 0;
8785 	int rc;
8786 
8787 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8788 	if (rc)
8789 		return rc;
8790 
8791 	if (!ctx) {
8792 		ctx = kzalloc_obj(*ctx);
8793 		if (!ctx)
8794 			return -ENOMEM;
8795 		bp->ctx = ctx;
8796 	}
8797 
8798 	resp = hwrm_req_hold(bp, req);
8799 
8800 	for (type = 0; type < BNXT_CTX_V2_MAX; type = next_type) {
8801 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8802 		u8 init_val, init_off, i;
8803 		u32 max_entries;
8804 		u16 entry_size;
8805 		__le32 *p;
8806 		u32 flags;
8807 
8808 		req->type = cpu_to_le16(type);
8809 		rc = hwrm_req_send(bp, req);
8810 		if (rc)
8811 			goto ctx_done;
8812 		flags = le32_to_cpu(resp->flags);
8813 		next_type = le16_to_cpu(resp->next_valid_type);
8814 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8815 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8816 			continue;
8817 		}
8818 		entry_size = le16_to_cpu(resp->entry_size);
8819 		max_entries = le32_to_cpu(resp->max_num_entries);
8820 		if (ctxm->mem_valid) {
8821 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8822 			    ctxm->entry_size != entry_size ||
8823 			    ctxm->max_entries != max_entries)
8824 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8825 			else
8826 				continue;
8827 		}
8828 		ctxm->type = type;
8829 		ctxm->entry_size = entry_size;
8830 		ctxm->flags = flags;
8831 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8832 		ctxm->entry_multiple = resp->entry_multiple;
8833 		ctxm->max_entries = max_entries;
8834 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8835 		init_val = resp->ctx_init_value;
8836 		init_off = resp->ctx_init_offset;
8837 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8838 					  BNXT_CTX_INIT_VALID(flags));
8839 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8840 					      BNXT_MAX_SPLIT_ENTRY);
8841 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8842 		     i++, p++)
8843 			ctxm->split[i] = le32_to_cpu(*p);
8844 	}
8845 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8846 
8847 ctx_done:
8848 	hwrm_req_drop(bp, req);
8849 	return rc;
8850 }
8851 
8852 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8853 {
8854 	struct hwrm_func_backing_store_qcaps_output *resp;
8855 	struct hwrm_func_backing_store_qcaps_input *req;
8856 	int rc;
8857 
8858 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8859 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8860 		return 0;
8861 
8862 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8863 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8864 
8865 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8866 	if (rc)
8867 		return rc;
8868 
8869 	resp = hwrm_req_hold(bp, req);
8870 	rc = hwrm_req_send_silent(bp, req);
8871 	if (!rc) {
8872 		struct bnxt_ctx_mem_type *ctxm;
8873 		struct bnxt_ctx_mem_info *ctx;
8874 		u8 init_val, init_idx = 0;
8875 		u16 init_mask;
8876 
8877 		ctx = bp->ctx;
8878 		if (!ctx) {
8879 			ctx = kzalloc_obj(*ctx);
8880 			if (!ctx) {
8881 				rc = -ENOMEM;
8882 				goto ctx_err;
8883 			}
8884 			bp->ctx = ctx;
8885 		}
8886 		init_val = resp->ctx_kind_initializer;
8887 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8888 
8889 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8890 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8891 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8892 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8893 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8894 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8895 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8896 					  (init_mask & (1 << init_idx++)) != 0);
8897 
8898 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8899 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8900 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8901 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8902 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8903 					  (init_mask & (1 << init_idx++)) != 0);
8904 
8905 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8906 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8907 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8908 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8909 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8910 					  (init_mask & (1 << init_idx++)) != 0);
8911 
8912 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8913 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8914 		ctxm->max_entries = ctxm->vnic_entries +
8915 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8916 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8917 		bnxt_init_ctx_initializer(ctxm, init_val,
8918 					  resp->vnic_init_offset,
8919 					  (init_mask & (1 << init_idx++)) != 0);
8920 
8921 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8922 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8923 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8924 		bnxt_init_ctx_initializer(ctxm, init_val,
8925 					  resp->stat_init_offset,
8926 					  (init_mask & (1 << init_idx++)) != 0);
8927 
8928 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8929 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8930 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8931 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8932 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8933 		if (!ctxm->entry_multiple)
8934 			ctxm->entry_multiple = 1;
8935 
8936 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8937 
8938 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8939 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8940 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8941 		ctxm->mrav_num_entries_units =
8942 			le16_to_cpu(resp->mrav_num_entries_units);
8943 		bnxt_init_ctx_initializer(ctxm, init_val,
8944 					  resp->mrav_init_offset,
8945 					  (init_mask & (1 << init_idx++)) != 0);
8946 
8947 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8948 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8949 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8950 
8951 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8952 		if (!ctx->tqm_fp_rings_count)
8953 			ctx->tqm_fp_rings_count = bp->max_q;
8954 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8955 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8956 
8957 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8958 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8959 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8960 
8961 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8962 	} else {
8963 		rc = 0;
8964 	}
8965 ctx_err:
8966 	hwrm_req_drop(bp, req);
8967 	return rc;
8968 }
8969 
8970 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8971 				  __le64 *pg_dir)
8972 {
8973 	if (!rmem->nr_pages)
8974 		return;
8975 
8976 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8977 	if (rmem->depth >= 1) {
8978 		if (rmem->depth == 2)
8979 			*pg_attr |= 2;
8980 		else
8981 			*pg_attr |= 1;
8982 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8983 	} else {
8984 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8985 	}
8986 }
8987 
8988 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8989 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8990 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8991 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8992 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8993 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8994 
8995 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8996 {
8997 	struct hwrm_func_backing_store_cfg_input *req;
8998 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8999 	struct bnxt_ctx_pg_info *ctx_pg;
9000 	struct bnxt_ctx_mem_type *ctxm;
9001 	void **__req = (void **)&req;
9002 	u32 req_len = sizeof(*req);
9003 	__le32 *num_entries;
9004 	__le64 *pg_dir;
9005 	u32 flags = 0;
9006 	u8 *pg_attr;
9007 	u32 ena;
9008 	int rc;
9009 	int i;
9010 
9011 	if (!ctx)
9012 		return 0;
9013 
9014 	if (req_len > bp->hwrm_max_ext_req_len)
9015 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
9016 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
9017 	if (rc)
9018 		return rc;
9019 
9020 	req->enables = cpu_to_le32(enables);
9021 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
9022 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9023 		ctx_pg = ctxm->pg_info;
9024 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
9025 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
9026 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
9027 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
9028 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9029 				      &req->qpc_pg_size_qpc_lvl,
9030 				      &req->qpc_page_dir);
9031 
9032 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
9033 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
9034 	}
9035 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
9036 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9037 		ctx_pg = ctxm->pg_info;
9038 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
9039 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
9040 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
9041 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9042 				      &req->srq_pg_size_srq_lvl,
9043 				      &req->srq_page_dir);
9044 	}
9045 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
9046 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9047 		ctx_pg = ctxm->pg_info;
9048 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
9049 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
9050 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
9051 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9052 				      &req->cq_pg_size_cq_lvl,
9053 				      &req->cq_page_dir);
9054 	}
9055 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
9056 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9057 		ctx_pg = ctxm->pg_info;
9058 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
9059 		req->vnic_num_ring_table_entries =
9060 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
9061 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
9062 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9063 				      &req->vnic_pg_size_vnic_lvl,
9064 				      &req->vnic_page_dir);
9065 	}
9066 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
9067 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9068 		ctx_pg = ctxm->pg_info;
9069 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
9070 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
9071 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9072 				      &req->stat_pg_size_stat_lvl,
9073 				      &req->stat_page_dir);
9074 	}
9075 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
9076 		u32 units;
9077 
9078 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9079 		ctx_pg = ctxm->pg_info;
9080 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
9081 		units = ctxm->mrav_num_entries_units;
9082 		if (units) {
9083 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
9084 			u32 entries;
9085 
9086 			num_mr = ctx_pg->entries - num_ah;
9087 			entries = ((num_mr / units) << 16) | (num_ah / units);
9088 			req->mrav_num_entries = cpu_to_le32(entries);
9089 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
9090 		}
9091 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
9092 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9093 				      &req->mrav_pg_size_mrav_lvl,
9094 				      &req->mrav_page_dir);
9095 	}
9096 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
9097 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9098 		ctx_pg = ctxm->pg_info;
9099 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
9100 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
9101 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9102 				      &req->tim_pg_size_tim_lvl,
9103 				      &req->tim_page_dir);
9104 	}
9105 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9106 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
9107 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
9108 	     pg_dir = &req->tqm_sp_page_dir,
9109 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
9110 	     ctx_pg = ctxm->pg_info;
9111 	     i < BNXT_MAX_TQM_RINGS;
9112 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
9113 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
9114 		if (!(enables & ena))
9115 			continue;
9116 
9117 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
9118 		*num_entries = cpu_to_le32(ctx_pg->entries);
9119 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
9120 	}
9121 	req->flags = cpu_to_le32(flags);
9122 	return hwrm_req_send(bp, req);
9123 }
9124 
9125 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
9126 				  struct bnxt_ctx_pg_info *ctx_pg)
9127 {
9128 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9129 
9130 	rmem->page_size = BNXT_PAGE_SIZE;
9131 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
9132 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
9133 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
9134 	if (rmem->depth >= 1)
9135 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
9136 	return bnxt_alloc_ring(bp, rmem);
9137 }
9138 
9139 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
9140 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
9141 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
9142 {
9143 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9144 	int rc;
9145 
9146 	if (!mem_size)
9147 		return -EINVAL;
9148 
9149 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
9150 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
9151 		ctx_pg->nr_pages = 0;
9152 		return -EINVAL;
9153 	}
9154 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
9155 		int nr_tbls, i;
9156 
9157 		rmem->depth = 2;
9158 		ctx_pg->ctx_pg_tbl = kzalloc_objs(ctx_pg, MAX_CTX_PAGES);
9159 		if (!ctx_pg->ctx_pg_tbl)
9160 			return -ENOMEM;
9161 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
9162 		rmem->nr_pages = nr_tbls;
9163 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
9164 		if (rc)
9165 			return rc;
9166 		for (i = 0; i < nr_tbls; i++) {
9167 			struct bnxt_ctx_pg_info *pg_tbl;
9168 
9169 			pg_tbl = kzalloc_obj(*pg_tbl);
9170 			if (!pg_tbl)
9171 				return -ENOMEM;
9172 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
9173 			rmem = &pg_tbl->ring_mem;
9174 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
9175 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
9176 			rmem->depth = 1;
9177 			rmem->nr_pages = MAX_CTX_PAGES;
9178 			rmem->ctx_mem = ctxm;
9179 			if (i == (nr_tbls - 1)) {
9180 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
9181 
9182 				if (rem)
9183 					rmem->nr_pages = rem;
9184 			}
9185 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
9186 			if (rc)
9187 				break;
9188 		}
9189 	} else {
9190 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
9191 		if (rmem->nr_pages > 1 || depth)
9192 			rmem->depth = 1;
9193 		rmem->ctx_mem = ctxm;
9194 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
9195 	}
9196 	return rc;
9197 }
9198 
9199 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
9200 				    struct bnxt_ctx_pg_info *ctx_pg,
9201 				    void *buf, size_t offset, size_t head,
9202 				    size_t tail)
9203 {
9204 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9205 	size_t nr_pages = ctx_pg->nr_pages;
9206 	int page_size = rmem->page_size;
9207 	size_t len = 0, total_len = 0;
9208 	u16 depth = rmem->depth;
9209 
9210 	tail %= nr_pages * page_size;
9211 	do {
9212 		if (depth > 1) {
9213 			int i = head / (page_size * MAX_CTX_PAGES);
9214 			struct bnxt_ctx_pg_info *pg_tbl;
9215 
9216 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9217 			rmem = &pg_tbl->ring_mem;
9218 		}
9219 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
9220 		head += len;
9221 		offset += len;
9222 		total_len += len;
9223 		if (head >= nr_pages * page_size)
9224 			head = 0;
9225 	} while (head != tail);
9226 	return total_len;
9227 }
9228 
9229 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
9230 				  struct bnxt_ctx_pg_info *ctx_pg)
9231 {
9232 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9233 
9234 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
9235 	    ctx_pg->ctx_pg_tbl) {
9236 		int i, nr_tbls = rmem->nr_pages;
9237 
9238 		for (i = 0; i < nr_tbls; i++) {
9239 			struct bnxt_ctx_pg_info *pg_tbl;
9240 			struct bnxt_ring_mem_info *rmem2;
9241 
9242 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9243 			if (!pg_tbl)
9244 				continue;
9245 			rmem2 = &pg_tbl->ring_mem;
9246 			bnxt_free_ring(bp, rmem2);
9247 			ctx_pg->ctx_pg_arr[i] = NULL;
9248 			kfree(pg_tbl);
9249 			ctx_pg->ctx_pg_tbl[i] = NULL;
9250 		}
9251 		kfree(ctx_pg->ctx_pg_tbl);
9252 		ctx_pg->ctx_pg_tbl = NULL;
9253 	}
9254 	bnxt_free_ring(bp, rmem);
9255 	ctx_pg->nr_pages = 0;
9256 }
9257 
9258 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
9259 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
9260 				   u8 pg_lvl)
9261 {
9262 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9263 	int i, rc = 0, n = 1;
9264 	u32 mem_size;
9265 
9266 	if (!ctxm->entry_size || !ctx_pg)
9267 		return -EINVAL;
9268 	if (ctxm->instance_bmap)
9269 		n = hweight32(ctxm->instance_bmap);
9270 	if (ctxm->entry_multiple)
9271 		entries = roundup(entries, ctxm->entry_multiple);
9272 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
9273 	mem_size = entries * ctxm->entry_size;
9274 	for (i = 0; i < n && !rc; i++) {
9275 		ctx_pg[i].entries = entries;
9276 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
9277 					    ctxm->init_value ? ctxm : NULL);
9278 	}
9279 	if (!rc)
9280 		ctxm->mem_valid = 1;
9281 	return rc;
9282 }
9283 
9284 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
9285 					       struct bnxt_ctx_mem_type *ctxm,
9286 					       bool last)
9287 {
9288 	struct hwrm_func_backing_store_cfg_v2_input *req;
9289 	u32 instance_bmap = ctxm->instance_bmap;
9290 	int i, j, rc = 0, n = 1;
9291 	__le32 *p;
9292 
9293 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
9294 		return 0;
9295 
9296 	if (instance_bmap)
9297 		n = hweight32(ctxm->instance_bmap);
9298 	else
9299 		instance_bmap = 1;
9300 
9301 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
9302 	if (rc)
9303 		return rc;
9304 	hwrm_req_hold(bp, req);
9305 	req->type = cpu_to_le16(ctxm->type);
9306 	req->entry_size = cpu_to_le16(ctxm->entry_size);
9307 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
9308 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
9309 		struct bnxt_bs_trace_info *bs_trace;
9310 		u32 enables;
9311 
9312 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
9313 		req->enables = cpu_to_le32(enables);
9314 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
9315 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
9316 	}
9317 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
9318 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
9319 		p[i] = cpu_to_le32(ctxm->split[i]);
9320 	for (i = 0, j = 0; j < n && !rc; i++) {
9321 		struct bnxt_ctx_pg_info *ctx_pg;
9322 
9323 		if (!(instance_bmap & (1 << i)))
9324 			continue;
9325 		req->instance = cpu_to_le16(i);
9326 		ctx_pg = &ctxm->pg_info[j++];
9327 		if (!ctx_pg->entries)
9328 			continue;
9329 		req->num_entries = cpu_to_le32(ctx_pg->entries);
9330 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9331 				      &req->page_size_pbl_level,
9332 				      &req->page_dir);
9333 		if (last && j == n)
9334 			req->flags =
9335 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
9336 		rc = hwrm_req_send(bp, req);
9337 	}
9338 	hwrm_req_drop(bp, req);
9339 	return rc;
9340 }
9341 
9342 static int bnxt_backing_store_cfg_v2(struct bnxt *bp)
9343 {
9344 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9345 	struct bnxt_ctx_mem_type *ctxm;
9346 	u16 last_type = BNXT_CTX_INV;
9347 	int rc = 0;
9348 	u16 type;
9349 
9350 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_QPC; type++) {
9351 		ctxm = &ctx->ctx_arr[type];
9352 		if (!bnxt_bs_trace_avail(bp, type))
9353 			continue;
9354 		if (!ctxm->mem_valid) {
9355 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9356 						     ctxm->max_entries, 1);
9357 			if (rc) {
9358 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9359 					    type);
9360 				continue;
9361 			}
9362 			bnxt_bs_trace_init(bp, ctxm);
9363 		}
9364 		last_type = type;
9365 	}
9366 
9367 	if (last_type == BNXT_CTX_INV) {
9368 		for (type = 0; type < BNXT_CTX_MAX; type++) {
9369 			ctxm = &ctx->ctx_arr[type];
9370 			if (ctxm->mem_valid)
9371 				last_type = type;
9372 		}
9373 		if (last_type == BNXT_CTX_INV)
9374 			return 0;
9375 	}
9376 	ctx->ctx_arr[last_type].last = 1;
9377 
9378 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9379 		ctxm = &ctx->ctx_arr[type];
9380 
9381 		if (!ctxm->mem_valid)
9382 			continue;
9383 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9384 		if (rc)
9385 			return rc;
9386 	}
9387 	return 0;
9388 }
9389 
9390 /**
9391  * __bnxt_copy_ctx_mem - copy host context memory
9392  * @bp: The driver context
9393  * @ctxm: The pointer to the context memory type
9394  * @buf: The destination buffer or NULL to just obtain the length
9395  * @offset: The buffer offset to copy the data to
9396  * @head: The head offset of context memory to copy from
9397  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9398  *
9399  * This function is called for debugging purposes to dump the host context
9400  * used by the chip.
9401  *
9402  * Return: Length of memory copied
9403  */
9404 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9405 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9406 				  size_t offset, size_t head, size_t tail)
9407 {
9408 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9409 	size_t len = 0, total_len = 0;
9410 	int i, n = 1;
9411 
9412 	if (!ctx_pg)
9413 		return 0;
9414 
9415 	if (ctxm->instance_bmap)
9416 		n = hweight32(ctxm->instance_bmap);
9417 	for (i = 0; i < n; i++) {
9418 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9419 					    tail);
9420 		offset += len;
9421 		total_len += len;
9422 	}
9423 	return total_len;
9424 }
9425 
9426 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9427 			 void *buf, size_t offset)
9428 {
9429 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9430 
9431 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9432 }
9433 
9434 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9435 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9436 {
9437 	struct bnxt_ctx_pg_info *ctx_pg;
9438 	int i, n = 1;
9439 
9440 	ctxm->last = 0;
9441 
9442 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9443 		return;
9444 
9445 	ctx_pg = ctxm->pg_info;
9446 	if (ctx_pg) {
9447 		if (ctxm->instance_bmap)
9448 			n = hweight32(ctxm->instance_bmap);
9449 		for (i = 0; i < n; i++)
9450 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9451 
9452 		kfree(ctx_pg);
9453 		ctxm->pg_info = NULL;
9454 		ctxm->mem_valid = 0;
9455 	}
9456 	memset(ctxm, 0, sizeof(*ctxm));
9457 }
9458 
9459 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9460 {
9461 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9462 	u16 type;
9463 
9464 	if (!ctx)
9465 		return;
9466 
9467 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9468 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9469 
9470 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9471 	if (force) {
9472 		kfree(ctx);
9473 		bp->ctx = NULL;
9474 	}
9475 }
9476 
9477 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9478 {
9479 	struct bnxt_ctx_mem_type *ctxm;
9480 	struct bnxt_ctx_mem_info *ctx;
9481 	u32 l2_qps, qp1_qps, max_qps;
9482 	u32 ena, entries_sp, entries;
9483 	u32 srqs, max_srqs, min;
9484 	u32 num_mr, num_ah;
9485 	u32 extra_srqs = 0;
9486 	u32 extra_qps = 0;
9487 	u32 fast_qpmd_qps;
9488 	u8 pg_lvl = 1;
9489 	int i, rc;
9490 
9491 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9492 	if (rc) {
9493 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9494 			   rc);
9495 		return rc;
9496 	}
9497 	ctx = bp->ctx;
9498 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9499 		return 0;
9500 
9501 	ena = 0;
9502 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
9503 		goto skip_legacy;
9504 
9505 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9506 	l2_qps = ctxm->qp_l2_entries;
9507 	qp1_qps = ctxm->qp_qp1_entries;
9508 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9509 	max_qps = ctxm->max_entries;
9510 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9511 	srqs = ctxm->srq_l2_entries;
9512 	max_srqs = ctxm->max_entries;
9513 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9514 		pg_lvl = 2;
9515 		if (BNXT_SW_RES_LMT(bp)) {
9516 			extra_qps = max_qps - l2_qps - qp1_qps;
9517 			extra_srqs = max_srqs - srqs;
9518 		} else {
9519 			extra_qps = min_t(u32, 65536,
9520 					  max_qps - l2_qps - qp1_qps);
9521 			/* allocate extra qps if fw supports RoCE fast qp
9522 			 * destroy feature
9523 			 */
9524 			extra_qps += fast_qpmd_qps;
9525 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9526 		}
9527 		if (fast_qpmd_qps)
9528 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9529 	}
9530 
9531 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9532 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9533 				     pg_lvl);
9534 	if (rc)
9535 		return rc;
9536 
9537 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9538 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9539 	if (rc)
9540 		return rc;
9541 
9542 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9543 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9544 				     extra_qps * 2, pg_lvl);
9545 	if (rc)
9546 		return rc;
9547 
9548 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9549 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9550 	if (rc)
9551 		return rc;
9552 
9553 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9554 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9555 	if (rc)
9556 		return rc;
9557 
9558 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9559 		goto skip_rdma;
9560 
9561 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9562 	if (BNXT_SW_RES_LMT(bp) &&
9563 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9564 		num_ah = ctxm->mrav_av_entries;
9565 		num_mr = ctxm->max_entries - num_ah;
9566 	} else {
9567 		/* 128K extra is needed to accommodate static AH context
9568 		 * allocation by f/w.
9569 		 */
9570 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9571 		num_ah = min_t(u32, num_mr, 1024 * 128);
9572 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9573 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9574 			ctxm->mrav_av_entries = num_ah;
9575 	}
9576 
9577 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9578 	if (rc)
9579 		return rc;
9580 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9581 
9582 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9583 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9584 	if (rc)
9585 		return rc;
9586 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9587 
9588 skip_rdma:
9589 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9590 	min = ctxm->min_entries;
9591 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9592 		     2 * (extra_qps + qp1_qps) + min;
9593 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9594 	if (rc)
9595 		return rc;
9596 
9597 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9598 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9599 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9600 	if (rc)
9601 		return rc;
9602 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9603 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9604 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9605 
9606 skip_legacy:
9607 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9608 		rc = bnxt_backing_store_cfg_v2(bp);
9609 	else
9610 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9611 	if (rc) {
9612 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9613 			   rc);
9614 		return rc;
9615 	}
9616 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9617 	return 0;
9618 }
9619 
9620 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9621 {
9622 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9623 	u16 page_attr;
9624 	int rc;
9625 
9626 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9627 		return 0;
9628 
9629 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9630 	if (rc)
9631 		return rc;
9632 
9633 	if (BNXT_PAGE_SIZE == 0x2000)
9634 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9635 	else if (BNXT_PAGE_SIZE == 0x10000)
9636 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9637 	else
9638 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9639 	req->pg_size_lvl = cpu_to_le16(page_attr |
9640 				       bp->fw_crash_mem->ring_mem.depth);
9641 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9642 	req->size = cpu_to_le32(bp->fw_crash_len);
9643 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9644 	return hwrm_req_send(bp, req);
9645 }
9646 
9647 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9648 {
9649 	if (bp->fw_crash_mem) {
9650 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9651 		kfree(bp->fw_crash_mem);
9652 		bp->fw_crash_mem = NULL;
9653 	}
9654 }
9655 
9656 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9657 {
9658 	u32 mem_size = 0;
9659 	int rc;
9660 
9661 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9662 		return 0;
9663 
9664 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9665 	if (rc)
9666 		return rc;
9667 
9668 	mem_size = round_up(mem_size, 4);
9669 
9670 	/* keep and use the existing pages */
9671 	if (bp->fw_crash_mem &&
9672 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9673 		goto alloc_done;
9674 
9675 	if (bp->fw_crash_mem)
9676 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9677 	else
9678 		bp->fw_crash_mem = kzalloc_obj(*bp->fw_crash_mem);
9679 	if (!bp->fw_crash_mem)
9680 		return -ENOMEM;
9681 
9682 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9683 	if (rc) {
9684 		bnxt_free_crash_dump_mem(bp);
9685 		return rc;
9686 	}
9687 
9688 alloc_done:
9689 	bp->fw_crash_len = mem_size;
9690 	return 0;
9691 }
9692 
9693 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9694 {
9695 	struct hwrm_func_resource_qcaps_output *resp;
9696 	struct hwrm_func_resource_qcaps_input *req;
9697 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9698 	int rc;
9699 
9700 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9701 	if (rc)
9702 		return rc;
9703 
9704 	req->fid = cpu_to_le16(0xffff);
9705 	resp = hwrm_req_hold(bp, req);
9706 	rc = hwrm_req_send_silent(bp, req);
9707 	if (rc)
9708 		goto hwrm_func_resc_qcaps_exit;
9709 
9710 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9711 	if (!all)
9712 		goto hwrm_func_resc_qcaps_exit;
9713 
9714 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9715 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9716 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9717 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9718 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9719 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9720 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9721 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9722 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9723 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9724 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9725 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9726 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9727 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9728 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9729 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9730 
9731 	if (hw_resc->max_rsscos_ctxs >=
9732 	    hw_resc->max_vnics * BNXT_LARGE_RSS_TO_VNIC_RATIO)
9733 		bp->rss_cap |= BNXT_RSS_CAP_LARGE_RSS_CTX;
9734 
9735 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9736 		u16 max_msix = le16_to_cpu(resp->max_msix);
9737 
9738 		hw_resc->max_nqs = max_msix;
9739 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9740 	}
9741 
9742 	if (BNXT_PF(bp)) {
9743 		struct bnxt_pf_info *pf = &bp->pf;
9744 
9745 		pf->vf_resv_strategy =
9746 			le16_to_cpu(resp->vf_reservation_strategy);
9747 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9748 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9749 	}
9750 hwrm_func_resc_qcaps_exit:
9751 	hwrm_req_drop(bp, req);
9752 	return rc;
9753 }
9754 
9755 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9756 {
9757 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9758 	struct hwrm_port_mac_ptp_qcfg_input *req;
9759 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9760 	u8 flags;
9761 	int rc;
9762 
9763 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9764 		rc = -ENODEV;
9765 		goto no_ptp;
9766 	}
9767 
9768 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9769 	if (rc)
9770 		goto no_ptp;
9771 
9772 	req->port_id = cpu_to_le16(bp->pf.port_id);
9773 	resp = hwrm_req_hold(bp, req);
9774 	rc = hwrm_req_send(bp, req);
9775 	if (rc)
9776 		goto exit;
9777 
9778 	flags = resp->flags;
9779 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9780 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9781 		rc = -ENODEV;
9782 		goto exit;
9783 	}
9784 	if (!ptp) {
9785 		ptp = kzalloc_obj(*ptp);
9786 		if (!ptp) {
9787 			rc = -ENOMEM;
9788 			goto exit;
9789 		}
9790 		ptp->bp = bp;
9791 		bp->ptp_cfg = ptp;
9792 	}
9793 
9794 	if (flags &
9795 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9796 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9797 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9798 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9799 	} else if (BNXT_CHIP_P5(bp)) {
9800 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9801 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9802 	} else {
9803 		rc = -ENODEV;
9804 		goto exit;
9805 	}
9806 	ptp->rtc_configured =
9807 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9808 	rc = bnxt_ptp_init(bp);
9809 	if (rc)
9810 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9811 exit:
9812 	hwrm_req_drop(bp, req);
9813 	if (!rc)
9814 		return 0;
9815 
9816 no_ptp:
9817 	bnxt_ptp_clear(bp);
9818 	kfree(ptp);
9819 	bp->ptp_cfg = NULL;
9820 	return rc;
9821 }
9822 
9823 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9824 {
9825 	u32 flags, flags_ext, flags_ext2, flags_ext3;
9826 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9827 	struct hwrm_func_qcaps_output *resp;
9828 	struct hwrm_func_qcaps_input *req;
9829 	int rc;
9830 
9831 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9832 	if (rc)
9833 		return rc;
9834 
9835 	req->fid = cpu_to_le16(0xffff);
9836 	resp = hwrm_req_hold(bp, req);
9837 	rc = hwrm_req_send(bp, req);
9838 	if (rc)
9839 		goto hwrm_func_qcaps_exit;
9840 
9841 	flags = le32_to_cpu(resp->flags);
9842 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9843 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9844 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9845 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9846 	if (flags & FUNC_QCAPS_RESP_FLAGS_LINK_ADMIN_STATUS_SUPPORTED)
9847 		bp->fw_cap |= BNXT_FW_CAP_LINK_ADMIN;
9848 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9849 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9850 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9851 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9852 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9853 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9854 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9855 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9856 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9857 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9858 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9859 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9860 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9861 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9862 
9863 	flags_ext = le32_to_cpu(resp->flags_ext);
9864 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9865 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9866 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9867 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9868 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PTM_SUPPORTED)
9869 		bp->fw_cap |= BNXT_FW_CAP_PTP_PTM;
9870 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9871 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9872 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9873 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9874 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9875 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9876 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED)
9877 		bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2;
9878 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9879 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9880 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9881 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9882 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9883 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9884 
9885 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9886 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9887 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9888 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9889 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9890 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9891 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9892 	if (flags_ext2 &
9893 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9894 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9895 	if (BNXT_PF(bp) &&
9896 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9897 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9898 
9899 	flags_ext3 = le32_to_cpu(resp->flags_ext3);
9900 	if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_ROCE_VF_DYN_ALLOC_SUPPORT)
9901 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_DYN_ALLOC_SUPPORT;
9902 	if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_MIRROR_ON_ROCE_SUPPORTED)
9903 		bp->fw_cap |= BNXT_FW_CAP_MIRROR_ON_ROCE;
9904 
9905 	bp->tx_push_thresh = 0;
9906 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9907 	    BNXT_FW_MAJ(bp) > 217)
9908 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9909 
9910 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9911 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9912 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9913 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9914 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9915 	if (!hw_resc->max_hw_ring_grps)
9916 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9917 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9918 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9919 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9920 
9921 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9922 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9923 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9924 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9925 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9926 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9927 
9928 	if (BNXT_PF(bp)) {
9929 		struct bnxt_pf_info *pf = &bp->pf;
9930 
9931 		pf->fw_fid = le16_to_cpu(resp->fid);
9932 		pf->port_id = le16_to_cpu(resp->port_id);
9933 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9934 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9935 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9936 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9937 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9938 			bp->flags |= BNXT_FLAG_WOL_CAP;
9939 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9940 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9941 		} else {
9942 			bnxt_ptp_clear(bp);
9943 			kfree(bp->ptp_cfg);
9944 			bp->ptp_cfg = NULL;
9945 		}
9946 	} else {
9947 #ifdef CONFIG_BNXT_SRIOV
9948 		struct bnxt_vf_info *vf = &bp->vf;
9949 
9950 		vf->fw_fid = le16_to_cpu(resp->fid);
9951 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9952 #endif
9953 	}
9954 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9955 
9956 hwrm_func_qcaps_exit:
9957 	hwrm_req_drop(bp, req);
9958 	return rc;
9959 }
9960 
9961 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9962 {
9963 	struct hwrm_dbg_qcaps_output *resp;
9964 	struct hwrm_dbg_qcaps_input *req;
9965 	int rc;
9966 
9967 	bp->fw_dbg_cap = 0;
9968 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9969 		return;
9970 
9971 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9972 	if (rc)
9973 		return;
9974 
9975 	req->fid = cpu_to_le16(0xffff);
9976 	resp = hwrm_req_hold(bp, req);
9977 	rc = hwrm_req_send(bp, req);
9978 	if (rc)
9979 		goto hwrm_dbg_qcaps_exit;
9980 
9981 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9982 
9983 hwrm_dbg_qcaps_exit:
9984 	hwrm_req_drop(bp, req);
9985 }
9986 
9987 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9988 
9989 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9990 {
9991 	int rc;
9992 
9993 	rc = __bnxt_hwrm_func_qcaps(bp);
9994 	if (rc)
9995 		return rc;
9996 
9997 	bnxt_hwrm_dbg_qcaps(bp);
9998 
9999 	rc = bnxt_hwrm_queue_qportcfg(bp);
10000 	if (rc) {
10001 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
10002 		return rc;
10003 	}
10004 	if (bp->hwrm_spec_code >= 0x10803) {
10005 		rc = bnxt_alloc_ctx_mem(bp);
10006 		if (rc)
10007 			return rc;
10008 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
10009 		if (!rc)
10010 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
10011 	}
10012 	return 0;
10013 }
10014 
10015 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
10016 {
10017 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
10018 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
10019 	u32 flags;
10020 	int rc;
10021 
10022 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
10023 		return 0;
10024 
10025 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
10026 	if (rc)
10027 		return rc;
10028 
10029 	resp = hwrm_req_hold(bp, req);
10030 	rc = hwrm_req_send(bp, req);
10031 	if (rc)
10032 		goto hwrm_cfa_adv_qcaps_exit;
10033 
10034 	flags = le32_to_cpu(resp->flags);
10035 	if (flags &
10036 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
10037 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
10038 
10039 	if (flags &
10040 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
10041 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
10042 
10043 	if (flags &
10044 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
10045 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
10046 
10047 hwrm_cfa_adv_qcaps_exit:
10048 	hwrm_req_drop(bp, req);
10049 	return rc;
10050 }
10051 
10052 static int __bnxt_alloc_fw_health(struct bnxt *bp)
10053 {
10054 	if (bp->fw_health)
10055 		return 0;
10056 
10057 	bp->fw_health = kzalloc_obj(*bp->fw_health);
10058 	if (!bp->fw_health)
10059 		return -ENOMEM;
10060 
10061 	mutex_init(&bp->fw_health->lock);
10062 	return 0;
10063 }
10064 
10065 static int bnxt_alloc_fw_health(struct bnxt *bp)
10066 {
10067 	int rc;
10068 
10069 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
10070 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
10071 		return 0;
10072 
10073 	rc = __bnxt_alloc_fw_health(bp);
10074 	if (rc) {
10075 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
10076 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10077 		return rc;
10078 	}
10079 
10080 	return 0;
10081 }
10082 
10083 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
10084 {
10085 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
10086 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
10087 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
10088 }
10089 
10090 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
10091 {
10092 	struct bnxt_fw_health *fw_health = bp->fw_health;
10093 	u32 reg_type;
10094 
10095 	if (!fw_health)
10096 		return;
10097 
10098 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
10099 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
10100 		fw_health->status_reliable = false;
10101 
10102 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
10103 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
10104 		fw_health->resets_reliable = false;
10105 }
10106 
10107 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
10108 {
10109 	void __iomem *hs;
10110 	u32 status_loc;
10111 	u32 reg_type;
10112 	u32 sig;
10113 
10114 	if (bp->fw_health)
10115 		bp->fw_health->status_reliable = false;
10116 
10117 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
10118 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
10119 
10120 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
10121 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
10122 		if (!bp->chip_num) {
10123 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
10124 			bp->chip_num = readl(bp->bar0 +
10125 					     BNXT_FW_HEALTH_WIN_BASE +
10126 					     BNXT_GRC_REG_CHIP_NUM);
10127 		}
10128 		if (!BNXT_CHIP_P5_PLUS(bp))
10129 			return;
10130 
10131 		status_loc = BNXT_GRC_REG_STATUS_P5 |
10132 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
10133 	} else {
10134 		status_loc = readl(hs + offsetof(struct hcomm_status,
10135 						 fw_status_loc));
10136 	}
10137 
10138 	if (__bnxt_alloc_fw_health(bp)) {
10139 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
10140 		return;
10141 	}
10142 
10143 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
10144 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
10145 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
10146 		__bnxt_map_fw_health_reg(bp, status_loc);
10147 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
10148 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
10149 	}
10150 
10151 	bp->fw_health->status_reliable = true;
10152 }
10153 
10154 static int bnxt_map_fw_health_regs(struct bnxt *bp)
10155 {
10156 	struct bnxt_fw_health *fw_health = bp->fw_health;
10157 	u32 reg_base = 0xffffffff;
10158 	int i;
10159 
10160 	bp->fw_health->status_reliable = false;
10161 	bp->fw_health->resets_reliable = false;
10162 	/* Only pre-map the monitoring GRC registers using window 3 */
10163 	for (i = 0; i < 4; i++) {
10164 		u32 reg = fw_health->regs[i];
10165 
10166 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
10167 			continue;
10168 		if (reg_base == 0xffffffff)
10169 			reg_base = reg & BNXT_GRC_BASE_MASK;
10170 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
10171 			return -ERANGE;
10172 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
10173 	}
10174 	bp->fw_health->status_reliable = true;
10175 	bp->fw_health->resets_reliable = true;
10176 	if (reg_base == 0xffffffff)
10177 		return 0;
10178 
10179 	__bnxt_map_fw_health_reg(bp, reg_base);
10180 	return 0;
10181 }
10182 
10183 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
10184 {
10185 	if (!bp->fw_health)
10186 		return;
10187 
10188 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
10189 		bp->fw_health->status_reliable = true;
10190 		bp->fw_health->resets_reliable = true;
10191 	} else {
10192 		bnxt_try_map_fw_health_reg(bp);
10193 	}
10194 }
10195 
10196 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
10197 {
10198 	struct bnxt_fw_health *fw_health = bp->fw_health;
10199 	struct hwrm_error_recovery_qcfg_output *resp;
10200 	struct hwrm_error_recovery_qcfg_input *req;
10201 	int rc, i;
10202 
10203 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
10204 		return 0;
10205 
10206 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
10207 	if (rc)
10208 		return rc;
10209 
10210 	resp = hwrm_req_hold(bp, req);
10211 	rc = hwrm_req_send(bp, req);
10212 	if (rc)
10213 		goto err_recovery_out;
10214 	fw_health->flags = le32_to_cpu(resp->flags);
10215 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
10216 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
10217 		rc = -EINVAL;
10218 		goto err_recovery_out;
10219 	}
10220 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
10221 	fw_health->master_func_wait_dsecs =
10222 		le32_to_cpu(resp->master_func_wait_period);
10223 	fw_health->normal_func_wait_dsecs =
10224 		le32_to_cpu(resp->normal_func_wait_period);
10225 	fw_health->post_reset_wait_dsecs =
10226 		le32_to_cpu(resp->master_func_wait_period_after_reset);
10227 	fw_health->post_reset_max_wait_dsecs =
10228 		le32_to_cpu(resp->max_bailout_time_after_reset);
10229 	fw_health->regs[BNXT_FW_HEALTH_REG] =
10230 		le32_to_cpu(resp->fw_health_status_reg);
10231 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
10232 		le32_to_cpu(resp->fw_heartbeat_reg);
10233 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
10234 		le32_to_cpu(resp->fw_reset_cnt_reg);
10235 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
10236 		le32_to_cpu(resp->reset_inprogress_reg);
10237 	fw_health->fw_reset_inprog_reg_mask =
10238 		le32_to_cpu(resp->reset_inprogress_reg_mask);
10239 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
10240 	if (fw_health->fw_reset_seq_cnt >= 16) {
10241 		rc = -EINVAL;
10242 		goto err_recovery_out;
10243 	}
10244 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
10245 		fw_health->fw_reset_seq_regs[i] =
10246 			le32_to_cpu(resp->reset_reg[i]);
10247 		fw_health->fw_reset_seq_vals[i] =
10248 			le32_to_cpu(resp->reset_reg_val[i]);
10249 		fw_health->fw_reset_seq_delay_msec[i] =
10250 			resp->delay_after_reset[i];
10251 	}
10252 err_recovery_out:
10253 	hwrm_req_drop(bp, req);
10254 	if (!rc)
10255 		rc = bnxt_map_fw_health_regs(bp);
10256 	if (rc)
10257 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10258 	return rc;
10259 }
10260 
10261 static int bnxt_hwrm_func_reset(struct bnxt *bp)
10262 {
10263 	struct hwrm_func_reset_input *req;
10264 	int rc;
10265 
10266 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
10267 	if (rc)
10268 		return rc;
10269 
10270 	req->enables = 0;
10271 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
10272 	return hwrm_req_send(bp, req);
10273 }
10274 
10275 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
10276 {
10277 	struct hwrm_nvm_get_dev_info_output nvm_info;
10278 
10279 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
10280 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
10281 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
10282 			 nvm_info.nvm_cfg_ver_upd);
10283 }
10284 
10285 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
10286 {
10287 	struct hwrm_queue_qportcfg_output *resp;
10288 	struct hwrm_queue_qportcfg_input *req;
10289 	u8 i, j, *qptr;
10290 	bool no_rdma;
10291 	int rc = 0;
10292 
10293 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
10294 	if (rc)
10295 		return rc;
10296 
10297 	resp = hwrm_req_hold(bp, req);
10298 	rc = hwrm_req_send(bp, req);
10299 	if (rc)
10300 		goto qportcfg_exit;
10301 
10302 	if (!resp->max_configurable_queues) {
10303 		rc = -EINVAL;
10304 		goto qportcfg_exit;
10305 	}
10306 	bp->max_tc = resp->max_configurable_queues;
10307 	bp->max_lltc = resp->max_configurable_lossless_queues;
10308 	if (bp->max_tc > BNXT_MAX_QUEUE)
10309 		bp->max_tc = BNXT_MAX_QUEUE;
10310 
10311 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
10312 	qptr = &resp->queue_id0;
10313 	for (i = 0, j = 0; i < bp->max_tc; i++) {
10314 		bp->q_info[j].queue_id = *qptr;
10315 		bp->q_ids[i] = *qptr++;
10316 		bp->q_info[j].queue_profile = *qptr++;
10317 		bp->tc_to_qidx[j] = j;
10318 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
10319 		    (no_rdma && BNXT_PF(bp)))
10320 			j++;
10321 	}
10322 	bp->max_q = bp->max_tc;
10323 	bp->max_tc = max_t(u8, j, 1);
10324 
10325 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
10326 		bp->max_tc = 1;
10327 
10328 	if (bp->max_lltc > bp->max_tc)
10329 		bp->max_lltc = bp->max_tc;
10330 
10331 qportcfg_exit:
10332 	hwrm_req_drop(bp, req);
10333 	return rc;
10334 }
10335 
10336 static int bnxt_hwrm_poll(struct bnxt *bp)
10337 {
10338 	struct hwrm_ver_get_input *req;
10339 	int rc;
10340 
10341 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10342 	if (rc)
10343 		return rc;
10344 
10345 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10346 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10347 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10348 
10349 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
10350 	rc = hwrm_req_send(bp, req);
10351 	return rc;
10352 }
10353 
10354 static int bnxt_hwrm_ver_get(struct bnxt *bp)
10355 {
10356 	struct hwrm_ver_get_output *resp;
10357 	struct hwrm_ver_get_input *req;
10358 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
10359 	u32 dev_caps_cfg, hwrm_ver;
10360 	int rc, len, max_tmo_secs;
10361 
10362 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10363 	if (rc)
10364 		return rc;
10365 
10366 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10367 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
10368 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10369 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10370 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10371 
10372 	resp = hwrm_req_hold(bp, req);
10373 	rc = hwrm_req_send(bp, req);
10374 	if (rc)
10375 		goto hwrm_ver_get_exit;
10376 
10377 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
10378 
10379 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
10380 			     resp->hwrm_intf_min_8b << 8 |
10381 			     resp->hwrm_intf_upd_8b;
10382 	if (resp->hwrm_intf_maj_8b < 1) {
10383 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
10384 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10385 			    resp->hwrm_intf_upd_8b);
10386 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10387 	}
10388 
10389 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10390 			HWRM_VERSION_UPDATE;
10391 
10392 	if (bp->hwrm_spec_code > hwrm_ver)
10393 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10394 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10395 			 HWRM_VERSION_UPDATE);
10396 	else
10397 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10398 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10399 			 resp->hwrm_intf_upd_8b);
10400 
10401 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10402 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10403 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10404 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10405 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10406 		len = FW_VER_STR_LEN;
10407 	} else {
10408 		fw_maj = resp->hwrm_fw_maj_8b;
10409 		fw_min = resp->hwrm_fw_min_8b;
10410 		fw_bld = resp->hwrm_fw_bld_8b;
10411 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10412 		len = BC_HWRM_STR_LEN;
10413 	}
10414 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10415 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10416 		 fw_rsv);
10417 
10418 	if (strlen(resp->active_pkg_name)) {
10419 		int fw_ver_len = strlen(bp->fw_ver_str);
10420 
10421 		snprintf(bp->fw_ver_str + fw_ver_len,
10422 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10423 			 resp->active_pkg_name);
10424 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10425 	}
10426 
10427 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10428 	if (!bp->hwrm_cmd_timeout)
10429 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10430 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10431 	if (!bp->hwrm_cmd_max_timeout)
10432 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10433 	max_tmo_secs = bp->hwrm_cmd_max_timeout / 1000;
10434 #ifdef CONFIG_DETECT_HUNG_TASK
10435 	if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT ||
10436 	    max_tmo_secs > CONFIG_DEFAULT_HUNG_TASK_TIMEOUT) {
10437 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog (kernel default %ds)\n",
10438 			    max_tmo_secs, CONFIG_DEFAULT_HUNG_TASK_TIMEOUT);
10439 	}
10440 #endif
10441 
10442 	if (resp->hwrm_intf_maj_8b >= 1) {
10443 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10444 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10445 	}
10446 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10447 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10448 
10449 	bp->chip_num = le16_to_cpu(resp->chip_num);
10450 	bp->chip_rev = resp->chip_rev;
10451 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10452 	    !resp->chip_metal)
10453 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10454 
10455 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10456 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10457 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10458 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10459 
10460 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10461 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10462 
10463 	if (dev_caps_cfg &
10464 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10465 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10466 
10467 	if (dev_caps_cfg &
10468 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10469 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10470 
10471 	if (dev_caps_cfg &
10472 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10473 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10474 
10475 hwrm_ver_get_exit:
10476 	hwrm_req_drop(bp, req);
10477 	return rc;
10478 }
10479 
10480 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10481 {
10482 	struct hwrm_fw_set_time_input *req;
10483 	struct tm tm;
10484 	time64_t now = ktime_get_real_seconds();
10485 	int rc;
10486 
10487 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10488 	    bp->hwrm_spec_code < 0x10400)
10489 		return -EOPNOTSUPP;
10490 
10491 	time64_to_tm(now, 0, &tm);
10492 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10493 	if (rc)
10494 		return rc;
10495 
10496 	req->year = cpu_to_le16(1900 + tm.tm_year);
10497 	req->month = 1 + tm.tm_mon;
10498 	req->day = tm.tm_mday;
10499 	req->hour = tm.tm_hour;
10500 	req->minute = tm.tm_min;
10501 	req->second = tm.tm_sec;
10502 	return hwrm_req_send(bp, req);
10503 }
10504 
10505 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10506 {
10507 	u64 sw_tmp;
10508 
10509 	hw &= mask;
10510 	sw_tmp = (*sw & ~mask) | hw;
10511 	if (hw < (*sw & mask))
10512 		sw_tmp += mask + 1;
10513 	WRITE_ONCE(*sw, sw_tmp);
10514 }
10515 
10516 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10517 				    int count, bool ignore_zero)
10518 {
10519 	int i;
10520 
10521 	for (i = 0; i < count; i++) {
10522 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10523 
10524 		if (ignore_zero && !hw)
10525 			continue;
10526 
10527 		if (masks[i] == -1ULL)
10528 			sw_stats[i] = hw;
10529 		else
10530 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10531 	}
10532 }
10533 
10534 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10535 {
10536 	if (!stats->hw_stats)
10537 		return;
10538 
10539 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10540 				stats->hw_masks, stats->len / 8, false);
10541 }
10542 
10543 static void bnxt_accumulate_ring_stats(struct bnxt *bp)
10544 {
10545 	struct bnxt_stats_mem *ring0_stats;
10546 	bool ignore_zero = false;
10547 	int i;
10548 
10549 	/* Chip bug.  Counter intermittently becomes 0. */
10550 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10551 		ignore_zero = true;
10552 
10553 	for (i = 0; i < bp->cp_nr_rings; i++) {
10554 		struct bnxt_napi *bnapi = bp->bnapi[i];
10555 		struct bnxt_cp_ring_info *cpr;
10556 		struct bnxt_stats_mem *stats;
10557 
10558 		cpr = &bnapi->cp_ring;
10559 		stats = &cpr->stats;
10560 		if (!i)
10561 			ring0_stats = stats;
10562 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10563 					ring0_stats->hw_masks,
10564 					ring0_stats->len / 8, ignore_zero);
10565 	}
10566 }
10567 
10568 static void bnxt_accumulate_port_stats(struct bnxt *bp)
10569 {
10570 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10571 		struct bnxt_stats_mem *stats = &bp->port_stats;
10572 		__le64 *hw_stats = stats->hw_stats;
10573 		u64 *sw_stats = stats->sw_stats;
10574 		u64 *masks = stats->hw_masks;
10575 		int cnt;
10576 
10577 		cnt = sizeof(struct rx_port_stats) / 8;
10578 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10579 
10580 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10581 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10582 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10583 		cnt = sizeof(struct tx_port_stats) / 8;
10584 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10585 	}
10586 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10587 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10588 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10589 	}
10590 }
10591 
10592 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10593 {
10594 	bnxt_accumulate_ring_stats(bp);
10595 	bnxt_accumulate_port_stats(bp);
10596 }
10597 
10598 /* Re-accumulate ring stats from DMA buffers if stale.
10599  * uAPIs for reading sw_stats should call this first.
10600  *
10601  * We promise user space update frequency of bp->stats_coal_ticks but
10602  * the update is a two step process - first device updates the DMA buffer,
10603  * then we have to update from that buffer to driver stats in the service work.
10604  * Worst case we would be 2x off from the desired frequency.
10605  * Sync the stats sooner, if stale. The 20% threshold was chosen arbitrarily.
10606  *
10607  * Ideally we would split the user-configured time into two portions,
10608  * i.e. also lower the DMA period by the 20%. But the DMA timer seems to have
10609  * too coarse granularity to play such tricks.
10610  */
10611 void bnxt_sync_ring_stats(struct bnxt *bp)
10612 {
10613 	unsigned long stale;
10614 
10615 	if (!netif_running(bp->dev) || !bp->stats_coal_ticks)
10616 		return;
10617 
10618 	spin_lock(&bp->stats_lock);
10619 	stale = usecs_to_jiffies(bp->stats_coal_ticks / 5);
10620 	if (time_after_eq(jiffies, bp->stats_updated_jiffies + stale)) {
10621 		bnxt_accumulate_ring_stats(bp);
10622 		bp->stats_updated_jiffies = jiffies;
10623 	}
10624 	spin_unlock(&bp->stats_lock);
10625 }
10626 
10627 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10628 {
10629 	struct hwrm_port_qstats_input *req;
10630 	struct bnxt_pf_info *pf = &bp->pf;
10631 	int rc;
10632 
10633 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10634 		return 0;
10635 
10636 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10637 		return -EOPNOTSUPP;
10638 
10639 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10640 	if (rc)
10641 		return rc;
10642 
10643 	req->flags = flags;
10644 	req->port_id = cpu_to_le16(pf->port_id);
10645 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10646 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10647 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10648 	return hwrm_req_send(bp, req);
10649 }
10650 
10651 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10652 {
10653 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10654 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10655 	struct hwrm_port_qstats_ext_output *resp_qs;
10656 	struct hwrm_port_qstats_ext_input *req_qs;
10657 	struct bnxt_pf_info *pf = &bp->pf;
10658 	u32 tx_stat_size;
10659 	int rc;
10660 
10661 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10662 		return 0;
10663 
10664 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10665 		return -EOPNOTSUPP;
10666 
10667 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10668 	if (rc)
10669 		return rc;
10670 
10671 	req_qs->flags = flags;
10672 	req_qs->port_id = cpu_to_le16(pf->port_id);
10673 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10674 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10675 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10676 		       sizeof(struct tx_port_stats_ext) : 0;
10677 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10678 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10679 	resp_qs = hwrm_req_hold(bp, req_qs);
10680 	rc = hwrm_req_send(bp, req_qs);
10681 	if (!rc) {
10682 		bp->fw_rx_stats_ext_size =
10683 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10684 		if (BNXT_FW_MAJ(bp) < 220 &&
10685 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10686 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10687 
10688 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10689 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10690 	} else {
10691 		bp->fw_rx_stats_ext_size = 0;
10692 		bp->fw_tx_stats_ext_size = 0;
10693 	}
10694 	hwrm_req_drop(bp, req_qs);
10695 
10696 	if (flags)
10697 		return rc;
10698 
10699 	if (bp->fw_tx_stats_ext_size <=
10700 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10701 		bp->pri2cos_valid = 0;
10702 		return rc;
10703 	}
10704 
10705 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10706 	if (rc)
10707 		return rc;
10708 
10709 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10710 
10711 	resp_qc = hwrm_req_hold(bp, req_qc);
10712 	rc = hwrm_req_send(bp, req_qc);
10713 	if (!rc) {
10714 		u8 *pri2cos;
10715 		int i, j;
10716 
10717 		pri2cos = &resp_qc->pri0_cos_queue_id;
10718 		for (i = 0; i < 8; i++) {
10719 			u8 queue_id = pri2cos[i];
10720 			u8 queue_idx;
10721 
10722 			/* Per port queue IDs start from 0, 10, 20, etc */
10723 			queue_idx = queue_id % 10;
10724 			if (queue_idx > BNXT_MAX_QUEUE) {
10725 				bp->pri2cos_valid = false;
10726 				hwrm_req_drop(bp, req_qc);
10727 				return rc;
10728 			}
10729 			for (j = 0; j < bp->max_q; j++) {
10730 				if (bp->q_ids[j] == queue_id)
10731 					bp->pri2cos_idx[i] = queue_idx;
10732 			}
10733 		}
10734 		bp->pri2cos_valid = true;
10735 	}
10736 	hwrm_req_drop(bp, req_qc);
10737 
10738 	return rc;
10739 }
10740 
10741 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10742 {
10743 	bnxt_hwrm_tunnel_dst_port_free(bp,
10744 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10745 	bnxt_hwrm_tunnel_dst_port_free(bp,
10746 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10747 }
10748 
10749 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10750 {
10751 	int rc, i;
10752 	u32 tpa_flags = 0;
10753 
10754 	if (set_tpa)
10755 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10756 	else if (BNXT_NO_FW_ACCESS(bp))
10757 		return 0;
10758 	for (i = 0; i < bp->nr_vnics; i++) {
10759 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10760 		if (rc) {
10761 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10762 				   i, rc);
10763 			return rc;
10764 		}
10765 	}
10766 	return 0;
10767 }
10768 
10769 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10770 {
10771 	int i;
10772 
10773 	for (i = 0; i < bp->nr_vnics; i++)
10774 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10775 }
10776 
10777 static void bnxt_clear_vnic(struct bnxt *bp)
10778 {
10779 	if (!bp->vnic_info)
10780 		return;
10781 
10782 	bnxt_hwrm_clear_vnic_filter(bp);
10783 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10784 		/* clear all RSS setting before free vnic ctx */
10785 		bnxt_hwrm_clear_vnic_rss(bp);
10786 		bnxt_hwrm_vnic_ctx_free(bp);
10787 	}
10788 	/* before free the vnic, undo the vnic tpa settings */
10789 	if (bp->flags & BNXT_FLAG_TPA)
10790 		bnxt_set_tpa(bp, false);
10791 	bnxt_hwrm_vnic_free(bp);
10792 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10793 		bnxt_hwrm_vnic_ctx_free(bp);
10794 }
10795 
10796 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10797 				    bool irq_re_init)
10798 {
10799 	bnxt_clear_vnic(bp);
10800 	bnxt_hwrm_ring_free(bp, close_path);
10801 	bnxt_hwrm_ring_grp_free(bp);
10802 	if (irq_re_init) {
10803 		bnxt_hwrm_stat_ctx_free(bp);
10804 		bnxt_hwrm_free_tunnel_ports(bp);
10805 	}
10806 }
10807 
10808 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10809 {
10810 	struct hwrm_func_cfg_input *req;
10811 	u8 evb_mode;
10812 	int rc;
10813 
10814 	if (br_mode == BRIDGE_MODE_VEB)
10815 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10816 	else if (br_mode == BRIDGE_MODE_VEPA)
10817 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10818 	else
10819 		return -EINVAL;
10820 
10821 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10822 	if (rc)
10823 		return rc;
10824 
10825 	req->fid = cpu_to_le16(0xffff);
10826 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10827 	req->evb_mode = evb_mode;
10828 	return hwrm_req_send(bp, req);
10829 }
10830 
10831 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10832 {
10833 	struct hwrm_func_cfg_input *req;
10834 	int rc;
10835 
10836 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10837 		return 0;
10838 
10839 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10840 	if (rc)
10841 		return rc;
10842 
10843 	req->fid = cpu_to_le16(0xffff);
10844 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10845 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10846 	if (size == 128)
10847 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10848 
10849 	return hwrm_req_send(bp, req);
10850 }
10851 
10852 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10853 {
10854 	int rc;
10855 
10856 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10857 		goto skip_rss_ctx;
10858 
10859 	/* allocate context for vnic */
10860 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10861 	if (rc) {
10862 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10863 			   vnic->vnic_id, rc);
10864 		goto vnic_setup_err;
10865 	}
10866 	bp->rsscos_nr_ctxs++;
10867 
10868 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10869 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10870 		if (rc) {
10871 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10872 				   vnic->vnic_id, rc);
10873 			goto vnic_setup_err;
10874 		}
10875 		bp->rsscos_nr_ctxs++;
10876 	}
10877 
10878 skip_rss_ctx:
10879 	bnxt_fill_hw_rss_tbl(bp, vnic);
10880 	/* configure default vnic, ring grp */
10881 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10882 	if (rc) {
10883 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10884 			   vnic->vnic_id, rc);
10885 		goto vnic_setup_err;
10886 	}
10887 
10888 	/* Enable RSS hashing on vnic */
10889 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10890 	if (rc) {
10891 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10892 			   vnic->vnic_id, rc);
10893 		goto vnic_setup_err;
10894 	}
10895 
10896 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10897 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10898 		if (rc) {
10899 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10900 				   vnic->vnic_id, rc);
10901 		}
10902 	}
10903 
10904 vnic_setup_err:
10905 	return rc;
10906 }
10907 
10908 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10909 			  u8 valid)
10910 {
10911 	struct hwrm_vnic_update_input *req;
10912 	int rc;
10913 
10914 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10915 	if (rc)
10916 		return rc;
10917 
10918 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10919 
10920 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10921 		req->mru = cpu_to_le16(vnic->mru);
10922 
10923 	req->enables = cpu_to_le32(valid);
10924 
10925 	return hwrm_req_send(bp, req);
10926 }
10927 
10928 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10929 {
10930 	int rc;
10931 
10932 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10933 	if (rc) {
10934 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10935 			   vnic->vnic_id, rc);
10936 		return rc;
10937 	}
10938 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10939 	if (rc)
10940 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10941 			   vnic->vnic_id, rc);
10942 	return rc;
10943 }
10944 
10945 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10946 {
10947 	int rc, i, nr_ctxs;
10948 
10949 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10950 	for (i = 0; i < nr_ctxs; i++) {
10951 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10952 		if (rc) {
10953 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10954 				   vnic->vnic_id, i, rc);
10955 			break;
10956 		}
10957 		bp->rsscos_nr_ctxs++;
10958 	}
10959 	if (i < nr_ctxs)
10960 		return -ENOMEM;
10961 
10962 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10963 	if (rc)
10964 		return rc;
10965 
10966 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10967 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10968 		if (rc) {
10969 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10970 				   vnic->vnic_id, rc);
10971 		}
10972 	}
10973 	return rc;
10974 }
10975 
10976 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10977 {
10978 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10979 		return __bnxt_setup_vnic_p5(bp, vnic);
10980 	else
10981 		return __bnxt_setup_vnic(bp, vnic);
10982 }
10983 
10984 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10985 				     struct bnxt_vnic_info *vnic,
10986 				     u16 start_rx_ring_idx, int rx_rings)
10987 {
10988 	int rc;
10989 
10990 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10991 	if (rc) {
10992 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10993 			   vnic->vnic_id, rc);
10994 		return rc;
10995 	}
10996 	return bnxt_setup_vnic(bp, vnic);
10997 }
10998 
10999 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
11000 {
11001 	struct bnxt_vnic_info *vnic;
11002 	int i, rc = 0;
11003 
11004 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
11005 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
11006 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
11007 	}
11008 
11009 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11010 		return 0;
11011 
11012 	for (i = 0; i < bp->rx_nr_rings; i++) {
11013 		u16 vnic_id = i + 1;
11014 		u16 ring_id = i;
11015 
11016 		if (vnic_id >= bp->nr_vnics)
11017 			break;
11018 
11019 		vnic = &bp->vnic_info[vnic_id];
11020 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
11021 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
11022 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
11023 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
11024 			break;
11025 	}
11026 	return rc;
11027 }
11028 
11029 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
11030 			  bool all)
11031 {
11032 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
11033 	struct bnxt_filter_base *usr_fltr, *tmp;
11034 	struct bnxt_ntuple_filter *ntp_fltr;
11035 	int i;
11036 
11037 	bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
11038 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
11039 		if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
11040 			bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
11041 	}
11042 	if (!all)
11043 		return;
11044 
11045 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
11046 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
11047 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
11048 			ntp_fltr = container_of(usr_fltr,
11049 						struct bnxt_ntuple_filter,
11050 						base);
11051 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
11052 			bnxt_del_ntp_filter(bp, ntp_fltr);
11053 			bnxt_del_one_usr_fltr(bp, usr_fltr);
11054 		}
11055 	}
11056 
11057 	if (vnic->rss_table)
11058 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
11059 				  vnic->rss_table,
11060 				  vnic->rss_table_dma_addr);
11061 	bp->num_rss_ctx--;
11062 }
11063 
11064 static bool bnxt_vnic_has_rx_ring(struct bnxt *bp, struct bnxt_vnic_info *vnic,
11065 				  int rxr_id)
11066 {
11067 	u16 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
11068 	int i, vnic_rx;
11069 
11070 	/* Ntuple VNIC always has all the rx rings. Any change of ring id
11071 	 * must be updated because a future filter may use it.
11072 	 */
11073 	if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
11074 		return true;
11075 
11076 	for (i = 0; i < tbl_size; i++) {
11077 		if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
11078 			vnic_rx = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
11079 		else
11080 			vnic_rx = bp->rss_indir_tbl[i];
11081 
11082 		if (rxr_id == vnic_rx)
11083 			return true;
11084 	}
11085 
11086 	return false;
11087 }
11088 
11089 static int bnxt_set_vnic_mru_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic,
11090 				u16 mru, int rxr_id)
11091 {
11092 	int rc;
11093 
11094 	if (!bnxt_vnic_has_rx_ring(bp, vnic, rxr_id))
11095 		return 0;
11096 
11097 	if (mru) {
11098 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
11099 		if (rc) {
11100 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
11101 				   vnic->vnic_id, rc);
11102 			return rc;
11103 		}
11104 		if (rxr_id == vnic->default_rx_ring) {
11105 			rc = bnxt_hwrm_vnic_cfg(bp, vnic);
11106 			if (rc)
11107 				return rc;
11108 		}
11109 	}
11110 	vnic->mru = mru;
11111 	bnxt_hwrm_vnic_update(bp, vnic,
11112 			      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
11113 
11114 	return 0;
11115 }
11116 
11117 static int bnxt_set_rss_ctx_vnic_mru(struct bnxt *bp, u16 mru, int rxr_id)
11118 {
11119 	struct ethtool_rxfh_context *ctx;
11120 	unsigned long context;
11121 	int rc;
11122 
11123 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11124 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11125 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
11126 
11127 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, rxr_id);
11128 		if (rc)
11129 			return rc;
11130 	}
11131 
11132 	return 0;
11133 }
11134 
11135 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
11136 {
11137 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
11138 	struct ethtool_rxfh_context *ctx;
11139 	unsigned long context;
11140 
11141 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11142 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11143 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
11144 
11145 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
11146 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
11147 		    __bnxt_setup_vnic_p5(bp, vnic)) {
11148 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
11149 				   rss_ctx->index);
11150 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
11151 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
11152 		}
11153 	}
11154 }
11155 
11156 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
11157 {
11158 	struct ethtool_rxfh_context *ctx;
11159 	unsigned long context;
11160 
11161 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11162 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11163 
11164 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
11165 	}
11166 }
11167 
11168 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
11169 static bool bnxt_promisc_ok(struct bnxt *bp)
11170 {
11171 #ifdef CONFIG_BNXT_SRIOV
11172 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
11173 		return false;
11174 #endif
11175 	return true;
11176 }
11177 
11178 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
11179 {
11180 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
11181 	unsigned int rc = 0;
11182 
11183 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
11184 	if (rc) {
11185 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
11186 			   rc);
11187 		return rc;
11188 	}
11189 
11190 	/* Setup the proper default RX ring */
11191 	bnxt_fill_hw_rss_tbl(bp, vnic);
11192 
11193 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
11194 	if (rc) {
11195 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
11196 			   rc);
11197 		return rc;
11198 	}
11199 	return rc;
11200 }
11201 
11202 static int bnxt_cfg_rx_mode(struct bnxt *, struct netdev_hw_addr_list *, bool);
11203 static bool bnxt_mc_list_updated(struct bnxt *, u32 *,
11204 				 const struct netdev_hw_addr_list *);
11205 
11206 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
11207 {
11208 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
11209 	int rc = 0;
11210 	unsigned int rx_nr_rings = bp->rx_nr_rings;
11211 
11212 	if (irq_re_init) {
11213 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
11214 		if (rc) {
11215 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
11216 				   rc);
11217 			goto err_out;
11218 		}
11219 	}
11220 
11221 	rc = bnxt_hwrm_ring_alloc(bp);
11222 	if (rc) {
11223 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
11224 		goto err_out;
11225 	}
11226 
11227 	rc = bnxt_hwrm_ring_grp_alloc(bp);
11228 	if (rc) {
11229 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
11230 		goto err_out;
11231 	}
11232 
11233 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11234 		rx_nr_rings--;
11235 
11236 	/* default vnic 0 */
11237 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
11238 	if (rc) {
11239 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
11240 		goto err_out;
11241 	}
11242 
11243 	if (BNXT_VF(bp))
11244 		bnxt_hwrm_func_qcfg(bp);
11245 
11246 	rc = bnxt_setup_vnic(bp, vnic);
11247 	if (rc)
11248 		goto err_out;
11249 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
11250 		bnxt_hwrm_update_rss_hash_cfg(bp);
11251 
11252 	if (bp->flags & BNXT_FLAG_RFS) {
11253 		rc = bnxt_alloc_rfs_vnics(bp);
11254 		if (rc)
11255 			goto err_out;
11256 	}
11257 
11258 	if (bp->flags & BNXT_FLAG_TPA) {
11259 		rc = bnxt_set_tpa(bp, true);
11260 		if (rc)
11261 			goto err_out;
11262 	}
11263 
11264 	if (BNXT_VF(bp))
11265 		bnxt_update_vf_mac(bp);
11266 
11267 	/* Filter for default vnic 0 */
11268 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
11269 	if (rc) {
11270 		if (BNXT_VF(bp) && rc == -ENODEV)
11271 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
11272 		else
11273 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
11274 		goto err_out;
11275 	}
11276 	vnic->uc_filter_count = 1;
11277 
11278 	vnic->rx_mask = 0;
11279 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
11280 		goto skip_rx_mask;
11281 
11282 	if (bp->dev->flags & IFF_BROADCAST)
11283 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
11284 
11285 	if (bp->dev->flags & IFF_PROMISC)
11286 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11287 
11288 	if (bp->dev->flags & IFF_ALLMULTI) {
11289 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
11290 		vnic->mc_list_count = 0;
11291 	} else if (bp->dev->flags & IFF_MULTICAST) {
11292 		u32 mask = 0;
11293 
11294 		bnxt_mc_list_updated(bp, &mask, &bp->dev->mc);
11295 		vnic->rx_mask |= mask;
11296 	}
11297 
11298 	rc = bnxt_cfg_rx_mode(bp, &bp->dev->uc, true);
11299 	if (rc == -EAGAIN) {
11300 		netif_rx_mode_schedule_retry(bp->dev);
11301 		rc = 0;
11302 	} else if (rc) {
11303 		goto err_out;
11304 	}
11305 
11306 skip_rx_mask:
11307 	rc = bnxt_hwrm_set_coal(bp);
11308 	if (rc)
11309 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
11310 				rc);
11311 
11312 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11313 		rc = bnxt_setup_nitroa0_vnic(bp);
11314 		if (rc)
11315 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
11316 				   rc);
11317 	}
11318 
11319 	if (BNXT_VF(bp)) {
11320 		bnxt_hwrm_func_qcfg(bp);
11321 		netdev_update_features(bp->dev);
11322 	}
11323 
11324 	return 0;
11325 
11326 err_out:
11327 	bnxt_hwrm_resource_free(bp, 0, true);
11328 
11329 	return rc;
11330 }
11331 
11332 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
11333 {
11334 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
11335 	return 0;
11336 }
11337 
11338 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
11339 {
11340 	bnxt_init_cp_rings(bp);
11341 	bnxt_init_rx_rings(bp);
11342 	bnxt_init_tx_rings(bp);
11343 	bnxt_init_ring_grps(bp, irq_re_init);
11344 	bnxt_init_vnics(bp);
11345 
11346 	return bnxt_init_chip(bp, irq_re_init);
11347 }
11348 
11349 static int bnxt_set_real_num_queues(struct bnxt *bp)
11350 {
11351 	int rc;
11352 	struct net_device *dev = bp->dev;
11353 
11354 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
11355 					  bp->tx_nr_rings_xdp);
11356 	if (rc)
11357 		return rc;
11358 
11359 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
11360 	if (rc)
11361 		return rc;
11362 
11363 #ifdef CONFIG_RFS_ACCEL
11364 	if (bp->flags & BNXT_FLAG_RFS)
11365 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
11366 #endif
11367 
11368 	return rc;
11369 }
11370 
11371 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11372 			     bool shared)
11373 {
11374 	int _rx = *rx, _tx = *tx;
11375 
11376 	if (shared) {
11377 		*rx = min_t(int, _rx, max);
11378 		*tx = min_t(int, _tx, max);
11379 	} else {
11380 		if (max < 2)
11381 			return -ENOMEM;
11382 
11383 		while (_rx + _tx > max) {
11384 			if (_rx > _tx && _rx > 1)
11385 				_rx--;
11386 			else if (_tx > 1)
11387 				_tx--;
11388 		}
11389 		*rx = _rx;
11390 		*tx = _tx;
11391 	}
11392 	return 0;
11393 }
11394 
11395 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
11396 {
11397 	return (tx - tx_xdp) / tx_sets + tx_xdp;
11398 }
11399 
11400 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
11401 {
11402 	int tcs = bp->num_tc;
11403 
11404 	if (!tcs)
11405 		tcs = 1;
11406 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
11407 }
11408 
11409 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
11410 {
11411 	int tcs = bp->num_tc;
11412 
11413 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
11414 	       bp->tx_nr_rings_xdp;
11415 }
11416 
11417 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11418 			   bool sh)
11419 {
11420 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
11421 
11422 	if (tx_cp != *tx) {
11423 		int tx_saved = tx_cp, rc;
11424 
11425 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
11426 		if (rc)
11427 			return rc;
11428 		if (tx_cp != tx_saved)
11429 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
11430 		return 0;
11431 	}
11432 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
11433 }
11434 
11435 static void bnxt_setup_msix(struct bnxt *bp)
11436 {
11437 	const int len = sizeof(bp->irq_tbl[0].name);
11438 	struct net_device *dev = bp->dev;
11439 	int tcs, i;
11440 
11441 	tcs = bp->num_tc;
11442 	if (tcs) {
11443 		int i, off, count;
11444 
11445 		for (i = 0; i < tcs; i++) {
11446 			count = bp->tx_nr_rings_per_tc;
11447 			off = BNXT_TC_TO_RING_BASE(bp, i);
11448 			netdev_set_tc_queue(dev, i, count, off);
11449 		}
11450 	}
11451 
11452 	for (i = 0; i < bp->cp_nr_rings; i++) {
11453 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11454 		char *attr;
11455 
11456 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11457 			attr = "TxRx";
11458 		else if (i < bp->rx_nr_rings)
11459 			attr = "rx";
11460 		else
11461 			attr = "tx";
11462 
11463 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
11464 			 attr, i);
11465 		bp->irq_tbl[map_idx].handler = bnxt_msix;
11466 	}
11467 }
11468 
11469 static int bnxt_init_int_mode(struct bnxt *bp);
11470 
11471 static int bnxt_change_msix(struct bnxt *bp, int total)
11472 {
11473 	struct msi_map map;
11474 	int i;
11475 
11476 	/* add MSIX to the end if needed */
11477 	for (i = bp->total_irqs; i < total; i++) {
11478 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
11479 		if (map.index < 0)
11480 			return bp->total_irqs;
11481 		bp->irq_tbl[i].vector = map.virq;
11482 		bp->total_irqs++;
11483 	}
11484 
11485 	/* trim MSIX from the end if needed */
11486 	for (i = bp->total_irqs; i > total; i--) {
11487 		map.index = i - 1;
11488 		map.virq = bp->irq_tbl[i - 1].vector;
11489 		pci_msix_free_irq(bp->pdev, map);
11490 		bp->total_irqs--;
11491 	}
11492 	return bp->total_irqs;
11493 }
11494 
11495 static int bnxt_setup_int_mode(struct bnxt *bp)
11496 {
11497 	int rc;
11498 
11499 	if (!bp->irq_tbl) {
11500 		rc = bnxt_init_int_mode(bp);
11501 		if (rc || !bp->irq_tbl)
11502 			return rc ?: -ENODEV;
11503 	}
11504 
11505 	bnxt_setup_msix(bp);
11506 
11507 	rc = bnxt_set_real_num_queues(bp);
11508 	return rc;
11509 }
11510 
11511 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
11512 {
11513 	return bp->hw_resc.max_rsscos_ctxs;
11514 }
11515 
11516 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
11517 {
11518 	return bp->hw_resc.max_vnics;
11519 }
11520 
11521 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11522 {
11523 	return bp->hw_resc.max_stat_ctxs;
11524 }
11525 
11526 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11527 {
11528 	return bp->hw_resc.max_cp_rings;
11529 }
11530 
11531 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11532 {
11533 	unsigned int cp = bp->hw_resc.max_cp_rings;
11534 
11535 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11536 		cp -= bnxt_get_ulp_msix_num(bp);
11537 
11538 	return cp;
11539 }
11540 
11541 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11542 {
11543 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11544 
11545 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11546 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11547 
11548 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11549 }
11550 
11551 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11552 {
11553 	bp->hw_resc.max_irqs = max_irqs;
11554 }
11555 
11556 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11557 {
11558 	unsigned int cp;
11559 
11560 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11561 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11562 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11563 	else
11564 		return cp - bp->cp_nr_rings;
11565 }
11566 
11567 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11568 {
11569 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11570 }
11571 
11572 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11573 {
11574 	int max_irq = bnxt_get_max_func_irqs(bp);
11575 	int total_req = bp->cp_nr_rings + num;
11576 
11577 	if (max_irq < total_req) {
11578 		num = max_irq - bp->cp_nr_rings;
11579 		if (num <= 0)
11580 			return 0;
11581 	}
11582 	return num;
11583 }
11584 
11585 static int bnxt_get_num_msix(struct bnxt *bp)
11586 {
11587 	if (!BNXT_NEW_RM(bp))
11588 		return bnxt_get_max_func_irqs(bp);
11589 
11590 	return bnxt_nq_rings_in_use(bp);
11591 }
11592 
11593 static int bnxt_init_int_mode(struct bnxt *bp)
11594 {
11595 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
11596 
11597 	total_vecs = bnxt_get_num_msix(bp);
11598 	max = bnxt_get_max_func_irqs(bp);
11599 	if (total_vecs > max)
11600 		total_vecs = max;
11601 
11602 	if (!total_vecs)
11603 		return 0;
11604 
11605 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11606 		min = 2;
11607 
11608 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11609 					   PCI_IRQ_MSIX);
11610 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11611 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11612 		rc = -ENODEV;
11613 		goto msix_setup_exit;
11614 	}
11615 
11616 	tbl_size = total_vecs;
11617 	if (pci_msix_can_alloc_dyn(bp->pdev))
11618 		tbl_size = max;
11619 	bp->irq_tbl = kzalloc_objs(*bp->irq_tbl, tbl_size);
11620 	if (bp->irq_tbl) {
11621 		for (i = 0; i < total_vecs; i++)
11622 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11623 
11624 		bp->total_irqs = total_vecs;
11625 		/* Trim rings based upon num of vectors allocated */
11626 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11627 				     total_vecs - ulp_msix, min == 1);
11628 		if (rc)
11629 			goto msix_setup_exit;
11630 
11631 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11632 		bp->cp_nr_rings = (min == 1) ?
11633 				  max_t(int, tx_cp, bp->rx_nr_rings) :
11634 				  tx_cp + bp->rx_nr_rings;
11635 
11636 	} else {
11637 		rc = -ENOMEM;
11638 		goto msix_setup_exit;
11639 	}
11640 	return 0;
11641 
11642 msix_setup_exit:
11643 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11644 	kfree(bp->irq_tbl);
11645 	bp->irq_tbl = NULL;
11646 	pci_free_irq_vectors(bp->pdev);
11647 	return rc;
11648 }
11649 
11650 static void bnxt_clear_int_mode(struct bnxt *bp)
11651 {
11652 	pci_free_irq_vectors(bp->pdev);
11653 
11654 	kfree(bp->irq_tbl);
11655 	bp->irq_tbl = NULL;
11656 }
11657 
11658 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11659 {
11660 	struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA];
11661 	bool irq_cleared = false;
11662 	bool irq_change = false;
11663 	int tcs = bp->num_tc;
11664 	int irqs_required;
11665 	int rc;
11666 
11667 	if (!bnxt_need_reserve_rings(bp))
11668 		return 0;
11669 
11670 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(edev)) {
11671 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11672 
11673 		if (ulp_msix > bp->ulp_num_msix_want)
11674 			ulp_msix = bp->ulp_num_msix_want;
11675 		irqs_required = ulp_msix + bp->cp_nr_rings;
11676 	} else {
11677 		irqs_required = bnxt_get_num_msix(bp);
11678 	}
11679 
11680 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11681 		irq_change = true;
11682 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11683 			bnxt_ulp_irq_stop(bp);
11684 			bnxt_clear_int_mode(bp);
11685 			irq_cleared = true;
11686 		}
11687 	}
11688 	rc = __bnxt_reserve_rings(bp);
11689 	if (irq_cleared) {
11690 		if (!rc)
11691 			rc = bnxt_init_int_mode(bp);
11692 		bnxt_ulp_irq_restart(bp, rc);
11693 	} else if (irq_change && !rc) {
11694 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11695 			rc = -ENOSPC;
11696 	}
11697 	if (rc) {
11698 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11699 		return rc;
11700 	}
11701 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11702 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11703 		netdev_err(bp->dev, "tx ring reservation failure\n");
11704 		netdev_reset_tc(bp->dev);
11705 		bp->num_tc = 0;
11706 		if (bp->tx_nr_rings_xdp)
11707 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11708 		else
11709 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11710 		return -ENOMEM;
11711 	}
11712 	return 0;
11713 }
11714 
11715 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx)
11716 {
11717 	struct bnxt_tx_ring_info *txr;
11718 	struct netdev_queue *txq;
11719 	struct bnxt_napi *bnapi;
11720 	int i;
11721 
11722 	bnapi = bp->bnapi[idx];
11723 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11724 		WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11725 		synchronize_net();
11726 
11727 		if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) {
11728 			txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11729 			if (txq) {
11730 				__netif_tx_lock_bh(txq);
11731 				netif_tx_stop_queue(txq);
11732 				__netif_tx_unlock_bh(txq);
11733 			}
11734 		}
11735 
11736 		if (!bp->tph_mode)
11737 			continue;
11738 
11739 		bnxt_hwrm_tx_ring_free(bp, txr, true);
11740 		bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr);
11741 		bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index);
11742 		bnxt_clear_one_cp_ring(bp, txr->tx_cpr);
11743 	}
11744 }
11745 
11746 static int bnxt_tx_queue_start(struct bnxt *bp, int idx)
11747 {
11748 	struct bnxt_tx_ring_info *txr;
11749 	struct netdev_queue *txq;
11750 	struct bnxt_napi *bnapi;
11751 	int rc, i;
11752 
11753 	bnapi = bp->bnapi[idx];
11754 	/* All rings have been reserved and previously allocated.
11755 	 * Reallocating with the same parameters should never fail.
11756 	 */
11757 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11758 		if (!bp->tph_mode)
11759 			goto start_tx;
11760 
11761 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
11762 		if (rc)
11763 			return rc;
11764 
11765 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false);
11766 		if (rc)
11767 			return rc;
11768 
11769 		txr->tx_prod = 0;
11770 		txr->tx_cons = 0;
11771 		txr->tx_hw_cons = 0;
11772 start_tx:
11773 		WRITE_ONCE(txr->dev_state, 0);
11774 		synchronize_net();
11775 
11776 		if (bnapi->flags & BNXT_NAPI_FLAG_XDP)
11777 			continue;
11778 
11779 		txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11780 		if (txq)
11781 			netif_tx_start_queue(txq);
11782 	}
11783 
11784 	return 0;
11785 }
11786 
11787 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify,
11788 				     const cpumask_t *mask)
11789 {
11790 	struct bnxt_irq *irq;
11791 	u16 tag;
11792 
11793 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11794 
11795 #ifdef CONFIG_RFS_ACCEL
11796 	if (irq->bp->dev->rx_cpu_rmap && irq->ring_nr < irq->bp->rx_nr_rings) {
11797 		int err;
11798 
11799 		err = cpu_rmap_update(irq->bp->dev->rx_cpu_rmap, irq->ring_nr,
11800 				      mask);
11801 		if (err)
11802 			netdev_warn(irq->bp->dev,
11803 				    "aRFS rmap update failed: %d\n", err);
11804 	}
11805 #endif
11806 
11807 	if (!irq->bp->tph_mode)
11808 		return;
11809 
11810 	cpumask_copy(irq->cpu_mask, mask);
11811 
11812 	if (irq->ring_nr >= irq->bp->rx_nr_rings)
11813 		return;
11814 
11815 	if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11816 				cpumask_first(mask), &tag))
11817 		return;
11818 
11819 	WRITE_ONCE(irq->new_tag, tag);
11820 	bnxt_queue_sp_work(irq->bp, BNXT_TPH_UPDATE_SP_EVENT);
11821 }
11822 
11823 static void bnxt_irq_affinity_release(struct kref *ref)
11824 {
11825 	struct irq_affinity_notify *notify =
11826 		container_of(ref, struct irq_affinity_notify, kref);
11827 	struct bnxt_irq *irq;
11828 
11829 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11830 
11831 	if (!irq->bp->tph_mode)
11832 		return;
11833 
11834 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) {
11835 		netdev_err(irq->bp->dev,
11836 			   "Setting ST=0 for MSIX entry %d failed\n",
11837 			   irq->msix_nr);
11838 		return;
11839 	}
11840 }
11841 
11842 static void bnxt_release_irq_notifier(struct bnxt_irq *irq)
11843 {
11844 	irq_set_affinity_notifier(irq->vector, NULL);
11845 }
11846 
11847 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq)
11848 {
11849 	struct irq_affinity_notify *notify;
11850 
11851 	irq->bp = bp;
11852 
11853 	/* Nothing to do if TPH is not enabled */
11854 	if (!bp->tph_mode)
11855 		return;
11856 
11857 	/* Register IRQ affinity notifier */
11858 	notify = &irq->affinity_notify;
11859 	notify->irq = irq->vector;
11860 	notify->notify = bnxt_irq_affinity_notify;
11861 	notify->release = bnxt_irq_affinity_release;
11862 
11863 	irq_set_affinity_notifier(irq->vector, notify);
11864 }
11865 
11866 static void bnxt_free_irq(struct bnxt *bp)
11867 {
11868 	struct bnxt_irq *irq;
11869 	int i;
11870 
11871 	if (!bp->irq_tbl || !bp->bnapi)
11872 		return;
11873 
11874 	for (i = 0; i < bp->cp_nr_rings; i++) {
11875 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11876 
11877 		irq = &bp->irq_tbl[map_idx];
11878 		if (irq->requested) {
11879 			bnxt_release_irq_notifier(irq);
11880 
11881 			if (irq->have_cpumask) {
11882 				irq_update_affinity_hint(irq->vector, NULL);
11883 				free_cpumask_var(irq->cpu_mask);
11884 				irq->have_cpumask = 0;
11885 			}
11886 
11887 			free_irq(irq->vector, bp->bnapi[i]);
11888 		}
11889 
11890 		irq->requested = 0;
11891 		irq->tag = 0;
11892 		irq->new_tag = 0;
11893 	}
11894 
11895 	/* Disable TPH support */
11896 	pcie_disable_tph(bp->pdev);
11897 	bp->tph_mode = 0;
11898 
11899 #ifdef CONFIG_RFS_ACCEL
11900 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11901 	bp->dev->rx_cpu_rmap = NULL;
11902 #endif
11903 }
11904 
11905 static int bnxt_request_irq(struct bnxt *bp)
11906 {
11907 	const int numa_node = dev_to_node(&bp->pdev->dev);
11908 	struct cpu_rmap *rmap = NULL;
11909 	int i, j, rc = 0;
11910 	unsigned long flags = 0;
11911 
11912 	rc = bnxt_setup_int_mode(bp);
11913 	if (rc) {
11914 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11915 			   rc);
11916 		return rc;
11917 	}
11918 #ifdef CONFIG_RFS_ACCEL
11919 	rmap = bp->dev->rx_cpu_rmap;
11920 #endif
11921 
11922 	/* Enable TPH support as part of IRQ request */
11923 	rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE);
11924 	if (!rc)
11925 		bp->tph_mode = PCI_TPH_ST_IV_MODE;
11926 
11927 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11928 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11929 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11930 		unsigned int cpu_num;
11931 		u16 tag;
11932 
11933 		if (IS_ENABLED(CONFIG_RFS_ACCEL) &&
11934 		    rmap && bp->bnapi[i]->rx_ring) {
11935 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11936 			if (rc)
11937 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11938 					    j);
11939 			j++;
11940 		}
11941 
11942 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11943 				 bp->bnapi[i]);
11944 		if (rc)
11945 			break;
11946 
11947 		netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector);
11948 		irq->requested = 1;
11949 
11950 		if (!zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL))
11951 			continue;
11952 
11953 		irq->have_cpumask = 1;
11954 		irq->msix_nr = map_idx;
11955 		irq->ring_nr = i;
11956 		cpu_num = cpumask_local_spread(i, numa_node);
11957 		cpumask_set_cpu(cpu_num, irq->cpu_mask);
11958 
11959 		/* Init ST table entry if we can get the mapping */
11960 		if (!pcie_tph_get_cpu_st(bp->pdev, TPH_MEM_TYPE_VM,
11961 					 cpu_num, &tag)) {
11962 			pcie_tph_set_st_entry(bp->pdev, irq->msix_nr, tag);
11963 			irq->tag = tag;
11964 			irq->new_tag = tag;
11965 		}
11966 
11967 		bnxt_register_irq_notifier(bp, irq);
11968 
11969 		rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
11970 		if (rc) {
11971 			netdev_warn(bp->dev,
11972 				    "Update affinity hint failed, IRQ = %d\n",
11973 				    irq->vector);
11974 			break;
11975 		}
11976 	}
11977 	return rc;
11978 }
11979 
11980 static void bnxt_del_napi(struct bnxt *bp)
11981 {
11982 	int i;
11983 
11984 	if (!bp->bnapi)
11985 		return;
11986 
11987 	for (i = 0; i < bp->rx_nr_rings; i++)
11988 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
11989 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
11990 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
11991 
11992 	for (i = 0; i < bp->cp_nr_rings; i++) {
11993 		struct bnxt_napi *bnapi = bp->bnapi[i];
11994 
11995 		__netif_napi_del_locked(&bnapi->napi);
11996 	}
11997 	/* We called __netif_napi_del_locked(), we need
11998 	 * to respect an RCU grace period before freeing napi structures.
11999 	 */
12000 	synchronize_net();
12001 }
12002 
12003 static void bnxt_init_napi(struct bnxt *bp)
12004 {
12005 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
12006 	unsigned int cp_nr_rings = bp->cp_nr_rings;
12007 	struct bnxt_napi *bnapi;
12008 	int i;
12009 
12010 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
12011 		poll_fn = bnxt_poll_p5;
12012 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
12013 		cp_nr_rings--;
12014 
12015 	set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
12016 
12017 	for (i = 0; i < cp_nr_rings; i++) {
12018 		bnapi = bp->bnapi[i];
12019 		netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn,
12020 					     bnapi->index);
12021 	}
12022 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
12023 		bnapi = bp->bnapi[cp_nr_rings];
12024 		netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
12025 	}
12026 }
12027 
12028 static void bnxt_disable_napi(struct bnxt *bp)
12029 {
12030 	int i;
12031 
12032 	if (!bp->bnapi ||
12033 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
12034 		return;
12035 
12036 	for (i = 0; i < bp->cp_nr_rings; i++) {
12037 		struct bnxt_napi *bnapi = bp->bnapi[i];
12038 		struct bnxt_cp_ring_info *cpr;
12039 
12040 		cpr = &bnapi->cp_ring;
12041 		if (bnapi->tx_fault)
12042 			cpr->sw_stats->tx.tx_resets++;
12043 		if (bnapi->in_reset)
12044 			cpr->sw_stats->rx.rx_resets++;
12045 		napi_disable_locked(&bnapi->napi);
12046 	}
12047 }
12048 
12049 static void bnxt_enable_napi(struct bnxt *bp)
12050 {
12051 	int i;
12052 
12053 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
12054 	for (i = 0; i < bp->cp_nr_rings; i++) {
12055 		struct bnxt_napi *bnapi = bp->bnapi[i];
12056 		struct bnxt_cp_ring_info *cpr;
12057 
12058 		bnapi->tx_fault = 0;
12059 
12060 		cpr = &bnapi->cp_ring;
12061 		bnapi->in_reset = false;
12062 
12063 		if (bnapi->rx_ring) {
12064 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
12065 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
12066 		}
12067 		napi_enable_locked(&bnapi->napi);
12068 	}
12069 }
12070 
12071 void bnxt_tx_disable(struct bnxt *bp)
12072 {
12073 	int i;
12074 	struct bnxt_tx_ring_info *txr;
12075 
12076 	if (bp->tx_ring) {
12077 		for (i = 0; i < bp->tx_nr_rings; i++) {
12078 			txr = &bp->tx_ring[i];
12079 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
12080 		}
12081 	}
12082 	/* Make sure napi polls see @dev_state change */
12083 	synchronize_net();
12084 	/* Drop carrier first to prevent TX timeout */
12085 	netif_carrier_off(bp->dev);
12086 	/* Stop all TX queues */
12087 	netif_tx_disable(bp->dev);
12088 }
12089 
12090 void bnxt_tx_enable(struct bnxt *bp)
12091 {
12092 	int i;
12093 	struct bnxt_tx_ring_info *txr;
12094 
12095 	for (i = 0; i < bp->tx_nr_rings; i++) {
12096 		txr = &bp->tx_ring[i];
12097 		WRITE_ONCE(txr->dev_state, 0);
12098 	}
12099 	/* Make sure napi polls see @dev_state change */
12100 	synchronize_net();
12101 	netif_tx_wake_all_queues(bp->dev);
12102 	if (BNXT_LINK_IS_UP(bp))
12103 		netif_carrier_on(bp->dev);
12104 }
12105 
12106 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
12107 {
12108 	u8 active_fec = link_info->active_fec_sig_mode &
12109 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
12110 
12111 	switch (active_fec) {
12112 	default:
12113 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
12114 		return "None";
12115 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
12116 		return "Clause 74 BaseR";
12117 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
12118 		return "Clause 91 RS(528,514)";
12119 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
12120 		return "Clause 91 RS544_1XN";
12121 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
12122 		return "Clause 91 RS(544,514)";
12123 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
12124 		return "Clause 91 RS272_1XN";
12125 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
12126 		return "Clause 91 RS(272,257)";
12127 	}
12128 }
12129 
12130 static char *bnxt_link_down_reason(struct bnxt_link_info *link_info)
12131 {
12132 	u8 reason = link_info->link_down_reason;
12133 
12134 	/* Multiple bits can be set, we report 1 bit only in order of
12135 	 * priority.
12136 	 */
12137 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_RF)
12138 		return "(Remote fault)";
12139 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_OTP_SPEED_VIOLATION)
12140 		return "(OTP Speed limit violation)";
12141 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_CABLE_REMOVED)
12142 		return "(Cable removed)";
12143 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_MODULE_FAULT)
12144 		return "(Module fault)";
12145 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_BMC_REQUEST)
12146 		return "(BMC request down)";
12147 	return "";
12148 }
12149 
12150 void bnxt_report_link(struct bnxt *bp)
12151 {
12152 	if (BNXT_LINK_IS_UP(bp)) {
12153 		const char *signal = "";
12154 		const char *flow_ctrl;
12155 		const char *duplex;
12156 		u32 speed;
12157 		u16 fec;
12158 
12159 		netif_carrier_on(bp->dev);
12160 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
12161 		if (speed == SPEED_UNKNOWN) {
12162 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
12163 			return;
12164 		}
12165 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
12166 			duplex = "full";
12167 		else
12168 			duplex = "half";
12169 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
12170 			flow_ctrl = "ON - receive & transmit";
12171 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
12172 			flow_ctrl = "ON - transmit";
12173 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
12174 			flow_ctrl = "ON - receive";
12175 		else
12176 			flow_ctrl = "none";
12177 		if (bp->link_info.phy_qcfg_resp.option_flags &
12178 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
12179 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
12180 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
12181 			switch (sig_mode) {
12182 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
12183 				signal = "(NRZ) ";
12184 				break;
12185 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
12186 				signal = "(PAM4 56Gbps) ";
12187 				break;
12188 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
12189 				signal = "(PAM4 112Gbps) ";
12190 				break;
12191 			default:
12192 				break;
12193 			}
12194 		}
12195 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
12196 			    speed, signal, duplex, flow_ctrl);
12197 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
12198 			netdev_info(bp->dev, "EEE is %s\n",
12199 				    bp->eee.eee_active ? "active" :
12200 							 "not active");
12201 		fec = bp->link_info.fec_cfg;
12202 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
12203 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
12204 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
12205 				    bnxt_report_fec(&bp->link_info));
12206 	} else {
12207 		char *str = bnxt_link_down_reason(&bp->link_info);
12208 
12209 		netif_carrier_off(bp->dev);
12210 		netdev_err(bp->dev, "NIC Link is Down %s\n", str);
12211 	}
12212 }
12213 
12214 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
12215 {
12216 	if (!resp->supported_speeds_auto_mode &&
12217 	    !resp->supported_speeds_force_mode &&
12218 	    !resp->supported_pam4_speeds_auto_mode &&
12219 	    !resp->supported_pam4_speeds_force_mode &&
12220 	    !resp->supported_speeds2_auto_mode &&
12221 	    !resp->supported_speeds2_force_mode)
12222 		return true;
12223 	return false;
12224 }
12225 
12226 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
12227 {
12228 	struct bnxt_link_info *link_info = &bp->link_info;
12229 	struct hwrm_port_phy_qcaps_output *resp;
12230 	struct hwrm_port_phy_qcaps_input *req;
12231 	int rc = 0;
12232 
12233 	if (bp->hwrm_spec_code < 0x10201)
12234 		return 0;
12235 
12236 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
12237 	if (rc)
12238 		return rc;
12239 
12240 	resp = hwrm_req_hold(bp, req);
12241 	rc = hwrm_req_send(bp, req);
12242 	if (rc)
12243 		goto hwrm_phy_qcaps_exit;
12244 
12245 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
12246 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
12247 		struct ethtool_keee *eee = &bp->eee;
12248 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
12249 
12250 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
12251 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
12252 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
12253 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
12254 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
12255 	}
12256 
12257 	if (bp->hwrm_spec_code >= 0x10a01) {
12258 		if (bnxt_phy_qcaps_no_speed(resp)) {
12259 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
12260 			netdev_warn(bp->dev, "Ethernet link disabled\n");
12261 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
12262 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
12263 			netdev_info(bp->dev, "Ethernet link enabled\n");
12264 			/* Phy re-enabled, reprobe the speeds */
12265 			link_info->support_auto_speeds = 0;
12266 			link_info->support_pam4_auto_speeds = 0;
12267 			link_info->support_auto_speeds2 = 0;
12268 		}
12269 	}
12270 	if (resp->supported_speeds_auto_mode)
12271 		link_info->support_auto_speeds =
12272 			le16_to_cpu(resp->supported_speeds_auto_mode);
12273 	if (resp->supported_pam4_speeds_auto_mode)
12274 		link_info->support_pam4_auto_speeds =
12275 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
12276 	if (resp->supported_speeds2_auto_mode)
12277 		link_info->support_auto_speeds2 =
12278 			le16_to_cpu(resp->supported_speeds2_auto_mode);
12279 
12280 	bp->port_count = resp->port_cnt;
12281 
12282 hwrm_phy_qcaps_exit:
12283 	hwrm_req_drop(bp, req);
12284 	return rc;
12285 }
12286 
12287 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
12288 {
12289 	struct hwrm_port_mac_qcaps_output *resp;
12290 	struct hwrm_port_mac_qcaps_input *req;
12291 	int rc;
12292 
12293 	if (bp->hwrm_spec_code < 0x10a03)
12294 		return;
12295 
12296 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
12297 	if (rc)
12298 		return;
12299 
12300 	resp = hwrm_req_hold(bp, req);
12301 	rc = hwrm_req_send_silent(bp, req);
12302 	if (!rc)
12303 		bp->mac_flags = resp->flags;
12304 	hwrm_req_drop(bp, req);
12305 }
12306 
12307 static bool bnxt_support_dropped(u16 advertising, u16 supported)
12308 {
12309 	u16 diff = advertising ^ supported;
12310 
12311 	return ((supported | diff) != supported);
12312 }
12313 
12314 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
12315 {
12316 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
12317 
12318 	/* Check if any advertised speeds are no longer supported. The caller
12319 	 * holds the link_lock mutex, so we can modify link_info settings.
12320 	 */
12321 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12322 		if (bnxt_support_dropped(link_info->advertising,
12323 					 link_info->support_auto_speeds2)) {
12324 			link_info->advertising = link_info->support_auto_speeds2;
12325 			return true;
12326 		}
12327 		return false;
12328 	}
12329 	if (bnxt_support_dropped(link_info->advertising,
12330 				 link_info->support_auto_speeds)) {
12331 		link_info->advertising = link_info->support_auto_speeds;
12332 		return true;
12333 	}
12334 	if (bnxt_support_dropped(link_info->advertising_pam4,
12335 				 link_info->support_pam4_auto_speeds)) {
12336 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
12337 		return true;
12338 	}
12339 	return false;
12340 }
12341 
12342 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
12343 {
12344 	struct bnxt_link_info *link_info = &bp->link_info;
12345 	struct hwrm_port_phy_qcfg_output *resp;
12346 	struct hwrm_port_phy_qcfg_input *req;
12347 	u8 link_state = link_info->link_state;
12348 	bool support_changed;
12349 	int rc;
12350 
12351 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
12352 	if (rc)
12353 		return rc;
12354 
12355 	resp = hwrm_req_hold(bp, req);
12356 	rc = hwrm_req_send(bp, req);
12357 	if (rc) {
12358 		hwrm_req_drop(bp, req);
12359 		if (BNXT_VF(bp) && rc == -ENODEV) {
12360 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
12361 			rc = 0;
12362 		}
12363 		return rc;
12364 	}
12365 
12366 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
12367 	link_info->phy_link_status = resp->link;
12368 	link_info->duplex = resp->duplex_cfg;
12369 	if (bp->hwrm_spec_code >= 0x10800)
12370 		link_info->duplex = resp->duplex_state;
12371 	link_info->pause = resp->pause;
12372 	link_info->auto_mode = resp->auto_mode;
12373 	link_info->auto_pause_setting = resp->auto_pause;
12374 	link_info->lp_pause = resp->link_partner_adv_pause;
12375 	link_info->force_pause_setting = resp->force_pause;
12376 	link_info->duplex_setting = resp->duplex_cfg;
12377 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
12378 		link_info->link_speed = le16_to_cpu(resp->link_speed);
12379 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
12380 			link_info->active_lanes = resp->active_lanes;
12381 	} else {
12382 		link_info->link_speed = 0;
12383 		link_info->active_lanes = 0;
12384 	}
12385 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
12386 	link_info->force_pam4_link_speed =
12387 		le16_to_cpu(resp->force_pam4_link_speed);
12388 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
12389 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
12390 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
12391 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
12392 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
12393 	link_info->auto_pam4_link_speeds =
12394 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
12395 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
12396 	link_info->lp_auto_link_speeds =
12397 		le16_to_cpu(resp->link_partner_adv_speeds);
12398 	link_info->lp_auto_pam4_link_speeds =
12399 		resp->link_partner_pam4_adv_speeds;
12400 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
12401 	link_info->phy_ver[0] = resp->phy_maj;
12402 	link_info->phy_ver[1] = resp->phy_min;
12403 	link_info->phy_ver[2] = resp->phy_bld;
12404 	link_info->media_type = resp->media_type;
12405 	link_info->phy_type = resp->phy_type;
12406 	link_info->transceiver = resp->xcvr_pkg_type;
12407 	link_info->phy_addr = resp->eee_config_phy_addr &
12408 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
12409 	link_info->module_status = resp->module_status;
12410 	link_info->link_down_reason = resp->link_down_reason;
12411 
12412 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
12413 		struct ethtool_keee *eee = &bp->eee;
12414 		u16 fw_speeds;
12415 
12416 		eee->eee_active = 0;
12417 		if (resp->eee_config_phy_addr &
12418 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
12419 			eee->eee_active = 1;
12420 			fw_speeds = le16_to_cpu(
12421 				resp->link_partner_adv_eee_link_speed_mask);
12422 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
12423 		}
12424 
12425 		/* Pull initial EEE config */
12426 		if (!chng_link_state) {
12427 			if (resp->eee_config_phy_addr &
12428 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
12429 				eee->eee_enabled = 1;
12430 
12431 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
12432 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
12433 
12434 			if (resp->eee_config_phy_addr &
12435 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
12436 				__le32 tmr;
12437 
12438 				eee->tx_lpi_enabled = 1;
12439 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
12440 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
12441 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
12442 			}
12443 		}
12444 	}
12445 
12446 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
12447 	if (bp->hwrm_spec_code >= 0x10504) {
12448 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
12449 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
12450 	}
12451 	/* TODO: need to add more logic to report VF link */
12452 	if (chng_link_state) {
12453 		if (link_info->phy_link_status == BNXT_LINK_LINK)
12454 			link_info->link_state = BNXT_LINK_STATE_UP;
12455 		else
12456 			link_info->link_state = BNXT_LINK_STATE_DOWN;
12457 		if (link_state != link_info->link_state)
12458 			bnxt_report_link(bp);
12459 	} else {
12460 		/* always link down if not require to update link state */
12461 		link_info->link_state = BNXT_LINK_STATE_DOWN;
12462 	}
12463 	hwrm_req_drop(bp, req);
12464 
12465 	if (!BNXT_PHY_CFG_ABLE(bp))
12466 		return 0;
12467 
12468 	support_changed = bnxt_support_speed_dropped(link_info);
12469 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
12470 		bnxt_hwrm_set_link_setting(bp, true, false);
12471 	return 0;
12472 }
12473 
12474 static void bnxt_get_port_module_status(struct bnxt *bp)
12475 {
12476 	struct bnxt_link_info *link_info = &bp->link_info;
12477 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
12478 	u8 module_status;
12479 
12480 	if (bnxt_update_link(bp, true))
12481 		return;
12482 
12483 	module_status = link_info->module_status;
12484 	switch (module_status) {
12485 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
12486 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
12487 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
12488 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
12489 			    bp->pf.port_id);
12490 		if (bp->hwrm_spec_code >= 0x10201) {
12491 			netdev_warn(bp->dev, "Module part number %s\n",
12492 				    resp->phy_vendor_partnumber);
12493 		}
12494 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
12495 			netdev_warn(bp->dev, "TX is disabled\n");
12496 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
12497 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
12498 	}
12499 }
12500 
12501 static void
12502 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12503 {
12504 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
12505 		if (bp->hwrm_spec_code >= 0x10201)
12506 			req->auto_pause =
12507 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
12508 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12509 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
12510 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12511 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
12512 		req->enables |=
12513 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12514 	} else {
12515 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12516 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
12517 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12518 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
12519 		req->enables |=
12520 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
12521 		if (bp->hwrm_spec_code >= 0x10201) {
12522 			req->auto_pause = req->force_pause;
12523 			req->enables |= cpu_to_le32(
12524 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12525 		}
12526 	}
12527 }
12528 
12529 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12530 {
12531 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
12532 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
12533 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12534 			req->enables |=
12535 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
12536 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
12537 		} else if (bp->link_info.advertising) {
12538 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
12539 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
12540 		}
12541 		if (bp->link_info.advertising_pam4) {
12542 			req->enables |=
12543 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
12544 			req->auto_link_pam4_speed_mask =
12545 				cpu_to_le16(bp->link_info.advertising_pam4);
12546 		}
12547 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
12548 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
12549 	} else {
12550 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
12551 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12552 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
12553 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
12554 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
12555 				   (u32)bp->link_info.req_link_speed);
12556 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
12557 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12558 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
12559 		} else {
12560 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12561 		}
12562 	}
12563 
12564 	/* tell chimp that the setting takes effect immediately */
12565 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
12566 }
12567 
12568 int bnxt_hwrm_set_pause(struct bnxt *bp)
12569 {
12570 	struct hwrm_port_phy_cfg_input *req;
12571 	int rc;
12572 
12573 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12574 	if (rc)
12575 		return rc;
12576 
12577 	bnxt_hwrm_set_pause_common(bp, req);
12578 
12579 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
12580 	    bp->link_info.force_link_chng)
12581 		bnxt_hwrm_set_link_common(bp, req);
12582 
12583 	rc = hwrm_req_send(bp, req);
12584 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
12585 		/* since changing of pause setting doesn't trigger any link
12586 		 * change event, the driver needs to update the current pause
12587 		 * result upon successfully return of the phy_cfg command
12588 		 */
12589 		bp->link_info.pause =
12590 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
12591 		bp->link_info.auto_pause_setting = 0;
12592 		if (!bp->link_info.force_link_chng)
12593 			bnxt_report_link(bp);
12594 	}
12595 	bp->link_info.force_link_chng = false;
12596 	return rc;
12597 }
12598 
12599 static void bnxt_hwrm_set_eee(struct bnxt *bp,
12600 			      struct hwrm_port_phy_cfg_input *req)
12601 {
12602 	struct ethtool_keee *eee = &bp->eee;
12603 
12604 	if (eee->eee_enabled) {
12605 		u16 eee_speeds;
12606 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
12607 
12608 		if (eee->tx_lpi_enabled)
12609 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
12610 		else
12611 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
12612 
12613 		req->flags |= cpu_to_le32(flags);
12614 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
12615 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
12616 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
12617 	} else {
12618 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
12619 	}
12620 }
12621 
12622 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
12623 {
12624 	struct hwrm_port_phy_cfg_input *req;
12625 	int rc;
12626 
12627 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12628 	if (rc)
12629 		return rc;
12630 
12631 	if (set_pause)
12632 		bnxt_hwrm_set_pause_common(bp, req);
12633 
12634 	bnxt_hwrm_set_link_common(bp, req);
12635 
12636 	if (set_eee)
12637 		bnxt_hwrm_set_eee(bp, req);
12638 	return hwrm_req_send(bp, req);
12639 }
12640 
12641 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
12642 {
12643 	struct hwrm_port_phy_cfg_input *req;
12644 	int rc;
12645 
12646 	if (!BNXT_SINGLE_PF(bp))
12647 		return 0;
12648 
12649 	if (pci_num_vf(bp->pdev) &&
12650 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
12651 		return 0;
12652 
12653 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12654 	if (rc)
12655 		return rc;
12656 
12657 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
12658 	rc = hwrm_req_send(bp, req);
12659 	if (!rc) {
12660 		mutex_lock(&bp->link_lock);
12661 		/* Device is not obliged link down in certain scenarios, even
12662 		 * when forced. Setting the state unknown is consistent with
12663 		 * driver startup and will force link state to be reported
12664 		 * during subsequent open based on PORT_PHY_QCFG.
12665 		 */
12666 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
12667 		mutex_unlock(&bp->link_lock);
12668 	}
12669 	return rc;
12670 }
12671 
12672 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
12673 {
12674 #ifdef CONFIG_TEE_BNXT_FW
12675 	int rc = tee_bnxt_fw_load();
12676 
12677 	if (rc)
12678 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
12679 
12680 	return rc;
12681 #else
12682 	netdev_err(bp->dev, "OP-TEE not supported\n");
12683 	return -ENODEV;
12684 #endif
12685 }
12686 
12687 static int bnxt_try_recover_fw(struct bnxt *bp)
12688 {
12689 	if (bp->fw_health && bp->fw_health->status_reliable) {
12690 		int retry = 0, rc;
12691 		u32 sts;
12692 
12693 		do {
12694 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12695 			rc = bnxt_hwrm_poll(bp);
12696 			if (!BNXT_FW_IS_BOOTING(sts) &&
12697 			    !BNXT_FW_IS_RECOVERING(sts))
12698 				break;
12699 			retry++;
12700 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
12701 
12702 		if (!BNXT_FW_IS_HEALTHY(sts)) {
12703 			netdev_err(bp->dev,
12704 				   "Firmware not responding, status: 0x%x\n",
12705 				   sts);
12706 			rc = -ENODEV;
12707 		}
12708 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
12709 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
12710 			return bnxt_fw_reset_via_optee(bp);
12711 		}
12712 		return rc;
12713 	}
12714 
12715 	return -ENODEV;
12716 }
12717 
12718 void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
12719 {
12720 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12721 
12722 	if (!BNXT_NEW_RM(bp))
12723 		return; /* no resource reservations required */
12724 
12725 	hw_resc->resv_cp_rings = 0;
12726 	hw_resc->resv_stat_ctxs = 0;
12727 	hw_resc->resv_irqs = 0;
12728 	hw_resc->resv_tx_rings = 0;
12729 	hw_resc->resv_rx_rings = 0;
12730 	hw_resc->resv_hw_ring_grps = 0;
12731 	hw_resc->resv_vnics = 0;
12732 	hw_resc->resv_rsscos_ctxs = 0;
12733 	if (!fw_reset) {
12734 		bp->tx_nr_rings = 0;
12735 		bp->rx_nr_rings = 0;
12736 	}
12737 }
12738 
12739 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12740 {
12741 	int rc;
12742 
12743 	if (!BNXT_NEW_RM(bp))
12744 		return 0; /* no resource reservations required */
12745 
12746 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12747 	if (rc)
12748 		netdev_err(bp->dev, "resc_qcaps failed\n");
12749 
12750 	bnxt_clear_reservations(bp, fw_reset);
12751 
12752 	return rc;
12753 }
12754 
12755 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12756 {
12757 	struct hwrm_func_drv_if_change_output *resp;
12758 	struct hwrm_func_drv_if_change_input *req;
12759 	bool resc_reinit = false;
12760 	bool caps_change = false;
12761 	int rc, retry = 0;
12762 	bool fw_reset;
12763 	u32 flags = 0;
12764 
12765 	fw_reset = (bp->fw_reset_state == BNXT_FW_RESET_STATE_ABORT);
12766 	bp->fw_reset_state = 0;
12767 
12768 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12769 		return 0;
12770 
12771 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12772 	if (rc)
12773 		return rc;
12774 
12775 	if (up)
12776 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12777 	resp = hwrm_req_hold(bp, req);
12778 
12779 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12780 	while (retry < BNXT_FW_IF_RETRY) {
12781 		rc = hwrm_req_send(bp, req);
12782 		if (rc != -EAGAIN)
12783 			break;
12784 
12785 		msleep(50);
12786 		retry++;
12787 	}
12788 
12789 	if (rc == -EAGAIN) {
12790 		hwrm_req_drop(bp, req);
12791 		return rc;
12792 	} else if (!rc) {
12793 		flags = le32_to_cpu(resp->flags);
12794 	} else if (up) {
12795 		rc = bnxt_try_recover_fw(bp);
12796 		fw_reset = true;
12797 	}
12798 	hwrm_req_drop(bp, req);
12799 	if (rc)
12800 		return rc;
12801 
12802 	if (!up) {
12803 		bnxt_inv_fw_health_reg(bp);
12804 		return 0;
12805 	}
12806 
12807 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12808 		resc_reinit = true;
12809 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12810 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12811 		fw_reset = true;
12812 	else
12813 		bnxt_remap_fw_health_regs(bp);
12814 
12815 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12816 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12817 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12818 		return -ENODEV;
12819 	}
12820 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE)
12821 		caps_change = true;
12822 
12823 	if (resc_reinit || fw_reset || caps_change) {
12824 		if (fw_reset || caps_change) {
12825 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12826 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12827 				bnxt_ulp_irq_stop(bp);
12828 			bnxt_free_ctx_mem(bp, false);
12829 			bnxt_dcb_free(bp);
12830 			rc = bnxt_fw_init_one(bp);
12831 			if (rc) {
12832 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12833 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12834 				return rc;
12835 			}
12836 			/* IRQ will be initialized later in bnxt_request_irq()*/
12837 			bnxt_clear_int_mode(bp);
12838 		}
12839 		rc = bnxt_cancel_reservations(bp, fw_reset);
12840 	}
12841 	return rc;
12842 }
12843 
12844 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12845 {
12846 	struct hwrm_port_led_qcaps_output *resp;
12847 	struct hwrm_port_led_qcaps_input *req;
12848 	struct bnxt_pf_info *pf = &bp->pf;
12849 	int rc;
12850 
12851 	bp->num_leds = 0;
12852 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12853 		return 0;
12854 
12855 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12856 	if (rc)
12857 		return rc;
12858 
12859 	req->port_id = cpu_to_le16(pf->port_id);
12860 	resp = hwrm_req_hold(bp, req);
12861 	rc = hwrm_req_send(bp, req);
12862 	if (rc) {
12863 		hwrm_req_drop(bp, req);
12864 		return rc;
12865 	}
12866 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12867 		int i;
12868 
12869 		bp->num_leds = resp->num_leds;
12870 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12871 						 bp->num_leds);
12872 		for (i = 0; i < bp->num_leds; i++) {
12873 			struct bnxt_led_info *led = &bp->leds[i];
12874 			__le16 caps = led->led_state_caps;
12875 
12876 			if (!led->led_group_id ||
12877 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12878 				bp->num_leds = 0;
12879 				break;
12880 			}
12881 		}
12882 	}
12883 	hwrm_req_drop(bp, req);
12884 	return 0;
12885 }
12886 
12887 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12888 {
12889 	struct hwrm_wol_filter_alloc_output *resp;
12890 	struct hwrm_wol_filter_alloc_input *req;
12891 	int rc;
12892 
12893 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12894 	if (rc)
12895 		return rc;
12896 
12897 	req->port_id = cpu_to_le16(bp->pf.port_id);
12898 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12899 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12900 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12901 
12902 	resp = hwrm_req_hold(bp, req);
12903 	rc = hwrm_req_send(bp, req);
12904 	if (!rc)
12905 		bp->wol_filter_id = resp->wol_filter_id;
12906 	hwrm_req_drop(bp, req);
12907 	return rc;
12908 }
12909 
12910 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12911 {
12912 	struct hwrm_wol_filter_free_input *req;
12913 	int rc;
12914 
12915 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12916 	if (rc)
12917 		return rc;
12918 
12919 	req->port_id = cpu_to_le16(bp->pf.port_id);
12920 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12921 	req->wol_filter_id = bp->wol_filter_id;
12922 
12923 	return hwrm_req_send(bp, req);
12924 }
12925 
12926 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12927 {
12928 	struct hwrm_wol_filter_qcfg_output *resp;
12929 	struct hwrm_wol_filter_qcfg_input *req;
12930 	u16 next_handle = 0;
12931 	int rc;
12932 
12933 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12934 	if (rc)
12935 		return rc;
12936 
12937 	req->port_id = cpu_to_le16(bp->pf.port_id);
12938 	req->handle = cpu_to_le16(handle);
12939 	resp = hwrm_req_hold(bp, req);
12940 	rc = hwrm_req_send(bp, req);
12941 	if (!rc) {
12942 		next_handle = le16_to_cpu(resp->next_handle);
12943 		if (next_handle != 0) {
12944 			if (resp->wol_type ==
12945 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12946 				bp->wol = 1;
12947 				bp->wol_filter_id = resp->wol_filter_id;
12948 			}
12949 		}
12950 	}
12951 	hwrm_req_drop(bp, req);
12952 	return next_handle;
12953 }
12954 
12955 static void bnxt_get_wol_settings(struct bnxt *bp)
12956 {
12957 	u16 handle = 0;
12958 
12959 	bp->wol = 0;
12960 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
12961 		return;
12962 
12963 	do {
12964 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
12965 	} while (handle && handle != 0xffff);
12966 }
12967 
12968 static bool bnxt_eee_config_ok(struct bnxt *bp)
12969 {
12970 	struct ethtool_keee *eee = &bp->eee;
12971 	struct bnxt_link_info *link_info = &bp->link_info;
12972 
12973 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
12974 		return true;
12975 
12976 	if (eee->eee_enabled) {
12977 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
12978 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
12979 
12980 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
12981 
12982 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12983 			eee->eee_enabled = 0;
12984 			return false;
12985 		}
12986 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
12987 			linkmode_and(eee->advertised, advertising,
12988 				     eee->supported);
12989 			return false;
12990 		}
12991 	}
12992 	return true;
12993 }
12994 
12995 static int bnxt_update_phy_setting(struct bnxt *bp)
12996 {
12997 	int rc;
12998 	bool update_link = false;
12999 	bool update_pause = false;
13000 	bool update_eee = false;
13001 	struct bnxt_link_info *link_info = &bp->link_info;
13002 
13003 	rc = bnxt_update_link(bp, true);
13004 	if (rc) {
13005 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
13006 			   rc);
13007 		return rc;
13008 	}
13009 	if (!BNXT_SINGLE_PF(bp))
13010 		return 0;
13011 
13012 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
13013 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
13014 	    link_info->req_flow_ctrl)
13015 		update_pause = true;
13016 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
13017 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
13018 		update_pause = true;
13019 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
13020 		if (BNXT_AUTO_MODE(link_info->auto_mode))
13021 			update_link = true;
13022 		if (bnxt_force_speed_updated(link_info))
13023 			update_link = true;
13024 		if (link_info->req_duplex != link_info->duplex_setting)
13025 			update_link = true;
13026 	} else {
13027 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
13028 			update_link = true;
13029 		if (bnxt_auto_speed_updated(link_info))
13030 			update_link = true;
13031 	}
13032 
13033 	/* The last close may have shutdown the link, so need to call
13034 	 * PHY_CFG to bring it back up.
13035 	 */
13036 	if (!BNXT_LINK_IS_UP(bp))
13037 		update_link = true;
13038 
13039 	if (!bnxt_eee_config_ok(bp))
13040 		update_eee = true;
13041 
13042 	if (update_link)
13043 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
13044 	else if (update_pause)
13045 		rc = bnxt_hwrm_set_pause(bp);
13046 	if (rc) {
13047 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
13048 			   rc);
13049 		return rc;
13050 	}
13051 
13052 	return rc;
13053 }
13054 
13055 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
13056 
13057 static int bnxt_reinit_after_abort(struct bnxt *bp)
13058 {
13059 	int rc;
13060 
13061 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13062 		return -EBUSY;
13063 
13064 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
13065 		return -ENODEV;
13066 
13067 	rc = bnxt_fw_init_one(bp);
13068 	if (!rc) {
13069 		bnxt_clear_int_mode(bp);
13070 		rc = bnxt_init_int_mode(bp);
13071 		if (!rc) {
13072 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13073 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
13074 		}
13075 	}
13076 	return rc;
13077 }
13078 
13079 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
13080 {
13081 	struct bnxt_ntuple_filter *ntp_fltr;
13082 	struct bnxt_l2_filter *l2_fltr;
13083 
13084 	if (list_empty(&fltr->list))
13085 		return;
13086 
13087 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
13088 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
13089 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
13090 		atomic_inc(&l2_fltr->refcnt);
13091 		ntp_fltr->l2_fltr = l2_fltr;
13092 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
13093 			bnxt_del_ntp_filter(bp, ntp_fltr);
13094 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
13095 				   fltr->sw_id);
13096 		}
13097 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
13098 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
13099 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
13100 			bnxt_del_l2_filter(bp, l2_fltr);
13101 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
13102 				   fltr->sw_id);
13103 		}
13104 	}
13105 }
13106 
13107 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
13108 {
13109 	struct bnxt_filter_base *usr_fltr, *tmp;
13110 
13111 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
13112 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
13113 }
13114 
13115 static int bnxt_set_xps_mapping(struct bnxt *bp)
13116 {
13117 	int numa_node = dev_to_node(&bp->pdev->dev);
13118 	unsigned int q_idx, map_idx, cpu, i;
13119 	const struct cpumask *cpu_mask_ptr;
13120 	int nr_cpus = num_online_cpus();
13121 	cpumask_t *q_map;
13122 	int rc = 0;
13123 
13124 	q_map = kzalloc_objs(*q_map, bp->tx_nr_rings_per_tc);
13125 	if (!q_map)
13126 		return -ENOMEM;
13127 
13128 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
13129 	 * Each TC has the same number of TX queues. The nth TX queue for each
13130 	 * TC will have the same CPU mask.
13131 	 */
13132 	for (i = 0; i < nr_cpus; i++) {
13133 		map_idx = i % bp->tx_nr_rings_per_tc;
13134 		cpu = cpumask_local_spread(i, numa_node);
13135 		cpu_mask_ptr = get_cpu_mask(cpu);
13136 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
13137 	}
13138 
13139 	/* Register CPU mask for each TX queue except the ones marked for XDP */
13140 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
13141 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
13142 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
13143 		if (rc) {
13144 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
13145 				    q_idx);
13146 			break;
13147 		}
13148 	}
13149 
13150 	kfree(q_map);
13151 
13152 	return rc;
13153 }
13154 
13155 static int bnxt_tx_nr_rings(struct bnxt *bp)
13156 {
13157 	return bp->num_tc ? bp->tx_nr_rings_per_tc * bp->num_tc :
13158 			    bp->tx_nr_rings_per_tc;
13159 }
13160 
13161 static int bnxt_tx_nr_rings_per_tc(struct bnxt *bp)
13162 {
13163 	return bp->num_tc ? bp->tx_nr_rings / bp->num_tc : bp->tx_nr_rings;
13164 }
13165 
13166 static void bnxt_set_xdp_tx_rings(struct bnxt *bp)
13167 {
13168 	bp->tx_nr_rings_xdp = bp->tx_nr_rings_per_tc;
13169 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
13170 }
13171 
13172 static void bnxt_adj_tx_rings(struct bnxt *bp)
13173 {
13174 	/* Make adjustments if reserved TX rings are less than requested */
13175 	bp->tx_nr_rings -= bp->tx_nr_rings_xdp;
13176 	bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp);
13177 	if (bp->tx_nr_rings_xdp)
13178 		bnxt_set_xdp_tx_rings(bp);
13179 }
13180 
13181 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13182 {
13183 	int rc = 0;
13184 
13185 	netif_carrier_off(bp->dev);
13186 	if (irq_re_init) {
13187 		/* Reserve rings now if none were reserved at driver probe. */
13188 		rc = bnxt_init_dflt_ring_mode(bp);
13189 		if (rc) {
13190 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
13191 			return rc;
13192 		}
13193 	}
13194 	rc = bnxt_reserve_rings(bp, irq_re_init);
13195 	if (rc)
13196 		return rc;
13197 
13198 	bnxt_adj_tx_rings(bp);
13199 	rc = bnxt_alloc_mem(bp, irq_re_init);
13200 	if (rc) {
13201 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
13202 		goto open_err_free_mem;
13203 	}
13204 
13205 	if (irq_re_init) {
13206 		bnxt_init_napi(bp);
13207 		rc = bnxt_request_irq(bp);
13208 		if (rc) {
13209 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
13210 			goto open_err_irq;
13211 		}
13212 	}
13213 
13214 	rc = bnxt_init_nic(bp, irq_re_init);
13215 	if (rc) {
13216 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
13217 		goto open_err_irq;
13218 	}
13219 
13220 	bnxt_enable_napi(bp);
13221 	bnxt_debug_dev_init(bp);
13222 
13223 	if (link_re_init) {
13224 		mutex_lock(&bp->link_lock);
13225 		rc = bnxt_update_phy_setting(bp);
13226 		mutex_unlock(&bp->link_lock);
13227 		if (rc) {
13228 			netdev_warn(bp->dev, "failed to update phy settings\n");
13229 			if (BNXT_SINGLE_PF(bp)) {
13230 				bp->link_info.phy_retry = true;
13231 				bp->link_info.phy_retry_expires =
13232 					jiffies + 5 * HZ;
13233 			}
13234 		}
13235 	}
13236 
13237 	if (irq_re_init) {
13238 		udp_tunnel_nic_reset_ntf(bp->dev);
13239 		rc = bnxt_set_xps_mapping(bp);
13240 		if (rc)
13241 			netdev_warn(bp->dev, "failed to set xps mapping\n");
13242 	}
13243 
13244 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
13245 		if (!static_key_enabled(&bnxt_xdp_locking_key))
13246 			static_branch_enable(&bnxt_xdp_locking_key);
13247 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
13248 		static_branch_disable(&bnxt_xdp_locking_key);
13249 	}
13250 	set_bit(BNXT_STATE_OPEN, &bp->state);
13251 	bnxt_enable_int(bp);
13252 	/* Enable TX queues */
13253 	bnxt_tx_enable(bp);
13254 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13255 	/* Poll link status and check for SFP+ module status */
13256 	mutex_lock(&bp->link_lock);
13257 	bnxt_get_port_module_status(bp);
13258 	mutex_unlock(&bp->link_lock);
13259 
13260 	/* VF-reps may need to be re-opened after the PF is re-opened */
13261 	if (BNXT_PF(bp))
13262 		bnxt_vf_reps_open(bp);
13263 	bnxt_ptp_init_rtc(bp, true);
13264 	bnxt_ptp_cfg_tstamp_filters(bp);
13265 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13266 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
13267 	bnxt_cfg_usr_fltrs(bp);
13268 	return 0;
13269 
13270 open_err_irq:
13271 	bnxt_del_napi(bp);
13272 
13273 open_err_free_mem:
13274 	bnxt_free_skbs(bp);
13275 	bnxt_free_irq(bp);
13276 	bnxt_free_mem(bp, true);
13277 	return rc;
13278 }
13279 
13280 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13281 {
13282 	int rc = 0;
13283 
13284 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
13285 		rc = -EIO;
13286 	if (!rc)
13287 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
13288 	if (rc) {
13289 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
13290 		netif_close(bp->dev);
13291 	}
13292 	return rc;
13293 }
13294 
13295 /* netdev instance lock held, open the NIC half way by allocating all
13296  * resources, but NAPI, IRQ, and TX are not enabled.  This is mainly used
13297  * for offline self tests.
13298  */
13299 int bnxt_half_open_nic(struct bnxt *bp)
13300 {
13301 	int rc = 0;
13302 
13303 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13304 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
13305 		rc = -ENODEV;
13306 		goto half_open_err;
13307 	}
13308 
13309 	rc = bnxt_alloc_mem(bp, true);
13310 	if (rc) {
13311 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
13312 		goto half_open_err;
13313 	}
13314 	bnxt_init_napi(bp);
13315 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13316 	rc = bnxt_init_nic(bp, true);
13317 	if (rc) {
13318 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13319 		bnxt_del_napi(bp);
13320 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
13321 		goto half_open_err;
13322 	}
13323 	return 0;
13324 
13325 half_open_err:
13326 	bnxt_free_skbs(bp);
13327 	bnxt_free_mem(bp, true);
13328 	netif_close(bp->dev);
13329 	return rc;
13330 }
13331 
13332 /* netdev instance lock held, this call can only be made after a previous
13333  * successful call to bnxt_half_open_nic().
13334  */
13335 void bnxt_half_close_nic(struct bnxt *bp)
13336 {
13337 	bnxt_hwrm_resource_free(bp, false, true);
13338 	bnxt_del_napi(bp);
13339 	bnxt_free_skbs(bp);
13340 	bnxt_free_mem(bp, true);
13341 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13342 }
13343 
13344 void bnxt_reenable_sriov(struct bnxt *bp)
13345 {
13346 	if (BNXT_PF(bp)) {
13347 		struct bnxt_pf_info *pf = &bp->pf;
13348 		int n = pf->active_vfs;
13349 
13350 		if (n)
13351 			bnxt_cfg_hw_sriov(bp, &n, true);
13352 	}
13353 }
13354 
13355 static int bnxt_open(struct net_device *dev)
13356 {
13357 	struct bnxt *bp = netdev_priv(dev);
13358 	int rc;
13359 
13360 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13361 		rc = bnxt_reinit_after_abort(bp);
13362 		if (rc) {
13363 			if (rc == -EBUSY)
13364 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
13365 			else
13366 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
13367 			return -ENODEV;
13368 		}
13369 	}
13370 
13371 	rc = bnxt_hwrm_if_change(bp, true);
13372 	if (rc)
13373 		return rc;
13374 
13375 	rc = __bnxt_open_nic(bp, true, true);
13376 	if (rc) {
13377 		bnxt_hwrm_if_change(bp, false);
13378 	} else {
13379 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
13380 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13381 				bnxt_queue_sp_work(bp,
13382 						   BNXT_RESTART_ULP_SP_EVENT);
13383 		}
13384 	}
13385 
13386 	return rc;
13387 }
13388 
13389 static bool bnxt_drv_busy(struct bnxt *bp)
13390 {
13391 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
13392 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
13393 }
13394 
13395 static void bnxt_get_ring_stats(struct bnxt *bp,
13396 				struct rtnl_link_stats64 *stats);
13397 
13398 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
13399 			     bool link_re_init)
13400 {
13401 	/* Close the VF-reps before closing PF */
13402 	if (BNXT_PF(bp))
13403 		bnxt_vf_reps_close(bp);
13404 
13405 	/* Change device state to avoid TX queue wake up's */
13406 	bnxt_tx_disable(bp);
13407 
13408 	clear_bit(BNXT_STATE_OPEN, &bp->state);
13409 	smp_mb__after_atomic();
13410 	while (bnxt_drv_busy(bp))
13411 		msleep(20);
13412 
13413 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13414 		bnxt_clear_rss_ctxs(bp);
13415 	/* Flush rings and disable interrupts */
13416 	bnxt_shutdown_nic(bp, irq_re_init);
13417 
13418 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
13419 
13420 	bnxt_debug_dev_exit(bp);
13421 	bnxt_disable_napi(bp);
13422 	timer_delete_sync(&bp->timer);
13423 	bnxt_free_skbs(bp);
13424 
13425 	/* Save ring stats before shutdown */
13426 	if (bp->bnapi && irq_re_init) {
13427 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
13428 		bnxt_get_ring_drv_stats(bp, &bp->ring_drv_stats_prev);
13429 	}
13430 	if (irq_re_init) {
13431 		bnxt_free_irq(bp);
13432 		bnxt_del_napi(bp);
13433 	}
13434 	bnxt_free_mem(bp, irq_re_init);
13435 }
13436 
13437 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13438 {
13439 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13440 		/* If we get here, it means firmware reset is in progress
13441 		 * while we are trying to close.  We can safely proceed with
13442 		 * the close because we are holding netdev instance lock.
13443 		 * Some firmware messages may fail as we proceed to close.
13444 		 * We set the ABORT_ERR flag here so that the FW reset thread
13445 		 * will later abort when it gets the netdev instance lock
13446 		 * and sees the flag.
13447 		 */
13448 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
13449 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13450 	}
13451 
13452 #ifdef CONFIG_BNXT_SRIOV
13453 	if (bp->sriov_cfg) {
13454 		int rc;
13455 
13456 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
13457 						      !bp->sriov_cfg,
13458 						      BNXT_SRIOV_CFG_WAIT_TMO);
13459 		if (!rc)
13460 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
13461 		else if (rc < 0)
13462 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
13463 	}
13464 #endif
13465 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
13466 }
13467 
13468 static int bnxt_close(struct net_device *dev)
13469 {
13470 	struct bnxt *bp = netdev_priv(dev);
13471 
13472 	bnxt_close_nic(bp, true, true);
13473 	bnxt_hwrm_shutdown_link(bp);
13474 	bnxt_hwrm_if_change(bp, false);
13475 	return 0;
13476 }
13477 
13478 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
13479 				   u16 *val)
13480 {
13481 	struct hwrm_port_phy_mdio_read_output *resp;
13482 	struct hwrm_port_phy_mdio_read_input *req;
13483 	int rc;
13484 
13485 	if (bp->hwrm_spec_code < 0x10a00)
13486 		return -EOPNOTSUPP;
13487 
13488 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
13489 	if (rc)
13490 		return rc;
13491 
13492 	req->port_id = cpu_to_le16(bp->pf.port_id);
13493 	req->phy_addr = phy_addr;
13494 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13495 	if (mdio_phy_id_is_c45(phy_addr)) {
13496 		req->cl45_mdio = 1;
13497 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13498 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13499 		req->reg_addr = cpu_to_le16(reg);
13500 	}
13501 
13502 	resp = hwrm_req_hold(bp, req);
13503 	rc = hwrm_req_send(bp, req);
13504 	if (!rc)
13505 		*val = le16_to_cpu(resp->reg_data);
13506 	hwrm_req_drop(bp, req);
13507 	return rc;
13508 }
13509 
13510 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
13511 				    u16 val)
13512 {
13513 	struct hwrm_port_phy_mdio_write_input *req;
13514 	int rc;
13515 
13516 	if (bp->hwrm_spec_code < 0x10a00)
13517 		return -EOPNOTSUPP;
13518 
13519 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
13520 	if (rc)
13521 		return rc;
13522 
13523 	req->port_id = cpu_to_le16(bp->pf.port_id);
13524 	req->phy_addr = phy_addr;
13525 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13526 	if (mdio_phy_id_is_c45(phy_addr)) {
13527 		req->cl45_mdio = 1;
13528 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13529 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13530 		req->reg_addr = cpu_to_le16(reg);
13531 	}
13532 	req->reg_data = cpu_to_le16(val);
13533 
13534 	return hwrm_req_send(bp, req);
13535 }
13536 
13537 /* netdev instance lock held */
13538 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13539 {
13540 	struct mii_ioctl_data *mdio = if_mii(ifr);
13541 	struct bnxt *bp = netdev_priv(dev);
13542 	int rc;
13543 
13544 	switch (cmd) {
13545 	case SIOCGMIIPHY:
13546 		mdio->phy_id = bp->link_info.phy_addr;
13547 
13548 		fallthrough;
13549 	case SIOCGMIIREG: {
13550 		u16 mii_regval = 0;
13551 
13552 		if (!netif_running(dev))
13553 			return -EAGAIN;
13554 
13555 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
13556 					     &mii_regval);
13557 		mdio->val_out = mii_regval;
13558 		return rc;
13559 	}
13560 
13561 	case SIOCSMIIREG:
13562 		if (!netif_running(dev))
13563 			return -EAGAIN;
13564 
13565 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
13566 						mdio->val_in);
13567 
13568 	default:
13569 		/* do nothing */
13570 		break;
13571 	}
13572 	return -EOPNOTSUPP;
13573 }
13574 
13575 static void bnxt_get_ring_stats(struct bnxt *bp,
13576 				struct rtnl_link_stats64 *stats)
13577 {
13578 	int i;
13579 
13580 	for (i = 0; i < bp->cp_nr_rings; i++) {
13581 		struct bnxt_napi *bnapi = bp->bnapi[i];
13582 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13583 		u64 *sw = cpr->stats.sw_stats;
13584 
13585 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
13586 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13587 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
13588 
13589 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
13590 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
13591 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
13592 
13593 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
13594 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
13595 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
13596 
13597 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
13598 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
13599 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
13600 
13601 		stats->rx_missed_errors +=
13602 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
13603 
13604 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13605 
13606 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
13607 
13608 		stats->rx_dropped +=
13609 			cpr->sw_stats->rx.rx_netpoll_discards +
13610 			cpr->sw_stats->rx.rx_oom_discards;
13611 	}
13612 }
13613 
13614 static void bnxt_add_prev_stats(struct bnxt *bp,
13615 				struct rtnl_link_stats64 *stats)
13616 {
13617 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
13618 
13619 	stats->rx_packets += prev_stats->rx_packets;
13620 	stats->tx_packets += prev_stats->tx_packets;
13621 	stats->rx_bytes += prev_stats->rx_bytes;
13622 	stats->tx_bytes += prev_stats->tx_bytes;
13623 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
13624 	stats->multicast += prev_stats->multicast;
13625 	stats->rx_dropped += prev_stats->rx_dropped;
13626 	stats->tx_dropped += prev_stats->tx_dropped;
13627 }
13628 
13629 static void
13630 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
13631 {
13632 	struct bnxt *bp = netdev_priv(dev);
13633 
13634 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
13635 	/* Make sure bnxt_close_nic() sees that we are reading stats before
13636 	 * we check the BNXT_STATE_OPEN flag.
13637 	 */
13638 	smp_mb__after_atomic();
13639 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13640 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13641 		*stats = bp->net_stats_prev;
13642 		return;
13643 	}
13644 
13645 	bnxt_sync_ring_stats(bp);
13646 	bnxt_get_ring_stats(bp, stats);
13647 	bnxt_add_prev_stats(bp, stats);
13648 
13649 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
13650 		u64 *rx = bp->port_stats.sw_stats;
13651 		u64 *tx = bp->port_stats.sw_stats +
13652 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
13653 
13654 		stats->rx_crc_errors =
13655 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
13656 		stats->rx_frame_errors =
13657 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
13658 		stats->rx_length_errors =
13659 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
13660 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
13661 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
13662 		stats->rx_errors =
13663 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
13664 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
13665 		stats->collisions =
13666 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
13667 		stats->tx_fifo_errors =
13668 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
13669 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
13670 	}
13671 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13672 }
13673 
13674 static void bnxt_get_one_ring_drv_stats(struct bnxt *bp,
13675 					struct bnxt_total_ring_drv_stats *stats,
13676 					struct bnxt_cp_ring_info *cpr)
13677 {
13678 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
13679 	u64 *hw_stats = cpr->stats.sw_stats;
13680 
13681 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
13682 	stats->rx_total_resets += sw_stats->rx.rx_resets;
13683 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
13684 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
13685 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
13686 	stats->rx_total_ring_discards +=
13687 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
13688 	stats->rx_total_hw_gro_packets += sw_stats->rx.rx_hw_gro_packets;
13689 	stats->rx_total_hw_gro_wire_packets += sw_stats->rx.rx_hw_gro_wire_packets;
13690 	stats->tx_total_resets += sw_stats->tx.tx_resets;
13691 	stats->tx_total_ring_discards +=
13692 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
13693 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
13694 }
13695 
13696 void bnxt_get_ring_drv_stats(struct bnxt *bp,
13697 			     struct bnxt_total_ring_drv_stats *stats)
13698 {
13699 	int i;
13700 
13701 	for (i = 0; i < bp->cp_nr_rings; i++)
13702 		bnxt_get_one_ring_drv_stats(bp, stats, &bp->bnapi[i]->cp_ring);
13703 }
13704 
13705 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask,
13706 				 const struct netdev_hw_addr_list *mc)
13707 {
13708 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13709 	struct netdev_hw_addr *ha;
13710 	u8 *haddr;
13711 	int mc_count = 0;
13712 	bool update = false;
13713 	int off = 0;
13714 
13715 	netdev_hw_addr_list_for_each(ha, mc) {
13716 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
13717 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13718 			vnic->mc_list_count = 0;
13719 			return false;
13720 		}
13721 		haddr = ha->addr;
13722 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
13723 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
13724 			update = true;
13725 		}
13726 		off += ETH_ALEN;
13727 		mc_count++;
13728 	}
13729 	if (mc_count)
13730 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13731 
13732 	if (mc_count != vnic->mc_list_count) {
13733 		vnic->mc_list_count = mc_count;
13734 		update = true;
13735 	}
13736 	return update;
13737 }
13738 
13739 static bool bnxt_uc_list_updated(struct bnxt *bp,
13740 				 const struct netdev_hw_addr_list *uc)
13741 {
13742 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13743 	struct netdev_hw_addr *ha;
13744 	int off = 0;
13745 
13746 	if (netdev_hw_addr_list_count(uc) != (vnic->uc_filter_count - 1))
13747 		return true;
13748 
13749 	netdev_hw_addr_list_for_each(ha, uc) {
13750 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
13751 			return true;
13752 
13753 		off += ETH_ALEN;
13754 	}
13755 	return false;
13756 }
13757 
13758 static int bnxt_set_rx_mode(struct net_device *dev,
13759 			    struct netdev_hw_addr_list *uc,
13760 			    struct netdev_hw_addr_list *mc)
13761 {
13762 	struct bnxt *bp = netdev_priv(dev);
13763 	struct bnxt_vnic_info *vnic;
13764 	bool mc_update = false;
13765 	bool uc_update;
13766 	u32 mask;
13767 
13768 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13769 		return 0;
13770 
13771 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13772 	mask = vnic->rx_mask;
13773 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13774 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13775 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13776 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13777 
13778 	if (dev->flags & IFF_PROMISC)
13779 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13780 
13781 	uc_update = bnxt_uc_list_updated(bp, uc);
13782 
13783 	if (dev->flags & IFF_BROADCAST)
13784 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13785 	if (dev->flags & IFF_ALLMULTI) {
13786 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13787 		vnic->mc_list_count = 0;
13788 	} else if (dev->flags & IFF_MULTICAST) {
13789 		mc_update = bnxt_mc_list_updated(bp, &mask, mc);
13790 	}
13791 
13792 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13793 		vnic->rx_mask = mask;
13794 
13795 		return bnxt_cfg_rx_mode(bp, uc, uc_update);
13796 	}
13797 
13798 	return 0;
13799 }
13800 
13801 static int bnxt_cfg_rx_mode(struct bnxt *bp, struct netdev_hw_addr_list *uc,
13802 			    bool uc_update)
13803 {
13804 	struct net_device *dev = bp->dev;
13805 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13806 	struct netdev_hw_addr *ha;
13807 	int i, off = 0, rc;
13808 
13809 	if (!uc_update)
13810 		goto skip_uc;
13811 
13812 	for (i = 1; i < vnic->uc_filter_count; i++) {
13813 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13814 
13815 		bnxt_hwrm_l2_filter_free(bp, fltr);
13816 		bnxt_del_l2_filter(bp, fltr);
13817 	}
13818 
13819 	vnic->uc_filter_count = 1;
13820 
13821 	netif_addr_lock_bh(dev);
13822 	if (netdev_hw_addr_list_count(uc) > (BNXT_MAX_UC_ADDRS - 1)) {
13823 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13824 	} else {
13825 		netdev_hw_addr_list_for_each(ha, uc) {
13826 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13827 			off += ETH_ALEN;
13828 			vnic->uc_filter_count++;
13829 		}
13830 	}
13831 	netif_addr_unlock_bh(dev);
13832 
13833 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13834 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13835 		if (rc) {
13836 			if (BNXT_VF(bp) && rc == -ENODEV) {
13837 				netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13838 				rc = -EAGAIN;
13839 			} else if (rc == -EAGAIN) {
13840 				netdev_warn(bp->dev, "FW busy while setting vnic filter, will retry\n");
13841 			} else {
13842 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13843 			}
13844 			vnic->uc_filter_count = i;
13845 			return rc;
13846 		}
13847 	}
13848 
13849 skip_uc:
13850 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13851 	    !bnxt_promisc_ok(bp))
13852 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13853 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13854 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13855 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13856 			    rc);
13857 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13858 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13859 		vnic->mc_list_count = 0;
13860 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13861 	}
13862 	if (rc)
13863 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13864 			   rc);
13865 
13866 	return rc;
13867 }
13868 
13869 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13870 {
13871 #ifdef CONFIG_BNXT_SRIOV
13872 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13873 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13874 
13875 		/* No minimum rings were provisioned by the PF.  Don't
13876 		 * reserve rings by default when device is down.
13877 		 */
13878 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13879 			return true;
13880 
13881 		if (!netif_running(bp->dev))
13882 			return false;
13883 	}
13884 #endif
13885 	return true;
13886 }
13887 
13888 /* If the chip and firmware supports RFS */
13889 static bool bnxt_rfs_supported(struct bnxt *bp)
13890 {
13891 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13892 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13893 			return true;
13894 		return false;
13895 	}
13896 	/* 212 firmware is broken for aRFS */
13897 	if (BNXT_FW_MAJ(bp) == 212)
13898 		return false;
13899 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13900 		return true;
13901 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13902 		return true;
13903 	return false;
13904 }
13905 
13906 /* If runtime conditions support RFS */
13907 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13908 {
13909 	struct bnxt_hw_rings hwr = {0};
13910 	int max_vnics, max_rss_ctxs;
13911 
13912 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13913 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13914 		return bnxt_rfs_supported(bp);
13915 
13916 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13917 		return false;
13918 
13919 	hwr.grp = bp->rx_nr_rings;
13920 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13921 	if (new_rss_ctx)
13922 		hwr.vnic++;
13923 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13924 	max_vnics = bnxt_get_max_func_vnics(bp);
13925 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13926 
13927 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13928 		if (bp->rx_nr_rings > 1)
13929 			netdev_warn(bp->dev,
13930 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13931 				    min(max_rss_ctxs - 1, max_vnics - 1));
13932 		return false;
13933 	}
13934 
13935 	if (!BNXT_NEW_RM(bp))
13936 		return true;
13937 
13938 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13939 	 * issue that will mess up the default VNIC if we reduce the
13940 	 * reservations.
13941 	 */
13942 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13943 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13944 		return true;
13945 
13946 	bnxt_hwrm_reserve_rings(bp, &hwr);
13947 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13948 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13949 		return true;
13950 
13951 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13952 	hwr.vnic = 1;
13953 	hwr.rss_ctx = 0;
13954 	bnxt_hwrm_reserve_rings(bp, &hwr);
13955 	return false;
13956 }
13957 
13958 static netdev_features_t bnxt_fix_features(struct net_device *dev,
13959 					   netdev_features_t features)
13960 {
13961 	struct bnxt *bp = netdev_priv(dev);
13962 	netdev_features_t vlan_features;
13963 
13964 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
13965 		features &= ~NETIF_F_NTUPLE;
13966 
13967 	if ((features & NETIF_F_GSO_UDP_L4) &&
13968 	    !(bp->flags & BNXT_FLAG_UDP_GSO_CAP) &&
13969 	    bp->tx_ring_size < 2 * BNXT_SW_USO_MAX_DESCS)
13970 		features &= ~NETIF_F_GSO_UDP_L4;
13971 
13972 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
13973 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13974 
13975 	if (!(features & NETIF_F_GRO))
13976 		features &= ~NETIF_F_GRO_HW;
13977 
13978 	if (features & NETIF_F_GRO_HW)
13979 		features &= ~NETIF_F_LRO;
13980 
13981 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
13982 	 * turned on or off together.
13983 	 */
13984 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
13985 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
13986 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13987 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13988 		else if (vlan_features)
13989 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13990 	}
13991 #ifdef CONFIG_BNXT_SRIOV
13992 	if (BNXT_VF(bp) && bp->vf.vlan)
13993 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13994 #endif
13995 	return features;
13996 }
13997 
13998 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
13999 				bool link_re_init, u32 flags, bool update_tpa)
14000 {
14001 	bnxt_close_nic(bp, irq_re_init, link_re_init);
14002 	bp->flags = flags;
14003 	if (update_tpa)
14004 		bnxt_set_ring_params(bp);
14005 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
14006 }
14007 
14008 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
14009 {
14010 	bool update_tpa = false, update_ntuple = false;
14011 	struct bnxt *bp = netdev_priv(dev);
14012 	u32 flags = bp->flags;
14013 	u32 changes;
14014 	int rc = 0;
14015 	bool re_init = false;
14016 
14017 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
14018 				   bnxt_min_tx_desc_cnt(bp, features));
14019 
14020 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
14021 	if (features & NETIF_F_GRO_HW)
14022 		flags |= BNXT_FLAG_GRO;
14023 	else if (features & NETIF_F_LRO)
14024 		flags |= BNXT_FLAG_LRO;
14025 
14026 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
14027 		flags &= ~BNXT_FLAG_TPA;
14028 
14029 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
14030 		flags |= BNXT_FLAG_STRIP_VLAN;
14031 
14032 	if (features & NETIF_F_NTUPLE)
14033 		flags |= BNXT_FLAG_RFS;
14034 	else
14035 		bnxt_clear_usr_fltrs(bp, true);
14036 
14037 	changes = flags ^ bp->flags;
14038 	if (changes & BNXT_FLAG_TPA) {
14039 		update_tpa = true;
14040 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
14041 		    (flags & BNXT_FLAG_TPA) == 0 ||
14042 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14043 			re_init = true;
14044 	}
14045 
14046 	if (changes & ~BNXT_FLAG_TPA)
14047 		re_init = true;
14048 
14049 	if (changes & BNXT_FLAG_RFS)
14050 		update_ntuple = true;
14051 
14052 	if (flags != bp->flags) {
14053 		u32 old_flags = bp->flags;
14054 
14055 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14056 			bp->flags = flags;
14057 			if (update_tpa)
14058 				bnxt_set_ring_params(bp);
14059 			return rc;
14060 		}
14061 
14062 		if (update_ntuple)
14063 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
14064 
14065 		if (re_init)
14066 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
14067 
14068 		if (update_tpa) {
14069 			bp->flags = flags;
14070 			rc = bnxt_set_tpa(bp,
14071 					  (flags & BNXT_FLAG_TPA) ?
14072 					  true : false);
14073 			if (rc)
14074 				bp->flags = old_flags;
14075 		}
14076 	}
14077 	return rc;
14078 }
14079 
14080 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
14081 			      u8 **nextp)
14082 {
14083 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
14084 	int hdr_count = 0;
14085 	u8 *nexthdr;
14086 	int start;
14087 
14088 	/* Check that there are at most 2 IPv6 extension headers, no
14089 	 * fragment header, and each is <= 64 bytes.
14090 	 */
14091 	start = nw_off + sizeof(*ip6h);
14092 	nexthdr = &ip6h->nexthdr;
14093 	while (ipv6_ext_hdr(*nexthdr)) {
14094 		struct ipv6_opt_hdr *hp;
14095 		int hdrlen;
14096 
14097 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
14098 		    *nexthdr == NEXTHDR_FRAGMENT)
14099 			return false;
14100 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
14101 					  skb_headlen(skb), NULL);
14102 		if (!hp)
14103 			return false;
14104 		if (*nexthdr == NEXTHDR_AUTH)
14105 			hdrlen = ipv6_authlen(hp);
14106 		else
14107 			hdrlen = ipv6_optlen(hp);
14108 
14109 		if (hdrlen > 64)
14110 			return false;
14111 
14112 		hdr_count++;
14113 		nexthdr = &hp->nexthdr;
14114 		start += hdrlen;
14115 	}
14116 	if (nextp) {
14117 		/* Caller will check inner protocol */
14118 		if (skb->encapsulation) {
14119 			*nextp = nexthdr;
14120 			return true;
14121 		}
14122 		*nextp = NULL;
14123 	}
14124 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
14125 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
14126 }
14127 
14128 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
14129 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
14130 {
14131 	struct udphdr *uh = udp_hdr(skb);
14132 	__be16 udp_port = uh->dest;
14133 
14134 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
14135 	    udp_port != bp->vxlan_gpe_port)
14136 		return false;
14137 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
14138 		struct ethhdr *eh = inner_eth_hdr(skb);
14139 
14140 		switch (eh->h_proto) {
14141 		case htons(ETH_P_IP):
14142 			return true;
14143 		case htons(ETH_P_IPV6):
14144 			return bnxt_exthdr_check(bp, skb,
14145 						 skb_inner_network_offset(skb),
14146 						 NULL);
14147 		}
14148 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
14149 		return true;
14150 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
14151 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
14152 					 NULL);
14153 	}
14154 	return false;
14155 }
14156 
14157 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
14158 {
14159 	switch (l4_proto) {
14160 	case IPPROTO_UDP:
14161 		return bnxt_udp_tunl_check(bp, skb);
14162 	case IPPROTO_IPIP:
14163 		return true;
14164 	case IPPROTO_GRE: {
14165 		switch (skb->inner_protocol) {
14166 		default:
14167 			return false;
14168 		case htons(ETH_P_IP):
14169 			return true;
14170 		case htons(ETH_P_IPV6):
14171 			fallthrough;
14172 		}
14173 	}
14174 	case IPPROTO_IPV6:
14175 		/* Check ext headers of inner ipv6 */
14176 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
14177 					 NULL);
14178 	}
14179 	return false;
14180 }
14181 
14182 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
14183 					     struct net_device *dev,
14184 					     netdev_features_t features)
14185 {
14186 	struct bnxt *bp = netdev_priv(dev);
14187 	u8 *l4_proto;
14188 
14189 	features = vlan_features_check(skb, features);
14190 	switch (vlan_get_protocol(skb)) {
14191 	case htons(ETH_P_IP):
14192 		if (!skb->encapsulation)
14193 			return features;
14194 		l4_proto = &ip_hdr(skb)->protocol;
14195 		if (bnxt_tunl_check(bp, skb, *l4_proto))
14196 			return features;
14197 		break;
14198 	case htons(ETH_P_IPV6):
14199 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
14200 				       &l4_proto))
14201 			break;
14202 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
14203 			return features;
14204 		break;
14205 	}
14206 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
14207 }
14208 
14209 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
14210 			 u32 *reg_buf)
14211 {
14212 	struct hwrm_dbg_read_direct_output *resp;
14213 	struct hwrm_dbg_read_direct_input *req;
14214 	__le32 *dbg_reg_buf;
14215 	dma_addr_t mapping;
14216 	int rc, i;
14217 
14218 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
14219 	if (rc)
14220 		return rc;
14221 
14222 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
14223 					 &mapping);
14224 	if (!dbg_reg_buf) {
14225 		rc = -ENOMEM;
14226 		goto dbg_rd_reg_exit;
14227 	}
14228 
14229 	req->host_dest_addr = cpu_to_le64(mapping);
14230 
14231 	resp = hwrm_req_hold(bp, req);
14232 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
14233 	req->read_len32 = cpu_to_le32(num_words);
14234 
14235 	rc = hwrm_req_send(bp, req);
14236 	if (rc || resp->error_code) {
14237 		rc = -EIO;
14238 		goto dbg_rd_reg_exit;
14239 	}
14240 	for (i = 0; i < num_words; i++)
14241 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
14242 
14243 dbg_rd_reg_exit:
14244 	hwrm_req_drop(bp, req);
14245 	return rc;
14246 }
14247 
14248 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
14249 				       u32 ring_id, u32 *prod, u32 *cons)
14250 {
14251 	struct hwrm_dbg_ring_info_get_output *resp;
14252 	struct hwrm_dbg_ring_info_get_input *req;
14253 	int rc;
14254 
14255 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
14256 	if (rc)
14257 		return rc;
14258 
14259 	req->ring_type = ring_type;
14260 	req->fw_ring_id = cpu_to_le32(ring_id);
14261 	resp = hwrm_req_hold(bp, req);
14262 	rc = hwrm_req_send(bp, req);
14263 	if (!rc) {
14264 		*prod = le32_to_cpu(resp->producer_index);
14265 		*cons = le32_to_cpu(resp->consumer_index);
14266 	}
14267 	hwrm_req_drop(bp, req);
14268 	return rc;
14269 }
14270 
14271 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
14272 {
14273 	struct bnxt_tx_ring_info *txr;
14274 	int i = bnapi->index, j;
14275 
14276 	bnxt_for_each_napi_tx(j, bnapi, txr)
14277 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
14278 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
14279 			    txr->tx_cons);
14280 }
14281 
14282 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
14283 {
14284 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
14285 	int i = bnapi->index;
14286 
14287 	if (!rxr)
14288 		return;
14289 
14290 	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",
14291 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
14292 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
14293 		    rxr->rx_sw_agg_prod);
14294 }
14295 
14296 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
14297 {
14298 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring, *cpr2;
14299 	int i = bnapi->index, j;
14300 
14301 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
14302 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
14303 	for (j = 0; j < cpr->cp_ring_count; j++) {
14304 		cpr2 = &cpr->cp_ring_arr[j];
14305 		if (!cpr2->bnapi)
14306 			continue;
14307 		netdev_info(bnapi->bp->dev, "[%d.%d]: cp{fw_ring: %d raw_cons: %x}\n",
14308 			    i, j, cpr2->cp_ring_struct.fw_ring_id,
14309 			    cpr2->cp_raw_cons);
14310 	}
14311 }
14312 
14313 static void bnxt_dbg_dump_states(struct bnxt *bp)
14314 {
14315 	int i;
14316 	struct bnxt_napi *bnapi;
14317 
14318 	for (i = 0; i < bp->cp_nr_rings; i++) {
14319 		bnapi = bp->bnapi[i];
14320 		if (netif_msg_drv(bp)) {
14321 			bnxt_dump_tx_sw_state(bnapi);
14322 			bnxt_dump_rx_sw_state(bnapi);
14323 			bnxt_dump_cp_sw_state(bnapi);
14324 		}
14325 	}
14326 }
14327 
14328 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
14329 {
14330 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
14331 	struct hwrm_ring_reset_input *req;
14332 	struct bnxt_napi *bnapi = rxr->bnapi;
14333 	struct bnxt_cp_ring_info *cpr;
14334 	u16 cp_ring_id;
14335 	int rc;
14336 
14337 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
14338 	if (rc)
14339 		return rc;
14340 
14341 	cpr = &bnapi->cp_ring;
14342 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
14343 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
14344 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
14345 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
14346 	return hwrm_req_send_silent(bp, req);
14347 }
14348 
14349 static void bnxt_reset_task(struct bnxt *bp, bool silent)
14350 {
14351 	if (!silent)
14352 		bnxt_dbg_dump_states(bp);
14353 	if (netif_running(bp->dev)) {
14354 		bnxt_close_nic(bp, !silent, false);
14355 		bnxt_open_nic(bp, !silent, false);
14356 	}
14357 }
14358 
14359 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
14360 {
14361 	struct bnxt *bp = netdev_priv(dev);
14362 
14363 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
14364 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
14365 }
14366 
14367 static void bnxt_fw_health_check(struct bnxt *bp)
14368 {
14369 	struct bnxt_fw_health *fw_health = bp->fw_health;
14370 	struct pci_dev *pdev = bp->pdev;
14371 	u32 val;
14372 
14373 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14374 		return;
14375 
14376 	/* Make sure it is enabled before checking the tmr_counter. */
14377 	smp_rmb();
14378 	if (fw_health->tmr_counter) {
14379 		fw_health->tmr_counter--;
14380 		return;
14381 	}
14382 
14383 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14384 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
14385 		fw_health->arrests++;
14386 		goto fw_reset;
14387 	}
14388 
14389 	fw_health->last_fw_heartbeat = val;
14390 
14391 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14392 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
14393 		fw_health->discoveries++;
14394 		goto fw_reset;
14395 	}
14396 
14397 	fw_health->tmr_counter = fw_health->tmr_multiplier;
14398 	return;
14399 
14400 fw_reset:
14401 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
14402 }
14403 
14404 static void bnxt_timer(struct timer_list *t)
14405 {
14406 	struct bnxt *bp = timer_container_of(bp, t, timer);
14407 	struct net_device *dev = bp->dev;
14408 
14409 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
14410 		return;
14411 
14412 	if (atomic_read(&bp->intr_sem) != 0)
14413 		goto bnxt_restart_timer;
14414 
14415 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
14416 		bnxt_fw_health_check(bp);
14417 
14418 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
14419 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
14420 
14421 	if (bnxt_tc_flower_enabled(bp))
14422 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
14423 
14424 #ifdef CONFIG_RFS_ACCEL
14425 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
14426 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14427 #endif /*CONFIG_RFS_ACCEL*/
14428 
14429 	if (bp->link_info.phy_retry) {
14430 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
14431 			bp->link_info.phy_retry = false;
14432 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
14433 		} else {
14434 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
14435 		}
14436 	}
14437 
14438 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
14439 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
14440 
14441 bnxt_restart_timer:
14442 	mod_timer(&bp->timer, jiffies + bp->current_interval);
14443 }
14444 
14445 static void bnxt_lock_sp(struct bnxt *bp)
14446 {
14447 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
14448 	 * set.  If the device is being closed, bnxt_close() may be holding
14449 	 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear.
14450 	 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev
14451 	 * instance lock.
14452 	 */
14453 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14454 	netdev_lock(bp->dev);
14455 }
14456 
14457 static void bnxt_unlock_sp(struct bnxt *bp)
14458 {
14459 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14460 	netdev_unlock(bp->dev);
14461 }
14462 
14463 /* Same as bnxt_lock_sp() with additional rtnl_lock */
14464 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
14465 {
14466 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14467 	rtnl_lock();
14468 	netdev_lock(bp->dev);
14469 }
14470 
14471 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
14472 {
14473 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14474 	netdev_unlock(bp->dev);
14475 	rtnl_unlock();
14476 }
14477 
14478 static void bnxt_tph_update(struct bnxt *bp)
14479 {
14480 	struct net_device *dev = bp->dev;
14481 	int i;
14482 
14483 	bnxt_lock_sp(bp);
14484 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
14485 		goto unlock;
14486 
14487 	for (i = 0; i < bp->rx_nr_rings; i++) {
14488 		struct bnxt_irq *irq;
14489 		int map_idx, err;
14490 		u16 tag;
14491 
14492 		map_idx = bnxt_cp_num_to_irq_num(bp, i);
14493 		irq = &bp->irq_tbl[map_idx];
14494 		tag = READ_ONCE(irq->new_tag);
14495 		if (irq->tag == tag)
14496 			continue;
14497 
14498 		if (pcie_tph_set_st_entry(bp->pdev, irq->msix_nr, tag))
14499 			continue;
14500 
14501 		err = netdev_rx_queue_restart(dev, irq->ring_nr);
14502 		if (err) {
14503 			netdev_err(dev, "RX queue restart failed: err=%d\n",
14504 				   err);
14505 			continue;
14506 		}
14507 
14508 		irq->tag = tag;
14509 	}
14510 
14511 unlock:
14512 	bnxt_unlock_sp(bp);
14513 }
14514 
14515 /* Only called from bnxt_sp_task() */
14516 static void bnxt_reset(struct bnxt *bp, bool silent)
14517 {
14518 	bnxt_rtnl_lock_sp(bp);
14519 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
14520 		bnxt_reset_task(bp, silent);
14521 	bnxt_rtnl_unlock_sp(bp);
14522 }
14523 
14524 /* Only called from bnxt_sp_task() */
14525 static void bnxt_rx_ring_reset(struct bnxt *bp)
14526 {
14527 	int i;
14528 
14529 	bnxt_rtnl_lock_sp(bp);
14530 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14531 		bnxt_rtnl_unlock_sp(bp);
14532 		return;
14533 	}
14534 	/* Disable and flush TPA before resetting the RX ring */
14535 	if (bp->flags & BNXT_FLAG_TPA)
14536 		bnxt_set_tpa(bp, false);
14537 	for (i = 0; i < bp->rx_nr_rings; i++) {
14538 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
14539 		struct bnxt_cp_ring_info *cpr;
14540 		int rc;
14541 
14542 		if (!rxr->bnapi->in_reset)
14543 			continue;
14544 
14545 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
14546 		if (rc) {
14547 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
14548 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
14549 			else
14550 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
14551 					    rc);
14552 			bnxt_reset_task(bp, true);
14553 			break;
14554 		}
14555 		bnxt_free_one_rx_ring_skbs(bp, rxr);
14556 		rxr->rx_prod = 0;
14557 		rxr->rx_agg_prod = 0;
14558 		rxr->rx_sw_agg_prod = 0;
14559 		rxr->rx_next_cons = 0;
14560 		rxr->bnapi->in_reset = false;
14561 		bnxt_alloc_one_rx_ring(bp, i);
14562 		cpr = &rxr->bnapi->cp_ring;
14563 		cpr->sw_stats->rx.rx_resets++;
14564 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
14565 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
14566 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
14567 	}
14568 	if (bp->flags & BNXT_FLAG_TPA)
14569 		bnxt_set_tpa(bp, true);
14570 	bnxt_rtnl_unlock_sp(bp);
14571 }
14572 
14573 static void bnxt_fw_fatal_close(struct bnxt *bp)
14574 {
14575 	bnxt_tx_disable(bp);
14576 	bnxt_disable_napi(bp);
14577 	bnxt_disable_int_sync(bp);
14578 	bnxt_free_irq(bp);
14579 	bnxt_clear_int_mode(bp);
14580 	pci_disable_device(bp->pdev);
14581 }
14582 
14583 static void bnxt_fw_reset_close(struct bnxt *bp)
14584 {
14585 	/* When firmware is in fatal state, quiesce device and disable
14586 	 * bus master to prevent any potential bad DMAs before freeing
14587 	 * kernel memory.
14588 	 */
14589 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
14590 		u16 val = 0;
14591 
14592 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14593 		if (val == 0xffff)
14594 			bp->fw_reset_min_dsecs = 0;
14595 		bnxt_fw_fatal_close(bp);
14596 	}
14597 	__bnxt_close_nic(bp, true, false);
14598 	bnxt_vf_reps_free(bp);
14599 	bnxt_clear_int_mode(bp);
14600 	bnxt_hwrm_func_drv_unrgtr(bp);
14601 	if (pci_is_enabled(bp->pdev))
14602 		pci_disable_device(bp->pdev);
14603 	bnxt_free_ctx_mem(bp, false);
14604 }
14605 
14606 static bool is_bnxt_fw_ok(struct bnxt *bp)
14607 {
14608 	struct bnxt_fw_health *fw_health = bp->fw_health;
14609 	bool no_heartbeat = false, has_reset = false;
14610 	u32 val;
14611 
14612 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14613 	if (val == fw_health->last_fw_heartbeat)
14614 		no_heartbeat = true;
14615 
14616 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14617 	if (val != fw_health->last_fw_reset_cnt)
14618 		has_reset = true;
14619 
14620 	if (!no_heartbeat && has_reset)
14621 		return true;
14622 
14623 	return false;
14624 }
14625 
14626 /* netdev instance lock is acquired before calling this function */
14627 static void bnxt_force_fw_reset(struct bnxt *bp)
14628 {
14629 	struct bnxt_fw_health *fw_health = bp->fw_health;
14630 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14631 	u32 wait_dsecs;
14632 
14633 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
14634 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14635 		return;
14636 
14637 	/* we have to serialize with bnxt_refclk_read()*/
14638 	if (ptp) {
14639 		unsigned long flags;
14640 
14641 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
14642 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14643 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14644 	} else {
14645 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14646 	}
14647 	bnxt_fw_reset_close(bp);
14648 	wait_dsecs = fw_health->master_func_wait_dsecs;
14649 	if (fw_health->primary) {
14650 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
14651 			wait_dsecs = 0;
14652 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14653 	} else {
14654 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
14655 		wait_dsecs = fw_health->normal_func_wait_dsecs;
14656 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14657 	}
14658 
14659 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
14660 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
14661 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14662 }
14663 
14664 void bnxt_fw_exception(struct bnxt *bp)
14665 {
14666 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
14667 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14668 	bnxt_ulp_stop(bp);
14669 	bnxt_lock_sp(bp);
14670 	bnxt_force_fw_reset(bp);
14671 	bnxt_unlock_sp(bp);
14672 }
14673 
14674 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
14675  * < 0 on error.
14676  */
14677 static int bnxt_get_registered_vfs(struct bnxt *bp)
14678 {
14679 #ifdef CONFIG_BNXT_SRIOV
14680 	int rc;
14681 
14682 	if (!BNXT_PF(bp))
14683 		return 0;
14684 
14685 	rc = bnxt_hwrm_func_qcfg(bp);
14686 	if (rc) {
14687 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
14688 		return rc;
14689 	}
14690 	if (bp->pf.registered_vfs)
14691 		return bp->pf.registered_vfs;
14692 	if (bp->sriov_cfg)
14693 		return 1;
14694 #endif
14695 	return 0;
14696 }
14697 
14698 void bnxt_fw_reset(struct bnxt *bp)
14699 {
14700 	bnxt_ulp_stop(bp);
14701 	bnxt_lock_sp(bp);
14702 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
14703 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14704 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14705 		int n = 0, tmo;
14706 
14707 		/* we have to serialize with bnxt_refclk_read()*/
14708 		if (ptp) {
14709 			unsigned long flags;
14710 
14711 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
14712 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14713 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14714 		} else {
14715 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14716 		}
14717 		if (bp->pf.active_vfs &&
14718 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
14719 			n = bnxt_get_registered_vfs(bp);
14720 		if (n < 0) {
14721 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
14722 				   n);
14723 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14724 			netif_close(bp->dev);
14725 			goto fw_reset_exit;
14726 		} else if (n > 0) {
14727 			u16 vf_tmo_dsecs = n * 10;
14728 
14729 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
14730 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
14731 			bp->fw_reset_state =
14732 				BNXT_FW_RESET_STATE_POLL_VF;
14733 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14734 			goto fw_reset_exit;
14735 		}
14736 		bnxt_fw_reset_close(bp);
14737 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14738 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14739 			tmo = HZ / 10;
14740 		} else {
14741 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14742 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14743 		}
14744 		bnxt_queue_fw_reset_work(bp, tmo);
14745 	}
14746 fw_reset_exit:
14747 	bnxt_unlock_sp(bp);
14748 }
14749 
14750 static void bnxt_chk_missed_irq(struct bnxt *bp)
14751 {
14752 	int i;
14753 
14754 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14755 		return;
14756 
14757 	for (i = 0; i < bp->cp_nr_rings; i++) {
14758 		struct bnxt_napi *bnapi = bp->bnapi[i];
14759 		struct bnxt_cp_ring_info *cpr;
14760 		u32 fw_ring_id;
14761 		int j;
14762 
14763 		if (!bnapi)
14764 			continue;
14765 
14766 		cpr = &bnapi->cp_ring;
14767 		for (j = 0; j < cpr->cp_ring_count; j++) {
14768 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
14769 			u32 val[2];
14770 
14771 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
14772 				continue;
14773 
14774 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
14775 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
14776 				continue;
14777 			}
14778 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
14779 			bnxt_dbg_hwrm_ring_info_get(bp,
14780 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
14781 				fw_ring_id, &val[0], &val[1]);
14782 			cpr->sw_stats->cmn.missed_irqs++;
14783 		}
14784 	}
14785 }
14786 
14787 static void bnxt_cfg_ntp_filters(struct bnxt *);
14788 
14789 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
14790 {
14791 	struct bnxt_link_info *link_info = &bp->link_info;
14792 
14793 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
14794 		link_info->autoneg = BNXT_AUTONEG_SPEED;
14795 		if (bp->hwrm_spec_code >= 0x10201) {
14796 			if (link_info->auto_pause_setting &
14797 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
14798 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14799 		} else {
14800 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14801 		}
14802 		bnxt_set_auto_speed(link_info);
14803 	} else {
14804 		bnxt_set_force_speed(link_info);
14805 		link_info->req_duplex = link_info->duplex_setting;
14806 	}
14807 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14808 		link_info->req_flow_ctrl =
14809 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14810 	else
14811 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14812 }
14813 
14814 static void bnxt_fw_echo_reply(struct bnxt *bp)
14815 {
14816 	struct bnxt_fw_health *fw_health = bp->fw_health;
14817 	struct hwrm_func_echo_response_input *req;
14818 	int rc;
14819 
14820 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14821 	if (rc)
14822 		return;
14823 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14824 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14825 	hwrm_req_send(bp, req);
14826 }
14827 
14828 static void bnxt_ulp_restart(struct bnxt *bp)
14829 {
14830 	bnxt_ulp_stop(bp);
14831 	bnxt_ulp_start(bp);
14832 }
14833 
14834 static void bnxt_sp_task(struct work_struct *work)
14835 {
14836 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14837 
14838 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14839 	smp_mb__after_atomic();
14840 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14841 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14842 		return;
14843 	}
14844 
14845 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14846 		bnxt_ulp_restart(bp);
14847 		bnxt_reenable_sriov(bp);
14848 	}
14849 
14850 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14851 		bnxt_cfg_ntp_filters(bp);
14852 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14853 		bnxt_hwrm_exec_fwd_req(bp);
14854 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14855 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14856 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14857 		bnxt_hwrm_port_qstats(bp, 0);
14858 		bnxt_hwrm_port_qstats_ext(bp, 0);
14859 		spin_lock(&bp->stats_lock);
14860 		bnxt_accumulate_all_stats(bp);
14861 		bp->stats_updated_jiffies = jiffies;
14862 		spin_unlock(&bp->stats_lock);
14863 	}
14864 
14865 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14866 		int rc;
14867 
14868 		mutex_lock(&bp->link_lock);
14869 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14870 				       &bp->sp_event))
14871 			bnxt_hwrm_phy_qcaps(bp);
14872 
14873 		rc = bnxt_update_link(bp, true);
14874 		if (rc)
14875 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14876 				   rc);
14877 
14878 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14879 				       &bp->sp_event))
14880 			bnxt_init_ethtool_link_settings(bp);
14881 		mutex_unlock(&bp->link_lock);
14882 	}
14883 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14884 		int rc;
14885 
14886 		mutex_lock(&bp->link_lock);
14887 		rc = bnxt_update_phy_setting(bp);
14888 		mutex_unlock(&bp->link_lock);
14889 		if (rc) {
14890 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14891 		} else {
14892 			bp->link_info.phy_retry = false;
14893 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14894 		}
14895 	}
14896 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14897 		mutex_lock(&bp->link_lock);
14898 		bnxt_get_port_module_status(bp);
14899 		mutex_unlock(&bp->link_lock);
14900 	}
14901 
14902 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14903 		bnxt_tc_flow_stats_work(bp);
14904 
14905 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14906 		bnxt_chk_missed_irq(bp);
14907 
14908 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14909 		bnxt_fw_echo_reply(bp);
14910 
14911 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14912 		bnxt_hwmon_notify_event(bp);
14913 
14914 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14915 	 * must be the last functions to be called before exiting.
14916 	 */
14917 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14918 		bnxt_reset(bp, false);
14919 
14920 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14921 		bnxt_reset(bp, true);
14922 
14923 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14924 		bnxt_rx_ring_reset(bp);
14925 
14926 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14927 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14928 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14929 			bnxt_devlink_health_fw_report(bp);
14930 		else
14931 			bnxt_fw_reset(bp);
14932 	}
14933 
14934 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14935 		if (!is_bnxt_fw_ok(bp))
14936 			bnxt_devlink_health_fw_report(bp);
14937 	}
14938 
14939 	if (test_and_clear_bit(BNXT_TPH_UPDATE_SP_EVENT, &bp->sp_event))
14940 		bnxt_tph_update(bp);
14941 
14942 	smp_mb__before_atomic();
14943 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14944 }
14945 
14946 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14947 				int *max_cp);
14948 
14949 /* Under netdev instance lock */
14950 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14951 		     int tx_xdp)
14952 {
14953 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14954 	struct bnxt_hw_rings hwr = {0};
14955 	int rx_rings = rx;
14956 	int rc;
14957 
14958 	if (tcs)
14959 		tx_sets = tcs;
14960 
14961 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
14962 
14963 	if (max_rx < rx_rings)
14964 		return -ENOMEM;
14965 
14966 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14967 		rx_rings <<= 1;
14968 
14969 	hwr.rx = rx_rings;
14970 	hwr.tx = tx * tx_sets + tx_xdp;
14971 	if (max_tx < hwr.tx)
14972 		return -ENOMEM;
14973 
14974 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
14975 
14976 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
14977 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
14978 	if (max_cp < hwr.cp)
14979 		return -ENOMEM;
14980 	hwr.stat = hwr.cp;
14981 	if (BNXT_NEW_RM(bp)) {
14982 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
14983 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
14984 		hwr.grp = rx;
14985 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14986 	}
14987 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
14988 		hwr.cp_p5 = hwr.tx + rx;
14989 	rc = bnxt_hwrm_check_rings(bp, &hwr);
14990 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
14991 		if (!bnxt_ulp_registered(bp->edev[BNXT_AUXDEV_RDMA])) {
14992 			hwr.cp += bnxt_get_ulp_msix_num(bp);
14993 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
14994 		}
14995 		if (hwr.cp > bp->total_irqs) {
14996 			int total_msix = bnxt_change_msix(bp, hwr.cp);
14997 
14998 			if (total_msix < hwr.cp) {
14999 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
15000 					    hwr.cp, total_msix);
15001 				rc = -ENOSPC;
15002 			}
15003 		}
15004 	}
15005 	return rc;
15006 }
15007 
15008 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
15009 {
15010 	if (bp->bar2) {
15011 		pci_iounmap(pdev, bp->bar2);
15012 		bp->bar2 = NULL;
15013 	}
15014 
15015 	if (bp->bar1) {
15016 		pci_iounmap(pdev, bp->bar1);
15017 		bp->bar1 = NULL;
15018 	}
15019 
15020 	if (bp->bar0) {
15021 		pci_iounmap(pdev, bp->bar0);
15022 		bp->bar0 = NULL;
15023 	}
15024 }
15025 
15026 static void bnxt_cleanup_pci(struct bnxt *bp)
15027 {
15028 	bnxt_unmap_bars(bp, bp->pdev);
15029 	pci_release_regions(bp->pdev);
15030 	if (pci_is_enabled(bp->pdev))
15031 		pci_disable_device(bp->pdev);
15032 }
15033 
15034 static void bnxt_init_dflt_coal(struct bnxt *bp)
15035 {
15036 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
15037 	struct bnxt_coal *coal;
15038 	u16 flags = 0;
15039 
15040 	if (coal_cap->cmpl_params &
15041 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
15042 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
15043 
15044 	/* Tick values in micro seconds.
15045 	 * 1 coal_buf x bufs_per_record = 1 completion record.
15046 	 */
15047 	coal = &bp->rx_coal;
15048 	coal->coal_ticks = 10;
15049 	coal->coal_bufs = 30;
15050 	coal->coal_ticks_irq = 1;
15051 	coal->coal_bufs_irq = 2;
15052 	coal->idle_thresh = 50;
15053 	coal->bufs_per_record = 2;
15054 	coal->budget = 64;		/* NAPI budget */
15055 	coal->flags = flags;
15056 
15057 	coal = &bp->tx_coal;
15058 	coal->coal_ticks = 28;
15059 	coal->coal_bufs = 30;
15060 	coal->coal_ticks_irq = 2;
15061 	coal->coal_bufs_irq = 2;
15062 	coal->bufs_per_record = 1;
15063 	coal->flags = flags;
15064 
15065 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
15066 }
15067 
15068 /* FW that pre-reserves 1 VNIC per function */
15069 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
15070 {
15071 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
15072 
15073 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
15074 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
15075 		return true;
15076 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
15077 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
15078 		return true;
15079 	return false;
15080 }
15081 
15082 static void bnxt_hwrm_pfcwd_qcaps(struct bnxt *bp)
15083 {
15084 	struct hwrm_queue_pfcwd_timeout_qcaps_output *resp;
15085 	struct hwrm_queue_pfcwd_timeout_qcaps_input *req;
15086 	int rc;
15087 
15088 	bp->max_pfcwd_tmo_ms = 0;
15089 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_PFCWD_TIMEOUT_QCAPS);
15090 	if (rc)
15091 		return;
15092 	resp = hwrm_req_hold(bp, req);
15093 	rc = hwrm_req_send_silent(bp, req);
15094 	if (!rc)
15095 		bp->max_pfcwd_tmo_ms = le16_to_cpu(resp->max_pfcwd_timeout);
15096 	hwrm_req_drop(bp, req);
15097 }
15098 
15099 static int bnxt_fw_init_one_p1(struct bnxt *bp)
15100 {
15101 	int rc;
15102 
15103 	bp->fw_cap = 0;
15104 	rc = bnxt_hwrm_ver_get(bp);
15105 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
15106 	 * so wait before continuing with recovery.
15107 	 */
15108 	if (rc)
15109 		msleep(100);
15110 	bnxt_try_map_fw_health_reg(bp);
15111 	if (rc) {
15112 		rc = bnxt_try_recover_fw(bp);
15113 		if (rc)
15114 			return rc;
15115 		rc = bnxt_hwrm_ver_get(bp);
15116 		if (rc)
15117 			return rc;
15118 	}
15119 
15120 	bnxt_nvm_cfg_ver_get(bp);
15121 
15122 	rc = bnxt_hwrm_func_reset(bp);
15123 	if (rc)
15124 		return -ENODEV;
15125 
15126 	bnxt_hwrm_fw_set_time(bp);
15127 	return 0;
15128 }
15129 
15130 static int bnxt_fw_init_one_p2(struct bnxt *bp)
15131 {
15132 	int rc;
15133 
15134 	/* Get the MAX capabilities for this function */
15135 	rc = bnxt_hwrm_func_qcaps(bp);
15136 	if (rc) {
15137 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
15138 			   rc);
15139 		return -ENODEV;
15140 	}
15141 
15142 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
15143 	if (rc)
15144 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
15145 			    rc);
15146 
15147 	if (bnxt_alloc_fw_health(bp)) {
15148 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
15149 	} else {
15150 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
15151 		if (rc)
15152 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
15153 				    rc);
15154 	}
15155 
15156 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
15157 	if (rc)
15158 		return -ENODEV;
15159 
15160 	rc = bnxt_alloc_crash_dump_mem(bp);
15161 	if (rc)
15162 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
15163 			    rc);
15164 	if (!rc) {
15165 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
15166 		if (rc) {
15167 			bnxt_free_crash_dump_mem(bp);
15168 			netdev_warn(bp->dev,
15169 				    "hwrm crash dump mem failure rc: %d\n", rc);
15170 		}
15171 	}
15172 
15173 	if (bnxt_fw_pre_resv_vnics(bp))
15174 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
15175 
15176 	bnxt_hwrm_pfcwd_qcaps(bp);
15177 	bnxt_hwrm_func_qcfg(bp);
15178 	bnxt_hwrm_vnic_qcaps(bp);
15179 	bnxt_hwrm_port_led_qcaps(bp);
15180 	bnxt_ethtool_init(bp);
15181 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
15182 		__bnxt_hwrm_ptp_qcfg(bp);
15183 	bnxt_dcb_init(bp);
15184 	bnxt_hwmon_init(bp);
15185 	return 0;
15186 }
15187 
15188 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
15189 {
15190 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
15191 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
15192 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
15193 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
15194 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
15195 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
15196 		bp->rss_hash_delta = bp->rss_hash_cfg;
15197 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
15198 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
15199 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
15200 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
15201 	}
15202 }
15203 
15204 static void bnxt_set_dflt_rfs(struct bnxt *bp)
15205 {
15206 	struct net_device *dev = bp->dev;
15207 
15208 	dev->hw_features &= ~NETIF_F_NTUPLE;
15209 	dev->features &= ~NETIF_F_NTUPLE;
15210 	bp->flags &= ~BNXT_FLAG_RFS;
15211 	if (bnxt_rfs_supported(bp)) {
15212 		dev->hw_features |= NETIF_F_NTUPLE;
15213 		if (bnxt_rfs_capable(bp, false)) {
15214 			bp->flags |= BNXT_FLAG_RFS;
15215 			dev->features |= NETIF_F_NTUPLE;
15216 		}
15217 	}
15218 }
15219 
15220 static void bnxt_fw_init_one_p3(struct bnxt *bp)
15221 {
15222 	struct pci_dev *pdev = bp->pdev;
15223 
15224 	bnxt_set_dflt_rss_hash_type(bp);
15225 	bnxt_set_dflt_rfs(bp);
15226 
15227 	bnxt_get_wol_settings(bp);
15228 	if (bp->flags & BNXT_FLAG_WOL_CAP)
15229 		device_set_wakeup_enable(&pdev->dev, bp->wol);
15230 	else
15231 		device_set_wakeup_capable(&pdev->dev, false);
15232 
15233 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
15234 	bnxt_hwrm_coal_params_qcaps(bp);
15235 }
15236 
15237 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
15238 
15239 int bnxt_fw_init_one(struct bnxt *bp)
15240 {
15241 	int rc;
15242 
15243 	rc = bnxt_fw_init_one_p1(bp);
15244 	if (rc) {
15245 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
15246 		return rc;
15247 	}
15248 	rc = bnxt_fw_init_one_p2(bp);
15249 	if (rc) {
15250 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
15251 		return rc;
15252 	}
15253 	rc = bnxt_probe_phy(bp, false);
15254 	if (rc)
15255 		return rc;
15256 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
15257 	if (rc)
15258 		return rc;
15259 
15260 	bnxt_fw_init_one_p3(bp);
15261 	return 0;
15262 }
15263 
15264 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
15265 {
15266 	struct bnxt_fw_health *fw_health = bp->fw_health;
15267 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
15268 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
15269 	u32 reg_type, reg_off, delay_msecs;
15270 
15271 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
15272 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
15273 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
15274 	switch (reg_type) {
15275 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
15276 		pci_write_config_dword(bp->pdev, reg_off, val);
15277 		break;
15278 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
15279 		writel(reg_off & BNXT_GRC_BASE_MASK,
15280 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
15281 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
15282 		fallthrough;
15283 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
15284 		writel(val, bp->bar0 + reg_off);
15285 		break;
15286 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
15287 		writel(val, bp->bar1 + reg_off);
15288 		break;
15289 	}
15290 	if (delay_msecs) {
15291 		pci_read_config_dword(bp->pdev, 0, &val);
15292 		msleep(delay_msecs);
15293 	}
15294 }
15295 
15296 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
15297 {
15298 	struct hwrm_func_qcfg_output *resp;
15299 	struct hwrm_func_qcfg_input *req;
15300 	bool result = true; /* firmware will enforce if unknown */
15301 
15302 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
15303 		return result;
15304 
15305 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
15306 		return result;
15307 
15308 	req->fid = cpu_to_le16(0xffff);
15309 	resp = hwrm_req_hold(bp, req);
15310 	if (!hwrm_req_send(bp, req))
15311 		result = !!(le16_to_cpu(resp->flags) &
15312 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
15313 	hwrm_req_drop(bp, req);
15314 	return result;
15315 }
15316 
15317 static void bnxt_reset_all(struct bnxt *bp)
15318 {
15319 	struct bnxt_fw_health *fw_health = bp->fw_health;
15320 	int i, rc;
15321 
15322 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15323 		bnxt_fw_reset_via_optee(bp);
15324 		bp->fw_reset_timestamp = jiffies;
15325 		return;
15326 	}
15327 
15328 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
15329 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
15330 			bnxt_fw_reset_writel(bp, i);
15331 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
15332 		struct hwrm_fw_reset_input *req;
15333 
15334 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
15335 		if (!rc) {
15336 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
15337 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
15338 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
15339 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
15340 			rc = hwrm_req_send(bp, req);
15341 		}
15342 		if (rc != -ENODEV)
15343 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
15344 	}
15345 	bp->fw_reset_timestamp = jiffies;
15346 }
15347 
15348 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
15349 {
15350 	return time_after(jiffies, bp->fw_reset_timestamp +
15351 			  (bp->fw_reset_max_dsecs * HZ / 10));
15352 }
15353 
15354 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
15355 {
15356 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15357 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
15358 		bnxt_dl_health_fw_status_update(bp, false);
15359 	bp->fw_reset_state = BNXT_FW_RESET_STATE_ABORT;
15360 	netif_close(bp->dev);
15361 }
15362 
15363 static void bnxt_fw_reset_task(struct work_struct *work)
15364 {
15365 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
15366 	int rc = 0;
15367 
15368 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
15369 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
15370 		return;
15371 	}
15372 
15373 	switch (bp->fw_reset_state) {
15374 	case BNXT_FW_RESET_STATE_POLL_VF: {
15375 		int n = bnxt_get_registered_vfs(bp);
15376 		int tmo;
15377 
15378 		if (n < 0) {
15379 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
15380 				   n, jiffies_to_msecs(jiffies -
15381 				   bp->fw_reset_timestamp));
15382 			goto fw_reset_abort;
15383 		} else if (n > 0) {
15384 			if (bnxt_fw_reset_timeout(bp)) {
15385 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15386 				bp->fw_reset_state = 0;
15387 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
15388 					   n);
15389 				goto ulp_start;
15390 			}
15391 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15392 			return;
15393 		}
15394 		bp->fw_reset_timestamp = jiffies;
15395 		netdev_lock(bp->dev);
15396 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
15397 			bnxt_fw_reset_abort(bp, rc);
15398 			netdev_unlock(bp->dev);
15399 			goto ulp_start;
15400 		}
15401 		bnxt_fw_reset_close(bp);
15402 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15403 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
15404 			tmo = HZ / 10;
15405 		} else {
15406 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15407 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
15408 		}
15409 		netdev_unlock(bp->dev);
15410 		bnxt_queue_fw_reset_work(bp, tmo);
15411 		return;
15412 	}
15413 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
15414 		u32 val;
15415 
15416 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15417 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
15418 		    !bnxt_fw_reset_timeout(bp)) {
15419 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15420 			return;
15421 		}
15422 
15423 		if (!bp->fw_health->primary) {
15424 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
15425 
15426 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15427 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
15428 			return;
15429 		}
15430 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
15431 	}
15432 		fallthrough;
15433 	case BNXT_FW_RESET_STATE_RESET_FW:
15434 		bnxt_reset_all(bp);
15435 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15436 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
15437 		return;
15438 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
15439 		bnxt_inv_fw_health_reg(bp);
15440 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
15441 		    !bp->fw_reset_min_dsecs) {
15442 			u16 val;
15443 
15444 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
15445 			if (val == 0xffff) {
15446 				if (bnxt_fw_reset_timeout(bp)) {
15447 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
15448 					rc = -ETIMEDOUT;
15449 					goto fw_reset_abort;
15450 				}
15451 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
15452 				return;
15453 			}
15454 		}
15455 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
15456 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
15457 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
15458 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
15459 			bnxt_dl_remote_reload(bp);
15460 		if (pci_enable_device(bp->pdev)) {
15461 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
15462 			rc = -ENODEV;
15463 			goto fw_reset_abort;
15464 		}
15465 		pci_set_master(bp->pdev);
15466 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
15467 		fallthrough;
15468 	case BNXT_FW_RESET_STATE_POLL_FW:
15469 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
15470 		rc = bnxt_hwrm_poll(bp);
15471 		if (rc) {
15472 			if (bnxt_fw_reset_timeout(bp)) {
15473 				netdev_err(bp->dev, "Firmware reset aborted\n");
15474 				goto fw_reset_abort_status;
15475 			}
15476 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15477 			return;
15478 		}
15479 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
15480 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
15481 		fallthrough;
15482 	case BNXT_FW_RESET_STATE_OPENING:
15483 		while (!rtnl_trylock()) {
15484 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15485 			return;
15486 		}
15487 		netdev_lock(bp->dev);
15488 		rc = bnxt_open(bp->dev);
15489 		if (rc) {
15490 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
15491 			bnxt_fw_reset_abort(bp, rc);
15492 			netdev_unlock(bp->dev);
15493 			rtnl_unlock();
15494 			goto ulp_start;
15495 		}
15496 
15497 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
15498 		    bp->fw_health->enabled) {
15499 			bp->fw_health->last_fw_reset_cnt =
15500 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
15501 		}
15502 		bp->fw_reset_state = 0;
15503 		/* Make sure fw_reset_state is 0 before clearing the flag */
15504 		smp_mb__before_atomic();
15505 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15506 		bnxt_ptp_reapply_pps(bp);
15507 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
15508 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
15509 			bnxt_dl_health_fw_recovery_done(bp);
15510 			bnxt_dl_health_fw_status_update(bp, true);
15511 		}
15512 		netdev_unlock(bp->dev);
15513 		rtnl_unlock();
15514 		bnxt_ulp_start(bp);
15515 		bnxt_reenable_sriov(bp);
15516 		netdev_lock(bp->dev);
15517 		bnxt_vf_reps_alloc(bp);
15518 		bnxt_vf_reps_open(bp);
15519 		netdev_unlock(bp->dev);
15520 		break;
15521 	}
15522 	return;
15523 
15524 fw_reset_abort_status:
15525 	if (bp->fw_health->status_reliable ||
15526 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
15527 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15528 
15529 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
15530 	}
15531 fw_reset_abort:
15532 	netdev_lock(bp->dev);
15533 	bnxt_fw_reset_abort(bp, rc);
15534 	netdev_unlock(bp->dev);
15535 ulp_start:
15536 	if (!rc)
15537 		bnxt_ulp_start(bp);
15538 }
15539 
15540 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
15541 {
15542 	int rc;
15543 	struct bnxt *bp = netdev_priv(dev);
15544 
15545 	SET_NETDEV_DEV(dev, &pdev->dev);
15546 
15547 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
15548 	rc = pci_enable_device(pdev);
15549 	if (rc) {
15550 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
15551 		goto init_err;
15552 	}
15553 
15554 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
15555 		dev_err(&pdev->dev,
15556 			"Cannot find PCI device base address, aborting\n");
15557 		rc = -ENODEV;
15558 		goto init_err_disable;
15559 	}
15560 
15561 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
15562 	if (rc) {
15563 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
15564 		goto init_err_disable;
15565 	}
15566 
15567 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
15568 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
15569 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
15570 		rc = -EIO;
15571 		goto init_err_release;
15572 	}
15573 
15574 	pci_set_master(pdev);
15575 
15576 	bp->dev = dev;
15577 	bp->pdev = pdev;
15578 
15579 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
15580 	 * determines the BAR size.
15581 	 */
15582 	bp->bar0 = pci_ioremap_bar(pdev, 0);
15583 	if (!bp->bar0) {
15584 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
15585 		rc = -ENOMEM;
15586 		goto init_err_release;
15587 	}
15588 
15589 	bp->bar2 = pci_ioremap_bar(pdev, 4);
15590 	if (!bp->bar2) {
15591 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
15592 		rc = -ENOMEM;
15593 		goto init_err_release;
15594 	}
15595 
15596 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
15597 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
15598 
15599 	spin_lock_init(&bp->ntp_fltr_lock);
15600 	spin_lock_init(&bp->stats_lock);
15601 #if BITS_PER_LONG == 32
15602 	spin_lock_init(&bp->db_lock);
15603 #endif
15604 
15605 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
15606 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
15607 
15608 	timer_setup(&bp->timer, bnxt_timer, 0);
15609 	bp->current_interval = BNXT_TIMER_INTERVAL;
15610 
15611 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
15612 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
15613 
15614 	clear_bit(BNXT_STATE_OPEN, &bp->state);
15615 	return 0;
15616 
15617 init_err_release:
15618 	bnxt_unmap_bars(bp, pdev);
15619 	pci_release_regions(pdev);
15620 
15621 init_err_disable:
15622 	pci_disable_device(pdev);
15623 
15624 init_err:
15625 	return rc;
15626 }
15627 
15628 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
15629 {
15630 	struct sockaddr *addr = p;
15631 	struct bnxt *bp = netdev_priv(dev);
15632 	int rc = 0;
15633 
15634 	netdev_assert_locked(dev);
15635 
15636 	if (!is_valid_ether_addr(addr->sa_data))
15637 		return -EADDRNOTAVAIL;
15638 
15639 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
15640 		return 0;
15641 
15642 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
15643 	if (rc)
15644 		return rc;
15645 
15646 	eth_hw_addr_set(dev, addr->sa_data);
15647 	bnxt_clear_usr_fltrs(bp, true);
15648 	if (netif_running(dev)) {
15649 		bnxt_close_nic(bp, false, false);
15650 		rc = bnxt_open_nic(bp, false, false);
15651 	}
15652 
15653 	return rc;
15654 }
15655 
15656 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
15657 {
15658 	struct bnxt *bp = netdev_priv(dev);
15659 
15660 	netdev_assert_locked(dev);
15661 
15662 	if (netif_running(dev))
15663 		bnxt_close_nic(bp, true, false);
15664 
15665 	WRITE_ONCE(dev->mtu, new_mtu);
15666 
15667 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
15668 	 * program is attached.  We need to set the AGG rings settings and
15669 	 * rx_skb_func accordingly.
15670 	 */
15671 	if (READ_ONCE(bp->xdp_prog))
15672 		bnxt_set_rx_skb_mode(bp, true);
15673 
15674 	bnxt_set_ring_params(bp);
15675 
15676 	if (netif_running(dev))
15677 		return bnxt_open_nic(bp, true, false);
15678 
15679 	return 0;
15680 }
15681 
15682 void bnxt_set_cp_rings(struct bnxt *bp, bool sh)
15683 {
15684 	int tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
15685 
15686 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
15687 			       tx_cp + bp->rx_nr_rings;
15688 }
15689 
15690 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
15691 {
15692 	struct bnxt *bp = netdev_priv(dev);
15693 	bool sh = false;
15694 	int rc;
15695 
15696 	if (tc > bp->max_tc) {
15697 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
15698 			   tc, bp->max_tc);
15699 		return -EINVAL;
15700 	}
15701 
15702 	if (bp->num_tc == tc)
15703 		return 0;
15704 
15705 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
15706 		sh = true;
15707 
15708 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
15709 			      sh, tc, bp->tx_nr_rings_xdp);
15710 	if (rc)
15711 		return rc;
15712 
15713 	/* Needs to close the device and do hw resource re-allocations */
15714 	if (netif_running(bp->dev))
15715 		bnxt_close_nic(bp, true, false);
15716 
15717 	if (tc) {
15718 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
15719 		netdev_set_num_tc(dev, tc);
15720 		bp->num_tc = tc;
15721 	} else {
15722 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15723 		netdev_reset_tc(dev);
15724 		bp->num_tc = 0;
15725 	}
15726 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
15727 	bnxt_set_cp_rings(bp, sh);
15728 
15729 	if (netif_running(bp->dev))
15730 		return bnxt_open_nic(bp, true, false);
15731 
15732 	return 0;
15733 }
15734 
15735 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
15736 				  void *cb_priv)
15737 {
15738 	struct bnxt *bp = cb_priv;
15739 
15740 	if (!bnxt_tc_flower_enabled(bp) ||
15741 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
15742 		return -EOPNOTSUPP;
15743 
15744 	switch (type) {
15745 	case TC_SETUP_CLSFLOWER:
15746 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
15747 	default:
15748 		return -EOPNOTSUPP;
15749 	}
15750 }
15751 
15752 LIST_HEAD(bnxt_block_cb_list);
15753 
15754 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
15755 			 void *type_data)
15756 {
15757 	struct bnxt *bp = netdev_priv(dev);
15758 
15759 	switch (type) {
15760 	case TC_SETUP_BLOCK:
15761 		return flow_block_cb_setup_simple(type_data,
15762 						  &bnxt_block_cb_list,
15763 						  bnxt_setup_tc_block_cb,
15764 						  bp, bp, true);
15765 	case TC_SETUP_QDISC_MQPRIO: {
15766 		struct tc_mqprio_qopt *mqprio = type_data;
15767 
15768 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
15769 
15770 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
15771 	}
15772 	default:
15773 		return -EOPNOTSUPP;
15774 	}
15775 }
15776 
15777 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
15778 			    const struct sk_buff *skb)
15779 {
15780 	struct bnxt_vnic_info *vnic;
15781 
15782 	if (skb)
15783 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
15784 
15785 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
15786 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
15787 }
15788 
15789 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
15790 			   u32 idx)
15791 {
15792 	struct hlist_head *head;
15793 	int bit_id;
15794 
15795 	spin_lock_bh(&bp->ntp_fltr_lock);
15796 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
15797 	if (bit_id < 0) {
15798 		spin_unlock_bh(&bp->ntp_fltr_lock);
15799 		return -ENOMEM;
15800 	}
15801 
15802 	fltr->base.sw_id = (u16)bit_id;
15803 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
15804 	fltr->base.flags |= BNXT_ACT_RING_DST;
15805 	head = &bp->ntp_fltr_hash_tbl[idx];
15806 	hlist_add_head_rcu(&fltr->base.hash, head);
15807 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
15808 	bnxt_insert_usr_fltr(bp, &fltr->base);
15809 	bp->ntp_fltr_count++;
15810 	spin_unlock_bh(&bp->ntp_fltr_lock);
15811 	return 0;
15812 }
15813 
15814 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
15815 			    struct bnxt_ntuple_filter *f2)
15816 {
15817 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
15818 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
15819 	struct flow_keys *keys1 = &f1->fkeys;
15820 	struct flow_keys *keys2 = &f2->fkeys;
15821 
15822 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
15823 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
15824 		return false;
15825 
15826 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
15827 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
15828 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
15829 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
15830 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
15831 			return false;
15832 	} else {
15833 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
15834 				     &keys2->addrs.v6addrs.src) ||
15835 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
15836 				     &masks2->addrs.v6addrs.src) ||
15837 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15838 				     &keys2->addrs.v6addrs.dst) ||
15839 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
15840 				     &masks2->addrs.v6addrs.dst))
15841 			return false;
15842 	}
15843 
15844 	return keys1->ports.src == keys2->ports.src &&
15845 	       masks1->ports.src == masks2->ports.src &&
15846 	       keys1->ports.dst == keys2->ports.dst &&
15847 	       masks1->ports.dst == masks2->ports.dst &&
15848 	       keys1->control.flags == keys2->control.flags &&
15849 	       f1->l2_fltr == f2->l2_fltr;
15850 }
15851 
15852 struct bnxt_ntuple_filter *
15853 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15854 				struct bnxt_ntuple_filter *fltr, u32 idx)
15855 {
15856 	struct bnxt_ntuple_filter *f;
15857 	struct hlist_head *head;
15858 
15859 	head = &bp->ntp_fltr_hash_tbl[idx];
15860 	hlist_for_each_entry_rcu(f, head, base.hash) {
15861 		if (bnxt_fltr_match(f, fltr))
15862 			return f;
15863 	}
15864 	return NULL;
15865 }
15866 
15867 #ifdef CONFIG_RFS_ACCEL
15868 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15869 			      u16 rxq_index, u32 flow_id)
15870 {
15871 	struct bnxt *bp = netdev_priv(dev);
15872 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15873 	struct flow_keys *fkeys;
15874 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15875 	struct bnxt_l2_filter *l2_fltr;
15876 	int rc = 0, idx;
15877 	u32 flags;
15878 
15879 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15880 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15881 		atomic_inc(&l2_fltr->refcnt);
15882 	} else {
15883 		struct bnxt_l2_key key;
15884 
15885 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15886 		key.vlan = 0;
15887 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15888 		if (!l2_fltr)
15889 			return -EINVAL;
15890 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15891 			bnxt_del_l2_filter(bp, l2_fltr);
15892 			return -EINVAL;
15893 		}
15894 	}
15895 	new_fltr = kzalloc_obj(*new_fltr, GFP_ATOMIC);
15896 	if (!new_fltr) {
15897 		bnxt_del_l2_filter(bp, l2_fltr);
15898 		return -ENOMEM;
15899 	}
15900 
15901 	fkeys = &new_fltr->fkeys;
15902 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15903 		rc = -EPROTONOSUPPORT;
15904 		goto err_free;
15905 	}
15906 
15907 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15908 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15909 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15910 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15911 		rc = -EPROTONOSUPPORT;
15912 		goto err_free;
15913 	}
15914 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15915 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15916 		if (bp->hwrm_spec_code < 0x10601) {
15917 			rc = -EPROTONOSUPPORT;
15918 			goto err_free;
15919 		}
15920 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15921 	}
15922 	flags = fkeys->control.flags;
15923 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15924 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15925 		rc = -EPROTONOSUPPORT;
15926 		goto err_free;
15927 	}
15928 	new_fltr->l2_fltr = l2_fltr;
15929 
15930 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15931 	rcu_read_lock();
15932 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15933 	if (fltr) {
15934 		rc = fltr->base.sw_id;
15935 		rcu_read_unlock();
15936 		goto err_free;
15937 	}
15938 	rcu_read_unlock();
15939 
15940 	new_fltr->flow_id = flow_id;
15941 	new_fltr->base.rxq = rxq_index;
15942 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15943 	if (!rc) {
15944 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15945 		return new_fltr->base.sw_id;
15946 	}
15947 
15948 err_free:
15949 	bnxt_del_l2_filter(bp, l2_fltr);
15950 	kfree(new_fltr);
15951 	return rc;
15952 }
15953 #endif
15954 
15955 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
15956 {
15957 	spin_lock_bh(&bp->ntp_fltr_lock);
15958 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
15959 		spin_unlock_bh(&bp->ntp_fltr_lock);
15960 		return;
15961 	}
15962 	hlist_del_rcu(&fltr->base.hash);
15963 	bnxt_del_one_usr_fltr(bp, &fltr->base);
15964 	bp->ntp_fltr_count--;
15965 	spin_unlock_bh(&bp->ntp_fltr_lock);
15966 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
15967 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
15968 	kfree_rcu(fltr, base.rcu);
15969 }
15970 
15971 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
15972 {
15973 #ifdef CONFIG_RFS_ACCEL
15974 	int i;
15975 
15976 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
15977 		struct hlist_head *head;
15978 		struct hlist_node *tmp;
15979 		struct bnxt_ntuple_filter *fltr;
15980 		int rc;
15981 
15982 		head = &bp->ntp_fltr_hash_tbl[i];
15983 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
15984 			bool del = false;
15985 
15986 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
15987 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
15988 					continue;
15989 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
15990 							fltr->flow_id,
15991 							fltr->base.sw_id)) {
15992 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
15993 									 fltr);
15994 					del = true;
15995 				}
15996 			} else {
15997 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
15998 								       fltr);
15999 				if (rc)
16000 					del = true;
16001 				else
16002 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
16003 			}
16004 
16005 			if (del)
16006 				bnxt_del_ntp_filter(bp, fltr);
16007 		}
16008 	}
16009 #endif
16010 }
16011 
16012 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
16013 				    unsigned int entry, struct udp_tunnel_info *ti)
16014 {
16015 	struct bnxt *bp = netdev_priv(netdev);
16016 	unsigned int cmd;
16017 
16018 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
16019 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
16020 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
16021 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
16022 	else
16023 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
16024 
16025 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
16026 }
16027 
16028 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
16029 				      unsigned int entry, struct udp_tunnel_info *ti)
16030 {
16031 	struct bnxt *bp = netdev_priv(netdev);
16032 	unsigned int cmd;
16033 
16034 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
16035 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
16036 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
16037 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
16038 	else
16039 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
16040 
16041 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
16042 }
16043 
16044 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
16045 	.set_port	= bnxt_udp_tunnel_set_port,
16046 	.unset_port	= bnxt_udp_tunnel_unset_port,
16047 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
16048 	.tables		= {
16049 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
16050 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
16051 	},
16052 }, bnxt_udp_tunnels_p7 = {
16053 	.set_port	= bnxt_udp_tunnel_set_port,
16054 	.unset_port	= bnxt_udp_tunnel_unset_port,
16055 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
16056 	.tables		= {
16057 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
16058 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
16059 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
16060 	},
16061 };
16062 
16063 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
16064 			       struct net_device *dev, u32 filter_mask,
16065 			       int nlflags)
16066 {
16067 	struct bnxt *bp = netdev_priv(dev);
16068 
16069 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
16070 				       nlflags, filter_mask, NULL);
16071 }
16072 
16073 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
16074 			       u16 flags, struct netlink_ext_ack *extack)
16075 {
16076 	struct bnxt *bp = netdev_priv(dev);
16077 	struct nlattr *attr, *br_spec;
16078 	int rem, rc = 0;
16079 
16080 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
16081 		return -EOPNOTSUPP;
16082 
16083 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
16084 	if (!br_spec)
16085 		return -EINVAL;
16086 
16087 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
16088 		u16 mode;
16089 
16090 		mode = nla_get_u16(attr);
16091 		if (mode == bp->br_mode)
16092 			break;
16093 
16094 		rc = bnxt_hwrm_set_br_mode(bp, mode);
16095 		if (!rc)
16096 			bp->br_mode = mode;
16097 		break;
16098 	}
16099 	return rc;
16100 }
16101 
16102 int bnxt_get_port_parent_id(struct net_device *dev,
16103 			    struct netdev_phys_item_id *ppid)
16104 {
16105 	struct bnxt *bp = netdev_priv(dev);
16106 
16107 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
16108 		return -EOPNOTSUPP;
16109 
16110 	/* The PF and it's VF-reps only support the switchdev framework */
16111 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
16112 		return -EOPNOTSUPP;
16113 
16114 	ppid->id_len = sizeof(bp->dsn);
16115 	memcpy(ppid->id, bp->dsn, ppid->id_len);
16116 
16117 	return 0;
16118 }
16119 
16120 static const struct net_device_ops bnxt_netdev_ops = {
16121 	.ndo_open		= bnxt_open,
16122 	.ndo_start_xmit		= bnxt_start_xmit,
16123 	.ndo_stop		= bnxt_close,
16124 	.ndo_get_stats64	= bnxt_get_stats64,
16125 	.ndo_set_rx_mode_async	= bnxt_set_rx_mode,
16126 	.ndo_eth_ioctl		= bnxt_ioctl,
16127 	.ndo_validate_addr	= eth_validate_addr,
16128 	.ndo_set_mac_address	= bnxt_change_mac_addr,
16129 	.ndo_change_mtu		= bnxt_change_mtu,
16130 	.ndo_fix_features	= bnxt_fix_features,
16131 	.ndo_set_features	= bnxt_set_features,
16132 	.ndo_features_check	= bnxt_features_check,
16133 	.ndo_tx_timeout		= bnxt_tx_timeout,
16134 #ifdef CONFIG_BNXT_SRIOV
16135 	.ndo_get_vf_config	= bnxt_get_vf_config,
16136 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
16137 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
16138 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
16139 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
16140 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
16141 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
16142 #endif
16143 	.ndo_setup_tc           = bnxt_setup_tc,
16144 #ifdef CONFIG_RFS_ACCEL
16145 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
16146 #endif
16147 	.ndo_bpf		= bnxt_xdp,
16148 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
16149 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
16150 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
16151 	.ndo_hwtstamp_get	= bnxt_hwtstamp_get,
16152 	.ndo_hwtstamp_set	= bnxt_hwtstamp_set,
16153 };
16154 
16155 static const struct xdp_metadata_ops bnxt_xdp_metadata_ops = {
16156 	.xmo_rx_hash		= bnxt_xdp_rx_hash,
16157 };
16158 
16159 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
16160 				    struct netdev_queue_stats_rx *stats)
16161 {
16162 	struct bnxt *bp = netdev_priv(dev);
16163 	struct bnxt_cp_ring_info *cpr;
16164 	u64 *sw;
16165 
16166 	if (!bp->bnapi)
16167 		return;
16168 
16169 	bnxt_sync_ring_stats(bp);
16170 	cpr = &bp->bnapi[i]->cp_ring;
16171 	sw = cpr->stats.sw_stats;
16172 
16173 	stats->packets = 0;
16174 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
16175 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
16176 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
16177 
16178 	stats->bytes = 0;
16179 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
16180 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
16181 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
16182 
16183 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
16184 	stats->hw_gro_packets = cpr->sw_stats->rx.rx_hw_gro_packets;
16185 	stats->hw_gro_wire_packets = cpr->sw_stats->rx.rx_hw_gro_wire_packets;
16186 }
16187 
16188 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
16189 				    struct netdev_queue_stats_tx *stats)
16190 {
16191 	struct bnxt *bp = netdev_priv(dev);
16192 	struct bnxt_napi *bnapi;
16193 	u64 *sw;
16194 
16195 	if (!bp->tx_ring)
16196 		return;
16197 
16198 	bnxt_sync_ring_stats(bp);
16199 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
16200 	sw = bnapi->cp_ring.stats.sw_stats;
16201 
16202 	stats->packets = 0;
16203 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
16204 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
16205 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
16206 
16207 	stats->bytes = 0;
16208 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
16209 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
16210 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
16211 }
16212 
16213 static void bnxt_get_base_stats(struct net_device *dev,
16214 				struct netdev_queue_stats_rx *rx,
16215 				struct netdev_queue_stats_tx *tx)
16216 {
16217 	struct bnxt *bp = netdev_priv(dev);
16218 
16219 	rx->packets = bp->net_stats_prev.rx_packets;
16220 	rx->bytes = bp->net_stats_prev.rx_bytes;
16221 	rx->alloc_fail = bp->ring_drv_stats_prev.rx_total_oom_discards;
16222 	rx->hw_gro_packets = bp->ring_drv_stats_prev.rx_total_hw_gro_packets;
16223 	rx->hw_gro_wire_packets = bp->ring_drv_stats_prev.rx_total_hw_gro_wire_packets;
16224 
16225 	tx->packets = bp->net_stats_prev.tx_packets;
16226 	tx->bytes = bp->net_stats_prev.tx_bytes;
16227 }
16228 
16229 static const struct netdev_stat_ops bnxt_stat_ops = {
16230 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
16231 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
16232 	.get_base_stats		= bnxt_get_base_stats,
16233 };
16234 
16235 static void bnxt_queue_default_qcfg(struct net_device *dev,
16236 				    struct netdev_queue_config *qcfg)
16237 {
16238 	qcfg->rx_page_size = BNXT_RX_PAGE_SIZE;
16239 }
16240 
16241 static int bnxt_validate_qcfg(struct net_device *dev,
16242 			      struct netdev_queue_config *qcfg,
16243 			      struct netlink_ext_ack *extack)
16244 {
16245 	struct bnxt *bp = netdev_priv(dev);
16246 
16247 	/* Older chips need MSS calc so rx_page_size is not supported */
16248 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16249 	    qcfg->rx_page_size != BNXT_RX_PAGE_SIZE)
16250 		return -EINVAL;
16251 
16252 	if (!is_power_of_2(qcfg->rx_page_size))
16253 		return -ERANGE;
16254 
16255 	if (qcfg->rx_page_size < BNXT_RX_PAGE_SIZE ||
16256 	    qcfg->rx_page_size > BNXT_MAX_RX_PAGE_SIZE)
16257 		return -ERANGE;
16258 
16259 	return 0;
16260 }
16261 
16262 static int bnxt_queue_mem_alloc(struct net_device *dev,
16263 				struct netdev_queue_config *qcfg,
16264 				void *qmem, int idx)
16265 {
16266 	struct bnxt_rx_ring_info *rxr, *clone;
16267 	struct bnxt *bp = netdev_priv(dev);
16268 	struct bnxt_ring_struct *ring;
16269 	int rc;
16270 
16271 	if (!bp->rx_ring)
16272 		return -ENETDOWN;
16273 
16274 	rxr = &bp->rx_ring[idx];
16275 	clone = qmem;
16276 	memcpy(clone, rxr, sizeof(*rxr));
16277 	bnxt_init_rx_ring_struct(bp, clone);
16278 	bnxt_reset_rx_ring_struct(bp, clone);
16279 
16280 	clone->rx_prod = 0;
16281 	clone->rx_agg_prod = 0;
16282 	clone->rx_sw_agg_prod = 0;
16283 	clone->rx_next_cons = 0;
16284 	clone->need_head_pool = false;
16285 	clone->rx_page_size = qcfg->rx_page_size;
16286 	clone->rx_agg_bmap = NULL;
16287 
16288 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
16289 	if (rc)
16290 		return rc;
16291 
16292 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
16293 	if (rc < 0)
16294 		goto err_page_pool_destroy;
16295 
16296 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
16297 					MEM_TYPE_PAGE_POOL,
16298 					clone->page_pool);
16299 	if (rc)
16300 		goto err_rxq_info_unreg;
16301 
16302 	ring = &clone->rx_ring_struct;
16303 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
16304 	if (rc)
16305 		goto err_free_rx_ring;
16306 
16307 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
16308 		ring = &clone->rx_agg_ring_struct;
16309 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
16310 		if (rc)
16311 			goto err_free_rx_agg_ring;
16312 
16313 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
16314 		if (rc)
16315 			goto err_free_rx_agg_ring;
16316 	}
16317 
16318 	if (bp->flags & BNXT_FLAG_TPA) {
16319 		rc = bnxt_alloc_one_tpa_info(bp, clone);
16320 		if (rc)
16321 			goto err_free_tpa_info;
16322 	}
16323 
16324 	bnxt_init_one_rx_ring_rxbd(bp, clone);
16325 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
16326 
16327 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
16328 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16329 		bnxt_alloc_one_rx_ring_netmem(bp, clone, idx);
16330 	if (bp->flags & BNXT_FLAG_TPA)
16331 		bnxt_alloc_one_tpa_info_data(bp, clone);
16332 
16333 	return 0;
16334 
16335 err_free_tpa_info:
16336 	bnxt_free_one_tpa_info(bp, clone);
16337 err_free_rx_agg_ring:
16338 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
16339 	kfree(clone->rx_agg_bmap);
16340 	clone->rx_agg_bmap = NULL;
16341 err_free_rx_ring:
16342 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
16343 err_rxq_info_unreg:
16344 	xdp_rxq_info_unreg(&clone->xdp_rxq);
16345 err_page_pool_destroy:
16346 	page_pool_destroy(clone->page_pool);
16347 	page_pool_destroy(clone->head_pool);
16348 	clone->page_pool = NULL;
16349 	clone->head_pool = NULL;
16350 	return rc;
16351 }
16352 
16353 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
16354 {
16355 	struct bnxt_rx_ring_info *rxr = qmem;
16356 	struct bnxt *bp = netdev_priv(dev);
16357 	struct bnxt_ring_struct *ring;
16358 
16359 	bnxt_free_one_rx_ring_skbs(bp, rxr);
16360 	bnxt_free_one_tpa_info(bp, rxr);
16361 
16362 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
16363 
16364 	page_pool_destroy(rxr->page_pool);
16365 	page_pool_destroy(rxr->head_pool);
16366 	rxr->page_pool = NULL;
16367 	rxr->head_pool = NULL;
16368 
16369 	ring = &rxr->rx_ring_struct;
16370 	bnxt_free_ring(bp, &ring->ring_mem);
16371 
16372 	ring = &rxr->rx_agg_ring_struct;
16373 	bnxt_free_ring(bp, &ring->ring_mem);
16374 
16375 	kfree(rxr->rx_agg_bmap);
16376 	rxr->rx_agg_bmap = NULL;
16377 }
16378 
16379 static void bnxt_copy_rx_ring(struct bnxt *bp,
16380 			      struct bnxt_rx_ring_info *dst,
16381 			      struct bnxt_rx_ring_info *src)
16382 {
16383 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
16384 	struct bnxt_ring_struct *dst_ring, *src_ring;
16385 	int i;
16386 
16387 	dst_ring = &dst->rx_ring_struct;
16388 	dst_rmem = &dst_ring->ring_mem;
16389 	src_ring = &src->rx_ring_struct;
16390 	src_rmem = &src_ring->ring_mem;
16391 
16392 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
16393 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
16394 	WARN_ON(dst_rmem->flags != src_rmem->flags);
16395 	WARN_ON(dst_rmem->depth != src_rmem->depth);
16396 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
16397 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
16398 
16399 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
16400 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
16401 	*dst_rmem->vmem = *src_rmem->vmem;
16402 	for (i = 0; i < dst_rmem->nr_pages; i++) {
16403 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
16404 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
16405 	}
16406 
16407 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
16408 		return;
16409 
16410 	dst_ring = &dst->rx_agg_ring_struct;
16411 	dst_rmem = &dst_ring->ring_mem;
16412 	src_ring = &src->rx_agg_ring_struct;
16413 	src_rmem = &src_ring->ring_mem;
16414 
16415 	dst->rx_page_size = src->rx_page_size;
16416 
16417 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
16418 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
16419 	WARN_ON(dst_rmem->flags != src_rmem->flags);
16420 	WARN_ON(dst_rmem->depth != src_rmem->depth);
16421 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
16422 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
16423 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
16424 
16425 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
16426 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
16427 	*dst_rmem->vmem = *src_rmem->vmem;
16428 	for (i = 0; i < dst_rmem->nr_pages; i++) {
16429 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
16430 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
16431 	}
16432 
16433 	dst->rx_agg_bmap = src->rx_agg_bmap;
16434 }
16435 
16436 static int bnxt_queue_start(struct net_device *dev,
16437 			    struct netdev_queue_config *qcfg,
16438 			    void *qmem, int idx)
16439 {
16440 	struct bnxt *bp = netdev_priv(dev);
16441 	struct bnxt_rx_ring_info *rxr, *clone;
16442 	struct bnxt_cp_ring_info *cpr;
16443 	struct bnxt_vnic_info *vnic;
16444 	struct bnxt_napi *bnapi;
16445 	int i, rc;
16446 	u16 mru;
16447 
16448 	rxr = &bp->rx_ring[idx];
16449 	clone = qmem;
16450 
16451 	rxr->rx_prod = clone->rx_prod;
16452 	rxr->rx_agg_prod = clone->rx_agg_prod;
16453 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
16454 	rxr->rx_next_cons = clone->rx_next_cons;
16455 	rxr->rx_tpa = clone->rx_tpa;
16456 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
16457 	rxr->page_pool = clone->page_pool;
16458 	rxr->head_pool = clone->head_pool;
16459 	rxr->xdp_rxq = clone->xdp_rxq;
16460 	rxr->need_head_pool = clone->need_head_pool;
16461 
16462 	bnxt_copy_rx_ring(bp, rxr, clone);
16463 
16464 	bnapi = rxr->bnapi;
16465 	cpr = &bnapi->cp_ring;
16466 
16467 	/* All rings have been reserved and previously allocated.
16468 	 * Reallocating with the same parameters should never fail.
16469 	 */
16470 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
16471 	if (rc)
16472 		goto err_reset;
16473 
16474 	if (bp->tph_mode) {
16475 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
16476 		if (rc)
16477 			goto err_reset;
16478 	}
16479 
16480 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
16481 	if (rc)
16482 		goto err_reset;
16483 
16484 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
16485 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16486 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
16487 
16488 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
16489 		rc = bnxt_tx_queue_start(bp, idx);
16490 		if (rc)
16491 			goto err_reset;
16492 	}
16493 
16494 	bnxt_enable_rx_page_pool(rxr);
16495 	napi_enable_locked(&bnapi->napi);
16496 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16497 
16498 	mru = bp->dev->mtu + VLAN_ETH_HLEN;
16499 	for (i = 0; i < bp->nr_vnics; i++) {
16500 		vnic = &bp->vnic_info[i];
16501 
16502 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, idx);
16503 		if (rc)
16504 			return rc;
16505 	}
16506 	return bnxt_set_rss_ctx_vnic_mru(bp, mru, idx);
16507 
16508 err_reset:
16509 	netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n",
16510 		   rc);
16511 	napi_enable_locked(&bnapi->napi);
16512 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16513 	netif_close(dev);
16514 	return rc;
16515 }
16516 
16517 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
16518 {
16519 	struct bnxt *bp = netdev_priv(dev);
16520 	struct bnxt_rx_ring_info *rxr;
16521 	struct bnxt_cp_ring_info *cpr;
16522 	struct bnxt_vnic_info *vnic;
16523 	struct bnxt_napi *bnapi;
16524 	int i;
16525 
16526 	for (i = 0; i < bp->nr_vnics; i++) {
16527 		vnic = &bp->vnic_info[i];
16528 
16529 		bnxt_set_vnic_mru_p5(bp, vnic, 0, idx);
16530 	}
16531 	bnxt_set_rss_ctx_vnic_mru(bp, 0, idx);
16532 	/* Make sure NAPI sees that the VNIC is disabled */
16533 	synchronize_net();
16534 	rxr = &bp->rx_ring[idx];
16535 	bnapi = rxr->bnapi;
16536 	cpr = &bnapi->cp_ring;
16537 	cancel_work_sync(&cpr->dim.work);
16538 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
16539 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
16540 	page_pool_disable_direct_recycling(rxr->page_pool);
16541 	if (bnxt_separate_head_pool(rxr))
16542 		page_pool_disable_direct_recycling(rxr->head_pool);
16543 
16544 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
16545 		bnxt_tx_queue_stop(bp, idx);
16546 
16547 	/* Disable NAPI now after freeing the rings because HWRM_RING_FREE
16548 	 * completion is handled in NAPI to guarantee no more DMA on that ring
16549 	 * after seeing the completion.
16550 	 */
16551 	napi_disable_locked(&bnapi->napi);
16552 
16553 	if (bp->tph_mode) {
16554 		bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr);
16555 		bnxt_clear_one_cp_ring(bp, rxr->rx_cpr);
16556 	}
16557 	bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
16558 
16559 	memcpy(qmem, rxr, sizeof(*rxr));
16560 	bnxt_init_rx_ring_struct(bp, qmem);
16561 
16562 	return 0;
16563 }
16564 
16565 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
16566 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
16567 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
16568 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
16569 	.ndo_queue_start	= bnxt_queue_start,
16570 	.ndo_queue_stop		= bnxt_queue_stop,
16571 	.ndo_default_qcfg	= bnxt_queue_default_qcfg,
16572 	.ndo_validate_qcfg	= bnxt_validate_qcfg,
16573 	.supported_params	= QCFG_RX_PAGE_SIZE,
16574 };
16575 
16576 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops_unsupp = {
16577 	.ndo_default_qcfg	= bnxt_queue_default_qcfg,
16578 };
16579 
16580 static void bnxt_remove_one(struct pci_dev *pdev)
16581 {
16582 	struct net_device *dev = pci_get_drvdata(pdev);
16583 	struct bnxt *bp = netdev_priv(dev);
16584 
16585 	if (BNXT_PF(bp))
16586 		__bnxt_sriov_disable(bp);
16587 
16588 	bnxt_aux_devices_del(bp);
16589 
16590 	unregister_netdev(dev);
16591 	bnxt_ptp_clear(bp);
16592 
16593 	bnxt_aux_devices_uninit(bp);
16594 	bnxt_auxdev_id_free(bp, bp->auxdev_id);
16595 
16596 	bnxt_free_l2_filters(bp, true);
16597 	bnxt_free_ntp_fltrs(bp, true);
16598 	WARN_ON(bp->num_rss_ctx);
16599 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16600 	/* Flush any pending tasks */
16601 	cancel_work_sync(&bp->sp_task);
16602 	cancel_delayed_work_sync(&bp->fw_reset_task);
16603 	bp->sp_event = 0;
16604 
16605 	bnxt_dl_fw_reporters_destroy(bp);
16606 	bnxt_dl_unregister(bp);
16607 	bnxt_shutdown_tc(bp);
16608 
16609 	bnxt_clear_int_mode(bp);
16610 	bnxt_hwrm_func_drv_unrgtr(bp);
16611 	bnxt_free_hwrm_resources(bp);
16612 	bnxt_hwmon_uninit(bp);
16613 	bnxt_ethtool_free(bp);
16614 	bnxt_dcb_free(bp);
16615 	kfree(bp->ptp_cfg);
16616 	bp->ptp_cfg = NULL;
16617 	kfree(bp->fw_health);
16618 	bp->fw_health = NULL;
16619 	bnxt_cleanup_pci(bp);
16620 	bnxt_free_ctx_mem(bp, true);
16621 	bnxt_free_crash_dump_mem(bp);
16622 	kfree(bp->rss_indir_tbl);
16623 	bp->rss_indir_tbl = NULL;
16624 	bnxt_free_port_stats(bp);
16625 	free_netdev(dev);
16626 }
16627 
16628 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
16629 {
16630 	int rc = 0;
16631 	struct bnxt_link_info *link_info = &bp->link_info;
16632 
16633 	bp->phy_flags = 0;
16634 	rc = bnxt_hwrm_phy_qcaps(bp);
16635 	if (rc) {
16636 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
16637 			   rc);
16638 		return rc;
16639 	}
16640 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
16641 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
16642 	else
16643 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
16644 
16645 	bp->mac_flags = 0;
16646 	bnxt_hwrm_mac_qcaps(bp);
16647 
16648 	if (!fw_dflt)
16649 		return 0;
16650 
16651 	mutex_lock(&bp->link_lock);
16652 	rc = bnxt_update_link(bp, false);
16653 	if (rc) {
16654 		mutex_unlock(&bp->link_lock);
16655 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
16656 			   rc);
16657 		return rc;
16658 	}
16659 
16660 	/* Older firmware does not have supported_auto_speeds, so assume
16661 	 * that all supported speeds can be autonegotiated.
16662 	 */
16663 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
16664 		link_info->support_auto_speeds = link_info->support_speeds;
16665 
16666 	bnxt_init_ethtool_link_settings(bp);
16667 	mutex_unlock(&bp->link_lock);
16668 	return 0;
16669 }
16670 
16671 static int bnxt_get_max_irq(struct pci_dev *pdev)
16672 {
16673 	u16 ctrl;
16674 
16675 	if (!pdev->msix_cap)
16676 		return 1;
16677 
16678 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
16679 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
16680 }
16681 
16682 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16683 				int *max_cp)
16684 {
16685 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
16686 	int max_ring_grps = 0, max_irq;
16687 
16688 	*max_tx = hw_resc->max_tx_rings;
16689 	*max_rx = hw_resc->max_rx_rings;
16690 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
16691 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
16692 			bnxt_get_ulp_msix_num_in_use(bp),
16693 			hw_resc->max_stat_ctxs -
16694 			bnxt_get_ulp_stat_ctxs_in_use(bp));
16695 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
16696 		*max_cp = min_t(int, *max_cp, max_irq);
16697 	max_ring_grps = hw_resc->max_hw_ring_grps;
16698 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
16699 		*max_cp -= 1;
16700 		*max_rx -= 2;
16701 	}
16702 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16703 		*max_rx >>= 1;
16704 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
16705 		int rc;
16706 
16707 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
16708 		if (rc) {
16709 			*max_rx = 0;
16710 			*max_tx = 0;
16711 		}
16712 		/* On P5 chips, max_cp output param should be available NQs */
16713 		*max_cp = max_irq;
16714 	}
16715 	*max_rx = min_t(int, *max_rx, max_ring_grps);
16716 }
16717 
16718 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
16719 {
16720 	int rx, tx, cp;
16721 
16722 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
16723 	*max_rx = rx;
16724 	*max_tx = tx;
16725 	if (!rx || !tx || !cp)
16726 		return -ENOMEM;
16727 
16728 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
16729 }
16730 
16731 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16732 			       bool shared)
16733 {
16734 	int rc;
16735 
16736 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16737 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
16738 		/* Not enough rings, try disabling agg rings. */
16739 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
16740 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16741 		if (rc) {
16742 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
16743 			bp->flags |= BNXT_FLAG_AGG_RINGS;
16744 			return rc;
16745 		}
16746 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
16747 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16748 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16749 		bnxt_set_ring_params(bp);
16750 	}
16751 
16752 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
16753 		int max_cp, max_stat, max_irq;
16754 
16755 		/* Reserve minimum resources for RoCE */
16756 		max_cp = bnxt_get_max_func_cp_rings(bp);
16757 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
16758 		max_irq = bnxt_get_max_func_irqs(bp);
16759 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
16760 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
16761 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
16762 			return 0;
16763 
16764 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
16765 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
16766 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
16767 		max_cp = min_t(int, max_cp, max_irq);
16768 		max_cp = min_t(int, max_cp, max_stat);
16769 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
16770 		if (rc)
16771 			rc = 0;
16772 	}
16773 	return rc;
16774 }
16775 
16776 /* In initial default shared ring setting, each shared ring must have a
16777  * RX/TX ring pair.
16778  */
16779 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
16780 {
16781 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
16782 	bp->rx_nr_rings = bp->cp_nr_rings;
16783 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
16784 	bp->tx_nr_rings = bnxt_tx_nr_rings(bp);
16785 }
16786 
16787 static void bnxt_adj_dflt_rings(struct bnxt *bp, bool sh)
16788 {
16789 	if (sh)
16790 		bnxt_trim_dflt_sh_rings(bp);
16791 	else
16792 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
16793 	bp->tx_nr_rings = bnxt_tx_nr_rings(bp);
16794 	if (sh && READ_ONCE(bp->xdp_prog)) {
16795 		bnxt_set_xdp_tx_rings(bp);
16796 		bnxt_set_cp_rings(bp, true);
16797 	}
16798 }
16799 
16800 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
16801 {
16802 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
16803 	int avail_msix;
16804 
16805 	if (!bnxt_can_reserve_rings(bp))
16806 		return 0;
16807 
16808 	if (sh)
16809 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
16810 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
16811 	/* Reduce default rings on multi-port cards so that total default
16812 	 * rings do not exceed CPU count.
16813 	 */
16814 	if (bp->port_count > 1) {
16815 		int max_rings =
16816 			max_t(int, num_online_cpus() / bp->port_count, 1);
16817 
16818 		dflt_rings = min_t(int, dflt_rings, max_rings);
16819 	}
16820 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
16821 	if (rc)
16822 		return rc;
16823 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
16824 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
16825 
16826 	bnxt_adj_dflt_rings(bp, sh);
16827 
16828 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
16829 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
16830 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
16831 
16832 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
16833 		bnxt_set_dflt_ulp_stat_ctxs(bp);
16834 	}
16835 
16836 	rc = __bnxt_reserve_rings(bp);
16837 	if (rc && rc != -ENODEV)
16838 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
16839 
16840 	bnxt_adj_tx_rings(bp);
16841 	if (sh)
16842 		bnxt_adj_dflt_rings(bp, true);
16843 
16844 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
16845 	if (bnxt_need_reserve_rings(bp)) {
16846 		rc = __bnxt_reserve_rings(bp);
16847 		if (rc && rc != -ENODEV)
16848 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
16849 		bnxt_adj_tx_rings(bp);
16850 	}
16851 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
16852 		bp->rx_nr_rings++;
16853 		bp->cp_nr_rings++;
16854 	}
16855 	if (rc) {
16856 		bp->tx_nr_rings = 0;
16857 		bp->rx_nr_rings = 0;
16858 	}
16859 	return rc;
16860 }
16861 
16862 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
16863 {
16864 	int rc;
16865 
16866 	if (bp->tx_nr_rings)
16867 		return 0;
16868 
16869 	bnxt_ulp_irq_stop(bp);
16870 	bnxt_clear_int_mode(bp);
16871 	rc = bnxt_set_dflt_rings(bp, true);
16872 	if (rc) {
16873 		if (BNXT_VF(bp) && rc == -ENODEV)
16874 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16875 		else
16876 			netdev_err(bp->dev, "Not enough rings available.\n");
16877 		goto init_dflt_ring_err;
16878 	}
16879 	rc = bnxt_init_int_mode(bp);
16880 	if (rc)
16881 		goto init_dflt_ring_err;
16882 
16883 	bnxt_adj_tx_rings(bp);
16884 
16885 	bnxt_set_dflt_rfs(bp);
16886 
16887 init_dflt_ring_err:
16888 	bnxt_ulp_irq_restart(bp, rc);
16889 	return rc;
16890 }
16891 
16892 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
16893 {
16894 	int rc;
16895 
16896 	netdev_assert_locked_ops_compat(bp->dev);
16897 	bnxt_hwrm_func_qcaps(bp);
16898 
16899 	if (netif_running(bp->dev))
16900 		__bnxt_close_nic(bp, true, false);
16901 
16902 	bnxt_ulp_irq_stop(bp);
16903 	bnxt_clear_int_mode(bp);
16904 	rc = bnxt_init_int_mode(bp);
16905 	bnxt_ulp_irq_restart(bp, rc);
16906 
16907 	if (netif_running(bp->dev)) {
16908 		if (rc)
16909 			netif_close(bp->dev);
16910 		else
16911 			rc = bnxt_open_nic(bp, true, false);
16912 	}
16913 
16914 	return rc;
16915 }
16916 
16917 static int bnxt_init_mac_addr(struct bnxt *bp)
16918 {
16919 	int rc = 0;
16920 
16921 	if (BNXT_PF(bp)) {
16922 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
16923 	} else {
16924 #ifdef CONFIG_BNXT_SRIOV
16925 		struct bnxt_vf_info *vf = &bp->vf;
16926 		bool strict_approval = true;
16927 
16928 		if (is_valid_ether_addr(vf->mac_addr)) {
16929 			/* overwrite netdev dev_addr with admin VF MAC */
16930 			eth_hw_addr_set(bp->dev, vf->mac_addr);
16931 			/* Older PF driver or firmware may not approve this
16932 			 * correctly.
16933 			 */
16934 			strict_approval = false;
16935 		} else {
16936 			eth_hw_addr_random(bp->dev);
16937 		}
16938 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
16939 #endif
16940 	}
16941 	return rc;
16942 }
16943 
16944 static void bnxt_vpd_read_info(struct bnxt *bp)
16945 {
16946 	struct pci_dev *pdev = bp->pdev;
16947 	unsigned int vpd_size, kw_len;
16948 	int pos, size;
16949 	u8 *vpd_data;
16950 
16951 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
16952 	if (IS_ERR(vpd_data)) {
16953 		pci_warn(pdev, "Unable to read VPD\n");
16954 		return;
16955 	}
16956 
16957 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16958 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
16959 	if (pos < 0)
16960 		goto read_sn;
16961 
16962 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16963 	memcpy(bp->board_partno, &vpd_data[pos], size);
16964 
16965 read_sn:
16966 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16967 					   PCI_VPD_RO_KEYWORD_SERIALNO,
16968 					   &kw_len);
16969 	if (pos < 0)
16970 		goto exit;
16971 
16972 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16973 	memcpy(bp->board_serialno, &vpd_data[pos], size);
16974 exit:
16975 	kfree(vpd_data);
16976 }
16977 
16978 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
16979 {
16980 	struct pci_dev *pdev = bp->pdev;
16981 	u64 qword;
16982 
16983 	qword = pci_get_dsn(pdev);
16984 	if (!qword) {
16985 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
16986 		return -EOPNOTSUPP;
16987 	}
16988 
16989 	put_unaligned_le64(qword, dsn);
16990 
16991 	bp->flags |= BNXT_FLAG_DSN_VALID;
16992 	return 0;
16993 }
16994 
16995 static int bnxt_map_db_bar(struct bnxt *bp)
16996 {
16997 	if (!bp->db_size)
16998 		return -ENODEV;
16999 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
17000 	if (!bp->bar1)
17001 		return -ENOMEM;
17002 	return 0;
17003 }
17004 
17005 void bnxt_print_device_info(struct bnxt *bp)
17006 {
17007 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
17008 		    board_info[bp->board_idx].name,
17009 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
17010 
17011 	pcie_print_link_status(bp->pdev);
17012 }
17013 
17014 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
17015 {
17016 	struct bnxt_hw_resc *hw_resc;
17017 	struct net_device *dev;
17018 	struct bnxt *bp;
17019 	int rc, max_irqs;
17020 
17021 	if (pci_is_bridge(pdev))
17022 		return -ENODEV;
17023 
17024 	if (!pdev->msix_cap) {
17025 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
17026 		return -ENODEV;
17027 	}
17028 
17029 	/* Clear any pending DMA transactions from crash kernel
17030 	 * while loading driver in capture kernel.
17031 	 */
17032 	if (is_kdump_kernel()) {
17033 		pci_clear_master(pdev);
17034 		pcie_flr(pdev);
17035 	}
17036 
17037 	max_irqs = bnxt_get_max_irq(pdev);
17038 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
17039 				 max_irqs);
17040 	if (!dev)
17041 		return -ENOMEM;
17042 
17043 	bp = netdev_priv(dev);
17044 	bp->board_idx = ent->driver_data;
17045 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
17046 	bnxt_set_max_func_irqs(bp, max_irqs);
17047 
17048 	if (bnxt_vf_pciid(bp->board_idx))
17049 		bp->flags |= BNXT_FLAG_VF;
17050 
17051 	/* No devlink port registration in case of a VF */
17052 	if (BNXT_PF(bp))
17053 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
17054 
17055 	rc = bnxt_init_board(pdev, dev);
17056 	if (rc < 0)
17057 		goto init_err_free;
17058 
17059 	dev->netdev_ops = &bnxt_netdev_ops;
17060 	dev->xdp_metadata_ops = &bnxt_xdp_metadata_ops;
17061 	dev->stat_ops = &bnxt_stat_ops;
17062 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
17063 	dev->ethtool_ops = &bnxt_ethtool_ops;
17064 	pci_set_drvdata(pdev, dev);
17065 
17066 	rc = bnxt_alloc_hwrm_resources(bp);
17067 	if (rc)
17068 		goto init_err_pci_clean;
17069 
17070 	mutex_init(&bp->hwrm_cmd_lock);
17071 	mutex_init(&bp->link_lock);
17072 
17073 	rc = bnxt_fw_init_one_p1(bp);
17074 	if (rc)
17075 		goto init_err_pci_clean;
17076 
17077 	if (BNXT_PF(bp))
17078 		bnxt_vpd_read_info(bp);
17079 
17080 	if (BNXT_CHIP_P5_PLUS(bp)) {
17081 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
17082 		if (BNXT_CHIP_P7(bp))
17083 			bp->flags |= BNXT_FLAG_CHIP_P7;
17084 	}
17085 
17086 	rc = bnxt_alloc_rss_indir_tbl(bp);
17087 	if (rc)
17088 		goto init_err_pci_clean;
17089 
17090 	rc = bnxt_fw_init_one_p2(bp);
17091 	if (rc)
17092 		goto init_err_pci_clean;
17093 
17094 	rc = bnxt_map_db_bar(bp);
17095 	if (rc) {
17096 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
17097 			rc);
17098 		goto init_err_pci_clean;
17099 	}
17100 
17101 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
17102 			   NETIF_F_TSO | NETIF_F_TSO6 |
17103 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
17104 			   NETIF_F_GSO_IPXIP4 |
17105 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
17106 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
17107 			   NETIF_F_RXCSUM | NETIF_F_GRO;
17108 	dev->hw_features |= NETIF_F_GSO_UDP_L4;
17109 
17110 	if (BNXT_SUPPORTS_TPA(bp))
17111 		dev->hw_features |= NETIF_F_LRO;
17112 
17113 	dev->hw_enc_features =
17114 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
17115 			NETIF_F_TSO | NETIF_F_TSO6 |
17116 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
17117 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
17118 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
17119 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
17120 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
17121 	if (bp->flags & BNXT_FLAG_CHIP_P7)
17122 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
17123 	else
17124 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
17125 
17126 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
17127 				    NETIF_F_GSO_GRE_CSUM;
17128 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
17129 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
17130 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
17131 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
17132 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
17133 	if (BNXT_SUPPORTS_TPA(bp))
17134 		dev->hw_features |= NETIF_F_GRO_HW;
17135 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
17136 	if (dev->features & NETIF_F_GRO_HW)
17137 		dev->features &= ~NETIF_F_LRO;
17138 	dev->priv_flags |= IFF_UNICAST_FLT;
17139 
17140 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
17141 	if (!(bp->flags & BNXT_FLAG_UDP_GSO_CAP)) {
17142 		u16 max_segs = BNXT_SW_USO_MAX_SEGS;
17143 
17144 		if (bp->tso_max_segs)
17145 			max_segs = min_t(u16, max_segs, bp->tso_max_segs);
17146 		netif_set_tso_max_segs(dev, max_segs);
17147 	} else if (bp->tso_max_segs) {
17148 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
17149 	}
17150 
17151 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
17152 			    NETDEV_XDP_ACT_RX_SG;
17153 
17154 #ifdef CONFIG_BNXT_SRIOV
17155 	init_waitqueue_head(&bp->sriov_cfg_wait);
17156 #endif
17157 	if (BNXT_SUPPORTS_TPA(bp)) {
17158 		bp->gro_func = bnxt_gro_func_5730x;
17159 		if (BNXT_CHIP_P4(bp))
17160 			bp->gro_func = bnxt_gro_func_5731x;
17161 		else if (BNXT_CHIP_P5_PLUS(bp))
17162 			bp->gro_func = bnxt_gro_func_5750x;
17163 	}
17164 	if (!BNXT_CHIP_P4_PLUS(bp))
17165 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
17166 
17167 	rc = bnxt_init_mac_addr(bp);
17168 	if (rc) {
17169 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
17170 		rc = -EADDRNOTAVAIL;
17171 		goto init_err_pci_clean;
17172 	}
17173 
17174 	if (BNXT_PF(bp)) {
17175 		/* Read the adapter's DSN to use as the eswitch switch_id */
17176 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
17177 	}
17178 
17179 	/* MTU range: 60 - FW defined max */
17180 	dev->min_mtu = ETH_ZLEN;
17181 	dev->max_mtu = bp->max_mtu;
17182 
17183 	rc = bnxt_probe_phy(bp, true);
17184 	if (rc)
17185 		goto init_err_pci_clean;
17186 
17187 	hw_resc = &bp->hw_resc;
17188 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
17189 		       BNXT_L2_FLTR_MAX_FLTR;
17190 	/* Older firmware may not report these filters properly */
17191 	if (bp->max_fltr < BNXT_MAX_FLTR)
17192 		bp->max_fltr = BNXT_MAX_FLTR;
17193 	bnxt_init_l2_fltr_tbl(bp);
17194 	__bnxt_set_rx_skb_mode(bp, false);
17195 	bnxt_set_tpa_flags(bp);
17196 	bnxt_init_ring_params(bp);
17197 	bnxt_set_ring_params(bp);
17198 	mutex_init(&bp->auxdev_lock);
17199 	if (!bnxt_auxdev_id_alloc(bp))
17200 		bnxt_aux_devices_init(bp);
17201 	rc = bnxt_set_dflt_rings(bp, true);
17202 	if (rc) {
17203 		if (BNXT_VF(bp) && rc == -ENODEV) {
17204 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
17205 		} else {
17206 			netdev_err(bp->dev, "Not enough rings available.\n");
17207 			rc = -ENOMEM;
17208 		}
17209 		goto init_err_pci_clean;
17210 	}
17211 
17212 	bnxt_fw_init_one_p3(bp);
17213 
17214 	bnxt_init_dflt_coal(bp);
17215 
17216 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
17217 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
17218 
17219 	rc = bnxt_init_int_mode(bp);
17220 	if (rc)
17221 		goto init_err_pci_clean;
17222 
17223 	/* No TC has been set yet and rings may have been trimmed due to
17224 	 * limited MSIX, so we re-initialize the TX rings per TC.
17225 	 */
17226 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
17227 
17228 	if (BNXT_PF(bp)) {
17229 		if (!bnxt_pf_wq) {
17230 			bnxt_pf_wq =
17231 				create_singlethread_workqueue("bnxt_pf_wq");
17232 			if (!bnxt_pf_wq) {
17233 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
17234 				rc = -ENOMEM;
17235 				goto init_err_pci_clean;
17236 			}
17237 		}
17238 		rc = bnxt_init_tc(bp);
17239 		if (rc)
17240 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
17241 				   rc);
17242 	}
17243 
17244 	bnxt_inv_fw_health_reg(bp);
17245 	rc = bnxt_dl_register(bp);
17246 	if (rc)
17247 		goto init_err_dl;
17248 
17249 	INIT_LIST_HEAD(&bp->usr_fltr_list);
17250 
17251 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
17252 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
17253 
17254 	dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops_unsupp;
17255 	if (BNXT_SUPPORTS_QUEUE_API(bp))
17256 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
17257 	dev->netmem_tx = NETMEM_TX_DMA;
17258 
17259 	rc = register_netdev(dev);
17260 	if (rc)
17261 		goto init_err_cleanup;
17262 
17263 	bnxt_dl_fw_reporters_create(bp);
17264 
17265 	bnxt_aux_devices_add(bp);
17266 
17267 	bnxt_print_device_info(bp);
17268 
17269 	pci_save_state(pdev);
17270 
17271 	return 0;
17272 init_err_cleanup:
17273 	bnxt_aux_devices_uninit(bp);
17274 	bnxt_auxdev_id_free(bp, bp->auxdev_id);
17275 	bnxt_dl_unregister(bp);
17276 init_err_dl:
17277 	bnxt_shutdown_tc(bp);
17278 	bnxt_clear_int_mode(bp);
17279 
17280 init_err_pci_clean:
17281 	bnxt_hwrm_func_drv_unrgtr(bp);
17282 	bnxt_ptp_clear(bp);
17283 	kfree(bp->ptp_cfg);
17284 	bp->ptp_cfg = NULL;
17285 	bnxt_free_hwrm_resources(bp);
17286 	bnxt_hwmon_uninit(bp);
17287 	bnxt_ethtool_free(bp);
17288 	kfree(bp->fw_health);
17289 	bp->fw_health = NULL;
17290 	bnxt_cleanup_pci(bp);
17291 	bnxt_free_ctx_mem(bp, true);
17292 	bnxt_free_crash_dump_mem(bp);
17293 	kfree(bp->rss_indir_tbl);
17294 	bp->rss_indir_tbl = NULL;
17295 
17296 init_err_free:
17297 	free_netdev(dev);
17298 	return rc;
17299 }
17300 
17301 static void bnxt_shutdown(struct pci_dev *pdev)
17302 {
17303 	struct net_device *dev = pci_get_drvdata(pdev);
17304 	struct bnxt *bp;
17305 
17306 	if (!dev)
17307 		return;
17308 
17309 	rtnl_lock();
17310 	netdev_lock(dev);
17311 	bp = netdev_priv(dev);
17312 	if (!bp)
17313 		goto shutdown_exit;
17314 
17315 	if (netif_running(dev))
17316 		netif_close(dev);
17317 
17318 	if (bnxt_hwrm_func_drv_unrgtr(bp)) {
17319 		pcie_flr(pdev);
17320 		goto shutdown_exit;
17321 	}
17322 	bnxt_ptp_clear(bp);
17323 	bnxt_clear_int_mode(bp);
17324 	pci_disable_device(pdev);
17325 
17326 	if (system_state == SYSTEM_POWER_OFF) {
17327 		pci_wake_from_d3(pdev, bp->wol);
17328 		pci_set_power_state(pdev, PCI_D3hot);
17329 	}
17330 
17331 shutdown_exit:
17332 	netdev_unlock(dev);
17333 	rtnl_unlock();
17334 }
17335 
17336 #ifdef CONFIG_PM_SLEEP
17337 static int bnxt_suspend(struct device *device)
17338 {
17339 	struct net_device *dev = dev_get_drvdata(device);
17340 	struct bnxt *bp = netdev_priv(dev);
17341 	int rc = 0;
17342 
17343 	bnxt_ulp_stop(bp);
17344 
17345 	netdev_lock(dev);
17346 	if (netif_running(dev)) {
17347 		netif_device_detach(dev);
17348 		rc = bnxt_close(dev);
17349 	}
17350 	bnxt_hwrm_func_drv_unrgtr(bp);
17351 	bnxt_ptp_clear(bp);
17352 	pci_disable_device(bp->pdev);
17353 	bnxt_free_ctx_mem(bp, false);
17354 	netdev_unlock(dev);
17355 	return rc;
17356 }
17357 
17358 static int bnxt_resume(struct device *device)
17359 {
17360 	struct net_device *dev = dev_get_drvdata(device);
17361 	struct bnxt *bp = netdev_priv(dev);
17362 	int rc = 0;
17363 
17364 	rtnl_lock();
17365 	netdev_lock(dev);
17366 	rc = pci_enable_device(bp->pdev);
17367 	if (rc) {
17368 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
17369 			   rc);
17370 		goto resume_exit;
17371 	}
17372 	pci_set_master(bp->pdev);
17373 	if (bnxt_hwrm_ver_get(bp)) {
17374 		rc = -ENODEV;
17375 		goto resume_exit;
17376 	}
17377 	rc = bnxt_hwrm_func_reset(bp);
17378 	if (rc) {
17379 		rc = -EBUSY;
17380 		goto resume_exit;
17381 	}
17382 
17383 	rc = bnxt_hwrm_func_qcaps(bp);
17384 	if (rc)
17385 		goto resume_exit;
17386 
17387 	bnxt_clear_reservations(bp, true);
17388 
17389 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
17390 		rc = -ENODEV;
17391 		goto resume_exit;
17392 	}
17393 	if (bp->fw_crash_mem)
17394 		bnxt_hwrm_crash_dump_mem_cfg(bp);
17395 
17396 	if (bnxt_ptp_init(bp)) {
17397 		kfree(bp->ptp_cfg);
17398 		bp->ptp_cfg = NULL;
17399 	}
17400 	bnxt_get_wol_settings(bp);
17401 	if (netif_running(dev)) {
17402 		rc = bnxt_open(dev);
17403 		if (!rc)
17404 			netif_device_attach(dev);
17405 	}
17406 
17407 resume_exit:
17408 	netdev_unlock(bp->dev);
17409 	rtnl_unlock();
17410 	if (!rc) {
17411 		bnxt_ulp_start(bp);
17412 		bnxt_reenable_sriov(bp);
17413 	}
17414 	return rc;
17415 }
17416 
17417 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
17418 #define BNXT_PM_OPS (&bnxt_pm_ops)
17419 
17420 #else
17421 
17422 #define BNXT_PM_OPS NULL
17423 
17424 #endif /* CONFIG_PM_SLEEP */
17425 
17426 /**
17427  * bnxt_io_error_detected - called when PCI error is detected
17428  * @pdev: Pointer to PCI device
17429  * @state: The current pci connection state
17430  *
17431  * This function is called after a PCI bus error affecting
17432  * this device has been detected.
17433  */
17434 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
17435 					       pci_channel_state_t state)
17436 {
17437 	struct net_device *netdev = pci_get_drvdata(pdev);
17438 	struct bnxt *bp = netdev_priv(netdev);
17439 	bool abort = false;
17440 
17441 	netdev_info(netdev, "PCI I/O error detected\n");
17442 
17443 	bnxt_ulp_stop(bp);
17444 
17445 	netdev_lock(netdev);
17446 	netif_device_detach(netdev);
17447 
17448 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
17449 		netdev_err(bp->dev, "Firmware reset already in progress\n");
17450 		abort = true;
17451 	}
17452 
17453 	if (abort || state == pci_channel_io_perm_failure) {
17454 		netdev_unlock(netdev);
17455 		return PCI_ERS_RESULT_DISCONNECT;
17456 	}
17457 
17458 	/* Link is not reliable anymore if state is pci_channel_io_frozen
17459 	 * so we disable bus master to prevent any potential bad DMAs before
17460 	 * freeing kernel memory.
17461 	 */
17462 	if (state == pci_channel_io_frozen) {
17463 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
17464 		bnxt_fw_fatal_close(bp);
17465 	}
17466 
17467 	if (netif_running(netdev))
17468 		__bnxt_close_nic(bp, true, true);
17469 
17470 	if (pci_is_enabled(pdev))
17471 		pci_disable_device(pdev);
17472 	bnxt_free_ctx_mem(bp, false);
17473 	netdev_unlock(netdev);
17474 
17475 	/* Request a slot reset. */
17476 	return PCI_ERS_RESULT_NEED_RESET;
17477 }
17478 
17479 /**
17480  * bnxt_io_slot_reset - called after the pci bus has been reset.
17481  * @pdev: Pointer to PCI device
17482  *
17483  * Restart the card from scratch, as if from a cold-boot.
17484  * At this point, the card has experienced a hard reset,
17485  * followed by fixups by BIOS, and has its config space
17486  * set up identically to what it was at cold boot.
17487  */
17488 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
17489 {
17490 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
17491 	struct net_device *netdev = pci_get_drvdata(pdev);
17492 	struct bnxt *bp = netdev_priv(netdev);
17493 	int retry = 0;
17494 	int err = 0;
17495 	int off;
17496 
17497 	netdev_info(bp->dev, "PCI Slot Reset\n");
17498 
17499 	if (test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state)) {
17500 		/* After DPC, the chip should return CRS when the vendor ID
17501 		 * config register is read until it is ready.  On all chips,
17502 		 * this is not happening reliably so add a 5-second delay as a
17503 		 * workaround.
17504 		 */
17505 		msleep(5000);
17506 	}
17507 
17508 	netdev_lock(netdev);
17509 
17510 	if (pci_enable_device(pdev)) {
17511 		dev_err(&pdev->dev,
17512 			"Cannot re-enable PCI device after reset.\n");
17513 	} else {
17514 		pci_set_master(pdev);
17515 		/* Upon fatal error, our device internal logic that latches to
17516 		 * BAR value is getting reset and will restore only upon
17517 		 * rewriting the BARs.
17518 		 *
17519 		 * As pci_restore_state() does not re-write the BARs if the
17520 		 * value is same as saved value earlier, driver needs to
17521 		 * write the BARs to 0 to force restore, in case of fatal error.
17522 		 */
17523 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
17524 				       &bp->state)) {
17525 			for (off = PCI_BASE_ADDRESS_0;
17526 			     off <= PCI_BASE_ADDRESS_5; off += 4)
17527 				pci_write_config_dword(bp->pdev, off, 0);
17528 		}
17529 		pci_restore_state(pdev);
17530 
17531 		bnxt_inv_fw_health_reg(bp);
17532 		bnxt_try_map_fw_health_reg(bp);
17533 
17534 		/* In some PCIe AER scenarios, firmware may take up to
17535 		 * 10 seconds to become ready in the worst case.
17536 		 */
17537 		do {
17538 			err = bnxt_try_recover_fw(bp);
17539 			if (!err)
17540 				break;
17541 			retry++;
17542 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
17543 
17544 		if (err) {
17545 			dev_err(&pdev->dev, "Firmware not ready\n");
17546 			goto reset_exit;
17547 		}
17548 
17549 		err = bnxt_hwrm_func_reset(bp);
17550 		if (!err)
17551 			result = PCI_ERS_RESULT_RECOVERED;
17552 
17553 		/* IRQ will be initialized later in bnxt_io_resume */
17554 		bnxt_ulp_irq_stop(bp);
17555 		bnxt_clear_int_mode(bp);
17556 	}
17557 
17558 reset_exit:
17559 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
17560 	bnxt_clear_reservations(bp, true);
17561 	netdev_unlock(netdev);
17562 
17563 	return result;
17564 }
17565 
17566 /**
17567  * bnxt_io_resume - called when traffic can start flowing again.
17568  * @pdev: Pointer to PCI device
17569  *
17570  * This callback is called when the error recovery driver tells
17571  * us that its OK to resume normal operation.
17572  */
17573 static void bnxt_io_resume(struct pci_dev *pdev)
17574 {
17575 	struct net_device *netdev = pci_get_drvdata(pdev);
17576 	struct bnxt *bp = netdev_priv(netdev);
17577 	int err;
17578 
17579 	netdev_info(bp->dev, "PCI Slot Resume\n");
17580 	rtnl_lock();
17581 	netdev_lock(netdev);
17582 
17583 	err = bnxt_hwrm_func_qcaps(bp);
17584 	if (!err) {
17585 		if (netif_running(netdev)) {
17586 			err = bnxt_open(netdev);
17587 		} else {
17588 			err = bnxt_reserve_rings(bp, true);
17589 			if (!err)
17590 				err = bnxt_init_int_mode(bp);
17591 		}
17592 	}
17593 
17594 	if (!err)
17595 		netif_device_attach(netdev);
17596 
17597 	netdev_unlock(netdev);
17598 	rtnl_unlock();
17599 	if (!err) {
17600 		bnxt_ulp_start(bp);
17601 		bnxt_reenable_sriov(bp);
17602 	}
17603 }
17604 
17605 static const struct pci_error_handlers bnxt_err_handler = {
17606 	.error_detected	= bnxt_io_error_detected,
17607 	.slot_reset	= bnxt_io_slot_reset,
17608 	.resume		= bnxt_io_resume
17609 };
17610 
17611 static struct pci_driver bnxt_pci_driver = {
17612 	.name		= DRV_MODULE_NAME,
17613 	.id_table	= bnxt_pci_tbl,
17614 	.probe		= bnxt_init_one,
17615 	.remove		= bnxt_remove_one,
17616 	.shutdown	= bnxt_shutdown,
17617 	.driver.pm	= BNXT_PM_OPS,
17618 	.err_handler	= &bnxt_err_handler,
17619 #if defined(CONFIG_BNXT_SRIOV)
17620 	.sriov_configure = bnxt_sriov_configure,
17621 #endif
17622 };
17623 
17624 static int __init bnxt_init(void)
17625 {
17626 	int err;
17627 
17628 	bnxt_debug_init();
17629 	err = pci_register_driver(&bnxt_pci_driver);
17630 	if (err) {
17631 		bnxt_debug_exit();
17632 		return err;
17633 	}
17634 
17635 	return 0;
17636 }
17637 
17638 static void __exit bnxt_exit(void)
17639 {
17640 	pci_unregister_driver(&bnxt_pci_driver);
17641 	if (bnxt_pf_wq)
17642 		destroy_workqueue(bnxt_pf_wq);
17643 	bnxt_debug_exit();
17644 }
17645 
17646 module_init(bnxt_init);
17647 module_exit(bnxt_exit);
17648