xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision 189f164e573e18d9f8876dbd3ad8fcbe11f93037)
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 
63 #include "bnxt.h"
64 #include "bnxt_hwrm.h"
65 #include "bnxt_ulp.h"
66 #include "bnxt_sriov.h"
67 #include "bnxt_ethtool.h"
68 #include "bnxt_dcb.h"
69 #include "bnxt_xdp.h"
70 #include "bnxt_ptp.h"
71 #include "bnxt_vfr.h"
72 #include "bnxt_tc.h"
73 #include "bnxt_devlink.h"
74 #include "bnxt_debugfs.h"
75 #include "bnxt_coredump.h"
76 #include "bnxt_hwmon.h"
77 
78 #define BNXT_TX_TIMEOUT		(5 * HZ)
79 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
80 				 NETIF_MSG_TX_ERR)
81 
82 MODULE_IMPORT_NS("NETDEV_INTERNAL");
83 MODULE_LICENSE("GPL");
84 MODULE_DESCRIPTION("Broadcom NetXtreme network driver");
85 
86 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
87 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
88 
89 #define BNXT_TX_PUSH_THRESH 164
90 
91 /* indexed by enum board_idx */
92 static const struct {
93 	char *name;
94 } board_info[] = {
95 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
96 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
97 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
98 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
99 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
100 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
101 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
102 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
103 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
104 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
105 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
106 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
108 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
109 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
110 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
111 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
112 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
113 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
114 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
115 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
116 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
117 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
118 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
119 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
120 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
121 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
122 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
123 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
124 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
125 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
126 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
127 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
128 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
129 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
130 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
131 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
132 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
133 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
134 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
135 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
136 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
137 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
138 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
139 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
140 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
141 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
142 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
143 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
144 	[NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" },
145 	[NETXTREME_E_P7_VF_HV] = { "Broadcom BCM5760X Virtual Function for Hyper-V" },
146 };
147 
148 static const struct pci_device_id bnxt_pci_tbl[] = {
149 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
150 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
151 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
152 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
153 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
155 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
157 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
159 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
160 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
164 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
165 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
166 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
167 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
168 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
169 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
170 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
171 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
172 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
173 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
174 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
175 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
176 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
178 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
179 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
180 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
181 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
182 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
183 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
184 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
185 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
187 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
188 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
189 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
190 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
191 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
192 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
193 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
194 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
195 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
196 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
197 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
198 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
199 #ifdef CONFIG_BNXT_SRIOV
200 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
201 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
202 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
203 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
204 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
205 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
206 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
207 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
208 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
209 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
210 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
211 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
212 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
214 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
215 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
216 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
217 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
218 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
219 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
220 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
221 	{ PCI_VDEVICE(BROADCOM, 0x181b), .driver_data = NETXTREME_E_P7_VF_HV },
222 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
223 #endif
224 	{ 0 }
225 };
226 
227 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
228 
229 static const u16 bnxt_vf_req_snif[] = {
230 	HWRM_FUNC_CFG,
231 	HWRM_FUNC_VF_CFG,
232 	HWRM_PORT_PHY_QCFG,
233 	HWRM_CFA_L2_FILTER_ALLOC,
234 };
235 
236 static const u16 bnxt_async_events_arr[] = {
237 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
238 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
239 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
240 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
241 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
242 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
243 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
244 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
245 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
246 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
247 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
248 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
249 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
250 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
251 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
252 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
253 	ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER,
254 };
255 
256 const u16 bnxt_bstore_to_trace[] = {
257 	[BNXT_CTX_SRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT_TRACE,
258 	[BNXT_CTX_SRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT2_TRACE,
259 	[BNXT_CTX_CRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT_TRACE,
260 	[BNXT_CTX_CRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT2_TRACE,
261 	[BNXT_CTX_RIGP0]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP0_TRACE,
262 	[BNXT_CTX_L2HWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_L2_HWRM_TRACE,
263 	[BNXT_CTX_REHWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ROCE_HWRM_TRACE,
264 	[BNXT_CTX_CA0]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA0_TRACE,
265 	[BNXT_CTX_CA1]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA1_TRACE,
266 	[BNXT_CTX_CA2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA2_TRACE,
267 	[BNXT_CTX_RIGP1]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP1_TRACE,
268 	[BNXT_CTX_KONG]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_AFM_KONG_HWRM_TRACE,
269 	[BNXT_CTX_QPC]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ERR_QPC_TRACE,
270 };
271 
272 static struct workqueue_struct *bnxt_pf_wq;
273 
274 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
275 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
276 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
277 
278 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
279 	.ports = {
280 		.src = 0,
281 		.dst = 0,
282 	},
283 	.addrs = {
284 		.v6addrs = {
285 			.src = BNXT_IPV6_MASK_NONE,
286 			.dst = BNXT_IPV6_MASK_NONE,
287 		},
288 	},
289 };
290 
291 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
292 	.ports = {
293 		.src = cpu_to_be16(0xffff),
294 		.dst = cpu_to_be16(0xffff),
295 	},
296 	.addrs = {
297 		.v6addrs = {
298 			.src = BNXT_IPV6_MASK_ALL,
299 			.dst = BNXT_IPV6_MASK_ALL,
300 		},
301 	},
302 };
303 
304 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
305 	.ports = {
306 		.src = cpu_to_be16(0xffff),
307 		.dst = cpu_to_be16(0xffff),
308 	},
309 	.addrs = {
310 		.v4addrs = {
311 			.src = cpu_to_be32(0xffffffff),
312 			.dst = cpu_to_be32(0xffffffff),
313 		},
314 	},
315 };
316 
317 static bool bnxt_vf_pciid(enum board_idx idx)
318 {
319 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
320 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
321 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
322 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF ||
323 		idx == NETXTREME_E_P7_VF_HV);
324 }
325 
326 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
327 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
328 
329 #define BNXT_DB_CQ(db, idx)						\
330 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
331 
332 #define BNXT_DB_NQ_P5(db, idx)						\
333 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
334 		    (db)->doorbell)
335 
336 #define BNXT_DB_NQ_P7(db, idx)						\
337 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
338 		    DB_RING_IDX(db, idx), (db)->doorbell)
339 
340 #define BNXT_DB_CQ_ARM(db, idx)						\
341 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
342 
343 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
344 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
345 		    DB_RING_IDX(db, idx), (db)->doorbell)
346 
347 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
348 {
349 	if (bp->flags & BNXT_FLAG_CHIP_P7)
350 		BNXT_DB_NQ_P7(db, idx);
351 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
352 		BNXT_DB_NQ_P5(db, idx);
353 	else
354 		BNXT_DB_CQ(db, idx);
355 }
356 
357 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
358 {
359 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
360 		BNXT_DB_NQ_ARM_P5(db, idx);
361 	else
362 		BNXT_DB_CQ_ARM(db, idx);
363 }
364 
365 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
366 {
367 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
368 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
369 			    DB_RING_IDX(db, idx), db->doorbell);
370 	else
371 		BNXT_DB_CQ(db, idx);
372 }
373 
374 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
375 {
376 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
377 		return;
378 
379 	if (BNXT_PF(bp))
380 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
381 	else
382 		schedule_delayed_work(&bp->fw_reset_task, delay);
383 }
384 
385 static void __bnxt_queue_sp_work(struct bnxt *bp)
386 {
387 	if (BNXT_PF(bp))
388 		queue_work(bnxt_pf_wq, &bp->sp_task);
389 	else
390 		schedule_work(&bp->sp_task);
391 }
392 
393 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
394 {
395 	set_bit(event, &bp->sp_event);
396 	__bnxt_queue_sp_work(bp);
397 }
398 
399 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
400 {
401 	if (!rxr->bnapi->in_reset) {
402 		rxr->bnapi->in_reset = true;
403 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
404 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
405 		else
406 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
407 		__bnxt_queue_sp_work(bp);
408 	}
409 	rxr->rx_next_cons = 0xffff;
410 }
411 
412 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
413 			  u16 curr)
414 {
415 	struct bnxt_napi *bnapi = txr->bnapi;
416 
417 	if (bnapi->tx_fault)
418 		return;
419 
420 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
421 		   txr->txq_index, txr->tx_hw_cons,
422 		   txr->tx_cons, txr->tx_prod, curr);
423 	WARN_ON_ONCE(1);
424 	bnapi->tx_fault = 1;
425 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
426 }
427 
428 const u16 bnxt_lhint_arr[] = {
429 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
430 	TX_BD_FLAGS_LHINT_512_TO_1023,
431 	TX_BD_FLAGS_LHINT_1024_TO_2047,
432 	TX_BD_FLAGS_LHINT_1024_TO_2047,
433 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
434 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
435 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
436 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
437 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
438 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
439 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
440 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
441 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
442 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
443 	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 };
449 
450 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
451 {
452 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
453 
454 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
455 		return 0;
456 
457 	return md_dst->u.port_info.port_id;
458 }
459 
460 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
461 			     u16 prod)
462 {
463 	/* Sync BD data before updating doorbell */
464 	wmb();
465 	bnxt_db_write(bp, &txr->tx_db, prod);
466 	txr->kick_pending = 0;
467 }
468 
469 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
470 {
471 	struct bnxt *bp = netdev_priv(dev);
472 	struct tx_bd *txbd, *txbd0;
473 	struct tx_bd_ext *txbd1;
474 	struct netdev_queue *txq;
475 	int i;
476 	dma_addr_t mapping;
477 	unsigned int length, pad = 0;
478 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
479 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
480 	struct pci_dev *pdev = bp->pdev;
481 	u16 prod, last_frag, txts_prod;
482 	struct bnxt_tx_ring_info *txr;
483 	struct bnxt_sw_tx_bd *tx_buf;
484 	__le32 lflags = 0;
485 	skb_frag_t *frag;
486 
487 	i = skb_get_queue_mapping(skb);
488 	if (unlikely(i >= bp->tx_nr_rings)) {
489 		dev_kfree_skb_any(skb);
490 		dev_core_stats_tx_dropped_inc(dev);
491 		return NETDEV_TX_OK;
492 	}
493 
494 	txq = netdev_get_tx_queue(dev, i);
495 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
496 	prod = txr->tx_prod;
497 
498 #if (MAX_SKB_FRAGS > TX_MAX_FRAGS)
499 	if (skb_shinfo(skb)->nr_frags > TX_MAX_FRAGS) {
500 		netdev_warn_once(dev, "SKB has too many (%d) fragments, max supported is %d.  SKB will be linearized.\n",
501 				 skb_shinfo(skb)->nr_frags, TX_MAX_FRAGS);
502 		if (skb_linearize(skb)) {
503 			dev_kfree_skb_any(skb);
504 			dev_core_stats_tx_dropped_inc(dev);
505 			return NETDEV_TX_OK;
506 		}
507 	}
508 #endif
509 	free_size = bnxt_tx_avail(bp, txr);
510 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
511 		/* We must have raced with NAPI cleanup */
512 		if (net_ratelimit() && txr->kick_pending)
513 			netif_warn(bp, tx_err, dev,
514 				   "bnxt: ring busy w/ flush pending!\n");
515 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
516 					bp->tx_wake_thresh))
517 			return NETDEV_TX_BUSY;
518 	}
519 
520 	length = skb->len;
521 	len = skb_headlen(skb);
522 	last_frag = skb_shinfo(skb)->nr_frags;
523 
524 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
525 
526 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
527 	tx_buf->skb = skb;
528 	tx_buf->nr_frags = last_frag;
529 
530 	vlan_tag_flags = 0;
531 	cfa_action = bnxt_xmit_get_cfa_action(skb);
532 	if (skb_vlan_tag_present(skb)) {
533 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
534 				 skb_vlan_tag_get(skb);
535 		/* Currently supports 8021Q, 8021AD vlan offloads
536 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
537 		 */
538 		if (skb->vlan_proto == htons(ETH_P_8021Q))
539 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
540 	}
541 
542 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp &&
543 	    ptp->tx_tstamp_en) {
544 		if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) {
545 			lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
546 			tx_buf->is_ts_pkt = 1;
547 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
548 		} else if (!skb_is_gso(skb)) {
549 			u16 seq_id, hdr_off;
550 
551 			if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) &&
552 			    !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) {
553 				if (vlan_tag_flags)
554 					hdr_off += VLAN_HLEN;
555 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
556 				tx_buf->is_ts_pkt = 1;
557 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
558 
559 				ptp->txts_req[txts_prod].tx_seqid = seq_id;
560 				ptp->txts_req[txts_prod].tx_hdr_off = hdr_off;
561 				tx_buf->txts_prod = txts_prod;
562 			}
563 		}
564 	}
565 	if (unlikely(skb->no_fcs))
566 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
567 
568 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
569 	    skb_frags_readable(skb) && !lflags) {
570 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
571 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
572 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
573 		void __iomem *db = txr->tx_db.doorbell;
574 		void *pdata = tx_push_buf->data;
575 		u64 *end;
576 		int j, push_len;
577 
578 		/* Set COAL_NOW to be ready quickly for the next push */
579 		tx_push->tx_bd_len_flags_type =
580 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
581 					TX_BD_TYPE_LONG_TX_BD |
582 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
583 					TX_BD_FLAGS_COAL_NOW |
584 					TX_BD_FLAGS_PACKET_END |
585 					TX_BD_CNT(2));
586 
587 		if (skb->ip_summed == CHECKSUM_PARTIAL)
588 			tx_push1->tx_bd_hsize_lflags =
589 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
590 		else
591 			tx_push1->tx_bd_hsize_lflags = 0;
592 
593 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
594 		tx_push1->tx_bd_cfa_action =
595 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
596 
597 		end = pdata + length;
598 		end = PTR_ALIGN(end, 8) - 1;
599 		*end = 0;
600 
601 		skb_copy_from_linear_data(skb, pdata, len);
602 		pdata += len;
603 		for (j = 0; j < last_frag; j++) {
604 			void *fptr;
605 
606 			frag = &skb_shinfo(skb)->frags[j];
607 			fptr = skb_frag_address_safe(frag);
608 			if (!fptr)
609 				goto normal_tx;
610 
611 			memcpy(pdata, fptr, skb_frag_size(frag));
612 			pdata += skb_frag_size(frag);
613 		}
614 
615 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
616 		txbd->tx_bd_haddr = txr->data_mapping;
617 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
618 		prod = NEXT_TX(prod);
619 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
620 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
621 		memcpy(txbd, tx_push1, sizeof(*txbd));
622 		prod = NEXT_TX(prod);
623 		tx_push->doorbell =
624 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
625 				    DB_RING_IDX(&txr->tx_db, prod));
626 		WRITE_ONCE(txr->tx_prod, prod);
627 
628 		tx_buf->is_push = 1;
629 		netdev_tx_sent_queue(txq, skb->len);
630 		wmb();	/* Sync is_push and byte queue before pushing data */
631 
632 		push_len = (length + sizeof(*tx_push) + 7) / 8;
633 		if (push_len > 16) {
634 			__iowrite64_copy(db, tx_push_buf, 16);
635 			__iowrite32_copy(db + 4, tx_push_buf + 1,
636 					 (push_len - 16) << 1);
637 		} else {
638 			__iowrite64_copy(db, tx_push_buf, push_len);
639 		}
640 
641 		goto tx_done;
642 	}
643 
644 normal_tx:
645 	if (length < BNXT_MIN_PKT_SIZE) {
646 		pad = BNXT_MIN_PKT_SIZE - length;
647 		if (skb_pad(skb, pad))
648 			/* SKB already freed. */
649 			goto tx_kick_pending;
650 		length = BNXT_MIN_PKT_SIZE;
651 	}
652 
653 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
654 
655 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
656 		goto tx_free;
657 
658 	dma_unmap_addr_set(tx_buf, mapping, mapping);
659 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
660 		TX_BD_CNT(last_frag + 2);
661 
662 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
663 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
664 
665 	prod = NEXT_TX(prod);
666 	txbd1 = (struct tx_bd_ext *)
667 		&txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
668 
669 	txbd1->tx_bd_hsize_lflags = lflags;
670 	if (skb_is_gso(skb)) {
671 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
672 		u32 hdr_len;
673 
674 		if (skb->encapsulation) {
675 			if (udp_gso)
676 				hdr_len = skb_inner_transport_offset(skb) +
677 					  sizeof(struct udphdr);
678 			else
679 				hdr_len = skb_inner_tcp_all_headers(skb);
680 		} else if (udp_gso) {
681 			hdr_len = skb_transport_offset(skb) +
682 				  sizeof(struct udphdr);
683 		} else {
684 			hdr_len = skb_tcp_all_headers(skb);
685 		}
686 
687 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
688 					TX_BD_FLAGS_T_IPID |
689 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
690 		length = skb_shinfo(skb)->gso_size;
691 		txbd1->tx_bd_mss = cpu_to_le32(length);
692 		length += hdr_len;
693 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
694 		txbd1->tx_bd_hsize_lflags |=
695 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
696 		txbd1->tx_bd_mss = 0;
697 	}
698 
699 	length >>= 9;
700 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
701 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
702 				     skb->len);
703 		i = 0;
704 		goto tx_dma_error;
705 	}
706 	flags |= bnxt_lhint_arr[length];
707 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
708 
709 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
710 	txbd1->tx_bd_cfa_action =
711 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
712 	txbd0 = txbd;
713 	for (i = 0; i < last_frag; i++) {
714 		frag = &skb_shinfo(skb)->frags[i];
715 		prod = NEXT_TX(prod);
716 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
717 
718 		len = skb_frag_size(frag);
719 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
720 					   DMA_TO_DEVICE);
721 
722 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
723 			goto tx_dma_error;
724 
725 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
726 		netmem_dma_unmap_addr_set(skb_frag_netmem(frag), tx_buf,
727 					  mapping, mapping);
728 
729 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
730 
731 		flags = len << TX_BD_LEN_SHIFT;
732 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
733 	}
734 
735 	flags &= ~TX_BD_LEN;
736 	txbd->tx_bd_len_flags_type =
737 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
738 			    TX_BD_FLAGS_PACKET_END);
739 
740 	netdev_tx_sent_queue(txq, skb->len);
741 
742 	skb_tx_timestamp(skb);
743 
744 	prod = NEXT_TX(prod);
745 	WRITE_ONCE(txr->tx_prod, prod);
746 
747 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
748 		bnxt_txr_db_kick(bp, txr, prod);
749 	} else {
750 		if (free_size >= bp->tx_wake_thresh)
751 			txbd0->tx_bd_len_flags_type |=
752 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
753 		txr->kick_pending = 1;
754 	}
755 
756 tx_done:
757 
758 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
759 		if (netdev_xmit_more() && !tx_buf->is_push) {
760 			txbd0->tx_bd_len_flags_type &=
761 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
762 			bnxt_txr_db_kick(bp, txr, prod);
763 		}
764 
765 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
766 				   bp->tx_wake_thresh);
767 	}
768 	return NETDEV_TX_OK;
769 
770 tx_dma_error:
771 	last_frag = i;
772 
773 	/* start back at beginning and unmap skb */
774 	prod = txr->tx_prod;
775 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
776 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
777 			 skb_headlen(skb), DMA_TO_DEVICE);
778 	prod = NEXT_TX(prod);
779 
780 	/* unmap remaining mapped pages */
781 	for (i = 0; i < last_frag; i++) {
782 		prod = NEXT_TX(prod);
783 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
784 		frag = &skb_shinfo(skb)->frags[i];
785 		netmem_dma_unmap_page_attrs(&pdev->dev,
786 					    dma_unmap_addr(tx_buf, mapping),
787 					    skb_frag_size(frag),
788 					    DMA_TO_DEVICE, 0);
789 	}
790 
791 tx_free:
792 	dev_kfree_skb_any(skb);
793 tx_kick_pending:
794 	if (BNXT_TX_PTP_IS_SET(lflags)) {
795 		txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].is_ts_pkt = 0;
796 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
797 		if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
798 			/* set SKB to err so PTP worker will clean up */
799 			ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO);
800 	}
801 	if (txr->kick_pending)
802 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
803 	txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].skb = NULL;
804 	dev_core_stats_tx_dropped_inc(dev);
805 	return NETDEV_TX_OK;
806 }
807 
808 /* Returns true if some remaining TX packets not processed. */
809 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
810 			  int budget)
811 {
812 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
813 	struct pci_dev *pdev = bp->pdev;
814 	u16 hw_cons = txr->tx_hw_cons;
815 	unsigned int tx_bytes = 0;
816 	u16 cons = txr->tx_cons;
817 	skb_frag_t *frag;
818 	int tx_pkts = 0;
819 	bool rc = false;
820 
821 	while (RING_TX(bp, cons) != hw_cons) {
822 		struct bnxt_sw_tx_bd *tx_buf;
823 		struct sk_buff *skb;
824 		bool is_ts_pkt;
825 		int j, last;
826 
827 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
828 		skb = tx_buf->skb;
829 
830 		if (unlikely(!skb)) {
831 			bnxt_sched_reset_txr(bp, txr, cons);
832 			return rc;
833 		}
834 
835 		is_ts_pkt = tx_buf->is_ts_pkt;
836 		if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) {
837 			rc = true;
838 			break;
839 		}
840 
841 		cons = NEXT_TX(cons);
842 		tx_pkts++;
843 		tx_bytes += skb->len;
844 		tx_buf->skb = NULL;
845 		tx_buf->is_ts_pkt = 0;
846 
847 		if (tx_buf->is_push) {
848 			tx_buf->is_push = 0;
849 			goto next_tx_int;
850 		}
851 
852 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
853 				 skb_headlen(skb), DMA_TO_DEVICE);
854 		last = tx_buf->nr_frags;
855 
856 		for (j = 0; j < last; j++) {
857 			frag = &skb_shinfo(skb)->frags[j];
858 			cons = NEXT_TX(cons);
859 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
860 			netmem_dma_unmap_page_attrs(&pdev->dev,
861 						    dma_unmap_addr(tx_buf,
862 								   mapping),
863 						    skb_frag_size(frag),
864 						    DMA_TO_DEVICE, 0);
865 		}
866 		if (unlikely(is_ts_pkt)) {
867 			if (BNXT_CHIP_P5(bp)) {
868 				/* PTP worker takes ownership of the skb */
869 				bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod);
870 				skb = NULL;
871 			}
872 		}
873 
874 next_tx_int:
875 		cons = NEXT_TX(cons);
876 
877 		napi_consume_skb(skb, budget);
878 	}
879 
880 	WRITE_ONCE(txr->tx_cons, cons);
881 
882 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
883 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
884 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
885 
886 	return rc;
887 }
888 
889 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
890 {
891 	struct bnxt_tx_ring_info *txr;
892 	bool more = false;
893 	int i;
894 
895 	bnxt_for_each_napi_tx(i, bnapi, txr) {
896 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
897 			more |= __bnxt_tx_int(bp, txr, budget);
898 	}
899 	if (!more)
900 		bnapi->events &= ~BNXT_TX_CMP_EVENT;
901 }
902 
903 static bool bnxt_separate_head_pool(struct bnxt_rx_ring_info *rxr)
904 {
905 	return rxr->need_head_pool || rxr->rx_page_size < PAGE_SIZE;
906 }
907 
908 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
909 					 struct bnxt_rx_ring_info *rxr,
910 					 unsigned int *offset,
911 					 gfp_t gfp)
912 {
913 	struct page *page;
914 
915 	if (rxr->rx_page_size < PAGE_SIZE) {
916 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
917 						rxr->rx_page_size);
918 	} else {
919 		page = page_pool_dev_alloc_pages(rxr->page_pool);
920 		*offset = 0;
921 	}
922 	if (!page)
923 		return NULL;
924 
925 	*mapping = page_pool_get_dma_addr(page) + *offset;
926 	return page;
927 }
928 
929 static netmem_ref __bnxt_alloc_rx_netmem(struct bnxt *bp, dma_addr_t *mapping,
930 					 struct bnxt_rx_ring_info *rxr,
931 					 unsigned int *offset,
932 					 gfp_t gfp)
933 {
934 	netmem_ref netmem;
935 
936 	if (rxr->rx_page_size < PAGE_SIZE) {
937 		netmem = page_pool_alloc_frag_netmem(rxr->page_pool, offset,
938 						     rxr->rx_page_size, gfp);
939 	} else {
940 		netmem = page_pool_alloc_netmems(rxr->page_pool, gfp);
941 		*offset = 0;
942 	}
943 	if (!netmem)
944 		return 0;
945 
946 	*mapping = page_pool_get_dma_addr_netmem(netmem) + *offset;
947 	return netmem;
948 }
949 
950 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
951 				       struct bnxt_rx_ring_info *rxr,
952 				       gfp_t gfp)
953 {
954 	unsigned int offset;
955 	struct page *page;
956 
957 	page = page_pool_alloc_frag(rxr->head_pool, &offset,
958 				    bp->rx_buf_size, gfp);
959 	if (!page)
960 		return NULL;
961 
962 	*mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset;
963 	return page_address(page) + offset;
964 }
965 
966 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
967 		       u16 prod, gfp_t gfp)
968 {
969 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
970 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
971 	dma_addr_t mapping;
972 
973 	if (BNXT_RX_PAGE_MODE(bp)) {
974 		unsigned int offset;
975 		struct page *page =
976 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
977 
978 		if (!page)
979 			return -ENOMEM;
980 
981 		mapping += bp->rx_dma_offset;
982 		rx_buf->data = page;
983 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
984 	} else {
985 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp);
986 
987 		if (!data)
988 			return -ENOMEM;
989 
990 		rx_buf->data = data;
991 		rx_buf->data_ptr = data + bp->rx_offset;
992 	}
993 	rx_buf->mapping = mapping;
994 
995 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
996 	return 0;
997 }
998 
999 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
1000 {
1001 	u16 prod = rxr->rx_prod;
1002 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1003 	struct bnxt *bp = rxr->bnapi->bp;
1004 	struct rx_bd *cons_bd, *prod_bd;
1005 
1006 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1007 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1008 
1009 	prod_rx_buf->data = data;
1010 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
1011 
1012 	prod_rx_buf->mapping = cons_rx_buf->mapping;
1013 
1014 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1015 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
1016 
1017 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
1018 }
1019 
1020 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1021 {
1022 	u16 next, max = rxr->rx_agg_bmap_size;
1023 
1024 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
1025 	if (next >= max)
1026 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
1027 	return next;
1028 }
1029 
1030 static int bnxt_alloc_rx_netmem(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1031 				u16 prod, gfp_t gfp)
1032 {
1033 	struct rx_bd *rxbd =
1034 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1035 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
1036 	u16 sw_prod = rxr->rx_sw_agg_prod;
1037 	unsigned int offset = 0;
1038 	dma_addr_t mapping;
1039 	netmem_ref netmem;
1040 
1041 	netmem = __bnxt_alloc_rx_netmem(bp, &mapping, rxr, &offset, gfp);
1042 	if (!netmem)
1043 		return -ENOMEM;
1044 
1045 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1046 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1047 
1048 	__set_bit(sw_prod, rxr->rx_agg_bmap);
1049 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
1050 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1051 
1052 	rx_agg_buf->netmem = netmem;
1053 	rx_agg_buf->offset = offset;
1054 	rx_agg_buf->mapping = mapping;
1055 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1056 	rxbd->rx_bd_opaque = sw_prod;
1057 	return 0;
1058 }
1059 
1060 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1061 				       struct bnxt_cp_ring_info *cpr,
1062 				       u16 cp_cons, u16 curr)
1063 {
1064 	struct rx_agg_cmp *agg;
1065 
1066 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1067 	agg = (struct rx_agg_cmp *)
1068 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1069 	return agg;
1070 }
1071 
1072 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1073 					      struct bnxt_rx_ring_info *rxr,
1074 					      u16 agg_id, u16 curr)
1075 {
1076 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1077 
1078 	return &tpa_info->agg_arr[curr];
1079 }
1080 
1081 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1082 				   u16 start, u32 agg_bufs, bool tpa)
1083 {
1084 	struct bnxt_napi *bnapi = cpr->bnapi;
1085 	struct bnxt *bp = bnapi->bp;
1086 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1087 	u16 prod = rxr->rx_agg_prod;
1088 	u16 sw_prod = rxr->rx_sw_agg_prod;
1089 	bool p5_tpa = false;
1090 	u32 i;
1091 
1092 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1093 		p5_tpa = true;
1094 
1095 	for (i = 0; i < agg_bufs; i++) {
1096 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1097 		struct rx_agg_cmp *agg;
1098 		struct rx_bd *prod_bd;
1099 		netmem_ref netmem;
1100 		u16 cons;
1101 
1102 		if (p5_tpa)
1103 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1104 		else
1105 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1106 		cons = agg->rx_agg_cmp_opaque;
1107 		__clear_bit(cons, rxr->rx_agg_bmap);
1108 
1109 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1110 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1111 
1112 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1113 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1114 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1115 
1116 		/* It is possible for sw_prod to be equal to cons, so
1117 		 * set cons_rx_buf->netmem to 0 first.
1118 		 */
1119 		netmem = cons_rx_buf->netmem;
1120 		cons_rx_buf->netmem = 0;
1121 		prod_rx_buf->netmem = netmem;
1122 		prod_rx_buf->offset = cons_rx_buf->offset;
1123 
1124 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1125 
1126 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1127 
1128 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1129 		prod_bd->rx_bd_opaque = sw_prod;
1130 
1131 		prod = NEXT_RX_AGG(prod);
1132 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1133 	}
1134 	rxr->rx_agg_prod = prod;
1135 	rxr->rx_sw_agg_prod = sw_prod;
1136 }
1137 
1138 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1139 					      struct bnxt_rx_ring_info *rxr,
1140 					      u16 cons, void *data, u8 *data_ptr,
1141 					      dma_addr_t dma_addr,
1142 					      unsigned int offset_and_len)
1143 {
1144 	unsigned int len = offset_and_len & 0xffff;
1145 	struct page *page = data;
1146 	u16 prod = rxr->rx_prod;
1147 	struct sk_buff *skb;
1148 	int err;
1149 
1150 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1151 	if (unlikely(err)) {
1152 		bnxt_reuse_rx_data(rxr, cons, data);
1153 		return NULL;
1154 	}
1155 	dma_addr -= bp->rx_dma_offset;
1156 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, rxr->rx_page_size,
1157 				bp->rx_dir);
1158 	skb = napi_build_skb(data_ptr - bp->rx_offset, rxr->rx_page_size);
1159 	if (!skb) {
1160 		page_pool_recycle_direct(rxr->page_pool, page);
1161 		return NULL;
1162 	}
1163 	skb_mark_for_recycle(skb);
1164 	skb_reserve(skb, bp->rx_offset);
1165 	__skb_put(skb, len);
1166 
1167 	return skb;
1168 }
1169 
1170 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1171 					struct bnxt_rx_ring_info *rxr,
1172 					u16 cons, void *data, u8 *data_ptr,
1173 					dma_addr_t dma_addr,
1174 					unsigned int offset_and_len)
1175 {
1176 	unsigned int payload = offset_and_len >> 16;
1177 	unsigned int len = offset_and_len & 0xffff;
1178 	skb_frag_t *frag;
1179 	struct page *page = data;
1180 	u16 prod = rxr->rx_prod;
1181 	struct sk_buff *skb;
1182 	int off, err;
1183 
1184 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1185 	if (unlikely(err)) {
1186 		bnxt_reuse_rx_data(rxr, cons, data);
1187 		return NULL;
1188 	}
1189 	dma_addr -= bp->rx_dma_offset;
1190 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, rxr->rx_page_size,
1191 				bp->rx_dir);
1192 
1193 	if (unlikely(!payload))
1194 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1195 
1196 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1197 	if (!skb) {
1198 		page_pool_recycle_direct(rxr->page_pool, page);
1199 		return NULL;
1200 	}
1201 
1202 	skb_mark_for_recycle(skb);
1203 	off = (void *)data_ptr - page_address(page);
1204 	skb_add_rx_frag(skb, 0, page, off, len, rxr->rx_page_size);
1205 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1206 	       payload + NET_IP_ALIGN);
1207 
1208 	frag = &skb_shinfo(skb)->frags[0];
1209 	skb_frag_size_sub(frag, payload);
1210 	skb_frag_off_add(frag, payload);
1211 	skb->data_len -= payload;
1212 	skb->tail += payload;
1213 
1214 	return skb;
1215 }
1216 
1217 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1218 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1219 				   void *data, u8 *data_ptr,
1220 				   dma_addr_t dma_addr,
1221 				   unsigned int offset_and_len)
1222 {
1223 	u16 prod = rxr->rx_prod;
1224 	struct sk_buff *skb;
1225 	int err;
1226 
1227 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1228 	if (unlikely(err)) {
1229 		bnxt_reuse_rx_data(rxr, cons, data);
1230 		return NULL;
1231 	}
1232 
1233 	skb = napi_build_skb(data, bp->rx_buf_size);
1234 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1235 				bp->rx_dir);
1236 	if (!skb) {
1237 		page_pool_free_va(rxr->head_pool, data, true);
1238 		return NULL;
1239 	}
1240 
1241 	skb_mark_for_recycle(skb);
1242 	skb_reserve(skb, bp->rx_offset);
1243 	skb_put(skb, offset_and_len & 0xffff);
1244 	return skb;
1245 }
1246 
1247 static u32 __bnxt_rx_agg_netmems(struct bnxt *bp,
1248 				 struct bnxt_cp_ring_info *cpr,
1249 				 u16 idx, u32 agg_bufs, bool tpa,
1250 				 struct sk_buff *skb,
1251 				 struct xdp_buff *xdp)
1252 {
1253 	struct bnxt_napi *bnapi = cpr->bnapi;
1254 	struct skb_shared_info *shinfo;
1255 	struct bnxt_rx_ring_info *rxr;
1256 	u32 i, total_frag_len = 0;
1257 	bool p5_tpa = false;
1258 	u16 prod;
1259 
1260 	rxr = bnapi->rx_ring;
1261 	prod = rxr->rx_agg_prod;
1262 
1263 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1264 		p5_tpa = true;
1265 
1266 	if (skb)
1267 		shinfo = skb_shinfo(skb);
1268 	else
1269 		shinfo = xdp_get_shared_info_from_buff(xdp);
1270 
1271 	for (i = 0; i < agg_bufs; i++) {
1272 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1273 		struct rx_agg_cmp *agg;
1274 		u16 cons, frag_len;
1275 		netmem_ref netmem;
1276 
1277 		if (p5_tpa)
1278 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1279 		else
1280 			agg = bnxt_get_agg(bp, cpr, idx, i);
1281 		cons = agg->rx_agg_cmp_opaque;
1282 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1283 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1284 
1285 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1286 		if (skb) {
1287 			skb_add_rx_frag_netmem(skb, i, cons_rx_buf->netmem,
1288 					       cons_rx_buf->offset,
1289 					       frag_len, rxr->rx_page_size);
1290 		} else {
1291 			skb_frag_t *frag = &shinfo->frags[i];
1292 
1293 			skb_frag_fill_netmem_desc(frag, cons_rx_buf->netmem,
1294 						  cons_rx_buf->offset,
1295 						  frag_len);
1296 			shinfo->nr_frags = i + 1;
1297 		}
1298 		__clear_bit(cons, rxr->rx_agg_bmap);
1299 
1300 		/* It is possible for bnxt_alloc_rx_netmem() to allocate
1301 		 * a sw_prod index that equals the cons index, so we
1302 		 * need to clear the cons entry now.
1303 		 */
1304 		netmem = cons_rx_buf->netmem;
1305 		cons_rx_buf->netmem = 0;
1306 
1307 		if (xdp && netmem_is_pfmemalloc(netmem))
1308 			xdp_buff_set_frag_pfmemalloc(xdp);
1309 
1310 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_ATOMIC) != 0) {
1311 			if (skb) {
1312 				skb->len -= frag_len;
1313 				skb->data_len -= frag_len;
1314 				skb->truesize -= rxr->rx_page_size;
1315 			}
1316 
1317 			--shinfo->nr_frags;
1318 			cons_rx_buf->netmem = netmem;
1319 
1320 			/* Update prod since possibly some netmems have been
1321 			 * allocated already.
1322 			 */
1323 			rxr->rx_agg_prod = prod;
1324 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1325 			return 0;
1326 		}
1327 
1328 		page_pool_dma_sync_netmem_for_cpu(rxr->page_pool, netmem, 0,
1329 						  rxr->rx_page_size);
1330 
1331 		total_frag_len += frag_len;
1332 		prod = NEXT_RX_AGG(prod);
1333 	}
1334 	rxr->rx_agg_prod = prod;
1335 	return total_frag_len;
1336 }
1337 
1338 static struct sk_buff *bnxt_rx_agg_netmems_skb(struct bnxt *bp,
1339 					       struct bnxt_cp_ring_info *cpr,
1340 					       struct sk_buff *skb, u16 idx,
1341 					       u32 agg_bufs, bool tpa)
1342 {
1343 	u32 total_frag_len = 0;
1344 
1345 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1346 					       skb, NULL);
1347 	if (!total_frag_len) {
1348 		skb_mark_for_recycle(skb);
1349 		dev_kfree_skb(skb);
1350 		return NULL;
1351 	}
1352 
1353 	return skb;
1354 }
1355 
1356 static u32 bnxt_rx_agg_netmems_xdp(struct bnxt *bp,
1357 				   struct bnxt_cp_ring_info *cpr,
1358 				   struct xdp_buff *xdp, u16 idx,
1359 				   u32 agg_bufs, bool tpa)
1360 {
1361 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1362 	u32 total_frag_len = 0;
1363 
1364 	if (!xdp_buff_has_frags(xdp))
1365 		shinfo->nr_frags = 0;
1366 
1367 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1368 					       NULL, xdp);
1369 	if (total_frag_len) {
1370 		xdp_buff_set_frags_flag(xdp);
1371 		shinfo->nr_frags = agg_bufs;
1372 		shinfo->xdp_frags_size = total_frag_len;
1373 	}
1374 	return total_frag_len;
1375 }
1376 
1377 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1378 			       u8 agg_bufs, u32 *raw_cons)
1379 {
1380 	u16 last;
1381 	struct rx_agg_cmp *agg;
1382 
1383 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1384 	last = RING_CMP(*raw_cons);
1385 	agg = (struct rx_agg_cmp *)
1386 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1387 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1388 }
1389 
1390 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1391 				      unsigned int len,
1392 				      dma_addr_t mapping)
1393 {
1394 	struct bnxt *bp = bnapi->bp;
1395 	struct pci_dev *pdev = bp->pdev;
1396 	struct sk_buff *skb;
1397 
1398 	skb = napi_alloc_skb(&bnapi->napi, len);
1399 	if (!skb)
1400 		return NULL;
1401 
1402 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copybreak,
1403 				bp->rx_dir);
1404 
1405 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1406 	       len + NET_IP_ALIGN);
1407 
1408 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copybreak,
1409 				   bp->rx_dir);
1410 
1411 	skb_put(skb, len);
1412 
1413 	return skb;
1414 }
1415 
1416 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1417 				     unsigned int len,
1418 				     dma_addr_t mapping)
1419 {
1420 	return bnxt_copy_data(bnapi, data, len, mapping);
1421 }
1422 
1423 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1424 				     struct xdp_buff *xdp,
1425 				     unsigned int len,
1426 				     dma_addr_t mapping)
1427 {
1428 	unsigned int metasize = 0;
1429 	u8 *data = xdp->data;
1430 	struct sk_buff *skb;
1431 
1432 	len = xdp->data_end - xdp->data_meta;
1433 	metasize = xdp->data - xdp->data_meta;
1434 	data = xdp->data_meta;
1435 
1436 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1437 	if (!skb)
1438 		return skb;
1439 
1440 	if (metasize) {
1441 		skb_metadata_set(skb, metasize);
1442 		__skb_pull(skb, metasize);
1443 	}
1444 
1445 	return skb;
1446 }
1447 
1448 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1449 			   u32 *raw_cons, void *cmp)
1450 {
1451 	struct rx_cmp *rxcmp = cmp;
1452 	u32 tmp_raw_cons = *raw_cons;
1453 	u8 cmp_type, agg_bufs = 0;
1454 
1455 	cmp_type = RX_CMP_TYPE(rxcmp);
1456 
1457 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1458 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1459 			    RX_CMP_AGG_BUFS) >>
1460 			   RX_CMP_AGG_BUFS_SHIFT;
1461 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1462 		struct rx_tpa_end_cmp *tpa_end = cmp;
1463 
1464 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1465 			return 0;
1466 
1467 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1468 	}
1469 
1470 	if (agg_bufs) {
1471 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1472 			return -EBUSY;
1473 	}
1474 	*raw_cons = tmp_raw_cons;
1475 	return 0;
1476 }
1477 
1478 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1479 {
1480 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1481 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1482 
1483 	if (test_bit(idx, map->agg_idx_bmap)) {
1484 		idx = find_first_zero_bit(map->agg_idx_bmap, MAX_TPA_P5);
1485 		if (idx >= MAX_TPA_P5)
1486 			return INVALID_HW_RING_ID;
1487 	}
1488 	__set_bit(idx, map->agg_idx_bmap);
1489 	map->agg_id_tbl[agg_id] = idx;
1490 	return idx;
1491 }
1492 
1493 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1494 {
1495 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1496 
1497 	__clear_bit(idx, map->agg_idx_bmap);
1498 }
1499 
1500 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1501 {
1502 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1503 
1504 	return map->agg_id_tbl[agg_id];
1505 }
1506 
1507 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1508 			      struct rx_tpa_start_cmp *tpa_start,
1509 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1510 {
1511 	tpa_info->cfa_code_valid = 1;
1512 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1513 	tpa_info->vlan_valid = 0;
1514 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1515 		tpa_info->vlan_valid = 1;
1516 		tpa_info->metadata =
1517 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1518 	}
1519 }
1520 
1521 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1522 				 struct rx_tpa_start_cmp *tpa_start,
1523 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1524 {
1525 	tpa_info->vlan_valid = 0;
1526 	if (TPA_START_VLAN_VALID(tpa_start)) {
1527 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1528 		u32 vlan_proto = ETH_P_8021Q;
1529 
1530 		tpa_info->vlan_valid = 1;
1531 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1532 			vlan_proto = ETH_P_8021AD;
1533 		tpa_info->metadata = vlan_proto << 16 |
1534 				     TPA_START_METADATA0_TCI(tpa_start1);
1535 	}
1536 }
1537 
1538 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1539 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1540 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1541 {
1542 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1543 	struct bnxt_tpa_info *tpa_info;
1544 	u16 cons, prod, agg_id;
1545 	struct rx_bd *prod_bd;
1546 	dma_addr_t mapping;
1547 
1548 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1549 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1550 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1551 		if (unlikely(agg_id == INVALID_HW_RING_ID)) {
1552 			netdev_warn(bp->dev, "Unable to allocate agg ID for ring %d, agg 0x%x\n",
1553 				    rxr->bnapi->index,
1554 				    TPA_START_AGG_ID_P5(tpa_start));
1555 			bnxt_sched_reset_rxr(bp, rxr);
1556 			return;
1557 		}
1558 	} else {
1559 		agg_id = TPA_START_AGG_ID(tpa_start);
1560 	}
1561 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1562 	prod = rxr->rx_prod;
1563 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1564 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1565 	tpa_info = &rxr->rx_tpa[agg_id];
1566 
1567 	if (unlikely(cons != rxr->rx_next_cons ||
1568 		     TPA_START_ERROR(tpa_start))) {
1569 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1570 			    cons, rxr->rx_next_cons,
1571 			    TPA_START_ERROR_CODE(tpa_start1));
1572 		bnxt_sched_reset_rxr(bp, rxr);
1573 		return;
1574 	}
1575 	prod_rx_buf->data = tpa_info->data;
1576 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1577 
1578 	mapping = tpa_info->mapping;
1579 	prod_rx_buf->mapping = mapping;
1580 
1581 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1582 
1583 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1584 
1585 	tpa_info->data = cons_rx_buf->data;
1586 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1587 	cons_rx_buf->data = NULL;
1588 	tpa_info->mapping = cons_rx_buf->mapping;
1589 
1590 	tpa_info->len =
1591 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1592 				RX_TPA_START_CMP_LEN_SHIFT;
1593 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1594 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1595 		tpa_info->gso_type = SKB_GSO_TCPV4;
1596 		if (TPA_START_IS_IPV6(tpa_start1))
1597 			tpa_info->gso_type = SKB_GSO_TCPV6;
1598 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1599 		else if (!BNXT_CHIP_P4_PLUS(bp) &&
1600 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1601 			tpa_info->gso_type = SKB_GSO_TCPV6;
1602 		tpa_info->rss_hash =
1603 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1604 	} else {
1605 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1606 		tpa_info->gso_type = 0;
1607 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1608 	}
1609 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1610 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1611 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1612 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1613 	else
1614 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1615 	tpa_info->agg_count = 0;
1616 
1617 	rxr->rx_prod = NEXT_RX(prod);
1618 	cons = RING_RX(bp, NEXT_RX(cons));
1619 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1620 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1621 
1622 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1623 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1624 	cons_rx_buf->data = NULL;
1625 }
1626 
1627 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1628 {
1629 	if (agg_bufs)
1630 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1631 }
1632 
1633 #ifdef CONFIG_INET
1634 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1635 {
1636 	struct udphdr *uh = NULL;
1637 
1638 	if (ip_proto == htons(ETH_P_IP)) {
1639 		struct iphdr *iph = (struct iphdr *)skb->data;
1640 
1641 		if (iph->protocol == IPPROTO_UDP)
1642 			uh = (struct udphdr *)(iph + 1);
1643 	} else {
1644 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1645 
1646 		if (iph->nexthdr == IPPROTO_UDP)
1647 			uh = (struct udphdr *)(iph + 1);
1648 	}
1649 	if (uh) {
1650 		if (uh->check)
1651 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1652 		else
1653 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1654 	}
1655 }
1656 #endif
1657 
1658 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1659 					   int payload_off, int tcp_ts,
1660 					   struct sk_buff *skb)
1661 {
1662 #ifdef CONFIG_INET
1663 	struct tcphdr *th;
1664 	int len, nw_off;
1665 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1666 	u32 hdr_info = tpa_info->hdr_info;
1667 	bool loopback = false;
1668 
1669 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1670 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1671 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1672 
1673 	/* If the packet is an internal loopback packet, the offsets will
1674 	 * have an extra 4 bytes.
1675 	 */
1676 	if (inner_mac_off == 4) {
1677 		loopback = true;
1678 	} else if (inner_mac_off > 4) {
1679 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1680 					    ETH_HLEN - 2));
1681 
1682 		/* We only support inner iPv4/ipv6.  If we don't see the
1683 		 * correct protocol ID, it must be a loopback packet where
1684 		 * the offsets are off by 4.
1685 		 */
1686 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1687 			loopback = true;
1688 	}
1689 	if (loopback) {
1690 		/* internal loopback packet, subtract all offsets by 4 */
1691 		inner_ip_off -= 4;
1692 		inner_mac_off -= 4;
1693 		outer_ip_off -= 4;
1694 	}
1695 
1696 	nw_off = inner_ip_off - ETH_HLEN;
1697 	skb_set_network_header(skb, nw_off);
1698 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1699 		struct ipv6hdr *iph = ipv6_hdr(skb);
1700 
1701 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1702 		len = skb->len - skb_transport_offset(skb);
1703 		th = tcp_hdr(skb);
1704 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1705 	} else {
1706 		struct iphdr *iph = ip_hdr(skb);
1707 
1708 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1709 		len = skb->len - skb_transport_offset(skb);
1710 		th = tcp_hdr(skb);
1711 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1712 	}
1713 
1714 	if (inner_mac_off) { /* tunnel */
1715 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1716 					    ETH_HLEN - 2));
1717 
1718 		bnxt_gro_tunnel(skb, proto);
1719 	}
1720 #endif
1721 	return skb;
1722 }
1723 
1724 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1725 					   int payload_off, int tcp_ts,
1726 					   struct sk_buff *skb)
1727 {
1728 #ifdef CONFIG_INET
1729 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1730 	u32 hdr_info = tpa_info->hdr_info;
1731 	int iphdr_len, nw_off;
1732 
1733 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1734 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1735 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1736 
1737 	nw_off = inner_ip_off - ETH_HLEN;
1738 	skb_set_network_header(skb, nw_off);
1739 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1740 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1741 	skb_set_transport_header(skb, nw_off + iphdr_len);
1742 
1743 	if (inner_mac_off) { /* tunnel */
1744 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1745 					    ETH_HLEN - 2));
1746 
1747 		bnxt_gro_tunnel(skb, proto);
1748 	}
1749 #endif
1750 	return skb;
1751 }
1752 
1753 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1754 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1755 
1756 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1757 					   int payload_off, int tcp_ts,
1758 					   struct sk_buff *skb)
1759 {
1760 #ifdef CONFIG_INET
1761 	struct tcphdr *th;
1762 	int len, nw_off, tcp_opt_len = 0;
1763 
1764 	if (tcp_ts)
1765 		tcp_opt_len = 12;
1766 
1767 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1768 		struct iphdr *iph;
1769 
1770 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1771 			 ETH_HLEN;
1772 		skb_set_network_header(skb, nw_off);
1773 		iph = ip_hdr(skb);
1774 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1775 		len = skb->len - skb_transport_offset(skb);
1776 		th = tcp_hdr(skb);
1777 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1778 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1779 		struct ipv6hdr *iph;
1780 
1781 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1782 			 ETH_HLEN;
1783 		skb_set_network_header(skb, nw_off);
1784 		iph = ipv6_hdr(skb);
1785 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1786 		len = skb->len - skb_transport_offset(skb);
1787 		th = tcp_hdr(skb);
1788 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1789 	} else {
1790 		dev_kfree_skb_any(skb);
1791 		return NULL;
1792 	}
1793 
1794 	if (nw_off) /* tunnel */
1795 		bnxt_gro_tunnel(skb, skb->protocol);
1796 #endif
1797 	return skb;
1798 }
1799 
1800 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1801 					   struct bnxt_tpa_info *tpa_info,
1802 					   struct rx_tpa_end_cmp *tpa_end,
1803 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1804 					   struct sk_buff *skb,
1805 					   struct bnxt_rx_sw_stats *rx_stats)
1806 {
1807 #ifdef CONFIG_INET
1808 	int payload_off;
1809 	u16 segs;
1810 
1811 	segs = TPA_END_TPA_SEGS(tpa_end);
1812 	if (segs == 1)
1813 		return skb;
1814 
1815 	rx_stats->rx_hw_gro_packets++;
1816 	rx_stats->rx_hw_gro_wire_packets += segs;
1817 
1818 	NAPI_GRO_CB(skb)->count = segs;
1819 	skb_shinfo(skb)->gso_size =
1820 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1821 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1822 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1823 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1824 	else
1825 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1826 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1827 	if (likely(skb))
1828 		tcp_gro_complete(skb);
1829 #endif
1830 	return skb;
1831 }
1832 
1833 /* Given the cfa_code of a received packet determine which
1834  * netdev (vf-rep or PF) the packet is destined to.
1835  */
1836 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1837 {
1838 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1839 
1840 	/* if vf-rep dev is NULL, it must belong to the PF */
1841 	return dev ? dev : bp->dev;
1842 }
1843 
1844 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1845 					   struct bnxt_cp_ring_info *cpr,
1846 					   u32 *raw_cons,
1847 					   struct rx_tpa_end_cmp *tpa_end,
1848 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1849 					   u8 *event)
1850 {
1851 	struct bnxt_napi *bnapi = cpr->bnapi;
1852 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1853 	struct net_device *dev = bp->dev;
1854 	u8 *data_ptr, agg_bufs;
1855 	unsigned int len;
1856 	struct bnxt_tpa_info *tpa_info;
1857 	dma_addr_t mapping;
1858 	struct sk_buff *skb;
1859 	u16 idx = 0, agg_id;
1860 	void *data;
1861 	bool gro;
1862 
1863 	if (unlikely(bnapi->in_reset)) {
1864 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1865 
1866 		if (rc < 0)
1867 			return ERR_PTR(-EBUSY);
1868 		return NULL;
1869 	}
1870 
1871 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1872 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1873 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1874 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1875 		tpa_info = &rxr->rx_tpa[agg_id];
1876 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1877 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1878 				    agg_bufs, tpa_info->agg_count);
1879 			agg_bufs = tpa_info->agg_count;
1880 		}
1881 		tpa_info->agg_count = 0;
1882 		*event |= BNXT_AGG_EVENT;
1883 		bnxt_free_agg_idx(rxr, agg_id);
1884 		idx = agg_id;
1885 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1886 	} else {
1887 		agg_id = TPA_END_AGG_ID(tpa_end);
1888 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1889 		tpa_info = &rxr->rx_tpa[agg_id];
1890 		idx = RING_CMP(*raw_cons);
1891 		if (agg_bufs) {
1892 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1893 				return ERR_PTR(-EBUSY);
1894 
1895 			*event |= BNXT_AGG_EVENT;
1896 			idx = NEXT_CMP(idx);
1897 		}
1898 		gro = !!TPA_END_GRO(tpa_end);
1899 	}
1900 	data = tpa_info->data;
1901 	data_ptr = tpa_info->data_ptr;
1902 	prefetch(data_ptr);
1903 	len = tpa_info->len;
1904 	mapping = tpa_info->mapping;
1905 
1906 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1907 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1908 		if (agg_bufs > MAX_SKB_FRAGS)
1909 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1910 				    agg_bufs, (int)MAX_SKB_FRAGS);
1911 		return NULL;
1912 	}
1913 
1914 	if (len <= bp->rx_copybreak) {
1915 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1916 		if (!skb) {
1917 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1918 			cpr->sw_stats->rx.rx_oom_discards += 1;
1919 			return NULL;
1920 		}
1921 	} else {
1922 		u8 *new_data;
1923 		dma_addr_t new_mapping;
1924 
1925 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr,
1926 						GFP_ATOMIC);
1927 		if (!new_data) {
1928 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1929 			cpr->sw_stats->rx.rx_oom_discards += 1;
1930 			return NULL;
1931 		}
1932 
1933 		tpa_info->data = new_data;
1934 		tpa_info->data_ptr = new_data + bp->rx_offset;
1935 		tpa_info->mapping = new_mapping;
1936 
1937 		skb = napi_build_skb(data, bp->rx_buf_size);
1938 		dma_sync_single_for_cpu(&bp->pdev->dev, mapping,
1939 					bp->rx_buf_use_size, bp->rx_dir);
1940 
1941 		if (!skb) {
1942 			page_pool_free_va(rxr->head_pool, data, true);
1943 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1944 			cpr->sw_stats->rx.rx_oom_discards += 1;
1945 			return NULL;
1946 		}
1947 		skb_mark_for_recycle(skb);
1948 		skb_reserve(skb, bp->rx_offset);
1949 		skb_put(skb, len);
1950 	}
1951 
1952 	if (agg_bufs) {
1953 		skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, idx, agg_bufs,
1954 					      true);
1955 		if (!skb) {
1956 			/* Page reuse already handled by bnxt_rx_pages(). */
1957 			cpr->sw_stats->rx.rx_oom_discards += 1;
1958 			return NULL;
1959 		}
1960 	}
1961 
1962 	if (tpa_info->cfa_code_valid)
1963 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1964 	skb->protocol = eth_type_trans(skb, dev);
1965 
1966 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1967 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1968 
1969 	if (tpa_info->vlan_valid &&
1970 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1971 		__be16 vlan_proto = htons(tpa_info->metadata >>
1972 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1973 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1974 
1975 		if (eth_type_vlan(vlan_proto)) {
1976 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1977 		} else {
1978 			dev_kfree_skb(skb);
1979 			return NULL;
1980 		}
1981 	}
1982 
1983 	skb_checksum_none_assert(skb);
1984 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1985 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1986 		skb->csum_level =
1987 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1988 	}
1989 
1990 	if (gro)
1991 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb,
1992 				   &cpr->sw_stats->rx);
1993 
1994 	return skb;
1995 }
1996 
1997 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1998 			 struct rx_agg_cmp *rx_agg)
1999 {
2000 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
2001 	struct bnxt_tpa_info *tpa_info;
2002 
2003 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
2004 	tpa_info = &rxr->rx_tpa[agg_id];
2005 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
2006 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
2007 }
2008 
2009 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
2010 			     struct sk_buff *skb)
2011 {
2012 	skb_mark_for_recycle(skb);
2013 
2014 	if (skb->dev != bp->dev) {
2015 		/* this packet belongs to a vf-rep */
2016 		bnxt_vf_rep_rx(bp, skb);
2017 		return;
2018 	}
2019 	skb_record_rx_queue(skb, bnapi->index);
2020 	napi_gro_receive(&bnapi->napi, skb);
2021 }
2022 
2023 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
2024 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
2025 {
2026 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
2027 
2028 	if (BNXT_PTP_RX_TS_VALID(flags))
2029 		goto ts_valid;
2030 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
2031 		return false;
2032 
2033 ts_valid:
2034 	*cmpl_ts = ts;
2035 	return true;
2036 }
2037 
2038 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
2039 				    struct rx_cmp *rxcmp,
2040 				    struct rx_cmp_ext *rxcmp1)
2041 {
2042 	__be16 vlan_proto;
2043 	u16 vtag;
2044 
2045 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
2046 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
2047 		u32 meta_data;
2048 
2049 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
2050 			return skb;
2051 
2052 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
2053 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
2054 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
2055 		if (eth_type_vlan(vlan_proto))
2056 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2057 		else
2058 			goto vlan_err;
2059 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2060 		if (RX_CMP_VLAN_VALID(rxcmp)) {
2061 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
2062 
2063 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
2064 				vlan_proto = htons(ETH_P_8021Q);
2065 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
2066 				vlan_proto = htons(ETH_P_8021AD);
2067 			else
2068 				goto vlan_err;
2069 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
2070 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2071 		}
2072 	}
2073 	return skb;
2074 vlan_err:
2075 	skb_mark_for_recycle(skb);
2076 	dev_kfree_skb(skb);
2077 	return NULL;
2078 }
2079 
2080 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp,
2081 					   struct rx_cmp *rxcmp)
2082 {
2083 	u8 ext_op;
2084 
2085 	ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp);
2086 	switch (ext_op) {
2087 	case EXT_OP_INNER_4:
2088 	case EXT_OP_OUTER_4:
2089 	case EXT_OP_INNFL_3:
2090 	case EXT_OP_OUTFL_3:
2091 		return PKT_HASH_TYPE_L4;
2092 	default:
2093 		return PKT_HASH_TYPE_L3;
2094 	}
2095 }
2096 
2097 /* returns the following:
2098  * 1       - 1 packet successfully received
2099  * 0       - successful TPA_START, packet not completed yet
2100  * -EBUSY  - completion ring does not have all the agg buffers yet
2101  * -ENOMEM - packet aborted due to out of memory
2102  * -EIO    - packet aborted due to hw error indicated in BD
2103  */
2104 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2105 		       u32 *raw_cons, u8 *event)
2106 {
2107 	struct bnxt_napi *bnapi = cpr->bnapi;
2108 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2109 	struct net_device *dev = bp->dev;
2110 	struct rx_cmp *rxcmp;
2111 	struct rx_cmp_ext *rxcmp1;
2112 	u32 tmp_raw_cons = *raw_cons;
2113 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2114 	struct skb_shared_info *sinfo;
2115 	struct bnxt_sw_rx_bd *rx_buf;
2116 	unsigned int len;
2117 	u8 *data_ptr, agg_bufs, cmp_type;
2118 	bool xdp_active = false;
2119 	dma_addr_t dma_addr;
2120 	struct sk_buff *skb;
2121 	struct xdp_buff xdp;
2122 	u32 flags, misc;
2123 	u32 cmpl_ts;
2124 	void *data;
2125 	int rc = 0;
2126 
2127 	rxcmp = (struct rx_cmp *)
2128 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2129 
2130 	cmp_type = RX_CMP_TYPE(rxcmp);
2131 
2132 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2133 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2134 		goto next_rx_no_prod_no_len;
2135 	}
2136 
2137 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2138 	cp_cons = RING_CMP(tmp_raw_cons);
2139 	rxcmp1 = (struct rx_cmp_ext *)
2140 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2141 
2142 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2143 		return -EBUSY;
2144 
2145 	/* The valid test of the entry must be done first before
2146 	 * reading any further.
2147 	 */
2148 	dma_rmb();
2149 	prod = rxr->rx_prod;
2150 
2151 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2152 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2153 		bnxt_tpa_start(bp, rxr, cmp_type,
2154 			       (struct rx_tpa_start_cmp *)rxcmp,
2155 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2156 
2157 		*event |= BNXT_RX_EVENT;
2158 		goto next_rx_no_prod_no_len;
2159 
2160 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2161 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2162 				   (struct rx_tpa_end_cmp *)rxcmp,
2163 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2164 
2165 		if (IS_ERR(skb))
2166 			return -EBUSY;
2167 
2168 		rc = -ENOMEM;
2169 		if (likely(skb)) {
2170 			bnxt_deliver_skb(bp, bnapi, skb);
2171 			rc = 1;
2172 		}
2173 		*event |= BNXT_RX_EVENT;
2174 		goto next_rx_no_prod_no_len;
2175 	}
2176 
2177 	cons = rxcmp->rx_cmp_opaque;
2178 	if (unlikely(cons != rxr->rx_next_cons)) {
2179 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2180 
2181 		/* 0xffff is forced error, don't print it */
2182 		if (rxr->rx_next_cons != 0xffff)
2183 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2184 				    cons, rxr->rx_next_cons);
2185 		bnxt_sched_reset_rxr(bp, rxr);
2186 		if (rc1)
2187 			return rc1;
2188 		goto next_rx_no_prod_no_len;
2189 	}
2190 	rx_buf = &rxr->rx_buf_ring[cons];
2191 	data = rx_buf->data;
2192 	data_ptr = rx_buf->data_ptr;
2193 	prefetch(data_ptr);
2194 
2195 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2196 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2197 
2198 	if (agg_bufs) {
2199 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2200 			return -EBUSY;
2201 
2202 		cp_cons = NEXT_CMP(cp_cons);
2203 		*event |= BNXT_AGG_EVENT;
2204 	}
2205 	*event |= BNXT_RX_EVENT;
2206 
2207 	rx_buf->data = NULL;
2208 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2209 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2210 
2211 		bnxt_reuse_rx_data(rxr, cons, data);
2212 		if (agg_bufs)
2213 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2214 					       false);
2215 
2216 		rc = -EIO;
2217 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2218 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2219 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2220 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2221 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2222 						 rx_err);
2223 				bnxt_sched_reset_rxr(bp, rxr);
2224 			}
2225 		}
2226 		goto next_rx_no_len;
2227 	}
2228 
2229 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2230 	len = flags >> RX_CMP_LEN_SHIFT;
2231 	dma_addr = rx_buf->mapping;
2232 
2233 	if (bnxt_xdp_attached(bp, rxr)) {
2234 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2235 		if (agg_bufs) {
2236 			u32 frag_len = bnxt_rx_agg_netmems_xdp(bp, cpr, &xdp,
2237 							       cp_cons,
2238 							       agg_bufs,
2239 							       false);
2240 			if (!frag_len)
2241 				goto oom_next_rx;
2242 
2243 		}
2244 		xdp_active = true;
2245 	}
2246 
2247 	if (xdp_active) {
2248 		if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) {
2249 			rc = 1;
2250 			goto next_rx;
2251 		}
2252 		if (xdp_buff_has_frags(&xdp)) {
2253 			sinfo = xdp_get_shared_info_from_buff(&xdp);
2254 			agg_bufs = sinfo->nr_frags;
2255 		} else {
2256 			agg_bufs = 0;
2257 		}
2258 	}
2259 
2260 	if (len <= bp->rx_copybreak) {
2261 		if (!xdp_active)
2262 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2263 		else
2264 			skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr);
2265 		bnxt_reuse_rx_data(rxr, cons, data);
2266 		if (!skb) {
2267 			if (agg_bufs) {
2268 				if (!xdp_active)
2269 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2270 							       agg_bufs, false);
2271 				else
2272 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2273 			}
2274 			goto oom_next_rx;
2275 		}
2276 	} else {
2277 		u32 payload;
2278 
2279 		if (rx_buf->data_ptr == data_ptr)
2280 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2281 		else
2282 			payload = 0;
2283 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2284 				      payload | len);
2285 		if (!skb)
2286 			goto oom_next_rx;
2287 	}
2288 
2289 	if (agg_bufs) {
2290 		if (!xdp_active) {
2291 			skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, cp_cons,
2292 						      agg_bufs, false);
2293 			if (!skb)
2294 				goto oom_next_rx;
2295 		} else {
2296 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, rxr, &xdp);
2297 			if (!skb) {
2298 				/* we should be able to free the old skb here */
2299 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2300 				goto oom_next_rx;
2301 			}
2302 		}
2303 	}
2304 
2305 	if (RX_CMP_HASH_VALID(rxcmp)) {
2306 		enum pkt_hash_types type;
2307 
2308 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2309 			type = bnxt_rss_ext_op(bp, rxcmp);
2310 		} else {
2311 			u32 itypes = RX_CMP_ITYPES(rxcmp);
2312 
2313 			if (itypes == RX_CMP_FLAGS_ITYPE_TCP ||
2314 			    itypes == RX_CMP_FLAGS_ITYPE_UDP)
2315 				type = PKT_HASH_TYPE_L4;
2316 			else
2317 				type = PKT_HASH_TYPE_L3;
2318 		}
2319 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2320 	}
2321 
2322 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2323 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2324 	skb->protocol = eth_type_trans(skb, dev);
2325 
2326 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2327 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2328 		if (!skb)
2329 			goto next_rx;
2330 	}
2331 
2332 	skb_checksum_none_assert(skb);
2333 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2334 		if (dev->features & NETIF_F_RXCSUM) {
2335 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2336 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2337 		}
2338 	} else {
2339 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2340 			if (dev->features & NETIF_F_RXCSUM)
2341 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2342 		}
2343 	}
2344 
2345 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2346 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2347 			u64 ns, ts;
2348 
2349 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2350 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2351 
2352 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2353 				memset(skb_hwtstamps(skb), 0,
2354 				       sizeof(*skb_hwtstamps(skb)));
2355 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2356 			}
2357 		}
2358 	}
2359 	bnxt_deliver_skb(bp, bnapi, skb);
2360 	rc = 1;
2361 
2362 next_rx:
2363 	cpr->rx_packets += 1;
2364 	cpr->rx_bytes += len;
2365 
2366 next_rx_no_len:
2367 	rxr->rx_prod = NEXT_RX(prod);
2368 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2369 
2370 next_rx_no_prod_no_len:
2371 	*raw_cons = tmp_raw_cons;
2372 
2373 	return rc;
2374 
2375 oom_next_rx:
2376 	cpr->sw_stats->rx.rx_oom_discards += 1;
2377 	rc = -ENOMEM;
2378 	goto next_rx;
2379 }
2380 
2381 /* In netpoll mode, if we are using a combined completion ring, we need to
2382  * discard the rx packets and recycle the buffers.
2383  */
2384 static int bnxt_force_rx_discard(struct bnxt *bp,
2385 				 struct bnxt_cp_ring_info *cpr,
2386 				 u32 *raw_cons, u8 *event)
2387 {
2388 	u32 tmp_raw_cons = *raw_cons;
2389 	struct rx_cmp_ext *rxcmp1;
2390 	struct rx_cmp *rxcmp;
2391 	u16 cp_cons;
2392 	u8 cmp_type;
2393 	int rc;
2394 
2395 	cp_cons = RING_CMP(tmp_raw_cons);
2396 	rxcmp = (struct rx_cmp *)
2397 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2398 
2399 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2400 	cp_cons = RING_CMP(tmp_raw_cons);
2401 	rxcmp1 = (struct rx_cmp_ext *)
2402 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2403 
2404 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2405 		return -EBUSY;
2406 
2407 	/* The valid test of the entry must be done first before
2408 	 * reading any further.
2409 	 */
2410 	dma_rmb();
2411 	cmp_type = RX_CMP_TYPE(rxcmp);
2412 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2413 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2414 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2415 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2416 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2417 		struct rx_tpa_end_cmp_ext *tpa_end1;
2418 
2419 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2420 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2421 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2422 	}
2423 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2424 	if (rc && rc != -EBUSY)
2425 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2426 	return rc;
2427 }
2428 
2429 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2430 {
2431 	struct bnxt_fw_health *fw_health = bp->fw_health;
2432 	u32 reg = fw_health->regs[reg_idx];
2433 	u32 reg_type, reg_off, val = 0;
2434 
2435 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2436 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2437 	switch (reg_type) {
2438 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2439 		pci_read_config_dword(bp->pdev, reg_off, &val);
2440 		break;
2441 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2442 		reg_off = fw_health->mapped_regs[reg_idx];
2443 		fallthrough;
2444 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2445 		val = readl(bp->bar0 + reg_off);
2446 		break;
2447 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2448 		val = readl(bp->bar1 + reg_off);
2449 		break;
2450 	}
2451 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2452 		val &= fw_health->fw_reset_inprog_reg_mask;
2453 	return val;
2454 }
2455 
2456 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2457 {
2458 	int i;
2459 
2460 	for (i = 0; i < bp->rx_nr_rings; i++) {
2461 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2462 		struct bnxt_ring_grp_info *grp_info;
2463 
2464 		grp_info = &bp->grp_info[grp_idx];
2465 		if (grp_info->agg_fw_ring_id == ring_id)
2466 			return grp_idx;
2467 	}
2468 	return INVALID_HW_RING_ID;
2469 }
2470 
2471 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2472 {
2473 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2474 
2475 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2476 		return link_info->force_link_speed2;
2477 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2478 		return link_info->force_pam4_link_speed;
2479 	return link_info->force_link_speed;
2480 }
2481 
2482 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2483 {
2484 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2485 
2486 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2487 		link_info->req_link_speed = link_info->force_link_speed2;
2488 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2489 		switch (link_info->req_link_speed) {
2490 		case BNXT_LINK_SPEED_50GB_PAM4:
2491 		case BNXT_LINK_SPEED_100GB_PAM4:
2492 		case BNXT_LINK_SPEED_200GB_PAM4:
2493 		case BNXT_LINK_SPEED_400GB_PAM4:
2494 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2495 			break;
2496 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2497 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2498 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2499 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2500 			break;
2501 		default:
2502 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2503 		}
2504 		return;
2505 	}
2506 	link_info->req_link_speed = link_info->force_link_speed;
2507 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2508 	if (link_info->force_pam4_link_speed) {
2509 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2510 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2511 	}
2512 }
2513 
2514 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2515 {
2516 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2517 
2518 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2519 		link_info->advertising = link_info->auto_link_speeds2;
2520 		return;
2521 	}
2522 	link_info->advertising = link_info->auto_link_speeds;
2523 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2524 }
2525 
2526 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2527 {
2528 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2529 
2530 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2531 		if (link_info->req_link_speed != link_info->force_link_speed2)
2532 			return true;
2533 		return false;
2534 	}
2535 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2536 	    link_info->req_link_speed != link_info->force_link_speed)
2537 		return true;
2538 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2539 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2540 		return true;
2541 	return false;
2542 }
2543 
2544 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2545 {
2546 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2547 
2548 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2549 		if (link_info->advertising != link_info->auto_link_speeds2)
2550 			return true;
2551 		return false;
2552 	}
2553 	if (link_info->advertising != link_info->auto_link_speeds ||
2554 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2555 		return true;
2556 	return false;
2557 }
2558 
2559 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type)
2560 {
2561 	u32 flags = bp->ctx->ctx_arr[type].flags;
2562 
2563 	return (flags & BNXT_CTX_MEM_TYPE_VALID) &&
2564 		((flags & BNXT_CTX_MEM_FW_TRACE) ||
2565 		 (flags & BNXT_CTX_MEM_FW_BIN_TRACE));
2566 }
2567 
2568 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm)
2569 {
2570 	u32 mem_size, pages, rem_bytes, magic_byte_offset;
2571 	u16 trace_type = bnxt_bstore_to_trace[ctxm->type];
2572 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
2573 	struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl;
2574 	struct bnxt_bs_trace_info *bs_trace;
2575 	int last_pg;
2576 
2577 	if (ctxm->instance_bmap && ctxm->instance_bmap > 1)
2578 		return;
2579 
2580 	mem_size = ctxm->max_entries * ctxm->entry_size;
2581 	rem_bytes = mem_size % BNXT_PAGE_SIZE;
2582 	pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
2583 
2584 	last_pg = (pages - 1) & (MAX_CTX_PAGES - 1);
2585 	magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1;
2586 
2587 	rmem = &ctx_pg[0].ring_mem;
2588 	bs_trace = &bp->bs_trace[trace_type];
2589 	bs_trace->ctx_type = ctxm->type;
2590 	bs_trace->trace_type = trace_type;
2591 	if (pages > MAX_CTX_PAGES) {
2592 		int last_pg_dir = rmem->nr_pages - 1;
2593 
2594 		rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem;
2595 		bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg];
2596 	} else {
2597 		bs_trace->magic_byte = rmem->pg_arr[last_pg];
2598 	}
2599 	bs_trace->magic_byte += magic_byte_offset;
2600 	*bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE;
2601 }
2602 
2603 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1)				\
2604 	(((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\
2605 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT)
2606 
2607 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2)				\
2608 	(((data2) &							\
2609 	  ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\
2610 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT)
2611 
2612 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2613 	((data2) &							\
2614 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2615 
2616 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2617 	(((data2) &							\
2618 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2619 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2620 
2621 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2622 	((data1) &							\
2623 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2624 
2625 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2626 	(((data1) &							\
2627 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2628 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2629 
2630 /* Return true if the workqueue has to be scheduled */
2631 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2632 {
2633 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2634 
2635 	switch (err_type) {
2636 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2637 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2638 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2639 		break;
2640 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2641 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2642 		break;
2643 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2644 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2645 		break;
2646 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2647 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2648 		char *threshold_type;
2649 		bool notify = false;
2650 		char *dir_str;
2651 
2652 		switch (type) {
2653 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2654 			threshold_type = "warning";
2655 			break;
2656 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2657 			threshold_type = "critical";
2658 			break;
2659 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2660 			threshold_type = "fatal";
2661 			break;
2662 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2663 			threshold_type = "shutdown";
2664 			break;
2665 		default:
2666 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2667 			return false;
2668 		}
2669 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2670 			dir_str = "above";
2671 			notify = true;
2672 		} else {
2673 			dir_str = "below";
2674 		}
2675 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2676 			    dir_str, threshold_type);
2677 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2678 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2679 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2680 		if (notify) {
2681 			bp->thermal_threshold_type = type;
2682 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2683 			return true;
2684 		}
2685 		return false;
2686 	}
2687 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2688 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2689 		break;
2690 	default:
2691 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2692 			   err_type);
2693 		break;
2694 	}
2695 	return false;
2696 }
2697 
2698 #define BNXT_GET_EVENT_PORT(data)	\
2699 	((data) &			\
2700 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2701 
2702 #define BNXT_EVENT_RING_TYPE(data2)	\
2703 	((data2) &			\
2704 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2705 
2706 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2707 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2708 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2709 
2710 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2711 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2712 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2713 
2714 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2715 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2716 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2717 
2718 #define BNXT_PHC_BITS	48
2719 
2720 static int bnxt_async_event_process(struct bnxt *bp,
2721 				    struct hwrm_async_event_cmpl *cmpl)
2722 {
2723 	u16 event_id = le16_to_cpu(cmpl->event_id);
2724 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2725 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2726 
2727 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2728 		   event_id, data1, data2);
2729 
2730 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2731 	switch (event_id) {
2732 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2733 		struct bnxt_link_info *link_info = &bp->link_info;
2734 
2735 		if (BNXT_VF(bp))
2736 			goto async_event_process_exit;
2737 
2738 		/* print unsupported speed warning in forced speed mode only */
2739 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2740 		    (data1 & 0x20000)) {
2741 			u16 fw_speed = bnxt_get_force_speed(link_info);
2742 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2743 
2744 			if (speed != SPEED_UNKNOWN)
2745 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2746 					    speed);
2747 		}
2748 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2749 	}
2750 		fallthrough;
2751 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2752 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2753 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2754 		fallthrough;
2755 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2756 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2757 		break;
2758 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2759 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2760 		break;
2761 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2762 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2763 
2764 		if (BNXT_VF(bp))
2765 			break;
2766 
2767 		if (bp->pf.port_id != port_id)
2768 			break;
2769 
2770 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2771 		break;
2772 	}
2773 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2774 		if (BNXT_PF(bp))
2775 			goto async_event_process_exit;
2776 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2777 		break;
2778 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2779 		char *type_str = "Solicited";
2780 
2781 		if (!bp->fw_health)
2782 			goto async_event_process_exit;
2783 
2784 		bp->fw_reset_timestamp = jiffies;
2785 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2786 		if (!bp->fw_reset_min_dsecs)
2787 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2788 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2789 		if (!bp->fw_reset_max_dsecs)
2790 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2791 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2792 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2793 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2794 			type_str = "Fatal";
2795 			bp->fw_health->fatalities++;
2796 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2797 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2798 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2799 			type_str = "Non-fatal";
2800 			bp->fw_health->survivals++;
2801 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2802 		}
2803 		netif_warn(bp, hw, bp->dev,
2804 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2805 			   type_str, data1, data2,
2806 			   bp->fw_reset_min_dsecs * 100,
2807 			   bp->fw_reset_max_dsecs * 100);
2808 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2809 		break;
2810 	}
2811 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2812 		struct bnxt_fw_health *fw_health = bp->fw_health;
2813 		char *status_desc = "healthy";
2814 		u32 status;
2815 
2816 		if (!fw_health)
2817 			goto async_event_process_exit;
2818 
2819 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2820 			fw_health->enabled = false;
2821 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2822 			break;
2823 		}
2824 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2825 		fw_health->tmr_multiplier =
2826 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2827 				     bp->current_interval * 10);
2828 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2829 		if (!fw_health->enabled)
2830 			fw_health->last_fw_heartbeat =
2831 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2832 		fw_health->last_fw_reset_cnt =
2833 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2834 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2835 		if (status != BNXT_FW_STATUS_HEALTHY)
2836 			status_desc = "unhealthy";
2837 		netif_info(bp, drv, bp->dev,
2838 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2839 			   fw_health->primary ? "primary" : "backup", status,
2840 			   status_desc, fw_health->last_fw_reset_cnt);
2841 		if (!fw_health->enabled) {
2842 			/* Make sure tmr_counter is set and visible to
2843 			 * bnxt_health_check() before setting enabled to true.
2844 			 */
2845 			smp_wmb();
2846 			fw_health->enabled = true;
2847 		}
2848 		goto async_event_process_exit;
2849 	}
2850 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2851 		netif_notice(bp, hw, bp->dev,
2852 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2853 			     data1, data2);
2854 		goto async_event_process_exit;
2855 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2856 		struct bnxt_rx_ring_info *rxr;
2857 		u16 grp_idx;
2858 
2859 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2860 			goto async_event_process_exit;
2861 
2862 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2863 			    BNXT_EVENT_RING_TYPE(data2), data1);
2864 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2865 			goto async_event_process_exit;
2866 
2867 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2868 		if (grp_idx == INVALID_HW_RING_ID) {
2869 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2870 				    data1);
2871 			goto async_event_process_exit;
2872 		}
2873 		rxr = bp->bnapi[grp_idx]->rx_ring;
2874 		bnxt_sched_reset_rxr(bp, rxr);
2875 		goto async_event_process_exit;
2876 	}
2877 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2878 		struct bnxt_fw_health *fw_health = bp->fw_health;
2879 
2880 		netif_notice(bp, hw, bp->dev,
2881 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2882 			     data1, data2);
2883 		if (fw_health) {
2884 			fw_health->echo_req_data1 = data1;
2885 			fw_health->echo_req_data2 = data2;
2886 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2887 			break;
2888 		}
2889 		goto async_event_process_exit;
2890 	}
2891 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2892 		bnxt_ptp_pps_event(bp, data1, data2);
2893 		goto async_event_process_exit;
2894 	}
2895 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2896 		if (bnxt_event_error_report(bp, data1, data2))
2897 			break;
2898 		goto async_event_process_exit;
2899 	}
2900 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2901 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2902 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2903 			if (BNXT_PTP_USE_RTC(bp)) {
2904 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2905 				unsigned long flags;
2906 				u64 ns;
2907 
2908 				if (!ptp)
2909 					goto async_event_process_exit;
2910 
2911 				bnxt_ptp_update_current_time(bp);
2912 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2913 				       BNXT_PHC_BITS) | ptp->current_time);
2914 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2915 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2916 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2917 			}
2918 			break;
2919 		}
2920 		goto async_event_process_exit;
2921 	}
2922 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2923 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2924 
2925 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2926 		goto async_event_process_exit;
2927 	}
2928 	case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: {
2929 		u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1);
2930 		u32 offset =  BNXT_EVENT_BUF_PRODUCER_OFFSET(data2);
2931 
2932 		bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset);
2933 		goto async_event_process_exit;
2934 	}
2935 	default:
2936 		goto async_event_process_exit;
2937 	}
2938 	__bnxt_queue_sp_work(bp);
2939 async_event_process_exit:
2940 	bnxt_ulp_async_events(bp, cmpl);
2941 	return 0;
2942 }
2943 
2944 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2945 {
2946 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2947 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2948 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2949 				(struct hwrm_fwd_req_cmpl *)txcmp;
2950 
2951 	switch (cmpl_type) {
2952 	case CMPL_BASE_TYPE_HWRM_DONE:
2953 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2954 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2955 		break;
2956 
2957 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2958 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2959 
2960 		if ((vf_id < bp->pf.first_vf_id) ||
2961 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2962 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2963 				   vf_id);
2964 			return -EINVAL;
2965 		}
2966 
2967 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2968 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2969 		break;
2970 
2971 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2972 		bnxt_async_event_process(bp,
2973 					 (struct hwrm_async_event_cmpl *)txcmp);
2974 		break;
2975 
2976 	default:
2977 		break;
2978 	}
2979 
2980 	return 0;
2981 }
2982 
2983 static bool bnxt_vnic_is_active(struct bnxt *bp)
2984 {
2985 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
2986 
2987 	return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0;
2988 }
2989 
2990 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2991 {
2992 	struct bnxt_napi *bnapi = dev_instance;
2993 	struct bnxt *bp = bnapi->bp;
2994 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2995 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2996 
2997 	cpr->event_ctr++;
2998 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2999 	napi_schedule(&bnapi->napi);
3000 	return IRQ_HANDLED;
3001 }
3002 
3003 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
3004 {
3005 	u32 raw_cons = cpr->cp_raw_cons;
3006 	u16 cons = RING_CMP(raw_cons);
3007 	struct tx_cmp *txcmp;
3008 
3009 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3010 
3011 	return TX_CMP_VALID(txcmp, raw_cons);
3012 }
3013 
3014 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3015 			    int budget)
3016 {
3017 	struct bnxt_napi *bnapi = cpr->bnapi;
3018 	u32 raw_cons = cpr->cp_raw_cons;
3019 	bool flush_xdp = false;
3020 	u32 cons;
3021 	int rx_pkts = 0;
3022 	u8 event = 0;
3023 	struct tx_cmp *txcmp;
3024 
3025 	cpr->has_more_work = 0;
3026 	cpr->had_work_done = 1;
3027 	while (1) {
3028 		u8 cmp_type;
3029 		int rc;
3030 
3031 		cons = RING_CMP(raw_cons);
3032 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3033 
3034 		if (!TX_CMP_VALID(txcmp, raw_cons))
3035 			break;
3036 
3037 		/* The valid test of the entry must be done first before
3038 		 * reading any further.
3039 		 */
3040 		dma_rmb();
3041 		cmp_type = TX_CMP_TYPE(txcmp);
3042 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
3043 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
3044 			u32 opaque = txcmp->tx_cmp_opaque;
3045 			struct bnxt_tx_ring_info *txr;
3046 			u16 tx_freed;
3047 
3048 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
3049 			event |= BNXT_TX_CMP_EVENT;
3050 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
3051 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
3052 			else
3053 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
3054 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
3055 				   bp->tx_ring_mask;
3056 			/* return full budget so NAPI will complete. */
3057 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
3058 				rx_pkts = budget;
3059 				raw_cons = NEXT_RAW_CMP(raw_cons);
3060 				if (budget)
3061 					cpr->has_more_work = 1;
3062 				break;
3063 			}
3064 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
3065 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
3066 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
3067 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
3068 			if (likely(budget))
3069 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3070 			else
3071 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
3072 							   &event);
3073 			if (event & BNXT_REDIRECT_EVENT)
3074 				flush_xdp = true;
3075 			if (likely(rc >= 0))
3076 				rx_pkts += rc;
3077 			/* Increment rx_pkts when rc is -ENOMEM to count towards
3078 			 * the NAPI budget.  Otherwise, we may potentially loop
3079 			 * here forever if we consistently cannot allocate
3080 			 * buffers.
3081 			 */
3082 			else if (rc == -ENOMEM && budget)
3083 				rx_pkts++;
3084 			else if (rc == -EBUSY)	/* partial completion */
3085 				break;
3086 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
3087 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
3088 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
3089 			bnxt_hwrm_handler(bp, txcmp);
3090 		}
3091 		raw_cons = NEXT_RAW_CMP(raw_cons);
3092 
3093 		if (rx_pkts && rx_pkts == budget) {
3094 			cpr->has_more_work = 1;
3095 			break;
3096 		}
3097 	}
3098 
3099 	if (flush_xdp) {
3100 		xdp_do_flush();
3101 		event &= ~BNXT_REDIRECT_EVENT;
3102 	}
3103 
3104 	if (event & BNXT_TX_EVENT) {
3105 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3106 		u16 prod = txr->tx_prod;
3107 
3108 		/* Sync BD data before updating doorbell */
3109 		wmb();
3110 
3111 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3112 		event &= ~BNXT_TX_EVENT;
3113 	}
3114 
3115 	cpr->cp_raw_cons = raw_cons;
3116 	bnapi->events |= event;
3117 	return rx_pkts;
3118 }
3119 
3120 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3121 				  int budget)
3122 {
3123 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3124 		bnapi->tx_int(bp, bnapi, budget);
3125 
3126 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3127 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3128 
3129 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3130 		bnapi->events &= ~BNXT_RX_EVENT;
3131 	}
3132 	if (bnapi->events & BNXT_AGG_EVENT) {
3133 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3134 
3135 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3136 		bnapi->events &= ~BNXT_AGG_EVENT;
3137 	}
3138 }
3139 
3140 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3141 			  int budget)
3142 {
3143 	struct bnxt_napi *bnapi = cpr->bnapi;
3144 	int rx_pkts;
3145 
3146 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3147 
3148 	/* ACK completion ring before freeing tx ring and producing new
3149 	 * buffers in rx/agg rings to prevent overflowing the completion
3150 	 * ring.
3151 	 */
3152 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3153 
3154 	__bnxt_poll_work_done(bp, bnapi, budget);
3155 	return rx_pkts;
3156 }
3157 
3158 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3159 {
3160 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3161 	struct bnxt *bp = bnapi->bp;
3162 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3163 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3164 	struct tx_cmp *txcmp;
3165 	struct rx_cmp_ext *rxcmp1;
3166 	u32 cp_cons, tmp_raw_cons;
3167 	u32 raw_cons = cpr->cp_raw_cons;
3168 	bool flush_xdp = false;
3169 	u32 rx_pkts = 0;
3170 	u8 event = 0;
3171 
3172 	while (1) {
3173 		int rc;
3174 
3175 		cp_cons = RING_CMP(raw_cons);
3176 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3177 
3178 		if (!TX_CMP_VALID(txcmp, raw_cons))
3179 			break;
3180 
3181 		/* The valid test of the entry must be done first before
3182 		 * reading any further.
3183 		 */
3184 		dma_rmb();
3185 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3186 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3187 			cp_cons = RING_CMP(tmp_raw_cons);
3188 			rxcmp1 = (struct rx_cmp_ext *)
3189 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3190 
3191 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3192 				break;
3193 
3194 			/* force an error to recycle the buffer */
3195 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3196 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3197 
3198 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3199 			if (likely(rc == -EIO) && budget)
3200 				rx_pkts++;
3201 			else if (rc == -EBUSY)	/* partial completion */
3202 				break;
3203 			if (event & BNXT_REDIRECT_EVENT)
3204 				flush_xdp = true;
3205 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3206 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3207 			bnxt_hwrm_handler(bp, txcmp);
3208 		} else {
3209 			netdev_err(bp->dev,
3210 				   "Invalid completion received on special ring\n");
3211 		}
3212 		raw_cons = NEXT_RAW_CMP(raw_cons);
3213 
3214 		if (rx_pkts == budget)
3215 			break;
3216 	}
3217 
3218 	cpr->cp_raw_cons = raw_cons;
3219 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3220 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3221 
3222 	if (event & BNXT_AGG_EVENT)
3223 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3224 	if (flush_xdp)
3225 		xdp_do_flush();
3226 
3227 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3228 		napi_complete_done(napi, rx_pkts);
3229 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3230 	}
3231 	return rx_pkts;
3232 }
3233 
3234 static int bnxt_poll(struct napi_struct *napi, int budget)
3235 {
3236 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3237 	struct bnxt *bp = bnapi->bp;
3238 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3239 	int work_done = 0;
3240 
3241 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3242 		napi_complete(napi);
3243 		return 0;
3244 	}
3245 	while (1) {
3246 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3247 
3248 		if (work_done >= budget) {
3249 			if (!budget)
3250 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3251 			break;
3252 		}
3253 
3254 		if (!bnxt_has_work(bp, cpr)) {
3255 			if (napi_complete_done(napi, work_done))
3256 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3257 			break;
3258 		}
3259 	}
3260 	if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3261 		struct dim_sample dim_sample = {};
3262 
3263 		dim_update_sample(cpr->event_ctr,
3264 				  cpr->rx_packets,
3265 				  cpr->rx_bytes,
3266 				  &dim_sample);
3267 		net_dim(&cpr->dim, &dim_sample);
3268 	}
3269 	return work_done;
3270 }
3271 
3272 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3273 {
3274 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3275 	int i, work_done = 0;
3276 
3277 	for (i = 0; i < cpr->cp_ring_count; i++) {
3278 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3279 
3280 		if (cpr2->had_nqe_notify) {
3281 			work_done += __bnxt_poll_work(bp, cpr2,
3282 						      budget - work_done);
3283 			cpr->has_more_work |= cpr2->has_more_work;
3284 		}
3285 	}
3286 	return work_done;
3287 }
3288 
3289 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3290 				 u64 dbr_type, int budget)
3291 {
3292 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3293 	int i;
3294 
3295 	for (i = 0; i < cpr->cp_ring_count; i++) {
3296 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3297 		struct bnxt_db_info *db;
3298 
3299 		if (cpr2->had_work_done) {
3300 			u32 tgl = 0;
3301 
3302 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3303 				cpr2->had_nqe_notify = 0;
3304 				tgl = cpr2->toggle;
3305 			}
3306 			db = &cpr2->cp_db;
3307 			bnxt_writeq(bp,
3308 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3309 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3310 				    db->doorbell);
3311 			cpr2->had_work_done = 0;
3312 		}
3313 	}
3314 	__bnxt_poll_work_done(bp, bnapi, budget);
3315 }
3316 
3317 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3318 {
3319 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3320 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3321 	struct bnxt_cp_ring_info *cpr_rx;
3322 	u32 raw_cons = cpr->cp_raw_cons;
3323 	struct bnxt *bp = bnapi->bp;
3324 	struct nqe_cn *nqcmp;
3325 	int work_done = 0;
3326 	u32 cons;
3327 
3328 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3329 		napi_complete(napi);
3330 		return 0;
3331 	}
3332 	if (cpr->has_more_work) {
3333 		cpr->has_more_work = 0;
3334 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3335 	}
3336 	while (1) {
3337 		u16 type;
3338 
3339 		cons = RING_CMP(raw_cons);
3340 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3341 
3342 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3343 			if (cpr->has_more_work)
3344 				break;
3345 
3346 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3347 					     budget);
3348 			cpr->cp_raw_cons = raw_cons;
3349 			if (napi_complete_done(napi, work_done))
3350 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3351 						  cpr->cp_raw_cons);
3352 			goto poll_done;
3353 		}
3354 
3355 		/* The valid test of the entry must be done first before
3356 		 * reading any further.
3357 		 */
3358 		dma_rmb();
3359 
3360 		type = le16_to_cpu(nqcmp->type);
3361 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3362 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3363 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3364 			struct bnxt_cp_ring_info *cpr2;
3365 
3366 			/* No more budget for RX work */
3367 			if (budget && work_done >= budget &&
3368 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3369 				break;
3370 
3371 			idx = BNXT_NQ_HDL_IDX(idx);
3372 			cpr2 = &cpr->cp_ring_arr[idx];
3373 			cpr2->had_nqe_notify = 1;
3374 			cpr2->toggle = NQE_CN_TOGGLE(type);
3375 			work_done += __bnxt_poll_work(bp, cpr2,
3376 						      budget - work_done);
3377 			cpr->has_more_work |= cpr2->has_more_work;
3378 		} else {
3379 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3380 		}
3381 		raw_cons = NEXT_RAW_CMP(raw_cons);
3382 	}
3383 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3384 	if (raw_cons != cpr->cp_raw_cons) {
3385 		cpr->cp_raw_cons = raw_cons;
3386 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3387 	}
3388 poll_done:
3389 	cpr_rx = &cpr->cp_ring_arr[0];
3390 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3391 	    (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3392 		struct dim_sample dim_sample = {};
3393 
3394 		dim_update_sample(cpr->event_ctr,
3395 				  cpr_rx->rx_packets,
3396 				  cpr_rx->rx_bytes,
3397 				  &dim_sample);
3398 		net_dim(&cpr->dim, &dim_sample);
3399 	}
3400 	return work_done;
3401 }
3402 
3403 static void bnxt_free_one_tx_ring_skbs(struct bnxt *bp,
3404 				       struct bnxt_tx_ring_info *txr, int idx)
3405 {
3406 	int i, max_idx;
3407 	struct pci_dev *pdev = bp->pdev;
3408 
3409 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3410 
3411 	for (i = 0; i < max_idx;) {
3412 		struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[i];
3413 		struct sk_buff *skb;
3414 		int j, last;
3415 
3416 		if (idx  < bp->tx_nr_rings_xdp &&
3417 		    tx_buf->action == XDP_REDIRECT) {
3418 			dma_unmap_single(&pdev->dev,
3419 					 dma_unmap_addr(tx_buf, mapping),
3420 					 dma_unmap_len(tx_buf, len),
3421 					 DMA_TO_DEVICE);
3422 			xdp_return_frame(tx_buf->xdpf);
3423 			tx_buf->action = 0;
3424 			tx_buf->xdpf = NULL;
3425 			i++;
3426 			continue;
3427 		}
3428 
3429 		skb = tx_buf->skb;
3430 		if (!skb) {
3431 			i++;
3432 			continue;
3433 		}
3434 
3435 		tx_buf->skb = NULL;
3436 
3437 		if (tx_buf->is_push) {
3438 			dev_kfree_skb(skb);
3439 			i += 2;
3440 			continue;
3441 		}
3442 
3443 		dma_unmap_single(&pdev->dev,
3444 				 dma_unmap_addr(tx_buf, mapping),
3445 				 skb_headlen(skb),
3446 				 DMA_TO_DEVICE);
3447 
3448 		last = tx_buf->nr_frags;
3449 		i += 2;
3450 		for (j = 0; j < last; j++, i++) {
3451 			int ring_idx = i & bp->tx_ring_mask;
3452 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
3453 
3454 			tx_buf = &txr->tx_buf_ring[ring_idx];
3455 			netmem_dma_unmap_page_attrs(&pdev->dev,
3456 						    dma_unmap_addr(tx_buf,
3457 								   mapping),
3458 						    skb_frag_size(frag),
3459 						    DMA_TO_DEVICE, 0);
3460 		}
3461 		dev_kfree_skb(skb);
3462 	}
3463 	netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, idx));
3464 }
3465 
3466 static void bnxt_free_tx_skbs(struct bnxt *bp)
3467 {
3468 	int i;
3469 
3470 	if (!bp->tx_ring)
3471 		return;
3472 
3473 	for (i = 0; i < bp->tx_nr_rings; i++) {
3474 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3475 
3476 		if (!txr->tx_buf_ring)
3477 			continue;
3478 
3479 		bnxt_free_one_tx_ring_skbs(bp, txr, i);
3480 	}
3481 
3482 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
3483 		bnxt_ptp_free_txts_skbs(bp->ptp_cfg);
3484 }
3485 
3486 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3487 {
3488 	int i, max_idx;
3489 
3490 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3491 
3492 	for (i = 0; i < max_idx; i++) {
3493 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3494 		void *data = rx_buf->data;
3495 
3496 		if (!data)
3497 			continue;
3498 
3499 		rx_buf->data = NULL;
3500 		if (BNXT_RX_PAGE_MODE(bp))
3501 			page_pool_recycle_direct(rxr->page_pool, data);
3502 		else
3503 			page_pool_free_va(rxr->head_pool, data, true);
3504 	}
3505 }
3506 
3507 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3508 {
3509 	int i, max_idx;
3510 
3511 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3512 
3513 	for (i = 0; i < max_idx; i++) {
3514 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3515 		netmem_ref netmem = rx_agg_buf->netmem;
3516 
3517 		if (!netmem)
3518 			continue;
3519 
3520 		rx_agg_buf->netmem = 0;
3521 		__clear_bit(i, rxr->rx_agg_bmap);
3522 
3523 		page_pool_recycle_direct_netmem(rxr->page_pool, netmem);
3524 	}
3525 }
3526 
3527 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3528 					struct bnxt_rx_ring_info *rxr)
3529 {
3530 	int i;
3531 
3532 	for (i = 0; i < bp->max_tpa; i++) {
3533 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3534 		u8 *data = tpa_info->data;
3535 
3536 		if (!data)
3537 			continue;
3538 
3539 		tpa_info->data = NULL;
3540 		page_pool_free_va(rxr->head_pool, data, false);
3541 	}
3542 }
3543 
3544 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3545 				       struct bnxt_rx_ring_info *rxr)
3546 {
3547 	struct bnxt_tpa_idx_map *map;
3548 
3549 	if (!rxr->rx_tpa)
3550 		goto skip_rx_tpa_free;
3551 
3552 	bnxt_free_one_tpa_info_data(bp, rxr);
3553 
3554 skip_rx_tpa_free:
3555 	if (!rxr->rx_buf_ring)
3556 		goto skip_rx_buf_free;
3557 
3558 	bnxt_free_one_rx_ring(bp, rxr);
3559 
3560 skip_rx_buf_free:
3561 	if (!rxr->rx_agg_ring)
3562 		goto skip_rx_agg_free;
3563 
3564 	bnxt_free_one_rx_agg_ring(bp, rxr);
3565 
3566 skip_rx_agg_free:
3567 	map = rxr->rx_tpa_idx_map;
3568 	if (map)
3569 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3570 }
3571 
3572 static void bnxt_free_rx_skbs(struct bnxt *bp)
3573 {
3574 	int i;
3575 
3576 	if (!bp->rx_ring)
3577 		return;
3578 
3579 	for (i = 0; i < bp->rx_nr_rings; i++)
3580 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3581 }
3582 
3583 static void bnxt_free_skbs(struct bnxt *bp)
3584 {
3585 	bnxt_free_tx_skbs(bp);
3586 	bnxt_free_rx_skbs(bp);
3587 }
3588 
3589 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3590 {
3591 	u8 init_val = ctxm->init_value;
3592 	u16 offset = ctxm->init_offset;
3593 	u8 *p2 = p;
3594 	int i;
3595 
3596 	if (!init_val)
3597 		return;
3598 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3599 		memset(p, init_val, len);
3600 		return;
3601 	}
3602 	for (i = 0; i < len; i += ctxm->entry_size)
3603 		*(p2 + i + offset) = init_val;
3604 }
3605 
3606 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3607 			       void *buf, size_t offset, size_t head,
3608 			       size_t tail)
3609 {
3610 	int i, head_page, start_idx, source_offset;
3611 	size_t len, rem_len, total_len, max_bytes;
3612 
3613 	head_page = head / rmem->page_size;
3614 	source_offset = head % rmem->page_size;
3615 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3616 	if (!total_len)
3617 		total_len = MAX_CTX_BYTES;
3618 	start_idx = head_page % MAX_CTX_PAGES;
3619 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3620 		    source_offset;
3621 	total_len = min(total_len, max_bytes);
3622 	rem_len = total_len;
3623 
3624 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3625 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3626 		if (buf)
3627 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3628 			       len);
3629 		offset += len;
3630 		rem_len -= len;
3631 	}
3632 	return total_len;
3633 }
3634 
3635 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3636 {
3637 	struct pci_dev *pdev = bp->pdev;
3638 	int i;
3639 
3640 	if (!rmem->pg_arr)
3641 		goto skip_pages;
3642 
3643 	for (i = 0; i < rmem->nr_pages; i++) {
3644 		if (!rmem->pg_arr[i])
3645 			continue;
3646 
3647 		dma_free_coherent(&pdev->dev, rmem->page_size,
3648 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3649 
3650 		rmem->pg_arr[i] = NULL;
3651 	}
3652 skip_pages:
3653 	if (rmem->pg_tbl) {
3654 		size_t pg_tbl_size = rmem->nr_pages * 8;
3655 
3656 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3657 			pg_tbl_size = rmem->page_size;
3658 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3659 				  rmem->pg_tbl, rmem->pg_tbl_map);
3660 		rmem->pg_tbl = NULL;
3661 	}
3662 	if (rmem->vmem_size && *rmem->vmem) {
3663 		vfree(*rmem->vmem);
3664 		*rmem->vmem = NULL;
3665 	}
3666 }
3667 
3668 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3669 {
3670 	struct pci_dev *pdev = bp->pdev;
3671 	u64 valid_bit = 0;
3672 	int i;
3673 
3674 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3675 		valid_bit = PTU_PTE_VALID;
3676 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3677 		size_t pg_tbl_size = rmem->nr_pages * 8;
3678 
3679 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3680 			pg_tbl_size = rmem->page_size;
3681 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3682 						  &rmem->pg_tbl_map,
3683 						  GFP_KERNEL);
3684 		if (!rmem->pg_tbl)
3685 			return -ENOMEM;
3686 	}
3687 
3688 	for (i = 0; i < rmem->nr_pages; i++) {
3689 		u64 extra_bits = valid_bit;
3690 
3691 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3692 						     rmem->page_size,
3693 						     &rmem->dma_arr[i],
3694 						     GFP_KERNEL);
3695 		if (!rmem->pg_arr[i])
3696 			return -ENOMEM;
3697 
3698 		if (rmem->ctx_mem)
3699 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3700 					  rmem->page_size);
3701 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3702 			if (i == rmem->nr_pages - 2 &&
3703 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3704 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3705 			else if (i == rmem->nr_pages - 1 &&
3706 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3707 				extra_bits |= PTU_PTE_LAST;
3708 			rmem->pg_tbl[i] =
3709 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3710 		}
3711 	}
3712 
3713 	if (rmem->vmem_size) {
3714 		*rmem->vmem = vzalloc(rmem->vmem_size);
3715 		if (!(*rmem->vmem))
3716 			return -ENOMEM;
3717 	}
3718 	return 0;
3719 }
3720 
3721 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3722 				   struct bnxt_rx_ring_info *rxr)
3723 {
3724 	int i;
3725 
3726 	kfree(rxr->rx_tpa_idx_map);
3727 	rxr->rx_tpa_idx_map = NULL;
3728 	if (rxr->rx_tpa) {
3729 		for (i = 0; i < bp->max_tpa; i++) {
3730 			kfree(rxr->rx_tpa[i].agg_arr);
3731 			rxr->rx_tpa[i].agg_arr = NULL;
3732 		}
3733 	}
3734 	kfree(rxr->rx_tpa);
3735 	rxr->rx_tpa = NULL;
3736 }
3737 
3738 static void bnxt_free_tpa_info(struct bnxt *bp)
3739 {
3740 	int i;
3741 
3742 	for (i = 0; i < bp->rx_nr_rings; i++) {
3743 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3744 
3745 		bnxt_free_one_tpa_info(bp, rxr);
3746 	}
3747 }
3748 
3749 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3750 				   struct bnxt_rx_ring_info *rxr)
3751 {
3752 	struct rx_agg_cmp *agg;
3753 	int i;
3754 
3755 	rxr->rx_tpa = kzalloc_objs(struct bnxt_tpa_info, bp->max_tpa);
3756 	if (!rxr->rx_tpa)
3757 		return -ENOMEM;
3758 
3759 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3760 		return 0;
3761 	for (i = 0; i < bp->max_tpa; i++) {
3762 		agg = kzalloc_objs(*agg, MAX_SKB_FRAGS);
3763 		if (!agg)
3764 			return -ENOMEM;
3765 		rxr->rx_tpa[i].agg_arr = agg;
3766 	}
3767 	rxr->rx_tpa_idx_map = kzalloc_obj(*rxr->rx_tpa_idx_map);
3768 	if (!rxr->rx_tpa_idx_map)
3769 		return -ENOMEM;
3770 
3771 	return 0;
3772 }
3773 
3774 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3775 {
3776 	int i, rc;
3777 
3778 	bp->max_tpa = MAX_TPA;
3779 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3780 		if (!bp->max_tpa_v2)
3781 			return 0;
3782 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3783 	}
3784 
3785 	for (i = 0; i < bp->rx_nr_rings; i++) {
3786 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3787 
3788 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3789 		if (rc)
3790 			return rc;
3791 	}
3792 	return 0;
3793 }
3794 
3795 static void bnxt_free_rx_rings(struct bnxt *bp)
3796 {
3797 	int i;
3798 
3799 	if (!bp->rx_ring)
3800 		return;
3801 
3802 	bnxt_free_tpa_info(bp);
3803 	for (i = 0; i < bp->rx_nr_rings; i++) {
3804 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3805 		struct bnxt_ring_struct *ring;
3806 
3807 		if (rxr->xdp_prog)
3808 			bpf_prog_put(rxr->xdp_prog);
3809 
3810 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3811 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3812 
3813 		page_pool_destroy(rxr->page_pool);
3814 		page_pool_destroy(rxr->head_pool);
3815 		rxr->page_pool = rxr->head_pool = NULL;
3816 
3817 		kfree(rxr->rx_agg_bmap);
3818 		rxr->rx_agg_bmap = NULL;
3819 
3820 		ring = &rxr->rx_ring_struct;
3821 		bnxt_free_ring(bp, &ring->ring_mem);
3822 
3823 		ring = &rxr->rx_agg_ring_struct;
3824 		bnxt_free_ring(bp, &ring->ring_mem);
3825 	}
3826 }
3827 
3828 static int bnxt_rx_agg_ring_fill_level(struct bnxt *bp,
3829 				       struct bnxt_rx_ring_info *rxr)
3830 {
3831 	/* User may have chosen larger than default rx_page_size,
3832 	 * we keep the ring sizes uniform and also want uniform amount
3833 	 * of bytes consumed per ring, so cap how much of the rings we fill.
3834 	 */
3835 	int fill_level = bp->rx_agg_ring_size;
3836 
3837 	if (rxr->rx_page_size > BNXT_RX_PAGE_SIZE)
3838 		fill_level /= rxr->rx_page_size / BNXT_RX_PAGE_SIZE;
3839 
3840 	return fill_level;
3841 }
3842 
3843 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3844 				   struct bnxt_rx_ring_info *rxr,
3845 				   int numa_node)
3846 {
3847 	unsigned int agg_size_fac = rxr->rx_page_size / BNXT_RX_PAGE_SIZE;
3848 	const unsigned int rx_size_fac = PAGE_SIZE / SZ_4K;
3849 	struct page_pool_params pp = { 0 };
3850 	struct page_pool *pool;
3851 
3852 	pp.pool_size = bnxt_rx_agg_ring_fill_level(bp, rxr) / agg_size_fac;
3853 	if (BNXT_RX_PAGE_MODE(bp))
3854 		pp.pool_size += bp->rx_ring_size / rx_size_fac;
3855 
3856 	pp.order = get_order(rxr->rx_page_size);
3857 	pp.nid = numa_node;
3858 	pp.netdev = bp->dev;
3859 	pp.dev = &bp->pdev->dev;
3860 	pp.dma_dir = bp->rx_dir;
3861 	pp.max_len = PAGE_SIZE << pp.order;
3862 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV |
3863 		   PP_FLAG_ALLOW_UNREADABLE_NETMEM;
3864 	pp.queue_idx = rxr->bnapi->index;
3865 
3866 	pool = page_pool_create(&pp);
3867 	if (IS_ERR(pool))
3868 		return PTR_ERR(pool);
3869 	rxr->page_pool = pool;
3870 
3871 	rxr->need_head_pool = page_pool_is_unreadable(pool);
3872 	rxr->need_head_pool |= !!pp.order;
3873 	if (bnxt_separate_head_pool(rxr)) {
3874 		pp.order = 0;
3875 		pp.max_len = PAGE_SIZE;
3876 		pp.pool_size = min(bp->rx_ring_size / rx_size_fac, 1024);
3877 		pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3878 		pool = page_pool_create(&pp);
3879 		if (IS_ERR(pool))
3880 			goto err_destroy_pp;
3881 	} else {
3882 		page_pool_get(pool);
3883 	}
3884 	rxr->head_pool = pool;
3885 
3886 	return 0;
3887 
3888 err_destroy_pp:
3889 	page_pool_destroy(rxr->page_pool);
3890 	rxr->page_pool = NULL;
3891 	return PTR_ERR(pool);
3892 }
3893 
3894 static void bnxt_enable_rx_page_pool(struct bnxt_rx_ring_info *rxr)
3895 {
3896 	page_pool_enable_direct_recycling(rxr->head_pool, &rxr->bnapi->napi);
3897 	page_pool_enable_direct_recycling(rxr->page_pool, &rxr->bnapi->napi);
3898 }
3899 
3900 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3901 {
3902 	u16 mem_size;
3903 
3904 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3905 	mem_size = rxr->rx_agg_bmap_size / 8;
3906 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3907 	if (!rxr->rx_agg_bmap)
3908 		return -ENOMEM;
3909 
3910 	return 0;
3911 }
3912 
3913 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3914 {
3915 	int numa_node = dev_to_node(&bp->pdev->dev);
3916 	int i, rc = 0, agg_rings = 0, cpu;
3917 
3918 	if (!bp->rx_ring)
3919 		return -ENOMEM;
3920 
3921 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3922 		agg_rings = 1;
3923 
3924 	for (i = 0; i < bp->rx_nr_rings; i++) {
3925 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3926 		struct bnxt_ring_struct *ring;
3927 		int cpu_node;
3928 
3929 		ring = &rxr->rx_ring_struct;
3930 
3931 		cpu = cpumask_local_spread(i, numa_node);
3932 		cpu_node = cpu_to_node(cpu);
3933 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3934 			   i, cpu_node);
3935 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3936 		if (rc)
3937 			return rc;
3938 		bnxt_enable_rx_page_pool(rxr);
3939 
3940 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3941 		if (rc < 0)
3942 			return rc;
3943 
3944 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3945 						MEM_TYPE_PAGE_POOL,
3946 						rxr->page_pool);
3947 		if (rc) {
3948 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3949 			return rc;
3950 		}
3951 
3952 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3953 		if (rc)
3954 			return rc;
3955 
3956 		ring->grp_idx = i;
3957 		if (agg_rings) {
3958 			ring = &rxr->rx_agg_ring_struct;
3959 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3960 			if (rc)
3961 				return rc;
3962 
3963 			ring->grp_idx = i;
3964 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
3965 			if (rc)
3966 				return rc;
3967 		}
3968 	}
3969 	if (bp->flags & BNXT_FLAG_TPA)
3970 		rc = bnxt_alloc_tpa_info(bp);
3971 	return rc;
3972 }
3973 
3974 static void bnxt_free_tx_rings(struct bnxt *bp)
3975 {
3976 	int i;
3977 	struct pci_dev *pdev = bp->pdev;
3978 
3979 	if (!bp->tx_ring)
3980 		return;
3981 
3982 	for (i = 0; i < bp->tx_nr_rings; i++) {
3983 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3984 		struct bnxt_ring_struct *ring;
3985 
3986 		if (txr->tx_push) {
3987 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3988 					  txr->tx_push, txr->tx_push_mapping);
3989 			txr->tx_push = NULL;
3990 		}
3991 
3992 		ring = &txr->tx_ring_struct;
3993 
3994 		bnxt_free_ring(bp, &ring->ring_mem);
3995 	}
3996 }
3997 
3998 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3999 	((tc) * (bp)->tx_nr_rings_per_tc)
4000 
4001 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
4002 	((tx) % (bp)->tx_nr_rings_per_tc)
4003 
4004 #define BNXT_RING_TO_TC(bp, tx)		\
4005 	((tx) / (bp)->tx_nr_rings_per_tc)
4006 
4007 static int bnxt_alloc_tx_rings(struct bnxt *bp)
4008 {
4009 	int i, j, rc;
4010 	struct pci_dev *pdev = bp->pdev;
4011 
4012 	bp->tx_push_size = 0;
4013 	if (bp->tx_push_thresh) {
4014 		int push_size;
4015 
4016 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
4017 					bp->tx_push_thresh);
4018 
4019 		if (push_size > 256) {
4020 			push_size = 0;
4021 			bp->tx_push_thresh = 0;
4022 		}
4023 
4024 		bp->tx_push_size = push_size;
4025 	}
4026 
4027 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
4028 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4029 		struct bnxt_ring_struct *ring;
4030 		u8 qidx;
4031 
4032 		ring = &txr->tx_ring_struct;
4033 
4034 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4035 		if (rc)
4036 			return rc;
4037 
4038 		ring->grp_idx = txr->bnapi->index;
4039 		if (bp->tx_push_size) {
4040 			dma_addr_t mapping;
4041 
4042 			/* One pre-allocated DMA buffer to backup
4043 			 * TX push operation
4044 			 */
4045 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
4046 						bp->tx_push_size,
4047 						&txr->tx_push_mapping,
4048 						GFP_KERNEL);
4049 
4050 			if (!txr->tx_push)
4051 				return -ENOMEM;
4052 
4053 			mapping = txr->tx_push_mapping +
4054 				sizeof(struct tx_push_bd);
4055 			txr->data_mapping = cpu_to_le64(mapping);
4056 		}
4057 		qidx = bp->tc_to_qidx[j];
4058 		ring->queue_id = bp->q_info[qidx].queue_id;
4059 		spin_lock_init(&txr->xdp_tx_lock);
4060 		if (i < bp->tx_nr_rings_xdp)
4061 			continue;
4062 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
4063 			j++;
4064 	}
4065 	return 0;
4066 }
4067 
4068 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
4069 {
4070 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4071 
4072 	kfree(cpr->cp_desc_ring);
4073 	cpr->cp_desc_ring = NULL;
4074 	ring->ring_mem.pg_arr = NULL;
4075 	kfree(cpr->cp_desc_mapping);
4076 	cpr->cp_desc_mapping = NULL;
4077 	ring->ring_mem.dma_arr = NULL;
4078 }
4079 
4080 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
4081 {
4082 	cpr->cp_desc_ring = kzalloc_objs(*cpr->cp_desc_ring, n);
4083 	if (!cpr->cp_desc_ring)
4084 		return -ENOMEM;
4085 	cpr->cp_desc_mapping = kzalloc_objs(*cpr->cp_desc_mapping, n);
4086 	if (!cpr->cp_desc_mapping)
4087 		return -ENOMEM;
4088 	return 0;
4089 }
4090 
4091 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
4092 {
4093 	int i;
4094 
4095 	if (!bp->bnapi)
4096 		return;
4097 	for (i = 0; i < bp->cp_nr_rings; i++) {
4098 		struct bnxt_napi *bnapi = bp->bnapi[i];
4099 
4100 		if (!bnapi)
4101 			continue;
4102 		bnxt_free_cp_arrays(&bnapi->cp_ring);
4103 	}
4104 }
4105 
4106 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
4107 {
4108 	int i, n = bp->cp_nr_pages;
4109 
4110 	for (i = 0; i < bp->cp_nr_rings; i++) {
4111 		struct bnxt_napi *bnapi = bp->bnapi[i];
4112 		int rc;
4113 
4114 		if (!bnapi)
4115 			continue;
4116 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
4117 		if (rc)
4118 			return rc;
4119 	}
4120 	return 0;
4121 }
4122 
4123 static void bnxt_free_cp_rings(struct bnxt *bp)
4124 {
4125 	int i;
4126 
4127 	if (!bp->bnapi)
4128 		return;
4129 
4130 	for (i = 0; i < bp->cp_nr_rings; i++) {
4131 		struct bnxt_napi *bnapi = bp->bnapi[i];
4132 		struct bnxt_cp_ring_info *cpr;
4133 		struct bnxt_ring_struct *ring;
4134 		int j;
4135 
4136 		if (!bnapi)
4137 			continue;
4138 
4139 		cpr = &bnapi->cp_ring;
4140 		ring = &cpr->cp_ring_struct;
4141 
4142 		bnxt_free_ring(bp, &ring->ring_mem);
4143 
4144 		if (!cpr->cp_ring_arr)
4145 			continue;
4146 
4147 		for (j = 0; j < cpr->cp_ring_count; j++) {
4148 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4149 
4150 			ring = &cpr2->cp_ring_struct;
4151 			bnxt_free_ring(bp, &ring->ring_mem);
4152 			bnxt_free_cp_arrays(cpr2);
4153 		}
4154 		kfree(cpr->cp_ring_arr);
4155 		cpr->cp_ring_arr = NULL;
4156 		cpr->cp_ring_count = 0;
4157 	}
4158 }
4159 
4160 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4161 				  struct bnxt_cp_ring_info *cpr)
4162 {
4163 	struct bnxt_ring_mem_info *rmem;
4164 	struct bnxt_ring_struct *ring;
4165 	int rc;
4166 
4167 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4168 	if (rc) {
4169 		bnxt_free_cp_arrays(cpr);
4170 		return -ENOMEM;
4171 	}
4172 	ring = &cpr->cp_ring_struct;
4173 	rmem = &ring->ring_mem;
4174 	rmem->nr_pages = bp->cp_nr_pages;
4175 	rmem->page_size = HW_CMPD_RING_SIZE;
4176 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4177 	rmem->dma_arr = cpr->cp_desc_mapping;
4178 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4179 	rc = bnxt_alloc_ring(bp, rmem);
4180 	if (rc) {
4181 		bnxt_free_ring(bp, rmem);
4182 		bnxt_free_cp_arrays(cpr);
4183 	}
4184 	return rc;
4185 }
4186 
4187 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4188 {
4189 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4190 	int i, j, rc, ulp_msix;
4191 	int tcs = bp->num_tc;
4192 
4193 	if (!tcs)
4194 		tcs = 1;
4195 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4196 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4197 		struct bnxt_napi *bnapi = bp->bnapi[i];
4198 		struct bnxt_cp_ring_info *cpr, *cpr2;
4199 		struct bnxt_ring_struct *ring;
4200 		int cp_count = 0, k;
4201 		int rx = 0, tx = 0;
4202 
4203 		if (!bnapi)
4204 			continue;
4205 
4206 		cpr = &bnapi->cp_ring;
4207 		cpr->bnapi = bnapi;
4208 		ring = &cpr->cp_ring_struct;
4209 
4210 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4211 		if (rc)
4212 			return rc;
4213 
4214 		ring->map_idx = ulp_msix + i;
4215 
4216 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4217 			continue;
4218 
4219 		if (i < bp->rx_nr_rings) {
4220 			cp_count++;
4221 			rx = 1;
4222 		}
4223 		if (i < bp->tx_nr_rings_xdp) {
4224 			cp_count++;
4225 			tx = 1;
4226 		} else if ((sh && i < bp->tx_nr_rings) ||
4227 			 (!sh && i >= bp->rx_nr_rings)) {
4228 			cp_count += tcs;
4229 			tx = 1;
4230 		}
4231 
4232 		cpr->cp_ring_arr = kzalloc_objs(*cpr, cp_count);
4233 		if (!cpr->cp_ring_arr)
4234 			return -ENOMEM;
4235 		cpr->cp_ring_count = cp_count;
4236 
4237 		for (k = 0; k < cp_count; k++) {
4238 			cpr2 = &cpr->cp_ring_arr[k];
4239 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4240 			if (rc)
4241 				return rc;
4242 			cpr2->bnapi = bnapi;
4243 			cpr2->sw_stats = cpr->sw_stats;
4244 			cpr2->cp_idx = k;
4245 			if (!k && rx) {
4246 				bp->rx_ring[i].rx_cpr = cpr2;
4247 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4248 			} else {
4249 				int n, tc = k - rx;
4250 
4251 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4252 				bp->tx_ring[n].tx_cpr = cpr2;
4253 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4254 			}
4255 		}
4256 		if (tx)
4257 			j++;
4258 	}
4259 	return 0;
4260 }
4261 
4262 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4263 				     struct bnxt_rx_ring_info *rxr)
4264 {
4265 	struct bnxt_ring_mem_info *rmem;
4266 	struct bnxt_ring_struct *ring;
4267 
4268 	ring = &rxr->rx_ring_struct;
4269 	rmem = &ring->ring_mem;
4270 	rmem->nr_pages = bp->rx_nr_pages;
4271 	rmem->page_size = HW_RXBD_RING_SIZE;
4272 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4273 	rmem->dma_arr = rxr->rx_desc_mapping;
4274 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4275 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4276 
4277 	ring = &rxr->rx_agg_ring_struct;
4278 	rmem = &ring->ring_mem;
4279 	rmem->nr_pages = bp->rx_agg_nr_pages;
4280 	rmem->page_size = HW_RXBD_RING_SIZE;
4281 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4282 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4283 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4284 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4285 }
4286 
4287 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4288 				      struct bnxt_rx_ring_info *rxr)
4289 {
4290 	struct bnxt_ring_mem_info *rmem;
4291 	struct bnxt_ring_struct *ring;
4292 	int i;
4293 
4294 	rxr->page_pool->p.napi = NULL;
4295 	rxr->page_pool = NULL;
4296 	rxr->head_pool->p.napi = NULL;
4297 	rxr->head_pool = NULL;
4298 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4299 
4300 	ring = &rxr->rx_ring_struct;
4301 	rmem = &ring->ring_mem;
4302 	rmem->pg_tbl = NULL;
4303 	rmem->pg_tbl_map = 0;
4304 	for (i = 0; i < rmem->nr_pages; i++) {
4305 		rmem->pg_arr[i] = NULL;
4306 		rmem->dma_arr[i] = 0;
4307 	}
4308 	*rmem->vmem = NULL;
4309 
4310 	ring = &rxr->rx_agg_ring_struct;
4311 	rmem = &ring->ring_mem;
4312 	rmem->pg_tbl = NULL;
4313 	rmem->pg_tbl_map = 0;
4314 	for (i = 0; i < rmem->nr_pages; i++) {
4315 		rmem->pg_arr[i] = NULL;
4316 		rmem->dma_arr[i] = 0;
4317 	}
4318 	*rmem->vmem = NULL;
4319 }
4320 
4321 static void bnxt_init_ring_struct(struct bnxt *bp)
4322 {
4323 	int i, j;
4324 
4325 	for (i = 0; i < bp->cp_nr_rings; i++) {
4326 		struct bnxt_napi *bnapi = bp->bnapi[i];
4327 		struct netdev_queue_config qcfg;
4328 		struct bnxt_ring_mem_info *rmem;
4329 		struct bnxt_cp_ring_info *cpr;
4330 		struct bnxt_rx_ring_info *rxr;
4331 		struct bnxt_tx_ring_info *txr;
4332 		struct bnxt_ring_struct *ring;
4333 
4334 		if (!bnapi)
4335 			continue;
4336 
4337 		cpr = &bnapi->cp_ring;
4338 		ring = &cpr->cp_ring_struct;
4339 		rmem = &ring->ring_mem;
4340 		rmem->nr_pages = bp->cp_nr_pages;
4341 		rmem->page_size = HW_CMPD_RING_SIZE;
4342 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4343 		rmem->dma_arr = cpr->cp_desc_mapping;
4344 		rmem->vmem_size = 0;
4345 
4346 		rxr = bnapi->rx_ring;
4347 		if (!rxr)
4348 			goto skip_rx;
4349 
4350 		netdev_queue_config(bp->dev, i, &qcfg);
4351 		rxr->rx_page_size = qcfg.rx_page_size;
4352 
4353 		ring = &rxr->rx_ring_struct;
4354 		rmem = &ring->ring_mem;
4355 		rmem->nr_pages = bp->rx_nr_pages;
4356 		rmem->page_size = HW_RXBD_RING_SIZE;
4357 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4358 		rmem->dma_arr = rxr->rx_desc_mapping;
4359 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4360 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4361 
4362 		ring = &rxr->rx_agg_ring_struct;
4363 		rmem = &ring->ring_mem;
4364 		rmem->nr_pages = bp->rx_agg_nr_pages;
4365 		rmem->page_size = HW_RXBD_RING_SIZE;
4366 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4367 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4368 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4369 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4370 
4371 skip_rx:
4372 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4373 			ring = &txr->tx_ring_struct;
4374 			rmem = &ring->ring_mem;
4375 			rmem->nr_pages = bp->tx_nr_pages;
4376 			rmem->page_size = HW_TXBD_RING_SIZE;
4377 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4378 			rmem->dma_arr = txr->tx_desc_mapping;
4379 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4380 			rmem->vmem = (void **)&txr->tx_buf_ring;
4381 		}
4382 	}
4383 }
4384 
4385 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4386 {
4387 	int i;
4388 	u32 prod;
4389 	struct rx_bd **rx_buf_ring;
4390 
4391 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4392 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4393 		int j;
4394 		struct rx_bd *rxbd;
4395 
4396 		rxbd = rx_buf_ring[i];
4397 		if (!rxbd)
4398 			continue;
4399 
4400 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4401 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4402 			rxbd->rx_bd_opaque = prod;
4403 		}
4404 	}
4405 }
4406 
4407 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4408 				       struct bnxt_rx_ring_info *rxr,
4409 				       int ring_nr)
4410 {
4411 	u32 prod;
4412 	int i;
4413 
4414 	prod = rxr->rx_prod;
4415 	for (i = 0; i < bp->rx_ring_size; i++) {
4416 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4417 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4418 				    ring_nr, i, bp->rx_ring_size);
4419 			break;
4420 		}
4421 		prod = NEXT_RX(prod);
4422 	}
4423 	rxr->rx_prod = prod;
4424 }
4425 
4426 static void bnxt_alloc_one_rx_ring_netmem(struct bnxt *bp,
4427 					  struct bnxt_rx_ring_info *rxr,
4428 					  int ring_nr)
4429 {
4430 	int fill_level, i;
4431 	u32 prod;
4432 
4433 	fill_level = bnxt_rx_agg_ring_fill_level(bp, rxr);
4434 
4435 	prod = rxr->rx_agg_prod;
4436 	for (i = 0; i < fill_level; i++) {
4437 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_KERNEL)) {
4438 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4439 				    ring_nr, i, bp->rx_agg_ring_size);
4440 			break;
4441 		}
4442 		prod = NEXT_RX_AGG(prod);
4443 	}
4444 	rxr->rx_agg_prod = prod;
4445 }
4446 
4447 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4448 					struct bnxt_rx_ring_info *rxr)
4449 {
4450 	dma_addr_t mapping;
4451 	u8 *data;
4452 	int i;
4453 
4454 	for (i = 0; i < bp->max_tpa; i++) {
4455 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4456 					    GFP_KERNEL);
4457 		if (!data)
4458 			return -ENOMEM;
4459 
4460 		rxr->rx_tpa[i].data = data;
4461 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4462 		rxr->rx_tpa[i].mapping = mapping;
4463 	}
4464 
4465 	return 0;
4466 }
4467 
4468 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4469 {
4470 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4471 	int rc;
4472 
4473 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4474 
4475 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4476 		return 0;
4477 
4478 	bnxt_alloc_one_rx_ring_netmem(bp, rxr, ring_nr);
4479 
4480 	if (rxr->rx_tpa) {
4481 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4482 		if (rc)
4483 			return rc;
4484 	}
4485 	return 0;
4486 }
4487 
4488 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4489 				       struct bnxt_rx_ring_info *rxr)
4490 {
4491 	struct bnxt_ring_struct *ring;
4492 	u32 type;
4493 
4494 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4495 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4496 
4497 	if (NET_IP_ALIGN == 2)
4498 		type |= RX_BD_FLAGS_SOP;
4499 
4500 	ring = &rxr->rx_ring_struct;
4501 	bnxt_init_rxbd_pages(ring, type);
4502 	ring->fw_ring_id = INVALID_HW_RING_ID;
4503 }
4504 
4505 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4506 					   struct bnxt_rx_ring_info *rxr)
4507 {
4508 	struct bnxt_ring_struct *ring;
4509 	u32 type;
4510 
4511 	ring = &rxr->rx_agg_ring_struct;
4512 	ring->fw_ring_id = INVALID_HW_RING_ID;
4513 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4514 		type = ((u32)rxr->rx_page_size << RX_BD_LEN_SHIFT) |
4515 			RX_BD_TYPE_RX_AGG_BD;
4516 
4517 		/* On P7, setting EOP will cause the chip to disable
4518 		 * Relaxed Ordering (RO) for TPA data.  Disable EOP for
4519 		 * potentially higher performance with RO.
4520 		 */
4521 		if (BNXT_CHIP_P5_AND_MINUS(bp) || !(bp->flags & BNXT_FLAG_TPA))
4522 			type |= RX_BD_FLAGS_AGG_EOP;
4523 
4524 		bnxt_init_rxbd_pages(ring, type);
4525 	}
4526 }
4527 
4528 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4529 {
4530 	struct bnxt_rx_ring_info *rxr;
4531 
4532 	rxr = &bp->rx_ring[ring_nr];
4533 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4534 
4535 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4536 			     &rxr->bnapi->napi);
4537 
4538 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4539 		bpf_prog_add(bp->xdp_prog, 1);
4540 		rxr->xdp_prog = bp->xdp_prog;
4541 	}
4542 
4543 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4544 
4545 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4546 }
4547 
4548 static void bnxt_init_cp_rings(struct bnxt *bp)
4549 {
4550 	int i, j;
4551 
4552 	for (i = 0; i < bp->cp_nr_rings; i++) {
4553 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4554 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4555 
4556 		ring->fw_ring_id = INVALID_HW_RING_ID;
4557 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4558 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4559 		if (!cpr->cp_ring_arr)
4560 			continue;
4561 		for (j = 0; j < cpr->cp_ring_count; j++) {
4562 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4563 
4564 			ring = &cpr2->cp_ring_struct;
4565 			ring->fw_ring_id = INVALID_HW_RING_ID;
4566 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4567 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4568 		}
4569 	}
4570 }
4571 
4572 static int bnxt_init_rx_rings(struct bnxt *bp)
4573 {
4574 	int i, rc = 0;
4575 
4576 	if (BNXT_RX_PAGE_MODE(bp)) {
4577 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4578 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4579 	} else {
4580 		bp->rx_offset = BNXT_RX_OFFSET;
4581 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4582 	}
4583 
4584 	for (i = 0; i < bp->rx_nr_rings; i++) {
4585 		rc = bnxt_init_one_rx_ring(bp, i);
4586 		if (rc)
4587 			break;
4588 	}
4589 
4590 	return rc;
4591 }
4592 
4593 static int bnxt_init_tx_rings(struct bnxt *bp)
4594 {
4595 	u16 i;
4596 
4597 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4598 				   BNXT_MIN_TX_DESC_CNT);
4599 
4600 	for (i = 0; i < bp->tx_nr_rings; i++) {
4601 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4602 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4603 
4604 		ring->fw_ring_id = INVALID_HW_RING_ID;
4605 
4606 		if (i >= bp->tx_nr_rings_xdp)
4607 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4608 					     NETDEV_QUEUE_TYPE_TX,
4609 					     &txr->bnapi->napi);
4610 	}
4611 
4612 	return 0;
4613 }
4614 
4615 static void bnxt_free_ring_grps(struct bnxt *bp)
4616 {
4617 	kfree(bp->grp_info);
4618 	bp->grp_info = NULL;
4619 }
4620 
4621 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4622 {
4623 	int i;
4624 
4625 	if (irq_re_init) {
4626 		bp->grp_info = kzalloc_objs(struct bnxt_ring_grp_info,
4627 					    bp->cp_nr_rings);
4628 		if (!bp->grp_info)
4629 			return -ENOMEM;
4630 	}
4631 	for (i = 0; i < bp->cp_nr_rings; i++) {
4632 		if (irq_re_init)
4633 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4634 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4635 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4636 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4637 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4638 	}
4639 	return 0;
4640 }
4641 
4642 static void bnxt_free_vnics(struct bnxt *bp)
4643 {
4644 	kfree(bp->vnic_info);
4645 	bp->vnic_info = NULL;
4646 	bp->nr_vnics = 0;
4647 }
4648 
4649 static int bnxt_alloc_vnics(struct bnxt *bp)
4650 {
4651 	int num_vnics = 1;
4652 
4653 #ifdef CONFIG_RFS_ACCEL
4654 	if (bp->flags & BNXT_FLAG_RFS) {
4655 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4656 			num_vnics++;
4657 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4658 			num_vnics += bp->rx_nr_rings;
4659 	}
4660 #endif
4661 
4662 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4663 		num_vnics++;
4664 
4665 	bp->vnic_info = kzalloc_objs(struct bnxt_vnic_info, num_vnics);
4666 	if (!bp->vnic_info)
4667 		return -ENOMEM;
4668 
4669 	bp->nr_vnics = num_vnics;
4670 	return 0;
4671 }
4672 
4673 static void bnxt_init_vnics(struct bnxt *bp)
4674 {
4675 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4676 	int i;
4677 
4678 	for (i = 0; i < bp->nr_vnics; i++) {
4679 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4680 		int j;
4681 
4682 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4683 		vnic->vnic_id = i;
4684 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4685 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4686 
4687 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4688 
4689 		if (bp->vnic_info[i].rss_hash_key) {
4690 			if (i == BNXT_VNIC_DEFAULT) {
4691 				u8 *key = (void *)vnic->rss_hash_key;
4692 				int k;
4693 
4694 				if (!bp->rss_hash_key_valid &&
4695 				    !bp->rss_hash_key_updated) {
4696 					get_random_bytes(bp->rss_hash_key,
4697 							 HW_HASH_KEY_SIZE);
4698 					bp->rss_hash_key_updated = true;
4699 				}
4700 
4701 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4702 				       HW_HASH_KEY_SIZE);
4703 
4704 				if (!bp->rss_hash_key_updated)
4705 					continue;
4706 
4707 				bp->rss_hash_key_updated = false;
4708 				bp->rss_hash_key_valid = true;
4709 
4710 				bp->toeplitz_prefix = 0;
4711 				for (k = 0; k < 8; k++) {
4712 					bp->toeplitz_prefix <<= 8;
4713 					bp->toeplitz_prefix |= key[k];
4714 				}
4715 			} else {
4716 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4717 				       HW_HASH_KEY_SIZE);
4718 			}
4719 		}
4720 	}
4721 }
4722 
4723 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4724 {
4725 	int pages;
4726 
4727 	pages = ring_size / desc_per_pg;
4728 
4729 	if (!pages)
4730 		return 1;
4731 
4732 	pages++;
4733 
4734 	while (pages & (pages - 1))
4735 		pages++;
4736 
4737 	return pages;
4738 }
4739 
4740 void bnxt_set_tpa_flags(struct bnxt *bp)
4741 {
4742 	bp->flags &= ~BNXT_FLAG_TPA;
4743 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4744 		return;
4745 	if (bp->dev->features & NETIF_F_LRO)
4746 		bp->flags |= BNXT_FLAG_LRO;
4747 	else if (bp->dev->features & NETIF_F_GRO_HW)
4748 		bp->flags |= BNXT_FLAG_GRO;
4749 }
4750 
4751 static void bnxt_init_ring_params(struct bnxt *bp)
4752 {
4753 	unsigned int rx_size;
4754 
4755 	bp->rx_copybreak = BNXT_DEFAULT_RX_COPYBREAK;
4756 	/* Try to fit 4 chunks into a 4k page */
4757 	rx_size = SZ_1K -
4758 		NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4759 	bp->dev->cfg->hds_thresh = max(BNXT_DEFAULT_RX_COPYBREAK, rx_size);
4760 }
4761 
4762 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4763  * be set on entry.
4764  */
4765 void bnxt_set_ring_params(struct bnxt *bp)
4766 {
4767 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4768 	u32 agg_factor = 0, agg_ring_size = 0;
4769 
4770 	/* 8 for CRC and VLAN */
4771 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4772 
4773 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4774 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4775 
4776 	ring_size = bp->rx_ring_size;
4777 	bp->rx_agg_ring_size = 0;
4778 	bp->rx_agg_nr_pages = 0;
4779 
4780 	if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS)
4781 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4782 
4783 	bp->flags &= ~BNXT_FLAG_JUMBO;
4784 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4785 		u32 jumbo_factor;
4786 
4787 		bp->flags |= BNXT_FLAG_JUMBO;
4788 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4789 		if (jumbo_factor > agg_factor)
4790 			agg_factor = jumbo_factor;
4791 	}
4792 	if (agg_factor) {
4793 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4794 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4795 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4796 				    bp->rx_ring_size, ring_size);
4797 			bp->rx_ring_size = ring_size;
4798 		}
4799 		agg_ring_size = ring_size * agg_factor;
4800 
4801 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4802 							RX_DESC_CNT);
4803 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4804 			u32 tmp = agg_ring_size;
4805 
4806 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4807 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4808 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4809 				    tmp, agg_ring_size);
4810 		}
4811 		bp->rx_agg_ring_size = agg_ring_size;
4812 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4813 
4814 		if (BNXT_RX_PAGE_MODE(bp)) {
4815 			rx_space = PAGE_SIZE;
4816 			rx_size = PAGE_SIZE -
4817 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4818 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4819 		} else {
4820 			rx_size = max3(BNXT_DEFAULT_RX_COPYBREAK,
4821 				       bp->rx_copybreak,
4822 				       bp->dev->cfg_pending->hds_thresh);
4823 			rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN);
4824 			rx_space = rx_size + NET_SKB_PAD +
4825 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4826 		}
4827 	}
4828 
4829 	bp->rx_buf_use_size = rx_size;
4830 	bp->rx_buf_size = rx_space;
4831 
4832 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4833 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4834 
4835 	ring_size = bp->tx_ring_size;
4836 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4837 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4838 
4839 	max_rx_cmpl = bp->rx_ring_size;
4840 	/* MAX TPA needs to be added because TPA_START completions are
4841 	 * immediately recycled, so the TPA completions are not bound by
4842 	 * the RX ring size.
4843 	 */
4844 	if (bp->flags & BNXT_FLAG_TPA)
4845 		max_rx_cmpl += bp->max_tpa;
4846 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4847 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4848 	bp->cp_ring_size = ring_size;
4849 
4850 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4851 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4852 		bp->cp_nr_pages = MAX_CP_PAGES;
4853 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4854 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4855 			    ring_size, bp->cp_ring_size);
4856 	}
4857 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4858 	bp->cp_ring_mask = bp->cp_bit - 1;
4859 }
4860 
4861 /* Changing allocation mode of RX rings.
4862  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4863  */
4864 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4865 {
4866 	struct net_device *dev = bp->dev;
4867 
4868 	if (page_mode) {
4869 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
4870 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4871 
4872 		if (bp->xdp_prog->aux->xdp_has_frags)
4873 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4874 		else
4875 			dev->max_mtu =
4876 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4877 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4878 			bp->flags |= BNXT_FLAG_JUMBO;
4879 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4880 		} else {
4881 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4882 			bp->rx_skb_func = bnxt_rx_page_skb;
4883 		}
4884 		bp->rx_dir = DMA_BIDIRECTIONAL;
4885 	} else {
4886 		dev->max_mtu = bp->max_mtu;
4887 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4888 		bp->rx_dir = DMA_FROM_DEVICE;
4889 		bp->rx_skb_func = bnxt_rx_skb;
4890 	}
4891 }
4892 
4893 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4894 {
4895 	__bnxt_set_rx_skb_mode(bp, page_mode);
4896 
4897 	if (!page_mode) {
4898 		int rx, tx;
4899 
4900 		bnxt_get_max_rings(bp, &rx, &tx, true);
4901 		if (rx > 1) {
4902 			bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS;
4903 			bp->dev->hw_features |= NETIF_F_LRO;
4904 		}
4905 	}
4906 
4907 	/* Update LRO and GRO_HW availability */
4908 	netdev_update_features(bp->dev);
4909 }
4910 
4911 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4912 {
4913 	int i;
4914 	struct bnxt_vnic_info *vnic;
4915 	struct pci_dev *pdev = bp->pdev;
4916 
4917 	if (!bp->vnic_info)
4918 		return;
4919 
4920 	for (i = 0; i < bp->nr_vnics; i++) {
4921 		vnic = &bp->vnic_info[i];
4922 
4923 		kfree(vnic->fw_grp_ids);
4924 		vnic->fw_grp_ids = NULL;
4925 
4926 		kfree(vnic->uc_list);
4927 		vnic->uc_list = NULL;
4928 
4929 		if (vnic->mc_list) {
4930 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4931 					  vnic->mc_list, vnic->mc_list_mapping);
4932 			vnic->mc_list = NULL;
4933 		}
4934 
4935 		if (vnic->rss_table) {
4936 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4937 					  vnic->rss_table,
4938 					  vnic->rss_table_dma_addr);
4939 			vnic->rss_table = NULL;
4940 		}
4941 
4942 		vnic->rss_hash_key = NULL;
4943 		vnic->flags = 0;
4944 	}
4945 }
4946 
4947 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4948 {
4949 	int i, rc = 0, size;
4950 	struct bnxt_vnic_info *vnic;
4951 	struct pci_dev *pdev = bp->pdev;
4952 	int max_rings;
4953 
4954 	for (i = 0; i < bp->nr_vnics; i++) {
4955 		vnic = &bp->vnic_info[i];
4956 
4957 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4958 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4959 
4960 			if (mem_size > 0) {
4961 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4962 				if (!vnic->uc_list) {
4963 					rc = -ENOMEM;
4964 					goto out;
4965 				}
4966 			}
4967 		}
4968 
4969 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4970 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4971 			vnic->mc_list =
4972 				dma_alloc_coherent(&pdev->dev,
4973 						   vnic->mc_list_size,
4974 						   &vnic->mc_list_mapping,
4975 						   GFP_KERNEL);
4976 			if (!vnic->mc_list) {
4977 				rc = -ENOMEM;
4978 				goto out;
4979 			}
4980 		}
4981 
4982 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4983 			goto vnic_skip_grps;
4984 
4985 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4986 			max_rings = bp->rx_nr_rings;
4987 		else
4988 			max_rings = 1;
4989 
4990 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4991 		if (!vnic->fw_grp_ids) {
4992 			rc = -ENOMEM;
4993 			goto out;
4994 		}
4995 vnic_skip_grps:
4996 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4997 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4998 			continue;
4999 
5000 		/* Allocate rss table and hash key */
5001 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
5002 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5003 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
5004 
5005 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
5006 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
5007 						     vnic->rss_table_size,
5008 						     &vnic->rss_table_dma_addr,
5009 						     GFP_KERNEL);
5010 		if (!vnic->rss_table) {
5011 			rc = -ENOMEM;
5012 			goto out;
5013 		}
5014 
5015 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
5016 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
5017 	}
5018 	return 0;
5019 
5020 out:
5021 	return rc;
5022 }
5023 
5024 static void bnxt_free_hwrm_resources(struct bnxt *bp)
5025 {
5026 	struct bnxt_hwrm_wait_token *token;
5027 
5028 	dma_pool_destroy(bp->hwrm_dma_pool);
5029 	bp->hwrm_dma_pool = NULL;
5030 
5031 	rcu_read_lock();
5032 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
5033 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
5034 	rcu_read_unlock();
5035 }
5036 
5037 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
5038 {
5039 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
5040 					    BNXT_HWRM_DMA_SIZE,
5041 					    BNXT_HWRM_DMA_ALIGN, 0);
5042 	if (!bp->hwrm_dma_pool)
5043 		return -ENOMEM;
5044 
5045 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
5046 
5047 	return 0;
5048 }
5049 
5050 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
5051 {
5052 	kfree(stats->hw_masks);
5053 	stats->hw_masks = NULL;
5054 	kfree(stats->sw_stats);
5055 	stats->sw_stats = NULL;
5056 	if (stats->hw_stats) {
5057 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
5058 				  stats->hw_stats_map);
5059 		stats->hw_stats = NULL;
5060 	}
5061 }
5062 
5063 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
5064 				bool alloc_masks)
5065 {
5066 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
5067 					     &stats->hw_stats_map, GFP_KERNEL);
5068 	if (!stats->hw_stats)
5069 		return -ENOMEM;
5070 
5071 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
5072 	if (!stats->sw_stats)
5073 		goto stats_mem_err;
5074 
5075 	if (alloc_masks) {
5076 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
5077 		if (!stats->hw_masks)
5078 			goto stats_mem_err;
5079 	}
5080 	return 0;
5081 
5082 stats_mem_err:
5083 	bnxt_free_stats_mem(bp, stats);
5084 	return -ENOMEM;
5085 }
5086 
5087 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
5088 {
5089 	int i;
5090 
5091 	for (i = 0; i < count; i++)
5092 		mask_arr[i] = mask;
5093 }
5094 
5095 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
5096 {
5097 	int i;
5098 
5099 	for (i = 0; i < count; i++)
5100 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
5101 }
5102 
5103 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
5104 				    struct bnxt_stats_mem *stats)
5105 {
5106 	struct hwrm_func_qstats_ext_output *resp;
5107 	struct hwrm_func_qstats_ext_input *req;
5108 	__le64 *hw_masks;
5109 	int rc;
5110 
5111 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
5112 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5113 		return -EOPNOTSUPP;
5114 
5115 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
5116 	if (rc)
5117 		return rc;
5118 
5119 	req->fid = cpu_to_le16(0xffff);
5120 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5121 
5122 	resp = hwrm_req_hold(bp, req);
5123 	rc = hwrm_req_send(bp, req);
5124 	if (!rc) {
5125 		hw_masks = &resp->rx_ucast_pkts;
5126 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
5127 	}
5128 	hwrm_req_drop(bp, req);
5129 	return rc;
5130 }
5131 
5132 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
5133 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
5134 
5135 static void bnxt_init_stats(struct bnxt *bp)
5136 {
5137 	struct bnxt_napi *bnapi = bp->bnapi[0];
5138 	struct bnxt_cp_ring_info *cpr;
5139 	struct bnxt_stats_mem *stats;
5140 	__le64 *rx_stats, *tx_stats;
5141 	int rc, rx_count, tx_count;
5142 	u64 *rx_masks, *tx_masks;
5143 	u64 mask;
5144 	u8 flags;
5145 
5146 	cpr = &bnapi->cp_ring;
5147 	stats = &cpr->stats;
5148 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
5149 	if (rc) {
5150 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5151 			mask = (1ULL << 48) - 1;
5152 		else
5153 			mask = -1ULL;
5154 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
5155 	}
5156 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
5157 		stats = &bp->port_stats;
5158 		rx_stats = stats->hw_stats;
5159 		rx_masks = stats->hw_masks;
5160 		rx_count = sizeof(struct rx_port_stats) / 8;
5161 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5162 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5163 		tx_count = sizeof(struct tx_port_stats) / 8;
5164 
5165 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
5166 		rc = bnxt_hwrm_port_qstats(bp, flags);
5167 		if (rc) {
5168 			mask = (1ULL << 40) - 1;
5169 
5170 			bnxt_fill_masks(rx_masks, mask, rx_count);
5171 			bnxt_fill_masks(tx_masks, mask, tx_count);
5172 		} else {
5173 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5174 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5175 			bnxt_hwrm_port_qstats(bp, 0);
5176 		}
5177 	}
5178 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5179 		stats = &bp->rx_port_stats_ext;
5180 		rx_stats = stats->hw_stats;
5181 		rx_masks = stats->hw_masks;
5182 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5183 		stats = &bp->tx_port_stats_ext;
5184 		tx_stats = stats->hw_stats;
5185 		tx_masks = stats->hw_masks;
5186 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5187 
5188 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5189 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5190 		if (rc) {
5191 			mask = (1ULL << 40) - 1;
5192 
5193 			bnxt_fill_masks(rx_masks, mask, rx_count);
5194 			if (tx_stats)
5195 				bnxt_fill_masks(tx_masks, mask, tx_count);
5196 		} else {
5197 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5198 			if (tx_stats)
5199 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5200 						   tx_count);
5201 			bnxt_hwrm_port_qstats_ext(bp, 0);
5202 		}
5203 	}
5204 }
5205 
5206 static void bnxt_free_port_stats(struct bnxt *bp)
5207 {
5208 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5209 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5210 
5211 	bnxt_free_stats_mem(bp, &bp->port_stats);
5212 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5213 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5214 }
5215 
5216 static void bnxt_free_ring_stats(struct bnxt *bp)
5217 {
5218 	int i;
5219 
5220 	if (!bp->bnapi)
5221 		return;
5222 
5223 	for (i = 0; i < bp->cp_nr_rings; i++) {
5224 		struct bnxt_napi *bnapi = bp->bnapi[i];
5225 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5226 
5227 		bnxt_free_stats_mem(bp, &cpr->stats);
5228 
5229 		kfree(cpr->sw_stats);
5230 		cpr->sw_stats = NULL;
5231 	}
5232 }
5233 
5234 static int bnxt_alloc_stats(struct bnxt *bp)
5235 {
5236 	u32 size, i;
5237 	int rc;
5238 
5239 	size = bp->hw_ring_stats_size;
5240 
5241 	for (i = 0; i < bp->cp_nr_rings; i++) {
5242 		struct bnxt_napi *bnapi = bp->bnapi[i];
5243 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5244 
5245 		cpr->sw_stats = kzalloc_obj(*cpr->sw_stats);
5246 		if (!cpr->sw_stats)
5247 			return -ENOMEM;
5248 
5249 		cpr->stats.len = size;
5250 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5251 		if (rc)
5252 			return rc;
5253 
5254 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5255 	}
5256 
5257 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5258 		return 0;
5259 
5260 	if (bp->port_stats.hw_stats)
5261 		goto alloc_ext_stats;
5262 
5263 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5264 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5265 	if (rc)
5266 		return rc;
5267 
5268 	bp->flags |= BNXT_FLAG_PORT_STATS;
5269 
5270 alloc_ext_stats:
5271 	/* Display extended statistics only if FW supports it */
5272 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5273 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5274 			return 0;
5275 
5276 	if (bp->rx_port_stats_ext.hw_stats)
5277 		goto alloc_tx_ext_stats;
5278 
5279 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5280 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5281 	/* Extended stats are optional */
5282 	if (rc)
5283 		return 0;
5284 
5285 alloc_tx_ext_stats:
5286 	if (bp->tx_port_stats_ext.hw_stats)
5287 		return 0;
5288 
5289 	if (bp->hwrm_spec_code >= 0x10902 ||
5290 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5291 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5292 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5293 		/* Extended stats are optional */
5294 		if (rc)
5295 			return 0;
5296 	}
5297 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5298 	return 0;
5299 }
5300 
5301 static void bnxt_clear_ring_indices(struct bnxt *bp)
5302 {
5303 	int i, j;
5304 
5305 	if (!bp->bnapi)
5306 		return;
5307 
5308 	for (i = 0; i < bp->cp_nr_rings; i++) {
5309 		struct bnxt_napi *bnapi = bp->bnapi[i];
5310 		struct bnxt_cp_ring_info *cpr;
5311 		struct bnxt_rx_ring_info *rxr;
5312 		struct bnxt_tx_ring_info *txr;
5313 
5314 		if (!bnapi)
5315 			continue;
5316 
5317 		cpr = &bnapi->cp_ring;
5318 		cpr->cp_raw_cons = 0;
5319 
5320 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5321 			txr->tx_prod = 0;
5322 			txr->tx_cons = 0;
5323 			txr->tx_hw_cons = 0;
5324 		}
5325 
5326 		rxr = bnapi->rx_ring;
5327 		if (rxr) {
5328 			rxr->rx_prod = 0;
5329 			rxr->rx_agg_prod = 0;
5330 			rxr->rx_sw_agg_prod = 0;
5331 			rxr->rx_next_cons = 0;
5332 		}
5333 		bnapi->events = 0;
5334 	}
5335 }
5336 
5337 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5338 {
5339 	u8 type = fltr->type, flags = fltr->flags;
5340 
5341 	INIT_LIST_HEAD(&fltr->list);
5342 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5343 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5344 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5345 }
5346 
5347 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5348 {
5349 	if (!list_empty(&fltr->list))
5350 		list_del_init(&fltr->list);
5351 }
5352 
5353 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5354 {
5355 	struct bnxt_filter_base *usr_fltr, *tmp;
5356 
5357 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5358 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5359 			continue;
5360 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5361 	}
5362 }
5363 
5364 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5365 {
5366 	hlist_del(&fltr->hash);
5367 	bnxt_del_one_usr_fltr(bp, fltr);
5368 	if (fltr->flags) {
5369 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5370 		bp->ntp_fltr_count--;
5371 	}
5372 	kfree(fltr);
5373 }
5374 
5375 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5376 {
5377 	int i;
5378 
5379 	netdev_assert_locked_or_invisible(bp->dev);
5380 
5381 	/* Under netdev instance lock and all our NAPIs have been disabled.
5382 	 * It's safe to delete the hash table.
5383 	 */
5384 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5385 		struct hlist_head *head;
5386 		struct hlist_node *tmp;
5387 		struct bnxt_ntuple_filter *fltr;
5388 
5389 		head = &bp->ntp_fltr_hash_tbl[i];
5390 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5391 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5392 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5393 				     !list_empty(&fltr->base.list)))
5394 				continue;
5395 			bnxt_del_fltr(bp, &fltr->base);
5396 		}
5397 	}
5398 	if (!all)
5399 		return;
5400 
5401 	bitmap_free(bp->ntp_fltr_bmap);
5402 	bp->ntp_fltr_bmap = NULL;
5403 	bp->ntp_fltr_count = 0;
5404 }
5405 
5406 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5407 {
5408 	int i, rc = 0;
5409 
5410 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5411 		return 0;
5412 
5413 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5414 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5415 
5416 	bp->ntp_fltr_count = 0;
5417 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5418 
5419 	if (!bp->ntp_fltr_bmap)
5420 		rc = -ENOMEM;
5421 
5422 	return rc;
5423 }
5424 
5425 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5426 {
5427 	int i;
5428 
5429 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5430 		struct hlist_head *head;
5431 		struct hlist_node *tmp;
5432 		struct bnxt_l2_filter *fltr;
5433 
5434 		head = &bp->l2_fltr_hash_tbl[i];
5435 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5436 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5437 				     !list_empty(&fltr->base.list)))
5438 				continue;
5439 			bnxt_del_fltr(bp, &fltr->base);
5440 		}
5441 	}
5442 }
5443 
5444 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5445 {
5446 	int i;
5447 
5448 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5449 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5450 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5451 }
5452 
5453 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5454 {
5455 	bnxt_free_vnic_attributes(bp);
5456 	bnxt_free_tx_rings(bp);
5457 	bnxt_free_rx_rings(bp);
5458 	bnxt_free_cp_rings(bp);
5459 	bnxt_free_all_cp_arrays(bp);
5460 	bnxt_free_ntp_fltrs(bp, false);
5461 	bnxt_free_l2_filters(bp, false);
5462 	if (irq_re_init) {
5463 		bnxt_free_ring_stats(bp);
5464 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5465 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5466 			bnxt_free_port_stats(bp);
5467 		bnxt_free_ring_grps(bp);
5468 		bnxt_free_vnics(bp);
5469 		kfree(bp->tx_ring_map);
5470 		bp->tx_ring_map = NULL;
5471 		kfree(bp->tx_ring);
5472 		bp->tx_ring = NULL;
5473 		kfree(bp->rx_ring);
5474 		bp->rx_ring = NULL;
5475 		kfree(bp->bnapi);
5476 		bp->bnapi = NULL;
5477 	} else {
5478 		bnxt_clear_ring_indices(bp);
5479 	}
5480 }
5481 
5482 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5483 {
5484 	int i, j, rc, size, arr_size;
5485 	void *bnapi;
5486 
5487 	if (irq_re_init) {
5488 		/* Allocate bnapi mem pointer array and mem block for
5489 		 * all queues
5490 		 */
5491 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5492 				bp->cp_nr_rings);
5493 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5494 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5495 		if (!bnapi)
5496 			return -ENOMEM;
5497 
5498 		bp->bnapi = bnapi;
5499 		bnapi += arr_size;
5500 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5501 			bp->bnapi[i] = bnapi;
5502 			bp->bnapi[i]->index = i;
5503 			bp->bnapi[i]->bp = bp;
5504 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5505 				struct bnxt_cp_ring_info *cpr =
5506 					&bp->bnapi[i]->cp_ring;
5507 
5508 				cpr->cp_ring_struct.ring_mem.flags =
5509 					BNXT_RMEM_RING_PTE_FLAG;
5510 			}
5511 		}
5512 
5513 		bp->rx_ring = kzalloc_objs(struct bnxt_rx_ring_info,
5514 					   bp->rx_nr_rings);
5515 		if (!bp->rx_ring)
5516 			return -ENOMEM;
5517 
5518 		for (i = 0; i < bp->rx_nr_rings; i++) {
5519 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5520 
5521 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5522 				rxr->rx_ring_struct.ring_mem.flags =
5523 					BNXT_RMEM_RING_PTE_FLAG;
5524 				rxr->rx_agg_ring_struct.ring_mem.flags =
5525 					BNXT_RMEM_RING_PTE_FLAG;
5526 			} else {
5527 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5528 			}
5529 			rxr->bnapi = bp->bnapi[i];
5530 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5531 		}
5532 
5533 		bp->tx_ring = kzalloc_objs(struct bnxt_tx_ring_info,
5534 					   bp->tx_nr_rings);
5535 		if (!bp->tx_ring)
5536 			return -ENOMEM;
5537 
5538 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5539 					  GFP_KERNEL);
5540 
5541 		if (!bp->tx_ring_map)
5542 			return -ENOMEM;
5543 
5544 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5545 			j = 0;
5546 		else
5547 			j = bp->rx_nr_rings;
5548 
5549 		for (i = 0; i < bp->tx_nr_rings; i++) {
5550 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5551 			struct bnxt_napi *bnapi2;
5552 
5553 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5554 				txr->tx_ring_struct.ring_mem.flags =
5555 					BNXT_RMEM_RING_PTE_FLAG;
5556 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5557 			if (i >= bp->tx_nr_rings_xdp) {
5558 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5559 
5560 				bnapi2 = bp->bnapi[k];
5561 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5562 				txr->tx_napi_idx =
5563 					BNXT_RING_TO_TC(bp, txr->txq_index);
5564 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5565 				bnapi2->tx_int = bnxt_tx_int;
5566 			} else {
5567 				bnapi2 = bp->bnapi[j];
5568 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5569 				bnapi2->tx_ring[0] = txr;
5570 				bnapi2->tx_int = bnxt_tx_int_xdp;
5571 				j++;
5572 			}
5573 			txr->bnapi = bnapi2;
5574 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5575 				txr->tx_cpr = &bnapi2->cp_ring;
5576 		}
5577 
5578 		rc = bnxt_alloc_stats(bp);
5579 		if (rc)
5580 			goto alloc_mem_err;
5581 		bnxt_init_stats(bp);
5582 
5583 		rc = bnxt_alloc_ntp_fltrs(bp);
5584 		if (rc)
5585 			goto alloc_mem_err;
5586 
5587 		rc = bnxt_alloc_vnics(bp);
5588 		if (rc)
5589 			goto alloc_mem_err;
5590 	}
5591 
5592 	rc = bnxt_alloc_all_cp_arrays(bp);
5593 	if (rc)
5594 		goto alloc_mem_err;
5595 
5596 	bnxt_init_ring_struct(bp);
5597 
5598 	rc = bnxt_alloc_rx_rings(bp);
5599 	if (rc)
5600 		goto alloc_mem_err;
5601 
5602 	rc = bnxt_alloc_tx_rings(bp);
5603 	if (rc)
5604 		goto alloc_mem_err;
5605 
5606 	rc = bnxt_alloc_cp_rings(bp);
5607 	if (rc)
5608 		goto alloc_mem_err;
5609 
5610 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5611 						  BNXT_VNIC_MCAST_FLAG |
5612 						  BNXT_VNIC_UCAST_FLAG;
5613 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5614 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5615 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5616 
5617 	rc = bnxt_alloc_vnic_attributes(bp);
5618 	if (rc)
5619 		goto alloc_mem_err;
5620 	return 0;
5621 
5622 alloc_mem_err:
5623 	bnxt_free_mem(bp, true);
5624 	return rc;
5625 }
5626 
5627 static void bnxt_disable_int(struct bnxt *bp)
5628 {
5629 	int i;
5630 
5631 	if (!bp->bnapi)
5632 		return;
5633 
5634 	for (i = 0; i < bp->cp_nr_rings; i++) {
5635 		struct bnxt_napi *bnapi = bp->bnapi[i];
5636 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5637 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5638 
5639 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5640 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5641 	}
5642 }
5643 
5644 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5645 {
5646 	struct bnxt_napi *bnapi = bp->bnapi[n];
5647 	struct bnxt_cp_ring_info *cpr;
5648 
5649 	cpr = &bnapi->cp_ring;
5650 	return cpr->cp_ring_struct.map_idx;
5651 }
5652 
5653 static void bnxt_disable_int_sync(struct bnxt *bp)
5654 {
5655 	int i;
5656 
5657 	if (!bp->irq_tbl)
5658 		return;
5659 
5660 	atomic_inc(&bp->intr_sem);
5661 
5662 	bnxt_disable_int(bp);
5663 	for (i = 0; i < bp->cp_nr_rings; i++) {
5664 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5665 
5666 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5667 	}
5668 }
5669 
5670 static void bnxt_enable_int(struct bnxt *bp)
5671 {
5672 	int i;
5673 
5674 	atomic_set(&bp->intr_sem, 0);
5675 	for (i = 0; i < bp->cp_nr_rings; i++) {
5676 		struct bnxt_napi *bnapi = bp->bnapi[i];
5677 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5678 
5679 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5680 	}
5681 }
5682 
5683 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5684 			    bool async_only)
5685 {
5686 	DECLARE_BITMAP(async_events_bmap, 256);
5687 	u32 *events = (u32 *)async_events_bmap;
5688 	struct hwrm_func_drv_rgtr_output *resp;
5689 	struct hwrm_func_drv_rgtr_input *req;
5690 	u32 flags;
5691 	int rc, i;
5692 
5693 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5694 	if (rc)
5695 		return rc;
5696 
5697 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5698 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5699 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5700 
5701 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5702 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5703 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5704 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5705 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5706 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5707 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5708 	if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2)
5709 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT;
5710 	req->flags = cpu_to_le32(flags);
5711 	req->ver_maj_8b = DRV_VER_MAJ;
5712 	req->ver_min_8b = DRV_VER_MIN;
5713 	req->ver_upd_8b = DRV_VER_UPD;
5714 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5715 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5716 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5717 
5718 	if (BNXT_PF(bp)) {
5719 		u32 data[8];
5720 		int i;
5721 
5722 		memset(data, 0, sizeof(data));
5723 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5724 			u16 cmd = bnxt_vf_req_snif[i];
5725 			unsigned int bit, idx;
5726 
5727 			if ((bp->fw_cap & BNXT_FW_CAP_LINK_ADMIN) &&
5728 			    cmd == HWRM_PORT_PHY_QCFG)
5729 				continue;
5730 
5731 			idx = cmd / 32;
5732 			bit = cmd % 32;
5733 			data[idx] |= 1 << bit;
5734 		}
5735 
5736 		for (i = 0; i < 8; i++)
5737 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5738 
5739 		req->enables |=
5740 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5741 	}
5742 
5743 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5744 		req->flags |= cpu_to_le32(
5745 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5746 
5747 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5748 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5749 		u16 event_id = bnxt_async_events_arr[i];
5750 
5751 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5752 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5753 			continue;
5754 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5755 		    !bp->ptp_cfg)
5756 			continue;
5757 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5758 	}
5759 	if (bmap && bmap_size) {
5760 		for (i = 0; i < bmap_size; i++) {
5761 			if (test_bit(i, bmap))
5762 				__set_bit(i, async_events_bmap);
5763 		}
5764 	}
5765 	for (i = 0; i < 8; i++)
5766 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5767 
5768 	if (async_only)
5769 		req->enables =
5770 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5771 
5772 	resp = hwrm_req_hold(bp, req);
5773 	rc = hwrm_req_send(bp, req);
5774 	if (!rc) {
5775 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5776 		if (resp->flags &
5777 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5778 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5779 	}
5780 	hwrm_req_drop(bp, req);
5781 	return rc;
5782 }
5783 
5784 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5785 {
5786 	struct hwrm_func_drv_unrgtr_input *req;
5787 	int rc;
5788 
5789 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5790 		return 0;
5791 
5792 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5793 	if (rc)
5794 		return rc;
5795 	return hwrm_req_send(bp, req);
5796 }
5797 
5798 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5799 
5800 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5801 {
5802 	struct hwrm_tunnel_dst_port_free_input *req;
5803 	int rc;
5804 
5805 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5806 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5807 		return 0;
5808 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5809 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5810 		return 0;
5811 
5812 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5813 	if (rc)
5814 		return rc;
5815 
5816 	req->tunnel_type = tunnel_type;
5817 
5818 	switch (tunnel_type) {
5819 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5820 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5821 		bp->vxlan_port = 0;
5822 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5823 		break;
5824 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5825 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5826 		bp->nge_port = 0;
5827 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5828 		break;
5829 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5830 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5831 		bp->vxlan_gpe_port = 0;
5832 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5833 		break;
5834 	default:
5835 		break;
5836 	}
5837 
5838 	rc = hwrm_req_send(bp, req);
5839 	if (rc)
5840 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5841 			   rc);
5842 	if (bp->flags & BNXT_FLAG_TPA)
5843 		bnxt_set_tpa(bp, true);
5844 	return rc;
5845 }
5846 
5847 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5848 					   u8 tunnel_type)
5849 {
5850 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5851 	struct hwrm_tunnel_dst_port_alloc_input *req;
5852 	int rc;
5853 
5854 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5855 	if (rc)
5856 		return rc;
5857 
5858 	req->tunnel_type = tunnel_type;
5859 	req->tunnel_dst_port_val = port;
5860 
5861 	resp = hwrm_req_hold(bp, req);
5862 	rc = hwrm_req_send(bp, req);
5863 	if (rc) {
5864 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5865 			   rc);
5866 		goto err_out;
5867 	}
5868 
5869 	switch (tunnel_type) {
5870 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5871 		bp->vxlan_port = port;
5872 		bp->vxlan_fw_dst_port_id =
5873 			le16_to_cpu(resp->tunnel_dst_port_id);
5874 		break;
5875 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5876 		bp->nge_port = port;
5877 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5878 		break;
5879 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5880 		bp->vxlan_gpe_port = port;
5881 		bp->vxlan_gpe_fw_dst_port_id =
5882 			le16_to_cpu(resp->tunnel_dst_port_id);
5883 		break;
5884 	default:
5885 		break;
5886 	}
5887 	if (bp->flags & BNXT_FLAG_TPA)
5888 		bnxt_set_tpa(bp, true);
5889 
5890 err_out:
5891 	hwrm_req_drop(bp, req);
5892 	return rc;
5893 }
5894 
5895 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5896 {
5897 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5898 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5899 	int rc;
5900 
5901 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5902 	if (rc)
5903 		return rc;
5904 
5905 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5906 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5907 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5908 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5909 	}
5910 	req->mask = cpu_to_le32(vnic->rx_mask);
5911 	return hwrm_req_send_silent(bp, req);
5912 }
5913 
5914 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5915 {
5916 	if (!atomic_dec_and_test(&fltr->refcnt))
5917 		return;
5918 	spin_lock_bh(&bp->ntp_fltr_lock);
5919 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5920 		spin_unlock_bh(&bp->ntp_fltr_lock);
5921 		return;
5922 	}
5923 	hlist_del_rcu(&fltr->base.hash);
5924 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5925 	if (fltr->base.flags) {
5926 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5927 		bp->ntp_fltr_count--;
5928 	}
5929 	spin_unlock_bh(&bp->ntp_fltr_lock);
5930 	kfree_rcu(fltr, base.rcu);
5931 }
5932 
5933 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5934 						      struct bnxt_l2_key *key,
5935 						      u32 idx)
5936 {
5937 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5938 	struct bnxt_l2_filter *fltr;
5939 
5940 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5941 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5942 
5943 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5944 		    l2_key->vlan == key->vlan)
5945 			return fltr;
5946 	}
5947 	return NULL;
5948 }
5949 
5950 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5951 						    struct bnxt_l2_key *key,
5952 						    u32 idx)
5953 {
5954 	struct bnxt_l2_filter *fltr = NULL;
5955 
5956 	rcu_read_lock();
5957 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5958 	if (fltr)
5959 		atomic_inc(&fltr->refcnt);
5960 	rcu_read_unlock();
5961 	return fltr;
5962 }
5963 
5964 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5965 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5966 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5967 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5968 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5969 
5970 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5971 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5972 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5973 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5974 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5975 
5976 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5977 {
5978 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5979 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5980 			return sizeof(fkeys->addrs.v4addrs) +
5981 			       sizeof(fkeys->ports);
5982 
5983 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5984 			return sizeof(fkeys->addrs.v4addrs);
5985 	}
5986 
5987 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5988 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5989 			return sizeof(fkeys->addrs.v6addrs) +
5990 			       sizeof(fkeys->ports);
5991 
5992 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5993 			return sizeof(fkeys->addrs.v6addrs);
5994 	}
5995 
5996 	return 0;
5997 }
5998 
5999 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
6000 			 const unsigned char *key)
6001 {
6002 	u64 prefix = bp->toeplitz_prefix, hash = 0;
6003 	struct bnxt_ipv4_tuple tuple4;
6004 	struct bnxt_ipv6_tuple tuple6;
6005 	int i, j, len = 0;
6006 	u8 *four_tuple;
6007 
6008 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
6009 	if (!len)
6010 		return 0;
6011 
6012 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
6013 		tuple4.v4addrs = fkeys->addrs.v4addrs;
6014 		tuple4.ports = fkeys->ports;
6015 		four_tuple = (unsigned char *)&tuple4;
6016 	} else {
6017 		tuple6.v6addrs = fkeys->addrs.v6addrs;
6018 		tuple6.ports = fkeys->ports;
6019 		four_tuple = (unsigned char *)&tuple6;
6020 	}
6021 
6022 	for (i = 0, j = 8; i < len; i++, j++) {
6023 		u8 byte = four_tuple[i];
6024 		int bit;
6025 
6026 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
6027 			if (byte & 0x80)
6028 				hash ^= prefix;
6029 		}
6030 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
6031 	}
6032 
6033 	/* The valid part of the hash is in the upper 32 bits. */
6034 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
6035 }
6036 
6037 #ifdef CONFIG_RFS_ACCEL
6038 static struct bnxt_l2_filter *
6039 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
6040 {
6041 	struct bnxt_l2_filter *fltr;
6042 	u32 idx;
6043 
6044 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6045 	      BNXT_L2_FLTR_HASH_MASK;
6046 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6047 	return fltr;
6048 }
6049 #endif
6050 
6051 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
6052 			       struct bnxt_l2_key *key, u32 idx)
6053 {
6054 	struct hlist_head *head;
6055 
6056 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
6057 	fltr->l2_key.vlan = key->vlan;
6058 	fltr->base.type = BNXT_FLTR_TYPE_L2;
6059 	if (fltr->base.flags) {
6060 		int bit_id;
6061 
6062 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6063 						 bp->max_fltr, 0);
6064 		if (bit_id < 0)
6065 			return -ENOMEM;
6066 		fltr->base.sw_id = (u16)bit_id;
6067 		bp->ntp_fltr_count++;
6068 	}
6069 	head = &bp->l2_fltr_hash_tbl[idx];
6070 	hlist_add_head_rcu(&fltr->base.hash, head);
6071 	bnxt_insert_usr_fltr(bp, &fltr->base);
6072 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
6073 	atomic_set(&fltr->refcnt, 1);
6074 	return 0;
6075 }
6076 
6077 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
6078 						   struct bnxt_l2_key *key,
6079 						   gfp_t gfp)
6080 {
6081 	struct bnxt_l2_filter *fltr;
6082 	u32 idx;
6083 	int rc;
6084 
6085 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6086 	      BNXT_L2_FLTR_HASH_MASK;
6087 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6088 	if (fltr)
6089 		return fltr;
6090 
6091 	fltr = kzalloc_obj(*fltr, gfp);
6092 	if (!fltr)
6093 		return ERR_PTR(-ENOMEM);
6094 	spin_lock_bh(&bp->ntp_fltr_lock);
6095 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6096 	spin_unlock_bh(&bp->ntp_fltr_lock);
6097 	if (rc) {
6098 		bnxt_del_l2_filter(bp, fltr);
6099 		fltr = ERR_PTR(rc);
6100 	}
6101 	return fltr;
6102 }
6103 
6104 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
6105 						struct bnxt_l2_key *key,
6106 						u16 flags)
6107 {
6108 	struct bnxt_l2_filter *fltr;
6109 	u32 idx;
6110 	int rc;
6111 
6112 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6113 	      BNXT_L2_FLTR_HASH_MASK;
6114 	spin_lock_bh(&bp->ntp_fltr_lock);
6115 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6116 	if (fltr) {
6117 		fltr = ERR_PTR(-EEXIST);
6118 		goto l2_filter_exit;
6119 	}
6120 	fltr = kzalloc_obj(*fltr, GFP_ATOMIC);
6121 	if (!fltr) {
6122 		fltr = ERR_PTR(-ENOMEM);
6123 		goto l2_filter_exit;
6124 	}
6125 	fltr->base.flags = flags;
6126 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6127 	if (rc) {
6128 		spin_unlock_bh(&bp->ntp_fltr_lock);
6129 		bnxt_del_l2_filter(bp, fltr);
6130 		return ERR_PTR(rc);
6131 	}
6132 
6133 l2_filter_exit:
6134 	spin_unlock_bh(&bp->ntp_fltr_lock);
6135 	return fltr;
6136 }
6137 
6138 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
6139 {
6140 #ifdef CONFIG_BNXT_SRIOV
6141 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
6142 
6143 	return vf->fw_fid;
6144 #else
6145 	return INVALID_HW_RING_ID;
6146 #endif
6147 }
6148 
6149 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6150 {
6151 	struct hwrm_cfa_l2_filter_free_input *req;
6152 	u16 target_id = 0xffff;
6153 	int rc;
6154 
6155 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6156 		struct bnxt_pf_info *pf = &bp->pf;
6157 
6158 		if (fltr->base.vf_idx >= pf->active_vfs)
6159 			return -EINVAL;
6160 
6161 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6162 		if (target_id == INVALID_HW_RING_ID)
6163 			return -EINVAL;
6164 	}
6165 
6166 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
6167 	if (rc)
6168 		return rc;
6169 
6170 	req->target_id = cpu_to_le16(target_id);
6171 	req->l2_filter_id = fltr->base.filter_id;
6172 	return hwrm_req_send(bp, req);
6173 }
6174 
6175 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6176 {
6177 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6178 	struct hwrm_cfa_l2_filter_alloc_input *req;
6179 	u16 target_id = 0xffff;
6180 	int rc;
6181 
6182 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6183 		struct bnxt_pf_info *pf = &bp->pf;
6184 
6185 		if (fltr->base.vf_idx >= pf->active_vfs)
6186 			return -EINVAL;
6187 
6188 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6189 	}
6190 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6191 	if (rc)
6192 		return rc;
6193 
6194 	req->target_id = cpu_to_le16(target_id);
6195 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6196 
6197 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6198 		req->flags |=
6199 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6200 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6201 	req->enables =
6202 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6203 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6204 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6205 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6206 	eth_broadcast_addr(req->l2_addr_mask);
6207 
6208 	if (fltr->l2_key.vlan) {
6209 		req->enables |=
6210 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6211 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6212 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6213 		req->num_vlans = 1;
6214 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6215 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6216 	}
6217 
6218 	resp = hwrm_req_hold(bp, req);
6219 	rc = hwrm_req_send(bp, req);
6220 	if (!rc) {
6221 		fltr->base.filter_id = resp->l2_filter_id;
6222 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6223 	}
6224 	hwrm_req_drop(bp, req);
6225 	return rc;
6226 }
6227 
6228 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6229 				     struct bnxt_ntuple_filter *fltr)
6230 {
6231 	struct hwrm_cfa_ntuple_filter_free_input *req;
6232 	int rc;
6233 
6234 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6235 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6236 	if (rc)
6237 		return rc;
6238 
6239 	req->ntuple_filter_id = fltr->base.filter_id;
6240 	return hwrm_req_send(bp, req);
6241 }
6242 
6243 #define BNXT_NTP_FLTR_FLAGS					\
6244 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6245 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6246 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6247 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6248 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6249 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6250 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6251 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6252 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6253 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6254 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6255 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6256 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6257 
6258 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6259 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6260 
6261 void bnxt_fill_ipv6_mask(__be32 mask[4])
6262 {
6263 	int i;
6264 
6265 	for (i = 0; i < 4; i++)
6266 		mask[i] = cpu_to_be32(~0);
6267 }
6268 
6269 static void
6270 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6271 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6272 			  struct bnxt_ntuple_filter *fltr)
6273 {
6274 	u16 rxq = fltr->base.rxq;
6275 
6276 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6277 		struct ethtool_rxfh_context *ctx;
6278 		struct bnxt_rss_ctx *rss_ctx;
6279 		struct bnxt_vnic_info *vnic;
6280 
6281 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6282 			      fltr->base.fw_vnic_id);
6283 		if (ctx) {
6284 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6285 			vnic = &rss_ctx->vnic;
6286 
6287 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6288 		}
6289 		return;
6290 	}
6291 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6292 		struct bnxt_vnic_info *vnic;
6293 		u32 enables;
6294 
6295 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6296 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6297 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6298 		req->enables |= cpu_to_le32(enables);
6299 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6300 	} else {
6301 		u32 flags;
6302 
6303 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6304 		req->flags |= cpu_to_le32(flags);
6305 		req->dst_id = cpu_to_le16(rxq);
6306 	}
6307 }
6308 
6309 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6310 				      struct bnxt_ntuple_filter *fltr)
6311 {
6312 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6313 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6314 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6315 	struct flow_keys *keys = &fltr->fkeys;
6316 	struct bnxt_l2_filter *l2_fltr;
6317 	struct bnxt_vnic_info *vnic;
6318 	int rc;
6319 
6320 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6321 	if (rc)
6322 		return rc;
6323 
6324 	l2_fltr = fltr->l2_fltr;
6325 	req->l2_filter_id = l2_fltr->base.filter_id;
6326 
6327 	if (fltr->base.flags & BNXT_ACT_DROP) {
6328 		req->flags =
6329 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6330 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6331 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6332 	} else {
6333 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6334 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6335 	}
6336 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6337 
6338 	req->ethertype = htons(ETH_P_IP);
6339 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6340 	req->ip_protocol = keys->basic.ip_proto;
6341 
6342 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6343 		req->ethertype = htons(ETH_P_IPV6);
6344 		req->ip_addr_type =
6345 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6346 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6347 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6348 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6349 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6350 	} else {
6351 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6352 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6353 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6354 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6355 	}
6356 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6357 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6358 		req->tunnel_type =
6359 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6360 	}
6361 
6362 	req->src_port = keys->ports.src;
6363 	req->src_port_mask = masks->ports.src;
6364 	req->dst_port = keys->ports.dst;
6365 	req->dst_port_mask = masks->ports.dst;
6366 
6367 	resp = hwrm_req_hold(bp, req);
6368 	rc = hwrm_req_send(bp, req);
6369 	if (!rc)
6370 		fltr->base.filter_id = resp->ntuple_filter_id;
6371 	hwrm_req_drop(bp, req);
6372 	return rc;
6373 }
6374 
6375 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6376 				     const u8 *mac_addr)
6377 {
6378 	struct bnxt_l2_filter *fltr;
6379 	struct bnxt_l2_key key;
6380 	int rc;
6381 
6382 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6383 	key.vlan = 0;
6384 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6385 	if (IS_ERR(fltr))
6386 		return PTR_ERR(fltr);
6387 
6388 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6389 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6390 	if (rc)
6391 		bnxt_del_l2_filter(bp, fltr);
6392 	else
6393 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6394 	return rc;
6395 }
6396 
6397 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6398 {
6399 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6400 
6401 	/* Any associated ntuple filters will also be cleared by firmware. */
6402 	for (i = 0; i < num_of_vnics; i++) {
6403 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6404 
6405 		for (j = 0; j < vnic->uc_filter_count; j++) {
6406 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6407 
6408 			bnxt_hwrm_l2_filter_free(bp, fltr);
6409 			bnxt_del_l2_filter(bp, fltr);
6410 		}
6411 		vnic->uc_filter_count = 0;
6412 	}
6413 }
6414 
6415 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6416 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6417 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6418 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6419 
6420 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6421 					   struct hwrm_vnic_tpa_cfg_input *req)
6422 {
6423 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6424 
6425 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6426 		return;
6427 
6428 	if (bp->vxlan_port)
6429 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6430 	if (bp->vxlan_gpe_port)
6431 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6432 	if (bp->nge_port)
6433 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6434 
6435 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6436 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6437 }
6438 
6439 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6440 			   u32 tpa_flags)
6441 {
6442 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6443 	struct hwrm_vnic_tpa_cfg_input *req;
6444 	int rc;
6445 
6446 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6447 		return 0;
6448 
6449 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6450 	if (rc)
6451 		return rc;
6452 
6453 	if (tpa_flags) {
6454 		u16 mss = bp->dev->mtu - 40;
6455 		u32 nsegs, n, segs = 0, flags;
6456 
6457 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6458 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6459 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6460 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6461 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6462 		if (tpa_flags & BNXT_FLAG_GRO)
6463 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6464 
6465 		req->flags = cpu_to_le32(flags);
6466 
6467 		req->enables =
6468 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6469 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6470 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6471 
6472 		/* Number of segs are log2 units, and first packet is not
6473 		 * included as part of this units.
6474 		 */
6475 		if (mss <= BNXT_RX_PAGE_SIZE) {
6476 			n = BNXT_RX_PAGE_SIZE / mss;
6477 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6478 		} else {
6479 			n = mss / BNXT_RX_PAGE_SIZE;
6480 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6481 				n++;
6482 			nsegs = (MAX_SKB_FRAGS - n) / n;
6483 		}
6484 
6485 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6486 			segs = MAX_TPA_SEGS_P5;
6487 			max_aggs = bp->max_tpa;
6488 		} else {
6489 			segs = ilog2(nsegs);
6490 		}
6491 		req->max_agg_segs = cpu_to_le16(segs);
6492 		req->max_aggs = cpu_to_le16(max_aggs);
6493 
6494 		req->min_agg_len = cpu_to_le32(512);
6495 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6496 	}
6497 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6498 
6499 	return hwrm_req_send(bp, req);
6500 }
6501 
6502 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6503 {
6504 	struct bnxt_ring_grp_info *grp_info;
6505 
6506 	grp_info = &bp->grp_info[ring->grp_idx];
6507 	return grp_info->cp_fw_ring_id;
6508 }
6509 
6510 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6511 {
6512 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6513 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6514 	else
6515 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6516 }
6517 
6518 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6519 {
6520 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6521 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6522 	else
6523 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6524 }
6525 
6526 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6527 {
6528 	int entries;
6529 
6530 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6531 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6532 	else
6533 		entries = HW_HASH_INDEX_SIZE;
6534 
6535 	bp->rss_indir_tbl_entries = entries;
6536 	bp->rss_indir_tbl =
6537 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6538 	if (!bp->rss_indir_tbl)
6539 		return -ENOMEM;
6540 
6541 	return 0;
6542 }
6543 
6544 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6545 				 struct ethtool_rxfh_context *rss_ctx)
6546 {
6547 	u16 max_rings, max_entries, pad, i;
6548 	u32 *rss_indir_tbl;
6549 
6550 	if (!bp->rx_nr_rings)
6551 		return;
6552 
6553 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6554 		max_rings = bp->rx_nr_rings - 1;
6555 	else
6556 		max_rings = bp->rx_nr_rings;
6557 
6558 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6559 	if (rss_ctx)
6560 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6561 	else
6562 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6563 
6564 	for (i = 0; i < max_entries; i++)
6565 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6566 
6567 	pad = bp->rss_indir_tbl_entries - max_entries;
6568 	if (pad)
6569 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6570 }
6571 
6572 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6573 {
6574 	u32 i, tbl_size, max_ring = 0;
6575 
6576 	if (!bp->rss_indir_tbl)
6577 		return 0;
6578 
6579 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6580 	for (i = 0; i < tbl_size; i++)
6581 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6582 	return max_ring;
6583 }
6584 
6585 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6586 {
6587 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6588 		if (!rx_rings)
6589 			return 0;
6590 		if (bp->rss_cap & BNXT_RSS_CAP_LARGE_RSS_CTX)
6591 			return BNXT_RSS_TABLE_MAX_TBL_P5;
6592 
6593 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6594 					       BNXT_RSS_TABLE_ENTRIES_P5);
6595 	}
6596 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6597 		return 2;
6598 	return 1;
6599 }
6600 
6601 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6602 {
6603 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6604 	u16 i, j;
6605 
6606 	/* Fill the RSS indirection table with ring group ids */
6607 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6608 		if (!no_rss)
6609 			j = bp->rss_indir_tbl[i];
6610 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6611 	}
6612 }
6613 
6614 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6615 				    struct bnxt_vnic_info *vnic)
6616 {
6617 	__le16 *ring_tbl = vnic->rss_table;
6618 	struct bnxt_rx_ring_info *rxr;
6619 	u16 tbl_size, i;
6620 
6621 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6622 
6623 	for (i = 0; i < tbl_size; i++) {
6624 		u16 ring_id, j;
6625 
6626 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6627 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6628 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6629 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6630 		else
6631 			j = bp->rss_indir_tbl[i];
6632 		rxr = &bp->rx_ring[j];
6633 
6634 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6635 		*ring_tbl++ = cpu_to_le16(ring_id);
6636 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6637 		*ring_tbl++ = cpu_to_le16(ring_id);
6638 	}
6639 }
6640 
6641 static void
6642 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6643 			 struct bnxt_vnic_info *vnic)
6644 {
6645 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6646 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6647 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6648 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6649 	} else {
6650 		bnxt_fill_hw_rss_tbl(bp, vnic);
6651 	}
6652 
6653 	if (bp->rss_hash_delta) {
6654 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6655 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6656 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6657 		else
6658 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6659 	} else {
6660 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6661 	}
6662 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6663 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6664 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6665 }
6666 
6667 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6668 				  bool set_rss)
6669 {
6670 	struct hwrm_vnic_rss_cfg_input *req;
6671 	int rc;
6672 
6673 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6674 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6675 		return 0;
6676 
6677 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6678 	if (rc)
6679 		return rc;
6680 
6681 	if (set_rss)
6682 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6683 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6684 	return hwrm_req_send(bp, req);
6685 }
6686 
6687 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6688 				     struct bnxt_vnic_info *vnic, bool set_rss)
6689 {
6690 	struct hwrm_vnic_rss_cfg_input *req;
6691 	dma_addr_t ring_tbl_map;
6692 	u32 i, nr_ctxs;
6693 	int rc;
6694 
6695 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6696 	if (rc)
6697 		return rc;
6698 
6699 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6700 	if (!set_rss)
6701 		return hwrm_req_send(bp, req);
6702 
6703 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6704 	ring_tbl_map = vnic->rss_table_dma_addr;
6705 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6706 
6707 	hwrm_req_hold(bp, req);
6708 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6709 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6710 		req->ring_table_pair_index = i;
6711 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6712 		rc = hwrm_req_send(bp, req);
6713 		if (rc)
6714 			goto exit;
6715 	}
6716 
6717 exit:
6718 	hwrm_req_drop(bp, req);
6719 	return rc;
6720 }
6721 
6722 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6723 {
6724 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6725 	struct hwrm_vnic_rss_qcfg_output *resp;
6726 	struct hwrm_vnic_rss_qcfg_input *req;
6727 
6728 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6729 		return;
6730 
6731 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6732 	/* all contexts configured to same hash_type, zero always exists */
6733 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6734 	resp = hwrm_req_hold(bp, req);
6735 	if (!hwrm_req_send(bp, req)) {
6736 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6737 		bp->rss_hash_delta = 0;
6738 	}
6739 	hwrm_req_drop(bp, req);
6740 }
6741 
6742 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6743 {
6744 	u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh;
6745 	struct hwrm_vnic_plcmodes_cfg_input *req;
6746 	int rc;
6747 
6748 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6749 	if (rc)
6750 		return rc;
6751 
6752 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6753 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6754 	req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6755 
6756 	if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
6757 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6758 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6759 		req->enables |=
6760 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6761 		req->hds_threshold = cpu_to_le16(hds_thresh);
6762 	}
6763 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6764 	return hwrm_req_send(bp, req);
6765 }
6766 
6767 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6768 					struct bnxt_vnic_info *vnic,
6769 					u16 ctx_idx)
6770 {
6771 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6772 
6773 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6774 		return;
6775 
6776 	req->rss_cos_lb_ctx_id =
6777 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6778 
6779 	hwrm_req_send(bp, req);
6780 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6781 }
6782 
6783 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6784 {
6785 	int i, j;
6786 
6787 	for (i = 0; i < bp->nr_vnics; i++) {
6788 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6789 
6790 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6791 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6792 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6793 		}
6794 	}
6795 	bp->rsscos_nr_ctxs = 0;
6796 }
6797 
6798 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6799 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6800 {
6801 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6802 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6803 	int rc;
6804 
6805 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6806 	if (rc)
6807 		return rc;
6808 
6809 	resp = hwrm_req_hold(bp, req);
6810 	rc = hwrm_req_send(bp, req);
6811 	if (!rc)
6812 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6813 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6814 	hwrm_req_drop(bp, req);
6815 
6816 	return rc;
6817 }
6818 
6819 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6820 {
6821 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6822 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6823 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6824 }
6825 
6826 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6827 {
6828 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6829 	struct hwrm_vnic_cfg_input *req;
6830 	unsigned int ring = 0, grp_idx;
6831 	u16 def_vlan = 0;
6832 	int rc;
6833 
6834 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6835 	if (rc)
6836 		return rc;
6837 
6838 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6839 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6840 
6841 		req->default_rx_ring_id =
6842 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6843 		req->default_cmpl_ring_id =
6844 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6845 		req->enables =
6846 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6847 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6848 		goto vnic_mru;
6849 	}
6850 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6851 	/* Only RSS support for now TBD: COS & LB */
6852 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6853 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6854 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6855 					   VNIC_CFG_REQ_ENABLES_MRU);
6856 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6857 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6858 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6859 					   VNIC_CFG_REQ_ENABLES_MRU);
6860 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6861 	} else {
6862 		req->rss_rule = cpu_to_le16(0xffff);
6863 	}
6864 
6865 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6866 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6867 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6868 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6869 	} else {
6870 		req->cos_rule = cpu_to_le16(0xffff);
6871 	}
6872 
6873 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6874 		ring = 0;
6875 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6876 		ring = vnic->vnic_id - 1;
6877 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6878 		ring = bp->rx_nr_rings - 1;
6879 
6880 	grp_idx = bp->rx_ring[ring].bnapi->index;
6881 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6882 	req->lb_rule = cpu_to_le16(0xffff);
6883 vnic_mru:
6884 	vnic->mru = bp->dev->mtu + VLAN_ETH_HLEN;
6885 	req->mru = cpu_to_le16(vnic->mru);
6886 
6887 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6888 #ifdef CONFIG_BNXT_SRIOV
6889 	if (BNXT_VF(bp))
6890 		def_vlan = bp->vf.vlan;
6891 #endif
6892 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6893 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6894 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6895 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6896 
6897 	return hwrm_req_send(bp, req);
6898 }
6899 
6900 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6901 				    struct bnxt_vnic_info *vnic)
6902 {
6903 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6904 		struct hwrm_vnic_free_input *req;
6905 
6906 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6907 			return;
6908 
6909 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6910 
6911 		hwrm_req_send(bp, req);
6912 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6913 	}
6914 }
6915 
6916 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6917 {
6918 	u16 i;
6919 
6920 	for (i = 0; i < bp->nr_vnics; i++)
6921 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6922 }
6923 
6924 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6925 			 unsigned int start_rx_ring_idx,
6926 			 unsigned int nr_rings)
6927 {
6928 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6929 	struct hwrm_vnic_alloc_output *resp;
6930 	struct hwrm_vnic_alloc_input *req;
6931 	int rc;
6932 
6933 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6934 	if (rc)
6935 		return rc;
6936 
6937 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6938 		goto vnic_no_ring_grps;
6939 
6940 	/* map ring groups to this vnic */
6941 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6942 		grp_idx = bp->rx_ring[i].bnapi->index;
6943 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6944 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6945 				   j, nr_rings);
6946 			break;
6947 		}
6948 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6949 	}
6950 
6951 vnic_no_ring_grps:
6952 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6953 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6954 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6955 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6956 
6957 	resp = hwrm_req_hold(bp, req);
6958 	rc = hwrm_req_send(bp, req);
6959 	if (!rc)
6960 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6961 	hwrm_req_drop(bp, req);
6962 	return rc;
6963 }
6964 
6965 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6966 {
6967 	struct hwrm_vnic_qcaps_output *resp;
6968 	struct hwrm_vnic_qcaps_input *req;
6969 	int rc;
6970 
6971 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6972 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6973 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6974 	if (bp->hwrm_spec_code < 0x10600)
6975 		return 0;
6976 
6977 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6978 	if (rc)
6979 		return rc;
6980 
6981 	resp = hwrm_req_hold(bp, req);
6982 	rc = hwrm_req_send(bp, req);
6983 	if (!rc) {
6984 		u32 flags = le32_to_cpu(resp->flags);
6985 
6986 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6987 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6988 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6989 		if (flags &
6990 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6991 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6992 
6993 		/* Older P5 fw before EXT_HW_STATS support did not set
6994 		 * VLAN_STRIP_CAP properly.
6995 		 */
6996 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6997 		    (BNXT_CHIP_P5(bp) &&
6998 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6999 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
7000 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
7001 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
7002 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
7003 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
7004 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
7005 		if (bp->max_tpa_v2) {
7006 			if (BNXT_CHIP_P5(bp))
7007 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
7008 			else
7009 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
7010 		}
7011 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
7012 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
7013 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
7014 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
7015 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
7016 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
7017 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
7018 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
7019 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
7020 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
7021 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPV6_FLOW_LABEL_CAP)
7022 			bp->rss_cap |= BNXT_RSS_CAP_IPV6_FLOW_LABEL_RSS_CAP;
7023 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
7024 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
7025 	}
7026 	hwrm_req_drop(bp, req);
7027 	return rc;
7028 }
7029 
7030 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
7031 {
7032 	struct hwrm_ring_grp_alloc_output *resp;
7033 	struct hwrm_ring_grp_alloc_input *req;
7034 	int rc;
7035 	u16 i;
7036 
7037 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7038 		return 0;
7039 
7040 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
7041 	if (rc)
7042 		return rc;
7043 
7044 	resp = hwrm_req_hold(bp, req);
7045 	for (i = 0; i < bp->rx_nr_rings; i++) {
7046 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
7047 
7048 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
7049 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
7050 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
7051 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
7052 
7053 		rc = hwrm_req_send(bp, req);
7054 
7055 		if (rc)
7056 			break;
7057 
7058 		bp->grp_info[grp_idx].fw_grp_id =
7059 			le32_to_cpu(resp->ring_group_id);
7060 	}
7061 	hwrm_req_drop(bp, req);
7062 	return rc;
7063 }
7064 
7065 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
7066 {
7067 	struct hwrm_ring_grp_free_input *req;
7068 	u16 i;
7069 
7070 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7071 		return;
7072 
7073 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
7074 		return;
7075 
7076 	hwrm_req_hold(bp, req);
7077 	for (i = 0; i < bp->cp_nr_rings; i++) {
7078 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
7079 			continue;
7080 		req->ring_group_id =
7081 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
7082 
7083 		hwrm_req_send(bp, req);
7084 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
7085 	}
7086 	hwrm_req_drop(bp, req);
7087 }
7088 
7089 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type,
7090 				       struct hwrm_ring_alloc_input *req,
7091 				       struct bnxt_rx_ring_info *rxr,
7092 				       struct bnxt_ring_struct *ring)
7093 {
7094 	struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx];
7095 	u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID |
7096 		      RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID;
7097 
7098 	if (ring_type == HWRM_RING_ALLOC_AGG) {
7099 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
7100 		req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
7101 		req->rx_buf_size = cpu_to_le16(rxr->rx_page_size);
7102 		enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID;
7103 	} else {
7104 		req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
7105 		if (NET_IP_ALIGN == 2)
7106 			req->flags =
7107 				cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD);
7108 	}
7109 	req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7110 	req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7111 	req->enables |= cpu_to_le32(enables);
7112 }
7113 
7114 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
7115 				    struct bnxt_rx_ring_info *rxr,
7116 				    struct bnxt_ring_struct *ring,
7117 				    u32 ring_type, u32 map_index)
7118 {
7119 	struct hwrm_ring_alloc_output *resp;
7120 	struct hwrm_ring_alloc_input *req;
7121 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
7122 	struct bnxt_ring_grp_info *grp_info;
7123 	int rc, err = 0;
7124 	u16 ring_id;
7125 
7126 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
7127 	if (rc)
7128 		goto exit;
7129 
7130 	req->enables = 0;
7131 	if (rmem->nr_pages > 1) {
7132 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
7133 		/* Page size is in log2 units */
7134 		req->page_size = BNXT_PAGE_SHIFT;
7135 		req->page_tbl_depth = 1;
7136 	} else {
7137 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
7138 	}
7139 	req->fbo = 0;
7140 	/* Association of ring index with doorbell index and MSIX number */
7141 	req->logical_id = cpu_to_le16(map_index);
7142 
7143 	switch (ring_type) {
7144 	case HWRM_RING_ALLOC_TX: {
7145 		struct bnxt_tx_ring_info *txr;
7146 		u16 flags = 0;
7147 
7148 		txr = container_of(ring, struct bnxt_tx_ring_info,
7149 				   tx_ring_struct);
7150 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
7151 		/* Association of transmit ring with completion ring */
7152 		grp_info = &bp->grp_info[ring->grp_idx];
7153 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
7154 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
7155 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7156 		req->queue_id = cpu_to_le16(ring->queue_id);
7157 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
7158 			req->cmpl_coal_cnt =
7159 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
7160 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
7161 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
7162 		req->flags = cpu_to_le16(flags);
7163 		break;
7164 	}
7165 	case HWRM_RING_ALLOC_RX:
7166 	case HWRM_RING_ALLOC_AGG:
7167 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
7168 		req->length = (ring_type == HWRM_RING_ALLOC_RX) ?
7169 			      cpu_to_le32(bp->rx_ring_mask + 1) :
7170 			      cpu_to_le32(bp->rx_agg_ring_mask + 1);
7171 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7172 			bnxt_set_rx_ring_params_p5(bp, ring_type, req,
7173 						   rxr, ring);
7174 		break;
7175 	case HWRM_RING_ALLOC_CMPL:
7176 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7177 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7178 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7179 			/* Association of cp ring with nq */
7180 			grp_info = &bp->grp_info[map_index];
7181 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7182 			req->cq_handle = cpu_to_le64(ring->handle);
7183 			req->enables |= cpu_to_le32(
7184 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7185 		} else {
7186 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7187 		}
7188 		break;
7189 	case HWRM_RING_ALLOC_NQ:
7190 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7191 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7192 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7193 		break;
7194 	default:
7195 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7196 			   ring_type);
7197 		return -EINVAL;
7198 	}
7199 
7200 	resp = hwrm_req_hold(bp, req);
7201 	rc = hwrm_req_send(bp, req);
7202 	err = le16_to_cpu(resp->error_code);
7203 	ring_id = le16_to_cpu(resp->ring_id);
7204 	hwrm_req_drop(bp, req);
7205 
7206 exit:
7207 	if (rc || err) {
7208 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7209 			   ring_type, rc, err);
7210 		return -EIO;
7211 	}
7212 	ring->fw_ring_id = ring_id;
7213 	return rc;
7214 }
7215 
7216 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7217 {
7218 	int rc;
7219 
7220 	if (BNXT_PF(bp)) {
7221 		struct hwrm_func_cfg_input *req;
7222 
7223 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7224 		if (rc)
7225 			return rc;
7226 
7227 		req->fid = cpu_to_le16(0xffff);
7228 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7229 		req->async_event_cr = cpu_to_le16(idx);
7230 		return hwrm_req_send(bp, req);
7231 	} else {
7232 		struct hwrm_func_vf_cfg_input *req;
7233 
7234 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7235 		if (rc)
7236 			return rc;
7237 
7238 		req->enables =
7239 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7240 		req->async_event_cr = cpu_to_le16(idx);
7241 		return hwrm_req_send(bp, req);
7242 	}
7243 }
7244 
7245 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7246 			     u32 ring_type)
7247 {
7248 	switch (ring_type) {
7249 	case HWRM_RING_ALLOC_TX:
7250 		db->db_ring_mask = bp->tx_ring_mask;
7251 		break;
7252 	case HWRM_RING_ALLOC_RX:
7253 		db->db_ring_mask = bp->rx_ring_mask;
7254 		break;
7255 	case HWRM_RING_ALLOC_AGG:
7256 		db->db_ring_mask = bp->rx_agg_ring_mask;
7257 		break;
7258 	case HWRM_RING_ALLOC_CMPL:
7259 	case HWRM_RING_ALLOC_NQ:
7260 		db->db_ring_mask = bp->cp_ring_mask;
7261 		break;
7262 	}
7263 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7264 		db->db_epoch_mask = db->db_ring_mask + 1;
7265 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7266 	}
7267 }
7268 
7269 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7270 			u32 map_idx, u32 xid)
7271 {
7272 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7273 		switch (ring_type) {
7274 		case HWRM_RING_ALLOC_TX:
7275 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7276 			break;
7277 		case HWRM_RING_ALLOC_RX:
7278 		case HWRM_RING_ALLOC_AGG:
7279 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7280 			break;
7281 		case HWRM_RING_ALLOC_CMPL:
7282 			db->db_key64 = DBR_PATH_L2;
7283 			break;
7284 		case HWRM_RING_ALLOC_NQ:
7285 			db->db_key64 = DBR_PATH_L2;
7286 			break;
7287 		}
7288 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7289 
7290 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7291 			db->db_key64 |= DBR_VALID;
7292 
7293 		db->doorbell = bp->bar1 + bp->db_offset;
7294 	} else {
7295 		db->doorbell = bp->bar1 + map_idx * 0x80;
7296 		switch (ring_type) {
7297 		case HWRM_RING_ALLOC_TX:
7298 			db->db_key32 = DB_KEY_TX;
7299 			break;
7300 		case HWRM_RING_ALLOC_RX:
7301 		case HWRM_RING_ALLOC_AGG:
7302 			db->db_key32 = DB_KEY_RX;
7303 			break;
7304 		case HWRM_RING_ALLOC_CMPL:
7305 			db->db_key32 = DB_KEY_CP;
7306 			break;
7307 		}
7308 	}
7309 	bnxt_set_db_mask(bp, db, ring_type);
7310 }
7311 
7312 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7313 				   struct bnxt_rx_ring_info *rxr)
7314 {
7315 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7316 	struct bnxt_napi *bnapi = rxr->bnapi;
7317 	u32 type = HWRM_RING_ALLOC_RX;
7318 	u32 map_idx = bnapi->index;
7319 	int rc;
7320 
7321 	rc = hwrm_ring_alloc_send_msg(bp, rxr, ring, type, map_idx);
7322 	if (rc)
7323 		return rc;
7324 
7325 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7326 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7327 
7328 	return 0;
7329 }
7330 
7331 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7332 				       struct bnxt_rx_ring_info *rxr)
7333 {
7334 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7335 	u32 type = HWRM_RING_ALLOC_AGG;
7336 	u32 grp_idx = ring->grp_idx;
7337 	u32 map_idx;
7338 	int rc;
7339 
7340 	map_idx = grp_idx + bp->rx_nr_rings;
7341 	rc = hwrm_ring_alloc_send_msg(bp, rxr, ring, type, map_idx);
7342 	if (rc)
7343 		return rc;
7344 
7345 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7346 		    ring->fw_ring_id);
7347 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7348 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7349 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7350 
7351 	return 0;
7352 }
7353 
7354 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp,
7355 				      struct bnxt_cp_ring_info *cpr)
7356 {
7357 	const u32 type = HWRM_RING_ALLOC_CMPL;
7358 	struct bnxt_napi *bnapi = cpr->bnapi;
7359 	struct bnxt_ring_struct *ring;
7360 	u32 map_idx = bnapi->index;
7361 	int rc;
7362 
7363 	ring = &cpr->cp_ring_struct;
7364 	ring->handle = BNXT_SET_NQ_HDL(cpr);
7365 	rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, map_idx);
7366 	if (rc)
7367 		return rc;
7368 	bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7369 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7370 	return 0;
7371 }
7372 
7373 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp,
7374 				   struct bnxt_tx_ring_info *txr, u32 tx_idx)
7375 {
7376 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7377 	const u32 type = HWRM_RING_ALLOC_TX;
7378 	int rc;
7379 
7380 	rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, tx_idx);
7381 	if (rc)
7382 		return rc;
7383 	bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id);
7384 	return 0;
7385 }
7386 
7387 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7388 {
7389 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7390 	int i, rc = 0;
7391 	u32 type;
7392 
7393 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7394 		type = HWRM_RING_ALLOC_NQ;
7395 	else
7396 		type = HWRM_RING_ALLOC_CMPL;
7397 	for (i = 0; i < bp->cp_nr_rings; i++) {
7398 		struct bnxt_napi *bnapi = bp->bnapi[i];
7399 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7400 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7401 		u32 map_idx = ring->map_idx;
7402 		unsigned int vector;
7403 
7404 		vector = bp->irq_tbl[map_idx].vector;
7405 		disable_irq_nosync(vector);
7406 		rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, map_idx);
7407 		if (rc) {
7408 			enable_irq(vector);
7409 			goto err_out;
7410 		}
7411 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7412 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7413 		enable_irq(vector);
7414 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7415 
7416 		if (!i) {
7417 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7418 			if (rc)
7419 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7420 		}
7421 	}
7422 
7423 	for (i = 0; i < bp->tx_nr_rings; i++) {
7424 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7425 
7426 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7427 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
7428 			if (rc)
7429 				goto err_out;
7430 		}
7431 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i);
7432 		if (rc)
7433 			goto err_out;
7434 	}
7435 
7436 	for (i = 0; i < bp->rx_nr_rings; i++) {
7437 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7438 
7439 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7440 		if (rc)
7441 			goto err_out;
7442 		/* If we have agg rings, post agg buffers first. */
7443 		if (!agg_rings)
7444 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7445 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7446 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
7447 			if (rc)
7448 				goto err_out;
7449 		}
7450 	}
7451 
7452 	if (agg_rings) {
7453 		for (i = 0; i < bp->rx_nr_rings; i++) {
7454 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7455 			if (rc)
7456 				goto err_out;
7457 		}
7458 	}
7459 err_out:
7460 	return rc;
7461 }
7462 
7463 static void bnxt_cancel_dim(struct bnxt *bp)
7464 {
7465 	int i;
7466 
7467 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7468 	 * if NAPI is enabled.
7469 	 */
7470 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7471 		return;
7472 
7473 	/* Make sure NAPI sees that the VNIC is disabled */
7474 	synchronize_net();
7475 	for (i = 0; i < bp->rx_nr_rings; i++) {
7476 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7477 		struct bnxt_napi *bnapi = rxr->bnapi;
7478 
7479 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7480 	}
7481 }
7482 
7483 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7484 				   struct bnxt_ring_struct *ring,
7485 				   u32 ring_type, int cmpl_ring_id)
7486 {
7487 	struct hwrm_ring_free_output *resp;
7488 	struct hwrm_ring_free_input *req;
7489 	u16 error_code = 0;
7490 	int rc;
7491 
7492 	if (BNXT_NO_FW_ACCESS(bp))
7493 		return 0;
7494 
7495 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7496 	if (rc)
7497 		goto exit;
7498 
7499 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7500 	req->ring_type = ring_type;
7501 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7502 
7503 	resp = hwrm_req_hold(bp, req);
7504 	rc = hwrm_req_send(bp, req);
7505 	error_code = le16_to_cpu(resp->error_code);
7506 	hwrm_req_drop(bp, req);
7507 exit:
7508 	if (rc || error_code) {
7509 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7510 			   ring_type, rc, error_code);
7511 		return -EIO;
7512 	}
7513 	return 0;
7514 }
7515 
7516 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp,
7517 				   struct bnxt_tx_ring_info *txr,
7518 				   bool close_path)
7519 {
7520 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7521 	u32 cmpl_ring_id;
7522 
7523 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7524 		return;
7525 
7526 	cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) :
7527 		       INVALID_HW_RING_ID;
7528 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX,
7529 				cmpl_ring_id);
7530 	ring->fw_ring_id = INVALID_HW_RING_ID;
7531 }
7532 
7533 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7534 				   struct bnxt_rx_ring_info *rxr,
7535 				   bool close_path)
7536 {
7537 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7538 	u32 grp_idx = rxr->bnapi->index;
7539 	u32 cmpl_ring_id;
7540 
7541 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7542 		return;
7543 
7544 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7545 	hwrm_ring_free_send_msg(bp, ring,
7546 				RING_FREE_REQ_RING_TYPE_RX,
7547 				close_path ? cmpl_ring_id :
7548 				INVALID_HW_RING_ID);
7549 	ring->fw_ring_id = INVALID_HW_RING_ID;
7550 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7551 }
7552 
7553 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7554 				       struct bnxt_rx_ring_info *rxr,
7555 				       bool close_path)
7556 {
7557 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7558 	u32 grp_idx = rxr->bnapi->index;
7559 	u32 type, cmpl_ring_id;
7560 
7561 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7562 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7563 	else
7564 		type = RING_FREE_REQ_RING_TYPE_RX;
7565 
7566 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7567 		return;
7568 
7569 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7570 	hwrm_ring_free_send_msg(bp, ring, type,
7571 				close_path ? cmpl_ring_id :
7572 				INVALID_HW_RING_ID);
7573 	ring->fw_ring_id = INVALID_HW_RING_ID;
7574 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7575 }
7576 
7577 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp,
7578 				   struct bnxt_cp_ring_info *cpr)
7579 {
7580 	struct bnxt_ring_struct *ring;
7581 
7582 	ring = &cpr->cp_ring_struct;
7583 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7584 		return;
7585 
7586 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL,
7587 				INVALID_HW_RING_ID);
7588 	ring->fw_ring_id = INVALID_HW_RING_ID;
7589 }
7590 
7591 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
7592 {
7593 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7594 	int i, size = ring->ring_mem.page_size;
7595 
7596 	cpr->cp_raw_cons = 0;
7597 	cpr->toggle = 0;
7598 
7599 	for (i = 0; i < bp->cp_nr_pages; i++)
7600 		if (cpr->cp_desc_ring[i])
7601 			memset(cpr->cp_desc_ring[i], 0, size);
7602 }
7603 
7604 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7605 {
7606 	u32 type;
7607 	int i;
7608 
7609 	if (!bp->bnapi)
7610 		return;
7611 
7612 	for (i = 0; i < bp->tx_nr_rings; i++)
7613 		bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path);
7614 
7615 	bnxt_cancel_dim(bp);
7616 	for (i = 0; i < bp->rx_nr_rings; i++) {
7617 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7618 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7619 	}
7620 
7621 	/* The completion rings are about to be freed.  After that the
7622 	 * IRQ doorbell will not work anymore.  So we need to disable
7623 	 * IRQ here.
7624 	 */
7625 	bnxt_disable_int_sync(bp);
7626 
7627 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7628 		type = RING_FREE_REQ_RING_TYPE_NQ;
7629 	else
7630 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7631 	for (i = 0; i < bp->cp_nr_rings; i++) {
7632 		struct bnxt_napi *bnapi = bp->bnapi[i];
7633 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7634 		struct bnxt_ring_struct *ring;
7635 		int j;
7636 
7637 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++)
7638 			bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]);
7639 
7640 		ring = &cpr->cp_ring_struct;
7641 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7642 			hwrm_ring_free_send_msg(bp, ring, type,
7643 						INVALID_HW_RING_ID);
7644 			ring->fw_ring_id = INVALID_HW_RING_ID;
7645 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7646 		}
7647 	}
7648 }
7649 
7650 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7651 			     bool shared);
7652 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7653 			   bool shared);
7654 
7655 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7656 {
7657 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7658 	struct hwrm_func_qcfg_output *resp;
7659 	struct hwrm_func_qcfg_input *req;
7660 	int rc;
7661 
7662 	if (bp->hwrm_spec_code < 0x10601)
7663 		return 0;
7664 
7665 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7666 	if (rc)
7667 		return rc;
7668 
7669 	req->fid = cpu_to_le16(0xffff);
7670 	resp = hwrm_req_hold(bp, req);
7671 	rc = hwrm_req_send(bp, req);
7672 	if (rc) {
7673 		hwrm_req_drop(bp, req);
7674 		return rc;
7675 	}
7676 
7677 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7678 	if (BNXT_NEW_RM(bp)) {
7679 		u16 cp, stats;
7680 
7681 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7682 		hw_resc->resv_hw_ring_grps =
7683 			le32_to_cpu(resp->alloc_hw_ring_grps);
7684 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7685 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7686 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7687 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7688 		hw_resc->resv_irqs = cp;
7689 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7690 			int rx = hw_resc->resv_rx_rings;
7691 			int tx = hw_resc->resv_tx_rings;
7692 
7693 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7694 				rx >>= 1;
7695 			if (cp < (rx + tx)) {
7696 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7697 				if (rc)
7698 					goto get_rings_exit;
7699 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7700 					rx <<= 1;
7701 				hw_resc->resv_rx_rings = rx;
7702 				hw_resc->resv_tx_rings = tx;
7703 			}
7704 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7705 			hw_resc->resv_hw_ring_grps = rx;
7706 		}
7707 		hw_resc->resv_cp_rings = cp;
7708 		hw_resc->resv_stat_ctxs = stats;
7709 	}
7710 get_rings_exit:
7711 	hwrm_req_drop(bp, req);
7712 	return rc;
7713 }
7714 
7715 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7716 {
7717 	struct hwrm_func_qcfg_output *resp;
7718 	struct hwrm_func_qcfg_input *req;
7719 	int rc;
7720 
7721 	if (bp->hwrm_spec_code < 0x10601)
7722 		return 0;
7723 
7724 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7725 	if (rc)
7726 		return rc;
7727 
7728 	req->fid = cpu_to_le16(fid);
7729 	resp = hwrm_req_hold(bp, req);
7730 	rc = hwrm_req_send(bp, req);
7731 	if (!rc)
7732 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7733 
7734 	hwrm_req_drop(bp, req);
7735 	return rc;
7736 }
7737 
7738 static bool bnxt_rfs_supported(struct bnxt *bp);
7739 
7740 static struct hwrm_func_cfg_input *
7741 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7742 {
7743 	struct hwrm_func_cfg_input *req;
7744 	u32 enables = 0;
7745 
7746 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7747 		return NULL;
7748 
7749 	req->fid = cpu_to_le16(0xffff);
7750 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7751 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7752 	if (BNXT_NEW_RM(bp)) {
7753 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7754 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7755 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7756 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7757 			enables |= hwr->cp_p5 ?
7758 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7759 		} else {
7760 			enables |= hwr->cp ?
7761 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7762 			enables |= hwr->grp ?
7763 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7764 		}
7765 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7766 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7767 					  0;
7768 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7769 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7770 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7771 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7772 			req->num_msix = cpu_to_le16(hwr->cp);
7773 		} else {
7774 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7775 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7776 		}
7777 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7778 		req->num_vnics = cpu_to_le16(hwr->vnic);
7779 	}
7780 	req->enables = cpu_to_le32(enables);
7781 	return req;
7782 }
7783 
7784 static struct hwrm_func_vf_cfg_input *
7785 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7786 {
7787 	struct hwrm_func_vf_cfg_input *req;
7788 	u32 enables = 0;
7789 
7790 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7791 		return NULL;
7792 
7793 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7794 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7795 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7796 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7797 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7798 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7799 		enables |= hwr->cp_p5 ?
7800 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7801 	} else {
7802 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7803 		enables |= hwr->grp ?
7804 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7805 	}
7806 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7807 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7808 
7809 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7810 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7811 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7812 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7813 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7814 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7815 	} else {
7816 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7817 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7818 	}
7819 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7820 	req->num_vnics = cpu_to_le16(hwr->vnic);
7821 
7822 	req->enables = cpu_to_le32(enables);
7823 	return req;
7824 }
7825 
7826 static int
7827 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7828 {
7829 	struct hwrm_func_cfg_input *req;
7830 	int rc;
7831 
7832 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7833 	if (!req)
7834 		return -ENOMEM;
7835 
7836 	if (!req->enables) {
7837 		hwrm_req_drop(bp, req);
7838 		return 0;
7839 	}
7840 
7841 	rc = hwrm_req_send(bp, req);
7842 	if (rc)
7843 		return rc;
7844 
7845 	if (bp->hwrm_spec_code < 0x10601)
7846 		bp->hw_resc.resv_tx_rings = hwr->tx;
7847 
7848 	return bnxt_hwrm_get_rings(bp);
7849 }
7850 
7851 static int
7852 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7853 {
7854 	struct hwrm_func_vf_cfg_input *req;
7855 	int rc;
7856 
7857 	if (!BNXT_NEW_RM(bp)) {
7858 		bp->hw_resc.resv_tx_rings = hwr->tx;
7859 		return 0;
7860 	}
7861 
7862 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7863 	if (!req)
7864 		return -ENOMEM;
7865 
7866 	rc = hwrm_req_send(bp, req);
7867 	if (rc)
7868 		return rc;
7869 
7870 	return bnxt_hwrm_get_rings(bp);
7871 }
7872 
7873 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7874 {
7875 	if (BNXT_PF(bp))
7876 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7877 	else
7878 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7879 }
7880 
7881 int bnxt_nq_rings_in_use(struct bnxt *bp)
7882 {
7883 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7884 }
7885 
7886 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7887 {
7888 	int cp;
7889 
7890 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7891 		return bnxt_nq_rings_in_use(bp);
7892 
7893 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7894 	return cp;
7895 }
7896 
7897 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7898 {
7899 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7900 }
7901 
7902 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7903 {
7904 	if (!hwr->grp)
7905 		return 0;
7906 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7907 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7908 
7909 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7910 			rss_ctx *= hwr->vnic;
7911 		return rss_ctx;
7912 	}
7913 	if (BNXT_VF(bp))
7914 		return BNXT_VF_MAX_RSS_CTX;
7915 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7916 		return hwr->grp + 1;
7917 	return 1;
7918 }
7919 
7920 /* Check if a default RSS map needs to be setup.  This function is only
7921  * used on older firmware that does not require reserving RX rings.
7922  */
7923 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7924 {
7925 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7926 
7927 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7928 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7929 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7930 		if (!netif_is_rxfh_configured(bp->dev))
7931 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7932 	}
7933 }
7934 
7935 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7936 {
7937 	if (bp->flags & BNXT_FLAG_RFS) {
7938 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7939 			return 2 + bp->num_rss_ctx;
7940 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7941 			return rx_rings + 1;
7942 	}
7943 	return 1;
7944 }
7945 
7946 static void bnxt_get_total_resources(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7947 {
7948 	hwr->cp = bnxt_nq_rings_in_use(bp);
7949 	hwr->cp_p5 = 0;
7950 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7951 		hwr->cp_p5 = bnxt_cp_rings_in_use(bp);
7952 	hwr->tx = bp->tx_nr_rings;
7953 	hwr->rx = bp->rx_nr_rings;
7954 	hwr->grp = hwr->rx;
7955 	hwr->vnic = bnxt_get_total_vnics(bp, hwr->rx);
7956 	hwr->rss_ctx = bnxt_get_total_rss_ctxs(bp, hwr);
7957 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7958 		hwr->rx <<= 1;
7959 	hwr->stat = bnxt_get_func_stat_ctxs(bp);
7960 }
7961 
7962 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7963 {
7964 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7965 	struct bnxt_hw_rings hwr;
7966 
7967 	bnxt_get_total_resources(bp, &hwr);
7968 
7969 	/* Old firmware does not need RX ring reservations but we still
7970 	 * need to setup a default RSS map when needed.  With new firmware
7971 	 * we go through RX ring reservations first and then set up the
7972 	 * RSS map for the successfully reserved RX rings when needed.
7973 	 */
7974 	if (!BNXT_NEW_RM(bp))
7975 		bnxt_check_rss_tbl_no_rmgr(bp);
7976 
7977 	if (hw_resc->resv_tx_rings != hwr.tx && bp->hwrm_spec_code >= 0x10601)
7978 		return true;
7979 
7980 	if (!BNXT_NEW_RM(bp))
7981 		return false;
7982 
7983 	if (hw_resc->resv_rx_rings != hwr.rx ||
7984 	    hw_resc->resv_vnics != hwr.vnic ||
7985 	    hw_resc->resv_stat_ctxs != hwr.stat ||
7986 	    hw_resc->resv_rsscos_ctxs != hwr.rss_ctx ||
7987 	    (hw_resc->resv_hw_ring_grps != hwr.grp &&
7988 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7989 		return true;
7990 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7991 		if (hw_resc->resv_cp_rings != hwr.cp_p5)
7992 			return true;
7993 	} else if (hw_resc->resv_cp_rings != hwr.cp) {
7994 		return true;
7995 	}
7996 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7997 	    hw_resc->resv_irqs != hwr.cp)
7998 		return true;
7999 	return false;
8000 }
8001 
8002 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8003 {
8004 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8005 
8006 	hwr->tx = hw_resc->resv_tx_rings;
8007 	if (BNXT_NEW_RM(bp)) {
8008 		hwr->rx = hw_resc->resv_rx_rings;
8009 		hwr->cp = hw_resc->resv_irqs;
8010 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8011 			hwr->cp_p5 = hw_resc->resv_cp_rings;
8012 		hwr->grp = hw_resc->resv_hw_ring_grps;
8013 		hwr->vnic = hw_resc->resv_vnics;
8014 		hwr->stat = hw_resc->resv_stat_ctxs;
8015 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
8016 	}
8017 }
8018 
8019 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8020 {
8021 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
8022 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
8023 }
8024 
8025 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
8026 
8027 static int __bnxt_reserve_rings(struct bnxt *bp)
8028 {
8029 	struct bnxt_hw_rings hwr = {0};
8030 	int rx_rings, old_rx_rings, rc;
8031 	int cp = bp->cp_nr_rings;
8032 	int ulp_msix = 0;
8033 	bool sh = false;
8034 	int tx_cp;
8035 
8036 	if (!bnxt_need_reserve_rings(bp))
8037 		return 0;
8038 
8039 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
8040 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
8041 		if (!ulp_msix)
8042 			bnxt_set_ulp_stat_ctxs(bp, 0);
8043 
8044 		if (ulp_msix > bp->ulp_num_msix_want)
8045 			ulp_msix = bp->ulp_num_msix_want;
8046 		hwr.cp = cp + ulp_msix;
8047 	} else {
8048 		hwr.cp = bnxt_nq_rings_in_use(bp);
8049 	}
8050 
8051 	hwr.tx = bp->tx_nr_rings;
8052 	hwr.rx = bp->rx_nr_rings;
8053 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
8054 		sh = true;
8055 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8056 		hwr.cp_p5 = hwr.rx + hwr.tx;
8057 
8058 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
8059 
8060 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8061 		hwr.rx <<= 1;
8062 	hwr.grp = bp->rx_nr_rings;
8063 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
8064 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
8065 	old_rx_rings = bp->hw_resc.resv_rx_rings;
8066 
8067 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
8068 	if (rc)
8069 		return rc;
8070 
8071 	bnxt_copy_reserved_rings(bp, &hwr);
8072 
8073 	rx_rings = hwr.rx;
8074 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8075 		if (hwr.rx >= 2) {
8076 			rx_rings = hwr.rx >> 1;
8077 		} else {
8078 			if (netif_running(bp->dev))
8079 				return -ENOMEM;
8080 
8081 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
8082 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
8083 			bp->dev->hw_features &= ~NETIF_F_LRO;
8084 			bp->dev->features &= ~NETIF_F_LRO;
8085 			bnxt_set_ring_params(bp);
8086 		}
8087 	}
8088 	rx_rings = min_t(int, rx_rings, hwr.grp);
8089 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
8090 	if (bnxt_ulp_registered(bp->edev) &&
8091 	    hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
8092 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
8093 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
8094 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
8095 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8096 		hwr.rx = rx_rings << 1;
8097 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
8098 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
8099 	if (hwr.tx != bp->tx_nr_rings) {
8100 		netdev_warn(bp->dev,
8101 			    "Able to reserve only %d out of %d requested TX rings\n",
8102 			    hwr.tx, bp->tx_nr_rings);
8103 	}
8104 	bp->tx_nr_rings = hwr.tx;
8105 
8106 	/* If we cannot reserve all the RX rings, reset the RSS map only
8107 	 * if absolutely necessary
8108 	 */
8109 	if (rx_rings != bp->rx_nr_rings) {
8110 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
8111 			    rx_rings, bp->rx_nr_rings);
8112 		if (netif_is_rxfh_configured(bp->dev) &&
8113 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
8114 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
8115 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
8116 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
8117 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
8118 		}
8119 	}
8120 	bp->rx_nr_rings = rx_rings;
8121 	bp->cp_nr_rings = hwr.cp;
8122 
8123 	/* Fall back if we cannot reserve enough HW RSS contexts */
8124 	if ((bp->rss_cap & BNXT_RSS_CAP_LARGE_RSS_CTX) &&
8125 	    hwr.rss_ctx < bnxt_get_total_rss_ctxs(bp, &hwr))
8126 		bp->rss_cap &= ~BNXT_RSS_CAP_LARGE_RSS_CTX;
8127 
8128 	if (!bnxt_rings_ok(bp, &hwr))
8129 		return -ENOMEM;
8130 
8131 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
8132 	    !netif_is_rxfh_configured(bp->dev))
8133 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8134 
8135 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
8136 		int resv_msix, resv_ctx, ulp_ctxs;
8137 		struct bnxt_hw_resc *hw_resc;
8138 
8139 		hw_resc = &bp->hw_resc;
8140 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
8141 		ulp_msix = min_t(int, resv_msix, ulp_msix);
8142 		bnxt_set_ulp_msix_num(bp, ulp_msix);
8143 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
8144 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
8145 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
8146 	}
8147 
8148 	return rc;
8149 }
8150 
8151 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8152 {
8153 	struct hwrm_func_vf_cfg_input *req;
8154 	u32 flags;
8155 
8156 	if (!BNXT_NEW_RM(bp))
8157 		return 0;
8158 
8159 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
8160 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
8161 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8162 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8163 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8164 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
8165 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
8166 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8167 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8168 
8169 	req->flags = cpu_to_le32(flags);
8170 	return hwrm_req_send_silent(bp, req);
8171 }
8172 
8173 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8174 {
8175 	struct hwrm_func_cfg_input *req;
8176 	u32 flags;
8177 
8178 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
8179 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
8180 	if (BNXT_NEW_RM(bp)) {
8181 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8182 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8183 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8184 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
8185 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8186 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
8187 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
8188 		else
8189 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8190 	}
8191 
8192 	req->flags = cpu_to_le32(flags);
8193 	return hwrm_req_send_silent(bp, req);
8194 }
8195 
8196 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8197 {
8198 	if (bp->hwrm_spec_code < 0x10801)
8199 		return 0;
8200 
8201 	if (BNXT_PF(bp))
8202 		return bnxt_hwrm_check_pf_rings(bp, hwr);
8203 
8204 	return bnxt_hwrm_check_vf_rings(bp, hwr);
8205 }
8206 
8207 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
8208 {
8209 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8210 	struct hwrm_ring_aggint_qcaps_output *resp;
8211 	struct hwrm_ring_aggint_qcaps_input *req;
8212 	int rc;
8213 
8214 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
8215 	coal_cap->num_cmpl_dma_aggr_max = 63;
8216 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
8217 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
8218 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
8219 	coal_cap->int_lat_tmr_min_max = 65535;
8220 	coal_cap->int_lat_tmr_max_max = 65535;
8221 	coal_cap->num_cmpl_aggr_int_max = 65535;
8222 	coal_cap->timer_units = 80;
8223 
8224 	if (bp->hwrm_spec_code < 0x10902)
8225 		return;
8226 
8227 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
8228 		return;
8229 
8230 	resp = hwrm_req_hold(bp, req);
8231 	rc = hwrm_req_send_silent(bp, req);
8232 	if (!rc) {
8233 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8234 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8235 		coal_cap->num_cmpl_dma_aggr_max =
8236 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8237 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8238 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8239 		coal_cap->cmpl_aggr_dma_tmr_max =
8240 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8241 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8242 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8243 		coal_cap->int_lat_tmr_min_max =
8244 			le16_to_cpu(resp->int_lat_tmr_min_max);
8245 		coal_cap->int_lat_tmr_max_max =
8246 			le16_to_cpu(resp->int_lat_tmr_max_max);
8247 		coal_cap->num_cmpl_aggr_int_max =
8248 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8249 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8250 	}
8251 	hwrm_req_drop(bp, req);
8252 }
8253 
8254 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8255 {
8256 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8257 
8258 	return usec * 1000 / coal_cap->timer_units;
8259 }
8260 
8261 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8262 	struct bnxt_coal *hw_coal,
8263 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8264 {
8265 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8266 	u16 val, tmr, max, flags = hw_coal->flags;
8267 	u32 cmpl_params = coal_cap->cmpl_params;
8268 
8269 	max = hw_coal->bufs_per_record * 128;
8270 	if (hw_coal->budget)
8271 		max = hw_coal->bufs_per_record * hw_coal->budget;
8272 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8273 
8274 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8275 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8276 
8277 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8278 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8279 
8280 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8281 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8282 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8283 
8284 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8285 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8286 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8287 
8288 	/* min timer set to 1/2 of interrupt timer */
8289 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8290 		val = tmr / 2;
8291 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8292 		req->int_lat_tmr_min = cpu_to_le16(val);
8293 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8294 	}
8295 
8296 	/* buf timer set to 1/4 of interrupt timer */
8297 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8298 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8299 
8300 	if (cmpl_params &
8301 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8302 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8303 		val = clamp_t(u16, tmr, 1,
8304 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8305 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8306 		req->enables |=
8307 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8308 	}
8309 
8310 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8311 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8312 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8313 	req->flags = cpu_to_le16(flags);
8314 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8315 }
8316 
8317 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8318 				   struct bnxt_coal *hw_coal)
8319 {
8320 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8321 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8322 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8323 	u32 nq_params = coal_cap->nq_params;
8324 	u16 tmr;
8325 	int rc;
8326 
8327 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8328 		return 0;
8329 
8330 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8331 	if (rc)
8332 		return rc;
8333 
8334 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8335 	req->flags =
8336 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8337 
8338 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8339 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8340 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8341 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8342 	return hwrm_req_send(bp, req);
8343 }
8344 
8345 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8346 {
8347 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8348 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8349 	struct bnxt_coal coal;
8350 	int rc;
8351 
8352 	/* Tick values in micro seconds.
8353 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8354 	 */
8355 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8356 
8357 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8358 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8359 
8360 	if (!bnapi->rx_ring)
8361 		return -ENODEV;
8362 
8363 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8364 	if (rc)
8365 		return rc;
8366 
8367 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8368 
8369 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8370 
8371 	return hwrm_req_send(bp, req_rx);
8372 }
8373 
8374 static int
8375 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8376 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8377 {
8378 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8379 
8380 	req->ring_id = cpu_to_le16(ring_id);
8381 	return hwrm_req_send(bp, req);
8382 }
8383 
8384 static int
8385 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8386 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8387 {
8388 	struct bnxt_tx_ring_info *txr;
8389 	int i, rc;
8390 
8391 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8392 		u16 ring_id;
8393 
8394 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8395 		req->ring_id = cpu_to_le16(ring_id);
8396 		rc = hwrm_req_send(bp, req);
8397 		if (rc)
8398 			return rc;
8399 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8400 			return 0;
8401 	}
8402 	return 0;
8403 }
8404 
8405 int bnxt_hwrm_set_coal(struct bnxt *bp)
8406 {
8407 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8408 	int i, rc;
8409 
8410 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8411 	if (rc)
8412 		return rc;
8413 
8414 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8415 	if (rc) {
8416 		hwrm_req_drop(bp, req_rx);
8417 		return rc;
8418 	}
8419 
8420 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8421 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8422 
8423 	hwrm_req_hold(bp, req_rx);
8424 	hwrm_req_hold(bp, req_tx);
8425 	for (i = 0; i < bp->cp_nr_rings; i++) {
8426 		struct bnxt_napi *bnapi = bp->bnapi[i];
8427 		struct bnxt_coal *hw_coal;
8428 
8429 		if (!bnapi->rx_ring)
8430 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8431 		else
8432 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8433 		if (rc)
8434 			break;
8435 
8436 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8437 			continue;
8438 
8439 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8440 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8441 			if (rc)
8442 				break;
8443 		}
8444 		if (bnapi->rx_ring)
8445 			hw_coal = &bp->rx_coal;
8446 		else
8447 			hw_coal = &bp->tx_coal;
8448 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8449 	}
8450 	hwrm_req_drop(bp, req_rx);
8451 	hwrm_req_drop(bp, req_tx);
8452 	return rc;
8453 }
8454 
8455 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8456 {
8457 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8458 	struct hwrm_stat_ctx_free_input *req;
8459 	int i;
8460 
8461 	if (!bp->bnapi)
8462 		return;
8463 
8464 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8465 		return;
8466 
8467 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8468 		return;
8469 	if (BNXT_FW_MAJ(bp) <= 20) {
8470 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8471 			hwrm_req_drop(bp, req);
8472 			return;
8473 		}
8474 		hwrm_req_hold(bp, req0);
8475 	}
8476 	hwrm_req_hold(bp, req);
8477 	for (i = 0; i < bp->cp_nr_rings; i++) {
8478 		struct bnxt_napi *bnapi = bp->bnapi[i];
8479 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8480 
8481 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8482 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8483 			if (req0) {
8484 				req0->stat_ctx_id = req->stat_ctx_id;
8485 				hwrm_req_send(bp, req0);
8486 			}
8487 			hwrm_req_send(bp, req);
8488 
8489 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8490 		}
8491 	}
8492 	hwrm_req_drop(bp, req);
8493 	if (req0)
8494 		hwrm_req_drop(bp, req0);
8495 }
8496 
8497 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8498 {
8499 	struct hwrm_stat_ctx_alloc_output *resp;
8500 	struct hwrm_stat_ctx_alloc_input *req;
8501 	int rc, i;
8502 
8503 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8504 		return 0;
8505 
8506 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8507 	if (rc)
8508 		return rc;
8509 
8510 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8511 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8512 
8513 	resp = hwrm_req_hold(bp, req);
8514 	for (i = 0; i < bp->cp_nr_rings; i++) {
8515 		struct bnxt_napi *bnapi = bp->bnapi[i];
8516 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8517 
8518 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8519 
8520 		rc = hwrm_req_send(bp, req);
8521 		if (rc)
8522 			break;
8523 
8524 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8525 
8526 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8527 	}
8528 	hwrm_req_drop(bp, req);
8529 	return rc;
8530 }
8531 
8532 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8533 {
8534 	struct hwrm_func_qcfg_output *resp;
8535 	struct hwrm_func_qcfg_input *req;
8536 	u16 flags;
8537 	int rc;
8538 
8539 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8540 	if (rc)
8541 		return rc;
8542 
8543 	req->fid = cpu_to_le16(0xffff);
8544 	resp = hwrm_req_hold(bp, req);
8545 	rc = hwrm_req_send(bp, req);
8546 	if (rc)
8547 		goto func_qcfg_exit;
8548 
8549 	flags = le16_to_cpu(resp->flags);
8550 #ifdef CONFIG_BNXT_SRIOV
8551 	if (BNXT_VF(bp)) {
8552 		struct bnxt_vf_info *vf = &bp->vf;
8553 
8554 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8555 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8556 			vf->flags |= BNXT_VF_TRUST;
8557 		else
8558 			vf->flags &= ~BNXT_VF_TRUST;
8559 	} else {
8560 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8561 	}
8562 #endif
8563 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8564 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8565 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8566 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8567 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8568 	}
8569 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8570 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8571 
8572 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8573 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8574 
8575 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8576 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8577 	if (resp->roce_bidi_opt_mode &
8578 	    FUNC_QCFG_RESP_ROCE_BIDI_OPT_MODE_DEDICATED)
8579 		bp->cos0_cos1_shared = 1;
8580 	else
8581 		bp->cos0_cos1_shared = 0;
8582 
8583 	switch (resp->port_partition_type) {
8584 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8585 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2:
8586 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8587 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8588 		bp->port_partition_type = resp->port_partition_type;
8589 		break;
8590 	}
8591 	if (bp->hwrm_spec_code < 0x10707 ||
8592 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8593 		bp->br_mode = BRIDGE_MODE_VEB;
8594 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8595 		bp->br_mode = BRIDGE_MODE_VEPA;
8596 	else
8597 		bp->br_mode = BRIDGE_MODE_UNDEF;
8598 
8599 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8600 	if (!bp->max_mtu)
8601 		bp->max_mtu = BNXT_MAX_MTU;
8602 
8603 	if (bp->db_size)
8604 		goto func_qcfg_exit;
8605 
8606 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8607 	if (BNXT_CHIP_P5(bp)) {
8608 		if (BNXT_PF(bp))
8609 			bp->db_offset = DB_PF_OFFSET_P5;
8610 		else
8611 			bp->db_offset = DB_VF_OFFSET_P5;
8612 	}
8613 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8614 				 1024);
8615 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8616 	    bp->db_size <= bp->db_offset)
8617 		bp->db_size = pci_resource_len(bp->pdev, 2);
8618 
8619 func_qcfg_exit:
8620 	hwrm_req_drop(bp, req);
8621 	return rc;
8622 }
8623 
8624 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8625 				      u8 init_val, u8 init_offset,
8626 				      bool init_mask_set)
8627 {
8628 	ctxm->init_value = init_val;
8629 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8630 	if (init_mask_set)
8631 		ctxm->init_offset = init_offset * 4;
8632 	else
8633 		ctxm->init_value = 0;
8634 }
8635 
8636 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8637 {
8638 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8639 	u16 type;
8640 
8641 	for (type = 0; type < ctx_max; type++) {
8642 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8643 		int n = 1;
8644 
8645 		if (!ctxm->max_entries || ctxm->pg_info)
8646 			continue;
8647 
8648 		if (ctxm->instance_bmap)
8649 			n = hweight32(ctxm->instance_bmap);
8650 		ctxm->pg_info = kzalloc_objs(*ctxm->pg_info, n);
8651 		if (!ctxm->pg_info)
8652 			return -ENOMEM;
8653 	}
8654 	return 0;
8655 }
8656 
8657 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8658 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8659 
8660 #define BNXT_CTX_INIT_VALID(flags)	\
8661 	(!!((flags) &			\
8662 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8663 
8664 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8665 {
8666 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8667 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8668 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8669 	u16 type;
8670 	int rc;
8671 
8672 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8673 	if (rc)
8674 		return rc;
8675 
8676 	if (!ctx) {
8677 		ctx = kzalloc_obj(*ctx);
8678 		if (!ctx)
8679 			return -ENOMEM;
8680 		bp->ctx = ctx;
8681 	}
8682 
8683 	resp = hwrm_req_hold(bp, req);
8684 
8685 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8686 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8687 		u8 init_val, init_off, i;
8688 		u32 max_entries;
8689 		u16 entry_size;
8690 		__le32 *p;
8691 		u32 flags;
8692 
8693 		req->type = cpu_to_le16(type);
8694 		rc = hwrm_req_send(bp, req);
8695 		if (rc)
8696 			goto ctx_done;
8697 		flags = le32_to_cpu(resp->flags);
8698 		type = le16_to_cpu(resp->next_valid_type);
8699 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8700 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8701 			continue;
8702 		}
8703 		entry_size = le16_to_cpu(resp->entry_size);
8704 		max_entries = le32_to_cpu(resp->max_num_entries);
8705 		if (ctxm->mem_valid) {
8706 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8707 			    ctxm->entry_size != entry_size ||
8708 			    ctxm->max_entries != max_entries)
8709 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8710 			else
8711 				continue;
8712 		}
8713 		ctxm->type = le16_to_cpu(resp->type);
8714 		ctxm->entry_size = entry_size;
8715 		ctxm->flags = flags;
8716 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8717 		ctxm->entry_multiple = resp->entry_multiple;
8718 		ctxm->max_entries = max_entries;
8719 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8720 		init_val = resp->ctx_init_value;
8721 		init_off = resp->ctx_init_offset;
8722 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8723 					  BNXT_CTX_INIT_VALID(flags));
8724 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8725 					      BNXT_MAX_SPLIT_ENTRY);
8726 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8727 		     i++, p++)
8728 			ctxm->split[i] = le32_to_cpu(*p);
8729 	}
8730 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8731 
8732 ctx_done:
8733 	hwrm_req_drop(bp, req);
8734 	return rc;
8735 }
8736 
8737 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8738 {
8739 	struct hwrm_func_backing_store_qcaps_output *resp;
8740 	struct hwrm_func_backing_store_qcaps_input *req;
8741 	int rc;
8742 
8743 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8744 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8745 		return 0;
8746 
8747 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8748 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8749 
8750 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8751 	if (rc)
8752 		return rc;
8753 
8754 	resp = hwrm_req_hold(bp, req);
8755 	rc = hwrm_req_send_silent(bp, req);
8756 	if (!rc) {
8757 		struct bnxt_ctx_mem_type *ctxm;
8758 		struct bnxt_ctx_mem_info *ctx;
8759 		u8 init_val, init_idx = 0;
8760 		u16 init_mask;
8761 
8762 		ctx = bp->ctx;
8763 		if (!ctx) {
8764 			ctx = kzalloc_obj(*ctx);
8765 			if (!ctx) {
8766 				rc = -ENOMEM;
8767 				goto ctx_err;
8768 			}
8769 			bp->ctx = ctx;
8770 		}
8771 		init_val = resp->ctx_kind_initializer;
8772 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8773 
8774 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8775 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8776 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8777 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8778 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8779 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8780 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8781 					  (init_mask & (1 << init_idx++)) != 0);
8782 
8783 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8784 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8785 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8786 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8787 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8788 					  (init_mask & (1 << init_idx++)) != 0);
8789 
8790 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8791 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8792 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8793 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8794 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8795 					  (init_mask & (1 << init_idx++)) != 0);
8796 
8797 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8798 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8799 		ctxm->max_entries = ctxm->vnic_entries +
8800 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8801 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8802 		bnxt_init_ctx_initializer(ctxm, init_val,
8803 					  resp->vnic_init_offset,
8804 					  (init_mask & (1 << init_idx++)) != 0);
8805 
8806 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8807 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8808 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8809 		bnxt_init_ctx_initializer(ctxm, init_val,
8810 					  resp->stat_init_offset,
8811 					  (init_mask & (1 << init_idx++)) != 0);
8812 
8813 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8814 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8815 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8816 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8817 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8818 		if (!ctxm->entry_multiple)
8819 			ctxm->entry_multiple = 1;
8820 
8821 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8822 
8823 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8824 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8825 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8826 		ctxm->mrav_num_entries_units =
8827 			le16_to_cpu(resp->mrav_num_entries_units);
8828 		bnxt_init_ctx_initializer(ctxm, init_val,
8829 					  resp->mrav_init_offset,
8830 					  (init_mask & (1 << init_idx++)) != 0);
8831 
8832 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8833 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8834 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8835 
8836 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8837 		if (!ctx->tqm_fp_rings_count)
8838 			ctx->tqm_fp_rings_count = bp->max_q;
8839 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8840 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8841 
8842 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8843 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8844 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8845 
8846 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8847 	} else {
8848 		rc = 0;
8849 	}
8850 ctx_err:
8851 	hwrm_req_drop(bp, req);
8852 	return rc;
8853 }
8854 
8855 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8856 				  __le64 *pg_dir)
8857 {
8858 	if (!rmem->nr_pages)
8859 		return;
8860 
8861 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8862 	if (rmem->depth >= 1) {
8863 		if (rmem->depth == 2)
8864 			*pg_attr |= 2;
8865 		else
8866 			*pg_attr |= 1;
8867 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8868 	} else {
8869 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8870 	}
8871 }
8872 
8873 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8874 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8875 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8876 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8877 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8878 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8879 
8880 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8881 {
8882 	struct hwrm_func_backing_store_cfg_input *req;
8883 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8884 	struct bnxt_ctx_pg_info *ctx_pg;
8885 	struct bnxt_ctx_mem_type *ctxm;
8886 	void **__req = (void **)&req;
8887 	u32 req_len = sizeof(*req);
8888 	__le32 *num_entries;
8889 	__le64 *pg_dir;
8890 	u32 flags = 0;
8891 	u8 *pg_attr;
8892 	u32 ena;
8893 	int rc;
8894 	int i;
8895 
8896 	if (!ctx)
8897 		return 0;
8898 
8899 	if (req_len > bp->hwrm_max_ext_req_len)
8900 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8901 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8902 	if (rc)
8903 		return rc;
8904 
8905 	req->enables = cpu_to_le32(enables);
8906 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8907 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8908 		ctx_pg = ctxm->pg_info;
8909 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8910 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8911 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8912 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8913 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8914 				      &req->qpc_pg_size_qpc_lvl,
8915 				      &req->qpc_page_dir);
8916 
8917 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8918 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8919 	}
8920 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8921 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8922 		ctx_pg = ctxm->pg_info;
8923 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8924 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8925 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8926 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8927 				      &req->srq_pg_size_srq_lvl,
8928 				      &req->srq_page_dir);
8929 	}
8930 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8931 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8932 		ctx_pg = ctxm->pg_info;
8933 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8934 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8935 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8936 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8937 				      &req->cq_pg_size_cq_lvl,
8938 				      &req->cq_page_dir);
8939 	}
8940 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8941 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8942 		ctx_pg = ctxm->pg_info;
8943 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8944 		req->vnic_num_ring_table_entries =
8945 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8946 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8947 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8948 				      &req->vnic_pg_size_vnic_lvl,
8949 				      &req->vnic_page_dir);
8950 	}
8951 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8952 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8953 		ctx_pg = ctxm->pg_info;
8954 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8955 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8956 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8957 				      &req->stat_pg_size_stat_lvl,
8958 				      &req->stat_page_dir);
8959 	}
8960 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8961 		u32 units;
8962 
8963 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8964 		ctx_pg = ctxm->pg_info;
8965 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8966 		units = ctxm->mrav_num_entries_units;
8967 		if (units) {
8968 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8969 			u32 entries;
8970 
8971 			num_mr = ctx_pg->entries - num_ah;
8972 			entries = ((num_mr / units) << 16) | (num_ah / units);
8973 			req->mrav_num_entries = cpu_to_le32(entries);
8974 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8975 		}
8976 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8977 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8978 				      &req->mrav_pg_size_mrav_lvl,
8979 				      &req->mrav_page_dir);
8980 	}
8981 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8982 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8983 		ctx_pg = ctxm->pg_info;
8984 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8985 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8986 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8987 				      &req->tim_pg_size_tim_lvl,
8988 				      &req->tim_page_dir);
8989 	}
8990 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8991 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8992 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8993 	     pg_dir = &req->tqm_sp_page_dir,
8994 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8995 	     ctx_pg = ctxm->pg_info;
8996 	     i < BNXT_MAX_TQM_RINGS;
8997 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8998 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8999 		if (!(enables & ena))
9000 			continue;
9001 
9002 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
9003 		*num_entries = cpu_to_le32(ctx_pg->entries);
9004 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
9005 	}
9006 	req->flags = cpu_to_le32(flags);
9007 	return hwrm_req_send(bp, req);
9008 }
9009 
9010 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
9011 				  struct bnxt_ctx_pg_info *ctx_pg)
9012 {
9013 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9014 
9015 	rmem->page_size = BNXT_PAGE_SIZE;
9016 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
9017 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
9018 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
9019 	if (rmem->depth >= 1)
9020 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
9021 	return bnxt_alloc_ring(bp, rmem);
9022 }
9023 
9024 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
9025 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
9026 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
9027 {
9028 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9029 	int rc;
9030 
9031 	if (!mem_size)
9032 		return -EINVAL;
9033 
9034 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
9035 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
9036 		ctx_pg->nr_pages = 0;
9037 		return -EINVAL;
9038 	}
9039 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
9040 		int nr_tbls, i;
9041 
9042 		rmem->depth = 2;
9043 		ctx_pg->ctx_pg_tbl = kzalloc_objs(ctx_pg, MAX_CTX_PAGES);
9044 		if (!ctx_pg->ctx_pg_tbl)
9045 			return -ENOMEM;
9046 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
9047 		rmem->nr_pages = nr_tbls;
9048 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
9049 		if (rc)
9050 			return rc;
9051 		for (i = 0; i < nr_tbls; i++) {
9052 			struct bnxt_ctx_pg_info *pg_tbl;
9053 
9054 			pg_tbl = kzalloc_obj(*pg_tbl);
9055 			if (!pg_tbl)
9056 				return -ENOMEM;
9057 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
9058 			rmem = &pg_tbl->ring_mem;
9059 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
9060 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
9061 			rmem->depth = 1;
9062 			rmem->nr_pages = MAX_CTX_PAGES;
9063 			rmem->ctx_mem = ctxm;
9064 			if (i == (nr_tbls - 1)) {
9065 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
9066 
9067 				if (rem)
9068 					rmem->nr_pages = rem;
9069 			}
9070 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
9071 			if (rc)
9072 				break;
9073 		}
9074 	} else {
9075 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
9076 		if (rmem->nr_pages > 1 || depth)
9077 			rmem->depth = 1;
9078 		rmem->ctx_mem = ctxm;
9079 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
9080 	}
9081 	return rc;
9082 }
9083 
9084 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
9085 				    struct bnxt_ctx_pg_info *ctx_pg,
9086 				    void *buf, size_t offset, size_t head,
9087 				    size_t tail)
9088 {
9089 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9090 	size_t nr_pages = ctx_pg->nr_pages;
9091 	int page_size = rmem->page_size;
9092 	size_t len = 0, total_len = 0;
9093 	u16 depth = rmem->depth;
9094 
9095 	tail %= nr_pages * page_size;
9096 	do {
9097 		if (depth > 1) {
9098 			int i = head / (page_size * MAX_CTX_PAGES);
9099 			struct bnxt_ctx_pg_info *pg_tbl;
9100 
9101 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9102 			rmem = &pg_tbl->ring_mem;
9103 		}
9104 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
9105 		head += len;
9106 		offset += len;
9107 		total_len += len;
9108 		if (head >= nr_pages * page_size)
9109 			head = 0;
9110 	} while (head != tail);
9111 	return total_len;
9112 }
9113 
9114 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
9115 				  struct bnxt_ctx_pg_info *ctx_pg)
9116 {
9117 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9118 
9119 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
9120 	    ctx_pg->ctx_pg_tbl) {
9121 		int i, nr_tbls = rmem->nr_pages;
9122 
9123 		for (i = 0; i < nr_tbls; i++) {
9124 			struct bnxt_ctx_pg_info *pg_tbl;
9125 			struct bnxt_ring_mem_info *rmem2;
9126 
9127 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9128 			if (!pg_tbl)
9129 				continue;
9130 			rmem2 = &pg_tbl->ring_mem;
9131 			bnxt_free_ring(bp, rmem2);
9132 			ctx_pg->ctx_pg_arr[i] = NULL;
9133 			kfree(pg_tbl);
9134 			ctx_pg->ctx_pg_tbl[i] = NULL;
9135 		}
9136 		kfree(ctx_pg->ctx_pg_tbl);
9137 		ctx_pg->ctx_pg_tbl = NULL;
9138 	}
9139 	bnxt_free_ring(bp, rmem);
9140 	ctx_pg->nr_pages = 0;
9141 }
9142 
9143 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
9144 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
9145 				   u8 pg_lvl)
9146 {
9147 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9148 	int i, rc = 0, n = 1;
9149 	u32 mem_size;
9150 
9151 	if (!ctxm->entry_size || !ctx_pg)
9152 		return -EINVAL;
9153 	if (ctxm->instance_bmap)
9154 		n = hweight32(ctxm->instance_bmap);
9155 	if (ctxm->entry_multiple)
9156 		entries = roundup(entries, ctxm->entry_multiple);
9157 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
9158 	mem_size = entries * ctxm->entry_size;
9159 	for (i = 0; i < n && !rc; i++) {
9160 		ctx_pg[i].entries = entries;
9161 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
9162 					    ctxm->init_value ? ctxm : NULL);
9163 	}
9164 	if (!rc)
9165 		ctxm->mem_valid = 1;
9166 	return rc;
9167 }
9168 
9169 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
9170 					       struct bnxt_ctx_mem_type *ctxm,
9171 					       bool last)
9172 {
9173 	struct hwrm_func_backing_store_cfg_v2_input *req;
9174 	u32 instance_bmap = ctxm->instance_bmap;
9175 	int i, j, rc = 0, n = 1;
9176 	__le32 *p;
9177 
9178 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
9179 		return 0;
9180 
9181 	if (instance_bmap)
9182 		n = hweight32(ctxm->instance_bmap);
9183 	else
9184 		instance_bmap = 1;
9185 
9186 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
9187 	if (rc)
9188 		return rc;
9189 	hwrm_req_hold(bp, req);
9190 	req->type = cpu_to_le16(ctxm->type);
9191 	req->entry_size = cpu_to_le16(ctxm->entry_size);
9192 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
9193 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
9194 		struct bnxt_bs_trace_info *bs_trace;
9195 		u32 enables;
9196 
9197 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
9198 		req->enables = cpu_to_le32(enables);
9199 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
9200 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
9201 	}
9202 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
9203 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
9204 		p[i] = cpu_to_le32(ctxm->split[i]);
9205 	for (i = 0, j = 0; j < n && !rc; i++) {
9206 		struct bnxt_ctx_pg_info *ctx_pg;
9207 
9208 		if (!(instance_bmap & (1 << i)))
9209 			continue;
9210 		req->instance = cpu_to_le16(i);
9211 		ctx_pg = &ctxm->pg_info[j++];
9212 		if (!ctx_pg->entries)
9213 			continue;
9214 		req->num_entries = cpu_to_le32(ctx_pg->entries);
9215 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9216 				      &req->page_size_pbl_level,
9217 				      &req->page_dir);
9218 		if (last && j == n)
9219 			req->flags =
9220 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
9221 		rc = hwrm_req_send(bp, req);
9222 	}
9223 	hwrm_req_drop(bp, req);
9224 	return rc;
9225 }
9226 
9227 static int bnxt_backing_store_cfg_v2(struct bnxt *bp)
9228 {
9229 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9230 	struct bnxt_ctx_mem_type *ctxm;
9231 	u16 last_type = BNXT_CTX_INV;
9232 	int rc = 0;
9233 	u16 type;
9234 
9235 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_QPC; type++) {
9236 		ctxm = &ctx->ctx_arr[type];
9237 		if (!bnxt_bs_trace_avail(bp, type))
9238 			continue;
9239 		if (!ctxm->mem_valid) {
9240 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9241 						     ctxm->max_entries, 1);
9242 			if (rc) {
9243 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9244 					    type);
9245 				continue;
9246 			}
9247 			bnxt_bs_trace_init(bp, ctxm);
9248 		}
9249 		last_type = type;
9250 	}
9251 
9252 	if (last_type == BNXT_CTX_INV) {
9253 		for (type = 0; type < BNXT_CTX_MAX; type++) {
9254 			ctxm = &ctx->ctx_arr[type];
9255 			if (ctxm->mem_valid)
9256 				last_type = type;
9257 		}
9258 		if (last_type == BNXT_CTX_INV)
9259 			return 0;
9260 	}
9261 	ctx->ctx_arr[last_type].last = 1;
9262 
9263 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9264 		ctxm = &ctx->ctx_arr[type];
9265 
9266 		if (!ctxm->mem_valid)
9267 			continue;
9268 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9269 		if (rc)
9270 			return rc;
9271 	}
9272 	return 0;
9273 }
9274 
9275 /**
9276  * __bnxt_copy_ctx_mem - copy host context memory
9277  * @bp: The driver context
9278  * @ctxm: The pointer to the context memory type
9279  * @buf: The destination buffer or NULL to just obtain the length
9280  * @offset: The buffer offset to copy the data to
9281  * @head: The head offset of context memory to copy from
9282  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9283  *
9284  * This function is called for debugging purposes to dump the host context
9285  * used by the chip.
9286  *
9287  * Return: Length of memory copied
9288  */
9289 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9290 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9291 				  size_t offset, size_t head, size_t tail)
9292 {
9293 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9294 	size_t len = 0, total_len = 0;
9295 	int i, n = 1;
9296 
9297 	if (!ctx_pg)
9298 		return 0;
9299 
9300 	if (ctxm->instance_bmap)
9301 		n = hweight32(ctxm->instance_bmap);
9302 	for (i = 0; i < n; i++) {
9303 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9304 					    tail);
9305 		offset += len;
9306 		total_len += len;
9307 	}
9308 	return total_len;
9309 }
9310 
9311 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9312 			 void *buf, size_t offset)
9313 {
9314 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9315 
9316 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9317 }
9318 
9319 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9320 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9321 {
9322 	struct bnxt_ctx_pg_info *ctx_pg;
9323 	int i, n = 1;
9324 
9325 	ctxm->last = 0;
9326 
9327 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9328 		return;
9329 
9330 	ctx_pg = ctxm->pg_info;
9331 	if (ctx_pg) {
9332 		if (ctxm->instance_bmap)
9333 			n = hweight32(ctxm->instance_bmap);
9334 		for (i = 0; i < n; i++)
9335 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9336 
9337 		kfree(ctx_pg);
9338 		ctxm->pg_info = NULL;
9339 		ctxm->mem_valid = 0;
9340 	}
9341 	memset(ctxm, 0, sizeof(*ctxm));
9342 }
9343 
9344 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9345 {
9346 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9347 	u16 type;
9348 
9349 	if (!ctx)
9350 		return;
9351 
9352 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9353 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9354 
9355 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9356 	if (force) {
9357 		kfree(ctx);
9358 		bp->ctx = NULL;
9359 	}
9360 }
9361 
9362 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9363 {
9364 	struct bnxt_ctx_mem_type *ctxm;
9365 	struct bnxt_ctx_mem_info *ctx;
9366 	u32 l2_qps, qp1_qps, max_qps;
9367 	u32 ena, entries_sp, entries;
9368 	u32 srqs, max_srqs, min;
9369 	u32 num_mr, num_ah;
9370 	u32 extra_srqs = 0;
9371 	u32 extra_qps = 0;
9372 	u32 fast_qpmd_qps;
9373 	u8 pg_lvl = 1;
9374 	int i, rc;
9375 
9376 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9377 	if (rc) {
9378 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9379 			   rc);
9380 		return rc;
9381 	}
9382 	ctx = bp->ctx;
9383 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9384 		return 0;
9385 
9386 	ena = 0;
9387 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
9388 		goto skip_legacy;
9389 
9390 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9391 	l2_qps = ctxm->qp_l2_entries;
9392 	qp1_qps = ctxm->qp_qp1_entries;
9393 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9394 	max_qps = ctxm->max_entries;
9395 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9396 	srqs = ctxm->srq_l2_entries;
9397 	max_srqs = ctxm->max_entries;
9398 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9399 		pg_lvl = 2;
9400 		if (BNXT_SW_RES_LMT(bp)) {
9401 			extra_qps = max_qps - l2_qps - qp1_qps;
9402 			extra_srqs = max_srqs - srqs;
9403 		} else {
9404 			extra_qps = min_t(u32, 65536,
9405 					  max_qps - l2_qps - qp1_qps);
9406 			/* allocate extra qps if fw supports RoCE fast qp
9407 			 * destroy feature
9408 			 */
9409 			extra_qps += fast_qpmd_qps;
9410 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9411 		}
9412 		if (fast_qpmd_qps)
9413 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9414 	}
9415 
9416 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9417 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9418 				     pg_lvl);
9419 	if (rc)
9420 		return rc;
9421 
9422 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9423 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9424 	if (rc)
9425 		return rc;
9426 
9427 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9428 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9429 				     extra_qps * 2, pg_lvl);
9430 	if (rc)
9431 		return rc;
9432 
9433 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9434 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9435 	if (rc)
9436 		return rc;
9437 
9438 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9439 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9440 	if (rc)
9441 		return rc;
9442 
9443 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9444 		goto skip_rdma;
9445 
9446 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9447 	if (BNXT_SW_RES_LMT(bp) &&
9448 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9449 		num_ah = ctxm->mrav_av_entries;
9450 		num_mr = ctxm->max_entries - num_ah;
9451 	} else {
9452 		/* 128K extra is needed to accommodate static AH context
9453 		 * allocation by f/w.
9454 		 */
9455 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9456 		num_ah = min_t(u32, num_mr, 1024 * 128);
9457 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9458 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9459 			ctxm->mrav_av_entries = num_ah;
9460 	}
9461 
9462 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9463 	if (rc)
9464 		return rc;
9465 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9466 
9467 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9468 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9469 	if (rc)
9470 		return rc;
9471 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9472 
9473 skip_rdma:
9474 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9475 	min = ctxm->min_entries;
9476 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9477 		     2 * (extra_qps + qp1_qps) + min;
9478 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9479 	if (rc)
9480 		return rc;
9481 
9482 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9483 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9484 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9485 	if (rc)
9486 		return rc;
9487 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9488 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9489 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9490 
9491 skip_legacy:
9492 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9493 		rc = bnxt_backing_store_cfg_v2(bp);
9494 	else
9495 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9496 	if (rc) {
9497 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9498 			   rc);
9499 		return rc;
9500 	}
9501 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9502 	return 0;
9503 }
9504 
9505 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9506 {
9507 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9508 	u16 page_attr;
9509 	int rc;
9510 
9511 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9512 		return 0;
9513 
9514 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9515 	if (rc)
9516 		return rc;
9517 
9518 	if (BNXT_PAGE_SIZE == 0x2000)
9519 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9520 	else if (BNXT_PAGE_SIZE == 0x10000)
9521 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9522 	else
9523 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9524 	req->pg_size_lvl = cpu_to_le16(page_attr |
9525 				       bp->fw_crash_mem->ring_mem.depth);
9526 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9527 	req->size = cpu_to_le32(bp->fw_crash_len);
9528 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9529 	return hwrm_req_send(bp, req);
9530 }
9531 
9532 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9533 {
9534 	if (bp->fw_crash_mem) {
9535 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9536 		kfree(bp->fw_crash_mem);
9537 		bp->fw_crash_mem = NULL;
9538 	}
9539 }
9540 
9541 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9542 {
9543 	u32 mem_size = 0;
9544 	int rc;
9545 
9546 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9547 		return 0;
9548 
9549 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9550 	if (rc)
9551 		return rc;
9552 
9553 	mem_size = round_up(mem_size, 4);
9554 
9555 	/* keep and use the existing pages */
9556 	if (bp->fw_crash_mem &&
9557 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9558 		goto alloc_done;
9559 
9560 	if (bp->fw_crash_mem)
9561 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9562 	else
9563 		bp->fw_crash_mem = kzalloc_obj(*bp->fw_crash_mem);
9564 	if (!bp->fw_crash_mem)
9565 		return -ENOMEM;
9566 
9567 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9568 	if (rc) {
9569 		bnxt_free_crash_dump_mem(bp);
9570 		return rc;
9571 	}
9572 
9573 alloc_done:
9574 	bp->fw_crash_len = mem_size;
9575 	return 0;
9576 }
9577 
9578 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9579 {
9580 	struct hwrm_func_resource_qcaps_output *resp;
9581 	struct hwrm_func_resource_qcaps_input *req;
9582 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9583 	int rc;
9584 
9585 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9586 	if (rc)
9587 		return rc;
9588 
9589 	req->fid = cpu_to_le16(0xffff);
9590 	resp = hwrm_req_hold(bp, req);
9591 	rc = hwrm_req_send_silent(bp, req);
9592 	if (rc)
9593 		goto hwrm_func_resc_qcaps_exit;
9594 
9595 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9596 	if (!all)
9597 		goto hwrm_func_resc_qcaps_exit;
9598 
9599 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9600 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9601 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9602 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9603 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9604 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9605 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9606 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9607 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9608 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9609 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9610 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9611 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9612 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9613 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9614 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9615 
9616 	if (hw_resc->max_rsscos_ctxs >=
9617 	    hw_resc->max_vnics * BNXT_LARGE_RSS_TO_VNIC_RATIO)
9618 		bp->rss_cap |= BNXT_RSS_CAP_LARGE_RSS_CTX;
9619 
9620 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9621 		u16 max_msix = le16_to_cpu(resp->max_msix);
9622 
9623 		hw_resc->max_nqs = max_msix;
9624 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9625 	}
9626 
9627 	if (BNXT_PF(bp)) {
9628 		struct bnxt_pf_info *pf = &bp->pf;
9629 
9630 		pf->vf_resv_strategy =
9631 			le16_to_cpu(resp->vf_reservation_strategy);
9632 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9633 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9634 	}
9635 hwrm_func_resc_qcaps_exit:
9636 	hwrm_req_drop(bp, req);
9637 	return rc;
9638 }
9639 
9640 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9641 {
9642 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9643 	struct hwrm_port_mac_ptp_qcfg_input *req;
9644 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9645 	u8 flags;
9646 	int rc;
9647 
9648 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9649 		rc = -ENODEV;
9650 		goto no_ptp;
9651 	}
9652 
9653 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9654 	if (rc)
9655 		goto no_ptp;
9656 
9657 	req->port_id = cpu_to_le16(bp->pf.port_id);
9658 	resp = hwrm_req_hold(bp, req);
9659 	rc = hwrm_req_send(bp, req);
9660 	if (rc)
9661 		goto exit;
9662 
9663 	flags = resp->flags;
9664 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9665 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9666 		rc = -ENODEV;
9667 		goto exit;
9668 	}
9669 	if (!ptp) {
9670 		ptp = kzalloc_obj(*ptp);
9671 		if (!ptp) {
9672 			rc = -ENOMEM;
9673 			goto exit;
9674 		}
9675 		ptp->bp = bp;
9676 		bp->ptp_cfg = ptp;
9677 	}
9678 
9679 	if (flags &
9680 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9681 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9682 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9683 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9684 	} else if (BNXT_CHIP_P5(bp)) {
9685 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9686 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9687 	} else {
9688 		rc = -ENODEV;
9689 		goto exit;
9690 	}
9691 	ptp->rtc_configured =
9692 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9693 	rc = bnxt_ptp_init(bp);
9694 	if (rc)
9695 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9696 exit:
9697 	hwrm_req_drop(bp, req);
9698 	if (!rc)
9699 		return 0;
9700 
9701 no_ptp:
9702 	bnxt_ptp_clear(bp);
9703 	kfree(ptp);
9704 	bp->ptp_cfg = NULL;
9705 	return rc;
9706 }
9707 
9708 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9709 {
9710 	u32 flags, flags_ext, flags_ext2, flags_ext3;
9711 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9712 	struct hwrm_func_qcaps_output *resp;
9713 	struct hwrm_func_qcaps_input *req;
9714 	int rc;
9715 
9716 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9717 	if (rc)
9718 		return rc;
9719 
9720 	req->fid = cpu_to_le16(0xffff);
9721 	resp = hwrm_req_hold(bp, req);
9722 	rc = hwrm_req_send(bp, req);
9723 	if (rc)
9724 		goto hwrm_func_qcaps_exit;
9725 
9726 	flags = le32_to_cpu(resp->flags);
9727 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9728 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9729 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9730 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9731 	if (flags & FUNC_QCAPS_RESP_FLAGS_LINK_ADMIN_STATUS_SUPPORTED)
9732 		bp->fw_cap |= BNXT_FW_CAP_LINK_ADMIN;
9733 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9734 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9735 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9736 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9737 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9738 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9739 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9740 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9741 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9742 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9743 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9744 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9745 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9746 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9747 
9748 	flags_ext = le32_to_cpu(resp->flags_ext);
9749 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9750 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9751 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9752 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9753 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PTM_SUPPORTED)
9754 		bp->fw_cap |= BNXT_FW_CAP_PTP_PTM;
9755 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9756 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9757 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9758 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9759 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9760 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9761 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED)
9762 		bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2;
9763 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9764 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9765 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9766 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9767 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9768 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9769 
9770 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9771 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9772 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9773 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9774 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9775 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9776 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9777 	if (flags_ext2 &
9778 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9779 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9780 	if (BNXT_PF(bp) &&
9781 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9782 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9783 
9784 	flags_ext3 = le32_to_cpu(resp->flags_ext3);
9785 	if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_ROCE_VF_DYN_ALLOC_SUPPORT)
9786 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_DYN_ALLOC_SUPPORT;
9787 	if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_MIRROR_ON_ROCE_SUPPORTED)
9788 		bp->fw_cap |= BNXT_FW_CAP_MIRROR_ON_ROCE;
9789 
9790 	bp->tx_push_thresh = 0;
9791 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9792 	    BNXT_FW_MAJ(bp) > 217)
9793 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9794 
9795 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9796 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9797 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9798 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9799 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9800 	if (!hw_resc->max_hw_ring_grps)
9801 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9802 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9803 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9804 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9805 
9806 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9807 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9808 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9809 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9810 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9811 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9812 
9813 	if (BNXT_PF(bp)) {
9814 		struct bnxt_pf_info *pf = &bp->pf;
9815 
9816 		pf->fw_fid = le16_to_cpu(resp->fid);
9817 		pf->port_id = le16_to_cpu(resp->port_id);
9818 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9819 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9820 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9821 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9822 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9823 			bp->flags |= BNXT_FLAG_WOL_CAP;
9824 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9825 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9826 		} else {
9827 			bnxt_ptp_clear(bp);
9828 			kfree(bp->ptp_cfg);
9829 			bp->ptp_cfg = NULL;
9830 		}
9831 	} else {
9832 #ifdef CONFIG_BNXT_SRIOV
9833 		struct bnxt_vf_info *vf = &bp->vf;
9834 
9835 		vf->fw_fid = le16_to_cpu(resp->fid);
9836 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9837 #endif
9838 	}
9839 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9840 
9841 hwrm_func_qcaps_exit:
9842 	hwrm_req_drop(bp, req);
9843 	return rc;
9844 }
9845 
9846 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9847 {
9848 	struct hwrm_dbg_qcaps_output *resp;
9849 	struct hwrm_dbg_qcaps_input *req;
9850 	int rc;
9851 
9852 	bp->fw_dbg_cap = 0;
9853 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9854 		return;
9855 
9856 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9857 	if (rc)
9858 		return;
9859 
9860 	req->fid = cpu_to_le16(0xffff);
9861 	resp = hwrm_req_hold(bp, req);
9862 	rc = hwrm_req_send(bp, req);
9863 	if (rc)
9864 		goto hwrm_dbg_qcaps_exit;
9865 
9866 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9867 
9868 hwrm_dbg_qcaps_exit:
9869 	hwrm_req_drop(bp, req);
9870 }
9871 
9872 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9873 
9874 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9875 {
9876 	int rc;
9877 
9878 	rc = __bnxt_hwrm_func_qcaps(bp);
9879 	if (rc)
9880 		return rc;
9881 
9882 	bnxt_hwrm_dbg_qcaps(bp);
9883 
9884 	rc = bnxt_hwrm_queue_qportcfg(bp);
9885 	if (rc) {
9886 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9887 		return rc;
9888 	}
9889 	if (bp->hwrm_spec_code >= 0x10803) {
9890 		rc = bnxt_alloc_ctx_mem(bp);
9891 		if (rc)
9892 			return rc;
9893 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9894 		if (!rc)
9895 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9896 	}
9897 	return 0;
9898 }
9899 
9900 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9901 {
9902 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9903 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9904 	u32 flags;
9905 	int rc;
9906 
9907 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9908 		return 0;
9909 
9910 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9911 	if (rc)
9912 		return rc;
9913 
9914 	resp = hwrm_req_hold(bp, req);
9915 	rc = hwrm_req_send(bp, req);
9916 	if (rc)
9917 		goto hwrm_cfa_adv_qcaps_exit;
9918 
9919 	flags = le32_to_cpu(resp->flags);
9920 	if (flags &
9921 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9922 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9923 
9924 	if (flags &
9925 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9926 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9927 
9928 	if (flags &
9929 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9930 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9931 
9932 hwrm_cfa_adv_qcaps_exit:
9933 	hwrm_req_drop(bp, req);
9934 	return rc;
9935 }
9936 
9937 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9938 {
9939 	if (bp->fw_health)
9940 		return 0;
9941 
9942 	bp->fw_health = kzalloc_obj(*bp->fw_health);
9943 	if (!bp->fw_health)
9944 		return -ENOMEM;
9945 
9946 	mutex_init(&bp->fw_health->lock);
9947 	return 0;
9948 }
9949 
9950 static int bnxt_alloc_fw_health(struct bnxt *bp)
9951 {
9952 	int rc;
9953 
9954 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9955 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9956 		return 0;
9957 
9958 	rc = __bnxt_alloc_fw_health(bp);
9959 	if (rc) {
9960 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9961 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9962 		return rc;
9963 	}
9964 
9965 	return 0;
9966 }
9967 
9968 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9969 {
9970 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9971 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9972 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9973 }
9974 
9975 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9976 {
9977 	struct bnxt_fw_health *fw_health = bp->fw_health;
9978 	u32 reg_type;
9979 
9980 	if (!fw_health)
9981 		return;
9982 
9983 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9984 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9985 		fw_health->status_reliable = false;
9986 
9987 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9988 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9989 		fw_health->resets_reliable = false;
9990 }
9991 
9992 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9993 {
9994 	void __iomem *hs;
9995 	u32 status_loc;
9996 	u32 reg_type;
9997 	u32 sig;
9998 
9999 	if (bp->fw_health)
10000 		bp->fw_health->status_reliable = false;
10001 
10002 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
10003 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
10004 
10005 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
10006 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
10007 		if (!bp->chip_num) {
10008 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
10009 			bp->chip_num = readl(bp->bar0 +
10010 					     BNXT_FW_HEALTH_WIN_BASE +
10011 					     BNXT_GRC_REG_CHIP_NUM);
10012 		}
10013 		if (!BNXT_CHIP_P5_PLUS(bp))
10014 			return;
10015 
10016 		status_loc = BNXT_GRC_REG_STATUS_P5 |
10017 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
10018 	} else {
10019 		status_loc = readl(hs + offsetof(struct hcomm_status,
10020 						 fw_status_loc));
10021 	}
10022 
10023 	if (__bnxt_alloc_fw_health(bp)) {
10024 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
10025 		return;
10026 	}
10027 
10028 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
10029 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
10030 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
10031 		__bnxt_map_fw_health_reg(bp, status_loc);
10032 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
10033 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
10034 	}
10035 
10036 	bp->fw_health->status_reliable = true;
10037 }
10038 
10039 static int bnxt_map_fw_health_regs(struct bnxt *bp)
10040 {
10041 	struct bnxt_fw_health *fw_health = bp->fw_health;
10042 	u32 reg_base = 0xffffffff;
10043 	int i;
10044 
10045 	bp->fw_health->status_reliable = false;
10046 	bp->fw_health->resets_reliable = false;
10047 	/* Only pre-map the monitoring GRC registers using window 3 */
10048 	for (i = 0; i < 4; i++) {
10049 		u32 reg = fw_health->regs[i];
10050 
10051 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
10052 			continue;
10053 		if (reg_base == 0xffffffff)
10054 			reg_base = reg & BNXT_GRC_BASE_MASK;
10055 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
10056 			return -ERANGE;
10057 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
10058 	}
10059 	bp->fw_health->status_reliable = true;
10060 	bp->fw_health->resets_reliable = true;
10061 	if (reg_base == 0xffffffff)
10062 		return 0;
10063 
10064 	__bnxt_map_fw_health_reg(bp, reg_base);
10065 	return 0;
10066 }
10067 
10068 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
10069 {
10070 	if (!bp->fw_health)
10071 		return;
10072 
10073 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
10074 		bp->fw_health->status_reliable = true;
10075 		bp->fw_health->resets_reliable = true;
10076 	} else {
10077 		bnxt_try_map_fw_health_reg(bp);
10078 	}
10079 }
10080 
10081 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
10082 {
10083 	struct bnxt_fw_health *fw_health = bp->fw_health;
10084 	struct hwrm_error_recovery_qcfg_output *resp;
10085 	struct hwrm_error_recovery_qcfg_input *req;
10086 	int rc, i;
10087 
10088 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
10089 		return 0;
10090 
10091 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
10092 	if (rc)
10093 		return rc;
10094 
10095 	resp = hwrm_req_hold(bp, req);
10096 	rc = hwrm_req_send(bp, req);
10097 	if (rc)
10098 		goto err_recovery_out;
10099 	fw_health->flags = le32_to_cpu(resp->flags);
10100 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
10101 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
10102 		rc = -EINVAL;
10103 		goto err_recovery_out;
10104 	}
10105 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
10106 	fw_health->master_func_wait_dsecs =
10107 		le32_to_cpu(resp->master_func_wait_period);
10108 	fw_health->normal_func_wait_dsecs =
10109 		le32_to_cpu(resp->normal_func_wait_period);
10110 	fw_health->post_reset_wait_dsecs =
10111 		le32_to_cpu(resp->master_func_wait_period_after_reset);
10112 	fw_health->post_reset_max_wait_dsecs =
10113 		le32_to_cpu(resp->max_bailout_time_after_reset);
10114 	fw_health->regs[BNXT_FW_HEALTH_REG] =
10115 		le32_to_cpu(resp->fw_health_status_reg);
10116 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
10117 		le32_to_cpu(resp->fw_heartbeat_reg);
10118 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
10119 		le32_to_cpu(resp->fw_reset_cnt_reg);
10120 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
10121 		le32_to_cpu(resp->reset_inprogress_reg);
10122 	fw_health->fw_reset_inprog_reg_mask =
10123 		le32_to_cpu(resp->reset_inprogress_reg_mask);
10124 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
10125 	if (fw_health->fw_reset_seq_cnt >= 16) {
10126 		rc = -EINVAL;
10127 		goto err_recovery_out;
10128 	}
10129 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
10130 		fw_health->fw_reset_seq_regs[i] =
10131 			le32_to_cpu(resp->reset_reg[i]);
10132 		fw_health->fw_reset_seq_vals[i] =
10133 			le32_to_cpu(resp->reset_reg_val[i]);
10134 		fw_health->fw_reset_seq_delay_msec[i] =
10135 			resp->delay_after_reset[i];
10136 	}
10137 err_recovery_out:
10138 	hwrm_req_drop(bp, req);
10139 	if (!rc)
10140 		rc = bnxt_map_fw_health_regs(bp);
10141 	if (rc)
10142 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10143 	return rc;
10144 }
10145 
10146 static int bnxt_hwrm_func_reset(struct bnxt *bp)
10147 {
10148 	struct hwrm_func_reset_input *req;
10149 	int rc;
10150 
10151 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
10152 	if (rc)
10153 		return rc;
10154 
10155 	req->enables = 0;
10156 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
10157 	return hwrm_req_send(bp, req);
10158 }
10159 
10160 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
10161 {
10162 	struct hwrm_nvm_get_dev_info_output nvm_info;
10163 
10164 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
10165 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
10166 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
10167 			 nvm_info.nvm_cfg_ver_upd);
10168 }
10169 
10170 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
10171 {
10172 	struct hwrm_queue_qportcfg_output *resp;
10173 	struct hwrm_queue_qportcfg_input *req;
10174 	u8 i, j, *qptr;
10175 	bool no_rdma;
10176 	int rc = 0;
10177 
10178 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
10179 	if (rc)
10180 		return rc;
10181 
10182 	resp = hwrm_req_hold(bp, req);
10183 	rc = hwrm_req_send(bp, req);
10184 	if (rc)
10185 		goto qportcfg_exit;
10186 
10187 	if (!resp->max_configurable_queues) {
10188 		rc = -EINVAL;
10189 		goto qportcfg_exit;
10190 	}
10191 	bp->max_tc = resp->max_configurable_queues;
10192 	bp->max_lltc = resp->max_configurable_lossless_queues;
10193 	if (bp->max_tc > BNXT_MAX_QUEUE)
10194 		bp->max_tc = BNXT_MAX_QUEUE;
10195 
10196 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
10197 	qptr = &resp->queue_id0;
10198 	for (i = 0, j = 0; i < bp->max_tc; i++) {
10199 		bp->q_info[j].queue_id = *qptr;
10200 		bp->q_ids[i] = *qptr++;
10201 		bp->q_info[j].queue_profile = *qptr++;
10202 		bp->tc_to_qidx[j] = j;
10203 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
10204 		    (no_rdma && BNXT_PF(bp)))
10205 			j++;
10206 	}
10207 	bp->max_q = bp->max_tc;
10208 	bp->max_tc = max_t(u8, j, 1);
10209 
10210 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
10211 		bp->max_tc = 1;
10212 
10213 	if (bp->max_lltc > bp->max_tc)
10214 		bp->max_lltc = bp->max_tc;
10215 
10216 qportcfg_exit:
10217 	hwrm_req_drop(bp, req);
10218 	return rc;
10219 }
10220 
10221 static int bnxt_hwrm_poll(struct bnxt *bp)
10222 {
10223 	struct hwrm_ver_get_input *req;
10224 	int rc;
10225 
10226 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10227 	if (rc)
10228 		return rc;
10229 
10230 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10231 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10232 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10233 
10234 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
10235 	rc = hwrm_req_send(bp, req);
10236 	return rc;
10237 }
10238 
10239 static int bnxt_hwrm_ver_get(struct bnxt *bp)
10240 {
10241 	struct hwrm_ver_get_output *resp;
10242 	struct hwrm_ver_get_input *req;
10243 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
10244 	u32 dev_caps_cfg, hwrm_ver;
10245 	int rc, len, max_tmo_secs;
10246 
10247 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10248 	if (rc)
10249 		return rc;
10250 
10251 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10252 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
10253 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10254 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10255 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10256 
10257 	resp = hwrm_req_hold(bp, req);
10258 	rc = hwrm_req_send(bp, req);
10259 	if (rc)
10260 		goto hwrm_ver_get_exit;
10261 
10262 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
10263 
10264 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
10265 			     resp->hwrm_intf_min_8b << 8 |
10266 			     resp->hwrm_intf_upd_8b;
10267 	if (resp->hwrm_intf_maj_8b < 1) {
10268 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
10269 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10270 			    resp->hwrm_intf_upd_8b);
10271 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10272 	}
10273 
10274 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10275 			HWRM_VERSION_UPDATE;
10276 
10277 	if (bp->hwrm_spec_code > hwrm_ver)
10278 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10279 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10280 			 HWRM_VERSION_UPDATE);
10281 	else
10282 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10283 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10284 			 resp->hwrm_intf_upd_8b);
10285 
10286 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10287 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10288 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10289 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10290 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10291 		len = FW_VER_STR_LEN;
10292 	} else {
10293 		fw_maj = resp->hwrm_fw_maj_8b;
10294 		fw_min = resp->hwrm_fw_min_8b;
10295 		fw_bld = resp->hwrm_fw_bld_8b;
10296 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10297 		len = BC_HWRM_STR_LEN;
10298 	}
10299 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10300 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10301 		 fw_rsv);
10302 
10303 	if (strlen(resp->active_pkg_name)) {
10304 		int fw_ver_len = strlen(bp->fw_ver_str);
10305 
10306 		snprintf(bp->fw_ver_str + fw_ver_len,
10307 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10308 			 resp->active_pkg_name);
10309 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10310 	}
10311 
10312 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10313 	if (!bp->hwrm_cmd_timeout)
10314 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10315 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10316 	if (!bp->hwrm_cmd_max_timeout)
10317 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10318 	max_tmo_secs = bp->hwrm_cmd_max_timeout / 1000;
10319 #ifdef CONFIG_DETECT_HUNG_TASK
10320 	if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT ||
10321 	    max_tmo_secs > CONFIG_DEFAULT_HUNG_TASK_TIMEOUT) {
10322 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog (kernel default %ds)\n",
10323 			    max_tmo_secs, CONFIG_DEFAULT_HUNG_TASK_TIMEOUT);
10324 	}
10325 #endif
10326 
10327 	if (resp->hwrm_intf_maj_8b >= 1) {
10328 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10329 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10330 	}
10331 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10332 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10333 
10334 	bp->chip_num = le16_to_cpu(resp->chip_num);
10335 	bp->chip_rev = resp->chip_rev;
10336 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10337 	    !resp->chip_metal)
10338 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10339 
10340 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10341 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10342 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10343 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10344 
10345 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10346 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10347 
10348 	if (dev_caps_cfg &
10349 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10350 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10351 
10352 	if (dev_caps_cfg &
10353 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10354 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10355 
10356 	if (dev_caps_cfg &
10357 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10358 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10359 
10360 hwrm_ver_get_exit:
10361 	hwrm_req_drop(bp, req);
10362 	return rc;
10363 }
10364 
10365 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10366 {
10367 	struct hwrm_fw_set_time_input *req;
10368 	struct tm tm;
10369 	time64_t now = ktime_get_real_seconds();
10370 	int rc;
10371 
10372 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10373 	    bp->hwrm_spec_code < 0x10400)
10374 		return -EOPNOTSUPP;
10375 
10376 	time64_to_tm(now, 0, &tm);
10377 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10378 	if (rc)
10379 		return rc;
10380 
10381 	req->year = cpu_to_le16(1900 + tm.tm_year);
10382 	req->month = 1 + tm.tm_mon;
10383 	req->day = tm.tm_mday;
10384 	req->hour = tm.tm_hour;
10385 	req->minute = tm.tm_min;
10386 	req->second = tm.tm_sec;
10387 	return hwrm_req_send(bp, req);
10388 }
10389 
10390 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10391 {
10392 	u64 sw_tmp;
10393 
10394 	hw &= mask;
10395 	sw_tmp = (*sw & ~mask) | hw;
10396 	if (hw < (*sw & mask))
10397 		sw_tmp += mask + 1;
10398 	WRITE_ONCE(*sw, sw_tmp);
10399 }
10400 
10401 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10402 				    int count, bool ignore_zero)
10403 {
10404 	int i;
10405 
10406 	for (i = 0; i < count; i++) {
10407 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10408 
10409 		if (ignore_zero && !hw)
10410 			continue;
10411 
10412 		if (masks[i] == -1ULL)
10413 			sw_stats[i] = hw;
10414 		else
10415 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10416 	}
10417 }
10418 
10419 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10420 {
10421 	if (!stats->hw_stats)
10422 		return;
10423 
10424 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10425 				stats->hw_masks, stats->len / 8, false);
10426 }
10427 
10428 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10429 {
10430 	struct bnxt_stats_mem *ring0_stats;
10431 	bool ignore_zero = false;
10432 	int i;
10433 
10434 	/* Chip bug.  Counter intermittently becomes 0. */
10435 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10436 		ignore_zero = true;
10437 
10438 	for (i = 0; i < bp->cp_nr_rings; i++) {
10439 		struct bnxt_napi *bnapi = bp->bnapi[i];
10440 		struct bnxt_cp_ring_info *cpr;
10441 		struct bnxt_stats_mem *stats;
10442 
10443 		cpr = &bnapi->cp_ring;
10444 		stats = &cpr->stats;
10445 		if (!i)
10446 			ring0_stats = stats;
10447 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10448 					ring0_stats->hw_masks,
10449 					ring0_stats->len / 8, ignore_zero);
10450 	}
10451 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10452 		struct bnxt_stats_mem *stats = &bp->port_stats;
10453 		__le64 *hw_stats = stats->hw_stats;
10454 		u64 *sw_stats = stats->sw_stats;
10455 		u64 *masks = stats->hw_masks;
10456 		int cnt;
10457 
10458 		cnt = sizeof(struct rx_port_stats) / 8;
10459 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10460 
10461 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10462 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10463 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10464 		cnt = sizeof(struct tx_port_stats) / 8;
10465 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10466 	}
10467 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10468 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10469 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10470 	}
10471 }
10472 
10473 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10474 {
10475 	struct hwrm_port_qstats_input *req;
10476 	struct bnxt_pf_info *pf = &bp->pf;
10477 	int rc;
10478 
10479 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10480 		return 0;
10481 
10482 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10483 		return -EOPNOTSUPP;
10484 
10485 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10486 	if (rc)
10487 		return rc;
10488 
10489 	req->flags = flags;
10490 	req->port_id = cpu_to_le16(pf->port_id);
10491 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10492 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10493 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10494 	return hwrm_req_send(bp, req);
10495 }
10496 
10497 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10498 {
10499 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10500 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10501 	struct hwrm_port_qstats_ext_output *resp_qs;
10502 	struct hwrm_port_qstats_ext_input *req_qs;
10503 	struct bnxt_pf_info *pf = &bp->pf;
10504 	u32 tx_stat_size;
10505 	int rc;
10506 
10507 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10508 		return 0;
10509 
10510 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10511 		return -EOPNOTSUPP;
10512 
10513 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10514 	if (rc)
10515 		return rc;
10516 
10517 	req_qs->flags = flags;
10518 	req_qs->port_id = cpu_to_le16(pf->port_id);
10519 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10520 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10521 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10522 		       sizeof(struct tx_port_stats_ext) : 0;
10523 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10524 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10525 	resp_qs = hwrm_req_hold(bp, req_qs);
10526 	rc = hwrm_req_send(bp, req_qs);
10527 	if (!rc) {
10528 		bp->fw_rx_stats_ext_size =
10529 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10530 		if (BNXT_FW_MAJ(bp) < 220 &&
10531 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10532 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10533 
10534 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10535 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10536 	} else {
10537 		bp->fw_rx_stats_ext_size = 0;
10538 		bp->fw_tx_stats_ext_size = 0;
10539 	}
10540 	hwrm_req_drop(bp, req_qs);
10541 
10542 	if (flags)
10543 		return rc;
10544 
10545 	if (bp->fw_tx_stats_ext_size <=
10546 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10547 		bp->pri2cos_valid = 0;
10548 		return rc;
10549 	}
10550 
10551 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10552 	if (rc)
10553 		return rc;
10554 
10555 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10556 
10557 	resp_qc = hwrm_req_hold(bp, req_qc);
10558 	rc = hwrm_req_send(bp, req_qc);
10559 	if (!rc) {
10560 		u8 *pri2cos;
10561 		int i, j;
10562 
10563 		pri2cos = &resp_qc->pri0_cos_queue_id;
10564 		for (i = 0; i < 8; i++) {
10565 			u8 queue_id = pri2cos[i];
10566 			u8 queue_idx;
10567 
10568 			/* Per port queue IDs start from 0, 10, 20, etc */
10569 			queue_idx = queue_id % 10;
10570 			if (queue_idx > BNXT_MAX_QUEUE) {
10571 				bp->pri2cos_valid = false;
10572 				hwrm_req_drop(bp, req_qc);
10573 				return rc;
10574 			}
10575 			for (j = 0; j < bp->max_q; j++) {
10576 				if (bp->q_ids[j] == queue_id)
10577 					bp->pri2cos_idx[i] = queue_idx;
10578 			}
10579 		}
10580 		bp->pri2cos_valid = true;
10581 	}
10582 	hwrm_req_drop(bp, req_qc);
10583 
10584 	return rc;
10585 }
10586 
10587 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10588 {
10589 	bnxt_hwrm_tunnel_dst_port_free(bp,
10590 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10591 	bnxt_hwrm_tunnel_dst_port_free(bp,
10592 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10593 }
10594 
10595 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10596 {
10597 	int rc, i;
10598 	u32 tpa_flags = 0;
10599 
10600 	if (set_tpa)
10601 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10602 	else if (BNXT_NO_FW_ACCESS(bp))
10603 		return 0;
10604 	for (i = 0; i < bp->nr_vnics; i++) {
10605 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10606 		if (rc) {
10607 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10608 				   i, rc);
10609 			return rc;
10610 		}
10611 	}
10612 	return 0;
10613 }
10614 
10615 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10616 {
10617 	int i;
10618 
10619 	for (i = 0; i < bp->nr_vnics; i++)
10620 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10621 }
10622 
10623 static void bnxt_clear_vnic(struct bnxt *bp)
10624 {
10625 	if (!bp->vnic_info)
10626 		return;
10627 
10628 	bnxt_hwrm_clear_vnic_filter(bp);
10629 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10630 		/* clear all RSS setting before free vnic ctx */
10631 		bnxt_hwrm_clear_vnic_rss(bp);
10632 		bnxt_hwrm_vnic_ctx_free(bp);
10633 	}
10634 	/* before free the vnic, undo the vnic tpa settings */
10635 	if (bp->flags & BNXT_FLAG_TPA)
10636 		bnxt_set_tpa(bp, false);
10637 	bnxt_hwrm_vnic_free(bp);
10638 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10639 		bnxt_hwrm_vnic_ctx_free(bp);
10640 }
10641 
10642 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10643 				    bool irq_re_init)
10644 {
10645 	bnxt_clear_vnic(bp);
10646 	bnxt_hwrm_ring_free(bp, close_path);
10647 	bnxt_hwrm_ring_grp_free(bp);
10648 	if (irq_re_init) {
10649 		bnxt_hwrm_stat_ctx_free(bp);
10650 		bnxt_hwrm_free_tunnel_ports(bp);
10651 	}
10652 }
10653 
10654 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10655 {
10656 	struct hwrm_func_cfg_input *req;
10657 	u8 evb_mode;
10658 	int rc;
10659 
10660 	if (br_mode == BRIDGE_MODE_VEB)
10661 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10662 	else if (br_mode == BRIDGE_MODE_VEPA)
10663 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10664 	else
10665 		return -EINVAL;
10666 
10667 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10668 	if (rc)
10669 		return rc;
10670 
10671 	req->fid = cpu_to_le16(0xffff);
10672 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10673 	req->evb_mode = evb_mode;
10674 	return hwrm_req_send(bp, req);
10675 }
10676 
10677 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10678 {
10679 	struct hwrm_func_cfg_input *req;
10680 	int rc;
10681 
10682 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10683 		return 0;
10684 
10685 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10686 	if (rc)
10687 		return rc;
10688 
10689 	req->fid = cpu_to_le16(0xffff);
10690 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10691 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10692 	if (size == 128)
10693 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10694 
10695 	return hwrm_req_send(bp, req);
10696 }
10697 
10698 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10699 {
10700 	int rc;
10701 
10702 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10703 		goto skip_rss_ctx;
10704 
10705 	/* allocate context for vnic */
10706 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10707 	if (rc) {
10708 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10709 			   vnic->vnic_id, rc);
10710 		goto vnic_setup_err;
10711 	}
10712 	bp->rsscos_nr_ctxs++;
10713 
10714 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10715 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10716 		if (rc) {
10717 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10718 				   vnic->vnic_id, rc);
10719 			goto vnic_setup_err;
10720 		}
10721 		bp->rsscos_nr_ctxs++;
10722 	}
10723 
10724 skip_rss_ctx:
10725 	/* configure default vnic, ring grp */
10726 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10727 	if (rc) {
10728 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10729 			   vnic->vnic_id, rc);
10730 		goto vnic_setup_err;
10731 	}
10732 
10733 	/* Enable RSS hashing on vnic */
10734 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10735 	if (rc) {
10736 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10737 			   vnic->vnic_id, rc);
10738 		goto vnic_setup_err;
10739 	}
10740 
10741 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10742 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10743 		if (rc) {
10744 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10745 				   vnic->vnic_id, rc);
10746 		}
10747 	}
10748 
10749 vnic_setup_err:
10750 	return rc;
10751 }
10752 
10753 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10754 			  u8 valid)
10755 {
10756 	struct hwrm_vnic_update_input *req;
10757 	int rc;
10758 
10759 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10760 	if (rc)
10761 		return rc;
10762 
10763 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10764 
10765 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10766 		req->mru = cpu_to_le16(vnic->mru);
10767 
10768 	req->enables = cpu_to_le32(valid);
10769 
10770 	return hwrm_req_send(bp, req);
10771 }
10772 
10773 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10774 {
10775 	int rc;
10776 
10777 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10778 	if (rc) {
10779 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10780 			   vnic->vnic_id, rc);
10781 		return rc;
10782 	}
10783 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10784 	if (rc)
10785 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10786 			   vnic->vnic_id, rc);
10787 	return rc;
10788 }
10789 
10790 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10791 {
10792 	int rc, i, nr_ctxs;
10793 
10794 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10795 	for (i = 0; i < nr_ctxs; i++) {
10796 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10797 		if (rc) {
10798 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10799 				   vnic->vnic_id, i, rc);
10800 			break;
10801 		}
10802 		bp->rsscos_nr_ctxs++;
10803 	}
10804 	if (i < nr_ctxs)
10805 		return -ENOMEM;
10806 
10807 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10808 	if (rc)
10809 		return rc;
10810 
10811 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10812 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10813 		if (rc) {
10814 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10815 				   vnic->vnic_id, rc);
10816 		}
10817 	}
10818 	return rc;
10819 }
10820 
10821 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10822 {
10823 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10824 		return __bnxt_setup_vnic_p5(bp, vnic);
10825 	else
10826 		return __bnxt_setup_vnic(bp, vnic);
10827 }
10828 
10829 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10830 				     struct bnxt_vnic_info *vnic,
10831 				     u16 start_rx_ring_idx, int rx_rings)
10832 {
10833 	int rc;
10834 
10835 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10836 	if (rc) {
10837 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10838 			   vnic->vnic_id, rc);
10839 		return rc;
10840 	}
10841 	return bnxt_setup_vnic(bp, vnic);
10842 }
10843 
10844 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10845 {
10846 	struct bnxt_vnic_info *vnic;
10847 	int i, rc = 0;
10848 
10849 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10850 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10851 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10852 	}
10853 
10854 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10855 		return 0;
10856 
10857 	for (i = 0; i < bp->rx_nr_rings; i++) {
10858 		u16 vnic_id = i + 1;
10859 		u16 ring_id = i;
10860 
10861 		if (vnic_id >= bp->nr_vnics)
10862 			break;
10863 
10864 		vnic = &bp->vnic_info[vnic_id];
10865 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10866 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10867 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10868 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10869 			break;
10870 	}
10871 	return rc;
10872 }
10873 
10874 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10875 			  bool all)
10876 {
10877 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10878 	struct bnxt_filter_base *usr_fltr, *tmp;
10879 	struct bnxt_ntuple_filter *ntp_fltr;
10880 	int i;
10881 
10882 	if (netif_running(bp->dev)) {
10883 		bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10884 		for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10885 			if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10886 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10887 		}
10888 	}
10889 	if (!all)
10890 		return;
10891 
10892 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10893 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10894 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10895 			ntp_fltr = container_of(usr_fltr,
10896 						struct bnxt_ntuple_filter,
10897 						base);
10898 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10899 			bnxt_del_ntp_filter(bp, ntp_fltr);
10900 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10901 		}
10902 	}
10903 
10904 	if (vnic->rss_table)
10905 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10906 				  vnic->rss_table,
10907 				  vnic->rss_table_dma_addr);
10908 	bp->num_rss_ctx--;
10909 }
10910 
10911 static bool bnxt_vnic_has_rx_ring(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10912 				  int rxr_id)
10913 {
10914 	u16 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
10915 	int i, vnic_rx;
10916 
10917 	/* Ntuple VNIC always has all the rx rings. Any change of ring id
10918 	 * must be updated because a future filter may use it.
10919 	 */
10920 	if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
10921 		return true;
10922 
10923 	for (i = 0; i < tbl_size; i++) {
10924 		if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
10925 			vnic_rx = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
10926 		else
10927 			vnic_rx = bp->rss_indir_tbl[i];
10928 
10929 		if (rxr_id == vnic_rx)
10930 			return true;
10931 	}
10932 
10933 	return false;
10934 }
10935 
10936 static int bnxt_set_vnic_mru_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10937 				u16 mru, int rxr_id)
10938 {
10939 	int rc;
10940 
10941 	if (!bnxt_vnic_has_rx_ring(bp, vnic, rxr_id))
10942 		return 0;
10943 
10944 	if (mru) {
10945 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10946 		if (rc) {
10947 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10948 				   vnic->vnic_id, rc);
10949 			return rc;
10950 		}
10951 	}
10952 	vnic->mru = mru;
10953 	bnxt_hwrm_vnic_update(bp, vnic,
10954 			      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
10955 
10956 	return 0;
10957 }
10958 
10959 static int bnxt_set_rss_ctx_vnic_mru(struct bnxt *bp, u16 mru, int rxr_id)
10960 {
10961 	struct ethtool_rxfh_context *ctx;
10962 	unsigned long context;
10963 	int rc;
10964 
10965 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10966 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10967 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10968 
10969 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, rxr_id);
10970 		if (rc)
10971 			return rc;
10972 	}
10973 
10974 	return 0;
10975 }
10976 
10977 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10978 {
10979 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10980 	struct ethtool_rxfh_context *ctx;
10981 	unsigned long context;
10982 
10983 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10984 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10985 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10986 
10987 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10988 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10989 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10990 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10991 				   rss_ctx->index);
10992 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10993 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
10994 		}
10995 	}
10996 }
10997 
10998 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
10999 {
11000 	struct ethtool_rxfh_context *ctx;
11001 	unsigned long context;
11002 
11003 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11004 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11005 
11006 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
11007 	}
11008 }
11009 
11010 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
11011 static bool bnxt_promisc_ok(struct bnxt *bp)
11012 {
11013 #ifdef CONFIG_BNXT_SRIOV
11014 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
11015 		return false;
11016 #endif
11017 	return true;
11018 }
11019 
11020 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
11021 {
11022 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
11023 	unsigned int rc = 0;
11024 
11025 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
11026 	if (rc) {
11027 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
11028 			   rc);
11029 		return rc;
11030 	}
11031 
11032 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
11033 	if (rc) {
11034 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
11035 			   rc);
11036 		return rc;
11037 	}
11038 	return rc;
11039 }
11040 
11041 static int bnxt_cfg_rx_mode(struct bnxt *);
11042 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
11043 
11044 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
11045 {
11046 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
11047 	int rc = 0;
11048 	unsigned int rx_nr_rings = bp->rx_nr_rings;
11049 
11050 	if (irq_re_init) {
11051 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
11052 		if (rc) {
11053 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
11054 				   rc);
11055 			goto err_out;
11056 		}
11057 	}
11058 
11059 	rc = bnxt_hwrm_ring_alloc(bp);
11060 	if (rc) {
11061 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
11062 		goto err_out;
11063 	}
11064 
11065 	rc = bnxt_hwrm_ring_grp_alloc(bp);
11066 	if (rc) {
11067 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
11068 		goto err_out;
11069 	}
11070 
11071 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11072 		rx_nr_rings--;
11073 
11074 	/* default vnic 0 */
11075 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
11076 	if (rc) {
11077 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
11078 		goto err_out;
11079 	}
11080 
11081 	if (BNXT_VF(bp))
11082 		bnxt_hwrm_func_qcfg(bp);
11083 
11084 	rc = bnxt_setup_vnic(bp, vnic);
11085 	if (rc)
11086 		goto err_out;
11087 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
11088 		bnxt_hwrm_update_rss_hash_cfg(bp);
11089 
11090 	if (bp->flags & BNXT_FLAG_RFS) {
11091 		rc = bnxt_alloc_rfs_vnics(bp);
11092 		if (rc)
11093 			goto err_out;
11094 	}
11095 
11096 	if (bp->flags & BNXT_FLAG_TPA) {
11097 		rc = bnxt_set_tpa(bp, true);
11098 		if (rc)
11099 			goto err_out;
11100 	}
11101 
11102 	if (BNXT_VF(bp))
11103 		bnxt_update_vf_mac(bp);
11104 
11105 	/* Filter for default vnic 0 */
11106 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
11107 	if (rc) {
11108 		if (BNXT_VF(bp) && rc == -ENODEV)
11109 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
11110 		else
11111 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
11112 		goto err_out;
11113 	}
11114 	vnic->uc_filter_count = 1;
11115 
11116 	vnic->rx_mask = 0;
11117 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
11118 		goto skip_rx_mask;
11119 
11120 	if (bp->dev->flags & IFF_BROADCAST)
11121 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
11122 
11123 	if (bp->dev->flags & IFF_PROMISC)
11124 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11125 
11126 	if (bp->dev->flags & IFF_ALLMULTI) {
11127 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
11128 		vnic->mc_list_count = 0;
11129 	} else if (bp->dev->flags & IFF_MULTICAST) {
11130 		u32 mask = 0;
11131 
11132 		bnxt_mc_list_updated(bp, &mask);
11133 		vnic->rx_mask |= mask;
11134 	}
11135 
11136 	rc = bnxt_cfg_rx_mode(bp);
11137 	if (rc)
11138 		goto err_out;
11139 
11140 skip_rx_mask:
11141 	rc = bnxt_hwrm_set_coal(bp);
11142 	if (rc)
11143 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
11144 				rc);
11145 
11146 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11147 		rc = bnxt_setup_nitroa0_vnic(bp);
11148 		if (rc)
11149 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
11150 				   rc);
11151 	}
11152 
11153 	if (BNXT_VF(bp)) {
11154 		bnxt_hwrm_func_qcfg(bp);
11155 		netdev_update_features(bp->dev);
11156 	}
11157 
11158 	return 0;
11159 
11160 err_out:
11161 	bnxt_hwrm_resource_free(bp, 0, true);
11162 
11163 	return rc;
11164 }
11165 
11166 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
11167 {
11168 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
11169 	return 0;
11170 }
11171 
11172 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
11173 {
11174 	bnxt_init_cp_rings(bp);
11175 	bnxt_init_rx_rings(bp);
11176 	bnxt_init_tx_rings(bp);
11177 	bnxt_init_ring_grps(bp, irq_re_init);
11178 	bnxt_init_vnics(bp);
11179 
11180 	return bnxt_init_chip(bp, irq_re_init);
11181 }
11182 
11183 static int bnxt_set_real_num_queues(struct bnxt *bp)
11184 {
11185 	int rc;
11186 	struct net_device *dev = bp->dev;
11187 
11188 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
11189 					  bp->tx_nr_rings_xdp);
11190 	if (rc)
11191 		return rc;
11192 
11193 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
11194 	if (rc)
11195 		return rc;
11196 
11197 #ifdef CONFIG_RFS_ACCEL
11198 	if (bp->flags & BNXT_FLAG_RFS)
11199 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
11200 #endif
11201 
11202 	return rc;
11203 }
11204 
11205 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11206 			     bool shared)
11207 {
11208 	int _rx = *rx, _tx = *tx;
11209 
11210 	if (shared) {
11211 		*rx = min_t(int, _rx, max);
11212 		*tx = min_t(int, _tx, max);
11213 	} else {
11214 		if (max < 2)
11215 			return -ENOMEM;
11216 
11217 		while (_rx + _tx > max) {
11218 			if (_rx > _tx && _rx > 1)
11219 				_rx--;
11220 			else if (_tx > 1)
11221 				_tx--;
11222 		}
11223 		*rx = _rx;
11224 		*tx = _tx;
11225 	}
11226 	return 0;
11227 }
11228 
11229 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
11230 {
11231 	return (tx - tx_xdp) / tx_sets + tx_xdp;
11232 }
11233 
11234 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
11235 {
11236 	int tcs = bp->num_tc;
11237 
11238 	if (!tcs)
11239 		tcs = 1;
11240 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
11241 }
11242 
11243 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
11244 {
11245 	int tcs = bp->num_tc;
11246 
11247 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
11248 	       bp->tx_nr_rings_xdp;
11249 }
11250 
11251 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11252 			   bool sh)
11253 {
11254 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
11255 
11256 	if (tx_cp != *tx) {
11257 		int tx_saved = tx_cp, rc;
11258 
11259 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
11260 		if (rc)
11261 			return rc;
11262 		if (tx_cp != tx_saved)
11263 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
11264 		return 0;
11265 	}
11266 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
11267 }
11268 
11269 static void bnxt_setup_msix(struct bnxt *bp)
11270 {
11271 	const int len = sizeof(bp->irq_tbl[0].name);
11272 	struct net_device *dev = bp->dev;
11273 	int tcs, i;
11274 
11275 	tcs = bp->num_tc;
11276 	if (tcs) {
11277 		int i, off, count;
11278 
11279 		for (i = 0; i < tcs; i++) {
11280 			count = bp->tx_nr_rings_per_tc;
11281 			off = BNXT_TC_TO_RING_BASE(bp, i);
11282 			netdev_set_tc_queue(dev, i, count, off);
11283 		}
11284 	}
11285 
11286 	for (i = 0; i < bp->cp_nr_rings; i++) {
11287 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11288 		char *attr;
11289 
11290 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11291 			attr = "TxRx";
11292 		else if (i < bp->rx_nr_rings)
11293 			attr = "rx";
11294 		else
11295 			attr = "tx";
11296 
11297 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
11298 			 attr, i);
11299 		bp->irq_tbl[map_idx].handler = bnxt_msix;
11300 	}
11301 }
11302 
11303 static int bnxt_init_int_mode(struct bnxt *bp);
11304 
11305 static int bnxt_change_msix(struct bnxt *bp, int total)
11306 {
11307 	struct msi_map map;
11308 	int i;
11309 
11310 	/* add MSIX to the end if needed */
11311 	for (i = bp->total_irqs; i < total; i++) {
11312 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
11313 		if (map.index < 0)
11314 			return bp->total_irqs;
11315 		bp->irq_tbl[i].vector = map.virq;
11316 		bp->total_irqs++;
11317 	}
11318 
11319 	/* trim MSIX from the end if needed */
11320 	for (i = bp->total_irqs; i > total; i--) {
11321 		map.index = i - 1;
11322 		map.virq = bp->irq_tbl[i - 1].vector;
11323 		pci_msix_free_irq(bp->pdev, map);
11324 		bp->total_irqs--;
11325 	}
11326 	return bp->total_irqs;
11327 }
11328 
11329 static int bnxt_setup_int_mode(struct bnxt *bp)
11330 {
11331 	int rc;
11332 
11333 	if (!bp->irq_tbl) {
11334 		rc = bnxt_init_int_mode(bp);
11335 		if (rc || !bp->irq_tbl)
11336 			return rc ?: -ENODEV;
11337 	}
11338 
11339 	bnxt_setup_msix(bp);
11340 
11341 	rc = bnxt_set_real_num_queues(bp);
11342 	return rc;
11343 }
11344 
11345 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
11346 {
11347 	return bp->hw_resc.max_rsscos_ctxs;
11348 }
11349 
11350 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
11351 {
11352 	return bp->hw_resc.max_vnics;
11353 }
11354 
11355 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11356 {
11357 	return bp->hw_resc.max_stat_ctxs;
11358 }
11359 
11360 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11361 {
11362 	return bp->hw_resc.max_cp_rings;
11363 }
11364 
11365 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11366 {
11367 	unsigned int cp = bp->hw_resc.max_cp_rings;
11368 
11369 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11370 		cp -= bnxt_get_ulp_msix_num(bp);
11371 
11372 	return cp;
11373 }
11374 
11375 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11376 {
11377 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11378 
11379 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11380 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11381 
11382 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11383 }
11384 
11385 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11386 {
11387 	bp->hw_resc.max_irqs = max_irqs;
11388 }
11389 
11390 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11391 {
11392 	unsigned int cp;
11393 
11394 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11395 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11396 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11397 	else
11398 		return cp - bp->cp_nr_rings;
11399 }
11400 
11401 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11402 {
11403 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11404 }
11405 
11406 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11407 {
11408 	int max_irq = bnxt_get_max_func_irqs(bp);
11409 	int total_req = bp->cp_nr_rings + num;
11410 
11411 	if (max_irq < total_req) {
11412 		num = max_irq - bp->cp_nr_rings;
11413 		if (num <= 0)
11414 			return 0;
11415 	}
11416 	return num;
11417 }
11418 
11419 static int bnxt_get_num_msix(struct bnxt *bp)
11420 {
11421 	if (!BNXT_NEW_RM(bp))
11422 		return bnxt_get_max_func_irqs(bp);
11423 
11424 	return bnxt_nq_rings_in_use(bp);
11425 }
11426 
11427 static int bnxt_init_int_mode(struct bnxt *bp)
11428 {
11429 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
11430 
11431 	total_vecs = bnxt_get_num_msix(bp);
11432 	max = bnxt_get_max_func_irqs(bp);
11433 	if (total_vecs > max)
11434 		total_vecs = max;
11435 
11436 	if (!total_vecs)
11437 		return 0;
11438 
11439 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11440 		min = 2;
11441 
11442 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11443 					   PCI_IRQ_MSIX);
11444 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11445 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11446 		rc = -ENODEV;
11447 		goto msix_setup_exit;
11448 	}
11449 
11450 	tbl_size = total_vecs;
11451 	if (pci_msix_can_alloc_dyn(bp->pdev))
11452 		tbl_size = max;
11453 	bp->irq_tbl = kzalloc_objs(*bp->irq_tbl, tbl_size);
11454 	if (bp->irq_tbl) {
11455 		for (i = 0; i < total_vecs; i++)
11456 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11457 
11458 		bp->total_irqs = total_vecs;
11459 		/* Trim rings based upon num of vectors allocated */
11460 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11461 				     total_vecs - ulp_msix, min == 1);
11462 		if (rc)
11463 			goto msix_setup_exit;
11464 
11465 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11466 		bp->cp_nr_rings = (min == 1) ?
11467 				  max_t(int, tx_cp, bp->rx_nr_rings) :
11468 				  tx_cp + bp->rx_nr_rings;
11469 
11470 	} else {
11471 		rc = -ENOMEM;
11472 		goto msix_setup_exit;
11473 	}
11474 	return 0;
11475 
11476 msix_setup_exit:
11477 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11478 	kfree(bp->irq_tbl);
11479 	bp->irq_tbl = NULL;
11480 	pci_free_irq_vectors(bp->pdev);
11481 	return rc;
11482 }
11483 
11484 static void bnxt_clear_int_mode(struct bnxt *bp)
11485 {
11486 	pci_free_irq_vectors(bp->pdev);
11487 
11488 	kfree(bp->irq_tbl);
11489 	bp->irq_tbl = NULL;
11490 }
11491 
11492 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11493 {
11494 	bool irq_cleared = false;
11495 	bool irq_change = false;
11496 	int tcs = bp->num_tc;
11497 	int irqs_required;
11498 	int rc;
11499 
11500 	if (!bnxt_need_reserve_rings(bp))
11501 		return 0;
11502 
11503 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
11504 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11505 
11506 		if (ulp_msix > bp->ulp_num_msix_want)
11507 			ulp_msix = bp->ulp_num_msix_want;
11508 		irqs_required = ulp_msix + bp->cp_nr_rings;
11509 	} else {
11510 		irqs_required = bnxt_get_num_msix(bp);
11511 	}
11512 
11513 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11514 		irq_change = true;
11515 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11516 			bnxt_ulp_irq_stop(bp);
11517 			bnxt_clear_int_mode(bp);
11518 			irq_cleared = true;
11519 		}
11520 	}
11521 	rc = __bnxt_reserve_rings(bp);
11522 	if (irq_cleared) {
11523 		if (!rc)
11524 			rc = bnxt_init_int_mode(bp);
11525 		bnxt_ulp_irq_restart(bp, rc);
11526 	} else if (irq_change && !rc) {
11527 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11528 			rc = -ENOSPC;
11529 	}
11530 	if (rc) {
11531 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11532 		return rc;
11533 	}
11534 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11535 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11536 		netdev_err(bp->dev, "tx ring reservation failure\n");
11537 		netdev_reset_tc(bp->dev);
11538 		bp->num_tc = 0;
11539 		if (bp->tx_nr_rings_xdp)
11540 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11541 		else
11542 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11543 		return -ENOMEM;
11544 	}
11545 	return 0;
11546 }
11547 
11548 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx)
11549 {
11550 	struct bnxt_tx_ring_info *txr;
11551 	struct netdev_queue *txq;
11552 	struct bnxt_napi *bnapi;
11553 	int i;
11554 
11555 	bnapi = bp->bnapi[idx];
11556 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11557 		WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11558 		synchronize_net();
11559 
11560 		if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) {
11561 			txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11562 			if (txq) {
11563 				__netif_tx_lock_bh(txq);
11564 				netif_tx_stop_queue(txq);
11565 				__netif_tx_unlock_bh(txq);
11566 			}
11567 		}
11568 
11569 		if (!bp->tph_mode)
11570 			continue;
11571 
11572 		bnxt_hwrm_tx_ring_free(bp, txr, true);
11573 		bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr);
11574 		bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index);
11575 		bnxt_clear_one_cp_ring(bp, txr->tx_cpr);
11576 	}
11577 }
11578 
11579 static int bnxt_tx_queue_start(struct bnxt *bp, int idx)
11580 {
11581 	struct bnxt_tx_ring_info *txr;
11582 	struct netdev_queue *txq;
11583 	struct bnxt_napi *bnapi;
11584 	int rc, i;
11585 
11586 	bnapi = bp->bnapi[idx];
11587 	/* All rings have been reserved and previously allocated.
11588 	 * Reallocating with the same parameters should never fail.
11589 	 */
11590 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11591 		if (!bp->tph_mode)
11592 			goto start_tx;
11593 
11594 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
11595 		if (rc)
11596 			return rc;
11597 
11598 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false);
11599 		if (rc)
11600 			return rc;
11601 
11602 		txr->tx_prod = 0;
11603 		txr->tx_cons = 0;
11604 		txr->tx_hw_cons = 0;
11605 start_tx:
11606 		WRITE_ONCE(txr->dev_state, 0);
11607 		synchronize_net();
11608 
11609 		if (bnapi->flags & BNXT_NAPI_FLAG_XDP)
11610 			continue;
11611 
11612 		txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11613 		if (txq)
11614 			netif_tx_start_queue(txq);
11615 	}
11616 
11617 	return 0;
11618 }
11619 
11620 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify,
11621 				     const cpumask_t *mask)
11622 {
11623 	struct bnxt_irq *irq;
11624 	u16 tag;
11625 	int err;
11626 
11627 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11628 
11629 	if (!irq->bp->tph_mode)
11630 		return;
11631 
11632 	cpumask_copy(irq->cpu_mask, mask);
11633 
11634 	if (irq->ring_nr >= irq->bp->rx_nr_rings)
11635 		return;
11636 
11637 	if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11638 				cpumask_first(irq->cpu_mask), &tag))
11639 		return;
11640 
11641 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag))
11642 		return;
11643 
11644 	netdev_lock(irq->bp->dev);
11645 	if (netif_running(irq->bp->dev)) {
11646 		err = netdev_rx_queue_restart(irq->bp->dev, irq->ring_nr);
11647 		if (err)
11648 			netdev_err(irq->bp->dev,
11649 				   "RX queue restart failed: err=%d\n", err);
11650 	}
11651 	netdev_unlock(irq->bp->dev);
11652 }
11653 
11654 static void bnxt_irq_affinity_release(struct kref *ref)
11655 {
11656 	struct irq_affinity_notify *notify =
11657 		container_of(ref, struct irq_affinity_notify, kref);
11658 	struct bnxt_irq *irq;
11659 
11660 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11661 
11662 	if (!irq->bp->tph_mode)
11663 		return;
11664 
11665 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) {
11666 		netdev_err(irq->bp->dev,
11667 			   "Setting ST=0 for MSIX entry %d failed\n",
11668 			   irq->msix_nr);
11669 		return;
11670 	}
11671 }
11672 
11673 static void bnxt_release_irq_notifier(struct bnxt_irq *irq)
11674 {
11675 	irq_set_affinity_notifier(irq->vector, NULL);
11676 }
11677 
11678 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq)
11679 {
11680 	struct irq_affinity_notify *notify;
11681 
11682 	irq->bp = bp;
11683 
11684 	/* Nothing to do if TPH is not enabled */
11685 	if (!bp->tph_mode)
11686 		return;
11687 
11688 	/* Register IRQ affinity notifier */
11689 	notify = &irq->affinity_notify;
11690 	notify->irq = irq->vector;
11691 	notify->notify = bnxt_irq_affinity_notify;
11692 	notify->release = bnxt_irq_affinity_release;
11693 
11694 	irq_set_affinity_notifier(irq->vector, notify);
11695 }
11696 
11697 static void bnxt_free_irq(struct bnxt *bp)
11698 {
11699 	struct bnxt_irq *irq;
11700 	int i;
11701 
11702 #ifdef CONFIG_RFS_ACCEL
11703 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11704 	bp->dev->rx_cpu_rmap = NULL;
11705 #endif
11706 	if (!bp->irq_tbl || !bp->bnapi)
11707 		return;
11708 
11709 	for (i = 0; i < bp->cp_nr_rings; i++) {
11710 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11711 
11712 		irq = &bp->irq_tbl[map_idx];
11713 		if (irq->requested) {
11714 			if (irq->have_cpumask) {
11715 				irq_update_affinity_hint(irq->vector, NULL);
11716 				free_cpumask_var(irq->cpu_mask);
11717 				irq->have_cpumask = 0;
11718 			}
11719 
11720 			bnxt_release_irq_notifier(irq);
11721 
11722 			free_irq(irq->vector, bp->bnapi[i]);
11723 		}
11724 
11725 		irq->requested = 0;
11726 	}
11727 
11728 	/* Disable TPH support */
11729 	pcie_disable_tph(bp->pdev);
11730 	bp->tph_mode = 0;
11731 }
11732 
11733 static int bnxt_request_irq(struct bnxt *bp)
11734 {
11735 	struct cpu_rmap *rmap = NULL;
11736 	int i, j, rc = 0;
11737 	unsigned long flags = 0;
11738 
11739 	rc = bnxt_setup_int_mode(bp);
11740 	if (rc) {
11741 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11742 			   rc);
11743 		return rc;
11744 	}
11745 #ifdef CONFIG_RFS_ACCEL
11746 	rmap = bp->dev->rx_cpu_rmap;
11747 #endif
11748 
11749 	/* Enable TPH support as part of IRQ request */
11750 	rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE);
11751 	if (!rc)
11752 		bp->tph_mode = PCI_TPH_ST_IV_MODE;
11753 
11754 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11755 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11756 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11757 
11758 		if (IS_ENABLED(CONFIG_RFS_ACCEL) &&
11759 		    rmap && bp->bnapi[i]->rx_ring) {
11760 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11761 			if (rc)
11762 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11763 					    j);
11764 			j++;
11765 		}
11766 
11767 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11768 				 bp->bnapi[i]);
11769 		if (rc)
11770 			break;
11771 
11772 		netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector);
11773 		irq->requested = 1;
11774 
11775 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
11776 			int numa_node = dev_to_node(&bp->pdev->dev);
11777 			u16 tag;
11778 
11779 			irq->have_cpumask = 1;
11780 			irq->msix_nr = map_idx;
11781 			irq->ring_nr = i;
11782 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
11783 					irq->cpu_mask);
11784 			rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
11785 			if (rc) {
11786 				netdev_warn(bp->dev,
11787 					    "Update affinity hint failed, IRQ = %d\n",
11788 					    irq->vector);
11789 				break;
11790 			}
11791 
11792 			bnxt_register_irq_notifier(bp, irq);
11793 
11794 			/* Init ST table entry */
11795 			if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11796 						cpumask_first(irq->cpu_mask),
11797 						&tag))
11798 				continue;
11799 
11800 			pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag);
11801 		}
11802 	}
11803 	return rc;
11804 }
11805 
11806 static void bnxt_del_napi(struct bnxt *bp)
11807 {
11808 	int i;
11809 
11810 	if (!bp->bnapi)
11811 		return;
11812 
11813 	for (i = 0; i < bp->rx_nr_rings; i++)
11814 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
11815 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
11816 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
11817 
11818 	for (i = 0; i < bp->cp_nr_rings; i++) {
11819 		struct bnxt_napi *bnapi = bp->bnapi[i];
11820 
11821 		__netif_napi_del_locked(&bnapi->napi);
11822 	}
11823 	/* We called __netif_napi_del_locked(), we need
11824 	 * to respect an RCU grace period before freeing napi structures.
11825 	 */
11826 	synchronize_net();
11827 }
11828 
11829 static void bnxt_init_napi(struct bnxt *bp)
11830 {
11831 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
11832 	unsigned int cp_nr_rings = bp->cp_nr_rings;
11833 	struct bnxt_napi *bnapi;
11834 	int i;
11835 
11836 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11837 		poll_fn = bnxt_poll_p5;
11838 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11839 		cp_nr_rings--;
11840 
11841 	set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11842 
11843 	for (i = 0; i < cp_nr_rings; i++) {
11844 		bnapi = bp->bnapi[i];
11845 		netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn,
11846 					     bnapi->index);
11847 	}
11848 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11849 		bnapi = bp->bnapi[cp_nr_rings];
11850 		netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
11851 	}
11852 }
11853 
11854 static void bnxt_disable_napi(struct bnxt *bp)
11855 {
11856 	int i;
11857 
11858 	if (!bp->bnapi ||
11859 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11860 		return;
11861 
11862 	for (i = 0; i < bp->cp_nr_rings; i++) {
11863 		struct bnxt_napi *bnapi = bp->bnapi[i];
11864 		struct bnxt_cp_ring_info *cpr;
11865 
11866 		cpr = &bnapi->cp_ring;
11867 		if (bnapi->tx_fault)
11868 			cpr->sw_stats->tx.tx_resets++;
11869 		if (bnapi->in_reset)
11870 			cpr->sw_stats->rx.rx_resets++;
11871 		napi_disable_locked(&bnapi->napi);
11872 	}
11873 }
11874 
11875 static void bnxt_enable_napi(struct bnxt *bp)
11876 {
11877 	int i;
11878 
11879 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11880 	for (i = 0; i < bp->cp_nr_rings; i++) {
11881 		struct bnxt_napi *bnapi = bp->bnapi[i];
11882 		struct bnxt_cp_ring_info *cpr;
11883 
11884 		bnapi->tx_fault = 0;
11885 
11886 		cpr = &bnapi->cp_ring;
11887 		bnapi->in_reset = false;
11888 
11889 		if (bnapi->rx_ring) {
11890 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
11891 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
11892 		}
11893 		napi_enable_locked(&bnapi->napi);
11894 	}
11895 }
11896 
11897 void bnxt_tx_disable(struct bnxt *bp)
11898 {
11899 	int i;
11900 	struct bnxt_tx_ring_info *txr;
11901 
11902 	if (bp->tx_ring) {
11903 		for (i = 0; i < bp->tx_nr_rings; i++) {
11904 			txr = &bp->tx_ring[i];
11905 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11906 		}
11907 	}
11908 	/* Make sure napi polls see @dev_state change */
11909 	synchronize_net();
11910 	/* Drop carrier first to prevent TX timeout */
11911 	netif_carrier_off(bp->dev);
11912 	/* Stop all TX queues */
11913 	netif_tx_disable(bp->dev);
11914 }
11915 
11916 void bnxt_tx_enable(struct bnxt *bp)
11917 {
11918 	int i;
11919 	struct bnxt_tx_ring_info *txr;
11920 
11921 	for (i = 0; i < bp->tx_nr_rings; i++) {
11922 		txr = &bp->tx_ring[i];
11923 		WRITE_ONCE(txr->dev_state, 0);
11924 	}
11925 	/* Make sure napi polls see @dev_state change */
11926 	synchronize_net();
11927 	netif_tx_wake_all_queues(bp->dev);
11928 	if (BNXT_LINK_IS_UP(bp))
11929 		netif_carrier_on(bp->dev);
11930 }
11931 
11932 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
11933 {
11934 	u8 active_fec = link_info->active_fec_sig_mode &
11935 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
11936 
11937 	switch (active_fec) {
11938 	default:
11939 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
11940 		return "None";
11941 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
11942 		return "Clause 74 BaseR";
11943 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
11944 		return "Clause 91 RS(528,514)";
11945 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
11946 		return "Clause 91 RS544_1XN";
11947 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
11948 		return "Clause 91 RS(544,514)";
11949 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
11950 		return "Clause 91 RS272_1XN";
11951 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
11952 		return "Clause 91 RS(272,257)";
11953 	}
11954 }
11955 
11956 static char *bnxt_link_down_reason(struct bnxt_link_info *link_info)
11957 {
11958 	u8 reason = link_info->link_down_reason;
11959 
11960 	/* Multiple bits can be set, we report 1 bit only in order of
11961 	 * priority.
11962 	 */
11963 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_RF)
11964 		return "(Remote fault)";
11965 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_OTP_SPEED_VIOLATION)
11966 		return "(OTP Speed limit violation)";
11967 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_CABLE_REMOVED)
11968 		return "(Cable removed)";
11969 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_MODULE_FAULT)
11970 		return "(Module fault)";
11971 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_BMC_REQUEST)
11972 		return "(BMC request down)";
11973 	return "";
11974 }
11975 
11976 void bnxt_report_link(struct bnxt *bp)
11977 {
11978 	if (BNXT_LINK_IS_UP(bp)) {
11979 		const char *signal = "";
11980 		const char *flow_ctrl;
11981 		const char *duplex;
11982 		u32 speed;
11983 		u16 fec;
11984 
11985 		netif_carrier_on(bp->dev);
11986 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
11987 		if (speed == SPEED_UNKNOWN) {
11988 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
11989 			return;
11990 		}
11991 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
11992 			duplex = "full";
11993 		else
11994 			duplex = "half";
11995 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
11996 			flow_ctrl = "ON - receive & transmit";
11997 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
11998 			flow_ctrl = "ON - transmit";
11999 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
12000 			flow_ctrl = "ON - receive";
12001 		else
12002 			flow_ctrl = "none";
12003 		if (bp->link_info.phy_qcfg_resp.option_flags &
12004 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
12005 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
12006 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
12007 			switch (sig_mode) {
12008 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
12009 				signal = "(NRZ) ";
12010 				break;
12011 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
12012 				signal = "(PAM4 56Gbps) ";
12013 				break;
12014 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
12015 				signal = "(PAM4 112Gbps) ";
12016 				break;
12017 			default:
12018 				break;
12019 			}
12020 		}
12021 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
12022 			    speed, signal, duplex, flow_ctrl);
12023 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
12024 			netdev_info(bp->dev, "EEE is %s\n",
12025 				    bp->eee.eee_active ? "active" :
12026 							 "not active");
12027 		fec = bp->link_info.fec_cfg;
12028 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
12029 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
12030 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
12031 				    bnxt_report_fec(&bp->link_info));
12032 	} else {
12033 		char *str = bnxt_link_down_reason(&bp->link_info);
12034 
12035 		netif_carrier_off(bp->dev);
12036 		netdev_err(bp->dev, "NIC Link is Down %s\n", str);
12037 	}
12038 }
12039 
12040 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
12041 {
12042 	if (!resp->supported_speeds_auto_mode &&
12043 	    !resp->supported_speeds_force_mode &&
12044 	    !resp->supported_pam4_speeds_auto_mode &&
12045 	    !resp->supported_pam4_speeds_force_mode &&
12046 	    !resp->supported_speeds2_auto_mode &&
12047 	    !resp->supported_speeds2_force_mode)
12048 		return true;
12049 	return false;
12050 }
12051 
12052 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
12053 {
12054 	struct bnxt_link_info *link_info = &bp->link_info;
12055 	struct hwrm_port_phy_qcaps_output *resp;
12056 	struct hwrm_port_phy_qcaps_input *req;
12057 	int rc = 0;
12058 
12059 	if (bp->hwrm_spec_code < 0x10201)
12060 		return 0;
12061 
12062 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
12063 	if (rc)
12064 		return rc;
12065 
12066 	resp = hwrm_req_hold(bp, req);
12067 	rc = hwrm_req_send(bp, req);
12068 	if (rc)
12069 		goto hwrm_phy_qcaps_exit;
12070 
12071 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
12072 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
12073 		struct ethtool_keee *eee = &bp->eee;
12074 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
12075 
12076 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
12077 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
12078 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
12079 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
12080 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
12081 	}
12082 
12083 	if (bp->hwrm_spec_code >= 0x10a01) {
12084 		if (bnxt_phy_qcaps_no_speed(resp)) {
12085 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
12086 			netdev_warn(bp->dev, "Ethernet link disabled\n");
12087 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
12088 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
12089 			netdev_info(bp->dev, "Ethernet link enabled\n");
12090 			/* Phy re-enabled, reprobe the speeds */
12091 			link_info->support_auto_speeds = 0;
12092 			link_info->support_pam4_auto_speeds = 0;
12093 			link_info->support_auto_speeds2 = 0;
12094 		}
12095 	}
12096 	if (resp->supported_speeds_auto_mode)
12097 		link_info->support_auto_speeds =
12098 			le16_to_cpu(resp->supported_speeds_auto_mode);
12099 	if (resp->supported_pam4_speeds_auto_mode)
12100 		link_info->support_pam4_auto_speeds =
12101 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
12102 	if (resp->supported_speeds2_auto_mode)
12103 		link_info->support_auto_speeds2 =
12104 			le16_to_cpu(resp->supported_speeds2_auto_mode);
12105 
12106 	bp->port_count = resp->port_cnt;
12107 
12108 hwrm_phy_qcaps_exit:
12109 	hwrm_req_drop(bp, req);
12110 	return rc;
12111 }
12112 
12113 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
12114 {
12115 	struct hwrm_port_mac_qcaps_output *resp;
12116 	struct hwrm_port_mac_qcaps_input *req;
12117 	int rc;
12118 
12119 	if (bp->hwrm_spec_code < 0x10a03)
12120 		return;
12121 
12122 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
12123 	if (rc)
12124 		return;
12125 
12126 	resp = hwrm_req_hold(bp, req);
12127 	rc = hwrm_req_send_silent(bp, req);
12128 	if (!rc)
12129 		bp->mac_flags = resp->flags;
12130 	hwrm_req_drop(bp, req);
12131 }
12132 
12133 static bool bnxt_support_dropped(u16 advertising, u16 supported)
12134 {
12135 	u16 diff = advertising ^ supported;
12136 
12137 	return ((supported | diff) != supported);
12138 }
12139 
12140 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
12141 {
12142 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
12143 
12144 	/* Check if any advertised speeds are no longer supported. The caller
12145 	 * holds the link_lock mutex, so we can modify link_info settings.
12146 	 */
12147 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12148 		if (bnxt_support_dropped(link_info->advertising,
12149 					 link_info->support_auto_speeds2)) {
12150 			link_info->advertising = link_info->support_auto_speeds2;
12151 			return true;
12152 		}
12153 		return false;
12154 	}
12155 	if (bnxt_support_dropped(link_info->advertising,
12156 				 link_info->support_auto_speeds)) {
12157 		link_info->advertising = link_info->support_auto_speeds;
12158 		return true;
12159 	}
12160 	if (bnxt_support_dropped(link_info->advertising_pam4,
12161 				 link_info->support_pam4_auto_speeds)) {
12162 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
12163 		return true;
12164 	}
12165 	return false;
12166 }
12167 
12168 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
12169 {
12170 	struct bnxt_link_info *link_info = &bp->link_info;
12171 	struct hwrm_port_phy_qcfg_output *resp;
12172 	struct hwrm_port_phy_qcfg_input *req;
12173 	u8 link_state = link_info->link_state;
12174 	bool support_changed;
12175 	int rc;
12176 
12177 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
12178 	if (rc)
12179 		return rc;
12180 
12181 	resp = hwrm_req_hold(bp, req);
12182 	rc = hwrm_req_send(bp, req);
12183 	if (rc) {
12184 		hwrm_req_drop(bp, req);
12185 		if (BNXT_VF(bp) && rc == -ENODEV) {
12186 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
12187 			rc = 0;
12188 		}
12189 		return rc;
12190 	}
12191 
12192 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
12193 	link_info->phy_link_status = resp->link;
12194 	link_info->duplex = resp->duplex_cfg;
12195 	if (bp->hwrm_spec_code >= 0x10800)
12196 		link_info->duplex = resp->duplex_state;
12197 	link_info->pause = resp->pause;
12198 	link_info->auto_mode = resp->auto_mode;
12199 	link_info->auto_pause_setting = resp->auto_pause;
12200 	link_info->lp_pause = resp->link_partner_adv_pause;
12201 	link_info->force_pause_setting = resp->force_pause;
12202 	link_info->duplex_setting = resp->duplex_cfg;
12203 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
12204 		link_info->link_speed = le16_to_cpu(resp->link_speed);
12205 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
12206 			link_info->active_lanes = resp->active_lanes;
12207 	} else {
12208 		link_info->link_speed = 0;
12209 		link_info->active_lanes = 0;
12210 	}
12211 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
12212 	link_info->force_pam4_link_speed =
12213 		le16_to_cpu(resp->force_pam4_link_speed);
12214 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
12215 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
12216 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
12217 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
12218 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
12219 	link_info->auto_pam4_link_speeds =
12220 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
12221 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
12222 	link_info->lp_auto_link_speeds =
12223 		le16_to_cpu(resp->link_partner_adv_speeds);
12224 	link_info->lp_auto_pam4_link_speeds =
12225 		resp->link_partner_pam4_adv_speeds;
12226 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
12227 	link_info->phy_ver[0] = resp->phy_maj;
12228 	link_info->phy_ver[1] = resp->phy_min;
12229 	link_info->phy_ver[2] = resp->phy_bld;
12230 	link_info->media_type = resp->media_type;
12231 	link_info->phy_type = resp->phy_type;
12232 	link_info->transceiver = resp->xcvr_pkg_type;
12233 	link_info->phy_addr = resp->eee_config_phy_addr &
12234 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
12235 	link_info->module_status = resp->module_status;
12236 	link_info->link_down_reason = resp->link_down_reason;
12237 
12238 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
12239 		struct ethtool_keee *eee = &bp->eee;
12240 		u16 fw_speeds;
12241 
12242 		eee->eee_active = 0;
12243 		if (resp->eee_config_phy_addr &
12244 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
12245 			eee->eee_active = 1;
12246 			fw_speeds = le16_to_cpu(
12247 				resp->link_partner_adv_eee_link_speed_mask);
12248 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
12249 		}
12250 
12251 		/* Pull initial EEE config */
12252 		if (!chng_link_state) {
12253 			if (resp->eee_config_phy_addr &
12254 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
12255 				eee->eee_enabled = 1;
12256 
12257 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
12258 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
12259 
12260 			if (resp->eee_config_phy_addr &
12261 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
12262 				__le32 tmr;
12263 
12264 				eee->tx_lpi_enabled = 1;
12265 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
12266 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
12267 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
12268 			}
12269 		}
12270 	}
12271 
12272 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
12273 	if (bp->hwrm_spec_code >= 0x10504) {
12274 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
12275 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
12276 	}
12277 	/* TODO: need to add more logic to report VF link */
12278 	if (chng_link_state) {
12279 		if (link_info->phy_link_status == BNXT_LINK_LINK)
12280 			link_info->link_state = BNXT_LINK_STATE_UP;
12281 		else
12282 			link_info->link_state = BNXT_LINK_STATE_DOWN;
12283 		if (link_state != link_info->link_state)
12284 			bnxt_report_link(bp);
12285 	} else {
12286 		/* always link down if not require to update link state */
12287 		link_info->link_state = BNXT_LINK_STATE_DOWN;
12288 	}
12289 	hwrm_req_drop(bp, req);
12290 
12291 	if (!BNXT_PHY_CFG_ABLE(bp))
12292 		return 0;
12293 
12294 	support_changed = bnxt_support_speed_dropped(link_info);
12295 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
12296 		bnxt_hwrm_set_link_setting(bp, true, false);
12297 	return 0;
12298 }
12299 
12300 static void bnxt_get_port_module_status(struct bnxt *bp)
12301 {
12302 	struct bnxt_link_info *link_info = &bp->link_info;
12303 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
12304 	u8 module_status;
12305 
12306 	if (bnxt_update_link(bp, true))
12307 		return;
12308 
12309 	module_status = link_info->module_status;
12310 	switch (module_status) {
12311 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
12312 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
12313 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
12314 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
12315 			    bp->pf.port_id);
12316 		if (bp->hwrm_spec_code >= 0x10201) {
12317 			netdev_warn(bp->dev, "Module part number %s\n",
12318 				    resp->phy_vendor_partnumber);
12319 		}
12320 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
12321 			netdev_warn(bp->dev, "TX is disabled\n");
12322 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
12323 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
12324 	}
12325 }
12326 
12327 static void
12328 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12329 {
12330 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
12331 		if (bp->hwrm_spec_code >= 0x10201)
12332 			req->auto_pause =
12333 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
12334 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12335 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
12336 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12337 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
12338 		req->enables |=
12339 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12340 	} else {
12341 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12342 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
12343 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12344 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
12345 		req->enables |=
12346 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
12347 		if (bp->hwrm_spec_code >= 0x10201) {
12348 			req->auto_pause = req->force_pause;
12349 			req->enables |= cpu_to_le32(
12350 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12351 		}
12352 	}
12353 }
12354 
12355 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12356 {
12357 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
12358 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
12359 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12360 			req->enables |=
12361 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
12362 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
12363 		} else if (bp->link_info.advertising) {
12364 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
12365 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
12366 		}
12367 		if (bp->link_info.advertising_pam4) {
12368 			req->enables |=
12369 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
12370 			req->auto_link_pam4_speed_mask =
12371 				cpu_to_le16(bp->link_info.advertising_pam4);
12372 		}
12373 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
12374 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
12375 	} else {
12376 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
12377 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12378 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
12379 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
12380 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
12381 				   (u32)bp->link_info.req_link_speed);
12382 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
12383 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12384 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
12385 		} else {
12386 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12387 		}
12388 	}
12389 
12390 	/* tell chimp that the setting takes effect immediately */
12391 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
12392 }
12393 
12394 int bnxt_hwrm_set_pause(struct bnxt *bp)
12395 {
12396 	struct hwrm_port_phy_cfg_input *req;
12397 	int rc;
12398 
12399 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12400 	if (rc)
12401 		return rc;
12402 
12403 	bnxt_hwrm_set_pause_common(bp, req);
12404 
12405 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
12406 	    bp->link_info.force_link_chng)
12407 		bnxt_hwrm_set_link_common(bp, req);
12408 
12409 	rc = hwrm_req_send(bp, req);
12410 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
12411 		/* since changing of pause setting doesn't trigger any link
12412 		 * change event, the driver needs to update the current pause
12413 		 * result upon successfully return of the phy_cfg command
12414 		 */
12415 		bp->link_info.pause =
12416 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
12417 		bp->link_info.auto_pause_setting = 0;
12418 		if (!bp->link_info.force_link_chng)
12419 			bnxt_report_link(bp);
12420 	}
12421 	bp->link_info.force_link_chng = false;
12422 	return rc;
12423 }
12424 
12425 static void bnxt_hwrm_set_eee(struct bnxt *bp,
12426 			      struct hwrm_port_phy_cfg_input *req)
12427 {
12428 	struct ethtool_keee *eee = &bp->eee;
12429 
12430 	if (eee->eee_enabled) {
12431 		u16 eee_speeds;
12432 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
12433 
12434 		if (eee->tx_lpi_enabled)
12435 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
12436 		else
12437 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
12438 
12439 		req->flags |= cpu_to_le32(flags);
12440 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
12441 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
12442 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
12443 	} else {
12444 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
12445 	}
12446 }
12447 
12448 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
12449 {
12450 	struct hwrm_port_phy_cfg_input *req;
12451 	int rc;
12452 
12453 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12454 	if (rc)
12455 		return rc;
12456 
12457 	if (set_pause)
12458 		bnxt_hwrm_set_pause_common(bp, req);
12459 
12460 	bnxt_hwrm_set_link_common(bp, req);
12461 
12462 	if (set_eee)
12463 		bnxt_hwrm_set_eee(bp, req);
12464 	return hwrm_req_send(bp, req);
12465 }
12466 
12467 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
12468 {
12469 	struct hwrm_port_phy_cfg_input *req;
12470 	int rc;
12471 
12472 	if (!BNXT_SINGLE_PF(bp))
12473 		return 0;
12474 
12475 	if (pci_num_vf(bp->pdev) &&
12476 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
12477 		return 0;
12478 
12479 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12480 	if (rc)
12481 		return rc;
12482 
12483 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
12484 	rc = hwrm_req_send(bp, req);
12485 	if (!rc) {
12486 		mutex_lock(&bp->link_lock);
12487 		/* Device is not obliged link down in certain scenarios, even
12488 		 * when forced. Setting the state unknown is consistent with
12489 		 * driver startup and will force link state to be reported
12490 		 * during subsequent open based on PORT_PHY_QCFG.
12491 		 */
12492 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
12493 		mutex_unlock(&bp->link_lock);
12494 	}
12495 	return rc;
12496 }
12497 
12498 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
12499 {
12500 #ifdef CONFIG_TEE_BNXT_FW
12501 	int rc = tee_bnxt_fw_load();
12502 
12503 	if (rc)
12504 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
12505 
12506 	return rc;
12507 #else
12508 	netdev_err(bp->dev, "OP-TEE not supported\n");
12509 	return -ENODEV;
12510 #endif
12511 }
12512 
12513 static int bnxt_try_recover_fw(struct bnxt *bp)
12514 {
12515 	if (bp->fw_health && bp->fw_health->status_reliable) {
12516 		int retry = 0, rc;
12517 		u32 sts;
12518 
12519 		do {
12520 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12521 			rc = bnxt_hwrm_poll(bp);
12522 			if (!BNXT_FW_IS_BOOTING(sts) &&
12523 			    !BNXT_FW_IS_RECOVERING(sts))
12524 				break;
12525 			retry++;
12526 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
12527 
12528 		if (!BNXT_FW_IS_HEALTHY(sts)) {
12529 			netdev_err(bp->dev,
12530 				   "Firmware not responding, status: 0x%x\n",
12531 				   sts);
12532 			rc = -ENODEV;
12533 		}
12534 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
12535 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
12536 			return bnxt_fw_reset_via_optee(bp);
12537 		}
12538 		return rc;
12539 	}
12540 
12541 	return -ENODEV;
12542 }
12543 
12544 void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
12545 {
12546 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12547 
12548 	if (!BNXT_NEW_RM(bp))
12549 		return; /* no resource reservations required */
12550 
12551 	hw_resc->resv_cp_rings = 0;
12552 	hw_resc->resv_stat_ctxs = 0;
12553 	hw_resc->resv_irqs = 0;
12554 	hw_resc->resv_tx_rings = 0;
12555 	hw_resc->resv_rx_rings = 0;
12556 	hw_resc->resv_hw_ring_grps = 0;
12557 	hw_resc->resv_vnics = 0;
12558 	hw_resc->resv_rsscos_ctxs = 0;
12559 	if (!fw_reset) {
12560 		bp->tx_nr_rings = 0;
12561 		bp->rx_nr_rings = 0;
12562 	}
12563 }
12564 
12565 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12566 {
12567 	int rc;
12568 
12569 	if (!BNXT_NEW_RM(bp))
12570 		return 0; /* no resource reservations required */
12571 
12572 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12573 	if (rc)
12574 		netdev_err(bp->dev, "resc_qcaps failed\n");
12575 
12576 	bnxt_clear_reservations(bp, fw_reset);
12577 
12578 	return rc;
12579 }
12580 
12581 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12582 {
12583 	struct hwrm_func_drv_if_change_output *resp;
12584 	struct hwrm_func_drv_if_change_input *req;
12585 	bool resc_reinit = false;
12586 	bool caps_change = false;
12587 	int rc, retry = 0;
12588 	bool fw_reset;
12589 	u32 flags = 0;
12590 
12591 	fw_reset = (bp->fw_reset_state == BNXT_FW_RESET_STATE_ABORT);
12592 	bp->fw_reset_state = 0;
12593 
12594 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12595 		return 0;
12596 
12597 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12598 	if (rc)
12599 		return rc;
12600 
12601 	if (up)
12602 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12603 	resp = hwrm_req_hold(bp, req);
12604 
12605 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12606 	while (retry < BNXT_FW_IF_RETRY) {
12607 		rc = hwrm_req_send(bp, req);
12608 		if (rc != -EAGAIN)
12609 			break;
12610 
12611 		msleep(50);
12612 		retry++;
12613 	}
12614 
12615 	if (rc == -EAGAIN) {
12616 		hwrm_req_drop(bp, req);
12617 		return rc;
12618 	} else if (!rc) {
12619 		flags = le32_to_cpu(resp->flags);
12620 	} else if (up) {
12621 		rc = bnxt_try_recover_fw(bp);
12622 		fw_reset = true;
12623 	}
12624 	hwrm_req_drop(bp, req);
12625 	if (rc)
12626 		return rc;
12627 
12628 	if (!up) {
12629 		bnxt_inv_fw_health_reg(bp);
12630 		return 0;
12631 	}
12632 
12633 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12634 		resc_reinit = true;
12635 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12636 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12637 		fw_reset = true;
12638 	else
12639 		bnxt_remap_fw_health_regs(bp);
12640 
12641 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12642 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12643 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12644 		return -ENODEV;
12645 	}
12646 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE)
12647 		caps_change = true;
12648 
12649 	if (resc_reinit || fw_reset || caps_change) {
12650 		if (fw_reset || caps_change) {
12651 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12652 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12653 				bnxt_ulp_irq_stop(bp);
12654 			bnxt_free_ctx_mem(bp, false);
12655 			bnxt_dcb_free(bp);
12656 			rc = bnxt_fw_init_one(bp);
12657 			if (rc) {
12658 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12659 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12660 				return rc;
12661 			}
12662 			/* IRQ will be initialized later in bnxt_request_irq()*/
12663 			bnxt_clear_int_mode(bp);
12664 		}
12665 		rc = bnxt_cancel_reservations(bp, fw_reset);
12666 	}
12667 	return rc;
12668 }
12669 
12670 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12671 {
12672 	struct hwrm_port_led_qcaps_output *resp;
12673 	struct hwrm_port_led_qcaps_input *req;
12674 	struct bnxt_pf_info *pf = &bp->pf;
12675 	int rc;
12676 
12677 	bp->num_leds = 0;
12678 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12679 		return 0;
12680 
12681 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12682 	if (rc)
12683 		return rc;
12684 
12685 	req->port_id = cpu_to_le16(pf->port_id);
12686 	resp = hwrm_req_hold(bp, req);
12687 	rc = hwrm_req_send(bp, req);
12688 	if (rc) {
12689 		hwrm_req_drop(bp, req);
12690 		return rc;
12691 	}
12692 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12693 		int i;
12694 
12695 		bp->num_leds = resp->num_leds;
12696 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12697 						 bp->num_leds);
12698 		for (i = 0; i < bp->num_leds; i++) {
12699 			struct bnxt_led_info *led = &bp->leds[i];
12700 			__le16 caps = led->led_state_caps;
12701 
12702 			if (!led->led_group_id ||
12703 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12704 				bp->num_leds = 0;
12705 				break;
12706 			}
12707 		}
12708 	}
12709 	hwrm_req_drop(bp, req);
12710 	return 0;
12711 }
12712 
12713 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12714 {
12715 	struct hwrm_wol_filter_alloc_output *resp;
12716 	struct hwrm_wol_filter_alloc_input *req;
12717 	int rc;
12718 
12719 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12720 	if (rc)
12721 		return rc;
12722 
12723 	req->port_id = cpu_to_le16(bp->pf.port_id);
12724 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12725 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12726 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12727 
12728 	resp = hwrm_req_hold(bp, req);
12729 	rc = hwrm_req_send(bp, req);
12730 	if (!rc)
12731 		bp->wol_filter_id = resp->wol_filter_id;
12732 	hwrm_req_drop(bp, req);
12733 	return rc;
12734 }
12735 
12736 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12737 {
12738 	struct hwrm_wol_filter_free_input *req;
12739 	int rc;
12740 
12741 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12742 	if (rc)
12743 		return rc;
12744 
12745 	req->port_id = cpu_to_le16(bp->pf.port_id);
12746 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12747 	req->wol_filter_id = bp->wol_filter_id;
12748 
12749 	return hwrm_req_send(bp, req);
12750 }
12751 
12752 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12753 {
12754 	struct hwrm_wol_filter_qcfg_output *resp;
12755 	struct hwrm_wol_filter_qcfg_input *req;
12756 	u16 next_handle = 0;
12757 	int rc;
12758 
12759 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12760 	if (rc)
12761 		return rc;
12762 
12763 	req->port_id = cpu_to_le16(bp->pf.port_id);
12764 	req->handle = cpu_to_le16(handle);
12765 	resp = hwrm_req_hold(bp, req);
12766 	rc = hwrm_req_send(bp, req);
12767 	if (!rc) {
12768 		next_handle = le16_to_cpu(resp->next_handle);
12769 		if (next_handle != 0) {
12770 			if (resp->wol_type ==
12771 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12772 				bp->wol = 1;
12773 				bp->wol_filter_id = resp->wol_filter_id;
12774 			}
12775 		}
12776 	}
12777 	hwrm_req_drop(bp, req);
12778 	return next_handle;
12779 }
12780 
12781 static void bnxt_get_wol_settings(struct bnxt *bp)
12782 {
12783 	u16 handle = 0;
12784 
12785 	bp->wol = 0;
12786 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
12787 		return;
12788 
12789 	do {
12790 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
12791 	} while (handle && handle != 0xffff);
12792 }
12793 
12794 static bool bnxt_eee_config_ok(struct bnxt *bp)
12795 {
12796 	struct ethtool_keee *eee = &bp->eee;
12797 	struct bnxt_link_info *link_info = &bp->link_info;
12798 
12799 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
12800 		return true;
12801 
12802 	if (eee->eee_enabled) {
12803 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
12804 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
12805 
12806 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
12807 
12808 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12809 			eee->eee_enabled = 0;
12810 			return false;
12811 		}
12812 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
12813 			linkmode_and(eee->advertised, advertising,
12814 				     eee->supported);
12815 			return false;
12816 		}
12817 	}
12818 	return true;
12819 }
12820 
12821 static int bnxt_update_phy_setting(struct bnxt *bp)
12822 {
12823 	int rc;
12824 	bool update_link = false;
12825 	bool update_pause = false;
12826 	bool update_eee = false;
12827 	struct bnxt_link_info *link_info = &bp->link_info;
12828 
12829 	rc = bnxt_update_link(bp, true);
12830 	if (rc) {
12831 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
12832 			   rc);
12833 		return rc;
12834 	}
12835 	if (!BNXT_SINGLE_PF(bp))
12836 		return 0;
12837 
12838 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12839 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
12840 	    link_info->req_flow_ctrl)
12841 		update_pause = true;
12842 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12843 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
12844 		update_pause = true;
12845 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12846 		if (BNXT_AUTO_MODE(link_info->auto_mode))
12847 			update_link = true;
12848 		if (bnxt_force_speed_updated(link_info))
12849 			update_link = true;
12850 		if (link_info->req_duplex != link_info->duplex_setting)
12851 			update_link = true;
12852 	} else {
12853 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
12854 			update_link = true;
12855 		if (bnxt_auto_speed_updated(link_info))
12856 			update_link = true;
12857 	}
12858 
12859 	/* The last close may have shutdown the link, so need to call
12860 	 * PHY_CFG to bring it back up.
12861 	 */
12862 	if (!BNXT_LINK_IS_UP(bp))
12863 		update_link = true;
12864 
12865 	if (!bnxt_eee_config_ok(bp))
12866 		update_eee = true;
12867 
12868 	if (update_link)
12869 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
12870 	else if (update_pause)
12871 		rc = bnxt_hwrm_set_pause(bp);
12872 	if (rc) {
12873 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
12874 			   rc);
12875 		return rc;
12876 	}
12877 
12878 	return rc;
12879 }
12880 
12881 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
12882 
12883 static int bnxt_reinit_after_abort(struct bnxt *bp)
12884 {
12885 	int rc;
12886 
12887 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12888 		return -EBUSY;
12889 
12890 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
12891 		return -ENODEV;
12892 
12893 	rc = bnxt_fw_init_one(bp);
12894 	if (!rc) {
12895 		bnxt_clear_int_mode(bp);
12896 		rc = bnxt_init_int_mode(bp);
12897 		if (!rc) {
12898 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12899 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12900 		}
12901 	}
12902 	return rc;
12903 }
12904 
12905 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
12906 {
12907 	struct bnxt_ntuple_filter *ntp_fltr;
12908 	struct bnxt_l2_filter *l2_fltr;
12909 
12910 	if (list_empty(&fltr->list))
12911 		return;
12912 
12913 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
12914 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
12915 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
12916 		atomic_inc(&l2_fltr->refcnt);
12917 		ntp_fltr->l2_fltr = l2_fltr;
12918 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
12919 			bnxt_del_ntp_filter(bp, ntp_fltr);
12920 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
12921 				   fltr->sw_id);
12922 		}
12923 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
12924 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
12925 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
12926 			bnxt_del_l2_filter(bp, l2_fltr);
12927 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
12928 				   fltr->sw_id);
12929 		}
12930 	}
12931 }
12932 
12933 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
12934 {
12935 	struct bnxt_filter_base *usr_fltr, *tmp;
12936 
12937 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
12938 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
12939 }
12940 
12941 static int bnxt_set_xps_mapping(struct bnxt *bp)
12942 {
12943 	int numa_node = dev_to_node(&bp->pdev->dev);
12944 	unsigned int q_idx, map_idx, cpu, i;
12945 	const struct cpumask *cpu_mask_ptr;
12946 	int nr_cpus = num_online_cpus();
12947 	cpumask_t *q_map;
12948 	int rc = 0;
12949 
12950 	q_map = kzalloc_objs(*q_map, bp->tx_nr_rings_per_tc);
12951 	if (!q_map)
12952 		return -ENOMEM;
12953 
12954 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
12955 	 * Each TC has the same number of TX queues. The nth TX queue for each
12956 	 * TC will have the same CPU mask.
12957 	 */
12958 	for (i = 0; i < nr_cpus; i++) {
12959 		map_idx = i % bp->tx_nr_rings_per_tc;
12960 		cpu = cpumask_local_spread(i, numa_node);
12961 		cpu_mask_ptr = get_cpu_mask(cpu);
12962 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
12963 	}
12964 
12965 	/* Register CPU mask for each TX queue except the ones marked for XDP */
12966 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
12967 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
12968 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
12969 		if (rc) {
12970 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
12971 				    q_idx);
12972 			break;
12973 		}
12974 	}
12975 
12976 	kfree(q_map);
12977 
12978 	return rc;
12979 }
12980 
12981 static int bnxt_tx_nr_rings(struct bnxt *bp)
12982 {
12983 	return bp->num_tc ? bp->tx_nr_rings_per_tc * bp->num_tc :
12984 			    bp->tx_nr_rings_per_tc;
12985 }
12986 
12987 static int bnxt_tx_nr_rings_per_tc(struct bnxt *bp)
12988 {
12989 	return bp->num_tc ? bp->tx_nr_rings / bp->num_tc : bp->tx_nr_rings;
12990 }
12991 
12992 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12993 {
12994 	int rc = 0;
12995 
12996 	netif_carrier_off(bp->dev);
12997 	if (irq_re_init) {
12998 		/* Reserve rings now if none were reserved at driver probe. */
12999 		rc = bnxt_init_dflt_ring_mode(bp);
13000 		if (rc) {
13001 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
13002 			return rc;
13003 		}
13004 	}
13005 	rc = bnxt_reserve_rings(bp, irq_re_init);
13006 	if (rc)
13007 		return rc;
13008 
13009 	/* Make adjustments if reserved TX rings are less than requested */
13010 	bp->tx_nr_rings -= bp->tx_nr_rings_xdp;
13011 	bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp);
13012 	if (bp->tx_nr_rings_xdp) {
13013 		bp->tx_nr_rings_xdp = bp->tx_nr_rings_per_tc;
13014 		bp->tx_nr_rings += bp->tx_nr_rings_xdp;
13015 	}
13016 	rc = bnxt_alloc_mem(bp, irq_re_init);
13017 	if (rc) {
13018 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
13019 		goto open_err_free_mem;
13020 	}
13021 
13022 	if (irq_re_init) {
13023 		bnxt_init_napi(bp);
13024 		rc = bnxt_request_irq(bp);
13025 		if (rc) {
13026 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
13027 			goto open_err_irq;
13028 		}
13029 	}
13030 
13031 	rc = bnxt_init_nic(bp, irq_re_init);
13032 	if (rc) {
13033 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
13034 		goto open_err_irq;
13035 	}
13036 
13037 	bnxt_enable_napi(bp);
13038 	bnxt_debug_dev_init(bp);
13039 
13040 	if (link_re_init) {
13041 		mutex_lock(&bp->link_lock);
13042 		rc = bnxt_update_phy_setting(bp);
13043 		mutex_unlock(&bp->link_lock);
13044 		if (rc) {
13045 			netdev_warn(bp->dev, "failed to update phy settings\n");
13046 			if (BNXT_SINGLE_PF(bp)) {
13047 				bp->link_info.phy_retry = true;
13048 				bp->link_info.phy_retry_expires =
13049 					jiffies + 5 * HZ;
13050 			}
13051 		}
13052 	}
13053 
13054 	if (irq_re_init) {
13055 		udp_tunnel_nic_reset_ntf(bp->dev);
13056 		rc = bnxt_set_xps_mapping(bp);
13057 		if (rc)
13058 			netdev_warn(bp->dev, "failed to set xps mapping\n");
13059 	}
13060 
13061 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
13062 		if (!static_key_enabled(&bnxt_xdp_locking_key))
13063 			static_branch_enable(&bnxt_xdp_locking_key);
13064 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
13065 		static_branch_disable(&bnxt_xdp_locking_key);
13066 	}
13067 	set_bit(BNXT_STATE_OPEN, &bp->state);
13068 	bnxt_enable_int(bp);
13069 	/* Enable TX queues */
13070 	bnxt_tx_enable(bp);
13071 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13072 	/* Poll link status and check for SFP+ module status */
13073 	mutex_lock(&bp->link_lock);
13074 	bnxt_get_port_module_status(bp);
13075 	mutex_unlock(&bp->link_lock);
13076 
13077 	/* VF-reps may need to be re-opened after the PF is re-opened */
13078 	if (BNXT_PF(bp))
13079 		bnxt_vf_reps_open(bp);
13080 	bnxt_ptp_init_rtc(bp, true);
13081 	bnxt_ptp_cfg_tstamp_filters(bp);
13082 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13083 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
13084 	bnxt_cfg_usr_fltrs(bp);
13085 	return 0;
13086 
13087 open_err_irq:
13088 	bnxt_del_napi(bp);
13089 
13090 open_err_free_mem:
13091 	bnxt_free_skbs(bp);
13092 	bnxt_free_irq(bp);
13093 	bnxt_free_mem(bp, true);
13094 	return rc;
13095 }
13096 
13097 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13098 {
13099 	int rc = 0;
13100 
13101 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
13102 		rc = -EIO;
13103 	if (!rc)
13104 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
13105 	if (rc) {
13106 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
13107 		netif_close(bp->dev);
13108 	}
13109 	return rc;
13110 }
13111 
13112 /* netdev instance lock held, open the NIC half way by allocating all
13113  * resources, but NAPI, IRQ, and TX are not enabled.  This is mainly used
13114  * for offline self tests.
13115  */
13116 int bnxt_half_open_nic(struct bnxt *bp)
13117 {
13118 	int rc = 0;
13119 
13120 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13121 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
13122 		rc = -ENODEV;
13123 		goto half_open_err;
13124 	}
13125 
13126 	rc = bnxt_alloc_mem(bp, true);
13127 	if (rc) {
13128 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
13129 		goto half_open_err;
13130 	}
13131 	bnxt_init_napi(bp);
13132 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13133 	rc = bnxt_init_nic(bp, true);
13134 	if (rc) {
13135 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13136 		bnxt_del_napi(bp);
13137 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
13138 		goto half_open_err;
13139 	}
13140 	return 0;
13141 
13142 half_open_err:
13143 	bnxt_free_skbs(bp);
13144 	bnxt_free_mem(bp, true);
13145 	netif_close(bp->dev);
13146 	return rc;
13147 }
13148 
13149 /* netdev instance lock held, this call can only be made after a previous
13150  * successful call to bnxt_half_open_nic().
13151  */
13152 void bnxt_half_close_nic(struct bnxt *bp)
13153 {
13154 	bnxt_hwrm_resource_free(bp, false, true);
13155 	bnxt_del_napi(bp);
13156 	bnxt_free_skbs(bp);
13157 	bnxt_free_mem(bp, true);
13158 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13159 }
13160 
13161 void bnxt_reenable_sriov(struct bnxt *bp)
13162 {
13163 	if (BNXT_PF(bp)) {
13164 		struct bnxt_pf_info *pf = &bp->pf;
13165 		int n = pf->active_vfs;
13166 
13167 		if (n)
13168 			bnxt_cfg_hw_sriov(bp, &n, true);
13169 	}
13170 }
13171 
13172 static int bnxt_open(struct net_device *dev)
13173 {
13174 	struct bnxt *bp = netdev_priv(dev);
13175 	int rc;
13176 
13177 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13178 		rc = bnxt_reinit_after_abort(bp);
13179 		if (rc) {
13180 			if (rc == -EBUSY)
13181 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
13182 			else
13183 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
13184 			return -ENODEV;
13185 		}
13186 	}
13187 
13188 	rc = bnxt_hwrm_if_change(bp, true);
13189 	if (rc)
13190 		return rc;
13191 
13192 	rc = __bnxt_open_nic(bp, true, true);
13193 	if (rc) {
13194 		bnxt_hwrm_if_change(bp, false);
13195 	} else {
13196 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
13197 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13198 				bnxt_queue_sp_work(bp,
13199 						   BNXT_RESTART_ULP_SP_EVENT);
13200 		}
13201 	}
13202 
13203 	return rc;
13204 }
13205 
13206 static bool bnxt_drv_busy(struct bnxt *bp)
13207 {
13208 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
13209 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
13210 }
13211 
13212 static void bnxt_get_ring_stats(struct bnxt *bp,
13213 				struct rtnl_link_stats64 *stats);
13214 
13215 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
13216 			     bool link_re_init)
13217 {
13218 	/* Close the VF-reps before closing PF */
13219 	if (BNXT_PF(bp))
13220 		bnxt_vf_reps_close(bp);
13221 
13222 	/* Change device state to avoid TX queue wake up's */
13223 	bnxt_tx_disable(bp);
13224 
13225 	clear_bit(BNXT_STATE_OPEN, &bp->state);
13226 	smp_mb__after_atomic();
13227 	while (bnxt_drv_busy(bp))
13228 		msleep(20);
13229 
13230 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13231 		bnxt_clear_rss_ctxs(bp);
13232 	/* Flush rings and disable interrupts */
13233 	bnxt_shutdown_nic(bp, irq_re_init);
13234 
13235 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
13236 
13237 	bnxt_debug_dev_exit(bp);
13238 	bnxt_disable_napi(bp);
13239 	timer_delete_sync(&bp->timer);
13240 	bnxt_free_skbs(bp);
13241 
13242 	/* Save ring stats before shutdown */
13243 	if (bp->bnapi && irq_re_init) {
13244 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
13245 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
13246 	}
13247 	if (irq_re_init) {
13248 		bnxt_free_irq(bp);
13249 		bnxt_del_napi(bp);
13250 	}
13251 	bnxt_free_mem(bp, irq_re_init);
13252 }
13253 
13254 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13255 {
13256 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13257 		/* If we get here, it means firmware reset is in progress
13258 		 * while we are trying to close.  We can safely proceed with
13259 		 * the close because we are holding netdev instance lock.
13260 		 * Some firmware messages may fail as we proceed to close.
13261 		 * We set the ABORT_ERR flag here so that the FW reset thread
13262 		 * will later abort when it gets the netdev instance lock
13263 		 * and sees the flag.
13264 		 */
13265 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
13266 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13267 	}
13268 
13269 #ifdef CONFIG_BNXT_SRIOV
13270 	if (bp->sriov_cfg) {
13271 		int rc;
13272 
13273 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
13274 						      !bp->sriov_cfg,
13275 						      BNXT_SRIOV_CFG_WAIT_TMO);
13276 		if (!rc)
13277 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
13278 		else if (rc < 0)
13279 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
13280 	}
13281 #endif
13282 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
13283 }
13284 
13285 static int bnxt_close(struct net_device *dev)
13286 {
13287 	struct bnxt *bp = netdev_priv(dev);
13288 
13289 	bnxt_close_nic(bp, true, true);
13290 	bnxt_hwrm_shutdown_link(bp);
13291 	bnxt_hwrm_if_change(bp, false);
13292 	return 0;
13293 }
13294 
13295 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
13296 				   u16 *val)
13297 {
13298 	struct hwrm_port_phy_mdio_read_output *resp;
13299 	struct hwrm_port_phy_mdio_read_input *req;
13300 	int rc;
13301 
13302 	if (bp->hwrm_spec_code < 0x10a00)
13303 		return -EOPNOTSUPP;
13304 
13305 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
13306 	if (rc)
13307 		return rc;
13308 
13309 	req->port_id = cpu_to_le16(bp->pf.port_id);
13310 	req->phy_addr = phy_addr;
13311 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13312 	if (mdio_phy_id_is_c45(phy_addr)) {
13313 		req->cl45_mdio = 1;
13314 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13315 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13316 		req->reg_addr = cpu_to_le16(reg);
13317 	}
13318 
13319 	resp = hwrm_req_hold(bp, req);
13320 	rc = hwrm_req_send(bp, req);
13321 	if (!rc)
13322 		*val = le16_to_cpu(resp->reg_data);
13323 	hwrm_req_drop(bp, req);
13324 	return rc;
13325 }
13326 
13327 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
13328 				    u16 val)
13329 {
13330 	struct hwrm_port_phy_mdio_write_input *req;
13331 	int rc;
13332 
13333 	if (bp->hwrm_spec_code < 0x10a00)
13334 		return -EOPNOTSUPP;
13335 
13336 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
13337 	if (rc)
13338 		return rc;
13339 
13340 	req->port_id = cpu_to_le16(bp->pf.port_id);
13341 	req->phy_addr = phy_addr;
13342 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13343 	if (mdio_phy_id_is_c45(phy_addr)) {
13344 		req->cl45_mdio = 1;
13345 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13346 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13347 		req->reg_addr = cpu_to_le16(reg);
13348 	}
13349 	req->reg_data = cpu_to_le16(val);
13350 
13351 	return hwrm_req_send(bp, req);
13352 }
13353 
13354 /* netdev instance lock held */
13355 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13356 {
13357 	struct mii_ioctl_data *mdio = if_mii(ifr);
13358 	struct bnxt *bp = netdev_priv(dev);
13359 	int rc;
13360 
13361 	switch (cmd) {
13362 	case SIOCGMIIPHY:
13363 		mdio->phy_id = bp->link_info.phy_addr;
13364 
13365 		fallthrough;
13366 	case SIOCGMIIREG: {
13367 		u16 mii_regval = 0;
13368 
13369 		if (!netif_running(dev))
13370 			return -EAGAIN;
13371 
13372 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
13373 					     &mii_regval);
13374 		mdio->val_out = mii_regval;
13375 		return rc;
13376 	}
13377 
13378 	case SIOCSMIIREG:
13379 		if (!netif_running(dev))
13380 			return -EAGAIN;
13381 
13382 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
13383 						mdio->val_in);
13384 
13385 	default:
13386 		/* do nothing */
13387 		break;
13388 	}
13389 	return -EOPNOTSUPP;
13390 }
13391 
13392 static void bnxt_get_ring_stats(struct bnxt *bp,
13393 				struct rtnl_link_stats64 *stats)
13394 {
13395 	int i;
13396 
13397 	for (i = 0; i < bp->cp_nr_rings; i++) {
13398 		struct bnxt_napi *bnapi = bp->bnapi[i];
13399 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13400 		u64 *sw = cpr->stats.sw_stats;
13401 
13402 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
13403 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13404 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
13405 
13406 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
13407 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
13408 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
13409 
13410 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
13411 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
13412 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
13413 
13414 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
13415 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
13416 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
13417 
13418 		stats->rx_missed_errors +=
13419 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
13420 
13421 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13422 
13423 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
13424 
13425 		stats->rx_dropped +=
13426 			cpr->sw_stats->rx.rx_netpoll_discards +
13427 			cpr->sw_stats->rx.rx_oom_discards;
13428 	}
13429 }
13430 
13431 static void bnxt_add_prev_stats(struct bnxt *bp,
13432 				struct rtnl_link_stats64 *stats)
13433 {
13434 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
13435 
13436 	stats->rx_packets += prev_stats->rx_packets;
13437 	stats->tx_packets += prev_stats->tx_packets;
13438 	stats->rx_bytes += prev_stats->rx_bytes;
13439 	stats->tx_bytes += prev_stats->tx_bytes;
13440 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
13441 	stats->multicast += prev_stats->multicast;
13442 	stats->rx_dropped += prev_stats->rx_dropped;
13443 	stats->tx_dropped += prev_stats->tx_dropped;
13444 }
13445 
13446 static void
13447 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
13448 {
13449 	struct bnxt *bp = netdev_priv(dev);
13450 
13451 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
13452 	/* Make sure bnxt_close_nic() sees that we are reading stats before
13453 	 * we check the BNXT_STATE_OPEN flag.
13454 	 */
13455 	smp_mb__after_atomic();
13456 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13457 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13458 		*stats = bp->net_stats_prev;
13459 		return;
13460 	}
13461 
13462 	bnxt_get_ring_stats(bp, stats);
13463 	bnxt_add_prev_stats(bp, stats);
13464 
13465 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
13466 		u64 *rx = bp->port_stats.sw_stats;
13467 		u64 *tx = bp->port_stats.sw_stats +
13468 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
13469 
13470 		stats->rx_crc_errors =
13471 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
13472 		stats->rx_frame_errors =
13473 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
13474 		stats->rx_length_errors =
13475 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
13476 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
13477 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
13478 		stats->rx_errors =
13479 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
13480 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
13481 		stats->collisions =
13482 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
13483 		stats->tx_fifo_errors =
13484 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
13485 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
13486 	}
13487 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13488 }
13489 
13490 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
13491 					struct bnxt_total_ring_err_stats *stats,
13492 					struct bnxt_cp_ring_info *cpr)
13493 {
13494 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
13495 	u64 *hw_stats = cpr->stats.sw_stats;
13496 
13497 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
13498 	stats->rx_total_resets += sw_stats->rx.rx_resets;
13499 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
13500 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
13501 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
13502 	stats->rx_total_ring_discards +=
13503 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
13504 	stats->rx_total_hw_gro_packets += sw_stats->rx.rx_hw_gro_packets;
13505 	stats->rx_total_hw_gro_wire_packets += sw_stats->rx.rx_hw_gro_wire_packets;
13506 	stats->tx_total_resets += sw_stats->tx.tx_resets;
13507 	stats->tx_total_ring_discards +=
13508 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
13509 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
13510 }
13511 
13512 void bnxt_get_ring_err_stats(struct bnxt *bp,
13513 			     struct bnxt_total_ring_err_stats *stats)
13514 {
13515 	int i;
13516 
13517 	for (i = 0; i < bp->cp_nr_rings; i++)
13518 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
13519 }
13520 
13521 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
13522 {
13523 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13524 	struct net_device *dev = bp->dev;
13525 	struct netdev_hw_addr *ha;
13526 	u8 *haddr;
13527 	int mc_count = 0;
13528 	bool update = false;
13529 	int off = 0;
13530 
13531 	netdev_for_each_mc_addr(ha, dev) {
13532 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
13533 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13534 			vnic->mc_list_count = 0;
13535 			return false;
13536 		}
13537 		haddr = ha->addr;
13538 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
13539 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
13540 			update = true;
13541 		}
13542 		off += ETH_ALEN;
13543 		mc_count++;
13544 	}
13545 	if (mc_count)
13546 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13547 
13548 	if (mc_count != vnic->mc_list_count) {
13549 		vnic->mc_list_count = mc_count;
13550 		update = true;
13551 	}
13552 	return update;
13553 }
13554 
13555 static bool bnxt_uc_list_updated(struct bnxt *bp)
13556 {
13557 	struct net_device *dev = bp->dev;
13558 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13559 	struct netdev_hw_addr *ha;
13560 	int off = 0;
13561 
13562 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
13563 		return true;
13564 
13565 	netdev_for_each_uc_addr(ha, dev) {
13566 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
13567 			return true;
13568 
13569 		off += ETH_ALEN;
13570 	}
13571 	return false;
13572 }
13573 
13574 static void bnxt_set_rx_mode(struct net_device *dev)
13575 {
13576 	struct bnxt *bp = netdev_priv(dev);
13577 	struct bnxt_vnic_info *vnic;
13578 	bool mc_update = false;
13579 	bool uc_update;
13580 	u32 mask;
13581 
13582 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13583 		return;
13584 
13585 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13586 	mask = vnic->rx_mask;
13587 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13588 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13589 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13590 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13591 
13592 	if (dev->flags & IFF_PROMISC)
13593 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13594 
13595 	uc_update = bnxt_uc_list_updated(bp);
13596 
13597 	if (dev->flags & IFF_BROADCAST)
13598 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13599 	if (dev->flags & IFF_ALLMULTI) {
13600 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13601 		vnic->mc_list_count = 0;
13602 	} else if (dev->flags & IFF_MULTICAST) {
13603 		mc_update = bnxt_mc_list_updated(bp, &mask);
13604 	}
13605 
13606 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13607 		vnic->rx_mask = mask;
13608 
13609 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13610 	}
13611 }
13612 
13613 static int bnxt_cfg_rx_mode(struct bnxt *bp)
13614 {
13615 	struct net_device *dev = bp->dev;
13616 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13617 	struct netdev_hw_addr *ha;
13618 	int i, off = 0, rc;
13619 	bool uc_update;
13620 
13621 	netif_addr_lock_bh(dev);
13622 	uc_update = bnxt_uc_list_updated(bp);
13623 	netif_addr_unlock_bh(dev);
13624 
13625 	if (!uc_update)
13626 		goto skip_uc;
13627 
13628 	for (i = 1; i < vnic->uc_filter_count; i++) {
13629 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13630 
13631 		bnxt_hwrm_l2_filter_free(bp, fltr);
13632 		bnxt_del_l2_filter(bp, fltr);
13633 	}
13634 
13635 	vnic->uc_filter_count = 1;
13636 
13637 	netif_addr_lock_bh(dev);
13638 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
13639 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13640 	} else {
13641 		netdev_for_each_uc_addr(ha, dev) {
13642 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13643 			off += ETH_ALEN;
13644 			vnic->uc_filter_count++;
13645 		}
13646 	}
13647 	netif_addr_unlock_bh(dev);
13648 
13649 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13650 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13651 		if (rc) {
13652 			if (BNXT_VF(bp) && rc == -ENODEV) {
13653 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13654 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13655 				else
13656 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
13657 				rc = 0;
13658 			} else {
13659 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13660 			}
13661 			vnic->uc_filter_count = i;
13662 			return rc;
13663 		}
13664 	}
13665 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13666 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
13667 
13668 skip_uc:
13669 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13670 	    !bnxt_promisc_ok(bp))
13671 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13672 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13673 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13674 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13675 			    rc);
13676 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13677 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13678 		vnic->mc_list_count = 0;
13679 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13680 	}
13681 	if (rc)
13682 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13683 			   rc);
13684 
13685 	return rc;
13686 }
13687 
13688 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13689 {
13690 #ifdef CONFIG_BNXT_SRIOV
13691 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13692 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13693 
13694 		/* No minimum rings were provisioned by the PF.  Don't
13695 		 * reserve rings by default when device is down.
13696 		 */
13697 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13698 			return true;
13699 
13700 		if (!netif_running(bp->dev))
13701 			return false;
13702 	}
13703 #endif
13704 	return true;
13705 }
13706 
13707 /* If the chip and firmware supports RFS */
13708 static bool bnxt_rfs_supported(struct bnxt *bp)
13709 {
13710 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13711 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13712 			return true;
13713 		return false;
13714 	}
13715 	/* 212 firmware is broken for aRFS */
13716 	if (BNXT_FW_MAJ(bp) == 212)
13717 		return false;
13718 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13719 		return true;
13720 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13721 		return true;
13722 	return false;
13723 }
13724 
13725 /* If runtime conditions support RFS */
13726 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13727 {
13728 	struct bnxt_hw_rings hwr = {0};
13729 	int max_vnics, max_rss_ctxs;
13730 
13731 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13732 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13733 		return bnxt_rfs_supported(bp);
13734 
13735 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13736 		return false;
13737 
13738 	hwr.grp = bp->rx_nr_rings;
13739 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13740 	if (new_rss_ctx)
13741 		hwr.vnic++;
13742 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13743 	max_vnics = bnxt_get_max_func_vnics(bp);
13744 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13745 
13746 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13747 		if (bp->rx_nr_rings > 1)
13748 			netdev_warn(bp->dev,
13749 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13750 				    min(max_rss_ctxs - 1, max_vnics - 1));
13751 		return false;
13752 	}
13753 
13754 	if (!BNXT_NEW_RM(bp))
13755 		return true;
13756 
13757 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13758 	 * issue that will mess up the default VNIC if we reduce the
13759 	 * reservations.
13760 	 */
13761 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13762 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13763 		return true;
13764 
13765 	bnxt_hwrm_reserve_rings(bp, &hwr);
13766 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13767 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13768 		return true;
13769 
13770 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13771 	hwr.vnic = 1;
13772 	hwr.rss_ctx = 0;
13773 	bnxt_hwrm_reserve_rings(bp, &hwr);
13774 	return false;
13775 }
13776 
13777 static netdev_features_t bnxt_fix_features(struct net_device *dev,
13778 					   netdev_features_t features)
13779 {
13780 	struct bnxt *bp = netdev_priv(dev);
13781 	netdev_features_t vlan_features;
13782 
13783 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
13784 		features &= ~NETIF_F_NTUPLE;
13785 
13786 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
13787 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13788 
13789 	if (!(features & NETIF_F_GRO))
13790 		features &= ~NETIF_F_GRO_HW;
13791 
13792 	if (features & NETIF_F_GRO_HW)
13793 		features &= ~NETIF_F_LRO;
13794 
13795 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
13796 	 * turned on or off together.
13797 	 */
13798 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
13799 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
13800 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13801 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13802 		else if (vlan_features)
13803 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13804 	}
13805 #ifdef CONFIG_BNXT_SRIOV
13806 	if (BNXT_VF(bp) && bp->vf.vlan)
13807 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13808 #endif
13809 	return features;
13810 }
13811 
13812 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
13813 				bool link_re_init, u32 flags, bool update_tpa)
13814 {
13815 	bnxt_close_nic(bp, irq_re_init, link_re_init);
13816 	bp->flags = flags;
13817 	if (update_tpa)
13818 		bnxt_set_ring_params(bp);
13819 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
13820 }
13821 
13822 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
13823 {
13824 	bool update_tpa = false, update_ntuple = false;
13825 	struct bnxt *bp = netdev_priv(dev);
13826 	u32 flags = bp->flags;
13827 	u32 changes;
13828 	int rc = 0;
13829 	bool re_init = false;
13830 
13831 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
13832 	if (features & NETIF_F_GRO_HW)
13833 		flags |= BNXT_FLAG_GRO;
13834 	else if (features & NETIF_F_LRO)
13835 		flags |= BNXT_FLAG_LRO;
13836 
13837 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
13838 		flags &= ~BNXT_FLAG_TPA;
13839 
13840 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13841 		flags |= BNXT_FLAG_STRIP_VLAN;
13842 
13843 	if (features & NETIF_F_NTUPLE)
13844 		flags |= BNXT_FLAG_RFS;
13845 	else
13846 		bnxt_clear_usr_fltrs(bp, true);
13847 
13848 	changes = flags ^ bp->flags;
13849 	if (changes & BNXT_FLAG_TPA) {
13850 		update_tpa = true;
13851 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
13852 		    (flags & BNXT_FLAG_TPA) == 0 ||
13853 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13854 			re_init = true;
13855 	}
13856 
13857 	if (changes & ~BNXT_FLAG_TPA)
13858 		re_init = true;
13859 
13860 	if (changes & BNXT_FLAG_RFS)
13861 		update_ntuple = true;
13862 
13863 	if (flags != bp->flags) {
13864 		u32 old_flags = bp->flags;
13865 
13866 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13867 			bp->flags = flags;
13868 			if (update_tpa)
13869 				bnxt_set_ring_params(bp);
13870 			return rc;
13871 		}
13872 
13873 		if (update_ntuple)
13874 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
13875 
13876 		if (re_init)
13877 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
13878 
13879 		if (update_tpa) {
13880 			bp->flags = flags;
13881 			rc = bnxt_set_tpa(bp,
13882 					  (flags & BNXT_FLAG_TPA) ?
13883 					  true : false);
13884 			if (rc)
13885 				bp->flags = old_flags;
13886 		}
13887 	}
13888 	return rc;
13889 }
13890 
13891 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
13892 			      u8 **nextp)
13893 {
13894 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
13895 	int hdr_count = 0;
13896 	u8 *nexthdr;
13897 	int start;
13898 
13899 	/* Check that there are at most 2 IPv6 extension headers, no
13900 	 * fragment header, and each is <= 64 bytes.
13901 	 */
13902 	start = nw_off + sizeof(*ip6h);
13903 	nexthdr = &ip6h->nexthdr;
13904 	while (ipv6_ext_hdr(*nexthdr)) {
13905 		struct ipv6_opt_hdr *hp;
13906 		int hdrlen;
13907 
13908 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
13909 		    *nexthdr == NEXTHDR_FRAGMENT)
13910 			return false;
13911 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
13912 					  skb_headlen(skb), NULL);
13913 		if (!hp)
13914 			return false;
13915 		if (*nexthdr == NEXTHDR_AUTH)
13916 			hdrlen = ipv6_authlen(hp);
13917 		else
13918 			hdrlen = ipv6_optlen(hp);
13919 
13920 		if (hdrlen > 64)
13921 			return false;
13922 
13923 		hdr_count++;
13924 		nexthdr = &hp->nexthdr;
13925 		start += hdrlen;
13926 	}
13927 	if (nextp) {
13928 		/* Caller will check inner protocol */
13929 		if (skb->encapsulation) {
13930 			*nextp = nexthdr;
13931 			return true;
13932 		}
13933 		*nextp = NULL;
13934 	}
13935 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
13936 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
13937 }
13938 
13939 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
13940 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
13941 {
13942 	struct udphdr *uh = udp_hdr(skb);
13943 	__be16 udp_port = uh->dest;
13944 
13945 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
13946 	    udp_port != bp->vxlan_gpe_port)
13947 		return false;
13948 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
13949 		struct ethhdr *eh = inner_eth_hdr(skb);
13950 
13951 		switch (eh->h_proto) {
13952 		case htons(ETH_P_IP):
13953 			return true;
13954 		case htons(ETH_P_IPV6):
13955 			return bnxt_exthdr_check(bp, skb,
13956 						 skb_inner_network_offset(skb),
13957 						 NULL);
13958 		}
13959 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
13960 		return true;
13961 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
13962 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13963 					 NULL);
13964 	}
13965 	return false;
13966 }
13967 
13968 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
13969 {
13970 	switch (l4_proto) {
13971 	case IPPROTO_UDP:
13972 		return bnxt_udp_tunl_check(bp, skb);
13973 	case IPPROTO_IPIP:
13974 		return true;
13975 	case IPPROTO_GRE: {
13976 		switch (skb->inner_protocol) {
13977 		default:
13978 			return false;
13979 		case htons(ETH_P_IP):
13980 			return true;
13981 		case htons(ETH_P_IPV6):
13982 			fallthrough;
13983 		}
13984 	}
13985 	case IPPROTO_IPV6:
13986 		/* Check ext headers of inner ipv6 */
13987 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13988 					 NULL);
13989 	}
13990 	return false;
13991 }
13992 
13993 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
13994 					     struct net_device *dev,
13995 					     netdev_features_t features)
13996 {
13997 	struct bnxt *bp = netdev_priv(dev);
13998 	u8 *l4_proto;
13999 
14000 	features = vlan_features_check(skb, features);
14001 	switch (vlan_get_protocol(skb)) {
14002 	case htons(ETH_P_IP):
14003 		if (!skb->encapsulation)
14004 			return features;
14005 		l4_proto = &ip_hdr(skb)->protocol;
14006 		if (bnxt_tunl_check(bp, skb, *l4_proto))
14007 			return features;
14008 		break;
14009 	case htons(ETH_P_IPV6):
14010 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
14011 				       &l4_proto))
14012 			break;
14013 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
14014 			return features;
14015 		break;
14016 	}
14017 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
14018 }
14019 
14020 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
14021 			 u32 *reg_buf)
14022 {
14023 	struct hwrm_dbg_read_direct_output *resp;
14024 	struct hwrm_dbg_read_direct_input *req;
14025 	__le32 *dbg_reg_buf;
14026 	dma_addr_t mapping;
14027 	int rc, i;
14028 
14029 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
14030 	if (rc)
14031 		return rc;
14032 
14033 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
14034 					 &mapping);
14035 	if (!dbg_reg_buf) {
14036 		rc = -ENOMEM;
14037 		goto dbg_rd_reg_exit;
14038 	}
14039 
14040 	req->host_dest_addr = cpu_to_le64(mapping);
14041 
14042 	resp = hwrm_req_hold(bp, req);
14043 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
14044 	req->read_len32 = cpu_to_le32(num_words);
14045 
14046 	rc = hwrm_req_send(bp, req);
14047 	if (rc || resp->error_code) {
14048 		rc = -EIO;
14049 		goto dbg_rd_reg_exit;
14050 	}
14051 	for (i = 0; i < num_words; i++)
14052 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
14053 
14054 dbg_rd_reg_exit:
14055 	hwrm_req_drop(bp, req);
14056 	return rc;
14057 }
14058 
14059 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
14060 				       u32 ring_id, u32 *prod, u32 *cons)
14061 {
14062 	struct hwrm_dbg_ring_info_get_output *resp;
14063 	struct hwrm_dbg_ring_info_get_input *req;
14064 	int rc;
14065 
14066 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
14067 	if (rc)
14068 		return rc;
14069 
14070 	req->ring_type = ring_type;
14071 	req->fw_ring_id = cpu_to_le32(ring_id);
14072 	resp = hwrm_req_hold(bp, req);
14073 	rc = hwrm_req_send(bp, req);
14074 	if (!rc) {
14075 		*prod = le32_to_cpu(resp->producer_index);
14076 		*cons = le32_to_cpu(resp->consumer_index);
14077 	}
14078 	hwrm_req_drop(bp, req);
14079 	return rc;
14080 }
14081 
14082 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
14083 {
14084 	struct bnxt_tx_ring_info *txr;
14085 	int i = bnapi->index, j;
14086 
14087 	bnxt_for_each_napi_tx(j, bnapi, txr)
14088 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
14089 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
14090 			    txr->tx_cons);
14091 }
14092 
14093 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
14094 {
14095 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
14096 	int i = bnapi->index;
14097 
14098 	if (!rxr)
14099 		return;
14100 
14101 	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",
14102 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
14103 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
14104 		    rxr->rx_sw_agg_prod);
14105 }
14106 
14107 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
14108 {
14109 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring, *cpr2;
14110 	int i = bnapi->index, j;
14111 
14112 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
14113 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
14114 	for (j = 0; j < cpr->cp_ring_count; j++) {
14115 		cpr2 = &cpr->cp_ring_arr[j];
14116 		if (!cpr2->bnapi)
14117 			continue;
14118 		netdev_info(bnapi->bp->dev, "[%d.%d]: cp{fw_ring: %d raw_cons: %x}\n",
14119 			    i, j, cpr2->cp_ring_struct.fw_ring_id,
14120 			    cpr2->cp_raw_cons);
14121 	}
14122 }
14123 
14124 static void bnxt_dbg_dump_states(struct bnxt *bp)
14125 {
14126 	int i;
14127 	struct bnxt_napi *bnapi;
14128 
14129 	for (i = 0; i < bp->cp_nr_rings; i++) {
14130 		bnapi = bp->bnapi[i];
14131 		if (netif_msg_drv(bp)) {
14132 			bnxt_dump_tx_sw_state(bnapi);
14133 			bnxt_dump_rx_sw_state(bnapi);
14134 			bnxt_dump_cp_sw_state(bnapi);
14135 		}
14136 	}
14137 }
14138 
14139 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
14140 {
14141 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
14142 	struct hwrm_ring_reset_input *req;
14143 	struct bnxt_napi *bnapi = rxr->bnapi;
14144 	struct bnxt_cp_ring_info *cpr;
14145 	u16 cp_ring_id;
14146 	int rc;
14147 
14148 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
14149 	if (rc)
14150 		return rc;
14151 
14152 	cpr = &bnapi->cp_ring;
14153 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
14154 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
14155 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
14156 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
14157 	return hwrm_req_send_silent(bp, req);
14158 }
14159 
14160 static void bnxt_reset_task(struct bnxt *bp, bool silent)
14161 {
14162 	if (!silent)
14163 		bnxt_dbg_dump_states(bp);
14164 	if (netif_running(bp->dev)) {
14165 		bnxt_close_nic(bp, !silent, false);
14166 		bnxt_open_nic(bp, !silent, false);
14167 	}
14168 }
14169 
14170 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
14171 {
14172 	struct bnxt *bp = netdev_priv(dev);
14173 
14174 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
14175 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
14176 }
14177 
14178 static void bnxt_fw_health_check(struct bnxt *bp)
14179 {
14180 	struct bnxt_fw_health *fw_health = bp->fw_health;
14181 	struct pci_dev *pdev = bp->pdev;
14182 	u32 val;
14183 
14184 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14185 		return;
14186 
14187 	/* Make sure it is enabled before checking the tmr_counter. */
14188 	smp_rmb();
14189 	if (fw_health->tmr_counter) {
14190 		fw_health->tmr_counter--;
14191 		return;
14192 	}
14193 
14194 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14195 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
14196 		fw_health->arrests++;
14197 		goto fw_reset;
14198 	}
14199 
14200 	fw_health->last_fw_heartbeat = val;
14201 
14202 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14203 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
14204 		fw_health->discoveries++;
14205 		goto fw_reset;
14206 	}
14207 
14208 	fw_health->tmr_counter = fw_health->tmr_multiplier;
14209 	return;
14210 
14211 fw_reset:
14212 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
14213 }
14214 
14215 static void bnxt_timer(struct timer_list *t)
14216 {
14217 	struct bnxt *bp = timer_container_of(bp, t, timer);
14218 	struct net_device *dev = bp->dev;
14219 
14220 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
14221 		return;
14222 
14223 	if (atomic_read(&bp->intr_sem) != 0)
14224 		goto bnxt_restart_timer;
14225 
14226 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
14227 		bnxt_fw_health_check(bp);
14228 
14229 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
14230 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
14231 
14232 	if (bnxt_tc_flower_enabled(bp))
14233 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
14234 
14235 #ifdef CONFIG_RFS_ACCEL
14236 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
14237 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14238 #endif /*CONFIG_RFS_ACCEL*/
14239 
14240 	if (bp->link_info.phy_retry) {
14241 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
14242 			bp->link_info.phy_retry = false;
14243 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
14244 		} else {
14245 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
14246 		}
14247 	}
14248 
14249 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
14250 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
14251 
14252 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
14253 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
14254 
14255 bnxt_restart_timer:
14256 	mod_timer(&bp->timer, jiffies + bp->current_interval);
14257 }
14258 
14259 static void bnxt_lock_sp(struct bnxt *bp)
14260 {
14261 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
14262 	 * set.  If the device is being closed, bnxt_close() may be holding
14263 	 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear.
14264 	 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev
14265 	 * instance lock.
14266 	 */
14267 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14268 	netdev_lock(bp->dev);
14269 }
14270 
14271 static void bnxt_unlock_sp(struct bnxt *bp)
14272 {
14273 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14274 	netdev_unlock(bp->dev);
14275 }
14276 
14277 /* Only called from bnxt_sp_task() */
14278 static void bnxt_reset(struct bnxt *bp, bool silent)
14279 {
14280 	bnxt_lock_sp(bp);
14281 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
14282 		bnxt_reset_task(bp, silent);
14283 	bnxt_unlock_sp(bp);
14284 }
14285 
14286 /* Only called from bnxt_sp_task() */
14287 static void bnxt_rx_ring_reset(struct bnxt *bp)
14288 {
14289 	int i;
14290 
14291 	bnxt_lock_sp(bp);
14292 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14293 		bnxt_unlock_sp(bp);
14294 		return;
14295 	}
14296 	/* Disable and flush TPA before resetting the RX ring */
14297 	if (bp->flags & BNXT_FLAG_TPA)
14298 		bnxt_set_tpa(bp, false);
14299 	for (i = 0; i < bp->rx_nr_rings; i++) {
14300 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
14301 		struct bnxt_cp_ring_info *cpr;
14302 		int rc;
14303 
14304 		if (!rxr->bnapi->in_reset)
14305 			continue;
14306 
14307 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
14308 		if (rc) {
14309 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
14310 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
14311 			else
14312 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
14313 					    rc);
14314 			bnxt_reset_task(bp, true);
14315 			break;
14316 		}
14317 		bnxt_free_one_rx_ring_skbs(bp, rxr);
14318 		rxr->rx_prod = 0;
14319 		rxr->rx_agg_prod = 0;
14320 		rxr->rx_sw_agg_prod = 0;
14321 		rxr->rx_next_cons = 0;
14322 		rxr->bnapi->in_reset = false;
14323 		bnxt_alloc_one_rx_ring(bp, i);
14324 		cpr = &rxr->bnapi->cp_ring;
14325 		cpr->sw_stats->rx.rx_resets++;
14326 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
14327 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
14328 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
14329 	}
14330 	if (bp->flags & BNXT_FLAG_TPA)
14331 		bnxt_set_tpa(bp, true);
14332 	bnxt_unlock_sp(bp);
14333 }
14334 
14335 static void bnxt_fw_fatal_close(struct bnxt *bp)
14336 {
14337 	bnxt_tx_disable(bp);
14338 	bnxt_disable_napi(bp);
14339 	bnxt_disable_int_sync(bp);
14340 	bnxt_free_irq(bp);
14341 	bnxt_clear_int_mode(bp);
14342 	pci_disable_device(bp->pdev);
14343 }
14344 
14345 static void bnxt_fw_reset_close(struct bnxt *bp)
14346 {
14347 	/* When firmware is in fatal state, quiesce device and disable
14348 	 * bus master to prevent any potential bad DMAs before freeing
14349 	 * kernel memory.
14350 	 */
14351 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
14352 		u16 val = 0;
14353 
14354 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14355 		if (val == 0xffff)
14356 			bp->fw_reset_min_dsecs = 0;
14357 		bnxt_fw_fatal_close(bp);
14358 	}
14359 	__bnxt_close_nic(bp, true, false);
14360 	bnxt_vf_reps_free(bp);
14361 	bnxt_clear_int_mode(bp);
14362 	bnxt_hwrm_func_drv_unrgtr(bp);
14363 	if (pci_is_enabled(bp->pdev))
14364 		pci_disable_device(bp->pdev);
14365 	bnxt_free_ctx_mem(bp, false);
14366 }
14367 
14368 static bool is_bnxt_fw_ok(struct bnxt *bp)
14369 {
14370 	struct bnxt_fw_health *fw_health = bp->fw_health;
14371 	bool no_heartbeat = false, has_reset = false;
14372 	u32 val;
14373 
14374 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14375 	if (val == fw_health->last_fw_heartbeat)
14376 		no_heartbeat = true;
14377 
14378 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14379 	if (val != fw_health->last_fw_reset_cnt)
14380 		has_reset = true;
14381 
14382 	if (!no_heartbeat && has_reset)
14383 		return true;
14384 
14385 	return false;
14386 }
14387 
14388 /* netdev instance lock is acquired before calling this function */
14389 static void bnxt_force_fw_reset(struct bnxt *bp)
14390 {
14391 	struct bnxt_fw_health *fw_health = bp->fw_health;
14392 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14393 	u32 wait_dsecs;
14394 
14395 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
14396 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14397 		return;
14398 
14399 	/* we have to serialize with bnxt_refclk_read()*/
14400 	if (ptp) {
14401 		unsigned long flags;
14402 
14403 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
14404 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14405 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14406 	} else {
14407 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14408 	}
14409 	bnxt_fw_reset_close(bp);
14410 	wait_dsecs = fw_health->master_func_wait_dsecs;
14411 	if (fw_health->primary) {
14412 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
14413 			wait_dsecs = 0;
14414 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14415 	} else {
14416 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
14417 		wait_dsecs = fw_health->normal_func_wait_dsecs;
14418 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14419 	}
14420 
14421 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
14422 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
14423 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14424 }
14425 
14426 void bnxt_fw_exception(struct bnxt *bp)
14427 {
14428 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
14429 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14430 	bnxt_ulp_stop(bp);
14431 	bnxt_lock_sp(bp);
14432 	bnxt_force_fw_reset(bp);
14433 	bnxt_unlock_sp(bp);
14434 }
14435 
14436 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
14437  * < 0 on error.
14438  */
14439 static int bnxt_get_registered_vfs(struct bnxt *bp)
14440 {
14441 #ifdef CONFIG_BNXT_SRIOV
14442 	int rc;
14443 
14444 	if (!BNXT_PF(bp))
14445 		return 0;
14446 
14447 	rc = bnxt_hwrm_func_qcfg(bp);
14448 	if (rc) {
14449 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
14450 		return rc;
14451 	}
14452 	if (bp->pf.registered_vfs)
14453 		return bp->pf.registered_vfs;
14454 	if (bp->sriov_cfg)
14455 		return 1;
14456 #endif
14457 	return 0;
14458 }
14459 
14460 void bnxt_fw_reset(struct bnxt *bp)
14461 {
14462 	bnxt_ulp_stop(bp);
14463 	bnxt_lock_sp(bp);
14464 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
14465 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14466 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14467 		int n = 0, tmo;
14468 
14469 		/* we have to serialize with bnxt_refclk_read()*/
14470 		if (ptp) {
14471 			unsigned long flags;
14472 
14473 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
14474 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14475 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14476 		} else {
14477 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14478 		}
14479 		if (bp->pf.active_vfs &&
14480 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
14481 			n = bnxt_get_registered_vfs(bp);
14482 		if (n < 0) {
14483 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
14484 				   n);
14485 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14486 			netif_close(bp->dev);
14487 			goto fw_reset_exit;
14488 		} else if (n > 0) {
14489 			u16 vf_tmo_dsecs = n * 10;
14490 
14491 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
14492 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
14493 			bp->fw_reset_state =
14494 				BNXT_FW_RESET_STATE_POLL_VF;
14495 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14496 			goto fw_reset_exit;
14497 		}
14498 		bnxt_fw_reset_close(bp);
14499 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14500 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14501 			tmo = HZ / 10;
14502 		} else {
14503 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14504 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14505 		}
14506 		bnxt_queue_fw_reset_work(bp, tmo);
14507 	}
14508 fw_reset_exit:
14509 	bnxt_unlock_sp(bp);
14510 }
14511 
14512 static void bnxt_chk_missed_irq(struct bnxt *bp)
14513 {
14514 	int i;
14515 
14516 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14517 		return;
14518 
14519 	for (i = 0; i < bp->cp_nr_rings; i++) {
14520 		struct bnxt_napi *bnapi = bp->bnapi[i];
14521 		struct bnxt_cp_ring_info *cpr;
14522 		u32 fw_ring_id;
14523 		int j;
14524 
14525 		if (!bnapi)
14526 			continue;
14527 
14528 		cpr = &bnapi->cp_ring;
14529 		for (j = 0; j < cpr->cp_ring_count; j++) {
14530 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
14531 			u32 val[2];
14532 
14533 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
14534 				continue;
14535 
14536 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
14537 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
14538 				continue;
14539 			}
14540 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
14541 			bnxt_dbg_hwrm_ring_info_get(bp,
14542 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
14543 				fw_ring_id, &val[0], &val[1]);
14544 			cpr->sw_stats->cmn.missed_irqs++;
14545 		}
14546 	}
14547 }
14548 
14549 static void bnxt_cfg_ntp_filters(struct bnxt *);
14550 
14551 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
14552 {
14553 	struct bnxt_link_info *link_info = &bp->link_info;
14554 
14555 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
14556 		link_info->autoneg = BNXT_AUTONEG_SPEED;
14557 		if (bp->hwrm_spec_code >= 0x10201) {
14558 			if (link_info->auto_pause_setting &
14559 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
14560 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14561 		} else {
14562 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14563 		}
14564 		bnxt_set_auto_speed(link_info);
14565 	} else {
14566 		bnxt_set_force_speed(link_info);
14567 		link_info->req_duplex = link_info->duplex_setting;
14568 	}
14569 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14570 		link_info->req_flow_ctrl =
14571 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14572 	else
14573 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14574 }
14575 
14576 static void bnxt_fw_echo_reply(struct bnxt *bp)
14577 {
14578 	struct bnxt_fw_health *fw_health = bp->fw_health;
14579 	struct hwrm_func_echo_response_input *req;
14580 	int rc;
14581 
14582 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14583 	if (rc)
14584 		return;
14585 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14586 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14587 	hwrm_req_send(bp, req);
14588 }
14589 
14590 static void bnxt_ulp_restart(struct bnxt *bp)
14591 {
14592 	bnxt_ulp_stop(bp);
14593 	bnxt_ulp_start(bp, 0);
14594 }
14595 
14596 static void bnxt_sp_task(struct work_struct *work)
14597 {
14598 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14599 
14600 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14601 	smp_mb__after_atomic();
14602 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14603 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14604 		return;
14605 	}
14606 
14607 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14608 		bnxt_ulp_restart(bp);
14609 		bnxt_reenable_sriov(bp);
14610 	}
14611 
14612 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
14613 		bnxt_cfg_rx_mode(bp);
14614 
14615 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14616 		bnxt_cfg_ntp_filters(bp);
14617 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14618 		bnxt_hwrm_exec_fwd_req(bp);
14619 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14620 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14621 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14622 		bnxt_hwrm_port_qstats(bp, 0);
14623 		bnxt_hwrm_port_qstats_ext(bp, 0);
14624 		bnxt_accumulate_all_stats(bp);
14625 	}
14626 
14627 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14628 		int rc;
14629 
14630 		mutex_lock(&bp->link_lock);
14631 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14632 				       &bp->sp_event))
14633 			bnxt_hwrm_phy_qcaps(bp);
14634 
14635 		rc = bnxt_update_link(bp, true);
14636 		if (rc)
14637 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14638 				   rc);
14639 
14640 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14641 				       &bp->sp_event))
14642 			bnxt_init_ethtool_link_settings(bp);
14643 		mutex_unlock(&bp->link_lock);
14644 	}
14645 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14646 		int rc;
14647 
14648 		mutex_lock(&bp->link_lock);
14649 		rc = bnxt_update_phy_setting(bp);
14650 		mutex_unlock(&bp->link_lock);
14651 		if (rc) {
14652 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14653 		} else {
14654 			bp->link_info.phy_retry = false;
14655 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14656 		}
14657 	}
14658 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14659 		mutex_lock(&bp->link_lock);
14660 		bnxt_get_port_module_status(bp);
14661 		mutex_unlock(&bp->link_lock);
14662 	}
14663 
14664 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14665 		bnxt_tc_flow_stats_work(bp);
14666 
14667 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14668 		bnxt_chk_missed_irq(bp);
14669 
14670 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14671 		bnxt_fw_echo_reply(bp);
14672 
14673 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14674 		bnxt_hwmon_notify_event(bp);
14675 
14676 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14677 	 * must be the last functions to be called before exiting.
14678 	 */
14679 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14680 		bnxt_reset(bp, false);
14681 
14682 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14683 		bnxt_reset(bp, true);
14684 
14685 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14686 		bnxt_rx_ring_reset(bp);
14687 
14688 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14689 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14690 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14691 			bnxt_devlink_health_fw_report(bp);
14692 		else
14693 			bnxt_fw_reset(bp);
14694 	}
14695 
14696 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14697 		if (!is_bnxt_fw_ok(bp))
14698 			bnxt_devlink_health_fw_report(bp);
14699 	}
14700 
14701 	smp_mb__before_atomic();
14702 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14703 }
14704 
14705 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14706 				int *max_cp);
14707 
14708 /* Under netdev instance lock */
14709 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14710 		     int tx_xdp)
14711 {
14712 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14713 	struct bnxt_hw_rings hwr = {0};
14714 	int rx_rings = rx;
14715 	int rc;
14716 
14717 	if (tcs)
14718 		tx_sets = tcs;
14719 
14720 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
14721 
14722 	if (max_rx < rx_rings)
14723 		return -ENOMEM;
14724 
14725 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14726 		rx_rings <<= 1;
14727 
14728 	hwr.rx = rx_rings;
14729 	hwr.tx = tx * tx_sets + tx_xdp;
14730 	if (max_tx < hwr.tx)
14731 		return -ENOMEM;
14732 
14733 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
14734 
14735 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
14736 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
14737 	if (max_cp < hwr.cp)
14738 		return -ENOMEM;
14739 	hwr.stat = hwr.cp;
14740 	if (BNXT_NEW_RM(bp)) {
14741 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
14742 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
14743 		hwr.grp = rx;
14744 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14745 	}
14746 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
14747 		hwr.cp_p5 = hwr.tx + rx;
14748 	rc = bnxt_hwrm_check_rings(bp, &hwr);
14749 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
14750 		if (!bnxt_ulp_registered(bp->edev)) {
14751 			hwr.cp += bnxt_get_ulp_msix_num(bp);
14752 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
14753 		}
14754 		if (hwr.cp > bp->total_irqs) {
14755 			int total_msix = bnxt_change_msix(bp, hwr.cp);
14756 
14757 			if (total_msix < hwr.cp) {
14758 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
14759 					    hwr.cp, total_msix);
14760 				rc = -ENOSPC;
14761 			}
14762 		}
14763 	}
14764 	return rc;
14765 }
14766 
14767 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
14768 {
14769 	if (bp->bar2) {
14770 		pci_iounmap(pdev, bp->bar2);
14771 		bp->bar2 = NULL;
14772 	}
14773 
14774 	if (bp->bar1) {
14775 		pci_iounmap(pdev, bp->bar1);
14776 		bp->bar1 = NULL;
14777 	}
14778 
14779 	if (bp->bar0) {
14780 		pci_iounmap(pdev, bp->bar0);
14781 		bp->bar0 = NULL;
14782 	}
14783 }
14784 
14785 static void bnxt_cleanup_pci(struct bnxt *bp)
14786 {
14787 	bnxt_unmap_bars(bp, bp->pdev);
14788 	pci_release_regions(bp->pdev);
14789 	if (pci_is_enabled(bp->pdev))
14790 		pci_disable_device(bp->pdev);
14791 }
14792 
14793 static void bnxt_init_dflt_coal(struct bnxt *bp)
14794 {
14795 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
14796 	struct bnxt_coal *coal;
14797 	u16 flags = 0;
14798 
14799 	if (coal_cap->cmpl_params &
14800 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
14801 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
14802 
14803 	/* Tick values in micro seconds.
14804 	 * 1 coal_buf x bufs_per_record = 1 completion record.
14805 	 */
14806 	coal = &bp->rx_coal;
14807 	coal->coal_ticks = 10;
14808 	coal->coal_bufs = 30;
14809 	coal->coal_ticks_irq = 1;
14810 	coal->coal_bufs_irq = 2;
14811 	coal->idle_thresh = 50;
14812 	coal->bufs_per_record = 2;
14813 	coal->budget = 64;		/* NAPI budget */
14814 	coal->flags = flags;
14815 
14816 	coal = &bp->tx_coal;
14817 	coal->coal_ticks = 28;
14818 	coal->coal_bufs = 30;
14819 	coal->coal_ticks_irq = 2;
14820 	coal->coal_bufs_irq = 2;
14821 	coal->bufs_per_record = 1;
14822 	coal->flags = flags;
14823 
14824 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
14825 }
14826 
14827 /* FW that pre-reserves 1 VNIC per function */
14828 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
14829 {
14830 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
14831 
14832 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14833 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
14834 		return true;
14835 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14836 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
14837 		return true;
14838 	return false;
14839 }
14840 
14841 static void bnxt_hwrm_pfcwd_qcaps(struct bnxt *bp)
14842 {
14843 	struct hwrm_queue_pfcwd_timeout_qcaps_output *resp;
14844 	struct hwrm_queue_pfcwd_timeout_qcaps_input *req;
14845 	int rc;
14846 
14847 	bp->max_pfcwd_tmo_ms = 0;
14848 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_PFCWD_TIMEOUT_QCAPS);
14849 	if (rc)
14850 		return;
14851 	resp = hwrm_req_hold(bp, req);
14852 	rc = hwrm_req_send_silent(bp, req);
14853 	if (!rc)
14854 		bp->max_pfcwd_tmo_ms = le16_to_cpu(resp->max_pfcwd_timeout);
14855 	hwrm_req_drop(bp, req);
14856 }
14857 
14858 static int bnxt_fw_init_one_p1(struct bnxt *bp)
14859 {
14860 	int rc;
14861 
14862 	bp->fw_cap = 0;
14863 	rc = bnxt_hwrm_ver_get(bp);
14864 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
14865 	 * so wait before continuing with recovery.
14866 	 */
14867 	if (rc)
14868 		msleep(100);
14869 	bnxt_try_map_fw_health_reg(bp);
14870 	if (rc) {
14871 		rc = bnxt_try_recover_fw(bp);
14872 		if (rc)
14873 			return rc;
14874 		rc = bnxt_hwrm_ver_get(bp);
14875 		if (rc)
14876 			return rc;
14877 	}
14878 
14879 	bnxt_nvm_cfg_ver_get(bp);
14880 
14881 	rc = bnxt_hwrm_func_reset(bp);
14882 	if (rc)
14883 		return -ENODEV;
14884 
14885 	bnxt_hwrm_fw_set_time(bp);
14886 	return 0;
14887 }
14888 
14889 static int bnxt_fw_init_one_p2(struct bnxt *bp)
14890 {
14891 	int rc;
14892 
14893 	/* Get the MAX capabilities for this function */
14894 	rc = bnxt_hwrm_func_qcaps(bp);
14895 	if (rc) {
14896 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
14897 			   rc);
14898 		return -ENODEV;
14899 	}
14900 
14901 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
14902 	if (rc)
14903 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
14904 			    rc);
14905 
14906 	if (bnxt_alloc_fw_health(bp)) {
14907 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
14908 	} else {
14909 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
14910 		if (rc)
14911 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
14912 				    rc);
14913 	}
14914 
14915 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
14916 	if (rc)
14917 		return -ENODEV;
14918 
14919 	rc = bnxt_alloc_crash_dump_mem(bp);
14920 	if (rc)
14921 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
14922 			    rc);
14923 	if (!rc) {
14924 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
14925 		if (rc) {
14926 			bnxt_free_crash_dump_mem(bp);
14927 			netdev_warn(bp->dev,
14928 				    "hwrm crash dump mem failure rc: %d\n", rc);
14929 		}
14930 	}
14931 
14932 	if (bnxt_fw_pre_resv_vnics(bp))
14933 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
14934 
14935 	bnxt_hwrm_pfcwd_qcaps(bp);
14936 	bnxt_hwrm_func_qcfg(bp);
14937 	bnxt_hwrm_vnic_qcaps(bp);
14938 	bnxt_hwrm_port_led_qcaps(bp);
14939 	bnxt_ethtool_init(bp);
14940 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
14941 		__bnxt_hwrm_ptp_qcfg(bp);
14942 	bnxt_dcb_init(bp);
14943 	bnxt_hwmon_init(bp);
14944 	return 0;
14945 }
14946 
14947 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
14948 {
14949 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
14950 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
14951 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
14952 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
14953 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
14954 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
14955 		bp->rss_hash_delta = bp->rss_hash_cfg;
14956 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
14957 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
14958 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
14959 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
14960 	}
14961 }
14962 
14963 static void bnxt_set_dflt_rfs(struct bnxt *bp)
14964 {
14965 	struct net_device *dev = bp->dev;
14966 
14967 	dev->hw_features &= ~NETIF_F_NTUPLE;
14968 	dev->features &= ~NETIF_F_NTUPLE;
14969 	bp->flags &= ~BNXT_FLAG_RFS;
14970 	if (bnxt_rfs_supported(bp)) {
14971 		dev->hw_features |= NETIF_F_NTUPLE;
14972 		if (bnxt_rfs_capable(bp, false)) {
14973 			bp->flags |= BNXT_FLAG_RFS;
14974 			dev->features |= NETIF_F_NTUPLE;
14975 		}
14976 	}
14977 }
14978 
14979 static void bnxt_fw_init_one_p3(struct bnxt *bp)
14980 {
14981 	struct pci_dev *pdev = bp->pdev;
14982 
14983 	bnxt_set_dflt_rss_hash_type(bp);
14984 	bnxt_set_dflt_rfs(bp);
14985 
14986 	bnxt_get_wol_settings(bp);
14987 	if (bp->flags & BNXT_FLAG_WOL_CAP)
14988 		device_set_wakeup_enable(&pdev->dev, bp->wol);
14989 	else
14990 		device_set_wakeup_capable(&pdev->dev, false);
14991 
14992 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
14993 	bnxt_hwrm_coal_params_qcaps(bp);
14994 }
14995 
14996 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
14997 
14998 int bnxt_fw_init_one(struct bnxt *bp)
14999 {
15000 	int rc;
15001 
15002 	rc = bnxt_fw_init_one_p1(bp);
15003 	if (rc) {
15004 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
15005 		return rc;
15006 	}
15007 	rc = bnxt_fw_init_one_p2(bp);
15008 	if (rc) {
15009 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
15010 		return rc;
15011 	}
15012 	rc = bnxt_probe_phy(bp, false);
15013 	if (rc)
15014 		return rc;
15015 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
15016 	if (rc)
15017 		return rc;
15018 
15019 	bnxt_fw_init_one_p3(bp);
15020 	return 0;
15021 }
15022 
15023 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
15024 {
15025 	struct bnxt_fw_health *fw_health = bp->fw_health;
15026 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
15027 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
15028 	u32 reg_type, reg_off, delay_msecs;
15029 
15030 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
15031 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
15032 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
15033 	switch (reg_type) {
15034 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
15035 		pci_write_config_dword(bp->pdev, reg_off, val);
15036 		break;
15037 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
15038 		writel(reg_off & BNXT_GRC_BASE_MASK,
15039 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
15040 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
15041 		fallthrough;
15042 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
15043 		writel(val, bp->bar0 + reg_off);
15044 		break;
15045 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
15046 		writel(val, bp->bar1 + reg_off);
15047 		break;
15048 	}
15049 	if (delay_msecs) {
15050 		pci_read_config_dword(bp->pdev, 0, &val);
15051 		msleep(delay_msecs);
15052 	}
15053 }
15054 
15055 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
15056 {
15057 	struct hwrm_func_qcfg_output *resp;
15058 	struct hwrm_func_qcfg_input *req;
15059 	bool result = true; /* firmware will enforce if unknown */
15060 
15061 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
15062 		return result;
15063 
15064 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
15065 		return result;
15066 
15067 	req->fid = cpu_to_le16(0xffff);
15068 	resp = hwrm_req_hold(bp, req);
15069 	if (!hwrm_req_send(bp, req))
15070 		result = !!(le16_to_cpu(resp->flags) &
15071 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
15072 	hwrm_req_drop(bp, req);
15073 	return result;
15074 }
15075 
15076 static void bnxt_reset_all(struct bnxt *bp)
15077 {
15078 	struct bnxt_fw_health *fw_health = bp->fw_health;
15079 	int i, rc;
15080 
15081 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15082 		bnxt_fw_reset_via_optee(bp);
15083 		bp->fw_reset_timestamp = jiffies;
15084 		return;
15085 	}
15086 
15087 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
15088 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
15089 			bnxt_fw_reset_writel(bp, i);
15090 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
15091 		struct hwrm_fw_reset_input *req;
15092 
15093 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
15094 		if (!rc) {
15095 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
15096 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
15097 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
15098 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
15099 			rc = hwrm_req_send(bp, req);
15100 		}
15101 		if (rc != -ENODEV)
15102 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
15103 	}
15104 	bp->fw_reset_timestamp = jiffies;
15105 }
15106 
15107 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
15108 {
15109 	return time_after(jiffies, bp->fw_reset_timestamp +
15110 			  (bp->fw_reset_max_dsecs * HZ / 10));
15111 }
15112 
15113 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
15114 {
15115 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15116 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
15117 		bnxt_dl_health_fw_status_update(bp, false);
15118 	bp->fw_reset_state = BNXT_FW_RESET_STATE_ABORT;
15119 	netif_close(bp->dev);
15120 }
15121 
15122 static void bnxt_fw_reset_task(struct work_struct *work)
15123 {
15124 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
15125 	int rc = 0;
15126 
15127 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
15128 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
15129 		return;
15130 	}
15131 
15132 	switch (bp->fw_reset_state) {
15133 	case BNXT_FW_RESET_STATE_POLL_VF: {
15134 		int n = bnxt_get_registered_vfs(bp);
15135 		int tmo;
15136 
15137 		if (n < 0) {
15138 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
15139 				   n, jiffies_to_msecs(jiffies -
15140 				   bp->fw_reset_timestamp));
15141 			goto fw_reset_abort;
15142 		} else if (n > 0) {
15143 			if (bnxt_fw_reset_timeout(bp)) {
15144 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15145 				bp->fw_reset_state = 0;
15146 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
15147 					   n);
15148 				goto ulp_start;
15149 			}
15150 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15151 			return;
15152 		}
15153 		bp->fw_reset_timestamp = jiffies;
15154 		netdev_lock(bp->dev);
15155 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
15156 			bnxt_fw_reset_abort(bp, rc);
15157 			netdev_unlock(bp->dev);
15158 			goto ulp_start;
15159 		}
15160 		bnxt_fw_reset_close(bp);
15161 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15162 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
15163 			tmo = HZ / 10;
15164 		} else {
15165 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15166 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
15167 		}
15168 		netdev_unlock(bp->dev);
15169 		bnxt_queue_fw_reset_work(bp, tmo);
15170 		return;
15171 	}
15172 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
15173 		u32 val;
15174 
15175 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15176 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
15177 		    !bnxt_fw_reset_timeout(bp)) {
15178 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15179 			return;
15180 		}
15181 
15182 		if (!bp->fw_health->primary) {
15183 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
15184 
15185 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15186 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
15187 			return;
15188 		}
15189 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
15190 	}
15191 		fallthrough;
15192 	case BNXT_FW_RESET_STATE_RESET_FW:
15193 		bnxt_reset_all(bp);
15194 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15195 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
15196 		return;
15197 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
15198 		bnxt_inv_fw_health_reg(bp);
15199 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
15200 		    !bp->fw_reset_min_dsecs) {
15201 			u16 val;
15202 
15203 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
15204 			if (val == 0xffff) {
15205 				if (bnxt_fw_reset_timeout(bp)) {
15206 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
15207 					rc = -ETIMEDOUT;
15208 					goto fw_reset_abort;
15209 				}
15210 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
15211 				return;
15212 			}
15213 		}
15214 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
15215 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
15216 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
15217 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
15218 			bnxt_dl_remote_reload(bp);
15219 		if (pci_enable_device(bp->pdev)) {
15220 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
15221 			rc = -ENODEV;
15222 			goto fw_reset_abort;
15223 		}
15224 		pci_set_master(bp->pdev);
15225 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
15226 		fallthrough;
15227 	case BNXT_FW_RESET_STATE_POLL_FW:
15228 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
15229 		rc = bnxt_hwrm_poll(bp);
15230 		if (rc) {
15231 			if (bnxt_fw_reset_timeout(bp)) {
15232 				netdev_err(bp->dev, "Firmware reset aborted\n");
15233 				goto fw_reset_abort_status;
15234 			}
15235 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15236 			return;
15237 		}
15238 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
15239 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
15240 		fallthrough;
15241 	case BNXT_FW_RESET_STATE_OPENING:
15242 		while (!netdev_trylock(bp->dev)) {
15243 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15244 			return;
15245 		}
15246 		rc = bnxt_open(bp->dev);
15247 		if (rc) {
15248 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
15249 			bnxt_fw_reset_abort(bp, rc);
15250 			netdev_unlock(bp->dev);
15251 			goto ulp_start;
15252 		}
15253 
15254 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
15255 		    bp->fw_health->enabled) {
15256 			bp->fw_health->last_fw_reset_cnt =
15257 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
15258 		}
15259 		bp->fw_reset_state = 0;
15260 		/* Make sure fw_reset_state is 0 before clearing the flag */
15261 		smp_mb__before_atomic();
15262 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15263 		bnxt_ptp_reapply_pps(bp);
15264 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
15265 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
15266 			bnxt_dl_health_fw_recovery_done(bp);
15267 			bnxt_dl_health_fw_status_update(bp, true);
15268 		}
15269 		netdev_unlock(bp->dev);
15270 		bnxt_ulp_start(bp, 0);
15271 		bnxt_reenable_sriov(bp);
15272 		netdev_lock(bp->dev);
15273 		bnxt_vf_reps_alloc(bp);
15274 		bnxt_vf_reps_open(bp);
15275 		netdev_unlock(bp->dev);
15276 		break;
15277 	}
15278 	return;
15279 
15280 fw_reset_abort_status:
15281 	if (bp->fw_health->status_reliable ||
15282 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
15283 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15284 
15285 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
15286 	}
15287 fw_reset_abort:
15288 	netdev_lock(bp->dev);
15289 	bnxt_fw_reset_abort(bp, rc);
15290 	netdev_unlock(bp->dev);
15291 ulp_start:
15292 	bnxt_ulp_start(bp, rc);
15293 }
15294 
15295 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
15296 {
15297 	int rc;
15298 	struct bnxt *bp = netdev_priv(dev);
15299 
15300 	SET_NETDEV_DEV(dev, &pdev->dev);
15301 
15302 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
15303 	rc = pci_enable_device(pdev);
15304 	if (rc) {
15305 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
15306 		goto init_err;
15307 	}
15308 
15309 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
15310 		dev_err(&pdev->dev,
15311 			"Cannot find PCI device base address, aborting\n");
15312 		rc = -ENODEV;
15313 		goto init_err_disable;
15314 	}
15315 
15316 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
15317 	if (rc) {
15318 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
15319 		goto init_err_disable;
15320 	}
15321 
15322 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
15323 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
15324 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
15325 		rc = -EIO;
15326 		goto init_err_release;
15327 	}
15328 
15329 	pci_set_master(pdev);
15330 
15331 	bp->dev = dev;
15332 	bp->pdev = pdev;
15333 
15334 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
15335 	 * determines the BAR size.
15336 	 */
15337 	bp->bar0 = pci_ioremap_bar(pdev, 0);
15338 	if (!bp->bar0) {
15339 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
15340 		rc = -ENOMEM;
15341 		goto init_err_release;
15342 	}
15343 
15344 	bp->bar2 = pci_ioremap_bar(pdev, 4);
15345 	if (!bp->bar2) {
15346 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
15347 		rc = -ENOMEM;
15348 		goto init_err_release;
15349 	}
15350 
15351 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
15352 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
15353 
15354 	spin_lock_init(&bp->ntp_fltr_lock);
15355 #if BITS_PER_LONG == 32
15356 	spin_lock_init(&bp->db_lock);
15357 #endif
15358 
15359 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
15360 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
15361 
15362 	timer_setup(&bp->timer, bnxt_timer, 0);
15363 	bp->current_interval = BNXT_TIMER_INTERVAL;
15364 
15365 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
15366 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
15367 
15368 	clear_bit(BNXT_STATE_OPEN, &bp->state);
15369 	return 0;
15370 
15371 init_err_release:
15372 	bnxt_unmap_bars(bp, pdev);
15373 	pci_release_regions(pdev);
15374 
15375 init_err_disable:
15376 	pci_disable_device(pdev);
15377 
15378 init_err:
15379 	return rc;
15380 }
15381 
15382 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
15383 {
15384 	struct sockaddr *addr = p;
15385 	struct bnxt *bp = netdev_priv(dev);
15386 	int rc = 0;
15387 
15388 	netdev_assert_locked(dev);
15389 
15390 	if (!is_valid_ether_addr(addr->sa_data))
15391 		return -EADDRNOTAVAIL;
15392 
15393 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
15394 		return 0;
15395 
15396 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
15397 	if (rc)
15398 		return rc;
15399 
15400 	eth_hw_addr_set(dev, addr->sa_data);
15401 	bnxt_clear_usr_fltrs(bp, true);
15402 	if (netif_running(dev)) {
15403 		bnxt_close_nic(bp, false, false);
15404 		rc = bnxt_open_nic(bp, false, false);
15405 	}
15406 
15407 	return rc;
15408 }
15409 
15410 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
15411 {
15412 	struct bnxt *bp = netdev_priv(dev);
15413 
15414 	netdev_assert_locked(dev);
15415 
15416 	if (netif_running(dev))
15417 		bnxt_close_nic(bp, true, false);
15418 
15419 	WRITE_ONCE(dev->mtu, new_mtu);
15420 
15421 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
15422 	 * program is attached.  We need to set the AGG rings settings and
15423 	 * rx_skb_func accordingly.
15424 	 */
15425 	if (READ_ONCE(bp->xdp_prog))
15426 		bnxt_set_rx_skb_mode(bp, true);
15427 
15428 	bnxt_set_ring_params(bp);
15429 
15430 	if (netif_running(dev))
15431 		return bnxt_open_nic(bp, true, false);
15432 
15433 	return 0;
15434 }
15435 
15436 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
15437 {
15438 	struct bnxt *bp = netdev_priv(dev);
15439 	bool sh = false;
15440 	int rc, tx_cp;
15441 
15442 	if (tc > bp->max_tc) {
15443 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
15444 			   tc, bp->max_tc);
15445 		return -EINVAL;
15446 	}
15447 
15448 	if (bp->num_tc == tc)
15449 		return 0;
15450 
15451 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
15452 		sh = true;
15453 
15454 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
15455 			      sh, tc, bp->tx_nr_rings_xdp);
15456 	if (rc)
15457 		return rc;
15458 
15459 	/* Needs to close the device and do hw resource re-allocations */
15460 	if (netif_running(bp->dev))
15461 		bnxt_close_nic(bp, true, false);
15462 
15463 	if (tc) {
15464 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
15465 		netdev_set_num_tc(dev, tc);
15466 		bp->num_tc = tc;
15467 	} else {
15468 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15469 		netdev_reset_tc(dev);
15470 		bp->num_tc = 0;
15471 	}
15472 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
15473 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
15474 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
15475 			       tx_cp + bp->rx_nr_rings;
15476 
15477 	if (netif_running(bp->dev))
15478 		return bnxt_open_nic(bp, true, false);
15479 
15480 	return 0;
15481 }
15482 
15483 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
15484 				  void *cb_priv)
15485 {
15486 	struct bnxt *bp = cb_priv;
15487 
15488 	if (!bnxt_tc_flower_enabled(bp) ||
15489 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
15490 		return -EOPNOTSUPP;
15491 
15492 	switch (type) {
15493 	case TC_SETUP_CLSFLOWER:
15494 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
15495 	default:
15496 		return -EOPNOTSUPP;
15497 	}
15498 }
15499 
15500 LIST_HEAD(bnxt_block_cb_list);
15501 
15502 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
15503 			 void *type_data)
15504 {
15505 	struct bnxt *bp = netdev_priv(dev);
15506 
15507 	switch (type) {
15508 	case TC_SETUP_BLOCK:
15509 		return flow_block_cb_setup_simple(type_data,
15510 						  &bnxt_block_cb_list,
15511 						  bnxt_setup_tc_block_cb,
15512 						  bp, bp, true);
15513 	case TC_SETUP_QDISC_MQPRIO: {
15514 		struct tc_mqprio_qopt *mqprio = type_data;
15515 
15516 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
15517 
15518 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
15519 	}
15520 	default:
15521 		return -EOPNOTSUPP;
15522 	}
15523 }
15524 
15525 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
15526 			    const struct sk_buff *skb)
15527 {
15528 	struct bnxt_vnic_info *vnic;
15529 
15530 	if (skb)
15531 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
15532 
15533 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
15534 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
15535 }
15536 
15537 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
15538 			   u32 idx)
15539 {
15540 	struct hlist_head *head;
15541 	int bit_id;
15542 
15543 	spin_lock_bh(&bp->ntp_fltr_lock);
15544 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
15545 	if (bit_id < 0) {
15546 		spin_unlock_bh(&bp->ntp_fltr_lock);
15547 		return -ENOMEM;
15548 	}
15549 
15550 	fltr->base.sw_id = (u16)bit_id;
15551 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
15552 	fltr->base.flags |= BNXT_ACT_RING_DST;
15553 	head = &bp->ntp_fltr_hash_tbl[idx];
15554 	hlist_add_head_rcu(&fltr->base.hash, head);
15555 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
15556 	bnxt_insert_usr_fltr(bp, &fltr->base);
15557 	bp->ntp_fltr_count++;
15558 	spin_unlock_bh(&bp->ntp_fltr_lock);
15559 	return 0;
15560 }
15561 
15562 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
15563 			    struct bnxt_ntuple_filter *f2)
15564 {
15565 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
15566 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
15567 	struct flow_keys *keys1 = &f1->fkeys;
15568 	struct flow_keys *keys2 = &f2->fkeys;
15569 
15570 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
15571 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
15572 		return false;
15573 
15574 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
15575 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
15576 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
15577 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
15578 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
15579 			return false;
15580 	} else {
15581 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
15582 				     &keys2->addrs.v6addrs.src) ||
15583 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
15584 				     &masks2->addrs.v6addrs.src) ||
15585 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15586 				     &keys2->addrs.v6addrs.dst) ||
15587 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
15588 				     &masks2->addrs.v6addrs.dst))
15589 			return false;
15590 	}
15591 
15592 	return keys1->ports.src == keys2->ports.src &&
15593 	       masks1->ports.src == masks2->ports.src &&
15594 	       keys1->ports.dst == keys2->ports.dst &&
15595 	       masks1->ports.dst == masks2->ports.dst &&
15596 	       keys1->control.flags == keys2->control.flags &&
15597 	       f1->l2_fltr == f2->l2_fltr;
15598 }
15599 
15600 struct bnxt_ntuple_filter *
15601 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15602 				struct bnxt_ntuple_filter *fltr, u32 idx)
15603 {
15604 	struct bnxt_ntuple_filter *f;
15605 	struct hlist_head *head;
15606 
15607 	head = &bp->ntp_fltr_hash_tbl[idx];
15608 	hlist_for_each_entry_rcu(f, head, base.hash) {
15609 		if (bnxt_fltr_match(f, fltr))
15610 			return f;
15611 	}
15612 	return NULL;
15613 }
15614 
15615 #ifdef CONFIG_RFS_ACCEL
15616 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15617 			      u16 rxq_index, u32 flow_id)
15618 {
15619 	struct bnxt *bp = netdev_priv(dev);
15620 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15621 	struct flow_keys *fkeys;
15622 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15623 	struct bnxt_l2_filter *l2_fltr;
15624 	int rc = 0, idx;
15625 	u32 flags;
15626 
15627 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15628 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15629 		atomic_inc(&l2_fltr->refcnt);
15630 	} else {
15631 		struct bnxt_l2_key key;
15632 
15633 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15634 		key.vlan = 0;
15635 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15636 		if (!l2_fltr)
15637 			return -EINVAL;
15638 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15639 			bnxt_del_l2_filter(bp, l2_fltr);
15640 			return -EINVAL;
15641 		}
15642 	}
15643 	new_fltr = kzalloc_obj(*new_fltr, GFP_ATOMIC);
15644 	if (!new_fltr) {
15645 		bnxt_del_l2_filter(bp, l2_fltr);
15646 		return -ENOMEM;
15647 	}
15648 
15649 	fkeys = &new_fltr->fkeys;
15650 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15651 		rc = -EPROTONOSUPPORT;
15652 		goto err_free;
15653 	}
15654 
15655 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15656 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15657 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15658 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15659 		rc = -EPROTONOSUPPORT;
15660 		goto err_free;
15661 	}
15662 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15663 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15664 		if (bp->hwrm_spec_code < 0x10601) {
15665 			rc = -EPROTONOSUPPORT;
15666 			goto err_free;
15667 		}
15668 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15669 	}
15670 	flags = fkeys->control.flags;
15671 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15672 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15673 		rc = -EPROTONOSUPPORT;
15674 		goto err_free;
15675 	}
15676 	new_fltr->l2_fltr = l2_fltr;
15677 
15678 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15679 	rcu_read_lock();
15680 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15681 	if (fltr) {
15682 		rc = fltr->base.sw_id;
15683 		rcu_read_unlock();
15684 		goto err_free;
15685 	}
15686 	rcu_read_unlock();
15687 
15688 	new_fltr->flow_id = flow_id;
15689 	new_fltr->base.rxq = rxq_index;
15690 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15691 	if (!rc) {
15692 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15693 		return new_fltr->base.sw_id;
15694 	}
15695 
15696 err_free:
15697 	bnxt_del_l2_filter(bp, l2_fltr);
15698 	kfree(new_fltr);
15699 	return rc;
15700 }
15701 #endif
15702 
15703 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
15704 {
15705 	spin_lock_bh(&bp->ntp_fltr_lock);
15706 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
15707 		spin_unlock_bh(&bp->ntp_fltr_lock);
15708 		return;
15709 	}
15710 	hlist_del_rcu(&fltr->base.hash);
15711 	bnxt_del_one_usr_fltr(bp, &fltr->base);
15712 	bp->ntp_fltr_count--;
15713 	spin_unlock_bh(&bp->ntp_fltr_lock);
15714 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
15715 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
15716 	kfree_rcu(fltr, base.rcu);
15717 }
15718 
15719 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
15720 {
15721 #ifdef CONFIG_RFS_ACCEL
15722 	int i;
15723 
15724 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
15725 		struct hlist_head *head;
15726 		struct hlist_node *tmp;
15727 		struct bnxt_ntuple_filter *fltr;
15728 		int rc;
15729 
15730 		head = &bp->ntp_fltr_hash_tbl[i];
15731 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
15732 			bool del = false;
15733 
15734 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
15735 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
15736 					continue;
15737 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
15738 							fltr->flow_id,
15739 							fltr->base.sw_id)) {
15740 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
15741 									 fltr);
15742 					del = true;
15743 				}
15744 			} else {
15745 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
15746 								       fltr);
15747 				if (rc)
15748 					del = true;
15749 				else
15750 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
15751 			}
15752 
15753 			if (del)
15754 				bnxt_del_ntp_filter(bp, fltr);
15755 		}
15756 	}
15757 #endif
15758 }
15759 
15760 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
15761 				    unsigned int entry, struct udp_tunnel_info *ti)
15762 {
15763 	struct bnxt *bp = netdev_priv(netdev);
15764 	unsigned int cmd;
15765 
15766 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15767 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
15768 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15769 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
15770 	else
15771 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
15772 
15773 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
15774 }
15775 
15776 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
15777 				      unsigned int entry, struct udp_tunnel_info *ti)
15778 {
15779 	struct bnxt *bp = netdev_priv(netdev);
15780 	unsigned int cmd;
15781 
15782 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15783 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
15784 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15785 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
15786 	else
15787 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
15788 
15789 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
15790 }
15791 
15792 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
15793 	.set_port	= bnxt_udp_tunnel_set_port,
15794 	.unset_port	= bnxt_udp_tunnel_unset_port,
15795 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15796 	.tables		= {
15797 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15798 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15799 	},
15800 }, bnxt_udp_tunnels_p7 = {
15801 	.set_port	= bnxt_udp_tunnel_set_port,
15802 	.unset_port	= bnxt_udp_tunnel_unset_port,
15803 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15804 	.tables		= {
15805 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15806 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15807 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
15808 	},
15809 };
15810 
15811 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
15812 			       struct net_device *dev, u32 filter_mask,
15813 			       int nlflags)
15814 {
15815 	struct bnxt *bp = netdev_priv(dev);
15816 
15817 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
15818 				       nlflags, filter_mask, NULL);
15819 }
15820 
15821 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
15822 			       u16 flags, struct netlink_ext_ack *extack)
15823 {
15824 	struct bnxt *bp = netdev_priv(dev);
15825 	struct nlattr *attr, *br_spec;
15826 	int rem, rc = 0;
15827 
15828 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
15829 		return -EOPNOTSUPP;
15830 
15831 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
15832 	if (!br_spec)
15833 		return -EINVAL;
15834 
15835 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
15836 		u16 mode;
15837 
15838 		mode = nla_get_u16(attr);
15839 		if (mode == bp->br_mode)
15840 			break;
15841 
15842 		rc = bnxt_hwrm_set_br_mode(bp, mode);
15843 		if (!rc)
15844 			bp->br_mode = mode;
15845 		break;
15846 	}
15847 	return rc;
15848 }
15849 
15850 int bnxt_get_port_parent_id(struct net_device *dev,
15851 			    struct netdev_phys_item_id *ppid)
15852 {
15853 	struct bnxt *bp = netdev_priv(dev);
15854 
15855 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
15856 		return -EOPNOTSUPP;
15857 
15858 	/* The PF and it's VF-reps only support the switchdev framework */
15859 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
15860 		return -EOPNOTSUPP;
15861 
15862 	ppid->id_len = sizeof(bp->dsn);
15863 	memcpy(ppid->id, bp->dsn, ppid->id_len);
15864 
15865 	return 0;
15866 }
15867 
15868 static const struct net_device_ops bnxt_netdev_ops = {
15869 	.ndo_open		= bnxt_open,
15870 	.ndo_start_xmit		= bnxt_start_xmit,
15871 	.ndo_stop		= bnxt_close,
15872 	.ndo_get_stats64	= bnxt_get_stats64,
15873 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
15874 	.ndo_eth_ioctl		= bnxt_ioctl,
15875 	.ndo_validate_addr	= eth_validate_addr,
15876 	.ndo_set_mac_address	= bnxt_change_mac_addr,
15877 	.ndo_change_mtu		= bnxt_change_mtu,
15878 	.ndo_fix_features	= bnxt_fix_features,
15879 	.ndo_set_features	= bnxt_set_features,
15880 	.ndo_features_check	= bnxt_features_check,
15881 	.ndo_tx_timeout		= bnxt_tx_timeout,
15882 #ifdef CONFIG_BNXT_SRIOV
15883 	.ndo_get_vf_config	= bnxt_get_vf_config,
15884 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
15885 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
15886 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
15887 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
15888 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
15889 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
15890 #endif
15891 	.ndo_setup_tc           = bnxt_setup_tc,
15892 #ifdef CONFIG_RFS_ACCEL
15893 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
15894 #endif
15895 	.ndo_bpf		= bnxt_xdp,
15896 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
15897 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
15898 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
15899 	.ndo_hwtstamp_get	= bnxt_hwtstamp_get,
15900 	.ndo_hwtstamp_set	= bnxt_hwtstamp_set,
15901 };
15902 
15903 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
15904 				    struct netdev_queue_stats_rx *stats)
15905 {
15906 	struct bnxt *bp = netdev_priv(dev);
15907 	struct bnxt_cp_ring_info *cpr;
15908 	u64 *sw;
15909 
15910 	if (!bp->bnapi)
15911 		return;
15912 
15913 	cpr = &bp->bnapi[i]->cp_ring;
15914 	sw = cpr->stats.sw_stats;
15915 
15916 	stats->packets = 0;
15917 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
15918 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
15919 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
15920 
15921 	stats->bytes = 0;
15922 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
15923 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
15924 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
15925 
15926 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
15927 	stats->hw_gro_packets = cpr->sw_stats->rx.rx_hw_gro_packets;
15928 	stats->hw_gro_wire_packets = cpr->sw_stats->rx.rx_hw_gro_wire_packets;
15929 }
15930 
15931 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
15932 				    struct netdev_queue_stats_tx *stats)
15933 {
15934 	struct bnxt *bp = netdev_priv(dev);
15935 	struct bnxt_napi *bnapi;
15936 	u64 *sw;
15937 
15938 	if (!bp->tx_ring)
15939 		return;
15940 
15941 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
15942 	sw = bnapi->cp_ring.stats.sw_stats;
15943 
15944 	stats->packets = 0;
15945 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
15946 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
15947 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
15948 
15949 	stats->bytes = 0;
15950 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
15951 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
15952 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
15953 }
15954 
15955 static void bnxt_get_base_stats(struct net_device *dev,
15956 				struct netdev_queue_stats_rx *rx,
15957 				struct netdev_queue_stats_tx *tx)
15958 {
15959 	struct bnxt *bp = netdev_priv(dev);
15960 
15961 	rx->packets = bp->net_stats_prev.rx_packets;
15962 	rx->bytes = bp->net_stats_prev.rx_bytes;
15963 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
15964 	rx->hw_gro_packets = bp->ring_err_stats_prev.rx_total_hw_gro_packets;
15965 	rx->hw_gro_wire_packets = bp->ring_err_stats_prev.rx_total_hw_gro_wire_packets;
15966 
15967 	tx->packets = bp->net_stats_prev.tx_packets;
15968 	tx->bytes = bp->net_stats_prev.tx_bytes;
15969 }
15970 
15971 static const struct netdev_stat_ops bnxt_stat_ops = {
15972 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
15973 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
15974 	.get_base_stats		= bnxt_get_base_stats,
15975 };
15976 
15977 static void bnxt_queue_default_qcfg(struct net_device *dev,
15978 				    struct netdev_queue_config *qcfg)
15979 {
15980 	qcfg->rx_page_size = BNXT_RX_PAGE_SIZE;
15981 }
15982 
15983 static int bnxt_validate_qcfg(struct net_device *dev,
15984 			      struct netdev_queue_config *qcfg,
15985 			      struct netlink_ext_ack *extack)
15986 {
15987 	struct bnxt *bp = netdev_priv(dev);
15988 
15989 	/* Older chips need MSS calc so rx_page_size is not supported */
15990 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
15991 	    qcfg->rx_page_size != BNXT_RX_PAGE_SIZE)
15992 		return -EINVAL;
15993 
15994 	if (!is_power_of_2(qcfg->rx_page_size))
15995 		return -ERANGE;
15996 
15997 	if (qcfg->rx_page_size < BNXT_RX_PAGE_SIZE ||
15998 	    qcfg->rx_page_size > BNXT_MAX_RX_PAGE_SIZE)
15999 		return -ERANGE;
16000 
16001 	return 0;
16002 }
16003 
16004 static int bnxt_queue_mem_alloc(struct net_device *dev,
16005 				struct netdev_queue_config *qcfg,
16006 				void *qmem, int idx)
16007 {
16008 	struct bnxt_rx_ring_info *rxr, *clone;
16009 	struct bnxt *bp = netdev_priv(dev);
16010 	struct bnxt_ring_struct *ring;
16011 	int rc;
16012 
16013 	if (!bp->rx_ring)
16014 		return -ENETDOWN;
16015 
16016 	rxr = &bp->rx_ring[idx];
16017 	clone = qmem;
16018 	memcpy(clone, rxr, sizeof(*rxr));
16019 	bnxt_init_rx_ring_struct(bp, clone);
16020 	bnxt_reset_rx_ring_struct(bp, clone);
16021 
16022 	clone->rx_prod = 0;
16023 	clone->rx_agg_prod = 0;
16024 	clone->rx_sw_agg_prod = 0;
16025 	clone->rx_next_cons = 0;
16026 	clone->need_head_pool = false;
16027 	clone->rx_page_size = qcfg->rx_page_size;
16028 
16029 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
16030 	if (rc)
16031 		return rc;
16032 
16033 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
16034 	if (rc < 0)
16035 		goto err_page_pool_destroy;
16036 
16037 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
16038 					MEM_TYPE_PAGE_POOL,
16039 					clone->page_pool);
16040 	if (rc)
16041 		goto err_rxq_info_unreg;
16042 
16043 	ring = &clone->rx_ring_struct;
16044 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
16045 	if (rc)
16046 		goto err_free_rx_ring;
16047 
16048 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
16049 		ring = &clone->rx_agg_ring_struct;
16050 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
16051 		if (rc)
16052 			goto err_free_rx_agg_ring;
16053 
16054 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
16055 		if (rc)
16056 			goto err_free_rx_agg_ring;
16057 	}
16058 
16059 	if (bp->flags & BNXT_FLAG_TPA) {
16060 		rc = bnxt_alloc_one_tpa_info(bp, clone);
16061 		if (rc)
16062 			goto err_free_tpa_info;
16063 	}
16064 
16065 	bnxt_init_one_rx_ring_rxbd(bp, clone);
16066 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
16067 
16068 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
16069 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16070 		bnxt_alloc_one_rx_ring_netmem(bp, clone, idx);
16071 	if (bp->flags & BNXT_FLAG_TPA)
16072 		bnxt_alloc_one_tpa_info_data(bp, clone);
16073 
16074 	return 0;
16075 
16076 err_free_tpa_info:
16077 	bnxt_free_one_tpa_info(bp, clone);
16078 err_free_rx_agg_ring:
16079 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
16080 err_free_rx_ring:
16081 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
16082 err_rxq_info_unreg:
16083 	xdp_rxq_info_unreg(&clone->xdp_rxq);
16084 err_page_pool_destroy:
16085 	page_pool_destroy(clone->page_pool);
16086 	page_pool_destroy(clone->head_pool);
16087 	clone->page_pool = NULL;
16088 	clone->head_pool = NULL;
16089 	return rc;
16090 }
16091 
16092 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
16093 {
16094 	struct bnxt_rx_ring_info *rxr = qmem;
16095 	struct bnxt *bp = netdev_priv(dev);
16096 	struct bnxt_ring_struct *ring;
16097 
16098 	bnxt_free_one_rx_ring_skbs(bp, rxr);
16099 	bnxt_free_one_tpa_info(bp, rxr);
16100 
16101 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
16102 
16103 	page_pool_destroy(rxr->page_pool);
16104 	page_pool_destroy(rxr->head_pool);
16105 	rxr->page_pool = NULL;
16106 	rxr->head_pool = NULL;
16107 
16108 	ring = &rxr->rx_ring_struct;
16109 	bnxt_free_ring(bp, &ring->ring_mem);
16110 
16111 	ring = &rxr->rx_agg_ring_struct;
16112 	bnxt_free_ring(bp, &ring->ring_mem);
16113 
16114 	kfree(rxr->rx_agg_bmap);
16115 	rxr->rx_agg_bmap = NULL;
16116 }
16117 
16118 static void bnxt_copy_rx_ring(struct bnxt *bp,
16119 			      struct bnxt_rx_ring_info *dst,
16120 			      struct bnxt_rx_ring_info *src)
16121 {
16122 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
16123 	struct bnxt_ring_struct *dst_ring, *src_ring;
16124 	int i;
16125 
16126 	dst_ring = &dst->rx_ring_struct;
16127 	dst_rmem = &dst_ring->ring_mem;
16128 	src_ring = &src->rx_ring_struct;
16129 	src_rmem = &src_ring->ring_mem;
16130 
16131 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
16132 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
16133 	WARN_ON(dst_rmem->flags != src_rmem->flags);
16134 	WARN_ON(dst_rmem->depth != src_rmem->depth);
16135 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
16136 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
16137 
16138 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
16139 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
16140 	*dst_rmem->vmem = *src_rmem->vmem;
16141 	for (i = 0; i < dst_rmem->nr_pages; i++) {
16142 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
16143 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
16144 	}
16145 
16146 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
16147 		return;
16148 
16149 	dst_ring = &dst->rx_agg_ring_struct;
16150 	dst_rmem = &dst_ring->ring_mem;
16151 	src_ring = &src->rx_agg_ring_struct;
16152 	src_rmem = &src_ring->ring_mem;
16153 
16154 	dst->rx_page_size = src->rx_page_size;
16155 
16156 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
16157 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
16158 	WARN_ON(dst_rmem->flags != src_rmem->flags);
16159 	WARN_ON(dst_rmem->depth != src_rmem->depth);
16160 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
16161 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
16162 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
16163 
16164 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
16165 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
16166 	*dst_rmem->vmem = *src_rmem->vmem;
16167 	for (i = 0; i < dst_rmem->nr_pages; i++) {
16168 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
16169 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
16170 	}
16171 
16172 	dst->rx_agg_bmap = src->rx_agg_bmap;
16173 }
16174 
16175 static int bnxt_queue_start(struct net_device *dev,
16176 			    struct netdev_queue_config *qcfg,
16177 			    void *qmem, int idx)
16178 {
16179 	struct bnxt *bp = netdev_priv(dev);
16180 	struct bnxt_rx_ring_info *rxr, *clone;
16181 	struct bnxt_cp_ring_info *cpr;
16182 	struct bnxt_vnic_info *vnic;
16183 	struct bnxt_napi *bnapi;
16184 	int i, rc;
16185 	u16 mru;
16186 
16187 	rxr = &bp->rx_ring[idx];
16188 	clone = qmem;
16189 
16190 	rxr->rx_prod = clone->rx_prod;
16191 	rxr->rx_agg_prod = clone->rx_agg_prod;
16192 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
16193 	rxr->rx_next_cons = clone->rx_next_cons;
16194 	rxr->rx_tpa = clone->rx_tpa;
16195 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
16196 	rxr->page_pool = clone->page_pool;
16197 	rxr->head_pool = clone->head_pool;
16198 	rxr->xdp_rxq = clone->xdp_rxq;
16199 	rxr->need_head_pool = clone->need_head_pool;
16200 
16201 	bnxt_copy_rx_ring(bp, rxr, clone);
16202 
16203 	bnapi = rxr->bnapi;
16204 	cpr = &bnapi->cp_ring;
16205 
16206 	/* All rings have been reserved and previously allocated.
16207 	 * Reallocating with the same parameters should never fail.
16208 	 */
16209 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
16210 	if (rc)
16211 		goto err_reset;
16212 
16213 	if (bp->tph_mode) {
16214 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
16215 		if (rc)
16216 			goto err_reset;
16217 	}
16218 
16219 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
16220 	if (rc)
16221 		goto err_reset;
16222 
16223 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
16224 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16225 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
16226 
16227 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
16228 		rc = bnxt_tx_queue_start(bp, idx);
16229 		if (rc)
16230 			goto err_reset;
16231 	}
16232 
16233 	bnxt_enable_rx_page_pool(rxr);
16234 	napi_enable_locked(&bnapi->napi);
16235 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16236 
16237 	mru = bp->dev->mtu + VLAN_ETH_HLEN;
16238 	for (i = 0; i < bp->nr_vnics; i++) {
16239 		vnic = &bp->vnic_info[i];
16240 
16241 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, idx);
16242 		if (rc)
16243 			return rc;
16244 	}
16245 	return bnxt_set_rss_ctx_vnic_mru(bp, mru, idx);
16246 
16247 err_reset:
16248 	netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n",
16249 		   rc);
16250 	napi_enable_locked(&bnapi->napi);
16251 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16252 	bnxt_reset_task(bp, true);
16253 	return rc;
16254 }
16255 
16256 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
16257 {
16258 	struct bnxt *bp = netdev_priv(dev);
16259 	struct bnxt_rx_ring_info *rxr;
16260 	struct bnxt_cp_ring_info *cpr;
16261 	struct bnxt_vnic_info *vnic;
16262 	struct bnxt_napi *bnapi;
16263 	int i;
16264 
16265 	for (i = 0; i < bp->nr_vnics; i++) {
16266 		vnic = &bp->vnic_info[i];
16267 
16268 		bnxt_set_vnic_mru_p5(bp, vnic, 0, idx);
16269 	}
16270 	bnxt_set_rss_ctx_vnic_mru(bp, 0, idx);
16271 	/* Make sure NAPI sees that the VNIC is disabled */
16272 	synchronize_net();
16273 	rxr = &bp->rx_ring[idx];
16274 	bnapi = rxr->bnapi;
16275 	cpr = &bnapi->cp_ring;
16276 	cancel_work_sync(&cpr->dim.work);
16277 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
16278 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
16279 	page_pool_disable_direct_recycling(rxr->page_pool);
16280 	if (bnxt_separate_head_pool(rxr))
16281 		page_pool_disable_direct_recycling(rxr->head_pool);
16282 
16283 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
16284 		bnxt_tx_queue_stop(bp, idx);
16285 
16286 	/* Disable NAPI now after freeing the rings because HWRM_RING_FREE
16287 	 * completion is handled in NAPI to guarantee no more DMA on that ring
16288 	 * after seeing the completion.
16289 	 */
16290 	napi_disable_locked(&bnapi->napi);
16291 
16292 	if (bp->tph_mode) {
16293 		bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr);
16294 		bnxt_clear_one_cp_ring(bp, rxr->rx_cpr);
16295 	}
16296 	bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
16297 
16298 	memcpy(qmem, rxr, sizeof(*rxr));
16299 	bnxt_init_rx_ring_struct(bp, qmem);
16300 
16301 	return 0;
16302 }
16303 
16304 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
16305 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
16306 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
16307 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
16308 	.ndo_queue_start	= bnxt_queue_start,
16309 	.ndo_queue_stop		= bnxt_queue_stop,
16310 	.ndo_default_qcfg	= bnxt_queue_default_qcfg,
16311 	.ndo_validate_qcfg	= bnxt_validate_qcfg,
16312 	.supported_params	= QCFG_RX_PAGE_SIZE,
16313 };
16314 
16315 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops_unsupp = {
16316 	.ndo_default_qcfg	= bnxt_queue_default_qcfg,
16317 };
16318 
16319 static void bnxt_remove_one(struct pci_dev *pdev)
16320 {
16321 	struct net_device *dev = pci_get_drvdata(pdev);
16322 	struct bnxt *bp = netdev_priv(dev);
16323 
16324 	if (BNXT_PF(bp))
16325 		__bnxt_sriov_disable(bp);
16326 
16327 	bnxt_rdma_aux_device_del(bp);
16328 
16329 	unregister_netdev(dev);
16330 	bnxt_ptp_clear(bp);
16331 
16332 	bnxt_rdma_aux_device_uninit(bp);
16333 
16334 	bnxt_free_l2_filters(bp, true);
16335 	bnxt_free_ntp_fltrs(bp, true);
16336 	WARN_ON(bp->num_rss_ctx);
16337 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16338 	/* Flush any pending tasks */
16339 	cancel_work_sync(&bp->sp_task);
16340 	cancel_delayed_work_sync(&bp->fw_reset_task);
16341 	bp->sp_event = 0;
16342 
16343 	bnxt_dl_fw_reporters_destroy(bp);
16344 	bnxt_dl_unregister(bp);
16345 	bnxt_shutdown_tc(bp);
16346 
16347 	bnxt_clear_int_mode(bp);
16348 	bnxt_hwrm_func_drv_unrgtr(bp);
16349 	bnxt_free_hwrm_resources(bp);
16350 	bnxt_hwmon_uninit(bp);
16351 	bnxt_ethtool_free(bp);
16352 	bnxt_dcb_free(bp);
16353 	kfree(bp->ptp_cfg);
16354 	bp->ptp_cfg = NULL;
16355 	kfree(bp->fw_health);
16356 	bp->fw_health = NULL;
16357 	bnxt_cleanup_pci(bp);
16358 	bnxt_free_ctx_mem(bp, true);
16359 	bnxt_free_crash_dump_mem(bp);
16360 	kfree(bp->rss_indir_tbl);
16361 	bp->rss_indir_tbl = NULL;
16362 	bnxt_free_port_stats(bp);
16363 	free_netdev(dev);
16364 }
16365 
16366 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
16367 {
16368 	int rc = 0;
16369 	struct bnxt_link_info *link_info = &bp->link_info;
16370 
16371 	bp->phy_flags = 0;
16372 	rc = bnxt_hwrm_phy_qcaps(bp);
16373 	if (rc) {
16374 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
16375 			   rc);
16376 		return rc;
16377 	}
16378 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
16379 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
16380 	else
16381 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
16382 
16383 	bp->mac_flags = 0;
16384 	bnxt_hwrm_mac_qcaps(bp);
16385 
16386 	if (!fw_dflt)
16387 		return 0;
16388 
16389 	mutex_lock(&bp->link_lock);
16390 	rc = bnxt_update_link(bp, false);
16391 	if (rc) {
16392 		mutex_unlock(&bp->link_lock);
16393 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
16394 			   rc);
16395 		return rc;
16396 	}
16397 
16398 	/* Older firmware does not have supported_auto_speeds, so assume
16399 	 * that all supported speeds can be autonegotiated.
16400 	 */
16401 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
16402 		link_info->support_auto_speeds = link_info->support_speeds;
16403 
16404 	bnxt_init_ethtool_link_settings(bp);
16405 	mutex_unlock(&bp->link_lock);
16406 	return 0;
16407 }
16408 
16409 static int bnxt_get_max_irq(struct pci_dev *pdev)
16410 {
16411 	u16 ctrl;
16412 
16413 	if (!pdev->msix_cap)
16414 		return 1;
16415 
16416 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
16417 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
16418 }
16419 
16420 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16421 				int *max_cp)
16422 {
16423 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
16424 	int max_ring_grps = 0, max_irq;
16425 
16426 	*max_tx = hw_resc->max_tx_rings;
16427 	*max_rx = hw_resc->max_rx_rings;
16428 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
16429 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
16430 			bnxt_get_ulp_msix_num_in_use(bp),
16431 			hw_resc->max_stat_ctxs -
16432 			bnxt_get_ulp_stat_ctxs_in_use(bp));
16433 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
16434 		*max_cp = min_t(int, *max_cp, max_irq);
16435 	max_ring_grps = hw_resc->max_hw_ring_grps;
16436 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
16437 		*max_cp -= 1;
16438 		*max_rx -= 2;
16439 	}
16440 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16441 		*max_rx >>= 1;
16442 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
16443 		int rc;
16444 
16445 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
16446 		if (rc) {
16447 			*max_rx = 0;
16448 			*max_tx = 0;
16449 		}
16450 		/* On P5 chips, max_cp output param should be available NQs */
16451 		*max_cp = max_irq;
16452 	}
16453 	*max_rx = min_t(int, *max_rx, max_ring_grps);
16454 }
16455 
16456 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
16457 {
16458 	int rx, tx, cp;
16459 
16460 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
16461 	*max_rx = rx;
16462 	*max_tx = tx;
16463 	if (!rx || !tx || !cp)
16464 		return -ENOMEM;
16465 
16466 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
16467 }
16468 
16469 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16470 			       bool shared)
16471 {
16472 	int rc;
16473 
16474 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16475 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
16476 		/* Not enough rings, try disabling agg rings. */
16477 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
16478 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16479 		if (rc) {
16480 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
16481 			bp->flags |= BNXT_FLAG_AGG_RINGS;
16482 			return rc;
16483 		}
16484 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
16485 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16486 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16487 		bnxt_set_ring_params(bp);
16488 	}
16489 
16490 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
16491 		int max_cp, max_stat, max_irq;
16492 
16493 		/* Reserve minimum resources for RoCE */
16494 		max_cp = bnxt_get_max_func_cp_rings(bp);
16495 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
16496 		max_irq = bnxt_get_max_func_irqs(bp);
16497 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
16498 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
16499 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
16500 			return 0;
16501 
16502 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
16503 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
16504 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
16505 		max_cp = min_t(int, max_cp, max_irq);
16506 		max_cp = min_t(int, max_cp, max_stat);
16507 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
16508 		if (rc)
16509 			rc = 0;
16510 	}
16511 	return rc;
16512 }
16513 
16514 /* In initial default shared ring setting, each shared ring must have a
16515  * RX/TX ring pair.
16516  */
16517 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
16518 {
16519 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
16520 	bp->rx_nr_rings = bp->cp_nr_rings;
16521 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
16522 	bp->tx_nr_rings = bnxt_tx_nr_rings(bp);
16523 }
16524 
16525 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
16526 {
16527 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
16528 	int avail_msix;
16529 
16530 	if (!bnxt_can_reserve_rings(bp))
16531 		return 0;
16532 
16533 	if (sh)
16534 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
16535 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
16536 	/* Reduce default rings on multi-port cards so that total default
16537 	 * rings do not exceed CPU count.
16538 	 */
16539 	if (bp->port_count > 1) {
16540 		int max_rings =
16541 			max_t(int, num_online_cpus() / bp->port_count, 1);
16542 
16543 		dflt_rings = min_t(int, dflt_rings, max_rings);
16544 	}
16545 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
16546 	if (rc)
16547 		return rc;
16548 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
16549 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
16550 	if (sh)
16551 		bnxt_trim_dflt_sh_rings(bp);
16552 	else
16553 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
16554 	bp->tx_nr_rings = bnxt_tx_nr_rings(bp);
16555 
16556 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
16557 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
16558 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
16559 
16560 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
16561 		bnxt_set_dflt_ulp_stat_ctxs(bp);
16562 	}
16563 
16564 	rc = __bnxt_reserve_rings(bp);
16565 	if (rc && rc != -ENODEV)
16566 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
16567 	bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp);
16568 	if (sh)
16569 		bnxt_trim_dflt_sh_rings(bp);
16570 
16571 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
16572 	if (bnxt_need_reserve_rings(bp)) {
16573 		rc = __bnxt_reserve_rings(bp);
16574 		if (rc && rc != -ENODEV)
16575 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
16576 		bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp);
16577 	}
16578 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
16579 		bp->rx_nr_rings++;
16580 		bp->cp_nr_rings++;
16581 	}
16582 	if (rc) {
16583 		bp->tx_nr_rings = 0;
16584 		bp->rx_nr_rings = 0;
16585 	}
16586 	return rc;
16587 }
16588 
16589 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
16590 {
16591 	int rc;
16592 
16593 	if (bp->tx_nr_rings)
16594 		return 0;
16595 
16596 	bnxt_ulp_irq_stop(bp);
16597 	bnxt_clear_int_mode(bp);
16598 	rc = bnxt_set_dflt_rings(bp, true);
16599 	if (rc) {
16600 		if (BNXT_VF(bp) && rc == -ENODEV)
16601 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16602 		else
16603 			netdev_err(bp->dev, "Not enough rings available.\n");
16604 		goto init_dflt_ring_err;
16605 	}
16606 	rc = bnxt_init_int_mode(bp);
16607 	if (rc)
16608 		goto init_dflt_ring_err;
16609 
16610 	bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp);
16611 
16612 	bnxt_set_dflt_rfs(bp);
16613 
16614 init_dflt_ring_err:
16615 	bnxt_ulp_irq_restart(bp, rc);
16616 	return rc;
16617 }
16618 
16619 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
16620 {
16621 	int rc;
16622 
16623 	netdev_ops_assert_locked(bp->dev);
16624 	bnxt_hwrm_func_qcaps(bp);
16625 
16626 	if (netif_running(bp->dev))
16627 		__bnxt_close_nic(bp, true, false);
16628 
16629 	bnxt_ulp_irq_stop(bp);
16630 	bnxt_clear_int_mode(bp);
16631 	rc = bnxt_init_int_mode(bp);
16632 	bnxt_ulp_irq_restart(bp, rc);
16633 
16634 	if (netif_running(bp->dev)) {
16635 		if (rc)
16636 			netif_close(bp->dev);
16637 		else
16638 			rc = bnxt_open_nic(bp, true, false);
16639 	}
16640 
16641 	return rc;
16642 }
16643 
16644 static int bnxt_init_mac_addr(struct bnxt *bp)
16645 {
16646 	int rc = 0;
16647 
16648 	if (BNXT_PF(bp)) {
16649 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
16650 	} else {
16651 #ifdef CONFIG_BNXT_SRIOV
16652 		struct bnxt_vf_info *vf = &bp->vf;
16653 		bool strict_approval = true;
16654 
16655 		if (is_valid_ether_addr(vf->mac_addr)) {
16656 			/* overwrite netdev dev_addr with admin VF MAC */
16657 			eth_hw_addr_set(bp->dev, vf->mac_addr);
16658 			/* Older PF driver or firmware may not approve this
16659 			 * correctly.
16660 			 */
16661 			strict_approval = false;
16662 		} else {
16663 			eth_hw_addr_random(bp->dev);
16664 		}
16665 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
16666 #endif
16667 	}
16668 	return rc;
16669 }
16670 
16671 static void bnxt_vpd_read_info(struct bnxt *bp)
16672 {
16673 	struct pci_dev *pdev = bp->pdev;
16674 	unsigned int vpd_size, kw_len;
16675 	int pos, size;
16676 	u8 *vpd_data;
16677 
16678 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
16679 	if (IS_ERR(vpd_data)) {
16680 		pci_warn(pdev, "Unable to read VPD\n");
16681 		return;
16682 	}
16683 
16684 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16685 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
16686 	if (pos < 0)
16687 		goto read_sn;
16688 
16689 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16690 	memcpy(bp->board_partno, &vpd_data[pos], size);
16691 
16692 read_sn:
16693 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16694 					   PCI_VPD_RO_KEYWORD_SERIALNO,
16695 					   &kw_len);
16696 	if (pos < 0)
16697 		goto exit;
16698 
16699 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16700 	memcpy(bp->board_serialno, &vpd_data[pos], size);
16701 exit:
16702 	kfree(vpd_data);
16703 }
16704 
16705 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
16706 {
16707 	struct pci_dev *pdev = bp->pdev;
16708 	u64 qword;
16709 
16710 	qword = pci_get_dsn(pdev);
16711 	if (!qword) {
16712 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
16713 		return -EOPNOTSUPP;
16714 	}
16715 
16716 	put_unaligned_le64(qword, dsn);
16717 
16718 	bp->flags |= BNXT_FLAG_DSN_VALID;
16719 	return 0;
16720 }
16721 
16722 static int bnxt_map_db_bar(struct bnxt *bp)
16723 {
16724 	if (!bp->db_size)
16725 		return -ENODEV;
16726 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
16727 	if (!bp->bar1)
16728 		return -ENOMEM;
16729 	return 0;
16730 }
16731 
16732 void bnxt_print_device_info(struct bnxt *bp)
16733 {
16734 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
16735 		    board_info[bp->board_idx].name,
16736 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
16737 
16738 	pcie_print_link_status(bp->pdev);
16739 }
16740 
16741 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
16742 {
16743 	struct bnxt_hw_resc *hw_resc;
16744 	struct net_device *dev;
16745 	struct bnxt *bp;
16746 	int rc, max_irqs;
16747 
16748 	if (pci_is_bridge(pdev))
16749 		return -ENODEV;
16750 
16751 	if (!pdev->msix_cap) {
16752 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
16753 		return -ENODEV;
16754 	}
16755 
16756 	/* Clear any pending DMA transactions from crash kernel
16757 	 * while loading driver in capture kernel.
16758 	 */
16759 	if (is_kdump_kernel()) {
16760 		pci_clear_master(pdev);
16761 		pcie_flr(pdev);
16762 	}
16763 
16764 	max_irqs = bnxt_get_max_irq(pdev);
16765 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
16766 				 max_irqs);
16767 	if (!dev)
16768 		return -ENOMEM;
16769 
16770 	bp = netdev_priv(dev);
16771 	bp->board_idx = ent->driver_data;
16772 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
16773 	bnxt_set_max_func_irqs(bp, max_irqs);
16774 
16775 	if (bnxt_vf_pciid(bp->board_idx))
16776 		bp->flags |= BNXT_FLAG_VF;
16777 
16778 	/* No devlink port registration in case of a VF */
16779 	if (BNXT_PF(bp))
16780 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
16781 
16782 	rc = bnxt_init_board(pdev, dev);
16783 	if (rc < 0)
16784 		goto init_err_free;
16785 
16786 	dev->netdev_ops = &bnxt_netdev_ops;
16787 	dev->stat_ops = &bnxt_stat_ops;
16788 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
16789 	dev->ethtool_ops = &bnxt_ethtool_ops;
16790 	pci_set_drvdata(pdev, dev);
16791 
16792 	rc = bnxt_alloc_hwrm_resources(bp);
16793 	if (rc)
16794 		goto init_err_pci_clean;
16795 
16796 	mutex_init(&bp->hwrm_cmd_lock);
16797 	mutex_init(&bp->link_lock);
16798 
16799 	rc = bnxt_fw_init_one_p1(bp);
16800 	if (rc)
16801 		goto init_err_pci_clean;
16802 
16803 	if (BNXT_PF(bp))
16804 		bnxt_vpd_read_info(bp);
16805 
16806 	if (BNXT_CHIP_P5_PLUS(bp)) {
16807 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
16808 		if (BNXT_CHIP_P7(bp))
16809 			bp->flags |= BNXT_FLAG_CHIP_P7;
16810 	}
16811 
16812 	rc = bnxt_alloc_rss_indir_tbl(bp);
16813 	if (rc)
16814 		goto init_err_pci_clean;
16815 
16816 	rc = bnxt_fw_init_one_p2(bp);
16817 	if (rc)
16818 		goto init_err_pci_clean;
16819 
16820 	rc = bnxt_map_db_bar(bp);
16821 	if (rc) {
16822 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
16823 			rc);
16824 		goto init_err_pci_clean;
16825 	}
16826 
16827 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16828 			   NETIF_F_TSO | NETIF_F_TSO6 |
16829 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16830 			   NETIF_F_GSO_IPXIP4 |
16831 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16832 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
16833 			   NETIF_F_RXCSUM | NETIF_F_GRO;
16834 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16835 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
16836 
16837 	if (BNXT_SUPPORTS_TPA(bp))
16838 		dev->hw_features |= NETIF_F_LRO;
16839 
16840 	dev->hw_enc_features =
16841 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16842 			NETIF_F_TSO | NETIF_F_TSO6 |
16843 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16844 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16845 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
16846 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16847 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
16848 	if (bp->flags & BNXT_FLAG_CHIP_P7)
16849 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
16850 	else
16851 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
16852 
16853 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
16854 				    NETIF_F_GSO_GRE_CSUM;
16855 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
16856 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
16857 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
16858 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
16859 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
16860 	if (BNXT_SUPPORTS_TPA(bp))
16861 		dev->hw_features |= NETIF_F_GRO_HW;
16862 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
16863 	if (dev->features & NETIF_F_GRO_HW)
16864 		dev->features &= ~NETIF_F_LRO;
16865 	dev->priv_flags |= IFF_UNICAST_FLT;
16866 
16867 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
16868 	if (bp->tso_max_segs)
16869 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
16870 
16871 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
16872 			    NETDEV_XDP_ACT_RX_SG;
16873 
16874 #ifdef CONFIG_BNXT_SRIOV
16875 	init_waitqueue_head(&bp->sriov_cfg_wait);
16876 #endif
16877 	if (BNXT_SUPPORTS_TPA(bp)) {
16878 		bp->gro_func = bnxt_gro_func_5730x;
16879 		if (BNXT_CHIP_P4(bp))
16880 			bp->gro_func = bnxt_gro_func_5731x;
16881 		else if (BNXT_CHIP_P5_PLUS(bp))
16882 			bp->gro_func = bnxt_gro_func_5750x;
16883 	}
16884 	if (!BNXT_CHIP_P4_PLUS(bp))
16885 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
16886 
16887 	rc = bnxt_init_mac_addr(bp);
16888 	if (rc) {
16889 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
16890 		rc = -EADDRNOTAVAIL;
16891 		goto init_err_pci_clean;
16892 	}
16893 
16894 	if (BNXT_PF(bp)) {
16895 		/* Read the adapter's DSN to use as the eswitch switch_id */
16896 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
16897 	}
16898 
16899 	/* MTU range: 60 - FW defined max */
16900 	dev->min_mtu = ETH_ZLEN;
16901 	dev->max_mtu = bp->max_mtu;
16902 
16903 	rc = bnxt_probe_phy(bp, true);
16904 	if (rc)
16905 		goto init_err_pci_clean;
16906 
16907 	hw_resc = &bp->hw_resc;
16908 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
16909 		       BNXT_L2_FLTR_MAX_FLTR;
16910 	/* Older firmware may not report these filters properly */
16911 	if (bp->max_fltr < BNXT_MAX_FLTR)
16912 		bp->max_fltr = BNXT_MAX_FLTR;
16913 	bnxt_init_l2_fltr_tbl(bp);
16914 	__bnxt_set_rx_skb_mode(bp, false);
16915 	bnxt_set_tpa_flags(bp);
16916 	bnxt_init_ring_params(bp);
16917 	bnxt_set_ring_params(bp);
16918 	bnxt_rdma_aux_device_init(bp);
16919 	rc = bnxt_set_dflt_rings(bp, true);
16920 	if (rc) {
16921 		if (BNXT_VF(bp) && rc == -ENODEV) {
16922 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16923 		} else {
16924 			netdev_err(bp->dev, "Not enough rings available.\n");
16925 			rc = -ENOMEM;
16926 		}
16927 		goto init_err_pci_clean;
16928 	}
16929 
16930 	bnxt_fw_init_one_p3(bp);
16931 
16932 	bnxt_init_dflt_coal(bp);
16933 
16934 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
16935 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
16936 
16937 	rc = bnxt_init_int_mode(bp);
16938 	if (rc)
16939 		goto init_err_pci_clean;
16940 
16941 	/* No TC has been set yet and rings may have been trimmed due to
16942 	 * limited MSIX, so we re-initialize the TX rings per TC.
16943 	 */
16944 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16945 
16946 	if (BNXT_PF(bp)) {
16947 		if (!bnxt_pf_wq) {
16948 			bnxt_pf_wq =
16949 				create_singlethread_workqueue("bnxt_pf_wq");
16950 			if (!bnxt_pf_wq) {
16951 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
16952 				rc = -ENOMEM;
16953 				goto init_err_pci_clean;
16954 			}
16955 		}
16956 		rc = bnxt_init_tc(bp);
16957 		if (rc)
16958 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
16959 				   rc);
16960 	}
16961 
16962 	bnxt_inv_fw_health_reg(bp);
16963 	rc = bnxt_dl_register(bp);
16964 	if (rc)
16965 		goto init_err_dl;
16966 
16967 	INIT_LIST_HEAD(&bp->usr_fltr_list);
16968 
16969 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
16970 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
16971 
16972 	dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops_unsupp;
16973 	if (BNXT_SUPPORTS_QUEUE_API(bp))
16974 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
16975 	dev->netmem_tx = true;
16976 
16977 	rc = register_netdev(dev);
16978 	if (rc)
16979 		goto init_err_cleanup;
16980 
16981 	bnxt_dl_fw_reporters_create(bp);
16982 
16983 	bnxt_rdma_aux_device_add(bp);
16984 
16985 	bnxt_print_device_info(bp);
16986 
16987 	pci_save_state(pdev);
16988 
16989 	return 0;
16990 init_err_cleanup:
16991 	bnxt_rdma_aux_device_uninit(bp);
16992 	bnxt_dl_unregister(bp);
16993 init_err_dl:
16994 	bnxt_shutdown_tc(bp);
16995 	bnxt_clear_int_mode(bp);
16996 
16997 init_err_pci_clean:
16998 	bnxt_hwrm_func_drv_unrgtr(bp);
16999 	bnxt_ptp_clear(bp);
17000 	kfree(bp->ptp_cfg);
17001 	bp->ptp_cfg = NULL;
17002 	bnxt_free_hwrm_resources(bp);
17003 	bnxt_hwmon_uninit(bp);
17004 	bnxt_ethtool_free(bp);
17005 	kfree(bp->fw_health);
17006 	bp->fw_health = NULL;
17007 	bnxt_cleanup_pci(bp);
17008 	bnxt_free_ctx_mem(bp, true);
17009 	bnxt_free_crash_dump_mem(bp);
17010 	kfree(bp->rss_indir_tbl);
17011 	bp->rss_indir_tbl = NULL;
17012 
17013 init_err_free:
17014 	free_netdev(dev);
17015 	return rc;
17016 }
17017 
17018 static void bnxt_shutdown(struct pci_dev *pdev)
17019 {
17020 	struct net_device *dev = pci_get_drvdata(pdev);
17021 	struct bnxt *bp;
17022 
17023 	if (!dev)
17024 		return;
17025 
17026 	rtnl_lock();
17027 	netdev_lock(dev);
17028 	bp = netdev_priv(dev);
17029 	if (!bp)
17030 		goto shutdown_exit;
17031 
17032 	if (netif_running(dev))
17033 		netif_close(dev);
17034 
17035 	if (bnxt_hwrm_func_drv_unrgtr(bp)) {
17036 		pcie_flr(pdev);
17037 		goto shutdown_exit;
17038 	}
17039 	bnxt_ptp_clear(bp);
17040 	bnxt_clear_int_mode(bp);
17041 	pci_disable_device(pdev);
17042 
17043 	if (system_state == SYSTEM_POWER_OFF) {
17044 		pci_wake_from_d3(pdev, bp->wol);
17045 		pci_set_power_state(pdev, PCI_D3hot);
17046 	}
17047 
17048 shutdown_exit:
17049 	netdev_unlock(dev);
17050 	rtnl_unlock();
17051 }
17052 
17053 #ifdef CONFIG_PM_SLEEP
17054 static int bnxt_suspend(struct device *device)
17055 {
17056 	struct net_device *dev = dev_get_drvdata(device);
17057 	struct bnxt *bp = netdev_priv(dev);
17058 	int rc = 0;
17059 
17060 	bnxt_ulp_stop(bp);
17061 
17062 	netdev_lock(dev);
17063 	if (netif_running(dev)) {
17064 		netif_device_detach(dev);
17065 		rc = bnxt_close(dev);
17066 	}
17067 	bnxt_hwrm_func_drv_unrgtr(bp);
17068 	bnxt_ptp_clear(bp);
17069 	pci_disable_device(bp->pdev);
17070 	bnxt_free_ctx_mem(bp, false);
17071 	netdev_unlock(dev);
17072 	return rc;
17073 }
17074 
17075 static int bnxt_resume(struct device *device)
17076 {
17077 	struct net_device *dev = dev_get_drvdata(device);
17078 	struct bnxt *bp = netdev_priv(dev);
17079 	int rc = 0;
17080 
17081 	netdev_lock(dev);
17082 	rc = pci_enable_device(bp->pdev);
17083 	if (rc) {
17084 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
17085 			   rc);
17086 		goto resume_exit;
17087 	}
17088 	pci_set_master(bp->pdev);
17089 	if (bnxt_hwrm_ver_get(bp)) {
17090 		rc = -ENODEV;
17091 		goto resume_exit;
17092 	}
17093 	rc = bnxt_hwrm_func_reset(bp);
17094 	if (rc) {
17095 		rc = -EBUSY;
17096 		goto resume_exit;
17097 	}
17098 
17099 	rc = bnxt_hwrm_func_qcaps(bp);
17100 	if (rc)
17101 		goto resume_exit;
17102 
17103 	bnxt_clear_reservations(bp, true);
17104 
17105 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
17106 		rc = -ENODEV;
17107 		goto resume_exit;
17108 	}
17109 	if (bp->fw_crash_mem)
17110 		bnxt_hwrm_crash_dump_mem_cfg(bp);
17111 
17112 	if (bnxt_ptp_init(bp)) {
17113 		kfree(bp->ptp_cfg);
17114 		bp->ptp_cfg = NULL;
17115 	}
17116 	bnxt_get_wol_settings(bp);
17117 	if (netif_running(dev)) {
17118 		rc = bnxt_open(dev);
17119 		if (!rc)
17120 			netif_device_attach(dev);
17121 	}
17122 
17123 resume_exit:
17124 	netdev_unlock(bp->dev);
17125 	bnxt_ulp_start(bp, rc);
17126 	if (!rc)
17127 		bnxt_reenable_sriov(bp);
17128 	return rc;
17129 }
17130 
17131 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
17132 #define BNXT_PM_OPS (&bnxt_pm_ops)
17133 
17134 #else
17135 
17136 #define BNXT_PM_OPS NULL
17137 
17138 #endif /* CONFIG_PM_SLEEP */
17139 
17140 /**
17141  * bnxt_io_error_detected - called when PCI error is detected
17142  * @pdev: Pointer to PCI device
17143  * @state: The current pci connection state
17144  *
17145  * This function is called after a PCI bus error affecting
17146  * this device has been detected.
17147  */
17148 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
17149 					       pci_channel_state_t state)
17150 {
17151 	struct net_device *netdev = pci_get_drvdata(pdev);
17152 	struct bnxt *bp = netdev_priv(netdev);
17153 	bool abort = false;
17154 
17155 	netdev_info(netdev, "PCI I/O error detected\n");
17156 
17157 	bnxt_ulp_stop(bp);
17158 
17159 	netdev_lock(netdev);
17160 	netif_device_detach(netdev);
17161 
17162 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
17163 		netdev_err(bp->dev, "Firmware reset already in progress\n");
17164 		abort = true;
17165 	}
17166 
17167 	if (abort || state == pci_channel_io_perm_failure) {
17168 		netdev_unlock(netdev);
17169 		return PCI_ERS_RESULT_DISCONNECT;
17170 	}
17171 
17172 	/* Link is not reliable anymore if state is pci_channel_io_frozen
17173 	 * so we disable bus master to prevent any potential bad DMAs before
17174 	 * freeing kernel memory.
17175 	 */
17176 	if (state == pci_channel_io_frozen) {
17177 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
17178 		bnxt_fw_fatal_close(bp);
17179 	}
17180 
17181 	if (netif_running(netdev))
17182 		__bnxt_close_nic(bp, true, true);
17183 
17184 	if (pci_is_enabled(pdev))
17185 		pci_disable_device(pdev);
17186 	bnxt_free_ctx_mem(bp, false);
17187 	netdev_unlock(netdev);
17188 
17189 	/* Request a slot reset. */
17190 	return PCI_ERS_RESULT_NEED_RESET;
17191 }
17192 
17193 /**
17194  * bnxt_io_slot_reset - called after the pci bus has been reset.
17195  * @pdev: Pointer to PCI device
17196  *
17197  * Restart the card from scratch, as if from a cold-boot.
17198  * At this point, the card has experienced a hard reset,
17199  * followed by fixups by BIOS, and has its config space
17200  * set up identically to what it was at cold boot.
17201  */
17202 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
17203 {
17204 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
17205 	struct net_device *netdev = pci_get_drvdata(pdev);
17206 	struct bnxt *bp = netdev_priv(netdev);
17207 	int retry = 0;
17208 	int err = 0;
17209 	int off;
17210 
17211 	netdev_info(bp->dev, "PCI Slot Reset\n");
17212 
17213 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
17214 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
17215 		msleep(900);
17216 
17217 	netdev_lock(netdev);
17218 
17219 	if (pci_enable_device(pdev)) {
17220 		dev_err(&pdev->dev,
17221 			"Cannot re-enable PCI device after reset.\n");
17222 	} else {
17223 		pci_set_master(pdev);
17224 		/* Upon fatal error, our device internal logic that latches to
17225 		 * BAR value is getting reset and will restore only upon
17226 		 * rewriting the BARs.
17227 		 *
17228 		 * As pci_restore_state() does not re-write the BARs if the
17229 		 * value is same as saved value earlier, driver needs to
17230 		 * write the BARs to 0 to force restore, in case of fatal error.
17231 		 */
17232 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
17233 				       &bp->state)) {
17234 			for (off = PCI_BASE_ADDRESS_0;
17235 			     off <= PCI_BASE_ADDRESS_5; off += 4)
17236 				pci_write_config_dword(bp->pdev, off, 0);
17237 		}
17238 		pci_restore_state(pdev);
17239 		pci_save_state(pdev);
17240 
17241 		bnxt_inv_fw_health_reg(bp);
17242 		bnxt_try_map_fw_health_reg(bp);
17243 
17244 		/* In some PCIe AER scenarios, firmware may take up to
17245 		 * 10 seconds to become ready in the worst case.
17246 		 */
17247 		do {
17248 			err = bnxt_try_recover_fw(bp);
17249 			if (!err)
17250 				break;
17251 			retry++;
17252 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
17253 
17254 		if (err) {
17255 			dev_err(&pdev->dev, "Firmware not ready\n");
17256 			goto reset_exit;
17257 		}
17258 
17259 		err = bnxt_hwrm_func_reset(bp);
17260 		if (!err)
17261 			result = PCI_ERS_RESULT_RECOVERED;
17262 
17263 		/* IRQ will be initialized later in bnxt_io_resume */
17264 		bnxt_ulp_irq_stop(bp);
17265 		bnxt_clear_int_mode(bp);
17266 	}
17267 
17268 reset_exit:
17269 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
17270 	bnxt_clear_reservations(bp, true);
17271 	netdev_unlock(netdev);
17272 
17273 	return result;
17274 }
17275 
17276 /**
17277  * bnxt_io_resume - called when traffic can start flowing again.
17278  * @pdev: Pointer to PCI device
17279  *
17280  * This callback is called when the error recovery driver tells
17281  * us that its OK to resume normal operation.
17282  */
17283 static void bnxt_io_resume(struct pci_dev *pdev)
17284 {
17285 	struct net_device *netdev = pci_get_drvdata(pdev);
17286 	struct bnxt *bp = netdev_priv(netdev);
17287 	int err;
17288 
17289 	netdev_info(bp->dev, "PCI Slot Resume\n");
17290 	netdev_lock(netdev);
17291 
17292 	err = bnxt_hwrm_func_qcaps(bp);
17293 	if (!err) {
17294 		if (netif_running(netdev)) {
17295 			err = bnxt_open(netdev);
17296 		} else {
17297 			err = bnxt_reserve_rings(bp, true);
17298 			if (!err)
17299 				err = bnxt_init_int_mode(bp);
17300 		}
17301 	}
17302 
17303 	if (!err)
17304 		netif_device_attach(netdev);
17305 
17306 	netdev_unlock(netdev);
17307 	bnxt_ulp_start(bp, err);
17308 	if (!err)
17309 		bnxt_reenable_sriov(bp);
17310 }
17311 
17312 static const struct pci_error_handlers bnxt_err_handler = {
17313 	.error_detected	= bnxt_io_error_detected,
17314 	.slot_reset	= bnxt_io_slot_reset,
17315 	.resume		= bnxt_io_resume
17316 };
17317 
17318 static struct pci_driver bnxt_pci_driver = {
17319 	.name		= DRV_MODULE_NAME,
17320 	.id_table	= bnxt_pci_tbl,
17321 	.probe		= bnxt_init_one,
17322 	.remove		= bnxt_remove_one,
17323 	.shutdown	= bnxt_shutdown,
17324 	.driver.pm	= BNXT_PM_OPS,
17325 	.err_handler	= &bnxt_err_handler,
17326 #if defined(CONFIG_BNXT_SRIOV)
17327 	.sriov_configure = bnxt_sriov_configure,
17328 #endif
17329 };
17330 
17331 static int __init bnxt_init(void)
17332 {
17333 	int err;
17334 
17335 	bnxt_debug_init();
17336 	err = pci_register_driver(&bnxt_pci_driver);
17337 	if (err) {
17338 		bnxt_debug_exit();
17339 		return err;
17340 	}
17341 
17342 	return 0;
17343 }
17344 
17345 static void __exit bnxt_exit(void)
17346 {
17347 	pci_unregister_driver(&bnxt_pci_driver);
17348 	if (bnxt_pf_wq)
17349 		destroy_workqueue(bnxt_pf_wq);
17350 	bnxt_debug_exit();
17351 }
17352 
17353 module_init(bnxt_init);
17354 module_exit(bnxt_exit);
17355