xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision 32756b82fde8f6cc98b174fdcd02c2a61033e263)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  * Copyright (c) 2016-2019 Broadcom Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  */
10 
11 #include <linux/module.h>
12 
13 #include <linux/stringify.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/pci.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/bitops.h>
27 #include <linux/io.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/byteorder.h>
31 #include <asm/page.h>
32 #include <linux/time.h>
33 #include <linux/mii.h>
34 #include <linux/mdio.h>
35 #include <linux/if.h>
36 #include <linux/if_vlan.h>
37 #include <linux/if_bridge.h>
38 #include <linux/rtc.h>
39 #include <linux/bpf.h>
40 #include <net/gro.h>
41 #include <net/ip.h>
42 #include <net/tcp.h>
43 #include <net/udp.h>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
46 #include <net/udp_tunnel.h>
47 #include <linux/workqueue.h>
48 #include <linux/prefetch.h>
49 #include <linux/cache.h>
50 #include <linux/log2.h>
51 #include <linux/bitmap.h>
52 #include <linux/cpu_rmap.h>
53 #include <linux/cpumask.h>
54 #include <net/pkt_cls.h>
55 #include <net/page_pool/helpers.h>
56 #include <linux/align.h>
57 #include <net/netdev_lock.h>
58 #include <net/netdev_queues.h>
59 #include <net/netdev_rx_queue.h>
60 #include <linux/pci-tph.h>
61 #include <linux/bnxt/hsi.h>
62 #include <linux/bnxt/ulp.h>
63 
64 #include "bnxt.h"
65 #include "bnxt_hwrm.h"
66 #include "bnxt_sriov.h"
67 #include "bnxt_ethtool.h"
68 #include "bnxt_dcb.h"
69 #include "bnxt_xdp.h"
70 #include "bnxt_ptp.h"
71 #include "bnxt_vfr.h"
72 #include "bnxt_tc.h"
73 #include "bnxt_devlink.h"
74 #include "bnxt_debugfs.h"
75 #include "bnxt_coredump.h"
76 #include "bnxt_hwmon.h"
77 #include "bnxt_gso.h"
78 #include <net/tso.h>
79 
80 #define BNXT_TX_TIMEOUT		(5 * HZ)
81 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
82 				 NETIF_MSG_TX_ERR)
83 
84 MODULE_IMPORT_NS("NETDEV_INTERNAL");
85 MODULE_LICENSE("GPL");
86 MODULE_DESCRIPTION("Broadcom NetXtreme network driver");
87 
88 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
89 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
90 
91 #define BNXT_TX_PUSH_THRESH 164
92 
93 /* indexed by enum board_idx */
94 static const struct {
95 	char *name;
96 } board_info[] = {
97 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
98 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
99 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
100 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
101 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
102 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
103 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
104 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
105 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
106 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
108 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
109 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
110 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
111 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
112 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
113 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
114 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
115 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
116 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
117 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
118 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
119 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
120 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
121 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
122 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
123 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
124 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
125 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
126 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
127 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
128 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
129 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
130 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
131 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
132 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
133 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
134 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
135 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
136 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
137 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
138 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
139 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
140 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
141 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
142 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
143 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
144 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
145 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
146 	[NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" },
147 	[NETXTREME_E_P7_VF_HV] = { "Broadcom BCM5760X Virtual Function for Hyper-V" },
148 };
149 
150 static const struct pci_device_id bnxt_pci_tbl[] = {
151 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
152 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
153 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
155 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
159 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
160 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
164 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
165 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
166 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
167 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
168 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
169 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
170 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
171 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
172 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
173 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
174 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
175 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
176 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
178 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
179 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
180 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
181 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
182 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
183 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
184 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
185 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
187 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
188 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
189 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
190 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
191 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
192 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
193 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
194 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
195 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
196 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
197 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
198 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
199 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
200 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
201 #ifdef CONFIG_BNXT_SRIOV
202 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
203 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
204 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
205 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
206 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
207 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
208 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
209 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
210 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
211 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
212 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
214 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
215 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
216 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
217 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
218 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
219 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
220 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
221 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
222 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
223 	{ PCI_VDEVICE(BROADCOM, 0x181b), .driver_data = NETXTREME_E_P7_VF_HV },
224 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
225 #endif
226 	{ 0 }
227 };
228 
229 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
230 
231 static const u16 bnxt_vf_req_snif[] = {
232 	HWRM_FUNC_CFG,
233 	HWRM_FUNC_VF_CFG,
234 	HWRM_PORT_PHY_QCFG,
235 	HWRM_CFA_L2_FILTER_ALLOC,
236 };
237 
238 static const u16 bnxt_async_events_arr[] = {
239 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
240 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
241 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
242 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
243 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
244 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
245 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
246 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
247 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
248 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
249 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
250 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
251 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
252 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
253 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
254 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
255 	ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER,
256 };
257 
258 const u16 bnxt_bstore_to_trace[] = {
259 	[BNXT_CTX_SRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT_TRACE,
260 	[BNXT_CTX_SRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT2_TRACE,
261 	[BNXT_CTX_CRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT_TRACE,
262 	[BNXT_CTX_CRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT2_TRACE,
263 	[BNXT_CTX_RIGP0]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP0_TRACE,
264 	[BNXT_CTX_L2HWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_L2_HWRM_TRACE,
265 	[BNXT_CTX_REHWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ROCE_HWRM_TRACE,
266 	[BNXT_CTX_CA0]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA0_TRACE,
267 	[BNXT_CTX_CA1]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA1_TRACE,
268 	[BNXT_CTX_CA2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA2_TRACE,
269 	[BNXT_CTX_RIGP1]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP1_TRACE,
270 	[BNXT_CTX_KONG]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_AFM_KONG_HWRM_TRACE,
271 	[BNXT_CTX_QPC]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ERR_QPC_TRACE,
272 };
273 
274 static struct workqueue_struct *bnxt_pf_wq;
275 
276 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
277 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
278 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
279 
280 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
281 	.ports = {
282 		.src = 0,
283 		.dst = 0,
284 	},
285 	.addrs = {
286 		.v6addrs = {
287 			.src = BNXT_IPV6_MASK_NONE,
288 			.dst = BNXT_IPV6_MASK_NONE,
289 		},
290 	},
291 };
292 
293 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
294 	.ports = {
295 		.src = cpu_to_be16(0xffff),
296 		.dst = cpu_to_be16(0xffff),
297 	},
298 	.addrs = {
299 		.v6addrs = {
300 			.src = BNXT_IPV6_MASK_ALL,
301 			.dst = BNXT_IPV6_MASK_ALL,
302 		},
303 	},
304 };
305 
306 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
307 	.ports = {
308 		.src = cpu_to_be16(0xffff),
309 		.dst = cpu_to_be16(0xffff),
310 	},
311 	.addrs = {
312 		.v4addrs = {
313 			.src = cpu_to_be32(0xffffffff),
314 			.dst = cpu_to_be32(0xffffffff),
315 		},
316 	},
317 };
318 
319 static bool bnxt_vf_pciid(enum board_idx idx)
320 {
321 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
322 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
323 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
324 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF ||
325 		idx == NETXTREME_E_P7_VF_HV);
326 }
327 
328 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
329 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
330 
331 #define BNXT_DB_CQ(db, idx)						\
332 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
333 
334 #define BNXT_DB_NQ_P5(db, idx)						\
335 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
336 		    (db)->doorbell)
337 
338 #define BNXT_DB_NQ_P7(db, idx)						\
339 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
340 		    DB_RING_IDX(db, idx), (db)->doorbell)
341 
342 #define BNXT_DB_CQ_ARM(db, idx)						\
343 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
344 
345 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
346 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
347 		    DB_RING_IDX(db, idx), (db)->doorbell)
348 
349 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
350 {
351 	if (bp->flags & BNXT_FLAG_CHIP_P7)
352 		BNXT_DB_NQ_P7(db, idx);
353 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
354 		BNXT_DB_NQ_P5(db, idx);
355 	else
356 		BNXT_DB_CQ(db, idx);
357 }
358 
359 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
360 {
361 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
362 		BNXT_DB_NQ_ARM_P5(db, idx);
363 	else
364 		BNXT_DB_CQ_ARM(db, idx);
365 }
366 
367 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
368 {
369 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
370 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
371 			    DB_RING_IDX(db, idx), db->doorbell);
372 	else
373 		BNXT_DB_CQ(db, idx);
374 }
375 
376 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
377 {
378 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
379 		return;
380 
381 	if (BNXT_PF(bp))
382 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
383 	else
384 		schedule_delayed_work(&bp->fw_reset_task, delay);
385 }
386 
387 static void __bnxt_queue_sp_work(struct bnxt *bp)
388 {
389 	if (BNXT_PF(bp))
390 		queue_work(bnxt_pf_wq, &bp->sp_task);
391 	else
392 		schedule_work(&bp->sp_task);
393 }
394 
395 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
396 {
397 	set_bit(event, &bp->sp_event);
398 	__bnxt_queue_sp_work(bp);
399 }
400 
401 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
402 {
403 	if (!rxr->bnapi->in_reset) {
404 		rxr->bnapi->in_reset = true;
405 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
406 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
407 		else
408 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
409 		__bnxt_queue_sp_work(bp);
410 	}
411 	rxr->rx_next_cons = 0xffff;
412 }
413 
414 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
415 			  u16 curr)
416 {
417 	struct bnxt_napi *bnapi = txr->bnapi;
418 
419 	if (bnapi->tx_fault)
420 		return;
421 
422 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
423 		   txr->txq_index, txr->tx_hw_cons,
424 		   txr->tx_cons, txr->tx_prod, curr);
425 	WARN_ON_ONCE(1);
426 	bnapi->tx_fault = 1;
427 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
428 }
429 
430 const u16 bnxt_lhint_arr[] = {
431 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
432 	TX_BD_FLAGS_LHINT_512_TO_1023,
433 	TX_BD_FLAGS_LHINT_1024_TO_2047,
434 	TX_BD_FLAGS_LHINT_1024_TO_2047,
435 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
436 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
437 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
438 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
439 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
440 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
441 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
442 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
443 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
444 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
445 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
446 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
447 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
448 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
449 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
450 };
451 
452 u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
453 {
454 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
455 
456 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
457 		return 0;
458 
459 	return md_dst->u.port_info.port_id;
460 }
461 
462 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
463 			     u16 prod)
464 {
465 	/* If the most recent BD has its completion suppressed, unset the bit
466 	 * so that a completion is generated, otherwise nothing is left to
467 	 * clean the ring and wake the queue.
468 	 */
469 	if (txr->kick_txbd0) {
470 		txr->kick_txbd0->tx_bd_len_flags_type &=
471 			cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
472 		txr->kick_txbd0 = NULL;
473 	}
474 
475 	/* Sync BD data before updating doorbell */
476 	wmb();
477 	bnxt_db_write(bp, &txr->tx_db, prod);
478 	txr->kick_pending = 0;
479 }
480 
481 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
482 {
483 	struct bnxt *bp = netdev_priv(dev);
484 	struct tx_bd *txbd, *txbd0;
485 	struct tx_bd_ext *txbd1;
486 	struct netdev_queue *txq;
487 	int i;
488 	dma_addr_t mapping;
489 	unsigned int length;
490 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
491 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
492 	struct pci_dev *pdev = bp->pdev;
493 	u16 prod, last_frag, txts_prod;
494 	struct bnxt_tx_ring_info *txr;
495 	struct bnxt_sw_tx_bd *tx_buf;
496 	__le32 lflags = 0;
497 	skb_frag_t *frag;
498 
499 	i = skb_get_queue_mapping(skb);
500 	if (unlikely(i >= bp->tx_nr_rings)) {
501 		dev_kfree_skb_any(skb);
502 		dev_core_stats_tx_dropped_inc(dev);
503 		return NETDEV_TX_OK;
504 	}
505 
506 	txq = netdev_get_tx_queue(dev, i);
507 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
508 	prod = txr->tx_prod;
509 
510 #if (MAX_SKB_FRAGS > TX_MAX_FRAGS)
511 	if (skb_shinfo(skb)->nr_frags > TX_MAX_FRAGS) {
512 		netdev_warn_once(dev, "SKB has too many (%d) fragments, max supported is %d.  SKB will be linearized.\n",
513 				 skb_shinfo(skb)->nr_frags, TX_MAX_FRAGS);
514 		if (skb_linearize(skb))
515 			goto tx_free;
516 	}
517 #endif
518 	if (skb_is_gso(skb) &&
519 	    (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) &&
520 	    !(bp->flags & BNXT_FLAG_UDP_GSO_CAP)) {
521 		int rc = bnxt_sw_udp_gso_xmit(bp, txr, txq, skb);
522 
523 		/* if SW USO queued a packet, the doorbell will be written
524 		 * below and there is no reason to track the last BD with
525 		 * suppressed completions
526 		 */
527 		if (rc > 0)
528 			txr->kick_txbd0 = NULL;
529 
530 		/* if a packet was queued by SW USO or a doorbell was pending
531 		 * from a previous xmit that was deferred, write the doorbell.
532 		 */
533 		if (rc > 0 || txr->kick_pending)
534 			bnxt_txr_db_kick(bp, txr, txr->tx_prod);
535 
536 		return rc < 0 ? NETDEV_TX_BUSY : NETDEV_TX_OK;
537 	}
538 
539 	/* Pad after the SW USO branch: bnxt_sw_udp_gso_xmit() would
540 	 * otherwise account the padding as UDP payload.
541 	 * Must be done before skb_shinfo(skb)->nr_frags is sampled,
542 	 * because skb_put_padto() might linearize the skb.
543 	 */
544 	if (skb_put_padto(skb, BNXT_MIN_PKT_SIZE)) {
545 		/* SKB already freed. */
546 		goto tx_kick_pending;
547 	}
548 
549 	free_size = bnxt_tx_avail(bp, txr);
550 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
551 		/* We must have raced with NAPI cleanup */
552 		if (net_ratelimit() && txr->kick_pending)
553 			netif_warn(bp, tx_err, dev,
554 				   "bnxt: ring busy w/ flush pending!\n");
555 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
556 					bp->tx_wake_thresh))
557 			return NETDEV_TX_BUSY;
558 	}
559 
560 	length = skb->len;
561 	len = skb_headlen(skb);
562 	last_frag = skb_shinfo(skb)->nr_frags;
563 
564 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
565 
566 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
567 	tx_buf->skb = skb;
568 	tx_buf->nr_frags = last_frag;
569 
570 	vlan_tag_flags = 0;
571 	cfa_action = bnxt_xmit_get_cfa_action(skb);
572 	if (skb_vlan_tag_present(skb)) {
573 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
574 				 skb_vlan_tag_get(skb);
575 		/* Currently supports 8021Q, 8021AD vlan offloads
576 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
577 		 */
578 		if (skb->vlan_proto == htons(ETH_P_8021Q))
579 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
580 	}
581 
582 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp &&
583 	    ptp->tx_tstamp_en) {
584 		if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) {
585 			lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
586 			tx_buf->is_ts_pkt = 1;
587 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
588 		} else if (!skb_is_gso(skb)) {
589 			u16 seq_id, hdr_off;
590 
591 			if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) &&
592 			    !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) {
593 				if (vlan_tag_flags)
594 					hdr_off += VLAN_HLEN;
595 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
596 				tx_buf->is_ts_pkt = 1;
597 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
598 
599 				ptp->txts_req[txts_prod].tx_seqid = seq_id;
600 				ptp->txts_req[txts_prod].tx_hdr_off = hdr_off;
601 				tx_buf->txts_prod = txts_prod;
602 			}
603 		}
604 	}
605 	if (unlikely(skb->no_fcs))
606 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
607 
608 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
609 	    skb_frags_readable(skb) && !lflags) {
610 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
611 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
612 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
613 		void __iomem *db = txr->tx_db.doorbell;
614 		void *pdata = tx_push_buf->data;
615 		u64 *end;
616 		int j, push_len;
617 
618 		/* Set COAL_NOW to be ready quickly for the next push */
619 		tx_push->tx_bd_len_flags_type =
620 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
621 					TX_BD_TYPE_LONG_TX_BD |
622 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
623 					TX_BD_FLAGS_COAL_NOW |
624 					TX_BD_FLAGS_PACKET_END |
625 					TX_BD_CNT(2));
626 
627 		if (skb->ip_summed == CHECKSUM_PARTIAL)
628 			tx_push1->tx_bd_hsize_lflags =
629 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
630 		else
631 			tx_push1->tx_bd_hsize_lflags = 0;
632 
633 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
634 		tx_push1->tx_bd_cfa_action =
635 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
636 
637 		end = pdata + length;
638 		end = PTR_ALIGN(end, 8) - 1;
639 		*end = 0;
640 
641 		skb_copy_from_linear_data(skb, pdata, len);
642 		pdata += len;
643 		for (j = 0; j < last_frag; j++) {
644 			void *fptr;
645 
646 			frag = &skb_shinfo(skb)->frags[j];
647 			fptr = skb_frag_address_safe(frag);
648 			if (!fptr)
649 				goto normal_tx;
650 
651 			memcpy(pdata, fptr, skb_frag_size(frag));
652 			pdata += skb_frag_size(frag);
653 		}
654 
655 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
656 		txbd->tx_bd_haddr = txr->data_mapping;
657 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
658 		prod = NEXT_TX(prod);
659 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
660 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
661 		memcpy(txbd, tx_push1, sizeof(*txbd));
662 		prod = NEXT_TX(prod);
663 		tx_push->doorbell =
664 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
665 				    DB_RING_IDX(&txr->tx_db, prod));
666 		WRITE_ONCE(txr->tx_prod, prod);
667 
668 		tx_buf->is_push = 1;
669 		netdev_tx_sent_queue(txq, skb->len);
670 		wmb();	/* Sync is_push and byte queue before pushing data */
671 
672 		push_len = (length + sizeof(*tx_push) + 7) / 8;
673 		if (push_len > 16) {
674 			__iowrite64_copy(db, tx_push_buf, 16);
675 			__iowrite32_copy(db + 4, tx_push_buf + 1,
676 					 (push_len - 16) << 1);
677 		} else {
678 			__iowrite64_copy(db, tx_push_buf, push_len);
679 		}
680 
681 		goto tx_done;
682 	}
683 
684 normal_tx:
685 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
686 
687 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
688 		goto tx_free;
689 
690 	dma_unmap_addr_set(tx_buf, mapping, mapping);
691 	dma_unmap_len_set(tx_buf, len, len);
692 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
693 		TX_BD_CNT(last_frag + 2);
694 
695 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
696 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
697 
698 	prod = NEXT_TX(prod);
699 	txbd1 = bnxt_init_ext_bd(bp, txr, prod, lflags, vlan_tag_flags,
700 				 cfa_action);
701 
702 	if (skb_is_gso(skb)) {
703 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
704 		u32 hdr_len;
705 
706 		if (skb->encapsulation) {
707 			if (udp_gso)
708 				hdr_len = skb_inner_transport_offset(skb) +
709 					  sizeof(struct udphdr);
710 			else
711 				hdr_len = skb_inner_tcp_all_headers(skb);
712 		} else if (udp_gso) {
713 			hdr_len = skb_transport_offset(skb) +
714 				  sizeof(struct udphdr);
715 		} else {
716 			hdr_len = skb_tcp_all_headers(skb);
717 		}
718 
719 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
720 					TX_BD_FLAGS_T_IPID |
721 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
722 		length = skb_shinfo(skb)->gso_size;
723 		txbd1->tx_bd_mss = cpu_to_le32(length);
724 		length += hdr_len;
725 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
726 		txbd1->tx_bd_hsize_lflags |=
727 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
728 	}
729 
730 	length >>= 9;
731 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
732 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
733 				     skb->len);
734 		i = 0;
735 		goto tx_dma_error;
736 	}
737 	flags |= bnxt_lhint_arr[length];
738 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
739 
740 	txbd0 = txbd;
741 	for (i = 0; i < last_frag; i++) {
742 		frag = &skb_shinfo(skb)->frags[i];
743 		prod = NEXT_TX(prod);
744 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
745 
746 		len = skb_frag_size(frag);
747 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
748 					   DMA_TO_DEVICE);
749 
750 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
751 			goto tx_dma_error;
752 
753 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
754 		netmem_dma_unmap_addr_set(skb_frag_netmem(frag), tx_buf,
755 					  mapping, mapping);
756 		dma_unmap_len_set(tx_buf, len, len);
757 
758 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
759 
760 		flags = len << TX_BD_LEN_SHIFT;
761 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
762 	}
763 
764 	txbd->tx_bd_len_flags_type |= cpu_to_le32(TX_BD_FLAGS_PACKET_END);
765 
766 	netdev_tx_sent_queue(txq, skb->len);
767 
768 	skb_tx_timestamp(skb);
769 
770 	prod = NEXT_TX(prod);
771 	WRITE_ONCE(txr->tx_prod, prod);
772 
773 	txr->kick_txbd0 = NULL;
774 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
775 		bnxt_txr_db_kick(bp, txr, prod);
776 	} else {
777 		if (free_size >= bp->tx_wake_thresh) {
778 			txbd0->tx_bd_len_flags_type |=
779 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
780 			txr->kick_txbd0 = txbd0;
781 		}
782 		txr->kick_pending = 1;
783 	}
784 
785 tx_done:
786 
787 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
788 		if (txr->kick_pending)
789 			bnxt_txr_db_kick(bp, txr, prod);
790 
791 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
792 				   bp->tx_wake_thresh);
793 	}
794 	return NETDEV_TX_OK;
795 
796 tx_dma_error:
797 	last_frag = i;
798 
799 	/* start back at beginning and unmap skb */
800 	prod = txr->tx_prod;
801 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
802 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
803 			 skb_headlen(skb), DMA_TO_DEVICE);
804 	prod = NEXT_TX(prod);
805 
806 	/* unmap remaining mapped pages */
807 	for (i = 0; i < last_frag; i++) {
808 		prod = NEXT_TX(prod);
809 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
810 		frag = &skb_shinfo(skb)->frags[i];
811 		netmem_dma_unmap_page_attrs(&pdev->dev,
812 					    dma_unmap_addr(tx_buf, mapping),
813 					    skb_frag_size(frag),
814 					    DMA_TO_DEVICE, 0);
815 	}
816 
817 tx_free:
818 	dev_kfree_skb_any(skb);
819 tx_kick_pending:
820 	if (BNXT_TX_PTP_IS_SET(lflags)) {
821 		txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].is_ts_pkt = 0;
822 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
823 		if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
824 			/* set SKB to err so PTP worker will clean up */
825 			ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO);
826 	}
827 	if (txr->kick_pending)
828 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
829 	txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].skb = NULL;
830 	dev_core_stats_tx_dropped_inc(dev);
831 	return NETDEV_TX_OK;
832 }
833 
834 /* Returns true if some remaining TX packets not processed. */
835 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
836 			  int budget)
837 {
838 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
839 	struct pci_dev *pdev = bp->pdev;
840 	u16 hw_cons = txr->tx_hw_cons;
841 	unsigned int tx_bytes = 0;
842 	u16 cons = txr->tx_cons;
843 	unsigned int dma_len;
844 	dma_addr_t dma_addr;
845 	int tx_pkts = 0;
846 	bool rc = false;
847 
848 	while (RING_TX(bp, cons) != hw_cons) {
849 		struct bnxt_sw_tx_bd *tx_buf, *head_buf;
850 		struct sk_buff *skb;
851 		bool is_ts_pkt;
852 		int j, last;
853 
854 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
855 		head_buf = tx_buf;
856 		skb = tx_buf->skb;
857 
858 		if (unlikely(!skb)) {
859 			bnxt_sched_reset_txr(bp, txr, cons);
860 			return rc;
861 		}
862 
863 		is_ts_pkt = tx_buf->is_ts_pkt;
864 		if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) {
865 			rc = true;
866 			break;
867 		}
868 
869 		cons = NEXT_TX(cons);
870 		tx_pkts++;
871 		tx_bytes += skb->len;
872 		tx_buf->skb = NULL;
873 		tx_buf->is_ts_pkt = 0;
874 
875 		if (tx_buf->is_push) {
876 			tx_buf->is_push = 0;
877 			goto next_tx_int;
878 		}
879 
880 		if (dma_unmap_len(tx_buf, len)) {
881 			dma_addr = dma_unmap_addr(tx_buf, mapping);
882 			dma_len = dma_unmap_len(tx_buf, len);
883 
884 			dma_unmap_single(&pdev->dev, dma_addr, dma_len,
885 					 DMA_TO_DEVICE);
886 		}
887 
888 		last = tx_buf->nr_frags;
889 
890 		for (j = 0; j < last; j++) {
891 			cons = NEXT_TX(cons);
892 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
893 			if (dma_unmap_len(tx_buf, len)) {
894 				dma_addr = dma_unmap_addr(tx_buf, mapping);
895 				dma_len = dma_unmap_len(tx_buf, len);
896 
897 				netmem_dma_unmap_page_attrs(&pdev->dev,
898 							    dma_addr, dma_len,
899 							    DMA_TO_DEVICE, 0);
900 			}
901 		}
902 
903 		if (unlikely(head_buf->is_sw_gso)) {
904 			u16 inline_cons = txr->tx_inline_cons + 1;
905 
906 			WRITE_ONCE(txr->tx_inline_cons, inline_cons);
907 			if (head_buf->is_sw_gso == BNXT_SW_GSO_LAST) {
908 				tso_dma_map_complete(&pdev->dev,
909 						     &head_buf->sw_gso_cstate);
910 			} else {
911 				tx_pkts--;
912 				tx_bytes -= skb->len;
913 				skb = NULL;
914 			}
915 			head_buf->is_sw_gso = 0;
916 		}
917 
918 		if (unlikely(is_ts_pkt)) {
919 			if (BNXT_CHIP_P5(bp)) {
920 				/* PTP worker takes ownership of the skb */
921 				bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod);
922 				skb = NULL;
923 			}
924 		}
925 
926 next_tx_int:
927 		cons = NEXT_TX(cons);
928 
929 		napi_consume_skb(skb, budget);
930 	}
931 
932 	WRITE_ONCE(txr->tx_cons, cons);
933 
934 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
935 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
936 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
937 
938 	return rc;
939 }
940 
941 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
942 {
943 	struct bnxt_tx_ring_info *txr;
944 	bool more = false;
945 	int i;
946 
947 	bnxt_for_each_napi_tx(i, bnapi, txr) {
948 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
949 			more |= __bnxt_tx_int(bp, txr, budget);
950 	}
951 	if (!more)
952 		bnapi->events &= ~BNXT_TX_CMP_EVENT;
953 }
954 
955 static bool bnxt_separate_head_pool(struct bnxt_rx_ring_info *rxr)
956 {
957 	return rxr->need_head_pool || rxr->rx_page_size < PAGE_SIZE;
958 }
959 
960 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
961 					 struct bnxt_rx_ring_info *rxr,
962 					 unsigned int *offset,
963 					 gfp_t gfp)
964 {
965 	struct page *page;
966 
967 	if (rxr->rx_page_size < PAGE_SIZE) {
968 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
969 						rxr->rx_page_size);
970 	} else {
971 		page = page_pool_dev_alloc_pages(rxr->page_pool);
972 		*offset = 0;
973 	}
974 	if (!page)
975 		return NULL;
976 
977 	*mapping = page_pool_get_dma_addr(page) + *offset;
978 	return page;
979 }
980 
981 static netmem_ref __bnxt_alloc_rx_netmem(struct bnxt *bp, dma_addr_t *mapping,
982 					 struct bnxt_rx_ring_info *rxr,
983 					 unsigned int *offset,
984 					 gfp_t gfp)
985 {
986 	netmem_ref netmem;
987 
988 	if (rxr->rx_page_size < PAGE_SIZE) {
989 		netmem = page_pool_alloc_frag_netmem(rxr->page_pool, offset,
990 						     rxr->rx_page_size, gfp);
991 	} else {
992 		netmem = page_pool_alloc_netmems(rxr->page_pool, gfp);
993 		*offset = 0;
994 	}
995 	if (!netmem)
996 		return 0;
997 
998 	*mapping = page_pool_get_dma_addr_netmem(netmem) + *offset;
999 	return netmem;
1000 }
1001 
1002 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
1003 				       struct bnxt_rx_ring_info *rxr,
1004 				       gfp_t gfp)
1005 {
1006 	unsigned int offset;
1007 	struct page *page;
1008 
1009 	page = page_pool_alloc_frag(rxr->head_pool, &offset,
1010 				    bp->rx_buf_size, gfp);
1011 	if (!page)
1012 		return NULL;
1013 
1014 	*mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset;
1015 	return page_address(page) + offset;
1016 }
1017 
1018 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1019 		       u16 prod, gfp_t gfp)
1020 {
1021 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1022 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1023 	dma_addr_t mapping;
1024 
1025 	if (BNXT_RX_PAGE_MODE(bp)) {
1026 		unsigned int offset;
1027 		struct page *page =
1028 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
1029 
1030 		if (!page)
1031 			return -ENOMEM;
1032 
1033 		mapping += bp->rx_dma_offset;
1034 		rx_buf->data = page;
1035 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
1036 		rx_buf->offset = offset;
1037 	} else {
1038 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp);
1039 
1040 		if (!data)
1041 			return -ENOMEM;
1042 
1043 		rx_buf->data = data;
1044 		rx_buf->data_ptr = data + bp->rx_offset;
1045 		rx_buf->offset = 0;
1046 	}
1047 	rx_buf->mapping = mapping;
1048 
1049 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1050 	return 0;
1051 }
1052 
1053 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
1054 {
1055 	u16 prod = rxr->rx_prod;
1056 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1057 	struct bnxt *bp = rxr->bnapi->bp;
1058 	struct rx_bd *cons_bd, *prod_bd;
1059 
1060 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1061 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1062 
1063 	prod_rx_buf->data = data;
1064 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
1065 
1066 	prod_rx_buf->mapping = cons_rx_buf->mapping;
1067 	prod_rx_buf->offset = cons_rx_buf->offset;
1068 
1069 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1070 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
1071 
1072 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
1073 }
1074 
1075 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1076 {
1077 	u16 next, max = rxr->rx_agg_bmap_size;
1078 
1079 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
1080 	if (next >= max)
1081 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
1082 	return next;
1083 }
1084 
1085 static int bnxt_alloc_rx_netmem(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1086 				u16 prod, gfp_t gfp)
1087 {
1088 	struct rx_bd *rxbd =
1089 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1090 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
1091 	u16 sw_prod = rxr->rx_sw_agg_prod;
1092 	unsigned int offset = 0;
1093 	dma_addr_t mapping;
1094 	netmem_ref netmem;
1095 
1096 	netmem = __bnxt_alloc_rx_netmem(bp, &mapping, rxr, &offset, gfp);
1097 	if (!netmem)
1098 		return -ENOMEM;
1099 
1100 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1101 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1102 
1103 	__set_bit(sw_prod, rxr->rx_agg_bmap);
1104 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
1105 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1106 
1107 	rx_agg_buf->netmem = netmem;
1108 	rx_agg_buf->offset = offset;
1109 	rx_agg_buf->mapping = mapping;
1110 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1111 	rxbd->rx_bd_opaque = sw_prod;
1112 	return 0;
1113 }
1114 
1115 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1116 				       struct bnxt_cp_ring_info *cpr,
1117 				       u16 cp_cons, u16 curr)
1118 {
1119 	struct rx_agg_cmp *agg;
1120 
1121 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1122 	agg = (struct rx_agg_cmp *)
1123 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1124 	return agg;
1125 }
1126 
1127 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1128 					      struct bnxt_rx_ring_info *rxr,
1129 					      u16 agg_id, u16 curr)
1130 {
1131 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1132 
1133 	return &tpa_info->agg_arr[curr];
1134 }
1135 
1136 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1137 				   u16 start, u32 agg_bufs, bool tpa)
1138 {
1139 	struct bnxt_napi *bnapi = cpr->bnapi;
1140 	struct bnxt *bp = bnapi->bp;
1141 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1142 	u16 prod = rxr->rx_agg_prod;
1143 	u16 sw_prod = rxr->rx_sw_agg_prod;
1144 	bool p5_tpa = false;
1145 	u32 i;
1146 
1147 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1148 		p5_tpa = true;
1149 
1150 	for (i = 0; i < agg_bufs; i++) {
1151 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1152 		struct rx_agg_cmp *agg;
1153 		struct rx_bd *prod_bd;
1154 		netmem_ref netmem;
1155 		u16 cons;
1156 
1157 		if (p5_tpa)
1158 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1159 		else
1160 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1161 		cons = agg->rx_agg_cmp_opaque;
1162 		__clear_bit(cons, rxr->rx_agg_bmap);
1163 
1164 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1165 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1166 
1167 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1168 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1169 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1170 
1171 		/* It is possible for sw_prod to be equal to cons, so
1172 		 * set cons_rx_buf->netmem to 0 first.
1173 		 */
1174 		netmem = cons_rx_buf->netmem;
1175 		cons_rx_buf->netmem = 0;
1176 		prod_rx_buf->netmem = netmem;
1177 		prod_rx_buf->offset = cons_rx_buf->offset;
1178 
1179 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1180 
1181 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1182 
1183 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1184 		prod_bd->rx_bd_opaque = sw_prod;
1185 
1186 		prod = NEXT_RX_AGG(prod);
1187 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1188 	}
1189 	rxr->rx_agg_prod = prod;
1190 	rxr->rx_sw_agg_prod = sw_prod;
1191 }
1192 
1193 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1194 					      struct bnxt_rx_ring_info *rxr,
1195 					      u16 cons, void *data, u8 *data_ptr,
1196 					      dma_addr_t dma_addr,
1197 					      unsigned int offset_and_len)
1198 {
1199 	unsigned int len = offset_and_len & 0xffff;
1200 	struct page *page = data;
1201 	u16 prod = rxr->rx_prod;
1202 	struct sk_buff *skb;
1203 	void *frag_start;
1204 	int err;
1205 
1206 	frag_start = page_address(page) + rxr->rx_buf_ring[cons].offset;
1207 
1208 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1209 	if (unlikely(err)) {
1210 		bnxt_reuse_rx_data(rxr, cons, data);
1211 		return NULL;
1212 	}
1213 	dma_addr -= bp->rx_dma_offset;
1214 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, rxr->rx_page_size,
1215 				bp->rx_dir);
1216 	skb = napi_build_skb(frag_start, rxr->rx_page_size);
1217 	if (!skb) {
1218 		struct page *new_page;
1219 
1220 		new_page = rxr->rx_buf_ring[RING_RX(bp, prod)].data;
1221 		/* The caller may still need the XDP frags from shinfo at the
1222 		 * end of the head page. Release the new buffer instead and
1223 		 * put the head back on the ring.
1224 		 */
1225 		page_pool_recycle_direct(rxr->page_pool, new_page);
1226 		bnxt_reuse_rx_data(rxr, cons, data);
1227 		return NULL;
1228 	}
1229 	skb_mark_for_recycle(skb);
1230 	skb_reserve(skb, data_ptr - (u8 *)frag_start);
1231 	__skb_put(skb, len);
1232 
1233 	return skb;
1234 }
1235 
1236 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1237 					struct bnxt_rx_ring_info *rxr,
1238 					u16 cons, void *data, u8 *data_ptr,
1239 					dma_addr_t dma_addr,
1240 					unsigned int offset_and_len)
1241 {
1242 	unsigned int payload = offset_and_len >> 16;
1243 	unsigned int len = offset_and_len & 0xffff;
1244 	skb_frag_t *frag;
1245 	struct page *page = data;
1246 	u16 prod = rxr->rx_prod;
1247 	struct sk_buff *skb;
1248 	int off, err;
1249 
1250 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1251 	if (unlikely(err)) {
1252 		bnxt_reuse_rx_data(rxr, cons, data);
1253 		return NULL;
1254 	}
1255 	dma_addr -= bp->rx_dma_offset;
1256 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, rxr->rx_page_size,
1257 				bp->rx_dir);
1258 
1259 	if (unlikely(!payload))
1260 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1261 
1262 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1263 	if (!skb) {
1264 		page_pool_recycle_direct(rxr->page_pool, page);
1265 		return NULL;
1266 	}
1267 
1268 	skb_mark_for_recycle(skb);
1269 	off = (void *)data_ptr - page_address(page);
1270 	skb_add_rx_frag(skb, 0, page, off, len, rxr->rx_page_size);
1271 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1272 	       payload + NET_IP_ALIGN);
1273 
1274 	frag = &skb_shinfo(skb)->frags[0];
1275 	skb_frag_size_sub(frag, payload);
1276 	skb_frag_off_add(frag, payload);
1277 	skb->data_len -= payload;
1278 	skb->tail += payload;
1279 
1280 	return skb;
1281 }
1282 
1283 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1284 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1285 				   void *data, u8 *data_ptr,
1286 				   dma_addr_t dma_addr,
1287 				   unsigned int offset_and_len)
1288 {
1289 	u16 prod = rxr->rx_prod;
1290 	struct sk_buff *skb;
1291 	int err;
1292 
1293 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1294 	if (unlikely(err)) {
1295 		bnxt_reuse_rx_data(rxr, cons, data);
1296 		return NULL;
1297 	}
1298 
1299 	skb = napi_build_skb(data, bp->rx_buf_size);
1300 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1301 				bp->rx_dir);
1302 	if (!skb) {
1303 		page_pool_free_va(rxr->head_pool, data, true);
1304 		return NULL;
1305 	}
1306 
1307 	skb_mark_for_recycle(skb);
1308 	skb_reserve(skb, bp->rx_offset);
1309 	skb_put(skb, offset_and_len & 0xffff);
1310 	return skb;
1311 }
1312 
1313 static u32 __bnxt_rx_agg_netmems(struct bnxt *bp,
1314 				 struct bnxt_cp_ring_info *cpr,
1315 				 u16 idx, u32 agg_bufs, bool tpa,
1316 				 struct sk_buff *skb,
1317 				 struct xdp_buff *xdp)
1318 {
1319 	struct bnxt_napi *bnapi = cpr->bnapi;
1320 	struct skb_shared_info *shinfo;
1321 	struct bnxt_rx_ring_info *rxr;
1322 	u32 i, total_frag_len = 0;
1323 	bool p5_tpa = false;
1324 	u16 prod;
1325 
1326 	rxr = bnapi->rx_ring;
1327 	prod = rxr->rx_agg_prod;
1328 
1329 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1330 		p5_tpa = true;
1331 
1332 	if (skb)
1333 		shinfo = skb_shinfo(skb);
1334 	else
1335 		shinfo = xdp_get_shared_info_from_buff(xdp);
1336 
1337 	for (i = 0; i < agg_bufs; i++) {
1338 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1339 		struct rx_agg_cmp *agg;
1340 		u16 cons, frag_len;
1341 		netmem_ref netmem;
1342 
1343 		if (p5_tpa)
1344 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1345 		else
1346 			agg = bnxt_get_agg(bp, cpr, idx, i);
1347 		cons = agg->rx_agg_cmp_opaque;
1348 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1349 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1350 
1351 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1352 		if (skb) {
1353 			skb_add_rx_frag_netmem(skb, i, cons_rx_buf->netmem,
1354 					       cons_rx_buf->offset,
1355 					       frag_len, rxr->rx_page_size);
1356 		} else {
1357 			skb_frag_t *frag = &shinfo->frags[i];
1358 
1359 			skb_frag_fill_netmem_desc(frag, cons_rx_buf->netmem,
1360 						  cons_rx_buf->offset,
1361 						  frag_len);
1362 			shinfo->nr_frags = i + 1;
1363 		}
1364 		__clear_bit(cons, rxr->rx_agg_bmap);
1365 
1366 		/* It is possible for bnxt_alloc_rx_netmem() to allocate
1367 		 * a sw_prod index that equals the cons index, so we
1368 		 * need to clear the cons entry now.
1369 		 */
1370 		netmem = cons_rx_buf->netmem;
1371 		cons_rx_buf->netmem = 0;
1372 
1373 		if (xdp && netmem_is_pfmemalloc(netmem))
1374 			xdp_buff_set_frag_pfmemalloc(xdp);
1375 
1376 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_ATOMIC) != 0) {
1377 			if (skb) {
1378 				skb->len -= frag_len;
1379 				skb->data_len -= frag_len;
1380 				skb->truesize -= rxr->rx_page_size;
1381 			}
1382 
1383 			--shinfo->nr_frags;
1384 			cons_rx_buf->netmem = netmem;
1385 
1386 			/* Update prod since possibly some netmems have been
1387 			 * allocated already.
1388 			 */
1389 			rxr->rx_agg_prod = prod;
1390 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1391 			return 0;
1392 		}
1393 
1394 		page_pool_dma_sync_netmem_for_cpu(rxr->page_pool, netmem, 0,
1395 						  rxr->rx_page_size);
1396 
1397 		total_frag_len += frag_len;
1398 		prod = NEXT_RX_AGG(prod);
1399 	}
1400 	rxr->rx_agg_prod = prod;
1401 	return total_frag_len;
1402 }
1403 
1404 static struct sk_buff *bnxt_rx_agg_netmems_skb(struct bnxt *bp,
1405 					       struct bnxt_cp_ring_info *cpr,
1406 					       struct sk_buff *skb, u16 idx,
1407 					       u32 agg_bufs, bool tpa)
1408 {
1409 	u32 total_frag_len = 0;
1410 
1411 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1412 					       skb, NULL);
1413 	if (!total_frag_len) {
1414 		skb_mark_for_recycle(skb);
1415 		dev_kfree_skb(skb);
1416 		return NULL;
1417 	}
1418 
1419 	return skb;
1420 }
1421 
1422 static u32 bnxt_rx_agg_netmems_xdp(struct bnxt *bp,
1423 				   struct bnxt_cp_ring_info *cpr,
1424 				   struct xdp_buff *xdp, u16 idx,
1425 				   u32 agg_bufs, bool tpa)
1426 {
1427 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1428 	u32 total_frag_len = 0;
1429 
1430 	if (!xdp_buff_has_frags(xdp)) {
1431 		xdp_buff_set_frags_flag(xdp);
1432 		shinfo->nr_frags = 0;
1433 	}
1434 
1435 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1436 					       NULL, xdp);
1437 	if (!total_frag_len) {
1438 		bnxt_xdp_buff_frags_free(cpr->bnapi->rx_ring, xdp);
1439 		return 0;
1440 	}
1441 	shinfo->nr_frags = agg_bufs;
1442 	shinfo->xdp_frags_size = total_frag_len;
1443 	return total_frag_len;
1444 }
1445 
1446 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1447 			       u8 agg_bufs, u32 *raw_cons)
1448 {
1449 	u16 last;
1450 	struct rx_agg_cmp *agg;
1451 
1452 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1453 	last = RING_CMP(*raw_cons);
1454 	agg = (struct rx_agg_cmp *)
1455 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1456 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1457 }
1458 
1459 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1460 				      unsigned int len,
1461 				      dma_addr_t mapping)
1462 {
1463 	struct bnxt *bp = bnapi->bp;
1464 	struct pci_dev *pdev = bp->pdev;
1465 	struct sk_buff *skb;
1466 
1467 	skb = napi_alloc_skb(&bnapi->napi, len);
1468 	if (!skb)
1469 		return NULL;
1470 
1471 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copybreak,
1472 				bp->rx_dir);
1473 
1474 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1475 	       len + NET_IP_ALIGN);
1476 
1477 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copybreak,
1478 				   bp->rx_dir);
1479 
1480 	skb_put(skb, len);
1481 
1482 	return skb;
1483 }
1484 
1485 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1486 				     unsigned int len,
1487 				     dma_addr_t mapping)
1488 {
1489 	return bnxt_copy_data(bnapi, data, len, mapping);
1490 }
1491 
1492 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1493 				     struct xdp_buff *xdp,
1494 				     unsigned int len,
1495 				     dma_addr_t mapping)
1496 {
1497 	unsigned int metasize = 0;
1498 	u8 *data = xdp->data;
1499 	struct sk_buff *skb;
1500 
1501 	len = xdp->data_end - xdp->data_meta;
1502 	metasize = xdp->data - xdp->data_meta;
1503 	data = xdp->data_meta;
1504 
1505 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1506 	if (!skb)
1507 		return skb;
1508 
1509 	if (metasize) {
1510 		skb_metadata_set(skb, metasize);
1511 		__skb_pull(skb, metasize);
1512 	}
1513 
1514 	if (unlikely(xdp_buff_has_frags(xdp))) {
1515 		struct skb_shared_info *sinfo;
1516 
1517 		sinfo = xdp_get_shared_info_from_buff(xdp);
1518 		memcpy(skb_shinfo(skb)->frags, sinfo->frags,
1519 		       sinfo->nr_frags * sizeof(skb_frag_t));
1520 	}
1521 
1522 	return skb;
1523 }
1524 
1525 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1526 			   u32 *raw_cons, void *cmp)
1527 {
1528 	struct rx_cmp *rxcmp = cmp;
1529 	u32 tmp_raw_cons = *raw_cons;
1530 	u8 cmp_type, agg_bufs = 0;
1531 
1532 	cmp_type = RX_CMP_TYPE(rxcmp);
1533 
1534 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1535 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1536 			    RX_CMP_AGG_BUFS) >>
1537 			   RX_CMP_AGG_BUFS_SHIFT;
1538 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1539 		struct rx_tpa_end_cmp *tpa_end = cmp;
1540 
1541 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1542 			return 0;
1543 
1544 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1545 	}
1546 
1547 	if (agg_bufs) {
1548 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1549 			return -EBUSY;
1550 	}
1551 	*raw_cons = tmp_raw_cons;
1552 	return 0;
1553 }
1554 
1555 static u16 bnxt_alloc_agg_idx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1556 			      u16 agg_id)
1557 {
1558 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1559 	u16 idx = agg_id & (bp->max_tpa_roundup_size - 1);
1560 
1561 	if (test_bit(idx, map->agg_idx_bmap)) {
1562 		idx = find_first_zero_bit(map->agg_idx_bmap,
1563 					  bp->max_tpa_roundup_size);
1564 		if (idx >= bp->max_tpa_roundup_size)
1565 			return INVALID_HW_RING_ID;
1566 	}
1567 	__set_bit(idx, map->agg_idx_bmap);
1568 	map->agg_id_tbl[agg_id] = idx;
1569 	return idx;
1570 }
1571 
1572 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1573 {
1574 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1575 
1576 	__clear_bit(idx, map->agg_idx_bmap);
1577 }
1578 
1579 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1580 {
1581 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1582 
1583 	return map->agg_id_tbl[agg_id];
1584 }
1585 
1586 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1587 			      struct rx_tpa_start_cmp *tpa_start,
1588 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1589 {
1590 	tpa_info->cfa_code_valid = 1;
1591 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1592 	tpa_info->vlan_valid = 0;
1593 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1594 		tpa_info->vlan_valid = 1;
1595 		tpa_info->metadata =
1596 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1597 	}
1598 }
1599 
1600 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1601 				 struct rx_tpa_start_cmp *tpa_start,
1602 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1603 {
1604 	tpa_info->vlan_valid = 0;
1605 	if (TPA_START_VLAN_VALID(tpa_start)) {
1606 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1607 		u32 vlan_proto = ETH_P_8021Q;
1608 
1609 		tpa_info->vlan_valid = 1;
1610 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1611 			vlan_proto = ETH_P_8021AD;
1612 		tpa_info->metadata = vlan_proto << 16 |
1613 				     TPA_START_METADATA0_TCI(tpa_start1);
1614 	}
1615 }
1616 
1617 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1618 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1619 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1620 {
1621 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1622 	struct bnxt_tpa_info *tpa_info;
1623 	u16 cons, prod, agg_id;
1624 	struct rx_bd *prod_bd;
1625 	dma_addr_t mapping;
1626 
1627 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1628 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1629 		agg_id = bnxt_alloc_agg_idx(bp, rxr, agg_id);
1630 		if (unlikely(agg_id == INVALID_HW_RING_ID)) {
1631 			netdev_warn(bp->dev, "Unable to allocate agg ID for ring %d, agg 0x%x\n",
1632 				    rxr->bnapi->index,
1633 				    TPA_START_AGG_ID_P5(tpa_start));
1634 			bnxt_sched_reset_rxr(bp, rxr);
1635 			return;
1636 		}
1637 	} else {
1638 		agg_id = TPA_START_AGG_ID(tpa_start);
1639 	}
1640 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1641 	prod = rxr->rx_prod;
1642 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1643 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1644 	tpa_info = &rxr->rx_tpa[agg_id];
1645 
1646 	if (unlikely(cons != rxr->rx_next_cons ||
1647 		     TPA_START_ERROR(tpa_start))) {
1648 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1649 			    cons, rxr->rx_next_cons,
1650 			    TPA_START_ERROR_CODE(tpa_start1));
1651 		bnxt_sched_reset_rxr(bp, rxr);
1652 		return;
1653 	}
1654 	prod_rx_buf->data = tpa_info->data;
1655 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1656 
1657 	mapping = tpa_info->mapping;
1658 	prod_rx_buf->mapping = mapping;
1659 
1660 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1661 
1662 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1663 
1664 	tpa_info->data = cons_rx_buf->data;
1665 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1666 	cons_rx_buf->data = NULL;
1667 	tpa_info->mapping = cons_rx_buf->mapping;
1668 
1669 	tpa_info->len =
1670 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1671 				RX_TPA_START_CMP_LEN_SHIFT;
1672 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1673 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1674 		tpa_info->gso_type = SKB_GSO_TCPV4;
1675 		if (TPA_START_IS_IPV6(tpa_start1))
1676 			tpa_info->gso_type = SKB_GSO_TCPV6;
1677 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1678 		else if (!BNXT_CHIP_P4_PLUS(bp) &&
1679 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1680 			tpa_info->gso_type = SKB_GSO_TCPV6;
1681 		tpa_info->rss_hash =
1682 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1683 	} else {
1684 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1685 		tpa_info->gso_type = 0;
1686 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1687 	}
1688 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1689 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1690 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1691 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1692 	else
1693 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1694 	tpa_info->agg_count = 0;
1695 
1696 	rxr->rx_prod = NEXT_RX(prod);
1697 	cons = RING_RX(bp, NEXT_RX(cons));
1698 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1699 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1700 
1701 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1702 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1703 	cons_rx_buf->data = NULL;
1704 }
1705 
1706 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1707 {
1708 	if (agg_bufs)
1709 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1710 }
1711 
1712 #ifdef CONFIG_INET
1713 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1714 {
1715 	struct udphdr *uh = NULL;
1716 
1717 	if (ip_proto == htons(ETH_P_IP)) {
1718 		struct iphdr *iph = (struct iphdr *)skb->data;
1719 
1720 		if (iph->protocol == IPPROTO_UDP)
1721 			uh = (struct udphdr *)(iph + 1);
1722 	} else {
1723 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1724 
1725 		if (iph->nexthdr == IPPROTO_UDP)
1726 			uh = (struct udphdr *)(iph + 1);
1727 	}
1728 	if (uh) {
1729 		if (uh->check)
1730 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1731 		else
1732 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1733 	}
1734 }
1735 #endif
1736 
1737 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1738 					   int payload_off, int tcp_ts,
1739 					   struct sk_buff *skb)
1740 {
1741 #ifdef CONFIG_INET
1742 	struct tcphdr *th;
1743 	int len, nw_off;
1744 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1745 	u32 hdr_info = tpa_info->hdr_info;
1746 	bool loopback = false;
1747 
1748 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1749 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1750 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1751 
1752 	/* If the packet is an internal loopback packet, the offsets will
1753 	 * have an extra 4 bytes.
1754 	 */
1755 	if (inner_mac_off == 4) {
1756 		loopback = true;
1757 	} else if (inner_mac_off > 4) {
1758 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1759 					    ETH_HLEN - 2));
1760 
1761 		/* We only support inner iPv4/ipv6.  If we don't see the
1762 		 * correct protocol ID, it must be a loopback packet where
1763 		 * the offsets are off by 4.
1764 		 */
1765 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1766 			loopback = true;
1767 	}
1768 	if (loopback) {
1769 		/* internal loopback packet, subtract all offsets by 4 */
1770 		inner_ip_off -= 4;
1771 		inner_mac_off -= 4;
1772 		outer_ip_off -= 4;
1773 	}
1774 
1775 	nw_off = inner_ip_off - ETH_HLEN;
1776 	skb_set_network_header(skb, nw_off);
1777 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1778 		struct ipv6hdr *iph = ipv6_hdr(skb);
1779 
1780 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1781 		len = skb->len - skb_transport_offset(skb);
1782 		th = tcp_hdr(skb);
1783 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1784 	} else {
1785 		struct iphdr *iph = ip_hdr(skb);
1786 
1787 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1788 		len = skb->len - skb_transport_offset(skb);
1789 		th = tcp_hdr(skb);
1790 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1791 	}
1792 
1793 	if (inner_mac_off) { /* tunnel */
1794 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1795 					    ETH_HLEN - 2));
1796 
1797 		bnxt_gro_tunnel(skb, proto);
1798 	}
1799 #endif
1800 	return skb;
1801 }
1802 
1803 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1804 					   int payload_off, int tcp_ts,
1805 					   struct sk_buff *skb)
1806 {
1807 #ifdef CONFIG_INET
1808 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1809 	u32 hdr_info = tpa_info->hdr_info;
1810 	int iphdr_len, nw_off;
1811 
1812 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1813 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1814 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1815 
1816 	nw_off = inner_ip_off - ETH_HLEN;
1817 	skb_set_network_header(skb, nw_off);
1818 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1819 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1820 	skb_set_transport_header(skb, nw_off + iphdr_len);
1821 
1822 	if (inner_mac_off) { /* tunnel */
1823 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1824 					    ETH_HLEN - 2));
1825 
1826 		bnxt_gro_tunnel(skb, proto);
1827 	}
1828 #endif
1829 	return skb;
1830 }
1831 
1832 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1833 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1834 
1835 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1836 					   int payload_off, int tcp_ts,
1837 					   struct sk_buff *skb)
1838 {
1839 #ifdef CONFIG_INET
1840 	struct tcphdr *th;
1841 	int len, nw_off, tcp_opt_len = 0;
1842 
1843 	if (tcp_ts)
1844 		tcp_opt_len = 12;
1845 
1846 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1847 		struct iphdr *iph;
1848 
1849 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1850 			 ETH_HLEN;
1851 		skb_set_network_header(skb, nw_off);
1852 		iph = ip_hdr(skb);
1853 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1854 		len = skb->len - skb_transport_offset(skb);
1855 		th = tcp_hdr(skb);
1856 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1857 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1858 		struct ipv6hdr *iph;
1859 
1860 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1861 			 ETH_HLEN;
1862 		skb_set_network_header(skb, nw_off);
1863 		iph = ipv6_hdr(skb);
1864 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1865 		len = skb->len - skb_transport_offset(skb);
1866 		th = tcp_hdr(skb);
1867 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1868 	} else {
1869 		dev_kfree_skb_any(skb);
1870 		return NULL;
1871 	}
1872 
1873 	if (nw_off) /* tunnel */
1874 		bnxt_gro_tunnel(skb, skb->protocol);
1875 #endif
1876 	return skb;
1877 }
1878 
1879 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1880 					   struct bnxt_tpa_info *tpa_info,
1881 					   struct rx_tpa_end_cmp *tpa_end,
1882 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1883 					   struct sk_buff *skb,
1884 					   struct bnxt_rx_sw_stats *rx_stats)
1885 {
1886 #ifdef CONFIG_INET
1887 	int payload_off;
1888 	u16 segs;
1889 
1890 	segs = TPA_END_TPA_SEGS(tpa_end);
1891 	if (segs == 1)
1892 		return skb;
1893 
1894 	rx_stats->rx_hw_gro_packets++;
1895 	rx_stats->rx_hw_gro_wire_packets += segs;
1896 
1897 	NAPI_GRO_CB(skb)->count = segs;
1898 	skb_shinfo(skb)->gso_size =
1899 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1900 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1901 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1902 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1903 	else
1904 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1905 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1906 	if (likely(skb))
1907 		tcp_gro_complete(skb);
1908 #endif
1909 	return skb;
1910 }
1911 
1912 /* Given the cfa_code of a received packet determine which
1913  * netdev (vf-rep or PF) the packet is destined to.
1914  */
1915 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1916 {
1917 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1918 
1919 	/* if vf-rep dev is NULL, it must belong to the PF */
1920 	return dev ? dev : bp->dev;
1921 }
1922 
1923 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1924 					   struct bnxt_cp_ring_info *cpr,
1925 					   u32 *raw_cons,
1926 					   struct rx_tpa_end_cmp *tpa_end,
1927 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1928 					   u8 *event)
1929 {
1930 	struct bnxt_napi *bnapi = cpr->bnapi;
1931 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1932 	struct net_device *dev = bp->dev;
1933 	u8 *data_ptr, agg_bufs;
1934 	unsigned int len;
1935 	struct bnxt_tpa_info *tpa_info;
1936 	dma_addr_t mapping;
1937 	struct sk_buff *skb;
1938 	u16 idx = 0, agg_id;
1939 	void *data;
1940 	bool gro;
1941 
1942 	if (unlikely(bnapi->in_reset)) {
1943 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1944 
1945 		if (rc < 0)
1946 			return ERR_PTR(-EBUSY);
1947 		return NULL;
1948 	}
1949 
1950 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1951 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1952 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1953 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1954 		tpa_info = &rxr->rx_tpa[agg_id];
1955 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1956 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1957 				    agg_bufs, tpa_info->agg_count);
1958 			agg_bufs = tpa_info->agg_count;
1959 		}
1960 		tpa_info->agg_count = 0;
1961 		*event |= BNXT_AGG_EVENT;
1962 		bnxt_free_agg_idx(rxr, agg_id);
1963 		idx = agg_id;
1964 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1965 	} else {
1966 		agg_id = TPA_END_AGG_ID(tpa_end);
1967 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1968 		tpa_info = &rxr->rx_tpa[agg_id];
1969 		idx = RING_CMP(*raw_cons);
1970 		if (agg_bufs) {
1971 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1972 				return ERR_PTR(-EBUSY);
1973 
1974 			*event |= BNXT_AGG_EVENT;
1975 			idx = NEXT_CMP(idx);
1976 		}
1977 		gro = !!TPA_END_GRO(tpa_end);
1978 	}
1979 	data = tpa_info->data;
1980 	data_ptr = tpa_info->data_ptr;
1981 	prefetch(data_ptr);
1982 	len = tpa_info->len;
1983 	mapping = tpa_info->mapping;
1984 
1985 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1986 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1987 		if (agg_bufs > MAX_SKB_FRAGS)
1988 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1989 				    agg_bufs, (int)MAX_SKB_FRAGS);
1990 		return NULL;
1991 	}
1992 
1993 	if (len <= bp->rx_copybreak) {
1994 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1995 		if (!skb) {
1996 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1997 			cpr->sw_stats->rx.rx_oom_discards += 1;
1998 			return NULL;
1999 		}
2000 	} else {
2001 		u8 *new_data;
2002 		dma_addr_t new_mapping;
2003 
2004 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr,
2005 						GFP_ATOMIC);
2006 		if (!new_data) {
2007 			bnxt_abort_tpa(cpr, idx, agg_bufs);
2008 			cpr->sw_stats->rx.rx_oom_discards += 1;
2009 			return NULL;
2010 		}
2011 
2012 		tpa_info->data = new_data;
2013 		tpa_info->data_ptr = new_data + bp->rx_offset;
2014 		tpa_info->mapping = new_mapping;
2015 
2016 		skb = napi_build_skb(data, bp->rx_buf_size);
2017 		dma_sync_single_for_cpu(&bp->pdev->dev, mapping,
2018 					bp->rx_buf_use_size, bp->rx_dir);
2019 
2020 		if (!skb) {
2021 			page_pool_free_va(rxr->head_pool, data, true);
2022 			bnxt_abort_tpa(cpr, idx, agg_bufs);
2023 			cpr->sw_stats->rx.rx_oom_discards += 1;
2024 			return NULL;
2025 		}
2026 		skb_mark_for_recycle(skb);
2027 		skb_reserve(skb, bp->rx_offset);
2028 		skb_put(skb, len);
2029 	}
2030 
2031 	if (agg_bufs) {
2032 		skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, idx, agg_bufs,
2033 					      true);
2034 		if (!skb) {
2035 			/* Page reuse already handled by bnxt_rx_pages(). */
2036 			cpr->sw_stats->rx.rx_oom_discards += 1;
2037 			return NULL;
2038 		}
2039 	}
2040 
2041 	if (tpa_info->cfa_code_valid)
2042 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
2043 	skb->protocol = eth_type_trans(skb, dev);
2044 
2045 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
2046 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
2047 
2048 	if (tpa_info->vlan_valid &&
2049 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
2050 		__be16 vlan_proto = htons(tpa_info->metadata >>
2051 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
2052 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
2053 
2054 		if (eth_type_vlan(vlan_proto)) {
2055 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2056 		} else {
2057 			dev_kfree_skb(skb);
2058 			return NULL;
2059 		}
2060 	}
2061 
2062 	skb_checksum_none_assert(skb);
2063 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
2064 		skb->ip_summed = CHECKSUM_UNNECESSARY;
2065 		skb->csum_level =
2066 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
2067 	}
2068 
2069 	if (gro)
2070 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb,
2071 				   &cpr->sw_stats->rx);
2072 
2073 	return skb;
2074 }
2075 
2076 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
2077 			 struct rx_agg_cmp *rx_agg)
2078 {
2079 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
2080 	struct bnxt_tpa_info *tpa_info;
2081 
2082 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
2083 	tpa_info = &rxr->rx_tpa[agg_id];
2084 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
2085 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
2086 }
2087 
2088 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
2089 			     struct sk_buff *skb)
2090 {
2091 	skb_mark_for_recycle(skb);
2092 
2093 	if (skb->dev != bp->dev) {
2094 		/* this packet belongs to a vf-rep */
2095 		bnxt_vf_rep_rx(bp, skb);
2096 		return;
2097 	}
2098 	skb_record_rx_queue(skb, bnapi->index);
2099 	napi_gro_receive(&bnapi->napi, skb);
2100 }
2101 
2102 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
2103 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
2104 {
2105 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
2106 
2107 	if (BNXT_PTP_RX_TS_VALID(flags))
2108 		goto ts_valid;
2109 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
2110 		return false;
2111 
2112 ts_valid:
2113 	*cmpl_ts = ts;
2114 	return true;
2115 }
2116 
2117 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
2118 				    struct rx_cmp *rxcmp,
2119 				    struct rx_cmp_ext *rxcmp1)
2120 {
2121 	__be16 vlan_proto;
2122 	u16 vtag;
2123 
2124 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
2125 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
2126 		u32 meta_data;
2127 
2128 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
2129 			return skb;
2130 
2131 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
2132 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
2133 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
2134 		if (eth_type_vlan(vlan_proto))
2135 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2136 		else
2137 			goto vlan_err;
2138 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2139 		if (RX_CMP_VLAN_VALID(rxcmp)) {
2140 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
2141 
2142 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
2143 				vlan_proto = htons(ETH_P_8021Q);
2144 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
2145 				vlan_proto = htons(ETH_P_8021AD);
2146 			else
2147 				goto vlan_err;
2148 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
2149 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2150 		}
2151 	}
2152 	return skb;
2153 vlan_err:
2154 	skb_mark_for_recycle(skb);
2155 	dev_kfree_skb(skb);
2156 	return NULL;
2157 }
2158 
2159 /* returns the following:
2160  * 1       - 1 packet successfully received
2161  * 0       - successful TPA_START, packet not completed yet
2162  * -EBUSY  - completion ring does not have all the agg buffers yet
2163  * -ENOMEM - packet aborted due to out of memory
2164  * -EIO    - packet aborted due to hw error indicated in BD
2165  */
2166 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2167 		       u32 *raw_cons, u8 *event)
2168 {
2169 	struct bnxt_napi *bnapi = cpr->bnapi;
2170 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2171 	struct net_device *dev = bp->dev;
2172 	struct rx_cmp *rxcmp;
2173 	struct rx_cmp_ext *rxcmp1;
2174 	u32 tmp_raw_cons = *raw_cons;
2175 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2176 	struct skb_shared_info *sinfo;
2177 	struct bnxt_xdp_buff bnxt_xdp;
2178 	struct bnxt_sw_rx_bd *rx_buf;
2179 	unsigned int len;
2180 	u8 *data_ptr, agg_bufs, cmp_type;
2181 	bool xdp_active = false;
2182 	dma_addr_t dma_addr;
2183 	struct sk_buff *skb;
2184 	u32 flags, misc;
2185 	u32 cmpl_ts;
2186 	void *data;
2187 	int rc = 0;
2188 
2189 	rxcmp = (struct rx_cmp *)
2190 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2191 
2192 	cmp_type = RX_CMP_TYPE(rxcmp);
2193 
2194 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2195 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2196 		goto next_rx_no_prod_no_len;
2197 	}
2198 
2199 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2200 	cp_cons = RING_CMP(tmp_raw_cons);
2201 	rxcmp1 = (struct rx_cmp_ext *)
2202 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2203 
2204 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2205 		return -EBUSY;
2206 
2207 	/* The valid test of the entry must be done first before
2208 	 * reading any further.
2209 	 */
2210 	dma_rmb();
2211 	prod = rxr->rx_prod;
2212 
2213 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2214 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2215 		bnxt_tpa_start(bp, rxr, cmp_type,
2216 			       (struct rx_tpa_start_cmp *)rxcmp,
2217 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2218 
2219 		*event |= BNXT_RX_EVENT;
2220 		goto next_rx_no_prod_no_len;
2221 
2222 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2223 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2224 				   (struct rx_tpa_end_cmp *)rxcmp,
2225 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2226 
2227 		if (IS_ERR(skb))
2228 			return -EBUSY;
2229 
2230 		rc = -ENOMEM;
2231 		if (likely(skb)) {
2232 			bnxt_deliver_skb(bp, bnapi, skb);
2233 			rc = 1;
2234 		}
2235 		*event |= BNXT_RX_EVENT;
2236 		goto next_rx_no_prod_no_len;
2237 	}
2238 
2239 	cons = rxcmp->rx_cmp_opaque;
2240 	if (unlikely(cons != rxr->rx_next_cons)) {
2241 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2242 
2243 		/* 0xffff is forced error, don't print it */
2244 		if (rxr->rx_next_cons != 0xffff)
2245 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2246 				    cons, rxr->rx_next_cons);
2247 		bnxt_sched_reset_rxr(bp, rxr);
2248 		if (rc1)
2249 			return rc1;
2250 		goto next_rx_no_prod_no_len;
2251 	}
2252 	rx_buf = &rxr->rx_buf_ring[cons];
2253 	data = rx_buf->data;
2254 	data_ptr = rx_buf->data_ptr;
2255 	prefetch(data_ptr);
2256 
2257 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2258 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2259 
2260 	if (agg_bufs) {
2261 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2262 			return -EBUSY;
2263 
2264 		cp_cons = NEXT_CMP(cp_cons);
2265 		*event |= BNXT_AGG_EVENT;
2266 	}
2267 	*event |= BNXT_RX_EVENT;
2268 
2269 	rx_buf->data = NULL;
2270 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2271 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2272 
2273 		bnxt_reuse_rx_data(rxr, cons, data);
2274 		if (agg_bufs)
2275 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2276 					       false);
2277 
2278 		rc = -EIO;
2279 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2280 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2281 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2282 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2283 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2284 						 rx_err);
2285 				bnxt_sched_reset_rxr(bp, rxr);
2286 			}
2287 		}
2288 		goto next_rx_no_len;
2289 	}
2290 
2291 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2292 	len = flags >> RX_CMP_LEN_SHIFT;
2293 	dma_addr = rx_buf->mapping;
2294 
2295 	if (bnxt_xdp_attached(bp, rxr)) {
2296 		bnxt_xdp.rxcmp = rxcmp;
2297 		bnxt_xdp.rxcmp1 = rxcmp1;
2298 		bnxt_xdp.cmp_type = cmp_type;
2299 
2300 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &bnxt_xdp.xdp);
2301 		if (agg_bufs) {
2302 			u32 frag_len = bnxt_rx_agg_netmems_xdp(bp, cpr,
2303 							       &bnxt_xdp.xdp,
2304 							       cp_cons,
2305 							       agg_bufs,
2306 							       false);
2307 			if (!frag_len) {
2308 				bnxt_reuse_rx_data(rxr, cons, data);
2309 				goto oom_next_rx;
2310 			}
2311 
2312 		}
2313 		xdp_active = true;
2314 	}
2315 
2316 	if (xdp_active) {
2317 		if (bnxt_rx_xdp(bp, rxr, cons, &bnxt_xdp.xdp, data, &data_ptr,
2318 				&len, event)) {
2319 			rc = 1;
2320 			goto next_rx;
2321 		}
2322 		if (xdp_buff_has_frags(&bnxt_xdp.xdp)) {
2323 			sinfo = xdp_get_shared_info_from_buff(&bnxt_xdp.xdp);
2324 			agg_bufs = sinfo->nr_frags;
2325 		} else {
2326 			agg_bufs = 0;
2327 		}
2328 	}
2329 
2330 	if (len <= bp->rx_copybreak) {
2331 		if (!xdp_active)
2332 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2333 		else
2334 			skb = bnxt_copy_xdp(bnapi, &bnxt_xdp.xdp, len,
2335 					    dma_addr);
2336 		bnxt_reuse_rx_data(rxr, cons, data);
2337 		if (!skb)
2338 			goto oom_next_rx_agg;
2339 	} else {
2340 		u32 payload;
2341 
2342 		if (rx_buf->data_ptr == data_ptr)
2343 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2344 		else
2345 			payload = 0;
2346 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2347 				      payload | len);
2348 		if (!skb)
2349 			goto oom_next_rx_agg;
2350 	}
2351 
2352 	if (agg_bufs) {
2353 		if (!xdp_active) {
2354 			skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, cp_cons,
2355 						      agg_bufs, false);
2356 			if (!skb)
2357 				goto oom_next_rx;
2358 		} else {
2359 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs,
2360 						 rxr, &bnxt_xdp.xdp);
2361 			if (!skb) {
2362 				/* we should be able to free the old skb here */
2363 				bnxt_xdp_buff_frags_free(rxr, &bnxt_xdp.xdp);
2364 				goto oom_next_rx;
2365 			}
2366 		}
2367 	}
2368 
2369 	if (RX_CMP_HASH_VALID(rxcmp)) {
2370 		enum pkt_hash_types type;
2371 
2372 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2373 			type = bnxt_rss_ext_op(bp, rxcmp);
2374 		} else {
2375 			u32 itypes = RX_CMP_ITYPES(rxcmp);
2376 
2377 			if (itypes == RX_CMP_FLAGS_ITYPE_TCP ||
2378 			    itypes == RX_CMP_FLAGS_ITYPE_UDP)
2379 				type = PKT_HASH_TYPE_L4;
2380 			else
2381 				type = PKT_HASH_TYPE_L3;
2382 		}
2383 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2384 	}
2385 
2386 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2387 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2388 	skb->protocol = eth_type_trans(skb, dev);
2389 
2390 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2391 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2392 		if (!skb)
2393 			goto next_rx;
2394 	}
2395 
2396 	skb_checksum_none_assert(skb);
2397 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2398 		if (dev->features & NETIF_F_RXCSUM) {
2399 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2400 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2401 		}
2402 	} else {
2403 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2404 			if (dev->features & NETIF_F_RXCSUM)
2405 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2406 		}
2407 	}
2408 
2409 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2410 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2411 			u64 ns, ts;
2412 
2413 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2414 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2415 
2416 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2417 				memset(skb_hwtstamps(skb), 0,
2418 				       sizeof(*skb_hwtstamps(skb)));
2419 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2420 			}
2421 		}
2422 	}
2423 	bnxt_deliver_skb(bp, bnapi, skb);
2424 	rc = 1;
2425 
2426 next_rx:
2427 	cpr->rx_packets += 1;
2428 	cpr->rx_bytes += len;
2429 
2430 next_rx_no_len:
2431 	rxr->rx_prod = NEXT_RX(prod);
2432 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2433 
2434 next_rx_no_prod_no_len:
2435 	*raw_cons = tmp_raw_cons;
2436 
2437 	return rc;
2438 
2439 oom_next_rx_agg:
2440 	if (agg_bufs) {
2441 		if (!xdp_active)
2442 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2443 					       false);
2444 		else
2445 			bnxt_xdp_buff_frags_free(rxr, &bnxt_xdp.xdp);
2446 	}
2447 oom_next_rx:
2448 	cpr->sw_stats->rx.rx_oom_discards += 1;
2449 	rc = -ENOMEM;
2450 	goto next_rx;
2451 }
2452 
2453 /* In netpoll mode, if we are using a combined completion ring, we need to
2454  * discard the rx packets and recycle the buffers.
2455  */
2456 static int bnxt_force_rx_discard(struct bnxt *bp,
2457 				 struct bnxt_cp_ring_info *cpr,
2458 				 u32 *raw_cons, u8 *event)
2459 {
2460 	u32 tmp_raw_cons = *raw_cons;
2461 	struct rx_cmp_ext *rxcmp1;
2462 	struct rx_cmp *rxcmp;
2463 	u16 cp_cons;
2464 	u8 cmp_type;
2465 	int rc;
2466 
2467 	cp_cons = RING_CMP(tmp_raw_cons);
2468 	rxcmp = (struct rx_cmp *)
2469 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2470 
2471 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2472 	cp_cons = RING_CMP(tmp_raw_cons);
2473 	rxcmp1 = (struct rx_cmp_ext *)
2474 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2475 
2476 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2477 		return -EBUSY;
2478 
2479 	/* The valid test of the entry must be done first before
2480 	 * reading any further.
2481 	 */
2482 	dma_rmb();
2483 	cmp_type = RX_CMP_TYPE(rxcmp);
2484 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2485 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2486 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2487 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2488 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2489 		struct rx_tpa_end_cmp_ext *tpa_end1;
2490 
2491 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2492 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2493 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2494 	}
2495 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2496 	if (rc && rc != -EBUSY)
2497 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2498 	return rc;
2499 }
2500 
2501 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2502 {
2503 	struct bnxt_fw_health *fw_health = bp->fw_health;
2504 	u32 reg = fw_health->regs[reg_idx];
2505 	u32 reg_type, reg_off, val = 0;
2506 
2507 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2508 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2509 	switch (reg_type) {
2510 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2511 		pci_read_config_dword(bp->pdev, reg_off, &val);
2512 		break;
2513 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2514 		reg_off = fw_health->mapped_regs[reg_idx];
2515 		fallthrough;
2516 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2517 		val = readl(bp->bar0 + reg_off);
2518 		break;
2519 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2520 		val = readl(bp->bar1 + reg_off);
2521 		break;
2522 	}
2523 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2524 		val &= fw_health->fw_reset_inprog_reg_mask;
2525 	return val;
2526 }
2527 
2528 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2529 {
2530 	int i;
2531 
2532 	for (i = 0; i < bp->rx_nr_rings; i++) {
2533 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2534 		struct bnxt_ring_grp_info *grp_info;
2535 
2536 		grp_info = &bp->grp_info[grp_idx];
2537 		if (grp_info->agg_fw_ring_id == ring_id)
2538 			return grp_idx;
2539 	}
2540 	return INVALID_HW_RING_ID;
2541 }
2542 
2543 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2544 {
2545 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2546 
2547 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2548 		return link_info->force_link_speed2;
2549 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2550 		return link_info->force_pam4_link_speed;
2551 	return link_info->force_link_speed;
2552 }
2553 
2554 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2555 {
2556 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2557 
2558 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2559 		link_info->req_link_speed = link_info->force_link_speed2;
2560 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2561 		switch (link_info->req_link_speed) {
2562 		case BNXT_LINK_SPEED_50GB_PAM4:
2563 		case BNXT_LINK_SPEED_100GB_PAM4:
2564 		case BNXT_LINK_SPEED_200GB_PAM4:
2565 		case BNXT_LINK_SPEED_400GB_PAM4:
2566 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2567 			break;
2568 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2569 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2570 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2571 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2572 			break;
2573 		default:
2574 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2575 		}
2576 		return;
2577 	}
2578 	link_info->req_link_speed = link_info->force_link_speed;
2579 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2580 	if (link_info->force_pam4_link_speed) {
2581 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2582 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2583 	}
2584 }
2585 
2586 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2587 {
2588 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2589 
2590 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2591 		link_info->advertising = link_info->auto_link_speeds2;
2592 		return;
2593 	}
2594 	link_info->advertising = link_info->auto_link_speeds;
2595 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2596 }
2597 
2598 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2599 {
2600 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2601 
2602 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2603 		if (link_info->req_link_speed != link_info->force_link_speed2)
2604 			return true;
2605 		return false;
2606 	}
2607 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2608 	    link_info->req_link_speed != link_info->force_link_speed)
2609 		return true;
2610 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2611 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2612 		return true;
2613 	return false;
2614 }
2615 
2616 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2617 {
2618 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2619 
2620 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2621 		if (link_info->advertising != link_info->auto_link_speeds2)
2622 			return true;
2623 		return false;
2624 	}
2625 	if (link_info->advertising != link_info->auto_link_speeds ||
2626 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2627 		return true;
2628 	return false;
2629 }
2630 
2631 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type)
2632 {
2633 	u32 flags = bp->ctx->ctx_arr[type].flags;
2634 
2635 	return (flags & BNXT_CTX_MEM_TYPE_VALID) &&
2636 		((flags & BNXT_CTX_MEM_FW_TRACE) ||
2637 		 (flags & BNXT_CTX_MEM_FW_BIN_TRACE));
2638 }
2639 
2640 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm)
2641 {
2642 	u32 mem_size, pages, rem_bytes, magic_byte_offset;
2643 	u16 trace_type = bnxt_bstore_to_trace[ctxm->type];
2644 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
2645 	struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl;
2646 	struct bnxt_bs_trace_info *bs_trace;
2647 	int last_pg;
2648 
2649 	if (ctxm->instance_bmap && ctxm->instance_bmap > 1)
2650 		return;
2651 
2652 	mem_size = ctxm->max_entries * ctxm->entry_size;
2653 	rem_bytes = mem_size % BNXT_PAGE_SIZE;
2654 	pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
2655 
2656 	last_pg = (pages - 1) & (MAX_CTX_PAGES - 1);
2657 	magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1;
2658 
2659 	rmem = &ctx_pg[0].ring_mem;
2660 	bs_trace = &bp->bs_trace[trace_type];
2661 	bs_trace->ctx_type = ctxm->type;
2662 	bs_trace->trace_type = trace_type;
2663 	if (pages > MAX_CTX_PAGES) {
2664 		int last_pg_dir = rmem->nr_pages - 1;
2665 
2666 		rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem;
2667 		bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg];
2668 	} else {
2669 		bs_trace->magic_byte = rmem->pg_arr[last_pg];
2670 	}
2671 	bs_trace->magic_byte += magic_byte_offset;
2672 	*bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE;
2673 }
2674 
2675 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1)				\
2676 	(((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\
2677 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT)
2678 
2679 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2)				\
2680 	(((data2) &							\
2681 	  ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\
2682 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT)
2683 
2684 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2685 	((data2) &							\
2686 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2687 
2688 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2689 	(((data2) &							\
2690 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2691 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2692 
2693 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2694 	((data1) &							\
2695 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2696 
2697 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2698 	(((data1) &							\
2699 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2700 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2701 
2702 /* Return true if the workqueue has to be scheduled */
2703 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2704 {
2705 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2706 
2707 	switch (err_type) {
2708 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2709 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2710 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2711 		break;
2712 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2713 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2714 		break;
2715 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2716 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2717 		break;
2718 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2719 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2720 		char *threshold_type;
2721 		bool notify = false;
2722 		char *dir_str;
2723 
2724 		switch (type) {
2725 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2726 			threshold_type = "warning";
2727 			break;
2728 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2729 			threshold_type = "critical";
2730 			break;
2731 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2732 			threshold_type = "fatal";
2733 			break;
2734 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2735 			threshold_type = "shutdown";
2736 			break;
2737 		default:
2738 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2739 			return false;
2740 		}
2741 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2742 			dir_str = "above";
2743 			notify = true;
2744 		} else {
2745 			dir_str = "below";
2746 		}
2747 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2748 			    dir_str, threshold_type);
2749 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2750 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2751 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2752 		if (notify) {
2753 			bp->thermal_threshold_type = type;
2754 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2755 			return true;
2756 		}
2757 		return false;
2758 	}
2759 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2760 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2761 		break;
2762 	default:
2763 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2764 			   err_type);
2765 		break;
2766 	}
2767 	return false;
2768 }
2769 
2770 #define BNXT_GET_EVENT_PORT(data)	\
2771 	((data) &			\
2772 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2773 
2774 #define BNXT_EVENT_RING_TYPE(data2)	\
2775 	((data2) &			\
2776 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2777 
2778 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2779 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2780 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2781 
2782 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2783 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2784 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2785 
2786 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2787 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2788 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2789 
2790 #define BNXT_PHC_BITS	48
2791 
2792 static int bnxt_async_event_process(struct bnxt *bp,
2793 				    struct hwrm_async_event_cmpl *cmpl)
2794 {
2795 	u16 event_id = le16_to_cpu(cmpl->event_id);
2796 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2797 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2798 
2799 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2800 		   event_id, data1, data2);
2801 
2802 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2803 	switch (event_id) {
2804 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2805 		struct bnxt_link_info *link_info = &bp->link_info;
2806 
2807 		if (BNXT_VF(bp))
2808 			goto async_event_process_exit;
2809 
2810 		/* print unsupported speed warning in forced speed mode only */
2811 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2812 		    (data1 & 0x20000)) {
2813 			u16 fw_speed = bnxt_get_force_speed(link_info);
2814 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2815 
2816 			if (speed != SPEED_UNKNOWN)
2817 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2818 					    speed);
2819 		}
2820 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2821 	}
2822 		fallthrough;
2823 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2824 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2825 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2826 		fallthrough;
2827 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2828 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2829 		break;
2830 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2831 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2832 		break;
2833 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2834 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2835 
2836 		if (BNXT_VF(bp))
2837 			break;
2838 
2839 		if (bp->pf.port_id != port_id)
2840 			break;
2841 
2842 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2843 		break;
2844 	}
2845 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2846 		if (BNXT_PF(bp))
2847 			goto async_event_process_exit;
2848 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2849 		break;
2850 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2851 		char *type_str = "Solicited";
2852 
2853 		if (!bp->fw_health)
2854 			goto async_event_process_exit;
2855 
2856 		bp->fw_reset_timestamp = jiffies;
2857 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2858 		if (!bp->fw_reset_min_dsecs)
2859 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2860 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2861 		if (!bp->fw_reset_max_dsecs)
2862 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2863 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2864 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2865 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2866 			type_str = "Fatal";
2867 			bp->fw_health->fatalities++;
2868 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2869 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2870 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2871 			type_str = "Non-fatal";
2872 			bp->fw_health->survivals++;
2873 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2874 		}
2875 		netif_warn(bp, hw, bp->dev,
2876 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2877 			   type_str, data1, data2,
2878 			   bp->fw_reset_min_dsecs * 100,
2879 			   bp->fw_reset_max_dsecs * 100);
2880 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2881 		break;
2882 	}
2883 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2884 		struct bnxt_fw_health *fw_health = bp->fw_health;
2885 		char *status_desc = "healthy";
2886 		u32 status;
2887 
2888 		if (!fw_health)
2889 			goto async_event_process_exit;
2890 
2891 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2892 			fw_health->enabled = false;
2893 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2894 			break;
2895 		}
2896 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2897 		fw_health->tmr_multiplier =
2898 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2899 				     bp->current_interval * 10);
2900 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2901 		if (!fw_health->enabled)
2902 			fw_health->last_fw_heartbeat =
2903 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2904 		fw_health->last_fw_reset_cnt =
2905 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2906 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2907 		if (status != BNXT_FW_STATUS_HEALTHY)
2908 			status_desc = "unhealthy";
2909 		netif_info(bp, drv, bp->dev,
2910 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2911 			   fw_health->primary ? "primary" : "backup", status,
2912 			   status_desc, fw_health->last_fw_reset_cnt);
2913 		if (!fw_health->enabled) {
2914 			/* Make sure tmr_counter is set and visible to
2915 			 * bnxt_health_check() before setting enabled to true.
2916 			 */
2917 			smp_wmb();
2918 			fw_health->enabled = true;
2919 		}
2920 		goto async_event_process_exit;
2921 	}
2922 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2923 		netif_notice(bp, hw, bp->dev,
2924 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2925 			     data1, data2);
2926 		goto async_event_process_exit;
2927 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2928 		struct bnxt_rx_ring_info *rxr;
2929 		u16 grp_idx;
2930 
2931 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2932 			goto async_event_process_exit;
2933 
2934 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2935 			    BNXT_EVENT_RING_TYPE(data2), data1);
2936 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2937 			goto async_event_process_exit;
2938 
2939 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2940 		if (grp_idx == INVALID_HW_RING_ID) {
2941 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2942 				    data1);
2943 			goto async_event_process_exit;
2944 		}
2945 		rxr = bp->bnapi[grp_idx]->rx_ring;
2946 		bnxt_sched_reset_rxr(bp, rxr);
2947 		goto async_event_process_exit;
2948 	}
2949 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2950 		struct bnxt_fw_health *fw_health = bp->fw_health;
2951 
2952 		netif_notice(bp, hw, bp->dev,
2953 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2954 			     data1, data2);
2955 		if (fw_health) {
2956 			fw_health->echo_req_data1 = data1;
2957 			fw_health->echo_req_data2 = data2;
2958 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2959 			break;
2960 		}
2961 		goto async_event_process_exit;
2962 	}
2963 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2964 		bnxt_ptp_pps_event(bp, data1, data2);
2965 		goto async_event_process_exit;
2966 	}
2967 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2968 		if (bnxt_event_error_report(bp, data1, data2))
2969 			break;
2970 		goto async_event_process_exit;
2971 	}
2972 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2973 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2974 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2975 			if (BNXT_PTP_USE_RTC(bp)) {
2976 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2977 				unsigned long flags;
2978 				u64 ns;
2979 
2980 				if (!ptp)
2981 					goto async_event_process_exit;
2982 
2983 				bnxt_ptp_update_current_time(bp);
2984 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2985 				       BNXT_PHC_BITS) | ptp->current_time);
2986 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2987 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2988 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2989 			}
2990 			break;
2991 		}
2992 		goto async_event_process_exit;
2993 	}
2994 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2995 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2996 
2997 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2998 		goto async_event_process_exit;
2999 	}
3000 	case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: {
3001 		u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1);
3002 		u32 offset =  BNXT_EVENT_BUF_PRODUCER_OFFSET(data2);
3003 
3004 		if (type >= ARRAY_SIZE(bp->bs_trace))
3005 			goto async_event_process_exit;
3006 		bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset);
3007 		goto async_event_process_exit;
3008 	}
3009 	default:
3010 		goto async_event_process_exit;
3011 	}
3012 	__bnxt_queue_sp_work(bp);
3013 async_event_process_exit:
3014 	bnxt_ulp_async_events(bp, cmpl);
3015 	return 0;
3016 }
3017 
3018 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
3019 {
3020 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
3021 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
3022 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
3023 				(struct hwrm_fwd_req_cmpl *)txcmp;
3024 
3025 	switch (cmpl_type) {
3026 	case CMPL_BASE_TYPE_HWRM_DONE:
3027 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
3028 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
3029 		break;
3030 
3031 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
3032 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
3033 
3034 		if ((vf_id < bp->pf.first_vf_id) ||
3035 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
3036 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
3037 				   vf_id);
3038 			return -EINVAL;
3039 		}
3040 
3041 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
3042 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
3043 		break;
3044 
3045 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
3046 		bnxt_async_event_process(bp,
3047 					 (struct hwrm_async_event_cmpl *)txcmp);
3048 		break;
3049 
3050 	default:
3051 		break;
3052 	}
3053 
3054 	return 0;
3055 }
3056 
3057 static bool bnxt_vnic_is_active(struct bnxt *bp)
3058 {
3059 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
3060 
3061 	return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0;
3062 }
3063 
3064 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
3065 {
3066 	struct bnxt_napi *bnapi = dev_instance;
3067 	struct bnxt *bp = bnapi->bp;
3068 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3069 	u32 cons = RING_CMP(cpr->cp_raw_cons);
3070 
3071 	cpr->event_ctr++;
3072 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
3073 	napi_schedule(&bnapi->napi);
3074 	return IRQ_HANDLED;
3075 }
3076 
3077 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
3078 {
3079 	u32 raw_cons = cpr->cp_raw_cons;
3080 	u16 cons = RING_CMP(raw_cons);
3081 	struct tx_cmp *txcmp;
3082 
3083 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3084 
3085 	return TX_CMP_VALID(txcmp, raw_cons);
3086 }
3087 
3088 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3089 			    int budget)
3090 {
3091 	struct bnxt_napi *bnapi = cpr->bnapi;
3092 	u32 raw_cons = cpr->cp_raw_cons;
3093 	bool flush_xdp = false;
3094 	u32 cons;
3095 	int rx_pkts = 0;
3096 	u8 event = 0;
3097 	struct tx_cmp *txcmp;
3098 
3099 	cpr->has_more_work = 0;
3100 	cpr->had_work_done = 1;
3101 	while (1) {
3102 		u8 cmp_type;
3103 		int rc;
3104 
3105 		cons = RING_CMP(raw_cons);
3106 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3107 
3108 		if (!TX_CMP_VALID(txcmp, raw_cons))
3109 			break;
3110 
3111 		/* The valid test of the entry must be done first before
3112 		 * reading any further.
3113 		 */
3114 		dma_rmb();
3115 		cmp_type = TX_CMP_TYPE(txcmp);
3116 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
3117 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
3118 			u32 opaque = txcmp->tx_cmp_opaque;
3119 			struct bnxt_tx_ring_info *txr;
3120 			u16 tx_freed;
3121 
3122 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
3123 			event |= BNXT_TX_CMP_EVENT;
3124 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
3125 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
3126 			else
3127 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
3128 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
3129 				   bp->tx_ring_mask;
3130 			/* return full budget so NAPI will complete. */
3131 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
3132 				rx_pkts = budget;
3133 				raw_cons = NEXT_RAW_CMP(raw_cons);
3134 				if (budget)
3135 					cpr->has_more_work = 1;
3136 				break;
3137 			}
3138 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
3139 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
3140 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
3141 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
3142 			if (likely(budget))
3143 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3144 			else
3145 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
3146 							   &event);
3147 			if (event & BNXT_REDIRECT_EVENT)
3148 				flush_xdp = true;
3149 			if (likely(rc >= 0))
3150 				rx_pkts += rc;
3151 			/* Increment rx_pkts when rc is -ENOMEM to count towards
3152 			 * the NAPI budget.  Otherwise, we may potentially loop
3153 			 * here forever if we consistently cannot allocate
3154 			 * buffers.
3155 			 */
3156 			else if (rc == -ENOMEM && budget)
3157 				rx_pkts++;
3158 			else if (rc == -EBUSY)	/* partial completion */
3159 				break;
3160 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
3161 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
3162 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
3163 			bnxt_hwrm_handler(bp, txcmp);
3164 		}
3165 		raw_cons = NEXT_RAW_CMP(raw_cons);
3166 
3167 		if (rx_pkts && rx_pkts == budget) {
3168 			cpr->has_more_work = 1;
3169 			break;
3170 		}
3171 	}
3172 
3173 	if (flush_xdp) {
3174 		xdp_do_flush();
3175 		event &= ~BNXT_REDIRECT_EVENT;
3176 	}
3177 
3178 	if (event & BNXT_TX_EVENT) {
3179 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3180 		u16 prod = txr->tx_prod;
3181 
3182 		/* Sync BD data before updating doorbell */
3183 		wmb();
3184 
3185 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3186 		event &= ~BNXT_TX_EVENT;
3187 	}
3188 
3189 	cpr->cp_raw_cons = raw_cons;
3190 	bnapi->events |= event;
3191 	return rx_pkts;
3192 }
3193 
3194 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3195 				  int budget)
3196 {
3197 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3198 		bnapi->tx_int(bp, bnapi, budget);
3199 
3200 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3201 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3202 
3203 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3204 		bnapi->events &= ~BNXT_RX_EVENT;
3205 	}
3206 	if (bnapi->events & BNXT_AGG_EVENT) {
3207 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3208 
3209 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3210 		bnapi->events &= ~BNXT_AGG_EVENT;
3211 	}
3212 }
3213 
3214 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3215 			  int budget)
3216 {
3217 	struct bnxt_napi *bnapi = cpr->bnapi;
3218 	int rx_pkts;
3219 
3220 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3221 
3222 	/* ACK completion ring before freeing tx ring and producing new
3223 	 * buffers in rx/agg rings to prevent overflowing the completion
3224 	 * ring.
3225 	 */
3226 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3227 
3228 	__bnxt_poll_work_done(bp, bnapi, budget);
3229 	return rx_pkts;
3230 }
3231 
3232 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3233 {
3234 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3235 	struct bnxt *bp = bnapi->bp;
3236 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3237 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3238 	struct tx_cmp *txcmp;
3239 	struct rx_cmp_ext *rxcmp1;
3240 	u32 cp_cons, tmp_raw_cons;
3241 	u32 raw_cons = cpr->cp_raw_cons;
3242 	bool flush_xdp = false;
3243 	u32 rx_pkts = 0;
3244 	u8 event = 0;
3245 
3246 	while (1) {
3247 		int rc;
3248 
3249 		cp_cons = RING_CMP(raw_cons);
3250 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3251 
3252 		if (!TX_CMP_VALID(txcmp, raw_cons))
3253 			break;
3254 
3255 		/* The valid test of the entry must be done first before
3256 		 * reading any further.
3257 		 */
3258 		dma_rmb();
3259 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3260 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3261 			cp_cons = RING_CMP(tmp_raw_cons);
3262 			rxcmp1 = (struct rx_cmp_ext *)
3263 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3264 
3265 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3266 				break;
3267 
3268 			/* force an error to recycle the buffer */
3269 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3270 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3271 
3272 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3273 			if (likely(rc == -EIO) && budget)
3274 				rx_pkts++;
3275 			else if (rc == -EBUSY)	/* partial completion */
3276 				break;
3277 			if (event & BNXT_REDIRECT_EVENT)
3278 				flush_xdp = true;
3279 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3280 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3281 			bnxt_hwrm_handler(bp, txcmp);
3282 		} else {
3283 			netdev_err(bp->dev,
3284 				   "Invalid completion received on special ring\n");
3285 		}
3286 		raw_cons = NEXT_RAW_CMP(raw_cons);
3287 
3288 		if (rx_pkts == budget)
3289 			break;
3290 	}
3291 
3292 	cpr->cp_raw_cons = raw_cons;
3293 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3294 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3295 
3296 	if (event & BNXT_AGG_EVENT)
3297 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3298 	if (flush_xdp)
3299 		xdp_do_flush();
3300 
3301 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3302 		napi_complete_done(napi, rx_pkts);
3303 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3304 	}
3305 	return rx_pkts;
3306 }
3307 
3308 static int bnxt_poll(struct napi_struct *napi, int budget)
3309 {
3310 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3311 	struct bnxt *bp = bnapi->bp;
3312 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3313 	int work_done = 0;
3314 
3315 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3316 		napi_complete(napi);
3317 		return 0;
3318 	}
3319 	while (1) {
3320 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3321 
3322 		if (work_done >= budget) {
3323 			if (!budget)
3324 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3325 			break;
3326 		}
3327 
3328 		if (!bnxt_has_work(bp, cpr)) {
3329 			if (napi_complete_done(napi, work_done))
3330 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3331 			break;
3332 		}
3333 	}
3334 	if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3335 		struct dim_sample dim_sample = {};
3336 
3337 		dim_update_sample(cpr->event_ctr,
3338 				  cpr->rx_packets,
3339 				  cpr->rx_bytes,
3340 				  &dim_sample);
3341 		net_dim(&cpr->dim, &dim_sample);
3342 	}
3343 	return work_done;
3344 }
3345 
3346 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3347 {
3348 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3349 	int i, work_done = 0;
3350 
3351 	for (i = 0; i < cpr->cp_ring_count; i++) {
3352 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3353 
3354 		if (cpr2->had_nqe_notify) {
3355 			work_done += __bnxt_poll_work(bp, cpr2,
3356 						      budget - work_done);
3357 			cpr->has_more_work |= cpr2->has_more_work;
3358 		}
3359 	}
3360 	return work_done;
3361 }
3362 
3363 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3364 				 u64 dbr_type, int budget)
3365 {
3366 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3367 	int i;
3368 
3369 	for (i = 0; i < cpr->cp_ring_count; i++) {
3370 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3371 		struct bnxt_db_info *db;
3372 
3373 		if (cpr2->had_work_done) {
3374 			u32 tgl = 0;
3375 
3376 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3377 				cpr2->had_nqe_notify = 0;
3378 				tgl = cpr2->toggle;
3379 			}
3380 			db = &cpr2->cp_db;
3381 			bnxt_writeq(bp,
3382 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3383 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3384 				    db->doorbell);
3385 			cpr2->had_work_done = 0;
3386 		}
3387 	}
3388 	__bnxt_poll_work_done(bp, bnapi, budget);
3389 }
3390 
3391 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3392 {
3393 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3394 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3395 	struct bnxt_cp_ring_info *cpr_rx;
3396 	u32 raw_cons = cpr->cp_raw_cons;
3397 	struct bnxt *bp = bnapi->bp;
3398 	struct nqe_cn *nqcmp;
3399 	int work_done = 0;
3400 	u32 cons;
3401 
3402 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3403 		napi_complete(napi);
3404 		return 0;
3405 	}
3406 	if (cpr->has_more_work) {
3407 		cpr->has_more_work = 0;
3408 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3409 	}
3410 	while (1) {
3411 		u16 type;
3412 
3413 		cons = RING_CMP(raw_cons);
3414 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3415 
3416 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3417 			if (cpr->has_more_work)
3418 				break;
3419 
3420 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3421 					     budget);
3422 			cpr->cp_raw_cons = raw_cons;
3423 			if (napi_complete_done(napi, work_done))
3424 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3425 						  cpr->cp_raw_cons);
3426 			goto poll_done;
3427 		}
3428 
3429 		/* The valid test of the entry must be done first before
3430 		 * reading any further.
3431 		 */
3432 		dma_rmb();
3433 
3434 		type = le16_to_cpu(nqcmp->type);
3435 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3436 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3437 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3438 			struct bnxt_cp_ring_info *cpr2;
3439 
3440 			/* No more budget for RX work */
3441 			if (budget && work_done >= budget &&
3442 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3443 				break;
3444 
3445 			idx = BNXT_NQ_HDL_IDX(idx);
3446 			cpr2 = &cpr->cp_ring_arr[idx];
3447 			cpr2->had_nqe_notify = 1;
3448 			cpr2->toggle = NQE_CN_TOGGLE(type);
3449 			work_done += __bnxt_poll_work(bp, cpr2,
3450 						      budget - work_done);
3451 			cpr->has_more_work |= cpr2->has_more_work;
3452 		} else {
3453 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3454 		}
3455 		raw_cons = NEXT_RAW_CMP(raw_cons);
3456 	}
3457 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3458 	if (raw_cons != cpr->cp_raw_cons) {
3459 		cpr->cp_raw_cons = raw_cons;
3460 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3461 	}
3462 poll_done:
3463 	cpr_rx = &cpr->cp_ring_arr[0];
3464 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3465 	    (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3466 		struct dim_sample dim_sample = {};
3467 
3468 		dim_update_sample(cpr->event_ctr,
3469 				  cpr_rx->rx_packets,
3470 				  cpr_rx->rx_bytes,
3471 				  &dim_sample);
3472 		net_dim(&cpr->dim, &dim_sample);
3473 	}
3474 	return work_done;
3475 }
3476 
3477 static void bnxt_free_one_tx_ring_skbs(struct bnxt *bp,
3478 				       struct bnxt_tx_ring_info *txr, int idx)
3479 {
3480 	int i, max_idx;
3481 	struct pci_dev *pdev = bp->pdev;
3482 	unsigned int dma_len;
3483 	dma_addr_t dma_addr;
3484 
3485 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3486 
3487 	for (i = 0; i < max_idx;) {
3488 		struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[i];
3489 		struct bnxt_sw_tx_bd *head_buf = tx_buf;
3490 		struct sk_buff *skb;
3491 		int j, last;
3492 
3493 		if (idx  < bp->tx_nr_rings_xdp &&
3494 		    tx_buf->action == XDP_REDIRECT) {
3495 			dma_addr = dma_unmap_addr(tx_buf, mapping);
3496 			dma_len = dma_unmap_len(tx_buf, len);
3497 
3498 			dma_unmap_single(&pdev->dev, dma_addr, dma_len,
3499 					 DMA_TO_DEVICE);
3500 			xdp_return_frame(tx_buf->xdpf);
3501 			tx_buf->action = 0;
3502 			tx_buf->xdpf = NULL;
3503 			i++;
3504 			continue;
3505 		}
3506 
3507 		skb = tx_buf->skb;
3508 		if (!skb) {
3509 			i++;
3510 			continue;
3511 		}
3512 
3513 		tx_buf->skb = NULL;
3514 
3515 		if (tx_buf->is_push) {
3516 			dev_kfree_skb(skb);
3517 			i += 2;
3518 			continue;
3519 		}
3520 
3521 		if (dma_unmap_len(tx_buf, len)) {
3522 			dma_addr = dma_unmap_addr(tx_buf, mapping);
3523 			dma_len = dma_unmap_len(tx_buf, len);
3524 
3525 			dma_unmap_single(&pdev->dev, dma_addr, dma_len,
3526 					 DMA_TO_DEVICE);
3527 		}
3528 
3529 		last = tx_buf->nr_frags;
3530 		i += 2;
3531 		for (j = 0; j < last; j++, i++) {
3532 			int ring_idx = i & bp->tx_ring_mask;
3533 
3534 			tx_buf = &txr->tx_buf_ring[ring_idx];
3535 			if (dma_unmap_len(tx_buf, len)) {
3536 				dma_addr = dma_unmap_addr(tx_buf, mapping);
3537 				dma_len = dma_unmap_len(tx_buf, len);
3538 
3539 				netmem_dma_unmap_page_attrs(&pdev->dev,
3540 							    dma_addr, dma_len,
3541 							    DMA_TO_DEVICE, 0);
3542 			}
3543 		}
3544 		if (head_buf->is_sw_gso) {
3545 			u16 inline_cons = txr->tx_inline_cons + 1;
3546 
3547 			WRITE_ONCE(txr->tx_inline_cons, inline_cons);
3548 			if (head_buf->is_sw_gso == BNXT_SW_GSO_LAST) {
3549 				tso_dma_map_complete(&pdev->dev,
3550 						     &head_buf->sw_gso_cstate);
3551 			} else {
3552 				skb = NULL;
3553 			}
3554 			head_buf->is_sw_gso = 0;
3555 		}
3556 		if (skb)
3557 			dev_kfree_skb(skb);
3558 	}
3559 	netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, idx));
3560 }
3561 
3562 static void bnxt_free_tx_skbs(struct bnxt *bp)
3563 {
3564 	int i;
3565 
3566 	if (!bp->tx_ring)
3567 		return;
3568 
3569 	for (i = 0; i < bp->tx_nr_rings; i++) {
3570 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3571 
3572 		if (!txr->tx_buf_ring)
3573 			continue;
3574 
3575 		bnxt_free_one_tx_ring_skbs(bp, txr, i);
3576 	}
3577 
3578 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
3579 		bnxt_ptp_free_txts_skbs(bp->ptp_cfg);
3580 }
3581 
3582 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3583 {
3584 	int i, max_idx;
3585 
3586 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3587 
3588 	for (i = 0; i < max_idx; i++) {
3589 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3590 		void *data = rx_buf->data;
3591 
3592 		if (!data)
3593 			continue;
3594 
3595 		rx_buf->data = NULL;
3596 		if (BNXT_RX_PAGE_MODE(bp))
3597 			page_pool_recycle_direct(rxr->page_pool, data);
3598 		else
3599 			page_pool_free_va(rxr->head_pool, data, true);
3600 	}
3601 }
3602 
3603 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3604 {
3605 	int i, max_idx;
3606 
3607 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3608 
3609 	for (i = 0; i < max_idx; i++) {
3610 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3611 		netmem_ref netmem = rx_agg_buf->netmem;
3612 
3613 		if (!netmem)
3614 			continue;
3615 
3616 		rx_agg_buf->netmem = 0;
3617 		__clear_bit(i, rxr->rx_agg_bmap);
3618 
3619 		page_pool_recycle_direct_netmem(rxr->page_pool, netmem);
3620 	}
3621 }
3622 
3623 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3624 					struct bnxt_rx_ring_info *rxr)
3625 {
3626 	int i;
3627 
3628 	for (i = 0; i < bp->max_tpa_roundup_size; i++) {
3629 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3630 		u8 *data = tpa_info->data;
3631 
3632 		if (!data)
3633 			continue;
3634 
3635 		tpa_info->data = NULL;
3636 		page_pool_free_va(rxr->head_pool, data, false);
3637 	}
3638 }
3639 
3640 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3641 				       struct bnxt_rx_ring_info *rxr)
3642 {
3643 	struct bnxt_tpa_idx_map *map;
3644 
3645 	if (!rxr->rx_tpa)
3646 		goto skip_rx_tpa_free;
3647 
3648 	bnxt_free_one_tpa_info_data(bp, rxr);
3649 
3650 skip_rx_tpa_free:
3651 	if (!rxr->rx_buf_ring)
3652 		goto skip_rx_buf_free;
3653 
3654 	bnxt_free_one_rx_ring(bp, rxr);
3655 
3656 skip_rx_buf_free:
3657 	if (!rxr->rx_agg_ring)
3658 		goto skip_rx_agg_free;
3659 
3660 	bnxt_free_one_rx_agg_ring(bp, rxr);
3661 
3662 skip_rx_agg_free:
3663 	map = rxr->rx_tpa_idx_map;
3664 	if (map)
3665 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3666 }
3667 
3668 static void bnxt_free_rx_skbs(struct bnxt *bp)
3669 {
3670 	int i;
3671 
3672 	if (!bp->rx_ring)
3673 		return;
3674 
3675 	for (i = 0; i < bp->rx_nr_rings; i++)
3676 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3677 }
3678 
3679 static void bnxt_free_skbs(struct bnxt *bp)
3680 {
3681 	bnxt_free_tx_skbs(bp);
3682 	bnxt_free_rx_skbs(bp);
3683 }
3684 
3685 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3686 {
3687 	u8 init_val = ctxm->init_value;
3688 	u16 offset = ctxm->init_offset;
3689 	u8 *p2 = p;
3690 	int i;
3691 
3692 	if (!init_val)
3693 		return;
3694 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3695 		memset(p, init_val, len);
3696 		return;
3697 	}
3698 	for (i = 0; i < len; i += ctxm->entry_size)
3699 		*(p2 + i + offset) = init_val;
3700 }
3701 
3702 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3703 			       void *buf, size_t offset, size_t head,
3704 			       size_t tail)
3705 {
3706 	int i, head_page, start_idx, source_offset;
3707 	size_t len, rem_len, total_len, max_bytes;
3708 
3709 	head_page = head / rmem->page_size;
3710 	source_offset = head % rmem->page_size;
3711 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3712 	if (!total_len)
3713 		total_len = MAX_CTX_BYTES;
3714 	start_idx = head_page % MAX_CTX_PAGES;
3715 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3716 		    source_offset;
3717 	total_len = min(total_len, max_bytes);
3718 	rem_len = total_len;
3719 
3720 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3721 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3722 		if (buf)
3723 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3724 			       len);
3725 		offset += len;
3726 		rem_len -= len;
3727 	}
3728 	return total_len;
3729 }
3730 
3731 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3732 {
3733 	struct pci_dev *pdev = bp->pdev;
3734 	int i;
3735 
3736 	if (!rmem->pg_arr)
3737 		goto skip_pages;
3738 
3739 	for (i = 0; i < rmem->nr_pages; i++) {
3740 		if (!rmem->pg_arr[i])
3741 			continue;
3742 
3743 		dma_free_coherent(&pdev->dev, rmem->page_size,
3744 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3745 
3746 		rmem->pg_arr[i] = NULL;
3747 	}
3748 skip_pages:
3749 	if (rmem->pg_tbl) {
3750 		size_t pg_tbl_size = rmem->nr_pages * 8;
3751 
3752 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3753 			pg_tbl_size = rmem->page_size;
3754 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3755 				  rmem->pg_tbl, rmem->pg_tbl_map);
3756 		rmem->pg_tbl = NULL;
3757 	}
3758 	if (rmem->vmem_size && *rmem->vmem) {
3759 		vfree(*rmem->vmem);
3760 		*rmem->vmem = NULL;
3761 	}
3762 }
3763 
3764 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3765 {
3766 	struct pci_dev *pdev = bp->pdev;
3767 	u64 valid_bit = 0;
3768 	int i;
3769 
3770 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3771 		valid_bit = PTU_PTE_VALID;
3772 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3773 		size_t pg_tbl_size = rmem->nr_pages * 8;
3774 
3775 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3776 			pg_tbl_size = rmem->page_size;
3777 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3778 						  &rmem->pg_tbl_map,
3779 						  GFP_KERNEL);
3780 		if (!rmem->pg_tbl)
3781 			return -ENOMEM;
3782 	}
3783 
3784 	for (i = 0; i < rmem->nr_pages; i++) {
3785 		u64 extra_bits = valid_bit;
3786 
3787 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3788 						     rmem->page_size,
3789 						     &rmem->dma_arr[i],
3790 						     GFP_KERNEL);
3791 		if (!rmem->pg_arr[i])
3792 			return -ENOMEM;
3793 
3794 		if (rmem->ctx_mem)
3795 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3796 					  rmem->page_size);
3797 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3798 			if (i == rmem->nr_pages - 2 &&
3799 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3800 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3801 			else if (i == rmem->nr_pages - 1 &&
3802 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3803 				extra_bits |= PTU_PTE_LAST;
3804 			rmem->pg_tbl[i] =
3805 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3806 		}
3807 	}
3808 
3809 	if (rmem->vmem_size) {
3810 		*rmem->vmem = vzalloc(rmem->vmem_size);
3811 		if (!(*rmem->vmem))
3812 			return -ENOMEM;
3813 	}
3814 	return 0;
3815 }
3816 
3817 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3818 				   struct bnxt_rx_ring_info *rxr)
3819 {
3820 	int i;
3821 
3822 	kfree(rxr->rx_tpa_idx_map);
3823 	rxr->rx_tpa_idx_map = NULL;
3824 	if (rxr->rx_tpa) {
3825 		for (i = 0; i < bp->max_tpa_roundup_size; i++) {
3826 			kfree(rxr->rx_tpa[i].agg_arr);
3827 			rxr->rx_tpa[i].agg_arr = NULL;
3828 		}
3829 	}
3830 	kfree(rxr->rx_tpa);
3831 	rxr->rx_tpa = NULL;
3832 }
3833 
3834 static void bnxt_free_tpa_info(struct bnxt *bp)
3835 {
3836 	int i;
3837 
3838 	for (i = 0; i < bp->rx_nr_rings; i++) {
3839 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3840 
3841 		bnxt_free_one_tpa_info(bp, rxr);
3842 	}
3843 }
3844 
3845 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3846 				   struct bnxt_rx_ring_info *rxr)
3847 {
3848 	struct rx_agg_cmp *agg;
3849 	int i;
3850 
3851 	rxr->rx_tpa = kzalloc_objs(struct bnxt_tpa_info,
3852 				   bp->max_tpa_roundup_size);
3853 	if (!rxr->rx_tpa)
3854 		return -ENOMEM;
3855 
3856 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3857 		return 0;
3858 	for (i = 0; i < bp->max_tpa_roundup_size; i++) {
3859 		agg = kzalloc_objs(*agg, MAX_SKB_FRAGS);
3860 		if (!agg)
3861 			return -ENOMEM;
3862 		rxr->rx_tpa[i].agg_arr = agg;
3863 	}
3864 	rxr->rx_tpa_idx_map = kzalloc_obj(*rxr->rx_tpa_idx_map);
3865 	if (!rxr->rx_tpa_idx_map)
3866 		return -ENOMEM;
3867 
3868 	return 0;
3869 }
3870 
3871 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3872 {
3873 	int i, rc;
3874 
3875 	bp->max_tpa = MAX_TPA;
3876 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3877 		/* TPA is not supported at all, so there is nothing to
3878 		 * allocate.
3879 		 */
3880 		if (!bp->max_tpa_v2)
3881 			return 0;
3882 		bp->max_tpa = min_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3883 		/* Older P5 FW sets max_tpa_v2 low by mistake except NPAR */
3884 		if (bp->max_tpa <= 32 && BNXT_CHIP_P5(bp) && !BNXT_NPAR(bp))
3885 			bp->max_tpa = MAX_TPA_P5;
3886 	}
3887 	bp->max_tpa_roundup_size = roundup_pow_of_two(bp->max_tpa);
3888 
3889 	for (i = 0; i < bp->rx_nr_rings; i++) {
3890 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3891 
3892 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3893 		if (rc)
3894 			return rc;
3895 	}
3896 	return 0;
3897 }
3898 
3899 static void bnxt_free_rx_rings(struct bnxt *bp)
3900 {
3901 	int i;
3902 
3903 	if (!bp->rx_ring)
3904 		return;
3905 
3906 	bnxt_free_tpa_info(bp);
3907 	for (i = 0; i < bp->rx_nr_rings; i++) {
3908 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3909 		struct bnxt_ring_struct *ring;
3910 
3911 		if (rxr->xdp_prog)
3912 			bpf_prog_put(rxr->xdp_prog);
3913 
3914 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3915 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3916 
3917 		page_pool_destroy(rxr->page_pool);
3918 		page_pool_destroy(rxr->head_pool);
3919 		rxr->page_pool = rxr->head_pool = NULL;
3920 
3921 		kfree(rxr->rx_agg_bmap);
3922 		rxr->rx_agg_bmap = NULL;
3923 
3924 		ring = &rxr->rx_ring_struct;
3925 		bnxt_free_ring(bp, &ring->ring_mem);
3926 
3927 		ring = &rxr->rx_agg_ring_struct;
3928 		bnxt_free_ring(bp, &ring->ring_mem);
3929 	}
3930 }
3931 
3932 static int bnxt_rx_agg_ring_fill_level(struct bnxt *bp,
3933 				       struct bnxt_rx_ring_info *rxr)
3934 {
3935 	/* User may have chosen larger than default rx_page_size,
3936 	 * we keep the ring sizes uniform and also want uniform amount
3937 	 * of bytes consumed per ring, so cap how much of the rings we fill.
3938 	 */
3939 	int fill_level = bp->rx_agg_ring_size;
3940 
3941 	if (rxr->rx_page_size > BNXT_RX_PAGE_SIZE)
3942 		fill_level /= rxr->rx_page_size / BNXT_RX_PAGE_SIZE;
3943 
3944 	return fill_level;
3945 }
3946 
3947 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3948 				   struct bnxt_rx_ring_info *rxr,
3949 				   int numa_node)
3950 {
3951 	unsigned int agg_size_fac = rxr->rx_page_size / BNXT_RX_PAGE_SIZE;
3952 	const unsigned int rx_size_fac = PAGE_SIZE / SZ_4K;
3953 	struct page_pool_params pp = { 0 };
3954 	struct page_pool *pool;
3955 
3956 	pp.pool_size = bnxt_rx_agg_ring_fill_level(bp, rxr) / agg_size_fac;
3957 	if (BNXT_RX_PAGE_MODE(bp))
3958 		pp.pool_size += bp->rx_ring_size / rx_size_fac;
3959 
3960 	pp.order = get_order(rxr->rx_page_size);
3961 	pp.nid = numa_node;
3962 	pp.netdev = bp->dev;
3963 	pp.dev = &bp->pdev->dev;
3964 	pp.dma_dir = bp->rx_dir;
3965 	pp.max_len = PAGE_SIZE << pp.order;
3966 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV |
3967 		   PP_FLAG_ALLOW_UNREADABLE_NETMEM;
3968 	pp.queue_idx = rxr->bnapi->index;
3969 
3970 	pool = page_pool_create(&pp);
3971 	if (IS_ERR(pool))
3972 		return PTR_ERR(pool);
3973 	rxr->page_pool = pool;
3974 
3975 	rxr->need_head_pool = page_pool_is_unreadable(pool);
3976 	rxr->need_head_pool |= !!pp.order;
3977 	if (bnxt_separate_head_pool(rxr)) {
3978 		pp.order = 0;
3979 		pp.max_len = PAGE_SIZE;
3980 		pp.pool_size = min(bp->rx_ring_size / rx_size_fac, 1024);
3981 		pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3982 		pool = page_pool_create(&pp);
3983 		if (IS_ERR(pool))
3984 			goto err_destroy_pp;
3985 	} else {
3986 		page_pool_get(pool);
3987 	}
3988 	rxr->head_pool = pool;
3989 
3990 	return 0;
3991 
3992 err_destroy_pp:
3993 	page_pool_destroy(rxr->page_pool);
3994 	rxr->page_pool = NULL;
3995 	return PTR_ERR(pool);
3996 }
3997 
3998 static void bnxt_enable_rx_page_pool(struct bnxt_rx_ring_info *rxr)
3999 {
4000 	page_pool_enable_direct_recycling(rxr->head_pool, &rxr->bnapi->napi);
4001 	page_pool_enable_direct_recycling(rxr->page_pool, &rxr->bnapi->napi);
4002 }
4003 
4004 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
4005 {
4006 	u16 mem_size;
4007 
4008 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
4009 	mem_size = rxr->rx_agg_bmap_size / 8;
4010 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
4011 	if (!rxr->rx_agg_bmap)
4012 		return -ENOMEM;
4013 
4014 	return 0;
4015 }
4016 
4017 static int bnxt_alloc_rx_rings(struct bnxt *bp)
4018 {
4019 	int numa_node = dev_to_node(&bp->pdev->dev);
4020 	int i, rc = 0, agg_rings = 0, cpu;
4021 
4022 	if (!bp->rx_ring)
4023 		return -ENOMEM;
4024 
4025 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
4026 		agg_rings = 1;
4027 
4028 	for (i = 0; i < bp->rx_nr_rings; i++) {
4029 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
4030 		struct bnxt_ring_struct *ring;
4031 		int cpu_node;
4032 
4033 		ring = &rxr->rx_ring_struct;
4034 
4035 		cpu = cpumask_local_spread(i, numa_node);
4036 		cpu_node = cpu_to_node(cpu);
4037 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
4038 			   i, cpu_node);
4039 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
4040 		if (rc)
4041 			return rc;
4042 		bnxt_enable_rx_page_pool(rxr);
4043 
4044 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
4045 		if (rc < 0)
4046 			return rc;
4047 
4048 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
4049 						MEM_TYPE_PAGE_POOL,
4050 						rxr->page_pool);
4051 		if (rc) {
4052 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
4053 			return rc;
4054 		}
4055 
4056 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4057 		if (rc)
4058 			return rc;
4059 
4060 		ring->grp_idx = i;
4061 		if (agg_rings) {
4062 			ring = &rxr->rx_agg_ring_struct;
4063 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4064 			if (rc)
4065 				return rc;
4066 
4067 			ring->grp_idx = i;
4068 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
4069 			if (rc)
4070 				return rc;
4071 		}
4072 	}
4073 	if (bp->flags & BNXT_FLAG_TPA)
4074 		rc = bnxt_alloc_tpa_info(bp);
4075 	return rc;
4076 }
4077 
4078 static void bnxt_free_tx_inline_buf(struct bnxt_tx_ring_info *txr,
4079 				    struct pci_dev *pdev)
4080 {
4081 	if (!txr->tx_inline_buf)
4082 		return;
4083 
4084 	dma_unmap_single(&pdev->dev, txr->tx_inline_dma,
4085 			 txr->tx_inline_size, DMA_TO_DEVICE);
4086 	kfree(txr->tx_inline_buf);
4087 	txr->tx_inline_buf = NULL;
4088 	txr->tx_inline_size = 0;
4089 }
4090 
4091 static int bnxt_alloc_tx_inline_buf(struct bnxt_tx_ring_info *txr,
4092 				    struct pci_dev *pdev,
4093 				    unsigned int size)
4094 {
4095 	txr->tx_inline_buf = kmalloc(size, GFP_KERNEL);
4096 	if (!txr->tx_inline_buf)
4097 		return -ENOMEM;
4098 
4099 	txr->tx_inline_dma = dma_map_single(&pdev->dev, txr->tx_inline_buf,
4100 					    size, DMA_TO_DEVICE);
4101 	if (dma_mapping_error(&pdev->dev, txr->tx_inline_dma)) {
4102 		kfree(txr->tx_inline_buf);
4103 		txr->tx_inline_buf = NULL;
4104 		return -ENOMEM;
4105 	}
4106 	txr->tx_inline_size = size;
4107 
4108 	return 0;
4109 }
4110 
4111 static void bnxt_free_tx_rings(struct bnxt *bp)
4112 {
4113 	int i;
4114 	struct pci_dev *pdev = bp->pdev;
4115 
4116 	if (!bp->tx_ring)
4117 		return;
4118 
4119 	for (i = 0; i < bp->tx_nr_rings; i++) {
4120 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4121 		struct bnxt_ring_struct *ring;
4122 
4123 		if (txr->tx_push) {
4124 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
4125 					  txr->tx_push, txr->tx_push_mapping);
4126 			txr->tx_push = NULL;
4127 		}
4128 
4129 		bnxt_free_tx_inline_buf(txr, pdev);
4130 
4131 		ring = &txr->tx_ring_struct;
4132 
4133 		bnxt_free_ring(bp, &ring->ring_mem);
4134 	}
4135 }
4136 
4137 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
4138 	((tc) * (bp)->tx_nr_rings_per_tc)
4139 
4140 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
4141 	((tx) % (bp)->tx_nr_rings_per_tc)
4142 
4143 #define BNXT_RING_TO_TC(bp, tx)		\
4144 	((tx) / (bp)->tx_nr_rings_per_tc)
4145 
4146 static int bnxt_alloc_tx_rings(struct bnxt *bp)
4147 {
4148 	int i, j, rc;
4149 	struct pci_dev *pdev = bp->pdev;
4150 
4151 	bp->tx_push_size = 0;
4152 	if (bp->tx_push_thresh) {
4153 		int push_size;
4154 
4155 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
4156 					bp->tx_push_thresh);
4157 
4158 		if (push_size > 256) {
4159 			push_size = 0;
4160 			bp->tx_push_thresh = 0;
4161 		}
4162 
4163 		bp->tx_push_size = push_size;
4164 	}
4165 
4166 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
4167 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4168 		struct bnxt_ring_struct *ring;
4169 		u8 qidx;
4170 
4171 		ring = &txr->tx_ring_struct;
4172 
4173 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4174 		if (rc)
4175 			return rc;
4176 
4177 		ring->grp_idx = txr->bnapi->index;
4178 		if (bp->tx_push_size) {
4179 			dma_addr_t mapping;
4180 
4181 			/* One pre-allocated DMA buffer to backup
4182 			 * TX push operation
4183 			 */
4184 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
4185 						bp->tx_push_size,
4186 						&txr->tx_push_mapping,
4187 						GFP_KERNEL);
4188 
4189 			if (!txr->tx_push)
4190 				return -ENOMEM;
4191 
4192 			mapping = txr->tx_push_mapping +
4193 				sizeof(struct tx_push_bd);
4194 			txr->data_mapping = cpu_to_le64(mapping);
4195 		}
4196 		if (!(bp->flags & BNXT_FLAG_UDP_GSO_CAP)) {
4197 			rc = bnxt_alloc_tx_inline_buf(txr, pdev,
4198 						      BNXT_SW_USO_MAX_SEGS *
4199 						      TSO_HEADER_SIZE);
4200 			if (rc)
4201 				return rc;
4202 		}
4203 		qidx = bp->tc_to_qidx[j];
4204 		ring->queue_id = bp->q_info[qidx].queue_id;
4205 		spin_lock_init(&txr->xdp_tx_lock);
4206 		if (i < bp->tx_nr_rings_xdp)
4207 			continue;
4208 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
4209 			j++;
4210 	}
4211 	return 0;
4212 }
4213 
4214 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
4215 {
4216 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4217 
4218 	kfree(cpr->cp_desc_ring);
4219 	cpr->cp_desc_ring = NULL;
4220 	ring->ring_mem.pg_arr = NULL;
4221 	kfree(cpr->cp_desc_mapping);
4222 	cpr->cp_desc_mapping = NULL;
4223 	ring->ring_mem.dma_arr = NULL;
4224 }
4225 
4226 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
4227 {
4228 	cpr->cp_desc_ring = kzalloc_objs(*cpr->cp_desc_ring, n);
4229 	if (!cpr->cp_desc_ring)
4230 		return -ENOMEM;
4231 	cpr->cp_desc_mapping = kzalloc_objs(*cpr->cp_desc_mapping, n);
4232 	if (!cpr->cp_desc_mapping)
4233 		return -ENOMEM;
4234 	return 0;
4235 }
4236 
4237 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
4238 {
4239 	int i;
4240 
4241 	if (!bp->bnapi)
4242 		return;
4243 	for (i = 0; i < bp->cp_nr_rings; i++) {
4244 		struct bnxt_napi *bnapi = bp->bnapi[i];
4245 
4246 		if (!bnapi)
4247 			continue;
4248 		bnxt_free_cp_arrays(&bnapi->cp_ring);
4249 	}
4250 }
4251 
4252 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
4253 {
4254 	int i, n = bp->cp_nr_pages;
4255 
4256 	for (i = 0; i < bp->cp_nr_rings; i++) {
4257 		struct bnxt_napi *bnapi = bp->bnapi[i];
4258 		int rc;
4259 
4260 		if (!bnapi)
4261 			continue;
4262 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
4263 		if (rc)
4264 			return rc;
4265 	}
4266 	return 0;
4267 }
4268 
4269 static void bnxt_free_cp_rings(struct bnxt *bp)
4270 {
4271 	int i;
4272 
4273 	if (!bp->bnapi)
4274 		return;
4275 
4276 	for (i = 0; i < bp->cp_nr_rings; i++) {
4277 		struct bnxt_napi *bnapi = bp->bnapi[i];
4278 		struct bnxt_cp_ring_info *cpr;
4279 		struct bnxt_ring_struct *ring;
4280 		int j;
4281 
4282 		if (!bnapi)
4283 			continue;
4284 
4285 		cpr = &bnapi->cp_ring;
4286 		ring = &cpr->cp_ring_struct;
4287 
4288 		bnxt_free_ring(bp, &ring->ring_mem);
4289 
4290 		if (!cpr->cp_ring_arr)
4291 			continue;
4292 
4293 		for (j = 0; j < cpr->cp_ring_count; j++) {
4294 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4295 
4296 			ring = &cpr2->cp_ring_struct;
4297 			bnxt_free_ring(bp, &ring->ring_mem);
4298 			bnxt_free_cp_arrays(cpr2);
4299 		}
4300 		kfree(cpr->cp_ring_arr);
4301 		cpr->cp_ring_arr = NULL;
4302 		cpr->cp_ring_count = 0;
4303 	}
4304 }
4305 
4306 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4307 				  struct bnxt_cp_ring_info *cpr)
4308 {
4309 	struct bnxt_ring_mem_info *rmem;
4310 	struct bnxt_ring_struct *ring;
4311 	int rc;
4312 
4313 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4314 	if (rc) {
4315 		bnxt_free_cp_arrays(cpr);
4316 		return -ENOMEM;
4317 	}
4318 	ring = &cpr->cp_ring_struct;
4319 	rmem = &ring->ring_mem;
4320 	rmem->nr_pages = bp->cp_nr_pages;
4321 	rmem->page_size = HW_CMPD_RING_SIZE;
4322 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4323 	rmem->dma_arr = cpr->cp_desc_mapping;
4324 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4325 	rc = bnxt_alloc_ring(bp, rmem);
4326 	if (rc) {
4327 		bnxt_free_ring(bp, rmem);
4328 		bnxt_free_cp_arrays(cpr);
4329 	}
4330 	return rc;
4331 }
4332 
4333 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4334 {
4335 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4336 	int i, j, rc, ulp_msix;
4337 	int tcs = bp->num_tc;
4338 
4339 	if (!tcs)
4340 		tcs = 1;
4341 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4342 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4343 		struct bnxt_napi *bnapi = bp->bnapi[i];
4344 		struct bnxt_cp_ring_info *cpr, *cpr2;
4345 		struct bnxt_ring_struct *ring;
4346 		int cp_count = 0, k;
4347 		int rx = 0, tx = 0;
4348 
4349 		if (!bnapi)
4350 			continue;
4351 
4352 		cpr = &bnapi->cp_ring;
4353 		cpr->bnapi = bnapi;
4354 		ring = &cpr->cp_ring_struct;
4355 
4356 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4357 		if (rc)
4358 			return rc;
4359 
4360 		ring->map_idx = ulp_msix + i;
4361 
4362 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4363 			continue;
4364 
4365 		if (i < bp->rx_nr_rings) {
4366 			cp_count++;
4367 			rx = 1;
4368 		}
4369 		if (i < bp->tx_nr_rings_xdp) {
4370 			cp_count++;
4371 			tx = 1;
4372 		} else if ((sh && i < bp->tx_nr_rings) ||
4373 			 (!sh && i >= bp->rx_nr_rings)) {
4374 			cp_count += tcs;
4375 			tx = 1;
4376 		}
4377 
4378 		cpr->cp_ring_arr = kzalloc_objs(*cpr, cp_count);
4379 		if (!cpr->cp_ring_arr)
4380 			return -ENOMEM;
4381 		cpr->cp_ring_count = cp_count;
4382 
4383 		for (k = 0; k < cp_count; k++) {
4384 			cpr2 = &cpr->cp_ring_arr[k];
4385 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4386 			if (rc)
4387 				return rc;
4388 			cpr2->bnapi = bnapi;
4389 			cpr2->sw_stats = cpr->sw_stats;
4390 			cpr2->cp_idx = k;
4391 			if (!k && rx) {
4392 				bp->rx_ring[i].rx_cpr = cpr2;
4393 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4394 			} else {
4395 				int n, tc = k - rx;
4396 
4397 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4398 				bp->tx_ring[n].tx_cpr = cpr2;
4399 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4400 			}
4401 		}
4402 		if (tx)
4403 			j++;
4404 	}
4405 	return 0;
4406 }
4407 
4408 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4409 				     struct bnxt_rx_ring_info *rxr)
4410 {
4411 	struct bnxt_ring_mem_info *rmem;
4412 	struct bnxt_ring_struct *ring;
4413 
4414 	ring = &rxr->rx_ring_struct;
4415 	rmem = &ring->ring_mem;
4416 	rmem->nr_pages = bp->rx_nr_pages;
4417 	rmem->page_size = HW_RXBD_RING_SIZE;
4418 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4419 	rmem->dma_arr = rxr->rx_desc_mapping;
4420 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4421 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4422 
4423 	ring = &rxr->rx_agg_ring_struct;
4424 	rmem = &ring->ring_mem;
4425 	rmem->nr_pages = bp->rx_agg_nr_pages;
4426 	rmem->page_size = HW_RXBD_RING_SIZE;
4427 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4428 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4429 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4430 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4431 }
4432 
4433 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4434 				      struct bnxt_rx_ring_info *rxr)
4435 {
4436 	struct bnxt_ring_mem_info *rmem;
4437 	struct bnxt_ring_struct *ring;
4438 	int i;
4439 
4440 	rxr->page_pool->p.napi = NULL;
4441 	rxr->page_pool = NULL;
4442 	rxr->head_pool->p.napi = NULL;
4443 	rxr->head_pool = NULL;
4444 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4445 
4446 	ring = &rxr->rx_ring_struct;
4447 	rmem = &ring->ring_mem;
4448 	rmem->pg_tbl = NULL;
4449 	rmem->pg_tbl_map = 0;
4450 	for (i = 0; i < rmem->nr_pages; i++) {
4451 		rmem->pg_arr[i] = NULL;
4452 		rmem->dma_arr[i] = 0;
4453 	}
4454 	*rmem->vmem = NULL;
4455 
4456 	ring = &rxr->rx_agg_ring_struct;
4457 	rmem = &ring->ring_mem;
4458 	rmem->pg_tbl = NULL;
4459 	rmem->pg_tbl_map = 0;
4460 	for (i = 0; i < rmem->nr_pages; i++) {
4461 		rmem->pg_arr[i] = NULL;
4462 		rmem->dma_arr[i] = 0;
4463 	}
4464 	*rmem->vmem = NULL;
4465 }
4466 
4467 static void bnxt_init_ring_struct(struct bnxt *bp)
4468 {
4469 	int i, j;
4470 
4471 	for (i = 0; i < bp->cp_nr_rings; i++) {
4472 		struct bnxt_napi *bnapi = bp->bnapi[i];
4473 		struct netdev_queue_config qcfg;
4474 		struct bnxt_ring_mem_info *rmem;
4475 		struct bnxt_cp_ring_info *cpr;
4476 		struct bnxt_rx_ring_info *rxr;
4477 		struct bnxt_tx_ring_info *txr;
4478 		struct bnxt_ring_struct *ring;
4479 
4480 		if (!bnapi)
4481 			continue;
4482 
4483 		cpr = &bnapi->cp_ring;
4484 		ring = &cpr->cp_ring_struct;
4485 		rmem = &ring->ring_mem;
4486 		rmem->nr_pages = bp->cp_nr_pages;
4487 		rmem->page_size = HW_CMPD_RING_SIZE;
4488 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4489 		rmem->dma_arr = cpr->cp_desc_mapping;
4490 		rmem->vmem_size = 0;
4491 
4492 		rxr = bnapi->rx_ring;
4493 		if (!rxr)
4494 			goto skip_rx;
4495 
4496 		netdev_queue_config(bp->dev, i, &qcfg);
4497 		rxr->rx_page_size = qcfg.rx_page_size;
4498 
4499 		ring = &rxr->rx_ring_struct;
4500 		rmem = &ring->ring_mem;
4501 		rmem->nr_pages = bp->rx_nr_pages;
4502 		rmem->page_size = HW_RXBD_RING_SIZE;
4503 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4504 		rmem->dma_arr = rxr->rx_desc_mapping;
4505 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4506 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4507 
4508 		ring = &rxr->rx_agg_ring_struct;
4509 		rmem = &ring->ring_mem;
4510 		rmem->nr_pages = bp->rx_agg_nr_pages;
4511 		rmem->page_size = HW_RXBD_RING_SIZE;
4512 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4513 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4514 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4515 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4516 
4517 skip_rx:
4518 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4519 			ring = &txr->tx_ring_struct;
4520 			rmem = &ring->ring_mem;
4521 			rmem->nr_pages = bp->tx_nr_pages;
4522 			rmem->page_size = HW_TXBD_RING_SIZE;
4523 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4524 			rmem->dma_arr = txr->tx_desc_mapping;
4525 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4526 			rmem->vmem = (void **)&txr->tx_buf_ring;
4527 		}
4528 	}
4529 }
4530 
4531 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4532 {
4533 	int i;
4534 	u32 prod;
4535 	struct rx_bd **rx_buf_ring;
4536 
4537 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4538 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4539 		int j;
4540 		struct rx_bd *rxbd;
4541 
4542 		rxbd = rx_buf_ring[i];
4543 		if (!rxbd)
4544 			continue;
4545 
4546 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4547 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4548 			rxbd->rx_bd_opaque = prod;
4549 		}
4550 	}
4551 }
4552 
4553 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4554 				       struct bnxt_rx_ring_info *rxr,
4555 				       int ring_nr)
4556 {
4557 	u32 prod;
4558 	int i;
4559 
4560 	prod = rxr->rx_prod;
4561 	for (i = 0; i < bp->rx_ring_size; i++) {
4562 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4563 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4564 				    ring_nr, i, bp->rx_ring_size);
4565 			break;
4566 		}
4567 		prod = NEXT_RX(prod);
4568 	}
4569 	rxr->rx_prod = prod;
4570 }
4571 
4572 static void bnxt_alloc_one_rx_ring_netmem(struct bnxt *bp,
4573 					  struct bnxt_rx_ring_info *rxr,
4574 					  int ring_nr)
4575 {
4576 	int fill_level, i;
4577 	u32 prod;
4578 
4579 	fill_level = bnxt_rx_agg_ring_fill_level(bp, rxr);
4580 
4581 	prod = rxr->rx_agg_prod;
4582 	for (i = 0; i < fill_level; i++) {
4583 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_KERNEL)) {
4584 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4585 				    ring_nr, i, bp->rx_agg_ring_size);
4586 			break;
4587 		}
4588 		prod = NEXT_RX_AGG(prod);
4589 	}
4590 	rxr->rx_agg_prod = prod;
4591 }
4592 
4593 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4594 					struct bnxt_rx_ring_info *rxr)
4595 {
4596 	dma_addr_t mapping;
4597 	u8 *data;
4598 	int i;
4599 
4600 	for (i = 0; i < bp->max_tpa_roundup_size; i++) {
4601 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4602 					    GFP_KERNEL);
4603 		if (!data)
4604 			return -ENOMEM;
4605 
4606 		rxr->rx_tpa[i].data = data;
4607 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4608 		rxr->rx_tpa[i].mapping = mapping;
4609 	}
4610 
4611 	return 0;
4612 }
4613 
4614 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4615 {
4616 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4617 	int rc;
4618 
4619 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4620 
4621 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4622 		return 0;
4623 
4624 	bnxt_alloc_one_rx_ring_netmem(bp, rxr, ring_nr);
4625 
4626 	if (rxr->rx_tpa) {
4627 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4628 		if (rc)
4629 			return rc;
4630 	}
4631 	return 0;
4632 }
4633 
4634 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4635 				       struct bnxt_rx_ring_info *rxr)
4636 {
4637 	struct bnxt_ring_struct *ring;
4638 	u32 type;
4639 
4640 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4641 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4642 
4643 	if (NET_IP_ALIGN == 2)
4644 		type |= RX_BD_FLAGS_SOP;
4645 
4646 	ring = &rxr->rx_ring_struct;
4647 	bnxt_init_rxbd_pages(ring, type);
4648 	ring->fw_ring_id = INVALID_HW_RING_ID;
4649 }
4650 
4651 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4652 					   struct bnxt_rx_ring_info *rxr)
4653 {
4654 	struct bnxt_ring_struct *ring;
4655 	u32 type;
4656 
4657 	ring = &rxr->rx_agg_ring_struct;
4658 	ring->fw_ring_id = INVALID_HW_RING_ID;
4659 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4660 		type = ((u32)rxr->rx_page_size << RX_BD_LEN_SHIFT) |
4661 			RX_BD_TYPE_RX_AGG_BD;
4662 
4663 		/* Disable EOP if TPA is enabled to prevent overlapping zero
4664 		 * padding with the next segment's data.  On P7_PLUS, EOP will
4665 		 * automatically disable Relaxed Ordering (RO) to prevent
4666 		 * potential data corruption (and may degrade performance).  On
4667 		 * older chips, RO will not be automatically disabled and may
4668 		 * cause corruption.
4669 		 */
4670 		if (!(bp->flags & BNXT_FLAG_TPA))
4671 			type |= RX_BD_FLAGS_AGG_EOP;
4672 
4673 		bnxt_init_rxbd_pages(ring, type);
4674 	}
4675 }
4676 
4677 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4678 {
4679 	struct bnxt_rx_ring_info *rxr;
4680 
4681 	rxr = &bp->rx_ring[ring_nr];
4682 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4683 
4684 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4685 			     &rxr->bnapi->napi);
4686 
4687 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4688 		bpf_prog_add(bp->xdp_prog, 1);
4689 		rxr->xdp_prog = bp->xdp_prog;
4690 	}
4691 
4692 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4693 
4694 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4695 }
4696 
4697 static void bnxt_init_cp_rings(struct bnxt *bp)
4698 {
4699 	int i, j;
4700 
4701 	for (i = 0; i < bp->cp_nr_rings; i++) {
4702 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4703 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4704 
4705 		ring->fw_ring_id = INVALID_HW_RING_ID;
4706 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4707 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4708 		if (!cpr->cp_ring_arr)
4709 			continue;
4710 		for (j = 0; j < cpr->cp_ring_count; j++) {
4711 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4712 
4713 			ring = &cpr2->cp_ring_struct;
4714 			ring->fw_ring_id = INVALID_HW_RING_ID;
4715 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4716 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4717 		}
4718 	}
4719 }
4720 
4721 static int bnxt_init_rx_rings(struct bnxt *bp)
4722 {
4723 	int i, rc = 0;
4724 
4725 	if (BNXT_RX_PAGE_MODE(bp)) {
4726 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4727 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4728 	} else {
4729 		bp->rx_offset = BNXT_RX_OFFSET;
4730 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4731 	}
4732 
4733 	for (i = 0; i < bp->rx_nr_rings; i++) {
4734 		rc = bnxt_init_one_rx_ring(bp, i);
4735 		if (rc)
4736 			break;
4737 	}
4738 
4739 	return rc;
4740 }
4741 
4742 static int bnxt_init_tx_rings(struct bnxt *bp)
4743 {
4744 	netdev_features_t features;
4745 	u16 i;
4746 
4747 	features = bp->dev->features;
4748 
4749 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4750 				   bnxt_min_tx_desc_cnt(bp, features));
4751 
4752 	for (i = 0; i < bp->tx_nr_rings; i++) {
4753 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4754 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4755 
4756 		ring->fw_ring_id = INVALID_HW_RING_ID;
4757 
4758 		if (i >= bp->tx_nr_rings_xdp)
4759 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4760 					     NETDEV_QUEUE_TYPE_TX,
4761 					     &txr->bnapi->napi);
4762 	}
4763 
4764 	return 0;
4765 }
4766 
4767 static void bnxt_free_ring_grps(struct bnxt *bp)
4768 {
4769 	kfree(bp->grp_info);
4770 	bp->grp_info = NULL;
4771 }
4772 
4773 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4774 {
4775 	int i;
4776 
4777 	if (irq_re_init) {
4778 		bp->grp_info = kzalloc_objs(struct bnxt_ring_grp_info,
4779 					    bp->cp_nr_rings);
4780 		if (!bp->grp_info)
4781 			return -ENOMEM;
4782 	}
4783 	for (i = 0; i < bp->cp_nr_rings; i++) {
4784 		if (irq_re_init)
4785 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4786 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4787 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4788 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4789 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4790 	}
4791 	return 0;
4792 }
4793 
4794 static void bnxt_free_vnics(struct bnxt *bp)
4795 {
4796 	kfree(bp->vnic_info);
4797 	bp->vnic_info = NULL;
4798 	bp->nr_vnics = 0;
4799 }
4800 
4801 static int bnxt_alloc_vnics(struct bnxt *bp)
4802 {
4803 	int num_vnics = 1;
4804 
4805 #ifdef CONFIG_RFS_ACCEL
4806 	if (bp->flags & BNXT_FLAG_RFS) {
4807 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4808 			num_vnics++;
4809 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4810 			num_vnics += bp->rx_nr_rings;
4811 	}
4812 #endif
4813 
4814 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4815 		num_vnics++;
4816 
4817 	bp->vnic_info = kzalloc_objs(struct bnxt_vnic_info, num_vnics);
4818 	if (!bp->vnic_info)
4819 		return -ENOMEM;
4820 
4821 	bp->nr_vnics = num_vnics;
4822 	return 0;
4823 }
4824 
4825 static void bnxt_init_vnics(struct bnxt *bp)
4826 {
4827 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4828 	int i;
4829 
4830 	for (i = 0; i < bp->nr_vnics; i++) {
4831 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4832 		int j;
4833 
4834 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4835 		vnic->vnic_id = i;
4836 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4837 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4838 
4839 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4840 
4841 		if (bp->vnic_info[i].rss_hash_key) {
4842 			if (i == BNXT_VNIC_DEFAULT) {
4843 				u8 *key = (void *)vnic->rss_hash_key;
4844 				int k;
4845 
4846 				if (!bp->rss_hash_key_valid &&
4847 				    !bp->rss_hash_key_updated) {
4848 					get_random_bytes(bp->rss_hash_key,
4849 							 HW_HASH_KEY_SIZE);
4850 					bp->rss_hash_key_updated = true;
4851 				}
4852 
4853 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4854 				       HW_HASH_KEY_SIZE);
4855 
4856 				if (!bp->rss_hash_key_updated)
4857 					continue;
4858 
4859 				bp->rss_hash_key_updated = false;
4860 				bp->rss_hash_key_valid = true;
4861 
4862 				bp->toeplitz_prefix = 0;
4863 				for (k = 0; k < 8; k++) {
4864 					bp->toeplitz_prefix <<= 8;
4865 					bp->toeplitz_prefix |= key[k];
4866 				}
4867 			} else {
4868 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4869 				       HW_HASH_KEY_SIZE);
4870 			}
4871 		}
4872 	}
4873 }
4874 
4875 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4876 {
4877 	int pages;
4878 
4879 	pages = ring_size / desc_per_pg;
4880 
4881 	if (!pages)
4882 		return 1;
4883 
4884 	pages++;
4885 
4886 	while (pages & (pages - 1))
4887 		pages++;
4888 
4889 	return pages;
4890 }
4891 
4892 void bnxt_set_tpa_flags(struct bnxt *bp)
4893 {
4894 	bp->flags &= ~BNXT_FLAG_TPA;
4895 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4896 		return;
4897 	if (bp->dev->features & NETIF_F_LRO)
4898 		bp->flags |= BNXT_FLAG_LRO;
4899 	else if (bp->dev->features & NETIF_F_GRO_HW)
4900 		bp->flags |= BNXT_FLAG_GRO;
4901 }
4902 
4903 static void bnxt_init_ring_params(struct bnxt *bp)
4904 {
4905 	unsigned int rx_size;
4906 
4907 	bp->rx_copybreak = 0; /* rx-copybreak disabled by default */
4908 	/* Try to fit 4 chunks into a 4k page */
4909 	rx_size = SZ_1K -
4910 		NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4911 	bp->dev->cfg->hds_thresh = max(BNXT_MIN_RX_HDR_BUF, rx_size);
4912 }
4913 
4914 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4915  * be set on entry.
4916  */
4917 void bnxt_set_ring_params(struct bnxt *bp)
4918 {
4919 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4920 	u32 agg_factor = 0, agg_ring_size = 0;
4921 
4922 	/* 8 for CRC and VLAN */
4923 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4924 
4925 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4926 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4927 
4928 	ring_size = bp->rx_ring_size;
4929 	bp->rx_agg_ring_size = 0;
4930 	bp->rx_agg_nr_pages = 0;
4931 
4932 	if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS)
4933 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4934 
4935 	bp->flags &= ~BNXT_FLAG_JUMBO;
4936 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4937 		u32 jumbo_factor;
4938 
4939 		bp->flags |= BNXT_FLAG_JUMBO;
4940 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4941 		if (jumbo_factor > agg_factor)
4942 			agg_factor = jumbo_factor;
4943 	}
4944 	if (agg_factor) {
4945 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4946 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4947 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4948 				    bp->rx_ring_size, ring_size);
4949 			bp->rx_ring_size = ring_size;
4950 		}
4951 		agg_ring_size = ring_size * agg_factor;
4952 
4953 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4954 							RX_DESC_CNT);
4955 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4956 			u32 tmp = agg_ring_size;
4957 
4958 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4959 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4960 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4961 				    tmp, agg_ring_size);
4962 		}
4963 		bp->rx_agg_ring_size = agg_ring_size;
4964 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4965 
4966 		if (BNXT_RX_PAGE_MODE(bp)) {
4967 			rx_space = PAGE_SIZE;
4968 			rx_size = PAGE_SIZE -
4969 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4970 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4971 		} else {
4972 			rx_size = max3(BNXT_MIN_RX_HDR_BUF,
4973 				       bp->rx_copybreak,
4974 				       bp->dev->cfg_pending->hds_thresh);
4975 			rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN);
4976 			rx_space = rx_size + NET_SKB_PAD +
4977 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4978 		}
4979 	}
4980 
4981 	bp->rx_buf_use_size = rx_size;
4982 	bp->rx_buf_size = rx_space;
4983 
4984 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4985 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4986 
4987 	ring_size = bp->tx_ring_size;
4988 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4989 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4990 
4991 	max_rx_cmpl = bp->rx_ring_size;
4992 	/* MAX TPA needs to be added because TPA_START completions are
4993 	 * immediately recycled, so the TPA completions are not bound by
4994 	 * the RX ring size.
4995 	 */
4996 	if (bp->flags & BNXT_FLAG_TPA)
4997 		max_rx_cmpl += bp->max_tpa;
4998 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4999 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
5000 	bp->cp_ring_size = ring_size;
5001 
5002 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
5003 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
5004 		bp->cp_nr_pages = MAX_CP_PAGES;
5005 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
5006 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
5007 			    ring_size, bp->cp_ring_size);
5008 	}
5009 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
5010 	bp->cp_ring_mask = bp->cp_bit - 1;
5011 }
5012 
5013 /* Changing allocation mode of RX rings.
5014  * TODO: Update when extending xdp_rxq_info to support allocation modes.
5015  */
5016 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
5017 {
5018 	struct net_device *dev = bp->dev;
5019 
5020 	if (page_mode) {
5021 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
5022 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
5023 
5024 		if (bp->xdp_prog->aux->xdp_has_frags)
5025 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
5026 		else
5027 			dev->max_mtu =
5028 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
5029 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
5030 			bp->flags |= BNXT_FLAG_JUMBO;
5031 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
5032 		} else {
5033 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
5034 			bp->rx_skb_func = bnxt_rx_page_skb;
5035 		}
5036 		bp->rx_dir = DMA_BIDIRECTIONAL;
5037 	} else {
5038 		dev->max_mtu = bp->max_mtu;
5039 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
5040 		bp->rx_dir = DMA_FROM_DEVICE;
5041 		bp->rx_skb_func = bnxt_rx_skb;
5042 	}
5043 }
5044 
5045 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
5046 {
5047 	__bnxt_set_rx_skb_mode(bp, page_mode);
5048 
5049 	if (!page_mode) {
5050 		int rx, tx;
5051 
5052 		bnxt_get_max_rings(bp, &rx, &tx, true);
5053 		if (rx > 1) {
5054 			bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS;
5055 			if (BNXT_SUPPORTS_TPA(bp))
5056 				bp->dev->hw_features |= NETIF_F_LRO;
5057 		}
5058 	}
5059 
5060 	/* Update LRO and GRO_HW availability */
5061 	netdev_update_features(bp->dev);
5062 }
5063 
5064 static void bnxt_free_vnic_attributes(struct bnxt *bp)
5065 {
5066 	int i;
5067 	struct bnxt_vnic_info *vnic;
5068 	struct pci_dev *pdev = bp->pdev;
5069 
5070 	if (!bp->vnic_info)
5071 		return;
5072 
5073 	for (i = 0; i < bp->nr_vnics; i++) {
5074 		vnic = &bp->vnic_info[i];
5075 
5076 		kfree(vnic->fw_grp_ids);
5077 		vnic->fw_grp_ids = NULL;
5078 
5079 		kfree(vnic->uc_list);
5080 		vnic->uc_list = NULL;
5081 
5082 		if (vnic->mc_list) {
5083 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
5084 					  vnic->mc_list, vnic->mc_list_mapping);
5085 			vnic->mc_list = NULL;
5086 		}
5087 
5088 		if (vnic->rss_table) {
5089 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
5090 					  vnic->rss_table,
5091 					  vnic->rss_table_dma_addr);
5092 			vnic->rss_table = NULL;
5093 		}
5094 
5095 		vnic->rss_hash_key = NULL;
5096 		vnic->flags = 0;
5097 	}
5098 }
5099 
5100 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
5101 {
5102 	int i, rc = 0, size;
5103 	struct bnxt_vnic_info *vnic;
5104 	struct pci_dev *pdev = bp->pdev;
5105 	int max_rings;
5106 
5107 	for (i = 0; i < bp->nr_vnics; i++) {
5108 		vnic = &bp->vnic_info[i];
5109 
5110 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
5111 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
5112 
5113 			if (mem_size > 0) {
5114 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
5115 				if (!vnic->uc_list) {
5116 					rc = -ENOMEM;
5117 					goto out;
5118 				}
5119 			}
5120 		}
5121 
5122 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
5123 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
5124 			vnic->mc_list =
5125 				dma_alloc_coherent(&pdev->dev,
5126 						   vnic->mc_list_size,
5127 						   &vnic->mc_list_mapping,
5128 						   GFP_KERNEL);
5129 			if (!vnic->mc_list) {
5130 				rc = -ENOMEM;
5131 				goto out;
5132 			}
5133 		}
5134 
5135 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5136 			goto vnic_skip_grps;
5137 
5138 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
5139 			max_rings = bp->rx_nr_rings;
5140 		else
5141 			max_rings = 1;
5142 
5143 		vnic->fw_grp_ids = kzalloc_objs(u16, max_rings);
5144 		if (!vnic->fw_grp_ids) {
5145 			rc = -ENOMEM;
5146 			goto out;
5147 		}
5148 vnic_skip_grps:
5149 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
5150 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
5151 			continue;
5152 
5153 		/* Allocate rss table and hash key */
5154 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
5155 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5156 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
5157 
5158 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
5159 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
5160 						     vnic->rss_table_size,
5161 						     &vnic->rss_table_dma_addr,
5162 						     GFP_KERNEL);
5163 		if (!vnic->rss_table) {
5164 			rc = -ENOMEM;
5165 			goto out;
5166 		}
5167 
5168 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
5169 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
5170 	}
5171 	return 0;
5172 
5173 out:
5174 	return rc;
5175 }
5176 
5177 static void bnxt_free_hwrm_resources(struct bnxt *bp)
5178 {
5179 	struct bnxt_hwrm_wait_token *token;
5180 
5181 	dma_pool_destroy(bp->hwrm_dma_pool);
5182 	bp->hwrm_dma_pool = NULL;
5183 
5184 	rcu_read_lock();
5185 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
5186 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
5187 	rcu_read_unlock();
5188 }
5189 
5190 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
5191 {
5192 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
5193 					    BNXT_HWRM_DMA_SIZE,
5194 					    BNXT_HWRM_DMA_ALIGN, 0);
5195 	if (!bp->hwrm_dma_pool)
5196 		return -ENOMEM;
5197 
5198 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
5199 
5200 	return 0;
5201 }
5202 
5203 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
5204 {
5205 	kfree(stats->hw_masks);
5206 	stats->hw_masks = NULL;
5207 	kfree(stats->sw_stats);
5208 	stats->sw_stats = NULL;
5209 	if (stats->hw_stats) {
5210 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
5211 				  stats->hw_stats_map);
5212 		stats->hw_stats = NULL;
5213 	}
5214 }
5215 
5216 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
5217 				bool alloc_masks)
5218 {
5219 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
5220 					     &stats->hw_stats_map, GFP_KERNEL);
5221 	if (!stats->hw_stats)
5222 		return -ENOMEM;
5223 
5224 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
5225 	if (!stats->sw_stats)
5226 		goto stats_mem_err;
5227 
5228 	if (alloc_masks) {
5229 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
5230 		if (!stats->hw_masks)
5231 			goto stats_mem_err;
5232 	}
5233 	return 0;
5234 
5235 stats_mem_err:
5236 	bnxt_free_stats_mem(bp, stats);
5237 	return -ENOMEM;
5238 }
5239 
5240 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
5241 {
5242 	int i;
5243 
5244 	for (i = 0; i < count; i++)
5245 		mask_arr[i] = mask;
5246 }
5247 
5248 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
5249 {
5250 	int i;
5251 
5252 	for (i = 0; i < count; i++)
5253 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
5254 }
5255 
5256 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
5257 				    struct bnxt_stats_mem *stats)
5258 {
5259 	struct hwrm_func_qstats_ext_output *resp;
5260 	struct hwrm_func_qstats_ext_input *req;
5261 	__le64 *hw_masks;
5262 	int rc;
5263 
5264 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
5265 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5266 		return -EOPNOTSUPP;
5267 
5268 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
5269 	if (rc)
5270 		return rc;
5271 
5272 	req->fid = cpu_to_le16(0xffff);
5273 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5274 
5275 	resp = hwrm_req_hold(bp, req);
5276 	rc = hwrm_req_send(bp, req);
5277 	if (!rc) {
5278 		hw_masks = &resp->rx_ucast_pkts;
5279 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
5280 	}
5281 	hwrm_req_drop(bp, req);
5282 	return rc;
5283 }
5284 
5285 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
5286 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
5287 
5288 static void bnxt_init_stats(struct bnxt *bp)
5289 {
5290 	struct bnxt_napi *bnapi = bp->bnapi[0];
5291 	struct bnxt_cp_ring_info *cpr;
5292 	struct bnxt_stats_mem *stats;
5293 	__le64 *rx_stats, *tx_stats;
5294 	int rc, rx_count, tx_count;
5295 	u64 *rx_masks, *tx_masks;
5296 	u64 mask;
5297 	u8 flags;
5298 
5299 	cpr = &bnapi->cp_ring;
5300 	stats = &cpr->stats;
5301 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
5302 	if (rc) {
5303 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5304 			mask = (1ULL << 48) - 1;
5305 		else
5306 			mask = -1ULL;
5307 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
5308 	}
5309 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
5310 		stats = &bp->port_stats;
5311 		rx_stats = stats->hw_stats;
5312 		rx_masks = stats->hw_masks;
5313 		rx_count = sizeof(struct rx_port_stats) / 8;
5314 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5315 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5316 		tx_count = sizeof(struct tx_port_stats) / 8;
5317 
5318 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
5319 		rc = bnxt_hwrm_port_qstats(bp, flags);
5320 		if (rc) {
5321 			mask = (1ULL << 40) - 1;
5322 
5323 			bnxt_fill_masks(rx_masks, mask, rx_count);
5324 			bnxt_fill_masks(tx_masks, mask, tx_count);
5325 		} else {
5326 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5327 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5328 			bnxt_hwrm_port_qstats(bp, 0);
5329 		}
5330 	}
5331 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5332 		stats = &bp->rx_port_stats_ext;
5333 		rx_stats = stats->hw_stats;
5334 		rx_masks = stats->hw_masks;
5335 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5336 		stats = &bp->tx_port_stats_ext;
5337 		tx_stats = stats->hw_stats;
5338 		tx_masks = stats->hw_masks;
5339 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5340 
5341 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5342 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5343 		if (rc) {
5344 			mask = (1ULL << 40) - 1;
5345 
5346 			bnxt_fill_masks(rx_masks, mask, rx_count);
5347 			if (tx_stats)
5348 				bnxt_fill_masks(tx_masks, mask, tx_count);
5349 		} else {
5350 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5351 			if (tx_stats)
5352 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5353 						   tx_count);
5354 			bnxt_hwrm_port_qstats_ext(bp, 0);
5355 		}
5356 	}
5357 }
5358 
5359 static void bnxt_free_port_stats(struct bnxt *bp)
5360 {
5361 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5362 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5363 
5364 	bnxt_free_stats_mem(bp, &bp->port_stats);
5365 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5366 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5367 }
5368 
5369 static void bnxt_free_ring_stats(struct bnxt *bp)
5370 {
5371 	int i;
5372 
5373 	if (!bp->bnapi)
5374 		return;
5375 
5376 	for (i = 0; i < bp->cp_nr_rings; i++) {
5377 		struct bnxt_napi *bnapi = bp->bnapi[i];
5378 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5379 
5380 		bnxt_free_stats_mem(bp, &cpr->stats);
5381 
5382 		kfree(cpr->sw_stats);
5383 		cpr->sw_stats = NULL;
5384 	}
5385 }
5386 
5387 static int bnxt_alloc_stats(struct bnxt *bp)
5388 {
5389 	u32 size, i;
5390 	int rc;
5391 
5392 	size = bp->hw_ring_stats_size;
5393 
5394 	for (i = 0; i < bp->cp_nr_rings; i++) {
5395 		struct bnxt_napi *bnapi = bp->bnapi[i];
5396 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5397 
5398 		cpr->sw_stats = kzalloc_obj(*cpr->sw_stats);
5399 		if (!cpr->sw_stats)
5400 			return -ENOMEM;
5401 
5402 		cpr->stats.len = size;
5403 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5404 		if (rc)
5405 			return rc;
5406 
5407 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5408 	}
5409 
5410 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5411 		return 0;
5412 
5413 	if (bp->port_stats.hw_stats)
5414 		goto alloc_ext_stats;
5415 
5416 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5417 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5418 	if (rc)
5419 		return rc;
5420 
5421 	bp->flags |= BNXT_FLAG_PORT_STATS;
5422 
5423 alloc_ext_stats:
5424 	/* Display extended statistics only if FW supports it */
5425 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5426 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5427 			return 0;
5428 
5429 	if (bp->rx_port_stats_ext.hw_stats)
5430 		goto alloc_tx_ext_stats;
5431 
5432 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5433 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5434 	/* Extended stats are optional */
5435 	if (rc)
5436 		return 0;
5437 
5438 alloc_tx_ext_stats:
5439 	if (bp->tx_port_stats_ext.hw_stats)
5440 		return 0;
5441 
5442 	if (bp->hwrm_spec_code >= 0x10902 ||
5443 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5444 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5445 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5446 		/* Extended stats are optional */
5447 		if (rc)
5448 			return 0;
5449 	}
5450 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5451 	return 0;
5452 }
5453 
5454 static void bnxt_clear_ring_indices(struct bnxt *bp)
5455 {
5456 	int i, j;
5457 
5458 	if (!bp->bnapi)
5459 		return;
5460 
5461 	for (i = 0; i < bp->cp_nr_rings; i++) {
5462 		struct bnxt_napi *bnapi = bp->bnapi[i];
5463 		struct bnxt_cp_ring_info *cpr;
5464 		struct bnxt_rx_ring_info *rxr;
5465 		struct bnxt_tx_ring_info *txr;
5466 
5467 		if (!bnapi)
5468 			continue;
5469 
5470 		cpr = &bnapi->cp_ring;
5471 		cpr->cp_raw_cons = 0;
5472 
5473 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5474 			txr->tx_prod = 0;
5475 			txr->tx_cons = 0;
5476 			txr->tx_hw_cons = 0;
5477 			txr->kick_pending = 0;
5478 			txr->kick_txbd0 = NULL;
5479 		}
5480 
5481 		rxr = bnapi->rx_ring;
5482 		if (rxr) {
5483 			rxr->rx_prod = 0;
5484 			rxr->rx_agg_prod = 0;
5485 			rxr->rx_sw_agg_prod = 0;
5486 			rxr->rx_next_cons = 0;
5487 		}
5488 		bnapi->events = 0;
5489 	}
5490 }
5491 
5492 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5493 {
5494 	u8 type = fltr->type, flags = fltr->flags;
5495 
5496 	INIT_LIST_HEAD(&fltr->list);
5497 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5498 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5499 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5500 }
5501 
5502 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5503 {
5504 	if (!list_empty(&fltr->list))
5505 		list_del_init(&fltr->list);
5506 }
5507 
5508 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5509 {
5510 	struct bnxt_filter_base *usr_fltr, *tmp;
5511 
5512 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5513 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5514 			continue;
5515 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5516 	}
5517 }
5518 
5519 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5520 {
5521 	hlist_del(&fltr->hash);
5522 	bnxt_del_one_usr_fltr(bp, fltr);
5523 	if (fltr->flags) {
5524 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5525 		bp->ntp_fltr_count--;
5526 	}
5527 	kfree(fltr);
5528 }
5529 
5530 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5531 {
5532 	int i;
5533 
5534 	netdev_assert_locked_or_invisible(bp->dev);
5535 
5536 	/* Under netdev instance lock and all our NAPIs have been disabled.
5537 	 * It's safe to delete the hash table.
5538 	 */
5539 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5540 		struct hlist_head *head;
5541 		struct hlist_node *tmp;
5542 		struct bnxt_ntuple_filter *fltr;
5543 
5544 		head = &bp->ntp_fltr_hash_tbl[i];
5545 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5546 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5547 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5548 				     !list_empty(&fltr->base.list)))
5549 				continue;
5550 			bnxt_del_fltr(bp, &fltr->base);
5551 		}
5552 	}
5553 	if (!all)
5554 		return;
5555 
5556 	bitmap_free(bp->ntp_fltr_bmap);
5557 	bp->ntp_fltr_bmap = NULL;
5558 	bp->ntp_fltr_count = 0;
5559 }
5560 
5561 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5562 {
5563 	int i, rc = 0;
5564 
5565 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5566 		return 0;
5567 
5568 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5569 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5570 
5571 	bp->ntp_fltr_count = 0;
5572 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5573 
5574 	if (!bp->ntp_fltr_bmap)
5575 		rc = -ENOMEM;
5576 
5577 	return rc;
5578 }
5579 
5580 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5581 {
5582 	int i;
5583 
5584 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5585 		struct hlist_head *head;
5586 		struct hlist_node *tmp;
5587 		struct bnxt_l2_filter *fltr;
5588 
5589 		head = &bp->l2_fltr_hash_tbl[i];
5590 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5591 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5592 				     !list_empty(&fltr->base.list)))
5593 				continue;
5594 			bnxt_del_fltr(bp, &fltr->base);
5595 		}
5596 	}
5597 }
5598 
5599 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5600 {
5601 	int i;
5602 
5603 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5604 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5605 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5606 }
5607 
5608 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5609 {
5610 	bnxt_free_vnic_attributes(bp);
5611 	bnxt_free_tx_rings(bp);
5612 	bnxt_free_rx_rings(bp);
5613 	bnxt_free_cp_rings(bp);
5614 	bnxt_free_all_cp_arrays(bp);
5615 	bnxt_free_ntp_fltrs(bp, false);
5616 	bnxt_free_l2_filters(bp, false);
5617 	if (irq_re_init) {
5618 		bnxt_free_ring_stats(bp);
5619 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5620 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5621 			bnxt_free_port_stats(bp);
5622 		bnxt_free_ring_grps(bp);
5623 		bnxt_free_vnics(bp);
5624 		kfree(bp->tx_ring_map);
5625 		bp->tx_ring_map = NULL;
5626 		kfree(bp->tx_ring);
5627 		bp->tx_ring = NULL;
5628 		kfree(bp->rx_ring);
5629 		bp->rx_ring = NULL;
5630 		kfree(bp->bnapi);
5631 		bp->bnapi = NULL;
5632 	} else {
5633 		bnxt_clear_ring_indices(bp);
5634 	}
5635 }
5636 
5637 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5638 {
5639 	int i, j, rc, size, arr_size;
5640 	void *bnapi;
5641 
5642 	if (irq_re_init) {
5643 		/* Allocate bnapi mem pointer array and mem block for
5644 		 * all queues
5645 		 */
5646 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5647 				bp->cp_nr_rings);
5648 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5649 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5650 		if (!bnapi)
5651 			return -ENOMEM;
5652 
5653 		bp->bnapi = bnapi;
5654 		bnapi += arr_size;
5655 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5656 			bp->bnapi[i] = bnapi;
5657 			bp->bnapi[i]->index = i;
5658 			bp->bnapi[i]->bp = bp;
5659 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5660 				struct bnxt_cp_ring_info *cpr =
5661 					&bp->bnapi[i]->cp_ring;
5662 
5663 				cpr->cp_ring_struct.ring_mem.flags =
5664 					BNXT_RMEM_RING_PTE_FLAG;
5665 			}
5666 		}
5667 
5668 		bp->rx_ring = kzalloc_objs(struct bnxt_rx_ring_info,
5669 					   bp->rx_nr_rings);
5670 		if (!bp->rx_ring)
5671 			return -ENOMEM;
5672 
5673 		for (i = 0; i < bp->rx_nr_rings; i++) {
5674 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5675 
5676 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5677 				rxr->rx_ring_struct.ring_mem.flags =
5678 					BNXT_RMEM_RING_PTE_FLAG;
5679 				rxr->rx_agg_ring_struct.ring_mem.flags =
5680 					BNXT_RMEM_RING_PTE_FLAG;
5681 			} else {
5682 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5683 			}
5684 			rxr->bnapi = bp->bnapi[i];
5685 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5686 		}
5687 
5688 		bp->tx_ring = kzalloc_objs(struct bnxt_tx_ring_info,
5689 					   bp->tx_nr_rings);
5690 		if (!bp->tx_ring)
5691 			return -ENOMEM;
5692 
5693 		bp->tx_ring_map = kzalloc_objs(u16, bp->tx_nr_rings);
5694 
5695 		if (!bp->tx_ring_map)
5696 			return -ENOMEM;
5697 
5698 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5699 			j = 0;
5700 		else
5701 			j = bp->rx_nr_rings;
5702 
5703 		for (i = 0; i < bp->tx_nr_rings; i++) {
5704 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5705 			struct bnxt_napi *bnapi2;
5706 
5707 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5708 				txr->tx_ring_struct.ring_mem.flags =
5709 					BNXT_RMEM_RING_PTE_FLAG;
5710 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5711 			if (i >= bp->tx_nr_rings_xdp) {
5712 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5713 
5714 				bnapi2 = bp->bnapi[k];
5715 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5716 				txr->tx_napi_idx =
5717 					BNXT_RING_TO_TC(bp, txr->txq_index);
5718 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5719 				bnapi2->tx_int = bnxt_tx_int;
5720 			} else {
5721 				bnapi2 = bp->bnapi[j];
5722 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5723 				bnapi2->tx_ring[0] = txr;
5724 				bnapi2->tx_int = bnxt_tx_int_xdp;
5725 				j++;
5726 			}
5727 			txr->bnapi = bnapi2;
5728 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5729 				txr->tx_cpr = &bnapi2->cp_ring;
5730 		}
5731 
5732 		rc = bnxt_alloc_stats(bp);
5733 		if (rc)
5734 			goto alloc_mem_err;
5735 		bnxt_init_stats(bp);
5736 
5737 		rc = bnxt_alloc_ntp_fltrs(bp);
5738 		if (rc)
5739 			goto alloc_mem_err;
5740 
5741 		rc = bnxt_alloc_vnics(bp);
5742 		if (rc)
5743 			goto alloc_mem_err;
5744 	}
5745 
5746 	rc = bnxt_alloc_all_cp_arrays(bp);
5747 	if (rc)
5748 		goto alloc_mem_err;
5749 
5750 	bnxt_init_ring_struct(bp);
5751 
5752 	rc = bnxt_alloc_rx_rings(bp);
5753 	if (rc)
5754 		goto alloc_mem_err;
5755 
5756 	rc = bnxt_alloc_tx_rings(bp);
5757 	if (rc)
5758 		goto alloc_mem_err;
5759 
5760 	rc = bnxt_alloc_cp_rings(bp);
5761 	if (rc)
5762 		goto alloc_mem_err;
5763 
5764 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5765 						  BNXT_VNIC_MCAST_FLAG |
5766 						  BNXT_VNIC_UCAST_FLAG;
5767 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5768 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5769 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5770 
5771 	rc = bnxt_alloc_vnic_attributes(bp);
5772 	if (rc)
5773 		goto alloc_mem_err;
5774 	return 0;
5775 
5776 alloc_mem_err:
5777 	bnxt_free_mem(bp, true);
5778 	return rc;
5779 }
5780 
5781 static void bnxt_disable_int(struct bnxt *bp)
5782 {
5783 	int i;
5784 
5785 	if (!bp->bnapi)
5786 		return;
5787 
5788 	for (i = 0; i < bp->cp_nr_rings; i++) {
5789 		struct bnxt_napi *bnapi = bp->bnapi[i];
5790 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5791 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5792 
5793 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5794 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5795 	}
5796 }
5797 
5798 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5799 {
5800 	struct bnxt_napi *bnapi = bp->bnapi[n];
5801 	struct bnxt_cp_ring_info *cpr;
5802 
5803 	cpr = &bnapi->cp_ring;
5804 	return cpr->cp_ring_struct.map_idx;
5805 }
5806 
5807 static void bnxt_disable_int_sync(struct bnxt *bp)
5808 {
5809 	int i;
5810 
5811 	if (!bp->irq_tbl || !bp->bnapi)
5812 		return;
5813 
5814 	atomic_inc(&bp->intr_sem);
5815 
5816 	bnxt_disable_int(bp);
5817 	for (i = 0; i < bp->cp_nr_rings; i++) {
5818 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5819 
5820 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5821 	}
5822 }
5823 
5824 static void bnxt_enable_int(struct bnxt *bp)
5825 {
5826 	int i;
5827 
5828 	atomic_set(&bp->intr_sem, 0);
5829 	for (i = 0; i < bp->cp_nr_rings; i++) {
5830 		struct bnxt_napi *bnapi = bp->bnapi[i];
5831 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5832 
5833 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5834 	}
5835 }
5836 
5837 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5838 			    bool async_only)
5839 {
5840 	DECLARE_BITMAP(async_events_bmap, 256);
5841 	u32 *events = (u32 *)async_events_bmap;
5842 	struct hwrm_func_drv_rgtr_output *resp;
5843 	struct hwrm_func_drv_rgtr_input *req;
5844 	u32 flags;
5845 	int rc, i;
5846 
5847 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5848 	if (rc)
5849 		return rc;
5850 
5851 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5852 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5853 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5854 
5855 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5856 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5857 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5858 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5859 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5860 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5861 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5862 	if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2)
5863 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT;
5864 	req->flags = cpu_to_le32(flags);
5865 	req->ver_maj_8b = DRV_VER_MAJ;
5866 	req->ver_min_8b = DRV_VER_MIN;
5867 	req->ver_upd_8b = DRV_VER_UPD;
5868 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5869 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5870 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5871 
5872 	if (BNXT_PF(bp)) {
5873 		u32 data[8];
5874 		int i;
5875 
5876 		memset(data, 0, sizeof(data));
5877 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5878 			u16 cmd = bnxt_vf_req_snif[i];
5879 			unsigned int bit, idx;
5880 
5881 			if ((bp->fw_cap & BNXT_FW_CAP_LINK_ADMIN) &&
5882 			    cmd == HWRM_PORT_PHY_QCFG)
5883 				continue;
5884 
5885 			idx = cmd / 32;
5886 			bit = cmd % 32;
5887 			data[idx] |= 1 << bit;
5888 		}
5889 
5890 		for (i = 0; i < 8; i++)
5891 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5892 
5893 		req->enables |=
5894 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5895 	}
5896 
5897 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5898 		req->flags |= cpu_to_le32(
5899 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5900 
5901 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5902 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5903 		u16 event_id = bnxt_async_events_arr[i];
5904 
5905 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5906 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5907 			continue;
5908 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5909 		    !bp->ptp_cfg)
5910 			continue;
5911 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5912 	}
5913 	if (bmap && bmap_size) {
5914 		for (i = 0; i < bmap_size; i++) {
5915 			if (test_bit(i, bmap))
5916 				__set_bit(i, async_events_bmap);
5917 		}
5918 	}
5919 	for (i = 0; i < 8; i++)
5920 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5921 
5922 	if (async_only)
5923 		req->enables =
5924 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5925 
5926 	resp = hwrm_req_hold(bp, req);
5927 	rc = hwrm_req_send(bp, req);
5928 	if (!rc) {
5929 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5930 		if (resp->flags &
5931 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5932 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5933 	}
5934 	hwrm_req_drop(bp, req);
5935 	return rc;
5936 }
5937 
5938 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5939 {
5940 	struct hwrm_func_drv_unrgtr_input *req;
5941 	int rc;
5942 
5943 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5944 		return 0;
5945 
5946 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5947 	if (rc)
5948 		return rc;
5949 	return hwrm_req_send(bp, req);
5950 }
5951 
5952 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5953 
5954 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5955 {
5956 	struct hwrm_tunnel_dst_port_free_input *req;
5957 	int rc;
5958 
5959 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5960 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5961 		return 0;
5962 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5963 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5964 		return 0;
5965 
5966 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5967 	if (rc)
5968 		return rc;
5969 
5970 	req->tunnel_type = tunnel_type;
5971 
5972 	switch (tunnel_type) {
5973 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5974 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5975 		bp->vxlan_port = 0;
5976 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5977 		break;
5978 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5979 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5980 		bp->nge_port = 0;
5981 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5982 		break;
5983 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5984 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5985 		bp->vxlan_gpe_port = 0;
5986 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5987 		break;
5988 	default:
5989 		break;
5990 	}
5991 
5992 	rc = hwrm_req_send(bp, req);
5993 	if (rc)
5994 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5995 			   rc);
5996 	if (bp->flags & BNXT_FLAG_TPA)
5997 		bnxt_set_tpa(bp, true);
5998 	return rc;
5999 }
6000 
6001 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
6002 					   u8 tunnel_type)
6003 {
6004 	struct hwrm_tunnel_dst_port_alloc_output *resp;
6005 	struct hwrm_tunnel_dst_port_alloc_input *req;
6006 	int rc;
6007 
6008 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
6009 	if (rc)
6010 		return rc;
6011 
6012 	req->tunnel_type = tunnel_type;
6013 	req->tunnel_dst_port_val = port;
6014 
6015 	resp = hwrm_req_hold(bp, req);
6016 	rc = hwrm_req_send(bp, req);
6017 	if (rc) {
6018 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
6019 			   rc);
6020 		goto err_out;
6021 	}
6022 
6023 	switch (tunnel_type) {
6024 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
6025 		bp->vxlan_port = port;
6026 		bp->vxlan_fw_dst_port_id =
6027 			le16_to_cpu(resp->tunnel_dst_port_id);
6028 		break;
6029 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
6030 		bp->nge_port = port;
6031 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
6032 		break;
6033 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
6034 		bp->vxlan_gpe_port = port;
6035 		bp->vxlan_gpe_fw_dst_port_id =
6036 			le16_to_cpu(resp->tunnel_dst_port_id);
6037 		break;
6038 	default:
6039 		break;
6040 	}
6041 	if (bp->flags & BNXT_FLAG_TPA)
6042 		bnxt_set_tpa(bp, true);
6043 
6044 err_out:
6045 	hwrm_req_drop(bp, req);
6046 	return rc;
6047 }
6048 
6049 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
6050 {
6051 	struct hwrm_cfa_l2_set_rx_mask_input *req;
6052 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
6053 	int rc;
6054 
6055 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
6056 	if (rc)
6057 		return rc;
6058 
6059 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6060 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
6061 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
6062 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
6063 	}
6064 	req->mask = cpu_to_le32(vnic->rx_mask);
6065 	return hwrm_req_send_silent(bp, req);
6066 }
6067 
6068 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6069 {
6070 	if (!atomic_dec_and_test(&fltr->refcnt))
6071 		return;
6072 	spin_lock_bh(&bp->ntp_fltr_lock);
6073 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
6074 		spin_unlock_bh(&bp->ntp_fltr_lock);
6075 		return;
6076 	}
6077 	hlist_del_rcu(&fltr->base.hash);
6078 	bnxt_del_one_usr_fltr(bp, &fltr->base);
6079 	if (fltr->base.flags) {
6080 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
6081 		bp->ntp_fltr_count--;
6082 	}
6083 	spin_unlock_bh(&bp->ntp_fltr_lock);
6084 	kfree_rcu(fltr, base.rcu);
6085 }
6086 
6087 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
6088 						      struct bnxt_l2_key *key,
6089 						      u32 idx)
6090 {
6091 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
6092 	struct bnxt_l2_filter *fltr;
6093 
6094 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
6095 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
6096 
6097 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
6098 		    l2_key->vlan == key->vlan)
6099 			return fltr;
6100 	}
6101 	return NULL;
6102 }
6103 
6104 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
6105 						    struct bnxt_l2_key *key,
6106 						    u32 idx)
6107 {
6108 	struct bnxt_l2_filter *fltr = NULL;
6109 
6110 	rcu_read_lock();
6111 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6112 	if (fltr)
6113 		atomic_inc(&fltr->refcnt);
6114 	rcu_read_unlock();
6115 	return fltr;
6116 }
6117 
6118 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
6119 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
6120 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
6121 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
6122 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
6123 
6124 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
6125 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
6126 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
6127 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
6128 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
6129 
6130 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
6131 {
6132 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
6133 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
6134 			return sizeof(fkeys->addrs.v4addrs) +
6135 			       sizeof(fkeys->ports);
6136 
6137 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
6138 			return sizeof(fkeys->addrs.v4addrs);
6139 	}
6140 
6141 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
6142 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
6143 			return sizeof(fkeys->addrs.v6addrs) +
6144 			       sizeof(fkeys->ports);
6145 
6146 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
6147 			return sizeof(fkeys->addrs.v6addrs);
6148 	}
6149 
6150 	return 0;
6151 }
6152 
6153 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
6154 			 const unsigned char *key)
6155 {
6156 	u64 prefix = bp->toeplitz_prefix, hash = 0;
6157 	struct bnxt_ipv4_tuple tuple4;
6158 	struct bnxt_ipv6_tuple tuple6;
6159 	int i, j, len = 0;
6160 	u8 *four_tuple;
6161 
6162 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
6163 	if (!len)
6164 		return 0;
6165 
6166 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
6167 		tuple4.v4addrs = fkeys->addrs.v4addrs;
6168 		tuple4.ports = fkeys->ports;
6169 		four_tuple = (unsigned char *)&tuple4;
6170 	} else {
6171 		tuple6.v6addrs = fkeys->addrs.v6addrs;
6172 		tuple6.ports = fkeys->ports;
6173 		four_tuple = (unsigned char *)&tuple6;
6174 	}
6175 
6176 	for (i = 0, j = 8; i < len; i++, j++) {
6177 		u8 byte = four_tuple[i];
6178 		int bit;
6179 
6180 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
6181 			if (byte & 0x80)
6182 				hash ^= prefix;
6183 		}
6184 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
6185 	}
6186 
6187 	/* The valid part of the hash is in the upper 32 bits. */
6188 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
6189 }
6190 
6191 #ifdef CONFIG_RFS_ACCEL
6192 static struct bnxt_l2_filter *
6193 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
6194 {
6195 	struct bnxt_l2_filter *fltr;
6196 	u32 idx;
6197 
6198 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6199 	      BNXT_L2_FLTR_HASH_MASK;
6200 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6201 	return fltr;
6202 }
6203 #endif
6204 
6205 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
6206 			       struct bnxt_l2_key *key, u32 idx)
6207 {
6208 	struct hlist_head *head;
6209 
6210 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
6211 	fltr->l2_key.vlan = key->vlan;
6212 	fltr->base.type = BNXT_FLTR_TYPE_L2;
6213 	if (fltr->base.flags) {
6214 		int bit_id;
6215 
6216 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6217 						 bp->max_fltr, 0);
6218 		if (bit_id < 0)
6219 			return -ENOMEM;
6220 		fltr->base.sw_id = (u16)bit_id;
6221 		bp->ntp_fltr_count++;
6222 	}
6223 	head = &bp->l2_fltr_hash_tbl[idx];
6224 	hlist_add_head_rcu(&fltr->base.hash, head);
6225 	bnxt_insert_usr_fltr(bp, &fltr->base);
6226 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
6227 	atomic_set(&fltr->refcnt, 1);
6228 	return 0;
6229 }
6230 
6231 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
6232 						   struct bnxt_l2_key *key,
6233 						   gfp_t gfp)
6234 {
6235 	struct bnxt_l2_filter *fltr;
6236 	u32 idx;
6237 	int rc;
6238 
6239 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6240 	      BNXT_L2_FLTR_HASH_MASK;
6241 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6242 	if (fltr)
6243 		return fltr;
6244 
6245 	fltr = kzalloc_obj(*fltr, gfp);
6246 	if (!fltr)
6247 		return ERR_PTR(-ENOMEM);
6248 	spin_lock_bh(&bp->ntp_fltr_lock);
6249 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6250 	spin_unlock_bh(&bp->ntp_fltr_lock);
6251 	if (rc) {
6252 		bnxt_del_l2_filter(bp, fltr);
6253 		fltr = ERR_PTR(rc);
6254 	}
6255 	return fltr;
6256 }
6257 
6258 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
6259 						struct bnxt_l2_key *key,
6260 						u16 flags)
6261 {
6262 	struct bnxt_l2_filter *fltr;
6263 	u32 idx;
6264 	int rc;
6265 
6266 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6267 	      BNXT_L2_FLTR_HASH_MASK;
6268 	spin_lock_bh(&bp->ntp_fltr_lock);
6269 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6270 	if (fltr) {
6271 		fltr = ERR_PTR(-EEXIST);
6272 		goto l2_filter_exit;
6273 	}
6274 	fltr = kzalloc_obj(*fltr, GFP_ATOMIC);
6275 	if (!fltr) {
6276 		fltr = ERR_PTR(-ENOMEM);
6277 		goto l2_filter_exit;
6278 	}
6279 	fltr->base.flags = flags;
6280 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6281 	if (rc) {
6282 		spin_unlock_bh(&bp->ntp_fltr_lock);
6283 		bnxt_del_l2_filter(bp, fltr);
6284 		return ERR_PTR(rc);
6285 	}
6286 
6287 l2_filter_exit:
6288 	spin_unlock_bh(&bp->ntp_fltr_lock);
6289 	return fltr;
6290 }
6291 
6292 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
6293 {
6294 #ifdef CONFIG_BNXT_SRIOV
6295 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
6296 
6297 	return vf->fw_fid;
6298 #else
6299 	return INVALID_HW_RING_ID;
6300 #endif
6301 }
6302 
6303 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6304 {
6305 	struct hwrm_cfa_l2_filter_free_input *req;
6306 	u16 target_id = 0xffff;
6307 	int rc;
6308 
6309 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6310 		struct bnxt_pf_info *pf = &bp->pf;
6311 
6312 		if (fltr->base.vf_idx >= pf->active_vfs)
6313 			return -EINVAL;
6314 
6315 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6316 		if (target_id == INVALID_HW_RING_ID)
6317 			return -EINVAL;
6318 	}
6319 
6320 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
6321 	if (rc)
6322 		return rc;
6323 
6324 	req->target_id = cpu_to_le16(target_id);
6325 	req->l2_filter_id = fltr->base.filter_id;
6326 	return hwrm_req_send(bp, req);
6327 }
6328 
6329 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6330 {
6331 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6332 	struct hwrm_cfa_l2_filter_alloc_input *req;
6333 	u16 target_id = 0xffff;
6334 	int rc;
6335 
6336 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6337 		struct bnxt_pf_info *pf = &bp->pf;
6338 
6339 		if (fltr->base.vf_idx >= pf->active_vfs)
6340 			return -EINVAL;
6341 
6342 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6343 	}
6344 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6345 	if (rc)
6346 		return rc;
6347 
6348 	req->target_id = cpu_to_le16(target_id);
6349 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6350 
6351 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6352 		req->flags |=
6353 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6354 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6355 	req->enables =
6356 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6357 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6358 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6359 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6360 	eth_broadcast_addr(req->l2_addr_mask);
6361 
6362 	if (fltr->l2_key.vlan) {
6363 		req->enables |=
6364 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6365 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6366 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6367 		req->num_vlans = 1;
6368 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6369 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6370 	}
6371 
6372 	resp = hwrm_req_hold(bp, req);
6373 	rc = hwrm_req_send(bp, req);
6374 	if (!rc) {
6375 		fltr->base.filter_id = resp->l2_filter_id;
6376 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6377 	}
6378 	hwrm_req_drop(bp, req);
6379 	return rc;
6380 }
6381 
6382 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6383 				     struct bnxt_ntuple_filter *fltr)
6384 {
6385 	struct hwrm_cfa_ntuple_filter_free_input *req;
6386 	int rc;
6387 
6388 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6389 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
6390 		return 0;
6391 
6392 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6393 	if (rc)
6394 		return rc;
6395 
6396 	req->ntuple_filter_id = fltr->base.filter_id;
6397 	return hwrm_req_send(bp, req);
6398 }
6399 
6400 #define BNXT_NTP_FLTR_FLAGS					\
6401 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6402 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6403 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6404 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6405 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6406 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6407 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6408 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6409 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6410 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6411 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6412 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6413 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6414 
6415 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6416 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6417 
6418 void bnxt_fill_ipv6_mask(__be32 mask[4])
6419 {
6420 	int i;
6421 
6422 	for (i = 0; i < 4; i++)
6423 		mask[i] = cpu_to_be32(~0);
6424 }
6425 
6426 static void
6427 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6428 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6429 			  struct bnxt_ntuple_filter *fltr)
6430 {
6431 	u16 rxq = fltr->base.rxq;
6432 
6433 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6434 		struct ethtool_rxfh_context *ctx;
6435 		struct bnxt_rss_ctx *rss_ctx;
6436 		struct bnxt_vnic_info *vnic;
6437 
6438 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6439 			      fltr->base.fw_vnic_id);
6440 		if (ctx) {
6441 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6442 			vnic = &rss_ctx->vnic;
6443 
6444 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6445 		}
6446 		return;
6447 	}
6448 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6449 		struct bnxt_vnic_info *vnic;
6450 		u32 enables;
6451 
6452 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6453 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6454 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6455 		req->enables |= cpu_to_le32(enables);
6456 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6457 	} else {
6458 		u32 flags;
6459 
6460 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6461 		req->flags |= cpu_to_le32(flags);
6462 		req->dst_id = cpu_to_le16(rxq);
6463 	}
6464 }
6465 
6466 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6467 				      struct bnxt_ntuple_filter *fltr)
6468 {
6469 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6470 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6471 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6472 	struct flow_keys *keys = &fltr->fkeys;
6473 	struct bnxt_l2_filter *l2_fltr;
6474 	struct bnxt_vnic_info *vnic;
6475 	int rc;
6476 
6477 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6478 	if (rc)
6479 		return rc;
6480 
6481 	l2_fltr = fltr->l2_fltr;
6482 	req->l2_filter_id = l2_fltr->base.filter_id;
6483 
6484 	if (fltr->base.flags & BNXT_ACT_DROP) {
6485 		req->flags =
6486 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6487 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6488 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6489 	} else {
6490 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6491 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6492 	}
6493 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6494 
6495 	req->ethertype = htons(ETH_P_IP);
6496 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6497 	req->ip_protocol = keys->basic.ip_proto;
6498 
6499 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6500 		req->ethertype = htons(ETH_P_IPV6);
6501 		req->ip_addr_type =
6502 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6503 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6504 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6505 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6506 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6507 	} else {
6508 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6509 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6510 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6511 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6512 	}
6513 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6514 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6515 		req->tunnel_type =
6516 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6517 	}
6518 
6519 	req->src_port = keys->ports.src;
6520 	req->src_port_mask = masks->ports.src;
6521 	req->dst_port = keys->ports.dst;
6522 	req->dst_port_mask = masks->ports.dst;
6523 
6524 	resp = hwrm_req_hold(bp, req);
6525 	rc = hwrm_req_send(bp, req);
6526 	if (!rc)
6527 		fltr->base.filter_id = resp->ntuple_filter_id;
6528 	hwrm_req_drop(bp, req);
6529 	return rc;
6530 }
6531 
6532 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6533 				     const u8 *mac_addr)
6534 {
6535 	struct bnxt_l2_filter *fltr;
6536 	struct bnxt_l2_key key;
6537 	int rc;
6538 
6539 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6540 	key.vlan = 0;
6541 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6542 	if (IS_ERR(fltr))
6543 		return PTR_ERR(fltr);
6544 
6545 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6546 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6547 	if (rc)
6548 		bnxt_del_l2_filter(bp, fltr);
6549 	else
6550 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6551 	return rc;
6552 }
6553 
6554 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6555 {
6556 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6557 
6558 	/* Any associated ntuple filters will also be cleared by firmware. */
6559 	for (i = 0; i < num_of_vnics; i++) {
6560 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6561 
6562 		for (j = 0; j < vnic->uc_filter_count; j++) {
6563 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6564 
6565 			bnxt_hwrm_l2_filter_free(bp, fltr);
6566 			bnxt_del_l2_filter(bp, fltr);
6567 		}
6568 		vnic->uc_filter_count = 0;
6569 	}
6570 }
6571 
6572 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6573 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6574 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6575 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6576 
6577 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6578 					   struct hwrm_vnic_tpa_cfg_input *req)
6579 {
6580 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6581 
6582 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6583 		return;
6584 
6585 	if (bp->vxlan_port)
6586 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6587 	if (bp->vxlan_gpe_port)
6588 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6589 	if (bp->nge_port)
6590 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6591 
6592 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6593 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6594 }
6595 
6596 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6597 			   u32 tpa_flags)
6598 {
6599 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6600 	struct hwrm_vnic_tpa_cfg_input *req;
6601 	int rc;
6602 
6603 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6604 		return 0;
6605 
6606 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6607 	if (rc)
6608 		return rc;
6609 
6610 	if (tpa_flags) {
6611 		u16 mss = bp->dev->mtu - 40;
6612 		u32 nsegs, n, segs = 0, flags;
6613 
6614 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6615 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6616 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6617 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6618 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6619 		if (tpa_flags & BNXT_FLAG_GRO)
6620 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6621 
6622 		req->flags = cpu_to_le32(flags);
6623 
6624 		req->enables =
6625 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6626 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6627 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6628 
6629 		/* Number of segs are log2 units, and first packet is not
6630 		 * included as part of this units.
6631 		 */
6632 		if (mss <= BNXT_RX_PAGE_SIZE) {
6633 			n = BNXT_RX_PAGE_SIZE / mss;
6634 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6635 		} else {
6636 			n = mss / BNXT_RX_PAGE_SIZE;
6637 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6638 				n++;
6639 			nsegs = (MAX_SKB_FRAGS - n) / n;
6640 		}
6641 
6642 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6643 			segs = MAX_TPA_SEGS_P5;
6644 			max_aggs = bp->max_tpa;
6645 		} else {
6646 			segs = ilog2(nsegs);
6647 		}
6648 		req->max_agg_segs = cpu_to_le16(segs);
6649 		req->max_aggs = cpu_to_le16(max_aggs);
6650 
6651 		req->min_agg_len = cpu_to_le32(512);
6652 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6653 	}
6654 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6655 
6656 	return hwrm_req_send(bp, req);
6657 }
6658 
6659 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6660 {
6661 	struct bnxt_ring_grp_info *grp_info;
6662 
6663 	grp_info = &bp->grp_info[ring->grp_idx];
6664 	return grp_info->cp_fw_ring_id;
6665 }
6666 
6667 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6668 {
6669 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6670 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6671 	else
6672 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6673 }
6674 
6675 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6676 {
6677 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6678 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6679 	else
6680 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6681 }
6682 
6683 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6684 {
6685 	int entries;
6686 
6687 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6688 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6689 	else
6690 		entries = HW_HASH_INDEX_SIZE;
6691 
6692 	bp->rss_indir_tbl_entries = entries;
6693 	bp->rss_indir_tbl =
6694 		kmalloc_objs(*bp->rss_indir_tbl, entries);
6695 	if (!bp->rss_indir_tbl)
6696 		return -ENOMEM;
6697 
6698 	return 0;
6699 }
6700 
6701 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6702 				 struct ethtool_rxfh_context *rss_ctx)
6703 {
6704 	u16 max_rings, max_entries, pad, i;
6705 	u32 *rss_indir_tbl;
6706 
6707 	if (!bp->rx_nr_rings)
6708 		return;
6709 
6710 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6711 		max_rings = bp->rx_nr_rings - 1;
6712 	else
6713 		max_rings = bp->rx_nr_rings;
6714 
6715 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6716 	if (rss_ctx)
6717 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6718 	else
6719 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6720 
6721 	for (i = 0; i < max_entries; i++)
6722 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6723 
6724 	pad = bp->rss_indir_tbl_entries - max_entries;
6725 	if (pad)
6726 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6727 }
6728 
6729 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6730 {
6731 	u32 i, tbl_size, max_ring = 0;
6732 
6733 	if (!bp->rss_indir_tbl)
6734 		return 0;
6735 
6736 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6737 	for (i = 0; i < tbl_size; i++)
6738 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6739 	return max_ring;
6740 }
6741 
6742 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6743 {
6744 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6745 		if (!rx_rings)
6746 			return 0;
6747 		if (bp->rss_cap & BNXT_RSS_CAP_LARGE_RSS_CTX)
6748 			return BNXT_RSS_TABLE_MAX_TBL_P5;
6749 
6750 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6751 					       BNXT_RSS_TABLE_ENTRIES_P5);
6752 	}
6753 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6754 		return 2;
6755 	return 1;
6756 }
6757 
6758 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6759 {
6760 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6761 	u16 i, j, min_j = bp->rx_nr_rings - 1;
6762 
6763 	if (!vnic->rss_table)
6764 		goto skip_rss_tbl;
6765 
6766 	/* Fill the RSS indirection table with ring group ids */
6767 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6768 		if (!no_rss)
6769 			j = bp->rss_indir_tbl[i];
6770 		min_j = min(j, min_j);
6771 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6772 	}
6773 
6774 skip_rss_tbl:
6775 	if (vnic->rss_table && !no_rss)
6776 		vnic->default_rx_ring = min_j;
6777 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6778 		vnic->default_rx_ring = vnic->vnic_id - 1;
6779 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6780 		vnic->default_rx_ring = bp->rx_nr_rings - 1;
6781 	else
6782 		vnic->default_rx_ring = 0;
6783 }
6784 
6785 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6786 				    struct bnxt_vnic_info *vnic)
6787 {
6788 	u16 tbl_size, i, min_j = bp->rx_nr_rings - 1;
6789 	__le16 *ring_tbl = vnic->rss_table;
6790 	struct bnxt_rx_ring_info *rxr;
6791 
6792 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6793 
6794 	for (i = 0; i < tbl_size; i++) {
6795 		u16 ring_id, j;
6796 
6797 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6798 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6799 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6800 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6801 		else
6802 			j = bp->rss_indir_tbl[i];
6803 		min_j = min(j, min_j);
6804 		rxr = &bp->rx_ring[j];
6805 
6806 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6807 		*ring_tbl++ = cpu_to_le16(ring_id);
6808 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6809 		*ring_tbl++ = cpu_to_le16(ring_id);
6810 	}
6811 	vnic->default_rx_ring = min_j;
6812 }
6813 
6814 static void
6815 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6816 			 struct bnxt_vnic_info *vnic)
6817 {
6818 	if (bp->flags & BNXT_FLAG_CHIP_P7)
6819 		req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6820 
6821 	if (bp->rss_hash_delta) {
6822 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6823 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6824 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6825 		else
6826 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6827 	} else {
6828 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6829 	}
6830 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6831 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6832 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6833 }
6834 
6835 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6836 				  bool set_rss)
6837 {
6838 	struct hwrm_vnic_rss_cfg_input *req;
6839 	int rc;
6840 
6841 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6842 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6843 		return 0;
6844 
6845 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6846 	if (rc)
6847 		return rc;
6848 
6849 	if (set_rss)
6850 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6851 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6852 	return hwrm_req_send(bp, req);
6853 }
6854 
6855 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6856 				     struct bnxt_vnic_info *vnic, bool set_rss)
6857 {
6858 	struct hwrm_vnic_rss_cfg_input *req;
6859 	dma_addr_t ring_tbl_map;
6860 	u32 i, nr_ctxs;
6861 	int rc;
6862 
6863 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6864 	if (rc)
6865 		return rc;
6866 
6867 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6868 	if (!set_rss)
6869 		return hwrm_req_send(bp, req);
6870 
6871 	bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6872 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6873 	ring_tbl_map = vnic->rss_table_dma_addr;
6874 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6875 
6876 	hwrm_req_hold(bp, req);
6877 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6878 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6879 		req->ring_table_pair_index = i;
6880 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6881 		rc = hwrm_req_send(bp, req);
6882 		if (rc)
6883 			goto exit;
6884 	}
6885 
6886 exit:
6887 	hwrm_req_drop(bp, req);
6888 	return rc;
6889 }
6890 
6891 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6892 {
6893 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6894 	struct hwrm_vnic_rss_qcfg_output *resp;
6895 	struct hwrm_vnic_rss_qcfg_input *req;
6896 
6897 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6898 		return;
6899 
6900 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6901 	/* all contexts configured to same hash_type, zero always exists */
6902 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6903 	resp = hwrm_req_hold(bp, req);
6904 	if (!hwrm_req_send(bp, req)) {
6905 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6906 		bp->rss_hash_delta = 0;
6907 	}
6908 	hwrm_req_drop(bp, req);
6909 }
6910 
6911 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6912 {
6913 	u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh;
6914 	struct hwrm_vnic_plcmodes_cfg_input *req;
6915 	int rc;
6916 
6917 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6918 	if (rc)
6919 		return rc;
6920 
6921 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6922 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6923 	req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6924 
6925 	if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
6926 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6927 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6928 		req->enables |=
6929 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6930 		req->hds_threshold = cpu_to_le16(hds_thresh);
6931 	}
6932 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6933 	return hwrm_req_send(bp, req);
6934 }
6935 
6936 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6937 					struct bnxt_vnic_info *vnic,
6938 					u16 ctx_idx)
6939 {
6940 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6941 
6942 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6943 		return;
6944 
6945 	req->rss_cos_lb_ctx_id =
6946 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6947 
6948 	hwrm_req_send(bp, req);
6949 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6950 }
6951 
6952 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6953 {
6954 	int i, j;
6955 
6956 	for (i = 0; i < bp->nr_vnics; i++) {
6957 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6958 
6959 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6960 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6961 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6962 		}
6963 	}
6964 	bp->rsscos_nr_ctxs = 0;
6965 }
6966 
6967 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6968 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6969 {
6970 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6971 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6972 	int rc;
6973 
6974 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6975 	if (rc)
6976 		return rc;
6977 
6978 	resp = hwrm_req_hold(bp, req);
6979 	rc = hwrm_req_send(bp, req);
6980 	if (!rc)
6981 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6982 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6983 	hwrm_req_drop(bp, req);
6984 
6985 	return rc;
6986 }
6987 
6988 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6989 {
6990 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6991 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6992 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6993 }
6994 
6995 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6996 {
6997 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6998 	struct hwrm_vnic_cfg_input *req;
6999 	unsigned int ring = 0, grp_idx;
7000 	u16 def_vlan = 0;
7001 	int rc;
7002 
7003 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
7004 	if (rc)
7005 		return rc;
7006 
7007 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7008 		struct bnxt_rx_ring_info *rxr;
7009 
7010 		rxr = &bp->rx_ring[vnic->default_rx_ring];
7011 		req->default_rx_ring_id =
7012 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
7013 		req->default_cmpl_ring_id =
7014 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
7015 		req->enables =
7016 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
7017 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
7018 		goto vnic_mru;
7019 	}
7020 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
7021 	/* Only RSS support for now TBD: COS & LB */
7022 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
7023 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
7024 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
7025 					   VNIC_CFG_REQ_ENABLES_MRU);
7026 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
7027 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
7028 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
7029 					   VNIC_CFG_REQ_ENABLES_MRU);
7030 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
7031 	} else {
7032 		req->rss_rule = cpu_to_le16(0xffff);
7033 	}
7034 
7035 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
7036 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
7037 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
7038 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
7039 	} else {
7040 		req->cos_rule = cpu_to_le16(0xffff);
7041 	}
7042 
7043 	ring = vnic->default_rx_ring;
7044 	grp_idx = bp->rx_ring[ring].bnapi->index;
7045 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
7046 	req->lb_rule = cpu_to_le16(0xffff);
7047 vnic_mru:
7048 	vnic->mru = bp->dev->mtu + VLAN_ETH_HLEN;
7049 	req->mru = cpu_to_le16(vnic->mru);
7050 
7051 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
7052 #ifdef CONFIG_BNXT_SRIOV
7053 	if (BNXT_VF(bp))
7054 		def_vlan = bp->vf.vlan;
7055 #endif
7056 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
7057 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
7058 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT &&
7059 	    bnxt_ulp_registered(bp->edev[BNXT_AUXDEV_RDMA]))
7060 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
7061 
7062 	return hwrm_req_send(bp, req);
7063 }
7064 
7065 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
7066 				    struct bnxt_vnic_info *vnic)
7067 {
7068 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
7069 		struct hwrm_vnic_free_input *req;
7070 
7071 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
7072 			return;
7073 
7074 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
7075 
7076 		hwrm_req_send(bp, req);
7077 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
7078 	}
7079 }
7080 
7081 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
7082 {
7083 	u16 i;
7084 
7085 	for (i = 0; i < bp->nr_vnics; i++)
7086 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
7087 }
7088 
7089 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
7090 			 unsigned int start_rx_ring_idx,
7091 			 unsigned int nr_rings)
7092 {
7093 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
7094 	struct hwrm_vnic_alloc_output *resp;
7095 	struct hwrm_vnic_alloc_input *req;
7096 	int rc;
7097 
7098 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
7099 	if (rc)
7100 		return rc;
7101 
7102 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7103 		goto vnic_no_ring_grps;
7104 
7105 	/* map ring groups to this vnic */
7106 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
7107 		grp_idx = bp->rx_ring[i].bnapi->index;
7108 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
7109 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
7110 				   j, nr_rings);
7111 			break;
7112 		}
7113 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
7114 	}
7115 
7116 vnic_no_ring_grps:
7117 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
7118 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
7119 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
7120 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
7121 
7122 	resp = hwrm_req_hold(bp, req);
7123 	rc = hwrm_req_send(bp, req);
7124 	if (!rc)
7125 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
7126 	hwrm_req_drop(bp, req);
7127 	return rc;
7128 }
7129 
7130 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
7131 {
7132 	struct hwrm_vnic_qcaps_output *resp;
7133 	struct hwrm_vnic_qcaps_input *req;
7134 	int rc;
7135 
7136 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
7137 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
7138 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
7139 	if (bp->hwrm_spec_code < 0x10600)
7140 		return 0;
7141 
7142 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
7143 	if (rc)
7144 		return rc;
7145 
7146 	resp = hwrm_req_hold(bp, req);
7147 	rc = hwrm_req_send(bp, req);
7148 	if (!rc) {
7149 		u32 flags = le32_to_cpu(resp->flags);
7150 
7151 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
7152 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
7153 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
7154 		if (flags &
7155 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
7156 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
7157 
7158 		/* Older P5 fw before EXT_HW_STATS support did not set
7159 		 * VLAN_STRIP_CAP properly.
7160 		 */
7161 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
7162 		    (BNXT_CHIP_P5(bp) &&
7163 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
7164 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
7165 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
7166 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
7167 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
7168 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
7169 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
7170 		if (bp->max_tpa_v2) {
7171 			if (BNXT_CHIP_P5(bp))
7172 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
7173 			else
7174 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
7175 		}
7176 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
7177 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
7178 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
7179 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
7180 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
7181 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
7182 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
7183 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
7184 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
7185 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
7186 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPV6_FLOW_LABEL_CAP)
7187 			bp->rss_cap |= BNXT_RSS_CAP_IPV6_FLOW_LABEL_RSS_CAP;
7188 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
7189 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
7190 	}
7191 	hwrm_req_drop(bp, req);
7192 	return rc;
7193 }
7194 
7195 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
7196 {
7197 	struct hwrm_ring_grp_alloc_output *resp;
7198 	struct hwrm_ring_grp_alloc_input *req;
7199 	int rc;
7200 	u16 i;
7201 
7202 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7203 		return 0;
7204 
7205 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
7206 	if (rc)
7207 		return rc;
7208 
7209 	resp = hwrm_req_hold(bp, req);
7210 	for (i = 0; i < bp->rx_nr_rings; i++) {
7211 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
7212 
7213 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
7214 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
7215 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
7216 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
7217 
7218 		rc = hwrm_req_send(bp, req);
7219 
7220 		if (rc)
7221 			break;
7222 
7223 		bp->grp_info[grp_idx].fw_grp_id =
7224 			le32_to_cpu(resp->ring_group_id);
7225 	}
7226 	hwrm_req_drop(bp, req);
7227 	return rc;
7228 }
7229 
7230 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
7231 {
7232 	struct hwrm_ring_grp_free_input *req;
7233 	u16 i;
7234 
7235 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7236 		return;
7237 
7238 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
7239 		return;
7240 
7241 	hwrm_req_hold(bp, req);
7242 	for (i = 0; i < bp->cp_nr_rings; i++) {
7243 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
7244 			continue;
7245 		req->ring_group_id =
7246 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
7247 
7248 		hwrm_req_send(bp, req);
7249 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
7250 	}
7251 	hwrm_req_drop(bp, req);
7252 }
7253 
7254 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type,
7255 				       struct hwrm_ring_alloc_input *req,
7256 				       struct bnxt_rx_ring_info *rxr,
7257 				       struct bnxt_ring_struct *ring)
7258 {
7259 	struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx];
7260 	u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID |
7261 		      RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID;
7262 
7263 	if (ring_type == HWRM_RING_ALLOC_AGG) {
7264 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
7265 		req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
7266 		req->rx_buf_size = cpu_to_le16(rxr->rx_page_size);
7267 		enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID;
7268 	} else {
7269 		req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
7270 		if (NET_IP_ALIGN == 2)
7271 			req->flags =
7272 				cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD);
7273 	}
7274 	req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7275 	req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7276 	req->enables |= cpu_to_le32(enables);
7277 }
7278 
7279 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
7280 				    struct bnxt_rx_ring_info *rxr,
7281 				    struct bnxt_ring_struct *ring,
7282 				    u32 ring_type, u32 map_index)
7283 {
7284 	struct hwrm_ring_alloc_output *resp;
7285 	struct hwrm_ring_alloc_input *req;
7286 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
7287 	struct bnxt_ring_grp_info *grp_info;
7288 	int rc, err = 0;
7289 	u16 ring_id;
7290 
7291 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
7292 	if (rc)
7293 		goto exit;
7294 
7295 	req->enables = 0;
7296 	if (rmem->nr_pages > 1) {
7297 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
7298 		/* Page size is in log2 units */
7299 		req->page_size = BNXT_PAGE_SHIFT;
7300 		req->page_tbl_depth = 1;
7301 	} else {
7302 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
7303 	}
7304 	req->fbo = 0;
7305 	/* Association of ring index with doorbell index and MSIX number */
7306 	req->logical_id = cpu_to_le16(map_index);
7307 
7308 	switch (ring_type) {
7309 	case HWRM_RING_ALLOC_TX: {
7310 		struct bnxt_tx_ring_info *txr;
7311 		u16 flags = 0;
7312 
7313 		txr = container_of(ring, struct bnxt_tx_ring_info,
7314 				   tx_ring_struct);
7315 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
7316 		/* Association of transmit ring with completion ring */
7317 		grp_info = &bp->grp_info[ring->grp_idx];
7318 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
7319 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
7320 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7321 		req->queue_id = cpu_to_le16(ring->queue_id);
7322 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
7323 			req->cmpl_coal_cnt =
7324 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
7325 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
7326 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
7327 		req->flags = cpu_to_le16(flags);
7328 		break;
7329 	}
7330 	case HWRM_RING_ALLOC_RX:
7331 	case HWRM_RING_ALLOC_AGG:
7332 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
7333 		req->length = (ring_type == HWRM_RING_ALLOC_RX) ?
7334 			      cpu_to_le32(bp->rx_ring_mask + 1) :
7335 			      cpu_to_le32(bp->rx_agg_ring_mask + 1);
7336 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7337 			bnxt_set_rx_ring_params_p5(bp, ring_type, req,
7338 						   rxr, ring);
7339 		break;
7340 	case HWRM_RING_ALLOC_CMPL:
7341 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7342 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7343 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7344 			/* Association of cp ring with nq */
7345 			grp_info = &bp->grp_info[map_index];
7346 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7347 			req->cq_handle = cpu_to_le64(ring->handle);
7348 			req->enables |= cpu_to_le32(
7349 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7350 		} else {
7351 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7352 		}
7353 		break;
7354 	case HWRM_RING_ALLOC_NQ:
7355 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7356 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7357 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7358 		break;
7359 	default:
7360 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7361 			   ring_type);
7362 		return -EINVAL;
7363 	}
7364 
7365 	resp = hwrm_req_hold(bp, req);
7366 	rc = hwrm_req_send(bp, req);
7367 	err = le16_to_cpu(resp->error_code);
7368 	ring_id = le16_to_cpu(resp->ring_id);
7369 	hwrm_req_drop(bp, req);
7370 
7371 exit:
7372 	if (rc || err) {
7373 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7374 			   ring_type, rc, err);
7375 		return -EIO;
7376 	}
7377 	ring->fw_ring_id = ring_id;
7378 	return rc;
7379 }
7380 
7381 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7382 {
7383 	int rc;
7384 
7385 	if (BNXT_PF(bp)) {
7386 		struct hwrm_func_cfg_input *req;
7387 
7388 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7389 		if (rc)
7390 			return rc;
7391 
7392 		req->fid = cpu_to_le16(0xffff);
7393 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7394 		req->async_event_cr = cpu_to_le16(idx);
7395 		return hwrm_req_send(bp, req);
7396 	} else {
7397 		struct hwrm_func_vf_cfg_input *req;
7398 
7399 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7400 		if (rc)
7401 			return rc;
7402 
7403 		req->enables =
7404 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7405 		req->async_event_cr = cpu_to_le16(idx);
7406 		return hwrm_req_send(bp, req);
7407 	}
7408 }
7409 
7410 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7411 			     u32 ring_type)
7412 {
7413 	switch (ring_type) {
7414 	case HWRM_RING_ALLOC_TX:
7415 		db->db_ring_mask = bp->tx_ring_mask;
7416 		break;
7417 	case HWRM_RING_ALLOC_RX:
7418 		db->db_ring_mask = bp->rx_ring_mask;
7419 		break;
7420 	case HWRM_RING_ALLOC_AGG:
7421 		db->db_ring_mask = bp->rx_agg_ring_mask;
7422 		break;
7423 	case HWRM_RING_ALLOC_CMPL:
7424 	case HWRM_RING_ALLOC_NQ:
7425 		db->db_ring_mask = bp->cp_ring_mask;
7426 		break;
7427 	}
7428 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7429 		db->db_epoch_mask = db->db_ring_mask + 1;
7430 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7431 	}
7432 }
7433 
7434 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7435 			u32 map_idx, u32 xid)
7436 {
7437 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7438 		switch (ring_type) {
7439 		case HWRM_RING_ALLOC_TX:
7440 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7441 			break;
7442 		case HWRM_RING_ALLOC_RX:
7443 		case HWRM_RING_ALLOC_AGG:
7444 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7445 			break;
7446 		case HWRM_RING_ALLOC_CMPL:
7447 			db->db_key64 = DBR_PATH_L2;
7448 			break;
7449 		case HWRM_RING_ALLOC_NQ:
7450 			db->db_key64 = DBR_PATH_L2;
7451 			break;
7452 		}
7453 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7454 
7455 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7456 			db->db_key64 |= DBR_VALID;
7457 
7458 		db->doorbell = bp->bar1 + bp->db_offset;
7459 	} else {
7460 		db->doorbell = bp->bar1 + map_idx * 0x80;
7461 		switch (ring_type) {
7462 		case HWRM_RING_ALLOC_TX:
7463 			db->db_key32 = DB_KEY_TX;
7464 			break;
7465 		case HWRM_RING_ALLOC_RX:
7466 		case HWRM_RING_ALLOC_AGG:
7467 			db->db_key32 = DB_KEY_RX;
7468 			break;
7469 		case HWRM_RING_ALLOC_CMPL:
7470 			db->db_key32 = DB_KEY_CP;
7471 			break;
7472 		}
7473 	}
7474 	bnxt_set_db_mask(bp, db, ring_type);
7475 }
7476 
7477 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7478 				   struct bnxt_rx_ring_info *rxr)
7479 {
7480 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7481 	struct bnxt_napi *bnapi = rxr->bnapi;
7482 	u32 type = HWRM_RING_ALLOC_RX;
7483 	u32 map_idx = bnapi->index;
7484 	int rc;
7485 
7486 	rc = hwrm_ring_alloc_send_msg(bp, rxr, ring, type, map_idx);
7487 	if (rc)
7488 		return rc;
7489 
7490 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7491 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7492 
7493 	return 0;
7494 }
7495 
7496 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7497 				       struct bnxt_rx_ring_info *rxr)
7498 {
7499 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7500 	u32 type = HWRM_RING_ALLOC_AGG;
7501 	u32 grp_idx = ring->grp_idx;
7502 	u32 map_idx;
7503 	int rc;
7504 
7505 	map_idx = grp_idx + bp->rx_nr_rings;
7506 	rc = hwrm_ring_alloc_send_msg(bp, rxr, ring, type, map_idx);
7507 	if (rc)
7508 		return rc;
7509 
7510 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7511 		    ring->fw_ring_id);
7512 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7513 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7514 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7515 
7516 	return 0;
7517 }
7518 
7519 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp,
7520 				      struct bnxt_cp_ring_info *cpr)
7521 {
7522 	const u32 type = HWRM_RING_ALLOC_CMPL;
7523 	struct bnxt_napi *bnapi = cpr->bnapi;
7524 	struct bnxt_ring_struct *ring;
7525 	u32 map_idx = bnapi->index;
7526 	int rc;
7527 
7528 	ring = &cpr->cp_ring_struct;
7529 	ring->handle = BNXT_SET_NQ_HDL(cpr);
7530 	rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, map_idx);
7531 	if (rc)
7532 		return rc;
7533 	bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7534 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7535 	return 0;
7536 }
7537 
7538 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp,
7539 				   struct bnxt_tx_ring_info *txr, u32 tx_idx)
7540 {
7541 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7542 	const u32 type = HWRM_RING_ALLOC_TX;
7543 	int rc;
7544 
7545 	rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, tx_idx);
7546 	if (rc)
7547 		return rc;
7548 	bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id);
7549 	return 0;
7550 }
7551 
7552 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7553 {
7554 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7555 	int i, rc = 0;
7556 	u32 type;
7557 
7558 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7559 		type = HWRM_RING_ALLOC_NQ;
7560 	else
7561 		type = HWRM_RING_ALLOC_CMPL;
7562 	for (i = 0; i < bp->cp_nr_rings; i++) {
7563 		struct bnxt_napi *bnapi = bp->bnapi[i];
7564 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7565 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7566 		u32 map_idx = ring->map_idx;
7567 		unsigned int vector;
7568 
7569 		vector = bp->irq_tbl[map_idx].vector;
7570 		disable_irq_nosync(vector);
7571 		rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, map_idx);
7572 		if (rc) {
7573 			enable_irq(vector);
7574 			goto err_out;
7575 		}
7576 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7577 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7578 		enable_irq(vector);
7579 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7580 
7581 		if (!i) {
7582 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7583 			if (rc)
7584 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7585 		}
7586 	}
7587 
7588 	for (i = 0; i < bp->tx_nr_rings; i++) {
7589 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7590 
7591 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7592 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
7593 			if (rc)
7594 				goto err_out;
7595 		}
7596 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i);
7597 		if (rc)
7598 			goto err_out;
7599 	}
7600 
7601 	for (i = 0; i < bp->rx_nr_rings; i++) {
7602 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7603 
7604 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7605 		if (rc)
7606 			goto err_out;
7607 		/* If we have agg rings, post agg buffers first. */
7608 		if (!agg_rings)
7609 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7610 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7611 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
7612 			if (rc)
7613 				goto err_out;
7614 		}
7615 	}
7616 
7617 	if (agg_rings) {
7618 		for (i = 0; i < bp->rx_nr_rings; i++) {
7619 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7620 			if (rc)
7621 				goto err_out;
7622 		}
7623 	}
7624 err_out:
7625 	return rc;
7626 }
7627 
7628 static void bnxt_cancel_dim(struct bnxt *bp)
7629 {
7630 	int i;
7631 
7632 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7633 	 * if NAPI is enabled.
7634 	 */
7635 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7636 		return;
7637 
7638 	/* Make sure NAPI sees that the VNIC is disabled */
7639 	synchronize_net();
7640 	for (i = 0; i < bp->rx_nr_rings; i++) {
7641 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7642 		struct bnxt_napi *bnapi = rxr->bnapi;
7643 
7644 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7645 	}
7646 }
7647 
7648 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7649 				   struct bnxt_ring_struct *ring,
7650 				   u32 ring_type, int cmpl_ring_id)
7651 {
7652 	struct hwrm_ring_free_output *resp;
7653 	struct hwrm_ring_free_input *req;
7654 	u16 error_code = 0;
7655 	int rc;
7656 
7657 	if (BNXT_NO_FW_ACCESS(bp))
7658 		return 0;
7659 
7660 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7661 	if (rc)
7662 		goto exit;
7663 
7664 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7665 	req->ring_type = ring_type;
7666 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7667 
7668 	resp = hwrm_req_hold(bp, req);
7669 	rc = hwrm_req_send(bp, req);
7670 	error_code = le16_to_cpu(resp->error_code);
7671 	hwrm_req_drop(bp, req);
7672 exit:
7673 	if (rc || error_code) {
7674 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7675 			   ring_type, rc, error_code);
7676 		return -EIO;
7677 	}
7678 	return 0;
7679 }
7680 
7681 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp,
7682 				   struct bnxt_tx_ring_info *txr,
7683 				   bool close_path)
7684 {
7685 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7686 	u32 cmpl_ring_id;
7687 
7688 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7689 		return;
7690 
7691 	cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) :
7692 		       INVALID_HW_RING_ID;
7693 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX,
7694 				cmpl_ring_id);
7695 	ring->fw_ring_id = INVALID_HW_RING_ID;
7696 }
7697 
7698 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7699 				   struct bnxt_rx_ring_info *rxr,
7700 				   bool close_path)
7701 {
7702 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7703 	u32 grp_idx = rxr->bnapi->index;
7704 	u32 cmpl_ring_id;
7705 
7706 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7707 		return;
7708 
7709 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7710 	hwrm_ring_free_send_msg(bp, ring,
7711 				RING_FREE_REQ_RING_TYPE_RX,
7712 				close_path ? cmpl_ring_id :
7713 				INVALID_HW_RING_ID);
7714 	ring->fw_ring_id = INVALID_HW_RING_ID;
7715 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7716 }
7717 
7718 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7719 				       struct bnxt_rx_ring_info *rxr,
7720 				       bool close_path)
7721 {
7722 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7723 	u32 grp_idx = rxr->bnapi->index;
7724 	u32 type, cmpl_ring_id;
7725 
7726 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7727 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7728 	else
7729 		type = RING_FREE_REQ_RING_TYPE_RX;
7730 
7731 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7732 		return;
7733 
7734 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7735 	hwrm_ring_free_send_msg(bp, ring, type,
7736 				close_path ? cmpl_ring_id :
7737 				INVALID_HW_RING_ID);
7738 	ring->fw_ring_id = INVALID_HW_RING_ID;
7739 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7740 }
7741 
7742 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp,
7743 				   struct bnxt_cp_ring_info *cpr)
7744 {
7745 	struct bnxt_ring_struct *ring;
7746 
7747 	ring = &cpr->cp_ring_struct;
7748 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7749 		return;
7750 
7751 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL,
7752 				INVALID_HW_RING_ID);
7753 	ring->fw_ring_id = INVALID_HW_RING_ID;
7754 }
7755 
7756 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
7757 {
7758 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7759 	int i, size = ring->ring_mem.page_size;
7760 
7761 	cpr->cp_raw_cons = 0;
7762 	cpr->toggle = 0;
7763 
7764 	for (i = 0; i < bp->cp_nr_pages; i++)
7765 		if (cpr->cp_desc_ring[i])
7766 			memset(cpr->cp_desc_ring[i], 0, size);
7767 }
7768 
7769 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7770 {
7771 	u32 type;
7772 	int i;
7773 
7774 	if (!bp->bnapi)
7775 		return;
7776 
7777 	for (i = 0; i < bp->tx_nr_rings; i++)
7778 		bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path);
7779 
7780 	bnxt_cancel_dim(bp);
7781 	for (i = 0; i < bp->rx_nr_rings; i++) {
7782 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7783 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7784 	}
7785 
7786 	/* The completion rings are about to be freed.  After that the
7787 	 * IRQ doorbell will not work anymore.  So we need to disable
7788 	 * IRQ here.
7789 	 */
7790 	bnxt_disable_int_sync(bp);
7791 
7792 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7793 		type = RING_FREE_REQ_RING_TYPE_NQ;
7794 	else
7795 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7796 	for (i = 0; i < bp->cp_nr_rings; i++) {
7797 		struct bnxt_napi *bnapi = bp->bnapi[i];
7798 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7799 		struct bnxt_ring_struct *ring;
7800 		int j;
7801 
7802 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++)
7803 			bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]);
7804 
7805 		ring = &cpr->cp_ring_struct;
7806 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7807 			hwrm_ring_free_send_msg(bp, ring, type,
7808 						INVALID_HW_RING_ID);
7809 			ring->fw_ring_id = INVALID_HW_RING_ID;
7810 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7811 		}
7812 	}
7813 }
7814 
7815 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7816 			     bool shared);
7817 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7818 			   bool shared);
7819 
7820 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7821 {
7822 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7823 	struct hwrm_func_qcfg_output *resp;
7824 	struct hwrm_func_qcfg_input *req;
7825 	int rc;
7826 
7827 	if (bp->hwrm_spec_code < 0x10601)
7828 		return 0;
7829 
7830 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7831 	if (rc)
7832 		return rc;
7833 
7834 	req->fid = cpu_to_le16(0xffff);
7835 	resp = hwrm_req_hold(bp, req);
7836 	rc = hwrm_req_send(bp, req);
7837 	if (rc) {
7838 		hwrm_req_drop(bp, req);
7839 		return rc;
7840 	}
7841 
7842 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7843 	if (BNXT_NEW_RM(bp)) {
7844 		u16 cp, stats;
7845 
7846 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7847 		hw_resc->resv_hw_ring_grps =
7848 			le32_to_cpu(resp->alloc_hw_ring_grps);
7849 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7850 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7851 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7852 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7853 		hw_resc->resv_irqs = cp;
7854 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7855 			int rx = hw_resc->resv_rx_rings;
7856 			int tx = hw_resc->resv_tx_rings;
7857 
7858 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7859 				rx >>= 1;
7860 			if (cp < (rx + tx)) {
7861 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7862 				if (rc)
7863 					goto get_rings_exit;
7864 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7865 					rx <<= 1;
7866 				hw_resc->resv_rx_rings = rx;
7867 				hw_resc->resv_tx_rings = tx;
7868 			}
7869 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7870 			hw_resc->resv_hw_ring_grps = rx;
7871 		}
7872 		hw_resc->resv_cp_rings = cp;
7873 		hw_resc->resv_stat_ctxs = stats;
7874 	}
7875 get_rings_exit:
7876 	hwrm_req_drop(bp, req);
7877 	return rc;
7878 }
7879 
7880 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7881 {
7882 	struct hwrm_func_qcfg_output *resp;
7883 	struct hwrm_func_qcfg_input *req;
7884 	int rc;
7885 
7886 	if (bp->hwrm_spec_code < 0x10601)
7887 		return 0;
7888 
7889 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7890 	if (rc)
7891 		return rc;
7892 
7893 	req->fid = cpu_to_le16(fid);
7894 	resp = hwrm_req_hold(bp, req);
7895 	rc = hwrm_req_send(bp, req);
7896 	if (!rc)
7897 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7898 
7899 	hwrm_req_drop(bp, req);
7900 	return rc;
7901 }
7902 
7903 static bool bnxt_rfs_supported(struct bnxt *bp);
7904 
7905 static struct hwrm_func_cfg_input *
7906 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7907 {
7908 	struct hwrm_func_cfg_input *req;
7909 	u32 enables = 0;
7910 
7911 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7912 		return NULL;
7913 
7914 	req->fid = cpu_to_le16(0xffff);
7915 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7916 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7917 	if (BNXT_NEW_RM(bp)) {
7918 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7919 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7920 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7921 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7922 			enables |= hwr->cp_p5 ?
7923 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7924 		} else {
7925 			enables |= hwr->cp ?
7926 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7927 			enables |= hwr->grp ?
7928 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7929 		}
7930 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7931 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7932 					  0;
7933 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7934 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7935 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7936 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7937 			req->num_msix = cpu_to_le16(hwr->cp);
7938 		} else {
7939 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7940 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7941 		}
7942 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7943 		req->num_vnics = cpu_to_le16(hwr->vnic);
7944 	}
7945 	req->enables = cpu_to_le32(enables);
7946 	return req;
7947 }
7948 
7949 static struct hwrm_func_vf_cfg_input *
7950 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7951 {
7952 	struct hwrm_func_vf_cfg_input *req;
7953 	u32 enables = 0;
7954 
7955 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7956 		return NULL;
7957 
7958 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7959 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7960 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7961 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7962 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7963 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7964 		enables |= hwr->cp_p5 ?
7965 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7966 	} else {
7967 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7968 		enables |= hwr->grp ?
7969 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7970 	}
7971 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7972 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7973 
7974 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7975 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7976 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7977 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7978 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7979 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7980 	} else {
7981 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7982 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7983 	}
7984 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7985 	req->num_vnics = cpu_to_le16(hwr->vnic);
7986 
7987 	req->enables = cpu_to_le32(enables);
7988 	return req;
7989 }
7990 
7991 static int
7992 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7993 {
7994 	struct hwrm_func_cfg_input *req;
7995 	int rc;
7996 
7997 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7998 	if (!req)
7999 		return -ENOMEM;
8000 
8001 	if (!req->enables) {
8002 		hwrm_req_drop(bp, req);
8003 		return 0;
8004 	}
8005 
8006 	rc = hwrm_req_send(bp, req);
8007 	if (rc)
8008 		return rc;
8009 
8010 	if (bp->hwrm_spec_code < 0x10601)
8011 		bp->hw_resc.resv_tx_rings = hwr->tx;
8012 
8013 	return bnxt_hwrm_get_rings(bp);
8014 }
8015 
8016 static int
8017 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8018 {
8019 	struct hwrm_func_vf_cfg_input *req;
8020 	int rc;
8021 
8022 	if (!BNXT_NEW_RM(bp)) {
8023 		bp->hw_resc.resv_tx_rings = hwr->tx;
8024 		return 0;
8025 	}
8026 
8027 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
8028 	if (!req)
8029 		return -ENOMEM;
8030 
8031 	rc = hwrm_req_send(bp, req);
8032 	if (rc)
8033 		return rc;
8034 
8035 	return bnxt_hwrm_get_rings(bp);
8036 }
8037 
8038 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8039 {
8040 	if (BNXT_PF(bp))
8041 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
8042 	else
8043 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
8044 }
8045 
8046 int bnxt_nq_rings_in_use(struct bnxt *bp)
8047 {
8048 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
8049 }
8050 
8051 static int bnxt_cp_rings_in_use(struct bnxt *bp)
8052 {
8053 	int cp;
8054 
8055 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8056 		return bnxt_nq_rings_in_use(bp);
8057 
8058 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
8059 	return cp;
8060 }
8061 
8062 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
8063 {
8064 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
8065 }
8066 
8067 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8068 {
8069 	if (!hwr->grp)
8070 		return 0;
8071 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8072 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
8073 
8074 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
8075 			rss_ctx *= hwr->vnic;
8076 		return rss_ctx;
8077 	}
8078 	if (BNXT_VF(bp))
8079 		return BNXT_VF_MAX_RSS_CTX;
8080 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
8081 		return hwr->grp + 1;
8082 	return 1;
8083 }
8084 
8085 /* Check if a default RSS map needs to be setup.  This function is only
8086  * used on older firmware that does not require reserving RX rings.
8087  */
8088 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
8089 {
8090 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8091 
8092 	/* The RSS map is valid for RX rings set to resv_rx_rings */
8093 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
8094 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
8095 		if (!netif_is_rxfh_configured(bp->dev))
8096 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8097 	}
8098 }
8099 
8100 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
8101 {
8102 	if (bp->flags & BNXT_FLAG_RFS) {
8103 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
8104 			return 2 + bp->num_rss_ctx;
8105 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8106 			return rx_rings + 1;
8107 	}
8108 	return 1;
8109 }
8110 
8111 static void bnxt_get_total_resources(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8112 {
8113 	hwr->cp = bnxt_nq_rings_in_use(bp);
8114 	hwr->cp_p5 = 0;
8115 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8116 		hwr->cp_p5 = bnxt_cp_rings_in_use(bp);
8117 	hwr->tx = bp->tx_nr_rings;
8118 	hwr->rx = bp->rx_nr_rings;
8119 	hwr->grp = hwr->rx;
8120 	hwr->vnic = bnxt_get_total_vnics(bp, hwr->rx);
8121 	hwr->rss_ctx = bnxt_get_total_rss_ctxs(bp, hwr);
8122 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8123 		hwr->rx <<= 1;
8124 	hwr->stat = bnxt_get_func_stat_ctxs(bp);
8125 }
8126 
8127 static bool bnxt_need_reserve_rings(struct bnxt *bp)
8128 {
8129 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8130 	struct bnxt_hw_rings hwr;
8131 
8132 	bnxt_get_total_resources(bp, &hwr);
8133 
8134 	/* Old firmware does not need RX ring reservations but we still
8135 	 * need to setup a default RSS map when needed.  With new firmware
8136 	 * we go through RX ring reservations first and then set up the
8137 	 * RSS map for the successfully reserved RX rings when needed.
8138 	 */
8139 	if (!BNXT_NEW_RM(bp))
8140 		bnxt_check_rss_tbl_no_rmgr(bp);
8141 
8142 	if (hw_resc->resv_tx_rings != hwr.tx && bp->hwrm_spec_code >= 0x10601)
8143 		return true;
8144 
8145 	if (!BNXT_NEW_RM(bp))
8146 		return false;
8147 
8148 	if (hw_resc->resv_rx_rings != hwr.rx ||
8149 	    hw_resc->resv_vnics != hwr.vnic ||
8150 	    hw_resc->resv_stat_ctxs != hwr.stat ||
8151 	    hw_resc->resv_rsscos_ctxs != hwr.rss_ctx ||
8152 	    (hw_resc->resv_hw_ring_grps != hwr.grp &&
8153 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
8154 		return true;
8155 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8156 		if (hw_resc->resv_cp_rings != hwr.cp_p5)
8157 			return true;
8158 	} else if (hw_resc->resv_cp_rings != hwr.cp) {
8159 		return true;
8160 	}
8161 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
8162 	    hw_resc->resv_irqs != hwr.cp)
8163 		return true;
8164 	return false;
8165 }
8166 
8167 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8168 {
8169 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8170 
8171 	hwr->tx = hw_resc->resv_tx_rings;
8172 	if (BNXT_NEW_RM(bp)) {
8173 		hwr->rx = hw_resc->resv_rx_rings;
8174 		hwr->cp = hw_resc->resv_irqs;
8175 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8176 			hwr->cp_p5 = hw_resc->resv_cp_rings;
8177 		hwr->grp = hw_resc->resv_hw_ring_grps;
8178 		hwr->vnic = hw_resc->resv_vnics;
8179 		hwr->stat = hw_resc->resv_stat_ctxs;
8180 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
8181 	}
8182 }
8183 
8184 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8185 {
8186 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
8187 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
8188 }
8189 
8190 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
8191 
8192 static int __bnxt_reserve_rings(struct bnxt *bp)
8193 {
8194 	struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA];
8195 	struct bnxt_hw_rings hwr = {0};
8196 	int rx_rings, old_rx_rings, rc;
8197 	int cp = bp->cp_nr_rings;
8198 	int ulp_msix = 0;
8199 	bool sh = false;
8200 	int tx_cp;
8201 
8202 	if (!bnxt_need_reserve_rings(bp))
8203 		return 0;
8204 
8205 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(edev)) {
8206 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
8207 		if (!ulp_msix)
8208 			bnxt_set_ulp_stat_ctxs(bp, 0);
8209 		else
8210 			bnxt_set_dflt_ulp_stat_ctxs(bp);
8211 
8212 		if (ulp_msix > bp->ulp_num_msix_want)
8213 			ulp_msix = bp->ulp_num_msix_want;
8214 		hwr.cp = cp + ulp_msix;
8215 	} else {
8216 		hwr.cp = bnxt_nq_rings_in_use(bp);
8217 	}
8218 
8219 	hwr.tx = bp->tx_nr_rings;
8220 	hwr.rx = bp->rx_nr_rings;
8221 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
8222 		sh = true;
8223 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8224 		hwr.cp_p5 = hwr.rx + hwr.tx;
8225 
8226 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
8227 
8228 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8229 		hwr.rx <<= 1;
8230 	hwr.grp = bp->rx_nr_rings;
8231 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
8232 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
8233 	old_rx_rings = bp->hw_resc.resv_rx_rings;
8234 
8235 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
8236 	if (rc)
8237 		return rc;
8238 
8239 	bnxt_copy_reserved_rings(bp, &hwr);
8240 
8241 	rx_rings = hwr.rx;
8242 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8243 		if (hwr.rx >= 2) {
8244 			rx_rings = hwr.rx >> 1;
8245 		} else {
8246 			if (netif_running(bp->dev))
8247 				return -ENOMEM;
8248 
8249 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
8250 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
8251 			bp->dev->hw_features &= ~NETIF_F_LRO;
8252 			bp->dev->features &= ~NETIF_F_LRO;
8253 			bnxt_set_ring_params(bp);
8254 		}
8255 	}
8256 	rx_rings = min_t(int, rx_rings, hwr.grp);
8257 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
8258 	if (bnxt_ulp_registered(edev) && hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
8259 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
8260 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
8261 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
8262 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8263 		hwr.rx = rx_rings << 1;
8264 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
8265 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
8266 	if (hwr.tx != bp->tx_nr_rings) {
8267 		netdev_warn(bp->dev,
8268 			    "Able to reserve only %d out of %d requested TX rings\n",
8269 			    hwr.tx, bp->tx_nr_rings);
8270 	}
8271 	bp->tx_nr_rings = hwr.tx;
8272 
8273 	/* If we cannot reserve all the RX rings, reset the RSS map only
8274 	 * if absolutely necessary
8275 	 */
8276 	if (rx_rings != bp->rx_nr_rings) {
8277 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
8278 			    rx_rings, bp->rx_nr_rings);
8279 		if (netif_is_rxfh_configured(bp->dev) &&
8280 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
8281 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
8282 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
8283 			ethtool_rxfh_indir_lost(bp->dev);
8284 		}
8285 	}
8286 	bp->rx_nr_rings = rx_rings;
8287 	bp->cp_nr_rings = hwr.cp;
8288 
8289 	/* Fall back if we cannot reserve enough HW RSS contexts */
8290 	if ((bp->rss_cap & BNXT_RSS_CAP_LARGE_RSS_CTX) &&
8291 	    hwr.rss_ctx < bnxt_get_total_rss_ctxs(bp, &hwr))
8292 		bp->rss_cap &= ~BNXT_RSS_CAP_LARGE_RSS_CTX;
8293 
8294 	if (!bnxt_rings_ok(bp, &hwr))
8295 		return -ENOMEM;
8296 
8297 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
8298 	    !netif_is_rxfh_configured(bp->dev))
8299 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8300 
8301 	if (!bnxt_ulp_registered(edev) && BNXT_NEW_RM(bp)) {
8302 		int resv_msix, resv_ctx, ulp_ctxs;
8303 		struct bnxt_hw_resc *hw_resc;
8304 
8305 		hw_resc = &bp->hw_resc;
8306 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
8307 		ulp_msix = min_t(int, resv_msix, ulp_msix);
8308 		bnxt_set_ulp_msix_num(bp, ulp_msix);
8309 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
8310 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
8311 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
8312 	}
8313 
8314 	return rc;
8315 }
8316 
8317 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8318 {
8319 	struct hwrm_func_vf_cfg_input *req;
8320 	u32 flags;
8321 
8322 	if (!BNXT_NEW_RM(bp))
8323 		return 0;
8324 
8325 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
8326 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
8327 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8328 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8329 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8330 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
8331 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
8332 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8333 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8334 
8335 	req->flags = cpu_to_le32(flags);
8336 	return hwrm_req_send_silent(bp, req);
8337 }
8338 
8339 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8340 {
8341 	struct hwrm_func_cfg_input *req;
8342 	u32 flags;
8343 
8344 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
8345 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
8346 	if (BNXT_NEW_RM(bp)) {
8347 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8348 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8349 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8350 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
8351 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8352 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
8353 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
8354 		else
8355 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8356 	}
8357 
8358 	req->flags = cpu_to_le32(flags);
8359 	return hwrm_req_send_silent(bp, req);
8360 }
8361 
8362 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8363 {
8364 	if (bp->hwrm_spec_code < 0x10801)
8365 		return 0;
8366 
8367 	if (BNXT_PF(bp))
8368 		return bnxt_hwrm_check_pf_rings(bp, hwr);
8369 
8370 	return bnxt_hwrm_check_vf_rings(bp, hwr);
8371 }
8372 
8373 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
8374 {
8375 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8376 	struct hwrm_ring_aggint_qcaps_output *resp;
8377 	struct hwrm_ring_aggint_qcaps_input *req;
8378 	int rc;
8379 
8380 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
8381 	coal_cap->num_cmpl_dma_aggr_max = 63;
8382 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
8383 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
8384 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
8385 	coal_cap->int_lat_tmr_min_max = 65535;
8386 	coal_cap->int_lat_tmr_max_max = 65535;
8387 	coal_cap->num_cmpl_aggr_int_max = 65535;
8388 	coal_cap->timer_units = 80;
8389 
8390 	if (bp->hwrm_spec_code < 0x10902)
8391 		return;
8392 
8393 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
8394 		return;
8395 
8396 	resp = hwrm_req_hold(bp, req);
8397 	rc = hwrm_req_send_silent(bp, req);
8398 	if (!rc) {
8399 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8400 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8401 		coal_cap->num_cmpl_dma_aggr_max =
8402 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8403 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8404 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8405 		coal_cap->cmpl_aggr_dma_tmr_max =
8406 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8407 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8408 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8409 		coal_cap->int_lat_tmr_min_max =
8410 			le16_to_cpu(resp->int_lat_tmr_min_max);
8411 		coal_cap->int_lat_tmr_max_max =
8412 			le16_to_cpu(resp->int_lat_tmr_max_max);
8413 		coal_cap->num_cmpl_aggr_int_max =
8414 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8415 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8416 	}
8417 	hwrm_req_drop(bp, req);
8418 }
8419 
8420 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8421 {
8422 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8423 
8424 	return usec * 1000 / coal_cap->timer_units;
8425 }
8426 
8427 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8428 	struct bnxt_coal *hw_coal,
8429 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8430 {
8431 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8432 	u16 val, tmr, max, flags = hw_coal->flags;
8433 	u32 cmpl_params = coal_cap->cmpl_params;
8434 
8435 	max = hw_coal->bufs_per_record * 128;
8436 	if (hw_coal->budget)
8437 		max = hw_coal->bufs_per_record * hw_coal->budget;
8438 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8439 
8440 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8441 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8442 
8443 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8444 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8445 
8446 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8447 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8448 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8449 
8450 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8451 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8452 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8453 
8454 	/* min timer set to 1/2 of interrupt timer */
8455 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8456 		val = tmr / 2;
8457 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8458 		req->int_lat_tmr_min = cpu_to_le16(val);
8459 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8460 	}
8461 
8462 	/* buf timer set to 1/4 of interrupt timer */
8463 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8464 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8465 
8466 	if (cmpl_params &
8467 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8468 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8469 		val = clamp_t(u16, tmr, 1,
8470 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8471 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8472 		req->enables |=
8473 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8474 	}
8475 
8476 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8477 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8478 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8479 	req->flags = cpu_to_le16(flags);
8480 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8481 }
8482 
8483 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8484 				   struct bnxt_coal *hw_coal)
8485 {
8486 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8487 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8488 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8489 	u32 nq_params = coal_cap->nq_params;
8490 	u16 tmr;
8491 	int rc;
8492 
8493 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8494 		return 0;
8495 
8496 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8497 	if (rc)
8498 		return rc;
8499 
8500 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8501 	req->flags =
8502 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8503 
8504 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8505 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8506 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8507 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8508 	return hwrm_req_send(bp, req);
8509 }
8510 
8511 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8512 {
8513 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8514 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8515 	struct bnxt_coal coal;
8516 	int rc;
8517 
8518 	/* Tick values in micro seconds.
8519 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8520 	 */
8521 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8522 
8523 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8524 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8525 
8526 	if (!bnapi->rx_ring)
8527 		return -ENODEV;
8528 
8529 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8530 	if (rc)
8531 		return rc;
8532 
8533 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8534 
8535 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8536 
8537 	return hwrm_req_send(bp, req_rx);
8538 }
8539 
8540 static int
8541 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8542 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8543 {
8544 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8545 
8546 	req->ring_id = cpu_to_le16(ring_id);
8547 	return hwrm_req_send(bp, req);
8548 }
8549 
8550 static int
8551 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8552 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8553 {
8554 	struct bnxt_tx_ring_info *txr;
8555 	int i, rc;
8556 
8557 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8558 		u16 ring_id;
8559 
8560 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8561 		req->ring_id = cpu_to_le16(ring_id);
8562 		rc = hwrm_req_send(bp, req);
8563 		if (rc)
8564 			return rc;
8565 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8566 			return 0;
8567 	}
8568 	return 0;
8569 }
8570 
8571 int bnxt_hwrm_set_coal(struct bnxt *bp)
8572 {
8573 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8574 	int i, rc;
8575 
8576 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8577 	if (rc)
8578 		return rc;
8579 
8580 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8581 	if (rc) {
8582 		hwrm_req_drop(bp, req_rx);
8583 		return rc;
8584 	}
8585 
8586 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8587 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8588 
8589 	hwrm_req_hold(bp, req_rx);
8590 	hwrm_req_hold(bp, req_tx);
8591 	for (i = 0; i < bp->cp_nr_rings; i++) {
8592 		struct bnxt_napi *bnapi = bp->bnapi[i];
8593 		struct bnxt_coal *hw_coal;
8594 
8595 		if (!bnapi->rx_ring)
8596 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8597 		else
8598 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8599 		if (rc)
8600 			break;
8601 
8602 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8603 			continue;
8604 
8605 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8606 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8607 			if (rc)
8608 				break;
8609 		}
8610 		if (bnapi->rx_ring)
8611 			hw_coal = &bp->rx_coal;
8612 		else
8613 			hw_coal = &bp->tx_coal;
8614 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8615 	}
8616 	hwrm_req_drop(bp, req_rx);
8617 	hwrm_req_drop(bp, req_tx);
8618 	return rc;
8619 }
8620 
8621 /* Program Rx coalescing of a single ring, e.g. after its completion ring
8622  * was re-allocated in the FW, which resets the coalescing parameters.
8623  */
8624 static int bnxt_hwrm_set_rx_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8625 {
8626 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8627 	int rc;
8628 
8629 	if (bp->flags & BNXT_FLAG_DIM)
8630 		return bnxt_hwrm_set_ring_coal(bp, bnapi);
8631 
8632 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8633 	if (rc)
8634 		return rc;
8635 
8636 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req);
8637 	return bnxt_hwrm_set_rx_coal(bp, bnapi, req);
8638 }
8639 
8640 /* Program Tx coalescing of the completion ring of a single Tx ring, e.g.
8641  * after it was re-allocated in the FW.
8642  */
8643 static int bnxt_hwrm_set_tx_ring_coal(struct bnxt *bp,
8644 				      struct bnxt_tx_ring_info *txr)
8645 {
8646 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8647 	int rc;
8648 
8649 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8650 	if (rc)
8651 		return rc;
8652 
8653 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req);
8654 	req->ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
8655 	return hwrm_req_send(bp, req);
8656 }
8657 
8658 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8659 {
8660 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8661 	struct hwrm_stat_ctx_free_input *req;
8662 	int i;
8663 
8664 	if (!bp->bnapi)
8665 		return;
8666 
8667 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8668 		return;
8669 
8670 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8671 		return;
8672 	if (BNXT_FW_MAJ(bp) <= 20) {
8673 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8674 			hwrm_req_drop(bp, req);
8675 			return;
8676 		}
8677 		hwrm_req_hold(bp, req0);
8678 	}
8679 	hwrm_req_hold(bp, req);
8680 	for (i = 0; i < bp->cp_nr_rings; i++) {
8681 		struct bnxt_napi *bnapi = bp->bnapi[i];
8682 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8683 
8684 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8685 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8686 			if (req0) {
8687 				req0->stat_ctx_id = req->stat_ctx_id;
8688 				hwrm_req_send(bp, req0);
8689 			}
8690 			hwrm_req_send(bp, req);
8691 
8692 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8693 		}
8694 	}
8695 	hwrm_req_drop(bp, req);
8696 	if (req0)
8697 		hwrm_req_drop(bp, req0);
8698 }
8699 
8700 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8701 {
8702 	struct hwrm_stat_ctx_alloc_output *resp;
8703 	struct hwrm_stat_ctx_alloc_input *req;
8704 	int rc, i;
8705 
8706 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8707 		return 0;
8708 
8709 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8710 	if (rc)
8711 		return rc;
8712 
8713 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8714 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8715 
8716 	resp = hwrm_req_hold(bp, req);
8717 	for (i = 0; i < bp->cp_nr_rings; i++) {
8718 		struct bnxt_napi *bnapi = bp->bnapi[i];
8719 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8720 
8721 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8722 
8723 		rc = hwrm_req_send(bp, req);
8724 		if (rc)
8725 			break;
8726 
8727 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8728 
8729 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8730 	}
8731 	hwrm_req_drop(bp, req);
8732 	return rc;
8733 }
8734 
8735 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8736 {
8737 	struct hwrm_func_qcfg_output *resp;
8738 	struct hwrm_func_qcfg_input *req;
8739 	u16 flags;
8740 	int rc;
8741 
8742 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8743 	if (rc)
8744 		return rc;
8745 
8746 	req->fid = cpu_to_le16(0xffff);
8747 	resp = hwrm_req_hold(bp, req);
8748 	rc = hwrm_req_send(bp, req);
8749 	if (rc)
8750 		goto func_qcfg_exit;
8751 
8752 	flags = le16_to_cpu(resp->flags);
8753 #ifdef CONFIG_BNXT_SRIOV
8754 	if (BNXT_VF(bp)) {
8755 		struct bnxt_vf_info *vf = &bp->vf;
8756 
8757 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8758 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8759 			vf->flags |= BNXT_VF_TRUST;
8760 		else
8761 			vf->flags &= ~BNXT_VF_TRUST;
8762 	} else {
8763 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8764 	}
8765 #endif
8766 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8767 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8768 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8769 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8770 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8771 	}
8772 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8773 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8774 
8775 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8776 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8777 
8778 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8779 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8780 	if (resp->roce_bidi_opt_mode &
8781 	    FUNC_QCFG_RESP_ROCE_BIDI_OPT_MODE_DEDICATED)
8782 		bp->cos0_cos1_shared = 1;
8783 	else
8784 		bp->cos0_cos1_shared = 0;
8785 
8786 	switch (resp->port_partition_type) {
8787 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8788 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2:
8789 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8790 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8791 		bp->port_partition_type = resp->port_partition_type;
8792 		break;
8793 	}
8794 	if (bp->hwrm_spec_code < 0x10707 ||
8795 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8796 		bp->br_mode = BRIDGE_MODE_VEB;
8797 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8798 		bp->br_mode = BRIDGE_MODE_VEPA;
8799 	else
8800 		bp->br_mode = BRIDGE_MODE_UNDEF;
8801 
8802 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8803 	if (!bp->max_mtu)
8804 		bp->max_mtu = BNXT_MAX_MTU;
8805 
8806 	if (bp->db_size)
8807 		goto func_qcfg_exit;
8808 
8809 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8810 	if (BNXT_CHIP_P5(bp)) {
8811 		if (BNXT_PF(bp))
8812 			bp->db_offset = DB_PF_OFFSET_P5;
8813 		else
8814 			bp->db_offset = DB_VF_OFFSET_P5;
8815 	}
8816 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8817 				 1024);
8818 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8819 	    bp->db_size <= bp->db_offset)
8820 		bp->db_size = pci_resource_len(bp->pdev, 2);
8821 
8822 func_qcfg_exit:
8823 	hwrm_req_drop(bp, req);
8824 	return rc;
8825 }
8826 
8827 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8828 				      u8 init_val, u8 init_offset,
8829 				      bool init_mask_set)
8830 {
8831 	ctxm->init_value = init_val;
8832 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8833 	if (init_mask_set)
8834 		ctxm->init_offset = init_offset * 4;
8835 	else
8836 		ctxm->init_value = 0;
8837 }
8838 
8839 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8840 {
8841 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8842 	u16 type;
8843 
8844 	for (type = 0; type < ctx_max; type++) {
8845 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8846 		int n = 1;
8847 
8848 		if (!ctxm->max_entries || ctxm->pg_info)
8849 			continue;
8850 
8851 		if (ctxm->instance_bmap)
8852 			n = hweight32(ctxm->instance_bmap);
8853 		ctxm->pg_info = kzalloc_objs(*ctxm->pg_info, n);
8854 		if (!ctxm->pg_info)
8855 			return -ENOMEM;
8856 	}
8857 	return 0;
8858 }
8859 
8860 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8861 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8862 
8863 #define BNXT_CTX_INIT_VALID(flags)	\
8864 	(!!((flags) &			\
8865 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8866 
8867 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8868 {
8869 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8870 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8871 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8872 	u16 type, next_type = 0;
8873 	int rc;
8874 
8875 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8876 	if (rc)
8877 		return rc;
8878 
8879 	if (!ctx) {
8880 		ctx = kzalloc_obj(*ctx);
8881 		if (!ctx)
8882 			return -ENOMEM;
8883 		bp->ctx = ctx;
8884 	}
8885 
8886 	resp = hwrm_req_hold(bp, req);
8887 
8888 	for (type = 0; type < BNXT_CTX_V2_MAX; type = next_type) {
8889 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8890 		u8 init_val, init_off, i;
8891 		u32 max_entries;
8892 		u16 entry_size;
8893 		__le32 *p;
8894 		u32 flags;
8895 
8896 		req->type = cpu_to_le16(type);
8897 		rc = hwrm_req_send(bp, req);
8898 		if (rc)
8899 			goto ctx_done;
8900 		flags = le32_to_cpu(resp->flags);
8901 		next_type = le16_to_cpu(resp->next_valid_type);
8902 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8903 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8904 			continue;
8905 		}
8906 		entry_size = le16_to_cpu(resp->entry_size);
8907 		max_entries = le32_to_cpu(resp->max_num_entries);
8908 		if (ctxm->mem_valid) {
8909 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8910 			    ctxm->entry_size != entry_size ||
8911 			    ctxm->max_entries != max_entries)
8912 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8913 			else
8914 				continue;
8915 		}
8916 		ctxm->type = type;
8917 		ctxm->entry_size = entry_size;
8918 		ctxm->flags = flags;
8919 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8920 		ctxm->entry_multiple = resp->entry_multiple;
8921 		ctxm->max_entries = max_entries;
8922 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8923 		init_val = resp->ctx_init_value;
8924 		init_off = resp->ctx_init_offset;
8925 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8926 					  BNXT_CTX_INIT_VALID(flags));
8927 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8928 					      BNXT_MAX_SPLIT_ENTRY);
8929 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8930 		     i++, p++)
8931 			ctxm->split[i] = le32_to_cpu(*p);
8932 	}
8933 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8934 
8935 ctx_done:
8936 	hwrm_req_drop(bp, req);
8937 	return rc;
8938 }
8939 
8940 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8941 {
8942 	struct hwrm_func_backing_store_qcaps_output *resp;
8943 	struct hwrm_func_backing_store_qcaps_input *req;
8944 	int rc;
8945 
8946 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8947 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8948 		return 0;
8949 
8950 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8951 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8952 
8953 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8954 	if (rc)
8955 		return rc;
8956 
8957 	resp = hwrm_req_hold(bp, req);
8958 	rc = hwrm_req_send_silent(bp, req);
8959 	if (!rc) {
8960 		struct bnxt_ctx_mem_type *ctxm;
8961 		struct bnxt_ctx_mem_info *ctx;
8962 		u8 init_val, init_idx = 0;
8963 		u16 init_mask;
8964 
8965 		ctx = bp->ctx;
8966 		if (!ctx) {
8967 			ctx = kzalloc_obj(*ctx);
8968 			if (!ctx) {
8969 				rc = -ENOMEM;
8970 				goto ctx_err;
8971 			}
8972 			bp->ctx = ctx;
8973 		}
8974 		init_val = resp->ctx_kind_initializer;
8975 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8976 
8977 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8978 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8979 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8980 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8981 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8982 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8983 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8984 					  (init_mask & (1 << init_idx++)) != 0);
8985 
8986 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8987 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8988 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8989 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8990 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8991 					  (init_mask & (1 << init_idx++)) != 0);
8992 
8993 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8994 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8995 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8996 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8997 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8998 					  (init_mask & (1 << init_idx++)) != 0);
8999 
9000 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9001 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
9002 		ctxm->max_entries = ctxm->vnic_entries +
9003 			le16_to_cpu(resp->vnic_max_ring_table_entries);
9004 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
9005 		bnxt_init_ctx_initializer(ctxm, init_val,
9006 					  resp->vnic_init_offset,
9007 					  (init_mask & (1 << init_idx++)) != 0);
9008 
9009 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9010 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
9011 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
9012 		bnxt_init_ctx_initializer(ctxm, init_val,
9013 					  resp->stat_init_offset,
9014 					  (init_mask & (1 << init_idx++)) != 0);
9015 
9016 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9017 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
9018 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
9019 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
9020 		ctxm->entry_multiple = resp->tqm_entries_multiple;
9021 		if (!ctxm->entry_multiple)
9022 			ctxm->entry_multiple = 1;
9023 
9024 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
9025 
9026 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9027 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
9028 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
9029 		ctxm->mrav_num_entries_units =
9030 			le16_to_cpu(resp->mrav_num_entries_units);
9031 		bnxt_init_ctx_initializer(ctxm, init_val,
9032 					  resp->mrav_init_offset,
9033 					  (init_mask & (1 << init_idx++)) != 0);
9034 
9035 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9036 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
9037 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
9038 
9039 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
9040 		if (!ctx->tqm_fp_rings_count)
9041 			ctx->tqm_fp_rings_count = bp->max_q;
9042 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
9043 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
9044 
9045 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9046 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
9047 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
9048 
9049 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
9050 	} else {
9051 		rc = 0;
9052 	}
9053 ctx_err:
9054 	hwrm_req_drop(bp, req);
9055 	return rc;
9056 }
9057 
9058 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
9059 				  __le64 *pg_dir)
9060 {
9061 	if (!rmem->nr_pages)
9062 		return;
9063 
9064 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
9065 	if (rmem->depth >= 1) {
9066 		if (rmem->depth == 2)
9067 			*pg_attr |= 2;
9068 		else
9069 			*pg_attr |= 1;
9070 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
9071 	} else {
9072 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
9073 	}
9074 }
9075 
9076 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
9077 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
9078 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
9079 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
9080 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
9081 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
9082 
9083 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
9084 {
9085 	struct hwrm_func_backing_store_cfg_input *req;
9086 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9087 	struct bnxt_ctx_pg_info *ctx_pg;
9088 	struct bnxt_ctx_mem_type *ctxm;
9089 	void **__req = (void **)&req;
9090 	u32 req_len = sizeof(*req);
9091 	__le32 *num_entries;
9092 	__le64 *pg_dir;
9093 	u32 flags = 0;
9094 	u8 *pg_attr;
9095 	u32 ena;
9096 	int rc;
9097 	int i;
9098 
9099 	if (!ctx)
9100 		return 0;
9101 
9102 	if (req_len > bp->hwrm_max_ext_req_len)
9103 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
9104 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
9105 	if (rc)
9106 		return rc;
9107 
9108 	req->enables = cpu_to_le32(enables);
9109 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
9110 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9111 		ctx_pg = ctxm->pg_info;
9112 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
9113 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
9114 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
9115 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
9116 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9117 				      &req->qpc_pg_size_qpc_lvl,
9118 				      &req->qpc_page_dir);
9119 
9120 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
9121 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
9122 	}
9123 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
9124 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9125 		ctx_pg = ctxm->pg_info;
9126 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
9127 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
9128 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
9129 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9130 				      &req->srq_pg_size_srq_lvl,
9131 				      &req->srq_page_dir);
9132 	}
9133 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
9134 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9135 		ctx_pg = ctxm->pg_info;
9136 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
9137 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
9138 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
9139 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9140 				      &req->cq_pg_size_cq_lvl,
9141 				      &req->cq_page_dir);
9142 	}
9143 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
9144 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9145 		ctx_pg = ctxm->pg_info;
9146 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
9147 		req->vnic_num_ring_table_entries =
9148 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
9149 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
9150 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9151 				      &req->vnic_pg_size_vnic_lvl,
9152 				      &req->vnic_page_dir);
9153 	}
9154 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
9155 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9156 		ctx_pg = ctxm->pg_info;
9157 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
9158 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
9159 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9160 				      &req->stat_pg_size_stat_lvl,
9161 				      &req->stat_page_dir);
9162 	}
9163 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
9164 		u32 units;
9165 
9166 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9167 		ctx_pg = ctxm->pg_info;
9168 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
9169 		units = ctxm->mrav_num_entries_units;
9170 		if (units) {
9171 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
9172 			u32 entries;
9173 
9174 			num_mr = ctx_pg->entries - num_ah;
9175 			entries = ((num_mr / units) << 16) | (num_ah / units);
9176 			req->mrav_num_entries = cpu_to_le32(entries);
9177 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
9178 		}
9179 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
9180 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9181 				      &req->mrav_pg_size_mrav_lvl,
9182 				      &req->mrav_page_dir);
9183 	}
9184 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
9185 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9186 		ctx_pg = ctxm->pg_info;
9187 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
9188 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
9189 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9190 				      &req->tim_pg_size_tim_lvl,
9191 				      &req->tim_page_dir);
9192 	}
9193 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9194 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
9195 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
9196 	     pg_dir = &req->tqm_sp_page_dir,
9197 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
9198 	     ctx_pg = ctxm->pg_info;
9199 	     i < BNXT_MAX_TQM_RINGS;
9200 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
9201 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
9202 		if (!(enables & ena))
9203 			continue;
9204 
9205 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
9206 		*num_entries = cpu_to_le32(ctx_pg->entries);
9207 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
9208 	}
9209 	req->flags = cpu_to_le32(flags);
9210 	return hwrm_req_send(bp, req);
9211 }
9212 
9213 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
9214 				  struct bnxt_ctx_pg_info *ctx_pg)
9215 {
9216 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9217 
9218 	rmem->page_size = BNXT_PAGE_SIZE;
9219 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
9220 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
9221 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
9222 	if (rmem->depth >= 1)
9223 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
9224 	return bnxt_alloc_ring(bp, rmem);
9225 }
9226 
9227 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
9228 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
9229 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
9230 {
9231 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9232 	int rc;
9233 
9234 	if (!mem_size)
9235 		return -EINVAL;
9236 
9237 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
9238 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
9239 		ctx_pg->nr_pages = 0;
9240 		return -EINVAL;
9241 	}
9242 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
9243 		int nr_tbls, i;
9244 
9245 		rmem->depth = 2;
9246 		ctx_pg->ctx_pg_tbl = kzalloc_objs(ctx_pg, MAX_CTX_PAGES);
9247 		if (!ctx_pg->ctx_pg_tbl)
9248 			return -ENOMEM;
9249 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
9250 		rmem->nr_pages = nr_tbls;
9251 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
9252 		if (rc)
9253 			return rc;
9254 		for (i = 0; i < nr_tbls; i++) {
9255 			struct bnxt_ctx_pg_info *pg_tbl;
9256 
9257 			pg_tbl = kzalloc_obj(*pg_tbl);
9258 			if (!pg_tbl)
9259 				return -ENOMEM;
9260 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
9261 			rmem = &pg_tbl->ring_mem;
9262 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
9263 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
9264 			rmem->depth = 1;
9265 			rmem->nr_pages = MAX_CTX_PAGES;
9266 			rmem->ctx_mem = ctxm;
9267 			if (i == (nr_tbls - 1)) {
9268 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
9269 
9270 				if (rem)
9271 					rmem->nr_pages = rem;
9272 			}
9273 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
9274 			if (rc)
9275 				break;
9276 		}
9277 	} else {
9278 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
9279 		if (rmem->nr_pages > 1 || depth)
9280 			rmem->depth = 1;
9281 		rmem->ctx_mem = ctxm;
9282 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
9283 	}
9284 	return rc;
9285 }
9286 
9287 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
9288 				    struct bnxt_ctx_pg_info *ctx_pg,
9289 				    void *buf, size_t offset, size_t head,
9290 				    size_t tail)
9291 {
9292 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9293 	size_t nr_pages = ctx_pg->nr_pages;
9294 	int page_size = rmem->page_size;
9295 	size_t len = 0, total_len = 0;
9296 	u16 depth = rmem->depth;
9297 
9298 	tail %= nr_pages * page_size;
9299 	do {
9300 		if (depth > 1) {
9301 			int i = head / (page_size * MAX_CTX_PAGES);
9302 			struct bnxt_ctx_pg_info *pg_tbl;
9303 
9304 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9305 			rmem = &pg_tbl->ring_mem;
9306 		}
9307 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
9308 		head += len;
9309 		offset += len;
9310 		total_len += len;
9311 		if (head >= nr_pages * page_size)
9312 			head = 0;
9313 	} while (head != tail);
9314 	return total_len;
9315 }
9316 
9317 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
9318 				  struct bnxt_ctx_pg_info *ctx_pg)
9319 {
9320 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9321 
9322 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
9323 	    ctx_pg->ctx_pg_tbl) {
9324 		int i, nr_tbls = rmem->nr_pages;
9325 
9326 		for (i = 0; i < nr_tbls; i++) {
9327 			struct bnxt_ctx_pg_info *pg_tbl;
9328 			struct bnxt_ring_mem_info *rmem2;
9329 
9330 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9331 			if (!pg_tbl)
9332 				continue;
9333 			rmem2 = &pg_tbl->ring_mem;
9334 			bnxt_free_ring(bp, rmem2);
9335 			ctx_pg->ctx_pg_arr[i] = NULL;
9336 			kfree(pg_tbl);
9337 			ctx_pg->ctx_pg_tbl[i] = NULL;
9338 		}
9339 		kfree(ctx_pg->ctx_pg_tbl);
9340 		ctx_pg->ctx_pg_tbl = NULL;
9341 	}
9342 	bnxt_free_ring(bp, rmem);
9343 	ctx_pg->nr_pages = 0;
9344 }
9345 
9346 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
9347 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
9348 				   u8 pg_lvl)
9349 {
9350 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9351 	int i, rc = 0, n = 1;
9352 	u32 mem_size;
9353 
9354 	if (!ctxm->entry_size || !ctx_pg)
9355 		return -EINVAL;
9356 	if (ctxm->instance_bmap)
9357 		n = hweight32(ctxm->instance_bmap);
9358 	if (ctxm->entry_multiple)
9359 		entries = roundup(entries, ctxm->entry_multiple);
9360 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
9361 	mem_size = entries * ctxm->entry_size;
9362 	for (i = 0; i < n && !rc; i++) {
9363 		ctx_pg[i].entries = entries;
9364 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
9365 					    ctxm->init_value ? ctxm : NULL);
9366 	}
9367 	if (!rc)
9368 		ctxm->mem_valid = 1;
9369 	return rc;
9370 }
9371 
9372 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
9373 					       struct bnxt_ctx_mem_type *ctxm,
9374 					       bool last)
9375 {
9376 	struct hwrm_func_backing_store_cfg_v2_input *req;
9377 	u32 instance_bmap = ctxm->instance_bmap;
9378 	int i, j, rc = 0, n = 1;
9379 	__le32 *p;
9380 
9381 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
9382 		return 0;
9383 
9384 	if (instance_bmap)
9385 		n = hweight32(ctxm->instance_bmap);
9386 	else
9387 		instance_bmap = 1;
9388 
9389 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
9390 	if (rc)
9391 		return rc;
9392 	hwrm_req_hold(bp, req);
9393 	req->type = cpu_to_le16(ctxm->type);
9394 	req->entry_size = cpu_to_le16(ctxm->entry_size);
9395 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
9396 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
9397 		struct bnxt_bs_trace_info *bs_trace;
9398 		u32 enables;
9399 
9400 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
9401 		req->enables = cpu_to_le32(enables);
9402 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
9403 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
9404 	}
9405 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
9406 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
9407 		p[i] = cpu_to_le32(ctxm->split[i]);
9408 	for (i = 0, j = 0; j < n && !rc; i++) {
9409 		struct bnxt_ctx_pg_info *ctx_pg;
9410 
9411 		if (!(instance_bmap & (1 << i)))
9412 			continue;
9413 		req->instance = cpu_to_le16(i);
9414 		ctx_pg = &ctxm->pg_info[j++];
9415 		if (!ctx_pg->entries)
9416 			continue;
9417 		req->num_entries = cpu_to_le32(ctx_pg->entries);
9418 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9419 				      &req->page_size_pbl_level,
9420 				      &req->page_dir);
9421 		if (last && j == n)
9422 			req->flags =
9423 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
9424 		rc = hwrm_req_send(bp, req);
9425 	}
9426 	hwrm_req_drop(bp, req);
9427 	return rc;
9428 }
9429 
9430 static int bnxt_backing_store_cfg_v2(struct bnxt *bp)
9431 {
9432 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9433 	struct bnxt_ctx_mem_type *ctxm;
9434 	u16 last_type = BNXT_CTX_INV;
9435 	int rc = 0;
9436 	u16 type;
9437 
9438 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_QPC; type++) {
9439 		ctxm = &ctx->ctx_arr[type];
9440 		if (!bnxt_bs_trace_avail(bp, type))
9441 			continue;
9442 		if (!ctxm->mem_valid) {
9443 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9444 						     ctxm->max_entries, 1);
9445 			if (rc) {
9446 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9447 					    type);
9448 				continue;
9449 			}
9450 			bnxt_bs_trace_init(bp, ctxm);
9451 		}
9452 		last_type = type;
9453 	}
9454 
9455 	if (last_type == BNXT_CTX_INV) {
9456 		for (type = 0; type < BNXT_CTX_MAX; type++) {
9457 			ctxm = &ctx->ctx_arr[type];
9458 			if (ctxm->mem_valid)
9459 				last_type = type;
9460 		}
9461 		if (last_type == BNXT_CTX_INV)
9462 			return 0;
9463 	}
9464 	ctx->ctx_arr[last_type].last = 1;
9465 
9466 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9467 		ctxm = &ctx->ctx_arr[type];
9468 
9469 		if (!ctxm->mem_valid)
9470 			continue;
9471 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9472 		if (rc)
9473 			return rc;
9474 	}
9475 	return 0;
9476 }
9477 
9478 /**
9479  * __bnxt_copy_ctx_mem - copy host context memory
9480  * @bp: The driver context
9481  * @ctxm: The pointer to the context memory type
9482  * @buf: The destination buffer or NULL to just obtain the length
9483  * @offset: The buffer offset to copy the data to
9484  * @head: The head offset of context memory to copy from
9485  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9486  *
9487  * This function is called for debugging purposes to dump the host context
9488  * used by the chip.
9489  *
9490  * Return: Length of memory copied
9491  */
9492 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9493 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9494 				  size_t offset, size_t head, size_t tail)
9495 {
9496 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9497 	size_t len = 0, total_len = 0;
9498 	int i, n = 1;
9499 
9500 	if (!ctx_pg)
9501 		return 0;
9502 
9503 	if (ctxm->instance_bmap)
9504 		n = hweight32(ctxm->instance_bmap);
9505 	for (i = 0; i < n; i++) {
9506 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9507 					    tail);
9508 		offset += len;
9509 		total_len += len;
9510 	}
9511 	return total_len;
9512 }
9513 
9514 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9515 			 void *buf, size_t offset)
9516 {
9517 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9518 
9519 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9520 }
9521 
9522 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9523 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9524 {
9525 	struct bnxt_ctx_pg_info *ctx_pg;
9526 	int i, n = 1;
9527 
9528 	ctxm->last = 0;
9529 
9530 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9531 		return;
9532 
9533 	ctx_pg = ctxm->pg_info;
9534 	if (ctx_pg) {
9535 		if (ctxm->instance_bmap)
9536 			n = hweight32(ctxm->instance_bmap);
9537 		for (i = 0; i < n; i++)
9538 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9539 
9540 		kfree(ctx_pg);
9541 		ctxm->pg_info = NULL;
9542 		ctxm->mem_valid = 0;
9543 	}
9544 	memset(ctxm, 0, sizeof(*ctxm));
9545 }
9546 
9547 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9548 {
9549 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9550 	u16 type;
9551 
9552 	if (!ctx)
9553 		return;
9554 
9555 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9556 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9557 
9558 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9559 	if (force) {
9560 		kfree(ctx);
9561 		bp->ctx = NULL;
9562 	}
9563 }
9564 
9565 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9566 {
9567 	struct bnxt_ctx_mem_type *ctxm;
9568 	struct bnxt_ctx_mem_info *ctx;
9569 	u32 l2_qps, qp1_qps, max_qps;
9570 	u32 ena, entries_sp, entries;
9571 	u32 srqs, max_srqs, min;
9572 	u32 num_mr, num_ah;
9573 	u32 extra_srqs = 0;
9574 	u32 extra_qps = 0;
9575 	u32 fast_qpmd_qps;
9576 	u8 pg_lvl = 1;
9577 	int i, rc;
9578 
9579 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9580 	if (rc) {
9581 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9582 			   rc);
9583 		return rc;
9584 	}
9585 	ctx = bp->ctx;
9586 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9587 		return 0;
9588 
9589 	ena = 0;
9590 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
9591 		goto skip_legacy;
9592 
9593 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9594 	l2_qps = ctxm->qp_l2_entries;
9595 	qp1_qps = ctxm->qp_qp1_entries;
9596 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9597 	max_qps = ctxm->max_entries;
9598 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9599 	srqs = ctxm->srq_l2_entries;
9600 	max_srqs = ctxm->max_entries;
9601 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9602 		pg_lvl = 2;
9603 		if (BNXT_SW_RES_LMT(bp)) {
9604 			extra_qps = max_qps - l2_qps - qp1_qps;
9605 			extra_srqs = max_srqs - srqs;
9606 		} else {
9607 			extra_qps = min_t(u32, 65536,
9608 					  max_qps - l2_qps - qp1_qps);
9609 			/* allocate extra qps if fw supports RoCE fast qp
9610 			 * destroy feature
9611 			 */
9612 			extra_qps += fast_qpmd_qps;
9613 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9614 		}
9615 		if (fast_qpmd_qps)
9616 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9617 	}
9618 
9619 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9620 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9621 				     pg_lvl);
9622 	if (rc)
9623 		return rc;
9624 
9625 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9626 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9627 	if (rc)
9628 		return rc;
9629 
9630 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9631 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9632 				     extra_qps * 2, pg_lvl);
9633 	if (rc)
9634 		return rc;
9635 
9636 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9637 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9638 	if (rc)
9639 		return rc;
9640 
9641 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9642 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9643 	if (rc)
9644 		return rc;
9645 
9646 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9647 		goto skip_rdma;
9648 
9649 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9650 	if (BNXT_SW_RES_LMT(bp) &&
9651 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9652 		num_ah = ctxm->mrav_av_entries;
9653 		num_mr = ctxm->max_entries - num_ah;
9654 	} else {
9655 		/* 128K extra is needed to accommodate static AH context
9656 		 * allocation by f/w.
9657 		 */
9658 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9659 		num_ah = min_t(u32, num_mr, 1024 * 128);
9660 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9661 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9662 			ctxm->mrav_av_entries = num_ah;
9663 	}
9664 
9665 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9666 	if (rc)
9667 		return rc;
9668 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9669 
9670 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9671 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9672 	if (rc)
9673 		return rc;
9674 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9675 
9676 skip_rdma:
9677 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9678 	min = ctxm->min_entries;
9679 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9680 		     2 * (extra_qps + qp1_qps) + min;
9681 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9682 	if (rc)
9683 		return rc;
9684 
9685 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9686 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9687 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9688 	if (rc)
9689 		return rc;
9690 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9691 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9692 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9693 
9694 skip_legacy:
9695 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9696 		rc = bnxt_backing_store_cfg_v2(bp);
9697 	else
9698 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9699 	if (rc) {
9700 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9701 			   rc);
9702 		return rc;
9703 	}
9704 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9705 	return 0;
9706 }
9707 
9708 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9709 {
9710 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9711 	u16 page_attr;
9712 	int rc;
9713 
9714 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9715 		return 0;
9716 
9717 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9718 	if (rc)
9719 		return rc;
9720 
9721 	if (BNXT_PAGE_SIZE == 0x2000)
9722 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9723 	else if (BNXT_PAGE_SIZE == 0x10000)
9724 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9725 	else
9726 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9727 	req->pg_size_lvl = cpu_to_le16(page_attr |
9728 				       bp->fw_crash_mem->ring_mem.depth);
9729 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9730 	req->size = cpu_to_le32(bp->fw_crash_len);
9731 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9732 	return hwrm_req_send(bp, req);
9733 }
9734 
9735 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9736 {
9737 	if (bp->fw_crash_mem) {
9738 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9739 		kfree(bp->fw_crash_mem);
9740 		bp->fw_crash_mem = NULL;
9741 	}
9742 }
9743 
9744 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9745 {
9746 	u32 mem_size = 0;
9747 	int rc;
9748 
9749 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9750 		return 0;
9751 
9752 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9753 	if (rc)
9754 		return rc;
9755 
9756 	mem_size = round_up(mem_size, 4);
9757 
9758 	/* keep and use the existing pages */
9759 	if (bp->fw_crash_mem &&
9760 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9761 		goto alloc_done;
9762 
9763 	if (bp->fw_crash_mem)
9764 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9765 	else
9766 		bp->fw_crash_mem = kzalloc_obj(*bp->fw_crash_mem);
9767 	if (!bp->fw_crash_mem)
9768 		return -ENOMEM;
9769 
9770 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9771 	if (rc) {
9772 		bnxt_free_crash_dump_mem(bp);
9773 		return rc;
9774 	}
9775 
9776 alloc_done:
9777 	bp->fw_crash_len = mem_size;
9778 	return 0;
9779 }
9780 
9781 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9782 {
9783 	struct hwrm_func_resource_qcaps_output *resp;
9784 	struct hwrm_func_resource_qcaps_input *req;
9785 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9786 	int rc;
9787 
9788 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9789 	if (rc)
9790 		return rc;
9791 
9792 	req->fid = cpu_to_le16(0xffff);
9793 	resp = hwrm_req_hold(bp, req);
9794 	rc = hwrm_req_send_silent(bp, req);
9795 	if (rc)
9796 		goto hwrm_func_resc_qcaps_exit;
9797 
9798 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9799 	if (!all)
9800 		goto hwrm_func_resc_qcaps_exit;
9801 
9802 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9803 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9804 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9805 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9806 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9807 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9808 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9809 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9810 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9811 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9812 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9813 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9814 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9815 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9816 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9817 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9818 
9819 	if (hw_resc->max_rsscos_ctxs >=
9820 	    hw_resc->max_vnics * BNXT_LARGE_RSS_TO_VNIC_RATIO)
9821 		bp->rss_cap |= BNXT_RSS_CAP_LARGE_RSS_CTX;
9822 
9823 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9824 		u16 max_msix = le16_to_cpu(resp->max_msix);
9825 
9826 		hw_resc->max_nqs = max_msix;
9827 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9828 	}
9829 
9830 	if (BNXT_PF(bp)) {
9831 		struct bnxt_pf_info *pf = &bp->pf;
9832 
9833 		pf->vf_resv_strategy =
9834 			le16_to_cpu(resp->vf_reservation_strategy);
9835 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9836 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9837 	}
9838 hwrm_func_resc_qcaps_exit:
9839 	hwrm_req_drop(bp, req);
9840 	return rc;
9841 }
9842 
9843 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9844 {
9845 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9846 	struct hwrm_port_mac_ptp_qcfg_input *req;
9847 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9848 	u8 flags;
9849 	int rc;
9850 
9851 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9852 		rc = -ENODEV;
9853 		goto no_ptp;
9854 	}
9855 
9856 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9857 	if (rc)
9858 		goto no_ptp;
9859 
9860 	req->port_id = cpu_to_le16(bp->pf.port_id);
9861 	resp = hwrm_req_hold(bp, req);
9862 	rc = hwrm_req_send(bp, req);
9863 	if (rc)
9864 		goto exit;
9865 
9866 	flags = resp->flags;
9867 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9868 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9869 		rc = -ENODEV;
9870 		goto exit;
9871 	}
9872 	if (!ptp) {
9873 		ptp = kzalloc_obj(*ptp);
9874 		if (!ptp) {
9875 			rc = -ENOMEM;
9876 			goto exit;
9877 		}
9878 		ptp->bp = bp;
9879 		bp->ptp_cfg = ptp;
9880 	}
9881 
9882 	if (flags &
9883 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9884 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9885 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9886 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9887 	} else if (BNXT_CHIP_P5(bp)) {
9888 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9889 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9890 	} else {
9891 		rc = -ENODEV;
9892 		goto exit;
9893 	}
9894 	ptp->rtc_configured =
9895 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9896 	rc = bnxt_ptp_init(bp);
9897 	if (rc)
9898 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9899 exit:
9900 	hwrm_req_drop(bp, req);
9901 	if (!rc)
9902 		return 0;
9903 
9904 no_ptp:
9905 	bnxt_ptp_clear(bp);
9906 	kfree(ptp);
9907 	bp->ptp_cfg = NULL;
9908 	return rc;
9909 }
9910 
9911 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9912 {
9913 	u32 flags, flags_ext, flags_ext2, flags_ext3;
9914 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9915 	struct hwrm_func_qcaps_output *resp;
9916 	struct hwrm_func_qcaps_input *req;
9917 	int rc;
9918 
9919 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9920 	if (rc)
9921 		return rc;
9922 
9923 	req->fid = cpu_to_le16(0xffff);
9924 	resp = hwrm_req_hold(bp, req);
9925 	rc = hwrm_req_send(bp, req);
9926 	if (rc)
9927 		goto hwrm_func_qcaps_exit;
9928 
9929 	flags = le32_to_cpu(resp->flags);
9930 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9931 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9932 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9933 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9934 	if (flags & FUNC_QCAPS_RESP_FLAGS_LINK_ADMIN_STATUS_SUPPORTED)
9935 		bp->fw_cap |= BNXT_FW_CAP_LINK_ADMIN;
9936 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9937 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9938 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9939 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9940 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9941 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9942 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9943 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9944 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9945 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9946 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9947 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9948 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9949 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9950 
9951 	flags_ext = le32_to_cpu(resp->flags_ext);
9952 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9953 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9954 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9955 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9956 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PTM_SUPPORTED)
9957 		bp->fw_cap |= BNXT_FW_CAP_PTP_PTM;
9958 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9959 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9960 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9961 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9962 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9963 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9964 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED)
9965 		bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2;
9966 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9967 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9968 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9969 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9970 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9971 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9972 
9973 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9974 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9975 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9976 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9977 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9978 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9979 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9980 	if (flags_ext2 &
9981 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9982 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9983 	if (BNXT_PF(bp) &&
9984 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9985 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9986 
9987 	flags_ext3 = le32_to_cpu(resp->flags_ext3);
9988 	if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_ROCE_VF_DYN_ALLOC_SUPPORT)
9989 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_DYN_ALLOC_SUPPORT;
9990 	if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_MIRROR_ON_ROCE_SUPPORTED)
9991 		bp->fw_cap |= BNXT_FW_CAP_MIRROR_ON_ROCE;
9992 
9993 	bp->tx_push_thresh = 0;
9994 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9995 	    BNXT_FW_MAJ(bp) > 217)
9996 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9997 
9998 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9999 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
10000 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
10001 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
10002 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
10003 	if (!hw_resc->max_hw_ring_grps)
10004 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
10005 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
10006 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
10007 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
10008 
10009 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
10010 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
10011 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
10012 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
10013 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
10014 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
10015 
10016 	if (BNXT_PF(bp)) {
10017 		struct bnxt_pf_info *pf = &bp->pf;
10018 
10019 		pf->fw_fid = le16_to_cpu(resp->fid);
10020 		pf->port_id = le16_to_cpu(resp->port_id);
10021 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
10022 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
10023 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
10024 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
10025 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
10026 			bp->flags |= BNXT_FLAG_WOL_CAP;
10027 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
10028 			bp->fw_cap |= BNXT_FW_CAP_PTP;
10029 		} else {
10030 			bnxt_ptp_clear(bp);
10031 			kfree(bp->ptp_cfg);
10032 			bp->ptp_cfg = NULL;
10033 		}
10034 	} else {
10035 #ifdef CONFIG_BNXT_SRIOV
10036 		struct bnxt_vf_info *vf = &bp->vf;
10037 
10038 		vf->fw_fid = le16_to_cpu(resp->fid);
10039 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
10040 #endif
10041 	}
10042 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
10043 
10044 hwrm_func_qcaps_exit:
10045 	hwrm_req_drop(bp, req);
10046 	return rc;
10047 }
10048 
10049 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
10050 {
10051 	struct hwrm_dbg_qcaps_output *resp;
10052 	struct hwrm_dbg_qcaps_input *req;
10053 	int rc;
10054 
10055 	bp->fw_dbg_cap = 0;
10056 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
10057 		return;
10058 
10059 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
10060 	if (rc)
10061 		return;
10062 
10063 	req->fid = cpu_to_le16(0xffff);
10064 	resp = hwrm_req_hold(bp, req);
10065 	rc = hwrm_req_send(bp, req);
10066 	if (rc)
10067 		goto hwrm_dbg_qcaps_exit;
10068 
10069 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
10070 
10071 hwrm_dbg_qcaps_exit:
10072 	hwrm_req_drop(bp, req);
10073 }
10074 
10075 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
10076 
10077 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
10078 {
10079 	int rc;
10080 
10081 	rc = __bnxt_hwrm_func_qcaps(bp);
10082 	if (rc)
10083 		return rc;
10084 
10085 	bnxt_hwrm_dbg_qcaps(bp);
10086 
10087 	rc = bnxt_hwrm_queue_qportcfg(bp);
10088 	if (rc) {
10089 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
10090 		return rc;
10091 	}
10092 	if (bp->hwrm_spec_code >= 0x10803) {
10093 		rc = bnxt_alloc_ctx_mem(bp);
10094 		if (rc)
10095 			return rc;
10096 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
10097 		if (!rc)
10098 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
10099 	}
10100 	return 0;
10101 }
10102 
10103 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
10104 {
10105 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
10106 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
10107 	u32 flags;
10108 	int rc;
10109 
10110 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
10111 		return 0;
10112 
10113 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
10114 	if (rc)
10115 		return rc;
10116 
10117 	resp = hwrm_req_hold(bp, req);
10118 	rc = hwrm_req_send(bp, req);
10119 	if (rc)
10120 		goto hwrm_cfa_adv_qcaps_exit;
10121 
10122 	flags = le32_to_cpu(resp->flags);
10123 	if (flags &
10124 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
10125 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
10126 
10127 	if (flags &
10128 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
10129 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
10130 
10131 	if (flags &
10132 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
10133 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
10134 
10135 hwrm_cfa_adv_qcaps_exit:
10136 	hwrm_req_drop(bp, req);
10137 	return rc;
10138 }
10139 
10140 static int __bnxt_alloc_fw_health(struct bnxt *bp)
10141 {
10142 	if (bp->fw_health)
10143 		return 0;
10144 
10145 	bp->fw_health = kzalloc_obj(*bp->fw_health);
10146 	if (!bp->fw_health)
10147 		return -ENOMEM;
10148 
10149 	mutex_init(&bp->fw_health->lock);
10150 	return 0;
10151 }
10152 
10153 static int bnxt_alloc_fw_health(struct bnxt *bp)
10154 {
10155 	int rc;
10156 
10157 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
10158 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
10159 		return 0;
10160 
10161 	rc = __bnxt_alloc_fw_health(bp);
10162 	if (rc) {
10163 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
10164 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10165 		return rc;
10166 	}
10167 
10168 	return 0;
10169 }
10170 
10171 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
10172 {
10173 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
10174 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
10175 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
10176 }
10177 
10178 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
10179 {
10180 	struct bnxt_fw_health *fw_health = bp->fw_health;
10181 	u32 reg_type;
10182 
10183 	if (!fw_health)
10184 		return;
10185 
10186 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
10187 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
10188 		fw_health->status_reliable = false;
10189 
10190 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
10191 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
10192 		fw_health->resets_reliable = false;
10193 }
10194 
10195 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
10196 {
10197 	void __iomem *hs;
10198 	u32 status_loc;
10199 	u32 reg_type;
10200 	u32 sig;
10201 
10202 	if (bp->fw_health)
10203 		bp->fw_health->status_reliable = false;
10204 
10205 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
10206 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
10207 
10208 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
10209 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
10210 		if (!bp->chip_num) {
10211 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
10212 			bp->chip_num = readl(bp->bar0 +
10213 					     BNXT_FW_HEALTH_WIN_BASE +
10214 					     BNXT_GRC_REG_CHIP_NUM);
10215 		}
10216 		if (!BNXT_CHIP_P5_PLUS(bp))
10217 			return;
10218 
10219 		status_loc = BNXT_GRC_REG_STATUS_P5 |
10220 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
10221 	} else {
10222 		status_loc = readl(hs + offsetof(struct hcomm_status,
10223 						 fw_status_loc));
10224 	}
10225 
10226 	if (__bnxt_alloc_fw_health(bp)) {
10227 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
10228 		return;
10229 	}
10230 
10231 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
10232 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
10233 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
10234 		__bnxt_map_fw_health_reg(bp, status_loc);
10235 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
10236 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
10237 	}
10238 
10239 	bp->fw_health->status_reliable = true;
10240 }
10241 
10242 static int bnxt_map_fw_health_regs(struct bnxt *bp)
10243 {
10244 	struct bnxt_fw_health *fw_health = bp->fw_health;
10245 	u32 reg_base = 0xffffffff;
10246 	int i;
10247 
10248 	bp->fw_health->status_reliable = false;
10249 	bp->fw_health->resets_reliable = false;
10250 	/* Only pre-map the monitoring GRC registers using window 3 */
10251 	for (i = 0; i < 4; i++) {
10252 		u32 reg = fw_health->regs[i];
10253 
10254 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
10255 			continue;
10256 		if (reg_base == 0xffffffff)
10257 			reg_base = reg & BNXT_GRC_BASE_MASK;
10258 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
10259 			return -ERANGE;
10260 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
10261 	}
10262 	bp->fw_health->status_reliable = true;
10263 	bp->fw_health->resets_reliable = true;
10264 	if (reg_base == 0xffffffff)
10265 		return 0;
10266 
10267 	__bnxt_map_fw_health_reg(bp, reg_base);
10268 	return 0;
10269 }
10270 
10271 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
10272 {
10273 	if (!bp->fw_health)
10274 		return;
10275 
10276 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
10277 		bp->fw_health->status_reliable = true;
10278 		bp->fw_health->resets_reliable = true;
10279 	} else {
10280 		bnxt_try_map_fw_health_reg(bp);
10281 	}
10282 }
10283 
10284 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
10285 {
10286 	struct bnxt_fw_health *fw_health = bp->fw_health;
10287 	struct hwrm_error_recovery_qcfg_output *resp;
10288 	struct hwrm_error_recovery_qcfg_input *req;
10289 	int rc, i;
10290 
10291 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
10292 		return 0;
10293 
10294 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
10295 	if (rc)
10296 		return rc;
10297 
10298 	resp = hwrm_req_hold(bp, req);
10299 	rc = hwrm_req_send(bp, req);
10300 	if (rc)
10301 		goto err_recovery_out;
10302 	fw_health->flags = le32_to_cpu(resp->flags);
10303 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
10304 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
10305 		rc = -EINVAL;
10306 		goto err_recovery_out;
10307 	}
10308 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
10309 	fw_health->master_func_wait_dsecs =
10310 		le32_to_cpu(resp->master_func_wait_period);
10311 	fw_health->normal_func_wait_dsecs =
10312 		le32_to_cpu(resp->normal_func_wait_period);
10313 	fw_health->post_reset_wait_dsecs =
10314 		le32_to_cpu(resp->master_func_wait_period_after_reset);
10315 	fw_health->post_reset_max_wait_dsecs =
10316 		le32_to_cpu(resp->max_bailout_time_after_reset);
10317 	fw_health->regs[BNXT_FW_HEALTH_REG] =
10318 		le32_to_cpu(resp->fw_health_status_reg);
10319 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
10320 		le32_to_cpu(resp->fw_heartbeat_reg);
10321 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
10322 		le32_to_cpu(resp->fw_reset_cnt_reg);
10323 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
10324 		le32_to_cpu(resp->reset_inprogress_reg);
10325 	fw_health->fw_reset_inprog_reg_mask =
10326 		le32_to_cpu(resp->reset_inprogress_reg_mask);
10327 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
10328 	if (fw_health->fw_reset_seq_cnt >= 16) {
10329 		rc = -EINVAL;
10330 		goto err_recovery_out;
10331 	}
10332 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
10333 		fw_health->fw_reset_seq_regs[i] =
10334 			le32_to_cpu(resp->reset_reg[i]);
10335 		fw_health->fw_reset_seq_vals[i] =
10336 			le32_to_cpu(resp->reset_reg_val[i]);
10337 		fw_health->fw_reset_seq_delay_msec[i] =
10338 			resp->delay_after_reset[i];
10339 	}
10340 err_recovery_out:
10341 	hwrm_req_drop(bp, req);
10342 	if (!rc)
10343 		rc = bnxt_map_fw_health_regs(bp);
10344 	if (rc)
10345 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10346 	return rc;
10347 }
10348 
10349 static int bnxt_hwrm_func_reset(struct bnxt *bp)
10350 {
10351 	struct hwrm_func_reset_input *req;
10352 	int rc;
10353 
10354 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
10355 	if (rc)
10356 		return rc;
10357 
10358 	req->enables = 0;
10359 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
10360 	return hwrm_req_send(bp, req);
10361 }
10362 
10363 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
10364 {
10365 	struct hwrm_nvm_get_dev_info_output nvm_info;
10366 
10367 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
10368 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
10369 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
10370 			 nvm_info.nvm_cfg_ver_upd);
10371 }
10372 
10373 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
10374 {
10375 	struct hwrm_queue_qportcfg_output *resp;
10376 	struct hwrm_queue_qportcfg_input *req;
10377 	u8 i, j, *qptr;
10378 	bool no_rdma;
10379 	int rc = 0;
10380 
10381 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
10382 	if (rc)
10383 		return rc;
10384 
10385 	resp = hwrm_req_hold(bp, req);
10386 	rc = hwrm_req_send(bp, req);
10387 	if (rc)
10388 		goto qportcfg_exit;
10389 
10390 	if (!resp->max_configurable_queues) {
10391 		rc = -EINVAL;
10392 		goto qportcfg_exit;
10393 	}
10394 	bp->max_tc = resp->max_configurable_queues;
10395 	bp->max_lltc = resp->max_configurable_lossless_queues;
10396 	if (bp->max_tc > BNXT_MAX_QUEUE)
10397 		bp->max_tc = BNXT_MAX_QUEUE;
10398 
10399 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
10400 	qptr = &resp->queue_id0;
10401 	for (i = 0, j = 0; i < bp->max_tc; i++) {
10402 		bp->q_info[j].queue_id = *qptr;
10403 		bp->q_ids[i] = *qptr++;
10404 		bp->q_info[j].queue_profile = *qptr++;
10405 		bp->tc_to_qidx[j] = j;
10406 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
10407 		    (no_rdma && BNXT_PF(bp)))
10408 			j++;
10409 	}
10410 	bp->max_q = bp->max_tc;
10411 	bp->max_tc = max_t(u8, j, 1);
10412 
10413 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
10414 		bp->max_tc = 1;
10415 
10416 	if (bp->max_lltc > bp->max_tc)
10417 		bp->max_lltc = bp->max_tc;
10418 
10419 qportcfg_exit:
10420 	hwrm_req_drop(bp, req);
10421 	return rc;
10422 }
10423 
10424 static int bnxt_hwrm_poll(struct bnxt *bp)
10425 {
10426 	struct hwrm_ver_get_input *req;
10427 	int rc;
10428 
10429 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10430 	if (rc)
10431 		return rc;
10432 
10433 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10434 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10435 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10436 
10437 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
10438 	rc = hwrm_req_send(bp, req);
10439 	return rc;
10440 }
10441 
10442 static int bnxt_hwrm_ver_get(struct bnxt *bp)
10443 {
10444 	struct hwrm_ver_get_output *resp;
10445 	struct hwrm_ver_get_input *req;
10446 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
10447 	u32 dev_caps_cfg, hwrm_ver;
10448 	int rc, len, max_tmo_secs;
10449 
10450 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10451 	if (rc)
10452 		return rc;
10453 
10454 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10455 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
10456 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10457 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10458 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10459 
10460 	resp = hwrm_req_hold(bp, req);
10461 	rc = hwrm_req_send(bp, req);
10462 	if (rc)
10463 		goto hwrm_ver_get_exit;
10464 
10465 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
10466 
10467 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
10468 			     resp->hwrm_intf_min_8b << 8 |
10469 			     resp->hwrm_intf_upd_8b;
10470 	if (resp->hwrm_intf_maj_8b < 1) {
10471 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
10472 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10473 			    resp->hwrm_intf_upd_8b);
10474 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10475 	}
10476 
10477 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10478 			HWRM_VERSION_UPDATE;
10479 
10480 	if (bp->hwrm_spec_code > hwrm_ver)
10481 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10482 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10483 			 HWRM_VERSION_UPDATE);
10484 	else
10485 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10486 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10487 			 resp->hwrm_intf_upd_8b);
10488 
10489 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10490 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10491 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10492 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10493 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10494 		len = FW_VER_STR_LEN;
10495 	} else {
10496 		fw_maj = resp->hwrm_fw_maj_8b;
10497 		fw_min = resp->hwrm_fw_min_8b;
10498 		fw_bld = resp->hwrm_fw_bld_8b;
10499 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10500 		len = BC_HWRM_STR_LEN;
10501 	}
10502 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10503 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10504 		 fw_rsv);
10505 
10506 	if (strlen(resp->active_pkg_name)) {
10507 		int fw_ver_len = strlen(bp->fw_ver_str);
10508 
10509 		snprintf(bp->fw_ver_str + fw_ver_len,
10510 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10511 			 resp->active_pkg_name);
10512 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10513 	}
10514 
10515 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10516 	if (!bp->hwrm_cmd_timeout)
10517 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10518 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10519 	if (!bp->hwrm_cmd_max_timeout)
10520 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10521 	max_tmo_secs = bp->hwrm_cmd_max_timeout / 1000;
10522 #ifdef CONFIG_DETECT_HUNG_TASK
10523 	if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT ||
10524 	    max_tmo_secs > CONFIG_DEFAULT_HUNG_TASK_TIMEOUT) {
10525 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog (kernel default %ds)\n",
10526 			    max_tmo_secs, CONFIG_DEFAULT_HUNG_TASK_TIMEOUT);
10527 	}
10528 #endif
10529 
10530 	if (resp->hwrm_intf_maj_8b >= 1) {
10531 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10532 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10533 	}
10534 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10535 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10536 
10537 	bp->chip_num = le16_to_cpu(resp->chip_num);
10538 	bp->chip_rev = resp->chip_rev;
10539 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10540 	    !resp->chip_metal)
10541 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10542 
10543 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10544 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10545 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10546 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10547 
10548 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10549 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10550 
10551 	if (dev_caps_cfg &
10552 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10553 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10554 
10555 	if (dev_caps_cfg &
10556 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10557 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10558 
10559 	if (dev_caps_cfg &
10560 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10561 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10562 
10563 hwrm_ver_get_exit:
10564 	hwrm_req_drop(bp, req);
10565 	return rc;
10566 }
10567 
10568 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10569 {
10570 	struct hwrm_fw_set_time_input *req;
10571 	struct tm tm;
10572 	time64_t now = ktime_get_real_seconds();
10573 	int rc;
10574 
10575 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10576 	    bp->hwrm_spec_code < 0x10400)
10577 		return -EOPNOTSUPP;
10578 
10579 	time64_to_tm(now, 0, &tm);
10580 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10581 	if (rc)
10582 		return rc;
10583 
10584 	req->year = cpu_to_le16(1900 + tm.tm_year);
10585 	req->month = 1 + tm.tm_mon;
10586 	req->day = tm.tm_mday;
10587 	req->hour = tm.tm_hour;
10588 	req->minute = tm.tm_min;
10589 	req->second = tm.tm_sec;
10590 	return hwrm_req_send(bp, req);
10591 }
10592 
10593 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10594 {
10595 	u64 sw_tmp;
10596 
10597 	hw &= mask;
10598 	sw_tmp = (*sw & ~mask) | hw;
10599 	if (hw < (*sw & mask))
10600 		sw_tmp += mask + 1;
10601 	WRITE_ONCE(*sw, sw_tmp);
10602 }
10603 
10604 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10605 				    int count, bool ignore_zero)
10606 {
10607 	int i;
10608 
10609 	for (i = 0; i < count; i++) {
10610 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10611 
10612 		if (ignore_zero && !hw)
10613 			continue;
10614 
10615 		if (masks[i] == -1ULL)
10616 			sw_stats[i] = hw;
10617 		else
10618 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10619 	}
10620 }
10621 
10622 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10623 {
10624 	if (!stats->hw_stats)
10625 		return;
10626 
10627 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10628 				stats->hw_masks, stats->len / 8, false);
10629 }
10630 
10631 static void bnxt_accumulate_ring_stats(struct bnxt *bp)
10632 {
10633 	struct bnxt_stats_mem *ring0_stats;
10634 	bool ignore_zero = false;
10635 	int i;
10636 
10637 	/* Chip bug.  Counter intermittently becomes 0. */
10638 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10639 		ignore_zero = true;
10640 
10641 	for (i = 0; i < bp->cp_nr_rings; i++) {
10642 		struct bnxt_napi *bnapi = bp->bnapi[i];
10643 		struct bnxt_cp_ring_info *cpr;
10644 		struct bnxt_stats_mem *stats;
10645 
10646 		cpr = &bnapi->cp_ring;
10647 		stats = &cpr->stats;
10648 		if (!i)
10649 			ring0_stats = stats;
10650 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10651 					ring0_stats->hw_masks,
10652 					ring0_stats->len / 8, ignore_zero);
10653 	}
10654 }
10655 
10656 static void bnxt_accumulate_port_stats(struct bnxt *bp)
10657 {
10658 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10659 		struct bnxt_stats_mem *stats = &bp->port_stats;
10660 		__le64 *hw_stats = stats->hw_stats;
10661 		u64 *sw_stats = stats->sw_stats;
10662 		u64 *masks = stats->hw_masks;
10663 		int cnt;
10664 
10665 		cnt = sizeof(struct rx_port_stats) / 8;
10666 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10667 
10668 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10669 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10670 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10671 		cnt = sizeof(struct tx_port_stats) / 8;
10672 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10673 	}
10674 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10675 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10676 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10677 	}
10678 }
10679 
10680 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10681 {
10682 	bnxt_accumulate_ring_stats(bp);
10683 	bnxt_accumulate_port_stats(bp);
10684 }
10685 
10686 /* Re-accumulate ring stats from DMA buffers if stale.
10687  * uAPIs for reading sw_stats should call this first.
10688  *
10689  * We promise user space update frequency of bp->stats_coal_ticks but
10690  * the update is a two step process - first device updates the DMA buffer,
10691  * then we have to update from that buffer to driver stats in the service work.
10692  * Worst case we would be 2x off from the desired frequency.
10693  * Sync the stats sooner, if stale. The 20% threshold was chosen arbitrarily.
10694  *
10695  * Ideally we would split the user-configured time into two portions,
10696  * i.e. also lower the DMA period by the 20%. But the DMA timer seems to have
10697  * too coarse granularity to play such tricks.
10698  */
10699 void bnxt_sync_ring_stats(struct bnxt *bp)
10700 {
10701 	unsigned long stale;
10702 
10703 	if (!netif_running(bp->dev) || !bp->stats_coal_ticks)
10704 		return;
10705 
10706 	spin_lock(&bp->stats_lock);
10707 	stale = usecs_to_jiffies(bp->stats_coal_ticks / 5);
10708 	if (time_after_eq(jiffies, bp->stats_updated_jiffies + stale)) {
10709 		bnxt_accumulate_ring_stats(bp);
10710 		bp->stats_updated_jiffies = jiffies;
10711 	}
10712 	spin_unlock(&bp->stats_lock);
10713 }
10714 
10715 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10716 {
10717 	struct hwrm_port_qstats_input *req;
10718 	struct bnxt_pf_info *pf = &bp->pf;
10719 	int rc;
10720 
10721 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10722 		return 0;
10723 
10724 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10725 		return -EOPNOTSUPP;
10726 
10727 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10728 	if (rc)
10729 		return rc;
10730 
10731 	req->flags = flags;
10732 	req->port_id = cpu_to_le16(pf->port_id);
10733 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10734 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10735 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10736 	return hwrm_req_send(bp, req);
10737 }
10738 
10739 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10740 {
10741 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10742 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10743 	struct hwrm_port_qstats_ext_output *resp_qs;
10744 	struct hwrm_port_qstats_ext_input *req_qs;
10745 	struct bnxt_pf_info *pf = &bp->pf;
10746 	u32 tx_stat_size;
10747 	int rc;
10748 
10749 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10750 		return 0;
10751 
10752 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10753 		return -EOPNOTSUPP;
10754 
10755 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10756 	if (rc)
10757 		return rc;
10758 
10759 	req_qs->flags = flags;
10760 	req_qs->port_id = cpu_to_le16(pf->port_id);
10761 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10762 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10763 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10764 		       sizeof(struct tx_port_stats_ext) : 0;
10765 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10766 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10767 	resp_qs = hwrm_req_hold(bp, req_qs);
10768 	rc = hwrm_req_send(bp, req_qs);
10769 	if (!rc) {
10770 		bp->fw_rx_stats_ext_size =
10771 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10772 		if (BNXT_FW_MAJ(bp) < 220 &&
10773 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10774 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10775 
10776 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10777 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10778 	} else {
10779 		bp->fw_rx_stats_ext_size = 0;
10780 		bp->fw_tx_stats_ext_size = 0;
10781 	}
10782 	hwrm_req_drop(bp, req_qs);
10783 
10784 	if (flags)
10785 		return rc;
10786 
10787 	if (bp->fw_tx_stats_ext_size <=
10788 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10789 		bp->pri2cos_valid = 0;
10790 		return rc;
10791 	}
10792 
10793 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10794 	if (rc)
10795 		return rc;
10796 
10797 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10798 
10799 	resp_qc = hwrm_req_hold(bp, req_qc);
10800 	rc = hwrm_req_send(bp, req_qc);
10801 	if (!rc) {
10802 		u8 *pri2cos;
10803 		int i, j;
10804 
10805 		pri2cos = &resp_qc->pri0_cos_queue_id;
10806 		for (i = 0; i < 8; i++) {
10807 			u8 queue_id = pri2cos[i];
10808 			u8 queue_idx;
10809 
10810 			/* Per port queue IDs start from 0, 10, 20, etc */
10811 			queue_idx = queue_id % 10;
10812 			if (queue_idx > BNXT_MAX_QUEUE) {
10813 				bp->pri2cos_valid = false;
10814 				hwrm_req_drop(bp, req_qc);
10815 				return rc;
10816 			}
10817 			for (j = 0; j < bp->max_q; j++) {
10818 				if (bp->q_ids[j] == queue_id)
10819 					bp->pri2cos_idx[i] = queue_idx;
10820 			}
10821 		}
10822 		bp->pri2cos_valid = true;
10823 	}
10824 	hwrm_req_drop(bp, req_qc);
10825 
10826 	return rc;
10827 }
10828 
10829 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10830 {
10831 	bnxt_hwrm_tunnel_dst_port_free(bp,
10832 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10833 	bnxt_hwrm_tunnel_dst_port_free(bp,
10834 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10835 }
10836 
10837 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10838 {
10839 	int rc, i;
10840 	u32 tpa_flags = 0;
10841 
10842 	if (set_tpa)
10843 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10844 	else if (BNXT_NO_FW_ACCESS(bp))
10845 		return 0;
10846 	for (i = 0; i < bp->nr_vnics; i++) {
10847 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10848 		if (rc) {
10849 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10850 				   i, rc);
10851 			return rc;
10852 		}
10853 	}
10854 	return 0;
10855 }
10856 
10857 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10858 {
10859 	int i;
10860 
10861 	for (i = 0; i < bp->nr_vnics; i++)
10862 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10863 }
10864 
10865 static void bnxt_clear_vnic(struct bnxt *bp)
10866 {
10867 	if (!bp->vnic_info)
10868 		return;
10869 
10870 	bnxt_hwrm_clear_vnic_filter(bp);
10871 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10872 		/* clear all RSS setting before free vnic ctx */
10873 		bnxt_hwrm_clear_vnic_rss(bp);
10874 		bnxt_hwrm_vnic_ctx_free(bp);
10875 	}
10876 	/* before free the vnic, undo the vnic tpa settings */
10877 	if (bp->flags & BNXT_FLAG_TPA)
10878 		bnxt_set_tpa(bp, false);
10879 	bnxt_hwrm_vnic_free(bp);
10880 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10881 		bnxt_hwrm_vnic_ctx_free(bp);
10882 }
10883 
10884 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10885 				    bool irq_re_init)
10886 {
10887 	bnxt_clear_vnic(bp);
10888 	bnxt_hwrm_ring_free(bp, close_path);
10889 	bnxt_hwrm_ring_grp_free(bp);
10890 	if (irq_re_init) {
10891 		bnxt_hwrm_stat_ctx_free(bp);
10892 		bnxt_hwrm_free_tunnel_ports(bp);
10893 	}
10894 }
10895 
10896 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10897 {
10898 	struct hwrm_func_cfg_input *req;
10899 	u8 evb_mode;
10900 	int rc;
10901 
10902 	if (br_mode == BRIDGE_MODE_VEB)
10903 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10904 	else if (br_mode == BRIDGE_MODE_VEPA)
10905 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10906 	else
10907 		return -EINVAL;
10908 
10909 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10910 	if (rc)
10911 		return rc;
10912 
10913 	req->fid = cpu_to_le16(0xffff);
10914 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10915 	req->evb_mode = evb_mode;
10916 	return hwrm_req_send(bp, req);
10917 }
10918 
10919 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10920 {
10921 	struct hwrm_func_cfg_input *req;
10922 	int rc;
10923 
10924 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10925 		return 0;
10926 
10927 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10928 	if (rc)
10929 		return rc;
10930 
10931 	req->fid = cpu_to_le16(0xffff);
10932 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10933 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10934 	if (size == 128)
10935 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10936 
10937 	return hwrm_req_send(bp, req);
10938 }
10939 
10940 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10941 {
10942 	int rc;
10943 
10944 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10945 		goto skip_rss_ctx;
10946 
10947 	/* allocate context for vnic */
10948 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10949 	if (rc) {
10950 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10951 			   vnic->vnic_id, rc);
10952 		goto vnic_setup_err;
10953 	}
10954 	bp->rsscos_nr_ctxs++;
10955 
10956 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10957 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10958 		if (rc) {
10959 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10960 				   vnic->vnic_id, rc);
10961 			goto vnic_setup_err;
10962 		}
10963 		bp->rsscos_nr_ctxs++;
10964 	}
10965 
10966 skip_rss_ctx:
10967 	bnxt_fill_hw_rss_tbl(bp, vnic);
10968 	/* configure default vnic, ring grp */
10969 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10970 	if (rc) {
10971 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10972 			   vnic->vnic_id, rc);
10973 		goto vnic_setup_err;
10974 	}
10975 
10976 	/* Enable RSS hashing on vnic */
10977 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10978 	if (rc) {
10979 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10980 			   vnic->vnic_id, rc);
10981 		goto vnic_setup_err;
10982 	}
10983 
10984 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10985 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10986 		if (rc) {
10987 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10988 				   vnic->vnic_id, rc);
10989 		}
10990 	}
10991 
10992 vnic_setup_err:
10993 	return rc;
10994 }
10995 
10996 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10997 			  u8 valid)
10998 {
10999 	struct hwrm_vnic_update_input *req;
11000 	int rc;
11001 
11002 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
11003 	if (rc)
11004 		return rc;
11005 
11006 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
11007 
11008 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
11009 		req->mru = cpu_to_le16(vnic->mru);
11010 
11011 	req->enables = cpu_to_le32(valid);
11012 
11013 	return hwrm_req_send(bp, req);
11014 }
11015 
11016 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
11017 {
11018 	int rc;
11019 
11020 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
11021 	if (rc) {
11022 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
11023 			   vnic->vnic_id, rc);
11024 		return rc;
11025 	}
11026 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
11027 	if (rc)
11028 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
11029 			   vnic->vnic_id, rc);
11030 	return rc;
11031 }
11032 
11033 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
11034 {
11035 	int rc, i, nr_ctxs;
11036 
11037 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
11038 	for (i = 0; i < nr_ctxs; i++) {
11039 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
11040 		if (rc) {
11041 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
11042 				   vnic->vnic_id, i, rc);
11043 			break;
11044 		}
11045 		bp->rsscos_nr_ctxs++;
11046 	}
11047 	if (i < nr_ctxs)
11048 		return -ENOMEM;
11049 
11050 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
11051 	if (rc)
11052 		return rc;
11053 
11054 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
11055 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
11056 		if (rc) {
11057 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
11058 				   vnic->vnic_id, rc);
11059 		}
11060 	}
11061 	return rc;
11062 }
11063 
11064 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
11065 {
11066 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11067 		return __bnxt_setup_vnic_p5(bp, vnic);
11068 	else
11069 		return __bnxt_setup_vnic(bp, vnic);
11070 }
11071 
11072 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
11073 				     struct bnxt_vnic_info *vnic,
11074 				     u16 start_rx_ring_idx, int rx_rings)
11075 {
11076 	int rc;
11077 
11078 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
11079 	if (rc) {
11080 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
11081 			   vnic->vnic_id, rc);
11082 		return rc;
11083 	}
11084 	return bnxt_setup_vnic(bp, vnic);
11085 }
11086 
11087 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
11088 {
11089 	struct bnxt_vnic_info *vnic;
11090 	int i, rc = 0;
11091 
11092 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
11093 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
11094 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
11095 	}
11096 
11097 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11098 		return 0;
11099 
11100 	for (i = 0; i < bp->rx_nr_rings; i++) {
11101 		u16 vnic_id = i + 1;
11102 		u16 ring_id = i;
11103 
11104 		if (vnic_id >= bp->nr_vnics)
11105 			break;
11106 
11107 		vnic = &bp->vnic_info[vnic_id];
11108 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
11109 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
11110 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
11111 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
11112 			break;
11113 	}
11114 	return rc;
11115 }
11116 
11117 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
11118 			  bool all)
11119 {
11120 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
11121 	struct bnxt_filter_base *usr_fltr, *tmp;
11122 	struct bnxt_ntuple_filter *ntp_fltr;
11123 	int i;
11124 
11125 	bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
11126 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
11127 		if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
11128 			bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
11129 	}
11130 	if (!all)
11131 		return;
11132 
11133 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
11134 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
11135 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
11136 			ntp_fltr = container_of(usr_fltr,
11137 						struct bnxt_ntuple_filter,
11138 						base);
11139 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
11140 			bnxt_del_ntp_filter(bp, ntp_fltr);
11141 			bnxt_del_one_usr_fltr(bp, usr_fltr);
11142 		}
11143 	}
11144 
11145 	if (vnic->rss_table)
11146 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
11147 				  vnic->rss_table,
11148 				  vnic->rss_table_dma_addr);
11149 	bp->num_rss_ctx--;
11150 }
11151 
11152 static bool bnxt_vnic_has_rx_ring(struct bnxt *bp, struct bnxt_vnic_info *vnic,
11153 				  int rxr_id)
11154 {
11155 	u16 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
11156 	int i, vnic_rx;
11157 
11158 	/* Ntuple VNIC always has all the rx rings. Any change of ring id
11159 	 * must be updated because a future filter may use it.
11160 	 */
11161 	if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
11162 		return true;
11163 
11164 	for (i = 0; i < tbl_size; i++) {
11165 		if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
11166 			vnic_rx = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
11167 		else
11168 			vnic_rx = bp->rss_indir_tbl[i];
11169 
11170 		if (rxr_id == vnic_rx)
11171 			return true;
11172 	}
11173 
11174 	return false;
11175 }
11176 
11177 static int bnxt_set_vnic_mru_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic,
11178 				u16 mru, int rxr_id)
11179 {
11180 	int rc;
11181 
11182 	if (!bnxt_vnic_has_rx_ring(bp, vnic, rxr_id))
11183 		return 0;
11184 
11185 	if (mru) {
11186 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
11187 		if (rc) {
11188 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
11189 				   vnic->vnic_id, rc);
11190 			return rc;
11191 		}
11192 		if (rxr_id == vnic->default_rx_ring) {
11193 			rc = bnxt_hwrm_vnic_cfg(bp, vnic);
11194 			if (rc)
11195 				return rc;
11196 		}
11197 	}
11198 	vnic->mru = mru;
11199 	bnxt_hwrm_vnic_update(bp, vnic,
11200 			      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
11201 
11202 	return 0;
11203 }
11204 
11205 static int bnxt_set_rss_ctx_vnic_mru(struct bnxt *bp, u16 mru, int rxr_id)
11206 {
11207 	struct ethtool_rxfh_context *ctx;
11208 	unsigned long context;
11209 	int rc;
11210 
11211 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11212 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11213 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
11214 
11215 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, rxr_id);
11216 		if (rc)
11217 			return rc;
11218 	}
11219 
11220 	return 0;
11221 }
11222 
11223 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
11224 {
11225 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
11226 	struct ethtool_rxfh_context *ctx;
11227 	unsigned long context;
11228 
11229 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11230 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11231 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
11232 
11233 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
11234 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
11235 		    __bnxt_setup_vnic_p5(bp, vnic)) {
11236 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
11237 				   rss_ctx->index);
11238 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
11239 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
11240 		}
11241 	}
11242 }
11243 
11244 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
11245 {
11246 	struct ethtool_rxfh_context *ctx;
11247 	unsigned long context;
11248 
11249 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11250 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11251 
11252 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
11253 	}
11254 }
11255 
11256 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
11257 static bool bnxt_promisc_ok(struct bnxt *bp)
11258 {
11259 #ifdef CONFIG_BNXT_SRIOV
11260 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
11261 		return false;
11262 #endif
11263 	return true;
11264 }
11265 
11266 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
11267 {
11268 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
11269 	unsigned int rc = 0;
11270 
11271 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
11272 	if (rc) {
11273 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
11274 			   rc);
11275 		return rc;
11276 	}
11277 
11278 	/* Setup the proper default RX ring */
11279 	bnxt_fill_hw_rss_tbl(bp, vnic);
11280 
11281 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
11282 	if (rc) {
11283 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
11284 			   rc);
11285 		return rc;
11286 	}
11287 	return rc;
11288 }
11289 
11290 static int bnxt_cfg_rx_mode(struct bnxt *, struct netdev_hw_addr_list *, bool);
11291 static bool bnxt_mc_list_updated(struct bnxt *, u32 *,
11292 				 const struct netdev_hw_addr_list *);
11293 
11294 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
11295 {
11296 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
11297 	int rc = 0;
11298 	unsigned int rx_nr_rings = bp->rx_nr_rings;
11299 
11300 	if (irq_re_init) {
11301 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
11302 		if (rc) {
11303 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
11304 				   rc);
11305 			goto err_out;
11306 		}
11307 	}
11308 
11309 	rc = bnxt_hwrm_ring_alloc(bp);
11310 	if (rc) {
11311 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
11312 		goto err_out;
11313 	}
11314 
11315 	rc = bnxt_hwrm_ring_grp_alloc(bp);
11316 	if (rc) {
11317 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
11318 		goto err_out;
11319 	}
11320 
11321 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11322 		rx_nr_rings--;
11323 
11324 	/* default vnic 0 */
11325 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
11326 	if (rc) {
11327 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
11328 		goto err_out;
11329 	}
11330 
11331 	if (BNXT_VF(bp))
11332 		bnxt_hwrm_func_qcfg(bp);
11333 
11334 	rc = bnxt_setup_vnic(bp, vnic);
11335 	if (rc)
11336 		goto err_out;
11337 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
11338 		bnxt_hwrm_update_rss_hash_cfg(bp);
11339 
11340 	if (bp->flags & BNXT_FLAG_RFS) {
11341 		rc = bnxt_alloc_rfs_vnics(bp);
11342 		if (rc)
11343 			goto err_out;
11344 	}
11345 
11346 	if (bp->flags & BNXT_FLAG_TPA) {
11347 		rc = bnxt_set_tpa(bp, true);
11348 		if (rc)
11349 			goto err_out;
11350 	}
11351 
11352 	if (BNXT_VF(bp))
11353 		bnxt_update_vf_mac(bp);
11354 
11355 	/* Filter for default vnic 0 */
11356 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
11357 	if (rc) {
11358 		if (rc == -ECOMM)
11359 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
11360 		else
11361 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
11362 		goto err_out;
11363 	}
11364 	vnic->uc_filter_count = 1;
11365 
11366 	vnic->rx_mask = 0;
11367 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
11368 		goto skip_rx_mask;
11369 
11370 	if (bp->dev->flags & IFF_BROADCAST)
11371 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
11372 
11373 	if (bp->dev->flags & IFF_PROMISC)
11374 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11375 
11376 	if (bp->dev->flags & IFF_ALLMULTI) {
11377 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
11378 		vnic->mc_list_count = 0;
11379 	} else if (bp->dev->flags & IFF_MULTICAST) {
11380 		u32 mask = 0;
11381 
11382 		bnxt_mc_list_updated(bp, &mask, &bp->dev->mc);
11383 		vnic->rx_mask |= mask;
11384 	}
11385 
11386 	rc = bnxt_cfg_rx_mode(bp, &bp->dev->uc, true);
11387 	if (rc == -EAGAIN) {
11388 		netif_rx_mode_schedule_retry(bp->dev);
11389 		rc = 0;
11390 	} else if (rc) {
11391 		goto err_out;
11392 	}
11393 
11394 skip_rx_mask:
11395 	rc = bnxt_hwrm_set_coal(bp);
11396 	if (rc)
11397 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
11398 				rc);
11399 
11400 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11401 		rc = bnxt_setup_nitroa0_vnic(bp);
11402 		if (rc)
11403 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
11404 				   rc);
11405 	}
11406 
11407 	if (BNXT_VF(bp)) {
11408 		bnxt_hwrm_func_qcfg(bp);
11409 		netdev_update_features(bp->dev);
11410 	}
11411 
11412 	return 0;
11413 
11414 err_out:
11415 	bnxt_hwrm_resource_free(bp, 0, true);
11416 
11417 	return rc;
11418 }
11419 
11420 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
11421 {
11422 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
11423 	return 0;
11424 }
11425 
11426 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
11427 {
11428 	int rc;
11429 
11430 	bnxt_init_cp_rings(bp);
11431 	rc = bnxt_init_rx_rings(bp);
11432 	if (rc)
11433 		return rc;
11434 
11435 	bnxt_init_tx_rings(bp);
11436 	bnxt_init_ring_grps(bp, irq_re_init);
11437 	bnxt_init_vnics(bp);
11438 
11439 	return bnxt_init_chip(bp, irq_re_init);
11440 }
11441 
11442 static int bnxt_set_real_num_queues(struct bnxt *bp)
11443 {
11444 	int rc;
11445 	struct net_device *dev = bp->dev;
11446 
11447 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
11448 					  bp->tx_nr_rings_xdp);
11449 	if (rc)
11450 		return rc;
11451 
11452 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
11453 	if (rc)
11454 		return rc;
11455 
11456 #ifdef CONFIG_RFS_ACCEL
11457 	if (bp->flags & BNXT_FLAG_RFS)
11458 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
11459 #endif
11460 
11461 	return rc;
11462 }
11463 
11464 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11465 			     bool shared)
11466 {
11467 	int _rx = *rx, _tx = *tx;
11468 
11469 	if (shared) {
11470 		*rx = min_t(int, _rx, max);
11471 		*tx = min_t(int, _tx, max);
11472 	} else {
11473 		if (max < 2)
11474 			return -ENOMEM;
11475 
11476 		while (_rx + _tx > max) {
11477 			if (_rx > _tx && _rx > 1)
11478 				_rx--;
11479 			else if (_tx > 1)
11480 				_tx--;
11481 		}
11482 		*rx = _rx;
11483 		*tx = _tx;
11484 	}
11485 	return 0;
11486 }
11487 
11488 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
11489 {
11490 	return (tx - tx_xdp) / tx_sets + tx_xdp;
11491 }
11492 
11493 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
11494 {
11495 	int tcs = bp->num_tc;
11496 
11497 	if (!tcs)
11498 		tcs = 1;
11499 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
11500 }
11501 
11502 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
11503 {
11504 	int tcs = bp->num_tc;
11505 
11506 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
11507 	       bp->tx_nr_rings_xdp;
11508 }
11509 
11510 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11511 			   bool sh)
11512 {
11513 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
11514 
11515 	if (tx_cp != *tx) {
11516 		int tx_saved = tx_cp, rc;
11517 
11518 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
11519 		if (rc)
11520 			return rc;
11521 		if (tx_cp != tx_saved)
11522 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
11523 		return 0;
11524 	}
11525 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
11526 }
11527 
11528 static void bnxt_setup_msix(struct bnxt *bp)
11529 {
11530 	const int len = sizeof(bp->irq_tbl[0].name);
11531 	struct net_device *dev = bp->dev;
11532 	int tcs, i;
11533 
11534 	tcs = bp->num_tc;
11535 	if (tcs) {
11536 		int i, off, count;
11537 
11538 		for (i = 0; i < tcs; i++) {
11539 			count = bp->tx_nr_rings_per_tc;
11540 			off = BNXT_TC_TO_RING_BASE(bp, i);
11541 			netdev_set_tc_queue(dev, i, count, off);
11542 		}
11543 	}
11544 
11545 	for (i = 0; i < bp->cp_nr_rings; i++) {
11546 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11547 		char *attr;
11548 
11549 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11550 			attr = "TxRx";
11551 		else if (i < bp->rx_nr_rings)
11552 			attr = "rx";
11553 		else
11554 			attr = "tx";
11555 
11556 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
11557 			 attr, i);
11558 		bp->irq_tbl[map_idx].handler = bnxt_msix;
11559 	}
11560 }
11561 
11562 static int bnxt_init_int_mode(struct bnxt *bp);
11563 
11564 static int bnxt_change_msix(struct bnxt *bp, int total)
11565 {
11566 	struct msi_map map;
11567 	int i;
11568 
11569 	/* add MSIX to the end if needed */
11570 	for (i = bp->total_irqs; i < total; i++) {
11571 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
11572 		if (map.index < 0)
11573 			return bp->total_irqs;
11574 		bp->irq_tbl[i].vector = map.virq;
11575 		bp->total_irqs++;
11576 	}
11577 
11578 	/* trim MSIX from the end if needed */
11579 	for (i = bp->total_irqs; i > total; i--) {
11580 		map.index = i - 1;
11581 		map.virq = bp->irq_tbl[i - 1].vector;
11582 		pci_msix_free_irq(bp->pdev, map);
11583 		bp->irq_tbl[i - 1].vector = 0;
11584 		bp->total_irqs--;
11585 	}
11586 	return bp->total_irqs;
11587 }
11588 
11589 static int bnxt_setup_int_mode(struct bnxt *bp)
11590 {
11591 	int rc;
11592 
11593 	if (!bp->irq_tbl) {
11594 		rc = bnxt_init_int_mode(bp);
11595 		if (rc || !bp->irq_tbl)
11596 			return rc ?: -ENODEV;
11597 	}
11598 
11599 	bnxt_setup_msix(bp);
11600 
11601 	rc = bnxt_set_real_num_queues(bp);
11602 	return rc;
11603 }
11604 
11605 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
11606 {
11607 	return bp->hw_resc.max_rsscos_ctxs;
11608 }
11609 
11610 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
11611 {
11612 	return bp->hw_resc.max_vnics;
11613 }
11614 
11615 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11616 {
11617 	return bp->hw_resc.max_stat_ctxs;
11618 }
11619 
11620 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11621 {
11622 	return bp->hw_resc.max_cp_rings;
11623 }
11624 
11625 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11626 {
11627 	unsigned int cp = bp->hw_resc.max_cp_rings;
11628 
11629 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11630 		cp -= bnxt_get_ulp_msix_num(bp);
11631 
11632 	return cp;
11633 }
11634 
11635 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11636 {
11637 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11638 
11639 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11640 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11641 
11642 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11643 }
11644 
11645 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11646 {
11647 	bp->hw_resc.max_irqs = max_irqs;
11648 }
11649 
11650 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11651 {
11652 	unsigned int cp;
11653 
11654 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11655 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11656 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11657 	else
11658 		return cp - bp->cp_nr_rings;
11659 }
11660 
11661 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11662 {
11663 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11664 }
11665 
11666 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11667 {
11668 	int max_irq = bnxt_get_max_func_irqs(bp);
11669 	int total_req = bp->cp_nr_rings + num;
11670 
11671 	if (max_irq < total_req) {
11672 		num = max_irq - bp->cp_nr_rings;
11673 		if (num <= 0)
11674 			return 0;
11675 	}
11676 	return num;
11677 }
11678 
11679 static int bnxt_get_num_msix(struct bnxt *bp)
11680 {
11681 	if (!BNXT_NEW_RM(bp))
11682 		return bnxt_get_max_func_irqs(bp);
11683 
11684 	return bnxt_nq_rings_in_use(bp);
11685 }
11686 
11687 static int bnxt_init_int_mode(struct bnxt *bp)
11688 {
11689 	int i, total_vecs, max, rc, min = 1, ulp_msix, tx_cp, tbl_size;
11690 
11691 	total_vecs = bnxt_get_num_msix(bp);
11692 	max = bnxt_get_max_func_irqs(bp);
11693 	if (total_vecs > max)
11694 		total_vecs = max;
11695 
11696 	if (!total_vecs)
11697 		return 0;
11698 
11699 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11700 		min = 2;
11701 
11702 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11703 					   PCI_IRQ_MSIX);
11704 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11705 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11706 		rc = -ENODEV;
11707 		goto msix_setup_exit;
11708 	}
11709 
11710 	tbl_size = total_vecs;
11711 	if (pci_msix_can_alloc_dyn(bp->pdev))
11712 		tbl_size = max;
11713 	bp->irq_tbl = kzalloc_objs(*bp->irq_tbl, tbl_size);
11714 	if (!bp->irq_tbl) {
11715 		rc = -ENOMEM;
11716 		goto msix_setup_exit;
11717 	}
11718 
11719 	for (i = 0; i < total_vecs; i++)
11720 		bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11721 
11722 	bp->total_irqs = total_vecs;
11723 	/* Trim rings based upon num of vectors allocated */
11724 	rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11725 			     total_vecs - ulp_msix, min == 1);
11726 	if (rc)
11727 		goto msix_setup_exit;
11728 
11729 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11730 	bp->cp_nr_rings = (min == 1) ?
11731 			  max_t(int, tx_cp, bp->rx_nr_rings) :
11732 			  tx_cp + bp->rx_nr_rings;
11733 
11734 	return 0;
11735 
11736 msix_setup_exit:
11737 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11738 	kfree(bp->irq_tbl);
11739 	bp->irq_tbl = NULL;
11740 	pci_free_irq_vectors(bp->pdev);
11741 	return rc;
11742 }
11743 
11744 static void bnxt_clear_int_mode(struct bnxt *bp)
11745 {
11746 	pci_free_irq_vectors(bp->pdev);
11747 
11748 	kfree(bp->irq_tbl);
11749 	bp->irq_tbl = NULL;
11750 }
11751 
11752 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11753 {
11754 	struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA];
11755 	bool irq_cleared = false;
11756 	bool irq_change = false;
11757 	int tcs = bp->num_tc;
11758 	int irqs_required;
11759 	int rc;
11760 
11761 	if (!bnxt_need_reserve_rings(bp)) {
11762 		/* FW may already hold the reservation from an earlier
11763 		 * reservation that was not followed by growing the dynamic
11764 		 * MSI-X vectors, e.g. because the allocation failed.
11765 		 */
11766 		if (irq_re_init && BNXT_NEW_RM(bp) && bp->irq_tbl &&
11767 		    pci_msix_can_alloc_dyn(bp->pdev)) {
11768 			irqs_required = bnxt_get_num_msix(bp);
11769 			if (irqs_required > bp->total_irqs &&
11770 			    bnxt_change_msix(bp, irqs_required) != irqs_required) {
11771 				netdev_err(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
11772 					   irqs_required, bp->total_irqs);
11773 				return -ENOSPC;
11774 			}
11775 		}
11776 		return 0;
11777 	}
11778 
11779 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(edev)) {
11780 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11781 
11782 		if (ulp_msix > bp->ulp_num_msix_want)
11783 			ulp_msix = bp->ulp_num_msix_want;
11784 		irqs_required = ulp_msix + bp->cp_nr_rings;
11785 	} else {
11786 		irqs_required = bnxt_get_num_msix(bp);
11787 	}
11788 
11789 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11790 		irq_change = true;
11791 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11792 			bnxt_ulp_irq_stop(bp);
11793 			bnxt_clear_int_mode(bp);
11794 			irq_cleared = true;
11795 		}
11796 	}
11797 	rc = __bnxt_reserve_rings(bp);
11798 	if (irq_cleared) {
11799 		if (!rc)
11800 			rc = bnxt_init_int_mode(bp);
11801 		bnxt_ulp_irq_restart(bp, rc);
11802 	} else if (irq_change && !rc) {
11803 		/* Size the vectors from what FW actually reserved, which
11804 		 * can be less than what was requested.
11805 		 */
11806 		irqs_required = bnxt_get_num_msix(bp);
11807 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11808 			rc = -ENOSPC;
11809 	}
11810 	if (rc) {
11811 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11812 		return rc;
11813 	}
11814 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11815 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11816 		netdev_err(bp->dev, "tx ring reservation failure\n");
11817 		netdev_reset_tc(bp->dev);
11818 		bp->num_tc = 0;
11819 		if (bp->tx_nr_rings_xdp)
11820 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11821 		else
11822 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11823 		return -ENOMEM;
11824 	}
11825 	return 0;
11826 }
11827 
11828 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx)
11829 {
11830 	struct bnxt_tx_ring_info *txr;
11831 	struct netdev_queue *txq;
11832 	struct bnxt_napi *bnapi;
11833 	int i;
11834 
11835 	bnapi = bp->bnapi[idx];
11836 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11837 		WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11838 		synchronize_net();
11839 
11840 		if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) {
11841 			txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11842 			if (txq) {
11843 				__netif_tx_lock_bh(txq);
11844 				netif_tx_stop_queue(txq);
11845 				__netif_tx_unlock_bh(txq);
11846 			}
11847 		}
11848 
11849 		if (!bp->tph_mode)
11850 			continue;
11851 
11852 		bnxt_hwrm_tx_ring_free(bp, txr, true);
11853 		bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr);
11854 		bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index);
11855 		bnxt_clear_one_cp_ring(bp, txr->tx_cpr);
11856 	}
11857 }
11858 
11859 static int bnxt_tx_queue_start(struct bnxt *bp, int idx)
11860 {
11861 	struct bnxt_tx_ring_info *txr;
11862 	struct netdev_queue *txq;
11863 	struct bnxt_napi *bnapi;
11864 	int rc, i;
11865 
11866 	bnapi = bp->bnapi[idx];
11867 	/* All rings have been reserved and previously allocated.
11868 	 * Reallocating with the same parameters should never fail.
11869 	 */
11870 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11871 		if (!bp->tph_mode)
11872 			goto start_tx;
11873 
11874 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
11875 		if (rc)
11876 			return rc;
11877 
11878 		rc = bnxt_hwrm_set_tx_ring_coal(bp, txr);
11879 		if (rc)
11880 			netdev_warn(bp->dev, "HWRM set coalescing failure rc: %d\n",
11881 				    rc);
11882 
11883 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false);
11884 		if (rc)
11885 			return rc;
11886 
11887 		txr->tx_prod = 0;
11888 		txr->tx_cons = 0;
11889 		txr->tx_hw_cons = 0;
11890 		txr->kick_pending = 0;
11891 		txr->kick_txbd0 = NULL;
11892 start_tx:
11893 		WRITE_ONCE(txr->dev_state, 0);
11894 		synchronize_net();
11895 
11896 		if (bnapi->flags & BNXT_NAPI_FLAG_XDP)
11897 			continue;
11898 
11899 		txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11900 		if (txq)
11901 			netif_tx_start_queue(txq);
11902 	}
11903 
11904 	return 0;
11905 }
11906 
11907 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify,
11908 				     const cpumask_t *mask)
11909 {
11910 	struct bnxt_irq *irq;
11911 	u16 tag;
11912 
11913 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11914 
11915 	cpumask_copy(irq->bp->ring_cpu_mask[irq->ring_nr], mask);
11916 	set_bit(irq->ring_nr, irq->bp->ring_affinity_set);
11917 
11918 #ifdef CONFIG_RFS_ACCEL
11919 	if (irq->bp->dev->rx_cpu_rmap && irq->ring_nr < irq->bp->rx_nr_rings) {
11920 		int err;
11921 
11922 		err = cpu_rmap_update(irq->bp->dev->rx_cpu_rmap, irq->ring_nr,
11923 				      mask);
11924 		if (err)
11925 			netdev_warn(irq->bp->dev,
11926 				    "aRFS rmap update failed: %d\n", err);
11927 	}
11928 #endif
11929 
11930 	if (!irq->bp->tph_mode)
11931 		return;
11932 
11933 	if (irq->ring_nr >= irq->bp->rx_nr_rings)
11934 		return;
11935 
11936 	if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11937 				cpumask_first(mask), &tag))
11938 		return;
11939 
11940 	WRITE_ONCE(irq->new_tag, tag);
11941 	bnxt_queue_sp_work(irq->bp, BNXT_TPH_UPDATE_SP_EVENT);
11942 }
11943 
11944 static void bnxt_irq_affinity_release(struct kref *ref)
11945 {
11946 	struct irq_affinity_notify *notify =
11947 		container_of(ref, struct irq_affinity_notify, kref);
11948 	struct bnxt_irq *irq;
11949 
11950 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11951 
11952 	if (!irq->bp->tph_mode)
11953 		return;
11954 
11955 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) {
11956 		netdev_err(irq->bp->dev,
11957 			   "Setting ST=0 for MSIX entry %d failed\n",
11958 			   irq->msix_nr);
11959 		return;
11960 	}
11961 }
11962 
11963 static void bnxt_release_irq_notifier(struct bnxt_irq *irq)
11964 {
11965 	irq_set_affinity_notifier(irq->vector, NULL);
11966 }
11967 
11968 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq)
11969 {
11970 	struct irq_affinity_notify *notify;
11971 
11972 	irq->bp = bp;
11973 
11974 	/* Register IRQ affinity notifier */
11975 	notify = &irq->affinity_notify;
11976 	notify->irq = irq->vector;
11977 	notify->notify = bnxt_irq_affinity_notify;
11978 	notify->release = bnxt_irq_affinity_release;
11979 
11980 	irq_set_affinity_notifier(irq->vector, notify);
11981 }
11982 
11983 static int bnxt_alloc_ring_cpu_masks(struct bnxt *bp)
11984 {
11985 	int i;
11986 
11987 	bp->ring_cpu_mask = kzalloc_objs(*bp->ring_cpu_mask, bp->max_irqs);
11988 	if (!bp->ring_cpu_mask)
11989 		return -ENOMEM;
11990 
11991 	bp->ring_affinity_set = bitmap_zalloc(bp->max_irqs, GFP_KERNEL);
11992 	if (!bp->ring_affinity_set)
11993 		return -ENOMEM;
11994 
11995 	for (i = 0; i < bp->max_irqs; i++)
11996 		if (!zalloc_cpumask_var(&bp->ring_cpu_mask[i], GFP_KERNEL))
11997 			return -ENOMEM;
11998 
11999 	return 0;
12000 }
12001 
12002 static void bnxt_free_ring_cpu_masks(struct bnxt *bp)
12003 {
12004 	int i;
12005 
12006 	if (!bp->ring_cpu_mask)
12007 		return;
12008 
12009 	for (i = 0; i < bp->max_irqs; i++)
12010 		free_cpumask_var(bp->ring_cpu_mask[i]);
12011 
12012 	bitmap_free(bp->ring_affinity_set);
12013 	bp->ring_affinity_set = NULL;
12014 	kfree(bp->ring_cpu_mask);
12015 	bp->ring_cpu_mask = NULL;
12016 }
12017 
12018 static void bnxt_free_irq(struct bnxt *bp)
12019 {
12020 	struct bnxt_irq *irq;
12021 	int i;
12022 
12023 	if (!bp->irq_tbl || !bp->bnapi)
12024 		return;
12025 
12026 	for (i = 0; i < bp->cp_nr_rings; i++) {
12027 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
12028 
12029 		irq = &bp->irq_tbl[map_idx];
12030 		if (irq->requested) {
12031 			bnxt_release_irq_notifier(irq);
12032 			irq_update_affinity_hint(irq->vector, NULL);
12033 			free_irq(irq->vector, bp->bnapi[i]);
12034 		}
12035 
12036 		irq->requested = 0;
12037 		irq->tag = 0;
12038 		irq->new_tag = 0;
12039 	}
12040 
12041 	/* Disable TPH support */
12042 	pcie_disable_tph(bp->pdev);
12043 	bp->tph_mode = 0;
12044 
12045 #ifdef CONFIG_RFS_ACCEL
12046 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
12047 	bp->dev->rx_cpu_rmap = NULL;
12048 #endif
12049 }
12050 
12051 static int bnxt_request_irq(struct bnxt *bp)
12052 {
12053 	const int numa_node = dev_to_node(&bp->pdev->dev);
12054 	struct cpu_rmap *rmap = NULL;
12055 	int i, j, rc = 0;
12056 	unsigned long flags = 0;
12057 
12058 	rc = bnxt_setup_int_mode(bp);
12059 	if (rc) {
12060 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
12061 			   rc);
12062 		return rc;
12063 	}
12064 #ifdef CONFIG_RFS_ACCEL
12065 	rmap = bp->dev->rx_cpu_rmap;
12066 #endif
12067 
12068 	/* Enable TPH support as part of IRQ request */
12069 	if (BNXT_SUPPORTS_QUEUE_API(bp)) {
12070 		rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE);
12071 		if (!rc)
12072 			bp->tph_mode = PCI_TPH_ST_IV_MODE;
12073 	}
12074 
12075 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
12076 		struct cpumask *cpu_mask = bp->ring_cpu_mask[i];
12077 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
12078 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
12079 		u16 tag;
12080 
12081 		if (IS_ENABLED(CONFIG_RFS_ACCEL) &&
12082 		    rmap && bp->bnapi[i]->rx_ring) {
12083 			rc = irq_cpu_rmap_add(rmap, irq->vector);
12084 			if (rc)
12085 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
12086 					    j);
12087 			j++;
12088 		}
12089 
12090 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
12091 				 bp->bnapi[i]);
12092 		if (rc)
12093 			break;
12094 
12095 		netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector);
12096 		irq->requested = 1;
12097 		irq->msix_nr = map_idx;
12098 		irq->ring_nr = i;
12099 
12100 		/* Reuse the mask recorded before the IRQs were freed. Nothing
12101 		 * was recorded yet on the very first request, and the mask
12102 		 * may have gone stale if the CPUs went offline in between.
12103 		 */
12104 		if (!test_bit(i, bp->ring_affinity_set) ||
12105 		    !cpumask_intersects(cpu_mask, cpu_online_mask)) {
12106 			clear_bit(i, bp->ring_affinity_set);
12107 			cpumask_clear(cpu_mask);
12108 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
12109 					cpu_mask);
12110 		}
12111 
12112 		/* Init ST table entry if we can get the mapping */
12113 		if (!pcie_tph_get_cpu_st(bp->pdev, TPH_MEM_TYPE_VM,
12114 					 cpumask_first(cpu_mask), &tag)) {
12115 			pcie_tph_set_st_entry(bp->pdev, irq->msix_nr, tag);
12116 			irq->tag = tag;
12117 			irq->new_tag = tag;
12118 		}
12119 
12120 		bnxt_register_irq_notifier(bp, irq);
12121 
12122 		/* Only put the IRQ back where it was configured to be, our own
12123 		 * placement is just a hint, the core spreads within
12124 		 * irq_default_affinity which we know nothing about.
12125 		 * Set after installing the notifier, if we race with the user
12126 		 * it's better to overwrite than miss the notification.
12127 		 */
12128 		if (test_bit(i, bp->ring_affinity_set))
12129 			rc = irq_set_affinity_and_hint(irq->vector, cpu_mask);
12130 		else
12131 			rc = irq_update_affinity_hint(irq->vector, cpu_mask);
12132 		if (rc) {
12133 			netdev_warn(bp->dev,
12134 				    "Setting IRQ affinity failed, IRQ = %d\n",
12135 				    irq->vector);
12136 			break;
12137 		}
12138 	}
12139 	return rc;
12140 }
12141 
12142 static void bnxt_del_napi(struct bnxt *bp)
12143 {
12144 	int i;
12145 
12146 	if (!bp->bnapi)
12147 		return;
12148 
12149 	for (i = 0; i < bp->rx_nr_rings; i++)
12150 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
12151 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
12152 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
12153 
12154 	for (i = 0; i < bp->cp_nr_rings; i++) {
12155 		struct bnxt_napi *bnapi = bp->bnapi[i];
12156 
12157 		__netif_napi_del_locked(&bnapi->napi);
12158 	}
12159 	/* We called __netif_napi_del_locked(), we need
12160 	 * to respect an RCU grace period before freeing napi structures.
12161 	 */
12162 	synchronize_net();
12163 }
12164 
12165 static void bnxt_init_napi(struct bnxt *bp)
12166 {
12167 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
12168 	unsigned int cp_nr_rings = bp->cp_nr_rings;
12169 	struct bnxt_napi *bnapi;
12170 	int i;
12171 
12172 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
12173 		poll_fn = bnxt_poll_p5;
12174 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
12175 		cp_nr_rings--;
12176 
12177 	set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
12178 
12179 	for (i = 0; i < cp_nr_rings; i++) {
12180 		bnapi = bp->bnapi[i];
12181 		netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn,
12182 					     bnapi->index);
12183 	}
12184 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
12185 		bnapi = bp->bnapi[cp_nr_rings];
12186 		netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
12187 	}
12188 }
12189 
12190 static void bnxt_disable_napi(struct bnxt *bp)
12191 {
12192 	int i;
12193 
12194 	if (!bp->bnapi ||
12195 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
12196 		return;
12197 
12198 	for (i = 0; i < bp->cp_nr_rings; i++) {
12199 		struct bnxt_napi *bnapi = bp->bnapi[i];
12200 		struct bnxt_cp_ring_info *cpr;
12201 
12202 		cpr = &bnapi->cp_ring;
12203 		if (bnapi->tx_fault)
12204 			cpr->sw_stats->tx.tx_resets++;
12205 		if (bnapi->in_reset)
12206 			cpr->sw_stats->rx.rx_resets++;
12207 		napi_disable_locked(&bnapi->napi);
12208 	}
12209 }
12210 
12211 static void bnxt_enable_napi(struct bnxt *bp)
12212 {
12213 	int i;
12214 
12215 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
12216 	for (i = 0; i < bp->cp_nr_rings; i++) {
12217 		struct bnxt_napi *bnapi = bp->bnapi[i];
12218 		struct bnxt_cp_ring_info *cpr;
12219 
12220 		bnapi->tx_fault = 0;
12221 
12222 		cpr = &bnapi->cp_ring;
12223 		bnapi->in_reset = false;
12224 
12225 		if (bnapi->rx_ring) {
12226 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
12227 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
12228 		}
12229 		napi_enable_locked(&bnapi->napi);
12230 	}
12231 }
12232 
12233 void bnxt_tx_disable(struct bnxt *bp)
12234 {
12235 	int i;
12236 	struct bnxt_tx_ring_info *txr;
12237 
12238 	if (bp->tx_ring) {
12239 		for (i = 0; i < bp->tx_nr_rings; i++) {
12240 			txr = &bp->tx_ring[i];
12241 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
12242 		}
12243 	}
12244 	/* Make sure napi polls see @dev_state change */
12245 	synchronize_net();
12246 	/* Drop carrier first to prevent TX timeout */
12247 	netif_carrier_off(bp->dev);
12248 	/* Stop all TX queues */
12249 	netif_tx_disable(bp->dev);
12250 }
12251 
12252 void bnxt_tx_enable(struct bnxt *bp)
12253 {
12254 	int i;
12255 	struct bnxt_tx_ring_info *txr;
12256 
12257 	for (i = 0; i < bp->tx_nr_rings; i++) {
12258 		txr = &bp->tx_ring[i];
12259 		WRITE_ONCE(txr->dev_state, 0);
12260 	}
12261 	/* Make sure napi polls see @dev_state change */
12262 	synchronize_net();
12263 	netif_tx_wake_all_queues(bp->dev);
12264 	if (BNXT_LINK_IS_UP(bp))
12265 		netif_carrier_on(bp->dev);
12266 }
12267 
12268 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
12269 {
12270 	u8 active_fec = link_info->active_fec_sig_mode &
12271 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
12272 
12273 	switch (active_fec) {
12274 	default:
12275 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
12276 		return "None";
12277 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
12278 		return "Clause 74 BaseR";
12279 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
12280 		return "Clause 91 RS(528,514)";
12281 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
12282 		return "Clause 91 RS544_1XN";
12283 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
12284 		return "Clause 91 RS(544,514)";
12285 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
12286 		return "Clause 91 RS272_1XN";
12287 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
12288 		return "Clause 91 RS(272,257)";
12289 	}
12290 }
12291 
12292 static char *bnxt_link_down_reason(struct bnxt_link_info *link_info)
12293 {
12294 	u8 reason = link_info->link_down_reason;
12295 
12296 	/* Multiple bits can be set, we report 1 bit only in order of
12297 	 * priority.
12298 	 */
12299 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_RF)
12300 		return "(Remote fault)";
12301 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_OTP_SPEED_VIOLATION)
12302 		return "(OTP Speed limit violation)";
12303 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_CABLE_REMOVED)
12304 		return "(Cable removed)";
12305 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_MODULE_FAULT)
12306 		return "(Module fault)";
12307 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_BMC_REQUEST)
12308 		return "(BMC request down)";
12309 	return "";
12310 }
12311 
12312 void bnxt_report_link(struct bnxt *bp)
12313 {
12314 	if (BNXT_LINK_IS_UP(bp)) {
12315 		const char *signal = "";
12316 		const char *flow_ctrl;
12317 		const char *duplex;
12318 		u32 speed;
12319 		u16 fec;
12320 
12321 		netif_carrier_on(bp->dev);
12322 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
12323 		if (speed == SPEED_UNKNOWN) {
12324 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
12325 			return;
12326 		}
12327 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
12328 			duplex = "full";
12329 		else
12330 			duplex = "half";
12331 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
12332 			flow_ctrl = "ON - receive & transmit";
12333 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
12334 			flow_ctrl = "ON - transmit";
12335 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
12336 			flow_ctrl = "ON - receive";
12337 		else
12338 			flow_ctrl = "none";
12339 		if (bp->link_info.phy_qcfg_resp.option_flags &
12340 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
12341 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
12342 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
12343 			switch (sig_mode) {
12344 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
12345 				signal = "(NRZ) ";
12346 				break;
12347 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
12348 				signal = "(PAM4 56Gbps) ";
12349 				break;
12350 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
12351 				signal = "(PAM4 112Gbps) ";
12352 				break;
12353 			default:
12354 				break;
12355 			}
12356 		}
12357 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
12358 			    speed, signal, duplex, flow_ctrl);
12359 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
12360 			netdev_info(bp->dev, "EEE is %s\n",
12361 				    bp->eee.eee_active ? "active" :
12362 							 "not active");
12363 		fec = bp->link_info.fec_cfg;
12364 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
12365 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
12366 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
12367 				    bnxt_report_fec(&bp->link_info));
12368 	} else {
12369 		char *str = bnxt_link_down_reason(&bp->link_info);
12370 
12371 		netif_carrier_off(bp->dev);
12372 		netdev_err(bp->dev, "NIC Link is Down %s\n", str);
12373 	}
12374 }
12375 
12376 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
12377 {
12378 	if (!resp->supported_speeds_auto_mode &&
12379 	    !resp->supported_speeds_force_mode &&
12380 	    !resp->supported_pam4_speeds_auto_mode &&
12381 	    !resp->supported_pam4_speeds_force_mode &&
12382 	    !resp->supported_speeds2_auto_mode &&
12383 	    !resp->supported_speeds2_force_mode)
12384 		return true;
12385 	return false;
12386 }
12387 
12388 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
12389 {
12390 	struct bnxt_link_info *link_info = &bp->link_info;
12391 	struct hwrm_port_phy_qcaps_output *resp;
12392 	struct hwrm_port_phy_qcaps_input *req;
12393 	int rc = 0;
12394 
12395 	if (bp->hwrm_spec_code < 0x10201)
12396 		return 0;
12397 
12398 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
12399 	if (rc)
12400 		return rc;
12401 
12402 	resp = hwrm_req_hold(bp, req);
12403 	rc = hwrm_req_send(bp, req);
12404 	if (rc)
12405 		goto hwrm_phy_qcaps_exit;
12406 
12407 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
12408 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
12409 		struct ethtool_keee *eee = &bp->eee;
12410 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
12411 
12412 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
12413 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
12414 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
12415 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
12416 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
12417 	}
12418 
12419 	if (bp->hwrm_spec_code >= 0x10a01) {
12420 		if (bnxt_phy_qcaps_no_speed(resp)) {
12421 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
12422 			netdev_warn(bp->dev, "Ethernet link disabled\n");
12423 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
12424 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
12425 			netdev_info(bp->dev, "Ethernet link enabled\n");
12426 			/* Phy re-enabled, reprobe the speeds */
12427 			link_info->support_auto_speeds = 0;
12428 			link_info->support_pam4_auto_speeds = 0;
12429 			link_info->support_auto_speeds2 = 0;
12430 		}
12431 	}
12432 	if (resp->supported_speeds_auto_mode)
12433 		link_info->support_auto_speeds =
12434 			le16_to_cpu(resp->supported_speeds_auto_mode);
12435 	if (resp->supported_pam4_speeds_auto_mode)
12436 		link_info->support_pam4_auto_speeds =
12437 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
12438 	if (resp->supported_speeds2_auto_mode)
12439 		link_info->support_auto_speeds2 =
12440 			le16_to_cpu(resp->supported_speeds2_auto_mode);
12441 
12442 	bp->port_count = resp->port_cnt;
12443 
12444 hwrm_phy_qcaps_exit:
12445 	hwrm_req_drop(bp, req);
12446 	return rc;
12447 }
12448 
12449 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
12450 {
12451 	struct hwrm_port_mac_qcaps_output *resp;
12452 	struct hwrm_port_mac_qcaps_input *req;
12453 	int rc;
12454 
12455 	if (bp->hwrm_spec_code < 0x10a03)
12456 		return;
12457 
12458 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
12459 	if (rc)
12460 		return;
12461 
12462 	resp = hwrm_req_hold(bp, req);
12463 	rc = hwrm_req_send_silent(bp, req);
12464 	if (!rc)
12465 		bp->mac_flags = resp->flags;
12466 	hwrm_req_drop(bp, req);
12467 }
12468 
12469 static bool bnxt_support_dropped(u16 advertising, u16 supported)
12470 {
12471 	u16 diff = advertising ^ supported;
12472 
12473 	return ((supported | diff) != supported);
12474 }
12475 
12476 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
12477 {
12478 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
12479 
12480 	/* Check if any advertised speeds are no longer supported. The caller
12481 	 * holds the link_lock mutex, so we can modify link_info settings.
12482 	 */
12483 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12484 		if (bnxt_support_dropped(link_info->advertising,
12485 					 link_info->support_auto_speeds2)) {
12486 			link_info->advertising = link_info->support_auto_speeds2;
12487 			return true;
12488 		}
12489 		return false;
12490 	}
12491 	if (bnxt_support_dropped(link_info->advertising,
12492 				 link_info->support_auto_speeds)) {
12493 		link_info->advertising = link_info->support_auto_speeds;
12494 		return true;
12495 	}
12496 	if (bnxt_support_dropped(link_info->advertising_pam4,
12497 				 link_info->support_pam4_auto_speeds)) {
12498 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
12499 		return true;
12500 	}
12501 	return false;
12502 }
12503 
12504 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
12505 {
12506 	struct bnxt_link_info *link_info = &bp->link_info;
12507 	struct hwrm_port_phy_qcfg_output *resp;
12508 	struct hwrm_port_phy_qcfg_input *req;
12509 	u8 link_state = link_info->link_state;
12510 	bool support_changed;
12511 	int rc;
12512 
12513 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
12514 	if (rc)
12515 		return rc;
12516 
12517 	resp = hwrm_req_hold(bp, req);
12518 	rc = hwrm_req_send(bp, req);
12519 	if (rc) {
12520 		hwrm_req_drop(bp, req);
12521 		if (rc == -ECOMM) {
12522 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
12523 			rc = 0;
12524 		}
12525 		return rc;
12526 	}
12527 
12528 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
12529 	link_info->phy_link_status = resp->link;
12530 	link_info->duplex = resp->duplex_cfg;
12531 	if (bp->hwrm_spec_code >= 0x10800)
12532 		link_info->duplex = resp->duplex_state;
12533 	link_info->pause = resp->pause;
12534 	link_info->auto_mode = resp->auto_mode;
12535 	link_info->auto_pause_setting = resp->auto_pause;
12536 	link_info->lp_pause = resp->link_partner_adv_pause;
12537 	link_info->force_pause_setting = resp->force_pause;
12538 	link_info->duplex_setting = resp->duplex_cfg;
12539 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
12540 		link_info->link_speed = le16_to_cpu(resp->link_speed);
12541 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
12542 			link_info->active_lanes = resp->active_lanes;
12543 	} else {
12544 		link_info->link_speed = 0;
12545 		link_info->active_lanes = 0;
12546 	}
12547 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
12548 	link_info->force_pam4_link_speed =
12549 		le16_to_cpu(resp->force_pam4_link_speed);
12550 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
12551 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
12552 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
12553 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
12554 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
12555 	link_info->auto_pam4_link_speeds =
12556 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
12557 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
12558 	link_info->lp_auto_link_speeds =
12559 		le16_to_cpu(resp->link_partner_adv_speeds);
12560 	link_info->lp_auto_pam4_link_speeds =
12561 		resp->link_partner_pam4_adv_speeds;
12562 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
12563 	link_info->phy_ver[0] = resp->phy_maj;
12564 	link_info->phy_ver[1] = resp->phy_min;
12565 	link_info->phy_ver[2] = resp->phy_bld;
12566 	link_info->media_type = resp->media_type;
12567 	link_info->phy_type = resp->phy_type;
12568 	link_info->transceiver = resp->xcvr_pkg_type;
12569 	link_info->phy_addr = resp->eee_config_phy_addr &
12570 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
12571 	link_info->module_status = resp->module_status;
12572 	link_info->link_down_reason = resp->link_down_reason;
12573 
12574 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
12575 		struct ethtool_keee *eee = &bp->eee;
12576 		u16 fw_speeds;
12577 
12578 		eee->eee_active = 0;
12579 		if (resp->eee_config_phy_addr &
12580 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
12581 			eee->eee_active = 1;
12582 			fw_speeds = le16_to_cpu(
12583 				resp->link_partner_adv_eee_link_speed_mask);
12584 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
12585 		}
12586 
12587 		/* Pull initial EEE config */
12588 		if (!chng_link_state) {
12589 			if (resp->eee_config_phy_addr &
12590 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
12591 				eee->eee_enabled = 1;
12592 
12593 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
12594 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
12595 
12596 			if (resp->eee_config_phy_addr &
12597 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
12598 				__le32 tmr;
12599 
12600 				eee->tx_lpi_enabled = 1;
12601 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
12602 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
12603 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
12604 			}
12605 		}
12606 	}
12607 
12608 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
12609 	if (bp->hwrm_spec_code >= 0x10504) {
12610 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
12611 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
12612 	}
12613 	/* TODO: need to add more logic to report VF link */
12614 	if (chng_link_state) {
12615 		if (link_info->phy_link_status == BNXT_LINK_LINK)
12616 			link_info->link_state = BNXT_LINK_STATE_UP;
12617 		else
12618 			link_info->link_state = BNXT_LINK_STATE_DOWN;
12619 		if (link_state != link_info->link_state)
12620 			bnxt_report_link(bp);
12621 	} else {
12622 		/* always link down if not require to update link state */
12623 		link_info->link_state = BNXT_LINK_STATE_DOWN;
12624 	}
12625 	hwrm_req_drop(bp, req);
12626 
12627 	if (!BNXT_PHY_CFG_ABLE(bp))
12628 		return 0;
12629 
12630 	support_changed = bnxt_support_speed_dropped(link_info);
12631 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
12632 		bnxt_hwrm_set_link_setting(bp, true, false);
12633 	return 0;
12634 }
12635 
12636 static void bnxt_get_port_module_status(struct bnxt *bp)
12637 {
12638 	struct bnxt_link_info *link_info = &bp->link_info;
12639 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
12640 	u8 module_status;
12641 
12642 	if (bnxt_update_link(bp, true))
12643 		return;
12644 
12645 	module_status = link_info->module_status;
12646 	switch (module_status) {
12647 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
12648 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
12649 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
12650 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
12651 			    bp->pf.port_id);
12652 		if (bp->hwrm_spec_code >= 0x10201) {
12653 			netdev_warn(bp->dev, "Module part number %s\n",
12654 				    resp->phy_vendor_partnumber);
12655 		}
12656 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
12657 			netdev_warn(bp->dev, "TX is disabled\n");
12658 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
12659 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
12660 	}
12661 }
12662 
12663 static void
12664 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12665 {
12666 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
12667 		if (bp->hwrm_spec_code >= 0x10201)
12668 			req->auto_pause =
12669 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
12670 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12671 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
12672 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12673 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
12674 		req->enables |=
12675 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12676 	} else {
12677 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12678 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
12679 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12680 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
12681 		req->enables |=
12682 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
12683 		if (bp->hwrm_spec_code >= 0x10201) {
12684 			req->auto_pause = req->force_pause;
12685 			req->enables |= cpu_to_le32(
12686 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12687 		}
12688 	}
12689 }
12690 
12691 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12692 {
12693 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
12694 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
12695 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12696 			req->enables |=
12697 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
12698 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
12699 		} else if (bp->link_info.advertising) {
12700 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
12701 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
12702 		}
12703 		if (bp->link_info.advertising_pam4) {
12704 			req->enables |=
12705 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
12706 			req->auto_link_pam4_speed_mask =
12707 				cpu_to_le16(bp->link_info.advertising_pam4);
12708 		}
12709 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
12710 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
12711 	} else {
12712 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
12713 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12714 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
12715 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
12716 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
12717 				   (u32)bp->link_info.req_link_speed);
12718 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
12719 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12720 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
12721 		} else {
12722 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12723 		}
12724 	}
12725 
12726 	/* tell chimp that the setting takes effect immediately */
12727 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
12728 }
12729 
12730 int bnxt_hwrm_set_pause(struct bnxt *bp)
12731 {
12732 	struct hwrm_port_phy_cfg_input *req;
12733 	int rc;
12734 
12735 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12736 	if (rc)
12737 		return rc;
12738 
12739 	bnxt_hwrm_set_pause_common(bp, req);
12740 
12741 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
12742 	    bp->link_info.force_link_chng)
12743 		bnxt_hwrm_set_link_common(bp, req);
12744 
12745 	rc = hwrm_req_send(bp, req);
12746 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
12747 		/* since changing of pause setting doesn't trigger any link
12748 		 * change event, the driver needs to update the current pause
12749 		 * result upon successfully return of the phy_cfg command
12750 		 */
12751 		bp->link_info.pause =
12752 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
12753 		bp->link_info.auto_pause_setting = 0;
12754 		if (!bp->link_info.force_link_chng)
12755 			bnxt_report_link(bp);
12756 	}
12757 	bp->link_info.force_link_chng = false;
12758 	return rc;
12759 }
12760 
12761 static void bnxt_hwrm_set_eee(struct bnxt *bp,
12762 			      struct hwrm_port_phy_cfg_input *req)
12763 {
12764 	struct ethtool_keee *eee = &bp->eee;
12765 
12766 	if (eee->eee_enabled) {
12767 		u16 eee_speeds;
12768 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
12769 
12770 		if (eee->tx_lpi_enabled)
12771 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
12772 		else
12773 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
12774 
12775 		req->flags |= cpu_to_le32(flags);
12776 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
12777 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
12778 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
12779 	} else {
12780 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
12781 	}
12782 }
12783 
12784 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
12785 {
12786 	struct hwrm_port_phy_cfg_input *req;
12787 	int rc;
12788 
12789 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12790 	if (rc)
12791 		return rc;
12792 
12793 	if (set_pause)
12794 		bnxt_hwrm_set_pause_common(bp, req);
12795 
12796 	bnxt_hwrm_set_link_common(bp, req);
12797 
12798 	if (set_eee)
12799 		bnxt_hwrm_set_eee(bp, req);
12800 	return hwrm_req_send(bp, req);
12801 }
12802 
12803 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
12804 {
12805 	struct hwrm_port_phy_cfg_input *req;
12806 	int rc;
12807 
12808 	if (!BNXT_SINGLE_PF(bp))
12809 		return 0;
12810 
12811 	if (pci_num_vf(bp->pdev) &&
12812 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
12813 		return 0;
12814 
12815 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12816 	if (rc)
12817 		return rc;
12818 
12819 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
12820 	rc = hwrm_req_send(bp, req);
12821 	if (!rc) {
12822 		mutex_lock(&bp->link_lock);
12823 		/* Device is not obliged link down in certain scenarios, even
12824 		 * when forced. Setting the state unknown is consistent with
12825 		 * driver startup and will force link state to be reported
12826 		 * during subsequent open based on PORT_PHY_QCFG.
12827 		 */
12828 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
12829 		mutex_unlock(&bp->link_lock);
12830 	}
12831 	return rc;
12832 }
12833 
12834 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
12835 {
12836 #ifdef CONFIG_TEE_BNXT_FW
12837 	int rc = tee_bnxt_fw_load();
12838 
12839 	if (rc)
12840 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
12841 
12842 	return rc;
12843 #else
12844 	netdev_err(bp->dev, "OP-TEE not supported\n");
12845 	return -ENODEV;
12846 #endif
12847 }
12848 
12849 static int bnxt_try_recover_fw(struct bnxt *bp)
12850 {
12851 	if (bp->fw_health && bp->fw_health->status_reliable) {
12852 		int retry = 0, rc;
12853 		u32 sts;
12854 
12855 		do {
12856 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12857 			rc = bnxt_hwrm_poll(bp);
12858 			if (!BNXT_FW_IS_BOOTING(sts) &&
12859 			    !BNXT_FW_IS_RECOVERING(sts))
12860 				break;
12861 			retry++;
12862 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
12863 
12864 		if (!BNXT_FW_IS_HEALTHY(sts)) {
12865 			netdev_err(bp->dev,
12866 				   "Firmware not responding, status: 0x%x\n",
12867 				   sts);
12868 			rc = -ENODEV;
12869 		}
12870 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
12871 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
12872 			return bnxt_fw_reset_via_optee(bp);
12873 		}
12874 		return rc;
12875 	}
12876 
12877 	return -ENODEV;
12878 }
12879 
12880 void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
12881 {
12882 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12883 
12884 	if (!BNXT_NEW_RM(bp))
12885 		return; /* no resource reservations required */
12886 
12887 	hw_resc->resv_cp_rings = 0;
12888 	hw_resc->resv_stat_ctxs = 0;
12889 	hw_resc->resv_irqs = 0;
12890 	hw_resc->resv_tx_rings = 0;
12891 	hw_resc->resv_rx_rings = 0;
12892 	hw_resc->resv_hw_ring_grps = 0;
12893 	hw_resc->resv_vnics = 0;
12894 	hw_resc->resv_rsscos_ctxs = 0;
12895 	if (!fw_reset) {
12896 		bp->tx_nr_rings = 0;
12897 		bp->rx_nr_rings = 0;
12898 	}
12899 }
12900 
12901 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12902 {
12903 	int rc;
12904 
12905 	if (!BNXT_NEW_RM(bp))
12906 		return 0; /* no resource reservations required */
12907 
12908 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12909 	if (rc)
12910 		netdev_err(bp->dev, "resc_qcaps failed\n");
12911 
12912 	bnxt_clear_reservations(bp, fw_reset);
12913 
12914 	return rc;
12915 }
12916 
12917 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12918 {
12919 	struct hwrm_func_drv_if_change_output *resp;
12920 	struct hwrm_func_drv_if_change_input *req;
12921 	bool resc_reinit = false;
12922 	bool caps_change = false;
12923 	int rc, retry = 0;
12924 	bool fw_reset;
12925 	u32 flags = 0;
12926 
12927 	fw_reset = (bp->fw_reset_state == BNXT_FW_RESET_STATE_ABORT);
12928 	bp->fw_reset_state = 0;
12929 
12930 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12931 		return 0;
12932 
12933 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12934 	if (rc)
12935 		return rc;
12936 
12937 	if (up)
12938 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12939 	resp = hwrm_req_hold(bp, req);
12940 
12941 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12942 	while (retry < BNXT_FW_IF_RETRY) {
12943 		rc = hwrm_req_send(bp, req);
12944 		if (rc != -EAGAIN)
12945 			break;
12946 
12947 		msleep(50);
12948 		retry++;
12949 	}
12950 
12951 	if (rc == -EAGAIN) {
12952 		hwrm_req_drop(bp, req);
12953 		return rc;
12954 	} else if (!rc) {
12955 		flags = le32_to_cpu(resp->flags);
12956 	} else if (up) {
12957 		rc = bnxt_try_recover_fw(bp);
12958 		fw_reset = true;
12959 	}
12960 	hwrm_req_drop(bp, req);
12961 	if (rc)
12962 		return rc;
12963 
12964 	if (!up) {
12965 		bnxt_inv_fw_health_reg(bp);
12966 		return 0;
12967 	}
12968 
12969 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12970 		resc_reinit = true;
12971 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12972 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12973 		fw_reset = true;
12974 	else
12975 		bnxt_remap_fw_health_regs(bp);
12976 
12977 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12978 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12979 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12980 		return -ENODEV;
12981 	}
12982 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE)
12983 		caps_change = true;
12984 
12985 	if (resc_reinit || fw_reset || caps_change) {
12986 		if (fw_reset || caps_change) {
12987 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12988 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12989 				bnxt_ulp_irq_stop(bp);
12990 			bnxt_free_ctx_mem(bp, false);
12991 			bnxt_dcb_free(bp);
12992 			rc = bnxt_fw_init_one(bp);
12993 			if (rc) {
12994 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12995 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12996 				return rc;
12997 			}
12998 			/* IRQ will be initialized later in bnxt_request_irq()*/
12999 			bnxt_clear_int_mode(bp);
13000 		}
13001 		rc = bnxt_cancel_reservations(bp, fw_reset);
13002 	}
13003 	return rc;
13004 }
13005 
13006 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
13007 {
13008 	struct hwrm_port_led_qcaps_output *resp;
13009 	struct hwrm_port_led_qcaps_input *req;
13010 	struct bnxt_pf_info *pf = &bp->pf;
13011 	int rc;
13012 
13013 	bp->num_leds = 0;
13014 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
13015 		return 0;
13016 
13017 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
13018 	if (rc)
13019 		return rc;
13020 
13021 	req->port_id = cpu_to_le16(pf->port_id);
13022 	resp = hwrm_req_hold(bp, req);
13023 	rc = hwrm_req_send(bp, req);
13024 	if (rc) {
13025 		hwrm_req_drop(bp, req);
13026 		return rc;
13027 	}
13028 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
13029 		int i;
13030 
13031 		bp->num_leds = resp->num_leds;
13032 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
13033 						 bp->num_leds);
13034 		for (i = 0; i < bp->num_leds; i++) {
13035 			struct bnxt_led_info *led = &bp->leds[i];
13036 			__le16 caps = led->led_state_caps;
13037 
13038 			if (!led->led_group_id ||
13039 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
13040 				bp->num_leds = 0;
13041 				break;
13042 			}
13043 		}
13044 	}
13045 	hwrm_req_drop(bp, req);
13046 	return 0;
13047 }
13048 
13049 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
13050 {
13051 	struct hwrm_wol_filter_alloc_output *resp;
13052 	struct hwrm_wol_filter_alloc_input *req;
13053 	int rc;
13054 
13055 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
13056 	if (rc)
13057 		return rc;
13058 
13059 	req->port_id = cpu_to_le16(bp->pf.port_id);
13060 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
13061 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
13062 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
13063 
13064 	resp = hwrm_req_hold(bp, req);
13065 	rc = hwrm_req_send(bp, req);
13066 	if (!rc)
13067 		bp->wol_filter_id = resp->wol_filter_id;
13068 	hwrm_req_drop(bp, req);
13069 	return rc;
13070 }
13071 
13072 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
13073 {
13074 	struct hwrm_wol_filter_free_input *req;
13075 	int rc;
13076 
13077 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
13078 	if (rc)
13079 		return rc;
13080 
13081 	req->port_id = cpu_to_le16(bp->pf.port_id);
13082 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
13083 	req->wol_filter_id = bp->wol_filter_id;
13084 
13085 	return hwrm_req_send(bp, req);
13086 }
13087 
13088 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
13089 {
13090 	struct hwrm_wol_filter_qcfg_output *resp;
13091 	struct hwrm_wol_filter_qcfg_input *req;
13092 	u16 next_handle = 0;
13093 	int rc;
13094 
13095 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
13096 	if (rc)
13097 		return rc;
13098 
13099 	req->port_id = cpu_to_le16(bp->pf.port_id);
13100 	req->handle = cpu_to_le16(handle);
13101 	resp = hwrm_req_hold(bp, req);
13102 	rc = hwrm_req_send(bp, req);
13103 	if (!rc) {
13104 		next_handle = le16_to_cpu(resp->next_handle);
13105 		if (next_handle != 0) {
13106 			if (resp->wol_type ==
13107 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
13108 				bp->wol = 1;
13109 				bp->wol_filter_id = resp->wol_filter_id;
13110 			}
13111 		}
13112 	}
13113 	hwrm_req_drop(bp, req);
13114 	return next_handle;
13115 }
13116 
13117 static void bnxt_get_wol_settings(struct bnxt *bp)
13118 {
13119 	u16 handle = 0;
13120 
13121 	bp->wol = 0;
13122 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
13123 		return;
13124 
13125 	do {
13126 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
13127 	} while (handle && handle != 0xffff);
13128 }
13129 
13130 static bool bnxt_eee_config_ok(struct bnxt *bp)
13131 {
13132 	struct ethtool_keee *eee = &bp->eee;
13133 	struct bnxt_link_info *link_info = &bp->link_info;
13134 
13135 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
13136 		return true;
13137 
13138 	if (eee->eee_enabled) {
13139 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
13140 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
13141 
13142 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
13143 
13144 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
13145 			eee->eee_enabled = 0;
13146 			return false;
13147 		}
13148 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
13149 			linkmode_and(eee->advertised, advertising,
13150 				     eee->supported);
13151 			return false;
13152 		}
13153 	}
13154 	return true;
13155 }
13156 
13157 static int bnxt_update_phy_setting(struct bnxt *bp)
13158 {
13159 	int rc;
13160 	bool update_link = false;
13161 	bool update_pause = false;
13162 	bool update_eee = false;
13163 	struct bnxt_link_info *link_info = &bp->link_info;
13164 
13165 	rc = bnxt_update_link(bp, true);
13166 	if (rc) {
13167 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
13168 			   rc);
13169 		return rc;
13170 	}
13171 	if (!BNXT_SINGLE_PF(bp))
13172 		return 0;
13173 
13174 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
13175 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
13176 	    link_info->req_flow_ctrl)
13177 		update_pause = true;
13178 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
13179 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
13180 		update_pause = true;
13181 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
13182 		if (BNXT_AUTO_MODE(link_info->auto_mode))
13183 			update_link = true;
13184 		if (bnxt_force_speed_updated(link_info))
13185 			update_link = true;
13186 		if (link_info->req_duplex != link_info->duplex_setting)
13187 			update_link = true;
13188 	} else {
13189 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
13190 			update_link = true;
13191 		if (bnxt_auto_speed_updated(link_info))
13192 			update_link = true;
13193 	}
13194 
13195 	/* The last close may have shutdown the link, so need to call
13196 	 * PHY_CFG to bring it back up.
13197 	 */
13198 	if (!BNXT_LINK_IS_UP(bp))
13199 		update_link = true;
13200 
13201 	if (!bnxt_eee_config_ok(bp))
13202 		update_eee = true;
13203 
13204 	if (update_link)
13205 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
13206 	else if (update_pause)
13207 		rc = bnxt_hwrm_set_pause(bp);
13208 	if (rc) {
13209 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
13210 			   rc);
13211 		return rc;
13212 	}
13213 
13214 	return rc;
13215 }
13216 
13217 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
13218 
13219 static int bnxt_reinit_after_abort(struct bnxt *bp)
13220 {
13221 	int rc;
13222 
13223 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13224 		return -EBUSY;
13225 
13226 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
13227 		return -ENODEV;
13228 
13229 	rc = bnxt_fw_init_one(bp);
13230 	if (!rc) {
13231 		bnxt_clear_int_mode(bp);
13232 		rc = bnxt_init_int_mode(bp);
13233 		if (!rc) {
13234 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13235 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
13236 		}
13237 	}
13238 	return rc;
13239 }
13240 
13241 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
13242 {
13243 	struct bnxt_ntuple_filter *ntp_fltr;
13244 	struct bnxt_l2_filter *l2_fltr;
13245 
13246 	if (list_empty(&fltr->list))
13247 		return;
13248 
13249 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
13250 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
13251 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
13252 		atomic_inc(&l2_fltr->refcnt);
13253 		ntp_fltr->l2_fltr = l2_fltr;
13254 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
13255 			bnxt_del_ntp_filter(bp, ntp_fltr);
13256 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
13257 				   fltr->sw_id);
13258 		}
13259 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
13260 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
13261 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
13262 			bnxt_del_l2_filter(bp, l2_fltr);
13263 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
13264 				   fltr->sw_id);
13265 		}
13266 	}
13267 }
13268 
13269 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
13270 {
13271 	struct bnxt_filter_base *usr_fltr, *tmp;
13272 
13273 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
13274 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
13275 }
13276 
13277 static int bnxt_set_xps_mapping(struct bnxt *bp)
13278 {
13279 	int numa_node = dev_to_node(&bp->pdev->dev);
13280 	unsigned int q_idx, map_idx, cpu, i;
13281 	const struct cpumask *cpu_mask_ptr;
13282 	int nr_cpus = num_online_cpus();
13283 	cpumask_t *q_map;
13284 	int rc = 0;
13285 
13286 	q_map = kzalloc_objs(*q_map, bp->tx_nr_rings_per_tc);
13287 	if (!q_map)
13288 		return -ENOMEM;
13289 
13290 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
13291 	 * Each TC has the same number of TX queues. The nth TX queue for each
13292 	 * TC will have the same CPU mask.
13293 	 */
13294 	for (i = 0; i < nr_cpus; i++) {
13295 		map_idx = i % bp->tx_nr_rings_per_tc;
13296 		cpu = cpumask_local_spread(i, numa_node);
13297 		cpu_mask_ptr = get_cpu_mask(cpu);
13298 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
13299 	}
13300 
13301 	/* Register CPU mask for each TX queue except the ones marked for XDP */
13302 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
13303 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
13304 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
13305 		if (rc) {
13306 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
13307 				    q_idx);
13308 			break;
13309 		}
13310 	}
13311 
13312 	kfree(q_map);
13313 
13314 	return rc;
13315 }
13316 
13317 static int bnxt_tx_nr_rings(struct bnxt *bp)
13318 {
13319 	return bp->num_tc ? bp->tx_nr_rings_per_tc * bp->num_tc :
13320 			    bp->tx_nr_rings_per_tc;
13321 }
13322 
13323 static int bnxt_tx_nr_rings_per_tc(struct bnxt *bp)
13324 {
13325 	return bp->num_tc ? bp->tx_nr_rings / bp->num_tc : bp->tx_nr_rings;
13326 }
13327 
13328 static void bnxt_set_xdp_tx_rings(struct bnxt *bp)
13329 {
13330 	bp->tx_nr_rings_xdp = bp->tx_nr_rings_per_tc;
13331 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
13332 }
13333 
13334 static void bnxt_adj_tx_rings(struct bnxt *bp)
13335 {
13336 	/* Make adjustments if reserved TX rings are less than requested */
13337 	bp->tx_nr_rings -= bp->tx_nr_rings_xdp;
13338 	bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp);
13339 	if (bp->tx_nr_rings_xdp)
13340 		bnxt_set_xdp_tx_rings(bp);
13341 }
13342 
13343 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13344 {
13345 	int rc = 0;
13346 
13347 	netif_carrier_off(bp->dev);
13348 	if (irq_re_init) {
13349 		/* Reserve rings now if none were reserved at driver probe. */
13350 		rc = bnxt_init_dflt_ring_mode(bp);
13351 		if (rc) {
13352 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
13353 			return rc;
13354 		}
13355 	}
13356 	rc = bnxt_reserve_rings(bp, irq_re_init);
13357 	if (rc)
13358 		return rc;
13359 
13360 	bnxt_adj_tx_rings(bp);
13361 	rc = bnxt_alloc_mem(bp, irq_re_init);
13362 	if (rc) {
13363 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
13364 		goto open_err_free_mem;
13365 	}
13366 
13367 	if (irq_re_init) {
13368 		bnxt_init_napi(bp);
13369 		rc = bnxt_request_irq(bp);
13370 		if (rc) {
13371 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
13372 			goto open_err_irq;
13373 		}
13374 	}
13375 
13376 	rc = bnxt_init_nic(bp, irq_re_init);
13377 	if (rc) {
13378 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
13379 		goto open_err_irq;
13380 	}
13381 
13382 	bnxt_enable_napi(bp);
13383 	bnxt_debug_dev_init(bp);
13384 
13385 	if (link_re_init) {
13386 		mutex_lock(&bp->link_lock);
13387 		rc = bnxt_update_phy_setting(bp);
13388 		mutex_unlock(&bp->link_lock);
13389 		if (rc) {
13390 			netdev_warn(bp->dev, "failed to update phy settings\n");
13391 			if (BNXT_SINGLE_PF(bp)) {
13392 				bp->link_info.phy_retry = true;
13393 				bp->link_info.phy_retry_expires =
13394 					jiffies + 5 * HZ;
13395 			}
13396 		}
13397 	}
13398 
13399 	if (irq_re_init) {
13400 		udp_tunnel_nic_reset_ntf(bp->dev);
13401 		rc = bnxt_set_xps_mapping(bp);
13402 		if (rc)
13403 			netdev_warn(bp->dev, "failed to set xps mapping\n");
13404 	}
13405 
13406 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
13407 		if (!static_key_enabled(&bnxt_xdp_locking_key))
13408 			static_branch_enable(&bnxt_xdp_locking_key);
13409 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
13410 		static_branch_disable(&bnxt_xdp_locking_key);
13411 	}
13412 	set_bit(BNXT_STATE_OPEN, &bp->state);
13413 	bnxt_enable_int(bp);
13414 	/* Enable TX queues */
13415 	bnxt_tx_enable(bp);
13416 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13417 	/* Poll link status and check for SFP+ module status */
13418 	mutex_lock(&bp->link_lock);
13419 	bnxt_get_port_module_status(bp);
13420 	mutex_unlock(&bp->link_lock);
13421 
13422 	/* VF-reps may need to be re-opened after the PF is re-opened */
13423 	if (BNXT_PF(bp))
13424 		bnxt_vf_reps_open(bp);
13425 	bnxt_ptp_init_rtc(bp, true);
13426 	bnxt_ptp_cfg_tstamp_filters(bp);
13427 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13428 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
13429 	bnxt_cfg_usr_fltrs(bp);
13430 	return 0;
13431 
13432 open_err_irq:
13433 	bnxt_del_napi(bp);
13434 
13435 open_err_free_mem:
13436 	bnxt_free_skbs(bp);
13437 	bnxt_free_irq(bp);
13438 	bnxt_free_mem(bp, true);
13439 	return rc;
13440 }
13441 
13442 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13443 {
13444 	int rc = 0;
13445 
13446 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
13447 		rc = -EIO;
13448 	if (!rc)
13449 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
13450 	if (rc) {
13451 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
13452 		netif_close(bp->dev);
13453 	}
13454 	return rc;
13455 }
13456 
13457 /* netdev instance lock held, open the NIC half way by allocating all
13458  * resources, but NAPI, IRQ, and TX are not enabled.  This is mainly used
13459  * for offline self tests.
13460  */
13461 int bnxt_half_open_nic(struct bnxt *bp)
13462 {
13463 	int rc = 0;
13464 
13465 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13466 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
13467 		rc = -ENODEV;
13468 		goto half_open_err;
13469 	}
13470 
13471 	rc = bnxt_alloc_mem(bp, true);
13472 	if (rc) {
13473 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
13474 		goto half_open_err;
13475 	}
13476 	bnxt_init_napi(bp);
13477 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13478 	rc = bnxt_init_nic(bp, true);
13479 	if (rc) {
13480 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13481 		bnxt_del_napi(bp);
13482 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
13483 		goto half_open_err;
13484 	}
13485 	return 0;
13486 
13487 half_open_err:
13488 	bnxt_free_skbs(bp);
13489 	bnxt_free_mem(bp, true);
13490 	netif_close(bp->dev);
13491 	return rc;
13492 }
13493 
13494 /* netdev instance lock held, this call can only be made after a previous
13495  * successful call to bnxt_half_open_nic().
13496  */
13497 void bnxt_half_close_nic(struct bnxt *bp)
13498 {
13499 	bnxt_hwrm_resource_free(bp, false, true);
13500 	bnxt_del_napi(bp);
13501 	bnxt_free_skbs(bp);
13502 	bnxt_free_mem(bp, true);
13503 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13504 }
13505 
13506 void bnxt_reenable_sriov(struct bnxt *bp)
13507 {
13508 	if (BNXT_PF(bp)) {
13509 		struct bnxt_pf_info *pf = &bp->pf;
13510 		int n = pf->active_vfs;
13511 
13512 		if (n)
13513 			bnxt_cfg_hw_sriov(bp, &n, true);
13514 	}
13515 }
13516 
13517 static int bnxt_open(struct net_device *dev)
13518 {
13519 	struct bnxt *bp = netdev_priv(dev);
13520 	int rc;
13521 
13522 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13523 		rc = bnxt_reinit_after_abort(bp);
13524 		if (rc) {
13525 			if (rc == -EBUSY)
13526 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
13527 			else
13528 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
13529 			return -ENODEV;
13530 		}
13531 	}
13532 
13533 	rc = bnxt_hwrm_if_change(bp, true);
13534 	if (rc)
13535 		return rc;
13536 
13537 	rc = __bnxt_open_nic(bp, true, true);
13538 	if (rc) {
13539 		bnxt_hwrm_if_change(bp, false);
13540 	} else {
13541 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
13542 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13543 				bnxt_queue_sp_work(bp,
13544 						   BNXT_RESTART_ULP_SP_EVENT);
13545 		}
13546 	}
13547 
13548 	return rc;
13549 }
13550 
13551 static bool bnxt_drv_busy(struct bnxt *bp)
13552 {
13553 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
13554 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
13555 }
13556 
13557 static void bnxt_get_ring_stats(struct bnxt *bp,
13558 				struct rtnl_link_stats64 *stats);
13559 
13560 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
13561 			     bool link_re_init)
13562 {
13563 	/* Close the VF-reps before closing PF */
13564 	if (BNXT_PF(bp))
13565 		bnxt_vf_reps_close(bp);
13566 
13567 	/* Change device state to avoid TX queue wake up's */
13568 	bnxt_tx_disable(bp);
13569 
13570 	clear_bit(BNXT_STATE_OPEN, &bp->state);
13571 	smp_mb__after_atomic();
13572 	while (bnxt_drv_busy(bp))
13573 		msleep(20);
13574 
13575 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13576 		bnxt_clear_rss_ctxs(bp);
13577 	/* Flush rings and disable interrupts */
13578 	bnxt_shutdown_nic(bp, irq_re_init);
13579 
13580 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
13581 
13582 	bnxt_debug_dev_exit(bp);
13583 	bnxt_disable_napi(bp);
13584 	timer_delete_sync(&bp->timer);
13585 	bnxt_free_skbs(bp);
13586 
13587 	/* Save ring stats before shutdown */
13588 	if (bp->bnapi && irq_re_init) {
13589 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
13590 		bnxt_get_ring_drv_stats(bp, &bp->ring_drv_stats_prev);
13591 	}
13592 	if (irq_re_init) {
13593 		bnxt_free_irq(bp);
13594 		bnxt_del_napi(bp);
13595 	}
13596 	bnxt_free_mem(bp, irq_re_init);
13597 }
13598 
13599 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13600 {
13601 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13602 		/* If we get here, it means firmware reset is in progress
13603 		 * while we are trying to close.  We can safely proceed with
13604 		 * the close because we are holding netdev instance lock.
13605 		 * Some firmware messages may fail as we proceed to close.
13606 		 * We set the ABORT_ERR flag here so that the FW reset thread
13607 		 * will later abort when it gets the netdev instance lock
13608 		 * and sees the flag.
13609 		 */
13610 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
13611 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13612 	}
13613 
13614 #ifdef CONFIG_BNXT_SRIOV
13615 	if (bp->sriov_cfg) {
13616 		int rc;
13617 
13618 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
13619 						      !bp->sriov_cfg,
13620 						      BNXT_SRIOV_CFG_WAIT_TMO);
13621 		if (!rc)
13622 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
13623 		else if (rc < 0)
13624 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
13625 	}
13626 #endif
13627 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
13628 }
13629 
13630 static int bnxt_close(struct net_device *dev)
13631 {
13632 	struct bnxt *bp = netdev_priv(dev);
13633 
13634 	bnxt_close_nic(bp, true, true);
13635 	bnxt_hwrm_shutdown_link(bp);
13636 	bnxt_hwrm_if_change(bp, false);
13637 	return 0;
13638 }
13639 
13640 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
13641 				   u16 *val)
13642 {
13643 	struct hwrm_port_phy_mdio_read_output *resp;
13644 	struct hwrm_port_phy_mdio_read_input *req;
13645 	int rc;
13646 
13647 	if (bp->hwrm_spec_code < 0x10a00)
13648 		return -EOPNOTSUPP;
13649 
13650 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
13651 	if (rc)
13652 		return rc;
13653 
13654 	req->port_id = cpu_to_le16(bp->pf.port_id);
13655 	req->phy_addr = phy_addr;
13656 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13657 	if (mdio_phy_id_is_c45(phy_addr)) {
13658 		req->cl45_mdio = 1;
13659 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13660 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13661 		req->reg_addr = cpu_to_le16(reg);
13662 	}
13663 
13664 	resp = hwrm_req_hold(bp, req);
13665 	rc = hwrm_req_send(bp, req);
13666 	if (!rc)
13667 		*val = le16_to_cpu(resp->reg_data);
13668 	hwrm_req_drop(bp, req);
13669 	return rc;
13670 }
13671 
13672 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
13673 				    u16 val)
13674 {
13675 	struct hwrm_port_phy_mdio_write_input *req;
13676 	int rc;
13677 
13678 	if (bp->hwrm_spec_code < 0x10a00)
13679 		return -EOPNOTSUPP;
13680 
13681 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
13682 	if (rc)
13683 		return rc;
13684 
13685 	req->port_id = cpu_to_le16(bp->pf.port_id);
13686 	req->phy_addr = phy_addr;
13687 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13688 	if (mdio_phy_id_is_c45(phy_addr)) {
13689 		req->cl45_mdio = 1;
13690 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13691 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13692 		req->reg_addr = cpu_to_le16(reg);
13693 	}
13694 	req->reg_data = cpu_to_le16(val);
13695 
13696 	return hwrm_req_send(bp, req);
13697 }
13698 
13699 /* netdev instance lock held */
13700 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13701 {
13702 	struct mii_ioctl_data *mdio = if_mii(ifr);
13703 	struct bnxt *bp = netdev_priv(dev);
13704 	int rc;
13705 
13706 	switch (cmd) {
13707 	case SIOCGMIIPHY:
13708 		mdio->phy_id = bp->link_info.phy_addr;
13709 
13710 		fallthrough;
13711 	case SIOCGMIIREG: {
13712 		u16 mii_regval = 0;
13713 
13714 		if (!netif_running(dev))
13715 			return -EAGAIN;
13716 
13717 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
13718 					     &mii_regval);
13719 		mdio->val_out = mii_regval;
13720 		return rc;
13721 	}
13722 
13723 	case SIOCSMIIREG:
13724 		if (!netif_running(dev))
13725 			return -EAGAIN;
13726 
13727 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
13728 						mdio->val_in);
13729 
13730 	default:
13731 		/* do nothing */
13732 		break;
13733 	}
13734 	return -EOPNOTSUPP;
13735 }
13736 
13737 static void bnxt_get_ring_stats(struct bnxt *bp,
13738 				struct rtnl_link_stats64 *stats)
13739 {
13740 	int i;
13741 
13742 	for (i = 0; i < bp->cp_nr_rings; i++) {
13743 		struct bnxt_napi *bnapi = bp->bnapi[i];
13744 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13745 		u64 *sw = cpr->stats.sw_stats;
13746 
13747 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
13748 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13749 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
13750 
13751 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
13752 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
13753 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
13754 
13755 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
13756 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
13757 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
13758 
13759 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
13760 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
13761 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
13762 
13763 		stats->rx_missed_errors +=
13764 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
13765 
13766 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13767 
13768 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
13769 
13770 		stats->rx_dropped +=
13771 			cpr->sw_stats->rx.rx_netpoll_discards +
13772 			cpr->sw_stats->rx.rx_oom_discards;
13773 	}
13774 }
13775 
13776 static void bnxt_add_prev_stats(struct bnxt *bp,
13777 				struct rtnl_link_stats64 *stats)
13778 {
13779 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
13780 
13781 	stats->rx_packets += prev_stats->rx_packets;
13782 	stats->tx_packets += prev_stats->tx_packets;
13783 	stats->rx_bytes += prev_stats->rx_bytes;
13784 	stats->tx_bytes += prev_stats->tx_bytes;
13785 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
13786 	stats->multicast += prev_stats->multicast;
13787 	stats->rx_dropped += prev_stats->rx_dropped;
13788 	stats->tx_dropped += prev_stats->tx_dropped;
13789 }
13790 
13791 static void
13792 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
13793 {
13794 	struct bnxt *bp = netdev_priv(dev);
13795 
13796 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
13797 	/* Make sure bnxt_close_nic() sees that we are reading stats before
13798 	 * we check the BNXT_STATE_OPEN flag.
13799 	 */
13800 	smp_mb__after_atomic();
13801 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13802 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13803 		*stats = bp->net_stats_prev;
13804 		return;
13805 	}
13806 
13807 	bnxt_sync_ring_stats(bp);
13808 	bnxt_get_ring_stats(bp, stats);
13809 	bnxt_add_prev_stats(bp, stats);
13810 
13811 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
13812 		u64 *rx = bp->port_stats.sw_stats;
13813 		u64 *tx = bp->port_stats.sw_stats +
13814 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
13815 
13816 		stats->rx_crc_errors =
13817 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
13818 		stats->rx_frame_errors =
13819 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
13820 		stats->rx_length_errors =
13821 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
13822 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
13823 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
13824 		stats->rx_errors =
13825 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
13826 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
13827 		stats->collisions =
13828 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
13829 		stats->tx_fifo_errors =
13830 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
13831 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
13832 	}
13833 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13834 }
13835 
13836 static void bnxt_get_one_ring_drv_stats(struct bnxt *bp,
13837 					struct bnxt_total_ring_drv_stats *stats,
13838 					struct bnxt_cp_ring_info *cpr)
13839 {
13840 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
13841 	u64 *hw_stats = cpr->stats.sw_stats;
13842 
13843 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
13844 	stats->rx_total_resets += sw_stats->rx.rx_resets;
13845 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
13846 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
13847 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
13848 	stats->rx_total_ring_discards +=
13849 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
13850 	stats->rx_total_hw_gro_packets += sw_stats->rx.rx_hw_gro_packets;
13851 	stats->rx_total_hw_gro_wire_packets += sw_stats->rx.rx_hw_gro_wire_packets;
13852 	stats->tx_total_resets += sw_stats->tx.tx_resets;
13853 	stats->tx_total_ring_discards +=
13854 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
13855 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
13856 }
13857 
13858 void bnxt_get_ring_drv_stats(struct bnxt *bp,
13859 			     struct bnxt_total_ring_drv_stats *stats)
13860 {
13861 	int i;
13862 
13863 	for (i = 0; i < bp->cp_nr_rings; i++)
13864 		bnxt_get_one_ring_drv_stats(bp, stats, &bp->bnapi[i]->cp_ring);
13865 }
13866 
13867 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask,
13868 				 const struct netdev_hw_addr_list *mc)
13869 {
13870 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13871 	struct netdev_hw_addr *ha;
13872 	u8 *haddr;
13873 	int mc_count = 0;
13874 	bool update = false;
13875 	int off = 0;
13876 
13877 	netdev_hw_addr_list_for_each(ha, mc) {
13878 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
13879 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13880 			vnic->mc_list_count = 0;
13881 			return false;
13882 		}
13883 		haddr = ha->addr;
13884 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
13885 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
13886 			update = true;
13887 		}
13888 		off += ETH_ALEN;
13889 		mc_count++;
13890 	}
13891 	if (mc_count)
13892 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13893 
13894 	if (mc_count != vnic->mc_list_count) {
13895 		vnic->mc_list_count = mc_count;
13896 		update = true;
13897 	}
13898 	return update;
13899 }
13900 
13901 static bool bnxt_uc_list_updated(struct bnxt *bp,
13902 				 const struct netdev_hw_addr_list *uc)
13903 {
13904 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13905 	struct netdev_hw_addr *ha;
13906 	int off = 0;
13907 
13908 	if (netdev_hw_addr_list_count(uc) != (vnic->uc_filter_count - 1))
13909 		return true;
13910 
13911 	netdev_hw_addr_list_for_each(ha, uc) {
13912 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
13913 			return true;
13914 
13915 		off += ETH_ALEN;
13916 	}
13917 	return false;
13918 }
13919 
13920 static int bnxt_set_rx_mode(struct net_device *dev,
13921 			    struct netdev_hw_addr_list *uc,
13922 			    struct netdev_hw_addr_list *mc)
13923 {
13924 	struct bnxt *bp = netdev_priv(dev);
13925 	struct bnxt_vnic_info *vnic;
13926 	bool mc_update = false;
13927 	bool uc_update;
13928 	u32 mask;
13929 
13930 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13931 		return 0;
13932 
13933 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13934 	mask = vnic->rx_mask;
13935 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13936 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13937 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13938 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13939 
13940 	if (dev->flags & IFF_PROMISC)
13941 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13942 
13943 	uc_update = bnxt_uc_list_updated(bp, uc);
13944 
13945 	if (dev->flags & IFF_BROADCAST)
13946 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13947 	if (dev->flags & IFF_ALLMULTI) {
13948 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13949 		vnic->mc_list_count = 0;
13950 	} else if (dev->flags & IFF_MULTICAST) {
13951 		mc_update = bnxt_mc_list_updated(bp, &mask, mc);
13952 	}
13953 
13954 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13955 		vnic->rx_mask = mask;
13956 
13957 		return bnxt_cfg_rx_mode(bp, uc, uc_update);
13958 	}
13959 
13960 	return 0;
13961 }
13962 
13963 static int bnxt_cfg_rx_mode(struct bnxt *bp, struct netdev_hw_addr_list *uc,
13964 			    bool uc_update)
13965 {
13966 	struct net_device *dev = bp->dev;
13967 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13968 	struct netdev_hw_addr *ha;
13969 	int i, off = 0, rc;
13970 
13971 	if (!uc_update)
13972 		goto skip_uc;
13973 
13974 	for (i = 1; i < vnic->uc_filter_count; i++) {
13975 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13976 
13977 		bnxt_hwrm_l2_filter_free(bp, fltr);
13978 		bnxt_del_l2_filter(bp, fltr);
13979 	}
13980 
13981 	vnic->uc_filter_count = 1;
13982 
13983 	netif_addr_lock_bh(dev);
13984 	if (netdev_hw_addr_list_count(uc) > (BNXT_MAX_UC_ADDRS - 1)) {
13985 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13986 	} else {
13987 		netdev_hw_addr_list_for_each(ha, uc) {
13988 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13989 			off += ETH_ALEN;
13990 			vnic->uc_filter_count++;
13991 		}
13992 	}
13993 	netif_addr_unlock_bh(dev);
13994 
13995 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13996 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13997 		if (rc) {
13998 			if (rc == -ECOMM) {
13999 				netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
14000 				rc = -EAGAIN;
14001 			} else if (rc == -EAGAIN) {
14002 				netdev_warn(bp->dev, "FW busy while setting vnic filter, will retry\n");
14003 			} else {
14004 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
14005 			}
14006 			vnic->uc_filter_count = i;
14007 			return rc;
14008 		}
14009 	}
14010 
14011 skip_uc:
14012 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
14013 	    !bnxt_promisc_ok(bp))
14014 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
14015 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
14016 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
14017 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
14018 			    rc);
14019 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
14020 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
14021 		vnic->mc_list_count = 0;
14022 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
14023 	}
14024 	if (rc)
14025 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
14026 			   rc);
14027 
14028 	return rc;
14029 }
14030 
14031 static bool bnxt_can_reserve_rings(struct bnxt *bp)
14032 {
14033 #ifdef CONFIG_BNXT_SRIOV
14034 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
14035 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
14036 
14037 		/* No minimum rings were provisioned by the PF.  Don't
14038 		 * reserve rings by default when device is down.
14039 		 */
14040 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
14041 			return true;
14042 
14043 		if (!netif_running(bp->dev))
14044 			return false;
14045 	}
14046 #endif
14047 	return true;
14048 }
14049 
14050 /* If the chip and firmware supports RFS */
14051 static bool bnxt_rfs_supported(struct bnxt *bp)
14052 {
14053 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
14054 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
14055 			return true;
14056 		return false;
14057 	}
14058 	/* 212 firmware is broken for aRFS */
14059 	if (BNXT_FW_MAJ(bp) == 212)
14060 		return false;
14061 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
14062 		return true;
14063 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
14064 		return true;
14065 	return false;
14066 }
14067 
14068 /* If runtime conditions support RFS */
14069 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
14070 {
14071 	struct bnxt_hw_rings hwr = {0};
14072 	int max_vnics, max_rss_ctxs;
14073 
14074 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14075 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
14076 		return bnxt_rfs_supported(bp);
14077 
14078 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
14079 		return false;
14080 
14081 	hwr.grp = bp->rx_nr_rings;
14082 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
14083 	if (new_rss_ctx)
14084 		hwr.vnic++;
14085 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14086 	max_vnics = bnxt_get_max_func_vnics(bp);
14087 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
14088 
14089 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
14090 		if (bp->rx_nr_rings > 1)
14091 			netdev_warn(bp->dev,
14092 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
14093 				    min(max_rss_ctxs - 1, max_vnics - 1));
14094 		return false;
14095 	}
14096 
14097 	if (!BNXT_NEW_RM(bp))
14098 		return true;
14099 
14100 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
14101 	 * issue that will mess up the default VNIC if we reduce the
14102 	 * reservations.
14103 	 */
14104 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
14105 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
14106 		return true;
14107 
14108 	bnxt_hwrm_reserve_rings(bp, &hwr);
14109 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
14110 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
14111 		return true;
14112 
14113 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
14114 	hwr.vnic = 1;
14115 	hwr.rss_ctx = 0;
14116 	bnxt_hwrm_reserve_rings(bp, &hwr);
14117 	return false;
14118 }
14119 
14120 static netdev_features_t bnxt_fix_features(struct net_device *dev,
14121 					   netdev_features_t features)
14122 {
14123 	struct bnxt *bp = netdev_priv(dev);
14124 	netdev_features_t vlan_features;
14125 
14126 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
14127 		features &= ~NETIF_F_NTUPLE;
14128 
14129 	if ((features & NETIF_F_GSO_UDP_L4) &&
14130 	    !(bp->flags & BNXT_FLAG_UDP_GSO_CAP) &&
14131 	    bp->tx_ring_size < 2 * BNXT_SW_USO_MAX_DESCS)
14132 		features &= ~NETIF_F_GSO_UDP_L4;
14133 
14134 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
14135 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
14136 
14137 	if (!(features & NETIF_F_GRO))
14138 		features &= ~NETIF_F_GRO_HW;
14139 
14140 	if (features & NETIF_F_GRO_HW)
14141 		features &= ~NETIF_F_LRO;
14142 
14143 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
14144 	 * turned on or off together.
14145 	 */
14146 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
14147 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
14148 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
14149 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
14150 		else if (vlan_features)
14151 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
14152 	}
14153 #ifdef CONFIG_BNXT_SRIOV
14154 	if (BNXT_VF(bp) && bp->vf.vlan)
14155 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
14156 #endif
14157 	return features;
14158 }
14159 
14160 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
14161 				bool link_re_init, u32 flags, bool update_tpa)
14162 {
14163 	bnxt_close_nic(bp, irq_re_init, link_re_init);
14164 	bp->flags = flags;
14165 	if (update_tpa)
14166 		bnxt_set_ring_params(bp);
14167 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
14168 }
14169 
14170 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
14171 {
14172 	bool update_tpa = false, update_ntuple = false;
14173 	struct bnxt *bp = netdev_priv(dev);
14174 	u32 flags = bp->flags;
14175 	u32 changes;
14176 	int rc = 0;
14177 	bool re_init = false;
14178 
14179 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
14180 				   bnxt_min_tx_desc_cnt(bp, features));
14181 
14182 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
14183 	if (features & NETIF_F_GRO_HW)
14184 		flags |= BNXT_FLAG_GRO;
14185 	else if (features & NETIF_F_LRO)
14186 		flags |= BNXT_FLAG_LRO;
14187 
14188 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
14189 		flags &= ~BNXT_FLAG_TPA;
14190 
14191 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
14192 		flags |= BNXT_FLAG_STRIP_VLAN;
14193 
14194 	if (features & NETIF_F_NTUPLE)
14195 		flags |= BNXT_FLAG_RFS;
14196 	else
14197 		bnxt_clear_usr_fltrs(bp, true);
14198 
14199 	changes = flags ^ bp->flags;
14200 	if (changes & BNXT_FLAG_TPA) {
14201 		update_tpa = true;
14202 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
14203 		    (flags & BNXT_FLAG_TPA) == 0 ||
14204 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14205 			re_init = true;
14206 	}
14207 
14208 	if (changes & ~BNXT_FLAG_TPA)
14209 		re_init = true;
14210 
14211 	if (changes & BNXT_FLAG_RFS)
14212 		update_ntuple = true;
14213 
14214 	if (flags != bp->flags) {
14215 		u32 old_flags = bp->flags;
14216 
14217 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14218 			bp->flags = flags;
14219 			if (update_tpa)
14220 				bnxt_set_ring_params(bp);
14221 			return rc;
14222 		}
14223 
14224 		if (update_ntuple)
14225 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
14226 
14227 		if (re_init)
14228 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
14229 
14230 		if (update_tpa) {
14231 			bp->flags = flags;
14232 			rc = bnxt_set_tpa(bp,
14233 					  (flags & BNXT_FLAG_TPA) ?
14234 					  true : false);
14235 			if (rc)
14236 				bp->flags = old_flags;
14237 		}
14238 	}
14239 	return rc;
14240 }
14241 
14242 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
14243 			      u8 **nextp)
14244 {
14245 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
14246 	int hdr_count = 0;
14247 	u8 *nexthdr;
14248 	int start;
14249 
14250 	/* Check that there are at most 2 IPv6 extension headers, no
14251 	 * fragment header, and each is <= 64 bytes.
14252 	 */
14253 	start = nw_off + sizeof(*ip6h);
14254 	nexthdr = &ip6h->nexthdr;
14255 	while (ipv6_ext_hdr(*nexthdr)) {
14256 		struct ipv6_opt_hdr *hp;
14257 		int hdrlen;
14258 
14259 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
14260 		    *nexthdr == NEXTHDR_FRAGMENT)
14261 			return false;
14262 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
14263 					  skb_headlen(skb), NULL);
14264 		if (!hp)
14265 			return false;
14266 		if (*nexthdr == NEXTHDR_AUTH)
14267 			hdrlen = ipv6_authlen(hp);
14268 		else
14269 			hdrlen = ipv6_optlen(hp);
14270 
14271 		if (hdrlen > 64)
14272 			return false;
14273 
14274 		hdr_count++;
14275 		nexthdr = &hp->nexthdr;
14276 		start += hdrlen;
14277 	}
14278 	if (nextp) {
14279 		/* Caller will check inner protocol */
14280 		if (skb->encapsulation) {
14281 			*nextp = nexthdr;
14282 			return true;
14283 		}
14284 		*nextp = NULL;
14285 	}
14286 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
14287 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
14288 }
14289 
14290 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
14291 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
14292 {
14293 	struct udphdr *uh = udp_hdr(skb);
14294 	__be16 udp_port = uh->dest;
14295 
14296 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
14297 	    udp_port != bp->vxlan_gpe_port)
14298 		return false;
14299 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
14300 		struct ethhdr *eh = inner_eth_hdr(skb);
14301 
14302 		switch (eh->h_proto) {
14303 		case htons(ETH_P_IP):
14304 			return true;
14305 		case htons(ETH_P_IPV6):
14306 			return bnxt_exthdr_check(bp, skb,
14307 						 skb_inner_network_offset(skb),
14308 						 NULL);
14309 		}
14310 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
14311 		return true;
14312 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
14313 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
14314 					 NULL);
14315 	}
14316 	return false;
14317 }
14318 
14319 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
14320 {
14321 	switch (l4_proto) {
14322 	case IPPROTO_UDP:
14323 		return bnxt_udp_tunl_check(bp, skb);
14324 	case IPPROTO_IPIP:
14325 		return true;
14326 	case IPPROTO_GRE: {
14327 		switch (skb->inner_protocol) {
14328 		default:
14329 			return false;
14330 		case htons(ETH_P_IP):
14331 			return true;
14332 		case htons(ETH_P_IPV6):
14333 			fallthrough;
14334 		}
14335 	}
14336 	case IPPROTO_IPV6:
14337 		/* Check ext headers of inner ipv6 */
14338 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
14339 					 NULL);
14340 	}
14341 	return false;
14342 }
14343 
14344 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
14345 					     struct net_device *dev,
14346 					     netdev_features_t features)
14347 {
14348 	struct bnxt *bp = netdev_priv(dev);
14349 	u8 *l4_proto;
14350 
14351 	features = vlan_features_check(skb, features);
14352 
14353 	/* SW USO pads at most the last segment; let the stack segment skbs
14354 	 * where every segment would be shorter than BNXT_MIN_PKT_SIZE.
14355 	 */
14356 	if (skb_is_gso(skb) &&
14357 	    (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) &&
14358 	    !(bp->flags & BNXT_FLAG_UDP_GSO_CAP) &&
14359 	    bnxt_sw_gso_pad_len(skb_transport_offset(skb) +
14360 				sizeof(struct udphdr),
14361 				skb_shinfo(skb)->gso_size))
14362 		features &= ~NETIF_F_GSO_MASK;
14363 
14364 	switch (vlan_get_protocol(skb)) {
14365 	case htons(ETH_P_IP):
14366 		if (!skb->encapsulation)
14367 			return features;
14368 		l4_proto = &ip_hdr(skb)->protocol;
14369 		if (bnxt_tunl_check(bp, skb, *l4_proto))
14370 			return features;
14371 		break;
14372 	case htons(ETH_P_IPV6):
14373 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
14374 				       &l4_proto))
14375 			break;
14376 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
14377 			return features;
14378 		break;
14379 	}
14380 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
14381 }
14382 
14383 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
14384 			 u32 *reg_buf)
14385 {
14386 	struct hwrm_dbg_read_direct_output *resp;
14387 	struct hwrm_dbg_read_direct_input *req;
14388 	__le32 *dbg_reg_buf;
14389 	dma_addr_t mapping;
14390 	int rc, i;
14391 
14392 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
14393 	if (rc)
14394 		return rc;
14395 
14396 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
14397 					 &mapping);
14398 	if (!dbg_reg_buf) {
14399 		rc = -ENOMEM;
14400 		goto dbg_rd_reg_exit;
14401 	}
14402 
14403 	req->host_dest_addr = cpu_to_le64(mapping);
14404 
14405 	resp = hwrm_req_hold(bp, req);
14406 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
14407 	req->read_len32 = cpu_to_le32(num_words);
14408 
14409 	rc = hwrm_req_send(bp, req);
14410 	if (rc || resp->error_code) {
14411 		rc = -EIO;
14412 		goto dbg_rd_reg_exit;
14413 	}
14414 	for (i = 0; i < num_words; i++)
14415 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
14416 
14417 dbg_rd_reg_exit:
14418 	hwrm_req_drop(bp, req);
14419 	return rc;
14420 }
14421 
14422 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
14423 				       u32 ring_id, u32 *prod, u32 *cons)
14424 {
14425 	struct hwrm_dbg_ring_info_get_output *resp;
14426 	struct hwrm_dbg_ring_info_get_input *req;
14427 	int rc;
14428 
14429 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
14430 	if (rc)
14431 		return rc;
14432 
14433 	req->ring_type = ring_type;
14434 	req->fw_ring_id = cpu_to_le32(ring_id);
14435 	resp = hwrm_req_hold(bp, req);
14436 	rc = hwrm_req_send(bp, req);
14437 	if (!rc) {
14438 		*prod = le32_to_cpu(resp->producer_index);
14439 		*cons = le32_to_cpu(resp->consumer_index);
14440 	}
14441 	hwrm_req_drop(bp, req);
14442 	return rc;
14443 }
14444 
14445 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
14446 {
14447 	struct bnxt_tx_ring_info *txr;
14448 	int i = bnapi->index, j;
14449 
14450 	bnxt_for_each_napi_tx(j, bnapi, txr)
14451 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
14452 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
14453 			    txr->tx_cons);
14454 }
14455 
14456 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
14457 {
14458 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
14459 	int i = bnapi->index;
14460 
14461 	if (!rxr)
14462 		return;
14463 
14464 	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",
14465 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
14466 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
14467 		    rxr->rx_sw_agg_prod);
14468 }
14469 
14470 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
14471 {
14472 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring, *cpr2;
14473 	int i = bnapi->index, j;
14474 
14475 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
14476 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
14477 	for (j = 0; j < cpr->cp_ring_count; j++) {
14478 		cpr2 = &cpr->cp_ring_arr[j];
14479 		if (!cpr2->bnapi)
14480 			continue;
14481 		netdev_info(bnapi->bp->dev, "[%d.%d]: cp{fw_ring: %d raw_cons: %x}\n",
14482 			    i, j, cpr2->cp_ring_struct.fw_ring_id,
14483 			    cpr2->cp_raw_cons);
14484 	}
14485 }
14486 
14487 static void bnxt_dbg_dump_states(struct bnxt *bp)
14488 {
14489 	int i;
14490 	struct bnxt_napi *bnapi;
14491 
14492 	for (i = 0; i < bp->cp_nr_rings; i++) {
14493 		bnapi = bp->bnapi[i];
14494 		if (netif_msg_drv(bp)) {
14495 			bnxt_dump_tx_sw_state(bnapi);
14496 			bnxt_dump_rx_sw_state(bnapi);
14497 			bnxt_dump_cp_sw_state(bnapi);
14498 		}
14499 	}
14500 }
14501 
14502 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
14503 {
14504 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
14505 	struct hwrm_ring_reset_input *req;
14506 	struct bnxt_napi *bnapi = rxr->bnapi;
14507 	struct bnxt_cp_ring_info *cpr;
14508 	u16 cp_ring_id;
14509 	int rc;
14510 
14511 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
14512 	if (rc)
14513 		return rc;
14514 
14515 	cpr = &bnapi->cp_ring;
14516 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
14517 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
14518 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
14519 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
14520 	return hwrm_req_send_silent(bp, req);
14521 }
14522 
14523 static void bnxt_reset_task(struct bnxt *bp, bool silent)
14524 {
14525 	if (!silent)
14526 		bnxt_dbg_dump_states(bp);
14527 	if (netif_running(bp->dev)) {
14528 		bnxt_close_nic(bp, !silent, false);
14529 		bnxt_open_nic(bp, !silent, false);
14530 	}
14531 }
14532 
14533 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
14534 {
14535 	struct bnxt *bp = netdev_priv(dev);
14536 
14537 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
14538 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
14539 }
14540 
14541 static void bnxt_fw_health_check(struct bnxt *bp)
14542 {
14543 	struct bnxt_fw_health *fw_health = bp->fw_health;
14544 	struct pci_dev *pdev = bp->pdev;
14545 	u32 val;
14546 
14547 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14548 		return;
14549 
14550 	/* Make sure it is enabled before checking the tmr_counter. */
14551 	smp_rmb();
14552 	if (fw_health->tmr_counter) {
14553 		fw_health->tmr_counter--;
14554 		return;
14555 	}
14556 
14557 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14558 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
14559 		fw_health->arrests++;
14560 		goto fw_reset;
14561 	}
14562 
14563 	fw_health->last_fw_heartbeat = val;
14564 
14565 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14566 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
14567 		fw_health->discoveries++;
14568 		goto fw_reset;
14569 	}
14570 
14571 	fw_health->tmr_counter = fw_health->tmr_multiplier;
14572 	return;
14573 
14574 fw_reset:
14575 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
14576 }
14577 
14578 static void bnxt_timer(struct timer_list *t)
14579 {
14580 	struct bnxt *bp = timer_container_of(bp, t, timer);
14581 	struct net_device *dev = bp->dev;
14582 
14583 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
14584 		return;
14585 
14586 	if (atomic_read(&bp->intr_sem) != 0)
14587 		goto bnxt_restart_timer;
14588 
14589 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
14590 		bnxt_fw_health_check(bp);
14591 
14592 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
14593 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
14594 
14595 	if (bnxt_tc_flower_enabled(bp))
14596 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
14597 
14598 #ifdef CONFIG_RFS_ACCEL
14599 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
14600 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14601 #endif /*CONFIG_RFS_ACCEL*/
14602 
14603 	if (bp->link_info.phy_retry) {
14604 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
14605 			bp->link_info.phy_retry = false;
14606 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
14607 		} else {
14608 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
14609 		}
14610 	}
14611 
14612 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
14613 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
14614 
14615 bnxt_restart_timer:
14616 	mod_timer(&bp->timer, jiffies + bp->current_interval);
14617 }
14618 
14619 static void bnxt_lock_sp(struct bnxt *bp)
14620 {
14621 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
14622 	 * set.  If the device is being closed, bnxt_close() may be holding
14623 	 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear.
14624 	 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev
14625 	 * instance lock.
14626 	 */
14627 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14628 	netdev_lock(bp->dev);
14629 }
14630 
14631 static void bnxt_unlock_sp(struct bnxt *bp)
14632 {
14633 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14634 	netdev_unlock(bp->dev);
14635 }
14636 
14637 /* Same as bnxt_lock_sp() with additional rtnl_lock */
14638 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
14639 {
14640 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14641 	rtnl_lock();
14642 	netdev_lock(bp->dev);
14643 }
14644 
14645 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
14646 {
14647 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14648 	netdev_unlock(bp->dev);
14649 	rtnl_unlock();
14650 }
14651 
14652 static void bnxt_tph_update(struct bnxt *bp)
14653 {
14654 	struct net_device *dev = bp->dev;
14655 	int i;
14656 
14657 	bnxt_lock_sp(bp);
14658 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
14659 		goto unlock;
14660 
14661 	for (i = 0; i < bp->rx_nr_rings; i++) {
14662 		struct bnxt_irq *irq;
14663 		int map_idx, err;
14664 		u16 tag;
14665 
14666 		map_idx = bnxt_cp_num_to_irq_num(bp, i);
14667 		irq = &bp->irq_tbl[map_idx];
14668 		tag = READ_ONCE(irq->new_tag);
14669 		if (irq->tag == tag)
14670 			continue;
14671 
14672 		if (pcie_tph_set_st_entry(bp->pdev, irq->msix_nr, tag))
14673 			continue;
14674 
14675 		err = netdev_rx_queue_restart(dev, irq->ring_nr);
14676 		if (err) {
14677 			netdev_err(dev, "RX queue restart failed: err=%d\n",
14678 				   err);
14679 			continue;
14680 		}
14681 
14682 		irq->tag = tag;
14683 	}
14684 
14685 unlock:
14686 	bnxt_unlock_sp(bp);
14687 }
14688 
14689 /* Only called from bnxt_sp_task() */
14690 static void bnxt_reset(struct bnxt *bp, bool silent)
14691 {
14692 	bnxt_rtnl_lock_sp(bp);
14693 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
14694 		bnxt_reset_task(bp, silent);
14695 	bnxt_rtnl_unlock_sp(bp);
14696 }
14697 
14698 /* Only called from bnxt_sp_task() */
14699 static void bnxt_rx_ring_reset(struct bnxt *bp)
14700 {
14701 	int i;
14702 
14703 	bnxt_rtnl_lock_sp(bp);
14704 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14705 		bnxt_rtnl_unlock_sp(bp);
14706 		return;
14707 	}
14708 	/* Disable and flush TPA before resetting the RX ring */
14709 	if (bp->flags & BNXT_FLAG_TPA)
14710 		bnxt_set_tpa(bp, false);
14711 	for (i = 0; i < bp->rx_nr_rings; i++) {
14712 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
14713 		struct bnxt_cp_ring_info *cpr;
14714 		int rc;
14715 
14716 		if (!rxr->bnapi->in_reset)
14717 			continue;
14718 
14719 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
14720 		if (rc) {
14721 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
14722 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
14723 			else
14724 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
14725 					    rc);
14726 			bnxt_reset_task(bp, true);
14727 			break;
14728 		}
14729 		bnxt_free_one_rx_ring_skbs(bp, rxr);
14730 		rxr->rx_prod = 0;
14731 		rxr->rx_agg_prod = 0;
14732 		rxr->rx_sw_agg_prod = 0;
14733 		rxr->rx_next_cons = 0;
14734 		rxr->bnapi->in_reset = false;
14735 		rc = bnxt_alloc_one_rx_ring(bp, i);
14736 		if (rc) {
14737 			netdev_warn(bp->dev, "RX ring reset failed to allocate buffers, rc = %d, falling back to global reset\n",
14738 				    rc);
14739 			bnxt_reset_task(bp, true);
14740 			bnxt_rtnl_unlock_sp(bp);
14741 			return;
14742 		}
14743 		cpr = &rxr->bnapi->cp_ring;
14744 		cpr->sw_stats->rx.rx_resets++;
14745 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
14746 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
14747 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
14748 	}
14749 	if (bp->flags & BNXT_FLAG_TPA)
14750 		bnxt_set_tpa(bp, true);
14751 	bnxt_rtnl_unlock_sp(bp);
14752 }
14753 
14754 static void bnxt_fw_fatal_close(struct bnxt *bp)
14755 {
14756 	bnxt_tx_disable(bp);
14757 	bnxt_disable_napi(bp);
14758 	bnxt_disable_int_sync(bp);
14759 	bnxt_free_irq(bp);
14760 	bnxt_clear_int_mode(bp);
14761 	pci_disable_device(bp->pdev);
14762 }
14763 
14764 static void bnxt_fw_reset_close(struct bnxt *bp)
14765 {
14766 	/* When firmware is in fatal state, quiesce device and disable
14767 	 * bus master to prevent any potential bad DMAs before freeing
14768 	 * kernel memory.
14769 	 */
14770 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
14771 		u16 val = 0;
14772 
14773 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14774 		if (val == 0xffff)
14775 			bp->fw_reset_min_dsecs = 0;
14776 		bnxt_fw_fatal_close(bp);
14777 	}
14778 	__bnxt_close_nic(bp, true, false);
14779 	bnxt_vf_reps_free(bp);
14780 	bnxt_clear_int_mode(bp);
14781 	bnxt_hwrm_func_drv_unrgtr(bp);
14782 	if (pci_is_enabled(bp->pdev))
14783 		pci_disable_device(bp->pdev);
14784 	bnxt_free_ctx_mem(bp, false);
14785 }
14786 
14787 static bool is_bnxt_fw_ok(struct bnxt *bp)
14788 {
14789 	struct bnxt_fw_health *fw_health = bp->fw_health;
14790 	bool no_heartbeat = false, has_reset = false;
14791 	u32 val;
14792 
14793 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14794 	if (val == fw_health->last_fw_heartbeat)
14795 		no_heartbeat = true;
14796 
14797 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14798 	if (val != fw_health->last_fw_reset_cnt)
14799 		has_reset = true;
14800 
14801 	if (!no_heartbeat && has_reset)
14802 		return true;
14803 
14804 	return false;
14805 }
14806 
14807 /* netdev instance lock is acquired before calling this function */
14808 static void bnxt_force_fw_reset(struct bnxt *bp)
14809 {
14810 	struct bnxt_fw_health *fw_health = bp->fw_health;
14811 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14812 	u32 wait_dsecs;
14813 
14814 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
14815 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14816 		return;
14817 
14818 	/* we have to serialize with bnxt_refclk_read()*/
14819 	if (ptp) {
14820 		unsigned long flags;
14821 
14822 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
14823 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14824 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14825 	} else {
14826 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14827 	}
14828 	bnxt_fw_reset_close(bp);
14829 	wait_dsecs = fw_health->master_func_wait_dsecs;
14830 	if (fw_health->primary) {
14831 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
14832 			wait_dsecs = 0;
14833 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14834 	} else {
14835 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
14836 		wait_dsecs = fw_health->normal_func_wait_dsecs;
14837 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14838 	}
14839 
14840 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
14841 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
14842 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14843 }
14844 
14845 void bnxt_fw_exception(struct bnxt *bp)
14846 {
14847 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
14848 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14849 	bnxt_ulp_stop(bp);
14850 	bnxt_lock_sp(bp);
14851 	bnxt_force_fw_reset(bp);
14852 	bnxt_unlock_sp(bp);
14853 }
14854 
14855 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
14856  * < 0 on error.
14857  */
14858 static int bnxt_get_registered_vfs(struct bnxt *bp)
14859 {
14860 #ifdef CONFIG_BNXT_SRIOV
14861 	int rc;
14862 
14863 	if (!BNXT_PF(bp))
14864 		return 0;
14865 
14866 	rc = bnxt_hwrm_func_qcfg(bp);
14867 	if (rc) {
14868 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
14869 		return rc;
14870 	}
14871 	if (bp->pf.registered_vfs)
14872 		return bp->pf.registered_vfs;
14873 	if (bp->sriov_cfg)
14874 		return 1;
14875 #endif
14876 	return 0;
14877 }
14878 
14879 void bnxt_fw_reset(struct bnxt *bp)
14880 {
14881 	bnxt_ulp_stop(bp);
14882 	if (BNXT_PF(bp))
14883 		bnxt_rtnl_lock_sp(bp);
14884 	else
14885 		bnxt_lock_sp(bp);
14886 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
14887 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14888 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14889 		int n = 0, tmo;
14890 
14891 		/* we have to serialize with bnxt_refclk_read()*/
14892 		if (ptp) {
14893 			unsigned long flags;
14894 
14895 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
14896 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14897 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14898 		} else {
14899 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14900 		}
14901 		if (bp->pf.active_vfs &&
14902 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
14903 			n = bnxt_get_registered_vfs(bp);
14904 		if (n < 0) {
14905 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
14906 				   n);
14907 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14908 			netif_close(bp->dev);
14909 			goto fw_reset_exit;
14910 		} else if (n > 0) {
14911 			u16 vf_tmo_dsecs = n * 10;
14912 
14913 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
14914 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
14915 			bp->fw_reset_state =
14916 				BNXT_FW_RESET_STATE_POLL_VF;
14917 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14918 			goto fw_reset_exit;
14919 		}
14920 		bnxt_fw_reset_close(bp);
14921 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14922 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14923 			tmo = HZ / 10;
14924 		} else {
14925 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14926 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14927 		}
14928 		bnxt_queue_fw_reset_work(bp, tmo);
14929 	}
14930 fw_reset_exit:
14931 	if (BNXT_PF(bp))
14932 		bnxt_rtnl_unlock_sp(bp);
14933 	else
14934 		bnxt_unlock_sp(bp);
14935 }
14936 
14937 static void bnxt_chk_missed_irq(struct bnxt *bp)
14938 {
14939 	int i;
14940 
14941 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14942 		return;
14943 
14944 	for (i = 0; i < bp->cp_nr_rings; i++) {
14945 		struct bnxt_napi *bnapi = bp->bnapi[i];
14946 		struct bnxt_cp_ring_info *cpr;
14947 		u32 fw_ring_id;
14948 		int j;
14949 
14950 		if (!bnapi)
14951 			continue;
14952 
14953 		cpr = &bnapi->cp_ring;
14954 		for (j = 0; j < cpr->cp_ring_count; j++) {
14955 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
14956 			u32 val[2];
14957 
14958 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
14959 				continue;
14960 
14961 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
14962 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
14963 				continue;
14964 			}
14965 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
14966 			bnxt_dbg_hwrm_ring_info_get(bp,
14967 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
14968 				fw_ring_id, &val[0], &val[1]);
14969 			cpr->sw_stats->cmn.missed_irqs++;
14970 		}
14971 	}
14972 }
14973 
14974 static void bnxt_cfg_ntp_filters(struct bnxt *);
14975 
14976 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
14977 {
14978 	struct bnxt_link_info *link_info = &bp->link_info;
14979 
14980 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
14981 		link_info->autoneg = BNXT_AUTONEG_SPEED;
14982 		if (bp->hwrm_spec_code >= 0x10201) {
14983 			if (link_info->auto_pause_setting &
14984 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
14985 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14986 		} else {
14987 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14988 		}
14989 		bnxt_set_auto_speed(link_info);
14990 	} else {
14991 		bnxt_set_force_speed(link_info);
14992 		link_info->req_duplex = link_info->duplex_setting;
14993 	}
14994 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14995 		link_info->req_flow_ctrl =
14996 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14997 	else
14998 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14999 }
15000 
15001 static void bnxt_fw_echo_reply(struct bnxt *bp)
15002 {
15003 	struct bnxt_fw_health *fw_health = bp->fw_health;
15004 	struct hwrm_func_echo_response_input *req;
15005 	int rc;
15006 
15007 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
15008 	if (rc)
15009 		return;
15010 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
15011 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
15012 	hwrm_req_send(bp, req);
15013 }
15014 
15015 static void bnxt_ulp_restart(struct bnxt *bp)
15016 {
15017 	bnxt_ulp_stop(bp);
15018 	bnxt_ulp_start(bp);
15019 }
15020 
15021 static void bnxt_sp_task(struct work_struct *work)
15022 {
15023 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
15024 
15025 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
15026 	smp_mb__after_atomic();
15027 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
15028 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
15029 		return;
15030 	}
15031 
15032 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
15033 		bnxt_ulp_restart(bp);
15034 		bnxt_reenable_sriov(bp);
15035 	}
15036 
15037 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
15038 		bnxt_cfg_ntp_filters(bp);
15039 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
15040 		bnxt_hwrm_exec_fwd_req(bp);
15041 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
15042 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
15043 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
15044 		bnxt_hwrm_port_qstats(bp, 0);
15045 		bnxt_hwrm_port_qstats_ext(bp, 0);
15046 		spin_lock(&bp->stats_lock);
15047 		bnxt_accumulate_all_stats(bp);
15048 		bp->stats_updated_jiffies = jiffies;
15049 		spin_unlock(&bp->stats_lock);
15050 	}
15051 
15052 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
15053 		int rc;
15054 
15055 		mutex_lock(&bp->link_lock);
15056 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
15057 				       &bp->sp_event))
15058 			bnxt_hwrm_phy_qcaps(bp);
15059 
15060 		rc = bnxt_update_link(bp, true);
15061 		if (rc)
15062 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
15063 				   rc);
15064 
15065 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
15066 				       &bp->sp_event))
15067 			bnxt_init_ethtool_link_settings(bp);
15068 		mutex_unlock(&bp->link_lock);
15069 	}
15070 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
15071 		int rc;
15072 
15073 		mutex_lock(&bp->link_lock);
15074 		rc = bnxt_update_phy_setting(bp);
15075 		mutex_unlock(&bp->link_lock);
15076 		if (rc) {
15077 			netdev_warn(bp->dev, "update phy settings retry failed\n");
15078 		} else {
15079 			bp->link_info.phy_retry = false;
15080 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
15081 		}
15082 	}
15083 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
15084 		mutex_lock(&bp->link_lock);
15085 		bnxt_get_port_module_status(bp);
15086 		mutex_unlock(&bp->link_lock);
15087 	}
15088 
15089 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
15090 		bnxt_tc_flow_stats_work(bp);
15091 
15092 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
15093 		bnxt_chk_missed_irq(bp);
15094 
15095 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
15096 		bnxt_fw_echo_reply(bp);
15097 
15098 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
15099 		bnxt_hwmon_notify_event(bp);
15100 
15101 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
15102 	 * must be the last functions to be called before exiting.
15103 	 */
15104 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
15105 		bnxt_reset(bp, false);
15106 
15107 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
15108 		bnxt_reset(bp, true);
15109 
15110 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
15111 		bnxt_rx_ring_reset(bp);
15112 
15113 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
15114 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
15115 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
15116 			bnxt_devlink_health_fw_report(bp);
15117 		else
15118 			bnxt_fw_reset(bp);
15119 	}
15120 
15121 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
15122 		if (!is_bnxt_fw_ok(bp))
15123 			bnxt_devlink_health_fw_report(bp);
15124 	}
15125 
15126 	if (test_and_clear_bit(BNXT_TPH_UPDATE_SP_EVENT, &bp->sp_event))
15127 		bnxt_tph_update(bp);
15128 
15129 	smp_mb__before_atomic();
15130 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
15131 }
15132 
15133 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15134 				int *max_cp);
15135 
15136 /* Under netdev instance lock */
15137 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
15138 		     int tx_xdp)
15139 {
15140 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
15141 	struct bnxt_hw_rings hwr = {0};
15142 	int rx_rings = rx;
15143 	int rc;
15144 
15145 	if (tcs)
15146 		tx_sets = tcs;
15147 
15148 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
15149 
15150 	if (max_rx < rx_rings)
15151 		return -ENOMEM;
15152 
15153 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15154 		rx_rings <<= 1;
15155 
15156 	hwr.rx = rx_rings;
15157 	hwr.tx = tx * tx_sets + tx_xdp;
15158 	if (max_tx < hwr.tx)
15159 		return -ENOMEM;
15160 
15161 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
15162 
15163 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
15164 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
15165 	if (max_cp < hwr.cp)
15166 		return -ENOMEM;
15167 	hwr.stat = hwr.cp;
15168 	if (BNXT_NEW_RM(bp)) {
15169 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
15170 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
15171 		hwr.grp = rx;
15172 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
15173 	}
15174 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
15175 		hwr.cp_p5 = hwr.tx + rx;
15176 	rc = bnxt_hwrm_check_rings(bp, &hwr);
15177 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
15178 		if (!bnxt_ulp_registered(bp->edev[BNXT_AUXDEV_RDMA])) {
15179 			hwr.cp += bnxt_get_ulp_msix_num(bp);
15180 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
15181 		}
15182 		if (hwr.cp > bp->total_irqs) {
15183 			int old_total = bp->total_irqs;
15184 			int total_msix = bnxt_change_msix(bp, hwr.cp);
15185 
15186 			if (total_msix < hwr.cp) {
15187 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
15188 					    hwr.cp, total_msix);
15189 				rc = -ENOSPC;
15190 			}
15191 			/* FW has not reserved the new vectors yet, and writes to
15192 			 * MSI-X table entries beyond the reserved IRQs don't
15193 			 * stick (seen on BCM57508).  Free them again;
15194 			 * bnxt_reserve_rings() allocates them after
15195 			 * __bnxt_reserve_rings().  Without the new resource
15196 			 * manager there is no IRQ reservation and
15197 			 * bnxt_reserve_rings() does not grow the vectors, so
15198 			 * keep them.
15199 			 */
15200 			if (BNXT_NEW_RM(bp))
15201 				bnxt_change_msix(bp, old_total);
15202 		}
15203 	}
15204 	return rc;
15205 }
15206 
15207 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
15208 {
15209 	if (bp->bar2) {
15210 		pci_iounmap(pdev, bp->bar2);
15211 		bp->bar2 = NULL;
15212 	}
15213 
15214 	if (bp->bar1) {
15215 		pci_iounmap(pdev, bp->bar1);
15216 		bp->bar1 = NULL;
15217 	}
15218 
15219 	if (bp->bar0) {
15220 		pci_iounmap(pdev, bp->bar0);
15221 		bp->bar0 = NULL;
15222 	}
15223 }
15224 
15225 static void bnxt_cleanup_pci(struct bnxt *bp)
15226 {
15227 	pci_disable_ptm(bp->pdev);
15228 	bnxt_unmap_bars(bp, bp->pdev);
15229 	pci_release_regions(bp->pdev);
15230 	if (pci_is_enabled(bp->pdev))
15231 		pci_disable_device(bp->pdev);
15232 }
15233 
15234 static void bnxt_init_dflt_coal(struct bnxt *bp)
15235 {
15236 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
15237 	struct bnxt_coal *coal;
15238 	u16 flags = 0;
15239 
15240 	if (coal_cap->cmpl_params &
15241 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
15242 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
15243 
15244 	/* Tick values in micro seconds.
15245 	 * 1 coal_buf x bufs_per_record = 1 completion record.
15246 	 */
15247 	coal = &bp->rx_coal;
15248 	coal->coal_ticks = 10;
15249 	coal->coal_bufs = 30;
15250 	coal->coal_ticks_irq = 1;
15251 	coal->coal_bufs_irq = 2;
15252 	coal->idle_thresh = 50;
15253 	coal->bufs_per_record = 2;
15254 	coal->budget = 64;		/* NAPI budget */
15255 	coal->flags = flags;
15256 
15257 	coal = &bp->tx_coal;
15258 	coal->coal_ticks = 28;
15259 	coal->coal_bufs = 30;
15260 	coal->coal_ticks_irq = 2;
15261 	coal->coal_bufs_irq = 2;
15262 	coal->bufs_per_record = 1;
15263 	coal->flags = flags;
15264 
15265 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
15266 }
15267 
15268 /* FW that pre-reserves 1 VNIC per function */
15269 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
15270 {
15271 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
15272 
15273 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
15274 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
15275 		return true;
15276 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
15277 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
15278 		return true;
15279 	return false;
15280 }
15281 
15282 static void bnxt_hwrm_pfcwd_qcaps(struct bnxt *bp)
15283 {
15284 	struct hwrm_queue_pfcwd_timeout_qcaps_output *resp;
15285 	struct hwrm_queue_pfcwd_timeout_qcaps_input *req;
15286 	int rc;
15287 
15288 	bp->max_pfcwd_tmo_ms = 0;
15289 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_PFCWD_TIMEOUT_QCAPS);
15290 	if (rc)
15291 		return;
15292 	resp = hwrm_req_hold(bp, req);
15293 	rc = hwrm_req_send_silent(bp, req);
15294 	if (!rc)
15295 		bp->max_pfcwd_tmo_ms = le16_to_cpu(resp->max_pfcwd_timeout);
15296 	hwrm_req_drop(bp, req);
15297 }
15298 
15299 static int bnxt_fw_init_one_p1(struct bnxt *bp)
15300 {
15301 	int rc;
15302 
15303 	bp->fw_cap = 0;
15304 	rc = bnxt_hwrm_ver_get(bp);
15305 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
15306 	 * so wait before continuing with recovery.
15307 	 */
15308 	if (rc)
15309 		msleep(100);
15310 	bnxt_try_map_fw_health_reg(bp);
15311 	if (rc) {
15312 		rc = bnxt_try_recover_fw(bp);
15313 		if (rc)
15314 			return rc;
15315 		rc = bnxt_hwrm_ver_get(bp);
15316 		if (rc)
15317 			return rc;
15318 	}
15319 
15320 	bnxt_nvm_cfg_ver_get(bp);
15321 
15322 	rc = bnxt_hwrm_func_reset(bp);
15323 	if (rc)
15324 		return -ENODEV;
15325 
15326 	bnxt_hwrm_fw_set_time(bp);
15327 	return 0;
15328 }
15329 
15330 static int bnxt_fw_init_one_p2(struct bnxt *bp)
15331 {
15332 	int rc;
15333 
15334 	/* Get the MAX capabilities for this function */
15335 	rc = bnxt_hwrm_func_qcaps(bp);
15336 	if (rc) {
15337 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
15338 			   rc);
15339 		return -ENODEV;
15340 	}
15341 
15342 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
15343 	if (rc)
15344 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
15345 			    rc);
15346 
15347 	if (bnxt_alloc_fw_health(bp)) {
15348 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
15349 	} else {
15350 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
15351 		if (rc)
15352 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
15353 				    rc);
15354 	}
15355 
15356 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
15357 	if (rc)
15358 		return -ENODEV;
15359 
15360 	rc = bnxt_alloc_crash_dump_mem(bp);
15361 	if (rc)
15362 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
15363 			    rc);
15364 	if (!rc) {
15365 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
15366 		if (rc) {
15367 			bnxt_free_crash_dump_mem(bp);
15368 			netdev_warn(bp->dev,
15369 				    "hwrm crash dump mem failure rc: %d\n", rc);
15370 		}
15371 	}
15372 
15373 	if (bnxt_fw_pre_resv_vnics(bp))
15374 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
15375 
15376 	bnxt_hwrm_pfcwd_qcaps(bp);
15377 	bnxt_hwrm_func_qcfg(bp);
15378 	bnxt_hwrm_vnic_qcaps(bp);
15379 	bnxt_hwrm_port_led_qcaps(bp);
15380 	bnxt_ethtool_init(bp);
15381 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
15382 		__bnxt_hwrm_ptp_qcfg(bp);
15383 	bnxt_dcb_init(bp);
15384 	bnxt_hwmon_init(bp);
15385 	return 0;
15386 }
15387 
15388 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
15389 {
15390 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
15391 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
15392 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
15393 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
15394 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
15395 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
15396 		bp->rss_hash_delta = bp->rss_hash_cfg;
15397 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
15398 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
15399 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
15400 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
15401 	}
15402 }
15403 
15404 static void bnxt_set_dflt_rfs(struct bnxt *bp)
15405 {
15406 	struct net_device *dev = bp->dev;
15407 
15408 	dev->hw_features &= ~NETIF_F_NTUPLE;
15409 	dev->features &= ~NETIF_F_NTUPLE;
15410 	bp->flags &= ~BNXT_FLAG_RFS;
15411 	if (bnxt_rfs_supported(bp)) {
15412 		dev->hw_features |= NETIF_F_NTUPLE;
15413 		if (bnxt_rfs_capable(bp, false)) {
15414 			bp->flags |= BNXT_FLAG_RFS;
15415 			dev->features |= NETIF_F_NTUPLE;
15416 		}
15417 	}
15418 }
15419 
15420 static void bnxt_fw_init_one_p3(struct bnxt *bp)
15421 {
15422 	struct pci_dev *pdev = bp->pdev;
15423 
15424 	bnxt_set_dflt_rss_hash_type(bp);
15425 	bnxt_set_dflt_rfs(bp);
15426 
15427 	bnxt_get_wol_settings(bp);
15428 	if (bp->flags & BNXT_FLAG_WOL_CAP)
15429 		device_set_wakeup_enable(&pdev->dev, bp->wol);
15430 	else
15431 		device_set_wakeup_capable(&pdev->dev, false);
15432 
15433 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
15434 	bnxt_hwrm_coal_params_qcaps(bp);
15435 }
15436 
15437 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
15438 
15439 int bnxt_fw_init_one(struct bnxt *bp)
15440 {
15441 	int rc;
15442 
15443 	rc = bnxt_fw_init_one_p1(bp);
15444 	if (rc) {
15445 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
15446 		return rc;
15447 	}
15448 	rc = bnxt_fw_init_one_p2(bp);
15449 	if (rc) {
15450 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
15451 		return rc;
15452 	}
15453 	rc = bnxt_probe_phy(bp, false);
15454 	if (rc)
15455 		return rc;
15456 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
15457 	if (rc)
15458 		return rc;
15459 
15460 	bnxt_fw_init_one_p3(bp);
15461 	return 0;
15462 }
15463 
15464 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
15465 {
15466 	struct bnxt_fw_health *fw_health = bp->fw_health;
15467 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
15468 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
15469 	u32 reg_type, reg_off, delay_msecs;
15470 
15471 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
15472 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
15473 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
15474 	switch (reg_type) {
15475 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
15476 		pci_write_config_dword(bp->pdev, reg_off, val);
15477 		break;
15478 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
15479 		writel(reg_off & BNXT_GRC_BASE_MASK,
15480 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
15481 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
15482 		fallthrough;
15483 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
15484 		writel(val, bp->bar0 + reg_off);
15485 		break;
15486 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
15487 		writel(val, bp->bar1 + reg_off);
15488 		break;
15489 	}
15490 	if (delay_msecs) {
15491 		pci_read_config_dword(bp->pdev, 0, &val);
15492 		msleep(delay_msecs);
15493 	}
15494 }
15495 
15496 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
15497 {
15498 	struct hwrm_func_qcfg_output *resp;
15499 	struct hwrm_func_qcfg_input *req;
15500 	bool result = true; /* firmware will enforce if unknown */
15501 
15502 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
15503 		return result;
15504 
15505 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
15506 		return result;
15507 
15508 	req->fid = cpu_to_le16(0xffff);
15509 	resp = hwrm_req_hold(bp, req);
15510 	if (!hwrm_req_send(bp, req))
15511 		result = !!(le16_to_cpu(resp->flags) &
15512 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
15513 	hwrm_req_drop(bp, req);
15514 	return result;
15515 }
15516 
15517 static void bnxt_reset_all(struct bnxt *bp)
15518 {
15519 	struct bnxt_fw_health *fw_health = bp->fw_health;
15520 	int i, rc;
15521 
15522 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15523 		bnxt_fw_reset_via_optee(bp);
15524 		bp->fw_reset_timestamp = jiffies;
15525 		return;
15526 	}
15527 
15528 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
15529 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
15530 			bnxt_fw_reset_writel(bp, i);
15531 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
15532 		struct hwrm_fw_reset_input *req;
15533 
15534 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
15535 		if (!rc) {
15536 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
15537 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
15538 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
15539 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
15540 			rc = hwrm_req_send(bp, req);
15541 		}
15542 		if (rc != -ENODEV)
15543 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
15544 	}
15545 	bp->fw_reset_timestamp = jiffies;
15546 }
15547 
15548 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
15549 {
15550 	return time_after(jiffies, bp->fw_reset_timestamp +
15551 			  (bp->fw_reset_max_dsecs * HZ / 10));
15552 }
15553 
15554 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
15555 {
15556 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15557 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
15558 		bnxt_dl_health_fw_status_update(bp, false);
15559 	bp->fw_reset_state = BNXT_FW_RESET_STATE_ABORT;
15560 	netif_close(bp->dev);
15561 }
15562 
15563 static void bnxt_fw_reset_task(struct work_struct *work)
15564 {
15565 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
15566 	int rc = 0;
15567 
15568 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
15569 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
15570 		return;
15571 	}
15572 
15573 	switch (bp->fw_reset_state) {
15574 	case BNXT_FW_RESET_STATE_POLL_VF: {
15575 		int n = bnxt_get_registered_vfs(bp);
15576 		int tmo;
15577 
15578 		if (n < 0) {
15579 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
15580 				   n, jiffies_to_msecs(jiffies -
15581 				   bp->fw_reset_timestamp));
15582 			goto fw_reset_abort;
15583 		} else if (n > 0) {
15584 			if (bnxt_fw_reset_timeout(bp)) {
15585 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15586 				bp->fw_reset_state = 0;
15587 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
15588 					   n);
15589 				goto ulp_start;
15590 			}
15591 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15592 			return;
15593 		}
15594 		bp->fw_reset_timestamp = jiffies;
15595 		netdev_lock(bp->dev);
15596 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
15597 			netdev_unlock(bp->dev);
15598 			goto fw_reset_abort;
15599 		}
15600 		bnxt_fw_reset_close(bp);
15601 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15602 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
15603 			tmo = HZ / 10;
15604 		} else {
15605 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15606 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
15607 		}
15608 		netdev_unlock(bp->dev);
15609 		bnxt_queue_fw_reset_work(bp, tmo);
15610 		return;
15611 	}
15612 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
15613 		u32 val;
15614 
15615 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15616 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
15617 		    !bnxt_fw_reset_timeout(bp)) {
15618 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15619 			return;
15620 		}
15621 
15622 		if (!bp->fw_health->primary) {
15623 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
15624 
15625 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15626 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
15627 			return;
15628 		}
15629 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
15630 	}
15631 		fallthrough;
15632 	case BNXT_FW_RESET_STATE_RESET_FW:
15633 		bnxt_reset_all(bp);
15634 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15635 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
15636 		return;
15637 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
15638 		bnxt_inv_fw_health_reg(bp);
15639 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
15640 		    !bp->fw_reset_min_dsecs) {
15641 			u16 val;
15642 
15643 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
15644 			if (val == 0xffff) {
15645 				if (bnxt_fw_reset_timeout(bp)) {
15646 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
15647 					rc = -ETIMEDOUT;
15648 					goto fw_reset_abort;
15649 				}
15650 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
15651 				return;
15652 			}
15653 		}
15654 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
15655 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
15656 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
15657 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
15658 			bnxt_dl_remote_reload(bp);
15659 		if (!pci_is_enabled(bp->pdev) && pci_enable_device(bp->pdev)) {
15660 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
15661 			rc = -ENODEV;
15662 			goto fw_reset_abort;
15663 		}
15664 		pci_set_master(bp->pdev);
15665 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
15666 		fallthrough;
15667 	case BNXT_FW_RESET_STATE_POLL_FW:
15668 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
15669 		rc = bnxt_hwrm_poll(bp);
15670 		if (rc) {
15671 			if (bnxt_fw_reset_timeout(bp)) {
15672 				netdev_err(bp->dev, "Firmware reset aborted\n");
15673 				goto fw_reset_abort_status;
15674 			}
15675 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15676 			return;
15677 		}
15678 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
15679 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
15680 		fallthrough;
15681 	case BNXT_FW_RESET_STATE_OPENING:
15682 		while (!rtnl_trylock()) {
15683 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15684 			return;
15685 		}
15686 		netdev_lock(bp->dev);
15687 		rc = bnxt_open(bp->dev);
15688 		if (rc) {
15689 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
15690 			bnxt_fw_reset_abort(bp, rc);
15691 			netdev_unlock(bp->dev);
15692 			rtnl_unlock();
15693 			goto ulp_start;
15694 		}
15695 
15696 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
15697 		    bp->fw_health->enabled) {
15698 			bp->fw_health->last_fw_reset_cnt =
15699 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
15700 		}
15701 		bp->fw_reset_state = 0;
15702 		/* Make sure fw_reset_state is 0 before clearing the flag */
15703 		smp_mb__before_atomic();
15704 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15705 		bnxt_ptp_reapply_pps(bp);
15706 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
15707 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
15708 			bnxt_dl_health_fw_recovery_done(bp);
15709 			bnxt_dl_health_fw_status_update(bp, true);
15710 		}
15711 		netdev_unlock(bp->dev);
15712 		rtnl_unlock();
15713 		bnxt_ulp_start(bp);
15714 		bnxt_reenable_sriov(bp);
15715 		netdev_lock(bp->dev);
15716 		bnxt_vf_reps_alloc(bp);
15717 		bnxt_vf_reps_open(bp);
15718 		netdev_unlock(bp->dev);
15719 		break;
15720 	}
15721 	return;
15722 
15723 fw_reset_abort_status:
15724 	if (bp->fw_health->status_reliable ||
15725 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
15726 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15727 
15728 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
15729 	}
15730 fw_reset_abort:
15731 	rtnl_lock();
15732 	netdev_lock(bp->dev);
15733 	bnxt_fw_reset_abort(bp, rc);
15734 	netdev_unlock(bp->dev);
15735 	rtnl_unlock();
15736 ulp_start:
15737 	if (!rc)
15738 		bnxt_ulp_start(bp);
15739 }
15740 
15741 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
15742 {
15743 	int rc;
15744 	struct bnxt *bp = netdev_priv(dev);
15745 
15746 	SET_NETDEV_DEV(dev, &pdev->dev);
15747 
15748 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
15749 	rc = pci_enable_device(pdev);
15750 	if (rc) {
15751 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
15752 		goto init_err;
15753 	}
15754 
15755 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
15756 		dev_err(&pdev->dev,
15757 			"Cannot find PCI device base address, aborting\n");
15758 		rc = -ENODEV;
15759 		goto init_err_disable;
15760 	}
15761 
15762 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
15763 	if (rc) {
15764 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
15765 		goto init_err_disable;
15766 	}
15767 
15768 	rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15769 	if (rc) {
15770 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
15771 		rc = -EIO;
15772 		goto init_err_release;
15773 	}
15774 
15775 	pci_set_master(pdev);
15776 
15777 	bp->dev = dev;
15778 	bp->pdev = pdev;
15779 
15780 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
15781 	 * determines the BAR size.
15782 	 */
15783 	bp->bar0 = pci_ioremap_bar(pdev, 0);
15784 	if (!bp->bar0) {
15785 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
15786 		rc = -ENOMEM;
15787 		goto init_err_release;
15788 	}
15789 
15790 	bp->bar2 = pci_ioremap_bar(pdev, 4);
15791 	if (!bp->bar2) {
15792 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
15793 		rc = -ENOMEM;
15794 		goto init_err_release;
15795 	}
15796 
15797 	pci_enable_ptm(pdev);
15798 
15799 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
15800 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
15801 
15802 	spin_lock_init(&bp->ntp_fltr_lock);
15803 	spin_lock_init(&bp->stats_lock);
15804 #if BITS_PER_LONG == 32
15805 	spin_lock_init(&bp->db_lock);
15806 #endif
15807 
15808 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
15809 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
15810 
15811 	timer_setup(&bp->timer, bnxt_timer, 0);
15812 	bp->current_interval = BNXT_TIMER_INTERVAL;
15813 
15814 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
15815 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
15816 
15817 	clear_bit(BNXT_STATE_OPEN, &bp->state);
15818 	return 0;
15819 
15820 init_err_release:
15821 	bnxt_unmap_bars(bp, pdev);
15822 	pci_release_regions(pdev);
15823 
15824 init_err_disable:
15825 	pci_disable_device(pdev);
15826 
15827 init_err:
15828 	return rc;
15829 }
15830 
15831 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
15832 {
15833 	struct sockaddr *addr = p;
15834 	struct bnxt *bp = netdev_priv(dev);
15835 	int rc = 0;
15836 
15837 	netdev_assert_locked(dev);
15838 
15839 	if (!is_valid_ether_addr(addr->sa_data))
15840 		return -EADDRNOTAVAIL;
15841 
15842 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
15843 		return 0;
15844 
15845 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
15846 	if (rc)
15847 		return rc;
15848 
15849 	eth_hw_addr_set(dev, addr->sa_data);
15850 	bnxt_clear_usr_fltrs(bp, true);
15851 	if (netif_running(dev)) {
15852 		bnxt_close_nic(bp, false, false);
15853 		rc = bnxt_open_nic(bp, false, false);
15854 	}
15855 
15856 	return rc;
15857 }
15858 
15859 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
15860 {
15861 	struct bnxt *bp = netdev_priv(dev);
15862 
15863 	netdev_assert_locked(dev);
15864 
15865 	if (netif_running(dev))
15866 		bnxt_close_nic(bp, true, false);
15867 
15868 	WRITE_ONCE(dev->mtu, new_mtu);
15869 
15870 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
15871 	 * program is attached.  We need to set the AGG rings settings and
15872 	 * rx_skb_func accordingly.
15873 	 */
15874 	if (READ_ONCE(bp->xdp_prog))
15875 		bnxt_set_rx_skb_mode(bp, true);
15876 
15877 	bnxt_set_ring_params(bp);
15878 
15879 	if (netif_running(dev))
15880 		return bnxt_open_nic(bp, true, false);
15881 
15882 	return 0;
15883 }
15884 
15885 void bnxt_set_cp_rings(struct bnxt *bp, bool sh)
15886 {
15887 	int tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
15888 
15889 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
15890 			       tx_cp + bp->rx_nr_rings;
15891 }
15892 
15893 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
15894 {
15895 	struct bnxt *bp = netdev_priv(dev);
15896 	bool sh = false;
15897 	int rc;
15898 
15899 	if (tc > bp->max_tc) {
15900 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
15901 			   tc, bp->max_tc);
15902 		return -EINVAL;
15903 	}
15904 
15905 	if (bp->num_tc == tc)
15906 		return 0;
15907 
15908 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
15909 		sh = true;
15910 
15911 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
15912 			      sh, tc, bp->tx_nr_rings_xdp);
15913 	if (rc)
15914 		return rc;
15915 
15916 	/* Needs to close the device and do hw resource re-allocations */
15917 	if (netif_running(bp->dev))
15918 		bnxt_close_nic(bp, true, false);
15919 
15920 	if (tc) {
15921 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
15922 		netdev_set_num_tc(dev, tc);
15923 		bp->num_tc = tc;
15924 	} else {
15925 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15926 		netdev_reset_tc(dev);
15927 		bp->num_tc = 0;
15928 	}
15929 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
15930 	bnxt_set_cp_rings(bp, sh);
15931 
15932 	if (netif_running(bp->dev))
15933 		return bnxt_open_nic(bp, true, false);
15934 
15935 	return 0;
15936 }
15937 
15938 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
15939 				  void *cb_priv)
15940 {
15941 	struct bnxt *bp = cb_priv;
15942 
15943 	if (!bnxt_tc_flower_enabled(bp) ||
15944 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
15945 		return -EOPNOTSUPP;
15946 
15947 	switch (type) {
15948 	case TC_SETUP_CLSFLOWER:
15949 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
15950 	default:
15951 		return -EOPNOTSUPP;
15952 	}
15953 }
15954 
15955 LIST_HEAD(bnxt_block_cb_list);
15956 
15957 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
15958 			 void *type_data)
15959 {
15960 	struct bnxt *bp = netdev_priv(dev);
15961 
15962 	switch (type) {
15963 	case TC_SETUP_BLOCK:
15964 		return flow_block_cb_setup_simple(type_data,
15965 						  &bnxt_block_cb_list,
15966 						  bnxt_setup_tc_block_cb,
15967 						  bp, bp, true);
15968 	case TC_SETUP_QDISC_MQPRIO: {
15969 		struct tc_mqprio_qopt *mqprio = type_data;
15970 
15971 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
15972 
15973 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
15974 	}
15975 	default:
15976 		return -EOPNOTSUPP;
15977 	}
15978 }
15979 
15980 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
15981 			    const struct sk_buff *skb)
15982 {
15983 	struct bnxt_vnic_info *vnic;
15984 
15985 	if (skb)
15986 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
15987 
15988 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
15989 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
15990 }
15991 
15992 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
15993 			   u32 idx)
15994 {
15995 	struct hlist_head *head;
15996 	int bit_id;
15997 
15998 	spin_lock_bh(&bp->ntp_fltr_lock);
15999 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
16000 	if (bit_id < 0) {
16001 		spin_unlock_bh(&bp->ntp_fltr_lock);
16002 		return -ENOMEM;
16003 	}
16004 
16005 	fltr->base.sw_id = (u16)bit_id;
16006 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
16007 	fltr->base.flags |= BNXT_ACT_RING_DST;
16008 	head = &bp->ntp_fltr_hash_tbl[idx];
16009 	hlist_add_head_rcu(&fltr->base.hash, head);
16010 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
16011 	bnxt_insert_usr_fltr(bp, &fltr->base);
16012 	bp->ntp_fltr_count++;
16013 	spin_unlock_bh(&bp->ntp_fltr_lock);
16014 	return 0;
16015 }
16016 
16017 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
16018 			    struct bnxt_ntuple_filter *f2)
16019 {
16020 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
16021 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
16022 	struct flow_keys *keys1 = &f1->fkeys;
16023 	struct flow_keys *keys2 = &f2->fkeys;
16024 
16025 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
16026 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
16027 		return false;
16028 
16029 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
16030 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
16031 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
16032 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
16033 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
16034 			return false;
16035 	} else {
16036 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
16037 				     &keys2->addrs.v6addrs.src) ||
16038 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
16039 				     &masks2->addrs.v6addrs.src) ||
16040 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
16041 				     &keys2->addrs.v6addrs.dst) ||
16042 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
16043 				     &masks2->addrs.v6addrs.dst))
16044 			return false;
16045 	}
16046 
16047 	return keys1->ports.src == keys2->ports.src &&
16048 	       masks1->ports.src == masks2->ports.src &&
16049 	       keys1->ports.dst == keys2->ports.dst &&
16050 	       masks1->ports.dst == masks2->ports.dst &&
16051 	       keys1->control.flags == keys2->control.flags &&
16052 	       f1->l2_fltr == f2->l2_fltr;
16053 }
16054 
16055 struct bnxt_ntuple_filter *
16056 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
16057 				struct bnxt_ntuple_filter *fltr, u32 idx)
16058 {
16059 	struct bnxt_ntuple_filter *f;
16060 	struct hlist_head *head;
16061 
16062 	head = &bp->ntp_fltr_hash_tbl[idx];
16063 	hlist_for_each_entry_rcu(f, head, base.hash) {
16064 		if (bnxt_fltr_match(f, fltr))
16065 			return f;
16066 	}
16067 	return NULL;
16068 }
16069 
16070 #ifdef CONFIG_RFS_ACCEL
16071 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
16072 			      u16 rxq_index, u32 flow_id)
16073 {
16074 	struct bnxt *bp = netdev_priv(dev);
16075 	struct bnxt_ntuple_filter *fltr, *new_fltr;
16076 	struct flow_keys *fkeys;
16077 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
16078 	struct bnxt_l2_filter *l2_fltr;
16079 	int rc = 0, idx;
16080 	u32 flags;
16081 
16082 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
16083 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
16084 		atomic_inc(&l2_fltr->refcnt);
16085 	} else {
16086 		struct bnxt_l2_key key;
16087 
16088 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
16089 		key.vlan = 0;
16090 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
16091 		if (!l2_fltr)
16092 			return -EINVAL;
16093 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
16094 			bnxt_del_l2_filter(bp, l2_fltr);
16095 			return -EINVAL;
16096 		}
16097 	}
16098 	new_fltr = kzalloc_obj(*new_fltr, GFP_ATOMIC);
16099 	if (!new_fltr) {
16100 		bnxt_del_l2_filter(bp, l2_fltr);
16101 		return -ENOMEM;
16102 	}
16103 
16104 	fkeys = &new_fltr->fkeys;
16105 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
16106 		rc = -EPROTONOSUPPORT;
16107 		goto err_free;
16108 	}
16109 
16110 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
16111 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
16112 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
16113 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
16114 		rc = -EPROTONOSUPPORT;
16115 		goto err_free;
16116 	}
16117 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
16118 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
16119 		if (bp->hwrm_spec_code < 0x10601) {
16120 			rc = -EPROTONOSUPPORT;
16121 			goto err_free;
16122 		}
16123 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
16124 	}
16125 	flags = fkeys->control.flags;
16126 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
16127 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
16128 		rc = -EPROTONOSUPPORT;
16129 		goto err_free;
16130 	}
16131 	new_fltr->l2_fltr = l2_fltr;
16132 
16133 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
16134 	rcu_read_lock();
16135 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
16136 	if (fltr) {
16137 		rc = fltr->base.sw_id;
16138 		rcu_read_unlock();
16139 		goto err_free;
16140 	}
16141 	rcu_read_unlock();
16142 
16143 	new_fltr->flow_id = flow_id;
16144 	new_fltr->base.rxq = rxq_index;
16145 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
16146 	if (!rc) {
16147 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
16148 		return new_fltr->base.sw_id;
16149 	}
16150 
16151 err_free:
16152 	bnxt_del_l2_filter(bp, l2_fltr);
16153 	kfree(new_fltr);
16154 	return rc;
16155 }
16156 #endif
16157 
16158 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
16159 {
16160 	spin_lock_bh(&bp->ntp_fltr_lock);
16161 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
16162 		spin_unlock_bh(&bp->ntp_fltr_lock);
16163 		return;
16164 	}
16165 	hlist_del_rcu(&fltr->base.hash);
16166 	bnxt_del_one_usr_fltr(bp, &fltr->base);
16167 	bp->ntp_fltr_count--;
16168 	spin_unlock_bh(&bp->ntp_fltr_lock);
16169 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
16170 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
16171 	kfree_rcu(fltr, base.rcu);
16172 }
16173 
16174 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
16175 {
16176 #ifdef CONFIG_RFS_ACCEL
16177 	int i;
16178 
16179 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
16180 		struct hlist_head *head;
16181 		struct hlist_node *tmp;
16182 		struct bnxt_ntuple_filter *fltr;
16183 		int rc;
16184 
16185 		head = &bp->ntp_fltr_hash_tbl[i];
16186 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
16187 			bool del = false;
16188 
16189 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
16190 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
16191 					continue;
16192 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
16193 							fltr->flow_id,
16194 							fltr->base.sw_id)) {
16195 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
16196 									 fltr);
16197 					del = true;
16198 				}
16199 			} else {
16200 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
16201 								       fltr);
16202 				if (rc)
16203 					del = true;
16204 				else
16205 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
16206 			}
16207 
16208 			if (del)
16209 				bnxt_del_ntp_filter(bp, fltr);
16210 		}
16211 	}
16212 #endif
16213 }
16214 
16215 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
16216 				    unsigned int entry, struct udp_tunnel_info *ti)
16217 {
16218 	struct bnxt *bp = netdev_priv(netdev);
16219 	unsigned int cmd;
16220 
16221 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
16222 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
16223 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
16224 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
16225 	else
16226 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
16227 
16228 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
16229 }
16230 
16231 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
16232 				      unsigned int entry, struct udp_tunnel_info *ti)
16233 {
16234 	struct bnxt *bp = netdev_priv(netdev);
16235 	unsigned int cmd;
16236 
16237 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
16238 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
16239 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
16240 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
16241 	else
16242 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
16243 
16244 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
16245 }
16246 
16247 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
16248 	.set_port	= bnxt_udp_tunnel_set_port,
16249 	.unset_port	= bnxt_udp_tunnel_unset_port,
16250 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
16251 	.tables		= {
16252 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
16253 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
16254 	},
16255 }, bnxt_udp_tunnels_p7 = {
16256 	.set_port	= bnxt_udp_tunnel_set_port,
16257 	.unset_port	= bnxt_udp_tunnel_unset_port,
16258 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
16259 	.tables		= {
16260 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
16261 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
16262 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
16263 	},
16264 };
16265 
16266 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
16267 			       struct net_device *dev, u32 filter_mask,
16268 			       int nlflags)
16269 {
16270 	struct bnxt *bp = netdev_priv(dev);
16271 
16272 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
16273 				       nlflags, filter_mask, NULL);
16274 }
16275 
16276 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
16277 			       u16 flags, struct netlink_ext_ack *extack)
16278 {
16279 	struct bnxt *bp = netdev_priv(dev);
16280 	struct nlattr *attr, *br_spec;
16281 	int rem, rc = 0;
16282 
16283 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
16284 		return -EOPNOTSUPP;
16285 
16286 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
16287 	if (!br_spec)
16288 		return -EINVAL;
16289 
16290 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
16291 		u16 mode;
16292 
16293 		mode = nla_get_u16(attr);
16294 		if (mode == bp->br_mode)
16295 			break;
16296 
16297 		rc = bnxt_hwrm_set_br_mode(bp, mode);
16298 		if (!rc)
16299 			bp->br_mode = mode;
16300 		break;
16301 	}
16302 	return rc;
16303 }
16304 
16305 int bnxt_get_port_parent_id(struct net_device *dev,
16306 			    struct netdev_phys_item_id *ppid)
16307 {
16308 	struct bnxt *bp = netdev_priv(dev);
16309 
16310 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
16311 		return -EOPNOTSUPP;
16312 
16313 	/* The PF and it's VF-reps only support the switchdev framework */
16314 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
16315 		return -EOPNOTSUPP;
16316 
16317 	ppid->id_len = sizeof(bp->dsn);
16318 	memcpy(ppid->id, bp->dsn, ppid->id_len);
16319 
16320 	return 0;
16321 }
16322 
16323 static const struct net_device_ops bnxt_netdev_ops = {
16324 	.ndo_open		= bnxt_open,
16325 	.ndo_start_xmit		= bnxt_start_xmit,
16326 	.ndo_stop		= bnxt_close,
16327 	.ndo_get_stats64	= bnxt_get_stats64,
16328 	.ndo_set_rx_mode_async	= bnxt_set_rx_mode,
16329 	.ndo_eth_ioctl		= bnxt_ioctl,
16330 	.ndo_validate_addr	= eth_validate_addr,
16331 	.ndo_set_mac_address	= bnxt_change_mac_addr,
16332 	.ndo_change_mtu		= bnxt_change_mtu,
16333 	.ndo_fix_features	= bnxt_fix_features,
16334 	.ndo_set_features	= bnxt_set_features,
16335 	.ndo_features_check	= bnxt_features_check,
16336 	.ndo_tx_timeout		= bnxt_tx_timeout,
16337 #ifdef CONFIG_BNXT_SRIOV
16338 	.ndo_get_vf_config	= bnxt_get_vf_config,
16339 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
16340 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
16341 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
16342 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
16343 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
16344 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
16345 #endif
16346 	.ndo_setup_tc           = bnxt_setup_tc,
16347 #ifdef CONFIG_RFS_ACCEL
16348 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
16349 #endif
16350 	.ndo_bpf		= bnxt_xdp,
16351 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
16352 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
16353 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
16354 	.ndo_hwtstamp_get	= bnxt_hwtstamp_get,
16355 	.ndo_hwtstamp_set	= bnxt_hwtstamp_set,
16356 };
16357 
16358 static const struct xdp_metadata_ops bnxt_xdp_metadata_ops = {
16359 	.xmo_rx_hash		= bnxt_xdp_rx_hash,
16360 };
16361 
16362 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
16363 				    struct netdev_queue_stats_rx *stats)
16364 {
16365 	struct bnxt *bp = netdev_priv(dev);
16366 	struct bnxt_cp_ring_info *cpr;
16367 	u64 *sw;
16368 
16369 	if (!bp->bnapi)
16370 		return;
16371 
16372 	bnxt_sync_ring_stats(bp);
16373 	cpr = &bp->bnapi[i]->cp_ring;
16374 	sw = cpr->stats.sw_stats;
16375 
16376 	stats->packets = 0;
16377 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
16378 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
16379 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
16380 
16381 	stats->bytes = 0;
16382 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
16383 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
16384 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
16385 
16386 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
16387 	stats->hw_gro_packets = cpr->sw_stats->rx.rx_hw_gro_packets;
16388 	stats->hw_gro_wire_packets = cpr->sw_stats->rx.rx_hw_gro_wire_packets;
16389 }
16390 
16391 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
16392 				    struct netdev_queue_stats_tx *stats)
16393 {
16394 	struct bnxt *bp = netdev_priv(dev);
16395 	struct bnxt_napi *bnapi;
16396 	u64 *sw;
16397 
16398 	if (!bp->tx_ring)
16399 		return;
16400 
16401 	bnxt_sync_ring_stats(bp);
16402 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
16403 	sw = bnapi->cp_ring.stats.sw_stats;
16404 
16405 	stats->packets = 0;
16406 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
16407 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
16408 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
16409 
16410 	stats->bytes = 0;
16411 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
16412 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
16413 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
16414 }
16415 
16416 static void bnxt_get_base_stats(struct net_device *dev,
16417 				struct netdev_queue_stats_rx *rx,
16418 				struct netdev_queue_stats_tx *tx)
16419 {
16420 	struct bnxt *bp = netdev_priv(dev);
16421 
16422 	rx->packets = bp->net_stats_prev.rx_packets;
16423 	rx->bytes = bp->net_stats_prev.rx_bytes;
16424 	rx->alloc_fail = bp->ring_drv_stats_prev.rx_total_oom_discards;
16425 	rx->hw_gro_packets = bp->ring_drv_stats_prev.rx_total_hw_gro_packets;
16426 	rx->hw_gro_wire_packets = bp->ring_drv_stats_prev.rx_total_hw_gro_wire_packets;
16427 
16428 	tx->packets = bp->net_stats_prev.tx_packets;
16429 	tx->bytes = bp->net_stats_prev.tx_bytes;
16430 }
16431 
16432 static const struct netdev_stat_ops bnxt_stat_ops = {
16433 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
16434 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
16435 	.get_base_stats		= bnxt_get_base_stats,
16436 };
16437 
16438 static void bnxt_queue_default_qcfg(struct net_device *dev,
16439 				    struct netdev_queue_config *qcfg)
16440 {
16441 	qcfg->rx_page_size = BNXT_RX_PAGE_SIZE;
16442 }
16443 
16444 static int bnxt_validate_qcfg(struct net_device *dev,
16445 			      struct netdev_queue_config *qcfg,
16446 			      struct netlink_ext_ack *extack)
16447 {
16448 	struct bnxt *bp = netdev_priv(dev);
16449 
16450 	/* Older chips need MSS calc so rx_page_size is not supported */
16451 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16452 	    qcfg->rx_page_size != BNXT_RX_PAGE_SIZE)
16453 		return -EINVAL;
16454 
16455 	if (!is_power_of_2(qcfg->rx_page_size))
16456 		return -ERANGE;
16457 
16458 	if (qcfg->rx_page_size < BNXT_RX_PAGE_SIZE ||
16459 	    qcfg->rx_page_size > BNXT_MAX_RX_PAGE_SIZE)
16460 		return -ERANGE;
16461 
16462 	return 0;
16463 }
16464 
16465 static int bnxt_queue_mem_alloc(struct net_device *dev,
16466 				struct netdev_queue_config *qcfg,
16467 				void *qmem, int idx)
16468 {
16469 	struct bnxt_rx_ring_info *rxr, *clone;
16470 	struct bnxt *bp = netdev_priv(dev);
16471 	struct bnxt_ring_struct *ring;
16472 	int rc;
16473 
16474 	if (!bp->rx_ring)
16475 		return -ENETDOWN;
16476 
16477 	rxr = &bp->rx_ring[idx];
16478 	clone = qmem;
16479 	memcpy(clone, rxr, sizeof(*rxr));
16480 	bnxt_init_rx_ring_struct(bp, clone);
16481 	bnxt_reset_rx_ring_struct(bp, clone);
16482 
16483 	clone->rx_prod = 0;
16484 	clone->rx_agg_prod = 0;
16485 	clone->rx_sw_agg_prod = 0;
16486 	clone->rx_next_cons = 0;
16487 	clone->need_head_pool = false;
16488 	clone->rx_page_size = qcfg->rx_page_size;
16489 	clone->rx_agg_bmap = NULL;
16490 	clone->rx_tpa = NULL;
16491 	clone->rx_tpa_idx_map = NULL;
16492 
16493 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
16494 	if (rc)
16495 		return rc;
16496 
16497 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
16498 	if (rc < 0)
16499 		goto err_page_pool_destroy;
16500 
16501 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
16502 					MEM_TYPE_PAGE_POOL,
16503 					clone->page_pool);
16504 	if (rc)
16505 		goto err_rxq_info_unreg;
16506 
16507 	ring = &clone->rx_ring_struct;
16508 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
16509 	if (rc)
16510 		goto err_free_rx_ring;
16511 
16512 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
16513 		ring = &clone->rx_agg_ring_struct;
16514 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
16515 		if (rc)
16516 			goto err_free_rx_agg_ring;
16517 
16518 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
16519 		if (rc)
16520 			goto err_free_rx_agg_ring;
16521 	}
16522 
16523 	if (bp->flags & BNXT_FLAG_TPA) {
16524 		rc = bnxt_alloc_one_tpa_info(bp, clone);
16525 		if (rc)
16526 			goto err_free_tpa_info;
16527 	}
16528 
16529 	bnxt_init_one_rx_ring_rxbd(bp, clone);
16530 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
16531 
16532 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
16533 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16534 		bnxt_alloc_one_rx_ring_netmem(bp, clone, idx);
16535 	if (bp->flags & BNXT_FLAG_TPA) {
16536 		rc = bnxt_alloc_one_tpa_info_data(bp, clone);
16537 		if (rc)
16538 			goto err_free_rx_ring_skbs;
16539 	}
16540 
16541 	return 0;
16542 
16543 err_free_rx_ring_skbs:
16544 	bnxt_free_one_rx_ring_skbs(bp, clone);
16545 err_free_tpa_info:
16546 	bnxt_free_one_tpa_info(bp, clone);
16547 err_free_rx_agg_ring:
16548 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
16549 	kfree(clone->rx_agg_bmap);
16550 	clone->rx_agg_bmap = NULL;
16551 err_free_rx_ring:
16552 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
16553 err_rxq_info_unreg:
16554 	xdp_rxq_info_unreg(&clone->xdp_rxq);
16555 err_page_pool_destroy:
16556 	page_pool_destroy(clone->page_pool);
16557 	page_pool_destroy(clone->head_pool);
16558 	clone->page_pool = NULL;
16559 	clone->head_pool = NULL;
16560 	return rc;
16561 }
16562 
16563 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
16564 {
16565 	struct bnxt_rx_ring_info *rxr = qmem;
16566 	struct bnxt *bp = netdev_priv(dev);
16567 	struct bnxt_ring_struct *ring;
16568 
16569 	bnxt_free_one_rx_ring_skbs(bp, rxr);
16570 	bnxt_free_one_tpa_info(bp, rxr);
16571 
16572 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
16573 
16574 	page_pool_destroy(rxr->page_pool);
16575 	page_pool_destroy(rxr->head_pool);
16576 	rxr->page_pool = NULL;
16577 	rxr->head_pool = NULL;
16578 
16579 	ring = &rxr->rx_ring_struct;
16580 	bnxt_free_ring(bp, &ring->ring_mem);
16581 
16582 	ring = &rxr->rx_agg_ring_struct;
16583 	bnxt_free_ring(bp, &ring->ring_mem);
16584 
16585 	kfree(rxr->rx_agg_bmap);
16586 	rxr->rx_agg_bmap = NULL;
16587 }
16588 
16589 static void bnxt_copy_rx_ring(struct bnxt *bp,
16590 			      struct bnxt_rx_ring_info *dst,
16591 			      struct bnxt_rx_ring_info *src)
16592 {
16593 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
16594 	struct bnxt_ring_struct *dst_ring, *src_ring;
16595 	int i;
16596 
16597 	dst_ring = &dst->rx_ring_struct;
16598 	dst_rmem = &dst_ring->ring_mem;
16599 	src_ring = &src->rx_ring_struct;
16600 	src_rmem = &src_ring->ring_mem;
16601 
16602 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
16603 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
16604 	WARN_ON(dst_rmem->flags != src_rmem->flags);
16605 	WARN_ON(dst_rmem->depth != src_rmem->depth);
16606 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
16607 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
16608 
16609 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
16610 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
16611 	*dst_rmem->vmem = *src_rmem->vmem;
16612 	for (i = 0; i < dst_rmem->nr_pages; i++) {
16613 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
16614 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
16615 	}
16616 
16617 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
16618 		return;
16619 
16620 	dst_ring = &dst->rx_agg_ring_struct;
16621 	dst_rmem = &dst_ring->ring_mem;
16622 	src_ring = &src->rx_agg_ring_struct;
16623 	src_rmem = &src_ring->ring_mem;
16624 
16625 	dst->rx_page_size = src->rx_page_size;
16626 
16627 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
16628 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
16629 	WARN_ON(dst_rmem->flags != src_rmem->flags);
16630 	WARN_ON(dst_rmem->depth != src_rmem->depth);
16631 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
16632 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
16633 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
16634 
16635 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
16636 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
16637 	*dst_rmem->vmem = *src_rmem->vmem;
16638 	for (i = 0; i < dst_rmem->nr_pages; i++) {
16639 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
16640 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
16641 	}
16642 
16643 	dst->rx_agg_bmap = src->rx_agg_bmap;
16644 }
16645 
16646 static int bnxt_queue_start(struct net_device *dev,
16647 			    struct netdev_queue_config *qcfg,
16648 			    void *qmem, int idx)
16649 {
16650 	struct bnxt *bp = netdev_priv(dev);
16651 	struct bnxt_rx_ring_info *rxr, *clone;
16652 	struct bnxt_cp_ring_info *cpr;
16653 	struct bnxt_vnic_info *vnic;
16654 	struct bnxt_napi *bnapi;
16655 	int i, rc;
16656 	u16 mru;
16657 
16658 	rxr = &bp->rx_ring[idx];
16659 	clone = qmem;
16660 
16661 	rxr->rx_prod = clone->rx_prod;
16662 	rxr->rx_agg_prod = clone->rx_agg_prod;
16663 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
16664 	rxr->rx_next_cons = clone->rx_next_cons;
16665 	rxr->rx_tpa = clone->rx_tpa;
16666 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
16667 	rxr->page_pool = clone->page_pool;
16668 	rxr->head_pool = clone->head_pool;
16669 	rxr->xdp_rxq = clone->xdp_rxq;
16670 	rxr->need_head_pool = clone->need_head_pool;
16671 
16672 	bnxt_copy_rx_ring(bp, rxr, clone);
16673 
16674 	bnapi = rxr->bnapi;
16675 	cpr = &bnapi->cp_ring;
16676 
16677 	/* All rings have been reserved and previously allocated.
16678 	 * Reallocating with the same parameters should never fail.
16679 	 */
16680 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
16681 	if (rc)
16682 		goto err_reset;
16683 
16684 	if (bp->tph_mode) {
16685 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
16686 		if (rc)
16687 			goto err_reset;
16688 
16689 		rc = bnxt_hwrm_set_rx_ring_coal(bp, bnapi);
16690 		if (rc)
16691 			netdev_warn(bp->dev, "HWRM set coalescing failure rc: %d\n",
16692 				    rc);
16693 	}
16694 
16695 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
16696 	if (rc)
16697 		goto err_reset;
16698 
16699 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
16700 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16701 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
16702 
16703 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
16704 		rc = bnxt_tx_queue_start(bp, idx);
16705 		if (rc)
16706 			goto err_reset;
16707 	}
16708 
16709 	bnxt_enable_rx_page_pool(rxr);
16710 	napi_enable_locked(&bnapi->napi);
16711 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16712 
16713 	mru = bp->dev->mtu + VLAN_ETH_HLEN;
16714 	for (i = 0; i < bp->nr_vnics; i++) {
16715 		vnic = &bp->vnic_info[i];
16716 
16717 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, idx);
16718 		if (rc)
16719 			return rc;
16720 	}
16721 	return bnxt_set_rss_ctx_vnic_mru(bp, mru, idx);
16722 
16723 err_reset:
16724 	netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n",
16725 		   rc);
16726 	napi_enable_locked(&bnapi->napi);
16727 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16728 	netif_close(dev);
16729 	return rc;
16730 }
16731 
16732 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
16733 {
16734 	struct bnxt *bp = netdev_priv(dev);
16735 	struct bnxt_rx_ring_info *rxr;
16736 	struct bnxt_cp_ring_info *cpr;
16737 	struct bnxt_vnic_info *vnic;
16738 	struct bnxt_napi *bnapi;
16739 	int i;
16740 
16741 	for (i = 0; i < bp->nr_vnics; i++) {
16742 		vnic = &bp->vnic_info[i];
16743 
16744 		bnxt_set_vnic_mru_p5(bp, vnic, 0, idx);
16745 	}
16746 	bnxt_set_rss_ctx_vnic_mru(bp, 0, idx);
16747 	/* Make sure NAPI sees that the VNIC is disabled */
16748 	synchronize_net();
16749 	rxr = &bp->rx_ring[idx];
16750 	bnapi = rxr->bnapi;
16751 	cpr = &bnapi->cp_ring;
16752 	cancel_work_sync(&cpr->dim.work);
16753 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
16754 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
16755 	page_pool_disable_direct_recycling(rxr->page_pool);
16756 	if (bnxt_separate_head_pool(rxr))
16757 		page_pool_disable_direct_recycling(rxr->head_pool);
16758 
16759 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
16760 		bnxt_tx_queue_stop(bp, idx);
16761 
16762 	/* Disable NAPI now after freeing the rings because HWRM_RING_FREE
16763 	 * completion is handled in NAPI to guarantee no more DMA on that ring
16764 	 * after seeing the completion.
16765 	 */
16766 	napi_disable_locked(&bnapi->napi);
16767 
16768 	if (bp->tph_mode) {
16769 		bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr);
16770 		bnxt_clear_one_cp_ring(bp, rxr->rx_cpr);
16771 	}
16772 	bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
16773 
16774 	memcpy(qmem, rxr, sizeof(*rxr));
16775 	bnxt_init_rx_ring_struct(bp, qmem);
16776 
16777 	return 0;
16778 }
16779 
16780 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
16781 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
16782 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
16783 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
16784 	.ndo_queue_start	= bnxt_queue_start,
16785 	.ndo_queue_stop		= bnxt_queue_stop,
16786 	.ndo_default_qcfg	= bnxt_queue_default_qcfg,
16787 	.ndo_validate_qcfg	= bnxt_validate_qcfg,
16788 	.supported_params	= QCFG_RX_PAGE_SIZE,
16789 };
16790 
16791 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops_unsupp = {
16792 	.ndo_default_qcfg	= bnxt_queue_default_qcfg,
16793 };
16794 
16795 static void bnxt_remove_one(struct pci_dev *pdev)
16796 {
16797 	struct net_device *dev = pci_get_drvdata(pdev);
16798 	struct bnxt *bp = netdev_priv(dev);
16799 
16800 	if (BNXT_PF(bp))
16801 		__bnxt_sriov_disable(bp);
16802 
16803 	bnxt_aux_devices_del(bp);
16804 
16805 	unregister_netdev(dev);
16806 	bnxt_ptp_clear(bp);
16807 
16808 	bnxt_aux_devices_uninit(bp);
16809 	bnxt_auxdev_id_free(bp, bp->auxdev_id);
16810 
16811 	bnxt_free_l2_filters(bp, true);
16812 	bnxt_free_ntp_fltrs(bp, true);
16813 	WARN_ON(bp->num_rss_ctx);
16814 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16815 	/* Flush any pending tasks */
16816 	cancel_work_sync(&bp->sp_task);
16817 	cancel_delayed_work_sync(&bp->fw_reset_task);
16818 	bp->sp_event = 0;
16819 
16820 	bnxt_dl_fw_reporters_destroy(bp);
16821 	bnxt_dl_unregister(bp);
16822 	bnxt_shutdown_tc(bp);
16823 
16824 	bnxt_clear_int_mode(bp);
16825 	bnxt_free_ring_cpu_masks(bp);
16826 	bnxt_hwrm_func_drv_unrgtr(bp);
16827 	bnxt_free_hwrm_resources(bp);
16828 	bnxt_hwmon_uninit(bp);
16829 	bnxt_ethtool_free(bp);
16830 	bnxt_dcb_free(bp);
16831 	kfree(bp->ptp_cfg);
16832 	bp->ptp_cfg = NULL;
16833 	kfree(bp->fw_health);
16834 	bp->fw_health = NULL;
16835 	bnxt_cleanup_pci(bp);
16836 	bnxt_free_ctx_mem(bp, true);
16837 	bnxt_free_crash_dump_mem(bp);
16838 	kfree(bp->rss_indir_tbl);
16839 	bp->rss_indir_tbl = NULL;
16840 	bnxt_free_port_stats(bp);
16841 	free_netdev(dev);
16842 }
16843 
16844 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
16845 {
16846 	int rc = 0;
16847 	struct bnxt_link_info *link_info = &bp->link_info;
16848 
16849 	bp->phy_flags = 0;
16850 	rc = bnxt_hwrm_phy_qcaps(bp);
16851 	if (rc) {
16852 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
16853 			   rc);
16854 		return rc;
16855 	}
16856 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
16857 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
16858 	else
16859 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
16860 
16861 	bp->mac_flags = 0;
16862 	bnxt_hwrm_mac_qcaps(bp);
16863 
16864 	if (!fw_dflt)
16865 		return 0;
16866 
16867 	mutex_lock(&bp->link_lock);
16868 	rc = bnxt_update_link(bp, false);
16869 	if (rc) {
16870 		mutex_unlock(&bp->link_lock);
16871 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
16872 			   rc);
16873 		return rc;
16874 	}
16875 
16876 	/* Older firmware does not have supported_auto_speeds, so assume
16877 	 * that all supported speeds can be autonegotiated.
16878 	 */
16879 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
16880 		link_info->support_auto_speeds = link_info->support_speeds;
16881 
16882 	bnxt_init_ethtool_link_settings(bp);
16883 	mutex_unlock(&bp->link_lock);
16884 	return 0;
16885 }
16886 
16887 static int bnxt_get_max_irq(struct pci_dev *pdev)
16888 {
16889 	u16 ctrl;
16890 
16891 	if (!pdev->msix_cap)
16892 		return 1;
16893 
16894 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
16895 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
16896 }
16897 
16898 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16899 				int *max_cp)
16900 {
16901 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
16902 	int max_ring_grps = 0, max_irq;
16903 
16904 	*max_tx = hw_resc->max_tx_rings;
16905 	*max_rx = hw_resc->max_rx_rings;
16906 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
16907 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
16908 			bnxt_get_ulp_msix_num_in_use(bp),
16909 			hw_resc->max_stat_ctxs -
16910 			bnxt_get_ulp_stat_ctxs_in_use(bp));
16911 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
16912 		*max_cp = min_t(int, *max_cp, max_irq);
16913 	max_ring_grps = hw_resc->max_hw_ring_grps;
16914 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
16915 		*max_cp -= 1;
16916 		*max_rx -= 2;
16917 	}
16918 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16919 		*max_rx >>= 1;
16920 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
16921 		int rc;
16922 
16923 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
16924 		if (rc) {
16925 			*max_rx = 0;
16926 			*max_tx = 0;
16927 		}
16928 		/* On P5 chips, max_cp output param should be available NQs */
16929 		*max_cp = max_irq;
16930 	}
16931 	*max_rx = min_t(int, *max_rx, max_ring_grps);
16932 }
16933 
16934 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
16935 {
16936 	int rx, tx, cp;
16937 
16938 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
16939 	*max_rx = rx;
16940 	*max_tx = tx;
16941 	if (!rx || !tx || !cp)
16942 		return -ENOMEM;
16943 
16944 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
16945 }
16946 
16947 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16948 			       bool shared)
16949 {
16950 	int rc;
16951 
16952 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16953 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
16954 		/* Not enough rings, try disabling agg rings. */
16955 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
16956 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16957 		if (rc) {
16958 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
16959 			bp->flags |= BNXT_FLAG_AGG_RINGS;
16960 			return rc;
16961 		}
16962 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
16963 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16964 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16965 		bnxt_set_ring_params(bp);
16966 	}
16967 
16968 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
16969 		int max_cp, max_stat, max_irq;
16970 
16971 		/* Reserve minimum resources for RoCE */
16972 		max_cp = bnxt_get_max_func_cp_rings(bp);
16973 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
16974 		max_irq = bnxt_get_max_func_irqs(bp);
16975 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
16976 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
16977 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
16978 			return 0;
16979 
16980 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
16981 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
16982 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
16983 		max_cp = min_t(int, max_cp, max_irq);
16984 		max_cp = min_t(int, max_cp, max_stat);
16985 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
16986 		if (rc)
16987 			rc = 0;
16988 	}
16989 	return rc;
16990 }
16991 
16992 /* In initial default shared ring setting, each shared ring must have a
16993  * RX/TX ring pair.
16994  */
16995 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
16996 {
16997 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
16998 	bp->rx_nr_rings = bp->cp_nr_rings;
16999 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
17000 	bp->tx_nr_rings = bnxt_tx_nr_rings(bp);
17001 }
17002 
17003 static void bnxt_adj_dflt_rings(struct bnxt *bp, bool sh)
17004 {
17005 	if (sh)
17006 		bnxt_trim_dflt_sh_rings(bp);
17007 	else
17008 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
17009 	bp->tx_nr_rings = bnxt_tx_nr_rings(bp);
17010 	if (sh && READ_ONCE(bp->xdp_prog)) {
17011 		bnxt_set_xdp_tx_rings(bp);
17012 		bnxt_set_cp_rings(bp, true);
17013 	}
17014 }
17015 
17016 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
17017 {
17018 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
17019 	int avail_msix;
17020 
17021 	if (!bnxt_can_reserve_rings(bp))
17022 		return 0;
17023 
17024 	if (sh)
17025 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
17026 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
17027 	/* Reduce default rings on multi-port cards so that total default
17028 	 * rings do not exceed CPU count.
17029 	 */
17030 	if (bp->port_count > 1) {
17031 		int max_rings =
17032 			max_t(int, num_online_cpus() / bp->port_count, 1);
17033 
17034 		dflt_rings = min_t(int, dflt_rings, max_rings);
17035 	}
17036 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
17037 	if (rc)
17038 		return rc;
17039 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
17040 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
17041 
17042 	bnxt_adj_dflt_rings(bp, sh);
17043 
17044 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
17045 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
17046 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
17047 
17048 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
17049 		bnxt_set_dflt_ulp_stat_ctxs(bp);
17050 	}
17051 
17052 	rc = __bnxt_reserve_rings(bp);
17053 	if (rc && rc != -ECOMM)
17054 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
17055 
17056 	bnxt_adj_tx_rings(bp);
17057 	if (sh)
17058 		bnxt_adj_dflt_rings(bp, true);
17059 
17060 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
17061 	if (bnxt_need_reserve_rings(bp)) {
17062 		rc = __bnxt_reserve_rings(bp);
17063 		if (rc && rc != -ECOMM)
17064 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
17065 		bnxt_adj_tx_rings(bp);
17066 	}
17067 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
17068 		bp->rx_nr_rings++;
17069 		bp->cp_nr_rings++;
17070 	}
17071 	if (rc) {
17072 		bp->tx_nr_rings = 0;
17073 		bp->rx_nr_rings = 0;
17074 	}
17075 	return rc;
17076 }
17077 
17078 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
17079 {
17080 	int rc;
17081 
17082 	if (bp->tx_nr_rings)
17083 		return 0;
17084 
17085 	bnxt_ulp_irq_stop(bp);
17086 	bnxt_clear_int_mode(bp);
17087 	rc = bnxt_set_dflt_rings(bp, true);
17088 	if (rc) {
17089 		if (rc == -ECOMM)
17090 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
17091 		else
17092 			netdev_err(bp->dev, "Not enough rings available.\n");
17093 		goto init_dflt_ring_err;
17094 	}
17095 	rc = bnxt_init_int_mode(bp);
17096 	if (rc)
17097 		goto init_dflt_ring_err;
17098 
17099 	bnxt_adj_tx_rings(bp);
17100 
17101 	bnxt_set_dflt_rfs(bp);
17102 
17103 init_dflt_ring_err:
17104 	bnxt_ulp_irq_restart(bp, rc);
17105 	return rc;
17106 }
17107 
17108 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
17109 {
17110 	int rc;
17111 
17112 	netdev_assert_locked_ops_compat(bp->dev);
17113 	bnxt_hwrm_func_qcaps(bp);
17114 
17115 	if (netif_running(bp->dev))
17116 		__bnxt_close_nic(bp, true, false);
17117 
17118 	bnxt_ulp_irq_stop(bp);
17119 	bnxt_clear_int_mode(bp);
17120 	rc = bnxt_init_int_mode(bp);
17121 	bnxt_ulp_irq_restart(bp, rc);
17122 
17123 	if (netif_running(bp->dev)) {
17124 		if (rc)
17125 			netif_close(bp->dev);
17126 		else
17127 			rc = bnxt_open_nic(bp, true, false);
17128 	}
17129 
17130 	return rc;
17131 }
17132 
17133 static int bnxt_init_mac_addr(struct bnxt *bp)
17134 {
17135 	int rc = 0;
17136 
17137 	if (BNXT_PF(bp)) {
17138 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
17139 	} else {
17140 #ifdef CONFIG_BNXT_SRIOV
17141 		struct bnxt_vf_info *vf = &bp->vf;
17142 		bool strict_approval = true;
17143 
17144 		if (is_valid_ether_addr(vf->mac_addr)) {
17145 			/* overwrite netdev dev_addr with admin VF MAC */
17146 			eth_hw_addr_set(bp->dev, vf->mac_addr);
17147 			/* Older PF driver or firmware may not approve this
17148 			 * correctly.
17149 			 */
17150 			strict_approval = false;
17151 		} else {
17152 			eth_hw_addr_random(bp->dev);
17153 		}
17154 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
17155 #endif
17156 	}
17157 	return rc;
17158 }
17159 
17160 static void bnxt_vpd_read_info(struct bnxt *bp)
17161 {
17162 	struct pci_dev *pdev = bp->pdev;
17163 	unsigned int vpd_size, kw_len;
17164 	int pos, size;
17165 	u8 *vpd_data;
17166 
17167 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
17168 	if (IS_ERR(vpd_data)) {
17169 		pci_warn(pdev, "Unable to read VPD\n");
17170 		return;
17171 	}
17172 
17173 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
17174 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
17175 	if (pos < 0)
17176 		goto read_sn;
17177 
17178 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
17179 	memcpy(bp->board_partno, &vpd_data[pos], size);
17180 
17181 read_sn:
17182 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
17183 					   PCI_VPD_RO_KEYWORD_SERIALNO,
17184 					   &kw_len);
17185 	if (pos < 0)
17186 		goto exit;
17187 
17188 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
17189 	memcpy(bp->board_serialno, &vpd_data[pos], size);
17190 exit:
17191 	kfree(vpd_data);
17192 }
17193 
17194 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
17195 {
17196 	struct pci_dev *pdev = bp->pdev;
17197 	u64 qword;
17198 
17199 	qword = pci_get_dsn(pdev);
17200 	if (!qword) {
17201 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
17202 		return -EOPNOTSUPP;
17203 	}
17204 
17205 	put_unaligned_le64(qword, dsn);
17206 
17207 	bp->flags |= BNXT_FLAG_DSN_VALID;
17208 	return 0;
17209 }
17210 
17211 static int bnxt_map_db_bar(struct bnxt *bp)
17212 {
17213 	if (!bp->db_size)
17214 		return -ENODEV;
17215 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
17216 	if (!bp->bar1)
17217 		return -ENOMEM;
17218 	return 0;
17219 }
17220 
17221 void bnxt_print_device_info(struct bnxt *bp)
17222 {
17223 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
17224 		    board_info[bp->board_idx].name,
17225 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
17226 
17227 	pcie_print_link_status(bp->pdev);
17228 }
17229 
17230 static void bnxt_clear_bars(struct pci_dev *pdev)
17231 {
17232 	int off;
17233 
17234 	for (off = PCI_BASE_ADDRESS_0; off <= PCI_BASE_ADDRESS_5; off += 4)
17235 		pci_write_config_dword(pdev, off, 0);
17236 }
17237 
17238 /* Clear any pending DMA transactions from crash kernel while loading driver in
17239  * capture kernel.
17240  */
17241 static int bnxt_kdump_reset(struct pci_dev *pdev)
17242 {
17243 	int rc, i;
17244 	u16 cmd;
17245 
17246 	pci_read_config_word(pdev, PCI_COMMAND, &cmd);
17247 	cmd &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
17248 	pci_write_config_word(pdev, PCI_COMMAND, cmd);
17249 
17250 	if (pci_save_state(pdev))
17251 		dev_warn(&pdev->dev, "Failed to save PCI state, PCI restore may be incomplete\n");
17252 
17253 	rc = pcie_flr(pdev);
17254 	if (rc)
17255 		dev_warn(&pdev->dev, "pcie_flr() failed (rc: %d), trying to continue\n",
17256 			 rc);
17257 
17258 	/* In case device is not returning CRS, wait 5 seconds longer */
17259 	for (i = 0; i < 50; i++) {
17260 		pci_read_config_word(pdev, PCI_COMMAND, &cmd);
17261 		if (!PCI_POSSIBLE_ERROR(cmd))
17262 			break;
17263 		msleep(100);
17264 	}
17265 	if (PCI_POSSIBLE_ERROR(cmd)) {
17266 		dev_err(&pdev->dev, "PCI config space inaccessible after FLR, aborting\n");
17267 		return -ENODEV;
17268 	}
17269 
17270 	bnxt_clear_bars(pdev);
17271 	pci_restore_state(pdev);
17272 	return 0;
17273 }
17274 
17275 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
17276 {
17277 	struct bnxt_hw_resc *hw_resc;
17278 	struct net_device *dev;
17279 	struct bnxt *bp;
17280 	int rc, max_irqs;
17281 
17282 	if (pci_is_bridge(pdev))
17283 		return -ENODEV;
17284 
17285 	if (!pdev->msix_cap) {
17286 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
17287 		return -ENODEV;
17288 	}
17289 
17290 	if (is_kdump_kernel()) {
17291 		rc = bnxt_kdump_reset(pdev);
17292 		if (rc)
17293 			return rc;
17294 	}
17295 
17296 	max_irqs = bnxt_get_max_irq(pdev);
17297 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
17298 				 max_irqs);
17299 	if (!dev)
17300 		return -ENOMEM;
17301 
17302 	bp = netdev_priv(dev);
17303 	bp->board_idx = ent->driver_data;
17304 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
17305 	bnxt_set_max_func_irqs(bp, max_irqs);
17306 
17307 	bp->max_irqs = max_irqs;
17308 	rc = bnxt_alloc_ring_cpu_masks(bp);
17309 	if (rc)
17310 		goto init_err_free;
17311 
17312 	if (bnxt_vf_pciid(bp->board_idx))
17313 		bp->flags |= BNXT_FLAG_VF;
17314 
17315 	/* No devlink port registration in case of a VF */
17316 	if (BNXT_PF(bp))
17317 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
17318 
17319 	rc = bnxt_init_board(pdev, dev);
17320 	if (rc < 0)
17321 		goto init_err_free;
17322 
17323 	dev->netdev_ops = &bnxt_netdev_ops;
17324 	dev->xdp_metadata_ops = &bnxt_xdp_metadata_ops;
17325 	dev->stat_ops = &bnxt_stat_ops;
17326 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
17327 	dev->ethtool_ops = &bnxt_ethtool_ops;
17328 	pci_set_drvdata(pdev, dev);
17329 
17330 	rc = bnxt_alloc_hwrm_resources(bp);
17331 	if (rc)
17332 		goto init_err_pci_clean;
17333 
17334 	mutex_init(&bp->hwrm_cmd_lock);
17335 	mutex_init(&bp->link_lock);
17336 
17337 	rc = bnxt_fw_init_one_p1(bp);
17338 	if (rc)
17339 		goto init_err_pci_clean;
17340 
17341 	if (BNXT_PF(bp))
17342 		bnxt_vpd_read_info(bp);
17343 
17344 	if (BNXT_CHIP_P5_PLUS(bp)) {
17345 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
17346 		if (BNXT_CHIP_P7(bp))
17347 			bp->flags |= BNXT_FLAG_CHIP_P7;
17348 	}
17349 
17350 	rc = bnxt_alloc_rss_indir_tbl(bp);
17351 	if (rc)
17352 		goto init_err_pci_clean;
17353 
17354 	rc = bnxt_fw_init_one_p2(bp);
17355 	if (rc)
17356 		goto init_err_pci_clean;
17357 
17358 	rc = bnxt_map_db_bar(bp);
17359 	if (rc) {
17360 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
17361 			rc);
17362 		goto init_err_pci_clean;
17363 	}
17364 
17365 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
17366 			   NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN |
17367 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
17368 			   NETIF_F_GSO_IPXIP4 |
17369 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
17370 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
17371 			   NETIF_F_RXCSUM | NETIF_F_GRO;
17372 	dev->hw_features |= NETIF_F_GSO_UDP_L4;
17373 
17374 	if (BNXT_SUPPORTS_TPA(bp))
17375 		dev->hw_features |= NETIF_F_LRO;
17376 
17377 	dev->hw_enc_features =
17378 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
17379 			NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN |
17380 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
17381 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
17382 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
17383 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
17384 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
17385 	if (bp->flags & BNXT_FLAG_CHIP_P7)
17386 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
17387 	else
17388 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
17389 
17390 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
17391 				    NETIF_F_GSO_GRE_CSUM;
17392 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
17393 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
17394 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
17395 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
17396 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
17397 	if (BNXT_SUPPORTS_TPA(bp))
17398 		dev->hw_features |= NETIF_F_GRO_HW;
17399 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
17400 	if (dev->features & NETIF_F_GRO_HW)
17401 		dev->features &= ~NETIF_F_LRO;
17402 	dev->priv_flags |= IFF_UNICAST_FLT;
17403 
17404 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
17405 	if (!(bp->flags & BNXT_FLAG_UDP_GSO_CAP)) {
17406 		u16 max_segs = BNXT_SW_USO_MAX_SEGS;
17407 
17408 		if (bp->tso_max_segs)
17409 			max_segs = min_t(u16, max_segs, bp->tso_max_segs);
17410 		netif_set_tso_max_segs(dev, max_segs);
17411 	} else if (bp->tso_max_segs) {
17412 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
17413 	}
17414 
17415 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
17416 			    NETDEV_XDP_ACT_RX_SG;
17417 
17418 #ifdef CONFIG_BNXT_SRIOV
17419 	init_waitqueue_head(&bp->sriov_cfg_wait);
17420 #endif
17421 	if (BNXT_SUPPORTS_TPA(bp)) {
17422 		bp->gro_func = bnxt_gro_func_5730x;
17423 		if (BNXT_CHIP_P4(bp))
17424 			bp->gro_func = bnxt_gro_func_5731x;
17425 		else if (BNXT_CHIP_P5_PLUS(bp))
17426 			bp->gro_func = bnxt_gro_func_5750x;
17427 	}
17428 	if (!BNXT_CHIP_P4_PLUS(bp))
17429 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
17430 
17431 	rc = bnxt_init_mac_addr(bp);
17432 	if (rc) {
17433 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
17434 		rc = -EADDRNOTAVAIL;
17435 		goto init_err_pci_clean;
17436 	}
17437 
17438 	if (BNXT_PF(bp)) {
17439 		/* Read the adapter's DSN to use as the eswitch switch_id */
17440 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
17441 	}
17442 
17443 	/* MTU range: 60 - FW defined max */
17444 	dev->min_mtu = ETH_ZLEN;
17445 	dev->max_mtu = bp->max_mtu;
17446 
17447 	rc = bnxt_probe_phy(bp, true);
17448 	if (rc)
17449 		goto init_err_pci_clean;
17450 
17451 	hw_resc = &bp->hw_resc;
17452 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
17453 		       BNXT_L2_FLTR_MAX_FLTR;
17454 	/* Older firmware may not report these filters properly */
17455 	if (bp->max_fltr < BNXT_MAX_FLTR)
17456 		bp->max_fltr = BNXT_MAX_FLTR;
17457 	bnxt_init_l2_fltr_tbl(bp);
17458 	__bnxt_set_rx_skb_mode(bp, false);
17459 	bnxt_set_tpa_flags(bp);
17460 	bnxt_init_ring_params(bp);
17461 	bnxt_set_ring_params(bp);
17462 	mutex_init(&bp->auxdev_lock);
17463 	if (!bnxt_auxdev_id_alloc(bp))
17464 		bnxt_aux_devices_init(bp);
17465 	rc = bnxt_set_dflt_rings(bp, true);
17466 	if (rc) {
17467 		if (rc == -ECOMM) {
17468 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
17469 		} else {
17470 			netdev_err(bp->dev, "Not enough rings available.\n");
17471 			rc = -ENOMEM;
17472 		}
17473 		goto init_err_pci_clean;
17474 	}
17475 
17476 	bnxt_fw_init_one_p3(bp);
17477 
17478 	bnxt_init_dflt_coal(bp);
17479 
17480 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
17481 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
17482 
17483 	rc = bnxt_init_int_mode(bp);
17484 	if (rc)
17485 		goto init_err_pci_clean;
17486 
17487 	/* No TC has been set yet and rings may have been trimmed due to
17488 	 * limited MSIX, so we re-initialize the TX rings per TC.
17489 	 */
17490 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
17491 
17492 	if (BNXT_PF(bp)) {
17493 		if (!bnxt_pf_wq) {
17494 			bnxt_pf_wq =
17495 				create_singlethread_workqueue("bnxt_pf_wq");
17496 			if (!bnxt_pf_wq) {
17497 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
17498 				rc = -ENOMEM;
17499 				goto init_err_pci_clean;
17500 			}
17501 		}
17502 		rc = bnxt_init_tc(bp);
17503 		if (rc)
17504 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
17505 				   rc);
17506 	}
17507 
17508 	bnxt_inv_fw_health_reg(bp);
17509 	rc = bnxt_dl_register(bp);
17510 	if (rc)
17511 		goto init_err_dl;
17512 
17513 	INIT_LIST_HEAD(&bp->usr_fltr_list);
17514 
17515 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
17516 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
17517 
17518 	dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops_unsupp;
17519 	if (BNXT_SUPPORTS_QUEUE_API(bp))
17520 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
17521 	dev->netmem_tx = NETMEM_TX_DMA;
17522 
17523 	rc = register_netdev(dev);
17524 	if (rc)
17525 		goto init_err_cleanup;
17526 
17527 	bnxt_dl_fw_reporters_create(bp);
17528 
17529 	bnxt_aux_devices_add(bp);
17530 
17531 	bnxt_print_device_info(bp);
17532 
17533 	pci_save_state(pdev);
17534 
17535 	return 0;
17536 init_err_cleanup:
17537 	bnxt_aux_devices_uninit(bp);
17538 	bnxt_auxdev_id_free(bp, bp->auxdev_id);
17539 	bnxt_dl_unregister(bp);
17540 init_err_dl:
17541 	bnxt_shutdown_tc(bp);
17542 	bnxt_clear_int_mode(bp);
17543 
17544 init_err_pci_clean:
17545 	bnxt_hwrm_func_drv_unrgtr(bp);
17546 	bnxt_ptp_clear(bp);
17547 	kfree(bp->ptp_cfg);
17548 	bp->ptp_cfg = NULL;
17549 	bnxt_free_hwrm_resources(bp);
17550 	bnxt_hwmon_uninit(bp);
17551 	bnxt_ethtool_free(bp);
17552 	kfree(bp->fw_health);
17553 	bp->fw_health = NULL;
17554 	bnxt_cleanup_pci(bp);
17555 	bnxt_free_ctx_mem(bp, true);
17556 	bnxt_free_crash_dump_mem(bp);
17557 	kfree(bp->rss_indir_tbl);
17558 	bp->rss_indir_tbl = NULL;
17559 
17560 init_err_free:
17561 	bnxt_free_ring_cpu_masks(bp);
17562 	free_netdev(dev);
17563 	return rc;
17564 }
17565 
17566 static void bnxt_shutdown(struct pci_dev *pdev)
17567 {
17568 	struct net_device *dev = pci_get_drvdata(pdev);
17569 	struct bnxt *bp;
17570 
17571 	if (!dev)
17572 		return;
17573 
17574 	rtnl_lock();
17575 	netdev_lock(dev);
17576 	bp = netdev_priv(dev);
17577 	if (!bp)
17578 		goto shutdown_exit;
17579 
17580 	if (netif_running(dev))
17581 		netif_close(dev);
17582 
17583 	if (bnxt_hwrm_func_drv_unrgtr(bp)) {
17584 		pcie_flr(pdev);
17585 		goto shutdown_exit;
17586 	}
17587 	bnxt_ptp_clear(bp);
17588 	bnxt_clear_int_mode(bp);
17589 	pci_disable_device(pdev);
17590 
17591 	if (system_state == SYSTEM_POWER_OFF) {
17592 		pci_wake_from_d3(pdev, bp->wol);
17593 		pci_set_power_state(pdev, PCI_D3hot);
17594 	}
17595 
17596 shutdown_exit:
17597 	netdev_unlock(dev);
17598 	rtnl_unlock();
17599 }
17600 
17601 #ifdef CONFIG_PM_SLEEP
17602 static int bnxt_suspend(struct device *device)
17603 {
17604 	struct net_device *dev = dev_get_drvdata(device);
17605 	struct bnxt *bp = netdev_priv(dev);
17606 	int rc = 0;
17607 
17608 	bnxt_ulp_stop(bp);
17609 
17610 	netdev_lock(dev);
17611 	if (netif_running(dev)) {
17612 		netif_device_detach(dev);
17613 		rc = bnxt_close(dev);
17614 	}
17615 	bnxt_hwrm_func_drv_unrgtr(bp);
17616 	bnxt_ptp_clear(bp);
17617 	pci_disable_device(bp->pdev);
17618 	bnxt_free_ctx_mem(bp, false);
17619 	netdev_unlock(dev);
17620 	return rc;
17621 }
17622 
17623 #define BNXT_RESUME_MAX_RETRY	10
17624 
17625 static int bnxt_resume(struct device *device)
17626 {
17627 	struct net_device *dev = dev_get_drvdata(device);
17628 	struct bnxt *bp = netdev_priv(dev);
17629 	int i, rc;
17630 
17631 	rc = pci_enable_device(bp->pdev);
17632 	if (rc) {
17633 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
17634 			   rc);
17635 		return rc;
17636 	}
17637 	pci_set_master(bp->pdev);
17638 	if (BNXT_VF(bp)) {
17639 		for (i = 0; i < BNXT_RESUME_MAX_RETRY; i++) {
17640 			rc = bnxt_hwrm_poll(bp);
17641 			if (rc != -EAGAIN && rc != -EBUSY && rc != -ECOMM)
17642 				break;
17643 
17644 			/* Wait briefly for -EAGAIN (FW busy), a bit longer for
17645 			 * -ECOMM (PF down) to allow the PF to fully initialize.
17646 			 * For -EBUSY, the message typically has timed out, so
17647 			 * try again immediately.
17648 			 */
17649 			if (i < BNXT_RESUME_MAX_RETRY - 1) {
17650 				if (rc == -EAGAIN)
17651 					msleep(250);
17652 				else if (rc == -ECOMM)
17653 					msleep(750);
17654 			}
17655 		}
17656 		if (rc) {
17657 			netdev_err(dev,
17658 				   "Firmware not responding during resume, rc = %d\n",
17659 				   rc);
17660 			return -ENODEV;
17661 		}
17662 	}
17663 
17664 	rtnl_lock();
17665 	netdev_lock(dev);
17666 
17667 	if (bnxt_hwrm_ver_get(bp)) {
17668 		rc = -ENODEV;
17669 		goto resume_exit;
17670 	}
17671 	rc = bnxt_hwrm_func_reset(bp);
17672 	if (rc) {
17673 		rc = -EBUSY;
17674 		goto resume_exit;
17675 	}
17676 
17677 	rc = bnxt_hwrm_func_qcaps(bp);
17678 	if (rc)
17679 		goto resume_exit;
17680 
17681 	bnxt_clear_reservations(bp, true);
17682 
17683 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
17684 		rc = -ENODEV;
17685 		goto resume_exit;
17686 	}
17687 	if (bp->fw_crash_mem)
17688 		bnxt_hwrm_crash_dump_mem_cfg(bp);
17689 
17690 	if (bnxt_ptp_init(bp)) {
17691 		kfree(bp->ptp_cfg);
17692 		bp->ptp_cfg = NULL;
17693 	}
17694 	bnxt_get_wol_settings(bp);
17695 	if (netif_running(dev)) {
17696 		rc = bnxt_open(dev);
17697 		if (!rc)
17698 			netif_device_attach(dev);
17699 	}
17700 
17701 resume_exit:
17702 	netdev_unlock(bp->dev);
17703 	rtnl_unlock();
17704 	if (!rc) {
17705 		bnxt_ulp_start(bp);
17706 		bnxt_reenable_sriov(bp);
17707 	}
17708 	return rc;
17709 }
17710 
17711 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
17712 #define BNXT_PM_OPS (&bnxt_pm_ops)
17713 
17714 #else
17715 
17716 #define BNXT_PM_OPS NULL
17717 
17718 #endif /* CONFIG_PM_SLEEP */
17719 
17720 /**
17721  * bnxt_io_error_detected - called when PCI error is detected
17722  * @pdev: Pointer to PCI device
17723  * @state: The current pci connection state
17724  *
17725  * This function is called after a PCI bus error affecting
17726  * this device has been detected.
17727  */
17728 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
17729 					       pci_channel_state_t state)
17730 {
17731 	struct net_device *netdev = pci_get_drvdata(pdev);
17732 	struct bnxt *bp = netdev_priv(netdev);
17733 	bool abort = false;
17734 
17735 	netdev_info(netdev, "PCI I/O error detected\n");
17736 
17737 	bnxt_ulp_stop(bp);
17738 
17739 	netdev_lock(netdev);
17740 	netif_device_detach(netdev);
17741 
17742 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
17743 		netdev_err(bp->dev, "Firmware reset already in progress\n");
17744 		abort = true;
17745 	}
17746 
17747 	if (abort || state == pci_channel_io_perm_failure) {
17748 		netdev_unlock(netdev);
17749 		return PCI_ERS_RESULT_DISCONNECT;
17750 	}
17751 
17752 	/* Link is not reliable anymore if state is pci_channel_io_frozen
17753 	 * so we disable bus master to prevent any potential bad DMAs before
17754 	 * freeing kernel memory.
17755 	 */
17756 	if (state == pci_channel_io_frozen)
17757 		bnxt_fw_fatal_close(bp);
17758 
17759 	if (netif_running(netdev))
17760 		__bnxt_close_nic(bp, true, true);
17761 
17762 	if (pci_is_enabled(pdev))
17763 		pci_disable_device(pdev);
17764 	bnxt_free_ctx_mem(bp, false);
17765 	netdev_unlock(netdev);
17766 
17767 	/* Request a slot reset. */
17768 	return PCI_ERS_RESULT_NEED_RESET;
17769 }
17770 
17771 /**
17772  * bnxt_io_slot_reset - called after the pci bus has been reset.
17773  * @pdev: Pointer to PCI device
17774  *
17775  * Restart the card from scratch, as if from a cold-boot.
17776  * At this point, the card has experienced a hard reset,
17777  * followed by fixups by BIOS, and has its config space
17778  * set up identically to what it was at cold boot.
17779  */
17780 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
17781 {
17782 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
17783 	struct net_device *netdev = pci_get_drvdata(pdev);
17784 	struct bnxt *bp = netdev_priv(netdev);
17785 	int retry = 0;
17786 	int err = 0;
17787 	u16 cmd;
17788 
17789 	netdev_info(bp->dev, "PCI Slot Reset\n");
17790 
17791 	/* After a PCIe hot reset, the chip should return CRS when the
17792 	 * vendor ID config register is read until it is ready.  On all
17793 	 * chips, this is not happening reliably so add a 5-second delay
17794 	 * as a workaround.
17795 	 */
17796 	msleep(5000);
17797 
17798 	netdev_lock(netdev);
17799 
17800 	pci_read_config_word(pdev, PCI_COMMAND, &cmd);
17801 	if (PCI_POSSIBLE_ERROR(cmd)) {
17802 		dev_err(&pdev->dev,
17803 			"PCI config space inaccessible after reset\n");
17804 		goto reset_exit;
17805 	}
17806 
17807 	/* Upon PCIe error, our device internal logic that latches to
17808 	 * BAR value is getting reset and will restore only upon
17809 	 * rewriting the BARs.
17810 	 *
17811 	 * As pci_restore_state() does not re-write the BARs if the
17812 	 * value is same as saved value earlier, driver needs to
17813 	 * write the BARs to 0 to force restore.
17814 	 */
17815 	pci_clear_master(pdev);
17816 	pci_read_config_word(pdev, PCI_COMMAND, &cmd);
17817 	cmd &= ~PCI_COMMAND_MEMORY;
17818 	pci_write_config_word(pdev, PCI_COMMAND, cmd);
17819 
17820 	bnxt_clear_bars(pdev);
17821 	pci_restore_state(pdev);
17822 
17823 	if (!pci_is_enabled(pdev) && pci_enable_device(pdev)) {
17824 		dev_err(&pdev->dev,
17825 			"Cannot re-enable PCI device after reset.\n");
17826 		pci_read_config_word(pdev, PCI_COMMAND, &cmd);
17827 		if (!PCI_POSSIBLE_ERROR(cmd)) {
17828 			cmd &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
17829 			pci_write_config_word(pdev, PCI_COMMAND, cmd);
17830 		}
17831 		goto reset_exit;
17832 	}
17833 	pci_set_master(pdev);
17834 
17835 	bnxt_inv_fw_health_reg(bp);
17836 	bnxt_try_map_fw_health_reg(bp);
17837 
17838 	/* In some PCIe AER scenarios, firmware may take up to
17839 	 * 10 seconds to become ready in the worst case.
17840 	 */
17841 	do {
17842 		err = bnxt_try_recover_fw(bp);
17843 		if (!err)
17844 			break;
17845 		retry++;
17846 	} while (retry < BNXT_FW_SLOT_RESET_RETRY);
17847 
17848 	if (err) {
17849 		dev_err(&pdev->dev, "Firmware not ready\n");
17850 		goto reset_exit;
17851 	}
17852 
17853 	err = bnxt_hwrm_func_reset(bp);
17854 	if (!err)
17855 		result = PCI_ERS_RESULT_RECOVERED;
17856 
17857 	/* IRQ will be initialized later in bnxt_io_resume */
17858 	bnxt_ulp_irq_stop(bp);
17859 	bnxt_clear_int_mode(bp);
17860 
17861 reset_exit:
17862 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
17863 	bnxt_clear_reservations(bp, true);
17864 	netdev_unlock(netdev);
17865 
17866 	return result;
17867 }
17868 
17869 /**
17870  * bnxt_io_resume - called when traffic can start flowing again.
17871  * @pdev: Pointer to PCI device
17872  *
17873  * This callback is called when the error recovery driver tells
17874  * us that its OK to resume normal operation.
17875  */
17876 static void bnxt_io_resume(struct pci_dev *pdev)
17877 {
17878 	struct net_device *netdev = pci_get_drvdata(pdev);
17879 	struct bnxt *bp = netdev_priv(netdev);
17880 	int err;
17881 
17882 	netdev_info(bp->dev, "PCI Slot Resume\n");
17883 	rtnl_lock();
17884 	netdev_lock(netdev);
17885 
17886 	err = bnxt_hwrm_func_qcaps(bp);
17887 	if (!err) {
17888 		if (netif_running(netdev)) {
17889 			err = bnxt_open(netdev);
17890 		} else {
17891 			err = bnxt_reserve_rings(bp, true);
17892 			if (!err)
17893 				err = bnxt_init_int_mode(bp);
17894 		}
17895 	}
17896 
17897 	if (!err)
17898 		netif_device_attach(netdev);
17899 
17900 	netdev_unlock(netdev);
17901 	rtnl_unlock();
17902 	if (!err) {
17903 		bnxt_ulp_start(bp);
17904 		bnxt_reenable_sriov(bp);
17905 	}
17906 }
17907 
17908 static const struct pci_error_handlers bnxt_err_handler = {
17909 	.error_detected	= bnxt_io_error_detected,
17910 	.slot_reset	= bnxt_io_slot_reset,
17911 	.resume		= bnxt_io_resume
17912 };
17913 
17914 static struct pci_driver bnxt_pci_driver = {
17915 	.name		= DRV_MODULE_NAME,
17916 	.id_table	= bnxt_pci_tbl,
17917 	.probe		= bnxt_init_one,
17918 	.remove		= bnxt_remove_one,
17919 	.shutdown	= bnxt_shutdown,
17920 	.driver.pm	= BNXT_PM_OPS,
17921 	.err_handler	= &bnxt_err_handler,
17922 #if defined(CONFIG_BNXT_SRIOV)
17923 	.sriov_configure = bnxt_sriov_configure,
17924 #endif
17925 };
17926 
17927 static int __init bnxt_init(void)
17928 {
17929 	int err;
17930 
17931 	bnxt_debug_init();
17932 	err = pci_register_driver(&bnxt_pci_driver);
17933 	if (err) {
17934 		bnxt_debug_exit();
17935 		return err;
17936 	}
17937 
17938 	return 0;
17939 }
17940 
17941 static void __exit bnxt_exit(void)
17942 {
17943 	pci_unregister_driver(&bnxt_pci_driver);
17944 	if (bnxt_pf_wq)
17945 		destroy_workqueue(bnxt_pf_wq);
17946 	bnxt_debug_exit();
17947 }
17948 
17949 module_init(bnxt_init);
17950 module_exit(bnxt_exit);
17951