xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision 40529e58629baa9ce72143cb46cf1b3d2ca0d465)
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 		/* On P7, setting EOP will cause the chip to disable
4615 		 * Relaxed Ordering (RO) for TPA data.  Disable EOP for
4616 		 * potentially higher performance with RO.
4617 		 */
4618 		if (BNXT_CHIP_P5_AND_MINUS(bp) || !(bp->flags & BNXT_FLAG_TPA))
4619 			type |= RX_BD_FLAGS_AGG_EOP;
4620 
4621 		bnxt_init_rxbd_pages(ring, type);
4622 	}
4623 }
4624 
4625 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4626 {
4627 	struct bnxt_rx_ring_info *rxr;
4628 
4629 	rxr = &bp->rx_ring[ring_nr];
4630 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4631 
4632 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4633 			     &rxr->bnapi->napi);
4634 
4635 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4636 		bpf_prog_add(bp->xdp_prog, 1);
4637 		rxr->xdp_prog = bp->xdp_prog;
4638 	}
4639 
4640 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4641 
4642 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4643 }
4644 
4645 static void bnxt_init_cp_rings(struct bnxt *bp)
4646 {
4647 	int i, j;
4648 
4649 	for (i = 0; i < bp->cp_nr_rings; i++) {
4650 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4651 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4652 
4653 		ring->fw_ring_id = INVALID_HW_RING_ID;
4654 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4655 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4656 		if (!cpr->cp_ring_arr)
4657 			continue;
4658 		for (j = 0; j < cpr->cp_ring_count; j++) {
4659 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4660 
4661 			ring = &cpr2->cp_ring_struct;
4662 			ring->fw_ring_id = INVALID_HW_RING_ID;
4663 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4664 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4665 		}
4666 	}
4667 }
4668 
4669 static int bnxt_init_rx_rings(struct bnxt *bp)
4670 {
4671 	int i, rc = 0;
4672 
4673 	if (BNXT_RX_PAGE_MODE(bp)) {
4674 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4675 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4676 	} else {
4677 		bp->rx_offset = BNXT_RX_OFFSET;
4678 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4679 	}
4680 
4681 	for (i = 0; i < bp->rx_nr_rings; i++) {
4682 		rc = bnxt_init_one_rx_ring(bp, i);
4683 		if (rc)
4684 			break;
4685 	}
4686 
4687 	return rc;
4688 }
4689 
4690 static int bnxt_init_tx_rings(struct bnxt *bp)
4691 {
4692 	netdev_features_t features;
4693 	u16 i;
4694 
4695 	features = bp->dev->features;
4696 
4697 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4698 				   bnxt_min_tx_desc_cnt(bp, features));
4699 
4700 	for (i = 0; i < bp->tx_nr_rings; i++) {
4701 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4702 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4703 
4704 		ring->fw_ring_id = INVALID_HW_RING_ID;
4705 
4706 		if (i >= bp->tx_nr_rings_xdp)
4707 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4708 					     NETDEV_QUEUE_TYPE_TX,
4709 					     &txr->bnapi->napi);
4710 	}
4711 
4712 	return 0;
4713 }
4714 
4715 static void bnxt_free_ring_grps(struct bnxt *bp)
4716 {
4717 	kfree(bp->grp_info);
4718 	bp->grp_info = NULL;
4719 }
4720 
4721 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4722 {
4723 	int i;
4724 
4725 	if (irq_re_init) {
4726 		bp->grp_info = kzalloc_objs(struct bnxt_ring_grp_info,
4727 					    bp->cp_nr_rings);
4728 		if (!bp->grp_info)
4729 			return -ENOMEM;
4730 	}
4731 	for (i = 0; i < bp->cp_nr_rings; i++) {
4732 		if (irq_re_init)
4733 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4734 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4735 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4736 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4737 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4738 	}
4739 	return 0;
4740 }
4741 
4742 static void bnxt_free_vnics(struct bnxt *bp)
4743 {
4744 	kfree(bp->vnic_info);
4745 	bp->vnic_info = NULL;
4746 	bp->nr_vnics = 0;
4747 }
4748 
4749 static int bnxt_alloc_vnics(struct bnxt *bp)
4750 {
4751 	int num_vnics = 1;
4752 
4753 #ifdef CONFIG_RFS_ACCEL
4754 	if (bp->flags & BNXT_FLAG_RFS) {
4755 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4756 			num_vnics++;
4757 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4758 			num_vnics += bp->rx_nr_rings;
4759 	}
4760 #endif
4761 
4762 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4763 		num_vnics++;
4764 
4765 	bp->vnic_info = kzalloc_objs(struct bnxt_vnic_info, num_vnics);
4766 	if (!bp->vnic_info)
4767 		return -ENOMEM;
4768 
4769 	bp->nr_vnics = num_vnics;
4770 	return 0;
4771 }
4772 
4773 static void bnxt_init_vnics(struct bnxt *bp)
4774 {
4775 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4776 	int i;
4777 
4778 	for (i = 0; i < bp->nr_vnics; i++) {
4779 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4780 		int j;
4781 
4782 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4783 		vnic->vnic_id = i;
4784 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4785 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4786 
4787 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4788 
4789 		if (bp->vnic_info[i].rss_hash_key) {
4790 			if (i == BNXT_VNIC_DEFAULT) {
4791 				u8 *key = (void *)vnic->rss_hash_key;
4792 				int k;
4793 
4794 				if (!bp->rss_hash_key_valid &&
4795 				    !bp->rss_hash_key_updated) {
4796 					get_random_bytes(bp->rss_hash_key,
4797 							 HW_HASH_KEY_SIZE);
4798 					bp->rss_hash_key_updated = true;
4799 				}
4800 
4801 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4802 				       HW_HASH_KEY_SIZE);
4803 
4804 				if (!bp->rss_hash_key_updated)
4805 					continue;
4806 
4807 				bp->rss_hash_key_updated = false;
4808 				bp->rss_hash_key_valid = true;
4809 
4810 				bp->toeplitz_prefix = 0;
4811 				for (k = 0; k < 8; k++) {
4812 					bp->toeplitz_prefix <<= 8;
4813 					bp->toeplitz_prefix |= key[k];
4814 				}
4815 			} else {
4816 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4817 				       HW_HASH_KEY_SIZE);
4818 			}
4819 		}
4820 	}
4821 }
4822 
4823 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4824 {
4825 	int pages;
4826 
4827 	pages = ring_size / desc_per_pg;
4828 
4829 	if (!pages)
4830 		return 1;
4831 
4832 	pages++;
4833 
4834 	while (pages & (pages - 1))
4835 		pages++;
4836 
4837 	return pages;
4838 }
4839 
4840 void bnxt_set_tpa_flags(struct bnxt *bp)
4841 {
4842 	bp->flags &= ~BNXT_FLAG_TPA;
4843 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4844 		return;
4845 	if (bp->dev->features & NETIF_F_LRO)
4846 		bp->flags |= BNXT_FLAG_LRO;
4847 	else if (bp->dev->features & NETIF_F_GRO_HW)
4848 		bp->flags |= BNXT_FLAG_GRO;
4849 }
4850 
4851 static void bnxt_init_ring_params(struct bnxt *bp)
4852 {
4853 	unsigned int rx_size;
4854 
4855 	bp->rx_copybreak = 0; /* rx-copybreak disabled by default */
4856 	/* Try to fit 4 chunks into a 4k page */
4857 	rx_size = SZ_1K -
4858 		NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4859 	bp->dev->cfg->hds_thresh = max(BNXT_MIN_RX_HDR_BUF, rx_size);
4860 }
4861 
4862 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4863  * be set on entry.
4864  */
4865 void bnxt_set_ring_params(struct bnxt *bp)
4866 {
4867 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4868 	u32 agg_factor = 0, agg_ring_size = 0;
4869 
4870 	/* 8 for CRC and VLAN */
4871 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4872 
4873 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4874 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4875 
4876 	ring_size = bp->rx_ring_size;
4877 	bp->rx_agg_ring_size = 0;
4878 	bp->rx_agg_nr_pages = 0;
4879 
4880 	if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS)
4881 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4882 
4883 	bp->flags &= ~BNXT_FLAG_JUMBO;
4884 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4885 		u32 jumbo_factor;
4886 
4887 		bp->flags |= BNXT_FLAG_JUMBO;
4888 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4889 		if (jumbo_factor > agg_factor)
4890 			agg_factor = jumbo_factor;
4891 	}
4892 	if (agg_factor) {
4893 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4894 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4895 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4896 				    bp->rx_ring_size, ring_size);
4897 			bp->rx_ring_size = ring_size;
4898 		}
4899 		agg_ring_size = ring_size * agg_factor;
4900 
4901 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4902 							RX_DESC_CNT);
4903 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4904 			u32 tmp = agg_ring_size;
4905 
4906 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4907 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4908 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4909 				    tmp, agg_ring_size);
4910 		}
4911 		bp->rx_agg_ring_size = agg_ring_size;
4912 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4913 
4914 		if (BNXT_RX_PAGE_MODE(bp)) {
4915 			rx_space = PAGE_SIZE;
4916 			rx_size = PAGE_SIZE -
4917 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4918 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4919 		} else {
4920 			rx_size = max3(BNXT_MIN_RX_HDR_BUF,
4921 				       bp->rx_copybreak,
4922 				       bp->dev->cfg_pending->hds_thresh);
4923 			rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN);
4924 			rx_space = rx_size + NET_SKB_PAD +
4925 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4926 		}
4927 	}
4928 
4929 	bp->rx_buf_use_size = rx_size;
4930 	bp->rx_buf_size = rx_space;
4931 
4932 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4933 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4934 
4935 	ring_size = bp->tx_ring_size;
4936 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4937 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4938 
4939 	max_rx_cmpl = bp->rx_ring_size;
4940 	/* MAX TPA needs to be added because TPA_START completions are
4941 	 * immediately recycled, so the TPA completions are not bound by
4942 	 * the RX ring size.
4943 	 */
4944 	if (bp->flags & BNXT_FLAG_TPA)
4945 		max_rx_cmpl += bp->max_tpa;
4946 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4947 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4948 	bp->cp_ring_size = ring_size;
4949 
4950 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4951 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4952 		bp->cp_nr_pages = MAX_CP_PAGES;
4953 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4954 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4955 			    ring_size, bp->cp_ring_size);
4956 	}
4957 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4958 	bp->cp_ring_mask = bp->cp_bit - 1;
4959 }
4960 
4961 /* Changing allocation mode of RX rings.
4962  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4963  */
4964 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4965 {
4966 	struct net_device *dev = bp->dev;
4967 
4968 	if (page_mode) {
4969 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
4970 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4971 
4972 		if (bp->xdp_prog->aux->xdp_has_frags)
4973 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4974 		else
4975 			dev->max_mtu =
4976 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4977 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4978 			bp->flags |= BNXT_FLAG_JUMBO;
4979 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4980 		} else {
4981 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4982 			bp->rx_skb_func = bnxt_rx_page_skb;
4983 		}
4984 		bp->rx_dir = DMA_BIDIRECTIONAL;
4985 	} else {
4986 		dev->max_mtu = bp->max_mtu;
4987 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4988 		bp->rx_dir = DMA_FROM_DEVICE;
4989 		bp->rx_skb_func = bnxt_rx_skb;
4990 	}
4991 }
4992 
4993 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4994 {
4995 	__bnxt_set_rx_skb_mode(bp, page_mode);
4996 
4997 	if (!page_mode) {
4998 		int rx, tx;
4999 
5000 		bnxt_get_max_rings(bp, &rx, &tx, true);
5001 		if (rx > 1) {
5002 			bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS;
5003 			bp->dev->hw_features |= NETIF_F_LRO;
5004 		}
5005 	}
5006 
5007 	/* Update LRO and GRO_HW availability */
5008 	netdev_update_features(bp->dev);
5009 }
5010 
5011 static void bnxt_free_vnic_attributes(struct bnxt *bp)
5012 {
5013 	int i;
5014 	struct bnxt_vnic_info *vnic;
5015 	struct pci_dev *pdev = bp->pdev;
5016 
5017 	if (!bp->vnic_info)
5018 		return;
5019 
5020 	for (i = 0; i < bp->nr_vnics; i++) {
5021 		vnic = &bp->vnic_info[i];
5022 
5023 		kfree(vnic->fw_grp_ids);
5024 		vnic->fw_grp_ids = NULL;
5025 
5026 		kfree(vnic->uc_list);
5027 		vnic->uc_list = NULL;
5028 
5029 		if (vnic->mc_list) {
5030 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
5031 					  vnic->mc_list, vnic->mc_list_mapping);
5032 			vnic->mc_list = NULL;
5033 		}
5034 
5035 		if (vnic->rss_table) {
5036 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
5037 					  vnic->rss_table,
5038 					  vnic->rss_table_dma_addr);
5039 			vnic->rss_table = NULL;
5040 		}
5041 
5042 		vnic->rss_hash_key = NULL;
5043 		vnic->flags = 0;
5044 	}
5045 }
5046 
5047 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
5048 {
5049 	int i, rc = 0, size;
5050 	struct bnxt_vnic_info *vnic;
5051 	struct pci_dev *pdev = bp->pdev;
5052 	int max_rings;
5053 
5054 	for (i = 0; i < bp->nr_vnics; i++) {
5055 		vnic = &bp->vnic_info[i];
5056 
5057 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
5058 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
5059 
5060 			if (mem_size > 0) {
5061 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
5062 				if (!vnic->uc_list) {
5063 					rc = -ENOMEM;
5064 					goto out;
5065 				}
5066 			}
5067 		}
5068 
5069 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
5070 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
5071 			vnic->mc_list =
5072 				dma_alloc_coherent(&pdev->dev,
5073 						   vnic->mc_list_size,
5074 						   &vnic->mc_list_mapping,
5075 						   GFP_KERNEL);
5076 			if (!vnic->mc_list) {
5077 				rc = -ENOMEM;
5078 				goto out;
5079 			}
5080 		}
5081 
5082 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5083 			goto vnic_skip_grps;
5084 
5085 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
5086 			max_rings = bp->rx_nr_rings;
5087 		else
5088 			max_rings = 1;
5089 
5090 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
5091 		if (!vnic->fw_grp_ids) {
5092 			rc = -ENOMEM;
5093 			goto out;
5094 		}
5095 vnic_skip_grps:
5096 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
5097 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
5098 			continue;
5099 
5100 		/* Allocate rss table and hash key */
5101 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
5102 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5103 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
5104 
5105 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
5106 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
5107 						     vnic->rss_table_size,
5108 						     &vnic->rss_table_dma_addr,
5109 						     GFP_KERNEL);
5110 		if (!vnic->rss_table) {
5111 			rc = -ENOMEM;
5112 			goto out;
5113 		}
5114 
5115 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
5116 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
5117 	}
5118 	return 0;
5119 
5120 out:
5121 	return rc;
5122 }
5123 
5124 static void bnxt_free_hwrm_resources(struct bnxt *bp)
5125 {
5126 	struct bnxt_hwrm_wait_token *token;
5127 
5128 	dma_pool_destroy(bp->hwrm_dma_pool);
5129 	bp->hwrm_dma_pool = NULL;
5130 
5131 	rcu_read_lock();
5132 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
5133 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
5134 	rcu_read_unlock();
5135 }
5136 
5137 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
5138 {
5139 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
5140 					    BNXT_HWRM_DMA_SIZE,
5141 					    BNXT_HWRM_DMA_ALIGN, 0);
5142 	if (!bp->hwrm_dma_pool)
5143 		return -ENOMEM;
5144 
5145 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
5146 
5147 	return 0;
5148 }
5149 
5150 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
5151 {
5152 	kfree(stats->hw_masks);
5153 	stats->hw_masks = NULL;
5154 	kfree(stats->sw_stats);
5155 	stats->sw_stats = NULL;
5156 	if (stats->hw_stats) {
5157 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
5158 				  stats->hw_stats_map);
5159 		stats->hw_stats = NULL;
5160 	}
5161 }
5162 
5163 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
5164 				bool alloc_masks)
5165 {
5166 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
5167 					     &stats->hw_stats_map, GFP_KERNEL);
5168 	if (!stats->hw_stats)
5169 		return -ENOMEM;
5170 
5171 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
5172 	if (!stats->sw_stats)
5173 		goto stats_mem_err;
5174 
5175 	if (alloc_masks) {
5176 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
5177 		if (!stats->hw_masks)
5178 			goto stats_mem_err;
5179 	}
5180 	return 0;
5181 
5182 stats_mem_err:
5183 	bnxt_free_stats_mem(bp, stats);
5184 	return -ENOMEM;
5185 }
5186 
5187 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
5188 {
5189 	int i;
5190 
5191 	for (i = 0; i < count; i++)
5192 		mask_arr[i] = mask;
5193 }
5194 
5195 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
5196 {
5197 	int i;
5198 
5199 	for (i = 0; i < count; i++)
5200 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
5201 }
5202 
5203 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
5204 				    struct bnxt_stats_mem *stats)
5205 {
5206 	struct hwrm_func_qstats_ext_output *resp;
5207 	struct hwrm_func_qstats_ext_input *req;
5208 	__le64 *hw_masks;
5209 	int rc;
5210 
5211 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
5212 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5213 		return -EOPNOTSUPP;
5214 
5215 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
5216 	if (rc)
5217 		return rc;
5218 
5219 	req->fid = cpu_to_le16(0xffff);
5220 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5221 
5222 	resp = hwrm_req_hold(bp, req);
5223 	rc = hwrm_req_send(bp, req);
5224 	if (!rc) {
5225 		hw_masks = &resp->rx_ucast_pkts;
5226 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
5227 	}
5228 	hwrm_req_drop(bp, req);
5229 	return rc;
5230 }
5231 
5232 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
5233 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
5234 
5235 static void bnxt_init_stats(struct bnxt *bp)
5236 {
5237 	struct bnxt_napi *bnapi = bp->bnapi[0];
5238 	struct bnxt_cp_ring_info *cpr;
5239 	struct bnxt_stats_mem *stats;
5240 	__le64 *rx_stats, *tx_stats;
5241 	int rc, rx_count, tx_count;
5242 	u64 *rx_masks, *tx_masks;
5243 	u64 mask;
5244 	u8 flags;
5245 
5246 	cpr = &bnapi->cp_ring;
5247 	stats = &cpr->stats;
5248 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
5249 	if (rc) {
5250 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5251 			mask = (1ULL << 48) - 1;
5252 		else
5253 			mask = -1ULL;
5254 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
5255 	}
5256 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
5257 		stats = &bp->port_stats;
5258 		rx_stats = stats->hw_stats;
5259 		rx_masks = stats->hw_masks;
5260 		rx_count = sizeof(struct rx_port_stats) / 8;
5261 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5262 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5263 		tx_count = sizeof(struct tx_port_stats) / 8;
5264 
5265 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
5266 		rc = bnxt_hwrm_port_qstats(bp, flags);
5267 		if (rc) {
5268 			mask = (1ULL << 40) - 1;
5269 
5270 			bnxt_fill_masks(rx_masks, mask, rx_count);
5271 			bnxt_fill_masks(tx_masks, mask, tx_count);
5272 		} else {
5273 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5274 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5275 			bnxt_hwrm_port_qstats(bp, 0);
5276 		}
5277 	}
5278 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5279 		stats = &bp->rx_port_stats_ext;
5280 		rx_stats = stats->hw_stats;
5281 		rx_masks = stats->hw_masks;
5282 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5283 		stats = &bp->tx_port_stats_ext;
5284 		tx_stats = stats->hw_stats;
5285 		tx_masks = stats->hw_masks;
5286 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5287 
5288 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5289 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5290 		if (rc) {
5291 			mask = (1ULL << 40) - 1;
5292 
5293 			bnxt_fill_masks(rx_masks, mask, rx_count);
5294 			if (tx_stats)
5295 				bnxt_fill_masks(tx_masks, mask, tx_count);
5296 		} else {
5297 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5298 			if (tx_stats)
5299 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5300 						   tx_count);
5301 			bnxt_hwrm_port_qstats_ext(bp, 0);
5302 		}
5303 	}
5304 }
5305 
5306 static void bnxt_free_port_stats(struct bnxt *bp)
5307 {
5308 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5309 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5310 
5311 	bnxt_free_stats_mem(bp, &bp->port_stats);
5312 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5313 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5314 }
5315 
5316 static void bnxt_free_ring_stats(struct bnxt *bp)
5317 {
5318 	int i;
5319 
5320 	if (!bp->bnapi)
5321 		return;
5322 
5323 	for (i = 0; i < bp->cp_nr_rings; i++) {
5324 		struct bnxt_napi *bnapi = bp->bnapi[i];
5325 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5326 
5327 		bnxt_free_stats_mem(bp, &cpr->stats);
5328 
5329 		kfree(cpr->sw_stats);
5330 		cpr->sw_stats = NULL;
5331 	}
5332 }
5333 
5334 static int bnxt_alloc_stats(struct bnxt *bp)
5335 {
5336 	u32 size, i;
5337 	int rc;
5338 
5339 	size = bp->hw_ring_stats_size;
5340 
5341 	for (i = 0; i < bp->cp_nr_rings; i++) {
5342 		struct bnxt_napi *bnapi = bp->bnapi[i];
5343 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5344 
5345 		cpr->sw_stats = kzalloc_obj(*cpr->sw_stats);
5346 		if (!cpr->sw_stats)
5347 			return -ENOMEM;
5348 
5349 		cpr->stats.len = size;
5350 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5351 		if (rc)
5352 			return rc;
5353 
5354 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5355 	}
5356 
5357 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5358 		return 0;
5359 
5360 	if (bp->port_stats.hw_stats)
5361 		goto alloc_ext_stats;
5362 
5363 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5364 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5365 	if (rc)
5366 		return rc;
5367 
5368 	bp->flags |= BNXT_FLAG_PORT_STATS;
5369 
5370 alloc_ext_stats:
5371 	/* Display extended statistics only if FW supports it */
5372 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5373 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5374 			return 0;
5375 
5376 	if (bp->rx_port_stats_ext.hw_stats)
5377 		goto alloc_tx_ext_stats;
5378 
5379 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5380 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5381 	/* Extended stats are optional */
5382 	if (rc)
5383 		return 0;
5384 
5385 alloc_tx_ext_stats:
5386 	if (bp->tx_port_stats_ext.hw_stats)
5387 		return 0;
5388 
5389 	if (bp->hwrm_spec_code >= 0x10902 ||
5390 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5391 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5392 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5393 		/* Extended stats are optional */
5394 		if (rc)
5395 			return 0;
5396 	}
5397 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5398 	return 0;
5399 }
5400 
5401 static void bnxt_clear_ring_indices(struct bnxt *bp)
5402 {
5403 	int i, j;
5404 
5405 	if (!bp->bnapi)
5406 		return;
5407 
5408 	for (i = 0; i < bp->cp_nr_rings; i++) {
5409 		struct bnxt_napi *bnapi = bp->bnapi[i];
5410 		struct bnxt_cp_ring_info *cpr;
5411 		struct bnxt_rx_ring_info *rxr;
5412 		struct bnxt_tx_ring_info *txr;
5413 
5414 		if (!bnapi)
5415 			continue;
5416 
5417 		cpr = &bnapi->cp_ring;
5418 		cpr->cp_raw_cons = 0;
5419 
5420 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5421 			txr->tx_prod = 0;
5422 			txr->tx_cons = 0;
5423 			txr->tx_hw_cons = 0;
5424 		}
5425 
5426 		rxr = bnapi->rx_ring;
5427 		if (rxr) {
5428 			rxr->rx_prod = 0;
5429 			rxr->rx_agg_prod = 0;
5430 			rxr->rx_sw_agg_prod = 0;
5431 			rxr->rx_next_cons = 0;
5432 		}
5433 		bnapi->events = 0;
5434 	}
5435 }
5436 
5437 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5438 {
5439 	u8 type = fltr->type, flags = fltr->flags;
5440 
5441 	INIT_LIST_HEAD(&fltr->list);
5442 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5443 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5444 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5445 }
5446 
5447 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5448 {
5449 	if (!list_empty(&fltr->list))
5450 		list_del_init(&fltr->list);
5451 }
5452 
5453 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5454 {
5455 	struct bnxt_filter_base *usr_fltr, *tmp;
5456 
5457 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5458 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5459 			continue;
5460 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5461 	}
5462 }
5463 
5464 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5465 {
5466 	hlist_del(&fltr->hash);
5467 	bnxt_del_one_usr_fltr(bp, fltr);
5468 	if (fltr->flags) {
5469 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5470 		bp->ntp_fltr_count--;
5471 	}
5472 	kfree(fltr);
5473 }
5474 
5475 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5476 {
5477 	int i;
5478 
5479 	netdev_assert_locked_or_invisible(bp->dev);
5480 
5481 	/* Under netdev instance lock and all our NAPIs have been disabled.
5482 	 * It's safe to delete the hash table.
5483 	 */
5484 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5485 		struct hlist_head *head;
5486 		struct hlist_node *tmp;
5487 		struct bnxt_ntuple_filter *fltr;
5488 
5489 		head = &bp->ntp_fltr_hash_tbl[i];
5490 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5491 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5492 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5493 				     !list_empty(&fltr->base.list)))
5494 				continue;
5495 			bnxt_del_fltr(bp, &fltr->base);
5496 		}
5497 	}
5498 	if (!all)
5499 		return;
5500 
5501 	bitmap_free(bp->ntp_fltr_bmap);
5502 	bp->ntp_fltr_bmap = NULL;
5503 	bp->ntp_fltr_count = 0;
5504 }
5505 
5506 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5507 {
5508 	int i, rc = 0;
5509 
5510 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5511 		return 0;
5512 
5513 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5514 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5515 
5516 	bp->ntp_fltr_count = 0;
5517 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5518 
5519 	if (!bp->ntp_fltr_bmap)
5520 		rc = -ENOMEM;
5521 
5522 	return rc;
5523 }
5524 
5525 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5526 {
5527 	int i;
5528 
5529 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5530 		struct hlist_head *head;
5531 		struct hlist_node *tmp;
5532 		struct bnxt_l2_filter *fltr;
5533 
5534 		head = &bp->l2_fltr_hash_tbl[i];
5535 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5536 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5537 				     !list_empty(&fltr->base.list)))
5538 				continue;
5539 			bnxt_del_fltr(bp, &fltr->base);
5540 		}
5541 	}
5542 }
5543 
5544 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5545 {
5546 	int i;
5547 
5548 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5549 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5550 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5551 }
5552 
5553 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5554 {
5555 	bnxt_free_vnic_attributes(bp);
5556 	bnxt_free_tx_rings(bp);
5557 	bnxt_free_rx_rings(bp);
5558 	bnxt_free_cp_rings(bp);
5559 	bnxt_free_all_cp_arrays(bp);
5560 	bnxt_free_ntp_fltrs(bp, false);
5561 	bnxt_free_l2_filters(bp, false);
5562 	if (irq_re_init) {
5563 		bnxt_free_ring_stats(bp);
5564 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5565 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5566 			bnxt_free_port_stats(bp);
5567 		bnxt_free_ring_grps(bp);
5568 		bnxt_free_vnics(bp);
5569 		kfree(bp->tx_ring_map);
5570 		bp->tx_ring_map = NULL;
5571 		kfree(bp->tx_ring);
5572 		bp->tx_ring = NULL;
5573 		kfree(bp->rx_ring);
5574 		bp->rx_ring = NULL;
5575 		kfree(bp->bnapi);
5576 		bp->bnapi = NULL;
5577 	} else {
5578 		bnxt_clear_ring_indices(bp);
5579 	}
5580 }
5581 
5582 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5583 {
5584 	int i, j, rc, size, arr_size;
5585 	void *bnapi;
5586 
5587 	if (irq_re_init) {
5588 		/* Allocate bnapi mem pointer array and mem block for
5589 		 * all queues
5590 		 */
5591 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5592 				bp->cp_nr_rings);
5593 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5594 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5595 		if (!bnapi)
5596 			return -ENOMEM;
5597 
5598 		bp->bnapi = bnapi;
5599 		bnapi += arr_size;
5600 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5601 			bp->bnapi[i] = bnapi;
5602 			bp->bnapi[i]->index = i;
5603 			bp->bnapi[i]->bp = bp;
5604 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5605 				struct bnxt_cp_ring_info *cpr =
5606 					&bp->bnapi[i]->cp_ring;
5607 
5608 				cpr->cp_ring_struct.ring_mem.flags =
5609 					BNXT_RMEM_RING_PTE_FLAG;
5610 			}
5611 		}
5612 
5613 		bp->rx_ring = kzalloc_objs(struct bnxt_rx_ring_info,
5614 					   bp->rx_nr_rings);
5615 		if (!bp->rx_ring)
5616 			return -ENOMEM;
5617 
5618 		for (i = 0; i < bp->rx_nr_rings; i++) {
5619 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5620 
5621 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5622 				rxr->rx_ring_struct.ring_mem.flags =
5623 					BNXT_RMEM_RING_PTE_FLAG;
5624 				rxr->rx_agg_ring_struct.ring_mem.flags =
5625 					BNXT_RMEM_RING_PTE_FLAG;
5626 			} else {
5627 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5628 			}
5629 			rxr->bnapi = bp->bnapi[i];
5630 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5631 		}
5632 
5633 		bp->tx_ring = kzalloc_objs(struct bnxt_tx_ring_info,
5634 					   bp->tx_nr_rings);
5635 		if (!bp->tx_ring)
5636 			return -ENOMEM;
5637 
5638 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5639 					  GFP_KERNEL);
5640 
5641 		if (!bp->tx_ring_map)
5642 			return -ENOMEM;
5643 
5644 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5645 			j = 0;
5646 		else
5647 			j = bp->rx_nr_rings;
5648 
5649 		for (i = 0; i < bp->tx_nr_rings; i++) {
5650 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5651 			struct bnxt_napi *bnapi2;
5652 
5653 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5654 				txr->tx_ring_struct.ring_mem.flags =
5655 					BNXT_RMEM_RING_PTE_FLAG;
5656 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5657 			if (i >= bp->tx_nr_rings_xdp) {
5658 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5659 
5660 				bnapi2 = bp->bnapi[k];
5661 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5662 				txr->tx_napi_idx =
5663 					BNXT_RING_TO_TC(bp, txr->txq_index);
5664 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5665 				bnapi2->tx_int = bnxt_tx_int;
5666 			} else {
5667 				bnapi2 = bp->bnapi[j];
5668 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5669 				bnapi2->tx_ring[0] = txr;
5670 				bnapi2->tx_int = bnxt_tx_int_xdp;
5671 				j++;
5672 			}
5673 			txr->bnapi = bnapi2;
5674 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5675 				txr->tx_cpr = &bnapi2->cp_ring;
5676 		}
5677 
5678 		rc = bnxt_alloc_stats(bp);
5679 		if (rc)
5680 			goto alloc_mem_err;
5681 		bnxt_init_stats(bp);
5682 
5683 		rc = bnxt_alloc_ntp_fltrs(bp);
5684 		if (rc)
5685 			goto alloc_mem_err;
5686 
5687 		rc = bnxt_alloc_vnics(bp);
5688 		if (rc)
5689 			goto alloc_mem_err;
5690 	}
5691 
5692 	rc = bnxt_alloc_all_cp_arrays(bp);
5693 	if (rc)
5694 		goto alloc_mem_err;
5695 
5696 	bnxt_init_ring_struct(bp);
5697 
5698 	rc = bnxt_alloc_rx_rings(bp);
5699 	if (rc)
5700 		goto alloc_mem_err;
5701 
5702 	rc = bnxt_alloc_tx_rings(bp);
5703 	if (rc)
5704 		goto alloc_mem_err;
5705 
5706 	rc = bnxt_alloc_cp_rings(bp);
5707 	if (rc)
5708 		goto alloc_mem_err;
5709 
5710 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5711 						  BNXT_VNIC_MCAST_FLAG |
5712 						  BNXT_VNIC_UCAST_FLAG;
5713 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5714 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5715 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5716 
5717 	rc = bnxt_alloc_vnic_attributes(bp);
5718 	if (rc)
5719 		goto alloc_mem_err;
5720 	return 0;
5721 
5722 alloc_mem_err:
5723 	bnxt_free_mem(bp, true);
5724 	return rc;
5725 }
5726 
5727 static void bnxt_disable_int(struct bnxt *bp)
5728 {
5729 	int i;
5730 
5731 	if (!bp->bnapi)
5732 		return;
5733 
5734 	for (i = 0; i < bp->cp_nr_rings; i++) {
5735 		struct bnxt_napi *bnapi = bp->bnapi[i];
5736 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5737 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5738 
5739 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5740 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5741 	}
5742 }
5743 
5744 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5745 {
5746 	struct bnxt_napi *bnapi = bp->bnapi[n];
5747 	struct bnxt_cp_ring_info *cpr;
5748 
5749 	cpr = &bnapi->cp_ring;
5750 	return cpr->cp_ring_struct.map_idx;
5751 }
5752 
5753 static void bnxt_disable_int_sync(struct bnxt *bp)
5754 {
5755 	int i;
5756 
5757 	if (!bp->irq_tbl || !bp->bnapi)
5758 		return;
5759 
5760 	atomic_inc(&bp->intr_sem);
5761 
5762 	bnxt_disable_int(bp);
5763 	for (i = 0; i < bp->cp_nr_rings; i++) {
5764 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5765 
5766 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5767 	}
5768 }
5769 
5770 static void bnxt_enable_int(struct bnxt *bp)
5771 {
5772 	int i;
5773 
5774 	atomic_set(&bp->intr_sem, 0);
5775 	for (i = 0; i < bp->cp_nr_rings; i++) {
5776 		struct bnxt_napi *bnapi = bp->bnapi[i];
5777 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5778 
5779 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5780 	}
5781 }
5782 
5783 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5784 			    bool async_only)
5785 {
5786 	DECLARE_BITMAP(async_events_bmap, 256);
5787 	u32 *events = (u32 *)async_events_bmap;
5788 	struct hwrm_func_drv_rgtr_output *resp;
5789 	struct hwrm_func_drv_rgtr_input *req;
5790 	u32 flags;
5791 	int rc, i;
5792 
5793 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5794 	if (rc)
5795 		return rc;
5796 
5797 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5798 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5799 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5800 
5801 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5802 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5803 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5804 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5805 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5806 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5807 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5808 	if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2)
5809 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT;
5810 	req->flags = cpu_to_le32(flags);
5811 	req->ver_maj_8b = DRV_VER_MAJ;
5812 	req->ver_min_8b = DRV_VER_MIN;
5813 	req->ver_upd_8b = DRV_VER_UPD;
5814 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5815 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5816 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5817 
5818 	if (BNXT_PF(bp)) {
5819 		u32 data[8];
5820 		int i;
5821 
5822 		memset(data, 0, sizeof(data));
5823 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5824 			u16 cmd = bnxt_vf_req_snif[i];
5825 			unsigned int bit, idx;
5826 
5827 			if ((bp->fw_cap & BNXT_FW_CAP_LINK_ADMIN) &&
5828 			    cmd == HWRM_PORT_PHY_QCFG)
5829 				continue;
5830 
5831 			idx = cmd / 32;
5832 			bit = cmd % 32;
5833 			data[idx] |= 1 << bit;
5834 		}
5835 
5836 		for (i = 0; i < 8; i++)
5837 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5838 
5839 		req->enables |=
5840 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5841 	}
5842 
5843 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5844 		req->flags |= cpu_to_le32(
5845 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5846 
5847 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5848 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5849 		u16 event_id = bnxt_async_events_arr[i];
5850 
5851 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5852 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5853 			continue;
5854 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5855 		    !bp->ptp_cfg)
5856 			continue;
5857 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5858 	}
5859 	if (bmap && bmap_size) {
5860 		for (i = 0; i < bmap_size; i++) {
5861 			if (test_bit(i, bmap))
5862 				__set_bit(i, async_events_bmap);
5863 		}
5864 	}
5865 	for (i = 0; i < 8; i++)
5866 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5867 
5868 	if (async_only)
5869 		req->enables =
5870 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5871 
5872 	resp = hwrm_req_hold(bp, req);
5873 	rc = hwrm_req_send(bp, req);
5874 	if (!rc) {
5875 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5876 		if (resp->flags &
5877 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5878 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5879 	}
5880 	hwrm_req_drop(bp, req);
5881 	return rc;
5882 }
5883 
5884 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5885 {
5886 	struct hwrm_func_drv_unrgtr_input *req;
5887 	int rc;
5888 
5889 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5890 		return 0;
5891 
5892 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5893 	if (rc)
5894 		return rc;
5895 	return hwrm_req_send(bp, req);
5896 }
5897 
5898 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5899 
5900 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5901 {
5902 	struct hwrm_tunnel_dst_port_free_input *req;
5903 	int rc;
5904 
5905 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5906 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5907 		return 0;
5908 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5909 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5910 		return 0;
5911 
5912 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5913 	if (rc)
5914 		return rc;
5915 
5916 	req->tunnel_type = tunnel_type;
5917 
5918 	switch (tunnel_type) {
5919 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5920 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5921 		bp->vxlan_port = 0;
5922 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5923 		break;
5924 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5925 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5926 		bp->nge_port = 0;
5927 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5928 		break;
5929 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5930 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5931 		bp->vxlan_gpe_port = 0;
5932 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5933 		break;
5934 	default:
5935 		break;
5936 	}
5937 
5938 	rc = hwrm_req_send(bp, req);
5939 	if (rc)
5940 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5941 			   rc);
5942 	if (bp->flags & BNXT_FLAG_TPA)
5943 		bnxt_set_tpa(bp, true);
5944 	return rc;
5945 }
5946 
5947 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5948 					   u8 tunnel_type)
5949 {
5950 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5951 	struct hwrm_tunnel_dst_port_alloc_input *req;
5952 	int rc;
5953 
5954 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5955 	if (rc)
5956 		return rc;
5957 
5958 	req->tunnel_type = tunnel_type;
5959 	req->tunnel_dst_port_val = port;
5960 
5961 	resp = hwrm_req_hold(bp, req);
5962 	rc = hwrm_req_send(bp, req);
5963 	if (rc) {
5964 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5965 			   rc);
5966 		goto err_out;
5967 	}
5968 
5969 	switch (tunnel_type) {
5970 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5971 		bp->vxlan_port = port;
5972 		bp->vxlan_fw_dst_port_id =
5973 			le16_to_cpu(resp->tunnel_dst_port_id);
5974 		break;
5975 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5976 		bp->nge_port = port;
5977 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5978 		break;
5979 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5980 		bp->vxlan_gpe_port = port;
5981 		bp->vxlan_gpe_fw_dst_port_id =
5982 			le16_to_cpu(resp->tunnel_dst_port_id);
5983 		break;
5984 	default:
5985 		break;
5986 	}
5987 	if (bp->flags & BNXT_FLAG_TPA)
5988 		bnxt_set_tpa(bp, true);
5989 
5990 err_out:
5991 	hwrm_req_drop(bp, req);
5992 	return rc;
5993 }
5994 
5995 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5996 {
5997 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5998 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5999 	int rc;
6000 
6001 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
6002 	if (rc)
6003 		return rc;
6004 
6005 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6006 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
6007 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
6008 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
6009 	}
6010 	req->mask = cpu_to_le32(vnic->rx_mask);
6011 	return hwrm_req_send_silent(bp, req);
6012 }
6013 
6014 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6015 {
6016 	if (!atomic_dec_and_test(&fltr->refcnt))
6017 		return;
6018 	spin_lock_bh(&bp->ntp_fltr_lock);
6019 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
6020 		spin_unlock_bh(&bp->ntp_fltr_lock);
6021 		return;
6022 	}
6023 	hlist_del_rcu(&fltr->base.hash);
6024 	bnxt_del_one_usr_fltr(bp, &fltr->base);
6025 	if (fltr->base.flags) {
6026 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
6027 		bp->ntp_fltr_count--;
6028 	}
6029 	spin_unlock_bh(&bp->ntp_fltr_lock);
6030 	kfree_rcu(fltr, base.rcu);
6031 }
6032 
6033 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
6034 						      struct bnxt_l2_key *key,
6035 						      u32 idx)
6036 {
6037 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
6038 	struct bnxt_l2_filter *fltr;
6039 
6040 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
6041 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
6042 
6043 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
6044 		    l2_key->vlan == key->vlan)
6045 			return fltr;
6046 	}
6047 	return NULL;
6048 }
6049 
6050 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
6051 						    struct bnxt_l2_key *key,
6052 						    u32 idx)
6053 {
6054 	struct bnxt_l2_filter *fltr = NULL;
6055 
6056 	rcu_read_lock();
6057 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6058 	if (fltr)
6059 		atomic_inc(&fltr->refcnt);
6060 	rcu_read_unlock();
6061 	return fltr;
6062 }
6063 
6064 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
6065 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
6066 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
6067 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
6068 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
6069 
6070 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
6071 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
6072 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
6073 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
6074 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
6075 
6076 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
6077 {
6078 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
6079 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
6080 			return sizeof(fkeys->addrs.v4addrs) +
6081 			       sizeof(fkeys->ports);
6082 
6083 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
6084 			return sizeof(fkeys->addrs.v4addrs);
6085 	}
6086 
6087 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
6088 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
6089 			return sizeof(fkeys->addrs.v6addrs) +
6090 			       sizeof(fkeys->ports);
6091 
6092 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
6093 			return sizeof(fkeys->addrs.v6addrs);
6094 	}
6095 
6096 	return 0;
6097 }
6098 
6099 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
6100 			 const unsigned char *key)
6101 {
6102 	u64 prefix = bp->toeplitz_prefix, hash = 0;
6103 	struct bnxt_ipv4_tuple tuple4;
6104 	struct bnxt_ipv6_tuple tuple6;
6105 	int i, j, len = 0;
6106 	u8 *four_tuple;
6107 
6108 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
6109 	if (!len)
6110 		return 0;
6111 
6112 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
6113 		tuple4.v4addrs = fkeys->addrs.v4addrs;
6114 		tuple4.ports = fkeys->ports;
6115 		four_tuple = (unsigned char *)&tuple4;
6116 	} else {
6117 		tuple6.v6addrs = fkeys->addrs.v6addrs;
6118 		tuple6.ports = fkeys->ports;
6119 		four_tuple = (unsigned char *)&tuple6;
6120 	}
6121 
6122 	for (i = 0, j = 8; i < len; i++, j++) {
6123 		u8 byte = four_tuple[i];
6124 		int bit;
6125 
6126 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
6127 			if (byte & 0x80)
6128 				hash ^= prefix;
6129 		}
6130 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
6131 	}
6132 
6133 	/* The valid part of the hash is in the upper 32 bits. */
6134 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
6135 }
6136 
6137 #ifdef CONFIG_RFS_ACCEL
6138 static struct bnxt_l2_filter *
6139 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
6140 {
6141 	struct bnxt_l2_filter *fltr;
6142 	u32 idx;
6143 
6144 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6145 	      BNXT_L2_FLTR_HASH_MASK;
6146 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6147 	return fltr;
6148 }
6149 #endif
6150 
6151 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
6152 			       struct bnxt_l2_key *key, u32 idx)
6153 {
6154 	struct hlist_head *head;
6155 
6156 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
6157 	fltr->l2_key.vlan = key->vlan;
6158 	fltr->base.type = BNXT_FLTR_TYPE_L2;
6159 	if (fltr->base.flags) {
6160 		int bit_id;
6161 
6162 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6163 						 bp->max_fltr, 0);
6164 		if (bit_id < 0)
6165 			return -ENOMEM;
6166 		fltr->base.sw_id = (u16)bit_id;
6167 		bp->ntp_fltr_count++;
6168 	}
6169 	head = &bp->l2_fltr_hash_tbl[idx];
6170 	hlist_add_head_rcu(&fltr->base.hash, head);
6171 	bnxt_insert_usr_fltr(bp, &fltr->base);
6172 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
6173 	atomic_set(&fltr->refcnt, 1);
6174 	return 0;
6175 }
6176 
6177 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
6178 						   struct bnxt_l2_key *key,
6179 						   gfp_t gfp)
6180 {
6181 	struct bnxt_l2_filter *fltr;
6182 	u32 idx;
6183 	int rc;
6184 
6185 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6186 	      BNXT_L2_FLTR_HASH_MASK;
6187 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6188 	if (fltr)
6189 		return fltr;
6190 
6191 	fltr = kzalloc_obj(*fltr, gfp);
6192 	if (!fltr)
6193 		return ERR_PTR(-ENOMEM);
6194 	spin_lock_bh(&bp->ntp_fltr_lock);
6195 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6196 	spin_unlock_bh(&bp->ntp_fltr_lock);
6197 	if (rc) {
6198 		bnxt_del_l2_filter(bp, fltr);
6199 		fltr = ERR_PTR(rc);
6200 	}
6201 	return fltr;
6202 }
6203 
6204 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
6205 						struct bnxt_l2_key *key,
6206 						u16 flags)
6207 {
6208 	struct bnxt_l2_filter *fltr;
6209 	u32 idx;
6210 	int rc;
6211 
6212 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6213 	      BNXT_L2_FLTR_HASH_MASK;
6214 	spin_lock_bh(&bp->ntp_fltr_lock);
6215 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6216 	if (fltr) {
6217 		fltr = ERR_PTR(-EEXIST);
6218 		goto l2_filter_exit;
6219 	}
6220 	fltr = kzalloc_obj(*fltr, GFP_ATOMIC);
6221 	if (!fltr) {
6222 		fltr = ERR_PTR(-ENOMEM);
6223 		goto l2_filter_exit;
6224 	}
6225 	fltr->base.flags = flags;
6226 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6227 	if (rc) {
6228 		spin_unlock_bh(&bp->ntp_fltr_lock);
6229 		bnxt_del_l2_filter(bp, fltr);
6230 		return ERR_PTR(rc);
6231 	}
6232 
6233 l2_filter_exit:
6234 	spin_unlock_bh(&bp->ntp_fltr_lock);
6235 	return fltr;
6236 }
6237 
6238 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
6239 {
6240 #ifdef CONFIG_BNXT_SRIOV
6241 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
6242 
6243 	return vf->fw_fid;
6244 #else
6245 	return INVALID_HW_RING_ID;
6246 #endif
6247 }
6248 
6249 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6250 {
6251 	struct hwrm_cfa_l2_filter_free_input *req;
6252 	u16 target_id = 0xffff;
6253 	int rc;
6254 
6255 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6256 		struct bnxt_pf_info *pf = &bp->pf;
6257 
6258 		if (fltr->base.vf_idx >= pf->active_vfs)
6259 			return -EINVAL;
6260 
6261 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6262 		if (target_id == INVALID_HW_RING_ID)
6263 			return -EINVAL;
6264 	}
6265 
6266 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
6267 	if (rc)
6268 		return rc;
6269 
6270 	req->target_id = cpu_to_le16(target_id);
6271 	req->l2_filter_id = fltr->base.filter_id;
6272 	return hwrm_req_send(bp, req);
6273 }
6274 
6275 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6276 {
6277 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6278 	struct hwrm_cfa_l2_filter_alloc_input *req;
6279 	u16 target_id = 0xffff;
6280 	int rc;
6281 
6282 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6283 		struct bnxt_pf_info *pf = &bp->pf;
6284 
6285 		if (fltr->base.vf_idx >= pf->active_vfs)
6286 			return -EINVAL;
6287 
6288 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6289 	}
6290 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6291 	if (rc)
6292 		return rc;
6293 
6294 	req->target_id = cpu_to_le16(target_id);
6295 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6296 
6297 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6298 		req->flags |=
6299 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6300 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6301 	req->enables =
6302 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6303 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6304 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6305 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6306 	eth_broadcast_addr(req->l2_addr_mask);
6307 
6308 	if (fltr->l2_key.vlan) {
6309 		req->enables |=
6310 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6311 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6312 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6313 		req->num_vlans = 1;
6314 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6315 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6316 	}
6317 
6318 	resp = hwrm_req_hold(bp, req);
6319 	rc = hwrm_req_send(bp, req);
6320 	if (!rc) {
6321 		fltr->base.filter_id = resp->l2_filter_id;
6322 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6323 	}
6324 	hwrm_req_drop(bp, req);
6325 	return rc;
6326 }
6327 
6328 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6329 				     struct bnxt_ntuple_filter *fltr)
6330 {
6331 	struct hwrm_cfa_ntuple_filter_free_input *req;
6332 	int rc;
6333 
6334 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6335 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
6336 		return 0;
6337 
6338 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6339 	if (rc)
6340 		return rc;
6341 
6342 	req->ntuple_filter_id = fltr->base.filter_id;
6343 	return hwrm_req_send(bp, req);
6344 }
6345 
6346 #define BNXT_NTP_FLTR_FLAGS					\
6347 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6348 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6349 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6350 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6351 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6352 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6353 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6354 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6355 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6356 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6357 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6358 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6359 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6360 
6361 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6362 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6363 
6364 void bnxt_fill_ipv6_mask(__be32 mask[4])
6365 {
6366 	int i;
6367 
6368 	for (i = 0; i < 4; i++)
6369 		mask[i] = cpu_to_be32(~0);
6370 }
6371 
6372 static void
6373 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6374 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6375 			  struct bnxt_ntuple_filter *fltr)
6376 {
6377 	u16 rxq = fltr->base.rxq;
6378 
6379 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6380 		struct ethtool_rxfh_context *ctx;
6381 		struct bnxt_rss_ctx *rss_ctx;
6382 		struct bnxt_vnic_info *vnic;
6383 
6384 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6385 			      fltr->base.fw_vnic_id);
6386 		if (ctx) {
6387 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6388 			vnic = &rss_ctx->vnic;
6389 
6390 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6391 		}
6392 		return;
6393 	}
6394 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6395 		struct bnxt_vnic_info *vnic;
6396 		u32 enables;
6397 
6398 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6399 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6400 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6401 		req->enables |= cpu_to_le32(enables);
6402 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6403 	} else {
6404 		u32 flags;
6405 
6406 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6407 		req->flags |= cpu_to_le32(flags);
6408 		req->dst_id = cpu_to_le16(rxq);
6409 	}
6410 }
6411 
6412 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6413 				      struct bnxt_ntuple_filter *fltr)
6414 {
6415 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6416 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6417 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6418 	struct flow_keys *keys = &fltr->fkeys;
6419 	struct bnxt_l2_filter *l2_fltr;
6420 	struct bnxt_vnic_info *vnic;
6421 	int rc;
6422 
6423 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6424 	if (rc)
6425 		return rc;
6426 
6427 	l2_fltr = fltr->l2_fltr;
6428 	req->l2_filter_id = l2_fltr->base.filter_id;
6429 
6430 	if (fltr->base.flags & BNXT_ACT_DROP) {
6431 		req->flags =
6432 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6433 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6434 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6435 	} else {
6436 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6437 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6438 	}
6439 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6440 
6441 	req->ethertype = htons(ETH_P_IP);
6442 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6443 	req->ip_protocol = keys->basic.ip_proto;
6444 
6445 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6446 		req->ethertype = htons(ETH_P_IPV6);
6447 		req->ip_addr_type =
6448 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6449 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6450 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6451 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6452 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6453 	} else {
6454 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6455 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6456 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6457 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6458 	}
6459 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6460 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6461 		req->tunnel_type =
6462 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6463 	}
6464 
6465 	req->src_port = keys->ports.src;
6466 	req->src_port_mask = masks->ports.src;
6467 	req->dst_port = keys->ports.dst;
6468 	req->dst_port_mask = masks->ports.dst;
6469 
6470 	resp = hwrm_req_hold(bp, req);
6471 	rc = hwrm_req_send(bp, req);
6472 	if (!rc)
6473 		fltr->base.filter_id = resp->ntuple_filter_id;
6474 	hwrm_req_drop(bp, req);
6475 	return rc;
6476 }
6477 
6478 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6479 				     const u8 *mac_addr)
6480 {
6481 	struct bnxt_l2_filter *fltr;
6482 	struct bnxt_l2_key key;
6483 	int rc;
6484 
6485 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6486 	key.vlan = 0;
6487 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6488 	if (IS_ERR(fltr))
6489 		return PTR_ERR(fltr);
6490 
6491 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6492 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6493 	if (rc)
6494 		bnxt_del_l2_filter(bp, fltr);
6495 	else
6496 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6497 	return rc;
6498 }
6499 
6500 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6501 {
6502 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6503 
6504 	/* Any associated ntuple filters will also be cleared by firmware. */
6505 	for (i = 0; i < num_of_vnics; i++) {
6506 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6507 
6508 		for (j = 0; j < vnic->uc_filter_count; j++) {
6509 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6510 
6511 			bnxt_hwrm_l2_filter_free(bp, fltr);
6512 			bnxt_del_l2_filter(bp, fltr);
6513 		}
6514 		vnic->uc_filter_count = 0;
6515 	}
6516 }
6517 
6518 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6519 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6520 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6521 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6522 
6523 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6524 					   struct hwrm_vnic_tpa_cfg_input *req)
6525 {
6526 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6527 
6528 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6529 		return;
6530 
6531 	if (bp->vxlan_port)
6532 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6533 	if (bp->vxlan_gpe_port)
6534 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6535 	if (bp->nge_port)
6536 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6537 
6538 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6539 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6540 }
6541 
6542 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6543 			   u32 tpa_flags)
6544 {
6545 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6546 	struct hwrm_vnic_tpa_cfg_input *req;
6547 	int rc;
6548 
6549 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6550 		return 0;
6551 
6552 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6553 	if (rc)
6554 		return rc;
6555 
6556 	if (tpa_flags) {
6557 		u16 mss = bp->dev->mtu - 40;
6558 		u32 nsegs, n, segs = 0, flags;
6559 
6560 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6561 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6562 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6563 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6564 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6565 		if (tpa_flags & BNXT_FLAG_GRO)
6566 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6567 
6568 		req->flags = cpu_to_le32(flags);
6569 
6570 		req->enables =
6571 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6572 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6573 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6574 
6575 		/* Number of segs are log2 units, and first packet is not
6576 		 * included as part of this units.
6577 		 */
6578 		if (mss <= BNXT_RX_PAGE_SIZE) {
6579 			n = BNXT_RX_PAGE_SIZE / mss;
6580 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6581 		} else {
6582 			n = mss / BNXT_RX_PAGE_SIZE;
6583 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6584 				n++;
6585 			nsegs = (MAX_SKB_FRAGS - n) / n;
6586 		}
6587 
6588 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6589 			segs = MAX_TPA_SEGS_P5;
6590 			max_aggs = bp->max_tpa;
6591 		} else {
6592 			segs = ilog2(nsegs);
6593 		}
6594 		req->max_agg_segs = cpu_to_le16(segs);
6595 		req->max_aggs = cpu_to_le16(max_aggs);
6596 
6597 		req->min_agg_len = cpu_to_le32(512);
6598 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6599 	}
6600 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6601 
6602 	return hwrm_req_send(bp, req);
6603 }
6604 
6605 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6606 {
6607 	struct bnxt_ring_grp_info *grp_info;
6608 
6609 	grp_info = &bp->grp_info[ring->grp_idx];
6610 	return grp_info->cp_fw_ring_id;
6611 }
6612 
6613 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6614 {
6615 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6616 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6617 	else
6618 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6619 }
6620 
6621 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6622 {
6623 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6624 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6625 	else
6626 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6627 }
6628 
6629 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6630 {
6631 	int entries;
6632 
6633 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6634 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6635 	else
6636 		entries = HW_HASH_INDEX_SIZE;
6637 
6638 	bp->rss_indir_tbl_entries = entries;
6639 	bp->rss_indir_tbl =
6640 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6641 	if (!bp->rss_indir_tbl)
6642 		return -ENOMEM;
6643 
6644 	return 0;
6645 }
6646 
6647 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6648 				 struct ethtool_rxfh_context *rss_ctx)
6649 {
6650 	u16 max_rings, max_entries, pad, i;
6651 	u32 *rss_indir_tbl;
6652 
6653 	if (!bp->rx_nr_rings)
6654 		return;
6655 
6656 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6657 		max_rings = bp->rx_nr_rings - 1;
6658 	else
6659 		max_rings = bp->rx_nr_rings;
6660 
6661 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6662 	if (rss_ctx)
6663 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6664 	else
6665 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6666 
6667 	for (i = 0; i < max_entries; i++)
6668 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6669 
6670 	pad = bp->rss_indir_tbl_entries - max_entries;
6671 	if (pad)
6672 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6673 }
6674 
6675 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6676 {
6677 	u32 i, tbl_size, max_ring = 0;
6678 
6679 	if (!bp->rss_indir_tbl)
6680 		return 0;
6681 
6682 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6683 	for (i = 0; i < tbl_size; i++)
6684 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6685 	return max_ring;
6686 }
6687 
6688 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6689 {
6690 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6691 		if (!rx_rings)
6692 			return 0;
6693 		if (bp->rss_cap & BNXT_RSS_CAP_LARGE_RSS_CTX)
6694 			return BNXT_RSS_TABLE_MAX_TBL_P5;
6695 
6696 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6697 					       BNXT_RSS_TABLE_ENTRIES_P5);
6698 	}
6699 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6700 		return 2;
6701 	return 1;
6702 }
6703 
6704 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6705 {
6706 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6707 	u16 i, j;
6708 
6709 	/* Fill the RSS indirection table with ring group ids */
6710 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6711 		if (!no_rss)
6712 			j = bp->rss_indir_tbl[i];
6713 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6714 	}
6715 }
6716 
6717 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6718 				    struct bnxt_vnic_info *vnic)
6719 {
6720 	__le16 *ring_tbl = vnic->rss_table;
6721 	struct bnxt_rx_ring_info *rxr;
6722 	u16 tbl_size, i;
6723 
6724 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6725 
6726 	for (i = 0; i < tbl_size; i++) {
6727 		u16 ring_id, j;
6728 
6729 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6730 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6731 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6732 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6733 		else
6734 			j = bp->rss_indir_tbl[i];
6735 		rxr = &bp->rx_ring[j];
6736 
6737 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6738 		*ring_tbl++ = cpu_to_le16(ring_id);
6739 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6740 		*ring_tbl++ = cpu_to_le16(ring_id);
6741 	}
6742 }
6743 
6744 static void
6745 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6746 			 struct bnxt_vnic_info *vnic)
6747 {
6748 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6749 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6750 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6751 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6752 	} else {
6753 		bnxt_fill_hw_rss_tbl(bp, vnic);
6754 	}
6755 
6756 	if (bp->rss_hash_delta) {
6757 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6758 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6759 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6760 		else
6761 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6762 	} else {
6763 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6764 	}
6765 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6766 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6767 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6768 }
6769 
6770 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6771 				  bool set_rss)
6772 {
6773 	struct hwrm_vnic_rss_cfg_input *req;
6774 	int rc;
6775 
6776 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6777 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6778 		return 0;
6779 
6780 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6781 	if (rc)
6782 		return rc;
6783 
6784 	if (set_rss)
6785 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6786 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6787 	return hwrm_req_send(bp, req);
6788 }
6789 
6790 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6791 				     struct bnxt_vnic_info *vnic, bool set_rss)
6792 {
6793 	struct hwrm_vnic_rss_cfg_input *req;
6794 	dma_addr_t ring_tbl_map;
6795 	u32 i, nr_ctxs;
6796 	int rc;
6797 
6798 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6799 	if (rc)
6800 		return rc;
6801 
6802 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6803 	if (!set_rss)
6804 		return hwrm_req_send(bp, req);
6805 
6806 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6807 	ring_tbl_map = vnic->rss_table_dma_addr;
6808 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6809 
6810 	hwrm_req_hold(bp, req);
6811 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6812 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6813 		req->ring_table_pair_index = i;
6814 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6815 		rc = hwrm_req_send(bp, req);
6816 		if (rc)
6817 			goto exit;
6818 	}
6819 
6820 exit:
6821 	hwrm_req_drop(bp, req);
6822 	return rc;
6823 }
6824 
6825 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6826 {
6827 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6828 	struct hwrm_vnic_rss_qcfg_output *resp;
6829 	struct hwrm_vnic_rss_qcfg_input *req;
6830 
6831 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6832 		return;
6833 
6834 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6835 	/* all contexts configured to same hash_type, zero always exists */
6836 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6837 	resp = hwrm_req_hold(bp, req);
6838 	if (!hwrm_req_send(bp, req)) {
6839 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6840 		bp->rss_hash_delta = 0;
6841 	}
6842 	hwrm_req_drop(bp, req);
6843 }
6844 
6845 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6846 {
6847 	u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh;
6848 	struct hwrm_vnic_plcmodes_cfg_input *req;
6849 	int rc;
6850 
6851 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6852 	if (rc)
6853 		return rc;
6854 
6855 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6856 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6857 	req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6858 
6859 	if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
6860 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6861 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6862 		req->enables |=
6863 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6864 		req->hds_threshold = cpu_to_le16(hds_thresh);
6865 	}
6866 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6867 	return hwrm_req_send(bp, req);
6868 }
6869 
6870 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6871 					struct bnxt_vnic_info *vnic,
6872 					u16 ctx_idx)
6873 {
6874 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6875 
6876 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6877 		return;
6878 
6879 	req->rss_cos_lb_ctx_id =
6880 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6881 
6882 	hwrm_req_send(bp, req);
6883 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6884 }
6885 
6886 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6887 {
6888 	int i, j;
6889 
6890 	for (i = 0; i < bp->nr_vnics; i++) {
6891 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6892 
6893 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6894 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6895 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6896 		}
6897 	}
6898 	bp->rsscos_nr_ctxs = 0;
6899 }
6900 
6901 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6902 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6903 {
6904 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6905 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6906 	int rc;
6907 
6908 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6909 	if (rc)
6910 		return rc;
6911 
6912 	resp = hwrm_req_hold(bp, req);
6913 	rc = hwrm_req_send(bp, req);
6914 	if (!rc)
6915 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6916 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6917 	hwrm_req_drop(bp, req);
6918 
6919 	return rc;
6920 }
6921 
6922 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6923 {
6924 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6925 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6926 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6927 }
6928 
6929 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6930 {
6931 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6932 	struct hwrm_vnic_cfg_input *req;
6933 	unsigned int ring = 0, grp_idx;
6934 	u16 def_vlan = 0;
6935 	int rc;
6936 
6937 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6938 	if (rc)
6939 		return rc;
6940 
6941 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6942 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6943 
6944 		req->default_rx_ring_id =
6945 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6946 		req->default_cmpl_ring_id =
6947 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6948 		req->enables =
6949 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6950 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6951 		goto vnic_mru;
6952 	}
6953 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6954 	/* Only RSS support for now TBD: COS & LB */
6955 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6956 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6957 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6958 					   VNIC_CFG_REQ_ENABLES_MRU);
6959 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6960 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6961 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6962 					   VNIC_CFG_REQ_ENABLES_MRU);
6963 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6964 	} else {
6965 		req->rss_rule = cpu_to_le16(0xffff);
6966 	}
6967 
6968 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6969 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6970 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6971 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6972 	} else {
6973 		req->cos_rule = cpu_to_le16(0xffff);
6974 	}
6975 
6976 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6977 		ring = 0;
6978 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6979 		ring = vnic->vnic_id - 1;
6980 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6981 		ring = bp->rx_nr_rings - 1;
6982 
6983 	grp_idx = bp->rx_ring[ring].bnapi->index;
6984 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6985 	req->lb_rule = cpu_to_le16(0xffff);
6986 vnic_mru:
6987 	vnic->mru = bp->dev->mtu + VLAN_ETH_HLEN;
6988 	req->mru = cpu_to_le16(vnic->mru);
6989 
6990 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6991 #ifdef CONFIG_BNXT_SRIOV
6992 	if (BNXT_VF(bp))
6993 		def_vlan = bp->vf.vlan;
6994 #endif
6995 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6996 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6997 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT &&
6998 	    bnxt_ulp_registered(bp->edev[BNXT_AUXDEV_RDMA]))
6999 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
7000 
7001 	return hwrm_req_send(bp, req);
7002 }
7003 
7004 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
7005 				    struct bnxt_vnic_info *vnic)
7006 {
7007 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
7008 		struct hwrm_vnic_free_input *req;
7009 
7010 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
7011 			return;
7012 
7013 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
7014 
7015 		hwrm_req_send(bp, req);
7016 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
7017 	}
7018 }
7019 
7020 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
7021 {
7022 	u16 i;
7023 
7024 	for (i = 0; i < bp->nr_vnics; i++)
7025 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
7026 }
7027 
7028 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
7029 			 unsigned int start_rx_ring_idx,
7030 			 unsigned int nr_rings)
7031 {
7032 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
7033 	struct hwrm_vnic_alloc_output *resp;
7034 	struct hwrm_vnic_alloc_input *req;
7035 	int rc;
7036 
7037 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
7038 	if (rc)
7039 		return rc;
7040 
7041 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7042 		goto vnic_no_ring_grps;
7043 
7044 	/* map ring groups to this vnic */
7045 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
7046 		grp_idx = bp->rx_ring[i].bnapi->index;
7047 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
7048 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
7049 				   j, nr_rings);
7050 			break;
7051 		}
7052 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
7053 	}
7054 
7055 vnic_no_ring_grps:
7056 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
7057 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
7058 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
7059 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
7060 
7061 	resp = hwrm_req_hold(bp, req);
7062 	rc = hwrm_req_send(bp, req);
7063 	if (!rc)
7064 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
7065 	hwrm_req_drop(bp, req);
7066 	return rc;
7067 }
7068 
7069 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
7070 {
7071 	struct hwrm_vnic_qcaps_output *resp;
7072 	struct hwrm_vnic_qcaps_input *req;
7073 	int rc;
7074 
7075 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
7076 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
7077 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
7078 	if (bp->hwrm_spec_code < 0x10600)
7079 		return 0;
7080 
7081 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
7082 	if (rc)
7083 		return rc;
7084 
7085 	resp = hwrm_req_hold(bp, req);
7086 	rc = hwrm_req_send(bp, req);
7087 	if (!rc) {
7088 		u32 flags = le32_to_cpu(resp->flags);
7089 
7090 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
7091 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
7092 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
7093 		if (flags &
7094 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
7095 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
7096 
7097 		/* Older P5 fw before EXT_HW_STATS support did not set
7098 		 * VLAN_STRIP_CAP properly.
7099 		 */
7100 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
7101 		    (BNXT_CHIP_P5(bp) &&
7102 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
7103 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
7104 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
7105 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
7106 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
7107 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
7108 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
7109 		if (bp->max_tpa_v2) {
7110 			if (BNXT_CHIP_P5(bp))
7111 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
7112 			else
7113 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
7114 		}
7115 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
7116 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
7117 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
7118 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
7119 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
7120 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
7121 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
7122 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
7123 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
7124 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
7125 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPV6_FLOW_LABEL_CAP)
7126 			bp->rss_cap |= BNXT_RSS_CAP_IPV6_FLOW_LABEL_RSS_CAP;
7127 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
7128 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
7129 	}
7130 	hwrm_req_drop(bp, req);
7131 	return rc;
7132 }
7133 
7134 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
7135 {
7136 	struct hwrm_ring_grp_alloc_output *resp;
7137 	struct hwrm_ring_grp_alloc_input *req;
7138 	int rc;
7139 	u16 i;
7140 
7141 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7142 		return 0;
7143 
7144 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
7145 	if (rc)
7146 		return rc;
7147 
7148 	resp = hwrm_req_hold(bp, req);
7149 	for (i = 0; i < bp->rx_nr_rings; i++) {
7150 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
7151 
7152 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
7153 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
7154 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
7155 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
7156 
7157 		rc = hwrm_req_send(bp, req);
7158 
7159 		if (rc)
7160 			break;
7161 
7162 		bp->grp_info[grp_idx].fw_grp_id =
7163 			le32_to_cpu(resp->ring_group_id);
7164 	}
7165 	hwrm_req_drop(bp, req);
7166 	return rc;
7167 }
7168 
7169 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
7170 {
7171 	struct hwrm_ring_grp_free_input *req;
7172 	u16 i;
7173 
7174 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7175 		return;
7176 
7177 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
7178 		return;
7179 
7180 	hwrm_req_hold(bp, req);
7181 	for (i = 0; i < bp->cp_nr_rings; i++) {
7182 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
7183 			continue;
7184 		req->ring_group_id =
7185 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
7186 
7187 		hwrm_req_send(bp, req);
7188 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
7189 	}
7190 	hwrm_req_drop(bp, req);
7191 }
7192 
7193 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type,
7194 				       struct hwrm_ring_alloc_input *req,
7195 				       struct bnxt_rx_ring_info *rxr,
7196 				       struct bnxt_ring_struct *ring)
7197 {
7198 	struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx];
7199 	u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID |
7200 		      RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID;
7201 
7202 	if (ring_type == HWRM_RING_ALLOC_AGG) {
7203 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
7204 		req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
7205 		req->rx_buf_size = cpu_to_le16(rxr->rx_page_size);
7206 		enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID;
7207 	} else {
7208 		req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
7209 		if (NET_IP_ALIGN == 2)
7210 			req->flags =
7211 				cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD);
7212 	}
7213 	req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7214 	req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7215 	req->enables |= cpu_to_le32(enables);
7216 }
7217 
7218 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
7219 				    struct bnxt_rx_ring_info *rxr,
7220 				    struct bnxt_ring_struct *ring,
7221 				    u32 ring_type, u32 map_index)
7222 {
7223 	struct hwrm_ring_alloc_output *resp;
7224 	struct hwrm_ring_alloc_input *req;
7225 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
7226 	struct bnxt_ring_grp_info *grp_info;
7227 	int rc, err = 0;
7228 	u16 ring_id;
7229 
7230 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
7231 	if (rc)
7232 		goto exit;
7233 
7234 	req->enables = 0;
7235 	if (rmem->nr_pages > 1) {
7236 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
7237 		/* Page size is in log2 units */
7238 		req->page_size = BNXT_PAGE_SHIFT;
7239 		req->page_tbl_depth = 1;
7240 	} else {
7241 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
7242 	}
7243 	req->fbo = 0;
7244 	/* Association of ring index with doorbell index and MSIX number */
7245 	req->logical_id = cpu_to_le16(map_index);
7246 
7247 	switch (ring_type) {
7248 	case HWRM_RING_ALLOC_TX: {
7249 		struct bnxt_tx_ring_info *txr;
7250 		u16 flags = 0;
7251 
7252 		txr = container_of(ring, struct bnxt_tx_ring_info,
7253 				   tx_ring_struct);
7254 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
7255 		/* Association of transmit ring with completion ring */
7256 		grp_info = &bp->grp_info[ring->grp_idx];
7257 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
7258 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
7259 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7260 		req->queue_id = cpu_to_le16(ring->queue_id);
7261 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
7262 			req->cmpl_coal_cnt =
7263 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
7264 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
7265 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
7266 		req->flags = cpu_to_le16(flags);
7267 		break;
7268 	}
7269 	case HWRM_RING_ALLOC_RX:
7270 	case HWRM_RING_ALLOC_AGG:
7271 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
7272 		req->length = (ring_type == HWRM_RING_ALLOC_RX) ?
7273 			      cpu_to_le32(bp->rx_ring_mask + 1) :
7274 			      cpu_to_le32(bp->rx_agg_ring_mask + 1);
7275 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7276 			bnxt_set_rx_ring_params_p5(bp, ring_type, req,
7277 						   rxr, ring);
7278 		break;
7279 	case HWRM_RING_ALLOC_CMPL:
7280 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7281 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7282 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7283 			/* Association of cp ring with nq */
7284 			grp_info = &bp->grp_info[map_index];
7285 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7286 			req->cq_handle = cpu_to_le64(ring->handle);
7287 			req->enables |= cpu_to_le32(
7288 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7289 		} else {
7290 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7291 		}
7292 		break;
7293 	case HWRM_RING_ALLOC_NQ:
7294 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7295 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7296 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7297 		break;
7298 	default:
7299 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7300 			   ring_type);
7301 		return -EINVAL;
7302 	}
7303 
7304 	resp = hwrm_req_hold(bp, req);
7305 	rc = hwrm_req_send(bp, req);
7306 	err = le16_to_cpu(resp->error_code);
7307 	ring_id = le16_to_cpu(resp->ring_id);
7308 	hwrm_req_drop(bp, req);
7309 
7310 exit:
7311 	if (rc || err) {
7312 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7313 			   ring_type, rc, err);
7314 		return -EIO;
7315 	}
7316 	ring->fw_ring_id = ring_id;
7317 	return rc;
7318 }
7319 
7320 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7321 {
7322 	int rc;
7323 
7324 	if (BNXT_PF(bp)) {
7325 		struct hwrm_func_cfg_input *req;
7326 
7327 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7328 		if (rc)
7329 			return rc;
7330 
7331 		req->fid = cpu_to_le16(0xffff);
7332 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7333 		req->async_event_cr = cpu_to_le16(idx);
7334 		return hwrm_req_send(bp, req);
7335 	} else {
7336 		struct hwrm_func_vf_cfg_input *req;
7337 
7338 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7339 		if (rc)
7340 			return rc;
7341 
7342 		req->enables =
7343 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7344 		req->async_event_cr = cpu_to_le16(idx);
7345 		return hwrm_req_send(bp, req);
7346 	}
7347 }
7348 
7349 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7350 			     u32 ring_type)
7351 {
7352 	switch (ring_type) {
7353 	case HWRM_RING_ALLOC_TX:
7354 		db->db_ring_mask = bp->tx_ring_mask;
7355 		break;
7356 	case HWRM_RING_ALLOC_RX:
7357 		db->db_ring_mask = bp->rx_ring_mask;
7358 		break;
7359 	case HWRM_RING_ALLOC_AGG:
7360 		db->db_ring_mask = bp->rx_agg_ring_mask;
7361 		break;
7362 	case HWRM_RING_ALLOC_CMPL:
7363 	case HWRM_RING_ALLOC_NQ:
7364 		db->db_ring_mask = bp->cp_ring_mask;
7365 		break;
7366 	}
7367 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7368 		db->db_epoch_mask = db->db_ring_mask + 1;
7369 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7370 	}
7371 }
7372 
7373 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7374 			u32 map_idx, u32 xid)
7375 {
7376 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7377 		switch (ring_type) {
7378 		case HWRM_RING_ALLOC_TX:
7379 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7380 			break;
7381 		case HWRM_RING_ALLOC_RX:
7382 		case HWRM_RING_ALLOC_AGG:
7383 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7384 			break;
7385 		case HWRM_RING_ALLOC_CMPL:
7386 			db->db_key64 = DBR_PATH_L2;
7387 			break;
7388 		case HWRM_RING_ALLOC_NQ:
7389 			db->db_key64 = DBR_PATH_L2;
7390 			break;
7391 		}
7392 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7393 
7394 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7395 			db->db_key64 |= DBR_VALID;
7396 
7397 		db->doorbell = bp->bar1 + bp->db_offset;
7398 	} else {
7399 		db->doorbell = bp->bar1 + map_idx * 0x80;
7400 		switch (ring_type) {
7401 		case HWRM_RING_ALLOC_TX:
7402 			db->db_key32 = DB_KEY_TX;
7403 			break;
7404 		case HWRM_RING_ALLOC_RX:
7405 		case HWRM_RING_ALLOC_AGG:
7406 			db->db_key32 = DB_KEY_RX;
7407 			break;
7408 		case HWRM_RING_ALLOC_CMPL:
7409 			db->db_key32 = DB_KEY_CP;
7410 			break;
7411 		}
7412 	}
7413 	bnxt_set_db_mask(bp, db, ring_type);
7414 }
7415 
7416 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7417 				   struct bnxt_rx_ring_info *rxr)
7418 {
7419 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7420 	struct bnxt_napi *bnapi = rxr->bnapi;
7421 	u32 type = HWRM_RING_ALLOC_RX;
7422 	u32 map_idx = bnapi->index;
7423 	int rc;
7424 
7425 	rc = hwrm_ring_alloc_send_msg(bp, rxr, ring, type, map_idx);
7426 	if (rc)
7427 		return rc;
7428 
7429 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7430 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7431 
7432 	return 0;
7433 }
7434 
7435 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7436 				       struct bnxt_rx_ring_info *rxr)
7437 {
7438 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7439 	u32 type = HWRM_RING_ALLOC_AGG;
7440 	u32 grp_idx = ring->grp_idx;
7441 	u32 map_idx;
7442 	int rc;
7443 
7444 	map_idx = grp_idx + bp->rx_nr_rings;
7445 	rc = hwrm_ring_alloc_send_msg(bp, rxr, ring, type, map_idx);
7446 	if (rc)
7447 		return rc;
7448 
7449 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7450 		    ring->fw_ring_id);
7451 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7452 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7453 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7454 
7455 	return 0;
7456 }
7457 
7458 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp,
7459 				      struct bnxt_cp_ring_info *cpr)
7460 {
7461 	const u32 type = HWRM_RING_ALLOC_CMPL;
7462 	struct bnxt_napi *bnapi = cpr->bnapi;
7463 	struct bnxt_ring_struct *ring;
7464 	u32 map_idx = bnapi->index;
7465 	int rc;
7466 
7467 	ring = &cpr->cp_ring_struct;
7468 	ring->handle = BNXT_SET_NQ_HDL(cpr);
7469 	rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, map_idx);
7470 	if (rc)
7471 		return rc;
7472 	bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7473 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7474 	return 0;
7475 }
7476 
7477 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp,
7478 				   struct bnxt_tx_ring_info *txr, u32 tx_idx)
7479 {
7480 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7481 	const u32 type = HWRM_RING_ALLOC_TX;
7482 	int rc;
7483 
7484 	rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, tx_idx);
7485 	if (rc)
7486 		return rc;
7487 	bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id);
7488 	return 0;
7489 }
7490 
7491 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7492 {
7493 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7494 	int i, rc = 0;
7495 	u32 type;
7496 
7497 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7498 		type = HWRM_RING_ALLOC_NQ;
7499 	else
7500 		type = HWRM_RING_ALLOC_CMPL;
7501 	for (i = 0; i < bp->cp_nr_rings; i++) {
7502 		struct bnxt_napi *bnapi = bp->bnapi[i];
7503 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7504 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7505 		u32 map_idx = ring->map_idx;
7506 		unsigned int vector;
7507 
7508 		vector = bp->irq_tbl[map_idx].vector;
7509 		disable_irq_nosync(vector);
7510 		rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, map_idx);
7511 		if (rc) {
7512 			enable_irq(vector);
7513 			goto err_out;
7514 		}
7515 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7516 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7517 		enable_irq(vector);
7518 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7519 
7520 		if (!i) {
7521 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7522 			if (rc)
7523 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7524 		}
7525 	}
7526 
7527 	for (i = 0; i < bp->tx_nr_rings; i++) {
7528 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7529 
7530 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7531 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
7532 			if (rc)
7533 				goto err_out;
7534 		}
7535 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i);
7536 		if (rc)
7537 			goto err_out;
7538 	}
7539 
7540 	for (i = 0; i < bp->rx_nr_rings; i++) {
7541 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7542 
7543 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7544 		if (rc)
7545 			goto err_out;
7546 		/* If we have agg rings, post agg buffers first. */
7547 		if (!agg_rings)
7548 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7549 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7550 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
7551 			if (rc)
7552 				goto err_out;
7553 		}
7554 	}
7555 
7556 	if (agg_rings) {
7557 		for (i = 0; i < bp->rx_nr_rings; i++) {
7558 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7559 			if (rc)
7560 				goto err_out;
7561 		}
7562 	}
7563 err_out:
7564 	return rc;
7565 }
7566 
7567 static void bnxt_cancel_dim(struct bnxt *bp)
7568 {
7569 	int i;
7570 
7571 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7572 	 * if NAPI is enabled.
7573 	 */
7574 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7575 		return;
7576 
7577 	/* Make sure NAPI sees that the VNIC is disabled */
7578 	synchronize_net();
7579 	for (i = 0; i < bp->rx_nr_rings; i++) {
7580 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7581 		struct bnxt_napi *bnapi = rxr->bnapi;
7582 
7583 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7584 	}
7585 }
7586 
7587 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7588 				   struct bnxt_ring_struct *ring,
7589 				   u32 ring_type, int cmpl_ring_id)
7590 {
7591 	struct hwrm_ring_free_output *resp;
7592 	struct hwrm_ring_free_input *req;
7593 	u16 error_code = 0;
7594 	int rc;
7595 
7596 	if (BNXT_NO_FW_ACCESS(bp))
7597 		return 0;
7598 
7599 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7600 	if (rc)
7601 		goto exit;
7602 
7603 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7604 	req->ring_type = ring_type;
7605 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7606 
7607 	resp = hwrm_req_hold(bp, req);
7608 	rc = hwrm_req_send(bp, req);
7609 	error_code = le16_to_cpu(resp->error_code);
7610 	hwrm_req_drop(bp, req);
7611 exit:
7612 	if (rc || error_code) {
7613 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7614 			   ring_type, rc, error_code);
7615 		return -EIO;
7616 	}
7617 	return 0;
7618 }
7619 
7620 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp,
7621 				   struct bnxt_tx_ring_info *txr,
7622 				   bool close_path)
7623 {
7624 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7625 	u32 cmpl_ring_id;
7626 
7627 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7628 		return;
7629 
7630 	cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) :
7631 		       INVALID_HW_RING_ID;
7632 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX,
7633 				cmpl_ring_id);
7634 	ring->fw_ring_id = INVALID_HW_RING_ID;
7635 }
7636 
7637 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7638 				   struct bnxt_rx_ring_info *rxr,
7639 				   bool close_path)
7640 {
7641 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7642 	u32 grp_idx = rxr->bnapi->index;
7643 	u32 cmpl_ring_id;
7644 
7645 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7646 		return;
7647 
7648 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7649 	hwrm_ring_free_send_msg(bp, ring,
7650 				RING_FREE_REQ_RING_TYPE_RX,
7651 				close_path ? cmpl_ring_id :
7652 				INVALID_HW_RING_ID);
7653 	ring->fw_ring_id = INVALID_HW_RING_ID;
7654 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7655 }
7656 
7657 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7658 				       struct bnxt_rx_ring_info *rxr,
7659 				       bool close_path)
7660 {
7661 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7662 	u32 grp_idx = rxr->bnapi->index;
7663 	u32 type, cmpl_ring_id;
7664 
7665 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7666 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7667 	else
7668 		type = RING_FREE_REQ_RING_TYPE_RX;
7669 
7670 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7671 		return;
7672 
7673 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7674 	hwrm_ring_free_send_msg(bp, ring, type,
7675 				close_path ? cmpl_ring_id :
7676 				INVALID_HW_RING_ID);
7677 	ring->fw_ring_id = INVALID_HW_RING_ID;
7678 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7679 }
7680 
7681 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp,
7682 				   struct bnxt_cp_ring_info *cpr)
7683 {
7684 	struct bnxt_ring_struct *ring;
7685 
7686 	ring = &cpr->cp_ring_struct;
7687 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7688 		return;
7689 
7690 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL,
7691 				INVALID_HW_RING_ID);
7692 	ring->fw_ring_id = INVALID_HW_RING_ID;
7693 }
7694 
7695 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
7696 {
7697 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7698 	int i, size = ring->ring_mem.page_size;
7699 
7700 	cpr->cp_raw_cons = 0;
7701 	cpr->toggle = 0;
7702 
7703 	for (i = 0; i < bp->cp_nr_pages; i++)
7704 		if (cpr->cp_desc_ring[i])
7705 			memset(cpr->cp_desc_ring[i], 0, size);
7706 }
7707 
7708 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7709 {
7710 	u32 type;
7711 	int i;
7712 
7713 	if (!bp->bnapi)
7714 		return;
7715 
7716 	for (i = 0; i < bp->tx_nr_rings; i++)
7717 		bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path);
7718 
7719 	bnxt_cancel_dim(bp);
7720 	for (i = 0; i < bp->rx_nr_rings; i++) {
7721 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7722 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7723 	}
7724 
7725 	/* The completion rings are about to be freed.  After that the
7726 	 * IRQ doorbell will not work anymore.  So we need to disable
7727 	 * IRQ here.
7728 	 */
7729 	bnxt_disable_int_sync(bp);
7730 
7731 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7732 		type = RING_FREE_REQ_RING_TYPE_NQ;
7733 	else
7734 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7735 	for (i = 0; i < bp->cp_nr_rings; i++) {
7736 		struct bnxt_napi *bnapi = bp->bnapi[i];
7737 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7738 		struct bnxt_ring_struct *ring;
7739 		int j;
7740 
7741 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++)
7742 			bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]);
7743 
7744 		ring = &cpr->cp_ring_struct;
7745 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7746 			hwrm_ring_free_send_msg(bp, ring, type,
7747 						INVALID_HW_RING_ID);
7748 			ring->fw_ring_id = INVALID_HW_RING_ID;
7749 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7750 		}
7751 	}
7752 }
7753 
7754 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7755 			     bool shared);
7756 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7757 			   bool shared);
7758 
7759 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7760 {
7761 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7762 	struct hwrm_func_qcfg_output *resp;
7763 	struct hwrm_func_qcfg_input *req;
7764 	int rc;
7765 
7766 	if (bp->hwrm_spec_code < 0x10601)
7767 		return 0;
7768 
7769 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7770 	if (rc)
7771 		return rc;
7772 
7773 	req->fid = cpu_to_le16(0xffff);
7774 	resp = hwrm_req_hold(bp, req);
7775 	rc = hwrm_req_send(bp, req);
7776 	if (rc) {
7777 		hwrm_req_drop(bp, req);
7778 		return rc;
7779 	}
7780 
7781 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7782 	if (BNXT_NEW_RM(bp)) {
7783 		u16 cp, stats;
7784 
7785 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7786 		hw_resc->resv_hw_ring_grps =
7787 			le32_to_cpu(resp->alloc_hw_ring_grps);
7788 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7789 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7790 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7791 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7792 		hw_resc->resv_irqs = cp;
7793 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7794 			int rx = hw_resc->resv_rx_rings;
7795 			int tx = hw_resc->resv_tx_rings;
7796 
7797 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7798 				rx >>= 1;
7799 			if (cp < (rx + tx)) {
7800 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7801 				if (rc)
7802 					goto get_rings_exit;
7803 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7804 					rx <<= 1;
7805 				hw_resc->resv_rx_rings = rx;
7806 				hw_resc->resv_tx_rings = tx;
7807 			}
7808 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7809 			hw_resc->resv_hw_ring_grps = rx;
7810 		}
7811 		hw_resc->resv_cp_rings = cp;
7812 		hw_resc->resv_stat_ctxs = stats;
7813 	}
7814 get_rings_exit:
7815 	hwrm_req_drop(bp, req);
7816 	return rc;
7817 }
7818 
7819 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7820 {
7821 	struct hwrm_func_qcfg_output *resp;
7822 	struct hwrm_func_qcfg_input *req;
7823 	int rc;
7824 
7825 	if (bp->hwrm_spec_code < 0x10601)
7826 		return 0;
7827 
7828 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7829 	if (rc)
7830 		return rc;
7831 
7832 	req->fid = cpu_to_le16(fid);
7833 	resp = hwrm_req_hold(bp, req);
7834 	rc = hwrm_req_send(bp, req);
7835 	if (!rc)
7836 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7837 
7838 	hwrm_req_drop(bp, req);
7839 	return rc;
7840 }
7841 
7842 static bool bnxt_rfs_supported(struct bnxt *bp);
7843 
7844 static struct hwrm_func_cfg_input *
7845 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7846 {
7847 	struct hwrm_func_cfg_input *req;
7848 	u32 enables = 0;
7849 
7850 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7851 		return NULL;
7852 
7853 	req->fid = cpu_to_le16(0xffff);
7854 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7855 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7856 	if (BNXT_NEW_RM(bp)) {
7857 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7858 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7859 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7860 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7861 			enables |= hwr->cp_p5 ?
7862 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7863 		} else {
7864 			enables |= hwr->cp ?
7865 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7866 			enables |= hwr->grp ?
7867 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7868 		}
7869 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7870 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7871 					  0;
7872 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7873 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7874 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7875 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7876 			req->num_msix = cpu_to_le16(hwr->cp);
7877 		} else {
7878 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7879 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7880 		}
7881 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7882 		req->num_vnics = cpu_to_le16(hwr->vnic);
7883 	}
7884 	req->enables = cpu_to_le32(enables);
7885 	return req;
7886 }
7887 
7888 static struct hwrm_func_vf_cfg_input *
7889 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7890 {
7891 	struct hwrm_func_vf_cfg_input *req;
7892 	u32 enables = 0;
7893 
7894 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7895 		return NULL;
7896 
7897 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7898 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7899 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7900 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7901 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7902 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7903 		enables |= hwr->cp_p5 ?
7904 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7905 	} else {
7906 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7907 		enables |= hwr->grp ?
7908 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7909 	}
7910 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7911 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7912 
7913 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7914 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7915 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7916 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7917 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7918 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7919 	} else {
7920 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7921 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7922 	}
7923 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7924 	req->num_vnics = cpu_to_le16(hwr->vnic);
7925 
7926 	req->enables = cpu_to_le32(enables);
7927 	return req;
7928 }
7929 
7930 static int
7931 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7932 {
7933 	struct hwrm_func_cfg_input *req;
7934 	int rc;
7935 
7936 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7937 	if (!req)
7938 		return -ENOMEM;
7939 
7940 	if (!req->enables) {
7941 		hwrm_req_drop(bp, req);
7942 		return 0;
7943 	}
7944 
7945 	rc = hwrm_req_send(bp, req);
7946 	if (rc)
7947 		return rc;
7948 
7949 	if (bp->hwrm_spec_code < 0x10601)
7950 		bp->hw_resc.resv_tx_rings = hwr->tx;
7951 
7952 	return bnxt_hwrm_get_rings(bp);
7953 }
7954 
7955 static int
7956 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7957 {
7958 	struct hwrm_func_vf_cfg_input *req;
7959 	int rc;
7960 
7961 	if (!BNXT_NEW_RM(bp)) {
7962 		bp->hw_resc.resv_tx_rings = hwr->tx;
7963 		return 0;
7964 	}
7965 
7966 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7967 	if (!req)
7968 		return -ENOMEM;
7969 
7970 	rc = hwrm_req_send(bp, req);
7971 	if (rc)
7972 		return rc;
7973 
7974 	return bnxt_hwrm_get_rings(bp);
7975 }
7976 
7977 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7978 {
7979 	if (BNXT_PF(bp))
7980 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7981 	else
7982 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7983 }
7984 
7985 int bnxt_nq_rings_in_use(struct bnxt *bp)
7986 {
7987 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7988 }
7989 
7990 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7991 {
7992 	int cp;
7993 
7994 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7995 		return bnxt_nq_rings_in_use(bp);
7996 
7997 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7998 	return cp;
7999 }
8000 
8001 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
8002 {
8003 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
8004 }
8005 
8006 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8007 {
8008 	if (!hwr->grp)
8009 		return 0;
8010 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8011 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
8012 
8013 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
8014 			rss_ctx *= hwr->vnic;
8015 		return rss_ctx;
8016 	}
8017 	if (BNXT_VF(bp))
8018 		return BNXT_VF_MAX_RSS_CTX;
8019 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
8020 		return hwr->grp + 1;
8021 	return 1;
8022 }
8023 
8024 /* Check if a default RSS map needs to be setup.  This function is only
8025  * used on older firmware that does not require reserving RX rings.
8026  */
8027 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
8028 {
8029 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8030 
8031 	/* The RSS map is valid for RX rings set to resv_rx_rings */
8032 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
8033 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
8034 		if (!netif_is_rxfh_configured(bp->dev))
8035 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8036 	}
8037 }
8038 
8039 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
8040 {
8041 	if (bp->flags & BNXT_FLAG_RFS) {
8042 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
8043 			return 2 + bp->num_rss_ctx;
8044 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8045 			return rx_rings + 1;
8046 	}
8047 	return 1;
8048 }
8049 
8050 static void bnxt_get_total_resources(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8051 {
8052 	hwr->cp = bnxt_nq_rings_in_use(bp);
8053 	hwr->cp_p5 = 0;
8054 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8055 		hwr->cp_p5 = bnxt_cp_rings_in_use(bp);
8056 	hwr->tx = bp->tx_nr_rings;
8057 	hwr->rx = bp->rx_nr_rings;
8058 	hwr->grp = hwr->rx;
8059 	hwr->vnic = bnxt_get_total_vnics(bp, hwr->rx);
8060 	hwr->rss_ctx = bnxt_get_total_rss_ctxs(bp, hwr);
8061 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8062 		hwr->rx <<= 1;
8063 	hwr->stat = bnxt_get_func_stat_ctxs(bp);
8064 }
8065 
8066 static bool bnxt_need_reserve_rings(struct bnxt *bp)
8067 {
8068 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8069 	struct bnxt_hw_rings hwr;
8070 
8071 	bnxt_get_total_resources(bp, &hwr);
8072 
8073 	/* Old firmware does not need RX ring reservations but we still
8074 	 * need to setup a default RSS map when needed.  With new firmware
8075 	 * we go through RX ring reservations first and then set up the
8076 	 * RSS map for the successfully reserved RX rings when needed.
8077 	 */
8078 	if (!BNXT_NEW_RM(bp))
8079 		bnxt_check_rss_tbl_no_rmgr(bp);
8080 
8081 	if (hw_resc->resv_tx_rings != hwr.tx && bp->hwrm_spec_code >= 0x10601)
8082 		return true;
8083 
8084 	if (!BNXT_NEW_RM(bp))
8085 		return false;
8086 
8087 	if (hw_resc->resv_rx_rings != hwr.rx ||
8088 	    hw_resc->resv_vnics != hwr.vnic ||
8089 	    hw_resc->resv_stat_ctxs != hwr.stat ||
8090 	    hw_resc->resv_rsscos_ctxs != hwr.rss_ctx ||
8091 	    (hw_resc->resv_hw_ring_grps != hwr.grp &&
8092 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
8093 		return true;
8094 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8095 		if (hw_resc->resv_cp_rings != hwr.cp_p5)
8096 			return true;
8097 	} else if (hw_resc->resv_cp_rings != hwr.cp) {
8098 		return true;
8099 	}
8100 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
8101 	    hw_resc->resv_irqs != hwr.cp)
8102 		return true;
8103 	return false;
8104 }
8105 
8106 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8107 {
8108 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8109 
8110 	hwr->tx = hw_resc->resv_tx_rings;
8111 	if (BNXT_NEW_RM(bp)) {
8112 		hwr->rx = hw_resc->resv_rx_rings;
8113 		hwr->cp = hw_resc->resv_irqs;
8114 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8115 			hwr->cp_p5 = hw_resc->resv_cp_rings;
8116 		hwr->grp = hw_resc->resv_hw_ring_grps;
8117 		hwr->vnic = hw_resc->resv_vnics;
8118 		hwr->stat = hw_resc->resv_stat_ctxs;
8119 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
8120 	}
8121 }
8122 
8123 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8124 {
8125 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
8126 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
8127 }
8128 
8129 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
8130 
8131 static int __bnxt_reserve_rings(struct bnxt *bp)
8132 {
8133 	struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA];
8134 	struct bnxt_hw_rings hwr = {0};
8135 	int rx_rings, old_rx_rings, rc;
8136 	int cp = bp->cp_nr_rings;
8137 	int ulp_msix = 0;
8138 	bool sh = false;
8139 	int tx_cp;
8140 
8141 	if (!bnxt_need_reserve_rings(bp))
8142 		return 0;
8143 
8144 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(edev)) {
8145 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
8146 		if (!ulp_msix)
8147 			bnxt_set_ulp_stat_ctxs(bp, 0);
8148 		else
8149 			bnxt_set_dflt_ulp_stat_ctxs(bp);
8150 
8151 		if (ulp_msix > bp->ulp_num_msix_want)
8152 			ulp_msix = bp->ulp_num_msix_want;
8153 		hwr.cp = cp + ulp_msix;
8154 	} else {
8155 		hwr.cp = bnxt_nq_rings_in_use(bp);
8156 	}
8157 
8158 	hwr.tx = bp->tx_nr_rings;
8159 	hwr.rx = bp->rx_nr_rings;
8160 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
8161 		sh = true;
8162 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8163 		hwr.cp_p5 = hwr.rx + hwr.tx;
8164 
8165 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
8166 
8167 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8168 		hwr.rx <<= 1;
8169 	hwr.grp = bp->rx_nr_rings;
8170 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
8171 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
8172 	old_rx_rings = bp->hw_resc.resv_rx_rings;
8173 
8174 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
8175 	if (rc)
8176 		return rc;
8177 
8178 	bnxt_copy_reserved_rings(bp, &hwr);
8179 
8180 	rx_rings = hwr.rx;
8181 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8182 		if (hwr.rx >= 2) {
8183 			rx_rings = hwr.rx >> 1;
8184 		} else {
8185 			if (netif_running(bp->dev))
8186 				return -ENOMEM;
8187 
8188 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
8189 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
8190 			bp->dev->hw_features &= ~NETIF_F_LRO;
8191 			bp->dev->features &= ~NETIF_F_LRO;
8192 			bnxt_set_ring_params(bp);
8193 		}
8194 	}
8195 	rx_rings = min_t(int, rx_rings, hwr.grp);
8196 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
8197 	if (bnxt_ulp_registered(edev) && hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
8198 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
8199 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
8200 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
8201 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8202 		hwr.rx = rx_rings << 1;
8203 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
8204 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
8205 	if (hwr.tx != bp->tx_nr_rings) {
8206 		netdev_warn(bp->dev,
8207 			    "Able to reserve only %d out of %d requested TX rings\n",
8208 			    hwr.tx, bp->tx_nr_rings);
8209 	}
8210 	bp->tx_nr_rings = hwr.tx;
8211 
8212 	/* If we cannot reserve all the RX rings, reset the RSS map only
8213 	 * if absolutely necessary
8214 	 */
8215 	if (rx_rings != bp->rx_nr_rings) {
8216 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
8217 			    rx_rings, bp->rx_nr_rings);
8218 		if (netif_is_rxfh_configured(bp->dev) &&
8219 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
8220 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
8221 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
8222 			ethtool_rxfh_indir_lost(bp->dev);
8223 		}
8224 	}
8225 	bp->rx_nr_rings = rx_rings;
8226 	bp->cp_nr_rings = hwr.cp;
8227 
8228 	/* Fall back if we cannot reserve enough HW RSS contexts */
8229 	if ((bp->rss_cap & BNXT_RSS_CAP_LARGE_RSS_CTX) &&
8230 	    hwr.rss_ctx < bnxt_get_total_rss_ctxs(bp, &hwr))
8231 		bp->rss_cap &= ~BNXT_RSS_CAP_LARGE_RSS_CTX;
8232 
8233 	if (!bnxt_rings_ok(bp, &hwr))
8234 		return -ENOMEM;
8235 
8236 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
8237 	    !netif_is_rxfh_configured(bp->dev))
8238 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8239 
8240 	if (!bnxt_ulp_registered(edev) && BNXT_NEW_RM(bp)) {
8241 		int resv_msix, resv_ctx, ulp_ctxs;
8242 		struct bnxt_hw_resc *hw_resc;
8243 
8244 		hw_resc = &bp->hw_resc;
8245 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
8246 		ulp_msix = min_t(int, resv_msix, ulp_msix);
8247 		bnxt_set_ulp_msix_num(bp, ulp_msix);
8248 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
8249 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
8250 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
8251 	}
8252 
8253 	return rc;
8254 }
8255 
8256 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8257 {
8258 	struct hwrm_func_vf_cfg_input *req;
8259 	u32 flags;
8260 
8261 	if (!BNXT_NEW_RM(bp))
8262 		return 0;
8263 
8264 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
8265 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
8266 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8267 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8268 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8269 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
8270 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
8271 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8272 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8273 
8274 	req->flags = cpu_to_le32(flags);
8275 	return hwrm_req_send_silent(bp, req);
8276 }
8277 
8278 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8279 {
8280 	struct hwrm_func_cfg_input *req;
8281 	u32 flags;
8282 
8283 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
8284 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
8285 	if (BNXT_NEW_RM(bp)) {
8286 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8287 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8288 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8289 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
8290 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8291 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
8292 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
8293 		else
8294 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8295 	}
8296 
8297 	req->flags = cpu_to_le32(flags);
8298 	return hwrm_req_send_silent(bp, req);
8299 }
8300 
8301 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8302 {
8303 	if (bp->hwrm_spec_code < 0x10801)
8304 		return 0;
8305 
8306 	if (BNXT_PF(bp))
8307 		return bnxt_hwrm_check_pf_rings(bp, hwr);
8308 
8309 	return bnxt_hwrm_check_vf_rings(bp, hwr);
8310 }
8311 
8312 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
8313 {
8314 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8315 	struct hwrm_ring_aggint_qcaps_output *resp;
8316 	struct hwrm_ring_aggint_qcaps_input *req;
8317 	int rc;
8318 
8319 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
8320 	coal_cap->num_cmpl_dma_aggr_max = 63;
8321 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
8322 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
8323 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
8324 	coal_cap->int_lat_tmr_min_max = 65535;
8325 	coal_cap->int_lat_tmr_max_max = 65535;
8326 	coal_cap->num_cmpl_aggr_int_max = 65535;
8327 	coal_cap->timer_units = 80;
8328 
8329 	if (bp->hwrm_spec_code < 0x10902)
8330 		return;
8331 
8332 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
8333 		return;
8334 
8335 	resp = hwrm_req_hold(bp, req);
8336 	rc = hwrm_req_send_silent(bp, req);
8337 	if (!rc) {
8338 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8339 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8340 		coal_cap->num_cmpl_dma_aggr_max =
8341 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8342 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8343 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8344 		coal_cap->cmpl_aggr_dma_tmr_max =
8345 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8346 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8347 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8348 		coal_cap->int_lat_tmr_min_max =
8349 			le16_to_cpu(resp->int_lat_tmr_min_max);
8350 		coal_cap->int_lat_tmr_max_max =
8351 			le16_to_cpu(resp->int_lat_tmr_max_max);
8352 		coal_cap->num_cmpl_aggr_int_max =
8353 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8354 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8355 	}
8356 	hwrm_req_drop(bp, req);
8357 }
8358 
8359 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8360 {
8361 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8362 
8363 	return usec * 1000 / coal_cap->timer_units;
8364 }
8365 
8366 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8367 	struct bnxt_coal *hw_coal,
8368 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8369 {
8370 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8371 	u16 val, tmr, max, flags = hw_coal->flags;
8372 	u32 cmpl_params = coal_cap->cmpl_params;
8373 
8374 	max = hw_coal->bufs_per_record * 128;
8375 	if (hw_coal->budget)
8376 		max = hw_coal->bufs_per_record * hw_coal->budget;
8377 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8378 
8379 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8380 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8381 
8382 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8383 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8384 
8385 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8386 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8387 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8388 
8389 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8390 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8391 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8392 
8393 	/* min timer set to 1/2 of interrupt timer */
8394 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8395 		val = tmr / 2;
8396 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8397 		req->int_lat_tmr_min = cpu_to_le16(val);
8398 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8399 	}
8400 
8401 	/* buf timer set to 1/4 of interrupt timer */
8402 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8403 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8404 
8405 	if (cmpl_params &
8406 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8407 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8408 		val = clamp_t(u16, tmr, 1,
8409 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8410 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8411 		req->enables |=
8412 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8413 	}
8414 
8415 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8416 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8417 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8418 	req->flags = cpu_to_le16(flags);
8419 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8420 }
8421 
8422 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8423 				   struct bnxt_coal *hw_coal)
8424 {
8425 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8426 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8427 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8428 	u32 nq_params = coal_cap->nq_params;
8429 	u16 tmr;
8430 	int rc;
8431 
8432 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8433 		return 0;
8434 
8435 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8436 	if (rc)
8437 		return rc;
8438 
8439 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8440 	req->flags =
8441 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8442 
8443 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8444 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8445 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8446 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8447 	return hwrm_req_send(bp, req);
8448 }
8449 
8450 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8451 {
8452 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8453 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8454 	struct bnxt_coal coal;
8455 	int rc;
8456 
8457 	/* Tick values in micro seconds.
8458 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8459 	 */
8460 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8461 
8462 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8463 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8464 
8465 	if (!bnapi->rx_ring)
8466 		return -ENODEV;
8467 
8468 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8469 	if (rc)
8470 		return rc;
8471 
8472 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8473 
8474 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8475 
8476 	return hwrm_req_send(bp, req_rx);
8477 }
8478 
8479 static int
8480 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8481 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8482 {
8483 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8484 
8485 	req->ring_id = cpu_to_le16(ring_id);
8486 	return hwrm_req_send(bp, req);
8487 }
8488 
8489 static int
8490 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8491 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8492 {
8493 	struct bnxt_tx_ring_info *txr;
8494 	int i, rc;
8495 
8496 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8497 		u16 ring_id;
8498 
8499 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8500 		req->ring_id = cpu_to_le16(ring_id);
8501 		rc = hwrm_req_send(bp, req);
8502 		if (rc)
8503 			return rc;
8504 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8505 			return 0;
8506 	}
8507 	return 0;
8508 }
8509 
8510 int bnxt_hwrm_set_coal(struct bnxt *bp)
8511 {
8512 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8513 	int i, rc;
8514 
8515 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8516 	if (rc)
8517 		return rc;
8518 
8519 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8520 	if (rc) {
8521 		hwrm_req_drop(bp, req_rx);
8522 		return rc;
8523 	}
8524 
8525 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8526 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8527 
8528 	hwrm_req_hold(bp, req_rx);
8529 	hwrm_req_hold(bp, req_tx);
8530 	for (i = 0; i < bp->cp_nr_rings; i++) {
8531 		struct bnxt_napi *bnapi = bp->bnapi[i];
8532 		struct bnxt_coal *hw_coal;
8533 
8534 		if (!bnapi->rx_ring)
8535 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8536 		else
8537 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8538 		if (rc)
8539 			break;
8540 
8541 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8542 			continue;
8543 
8544 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8545 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8546 			if (rc)
8547 				break;
8548 		}
8549 		if (bnapi->rx_ring)
8550 			hw_coal = &bp->rx_coal;
8551 		else
8552 			hw_coal = &bp->tx_coal;
8553 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8554 	}
8555 	hwrm_req_drop(bp, req_rx);
8556 	hwrm_req_drop(bp, req_tx);
8557 	return rc;
8558 }
8559 
8560 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8561 {
8562 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8563 	struct hwrm_stat_ctx_free_input *req;
8564 	int i;
8565 
8566 	if (!bp->bnapi)
8567 		return;
8568 
8569 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8570 		return;
8571 
8572 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8573 		return;
8574 	if (BNXT_FW_MAJ(bp) <= 20) {
8575 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8576 			hwrm_req_drop(bp, req);
8577 			return;
8578 		}
8579 		hwrm_req_hold(bp, req0);
8580 	}
8581 	hwrm_req_hold(bp, req);
8582 	for (i = 0; i < bp->cp_nr_rings; i++) {
8583 		struct bnxt_napi *bnapi = bp->bnapi[i];
8584 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8585 
8586 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8587 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8588 			if (req0) {
8589 				req0->stat_ctx_id = req->stat_ctx_id;
8590 				hwrm_req_send(bp, req0);
8591 			}
8592 			hwrm_req_send(bp, req);
8593 
8594 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8595 		}
8596 	}
8597 	hwrm_req_drop(bp, req);
8598 	if (req0)
8599 		hwrm_req_drop(bp, req0);
8600 }
8601 
8602 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8603 {
8604 	struct hwrm_stat_ctx_alloc_output *resp;
8605 	struct hwrm_stat_ctx_alloc_input *req;
8606 	int rc, i;
8607 
8608 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8609 		return 0;
8610 
8611 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8612 	if (rc)
8613 		return rc;
8614 
8615 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8616 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8617 
8618 	resp = hwrm_req_hold(bp, req);
8619 	for (i = 0; i < bp->cp_nr_rings; i++) {
8620 		struct bnxt_napi *bnapi = bp->bnapi[i];
8621 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8622 
8623 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8624 
8625 		rc = hwrm_req_send(bp, req);
8626 		if (rc)
8627 			break;
8628 
8629 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8630 
8631 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8632 	}
8633 	hwrm_req_drop(bp, req);
8634 	return rc;
8635 }
8636 
8637 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8638 {
8639 	struct hwrm_func_qcfg_output *resp;
8640 	struct hwrm_func_qcfg_input *req;
8641 	u16 flags;
8642 	int rc;
8643 
8644 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8645 	if (rc)
8646 		return rc;
8647 
8648 	req->fid = cpu_to_le16(0xffff);
8649 	resp = hwrm_req_hold(bp, req);
8650 	rc = hwrm_req_send(bp, req);
8651 	if (rc)
8652 		goto func_qcfg_exit;
8653 
8654 	flags = le16_to_cpu(resp->flags);
8655 #ifdef CONFIG_BNXT_SRIOV
8656 	if (BNXT_VF(bp)) {
8657 		struct bnxt_vf_info *vf = &bp->vf;
8658 
8659 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8660 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8661 			vf->flags |= BNXT_VF_TRUST;
8662 		else
8663 			vf->flags &= ~BNXT_VF_TRUST;
8664 	} else {
8665 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8666 	}
8667 #endif
8668 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8669 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8670 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8671 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8672 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8673 	}
8674 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8675 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8676 
8677 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8678 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8679 
8680 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8681 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8682 	if (resp->roce_bidi_opt_mode &
8683 	    FUNC_QCFG_RESP_ROCE_BIDI_OPT_MODE_DEDICATED)
8684 		bp->cos0_cos1_shared = 1;
8685 	else
8686 		bp->cos0_cos1_shared = 0;
8687 
8688 	switch (resp->port_partition_type) {
8689 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8690 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2:
8691 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8692 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8693 		bp->port_partition_type = resp->port_partition_type;
8694 		break;
8695 	}
8696 	if (bp->hwrm_spec_code < 0x10707 ||
8697 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8698 		bp->br_mode = BRIDGE_MODE_VEB;
8699 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8700 		bp->br_mode = BRIDGE_MODE_VEPA;
8701 	else
8702 		bp->br_mode = BRIDGE_MODE_UNDEF;
8703 
8704 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8705 	if (!bp->max_mtu)
8706 		bp->max_mtu = BNXT_MAX_MTU;
8707 
8708 	if (bp->db_size)
8709 		goto func_qcfg_exit;
8710 
8711 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8712 	if (BNXT_CHIP_P5(bp)) {
8713 		if (BNXT_PF(bp))
8714 			bp->db_offset = DB_PF_OFFSET_P5;
8715 		else
8716 			bp->db_offset = DB_VF_OFFSET_P5;
8717 	}
8718 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8719 				 1024);
8720 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8721 	    bp->db_size <= bp->db_offset)
8722 		bp->db_size = pci_resource_len(bp->pdev, 2);
8723 
8724 func_qcfg_exit:
8725 	hwrm_req_drop(bp, req);
8726 	return rc;
8727 }
8728 
8729 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8730 				      u8 init_val, u8 init_offset,
8731 				      bool init_mask_set)
8732 {
8733 	ctxm->init_value = init_val;
8734 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8735 	if (init_mask_set)
8736 		ctxm->init_offset = init_offset * 4;
8737 	else
8738 		ctxm->init_value = 0;
8739 }
8740 
8741 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8742 {
8743 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8744 	u16 type;
8745 
8746 	for (type = 0; type < ctx_max; type++) {
8747 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8748 		int n = 1;
8749 
8750 		if (!ctxm->max_entries || ctxm->pg_info)
8751 			continue;
8752 
8753 		if (ctxm->instance_bmap)
8754 			n = hweight32(ctxm->instance_bmap);
8755 		ctxm->pg_info = kzalloc_objs(*ctxm->pg_info, n);
8756 		if (!ctxm->pg_info)
8757 			return -ENOMEM;
8758 	}
8759 	return 0;
8760 }
8761 
8762 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8763 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8764 
8765 #define BNXT_CTX_INIT_VALID(flags)	\
8766 	(!!((flags) &			\
8767 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8768 
8769 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8770 {
8771 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8772 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8773 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8774 	u16 type, next_type = 0;
8775 	int rc;
8776 
8777 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8778 	if (rc)
8779 		return rc;
8780 
8781 	if (!ctx) {
8782 		ctx = kzalloc_obj(*ctx);
8783 		if (!ctx)
8784 			return -ENOMEM;
8785 		bp->ctx = ctx;
8786 	}
8787 
8788 	resp = hwrm_req_hold(bp, req);
8789 
8790 	for (type = 0; type < BNXT_CTX_V2_MAX; type = next_type) {
8791 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8792 		u8 init_val, init_off, i;
8793 		u32 max_entries;
8794 		u16 entry_size;
8795 		__le32 *p;
8796 		u32 flags;
8797 
8798 		req->type = cpu_to_le16(type);
8799 		rc = hwrm_req_send(bp, req);
8800 		if (rc)
8801 			goto ctx_done;
8802 		flags = le32_to_cpu(resp->flags);
8803 		next_type = le16_to_cpu(resp->next_valid_type);
8804 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8805 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8806 			continue;
8807 		}
8808 		entry_size = le16_to_cpu(resp->entry_size);
8809 		max_entries = le32_to_cpu(resp->max_num_entries);
8810 		if (ctxm->mem_valid) {
8811 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8812 			    ctxm->entry_size != entry_size ||
8813 			    ctxm->max_entries != max_entries)
8814 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8815 			else
8816 				continue;
8817 		}
8818 		ctxm->type = type;
8819 		ctxm->entry_size = entry_size;
8820 		ctxm->flags = flags;
8821 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8822 		ctxm->entry_multiple = resp->entry_multiple;
8823 		ctxm->max_entries = max_entries;
8824 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8825 		init_val = resp->ctx_init_value;
8826 		init_off = resp->ctx_init_offset;
8827 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8828 					  BNXT_CTX_INIT_VALID(flags));
8829 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8830 					      BNXT_MAX_SPLIT_ENTRY);
8831 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8832 		     i++, p++)
8833 			ctxm->split[i] = le32_to_cpu(*p);
8834 	}
8835 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8836 
8837 ctx_done:
8838 	hwrm_req_drop(bp, req);
8839 	return rc;
8840 }
8841 
8842 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8843 {
8844 	struct hwrm_func_backing_store_qcaps_output *resp;
8845 	struct hwrm_func_backing_store_qcaps_input *req;
8846 	int rc;
8847 
8848 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8849 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8850 		return 0;
8851 
8852 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8853 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8854 
8855 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8856 	if (rc)
8857 		return rc;
8858 
8859 	resp = hwrm_req_hold(bp, req);
8860 	rc = hwrm_req_send_silent(bp, req);
8861 	if (!rc) {
8862 		struct bnxt_ctx_mem_type *ctxm;
8863 		struct bnxt_ctx_mem_info *ctx;
8864 		u8 init_val, init_idx = 0;
8865 		u16 init_mask;
8866 
8867 		ctx = bp->ctx;
8868 		if (!ctx) {
8869 			ctx = kzalloc_obj(*ctx);
8870 			if (!ctx) {
8871 				rc = -ENOMEM;
8872 				goto ctx_err;
8873 			}
8874 			bp->ctx = ctx;
8875 		}
8876 		init_val = resp->ctx_kind_initializer;
8877 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8878 
8879 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8880 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8881 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8882 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8883 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8884 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8885 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8886 					  (init_mask & (1 << init_idx++)) != 0);
8887 
8888 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8889 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8890 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8891 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8892 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8893 					  (init_mask & (1 << init_idx++)) != 0);
8894 
8895 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8896 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8897 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8898 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8899 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8900 					  (init_mask & (1 << init_idx++)) != 0);
8901 
8902 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8903 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8904 		ctxm->max_entries = ctxm->vnic_entries +
8905 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8906 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8907 		bnxt_init_ctx_initializer(ctxm, init_val,
8908 					  resp->vnic_init_offset,
8909 					  (init_mask & (1 << init_idx++)) != 0);
8910 
8911 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8912 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8913 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8914 		bnxt_init_ctx_initializer(ctxm, init_val,
8915 					  resp->stat_init_offset,
8916 					  (init_mask & (1 << init_idx++)) != 0);
8917 
8918 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8919 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8920 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8921 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8922 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8923 		if (!ctxm->entry_multiple)
8924 			ctxm->entry_multiple = 1;
8925 
8926 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8927 
8928 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8929 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8930 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8931 		ctxm->mrav_num_entries_units =
8932 			le16_to_cpu(resp->mrav_num_entries_units);
8933 		bnxt_init_ctx_initializer(ctxm, init_val,
8934 					  resp->mrav_init_offset,
8935 					  (init_mask & (1 << init_idx++)) != 0);
8936 
8937 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8938 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8939 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8940 
8941 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8942 		if (!ctx->tqm_fp_rings_count)
8943 			ctx->tqm_fp_rings_count = bp->max_q;
8944 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8945 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8946 
8947 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8948 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8949 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8950 
8951 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8952 	} else {
8953 		rc = 0;
8954 	}
8955 ctx_err:
8956 	hwrm_req_drop(bp, req);
8957 	return rc;
8958 }
8959 
8960 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8961 				  __le64 *pg_dir)
8962 {
8963 	if (!rmem->nr_pages)
8964 		return;
8965 
8966 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8967 	if (rmem->depth >= 1) {
8968 		if (rmem->depth == 2)
8969 			*pg_attr |= 2;
8970 		else
8971 			*pg_attr |= 1;
8972 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8973 	} else {
8974 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8975 	}
8976 }
8977 
8978 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8979 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8980 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8981 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8982 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8983 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8984 
8985 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8986 {
8987 	struct hwrm_func_backing_store_cfg_input *req;
8988 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8989 	struct bnxt_ctx_pg_info *ctx_pg;
8990 	struct bnxt_ctx_mem_type *ctxm;
8991 	void **__req = (void **)&req;
8992 	u32 req_len = sizeof(*req);
8993 	__le32 *num_entries;
8994 	__le64 *pg_dir;
8995 	u32 flags = 0;
8996 	u8 *pg_attr;
8997 	u32 ena;
8998 	int rc;
8999 	int i;
9000 
9001 	if (!ctx)
9002 		return 0;
9003 
9004 	if (req_len > bp->hwrm_max_ext_req_len)
9005 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
9006 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
9007 	if (rc)
9008 		return rc;
9009 
9010 	req->enables = cpu_to_le32(enables);
9011 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
9012 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9013 		ctx_pg = ctxm->pg_info;
9014 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
9015 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
9016 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
9017 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
9018 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9019 				      &req->qpc_pg_size_qpc_lvl,
9020 				      &req->qpc_page_dir);
9021 
9022 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
9023 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
9024 	}
9025 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
9026 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9027 		ctx_pg = ctxm->pg_info;
9028 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
9029 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
9030 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
9031 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9032 				      &req->srq_pg_size_srq_lvl,
9033 				      &req->srq_page_dir);
9034 	}
9035 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
9036 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9037 		ctx_pg = ctxm->pg_info;
9038 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
9039 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
9040 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
9041 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9042 				      &req->cq_pg_size_cq_lvl,
9043 				      &req->cq_page_dir);
9044 	}
9045 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
9046 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9047 		ctx_pg = ctxm->pg_info;
9048 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
9049 		req->vnic_num_ring_table_entries =
9050 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
9051 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
9052 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9053 				      &req->vnic_pg_size_vnic_lvl,
9054 				      &req->vnic_page_dir);
9055 	}
9056 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
9057 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9058 		ctx_pg = ctxm->pg_info;
9059 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
9060 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
9061 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9062 				      &req->stat_pg_size_stat_lvl,
9063 				      &req->stat_page_dir);
9064 	}
9065 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
9066 		u32 units;
9067 
9068 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9069 		ctx_pg = ctxm->pg_info;
9070 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
9071 		units = ctxm->mrav_num_entries_units;
9072 		if (units) {
9073 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
9074 			u32 entries;
9075 
9076 			num_mr = ctx_pg->entries - num_ah;
9077 			entries = ((num_mr / units) << 16) | (num_ah / units);
9078 			req->mrav_num_entries = cpu_to_le32(entries);
9079 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
9080 		}
9081 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
9082 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9083 				      &req->mrav_pg_size_mrav_lvl,
9084 				      &req->mrav_page_dir);
9085 	}
9086 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
9087 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9088 		ctx_pg = ctxm->pg_info;
9089 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
9090 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
9091 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9092 				      &req->tim_pg_size_tim_lvl,
9093 				      &req->tim_page_dir);
9094 	}
9095 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9096 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
9097 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
9098 	     pg_dir = &req->tqm_sp_page_dir,
9099 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
9100 	     ctx_pg = ctxm->pg_info;
9101 	     i < BNXT_MAX_TQM_RINGS;
9102 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
9103 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
9104 		if (!(enables & ena))
9105 			continue;
9106 
9107 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
9108 		*num_entries = cpu_to_le32(ctx_pg->entries);
9109 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
9110 	}
9111 	req->flags = cpu_to_le32(flags);
9112 	return hwrm_req_send(bp, req);
9113 }
9114 
9115 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
9116 				  struct bnxt_ctx_pg_info *ctx_pg)
9117 {
9118 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9119 
9120 	rmem->page_size = BNXT_PAGE_SIZE;
9121 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
9122 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
9123 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
9124 	if (rmem->depth >= 1)
9125 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
9126 	return bnxt_alloc_ring(bp, rmem);
9127 }
9128 
9129 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
9130 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
9131 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
9132 {
9133 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9134 	int rc;
9135 
9136 	if (!mem_size)
9137 		return -EINVAL;
9138 
9139 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
9140 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
9141 		ctx_pg->nr_pages = 0;
9142 		return -EINVAL;
9143 	}
9144 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
9145 		int nr_tbls, i;
9146 
9147 		rmem->depth = 2;
9148 		ctx_pg->ctx_pg_tbl = kzalloc_objs(ctx_pg, MAX_CTX_PAGES);
9149 		if (!ctx_pg->ctx_pg_tbl)
9150 			return -ENOMEM;
9151 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
9152 		rmem->nr_pages = nr_tbls;
9153 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
9154 		if (rc)
9155 			return rc;
9156 		for (i = 0; i < nr_tbls; i++) {
9157 			struct bnxt_ctx_pg_info *pg_tbl;
9158 
9159 			pg_tbl = kzalloc_obj(*pg_tbl);
9160 			if (!pg_tbl)
9161 				return -ENOMEM;
9162 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
9163 			rmem = &pg_tbl->ring_mem;
9164 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
9165 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
9166 			rmem->depth = 1;
9167 			rmem->nr_pages = MAX_CTX_PAGES;
9168 			rmem->ctx_mem = ctxm;
9169 			if (i == (nr_tbls - 1)) {
9170 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
9171 
9172 				if (rem)
9173 					rmem->nr_pages = rem;
9174 			}
9175 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
9176 			if (rc)
9177 				break;
9178 		}
9179 	} else {
9180 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
9181 		if (rmem->nr_pages > 1 || depth)
9182 			rmem->depth = 1;
9183 		rmem->ctx_mem = ctxm;
9184 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
9185 	}
9186 	return rc;
9187 }
9188 
9189 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
9190 				    struct bnxt_ctx_pg_info *ctx_pg,
9191 				    void *buf, size_t offset, size_t head,
9192 				    size_t tail)
9193 {
9194 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9195 	size_t nr_pages = ctx_pg->nr_pages;
9196 	int page_size = rmem->page_size;
9197 	size_t len = 0, total_len = 0;
9198 	u16 depth = rmem->depth;
9199 
9200 	tail %= nr_pages * page_size;
9201 	do {
9202 		if (depth > 1) {
9203 			int i = head / (page_size * MAX_CTX_PAGES);
9204 			struct bnxt_ctx_pg_info *pg_tbl;
9205 
9206 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9207 			rmem = &pg_tbl->ring_mem;
9208 		}
9209 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
9210 		head += len;
9211 		offset += len;
9212 		total_len += len;
9213 		if (head >= nr_pages * page_size)
9214 			head = 0;
9215 	} while (head != tail);
9216 	return total_len;
9217 }
9218 
9219 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
9220 				  struct bnxt_ctx_pg_info *ctx_pg)
9221 {
9222 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9223 
9224 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
9225 	    ctx_pg->ctx_pg_tbl) {
9226 		int i, nr_tbls = rmem->nr_pages;
9227 
9228 		for (i = 0; i < nr_tbls; i++) {
9229 			struct bnxt_ctx_pg_info *pg_tbl;
9230 			struct bnxt_ring_mem_info *rmem2;
9231 
9232 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9233 			if (!pg_tbl)
9234 				continue;
9235 			rmem2 = &pg_tbl->ring_mem;
9236 			bnxt_free_ring(bp, rmem2);
9237 			ctx_pg->ctx_pg_arr[i] = NULL;
9238 			kfree(pg_tbl);
9239 			ctx_pg->ctx_pg_tbl[i] = NULL;
9240 		}
9241 		kfree(ctx_pg->ctx_pg_tbl);
9242 		ctx_pg->ctx_pg_tbl = NULL;
9243 	}
9244 	bnxt_free_ring(bp, rmem);
9245 	ctx_pg->nr_pages = 0;
9246 }
9247 
9248 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
9249 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
9250 				   u8 pg_lvl)
9251 {
9252 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9253 	int i, rc = 0, n = 1;
9254 	u32 mem_size;
9255 
9256 	if (!ctxm->entry_size || !ctx_pg)
9257 		return -EINVAL;
9258 	if (ctxm->instance_bmap)
9259 		n = hweight32(ctxm->instance_bmap);
9260 	if (ctxm->entry_multiple)
9261 		entries = roundup(entries, ctxm->entry_multiple);
9262 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
9263 	mem_size = entries * ctxm->entry_size;
9264 	for (i = 0; i < n && !rc; i++) {
9265 		ctx_pg[i].entries = entries;
9266 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
9267 					    ctxm->init_value ? ctxm : NULL);
9268 	}
9269 	if (!rc)
9270 		ctxm->mem_valid = 1;
9271 	return rc;
9272 }
9273 
9274 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
9275 					       struct bnxt_ctx_mem_type *ctxm,
9276 					       bool last)
9277 {
9278 	struct hwrm_func_backing_store_cfg_v2_input *req;
9279 	u32 instance_bmap = ctxm->instance_bmap;
9280 	int i, j, rc = 0, n = 1;
9281 	__le32 *p;
9282 
9283 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
9284 		return 0;
9285 
9286 	if (instance_bmap)
9287 		n = hweight32(ctxm->instance_bmap);
9288 	else
9289 		instance_bmap = 1;
9290 
9291 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
9292 	if (rc)
9293 		return rc;
9294 	hwrm_req_hold(bp, req);
9295 	req->type = cpu_to_le16(ctxm->type);
9296 	req->entry_size = cpu_to_le16(ctxm->entry_size);
9297 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
9298 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
9299 		struct bnxt_bs_trace_info *bs_trace;
9300 		u32 enables;
9301 
9302 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
9303 		req->enables = cpu_to_le32(enables);
9304 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
9305 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
9306 	}
9307 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
9308 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
9309 		p[i] = cpu_to_le32(ctxm->split[i]);
9310 	for (i = 0, j = 0; j < n && !rc; i++) {
9311 		struct bnxt_ctx_pg_info *ctx_pg;
9312 
9313 		if (!(instance_bmap & (1 << i)))
9314 			continue;
9315 		req->instance = cpu_to_le16(i);
9316 		ctx_pg = &ctxm->pg_info[j++];
9317 		if (!ctx_pg->entries)
9318 			continue;
9319 		req->num_entries = cpu_to_le32(ctx_pg->entries);
9320 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9321 				      &req->page_size_pbl_level,
9322 				      &req->page_dir);
9323 		if (last && j == n)
9324 			req->flags =
9325 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
9326 		rc = hwrm_req_send(bp, req);
9327 	}
9328 	hwrm_req_drop(bp, req);
9329 	return rc;
9330 }
9331 
9332 static int bnxt_backing_store_cfg_v2(struct bnxt *bp)
9333 {
9334 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9335 	struct bnxt_ctx_mem_type *ctxm;
9336 	u16 last_type = BNXT_CTX_INV;
9337 	int rc = 0;
9338 	u16 type;
9339 
9340 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_QPC; type++) {
9341 		ctxm = &ctx->ctx_arr[type];
9342 		if (!bnxt_bs_trace_avail(bp, type))
9343 			continue;
9344 		if (!ctxm->mem_valid) {
9345 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9346 						     ctxm->max_entries, 1);
9347 			if (rc) {
9348 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9349 					    type);
9350 				continue;
9351 			}
9352 			bnxt_bs_trace_init(bp, ctxm);
9353 		}
9354 		last_type = type;
9355 	}
9356 
9357 	if (last_type == BNXT_CTX_INV) {
9358 		for (type = 0; type < BNXT_CTX_MAX; type++) {
9359 			ctxm = &ctx->ctx_arr[type];
9360 			if (ctxm->mem_valid)
9361 				last_type = type;
9362 		}
9363 		if (last_type == BNXT_CTX_INV)
9364 			return 0;
9365 	}
9366 	ctx->ctx_arr[last_type].last = 1;
9367 
9368 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9369 		ctxm = &ctx->ctx_arr[type];
9370 
9371 		if (!ctxm->mem_valid)
9372 			continue;
9373 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9374 		if (rc)
9375 			return rc;
9376 	}
9377 	return 0;
9378 }
9379 
9380 /**
9381  * __bnxt_copy_ctx_mem - copy host context memory
9382  * @bp: The driver context
9383  * @ctxm: The pointer to the context memory type
9384  * @buf: The destination buffer or NULL to just obtain the length
9385  * @offset: The buffer offset to copy the data to
9386  * @head: The head offset of context memory to copy from
9387  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9388  *
9389  * This function is called for debugging purposes to dump the host context
9390  * used by the chip.
9391  *
9392  * Return: Length of memory copied
9393  */
9394 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9395 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9396 				  size_t offset, size_t head, size_t tail)
9397 {
9398 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9399 	size_t len = 0, total_len = 0;
9400 	int i, n = 1;
9401 
9402 	if (!ctx_pg)
9403 		return 0;
9404 
9405 	if (ctxm->instance_bmap)
9406 		n = hweight32(ctxm->instance_bmap);
9407 	for (i = 0; i < n; i++) {
9408 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9409 					    tail);
9410 		offset += len;
9411 		total_len += len;
9412 	}
9413 	return total_len;
9414 }
9415 
9416 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9417 			 void *buf, size_t offset)
9418 {
9419 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9420 
9421 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9422 }
9423 
9424 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9425 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9426 {
9427 	struct bnxt_ctx_pg_info *ctx_pg;
9428 	int i, n = 1;
9429 
9430 	ctxm->last = 0;
9431 
9432 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9433 		return;
9434 
9435 	ctx_pg = ctxm->pg_info;
9436 	if (ctx_pg) {
9437 		if (ctxm->instance_bmap)
9438 			n = hweight32(ctxm->instance_bmap);
9439 		for (i = 0; i < n; i++)
9440 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9441 
9442 		kfree(ctx_pg);
9443 		ctxm->pg_info = NULL;
9444 		ctxm->mem_valid = 0;
9445 	}
9446 	memset(ctxm, 0, sizeof(*ctxm));
9447 }
9448 
9449 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9450 {
9451 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9452 	u16 type;
9453 
9454 	if (!ctx)
9455 		return;
9456 
9457 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9458 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9459 
9460 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9461 	if (force) {
9462 		kfree(ctx);
9463 		bp->ctx = NULL;
9464 	}
9465 }
9466 
9467 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9468 {
9469 	struct bnxt_ctx_mem_type *ctxm;
9470 	struct bnxt_ctx_mem_info *ctx;
9471 	u32 l2_qps, qp1_qps, max_qps;
9472 	u32 ena, entries_sp, entries;
9473 	u32 srqs, max_srqs, min;
9474 	u32 num_mr, num_ah;
9475 	u32 extra_srqs = 0;
9476 	u32 extra_qps = 0;
9477 	u32 fast_qpmd_qps;
9478 	u8 pg_lvl = 1;
9479 	int i, rc;
9480 
9481 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9482 	if (rc) {
9483 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9484 			   rc);
9485 		return rc;
9486 	}
9487 	ctx = bp->ctx;
9488 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9489 		return 0;
9490 
9491 	ena = 0;
9492 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
9493 		goto skip_legacy;
9494 
9495 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9496 	l2_qps = ctxm->qp_l2_entries;
9497 	qp1_qps = ctxm->qp_qp1_entries;
9498 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9499 	max_qps = ctxm->max_entries;
9500 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9501 	srqs = ctxm->srq_l2_entries;
9502 	max_srqs = ctxm->max_entries;
9503 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9504 		pg_lvl = 2;
9505 		if (BNXT_SW_RES_LMT(bp)) {
9506 			extra_qps = max_qps - l2_qps - qp1_qps;
9507 			extra_srqs = max_srqs - srqs;
9508 		} else {
9509 			extra_qps = min_t(u32, 65536,
9510 					  max_qps - l2_qps - qp1_qps);
9511 			/* allocate extra qps if fw supports RoCE fast qp
9512 			 * destroy feature
9513 			 */
9514 			extra_qps += fast_qpmd_qps;
9515 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9516 		}
9517 		if (fast_qpmd_qps)
9518 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9519 	}
9520 
9521 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9522 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9523 				     pg_lvl);
9524 	if (rc)
9525 		return rc;
9526 
9527 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9528 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9529 	if (rc)
9530 		return rc;
9531 
9532 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9533 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9534 				     extra_qps * 2, pg_lvl);
9535 	if (rc)
9536 		return rc;
9537 
9538 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9539 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9540 	if (rc)
9541 		return rc;
9542 
9543 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9544 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9545 	if (rc)
9546 		return rc;
9547 
9548 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9549 		goto skip_rdma;
9550 
9551 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9552 	if (BNXT_SW_RES_LMT(bp) &&
9553 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9554 		num_ah = ctxm->mrav_av_entries;
9555 		num_mr = ctxm->max_entries - num_ah;
9556 	} else {
9557 		/* 128K extra is needed to accommodate static AH context
9558 		 * allocation by f/w.
9559 		 */
9560 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9561 		num_ah = min_t(u32, num_mr, 1024 * 128);
9562 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9563 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9564 			ctxm->mrav_av_entries = num_ah;
9565 	}
9566 
9567 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9568 	if (rc)
9569 		return rc;
9570 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9571 
9572 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9573 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9574 	if (rc)
9575 		return rc;
9576 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9577 
9578 skip_rdma:
9579 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9580 	min = ctxm->min_entries;
9581 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9582 		     2 * (extra_qps + qp1_qps) + min;
9583 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9584 	if (rc)
9585 		return rc;
9586 
9587 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9588 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9589 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9590 	if (rc)
9591 		return rc;
9592 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9593 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9594 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9595 
9596 skip_legacy:
9597 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9598 		rc = bnxt_backing_store_cfg_v2(bp);
9599 	else
9600 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9601 	if (rc) {
9602 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9603 			   rc);
9604 		return rc;
9605 	}
9606 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9607 	return 0;
9608 }
9609 
9610 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9611 {
9612 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9613 	u16 page_attr;
9614 	int rc;
9615 
9616 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9617 		return 0;
9618 
9619 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9620 	if (rc)
9621 		return rc;
9622 
9623 	if (BNXT_PAGE_SIZE == 0x2000)
9624 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9625 	else if (BNXT_PAGE_SIZE == 0x10000)
9626 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9627 	else
9628 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9629 	req->pg_size_lvl = cpu_to_le16(page_attr |
9630 				       bp->fw_crash_mem->ring_mem.depth);
9631 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9632 	req->size = cpu_to_le32(bp->fw_crash_len);
9633 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9634 	return hwrm_req_send(bp, req);
9635 }
9636 
9637 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9638 {
9639 	if (bp->fw_crash_mem) {
9640 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9641 		kfree(bp->fw_crash_mem);
9642 		bp->fw_crash_mem = NULL;
9643 	}
9644 }
9645 
9646 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9647 {
9648 	u32 mem_size = 0;
9649 	int rc;
9650 
9651 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9652 		return 0;
9653 
9654 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9655 	if (rc)
9656 		return rc;
9657 
9658 	mem_size = round_up(mem_size, 4);
9659 
9660 	/* keep and use the existing pages */
9661 	if (bp->fw_crash_mem &&
9662 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9663 		goto alloc_done;
9664 
9665 	if (bp->fw_crash_mem)
9666 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9667 	else
9668 		bp->fw_crash_mem = kzalloc_obj(*bp->fw_crash_mem);
9669 	if (!bp->fw_crash_mem)
9670 		return -ENOMEM;
9671 
9672 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9673 	if (rc) {
9674 		bnxt_free_crash_dump_mem(bp);
9675 		return rc;
9676 	}
9677 
9678 alloc_done:
9679 	bp->fw_crash_len = mem_size;
9680 	return 0;
9681 }
9682 
9683 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9684 {
9685 	struct hwrm_func_resource_qcaps_output *resp;
9686 	struct hwrm_func_resource_qcaps_input *req;
9687 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9688 	int rc;
9689 
9690 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9691 	if (rc)
9692 		return rc;
9693 
9694 	req->fid = cpu_to_le16(0xffff);
9695 	resp = hwrm_req_hold(bp, req);
9696 	rc = hwrm_req_send_silent(bp, req);
9697 	if (rc)
9698 		goto hwrm_func_resc_qcaps_exit;
9699 
9700 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9701 	if (!all)
9702 		goto hwrm_func_resc_qcaps_exit;
9703 
9704 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9705 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9706 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9707 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9708 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9709 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9710 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9711 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9712 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9713 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9714 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9715 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9716 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9717 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9718 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9719 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9720 
9721 	if (hw_resc->max_rsscos_ctxs >=
9722 	    hw_resc->max_vnics * BNXT_LARGE_RSS_TO_VNIC_RATIO)
9723 		bp->rss_cap |= BNXT_RSS_CAP_LARGE_RSS_CTX;
9724 
9725 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9726 		u16 max_msix = le16_to_cpu(resp->max_msix);
9727 
9728 		hw_resc->max_nqs = max_msix;
9729 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9730 	}
9731 
9732 	if (BNXT_PF(bp)) {
9733 		struct bnxt_pf_info *pf = &bp->pf;
9734 
9735 		pf->vf_resv_strategy =
9736 			le16_to_cpu(resp->vf_reservation_strategy);
9737 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9738 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9739 	}
9740 hwrm_func_resc_qcaps_exit:
9741 	hwrm_req_drop(bp, req);
9742 	return rc;
9743 }
9744 
9745 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9746 {
9747 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9748 	struct hwrm_port_mac_ptp_qcfg_input *req;
9749 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9750 	u8 flags;
9751 	int rc;
9752 
9753 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9754 		rc = -ENODEV;
9755 		goto no_ptp;
9756 	}
9757 
9758 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9759 	if (rc)
9760 		goto no_ptp;
9761 
9762 	req->port_id = cpu_to_le16(bp->pf.port_id);
9763 	resp = hwrm_req_hold(bp, req);
9764 	rc = hwrm_req_send(bp, req);
9765 	if (rc)
9766 		goto exit;
9767 
9768 	flags = resp->flags;
9769 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9770 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9771 		rc = -ENODEV;
9772 		goto exit;
9773 	}
9774 	if (!ptp) {
9775 		ptp = kzalloc_obj(*ptp);
9776 		if (!ptp) {
9777 			rc = -ENOMEM;
9778 			goto exit;
9779 		}
9780 		ptp->bp = bp;
9781 		bp->ptp_cfg = ptp;
9782 	}
9783 
9784 	if (flags &
9785 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9786 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9787 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9788 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9789 	} else if (BNXT_CHIP_P5(bp)) {
9790 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9791 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9792 	} else {
9793 		rc = -ENODEV;
9794 		goto exit;
9795 	}
9796 	ptp->rtc_configured =
9797 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9798 	rc = bnxt_ptp_init(bp);
9799 	if (rc)
9800 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9801 exit:
9802 	hwrm_req_drop(bp, req);
9803 	if (!rc)
9804 		return 0;
9805 
9806 no_ptp:
9807 	bnxt_ptp_clear(bp);
9808 	kfree(ptp);
9809 	bp->ptp_cfg = NULL;
9810 	return rc;
9811 }
9812 
9813 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9814 {
9815 	u32 flags, flags_ext, flags_ext2, flags_ext3;
9816 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9817 	struct hwrm_func_qcaps_output *resp;
9818 	struct hwrm_func_qcaps_input *req;
9819 	int rc;
9820 
9821 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9822 	if (rc)
9823 		return rc;
9824 
9825 	req->fid = cpu_to_le16(0xffff);
9826 	resp = hwrm_req_hold(bp, req);
9827 	rc = hwrm_req_send(bp, req);
9828 	if (rc)
9829 		goto hwrm_func_qcaps_exit;
9830 
9831 	flags = le32_to_cpu(resp->flags);
9832 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9833 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9834 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9835 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9836 	if (flags & FUNC_QCAPS_RESP_FLAGS_LINK_ADMIN_STATUS_SUPPORTED)
9837 		bp->fw_cap |= BNXT_FW_CAP_LINK_ADMIN;
9838 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9839 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9840 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9841 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9842 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9843 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9844 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9845 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9846 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9847 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9848 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9849 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9850 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9851 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9852 
9853 	flags_ext = le32_to_cpu(resp->flags_ext);
9854 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9855 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9856 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9857 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9858 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PTM_SUPPORTED)
9859 		bp->fw_cap |= BNXT_FW_CAP_PTP_PTM;
9860 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9861 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9862 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9863 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9864 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9865 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9866 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED)
9867 		bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2;
9868 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9869 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9870 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9871 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9872 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9873 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9874 
9875 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9876 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9877 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9878 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9879 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9880 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9881 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9882 	if (flags_ext2 &
9883 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9884 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9885 	if (BNXT_PF(bp) &&
9886 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9887 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9888 
9889 	flags_ext3 = le32_to_cpu(resp->flags_ext3);
9890 	if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_ROCE_VF_DYN_ALLOC_SUPPORT)
9891 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_DYN_ALLOC_SUPPORT;
9892 	if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_MIRROR_ON_ROCE_SUPPORTED)
9893 		bp->fw_cap |= BNXT_FW_CAP_MIRROR_ON_ROCE;
9894 
9895 	bp->tx_push_thresh = 0;
9896 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9897 	    BNXT_FW_MAJ(bp) > 217)
9898 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9899 
9900 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9901 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9902 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9903 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9904 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9905 	if (!hw_resc->max_hw_ring_grps)
9906 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9907 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9908 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9909 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9910 
9911 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9912 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9913 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9914 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9915 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9916 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9917 
9918 	if (BNXT_PF(bp)) {
9919 		struct bnxt_pf_info *pf = &bp->pf;
9920 
9921 		pf->fw_fid = le16_to_cpu(resp->fid);
9922 		pf->port_id = le16_to_cpu(resp->port_id);
9923 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9924 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9925 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9926 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9927 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9928 			bp->flags |= BNXT_FLAG_WOL_CAP;
9929 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9930 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9931 		} else {
9932 			bnxt_ptp_clear(bp);
9933 			kfree(bp->ptp_cfg);
9934 			bp->ptp_cfg = NULL;
9935 		}
9936 	} else {
9937 #ifdef CONFIG_BNXT_SRIOV
9938 		struct bnxt_vf_info *vf = &bp->vf;
9939 
9940 		vf->fw_fid = le16_to_cpu(resp->fid);
9941 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9942 #endif
9943 	}
9944 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9945 
9946 hwrm_func_qcaps_exit:
9947 	hwrm_req_drop(bp, req);
9948 	return rc;
9949 }
9950 
9951 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9952 {
9953 	struct hwrm_dbg_qcaps_output *resp;
9954 	struct hwrm_dbg_qcaps_input *req;
9955 	int rc;
9956 
9957 	bp->fw_dbg_cap = 0;
9958 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9959 		return;
9960 
9961 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9962 	if (rc)
9963 		return;
9964 
9965 	req->fid = cpu_to_le16(0xffff);
9966 	resp = hwrm_req_hold(bp, req);
9967 	rc = hwrm_req_send(bp, req);
9968 	if (rc)
9969 		goto hwrm_dbg_qcaps_exit;
9970 
9971 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9972 
9973 hwrm_dbg_qcaps_exit:
9974 	hwrm_req_drop(bp, req);
9975 }
9976 
9977 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9978 
9979 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9980 {
9981 	int rc;
9982 
9983 	rc = __bnxt_hwrm_func_qcaps(bp);
9984 	if (rc)
9985 		return rc;
9986 
9987 	bnxt_hwrm_dbg_qcaps(bp);
9988 
9989 	rc = bnxt_hwrm_queue_qportcfg(bp);
9990 	if (rc) {
9991 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9992 		return rc;
9993 	}
9994 	if (bp->hwrm_spec_code >= 0x10803) {
9995 		rc = bnxt_alloc_ctx_mem(bp);
9996 		if (rc)
9997 			return rc;
9998 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9999 		if (!rc)
10000 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
10001 	}
10002 	return 0;
10003 }
10004 
10005 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
10006 {
10007 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
10008 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
10009 	u32 flags;
10010 	int rc;
10011 
10012 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
10013 		return 0;
10014 
10015 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
10016 	if (rc)
10017 		return rc;
10018 
10019 	resp = hwrm_req_hold(bp, req);
10020 	rc = hwrm_req_send(bp, req);
10021 	if (rc)
10022 		goto hwrm_cfa_adv_qcaps_exit;
10023 
10024 	flags = le32_to_cpu(resp->flags);
10025 	if (flags &
10026 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
10027 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
10028 
10029 	if (flags &
10030 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
10031 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
10032 
10033 	if (flags &
10034 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
10035 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
10036 
10037 hwrm_cfa_adv_qcaps_exit:
10038 	hwrm_req_drop(bp, req);
10039 	return rc;
10040 }
10041 
10042 static int __bnxt_alloc_fw_health(struct bnxt *bp)
10043 {
10044 	if (bp->fw_health)
10045 		return 0;
10046 
10047 	bp->fw_health = kzalloc_obj(*bp->fw_health);
10048 	if (!bp->fw_health)
10049 		return -ENOMEM;
10050 
10051 	mutex_init(&bp->fw_health->lock);
10052 	return 0;
10053 }
10054 
10055 static int bnxt_alloc_fw_health(struct bnxt *bp)
10056 {
10057 	int rc;
10058 
10059 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
10060 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
10061 		return 0;
10062 
10063 	rc = __bnxt_alloc_fw_health(bp);
10064 	if (rc) {
10065 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
10066 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10067 		return rc;
10068 	}
10069 
10070 	return 0;
10071 }
10072 
10073 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
10074 {
10075 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
10076 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
10077 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
10078 }
10079 
10080 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
10081 {
10082 	struct bnxt_fw_health *fw_health = bp->fw_health;
10083 	u32 reg_type;
10084 
10085 	if (!fw_health)
10086 		return;
10087 
10088 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
10089 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
10090 		fw_health->status_reliable = false;
10091 
10092 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
10093 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
10094 		fw_health->resets_reliable = false;
10095 }
10096 
10097 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
10098 {
10099 	void __iomem *hs;
10100 	u32 status_loc;
10101 	u32 reg_type;
10102 	u32 sig;
10103 
10104 	if (bp->fw_health)
10105 		bp->fw_health->status_reliable = false;
10106 
10107 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
10108 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
10109 
10110 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
10111 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
10112 		if (!bp->chip_num) {
10113 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
10114 			bp->chip_num = readl(bp->bar0 +
10115 					     BNXT_FW_HEALTH_WIN_BASE +
10116 					     BNXT_GRC_REG_CHIP_NUM);
10117 		}
10118 		if (!BNXT_CHIP_P5_PLUS(bp))
10119 			return;
10120 
10121 		status_loc = BNXT_GRC_REG_STATUS_P5 |
10122 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
10123 	} else {
10124 		status_loc = readl(hs + offsetof(struct hcomm_status,
10125 						 fw_status_loc));
10126 	}
10127 
10128 	if (__bnxt_alloc_fw_health(bp)) {
10129 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
10130 		return;
10131 	}
10132 
10133 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
10134 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
10135 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
10136 		__bnxt_map_fw_health_reg(bp, status_loc);
10137 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
10138 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
10139 	}
10140 
10141 	bp->fw_health->status_reliable = true;
10142 }
10143 
10144 static int bnxt_map_fw_health_regs(struct bnxt *bp)
10145 {
10146 	struct bnxt_fw_health *fw_health = bp->fw_health;
10147 	u32 reg_base = 0xffffffff;
10148 	int i;
10149 
10150 	bp->fw_health->status_reliable = false;
10151 	bp->fw_health->resets_reliable = false;
10152 	/* Only pre-map the monitoring GRC registers using window 3 */
10153 	for (i = 0; i < 4; i++) {
10154 		u32 reg = fw_health->regs[i];
10155 
10156 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
10157 			continue;
10158 		if (reg_base == 0xffffffff)
10159 			reg_base = reg & BNXT_GRC_BASE_MASK;
10160 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
10161 			return -ERANGE;
10162 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
10163 	}
10164 	bp->fw_health->status_reliable = true;
10165 	bp->fw_health->resets_reliable = true;
10166 	if (reg_base == 0xffffffff)
10167 		return 0;
10168 
10169 	__bnxt_map_fw_health_reg(bp, reg_base);
10170 	return 0;
10171 }
10172 
10173 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
10174 {
10175 	if (!bp->fw_health)
10176 		return;
10177 
10178 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
10179 		bp->fw_health->status_reliable = true;
10180 		bp->fw_health->resets_reliable = true;
10181 	} else {
10182 		bnxt_try_map_fw_health_reg(bp);
10183 	}
10184 }
10185 
10186 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
10187 {
10188 	struct bnxt_fw_health *fw_health = bp->fw_health;
10189 	struct hwrm_error_recovery_qcfg_output *resp;
10190 	struct hwrm_error_recovery_qcfg_input *req;
10191 	int rc, i;
10192 
10193 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
10194 		return 0;
10195 
10196 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
10197 	if (rc)
10198 		return rc;
10199 
10200 	resp = hwrm_req_hold(bp, req);
10201 	rc = hwrm_req_send(bp, req);
10202 	if (rc)
10203 		goto err_recovery_out;
10204 	fw_health->flags = le32_to_cpu(resp->flags);
10205 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
10206 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
10207 		rc = -EINVAL;
10208 		goto err_recovery_out;
10209 	}
10210 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
10211 	fw_health->master_func_wait_dsecs =
10212 		le32_to_cpu(resp->master_func_wait_period);
10213 	fw_health->normal_func_wait_dsecs =
10214 		le32_to_cpu(resp->normal_func_wait_period);
10215 	fw_health->post_reset_wait_dsecs =
10216 		le32_to_cpu(resp->master_func_wait_period_after_reset);
10217 	fw_health->post_reset_max_wait_dsecs =
10218 		le32_to_cpu(resp->max_bailout_time_after_reset);
10219 	fw_health->regs[BNXT_FW_HEALTH_REG] =
10220 		le32_to_cpu(resp->fw_health_status_reg);
10221 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
10222 		le32_to_cpu(resp->fw_heartbeat_reg);
10223 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
10224 		le32_to_cpu(resp->fw_reset_cnt_reg);
10225 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
10226 		le32_to_cpu(resp->reset_inprogress_reg);
10227 	fw_health->fw_reset_inprog_reg_mask =
10228 		le32_to_cpu(resp->reset_inprogress_reg_mask);
10229 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
10230 	if (fw_health->fw_reset_seq_cnt >= 16) {
10231 		rc = -EINVAL;
10232 		goto err_recovery_out;
10233 	}
10234 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
10235 		fw_health->fw_reset_seq_regs[i] =
10236 			le32_to_cpu(resp->reset_reg[i]);
10237 		fw_health->fw_reset_seq_vals[i] =
10238 			le32_to_cpu(resp->reset_reg_val[i]);
10239 		fw_health->fw_reset_seq_delay_msec[i] =
10240 			resp->delay_after_reset[i];
10241 	}
10242 err_recovery_out:
10243 	hwrm_req_drop(bp, req);
10244 	if (!rc)
10245 		rc = bnxt_map_fw_health_regs(bp);
10246 	if (rc)
10247 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10248 	return rc;
10249 }
10250 
10251 static int bnxt_hwrm_func_reset(struct bnxt *bp)
10252 {
10253 	struct hwrm_func_reset_input *req;
10254 	int rc;
10255 
10256 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
10257 	if (rc)
10258 		return rc;
10259 
10260 	req->enables = 0;
10261 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
10262 	return hwrm_req_send(bp, req);
10263 }
10264 
10265 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
10266 {
10267 	struct hwrm_nvm_get_dev_info_output nvm_info;
10268 
10269 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
10270 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
10271 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
10272 			 nvm_info.nvm_cfg_ver_upd);
10273 }
10274 
10275 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
10276 {
10277 	struct hwrm_queue_qportcfg_output *resp;
10278 	struct hwrm_queue_qportcfg_input *req;
10279 	u8 i, j, *qptr;
10280 	bool no_rdma;
10281 	int rc = 0;
10282 
10283 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
10284 	if (rc)
10285 		return rc;
10286 
10287 	resp = hwrm_req_hold(bp, req);
10288 	rc = hwrm_req_send(bp, req);
10289 	if (rc)
10290 		goto qportcfg_exit;
10291 
10292 	if (!resp->max_configurable_queues) {
10293 		rc = -EINVAL;
10294 		goto qportcfg_exit;
10295 	}
10296 	bp->max_tc = resp->max_configurable_queues;
10297 	bp->max_lltc = resp->max_configurable_lossless_queues;
10298 	if (bp->max_tc > BNXT_MAX_QUEUE)
10299 		bp->max_tc = BNXT_MAX_QUEUE;
10300 
10301 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
10302 	qptr = &resp->queue_id0;
10303 	for (i = 0, j = 0; i < bp->max_tc; i++) {
10304 		bp->q_info[j].queue_id = *qptr;
10305 		bp->q_ids[i] = *qptr++;
10306 		bp->q_info[j].queue_profile = *qptr++;
10307 		bp->tc_to_qidx[j] = j;
10308 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
10309 		    (no_rdma && BNXT_PF(bp)))
10310 			j++;
10311 	}
10312 	bp->max_q = bp->max_tc;
10313 	bp->max_tc = max_t(u8, j, 1);
10314 
10315 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
10316 		bp->max_tc = 1;
10317 
10318 	if (bp->max_lltc > bp->max_tc)
10319 		bp->max_lltc = bp->max_tc;
10320 
10321 qportcfg_exit:
10322 	hwrm_req_drop(bp, req);
10323 	return rc;
10324 }
10325 
10326 static int bnxt_hwrm_poll(struct bnxt *bp)
10327 {
10328 	struct hwrm_ver_get_input *req;
10329 	int rc;
10330 
10331 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10332 	if (rc)
10333 		return rc;
10334 
10335 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10336 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10337 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10338 
10339 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
10340 	rc = hwrm_req_send(bp, req);
10341 	return rc;
10342 }
10343 
10344 static int bnxt_hwrm_ver_get(struct bnxt *bp)
10345 {
10346 	struct hwrm_ver_get_output *resp;
10347 	struct hwrm_ver_get_input *req;
10348 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
10349 	u32 dev_caps_cfg, hwrm_ver;
10350 	int rc, len, max_tmo_secs;
10351 
10352 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10353 	if (rc)
10354 		return rc;
10355 
10356 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10357 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
10358 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10359 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10360 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10361 
10362 	resp = hwrm_req_hold(bp, req);
10363 	rc = hwrm_req_send(bp, req);
10364 	if (rc)
10365 		goto hwrm_ver_get_exit;
10366 
10367 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
10368 
10369 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
10370 			     resp->hwrm_intf_min_8b << 8 |
10371 			     resp->hwrm_intf_upd_8b;
10372 	if (resp->hwrm_intf_maj_8b < 1) {
10373 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
10374 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10375 			    resp->hwrm_intf_upd_8b);
10376 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10377 	}
10378 
10379 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10380 			HWRM_VERSION_UPDATE;
10381 
10382 	if (bp->hwrm_spec_code > hwrm_ver)
10383 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10384 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10385 			 HWRM_VERSION_UPDATE);
10386 	else
10387 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10388 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10389 			 resp->hwrm_intf_upd_8b);
10390 
10391 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10392 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10393 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10394 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10395 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10396 		len = FW_VER_STR_LEN;
10397 	} else {
10398 		fw_maj = resp->hwrm_fw_maj_8b;
10399 		fw_min = resp->hwrm_fw_min_8b;
10400 		fw_bld = resp->hwrm_fw_bld_8b;
10401 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10402 		len = BC_HWRM_STR_LEN;
10403 	}
10404 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10405 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10406 		 fw_rsv);
10407 
10408 	if (strlen(resp->active_pkg_name)) {
10409 		int fw_ver_len = strlen(bp->fw_ver_str);
10410 
10411 		snprintf(bp->fw_ver_str + fw_ver_len,
10412 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10413 			 resp->active_pkg_name);
10414 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10415 	}
10416 
10417 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10418 	if (!bp->hwrm_cmd_timeout)
10419 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10420 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10421 	if (!bp->hwrm_cmd_max_timeout)
10422 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10423 	max_tmo_secs = bp->hwrm_cmd_max_timeout / 1000;
10424 #ifdef CONFIG_DETECT_HUNG_TASK
10425 	if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT ||
10426 	    max_tmo_secs > CONFIG_DEFAULT_HUNG_TASK_TIMEOUT) {
10427 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog (kernel default %ds)\n",
10428 			    max_tmo_secs, CONFIG_DEFAULT_HUNG_TASK_TIMEOUT);
10429 	}
10430 #endif
10431 
10432 	if (resp->hwrm_intf_maj_8b >= 1) {
10433 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10434 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10435 	}
10436 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10437 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10438 
10439 	bp->chip_num = le16_to_cpu(resp->chip_num);
10440 	bp->chip_rev = resp->chip_rev;
10441 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10442 	    !resp->chip_metal)
10443 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10444 
10445 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10446 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10447 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10448 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10449 
10450 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10451 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10452 
10453 	if (dev_caps_cfg &
10454 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10455 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10456 
10457 	if (dev_caps_cfg &
10458 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10459 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10460 
10461 	if (dev_caps_cfg &
10462 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10463 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10464 
10465 hwrm_ver_get_exit:
10466 	hwrm_req_drop(bp, req);
10467 	return rc;
10468 }
10469 
10470 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10471 {
10472 	struct hwrm_fw_set_time_input *req;
10473 	struct tm tm;
10474 	time64_t now = ktime_get_real_seconds();
10475 	int rc;
10476 
10477 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10478 	    bp->hwrm_spec_code < 0x10400)
10479 		return -EOPNOTSUPP;
10480 
10481 	time64_to_tm(now, 0, &tm);
10482 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10483 	if (rc)
10484 		return rc;
10485 
10486 	req->year = cpu_to_le16(1900 + tm.tm_year);
10487 	req->month = 1 + tm.tm_mon;
10488 	req->day = tm.tm_mday;
10489 	req->hour = tm.tm_hour;
10490 	req->minute = tm.tm_min;
10491 	req->second = tm.tm_sec;
10492 	return hwrm_req_send(bp, req);
10493 }
10494 
10495 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10496 {
10497 	u64 sw_tmp;
10498 
10499 	hw &= mask;
10500 	sw_tmp = (*sw & ~mask) | hw;
10501 	if (hw < (*sw & mask))
10502 		sw_tmp += mask + 1;
10503 	WRITE_ONCE(*sw, sw_tmp);
10504 }
10505 
10506 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10507 				    int count, bool ignore_zero)
10508 {
10509 	int i;
10510 
10511 	for (i = 0; i < count; i++) {
10512 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10513 
10514 		if (ignore_zero && !hw)
10515 			continue;
10516 
10517 		if (masks[i] == -1ULL)
10518 			sw_stats[i] = hw;
10519 		else
10520 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10521 	}
10522 }
10523 
10524 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10525 {
10526 	if (!stats->hw_stats)
10527 		return;
10528 
10529 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10530 				stats->hw_masks, stats->len / 8, false);
10531 }
10532 
10533 static void bnxt_accumulate_ring_stats(struct bnxt *bp)
10534 {
10535 	struct bnxt_stats_mem *ring0_stats;
10536 	bool ignore_zero = false;
10537 	int i;
10538 
10539 	/* Chip bug.  Counter intermittently becomes 0. */
10540 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10541 		ignore_zero = true;
10542 
10543 	for (i = 0; i < bp->cp_nr_rings; i++) {
10544 		struct bnxt_napi *bnapi = bp->bnapi[i];
10545 		struct bnxt_cp_ring_info *cpr;
10546 		struct bnxt_stats_mem *stats;
10547 
10548 		cpr = &bnapi->cp_ring;
10549 		stats = &cpr->stats;
10550 		if (!i)
10551 			ring0_stats = stats;
10552 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10553 					ring0_stats->hw_masks,
10554 					ring0_stats->len / 8, ignore_zero);
10555 	}
10556 }
10557 
10558 static void bnxt_accumulate_port_stats(struct bnxt *bp)
10559 {
10560 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10561 		struct bnxt_stats_mem *stats = &bp->port_stats;
10562 		__le64 *hw_stats = stats->hw_stats;
10563 		u64 *sw_stats = stats->sw_stats;
10564 		u64 *masks = stats->hw_masks;
10565 		int cnt;
10566 
10567 		cnt = sizeof(struct rx_port_stats) / 8;
10568 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10569 
10570 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10571 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10572 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10573 		cnt = sizeof(struct tx_port_stats) / 8;
10574 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10575 	}
10576 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10577 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10578 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10579 	}
10580 }
10581 
10582 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10583 {
10584 	bnxt_accumulate_ring_stats(bp);
10585 	bnxt_accumulate_port_stats(bp);
10586 }
10587 
10588 /* Re-accumulate ring stats from DMA buffers if stale.
10589  * uAPIs for reading sw_stats should call this first.
10590  *
10591  * We promise user space update frequency of bp->stats_coal_ticks but
10592  * the update is a two step process - first device updates the DMA buffer,
10593  * then we have to update from that buffer to driver stats in the service work.
10594  * Worst case we would be 2x off from the desired frequency.
10595  * Sync the stats sooner, if stale. The 20% threshold was chosen arbitrarily.
10596  *
10597  * Ideally we would split the user-configured time into two portions,
10598  * i.e. also lower the DMA period by the 20%. But the DMA timer seems to have
10599  * too coarse granularity to play such tricks.
10600  */
10601 void bnxt_sync_ring_stats(struct bnxt *bp)
10602 {
10603 	unsigned long stale;
10604 
10605 	if (!netif_running(bp->dev) || !bp->stats_coal_ticks)
10606 		return;
10607 
10608 	spin_lock(&bp->stats_lock);
10609 	stale = usecs_to_jiffies(bp->stats_coal_ticks / 5);
10610 	if (time_after_eq(jiffies, bp->stats_updated_jiffies + stale)) {
10611 		bnxt_accumulate_ring_stats(bp);
10612 		bp->stats_updated_jiffies = jiffies;
10613 	}
10614 	spin_unlock(&bp->stats_lock);
10615 }
10616 
10617 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10618 {
10619 	struct hwrm_port_qstats_input *req;
10620 	struct bnxt_pf_info *pf = &bp->pf;
10621 	int rc;
10622 
10623 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10624 		return 0;
10625 
10626 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10627 		return -EOPNOTSUPP;
10628 
10629 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10630 	if (rc)
10631 		return rc;
10632 
10633 	req->flags = flags;
10634 	req->port_id = cpu_to_le16(pf->port_id);
10635 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10636 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10637 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10638 	return hwrm_req_send(bp, req);
10639 }
10640 
10641 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10642 {
10643 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10644 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10645 	struct hwrm_port_qstats_ext_output *resp_qs;
10646 	struct hwrm_port_qstats_ext_input *req_qs;
10647 	struct bnxt_pf_info *pf = &bp->pf;
10648 	u32 tx_stat_size;
10649 	int rc;
10650 
10651 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10652 		return 0;
10653 
10654 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10655 		return -EOPNOTSUPP;
10656 
10657 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10658 	if (rc)
10659 		return rc;
10660 
10661 	req_qs->flags = flags;
10662 	req_qs->port_id = cpu_to_le16(pf->port_id);
10663 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10664 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10665 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10666 		       sizeof(struct tx_port_stats_ext) : 0;
10667 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10668 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10669 	resp_qs = hwrm_req_hold(bp, req_qs);
10670 	rc = hwrm_req_send(bp, req_qs);
10671 	if (!rc) {
10672 		bp->fw_rx_stats_ext_size =
10673 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10674 		if (BNXT_FW_MAJ(bp) < 220 &&
10675 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10676 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10677 
10678 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10679 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10680 	} else {
10681 		bp->fw_rx_stats_ext_size = 0;
10682 		bp->fw_tx_stats_ext_size = 0;
10683 	}
10684 	hwrm_req_drop(bp, req_qs);
10685 
10686 	if (flags)
10687 		return rc;
10688 
10689 	if (bp->fw_tx_stats_ext_size <=
10690 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10691 		bp->pri2cos_valid = 0;
10692 		return rc;
10693 	}
10694 
10695 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10696 	if (rc)
10697 		return rc;
10698 
10699 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10700 
10701 	resp_qc = hwrm_req_hold(bp, req_qc);
10702 	rc = hwrm_req_send(bp, req_qc);
10703 	if (!rc) {
10704 		u8 *pri2cos;
10705 		int i, j;
10706 
10707 		pri2cos = &resp_qc->pri0_cos_queue_id;
10708 		for (i = 0; i < 8; i++) {
10709 			u8 queue_id = pri2cos[i];
10710 			u8 queue_idx;
10711 
10712 			/* Per port queue IDs start from 0, 10, 20, etc */
10713 			queue_idx = queue_id % 10;
10714 			if (queue_idx > BNXT_MAX_QUEUE) {
10715 				bp->pri2cos_valid = false;
10716 				hwrm_req_drop(bp, req_qc);
10717 				return rc;
10718 			}
10719 			for (j = 0; j < bp->max_q; j++) {
10720 				if (bp->q_ids[j] == queue_id)
10721 					bp->pri2cos_idx[i] = queue_idx;
10722 			}
10723 		}
10724 		bp->pri2cos_valid = true;
10725 	}
10726 	hwrm_req_drop(bp, req_qc);
10727 
10728 	return rc;
10729 }
10730 
10731 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10732 {
10733 	bnxt_hwrm_tunnel_dst_port_free(bp,
10734 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10735 	bnxt_hwrm_tunnel_dst_port_free(bp,
10736 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10737 }
10738 
10739 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10740 {
10741 	int rc, i;
10742 	u32 tpa_flags = 0;
10743 
10744 	if (set_tpa)
10745 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10746 	else if (BNXT_NO_FW_ACCESS(bp))
10747 		return 0;
10748 	for (i = 0; i < bp->nr_vnics; i++) {
10749 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10750 		if (rc) {
10751 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10752 				   i, rc);
10753 			return rc;
10754 		}
10755 	}
10756 	return 0;
10757 }
10758 
10759 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10760 {
10761 	int i;
10762 
10763 	for (i = 0; i < bp->nr_vnics; i++)
10764 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10765 }
10766 
10767 static void bnxt_clear_vnic(struct bnxt *bp)
10768 {
10769 	if (!bp->vnic_info)
10770 		return;
10771 
10772 	bnxt_hwrm_clear_vnic_filter(bp);
10773 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10774 		/* clear all RSS setting before free vnic ctx */
10775 		bnxt_hwrm_clear_vnic_rss(bp);
10776 		bnxt_hwrm_vnic_ctx_free(bp);
10777 	}
10778 	/* before free the vnic, undo the vnic tpa settings */
10779 	if (bp->flags & BNXT_FLAG_TPA)
10780 		bnxt_set_tpa(bp, false);
10781 	bnxt_hwrm_vnic_free(bp);
10782 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10783 		bnxt_hwrm_vnic_ctx_free(bp);
10784 }
10785 
10786 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10787 				    bool irq_re_init)
10788 {
10789 	bnxt_clear_vnic(bp);
10790 	bnxt_hwrm_ring_free(bp, close_path);
10791 	bnxt_hwrm_ring_grp_free(bp);
10792 	if (irq_re_init) {
10793 		bnxt_hwrm_stat_ctx_free(bp);
10794 		bnxt_hwrm_free_tunnel_ports(bp);
10795 	}
10796 }
10797 
10798 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10799 {
10800 	struct hwrm_func_cfg_input *req;
10801 	u8 evb_mode;
10802 	int rc;
10803 
10804 	if (br_mode == BRIDGE_MODE_VEB)
10805 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10806 	else if (br_mode == BRIDGE_MODE_VEPA)
10807 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10808 	else
10809 		return -EINVAL;
10810 
10811 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10812 	if (rc)
10813 		return rc;
10814 
10815 	req->fid = cpu_to_le16(0xffff);
10816 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10817 	req->evb_mode = evb_mode;
10818 	return hwrm_req_send(bp, req);
10819 }
10820 
10821 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10822 {
10823 	struct hwrm_func_cfg_input *req;
10824 	int rc;
10825 
10826 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10827 		return 0;
10828 
10829 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10830 	if (rc)
10831 		return rc;
10832 
10833 	req->fid = cpu_to_le16(0xffff);
10834 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10835 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10836 	if (size == 128)
10837 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10838 
10839 	return hwrm_req_send(bp, req);
10840 }
10841 
10842 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10843 {
10844 	int rc;
10845 
10846 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10847 		goto skip_rss_ctx;
10848 
10849 	/* allocate context for vnic */
10850 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10851 	if (rc) {
10852 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10853 			   vnic->vnic_id, rc);
10854 		goto vnic_setup_err;
10855 	}
10856 	bp->rsscos_nr_ctxs++;
10857 
10858 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10859 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10860 		if (rc) {
10861 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10862 				   vnic->vnic_id, rc);
10863 			goto vnic_setup_err;
10864 		}
10865 		bp->rsscos_nr_ctxs++;
10866 	}
10867 
10868 skip_rss_ctx:
10869 	/* configure default vnic, ring grp */
10870 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10871 	if (rc) {
10872 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10873 			   vnic->vnic_id, rc);
10874 		goto vnic_setup_err;
10875 	}
10876 
10877 	/* Enable RSS hashing on vnic */
10878 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10879 	if (rc) {
10880 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10881 			   vnic->vnic_id, rc);
10882 		goto vnic_setup_err;
10883 	}
10884 
10885 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10886 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10887 		if (rc) {
10888 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10889 				   vnic->vnic_id, rc);
10890 		}
10891 	}
10892 
10893 vnic_setup_err:
10894 	return rc;
10895 }
10896 
10897 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10898 			  u8 valid)
10899 {
10900 	struct hwrm_vnic_update_input *req;
10901 	int rc;
10902 
10903 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10904 	if (rc)
10905 		return rc;
10906 
10907 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10908 
10909 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10910 		req->mru = cpu_to_le16(vnic->mru);
10911 
10912 	req->enables = cpu_to_le32(valid);
10913 
10914 	return hwrm_req_send(bp, req);
10915 }
10916 
10917 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10918 {
10919 	int rc;
10920 
10921 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10922 	if (rc) {
10923 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10924 			   vnic->vnic_id, rc);
10925 		return rc;
10926 	}
10927 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10928 	if (rc)
10929 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10930 			   vnic->vnic_id, rc);
10931 	return rc;
10932 }
10933 
10934 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10935 {
10936 	int rc, i, nr_ctxs;
10937 
10938 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10939 	for (i = 0; i < nr_ctxs; i++) {
10940 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10941 		if (rc) {
10942 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10943 				   vnic->vnic_id, i, rc);
10944 			break;
10945 		}
10946 		bp->rsscos_nr_ctxs++;
10947 	}
10948 	if (i < nr_ctxs)
10949 		return -ENOMEM;
10950 
10951 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10952 	if (rc)
10953 		return rc;
10954 
10955 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10956 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10957 		if (rc) {
10958 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10959 				   vnic->vnic_id, rc);
10960 		}
10961 	}
10962 	return rc;
10963 }
10964 
10965 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10966 {
10967 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10968 		return __bnxt_setup_vnic_p5(bp, vnic);
10969 	else
10970 		return __bnxt_setup_vnic(bp, vnic);
10971 }
10972 
10973 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10974 				     struct bnxt_vnic_info *vnic,
10975 				     u16 start_rx_ring_idx, int rx_rings)
10976 {
10977 	int rc;
10978 
10979 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10980 	if (rc) {
10981 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10982 			   vnic->vnic_id, rc);
10983 		return rc;
10984 	}
10985 	return bnxt_setup_vnic(bp, vnic);
10986 }
10987 
10988 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10989 {
10990 	struct bnxt_vnic_info *vnic;
10991 	int i, rc = 0;
10992 
10993 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10994 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10995 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10996 	}
10997 
10998 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10999 		return 0;
11000 
11001 	for (i = 0; i < bp->rx_nr_rings; i++) {
11002 		u16 vnic_id = i + 1;
11003 		u16 ring_id = i;
11004 
11005 		if (vnic_id >= bp->nr_vnics)
11006 			break;
11007 
11008 		vnic = &bp->vnic_info[vnic_id];
11009 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
11010 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
11011 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
11012 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
11013 			break;
11014 	}
11015 	return rc;
11016 }
11017 
11018 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
11019 			  bool all)
11020 {
11021 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
11022 	struct bnxt_filter_base *usr_fltr, *tmp;
11023 	struct bnxt_ntuple_filter *ntp_fltr;
11024 	int i;
11025 
11026 	bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
11027 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
11028 		if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
11029 			bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
11030 	}
11031 	if (!all)
11032 		return;
11033 
11034 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
11035 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
11036 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
11037 			ntp_fltr = container_of(usr_fltr,
11038 						struct bnxt_ntuple_filter,
11039 						base);
11040 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
11041 			bnxt_del_ntp_filter(bp, ntp_fltr);
11042 			bnxt_del_one_usr_fltr(bp, usr_fltr);
11043 		}
11044 	}
11045 
11046 	if (vnic->rss_table)
11047 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
11048 				  vnic->rss_table,
11049 				  vnic->rss_table_dma_addr);
11050 	bp->num_rss_ctx--;
11051 }
11052 
11053 static bool bnxt_vnic_has_rx_ring(struct bnxt *bp, struct bnxt_vnic_info *vnic,
11054 				  int rxr_id)
11055 {
11056 	u16 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
11057 	int i, vnic_rx;
11058 
11059 	/* Ntuple VNIC always has all the rx rings. Any change of ring id
11060 	 * must be updated because a future filter may use it.
11061 	 */
11062 	if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
11063 		return true;
11064 
11065 	for (i = 0; i < tbl_size; i++) {
11066 		if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
11067 			vnic_rx = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
11068 		else
11069 			vnic_rx = bp->rss_indir_tbl[i];
11070 
11071 		if (rxr_id == vnic_rx)
11072 			return true;
11073 	}
11074 
11075 	return false;
11076 }
11077 
11078 static int bnxt_set_vnic_mru_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic,
11079 				u16 mru, int rxr_id)
11080 {
11081 	int rc;
11082 
11083 	if (!bnxt_vnic_has_rx_ring(bp, vnic, rxr_id))
11084 		return 0;
11085 
11086 	if (mru) {
11087 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
11088 		if (rc) {
11089 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
11090 				   vnic->vnic_id, rc);
11091 			return rc;
11092 		}
11093 	}
11094 	vnic->mru = mru;
11095 	bnxt_hwrm_vnic_update(bp, vnic,
11096 			      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
11097 
11098 	return 0;
11099 }
11100 
11101 static int bnxt_set_rss_ctx_vnic_mru(struct bnxt *bp, u16 mru, int rxr_id)
11102 {
11103 	struct ethtool_rxfh_context *ctx;
11104 	unsigned long context;
11105 	int rc;
11106 
11107 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11108 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11109 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
11110 
11111 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, rxr_id);
11112 		if (rc)
11113 			return rc;
11114 	}
11115 
11116 	return 0;
11117 }
11118 
11119 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
11120 {
11121 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
11122 	struct ethtool_rxfh_context *ctx;
11123 	unsigned long context;
11124 
11125 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11126 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11127 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
11128 
11129 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
11130 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
11131 		    __bnxt_setup_vnic_p5(bp, vnic)) {
11132 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
11133 				   rss_ctx->index);
11134 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
11135 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
11136 		}
11137 	}
11138 }
11139 
11140 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
11141 {
11142 	struct ethtool_rxfh_context *ctx;
11143 	unsigned long context;
11144 
11145 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11146 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11147 
11148 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
11149 	}
11150 }
11151 
11152 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
11153 static bool bnxt_promisc_ok(struct bnxt *bp)
11154 {
11155 #ifdef CONFIG_BNXT_SRIOV
11156 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
11157 		return false;
11158 #endif
11159 	return true;
11160 }
11161 
11162 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
11163 {
11164 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
11165 	unsigned int rc = 0;
11166 
11167 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
11168 	if (rc) {
11169 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
11170 			   rc);
11171 		return rc;
11172 	}
11173 
11174 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
11175 	if (rc) {
11176 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
11177 			   rc);
11178 		return rc;
11179 	}
11180 	return rc;
11181 }
11182 
11183 static int bnxt_cfg_rx_mode(struct bnxt *, struct netdev_hw_addr_list *, bool);
11184 static bool bnxt_mc_list_updated(struct bnxt *, u32 *,
11185 				 const struct netdev_hw_addr_list *);
11186 
11187 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
11188 {
11189 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
11190 	int rc = 0;
11191 	unsigned int rx_nr_rings = bp->rx_nr_rings;
11192 
11193 	if (irq_re_init) {
11194 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
11195 		if (rc) {
11196 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
11197 				   rc);
11198 			goto err_out;
11199 		}
11200 	}
11201 
11202 	rc = bnxt_hwrm_ring_alloc(bp);
11203 	if (rc) {
11204 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
11205 		goto err_out;
11206 	}
11207 
11208 	rc = bnxt_hwrm_ring_grp_alloc(bp);
11209 	if (rc) {
11210 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
11211 		goto err_out;
11212 	}
11213 
11214 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11215 		rx_nr_rings--;
11216 
11217 	/* default vnic 0 */
11218 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
11219 	if (rc) {
11220 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
11221 		goto err_out;
11222 	}
11223 
11224 	if (BNXT_VF(bp))
11225 		bnxt_hwrm_func_qcfg(bp);
11226 
11227 	rc = bnxt_setup_vnic(bp, vnic);
11228 	if (rc)
11229 		goto err_out;
11230 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
11231 		bnxt_hwrm_update_rss_hash_cfg(bp);
11232 
11233 	if (bp->flags & BNXT_FLAG_RFS) {
11234 		rc = bnxt_alloc_rfs_vnics(bp);
11235 		if (rc)
11236 			goto err_out;
11237 	}
11238 
11239 	if (bp->flags & BNXT_FLAG_TPA) {
11240 		rc = bnxt_set_tpa(bp, true);
11241 		if (rc)
11242 			goto err_out;
11243 	}
11244 
11245 	if (BNXT_VF(bp))
11246 		bnxt_update_vf_mac(bp);
11247 
11248 	/* Filter for default vnic 0 */
11249 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
11250 	if (rc) {
11251 		if (BNXT_VF(bp) && rc == -ENODEV)
11252 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
11253 		else
11254 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
11255 		goto err_out;
11256 	}
11257 	vnic->uc_filter_count = 1;
11258 
11259 	vnic->rx_mask = 0;
11260 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
11261 		goto skip_rx_mask;
11262 
11263 	if (bp->dev->flags & IFF_BROADCAST)
11264 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
11265 
11266 	if (bp->dev->flags & IFF_PROMISC)
11267 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11268 
11269 	if (bp->dev->flags & IFF_ALLMULTI) {
11270 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
11271 		vnic->mc_list_count = 0;
11272 	} else if (bp->dev->flags & IFF_MULTICAST) {
11273 		u32 mask = 0;
11274 
11275 		bnxt_mc_list_updated(bp, &mask, &bp->dev->mc);
11276 		vnic->rx_mask |= mask;
11277 	}
11278 
11279 	rc = bnxt_cfg_rx_mode(bp, &bp->dev->uc, true);
11280 	if (rc == -EAGAIN) {
11281 		netif_rx_mode_schedule_retry(bp->dev);
11282 		rc = 0;
11283 	} else if (rc) {
11284 		goto err_out;
11285 	}
11286 
11287 skip_rx_mask:
11288 	rc = bnxt_hwrm_set_coal(bp);
11289 	if (rc)
11290 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
11291 				rc);
11292 
11293 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11294 		rc = bnxt_setup_nitroa0_vnic(bp);
11295 		if (rc)
11296 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
11297 				   rc);
11298 	}
11299 
11300 	if (BNXT_VF(bp)) {
11301 		bnxt_hwrm_func_qcfg(bp);
11302 		netdev_update_features(bp->dev);
11303 	}
11304 
11305 	return 0;
11306 
11307 err_out:
11308 	bnxt_hwrm_resource_free(bp, 0, true);
11309 
11310 	return rc;
11311 }
11312 
11313 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
11314 {
11315 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
11316 	return 0;
11317 }
11318 
11319 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
11320 {
11321 	bnxt_init_cp_rings(bp);
11322 	bnxt_init_rx_rings(bp);
11323 	bnxt_init_tx_rings(bp);
11324 	bnxt_init_ring_grps(bp, irq_re_init);
11325 	bnxt_init_vnics(bp);
11326 
11327 	return bnxt_init_chip(bp, irq_re_init);
11328 }
11329 
11330 static int bnxt_set_real_num_queues(struct bnxt *bp)
11331 {
11332 	int rc;
11333 	struct net_device *dev = bp->dev;
11334 
11335 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
11336 					  bp->tx_nr_rings_xdp);
11337 	if (rc)
11338 		return rc;
11339 
11340 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
11341 	if (rc)
11342 		return rc;
11343 
11344 #ifdef CONFIG_RFS_ACCEL
11345 	if (bp->flags & BNXT_FLAG_RFS)
11346 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
11347 #endif
11348 
11349 	return rc;
11350 }
11351 
11352 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11353 			     bool shared)
11354 {
11355 	int _rx = *rx, _tx = *tx;
11356 
11357 	if (shared) {
11358 		*rx = min_t(int, _rx, max);
11359 		*tx = min_t(int, _tx, max);
11360 	} else {
11361 		if (max < 2)
11362 			return -ENOMEM;
11363 
11364 		while (_rx + _tx > max) {
11365 			if (_rx > _tx && _rx > 1)
11366 				_rx--;
11367 			else if (_tx > 1)
11368 				_tx--;
11369 		}
11370 		*rx = _rx;
11371 		*tx = _tx;
11372 	}
11373 	return 0;
11374 }
11375 
11376 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
11377 {
11378 	return (tx - tx_xdp) / tx_sets + tx_xdp;
11379 }
11380 
11381 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
11382 {
11383 	int tcs = bp->num_tc;
11384 
11385 	if (!tcs)
11386 		tcs = 1;
11387 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
11388 }
11389 
11390 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
11391 {
11392 	int tcs = bp->num_tc;
11393 
11394 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
11395 	       bp->tx_nr_rings_xdp;
11396 }
11397 
11398 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11399 			   bool sh)
11400 {
11401 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
11402 
11403 	if (tx_cp != *tx) {
11404 		int tx_saved = tx_cp, rc;
11405 
11406 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
11407 		if (rc)
11408 			return rc;
11409 		if (tx_cp != tx_saved)
11410 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
11411 		return 0;
11412 	}
11413 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
11414 }
11415 
11416 static void bnxt_setup_msix(struct bnxt *bp)
11417 {
11418 	const int len = sizeof(bp->irq_tbl[0].name);
11419 	struct net_device *dev = bp->dev;
11420 	int tcs, i;
11421 
11422 	tcs = bp->num_tc;
11423 	if (tcs) {
11424 		int i, off, count;
11425 
11426 		for (i = 0; i < tcs; i++) {
11427 			count = bp->tx_nr_rings_per_tc;
11428 			off = BNXT_TC_TO_RING_BASE(bp, i);
11429 			netdev_set_tc_queue(dev, i, count, off);
11430 		}
11431 	}
11432 
11433 	for (i = 0; i < bp->cp_nr_rings; i++) {
11434 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11435 		char *attr;
11436 
11437 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11438 			attr = "TxRx";
11439 		else if (i < bp->rx_nr_rings)
11440 			attr = "rx";
11441 		else
11442 			attr = "tx";
11443 
11444 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
11445 			 attr, i);
11446 		bp->irq_tbl[map_idx].handler = bnxt_msix;
11447 	}
11448 }
11449 
11450 static int bnxt_init_int_mode(struct bnxt *bp);
11451 
11452 static int bnxt_change_msix(struct bnxt *bp, int total)
11453 {
11454 	struct msi_map map;
11455 	int i;
11456 
11457 	/* add MSIX to the end if needed */
11458 	for (i = bp->total_irqs; i < total; i++) {
11459 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
11460 		if (map.index < 0)
11461 			return bp->total_irqs;
11462 		bp->irq_tbl[i].vector = map.virq;
11463 		bp->total_irqs++;
11464 	}
11465 
11466 	/* trim MSIX from the end if needed */
11467 	for (i = bp->total_irqs; i > total; i--) {
11468 		map.index = i - 1;
11469 		map.virq = bp->irq_tbl[i - 1].vector;
11470 		pci_msix_free_irq(bp->pdev, map);
11471 		bp->total_irqs--;
11472 	}
11473 	return bp->total_irqs;
11474 }
11475 
11476 static int bnxt_setup_int_mode(struct bnxt *bp)
11477 {
11478 	int rc;
11479 
11480 	if (!bp->irq_tbl) {
11481 		rc = bnxt_init_int_mode(bp);
11482 		if (rc || !bp->irq_tbl)
11483 			return rc ?: -ENODEV;
11484 	}
11485 
11486 	bnxt_setup_msix(bp);
11487 
11488 	rc = bnxt_set_real_num_queues(bp);
11489 	return rc;
11490 }
11491 
11492 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
11493 {
11494 	return bp->hw_resc.max_rsscos_ctxs;
11495 }
11496 
11497 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
11498 {
11499 	return bp->hw_resc.max_vnics;
11500 }
11501 
11502 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11503 {
11504 	return bp->hw_resc.max_stat_ctxs;
11505 }
11506 
11507 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11508 {
11509 	return bp->hw_resc.max_cp_rings;
11510 }
11511 
11512 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11513 {
11514 	unsigned int cp = bp->hw_resc.max_cp_rings;
11515 
11516 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11517 		cp -= bnxt_get_ulp_msix_num(bp);
11518 
11519 	return cp;
11520 }
11521 
11522 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11523 {
11524 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11525 
11526 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11527 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11528 
11529 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11530 }
11531 
11532 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11533 {
11534 	bp->hw_resc.max_irqs = max_irqs;
11535 }
11536 
11537 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11538 {
11539 	unsigned int cp;
11540 
11541 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11542 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11543 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11544 	else
11545 		return cp - bp->cp_nr_rings;
11546 }
11547 
11548 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11549 {
11550 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11551 }
11552 
11553 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11554 {
11555 	int max_irq = bnxt_get_max_func_irqs(bp);
11556 	int total_req = bp->cp_nr_rings + num;
11557 
11558 	if (max_irq < total_req) {
11559 		num = max_irq - bp->cp_nr_rings;
11560 		if (num <= 0)
11561 			return 0;
11562 	}
11563 	return num;
11564 }
11565 
11566 static int bnxt_get_num_msix(struct bnxt *bp)
11567 {
11568 	if (!BNXT_NEW_RM(bp))
11569 		return bnxt_get_max_func_irqs(bp);
11570 
11571 	return bnxt_nq_rings_in_use(bp);
11572 }
11573 
11574 static int bnxt_init_int_mode(struct bnxt *bp)
11575 {
11576 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
11577 
11578 	total_vecs = bnxt_get_num_msix(bp);
11579 	max = bnxt_get_max_func_irqs(bp);
11580 	if (total_vecs > max)
11581 		total_vecs = max;
11582 
11583 	if (!total_vecs)
11584 		return 0;
11585 
11586 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11587 		min = 2;
11588 
11589 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11590 					   PCI_IRQ_MSIX);
11591 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11592 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11593 		rc = -ENODEV;
11594 		goto msix_setup_exit;
11595 	}
11596 
11597 	tbl_size = total_vecs;
11598 	if (pci_msix_can_alloc_dyn(bp->pdev))
11599 		tbl_size = max;
11600 	bp->irq_tbl = kzalloc_objs(*bp->irq_tbl, tbl_size);
11601 	if (bp->irq_tbl) {
11602 		for (i = 0; i < total_vecs; i++)
11603 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11604 
11605 		bp->total_irqs = total_vecs;
11606 		/* Trim rings based upon num of vectors allocated */
11607 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11608 				     total_vecs - ulp_msix, min == 1);
11609 		if (rc)
11610 			goto msix_setup_exit;
11611 
11612 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11613 		bp->cp_nr_rings = (min == 1) ?
11614 				  max_t(int, tx_cp, bp->rx_nr_rings) :
11615 				  tx_cp + bp->rx_nr_rings;
11616 
11617 	} else {
11618 		rc = -ENOMEM;
11619 		goto msix_setup_exit;
11620 	}
11621 	return 0;
11622 
11623 msix_setup_exit:
11624 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11625 	kfree(bp->irq_tbl);
11626 	bp->irq_tbl = NULL;
11627 	pci_free_irq_vectors(bp->pdev);
11628 	return rc;
11629 }
11630 
11631 static void bnxt_clear_int_mode(struct bnxt *bp)
11632 {
11633 	pci_free_irq_vectors(bp->pdev);
11634 
11635 	kfree(bp->irq_tbl);
11636 	bp->irq_tbl = NULL;
11637 }
11638 
11639 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11640 {
11641 	struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA];
11642 	bool irq_cleared = false;
11643 	bool irq_change = false;
11644 	int tcs = bp->num_tc;
11645 	int irqs_required;
11646 	int rc;
11647 
11648 	if (!bnxt_need_reserve_rings(bp))
11649 		return 0;
11650 
11651 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(edev)) {
11652 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11653 
11654 		if (ulp_msix > bp->ulp_num_msix_want)
11655 			ulp_msix = bp->ulp_num_msix_want;
11656 		irqs_required = ulp_msix + bp->cp_nr_rings;
11657 	} else {
11658 		irqs_required = bnxt_get_num_msix(bp);
11659 	}
11660 
11661 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11662 		irq_change = true;
11663 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11664 			bnxt_ulp_irq_stop(bp);
11665 			bnxt_clear_int_mode(bp);
11666 			irq_cleared = true;
11667 		}
11668 	}
11669 	rc = __bnxt_reserve_rings(bp);
11670 	if (irq_cleared) {
11671 		if (!rc)
11672 			rc = bnxt_init_int_mode(bp);
11673 		bnxt_ulp_irq_restart(bp, rc);
11674 	} else if (irq_change && !rc) {
11675 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11676 			rc = -ENOSPC;
11677 	}
11678 	if (rc) {
11679 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11680 		return rc;
11681 	}
11682 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11683 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11684 		netdev_err(bp->dev, "tx ring reservation failure\n");
11685 		netdev_reset_tc(bp->dev);
11686 		bp->num_tc = 0;
11687 		if (bp->tx_nr_rings_xdp)
11688 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11689 		else
11690 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11691 		return -ENOMEM;
11692 	}
11693 	return 0;
11694 }
11695 
11696 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx)
11697 {
11698 	struct bnxt_tx_ring_info *txr;
11699 	struct netdev_queue *txq;
11700 	struct bnxt_napi *bnapi;
11701 	int i;
11702 
11703 	bnapi = bp->bnapi[idx];
11704 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11705 		WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11706 		synchronize_net();
11707 
11708 		if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) {
11709 			txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11710 			if (txq) {
11711 				__netif_tx_lock_bh(txq);
11712 				netif_tx_stop_queue(txq);
11713 				__netif_tx_unlock_bh(txq);
11714 			}
11715 		}
11716 
11717 		if (!bp->tph_mode)
11718 			continue;
11719 
11720 		bnxt_hwrm_tx_ring_free(bp, txr, true);
11721 		bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr);
11722 		bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index);
11723 		bnxt_clear_one_cp_ring(bp, txr->tx_cpr);
11724 	}
11725 }
11726 
11727 static int bnxt_tx_queue_start(struct bnxt *bp, int idx)
11728 {
11729 	struct bnxt_tx_ring_info *txr;
11730 	struct netdev_queue *txq;
11731 	struct bnxt_napi *bnapi;
11732 	int rc, i;
11733 
11734 	bnapi = bp->bnapi[idx];
11735 	/* All rings have been reserved and previously allocated.
11736 	 * Reallocating with the same parameters should never fail.
11737 	 */
11738 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11739 		if (!bp->tph_mode)
11740 			goto start_tx;
11741 
11742 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
11743 		if (rc)
11744 			return rc;
11745 
11746 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false);
11747 		if (rc)
11748 			return rc;
11749 
11750 		txr->tx_prod = 0;
11751 		txr->tx_cons = 0;
11752 		txr->tx_hw_cons = 0;
11753 start_tx:
11754 		WRITE_ONCE(txr->dev_state, 0);
11755 		synchronize_net();
11756 
11757 		if (bnapi->flags & BNXT_NAPI_FLAG_XDP)
11758 			continue;
11759 
11760 		txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11761 		if (txq)
11762 			netif_tx_start_queue(txq);
11763 	}
11764 
11765 	return 0;
11766 }
11767 
11768 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify,
11769 				     const cpumask_t *mask)
11770 {
11771 	struct bnxt_irq *irq;
11772 	u16 tag;
11773 	int err;
11774 
11775 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11776 
11777 	if (!irq->bp->tph_mode)
11778 		return;
11779 
11780 	cpumask_copy(irq->cpu_mask, mask);
11781 
11782 	if (irq->ring_nr >= irq->bp->rx_nr_rings)
11783 		return;
11784 
11785 	if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11786 				cpumask_first(irq->cpu_mask), &tag))
11787 		return;
11788 
11789 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag))
11790 		return;
11791 
11792 	netdev_lock(irq->bp->dev);
11793 	if (netif_running(irq->bp->dev)) {
11794 		err = netdev_rx_queue_restart(irq->bp->dev, irq->ring_nr);
11795 		if (err)
11796 			netdev_err(irq->bp->dev,
11797 				   "RX queue restart failed: err=%d\n", err);
11798 	}
11799 	netdev_unlock(irq->bp->dev);
11800 }
11801 
11802 static void bnxt_irq_affinity_release(struct kref *ref)
11803 {
11804 	struct irq_affinity_notify *notify =
11805 		container_of(ref, struct irq_affinity_notify, kref);
11806 	struct bnxt_irq *irq;
11807 
11808 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11809 
11810 	if (!irq->bp->tph_mode)
11811 		return;
11812 
11813 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) {
11814 		netdev_err(irq->bp->dev,
11815 			   "Setting ST=0 for MSIX entry %d failed\n",
11816 			   irq->msix_nr);
11817 		return;
11818 	}
11819 }
11820 
11821 static void bnxt_release_irq_notifier(struct bnxt_irq *irq)
11822 {
11823 	irq_set_affinity_notifier(irq->vector, NULL);
11824 }
11825 
11826 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq)
11827 {
11828 	struct irq_affinity_notify *notify;
11829 
11830 	irq->bp = bp;
11831 
11832 	/* Nothing to do if TPH is not enabled */
11833 	if (!bp->tph_mode)
11834 		return;
11835 
11836 	/* Register IRQ affinity notifier */
11837 	notify = &irq->affinity_notify;
11838 	notify->irq = irq->vector;
11839 	notify->notify = bnxt_irq_affinity_notify;
11840 	notify->release = bnxt_irq_affinity_release;
11841 
11842 	irq_set_affinity_notifier(irq->vector, notify);
11843 }
11844 
11845 static void bnxt_free_irq(struct bnxt *bp)
11846 {
11847 	struct bnxt_irq *irq;
11848 	int i;
11849 
11850 #ifdef CONFIG_RFS_ACCEL
11851 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11852 	bp->dev->rx_cpu_rmap = NULL;
11853 #endif
11854 	if (!bp->irq_tbl || !bp->bnapi)
11855 		return;
11856 
11857 	for (i = 0; i < bp->cp_nr_rings; i++) {
11858 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11859 
11860 		irq = &bp->irq_tbl[map_idx];
11861 		if (irq->requested) {
11862 			if (irq->have_cpumask) {
11863 				irq_update_affinity_hint(irq->vector, NULL);
11864 				free_cpumask_var(irq->cpu_mask);
11865 				irq->have_cpumask = 0;
11866 			}
11867 
11868 			bnxt_release_irq_notifier(irq);
11869 
11870 			free_irq(irq->vector, bp->bnapi[i]);
11871 		}
11872 
11873 		irq->requested = 0;
11874 	}
11875 
11876 	/* Disable TPH support */
11877 	pcie_disable_tph(bp->pdev);
11878 	bp->tph_mode = 0;
11879 }
11880 
11881 static int bnxt_request_irq(struct bnxt *bp)
11882 {
11883 	struct cpu_rmap *rmap = NULL;
11884 	int i, j, rc = 0;
11885 	unsigned long flags = 0;
11886 
11887 	rc = bnxt_setup_int_mode(bp);
11888 	if (rc) {
11889 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11890 			   rc);
11891 		return rc;
11892 	}
11893 #ifdef CONFIG_RFS_ACCEL
11894 	rmap = bp->dev->rx_cpu_rmap;
11895 #endif
11896 
11897 	/* Enable TPH support as part of IRQ request */
11898 	rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE);
11899 	if (!rc)
11900 		bp->tph_mode = PCI_TPH_ST_IV_MODE;
11901 
11902 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11903 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11904 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11905 
11906 		if (IS_ENABLED(CONFIG_RFS_ACCEL) &&
11907 		    rmap && bp->bnapi[i]->rx_ring) {
11908 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11909 			if (rc)
11910 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11911 					    j);
11912 			j++;
11913 		}
11914 
11915 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11916 				 bp->bnapi[i]);
11917 		if (rc)
11918 			break;
11919 
11920 		netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector);
11921 		irq->requested = 1;
11922 
11923 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
11924 			int numa_node = dev_to_node(&bp->pdev->dev);
11925 			u16 tag;
11926 
11927 			irq->have_cpumask = 1;
11928 			irq->msix_nr = map_idx;
11929 			irq->ring_nr = i;
11930 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
11931 					irq->cpu_mask);
11932 			rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
11933 			if (rc) {
11934 				netdev_warn(bp->dev,
11935 					    "Update affinity hint failed, IRQ = %d\n",
11936 					    irq->vector);
11937 				break;
11938 			}
11939 
11940 			bnxt_register_irq_notifier(bp, irq);
11941 
11942 			/* Init ST table entry */
11943 			if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11944 						cpumask_first(irq->cpu_mask),
11945 						&tag))
11946 				continue;
11947 
11948 			pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag);
11949 		}
11950 	}
11951 	return rc;
11952 }
11953 
11954 static void bnxt_del_napi(struct bnxt *bp)
11955 {
11956 	int i;
11957 
11958 	if (!bp->bnapi)
11959 		return;
11960 
11961 	for (i = 0; i < bp->rx_nr_rings; i++)
11962 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
11963 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
11964 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
11965 
11966 	for (i = 0; i < bp->cp_nr_rings; i++) {
11967 		struct bnxt_napi *bnapi = bp->bnapi[i];
11968 
11969 		__netif_napi_del_locked(&bnapi->napi);
11970 	}
11971 	/* We called __netif_napi_del_locked(), we need
11972 	 * to respect an RCU grace period before freeing napi structures.
11973 	 */
11974 	synchronize_net();
11975 }
11976 
11977 static void bnxt_init_napi(struct bnxt *bp)
11978 {
11979 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
11980 	unsigned int cp_nr_rings = bp->cp_nr_rings;
11981 	struct bnxt_napi *bnapi;
11982 	int i;
11983 
11984 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11985 		poll_fn = bnxt_poll_p5;
11986 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11987 		cp_nr_rings--;
11988 
11989 	set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11990 
11991 	for (i = 0; i < cp_nr_rings; i++) {
11992 		bnapi = bp->bnapi[i];
11993 		netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn,
11994 					     bnapi->index);
11995 	}
11996 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11997 		bnapi = bp->bnapi[cp_nr_rings];
11998 		netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
11999 	}
12000 }
12001 
12002 static void bnxt_disable_napi(struct bnxt *bp)
12003 {
12004 	int i;
12005 
12006 	if (!bp->bnapi ||
12007 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
12008 		return;
12009 
12010 	for (i = 0; i < bp->cp_nr_rings; i++) {
12011 		struct bnxt_napi *bnapi = bp->bnapi[i];
12012 		struct bnxt_cp_ring_info *cpr;
12013 
12014 		cpr = &bnapi->cp_ring;
12015 		if (bnapi->tx_fault)
12016 			cpr->sw_stats->tx.tx_resets++;
12017 		if (bnapi->in_reset)
12018 			cpr->sw_stats->rx.rx_resets++;
12019 		napi_disable_locked(&bnapi->napi);
12020 	}
12021 }
12022 
12023 static void bnxt_enable_napi(struct bnxt *bp)
12024 {
12025 	int i;
12026 
12027 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
12028 	for (i = 0; i < bp->cp_nr_rings; i++) {
12029 		struct bnxt_napi *bnapi = bp->bnapi[i];
12030 		struct bnxt_cp_ring_info *cpr;
12031 
12032 		bnapi->tx_fault = 0;
12033 
12034 		cpr = &bnapi->cp_ring;
12035 		bnapi->in_reset = false;
12036 
12037 		if (bnapi->rx_ring) {
12038 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
12039 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
12040 		}
12041 		napi_enable_locked(&bnapi->napi);
12042 	}
12043 }
12044 
12045 void bnxt_tx_disable(struct bnxt *bp)
12046 {
12047 	int i;
12048 	struct bnxt_tx_ring_info *txr;
12049 
12050 	if (bp->tx_ring) {
12051 		for (i = 0; i < bp->tx_nr_rings; i++) {
12052 			txr = &bp->tx_ring[i];
12053 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
12054 		}
12055 	}
12056 	/* Make sure napi polls see @dev_state change */
12057 	synchronize_net();
12058 	/* Drop carrier first to prevent TX timeout */
12059 	netif_carrier_off(bp->dev);
12060 	/* Stop all TX queues */
12061 	netif_tx_disable(bp->dev);
12062 }
12063 
12064 void bnxt_tx_enable(struct bnxt *bp)
12065 {
12066 	int i;
12067 	struct bnxt_tx_ring_info *txr;
12068 
12069 	for (i = 0; i < bp->tx_nr_rings; i++) {
12070 		txr = &bp->tx_ring[i];
12071 		WRITE_ONCE(txr->dev_state, 0);
12072 	}
12073 	/* Make sure napi polls see @dev_state change */
12074 	synchronize_net();
12075 	netif_tx_wake_all_queues(bp->dev);
12076 	if (BNXT_LINK_IS_UP(bp))
12077 		netif_carrier_on(bp->dev);
12078 }
12079 
12080 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
12081 {
12082 	u8 active_fec = link_info->active_fec_sig_mode &
12083 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
12084 
12085 	switch (active_fec) {
12086 	default:
12087 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
12088 		return "None";
12089 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
12090 		return "Clause 74 BaseR";
12091 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
12092 		return "Clause 91 RS(528,514)";
12093 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
12094 		return "Clause 91 RS544_1XN";
12095 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
12096 		return "Clause 91 RS(544,514)";
12097 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
12098 		return "Clause 91 RS272_1XN";
12099 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
12100 		return "Clause 91 RS(272,257)";
12101 	}
12102 }
12103 
12104 static char *bnxt_link_down_reason(struct bnxt_link_info *link_info)
12105 {
12106 	u8 reason = link_info->link_down_reason;
12107 
12108 	/* Multiple bits can be set, we report 1 bit only in order of
12109 	 * priority.
12110 	 */
12111 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_RF)
12112 		return "(Remote fault)";
12113 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_OTP_SPEED_VIOLATION)
12114 		return "(OTP Speed limit violation)";
12115 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_CABLE_REMOVED)
12116 		return "(Cable removed)";
12117 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_MODULE_FAULT)
12118 		return "(Module fault)";
12119 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_BMC_REQUEST)
12120 		return "(BMC request down)";
12121 	return "";
12122 }
12123 
12124 void bnxt_report_link(struct bnxt *bp)
12125 {
12126 	if (BNXT_LINK_IS_UP(bp)) {
12127 		const char *signal = "";
12128 		const char *flow_ctrl;
12129 		const char *duplex;
12130 		u32 speed;
12131 		u16 fec;
12132 
12133 		netif_carrier_on(bp->dev);
12134 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
12135 		if (speed == SPEED_UNKNOWN) {
12136 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
12137 			return;
12138 		}
12139 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
12140 			duplex = "full";
12141 		else
12142 			duplex = "half";
12143 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
12144 			flow_ctrl = "ON - receive & transmit";
12145 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
12146 			flow_ctrl = "ON - transmit";
12147 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
12148 			flow_ctrl = "ON - receive";
12149 		else
12150 			flow_ctrl = "none";
12151 		if (bp->link_info.phy_qcfg_resp.option_flags &
12152 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
12153 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
12154 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
12155 			switch (sig_mode) {
12156 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
12157 				signal = "(NRZ) ";
12158 				break;
12159 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
12160 				signal = "(PAM4 56Gbps) ";
12161 				break;
12162 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
12163 				signal = "(PAM4 112Gbps) ";
12164 				break;
12165 			default:
12166 				break;
12167 			}
12168 		}
12169 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
12170 			    speed, signal, duplex, flow_ctrl);
12171 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
12172 			netdev_info(bp->dev, "EEE is %s\n",
12173 				    bp->eee.eee_active ? "active" :
12174 							 "not active");
12175 		fec = bp->link_info.fec_cfg;
12176 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
12177 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
12178 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
12179 				    bnxt_report_fec(&bp->link_info));
12180 	} else {
12181 		char *str = bnxt_link_down_reason(&bp->link_info);
12182 
12183 		netif_carrier_off(bp->dev);
12184 		netdev_err(bp->dev, "NIC Link is Down %s\n", str);
12185 	}
12186 }
12187 
12188 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
12189 {
12190 	if (!resp->supported_speeds_auto_mode &&
12191 	    !resp->supported_speeds_force_mode &&
12192 	    !resp->supported_pam4_speeds_auto_mode &&
12193 	    !resp->supported_pam4_speeds_force_mode &&
12194 	    !resp->supported_speeds2_auto_mode &&
12195 	    !resp->supported_speeds2_force_mode)
12196 		return true;
12197 	return false;
12198 }
12199 
12200 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
12201 {
12202 	struct bnxt_link_info *link_info = &bp->link_info;
12203 	struct hwrm_port_phy_qcaps_output *resp;
12204 	struct hwrm_port_phy_qcaps_input *req;
12205 	int rc = 0;
12206 
12207 	if (bp->hwrm_spec_code < 0x10201)
12208 		return 0;
12209 
12210 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
12211 	if (rc)
12212 		return rc;
12213 
12214 	resp = hwrm_req_hold(bp, req);
12215 	rc = hwrm_req_send(bp, req);
12216 	if (rc)
12217 		goto hwrm_phy_qcaps_exit;
12218 
12219 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
12220 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
12221 		struct ethtool_keee *eee = &bp->eee;
12222 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
12223 
12224 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
12225 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
12226 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
12227 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
12228 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
12229 	}
12230 
12231 	if (bp->hwrm_spec_code >= 0x10a01) {
12232 		if (bnxt_phy_qcaps_no_speed(resp)) {
12233 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
12234 			netdev_warn(bp->dev, "Ethernet link disabled\n");
12235 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
12236 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
12237 			netdev_info(bp->dev, "Ethernet link enabled\n");
12238 			/* Phy re-enabled, reprobe the speeds */
12239 			link_info->support_auto_speeds = 0;
12240 			link_info->support_pam4_auto_speeds = 0;
12241 			link_info->support_auto_speeds2 = 0;
12242 		}
12243 	}
12244 	if (resp->supported_speeds_auto_mode)
12245 		link_info->support_auto_speeds =
12246 			le16_to_cpu(resp->supported_speeds_auto_mode);
12247 	if (resp->supported_pam4_speeds_auto_mode)
12248 		link_info->support_pam4_auto_speeds =
12249 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
12250 	if (resp->supported_speeds2_auto_mode)
12251 		link_info->support_auto_speeds2 =
12252 			le16_to_cpu(resp->supported_speeds2_auto_mode);
12253 
12254 	bp->port_count = resp->port_cnt;
12255 
12256 hwrm_phy_qcaps_exit:
12257 	hwrm_req_drop(bp, req);
12258 	return rc;
12259 }
12260 
12261 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
12262 {
12263 	struct hwrm_port_mac_qcaps_output *resp;
12264 	struct hwrm_port_mac_qcaps_input *req;
12265 	int rc;
12266 
12267 	if (bp->hwrm_spec_code < 0x10a03)
12268 		return;
12269 
12270 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
12271 	if (rc)
12272 		return;
12273 
12274 	resp = hwrm_req_hold(bp, req);
12275 	rc = hwrm_req_send_silent(bp, req);
12276 	if (!rc)
12277 		bp->mac_flags = resp->flags;
12278 	hwrm_req_drop(bp, req);
12279 }
12280 
12281 static bool bnxt_support_dropped(u16 advertising, u16 supported)
12282 {
12283 	u16 diff = advertising ^ supported;
12284 
12285 	return ((supported | diff) != supported);
12286 }
12287 
12288 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
12289 {
12290 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
12291 
12292 	/* Check if any advertised speeds are no longer supported. The caller
12293 	 * holds the link_lock mutex, so we can modify link_info settings.
12294 	 */
12295 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12296 		if (bnxt_support_dropped(link_info->advertising,
12297 					 link_info->support_auto_speeds2)) {
12298 			link_info->advertising = link_info->support_auto_speeds2;
12299 			return true;
12300 		}
12301 		return false;
12302 	}
12303 	if (bnxt_support_dropped(link_info->advertising,
12304 				 link_info->support_auto_speeds)) {
12305 		link_info->advertising = link_info->support_auto_speeds;
12306 		return true;
12307 	}
12308 	if (bnxt_support_dropped(link_info->advertising_pam4,
12309 				 link_info->support_pam4_auto_speeds)) {
12310 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
12311 		return true;
12312 	}
12313 	return false;
12314 }
12315 
12316 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
12317 {
12318 	struct bnxt_link_info *link_info = &bp->link_info;
12319 	struct hwrm_port_phy_qcfg_output *resp;
12320 	struct hwrm_port_phy_qcfg_input *req;
12321 	u8 link_state = link_info->link_state;
12322 	bool support_changed;
12323 	int rc;
12324 
12325 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
12326 	if (rc)
12327 		return rc;
12328 
12329 	resp = hwrm_req_hold(bp, req);
12330 	rc = hwrm_req_send(bp, req);
12331 	if (rc) {
12332 		hwrm_req_drop(bp, req);
12333 		if (BNXT_VF(bp) && rc == -ENODEV) {
12334 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
12335 			rc = 0;
12336 		}
12337 		return rc;
12338 	}
12339 
12340 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
12341 	link_info->phy_link_status = resp->link;
12342 	link_info->duplex = resp->duplex_cfg;
12343 	if (bp->hwrm_spec_code >= 0x10800)
12344 		link_info->duplex = resp->duplex_state;
12345 	link_info->pause = resp->pause;
12346 	link_info->auto_mode = resp->auto_mode;
12347 	link_info->auto_pause_setting = resp->auto_pause;
12348 	link_info->lp_pause = resp->link_partner_adv_pause;
12349 	link_info->force_pause_setting = resp->force_pause;
12350 	link_info->duplex_setting = resp->duplex_cfg;
12351 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
12352 		link_info->link_speed = le16_to_cpu(resp->link_speed);
12353 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
12354 			link_info->active_lanes = resp->active_lanes;
12355 	} else {
12356 		link_info->link_speed = 0;
12357 		link_info->active_lanes = 0;
12358 	}
12359 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
12360 	link_info->force_pam4_link_speed =
12361 		le16_to_cpu(resp->force_pam4_link_speed);
12362 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
12363 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
12364 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
12365 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
12366 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
12367 	link_info->auto_pam4_link_speeds =
12368 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
12369 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
12370 	link_info->lp_auto_link_speeds =
12371 		le16_to_cpu(resp->link_partner_adv_speeds);
12372 	link_info->lp_auto_pam4_link_speeds =
12373 		resp->link_partner_pam4_adv_speeds;
12374 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
12375 	link_info->phy_ver[0] = resp->phy_maj;
12376 	link_info->phy_ver[1] = resp->phy_min;
12377 	link_info->phy_ver[2] = resp->phy_bld;
12378 	link_info->media_type = resp->media_type;
12379 	link_info->phy_type = resp->phy_type;
12380 	link_info->transceiver = resp->xcvr_pkg_type;
12381 	link_info->phy_addr = resp->eee_config_phy_addr &
12382 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
12383 	link_info->module_status = resp->module_status;
12384 	link_info->link_down_reason = resp->link_down_reason;
12385 
12386 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
12387 		struct ethtool_keee *eee = &bp->eee;
12388 		u16 fw_speeds;
12389 
12390 		eee->eee_active = 0;
12391 		if (resp->eee_config_phy_addr &
12392 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
12393 			eee->eee_active = 1;
12394 			fw_speeds = le16_to_cpu(
12395 				resp->link_partner_adv_eee_link_speed_mask);
12396 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
12397 		}
12398 
12399 		/* Pull initial EEE config */
12400 		if (!chng_link_state) {
12401 			if (resp->eee_config_phy_addr &
12402 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
12403 				eee->eee_enabled = 1;
12404 
12405 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
12406 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
12407 
12408 			if (resp->eee_config_phy_addr &
12409 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
12410 				__le32 tmr;
12411 
12412 				eee->tx_lpi_enabled = 1;
12413 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
12414 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
12415 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
12416 			}
12417 		}
12418 	}
12419 
12420 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
12421 	if (bp->hwrm_spec_code >= 0x10504) {
12422 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
12423 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
12424 	}
12425 	/* TODO: need to add more logic to report VF link */
12426 	if (chng_link_state) {
12427 		if (link_info->phy_link_status == BNXT_LINK_LINK)
12428 			link_info->link_state = BNXT_LINK_STATE_UP;
12429 		else
12430 			link_info->link_state = BNXT_LINK_STATE_DOWN;
12431 		if (link_state != link_info->link_state)
12432 			bnxt_report_link(bp);
12433 	} else {
12434 		/* always link down if not require to update link state */
12435 		link_info->link_state = BNXT_LINK_STATE_DOWN;
12436 	}
12437 	hwrm_req_drop(bp, req);
12438 
12439 	if (!BNXT_PHY_CFG_ABLE(bp))
12440 		return 0;
12441 
12442 	support_changed = bnxt_support_speed_dropped(link_info);
12443 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
12444 		bnxt_hwrm_set_link_setting(bp, true, false);
12445 	return 0;
12446 }
12447 
12448 static void bnxt_get_port_module_status(struct bnxt *bp)
12449 {
12450 	struct bnxt_link_info *link_info = &bp->link_info;
12451 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
12452 	u8 module_status;
12453 
12454 	if (bnxt_update_link(bp, true))
12455 		return;
12456 
12457 	module_status = link_info->module_status;
12458 	switch (module_status) {
12459 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
12460 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
12461 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
12462 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
12463 			    bp->pf.port_id);
12464 		if (bp->hwrm_spec_code >= 0x10201) {
12465 			netdev_warn(bp->dev, "Module part number %s\n",
12466 				    resp->phy_vendor_partnumber);
12467 		}
12468 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
12469 			netdev_warn(bp->dev, "TX is disabled\n");
12470 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
12471 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
12472 	}
12473 }
12474 
12475 static void
12476 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12477 {
12478 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
12479 		if (bp->hwrm_spec_code >= 0x10201)
12480 			req->auto_pause =
12481 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
12482 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12483 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
12484 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12485 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
12486 		req->enables |=
12487 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12488 	} else {
12489 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12490 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
12491 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12492 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
12493 		req->enables |=
12494 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
12495 		if (bp->hwrm_spec_code >= 0x10201) {
12496 			req->auto_pause = req->force_pause;
12497 			req->enables |= cpu_to_le32(
12498 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12499 		}
12500 	}
12501 }
12502 
12503 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12504 {
12505 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
12506 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
12507 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12508 			req->enables |=
12509 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
12510 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
12511 		} else if (bp->link_info.advertising) {
12512 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
12513 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
12514 		}
12515 		if (bp->link_info.advertising_pam4) {
12516 			req->enables |=
12517 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
12518 			req->auto_link_pam4_speed_mask =
12519 				cpu_to_le16(bp->link_info.advertising_pam4);
12520 		}
12521 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
12522 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
12523 	} else {
12524 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
12525 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12526 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
12527 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
12528 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
12529 				   (u32)bp->link_info.req_link_speed);
12530 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
12531 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12532 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
12533 		} else {
12534 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12535 		}
12536 	}
12537 
12538 	/* tell chimp that the setting takes effect immediately */
12539 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
12540 }
12541 
12542 int bnxt_hwrm_set_pause(struct bnxt *bp)
12543 {
12544 	struct hwrm_port_phy_cfg_input *req;
12545 	int rc;
12546 
12547 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12548 	if (rc)
12549 		return rc;
12550 
12551 	bnxt_hwrm_set_pause_common(bp, req);
12552 
12553 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
12554 	    bp->link_info.force_link_chng)
12555 		bnxt_hwrm_set_link_common(bp, req);
12556 
12557 	rc = hwrm_req_send(bp, req);
12558 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
12559 		/* since changing of pause setting doesn't trigger any link
12560 		 * change event, the driver needs to update the current pause
12561 		 * result upon successfully return of the phy_cfg command
12562 		 */
12563 		bp->link_info.pause =
12564 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
12565 		bp->link_info.auto_pause_setting = 0;
12566 		if (!bp->link_info.force_link_chng)
12567 			bnxt_report_link(bp);
12568 	}
12569 	bp->link_info.force_link_chng = false;
12570 	return rc;
12571 }
12572 
12573 static void bnxt_hwrm_set_eee(struct bnxt *bp,
12574 			      struct hwrm_port_phy_cfg_input *req)
12575 {
12576 	struct ethtool_keee *eee = &bp->eee;
12577 
12578 	if (eee->eee_enabled) {
12579 		u16 eee_speeds;
12580 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
12581 
12582 		if (eee->tx_lpi_enabled)
12583 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
12584 		else
12585 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
12586 
12587 		req->flags |= cpu_to_le32(flags);
12588 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
12589 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
12590 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
12591 	} else {
12592 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
12593 	}
12594 }
12595 
12596 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
12597 {
12598 	struct hwrm_port_phy_cfg_input *req;
12599 	int rc;
12600 
12601 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12602 	if (rc)
12603 		return rc;
12604 
12605 	if (set_pause)
12606 		bnxt_hwrm_set_pause_common(bp, req);
12607 
12608 	bnxt_hwrm_set_link_common(bp, req);
12609 
12610 	if (set_eee)
12611 		bnxt_hwrm_set_eee(bp, req);
12612 	return hwrm_req_send(bp, req);
12613 }
12614 
12615 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
12616 {
12617 	struct hwrm_port_phy_cfg_input *req;
12618 	int rc;
12619 
12620 	if (!BNXT_SINGLE_PF(bp))
12621 		return 0;
12622 
12623 	if (pci_num_vf(bp->pdev) &&
12624 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
12625 		return 0;
12626 
12627 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12628 	if (rc)
12629 		return rc;
12630 
12631 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
12632 	rc = hwrm_req_send(bp, req);
12633 	if (!rc) {
12634 		mutex_lock(&bp->link_lock);
12635 		/* Device is not obliged link down in certain scenarios, even
12636 		 * when forced. Setting the state unknown is consistent with
12637 		 * driver startup and will force link state to be reported
12638 		 * during subsequent open based on PORT_PHY_QCFG.
12639 		 */
12640 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
12641 		mutex_unlock(&bp->link_lock);
12642 	}
12643 	return rc;
12644 }
12645 
12646 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
12647 {
12648 #ifdef CONFIG_TEE_BNXT_FW
12649 	int rc = tee_bnxt_fw_load();
12650 
12651 	if (rc)
12652 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
12653 
12654 	return rc;
12655 #else
12656 	netdev_err(bp->dev, "OP-TEE not supported\n");
12657 	return -ENODEV;
12658 #endif
12659 }
12660 
12661 static int bnxt_try_recover_fw(struct bnxt *bp)
12662 {
12663 	if (bp->fw_health && bp->fw_health->status_reliable) {
12664 		int retry = 0, rc;
12665 		u32 sts;
12666 
12667 		do {
12668 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12669 			rc = bnxt_hwrm_poll(bp);
12670 			if (!BNXT_FW_IS_BOOTING(sts) &&
12671 			    !BNXT_FW_IS_RECOVERING(sts))
12672 				break;
12673 			retry++;
12674 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
12675 
12676 		if (!BNXT_FW_IS_HEALTHY(sts)) {
12677 			netdev_err(bp->dev,
12678 				   "Firmware not responding, status: 0x%x\n",
12679 				   sts);
12680 			rc = -ENODEV;
12681 		}
12682 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
12683 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
12684 			return bnxt_fw_reset_via_optee(bp);
12685 		}
12686 		return rc;
12687 	}
12688 
12689 	return -ENODEV;
12690 }
12691 
12692 void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
12693 {
12694 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12695 
12696 	if (!BNXT_NEW_RM(bp))
12697 		return; /* no resource reservations required */
12698 
12699 	hw_resc->resv_cp_rings = 0;
12700 	hw_resc->resv_stat_ctxs = 0;
12701 	hw_resc->resv_irqs = 0;
12702 	hw_resc->resv_tx_rings = 0;
12703 	hw_resc->resv_rx_rings = 0;
12704 	hw_resc->resv_hw_ring_grps = 0;
12705 	hw_resc->resv_vnics = 0;
12706 	hw_resc->resv_rsscos_ctxs = 0;
12707 	if (!fw_reset) {
12708 		bp->tx_nr_rings = 0;
12709 		bp->rx_nr_rings = 0;
12710 	}
12711 }
12712 
12713 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12714 {
12715 	int rc;
12716 
12717 	if (!BNXT_NEW_RM(bp))
12718 		return 0; /* no resource reservations required */
12719 
12720 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12721 	if (rc)
12722 		netdev_err(bp->dev, "resc_qcaps failed\n");
12723 
12724 	bnxt_clear_reservations(bp, fw_reset);
12725 
12726 	return rc;
12727 }
12728 
12729 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12730 {
12731 	struct hwrm_func_drv_if_change_output *resp;
12732 	struct hwrm_func_drv_if_change_input *req;
12733 	bool resc_reinit = false;
12734 	bool caps_change = false;
12735 	int rc, retry = 0;
12736 	bool fw_reset;
12737 	u32 flags = 0;
12738 
12739 	fw_reset = (bp->fw_reset_state == BNXT_FW_RESET_STATE_ABORT);
12740 	bp->fw_reset_state = 0;
12741 
12742 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12743 		return 0;
12744 
12745 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12746 	if (rc)
12747 		return rc;
12748 
12749 	if (up)
12750 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12751 	resp = hwrm_req_hold(bp, req);
12752 
12753 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12754 	while (retry < BNXT_FW_IF_RETRY) {
12755 		rc = hwrm_req_send(bp, req);
12756 		if (rc != -EAGAIN)
12757 			break;
12758 
12759 		msleep(50);
12760 		retry++;
12761 	}
12762 
12763 	if (rc == -EAGAIN) {
12764 		hwrm_req_drop(bp, req);
12765 		return rc;
12766 	} else if (!rc) {
12767 		flags = le32_to_cpu(resp->flags);
12768 	} else if (up) {
12769 		rc = bnxt_try_recover_fw(bp);
12770 		fw_reset = true;
12771 	}
12772 	hwrm_req_drop(bp, req);
12773 	if (rc)
12774 		return rc;
12775 
12776 	if (!up) {
12777 		bnxt_inv_fw_health_reg(bp);
12778 		return 0;
12779 	}
12780 
12781 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12782 		resc_reinit = true;
12783 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12784 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12785 		fw_reset = true;
12786 	else
12787 		bnxt_remap_fw_health_regs(bp);
12788 
12789 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12790 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12791 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12792 		return -ENODEV;
12793 	}
12794 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE)
12795 		caps_change = true;
12796 
12797 	if (resc_reinit || fw_reset || caps_change) {
12798 		if (fw_reset || caps_change) {
12799 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12800 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12801 				bnxt_ulp_irq_stop(bp);
12802 			bnxt_free_ctx_mem(bp, false);
12803 			bnxt_dcb_free(bp);
12804 			rc = bnxt_fw_init_one(bp);
12805 			if (rc) {
12806 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12807 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12808 				return rc;
12809 			}
12810 			/* IRQ will be initialized later in bnxt_request_irq()*/
12811 			bnxt_clear_int_mode(bp);
12812 		}
12813 		rc = bnxt_cancel_reservations(bp, fw_reset);
12814 	}
12815 	return rc;
12816 }
12817 
12818 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12819 {
12820 	struct hwrm_port_led_qcaps_output *resp;
12821 	struct hwrm_port_led_qcaps_input *req;
12822 	struct bnxt_pf_info *pf = &bp->pf;
12823 	int rc;
12824 
12825 	bp->num_leds = 0;
12826 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12827 		return 0;
12828 
12829 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12830 	if (rc)
12831 		return rc;
12832 
12833 	req->port_id = cpu_to_le16(pf->port_id);
12834 	resp = hwrm_req_hold(bp, req);
12835 	rc = hwrm_req_send(bp, req);
12836 	if (rc) {
12837 		hwrm_req_drop(bp, req);
12838 		return rc;
12839 	}
12840 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12841 		int i;
12842 
12843 		bp->num_leds = resp->num_leds;
12844 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12845 						 bp->num_leds);
12846 		for (i = 0; i < bp->num_leds; i++) {
12847 			struct bnxt_led_info *led = &bp->leds[i];
12848 			__le16 caps = led->led_state_caps;
12849 
12850 			if (!led->led_group_id ||
12851 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12852 				bp->num_leds = 0;
12853 				break;
12854 			}
12855 		}
12856 	}
12857 	hwrm_req_drop(bp, req);
12858 	return 0;
12859 }
12860 
12861 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12862 {
12863 	struct hwrm_wol_filter_alloc_output *resp;
12864 	struct hwrm_wol_filter_alloc_input *req;
12865 	int rc;
12866 
12867 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12868 	if (rc)
12869 		return rc;
12870 
12871 	req->port_id = cpu_to_le16(bp->pf.port_id);
12872 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12873 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12874 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12875 
12876 	resp = hwrm_req_hold(bp, req);
12877 	rc = hwrm_req_send(bp, req);
12878 	if (!rc)
12879 		bp->wol_filter_id = resp->wol_filter_id;
12880 	hwrm_req_drop(bp, req);
12881 	return rc;
12882 }
12883 
12884 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12885 {
12886 	struct hwrm_wol_filter_free_input *req;
12887 	int rc;
12888 
12889 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12890 	if (rc)
12891 		return rc;
12892 
12893 	req->port_id = cpu_to_le16(bp->pf.port_id);
12894 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12895 	req->wol_filter_id = bp->wol_filter_id;
12896 
12897 	return hwrm_req_send(bp, req);
12898 }
12899 
12900 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12901 {
12902 	struct hwrm_wol_filter_qcfg_output *resp;
12903 	struct hwrm_wol_filter_qcfg_input *req;
12904 	u16 next_handle = 0;
12905 	int rc;
12906 
12907 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12908 	if (rc)
12909 		return rc;
12910 
12911 	req->port_id = cpu_to_le16(bp->pf.port_id);
12912 	req->handle = cpu_to_le16(handle);
12913 	resp = hwrm_req_hold(bp, req);
12914 	rc = hwrm_req_send(bp, req);
12915 	if (!rc) {
12916 		next_handle = le16_to_cpu(resp->next_handle);
12917 		if (next_handle != 0) {
12918 			if (resp->wol_type ==
12919 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12920 				bp->wol = 1;
12921 				bp->wol_filter_id = resp->wol_filter_id;
12922 			}
12923 		}
12924 	}
12925 	hwrm_req_drop(bp, req);
12926 	return next_handle;
12927 }
12928 
12929 static void bnxt_get_wol_settings(struct bnxt *bp)
12930 {
12931 	u16 handle = 0;
12932 
12933 	bp->wol = 0;
12934 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
12935 		return;
12936 
12937 	do {
12938 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
12939 	} while (handle && handle != 0xffff);
12940 }
12941 
12942 static bool bnxt_eee_config_ok(struct bnxt *bp)
12943 {
12944 	struct ethtool_keee *eee = &bp->eee;
12945 	struct bnxt_link_info *link_info = &bp->link_info;
12946 
12947 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
12948 		return true;
12949 
12950 	if (eee->eee_enabled) {
12951 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
12952 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
12953 
12954 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
12955 
12956 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12957 			eee->eee_enabled = 0;
12958 			return false;
12959 		}
12960 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
12961 			linkmode_and(eee->advertised, advertising,
12962 				     eee->supported);
12963 			return false;
12964 		}
12965 	}
12966 	return true;
12967 }
12968 
12969 static int bnxt_update_phy_setting(struct bnxt *bp)
12970 {
12971 	int rc;
12972 	bool update_link = false;
12973 	bool update_pause = false;
12974 	bool update_eee = false;
12975 	struct bnxt_link_info *link_info = &bp->link_info;
12976 
12977 	rc = bnxt_update_link(bp, true);
12978 	if (rc) {
12979 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
12980 			   rc);
12981 		return rc;
12982 	}
12983 	if (!BNXT_SINGLE_PF(bp))
12984 		return 0;
12985 
12986 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12987 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
12988 	    link_info->req_flow_ctrl)
12989 		update_pause = true;
12990 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12991 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
12992 		update_pause = true;
12993 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12994 		if (BNXT_AUTO_MODE(link_info->auto_mode))
12995 			update_link = true;
12996 		if (bnxt_force_speed_updated(link_info))
12997 			update_link = true;
12998 		if (link_info->req_duplex != link_info->duplex_setting)
12999 			update_link = true;
13000 	} else {
13001 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
13002 			update_link = true;
13003 		if (bnxt_auto_speed_updated(link_info))
13004 			update_link = true;
13005 	}
13006 
13007 	/* The last close may have shutdown the link, so need to call
13008 	 * PHY_CFG to bring it back up.
13009 	 */
13010 	if (!BNXT_LINK_IS_UP(bp))
13011 		update_link = true;
13012 
13013 	if (!bnxt_eee_config_ok(bp))
13014 		update_eee = true;
13015 
13016 	if (update_link)
13017 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
13018 	else if (update_pause)
13019 		rc = bnxt_hwrm_set_pause(bp);
13020 	if (rc) {
13021 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
13022 			   rc);
13023 		return rc;
13024 	}
13025 
13026 	return rc;
13027 }
13028 
13029 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
13030 
13031 static int bnxt_reinit_after_abort(struct bnxt *bp)
13032 {
13033 	int rc;
13034 
13035 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13036 		return -EBUSY;
13037 
13038 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
13039 		return -ENODEV;
13040 
13041 	rc = bnxt_fw_init_one(bp);
13042 	if (!rc) {
13043 		bnxt_clear_int_mode(bp);
13044 		rc = bnxt_init_int_mode(bp);
13045 		if (!rc) {
13046 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13047 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
13048 		}
13049 	}
13050 	return rc;
13051 }
13052 
13053 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
13054 {
13055 	struct bnxt_ntuple_filter *ntp_fltr;
13056 	struct bnxt_l2_filter *l2_fltr;
13057 
13058 	if (list_empty(&fltr->list))
13059 		return;
13060 
13061 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
13062 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
13063 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
13064 		atomic_inc(&l2_fltr->refcnt);
13065 		ntp_fltr->l2_fltr = l2_fltr;
13066 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
13067 			bnxt_del_ntp_filter(bp, ntp_fltr);
13068 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
13069 				   fltr->sw_id);
13070 		}
13071 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
13072 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
13073 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
13074 			bnxt_del_l2_filter(bp, l2_fltr);
13075 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
13076 				   fltr->sw_id);
13077 		}
13078 	}
13079 }
13080 
13081 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
13082 {
13083 	struct bnxt_filter_base *usr_fltr, *tmp;
13084 
13085 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
13086 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
13087 }
13088 
13089 static int bnxt_set_xps_mapping(struct bnxt *bp)
13090 {
13091 	int numa_node = dev_to_node(&bp->pdev->dev);
13092 	unsigned int q_idx, map_idx, cpu, i;
13093 	const struct cpumask *cpu_mask_ptr;
13094 	int nr_cpus = num_online_cpus();
13095 	cpumask_t *q_map;
13096 	int rc = 0;
13097 
13098 	q_map = kzalloc_objs(*q_map, bp->tx_nr_rings_per_tc);
13099 	if (!q_map)
13100 		return -ENOMEM;
13101 
13102 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
13103 	 * Each TC has the same number of TX queues. The nth TX queue for each
13104 	 * TC will have the same CPU mask.
13105 	 */
13106 	for (i = 0; i < nr_cpus; i++) {
13107 		map_idx = i % bp->tx_nr_rings_per_tc;
13108 		cpu = cpumask_local_spread(i, numa_node);
13109 		cpu_mask_ptr = get_cpu_mask(cpu);
13110 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
13111 	}
13112 
13113 	/* Register CPU mask for each TX queue except the ones marked for XDP */
13114 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
13115 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
13116 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
13117 		if (rc) {
13118 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
13119 				    q_idx);
13120 			break;
13121 		}
13122 	}
13123 
13124 	kfree(q_map);
13125 
13126 	return rc;
13127 }
13128 
13129 static int bnxt_tx_nr_rings(struct bnxt *bp)
13130 {
13131 	return bp->num_tc ? bp->tx_nr_rings_per_tc * bp->num_tc :
13132 			    bp->tx_nr_rings_per_tc;
13133 }
13134 
13135 static int bnxt_tx_nr_rings_per_tc(struct bnxt *bp)
13136 {
13137 	return bp->num_tc ? bp->tx_nr_rings / bp->num_tc : bp->tx_nr_rings;
13138 }
13139 
13140 static void bnxt_set_xdp_tx_rings(struct bnxt *bp)
13141 {
13142 	bp->tx_nr_rings_xdp = bp->tx_nr_rings_per_tc;
13143 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
13144 }
13145 
13146 static void bnxt_adj_tx_rings(struct bnxt *bp)
13147 {
13148 	/* Make adjustments if reserved TX rings are less than requested */
13149 	bp->tx_nr_rings -= bp->tx_nr_rings_xdp;
13150 	bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp);
13151 	if (bp->tx_nr_rings_xdp)
13152 		bnxt_set_xdp_tx_rings(bp);
13153 }
13154 
13155 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13156 {
13157 	int rc = 0;
13158 
13159 	netif_carrier_off(bp->dev);
13160 	if (irq_re_init) {
13161 		/* Reserve rings now if none were reserved at driver probe. */
13162 		rc = bnxt_init_dflt_ring_mode(bp);
13163 		if (rc) {
13164 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
13165 			return rc;
13166 		}
13167 	}
13168 	rc = bnxt_reserve_rings(bp, irq_re_init);
13169 	if (rc)
13170 		return rc;
13171 
13172 	bnxt_adj_tx_rings(bp);
13173 	rc = bnxt_alloc_mem(bp, irq_re_init);
13174 	if (rc) {
13175 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
13176 		goto open_err_free_mem;
13177 	}
13178 
13179 	if (irq_re_init) {
13180 		bnxt_init_napi(bp);
13181 		rc = bnxt_request_irq(bp);
13182 		if (rc) {
13183 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
13184 			goto open_err_irq;
13185 		}
13186 	}
13187 
13188 	rc = bnxt_init_nic(bp, irq_re_init);
13189 	if (rc) {
13190 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
13191 		goto open_err_irq;
13192 	}
13193 
13194 	bnxt_enable_napi(bp);
13195 	bnxt_debug_dev_init(bp);
13196 
13197 	if (link_re_init) {
13198 		mutex_lock(&bp->link_lock);
13199 		rc = bnxt_update_phy_setting(bp);
13200 		mutex_unlock(&bp->link_lock);
13201 		if (rc) {
13202 			netdev_warn(bp->dev, "failed to update phy settings\n");
13203 			if (BNXT_SINGLE_PF(bp)) {
13204 				bp->link_info.phy_retry = true;
13205 				bp->link_info.phy_retry_expires =
13206 					jiffies + 5 * HZ;
13207 			}
13208 		}
13209 	}
13210 
13211 	if (irq_re_init) {
13212 		udp_tunnel_nic_reset_ntf(bp->dev);
13213 		rc = bnxt_set_xps_mapping(bp);
13214 		if (rc)
13215 			netdev_warn(bp->dev, "failed to set xps mapping\n");
13216 	}
13217 
13218 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
13219 		if (!static_key_enabled(&bnxt_xdp_locking_key))
13220 			static_branch_enable(&bnxt_xdp_locking_key);
13221 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
13222 		static_branch_disable(&bnxt_xdp_locking_key);
13223 	}
13224 	set_bit(BNXT_STATE_OPEN, &bp->state);
13225 	bnxt_enable_int(bp);
13226 	/* Enable TX queues */
13227 	bnxt_tx_enable(bp);
13228 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13229 	/* Poll link status and check for SFP+ module status */
13230 	mutex_lock(&bp->link_lock);
13231 	bnxt_get_port_module_status(bp);
13232 	mutex_unlock(&bp->link_lock);
13233 
13234 	/* VF-reps may need to be re-opened after the PF is re-opened */
13235 	if (BNXT_PF(bp))
13236 		bnxt_vf_reps_open(bp);
13237 	bnxt_ptp_init_rtc(bp, true);
13238 	bnxt_ptp_cfg_tstamp_filters(bp);
13239 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13240 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
13241 	bnxt_cfg_usr_fltrs(bp);
13242 	return 0;
13243 
13244 open_err_irq:
13245 	bnxt_del_napi(bp);
13246 
13247 open_err_free_mem:
13248 	bnxt_free_skbs(bp);
13249 	bnxt_free_irq(bp);
13250 	bnxt_free_mem(bp, true);
13251 	return rc;
13252 }
13253 
13254 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13255 {
13256 	int rc = 0;
13257 
13258 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
13259 		rc = -EIO;
13260 	if (!rc)
13261 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
13262 	if (rc) {
13263 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
13264 		netif_close(bp->dev);
13265 	}
13266 	return rc;
13267 }
13268 
13269 /* netdev instance lock held, open the NIC half way by allocating all
13270  * resources, but NAPI, IRQ, and TX are not enabled.  This is mainly used
13271  * for offline self tests.
13272  */
13273 int bnxt_half_open_nic(struct bnxt *bp)
13274 {
13275 	int rc = 0;
13276 
13277 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13278 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
13279 		rc = -ENODEV;
13280 		goto half_open_err;
13281 	}
13282 
13283 	rc = bnxt_alloc_mem(bp, true);
13284 	if (rc) {
13285 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
13286 		goto half_open_err;
13287 	}
13288 	bnxt_init_napi(bp);
13289 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13290 	rc = bnxt_init_nic(bp, true);
13291 	if (rc) {
13292 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13293 		bnxt_del_napi(bp);
13294 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
13295 		goto half_open_err;
13296 	}
13297 	return 0;
13298 
13299 half_open_err:
13300 	bnxt_free_skbs(bp);
13301 	bnxt_free_mem(bp, true);
13302 	netif_close(bp->dev);
13303 	return rc;
13304 }
13305 
13306 /* netdev instance lock held, this call can only be made after a previous
13307  * successful call to bnxt_half_open_nic().
13308  */
13309 void bnxt_half_close_nic(struct bnxt *bp)
13310 {
13311 	bnxt_hwrm_resource_free(bp, false, true);
13312 	bnxt_del_napi(bp);
13313 	bnxt_free_skbs(bp);
13314 	bnxt_free_mem(bp, true);
13315 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13316 }
13317 
13318 void bnxt_reenable_sriov(struct bnxt *bp)
13319 {
13320 	if (BNXT_PF(bp)) {
13321 		struct bnxt_pf_info *pf = &bp->pf;
13322 		int n = pf->active_vfs;
13323 
13324 		if (n)
13325 			bnxt_cfg_hw_sriov(bp, &n, true);
13326 	}
13327 }
13328 
13329 static int bnxt_open(struct net_device *dev)
13330 {
13331 	struct bnxt *bp = netdev_priv(dev);
13332 	int rc;
13333 
13334 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13335 		rc = bnxt_reinit_after_abort(bp);
13336 		if (rc) {
13337 			if (rc == -EBUSY)
13338 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
13339 			else
13340 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
13341 			return -ENODEV;
13342 		}
13343 	}
13344 
13345 	rc = bnxt_hwrm_if_change(bp, true);
13346 	if (rc)
13347 		return rc;
13348 
13349 	rc = __bnxt_open_nic(bp, true, true);
13350 	if (rc) {
13351 		bnxt_hwrm_if_change(bp, false);
13352 	} else {
13353 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
13354 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13355 				bnxt_queue_sp_work(bp,
13356 						   BNXT_RESTART_ULP_SP_EVENT);
13357 		}
13358 	}
13359 
13360 	return rc;
13361 }
13362 
13363 static bool bnxt_drv_busy(struct bnxt *bp)
13364 {
13365 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
13366 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
13367 }
13368 
13369 static void bnxt_get_ring_stats(struct bnxt *bp,
13370 				struct rtnl_link_stats64 *stats);
13371 
13372 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
13373 			     bool link_re_init)
13374 {
13375 	/* Close the VF-reps before closing PF */
13376 	if (BNXT_PF(bp))
13377 		bnxt_vf_reps_close(bp);
13378 
13379 	/* Change device state to avoid TX queue wake up's */
13380 	bnxt_tx_disable(bp);
13381 
13382 	clear_bit(BNXT_STATE_OPEN, &bp->state);
13383 	smp_mb__after_atomic();
13384 	while (bnxt_drv_busy(bp))
13385 		msleep(20);
13386 
13387 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13388 		bnxt_clear_rss_ctxs(bp);
13389 	/* Flush rings and disable interrupts */
13390 	bnxt_shutdown_nic(bp, irq_re_init);
13391 
13392 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
13393 
13394 	bnxt_debug_dev_exit(bp);
13395 	bnxt_disable_napi(bp);
13396 	timer_delete_sync(&bp->timer);
13397 	bnxt_free_skbs(bp);
13398 
13399 	/* Save ring stats before shutdown */
13400 	if (bp->bnapi && irq_re_init) {
13401 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
13402 		bnxt_get_ring_drv_stats(bp, &bp->ring_drv_stats_prev);
13403 	}
13404 	if (irq_re_init) {
13405 		bnxt_free_irq(bp);
13406 		bnxt_del_napi(bp);
13407 	}
13408 	bnxt_free_mem(bp, irq_re_init);
13409 }
13410 
13411 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13412 {
13413 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13414 		/* If we get here, it means firmware reset is in progress
13415 		 * while we are trying to close.  We can safely proceed with
13416 		 * the close because we are holding netdev instance lock.
13417 		 * Some firmware messages may fail as we proceed to close.
13418 		 * We set the ABORT_ERR flag here so that the FW reset thread
13419 		 * will later abort when it gets the netdev instance lock
13420 		 * and sees the flag.
13421 		 */
13422 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
13423 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13424 	}
13425 
13426 #ifdef CONFIG_BNXT_SRIOV
13427 	if (bp->sriov_cfg) {
13428 		int rc;
13429 
13430 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
13431 						      !bp->sriov_cfg,
13432 						      BNXT_SRIOV_CFG_WAIT_TMO);
13433 		if (!rc)
13434 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
13435 		else if (rc < 0)
13436 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
13437 	}
13438 #endif
13439 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
13440 }
13441 
13442 static int bnxt_close(struct net_device *dev)
13443 {
13444 	struct bnxt *bp = netdev_priv(dev);
13445 
13446 	bnxt_close_nic(bp, true, true);
13447 	bnxt_hwrm_shutdown_link(bp);
13448 	bnxt_hwrm_if_change(bp, false);
13449 	return 0;
13450 }
13451 
13452 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
13453 				   u16 *val)
13454 {
13455 	struct hwrm_port_phy_mdio_read_output *resp;
13456 	struct hwrm_port_phy_mdio_read_input *req;
13457 	int rc;
13458 
13459 	if (bp->hwrm_spec_code < 0x10a00)
13460 		return -EOPNOTSUPP;
13461 
13462 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
13463 	if (rc)
13464 		return rc;
13465 
13466 	req->port_id = cpu_to_le16(bp->pf.port_id);
13467 	req->phy_addr = phy_addr;
13468 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13469 	if (mdio_phy_id_is_c45(phy_addr)) {
13470 		req->cl45_mdio = 1;
13471 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13472 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13473 		req->reg_addr = cpu_to_le16(reg);
13474 	}
13475 
13476 	resp = hwrm_req_hold(bp, req);
13477 	rc = hwrm_req_send(bp, req);
13478 	if (!rc)
13479 		*val = le16_to_cpu(resp->reg_data);
13480 	hwrm_req_drop(bp, req);
13481 	return rc;
13482 }
13483 
13484 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
13485 				    u16 val)
13486 {
13487 	struct hwrm_port_phy_mdio_write_input *req;
13488 	int rc;
13489 
13490 	if (bp->hwrm_spec_code < 0x10a00)
13491 		return -EOPNOTSUPP;
13492 
13493 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
13494 	if (rc)
13495 		return rc;
13496 
13497 	req->port_id = cpu_to_le16(bp->pf.port_id);
13498 	req->phy_addr = phy_addr;
13499 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13500 	if (mdio_phy_id_is_c45(phy_addr)) {
13501 		req->cl45_mdio = 1;
13502 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13503 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13504 		req->reg_addr = cpu_to_le16(reg);
13505 	}
13506 	req->reg_data = cpu_to_le16(val);
13507 
13508 	return hwrm_req_send(bp, req);
13509 }
13510 
13511 /* netdev instance lock held */
13512 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13513 {
13514 	struct mii_ioctl_data *mdio = if_mii(ifr);
13515 	struct bnxt *bp = netdev_priv(dev);
13516 	int rc;
13517 
13518 	switch (cmd) {
13519 	case SIOCGMIIPHY:
13520 		mdio->phy_id = bp->link_info.phy_addr;
13521 
13522 		fallthrough;
13523 	case SIOCGMIIREG: {
13524 		u16 mii_regval = 0;
13525 
13526 		if (!netif_running(dev))
13527 			return -EAGAIN;
13528 
13529 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
13530 					     &mii_regval);
13531 		mdio->val_out = mii_regval;
13532 		return rc;
13533 	}
13534 
13535 	case SIOCSMIIREG:
13536 		if (!netif_running(dev))
13537 			return -EAGAIN;
13538 
13539 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
13540 						mdio->val_in);
13541 
13542 	default:
13543 		/* do nothing */
13544 		break;
13545 	}
13546 	return -EOPNOTSUPP;
13547 }
13548 
13549 static void bnxt_get_ring_stats(struct bnxt *bp,
13550 				struct rtnl_link_stats64 *stats)
13551 {
13552 	int i;
13553 
13554 	for (i = 0; i < bp->cp_nr_rings; i++) {
13555 		struct bnxt_napi *bnapi = bp->bnapi[i];
13556 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13557 		u64 *sw = cpr->stats.sw_stats;
13558 
13559 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
13560 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13561 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
13562 
13563 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
13564 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
13565 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
13566 
13567 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
13568 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
13569 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
13570 
13571 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
13572 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
13573 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
13574 
13575 		stats->rx_missed_errors +=
13576 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
13577 
13578 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13579 
13580 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
13581 
13582 		stats->rx_dropped +=
13583 			cpr->sw_stats->rx.rx_netpoll_discards +
13584 			cpr->sw_stats->rx.rx_oom_discards;
13585 	}
13586 }
13587 
13588 static void bnxt_add_prev_stats(struct bnxt *bp,
13589 				struct rtnl_link_stats64 *stats)
13590 {
13591 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
13592 
13593 	stats->rx_packets += prev_stats->rx_packets;
13594 	stats->tx_packets += prev_stats->tx_packets;
13595 	stats->rx_bytes += prev_stats->rx_bytes;
13596 	stats->tx_bytes += prev_stats->tx_bytes;
13597 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
13598 	stats->multicast += prev_stats->multicast;
13599 	stats->rx_dropped += prev_stats->rx_dropped;
13600 	stats->tx_dropped += prev_stats->tx_dropped;
13601 }
13602 
13603 static void
13604 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
13605 {
13606 	struct bnxt *bp = netdev_priv(dev);
13607 
13608 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
13609 	/* Make sure bnxt_close_nic() sees that we are reading stats before
13610 	 * we check the BNXT_STATE_OPEN flag.
13611 	 */
13612 	smp_mb__after_atomic();
13613 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13614 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13615 		*stats = bp->net_stats_prev;
13616 		return;
13617 	}
13618 
13619 	bnxt_sync_ring_stats(bp);
13620 	bnxt_get_ring_stats(bp, stats);
13621 	bnxt_add_prev_stats(bp, stats);
13622 
13623 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
13624 		u64 *rx = bp->port_stats.sw_stats;
13625 		u64 *tx = bp->port_stats.sw_stats +
13626 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
13627 
13628 		stats->rx_crc_errors =
13629 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
13630 		stats->rx_frame_errors =
13631 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
13632 		stats->rx_length_errors =
13633 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
13634 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
13635 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
13636 		stats->rx_errors =
13637 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
13638 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
13639 		stats->collisions =
13640 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
13641 		stats->tx_fifo_errors =
13642 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
13643 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
13644 	}
13645 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13646 }
13647 
13648 static void bnxt_get_one_ring_drv_stats(struct bnxt *bp,
13649 					struct bnxt_total_ring_drv_stats *stats,
13650 					struct bnxt_cp_ring_info *cpr)
13651 {
13652 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
13653 	u64 *hw_stats = cpr->stats.sw_stats;
13654 
13655 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
13656 	stats->rx_total_resets += sw_stats->rx.rx_resets;
13657 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
13658 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
13659 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
13660 	stats->rx_total_ring_discards +=
13661 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
13662 	stats->rx_total_hw_gro_packets += sw_stats->rx.rx_hw_gro_packets;
13663 	stats->rx_total_hw_gro_wire_packets += sw_stats->rx.rx_hw_gro_wire_packets;
13664 	stats->tx_total_resets += sw_stats->tx.tx_resets;
13665 	stats->tx_total_ring_discards +=
13666 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
13667 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
13668 }
13669 
13670 void bnxt_get_ring_drv_stats(struct bnxt *bp,
13671 			     struct bnxt_total_ring_drv_stats *stats)
13672 {
13673 	int i;
13674 
13675 	for (i = 0; i < bp->cp_nr_rings; i++)
13676 		bnxt_get_one_ring_drv_stats(bp, stats, &bp->bnapi[i]->cp_ring);
13677 }
13678 
13679 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask,
13680 				 const struct netdev_hw_addr_list *mc)
13681 {
13682 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13683 	struct netdev_hw_addr *ha;
13684 	u8 *haddr;
13685 	int mc_count = 0;
13686 	bool update = false;
13687 	int off = 0;
13688 
13689 	netdev_hw_addr_list_for_each(ha, mc) {
13690 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
13691 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13692 			vnic->mc_list_count = 0;
13693 			return false;
13694 		}
13695 		haddr = ha->addr;
13696 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
13697 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
13698 			update = true;
13699 		}
13700 		off += ETH_ALEN;
13701 		mc_count++;
13702 	}
13703 	if (mc_count)
13704 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13705 
13706 	if (mc_count != vnic->mc_list_count) {
13707 		vnic->mc_list_count = mc_count;
13708 		update = true;
13709 	}
13710 	return update;
13711 }
13712 
13713 static bool bnxt_uc_list_updated(struct bnxt *bp,
13714 				 const struct netdev_hw_addr_list *uc)
13715 {
13716 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13717 	struct netdev_hw_addr *ha;
13718 	int off = 0;
13719 
13720 	if (netdev_hw_addr_list_count(uc) != (vnic->uc_filter_count - 1))
13721 		return true;
13722 
13723 	netdev_hw_addr_list_for_each(ha, uc) {
13724 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
13725 			return true;
13726 
13727 		off += ETH_ALEN;
13728 	}
13729 	return false;
13730 }
13731 
13732 static int bnxt_set_rx_mode(struct net_device *dev,
13733 			    struct netdev_hw_addr_list *uc,
13734 			    struct netdev_hw_addr_list *mc)
13735 {
13736 	struct bnxt *bp = netdev_priv(dev);
13737 	struct bnxt_vnic_info *vnic;
13738 	bool mc_update = false;
13739 	bool uc_update;
13740 	u32 mask;
13741 
13742 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13743 		return 0;
13744 
13745 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13746 	mask = vnic->rx_mask;
13747 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13748 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13749 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13750 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13751 
13752 	if (dev->flags & IFF_PROMISC)
13753 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13754 
13755 	uc_update = bnxt_uc_list_updated(bp, uc);
13756 
13757 	if (dev->flags & IFF_BROADCAST)
13758 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13759 	if (dev->flags & IFF_ALLMULTI) {
13760 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13761 		vnic->mc_list_count = 0;
13762 	} else if (dev->flags & IFF_MULTICAST) {
13763 		mc_update = bnxt_mc_list_updated(bp, &mask, mc);
13764 	}
13765 
13766 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13767 		vnic->rx_mask = mask;
13768 
13769 		return bnxt_cfg_rx_mode(bp, uc, uc_update);
13770 	}
13771 
13772 	return 0;
13773 }
13774 
13775 static int bnxt_cfg_rx_mode(struct bnxt *bp, struct netdev_hw_addr_list *uc,
13776 			    bool uc_update)
13777 {
13778 	struct net_device *dev = bp->dev;
13779 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13780 	struct netdev_hw_addr *ha;
13781 	int i, off = 0, rc;
13782 
13783 	if (!uc_update)
13784 		goto skip_uc;
13785 
13786 	for (i = 1; i < vnic->uc_filter_count; i++) {
13787 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13788 
13789 		bnxt_hwrm_l2_filter_free(bp, fltr);
13790 		bnxt_del_l2_filter(bp, fltr);
13791 	}
13792 
13793 	vnic->uc_filter_count = 1;
13794 
13795 	netif_addr_lock_bh(dev);
13796 	if (netdev_hw_addr_list_count(uc) > (BNXT_MAX_UC_ADDRS - 1)) {
13797 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13798 	} else {
13799 		netdev_hw_addr_list_for_each(ha, uc) {
13800 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13801 			off += ETH_ALEN;
13802 			vnic->uc_filter_count++;
13803 		}
13804 	}
13805 	netif_addr_unlock_bh(dev);
13806 
13807 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13808 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13809 		if (rc) {
13810 			if (BNXT_VF(bp) && rc == -ENODEV) {
13811 				netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13812 				rc = -EAGAIN;
13813 			} else if (rc == -EAGAIN) {
13814 				netdev_warn(bp->dev, "FW busy while setting vnic filter, will retry\n");
13815 			} else {
13816 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13817 			}
13818 			vnic->uc_filter_count = i;
13819 			return rc;
13820 		}
13821 	}
13822 
13823 skip_uc:
13824 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13825 	    !bnxt_promisc_ok(bp))
13826 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13827 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13828 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13829 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13830 			    rc);
13831 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13832 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13833 		vnic->mc_list_count = 0;
13834 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13835 	}
13836 	if (rc)
13837 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13838 			   rc);
13839 
13840 	return rc;
13841 }
13842 
13843 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13844 {
13845 #ifdef CONFIG_BNXT_SRIOV
13846 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13847 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13848 
13849 		/* No minimum rings were provisioned by the PF.  Don't
13850 		 * reserve rings by default when device is down.
13851 		 */
13852 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13853 			return true;
13854 
13855 		if (!netif_running(bp->dev))
13856 			return false;
13857 	}
13858 #endif
13859 	return true;
13860 }
13861 
13862 /* If the chip and firmware supports RFS */
13863 static bool bnxt_rfs_supported(struct bnxt *bp)
13864 {
13865 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13866 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13867 			return true;
13868 		return false;
13869 	}
13870 	/* 212 firmware is broken for aRFS */
13871 	if (BNXT_FW_MAJ(bp) == 212)
13872 		return false;
13873 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13874 		return true;
13875 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13876 		return true;
13877 	return false;
13878 }
13879 
13880 /* If runtime conditions support RFS */
13881 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13882 {
13883 	struct bnxt_hw_rings hwr = {0};
13884 	int max_vnics, max_rss_ctxs;
13885 
13886 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13887 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13888 		return bnxt_rfs_supported(bp);
13889 
13890 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13891 		return false;
13892 
13893 	hwr.grp = bp->rx_nr_rings;
13894 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13895 	if (new_rss_ctx)
13896 		hwr.vnic++;
13897 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13898 	max_vnics = bnxt_get_max_func_vnics(bp);
13899 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13900 
13901 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13902 		if (bp->rx_nr_rings > 1)
13903 			netdev_warn(bp->dev,
13904 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13905 				    min(max_rss_ctxs - 1, max_vnics - 1));
13906 		return false;
13907 	}
13908 
13909 	if (!BNXT_NEW_RM(bp))
13910 		return true;
13911 
13912 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13913 	 * issue that will mess up the default VNIC if we reduce the
13914 	 * reservations.
13915 	 */
13916 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13917 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13918 		return true;
13919 
13920 	bnxt_hwrm_reserve_rings(bp, &hwr);
13921 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13922 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13923 		return true;
13924 
13925 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13926 	hwr.vnic = 1;
13927 	hwr.rss_ctx = 0;
13928 	bnxt_hwrm_reserve_rings(bp, &hwr);
13929 	return false;
13930 }
13931 
13932 static netdev_features_t bnxt_fix_features(struct net_device *dev,
13933 					   netdev_features_t features)
13934 {
13935 	struct bnxt *bp = netdev_priv(dev);
13936 	netdev_features_t vlan_features;
13937 
13938 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
13939 		features &= ~NETIF_F_NTUPLE;
13940 
13941 	if ((features & NETIF_F_GSO_UDP_L4) &&
13942 	    !(bp->flags & BNXT_FLAG_UDP_GSO_CAP) &&
13943 	    bp->tx_ring_size < 2 * BNXT_SW_USO_MAX_DESCS)
13944 		features &= ~NETIF_F_GSO_UDP_L4;
13945 
13946 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
13947 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13948 
13949 	if (!(features & NETIF_F_GRO))
13950 		features &= ~NETIF_F_GRO_HW;
13951 
13952 	if (features & NETIF_F_GRO_HW)
13953 		features &= ~NETIF_F_LRO;
13954 
13955 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
13956 	 * turned on or off together.
13957 	 */
13958 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
13959 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
13960 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13961 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13962 		else if (vlan_features)
13963 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13964 	}
13965 #ifdef CONFIG_BNXT_SRIOV
13966 	if (BNXT_VF(bp) && bp->vf.vlan)
13967 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13968 #endif
13969 	return features;
13970 }
13971 
13972 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
13973 				bool link_re_init, u32 flags, bool update_tpa)
13974 {
13975 	bnxt_close_nic(bp, irq_re_init, link_re_init);
13976 	bp->flags = flags;
13977 	if (update_tpa)
13978 		bnxt_set_ring_params(bp);
13979 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
13980 }
13981 
13982 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
13983 {
13984 	bool update_tpa = false, update_ntuple = false;
13985 	struct bnxt *bp = netdev_priv(dev);
13986 	u32 flags = bp->flags;
13987 	u32 changes;
13988 	int rc = 0;
13989 	bool re_init = false;
13990 
13991 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
13992 				   bnxt_min_tx_desc_cnt(bp, features));
13993 
13994 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
13995 	if (features & NETIF_F_GRO_HW)
13996 		flags |= BNXT_FLAG_GRO;
13997 	else if (features & NETIF_F_LRO)
13998 		flags |= BNXT_FLAG_LRO;
13999 
14000 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
14001 		flags &= ~BNXT_FLAG_TPA;
14002 
14003 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
14004 		flags |= BNXT_FLAG_STRIP_VLAN;
14005 
14006 	if (features & NETIF_F_NTUPLE)
14007 		flags |= BNXT_FLAG_RFS;
14008 	else
14009 		bnxt_clear_usr_fltrs(bp, true);
14010 
14011 	changes = flags ^ bp->flags;
14012 	if (changes & BNXT_FLAG_TPA) {
14013 		update_tpa = true;
14014 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
14015 		    (flags & BNXT_FLAG_TPA) == 0 ||
14016 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14017 			re_init = true;
14018 	}
14019 
14020 	if (changes & ~BNXT_FLAG_TPA)
14021 		re_init = true;
14022 
14023 	if (changes & BNXT_FLAG_RFS)
14024 		update_ntuple = true;
14025 
14026 	if (flags != bp->flags) {
14027 		u32 old_flags = bp->flags;
14028 
14029 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14030 			bp->flags = flags;
14031 			if (update_tpa)
14032 				bnxt_set_ring_params(bp);
14033 			return rc;
14034 		}
14035 
14036 		if (update_ntuple)
14037 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
14038 
14039 		if (re_init)
14040 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
14041 
14042 		if (update_tpa) {
14043 			bp->flags = flags;
14044 			rc = bnxt_set_tpa(bp,
14045 					  (flags & BNXT_FLAG_TPA) ?
14046 					  true : false);
14047 			if (rc)
14048 				bp->flags = old_flags;
14049 		}
14050 	}
14051 	return rc;
14052 }
14053 
14054 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
14055 			      u8 **nextp)
14056 {
14057 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
14058 	int hdr_count = 0;
14059 	u8 *nexthdr;
14060 	int start;
14061 
14062 	/* Check that there are at most 2 IPv6 extension headers, no
14063 	 * fragment header, and each is <= 64 bytes.
14064 	 */
14065 	start = nw_off + sizeof(*ip6h);
14066 	nexthdr = &ip6h->nexthdr;
14067 	while (ipv6_ext_hdr(*nexthdr)) {
14068 		struct ipv6_opt_hdr *hp;
14069 		int hdrlen;
14070 
14071 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
14072 		    *nexthdr == NEXTHDR_FRAGMENT)
14073 			return false;
14074 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
14075 					  skb_headlen(skb), NULL);
14076 		if (!hp)
14077 			return false;
14078 		if (*nexthdr == NEXTHDR_AUTH)
14079 			hdrlen = ipv6_authlen(hp);
14080 		else
14081 			hdrlen = ipv6_optlen(hp);
14082 
14083 		if (hdrlen > 64)
14084 			return false;
14085 
14086 		hdr_count++;
14087 		nexthdr = &hp->nexthdr;
14088 		start += hdrlen;
14089 	}
14090 	if (nextp) {
14091 		/* Caller will check inner protocol */
14092 		if (skb->encapsulation) {
14093 			*nextp = nexthdr;
14094 			return true;
14095 		}
14096 		*nextp = NULL;
14097 	}
14098 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
14099 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
14100 }
14101 
14102 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
14103 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
14104 {
14105 	struct udphdr *uh = udp_hdr(skb);
14106 	__be16 udp_port = uh->dest;
14107 
14108 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
14109 	    udp_port != bp->vxlan_gpe_port)
14110 		return false;
14111 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
14112 		struct ethhdr *eh = inner_eth_hdr(skb);
14113 
14114 		switch (eh->h_proto) {
14115 		case htons(ETH_P_IP):
14116 			return true;
14117 		case htons(ETH_P_IPV6):
14118 			return bnxt_exthdr_check(bp, skb,
14119 						 skb_inner_network_offset(skb),
14120 						 NULL);
14121 		}
14122 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
14123 		return true;
14124 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
14125 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
14126 					 NULL);
14127 	}
14128 	return false;
14129 }
14130 
14131 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
14132 {
14133 	switch (l4_proto) {
14134 	case IPPROTO_UDP:
14135 		return bnxt_udp_tunl_check(bp, skb);
14136 	case IPPROTO_IPIP:
14137 		return true;
14138 	case IPPROTO_GRE: {
14139 		switch (skb->inner_protocol) {
14140 		default:
14141 			return false;
14142 		case htons(ETH_P_IP):
14143 			return true;
14144 		case htons(ETH_P_IPV6):
14145 			fallthrough;
14146 		}
14147 	}
14148 	case IPPROTO_IPV6:
14149 		/* Check ext headers of inner ipv6 */
14150 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
14151 					 NULL);
14152 	}
14153 	return false;
14154 }
14155 
14156 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
14157 					     struct net_device *dev,
14158 					     netdev_features_t features)
14159 {
14160 	struct bnxt *bp = netdev_priv(dev);
14161 	u8 *l4_proto;
14162 
14163 	features = vlan_features_check(skb, features);
14164 	switch (vlan_get_protocol(skb)) {
14165 	case htons(ETH_P_IP):
14166 		if (!skb->encapsulation)
14167 			return features;
14168 		l4_proto = &ip_hdr(skb)->protocol;
14169 		if (bnxt_tunl_check(bp, skb, *l4_proto))
14170 			return features;
14171 		break;
14172 	case htons(ETH_P_IPV6):
14173 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
14174 				       &l4_proto))
14175 			break;
14176 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
14177 			return features;
14178 		break;
14179 	}
14180 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
14181 }
14182 
14183 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
14184 			 u32 *reg_buf)
14185 {
14186 	struct hwrm_dbg_read_direct_output *resp;
14187 	struct hwrm_dbg_read_direct_input *req;
14188 	__le32 *dbg_reg_buf;
14189 	dma_addr_t mapping;
14190 	int rc, i;
14191 
14192 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
14193 	if (rc)
14194 		return rc;
14195 
14196 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
14197 					 &mapping);
14198 	if (!dbg_reg_buf) {
14199 		rc = -ENOMEM;
14200 		goto dbg_rd_reg_exit;
14201 	}
14202 
14203 	req->host_dest_addr = cpu_to_le64(mapping);
14204 
14205 	resp = hwrm_req_hold(bp, req);
14206 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
14207 	req->read_len32 = cpu_to_le32(num_words);
14208 
14209 	rc = hwrm_req_send(bp, req);
14210 	if (rc || resp->error_code) {
14211 		rc = -EIO;
14212 		goto dbg_rd_reg_exit;
14213 	}
14214 	for (i = 0; i < num_words; i++)
14215 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
14216 
14217 dbg_rd_reg_exit:
14218 	hwrm_req_drop(bp, req);
14219 	return rc;
14220 }
14221 
14222 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
14223 				       u32 ring_id, u32 *prod, u32 *cons)
14224 {
14225 	struct hwrm_dbg_ring_info_get_output *resp;
14226 	struct hwrm_dbg_ring_info_get_input *req;
14227 	int rc;
14228 
14229 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
14230 	if (rc)
14231 		return rc;
14232 
14233 	req->ring_type = ring_type;
14234 	req->fw_ring_id = cpu_to_le32(ring_id);
14235 	resp = hwrm_req_hold(bp, req);
14236 	rc = hwrm_req_send(bp, req);
14237 	if (!rc) {
14238 		*prod = le32_to_cpu(resp->producer_index);
14239 		*cons = le32_to_cpu(resp->consumer_index);
14240 	}
14241 	hwrm_req_drop(bp, req);
14242 	return rc;
14243 }
14244 
14245 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
14246 {
14247 	struct bnxt_tx_ring_info *txr;
14248 	int i = bnapi->index, j;
14249 
14250 	bnxt_for_each_napi_tx(j, bnapi, txr)
14251 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
14252 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
14253 			    txr->tx_cons);
14254 }
14255 
14256 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
14257 {
14258 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
14259 	int i = bnapi->index;
14260 
14261 	if (!rxr)
14262 		return;
14263 
14264 	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",
14265 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
14266 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
14267 		    rxr->rx_sw_agg_prod);
14268 }
14269 
14270 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
14271 {
14272 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring, *cpr2;
14273 	int i = bnapi->index, j;
14274 
14275 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
14276 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
14277 	for (j = 0; j < cpr->cp_ring_count; j++) {
14278 		cpr2 = &cpr->cp_ring_arr[j];
14279 		if (!cpr2->bnapi)
14280 			continue;
14281 		netdev_info(bnapi->bp->dev, "[%d.%d]: cp{fw_ring: %d raw_cons: %x}\n",
14282 			    i, j, cpr2->cp_ring_struct.fw_ring_id,
14283 			    cpr2->cp_raw_cons);
14284 	}
14285 }
14286 
14287 static void bnxt_dbg_dump_states(struct bnxt *bp)
14288 {
14289 	int i;
14290 	struct bnxt_napi *bnapi;
14291 
14292 	for (i = 0; i < bp->cp_nr_rings; i++) {
14293 		bnapi = bp->bnapi[i];
14294 		if (netif_msg_drv(bp)) {
14295 			bnxt_dump_tx_sw_state(bnapi);
14296 			bnxt_dump_rx_sw_state(bnapi);
14297 			bnxt_dump_cp_sw_state(bnapi);
14298 		}
14299 	}
14300 }
14301 
14302 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
14303 {
14304 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
14305 	struct hwrm_ring_reset_input *req;
14306 	struct bnxt_napi *bnapi = rxr->bnapi;
14307 	struct bnxt_cp_ring_info *cpr;
14308 	u16 cp_ring_id;
14309 	int rc;
14310 
14311 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
14312 	if (rc)
14313 		return rc;
14314 
14315 	cpr = &bnapi->cp_ring;
14316 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
14317 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
14318 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
14319 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
14320 	return hwrm_req_send_silent(bp, req);
14321 }
14322 
14323 static void bnxt_reset_task(struct bnxt *bp, bool silent)
14324 {
14325 	if (!silent)
14326 		bnxt_dbg_dump_states(bp);
14327 	if (netif_running(bp->dev)) {
14328 		bnxt_close_nic(bp, !silent, false);
14329 		bnxt_open_nic(bp, !silent, false);
14330 	}
14331 }
14332 
14333 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
14334 {
14335 	struct bnxt *bp = netdev_priv(dev);
14336 
14337 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
14338 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
14339 }
14340 
14341 static void bnxt_fw_health_check(struct bnxt *bp)
14342 {
14343 	struct bnxt_fw_health *fw_health = bp->fw_health;
14344 	struct pci_dev *pdev = bp->pdev;
14345 	u32 val;
14346 
14347 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14348 		return;
14349 
14350 	/* Make sure it is enabled before checking the tmr_counter. */
14351 	smp_rmb();
14352 	if (fw_health->tmr_counter) {
14353 		fw_health->tmr_counter--;
14354 		return;
14355 	}
14356 
14357 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14358 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
14359 		fw_health->arrests++;
14360 		goto fw_reset;
14361 	}
14362 
14363 	fw_health->last_fw_heartbeat = val;
14364 
14365 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14366 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
14367 		fw_health->discoveries++;
14368 		goto fw_reset;
14369 	}
14370 
14371 	fw_health->tmr_counter = fw_health->tmr_multiplier;
14372 	return;
14373 
14374 fw_reset:
14375 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
14376 }
14377 
14378 static void bnxt_timer(struct timer_list *t)
14379 {
14380 	struct bnxt *bp = timer_container_of(bp, t, timer);
14381 	struct net_device *dev = bp->dev;
14382 
14383 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
14384 		return;
14385 
14386 	if (atomic_read(&bp->intr_sem) != 0)
14387 		goto bnxt_restart_timer;
14388 
14389 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
14390 		bnxt_fw_health_check(bp);
14391 
14392 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
14393 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
14394 
14395 	if (bnxt_tc_flower_enabled(bp))
14396 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
14397 
14398 #ifdef CONFIG_RFS_ACCEL
14399 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
14400 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14401 #endif /*CONFIG_RFS_ACCEL*/
14402 
14403 	if (bp->link_info.phy_retry) {
14404 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
14405 			bp->link_info.phy_retry = false;
14406 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
14407 		} else {
14408 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
14409 		}
14410 	}
14411 
14412 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
14413 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
14414 
14415 bnxt_restart_timer:
14416 	mod_timer(&bp->timer, jiffies + bp->current_interval);
14417 }
14418 
14419 static void bnxt_lock_sp(struct bnxt *bp)
14420 {
14421 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
14422 	 * set.  If the device is being closed, bnxt_close() may be holding
14423 	 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear.
14424 	 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev
14425 	 * instance lock.
14426 	 */
14427 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14428 	netdev_lock(bp->dev);
14429 }
14430 
14431 static void bnxt_unlock_sp(struct bnxt *bp)
14432 {
14433 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14434 	netdev_unlock(bp->dev);
14435 }
14436 
14437 /* Same as bnxt_lock_sp() with additional rtnl_lock */
14438 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
14439 {
14440 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14441 	rtnl_lock();
14442 	netdev_lock(bp->dev);
14443 }
14444 
14445 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
14446 {
14447 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14448 	netdev_unlock(bp->dev);
14449 	rtnl_unlock();
14450 }
14451 
14452 /* Only called from bnxt_sp_task() */
14453 static void bnxt_reset(struct bnxt *bp, bool silent)
14454 {
14455 	bnxt_rtnl_lock_sp(bp);
14456 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
14457 		bnxt_reset_task(bp, silent);
14458 	bnxt_rtnl_unlock_sp(bp);
14459 }
14460 
14461 /* Only called from bnxt_sp_task() */
14462 static void bnxt_rx_ring_reset(struct bnxt *bp)
14463 {
14464 	int i;
14465 
14466 	bnxt_rtnl_lock_sp(bp);
14467 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14468 		bnxt_rtnl_unlock_sp(bp);
14469 		return;
14470 	}
14471 	/* Disable and flush TPA before resetting the RX ring */
14472 	if (bp->flags & BNXT_FLAG_TPA)
14473 		bnxt_set_tpa(bp, false);
14474 	for (i = 0; i < bp->rx_nr_rings; i++) {
14475 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
14476 		struct bnxt_cp_ring_info *cpr;
14477 		int rc;
14478 
14479 		if (!rxr->bnapi->in_reset)
14480 			continue;
14481 
14482 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
14483 		if (rc) {
14484 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
14485 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
14486 			else
14487 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
14488 					    rc);
14489 			bnxt_reset_task(bp, true);
14490 			break;
14491 		}
14492 		bnxt_free_one_rx_ring_skbs(bp, rxr);
14493 		rxr->rx_prod = 0;
14494 		rxr->rx_agg_prod = 0;
14495 		rxr->rx_sw_agg_prod = 0;
14496 		rxr->rx_next_cons = 0;
14497 		rxr->bnapi->in_reset = false;
14498 		bnxt_alloc_one_rx_ring(bp, i);
14499 		cpr = &rxr->bnapi->cp_ring;
14500 		cpr->sw_stats->rx.rx_resets++;
14501 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
14502 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
14503 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
14504 	}
14505 	if (bp->flags & BNXT_FLAG_TPA)
14506 		bnxt_set_tpa(bp, true);
14507 	bnxt_rtnl_unlock_sp(bp);
14508 }
14509 
14510 static void bnxt_fw_fatal_close(struct bnxt *bp)
14511 {
14512 	bnxt_tx_disable(bp);
14513 	bnxt_disable_napi(bp);
14514 	bnxt_disable_int_sync(bp);
14515 	bnxt_free_irq(bp);
14516 	bnxt_clear_int_mode(bp);
14517 	pci_disable_device(bp->pdev);
14518 }
14519 
14520 static void bnxt_fw_reset_close(struct bnxt *bp)
14521 {
14522 	/* When firmware is in fatal state, quiesce device and disable
14523 	 * bus master to prevent any potential bad DMAs before freeing
14524 	 * kernel memory.
14525 	 */
14526 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
14527 		u16 val = 0;
14528 
14529 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14530 		if (val == 0xffff)
14531 			bp->fw_reset_min_dsecs = 0;
14532 		bnxt_fw_fatal_close(bp);
14533 	}
14534 	__bnxt_close_nic(bp, true, false);
14535 	bnxt_vf_reps_free(bp);
14536 	bnxt_clear_int_mode(bp);
14537 	bnxt_hwrm_func_drv_unrgtr(bp);
14538 	if (pci_is_enabled(bp->pdev))
14539 		pci_disable_device(bp->pdev);
14540 	bnxt_free_ctx_mem(bp, false);
14541 }
14542 
14543 static bool is_bnxt_fw_ok(struct bnxt *bp)
14544 {
14545 	struct bnxt_fw_health *fw_health = bp->fw_health;
14546 	bool no_heartbeat = false, has_reset = false;
14547 	u32 val;
14548 
14549 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14550 	if (val == fw_health->last_fw_heartbeat)
14551 		no_heartbeat = true;
14552 
14553 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14554 	if (val != fw_health->last_fw_reset_cnt)
14555 		has_reset = true;
14556 
14557 	if (!no_heartbeat && has_reset)
14558 		return true;
14559 
14560 	return false;
14561 }
14562 
14563 /* netdev instance lock is acquired before calling this function */
14564 static void bnxt_force_fw_reset(struct bnxt *bp)
14565 {
14566 	struct bnxt_fw_health *fw_health = bp->fw_health;
14567 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14568 	u32 wait_dsecs;
14569 
14570 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
14571 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14572 		return;
14573 
14574 	/* we have to serialize with bnxt_refclk_read()*/
14575 	if (ptp) {
14576 		unsigned long flags;
14577 
14578 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
14579 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14580 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14581 	} else {
14582 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14583 	}
14584 	bnxt_fw_reset_close(bp);
14585 	wait_dsecs = fw_health->master_func_wait_dsecs;
14586 	if (fw_health->primary) {
14587 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
14588 			wait_dsecs = 0;
14589 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14590 	} else {
14591 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
14592 		wait_dsecs = fw_health->normal_func_wait_dsecs;
14593 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14594 	}
14595 
14596 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
14597 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
14598 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14599 }
14600 
14601 void bnxt_fw_exception(struct bnxt *bp)
14602 {
14603 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
14604 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14605 	bnxt_ulp_stop(bp);
14606 	bnxt_lock_sp(bp);
14607 	bnxt_force_fw_reset(bp);
14608 	bnxt_unlock_sp(bp);
14609 }
14610 
14611 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
14612  * < 0 on error.
14613  */
14614 static int bnxt_get_registered_vfs(struct bnxt *bp)
14615 {
14616 #ifdef CONFIG_BNXT_SRIOV
14617 	int rc;
14618 
14619 	if (!BNXT_PF(bp))
14620 		return 0;
14621 
14622 	rc = bnxt_hwrm_func_qcfg(bp);
14623 	if (rc) {
14624 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
14625 		return rc;
14626 	}
14627 	if (bp->pf.registered_vfs)
14628 		return bp->pf.registered_vfs;
14629 	if (bp->sriov_cfg)
14630 		return 1;
14631 #endif
14632 	return 0;
14633 }
14634 
14635 void bnxt_fw_reset(struct bnxt *bp)
14636 {
14637 	bnxt_ulp_stop(bp);
14638 	bnxt_lock_sp(bp);
14639 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
14640 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14641 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14642 		int n = 0, tmo;
14643 
14644 		/* we have to serialize with bnxt_refclk_read()*/
14645 		if (ptp) {
14646 			unsigned long flags;
14647 
14648 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
14649 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14650 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14651 		} else {
14652 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14653 		}
14654 		if (bp->pf.active_vfs &&
14655 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
14656 			n = bnxt_get_registered_vfs(bp);
14657 		if (n < 0) {
14658 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
14659 				   n);
14660 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14661 			netif_close(bp->dev);
14662 			goto fw_reset_exit;
14663 		} else if (n > 0) {
14664 			u16 vf_tmo_dsecs = n * 10;
14665 
14666 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
14667 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
14668 			bp->fw_reset_state =
14669 				BNXT_FW_RESET_STATE_POLL_VF;
14670 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14671 			goto fw_reset_exit;
14672 		}
14673 		bnxt_fw_reset_close(bp);
14674 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14675 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14676 			tmo = HZ / 10;
14677 		} else {
14678 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14679 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14680 		}
14681 		bnxt_queue_fw_reset_work(bp, tmo);
14682 	}
14683 fw_reset_exit:
14684 	bnxt_unlock_sp(bp);
14685 }
14686 
14687 static void bnxt_chk_missed_irq(struct bnxt *bp)
14688 {
14689 	int i;
14690 
14691 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14692 		return;
14693 
14694 	for (i = 0; i < bp->cp_nr_rings; i++) {
14695 		struct bnxt_napi *bnapi = bp->bnapi[i];
14696 		struct bnxt_cp_ring_info *cpr;
14697 		u32 fw_ring_id;
14698 		int j;
14699 
14700 		if (!bnapi)
14701 			continue;
14702 
14703 		cpr = &bnapi->cp_ring;
14704 		for (j = 0; j < cpr->cp_ring_count; j++) {
14705 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
14706 			u32 val[2];
14707 
14708 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
14709 				continue;
14710 
14711 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
14712 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
14713 				continue;
14714 			}
14715 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
14716 			bnxt_dbg_hwrm_ring_info_get(bp,
14717 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
14718 				fw_ring_id, &val[0], &val[1]);
14719 			cpr->sw_stats->cmn.missed_irqs++;
14720 		}
14721 	}
14722 }
14723 
14724 static void bnxt_cfg_ntp_filters(struct bnxt *);
14725 
14726 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
14727 {
14728 	struct bnxt_link_info *link_info = &bp->link_info;
14729 
14730 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
14731 		link_info->autoneg = BNXT_AUTONEG_SPEED;
14732 		if (bp->hwrm_spec_code >= 0x10201) {
14733 			if (link_info->auto_pause_setting &
14734 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
14735 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14736 		} else {
14737 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14738 		}
14739 		bnxt_set_auto_speed(link_info);
14740 	} else {
14741 		bnxt_set_force_speed(link_info);
14742 		link_info->req_duplex = link_info->duplex_setting;
14743 	}
14744 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14745 		link_info->req_flow_ctrl =
14746 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14747 	else
14748 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14749 }
14750 
14751 static void bnxt_fw_echo_reply(struct bnxt *bp)
14752 {
14753 	struct bnxt_fw_health *fw_health = bp->fw_health;
14754 	struct hwrm_func_echo_response_input *req;
14755 	int rc;
14756 
14757 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14758 	if (rc)
14759 		return;
14760 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14761 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14762 	hwrm_req_send(bp, req);
14763 }
14764 
14765 static void bnxt_ulp_restart(struct bnxt *bp)
14766 {
14767 	bnxt_ulp_stop(bp);
14768 	bnxt_ulp_start(bp);
14769 }
14770 
14771 static void bnxt_sp_task(struct work_struct *work)
14772 {
14773 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14774 
14775 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14776 	smp_mb__after_atomic();
14777 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14778 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14779 		return;
14780 	}
14781 
14782 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14783 		bnxt_ulp_restart(bp);
14784 		bnxt_reenable_sriov(bp);
14785 	}
14786 
14787 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14788 		bnxt_cfg_ntp_filters(bp);
14789 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14790 		bnxt_hwrm_exec_fwd_req(bp);
14791 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14792 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14793 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14794 		bnxt_hwrm_port_qstats(bp, 0);
14795 		bnxt_hwrm_port_qstats_ext(bp, 0);
14796 		spin_lock(&bp->stats_lock);
14797 		bnxt_accumulate_all_stats(bp);
14798 		bp->stats_updated_jiffies = jiffies;
14799 		spin_unlock(&bp->stats_lock);
14800 	}
14801 
14802 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14803 		int rc;
14804 
14805 		mutex_lock(&bp->link_lock);
14806 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14807 				       &bp->sp_event))
14808 			bnxt_hwrm_phy_qcaps(bp);
14809 
14810 		rc = bnxt_update_link(bp, true);
14811 		if (rc)
14812 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14813 				   rc);
14814 
14815 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14816 				       &bp->sp_event))
14817 			bnxt_init_ethtool_link_settings(bp);
14818 		mutex_unlock(&bp->link_lock);
14819 	}
14820 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14821 		int rc;
14822 
14823 		mutex_lock(&bp->link_lock);
14824 		rc = bnxt_update_phy_setting(bp);
14825 		mutex_unlock(&bp->link_lock);
14826 		if (rc) {
14827 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14828 		} else {
14829 			bp->link_info.phy_retry = false;
14830 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14831 		}
14832 	}
14833 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14834 		mutex_lock(&bp->link_lock);
14835 		bnxt_get_port_module_status(bp);
14836 		mutex_unlock(&bp->link_lock);
14837 	}
14838 
14839 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14840 		bnxt_tc_flow_stats_work(bp);
14841 
14842 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14843 		bnxt_chk_missed_irq(bp);
14844 
14845 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14846 		bnxt_fw_echo_reply(bp);
14847 
14848 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14849 		bnxt_hwmon_notify_event(bp);
14850 
14851 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14852 	 * must be the last functions to be called before exiting.
14853 	 */
14854 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14855 		bnxt_reset(bp, false);
14856 
14857 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14858 		bnxt_reset(bp, true);
14859 
14860 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14861 		bnxt_rx_ring_reset(bp);
14862 
14863 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14864 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14865 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14866 			bnxt_devlink_health_fw_report(bp);
14867 		else
14868 			bnxt_fw_reset(bp);
14869 	}
14870 
14871 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14872 		if (!is_bnxt_fw_ok(bp))
14873 			bnxt_devlink_health_fw_report(bp);
14874 	}
14875 
14876 	smp_mb__before_atomic();
14877 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14878 }
14879 
14880 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14881 				int *max_cp);
14882 
14883 /* Under netdev instance lock */
14884 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14885 		     int tx_xdp)
14886 {
14887 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14888 	struct bnxt_hw_rings hwr = {0};
14889 	int rx_rings = rx;
14890 	int rc;
14891 
14892 	if (tcs)
14893 		tx_sets = tcs;
14894 
14895 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
14896 
14897 	if (max_rx < rx_rings)
14898 		return -ENOMEM;
14899 
14900 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14901 		rx_rings <<= 1;
14902 
14903 	hwr.rx = rx_rings;
14904 	hwr.tx = tx * tx_sets + tx_xdp;
14905 	if (max_tx < hwr.tx)
14906 		return -ENOMEM;
14907 
14908 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
14909 
14910 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
14911 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
14912 	if (max_cp < hwr.cp)
14913 		return -ENOMEM;
14914 	hwr.stat = hwr.cp;
14915 	if (BNXT_NEW_RM(bp)) {
14916 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
14917 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
14918 		hwr.grp = rx;
14919 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14920 	}
14921 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
14922 		hwr.cp_p5 = hwr.tx + rx;
14923 	rc = bnxt_hwrm_check_rings(bp, &hwr);
14924 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
14925 		if (!bnxt_ulp_registered(bp->edev[BNXT_AUXDEV_RDMA])) {
14926 			hwr.cp += bnxt_get_ulp_msix_num(bp);
14927 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
14928 		}
14929 		if (hwr.cp > bp->total_irqs) {
14930 			int total_msix = bnxt_change_msix(bp, hwr.cp);
14931 
14932 			if (total_msix < hwr.cp) {
14933 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
14934 					    hwr.cp, total_msix);
14935 				rc = -ENOSPC;
14936 			}
14937 		}
14938 	}
14939 	return rc;
14940 }
14941 
14942 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
14943 {
14944 	if (bp->bar2) {
14945 		pci_iounmap(pdev, bp->bar2);
14946 		bp->bar2 = NULL;
14947 	}
14948 
14949 	if (bp->bar1) {
14950 		pci_iounmap(pdev, bp->bar1);
14951 		bp->bar1 = NULL;
14952 	}
14953 
14954 	if (bp->bar0) {
14955 		pci_iounmap(pdev, bp->bar0);
14956 		bp->bar0 = NULL;
14957 	}
14958 }
14959 
14960 static void bnxt_cleanup_pci(struct bnxt *bp)
14961 {
14962 	bnxt_unmap_bars(bp, bp->pdev);
14963 	pci_release_regions(bp->pdev);
14964 	if (pci_is_enabled(bp->pdev))
14965 		pci_disable_device(bp->pdev);
14966 }
14967 
14968 static void bnxt_init_dflt_coal(struct bnxt *bp)
14969 {
14970 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
14971 	struct bnxt_coal *coal;
14972 	u16 flags = 0;
14973 
14974 	if (coal_cap->cmpl_params &
14975 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
14976 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
14977 
14978 	/* Tick values in micro seconds.
14979 	 * 1 coal_buf x bufs_per_record = 1 completion record.
14980 	 */
14981 	coal = &bp->rx_coal;
14982 	coal->coal_ticks = 10;
14983 	coal->coal_bufs = 30;
14984 	coal->coal_ticks_irq = 1;
14985 	coal->coal_bufs_irq = 2;
14986 	coal->idle_thresh = 50;
14987 	coal->bufs_per_record = 2;
14988 	coal->budget = 64;		/* NAPI budget */
14989 	coal->flags = flags;
14990 
14991 	coal = &bp->tx_coal;
14992 	coal->coal_ticks = 28;
14993 	coal->coal_bufs = 30;
14994 	coal->coal_ticks_irq = 2;
14995 	coal->coal_bufs_irq = 2;
14996 	coal->bufs_per_record = 1;
14997 	coal->flags = flags;
14998 
14999 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
15000 }
15001 
15002 /* FW that pre-reserves 1 VNIC per function */
15003 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
15004 {
15005 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
15006 
15007 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
15008 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
15009 		return true;
15010 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
15011 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
15012 		return true;
15013 	return false;
15014 }
15015 
15016 static void bnxt_hwrm_pfcwd_qcaps(struct bnxt *bp)
15017 {
15018 	struct hwrm_queue_pfcwd_timeout_qcaps_output *resp;
15019 	struct hwrm_queue_pfcwd_timeout_qcaps_input *req;
15020 	int rc;
15021 
15022 	bp->max_pfcwd_tmo_ms = 0;
15023 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_PFCWD_TIMEOUT_QCAPS);
15024 	if (rc)
15025 		return;
15026 	resp = hwrm_req_hold(bp, req);
15027 	rc = hwrm_req_send_silent(bp, req);
15028 	if (!rc)
15029 		bp->max_pfcwd_tmo_ms = le16_to_cpu(resp->max_pfcwd_timeout);
15030 	hwrm_req_drop(bp, req);
15031 }
15032 
15033 static int bnxt_fw_init_one_p1(struct bnxt *bp)
15034 {
15035 	int rc;
15036 
15037 	bp->fw_cap = 0;
15038 	rc = bnxt_hwrm_ver_get(bp);
15039 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
15040 	 * so wait before continuing with recovery.
15041 	 */
15042 	if (rc)
15043 		msleep(100);
15044 	bnxt_try_map_fw_health_reg(bp);
15045 	if (rc) {
15046 		rc = bnxt_try_recover_fw(bp);
15047 		if (rc)
15048 			return rc;
15049 		rc = bnxt_hwrm_ver_get(bp);
15050 		if (rc)
15051 			return rc;
15052 	}
15053 
15054 	bnxt_nvm_cfg_ver_get(bp);
15055 
15056 	rc = bnxt_hwrm_func_reset(bp);
15057 	if (rc)
15058 		return -ENODEV;
15059 
15060 	bnxt_hwrm_fw_set_time(bp);
15061 	return 0;
15062 }
15063 
15064 static int bnxt_fw_init_one_p2(struct bnxt *bp)
15065 {
15066 	int rc;
15067 
15068 	/* Get the MAX capabilities for this function */
15069 	rc = bnxt_hwrm_func_qcaps(bp);
15070 	if (rc) {
15071 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
15072 			   rc);
15073 		return -ENODEV;
15074 	}
15075 
15076 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
15077 	if (rc)
15078 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
15079 			    rc);
15080 
15081 	if (bnxt_alloc_fw_health(bp)) {
15082 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
15083 	} else {
15084 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
15085 		if (rc)
15086 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
15087 				    rc);
15088 	}
15089 
15090 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
15091 	if (rc)
15092 		return -ENODEV;
15093 
15094 	rc = bnxt_alloc_crash_dump_mem(bp);
15095 	if (rc)
15096 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
15097 			    rc);
15098 	if (!rc) {
15099 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
15100 		if (rc) {
15101 			bnxt_free_crash_dump_mem(bp);
15102 			netdev_warn(bp->dev,
15103 				    "hwrm crash dump mem failure rc: %d\n", rc);
15104 		}
15105 	}
15106 
15107 	if (bnxt_fw_pre_resv_vnics(bp))
15108 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
15109 
15110 	bnxt_hwrm_pfcwd_qcaps(bp);
15111 	bnxt_hwrm_func_qcfg(bp);
15112 	bnxt_hwrm_vnic_qcaps(bp);
15113 	bnxt_hwrm_port_led_qcaps(bp);
15114 	bnxt_ethtool_init(bp);
15115 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
15116 		__bnxt_hwrm_ptp_qcfg(bp);
15117 	bnxt_dcb_init(bp);
15118 	bnxt_hwmon_init(bp);
15119 	return 0;
15120 }
15121 
15122 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
15123 {
15124 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
15125 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
15126 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
15127 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
15128 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
15129 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
15130 		bp->rss_hash_delta = bp->rss_hash_cfg;
15131 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
15132 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
15133 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
15134 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
15135 	}
15136 }
15137 
15138 static void bnxt_set_dflt_rfs(struct bnxt *bp)
15139 {
15140 	struct net_device *dev = bp->dev;
15141 
15142 	dev->hw_features &= ~NETIF_F_NTUPLE;
15143 	dev->features &= ~NETIF_F_NTUPLE;
15144 	bp->flags &= ~BNXT_FLAG_RFS;
15145 	if (bnxt_rfs_supported(bp)) {
15146 		dev->hw_features |= NETIF_F_NTUPLE;
15147 		if (bnxt_rfs_capable(bp, false)) {
15148 			bp->flags |= BNXT_FLAG_RFS;
15149 			dev->features |= NETIF_F_NTUPLE;
15150 		}
15151 	}
15152 }
15153 
15154 static void bnxt_fw_init_one_p3(struct bnxt *bp)
15155 {
15156 	struct pci_dev *pdev = bp->pdev;
15157 
15158 	bnxt_set_dflt_rss_hash_type(bp);
15159 	bnxt_set_dflt_rfs(bp);
15160 
15161 	bnxt_get_wol_settings(bp);
15162 	if (bp->flags & BNXT_FLAG_WOL_CAP)
15163 		device_set_wakeup_enable(&pdev->dev, bp->wol);
15164 	else
15165 		device_set_wakeup_capable(&pdev->dev, false);
15166 
15167 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
15168 	bnxt_hwrm_coal_params_qcaps(bp);
15169 }
15170 
15171 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
15172 
15173 int bnxt_fw_init_one(struct bnxt *bp)
15174 {
15175 	int rc;
15176 
15177 	rc = bnxt_fw_init_one_p1(bp);
15178 	if (rc) {
15179 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
15180 		return rc;
15181 	}
15182 	rc = bnxt_fw_init_one_p2(bp);
15183 	if (rc) {
15184 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
15185 		return rc;
15186 	}
15187 	rc = bnxt_probe_phy(bp, false);
15188 	if (rc)
15189 		return rc;
15190 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
15191 	if (rc)
15192 		return rc;
15193 
15194 	bnxt_fw_init_one_p3(bp);
15195 	return 0;
15196 }
15197 
15198 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
15199 {
15200 	struct bnxt_fw_health *fw_health = bp->fw_health;
15201 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
15202 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
15203 	u32 reg_type, reg_off, delay_msecs;
15204 
15205 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
15206 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
15207 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
15208 	switch (reg_type) {
15209 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
15210 		pci_write_config_dword(bp->pdev, reg_off, val);
15211 		break;
15212 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
15213 		writel(reg_off & BNXT_GRC_BASE_MASK,
15214 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
15215 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
15216 		fallthrough;
15217 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
15218 		writel(val, bp->bar0 + reg_off);
15219 		break;
15220 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
15221 		writel(val, bp->bar1 + reg_off);
15222 		break;
15223 	}
15224 	if (delay_msecs) {
15225 		pci_read_config_dword(bp->pdev, 0, &val);
15226 		msleep(delay_msecs);
15227 	}
15228 }
15229 
15230 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
15231 {
15232 	struct hwrm_func_qcfg_output *resp;
15233 	struct hwrm_func_qcfg_input *req;
15234 	bool result = true; /* firmware will enforce if unknown */
15235 
15236 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
15237 		return result;
15238 
15239 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
15240 		return result;
15241 
15242 	req->fid = cpu_to_le16(0xffff);
15243 	resp = hwrm_req_hold(bp, req);
15244 	if (!hwrm_req_send(bp, req))
15245 		result = !!(le16_to_cpu(resp->flags) &
15246 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
15247 	hwrm_req_drop(bp, req);
15248 	return result;
15249 }
15250 
15251 static void bnxt_reset_all(struct bnxt *bp)
15252 {
15253 	struct bnxt_fw_health *fw_health = bp->fw_health;
15254 	int i, rc;
15255 
15256 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15257 		bnxt_fw_reset_via_optee(bp);
15258 		bp->fw_reset_timestamp = jiffies;
15259 		return;
15260 	}
15261 
15262 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
15263 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
15264 			bnxt_fw_reset_writel(bp, i);
15265 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
15266 		struct hwrm_fw_reset_input *req;
15267 
15268 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
15269 		if (!rc) {
15270 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
15271 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
15272 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
15273 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
15274 			rc = hwrm_req_send(bp, req);
15275 		}
15276 		if (rc != -ENODEV)
15277 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
15278 	}
15279 	bp->fw_reset_timestamp = jiffies;
15280 }
15281 
15282 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
15283 {
15284 	return time_after(jiffies, bp->fw_reset_timestamp +
15285 			  (bp->fw_reset_max_dsecs * HZ / 10));
15286 }
15287 
15288 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
15289 {
15290 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15291 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
15292 		bnxt_dl_health_fw_status_update(bp, false);
15293 	bp->fw_reset_state = BNXT_FW_RESET_STATE_ABORT;
15294 	netif_close(bp->dev);
15295 }
15296 
15297 static void bnxt_fw_reset_task(struct work_struct *work)
15298 {
15299 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
15300 	int rc = 0;
15301 
15302 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
15303 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
15304 		return;
15305 	}
15306 
15307 	switch (bp->fw_reset_state) {
15308 	case BNXT_FW_RESET_STATE_POLL_VF: {
15309 		int n = bnxt_get_registered_vfs(bp);
15310 		int tmo;
15311 
15312 		if (n < 0) {
15313 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
15314 				   n, jiffies_to_msecs(jiffies -
15315 				   bp->fw_reset_timestamp));
15316 			goto fw_reset_abort;
15317 		} else if (n > 0) {
15318 			if (bnxt_fw_reset_timeout(bp)) {
15319 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15320 				bp->fw_reset_state = 0;
15321 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
15322 					   n);
15323 				goto ulp_start;
15324 			}
15325 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15326 			return;
15327 		}
15328 		bp->fw_reset_timestamp = jiffies;
15329 		netdev_lock(bp->dev);
15330 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
15331 			bnxt_fw_reset_abort(bp, rc);
15332 			netdev_unlock(bp->dev);
15333 			goto ulp_start;
15334 		}
15335 		bnxt_fw_reset_close(bp);
15336 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15337 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
15338 			tmo = HZ / 10;
15339 		} else {
15340 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15341 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
15342 		}
15343 		netdev_unlock(bp->dev);
15344 		bnxt_queue_fw_reset_work(bp, tmo);
15345 		return;
15346 	}
15347 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
15348 		u32 val;
15349 
15350 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15351 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
15352 		    !bnxt_fw_reset_timeout(bp)) {
15353 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15354 			return;
15355 		}
15356 
15357 		if (!bp->fw_health->primary) {
15358 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
15359 
15360 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15361 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
15362 			return;
15363 		}
15364 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
15365 	}
15366 		fallthrough;
15367 	case BNXT_FW_RESET_STATE_RESET_FW:
15368 		bnxt_reset_all(bp);
15369 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15370 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
15371 		return;
15372 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
15373 		bnxt_inv_fw_health_reg(bp);
15374 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
15375 		    !bp->fw_reset_min_dsecs) {
15376 			u16 val;
15377 
15378 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
15379 			if (val == 0xffff) {
15380 				if (bnxt_fw_reset_timeout(bp)) {
15381 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
15382 					rc = -ETIMEDOUT;
15383 					goto fw_reset_abort;
15384 				}
15385 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
15386 				return;
15387 			}
15388 		}
15389 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
15390 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
15391 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
15392 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
15393 			bnxt_dl_remote_reload(bp);
15394 		if (pci_enable_device(bp->pdev)) {
15395 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
15396 			rc = -ENODEV;
15397 			goto fw_reset_abort;
15398 		}
15399 		pci_set_master(bp->pdev);
15400 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
15401 		fallthrough;
15402 	case BNXT_FW_RESET_STATE_POLL_FW:
15403 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
15404 		rc = bnxt_hwrm_poll(bp);
15405 		if (rc) {
15406 			if (bnxt_fw_reset_timeout(bp)) {
15407 				netdev_err(bp->dev, "Firmware reset aborted\n");
15408 				goto fw_reset_abort_status;
15409 			}
15410 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15411 			return;
15412 		}
15413 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
15414 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
15415 		fallthrough;
15416 	case BNXT_FW_RESET_STATE_OPENING:
15417 		while (!rtnl_trylock()) {
15418 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15419 			return;
15420 		}
15421 		netdev_lock(bp->dev);
15422 		rc = bnxt_open(bp->dev);
15423 		if (rc) {
15424 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
15425 			bnxt_fw_reset_abort(bp, rc);
15426 			netdev_unlock(bp->dev);
15427 			rtnl_unlock();
15428 			goto ulp_start;
15429 		}
15430 
15431 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
15432 		    bp->fw_health->enabled) {
15433 			bp->fw_health->last_fw_reset_cnt =
15434 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
15435 		}
15436 		bp->fw_reset_state = 0;
15437 		/* Make sure fw_reset_state is 0 before clearing the flag */
15438 		smp_mb__before_atomic();
15439 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15440 		bnxt_ptp_reapply_pps(bp);
15441 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
15442 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
15443 			bnxt_dl_health_fw_recovery_done(bp);
15444 			bnxt_dl_health_fw_status_update(bp, true);
15445 		}
15446 		netdev_unlock(bp->dev);
15447 		rtnl_unlock();
15448 		bnxt_ulp_start(bp);
15449 		bnxt_reenable_sriov(bp);
15450 		netdev_lock(bp->dev);
15451 		bnxt_vf_reps_alloc(bp);
15452 		bnxt_vf_reps_open(bp);
15453 		netdev_unlock(bp->dev);
15454 		break;
15455 	}
15456 	return;
15457 
15458 fw_reset_abort_status:
15459 	if (bp->fw_health->status_reliable ||
15460 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
15461 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15462 
15463 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
15464 	}
15465 fw_reset_abort:
15466 	netdev_lock(bp->dev);
15467 	bnxt_fw_reset_abort(bp, rc);
15468 	netdev_unlock(bp->dev);
15469 ulp_start:
15470 	if (!rc)
15471 		bnxt_ulp_start(bp);
15472 }
15473 
15474 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
15475 {
15476 	int rc;
15477 	struct bnxt *bp = netdev_priv(dev);
15478 
15479 	SET_NETDEV_DEV(dev, &pdev->dev);
15480 
15481 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
15482 	rc = pci_enable_device(pdev);
15483 	if (rc) {
15484 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
15485 		goto init_err;
15486 	}
15487 
15488 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
15489 		dev_err(&pdev->dev,
15490 			"Cannot find PCI device base address, aborting\n");
15491 		rc = -ENODEV;
15492 		goto init_err_disable;
15493 	}
15494 
15495 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
15496 	if (rc) {
15497 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
15498 		goto init_err_disable;
15499 	}
15500 
15501 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
15502 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
15503 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
15504 		rc = -EIO;
15505 		goto init_err_release;
15506 	}
15507 
15508 	pci_set_master(pdev);
15509 
15510 	bp->dev = dev;
15511 	bp->pdev = pdev;
15512 
15513 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
15514 	 * determines the BAR size.
15515 	 */
15516 	bp->bar0 = pci_ioremap_bar(pdev, 0);
15517 	if (!bp->bar0) {
15518 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
15519 		rc = -ENOMEM;
15520 		goto init_err_release;
15521 	}
15522 
15523 	bp->bar2 = pci_ioremap_bar(pdev, 4);
15524 	if (!bp->bar2) {
15525 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
15526 		rc = -ENOMEM;
15527 		goto init_err_release;
15528 	}
15529 
15530 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
15531 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
15532 
15533 	spin_lock_init(&bp->ntp_fltr_lock);
15534 	spin_lock_init(&bp->stats_lock);
15535 #if BITS_PER_LONG == 32
15536 	spin_lock_init(&bp->db_lock);
15537 #endif
15538 
15539 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
15540 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
15541 
15542 	timer_setup(&bp->timer, bnxt_timer, 0);
15543 	bp->current_interval = BNXT_TIMER_INTERVAL;
15544 
15545 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
15546 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
15547 
15548 	clear_bit(BNXT_STATE_OPEN, &bp->state);
15549 	return 0;
15550 
15551 init_err_release:
15552 	bnxt_unmap_bars(bp, pdev);
15553 	pci_release_regions(pdev);
15554 
15555 init_err_disable:
15556 	pci_disable_device(pdev);
15557 
15558 init_err:
15559 	return rc;
15560 }
15561 
15562 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
15563 {
15564 	struct sockaddr *addr = p;
15565 	struct bnxt *bp = netdev_priv(dev);
15566 	int rc = 0;
15567 
15568 	netdev_assert_locked(dev);
15569 
15570 	if (!is_valid_ether_addr(addr->sa_data))
15571 		return -EADDRNOTAVAIL;
15572 
15573 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
15574 		return 0;
15575 
15576 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
15577 	if (rc)
15578 		return rc;
15579 
15580 	eth_hw_addr_set(dev, addr->sa_data);
15581 	bnxt_clear_usr_fltrs(bp, true);
15582 	if (netif_running(dev)) {
15583 		bnxt_close_nic(bp, false, false);
15584 		rc = bnxt_open_nic(bp, false, false);
15585 	}
15586 
15587 	return rc;
15588 }
15589 
15590 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
15591 {
15592 	struct bnxt *bp = netdev_priv(dev);
15593 
15594 	netdev_assert_locked(dev);
15595 
15596 	if (netif_running(dev))
15597 		bnxt_close_nic(bp, true, false);
15598 
15599 	WRITE_ONCE(dev->mtu, new_mtu);
15600 
15601 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
15602 	 * program is attached.  We need to set the AGG rings settings and
15603 	 * rx_skb_func accordingly.
15604 	 */
15605 	if (READ_ONCE(bp->xdp_prog))
15606 		bnxt_set_rx_skb_mode(bp, true);
15607 
15608 	bnxt_set_ring_params(bp);
15609 
15610 	if (netif_running(dev))
15611 		return bnxt_open_nic(bp, true, false);
15612 
15613 	return 0;
15614 }
15615 
15616 void bnxt_set_cp_rings(struct bnxt *bp, bool sh)
15617 {
15618 	int tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
15619 
15620 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
15621 			       tx_cp + bp->rx_nr_rings;
15622 }
15623 
15624 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
15625 {
15626 	struct bnxt *bp = netdev_priv(dev);
15627 	bool sh = false;
15628 	int rc;
15629 
15630 	if (tc > bp->max_tc) {
15631 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
15632 			   tc, bp->max_tc);
15633 		return -EINVAL;
15634 	}
15635 
15636 	if (bp->num_tc == tc)
15637 		return 0;
15638 
15639 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
15640 		sh = true;
15641 
15642 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
15643 			      sh, tc, bp->tx_nr_rings_xdp);
15644 	if (rc)
15645 		return rc;
15646 
15647 	/* Needs to close the device and do hw resource re-allocations */
15648 	if (netif_running(bp->dev))
15649 		bnxt_close_nic(bp, true, false);
15650 
15651 	if (tc) {
15652 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
15653 		netdev_set_num_tc(dev, tc);
15654 		bp->num_tc = tc;
15655 	} else {
15656 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15657 		netdev_reset_tc(dev);
15658 		bp->num_tc = 0;
15659 	}
15660 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
15661 	bnxt_set_cp_rings(bp, sh);
15662 
15663 	if (netif_running(bp->dev))
15664 		return bnxt_open_nic(bp, true, false);
15665 
15666 	return 0;
15667 }
15668 
15669 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
15670 				  void *cb_priv)
15671 {
15672 	struct bnxt *bp = cb_priv;
15673 
15674 	if (!bnxt_tc_flower_enabled(bp) ||
15675 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
15676 		return -EOPNOTSUPP;
15677 
15678 	switch (type) {
15679 	case TC_SETUP_CLSFLOWER:
15680 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
15681 	default:
15682 		return -EOPNOTSUPP;
15683 	}
15684 }
15685 
15686 LIST_HEAD(bnxt_block_cb_list);
15687 
15688 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
15689 			 void *type_data)
15690 {
15691 	struct bnxt *bp = netdev_priv(dev);
15692 
15693 	switch (type) {
15694 	case TC_SETUP_BLOCK:
15695 		return flow_block_cb_setup_simple(type_data,
15696 						  &bnxt_block_cb_list,
15697 						  bnxt_setup_tc_block_cb,
15698 						  bp, bp, true);
15699 	case TC_SETUP_QDISC_MQPRIO: {
15700 		struct tc_mqprio_qopt *mqprio = type_data;
15701 
15702 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
15703 
15704 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
15705 	}
15706 	default:
15707 		return -EOPNOTSUPP;
15708 	}
15709 }
15710 
15711 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
15712 			    const struct sk_buff *skb)
15713 {
15714 	struct bnxt_vnic_info *vnic;
15715 
15716 	if (skb)
15717 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
15718 
15719 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
15720 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
15721 }
15722 
15723 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
15724 			   u32 idx)
15725 {
15726 	struct hlist_head *head;
15727 	int bit_id;
15728 
15729 	spin_lock_bh(&bp->ntp_fltr_lock);
15730 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
15731 	if (bit_id < 0) {
15732 		spin_unlock_bh(&bp->ntp_fltr_lock);
15733 		return -ENOMEM;
15734 	}
15735 
15736 	fltr->base.sw_id = (u16)bit_id;
15737 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
15738 	fltr->base.flags |= BNXT_ACT_RING_DST;
15739 	head = &bp->ntp_fltr_hash_tbl[idx];
15740 	hlist_add_head_rcu(&fltr->base.hash, head);
15741 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
15742 	bnxt_insert_usr_fltr(bp, &fltr->base);
15743 	bp->ntp_fltr_count++;
15744 	spin_unlock_bh(&bp->ntp_fltr_lock);
15745 	return 0;
15746 }
15747 
15748 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
15749 			    struct bnxt_ntuple_filter *f2)
15750 {
15751 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
15752 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
15753 	struct flow_keys *keys1 = &f1->fkeys;
15754 	struct flow_keys *keys2 = &f2->fkeys;
15755 
15756 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
15757 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
15758 		return false;
15759 
15760 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
15761 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
15762 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
15763 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
15764 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
15765 			return false;
15766 	} else {
15767 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
15768 				     &keys2->addrs.v6addrs.src) ||
15769 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
15770 				     &masks2->addrs.v6addrs.src) ||
15771 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15772 				     &keys2->addrs.v6addrs.dst) ||
15773 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
15774 				     &masks2->addrs.v6addrs.dst))
15775 			return false;
15776 	}
15777 
15778 	return keys1->ports.src == keys2->ports.src &&
15779 	       masks1->ports.src == masks2->ports.src &&
15780 	       keys1->ports.dst == keys2->ports.dst &&
15781 	       masks1->ports.dst == masks2->ports.dst &&
15782 	       keys1->control.flags == keys2->control.flags &&
15783 	       f1->l2_fltr == f2->l2_fltr;
15784 }
15785 
15786 struct bnxt_ntuple_filter *
15787 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15788 				struct bnxt_ntuple_filter *fltr, u32 idx)
15789 {
15790 	struct bnxt_ntuple_filter *f;
15791 	struct hlist_head *head;
15792 
15793 	head = &bp->ntp_fltr_hash_tbl[idx];
15794 	hlist_for_each_entry_rcu(f, head, base.hash) {
15795 		if (bnxt_fltr_match(f, fltr))
15796 			return f;
15797 	}
15798 	return NULL;
15799 }
15800 
15801 #ifdef CONFIG_RFS_ACCEL
15802 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15803 			      u16 rxq_index, u32 flow_id)
15804 {
15805 	struct bnxt *bp = netdev_priv(dev);
15806 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15807 	struct flow_keys *fkeys;
15808 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15809 	struct bnxt_l2_filter *l2_fltr;
15810 	int rc = 0, idx;
15811 	u32 flags;
15812 
15813 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15814 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15815 		atomic_inc(&l2_fltr->refcnt);
15816 	} else {
15817 		struct bnxt_l2_key key;
15818 
15819 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15820 		key.vlan = 0;
15821 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15822 		if (!l2_fltr)
15823 			return -EINVAL;
15824 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15825 			bnxt_del_l2_filter(bp, l2_fltr);
15826 			return -EINVAL;
15827 		}
15828 	}
15829 	new_fltr = kzalloc_obj(*new_fltr, GFP_ATOMIC);
15830 	if (!new_fltr) {
15831 		bnxt_del_l2_filter(bp, l2_fltr);
15832 		return -ENOMEM;
15833 	}
15834 
15835 	fkeys = &new_fltr->fkeys;
15836 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15837 		rc = -EPROTONOSUPPORT;
15838 		goto err_free;
15839 	}
15840 
15841 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15842 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15843 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15844 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15845 		rc = -EPROTONOSUPPORT;
15846 		goto err_free;
15847 	}
15848 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15849 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15850 		if (bp->hwrm_spec_code < 0x10601) {
15851 			rc = -EPROTONOSUPPORT;
15852 			goto err_free;
15853 		}
15854 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15855 	}
15856 	flags = fkeys->control.flags;
15857 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15858 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15859 		rc = -EPROTONOSUPPORT;
15860 		goto err_free;
15861 	}
15862 	new_fltr->l2_fltr = l2_fltr;
15863 
15864 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15865 	rcu_read_lock();
15866 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15867 	if (fltr) {
15868 		rc = fltr->base.sw_id;
15869 		rcu_read_unlock();
15870 		goto err_free;
15871 	}
15872 	rcu_read_unlock();
15873 
15874 	new_fltr->flow_id = flow_id;
15875 	new_fltr->base.rxq = rxq_index;
15876 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15877 	if (!rc) {
15878 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15879 		return new_fltr->base.sw_id;
15880 	}
15881 
15882 err_free:
15883 	bnxt_del_l2_filter(bp, l2_fltr);
15884 	kfree(new_fltr);
15885 	return rc;
15886 }
15887 #endif
15888 
15889 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
15890 {
15891 	spin_lock_bh(&bp->ntp_fltr_lock);
15892 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
15893 		spin_unlock_bh(&bp->ntp_fltr_lock);
15894 		return;
15895 	}
15896 	hlist_del_rcu(&fltr->base.hash);
15897 	bnxt_del_one_usr_fltr(bp, &fltr->base);
15898 	bp->ntp_fltr_count--;
15899 	spin_unlock_bh(&bp->ntp_fltr_lock);
15900 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
15901 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
15902 	kfree_rcu(fltr, base.rcu);
15903 }
15904 
15905 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
15906 {
15907 #ifdef CONFIG_RFS_ACCEL
15908 	int i;
15909 
15910 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
15911 		struct hlist_head *head;
15912 		struct hlist_node *tmp;
15913 		struct bnxt_ntuple_filter *fltr;
15914 		int rc;
15915 
15916 		head = &bp->ntp_fltr_hash_tbl[i];
15917 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
15918 			bool del = false;
15919 
15920 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
15921 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
15922 					continue;
15923 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
15924 							fltr->flow_id,
15925 							fltr->base.sw_id)) {
15926 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
15927 									 fltr);
15928 					del = true;
15929 				}
15930 			} else {
15931 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
15932 								       fltr);
15933 				if (rc)
15934 					del = true;
15935 				else
15936 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
15937 			}
15938 
15939 			if (del)
15940 				bnxt_del_ntp_filter(bp, fltr);
15941 		}
15942 	}
15943 #endif
15944 }
15945 
15946 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
15947 				    unsigned int entry, struct udp_tunnel_info *ti)
15948 {
15949 	struct bnxt *bp = netdev_priv(netdev);
15950 	unsigned int cmd;
15951 
15952 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15953 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
15954 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15955 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
15956 	else
15957 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
15958 
15959 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
15960 }
15961 
15962 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
15963 				      unsigned int entry, struct udp_tunnel_info *ti)
15964 {
15965 	struct bnxt *bp = netdev_priv(netdev);
15966 	unsigned int cmd;
15967 
15968 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15969 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
15970 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15971 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
15972 	else
15973 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
15974 
15975 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
15976 }
15977 
15978 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
15979 	.set_port	= bnxt_udp_tunnel_set_port,
15980 	.unset_port	= bnxt_udp_tunnel_unset_port,
15981 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15982 	.tables		= {
15983 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15984 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15985 	},
15986 }, bnxt_udp_tunnels_p7 = {
15987 	.set_port	= bnxt_udp_tunnel_set_port,
15988 	.unset_port	= bnxt_udp_tunnel_unset_port,
15989 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15990 	.tables		= {
15991 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15992 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15993 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
15994 	},
15995 };
15996 
15997 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
15998 			       struct net_device *dev, u32 filter_mask,
15999 			       int nlflags)
16000 {
16001 	struct bnxt *bp = netdev_priv(dev);
16002 
16003 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
16004 				       nlflags, filter_mask, NULL);
16005 }
16006 
16007 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
16008 			       u16 flags, struct netlink_ext_ack *extack)
16009 {
16010 	struct bnxt *bp = netdev_priv(dev);
16011 	struct nlattr *attr, *br_spec;
16012 	int rem, rc = 0;
16013 
16014 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
16015 		return -EOPNOTSUPP;
16016 
16017 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
16018 	if (!br_spec)
16019 		return -EINVAL;
16020 
16021 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
16022 		u16 mode;
16023 
16024 		mode = nla_get_u16(attr);
16025 		if (mode == bp->br_mode)
16026 			break;
16027 
16028 		rc = bnxt_hwrm_set_br_mode(bp, mode);
16029 		if (!rc)
16030 			bp->br_mode = mode;
16031 		break;
16032 	}
16033 	return rc;
16034 }
16035 
16036 int bnxt_get_port_parent_id(struct net_device *dev,
16037 			    struct netdev_phys_item_id *ppid)
16038 {
16039 	struct bnxt *bp = netdev_priv(dev);
16040 
16041 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
16042 		return -EOPNOTSUPP;
16043 
16044 	/* The PF and it's VF-reps only support the switchdev framework */
16045 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
16046 		return -EOPNOTSUPP;
16047 
16048 	ppid->id_len = sizeof(bp->dsn);
16049 	memcpy(ppid->id, bp->dsn, ppid->id_len);
16050 
16051 	return 0;
16052 }
16053 
16054 static const struct net_device_ops bnxt_netdev_ops = {
16055 	.ndo_open		= bnxt_open,
16056 	.ndo_start_xmit		= bnxt_start_xmit,
16057 	.ndo_stop		= bnxt_close,
16058 	.ndo_get_stats64	= bnxt_get_stats64,
16059 	.ndo_set_rx_mode_async	= bnxt_set_rx_mode,
16060 	.ndo_eth_ioctl		= bnxt_ioctl,
16061 	.ndo_validate_addr	= eth_validate_addr,
16062 	.ndo_set_mac_address	= bnxt_change_mac_addr,
16063 	.ndo_change_mtu		= bnxt_change_mtu,
16064 	.ndo_fix_features	= bnxt_fix_features,
16065 	.ndo_set_features	= bnxt_set_features,
16066 	.ndo_features_check	= bnxt_features_check,
16067 	.ndo_tx_timeout		= bnxt_tx_timeout,
16068 #ifdef CONFIG_BNXT_SRIOV
16069 	.ndo_get_vf_config	= bnxt_get_vf_config,
16070 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
16071 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
16072 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
16073 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
16074 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
16075 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
16076 #endif
16077 	.ndo_setup_tc           = bnxt_setup_tc,
16078 #ifdef CONFIG_RFS_ACCEL
16079 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
16080 #endif
16081 	.ndo_bpf		= bnxt_xdp,
16082 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
16083 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
16084 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
16085 	.ndo_hwtstamp_get	= bnxt_hwtstamp_get,
16086 	.ndo_hwtstamp_set	= bnxt_hwtstamp_set,
16087 };
16088 
16089 static const struct xdp_metadata_ops bnxt_xdp_metadata_ops = {
16090 	.xmo_rx_hash		= bnxt_xdp_rx_hash,
16091 };
16092 
16093 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
16094 				    struct netdev_queue_stats_rx *stats)
16095 {
16096 	struct bnxt *bp = netdev_priv(dev);
16097 	struct bnxt_cp_ring_info *cpr;
16098 	u64 *sw;
16099 
16100 	if (!bp->bnapi)
16101 		return;
16102 
16103 	bnxt_sync_ring_stats(bp);
16104 	cpr = &bp->bnapi[i]->cp_ring;
16105 	sw = cpr->stats.sw_stats;
16106 
16107 	stats->packets = 0;
16108 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
16109 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
16110 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
16111 
16112 	stats->bytes = 0;
16113 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
16114 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
16115 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
16116 
16117 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
16118 	stats->hw_gro_packets = cpr->sw_stats->rx.rx_hw_gro_packets;
16119 	stats->hw_gro_wire_packets = cpr->sw_stats->rx.rx_hw_gro_wire_packets;
16120 }
16121 
16122 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
16123 				    struct netdev_queue_stats_tx *stats)
16124 {
16125 	struct bnxt *bp = netdev_priv(dev);
16126 	struct bnxt_napi *bnapi;
16127 	u64 *sw;
16128 
16129 	if (!bp->tx_ring)
16130 		return;
16131 
16132 	bnxt_sync_ring_stats(bp);
16133 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
16134 	sw = bnapi->cp_ring.stats.sw_stats;
16135 
16136 	stats->packets = 0;
16137 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
16138 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
16139 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
16140 
16141 	stats->bytes = 0;
16142 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
16143 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
16144 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
16145 }
16146 
16147 static void bnxt_get_base_stats(struct net_device *dev,
16148 				struct netdev_queue_stats_rx *rx,
16149 				struct netdev_queue_stats_tx *tx)
16150 {
16151 	struct bnxt *bp = netdev_priv(dev);
16152 
16153 	rx->packets = bp->net_stats_prev.rx_packets;
16154 	rx->bytes = bp->net_stats_prev.rx_bytes;
16155 	rx->alloc_fail = bp->ring_drv_stats_prev.rx_total_oom_discards;
16156 	rx->hw_gro_packets = bp->ring_drv_stats_prev.rx_total_hw_gro_packets;
16157 	rx->hw_gro_wire_packets = bp->ring_drv_stats_prev.rx_total_hw_gro_wire_packets;
16158 
16159 	tx->packets = bp->net_stats_prev.tx_packets;
16160 	tx->bytes = bp->net_stats_prev.tx_bytes;
16161 }
16162 
16163 static const struct netdev_stat_ops bnxt_stat_ops = {
16164 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
16165 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
16166 	.get_base_stats		= bnxt_get_base_stats,
16167 };
16168 
16169 static void bnxt_queue_default_qcfg(struct net_device *dev,
16170 				    struct netdev_queue_config *qcfg)
16171 {
16172 	qcfg->rx_page_size = BNXT_RX_PAGE_SIZE;
16173 }
16174 
16175 static int bnxt_validate_qcfg(struct net_device *dev,
16176 			      struct netdev_queue_config *qcfg,
16177 			      struct netlink_ext_ack *extack)
16178 {
16179 	struct bnxt *bp = netdev_priv(dev);
16180 
16181 	/* Older chips need MSS calc so rx_page_size is not supported */
16182 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16183 	    qcfg->rx_page_size != BNXT_RX_PAGE_SIZE)
16184 		return -EINVAL;
16185 
16186 	if (!is_power_of_2(qcfg->rx_page_size))
16187 		return -ERANGE;
16188 
16189 	if (qcfg->rx_page_size < BNXT_RX_PAGE_SIZE ||
16190 	    qcfg->rx_page_size > BNXT_MAX_RX_PAGE_SIZE)
16191 		return -ERANGE;
16192 
16193 	return 0;
16194 }
16195 
16196 static int bnxt_queue_mem_alloc(struct net_device *dev,
16197 				struct netdev_queue_config *qcfg,
16198 				void *qmem, int idx)
16199 {
16200 	struct bnxt_rx_ring_info *rxr, *clone;
16201 	struct bnxt *bp = netdev_priv(dev);
16202 	struct bnxt_ring_struct *ring;
16203 	int rc;
16204 
16205 	if (!bp->rx_ring)
16206 		return -ENETDOWN;
16207 
16208 	rxr = &bp->rx_ring[idx];
16209 	clone = qmem;
16210 	memcpy(clone, rxr, sizeof(*rxr));
16211 	bnxt_init_rx_ring_struct(bp, clone);
16212 	bnxt_reset_rx_ring_struct(bp, clone);
16213 
16214 	clone->rx_prod = 0;
16215 	clone->rx_agg_prod = 0;
16216 	clone->rx_sw_agg_prod = 0;
16217 	clone->rx_next_cons = 0;
16218 	clone->need_head_pool = false;
16219 	clone->rx_page_size = qcfg->rx_page_size;
16220 
16221 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
16222 	if (rc)
16223 		return rc;
16224 
16225 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
16226 	if (rc < 0)
16227 		goto err_page_pool_destroy;
16228 
16229 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
16230 					MEM_TYPE_PAGE_POOL,
16231 					clone->page_pool);
16232 	if (rc)
16233 		goto err_rxq_info_unreg;
16234 
16235 	ring = &clone->rx_ring_struct;
16236 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
16237 	if (rc)
16238 		goto err_free_rx_ring;
16239 
16240 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
16241 		ring = &clone->rx_agg_ring_struct;
16242 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
16243 		if (rc)
16244 			goto err_free_rx_agg_ring;
16245 
16246 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
16247 		if (rc)
16248 			goto err_free_rx_agg_ring;
16249 	}
16250 
16251 	if (bp->flags & BNXT_FLAG_TPA) {
16252 		rc = bnxt_alloc_one_tpa_info(bp, clone);
16253 		if (rc)
16254 			goto err_free_tpa_info;
16255 	}
16256 
16257 	bnxt_init_one_rx_ring_rxbd(bp, clone);
16258 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
16259 
16260 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
16261 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16262 		bnxt_alloc_one_rx_ring_netmem(bp, clone, idx);
16263 	if (bp->flags & BNXT_FLAG_TPA)
16264 		bnxt_alloc_one_tpa_info_data(bp, clone);
16265 
16266 	return 0;
16267 
16268 err_free_tpa_info:
16269 	bnxt_free_one_tpa_info(bp, clone);
16270 err_free_rx_agg_ring:
16271 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
16272 err_free_rx_ring:
16273 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
16274 err_rxq_info_unreg:
16275 	xdp_rxq_info_unreg(&clone->xdp_rxq);
16276 err_page_pool_destroy:
16277 	page_pool_destroy(clone->page_pool);
16278 	page_pool_destroy(clone->head_pool);
16279 	clone->page_pool = NULL;
16280 	clone->head_pool = NULL;
16281 	return rc;
16282 }
16283 
16284 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
16285 {
16286 	struct bnxt_rx_ring_info *rxr = qmem;
16287 	struct bnxt *bp = netdev_priv(dev);
16288 	struct bnxt_ring_struct *ring;
16289 
16290 	bnxt_free_one_rx_ring_skbs(bp, rxr);
16291 	bnxt_free_one_tpa_info(bp, rxr);
16292 
16293 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
16294 
16295 	page_pool_destroy(rxr->page_pool);
16296 	page_pool_destroy(rxr->head_pool);
16297 	rxr->page_pool = NULL;
16298 	rxr->head_pool = NULL;
16299 
16300 	ring = &rxr->rx_ring_struct;
16301 	bnxt_free_ring(bp, &ring->ring_mem);
16302 
16303 	ring = &rxr->rx_agg_ring_struct;
16304 	bnxt_free_ring(bp, &ring->ring_mem);
16305 
16306 	kfree(rxr->rx_agg_bmap);
16307 	rxr->rx_agg_bmap = NULL;
16308 }
16309 
16310 static void bnxt_copy_rx_ring(struct bnxt *bp,
16311 			      struct bnxt_rx_ring_info *dst,
16312 			      struct bnxt_rx_ring_info *src)
16313 {
16314 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
16315 	struct bnxt_ring_struct *dst_ring, *src_ring;
16316 	int i;
16317 
16318 	dst_ring = &dst->rx_ring_struct;
16319 	dst_rmem = &dst_ring->ring_mem;
16320 	src_ring = &src->rx_ring_struct;
16321 	src_rmem = &src_ring->ring_mem;
16322 
16323 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
16324 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
16325 	WARN_ON(dst_rmem->flags != src_rmem->flags);
16326 	WARN_ON(dst_rmem->depth != src_rmem->depth);
16327 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
16328 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
16329 
16330 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
16331 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
16332 	*dst_rmem->vmem = *src_rmem->vmem;
16333 	for (i = 0; i < dst_rmem->nr_pages; i++) {
16334 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
16335 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
16336 	}
16337 
16338 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
16339 		return;
16340 
16341 	dst_ring = &dst->rx_agg_ring_struct;
16342 	dst_rmem = &dst_ring->ring_mem;
16343 	src_ring = &src->rx_agg_ring_struct;
16344 	src_rmem = &src_ring->ring_mem;
16345 
16346 	dst->rx_page_size = src->rx_page_size;
16347 
16348 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
16349 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
16350 	WARN_ON(dst_rmem->flags != src_rmem->flags);
16351 	WARN_ON(dst_rmem->depth != src_rmem->depth);
16352 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
16353 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
16354 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
16355 
16356 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
16357 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
16358 	*dst_rmem->vmem = *src_rmem->vmem;
16359 	for (i = 0; i < dst_rmem->nr_pages; i++) {
16360 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
16361 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
16362 	}
16363 
16364 	dst->rx_agg_bmap = src->rx_agg_bmap;
16365 }
16366 
16367 static int bnxt_queue_start(struct net_device *dev,
16368 			    struct netdev_queue_config *qcfg,
16369 			    void *qmem, int idx)
16370 {
16371 	struct bnxt *bp = netdev_priv(dev);
16372 	struct bnxt_rx_ring_info *rxr, *clone;
16373 	struct bnxt_cp_ring_info *cpr;
16374 	struct bnxt_vnic_info *vnic;
16375 	struct bnxt_napi *bnapi;
16376 	int i, rc;
16377 	u16 mru;
16378 
16379 	rxr = &bp->rx_ring[idx];
16380 	clone = qmem;
16381 
16382 	rxr->rx_prod = clone->rx_prod;
16383 	rxr->rx_agg_prod = clone->rx_agg_prod;
16384 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
16385 	rxr->rx_next_cons = clone->rx_next_cons;
16386 	rxr->rx_tpa = clone->rx_tpa;
16387 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
16388 	rxr->page_pool = clone->page_pool;
16389 	rxr->head_pool = clone->head_pool;
16390 	rxr->xdp_rxq = clone->xdp_rxq;
16391 	rxr->need_head_pool = clone->need_head_pool;
16392 
16393 	bnxt_copy_rx_ring(bp, rxr, clone);
16394 
16395 	bnapi = rxr->bnapi;
16396 	cpr = &bnapi->cp_ring;
16397 
16398 	/* All rings have been reserved and previously allocated.
16399 	 * Reallocating with the same parameters should never fail.
16400 	 */
16401 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
16402 	if (rc)
16403 		goto err_reset;
16404 
16405 	if (bp->tph_mode) {
16406 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
16407 		if (rc)
16408 			goto err_reset;
16409 	}
16410 
16411 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
16412 	if (rc)
16413 		goto err_reset;
16414 
16415 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
16416 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16417 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
16418 
16419 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
16420 		rc = bnxt_tx_queue_start(bp, idx);
16421 		if (rc)
16422 			goto err_reset;
16423 	}
16424 
16425 	bnxt_enable_rx_page_pool(rxr);
16426 	napi_enable_locked(&bnapi->napi);
16427 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16428 
16429 	mru = bp->dev->mtu + VLAN_ETH_HLEN;
16430 	for (i = 0; i < bp->nr_vnics; i++) {
16431 		vnic = &bp->vnic_info[i];
16432 
16433 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, idx);
16434 		if (rc)
16435 			return rc;
16436 	}
16437 	return bnxt_set_rss_ctx_vnic_mru(bp, mru, idx);
16438 
16439 err_reset:
16440 	netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n",
16441 		   rc);
16442 	napi_enable_locked(&bnapi->napi);
16443 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16444 	netif_close(dev);
16445 	return rc;
16446 }
16447 
16448 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
16449 {
16450 	struct bnxt *bp = netdev_priv(dev);
16451 	struct bnxt_rx_ring_info *rxr;
16452 	struct bnxt_cp_ring_info *cpr;
16453 	struct bnxt_vnic_info *vnic;
16454 	struct bnxt_napi *bnapi;
16455 	int i;
16456 
16457 	for (i = 0; i < bp->nr_vnics; i++) {
16458 		vnic = &bp->vnic_info[i];
16459 
16460 		bnxt_set_vnic_mru_p5(bp, vnic, 0, idx);
16461 	}
16462 	bnxt_set_rss_ctx_vnic_mru(bp, 0, idx);
16463 	/* Make sure NAPI sees that the VNIC is disabled */
16464 	synchronize_net();
16465 	rxr = &bp->rx_ring[idx];
16466 	bnapi = rxr->bnapi;
16467 	cpr = &bnapi->cp_ring;
16468 	cancel_work_sync(&cpr->dim.work);
16469 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
16470 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
16471 	page_pool_disable_direct_recycling(rxr->page_pool);
16472 	if (bnxt_separate_head_pool(rxr))
16473 		page_pool_disable_direct_recycling(rxr->head_pool);
16474 
16475 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
16476 		bnxt_tx_queue_stop(bp, idx);
16477 
16478 	/* Disable NAPI now after freeing the rings because HWRM_RING_FREE
16479 	 * completion is handled in NAPI to guarantee no more DMA on that ring
16480 	 * after seeing the completion.
16481 	 */
16482 	napi_disable_locked(&bnapi->napi);
16483 
16484 	if (bp->tph_mode) {
16485 		bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr);
16486 		bnxt_clear_one_cp_ring(bp, rxr->rx_cpr);
16487 	}
16488 	bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
16489 
16490 	memcpy(qmem, rxr, sizeof(*rxr));
16491 	bnxt_init_rx_ring_struct(bp, qmem);
16492 
16493 	return 0;
16494 }
16495 
16496 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
16497 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
16498 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
16499 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
16500 	.ndo_queue_start	= bnxt_queue_start,
16501 	.ndo_queue_stop		= bnxt_queue_stop,
16502 	.ndo_default_qcfg	= bnxt_queue_default_qcfg,
16503 	.ndo_validate_qcfg	= bnxt_validate_qcfg,
16504 	.supported_params	= QCFG_RX_PAGE_SIZE,
16505 };
16506 
16507 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops_unsupp = {
16508 	.ndo_default_qcfg	= bnxt_queue_default_qcfg,
16509 };
16510 
16511 static void bnxt_remove_one(struct pci_dev *pdev)
16512 {
16513 	struct net_device *dev = pci_get_drvdata(pdev);
16514 	struct bnxt *bp = netdev_priv(dev);
16515 
16516 	if (BNXT_PF(bp))
16517 		__bnxt_sriov_disable(bp);
16518 
16519 	bnxt_aux_devices_del(bp);
16520 
16521 	unregister_netdev(dev);
16522 	bnxt_ptp_clear(bp);
16523 
16524 	bnxt_aux_devices_uninit(bp);
16525 	bnxt_auxdev_id_free(bp, bp->auxdev_id);
16526 
16527 	bnxt_free_l2_filters(bp, true);
16528 	bnxt_free_ntp_fltrs(bp, true);
16529 	WARN_ON(bp->num_rss_ctx);
16530 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16531 	/* Flush any pending tasks */
16532 	cancel_work_sync(&bp->sp_task);
16533 	cancel_delayed_work_sync(&bp->fw_reset_task);
16534 	bp->sp_event = 0;
16535 
16536 	bnxt_dl_fw_reporters_destroy(bp);
16537 	bnxt_dl_unregister(bp);
16538 	bnxt_shutdown_tc(bp);
16539 
16540 	bnxt_clear_int_mode(bp);
16541 	bnxt_hwrm_func_drv_unrgtr(bp);
16542 	bnxt_free_hwrm_resources(bp);
16543 	bnxt_hwmon_uninit(bp);
16544 	bnxt_ethtool_free(bp);
16545 	bnxt_dcb_free(bp);
16546 	kfree(bp->ptp_cfg);
16547 	bp->ptp_cfg = NULL;
16548 	kfree(bp->fw_health);
16549 	bp->fw_health = NULL;
16550 	bnxt_cleanup_pci(bp);
16551 	bnxt_free_ctx_mem(bp, true);
16552 	bnxt_free_crash_dump_mem(bp);
16553 	kfree(bp->rss_indir_tbl);
16554 	bp->rss_indir_tbl = NULL;
16555 	bnxt_free_port_stats(bp);
16556 	free_netdev(dev);
16557 }
16558 
16559 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
16560 {
16561 	int rc = 0;
16562 	struct bnxt_link_info *link_info = &bp->link_info;
16563 
16564 	bp->phy_flags = 0;
16565 	rc = bnxt_hwrm_phy_qcaps(bp);
16566 	if (rc) {
16567 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
16568 			   rc);
16569 		return rc;
16570 	}
16571 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
16572 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
16573 	else
16574 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
16575 
16576 	bp->mac_flags = 0;
16577 	bnxt_hwrm_mac_qcaps(bp);
16578 
16579 	if (!fw_dflt)
16580 		return 0;
16581 
16582 	mutex_lock(&bp->link_lock);
16583 	rc = bnxt_update_link(bp, false);
16584 	if (rc) {
16585 		mutex_unlock(&bp->link_lock);
16586 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
16587 			   rc);
16588 		return rc;
16589 	}
16590 
16591 	/* Older firmware does not have supported_auto_speeds, so assume
16592 	 * that all supported speeds can be autonegotiated.
16593 	 */
16594 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
16595 		link_info->support_auto_speeds = link_info->support_speeds;
16596 
16597 	bnxt_init_ethtool_link_settings(bp);
16598 	mutex_unlock(&bp->link_lock);
16599 	return 0;
16600 }
16601 
16602 static int bnxt_get_max_irq(struct pci_dev *pdev)
16603 {
16604 	u16 ctrl;
16605 
16606 	if (!pdev->msix_cap)
16607 		return 1;
16608 
16609 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
16610 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
16611 }
16612 
16613 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16614 				int *max_cp)
16615 {
16616 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
16617 	int max_ring_grps = 0, max_irq;
16618 
16619 	*max_tx = hw_resc->max_tx_rings;
16620 	*max_rx = hw_resc->max_rx_rings;
16621 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
16622 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
16623 			bnxt_get_ulp_msix_num_in_use(bp),
16624 			hw_resc->max_stat_ctxs -
16625 			bnxt_get_ulp_stat_ctxs_in_use(bp));
16626 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
16627 		*max_cp = min_t(int, *max_cp, max_irq);
16628 	max_ring_grps = hw_resc->max_hw_ring_grps;
16629 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
16630 		*max_cp -= 1;
16631 		*max_rx -= 2;
16632 	}
16633 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16634 		*max_rx >>= 1;
16635 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
16636 		int rc;
16637 
16638 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
16639 		if (rc) {
16640 			*max_rx = 0;
16641 			*max_tx = 0;
16642 		}
16643 		/* On P5 chips, max_cp output param should be available NQs */
16644 		*max_cp = max_irq;
16645 	}
16646 	*max_rx = min_t(int, *max_rx, max_ring_grps);
16647 }
16648 
16649 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
16650 {
16651 	int rx, tx, cp;
16652 
16653 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
16654 	*max_rx = rx;
16655 	*max_tx = tx;
16656 	if (!rx || !tx || !cp)
16657 		return -ENOMEM;
16658 
16659 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
16660 }
16661 
16662 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16663 			       bool shared)
16664 {
16665 	int rc;
16666 
16667 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16668 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
16669 		/* Not enough rings, try disabling agg rings. */
16670 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
16671 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16672 		if (rc) {
16673 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
16674 			bp->flags |= BNXT_FLAG_AGG_RINGS;
16675 			return rc;
16676 		}
16677 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
16678 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16679 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16680 		bnxt_set_ring_params(bp);
16681 	}
16682 
16683 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
16684 		int max_cp, max_stat, max_irq;
16685 
16686 		/* Reserve minimum resources for RoCE */
16687 		max_cp = bnxt_get_max_func_cp_rings(bp);
16688 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
16689 		max_irq = bnxt_get_max_func_irqs(bp);
16690 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
16691 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
16692 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
16693 			return 0;
16694 
16695 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
16696 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
16697 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
16698 		max_cp = min_t(int, max_cp, max_irq);
16699 		max_cp = min_t(int, max_cp, max_stat);
16700 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
16701 		if (rc)
16702 			rc = 0;
16703 	}
16704 	return rc;
16705 }
16706 
16707 /* In initial default shared ring setting, each shared ring must have a
16708  * RX/TX ring pair.
16709  */
16710 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
16711 {
16712 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
16713 	bp->rx_nr_rings = bp->cp_nr_rings;
16714 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
16715 	bp->tx_nr_rings = bnxt_tx_nr_rings(bp);
16716 }
16717 
16718 static void bnxt_adj_dflt_rings(struct bnxt *bp, bool sh)
16719 {
16720 	if (sh)
16721 		bnxt_trim_dflt_sh_rings(bp);
16722 	else
16723 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
16724 	bp->tx_nr_rings = bnxt_tx_nr_rings(bp);
16725 	if (sh && READ_ONCE(bp->xdp_prog)) {
16726 		bnxt_set_xdp_tx_rings(bp);
16727 		bnxt_set_cp_rings(bp, true);
16728 	}
16729 }
16730 
16731 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
16732 {
16733 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
16734 	int avail_msix;
16735 
16736 	if (!bnxt_can_reserve_rings(bp))
16737 		return 0;
16738 
16739 	if (sh)
16740 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
16741 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
16742 	/* Reduce default rings on multi-port cards so that total default
16743 	 * rings do not exceed CPU count.
16744 	 */
16745 	if (bp->port_count > 1) {
16746 		int max_rings =
16747 			max_t(int, num_online_cpus() / bp->port_count, 1);
16748 
16749 		dflt_rings = min_t(int, dflt_rings, max_rings);
16750 	}
16751 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
16752 	if (rc)
16753 		return rc;
16754 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
16755 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
16756 
16757 	bnxt_adj_dflt_rings(bp, sh);
16758 
16759 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
16760 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
16761 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
16762 
16763 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
16764 		bnxt_set_dflt_ulp_stat_ctxs(bp);
16765 	}
16766 
16767 	rc = __bnxt_reserve_rings(bp);
16768 	if (rc && rc != -ENODEV)
16769 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
16770 
16771 	bnxt_adj_tx_rings(bp);
16772 	if (sh)
16773 		bnxt_adj_dflt_rings(bp, true);
16774 
16775 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
16776 	if (bnxt_need_reserve_rings(bp)) {
16777 		rc = __bnxt_reserve_rings(bp);
16778 		if (rc && rc != -ENODEV)
16779 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
16780 		bnxt_adj_tx_rings(bp);
16781 	}
16782 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
16783 		bp->rx_nr_rings++;
16784 		bp->cp_nr_rings++;
16785 	}
16786 	if (rc) {
16787 		bp->tx_nr_rings = 0;
16788 		bp->rx_nr_rings = 0;
16789 	}
16790 	return rc;
16791 }
16792 
16793 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
16794 {
16795 	int rc;
16796 
16797 	if (bp->tx_nr_rings)
16798 		return 0;
16799 
16800 	bnxt_ulp_irq_stop(bp);
16801 	bnxt_clear_int_mode(bp);
16802 	rc = bnxt_set_dflt_rings(bp, true);
16803 	if (rc) {
16804 		if (BNXT_VF(bp) && rc == -ENODEV)
16805 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16806 		else
16807 			netdev_err(bp->dev, "Not enough rings available.\n");
16808 		goto init_dflt_ring_err;
16809 	}
16810 	rc = bnxt_init_int_mode(bp);
16811 	if (rc)
16812 		goto init_dflt_ring_err;
16813 
16814 	bnxt_adj_tx_rings(bp);
16815 
16816 	bnxt_set_dflt_rfs(bp);
16817 
16818 init_dflt_ring_err:
16819 	bnxt_ulp_irq_restart(bp, rc);
16820 	return rc;
16821 }
16822 
16823 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
16824 {
16825 	int rc;
16826 
16827 	netdev_assert_locked_ops_compat(bp->dev);
16828 	bnxt_hwrm_func_qcaps(bp);
16829 
16830 	if (netif_running(bp->dev))
16831 		__bnxt_close_nic(bp, true, false);
16832 
16833 	bnxt_ulp_irq_stop(bp);
16834 	bnxt_clear_int_mode(bp);
16835 	rc = bnxt_init_int_mode(bp);
16836 	bnxt_ulp_irq_restart(bp, rc);
16837 
16838 	if (netif_running(bp->dev)) {
16839 		if (rc)
16840 			netif_close(bp->dev);
16841 		else
16842 			rc = bnxt_open_nic(bp, true, false);
16843 	}
16844 
16845 	return rc;
16846 }
16847 
16848 static int bnxt_init_mac_addr(struct bnxt *bp)
16849 {
16850 	int rc = 0;
16851 
16852 	if (BNXT_PF(bp)) {
16853 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
16854 	} else {
16855 #ifdef CONFIG_BNXT_SRIOV
16856 		struct bnxt_vf_info *vf = &bp->vf;
16857 		bool strict_approval = true;
16858 
16859 		if (is_valid_ether_addr(vf->mac_addr)) {
16860 			/* overwrite netdev dev_addr with admin VF MAC */
16861 			eth_hw_addr_set(bp->dev, vf->mac_addr);
16862 			/* Older PF driver or firmware may not approve this
16863 			 * correctly.
16864 			 */
16865 			strict_approval = false;
16866 		} else {
16867 			eth_hw_addr_random(bp->dev);
16868 		}
16869 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
16870 #endif
16871 	}
16872 	return rc;
16873 }
16874 
16875 static void bnxt_vpd_read_info(struct bnxt *bp)
16876 {
16877 	struct pci_dev *pdev = bp->pdev;
16878 	unsigned int vpd_size, kw_len;
16879 	int pos, size;
16880 	u8 *vpd_data;
16881 
16882 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
16883 	if (IS_ERR(vpd_data)) {
16884 		pci_warn(pdev, "Unable to read VPD\n");
16885 		return;
16886 	}
16887 
16888 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16889 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
16890 	if (pos < 0)
16891 		goto read_sn;
16892 
16893 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16894 	memcpy(bp->board_partno, &vpd_data[pos], size);
16895 
16896 read_sn:
16897 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16898 					   PCI_VPD_RO_KEYWORD_SERIALNO,
16899 					   &kw_len);
16900 	if (pos < 0)
16901 		goto exit;
16902 
16903 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16904 	memcpy(bp->board_serialno, &vpd_data[pos], size);
16905 exit:
16906 	kfree(vpd_data);
16907 }
16908 
16909 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
16910 {
16911 	struct pci_dev *pdev = bp->pdev;
16912 	u64 qword;
16913 
16914 	qword = pci_get_dsn(pdev);
16915 	if (!qword) {
16916 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
16917 		return -EOPNOTSUPP;
16918 	}
16919 
16920 	put_unaligned_le64(qword, dsn);
16921 
16922 	bp->flags |= BNXT_FLAG_DSN_VALID;
16923 	return 0;
16924 }
16925 
16926 static int bnxt_map_db_bar(struct bnxt *bp)
16927 {
16928 	if (!bp->db_size)
16929 		return -ENODEV;
16930 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
16931 	if (!bp->bar1)
16932 		return -ENOMEM;
16933 	return 0;
16934 }
16935 
16936 void bnxt_print_device_info(struct bnxt *bp)
16937 {
16938 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
16939 		    board_info[bp->board_idx].name,
16940 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
16941 
16942 	pcie_print_link_status(bp->pdev);
16943 }
16944 
16945 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
16946 {
16947 	struct bnxt_hw_resc *hw_resc;
16948 	struct net_device *dev;
16949 	struct bnxt *bp;
16950 	int rc, max_irqs;
16951 
16952 	if (pci_is_bridge(pdev))
16953 		return -ENODEV;
16954 
16955 	if (!pdev->msix_cap) {
16956 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
16957 		return -ENODEV;
16958 	}
16959 
16960 	/* Clear any pending DMA transactions from crash kernel
16961 	 * while loading driver in capture kernel.
16962 	 */
16963 	if (is_kdump_kernel()) {
16964 		pci_clear_master(pdev);
16965 		pcie_flr(pdev);
16966 	}
16967 
16968 	max_irqs = bnxt_get_max_irq(pdev);
16969 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
16970 				 max_irqs);
16971 	if (!dev)
16972 		return -ENOMEM;
16973 
16974 	bp = netdev_priv(dev);
16975 	bp->board_idx = ent->driver_data;
16976 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
16977 	bnxt_set_max_func_irqs(bp, max_irqs);
16978 
16979 	if (bnxt_vf_pciid(bp->board_idx))
16980 		bp->flags |= BNXT_FLAG_VF;
16981 
16982 	/* No devlink port registration in case of a VF */
16983 	if (BNXT_PF(bp))
16984 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
16985 
16986 	rc = bnxt_init_board(pdev, dev);
16987 	if (rc < 0)
16988 		goto init_err_free;
16989 
16990 	dev->netdev_ops = &bnxt_netdev_ops;
16991 	dev->xdp_metadata_ops = &bnxt_xdp_metadata_ops;
16992 	dev->stat_ops = &bnxt_stat_ops;
16993 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
16994 	dev->ethtool_ops = &bnxt_ethtool_ops;
16995 	pci_set_drvdata(pdev, dev);
16996 
16997 	rc = bnxt_alloc_hwrm_resources(bp);
16998 	if (rc)
16999 		goto init_err_pci_clean;
17000 
17001 	mutex_init(&bp->hwrm_cmd_lock);
17002 	mutex_init(&bp->link_lock);
17003 
17004 	rc = bnxt_fw_init_one_p1(bp);
17005 	if (rc)
17006 		goto init_err_pci_clean;
17007 
17008 	if (BNXT_PF(bp))
17009 		bnxt_vpd_read_info(bp);
17010 
17011 	if (BNXT_CHIP_P5_PLUS(bp)) {
17012 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
17013 		if (BNXT_CHIP_P7(bp))
17014 			bp->flags |= BNXT_FLAG_CHIP_P7;
17015 	}
17016 
17017 	rc = bnxt_alloc_rss_indir_tbl(bp);
17018 	if (rc)
17019 		goto init_err_pci_clean;
17020 
17021 	rc = bnxt_fw_init_one_p2(bp);
17022 	if (rc)
17023 		goto init_err_pci_clean;
17024 
17025 	rc = bnxt_map_db_bar(bp);
17026 	if (rc) {
17027 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
17028 			rc);
17029 		goto init_err_pci_clean;
17030 	}
17031 
17032 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
17033 			   NETIF_F_TSO | NETIF_F_TSO6 |
17034 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
17035 			   NETIF_F_GSO_IPXIP4 |
17036 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
17037 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
17038 			   NETIF_F_RXCSUM | NETIF_F_GRO;
17039 	dev->hw_features |= NETIF_F_GSO_UDP_L4;
17040 
17041 	if (BNXT_SUPPORTS_TPA(bp))
17042 		dev->hw_features |= NETIF_F_LRO;
17043 
17044 	dev->hw_enc_features =
17045 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
17046 			NETIF_F_TSO | NETIF_F_TSO6 |
17047 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
17048 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
17049 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
17050 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
17051 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
17052 	if (bp->flags & BNXT_FLAG_CHIP_P7)
17053 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
17054 	else
17055 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
17056 
17057 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
17058 				    NETIF_F_GSO_GRE_CSUM;
17059 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
17060 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
17061 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
17062 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
17063 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
17064 	if (BNXT_SUPPORTS_TPA(bp))
17065 		dev->hw_features |= NETIF_F_GRO_HW;
17066 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
17067 	if (dev->features & NETIF_F_GRO_HW)
17068 		dev->features &= ~NETIF_F_LRO;
17069 	dev->priv_flags |= IFF_UNICAST_FLT;
17070 
17071 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
17072 	if (!(bp->flags & BNXT_FLAG_UDP_GSO_CAP)) {
17073 		u16 max_segs = BNXT_SW_USO_MAX_SEGS;
17074 
17075 		if (bp->tso_max_segs)
17076 			max_segs = min_t(u16, max_segs, bp->tso_max_segs);
17077 		netif_set_tso_max_segs(dev, max_segs);
17078 	} else if (bp->tso_max_segs) {
17079 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
17080 	}
17081 
17082 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
17083 			    NETDEV_XDP_ACT_RX_SG;
17084 
17085 #ifdef CONFIG_BNXT_SRIOV
17086 	init_waitqueue_head(&bp->sriov_cfg_wait);
17087 #endif
17088 	if (BNXT_SUPPORTS_TPA(bp)) {
17089 		bp->gro_func = bnxt_gro_func_5730x;
17090 		if (BNXT_CHIP_P4(bp))
17091 			bp->gro_func = bnxt_gro_func_5731x;
17092 		else if (BNXT_CHIP_P5_PLUS(bp))
17093 			bp->gro_func = bnxt_gro_func_5750x;
17094 	}
17095 	if (!BNXT_CHIP_P4_PLUS(bp))
17096 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
17097 
17098 	rc = bnxt_init_mac_addr(bp);
17099 	if (rc) {
17100 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
17101 		rc = -EADDRNOTAVAIL;
17102 		goto init_err_pci_clean;
17103 	}
17104 
17105 	if (BNXT_PF(bp)) {
17106 		/* Read the adapter's DSN to use as the eswitch switch_id */
17107 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
17108 	}
17109 
17110 	/* MTU range: 60 - FW defined max */
17111 	dev->min_mtu = ETH_ZLEN;
17112 	dev->max_mtu = bp->max_mtu;
17113 
17114 	rc = bnxt_probe_phy(bp, true);
17115 	if (rc)
17116 		goto init_err_pci_clean;
17117 
17118 	hw_resc = &bp->hw_resc;
17119 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
17120 		       BNXT_L2_FLTR_MAX_FLTR;
17121 	/* Older firmware may not report these filters properly */
17122 	if (bp->max_fltr < BNXT_MAX_FLTR)
17123 		bp->max_fltr = BNXT_MAX_FLTR;
17124 	bnxt_init_l2_fltr_tbl(bp);
17125 	__bnxt_set_rx_skb_mode(bp, false);
17126 	bnxt_set_tpa_flags(bp);
17127 	bnxt_init_ring_params(bp);
17128 	bnxt_set_ring_params(bp);
17129 	mutex_init(&bp->auxdev_lock);
17130 	if (!bnxt_auxdev_id_alloc(bp))
17131 		bnxt_aux_devices_init(bp);
17132 	rc = bnxt_set_dflt_rings(bp, true);
17133 	if (rc) {
17134 		if (BNXT_VF(bp) && rc == -ENODEV) {
17135 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
17136 		} else {
17137 			netdev_err(bp->dev, "Not enough rings available.\n");
17138 			rc = -ENOMEM;
17139 		}
17140 		goto init_err_pci_clean;
17141 	}
17142 
17143 	bnxt_fw_init_one_p3(bp);
17144 
17145 	bnxt_init_dflt_coal(bp);
17146 
17147 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
17148 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
17149 
17150 	rc = bnxt_init_int_mode(bp);
17151 	if (rc)
17152 		goto init_err_pci_clean;
17153 
17154 	/* No TC has been set yet and rings may have been trimmed due to
17155 	 * limited MSIX, so we re-initialize the TX rings per TC.
17156 	 */
17157 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
17158 
17159 	if (BNXT_PF(bp)) {
17160 		if (!bnxt_pf_wq) {
17161 			bnxt_pf_wq =
17162 				create_singlethread_workqueue("bnxt_pf_wq");
17163 			if (!bnxt_pf_wq) {
17164 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
17165 				rc = -ENOMEM;
17166 				goto init_err_pci_clean;
17167 			}
17168 		}
17169 		rc = bnxt_init_tc(bp);
17170 		if (rc)
17171 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
17172 				   rc);
17173 	}
17174 
17175 	bnxt_inv_fw_health_reg(bp);
17176 	rc = bnxt_dl_register(bp);
17177 	if (rc)
17178 		goto init_err_dl;
17179 
17180 	INIT_LIST_HEAD(&bp->usr_fltr_list);
17181 
17182 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
17183 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
17184 
17185 	dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops_unsupp;
17186 	if (BNXT_SUPPORTS_QUEUE_API(bp))
17187 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
17188 	dev->netmem_tx = NETMEM_TX_DMA;
17189 
17190 	rc = register_netdev(dev);
17191 	if (rc)
17192 		goto init_err_cleanup;
17193 
17194 	bnxt_dl_fw_reporters_create(bp);
17195 
17196 	bnxt_aux_devices_add(bp);
17197 
17198 	bnxt_print_device_info(bp);
17199 
17200 	pci_save_state(pdev);
17201 
17202 	return 0;
17203 init_err_cleanup:
17204 	bnxt_aux_devices_uninit(bp);
17205 	bnxt_auxdev_id_free(bp, bp->auxdev_id);
17206 	bnxt_dl_unregister(bp);
17207 init_err_dl:
17208 	bnxt_shutdown_tc(bp);
17209 	bnxt_clear_int_mode(bp);
17210 
17211 init_err_pci_clean:
17212 	bnxt_hwrm_func_drv_unrgtr(bp);
17213 	bnxt_ptp_clear(bp);
17214 	kfree(bp->ptp_cfg);
17215 	bp->ptp_cfg = NULL;
17216 	bnxt_free_hwrm_resources(bp);
17217 	bnxt_hwmon_uninit(bp);
17218 	bnxt_ethtool_free(bp);
17219 	kfree(bp->fw_health);
17220 	bp->fw_health = NULL;
17221 	bnxt_cleanup_pci(bp);
17222 	bnxt_free_ctx_mem(bp, true);
17223 	bnxt_free_crash_dump_mem(bp);
17224 	kfree(bp->rss_indir_tbl);
17225 	bp->rss_indir_tbl = NULL;
17226 
17227 init_err_free:
17228 	free_netdev(dev);
17229 	return rc;
17230 }
17231 
17232 static void bnxt_shutdown(struct pci_dev *pdev)
17233 {
17234 	struct net_device *dev = pci_get_drvdata(pdev);
17235 	struct bnxt *bp;
17236 
17237 	if (!dev)
17238 		return;
17239 
17240 	rtnl_lock();
17241 	netdev_lock(dev);
17242 	bp = netdev_priv(dev);
17243 	if (!bp)
17244 		goto shutdown_exit;
17245 
17246 	if (netif_running(dev))
17247 		netif_close(dev);
17248 
17249 	if (bnxt_hwrm_func_drv_unrgtr(bp)) {
17250 		pcie_flr(pdev);
17251 		goto shutdown_exit;
17252 	}
17253 	bnxt_ptp_clear(bp);
17254 	bnxt_clear_int_mode(bp);
17255 	pci_disable_device(pdev);
17256 
17257 	if (system_state == SYSTEM_POWER_OFF) {
17258 		pci_wake_from_d3(pdev, bp->wol);
17259 		pci_set_power_state(pdev, PCI_D3hot);
17260 	}
17261 
17262 shutdown_exit:
17263 	netdev_unlock(dev);
17264 	rtnl_unlock();
17265 }
17266 
17267 #ifdef CONFIG_PM_SLEEP
17268 static int bnxt_suspend(struct device *device)
17269 {
17270 	struct net_device *dev = dev_get_drvdata(device);
17271 	struct bnxt *bp = netdev_priv(dev);
17272 	int rc = 0;
17273 
17274 	bnxt_ulp_stop(bp);
17275 
17276 	netdev_lock(dev);
17277 	if (netif_running(dev)) {
17278 		netif_device_detach(dev);
17279 		rc = bnxt_close(dev);
17280 	}
17281 	bnxt_hwrm_func_drv_unrgtr(bp);
17282 	bnxt_ptp_clear(bp);
17283 	pci_disable_device(bp->pdev);
17284 	bnxt_free_ctx_mem(bp, false);
17285 	netdev_unlock(dev);
17286 	return rc;
17287 }
17288 
17289 static int bnxt_resume(struct device *device)
17290 {
17291 	struct net_device *dev = dev_get_drvdata(device);
17292 	struct bnxt *bp = netdev_priv(dev);
17293 	int rc = 0;
17294 
17295 	rtnl_lock();
17296 	netdev_lock(dev);
17297 	rc = pci_enable_device(bp->pdev);
17298 	if (rc) {
17299 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
17300 			   rc);
17301 		goto resume_exit;
17302 	}
17303 	pci_set_master(bp->pdev);
17304 	if (bnxt_hwrm_ver_get(bp)) {
17305 		rc = -ENODEV;
17306 		goto resume_exit;
17307 	}
17308 	rc = bnxt_hwrm_func_reset(bp);
17309 	if (rc) {
17310 		rc = -EBUSY;
17311 		goto resume_exit;
17312 	}
17313 
17314 	rc = bnxt_hwrm_func_qcaps(bp);
17315 	if (rc)
17316 		goto resume_exit;
17317 
17318 	bnxt_clear_reservations(bp, true);
17319 
17320 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
17321 		rc = -ENODEV;
17322 		goto resume_exit;
17323 	}
17324 	if (bp->fw_crash_mem)
17325 		bnxt_hwrm_crash_dump_mem_cfg(bp);
17326 
17327 	if (bnxt_ptp_init(bp)) {
17328 		kfree(bp->ptp_cfg);
17329 		bp->ptp_cfg = NULL;
17330 	}
17331 	bnxt_get_wol_settings(bp);
17332 	if (netif_running(dev)) {
17333 		rc = bnxt_open(dev);
17334 		if (!rc)
17335 			netif_device_attach(dev);
17336 	}
17337 
17338 resume_exit:
17339 	netdev_unlock(bp->dev);
17340 	rtnl_unlock();
17341 	if (!rc) {
17342 		bnxt_ulp_start(bp);
17343 		bnxt_reenable_sriov(bp);
17344 	}
17345 	return rc;
17346 }
17347 
17348 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
17349 #define BNXT_PM_OPS (&bnxt_pm_ops)
17350 
17351 #else
17352 
17353 #define BNXT_PM_OPS NULL
17354 
17355 #endif /* CONFIG_PM_SLEEP */
17356 
17357 /**
17358  * bnxt_io_error_detected - called when PCI error is detected
17359  * @pdev: Pointer to PCI device
17360  * @state: The current pci connection state
17361  *
17362  * This function is called after a PCI bus error affecting
17363  * this device has been detected.
17364  */
17365 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
17366 					       pci_channel_state_t state)
17367 {
17368 	struct net_device *netdev = pci_get_drvdata(pdev);
17369 	struct bnxt *bp = netdev_priv(netdev);
17370 	bool abort = false;
17371 
17372 	netdev_info(netdev, "PCI I/O error detected\n");
17373 
17374 	bnxt_ulp_stop(bp);
17375 
17376 	netdev_lock(netdev);
17377 	netif_device_detach(netdev);
17378 
17379 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
17380 		netdev_err(bp->dev, "Firmware reset already in progress\n");
17381 		abort = true;
17382 	}
17383 
17384 	if (abort || state == pci_channel_io_perm_failure) {
17385 		netdev_unlock(netdev);
17386 		return PCI_ERS_RESULT_DISCONNECT;
17387 	}
17388 
17389 	/* Link is not reliable anymore if state is pci_channel_io_frozen
17390 	 * so we disable bus master to prevent any potential bad DMAs before
17391 	 * freeing kernel memory.
17392 	 */
17393 	if (state == pci_channel_io_frozen) {
17394 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
17395 		bnxt_fw_fatal_close(bp);
17396 	}
17397 
17398 	if (netif_running(netdev))
17399 		__bnxt_close_nic(bp, true, true);
17400 
17401 	if (pci_is_enabled(pdev))
17402 		pci_disable_device(pdev);
17403 	bnxt_free_ctx_mem(bp, false);
17404 	netdev_unlock(netdev);
17405 
17406 	/* Request a slot reset. */
17407 	return PCI_ERS_RESULT_NEED_RESET;
17408 }
17409 
17410 /**
17411  * bnxt_io_slot_reset - called after the pci bus has been reset.
17412  * @pdev: Pointer to PCI device
17413  *
17414  * Restart the card from scratch, as if from a cold-boot.
17415  * At this point, the card has experienced a hard reset,
17416  * followed by fixups by BIOS, and has its config space
17417  * set up identically to what it was at cold boot.
17418  */
17419 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
17420 {
17421 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
17422 	struct net_device *netdev = pci_get_drvdata(pdev);
17423 	struct bnxt *bp = netdev_priv(netdev);
17424 	int retry = 0;
17425 	int err = 0;
17426 	int off;
17427 
17428 	netdev_info(bp->dev, "PCI Slot Reset\n");
17429 
17430 	if (test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state)) {
17431 		/* After DPC, the chip should return CRS when the vendor ID
17432 		 * config register is read until it is ready.  On all chips,
17433 		 * this is not happening reliably so add a 5-second delay as a
17434 		 * workaround.
17435 		 */
17436 		msleep(5000);
17437 	}
17438 
17439 	netdev_lock(netdev);
17440 
17441 	if (pci_enable_device(pdev)) {
17442 		dev_err(&pdev->dev,
17443 			"Cannot re-enable PCI device after reset.\n");
17444 	} else {
17445 		pci_set_master(pdev);
17446 		/* Upon fatal error, our device internal logic that latches to
17447 		 * BAR value is getting reset and will restore only upon
17448 		 * rewriting the BARs.
17449 		 *
17450 		 * As pci_restore_state() does not re-write the BARs if the
17451 		 * value is same as saved value earlier, driver needs to
17452 		 * write the BARs to 0 to force restore, in case of fatal error.
17453 		 */
17454 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
17455 				       &bp->state)) {
17456 			for (off = PCI_BASE_ADDRESS_0;
17457 			     off <= PCI_BASE_ADDRESS_5; off += 4)
17458 				pci_write_config_dword(bp->pdev, off, 0);
17459 		}
17460 		pci_restore_state(pdev);
17461 
17462 		bnxt_inv_fw_health_reg(bp);
17463 		bnxt_try_map_fw_health_reg(bp);
17464 
17465 		/* In some PCIe AER scenarios, firmware may take up to
17466 		 * 10 seconds to become ready in the worst case.
17467 		 */
17468 		do {
17469 			err = bnxt_try_recover_fw(bp);
17470 			if (!err)
17471 				break;
17472 			retry++;
17473 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
17474 
17475 		if (err) {
17476 			dev_err(&pdev->dev, "Firmware not ready\n");
17477 			goto reset_exit;
17478 		}
17479 
17480 		err = bnxt_hwrm_func_reset(bp);
17481 		if (!err)
17482 			result = PCI_ERS_RESULT_RECOVERED;
17483 
17484 		/* IRQ will be initialized later in bnxt_io_resume */
17485 		bnxt_ulp_irq_stop(bp);
17486 		bnxt_clear_int_mode(bp);
17487 	}
17488 
17489 reset_exit:
17490 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
17491 	bnxt_clear_reservations(bp, true);
17492 	netdev_unlock(netdev);
17493 
17494 	return result;
17495 }
17496 
17497 /**
17498  * bnxt_io_resume - called when traffic can start flowing again.
17499  * @pdev: Pointer to PCI device
17500  *
17501  * This callback is called when the error recovery driver tells
17502  * us that its OK to resume normal operation.
17503  */
17504 static void bnxt_io_resume(struct pci_dev *pdev)
17505 {
17506 	struct net_device *netdev = pci_get_drvdata(pdev);
17507 	struct bnxt *bp = netdev_priv(netdev);
17508 	int err;
17509 
17510 	netdev_info(bp->dev, "PCI Slot Resume\n");
17511 	rtnl_lock();
17512 	netdev_lock(netdev);
17513 
17514 	err = bnxt_hwrm_func_qcaps(bp);
17515 	if (!err) {
17516 		if (netif_running(netdev)) {
17517 			err = bnxt_open(netdev);
17518 		} else {
17519 			err = bnxt_reserve_rings(bp, true);
17520 			if (!err)
17521 				err = bnxt_init_int_mode(bp);
17522 		}
17523 	}
17524 
17525 	if (!err)
17526 		netif_device_attach(netdev);
17527 
17528 	netdev_unlock(netdev);
17529 	rtnl_unlock();
17530 	if (!err) {
17531 		bnxt_ulp_start(bp);
17532 		bnxt_reenable_sriov(bp);
17533 	}
17534 }
17535 
17536 static const struct pci_error_handlers bnxt_err_handler = {
17537 	.error_detected	= bnxt_io_error_detected,
17538 	.slot_reset	= bnxt_io_slot_reset,
17539 	.resume		= bnxt_io_resume
17540 };
17541 
17542 static struct pci_driver bnxt_pci_driver = {
17543 	.name		= DRV_MODULE_NAME,
17544 	.id_table	= bnxt_pci_tbl,
17545 	.probe		= bnxt_init_one,
17546 	.remove		= bnxt_remove_one,
17547 	.shutdown	= bnxt_shutdown,
17548 	.driver.pm	= BNXT_PM_OPS,
17549 	.err_handler	= &bnxt_err_handler,
17550 #if defined(CONFIG_BNXT_SRIOV)
17551 	.sriov_configure = bnxt_sriov_configure,
17552 #endif
17553 };
17554 
17555 static int __init bnxt_init(void)
17556 {
17557 	int err;
17558 
17559 	bnxt_debug_init();
17560 	err = pci_register_driver(&bnxt_pci_driver);
17561 	if (err) {
17562 		bnxt_debug_exit();
17563 		return err;
17564 	}
17565 
17566 	return 0;
17567 }
17568 
17569 static void __exit bnxt_exit(void)
17570 {
17571 	pci_unregister_driver(&bnxt_pci_driver);
17572 	if (bnxt_pf_wq)
17573 		destroy_workqueue(bnxt_pf_wq);
17574 	bnxt_debug_exit();
17575 }
17576 
17577 module_init(bnxt_init);
17578 module_exit(bnxt_exit);
17579